Skip to content
← all posts

Beyond git worktree: snapshots for parallel agent trials

Git worktrees isolate source files, not databases or caches. Use BoxLite QCOW2 snapshots, rollback after stop, and live cloning for parallel agent trials.

Cocona L7 min read

Beyond git worktree: snapshots for parallel agent trials

Social Post (X / HN)

When multiple coding agents fix bugs, upgrade dependencies, or test database migrations in parallel, git worktree only isolates tracked source files. node_modules, SQLite files, system packages, and stray background processes still sit shared on the host, so one broken run forces a full reinstall that takes longer than the tests. BoxLite is an open-source embedded microVM runtime with QCOW2 disk snapshots and live cloning on a single machine, with no Docker daemon. From its Python SDK, you can roll back dependencies and database state to a clean snapshot, then clone isolated copies for parallel trials locally.


Why git worktrees fail during parallel agent trials

Giving each coding agent its own git worktree is the default way to test multiple fixes in parallel. For syntax refactors or pure unit tests with no side effects, worktrees have low overhead and near-zero switching cost.

Once a task touches the real runtime environment, worktrees break down fast. git worktree tracks only files under Git control, while everything else stays on the host.

Running npm install, pip install, or apt-get install across branches can overwrite global caches, build artifacts, or system shared libraries. If an agent runs alembic upgrade or edits a local SQLite or PostgreSQL data directory and breaks a migration, git checkout can't restore the schema or data. Local servers started by tests, bound ports, lock files under /tmp, and hidden config files also collide across concurrent runs.

Once a run corrupts the environment, you're stuck rebuilding a container or reinstalling dependencies from scratch. Every failed attempt triggers a multi-minute setup loop, so most of the run goes into resetting the environment.

Comparing three isolation options

On a single machine, three common ways to run parallel agent trials are git worktree, Docker with mounted volumes, and the embedded microVM runtime BoxLite.

Dimensiongit worktreeDocker + VolumeBoxLite microVM
Filesystem isolationGit-tracked files onlyWritable container layer is isolated, while mounted volumes are shared by defaultIndependent guest root filesystem and QCOW2 container disk
Dependencies and system packagesShares host package managers and global cachesRelies on image layers, and dynamic installs pollute the containerPersisted on the container disk and reused by snapshots and clones
Local database stateNo isolation, needs manual backup and restoreNo native branching inside volumes, and copying volumes is expensiveIncluded in the full-disk QCOW2 copy-on-write chain
Process and kernel isolationNo isolation, runs on the hostShares the host kernel using namespacesIndependent hardware-virtualized kernel (Apple Hypervisor / KVM)
Rollback and branching granularitySource files onlyRebuild containers or manual docker commit exportsAtomic full-disk snapshot, rollback after stop, live cloning while running

Docker works well for layered image builds and cross-team image distribution. In a tight agent trial loop, writing a Dockerfile or running docker commit on every attempt adds overhead, and containers still share the host kernel. When you need to isolate side effects and roll back repeatedly on a laptop or single server, disk-level copy-on-write is more direct.

How BoxLite disk snapshots and live cloning work

BoxLite is an Apache-2.0 microVM runtime that embeds into Python, Node.js, Rust, or Go apps as a library, with no Docker daemon and no root privileges. On Apple Silicon macOS, it uses libkrun and Hypervisor.framework. On Linux and WSL2, it uses KVM. Each Box runs a standard OCI image inside its own hardware-virtualized microVM.

Every persistent Box keeps its QCOW2 container disk under ~/.boxlite/boxes/{box_id}/. BoxLite exposes two disk operations on top of it.

Disk snapshots (box.snapshot)

The API supports create, list, get, remove, and restore. When you call create, the runtime writes a crash-recovery marker, pauses the microVM with PauseGuard and SIGSTOP for disk consistency, and renames the active disks/disk.qcow2 to an immutable snapshots/{name}/disk.qcow2. It then creates a new copy-on-write child disk at the original path so the microVM can keep writing.

