Skip to content

Commit 6919aa4

Browse files
only21milclaudemaria-rcks
authored
fix(codex): keep late subagent interrupts from freezing other threads (#25322)
Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: maria-rcks <[email protected]>
1 parent 9474bcb commit 6919aa4

2 files changed

Lines changed: 141 additions & 13 deletions

File tree

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

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4909,6 +4909,132 @@ describe("CodexAdapterV2 post-settle continuation", () => {
49094909
),
49104910
);
49114911

4912+
const interruptSubagentTurnAfterStopTranscript = makeCodexReplayTranscript({
4913+
scenario: "codex-interrupt-subagent-turn-after-stop",
4914+
entries: [
4915+
...interruptSubagentCommandTranscript.entries.slice(0, -1),
4916+
{
4917+
type: "emit_inbound",
4918+
label: "turn/started/child-after-stop",
4919+
frame: {
4920+
method: "turn/started",
4921+
params: {
4922+
threadId: INTERRUPT_CHILD_NATIVE_THREAD,
4923+
turn: makeCodexReplayTurn({
4924+
id: INTERRUPT_LATE_CHILD_NATIVE_TURN,
4925+
status: "inProgress",
4926+
}),
4927+
},
4928+
},
4929+
},
4930+
{
4931+
type: "expect_outbound",
4932+
label: "turn/interrupt/child-after-stop",
4933+
frame: {
4934+
id: 7,
4935+
method: "turn/interrupt",
4936+
params: {
4937+
threadId: INTERRUPT_CHILD_NATIVE_THREAD,
4938+
turnId: INTERRUPT_LATE_CHILD_NATIVE_TURN,
4939+
},
4940+
},
4941+
},
4942+
{
4943+
type: "emit_inbound",
4944+
label: "turn/interrupt/child-after-stop",
4945+
frame: { id: 7, result: {} },
4946+
},
4947+
{
4948+
type: "emit_inbound",
4949+
label: "thread/goal/updated/root",
4950+
frame: {
4951+
method: "thread/goal/updated",
4952+
params: {
4953+
threadId: INTERRUPT_NATIVE_THREAD,
4954+
turnId: null,
4955+
goal: {
4956+
threadId: INTERRUPT_NATIVE_THREAD,
4957+
objective: INTERRUPT_PROMPT,
4958+
status: "paused",
4959+
tokenBudget: null,
4960+
tokensUsed: 0,
4961+
timeUsedSeconds: 0,
4962+
createdAt: 1782622440,
4963+
updatedAt: 1782622450,
4964+
},
4965+
},
4966+
},
4967+
},
4968+
{ type: "runtime_exit", status: "success" },
4969+
],
4970+
});
4971+
4972+
it.effect(
4973+
"keeps reading notifications while interrupting a child turn that starts after Stop",
4974+
() =>
4975+
Effect.scoped(
4976+
Effect.gen(function* () {
4977+
const harness = yield* makeCodexReplayHarness(interruptSubagentTurnAfterStopTranscript);
4978+
const now = yield* DateTime.now;
4979+
4980+
yield* harness.runtime.startTurn(
4981+
makeCodexTestTurnInput({
4982+
threadId: harness.threadId,
4983+
providerThread: harness.providerThread,
4984+
now,
4985+
attemptId: RunAttemptId.make("attempt-codex-interrupt-subagent-turn-after-stop"),
4986+
text: INTERRUPT_PROMPT,
4987+
}),
4988+
);
4989+
yield* harness.until(() =>
4990+
harness.events.some(
4991+
(event) =>
4992+
event.type === "turn_item.updated" &&
4993+
event.turnItem.type === "command_execution" &&
4994+
event.turnItem.nativeItemRef?.nativeId === INTERRUPT_CHILD_COMMAND_ITEM &&
4995+
event.turnItem.status === "running",
4996+
),
4997+
);
4998+
const providerTurnId = harness.events.find(
4999+
(
5000+
event,
5001+
): event is Extract<
5002+
ProviderAdapter.ProviderAdapterV2Event,
5003+
{ type: "provider_turn.updated" }
5004+
> => event.type === "provider_turn.updated" && event.threadId === harness.threadId,
5005+
)?.providerTurn.id;
5006+
assert.isDefined(providerTurnId);
5007+
5008+
yield* harness.runtime.interruptTurn({
5009+
providerThread: harness.providerThread,
5010+
providerTurnId,
5011+
});
5012+
yield* harness.awaitTerminal(1);
5013+
5014+
// The TestClock never reaches the interrupt's timeout, so this arrives only
5015+
// if the reader is free to read the interrupt response and the frame after it.
5016+
yield* harness.until(() =>
5017+
harness.events.some(
5018+
(event) =>
5019+
event.type === "provider_thread.updated" &&
5020+
event.providerThread.goal?.status === "paused",
5021+
),
5022+
);
5023+
assert.isFalse(
5024+
harness.events.some(
5025+
(event) =>
5026+
event.type === "provider_turn.updated" &&
5027+
event.providerTurn.nativeTurnRef?.nativeId === INTERRUPT_LATE_CHILD_NATIVE_TURN,
5028+
),
5029+
);
5030+
}).pipe(
5031+
Effect.provide(
5032+
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
5033+
),
5034+
),
5035+
),
5036+
);
5037+
49125038
it.effect.each([
49135039
{
49145040
name: "usage",

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

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2565,31 +2565,33 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
25652565
while (ancestor !== undefined) {
25662566
if (terminalizedNativeTurns.has(ancestor.nativeTurnId)) {
25672567
const nativeThreadId = yield* getNativeThreadId(subagent.providerThread);
2568-
const interrupted = yield* client
2568+
// Fork requests off the notification reader so it can read their responses.
2569+
yield* client
25692570
.request("turn/interrupt", {
25702571
threadId: nativeThreadId,
25712572
turnId: turn.nativeTurnId,
25722573
})
25732574
.pipe(
2574-
Effect.as(true),
25752575
Effect.catch((cause) =>
25762576
Effect.logWarning("orchestration-v2.codex-late-subagent-interrupt-failed", {
25772577
nativeThreadId,
25782578
nativeTurnId: turn.nativeTurnId,
25792579
cause,
2580-
}).pipe(Effect.as(false)),
2580+
}),
25812581
),
2582-
Effect.timeoutOption("10 seconds"),
2583-
);
2584-
if (Option.isNone(interrupted)) {
2585-
yield* Effect.logWarning(
2586-
"orchestration-v2.codex-late-subagent-interrupt-timeout",
2587-
{
2588-
nativeThreadId,
2589-
nativeTurnId: turn.nativeTurnId,
2590-
},
2582+
Effect.timeoutOrElse({
2583+
duration: "10 seconds",
2584+
orElse: () =>
2585+
Effect.logWarning(
2586+
"orchestration-v2.codex-late-subagent-interrupt-timeout",
2587+
{
2588+
nativeThreadId,
2589+
nativeTurnId: turn.nativeTurnId,
2590+
},
2591+
),
2592+
}),
2593+
Effect.forkIn(scope),
25912594
);
2592-
}
25932595
return;
25942596
}
25952597
ancestor = ancestor.subagent?.parentContext;

0 commit comments

Comments
 (0)