Isolating parallel agent sessions in one git checkout

in Multi-agent git

Two AI agent sessions in one git checkout share its branch and staging area, so one session's commit or stash moves the other's work without either seeing it.

  1. What do two chats in one folder share?
  2. What do you need?
  3. What broke?
  4. How does a chat know it isn't alone?
  5. How does a chat commit only its own files?
  6. When does a chat move to its own worktree?
  7. What does it still miss?
  8. Summary

Most days several Claude Code chats work in our main repo folder at once. Each one thinks it has the folder to itself. It doesn't: when chat A stages, commits, stashes or switches branch, chat B's work moves too, and neither of them sees it happen. This post follows two chats, A and B, through what went wrong and the pieces that now keep them apart. Pushing has its own lock, covered in One push at a time; the shared journal the chats read is covered in CAM in practice.

What do two chats in one folder share?

They share everything git keeps per checkout: one HEAD, one index and one working tree.

  • HEAD is the branch the folder is on. If chat A runs git switch, chat B's next edit lands on A's branch.
  • The index (the staging area) is what the next git commit will contain. If chat A runs git add ., chat B's half-done files are staged too.
  • The working tree is the files on disk, with both chats' edits mixed in.
chat Aruns git add -A chat Bedit half done one checkoutshared by both chats HEADone branch indexone next commit working treeboth chats' edits chat A's add -A stages chat B's edit
Figure 1. Two chats sharing one HEAD, one index and one working tree.

Two things follow. git status shows both chats' changes together. And any command that acts on "the whole tree", such as stash, reset --hard or clean, acts on the other chat's work as well.

What do you need?

Only git to follow the idea. The guards below are Claude Code hooks: one runs when a session starts, one before each shell command. The commit script is plain Python with no dependencies.

What broke?

Each rule we have now came from a real loss. Five of them, in order:

Date What we saw Cause What we do now
1 Sep A failed commit left 8 paths staged while a peer chat was committing git -c user.name="$(git config user.name)" expanded to empty, so git stopped with empty ident name Unstage your paths in the same turn, before you look for the cause; let git use the repo's own identity
1 Sep A commit meant to untrack 3 files reported 4 files changed, 166 insertions(+); all 3 stayed tracked git commit -- <paths> reads the files on disk, so it threw away the staged removal Untrack with a bare git commit, after checking git diff --cached --name-status holds only your paths
13 Sep A one-row fix showed 3 files, 84 insertions git commit --amend amended HEAD, and HEAD was now a peer's commit that had landed seconds earlier. It took in 84 of their lines and dropped two of our files Never amend in a shared folder; make a follow-up commit
22 Sep In another of our repos, main sat on another run's unreviewed branch for about 15 minutes, and other sessions' uncommitted work was gone An agent lane ran git reset --hard in the shared folder. It ran under Cursor, whose hook input our guards didn't read yet reset --hard is refused outright while a peer is live, and a git-level hook refuses rewinding main from any tool
27 Sep A live chat's half-written test went out in three sync: commits A session-end script commits every changed file in some folders, including a peer's That script now refuses to commit while another session is live

The 13 September case was caught only because the commit summary was 84 lines bigger than expected. Nothing failed. In all five, git did what it was told; the command covered the whole folder, and the other chat's files were in it.

How does a chat know it isn't alone?

Two hooks tell it. Each session leaves a small marker file recording its last command, and a marker updated in the last 15 minutes counts as a live peer.

When chat B starts and chat A is live, a session-start hook prints a notice. Its core line:

A git checkout has one HEAD and one index, so branch switches and staging are
global. Concurrent work here is not safe, only careful.

It then lists the rules: give git add explicit paths, never -A or .; never switch branches; never git stash, reset --hard or clean -fd, because "they hit the whole tree"; never --amend.

