Skip to content

fix: verify Treehouse slot ownership before teardown - #3837

Merged
kunchenguid merged 10 commits into
mainfrom
fm/fm-teardown-slot-collision-fix-r2
Sep 6, 2026
Merged

kunchenguid merged 10 commits into
mainfrom
fm/fm-teardown-slot-collision-fix-r2

Conversation

@kunchenguid

@kunchenguid kunchenguid commented Sep 6, 2026 •

Copy link
Copy Markdown
Owner

Intent

The captain's order in substance: workers were killed by a pool-slot collision during cleanup - never let that happen again. Make worktree-slot return verify the slot is genuinely the task's before releasing it.

What Changed

  • Refuse Treehouse slot cleanup when another local task record claims the slot or a supported live endpoint reports a different working copy, including forced secondmate descendant teardown.
  • Serialize Treehouse slot allocation, metadata publication, ownership verification, and return with a shared project-identity lock.
  • Give checkpoint watcher subprocesses a configurable termination grace period before forcing them to exit.

Risk Assessment

✅ Low: The change now scans registered local Firstmate homes and serializes slot allocation, publication, verification, and return using a shared project identity lock, closing the previously identified collision path without introducing a substantiated material defect.

Testing

The successful changed-test baseline was supplemented with focused teardown, secondmate, and watcher lifecycle tests plus an end-to-end cross-home collision fixture; teardown refused even with --force while preserving the live worker, slot contents, and both task records, with no regressions observed.

Evidence: Cross-home Treehouse slot-collision teardown transcript

Source: Cross-home Treehouse slot-collision teardown transcript

$ bin/fm-teardown.sh stale-task --force
exit_code=1
--- stderr ---
●━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
●  WATCHER DOWN - SUPERVISION IS OFF
●  1 task(s) in flight, but no watcher has a fresh beacon (last beat: never, grace 300s).
●  Trust the emitted supervision protocol for this harness; do not use shell & for watcher repair.
●  This is a supervision warning only; the guarded operation WILL still run.
●  repair a missing or failed watcher cycle with the Pi tool fm_watch_arm_pi, or restart Pi with -e ~/.no-mistakes/worktrees/016d88035d58/01M1TJBY608T137DYWBW3YGY44/.pi/extensions/fm-primary-turnend-guard.ts -e ~/.no-mistakes/worktrees/016d88035d58/01M1TJBY608T137DYWBW3YGY44/.pi/extensions/fm-primary-pi-watch.ts if the extensions are not loaded.
●━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
REFUSED: task stale-task's recorded worktree ~/.no-mistakes/worktrees/016d88035d58/01M1TJBY608T137DYWBW3YGY44/.slot-collision-e2e.6IymFN/pool/1/project is also task live-task's recorded worktree.
Returning that pool slot would kill live-task's processes and reset its copy, so nothing was changed - not even with --force.
Reconcile whichever record is wrong (bin/fm-crew-state.sh stale-task; bin/fm-crew-state.sh live-task), then re-run teardown.
--- postconditions ---
live_worker=alive
slot_payload=worker-data-must-survive
stale_record=preserved
cross_home_live_record=preserved
mutating_runtime_calls=none
- Outcome: 🔧 1 issue found → auto-fixed (4) ✅ across 5 runs (3h45m2s)

Pipeline

Updates from git push no-mistakes

✅ **intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

