Skip to content

fix(watch): preserve queued wake diagnostics - #8

Merged
raywest merged 2 commits into
mainfrom
fm/fm-armfix-backport-b1
Jul 21, 2026
Merged

raywest merged 2 commits into
mainfrom
fm/fm-armfix-backport-b1

Conversation

@raywest

@raywest raywest commented Jul 21, 2026

Copy link
Copy Markdown
Owner

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

  • Snapshot the wake-queue boundary before starting or attaching to watcher cycles, and retain it across replacement watchers so queued wakes are diagnosed correctly.
  • Deduplicate status files re-listed during the grace-period rescan when counting and appending signal wakes, producing one durable queue entry per file.
  • Update the watcher continuity contract and add regression coverage for both queue-boundary timing and repeated-scan deduplication.

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
Watcher exit: 0
Watcher-visible wake:
signal: /var/folders/md/6d3x0xmj0rd3sxvq647l2jnh0000gn/T/no-mistakes-evidence/01KY2GSXAVSR15SC63PT3WR1JG/watch-triage-e2e.GnvUhW/durable-queue/state/task-one.status /var/folders/md/6d3x0xmj0rd3sxvq647l2jnh0000gn/T/no-mistakes-evidence/01KY2GSXAVSR15SC63PT3WR1JG/watch-triage-e2e.GnvUhW/durable-queue/state/task-two.status
Persisted wake queue (seq, kind, file, reason):
1	signal	task-one.status	signal: /var/folders/md/6d3x0xmj0rd3sxvq647l2jnh0000gn/T/no-mistakes-evidence/01KY2GSXAVSR15SC63PT3WR1JG/watch-triage-e2e.GnvUhW/durable-queue/state/task-one.status /var/folders/md/6d3x0xmj0rd3sxvq647l2jnh0000gn/T/no-mistakes-evidence/01KY2GSXAVSR15SC63PT3WR1JG/watch-triage-e2e.GnvUhW/durable-queue/state/task-two.status
2	signal	task-two.status	signal: /var/folders/md/6d3x0xmj0rd3sxvq647l2jnh0000gn/T/no-mistakes-evidence/01KY2GSXAVSR15SC63PT3WR1JG/watch-triage-e2e.GnvUhW/durable-queue/state/task-one.status /var/folders/md/6d3x0xmj0rd3sxvq647l2jnh0000gn/T/no-mistakes-evidence/01KY2GSXAVSR15SC63PT3WR1JG/watch-triage-e2e.GnvUhW/durable-queue/state/task-two.status
Per-file durable record count:
task-one.status	1
task-two.status	1
Evidence: Pre-fork child-append race transcript
Arm exit: 4
Operator-visible outcome:
watcher: cycle ended - 1 wake(s) already queued (no visible reason from this process) - see state/.wake-queue
Persisted wake queue (seq, kind, file, reason):
1	signal	boundary-wake	signal: boundary wake
False FAILED outcome: absent
Evidence: Successor-adoption boundary transcript
Arm exit: 4
Operator-visible cycle history:
watcher: attached pid=13002 (beacon 0s)
watcher: attached pid=13521 (beacon 2s)
watcher: cycle ended - 1 wake(s) already queued (no visible reason from this process) - see state/.wake-queue
Persisted wake queue (seq, kind, file, reason):
1	signal	replacement-wake	signal: replacement wake
False FAILED outcome: absent

Pipeline

Updates from git push no-mistakes

✅ **intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

⚠️ **Review** - 1 info
  • ℹ️ docs/watcher-continuity.md:38 - cycle_begin now receives a pre-captured boundary, but the continuity contract still says that cycle_begin itself 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.sh
  • bash tests/fm-watch-triage.test.sh
  • Manual end-to-end bin/fm-watch.sh run with two actionable status files persisting across the grace rescan; inspected raw durable queue records.
  • Manual end-to-end bin/fm-watch-arm.sh run 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.

Ray West 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.
@raywest
raywest merged commit 8da6e0d into main Jul 21, 2026
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.

1 participant