# Sandcastle

> A TypeScript library for orchestrating AI coding agents in isolated sandboxes using Docker, Podman, or Vercel, with configurable branch strategies and a simple sandcastle.run() API.

Sandcastle is a TypeScript library that lets you orchestrate AI coding agents inside isolated sandboxes with a single `sandcastle.run()` call. It handles sandboxing via configurable providers (Docker, Podman, Vercel, or custom), manages git branch strategies so agent commits land cleanly, and merges changes back automatically. It is provider-agnostic and designed for parallelizing multiple AFK agents, building review pipelines, or composing custom agent workflows.

- **`sandcastle.run()`** — *invoke an agent with a single call; Sandcastle handles sandbox lifecycle, branching, and commit merging automatically.*
- **Multiple sandbox providers** — *built-in support for Docker, Podman, and Vercel Firecracker microVMs; create custom bind-mount or isolated providers using `createBindMountSandboxProvider` / `createIsolatedSandboxProvider`.*
- **Branch strategies** — *choose `head` (direct write), `merge-to-head` (temp branch merged back), or `branch` (explicit named branch) to control where agent commits land.*
- **`createSandbox()`** — *reuse a single sandbox across multiple agent runs to avoid repeated container startup costs and keep all runs on the same branch.*
- **`createWorktree()`** — *manage git worktrees as independent first-class objects, enabling interactive-then-AFK agent handoffs on the same branch.*
- **Prompt system** — *use inline strings or prompt files with `{{KEY}}` placeholder substitution, dynamic shell expression expansion (`` !`command` ``), and built-in `{{SOURCE_BRANCH}}` / `{{TARGET_BRANCH}}` variables.*
- **Lifecycle hooks** — *run setup commands on the host or inside the sandbox at `onWorktreeReady` and `onSandboxReady` hook points, with per-hook timeout control.*
- **Session capture & resume** — *automatically captures Claude Code session JSONL to the host for `claude --resume` support; pass `resumeSession` to continue a prior conversation in a new sandbox.*
- **CLI tooling** — *`sandcastle init` scaffolds the `.sandcastle/` config directory and Docker image; `sandcastle docker build-image` / `sandcastle podman build-image` rebuild images after Dockerfile changes.*
- **Workflow templates** — *choose from `blank`, `simple-loop`, `sequential-reviewer`, `parallel-planner`, or `parallel-planner-with-review` during `sandcastle init` to scaffold ready-to-use orchestration scripts.*
- **Iteration control** — *configure `maxIterations`, `completionSignal`, and `idleTimeoutSeconds` to control how long agents run and when they stop.*
- **AbortSignal support** — *pass a `signal` to `run()` to cancel in-flight agent subprocesses and lifecycle hooks cleanly.*

## Features
- Provider-agnostic sandbox orchestration (Docker, Podman, Vercel, custom)
- Single sandcastle.run() API for invoking AI coding agents
- Configurable branch strategies: head, merge-to-head, branch
- createSandbox() for reusable multi-run sandboxes
- createWorktree() for independent git worktree lifecycle management
- Prompt files with {{KEY}} substitution and !`command` shell expansion
- Built-in {{SOURCE_BRANCH}} and {{TARGET_BRANCH}} prompt variables
- Lifecycle hooks (host and sandbox) at onWorktreeReady and onSandboxReady
- Session capture and resume for Claude Code agents
- CLI: sandcastle init, docker build-image, podman build-image
- Workflow templates: blank, simple-loop, sequential-reviewer, parallel-planner, parallel-planner-with-review
- maxIterations, completionSignal, and idleTimeoutSeconds controls
- AbortSignal support for cancellation
- Custom sandbox provider API (bind-mount and isolated)
- Parallel agent execution support
- Per-hook timeout configuration
- Environment variable injection via agent and sandbox provider env options
- Automatic worktree cleanup with await using / Symbol.asyncDispose

## Integrations
Docker, Podman, Vercel (@vercel/sandbox), Claude Code (Anthropic), GitHub CLI (gh), Git, Node.js, OpenAI Codex, OpenCode

## Platforms
CLI, API, DEVELOPER_SDK

## Pricing
Open Source

## Version
v0.5.6

## Links
- Website: https://github.com/mattpocock/sandcastle
- Documentation: https://github.com/mattpocock/sandcastle#readme
- Repository: https://github.com/mattpocock/sandcastle
- EveryDev.ai: https://www.everydev.ai/tools/sandcastle
