
Killed as Orphans
The first sandboxes soulvm created lived for about a minute. I had E2B’s open-source Firecracker orchestrator running on one machine, E2B’s API still up beside it, and my new control plane talking to the orchestrator directly. The API found sandboxes on the node that it hadn’t created, decided they were orphans and killed them. It was doing its job. A control plane that’s the source of truth removes whatever it didn’t create, so two of them can’t share a machine. One had to go, and I removed E2B’s.
In Where AI Agents Live I mapped the choices behind agent sandboxes, with notes from soulvm, the platform I built on that orchestrator so my agents’ sandboxes and apps have somewhere to run. I built it mainly to learn how sandboxes work from the inside, and partly because E2B’s full stack, made to run a cloud service for many teams with Postgres, Redis and ClickHouse beside its API, was far more than one machine at home needed. So I kept the one piece that runs the VMs and wrote the rest myself: the control plane, which is what this post is about. soulvm has since grown into a platform that friends and family use too, which made getting the bookkeeping right matter more.
Starting, pausing and snapshotting VMs worked from the first day, because the orchestrator had already solved that. What broke was everything the API had been doing without anyone asking. I found most of it by its absence: a sandbox still running past its deadline, 209 build folders on a disk where soulvm knew about 24, and a cache serving images whose files had been deleted. Snapshots bring a machine’s state back fast. They don’t tell you who owns it, or when it’s safe to throw away. That’s the control plane, and I couldn’t find a first-hand account of writing one for E2B’s orchestrator alone, so this is mine.
What the Orchestrator Leaves to You
E2B’s runtime is a whole platform: an API, the orchestrator, envd (the agent inside each VM), a client proxy, a template builder, Postgres, Redis and ClickHouse. Its design keeps the control plane and the data plane apart. The API decides where a sandbox runs and keeps the records. The orchestrator on each machine decides how it runs, and sandbox traffic never passes through the API. A sandbox is a resumed snapshot. A template is a Firecracker VM that was booted once and saved (memory, disk and CPU state), and creating a sandbox resumes it, with memory pages loaded lazily and the disk served as a copy-on-write overlay.
On one machine the “where” question disappears, so I kept the orchestrator and envd, unmodified, and replaced everything else with one Go binary that keeps its state in SQLite. Without a REDIS_URL the orchestrator logs “redis disabled” and skips what Redis was for: routing records, sandbox events and upload signals between nodes. All three serve E2B’s API or other nodes, and I had neither. What the orchestrator does and doesn’t do then sets the control plane’s job:
| The orchestrator does | It leaves to the caller |
|---|---|
| creates, pauses and resumes VMs | every value in a sandbox’s configuration |
stores a sandbox’s end_time | acting on it, and any maximum run length |
| reports what’s running now | VMs nobody asked for |
| keeps a build’s log for 10 minutes | build history after that, or after a restart |
| writes snapshot and build files to disk | deciding which of them are still needed |
Pause and resume are a build ID you choose. Pause snapshots memory and disk under a build ID the caller picks, then stops the VM. Resume is an ordinary Create from that snapshot, under the same sandbox ID:
// simplified from soulvm: pause, then resume, over the orchestrator's gRPC
snap := uuid.NewString() // the caller names the snapshot
_, err := orch.Pause(ctx, &pb.SandboxPauseRequest{
SandboxId: sbx.ID, TemplateId: sbx.TemplateID, BuildId: snap,
})
// resume: a Create from that snapshot, same sandbox ID
_, err = orch.Create(ctx, &pb.SandboxCreateRequest{
Sandbox: &pb.SandboxConfig{
SandboxId: sbx.ID, TemplateId: sbx.TemplateID, BuildId: snap, Snapshot: true,
Vcpu: sbx.VCPU, RamMb: sbx.RAMMB, TeamId: teamID,
KernelVersion: k, FirecrackerVersion: fc, EnvdVersion: envd,
},
StartTime: timestamppb.Now(), EndTime: timestamppb.New(deadline),
})
On the server it runs on, a create takes about 120 ms and a resume about 120, end to end through soulvm’s API. Files and background processes survive a pause. Paused sandboxes also survive an orchestrator restart, since their snapshots are files on disk that a later Create reads. Running ones don’t. When the orchestrator is asked to stop, it waits for its sandboxes to exit, and a new orchestrator process can’t take over the VMs of an old one. So soulvm’s launcher, adapted from E2B’s own, kills every sandbox’s VM before it stops the orchestrator, and a restart ends every running sandbox.
The Control Plane on One Screen
Everything in the right-hand column became a loop. This is how soulvm’s control plane starts:
// simplified from soulvm's Service.Start
func (s *Service) Start(ctx context.Context) {
s.recoverInterrupted(ctx) // settle what a soulvm restart left mid-change
go s.loop(ctx, time.Second, s.reap) // deadlines: sleep or end
go s.loop(ctx, 5*time.Second, s.reconcile) // records against the node
go s.loop(ctx, 15*time.Second, s.sweepSnapshots) // snapshots nothing owns
go s.loop(ctx, 10*time.Minute, s.sweepLayers) // build layers nothing needs
go s.loop(ctx, time.Hour, s.prune) // history past its retention
go s.loop(ctx, jobCheckEvery, s.runDueJobs) // scheduled jobs, which wake sandboxes
for _, b := range buildsStillBuilding() {
go s.watchBuild(ctx, b.ID) // copy status and log while they exist
}
}
The real one also checks domains, disk use, notifications and app health. Each loop runs only on the leader, because it acts on shared state and must run once however many copies of soulvm there are. With one copy, it’s always the leader. Between them, the loops move every sandbox through its life:
Ownership
Four places each know something about a sandbox, and any two of them can disagree:
| Ledger | What it knows |
|---|---|
| soulvm’s SQLite | what soulvm asked for: sandboxes, snapshots, builds |
| The orchestrator’s memory | what’s running now, and recent build status |
| The storage directory | which snapshot and build files exist |
| envd, inside each VM | the processes running in the sandbox |
Something had to be the authority, and the first day had already shown there could only be one. The orchestrator’s memory couldn’t be it: it forgets a build after 10 minutes, a restart ends every running VM, and it never acts on a deadline. The storage directory couldn’t be it either, because a pause snapshot and a template build look identical on disk. That left soulvm’s own records. SQLite became the authority, and the other three became observations to check against it.
Write the record before the action. The orchestrator returns from Pause before the snapshot has reached storage. When a kill came shortly after a pause, soulvm told the orchestrator to delete files that didn’t exist yet. They landed a moment later with nothing pointing at them, and old test runs had left 35 of them, about 4.7 GB. Only soulvm’s records could tell those files from a template build, so soulvm now writes a row for every snapshot before it calls Pause, and the snapshot sweep deletes only what its records say it created, once the sandbox has ended and the files have stopped changing for 30 seconds.
Take Turns: One Lock per Sandbox
Every lifecycle change, whether start, pause, resume or end, takes the sandbox’s lock, so only one happens at a time. Twenty requests for the same paused app mustn’t start twenty resumes: the first resumes it, and the rest wait and go straight through. When a redeploy hands an app’s address to a new sandbox, the old one gets no new requests, and its handover waits up to 10 seconds for the ones still in flight before ending it.
Order matters inside a change too. When a job’s run ends, soulvm now updates the job before the run, because whatever waits on the run has to find the job already true. A test had been passing only by timing. And soulvm once counted its own restart of a crashed app as another crash, and gave up on a healthy app.
Recovery
Notice: Reconcile, Every Five Seconds
Reconcile compares SQLite with what the orchestrator says is running. Two things made it harder than a diff.
A dying service still answers. When the orchestrator was being stopped, its launcher killed the VMs first, and the old process kept answering for a moment with its old startup time and an empty list. Reconcile saw every sandbox vanish, and with no restart in sight it recorded the wrong reason for all of them. Now it waits for a second miss in a row, and by then a real restart shows up as a new startup time and gets the right reason.
It removes what it didn’t start. This is soulvm’s policy, not the orchestrator’s. On its own the orchestrator kills nothing: with E2B’s API gone, a sandbox created straight over gRPC kept running. A VM running on the node that soulvm didn’t start, or that its records say has ended, is killed once it’s a minute old. Younger ones may be a create still in flight. The same duty killed my sandboxes on the first day. soulvm’s records are the authority now, so soulvm does it too.
Copy What’s About to Vanish
The orchestrator keeps a build’s status and log in an in-memory cache whose entries expire after 10 minutes (buildInfoExpiration in its code), hands the log out 100 lines at a time, and forgets it all when it restarts. soulvm watches each build, copies the log into SQLite as it arrives, and records a build the orchestrator forgot as “lost”, with a reason. Sandbox history works the same way. The one function that saves a sandbox also records any change in its state, so no code path can change a state without leaving it in the history.
Recover from Yourself
The control plane can crash too, and a soulvm restart can land in the middle of a pause. So before any loop starts, soulvm settles every sandbox its records show mid-change. If the node still has it, it’s running. If it was pausing or resuming and a snapshot exists, it’s paused. Otherwise it failed, and its history says soulvm restarted while it was pausing, resuming or starting. Then soulvm picks up what it was watching. Builds still in progress get a watcher again, and running apps’ output is followed again, minus whatever they printed while soulvm was down.
Cleanup
Keep Time: the Reaper, Every Second
The orchestrator stores each sandbox’s end_time and never acts on it, as the first post showed with a sandbox still running 70 seconds past its deadline. It doesn’t enforce a maximum length either. E2B’s API had been doing both, in an evictor I’d removed along with it. On my own machine there’s no bill, but an agent’s app left running overnight holds its memory until someone notices.
soulvm’s reaper checks every second. At its deadline a sandbox sleeps, keeping what its kind needs. An app’s sandbox keeps nothing by default, since starting fresh from its build is as fast as resuming it, and other sandboxes keep what they were started with: their files, or their memory. A sleeping sandbox is deleted later, after 24 hours if it kept its memory or seven days if it kept its files, unless it serves an app or belongs to a chat.
More than one thing can need a sandbox awake, such as a visitor or a scheduled job running in it. If each set the deadline directly, one finishing would cut the others short. So each job run holds the deadline it needs, and when it’s released, the sandbox goes idle from the latest of what’s left.
209 Folders, 24 Known
The first layer sweep freed 15.6 GB, and the old server needed it. Its disk was nearly full, and 30 GB of it was template storage: 209 build folders, of which soulvm had records for 24. None of its events mentioned 179 of the rest, which pointed at something soulvm never started.
It was E2B’s template builder. It saves a cached layer for each build step under a build ID of its own, and deleting a template only removed the final build. Every test deploy left two or three layers of about 100 MB behind.
Deleting a Dependency Graph
Layers depend on each other, and E2B’s on-disk format says how. A finished build is a directory of diffs (memfile, rootfs.ext4, snapfile) plus a header for each, and every header starts the same way:
// the start of every snapshot header, little endian
type header struct {
Version, BlockSize, Size, Generation uint64
BuildId, BaseBuildId [16]byte
}
After that come block mappings, each naming the build that holds a range of blocks, so any build ID in a header is a parent. The metadata travels with the blocks, and that makes cleaning up a graph walk, not a list. The layer sweep is a mark and sweep:
// pseudocode: the layer sweep
if a build is in progress: stop
roots = builds soulvm has a row for
+ each template's current build
+ builds of live/paused sandboxes
+ whatever a URL serves
+ folders under an hour old
keep = roots + every build they map
blocks to, transitively
(read from the headers)
delete every build folder not in keep
warn if a kept build's parents are gone
The sweep skips a run while any build is in progress, and treats anything under an hour old as a root, so a fresh layer can’t be swept before a record points at it. The first run removed 121 folders, and every remaining template still started.
Two mistakes came first. The first sweep kept anything written in the last hour, but not the older layers those young layers were built on, so the young layers looked fine and couldn’t be used. In the version above, age only decides what counts as a root, never whether a root’s dependencies stay. The second was a test that passed for the wrong reason: the builder seemed to check its cached layers, but it only reads the one it looks up, so with a middle layer gone it reported the step as cached and failed at the end with an opaque internal error.
A correct sweep still isn’t a bound. Two rounds of rebuilding every base image plus three test runs in one afternoon filled the disk again, with 28.5 GB waiting out the hour. Builds should check free disk before they start, and soulvm doesn’t do that yet.
One Deleter
This one looked like no bug at all. An image saved from a sandbox after a suspend and resume still started fine a minute after its sandbox was deleted. Its header said otherwise. It mapped blocks to a folder that no longer existed. The orchestrator had served the start from its cache. After the next orchestrator restart, those images would have failed, with nothing to show what had broken them.
Three code paths deleted files without asking the sweep, the only code that knew which builds other builds depended on. Now dropping a snapshot or a template only marks it, and the sweeps are the only things that delete. It’s the same kind of failure Fly found in the old storage under its Sprites, which I described in the first post: separate records that drifted apart, with nothing to say which one was true.
Where One Machine Is Baked In
Moving SQLite to Postgres wouldn’t take soulvm past one machine. The database was the smallest part of what stood in the way:
| Assumption | Why it blocks more machines |
|---|---|
| locks in process memory | two copies of soulvm would both hold a sandbox’s lock |
| loops in every copy | two reapers would pause the same sandbox, and one copy’s reconcile would kill what another just started |
| live UI updates in process | a second copy’s pages would never refresh |
| rows with no node | every row written without one needs a backfill later |
| snapshots on local disk | a sandbox can only resume on the machine where it paused |
The rows were the expensive one, because every row written without a node is a debt that grows. So sandboxes, builds and snapshots record their node now, and old rows count as local. The rest are seams with one-machine defaults (Locker, Leader, Changes, Storage, pickNode), and nothing has needed more than the defaults yet.
What It Taught Me
Each failure pointed at a duty the control plane had to take on:
| Symptom | Cause | Lesson |
|---|---|---|
| 209 build folders, 24 known | the builder’s cached layers, never recorded | delete by reference |
| a sandbox past its deadline | end_time stored, never acted on | a removed layer’s duties stay |
| 35 stray snapshots, 4.7 GB | Pause returns before the files land | write the record first |
| every sandbox “gone” at shutdown | a stopping orchestrator still answers | don’t trust one look |
| images that worked, then wouldn’t | deletes behind the sweep’s back | one deleter |
Getting a machine’s state back was never the hard part. Knowing who owns each sandbox, how to recover it and when it’s safe to destroy is the control plane, and that turned out to be most of the work.
Once soulvm could run what the agents built, the next step was to run the agents themselves, in VMs of their own, and within days that was most of what I used it for. The next post is how four of them, Claude Code, Codex, OpenCode and pi, ended up behind one protocol, with no editor anywhere.
Methodology & Limitations
The incidents come from soulvm’s engineering notes, from its first weeks. What the post says about how soulvm and E2B’s orchestrator work was checked against their code, with the orchestrator at release v0.16.202610201713-75c97c0d1da and soulvm’s client generated from the matching protos in E2B’s repository (commit 0bb1c43). The timings are medians of five runs from soulvm’s end-to-end timing test, run on the server itself (a VM on an i5-12500T). The code is simplified, and “pseudocode” blocks show logic, not text. I built soulvm with Claude Code.