Open History to see the last commit
1---
2title: Workspace
3id: workspace
4order: 4
5description: "Describe what the agent sees inside the sandbox: source repo, package manager, setup commands, and named scripts, in one portable definition."
6---
7
8The workspace is what the agent boots into: a cloned, installed repo with the
9commands you want it to run. `defineWorkspace()` describes that working tree
10once. Each harness adapter projects it into its own native format,
11and it runs inside whichever [provider](./providers) you chose. This page covers
12the working tree itself: `source`, `packageManager`, `setup`, and `scripts`.
13Secrets, skills, and MCP servers live on [Provisioning](./provisioning).
14
15```ts
16import { createSecrets, defineWorkspace, githubRepo } from '@tanstack/ai-sandbox'
17
18defineWorkspace({
19 // Where the working tree comes from.
20 source: githubRepo({ repo: 'owner/repo', ref: 'main' }),
21 // Package manager (auto-detected from the lockfile when omitted).
22 packageManager: 'pnpm',
23 // Commands run once during bootstrap.
24 setup: ['corepack enable', 'pnpm install'],
25 // Named commands the agent can run.
26 scripts: { test: 'pnpm test', build: 'pnpm build' },
27 // Injected into the sandbox env at create/resume, never persisted.
28 secrets: createSecrets({ XAI_API_KEY: process.env.XAI_API_KEY ?? '' }),
29})
30```
31
32The fields:
33
34| Field | What it sets |
35| ---------------- | ------------------------------------------------------------------------- |
36| `source` | Where the working tree comes from (git repo, local path, or nothing). |
37| `packageManager` | `npm` / `pnpm` / `yarn` / `bun` / `auto`. Defaults to `auto`. |
38| `setup` | Commands run once during bootstrap (serial array or serial/parallel groups). |
39| `scripts` | Named commands the agent and user can invoke by name. |
40| `secrets` | Typed secret refs injected into the env. See [Provisioning](./provisioning). |
41
42> `defineWorkspace()` also takes `skills`, `plugins`, and `instructions` for
43> provisioning the agent environment. Those are covered on
44> [Provisioning](./provisioning) rather than duplicated here.
45
46## Source
47
48`source` is where the working tree comes from. Five shapes:
49
50```ts
51import { defineWorkspace, githubRepo, gitSource } from '@tanstack/ai-sandbox'
52
53// Shorthand for a GitHub repo (owner/repo or a full URL).
54defineWorkspace({ source: githubRepo({ repo: 'owner/repo', ref: 'main' }) })
55
56// Any git URL.
57defineWorkspace({ source: gitSource({ url: 'https://git.example.com/owner/repo.git' }) })
58
59// The same as gitSource, written out as a plain object.
60defineWorkspace({ source: { type: 'git', url: 'https://github.com/owner/repo', ref: 'main' } })
61
62// An existing directory on the host (e.g. local-process dev loop).
63defineWorkspace({ source: { type: 'local', path: '/abs/path/to/repo' } })
64
65// No working tree: the agent starts in an empty workspace.
66defineWorkspace({ source: { type: 'none' } })
67```
68
69`githubRepo` is a convenience wrapper over `gitSource`: a short `owner/repo`
70expands to `https://github.com/owner/repo.git`, while a full URL is used as-is.
71Both produce a `{ type: 'git' }` source, so you can also write that object
72literal directly when you want full control.
73
74### Shallow clone by default
75
76`githubRepo` and `gitSource` default to a shallow single-branch clone
77(`--depth 1 --single-branch`) so cold starts stay fast. Pass a `depth` number
78for a specific history depth, or `'full'` to fetch everything:
79
80```ts
81import { defineWorkspace, githubRepo } from '@tanstack/ai-sandbox'
82
83// Shallow clone (depth 1) is the default.
84defineWorkspace({ source: githubRepo({ repo: 'owner/app' }) })
85
86// Explicit depth: fetches the last 10 commits.
87defineWorkspace({ source: githubRepo({ repo: 'owner/app', depth: 10 }) })
88
89// Full history: disables the depth flag entirely.
90defineWorkspace({ source: githubRepo({ repo: 'owner/app', depth: 'full' }) })
91```
92
93## Package manager
94
95`packageManager` is `'npm'`, `'pnpm'`, `'yarn'`, `'bun'`, or `'auto'`. It
96defaults to `'auto'`, which detects the manager from the lockfile after the
97source lands. Set it explicitly when you want to pin the choice rather than
98infer it.
99
100## Setup
101
102`setup` runs once during bootstrap to turn a freshly cloned repo into an
103installed one. It accepts either a plain string array (every step runs serially) or
104a builder callback that records serial and parallel groups.
105
106The simplest form is the array, equivalent to all-serial:
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
117The callback runs over a **persistent shell**: the working directory and
118environment carry over between steps, so a `cd` or `export` in a serial step is
119visible to the next one. Use `parallel([...])` to launch independent commands
120concurrently. They inherit the shell's cwd and env, and the next serial step
121waits for all of them to finish:
122
123```ts
124import { defineWorkspace, githubRepo } from '@tanstack/ai-sandbox'
125
126defineWorkspace({
127 source: githubRepo({ repo: 'owner/app' }),
128 setup: ({ serial, parallel }) => {
129 // Runs in order on the persistent shell; cwd/env carry over.
130 serial('corepack enable')
131 serial('pnpm install')
132 // Both commands launch concurrently, inheriting cwd + env from the shell.
133 parallel(['pnpm build', 'pnpm typecheck'])
134 // Runs after both parallel steps complete.
135 serial('echo bootstrap done')
136 },
137})
138```
139
140> When the provider supports snapshots, bootstrap caches the result after
141> `setup` so subsequent runs skip it. See
142> [Lifecycle & snapshots](./lifecycle).
143
144### Installing an agent CLI
145
146Every step runs through `sh -c`, and a step fails the bootstrap when its exit
147code is non-zero. When a step installs a harness CLI globally, make it verify the
148CLI before it returns. The agent CLIs (`@openai/codex`,
149`@anthropic-ai/claude-code`, …) ship their native binary as a platform-specific
150**optional** dependency, and npm treats optional dependencies as best-effort: a
151failed download is not an install error, so `npm install -g` exits `0` and leaves
152a CLI that dies later with `Missing optional dependency`. Running the CLI turns
153that into a loud bootstrap failure instead of a broken run:
154
155```ts
156import { defineWorkspace, githubRepo } from '@tanstack/ai-sandbox'
157
158const install = 'npm install -g @openai/codex --include=optional && codex --version'
159
160defineWorkspace({
161 source: githubRepo({ repo: 'owner/app' }),
162 // Retry once: the failure above is usually a transient download.
163 setup: [`${install} || { ${install} ; }`],
164})
165```
166
167Keep each command self-contained. Do not build a step by splicing one command
168into another (`` `${cmd} || sudo ${cmd}` ``): a command that starts with a
169subshell (like the Grok CLI installer, `(curl … || curl …) | bash`) is not a
170valid argument to another command, and `sh` then fails at parse time with
171`syntax error: unexpected "("` (exit `2`) without running anything.
172
173Some providers run as a non-root user. Workspace-local installs such as
174`pnpm install` do not need sudo. System or global package installs on those
175providers need non-interactive `sudo -n` in `setup`. See
176[Providers](./providers) and [Policy](./policy).
177
178## Scripts
179
180`scripts` is a map of named commands the agent and user can invoke by name,
181without restating the full command line each time:
182
183```ts
184import { defineWorkspace, githubRepo } from '@tanstack/ai-sandbox'
185
186defineWorkspace({
187 source: githubRepo({ repo: 'owner/app' }),
188 scripts: {
189 test: 'pnpm test',
190 build: 'pnpm build',
191 typecheck: 'pnpm test:types',
192 },
193})
194```
195
196These are surfaced as named commands rather than freeform shell, which also
197gives [Policy](./policy) a stable name to allow, ask about, or deny.