Per-worktree environments that fork and reset.
spwn gives every coding agent its own running copy of your stack: services, databases, files, ports and URL. The world is defined in one committed spwn.toml, forked in seconds mid-task, and reset when the agent breaks it. The file is shaped like Docker Compose, so there is nothing new to learn.
Install go install github.com/spwn-gg/envs/cmd/spwn@latest
$ spwn status abcd orders running api :41000 http://api.abcd.localhost:8642 postgres :41001 redis :41002 $ spwn fork --name try-migration forked k7mq from abcd worktree ../orders-k7mq spwn/k7mq pgdata copy-on-write pause 140 ms api :41010 http://api.k7mq.localhost:8642 postgres :41011 redis :41012 $ cd ../orders-k7mq && go run ./cmd/migrate up 0042 drop column orders.legacy_ref $ spwn reset reset k7mq to seeded@3f9a1c
Already on Compose? Keep your file.
include reads your Compose file exactly as docker compose does and never edits it. Every env gets its own project name, the published ports move into the env's port block, and the named volumes fork. spwn.toml only adds what Compose can't say: where a fresh database comes from and how it forks.
services:
db:
image: postgres:17
ports: ["5432:5432"]
environment:
POSTGRES_PASSWORD: dev
volumes:
- pgdata:/var/lib/postgresql/data
redis:
image: redis:7
volumes:
pgdata:
version = 1
include = ["compose.yml"] # db and redis live here
[volumes.pgdata] # the Compose volume, forkable
source = "snapshot:seeded" # new envs clone the seed
[snapshots.seeded]
volumes = ["pgdata"]
steps = ["npm run migrate", "npm run seed"]
inputs = ["db/**"]
Per env, db listens on a port from the env's block instead of 5432, pgdata is a copy-on-write clone of the golden snapshot, and http://db.<slug>.localhost points at this env alone. Services you write in TOML use the same names Compose does: services, image, ports, volumes, environment, healthcheck, depends_on, profiles. The Compose-shaped file is here today; the Compose driver that reads included files is on the P0 roadmap.
Worktrees isolate code. spwn isolates everything else.
Five agents on five branches still share one machine's ports, one local database and one Docker daemon. They collide, and each agent blames its own code. spwn gives each worktree a port block, its own services, its own data and a browser URL, and lets any of them be forked as one unit while it runs.
One file, four verbs
The committed spwn.toml is the contract. The CLI that your host calls, the MCP server your agent calls, and the hooks you install all read the same file and behave the same way.
-
Describe the world once
Declare each service the way you would in Compose: a host process or a container image, with its ports, the volumes it owns, its health check and the commands to run inside it. Host hooks install dependencies and run migrations.
spwn checkreports mistakes with file and line. Orincludeyour Compose file as it is, or letspwn initdraft the TOML from it (both on the P0 roadmap). -
spwn upin every worktreeYour host calls it when a worktree is created. The first run seeds a golden snapshot keyed by the hash of your migrations and seeds. Every later env, on any branch, clones that snapshot instead of seeding again.
upis idempotent and resumes after a crash. -
spwn forkmid-taskEvery stateful service, the working tree, and the ports and URL bound to them, copied as one operation while the parent keeps running. On ZFS or btrfs the fork is an atomic snapshot. On APFS the services pause together for the length of the clone, and the fork reports how long that took.
-
spwn resetwhen it breaks,spwn downwhen it endsReset returns the env's volumes to the snapshot they started from and leaves everything else running. Down stops the services, frees the port block and removes the volumes. After a week of heavy agent use,
spwn doctorfinds nothing to clean up.
A typical service repo, in one file
A Go API and worker with Postgres and Redis. New envs fork a seeded Postgres, Redis starts cold, and the agent gets a context file and fork tools.
Ports
Every env gets one contiguous block of host ports. Named ports get a fixed offset in it and are exported as environment variables, so nothing listens on 3000 or 5432 by accident. HTTP ports also get a browser URL under *.localhost, with no DNS or hosts-file changes.
Services
The driver is inferred from the keys: run is a host process, image a container, and a service from an included Compose file is a Compose service. depends_on sets the start order: a migration that depends on Postgres makes Postgres start first and holds the API until the migration is done. Health checks are HTTP, TCP, command or log.
Volumes and snapshots
A named volume is a directory one service mounts, as in Compose. Its [volumes.*] entry says where a fresh env's data comes from and how a fork copies it. A golden snapshot is built once in a throwaway env and rebuilt only when its inputs change.
Agent
At ready, spwn writes .spwn/AGENT.md with the env's URLs, ports and database URL, and the rule for when to fork or reset. Most agents won't call a tool they don't know about, but they do read a file that tells them where the API is.
version = 1
name = "orders"
[[hooks.create]]
run = "go mod download"
inputs = ["go.sum"] # skipped while go.sum is unchanged
[[hooks.provision]]
run = "go run ./cmd/migrate up"
depends_on = ["postgres"] # postgres first; api waits for this
inputs = ["db/migrations/**"]
[services.postgres]
image = "postgres:17"
ports = ["pg:5432/tcp"]
environment = { POSTGRES_HOST_AUTH_METHOD = "trust" }
volumes = ["pgdata:/var/lib/postgresql/data"]
healthcheck = { cmd = "pg_isready -p {{ ports.pg }}" }
[services.api]
run = "go run ./cmd/api"
ports = [{ name = "api", env = "PORT" }]
healthcheck = { http = "/healthz" }
[volumes.pgdata]
source = "snapshot:seeded" # new envs fork the golden snapshot
url = "postgres://postgres@127.0.0.1:{{ ports.pg }}/orders"
[snapshots.seeded]
volumes = ["pgdata"]
steps = ["go run ./cmd/seed"]
inputs = ["db/migrations/**", "db/seeds/**"]
[env_file]
environment = { DATABASE_URL = "{{ volumes.pgdata.url }}" }
[agent]
include_in = ["CLAUDE.md"]
notes = "make test-int reads .spwn/env"
[integrations]
claude_code = true
Heavy stack? Keep every worktree, run one at a time.
Two worktrees cost two stacks of memory. A search cluster, a few databases and a queue don't fit on a laptop five times over. The env lock lets a repo cap how many of its envs run at once, while every worktree keeps its env: data, ports, worktree and URL stay; only the services stop.
- feature-authrunning, holds the lockapi :41000, postgres :41001, search :41003
- fix-billingparkedservices stopped; volumes, ports and URL kept
- refactor-queuewaiting for the slotresumes exactly where it left off
Agents take turns on their own
With the cap on, AGENT.md opens with the rule: call env_lock before running tests or hitting the URLs, and env_unlock when the task is done. The slot passes to whoever is waiting, and a parked env comes back as it was left.
Nothing gets stuck
A holder that goes quiet for lock_idle (30 minutes by default) loses the slot and is parked. Forks made by the holder share its slot. Claude Code sessions start with spwn up --no-wait, so a new session never blocks on another one's env, and spwn unlock runs when a session ends.
# spwn.toml
[limits]
active = 1 # one env of this repo at a time
lock_idle = "30m" # a silent holder loses the slot
# what the agent does, over MCP
> env_lock
{ "state": "waiting", "holder": "feature-auth",
"position": 1 }
… the holder calls env_unlock …
{ "state": "active",
"ports": { "api": 41010, "postgres": 41011 },
"urls": { "api": "http://api.k7mq.localhost:8642" } }
> env_exec { "service": "api", "cmd": "make test-int" }
> env_unlock
{ "state": "parked", "next": "refactor-queue" }
# the same from a shell
$ spwn lock status
$ spwn lock # waits, then resumes the env
$ spwn unlock # parks it if someone waits
Plugs into what you already run
spwn never launches or supervises an agent. Whatever creates the worktree calls spwn up; whatever removes it calls spwn down. Existing Compose files and scripts are read as they are, never edited.
Claude Code
spwn install adds the worktree hooks to the project's settings and registers the MCP server in .mcp.json. New worktrees come up with their own env.
Herdr and Cursor
An env per pane with its health in the sidebar, or setup and teardown commands in Cursor's worktree config. Declared once in [integrations] so every teammate gets the same wiring.
Docker Compose
Compose files are loaded exactly as docker compose loads them. spwn writes an override that remaps ports into the env's block and swaps volumes for forked directories.
Any filesystem you have
Atomic snapshots on ZFS and btrfs. Per-file clones on APFS and XFS with a short pause. Plain copy as a fallback, with a warning. One static binary for macOS and Linux.
What spwn is not
- Not an agent runner. It never starts Claude, Codex or anything else. Session management, terminal panes and the agent UI belong to your host.
- Not a VM or Docker-in-Docker. Agents run on the host, services run as processes or containers, and spwn adds no per-env process beyond the services you declared.
- Not a cloud sandbox. Everything runs on 127.0.0.1 on your machine, inside a Coder workspace or a devcontainer, or on an air-gapped host. spwn makes no network call you didn't configure.
- Not a secrets leak. App runtime secrets are held in memory and injected only where you opt in. The MCP server reports secret names and whether they resolved, never values.