TypeScript
feat(server,web,mobile): approvals in full access say why Claude asked (#22546)
Julius Marmingee74924538705
diff --git a/apps/mobile/src/features/threads/PendingApprovalCard.tsx b/apps/mobile/src/features/threads/PendingApprovalCard.tsxindex 3037ee422bd..b5a9d4ed27e 100644--- a/apps/mobile/src/features/threads/PendingApprovalCard.tsx+++ b/apps/mobile/src/features/threads/PendingApprovalCard.tsx@@ -4,7 +4,12 @@ import type {ProviderApprovalOption,RuntimeRequestId,} from "@t3tools/contracts";-import { View } from "react-native";+import {+ FULL_ACCESS_OVERRIDE_QUESTION,+ fullAccessOverrideExplanation,+} from "@t3tools/client-runtime/state/thread-requests";+import { useState } from "react";+import { Pressable, View } from "react-native";import { AppText as Text } from "../../components/ui/AppText";import type { PendingApproval } from "../../lib/threadActivity";@@ -31,6 +36,7 @@ export function PendingApprovalCard(props: PendingApprovalCardProps) {const warning = options.find((option) => option.warning)?.warning;const canRespond = props.approval.responseCapability === "live";const disabled = !canRespond || props.respondingApprovalId === props.approval.requestId;+ const [explanationOpen, setExplanationOpen] = useState(false);return (<View className="gap-2.5 rounded-[20px] border border-border bg-card-alt p-4"><Text className="font-t3-bold text-2xs uppercase tracking-[1.1px] text-foreground-secondary">@@ -44,6 +50,27 @@ export function PendingApprovalCard(props: PendingApprovalCardProps) {{props.approval.detail}</Text>) : null}+ {props.approval.reason ? (+ <Text className="font-sans text-xs leading-normal text-warning-foreground">+ {props.approval.reason}+ </Text>+ ) : null}+ {props.approval.fullAccessOverride ? (+ <Pressable+ accessibilityRole="button"+ accessibilityState={{ expanded: explanationOpen }}+ onPress={() => setExplanationOpen((open) => !open)}+ >+ <Text className="font-t3-bold text-xs text-foreground-secondary underline">+ {FULL_ACCESS_OVERRIDE_QUESTION}+ </Text>+ {explanationOpen ? (+ <Text className="mt-1 font-sans text-xs leading-normal text-foreground-muted">+ {fullAccessOverrideExplanation(props.approval.fullAccessOverride)}+ </Text>+ ) : null}+ </Pressable>+ ) : null}{!canRespond ? (<Text className="font-sans text-sm leading-5 text-foreground-muted">The provider process for this request is no longer available. Interrupt or restart the rundiff --git a/apps/server/src/mcp/OrchestratorMcpService.ts b/apps/server/src/mcp/OrchestratorMcpService.tsindex 78ad1a5d5c9..edaa8777b42 100644--- a/apps/server/src/mcp/OrchestratorMcpService.ts+++ b/apps/server/src/mcp/OrchestratorMcpService.ts@@ -988,7 +988,9 @@ function turnItemText(item: OrchestrationV2TurnItem): string | null {case "web_search":return jsonText({ patterns: item.patterns, results: item.results });case "approval_request":- return item.prompt ?? item.requestKind;+ return [item.prompt ?? item.requestKind, item.reason]+ .filter((line): line is string => line !== undefined)+ .join("\n");case "checkpoint":return jsonText(item.files);case "run_interrupt_request":diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.tsindex aeb128bf6fe..b231fbfca9d 100644--- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts+++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts@@ -588,6 +588,47 @@ describe("ClaudeAdapterV2 context usage", () => {});});+describe("ClaudeAdapterV2 approval presentation", () => {+ it("marks asks that full access could not auto-allow and keeps the CLI's reason", () => {+ const presentation = ClaudeAdapterV2.claudeApprovalPresentation({+ permissionMode: "bypassPermissions",+ prompt: 'Claude wants to run rm -r -- "$rr/$h"',+ callbackOptions: {+ decisionReason: "\u001b[33mDangerous rm on a path built from a variable\u001b[39m",+ suppressAlwaysAllowRule: true,+ },+ });+ assert.deepEqual(presentation, {+ reason: "Dangerous rm on a path built from a variable",+ fullAccessOverride: "provider_check",+ options: [+ { decision: "decline", label: "Decline" },+ { decision: "accept", label: "Approve" },+ ],+ });+ });++ it("attributes a bypass ask to the user's ask rule when one matched", () => {+ const presentation = ClaudeAdapterV2.claudeApprovalPresentation({+ permissionMode: "bypassPermissions",+ prompt: "Claude wants to run git push",+ callbackOptions: {+ matchedAskRule: { source: "userSettings", toolName: "Bash", ruleContent: "git push:*" },+ },+ });+ assert.deepEqual(presentation, { fullAccessOverride: "ask_rule" });+ });++ it("adds nothing to ordinary approvals outside full access", () => {+ const presentation = ClaudeAdapterV2.claudeApprovalPresentation({+ permissionMode: "default",+ prompt: "Run make?",+ callbackOptions: { decisionReason: "Run make?" },+ });+ assert.deepEqual(presentation, {});+ });+});+describe("ClaudeAdapterV2 session permissions", () => {// regression: #15892 an explicit user refusal must stay user_reject, not a tool failureit("keeps explicit user refusals classified as user_reject", () => {diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.tsindex 91e926cc85e..5d3e9a56799 100644--- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts+++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts@@ -51,6 +51,7 @@ import type {} from "@anthropic-ai/claude-agent-sdk/sdk-tools";// @effect-diagnostics-next-line nodeBuiltinImport:off -- the SDK takes a Node ChildProcess for the CLI.import * as NodeChildProcess from "node:child_process";+import * as NodeUtil from "node:util";import { parseCliArgs } from "@t3tools/shared/cliArgs";import { fromLenientJson } from "@t3tools/shared/schemaJson";import { AgentScope } from "@t3tools/shared/AgentScope";@@ -87,6 +88,8 @@ import {type OrchestrationV2TurnItem,type OrchestrationV2WebSearchResult,type ProviderApprovalDecision,+ type ProviderApprovalFullAccessOverride,+ type ProviderApprovalOption,ProviderDriverKind,type ProviderInstanceId,type ProviderRequestKind,@@ -2827,6 +2830,45 @@ function parentToolUseIdFromSdkMessage(message: SDKMessage): string | null {: null;}+const CLAUDE_NO_STANDING_GRANT_OPTIONS = [+ { decision: "decline", label: "Decline" },+ { decision: "accept", label: "Approve" },+] satisfies ReadonlyArray<ProviderApprovalOption>;++export function claudeApprovalPresentation(input: {+ readonly permissionMode: PermissionMode;+ readonly prompt: string;+ readonly callbackOptions: Pick<+ Parameters<CanUseTool>[2],+ "decisionReason" | "matchedAskRule" | "suppressAlwaysAllowRule"+ >;+}): {+ readonly reason?: string;+ readonly fullAccessOverride?: ProviderApprovalFullAccessOverride;+ readonly options?: ReadonlyArray<ProviderApprovalOption>;+} {+ // The reason can carry terminal escapes from the CLI's own prompt renderer.+ const reason = NodeUtil.stripVTControlCharacters(+ input.callbackOptions.decisionReason ?? "",+ ).trim();+ // Bypass auto-allows everything it can, so any ask that still reaches the+ // callback is one bypass refused. The SDK drops the CLI's decision_reason_type,+ // leaving the matched ask rule as the only way to tell the two causes apart.+ const fullAccessOverride: ProviderApprovalFullAccessOverride | undefined =+ input.permissionMode !== "bypassPermissions"+ ? undefined+ : input.callbackOptions.matchedAskRule === undefined+ ? "provider_check"+ : "ask_rule";+ return {+ ...(reason === "" || reason === input.prompt ? {} : { reason }),+ ...(fullAccessOverride === undefined ? {} : { fullAccessOverride }),+ ...(input.callbackOptions.suppressAlwaysAllowRule === true+ ? { options: CLAUDE_NO_STANDING_GRANT_OPTIONS }+ : {}),+ };+}+export function permissionResultFromDecision(input: {readonly toolName: string;readonly decision: ProviderApprovalDecision;@@ -6347,6 +6389,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (readonly nativeRequestId: string;readonly requestKind: OrchestrationV2RuntimeRequest["kind"];readonly prompt?: string;+ readonly presentation?: ReturnType<typeof claudeApprovalPresentation>;readonly questions?: ReadonlyArray<OrchestrationV2UserInputQuestion>;}) {const createdAt = yield* DateTime.now;@@ -6431,6 +6474,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (requestId,requestKind: input.requestKind as ProviderRequestKind,...(input.prompt === undefined ? {} : { prompt: input.prompt }),+ ...input.presentation,}: {type: "user_input_request" as const,@@ -8918,6 +8962,11 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (nativeRequestId,requestKind,prompt,+ presentation: claudeApprovalPresentation({+ permissionMode: context.queryPolicy.permissionMode,+ prompt,+ callbackOptions,+ }),});const decision = yield* Deferred.make<ProviderApprovalDecision, never>();yield* Ref.update(pendingRuntimeRequests, (current) => {diff --git a/apps/web/src/components/chat/ComposerBanner.tsx b/apps/web/src/components/chat/ComposerBanner.tsxindex 123c82cc933..01a93f0c6d3 100644--- a/apps/web/src/components/chat/ComposerBanner.tsx+++ b/apps/web/src/components/chat/ComposerBanner.tsx@@ -160,13 +160,18 @@ function Root({density === "comfortable" && "[--composer-banner-padding-block:--spacing(1.25)]",density === "spacious" && "px-3 [--composer-banner-padding-block:--spacing(3)]",width === "content" ? "w-fit max-w-full flex-none" : "@container",- "[&>[data-composer-banner-row]:first-child>[data-slot=composer-banner-actions]>[data-slot=button]:last-child]:rounded-se-xl",- "[&>[data-composer-banner-row]:first-child>[data-slot=composer-banner-actions]>:last-child>[data-slot=button]:last-of-type]:rounded-se-xl",- "[&>:first-child>[data-composer-banner-row]:first-child>[data-slot=composer-banner-actions]>[data-slot=button]:last-child]:rounded-se-xl",- placement === "floating" && [- "[&>[data-composer-banner-row]:only-child>[data-slot=composer-banner-actions]>[data-slot=button]:last-child]:rounded-ee-xl",- "[&>[data-composer-banner-row]:only-child>[data-slot=composer-banner-actions]>:last-child>[data-slot=button]:last-of-type]:rounded-ee-xl",+ // A corner button nests inside the banner's radius only when the padding+ // is tight; spacious padding already leaves the default radius concentric.+ density !== "spacious" && [+ "[&>[data-composer-banner-row]:first-child>[data-slot=composer-banner-actions]>[data-slot=button]:last-child]:rounded-se-xl",+ "[&>[data-composer-banner-row]:first-child>[data-slot=composer-banner-actions]>:last-child>[data-slot=button]:last-of-type]:rounded-se-xl",+ "[&>:first-child>[data-composer-banner-row]:first-child>[data-slot=composer-banner-actions]>[data-slot=button]:last-child]:rounded-se-xl",],+ placement === "floating" &&+ density !== "spacious" && [+ "[&>[data-composer-banner-row]:only-child>[data-slot=composer-banner-actions]>[data-slot=button]:last-child]:rounded-ee-xl",+ "[&>[data-composer-banner-row]:only-child>[data-slot=composer-banner-actions]>:last-child>[data-slot=button]:last-of-type]:rounded-ee-xl",+ ],className,)}data-slot="composer-banner"diff --git a/apps/web/src/components/chat/ComposerPendingApprovalPanel.tsx b/apps/web/src/components/chat/ComposerPendingApprovalPanel.tsxindex ccb2eeabe62..9d700e222a4 100644--- a/apps/web/src/components/chat/ComposerPendingApprovalPanel.tsx+++ b/apps/web/src/components/chat/ComposerPendingApprovalPanel.tsx@@ -1,5 +1,12 @@+import {+ FULL_ACCESS_OVERRIDE_QUESTION,+ fullAccessOverrideExplanation,+} from "@t3tools/client-runtime/state/thread-requests";import { memo } from "react";import { type PendingApproval } from "../../session-logic";+import { InlineButton } from "../ui/button";+import { Popover, PopoverPopup, PopoverTrigger } from "../ui/popover";+import { composerFloatingLayerProps } from "./composerEventScope";import { cn } from "~/lib/utils";interface ComposerPendingApprovalPanelProps {@@ -44,6 +51,22 @@ export const ComposerPendingApprovalPanel = memo(function ComposerPendingApprova<span className="flex w-full min-w-0 items-center gap-2 text-2xs text-muted-foreground"><span className="shrink-0 font-medium text-warning">{fallbackLabel}</span>{approval.appName ? <span className="min-w-0 truncate">{approval.appName}</span> : null}+ {approval.fullAccessOverride ? (+ <Popover>+ <PopoverTrigger openOnHover render={<InlineButton tone="muted" />}>+ {FULL_ACCESS_OVERRIDE_QUESTION}+ </PopoverTrigger>+ <PopoverPopup+ {...composerFloatingLayerProps}+ aria-label={FULL_ACCESS_OVERRIDE_QUESTION}+ tooltipStyle+ side="top"+ className="max-w-[min(22rem,calc(100vw-2rem))] whitespace-normal"+ >+ {fullAccessOverrideExplanation(approval.fullAccessOverride)}+ </PopoverPopup>+ </Popover>+ ) : null}{pendingCount > 1 ? (<span className="ml-auto shrink-0 tabular-nums">1/{pendingCount}</span>) : null}@@ -63,6 +86,11 @@ export const ComposerPendingApprovalPanel = memo(function ComposerPendingApprova? "Provider process is gone — interrupt or restart the run to respond.": approval.detail || fallbackLabel}</Detail>+ {approval.reason ? (+ <span className="w-full min-w-0 text-2xs text-muted-foreground wrap-break-word">+ {approval.reason}+ </span>+ ) : null}</span>);});diff --git a/packages/client-runtime/src/state/threadRequests.ts b/packages/client-runtime/src/state/threadRequests.tsindex 3735869edb6..d3fe7344496 100644--- a/packages/client-runtime/src/state/threadRequests.ts+++ b/packages/client-runtime/src/state/threadRequests.ts@@ -2,6 +2,7 @@ import type {OrchestrationV2ThreadProjection,OrchestrationV2RuntimeRequest,OrchestrationV2UserInputQuestion,+ ProviderApprovalFullAccessOverride,ProviderApprovalOption,ProviderApprovalDecision,ProviderRequestKind,@@ -33,11 +34,23 @@ export interface ThreadPendingApproval {readonly requestKind: ProviderRequestKind;readonly createdAt: string;readonly detail?: string;+ readonly reason?: string;+ readonly fullAccessOverride?: ProviderApprovalFullAccessOverride;readonly appName?: string;readonly options?: ReadonlyArray<ProviderApprovalOption>;readonly responseCapability: "live" | "not_resumable";}+export const FULL_ACCESS_OVERRIDE_QUESTION = "Why am I being asked this?";++export function fullAccessOverrideExplanation(+ override: ProviderApprovalFullAccessOverride,+): string {+ return override === "ask_rule"+ ? "Full access skips approvals, but an ask rule in the agent's own settings matches this action, so it still asks. Remove or narrow that rule to stop these prompts."+ : "Full access skips approvals, but the agent still stops for a few safety checks of its own that no setting can skip, such as a delete whose path it can't verify. T3 Code can't turn these off, so check the request and approve it if it's what you expect.";+}+export interface ThreadUserInputQuestion extends Omit<OrchestrationV2UserInputQuestion,"multiSelect"@@ -119,6 +132,10 @@ export function derivePendingThreadRequests(requestKind: request.kind,createdAt: DateTime.formatIso(request.createdAt),...(item?.type === "approval_request" && item.prompt ? { detail: item.prompt } : {}),+ ...(item?.type === "approval_request" && item.reason ? { reason: item.reason } : {}),+ ...(item?.type === "approval_request" && item.fullAccessOverride+ ? { fullAccessOverride: item.fullAccessOverride }+ : {}),...(item?.type === "approval_request" && item.appName ? { appName: item.appName } : {}),...(item?.type === "approval_request" && item.options !== undefined? { options: item.options }diff --git a/packages/contracts/src/orchestrationV2.ts b/packages/contracts/src/orchestrationV2.tsindex 42483d96fee..fc211c946ec 100644--- a/packages/contracts/src/orchestrationV2.ts+++ b/packages/contracts/src/orchestrationV2.ts@@ -55,6 +55,7 @@ import {} from "./threadPullRequest.ts";import {ProviderApprovalDecision,+ ProviderApprovalFullAccessOverride,ProviderApprovalOption,ProviderInteractionMode,ProviderRequestKind,@@ -1429,6 +1430,8 @@ export const OrchestrationV2TurnItem = Schema.Union([requestId: RuntimeRequestId,requestKind: ProviderRequestKind,prompt: Schema.optional(Schema.String),+ reason: Schema.optional(Schema.String),+ fullAccessOverride: Schema.optional(ProviderApprovalFullAccessOverride),appName: Schema.optional(Schema.String),options: Schema.optional(Schema.Array(ProviderApprovalOption)),}),@@ -2189,6 +2192,8 @@ export const OrchestrationV2TurnItemJson = Schema.Union([requestId: RuntimeRequestId,requestKind: ProviderRequestKind,prompt: Schema.optional(Schema.String),+ reason: Schema.optional(Schema.String),+ fullAccessOverride: Schema.optional(ProviderApprovalFullAccessOverride),appName: Schema.optional(Schema.String),options: Schema.optional(Schema.Array(ProviderApprovalOption)),}),diff --git a/packages/contracts/src/providerPolicy.ts b/packages/contracts/src/providerPolicy.tsindex 52e54bbf108..e10a007e102 100644--- a/packages/contracts/src/providerPolicy.ts+++ b/packages/contracts/src/providerPolicy.ts@@ -56,6 +56,11 @@ export const ProviderApprovalDecision = Schema.Literals([]);export type ProviderApprovalDecision = typeof ProviderApprovalDecision.Type;+// Why an approval reached the user although the thread runs with full access:+// the user's own ask rule, or a check the provider enforces even when bypassed.+export const ProviderApprovalFullAccessOverride = Schema.Literals(["ask_rule", "provider_check"]);+export type ProviderApprovalFullAccessOverride = typeof ProviderApprovalFullAccessOverride.Type;+export const ProviderApprovalOption = Schema.Struct({decision: ProviderApprovalDecision,label: TrimmedNonEmptyString,