Skip to content

fix(bin): refuse watchers from disposable checkouts and exit when the home is gone - #5552

Merged
kunchenguid merged 5 commits into
kunchenguid:mainfrom
karotkriss:fm/fm-up-321-4760-disposable-watchers
Sep 25, 2026
Merged

kunchenguid merged 5 commits into
kunchenguid:mainfrom
karotkriss:fm/fm-up-321-4760-disposable-watchers

Conversation

@karotkriss

@karotkriss karotkriss commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Intent

Fixes #321
Fixes #4760

A watcher can be armed from a disposable no-mistakes validation checkout under .no-mistakes/worktrees/, and a running watcher never exits when its home or state directory disappears, so validation runs leave orphan watchers that keep writing state into the live home.
Refuse to arm a real watcher from a disposable validation checkout, make a running watcher exit when its home or state directory is gone (scoped to itself, never a global kill), and have the test helpers reap watchers they armed for a temporary home.
This closes upstream issues #321 and #4760.

What Changed

  • bin/fm-watch-arm.sh now refuses every mode with a typed watcher: FAILED - refusing to arm from a disposable validation checkout and exits 1 before touching state when its own script path lives under .no-mistakes/worktrees/, with the existing FM_GATE_REFUSE_BYPASS=1 escape hatch lifting the refusal for sandboxed test homes.
  • bin/fm-watch.sh adds a once-per-poll home-gone check that exits 1 with a logged reason when its home (if it existed at arm time), state directory, singleton lock, or code root disappears, scoped to the current process so no other watcher is signalled.
  • Updated docs/watcher-continuity.md and the test helpers/suites (tests/lib.sh, tests/wake-helpers.sh, tests/fm-watch-arm.test.sh) to document the behavior and add a test reaper plus coverage for the arm refusal and the home-gone/state-gone exits.

Risk Assessment

✅ Low: The change is additive, self-scoped, and well-covered by behavior-asserting tests; the refusal, self-exit, and reaper each map directly to the stated intent with no reachable false-positive path found.

Testing

Ran the targeted real-process suite tests/fm-watch-arm.test.sh (all 21 tests pass, exit 0), which arms real watchers for temporary homes and observes end-to-end behavior. The four new tests drove: the disposable-checkout arm refusal, the state-directory-gone watcher exit within one poll, the home-gone watcher exit with lock release, and the tests/lib.sh reaper stopping a tracked watcher. I additionally reproduced the refusal manually - this validation worktree itself lives under .no-mistakes/worktrees/, so running the arm here with the bypass cleared is an authentic end-user reproduction and it printed the typed failure line and exited 1 before taking any lock. No screenshot applies; this is a CLI/daemon behavior change and the CLI transcript is the reviewer-visible surface. Worktree left clean; test temp dirs live outside it.

  • Live validation: ✅ go - 5 of 5 scenarios driven live against the product
Scenario Result Live Evidence
Arming a watcher from a disposable .no-mistakes/worktrees/ checkout is refused with the typed failure line and takes no lock/beacon ✅ pass live test_arm_refuses_a_disposable_validation_checkout in tests/fm-watch-arm.test.sh plus manual FM_GATE_REFUSE_BYPASS= bash bin/fm-watch-arm.sh --stop from this real disposable worktree: 'watcher: FAI…
A running watcher exits within one poll when its state directory is deleted, logging the state-gone reason and not a false wake ✅ pass live test_watcher_exits_when_its_state_directory_is_removed - real watcher armed, rm -rf $state, pid gone within 30x0.1s polls, 'watcher: exiting - state directory' logged
A running watcher exits within one poll when its home is deleted and releases its singleton lock (self-scoped, no global kill) ✅ pass live test_watcher_exits_when_its_home_is_removed - real watcher armed, rm -rf $home, pid gone, 'watcher: exiting - home no longer exists' logged, lock no longer names the exited pid
The tests/lib.sh exit-time reaper stops a watcher armed for a tracked temporary home via the home-scoped stop and consumes its registry ✅ pass live test_reaper_stops_a_tracked_watcher - seed watcher armed, fm_test_reap_watchers driven, watcher stopped and registry consumed
With the FM_GATE_REFUSE_BYPASS escape hatch set, the suite's own sandboxed arms still run (refusal does not break legitimate test homes) ✅ pass live Full tests/fm-watch-arm.test.sh (21 real-arm tests) runs to exit 0 under the exported bypass from tests/lib.sh
Evidence: New-scenario results and manual refusal reproduction

