Skip to content

Commit 69463d6

Browse files
fix(server): a subagent from a closed Claude process no longer blocks model changes
A subagent registered as running when its CLI process was closed (Stop, rollback, fork, or the replacement this guard allows) never reports its end, so it kept refusing later model changes. The live query now remembers which running subagents predate it and only counts the ones it started. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 0ea993d commit 69463d6

2 files changed

Lines changed: 39 additions & 4 deletions

File tree

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7268,6 +7268,29 @@ describe("ClaudeAdapterV2 background wake turns", () => {
72687268
});
72697269
yield* switchTurn("attempt-claude-model-change-running-subagent-c");
72707270
assert.lengthOf(processQueues, 2);
7271+
7272+
// The stopped subagent never reports its end, so it must not block
7273+
// later changes on the replacement process either.
7274+
yield* Queue.offer(
7275+
processQueues[1]!,
7276+
makeResultFrame({
7277+
uuid: "00000000-0000-4000-8000-000000000903",
7278+
result: "Switched model.",
7279+
}),
7280+
);
7281+
yield* awaitUntil(() => terminals().length === 2, "switched turn terminal");
7282+
yield* runtime.startTurn(
7283+
makeClaudeTestTurnInput({
7284+
threadId,
7285+
providerThread: { ...providerThread, status: "active" },
7286+
now,
7287+
attemptId: RunAttemptId.make("attempt-claude-model-change-running-subagent-d"),
7288+
text: "Switch back.",
7289+
attachments: [],
7290+
providerTurnOrdinal: 3,
7291+
}),
7292+
);
7293+
assert.lengthOf(processQueues, 3);
72717294
}).pipe(Effect.provide(Layer.merge(IdAllocator.layer, NodeServices.layer))),
72727295
),
72737296
);

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2617,6 +2617,10 @@ interface ClaudeLiveQueryContext {
26172617
promptEchoMode: "unknown" | "acknowledged" | "early" | "result_only";
26182618
// Stop, rollback or fork is closing this process; its work is ending.
26192619
stopping: boolean;
2620+
// Registry entries still running when this process opened. Their process
2621+
// is gone and never reports their end; any later task_started replaces the
2622+
// entry, so an entry still in this set runs nowhere.
2623+
readonly subagentsFromEarlierProcesses: ReadonlySet<ActiveClaudeSubagent>;
26202624
}
26212625

26222626
interface ActiveClaudeToolCall {
@@ -6663,19 +6667,22 @@ export function makeClaudeAdapterV2(
66636667

66646668
// Work the live process still runs. A subagent whose completion is
66656669
// already buffered is done: the buffer outlives the process.
6666-
const liveProcessRunsBackgroundWork = Effect.fnUntraced(function* (nativeThreadId: string) {
6670+
const liveProcessRunsBackgroundWork = Effect.fnUntraced(function* (
6671+
live: ClaudeLiveQueryContext,
6672+
) {
66676673
if (
66686674
rosterForNativeThread(
66696675
yield* Ref.get(pendingBackgroundTasksByNativeThread),
6670-
nativeThreadId,
6676+
live.nativeThreadId,
66716677
).size > 0
66726678
) {
66736679
return true;
66746680
}
6675-
const buffered = (yield* Ref.get(wakeBuffers)).get(nativeThreadId)?.messages ?? [];
6681+
const buffered = (yield* Ref.get(wakeBuffers)).get(live.nativeThreadId)?.messages ?? [];
66766682
for (const [taskId, subagent] of yield* Ref.get(sessionSubagentsByTaskId)) {
66776683
if (
66786684
subagent.task.status === "running" &&
6685+
!live.subagentsFromEarlierProcesses.has(subagent) &&
66796686
!buffered.some(
66806687
(message) =>
66816688
message.type === "system" &&
@@ -6727,7 +6734,7 @@ export function makeClaudeAdapterV2(
67276734
existing !== null &&
67286735
existing.nativeThreadId === nativeThreadId &&
67296736
!existing.stopping &&
6730-
(yield* liveProcessRunsBackgroundWork(nativeThreadId))
6737+
(yield* liveProcessRunsBackgroundWork(existing))
67316738
) {
67326739
return yield* new ClaudeBackgroundWorkBlocksQueryReplacementError();
67336740
}
@@ -6829,6 +6836,11 @@ export function makeClaudeAdapterV2(
68296836
closed,
68306837
promptEchoMode: "unknown",
68316838
stopping: false,
6839+
subagentsFromEarlierProcesses: new Set(
6840+
[...(yield* Ref.get(sessionSubagentsByTaskId)).values()].filter(
6841+
(subagent) => subagent.task.status === "running",
6842+
),
6843+
),
68326844
};
68336845
yield* Ref.set(queryContext, context);
68346846
yield* querySession.messages.pipe(

0 commit comments

Comments
 (0)