Skip to content
StrucTime

Running several coding agents at once without them colliding

Four agents on one working copy give you four processes overwriting each other's files, with no record of who won.

The appeal is obvious. If one agent handles a task in twenty minutes, four agents should handle four tasks in the same twenty. The arithmetic works right up to the point where all four write to the same directory.

What goes wrong

Two agents editing one working copy produce silent overwrites instead of a merge conflict, which would at least be visible. Agent A reads a file, thinks for ninety seconds, and writes its version back over the edit agent B made in the meantime. Nothing errors. The lost change is discovered later, by someone who assumes they imagined writing it.

Then there is the shared build. Two test runs in the same directory fight over the same output folder, the same port, the same database. Failures appear that reproduce nowhere, which is the most expensive kind.

Isolation, then merge

The fix is old and boring: give each agent its own copy of the repository. A git worktree does this cheaply, sharing the object store while keeping separate checked-out files. Each agent branches, works, commits. Nothing is shared until a merge step at the end, where conflicts surface as conflicts, in the open, with both versions available.

This has a cost, a few hundred milliseconds and some disk per agent, so it is worth doing when agents write and not when they only read. Four agents reading the codebase to answer four questions need no isolation at all.

Tasks that should not be parallel

Parallelism helps when tasks are fully independent. It hurts in three cases worth naming.

Tasks that touch the same file. Two features in one module will conflict at merge, and resolving that conflict costs more than doing them in sequence would have.

Tasks with a real dependency. Implementing the API and the client that consumes it at the same time produces two guesses about the interface. One of them is wrong.

Anything that needs a shared external resource. A single test database, a rate-limited third-party service, one deployment target. Concurrency there just moves the queue.

The shape that works

Split by directory or by subsystem rather than by ticket. Give each agent a task it can finish without needing another agent’s output. Merge in a defined order, run the full test suite once after merging rather than in each branch, and read the combined diff.

StrucTime Code runs several agents this way, each in its own copy of the repository, merged at the end. The isolation is what makes the parallelism safe.

More on this topic