@@ -10088,6 +10088,96 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1008810088 ),
1008910089 );
1009010090
10091+ it.effect.each([
10092+ { card: "an approval", toolName: "Bash", toolInput: { command: "echo probe" } },
10093+ {
10094+ card: "a question",
10095+ toolName: "AskUserQuestion",
10096+ toolInput: {
10097+ questions: [
10098+ {
10099+ header: "Target",
10100+ question: "Where should this deploy?",
10101+ options: [
10102+ { label: "Production", description: "Deploy to production." },
10103+ { label: "Staging", description: "Deploy to staging." },
10104+ ],
10105+ multiSelect: false,
10106+ },
10107+ ],
10108+ },
10109+ },
10110+ ])("closes $card card when Claude withdraws its request", ({ toolName, toolInput }) =>
10111+ Effect.scoped(
10112+ Effect.gen(function* () {
10113+ const harness = yield* makeWakeHarness;
10114+ yield* harness.runtime.startTurn(
10115+ makeClaudeTestTurnInput({
10116+ threadId: harness.threadId,
10117+ providerThread: harness.providerThread,
10118+ now: yield* DateTime.now,
10119+ attemptId: RunAttemptId.make("attempt-claude-withdrawn-approval"),
10120+ text: "Run the probe.",
10121+ attachments: [],
10122+ runtimePolicy: ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
10123+ runtimeMode: "approval-required",
10124+ interactionMode: "default",
10125+ cwd: "/workspace",
10126+ }),
10127+ }),
10128+ );
10129+ // The CLI cancels a request once a PermissionRequest hook has answered it.
10130+ // @effect-diagnostics-next-line abortControllerInEffect:off - Claude, not the test fiber, withdraws the request.
10131+ const withdrawal = new AbortController();
10132+ const permission = yield* Effect.promise(() =>
10133+ harness.getOpenedOptions()!.canUseTool!(toolName, toolInput, {
10134+ signal: withdrawal.signal,
10135+ toolUseID: "toolu_01WithdrawnApproval",
10136+ requestId: "request-withdrawn-approval",
10137+ }),
10138+ ).pipe(Effect.forkScoped);
10139+ const pending = (yield* Queue.take(harness.runtimeRequestReceipts)).runtimeRequest;
10140+ assert.equal(pending.status, "pending");
10141+
10142+ // The turn can end right after the withdrawal, before the callback returns.
10143+ withdrawal.abort();
10144+ yield* Queue.offer(
10145+ harness.sdkMessages,
10146+ claudeSdkFrame({
10147+ ...makeResultFrame({ uuid: "00000000-0000-4000-8000-000000000790", result: "Done." }),
10148+ user_message_uuid: harness.promptUuid(0),
10149+ }),
10150+ );
10151+ yield* harness.awaitTerminal(1);
10152+ assert.equal((yield* Fiber.join(permission))?.behavior, "deny");
10153+ // The cancellation has to land before the turn's end finalizes the run.
10154+ const beforeTerminal = harness.events.slice(
10155+ 0,
10156+ harness.events.findIndex((event) => event.type === "turn.terminal"),
10157+ );
10158+ const statusOf = (type: "runtime_request.updated" | "node.updated" | "turn_item.updated") =>
10159+ beforeTerminal
10160+ .flatMap((event): Array<{ readonly type: string; readonly status: string }> =>
10161+ event.type === "runtime_request.updated" && event.runtimeRequest.id === pending.id
10162+ ? [{ type: event.type, status: event.runtimeRequest.status }]
10163+ : event.type === "node.updated" && event.node.id === pending.nodeId
10164+ ? [{ type: event.type, status: event.node.status }]
10165+ : event.type === "turn_item.updated" && event.turnItem.nodeId === pending.nodeId
10166+ ? [{ type: event.type, status: event.turnItem.status }]
10167+ : [],
10168+ )
10169+ .findLast((entry) => entry.type === type)?.status;
10170+ assert.equal(statusOf("runtime_request.updated"), "cancelled");
10171+ assert.equal(statusOf("node.updated"), "cancelled");
10172+ assert.equal(statusOf("turn_item.updated"), "cancelled");
10173+ }).pipe(
10174+ Effect.provide(
10175+ Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
10176+ ),
10177+ ),
10178+ ),
10179+ );
10180+
1009110181 it.effect("releases output held for a prompt echo when the stream ends", () =>
1009210182 Effect.scoped(
1009310183 Effect.gen(function* () {
0 commit comments