Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

policy.ts

111 lines3.6 KB

1/**
2 * Sandbox policy — a portable, harness-agnostic description of what the agent
3 * may do. Each harness adapter MAPS this onto its native permission system
4 * (Claude Code → canUseTool + allowedTools/disallowedTools/permissionMode).
5 *
6 * Command rules are matched as glob/prefix patterns against the command line.
7 * Precedence is deny > ask > allow; unmatched commands fall to `default`.
8 * `'ask'` surfaces the existing resume-based `approval-requested` flow.
9 */
10​
11export type PolicyDecision = 'allow' | 'ask' | 'deny'
12​
13export interface CommandRules {
14 /** Glob/prefix patterns to allow outright (e.g. 'pnpm *', 'git diff'). */
15 allow?: Array<string>
16 /** Patterns that require approval before running. */
17 ask?: Array<string>
18 /** Patterns to refuse (e.g. 'sudo *', 'rm -rf *'). */
19 deny?: Array<string>
21​
22/** Coarse, non-command capability gates for tools like Write/Edit and network. */
23export interface CapabilityRules {
24 /** File-modifying tools (Write/Edit). Defaults to the policy `default`. */
25 fileWrite?: PolicyDecision
26 /** Outbound network access. Defaults to the policy `default`. */
27 network?: PolicyDecision
29​
30export interface SandboxPolicy {
31 commands?: CommandRules
32 capabilities?: CapabilityRules
33 /** Decision for anything not matched by a rule. Defaults to `'ask'`. */
34 default?: PolicyDecision
36​
37export function defineSandboxPolicy(policy: SandboxPolicy): SandboxPolicy {
38 return policy
40​
41/** Convert a glob/prefix pattern to a RegExp anchored to the full command. */
42function patternToRegExp(pattern: string): RegExp {
43 // Escape regex metacharacters except '*', then turn '*' into '.*'.
44 const escaped = pattern
45 .replace(/[.+?^${}()|[\]\\]/g, '\\$&')
46 .replace(/\*/g, '.*')
47 return new RegExp(`^${escaped}$`)
49​
50/**
51 * All equivalent forms of a command line when workspace scripts are defined:
52 * the literal command, its expanded script value, and any script name that
53 * expands to the same value.
54 */
55export function commandAliases(
56 command: string,
57 scripts: Record<string, string> | undefined,
58): Array<string> {
59 const trimmed = command.trim()
60 const aliases = new Set<string>([trimmed])
61 if (scripts === undefined) return [...aliases]
62​
63 const expanded = scripts[trimmed]
64 if (expanded !== undefined) aliases.add(expanded)
65​
66 for (const [name, value] of Object.entries(scripts)) {
67 if (value === trimmed) aliases.add(name)
68 }
69 return [...aliases]
71​
72function patternMatchesCommand(
73 pattern: string,
74 command: string,
75 scripts: Record<string, string> | undefined,
76): boolean {
77 const commandForms = commandAliases(command, scripts)
78 for (const patternForm of commandAliases(pattern, scripts)) {
79 const re = patternToRegExp(patternForm)
80 if (commandForms.some((form) => re.test(form))) return true
81 }
82 return false
84​
85/**
86 * Resolve a command line against the policy. Precedence: deny > ask > allow,
87 * then `default` (defaults to `'ask'`). Exported for adapter permission
88 * mappers and unit tests.
89 *
90 * When `scripts` is provided, policy patterns may match either a script name
91 * or its expanded command value (and vice versa for the command under test).
92 */
93export function evaluateCommand(
94 command: string,
95 policy: SandboxPolicy | undefined,
96 scripts?: Record<string, string>,
97): PolicyDecision {
98 const fallback = policy?.default ?? 'ask'
99 const rules = policy?.commands
100 if (!rules) return fallback
101​
102 const matches = (patterns: Array<string> | undefined): boolean =>
103 (patterns ?? []).some((pattern) =>
104 patternMatchesCommand(pattern, command, scripts),
105 )
106​
107 if (matches(rules.deny)) return 'deny'
108 if (matches(rules.ask)) return 'ask'
109 if (matches(rules.allow)) return 'allow'
110 return fallback