@devries-cameron20 / t3code-1 / commits / 11adcb0e25
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): resume Codex parents after native children finish (#23840)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Oct 22, 2026 at 15:41 UTC 11adcb0e25a04785f70d01c3f2a858889683007e
2 files changed +379 -1
apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts
+332 -1
@@ -4907,6 +4907,335 @@ describe("CodexAdapterV2 post-settle continuation", () => {
4907 ],
4908 });
4909
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 +
5239 it.effect.each(["model/rerouted"] as const)(
5240 "keeps %s child metadata when an older lookup finishes later",
5241 (method) =>
@@ -5046,7 +5375,9 @@ describe("CodexAdapterV2 post-settle continuation", () => {
5375 });
5376 assert.isFalse(yield* harness.hasPendingBackgroundWork);
5377 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");
5381 }).pipe(
5382 Effect.provide(
5383 Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts
+47
@@ -448,6 +448,18 @@ function codexBackgroundCommandDetail(item: {
448 return outputTail.length === 0 ? header : `${header}\n\nOutput tail:\n${outputTail}`;
449 }
450
451 +// Codex spawns can omit the prompt, which registers as an empty string.
452 +function codexNativeSubagentLabel(task: OrchestrationV2Subagent): string | undefined {
453 + return task.title?.trim() || task.prompt.trim() || undefined;
454 +}
455 +
456 +function codexNativeSubagentDetail(task: OrchestrationV2Subagent): string {
457 + const header = `Subagent ${task.status}: ${codexNativeSubagentLabel(task) ?? task.id}`;
458 + return task.result == null || task.result.length === 0
459 + ? header
460 + : `${header}\n\nResult:\n${task.result}`;
461 +}
462 +
463 export interface CodexDynamicToolProjection extends McpToolPresentation {
464 readonly toolName: string;
465 readonly title?: string;
@@ -6053,6 +6065,41 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
6065 status: input.status,
6066 completedAt: input.completedAt,
6067 });
6068 + // Codex does not wake a parent whose turn already completed, so
6069 + // the child's result would sit unread until the next message.
6070 + // Only direct children of a root turn wake it: a grandchild
6071 + // reports to its own parent, and a root turn already running on
6072 + // the thread receives the result natively.
6073 + const parent = input.context.subagent.parentContext;
6074 + if (
6075 + continuationRequests !== undefined &&
6076 + parent.subagent === null &&
6077 + (input.status === "completed" || input.status === "failed") &&
6078 + !(yield* Ref.get(terminalizedNonCompletedNativeTurns)).has(parent.nativeTurnId) &&
6079 + ![...(yield* Ref.get(activeTurns)).values()].some(
6080 + (active) =>
6081 + active.subagent === null &&
6082 + active.providerThread.id === parent.providerThread.id,
6083 + )
6084 + ) {
6085 + const task = input.context.subagent.task;
6086 + yield* continuationRequests.offer({
6087 + threadId: parent.projectionThreadId,
6088 + providerThreadId: parent.providerThread.id,
6089 + driver: CODEX_PROVIDER,
6090 + detail: codexNativeSubagentDetail(task),
6091 + // The detail is the whole wake; Codex buffers no output for it.
6092 + delivery: "message_text",
6093 + notification: backgroundWorkNotification([
6094 + {
6095 + kind: "subagent",
6096 + label: codexNativeSubagentLabel(task),
6097 + outcome: input.status,
6098 + childThreadId: input.context.subagent.childThreadId,
6099 + },
6100 + ]),
6101 + });
6102 + }
6103 }
6104 if (input.status === "interrupted" || input.status === "failed") {
6105 yield* terminalizeRunningCommandItems(