Skip to content

Commit e32ab94

Browse files
PR Batch Testerclaude
andcommitted
fix(server): an unanswered background request cannot hold a steer
The SDK puts no deadline on control requests, so each backgroundTasks call now times out after 5 seconds and the calls run together. The steer goes out either way. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 0f55d6b commit e32ab94

2 files changed

Lines changed: 48 additions & 29 deletions

File tree

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

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13796,9 +13796,13 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1379613796
),
1379713797
);
1379813798

13799-
it.effect.each([{ backgroundingFails: false }, { backgroundingFails: true }])(
13800-
"moves the root's foreground subagents to the background before a steer (fails=$backgroundingFails)",
13801-
({ backgroundingFails }) =>
13799+
it.effect.each([
13800+
{ backgrounding: "succeeds" },
13801+
{ backgrounding: "fails" },
13802+
{ backgrounding: "never answers" },
13803+
] as const)(
13804+
"moves the root's foreground subagents to the background before a steer ($backgrounding)",
13805+
({ backgrounding }) =>
1380213806
Effect.scoped(
1380313807
Effect.gen(function* () {
1380413808
const FOREGROUND_TOOL_USE_ID = "toolu-steer-foreground";
@@ -13809,14 +13813,16 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1380913813
backgroundTasks: (toolUseId) =>
1381013814
Effect.sync(() => calls.push(toolUseId)).pipe(
1381113815
Effect.andThen(
13812-
backgroundingFails
13816+
backgrounding === "fails"
1381313817
? Effect.fail(
1381413818
new ClaudeAdapterV2.ClaudeAgentSdkQueryRunnerError({
1381513819
method: "backgroundTasks",
1381613820
cause: new Error("Background tasks are disabled in this session."),
1381713821
}),
1381813822
)
13819-
: Effect.succeed(true),
13823+
: backgrounding === "never answers"
13824+
? Effect.never
13825+
: Effect.succeed(true),
1382013826
),
1382113827
),
1382213828
});
@@ -13912,7 +13918,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1391213918
);
1391313919

1391413920
const offered = harness.offeredMessages.length;
13915-
yield* harness.runtime.steerTurn({
13921+
const steer = harness.runtime.steerTurn({
1391613922
threadId: harness.threadId,
1391713923
runId: input.runId,
1391813924
providerThread: harness.providerThread,
@@ -13928,10 +13934,17 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1392813934
attachments: [],
1392913935
},
1393013936
});
13937+
const steering = yield* Effect.forkScoped(steer);
13938+
if (backgrounding === "never answers") {
13939+
yield* awaitUntil(() => calls.length === 1, "background request");
13940+
assert.equal(harness.offeredMessages.length, offered);
13941+
yield* TestClock.adjust("5 seconds");
13942+
}
13943+
yield* Fiber.join(steering);
1393113944

1393213945
// Only the root's running subagent moves: the nested one follows its
13933-
// owner and the finished one has nothing left to move. A refused move
13934-
// still lets the steer through.
13946+
// owner and the finished one has nothing left to move. A refused or
13947+
// unanswered move still lets the steer through.
1393513948
assert.deepEqual(calls, [FOREGROUND_TOOL_USE_ID]);
1393613949
assert.equal(harness.offeredMessages.length, offered + 1);
1393713950
assert.equal(harness.offeredMessages.at(-1)?.priority, "now");

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

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8847,28 +8847,34 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
88478847
const backgroundForegroundSubagents = Effect.fnUntraced(function* (
88488848
live: ClaudeLiveQueryContext,
88498849
) {
8850-
for (const subagent of (yield* Ref.get(sessionSubagentsByTaskId)).values()) {
8851-
const toolUseId = subagent.runToolUseId;
8852-
if (
8853-
toolUseId === null ||
8854-
subagent.task.status !== "running" ||
8855-
subagent.task.parentNodeId !== subagent.rootNodeId ||
8856-
live.subagentsFromEarlierProcesses.has(subagent)
8857-
) {
8858-
continue;
8859-
}
8860-
// Backgrounding is refused when the session disables background
8861-
// tasks. The steer still goes through, as it did before.
8862-
yield* live.query.backgroundTasks(toolUseId).pipe(
8863-
Effect.catch((cause) =>
8864-
Effect.logWarning("orchestration-v2.claude-steer-background-subagent-failed", {
8865-
taskId: subagent.task.nativeTaskRef?.nativeId,
8866-
toolUseId,
8867-
cause,
8868-
}),
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+
);
8860+
// Backgrounding is refused when the session disables background
8861+
// tasks, and the SDK puts no deadline on the request. Either way the
8862+
// steer still goes through, as it did before.
8863+
yield* Effect.forEach(
8864+
foreground,
8865+
({ subagent, toolUseId }) =>
8866+
live.query.backgroundTasks(toolUseId).pipe(
8867+
Effect.timeout("5 seconds"),
8868+
Effect.catch((cause) =>
8869+
Effect.logWarning("orchestration-v2.claude-steer-background-subagent-failed", {
8870+
taskId: subagent.task.nativeTaskRef?.nativeId,
8871+
toolUseId,
8872+
cause,
8873+
}),
8874+
),
88698875
),
8870-
);
8871-
}
8876+
{ concurrency: "unbounded", discard: true },
8877+
);
88728878
});
88738879

88748880
const steerTurn = Effect.fn("ClaudeAdapterV2.steerTurn")(

0 commit comments

Comments
 (0)