Repository navigation
fix(watch): preserve queued wake diagnostics - #8
Merged
Merged
Conversation
added 2 commits
July 21, 2026 10:20
…d count Port two review-confirmed fixes for the watcher-cycle queued-wake reporting landed by fork PR #7: - Cycle-begin snapshot-before-fork race: the wake-queue sequence baseline is now captured before starting or attaching to a watcher cycle, not after. Capturing it after the fork let a child append and die in the gap, making the new-wake count zero and re-enabling the false FAILED outcome PR #7 was meant to eliminate. The same race in successor adoption is fixed by carrying the original boundary forward across replacement watchers instead of resampling mid-loop. Ported from raywest/firstmate fm/fm-watch-armfix-upstream-r1 (upstream kunchenguid/firstmate PR kunchenguid#807), narrowed to just this fix - the rest of that branch was rebuilt on upstream/main and carries unrelated packaging differences. - Unique-file partial-append count: bin/fm-watch.sh's signal loop scans twice (before and after the grace sleep), so an unchanged pending file is listed twice. The append loop and the pending_total diagnostic both iterated raw pending lines instead of unique files, so a single file was durably appended to the wake queue twice. This fix originates here from the upstream PR kunchenguid#807 reviewer's finding, not as a port - no commit on that branch (or its predecessor) actually applies it, so it does not exist upstream to port. Regression tests adapted/added for both paths.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Intent
Port two review-confirmed fixes for the watcher-cycle queued-wake diagnostics, both improving code that already merged via fork PR #7 (fix(watch): report queued wakes at arm cycle end). (1) Cycle-begin snapshot-before-fork race: bin/fm-watch-arm.sh's cycle_begin now takes the wake-queue sequence baseline as a parameter captured immediately before forking or attaching to a watcher cycle, instead of sampling it inside cycle_begin (which ran after the fork/attach, letting a child append+die in the gap and zero out the new-wake count, re-enabling the false FAILED outcome PR #7 was meant to eliminate). The same race in successor adoption inside attach_and_wait is fixed by carrying the original cycle_queue_seq_before forward across replacement watchers instead of resampling wake_queue_seq() mid-loop. This was ported (cherry-picked/re-derived by hand, diffed carefully) from raywest/firstmate branch fm/fm-watch-armfix-upstream-r1 (which is our upstream contribution as kunchenguid/firstmate PR kunchenguid#807), narrowed to ONLY this fix - the rest of that branch was rebuilt on top of upstream/main and carries large unrelated packaging/behavioral differences we deliberately did not port. (2) Unique-file partial-append count: bin/fm-watch.sh's signal-triage loop calls scan_signals twice (once immediately, once after sleeping FM_SIGNAL_GRACE and re-scanning) by design, so an unchanged pending status file is legitimately listed twtwice in $pending. The pending_total tally and the fm_wake_append loop both iterated raw pending lines instead of unique files, so ordinary operation was durably double-appending the same file to the wake queue every time a signal persisted across the grace-period rescan (verified empirically with a standalone repro: unfixed code produced 4 wake-queue records for 2 status files; fixed code produces exactly 2). IMPORTANT PROVENANCE CORRECTION: I initially could not find this second fix anywhere in PR kunchenguid#807 (checked all 9 commits, the full PR body, review comments, and the diff) and escalated that discrepancy to the captain. The captain independently verified my finding and confirmed: this fix does NOT exist upstream at all - it was never actually implemented in any commit on that branch or its predecessor, despite an earlier task description claiming it was already folded in. The captain explicitly decided (option C of an ask-user decision) that I should implement it here as an originated-here fix, and that the PR body must state fix 2 originated here from the upstream PR kunchenguid#807 reviewer's own finding text, not as a port of an existing upstream commit. Both fixes: minimal diffs, existing repo/test conventions followed (POSIX-style space-separated dedup, matching the existing 'files' dedup pattern already in the file), regression tests adapted from the upstream branch (fix 1) or newly written and verified to fail without the fix / pass with it (fix 2, verified via a temporary git-stash of just bin/fm-watch.sh). shellcheck (bin/fm-lint.sh) clean. Full relevant test suites green: fm-watcher-lock.test.sh (32/32), fm-watch-triage.test.sh (36/36), fm-wake-queue.test.sh (12/12), fm-daemon.test.sh (95/95).
What Changed
Risk Assessment
✅ Low: Captain, this is a tightly scoped queue-boundary and deduplication fix with targeted coverage; only the stale contract wording remains.
Testing
The supplied full-suite baseline had succeeded; I completed both directly affected real-process suites and three CLI lifecycle checks. The evidence shows exactly one durable record per pending status file and queued-wake outcomes—not false FAILED outcomes—for both fork and successor-adoption races. This background CLI behavior has no rendered UI, so operator-visible transcripts and persisted queue records are the appropriate reviewer evidence.
Evidence: Grace-rescan signal triage transcript
Evidence: Pre-fork child-append race transcript
Evidence: Successor-adoption boundary transcript
Pipeline
Updates from git push no-mistakes
✅ **intent** - passed
✅ No issues found.
✅ **Rebase** - passed
✅ No issues found.
docs/watcher-continuity.md:38-cycle_beginnow receives a pre-captured boundary, but the continuity contract still says thatcycle_beginitself snapshots the queue under lock. Update the wording so a future refactor does not move the read back after fork/attach and reintroduce the race.✅ **Test** - passed
✅ No issues found.
command -v tmux >/dev/null || { echo "tmux is required for e2e tests" >&2; exit 1; }; tmux -V; rc=0; for t in tests/*.test.sh; do echo "== $t =="; bash "$t" || rc=1; done; exit "$rc"Baseline previously completed successfully:command -v tmux >/dev/null || { echo "tmux is required for e2e tests" >&2; exit 1; }; tmux -V; rc=0; for t in tests/*.test.sh; do echo "== $t =="; bash "$t" || rc=1; done; exit "$rc"bash tests/fm-watcher-lock.test.shbash tests/fm-watch-triage.test.shManual end-to-endbin/fm-watch.shrun with two actionable status files persisting across the grace rescan; inspected raw durable queue records.Manual end-to-endbin/fm-watch-arm.shrun where its child appends before cycle setup; inspected operator output and raw durable queue.Manual end-to-end attached-successor replacement run; appended a wake before successor adoption and inspected operator output and raw durable queue.✅ **Document** - passed
✅ No issues found.
✅ **Lint** - passed
✅ No issues found.
✅ **Push** - passed
✅ No issues found.