Skip to content

Commit beccdec

Browse files
authored
fix(native-chat): a Codex Stop that Codex refuses or never answers ends the Codex process (#24334)
* fix(codex): a Stop whose interrupt Codex refused or never answered ends the session A Codex Stop is the interrupt alone, so its background terminals keep running. When Codex refused the interrupt, or never answered it, the turn kept running and the chat said "Codex didn't stop" or "Cancellation was not confirmed." with no way to stop it short of closing the chat. Now, after an interrupt that failed, the Stop re-reads the conversation once Codex's frames already received have landed, and ends the child through the host's usual stop (proof of exit, then the lease release) when it still runs what the Stop was sent for. It skips a conversation at rest, a child whose exit already ended its turn, and one Codex has moved on to a different turn. The turn then reads as the user's cancellation. If the child's exit is not proven, the failure is reported and the Stop keeps its "didn't stop" or "not confirmed" row, which is still true. A Stop Codex took keeps the child, unchanged. * fix(codex): end the child only for an interrupt whose effect is unknown, on the turn the Stop meant - Codex's invalid-request refusal (-32600: no active turn, another turn active, thread not loaded) states the named turn is not running, and can arrive before that turn's end frame. The Codex adapter now marks it `turnNotRunning`, and such a refusal never ends the child. Only an internal-error refusal (-32603), an unanswered interrupt, or a thrown cancel does. - A Stop that named a turn, or an unnamed Stop that read one, ends the child only when the journal, after draining received frames, still shows that same turn. A session working on a follow-up whose turn has not opened is no longer ended by a Stop of the finished turn. - When the child's exit was proven and only a later cleanup step failed, the Stop reads as requested; the failure is still reported. - `AgentSessionCancelOutcome` moves beside the adapter's other Stop members (re-exported), which keeps the adapter file within its line budget. * test(codex): pin that a failed interrupt decides on the drained journal The frames Codex sent before the interrupt failed are held in the sink, so a read that skips the drain sees the stopped turn still running. * test(codex): a refusal naming a turn Codex is not running carries turnNotRunning * test(native-chat): fail the route release synchronously, as the adapter's acknowledgement is * docs(codex): a -32600 Codex could not parse also reads as not running and keeps the child * fix(native-chat): a named Stop whose child end is unproven says Codex didn't stop, not that the turn finished The new branch has just read the turn running, so the named Stop's 'already finished' row was false.
1 parent 34a582b commit beccdec

9 files changed

Lines changed: 480 additions & 44 deletions

‎src/main/codex/codex-structured-conversation-stop.test.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ describe('a Codex Stop that names no turn', () => {
5252
detail: {
5353
text: 'expected active turn id turn-journal but found turn-1',
5454
audience: 'person'
55-
}
55+
},
56+
// An invalid-request refusal: the named turn is not the one Codex is running.
57+
turnNotRunning: true
5658
}
5759
})
5860
expect(rig.interrupts().map((call) => call.params?.turnId)).toEqual(['turn-journal'])

‎src/main/codex/codex-structured-session-adapter-fixture.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
type CodexStructuredLaunch,
1414
type CodexStructuredSessionEvent
1515
} from './codex-structured-session-adapter'
16+
import type { CodexStructuredSessionAdapterDeps } from './codex-structured-session-state'
1617

1718
export const THREAD_ID = 'thread-abc'
1819

@@ -104,7 +105,9 @@ export function answerWithOpenedTurn(
104105
export function adapterFor(
105106
codex: ReturnType<typeof fakeCodex>,
106107
launch: Partial<CodexStructuredLaunch> = {},
107-
events: CodexStructuredSessionEvent[] = []
108+
events: CodexStructuredSessionEvent[] = [],
109+
/** Host wiring the runtime adds, such as the late dispatch settlement. */
110+
deps: Partial<CodexStructuredSessionAdapterDeps> = {}
108111
): CodexStructuredSessionAdapter {
109112
let acquisitionGeneration = 0
110113
return new CodexStructuredSessionAdapter({
@@ -120,7 +123,8 @@ export function adapterFor(
120123
openConnection: codex.openConnection,
121124
readProcessStartTime: async () => 1_700_000_000_000,
122125
now: () => 1_700_000_000_500,
123-
mintAcquisitionGeneration: () => `generation-${++acquisitionGeneration}`
126+
mintAcquisitionGeneration: () => `generation-${++acquisitionGeneration}`,
127+
...deps
124128
})
125129
}
126130

‎src/main/codex/codex-structured-turn-cancellation.ts‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,21 @@ import type { AgentSessionCancelOutcome } from '../native-chat/agent-session-wir
55
import type { CodexSession } from './codex-structured-session-state'
66
import type { CodexJournalTranslationAdmission } from './codex-structured-journal-contracts'
77

8+
/**
9+
* Codex's interrupt handler answers -32600 when the named turn is not its running one (no turn,
10+
* another turn, thread not loaded) and -32603 when it could not submit the interrupt. A request it
11+
* could not parse, or one before initialize, also answers -32600: read as not running, it keeps the
12+
* child, as before.
13+
*/
14+
function isCodexTurnNotRunningRefusal(error: unknown): boolean {
15+
return isCodexAppServerRequestError(error) && error.code === -32600
16+
}
17+
818
/**
919
* Codex answers a turn's interrupt only as that turn ends, so the answer is what confirms the
10-
* Stop. The interrupt is the whole Stop: Codex kills the turn's one-shot commands itself and keeps
11-
* its background terminals running until the thread ends.
20+
* Stop. An answered interrupt is the whole Stop: Codex kills the turn's one-shot commands itself
21+
* and keeps its background terminals running until the thread ends. One Codex could not carry out,
22+
* or never answered, leaves the host to end the child (`performCancel`).
1223
*/
1324
export async function interruptCodexTurn(input: {
1425
session: CodexSession
@@ -29,7 +40,13 @@ export async function interruptCodexTurn(input: {
2940
throw error
3041
}
3142
const detail = providerDiagnosticOf(error)
32-
return { cancelled: false, refusal: detail ? { detail } : {} }
43+
return {
44+
cancelled: false,
45+
refusal: {
46+
...(detail ? { detail } : {}),
47+
...(isCodexTurnNotRunningRefusal(error) ? { turnNotRunning: true } : {})
48+
}
49+
}
3350
}
3451
const promptAdmission = input.onConfirmed?.()
3552
if (promptAdmission && !promptAdmission.accepted) {

‎src/main/native-chat/agent-session-wire/structured-agent-session-adapter-stop.ts‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,29 @@
11
// How a provider's Stop, and a prompt card's own Cancel, end what they end. Every member is
2-
// optional: a provider that declares none keeps its child after a Stop, and its card's Cancel
3-
// interrupts the turn holding the card.
2+
// optional: a provider that declares none keeps its child after a Stop it took, and its card's
3+
// Cancel interrupts the turn holding the card.
44

55
import type {
66
AgentJournalApprovalItem,
77
AgentJournalQuestionItem
88
} from '../../../shared/agent-session-journal-types'
9+
import type { ProviderDiagnostic } from '../../../shared/agent-session-failure'
10+
11+
/** `refusal`: the provider answered the Stop and declined it, in its own words when it gave any.
12+
* `turnNotRunning`: its refusal is the kind it gives for a turn not running there, so the Stop keeps
13+
* the child; absent, it could not interrupt that turn, which may run on. */
14+
export type AgentSessionCancelOutcome = {
15+
cancelled: boolean
16+
refusal?: { detail?: ProviderDiagnostic; turnNotRunning?: true }
17+
}
918

1019
/** Where a card's Cancel goes: a dismissal (`dismissPrompt`), or the chat's Stop. */
1120
export type AgentSessionPromptCancelRoute = { kind: 'dismiss' } | { kind: 'stop' }
1221

1322
export type StructuredAgentSessionAdapterStop = {
1423
/** A Stop ends this provider's child after `cancelTurn`, whatever it answered, unless it named a
1524
* turn that is no longer live and the cancel answered that it did not take it; the next send
16-
* resumes the conversation. Absent or false keeps the child after a Stop. */
25+
* resumes the conversation. Absent or false keeps the child after a Stop the provider took; a
26+
* failed interrupt still ends a child running the turn the Stop meant. */
1727
stopEndsSession?(sessionId: string): boolean
1828
/** What a Stop that ends the session waits on before it ends the child: resolves once the provider
1929
* has nothing in flight, a send it has not answered included, or when its grace, counted from

‎src/main/native-chat/agent-session-wire/structured-agent-session-adapter.ts‎

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,13 @@ import type {
3232
AgentSessionThreadGoalChange
3333
} from '../../../shared/agent-session-wire'
3434
import type { AgentSessionRefusalReason } from '../../../shared/agent-session-wire-refusals'
35-
import type {
36-
ProviderDiagnostic,
37-
SubmissionRejectionFact
38-
} from '../../../shared/agent-session-failure'
35+
import type { SubmissionRejectionFact } from '../../../shared/agent-session-failure'
3936
import type { StructuredAgentSessionStopCause } from './structured-agent-session-stop-cause'
40-
import type { StructuredAgentSessionAdapterStop } from './structured-agent-session-adapter-stop'
37+
import type {
38+
AgentSessionCancelOutcome,
39+
StructuredAgentSessionAdapterStop
40+
} from './structured-agent-session-adapter-stop'
41+
export type { AgentSessionCancelOutcome } from './structured-agent-session-adapter-stop'
4142
export type {
4243
StructuredAgentSessionChildEndCause,
4344
StructuredAgentSessionStopCause
@@ -242,12 +243,6 @@ export type StructuredAgentSessionSetOptionInput = {
242243
fence: number
243244
}
244245

245-
/** `refusal`: the provider answered the Stop and declined it, in its own words when it gave any. */
246-
export type AgentSessionCancelOutcome = {
247-
cancelled: boolean
248-
refusal?: { detail?: ProviderDiagnostic }
249-
}
250-
251246
export type StructuredAgentSessionAdapter = StructuredAgentSessionAdapterStop & {
252247
/** Provider-aware capability check for hosts that route more than one adapter. */
253248
supportsCreate?(location: AgentSessionExecutionLocation, agent: string): boolean

‎src/main/native-chat/agent-session-wire/structured-agent-session-chat-stop.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// The chat's Stop, however a client reached it: the Stop button or a question card's Cancel. One
22
// body and one order: withdraw what is queued, record where the Stop took effect, interrupt, then
3-
// end the child in the next step on the session's lane. The body is reachable only through
4-
// `mutateWithChatStop`, which queues that step in the same synchronous call as the mutation.
3+
// end the child: in this step when the interrupt failed with the turn still running, else in the
4+
// next step on the session's lane for a provider whose Stop ends its session. The body is
5+
// reachable only through `mutateWithChatStop`, which queues that step in the same synchronous call
6+
// as the mutation.
57

68
import { agentSessionFailureWords } from '../../../shared/agent-session-failure-words'
79
import { agentSessionFailureFact } from '../../../shared/agent-session-failure'
@@ -83,6 +85,9 @@ export function mutateWithChatStop<TValue>(
8385
clientOperationId: envelope.clientOperationId,
8486
...named,
8587
stopChild: () => context.stopAgent(sessionId),
88+
onStopChildError: (error) => context.deps.onEventSinkError?.({ sessionId, error }),
89+
// The host drops its child only once the exit is proven, and nothing else runs meanwhile.
90+
childReleased: () => context.sessions.get(sessionId)?.child !== child,
8691
endSession: (owed) => {
8792
windDown = owed
8893
},

0 commit comments

Comments
 (0)