Skip to content

Commit f94f179

Browse files
Vantrongsclaude
andcommitted
fix(server): end a subagent's open calls when Stop's close of the CLI times out
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 80f3dce commit f94f179

2 files changed

Lines changed: 92 additions & 14 deletions

File tree

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

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import * as Queue from "effect/Queue";
4343
import * as Schema from "effect/Schema";
4444
import * as Scope from "effect/Scope";
4545
import * as Stream from "effect/Stream";
46+
import * as TestClock from "effect/testing/TestClock";
4647
import { Tool } from "effect/ai";
4748
import { formatClaudeResumeCompactionQuestion } from "@t3tools/shared/claudeCompaction";
4849
import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
@@ -6627,6 +6628,68 @@ describe("ClaudeAdapterV2 background wake turns", () => {
66276628
),
66286629
);
66296630

6631+
it.effect("ends a subagent's open call when Stop's close of the CLI times out", () =>
6632+
Effect.scoped(
6633+
Effect.gen(function* () {
6634+
const TOOL_USE_ID = "toolu-idle-close-timeout";
6635+
const BASH = "toolu-idle-close-timeout-bash";
6636+
// close() leaves the stream open, so Stop gives up waiting for it.
6637+
const harness = yield* makeWakeHarnessWithOptions();
6638+
const now = yield* DateTime.now;
6639+
const bashStatuses = () =>
6640+
harness.events.flatMap((event) =>
6641+
event.type === "turn_item.updated" && event.turnItem.nativeItemRef?.nativeId === BASH
6642+
? [`${event.turnItem.type}:${event.turnItem.status}`]
6643+
: [],
6644+
);
6645+
yield* harness.runtime.startTurn(
6646+
makeClaudeTestTurnInput({
6647+
threadId: harness.threadId,
6648+
providerThread: harness.providerThread,
6649+
now,
6650+
attemptId: RunAttemptId.make("attempt-claude-idle-close-timeout"),
6651+
text: "Audit in the background.",
6652+
attachments: [],
6653+
}),
6654+
);
6655+
for (const frame of [
6656+
makeSubagentTaskStartedFrame({
6657+
taskId: "task-idle-close-timeout",
6658+
toolUseId: TOOL_USE_ID,
6659+
uuid: "00000000-0000-4000-8000-000000000985",
6660+
}),
6661+
...makeSubagentAssistantFrames({
6662+
parentToolUseId: TOOL_USE_ID,
6663+
uuid: "00000000-0000-4000-8000-000000000986",
6664+
bashToolUseId: BASH,
6665+
}),
6666+
makeResultFrame({
6667+
uuid: "00000000-0000-4000-8000-000000000987",
6668+
result: "Auditing in the background.",
6669+
}),
6670+
]) {
6671+
yield* Queue.offer(harness.sdkMessages, frame);
6672+
}
6673+
yield* awaitUntil(() => harness.terminalEvents().length === 1, "launch turn terminal");
6674+
assert.deepEqual(bashStatuses(), ["command_execution:running"]);
6675+
6676+
const stop = yield* harness.runtime
6677+
.interruptTurn({
6678+
providerThread: harness.providerThread,
6679+
providerTurnId: harness.terminalEvents()[0]!.providerTurnId,
6680+
requestRuntimeRestart: true,
6681+
})
6682+
.pipe(Effect.forkScoped);
6683+
yield* TestClock.adjust("10 seconds");
6684+
yield* Fiber.join(stop);
6685+
assert.deepEqual(bashStatuses(), [
6686+
"command_execution:running",
6687+
"command_execution:interrupted",
6688+
]);
6689+
}).pipe(Effect.provide(Layer.merge(IdAllocator.layer, NodeServices.layer))),
6690+
),
6691+
);
6692+
66306693
it.effect("releases held frames before the notification that first names the tool use", () =>
66316694
Effect.scoped(
66326695
Effect.gen(function* () {

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

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4935,6 +4935,24 @@ export function makeClaudeAdapterV2(
49354935
);
49364936
}
49374937
});
4938+
// Ends the calls a subagent kept open past the root's last turn, once
4939+
// the CLI process that ran them is gone.
4940+
const endSubagentCallsOfClosedQuery = Effect.fnUntraced(function* (
4941+
nativeThreadId: string,
4942+
status: "interrupted" | "failed",
4943+
) {
4944+
const settled = settledTurnByNativeThread.get(nativeThreadId);
4945+
if (settled === undefined || subagentToolCalls.size === 0) {
4946+
return;
4947+
}
4948+
yield* endToolCalls({
4949+
context: settled,
4950+
toolCalls: [...subagentToolCalls.values()],
4951+
status,
4952+
completedAt: yield* DateTime.now,
4953+
});
4954+
subagentToolCalls.clear();
4955+
});
49384956

49394957
const finalizeActiveTurn = Effect.fnUntraced(function* (input: {
49404958
readonly context: ActiveClaudeTurnContext;
@@ -7344,18 +7362,10 @@ export function makeClaudeAdapterV2(
73447362
yield* finalizeActiveTurnAfterQueryExit(
73457363
exit._tag === "Failure" ? exit.cause : undefined,
73467364
);
7347-
// Calls a subagent kept open past the root's last turn end
7348-
// with the process that ran them.
7349-
const settled = settledTurnByNativeThread.get(context.nativeThreadId);
7350-
if (settled !== undefined && subagentToolCalls.size > 0) {
7351-
yield* endToolCalls({
7352-
context: settled,
7353-
toolCalls: [...subagentToolCalls.values()],
7354-
status: context.stopping ? "interrupted" : "failed",
7355-
completedAt: yield* DateTime.now,
7356-
});
7357-
subagentToolCalls.clear();
7358-
}
7365+
yield* endSubagentCallsOfClosedQuery(
7366+
context.nativeThreadId,
7367+
context.stopping ? "interrupted" : "failed",
7368+
);
73597369
}
73607370
}),
73617371
),
@@ -7716,9 +7726,14 @@ export function makeClaudeAdapterV2(
77167726
providerSessionId: input.providerSessionId,
77177727
nativeThreadId,
77187728
});
7719-
yield* Ref.update(queryContext, (current) =>
7720-
current?.query === existing.query ? null : current,
7729+
// Once ownership is dropped here, the old stream's exit skips the
7730+
// subagent's open calls, so they end now.
7731+
const ownedLiveQuery = yield* Ref.modify(queryContext, (current) =>
7732+
current?.query === existing.query ? [true, null] : [false, current],
77217733
);
7734+
if (ownedLiveQuery) {
7735+
yield* endSubagentCallsOfClosedQuery(nativeThreadId, "interrupted");
7736+
}
77227737
yield* Deferred.succeed(existing.closed, undefined);
77237738
});
77247739
yield* Effect.addFinalizer(() => closeSession());

0 commit comments

Comments
 (0)