fix(orchestration-v2): apply Codex collab states that arrive before an activation

registerSubagentThreads emits a running subagent whose currentActivationId
stays null until the child turn opens an activation. The settled-noise gate
skipped every state without one, so a terminal state carried on the spawning
frame itself was dropped and the agent stuck at running. The gate now only
drops states for identities that are already settled; an active identity
without an activation is a just-registered agent whose state still counts.

Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
Shivam SharmaandClaude Fable 5 authored and T3 Code Test committed 2026-08-14 15:04:12 +05:30
1 parent b95171112d
commit 4b3f8be173
2 files changed
+106 -1

No files matched your search

@@ -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* () {
@@ -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({