Srijan Guchhait

Running parallel coding-agent lanes on a server

What one small server needs so that several coding agents can work at once: worktrees, a memory guard, a lock for heavy jobs and a time limit on every command.

  • agents
  • linux
  • reliability

I run several coding agents at the same time on one server. Each task gets its own lane. A lane is one agent session in its own terminal tab, with its own written brief. This post covers what the server needs so that lanes do not hurt each other. My server ran out of memory once and rebooted. That event shaped most of the rules below.

One checkout for each lane

Two agents that edit the same working directory will overwrite each other. A git worktree fixes this. Each lane gets its own directory and its own branch, and all worktrees share one object store. A new worktree costs little disk space and starts fast.

git worktree add ../lane-fix-login -b lane/fix-login main

The lane works only inside that directory. When the work is merged, I remove the worktree and delete the branch. The branch name carries the lane key, so I can match a branch to a lane at a glance.

Memory is the first limit

A lane is more than an agent. It also starts builds, test runs and dev servers. Those child processes use the memory. When the host runs out, the Linux out-of-memory killer picks a process to stop. It may pick the wrong one, and the machine may become unresponsive before it acts.

I now check memory before I start a lane. The check reads MemAvailable, which counts memory that the kernel can reclaim. MemFree is smaller and gives a false alarm.

#!/usr/bin/env bash
set -euo pipefail
min_free_mb=${MIN_FREE_MB:?set the limit for this host}
free_mb=$(awk '/MemAvailable/ {print int($2/1024)}' /proc/meminfo)
if [ "$free_mb" -lt "$min_free_mb" ]; then
  echo "HOLD: ${free_mb} MB free, limit ${min_free_mb} MB"
  exit 1
fi

The script has no default limit on purpose. The right number depends on the host and on what your builds need. A launcher calls the script and starts the lane only when it exits with zero. My harness runs the same kind of check for memory, disk space and the number of open panes.

A small cap on open lanes

More lanes do not mean more finished work. I can verify about three lanes at a time. My hourly loop therefore keeps at most three lanes open. It starts at most two in one run. It starts none while the host is loaded. Work waits in a backlog until a slot frees up.

One heavy job at a time

Builds and test suites cause the memory spikes. Two lanes can run them at the same moment and push the host over the limit. A lock file prevents that. The flock tool from util-linux makes the second job wait until the first one ends.

flock "$HOME/.cache/heavy.lock" bun run build

Every lane uses the same lock path for builds, test suites and local servers. The rule is simple: one build or one server at a time on the host. The wait costs a little time. It saves a crash.

A time limit on every command

A stuck command keeps its memory and blocks the lane. I learned this from recursive searches. A search through a very large source tree ran far past its budget and hung my shell tools more than once. The fix has two parts. I put timeout in front of any command that can run long, and I exclude build output and dependency folders from searches.

timeout 20 grep -rIl --exclude-dir=node_modules --exclude-dir=.git "pattern" src

When a command hits the limit, the exit code is 124. I treat that as a failure to investigate. I do not treat it as a reason to rerun the same command with a longer limit.

Scratch files in one place

Lanes create scratch files: build output, copies of the site for a local server, logs. I give them one folder under the user cache directory. Cleanup is one command, and no lane writes outside its own area. I also never put scratch files in a shared system directory, because another lane or another user may read them.

State on disk survives the reboot

The harness keeps a registry of lanes as JSON files. After the out-of-memory reboot, a boot command read the registry and compared it with the panes that still existed. Lanes whose panes had vanished were marked lost. My own session came back too.

My first step after the restart was git status in the repository. Git is the ground truth for the work. A message from before the crash says what an agent meant to do. The working tree says what it did.

After that reboot, I set a memory cap for my session and the rule above: one build or one server at a time.

← All posts