Skip to content

Commit 0bf5d2b

Browse files
voltcrashclaude
andcommitted
fix(server): Claude subagents stopped by a replaced session no longer stay Running
When a Claude session is replaced, the new process resumes with an empty native-task registry. On resume it reports each background agent the old process left as a stopped task_notification (reason "worker_restart"), but updateClaudeSubagentNode returned early for any terminal update it could not find, so the durable subagent stayed running. The adapter now emits a `subagent.ended` event for a terminal native outcome it does not track. The ingestor settles the durable record with the existing endOrphanedNativeSubagent and endRunlessRootTurns helpers, keeping its own run and provider ownership, and the live run releases its hold on the inherited item. Fixes #18877 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 6524a8d commit 0bf5d2b

5 files changed

Lines changed: 360 additions & 0 deletions

File tree

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4874,6 +4874,18 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
48744874
: input.context.subagentsByToolUseId.get(input.toolUseId)) ??
48754875
registeredSubagent);
48764876
if (existingSubagent === undefined && input.status !== "running") {
4877+
// A task an earlier process started (the replacement reports it
4878+
// stopped on resume) is unknown here, so the server settles it.
4879+
if (input.nativeOutcome !== undefined) {
4880+
yield* emitProviderEvent({
4881+
type: "subagent.ended",
4882+
driver: CLAUDE_PROVIDER,
4883+
threadId: input.context.input.threadId,
4884+
subagentId: claudeSubagentIds(input.context, input.taskId).nodeId,
4885+
status: input.status,
4886+
result: input.result ?? null,
4887+
});
4888+
}
48774889
return;
48784890
}
48794891
if (
Lines changed: 261 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,261 @@
1+
import type { SDKMessage } from "@anthropic-ai/claude-agent-sdk";
2+
import * as NodeServices from "@effect/platform-node/NodeServices";
3+
import { assert, it } from "@effect/vitest";
4+
import {
5+
ClaudeSettings,
6+
CommandId,
7+
MessageId,
8+
ProjectId,
9+
ProviderInstanceId,
10+
ThreadId,
11+
type OrchestrationV2DomainEvent,
12+
} from "@t3tools/contracts";
13+
import * as Crypto from "effect/Crypto";
14+
import * as Effect from "effect/Effect";
15+
import * as Fiber from "effect/Fiber";
16+
import * as FileSystem from "effect/FileSystem";
17+
import * as Layer from "effect/Layer";
18+
import * as Path from "effect/Path";
19+
import * as Queue from "effect/Queue";
20+
import * as Schema from "effect/Schema";
21+
import * as Stream from "effect/Stream";
22+
import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
23+
import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderSessions";
24+
import { checkpointWorkspace } from "@t3tools/provider-testing/replayWorkspace";
25+
import * as ClaudeAdapterV2 from "./Adapters/ClaudeAdapterV2.ts";
26+
import * as EffectWorker from "./EffectWorker.ts";
27+
import * as EventSink from "./EventSink.ts";
28+
import * as Orchestrator from "./Orchestrator.ts";
29+
import * as ProviderAdapterRegistry from "./ProviderAdapterRegistry.ts";
30+
import * as ProviderSessionManager from "./ProviderSessionManager.ts";
31+
import * as ProviderReplayHarness from "./testkit/ProviderReplayHarness.ts";
32+
33+
const modelSelection = {
34+
instanceId: ProviderInstanceId.make("claudeAgent"),
35+
model: "claude-sonnet-4-6",
36+
};
37+
const sessionId = "orphaned-subagent-session";
38+
const decodeClaudeSettings = Schema.decodeEffect(ClaudeSettings);
39+
const frame = (value: unknown) => value as SDKMessage;
40+
41+
// Only the SDK stream is replayed; adapter, session manager, ingestor,
42+
// orchestrator, and SQLite projections are the production ones.
43+
it.effect("settles a subagent the replacement Claude process reports stopped after resume", () =>
44+
Effect.scoped(
45+
Effect.gen(function* () {
46+
const cwd = yield* checkpointWorkspace("claude-orphaned-subagent");
47+
const processes: Array<Queue.Queue<SDKMessage>> = [];
48+
const adapter = yield* ClaudeAdapterV2.makeClaudeAdapterV2({
49+
instanceId: modelSelection.instanceId,
50+
settings: yield* decodeClaudeSettings({}),
51+
environment: {},
52+
attachmentsDir: cwd,
53+
fileSystem: yield* FileSystem.FileSystem,
54+
path: yield* Path.Path,
55+
crypto: yield* Crypto.Crypto,
56+
idAllocator: yield* IdAllocator.IdAllocatorV2,
57+
queryRunner: {
58+
allocateSessionId: Effect.succeed(sessionId),
59+
assertComplete: Effect.void,
60+
subagentLaunchToolUseId: () => Effect.succeed(null),
61+
open: () =>
62+
Effect.gen(function* () {
63+
const messages = yield* Queue.unbounded<SDKMessage>();
64+
processes.push(messages);
65+
return {
66+
messages: Stream.fromQueue(messages),
67+
offer: () => Effect.void,
68+
setModel: () => Effect.void,
69+
setPermissionMode: () => Effect.void,
70+
stopTask: () => Effect.void,
71+
interrupt: Effect.void,
72+
close: Queue.shutdown(messages),
73+
};
74+
}),
75+
forkSession: () => Effect.die("unused forkSession"),
76+
},
77+
});
78+
yield* Effect.gen(function* () {
79+
const orchestrator = yield* Orchestrator.OrchestratorV2;
80+
const sink = yield* EventSink.EventSinkV2;
81+
const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
82+
const manager = yield* ProviderSessionManager.ProviderSessionManagerV2;
83+
const threadId = ThreadId.make("thread:orphaned-subagent");
84+
// Subscribe before offering the frame so a fast commit is not missed.
85+
const awaitEvent = (predicate: (event: OrchestrationV2DomainEvent) => boolean) =>
86+
Effect.gen(function* () {
87+
const afterSequence = yield* sink.latestSequence();
88+
return yield* sink.stream({ afterSequence }).pipe(
89+
Stream.filter((stored) => predicate(stored.event)),
90+
Stream.take(1),
91+
Stream.runDrain,
92+
Effect.forkScoped,
93+
);
94+
});
95+
const sendMessage = (id: string) =>
96+
orchestrator.dispatch({
97+
type: "message.dispatch",
98+
commandId: CommandId.make(id),
99+
threadId,
100+
messageId: MessageId.make(id),
101+
text: "Continue.",
102+
attachments: [],
103+
dispatchMode: { type: "start_immediately" },
104+
createdBy: "user",
105+
creationSource: "web",
106+
});
107+
yield* orchestrator.dispatch({
108+
type: "thread.create",
109+
commandId: CommandId.make("create"),
110+
threadId,
111+
projectId: ProjectId.make("project:orphaned-subagent"),
112+
title: "Orphaned subagent",
113+
modelSelection,
114+
runtimeMode: "full-access",
115+
interactionMode: "default",
116+
branch: null,
117+
worktreePath: cwd,
118+
createdBy: "user",
119+
creationSource: "web",
120+
});
121+
yield* sendMessage("first");
122+
yield* worker.drain();
123+
const first = processes[0]!;
124+
const started = yield* awaitEvent(
125+
(event) =>
126+
event.type === "subagent.updated" &&
127+
event.payload.nativeTaskRef?.nativeId === "old-task",
128+
);
129+
yield* Queue.offer(
130+
first,
131+
frame({
132+
type: "system",
133+
subtype: "task_started",
134+
task_id: "old-task",
135+
tool_use_id: "old-tool",
136+
description: "Review recent changes",
137+
subagent_type: "general-purpose",
138+
is_backgrounded: true,
139+
task_type: "local_agent",
140+
prompt: "Review recent changes.",
141+
uuid: "old-task-started",
142+
session_id: sessionId,
143+
}),
144+
);
145+
yield* Fiber.join(started);
146+
const firstTurnDone = yield* awaitEvent(
147+
(event) => event.type === "provider-turn.updated" && event.payload.status === "completed",
148+
);
149+
yield* Queue.offer(
150+
first,
151+
frame({
152+
type: "result",
153+
subtype: "success",
154+
uuid: "first-result",
155+
session_id: sessionId,
156+
is_error: false,
157+
num_turns: 1,
158+
result: "The review continues in the background.",
159+
stop_reason: "end_turn",
160+
permission_denials: [],
161+
duration_ms: 1,
162+
duration_api_ms: 1,
163+
total_cost_usd: 0,
164+
usage: { input_tokens: 1, output_tokens: 1 },
165+
modelUsage: {},
166+
}),
167+
);
168+
yield* Fiber.join(firstTurnDone);
169+
yield* worker.drain();
170+
const before = yield* orchestrator.getThreadProjection(threadId);
171+
const task = before.subagents.find(
172+
(entry) => entry.nativeTaskRef?.nativeId === "old-task",
173+
)!;
174+
assert.equal(task.status, "running");
175+
176+
// The replacement process starts with no record of the old task.
177+
yield* manager.release({
178+
providerSessionId: before.providerSessions.at(-1)!.id,
179+
reason: "runtime_error",
180+
detail: "fixture process exited",
181+
});
182+
yield* sendMessage("second");
183+
yield* worker.drain();
184+
assert.lengthOf(processes, 2);
185+
const second = processes[1]!;
186+
// A later frame's persisted item marks the notification as consumed.
187+
const consumed = yield* awaitEvent(
188+
(event) =>
189+
event.type === "turn-item.updated" &&
190+
event.payload.nativeItemRef?.nativeId === "barrier-tool",
191+
);
192+
yield* Queue.offer(
193+
second,
194+
frame({
195+
type: "system",
196+
subtype: "task_notification",
197+
task_id: "old-task",
198+
status: "stopped",
199+
reason: "worker_restart",
200+
summary: "Background agent didn't finish before the previous session ended",
201+
output_file: "",
202+
uuid: "old-task-stopped",
203+
session_id: sessionId,
204+
}),
205+
);
206+
yield* Queue.offer(
207+
second,
208+
frame({
209+
type: "assistant",
210+
uuid: "barrier",
211+
session_id: sessionId,
212+
parent_tool_use_id: null,
213+
message: {
214+
id: "barrier-message",
215+
role: "assistant",
216+
type: "message",
217+
model: modelSelection.model,
218+
content: [
219+
{ type: "tool_use", id: "barrier-tool", name: "Bash", input: { command: "true" } },
220+
],
221+
stop_reason: "tool_use",
222+
stop_sequence: null,
223+
usage: { input_tokens: 1, output_tokens: 1 },
224+
},
225+
}),
226+
);
227+
yield* Fiber.join(consumed);
228+
229+
const after = yield* orchestrator.getThreadProjection(threadId);
230+
const ended = after.subagents.find((entry) => entry.id === task.id)!;
231+
assert.equal(ended.status, "cancelled");
232+
assert.equal(ended.runId, task.runId);
233+
assert.equal(
234+
ended.result,
235+
"Background agent didn't finish before the previous session ended",
236+
);
237+
assert.equal(after.nodes.find((node) => node.id === task.id)?.status, "cancelled");
238+
const item = after.turnItems.find(
239+
(entry) => entry.type === "subagent" && entry.subagentId === task.id,
240+
);
241+
assert.equal(item?.status, "cancelled");
242+
const child = yield* orchestrator.getThreadProjection(task.childThreadId!);
243+
const childRoots = child.nodes.filter((node) => node.kind === "root_turn");
244+
assert.isAbove(childRoots.length, 0);
245+
assert.isTrue(childRoots.every((node) => node.status === "cancelled"));
246+
}).pipe(
247+
Effect.provide(
248+
ProviderReplayHarness.layerWithRegistry(
249+
{ name: "claude-orphaned-subagent" },
250+
ProviderAdapterRegistry.layerSingle(adapter),
251+
{ runEffectWorker: false },
252+
),
253+
),
254+
);
255+
}).pipe(
256+
Effect.provide(
257+
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
258+
),
259+
),
260+
),
261+
);

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ import * as ProjectionStore from "./ProjectionStore.ts";
3434
import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
3535
import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
3636
import { makeProviderFailureTurnItem } from "@t3tools/provider-core/server/failure";
37+
import {
38+
endOrphanedNativeSubagent,
39+
endRunlessRootTurns,
40+
} from "@t3tools/provider-core/server/subagentProjection";
3741
import * as ThreadCommandExecutor from "./ThreadCommandExecutor.ts";
3842
import { stripUnservedToolOutputImageBytes } from "./toolOutputImageBytes.ts";
3943

@@ -532,6 +536,59 @@ export const layer: Layer.Layer<
532536
return events;
533537
});
534538

