Skip to content

Commit e169f91

Browse files
PR Batch Testerclaude
andcommitted
fix(server): a late background answer does not mark a later run of the subagent
If the subagent ended and ran again under a new tool call before the move was answered, the new run was marked as backgrounded. The mark now also requires the run the move was asked for. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 440e2e8 commit e169f91

2 files changed

Lines changed: 52 additions & 14 deletions

File tree

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

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13801,6 +13801,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1380113801
{ backgrounding: "fails" },
1380213802
{ backgrounding: "never answers" },
1380313803
{ backgrounding: "answers after the turn ended" },
13804+
{ backgrounding: "answers after the subagent ran again" },
1380413805
] as const)(
1380513806
"moves the root's foreground subagents to the background before a steer ($backgrounding)",
1380613807
({ backgrounding }) =>
@@ -13810,14 +13811,15 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1381013811
const NESTED_TOOL_USE_ID = "toolu-steer-nested";
1381113812
const FINISHED_TOOL_USE_ID = "toolu-steer-finished";
1381213813
const calls: Array<string> = [];
13813-
// The root turn settles while the move is still unanswered.
13814-
let endTurnFirst: Effect.Effect<void> = Effect.void;
13814+
// What happens while the move is still unanswered.
13815+
let beforeAnswer: Effect.Effect<void> = Effect.void;
1381513816
const harness = yield* makeWakeHarnessWithOptions({
1381613817
backgroundTasks: (toolUseId) =>
1381713818
Effect.sync(() => calls.push(toolUseId)).pipe(
1381813819
Effect.andThen(
13819-
backgrounding === "answers after the turn ended"
13820-
? Effect.suspend(() => endTurnFirst).pipe(Effect.as(true))
13820+
backgrounding === "answers after the turn ended" ||
13821+
backgrounding === "answers after the subagent ran again"
13822+
? Effect.suspend(() => beforeAnswer).pipe(Effect.as(true))
1382113823
: backgrounding === "fails"
1382213824
? Effect.fail(
1382313825
new ClaudeAdapterV2.ClaudeAgentSdkQueryRunnerError({
@@ -13842,12 +13844,14 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1384213844
attachments: [],
1384313845
});
1384413846
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-
);
13847+
if (backgrounding === "answers after the turn ended") {
13848+
beforeAnswer = Queue.offer(
13849+
harness.sdkMessages,
13850+
makeResultFrame({ uuid: "00000000-0000-4000-8000-000000000a10", result: "Done." }),
13851+
).pipe(
13852+
Effect.andThen(awaitUntil(() => harness.terminalEvents().length === 1, "turn end")),
13853+
);
13854+
}
1385113855
const foregroundTaskStarted = (task: {
1385213856
readonly spawnDepth?: number;
1385313857
readonly taskId: string;
@@ -13955,6 +13959,30 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1395513959
attachments: [],
1395613960
},
1395713961
});
13962+
const RERUN_TOOL_USE_ID = "toolu-steer-rerun";
13963+
if (backgrounding === "answers after the subagent ran again") {
13964+
// It ends and a SendMessage runs it again, in the foreground.
13965+
beforeAnswer = harness
13966+
.offerAndWait(
13967+
makeSubagentNotificationFrame({
13968+
taskId: "task-steer-foreground",
13969+
toolUseId: FOREGROUND_TOOL_USE_ID,
13970+
summary: "First run done.",
13971+
uuid: "00000000-0000-4000-8000-000000000a12",
13972+
}),
13973+
)
13974+
.pipe(
13975+
Effect.andThen(
13976+
harness.offerAndWait(
13977+
foregroundTaskStarted({
13978+
taskId: "task-steer-foreground",
13979+
toolUseId: RERUN_TOOL_USE_ID,
13980+
uuid: "00000000-0000-4000-8000-000000000a13",
13981+
}),
13982+
),
13983+
),
13984+
);
13985+
}
1395813986
const steering = yield* Effect.forkScoped(steer);
1395913987
if (backgrounding === "never answers") {
1396013988
yield* awaitUntil(() => calls.length === 1, "background request");
@@ -13976,7 +14004,15 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1397614004
assert.isTrue(Exit.isSuccess(steered));
1397714005
assert.equal(harness.offeredMessages.length, offered + 1);
1397814006
assert.equal(harness.offeredMessages.at(-1)?.priority, "now");
13979-
if (backgrounding !== "succeeds") return;
14007+
if (
14008+
backgrounding !== "succeeds" &&
14009+
backgrounding !== "answers after the subagent ran again"
14010+
) {
14011+
return;
14012+
}
14013+
// The move answered for the first run; the second runs in the
14014+
// foreground, so its end is the parent's tool result, not a wake.
14015+
const reran = backgrounding === "answers after the subagent ran again";
1398014016

1398114017
// The moved subagent outlives the steered turn. When it ends during
1398214018
// the user's next turn, Claude's wake after that turn names it.
@@ -14005,7 +14041,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1400514041
for (const frame of [
1400614042
makeSubagentNotificationFrame({
1400714043
taskId: "task-steer-foreground",
14008-
toolUseId: FOREGROUND_TOOL_USE_ID,
14044+
toolUseId: reran ? RERUN_TOOL_USE_ID : FOREGROUND_TOOL_USE_ID,
1400914045
summary: "AUDITED",
1401014046
uuid: "00000000-0000-4000-8000-000000000a08",
1401114047
}),
@@ -14018,7 +14054,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1401814054
yield* awaitUntil(() => harness.continuationRequests.length === 1, "subagent wake");
1401914055
assert.equal(
1402014056
harness.continuationRequests[0]?.notification?.summary,
14021-
'Subagent "task-steer-foreground" finished',
14057+
reran ? undefined : 'Subagent "task-steer-foreground" finished',
1402214058
);
1402314059
}).pipe(
1402414060
Effect.provide(

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8881,7 +8881,9 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
88818881
moved
88828882
? Ref.get(sessionSubagentsByTaskId).pipe(
88838883
Effect.flatMap((registry) =>
8884-
registry.get(taskId)?.task.status === "running"
8884+
registry.get(taskId)?.task.status === "running" &&
8885+
// Not a later run of it that started meanwhile.
8886+
registry.get(taskId)?.runToolUseId === toolUseId
88858887
? Ref.update(backgroundedSubagentTaskIds, (current) =>
88868888
new Set(current).add(taskId),
88878889
)

0 commit comments

Comments
 (0)