Repository navigation
feat(bin): defer spawns beyond a declared per-project capacity, opt-in - #5343
Merged
kunchenguid merged 20 commits intoOct 7, 2026
Merged
kunchenguid merged 20 commits into
kunchenguid merged 20 commits into
Conversation
Teardown's walk over the root home and its registered local secondmate homes moves into bin/fm-wake-lib.sh as fm_local_firstmate_state_dirs, next to fm_firstmate_root_home, so a second consumer can count task records across this machine's homes without a copy. Teardown keeps its exact refusal wording through a thin wrapper.
A project whose machine-local resource only serves a few workers at once had no way to tell Firstmate so: every queued item was launched, and the surplus workers spent full-context turns retrying the resource. config/project-capacity in the root home now declares how many workers each named project admits at once on this machine. bin/fm-spawn.sh counts the ship and scout records on the same project origin across the root and its local secondmate homes, skipping ones whose ready PR is recorded, while holding the shared project lock through publication. A spawn with every place held exits 75 before any brief render, endpoint, worktree, record, or backlog move, so the item stays queued; batches report it as deferred. Undeclared projects keep today's uncapped dispatch, and an unreadable declaration refuses rather than guessing the limit. Refs kunchenguid#4237
Keep project-capacity admission beside main's worker-account pin so the existing pull request can merge.
Contributor
Author
|
Friendly nudge: head |
Keep project-capacity admission beside main's move of the home layout into the operational-home-layout skill.
|
…tests/fm-project-capacity.test.sh pass, and shellcheck at warning level is clean on the changed files. Each new test failed against the old code and passes now. - **ci-1 (spaced names):** a declaration line must give a name its capacity whenever the name is a valid clone directory name. `fm_project_capacity_lookup` now trims each line, skips blank lines and lines whose first non-blank character is `#`, and takes the last field as the capacity. Everything before that field is the name, so it may contain spaces. The old error cases still refuse: a single field is rejected, and trailing text leaves a last field that is not an integer. The library header and docs/configuration.md now say a name starting with `#` cannot be declared. New test `test_spaced_project_name_is_declared` declares `my heavy project 1` next to an indented comment line and gets a deferral. - **ci-2 (unreadable records):** the holder count must never silently leave out a holder. `fm_project_capacity_occupants` now refuses when a local home's state directory exists but cannot be read or listed, or when a `.meta` file cannot be read. The error names the path, and `fm-spawn.sh` shows it in its existing refusal message. New test `test_unreadable_holders_refuse_admission` covers an unreadable record in the root home and an unreadable state directory in a registered local secondmate home, then checks that the spawn is admitted once both are readable. The test is skipped when run as root. - **ci-3 (Orca lock):** any spawn that can become a holder for a capped project must take that project's lock. The lookup now also reports whether the declaration caps any project at all, and an Orca spawn takes the per-origin lock whenever it does. This covers every capped same-origin clone. It also covers some cases where no same-origin clone is capped, because a spawn cannot find clones under other directory names without searching for them. With no declaration file, Orca still skips the lock. The comments in the library and in the `fm-spawn.sh` header are updated. The Orca test now clones the origin as `project-2`, which has no declaration, and checks that its Orca spawn refuses while the lock is held and publishes no record. - **ci-4 (worktrees):** `assert_nothing_created` now also compares the project's `git worktree list` from before and after a deferred spawn. Both tests that call it take that snapshot first. Files changed: bin/fm-project-capacity-lib.sh, bin/fm-spawn.sh, docs/configuration.md, tests/fm-project-capacity.test.sh
A clone directory whose name begins with # was skipped as a comment, so that project stayed uncapped. A line is a declaration when the # is written against the rest of the name and the line ends with a capacity; a # followed by whitespace stays a comment.
…k past its memory cap. ShellCheck ran out of memory analyzing bin/fm-teardown.sh in CI (reason=memory, rc=251, peak about 8.39 GB). On current main the same file passes at about 7.29 GB. **Cause:** the new `fm_local_firstmate_state_dirs` function in bin/fm-wake-lib.sh had a conditional `. fm-secondmate-registry-lib.sh` with a `# shellcheck source=` directive inside the function. ShellCheck followed that source again, inside a function scope, wherever fm-wake-lib.sh is sourced, and bin/fm-teardown.sh is the heaviest root that sources it. Measured locally with `shellcheck --norc --external-sources bin/fm-teardown.sh`: - current main (fd325b1): 7.29 GB - main merged with this PR: 7.86 GB - the same merge without the in-function source: 7.27 GB **Rule this restores:** this change must not make any lint root heavier than it is on main. That function holds the only new nested source in the change. **Fix:** I removed the in-function source, which no caller needs. Both callers already load the registry library at top level before calling the function: - bin/fm-teardown.sh sources it directly. - bin/fm-spawn.sh, the only user of bin/fm-project-capacity-lib.sh, gets it through bin/fm-ff-lib.sh. I also documented the requirement in the function's comment and in the "Requires" note in bin/fm-project-capacity-lib.sh. No behaviour changes. **Verification:** - ShellCheck on head: bin/fm-teardown.sh peaks at 7.12 GB and bin/fm-spawn.sh at 6.68 GB, both with rc=0. bin/fm-wake-lib.sh and bin/fm-project-capacity-lib.sh lint clean. - tests/fm-project-capacity.test.sh, tests/fm-teardown.test.sh (102 ok) and tests/fm-teardown-endpoint-safety.test.sh all pass. Files changed: bin/fm-wake-lib.sh, bin/fm-project-capacity-lib.sh
Intent
Fixes #4237.
Keep #5343 (issue #4237) mergeable: it turned CONFLICTING against upstream main on 2026-09-24 after being green. Accepted behavior to preserve: project-capacity admission as accepted in the existing pull request and its maintainer triage (opt-in class).
The accepted admission stays opt-in. Without config/project-capacity, dispatch stays uncapped. With a declaration, a spawn beyond the declared number prints one deferred line, exits 75, creates nothing, and leaves the backlog item queued. Relaunches and secondmates are not counted. Holders are counted across the root home and local secondmate homes, including other clones of the same origin, until a ready pull request is recorded or the task is cleaned up. A malformed or unreadable declaration refuses the spawn. Matching is by clone directory name. A project name may begin with # when that # is written against the rest of the name. A line that is only #, or that begins with # followed by whitespace, stays a comment.
Maintainer triage treats the change as opt-in once the always-loaded backlog contract re-evaluates queued work after every teardown and heartbeat, and also after a recorded PR-ready handoff only when config/project-capacity caps that project. The conditional dispatch line in section 7 stays as it is.
Added 2026-10-04: "on opensource, some of my PRs need resync with main. make sure that is done, and CI is fully green for maintainer, on all of our work." "make sure the work is moving, nudging maintainer, making sure PRs are green and ready for maintainer"
What Changed
bin/fm-spawn.shnow reads an optionalconfig/project-capacityfrom the root Firstmate home through the newbin/fm-project-capacity-lib.sh. With no declaration, dispatch stays uncapped. When every declared place for the project is held, a fresh ship or scout spawn prints onedeferred:line and exits 75 before it creates a brief, endpoint, worktree, record, or backlog move. A malformed or unreadable declaration, or a task record that cannot be read while counting, refuses the spawn with exit 1. A batch reports such a pair asbatch: DEFERRED. Relaunches and--secondmatespawns are not counted.bin/fm-teardown.shintofm_local_firstmate_state_dirsinbin/fm-wake-lib.sh, so teardown and capacity admission share it.AGENTS.md,docs/configuration.md("Project capacity"), and the operational-home-layout skill document the file format, the#comment rules, and matching by clone directory name. The backlog contract now also re-evaluates queued work after a recorded PR-ready handoff whenconfig/project-capacitycaps that project. The branch addstests/fm-project-capacity.test.sh, registers it inbin/fm-test-run.sh, and freezes the clock in onetests/fm-contributions.test.shcase so its one-second budget cannot expire before the first forge call.Risk Assessment
✅ Low: The admission is opt-in, runs before any brief, worktree, record or backlog change, releases its lock through the existing exit trap and publication release, and matches every stated intent criterion; the traces I ran (declaration parsing, own-record skip, symlinked-home dedupe, batch exit codes, Orca lock path, teardown refactor) found no wrong result.
Testing
No baseline command ran before this step. I ran the targeted behavior test tests/fm-project-capacity.test.sh (16 cases, all passed, real fm-spawn with fake tmux and treehouse). I then built a disposable lab home with bin/fm-lab-home.sh, a throwaway project with a local bare origin, a second clone named '#repo', a registered local secondmate home, and a markdown backlog, and drove ten script-level scenarios through a real tmux pane on the private fm-lab socket. Worker launches used the documented raw launch command 'sleep 3600', so no paid worker agent ran; tmux windows, treehouse worktrees, task records and backlog moves were all real. Finally I started a real Claude primary (normal login) in the lab and drove three turns covering deferral, re-dispatch after teardown, and re-dispatch after a recorded PR-ready handoff. Two scenarios were not driven live and are reported as untested: a secondmate-kind record not holding a place, and two concurrent spawns racing for the last place. The automated behavior test covers both (cases
only this project's ship and scout records hold placesandconcurrent spawns can never both take the last placein capacity-behavior-test.log), but that is not a live result. This is a CLI change with no rendered UI, so the evidence is CLI transcripts and a pane capture rather than screenshots. One fixture problem (the bare origin's default branch) was mine and fixed before the recorded run. Overall result: all thirteen live-driven scenarios passed, and two scenarios are untested live.spawned ...with exit 0; two task records, two tmux windows, two treehouse worktrees, both items under In flightheavy 2declared and two holders, a third spawn prints one deferred line, exits 75, creates nothing, and its backlog item stays queueddeferred:naming holders cap-a, cap-b; data/cap-c holds only brief.md; records, windows and worktrees unchanged; cap-c still un…#,# heavy 9, an indented# ...line,#note-with-no-numberand a blank line aboveheavy 2, the spawn is deferred at 2, not 9, and not refused#repois capped by the line#repo 2, matched by clone directory namedeferred: project #repo admits 2 worker(s)with exit 75; after changing the line to#repo 3the same spawn is admittedheavy 3, cap-c is deferred with holderscap-a, cap-b, cap-d…bin/fm-spawn.sh cap-a --relaunchexits 0 and the cap-a pane runs the agent command againbin/fm-teardown.sh cap-d, cap-c is admitted and the next spawn cap-e is deferred with holders cap-a, cap-b, cap-cbin/fm-pr-check.sh cap-b <url>writespr=to cap-b's record, and the next spawn cap-e is admitted although cap-b's record and window still existstill under Queued, not blocked, and names cleanup or a ready PR as the retry triggerHeavy has a declared capacity, so my standing instructions call for re-evaluating the queue nowand spawns c…Evidence: Live lab transcript: real fm-spawn, fm-teardown and fm-pr-check in a disposable lab home (scenarios S1-S10)
Source: Live lab transcript: real fm-spawn, fm-teardown and fm-pr-check in a disposable lab home (scenarios S1-S10)
Evidence: Real Claude primary in the lab: pane capture of three turns plus lab state
Source: Real Claude primary in the lab: pane capture of three turns plus lab state
Evidence: Deferred spawn at capacity (live)
Evidence: Malformed declaration refuses (live)
Evidence: Shared cap across the root home and a local secondmate home (live)
Pipeline
Updates from git push no-mistakes
✅ **intent** - passed
✅ No issues found.
✅ **Rebase** - passed
✅ No issues found.
bin/fm-spawn.sh:3046- Every fresh ship or scout spawn now resolves the root Firstmate home through fm_project_capacity_config_dir before it knows whether a declaration exists. On non-Orca backends this adds no new failure, because fm_treehouse_project_lock_path already resolved the same root at line 3059. On Orca it is new: an Orca spawn from a secondmate home whose parent binding is malformed or unreachable now exits 1 with "could not resolve the root Firstmate home that declares project capacity", even when no config/project-capacity exists anywhere. This is fail-closed and matches the intent that an unreadable declaration refuses the spawn, since the spawn cannot tell whether the root declares a cap. No change is needed; the note records the one path where a home without a declaration behaves differently from before.✅ **Test** - passed
✅ No issues found.
spawned ...with exit 0; two task records, two tmux windows, two treehouse worktrees, both items under In flightheavy 2declared and two holders, a third spawn prints one deferred line, exits 75, creates nothing, and its backlog item stays queueddeferred:naming holders cap-a, cap-b; data/cap-c holds only brief.md; records, windows and worktrees unchanged; cap-c still un…#,# heavy 9, an indented# ...line,#note-with-no-numberand a blank line aboveheavy 2, the spawn is deferred at 2, not 9, and not refused#repois capped by the line#repo 2, matched by clone directory namedeferred: project #repo admits 2 worker(s)with exit 75; after changing the line to#repo 3the same spawn is admittedheavy 3, cap-c is deferred with holderscap-a, cap-b, cap-d…bin/fm-spawn.sh cap-a --relaunchexits 0 and the cap-a pane runs the agent command againbin/fm-teardown.sh cap-d, cap-c is admitted and the next spawn cap-e is deferred with holders cap-a, cap-b, cap-cbin/fm-pr-check.sh cap-b <url>writespr=to cap-b's record, and the next spawn cap-e is admitted although cap-b's record and window still existstill under Queued, not blocked, and names cleanup or a ready PR as the retry triggerHeavy has a declared capacity, so my standing instructions call for re-evaluating the queue nowand spawns c…bash tests/fm-project-capacity.test.sh- 16 behavior cases passed, exit 0bin/fm-lab-home.sh create $LABplus a private tmux servertmux -L fm-labwith FM_HOME=$LAB; all commands below were typed into that pane with send-keysbin/fm-spawn.sh cap-a|cap-b <lab>/projects/heavy --mode local-only --yolo off --harness 'sleep 3600'with no config/project-capacity - both launched, exit 0bin/fm-spawn.sh cap-c ...withheavy 2declared and two holders - onedeferred:line, exit 75, no record, no window, no worktree, no launch brief, item still under Queuedbin/fm-spawn.sh cap-c ...against six bad declarations (heavy two,heavy 0, a project named twice, a line with no name, mode 000 file, a directory) - each refused with exit 1 and created nothingbin/fm-spawn.sh cap-c ...with#,# heavy 9,#note-with-no-number, an indented# ...line and a blank line beforeheavy 2- still deferred at 2bin/fm-spawn.sh cap-d '<lab>/projects/#repo' ...with#repo 2- deferred, naming holders cap-a and cap-b from the other clone; with#repo 3- admittedbin/fm-spawn.sh cap-c ...withheavy 3- deferred, counting cap-d from the '#repo' clone as the third holderbin/fm-spawn.sh cap-a --relaunch --harness 'sleep 3600'while the project was full - relaunched, exit 0bin/fm-teardown.sh cap-dthenbin/fm-spawn.sh cap-c ...(admitted) thenbin/fm-spawn.sh cap-e ...(deferred)bin/fm-pr-check.sh cap-b https://github.com/o/r/pull/7(recorded pr=) thenbin/fm-spawn.sh cap-e ...(admitted)env FM_HOME=<lab>/mate bin/fm-spawn.sh mate-1 <lab>/mate/projects/heavy ...from a registered local secondmate home - deferred on the root home's declaration and holders; admitted afterbin/fm-teardown.sh cap-e; then rootbin/fm-spawn.sh cap-f ...deferred namingmate-1 in <lab>/mateRealclaudeprimary in the lab (tmux sessioncaptain): asked to dispatch cap-f at full capacity, then to clean up cap-c, then to record cap-a's ready PR with cap-g queuedTeardown:tmux -L fm-lab kill-server, removed the lab directory, confirmedgit status --shortis empty✅ **Document** - passed
✅ No issues found.
✅ **Lint** - passed
✅ No issues found.
✅ **Push** - passed
✅ No issues found.