Skip to content

Commit 440e2e8

Browse files
PR Batch Testerclaude
andcommitted
fix(server): steer backgrounding skips nested subagents and turns that ended
- A nested subagent whose launch the adapter never saw falls back to the root as its parent node; its spawn depth now keeps it out of the move. - The turn can settle while the moves are answered. The steer then fails instead of opening a turn nobody asked for, so the orchestrator starts the follow-up turn as it does for any steer that misses its turn. - A subagent that ended before its move was answered is not marked as backgrounded, so a later foreground run of it cannot report a wake. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 51ba271 commit 440e2e8

2 files changed

Lines changed: 90 additions & 35 deletions

File tree

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

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13800,6 +13800,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1380013800
{ backgrounding: "succeeds" },
1380113801
{ backgrounding: "fails" },
1380213802
{ backgrounding: "never answers" },
13803+
{ backgrounding: "answers after the turn ended" },
1380313804
] as const)(
1380413805
"moves the root's foreground subagents to the background before a steer ($backgrounding)",
1380513806
({ backgrounding }) =>
@@ -13809,20 +13810,24 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1380913810
const NESTED_TOOL_USE_ID = "toolu-steer-nested";
1381013811
const FINISHED_TOOL_USE_ID = "toolu-steer-finished";
1381113812
const calls: Array<string> = [];
13813+
// The root turn settles while the move is still unanswered.
13814+
let endTurnFirst: Effect.Effect<void> = Effect.void;
1381213815
const harness = yield* makeWakeHarnessWithOptions({
1381313816
backgroundTasks: (toolUseId) =>
1381413817
Effect.sync(() => calls.push(toolUseId)).pipe(
1381513818
Effect.andThen(
13816-
backgrounding === "fails"
13817-
? Effect.fail(
13818-
new ClaudeAdapterV2.ClaudeAgentSdkQueryRunnerError({
13819-
method: "backgroundTasks",
13820-
cause: new Error("Background tasks are disabled in this session."),
13821-
}),
13822-
)
13823-
: backgrounding === "never answers"
13824-
? Effect.never
13825-
: Effect.succeed(true),
13819+
backgrounding === "answers after the turn ended"
13820+
? Effect.suspend(() => endTurnFirst).pipe(Effect.as(true))
13821+
: backgrounding === "fails"
13822+
? Effect.fail(
13823+
new ClaudeAdapterV2.ClaudeAgentSdkQueryRunnerError({
13824+
method: "backgroundTasks",
13825+
cause: new Error("Background tasks are disabled in this session."),
13826+
}),
13827+
)
13828+
: backgrounding === "never answers"
13829+
? Effect.never
13830+
: Effect.succeed(true),
1382613831
),
1382713832
),
1382813833
});
@@ -13837,7 +13842,14 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1383713842
attachments: [],
1383813843
});
1383913844
yield* harness.runtime.startTurn(input);
13845+
endTurnFirst = Queue.offer(
13846+
harness.sdkMessages,
13847+
makeResultFrame({ uuid: "00000000-0000-4000-8000-000000000a10", result: "Done." }),
13848+
).pipe(
13849+
Effect.andThen(awaitUntil(() => harness.terminalEvents().length === 1, "turn end")),
13850+
);
1384013851
const foregroundTaskStarted = (task: {
13852+
readonly spawnDepth?: number;
1384113853
readonly taskId: string;
1384213854
readonly toolUseId: string;
1384313855
readonly uuid: string;
@@ -13852,6 +13864,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1385213864
is_backgrounded: false,
1385313865
task_type: "local_agent",
1385413866
prompt: "Audit.",
13867+
...(task.spawnDepth === undefined ? {} : { spawn_depth: task.spawnDepth }),
1385513868
uuid: task.uuid,
1385613869
session_id: WAKE_NATIVE_SESSION,
1385713870
});
@@ -13897,6 +13910,13 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1389713910
toolUseId: NESTED_TOOL_USE_ID,
1389813911
uuid: "00000000-0000-4000-8000-000000000a05",
1389913912
}),
13913+
// Nested too, though its launch never reached the adapter.
13914+
foregroundTaskStarted({
13915+
taskId: "task-steer-deep",
13916+
toolUseId: "toolu-steer-deep",
13917+
spawnDepth: 2,
13918+
uuid: "00000000-0000-4000-8000-000000000a11",
13919+
}),
1390013920
]) {
1390113921
yield* Queue.offer(harness.sdkMessages, frame);
1390213922
}
@@ -13912,6 +13932,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1391213932
yield* awaitUntil(
1391313933
() =>
1391413934
latestSubagent("task-steer-nested")?.status === "running" &&
13935+
latestSubagent("task-steer-deep")?.status === "running" &&
1391513936
latestSubagent("task-steer-foreground")?.status === "running" &&
1391613937
latestSubagent("task-steer-finished")?.status === "completed",
1391713938
"subagents registered",
@@ -13940,12 +13961,19 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1394013961
assert.equal(harness.offeredMessages.length, offered);
1394113962
yield* TestClock.adjust("5 seconds");
1394213963
}
13943-
yield* Fiber.join(steering);
13964+
const steered = yield* Fiber.await(steering);
1394413965

