Dev.to AI 🤖 Ai 👁 0 📖 4 min read

We let two Claude Code agents change the same line at once

The usual advice for running several Claude Code agents is to give each one its own git worktree. We wanted to see what that buys you when two agents want the same line, so we tested it. The same afternoon, our own agent

The usual advice for running several Claude Code agents is to give each one its own git worktree. We wanted to see what that buys you when two agents want the same line, so we tested it. The same afternoon, our own agents ran into the real version.

The test

On 25 November 2026 we made a throwaway repository with one file that matters, src/price.js:

export const TAX_RATE = 0.10;

Then we started two Claude Code sessions (version 2.1.331) at the same moment, each in its own worktree, with conflicting jobs. Our AI agent sessions ran the commands; nobody typed them by hand. The core of each one:

claude -p "In src/price.js change TAX_RATE to 0.15. Then commit with git: git commit -am 'Tax 15'. Reply with the commit hash only." --worktree tax-a
claude -p "In src/price.js change TAX_RATE to 0.125. Then commit with git: git commit -am 'Tax 12.5'. Reply with the commit hash only." --worktree tax-b

Our wrapper script also pinned the model and kept our own settings and MCP servers out of the run.

What happened:

  • Each session finished in about seven seconds.
  • Each one edited the file and committed on its own branch, worktree-tax-a and worktree-tax-b. Neither touched the other's files.
  • The main checkout still said 0.10.
  • git worktree list showed both worktrees as locked, and .claude/ showed up as an untracked folder in the main checkout.

During the work, nothing collided. The collision waited for the merge. Merging the first branch was a fast-forward. Merging the second gave this:

Auto-merging src/price.js
CONFLICT (content): Merge conflict in src/price.js
Automatic merge failed; fix conflicts and then commit the result.

That is the right result. Git cannot know whether tax is 15% or 12.5%. Somebody has to decide, and that person needs to know two agents were asked two different things. The worktrees kept the agents from overwriting each other's files. Neither agent ever learned the other one existed.

The same thing, for real, on the same day

That afternoon our agents built three new guide pages for our website, one pull request each. Each page needs its route added to a list in the site's worker.js. That list is a single line. On main today it holds 51 routes in about 1,200 characters.

Every pull request added one entry to that one line. Nothing else overlapped: the guides index page and the sitemap merged cleanly. A trial merge before review had already shown that the second and third pull requests would conflict on that line. Knowing in advance did not make it free.

  • The first pull request merged.
  • The second was approved 12 seconds later, while it already conflicted. The repair, merging main in and keeping both routes, went up about a minute and a half after the approval. GitHub dismissed the approval 5 seconds after the push, so it had to be approved again.
  • The third was repaired in the same minute. When the second merged, about an hour and a quarter later, the third conflicted on the same line again and needed another repair.

Each repair was mechanical: merge main, keep every route from both sides, change no wording, rerun the tests (195 passed each time). From the first merge to the last took about an hour and a half. None of the repairs was hard. Most of that time was a pull request waiting for review while the branch under it kept moving.

So the collisions that cost us time showed up at merge time, on a small shared structure, and in the review step that resets when a branch changes. While the agents worked, the worktrees kept them apart just as the test showed.

Two more from our notes

Worktrees share more than you might expect. On 2 November two of our agent sessions, each in its own worktree of the same repository, each used git stash. Each pop brought back the other session's changes. Git keeps one stash list per repository, and every worktree pushes and pops the same stack. We saved both diffs as patches and put each one back in its own worktree.

On 4 November a local check said a branch would merge cleanly. The pull request opened as conflicting in seven files. The worktree's copy of origin/main was an hour old, and another change had landed in the same spots since.

What we do now

Right before opening a pull request, the agent fetches and checks the merge against the real main:

git fetch origin
git merge-tree --write-tree --name-only origin/main HEAD

No git stash in a repository where other agents are running. To set changes aside, we use a patch instead:

git diff > x.patch
git apply -R x.patch

When several branches add to the same shared list, we expect the conflict, merge them one at a time, and repair each in turn by keeping both sides. The route list is still one line. We have not fixed that yet.

The setup itself (worktree commands, subagents, the experimental agent teams switch and its limits) is in our guide to running several Claude Code agents at once. One finding from that guide is worth repeating here: a claude -p run with agent teams switched on, asked for two teammates, got two ordinary subagents and no team. Teammates start only in interactive sessions.

Disclosure

We build KoalaFix, a tool for coordinating a team's AI coding agents, so we have an interest in this topic. Weigh what we say with that in mind.

This post was drafted with AI assistance and checked by a founder.

📰 Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.