Skip to content

Commit 98ab424

Browse files
fix(server): only a held Codex turn's own children wait with it
Codex sessions are shared by every T3 thread on an instance, so treating any unregistered native thread as the held turn's child captured other threads' subagents: their early turn/started was dropped, and their approvals failed, when the held turn was rejected. Children are now attributed through the spawn parent Codex reports in thread/started, and only threads whose root ancestor has a held or rejected external turn are held with it. AI-assisted; verified with the tests and tools listed above.
1 parent 52f370f commit 98ab424

2 files changed

Lines changed: 124 additions & 23 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts‎

Lines changed: 92 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4557,6 +4557,34 @@ describe("CodexAdapterV2 post-settle continuation", () => {
45574557
});
45584558
};
45594559

4560+
const spawnedThreadStarted = (
4561+
threadId: string,
4562+
parentThreadId: string,
4563+
): CodexReplay.CodexAppServerReplayEntry => ({
4564+
type: "emit_inbound",
4565+
label: `${threadId} thread/started`,
4566+
frame: {
4567+
method: "thread/started",
4568+
params: {
4569+
thread: {
4570+
id: threadId,
4571+
sessionId: threadId,
4572+
preview: "",
4573+
ephemeral: false,
4574+
modelProvider: "openai",
4575+
createdAt: 1782622459,
4576+
updatedAt: 1782622459,
4577+
status: { type: "idle" },
4578+
cwd: "/workspace",
4579+
cliVersion: "0.144.0",
4580+
projectId: null,
4581+
source: { subAgent: { thread_spawn: { parent_thread_id: parentThreadId, depth: 1 } } },
4582+
turns: [],
4583+
},
4584+
},
4585+
},
4586+
});
4587+
45604588
it.effect(
45614589
"adopts an external Codex turn that waits on approval and reads like a T3 command",
45624590
() =>
@@ -4687,6 +4715,7 @@ describe("CodexAdapterV2 post-settle continuation", () => {
46874715
turnId,
46884716
completedAtMs: 1782622459100,
46894717
}),
4718+
spawnedThreadStarted(childThreadId, nativeThreadId),
46904719
frame(childThreadId, "turn/started", {
46914720
turn: makeCodexReplayTurn({ id: childTurnId, status: "inProgress" }),
46924721
}),
@@ -4750,28 +4779,69 @@ describe("CodexAdapterV2 post-settle continuation", () => {
47504779
const scenario = "codex-external-queue-rejected";
47514780
const nativeThreadId = `native-${scenario}-thread`;
47524781
const nextTurnId = `native-${scenario}-next-turn`;
4782+
const otherChildThreadId = `native-${scenario}-other-child-thread`;
4783+
const otherChildTurnId = `native-${scenario}-other-child-turn`;
47534784
const prompt = "Reply with the word QUEUED.";
47544785
const nextTurnStart = codexReplayPreamble({
47554786
nativeThreadId,
47564787
nativeTurnId: nextTurnId,
47574788
prompt,
47584789
}).slice(-3);
4759-
const transcript = externalQueueTranscript(scenario, prompt, () => [], [
4760-
withReplayRequestId(nextTurnStart[0]!, 4),
4761-
withReplayRequestId(nextTurnStart[1]!, 4),
4762-
nextTurnStart[2]!,
4763-
{
4764-
type: "emit_inbound",
4765-
label: "next turn/completed",
4766-
frame: {
4767-
method: "turn/completed",
4768-
params: {
4769-
threadId: nativeThreadId,
4770-
turn: makeCodexReplayTurn({ id: nextTurnId, status: "completed" }),
4790+
// The session is shared by every T3 thread on the instance: another
4791+
// thread's child starting now must not be taken for this held turn's.
4792+
const transcript = externalQueueTranscript(
4793+
scenario,
4794+
prompt,
4795+
() => [
4796+
spawnedThreadStarted(otherChildThreadId, `native-${scenario}-other-thread`),
4797+
{
4798+
type: "emit_inbound",
4799+
label: "other child turn/started",
4800+
frame: {
4801+
method: "turn/started",
4802+
params: {
4803+
threadId: otherChildThreadId,
4804+
turn: makeCodexReplayTurn({ id: otherChildTurnId, status: "inProgress" }),
4805+
},
47714806
},
47724807
},
4773-
},
4774-
]);
4808+
],
4809+
[
4810+
withReplayRequestId(nextTurnStart[0]!, 4),
4811+
withReplayRequestId(nextTurnStart[1]!, 4),
4812+
nextTurnStart[2]!,
4813+
{
4814+
type: "emit_inbound",
4815+
label: "other child spawn registered",
4816+
frame: {
4817+
method: "item/completed",
4818+
params: {
4819+
item: {
4820+
type: "subAgentActivity",
4821+
id: `${scenario}-other-spawn`,
4822+
kind: "started",
4823+
agentThreadId: otherChildThreadId,
4824+
agentPath: "/root/other_agent",
4825+
},
4826+
threadId: nativeThreadId,
4827+
turnId: nextTurnId,
4828+
completedAtMs: 1782622461000,
4829+
},
4830+
},
4831+
},
4832+
{
4833+
type: "emit_inbound",
4834+
label: "next turn/completed",
4835+
frame: {
4836+
method: "turn/completed",
4837+
params: {
4838+
threadId: nativeThreadId,
4839+
turn: makeCodexReplayTurn({ id: nextTurnId, status: "completed" }),
4840+
},
4841+
},
4842+
},
4843+
],
4844+
);
47754845
const harness = yield* makeCodexReplayHarness(transcript);
47764846
const startTurn = (attempt: string, text: string, messageId?: MessageId) =>
47774847
Effect.gen(function* () {
@@ -4800,6 +4870,14 @@ describe("CodexAdapterV2 post-settle continuation", () => {
48004870
// starts its own native turn instead of adopting the released one.
48014871
yield* startTurn("attempt-late", prompt, wake.messageId);
48024872
yield* harness.awaitTerminal(2);
4873+
assert.isTrue(
4874+
harness.events.some(
4875+
(event) =>
4876+
event.type === "provider_turn.updated" &&
4877+
event.providerTurn.nativeTurnRef?.nativeId === otherChildTurnId,
4878+
),
4879+
"another thread's child turn that started during the hold was lost",
4880+
);
48034881
}).pipe(
48044882
Effect.provide(
48054883
Layer.mergeAll(

‎apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts‎

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4534,7 +4534,9 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
45344534
* later requests still need an answer, and no ordinary handler has a
45354535
* turn to give one.
45364536
*/
4537-
const rejectedExternalTurns = new Set<string>();
4537+
const rejectedExternalTurns = new Map<string, string>();
4538+
/** Codex's spawn parent of each child native thread, from `thread/started`. */
4539+
const nativeParentThreads = new Map<string, string>();
45384540
const externalTurnIdOf = (payload: unknown) => {
45394541
if (typeof payload !== "object" || payload === null) return undefined;
45404542
const turnId =
@@ -4566,7 +4568,8 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
45664568
yield* held.frames[index]!(adopted);
45674569
}
45684570
heldExternalTurns.delete(nativeTurnId);
4569-
if (!adopted && !held.completed) rejectedExternalTurns.add(nativeTurnId);
4571+
if (!adopted && !held.completed)
4572+
rejectedExternalTurns.set(nativeTurnId, held.nativeThreadId);
45704573
}),
45714574
);
45724575
});
@@ -4631,21 +4634,41 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
46314634
onRunSettled: rejectIfCurrent,
46324635
});
46334636
});
4634-
// Codex runs one turn per root thread, so a thread T3 does not know yet
4635-
// while an external turn is held is a descendant that turn spawned. Its
4636-
// frames wait with the parent's so the replayed spawn registers it first.
4637+
// A child the held turn spawned is not registered until its replayed spawn
4638+
// item runs, so its frames wait with the parent's. Sessions are shared by
4639+
// every T3 thread on the instance: only Codex's spawn ancestry says whose it is.
46374640
const heldDescendantOwner =
46384641
/* Runs for every Codex frame while a turn is held; avoid a span each. */
46394642
Effect.fnUntraced(function* (payload: unknown) {
46404643
const threadId = (payload as { readonly threadId?: unknown } | null)?.threadId;
4641-
if (typeof threadId !== "string" || rootProviderThreads.has(threadId)) return undefined;
4644+
if (typeof threadId !== "string") return undefined;
46424645
if ((yield* Ref.get(subagentThreads)).has(threadId)) return undefined;
4643-
const owner = [...heldExternalTurns.keys()].at(-1);
4644-
if (owner !== undefined) return { owner, rejected: false };
4645-
return rejectedExternalTurns.size > 0
4646+
let root = threadId;
4647+
for (let depth = 0; depth <= nativeParentThreads.size; depth++) {
4648+
const parent = nativeParentThreads.get(root);
4649+
if (parent === undefined) break;
4650+
root = parent;
4651+
}
4652+
if (root === threadId) return undefined;
4653+
for (const [owner, held] of heldExternalTurns) {
4654+
if (held.nativeThreadId === root) return { owner, rejected: false };
4655+
}
4656+
return [...rejectedExternalTurns.values()].includes(root)
46464657
? { owner: undefined, rejected: true }
46474658
: undefined;
46484659
});
4660+
yield* client.handleServerNotification("thread/started", ({ thread }) =>
4661+
Effect.sync(() => {
4662+
const source = thread.source;
4663+
if (
4664+
typeof source === "object" &&
4665+
"subAgent" in source &&
4666+
typeof source.subAgent === "object" &&
4667+
"thread_spawn" in source.subAgent
4668+
)
4669+
nativeParentThreads.set(thread.id, source.subAgent.thread_spawn.parent_thread_id);
4670+
}),
4671+
);
46494672
const noExternalTurns = () =>
46504673
heldExternalTurns.size === 0 && rejectedExternalTurns.size === 0;
46514674
const handleCodexNotification: CodexClient.CodexAppServerClient["Service"]["handleServerNotification"] =

0 commit comments

Comments
 (0)