Skip to content

Commit 64416cb

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
1 parent 3ddfcfc commit 64416cb

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
@@ -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* () {

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9388,6 +9388,31 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
93889388
}),
93899389
),
93909390
);
9391+
// The CLI withdrew the request (a PermissionRequest hook or Auto mode answered it),
9392+
// so nobody can answer this card any more.
9393+
if (!(yield* Deferred.isDone(decision))) {
9394+
const resolvedAt = yield* DateTime.now;
9395+
yield* emitProviderEvent({
9396+
type: "runtime_request.updated",
9397+
driver: CLAUDE_PROVIDER,
9398+
runtimeRequest: { ...artifacts.request, status: "cancelled", resolvedAt },
9399+
});
9400+
yield* emitProviderEvent({
9401+
type: "node.updated",
9402+
driver: CLAUDE_PROVIDER,
9403+
node: { ...artifacts.node, status: "cancelled", completedAt: resolvedAt },
9404+
});
9405+
yield* emitProviderEvent({
9406+
type: "turn_item.updated",
9407+
driver: CLAUDE_PROVIDER,
9408+
turnItem: {
9409+
...artifacts.turnItem,
9410+
status: "cancelled",
9411+
completedAt: resolvedAt,
9412+
updatedAt: resolvedAt,
9413+
},
9414+
});
9415+
}
93919416

93929417
return permissionResultFromDecision({
93939418
toolName,

0 commit comments

Comments
 (0)