Zero-Copy Agent Isolation: How rudder-overlay Forks Warm Snapshots with Stacked OverlayFS, Private User Namespaces, and APFS Clones
Giving ten concurrent coding agents separate checkouts is easy until every launch copies 4 GB of node_modules, compiles 500 Rust crates from scratch, and walks 180,000 files on every VCS status check. Here is how Rudder turns workspace creation and snapshotting into metadata-only operations on both Linux and macOS.
Why plain worktrees break at 10 parallel agents
A single agent editing your main checkout inherits a warm compiler cache and installed packages, at the cost of locking you out of the tree while it works. Standard git worktree add or jj workspace add solves source collisions by materializing a fresh directory per agent, but each new directory starts completely cold:
- Cold dependencies and build artifacts: Untracked trees like
node_modules/,.venv/, and Cargo'starget/are absent. Every agent pays a 30-to-180 second bootstrap penalty plus gigabytes of duplicate blocks on disk. - Hardcoded build paths: Even if you copy a warm
target/directory into a new checkout, Cargo fingerprints, CMake caches, and DWARF paths embed the absolute path where the build originally ran, triggering cascading rebuilds anyway. - O(tree) VCS status scans: Jujutsu snapshots the working copy automatically before almost every command. On a cold or copied local tree without a valid Watchman clock, scanning hundreds of thousands of inodes adds visible latency to every agent step.
Rudder solves all three problems together by splitting workspace management into two halves: a single background warm snapshot builder that tracks trunk, and a zero-copy fork working copy (rudder-overlay on Linux, clonefile on macOS) that mounts or clones the latest ready build without copying file bytes.
Trunk-following .warm/<key> snapshots and MAX_LAYERS = 8 compaction
Inside .rudder-workspaces/<repo>/.warm/, one detached builder process per project holds builder.pid and polls currentBaseCommit every FOLLOW_POLL_MS = 1_000 (exiting cleanly after 30 minutes of idle trunk via FOLLOW_IDLE_MS). Each commit is keyed by its 12-character hex prefix (commit.slice(0, 12)) and progresses through atomic marker files: <key>.building (opened with wx exclusive create) to <key>.ready.tmp, renamed atomically onto <key>.ready so concurrent launches never read a half-written JSON descriptor.
When trunk advances, Rudder does not rebuild from scratch. It computes the file distance from every existing ready snapshot using git --git-dir <gitDir> diff-tree -r -z --no-renames --name-only and picks the nearest base. On Linux with rootless namespaces available, the refresh builds a thin OverlayFS delta layer directly on top of the previous snapshot stack:
- Fixed build path: Regardless of which commit is building, the builder mounts the stack inside a private mount namespace at the canonical path
.warm/build, applies only the changed source paths via a two-tree Git checkout, and runs install/build steps there (skipping package install altogether when lockfiles and manifests did not change). - Thin incremental upper: Compiler and bundler writes land in
.warm/<key>/, while.warm/<key>.lowerdirrecords the colon-separated stack list, top layer first, exactly as Linux OverlayFS expects itslowerdir=mount option. - Compaction at
MAX_LAYERS = 8: Deep OverlayFS stacks eventually degradelookup()latency and approach kernel option-string limits. When a stack reachesMAX_LAYERS = 8, the next refresh mounts the read-only stack view and copies the merged projection into a fresh single-layer base directory, resetting stack depth to 1 without interrupting live agents still holding older layers.
If an agent launches when the nearest ready snapshot is within NEAR_SNAPSHOT_FILES = 500 changed files of the target revision, Rudder forks immediately and patches only the delta; if trunk jumped further than 500 files (for example after a major rebase), launches wait up to WAIT_FOR_BUILD_MS = 90_000 for the fresh base snapshot.
.jj/. Inside .fork/enter, Linux merges the stacked warm snapshot lowers with the fork's private upper/ directory, aliases .warm/build to the fork, and isolates sccache per mount namespace.
Inside .fork/enter: rootless unshare -rm, path aliasing, and sccache sockets
When Rudder creates a Linux fork via rudder-jj rudder-fork-add, the host directory at <workspace> contains only .jj/. Zero source files exist in the parent mount namespace. Alongside it, Rudder writes a sibling control directory <workspace>.fork/ containing lower, alias, bin/jj, and an executable enter wrapper. Whenever Rudder's native PTY host or task runner spawns a process whose working directory has a sibling .fork/enter, inFork() routes execution through this wrapper:
#!/bin/sh
# Generated in src/warm-snapshot.ts (ENTER_SCRIPT)
F="$(dirname "$(readlink -f "$0")")"; M="${F%.fork}"; U="$(id -u)"; G="$(id -g)"
exec unshare -rm --propagation private sh -ec '
F="$1"; M="$2"; U="$3"; G="$4"; shift 4
mount --bind "$M/.jj" "$F/jjmnt"
W="$F/jjmnt/working_copy"
L="$(readlink -f "$(cat "$W/lower")")"
if [ -f "$L.lowerdir" ]; then L="$(cat "$L.lowerdir")"; fi
mount -t overlay overlay -o "lowerdir=$L,upperdir=$W/upper,workdir=$W/work,userxattr" "$M"
[ -d "$M/.jj" ] || mkdir "$M/.jj"
mount --bind "$F/jjmnt" "$M/.jj"
A="$(cat "$F/alias" 2>/dev/null || true)"
if [ -n "$A" ] && [ -d "$A" ]; then mount --rbind "$M" "$A"; fi
J="$(readlink -f "$F/bin/jj")"
for d in $(printf %s "$PATH" | tr : " ") /usr/local/bin /usr/bin "$HOME/.local/bin"; do
[ -f "$d/jj" ] && [ "$(readlink -f "$d/jj")" != "$J" ] && mount --bind "$J" "$d/jj" 2>/dev/null || true
done
cd "$M"
PATH="$F/bin:$PATH"; export PATH
SCCACHE_SERVER_UDS="\x00rudder-sccache-$(printf %s "$F" | cksum | cut -d" " -f1)"; export SCCACHE_SERVER_UDS
exec unshare -U --map-user="$U" --map-group="$G" -- "$@"' sh "$F" "$M" "$U" "$G" "$@"
Every line in ENTER_SCRIPT closes a real production failure mode we hit while running real compiler toolchains inside rootless namespaces:
- Two-stage
.jjbind dance: Mounting OverlayFS onto$Mhides the underlying$M/.jjdirectory that holdsupperandwork. Binding$M/.jjto$F/jjmntfirst, mounting the overlay withuserxattr(required for unprivileged OverlayFS in user namespaces), and then binding$F/jjmntback onto$M/.jjkeeps VCS metadata outside the overlay while exposing it where tools expect it. - Builder path aliasing (
mount --rbind "$M" "$A"): Because the warm builder compiled at.warm/build, generated files like Cargo dep-info, CMake Makefiles, or Next.js build manifests can point into.warm/build/.... Recursive-binding the fork onto.warm/buildinside its private mount namespace makes hardcoded absolute paths resolve transparently to the agent's own copy-on-write files. - Login-shell
jjbind-over: Stockjjdoes not know how to load therudder-overlayworking copy type, and login shells spawned by coding harnesses frequently resetPATH. Bind-mountingrudder-jjover existingjjbinaries onPATHinside the private mount namespace guarantees every subshell invokes the fork-aware binary. - Per-namespace abstract
sccachesockets: By default,sccachedelegates compilation to a single machine-wide background daemon. That daemon runs in the host mount namespace, where<workspace>is an empty folder and.warm/buildis unmounted, causing every Rust crate compile to fail withcouldn't find file. Because network namespaces remain shared across forks, Rudder assigns each fork a deterministic Linux abstract Unix domain socket (\x00rudder-sccache-<cksum>) so an on-demandsccacheserver starts inside the fork's mount namespace while still sharing the underlying disk/remote object cache. - Nested identity restoration: The outer
unshare -rmmaps UID 0 inside a user namespace somount(2)succeeds withoutCAP_SYS_ADMINon the host; the innerunshare -U --map-user="$U" --map-group="$G"restores the caller's real non-root UID and GID before running the agent harness, preventing tools like Claude Code, npm, or Cargo from thinking they are running asroot.
Upper-only jj snapshots without tree scans
Fast workspace creation is only half the battle if jj status or jj diff still walks 200,000 lower-layer files. In engine/src/jj_working_copy/mod.rs, OverlayWorkingCopy implements Jujutsu's WorkingCopy trait directly against the private upper layer (.jj/working_copy/upper) and the recorded base MergedTree:
- Regular files and symlinks in
upper/are hashed and inserted into aMergedTreeBuilderinitialized fromlower_tree, evaluated against.gitignoreand.git/info/excluderules. - Character devices with major/minor
0/0are kernel OverlayFS whiteout nodes;OverlayWorkingCopyrecordsMergedTreeValue::absent()at that path without touching the lower layer. - Directories carrying
user.overlay.opaque = "y"(written by the kernel when a lower directory is unlinked and recreated underuserxattr) clear all inherited lower entries beneath that prefix before adding the new upper children.
Because untouched lower directories are never opened or stat()ed, snapshotting a workspace with 3.8 GB of target/ and node_modules/ after an agent edits four TypeScript files reads only four inodes. Checking out a new tree in OverlayWorkingCopy::check_out is equally cheap: it links or materializes the target tree in lowers_root, updates overlay_state.json, and resets upper/ and work/.
Rootless OverlayFS with userxattr depends on kernel support (Linux 5.11+, disabled on some restricted container hosts). Rudder probes an actual mount once per kernel release and caches os.release() in .warm/overlay-proven; if unavailable, workspace creation falls back cleanly to standard local checkouts rather than failing halfway through launch.
macOS parity: clonefile(2) and Watchman clock resets
macOS lacks OverlayFS and Linux mount namespaces, so rudder-jj rudder-clone-add (in vcs-bridge/src/main.rs) uses Apple's filesystem-level copy-on-write primitive instead: libc::clonefile(from, to, CLONE_NOFOLLOW) from <sys/clonefile.h>.
A single clonefile() syscall clones the entire warm snapshot directory tree on APFS in tens of milliseconds while sharing every underlying physical extent until modified. Crucially, clonefile() preserves exact mtime, file size, and permission bits across the entire tree, including .jj/working_copy/. Rudder then performs three surgical adjustments before publishing the workspace transaction:
- Rewrites
destination/.jj/repowith the relative path back to the shared colocated.jj/repostore. - Loads
LocalWorkingCopy, renames the workspace in its locked state, and callslocal.reset_watchman(). Clearing the inherited Watchman clock forceslocked.finish()to rewrite the working-copy state file with a fresh timestamp while keeping every cloned file's recordedmtime/size stat cache valid, so the nextjj statustrusts the stat cache instead of rehashing gigabytes of source. - Runs
locked.check_out(&commit)before committing the repo transaction, writing only the handful of files that changed between the warm snapshot's commit and the target revision. If checkout or workspace registration fails, Rudder rolls back the unpublished workspace commit so no orphan workspace name leaks.
Isolation primitives side by side
| operation | plain jj / git worktree | linux rudder-overlay | macos rudder-clone-add |
|---|---|---|---|
| Fork disk bytes | Full source copy; 0 build artifacts | 0 file bytes (.jj/ metadata only) |
0 data blocks (APFS CoW extents) |
target/ & node_modules/ |
Cold (must reinstall & recompile) | Warm via stacked .warm/<key> lowers |
Warm via APFS snapshot clone |
jj status / snapshot cost |
O(tracked + untracked tree scan) | O(.jj/working_copy/upper entries) |
O(stat cache delta after Watchman reset) |
| Compiler path stability | Path changes per worktree | mount --rbind aliases .warm/build |
Relocatable tool cache via toolCacheEnv |
| Snapshot GC safety | Manual worktree pruning | Reference-counted via layersInUse() |
Independent CoW extents once cloned |
Reference-counted layer pruning
Because stacked snapshots share lower layers and long-running agents can outlive newer trunk builds, deleting an old snapshot directory naively would rip lower inodes out from under live OverlayFS mounts. pruneWarmSnapshots() computes the live transitive closure in layersInUse() before touching any directory:
- It retains the newest
KEEP_READY = 1ready snapshot and expands every entry in its.lowerdirchain. - For every active
<workspace>.forkdirectory, it retains both.fork/lower(the snapshot the agent's live mount namespace originally mounted) and.jj/working_copy/lower(in casecheck_outrepointed the working copy). - It sweeps orphaned
<key>and<key>.workdirectories left behind by interrupted builders (pruneUnfinishedBuilds, usingchmod -R u+rwXfirst for read-only Go/Cargo module caches) and cleans unreferenced.warm/lower-links/entries after a 5-minute grace window (UNUSED_LOWER_GRACE_MS).
OverlayFS and APFS clones give local and single-host Cloud workspaces near-instant warm starts. When workspaces span remote cas-backed images and explicit quota budgets, Rudder pairs this layer with a Rust virtual filesystem and content-addressed object store, covered in Article 02.
Spawn ten warm agent workspaces in milliseconds.
Run Claude Code, Codex, Gemini Antigravity, and OpenCode in parallel without copying node_modules or rebuilding cold Rust targets. Control every agent from your Mac or iPhone.
$ npm install -g @exla/rudder@latest