Skip to content

Commit 41588d4

Browse files
ashx-jclaude
andauthored
fix(claude): approval cards close when Claude withdraws the request (pingdotgg#27320)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 0bccd29 commit 41588d4

2 files changed

Lines changed: 126 additions & 0 deletions

File tree

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

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

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7258,6 +7258,40 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
72587258
return { node, request, turnItem };
72597259
});
72607260

7261+
// Claude withdraws a permission request it no longer needs, for example
7262+
// when a PermissionRequest hook answered it. Close the request and its
7263+
// card, which would otherwise wait for an answer nothing reads.
7264+
const cancelWithdrawnRequest = Effect.fnUntraced(function* (
7265+
artifacts: Effect.Success<ReturnType<typeof buildApprovalRequestArtifacts>>,
7266+
) {
7267+
const now = yield* DateTime.now;
7268+
yield* Effect.all(
7269+
[
7270+
emitProviderEvent({
7271+
type: "runtime_request.updated",
7272+
driver: CLAUDE_PROVIDER,
7273+
runtimeRequest: { ...artifacts.request, status: "cancelled", resolvedAt: now },
7274+
}),
7275+
emitProviderEvent({
7276+
type: "node.updated",
7277+
driver: CLAUDE_PROVIDER,
7278+
node: { ...artifacts.node, status: "cancelled", completedAt: now },
7279+
}),
7280+
emitProviderEvent({
7281+
type: "turn_item.updated",
7282+
driver: CLAUDE_PROVIDER,
7283+
turnItem: {
7284+
...artifacts.turnItem,
7285+
status: "cancelled",
7286+
completedAt: now,
7287+
updatedAt: now,
7288+
},
7289+
}),
7290+
],
7291+
{ concurrency: 1 },
7292+
);
7293+
});
7294+
72617295
const ensureReasoningBlock = Effect.fnUntraced(function* (
72627296
context: ActiveClaudeTurnContext,
72637297
itemId: string,
@@ -9735,6 +9769,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
97359769
}),
97369770
),
97379771
);
9772+
if (!(yield* Deferred.isDone(answers))) yield* cancelWithdrawnRequest(artifacts);
97389773
return callbackOptions.signal.aborted
97399774
? ({
97409775
behavior: "deny",
@@ -9875,6 +9910,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
98759910
}),
98769911
),
98779912
);
9913+
if (!(yield* Deferred.isDone(decision))) yield* cancelWithdrawnRequest(artifacts);
98789914

98799915
return permissionResultFromDecision({
98809916
toolName,

0 commit comments

Comments
 (0)