Parallel agents collide on shared test infrastructure
If you run several AI coding agents at once, each in its own git worktree (its own checkout of the repository), and your tests boot a Docker Compose database, this is for you. It starts the week I went from one developer to a swarm of agents, and my test suite began failing at random.
Each agent runs the tests as it goes, and within a day no test run was safe. The failures looked unrelated.
Nothing in the test code had changed. The root cause was one line: a hardcoded Docker Compose project name, the label Compose uses to decide which containers belong together.
Because it was hardcoded, every worktree, and so every agent, was booting and sharing the same test database container. One agent’s teardown was another agent’s mid-test crash.
# the problem: every worktree, every agent, boots this exact stack
docker compose -p myapp-test up -d db_test
# ^^^^^^^^^^ same project name everywhere =
# one shared container for the whole machineThe obvious fix is also where I went wrong first.
Isolate the container, not the database inside it
My first instinct was to give each worktree its own database name inside the shared container. It’s less disruptive, and it appears to work, until any worktree recreates the container.
Databases don’t protect you when the thing that dies is the process serving all of them. The container is the shared resource, so the container is what has to be per-worktree.
The real fix is to derive the Compose project name from the worktree path:
# the fix: key the stack to the worktree
WORKTREE_ID=$(git rev-parse --show-toplevel | shasum | cut -c1-8)
docker compose -p "myapp-test-${WORKTREE_ID}" up -d db_test
# N worktrees -> N project names -> N isolated containersEvery worktree gets its own container, its own lifecycle and its own failures. That was the theory, and then the assumptions started falling.
Getting to real isolation took four more failures, each one hiding behind the previous fix, and each one taught me something about what “isolated” actually means.
Treat every port you publish as machine-wide
Any port you publish is a host-wide singleton. Treat every one of them that way.
Give each container its own data, ideally a named volume
Separate containers means separate everything, right? It doesn’t. The containers were per-worktree, but they all pointed at the same host PGDATA bind mount. Two PostgreSQL instances writing one data directory corrupt it almost instantly. This was the killer.
The expensive part: it never says “corruption”. It says unhealthy. It says WAL crash-recovery on boot. It says too many open files. I blamed ports and file descriptors for hours because every symptom pointed somewhere else.
Keying the data directory by worktree stopped the bleeding, provided your reaper rm -rfs the mounts itself, because compose down --volumes doesn’t touch bind mounts. But the real fix was killing that bind mount entirely.
The container was never the bottleneck. The host-to-VM bridge was.
Budget boot time and open files for N stacks
Per-worktree containers migrate from scratch. Fine, I thought, that’s what containers are for. Except a fresh migration replay resurfaced a bug that only fails on from-scratch replay, hiding for months behind the shared, already-migrated container.
And every new worktree paid a 30-minute migrate before its first test. Isolation forced work I hadn’t planned: a squashed schema baseline, so a new stack boots light and fast. If fresh boot is slow, isolation is unusable, and people will quietly route around it back to sharing.
Out of files, for the whole machine
With boot time sorted, I figured what was left was a speed problem. It wasn’t. A few isolated databases booting at once exhausted the system-wide file descriptor limit: ENFILE, “too many open files in system”.
That is not the per-process ulimit -n everyone reaches for first. The host’s file table is itself a shared resource, and N isolated stacks multiplied straight into it.
The fix was the light boot, one database at a time, and a reaper that garbage-collects stale stacks by an owner-worktree label, and never touches a live one.
Assert isolation in a test on every commit
Every fix above is a change someone can undo without noticing. What actually ended the incidents is a one-screen architecture test.
It runs the functions that work out each worktree’s project name, ports, network and data location for two different worktree roots, and asserts that every one of them differs. It would have caught the worst of these bugs on the exact commit that shipped it.
Isolation is not a change you make once. It’s a property you assert, on every commit, forever.
Audit what your test stack shares with the host
Isolation doesn’t remove shared resources, it relocates them. Every shared singleton you split reveals the next one underneath: the container gave way to the host port, the port to the data directory, the data directory to the file descriptor table, the file table to the migration chain.
After all that, a fixed Docker network name meant one worktree’s DB hostname could resolve to a sibling’s container. “Database does not exist”, while it plainly existed, in someone else’s stack.
You find these one incident at a time, unless you go looking. The audit that would have saved me days is boring, and it fits on one slide.
How it works
Everything hangs off one value, the worktree root, and the test asserts that two roots never resolve to the same project name, port, network or data location.
The multi-agent era is going to hand this class of bug to a lot of teams. Shared mutable test infrastructure was fine when one person ran the suite, and it’s a correctness problem when ten agents do.
I still think the swarm is worth it. But check what your test harness quietly assumed back when it was just you.