@devries-cameron20 / t3code Mirror · read-only / commits / 41588d4520
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 08:15 UTC

fix(claude): approval cards close when Claude withdraws the request (#27320)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

ash committed Nov 1, 2026 at 22:43 UTC 41588d4520b345ce42b5824035c1f498d8de2d3a
2 files changed +126
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts
+90
@@ -10088,6 +10088,96 @@ describe("ClaudeAdapterV2 background wake turns", () => {
10088 ),
10089 );
10090
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 +
10181 it.effect("releases output held for a prompt echo when the stream ends", () =>
10182 Effect.scoped(
10183 Effect.gen(function* () {
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
+36
@@ -7258,6 +7258,40 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
7258 return { node, request, turnItem };
7259 });
7260
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 +
7295 const ensureReasoningBlock = Effect.fnUntraced(function* (
7296 context: ActiveClaudeTurnContext,
7297 itemId: string,
@@ -9735,6 +9769,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
9769 }),
9770 ),
9771 );
9772 + if (!(yield* Deferred.isDone(answers))) yield* cancelWithdrawnRequest(artifacts);
9773 return callbackOptions.signal.aborted
9774 ? ({
9775 behavior: "deny",
@@ -9875,6 +9910,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
9910 }),
9911 ),
9912 );
9913 + if (!(yield* Deferred.isDone(decision))) yield* cancelWithdrawnRequest(artifacts);
9914
9915 return permissionResultFromDecision({
9916 toolName,