Source: New-scenario results and manual refusal reproduction

ok - watch-arm: a disposable validation checkout refuses to arm
ok - watch-arm: a watcher exits within one poll when its state directory is removed
ok - watch-arm: a watcher exits within one poll when its home is removed
ok - watch-arm: the test reaper stops a watcher armed for a tracked temporary home
=== Manual refusal reproduction from this real disposable checkout ===
$ FM_GATE_REFUSE_BYPASS= bash bin/fm-watch-arm.sh --stop
watcher: FAILED - refusing to arm from a disposable validation checkout: .../01M3A619X4RF67MKVPVHQRH90F/bin
EXIT=1

Pipeline

Updates from git push no-mistakes

✅ **intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

⚠️ **Review** - 1 info
  • ℹ️ bin/fm-watch.sh:2562 - bin/fm-watch.sh:2562 adds a fourth per-poll self-exit trigger (SCRIPT_DIR/code-root gone) beyond the intent's stated remedy of exiting when 'home or state directory is gone'. It is defensible because the intent's problem statement names 'a discarded disposable checkout' (i.e. the code root) as an orphan source, and the check is cheap and self-scoped, so no action is required. The sibling lock-removed check (2559) is in-scope: it is needed so a detached helper's mkdir -p "$STATE" recreating the state dir cannot defeat state-gone detection.
✅ **Test** - passed

✅ No issues found.

  • Live validation: ✅ go - 5 of 5 scenarios driven live against the product
Scenario Result Live Evidence
Arming a watcher from a disposable .no-mistakes/worktrees/ checkout is refused with the typed failure line and takes no lock/beacon ✅ pass live test_arm_refuses_a_disposable_validation_checkout in tests/fm-watch-arm.test.sh plus manual FM_GATE_REFUSE_BYPASS= bash bin/fm-watch-arm.sh --stop from this real disposable worktree: 'watcher: FAI…
A running watcher exits within one poll when its state directory is deleted, logging the state-gone reason and not a false wake ✅ pass live test_watcher_exits_when_its_state_directory_is_removed - real watcher armed, rm -rf $state, pid gone within 30x0.1s polls, 'watcher: exiting - state directory' logged
A running watcher exits within one poll when its home is deleted and releases its singleton lock (self-scoped, no global kill) ✅ pass live test_watcher_exits_when_its_home_is_removed - real watcher armed, rm -rf $home, pid gone, 'watcher: exiting - home no longer exists' logged, lock no longer names the exited pid
The tests/lib.sh exit-time reaper stops a watcher armed for a tracked temporary home via the home-scoped stop and consumes its registry ✅ pass live test_reaper_stops_a_tracked_watcher - seed watcher armed, fm_test_reap_watchers driven, watcher stopped and registry consumed
With the FM_GATE_REFUSE_BYPASS escape hatch set, the suite's own sandboxed arms still run (refusal does not break legitimate test homes) ✅ pass live Full tests/fm-watch-arm.test.sh (21 real-arm tests) runs to exit 0 under the exported bypass from tests/lib.sh
  • bash tests/fm-watch-arm.test.sh (21 real-process tests, exit 0, including the four new behavior tests)
  • FM_GATE_REFUSE_BYPASS= bash bin/fm-watch-arm.sh --stop run from the real disposable worktree checkout - refused with exit 1
✅ **Document** - passed

✅ No issues found.

🔧 **Lint** - 1 issue found → auto-fixed ✅
  • ⚠️ linter found issues (exit code 1)

🔧 Fix applied.
✅ Re-checked - no issues remain.

✅ **Push** - passed

✅ No issues found.

… home is gone

Fixes kunchenguid#321
Fixes kunchenguid#4760

A watcher armed from a disposable no-mistakes validation checkout under
.no-mistakes/worktrees/ outlived the validation step and kept writing the
real home's state, and a running watcher never noticed when its home,
state directory, or code root disappeared. The arm now refuses from such
a checkout with the typed failure line, the watcher checks once per poll
that its home, state directory (or its own lock holder record), and bin
directory still exist and exits with a logged reason scoped to itself,
and the shared test helpers reap every watcher a suite armed for a
temporary home through the home-scoped stop.
@karotkriss karotkriss changed the title fix(bin): stop watchers armed from or orphaned by disposable validation checkouts fix(bin): refuse watchers from disposable checkouts and exit when the home is gone Sep 24, 2026
@kunchenguid

Copy link
Copy Markdown
Owner

Speaking as Kun's firstmate:

