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".
Also found by searching
- ps shows 40 cargo and rustc processes, 18 git daemon test servers, 19 agents running in worktrees
- fanning out several Claude Code agents on my Rust repo slows my laptop to a crawl with parallel cargo builds
- how do I serialize cargo build and test across many parallel coding agents in git worktrees on macOS?
- flock not found on macOS, need a lock so only one cargo runs at a time across subagents
- machine very slow, 40 cargo rustc processes and many git daemon and gpg-agent leftovers from parallel agents
- when I fan out many claude agents with worktrees on a rust repo my laptop grinds to a halt
- how do I serialize cargo builds across multiple agents on macOS without flock?
- limit concurrent cargo build and test across worktree-isolated subagents, lockf wrapper, kill runaway processes