@@ -3280,6 +3280,129 @@ describe("ClaudeAdapterV2 background wake turns", () => {
32803280 ),
32813281 );
32823282
3283+ describe("approvals Claude withdraws", () => {
3284+ const startFullAccessTurn = Effect.fn("startFullAccessTurn")(function* (
3285+ harness: Effect.Success<typeof makeWakeHarness>,
3286+ attempt: string,
3287+ ) {
3288+ yield* harness.runtime.startTurn(
3289+ makeClaudeTestTurnInput({
3290+ threadId: harness.threadId,
3291+ providerThread: harness.providerThread,
3292+ now: yield* DateTime.now,
3293+ attemptId: RunAttemptId.make(attempt),
3294+ text: "Clear the render folder.",
3295+ attachments: [],
3296+ }),
3297+ );
3298+ });
3299+
3300+ const openApproval = Effect.fn("openApproval")(function* (
3301+ harness: Effect.Success<typeof makeWakeHarness>,
3302+ toolUseID: string,
3303+ ) {
3304+ const controller = new AbortController();
3305+ const callback = yield* Effect.promise(() =>
3306+ harness.getOpenedOptions()!.canUseTool!(
3307+ "Bash",
3308+ { command: "F=$(cat dir.txt); rm -f $F/*" },
3309+ {
3310+ signal: controller.signal,
3311+ toolUseID,
3312+ requestId: `${toolUseID}-request`,
3313+ decisionReason: "Dangerous rm operation on possibly-empty variable path",
3314+ },
3315+ ),
3316+ ).pipe(Effect.forkScoped);
3317+ let opened = yield* Queue.take(harness.runtimeRequestReceipts);
3318+ while (opened.runtimeRequest.nativeRequestRef?.nativeId !== toolUseID) {
3319+ opened = yield* Queue.take(harness.runtimeRequestReceipts);
3320+ }
3321+ assert.equal(opened.runtimeRequest.kind, "command");
3322+ assert.equal(opened.runtimeRequest.status, "pending");
3323+ return { controller, callback, request: opened.runtimeRequest };
3324+ });
3325+
3326+ // Events reach the harness in emission order, so once a later card has arrived every
3327+ // update the adapter made to an earlier one has too.
3328+ const updatesAfterBarrier = Effect.fn("updatesAfterBarrier")(function* (
3329+ harness: Effect.Success<typeof makeWakeHarness>,
3330+ request: Effect.Success<ReturnType<typeof openApproval>>["request"],
3331+ ) {
3332+ yield* openApproval(harness, "barrier-tool");
3333+ return harness.events.flatMap((event) => {
3334+ if (event.type === "runtime_request.updated") {
3335+ return event.runtimeRequest.id === request.id
3336+ ? [`request:${event.runtimeRequest.status}`]
3337+ : [];
3338+ }
3339+ if (event.type === "node.updated") {
3340+ return event.node.id === request.nodeId ? [`node:${event.node.status}`] : [];
3341+ }
3342+ if (event.type === "turn_item.updated" && event.turnItem.type === "approval_request") {
3343+ return event.turnItem.requestId === request.id ? [`item:${event.turnItem.status}`] : [];
3344+ }
3345+ return [];
3346+ });
3347+ });
3348+
3349+ // A PermissionRequest hook or Auto mode's classifier answers while T3's card is open, and
3350+ // the CLI withdraws the request it sent T3.
3351+ it.effect("closes the card when the CLI cancels its request", () =>
3352+ Effect.gen(function* () {
3353+ const harness = yield* makeWakeHarness;
3354+ yield* startFullAccessTurn(harness, "attempt-withdrawn-approval");
3355+ const approval = yield* openApproval(harness, "withdrawn-tool");
3356+
3357+ approval.controller.abort();
3358+ assert.equal((yield* Fiber.join(approval.callback))?.behavior, "deny");
3359+ assert.deepEqual(yield* updatesAfterBarrier(harness, approval.request), [
3360+ "node:waiting",
3361+ "request:pending",
3362+ "item:waiting",
3363+ "request:cancelled",
3364+ "node:cancelled",
3365+ "item:cancelled",
3366+ ]);
3367+ }).pipe(
3368+ Effect.provide(
3369+ Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
3370+ ),
3371+ ),
3372+ );
3373+
3374+ it.effect("leaves a card the user answered to the user's decision", () =>
3375+ Effect.gen(function* () {
3376+ const harness = yield* makeWakeHarness;
3377+ yield* startFullAccessTurn(harness, "attempt-cancelled-by-user");
3378+ const approval = yield* openApproval(harness, "cancelled-by-user-tool");
3379+
3380+ yield* harness.runtime.respondToRuntimeRequest({
3381+ requestId: approval.request.id,
3382+ decision: "cancel",
3383+ });
3384+ assert.deepEqual(yield* Fiber.join(approval.callback), {
3385+ behavior: "deny",
3386+ message: "User cancelled tool execution.",
3387+ toolUseID: "cancelled-by-user-tool",
3388+ decisionClassification: "user_reject",
3389+ interrupt: true,
3390+ });
3391+ // The interrupt the answer asked for then aborts the answered request.
3392+ approval.controller.abort();
3393+ assert.deepEqual(yield* updatesAfterBarrier(harness, approval.request), [
3394+ "node:waiting",
3395+ "request:pending",
3396+ "item:waiting",
3397+ ]);
3398+ }).pipe(
3399+ Effect.provide(
3400+ Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
3401+ ),
3402+ ),
3403+ );
3404+ });
3405+
32833406 describe("Claude settings restrictions", () => {
32843407 it.effect("resolves cached managed policy from the configured home through the real SDK", () =>
32853408 Effect.gen(function* () {
0 commit comments