diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts index c07ab96746eb5..6998e4aa31861 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts @@ -948,6 +948,102 @@ describe("ClaudeAdapterV2 context usage", () => { }); }); +describe("ClaudeAdapterV2 full-access command approval", () => { + const policy = (input: { + readonly runtimeMode?: "full-access" | "approval-required"; + readonly interactionMode?: "default" | "plan"; + readonly approvalPolicy?: "on-request" | "never"; + readonly sandboxPolicy?: { + readonly type: "readOnly" | "dangerFullAccess"; + readonly access?: { readonly type: "fullAccess" }; + readonly networkAccess?: false; + }; + }) => + ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({ + runtimeMode: input.runtimeMode ?? "full-access", + interactionMode: input.interactionMode ?? "default", + cwd: "/workspace", + ...(input.approvalPolicy === undefined ? {} : { approvalPolicy: input.approvalPolicy }), + ...(input.sandboxPolicy === undefined ? {} : { sandboxPolicy: input.sandboxPolicy }), + }); + + it("runs a full-access command instead of waiting, and still denies removal and permission-rule checks", () => { + assert.equal( + ClaudeAdapterV2.claudeCommandApprovalDisposition({ + runtimePolicy: policy({}), + permissionMode: "auto", + toolName: "Bash", + safetyReason: "", + }), + "allow", + ); + assert.equal( + ClaudeAdapterV2.claudeCommandApprovalDisposition({ + runtimePolicy: policy({}), + permissionMode: "auto", + toolName: "Bash", + safetyReason: "Dangerous rm on a path built from a variable", + }), + "deny", + ); + assert.equal( + ClaudeAdapterV2.claudeCommandApprovalDisposition({ + runtimePolicy: policy({}), + permissionMode: "bypassPermissions", + toolName: "Bash", + safetyReason: "A permission rule denies a command in this shell -c script", + }), + "deny", + ); + assert.equal( + ClaudeAdapterV2.claudeCommandApprovalDisposition({ + runtimePolicy: policy({ runtimeMode: "approval-required" }), + permissionMode: "default", + toolName: "Bash", + safetyReason: "", + }), + "ask", + ); + assert.equal( + ClaudeAdapterV2.claudeCommandApprovalDisposition({ + runtimePolicy: policy({ interactionMode: "plan" }), + permissionMode: "plan", + toolName: "Bash", + safetyReason: "", + }), + "ask", + ); + assert.equal( + ClaudeAdapterV2.claudeCommandApprovalDisposition({ + runtimePolicy: policy({ + approvalPolicy: "on-request", + sandboxPolicy: { + type: "readOnly", + access: { type: "fullAccess" }, + networkAccess: false, + }, + }), + permissionMode: "default", + toolName: "Bash", + safetyReason: "", + }), + "ask", + ); + assert.equal( + ClaudeAdapterV2.claudeCommandApprovalDisposition({ + runtimePolicy: policy({ + approvalPolicy: "never", + sandboxPolicy: { type: "readOnly", access: { type: "fullAccess" }, networkAccess: false }, + }), + permissionMode: "dontAsk", + toolName: "Bash", + safetyReason: "", + }), + "ask", + ); + }); +}); + describe("ClaudeAdapterV2 approval presentation", () => { it("marks asks that full access could not auto-allow and keeps the CLI's reason", () => { const presentation = ClaudeAdapterV2.claudeApprovalPresentation({ @@ -2016,25 +2112,38 @@ describe("ClaudeAdapterV2 resume compaction", () => { Effect.map((event) => ({ type: "approval", event }) as const), ), ); - assert.equal(safety.type, askRule ? "approval" : "decision"); + assert.equal(safety.type, "decision"); if (safety.type === "decision") { assert.equal(safety.result?.behavior, "deny"); if (safety.result?.behavior === "deny") { assert.include(safety.result.message, reason); assert.notInclude(safety.result.message, "\u001b["); } - } else if ( - Option.isSome(safety.event) && - safety.event.value.type === "runtime_request.updated" - ) { - yield* runtime.respondToRuntimeRequest({ - requestId: safety.event.value.runtimeRequest.id, - decision: "decline", - }); - assert.equal((yield* Fiber.join(safetyDecision))?.behavior, "deny"); } yield* Fiber.interrupt(safetyRequest); } + const mergeSignal = yield* Effect.abortSignal; + const mergeDecision = yield* Effect.promise(() => + canUseTool!( + "Bash", + { + command: + "gh pr merge 1968 -R nullStack65/Closura --squash --match-head-commit b2fdb044ce56f30784c10f4a2daf59c51f8a0346", + }, + { + signal: mergeSignal, + toolUseID: "tool-merge-ask-rule", + requestId: "request-merge-ask-rule", + matchedAskRule: { + source: "projectSettings" as const, + toolName: "Bash", + ruleContent: "gh pr merge *", + }, + }, + ), + ); + assert.equal(mergeDecision?.behavior, "allow"); + const longQuestion = `Choose a deployment target: ${"region ".repeat(80)}`; const questionRequestEvent = yield* runtime.events.pipe( Stream.filter((event) => event.type === "runtime_request.updated"), diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index 25d9d616cb939..8da1e2f181440 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -3490,8 +3490,7 @@ function claudeBypassAvailability( } function requiresClaudeApproval(context: ActiveClaudeTurnContext): boolean { - // CLI-forwarded requests need a person's decision even under bypass (ask rules, - // safety checks, or settings fallback). Do not wait for queued init/status updates. + // Bypass still reaches the callback so a removal check or deny rule can fail closed. const policy = context.queryPolicy; return policy.installPermissionCallback || policy.permissionMode === "bypassPermissions"; } @@ -4280,6 +4279,26 @@ const CLAUDE_NO_STANDING_GRANT_OPTIONS = [ { decision: "accept", label: "Approve" }, ] satisfies ReadonlyArray; +export function claudeCommandApprovalDisposition(input: { + readonly runtimePolicy: ProviderAdapter.ProviderAdapterV2RuntimePolicy; + readonly permissionMode: PermissionMode; + readonly toolName: string; + readonly safetyReason: string; +}): "allow" | "deny" | "ask" { + const safetyDenial = + input.toolName === "Bash" && + (/^Dangerous rm(?:dir)?\b/i.test(input.safetyReason) || + input.safetyReason === "This shell -c script runs rm and could not be checked" || + input.safetyReason === "A permission rule denies a command in this shell -c script"); + const fullAccess = + permissionModeForClaudeRuntimePolicy(input.runtimePolicy) === "bypassPermissions"; + if (safetyDenial && (fullAccess || input.permissionMode === "bypassPermissions")) { + return "deny"; + } + if (fullAccess) return "allow"; + return "ask"; +} + export function claudeApprovalPresentation(input: { readonly permissionMode: PermissionMode; readonly prompt: string; @@ -12234,17 +12253,29 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( callbackOptions.decisionReason ?? "", ).trim(); // The SDK omits the CLI's dangerousRemoval discriminator, so match its removal warnings. - if ( - toolName === "Bash" && - activePermissionMode === "bypassPermissions" && - callbackOptions.matchedAskRule === undefined && - (/^Dangerous rm(?:dir)?\b/i.test(safetyReason) || - safetyReason === "This shell -c script runs rm and could not be checked" || - safetyReason === "A permission rule denies a command in this shell -c script") - ) { + const disposition = claudeCommandApprovalDisposition({ + runtimePolicy: context.input.runtimePolicy, + permissionMode: activePermissionMode, + toolName, + safetyReason, + }); + if (disposition === "deny") { + const removal = + /^Dangerous rm(?:dir)?\b/i.test(safetyReason) || + safetyReason === "This shell -c script runs rm and could not be checked"; + const message = removal + ? `Claude Code's removal safety check denied this command; the user did not reject it. The command did not run. Do not bypass the check by splitting the removal, putting it in a script, or using another tool or shell. If the warning below supplies a safe rewrite, use it and let the same check evaluate it. Otherwise continue the rest of the task without the removal and tell the user what you needed to delete and why. Warning: ${safetyReason}` + : `A permission rule denied this command before it ran. Nobody is waiting to approve it. Do not bypass the rule by splitting the command, putting it in a script, or using another tool. Warning: ${safetyReason}`; return { behavior: "deny", - message: `Claude Code's removal safety check denied this command; the user did not reject it. The command did not run. Do not bypass the check by splitting the removal, putting it in a script, or using another tool or shell. If the warning below supplies a safe rewrite, use it and let the same check evaluate it. Otherwise continue the rest of the task without the removal and tell the user what you needed to delete and why. Warning: ${safetyReason}`, + message, + toolUseID: callbackOptions.toolUseID, + } satisfies PermissionResult; + } + if (disposition === "allow") { + return { + behavior: "allow", + updatedInput: toolInput, toolUseID: callbackOptions.toolUseID, } satisfies PermissionResult; } @@ -13215,7 +13246,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( } const nativeItemId = `bypass-policy-downgrade:${context.providerTurnId}`; const notice = - "T3 could not confirm Claude bypass permissions here, so Full access is running with approval checks."; + "Claude settings disable bypass permissions here. Full access will not wait for approval. A deny rule or Claude's removal check still fails the command immediately."; yield* emitProviderEvent({ type: "turn_item.updated", driver: CLAUDE_PROVIDER,