Skip to content

pool: stop warm provisioning bricking pool slots (fixes all 10 review findings on #44) - #45

Merged
webjema merged 3 commits into
mainfrom
fm/pool-slot-brick-fix-k7
Jul 30, 2026
Merged

webjema merged 3 commits into
mainfrom
fm/pool-slot-brick-fix-k7

Conversation

@ignovak

@ignovak ignovak commented Jul 30, 2026

Copy link
Copy Markdown
Collaborator

Follow-up to #44, which is merged. An independent review of it found 10 defects; this branch fixes all 10, plus one small adjacent thing that bit us today.

Read this even if you are not on macOS. #44 was APFS-clone work, so the natural assumption is that its fallout is macOS-only. It is not. Its worst defect permanently retires a pool slot on every warm, on Linux and WSL too - the clone is the macOS-specific half, but the install runs inside the leased slot on every platform, and that is what does the damage. It is live on main right now. Everything else here is ordinary review follow-up.

This PR stops new bricking; it does not repair what is already broken. optiroq's slots 1 and 4 are dirty today and stay that way. Reclaiming them is destructive and needs an explicit operator decision, separately from this PR. A green treehouse status after this merges does not mean the pool is whole.


1. Warm provisioning bricks the slot it warms - on every platform

What happens

bin/fm-pool-warm.sh leases a slot, runs the project's installer inside it, and returns it. npm rewrites the lockfile it installed from, and that lockfile is a tracked file. treehouse's reset on return is git clean -fd - no -x, no checkout - so it removes untracked cruft but cannot revert a tracked edit. The slot comes back dirty; treehouse get skips a dirty slot forever and prune refuses to reclaim it. Every warm retires one slot permanently.

It is worse than a leak, because the state it leaves is misread: bin/fm-pool-status.sh reports the slot DIRTY, and bootstrap-diagnostics tells firstmate to read a dirty slot as a dead crew's uncommitted work and points at treehouse destroy --include-unlanded - a discard command aimed at a slot that never held anyone's work.

On this box it had already taken half of optiroq's pool: slots 1 and 4, both M src/admin-app/package-lock.json.

Those two are still dirty after this PR, deliberately. This change prevents further bricking; it does not repair damage already done. Reclaiming a dirty slot means discarding whatever is in it, and no automated fix should decide that a modified tracked file is worthless - it looks identical to a dead crew's uncommitted work. Reclaiming slots 1 and 4 is a separate, explicitly user-authorised action. Do not read a green treehouse status after this merges as "the pool is whole".

Is Linux affected? Yes.

Confirmed, and worth stating plainly because it changes who is exposed. The clone is the macOS-specific half: on Linux can_clone fails cleanly (GNU cp has no -c) and the provision degrades to a plain install. But the install still runs inside the leased slot, so it still rewrites the tracked lockfile and the slot still comes back dirty. Nothing on the dirtying path is macOS-specific. A WSL user gets bricked slots the same way, minus the disk saving. tests/fm-worktree-provision.test.sh case (s) pins that down: it forces the no-clone path and asserts the slot still comes back clean.

The fix

Before the installer runs, the provision records which tracked paths were already modified; on the way out - including the SIGINT/SIGTERM path - it restores from HEAD exactly the ones the installer added, and nothing else. Never a blanket git checkout -- ., which would discard a crew's own edits.

One thing that only showed up when it was run for real, and that a reasoned argument would have got wrong: the first version detected the modification with git diff --name-only HEAD, and that predicate cannot see the case that actually bricks slots. optiroq's .gitattributes sets text=auto eol=lf, npm rewrote package-lock.json with CRLF, and:

$ git -C <slot> diff --name-only HEAD --
warning: in the working copy of 'src/admin-app/package-lock.json', CRLF will be replaced by LF the next time Git touches it
                                        # <- no path: diff normalizes, then calls it unchanged
$ git -C <slot> status --porcelain
 M src/admin-app/package-lock.json       # <- what treehouse agrees with

So the restore found nothing to restore and left the slot bricked anyway. It now asks git status --porcelain, the same predicate that decides the slot is dirty. git checkout HEAD -- <path> still repairs the file regardless of what diff thinks.

The evidence

Real npm on this box, against real optiroq, driving the warm's own three steps (treehouse get --lease -> provision -> treehouse return):

=== treehouse status BEFORE
1     dirty        ~/.treehouse/optiroq-84584f/1/optiroq
2     in-use       ~/.treehouse/optiroq-84584f/2/optiroq
3     available    ~/.treehouse/optiroq-84584f/3/optiroq
4     dirty        ~/.treehouse/optiroq-84584f/4/optiroq
5     available    ~/.treehouse/optiroq-84584f/5/optiroq
=== leased /Users/inovak/.treehouse/optiroq-84584f/3/optiroq
--- git status in it before: []
provision: /Users/inovak/.treehouse/optiroq-84584f/3/optiroq (pool optiroq-84584f)
  .: clone=already, reconciled in 2s, cache seeded
  src/admin-app: clone=already, reconciled in 4s
  src/portal-ui: install FAILED after clone=already (slot stays cold here)
provision: finished with at least one cold root
  restored 1 tracked file(s) the installer rewrote - a warmed slot must come back clean, not dirty
=== provision exit=1 elapsed=37s
--- git status in it after provision: []
🌳 Worktree returned to pool.
=== treehouse status AFTER
1     dirty        ~/.treehouse/optiroq-84584f/1/optiroq
2     in-use       ~/.treehouse/optiroq-84584f/2/optiroq
3     available    ~/.treehouse/optiroq-84584f/3/optiroq
4     dirty        ~/.treehouse/optiroq-84584f/4/optiroq
5     available    ~/.treehouse/optiroq-84584f/5/optiroq

The slot npm rewrote a tracked file in comes back available, not dirty, and the two pre-existing dirty slots are untouched. It went straight back into service: a crew picked slot 3 up minutes later.

The run against the pre-fix code, on the same pool, is the counterfactual: it left the slot with M src/admin-app/package-lock.json, and treehouse return then refused it (Worktree has uncommitted changes. Clean and return? [Y/n] -> Aborted), stranding it leased. That slot was restored by hand and returned; the pool is whole apart from the two pre-existing dirty slots.

src/portal-ui: install FAILED is a pre-existing dependency conflict in optiroq itself, not something this branch causes: @mantine/[email protected] needs @mantine/[email protected], the project pins ^9.4.1. It is shown here rather than hidden because it exercises finding 3 below - the root is reported cold and the provision exits 1.


2. The pool guard was dead code

pool_key_of derived the pool key from cd "$wt/../..", which succeeds for very nearly any path, so the guard never fired: the script would run npm install in any directory handed to it, including projects/<repo> - the read-only clone AGENTS.md rail 1 forbids writing to.

It now verifies containment under $TREEHOUSE_ROOT and the <pool>/<numeric-slot>/<repo> shape treehouse lays a slot out in, and refuses with a reason otherwise. A guard that cannot fail is not a guard.

Case (i) asserted only rc == 0 and passed vacuously. It now asserts the installer did not run and no cache was written, and covers a misshapen in-root path (.../pool-x/notanumber/repo) as well as an outside-the-root one.

3. A failed provision was reported as a warm slot

The provision's exit status was discarded, so an install that failed was logged WARMED and clear_blocked told the disk-budget rail all was well. Before #44 an install failure surfaced through the treehouse get rc as FAILED <name>; that path had no equivalent.

The status is now captured and propagated: FAILED <name>: <path> is free but COLD (provision exited N); the next crew installs for itself. The lease is still released either way - a cold slot belongs back in the pool. The evidence run above shows it firing on optiroq's real portal-ui failure.

4. Fail-closed overshot into permanent exemption

The reaper sparing a directory on an unknown answer is right, but one unreadable subdir - or a file another process removed mid-walk - exempted that session dir forever and printed an unexplained alarm, so scratch would accumulate without bound.

"The probe cannot run at all" and "traversal hit one bad entry while otherwise answering" are now different states. The first is fatal to the whole sweep: nothing is reaped and the run says so. The second spares that one directory, names it, and is bounded by a hard ceiling (--max-age-hours x 7, FM_SCRATCH_HARD_CEILING_MULTIPLE) measured on the directory itself with -maxdepth 0, so no unreadable child can defeat it. Unknown is transient and diagnosable, not permanent.

5. can_clone could not detect a silent non-clone

cp -c on macOS falls back to copyfile(2) and exits 0 when cloning is impossible, so the probe's success proved nothing. On HFS+, exFAT, an external drive or a network mount the log would claim clone=yes and 96 MB while the box actually paid ~2.7 GB. It also probed only the destination, so a cross-volume pair was misjudged.

Detection is now: same device for src and dst (df -P), then that device's filesystem is apfs (mount), then a real cp -c probe. All three must hold. When they do not it says so with the reason and installs without cloning - it never claims a saving it cannot prove. Linux keeps the property that made it safe there: GNU cp has no -c, so the probe fails cleanly.

Case (o) stubs df/mount to report a non-APFS device while real cp -c still exits 0. Against the pre-fix code it reports clone=yes and caches - the silent non-clone, reproduced.

6. The provision deadline doubled the lease hold

The warm's timeout was taken after treehouse get returned, and the get is itself bounded by the same value - so a slow get plus a full provision budget held the treehouse lease and the pool lock for up to twice the bound the header promises. The lock is shared with every secondmate home pointing at that pool, so a second warmer waits out the doubled window.

One deadline is now taken before the lease is asked for and passed down as FM_PROVISION_DEADLINE. Case (r) drives a 4 s get against a 20 s bound and asserts the deadline handed to the provision is within 21 s of the start; against the pre-fix code it is 24 s.

7. -quit was dropped on a justification that is factually wrong

The comment claimed BSD find does not accept -quit. It does - on this box, today:

$ /usr/bin/find /tmp -maxdepth 0 -mmin -60 -print -quit >/dev/null; echo $?
0

The early exit is restored behind a capability probe (so a find that genuinely lacks it still works, just without the early exit), and the comment now says what is true. A wrong justification in a comment is worse than no comment, because the next reader will not revisit it.


The three PLAUSIBLE findings: all three implemented

  • 8. clone_tree destroyed a usable cache on a failed refresh. It did rm -rf "$dst" before cloning. It now clones into a temp sibling and swaps, restoring the previous tree if the swap fails. Case (p) stubs a cp that fails only on -R and asserts a canary in the cache survives.
  • 9. The EXIT/INT/TERM trap released the lock without exiting. After a SIGTERM the script kept cloning, unlocked. INT and TERM now clear the EXIT trap, clean up, and exit 130/143.
  • 10. --max-age-hours 0 gave -mmin -0. Where that matches nothing, every session dir including the live caller's reads as dead. It is now rejected with a reason. The check is textual (*[!0]*) rather than arithmetic, because $(( 08 )) is an octal error, not the eight the caller meant.

Also here: .fm/ is excluded from the crew's worktree

Adjacent and small, but it cost a forced teardown today. bin/fm-brief.sh tells every crew to keep a running plan at .fm/progress.md and never commit it - and bin/fm-teardown.sh reads untracked files as unlanded work and refuses to release the worktree. A crew that follows its brief bricks its own teardown. fm-spawn.sh already excludes its harness hook files this way; .fm/ now joins them, for every harness.

exclude_path needed one fix to do that safely: git rev-parse --git-path answers relative to the repo it was asked about, and the spawn's cwd is not that repo, so on a plain clone it would have created a stray .git/info/ wherever the spawn happened to be standing. It now resolves a relative answer against the worktree.


What #44 measured still stands: disk, not speed

Unchanged from the merged PR, restated because it is the reason the feature exists. On optiroq, as df deltas (du cannot see any of this - it counts every block a file references, shared or not):

wall time real disk (df delta)
cold install from scratch 56 s 2.70 GB
clone from cache + reconcile 53 s 96 MB

96.5% less real disk per additional slot, and essentially no time saved. npm install dominates and the npm cache is already shared. Anyone deciding whether this is worth running should know it buys disk, not speed.


Coverage

CI was green on #44's head, which is the plainest statement possible that the suite covered none of this. New coverage is part of the fix, not an extra - and every case below was run against the pre-fix code and confirmed to fail there:

case what it pins
provision (q) an installer that rewrites a tracked file leaves the slot clean, sparing edits that were already there
provision (r) a CRLF-only rewrite under eol normalization - the one git diff cannot see - is still restored
provision (s) the same guarantee on the no-clone path, which is what Linux and WSL run
provision (i) a path outside a pool, and a misshapen path inside one, install nothing and cache nothing
provision (o) a non-APFS volume is refused even though cp -c exits 0 on it
provision (p) a failed cache refresh leaves the previous entry intact
provision (h) a failed install exits 1 and names the cold root
warm (q) a slot whose install failed is logged COLD, never as warmed
warm (r) the whole warm shares one deadline, so a slow get cannot double the lock hold
warm (s) end to end: a warm whose installer rewrites a tracked file returns the slot clean
reap (i) one unreadable subdir spares that dir only, and says which
reap (j) a spared dir past the hard ceiling is still reaped, so sparing cannot be permanent
reap (k) --max-age-hours 0 is refused
spawn-exclude a crew that keeps .fm/progress.md as briefed leaves a clean worktree

bin/fm-lint.sh clean.

Test status, honestly

The full suite ran end to end: 69 of 74 suites passing in 541 s.

Four of the five failures are not this branch's doing - they fail identically against a pristine origin/main checkout in this environment, verified by extracting origin/main to a scratch dir and running them there:

suite branch pristine origin/main
fm-fleet-sync 22 ok, 1 not ok 22 ok, 1 not ok
fm-graph 1 ok, 1 not ok 1 ok, 1 not ok
fm-mission 3 ok, 1 not ok 3 ok, 1 not ok
fm-watch-checkpoint 0 ok, 1 not ok 0 ok, 1 not ok

fm-mission is the known macOS BWK awk newline defect at bin/fm-mission.sh:191 - deliberately not fixed here, worth its own task. fm-watch-checkpoint is an artifact of the timeout stand-in this box needs to run bin/fm-test.sh at all; CI has real coreutils timeout.

The fifth, fm-bootstrap, flaked once under the parallel full-suite run on a 59 s timing assertion and passes 3/3 standalone on this branch (8 ok, 0 not ok), as it does on origin/main. Reported rather than quietly re-run.

ignovak added 3 commits July 30, 2026 16:10
The warm runs the project's installer inside the leased slot, npm rewrites
the tracked lockfile it installed from, and treehouse's reset on return is
`git clean -fd` - no checkout - so the slot comes back dirty, is skipped by
every later get, and is refused by prune. Every warm retired one slot. Half
of optiroq's pool had already gone that way.

Not macOS-specific: the clone degrades cleanly on Linux, but the install
still runs in the slot, so WSL gets bricked slots too, minus the saving.

The provision now snapshots which tracked paths were already modified and,
on the way out including SIGINT/SIGTERM, restores from HEAD exactly the ones
the installer added. The predicate is `git status`, not `git diff`: under
optiroq's `text=auto eol=lf` a CRLF rewrite is invisible to diff and visible
to status, which is the case that bricked slots in practice.

Also in the provisioner and the warm:

- The pool guard was dead code. `cd "$wt/../.."` succeeds for nearly any
  path, so the script would install into any directory handed to it. It now
  verifies containment under the treehouse root and the <pool>/<slot>/<repo>
  shape, and refuses with a reason.
- A failed provision was logged WARMED and cleared the disk-budget block.
  The status is propagated and the slot is reported COLD.
- `cp -c` exits 0 when it silently falls back to a full copy, so the probe
  proved nothing. Detection is now same-device, then apfs, then the probe.
- The provision deadline was taken after the get, which is bounded by the
  same value, so the lease and pool lock could be held for twice the stated
  bound. One deadline is taken before the lease and passed down.
- A failed cache refresh no longer destroys the usable previous entry.
- INT/TERM now clean up and exit rather than continuing unlocked.

Coverage for each, all confirmed to fail against the pre-fix code.
Fail-closed was right, but one unreadable subdir - or a file another process
removed mid-walk - exempted a session dir forever and printed an unexplained
alarm, so scratch would grow without bound.

"The probe cannot run at all" and "traversal hit one bad entry" are now
different states. The first is fatal to the sweep and says so. The second
spares one directory, names it in the summary, and is bounded by a hard
ceiling measured on the directory itself with -maxdepth 0, so no unreadable
child can defeat it.

Two smaller ones in the same file:

- `--max-age-hours 0` produced `-mmin -0`, which matches nothing, so every
  session dir including the live caller's read as dead. Rejected now, with a
  textual check because $(( 08 )) is an octal error rather than eight.
- The comment justifying the removal of -quit was factually wrong: BSD find
  on this box accepts it. Restored behind a capability probe, with a comment
  that says what is true.
bin/fm-brief.sh tells every crew to keep a running plan at .fm/progress.md
and never commit it, and bin/fm-teardown.sh reads untracked files as unlanded
work and refuses to release the worktree. A crew that follows its brief bricks
its own teardown; one had to be force-released today. fm-spawn.sh already
excludes its harness hook files this way, so .fm/ joins them, for every
harness.

`git rev-parse --git-path` answers relative to the repo it was asked about,
and the spawn's cwd is not that repo, so exclude_path now resolves a relative
answer against the worktree instead of creating a stray .git/info/ wherever
the spawn happens to be standing.
@webjema
webjema merged commit bb344c4 into main Jul 30, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants