TypeScript
fix(codex): keep late subagent interrupts from freezing other threads (#25322)
Victor Vogel6919aa4cf1bf
diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.tsindex 1f39dee9a10..e50b6689ef1 100644--- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts+++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts@@ -4909,6 +4909,132 @@ describe("CodexAdapterV2 post-settle continuation", () => {),);+ const interruptSubagentTurnAfterStopTranscript = makeCodexReplayTranscript({+ scenario: "codex-interrupt-subagent-turn-after-stop",+ entries: [+ ...interruptSubagentCommandTranscript.entries.slice(0, -1),+ {+ type: "emit_inbound",+ label: "turn/started/child-after-stop",+ frame: {+ method: "turn/started",+ params: {+ threadId: INTERRUPT_CHILD_NATIVE_THREAD,+ turn: makeCodexReplayTurn({+ id: INTERRUPT_LATE_CHILD_NATIVE_TURN,+ status: "inProgress",+ }),+ },+ },+ },+ {+ type: "expect_outbound",+ label: "turn/interrupt/child-after-stop",+ frame: {+ id: 7,+ method: "turn/interrupt",+ params: {+ threadId: INTERRUPT_CHILD_NATIVE_THREAD,+ turnId: INTERRUPT_LATE_CHILD_NATIVE_TURN,+ },+ },+ },+ {+ type: "emit_inbound",+ label: "turn/interrupt/child-after-stop",+ frame: { id: 7, result: {} },+ },+ {+ type: "emit_inbound",+ label: "thread/goal/updated/root",+ frame: {+ method: "thread/goal/updated",+ params: {+ threadId: INTERRUPT_NATIVE_THREAD,+ turnId: null,+ goal: {+ threadId: INTERRUPT_NATIVE_THREAD,+ objective: INTERRUPT_PROMPT,+ status: "paused",+ tokenBudget: null,+ tokensUsed: 0,+ timeUsedSeconds: 0,+ createdAt: 1782622440,+ updatedAt: 1782622450,+ },+ },+ },+ },+ { type: "runtime_exit", status: "success" },+ ],+ });++ it.effect(+ "keeps reading notifications while interrupting a child turn that starts after Stop",+ () =>+ Effect.scoped(+ Effect.gen(function* () {+ const harness = yield* makeCodexReplayHarness(interruptSubagentTurnAfterStopTranscript);+ const now = yield* DateTime.now;++ yield* harness.runtime.startTurn(+ makeCodexTestTurnInput({+ threadId: harness.threadId,+ providerThread: harness.providerThread,+ now,+ attemptId: RunAttemptId.make("attempt-codex-interrupt-subagent-turn-after-stop"),+ text: INTERRUPT_PROMPT,+ }),+ );+ yield* harness.until(() =>+ harness.events.some(+ (event) =>+ event.type === "turn_item.updated" &&+ event.turnItem.type === "command_execution" &&+ event.turnItem.nativeItemRef?.nativeId === INTERRUPT_CHILD_COMMAND_ITEM &&+ event.turnItem.status === "running",+ ),+ );+ const providerTurnId = harness.events.find(+ (+ event,+ ): event is Extract<+ ProviderAdapter.ProviderAdapterV2Event,+ { type: "provider_turn.updated" }+ > => event.type === "provider_turn.updated" && event.threadId === harness.threadId,+ )?.providerTurn.id;+ assert.isDefined(providerTurnId);++ yield* harness.runtime.interruptTurn({+ providerThread: harness.providerThread,+ providerTurnId,+ });+ yield* harness.awaitTerminal(1);++ // The TestClock never reaches the interrupt's timeout, so this arrives only+ // if the reader is free to read the interrupt response and the frame after it.+ yield* harness.until(() =>+ harness.events.some(+ (event) =>+ event.type === "provider_thread.updated" &&+ event.providerThread.goal?.status === "paused",+ ),+ );+ assert.isFalse(+ harness.events.some(+ (event) =>+ event.type === "provider_turn.updated" &&+ event.providerTurn.nativeTurnRef?.nativeId === INTERRUPT_LATE_CHILD_NATIVE_TURN,+ ),+ );+ }).pipe(+ Effect.provide(+ Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),+ ),+ ),+ ),+ );+it.effect.each([{name: "usage",diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.tsindex d637851fb38..2e2fdd4331c 100644--- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts+++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts@@ -2565,31 +2565,33 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (while (ancestor !== undefined) {if (terminalizedNativeTurns.has(ancestor.nativeTurnId)) {const nativeThreadId = yield* getNativeThreadId(subagent.providerThread);- const interrupted = yield* client+ // Fork requests off the notification reader so it can read their responses.+ yield* client.request("turn/interrupt", {threadId: nativeThreadId,turnId: turn.nativeTurnId,}).pipe(- Effect.as(true),Effect.catch((cause) =>Effect.logWarning("orchestration-v2.codex-late-subagent-interrupt-failed", {nativeThreadId,nativeTurnId: turn.nativeTurnId,cause,- }).pipe(Effect.as(false)),+ }),),- Effect.timeoutOption("10 seconds"),- );- if (Option.isNone(interrupted)) {- yield* Effect.logWarning(- "orchestration-v2.codex-late-subagent-interrupt-timeout",- {- nativeThreadId,- nativeTurnId: turn.nativeTurnId,- },+ Effect.timeoutOrElse({+ duration: "10 seconds",+ orElse: () =>+ Effect.logWarning(+ "orchestration-v2.codex-late-subagent-interrupt-timeout",+ {+ nativeThreadId,+ nativeTurnId: turn.nativeTurnId,+ },+ ),+ }),+ Effect.forkIn(scope),);- }return;}ancestor = ancestor.subagent?.parentContext;