When you delete a snapshot, the engine checks the full dependency chain across the active disk, other snapshots, and clone base disks. If any child disk still references that snapshot, deletion is refused.

Live cloning and export (clone_box and export)

clone_box() creates a copy whether the Box is running or stopped. During a short pause window, the runtime converts the source disk into a shared read-only base disk under bases/. After the source Box resumes, it generates a small QCOW2 overlay header file for each clone, so the source Box and every branch write to separate delta blocks without stepping on each other.

Once a trial branch passes, export() packages it into a portable .boxlite archive. runtime.import_box() can load that archive locally or onto a self-hosted boxlite serve instance.

The diagram below shows the disk chain for taking a baseline snapshot after dependency setup, rolling back a failed migration, and forking two parallel trials:

flowchart TD
    A["Initial Box disk (dependencies and SQLite ready)"] --> B["Create snapshot pre-migration"]
    B --> C["Immutable snapshot layer (snapshots/pre-migration/disk.qcow2)"]
    C --> D["Active copy-on-write child disk (migration script fails)"]
    D -->|"Call snapshot.restore after stop"| E["Rebuild clean child disk (backed by same immutable snapshot)"]
    C -.-> E
    E --> F["clone_box splits shared base layer (bases/id.qcow2)"]
    F --> G["Branch A (upgrade-v1 fails and is removed)"]
    F --> H["Branch B (upgrade-v2 tests pass)"]
    H --> I["export packages .boxlite archive"]

Minimal runnable examples

Both snippets use the current Python SDK signatures from the BoxLite repo. Set auto_remove=False so Box state persists across stops and restores.

The first snippet takes a baseline snapshot after installing dependencies. If the migration or test suite fails, it stops the Box and restores the disk to its pre-migration state:

import boxlite

runtime = boxlite.Boxlite.default()
box = await runtime.create(
    boxlite.BoxOptions(image="python:3.12-slim", auto_remove=False),
    name="migration-lab",
)
await (await box.exec("sh", ["-c", "pip install -r /app/requirements.txt"])).wait()
await box.snapshot.create(name="pre-migration")

res = await (await box.exec("sh", ["-c", "alembic upgrade head && pytest"])).wait()
if res.exit_code != 0:
    await box.stop()
    await box.snapshot.restore("pre-migration")

Note: if you want to run exec() again after stop() and restore(), fetch a fresh handle with box = await runtime.get(box.id).

The second snippet live-clones two independent branches from that clean environment, tests two dependency upgrades concurrently with asyncio.gather, and exports the passing environment as an archive:

import asyncio

trial_a = await box.clone_box(name="upgrade-v1")
trial_b = await box.clone_box(name="upgrade-v2")

exec_a, exec_b = await asyncio.gather(
    trial_a.exec("sh", ["-c", "npm install pkg@1 && npm test"]),
    trial_b.exec("sh", ["-c", "npm install pkg@2 && npm test"]),
)
res_a, res_b = await asyncio.gather(exec_a.wait(), exec_b.wait())
winner = trial_a if res_a.exit_code == 0 else trial_b
archive_path = await winner.export(dest="/tmp/exports")

Both trial_a and trial_b start stopped. The first exec() call boots a separate microVM for each clone, keeping filesystem edits, database writes, and process state isolated between branches.

Practical feel and getting started

BoxLite snapshots operate at the QCOW2 disk layer. After you call box.stop(), snapshot.restore() reverts the entire disk to the target snapshot. Once the local image cache is populated, restoring a snapshot or booting a clone takes 1 to 2 seconds, close to Docker in day-to-day use, while each copy runs inside its own hardware-virtualized kernel.

BoxLite runs on Apple Silicon macOS, Linux KVM, and WSL2 through the local open-source SDK or a self-hosted boxlite serve instance. To try this workflow, install dependencies, seed test data, and take a baseline snapshot. Run migrations and parallel trials inside clones, then export a .boxlite archive once a branch passes.