Skip to content

Commit 6529453

Browse files
committed
fix(server): settle Claude background agents when their process exits
1 parent 7ac952c commit 6529453

2 files changed

Lines changed: 122 additions & 15 deletions

File tree

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

Lines changed: 117 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8073,30 +8073,39 @@ describe("ClaudeAdapterV2 background wake turns", () => {
80738073
attachments: [],
80748074
}),
80758075
);
8076-
for (const frame of [
8076+
yield* harness.offerAndWait(
80778077
makeSubagentTaskStartedFrame({
80788078
taskId: "task-idle-exit",
80798079
toolUseId: TOOL_USE_ID,
80808080
uuid: "00000000-0000-4000-8000-000000000981",
80818081
}),
8082-
...makeSubagentAssistantFrames({
8083-
parentToolUseId: TOOL_USE_ID,
8084-
uuid: "00000000-0000-4000-8000-000000000982",
8085-
bashToolUseId: BASH,
8086-
}),
8082+
);
8083+
const started = yield* Queue.take(harness.subagentReceipts);
8084+
assert.equal(started.subagent.status, "running");
8085+
for (const frame of makeSubagentAssistantFrames({
8086+
parentToolUseId: TOOL_USE_ID,
8087+
uuid: "00000000-0000-4000-8000-000000000982",
8088+
bashToolUseId: BASH,
8089+
})) {
8090+
yield* harness.offerAndWait(frame);
8091+
}
8092+
yield* harness.offerAndWait(
80878093
makeResultFrame({
80888094
uuid: "00000000-0000-4000-8000-000000000983",
80898095
result: "Auditing in the background.",
80908096
}),
8091-
]) {
8092-
yield* Queue.offer(harness.sdkMessages, frame);
8093-
}
8094-
yield* awaitUntil(() => harness.terminalEvents().length === 1, "launch turn terminal");
8097+
);
8098+
yield* Queue.take(harness.terminalReceipts);
80958099
assert.deepEqual(bashStatuses(), ["command_execution:running"]);
80968100

80978101
// The CLI exits before the call returns; its result can never arrive.
80988102
yield* Queue.shutdown(harness.sdkMessages);
8099-
yield* awaitUntil(() => bashStatuses().length === 2, "call ended");
8103+
let ended = yield* Queue.take(harness.subagentReceipts);
8104+
while (ended.subagent.status === "running") {
8105+
ended = yield* Queue.take(harness.subagentReceipts);
8106+
}
8107+
assert.equal(ended.subagent.status, "failed");
8108+
assert.isFalse(yield* harness.hasPendingBackgroundWork);
81008109
assert.deepEqual(bashStatuses(), ["command_execution:running", "command_execution:failed"]);
81018110
}).pipe(
81028111
Effect.provide(
@@ -8172,6 +8181,62 @@ describe("ClaudeAdapterV2 background wake turns", () => {
81728181
),
81738182
);
81748183

8184+
it.effect("cancels a plain background subagent when Stop closes its CLI process", () =>
8185+
Effect.scoped(
8186+
Effect.gen(function* () {
8187+
const TASK_ID = "task-idle-stop-exit";
8188+
const TOOL_USE_ID = "toolu-idle-stop-exit";
8189+
const harness = yield* makeWakeHarnessWithOptions({
8190+
close: (sdkMessages) => Queue.shutdown(sdkMessages),
8191+
});
8192+
const now = yield* DateTime.now;
8193+
yield* harness.runtime.startTurn(
8194+
makeClaudeTestTurnInput({
8195+
threadId: harness.threadId,
8196+
providerThread: harness.providerThread,
8197+
now,
8198+
attemptId: RunAttemptId.make("attempt-claude-idle-stop-exit"),
8199+
text: "Audit in the background.",
8200+
attachments: [],
8201+
}),
8202+
);
8203+
yield* harness.offerAndWait(
8204+
makeSubagentTaskStartedFrame({
8205+
taskId: TASK_ID,
8206+
toolUseId: TOOL_USE_ID,
8207+
uuid: "00000000-0000-4000-8000-000000000989",
8208+
}),
8209+
);
8210+
const started = yield* Queue.take(harness.subagentReceipts);
8211+
assert.equal(started.subagent.status, "running");
8212+
yield* harness.offerAndWait(
8213+
makeResultFrame({
8214+
uuid: "00000000-0000-4000-8000-000000000990",
8215+
result: "Auditing in the background.",
8216+
}),
8217+
);
8218+
const terminal = yield* Queue.take(harness.terminalReceipts);
8219+
8220+
yield* harness.runtime.interruptTurn({
8221+
providerThread: harness.providerThread,
8222+
providerTurnId: terminal.providerTurnId,
8223+
requestRuntimeRestart: true,
8224+
});
8225+
8226+
let ended = yield* Queue.take(harness.subagentReceipts);
8227+
while (ended.subagent.status === "running") {
8228+
ended = yield* Queue.take(harness.subagentReceipts);
8229+
}
8230+
assert.equal(ended.subagent.status, "cancelled");
8231+
assert.isFalse(yield* harness.hasPendingBackgroundWork);
8232+
}).pipe(
8233+
Effect.provide(
8234+
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
8235+
),
8236+
),
8237+
),
8238+
);
8239+
81758240
it.effect.each(["before", "after"] as const)(
81768241
"keeps a subagent call's result when the CLI exits before the drain (result %s the nested launch's)",
81778242
(bashResultOrder) =>
@@ -11714,6 +11779,47 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1171411779
);
1171511780
});
1171611781

