Repository navigation
feat(bin): defer worker spawns when the machine is short of memory headroom - #7235
andrewkimcanidium wants to merge 9 commits into
Conversation
|
Speaking as Kun's firstmate: thank you @andrewkimcanidium — a machine-wide headroom check before fresh ship/scout spawns (shared budget, reservation for running workers, exit 75 deferral) is a thoughtful answer to raising pool caps without OOM-killing the fleet. Diff review: new Attestation: MATCH Contract-class: new-default. Absent VISION.md: one-interface / scripts / restart / spine / vendor / scope align; authority in tension (assumes consent to defer under defaults). Outcome: waiting-ci. Main is red; no merge. When CI + no-mistakes are green on a green main, this is a captain merge-or-leave (new-default). Nothing needed from you unless CI fails. |
|
CI status on this PR (run 38451404305): 17 passed, 2 without a passing verdict, neither caused by this change. Behavior portable parallel 1 - pre-existing failure on main. The same test fails with the same two errors on main's own CI at this branch's base 6407052 (run 38316792015) and at 7df545b (run 38447546605). Behavior portable serial 2 - cancelled without a verdict. |
|
Speaking as Kun's firstmate: thanks @andrewkimcanidium, and thanks for the CI breakdown. You're right on both counts. The parallel 1 failure is The catch is that this PR's CI run was built on the old main (
Status: waiting on you for the rebase. Once CI and no-mistakes are green on current main, this goes to the captain as a merge-or-leave decision. It won't be waiting on you after that. |
6fa7144 to
82b0ac6
Compare
|
Speaking as Kun's firstmate: thank you @andrewkimcanidium for rebasing and re-raising this. It's much appreciated.
Status: waiting on CI. If it's green, this goes to the captain as a merge-or-leave decision. |
|
Real-use evidence for the footprint log, as asked: one admitted spawn and its matching teardown from The spawn line has no RSS ( |
82b0ac6 to
d78a1da
Compare
|
Speaking as Kun's firstmate: thank you @andrewkimcanidium for the real footprint lines. That closes the live gap I asked about: one admitted spawn whose matching teardown recorded
|
|
On head d78a1da5, "Behavior portable serial 2" was cancelled at the 30-minute job limit (06:48:59 to 07:19:20 UTC) rather than failing: every test that finished passed, and it was mid-way through |
|
Speaking as Kun's firstmate: thanks @andrewkimcanidium, and thanks for chasing down the serial 2 result. You're right that it was the known 30-minute job-limit cancel in I didn't rerun that job, though. Since your last push, main has moved to Could you rebase onto current main, resolve those three files and push through no-mistakes again (
|
Add bin/fm-host-headroom.sh, which measures available memory, swap, load per core, and macOS memory pressure, judges them against config/host-headroom thresholds, and keeps a per-worker footprint log with a report of typical worker memory and the concurrent load the thresholds imply. fm-spawn defers a fresh ship or scout launch with exit 75 when the machine is below them, and spawn and teardown append footprint records.
Tag every footprint record with the worker's project, kind, harness, and model, keep the log and thresholds in the root home for the whole machine, and gate each spawn on current headroom less the new worker's expected footprint, learned from earlier workers of its kind and harness. The report breaks memory down by kind, harness, and project, states the machine budget and implied concurrent count, and names project worktree pools that would bind first.
…1630), as the maintainer asked. It already includes kunchenguid#7248, the fm-pi-primary-types fix behind the "parallel 1" failure. All six PR commits are kept with no scope change. The PR's own diff is still 10 files, 1498 insertions and 4 deletions. Three conflicts came up, each just two changes landing on the same lines, and I kept both sides: - bin/fm-spawn.sh: main's Herdr project-groups hook and this PR's footprint record. - docs/configuration.md: main's paseo FM_BACKEND line and this PR's FM_SPAWN_IGNORE_HEADROOM line. - docs/scripts.md: main's updated trailer-strip and push-guard rows and this PR's fm-host-headroom row. After the rebase, the spawn case "a spawn with headroom was refused" in tests/fm-host-headroom.test.sh failed. The cause is upstream aa9ab04 (kunchenguid#7409): before sending keys, the tmux backend now runs `tmux list-windows` to check that the window exists. The fake tmux in this test answered list-windows with nothing, so the spawn rolled back and exited 1. This test also failed the same way locally on the pre-rebase head (with a different error from an unrelated preflight); it is not a defect in the gate. I made one small change, in the test only: the fake tmux now remembers the window names it creates with new-window and lists them back. That is the same pattern tests/fm-project-capacity.test.sh already uses. Results: - tests/fm-host-headroom.test.sh: exit 0, all cases ok. - tests/fm-project-capacity.test.sh: exit 0. - tests/fm-pi-primary-types.test.sh: exit 0 (Pi 1.2.0). - shellcheck on the changed test: clean. The "serial 2" failure was a timeout cancel during fm-busy-adapter-wiring.test.sh, not a test failure, so it needs no code change; it will re-run when CI runs on the new head. I did not push; the outer executor still has to commit the test change and push the rebased head
d78a1da to
083b6b3
Compare
|
Speaking as Kun's firstmate: thanks @andrewkimcanidium for the quick rebase. I compared the new head
Status: waiting on CI. Nothing is needed from you. If serial 2 hits the 30-minute cancel again, I'll rerun it. Once it's green, this goes to Kun as a merge-or-leave decision, not an auto-merge. |
|
Speaking as Kun's firstmate: thanks @andrewkimcanidium — head Contract-class: new-default (my own read, unchanged): with no I've put this in front of Kun as a merge-or-leave decision. It's not waiting on you — nothing more is needed unless he asks for a change (for example making the gate opt-in). |
Intent
"Okay, can we suggest a raised cap without potentially hitting the memory limits to kill our setup? Maybe something can keep an eye on that and see what a healthy worker load would be. As we spawn off sub-agents, we make sure that we're not hitting that critical fail."
Context: the SAP CPQ fan-out hit the treehouse workspace pool cap of 16 (the default max_trees) on the sap-cpq-axi clone, and raising the cap was proposed. The Mac running the fleet has 18 cores and 48 GB RAM; at the time of the ask about 84 agent processes used about 7 GB resident, system memory free was 41%, load average about 11. Nothing today checks machine headroom before a spawn, so a higher cap could push the machine into memory exhaustion and take down every firstmate home and worker at once.
"I wouldn't say other projects keep their caps. I feel like we should have kind of these diagnostics per all projects so we can find out what caps need to be. And since we could be running a mixture of agents we really like from different projects, I think a cap per project is kind of weird. I think it's kind of like, what is the task, right? How much memory do you think it's going to use? How many agents can we spawn for this type of project? Additional to the 48. Do you understand it's like a group problem not a specific project problem?"
What Changed
bin/fm-host-headroom.sh, which treats the machine's memory as one budget shared by every project. Itscheckcommand reads available memory, swap in use, load per core and (on macOS) the kernel memory-pressure level. It reserves the memory each running worker still needs to reach its expected size, then decides whether one more worker of the given kind and harness fits. The expected footprint is learned from mid-work samples of earlier workers in the root home'sstate/host-footprint.log, falling back to a 1536 MB default until there are enough samples.reportsummarizes typical and p90 memory by kind/harness and by project, how many more workers fit, and which project worktree pools would hit their limit first. Optional thresholds go inconfig/host-headroom. A malformed file makes the spawn refuse, and any value the machine can't measure is reported but never judged.bin/fm-spawn.shruns this check before every fresh ship or scout launch. It holds a machine-wide.host-headroom.lockin the root home from the check until the task record is published, so concurrent spawns from any home are judged one at a time. A worker that doesn't fit gets the samedeferred:line and exit 75 as a project-capacity deferral, and the batch message now names both causes.FM_SPAWN_IGNORE_HEADROOM=1skips the check for one spawn. Relaunches and--secondmatespawns are not gated.bin/fm-teardown.shbefore the worker's processes are reaped (best effort, never blocks either step). New docs cover all of this: a "Host headroom" section indocs/configuration.md, plus updates todocs/scripts.mdand the operational-home-layout skill. Tests:tests/lib.shpins a healthy headroom sample so unrelated spawn tests never defer, the newtests/fm-host-headroom.test.shis added and wired intobin/fm-test-run.sh, andtests/fm-project-capacity.test.shgets a one-line change.🤖 Generated with Claude Code
Risk Assessment
Testing
I ran the three targeted test files (all pass), then tested the gate live in a disposable lab home, with no test seam, against this Mac's real memory, load and swap readings and real Claude scout workers. The live
checkmatchedsysctl. The real fm-spawn fan-out admitted two workers, sampled their real memory use (~440 MB each) and deferred the third with exit 75. Its message shows ~21.6 GB reserved for the two running workers and the floor that was breached, and the deferred spawn published no task record.reportgave the budget for the whole machine and the pool-cap comparison. Teardown logged footprint records, and the override launched fan-3 with its notice. All six live scenarios passed. The unreadable thresholds/home scenario was covered only by repository tests, so it is marked untested for live validation. The lab, its tmux server and its workers were all removed.fm-host-headroom.sh checkon the real Mac and gets an ok verdict naming the real available memory, swap, load and pressurereportand sees the shared budget for the whole machine, the running workers' memory, the safe worker count, and whether the project's pool cap of 16 or the memory budget binds firstEvidence: Live check against real Mac measurements
Source: Live check against real Mac measurements
Evidence: Live fan-out: 2 real scouts admitted, 3rd deferred (exit 75) by reservation
Source: Live fan-out: 2 real scouts admitted, 3rd deferred (exit 75) by reservation
deferred: this machine is short of headroom for another worker (available memory 37847 MB less 21583 MB reserved for running workers and this worker's expected 11206 MB leaves 5058 MB, below min_avail_pct=20 (9830 MB) ...; 2 running worker(s) reserve 21583 MB); task fan-3 was not launched and its backlog item stays queued ... SPAWN3_EXIT=75Evidence: Live report with running workers and pool-cap comparison
Source: Live report with running workers and pool-cap comparison
Evidence: Teardown records and FM_SPAWN_IGNORE_HEADROOM override transcript
Source: Teardown records and FM_SPAWN_IGNORE_HEADROOM override transcript
Evidence: Footprint log from the live run (spawn/sample/teardown events)
Source: Footprint log from the live run (spawn/sample/teardown events)
Evidence: Targeted test log: fm-host-headroom
Source: Targeted test log: fm-host-headroom
Pipeline
Updates from git push no-mistakes
✅ **intent** - passed
✅ No issues found.
✅ **Rebase** - passed
✅ No issues found.
✅ No issues found.
bin/fm-host-headroom.sh:474-checkaccepts--modeland--projectand throws them away (bin/fm-host-headroom.sh:34-35 says they are accepted "for the caller's symmetry and do not change the verdict"). bin/fm-spawn.sh:3574-3575 still computes and passes both. Nothing in the intent or the recorded decisions needs an input that has no effect on the verdict: the expected footprint is keyed only by kind and harness, and the per-project diagnostics come from the meta-derivedproject=field in the log. A reader of the spawn call would reasonably think model and project affect admission, and they do not. Suggested remedy: remove the two flags fromcheck, its usage text, and the fm-spawn.sh call. The other option is to actually key the footprint on them, but that would add scope.🔧 Fix applied.
✅ Re-checked - no issues remain.
✅ No issues found.
✅ No issues found.
🔧 **Test** - 1 issue found ✅
bin/fm-host-headroom.sh:724- Thereportline says "Per-worker memory: no worker samples yet in <log>" when the log holds many event=sample readings, none of which count as mid-work yet (all under 600 s old). In the live run, 4 real workers had about 20 samples at ~360 MB each. The wording could mislead an operator into thinking sampling is broken; something like "no mid-work samples yet" would be accurate.fm-host-headroom.sh checkon the real Mac and gets an ok verdict naming available memory, swap, load per core, pressure level, expected footprint and reservationok: available 71% (34897 of 49152 MB), swap used 9528 MB, load 4.15 on 18 cores ... expected worker 1536 MB (default_worker_mb ...), exit 0min_avail_pct must be a whole percent from 0 to 100 or off, exit=1deferred: this machine is short of headroom ... below min_avail_pct=95, exit=75; state/ holds only .last-watcher-beat; no fm-hr-a window; no .treehouse poolspawned hr-a harness=claude kind=ship ...andevent=spawn task=hr-a project=demo kind=ship harness=claude model=haiku ... avail_mb=25559expected worker 4800 MB (default_worker_mb, fewer than 3 measured workers of kind ship)notice: FM_SPAWN_IGNORE_HEADROOM=1 - launching hr-d without checking this machine's headroomthenspawned hr-dreportand sees the shared machine budget after reservations, running workers, the implied safe concurrent count, and whether each project's pool cap or the machine budget binds firstMachine budget: 0 MB ... after 17734 MB reserved for 4 running worker(s) ... shared by every project;demo: pool cap 16; the machine budget (4 workers) binds firstevent=teardown task=hr-d ... worker_rss_mb=367written before the reap; 4 teardown records after all workers were torn downbash tests/fm-host-headroom.test.sh(targeted suite, all 19 cases ok)FM_HOME=$LAB bin/fm-host-headroom.sh check --kind ship --harness claudewith real macOS measurement: defaults, then min_avail_pct=95, then a malformed min_avail_pctRealbin/fm-spawn.sh hr-a <lab>/projects/demo --mode no-mistakes --yolo off --model haikufrom a pane on the lab's private tmux socket, with min_avail_pct=95 (deferred) and then with defaults (launched)Live sequential fan-out with default_worker_mb=4800: hr-b and hr-c admitted, then hr-d and hr-e deferred by the running workers' reservationTwo concurrent spawns from one home (hr-c and hr-d in separate panes)Held state/.host-headroom.lock from another process for 12 s while spawning hr-e, and timed the waitFM_SPAWN_IGNORE_HEADROOM=1 fm-spawn.sh hr-dpast the budgetFM_HOME=$LAB bin/fm-host-headroom.sh reportwith 4 live workersbin/fm-teardown.sh <id> --forcefor hr-a..hr-d, checking event=teardown lines in state/host-footprint.logLab teardown: tmux kill-server on the fm-lab socket, then rm -rf of the lab homeℹ️
bin/fm-host-headroom.sh:9- The header comment still says bin/fm-spawn.sh runscheck"with the new worker's kind, harness, model, and project". After the flag removal it passes only kind and harness, so this line should read "kind and harness".Live validation: ✅ go - 5 of 7 scenarios driven live against the product
check --kind ship --harness claudeon the real machine and gets an ok verdict naming the measured memory, swap, load, pressure, expected footprint and reservationbash tests/fm-host-headroom.test.sh(every case passed, including: rapid fan-out stopped at the budget, startup samples never lowering the default, an unreadable home not hiding the other homes, and the spawn defer/launch/override/refuse cases)bash tests/fm-project-capacity.test.sh(exit 0)Live:FM_HOME=<lab> bin/fm-host-headroom.sh check --kind ship --harness claudeagainst real sysctl measurements -> ok, exit 0Live:check ... --model opusandcheck --project x-> usage, exit 2Live: lab config/host-headroom min_avail_pct=95 ->check --kind scout --harness codexdefer, exit 75; malformed value -> exit 1Live:bin/fm-host-headroom.sh reporton the real machineLive: realbin/fm-spawn.sh live-1 <lab project>in a marked lab home on a private fm-lab tmux socket, real memory, min_avail_pct=95 -> deferred exit 75, no task record/footprint log/tmux window; default_worker_mb=off -> spawn refused exit 1; lab torn down✅ No issues found.
checkon the real machine with default thresholds and getsoknaming memory, swap, load, pressure, expected footprint and reservationcheckare rejected as bad usagecheckdefer with exit 75 and name the breachreportshows the machine-wide budget shared by every project and the implied safe concurrent worker countbash tests/fm-host-headroom.test.sh(exit 0, all 19 cases ok)Live in a marked lab home (bin/fm-lab-home.sh create), real sysctl measurements:fm-host-headroom.sh check --kind ship --harness claudeandcheck --kind scout --harness codexLive:fm-host-headroom.sh check --kind ship --model opusandcheck --project foo(removed flags)Live:checkwithconfig/host-headroommin_avail_pct=95 (defer) and min_avail_pct=banana (refuse)Live:fm-host-headroom.sh sampleandreportLive:bin/fm-spawn.sh task-live <lab project> --mode no-mistakes --yolo offwith FM_HOME=lab, a real tmux server on the private fm-lab socket, real measurements, a treehouse tripwire on PATH, and min_avail_pct=95Teardown:tmux -L fm-lab kill-serverandrm -rfof the lab home✅ No issues found.
fm-host-headroom.sh checkon the real Mac and gets an ok verdict naming the real available memory, swap, load and pressurereportand sees the shared budget for the whole machine, the running workers' memory, the safe worker count, and whether the project's pool cap of 16 or the memory budget binds firstbash tests/fm-host-headroom.test.sh(exit 0, includes the fan-out-stops-at-budget, startup-sample floor, unreadable-home and spawn defer/override cases)bash tests/fm-project-capacity.test.sh(exit 0)bash tests/fm-teardown.test.sh(exit 0)Lab home viabin/fm-lab-home.sh create, thenbin/fm-host-headroom.sh check --kind ship --harness claudeandreportwith real sysctl measurement, compared againstsysctl kern.memorystatus_level vm.loadavg vm.swapusageRealbin/fm-spawn.sh fan-{1,2,3} <lab>/projects/demo --scout --harness clauderun in a lab tmux session (tmux -L fm-lab, TREEHOUSE_ROOT in the lab), config/host-headroom sized so the real budget fits two workersbin/fm-host-headroom.sh reportwith 2 live workersbin/fm-teardown.sh fan-1/fan-2, then checked the event=teardown lines in state/host-footprint.logFM_SPAWN_IGNORE_HEADROOM=1 bin/fm-spawn.sh fan-3 ...overrideLab teardown:tmux -L fm-lab kill-server,chmod -R u+w+rm -rfof the lab, confirmed no leftover processes and a clean worktree✅ **Document** - passed
✅ No issues found.
✅ No issues found.
✅ No issues found.
✅ No issues found.
✅ **Lint** - passed
✅ No issues found.
✅ No issues found.
✅ No issues found.
✅ No issues found.
✅ **Push** - passed
✅ No issues found.
✅ No issues found.
✅ No issues found.
✅ No issues found.