claude work lock/queue A developer built work.zsh, a small job coordinator on kernel file locks that lets multiple coding agents and people share one workstation for builds, tests, fuzzing and benchmarks. The tool assigns each job a kind (bench, fuzz, build) with different exclusivity rules, so benchmarks run alone while builds can overlap, and it integrates with Claude Code to show the queue and stop agents from running heavy work outside it. The design was motivated by failures including a baseline discarded after running beside builds at 100 °C and an unlocked analysis script skewing an A/B comparison by about 45%. 2026-12-01 · How we keep benchmarks honest on one workstation that several coding agents and people build, test, fuzz and measure on at the same time: a small job coordinator, work.zsh , built on kernel file locks, and a Claude Code integration that shows its queue and stops agents from running heavy work outside it. Everything needed to rebuild it is here, the full scripts included appendix . We built it for the development of ada https://junkdog.github.io/ada-md/ , a terminal markdown viewer and pager whose speed is benchmarked against other viewers, by coding agents that build, fuzz and measure on one shared machine. Nothing in the design is specific to ada, but the figures under "In practice" come from that project. One machine, several workers at once. In our case two Claude Code agents, each in its own checkout of the same repository, plus scratch trees for experiments, all on one 32-thread workstation. Their work comes in three kinds, and they don't mix the same way: - Benchmarks must have the machine to themselves. A compile beside a timing doesn't just add noise: it changes clocks, heat and cache state. We once threw away a baseline that ran beside builds with the CPU at 100 °C, and an analysis script running outside any lock skewed an A/B comparison by ~45%. - Builds and test suites load every core but don't care what runs beside them, as long as it isn't a benchmark. Serializing them behind each other wastes hours. - Long-running jobs in our case fuzzing campaigns are happy beside builds but not beside each other, since two of them would fight for the same cores for hours. A single global "exclusive" lock gets the first point right and the other two wrong. That's where we started, and it made agents wait on each other's compiles all day. What we needed was a lock that knows what kind of work each job is. Three more things went wrong often enough to design for: - A job's child process outlived its parent a fuzzer's fork worker, reparented to init and silently kept holding the lock. Nobody could tell what was holding it. - A "5-minute" run kept going for 45 minutes because the stop signal never reached it, and nobody asked until much later. - Agents waited for background work with polling loops that matched their own shell pgrep -f , and hung forever. Every heavy command is run through one wrapper, with a kind and a label: work.zsh build "unit tests" cargo test --release --no-run work.zsh bench --cpus 2 "parser speed" cargo test --release --test speed -- --ignored --nocapture work.zsh fuzz "parser smoke" cargo fuzz run parser -- -max total time=600 | kind | for | may run beside | |---|---|---| | bench | anything timed: microbenchmarks, hyperfine, end-to-end timing runs, A/B comparisons | nothing | | fuzz | long jobs of which only one at a time makes sense: fuzzing, soak tests, corpus minimization | builds, not another fuzz | | build | compiles, test suites, lints, analysis scripts that load cores | builds and one fuzz | The rules around it: 1. Compile apart from the measurement. Build the benchmark binary under build , then time the built binary under bench . A compile never holds the whole machine. 2. The wait is the kernel's. The wrapper blocks in flock until its locks are free: no polling, no sleep loops, no PID files. Start a job in the background and wait for it to exit. 3. The exit status is the command's, so the wrapper drops into scripts and && chains unchanged. 4. One shared state directory for the whole machine, whichever checkout or user the job comes from. The parts: | part | what it does | |---|---| | work.zsh | the lock and queue: takes the right locks for the kind, runs the command, logs everything | | the job registry | one JSON file per waiting or running job, each guarded by a lock of its own, so liveness is never guessed | | the event log | an append-only JSONL history: queued, started, ended, skipped, capped, lost | | the busy gate | a benchmark's last check: the CPUs must actually be idle, which catches work that never took the lock | | Claude Code integration | the queue shown above the prompt, and a guard that refuses heavy commands run outside the wrapper | Everything lives in one directory WORK DIR , by default ~/.local/state/work-lock ; every user of the machine must point at the same one : | file | what | who holds it | |---|---|---| | ccd0 , ccd1 , … | one lock per L3 cache domain a CCD on AMD; a single ccd0 on most Intel parts | a bench exclusively all of them , others shared their CPUs' domains | | fuzz | one long job at a time | a fuzz job, exclusively | | gate | the queue's front door | every job while it takes its locks; a waiting bench for as long as it waits | | jobs/ID.json | the job's record, one line of JSON, rewritten atomically write .tmp , then mv | — | | jobs/ID.lock | the job's liveness lock | the job, exclusively, for its whole life | | log.jsonl | the event history, append-only | — | A job's ID is YYYYMMDD-HHMMSS-PID . The script reads each online CPU's L3 cache id from /sys/devices/system/cpu/cpuN/cache/index3/id , so it needs no table of the machine. --cpus 0-15 or --cpus 2,4,6 pins the command with taskset and decides which domains the job locks; without it the job may use every CPU, unpinned, and locks every domain. Per-domain locks leave room for one refinement, off by default: with WORK SPLIT=1 , a bench pinned inside one domain locks only that domain, and a build that finds a domain held pins itself to the free ones. Measure before turning it on. The domains still share power, thermal headroom and memory bandwidth, so a build on one can move a benchmark on the other. Linux's flock doesn't favor a waiting exclusive locker. A bench waiting for ccd0 exclusively would wait forever while new builds keep arriving and taking it shared, since there's always at least one build holding it. So every job first takes gate exclusively, takes its domain locks, then releases gate . A build passes through the gate in a moment. A bench waiting for running builds to drain holds the gate the whole time, so builds started after it queue at the gate instead of overtaking it. In practice the queue is first come, first served, without any queue data structure. A fuzz job waits for the fuzz lock before the gate. Otherwise a long job queued behind another long job would hold the gate, and with it every build, for hours. php flowchart TD A take ID.lock, write ID.json state=waiting, log queue -- B{fuzz?} B -- yes -- C wait for the fuzz lock B -- no -- D C -- D wait for the gate D -- E wait for each domain lock: bench exclusive, others shared E -- F release the gate and close its descriptor F -- I{bench?} I -- yes -- J sample /proc/stat until its CPUs are idle, or give up: exit 75, log skip I -- no -- K J -- K ID.json state=running, log start K -- L run the command under taskset and, for fuzz, timeout L -- M log end with rc; remove ID.json and ID.lock, or mark it leftover 1. Register. Take jobs/ID.lock before writing jobs/ID.json . A job file whose lock nobody holds then always belongs to a job that's gone. Log queue with the directory, git branch, CPUs, locks, load average and command line. 2. Take the locks in the order above. 3. Run. Every lock descriptor is inherited by the command and everything it starts. The locks are therefore held until the last descendant exits, and the kernel drops them however things end: crash, SIGKILL , closed terminal. There is nothing to clean up after a death. The gate's descriptor is closed first, so the command doesn't inherit the gate. 4. End, from an EXIT trap, so also on signals. Close ID.lock 's descriptor and try to take it back. If that works, no descendant is left: remove the job's files. If not, a process the command started still holds it: the record is rewritten as leftover and stays. Either way, log end with the exit code and run time. Every status call tries a non-blocking shared lock on each jobs/ID.lock : - Lock free: the job is gone. The reader renames the record to ID.json.gone the rename makes sure exactly one concurrent reader logs it , logs lost killed without its trap running or clear a leftover whose processes have now ended , and removes the files. - Lock held, wrapper's PID dead: the job is leftover . Its processes still run, and fuser -v WORK DIR/jobs/ID.lock names them. No PID is ever guessed from a command line. That's the whole fix for the stray-fuzzer problem: the orphan still holds the lock, and now the lock tells you who it is. Locks only see jobs that take them. So once a bench holds its locks, it samples /proc/stat for the CPUs of its domains over WORK BUSY WINDOW seconds 5 . It starts only when their mean busy share is under WORK MAX BUSY percent 5 , retrying for up to WORK WAIT MIN minutes 30 . If they never get quiet it exits 75 without running the command. That catches an unwrapped build, a stray script or a busy desktop app. Every bench pays at least one window 5 s before it starts. One known weakness: a 5-second mean lets a bursty load through. One day a busy desktop made interactive benchmark cells read 2–4× slow while every bench passed the gate. For long, important runs, also make sure the desktop is quiet. A fuzz job runs under timeout -k 60 with a cap of WORK FUZZ MAX minutes 150; 0 for none . timeout puts the command in its own process group and signals the whole group: SIGTERM at the cap, SIGKILL 60 s later. The job ends with exit 124 and a capped event. A process the command moved into a session of its own setsid is outside that group, and stopping it is that script's job. Put a per-step cap inside long scripts too: the wrapper's cap is the last line of defense, not the plan. work.zsh status one line per waiting, running or leftover job work.zsh status --json {now, dir, jobs: the job records , recent: the last WORK RECENT ends } work.zsh log N the last N events 20 , tabulated through jq when it's installed status in a terminal: work.zsh: jobs in /home/you/.local/state/work-lock, 2026-12-01T17:30:16+01:00, load 1.25 1.06 0.75 running build "b1" 2s project main , cpus 0-31, pid 331803 waiting bench "bench" 1s project main , cpus 0-31, pid 331825 waiting build "b2" 1s project main , cpus 0-31, pid 331842 A job record: {"id":"20261105-120000-42","pid":42,"kind":"bench","label":"print A/B","state":"running", "checkout":"/home/you/src/project-second","branch":"dev","cpus":"0-15","pinned":1,"locks":"ccd0,ccd1", "queued":1793874000,"started":1793874010,"command":"tools/ab.zsh a b"} | exit status | meaning | |---|---| | the command's | the job ran | | 75 | a bench not run: its CPUs never got quiet skip event | | 124 / 137 | a fuzz job stopped at its cap capped event | | 129 / 130 / 143 | the wrapper itself got SIGHUP / SIGINT / SIGTERM | | 2 | usage, a bad --cpus or WORK FUZZ MAX | | variable | default | effect | |---|---|---| | WORK DIR | ~/.local/state/work-lock | the shared state directory | | WORK MAX BUSY | 5 | a bench's busy-gate threshold, mean percent; off skips the check | | WORK BUSY WINDOW | 5 | the busy gate's sample window, seconds | | WORK WAIT MIN | 30 | how long a bench waits for idle CPUs before exit 75 | | WORK FUZZ MAX | 150 | a fuzz job's cap, minutes; 0 for none | | WORK SPLIT | 0 | 1: per-domain benches, and builds that pin themselves to free domains | | WORK RECENT | 8 | how many ended jobs status --json lists | Log events are queue , start , end , skip , capped , lost and clear . Each has t ISO time , ts epoch , ev and id , plus the kind and label for lost and clear , the whole job record under job . start carries wait s , end carries rc , run s and leftover . The lock only helps if every agent uses it, and agents forget. Four layers make sure they do: instructions, one memory shared by every checkout, a guard, and a visible queue. In the project's CLAUDE.md or the user's global one : Heavy work goes through the work lock - Run every build, test suite, benchmark and fuzz run as tools/work.zsh KIND --cpus LIST LABEL CMD… : bench for anything timed, fuzz for long fuzz or soak runs, build for everything else that loads cores. - Compile under build first, then time the built binary under bench . Never build inside a bench job. - Start jobs with run in background and wait for the completion notification. Never write a polling loop, and never identify a process with pgrep -f / pkill -f : they match your own shell. - Check tools/work.zsh status before a foreground build: behind a long bench it will exceed the tool timeout. - A queued job that doesn't start: read status . A leftover job's processes are named by fuser -v