11782+
it.effect("fails a workflow when its CLI process exits while the root turn is idle", () =>
11783+
Effect.scoped(
11784+
Effect.gen(function* () {
11785+
const harness = yield* makeWorkflowHarnessWithOptions({ close: Queue.shutdown });
11786+
yield* harness.runtime.startTurn(
11787+
makeClaudeTestTurnInput({
11788+
threadId: harness.threadId,
11789+
providerThread: harness.providerThread,
11790+
now: yield* DateTime.now,
11791+
attemptId: RunAttemptId.make("attempt-workflow-idle-exit"),
11792+
text: "Run the workflow.",
11793+
attachments: [],
11794+
}),
11795+
);
11796+
yield* launchWorkflow({
11797+
harness,
11798+
snapshotUuid: "00000000-0000-4000-8000-000000001210",
11799+
});
11800+
yield* harness.offerAndWait(
11801+
makeResultFrame({
11802+
uuid: "00000000-0000-4000-8000-000000001211",
11803+
result: "Workflow launched.",
11804+
}),
11805+
);
11806+
yield* Queue.take(harness.terminalReceipts);
11807+
11808+
yield* Queue.shutdown(harness.sdkMessages);
11809+
let ended = yield* Queue.take(harness.subagentReceipts);
11810+
while (ended.subagent.status === "running") {
11811+
ended = yield* Queue.take(harness.subagentReceipts);
11812+
}
11813+
assert.equal(ended.subagent.status, "failed");
11814+
assert.isFalse(yield* harness.hasPendingBackgroundWork);
11815+
}).pipe(
11816+
Effect.provide(
11817+
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
11818+
),
11819+
),
11820+
),
11821+
);
11822+
1171711823
it.effect.each(["Agent", "Workflow"] as const)(
1171811824
"keeps a workflow running after its %s launch reply without an async flag",
1171911825
(toolName) =>

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6182,8 +6182,10 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
61826182
cause?: Cause.Cause<ClaudeAgentSdkQueryRunnerError>,
61836183
) {
61846184
const current = yield* Ref.get(activeTurn);
6185-
const context =
6186-
current?.nativeThreadId === closedQuery.nativeThreadId ? current : closedQuery.lastTurn;
6185+
// The query has already retired its ownership before this cleanup
6186+
// runs. Use its own last turn so a replacement query that starts
6187+
// during the cleanup cannot be finalized by the old process.
6188+
const context = closedQuery.lastTurn;
61876189
if (context === null) return;
61886190
const interrupted =
61896191
closedQuery.stopping || (yield* Ref.get(interruptedTurns)).has(context.providerTurnId);
@@ -6206,12 +6208,11 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
62066208
} else {
62076209
yield* resetBackgroundTaskStateForNativeThreadProcess(closedQuery.nativeThreadId);
62086210
}
6209-
// A workflow can outlive its launching turn, but not its CLI process.
6211+
// A background subagent can outlive its launching turn, but not its CLI process.
62106212
// Apply buffered outcomes before Stop can discard their wake buffer.
62116213
// Capture ownership before finalization, which may yield to a new process.
62126214
for (const [taskId, subagent] of subagents) {
62136215
if (
6214-
subagent.task.workflow === undefined ||
62156216
subagent.task.status !== "running" ||
62166217
closedQuery.subagentsFromEarlierProcesses.has(subagent)
62176218
)

0 commit comments

Comments
 (0)