Skip to content

Commit a32ef0d

Browse files
id0Schclaude
andcommitted
fix(server): a failed Claude reopen interrupts subagents nothing will end
When the replacement process fails to open, no CLI process of the session is left and every earlier wake buffer is gone, so interrupt the subagents still marked running instead of waiting for the next successful turn. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent fb59d45 commit a32ef0d

2 files changed

Lines changed: 182 additions & 5 deletions

File tree

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

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10502,6 +10502,165 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1050210502
),
1050310503
);
1050410504

10505+
it.effect("interrupts a stopped subagent when the replacement process fails to open", () =>
10506+
Effect.scoped(
10507+
Effect.gen(function* () {
10508+
const SUBAGENT_TASK_ID = "task-replace-open-fail-subagent";
10509+
const fileSystem = yield* FileSystem.FileSystem;
10510+
const idAllocator = yield* IdAllocator.IdAllocatorV2;
10511+
const attachmentsDir = yield* fileSystem.makeTempDirectoryScoped({
10512+
prefix: "t3-claude-v2-replace-open-fail-subagent-",
10513+
});
10514+
let openCount = 0;
10515+
const processQueues: Array<Queue.Queue<SDKMessage>> = [];
10516+
const events: Array<ProviderAdapter.ProviderAdapterV2Event> = [];
10517+
const adapter = yield* ClaudeAdapterV2.makeClaudeAdapterV2({
10518+
instanceId: ClaudeAdapterV2.CLAUDE_DEFAULT_INSTANCE_ID,
10519+
settings: DEFAULT_CLAUDE_SETTINGS,
10520+
environment: {},
10521+
attachmentsDir,
10522+
fileSystem,
10523+
path: yield* Path.Path,
10524+
crypto: yield* Crypto.Crypto,
10525+
idAllocator,
10526+
continuationRequests: { offer: () => Effect.void },
10527+
queryRunner: {
10528+
allocateSessionId: Effect.succeed(WAKE_NATIVE_SESSION),
10529+
open: () => {
10530+
openCount += 1;
10531+
if (openCount === 2) {
10532+
return Effect.fail(
10533+
new ClaudeAdapterV2.ClaudeAgentSdkQueryRunnerError({
10534+
method: "open",
10535+
cause: "forced replacement open failure",
10536+
}),
10537+
);
10538+
}
10539+
return Effect.gen(function* () {
10540+
const sdkMessages = yield* Queue.unbounded<SDKMessage>();
10541+
processQueues.push(sdkMessages);
10542+
return {
10543+
messages: Stream.fromQueue(sdkMessages),
10544+
offer: () => Effect.void,
10545+
setModel: () => Effect.void,
10546+
setPermissionMode: () => Effect.void,
10547+
stopTask: () => Effect.void,
10548+
interrupt: Effect.void,
10549+
close: Queue.shutdown(sdkMessages),
10550+
};
10551+
});
10552+
},
10553+
forkSession: () => Effect.die("unused forkSession"),
10554+
subagentLaunchToolUseId: () => Effect.succeed(null),
10555+
assertComplete: Effect.void,
10556+
},
10557+
});
10558+
const threadId = ThreadId.make("thread-claude-replace-open-fail-subagent");
10559+
const runtime = yield* adapter.openSession({
10560+
threadId,
10561+
providerSessionId: ProviderSessionId.make(
10562+
"provider-session-claude-replace-open-fail-subagent",
10563+
),
10564+
modelSelection: CLAUDE_TEST_MODEL_SELECTION,
10565+
runtimePolicy: CLAUDE_TEST_RUNTIME_POLICY,
10566+
});
10567+
const providerThread = yield* runtime.ensureThread({
10568+
threadId,
10569+
modelSelection: CLAUDE_TEST_MODEL_SELECTION,
10570+
runtimePolicy: CLAUDE_TEST_RUNTIME_POLICY,
10571+
});
10572+
yield* runtime.events.pipe(
10573+
Stream.runForEach((event) =>
10574+
Effect.sync(() => {
10575+
events.push(event);
10576+
}),
10577+
),
10578+
Effect.forkScoped,
10579+
);
10580+
const terminals = () => events.filter((event) => event.type === "turn.terminal");
10581+
const now = yield* DateTime.now;
10582+
10583+
yield* runtime.startTurn(
10584+
makeClaudeTestTurnInput({
10585+
threadId,
10586+
providerThread,
10587+
now,
10588+
attemptId: RunAttemptId.make("attempt-claude-replace-open-fail-subagent-a"),
10589+
text: "Spawn a background subagent and stop.",
10590+
attachments: [],
10591+
}),
10592+
);
10593+
yield* Queue.offer(
10594+
processQueues[0]!,
10595+
claudeSdkFrame({
10596+
type: "system",
10597+
subtype: "task_started",
10598+
task_id: SUBAGENT_TASK_ID,
10599+
tool_use_id: "toolu-replace-open-fail-subagent",
10600+
description: "Background research",
10601+
subagent_type: "general-purpose",
10602+
task_type: "local_agent",
10603+
prompt: "Research, then report.",
10604+
uuid: "00000000-0000-4000-8000-000000000911",
10605+
session_id: WAKE_NATIVE_SESSION,
10606+
}),
10607+
);
10608+
yield* Queue.offer(
10609+
processQueues[0]!,
10610+
makeResultFrame({
10611+
uuid: "00000000-0000-4000-8000-000000000912",
10612+
result: "Spawned the subagent in the background.",
10613+
}),
10614+
);
10615+
yield* awaitUntil(() => terminals().length === 1, "first turn terminal");
10616+
10617+
yield* runtime.interruptTurn({
10618+
providerThread,
10619+
providerTurnId: terminals()[0]!.providerTurnId,
10620+
requestRuntimeRestart: true,
10621+
});
10622+
const failedStart = yield* runtime
10623+
.startTurn(
10624+
makeClaudeTestTurnInput({
10625+
threadId,
10626+
providerThread: { ...providerThread, status: "active" },
10627+
now,
10628+
attemptId: RunAttemptId.make("attempt-claude-replace-open-fail-subagent-b"),
10629+
text: "Switch model after Stop.",
10630+
attachments: [],
10631+
providerTurnOrdinal: 2,
10632+
modelSelection: {
10633+
...CLAUDE_TEST_MODEL_SELECTION,
10634+
model: "claude-haiku-4-5-20251001",
10635+
},
10636+
}),
10637+
)
10638+
.pipe(Effect.exit);
10639+
assert.isTrue(Exit.isFailure(failedStart));
10640+
assert.equal(openCount, 2);
10641+
10642+
// The first process is closed and no replacement started, so nothing
10643+
// will ever report the subagent's end.
10644+
const subagentUpdates = () =>
10645+
events.flatMap((event) =>
10646+
event.type === "subagent.updated" &&
10647+
event.subagent.nativeTaskRef?.nativeId === SUBAGENT_TASK_ID
10648+
? [event.subagent]
10649+
: [],
10650+
);
10651+
yield* awaitUntil(
10652+
() => subagentUpdates().at(-1)?.status === "interrupted",
10653+
"orphaned subagent interrupted",
10654+
);
10655+
assert.equal(subagentUpdates().at(-1)?.runId, subagentUpdates()[0]?.runId);
10656+
}).pipe(
10657+
Effect.provide(
10658+
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
10659+
),
10660+
),
10661+
),
10662+
);
10663+
1050510664
it.effect(
1050610665
"keeps the process and its roster when a model change meets a running background shell",
1050710666
() =>

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

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2437,10 +2437,12 @@ function toSessionPermissionUpdates(
24372437
toolName: string,
24382438
suggestions: ReadonlyArray<PermissionUpdate> | undefined,
24392439
): Array<PermissionUpdate> {
2440-
const updates = (suggestions ?? []).map((suggestion): PermissionUpdate => ({
2441-
...suggestion,
2442-
destination: "session",
2443-
}));
2440+
const updates = (suggestions ?? []).map(
2441+
(suggestion): PermissionUpdate => ({
2442+
...suggestion,
2443+
destination: "session",
2444+
}),
2445+
);
24442446
if (updates.length > 0) {
24452447
return updates;
24462448
}
@@ -8176,6 +8178,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
81768178
const openQuery = Effect.fnUntraced(function* (
81778179
turnInput: ProviderAdapter.ProviderAdapterV2TurnInput,
81788180
nativeThreadId: string,
8181+
turnContext: ActiveClaudeTurnContext,
81798182
) {
81808183
const queryPolicy = claudeRuntimeQueryPolicyForRuntimePolicy(turnInput.runtimePolicy);
81818184
const mcpOverrides = claudeMcpQueryOverrides({
@@ -8303,6 +8306,21 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
83038306
})
83048307
: Effect.void,
83058308
),
8309+
// No process of this session is left to report a subagent's
8310+
// end, and every earlier process's wake buffer is already gone.
8311+
Effect.tapError(() =>
8312+
Effect.gen(function* () {
8313+
for (const [taskId, subagent] of yield* Ref.get(sessionSubagentsByTaskId)) {
8314+
if (subagent.task.status === "running") {
8315+
yield* updateClaudeSubagentNode({
8316+
context: turnContext,
8317+
taskId,
8318+
status: "interrupted",
8319+
});
8320+
}
8321+
}
8322+
}),
8323+
),
83068324
);
83078325
// Marked only after a successful open: a failed create must not
83088326
// leave the runtime believing the native session exists, or the
@@ -8496,7 +8514,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
84968514
skillNames: yield* userInvocableSkillNames(turnInput.runtimePolicy.cwd),
84978515
uuid: promptUuid,
84988516
});
8499-
const querySession = yield* openQuery(turnInput, nativeThreadId);
8517+
const querySession = yield* openQuery(turnInput, nativeThreadId, context);
85008518
const attachTurn = Effect.gen(function* () {
85018519
querySession.lastTurn = context;
85028520
yield* Ref.set(activeTurn, context);

0 commit comments

Comments
 (0)