Many agent sessions, one repository

One session committed another session’s edits. Here’s how we keep the work apart until it’s ready to come together.

A row of desks seen from above, each with its own copy of the same plan and its own ticket at work; a line from every desk runs forward and merges into one path.

On 4 September, one of our coding sessions made a commit with another session’s uncommitted edits in it. The rest of those edits were discarded. Git raised no conflict.

Both sessions were working on 2mw2lt. They had separate conversations and the same working directory. They shared the files on disk, the index that holds what is staged for the next commit, and HEAD, which tells Git where that commit goes. A commit can look perfectly ordinary in the history while carrying work the other session was still in the middle of.

Give the session a directory

We start each session that will edit the repository in a fresh worktree. From the repository, replace <name> with a directory name for the session and <branch> with its new branch name, then run this.

git fetch origin && git worktree add .claude/worktrees/<name> -b <branch> origin/main

The fetch gives the session a current starting point on origin/main. The linked worktree gives it its own files, index and HEAD. The next session can stage, commit or reset in its directory while this one keeps editing in its own.

Check where the tool is standing

On 8 September, edits from one session landed in another session’s worktree. A stash, rebase and stash pop left conflict markers in a file the running agent imported. We had separate directories by then. The foreground working directory could still fall back to the shared checkout between tool calls.

Later, an agent called EnterWorktree and moved its parent session’s working directory as well as its own. The parent could run a check and come away with a result from the agent’s checkout. That is an easy result to trust if all you read is the last line.

Run git rev-parse --show-toplevel in the shell you’re about to use. Read the path it prints. We pass the intended directory to Git on each call with git -C "$worktree", where $worktree is the absolute path to the session’s checkout. We give editing tools absolute paths as well. The path goes with the action, even when the previous call ran in the right place.

The processes still share a machine

With development servers running from several worktrees, next dev hit EMFILE: too many open files, watch. Starting it again hit the same limit. Each server had its own source files. They were all asking the same machine to watch them.

Keep the process ID when you start a background job. Our test daemon and live daemon can run the same module from different directories, so the module name is a poor way to pick which one to stop. Use the PID you recorded for the job you started.

Give each worktree its own installed dependencies, too, rather than a symlink to another checkout’s. Our console’s build refuses a node_modules symlink that points out of the worktree, and pnpm install --frozen-lockfile in the worktree hardlinks from the shared store in seconds.

Run the checks where the changes meet

In the previous post, the reviewer changed the homepage wordmark to x and the test still passed. Here, the change worth testing came from resolving a merge conflict.

The code chose where to send a review by looking at work already in flight. Resolving the conflict put an await between reading that load and committing an offer. The load could now change in the gap, while the code was still working from its earlier reading. Each branch on its own was clear of that race. The suite caught it when it ran on the combined change.

After a real conflict, rerun the checks that cover the code you just joined. Read the resolution as code you wrote. It can change the order in which things happen even if every line came from one side or the other.

Leave the logs somewhere they can be read

After one pull request merged, its worktree vanished during closeout. Its ignored verification logs went with it. The record leaves the cause open.

Move any verification inputs and logs you still need outside the worktree before merging. Finish the background jobs you started. Once the branch lands, remove its worktree. The next session can start from a fresh checkout and still read what you ran.

Read next2mw2lt builds 2mw2lt