Skip to content

Commit 343c1ec

Browse files
fix(server): resume a waiting account switch when a background tool finishes
A Codex MCP or dynamic tool that outlives its turn settles with only a node update, so a queued native-history account switch never retried after the tool finished. The settled-work listener now also retries the queue on terminal tool_call node updates. AI-assisted; verified with the tests and tools listed above. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent f68acf6 commit 343c1ec

2 files changed

Lines changed: 60 additions & 27 deletions

File tree

‎apps/server/src/orchestration-v2/Orchestrator.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14366,14 +14366,24 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1436614366
afterSequence: terminalEventsAfterSequence,
1436714367
eventType: "provider-thread.updated",
1436814368
}),
14369-
eventSink.stream({ afterSequence: terminalEventsAfterSequence, eventType: "subagent.updated" }),
14369+
Stream.merge(
14370+
eventSink.stream({
14371+
afterSequence: terminalEventsAfterSequence,
14372+
eventType: "subagent.updated",
14373+
}),
14374+
// Codex MCP and dynamic tools that outlive their turn settle with only a node update.
14375+
eventSink.stream({ afterSequence: terminalEventsAfterSequence, eventType: "node.updated" }),
14376+
),
1437014377
).pipe(
1437114378
Stream.map((stored) => ({ stored, settlesChild: settlesChildWork(stored) })),
1437214379
Stream.filter(
1437314380
({ stored, settlesChild }) =>
1437414381
settlesChild ||
1437514382
(stored.event.type === "provider-thread.updated" &&
14376-
!backgroundWorkHoldsCompletion(stored.event.payload.pendingBackgroundTasks ?? [])),
14383+
!backgroundWorkHoldsCompletion(stored.event.payload.pendingBackgroundTasks ?? [])) ||
14384+
(stored.event.type === "node.updated" &&
14385+
stored.event.payload.kind === "tool_call" &&
14386+
!isOrchestrationV2WorkActive(stored.event.payload.status)),
1437714387
),
1437814388
Stream.runForEach(({ stored, settlesChild }) =>
1437914389
Effect.gen(function* () {

‎apps/server/src/orchestration-v2/testkit/ProviderSwitch.integration.test.ts‎

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2557,7 +2557,7 @@ describe("orchestration v2 provider switching", () => {
25572557
{ background: true, queued: false, sharedHistory: false, buffered: false, held: false },
25582558
],
25592559
[
2560-
"waits for background work before resuming a direct account switch",
2560+
"resumes a direct account switch when a background tool finishes",
25612561
{ background: true, queued: false, sharedHistory: true, buffered: false, held: false },
25622562
],
25632563
[
@@ -2807,31 +2807,54 @@ describe("orchestration v2 provider switching", () => {
28072807
type: "provider-thread.updated",
28082808
payload: { ...afterFirst.providerThreads[0]!, pendingBackgroundTasks: [] },
28092809
}
2810-
: {
2811-
...settledEvent,
2812-
type: "subagent.updated",
2813-
payload: {
2814-
id: NodeId.make("subagent:handoff-background"),
2815-
threadId: handoffThreadId,
2816-
runId: afterFirst.runs[0]!.id,
2817-
parentNodeId: afterFirst.runs[0]!.rootNodeId!,
2818-
origin: "provider_native",
2819-
createdBy: "agent",
2820-
driver: CLAUDE_DRIVER,
2821-
providerInstanceId: CLAUDE_MODEL_SELECTION.instanceId,
2822-
providerThreadId: afterFirst.providerThreads[0]!.id,
2823-
childThreadId: null,
2824-
nativeTaskRef: null,
2825-
prompt: "Background work",
2826-
title: null,
2827-
model: null,
2828-
status: "completed",
2829-
result: "Done",
2830-
startedAt: now,
2831-
completedAt: now,
2832-
updatedAt: now,
2810+
: !buffered
2811+
? {
2812+
// Like a Codex MCP tool finishing after its turn: no roster or subagent update.
2813+
...settledEvent,
2814+
type: "node.updated",
2815+
payload: {
2816+
id: NodeId.make("node:handoff-background-tool"),
2817+
threadId: handoffThreadId,
2818+
runId: afterFirst.runs[0]!.id,
2819+
parentNodeId: afterFirst.runs[0]!.rootNodeId!,
2820+
rootNodeId: afterFirst.runs[0]!.rootNodeId!,
2821+
kind: "tool_call",
2822+
status: "completed",
2823+
countsForRun: true,
2824+
providerThreadId: afterFirst.providerThreads[0]!.id,
2825+
providerTurnId: null,
2826+
nativeItemRef: null,
2827+
runtimeRequestId: null,
2828+
checkpointScopeId: null,
2829+
startedAt: now,
2830+
completedAt: now,
2831+
},
2832+
}
2833+
: {
2834+
...settledEvent,
2835+
type: "subagent.updated",
2836+
payload: {
2837+
id: NodeId.make("subagent:handoff-background"),
2838+
threadId: handoffThreadId,
2839+
runId: afterFirst.runs[0]!.id,
2840+
parentNodeId: afterFirst.runs[0]!.rootNodeId!,
2841+
origin: "provider_native",
2842+
createdBy: "agent",
2843+
driver: CLAUDE_DRIVER,
2844+
providerInstanceId: CLAUDE_MODEL_SELECTION.instanceId,
2845+
providerThreadId: afterFirst.providerThreads[0]!.id,
2846+
childThreadId: null,
2847+
nativeTaskRef: null,
2848+
prompt: "Background work",
2849+
title: null,
2850+
model: null,
2851+
status: "completed",
2852+
result: "Done",
2853+
startedAt: now,
2854+
completedAt: now,
2855+
updatedAt: now,
2856+
},
28332857
},
2834-
},
28352858
],
28362859
});
28372860
yield* orchestrator.streamStoredEvents.pipe(

0 commit comments

Comments
 (0)