Writing · Tools

Running Several Coding Agents on One Codebase

How we keep Claude Code and Codex agents from overwriting each other’s work, on one machine or several.

This website is one repository, and on a busy day several AI coding agents work on it at once: one on the comic, one on the infrastructure, one on the main pages, one on the social card API. Each runs in its own terminal, and none of them knows by default what the others are doing. Left alone, two agents will edit the same file, run the same tests against the same build output, or deploy over each other.

Most of the answer is plain git, and we describe our setup below. The part git can’t cover is what we use Nattermatic for, a free tool of ours that lets agents on different machines claim shared resources and message each other.

One worktree per agent

Each agent gets a lane: a git worktree of its own, on its own local branch taken from master. A worktree is a second working copy that shares the one repository, so every agent has its own files on disk, its own build output and its own installed dependencies. An agent can rewrite half a stylesheet without another agent’s test run seeing it halfway through.

One command creates a lane, installs its dependencies and starts an agent in a new terminal tab, coloured and labelled with the lane’s name so a row of tabs can be told apart at a glance. The agent is told which lane it is in when it starts.

Who owns which files

Separate working copies stop agents tripping over each other while they work. They do nothing about two agents changing the same lines and meeting at merge time. For that we keep a short file in the repository that maps path prefixes to lanes: the comic owns its pages, data and stylesheet, the infrastructure lane owns the Terraform, and so on. The longest matching prefix wins, so a specific entry beats a general one. Anything that matches no lane is core, shared ground such as the layout, the build and the sitewide stylesheet, where only one agent should be editing at a time.

Work reaches master through one command, npm run land. It names the lanes a change touches, refuses a change that edits another lane’s files unless told to allow it, and says out loud when a change touches core. Then it rebases onto master, runs the whole test suite and pushes. Before that, a reviewing agent reads the diff, renders anything visual and checks the prose. It reports and never edits.

Only one session deploys: the one in the main checkout. The deploy script refuses to run from a lane. When a lane’s work has landed and should go live, its agent asks the main session to pull and deploy.

What worktrees don’t cover

A worktree separates files. Plenty of what agents share isn’t a file in the repository, and two agents with perfectly separate working copies can still collide over it. In our repository:

  • The deploy. Two deploys must never overlap, so the deploy script takes a lock. We had to write that lock ourselves.
  • A rate limit. The social card API allows a set number of calls per network address each day. Every lane runs on the same machine, so two agents testing it drain one allowance between them.
  • A checkout. One of our test tools breaks source files on purpose, to prove the suite notices, and restores them afterwards. Once it was left running in the background while another command committed, and a deliberately broken file went into a commit.

Other projects have the classic cases: a shared test database that one agent is migrating while another runs tests against it, a port both want for a dev server, a staging environment. Git has no opinion about any of these. Each team ends up writing a lock file, a convention in the agents’ instructions, or nothing, and the problem only shows up when two agents happen to overlap.

Leases for shared resources

Nattermatic gives agents leases. Before an agent uses a shared resource, it claims a lease on it, and it releases the lease when it’s done. You can ask for this in plain words: “Claim testdb before you run the migrations, and release it when you’re done.”

  • Resources are named like agents, as testdb@desk.local, and that machine keeps their leases.
  • An agent that finds a resource taken is told who holds it, why and for how long, and can ask for a message when it’s free.
  • A lease lasts 15 minutes unless it’s renewed, and 4 hours at most. Claude Code renews a session’s leases while it keeps working, and since version 0.4.3 so does Codex. A lease also ends when it’s released, when its session has gone, or when a person breaks it, so a crashed session doesn’t hold a database for ever.
  • A session that restarts keeps its leases.

For a long job that makes no tool calls, such as a database restore, a terminal or script can hold the lease for exactly as long as a command runs:

natter lease run testdb -- npm test

It doesn’t start the command if the resource is taken. Leases are advisory: they tell agents what’s in use and lock nothing. A script that wants to enforce one can ask first with natter lease check.

Messages for everything else

Some coordination is a question. The agent on the web app needs to know which endpoint the agent on the API has just added. In Nattermatic every agent has an address, name@host, named after its project folder by default, and a message wakes the receiving session even if it was idle. It reads the message, does the work and replies.

Claude Code and Codex share the network. With a Claude Code session already in ~/code/api, a Codex session in the same folder is api-2, so you can ask Claude to have Codex review a migration, and Codex’s answer wakes Claude. Our essay Agents that wake each other up explains how the waking works and how messages are kept safe.

Across machines

None of this needs everything on one computer. There’s no Nattermatic server or account: each machine runs a small node, and the nodes talk to each other directly. Machines on the same network find each other, and machines on different networks connect through Tailscale. An agent on a laptop can claim testdb@desk.local and wait its turn behind an agent on the desktop, and sessions under different Claude accounts reach each other the same way.

It runs on Apple silicon Macs and Linux. Intel Macs aren’t currently supported.

What we’d set up first

For a team starting with several agents on one repository, we’d do it in this order. Give each agent its own worktree. Write down which agent owns which paths, and have something refuse changes that cross the line. Let one session deploy. Then list what your agents share outside git, and put a lease on each item.

Nattermatic is free and MIT licensed. Setup is one line, run in a terminal or pasted to Claude Code, and it prints a prompt to paste on your next machine:

curl -fsSL https://nattermatic.com/install.sh | sh

The Nattermatic page has a one-minute demo and the full setup.