A second hook checks every shell command while the peer stays live, and answers in two strengths:

  • Refused (5 patterns): git reset --hard, git clean (unless it's a dry run), checkout or restore of the whole tree, switching off the git-level branch guard, and repointing a branch with symbolic-ref. The first three delete a peer's uncommitted files, and no reflog brings those back. The guard's own comment gives the reason for refusing: after the 22 September loss, an ask "is one click from that".
  • Asked (13 patterns): checkout or restore of named paths, checkout or switch, checkout-index, any other reset, stash, a dry-run clean, rebase or merge, add -A or add ., commit -a, commit --amend, forced branch moves, the session-end sync, and a hand-typed git push.

File edits, test runs and git add <path> are never gated.

How does a chat commit only its own files?

It runs commit-own with a message file and the paths it changed. The script builds the commit away from the shared index and never touches the working tree.

We built it after 26 September, when four chats in another of our repos each hand-assembled the same thing out of git plumbing commands, each one slightly different. In the flow, dashed arrows are writes into the shared checkout.

commit-own checkout your paths-F msgfile build the commitin a private index move maincompare-and-swap ✕ main moved? sync shared indexonly entries at the parent committedexit 0 exit 3nothing changed no onto your paths on other paths: rebuild on the new main, up to 3 tries mainbranch ref shared indexa peer's staged change stays new and old commit id update-index 1 2 3 4
  1. Build the commit in a private index: HEAD, plus the paths you named.
  2. Move main with a compare-and-swap that names the commit it expects to replace.
  3. If main moved on other paths, rebuild on the new one, up to 3 tries. If it moved onto your paths, stop with exit 3; nothing changed.
  4. Update the shared index only where an entry still matches the parent, so a peer's staged change stays.
Figure 2. How commit-own builds a commit and moves main.
  • A private index. It sets GIT_INDEX_FILE to a temporary file for each git command it runs, never exported. That index starts from HEAD and gets only your paths, so chat A's commit can't pick up anything chat B staged.
  • A compare-and-swap. git update-ref moves main only if it still points at the commit the build started from. If chat B committed in the meantime on other files, the script rebuilds on B's commit, up to 3 tries. If B touched the same files, it stops with exit 3 and changes nothing.
  • A careful sync. After the commit, the shared index still holds the old versions of your files, and a peer's bare git commit would undo your change. So the script updates those entries, and only where they still match the parent commit. A file chat B staged meanwhile is left alone, with a warning.

It also skips commit hooks. The pre-commit framework stashes and restores the working tree to run its checks, which is a whole-tree move.

When commit-own isn't available, the fallback is git commit -F <msgfile> -- <paths>. It ignores the index, so it can't sweep in a peer's staged file either.

When does a chat move to its own worktree?

When it edits code: source, hooks, scripts or tests. A worktree is a second folder on the same repo with its own HEAD and its own index, so none of the above applies inside it.

With a peer live, the session-start notice tells the chat to move code edits into a worktree without asking, commit there, and land the branch on main as a fast-forward. A fast-forward only rewrites the files that changed, so chat B's uncommitted files survive it. The file path decides, not the kind of chat.

Board and docs work stays in the shared folder. A worktree would give the chat its own copy of the task board, and turn a live collision on a busy docs file into a merge conflict later.

What does it still miss?

  • Every hook fails open. If a guard crashes, the command runs. Its silence doesn't prove you're alone.
  • A marker means "ran a command in the last 15 minutes". A chat idle for 14 minutes counts as live, and the notice itself says a peer "may mean idle or closed rather than present".
  • One window inside commit-own. Its sync reads the shared index, then writes it, and git has no compare-and-swap for index entries. A peer's git add of one of your files in between can be overwritten.
  • The files on disk aren't the commit. On 4 October a chat read a file in the shared folder, saw a peer's uncommitted edit, and wrote into a plan that a commit had landed it. It hadn't. Ask git instead: git show HEAD:<path>.

Summary

  • A git checkout has one HEAD, one index and one working tree, and every agent session in it shares all three.
  • Any command without explicit paths acts on everyone's work.
  • We stopped the losses by making whole-tree commands hard to run, committing from a private index with a compare-and-swap, and moving code work into worktrees.
  1. Give each agent that edits code its own worktree. It is the only piece here that removes the problem instead of guarding it.
  2. Commit by explicit path, and from a private index where you can.
  3. Refuse outright the commands that delete uncommitted files: reset --hard, clean, whole-tree checkout.