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

overview.md

150 lines6.8 KB

1---
2id: overview
3order: 1
4title: Sandboxes Overview
5description: "Run coding-agent CLIs (Grok Build, Claude Code, Codex, OpenCode) inside an isolated sandbox with a real filesystem and a cloned repo, and stream their work back through chat()."
6---
7​
8A **sandbox** gives a coding agent a real computer to work in: a filesystem, a
9shell, processes, and a cloned repository. You point a **harness adapter** (a
10coding-agent CLI like Grok Build) at it through `chat()`, and the agent's work (edits,
11commands, tool calls) streams back to you like any other chat run.
12​
13The same code runs on your laptop, in CI, in a Docker container, or on the edge.
14Only the **provider** changes.
15​
16```ts
17import { chat } from '@tanstack/ai'
18import { grokBuildText } from '@tanstack/ai-grok-build'
19import {
20 createSecrets,
21 defineSandbox,
22 defineWorkspace,
23 githubRepo,
24 withSandbox,
25} from '@tanstack/ai-sandbox'
26import { dockerSandbox } from '@tanstack/ai-sandbox-docker'
27import { messages, threadId } from './chat-context'
28​
29const repoSandbox = defineSandbox({
30 id: 'repo-agent',
31 provider: dockerSandbox({ image: 'node:22' }),
32 workspace: defineWorkspace({
33 source: githubRepo({ repo: 'TanStack/ai' }),
34 packageManager: 'pnpm',
35 setup: ['corepack enable', 'pnpm install'],
36 scripts: { test: 'pnpm test', typecheck: 'pnpm test:types' },
37 secrets: createSecrets({
38 XAI_API_KEY: process.env.XAI_API_KEY ?? '',
39 }),
40 }),
41 lifecycle: { reuse: 'thread', snapshot: 'after-setup', keepAlive: '30m' },
42})
43​
44chat({
45 threadId,
46 adapter: grokBuildText('grok-build'),
47 messages,
48 middleware: [withSandbox(repoSandbox)],
49})
50```
51​
52`sbxSandbox()` from the same package runs the agent in a Docker Sandboxes microVM. See [Providers](./providers).
53​
54## The three moving parts
55​
56A sandboxed run is the composition of three independent pieces. You can change
57any one without touching the others.
58​
59| Part | What it is | You pick it with |
60| --- | --- | --- |
61| **Provider** | *Where* the agent runs (your host, a container, a microVM, a cloud VM). | A provider package (`dockerSandbox`, `sbxSandbox`, `localProcessSandbox`, …) |
62| **Workspace** | *What the agent sees*: the source repo, package manager, setup commands, secrets. | `defineWorkspace({ … })` |
63| **Harness adapter** | *Which agent runs* and how its output is translated to chat chunks. | `grokBuildText`, `claudeCodeText`, `codexText`, `opencodeText`, or `acpCompatible` for [any ACP agent](./harnesses) |
64​
65`defineSandbox()` binds a provider + workspace (+ optional policy, lifecycle, and
66hooks) into a reusable definition. `withSandbox(definition)` is the `chat()`
67middleware that turns it on for a run.
68​
69The provider is where the agent runs, not how it signs in. The default
70`authMode` is `'api-key'`. Set `'host'` when the machine already has a CLI
71login. See [Harness Auth](./auth).
72​
73### How a run executes
74​
75```txt
76chat({ adapter: grokBuildText(), middleware: [withSandbox(repoSandbox)] })
77 │
78 ├─ withSandbox.setup → ensure the sandbox: resume → restore snapshot → create + bootstrap
79 ├─ adapter.chatStream → spawn `grok` INSIDE the sandbox; stream its events back as AG-UI chunks
80 └─ withSandbox.onFinish → snapshot / destroy per the lifecycle
81```
82​
83A harness adapter declares `requires: [SandboxCapability]`, so `chat()` fails
84fast at the call site if no middleware provides a sandbox, you can't
85accidentally run a coding agent with nowhere to run it.
86​
87## When to use a sandbox
88​
89Reach for a sandbox whenever you want an agent to **act on a real codebase**,
90not just talk about one. A few shapes this takes:
91​
92- **CI issue triage / bug-fix bots.** On a new issue, clone the repo into a
93 sandbox, let the agent reproduce and root-cause it, and post the findings (or
94 a draft fix) back.
95- **PR review automation.** Check out a branch, run the test/lint scripts, and
96 have the agent comment on what it found.
97- **Build-and-preview.** Ask the agent to scaffold or modify an app, run the dev
98 server inside the sandbox, and hand the user a live preview URL, see the
99 [Cloudflare guide](./cloudflare) and the `examples/sandbox-*-web` apps.
100- **Eval / benchmark harnesses.** Run a coding agent against a fixture repo with
101 a known bug and assert on the resulting diff, reproducibly, in isolation.
102- **Interactive coding copilots** that need to actually execute code, edit
103 files, and run commands rather than only suggest them.
104​
105If you only need the model to read code you already have in memory, you don't
106need a sandbox, a normal `chat()` with [tools](../tools/server-tools) is
107enough. The sandbox earns its keep the moment the agent needs a filesystem and a
108shell.
109​
110## Where to go next
111​
112[Quick Start](./quick-start) gets an agent fixing a bug in a sandbox on your laptop.
113After that, pick the piece you need:
114​
115- [Providers](./providers): local process, Docker container, Docker Sandboxes
116 (`sbxSandbox`), Daytona, Vercel, Sprites, and what each one can do.
117- [Harnesses](./harnesses): which agent runs. Grok Build, Claude Code, Codex,
118 OpenCode, or any ACP agent.
119- [Harness Auth](./auth): host CLI login or an API key. The sandbox type does
120 not pick this.
121- [Workspace](./workspace): the source repo, clone depth, and setup commands.
122- [Tools](./tools): bridge your app's own tools into the in-sandbox agent.
123- [Policy](./policy): allow, ask or deny guardrails on what the agent may run.
124- [Lifecycle & Snapshots](./lifecycle): reuse a sandbox, snapshot after setup, resume.
125- [Portable Snapshots](./portable-snapshots): keep completed files after the
126 sandbox is gone. Start with [Keep Files After Reload](./portable-snapshots-configure).
127 To keep only some files, see [Pick Which Files to Keep](./portable-snapshots-files).
128- [Instance Durability](./durability): reuse it across replicas too.
129- [Durable Runs](./durable-runs): let a run outlive the tab, and turn it on.
130- [Events](./events): stream the agent's edits and tool calls to a UI, and choose what
131 to store.
132​
133The Advanced group in the sidebar has the rest: the journal file, takeover from
134another host, the reaping sweeper, provisioning, observability, Cloudflare, and how to
135build an adapter.
136​
137## Try it
138​
139Three runnable demos:
140​
141- [`examples/sandbox-web`](https://github.com/TanStack/ai/tree/main/examples/sandbox-web):
142 a "build me an app" agent on Docker with durable runs wired. It scaffolds an app,
143 runs the dev server, and hands back a live preview URL. The run survives a refresh
144 and a closed tab, and Stop is a real cancel.
145- [`examples/sandbox-cloudflare`](https://github.com/TanStack/ai/tree/main/examples/sandbox-cloudflare):
146 the same idea at the edge, with the harness picked per run from the UI.
147- [`examples/ts-react-chat`](https://github.com/TanStack/ai/tree/main/examples/ts-react-chat)
148 at `/repo-report`: clone `TanStack/ai`, pick the harness, pick Auth
149 (`host` or `api-key`), and read a typed report from `useChat().final`. See
150 [Harness Agents](../structured-outputs/harnesses).