Open History to see the last commit
1---
2title: Policy
3id: policy
4order: 7
5description: "Set allow/ask/deny guardrails over the commands and capabilities the in-sandbox agent may run, ask about, or never run, in one portable description that each harness maps onto its native permissions."
6---
7
8A policy is your guardrail layer: it decides which commands and capabilities the
9in-sandbox agent may run outright, must ask about first, or can never run.
10`defineSandboxPolicy()` describes those rules once, portably, and each
11[provider](./providers)'s harness adapter maps them onto its own native
12permission system. You attach a policy to a sandbox via
13[`defineSandbox({ policy })`](./providers), where it guards the commands the
14[workspace](./workspace) setup and bridged [tools](./tools) run.
15
16```ts
17import { defineSandboxPolicy, defineSandbox } from '@tanstack/ai-sandbox'
18import { dockerSandbox } from '@tanstack/ai-sandbox-docker'
19
20const policy = defineSandboxPolicy({
21 commands: {
22 allow: ['pnpm test', 'pnpm typecheck', 'git diff'],
23 ask: ['pnpm install', 'curl *'],
24 deny: ['sudo *', 'rm -rf *'],
25 },
26 capabilities: { fileWrite: 'allow', network: 'ask' },
27 default: 'ask',
28})
29
30const sandbox = defineSandbox({
31 id: 'repo-agent',
32 provider: dockerSandbox({ image: 'node:22' }),
33 policy,
34})
35```
36
37## Decisions
38
39Every command or capability resolves to one of three decisions:
40
41| Decision | Meaning |
42| --- | --- |
43| `allow` | The agent runs it without interruption. |
44| `ask` | The agent pauses; the harness emits an approval request the client answers before the action proceeds. |
45| `deny` | The action is blocked outright. The agent cannot run it. |
46
47## Commands
48
49`commands` holds three lists of command patterns: `allow`, `ask` and `deny`.
50A pattern matches against the command the agent is about to run:
51
52```ts
53import { defineSandboxPolicy } from '@tanstack/ai-sandbox'
54
55const policy = defineSandboxPolicy({
56 commands: {
57 allow: ['pnpm test', 'pnpm typecheck', 'git diff'],
58 ask: ['pnpm install', 'curl *'],
59 deny: ['sudo *', 'rm -rf *'],
60 },
61})
62```
63
64### Glob patterns
65
66Patterns support `*` globs, so you can gate a whole family of commands with one
67entry. `curl *` matches any `curl` invocation; `sudo *` matches anything run
68through `sudo`. An exact string like `pnpm test` matches only that command.
69Prefer the named [workspace scripts](./workspace) (`pnpm test`, `pnpm build`)
70in your `allow` list. They give the policy stable names to match rather than
71freeform shell.
72
73## Capabilities
74
75`capabilities` applies the same `allow` / `ask` / `deny` decisions to
76coarse-grained abilities rather than individual commands, for example
77filesystem writes or outbound network access:
78
79```ts
80import { defineSandboxPolicy } from '@tanstack/ai-sandbox'
81
82const policy = defineSandboxPolicy({
83 capabilities: {
84 fileWrite: 'allow', // let the agent edit the working tree freely
85 network: 'ask', // pause for approval before any outbound request
86 },
87})
88```
89
90This is the broad backstop: even if a specific network command isn't in your
91`commands` lists, `network: 'ask'` still forces an approval for anything that
92reaches out.
93
94Some providers also enforce network at create time when `network: 'deny'`. See
95[Providers](./providers) for that mapping.
96
97## Network policy on Docker Sandboxes
98
99Most providers leave `capabilities.network` to the harness. `sbxSandbox()` also enforces it on the host HTTP/HTTPS proxy (`networkPolicy: true`).
100
101`sbx policy` can allow or deny hosts. It has no `ask`. Deny wins over allow, so `network: 'deny'` is an allowlist, not a full deny.
102
103| TanStack `capabilities.network` | What `sbxSandbox()` writes |
104| --- | --- |
105| No policy and no `allowNetwork` / `denyNetwork` | If the policy list is empty, `sbxSandbox()` runs `sbx policy init deny-all`. A known adapter (`grok-build`, `claude-code`, `codex`) then writes its model API host and `localhost` as a per-sandbox allow on top of that deny-all. Unknown adapters stay on the machine preset. |
106| No policy and `allowNetwork` | Per-sandbox allow of the model API host (when the adapter is known), `localhost`, and `allowNetwork`, then apply `denyNetwork`. |
107| No policy and `denyNetwork` only | Per-sandbox deny of those hosts. Allow stays empty. No auto hosts and no `localhost`. This is additive deny on the machine preset. |
108| `allow` | Allow `**`, then apply `denyNetwork`. |
109| `deny` | Allow the model API host, `localhost`, and `allowNetwork`, then apply `denyNetwork`. |
110| `ask` (or the policy `default` when `network` is unset) | Same allowlist as `deny`. The harness still asks for tools and commands. |
111
112Auto-allowed model hosts:
113
114- `grok-build` → `api.x.ai`
115- `claude-code` → `api.anthropic.com`
116- `codex` → `api.openai.com`
117- `opencode` or an unknown adapter → none
118
119If the allowlist would be empty under `deny` or `ask`, create throws. Pass `allowNetwork`, or use `grokBuildText` / `claudeCodeText` / `codexText`.
120
121The guest still dials `host.docker.internal` for the tool bridge. The `sbx` proxy rewrites that host to `localhost` before the policy match. `sbxSandbox()` adds `localhost` when it writes a real allowlist. It does not add `localhost` for `denyNetwork` only.
122
123```ts
124import { sbxSandbox } from '@tanstack/ai-sandbox-docker'
125import { defineSandbox, defineSandboxPolicy } from '@tanstack/ai-sandbox'
126
127const sandbox = defineSandbox({
128 id: 'repo-agent',
129 provider: sbxSandbox({
130 allowNetwork: ['*.npmjs.org', 'registry.npmjs.org'],
131 }),
132 policy: defineSandboxPolicy({
133 capabilities: { network: 'deny' },
134 commands: { allow: ['pnpm test'], deny: ['sudo *'] },
135 }),
136})
137```
138
139Command rules and `fileWrite` stay with the harness. `sbx` does not see command lines.
140
141## Precedence: deny > ask > allow
142
143When more than one rule could match an action, the strictest wins. The order is
144**`deny` > `ask` > `allow`**:
145
146- If any matching rule says `deny`, the action is blocked and no other rule
147 overrides it.
148- Otherwise, if any matching rule says `ask`, the action requires approval.
149- Otherwise, if a rule says `allow`, it runs.
150- If nothing matches, the `default` decision applies.
151
152```ts
153import { defineSandboxPolicy } from '@tanstack/ai-sandbox'
154
155const policy = defineSandboxPolicy({
156 commands: {
157 // `curl` is allowed broadly…
158 allow: ['curl *'],
159 // …but `deny` wins, so this specific host is always blocked.
160 deny: ['curl * internal.example.com*'],
161 },
162 default: 'deny',
163})
164```
165
166This means you can paint with a broad `allow` and carve exceptions out with
167narrower `ask` / `deny` patterns, confident the exceptions take priority.
168
169## The default
170
171`default` is the decision for anything none of your rules match. Set it to the
172posture you want at the edges:
173
174- `default: 'allow'` is permissive: only the things you explicitly `ask` about or
175 `deny` are gated. Reasonable for trusted dev loops.
176- `default: 'ask'` is cautious: unknown actions pause for approval. A good middle
177 ground.
178- `default: 'deny'`, locked down: the agent can only run what you explicitly
179 `allow`. Strongest posture for untrusted or production runs.
180
181When omitted, treat the default as `ask` so unforeseen actions surface rather
182than silently running.
183
184## How `ask` surfaces
185
186An `ask` decision is not a guess the SDK makes, it's a question routed back to
187you. When the agent attempts an `ask`-gated action, the harness pauses it and
188emits an **approval request** into the run stream. Your client answers that
189request (approve or reject), and the harness either lets the action proceed or
190blocks it based on the answer. Until the client responds, the action is held.
191
192This is why `ask` is the right choice for actions that are usually fine but
193occasionally dangerous (`pnpm install` pulling a new dependency, an outbound
194`curl`): the human stays in the loop without you having to enumerate every safe
195command up front.
196
197## How adapters map a policy
198
199A policy is portable. Each harness adapter translates the same
200`allow` / `ask` / `deny` description into its own native permission system:
201
202- A Grok Build harness maps it onto the `grok` CLI's permission flags.
203- A Claude Code harness maps it onto Claude Code's permission rules
204 (allowed/ask/denied tool and command rules).
205- A Codex harness maps it onto Codex's approval and sandbox settings.
206- Other harnesses map it onto whatever native gate they expose.
207
208Where a harness can't express a particular rule, it degrades rather than
209failing the run, the unsupported rule is skipped (with a warning) instead of
210throwing. Because the mapping is the adapter's job, you write the policy once
211and it behaves consistently no matter which provider or harness runs the
212sandbox.
213
214A deny-only list with `default: 'allow'` stays permissive on Grok Build and
215Codex. Those harnesses keep auto-approve. They do not enforce
216`commands.deny`. Isolation is the outer sandbox. Use Claude Code when you
217need command-level deny.
218
219Some providers run as a non-root user, so package installs in `setup` need
220`sudo`. Do not deny `sudo *` on those providers. See [Providers](./providers)
221for the list, and [Workspace](./workspace) for how to write setup commands.
222
223## Wiring it on
224
225A policy does nothing on its own, it takes effect when you attach it to a
226sandbox. Pass it as `policy` on [`defineSandbox`](./providers); from there it
227guards every run that uses that sandbox, including the [workspace](./workspace)
228setup commands and any host [tools](./tools) bridged into the agent.
229
230```ts
231import { defineSandboxPolicy, defineSandbox, defineWorkspace, githubRepo } from '@tanstack/ai-sandbox'
232import { dockerSandbox } from '@tanstack/ai-sandbox-docker'
233
234const policy = defineSandboxPolicy({
235 commands: {
236 allow: ['pnpm test', 'pnpm typecheck', 'git diff'],
237 ask: ['pnpm install', 'curl *'],
238 deny: ['sudo *', 'rm -rf *'],
239 },
240 capabilities: { fileWrite: 'allow', network: 'ask' },
241 default: 'ask',
242})
243
244const sandbox = defineSandbox({
245 id: 'repo-agent',
246 provider: dockerSandbox({ image: 'node:22' }),
247 workspace: defineWorkspace({
248 source: githubRepo({ repo: 'owner/app' }),
249 setup: ['corepack enable', 'pnpm install'],
250 }),
251 policy,
252})
253```
254
255## Next steps
256
257- [Providers](./providers), attach the policy via `defineSandbox`.
258- [Workspace](./workspace), the setup commands and scripts the policy guards.
259- [Tools](./tools), host tools bridged into the agent run under the same policy.
260- [Lifecycle](./lifecycle), how a guarded sandbox resumes and tears down.