@@ -2424,6 +2424,129 @@ describe("ClaudeAdapterV2 background wake turns", () => {
24242424 });
24252425 const makeWakeHarness = makeWakeHarnessWithOptions();
24262426
2427+ describe("approvals Claude withdraws", () => {
2428+ const startFullAccessTurn = Effect.fn("startFullAccessTurn")(function* (
2429+ harness: Effect.Success<typeof makeWakeHarness>,
2430+ attempt: string,
2431+ ) {
2432+ yield* harness.runtime.startTurn(
2433+ makeClaudeTestTurnInput({
2434+ threadId: harness.threadId,
2435+ providerThread: harness.providerThread,
2436+ now: yield* DateTime.now,
2437+ attemptId: RunAttemptId.make(attempt),
2438+ text: "Clear the render folder.",
2439+ attachments: [],
2440+ }),
2441+ );
2442+ });
2443+
2444+ const openApproval = Effect.fn("openApproval")(function* (
2445+ harness: Effect.Success<typeof makeWakeHarness>,
2446+ toolUseID: string,
2447+ ) {
2448+ const controller = new AbortController();
2449+ const callback = yield* Effect.promise(() =>
2450+ harness.getOpenedOptions()!.canUseTool!(
2451+ "Bash",
2452+ { command: "F=$(cat dir.txt); rm -f $F/*" },
2453+ {
2454+ signal: controller.signal,
2455+ toolUseID,
2456+ requestId: `${toolUseID}-request`,
2457+ decisionReason: "Dangerous rm operation on possibly-empty variable path",
2458+ },
2459+ ),
2460+ ).pipe(Effect.forkScoped);
2461+ let opened = yield* Queue.take(harness.runtimeRequestReceipts);
2462+ while (opened.runtimeRequest.nativeRequestRef?.nativeId !== toolUseID) {
2463+ opened = yield* Queue.take(harness.runtimeRequestReceipts);
2464+ }
2465+ assert.equal(opened.runtimeRequest.kind, "command");
2466+ assert.equal(opened.runtimeRequest.status, "pending");
2467+ return { controller, callback, request: opened.runtimeRequest };
2468+ });
2469+
2470+ // Events reach the harness in emission order, so once a later card has arrived every
2471+ // update the adapter made to an earlier one has too.
2472+ const updatesAfterBarrier = Effect.fn("updatesAfterBarrier")(function* (
2473+ harness: Effect.Success<typeof makeWakeHarness>,
2474+ request: Effect.Success<ReturnType<typeof openApproval>>["request"],
2475+ ) {
2476+ yield* openApproval(harness, "barrier-tool");
2477+ return harness.events.flatMap((event) => {
2478+ if (event.type === "runtime_request.updated") {
2479+ return event.runtimeRequest.id === request.id
2480+ ? [`request:${event.runtimeRequest.status}`]
2481+ : [];
2482+ }
2483+ if (event.type === "node.updated") {
2484+ return event.node.id === request.nodeId ? [`node:${event.node.status}`] : [];
2485+ }
2486+ if (event.type === "turn_item.updated" && event.turnItem.type === "approval_request") {
2487+ return event.turnItem.requestId === request.id ? [`item:${event.turnItem.status}`] : [];
2488+ }
2489+ return [];
2490+ });
2491+ });
2492+
2493+ // A PermissionRequest hook or Auto mode's classifier answers while T3's card is open, and
2494+ // the CLI withdraws the request it sent T3.
2495+ it.effect("closes the card when the CLI cancels its request", () =>
2496+ Effect.gen(function* () {
2497+ const harness = yield* makeWakeHarness;
2498+ yield* startFullAccessTurn(harness, "attempt-withdrawn-approval");
2499+ const approval = yield* openApproval(harness, "withdrawn-tool");
2500+
2501+ approval.controller.abort();
2502+ assert.equal((yield* Fiber.join(approval.callback))?.behavior, "deny");
2503+ assert.deepEqual(yield* updatesAfterBarrier(harness, approval.request), [
2504+ "node:waiting",
2505+ "request:pending",
2506+ "item:waiting",
2507+ "request:cancelled",
2508+ "node:cancelled",
2509+ "item:cancelled",
2510+ ]);
2511+ }).pipe(
2512+ Effect.provide(
2513+ Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
2514+ ),
2515+ ),
2516+ );
2517+
2518+ it.effect("leaves a card the user answered to the user's decision", () =>
2519+ Effect.gen(function* () {
2520+ const harness = yield* makeWakeHarness;
2521+ yield* startFullAccessTurn(harness, "attempt-cancelled-by-user");
2522+ const approval = yield* openApproval(harness, "cancelled-by-user-tool");
2523+
2524+ yield* harness.runtime.respondToRuntimeRequest({
2525+ requestId: approval.request.id,
2526+ decision: "cancel",
2527+ });
2528+ assert.deepEqual(yield* Fiber.join(approval.callback), {
2529+ behavior: "deny",
2530+ message: "User cancelled tool execution.",
2531+ toolUseID: "cancelled-by-user-tool",
2532+ decisionClassification: "user_reject",
2533+ interrupt: true,
2534+ });
2535+ // The interrupt the answer asked for then aborts the answered request.
2536+ approval.controller.abort();
2537+ assert.deepEqual(yield* updatesAfterBarrier(harness, approval.request), [
2538+ "node:waiting",
2539+ "request:pending",
2540+ "item:waiting",
2541+ ]);
2542+ }).pipe(
2543+ Effect.provide(
2544+ Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
2545+ ),
2546+ ),
2547+ );
2548+ });
2549+
24272550 describe("Claude settings restrictions", () => {
24282551 it.effect("resolves cached managed policy from the configured home through the real SDK", () =>
24292552 Effect.gen(function* () {
0 commit comments