Skip to content

fix(watch): report queued wakes at arm cycle end - #7

Merged
raywest merged 5 commits into
mainfrom
fm/fm-watch-armfix-f1
Jul 20, 2026
Merged

raywest merged 5 commits into
mainfrom
fm/fm-watch-armfix-f1

Conversation

@raywest

@raywest raywest commented Jul 20, 2026

Copy link
Copy Markdown
Owner

Intent

Fix watcher-cycle end misreporting in the arm layer per the diagnosis in data/scout-watcher-chain-w1/report.md (root-caused to upstream PR kunchenguid#693 / issue kunchenguid#693). Three deliberate fixes: (1) bin/fm-watch-arm.sh's owned_child_finished and attach_and_wait/fail_unexplained_cycle no longer trust empty output as proof nothing happened - a shared helper snapshots state/.wake-queue.seq at cycle_begin and, if the queue advanced during the cycle, reports an accurate 'N wake(s) already queued' line and a distinct nonzero exit code (4) instead of the misleading 'watcher: FAILED - ... without an actionable reason' text; the FAILED text and its exit-code-1 semantics are preserved for the genuinely-empty case, so downstream consumers keying on the literal FAILED substring (docs/supervision-protocols/claude.md, grok.md) are unaffected. (2) bin/fm-watch.sh hardening: handle_push_transition's fm_backend_commit_transition call after a successful fm_wake_append is now best-effort (triage_log + fall through to wake()) instead of 'exit 1', so an already-durable wake can never be silently discarded by an unrelated dedupe-marker failure; the multi-file signal-actionable loop now tracks how many of N pending files it appended before a failure and prints a short 'signal: partial-append-failure (appended: X/N)' diagnostic instead of dying silently mid-batch. (3) bin/backends/herdr.sh's events-capability probe switched from a piped 'printf | grep -q' (which SIGPIPEs when grep exits early on its first match, mid-write of a ~220KB blob) to a herestring, eliminating pre-existing (since PR kunchenguid#472), purely cosmetic stderr noise - functionally inert, bundled because the same investigation covered it. Deliberately scoped to exactly these three files plus their docs (docs/watcher-continuity.md's arm-layer contract wording updated to describe the new third outcome; docs/architecture.md's one-line cross-reference updated to match) - did NOT touch the Pi/OpenCode continuity extensions (.pi/extensions/fm-primary-pi-watch.ts, .opencode/plugins/fm-primary-watch-arm.js) since their own text-pattern classification is out of scope per the task brief; a caller there still falls back to their own generic 'exited N' failure text for this new line, which is fine since the underlying wake is durable and surfaces via the normal drain path either way. New regression tests added: tests/fm-watcher-lock.test.sh gets two new tests (test_attached_arm_reports_queued_wake_without_visible_reason, test_owned_arm_reports_queued_wake_on_signal_exit) covering both the attached and owned arm shapes with a real durably-queued wake and no visible reason text, while the existing test_arm_attaches_and_waits_for_live_fresh_watcher's kill-with-nothing-pending FAILED assertion is left untouched; tests/fm-supervision-events.test.sh gets a unit test proving handle_push_transition's failed-commit path is best-effort and never discards the already-queued wake; tests/fm-watch-triage.test.sh gets a real-subprocess regression test proving the multi-file loop's ordinary all-succeed path (two simultaneous status files) is unaffected by the new bookkeeping. Deliberately did not add a live-subprocess test forcing a genuine partial fm_wake_append failure inside the multi-file loop (2nd-of-N fails after 1st succeeds) - fm_wake_append is a bash function sourced in-process, not an external command, so there is no clean seam to fail it deterministically inside a real unmodified fm-watch.sh subprocess without adding a test-only hook, which would be scope creep beyond the report's ask; that specific line's correctness rests on shellcheck plus code review of a small, low-risk diff (increment a counter, one conditional echo, same exit 1 as before). Full targeted test sweep (fm-watcher-lock, fm-watch-triage, fm-supervision-events, fm-wake-queue, fm-guard-stale-banner, fm-turnend-guard, fm-backend-herdr, fm-backend-herdr-eventwait-smoke against a real herdr binary) all green, shellcheck clean via bin/fm-lint.sh.

What Changed

  • Snapshot the wake-queue sequence at arm-cycle start and report post-boundary durable wakes as an already-queued outcome, while retaining FAILED for genuinely empty cycles.
  • Keep durable watcher wakes actionable when Herdr dedupe commits fail, and emit a diagnostic for partial multi-file enqueue failures; add regression coverage and update continuity documentation.
  • Use here-strings for Herdr event-capability schema checks to prevent SIGPIPE noise on large schema output.

Risk Assessment

✅ Low: The revised direct queue-record check removes the prior two-file publication gap, and the remaining changes are bounded to the intended watcher paths.

Testing

The supplied full baseline had passed; focused watcher, triage, queue, event, and Herdr tests passed, and live CLI evidence confirms durable wakes are reported accurately, same-second prior wakes are excluded, and the large-schema probe is silent.

Evidence: Attached arm reports a durable wake with exit 4
E2E attached-arm durable wake scenario
seed_pid=1729
durable_queue_sequence=1 rows=1
arm_exit=4
arm_output:
watcher: attached pid=1729 (beacon 0s)
watcher: cycle ended - 1 wake(s) already queued (no visible reason from this process) - see state/.wake-queue
Evidence: Owned arm preserves child exit while reporting durable wake
E2E owned-arm durable wake scenario
owned_watcher_pid=7727
durable_queue_sequence=1 rows=1
arm_exit=1
arm_output:
watcher: started pid=7727 (beacon fresh)
watcher: cycle ended abnormally (rc=1) - 1 wake(s) already queued - see state/.wake-queue
Evidence: Same-second pre-cycle wake remains an empty-cycle failure
E2E attached-arm pre-cycle wake in the same epoch second
durable_queue_epoch=1784571020 sequence=1
arm_cycle_started_at=1784571020
arm_exit=1
arm_output:
watcher: attached pid=3866 (beacon 1s)
watcher: FAILED - cycle ended without an actionable reason
Evidence: Large Herdr schema probe has no stderr noise
Herdr large-schema capability probe
schema_bytes=231043
capability_exit=0
stderr_bytes=0
- Outcome: 🔧 1 issue found → auto-fixed ✅ across 2 runs (1h25m3s)

Pipeline

Updates from git push no-mistakes

✅ **intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

🔧 **Review** - 2 issues found → auto-fixed (2) ✅
  • 🚨 bin/fm-watch-arm.sh:496 - Intent requires the already-queued outcome to use “a distinct nonzero exit code (4).” The new owned-child branch prints that outcome at line 496, but then returns the child’s original status (status=$rc; return "$status"), so the supplied TERM case exits 1 (as the new test explicitly asserts), not 4. Confirm whether preserving the child status is intended; otherwise this branch must return 4.
  • 🚨 bin/fm-watch-arm.sh:144 - Intent requires an accurate “N wake(s) already queued” result when the sequence advances. This new helper treats .wake-queue.seq as durable proof, but fm_wake_append writes that counter before appending the queue record and can fail on the latter write; the arm can therefore suppress watcher: FAILED while the queue has no valid wake. The unlocked read can also observe the counter mid-write. Broaden the scope to make sequence publication transactional with the queue record, or avoid using it as durable proof.

🔧 Fix: Publish wake sequence after durable queue append
1 error still open:

  • 🚨 bin/fm-wake-lib.sh:373 - The new two-file commit order still leaves the exact signal window this change is meant to close. The queue row is appended at line 373 before the sequence is published at lines 375-380; the watcher’s HUP/INT/TERM trap exits immediately, so a signal in that interval leaves a real durable row but an unchanged counter. The arm then emits the literal FAILED result, and a later append can reuse the same sequence number. This contradicts the required accurate already-queued outcome; use an atomic/deferred commit strategy or have the arm recognize committed rows when sequence publication fails.

🔧 Fix: Count queued wake records directly
✅ Re-checked - no issues remain.

🔧 **Test** - 1 issue found → auto-fixed ✅
  • 🚨 bin/fm-watch-arm.sh:132 - The arm does not snapshot .wake-queue.seq; it counts queue records with epoch >= cycle_started_at. A real attached-arm reproduction queued a wake before the arm started in the same second, made no queue write during the arm cycle, and left sequence 1/1; nevertheless the arm exited 4 and printed 1 wake(s) already queued. Restore a monotonic cycle-boundary snapshot and add this regression case.
  • 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 reported by the task: 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
  • bash tests/fm-supervision-events.test.sh
  • bash tests/fm-wake-queue.test.sh
  • bash tests/fm-backend-herdr.test.sh
  • bash tests/fm-backend-herdr-eventwait-smoke.test.sh
  • Manual isolated bin/fm-watch.sh + attached bin/fm-watch-arm.sh flow with a durable queued wake.
  • Manual isolated attached-arm flow with a durable wake queued before the arm cycle in the same real-time second.

🔧 Fix: Snapshot wake queue sequence at arm boundary
✅ Re-checked - no issues remain.

  • 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"
  • Provided full baseline command (already passed).
  • bash tests/fm-watcher-lock.test.sh
  • bash tests/fm-watch-triage.test.sh
  • bash tests/fm-supervision-events.test.sh
  • bash tests/fm-wake-queue.test.sh
  • bash tests/fm-backend-herdr.test.sh
  • bash tests/fm-backend-herdr-eventwait-smoke.test.sh
  • Manual attached and owned arm-cycle reproductions using isolated FM_HOME evidence homes.
  • Manual same-second pre-cycle queue-boundary reproduction.
  • Manual 231,043-byte Herdr capability-schema probe.
✅ **Document** - passed

✅ No issues found.

✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.

Ray West added 5 commits July 20, 2026 12:13
PR kunchenguid#693 replaced the arm layer's silent empty-cycle exit with a loud
"watcher: FAILED" line, but never checked whether a wake had already
landed durably before claiming "no actionable reason" - so a wake
that queued successfully just before the process lost visibility into
its own reason text (an attached arm never sees the owning watcher's
stdout; an owned child can die between a successful append and its
own wake() call) got the same false alarm a genuinely stuck cycle
gets. bin/fm-watch-arm.sh now snapshots state/.wake-queue.seq at
cycle start and reports an accurate "N wake(s) already queued" line
instead of the typed FAILED text whenever the queue advanced during
the cycle, keeping the FAILED alarm only for the genuinely-empty case.

bin/fm-watch.sh's handle_push_transition and multi-file signal loop
also stop discarding an already-queued wake on an unrelated
post-append failure (a dedupe-marker commit, a partial append): those
failures are now best-effort/diagnostic instead of a silent exit 1.

bin/backends/herdr.sh's capability probe no longer SIGPIPEs on stderr
(cosmetic, pre-existing since PR kunchenguid#472; unrelated to the misreporting
but bundled per the same investigation).

Root-caused and reproduced deterministically in
data/scout-watcher-chain-w1/report.md; upstream issue kunchenguid#693.
@raywest
raywest merged commit bd52bc5 into main Jul 20, 2026
raywest added a commit that referenced this pull request Jul 21, 2026
* fix(watch): snapshot queue boundary before fork; dedupe partial-append 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.

* no-mistakes(document): Document watcher queue boundary timing

---------

Co-authored-by: Ray West <[email protected]>
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