Skip to content

Commit 52d25f8

Browse files
PR Batch Testerclaude
andcommitted
fix(server): repeated steers reuse a background move Claude has not answered
Each steer forked a new backgroundTasks request for every running subagent, so a request Claude never answered piled up once per steer. A later steer now waits on the move still in flight for that run. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent e8ab9f1 commit 52d25f8

2 files changed

Lines changed: 37 additions & 5 deletions

File tree

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14010,6 +14010,22 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1401014010
assert.isTrue(Exit.isSuccess(steered));
1401114011
assert.equal(harness.offeredMessages.length, offered + 1);
1401214012
assert.equal(harness.offeredMessages.at(-1)?.priority, "now");
14013+
if (backgrounding === "never answers") {
14014+
// Later steers wait on the move still in flight instead of
14015+
// piling up another request for the same run.
14016+
for (let repeat = 1; repeat <= 2; repeat++) {
14017+
const again = yield* Effect.forkScoped(steer);
14018+
// The steer's 5 second wait starts once it reaches it; move the
14019+
// clock until the steer has gone out.
14020+
for (let tick = 0; tick < 100 && again.pollUnsafe() === undefined; tick++) {
14021+
yield* TestClock.adjust("1 second");
14022+
yield* Effect.yieldNow;
14023+
}
14024+
assert.isTrue(Exit.isSuccess(yield* Fiber.await(again)));
14025+
assert.equal(harness.offeredMessages.length, offered + 1 + repeat);
14026+
}
14027+
assert.deepEqual(calls, [FOREGROUND_TOOL_USE_ID]);
14028+
}
1401314029
if (backgrounding === "answers after the steer went out") {
1401414030
yield* TestClock.adjust("1 second");
1401514031
} else if (

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

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3498,6 +3498,10 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
34983498
// the turn that launched the subagent (wake drain, later turns) resolve
34993499
// their subagent through this index into sessionSubagentsByTaskId.
35003500
const sessionSubagentTaskIdsByToolUseId = yield* Ref.make(new Map<string, string>());
3501+
// Requests that move a running subagent to the background before a
3502+
// steer, by the tool call that started its run. Claude may never
3503+
// answer one, so a later steer waits on it rather than asking again.
3504+
const backgroundMovesByToolUseId = new Map<string, Fiber.Fiber<void>>();
35013505
// A subagent's tool calls can outlive the turn that started them, so
35023506
// they live here rather than on a turn context: a turn that ends while
35033507
// one runs neither fails it nor hides it from the turn that follows.
@@ -8870,9 +8874,19 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
88708874
// Backgrounding is refused when the session disables background
88718875
// tasks, and the SDK puts no deadline on the request. Either way the
88728876
// steer still goes through, as it did before. A move answered after
8873-
// the steer stopped waiting for it still marks the subagent.
8874-
const moves = yield* Effect.forEach(foreground, ({ taskId, toolUseId }) =>
8875-
live.query.backgroundTasks(toolUseId).pipe(
8877+
// the steer stopped waiting for it still marks the subagent, and a
8878+
// later steer waits on that same move instead of asking again.
8879+
for (const [toolUseId, move] of backgroundMovesByToolUseId) {
8880+
if (move.pollUnsafe() !== undefined) backgroundMovesByToolUseId.delete(toolUseId);
8881+
}
8882+
const moves: Array<Fiber.Fiber<void>> = [];
8883+
for (const { taskId, toolUseId } of foreground) {
8884+
const inFlight = backgroundMovesByToolUseId.get(toolUseId);
8885+
if (inFlight !== undefined) {
8886+
moves.push(inFlight);
8887+
continue;
8888+
}
8889+
const move = yield* live.query.backgroundTasks(toolUseId).pipe(
88768890
// Now a background subagent: its end wakes the root like one
88778891
// Claude started in the background. One that already ended
88788892
// while the move was answered has nothing left to report.
@@ -8899,8 +8913,10 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
88998913
}),
89008914
),
89018915
Effect.forkIn(sessionScope),
8902-
),
8903-
);
8916+
);
8917+
backgroundMovesByToolUseId.set(toolUseId, move);
8918+
moves.push(move);
8919+
}
89048920
yield* Fiber.joinAll(moves).pipe(Effect.timeoutOption("5 seconds"));
89058921
});
89068922

0 commit comments

Comments
 (0)