Skip to content

Commit 11adcb0

Browse files
fix(server): resume Codex parents after native children finish (pingdotgg#23840)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent b3c0109 commit 11adcb0

2 files changed

Lines changed: 379 additions & 1 deletion

File tree

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

Lines changed: 332 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4907,6 +4907,335 @@ describe("CodexAdapterV2 post-settle continuation", () => {
49074907
],
49084908
});
49094909

4910+
it.effect("wakes the parent when a native child finishes after the parent's turn", () =>
4911+
Effect.scoped(
4912+
Effect.gen(function* () {
4913+
const entries = resumeSubagentTranscript.entries;
4914+
const labelIndex = (label: string) =>
4915+
entries.findIndex((entry) => entry.type === "emit_inbound" && entry.label === label);
4916+
const rootCompleted = labelIndex("turn/completed/root");
4917+
const harness = yield* makeCodexReplayHarness(
4918+
makeCodexReplayTranscript({
4919+
scenario: RESUME_SCENARIO,
4920+
entries: [
4921+
...entries.slice(0, labelIndex(`turn/started/${RESUME_CHILD_TURN_1}`) + 1),
4922+
...entries.slice(rootCompleted - 1, rootCompleted + 1),
4923+
childAgentMessage({
4924+
id: "child-late-answer",
4925+
text: "NATIVE_CHILD_16900_DONE",
4926+
turnId: RESUME_CHILD_TURN_1,
4927+
completedAtMs: 1782622480000,
4928+
afterMs: 60_000,
4929+
}),
4930+
childTurnCompleted(RESUME_CHILD_TURN_1),
4931+
],
4932+
}),
4933+
);
4934+
4935+
yield* harness.runtime.startTurn(
4936+
makeCodexTestTurnInput({
4937+
threadId: harness.threadId,
4938+
providerThread: harness.providerThread,
4939+
now: yield* DateTime.now,
4940+
attemptId: RunAttemptId.make("attempt-codex-late-child"),
4941+
text: RESUME_PROMPT,
4942+
}),
4943+
);
4944+
yield* harness.awaitTerminal(1);
4945+
assert.equal(harness.terminalEvents()[0]?.status, "completed");
4946+
assert.lengthOf(harness.continuationRequests, 0);
4947+
4948+
yield* TestClock.adjust("60 seconds");
4949+
yield* harness.until(
4950+
() => harness.subagentUpdates().at(-1)?.subagent.result === "NATIVE_CHILD_16900_DONE",
4951+
);
4952+
assert.equal(harness.subagentUpdates().at(-1)?.subagent.status, "completed");
4953+
assert.lengthOf(harness.continuationRequests, 1);
4954+
assert.include(harness.continuationRequests[0]?.detail, "NATIVE_CHILD_16900_DONE");
4955+
assert.equal(harness.continuationRequests[0]?.delivery, "message_text");
4956+
}).pipe(
4957+
Effect.provide(
4958+
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
4959+
),
4960+
),
4961+
),
4962+
);
4963+
4964+
const lateChildPrefix = () => {
4965+
const entries = resumeSubagentTranscript.entries;
4966+
const labelIndex = (label: string) =>
4967+
entries.findIndex((entry) => entry.type === "emit_inbound" && entry.label === label);
4968+
const rootCompleted = labelIndex("turn/completed/root");
4969+
return {
4970+
spawned: entries.slice(0, labelIndex(`turn/started/${RESUME_CHILD_TURN_1}`) + 1),
4971+
rootFinished: entries.slice(rootCompleted - 1, rootCompleted + 1),
4972+
};
4973+
};
4974+
4975+
it.effect("names a late child spawned without a prompt in its wake", () =>
4976+
Effect.scoped(
4977+
Effect.gen(function* () {
4978+
const { spawned, rootFinished } = lateChildPrefix();
4979+
const harness = yield* makeCodexReplayHarness(
4980+
makeCodexReplayTranscript({
4981+
scenario: RESUME_SCENARIO,
4982+
entries: [
4983+
...spawned.map((entry) =>
4984+
entry.type === "emit_inbound" &&
4985+
entry.label === "item/completed/subAgentActivity-started"
4986+
? {
4987+
...entry,
4988+
frame: {
4989+
method: "item/completed",
4990+
params: {
4991+
item: {
4992+
type: "collabAgentToolCall",
4993+
id: "call-codex-resume-spawn",
4994+
tool: "spawnAgent",
4995+
status: "completed",
4996+
senderThreadId: RESUME_NATIVE_THREAD,
4997+
receiverThreadIds: [RESUME_CHILD_THREAD],
4998+
agentsStates: {},
4999+
},
5000+
threadId: RESUME_NATIVE_THREAD,
5001+
turnId: RESUME_NATIVE_TURN,
5002+
completedAtMs: 1782622441000,
5003+
},
5004+
},
5005+
}
5006+
: entry,
5007+
),
5008+
...rootFinished,
5009+
childAgentMessage({
5010+
id: "child-late-answer",
5011+
text: "UNPROMPTED_CHILD_DONE",
5012+
turnId: RESUME_CHILD_TURN_1,
5013+
completedAtMs: 1782622480000,
5014+
afterMs: 60_000,
5015+
}),
5016+
childTurnCompleted(RESUME_CHILD_TURN_1),
5017+
],
5018+
}),
5019+
);
5020+
5021+
yield* harness.runtime.startTurn(
5022+
makeCodexTestTurnInput({
5023+
threadId: harness.threadId,
5024+
providerThread: harness.providerThread,
5025+
now: yield* DateTime.now,
5026+
attemptId: RunAttemptId.make("attempt-codex-unprompted-child"),
5027+
text: RESUME_PROMPT,
5028+
}),
5029+
);
5030+
yield* harness.awaitTerminal(1);
5031+
yield* TestClock.adjust("60 seconds");
5032+
yield* harness.until(() => harness.continuationRequests.length > 0);
5033+
const subagent = harness.subagentUpdates().at(-1)?.subagent;
5034+
assert.equal(subagent?.prompt, "");
5035+
assert.equal(
5036+
harness.continuationRequests[0]?.detail,
5037+
`Subagent completed: ${subagent?.id}\n\nResult:\nUNPROMPTED_CHILD_DONE`,
5038+
);
5039+
}).pipe(
5040+
Effect.provide(
5041+
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
5042+
),
5043+
),
5044+
),
5045+
);
5046+
5047+
it.effect("wakes the root only when its direct child finishes, not a grandchild", () =>
5048+
Effect.scoped(
5049+
Effect.gen(function* () {
5050+
const grandchildThread = "native-codex-resume-grandchild-thread";
5051+
const grandchildTurn = "native-codex-resume-grandchild-turn";
5052+
const { spawned, rootFinished } = lateChildPrefix();
5053+
const harness = yield* makeCodexReplayHarness(
5054+
makeCodexReplayTranscript({
5055+
scenario: RESUME_SCENARIO,
5056+
entries: [
5057+
...spawned,
5058+
{
5059+
type: "emit_inbound",
5060+
label: "item/completed/grandchild-spawn",
5061+
frame: {
5062+
method: "item/completed",
5063+
params: {
5064+
item: {
5065+
type: "subAgentActivity",
5066+
id: "call-codex-grandchild-spawn",
5067+
kind: "started",
5068+
agentThreadId: grandchildThread,
5069+
agentPath: "/root/resume_agent/grandchild",
5070+
},
5071+
threadId: RESUME_CHILD_THREAD,
5072+
turnId: RESUME_CHILD_TURN_1,
5073+
completedAtMs: 1782622441500,
5074+
},
5075+
},
5076+
},
5077+
{
5078+
type: "emit_inbound",
5079+
label: "turn/started/grandchild",
5080+
frame: {
5081+
method: "turn/started",
5082+
params: {
5083+
threadId: grandchildThread,
5084+
turn: makeCodexReplayTurn({ id: grandchildTurn, status: "inProgress" }),
5085+
},
5086+
},
5087+
},
5088+
...rootFinished,
5089+
{
5090+
type: "emit_inbound",
5091+
label: "item/completed/grandchild-answer",
5092+
afterMs: 30_000,
5093+
frame: {
5094+
method: "item/completed",
5095+
params: {
5096+
item: {
5097+
type: "agentMessage",
5098+
id: "grandchild-answer",
5099+
text: "GRANDCHILD_DONE",
5100+
phase: "final_answer",
5101+
memoryCitation: null,
5102+
},
5103+
threadId: grandchildThread,
5104+
turnId: grandchildTurn,
5105+
completedAtMs: 1782622470000,
5106+
},
5107+
},
5108+
},
5109+
{
5110+
type: "emit_inbound",
5111+
label: "turn/completed/grandchild",
5112+
frame: {
5113+
method: "turn/completed",
5114+
params: {
5115+
threadId: grandchildThread,
5116+
turn: makeCodexReplayTurn({ id: grandchildTurn, status: "completed" }),
5117+
},
5118+
},
5119+
},
5120+
childAgentMessage({
5121+
id: "child-late-answer",
5122+
text: "CHILD_AFTER_GRANDCHILD",
5123+
turnId: RESUME_CHILD_TURN_1,
5124+
completedAtMs: 1782622500000,
5125+
afterMs: 30_000,
5126+
}),
5127+
childTurnCompleted(RESUME_CHILD_TURN_1),
5128+
],
5129+
}),
5130+
);
5131+
5132+
yield* harness.runtime.startTurn(
5133+
makeCodexTestTurnInput({
5134+
threadId: harness.threadId,
5135+
providerThread: harness.providerThread,
5136+
now: yield* DateTime.now,
5137+
attemptId: RunAttemptId.make("attempt-codex-late-grandchild"),
5138+
text: RESUME_PROMPT,
5139+
}),
5140+
);
5141+
yield* harness.awaitTerminal(1);
5142+
5143+
yield* TestClock.adjust("30 seconds");
5144+
yield* harness.until(() =>
5145+
harness.subagentUpdates().some((event) => event.subagent.result === "GRANDCHILD_DONE"),
5146+
);
5147+
assert.lengthOf(harness.continuationRequests, 0);
5148+
5149+
yield* TestClock.adjust("30 seconds");
5150+
yield* harness.until(() => harness.continuationRequests.length > 0);
5151+
assert.lengthOf(harness.continuationRequests, 1);
5152+
assert.include(harness.continuationRequests[0]?.detail, "CHILD_AFTER_GRANDCHILD");
5153+
}).pipe(
5154+
Effect.provide(
5155+
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
5156+
),
5157+
),
5158+
),
5159+
);
5160+
5161+
it.effect("leaves a late child's result to the root's next turn when one is running", () =>
5162+
Effect.scoped(
5163+
Effect.gen(function* () {
5164+
const secondPrompt = "Wait for the sub-agent.";
5165+
const secondTurn = "native-codex-resume-root-turn-2";
5166+
const { spawned, rootFinished } = lateChildPrefix();
5167+
// Reuse the preamble's turn/start exchange under the session's next request id.
5168+
const secondTurnStart = codexReplayPreamble({
5169+
nativeThreadId: RESUME_NATIVE_THREAD,
5170+
nativeTurnId: secondTurn,
5171+
prompt: secondPrompt,
5172+
})
5173+
.slice(-3)
5174+
.map((entry) =>
5175+
entry.type !== "runtime_exit" &&
5176+
typeof entry.frame === "object" &&
5177+
entry.frame !== null &&
5178+
"id" in entry.frame
5179+
? { ...entry, frame: { ...entry.frame, id: 4 } }
5180+
: entry,
5181+
);
5182+
const harness = yield* makeCodexReplayHarness(
5183+
makeCodexReplayTranscript({
5184+
scenario: RESUME_SCENARIO,
5185+
entries: [
5186+
...spawned,
5187+
...rootFinished,
5188+
...secondTurnStart,
5189+
childAgentMessage({
5190+
id: "child-late-answer",
5191+
text: "CHILD_DURING_SECOND_TURN",
5192+
turnId: RESUME_CHILD_TURN_1,
5193+
completedAtMs: 1782622480000,
5194+
}),
5195+
childTurnCompleted(RESUME_CHILD_TURN_1),
5196+
{
5197+
type: "emit_inbound",
5198+
label: "turn/completed/root-2",
5199+
frame: {
5200+
method: "turn/completed",
5201+
params: {
5202+
threadId: RESUME_NATIVE_THREAD,
5203+
turn: makeCodexReplayTurn({ id: secondTurn, status: "completed" }),
5204+
},
5205+
},
5206+
},
5207+
],
5208+
}),
5209+
);
5210+
5211+
const turnInput = (index: number, text: string) =>
5212+
Effect.gen(function* () {
5213+
return {
5214+
...makeCodexTestTurnInput({
5215+
threadId: harness.threadId,
5216+
providerThread: harness.providerThread,
5217+
now: yield* DateTime.now,
5218+
attemptId: RunAttemptId.make(`attempt-codex-active-root-${index}`),
5219+
text,
5220+
}),
5221+
runOrdinal: index,
5222+
providerTurnOrdinal: index,
5223+
};
5224+
});
5225+
yield* harness.runtime.startTurn(yield* turnInput(1, RESUME_PROMPT));
5226+
yield* harness.awaitTerminal(1);
5227+
yield* harness.runtime.startTurn(yield* turnInput(2, secondPrompt));
5228+
yield* harness.awaitTerminal(2);
5229+
assert.equal(harness.subagentUpdates().at(-1)?.subagent.result, "CHILD_DURING_SECOND_TURN");
5230+
assert.lengthOf(harness.continuationRequests, 0);
5231+
}).pipe(
5232+
Effect.provide(
5233+
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
5234+
),
5235+
),
5236+
),
5237+
);
5238+
49105239
it.effect.each(["model/rerouted"] as const)(
49115240
"keeps %s child metadata when an older lookup finishes later",
49125241
(method) =>
@@ -5046,7 +5375,9 @@ describe("CodexAdapterV2 post-settle continuation", () => {
50465375
});
50475376
assert.isFalse(yield* harness.hasPendingBackgroundWork);
50485377
assert.lengthOf(harness.terminalEvents(), 1);
5049-
assert.lengthOf(harness.continuationRequests, 0);
5378+
// The first child turn finished inside the root turn; only the resume wakes it.
5379+
assert.lengthOf(harness.continuationRequests, 1);
5380+
assert.include(harness.continuationRequests[0]?.detail, "CODEX_RESUME_DONE");
50505381
}).pipe(
50515382
Effect.provide(
50525383
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),

0 commit comments

Comments
 (0)