Skip to content

Commit 73d0579

Browse files
only21milclaude
andcommitted
fix(codex): keep late subagent interrupts from freezing other threads
When a native child starts a turn after its root run was stopped or failed, the adapter sent turn/interrupt and waited for the reply inside the turn/started notification handler. The protocol reads replies on the same fiber that runs notification handlers, so the reply could not be read until the 10 second timeout fired. Every Codex thread on that app-server stalled for those 10 seconds and the timeout warning was false. Fork the interrupt off the notification reader, as the nearby goal-turn interrupt and child model lookup already do. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent d1f7018 commit 73d0579

2 files changed

Lines changed: 140 additions & 10 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
@@ -3911,6 +3911,132 @@ describe("CodexAdapterV2 post-settle continuation", () => {
39113911
),
39123912
);
39133913

3914+
const interruptSubagentTurnAfterStopTranscript = makeCodexReplayTranscript({
3915+
scenario: "codex-interrupt-subagent-turn-after-stop",
3916+
entries: [
3917+
...interruptSubagentCommandTranscript.entries.slice(0, -1),
3918+
{
3919+
type: "emit_inbound",
3920+
label: "turn/started/child-after-stop",
3921+
frame: {
3922+
method: "turn/started",
3923+
params: {
3924+
threadId: INTERRUPT_CHILD_NATIVE_THREAD,
3925+
turn: makeCodexReplayTurn({
3926+
id: INTERRUPT_LATE_CHILD_NATIVE_TURN,
3927+
status: "inProgress",
3928+
}),
3929+
},
3930+
},
3931+
},
3932+
{
3933+
type: "expect_outbound",
3934+
label: "turn/interrupt/child-after-stop",
3935+
frame: {
3936+
id: 7,
3937+
method: "turn/interrupt",
3938+
params: {
3939+
threadId: INTERRUPT_CHILD_NATIVE_THREAD,
3940+
turnId: INTERRUPT_LATE_CHILD_NATIVE_TURN,
3941+
},
3942+
},
3943+
},
3944+
{
3945+
type: "emit_inbound",
3946+
label: "turn/interrupt/child-after-stop",
3947+
frame: { id: 7, result: {} },
3948+
},
3949+
{
3950+
type: "emit_inbound",
3951+
label: "thread/goal/updated/root",
3952+
frame: {
3953+
method: "thread/goal/updated",
3954+
params: {
3955+
threadId: INTERRUPT_NATIVE_THREAD,
3956+
turnId: null,
3957+
goal: {
3958+
threadId: INTERRUPT_NATIVE_THREAD,
3959+
objective: INTERRUPT_PROMPT,
3960+
status: "paused",
3961+
tokenBudget: null,
3962+
tokensUsed: 0,
3963+
timeUsedSeconds: 0,
3964+
createdAt: 1782622440,
3965+
updatedAt: 1782622450,
3966+
},
3967+
},
3968+
},
3969+
},
3970+
{ type: "runtime_exit", status: "success" },
3971+
],
3972+
});
3973+
3974+
it.effect(
3975+
"keeps reading notifications while interrupting a child turn that starts after Stop",
3976+
() =>
3977+
Effect.scoped(
3978+
Effect.gen(function* () {
3979+
const harness = yield* makeCodexReplayHarness(interruptSubagentTurnAfterStopTranscript);
3980+
const now = yield* DateTime.now;
3981+
3982+
yield* harness.runtime.startTurn(
3983+
makeCodexTestTurnInput({
3984+
threadId: harness.threadId,
3985+
providerThread: harness.providerThread,
3986+
now,
3987+
attemptId: RunAttemptId.make("attempt-codex-interrupt-subagent-turn-after-stop"),
3988+
text: INTERRUPT_PROMPT,
3989+
}),
3990+
);
3991+
yield* harness.until(() =>
3992+
harness.events.some(
3993+
(event) =>
3994+
event.type === "turn_item.updated" &&
3995+
event.turnItem.type === "command_execution" &&
3996+
event.turnItem.nativeItemRef?.nativeId === INTERRUPT_CHILD_COMMAND_ITEM &&
3997+
event.turnItem.status === "running",
3998+
),
3999+
);
4000+
const providerTurnId = harness.events.find(
4001+
(
4002+
event,
4003+
): event is Extract<
4004+
ProviderAdapter.ProviderAdapterV2Event,
4005+
{ type: "provider_turn.updated" }
4006+
> => event.type === "provider_turn.updated" && event.threadId === harness.threadId,
4007+
)?.providerTurn.id;
4008+
assert.isDefined(providerTurnId);
4009+
4010+
yield* harness.runtime.interruptTurn({
4011+
providerThread: harness.providerThread,
4012+
providerTurnId,
4013+
});
4014+
yield* harness.awaitTerminal(1);
4015+
4016+
// The TestClock never reaches the interrupt's timeout, so this arrives only
4017+
// if the reader is free to read the interrupt response and the frame after it.
4018+
yield* harness.until(() =>
4019+
harness.events.some(
4020+
(event) =>
4021+
event.type === "provider_thread.updated" &&
4022+
event.providerThread.goal?.status === "paused",
4023+
),
4024+
);
4025+
assert.isFalse(
4026+
harness.events.some(
4027+
(event) =>
4028+
event.type === "provider_turn.updated" &&
4029+
event.providerTurn.nativeTurnRef?.nativeId === INTERRUPT_LATE_CHILD_NATIVE_TURN,
4030+
),
4031+
);
4032+
}).pipe(
4033+
Effect.provide(
4034+
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
4035+
),
4036+
),
4037+
),
4038+
);
4039+
39144040
it.effect.each([
39154041
{
39164042
name: "usage",

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2455,7 +2455,8 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
24552455
while (ancestor !== undefined) {
24562456
if (terminalizedNativeTurns.has(ancestor.nativeTurnId)) {
24572457
const nativeThreadId = yield* getNativeThreadId(subagent.providerThread);
2458-
const interrupted = yield* client
2458+
// Fork requests off the notification reader so it can read their responses.
2459+
yield* client
24592460
.request("turn/interrupt", {
24602461
threadId: nativeThreadId,
24612462
turnId: turn.nativeTurnId,
@@ -2470,16 +2471,19 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
24702471
}).pipe(Effect.as(false)),
24712472
),
24722473
Effect.timeoutOption("10 seconds"),
2474+
Effect.flatMap((interrupted) =>
2475+
Option.isNone(interrupted)
2476+
? Effect.logWarning(
2477+
"orchestration-v2.codex-late-subagent-interrupt-timeout",
2478+
{
2479+
nativeThreadId,
2480+
nativeTurnId: turn.nativeTurnId,
2481+
},
2482+
)
2483+
: Effect.void,
2484+
),
2485+
Effect.forkIn(scope),
24732486
);
2474-
if (Option.isNone(interrupted)) {
2475-
yield* Effect.logWarning(
2476-
"orchestration-v2.codex-late-subagent-interrupt-timeout",
2477-
{
2478-
nativeThreadId,
2479-
nativeTurnId: turn.nativeTurnId,
2480-
},
2481-
);
2482-
}
24832487
return;
24842488
}
24852489
ancestor = ancestor.subagent?.parentContext;

0 commit comments

Comments
 (0)