Skip to content

Commit 23cfcc8

Browse files
committed
fix(server): close Claude approval cards the CLI withdraws
Claude Code sends T3 its permission request at the same time as it runs PermissionRequest hooks (and Auto mode's classifier), then cancels T3's request when one of them answers first. The adapter returned, but the card it had drawn stayed pending: Approve failed with "No pending Claude runtime request", and until the server restarted the pending request kept the thread from being settled, snoozed, resumed or idle-compacted. When the request is aborted before anyone answered it, the adapter now marks its request, node and turn item cancelled, as the question path in #22803 does. A card someone answered keeps its answer.
1 parent 90c455f commit 23cfcc8

2 files changed

Lines changed: 148 additions & 0 deletions

File tree

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

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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* () {

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9092,6 +9092,31 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
90929092
}),
90939093
),
90949094
);
9095+
// The CLI withdrew the request (a PermissionRequest hook or Auto mode answered it),
9096+
// so nobody can answer this card any more.
9097+
if (!(yield* Deferred.isDone(decision))) {
9098+
const resolvedAt = yield* DateTime.now;
9099+
yield* emitProviderEvent({
9100+
type: "runtime_request.updated",
9101+
driver: CLAUDE_PROVIDER,
9102+
runtimeRequest: { ...artifacts.request, status: "cancelled", resolvedAt },
9103+
});
9104+
yield* emitProviderEvent({
9105+
type: "node.updated",
9106+
driver: CLAUDE_PROVIDER,
9107+
node: { ...artifacts.node, status: "cancelled", completedAt: resolvedAt },
9108+
});
9109+
yield* emitProviderEvent({
9110+
type: "turn_item.updated",
9111+
driver: CLAUDE_PROVIDER,
9112+
turnItem: {
9113+
...artifacts.turnItem,
9114+
status: "cancelled",
9115+
completedAt: resolvedAt,
9116+
updatedAt: resolvedAt,
9117+
},
9118+
});
9119+
}
90959120

90969121
return permissionResultFromDecision({
90979122
toolName,

0 commit comments

Comments
 (0)