@@ -2136,7 +2136,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
21362136
21372137 const makeWakeHarnessWithOptions = (options?: {
21382138 readonly close?: (sdkMessages: Queue.Queue<SDKMessage>) => Effect.Effect<void>;
2139- readonly interrupt?: Effect.Effect<void>;
2139+ readonly interrupt?: Effect.Effect<void, ClaudeAdapterV2.ClaudeAgentSdkQueryRunnerError >;
21402140 readonly setPermissionMode?: ClaudeAdapterV2.ClaudeAgentSdkQuerySession["setPermissionMode"];
21412141 readonly stopTask?: (
21422142 taskId: string,
@@ -3568,6 +3568,68 @@ describe("ClaudeAdapterV2 background wake turns", () => {
35683568
35693569 // #15720: priority "now" alone leaves a running foreground Bash command
35703570 // blocking the CLI, so the steer is never answered until it exits.
3571+ const steerDuringRunningBash = Effect.fn("steerDuringRunningBash")(function* (
3572+ harness: Effect.Success<ReturnType<typeof makeWakeHarnessWithOptions>>,
3573+ name: string,
3574+ ) {
3575+ const idAllocator = yield* IdAllocator.IdAllocatorV2;
3576+ const attemptId = RunAttemptId.make(`attempt-${name}`);
3577+ const input = makeClaudeTestTurnInput({
3578+ threadId: harness.threadId,
3579+ providerThread: harness.providerThread,
3580+ now: yield* DateTime.now,
3581+ attemptId,
3582+ text: "Run the slow request.",
3583+ attachments: [],
3584+ });
3585+ yield* harness.runtime.startTurn(input);
3586+ yield* harness.offerAndWait(
3587+ claudeSdkFrame({
3588+ type: "assistant",
3589+ message: {
3590+ model: "claude-sonnet-4-6",
3591+ id: `msg_${name}`,
3592+ type: "message",
3593+ role: "assistant",
3594+ content: [
3595+ {
3596+ type: "tool_use",
3597+ id: "toolu_steer_curl",
3598+ name: "Bash",
3599+ input: { command: "curl --max-time 180 http://127.0.0.1:15820/" },
3600+ },
3601+ ],
3602+ },
3603+ parent_tool_use_id: null,
3604+ uuid: "00000000-0000-4000-8000-000000000910",
3605+ session_id: WAKE_NATIVE_SESSION,
3606+ }),
3607+ );
3608+ return {
3609+ steer: harness.runtime.steerTurn({
3610+ threadId: harness.threadId,
3611+ runId: input.runId,
3612+ providerThread: harness.providerThread,
3613+ providerTurnId: idAllocator.derive.providerTurn({
3614+ driver: ClaudeAdapterV2.CLAUDE_PROVIDER,
3615+ nativeTurnId: `turn:${attemptId}`,
3616+ }),
3617+ message: {
3618+ createdBy: "user",
3619+ creationSource: "web",
3620+ messageId: MessageId.make(`message-${name}`),
3621+ text: "Change of plan: stop waiting for that command and reply with exactly STEERED_NOW.",
3622+ attachments: [],
3623+ },
3624+ }),
3625+ };
3626+ });
3627+ const steerTestLayer = Layer.mergeAll(
3628+ IdAllocator.layer,
3629+ McpProviderSessions.layer,
3630+ NodeServices.layer,
3631+ );
3632+
35713633 it.effect("steering releases a running foreground command", () =>
35723634 Effect.scoped(
35733635 Effect.gen(function* () {
@@ -3582,66 +3644,108 @@ describe("ClaudeAdapterV2 background wake turns", () => {
35823644 return true;
35833645 }),
35843646 });
3585- const idAllocator = yield* IdAllocator.IdAllocatorV2;
3586- const attemptId = RunAttemptId.make("attempt-steer-running-command");
3587- const input = makeClaudeTestTurnInput({
3588- threadId: harness.threadId,
3589- providerThread: harness.providerThread,
3590- now: yield* DateTime.now,
3591- attemptId,
3592- text: "Run the slow request.",
3593- attachments: [],
3647+ const { steer } = yield* steerDuringRunningBash(harness, "steer-running-command");
3648+ yield* steer;
3649+ assert.equal(harness.offeredMessages[1]?.priority, "now");
3650+ assert.deepEqual(
3651+ releases,
3652+ ["interrupt"],
3653+ "steer left the running Bash command blocking the turn",
3654+ );
3655+ }).pipe(Effect.provide(steerTestLayer)),
3656+ ),
3657+ );
3658+
3659+ // Interrupting aborts every pending permission callback, which would deny a
3660+ // question or approval the user never answered.
3661+ it.effect("steering leaves the command running while the user is asked something", () =>
3662+ Effect.scoped(
3663+ Effect.gen(function* () {
3664+ let interrupted = false;
3665+ const harness = yield* makeWakeHarnessWithOptions({
3666+ interrupt: Effect.sync(() => {
3667+ interrupted = true;
3668+ }),
35943669 });
3595- yield* harness.runtime.startTurn(input);
3596- yield* harness.offerAndWait(
3597- claudeSdkFrame({
3598- type: "assistant",
3599- message: {
3600- model: "claude-sonnet-4-6",
3601- id: "msg_steer_running_command",
3602- type: "message",
3603- role: "assistant",
3604- content: [
3670+ const { steer } = yield* steerDuringRunningBash(harness, "steer-pending-question");
3671+ const question = new AbortController();
3672+ yield* Effect.promise(() =>
3673+ harness.getOpenedOptions()!.canUseTool!(
3674+ "AskUserQuestion",
3675+ {
3676+ questions: [
36053677 {
3606- type: "tool_use",
3607- id: "toolu_steer_curl",
3608- name: "Bash",
3609- input: { command: "curl --max-time 180 http://127.0.0.1:15820/" },
3678+ header: "Target",
3679+ question: "Which environment?",
3680+ options: [
3681+ { label: "Production", description: "Deploy to production." },
3682+ { label: "Staging", description: "Deploy to staging." },
3683+ ],
3684+ multiSelect: false,
36103685 },
36113686 ],
36123687 },
3613- parent_tool_use_id: null,
3614- uuid: "00000000-0000-4000-8000-000000000910",
3615- session_id: WAKE_NATIVE_SESSION,
3688+ {
3689+ signal: question.signal,
3690+ toolUseID: "toolu_steer_question",
3691+ requestId: "request-steer-question",
3692+ },
3693+ ),
3694+ ).pipe(Effect.forkScoped);
3695+ yield* Queue.take(harness.runtimeRequestReceipts);
3696+ yield* steer;
3697+ assert.isFalse(interrupted, "steer cancelled the pending question");
3698+ assert.equal(harness.offeredMessages[1]?.priority, "now");
3699+ }).pipe(Effect.provide(steerTestLayer)),
3700+ ),
3701+ );
3702+
3703+ it.effect("a steer is not sent when the turn ends while the command is interrupted", () =>
3704+ Effect.scoped(
3705+ Effect.gen(function* () {
3706+ const interruptStarted = yield* Deferred.make<void>();
3707+ const interruptAnswered = yield* Deferred.make<void>();
3708+ const harness = yield* makeWakeHarnessWithOptions({
3709+ interrupt: Deferred.succeed(interruptStarted, undefined).pipe(
3710+ Effect.andThen(Deferred.await(interruptAnswered)),
3711+ ),
3712+ });
3713+ const { steer } = yield* steerDuringRunningBash(harness, "steer-turn-ends");
3714+ const steering = yield* Effect.exit(steer).pipe(Effect.forkScoped);
3715+ yield* Deferred.await(interruptStarted);
3716+ // The command finished on its own before the interrupt reached it.
3717+ yield* harness.offerAndWait(
3718+ makeResultFrame({
3719+ uuid: "00000000-0000-4000-8000-000000000911",
3720+ result: "Done.",
36163721 }),
36173722 );
3618- yield* harness.runtime.steerTurn({
3619- threadId: harness.threadId,
3620- runId: input.runId,
3621- providerThread: harness.providerThread,
3622- providerTurnId: idAllocator.derive.providerTurn({
3623- driver: ClaudeAdapterV2.CLAUDE_PROVIDER,
3624- nativeTurnId: `turn:${attemptId}`,
3625- }),
3626- message: {
3627- createdBy: "user",
3628- creationSource: "web",
3629- messageId: MessageId.make("message-steer-running-command"),
3630- text: "Change of plan: stop waiting for that command and reply with exactly STEERED_NOW.",
3631- attachments: [],
3632- },
3723+ yield* Queue.take(harness.terminalReceipts);
3724+ yield* Deferred.succeed(interruptAnswered, undefined);
3725+ const exit = yield* Fiber.join(steering);
3726+ assert.isTrue(Exit.isFailure(exit), "steer reported success after its turn ended");
3727+ assert.equal(harness.offeredMessages.length, 1, "steer started untracked native work");
3728+ }).pipe(Effect.provide(steerTestLayer)),
3729+ ),
3730+ );
3731+
3732+ // The CLI may already have aborted the command, and its aborted result is
3733+ // swallowed as a steered segment, so only the steer can continue the turn.
3734+ it.effect("a steer is still sent when interrupting the command fails", () =>
3735+ Effect.scoped(
3736+ Effect.gen(function* () {
3737+ const harness = yield* makeWakeHarnessWithOptions({
3738+ interrupt: Effect.fail(
3739+ new ClaudeAdapterV2.ClaudeAgentSdkQueryRunnerError({
3740+ method: "interrupt",
3741+ cause: new Error("control request failed"),
3742+ }),
3743+ ),
36333744 });
3745+ const { steer } = yield* steerDuringRunningBash(harness, "steer-interrupt-fails");
3746+ yield* steer;
36343747 assert.equal(harness.offeredMessages[1]?.priority, "now");
3635- assert.deepEqual(
3636- releases,
3637- ["interrupt"],
3638- "steer left the running Bash command blocking the turn",
3639- );
3640- }).pipe(
3641- Effect.provide(
3642- Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
3643- ),
3644- ),
3748+ }).pipe(Effect.provide(steerTestLayer)),
36453749 ),
36463750 );
36473751
0 commit comments