Repository navigation
fix(bin): stop the watcher hot-looping on a parked idle crew - #1
Merged
Merged
Conversation
CORRECTION 1 - the recorded hypothesis was wrong. The backlog item watcher-paused-idle-loop-p3 blamed the first-sight stale path in the arm wrapper, reasoning that because the watcher exits on a wake, every re-arm is a fresh first sight. It is not. bin/fm-watch-arm.sh emits no wake of its own; it only prints the watcher's output. A restart alone does not re-trigger a first sight either, because .hash-*, .count-* and .stale-* all persist on disk - six re-arms against a stable idle pane produce one wake, then silence. The real cause is pane-hash churn. The stale suppressor .stale-<key> is keyed on the PANE HASH, and a parked crew's pane keeps re-rendering an elapsed clock or a context counter, so every re-render is a genuinely new hash and therefore a real fresh first sight. Both first-sight paths then surfaced a bare `stale: <window>` with no cadence throttle: the non-terminal path because pause_state_class returns `none` whenever the agent is alive, and the terminal path because needs-decision: is captain-relevant and the crew is not provably working. A third site sustained it for paused: the changed-hash branch called clear_pause_tracking, discarding the recheck throttle on every re-render. CORRECTION 2 - the continuity-gate recommendation is moot. The item recommends weighing whether to add bin/fm-send.sh to bin/fm-continuity-pretool-check.sh's recovery set. That gate no longer exists: it and fm-continuity-command-policy.mjs were deleted in b05eb24 (kunchenguid#1001, 2026-07-24). docs/watcher-continuity.md now states that no PreToolUse hook denies fleet commands based on watcher status, and the surviving policy returns allow for fm-send.sh, fm-peek.sh, fm-crew-state.sh and fm-session-start.sh. No safety-boundary change is needed. THE FIX. A crew is PARKED when it has declared where it stopped and firstmate already knows: a `paused:` external wait, a `captain-held` transfer, or a captain-relevant status whose delivery is recorded in state/.hb-surfaced-<task>. Both first-sight surface sites now gate on that, absorbing and re-confirming once per FM_PAUSE_RESURFACE_SECS, anchored on the crew's own status file so pane churn cannot reset the cadence. The changed-hash path keeps the park bookkeeping when the agent is live rather than discarding it. Detection is not widened by the absorb. The delivery test means an undelivered done/needs-decision/blocked/failed still surfaces on first sight, and the heartbeat backstop rechecks the same marker. A crew with no declaration, the provably-working wedge timer, the escalation counter, secondmate handling and away-mode triage are all untouched. The one real trade-off: a crew that declares `paused:` and then genuinely wedges now surfaces at the recheck window rather than shortly after each pane re-render.
…ed-terminal wedges once
…ark wake before latching
SC1010: in argument position bash already treats the bare word as the literal string, so this is a parse-clarity fix with no behaviour change. Quoted per shellcheck's own guidance rather than suppressed with a disable directive. This is the first finding real shellcheck has ever reported on this branch: the earlier lint step exited 127 because the binary was absent.
…ot the watcher lifetime
…re one status-mtime read
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
Stop firstmate's watcher (bin/fm-watch.sh) hot-looping on a legitimately idle crew pane. Live defect: two crews finished and waiting on a merge each woke firstmate roughly every two minutes with nothing to do.
ROOT CAUSE, correcting the backlog item. The item hypothesised the first-sight stale path in bin/fm-watch-arm.sh, reasoning that because the watcher exits on a wake every re-arm is a fresh first sight. I verified before fixing and that is WRONG: the arm wrapper emits no wake of its own and marker files persist across restarts, so six re-arms against a stable idle pane produce exactly one wake. The real cause is pane-hash churn - .stale- is keyed on the pane hash, and a parked crew's pane keeps re-rendering an elapsed clock or context counter, so every re-render is a genuinely new hash and a real fresh first sight, which both first-sight paths surfaced immediately with no cadence throttle.
DESIGN, deliberate decisions so they are not read as accidents:
KNOWN OPEN FINDINGS from the previous review of this identical code, all four already decided FIX by the supervisor and to be fixed in three rounds this run: park-latch-never-released (the fired latch in .park-since- has no release path a re-tasked crew hits, because the busy-pane release needs the 40-line tail byte-identical across two polls while a working TUI churns it every poll, so the one-shot becomes once per watcher lifetime instead of once per park spell - the 2026-07-26 sixteen-hour outage failure reappearing inside its own fix, needing a test proving TWO park spells in one lifetime each escalate); daemon-park-cleanup-unreachable (the .park-since cleanup added to the daemon's clear_pause_tracking cannot be reached for the only state producing that marker); doc-no-verb-path-imprecise (the architecture.md no-verb-signal-path sentence is true only for the paused/captain-held half, since captain-relevant declarations surface via the captain-relevant path); stale-key-duplication (six open-coded window-to-stale-key transforms in two idioms that must stay in lockstep or silently split a window's markers).
This branch is fm/watcher-paused-idle-loop-p3b, a fresh branch at the identical commit as the earlier p3 branch, created only because a cancelled run left custody stranded and both guarded recovery paths refused; no code differs.
SC1010: commit 7ac2112 quotes the 'done' task id in tests/fm-watch-triage.test.sh. Bash already treats it as a literal string in argument position, so it is a parse-clarity fix with zero behaviour change, quoted per shellcheck's own remedy with an explanatory comment and deliberately not suppressed with a disable directive. Shellcheck 0.11.0 is installed and bin/fm-lint.sh passes clean.
Rebased cleanly onto a5fe1bc; the only overlapping file was bin/fm-supervise-daemon.sh where kunchenguid#1066's edits and my .park-since- marker addition both survive. Regression coverage: paused and needs-decision parks each silent across four re-arms of a re-rendering pane; the recheck firing once per window for both verbs labelled a recheck not a wedge; a frozen pane still reaching its recheck; a dead run behind a delivered terminal status escalating; an undelivered needs-decision still surfacing; a genuinely unresponsive pane still reported. 40/40 triage and 99/99 daemon on the rebased base.
Firstmate's own shared tracked material, so repo style applies: one sentence per line in prose, plain dashes, shellcheck-clean bin scripts, tests colocated in tests/.
What Changed
bin/fm-watch.shnow gates both first-sight stale paths on acrew_is_parkedtest instead of treating every pane-hash change as a fresh sighting. A park is a declaredpaused:wait, a captain-held transfer, or a captain-relevant status whose delivery is already recorded instate/.hb-surfaced-<task>; the vocabulary half lands inbin/fm-classify-lib.shasstatus_is_parkedso the watcher and the away-mode daemon share it. A parked crew whose pane keeps re-rendering an elapsed clock or context counter no longer surfaces on every re-render, and an undelivered terminal status still surfaces immediately. Deliberate behaviour change: a live-agent crew parked at an external-decision gate no longer emits an immediate bare stale wake, since its declaring status write already surfaces through the signal path.park_recheck_duesolely owns the resurface cadence and the shared.paused-resurfaced-<key>marker, anchored on the status file mtime rather than the pane hash, andhandle_parked_staleis reachable on the repeat-hash poll so a park whose pane freezes still reaches its recheck.park_wedge_checkgives a park on an already-delivered captain-relevant status one escalation per park spell atFM_STALE_ESCALATE_SECS, on its own.park-since-<key>timer keyed to the status mtime so a re-tasked crew starts a new spell;bin/fm-supervise-daemon.shclears that marker alongside the other pause tracking. Away-mode triage is untouched —afk_presentshort-circuits ahead of every park-gate call site.tests/fm-watch-triage.test.shgrows park coverage: silence across re-arms of a re-rendering pane for bothpaused:andneeds-decision:parks, one recheck per window labelled a recheck rather than a wedge, a frozen pane still reaching its recheck, a dead run behind a delivered terminal status escalating, an undeliveredneeds-decision:still surfacing, a genuinely unresponsive pane still reported, and a regression driving two park spells in one watcher lifetime that each escalate.docs/architecture.md,docs/configuration.md,AGENTS.md, and.agents/skills/afk/SKILL.mddocument the park markers, cadence, and per-verb signal paths.Risk Assessment
Testing
I ran the watcher triage suite (41/41), the daemon suite (99/99) and the adjacent watcher-lock, wake-queue and watch-checkpoint suites (all green), then built CLI transcripts that drive the real bin/fm-watch.sh through re-arms the way fm-watch-arm.sh does: the live defect burns 12 firstmate wakes over six re-arms of two parked crews on base a5fe1bc and zero on this branch, the declaring status still reaches firstmate once through the signal path while only the redundant bare stale wakes disappear, the bounded nets each fire exactly once (recheck labelled a recheck, frozen pane still rechecked, escalation flagged a possible wedge, declared waits never escalating, undelivered and undeclared crews still surfaced immediately), and a before/after comparison against ece9fdc shows the park latch that previously silenced the second park spell forever now yielding one escalation per spell. This is a shell watcher with no rendered surface, so the reviewer-visible evidence is CLI transcripts rather than screenshots; the worktree was left clean and the temporary base checkouts removed.
Evidence: Hot loop before/after: 12 wakes on base a5fe1bc vs 0 on this branch
=== BEFORE (base a5fe1bc, pre-fix watcher) === crew test:fm-merge - declared external wait, agent still alive re-arm 1..6 (pane re-rendered): WOKE -> stale: test:fm-merge (x6) crew test:fm-decision - needs-decision already delivered to firstmate re-arm 1..6 (pane re-rendered): WOKE -> stale: test:fm-decision (x6) TOTAL firstmate wakes across both crews / 6 re-arms each: 12 === AFTER (6b1502d, this branch) === crew test:fm-merge re-arm 1..6: quiet -> absorbed stale (paused, awaiting external, age 5s..31s) crew test:fm-decision re-arm 1..6: quiet -> absorbed stale (parked on needs-decision, age 5s..31s) wake queue firstmate would drain: (empty) TOTAL firstmate wakes across both crews / 6 re-arms each: 0Evidence: AFTER transcript (this branch, wake queue empty)
Evidence: Bounded-silence nets on this branch (recheck, frozen pane, escalation, undelivered, unresponsive)
[recheck] re-arm 1 WOKE stale: test:fm-decision (parked 504s on needs-decision, rechecked on a long cadence not a wedge; confirm the crew is still legitimately waiting) / re-arms 2-3 quiet -> 1 wake [frozen] re-arm 1 WOKE same recheck reason on the repeat-hash path / re-arm 2 quiet -> 1 wake [escalate] re-arm 1 WOKE stale: ... (idle 500s behind an already-delivered needs-decision, possible wedge - the crew may have been re-tasked and stopped since; peek before re-absorbing) / re-arm 2 quiet declared paused, idle 500s past threshold: quiet -> 0 wakes [undelivered] re-arm 1 WOKE stale: test:fm-decision -> 1 wake [unresponsive] re-arm 1 WOKE stale: test:fm-quiet -> 1 wakeEvidence: Park-latch regression: second park spell silent pre-fix (ece9fdc), escalates once per spell after
=== ece9fdc (before 382af82), re-tasked pane churns every poll === spell 1: WOKE (possible wedge) latch after spell 1: fired re-tasked: pane busy again latch after the busy pane: fired <- never released spell 2: past due on its own timer: quiet spell 2: still past due: quiet => stale wakes queued: 1 === 6b1502d (this branch), same scenario === spell 1: WOKE (possible wedge) latch after spell 1: 1785146699 fired re-tasked: pane busy again latch after the busy pane: (released) spell 2: past due on its own timer: WOKE (possible wedge) spell 2: still past due: quiet => stale wakes queued: 2Evidence: Deliberate change: declaration still delivered once, only the redundant stale wakes gone
BASE a5fe1bc: re-arm 1 WOKE signal: .../paused.status ; re-arm 2 WOKE stale: test:fm-paused ; re-arm 3 WOKE stale: test:fm-paused 6b1502d: re-arm 1 WOKE signal: .../paused.status ; re-arm 2 quiet ; re-arm 3 quietEvidence: Repro driver: parked-crew hot loop across watcher re-arms
Evidence: Repro driver: park safety nets and two-spell escalation
Evidence: Watch triage suite log (41/41)
/tmp/no-mistakes-evidence/01KYHCVFCKSYDS2DVBY13BFW0F/daemon-suite.log)Evidence: Evidence index and re-run instructions
Pipeline
Updates from git push no-mistakes
✅ **intent** - passed
✅ No issues found.
✅ **Rebase** - passed
✅ No issues found.
🔧 **Review** - 4 issues found → auto-fixed (2) ✅
bin/fm-watch.sh:412- Thefiredlatch park_wedge_check writes into.park-since-<key>has no release path a re-tasked crew reaches, so the escalation is one-shot per watcher lifetime, not per park spell. Trace: crew parks on a deliveredneeds-decision:, escalates, latches. Firstmate re-tasks it. (a) The busy release at line 1098 requiresn >= 2, i.e. three consecutive byte-identical 40-line captures whilewindow_is_busyis true - a working TUI churns its footer every poll, so the hash never repeats and control always lands in the changed-hash branch at line 1103, which deliberately preserves$pkf. (b) The provably-working release at line 1001 requires a stably-stale NEW hash plus crew_is_provably_working, which a churning pane never reaches. (c) The surface release at line 1014 requires the stale path to see the new status while still undelivered, but the signal path surfaces a fresh status first, calls mark_surfaced, andwakeexits the process before the stale loop at line 933 runs; the next cycle already sees the status delivered, so crew_is_parked is true and park_wedge_check returns early onfired. Net effect: for park spell 2 onward a re-tasked crew whose run died behind an unchanged delivered terminal line is only caught at the FM_PAUSE_RESURFACE_SECS recheck (3600s default) instead of FM_STALE_ESCALATE_SECS (240s). docs/architecture.md:27 asserts the opposite ("fires at most once per park spell and is released only by evidence the crew resumed"), and no test exercises two park spells in one watcher lifetime - test_delivered_terminal_park_escalates_once_when_its_run_dies only pins the first escalation and the latch holding. Needs a real release condition plus a regression test driving two park spells in one lifetime, each escalating.bin/fm-supervise-daemon.sh:468- The.park-since-$watcher_keyremoval added to the daemon's clear_pause_tracking is dead code for the only state that produces the marker..park-since-<key>is written exclusively by fm-watch.sh's park_wedge_check, which runs only for a captain-relevant status (handle_parked_stale returns early and rm's the file forpaused:/captain-held). The daemon reaches clear_pause_tracking only from reconcile_pause_tracking'selif [ -e "$marker" ] || [ -e "$state/.paused-$watcher_key" ]branch, and for a captain-relevant status neither file can exist: pause_marker_record writes.subsuper-paused-<task>only under status_is_paused, and.paused-<watcher_key>is cleared by fm-watch.sh:940 the moment the status stops being paused/captain-held while handle_parked_stale never sets it. Consequence: afiredlatch (or a live park timer) survives an away-mode round trip untouched, compounding the latch-release defect above. Either give the daemon a reachable trigger keyed on the captain-relevant park, or drop the unreachable rm and record the ownership.docs/architecture.md:28- "A park's own declaring status write still surfaces once through the normal-mode no-verb signal path" generalizes to every park, but the no-verb path only covers thepaused:/captain-heldhalf.done:,needs-decision:,blocked:andfailed:all satisfy status_is_captain_relevant, so their declaring write surfaces through the captain-relevant signal path (and the heartbeat's.hb-surfaced-<task>backstop), not the no-verb path - which is precisely why crew_is_parked needs the delivery marker for that half and not for the other. Split the sentence so each half names its actual delivery path.bin/fm-watch.sh:948- The window-to-stale-key transform is open-coded eight times in fm-watch.sh across two idioms that must stay in lockstep, and this change adds a site (handle_parked_stale, line 364).printf '%s' "$win" | tr ':/.' '___'at lines 319, 364, 485, 948; thekey=${win//:/_}; key=${key//\//_}; key=${key//./_}chain at lines 418-420, 426-428, 439-441, 936-938. Lines 936-938 and 948 recompute the identical value inside the same loop iteration. fm-supervise-daemon.sh:427 already defines_stale_key(); lift it into a shared lib (fm-classify-lib.sh or fm-push-transition-lib.sh) and call it from every site. If the character set ever diverges between the two idioms, a window's.stale-<key>and.park-since-<key>silently point at different keys and both park nets break with no error.🔧 Fix: scope park escalation latch to its park spell, not the watcher lifetime
2 issues (1 warning, 1 info) still open:
tests/fm-watch-triage.test.sh:1159- The new-status-event half of test_two_park_spells_in_one_lifetime_each_escalate cannot fail on the regression it names. Line 1152 seeds the spent latch as<pre-append mtime> fired; line 1153 appends a new status so the file mtime becomes now. Line 1159 then compares the marker againstfired_latch "$state" decision, which evaluates to<post-append mtime> fired- a string the implementation never writes on this path. An implementation that ignored the spell id and carried the spent latch across the status event would leave<pre-append mtime> firedin place, the comparison would be false, and the&& failwould not trigger. Line 1161's seed_park_timer then overwrites the marker with a fresh numeric timer, so the escalation assertion at line 1165 passes regardless. The busy-pane half of the same test does discriminate correctly, so the headline once-per-park-spell requirement is covered; only this release path is untested. Fix: capture the old-spell latch string into a local before deliver_park_status and assert the marker no longer equals it (or assert the marker matches<new mtime> <digits>), and let spell 2's own timer age past the threshold instead of overwriting it.bin/fm-watch.sh:291- The new park_status_mtime helper's header states "Both park nets are anchored on it", but only park_wedge_check calls it. park_recheck_due (line 313) still open-codes the samestat_mtime "$STATE/$task.status"read, and the two disagree on the unreadable-file fallback: park_status_mtime yields 0 (line 297) while park_recheck_due yieldsdate +%s(line 314). If a status file is momentarily unreadable, park_wedge_check treats the spell as id 0 and rewrites the marker with a restarted timer while park_recheck_due computes PARK_AGE=0 and suppresses the recheck - both fail safe, but the helper's stated single-source invariant is false. Route park_recheck_due through park_status_mtime, or narrow the comment to name only park_wedge_check.🔧 Fix: discriminate the park-spell release test and share one status-mtime read
✅ Re-checked - no issues remain.
✅ **Test** - passed
✅ No issues found.
bash tests/fm-watch-triage.test.sh(41/41, includes the seven new park cases and the two-park-spell regression test)bash tests/fm-daemon.test.sh(99/99, covers the daemon-side .park-since cleanup edit)bash tests/fm-watcher-lock.test.sh(28/28),bash tests/fm-wake-queue.test.sh(12/12),bash tests/fm-watch-checkpoint.test.sh(4/4)Manual end-to-end hot-loop replay driving the real bin/fm-watch.sh through 6 watcher re-arms for apaused:crew and a deliveredneeds-decision:crew with a re-rendering pane:bash park-hotloop-repro.sh /tmp/fm-base-tree "BEFORE"vsbash park-hotloop-repro.sh <worktree> "AFTER"Manual safety-net replaybash park-nets-repro.sh <worktree> "SAFETY NETS"covering scenarios declaration / recheck / frozen / escalate / spells / undelivered / unresponsive against the real watcherManual park-latch regression comparisonbash park-nets-repro.sh /tmp/fm-prelatch-tree ... spells(ece9fdc) vs the same scenario on 6b1502d, with the re-tasked pane churning on every pollManual deliberate-change checkbash park-nets-repro.sh /tmp/fm-base-tree ... declaration(base a5fe1bc) vs the [declaration] block on this branch✅ **Document** - passed
✅ No issues found.
✅ **Lint** - passed
✅ No issues found.
✅ **Push** - passed
✅ No issues found.