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.
- What do two chats in one folder share?
- What do you need?
- What broke?
- How does a chat know it isn't alone?
- How does a chat commit only its own files?
- When does a chat move to its own worktree?
- What does it still miss?
- 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 commitwill contain. If chat A runsgit add ., chat B's half-done files are staged too. - The working tree is the files on disk, with both chats' edits mixed in.
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),checkoutorrestoreof the whole tree, switching off the git-level branch guard, and repointing a branch withsymbolic-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):
checkoutorrestoreof named paths,checkoutorswitch,checkout-index, any otherreset,stash, a dry-runclean,rebaseormerge,add -Aoradd .,commit -a,commit --amend, forced branch moves, the session-end sync, and a hand-typedgit 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.
- Build the commit in a private index: HEAD, plus the paths you named.
- Move
mainwith a compare-and-swap that names the commit it expects to replace. - If
mainmoved on other paths, rebuild on the new one, up to 3 tries. If it moved onto your paths, stop with exit 3; nothing changed. - Update the shared index only where an entry still matches the parent, so a peer's staged change stays.
- A private index. It sets
GIT_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.INDEX_ FILE - A compare-and-swap.
git update-refmovesmainonly 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 commitwould 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'sgit addof 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.
- Give each agent that edits code its own worktree. It is the only piece here that removes the problem instead of guarding it.
- Commit by explicit path, and from a private index where you can.
- Refuse outright the commands that delete uncommitted files:
reset --hard,clean, whole-treecheckout.