Run many coding agents on one Rust repository without overloading the machine
When to use#
You fan out several coding agents (Claude Code Agent with worktree isolation, or similar) on one Rust repository. Each worktree has its own target/ folder, so each agent runs its own full cargo build and cargo test. Agents also start their own helper agents and forks unless told not to. In one session 6 agents grew to 19, with about 40 cargo/rustc/test processes, 18 leftover git daemon test servers and several test gpg-agents, and the user's machine slowed down badly.
Steps#
Put a turn lock around cargo. On macOS
/usr/bin/lockfexists andflockdoes not. Write a wrapper in a scratch folder:#!/bin/sh # One cargo at a time across all agents: wait for the shared lock, then run.In every agent prompt, write these rules:
- Run every
cargo build,testandclippythrough the wrapper.cargo fmtmay run directly. - Do not start helper agents or forks.
- Run targeted tests (
--test <file>, a name filter) while iterating; run the full suite only before a commit.
- Run every
Send the rules to agents that are already running, including their helpers. A message to an agent that already finished wakes it up again; it only acknowledges, but expect the extra notices.
To stop the load at once, kill only processes that belong to the repository's worktrees and tests, never the user's own daemons:
;Check the process list first; agents that are still alive start new builds right away, and with the lock only one runs.
Keep fan-out small: about 6 main agents was acceptable once the lock was in place.
Evidence#
rgit, 2026-09-27/28: after the lock and rules were sent, ps showed exactly one lockf ... cargo build -q with one rustc, and the other agents waited for their turn. Before that, 19 agents were listed by ListAgents and the user reported "it slow down a lot".