Skip to content

Commit aa3d698

Browse files
committed
fix(server): Claude steer interrupts a running command instead of waiting for it
A user's steer interrupts the Claude turn without closing the query, then offers the message, as Esc then send does in Claude Code. The existing active-steering handling keeps the turn open across the aborted result. Agent and scheduled-task steers keep the old behavior. The query declares perTaskStopAffordance so the interrupt spares background subagents. Fixes #15720.
1 parent 1e2ecbd commit aa3d698

5 files changed

Lines changed: 163 additions & 13 deletions

File tree

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

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
ProviderTurnId,
2727
RunAttemptId,
2828
RunId,
29+
ScheduledTaskId,
2930
ThreadId,
3031
} from "@t3tools/contracts";
3132
import { assert, describe, it } from "@effect/vitest";
@@ -2060,7 +2061,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
20602061

20612062
const makeWakeHarnessWithOptions = (options?: {
20622063
readonly close?: (sdkMessages: Queue.Queue<SDKMessage>) => Effect.Effect<void>;
2063-
readonly interrupt?: Effect.Effect<void>;
2064+
readonly interrupt?: Effect.Effect<void, ClaudeAdapterV2.ClaudeAgentSdkQueryRunnerError>;
20642065
readonly environment?: NodeJS.ProcessEnv;
20652066
}) =>
20662067
Effect.gen(function* () {
@@ -2553,6 +2554,106 @@ describe("ClaudeAdapterV2 background wake turns", () => {
25532554
),
25542555
);
25552556

2557+
it.effect.each([
2558+
{ sender: "a user's", createdBy: "user", scheduled: false, interrupts: true },
2559+
{ sender: "an agent's", createdBy: "agent", scheduled: false, interrupts: false },
2560+
{ sender: "a scheduled task's", createdBy: "user", scheduled: true, interrupts: false },
2561+
] as const)(
2562+
"$sender steer interrupts the turn before it is offered: $interrupts",
2563+
({ createdBy, scheduled, interrupts }) =>
2564+
Effect.scoped(
2565+
Effect.gen(function* () {
2566+
// Offers already sent when each interrupt reached the CLI.
2567+
const offersAtInterrupt: Array<number> = [];
2568+
let offered: ReadonlyArray<SDKUserMessage> = [];
2569+
const harness = yield* makeWakeHarnessWithOptions({
2570+
interrupt: Effect.sync(() => {
2571+
offersAtInterrupt.push(offered.length);
2572+
}),
2573+
});
2574+
offered = harness.offeredMessages;
2575+
const idAllocator = yield* IdAllocator.IdAllocatorV2;
2576+
const attemptId = RunAttemptId.make("attempt-steer-interrupt");
2577+
const input = makeClaudeTestTurnInput({
2578+
threadId: harness.threadId,
2579+
providerThread: harness.providerThread,
2580+
now: yield* DateTime.now,
2581+
attemptId,
2582+
text: "Scan the repository.",
2583+
attachments: [],
2584+
});
2585+
yield* harness.runtime.startTurn(input);
2586+
yield* harness.runtime.steerTurn({
2587+
threadId: harness.threadId,
2588+
runId: input.runId,
2589+
providerThread: harness.providerThread,
2590+
providerTurnId: idAllocator.derive.providerTurn({
2591+
driver: ClaudeAdapterV2.CLAUDE_PROVIDER,
2592+
nativeTurnId: `turn:${attemptId}`,
2593+
}),
2594+
message: {
2595+
createdBy,
2596+
creationSource: scheduled ? "server" : "web",
2597+
messageId: MessageId.make("message-steer-interrupt"),
2598+
text: "Stop waiting and summarize what you have.",
2599+
attachments: [],
2600+
...(scheduled ? { scheduledTaskId: ScheduledTaskId.make("scheduled-scan") } : {}),
2601+
},
2602+
});
2603+
2604+
// The interrupt lands before the steer is offered, so the steer is
2605+
// never folded into the turn the interrupt aborts.
2606+
assert.deepEqual(offersAtInterrupt, interrupts ? [1] : []);
2607+
assert.equal(harness.offeredMessages[1]?.priority, "now");
2608+
}).pipe(Effect.provide(Layer.merge(IdAllocator.layer, NodeServices.layer))),
2609+
),
2610+
);
2611+
2612+
it.effect("still offers a user steer when the interrupt fails", () =>
2613+
Effect.scoped(
2614+
Effect.gen(function* () {
2615+
const harness = yield* makeWakeHarnessWithOptions({
2616+
interrupt: Effect.fail(
2617+
new ClaudeAdapterV2.ClaudeAgentSdkQueryRunnerError({
2618+
method: "interrupt",
2619+
cause: "forced interrupt failure",
2620+
}),
2621+
),
2622+
});
2623+
const idAllocator = yield* IdAllocator.IdAllocatorV2;
2624+
const attemptId = RunAttemptId.make("attempt-steer-interrupt-failed");
2625+
const input = makeClaudeTestTurnInput({
2626+
threadId: harness.threadId,
2627+
providerThread: harness.providerThread,
2628+
now: yield* DateTime.now,
2629+
attemptId,
2630+
text: "Scan the repository.",
2631+
attachments: [],
2632+
});
2633+
yield* harness.runtime.startTurn(input);
2634+
yield* harness.runtime.steerTurn({
2635+
threadId: harness.threadId,
2636+
runId: input.runId,
2637+
providerThread: harness.providerThread,
2638+
providerTurnId: idAllocator.derive.providerTurn({
2639+
driver: ClaudeAdapterV2.CLAUDE_PROVIDER,
2640+
nativeTurnId: `turn:${attemptId}`,
2641+
}),
2642+
message: {
2643+
createdBy: "user",
2644+
creationSource: "web",
2645+
messageId: MessageId.make("message-steer-interrupt-failed"),
2646+
text: "Stop waiting and summarize what you have.",
2647+
attachments: [],
2648+
},
2649+
});
2650+
2651+
assert.lengthOf(harness.offeredMessages, 2);
2652+
assert.equal(harness.offeredMessages[1]?.priority, "now");
2653+
}).pipe(Effect.provide(Layer.merge(IdAllocator.layer, NodeServices.layer))),
2654+
),
2655+
);
2656+
25562657
it.effect("announces usage-limit pauses once per window and again on a new turn", () =>
25572658
Effect.gen(function* () {
25582659
const harness = yield* makeWakeHarness;

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1662,9 +1662,16 @@ async function recordClaudeActiveSteeringQuery(input: {
16621662
promptQueue.offer(message);
16631663
offeredPrompts.add(index);
16641664
};
1665-
const offerSteeringPrompts = () => {
1665+
// Like ClaudeAdapterV2.steerTurn, a user's steer interrupts the turn before it is offered.
1666+
const offerSteeringPrompts = async () => {
16661667
for (let index = 1; index < input.prompts.length; index += 1) {
16671668
if (!offeredPrompts.has(index)) {
1669+
input.entries.push({
1670+
type: "expect_outbound",
1671+
label: `query.interrupt:${index}`,
1672+
frame: { type: "query.interrupt" },
1673+
});
1674+
await queryRuntime.interrupt();
16681675
offerPrompt(index, "now");
16691676
}
16701677
}
@@ -1731,7 +1738,7 @@ async function recordClaudeActiveSteeringQuery(input: {
17311738
const iterator = queryRuntime[Symbol.asyncIterator]();
17321739
try {
17331740
offerPrompt(0);
1734-
offerSteeringPrompts();
1741+
await offerSteeringPrompts();
17351742
const completed = await recordMessagesUntilTurnResults({
17361743
iterator,
17371744
entries: input.entries,

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -902,6 +902,10 @@ export function makeClaudeQueryOptions(input: {
902902
...(input.settings?.binaryPath
903903
? { pathToClaudeCodeExecutable: input.settings.binaryPath }
904904
: {}),
905+
// A steer interrupts the turn without closing the query. Undeclared, that
906+
// interrupt also stops background subagents. T3 stops background work by
907+
// closing the query (Stop) rather than through `stop_task`.
908+
perTaskStopAffordance: true,
905909
...(input.environment === undefined ? {} : { env: input.environment }),
906910
...(input.mcpServers === undefined ? {} : { mcpServers: input.mcpServers }),
907911
systemPrompt: {
@@ -2344,6 +2348,14 @@ function isClaudeActiveSteeringAbortResult(message: SDKResultMessage): boolean {
23442348
);
23452349
}
23462350

2351+
// Claude reads a `now` message only once the running tool returns, so a steer
2352+
// the user sends interrupts the turn first, as Esc then send does in Claude
2353+
// Code. Agent and scheduled deliveries never cut off a command the agent is
2354+
// waiting on; they join at the next tool boundary.
2355+
function claudeSteerInterruptsTurn(message: ProviderAdapter.ProviderAdapterV2TurnMessage): boolean {
2356+
return message.createdBy === "user" && message.scheduledTaskId === undefined;
2357+
}
2358+
23472359
function isClaudeProviderContinuationTurn(
23482360
input: ProviderAdapter.ProviderAdapterV2TurnInput,
23492361
): boolean {
@@ -7375,6 +7387,21 @@ export function makeClaudeAdapterV2(
73757387
next.add(turnInput.providerTurnId);
73767388
return next;
73777389
});
7390+
// The query stays open: the interrupted turn's aborted result is
7391+
// skipped (see isClaudeActiveSteeringAbortResult) and the steer
7392+
// runs next in the same turn. Offering after the interrupt keeps
7393+
// the steer out of the aborted turn, which a tool returning just
7394+
// before the interrupt could otherwise fold it into.
7395+
if (claudeSteerInterruptsTurn(turnInput.message)) {
7396+
yield* existing.query.interrupt.pipe(
7397+
Effect.catch((cause) =>
7398+
Effect.logWarning("orchestration-v2.claude-steer-interrupt-failed", {
7399+
providerTurnId: turnInput.providerTurnId,
7400+
cause,
7401+
}),
7402+
),
7403+
);
7404+
}
73787405
yield* existing.query.offer(userMessage);
73797406
},
73807407
(effect, turnInput) =>

0 commit comments

Comments
 (0)