Open History to see the last commit
1---
2title: Workspace
3id: workspace
4order: 4
5description: "Describe what the agent sees inside the sandbox: the source repo, the package manager, the setup commands, and named scripts, in one portable definition."
6---
7
8The workspace clones your repo and installs dependencies before the agent starts. The same setup runs on each provider and each agent.
9
10`defineWorkspace()` describes the working tree once. Each harness adapter converts it to its own format, and it runs in any [provider](./providers).
11
12## Define a workspace
13
14Pass a workspace to `defineSandbox`:
15
16```ts
17import {
18 createSecrets,
19 defineSandbox,
20 defineWorkspace,
21 githubRepo,
22} from '@tanstack/ai-sandbox'
23import { dockerSandbox } from '@tanstack/ai-sandbox-docker'
24
25export const sandbox = defineSandbox({
26 id: 'repo-agent',
27 provider: dockerSandbox({ image: 'node:22' }),
28 workspace: defineWorkspace({
29 // Where the code comes from.
30 source: githubRepo({ repo: 'owner/repo', ref: 'main' }),
31 // Detected from the lockfile when you omit it.
32 packageManager: 'pnpm',
33 // Runs once, during bootstrap.
34 setup: ['corepack enable', 'pnpm install'],
35 // Named commands the agent can run.
36 scripts: { test: 'pnpm test', build: 'pnpm build' },
37 // Put into the sandbox env at create and resume. Never saved.
38 secrets: createSecrets({ XAI_API_KEY: process.env.XAI_API_KEY ?? '' }),
39 }),
40})
41```
42
43| Field | What it sets |
44| --- | --- |
45| `source` | Where the code comes from: a git repo, a local path, or nothing. |
46| `packageManager` | `npm`, `pnpm`, `yarn`, `bun`, or `auto`. The default is `auto`. |
47| `setup` | Commands that run once during bootstrap. |
48| `scripts` | Named commands that the agent and the user can run by name. |
49| `secrets` | Typed secret refs injected into the env. See [Provisioning](./provisioning). |
50
51`defineWorkspace()` also takes `skills`, `plugins`, and `instructions`. Those are on [Provisioning](./provisioning).
52
53## Source
54
55`source` has five shapes:
56
57```ts
58import { defineWorkspace, githubRepo, gitSource } from '@tanstack/ai-sandbox'
59
60// A GitHub repo (owner/repo or a full URL).
61defineWorkspace({ source: githubRepo({ repo: 'owner/repo', ref: 'main' }) })
62
63// Any git URL.
64defineWorkspace({ source: gitSource({ url: 'https://git.example.com/owner/repo.git' }) })
65
66// The same as gitSource, as a plain object.
67defineWorkspace({ source: { type: 'git', url: 'https://github.com/owner/repo', ref: 'main' } })
68
69// A directory on the host (for example, the local-process dev loop).
70defineWorkspace({ source: { type: 'local', path: '/abs/path/to/repo' } })
71
72// No code: the agent starts in an empty workspace.
73defineWorkspace({ source: { type: 'none' } })
74```
75
76`githubRepo` is a short form of `gitSource`:
77
78- A short `owner/repo` becomes `https://github.com/owner/repo.git`.
79- A full URL stays as it is.
80
81Both make a `{ type: 'git' }` source, so you can also write that object yourself.
82
83### Shallow clone by default
84
85`githubRepo` and `gitSource` make a shallow single-branch clone (`--depth 1 --single-branch`), so cold starts stay fast. Pass `depth` for more history:
86
87```ts
88import { defineWorkspace, githubRepo } from '@tanstack/ai-sandbox'
89
90// Shallow clone (depth 1). This is the default.
91defineWorkspace({ source: githubRepo({ repo: 'owner/app' }) })
92
93// The last 10 commits.
94defineWorkspace({ source: githubRepo({ repo: 'owner/app', depth: 10 }) })
95
96// Full history. No depth flag.
97defineWorkspace({ source: githubRepo({ repo: 'owner/app', depth: 'full' }) })
98```
99
100## Package manager
101
102`packageManager` is `'npm'`, `'pnpm'`, `'yarn'`, `'bun'`, or `'auto'`. The default `'auto'` reads the lockfile after the source lands. To pin the choice, set a value.
103
104## Setup
105
106`setup` turns a fresh clone into an installed repo. It runs once, during bootstrap. The simplest form is an array. Each step runs after the one before it:
107
108```ts
109import { defineWorkspace, githubRepo } from '@tanstack/ai-sandbox'
110
111defineWorkspace({
112 source: githubRepo({ repo: 'owner/app' }),
113 setup: ['corepack enable', 'pnpm install'],
114})
115```
116
117To run some steps at the same time, pass a callback:
118
119```ts
120import { defineWorkspace, githubRepo } from '@tanstack/ai-sandbox'
121
122defineWorkspace({
123 source: githubRepo({ repo: 'owner/app' }),
124 setup: ({ serial, parallel }) => {
125 // These run in order. cwd and env carry over.
126 serial('corepack enable')
127 serial('pnpm install')
128 // These two start at the same time, with the same cwd and env.
129 parallel(['pnpm build', 'pnpm typecheck'])
130 // This waits for both parallel steps.
131 serial('echo bootstrap done')
132 },
133})
134```
135
136The callback runs on one persistent shell. A `cd` or an `export` in a serial step is visible to the next step.
137
138> [!NOTE]
139> When the provider supports snapshots, bootstrap saves the result after `setup`, so later runs skip it. See [Lifecycle & Snapshots](./lifecycle).
140
141## Scripts
142
143`scripts` maps names to commands. The agent and the user can run a command by its name:
144
145```ts
146import { defineWorkspace, githubRepo } from '@tanstack/ai-sandbox'
147
148defineWorkspace({
149 source: githubRepo({ repo: 'owner/app' }),
150 scripts: {
151 test: 'pnpm test',
152 build: 'pnpm build',
153 typecheck: 'pnpm test:types',
154 },
155})
156```
157
158Named scripts also give [Policy](./policy) stable names to allow, ask about, or deny.
159
160## Advanced
161
162### Installing an agent CLI
163
164Every setup step runs through `sh -c`. A step with a non-zero exit code fails the bootstrap.
165
166When a step installs a harness CLI globally, run the CLI at the end of the step. The agent CLIs (`@openai/codex`, `@anthropic-ai/claude-code`, and more) ship their native binary as a platform-specific **optional** dependency. npm treats optional dependencies as best effort:
167
1681. A download of the binary fails.
1692. `npm install -g` still exits `0`.
1703. The CLI fails later, during the run, with `Missing optional dependency`.
171
172If the step runs the CLI, that failure happens at bootstrap, where you see it:
173
174```ts
175import { defineWorkspace, githubRepo } from '@tanstack/ai-sandbox'
176
177const install = 'npm install -g @openai/codex --include=optional && codex --version'
178
179defineWorkspace({
180 source: githubRepo({ repo: 'owner/app' }),
181 // Retry once: this failure is usually a short download problem.
182 setup: [`${install} || { ${install} ; }`],
183})
184```
185
186Keep each command self-contained. Do not build a step by putting one command inside another (`` `${cmd} || sudo ${cmd}` ``). A command that starts with a subshell, such as the Grok CLI installer `(curl ... || curl ...) | bash`, is not a valid argument to another command. `sh` then fails to parse it with `syntax error: unexpected "("` (exit `2`) and runs nothing.
187
188### Non-root providers
189
190Some providers run as a non-root user:
191
192- Installs inside the workspace, such as `pnpm install`, do not need `sudo`.
193- System or global package installs on those providers need `sudo -n` in `setup`.
194
195See [Providers (Advanced)](./providers-advanced) for which providers do this, and [Policy](./policy) so that you do not deny `sudo` there.