Verdict: Diff reviewed (disposable .no-mistakes/worktrees/ arm refusal; home/state/code-root gone self-exit; test reaper). Closes ready-for-pr #321 and #4760 (Fixes keywords verified). contract-class: restore — existing watcher arm/poll path was leaking orphans from disposable checkouts; no new default-on surface.

VISION: One captain/interface — aligns. Authority — aligns (FM_GATE_REFUSE_BYPASS test hatch only). Scripts/agents — aligns. Restart — aligns (orphan exit). Delegation/spine — aligns. Vendor — aligns. Scope — aligns.

Attestation: MATCH 3373b953b512cd0507e97ff42e630f69598f4658. NM: SUCCESS. CI: red — Lint 2 exit 143 (run 36033225117 job 107746876388); Behavior portable serial 2 cancelled. workflow-zero: yes. No workflow approval needed this pass. Firstmate flag: no. Waiting on author/CI to clear the red checks (not captain).

karotkriss and others added 2 commits September 24, 2026 15:06
…ced by this branch's test-watcher reaper, which is what killed both CI checks (serial-2 cancelled at the 30-min cap; Lint 2 exit 143 = the suite's own TERM-trap code). Root cause: test_drain_asserts_watcher_liveness (tests/fm-wake-queue.test.sh) fabricates a .watch.lock whose pid is the test runner's own $$ with the runner's real identity, to make the drain believe a live watcher exists. The new make_case tracking registers that state dir for reaping, so at fm_test_cleanup the new fm_test_reap_watchers drives fm-watch-arm.sh --stop; its identity check matches (the fixture recorded the runner's identity) and it kill -TERMs the test runner. tests/lib.sh:231 is `trap 'fm_test_cleanup; exit 143' TERM`, so the TERM re-enters cleanup -> reap -> kills $$ again -> infinite loop until the runner cap. I reproduced this locally: the suite ran all tests then looped forever in cleanup spawning fm-watch-arm.sh --stop against a lock naming its own PID. Fix (tests/lib.sh, +5 lines): in fm_test_reap_watchers, skip any tracked lock whose pid equals our own $$ before driving --stop. This is the single shared reap boundary; seven $$-self-lock fixtures across four test files are all covered by the one guard, and real armed watchers (pid != $$) are still reaped. Invariant: the test reaper must only signal real armed watcher processes, never the test runner itself. Verified locally: tests/fm-wake-queue.test.sh -> EXIT 0 (63 ok, no hang); tests/fm-watch-arm.test.sh -> EXIT 0 (21 ok, including test_reaper_stops_a_tracked_watcher, confirming the guard does not over-skip). Lint 2's exit 143 was the same shard/cap signature; a fresh CI run on this new commit will re-evaluate it
… + stall eviction

Header comment conflict only after kunchenguid#5594; retain both the stalled-holder
eviction docs from main and this PR's per-poll home/state/code-root exit.
@kunchenguid

Copy link
Copy Markdown
Owner

Speaking as Kun's firstmate:

Verdict: Diff re-reviewed (disposable .no-mistakes/worktrees/ arm refusal; per-poll home/state/lock/code-root gone self-exit; test reaper). Closes ready-for-pr #321 and #4760 (Fixes keywords + author intent verified). contract-class: restore — existing watcher arm/poll path leaked orphans from disposable checkouts; no new default-on surface.

VISION: aligns — One captain one interface: yes (no silent orphan watchers writing live homes). Authority explicit: yes (FM_GATE_REFUSE_BYPASS stays test-only escape). Scripts vs agents: yes. Restart non-event: yes (home-gone self-exit). Delegation with spine: yes (typed refusal). Fleet outlives vendor: n/a. Scope: yes. Closing: strengthens refuse path; resists leaking validation watchers into live state.

Conflict: DIRTY after #5594 (stall-eviction) landed on same header in bin/fm-watch.sh. Resolved on existing branch (maintainer-can-modify): kept both header docs + both bodies (stall eviction from main + home-gone from this PR). New HEAD ed472cad5dea…. Waiting on CI re-green; will squash-merge when CLEAN+green.

…e-watcher docs

docs/watcher-continuity.md conflict after main restructured the regression
section; retain main's headings and add disposable-checkout / home-gone /
reaper coverage bullets from this PR.
@kunchenguid

Copy link
Copy Markdown
Owner

Speaking as Kun's firstmate:

Second conflict resolve: After #5554 landed, docs/watcher-continuity.md conflicted with main's restructured regression section. Resolved on existing branch: kept main headings + disposable-checkout / home-gone / reaper bullets. Attestation rebound to 9fea65f8…. Waiting CI re-green then squash-merge.

@kunchenguid

Copy link
Copy Markdown
Owner

Speaking as Kun's firstmate:

Post-resolve: Second conflict (docs after #5554) resolved; Behavior portable serial 3 flake (fm-secondmate-reconcile oversized stream bound — unrelated to disposable-watcher diff) re-ran green. Squash-merging as restore.

@kunchenguid
kunchenguid merged commit 1a814e4 into kunchenguid:main Sep 25, 2026
37 of 38 checks passed
@kunchenguid

Copy link
Copy Markdown
Owner

Speaking as Kun's firstmate: this is merged. Thank you @karotkriss — really appreciate you taking the time on this.

mituso89 pushed a commit to mituso89/firstmate that referenced this pull request Sep 26, 2026
… home is gone (kunchenguid#5552)

* fix(bin): refuse watchers from disposable checkouts and exit when the home is gone

Fixes kunchenguid#321
Fixes kunchenguid#4760

A watcher armed from a disposable no-mistakes validation checkout under
.no-mistakes/worktrees/ outlived the validation step and kept writing the
real home's state, and a running watcher never noticed when its home,
state directory, or code root disappeared. The arm now refuses from such
a checkout with the typed failure line, the watcher checks once per poll
that its home, state directory (or its own lock holder record), and bin
directory still exist and exits with a logged reason scoped to itself,
and the shared test helpers reap every watcher a suite armed for a
temporary home through the home-scoped stop.

* no-mistakes(lint): fix SC1007 by assigning empty string in watch-arm test

* no-mistakes(ci): Found and fixed a genuine, reproducible hang introduced by this branch's test-watcher reaper, which is what killed both CI checks (serial-2 cancelled at the 30-min cap; Lint 2 exit 143 = the suite's own TERM-trap code). Root cause: test_drain_asserts_watcher_liveness (tests/fm-wake-queue.test.sh) fabricates a .watch.lock whose pid is the test runner's own $$ with the runner's real identity, to make the drain believe a live watcher exists. The new make_case tracking registers that state dir for reaping, so at fm_test_cleanup the new fm_test_reap_watchers drives fm-watch-arm.sh --stop; its identity check matches (the fixture recorded the runner's identity) and it kill -TERMs the test runner. tests/lib.sh:231 is `trap 'fm_test_cleanup; exit 143' TERM`, so the TERM re-enters cleanup -> reap -> kills $$ again -> infinite loop until the runner cap. I reproduced this locally: the suite ran all tests then looped forever in cleanup spawning fm-watch-arm.sh --stop against a lock naming its own PID. Fix (tests/lib.sh, +5 lines): in fm_test_reap_watchers, skip any tracked lock whose pid equals our own $$ before driving --stop. This is the single shared reap boundary; seven $$-self-lock fixtures across four test files are all covered by the one guard, and real armed watchers (pid != $$) are still reaped. Invariant: the test reaper must only signal real armed watcher processes, never the test runner itself. Verified locally: tests/fm-wake-queue.test.sh -> EXIT 0 (63 ok, no hang); tests/fm-watch-arm.test.sh -> EXIT 0 (21 ok, including test_reaper_stops_a_tracked_watcher, confirming the guard does not over-skip). Lint 2's exit 143 was the same shard/cap signature; a fresh CI run on this new commit will re-evaluate it

---------

Co-authored-by: firstmate-oss <[email protected]>
mehulbhagwani pushed a commit to mehulbhagwani/firstmate that referenced this pull request Sep 26, 2026
… home is gone (kunchenguid#5552)

* fix(bin): refuse watchers from disposable checkouts and exit when the home is gone

Fixes kunchenguid#321
Fixes kunchenguid#4760

A watcher armed from a disposable no-mistakes validation checkout under
.no-mistakes/worktrees/ outlived the validation step and kept writing the
real home's state, and a running watcher never noticed when its home,
state directory, or code root disappeared. The arm now refuses from such
a checkout with the typed failure line, the watcher checks once per poll
that its home, state directory (or its own lock holder record), and bin
directory still exist and exits with a logged reason scoped to itself,
and the shared test helpers reap every watcher a suite armed for a
temporary home through the home-scoped stop.

* no-mistakes(lint): fix SC1007 by assigning empty string in watch-arm test

* no-mistakes(ci): Found and fixed a genuine, reproducible hang introduced by this branch's test-watcher reaper, which is what killed both CI checks (serial-2 cancelled at the 30-min cap; Lint 2 exit 143 = the suite's own TERM-trap code). Root cause: test_drain_asserts_watcher_liveness (tests/fm-wake-queue.test.sh) fabricates a .watch.lock whose pid is the test runner's own $$ with the runner's real identity, to make the drain believe a live watcher exists. The new make_case tracking registers that state dir for reaping, so at fm_test_cleanup the new fm_test_reap_watchers drives fm-watch-arm.sh --stop; its identity check matches (the fixture recorded the runner's identity) and it kill -TERMs the test runner. tests/lib.sh:231 is `trap 'fm_test_cleanup; exit 143' TERM`, so the TERM re-enters cleanup -> reap -> kills $$ again -> infinite loop until the runner cap. I reproduced this locally: the suite ran all tests then looped forever in cleanup spawning fm-watch-arm.sh --stop against a lock naming its own PID. Fix (tests/lib.sh, +5 lines): in fm_test_reap_watchers, skip any tracked lock whose pid equals our own $$ before driving --stop. This is the single shared reap boundary; seven $$-self-lock fixtures across four test files are all covered by the one guard, and real armed watchers (pid != $$) are still reaped. Invariant: the test reaper must only signal real armed watcher processes, never the test runner itself. Verified locally: tests/fm-wake-queue.test.sh -> EXIT 0 (63 ok, no hang); tests/fm-watch-arm.test.sh -> EXIT 0 (21 ok, including test_reaper_stops_a_tracked_watcher, confirming the guard does not over-skip). Lint 2's exit 143 was the same shard/cap signature; a fresh CI run on this new commit will re-evaluate it

---------

Co-authored-by: firstmate-oss <[email protected]>
mehulbhagwani pushed a commit to mehulbhagwani/firstmate that referenced this pull request Sep 26, 2026
… home is gone (kunchenguid#5552)

* fix(bin): refuse watchers from disposable checkouts and exit when the home is gone

Fixes kunchenguid#321
Fixes kunchenguid#4760

A watcher armed from a disposable no-mistakes validation checkout under
.no-mistakes/worktrees/ outlived the validation step and kept writing the
real home's state, and a running watcher never noticed when its home,
state directory, or code root disappeared. The arm now refuses from such
a checkout with the typed failure line, the watcher checks once per poll
that its home, state directory (or its own lock holder record), and bin
directory still exist and exits with a logged reason scoped to itself,
and the shared test helpers reap every watcher a suite armed for a
temporary home through the home-scoped stop.

* no-mistakes(lint): fix SC1007 by assigning empty string in watch-arm test

* no-mistakes(ci): Found and fixed a genuine, reproducible hang introduced by this branch's test-watcher reaper, which is what killed both CI checks (serial-2 cancelled at the 30-min cap; Lint 2 exit 143 = the suite's own TERM-trap code). Root cause: test_drain_asserts_watcher_liveness (tests/fm-wake-queue.test.sh) fabricates a .watch.lock whose pid is the test runner's own $$ with the runner's real identity, to make the drain believe a live watcher exists. The new make_case tracking registers that state dir for reaping, so at fm_test_cleanup the new fm_test_reap_watchers drives fm-watch-arm.sh --stop; its identity check matches (the fixture recorded the runner's identity) and it kill -TERMs the test runner. tests/lib.sh:231 is `trap 'fm_test_cleanup; exit 143' TERM`, so the TERM re-enters cleanup -> reap -> kills $$ again -> infinite loop until the runner cap. I reproduced this locally: the suite ran all tests then looped forever in cleanup spawning fm-watch-arm.sh --stop against a lock naming its own PID. Fix (tests/lib.sh, +5 lines): in fm_test_reap_watchers, skip any tracked lock whose pid equals our own $$ before driving --stop. This is the single shared reap boundary; seven $$-self-lock fixtures across four test files are all covered by the one guard, and real armed watchers (pid != $$) are still reaped. Invariant: the test reaper must only signal real armed watcher processes, never the test runner itself. Verified locally: tests/fm-wake-queue.test.sh -> EXIT 0 (63 ok, no hang); tests/fm-watch-arm.test.sh -> EXIT 0 (21 ok, including test_reaper_stops_a_tracked_watcher, confirming the guard does not over-skip). Lint 2's exit 143 was the same shard/cap signature; a fresh CI run on this new commit will re-evaluate it

---------

Co-authored-by: firstmate-oss <[email protected]>
ashkonmousavi added a commit to ashkonmousavi/firstmate that referenced this pull request Sep 26, 2026