From 42a3262c2fae6573da61b4e364b6496e5f3e8a0f Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Sun, 8 Nov 2026 20:25:30 -0800 Subject: [PATCH] perf(agents): release published child payloads and index live work --- src/main/acp/timeline/acp-subagents.ts | 7 +- .../codex-background-command-tracker.ts | 27 ++++-- .../codex-background-task-tracker.ts | 91 +++++++++++++++--- .../codex-child-work-evidence.ts | 31 ++++--- .../codex-subagent-executions.ts | 93 ++++++++++++++----- 5 files changed, 194 insertions(+), 55 deletions(-) diff --git a/src/main/acp/timeline/acp-subagents.ts b/src/main/acp/timeline/acp-subagents.ts index 2f84da87a69..718ba8a5ad6 100644 --- a/src/main/acp/timeline/acp-subagents.ts +++ b/src/main/acp/timeline/acp-subagents.ts @@ -19,7 +19,7 @@ import type { AcpSubagentUpdate } from '../acp-dialects/acp-dialect' type KnownSubagent = { /** Pin replies to the spawning turn. */ placement: ProviderTimelineJoin - reply?: string + replyDigest?: string /** Preserve the first terminal state; only completed runs can publish replies. */ ended?: NativeChatSubagentState } @@ -102,10 +102,11 @@ export class AcpSubagents { } private reply(id: string, subagent: KnownSubagent, text: string): ProviderTimelineEvent[] { - if (subagent.reply === text) { + const digest = createHash('sha256').update(text, 'utf16le').digest('hex') + if (subagent.replyDigest === digest) { return [] } - subagent.reply = text + subagent.replyDigest = digest return [ { type: 'item.update', diff --git a/src/main/codex/structured/background-work/codex-background-command-tracker.ts b/src/main/codex/structured/background-work/codex-background-command-tracker.ts index 8a2ddfcc3c7..583f958aa0d 100644 --- a/src/main/codex/structured/background-work/codex-background-command-tracker.ts +++ b/src/main/codex/structured/background-work/codex-background-command-tracker.ts @@ -35,6 +35,7 @@ export type CodexBackgroundCommandChange = export class CodexBackgroundCommandTracker { private readonly commands = new Map() + private readonly byThread = new Map>() private readonly settled = new Set() constructor(private readonly primaryThreadId: string) {} @@ -52,13 +53,20 @@ export class CodexBackgroundCommandTracker { return null } this.commands.set(key, command) + const thread = this.byThread.get(command.threadId) ?? new Set() + thread.add(key) + this.byThread.set(command.threadId, thread) return this.liveChange(command) } /** A surviving process becomes background work when the turn that launched it ends. */ endTurn(threadId: string, turnId: string): CodexBackgroundCommandChange[] { const backgrounded: CodexBackgroundCommandChange[] = [] - for (const command of this.commands.values()) { + for (const key of this.byThread.get(threadId) ?? []) { + const command = this.commands.get(key) + if (!command) { + continue + } if ( command.threadId === threadId && (command.turnId === turnId || command.turnId === null) && @@ -86,9 +94,7 @@ export class CodexBackgroundCommandTracker { /** The thread closed: Codex stops its processes first, so none of them can report an exit. */ endThread(threadId: string): CodexBackgroundCommandChange[] { - return [...this.commands] - .filter(([, command]) => command.threadId === threadId) - .flatMap(([key]) => this.end(key) ?? []) + return [...(this.byThread.get(threadId) ?? [])].flatMap((key) => this.end(key) ?? []) } /** Its approval went unanswered until its turn ended, so its process never started. */ @@ -129,9 +135,10 @@ export class CodexBackgroundCommandTracker { /** Live processes, including foreground tools that are absent from the strip. */ threadCommands(threadId: string): CodexBackgroundCommandChange[] { - return [...this.commands.values()] - .filter((command) => command.threadId === threadId) - .map((command) => this.liveChange(command)) + return [...(this.byThread.get(threadId) ?? [])].flatMap((key) => { + const command = this.commands.get(key) + return command ? [this.liveChange(command)] : [] + }) } clear(): CodexBackgroundCommandChange[] { @@ -143,6 +150,7 @@ export class CodexBackgroundCommandTracker { }) ) this.commands.clear() + this.byThread.clear() this.settled.clear() return ended } @@ -152,6 +160,11 @@ export class CodexBackgroundCommandTracker { const existing = this.commands.get(key) if (existing) { this.commands.delete(key) + const thread = this.byThread.get(existing.threadId) + thread?.delete(key) + if (thread?.size === 0) { + this.byThread.delete(existing.threadId) + } } this.rememberSettled(key) return existing diff --git a/src/main/codex/structured/background-work/codex-background-task-tracker.ts b/src/main/codex/structured/background-work/codex-background-task-tracker.ts index 67a57591e53..cda46518af4 100644 --- a/src/main/codex/structured/background-work/codex-background-task-tracker.ts +++ b/src/main/codex/structured/background-work/codex-background-task-tracker.ts @@ -11,11 +11,15 @@ import { type CodexBackgroundTaskEvent } from './codex-background-task-frames' import { CodexSubagentExecutions } from './codex-subagent-executions' -import { CodexBackgroundCommandTracker } from './codex-background-command-tracker' +import { + CodexBackgroundCommandTracker, + type CodexBackgroundCommandChange +} from './codex-background-command-tracker' import { CodexChildWorkEvidence } from './codex-child-work-evidence' import type { CodexAbandonedCommand } from '../prompts/codex-prompt-registry' import type { CodexStructuredSessionAdapterDeps } from '../session-lifecycle/codex-structured-session-state' import { boundSubagentLabel } from '../../../native-chat/agent-session-journal/item-lifecycle/journal-subagent-item' +import { codexChildCommandDescription } from '../../../../shared/codex-child-command-description' export type CodexChildWorkSink = { deliver: (evidence: AgentChildWorkEvidence[]) => void @@ -40,6 +44,7 @@ export type CodexTerminalStopSupport = 'supported' | 'unsupported' | 'unknown' export class CodexBackgroundTaskTracker { private publishedFingerprint = '[]' private publishedState: AgentSessionBackgroundTaskState | null = null + private readonly projectedTasks = new Map() private readonly commands: CodexBackgroundCommandTracker private readonly childWork: CodexChildWorkEvidence private terminalStops: CodexTerminalStopSupport | 'probing' = 'unknown' @@ -91,11 +96,18 @@ export class CodexBackgroundTaskTracker { turns: { threadId: string; turnId: string }[] terminals: { threadId: string; processId: string }[] } { - const named = new Set(taskIds) - const lineage = this.executions - .workingChildren() - .filter((child) => named.has(child.agentThreadId)) - .flatMap((child) => this.executions.lineage(child.agentThreadId)) + const named = new Set( + taskIds.filter((id) => { + const child = this.executions.find(id) + return child?.registered && child.execution?.state === 'working' + }) + ) + const lineage = named.size + ? this.executions.lineage( + named, + this.commands.liveCommands().map((command) => command.threadId) + ) + : [] const threads = [...new Set(lineage.map((child) => child.agentThreadId))] return { turns: threads.flatMap((threadId) => { @@ -147,16 +159,16 @@ export class CodexBackgroundTaskTracker { ] observeCodexExecutionFrame(this.executions, frame, this.primaryThreadId) this.childWork.observe(event, frame, commands) - if (!frame) { - return itemEvent || commands.length > 0 ? this.refresh() : false - } - // Children can outlive the parent turn; republish without settling or sweeping them. - return this.refresh() + // The journal may have dirtied this shared registry before admitting the frame. + return this.updateProjection(this.executions.takeChangedThreads(), commands) + ? this.refresh() + : false } clear(processExit?: StructuredAgentSessionEndedEvent['processExit']): boolean { this.childWork.clear(this.commands.clear(), processExit) this.executions.clear() + this.projectedTasks.clear() return this.refresh() } @@ -188,6 +200,63 @@ export class CodexBackgroundTaskTracker { ] } + private updateProjection( + threads: readonly string[], + commands: readonly CodexBackgroundCommandChange[] + ): boolean { + let changed = false + for (const threadId of threads) { + const child = this.executions.find(threadId) + const task = + child?.registered && child.execution?.state === 'working' + ? { + id: `codex-agent:${threadId}`, + kind: 'agent' as const, + ...(child.label ? { description: boundSubagentLabel(child.label) } : {}) + } + : null + changed = this.projectTask(`codex-agent:${threadId}`, task) || changed + for (const command of this.commands.threadCommands(threadId)) { + changed = this.projectCommand(command) || changed + } + } + for (const command of commands) { + changed = this.projectCommand(command) || changed + } + return changed + } + + private projectCommand(command: CodexBackgroundCommandChange): boolean { + if (command.type === 'ended') { + return this.projectTask(command.taskId, null) + } + const child = this.executions.find(command.threadId) + if (!command.backgrounded || (child?.registered && child.execution?.state === 'working')) { + return this.projectTask(command.task.id, null) + } + const label = + command.threadId === this.primaryThreadId ? null : this.executions.label(command.threadId) + const task = label + ? { + ...command.task, + description: codexChildCommandDescription(label, command.task.description) + } + : command.task + return this.projectTask(task.id, task) + } + + private projectTask(id: string, task: AgentSessionBackgroundTask | null): boolean { + if (task === null) { + return this.projectedTasks.delete(id) + } + const previous = this.projectedTasks.get(id) + if (previous?.kind === task.kind && previous.description === task.description) { + return false + } + this.projectedTasks.set(id, task) + return true + } + private refresh(): boolean { const tasks = this.tasks() const fingerprint = JSON.stringify(tasks) diff --git a/src/main/codex/structured/background-work/codex-child-work-evidence.ts b/src/main/codex/structured/background-work/codex-child-work-evidence.ts index e53243989f8..4c7bba71fab 100644 --- a/src/main/codex/structured/background-work/codex-child-work-evidence.ts +++ b/src/main/codex/structured/background-work/codex-child-work-evidence.ts @@ -207,14 +207,21 @@ export class CodexChildWorkEvidence { const lastMessage = ofTurn(facts.lastMessage, turnId)?.text const { totalTokens } = facts const outcome = codexChildTurnOutcome(state) - this.publish(facts, JSON.stringify(['ended', turnId, state]), (observedAt) => ({ - type: 'ended', - observedAt, - handle: { idKind: 'thread_id', id: threadId, runId: turnId }, - outcome, - ...(lastMessage ? { lastMessage } : {}), - ...(totalTokens !== undefined ? { totalTokens } : {}) - })) + const published = this.publish( + facts, + JSON.stringify(['ended', turnId, state]), + (observedAt) => ({ + type: 'ended', + observedAt, + handle: { idKind: 'thread_id', id: threadId, runId: turnId }, + outcome, + ...(lastMessage ? { lastMessage } : {}), + ...(totalTokens !== undefined ? { totalTokens } : {}) + }) + ) + if (facts.lastMessage?.turnId === turnId && (published || state !== 'unverifiable')) { + facts.lastMessage = undefined + } return } for (const [itemId, call] of facts.openCalls) { @@ -267,19 +274,21 @@ export class CodexChildWorkEvidence { } } - private publish(facts: ChildFacts, fingerprint: string, edge: CodexPendingChildWork): void { + private publish(facts: ChildFacts, fingerprint: string, edge: CodexPendingChildWork): boolean { if (facts.published !== fingerprint) { facts.published = fingerprint this.pending.push(edge) + return true } + return false } // Reassert ownership for work admitted before its owner's record existed. private requeueOwnedBy(threadId: string): void { this.queueCommands(this.liveCommands(threadId)) - for (const spawned of this.executions.workingChildren()) { + for (const spawned of this.executions.workingSpawnedBy(threadId)) { const facts = this.facts.get(spawned.agentThreadId) - if (spawned.spawnerThreadId === threadId && facts?.published !== undefined) { + if (facts?.published !== undefined) { facts.published = undefined this.queueChild(spawned.agentThreadId) } diff --git a/src/main/codex/structured/background-work/codex-subagent-executions.ts b/src/main/codex/structured/background-work/codex-subagent-executions.ts index 243455beb9e..dac855bd517 100644 --- a/src/main/codex/structured/background-work/codex-subagent-executions.ts +++ b/src/main/codex/structured/background-work/codex-subagent-executions.ts @@ -15,6 +15,7 @@ export type CodexChildExecution = { export type CodexExecutionChild = { agentThreadId: string + observationOrder: number registered: boolean label: string | null /** Spawn announcements arrive on the parent's thread. */ @@ -28,6 +29,9 @@ export type CodexExecutionChild = { export class CodexSubagentExecutions { private readonly children = new Map() private readonly settledTurns = new Map() + private readonly working = new Map() + private readonly workingBySpawner = new Map>() + private readonly changed = new Set() register( agentThreadId: string, @@ -35,6 +39,7 @@ export class CodexSubagentExecutions { spawnerThreadId?: string ): CodexExecutionChild { const child = this.child(agentThreadId) + const previousLabel = child.label // Keep the original spawner across duplicate announcement frames. child.spawnerThreadId ??= spawnerThreadId ?? null child.registered = true @@ -44,6 +49,10 @@ export class CodexSubagentExecutions { ?.trim() .replace(/\s+/g, ' ') .slice(0, MAX_SUBAGENT_FIELD_CHARS + 1) || null + this.updateWorking(child) + if (previousLabel !== child.label) { + this.changed.add(agentThreadId) + } return child } @@ -83,6 +92,7 @@ export class CodexSubagentExecutions { } if (state === 'working' || !child.execution || child.execution.turnId === turnId) { child.execution = execution + this.updateWorking(child) } return { child, execution } } @@ -113,42 +123,53 @@ export class CodexSubagentExecutions { return this.children.get(agentThreadId) } - lineage(agentThreadId: string): CodexExecutionChild[] { - const spawnedByThread = new Map() - for (const child of this.children.values()) { - if (child.spawnerThreadId === null) { - continue - } - const siblings = spawnedByThread.get(child.spawnerThreadId) ?? [] - siblings.push(child.agentThreadId) - spawnedByThread.set(child.spawnerThreadId, siblings) - } + lineage(roots: ReadonlySet, commandThreads: Iterable): CodexExecutionChild[] { const lineage: CodexExecutionChild[] = [] - const seen = new Set() - const pending = [agentThreadId] - for (let id = pending.pop(); id !== undefined; id = pending.pop()) { - const child = this.children.get(id) - if (!child || seen.has(id)) { - continue + const reachesRoot = new Map() + const candidates = new Set([...this.working.keys(), ...commandThreads]) + for (const threadId of candidates) { + let id = threadId + const path = new Set() + while (!roots.has(id) && !reachesRoot.has(id) && !path.has(id)) { + path.add(id) + const spawner = this.children.get(id)?.spawnerThreadId + if (spawner === null || spawner === undefined) { + break + } + id = spawner } - seen.add(id) - lineage.push(child) - for (const spawned of spawnedByThread.get(id) ?? []) { - pending.push(spawned) + const included = roots.has(id) || reachesRoot.get(id) === true + for (const ancestor of path) { + reachesRoot.set(ancestor, included) + } + const child = this.children.get(threadId) + if (included && child) { + lineage.push(child) } } return lineage } workingChildren(): CodexExecutionChild[] { - return [...this.children.values()].filter( - (child) => child.registered && child.execution?.state === 'working' - ) + return [...this.working.values()].sort((a, b) => a.observationOrder - b.observationOrder) + } + + workingSpawnedBy(threadId: string): CodexExecutionChild[] { + return [...(this.workingBySpawner.get(threadId) ?? [])] + } + + takeChangedThreads(): string[] { + const threads = [...this.changed] + this.changed.clear() + return threads } clear(): void { this.children.clear() this.settledTurns.clear() + this.working.clear() + this.workingBySpawner.clear() + this.changed.clear() } retentionSizes(): { children: number; settledTurns: number } { @@ -162,6 +183,7 @@ export class CodexSubagentExecutions { } const child: CodexExecutionChild = { agentThreadId, + observationOrder: this.children.size, registered: false, label: null, spawnerThreadId: null, @@ -173,6 +195,31 @@ export class CodexSubagentExecutions { return child } + private updateWorking(child: CodexExecutionChild): void { + const working = child.registered && child.execution?.state === 'working' + if (working !== this.working.has(child.agentThreadId)) { + this.changed.add(child.agentThreadId) + if (working) { + this.working.set(child.agentThreadId, child) + } else { + this.working.delete(child.agentThreadId) + } + } + const spawner = child.spawnerThreadId + if (spawner !== null) { + const children = this.workingBySpawner.get(spawner) ?? new Set() + if (working) { + children.add(child) + this.workingBySpawner.set(spawner, children) + } else { + children.delete(child) + if (children.size === 0) { + this.workingBySpawner.delete(spawner) + } + } + } + } + private numberTurn(child: CodexExecutionChild, turnId: string): void { if (child.turnOrdinals.has(turnId)) { return