Concepts
Sandbox providers
On this page
The sandbox option on run(), interactive(), and createSandbox() accepts any sandbox provider, including noSandbox(): opt in to running the agent directly on the host when isolation is undesired. Built-in providers:
| Provider | Import path | Type | Accepted by |
|---|---|---|---|
| Bubblewrap | @ai-hero/sandcastle/sandboxes/bwrap | Native (bind-mount) | run(), createSandbox() |
| Seatbelt | @ai-hero/sandcastle/sandboxes/seatbelt | Native (bind-mount) | run(), createSandbox() |
| Docker | @ai-hero/sandcastle/sandboxes/docker | Bind-mount | run(), createSandbox(), interactive() |
| Podman | @ai-hero/sandcastle/sandboxes/podman | Bind-mount | run(), createSandbox(), interactive() |
| E2B | @ai-hero/sandcastle/sandboxes/e2b | Isolated | run(), createSandbox() |
| Modal | @ai-hero/sandcastle/sandboxes/modal | Isolated | run(), createSandbox() |
| Sprites | @ai-hero/sandcastle/sandboxes/sprites | Isolated | run(), createSandbox() |
| Vercel | @ai-hero/sandcastle/sandboxes/vercel | Isolated | run(), createSandbox(), interactive() |
| No-sandbox | @ai-hero/sandcastle/sandboxes/no-sandbox | None | run(), createSandbox(), interactive() |
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.
import { run, interactive, claudeCode } from "@ai-hero/sandcastle";import { bwrap } from "@ai-hero/sandcastle/sandboxes/bwrap";import { seatbelt } from "@ai-hero/sandcastle/sandboxes/seatbelt";import { docker } from "@ai-hero/sandcastle/sandboxes/docker";import { noSandbox } from "@ai-hero/sandcastle/sandboxes/no-sandbox";// Pick the native sandbox for this OS, so one main.ts works on macOS and Linux:const sandbox = process.platform === "darwin" ? seatbelt() : bwrap();// Native, Docker, Podman, and Vercel providers are interchangeable in run() and createSandbox():await run({agent: claudeCode("claude-opus-4-8"),sandbox,prompt: "...",});// No-sandbox runs the agent directly on the host, accepted by run(),// createSandbox(), and interactive(). Skips isolation entirely:await interactive({agent: claudeCode("claude-opus-4-8"),sandbox: noSandbox(),prompt: "...", // optional: omit to launch the TUI with no initial promptcwd: "/path/to/other-repo", // optional: defaults to process.cwd()});
You can also create your own provider using createBindMountSandboxProvider or createIsolatedSandboxProvider.
Native sandboxes: bwrap() and seatbelt()
A native sandbox contains the agent with your OS's own process sandbox instead of a container: Bubblewrap on Linux and WSL2, Seatbelt on macOS. There's no image to build and no daemon to run. Each exec is a fresh sandboxed process that runs your host's own tools, read-only.
- Files. The agent can write only the repo (or its worktree), the git dirs Sandcastle mounts, a private temp dir and a private home. Your home directory, SSH keys, agent logins and other repos are hidden. The repo appears at its real host path, as it does under
noSandbox(). - Toolchain. Every directory on your host
PATHis readable, plus the install root of each executable symlinked from one, so aclaudein~/.local/binor an nvm-installedcodexjust works. The install root is the executable's package when it's installed under anode_modulesdir, and otherwise only the executable's own directory, so annpm linked checkout isn't exposed. APATHentry equal to your home directory or an ancestor of it, and an install root holding a.git, are never mounted. Add other paths withmounts. - Logins. Each sandbox gets a fresh private home, removed on close, so the agent can't use the logins stored in your home directory:
~/.claude,~/.codex,~/.config/gh.seatbelt()doesn't promise to keep the agent away from your macOS Keychain, so don't rely on it for that. Put the agent's login in.sandcastle/.env(CLAUDE_CODE_OAUTH_TOKEN,ANTHROPIC_API_KEYand so on), and aGH_TOKENthere forgh. Your git identity is still copied in, so commits carry your name. - Env. The agent sees only the env vars Sandcastle passes in, plus
PATH,HOMEandTMPDIR. - Processes. The agent can't signal your processes, and every process it started is killed when the sandbox closes. Under
seatbelt(), a process that leaves its process group (by callingsetsidorsetpgid) is the exception: see Containment. - Network. Open, including your host's localhost.
- No root.
execand hooks withsudo: truereject, pointing todocker(). There's no image to install system packages into, so install them on your host. - No
interactive().interactive()with a native provider throwsNativeInteractiveNotSupportedError. Userun(), or passdocker()ornoSandbox()tointeractive().
Both take the same small set of options:
bwrap({env: { MY_VAR: "value" }, // merged at launch, like docker()'s envmounts: [// Each path appears at its own host path inside the sandbox, so// sandboxPath must equal hostPath. readonly defaults to false.{ hostPath: "/opt/my-tool-cache", sandboxPath: "/opt/my-tool-cache" },{hostPath: "/home/me/shared-config",sandboxPath: "/home/me/shared-config",readonly: true,},],maxOutputTailChars: 65_536, // same as docker()'s});
Constructing a native provider checks, once per process, that your machine can run it. If it can't, it throws NativeSandboxUnavailableError, with a fixCommand when there's one to run. It throws before any worktree exists, and it never falls back to running the agent unsandboxed.
import { NativeSandboxUnavailableError } from "@ai-hero/sandcastle";try {bwrap();} catch (error) {if (error instanceof NativeSandboxUnavailableError) {console.error(error.message); // why, in plain wordsconsole.error(error.fixCommand); // e.g. "sudo apt install bubblewrap", or undefined}}
bwrap() on Linux. It needs bwrap on PATH; the error gives your distro's install command if it's missing. Ubuntu 24.04 and later block the unprivileged user namespaces Bubblewrap needs with AppArmor. The error says so, and its fixCommand gives bwrap an AppArmor profile that allows them:
printf 'abi <abi/4.0>,\ninclude <tunables/global>\nprofile bwrap %s flags=(unconfined) {\n userns,\n}\n' "$(command -v bwrap)" \| sudo tee /etc/apparmor.d/bwrap >/dev/null && sudo apparmor_parser -r /etc/apparmor.d/bwrap
Or, more bluntly, allow user namespaces for every program: sudo sysctl -w kernel.apparmor_restrict_unprivileged_userns=0.
bwrap() on Windows. Run Sandcastle inside WSL2 and use bwrap(), with your repo on the Linux filesystem (ext4, for example ~/code/my-repo), not under /mnt/c. Inside the sandbox, /mnt, /run/WSL and WSL_INTEROP are hidden, so the agent can't run a Windows .exe.
seatbelt() on macOS. It needs macOS 14 or later and runs the system /usr/bin/sandbox-exec, so there's nothing to install. macOS refuses to nest Seatbelt sandboxes, so seatbelt() can't start inside another one; the error says so.
Agents' own sandboxes. The native sandbox is the containment layer, and Sandcastle already runs agents with their permission checks skipped, so turn off the agent's own sandboxing. An agent can't apply its own Seatbelt sandbox inside seatbelt(), because macOS refuses to nest them. Inside bwrap(), whether an agent's own Bubblewrap sandbox can start depends on your kernel.
E2B: e2b()
e2b() runs the agent in an E2B sandbox, a Firecracker microVM. Install the SDK (npm install e2b; it needs Node 22.19 or later) and set E2B_API_KEY in your shell, or pass the apiKey option. Don't put the key in .sandcastle/.env: the keys declared there are forwarded into the sandbox.
E2B's default template has no agent CLI. Its prebuilt claude and codex templates ship one:
import { run, claudeCode } from "@ai-hero/sandcastle";import { e2b } from "@ai-hero/sandcastle/sandboxes/e2b";await run({agent: claudeCode("claude-opus-4-8"),sandbox: e2b({ template: "claude" }),prompt: "...",});
e2b() accepts the SDK's Sandbox.create() options (template, timeoutMs, apiKey, metadata, network, ...) except envs and lifecycle, which the provider sets. The repo lands in $HOME/workspace. Each sandbox is killed on close(), or when timeoutMs (default 30 minutes) runs out, so the timeout must cover setup, the agent run and syncing the commits back. E2B's free Hobby plan caps a sandbox at 1 hour. The handle's sandboxId is the E2B sandbox id. interactive() isn't supported yet.
Fly.io Sprites: sprites()
sprites() runs each sandbox in a new Fly.io Sprite, a Linux microVM. It's an isolated provider: Sandcastle copies the repo in at /home/sprite/workspace and brings the agent's commits back out. Install the SDK alongside Sandcastle:
npm install @fly/sprites
- Auth. Put a Sprites token in
SPRITES_TOKEN(in.sandcastle/.envor your shell).SPRITE_TOKEN, the name the Sprites CLI uses, works too. Thetokenoption wins over both. - Node 24.
@fly/spritesasks for Node 24 or later on the host. - Image. Sprites have one base image, with
git, Node and theclaudeCLI already installed. Install any other agent's CLI in anonSandboxReadyhook. - Teardown. A Sprite keeps running until it's deleted.
close()deletes it, and so does a failed setup. If the process is interrupted (SIGINT/SIGTERM) or exits without closing the sandbox, Sandcastle deletes the Sprite from a background process. sandboxIdis the Sprite's name, which the Sprites CLI and API take.sudo: trueuses the Sprite's own passwordlesssudo.- No
interactive(). Userun()orcreateSandbox().
import { run, claudeCode } from "@ai-hero/sandcastle";import { sprites } from "@ai-hero/sandcastle/sandboxes/sprites";await run({agent: claudeCode("claude-opus-4-8"),sandbox: sprites({env: { MY_VAR: "value" }, // merged at launch, like docker()'s env}),prompt: "...",});
Modal: modal()
modal() runs the agent in a Modal sandbox. Install the SDK, an optional peer dependency, and give it a Modal token: MODAL_TOKEN_ID and MODAL_TOKEN_SECRET (from modal token new or the dashboard), or the tokenId and tokenSecret options, which win. With neither, the SDK reads ~/.modal.toml.
npm install modal
import { run, claudeCode } from "@ai-hero/sandcastle";import { modal } from "@ai-hero/sandcastle/sandboxes/modal";await run({agent: claudeCode("claude-opus-4-8"),sandbox: modal(),prompt: "...",});
- Image. Modal has no default image, so
modal()builds one:node:24-bookworm(Node LTS,git,tar) with Claude Code installed. Modal builds it once and caches it. Passimageto use your own: a registry tag such as"node:24-bookworm", or a ModalImage. It needsshandgit; install other agents' CLIs in it, or in anonSandboxReadyhook. - Root. Modal runs every command as root, whatever the image's
USER, sosudo: truechanges nothing.modal()setsIS_SANDBOX=1, which Claude Code needs before it skips its permission checks as root. - Lifetime. A sandbox lives for at most
timeoutMs, an hour by default (Modal's own default is 5 minutes, and its limit is 24 hours). Modal can't extend it after create, so set it above your longest run.close()terminates the sandbox. - Other options. Sandboxes go in the Modal App named by
appName(default"sandcastle"), created if missing.createpasses othersandboxes.create()options through, such ascpu,memoryMiBandregions. sandboxIdis the Modal sandbox id (sb-…). Reattach withmodal.sandboxes.fromId(<sandboxId>)from themodalSDK, thenterminate(). Some accounts' sandboxes don't appear insandboxes.list(), so keep the id.- Host. The
modalSDK needs Node 22 or later.
Containment
What a sandbox protects you from depends on the provider. Native sandboxes are built for accidents and prompt-injection attacks: an agent that runs the wrong command, or follows instructions planted in an issue or a web page, can't read your credentials, write outside the repo, or touch your other processes.
They don't protect against:
- Kernel escapes. A native sandbox is a shared-kernel process sandbox. On Linux,
bwrap()is about as strong as a runc container (Docker on Linux). On macOS,seatbelt()is weaker than Docker Desktop, which runs containers in a VM. - Services on your host's localhost. The network is open, and the sandbox can reach anything listening on
127.0.0.1, such as a dev server or a local database. - Process listing on macOS. Seatbelt has no PID namespace. The agent can't signal your processes, and
ps,pgrepand the process-list sysctl are blocked, but your processes aren't hidden the way they are inbwrap()'s own PID namespace. Paths outside the sandbox are visible as metadata: the agent can check that a path exists and read its size and timestamps, but not its contents. - Detached processes on macOS.
seatbelt()kills eachexec's process group. A process that leaves that group, by callingsetsidorsetpgid, survivesclose()and your Node process being killed. It is still under the Seatbelt profile, so it can still write the worktree and reach the network, until it exits or you kill it.
For an image-based sandbox, or a VM boundary on macOS and Windows, use docker() or podman(). For stronger isolation than a shared kernel, use a remote provider such as vercel(), which runs the agent in its own Firecracker microVM.
Build-image and remove-image
An image-based provider (docker(), podman()) runs the agent in an image built from the config directory, the .sandcastle/ folder that sandcastle init creates in your repo. After you change the Dockerfile or Containerfile, rebuild the image with build-image, the CLI command that builds it, and when you no longer need it, delete it with remove-image: see sandcastle docker build-image and the commands after it. Native providers have no image.
Custom sandbox providers
Sandcastle ships with built-in providers (see the table above), but you can create your own. There are two kinds:
- 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.
- Isolated — the sandbox has its own filesystem (e.g. a cloud VM). The provider handles syncing code in and out via
copyInandcopyFileOut. Use this when the sandbox cannot access the host filesystem.
The sandbox handle contract
Both provider types return a sandbox handle from their create() function. The handle exposes:
| Method | Required | Description |
|---|---|---|
exec | Both | Run a command, optionally streaming stdout line-by-line via options.onLine |
close | Both | Tear down the sandbox |
copyFileIn | Bind-mount | Copy a single file from the host into the sandbox |
copyFileOut | Both | Copy a single file from the sandbox to the host |
copyIn | Isolated | Copy a file or directory from the host into the sandbox |
worktreePath | Both | Absolute path to the repo directory inside the sandbox |
sandboxHomedir | Bind-mount, optional | The agent's home as commands inside the sandbox see it. Defaults to /home/agent |
sandboxTmpdir | Bind-mount, optional | A writable temp dir as commands inside the sandbox see it. Defaults to /tmp |
sandboxId | Both, optional | An id outside tools can address the sandbox by, such as a container name or a vendor sandbox id. Surfaced as Sandbox.sandboxId |
ExecResult
Every exec call returns an ExecResult:
interface ExecResult {readonly stdout: string;readonly stderr: string;readonly exitCode: number;}
The Docker and Podman providers return exitCode: 1 when their command process
is terminated by a signal. Numeric exit codes are preserved, including 0 for
success. Custom providers should also report signal termination as a non-zero
exit code.
Bind-mount provider example
A minimal bind-mount provider that shells out to local processes (no container):
import {createBindMountSandboxProvider,type BindMountCreateOptions,type BindMountSandboxHandle,type ExecResult,} from "@ai-hero/sandcastle";import { execFile, spawn } from "node:child_process";import { copyFile as fsCopyFile, mkdir as fsMkdir } from "node:fs/promises";import { dirname } from "node:path";import { createInterface } from "node:readline";const localProcess = () =>createBindMountSandboxProvider({name: "local-process",create: async (options: BindMountCreateOptions,): Promise<BindMountSandboxHandle> => {const worktreePath = options.worktreePath;return {worktreePath,exec: (command: string,opts?: { onLine?: (line: string) => void; cwd?: string },): Promise<ExecResult> => {if (opts?.onLine) {const onLine = opts.onLine;return new Promise((resolve, reject) => {const proc = spawn("sh", ["-c", command], {cwd: opts?.cwd ?? worktreePath,stdio: ["ignore", "pipe", "pipe"],});const stdoutChunks: string[] = [];const stderrChunks: string[] = [];const rl = createInterface({ input: proc.stdout! });rl.on("line", (line) => {stdoutChunks.push(line);onLine(line); // forward each line to Sandcastle});proc.stderr!.on("data", (chunk: Buffer) => {stderrChunks.push(chunk.toString());});proc.on("error", (err) => reject(err));proc.on("close", (code) => {resolve({stdout: stdoutChunks.join("\n"),stderr: stderrChunks.join(""),exitCode: code ?? 1,});});});}return new Promise((resolve, reject) => {execFile("sh",["-c", command],{ cwd: opts?.cwd ?? worktreePath, maxBuffer: 10 * 1024 * 1024 },(error, stdout, stderr) => {if (error && error.code === undefined) {reject(new Error(`exec failed: ${error.message}`));} else {resolve({stdout: stdout.toString(),stderr: stderr.toString(),exitCode: error? typeof error.code === "number"? error.code: 1: 0,});}},);});},copyFileIn: async (hostPath: string, sandboxPath: string) => {await fsMkdir(dirname(sandboxPath), { recursive: true });await fsCopyFile(hostPath, sandboxPath);},copyFileOut: async (sandboxPath: string, hostPath: string) => {await fsMkdir(dirname(hostPath), { recursive: true });await fsCopyFile(sandboxPath, hostPath);},close: async () => {// nothing to tear down for a local process},};},});
Isolated provider example
A minimal isolated provider using a temp directory:
import {createIsolatedSandboxProvider,type IsolatedSandboxHandle,type ExecResult,} from "@ai-hero/sandcastle";import { execFile, spawn } from "node:child_process";import { copyFile, mkdir, mkdtemp, rm } from "node:fs/promises";import { tmpdir } from "node:os";import { dirname, join } from "node:path";import { createInterface } from "node:readline";const tempDir = () =>createIsolatedSandboxProvider({name: "temp-dir",create: async (): Promise<IsolatedSandboxHandle> => {const root = await mkdtemp(join(tmpdir(), "sandbox-"));const worktreePath = join(root, "workspace");await mkdir(worktreePath, { recursive: true });return {worktreePath,exec: (command: string,opts?: { onLine?: (line: string) => void; cwd?: string },): Promise<ExecResult> => {if (opts?.onLine) {const onLine = opts.onLine;return new Promise((resolve, reject) => {const proc = spawn("sh", ["-c", command], {cwd: opts?.cwd ?? worktreePath,stdio: ["ignore", "pipe", "pipe"],});const stdoutChunks: string[] = [];const stderrChunks: string[] = [];const rl = createInterface({ input: proc.stdout! });rl.on("line", (line) => {stdoutChunks.push(line);onLine(line);});proc.stderr!.on("data", (chunk: Buffer) => {stderrChunks.push(chunk.toString());});proc.on("error", (err) => reject(err));proc.on("close", (code) => {resolve({stdout: stdoutChunks.join("\n"),stderr: stderrChunks.join(""),exitCode: code ?? 1,});});});}return new Promise((resolve, reject) => {execFile("sh",["-c", command],{ cwd: opts?.cwd ?? worktreePath, maxBuffer: 10 * 1024 * 1024 },(error, stdout, stderr) => {if (error && error.code === undefined) {reject(new Error(`exec failed: ${error.message}`));} else {resolve({stdout: stdout.toString(),stderr: stderr.toString(),exitCode: error? typeof error.code === "number"? error.code: 1: 0,});}},);});},copyIn: async (hostPath: string, sandboxPath: string) => {const info = await stat(hostPath);if (info.isDirectory()) {await cp(hostPath, sandboxPath, { recursive: true });} else {await mkdir(dirname(sandboxPath), { recursive: true });await copyFile(hostPath, sandboxPath);}},copyFileOut: async (sandboxPath: string, hostPath: string) => {await mkdir(dirname(hostPath), { recursive: true });await copyFile(sandboxPath, hostPath);},close: async () => {await rm(root, { recursive: true, force: true });},};},});
Passing to run()
Pass your custom provider via the sandbox option — it works the same as the built-in docker() provider:
import { run, claudeCode } from "@ai-hero/sandcastle";const result = await run({agent: claudeCode("claude-opus-4-8"),sandbox: localProcess(), // your custom providerprompt: "Fix issue #42 in this repo.",});
Reference implementations
For real-world examples, see:
src/sandboxes/docker.ts— bind-mount provider using Docker containers (with SELinux label support)src/sandboxes/e2b.ts— isolated provider using E2B sandboxes viae2bsrc/sandboxes/vercel.ts— isolated provider using Vercel Firecracker microVMs via@vercel/sandboxsrc/sandboxes/modal.ts— isolated provider using Modal sandboxes viamodalsrc/sandboxes/podman.ts— bind-mount provider using Podman containers (with SELinux label support)src/sandboxes/test-isolated.ts— isolated provider using temp directories (used in tests)
Related terms
- Core concepts: sandbox, host and agent
- Branch strategies: how a worktree is mounted into a bind-mount sandbox, or synced with an isolated one
- Hooks: commands that run inside the sandbox as it starts, and
sudo: trueon each provider - Init: choosing a provider during init, and the custom Dockerfile
- CLI commands: build-image and remove-image
createSandbox(): one sandbox for several runs