Files
orca/src/main/codex/codex-structured-session-teardown.ts
T
Brennan BensonandMerge Sim 5868fdc9e3 feat(native-chat): report Codex background tasks in the chat strip (#19346)
* feat(native-chat): report Codex background tasks in the chat strip

The background-tasks strip works for Claude only; a structured Codex
session shows nothing in it. Feed it from the Codex app-server stream.

The strip stands for work that OUTLIVED a turn, which is what the
monitoring header, Claude's foreground suppression, and the conversation
command gate all already assume. Codex has no `is_backgrounded` flag, so
that fact is derived from the turn boundary: a `subAgentActivity` child or
a primary-thread `commandExecution` becomes visible once the turn it
belongs to completes and it is still unsettled.

`turn/completed` only reveals a task here, never settles one — measured on
`codex app-server` 0.153.4, a spawn_agent child reported `completed` 95.8s
after its parent turn ended. Only a child's own activity kind settles it.

Codex exposes no honest stop: `turn/interrupt` on a child ends its turn
without emitting a terminal activity item and leaves its shell running. So
the state carries a new optional `supportsStopAll: false`, the strip hides
a control that could not act, and the blocked-command message asks the user
to wait rather than to press a button that does not exist.

* refactor(codex): move session teardown out of the structured adapter

Merging main crossed the 300-line cap on
`codex-structured-session-adapter.ts`: the rewind backend (#19235) and this
branch's close-time strip clear both landed in it. The four close paths move
verbatim into `codex-structured-session-teardown.ts`, where they funnel
through one `settled` helper instead of repeating the notification-retry and
background-task cleanup at each call site. No ratchet bump.

Also normalize a background task's description once at receipt rather than on
every projection; the roster is re-projected on each observed frame.

* fix(codex): drop the shell row the journal already settles

A `commandExecution` still `inProgress` when its turn ends was reported as a
`command` task. But `settleCodexJournalTurn` writes exactly those items to the
journal as `state: 'failed'` on `turn/completed` and forgets them, so the strip
row would have claimed a shell was still running at the same instant Orca
recorded that it was not — two surfaces contradicting each other about the same
process.

A subagent is the opposite case and stays: the roster pointedly does not sweep
at a turn boundary, because children measurably outlive it. That leaves the
producer making exactly one claim — these spawn_agent children are still live
after their turn — which the durable roster row corroborates.

* fix(native-chat): track Codex background execution lifetimes

* fix(native-chat): keep running tool groups from claiming completion

* Fix runtime catalog and capability expectation

* fix(codex): keep a child's name on the command row that outlives it

A child agent's commands stay hidden behind its agent row while the child
works. Once the child's turn settles with a command still running, that
command surfaces as its own row labelled from the raw command string, so
'long_probe' became "/bin/zsh -lc 'ping -c 300 127.0.0.1 > /dev/null'"
at the moment that row was the only remaining signal for the work.

Qualify a child's command row with the child's label. Resolved on read,
so a label registered after the command still lands, and bounded by the
existing description cap so admission accounting stays valid. Primary-
thread commands are left unqualified: they have no child to name.

---------

Co-authored-by: Merge Sim <sim@local>
2026-09-09 00:13:20 -07:00

95 lines
3.1 KiB
TypeScript

// Stopping one Codex app-server child, in the four ways the host asks for it.
//
// Every path funnels through `settled` so the ephemeral surfaces a closed
// session owns are cleared exactly once, and only when the child was actually
// proven stopped — a refused close leaves the session indexed for a retry.
import type { AgentSessionBackgroundTaskState } from '../../shared/agent-session-wire'
import {
closeAllCodexSessions,
closeCodexPublishedSession,
closeCodexSession
} from './codex-structured-session-close'
import type {
CodexAcquisitionRegistry,
CodexSession,
CodexStructuredSessionEvent
} from './codex-structured-session-state'
export type CodexStructuredSessionTeardownDeps = {
sessions: Map<string, CodexSession>
acquisitions: CodexAcquisitionRegistry
onEvent?: (event: CodexStructuredSessionEvent) => void
onBackgroundTasksChanged?: (
sessionId: string,
state: AgentSessionBackgroundTaskState | null
) => void
forgetNotificationRetries: (sessionId: string) => void
}
export class CodexStructuredSessionTeardown {
constructor(private readonly deps: CodexStructuredSessionTeardownDeps) {}
close = async (sessionId: string): Promise<boolean> => {
const closed = await closeCodexSession(
sessionId,
this.deps.sessions,
this.deps.acquisitions,
this.deps.onEvent
)
return this.settled(sessionId, closed)
}
forceClose = async (sessionId: string): Promise<boolean> => {
const closed = await closeCodexPublishedSession(
this.deps.sessions,
sessionId,
this.deps.onEvent,
{ allowFailedSettlement: true, requestedClose: false }
)
return this.settled(sessionId, closed)
}
/** Terminates this exact child as an unexpected death. Every ownership check
* stays here so a stale caller cannot close a replacement child. */
forceCloseUnexpected = (
sessionId: string,
fence: number,
acquisitionGeneration: string,
reason: Error
): Promise<boolean> => {
const session = this.deps.sessions.get(sessionId)
if (
!session ||
session.ended ||
session.fence !== fence ||
session.acquisitionGeneration !== acquisitionGeneration
) {
return Promise.resolve(false)
}
return closeCodexPublishedSession(this.deps.sessions, sessionId, this.deps.onEvent, {
allowFailedSettlement: true,
requestedClose: false,
expectedFence: fence,
expectedAcquisitionGeneration: acquisitionGeneration,
unexpectedReason: reason
}).then((closed) => this.settled(sessionId, closed))
}
closeAll = (): Promise<void> =>
closeAllCodexSessions(this.deps.sessions, this.deps.acquisitions, (sessionId) =>
this.close(sessionId)
)
private settled(sessionId: string, closed: boolean): boolean {
if (closed) {
this.deps.forgetNotificationRetries(sessionId)
// Explicit null, not silence: the state reader answers `undefined` once
// the session leaves the map, which every channel reads as "unchanged"
// and would leave the last roster on screen.
this.deps.onBackgroundTasksChanged?.(sessionId, null)
}
return closed
}
}