539+
/**
540+
* Ends a native subagent that no live provider process tracks: its record,
541+
* node, and parent item, then its own thread's runless root turn. Keeps the
542+
* record's own run and provider ownership. Nothing to end is a no-op.
543+
*/
544+
const endUntrackedSubagent = Effect.fn("ProviderEventIngestor.endUntrackedSubagent")(function* (
545+
input: ProviderEventIngestInput,
546+
ended: Extract<ProviderAdapter.ProviderAdapterV2Event, { readonly type: "subagent.ended" }>,
547+
) {
548+
const projection = yield* projections.getThreadRecords(
549+
ended.threadId,
550+
["nodes", "subagents", "turnItems"],
551+
{ turnItemTypes: ["subagent"], turnItemStatuses: ["pending", "running", "waiting"] },
552+
);
553+
const subagent = projection.subagents.find((task) => task.id === ended.subagentId);
554+
if (subagent === undefined || subagent.driver !== ended.driver) return [];
555+
const now = yield* DateTime.now;
556+
const allocateEventId = () =>
557+
idAllocator.allocate.event({
558+
threadId: ended.threadId,
559+
providerSessionId: input.providerSessionId,
560+
});
561+
const events = yield* endOrphanedNativeSubagent({
562+
projection,
563+
subagentId: subagent.id,
564+
status: ended.status,
565+
result: ended.result,
566+
now,
567+
emitted: [],
568+
allocateEventId,
569+
});
570+
if (events.length === 0 || subagent.childThreadId === null) return events;
571+
const child = yield* projections
572+
.getThreadRecords(subagent.childThreadId, ["nodes", "turnItems"], {
573+
turnItemRunIds: [null],
574+
turnItemStatuses: ["pending", "running", "waiting"],
575+
})
576+
.pipe(Effect.catchTags({ ProjectionStoreThreadNotFoundError: () => Effect.succeed(null) }));
577+
if (child === null) return events;
578+
return [
579+
...events,
580+
...(yield* endRunlessRootTurns({
581+
threadId: subagent.childThreadId,
582+
providerInstanceId: subagent.providerInstanceId,
583+
projection: child,
584+
status: ended.status,
585+
now,
586+
emitted: events,
587+
allocateEventId,
588+
})),
589+
];
590+
});
591+
535592
const normalize: ProviderEventIngestorV2Shape["normalize"] = (input) =>
536593
Effect.gen(function* () {
537594
switch (input.event.type) {
@@ -629,6 +686,8 @@ export const layer: Layer.Layer<
629686
nodeId: input.event.subagent.id,
630687
}),
631688
];
689+
case "subagent.ended":
690+
return yield* endUntrackedSubagent(input, input.event);
632691
case "message.updated":
633692
return [
634693
yield* makeDomainEvent(input, {

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,8 @@ export function routeProviderEvent(
442442
}
443443
return [true, { ...state, workflowMemberParents }];
444444
}
445+
case "subagent.ended":
446+
return [event.threadId === input.threadId, state];
445447
case "message.updated":
446448
return [ownsRun(event.message.runId) || ownsChildThread(event.message.threadId), state];
447449
case "turn_item.updated": {
@@ -1277,6 +1279,19 @@ export const layer: Layer.Layer<
12771279
: {}),
12781280
});
12791281
storedEventCount = storedEvents.length;
1282+
// No adapter item update follows, so an inherited item this
1283+
// settled stops holding the stream here.
1284+
if (event.type === "subagent.ended") {
1285+
const settledItemIds = new Set(
1286+
storedEvents.flatMap((stored) =>
1287+
stored.event.type === "turn-item.updated" ? [stored.event.payload.id] : [],
1288+
),
1289+
);
1290+
yield* Ref.update(
1291+
activeBackgroundTurnItems,
1292+
(current) => new Set([...current].filter((id) => !settledItemIds.has(id))),
1293+
);
1294+
}
12801295
if (
12811296
isRootProviderThreadUpdate &&
12821297
rootTerminalAlreadySeen &&

0 commit comments

Comments
 (0)