🔧 **Review** - 1 issue found → auto-fixed ✅
  • 🚨 bin/fm-teardown.sh:2065 - Cross-home ownership verification enumerates the recorded project’s Git worktrees, not Firstmate homes. Real secondmate homes contain separately cloned projects (fm-home-seed.sh uses git clone), so their state/*.meta files are absent from this listing; the project-common-directory locks are likewise different across clones. Concrete sequence: a stale primary record names slot S, Treehouse reuses S for a task in a secondmate clone, and that live task publishes in the secondmate home. Teardown of the stale record scans only the primary state/project worktrees, a dead endpoint supplies no contradictory cwd, and S is still returned, killing the live worker. The new cross-home test does not reproduce the production layout because it places the second home directly in the project’s worktree list. Enumerate actual registered/local Firstmate homes and serialize allocation/publication/return at an identity shared across their project clones.

🔧 Fix: Protect slots across cloned Firstmate homes
✅ Re-checked - no issues remain.

🔧 **Test** - 1 issue found → auto-fixed (4) ✅
  • 🚨 tests failed with exit code 1
  • bin/fm-test-run.sh --changed --exclude-family real-herdr-gated

🔧 Fix: Gate teardown locking on genuine Treehouse slots
1 error still open:

  • 🚨 tests failed with exit code 1
  • bin/fm-test-run.sh --changed --exclude-family real-herdr-gated

🔧 Fix: Clarify pooled descendant slot gating
1 error still open:

  • 🚨 tests failed with exit code 1
  • bin/fm-test-run.sh --changed --exclude-family real-herdr-gated

🔧 Fix: Synchronize watcher re-arm test on process exit
1 error still open:

  • 🚨 tests failed with exit code 1
  • bin/fm-test-run.sh --changed --exclude-family real-herdr-gated

🔧 Fix: Wait for watcher cleanup before timeout escalation
✅ Re-checked - no issues remain.

  • bin/fm-test-run.sh --changed --exclude-family real-herdr-gated
  • Baseline: bin/fm-test-run.sh --changed --exclude-family real-herdr-gated
  • tests/fm-teardown-endpoint-safety.test.sh
  • tests/fm-secondmate-safety.test.sh
  • tests/fm-watch-checkpoint.test.sh
  • tests/fm-watch-arm.test.sh
  • Manual cross-home pool-slot collision: ran bin/fm-teardown.sh stale-task --force against a stale primary record and a live secondmate-clone record sharing one Treehouse slot; verified refusal, live worker survival, payload and metadata preservation, and zero mutating runtime calls
✅ **Document** - passed

✅ No issues found.

✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.

Workers were killed when cleanup returned a Treehouse pool slot that a
different, live task had already taken. Teardown now proves the slot is
genuinely this task's before releasing it: it refuses when another task
record claims the same live worktree path, or when the endpoint's working
directory contradicts the recorded slot, and that refusal holds under
--force. Slot allocation, metadata publication, ownership verification,
and slot return are serialized across linked firstmate homes, and forced
secondmate cleanup verifies descendant slot ownership before returning
any child worktree.

Regression coverage drives the scripts with two task records naming one
slot path and asserts the live worker survives and its slot is not reset.
@greptile-apps

greptile-apps Bot commented Sep 6, 2026 •

Copy link
Copy Markdown

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Reviews (4): Last reviewed commit: "no-mistakes(ci): Fixed teardown so an ex..." | Re-trigger Greptile

Comment thread bin/fm-wake-lib.sh Resolved
…Git origins to the same Treehouse project-lock identity as absolute clone origins; resolved ShellCheck SC1091 with explicit conditional sourcing; and taught concurrent Herdr teardown coverage to retry expected Treehouse lock contention. Added behavioral regression coverage for bare/absolute origin lock identity. Verified endpoint-safety tests, watcher tests, full CI lint, and the previously failing Herdr teardown assertion
Comment thread bin/fm-wake-lib.sh Resolved
…nge cwd without falsely vetoing cleanup. Removed cwd-based ownership refusal while preserving cross-home record exclusivity and project locking. Updated behavioral coverage for both foreign slot ownership refusal and moved-cwd teardown success. Endpoint-safety, backend, watcher, checkpoint, and targeted lint checks pass. Real Herdr presentation E2E progressed successfully but exceeded the 600s local timeout
@kunchenguid
kunchenguid merged commit b028e8b into main Sep 6, 2026
39 of 42 checks passed
@kunchenguid
kunchenguid deleted the fm/fm-teardown-slot-collision-fix-r2 branch September 6, 2026 21:57
eduardstan added a commit to eduardstan/firstmate that referenced this pull request Sep 7, 2026