Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 10 additions & 22 deletions src/main/codex/codex-background-task-frames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import { codexChildTurnState } from './codex-subagent-executions'
import { readRecord } from './codex-item-field-readers'
import { readCodexThreadItem } from './codex-structured-item-translation'
import { readCodexErrorWillRetry, readCodexTurnId } from './codex-structured-thread-facts'
import { readCodexTurnId } from './codex-structured-thread-facts'

export type CodexBackgroundTaskFrame =
| {
Expand All @@ -25,40 +25,28 @@ export type CodexBackgroundTaskFrame =
state: NativeChatSubagentState
}
| {
/** A child turn that ended with no `turn/completed`. No `turnId`: the one it is running. */
kind: 'turn-ended'
/** A child thread that closed: it ran its last turn, and Codex never said how it went. */
kind: 'thread-closed'
threadId: string
turnId: string | null
state: CodexChildTurnEnding
}

type CodexChildTurnEnding = Extract<NativeChatSubagentState, 'failed' | 'unverifiable'>

export type CodexBackgroundTaskEvent = {
method: string
threadId: string
params: unknown
}

/**
* The two ways Codex ends a child's turn without `turn/completed`. An `error` it will not retry is
* that turn's own end: for a child, `turn/completed` may never follow, and without this end the
* child's lifecycle row latches on `working` for the life of the session. A closed thread ran its
* last turn, and Codex never said how it went.
*
* #23682 dropped the equivalent reading from the primary journal path, where Codex's own failed
* `turn/completed` always follows within ~32 ms and carries the duration the error lacks. A child
* has no such guarantee, which is why this path still settles on the error.
* The one way Codex ends a child's turn without `turn/completed`: a closed thread ran its last
* turn and Codex never said how it went. A turn-ending `error` is not one — Codex follows it with
* a failed `turn/completed` for the same turn, which is that turn's end and carries the duration
* and receipt time the error does not.
Comment on lines +40 to +43

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ This JSDoc — and the removed error branch it describes — now contradicts main, not just the pre-#23808 code. #23808 restored exactly this path, and the merge of #23810 (15e7a42) resolved the conflict by keeping the branch's removal, so head has no child error path while origin/main does. Merging this PR would revert #23808, whose stated risk is a child that latches on working for the session when no turn/completed follows the error; please reconcile deliberately (keep the path, or record why it is being withdrawn and on what evidence).

Technical details
# Merge reverts #23808's child error-settling path

## Affected sites
- `src/main/codex/codex-background-task-frames.ts:40-43` — JSDoc asserts a turn-ending `error` is not an end; `main`'s `readCodexChildTurnEnding` returns `{ kind: 'turn-ended', threadId, turnId, state: 'failed' }` for a non-retryable `error`.
- `src/main/codex/codex-structured-thread-facts.ts` — `readCodexErrorWillRetry` exists on `origin/main` (`b8a7dafa`) and was added back by #23808; this branch deletes it.
- `src/main/codex/codex-child-work-evidence.test.ts:173-183` — the error step asserts `live`; `main`'s version asserts the error settles `failed`.

## Evidence
- `git show 215ce16:src/main/codex/codex-background-task-frames.ts` and `git show 15e7a42:...` are identical; `git diff 215ce16 15e7a42 -- src/main/codex/` is empty.
- `git show f8f656ca19:...` and `git show b8a7dafa:...` (current `main`) both contain the error branch.
- `git diff f8f656ca19 15e7a42 -- src/main/codex/` removes it (6 files, +49/-75).

## Required outcome
- `main`'s #23808 must not be dropped without an explicit decision: either retain the error path, or state in the PR why it is being reverted, with evidence that resolves the disagreement with #23808's no-guarantee argument.

*/
function readCodexChildTurnEnding(
function readCodexChildThreadClosed(
event: CodexBackgroundTaskEvent
): CodexBackgroundTaskFrame | null {
if (event.method === 'error' && !readCodexErrorWillRetry(event.params)) {
const turnId = readCodexTurnId(event.params)
return { kind: 'turn-ended', threadId: event.threadId, turnId, state: 'failed' }
}
return event.method === 'thread/closed'
? { kind: 'turn-ended', threadId: event.threadId, turnId: null, state: 'unverifiable' }
? { kind: 'thread-closed', threadId: event.threadId }
: null
}

Expand All @@ -67,7 +55,7 @@ export function readCodexBackgroundTaskFrame(
primaryThreadId: string
): CodexBackgroundTaskFrame | null {
// The session's own turn ends through the journal's turn boundaries, never here.
const ending = event.threadId === primaryThreadId ? null : readCodexChildTurnEnding(event)
const ending = event.threadId === primaryThreadId ? null : readCodexChildThreadClosed(event)
if (ending) {
return ending
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/codex/codex-background-task-tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ export class CodexBackgroundTaskTracker {
frame.parentTurnId,
frame.spawnerThreadId
)
} else if (frame?.kind === 'turn-ended') {
this.executions.endTurn(frame.threadId, frame.turnId, frame.state)
} else if (frame?.kind === 'thread-closed') {
this.executions.closeThread(frame.threadId)
} else if (frame && frame.threadId !== this.primaryThreadId) {
this.executions.observeTurn(frame.threadId, frame.turnId, frame.state)
}
Expand Down
58 changes: 26 additions & 32 deletions src/main/codex/codex-child-work-evidence.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,44 +159,41 @@ describe('Codex child-work evidence', () => {
expect(tracker.state).toBeNull()
})

const childError = (
turnId: string | undefined,
willRetry: boolean
): CodexBackgroundTaskEvent => ({
const childError = (turnId: string, willRetry: boolean): CodexBackgroundTaskEvent => ({
method: 'error',
threadId: CHILD,
params: {
threadId: CHILD,
...(turnId ? { turnId } : {}),
willRetry,
error: { message: 'boom' }
}
params: { threadId: CHILD, turnId, willRetry, error: { message: 'boom' } }
})
const childClosed: CodexBackgroundTaskEvent = {
method: 'thread/closed',
threadId: CHILD,
params: { threadId: CHILD }
}

it.each([
['an error naming its turn that Codex will not retry', childError('c1', false), 'failed'],
['an error naming no turn that Codex will not retry', childError(undefined, false), 'failed'],
['its thread closing', childClosed, 'unknown']
])(
'settles a working child whose turn ended with no turn/completed, by %s, in the strip and the record together',
(_label, ending, outcome) => {
const { send, tracker, records } = runningChild()
expect(tracker.state?.tasks).toHaveLength(1)
send(ending)
expect(records()).toEqual([
expect.objectContaining({ membership: 'settled', state: 'done', outcome })
])
expect(tracker.state).toBeNull()
// The first ending a turn gets stands.
send(turn('turn/completed', CHILD, 'c1', 'completed'))
expect(records()).toEqual([expect.objectContaining({ outcome })])
}
)
it('keeps a child working through an error Codex will not retry, and settles it on the failed turn/completed that follows', () => {
const { send, tracker, records } = runningChild()
send(childError('c1', false))
expect(records()).toEqual([expect.objectContaining({ membership: 'live' })])
expect(tracker.state?.tasks).toHaveLength(1)
send(turn('turn/completed', CHILD, 'c1', 'failed'))
expect(records()).toEqual([
expect.objectContaining({ membership: 'settled', state: 'done', outcome: 'failed' })
])
expect(tracker.state).toBeNull()
})

it('settles a working child whose thread closed with no turn/completed, in the strip and the record together', () => {
const { send, tracker, records } = runningChild()
expect(tracker.state?.tasks).toHaveLength(1)
send(childClosed)
expect(records()).toEqual([
expect.objectContaining({ membership: 'settled', state: 'done', outcome: 'unknown' })
])
expect(tracker.state).toBeNull()
// The first ending a turn gets stands.
send(turn('turn/completed', CHILD, 'c1', 'completed'))
expect(records()).toEqual([expect.objectContaining({ outcome: 'unknown' })])
})

it('keeps a child working through a retried error and a systemError status: its turn runs on', () => {
const { send, tracker, records } = runningChild()
Expand All @@ -207,9 +204,6 @@ describe('Codex child-work evidence', () => {
})
expect(records()).toEqual([expect.objectContaining({ membership: 'live', state: 'working' })])
expect(tracker.state?.tasks).toHaveLength(1)
// A fatal error naming a turn the child already finished ends nothing.
send(turn('turn/completed', CHILD, 'c1'), childError('c1', false))
expect(records()).toEqual([expect.objectContaining({ outcome: 'succeeded' })])
})

it('never settles a child on its PARENT turn ending: children outlive the turn', () => {
Expand Down
7 changes: 6 additions & 1 deletion src/main/codex/codex-structured-child-work-producer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,13 +332,18 @@ describe('Codex structured child-work producer', () => {
{ frame: turn('turn/started', TESTER, 't1'), lead: 'working' },
{ frame: spawned(LINTER, 'lint', 'p2'), lead: 'working' },
{ frame: turn('turn/started', LINTER, 'l1'), lead: 'working' },
// Codex ends this child's turn with an error it will not retry, and no turn/completed.
// Codex ends this child's turn with an error it will not retry, then a failed completion.
{
frame: {
method: 'error',
params: { threadId: LINTER, turnId: 'l1', willRetry: false, error: { message: 'boom' } }
},
lead: 'working',
check: () => expect(byDescription('lint')).toMatchObject({ membership: 'live' })
},
{
frame: turn('turn/completed', LINTER, 'l1', 'failed'),
lead: 'working',
check: () =>
expect(byDescription('lint')).toMatchObject({ membership: 'settled', outcome: 'failed' })
},
Expand Down
8 changes: 0 additions & 8 deletions src/main/codex/codex-structured-thread-facts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,6 @@ export function readCodexTurnDurationMs(payload: unknown): number | null {
/** `thread/status/changed` carries a TAGGED status (`{status:{type}}`), never a
* bare string. `idle` and `systemError` are the two arms that mean the thread
* is not running; `active` and `notLoaded` are not. */
/**
* Codex hardcodes `willRetry: false` on an `error` that affects turn status, and `true` on a
* stream error it is about to retry, which ends nothing.
*/
export function readCodexErrorWillRetry(payload: unknown): boolean {
return record(payload)?.willRetry === true
}

export function codexThreadStoppedRunning(payload: unknown): boolean {
const type = record(record(payload)?.status)?.type
return type === 'idle' || type === 'systemError'
Expand Down
15 changes: 5 additions & 10 deletions src/main/codex/codex-subagent-executions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,12 @@ export class CodexSubagentExecutions {
return { child, execution }
}

/** A child turn that ended with no `turn/completed`. With no turn named, the one the child is
* running ended; a child running none has nothing to end. The first ending a turn gets stands. */
endTurn(
agentThreadId: string,
turnId: string | null,
state: Exclude<NativeChatSubagentState, 'working'>
): void {
/** The child's thread closed with no `turn/completed`: the turn it was running ended, and how
* it went is unknown. A child running none has nothing to end. */
closeThread(agentThreadId: string): void {
const current = this.children.get(agentThreadId)?.execution
const ended = turnId ?? (current?.state === 'working' ? current.turnId : null)
if (current && ended !== null) {
this.observeTurn(agentThreadId, ended, state)
if (current?.state === 'working') {
this.observeTurn(agentThreadId, current.turnId, 'unverifiable')
}
}

Expand Down
Loading