diff --git a/package-lock.json b/package-lock.json index 4dbb6d3eb..52bce5f98 100644 --- a/package-lock.json +++ b/package-lock.json @@ -130,22 +130,22 @@ } }, "node_modules/@anthropic-ai/claude-agent-sdk": { - "version": "0.3.246", - "resolved": "https://registry.npmjs.org/@anthropic-ai/claude-agent-sdk/-/claude-agent-sdk-0.3.246.tgz", - "integrity": "sha512-FtR0HoHHNqeqJWjZN8qLUAzZVFUI9ztXYNPPwv98Ecmv9qq2QTauI8IzkY26CC0mleWAqb9RQEW2C0OtiUliug==", + "version": "0.3.326", + "resolved": "https://registry.npmjs.org/@anthropic-ai/claude-agent-sdk/-/claude-agent-sdk-0.3.326.tgz", + "integrity": "sha512-BUTSky/4YWfL/MxC+4kV0NDbJFjbJPoQMFEk/s9G6hV9LUfYPMbsWz94bFQ7ZIo2ubj2xwJ2c2NVxkNlQhVw+Q==", "license": "SEE LICENSE IN README.md", "engines": { - "node": ">=18.0.0" + "node": ">=18.14.0" }, "optionalDependencies": { - "@anthropic-ai/claude-agent-sdk-darwin-arm64": "0.3.246", - "@anthropic-ai/claude-agent-sdk-darwin-x64": "0.3.246", - "@anthropic-ai/claude-agent-sdk-linux-arm64": "0.3.246", - "@anthropic-ai/claude-agent-sdk-linux-arm64-musl": "0.3.246", - "@anthropic-ai/claude-agent-sdk-linux-x64": "0.3.246", - "@anthropic-ai/claude-agent-sdk-linux-x64-musl": "0.3.246", - "@anthropic-ai/claude-agent-sdk-win32-arm64": "0.3.246", - "@anthropic-ai/claude-agent-sdk-win32-x64": "0.3.246" + "@anthropic-ai/claude-agent-sdk-darwin-arm64": "0.3.326", + "@anthropic-ai/claude-agent-sdk-darwin-x64": "0.3.326", + "@anthropic-ai/claude-agent-sdk-linux-arm64": "0.3.326", + "@anthropic-ai/claude-agent-sdk-linux-arm64-musl": "0.3.326", + "@anthropic-ai/claude-agent-sdk-linux-x64": "0.3.326", + "@anthropic-ai/claude-agent-sdk-linux-x64-musl": "0.3.326", + "@anthropic-ai/claude-agent-sdk-win32-arm64": "0.3.326", + "@anthropic-ai/claude-agent-sdk-win32-x64": "0.3.326" }, "peerDependencies": { "@anthropic-ai/sdk": ">=0.93.0", @@ -154,9 +154,9 @@ } }, "node_modules/@anthropic-ai/claude-agent-sdk-darwin-arm64": { - "version": "0.3.246", - "resolved": "https://registry.npmjs.org/@anthropic-ai/claude-agent-sdk-darwin-arm64/-/claude-agent-sdk-darwin-arm64-0.3.246.tgz", - "integrity": "sha512-pYDv2RT+UVOvEapMEWKf7Gm5a87Si1Zf+XR23IMGYbG5mESatNJ8Gz2PmkrwfTezZknb0ljWZe8zgcTFITM3dA==", + "version": "0.3.326", + "resolved": "https://registry.npmjs.org/@anthropic-ai/claude-agent-sdk-darwin-arm64/-/claude-agent-sdk-darwin-arm64-0.3.326.tgz", + "integrity": "sha512-D3ZbowCMdKNet0YPZlsJxdJUW1t1Q8KLy1BwNd+5eilf18TlCRtWZlm8DrewFLmu/gxBK7gcsq6uysrIVbPyQg==", "cpu": [ "arm64" ], @@ -167,9 +167,9 @@ ] }, "node_modules/@anthropic-ai/claude-agent-sdk-darwin-x64": { - "version": "0.3.246", - "resolved": "https://registry.npmjs.org/@anthropic-ai/claude-agent-sdk-darwin-x64/-/claude-agent-sdk-darwin-x64-0.3.246.tgz", - "integrity": "sha512-yl+gMMFUQ8++SBaxUe2JxBqU0LOJO4HZTsWWTOrH5kRlTRhdQDF/lESsoNXCHgfF/+77YNKgef0h+rl/D12Jfg==", + "version": "0.3.326", + "resolved": "https://registry.npmjs.org/@anthropic-ai/claude-agent-sdk-darwin-x64/-/claude-agent-sdk-darwin-x64-0.3.326.tgz", + "integrity": "sha512-m8K3Im2S8STdAVBrriuDZyJcnjsu/Vc3476wIOqTAefnCCdtdZ4AHcOcWxt+4KIHE5Vx1qT2qacTRrQBaVVweA==", "cpu": [ "x64" ], @@ -180,9 +180,9 @@ ] }, "node_modules/@anthropic-ai/claude-agent-sdk-linux-arm64": { - "version": "0.3.246", - "resolved": "https://registry.npmjs.org/@anthropic-ai/claude-agent-sdk-linux-arm64/-/claude-agent-sdk-linux-arm64-0.3.246.tgz", - "integrity": "sha512-t7AEewD7bcjZYMmrfFLjsUSxPo4z+5J5O5cC9V2zOvAhNkefdMeGWI2/1cdqZxuaOiFVt46byjlZuiNNunF+bQ==", + "version": "0.3.326", + "resolved": "https://registry.npmjs.org/@anthropic-ai/claude-agent-sdk-linux-arm64/-/claude-agent-sdk-linux-arm64-0.3.326.tgz", + "integrity": "sha512-nYVN8jCcH490DCXIvby7rllzQFxLzKeKClfo2u/yv7mx3ipZQv344caULL/amwZ8w8GUzYyZ8hnS6pI9goSq+A==", "cpu": [ "arm64" ], @@ -193,9 +193,9 @@ ] }, "node_modules/@anthropic-ai/claude-agent-sdk-linux-arm64-musl": { - "version": "0.3.246", - "resolved": "https://registry.npmjs.org/@anthropic-ai/claude-agent-sdk-linux-arm64-musl/-/claude-agent-sdk-linux-arm64-musl-0.3.246.tgz", - "integrity": "sha512-AeiDXveuA9dVRDouSRiQzL40P3dOuJPfZOlwwsFNbVUN0D+06WtwgmeWYnyRG4dxsM67Pf4lYcRZ0x9pfEZUwQ==", + "version": "0.3.326", + "resolved": "https://registry.npmjs.org/@anthropic-ai/claude-agent-sdk-linux-arm64-musl/-/claude-agent-sdk-linux-arm64-musl-0.3.326.tgz", + "integrity": "sha512-baSsuP2/wuWujRpHjKT4WkJB5lhttqKq9jcTfY2KRtQpylbk8E5PWcct/pmwJfNCKIUoE8CD+JpmoWzsdd5Ajw==", "cpu": [ "arm64" ], @@ -206,9 +206,9 @@ ] }, "node_modules/@anthropic-ai/claude-agent-sdk-linux-x64": { - "version": "0.3.246", - "resolved": "https://registry.npmjs.org/@anthropic-ai/claude-agent-sdk-linux-x64/-/claude-agent-sdk-linux-x64-0.3.246.tgz", - "integrity": "sha512-BIbBlJ6x7Cys6UFvahUfce26H+U+Q4tLWjKRD39AaV7JvRav8/vfdFytzWq6E+h9hT1OOWYuPYx2gMn5vDhZhQ==", + "version": "0.3.326", + "resolved": "https://registry.npmjs.org/@anthropic-ai/claude-agent-sdk-linux-x64/-/claude-agent-sdk-linux-x64-0.3.326.tgz", + "integrity": "sha512-WMhDY07gk2O0E8FXTbrVYmPjSLFaYPDCPwsNueVxm6vZ7ek+lPaKuOMAxTb1K07TzSmTOexoWeX2Y9VrbVDMpw==", "cpu": [ "x64" ], @@ -219,9 +219,9 @@ ] }, "node_modules/@anthropic-ai/claude-agent-sdk-linux-x64-musl": { - "version": "0.3.246", - "resolved": "https://registry.npmjs.org/@anthropic-ai/claude-agent-sdk-linux-x64-musl/-/claude-agent-sdk-linux-x64-musl-0.3.246.tgz", - "integrity": "sha512-+3PrWX3qXgakO3tZImr7keKFry5XIkbHBEaUxgi3/RtPmxGwS+JdVAr0xxzOoC2F6Y66Kn0lUnztRnZHORBc+Q==", + "version": "0.3.326", + "resolved": "https://registry.npmjs.org/@anthropic-ai/claude-agent-sdk-linux-x64-musl/-/claude-agent-sdk-linux-x64-musl-0.3.326.tgz", + "integrity": "sha512-kK9VO3uFqNd4wgeU0Cefluam5AdPu6mvF1nn6aboGy8TA5NoxlWTmA4/AwMhK39zK1S9Q2OjeX0JtAahYlIchw==", "cpu": [ "x64" ], @@ -232,9 +232,9 @@ ] }, "node_modules/@anthropic-ai/claude-agent-sdk-win32-arm64": { - "version": "0.3.246", - "resolved": "https://registry.npmjs.org/@anthropic-ai/claude-agent-sdk-win32-arm64/-/claude-agent-sdk-win32-arm64-0.3.246.tgz", - "integrity": "sha512-JV8b28OJ8d+EMVEVIWlN3aEkT8uSehO3ZN/PJzXFG7Jgn56oVYUew4aY9vK4jao/4E4mdfzvs358w4HcTBUzVw==", + "version": "0.3.326", + "resolved": "https://registry.npmjs.org/@anthropic-ai/claude-agent-sdk-win32-arm64/-/claude-agent-sdk-win32-arm64-0.3.326.tgz", + "integrity": "sha512-S+Ai+HhGRVFfTDmYWRfTrdWF3b4KMsfLSThZCqRJ0WCOKOXrRShMKV1hqqHBljx/wkMDPCpY0V/9S/e6w57eGQ==", "cpu": [ "arm64" ], @@ -245,9 +245,9 @@ ] }, "node_modules/@anthropic-ai/claude-agent-sdk-win32-x64": { - "version": "0.3.246", - "resolved": "https://registry.npmjs.org/@anthropic-ai/claude-agent-sdk-win32-x64/-/claude-agent-sdk-win32-x64-0.3.246.tgz", - "integrity": "sha512-omI65bYGynLpE1Ybijk56K1wBnt0H6WujNwlg1LzIdeHrYmpUxoqncK92xKH5kb9XWUz3byHqArbv7wsT+M/2A==", + "version": "0.3.326", + "resolved": "https://registry.npmjs.org/@anthropic-ai/claude-agent-sdk-win32-x64/-/claude-agent-sdk-win32-x64-0.3.326.tgz", + "integrity": "sha512-Yn1gZXYFuHmjHz7SpHx4TqDcRDsC8TfwWcntJdYjFzMWm+caPAp6QRqr/aTvirjqVA1D+D8Rd29xhuytpE3xTQ==", "cpu": [ "x64" ], @@ -40521,7 +40521,7 @@ "license": "Apache-2.0", "dependencies": { "@agentclientprotocol/sdk": "^0.17.1", - "@anthropic-ai/claude-agent-sdk": "0.3.246", + "@anthropic-ai/claude-agent-sdk": "0.3.326", "@anthropic-ai/sdk": "^0.104.2", "@getpaseo/client": "0.16.0", "@getpaseo/highlight": "0.16.0", diff --git a/packages/server/package.json b/packages/server/package.json index 58adbd86f..486208f8f 100644 --- a/packages/server/package.json +++ b/packages/server/package.json @@ -107,7 +107,7 @@ }, "dependencies": { "@agentclientprotocol/sdk": "^0.17.1", - "@anthropic-ai/claude-agent-sdk": "0.3.246", + "@anthropic-ai/claude-agent-sdk": "0.3.326", "@anthropic-ai/sdk": "^0.104.2", "@getpaseo/client": "0.16.0", "@getpaseo/highlight": "0.16.0", diff --git a/packages/server/src/server/agent/providers/claude/agent.auto-plan-approval.test.ts b/packages/server/src/server/agent/providers/claude/agent.auto-plan-approval.test.ts index 23b1e7905..7f37a50e1 100644 --- a/packages/server/src/server/agent/providers/claude/agent.auto-plan-approval.test.ts +++ b/packages/server/src/server/agent/providers/claude/agent.auto-plan-approval.test.ts @@ -91,7 +91,7 @@ test.each([ } }); -test("bypass allows execution without publishing permission events and follows live mode changes", async () => { +async function createBypassSession() { let requestPermission: CanUseTool | undefined; const query = Object.assign((async function* () {})(), { async setPermissionMode(_mode: PermissionMode) {}, @@ -112,17 +112,25 @@ test("bypass allows execution without publishing permission events and follows l }); const events: AgentStreamEvent[] = []; session.subscribe((event) => events.push(event)); + await session.setMode("bypassPermissions"); + if (!requestPermission) throw new Error("SDK permission callback was not installed"); + return { session, events, requestPermission }; +} + +test("bypass approves Claude's own checks without publishing permission events and follows live mode changes", async () => { + const { session, events, requestPermission } = await createBypassSession(); try { - await session.setMode("bypassPermissions"); - if (!requestPermission) throw new Error("SDK permission callback was not installed"); const controller = new AbortController(); - const input = { command: "echo hello" }; - const result = requestPermission("Bash", input, { + const input = { command: "rm -rf build" }; + // Claude Code sends these fields, but this SDK version does not declare them on CanUseTool. + const ownCheck = { signal: controller.signal, toolUseID: "execution", - decisionReason: "Explicit ask rule", - }); - // Abort also settles the pre-fix callback, so this repro never hangs. + decisionReason: "Dangerous rm command", + decisionReasonType: "safetyCheck", + }; + const result = requestPermission("Bash", input, ownCheck); + // Abort also settles a prompting callback, so this test never hangs. controller.abort(); await expect(result).resolves.toMatchObject({ behavior: "allow", updatedInput: input }); expect(events.filter((event) => event.type.startsWith("permission_"))).toEqual([]); @@ -135,6 +143,7 @@ test("bypass allows execution without publishing permission events and follows l ] as const) { await session.setMode(mode); const resolution = requestPermission(tool, toolInput, { + ...ownCheck, signal: new AbortController().signal, toolUseID: tool, }); @@ -149,3 +158,44 @@ test("bypass allows execution without publishing permission events and follows l await session.close(); } }); + +test.each([ + ["an ask rule", { decisionReasonType: "rule" }], + ["a hook", { decisionReasonType: "hook", decisionReason: "Confirm before posting" }], + [ + "an ask rule on Claude's own check", + { + decisionReasonType: "safetyCheck", + matchedAskRule: { source: "policySettings", toolName: "Bash", ruleContent: "rm *" }, + }, + ], + [ + "a tool the user answers on the card", + { decisionReasonType: "other", requiresUserInteraction: true }, + ], + ["a compound command with an ask rule in one part", { decisionReasonType: "subcommandResults" }], + [ + "a read the user blocked outside the working directories", + { decisionReasonType: "safetyCheck", decisionReasonCode: "outside_reads_blocked" }, + ], + [ + "an organization's ask limit", + { decisionReasonType: "workingDir", decisionReasonCode: "org_ask_ceiling" }, + ], + ["a sandbox override the user's read block raised", { decisionReasonType: "sandboxOverride" }], + ["a Claude Code that does not say why it asks", {}], +])("bypass still asks the user about %s", async (_source, why) => { + const { session, events, requestPermission } = await createBypassSession(); + try { + const ask = { signal: new AbortController().signal, toolUseID: "execution", ...why }; + const resolution = requestPermission("Bash", { command: "gh pr comment 1" }, ask); + const requestEvent = events.at(-1); + expect(requestEvent?.type).toBe("permission_requested"); + if (requestEvent?.type !== "permission_requested") + throw new Error("Missing permission request"); + await session.respondToPermission(requestEvent.request.id, { behavior: "deny" }); + await expect(resolution).resolves.toMatchObject({ behavior: "deny" }); + } finally { + await session.close(); + } +}); diff --git a/packages/server/src/server/agent/providers/claude/agent.ts b/packages/server/src/server/agent/providers/claude/agent.ts index 5c829801d..7bd696e7a 100644 --- a/packages/server/src/server/agent/providers/claude/agent.ts +++ b/packages/server/src/server/agent/providers/claude/agent.ts @@ -11,6 +11,7 @@ import path from "node:path"; import { LRUCache } from "lru-cache"; import stripAnsi from "strip-ansi"; import { createImportDescriptorScheduler } from "./import-descriptor-scheduler.js"; +import { bypassApprovesAsk } from "./bypass-approval.js"; import { type AgentDefinition, type CanUseTool, @@ -5319,10 +5320,8 @@ class ClaudeAgentSession implements AgentSession { input, options, ): Promise => { - // Decide before publishing anything: bypassed tools never enter the permission UI. - // Claude also routes questions and plan review through this callback. - const needsUserAnswer = toolName === "AskUserQuestion" || toolName === "ExitPlanMode"; - if (this.currentMode === "bypassPermissions" && !needsUserAnswer) { + // Decide before publishing anything: bypassed asks never enter the permission UI. + if (this.currentMode === "bypassPermissions" && bypassApprovesAsk(toolName, options)) { return { behavior: "allow", updatedInput: input, toolUseID: options.toolUseID }; } diff --git a/packages/server/src/server/agent/providers/claude/bypass-approval.ts b/packages/server/src/server/agent/providers/claude/bypass-approval.ts new file mode 100644 index 000000000..cd871439e --- /dev/null +++ b/packages/server/src/server/agent/providers/claude/bypass-approval.ts @@ -0,0 +1,43 @@ +import type { CanUseTool } from "@anthropic-ai/claude-agent-sdk"; + +type PermissionOptions = Parameters[2]; + +// In bypassPermissions, Claude Code still forwards an ask when the user configured one +// (a permissions.ask rule, including managed policy, or a PreToolUse hook), when the tool +// needs an answer, or when one of its own checks fires. Bypass approves only Claude's own +// checks. A missing or unknown reason type stays with the user. +// `subcommandResults` is left out: a compound or piped command uses it as a container, +// including when a user's ask rule matched one of its parts. `sandboxOverride` is left +// out: in Bypass, Claude Code forwards it only when the user's read block or hook raised it. +const CLAUDE_OWN_CHECK_TYPES: ReadonlySet = new Set([ + "safetyCheck", + "workingDir", + "mode", + "classifier", + "asyncAgent", +]); + +// Settings the user or organization chose, which Claude Code reports under its own types: +// permissions.blockReadsOutsideWorkingDirectories, and an organization's ask limit. +const CONFIGURED_ASK_CODES: ReadonlySet = new Set([ + "outside_reads_blocked", + "org_ask_ceiling", +]); + +const QUESTION_TOOLS: ReadonlySet = new Set(["AskUserQuestion", "ExitPlanMode"]); + +export function bypassApprovesAsk(toolName: string, options: PermissionOptions): boolean { + if (QUESTION_TOOLS.has(toolName) || options.matchedAskRule) { + return false; + } + // The SDK forwards these from Claude Code without declaring them on CanUseTool. + if ("requiresUserInteraction" in options && options.requiresUserInteraction === true) { + return false; + } + const reasonCode = "decisionReasonCode" in options ? options.decisionReasonCode : undefined; + if (typeof reasonCode === "string" && CONFIGURED_ASK_CODES.has(reasonCode)) { + return false; + } + const reasonType = "decisionReasonType" in options ? options.decisionReasonType : undefined; + return typeof reasonType === "string" && CLAUDE_OWN_CHECK_TYPES.has(reasonType); +}