13945-
// Only the root's running subagent moves: the nested one follows its
13946-
// owner and the finished one has nothing left to move. A refused or
13947-
// unanswered move still lets the steer through.
13966+
// Only the root's running subagent moves: the nested ones follow
13967+
// their owner and the finished one has nothing left to move.
1394813968
assert.deepEqual(calls, [FOREGROUND_TOOL_USE_ID]);
13969+
if (backgrounding === "answers after the turn ended") {
13970+
// The steer is refused, so the orchestrator starts its follow-up turn.
13971+
assert.isTrue(Exit.isFailure(steered));
13972+
assert.equal(harness.offeredMessages.length, offered);
13973+
return;
13974+
}
13975+
// A refused or unanswered move still lets the steer through.
13976+
assert.isTrue(Exit.isSuccess(steered));
1394913977
assert.equal(harness.offeredMessages.length, offered + 1);
1395013978
assert.equal(harness.offeredMessages.at(-1)?.priority, "now");
1395113979
if (backgrounding !== "succeeds") return;

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

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8847,37 +8847,52 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
88478847
const backgroundForegroundSubagents = Effect.fnUntraced(function* (
88488848
live: ClaudeLiveQueryContext,
88498849
) {
8850-
const foreground = Array.from(
8851-
(yield* Ref.get(sessionSubagentsByTaskId)).values(),
8852-
).flatMap((subagent) =>
8853-
subagent.runToolUseId !== null &&
8854-
subagent.task.status === "running" &&
8855-
subagent.task.parentNodeId === subagent.rootNodeId &&
8856-
!live.subagentsFromEarlierProcesses.has(subagent)
8857-
? [{ subagent, toolUseId: subagent.runToolUseId }]
8858-
: [],
8859-
);
8850+
const foreground: Array<{
8851+
readonly taskId: string;
8852+
readonly toolUseId: string;
8853+
}> = [];
8854+
for (const [taskId, subagent] of yield* Ref.get(sessionSubagentsByTaskId)) {
8855+
const toolUseId = subagent.runToolUseId;
8856+
if (
8857+
toolUseId === null ||
8858+
subagent.task.status !== "running" ||
8859+
subagent.task.parentNodeId !== subagent.rootNodeId ||
8860+
live.subagentsFromEarlierProcesses.has(subagent) ||
8861+
// A nested launch whose owner was not known yet falls back to
8862+
// the root as its parent node; its spawn depth still marks it.
8863+
(yield* isNestedSubagentTask(taskId))
8864+
) {
8865+
continue;
8866+
}
8867+
foreground.push({ taskId, toolUseId });
8868+
}
88608869
// Backgrounding is refused when the session disables background
88618870
// tasks, and the SDK puts no deadline on the request. Either way the
88628871
// steer still goes through, as it did before.
88638872
yield* Effect.forEach(
88648873
foreground,
8865-
({ subagent, toolUseId }) =>
8874+
({ taskId, toolUseId }) =>
88668875
live.query.backgroundTasks(toolUseId).pipe(
88678876
Effect.timeout("5 seconds"),
88688877
// Now a background subagent: its end wakes the root like one
8869-
// Claude started in the background.
8870-
Effect.flatMap((moved) => {
8871-
const taskId = subagent.task.nativeTaskRef?.nativeId;
8872-
return moved && typeof taskId === "string"
8873-
? Ref.update(backgroundedSubagentTaskIds, (current) =>
8874-
new Set(current).add(taskId),
8878+
// Claude started in the background. One that already ended
8879+
// while the move was answered has nothing left to report.
8880+
Effect.flatMap((moved) =>
8881+
moved
8882+
? Ref.get(sessionSubagentsByTaskId).pipe(
8883+
Effect.flatMap((registry) =>
8884+
registry.get(taskId)?.task.status === "running"
8885+
? Ref.update(backgroundedSubagentTaskIds, (current) =>
8886+
new Set(current).add(taskId),
8887+
)
8888+
: Effect.void,
8889+
),
88758890
)
8876-
: Effect.void;
8877-
}),
8891+
: Effect.void,
8892+
),
88788893
Effect.catch((cause) =>
88798894
Effect.logWarning("orchestration-v2.claude-steer-background-subagent-failed", {
8880-
taskId: subagent.task.nativeTaskRef?.nativeId,
8895+
taskId,
88818896
toolUseId,
88828897
cause,
88838898
}),
@@ -8914,12 +8929,24 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
89148929
fileSystem,
89158930
skillNames: yield* userInvocableSkillNames(currentTurn.input.runtimePolicy.cwd),
89168931
});
8932+
yield* backgroundForegroundSubagents(existing);
8933+
// The turn can end while the moves are answered. A steer offered
8934+
// then would start a new turn the orchestrator never asked for, so
8935+
// the steer fails and the orchestrator starts the follow-up turn.
8936+
if (
8937+
(yield* Ref.get(activeTurn))?.providerTurnId !== turnInput.providerTurnId ||
8938+
(yield* Ref.get(queryContext)) !== existing
8939+
) {
8940+
return yield* new ProviderAdapter.ProviderAdapterProtocolError({
8941+
driver: CLAUDE_PROVIDER,
8942+
detail: `Claude provider turn ${turnInput.providerTurnId} ended before the steer was sent.`,
8943+
});
8944+
}
89178945
yield* Ref.update(steeredTurns, (current) => {
89188946
const next = new Set(current);
89198947
next.add(turnInput.providerTurnId);
89208948
return next;
89218949
});
8922-
yield* backgroundForegroundSubagents(existing);
89238950
yield* existing.query.offer(userMessage);
89248951
},
89258952
(effect, turnInput) =>

0 commit comments

Comments
 (0)