Two AI Coding Agents on the Same Repo
Two agents in one checkout do not conflict politely like two developers do. Isolate them with a worktree each, then merge in the order that keeps your lockfile honest.
Running two AI coding agents on the same repo at once is the obvious next move after one agent starts earning its keep, and it works, provided you stop them sharing a working directory. Two agents in one checkout do not conflict politely like two developers do, because neither of them knows the other exists. They will both edit the same file, both reinstall dependencies, and both assume the branch belongs to them.
The fix is not coordination between the agents. It is isolation at the filesystem level, plus one rule about the order you merge in. Git already ships the tool for the first part.
The three ways two agents collide
These are worth naming separately because they fail at different moments and only one of them is loud.
Same file, same minute
Two agents editing one file in one checkout produce last-write-wins, silently. There is no merge, no conflict marker and no error, because from Git's point of view only one set of changes ever reached the working tree. Agent A reads the file, reasons for ninety seconds, writes its version, and overwrites the edit Agent B made in between. The output looks like an agent that forgot its own work.
Shared dependency and build state
Both agents will happily run an install. In one checkout that means one lockfile being rewritten under both of them, and a dependency tree that matches neither task. It also breaks the reproducible install path: npm's own docs say npm ci requires "an existing package-lock.json or npm-shrinkwrap.json", that "if dependencies in the package lock do not match those in package.json, npm ci will exit with an error", and that "it will never write to package.json or any of the package-locks: installs are essentially frozen". A half-updated lockfile therefore fails CI rather than quietly recovering, which is the good outcome compared to shipping it.
Both agents own the branch
The third mode is the one that costs an afternoon. Each agent commits to the branch that happens to be checked out, so you get interleaved commits from two unrelated tasks, and any rebase or reset one agent performs rewrites the other's history underneath it. Nothing is lost, since the commits are still reachable, but untangling them is manual work that neither agent can help with because neither has the context.
Give each agent its own working tree
Git's answer predates agents by a decade. A linked worktree is a second checkout of the same repository, with its own working directory, its own HEAD and its own index. The git worktree documentation describes a linked worktree as "sharing everything except per-worktree files such as HEAD, index, etc.", and notes that "in general, all pseudo refs are per-worktree and all refs starting with refs/ are shared".
That split is exactly the property you want. The object database and your branches are shared, so nothing has to be pushed between the two agents. The checkout, staging area and current branch are not, so neither agent can touch the other's files.
# one worktree per agent, each on its own new branch
git worktree add -b agent/auth-refactor ../wt-auth
git worktree add -b agent/invoice-export ../wt-invoices
git worktree list
# /repo 9f21a0c [main]
# /wt-auth 9f21a0c [agent/auth-refactor]
# /wt-invoices 9f21a0c [agent/invoice-export]Point each agent at one directory and nothing else. Most agent runners take a working directory argument or inherit the shell's; either way the agent's view of the project is the worktree path, and the other task is invisible to it.
Git enforces the branch rule for you. The docs state that add checks out an existing branch only "if it's not checked out anywhere else, otherwise the command will refuse to create the worktree", with --force described as overriding that safeguard. Do not reach for --force here. The refusal is the feature: it is Git telling you that two agents are about to own one branch.
Cleanup is deliberate too. git worktree remove only removes clean worktrees, since "only clean worktrees (no untracked files and no modification in tracked files) can be removed", and "the main worktree cannot be removed". That means an agent's half-finished work cannot be deleted by accident, and it also means you need to decide what to do with it rather than letting it rot in a stale directory.
Three things each worktree still needs
Its own install. A fresh worktree has no node_modules, no virtualenv and no build cache. Run the install once per worktree, then let each agent keep its own.
Its own environment file, since environment files are usually untracked and will not appear in a new checkout. Copy a redacted one in rather than pointing both worktrees at a shared file.
Its own ports. Two agents running a dev server or test suite on the same port produce failures that look like broken code. Assign a port per worktree and put it in that worktree's environment file.
Item three catches almost everyone on the first attempt, and it produces the most misleading error of the three, because a port collision surfaces as a connection failure inside the test run rather than as a port error.
The merge-order rule
Isolation solves concurrent editing. It does not solve the fact that two branches may both change generated files, and that is where a parallel run still bites. The rule is simple: merge one branch, regenerate the shared artefacts, then merge the second.
Artefact | If both branches changed it | Correct move |
|---|---|---|
Lockfile | Conflict or a silently wrong tree | Take the first branch's, re-run the install on the second |
Database migration | Two migrations with the same ordinal | Renumber the second after the first lands |
Generated client or types | Conflict in a file nobody edits by hand | Regenerate after the first merge, never hand-merge |
Changelog or version file | Trivial conflict, every time | Resolve by hand, keep both entries |
Never hand-resolve a lockfile or a generated file. Take one side, run the repo's own generation command, and commit the result. A hand-merged lockfile installs a dependency tree that no tool produced and nobody can reproduce.
Migrations deserve particular care, because the collision is invisible until deploy: two agents each add a migration numbered after the last one they saw, and both are valid in isolation. The sequencing discipline for that is the whole subject of AI coding agent database migrations, and the short version is that whichever branch merges second gets renumbered.
When one agent is the better answer
Parallelism is not free, and two agents are a poor fit for some work. Two tasks that touch the same module will produce two plausible refactors of it, and you will spend longer reconciling them than one agent would have taken to do both. Anything with a shared interface change belongs to one agent, in one branch, in sequence.
The split that works is by seam: two features in different directories, a feature and a test backfill, a refactor and a documentation pass. The split that does not is by layer, where one agent takes the API and the other the client of that same API, since each is guessing at the contract the other is writing.
Review capacity is the real ceiling. Two agents produce two branches needing one human's attention, which is the pattern already visible in the data behind AI coding agents and pull request volume. If you cannot review two branches today, running two agents only moves the queue.
Keeping each agent inside its own branch is the same discipline as keeping an AI coding agent inside one feature branch, applied twice, and the Git habits in using Git with an AI coding agent matter more with two of them running. Choosing the runner itself is covered in AI coding tools.
FAQ
Can I just use two clones instead of worktrees?
You can, and it works. Two clones cost disk and require pushing through a remote to move commits between them. Worktrees share one object database, so branches are visible to both immediately, which is why they suit short-lived parallel tasks better.
What if both agents need to run the test suite?
Give each worktree its own ports and its own test database, and run them. The common failure is a shared database being migrated and truncated by two suites at once, which produces flaky failures in both.
How do I stop an agent wandering into the other worktree?
Keep the worktrees outside the repository directory, as sibling paths, and restrict the agent's permitted paths if your runner supports it. An agent given a project root above both worktrees will eventually read, and then edit, the other one.
Is it worth it for two small tasks?
Usually not. The setup is a few minutes, but the review and merge tax is constant. Two agents pay off when each task is big enough that waiting for the first to finish would cost more than reconciling two branches.
How do agents hand work to each other?
Deliberately, in writing. A handoff between two agents needs the diff and the test output rather than one agent's summary of what it did, and handing off a task between AI coding agents sets out the fields that have to travel with it.
How did this land?
About the author

Developer Advocate
Steve builds something with Swarmz every week and writes up what worked, what broke, and what he'd do differently. Tutorials and hands-on guides are his lane.


