@rockclaver/sandcastle 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +1355 -0
- package/dist/MountConfig-CmXclHA5.d.ts +26 -0
- package/dist/SandboxProvider-EkSMuBp8.d.ts +243 -0
- package/dist/chunk-72UVAC7B.js +99 -0
- package/dist/chunk-72UVAC7B.js.map +1 -0
- package/dist/chunk-BIWNFKGV.js +22 -0
- package/dist/chunk-BIWNFKGV.js.map +1 -0
- package/dist/chunk-FKX3DRTL.js +362 -0
- package/dist/chunk-FKX3DRTL.js.map +1 -0
- package/dist/chunk-NGBM7T3E.js +76 -0
- package/dist/chunk-NGBM7T3E.js.map +1 -0
- package/dist/chunk-QCLZLPJ7.js +26431 -0
- package/dist/chunk-QCLZLPJ7.js.map +1 -0
- package/dist/chunk-VAKEM3U2.js +26997 -0
- package/dist/chunk-VAKEM3U2.js.map +1 -0
- package/dist/index.d.ts +943 -0
- package/dist/index.js +2393 -0
- package/dist/index.js.map +1 -0
- package/dist/main.d.ts +1 -0
- package/dist/main.js +19268 -0
- package/dist/main.js.map +1 -0
- package/dist/mountUtils-CCA-bbpK.d.ts +25 -0
- package/dist/sandboxes/daytona.d.ts +60 -0
- package/dist/sandboxes/daytona.js +122 -0
- package/dist/sandboxes/daytona.js.map +1 -0
- package/dist/sandboxes/docker.d.ts +110 -0
- package/dist/sandboxes/docker.js +9 -0
- package/dist/sandboxes/docker.js.map +1 -0
- package/dist/sandboxes/no-sandbox.d.ts +38 -0
- package/dist/sandboxes/no-sandbox.js +7 -0
- package/dist/sandboxes/no-sandbox.js.map +1 -0
- package/dist/sandboxes/podman.d.ts +124 -0
- package/dist/sandboxes/podman.js +299 -0
- package/dist/sandboxes/podman.js.map +1 -0
- package/dist/sandboxes/vercel.d.ts +104 -0
- package/dist/sandboxes/vercel.js +148 -0
- package/dist/sandboxes/vercel.js.map +1 -0
- package/dist/templates/blank/main.mts +14 -0
- package/dist/templates/blank/prompt.md +12 -0
- package/dist/templates/blank/template.json +4 -0
- package/dist/templates/parallel-planner/implement-prompt.md +62 -0
- package/dist/templates/parallel-planner/main.mts +204 -0
- package/dist/templates/parallel-planner/merge-prompt.md +26 -0
- package/dist/templates/parallel-planner/plan-prompt.md +37 -0
- package/dist/templates/parallel-planner/template.json +4 -0
- package/dist/templates/parallel-planner-with-review/CODING_STANDARDS.md +27 -0
- package/dist/templates/parallel-planner-with-review/implement-prompt.md +62 -0
- package/dist/templates/parallel-planner-with-review/main.mts +226 -0
- package/dist/templates/parallel-planner-with-review/merge-prompt.md +26 -0
- package/dist/templates/parallel-planner-with-review/plan-prompt.md +37 -0
- package/dist/templates/parallel-planner-with-review/review-prompt.md +55 -0
- package/dist/templates/parallel-planner-with-review/template.json +4 -0
- package/dist/templates/sequential-reviewer/CODING_STANDARDS.md +27 -0
- package/dist/templates/sequential-reviewer/implement-prompt.md +53 -0
- package/dist/templates/sequential-reviewer/main.mts +119 -0
- package/dist/templates/sequential-reviewer/review-prompt.md +55 -0
- package/dist/templates/sequential-reviewer/template.json +4 -0
- package/dist/templates/simple-loop/main.mts +49 -0
- package/dist/templates/simple-loop/prompt.md +53 -0
- package/dist/templates/simple-loop/template.json +4 -0
- package/package.json +104 -0
package/README.md
ADDED
|
@@ -0,0 +1,1355 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
<picture>
|
|
3
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://res.cloudinary.com/total-typescript/image/upload/v1775033787/readme-sandcastle-ondark_2x.png">
|
|
4
|
+
<source media="(prefers-color-scheme: light)" srcset="https://res.cloudinary.com/total-typescript/image/upload/v1775033787/readme-sandcastle-onlight_2x.png">
|
|
5
|
+
<img alt="Sandcastle" src="https://res.cloudinary.com/total-typescript/image/upload/v1775033787/readme-sandcastle-onlight_2x.png" height="200" style="margin-bottom: 20px;">
|
|
6
|
+
</picture>
|
|
7
|
+
</div>
|
|
8
|
+
|
|
9
|
+
## What Is Sandcastle?
|
|
10
|
+
|
|
11
|
+
A TypeScript library for orchestrating AI coding agents in isolated sandboxes:
|
|
12
|
+
|
|
13
|
+
1. You invoke agents with a single `sandcastle.run()`.
|
|
14
|
+
2. Sandcastle handles sandboxing the agent with a configurable branch strategy.
|
|
15
|
+
3. The commits made on the branches get merged back.
|
|
16
|
+
|
|
17
|
+
Sandcastle is provider-agnostic — it ships with built-in providers for Docker, Podman, and Vercel, and you can create your own. Great for parallelizing multiple AFK agents, creating review pipelines, or even just orchestrating your own agents.
|
|
18
|
+
|
|
19
|
+
## Prerequisites
|
|
20
|
+
|
|
21
|
+
- [Git](https://git-scm.com/)
|
|
22
|
+
- A sandbox provider — Sandcastle needs an isolated environment to run agents in. Built-in options:
|
|
23
|
+
- [Docker Desktop](https://www.docker.com/) — most common for local development
|
|
24
|
+
- [Podman](https://podman.io/) — rootless alternative to Docker
|
|
25
|
+
- [Vercel](https://vercel.com/) — cloud-based Firecracker microVMs via `@vercel/sandbox`
|
|
26
|
+
- Or [create your own](#custom-sandbox-providers) using `createBindMountSandboxProvider` or `createIsolatedSandboxProvider`
|
|
27
|
+
|
|
28
|
+
## Quick start
|
|
29
|
+
|
|
30
|
+
1. Install the package:
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
npm install --save-dev @ai-hero/sandcastle
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
2. Run `npx @ai-hero/sandcastle init`. This scaffolds a `.sandcastle` directory with all the files needed.
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
npx @ai-hero/sandcastle init
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
3. Edit `.sandcastle/.env` and fill in your default values for `ANTHROPIC_API_KEY`. If you want to use your Claude subscription instead of an API key, see [#191](https://github.com/mattpocock/sandcastle/issues/191).
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
cp .sandcastle/.env.example .sandcastle/.env
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
4. Run the `.sandcastle/main.ts` (or `main.mts`) file with `npx tsx`
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
npx tsx .sandcastle/main.ts
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
```typescript
|
|
55
|
+
// 3. Run the agent via the JS API
|
|
56
|
+
import { run, claudeCode } from "@ai-hero/sandcastle";
|
|
57
|
+
import { docker } from "@ai-hero/sandcastle/sandboxes/docker";
|
|
58
|
+
|
|
59
|
+
await run({
|
|
60
|
+
agent: claudeCode("claude-opus-4-7"),
|
|
61
|
+
sandbox: docker(), // or podman(), vercel(), or your own provider
|
|
62
|
+
promptFile: ".sandcastle/prompt.md",
|
|
63
|
+
});
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Sandbox Providers
|
|
67
|
+
|
|
68
|
+
Sandcastle uses a `SandboxProvider` to create isolated environments. The `sandbox` option on `run()`, `interactive()`, and `createSandbox()` accepts any provider, including `noSandbox()` — opt in to running the agent directly on the host when container isolation is undesired. Built-in providers:
|
|
69
|
+
|
|
70
|
+
| Provider | Import path | Type | Accepted by |
|
|
71
|
+
| ---------- | ------------------------------------------ | ---------- | ------------------------------------------- |
|
|
72
|
+
| Docker | `@ai-hero/sandcastle/sandboxes/docker` | Bind-mount | `run()`, `createSandbox()`, `interactive()` |
|
|
73
|
+
| Podman | `@ai-hero/sandcastle/sandboxes/podman` | Bind-mount | `run()`, `createSandbox()`, `interactive()` |
|
|
74
|
+
| Vercel | `@ai-hero/sandcastle/sandboxes/vercel` | Isolated | `run()`, `createSandbox()`, `interactive()` |
|
|
75
|
+
| No-sandbox | `@ai-hero/sandcastle/sandboxes/no-sandbox` | None | `run()`, `createSandbox()`, `interactive()` |
|
|
76
|
+
|
|
77
|
+
Worktree methods (`wt.run()`, `wt.interactive()`, `wt.createSandbox()`) accept the same providers as their top-level counterparts. `wt.interactive()` defaults to `noSandbox()` when no sandbox is specified.
|
|
78
|
+
|
|
79
|
+
```typescript
|
|
80
|
+
import { docker } from "@ai-hero/sandcastle/sandboxes/docker";
|
|
81
|
+
import { podman } from "@ai-hero/sandcastle/sandboxes/podman";
|
|
82
|
+
import { vercel } from "@ai-hero/sandcastle/sandboxes/vercel";
|
|
83
|
+
import { noSandbox } from "@ai-hero/sandcastle/sandboxes/no-sandbox";
|
|
84
|
+
|
|
85
|
+
// Docker, Podman, and Vercel are interchangeable in run() and createSandbox():
|
|
86
|
+
await run({
|
|
87
|
+
agent: claudeCode("claude-opus-4-7"),
|
|
88
|
+
sandbox: docker(),
|
|
89
|
+
prompt: "...",
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
// No-sandbox runs the agent directly on the host — accepted by run(),
|
|
93
|
+
// createSandbox(), and interactive(). Skips container isolation entirely:
|
|
94
|
+
await interactive({
|
|
95
|
+
agent: claudeCode("claude-opus-4-7"),
|
|
96
|
+
sandbox: noSandbox(),
|
|
97
|
+
prompt: "...", // optional — omit to launch the TUI with no initial prompt
|
|
98
|
+
cwd: "/path/to/other-repo", // optional — defaults to process.cwd()
|
|
99
|
+
});
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
You can also [create your own provider](#custom-sandbox-providers) using `createBindMountSandboxProvider` or `createIsolatedSandboxProvider`.
|
|
103
|
+
|
|
104
|
+
## API
|
|
105
|
+
|
|
106
|
+
Sandcastle exports a programmatic `run()` function for use in scripts, CI pipelines, or custom tooling. The examples below use `docker()`, but any `SandboxProvider` works in its place.
|
|
107
|
+
|
|
108
|
+
```typescript
|
|
109
|
+
import { run, claudeCode } from "@ai-hero/sandcastle";
|
|
110
|
+
import { docker } from "@ai-hero/sandcastle/sandboxes/docker";
|
|
111
|
+
|
|
112
|
+
const result = await run({
|
|
113
|
+
agent: claudeCode("claude-opus-4-7"),
|
|
114
|
+
sandbox: docker(),
|
|
115
|
+
promptFile: ".sandcastle/prompt.md",
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
console.log(result.iterations.length); // number of iterations executed
|
|
119
|
+
console.log(result.iterations); // per-iteration results with optional sessionId
|
|
120
|
+
console.log(result.commits); // array of { sha } for commits created
|
|
121
|
+
console.log(result.branch); // target branch name
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
### All options
|
|
125
|
+
|
|
126
|
+
```typescript
|
|
127
|
+
import { run, claudeCode } from "@ai-hero/sandcastle";
|
|
128
|
+
import { docker } from "@ai-hero/sandcastle/sandboxes/docker";
|
|
129
|
+
|
|
130
|
+
const result = await run({
|
|
131
|
+
// Agent provider — required. Pass a model string to claudeCode().
|
|
132
|
+
// Optional second arg for provider-specific options like effort level.
|
|
133
|
+
agent: claudeCode("claude-opus-4-7", { effort: "high" }),
|
|
134
|
+
|
|
135
|
+
// Sandbox provider — required. Any SandboxProvider works (docker, podman, vercel, or custom).
|
|
136
|
+
// Provider-specific config (like imageName, mounts) lives inside the provider factory call.
|
|
137
|
+
sandbox: docker({
|
|
138
|
+
imageName: "sandcastle:local",
|
|
139
|
+
// Optional: override the UID/GID used for --user flag (defaults to host UID/GID).
|
|
140
|
+
// Must match the UID baked into the image. Pre-flight check catches mismatches.
|
|
141
|
+
// containerUid: 1000,
|
|
142
|
+
// containerGid: 1000,
|
|
143
|
+
// Optional: mount host directories into the sandbox (e.g. package manager caches)
|
|
144
|
+
// hostPath supports absolute, tilde-expanded (~), and relative paths (resolved from cwd).
|
|
145
|
+
// sandboxPath supports absolute and relative paths (resolved from the sandbox repo directory).
|
|
146
|
+
mounts: [
|
|
147
|
+
{ hostPath: "~/.npm", sandboxPath: "/home/agent/.npm", readonly: true },
|
|
148
|
+
{ hostPath: "data", sandboxPath: "data" }, // mounts <cwd>/data → <sandbox-repo>/data
|
|
149
|
+
],
|
|
150
|
+
// Optional: SELinux volume label — "z" (default, shared), "Z" (private), or false (none).
|
|
151
|
+
// No-op on non-SELinux systems (Docker Desktop on macOS/Windows, Linux without SELinux).
|
|
152
|
+
selinuxLabel: "z",
|
|
153
|
+
// Optional: provider-level env vars merged at launch time
|
|
154
|
+
env: { DOCKER_SPECIFIC: "value" },
|
|
155
|
+
// Optional: attach container to Docker network(s) — string or string[]
|
|
156
|
+
network: "my-network",
|
|
157
|
+
// Optional: add the container user to supplementary groups via --group-add.
|
|
158
|
+
// Accepts group names or numeric GIDs (e.g. for a bind-mounted Docker socket).
|
|
159
|
+
groups: ["docker", 999],
|
|
160
|
+
// Optional: expose host devices via --device. Each entry is a full device
|
|
161
|
+
// spec in host[:container[:permissions]] form (e.g. "/dev/kvm").
|
|
162
|
+
devices: ["/dev/kvm"],
|
|
163
|
+
// Optional: limit CPU resources via --cpus. Fractional values allowed (e.g. 1.5).
|
|
164
|
+
// cpus: 2,
|
|
165
|
+
}),
|
|
166
|
+
|
|
167
|
+
// Host repo directory — replaces process.cwd() as the anchor for
|
|
168
|
+
// .sandcastle/ artifacts (worktrees, logs, env, patches) and git operations.
|
|
169
|
+
// Relative paths resolve against process.cwd(). Defaults to process.cwd().
|
|
170
|
+
cwd: "../other-repo",
|
|
171
|
+
|
|
172
|
+
// Branch strategy — controls how the agent's changes relate to branches.
|
|
173
|
+
// Defaults to { type: "head" } for bind-mount and { type: "merge-to-head" } for isolated providers.
|
|
174
|
+
branchStrategy: { type: "branch", branch: "agent/fix-42" },
|
|
175
|
+
|
|
176
|
+
// Prompt source — provide one of these, not both.
|
|
177
|
+
// Note: promptFile resolves against process.cwd(), NOT cwd.
|
|
178
|
+
promptFile: ".sandcastle/prompt.md", // path to a prompt file
|
|
179
|
+
// prompt: "Fix issue #42 in this repo", // OR an inline prompt string
|
|
180
|
+
|
|
181
|
+
// Values substituted for {{KEY}} placeholders in the prompt.
|
|
182
|
+
promptArgs: {
|
|
183
|
+
ISSUE_NUMBER: "42",
|
|
184
|
+
},
|
|
185
|
+
|
|
186
|
+
// Maximum number of agent iterations to run before stopping. Default: 1
|
|
187
|
+
maxIterations: 5,
|
|
188
|
+
|
|
189
|
+
// Display name for this run, shown as a prefix in log output.
|
|
190
|
+
name: "fix-issue-42",
|
|
191
|
+
|
|
192
|
+
// Lifecycle hooks grouped by where they run: host or sandbox.
|
|
193
|
+
hooks: {
|
|
194
|
+
host: {
|
|
195
|
+
onWorktreeReady: [{ command: "cp .env.example .env" }],
|
|
196
|
+
onSandboxReady: [{ command: "echo setup done" }],
|
|
197
|
+
},
|
|
198
|
+
sandbox: {
|
|
199
|
+
onSandboxReady: [{ command: "npm install" }],
|
|
200
|
+
},
|
|
201
|
+
},
|
|
202
|
+
|
|
203
|
+
// Host-relative file paths to copy into the sandbox before the container starts.
|
|
204
|
+
// Not supported with branchStrategy: { type: "head" }.
|
|
205
|
+
copyToWorktree: [".env"],
|
|
206
|
+
|
|
207
|
+
// Override default timeouts for built-in lifecycle steps.
|
|
208
|
+
// Unset keys keep their defaults.
|
|
209
|
+
timeouts: {
|
|
210
|
+
copyToWorktreeMs: 120_000, // default: 60_000
|
|
211
|
+
gitSetupMs: 30_000, // default: 10_000
|
|
212
|
+
commitCollectionMs: 60_000, // default: 30_000
|
|
213
|
+
mergeToHostMs: 60_000, // default: 30_000
|
|
214
|
+
},
|
|
215
|
+
|
|
216
|
+
// How to record progress. Default: write to a file under .sandcastle/logs/
|
|
217
|
+
logging: {
|
|
218
|
+
type: "file",
|
|
219
|
+
path: ".sandcastle/logs/my-run.log",
|
|
220
|
+
// Optional: forward the agent's output stream to your own observability system.
|
|
221
|
+
// Fires for each text chunk and tool call the agent produces. Errors thrown
|
|
222
|
+
// by the callback are swallowed so a broken forwarder cannot kill the run.
|
|
223
|
+
onAgentStreamEvent: (event) => {
|
|
224
|
+
// event is { type: "text" | "toolCall", iteration, timestamp, ... }
|
|
225
|
+
myLogger.info(event);
|
|
226
|
+
},
|
|
227
|
+
},
|
|
228
|
+
// logging: { type: "stdout" }, // OR render an interactive UI in the terminal
|
|
229
|
+
|
|
230
|
+
// String (or array of strings) the agent emits to end the iteration loop early.
|
|
231
|
+
// Default: "<promise>COMPLETE</promise>"
|
|
232
|
+
completionSignal: "<promise>COMPLETE</promise>",
|
|
233
|
+
|
|
234
|
+
// Idle timeout in seconds — resets whenever the agent produces output. Default: 600 (10 minutes)
|
|
235
|
+
idleTimeoutSeconds: 600,
|
|
236
|
+
|
|
237
|
+
// Grace window in seconds after the agent emits a completion signal but
|
|
238
|
+
// before its process has exited (a "hanging process" — typically a spawned
|
|
239
|
+
// `gh`/git child or MCP server keeping stdout open). Resets on every
|
|
240
|
+
// subsequent output line so trailing data is still captured. Default: 60
|
|
241
|
+
completionTimeoutSeconds: 60,
|
|
242
|
+
|
|
243
|
+
// Structured output — extract a typed payload from the agent's stdout.
|
|
244
|
+
// Requires maxIterations === 1 and the tag must appear in the prompt.
|
|
245
|
+
// output: Output.object({ tag: "result", schema: z.object({ answer: z.number() }) }),
|
|
246
|
+
// output: Output.string({ tag: "summary" }),
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
console.log(result.iterations.length); // number of iterations executed
|
|
250
|
+
console.log(result.completionSignal); // matched signal string, or undefined if none fired
|
|
251
|
+
console.log(result.commits); // array of { sha } for commits created
|
|
252
|
+
console.log(result.branch); // target branch name
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
### `createSandbox()` — reusable sandbox
|
|
256
|
+
|
|
257
|
+
Use `createSandbox()` when you need to run multiple agents (or multiple rounds of the same agent) inside a single sandbox. It creates the sandbox once, and you call `sandbox.run()` as many times as you need. This avoids repeated container startup costs and keeps all runs on the same branch.
|
|
258
|
+
|
|
259
|
+
Use `run()` instead when you only need a single one-shot invocation — it handles sandbox lifecycle automatically.
|
|
260
|
+
|
|
261
|
+
#### Basic single-run usage
|
|
262
|
+
|
|
263
|
+
```typescript
|
|
264
|
+
import { createSandbox, claudeCode } from "@ai-hero/sandcastle";
|
|
265
|
+
import { docker } from "@ai-hero/sandcastle/sandboxes/docker";
|
|
266
|
+
|
|
267
|
+
await using sandbox = await createSandbox({
|
|
268
|
+
branch: "agent/fix-42",
|
|
269
|
+
sandbox: docker(),
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
const result = await sandbox.run({
|
|
273
|
+
agent: claudeCode("claude-opus-4-7"),
|
|
274
|
+
prompt: "Fix issue #42 in this repo.",
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
console.log(result.commits); // [{ sha: "abc123" }]
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
#### Multi-run implement-then-review
|
|
281
|
+
|
|
282
|
+
```typescript
|
|
283
|
+
import { createSandbox, claudeCode } from "@ai-hero/sandcastle";
|
|
284
|
+
import { docker } from "@ai-hero/sandcastle/sandboxes/docker";
|
|
285
|
+
|
|
286
|
+
await using sandbox = await createSandbox({
|
|
287
|
+
branch: "agent/fix-42",
|
|
288
|
+
sandbox: docker(),
|
|
289
|
+
hooks: { sandbox: { onSandboxReady: [{ command: "npm install" }] } },
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
// Step 1: implement
|
|
293
|
+
const implResult = await sandbox.run({
|
|
294
|
+
agent: claudeCode("claude-opus-4-7"),
|
|
295
|
+
promptFile: ".sandcastle/implement.md",
|
|
296
|
+
maxIterations: 5,
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
// Step 2: review on the same branch, same container
|
|
300
|
+
const reviewResult = await sandbox.run({
|
|
301
|
+
agent: claudeCode("claude-sonnet-4-6"),
|
|
302
|
+
prompt: "Review the changes and fix any issues.",
|
|
303
|
+
});
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
Commits from all `run()` calls accumulate on the same branch. The sandbox container stays alive between runs, so installed dependencies and build artifacts persist.
|
|
307
|
+
|
|
308
|
+
#### Automatic cleanup with `await using`
|
|
309
|
+
|
|
310
|
+
`await using` calls `sandbox.close()` automatically when the block exits. If the sandbox has uncommitted changes, the worktree is preserved on disk; if clean, both container and worktree are removed.
|
|
311
|
+
|
|
312
|
+
#### Manual `close()` with `CloseResult`
|
|
313
|
+
|
|
314
|
+
```typescript
|
|
315
|
+
const sandbox = await createSandbox({
|
|
316
|
+
branch: "agent/fix-42",
|
|
317
|
+
sandbox: docker(),
|
|
318
|
+
});
|
|
319
|
+
// ... run agents ...
|
|
320
|
+
const closeResult = await sandbox.close();
|
|
321
|
+
if (closeResult.preservedWorktreePath) {
|
|
322
|
+
console.log(`Worktree preserved at ${closeResult.preservedWorktreePath}`);
|
|
323
|
+
}
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
#### `CreateSandboxOptions`
|
|
327
|
+
|
|
328
|
+
| Option | Type | Default | Description |
|
|
329
|
+
| ---------------- | --------------- | --------------- | ------------------------------------------------------------------------------------------------------------------- |
|
|
330
|
+
| `branch` | string | — | **Required.** Explicit branch for the sandbox |
|
|
331
|
+
| `sandbox` | SandboxProvider | — | **Required.** Sandbox provider (e.g. `docker()`, `podman()`) |
|
|
332
|
+
| `cwd` | string | `process.cwd()` | Host repo directory — relative paths resolve against `process.cwd()` |
|
|
333
|
+
| `hooks` | SandboxHooks | — | Lifecycle hooks (`host.*`, `sandbox.*`) — run once at creation time |
|
|
334
|
+
| `copyToWorktree` | string[] | — | Host-relative file paths to copy into the sandbox at creation time |
|
|
335
|
+
| `timeouts` | Timeouts | — | Override built-in lifecycle step timeouts (`copyToWorktreeMs`, `gitSetupMs`, `commitCollectionMs`, `mergeToHostMs`) |
|
|
336
|
+
|
|
337
|
+
#### `Sandbox`
|
|
338
|
+
|
|
339
|
+
| Property / Method | Type | Description |
|
|
340
|
+
| ----------------------- | ------------------------------------------------------------------ | -------------------------------------------- |
|
|
341
|
+
| `branch` | string | The branch the sandbox is on |
|
|
342
|
+
| `worktreePath` | string | Host path to the worktree |
|
|
343
|
+
| `run(options)` | `(SandboxRunOptions) => Promise<SandboxRunResult>` | Invoke an agent inside the existing sandbox |
|
|
344
|
+
| `interactive(options)` | `(SandboxInteractiveOptions) => Promise<SandboxInteractiveResult>` | Launch an interactive session in the sandbox |
|
|
345
|
+
| `close()` | `() => Promise<CloseResult>` | Tear down the container and sandbox |
|
|
346
|
+
| `[Symbol.asyncDispose]` | `() => Promise<void>` | Auto teardown via `await using` |
|
|
347
|
+
|
|
348
|
+
#### `SandboxRunOptions`
|
|
349
|
+
|
|
350
|
+
| Option | Type | Default | Description |
|
|
351
|
+
| -------------------------- | ------------------ | ----------------------------- | ------------------------------------------------------------------------------------ |
|
|
352
|
+
| `agent` | AgentProvider | — | **Required.** Agent provider (e.g. `claudeCode("claude-opus-4-7")`) |
|
|
353
|
+
| `prompt` | string | — | Inline prompt (mutually exclusive with `promptFile`) |
|
|
354
|
+
| `promptFile` | string | — | Path to prompt file (mutually exclusive with `prompt`) |
|
|
355
|
+
| `promptArgs` | PromptArgs | — | Key-value map for `{{KEY}}` placeholder substitution |
|
|
356
|
+
| `maxIterations` | number | `1` | Maximum iterations to run |
|
|
357
|
+
| `completionSignal` | string \| string[] | `<promise>COMPLETE</promise>` | String(s) the agent emits to stop the iteration loop early |
|
|
358
|
+
| `idleTimeoutSeconds` | number | `600` | Idle timeout in seconds — resets on each agent output event |
|
|
359
|
+
| `completionTimeoutSeconds` | number | `60` | Grace window after the completion signal is seen but the agent process hasn't exited |
|
|
360
|
+
| `name` | string | — | Display name for the run |
|
|
361
|
+
| `logging` | object | file (auto-generated) | `{ type: 'file', path }` or `{ type: 'stdout' }` |
|
|
362
|
+
| `signal` | AbortSignal | — | Cancels the run when aborted; handle stays usable afterward |
|
|
363
|
+
|
|
364
|
+
#### `SandboxRunResult`
|
|
365
|
+
|
|
366
|
+
| Field | Type | Description |
|
|
367
|
+
| ------------------ | ------------------- | ------------------------------------------------------------------ |
|
|
368
|
+
| `iterations` | `IterationResult[]` | Per-iteration results (use `.length` for the count) |
|
|
369
|
+
| `completionSignal` | string? | The matched completion signal string, or `undefined` if none fired |
|
|
370
|
+
| `stdout` | string | Combined agent output from all iterations |
|
|
371
|
+
| `commits` | `{ sha }[]` | Commits created during the run |
|
|
372
|
+
| `logFilePath` | string? | Path to the log file (only when logging to a file) |
|
|
373
|
+
|
|
374
|
+
#### `CloseResult`
|
|
375
|
+
|
|
376
|
+
| Field | Type | Description |
|
|
377
|
+
| ----------------------- | ------- | ------------------------------------------------------------------------ |
|
|
378
|
+
| `preservedWorktreePath` | string? | Host path to the preserved worktree, set when it had uncommitted changes |
|
|
379
|
+
|
|
380
|
+
### `createWorktree()` — independent worktree lifecycle
|
|
381
|
+
|
|
382
|
+
Use `createWorktree()` when you need a worktree (git worktree) as an independent, first-class concept — separate from any sandbox. This is useful when you want to run an interactive session first and then hand the same worktree to a sandboxed AFK agent.
|
|
383
|
+
|
|
384
|
+
Only `branch` and `merge-to-head` strategies are accepted; `head` is a compile-time type error since it means no worktree.
|
|
385
|
+
|
|
386
|
+
Pass `cwd` to target a repo other than `process.cwd()`. Relative paths resolve against `process.cwd()`; absolute paths pass through. A `CwdError` is thrown if the path does not exist or is not a directory.
|
|
387
|
+
|
|
388
|
+
```typescript
|
|
389
|
+
import { createWorktree } from "@ai-hero/sandcastle";
|
|
390
|
+
|
|
391
|
+
await using wt = await createWorktree({
|
|
392
|
+
branchStrategy: { type: "branch", branch: "agent/fix-42" },
|
|
393
|
+
copyToWorktree: ["node_modules"],
|
|
394
|
+
cwd: "/path/to/other-repo", // optional — defaults to process.cwd()
|
|
395
|
+
});
|
|
396
|
+
|
|
397
|
+
console.log(wt.worktreePath); // host path to the worktree
|
|
398
|
+
console.log(wt.branch); // "agent/fix-42"
|
|
399
|
+
|
|
400
|
+
// Run an interactive session in the worktree (defaults to noSandbox)
|
|
401
|
+
await wt.interactive({
|
|
402
|
+
agent: claudeCode("claude-opus-4-7"),
|
|
403
|
+
prompt: "Explore the codebase and understand the bug.",
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
// Run an AFK agent in the worktree (sandbox is required)
|
|
407
|
+
const result = await wt.run({
|
|
408
|
+
agent: claudeCode("claude-opus-4-7"),
|
|
409
|
+
sandbox: docker({ imageName: "sandcastle:myrepo" }),
|
|
410
|
+
prompt: "Fix issue #42.",
|
|
411
|
+
maxIterations: 3,
|
|
412
|
+
});
|
|
413
|
+
console.log(result.commits); // commits made during the run
|
|
414
|
+
|
|
415
|
+
// Create a long-lived sandbox from the worktree
|
|
416
|
+
import { docker } from "@ai-hero/sandcastle/sandboxes/docker";
|
|
417
|
+
|
|
418
|
+
await using sandbox = await wt.createSandbox({
|
|
419
|
+
sandbox: docker(),
|
|
420
|
+
hooks: { sandbox: { onSandboxReady: [{ command: "npm install" }] } },
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
// sandbox.close() tears down the container only — the worktree stays
|
|
424
|
+
await sandbox.close();
|
|
425
|
+
|
|
426
|
+
// wt.close() cleans up the worktree
|
|
427
|
+
```
|
|
428
|
+
|
|
429
|
+
`wt.close()` checks for uncommitted changes: if the worktree is dirty, it's preserved on disk; if clean, it's removed. `await using` calls `close()` automatically. The worktree persists after `run()`, `interactive()`, and `createSandbox()` complete, so you can hand it to another agent or inspect it.
|
|
430
|
+
|
|
431
|
+
**Split ownership**: When a sandbox is created via `wt.createSandbox()`, `sandbox.close()` tears down the container only — the worktree remains. `wt.close()` is responsible for worktree cleanup. This differs from the top-level `createSandbox()`, where `sandbox.close()` owns both container and worktree.
|
|
432
|
+
|
|
433
|
+
#### `CreateWorktreeOptions`
|
|
434
|
+
|
|
435
|
+
| Option | Type | Default | Description |
|
|
436
|
+
| ---------------- | ---------------------- | ------- | ------------------------------------------------------------------------------------------------------------------- |
|
|
437
|
+
| `branchStrategy` | WorktreeBranchStrategy | — | **Required.** `{ type: "branch", branch }` or `{ type: "merge-to-head" }` |
|
|
438
|
+
| `copyToWorktree` | string[] | — | Host-relative file paths to copy into the worktree at creation time |
|
|
439
|
+
| `timeouts` | Timeouts | — | Override built-in lifecycle step timeouts (`copyToWorktreeMs`, `gitSetupMs`, `commitCollectionMs`, `mergeToHostMs`) |
|
|
440
|
+
|
|
441
|
+
#### `Worktree`
|
|
442
|
+
|
|
443
|
+
| Property / Method | Type | Description |
|
|
444
|
+
| ------------------------ | --------------------------------------------------------------------- | --------------------------------------------------- |
|
|
445
|
+
| `branch` | string | The branch the worktree is on |
|
|
446
|
+
| `worktreePath` | string | Host path to the worktree |
|
|
447
|
+
| `run(options)` | `(options: WorktreeRunOptions) => Promise<WorktreeRunResult>` | Run an AFK agent in the worktree (sandbox required) |
|
|
448
|
+
| `interactive(options)` | `(options: WorktreeInteractiveOptions) => Promise<InteractiveResult>` | Run an interactive agent session in the worktree |
|
|
449
|
+
| `createSandbox(options)` | `(options: WorktreeCreateSandboxOptions) => Promise<Sandbox>` | Create a long-lived sandbox backed by this worktree |
|
|
450
|
+
| `close()` | `() => Promise<CloseResult>` | Clean up the worktree (preserves if dirty) |
|
|
451
|
+
| `[Symbol.asyncDispose]` | `() => Promise<void>` | Auto cleanup via `await using` |
|
|
452
|
+
|
|
453
|
+
#### `WorktreeInteractiveOptions`
|
|
454
|
+
|
|
455
|
+
| Option | Type | Default | Description |
|
|
456
|
+
| ------------ | ---------------------- | ------------- | ------------------------------------------------------------------------------------------------- |
|
|
457
|
+
| `agent` | AgentProvider | — | **Required.** Agent provider |
|
|
458
|
+
| `sandbox` | AnySandboxProvider | `noSandbox()` | Sandbox provider (defaults to no sandbox) |
|
|
459
|
+
| `prompt` | string | — | Inline prompt (mutually exclusive with `promptFile`) |
|
|
460
|
+
| `promptFile` | string | — | Path to prompt file |
|
|
461
|
+
| `name` | string | — | Optional session name |
|
|
462
|
+
| `hooks` | SandboxHooks | — | Lifecycle hooks (`host.*`, `sandbox.*`) |
|
|
463
|
+
| `promptArgs` | PromptArgs | — | Key-value map for `{{KEY}}` placeholder substitution |
|
|
464
|
+
| `env` | Record<string, string> | — | Environment variables to inject into the sandbox |
|
|
465
|
+
| `signal` | AbortSignal | — | Cancel the session when aborted. The worktree is preserved on disk. Rejects with `signal.reason`. |
|
|
466
|
+
|
|
467
|
+
#### `WorktreeRunOptions`
|
|
468
|
+
|
|
469
|
+
| Option | Type | Default | Description |
|
|
470
|
+
| -------------------------- | ---------------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------ |
|
|
471
|
+
| `agent` | AgentProvider | — | **Required.** Agent provider |
|
|
472
|
+
| `sandbox` | SandboxProvider | — | **Required.** Sandbox provider (AFK agents must be sandboxed) |
|
|
473
|
+
| `prompt` | string | — | Inline prompt (mutually exclusive with `promptFile`) |
|
|
474
|
+
| `promptFile` | string | — | Path to prompt file |
|
|
475
|
+
| `maxIterations` | number | 1 | Maximum iterations to run |
|
|
476
|
+
| `completionSignal` | string \| string[] | — | Substring(s) to stop the iteration loop early |
|
|
477
|
+
| `idleTimeoutSeconds` | number | 600 | Idle timeout in seconds |
|
|
478
|
+
| `completionTimeoutSeconds` | number | 60 | Grace window after completion signal is seen but agent process hasn't exited |
|
|
479
|
+
| `name` | string | — | Optional run name |
|
|
480
|
+
| `logging` | LoggingOption | file | Logging mode |
|
|
481
|
+
| `hooks` | SandboxHooks | — | Lifecycle hooks (`host.*`, `sandbox.*`) |
|
|
482
|
+
| `promptArgs` | PromptArgs | — | Key-value map for `{{KEY}}` placeholder substitution |
|
|
483
|
+
| `env` | Record<string, string> | — | Environment variables to inject into the sandbox |
|
|
484
|
+
| `resumeSession` | string | — | Resume a prior session by ID for agents that support resume. Incompatible with `maxIterations > 1`. Session file must exist on host. |
|
|
485
|
+
| `signal` | AbortSignal | — | Cancel the run when aborted. Kills the in-flight agent subprocess; the worktree is preserved on disk. Rejects with `signal.reason`. |
|
|
486
|
+
|
|
487
|
+
#### `WorktreeRunResult`
|
|
488
|
+
|
|
489
|
+
| Property | Type | Description |
|
|
490
|
+
| ------------------ | ------------------- | ------------------------------------------------------ |
|
|
491
|
+
| `iterations` | `IterationResult[]` | Per-iteration results (use `.length` for the count) |
|
|
492
|
+
| `completionSignal` | string | The matched completion signal, or undefined |
|
|
493
|
+
| `stdout` | string | Combined stdout output from all agent iterations |
|
|
494
|
+
| `commits` | { sha: string }[] | List of commits made by the agent during the run |
|
|
495
|
+
| `branch` | string | The branch name the agent worked on |
|
|
496
|
+
| `logFilePath` | string | Path to the log file, if logging was drained to a file |
|
|
497
|
+
|
|
498
|
+
#### `WorktreeCreateSandboxOptions`
|
|
499
|
+
|
|
500
|
+
| Option | Type | Default | Description |
|
|
501
|
+
| ---------------- | --------------- | ------- | ------------------------------------------------------------------------------------------------------------------- |
|
|
502
|
+
| `sandbox` | SandboxProvider | — | **Required.** Sandbox provider (e.g. `docker()`) |
|
|
503
|
+
| `hooks` | SandboxHooks | — | Lifecycle hooks (`host.*`, `sandbox.*`) |
|
|
504
|
+
| `copyToWorktree` | string[] | — | Host-relative file paths to copy into the worktree at creation time |
|
|
505
|
+
| `timeouts` | Timeouts | — | Override built-in lifecycle step timeouts (`copyToWorktreeMs`, `gitSetupMs`, `commitCollectionMs`, `mergeToHostMs`) |
|
|
506
|
+
|
|
507
|
+
## How it works
|
|
508
|
+
|
|
509
|
+
Sandcastle uses a **branch strategy** configured on the sandbox provider to control how the agent's changes relate to branches. There are three strategies:
|
|
510
|
+
|
|
511
|
+
- **Head** (`{ type: "head" }`) — The agent writes directly to the host working directory. No worktree, no branch indirection. This is the default for bind-mount providers like `docker()`.
|
|
512
|
+
- **Merge-to-head** (`{ type: "merge-to-head" }`) — Sandcastle creates a temporary branch in a git worktree. The agent works on the temp branch, and changes are merged back to HEAD when done. The temp branch is cleaned up after merge.
|
|
513
|
+
- **Branch** (`{ type: "branch", branch: "foo" }`) — Commits land on an explicitly named branch in a git worktree. Re-running with the same branch reuses the existing worktree and fast-forwards it from `origin` when safe — see [ADR 0003](docs/adr/0003-reuse-worktree-by-default.md).
|
|
514
|
+
|
|
515
|
+
For bind-mount providers (like Docker), the worktree directory is bind-mounted into the container — the agent writes directly to the host filesystem through the mount, so no sync is needed.
|
|
516
|
+
|
|
517
|
+
From your point of view, you just configure `branchStrategy: { type: 'branch', branch: 'foo' }` on `run()`, and get a commit on branch `foo` once it's complete. All 100% local.
|
|
518
|
+
|
|
519
|
+
## Prompts
|
|
520
|
+
|
|
521
|
+
Sandcastle uses a flexible prompt system. You write the prompt, and the engine executes it — no opinions about workflow, task management, or context sources are imposed.
|
|
522
|
+
|
|
523
|
+
### Prompt resolution
|
|
524
|
+
|
|
525
|
+
You must provide exactly one of:
|
|
526
|
+
|
|
527
|
+
1. `prompt: "inline string"` — pass an inline prompt directly via `RunOptions`
|
|
528
|
+
2. `promptFile: "./path/to/prompt.md"` — point to a specific file via `RunOptions`
|
|
529
|
+
|
|
530
|
+
`prompt` and `promptFile` are mutually exclusive — providing both is an error. If neither is provided, `run()` throws an error asking you to supply one.
|
|
531
|
+
|
|
532
|
+
**Inline prompts (`prompt: "..."`) are passed to the agent literally.** No `{{KEY}}` substitution, no `` !`command` `` expansion, no built-in `{{SOURCE_BRANCH}}` / `{{TARGET_BRANCH}}` injection. If you need values interpolated into an inline prompt, build the string in JavaScript (`` `Work on ${branch}…` ``). Passing `promptArgs` alongside an inline prompt is an error — switch to `promptFile` to use substitution.
|
|
533
|
+
|
|
534
|
+
The substitution and expansion features below apply **only** to prompts sourced from `promptFile`.
|
|
535
|
+
|
|
536
|
+
> **Convention**: `sandcastle init` scaffolds `.sandcastle/prompt.md` and all templates explicitly reference it via `promptFile: ".sandcastle/prompt.md"`. This is a convention, not an automatic fallback — Sandcastle does not read `.sandcastle/prompt.md` unless you pass it as `promptFile`.
|
|
537
|
+
|
|
538
|
+
### Dynamic context with `` !`command` ``
|
|
539
|
+
|
|
540
|
+
Use `` !`command` `` expressions in your prompt to pull in dynamic context. Each expression is replaced with the command's stdout before the prompt is sent to the agent. All expressions in a prompt run **in parallel** for faster expansion.
|
|
541
|
+
|
|
542
|
+
Commands run **inside the sandbox** after `sandbox.onSandboxReady` hooks complete, so they see the same repo state the agent sees (including installed dependencies).
|
|
543
|
+
|
|
544
|
+
```markdown
|
|
545
|
+
# Open issues
|
|
546
|
+
|
|
547
|
+
!`gh issue list --state open --label Sandcastle --json number,title,body,comments,labels --limit 100`
|
|
548
|
+
|
|
549
|
+
# Recent commits
|
|
550
|
+
|
|
551
|
+
!`git log --oneline -10`
|
|
552
|
+
```
|
|
553
|
+
|
|
554
|
+
If any command exits with a non-zero code, the run fails immediately with an error.
|
|
555
|
+
|
|
556
|
+
### Prompt arguments with `{{KEY}}`
|
|
557
|
+
|
|
558
|
+
Use `{{KEY}}` placeholders in your prompt to inject values from the `promptArgs` option. This is useful for reusing the same prompt file across multiple runs with different parameters.
|
|
559
|
+
|
|
560
|
+
```typescript
|
|
561
|
+
import { run } from "@ai-hero/sandcastle";
|
|
562
|
+
|
|
563
|
+
await run({
|
|
564
|
+
promptFile: "./my-prompt.md",
|
|
565
|
+
promptArgs: { ISSUE_NUMBER: 42, PRIORITY: "high" },
|
|
566
|
+
});
|
|
567
|
+
```
|
|
568
|
+
|
|
569
|
+
In the prompt file:
|
|
570
|
+
|
|
571
|
+
```markdown
|
|
572
|
+
Work on issue #{{ISSUE_NUMBER}} (priority: {{PRIORITY}}).
|
|
573
|
+
```
|
|
574
|
+
|
|
575
|
+
Prompt argument substitution runs on the host before shell expression expansion, so `{{KEY}}` placeholders inside `` !`command` `` expressions are replaced first:
|
|
576
|
+
|
|
577
|
+
```markdown
|
|
578
|
+
!`gh issue view {{ISSUE_NUMBER}} --json body -q .body`
|
|
579
|
+
```
|
|
580
|
+
|
|
581
|
+
A `{{KEY}}` placeholder with no matching prompt argument is an error. Unused prompt arguments produce a warning.
|
|
582
|
+
|
|
583
|
+
`` !`command` `` expansion only runs on shell blocks written in the prompt file itself. Any `` !`…` `` pattern that appears inside an argument value is treated as inert text — it won't be executed against the host shell. This makes it safe to pass user-authored content (issue titles, PR descriptions, docs excerpts) through `promptArgs`.
|
|
584
|
+
|
|
585
|
+
### Built-in prompt arguments
|
|
586
|
+
|
|
587
|
+
Sandcastle automatically injects two built-in prompt arguments into every prompt:
|
|
588
|
+
|
|
589
|
+
| Placeholder | Value |
|
|
590
|
+
| ------------------- | ----------------------------------------------------------------- |
|
|
591
|
+
| `{{SOURCE_BRANCH}}` | The branch the agent works on (determined by the branch strategy) |
|
|
592
|
+
| `{{TARGET_BRANCH}}` | The host's active branch at `run()` time |
|
|
593
|
+
|
|
594
|
+
Use them in your prompt without passing them via `promptArgs`:
|
|
595
|
+
|
|
596
|
+
```markdown
|
|
597
|
+
You are working on {{SOURCE_BRANCH}}. When diffing, compare against {{TARGET_BRANCH}}.
|
|
598
|
+
```
|
|
599
|
+
|
|
600
|
+
Passing `SOURCE_BRANCH` or `TARGET_BRANCH` in `promptArgs` is an error — built-in prompt arguments cannot be overridden.
|
|
601
|
+
|
|
602
|
+
### Early termination with `<promise>COMPLETE</promise>`
|
|
603
|
+
|
|
604
|
+
When the agent outputs `<promise>COMPLETE</promise>`, the orchestrator stops the iteration loop early. This is a convention you document in your prompt for the agent to follow — the engine never injects it.
|
|
605
|
+
|
|
606
|
+
This is useful for task-based workflows where the agent should stop once it has finished, rather than running all remaining iterations.
|
|
607
|
+
|
|
608
|
+
You can override the default signal by passing `completionSignal` to `run()`. It accepts a single string or an array of strings:
|
|
609
|
+
|
|
610
|
+
```ts
|
|
611
|
+
await run({
|
|
612
|
+
// ...
|
|
613
|
+
completionSignal: "DONE",
|
|
614
|
+
});
|
|
615
|
+
|
|
616
|
+
// Or pass multiple signals — the loop stops on the first match:
|
|
617
|
+
await run({
|
|
618
|
+
// ...
|
|
619
|
+
completionSignal: ["TASK_COMPLETE", "TASK_ABORTED"],
|
|
620
|
+
});
|
|
621
|
+
```
|
|
622
|
+
|
|
623
|
+
Tell the agent to output your chosen string(s) in the prompt, and the orchestrator will stop when it detects any of them. The matched signal is returned as `result.completionSignal`.
|
|
624
|
+
|
|
625
|
+
#### Hanging processes after the completion signal
|
|
626
|
+
|
|
627
|
+
The agent process is expected to exit shortly after emitting the completion signal. When a child it spawned — a `gh`/git subprocess, a long-lived MCP server, etc. — inherits the agent's stdout pipe and keeps it open, the parent process can linger long past its logical end. Sandcastle would otherwise wait for the full `idleTimeoutSeconds` and fail with `AgentIdleTimeoutError`, throwing away the commits the agent already made.
|
|
628
|
+
|
|
629
|
+
Instead, once the completion signal is observed in the output buffer, Sandcastle swaps in a short **completion timeout** (default 60 s). When it expires, the run resolves successfully with a warning that the process was hanging; `result.commits` and `result.completionSignal` are populated as if the process had exited cleanly. The timer resets on every subsequent output line, so trailing data emitted after the signal — token-usage events, terminal `result` events, a structured-output `<tag>` — is still captured.
|
|
630
|
+
|
|
631
|
+
A clean process exit always wins the race, so healthy runs gain zero added latency. The completion timeout only matters when the process hangs.
|
|
632
|
+
|
|
633
|
+
Tune the window with `completionTimeoutSeconds`:
|
|
634
|
+
|
|
635
|
+
```ts
|
|
636
|
+
await run({
|
|
637
|
+
// ...
|
|
638
|
+
completionTimeoutSeconds: 30, // shorter grace window
|
|
639
|
+
});
|
|
640
|
+
```
|
|
641
|
+
|
|
642
|
+
This is independent of `idleTimeoutSeconds`. They cover different phases: `idleTimeoutSeconds` runs **before** any signal is seen (genuinely stuck agent → fail); `completionTimeoutSeconds` runs **after** the signal is seen (hanging process → succeed with warning). See [ADR 0019](docs/adr/0019-completion-timeout-for-hanging-process.md).
|
|
643
|
+
|
|
644
|
+
### Structured output
|
|
645
|
+
|
|
646
|
+
Use `Output.object()` to extract a typed, schema-validated JSON payload from the agent's stdout. The agent emits its answer inside an XML tag you specify, and Sandcastle parses, validates, and returns it on `result.output`. The schema can be any [Standard Schema](https://standardschema.dev) validator — the examples below use [Zod](https://zod.dev), but Valibot, ArkType, and others work identically. See [ADR 0010](docs/adr/0010-structured-output.md) for design rationale.
|
|
647
|
+
|
|
648
|
+
```ts
|
|
649
|
+
import { run, Output, claudeCode } from "@ai-hero/sandcastle";
|
|
650
|
+
import { docker } from "@ai-hero/sandcastle/sandboxes/docker";
|
|
651
|
+
import { z } from "zod";
|
|
652
|
+
|
|
653
|
+
const result = await run({
|
|
654
|
+
agent: claudeCode("claude-opus-4-7"),
|
|
655
|
+
sandbox: docker(),
|
|
656
|
+
prompt: `Analyze the code, and output the result as JSON inside <result> tags.
|
|
657
|
+
The result must match this schema:
|
|
658
|
+
{ summary: string; score: string }
|
|
659
|
+
`,
|
|
660
|
+
output: Output.object({
|
|
661
|
+
tag: "result",
|
|
662
|
+
schema: z.object({ summary: z.string(), score: z.number() }),
|
|
663
|
+
}),
|
|
664
|
+
});
|
|
665
|
+
|
|
666
|
+
console.log(result.output.summary); // typed as string
|
|
667
|
+
console.log(result.output.score); // typed as number
|
|
668
|
+
```
|
|
669
|
+
|
|
670
|
+
`Output.string({ tag })` extracts the tag contents as a plain string (trimmed, no JSON parsing). Both helpers require `maxIterations` to be `1` (the default). The resolved prompt must contain the configured opening tag literal.
|
|
671
|
+
|
|
672
|
+
When extraction or validation fails, `run()` throws a `StructuredOutputError`. Alongside `tag`, `rawMatched`, `cause`, `commits`, `branch`, and `preservedWorktreePath`, the error carries the `sessionId` (and `sessionFilePath`, when the session was captured) of the run that produced the bad output. You can resume that session to ask the agent to re-emit corrected output, without repeating the work:
|
|
673
|
+
|
|
674
|
+
```ts
|
|
675
|
+
import { run, Output, StructuredOutputError } from "@ai-hero/sandcastle";
|
|
676
|
+
|
|
677
|
+
try {
|
|
678
|
+
return await run({ ...opts, output });
|
|
679
|
+
} catch (e) {
|
|
680
|
+
if (e instanceof StructuredOutputError && e.sessionId) {
|
|
681
|
+
return await run({
|
|
682
|
+
...opts,
|
|
683
|
+
output,
|
|
684
|
+
resumeSession: e.sessionId,
|
|
685
|
+
prompt: `Your previous output failed: ${e.message}. Re-emit it inside <${e.tag}> tags.`,
|
|
686
|
+
});
|
|
687
|
+
}
|
|
688
|
+
throw e;
|
|
689
|
+
}
|
|
690
|
+
```
|
|
691
|
+
|
|
692
|
+
### Templates
|
|
693
|
+
|
|
694
|
+
`sandcastle init` prompts you to choose a sandbox provider (Docker or Podman), an issue tracker (GitHub Issues, Beads, or Custom), and a template, which scaffolds a ready-to-use prompt and `main.mts` suited to a specific workflow. If your project's `package.json` has `"type": "module"`, the file will be named `main.ts` instead. Choosing **Custom** scaffolds the project in a deliberately broken-until-configured state plus a `.sandcastle/SETUP_ISSUE_TRACKER.md` prompt you feed to your coding agent, which wires up your own tracker by editing the scaffolded files in place. Five templates are available:
|
|
695
|
+
|
|
696
|
+
| Template | Description |
|
|
697
|
+
| ------------------------------ | ------------------------------------------------------------------------- |
|
|
698
|
+
| `blank` | Bare scaffold — write your own prompt and orchestration |
|
|
699
|
+
| `simple-loop` | Picks issues one by one and closes them |
|
|
700
|
+
| `sequential-reviewer` | Implements issues one by one, with a code review step after each |
|
|
701
|
+
| `parallel-planner` | Plans parallelizable issues, executes on separate branches, then merges |
|
|
702
|
+
| `parallel-planner-with-review` | Plans parallelizable issues, executes with per-branch review, then merges |
|
|
703
|
+
|
|
704
|
+
Select a template during `sandcastle init` when prompted, or re-run init in a fresh repo to try a different one.
|
|
705
|
+
|
|
706
|
+
## CLI commands
|
|
707
|
+
|
|
708
|
+
### `sandcastle init`
|
|
709
|
+
|
|
710
|
+
Scaffolds the `.sandcastle/` config directory and builds the container image. This is the first command you run in a new repo. You choose a sandbox provider (Docker or Podman) during init — selecting Podman writes a `Containerfile` instead of `Dockerfile` and uses `sandcastle podman build-image` for the build step.
|
|
711
|
+
|
|
712
|
+
Init detects your host package manager (npm, pnpm, yarn, or bun) from a `packageManager` field or lockfile, defaulting to npm. Templates whose `main` file imports a host dependency — the planner templates import [Zod](https://zod.dev) for their `<plan>` output schema — prompt you to install it with that package manager when it isn't already in your `package.json`, so the first `npx tsx .sandcastle/main.ts` doesn't fail with `ERR_MODULE_NOT_FOUND`.
|
|
713
|
+
|
|
714
|
+
Every interactive prompt has a paired `--flag` so the entire init can run non-interactively (e.g. in CI or a scripted setup). When stdin is not a TTY and a required flag is missing, init fails fast with a clear error rather than wedging on a prompt.
|
|
715
|
+
|
|
716
|
+
| Option | Required | Default | Description |
|
|
717
|
+
| ------------------------- | -------- | ---------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
718
|
+
| `--image-name` | No | `sandcastle:<repo-dir-name>` | Docker image name |
|
|
719
|
+
| `--agent` | No | Interactive multi-select | One or more agents, comma-separated (e.g. `claude-code,codex`). The first is the generated `agent()` default. Valid: `claude-code`, `pi`, `codex`, `cursor`, `opencode`, `copilot` |
|
|
720
|
+
| `--model` | No | Agent's default model | Pre-fills `AGENT_MODEL` in `.env.example` (e.g. `claude-sonnet-4-6`). Left commented out when unset |
|
|
721
|
+
| `--sandbox` | No | Interactive prompt | Sandbox provider to use (`docker`, `podman`) |
|
|
722
|
+
| `--template` | No | Interactive prompt | Template to scaffold (e.g. `blank`, `simple-loop`) |
|
|
723
|
+
| `--issue-tracker` | No | Interactive prompt | Issue tracker to use (`github-issues`, `beads`, `custom`) |
|
|
724
|
+
| `--create-label` | No | Interactive prompt | `true` / `false` — whether to create the `Sandcastle` GitHub label (only with `--issue-tracker github-issues`) |
|
|
725
|
+
| `--build-image` | No | Interactive prompt | `true` / `false` — whether to build the sandbox image now (silently ignored with `--issue-tracker custom`) |
|
|
726
|
+
| `--install-template-deps` | No | Interactive prompt | `true` / `false` — whether to install template host deps (e.g. `zod` for the planner templates) |
|
|
727
|
+
|
|
728
|
+
Creates the following files:
|
|
729
|
+
|
|
730
|
+
```
|
|
731
|
+
.sandcastle/
|
|
732
|
+
├── Dockerfile # Sandbox environment (customize as needed)
|
|
733
|
+
├── prompt.md # Agent instructions
|
|
734
|
+
├── .env.example # Token placeholders
|
|
735
|
+
└── .gitignore # Ignores .env, logs/
|
|
736
|
+
```
|
|
737
|
+
|
|
738
|
+
Errors if `.sandcastle/` already exists to prevent overwriting customizations.
|
|
739
|
+
|
|
740
|
+
### `sandcastle docker build-image`
|
|
741
|
+
|
|
742
|
+
Rebuilds the Docker image from an existing `.sandcastle/` directory. Use this after modifying the Dockerfile. On Linux/macOS, the build automatically passes `--build-arg AGENT_UID=$(id -u)` and `AGENT_GID=$(id -g)` so the image's `agent` user matches the host UID — this prevents permission errors on image-built files without runtime chown.
|
|
743
|
+
|
|
744
|
+
| Option | Required | Default | Description |
|
|
745
|
+
| -------------- | -------- | ---------------------------- | --------------------------------------------------------------------------------- |
|
|
746
|
+
| `--image-name` | No | `sandcastle:<repo-dir-name>` | Docker image name |
|
|
747
|
+
| `--dockerfile` | No | — | Path to a custom Dockerfile (build context will be the current working directory) |
|
|
748
|
+
|
|
749
|
+
### `sandcastle docker remove-image`
|
|
750
|
+
|
|
751
|
+
Removes the Docker image.
|
|
752
|
+
|
|
753
|
+
| Option | Required | Default | Description |
|
|
754
|
+
| -------------- | -------- | ---------------------------- | ----------------- |
|
|
755
|
+
| `--image-name` | No | `sandcastle:<repo-dir-name>` | Docker image name |
|
|
756
|
+
|
|
757
|
+
### `sandcastle podman build-image`
|
|
758
|
+
|
|
759
|
+
Builds the Podman image from an existing `.sandcastle/` directory. Use this after modifying the Containerfile.
|
|
760
|
+
|
|
761
|
+
| Option | Required | Default | Description |
|
|
762
|
+
| ----------------- | -------- | ---------------------------- | ------------------------------------------------------------------------------------ |
|
|
763
|
+
| `--image-name` | No | `sandcastle:<repo-dir-name>` | Podman image name |
|
|
764
|
+
| `--containerfile` | No | — | Path to a custom Containerfile (build context will be the current working directory) |
|
|
765
|
+
|
|
766
|
+
### `sandcastle podman remove-image`
|
|
767
|
+
|
|
768
|
+
Removes the Podman image.
|
|
769
|
+
|
|
770
|
+
| Option | Required | Default | Description |
|
|
771
|
+
| -------------- | -------- | ---------------------------- | ----------------- |
|
|
772
|
+
| `--image-name` | No | `sandcastle:<repo-dir-name>` | Podman image name |
|
|
773
|
+
|
|
774
|
+
### `RunOptions`
|
|
775
|
+
|
|
776
|
+
| Option | Type | Default | Description |
|
|
777
|
+
| -------------------------- | ------------------ | ----------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
778
|
+
| `agent` | AgentProvider | — | **Required.** Agent provider (e.g. `claudeCode("claude-opus-4-7")`, `pi("claude-sonnet-4-6")`, `codex("gpt-5.4-mini")`, `cursor("composer-2")`, `opencode("opencode/big-pickle")`, `copilot("claude-sonnet-4.5")`) |
|
|
779
|
+
| `sandbox` | SandboxProvider | — | **Required.** Sandbox provider (e.g. `docker()`, `podman()`, `docker({ imageName: "sandcastle:local" })`) |
|
|
780
|
+
| `cwd` | string | `process.cwd()` | Host repo directory — anchor for `.sandcastle/` artifacts and git operations. Relative paths resolve against `process.cwd()`. |
|
|
781
|
+
| `prompt` | string | — | Inline prompt (mutually exclusive with `promptFile`) |
|
|
782
|
+
| `promptFile` | string | — | Path to prompt file (mutually exclusive with `prompt`). Resolves against `process.cwd()`, **not** `cwd`. |
|
|
783
|
+
| `maxIterations` | number | `1` | Maximum iterations to run |
|
|
784
|
+
| `hooks` | SandboxHooks | — | Lifecycle hooks (`host.*`, `sandbox.*`) |
|
|
785
|
+
| `name` | string | — | Display name for the run, shown as a prefix in log output |
|
|
786
|
+
| `promptArgs` | PromptArgs | — | Key-value map for `{{KEY}}` placeholder substitution |
|
|
787
|
+
| `branchStrategy` | BranchStrategy | per-provider default | Branch strategy: `{ type: 'head' }`, `{ type: 'merge-to-head' }`, or `{ type: 'branch', branch: '…' }` |
|
|
788
|
+
| `copyToWorktree` | string[] | — | Host-relative file paths to copy into the sandbox before start (not supported with `branchStrategy: { type: 'head' }`) |
|
|
789
|
+
| `logging` | object | file (auto-generated) | `{ type: 'file', path }` or `{ type: 'stdout' }` |
|
|
790
|
+
| `completionSignal` | string \| string[] | `<promise>COMPLETE</promise>` | String or array of strings the agent emits to stop the iteration loop early |
|
|
791
|
+
| `idleTimeoutSeconds` | number | `600` | Idle timeout in seconds — resets on each agent output event |
|
|
792
|
+
| `completionTimeoutSeconds` | number | `60` | Grace window in seconds after the completion signal is observed but the agent process has not exited (hanging process). See [Hanging processes after the completion signal](#hanging-processes-after-the-completion-signal). |
|
|
793
|
+
| `resumeSession` | string | — | Resume a prior session by ID for agents that support resume. Incompatible with `maxIterations > 1`. Session file must exist on host. |
|
|
794
|
+
| `signal` | AbortSignal | — | Cancel the run when aborted. Kills the in-flight agent subprocess and cancels lifecycle hooks; the worktree is preserved on disk. Rejects with `signal.reason`. |
|
|
795
|
+
| `timeouts` | Timeouts | — | Override default timeouts for built-in lifecycle steps: `copyToWorktreeMs` (60 000), `gitSetupMs` (10 000), `commitCollectionMs` (30 000), `mergeToHostMs` (30 000). |
|
|
796
|
+
| `output` | OutputDefinition | — | Structured output definition (`Output.object(…)` or `Output.string(…)`). Requires `maxIterations === 1`. See [Structured output](#structured-output). |
|
|
797
|
+
|
|
798
|
+
### `RunResult`
|
|
799
|
+
|
|
800
|
+
| Field | Type | Description |
|
|
801
|
+
| ------------------ | ------------------- | ------------------------------------------------------------------ |
|
|
802
|
+
| `iterations` | `IterationResult[]` | Per-iteration results (use `.length` for the count) |
|
|
803
|
+
| `completionSignal` | string? | The matched completion signal string, or `undefined` if none fired |
|
|
804
|
+
| `stdout` | string | Agent output |
|
|
805
|
+
| `commits` | `{ sha }[]` | Commits created during the run |
|
|
806
|
+
| `branch` | string | Target branch name |
|
|
807
|
+
| `logFilePath` | string? | Path to the log file (only when logging to a file) |
|
|
808
|
+
| `output` | T? | Typed structured output (only present when `output` option is set) |
|
|
809
|
+
|
|
810
|
+
### `IterationResult`
|
|
811
|
+
|
|
812
|
+
| Field | Type | Description |
|
|
813
|
+
| ----------------- | ----------------- | ----------------------------------------------------------------------------------------------------------------------------------- |
|
|
814
|
+
| `sessionId` | string? | Agent session ID from the provider stream, or `undefined` if the provider does not emit one |
|
|
815
|
+
| `sessionFilePath` | string? | Absolute host path to the captured session JSONL, or `undefined` when capture is off |
|
|
816
|
+
| `usage` | `IterationUsage`? | Token usage snapshot from the last assistant message, or `undefined` when capture is off or provider does not support usage parsing |
|
|
817
|
+
|
|
818
|
+
### `IterationUsage`
|
|
819
|
+
|
|
820
|
+
| Field | Type | Description |
|
|
821
|
+
| -------------------------- | ------ | ------------------------------------------ |
|
|
822
|
+
| `inputTokens` | number | Input tokens consumed |
|
|
823
|
+
| `cacheCreationInputTokens` | number | Tokens used to create prompt cache entries |
|
|
824
|
+
| `cacheReadInputTokens` | number | Tokens read from prompt cache |
|
|
825
|
+
| `outputTokens` | number | Output tokens generated |
|
|
826
|
+
|
|
827
|
+
### Session capture
|
|
828
|
+
|
|
829
|
+
After each resumable provider iteration, Sandcastle automatically captures the agent's session file from the sandbox to the host. Claude Code sessions are stored under `~/.claude/projects/<encoded-path>/<session-id>.jsonl`; Codex sessions are stored under `~/.codex/sessions/YYYY/MM/DD/rollout-*-<session-id>.jsonl`; Pi sessions are stored under `~/.pi/agent/sessions/--<encoded-cwd>--/<timestamp>_<session-id>.jsonl`. Any provider-specific `cwd` fields are rewritten to match the host repo root, so the provider's native resume command works.
|
|
830
|
+
|
|
831
|
+
Session capture is enabled by default for `claudeCode()`, `codex()`, and `pi()` and can be opted out via `captureSessions: false`. Providers without `sessionStorage` do not attempt capture. Capture failure fails the run.
|
|
832
|
+
|
|
833
|
+
### Session resume
|
|
834
|
+
|
|
835
|
+
Pass `resumeSession` to `run()` to continue a prior Claude Code, Codex, or Pi conversation inside a new sandbox:
|
|
836
|
+
|
|
837
|
+
```typescript
|
|
838
|
+
const result = await run({
|
|
839
|
+
agent: claudeCode("claude-opus-4-7"),
|
|
840
|
+
sandbox: docker(),
|
|
841
|
+
prompt: "Continue where you left off",
|
|
842
|
+
resumeSession: "abc-123-def",
|
|
843
|
+
});
|
|
844
|
+
```
|
|
845
|
+
|
|
846
|
+
You can also continue the last captured session from a result:
|
|
847
|
+
|
|
848
|
+
```typescript
|
|
849
|
+
const first = await run({
|
|
850
|
+
agent: codex("gpt-5.4-mini"),
|
|
851
|
+
sandbox: docker(),
|
|
852
|
+
prompt: "Draft a plan",
|
|
853
|
+
});
|
|
854
|
+
|
|
855
|
+
const second = await first.resume?.("Now implement the plan");
|
|
856
|
+
```
|
|
857
|
+
|
|
858
|
+
`resume` is present only on results from resumable providers (Claude Code, Codex, Pi) — hence the optional-chaining call.
|
|
859
|
+
|
|
860
|
+
Before the sandbox starts, Sandcastle validates that the session file exists on the host and transfers it into the sandbox with `cwd` fields rewritten to match the sandbox-side path. Claude Code receives `--resume <id>`; Codex receives `codex exec resume <id>` with the prompt piped over stdin; Pi receives `--session <id>`.
|
|
861
|
+
|
|
862
|
+
Constraints:
|
|
863
|
+
|
|
864
|
+
- `resumeSession` is incompatible with `maxIterations > 1` (throws before sandbox creation).
|
|
865
|
+
- The provider's host session file must exist (throws before sandbox creation).
|
|
866
|
+
- Only iteration 1 receives the resume flag; subsequent iterations (if any) start fresh.
|
|
867
|
+
- Providers without resume support reject `resumeSession`.
|
|
868
|
+
|
|
869
|
+
### Session fork
|
|
870
|
+
|
|
871
|
+
`RunResult.fork(prompt, options?)` is the sibling of `.resume()`: it continues from the last captured session but leaves the parent session JSONL untouched and writes the child under a new session id. The mechanism is the agent's native fork flag — `claude --resume <id> --fork-session` for Claude Code, `codex exec fork <id>` for Codex.
|
|
872
|
+
|
|
873
|
+
Fork enables fan-out workflows where a single parent run is the starting point for several independent children:
|
|
874
|
+
|
|
875
|
+
```typescript
|
|
876
|
+
const parent = await run({
|
|
877
|
+
agent: claudeCode("claude-opus-4-7"),
|
|
878
|
+
sandbox: docker(),
|
|
879
|
+
prompt: "Read the codebase and summarise the data model",
|
|
880
|
+
});
|
|
881
|
+
|
|
882
|
+
const [reviewA, reviewB] = await Promise.all([
|
|
883
|
+
parent.fork?.("Review the migration plan", {
|
|
884
|
+
branchStrategy: { type: "branch", branch: "review-a" },
|
|
885
|
+
}),
|
|
886
|
+
parent.fork?.("Audit the auth layer", {
|
|
887
|
+
branchStrategy: { type: "branch", branch: "review-b" },
|
|
888
|
+
}),
|
|
889
|
+
]);
|
|
890
|
+
```
|
|
891
|
+
|
|
892
|
+
**Fork is session-only.** `--fork-session` and `codex exec fork` isolate the agent session JSONL — they do **not** isolate the branch, worktree, or sandbox. Safe concurrent fan-out (`Promise.all([r.fork(a), r.fork(b)])`) requires the caller to give each child a distinct `branch` via `branchStrategy: { type: "branch", branch: "..." }`. The default `head` and `merge-to-head` strategies are **not** safe for concurrent forks: `head` shares the host working directory across all children, and `merge-to-head` races `git merge` against the same HEAD. See [ADR 0018](docs/adr/0018-fork-is-session-only.md).
|
|
893
|
+
|
|
894
|
+
`fork` is present only on results from providers with `sessionStorage` (Claude Code, Codex) — hence the optional-chaining call. The same single-iteration and session-file constraints as `.resume()` apply.
|
|
895
|
+
|
|
896
|
+
### Agent selection
|
|
897
|
+
|
|
898
|
+
Use `agent()` when the provider should be selected at runtime rather than hard-coded in `main.ts`. The resolver reads `AGENT` to choose a provider, reads `AGENT_MODEL` to override that provider's default model, and falls back to `default` when `AGENT` is unset:
|
|
899
|
+
|
|
900
|
+
```typescript
|
|
901
|
+
import { agent, run } from "@ai-hero/sandcastle";
|
|
902
|
+
|
|
903
|
+
await run({
|
|
904
|
+
agent: agent({
|
|
905
|
+
default: "claude-code",
|
|
906
|
+
codex: { effort: "high" },
|
|
907
|
+
}),
|
|
908
|
+
prompt: "Fix issue #42",
|
|
909
|
+
});
|
|
910
|
+
```
|
|
911
|
+
|
|
912
|
+
`sandcastle init` scaffolds this shape from the agent selection step. In the interactive flow, choose one or more agents with the multi-select prompt; non-interactive init can pass a comma-separated `--agent claude-code,codex` value. The first selected agent becomes the generated `agent({ default })` provider. Passing `--model` pre-fills `AGENT_MODEL` in `.env.example`, and users can later switch among the selected, installed providers by setting `AGENT` without editing the generated script.
|
|
913
|
+
|
|
914
|
+
### `ClaudeCodeOptions`
|
|
915
|
+
|
|
916
|
+
The `claudeCode()` factory accepts an optional second argument for provider-specific options:
|
|
917
|
+
|
|
918
|
+
```typescript
|
|
919
|
+
agent: claudeCode("claude-opus-4-7", { effort: "high" });
|
|
920
|
+
```
|
|
921
|
+
|
|
922
|
+
| Option | Type | Default | Description |
|
|
923
|
+
| ----------------- | --------------------------------------------------------- | ------- | --------------------------------------------------------- |
|
|
924
|
+
| `effort` | `"low"` \| `"medium"` \| `"high"` \| `"xhigh"` \| `"max"` | — | Claude Code reasoning effort level (`max` is Opus only) |
|
|
925
|
+
| `env` | `Record<string, string>` | `{}` | Environment variables injected by this agent provider |
|
|
926
|
+
| `captureSessions` | `boolean` | `true` | Capture agent session JSONL to host for `claude --resume` |
|
|
927
|
+
|
|
928
|
+
### `CodexOptions`
|
|
929
|
+
|
|
930
|
+
The `codex()` factory accepts an optional second argument for provider-specific options:
|
|
931
|
+
|
|
932
|
+
```typescript
|
|
933
|
+
agent: codex("gpt-5.4", { effort: "high" });
|
|
934
|
+
```
|
|
935
|
+
|
|
936
|
+
| Option | Type | Default | Description |
|
|
937
|
+
| ----------------- | ---------------------------------------------- | ------- | --------------------------------------------------------- |
|
|
938
|
+
| `effort` | `"low"` \| `"medium"` \| `"high"` \| `"xhigh"` | — | Codex reasoning effort level via `model_reasoning_effort` |
|
|
939
|
+
| `env` | `Record<string, string>` | `{}` | Environment variables injected by this agent provider |
|
|
940
|
+
| `captureSessions` | `boolean` | `true` | Capture Codex rollout JSONL to host for resume |
|
|
941
|
+
|
|
942
|
+
### `PiOptions`
|
|
943
|
+
|
|
944
|
+
The `pi()` factory accepts an optional second argument for provider-specific options:
|
|
945
|
+
|
|
946
|
+
```typescript
|
|
947
|
+
agent: pi("claude-sonnet-4-6", { thinking: "high" });
|
|
948
|
+
```
|
|
949
|
+
|
|
950
|
+
| Option | Type | Default | Description |
|
|
951
|
+
| ----------------- | ------------------------------------------------------------------------ | ------- | -------------------------------------------------------- |
|
|
952
|
+
| `thinking` | `"off"` \| `"minimal"` \| `"low"` \| `"medium"` \| `"high"` \| `"xhigh"` | — | Pi reasoning effort level via the `--thinking` flag |
|
|
953
|
+
| `env` | `Record<string, string>` | `{}` | Environment variables injected by this agent provider |
|
|
954
|
+
| `captureSessions` | `boolean` | `true` | Capture pi session JSONL to host for `pi --session <id>` |
|
|
955
|
+
|
|
956
|
+
### Provider `env`
|
|
957
|
+
|
|
958
|
+
Both **agent providers** and **sandbox providers** accept an optional `env: Record<string, string>` in their options. These environment variables are merged with the `.sandcastle/.env` resolver output at launch time:
|
|
959
|
+
|
|
960
|
+
```typescript
|
|
961
|
+
await run({
|
|
962
|
+
agent: claudeCode("claude-opus-4-7", {
|
|
963
|
+
env: { ANTHROPIC_API_KEY: "sk-ant-..." },
|
|
964
|
+
}),
|
|
965
|
+
sandbox: docker({
|
|
966
|
+
env: { DOCKER_SPECIFIC_VAR: "value" },
|
|
967
|
+
}),
|
|
968
|
+
prompt: "Fix issue #42",
|
|
969
|
+
});
|
|
970
|
+
```
|
|
971
|
+
|
|
972
|
+
**Merge rules:**
|
|
973
|
+
|
|
974
|
+
- Provider env (agent + sandbox) overrides `.sandcastle/.env` resolver output for shared keys
|
|
975
|
+
- Agent provider env and sandbox provider env **must not overlap** — if they share any key, `run()` throws an error
|
|
976
|
+
- When `env` is not provided, it defaults to `{}`
|
|
977
|
+
|
|
978
|
+
Environment variables are also resolved automatically from `.sandcastle/.env` and `process.env` — no need to pass them to the API. The required variables depend on the **agent provider** (see `sandcastle init` output for details).
|
|
979
|
+
|
|
980
|
+
## Custom Sandbox Providers
|
|
981
|
+
|
|
982
|
+
Sandcastle ships with built-in providers for Docker, Podman, and Vercel, but you can create your own. A sandbox provider tells Sandcastle how to execute commands in an isolated environment. There are two kinds:
|
|
983
|
+
|
|
984
|
+
- **Bind-mount** — the sandbox can mount a host directory. Sandcastle creates a worktree on the host and the provider mounts it in. No file sync needed. Use this for Docker, Podman, or any local container runtime.
|
|
985
|
+
- **Isolated** — the sandbox has its own filesystem (e.g. a cloud VM). The provider handles syncing code in and out via `copyIn` and `copyFileOut`. Use this when the sandbox cannot access the host filesystem.
|
|
986
|
+
|
|
987
|
+
### The sandbox handle contract
|
|
988
|
+
|
|
989
|
+
Both provider types return a **sandbox handle** from their `create()` function. The handle exposes:
|
|
990
|
+
|
|
991
|
+
| Method | Required | Description |
|
|
992
|
+
| -------------- | ---------- | ---------------------------------------------------------------------------- |
|
|
993
|
+
| `exec` | Both | Run a command, optionally streaming stdout line-by-line via `options.onLine` |
|
|
994
|
+
| `close` | Both | Tear down the sandbox |
|
|
995
|
+
| `copyFileIn` | Bind-mount | Copy a single file from the host into the sandbox |
|
|
996
|
+
| `copyFileOut` | Both | Copy a single file from the sandbox to the host |
|
|
997
|
+
| `copyIn` | Isolated | Copy a file or directory from the host into the sandbox |
|
|
998
|
+
| `worktreePath` | Both | Absolute path to the repo directory inside the sandbox |
|
|
999
|
+
|
|
1000
|
+
### `ExecResult`
|
|
1001
|
+
|
|
1002
|
+
Every `exec` call returns an `ExecResult`:
|
|
1003
|
+
|
|
1004
|
+
```typescript
|
|
1005
|
+
interface ExecResult {
|
|
1006
|
+
readonly stdout: string;
|
|
1007
|
+
readonly stderr: string;
|
|
1008
|
+
readonly exitCode: number;
|
|
1009
|
+
}
|
|
1010
|
+
```
|
|
1011
|
+
|
|
1012
|
+
### Bind-mount provider example
|
|
1013
|
+
|
|
1014
|
+
A minimal bind-mount provider that shells out to local processes (no container):
|
|
1015
|
+
|
|
1016
|
+
```typescript
|
|
1017
|
+
import {
|
|
1018
|
+
createBindMountSandboxProvider,
|
|
1019
|
+
type BindMountCreateOptions,
|
|
1020
|
+
type BindMountSandboxHandle,
|
|
1021
|
+
type ExecResult,
|
|
1022
|
+
} from "@ai-hero/sandcastle";
|
|
1023
|
+
import { execFile, spawn } from "node:child_process";
|
|
1024
|
+
import { copyFile as fsCopyFile, mkdir as fsMkdir } from "node:fs/promises";
|
|
1025
|
+
import { dirname } from "node:path";
|
|
1026
|
+
import { createInterface } from "node:readline";
|
|
1027
|
+
|
|
1028
|
+
const localProcess = () =>
|
|
1029
|
+
createBindMountSandboxProvider({
|
|
1030
|
+
name: "local-process",
|
|
1031
|
+
create: async (
|
|
1032
|
+
options: BindMountCreateOptions,
|
|
1033
|
+
): Promise<BindMountSandboxHandle> => {
|
|
1034
|
+
const worktreePath = options.worktreePath;
|
|
1035
|
+
|
|
1036
|
+
return {
|
|
1037
|
+
worktreePath,
|
|
1038
|
+
|
|
1039
|
+
exec: (
|
|
1040
|
+
command: string,
|
|
1041
|
+
opts?: { onLine?: (line: string) => void; cwd?: string },
|
|
1042
|
+
): Promise<ExecResult> => {
|
|
1043
|
+
if (opts?.onLine) {
|
|
1044
|
+
const onLine = opts.onLine;
|
|
1045
|
+
return new Promise((resolve, reject) => {
|
|
1046
|
+
const proc = spawn("sh", ["-c", command], {
|
|
1047
|
+
cwd: opts?.cwd ?? worktreePath,
|
|
1048
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
1049
|
+
});
|
|
1050
|
+
|
|
1051
|
+
const stdoutChunks: string[] = [];
|
|
1052
|
+
const stderrChunks: string[] = [];
|
|
1053
|
+
|
|
1054
|
+
const rl = createInterface({ input: proc.stdout! });
|
|
1055
|
+
rl.on("line", (line) => {
|
|
1056
|
+
stdoutChunks.push(line);
|
|
1057
|
+
onLine(line); // forward each line to Sandcastle
|
|
1058
|
+
});
|
|
1059
|
+
|
|
1060
|
+
proc.stderr!.on("data", (chunk: Buffer) => {
|
|
1061
|
+
stderrChunks.push(chunk.toString());
|
|
1062
|
+
});
|
|
1063
|
+
|
|
1064
|
+
proc.on("error", (err) => reject(err));
|
|
1065
|
+
proc.on("close", (code) => {
|
|
1066
|
+
resolve({
|
|
1067
|
+
stdout: stdoutChunks.join("\n"),
|
|
1068
|
+
stderr: stderrChunks.join(""),
|
|
1069
|
+
exitCode: code ?? 0,
|
|
1070
|
+
});
|
|
1071
|
+
});
|
|
1072
|
+
});
|
|
1073
|
+
}
|
|
1074
|
+
|
|
1075
|
+
return new Promise((resolve, reject) => {
|
|
1076
|
+
execFile(
|
|
1077
|
+
"sh",
|
|
1078
|
+
["-c", command],
|
|
1079
|
+
{ cwd: opts?.cwd ?? worktreePath, maxBuffer: 10 * 1024 * 1024 },
|
|
1080
|
+
(error, stdout, stderr) => {
|
|
1081
|
+
if (error && error.code === undefined) {
|
|
1082
|
+
reject(new Error(`exec failed: ${error.message}`));
|
|
1083
|
+
} else {
|
|
1084
|
+
resolve({
|
|
1085
|
+
stdout: stdout.toString(),
|
|
1086
|
+
stderr: stderr.toString(),
|
|
1087
|
+
exitCode: typeof error?.code === "number" ? error.code : 0,
|
|
1088
|
+
});
|
|
1089
|
+
}
|
|
1090
|
+
},
|
|
1091
|
+
);
|
|
1092
|
+
});
|
|
1093
|
+
},
|
|
1094
|
+
|
|
1095
|
+
copyFileIn: async (hostPath: string, sandboxPath: string) => {
|
|
1096
|
+
await fsMkdir(dirname(sandboxPath), { recursive: true });
|
|
1097
|
+
await fsCopyFile(hostPath, sandboxPath);
|
|
1098
|
+
},
|
|
1099
|
+
|
|
1100
|
+
copyFileOut: async (sandboxPath: string, hostPath: string) => {
|
|
1101
|
+
await fsMkdir(dirname(hostPath), { recursive: true });
|
|
1102
|
+
await fsCopyFile(sandboxPath, hostPath);
|
|
1103
|
+
},
|
|
1104
|
+
|
|
1105
|
+
close: async () => {
|
|
1106
|
+
// nothing to tear down for a local process
|
|
1107
|
+
},
|
|
1108
|
+
};
|
|
1109
|
+
},
|
|
1110
|
+
});
|
|
1111
|
+
```
|
|
1112
|
+
|
|
1113
|
+
### Isolated provider example
|
|
1114
|
+
|
|
1115
|
+
A minimal isolated provider using a temp directory:
|
|
1116
|
+
|
|
1117
|
+
```typescript
|
|
1118
|
+
import {
|
|
1119
|
+
createIsolatedSandboxProvider,
|
|
1120
|
+
type IsolatedSandboxHandle,
|
|
1121
|
+
type ExecResult,
|
|
1122
|
+
} from "@ai-hero/sandcastle";
|
|
1123
|
+
import { execFile, spawn } from "node:child_process";
|
|
1124
|
+
import { copyFile, mkdir, mkdtemp, rm } from "node:fs/promises";
|
|
1125
|
+
import { tmpdir } from "node:os";
|
|
1126
|
+
import { dirname, join } from "node:path";
|
|
1127
|
+
import { createInterface } from "node:readline";
|
|
1128
|
+
|
|
1129
|
+
const tempDir = () =>
|
|
1130
|
+
createIsolatedSandboxProvider({
|
|
1131
|
+
name: "temp-dir",
|
|
1132
|
+
create: async (): Promise<IsolatedSandboxHandle> => {
|
|
1133
|
+
const root = await mkdtemp(join(tmpdir(), "sandbox-"));
|
|
1134
|
+
const worktreePath = join(root, "workspace");
|
|
1135
|
+
await mkdir(worktreePath, { recursive: true });
|
|
1136
|
+
|
|
1137
|
+
return {
|
|
1138
|
+
worktreePath,
|
|
1139
|
+
|
|
1140
|
+
exec: (
|
|
1141
|
+
command: string,
|
|
1142
|
+
opts?: { onLine?: (line: string) => void; cwd?: string },
|
|
1143
|
+
): Promise<ExecResult> => {
|
|
1144
|
+
if (opts?.onLine) {
|
|
1145
|
+
const onLine = opts.onLine;
|
|
1146
|
+
return new Promise((resolve, reject) => {
|
|
1147
|
+
const proc = spawn("sh", ["-c", command], {
|
|
1148
|
+
cwd: opts?.cwd ?? worktreePath,
|
|
1149
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
1150
|
+
});
|
|
1151
|
+
|
|
1152
|
+
const stdoutChunks: string[] = [];
|
|
1153
|
+
const stderrChunks: string[] = [];
|
|
1154
|
+
|
|
1155
|
+
const rl = createInterface({ input: proc.stdout! });
|
|
1156
|
+
rl.on("line", (line) => {
|
|
1157
|
+
stdoutChunks.push(line);
|
|
1158
|
+
onLine(line);
|
|
1159
|
+
});
|
|
1160
|
+
|
|
1161
|
+
proc.stderr!.on("data", (chunk: Buffer) => {
|
|
1162
|
+
stderrChunks.push(chunk.toString());
|
|
1163
|
+
});
|
|
1164
|
+
|
|
1165
|
+
proc.on("error", (err) => reject(err));
|
|
1166
|
+
proc.on("close", (code) => {
|
|
1167
|
+
resolve({
|
|
1168
|
+
stdout: stdoutChunks.join("\n"),
|
|
1169
|
+
stderr: stderrChunks.join(""),
|
|
1170
|
+
exitCode: code ?? 0,
|
|
1171
|
+
});
|
|
1172
|
+
});
|
|
1173
|
+
});
|
|
1174
|
+
}
|
|
1175
|
+
|
|
1176
|
+
return new Promise((resolve, reject) => {
|
|
1177
|
+
execFile(
|
|
1178
|
+
"sh",
|
|
1179
|
+
["-c", command],
|
|
1180
|
+
{ cwd: opts?.cwd ?? worktreePath, maxBuffer: 10 * 1024 * 1024 },
|
|
1181
|
+
(error, stdout, stderr) => {
|
|
1182
|
+
if (error && error.code === undefined) {
|
|
1183
|
+
reject(new Error(`exec failed: ${error.message}`));
|
|
1184
|
+
} else {
|
|
1185
|
+
resolve({
|
|
1186
|
+
stdout: stdout.toString(),
|
|
1187
|
+
stderr: stderr.toString(),
|
|
1188
|
+
exitCode: typeof error?.code === "number" ? error.code : 0,
|
|
1189
|
+
});
|
|
1190
|
+
}
|
|
1191
|
+
},
|
|
1192
|
+
);
|
|
1193
|
+
});
|
|
1194
|
+
},
|
|
1195
|
+
|
|
1196
|
+
copyIn: async (hostPath: string, sandboxPath: string) => {
|
|
1197
|
+
const info = await stat(hostPath);
|
|
1198
|
+
if (info.isDirectory()) {
|
|
1199
|
+
await cp(hostPath, sandboxPath, { recursive: true });
|
|
1200
|
+
} else {
|
|
1201
|
+
await mkdir(dirname(sandboxPath), { recursive: true });
|
|
1202
|
+
await copyFile(hostPath, sandboxPath);
|
|
1203
|
+
}
|
|
1204
|
+
},
|
|
1205
|
+
|
|
1206
|
+
copyFileOut: async (sandboxPath: string, hostPath: string) => {
|
|
1207
|
+
await mkdir(dirname(hostPath), { recursive: true });
|
|
1208
|
+
await copyFile(sandboxPath, hostPath);
|
|
1209
|
+
},
|
|
1210
|
+
|
|
1211
|
+
close: async () => {
|
|
1212
|
+
await rm(root, { recursive: true, force: true });
|
|
1213
|
+
},
|
|
1214
|
+
};
|
|
1215
|
+
},
|
|
1216
|
+
});
|
|
1217
|
+
```
|
|
1218
|
+
|
|
1219
|
+
### Branch strategies
|
|
1220
|
+
|
|
1221
|
+
A branch strategy controls where the agent's commits land. Configure it when constructing the provider:
|
|
1222
|
+
|
|
1223
|
+
| Strategy | Behavior | Bind-mount | Isolated |
|
|
1224
|
+
| --------------- | ------------------------------------------------------------------------ | ---------- | --------- |
|
|
1225
|
+
| `head` | Agent writes directly to the host working directory. No worktree created | Default | N/A |
|
|
1226
|
+
| `merge-to-head` | Sandcastle creates a temp branch, merges back to HEAD when done | Supported | Default |
|
|
1227
|
+
| `branch` | Commits land on an explicit named branch you provide | Supported | Supported |
|
|
1228
|
+
|
|
1229
|
+
**When to use each:**
|
|
1230
|
+
|
|
1231
|
+
- **`head`** — fast iteration during development. No branch indirection, no merge step. Only works with bind-mount providers since the agent needs direct host filesystem access.
|
|
1232
|
+
- **`merge-to-head`** — safe default for automation. The agent works on a throwaway branch; if something goes wrong, HEAD is untouched. Use this for CI or unattended runs.
|
|
1233
|
+
- **`branch`** — when you want commits on a specific branch (e.g. for a PR). Pass `{ type: "branch", branch: "agent/fix-42" }`.
|
|
1234
|
+
|
|
1235
|
+
Branch strategy is now configured on `run()`, not on the provider:
|
|
1236
|
+
|
|
1237
|
+
```typescript
|
|
1238
|
+
import { run, claudeCode } from "@ai-hero/sandcastle";
|
|
1239
|
+
import { docker } from "@ai-hero/sandcastle/sandboxes/docker";
|
|
1240
|
+
|
|
1241
|
+
// head — direct write, bind-mount only (default for bind-mount providers)
|
|
1242
|
+
await run({
|
|
1243
|
+
agent: claudeCode("claude-opus-4-7"),
|
|
1244
|
+
sandbox: docker(),
|
|
1245
|
+
prompt: "…",
|
|
1246
|
+
});
|
|
1247
|
+
// merge-to-head — temp branch, merge back (default for isolated providers)
|
|
1248
|
+
await run({
|
|
1249
|
+
agent: claudeCode("claude-opus-4-7"),
|
|
1250
|
+
sandbox: tempDir(),
|
|
1251
|
+
prompt: "…",
|
|
1252
|
+
});
|
|
1253
|
+
// branch — explicit named branch
|
|
1254
|
+
await run({
|
|
1255
|
+
agent: claudeCode("claude-opus-4-7"),
|
|
1256
|
+
sandbox: docker(),
|
|
1257
|
+
branchStrategy: { type: "branch", branch: "agent/fix-42" },
|
|
1258
|
+
prompt: "…",
|
|
1259
|
+
});
|
|
1260
|
+
```
|
|
1261
|
+
|
|
1262
|
+
### Passing to `run()`
|
|
1263
|
+
|
|
1264
|
+
Pass your custom provider via the `sandbox` option — it works the same as the built-in `docker()` provider:
|
|
1265
|
+
|
|
1266
|
+
```typescript
|
|
1267
|
+
import { run, claudeCode } from "@ai-hero/sandcastle";
|
|
1268
|
+
|
|
1269
|
+
const result = await run({
|
|
1270
|
+
agent: claudeCode("claude-opus-4-7"),
|
|
1271
|
+
sandbox: localProcess(), // your custom provider
|
|
1272
|
+
prompt: "Fix issue #42 in this repo.",
|
|
1273
|
+
});
|
|
1274
|
+
```
|
|
1275
|
+
|
|
1276
|
+
### Reference implementations
|
|
1277
|
+
|
|
1278
|
+
For real-world examples, see:
|
|
1279
|
+
|
|
1280
|
+
- [`src/sandboxes/docker.ts`](src/sandboxes/docker.ts) — bind-mount provider using Docker containers (with SELinux label support)
|
|
1281
|
+
- [`src/sandboxes/vercel.ts`](src/sandboxes/vercel.ts) — isolated provider using Vercel Firecracker microVMs via `@vercel/sandbox`
|
|
1282
|
+
- [`src/sandboxes/podman.ts`](src/sandboxes/podman.ts) — bind-mount provider using Podman containers (with SELinux label support)
|
|
1283
|
+
- [`src/sandboxes/test-isolated.ts`](src/sandboxes/test-isolated.ts) — isolated provider using temp directories (used in tests)
|
|
1284
|
+
|
|
1285
|
+
## Configuration
|
|
1286
|
+
|
|
1287
|
+
### Config directory (`.sandcastle/`)
|
|
1288
|
+
|
|
1289
|
+
All per-repo sandbox configuration lives in `.sandcastle/`. Run `sandcastle init` to create it.
|
|
1290
|
+
|
|
1291
|
+
### Custom Dockerfile
|
|
1292
|
+
|
|
1293
|
+
The `.sandcastle/Dockerfile` controls the sandbox environment. The default template installs:
|
|
1294
|
+
|
|
1295
|
+
- **Node.js 22** (base image)
|
|
1296
|
+
- **git**, **curl**, **jq** (system dependencies)
|
|
1297
|
+
- **GitHub CLI** (`gh`)
|
|
1298
|
+
- **Claude Code CLI**
|
|
1299
|
+
- A non-root `agent` user (required — Claude runs as this user)
|
|
1300
|
+
|
|
1301
|
+
When customizing the Dockerfile, ensure you keep:
|
|
1302
|
+
|
|
1303
|
+
- A non-root user (the default `agent` user) for Claude to run as
|
|
1304
|
+
- `git` (required for commits and branch operations)
|
|
1305
|
+
- `gh` (required for issue fetching)
|
|
1306
|
+
- Claude Code CLI installed and on PATH
|
|
1307
|
+
|
|
1308
|
+
Add your project-specific dependencies (e.g., language runtimes, build tools) to the Dockerfile as needed.
|
|
1309
|
+
|
|
1310
|
+
### Hooks
|
|
1311
|
+
|
|
1312
|
+
Hooks are grouped by **where** they run — `host` (on the developer's machine) or `sandbox` (inside the container):
|
|
1313
|
+
|
|
1314
|
+
```ts
|
|
1315
|
+
hooks: {
|
|
1316
|
+
host: {
|
|
1317
|
+
onWorktreeReady: [{ command: "cp .env.example .env" }],
|
|
1318
|
+
onSandboxReady: [{ command: "echo sandbox is up" }],
|
|
1319
|
+
},
|
|
1320
|
+
sandbox: {
|
|
1321
|
+
onSandboxReady: [
|
|
1322
|
+
{ command: "npm install", timeoutMs: 300_000 },
|
|
1323
|
+
{ command: "apt-get install -y ffmpeg", sudo: true },
|
|
1324
|
+
],
|
|
1325
|
+
},
|
|
1326
|
+
}
|
|
1327
|
+
```
|
|
1328
|
+
|
|
1329
|
+
| Hook | Runs on | When | Working directory |
|
|
1330
|
+
| ------------------------ | ------- | -------------------------------------------- | ------------------------------------------- |
|
|
1331
|
+
| `host.onWorktreeReady` | Host | After `copyToWorktree`, before sandbox start | Worktree path (host repo root under `head`) |
|
|
1332
|
+
| `host.onSandboxReady` | Host | After sandbox is up | Worktree path (host repo root under `head`) |
|
|
1333
|
+
| `sandbox.onSandboxReady` | Sandbox | After sandbox is up | Sandbox repo directory |
|
|
1334
|
+
|
|
1335
|
+
**Ordering:** `copyToWorktree` -> `host.onWorktreeReady` (sequential) -> sandbox created -> `host.onSandboxReady` + `sandbox.onSandboxReady` (parallel).
|
|
1336
|
+
|
|
1337
|
+
- **Host hooks** accept `{ command: string; timeoutMs?: number }` — no `sudo`, no `cwd`. Use `cd` or inline env in the command string.
|
|
1338
|
+
- **Sandbox hooks** accept `{ command: string; sudo?: boolean; timeoutMs?: number }` — set `sudo: true` for elevated privileges.
|
|
1339
|
+
- **`timeoutMs`** overrides the default 60 s per-hook timeout. Useful for long-running setup commands like dependency installs (e.g. `timeoutMs: 300_000` for 5 minutes).
|
|
1340
|
+
- Within each hook point, sandbox hooks run in parallel; host hooks within `onSandboxReady` also run in parallel with sandbox hooks. `host.onWorktreeReady` hooks run sequentially in declared order.
|
|
1341
|
+
- If any hook exits non-zero, setup fails fast.
|
|
1342
|
+
- When a `signal` is passed to `run()`, it is threaded to all hooks — aborting the signal cancels any in-flight hook commands.
|
|
1343
|
+
|
|
1344
|
+
## Development
|
|
1345
|
+
|
|
1346
|
+
```bash
|
|
1347
|
+
npm install
|
|
1348
|
+
npm run build # Bundle with tsup
|
|
1349
|
+
npm test # Run tests with vitest
|
|
1350
|
+
npm run typecheck # Type-check
|
|
1351
|
+
```
|
|
1352
|
+
|
|
1353
|
+
## License
|
|
1354
|
+
|
|
1355
|
+
MIT
|