diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts index daba8b2e14..c46dd2bc65 100644 --- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts @@ -4036,6 +4036,97 @@ describe("CodexAdapterV2 post-settle continuation", () => { ), ); + const spawnEarlyTerminalTranscript = makeCodexReplayTranscript({ + scenario: `${RESTART_SCENARIO}-spawn-early-terminal`, + entries: [ + ...codexReplayPreamble({ + nativeThreadId: RESTART_NATIVE_THREAD, + nativeTurnId: RESTART_NATIVE_TURN, + prompt: RESTART_PROMPT, + }), + restartChildFrame({ + label: "item/completed/spawn-with-terminal-state", + method: "item/completed", + params: { + item: { + type: "collabAgentToolCall", + id: "call-codex-spawn-early-terminal", + tool: "spawnAgent", + status: "completed", + senderThreadId: RESTART_NATIVE_THREAD, + receiverThreadIds: [RESTART_CHILD_THREAD], + prompt: "finish instantly", + model: null, + reasoningEffort: null, + agentsStates: { + [RESTART_CHILD_THREAD]: { + status: "completed", + message: "EARLY_TERMINAL_RESULT", + }, + }, + }, + threadId: RESTART_NATIVE_THREAD, + turnId: RESTART_NATIVE_TURN, + completedAtMs: 1782622482000, + }, + }), + restartChildFrame({ + label: "item/completed/root-answer", + method: "item/completed", + params: { + item: { + type: "agentMessage", + id: "root-answer-spawn-early-terminal", + text: "SPAWNED", + phase: "final_answer", + memoryCitation: null, + }, + threadId: RESTART_NATIVE_THREAD, + turnId: RESTART_NATIVE_TURN, + completedAtMs: 1782622483000, + }, + }), + restartChildFrame({ + label: "turn/completed/root", + method: "turn/completed", + params: { + threadId: RESTART_NATIVE_THREAD, + turn: makeCodexReplayTurn({ id: RESTART_NATIVE_TURN, status: "completed" }), + }, + }), + ], + }); + + it.effect("applies a terminal state arriving on the spawning frame itself", () => + Effect.scoped( + Effect.gen(function* () { + // The spawn registers the agent before any child turn opens an + // activation, so its currentActivationId is still null when the same + // frame's agentsStates carries the terminal state. The settled-noise + // gate must not swallow it, or the agent sticks at running forever. + const harness = yield* makeCodexReplayHarness(spawnEarlyTerminalTranscript); + const now = yield* DateTime.now; + + yield* harness.runtime.startTurn( + makeCodexTestTurnInput({ + threadId: harness.threadId, + providerThread: harness.providerThread, + now, + attemptId: RunAttemptId.make("attempt-codex-spawn-early-terminal"), + text: RESTART_PROMPT, + }), + ); + yield* awaitUntil(() => harness.terminalEvents().length === 1, "root turn terminal"); + + // A completed Codex agent rests at idle (its identity stays reusable); + // what matters is that it left running and carries its result. + const finalUpdate = harness.subagentUpdates().at(-1); + assert.equal(finalUpdate?.subagent.status, "idle"); + assert.equal(finalUpdate?.subagent.result, "EARLY_TERMINAL_RESULT"); + }).pipe(Effect.provide(Layer.merge(idAllocatorLayer, NodeServices.layer))), + ), + ); + it.effect("does not adopt an unknown native thread when nothing is rehydrated", () => Effect.scoped( Effect.gen(function* () { diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts index 762898a172..314665b40f 100644 --- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts @@ -2476,7 +2476,21 @@ export function makeCodexAdapterV2(adapterOptions: CodexAdapterV2Options): Provi const subagents = yield* Ref.get(subagentThreads); for (const [nativeThreadId, state] of Object.entries(input.item.agentsStates)) { const subagent = subagents.get(nativeThreadId); - if (subagent === undefined || subagent.task.currentActivationId === null) { + if (subagent === undefined) { + continue; + } + // A settled identity with no live activation is trailing noise. + // An *active* one with no activation is a just-registered agent + // whose child turn has not opened yet — its state on this very + // frame can already be terminal, and dropping it would leave the + // agent running forever. + const settled = + subagent.task.status === "idle" || + subagent.task.status === "completed" || + subagent.task.status === "failed" || + subagent.task.status === "cancelled" || + subagent.task.status === "interrupted"; + if (subagent.task.currentActivationId === null && settled) { continue; } yield* emitSubagentTaskUpdate({