From 9a91ff0c7082d052f1fa89a6f91168500f82a697 Mon Sep 17 00:00:00 2001 From: jieyuexing Date: Fri, 20 Nov 2026 21:54:55 +0800 Subject: [PATCH] fix(server): a released provider session ends its tool call nodes too Releasing a provider session (detach, stream failure, provider stop, early close) interrupted the cut-off tool call turn items but left the tool_call execution node with the same nodeId running with no completed_at. The node update now goes into the same conditional write as the turn item update. Co-Authored-By: Claude Sonnet 5.5 --- .../ProviderSessionManager.test.ts | 43 ++++++++++++++++++- .../ProviderSessionManager.ts | 41 ++++++++++++------ 2 files changed, 69 insertions(+), 15 deletions(-) diff --git a/apps/server/src/orchestration-v2/ProviderSessionManager.test.ts b/apps/server/src/orchestration-v2/ProviderSessionManager.test.ts index 185bf0f019bbe..280c899882bad 100644 --- a/apps/server/src/orchestration-v2/ProviderSessionManager.test.ts +++ b/apps/server/src/orchestration-v2/ProviderSessionManager.test.ts @@ -3414,8 +3414,12 @@ const openSessionWithBackgroundCommand = (fixtureName: string) => toolName: "mcp__t3-code__t3_worktree_handoff", input: {}, }) satisfies OrchestrationV2TurnItem; + const cutOffNodeId = idAllocator.derive.nodeFromProviderItem({ + driver: CODEX_DRIVER, + nativeItemId: "cut-off", + }); const seededToolCalls = { - cutOff: toolCall("cut-off", providerThreads[0]!, "running", now), + cutOff: { ...toolCall("cut-off", providerThreads[0]!, "running", now), nodeId: cutOffNodeId }, finished: toolCall("finished", providerThreads[0]!, "completed", now), otherSession: toolCall("other-session", providerThreads[1]!, "running", now), // Started by a replacement session after the release. @@ -3441,7 +3445,41 @@ const openSessionWithBackgroundCommand = (fixtureName: string) => ], }); }); + yield* eventSink.write({ + events: [ + { + id: yield* idAllocator.allocate.event({ threadId }), + type: "node.updated", + threadId, + nodeId: cutOffNodeId, + driver: CODEX_DRIVER, + occurredAt: now, + payload: { + id: cutOffNodeId, + threadId, + runId: null, + parentNodeId: null, + rootNodeId: cutOffNodeId, + kind: "tool_call", + status: "running", + countsForRun: true, + providerThreadId: providerThreads[0]!.id, + providerTurnId: null, + nativeItemRef: null, + runtimeRequestId: null, + checkpointScopeId: null, + startedAt: now, + completedAt: null, + }, + }, + ], + }); yield* Effect.forEach(Object.values(seededToolCalls), writeToolCall); + const cutOffNodeStatus = Effect.gen(function* () { + const projectionStore = yield* ProjectionStore.ProjectionStoreV2; + const projection = yield* projectionStore.getThreadProjection(threadId); + return projection.nodes.find((node) => node.id === cutOffNodeId)?.status; + }); // A background native subagent that outlived its completed run; Claude records no provider thread on it. const nativeSubagentRunId = idAllocator.derive.run({ threadId, ordinal: 1 }); const nativeSubagentId = idAllocator.derive.nodeFromProviderItem({ @@ -3525,6 +3563,7 @@ const openSessionWithBackgroundCommand = (fixtureName: string) => toolCalls, toolCallStatus, nativeSubagentStatus, + cutOffNodeStatus, }; }); @@ -3544,6 +3583,7 @@ it.effect.each(["detach", "stream failure", "provider stop", "early close"] as c seededToolCalls, writeToolCall, nativeSubagentStatus, + cutOffNodeStatus, } = yield* openSessionWithBackgroundCommand("provider-session-manager-detach-roster"); const runtime = Option.getOrThrow(yield* manager.get(providerSessionId)); const subscription = yield* runtime.subscribeEvents!; @@ -3628,6 +3668,7 @@ it.effect.each(["detach", "stream failure", "provider stop", "early close"] as c otherSession: "running", replacement: "running", }); + assert.equal(yield* cutOffNodeStatus, "interrupted"); // regression: #36548 a native subagent the dead process ran stayed running after its run completed assert.notInclude(["pending", "running", "waiting"], yield* nativeSubagentStatus); }); diff --git a/apps/server/src/orchestration-v2/ProviderSessionManager.ts b/apps/server/src/orchestration-v2/ProviderSessionManager.ts index aa1938305b182..a495ec7be1630 100644 --- a/apps/server/src/orchestration-v2/ProviderSessionManager.ts +++ b/apps/server/src/orchestration-v2/ProviderSessionManager.ts @@ -1050,23 +1050,36 @@ export const layerWithOptions = ( // after each pass: queued events can also store more cut-off items. let pending = yield* readCutOff; for (let pass = 0; pass < 3 && pending.length > 0; pass++) { + const { nodes } = yield* projectionStore.getThreadRecords(threadId, ["nodes"], { + nodeIds: pending.flatMap((item) => (item.nodeId === null ? [] : [item.nodeId])), + }); for (const item of pending) { const now = yield* DateTime.now; - yield* eventSink.writeIfTurnItemUnchanged({ - expected: item, - events: [ - { - id: yield* idAllocator.allocate.event({ threadId, providerSessionId }), - type: "turn-item.updated", - threadId, - ...(item.runId === null ? {} : { runId: item.runId }), - ...(item.nodeId === null ? {} : { nodeId: item.nodeId }), - driver, - occurredAt: now, - payload: { ...item, status: "interrupted", completedAt: now, updatedAt: now }, - }, - ], + const node = nodes.find((candidate) => candidate.id === item.nodeId); + const events: Array = []; + if (node !== undefined && isOrchestrationV2WorkActive(node.status)) { + events.push({ + id: yield* idAllocator.allocate.event({ threadId, providerSessionId }), + type: "node.updated", + threadId, + ...(node.runId === null ? {} : { runId: node.runId }), + nodeId: node.id, + driver, + occurredAt: now, + payload: { ...node, status: "interrupted", completedAt: now }, + }); + } + events.push({ + id: yield* idAllocator.allocate.event({ threadId, providerSessionId }), + type: "turn-item.updated", + threadId, + ...(item.runId === null ? {} : { runId: item.runId }), + ...(item.nodeId === null ? {} : { nodeId: item.nodeId }), + driver, + occurredAt: now, + payload: { ...item, status: "interrupted", completedAt: now, updatedAt: now }, }); + yield* eventSink.writeIfTurnItemUnchanged({ expected: item, events }); } pending = yield* readCutOff; }