Skip to content

fix(watch): skip empty rearm-resurface wakes and stop wedge-nagging a done: worker's idle pane - #3547

Open
dklKevin wants to merge 2 commits into
kunchenguid:mainfrom
dklKevin:fix/watcher-empty-resurface-and-done-idle
Open

dklKevin wants to merge 2 commits into
kunchenguid:mainfrom
dklKevin:fix/watcher-empty-resurface-and-done-idle

Conversation

@dklKevin

@dklKevin dklKevin commented Sep 2, 2026

Copy link
Copy Markdown

Two small watcher fixes that remove two classes of pointless wake-ups, one commit each with its colocated tests.

Problem 1: empty check: rearm-resurface wakes

Observed. On a Cursor primary every turn end re-arms a watcher cycle. The fresh cycle reads the gap since the previous clean close as downtime, the recovery marker says recover, and resurface_after_downtime fired wake "check: rearm-resurface" unconditionally. The handling turn then drained an empty queue (the drain printed --ack-through 0) and did nothing. The home's delivery log records nine of these between 10:44 and 12:08 on 2026-09-02 (10:44, 11:12, 11:12, 11:16, 11:57, 11:57, 11:59, 12:03, 12:08), at least five of which the handling turn drained as --ack-through 0, and ten more on 2026-09-01.

Change (bin/fm-watch.sh, bin/fm-watch-arm.sh, docs/watcher-continuity.md). The recovery marker is consumed exactly as before (pending:downtime to announced:downtime, same token grammar, same generation-bound acknowledgement, same release-lock / release-lock-existing cleanup). Only the wake is now gated on the drain having something to present:

  • an unacknowledged row in the durable wake queue, or
  • a decision still open in a status log (scan_open_decisions, the pure whole-file fold owned by bin/fm-classify-lib.sh, so the watcher never advances the drain's incremental cursors) - this is the buried-decision case the recovery episode contract in docs/watcher-continuity.md already names and the existing test_rearm_resurfaces_durable_queue_and_remote_open_decision already requires, or
  • the watcher reclaimed a stale lock from a predecessor that died without cleanup (FM_LOCK_RECOVERED_PID from the lock library, or FM_WATCH_LOCK_RECLAIMED=1 which bin/fm-watch-arm.sh --restart now hands to the child it starts after clearing a reused-pid lock, so both stale-lock reclaim paths keep announcing as the existing arm and lock tests assert).

With none of those, the cycle writes one triage line, absorbed rearm-resurface (queue empty, no open decisions), clears its in-process pending flag, and keeps supervising. A later append in that cycle reuses the announced generation and its drain acknowledges it, so no marker is left dangling; the next non-successor arm mints a fresh generation exactly as today.

Guardrails preserved. No change to the marker state machine, token grammar, or ack/generation format. A queue with records always wakes. Handling successors still never re-announce. UNREAD STATUS notes are deliberately not part of the gate: they are not in the fm_recovery_* contract, and a status append during downtime is caught by the signal scan on the next cycle anyway.

Tests (tests/fm-watch-triage.test.sh):

  • test_rearm_resurface_absorbed_when_nothing_to_present - empty queue + pending:downtime marker: no wake across two full polls, no queue row, marker consumed to announced:downtime:<same generation>, exactly one triage line.
  • test_rearm_resurface_delivered_for_queued_row - one unacknowledged row + the same marker: check: rearm-resurface delivered, generation kept announced, row drained.
  • test_rearm_resurface_delivered_for_open_decision - empty queue but a still-open needs-decision [key=...]: wake still delivered.
  • tests/fm-watch-arm.test.sh test_malformed_marker_is_quarantined_once now seeds one durable row, because a quarantined marker over an empty queue is now absorbed; its quarantine-exactly-once and no-loop assertions are unchanged.

Problem 2: a worker that reported done: PR <url> is waiting, not stale

Observed. A direct-PR ship worker opened its PR, appended done: PR https://..., and idled for review. Its TUI re-rendered its idle footer now and then, so every new pane hash re-ran the terminal stale path and woke firstmate with another stale: <window> for the same finished worker. The home's delivery log shows this repeating on one finished worker's pane on 2026-08-31 (12:51, 13:56, 17:39) and 2026-09-01 (09:32, 09:54), and again at 11:42 on 2026-09-02 on another; the same log shows each reason was a plain stale: <window> re-surface on a new hash rather than a wedge escalation. Both workers' state/<id>.meta recorded pr= (corroborating, not required).

Change (bin/fm-watch.sh, bin/fm-classify-lib.sh). status_is_done is a new verb predicate in bin/fm-classify-lib.sh, next to status_is_terminal_verb; the watcher uses it rather than re-parsing the log. When a task's last status event is a terminal done: and its pane is idle (two identical hashes, no busy verdict), the stale path now classifies it like a declared paused: wait through the existing handle_paused_stale: absorbed on first sight, rechecked once per PAUSE_RESURFACE_SECS with a new wording (done <age>s, awaiting merge authority - ... the PR poll owns the merge outcome), never fed to the wedge timer. The pause bookkeeping (.paused-*, the re-surface throttle) now also survives a re-rendered idle footer, which is what made the noise repeat. Mirroring the terminal path, a NEW hash still lets an actively running run-step override the log and keep the wedge timer (crew_is_provably_working), so a frozen validation firstmate started after the done: line still escalates, as test_stale_terminal_status_overridden_by_active_run continues to assert.

Guardrails preserved. Anything that is not a terminal done: keeps today's stale behaviour exactly: working: followed by silence surfaces at once and wedge-escalates past STALE_ESCALATE_SECS; needs-decision:, blocked:, and failed: keep the immediate terminal surface. A done: worker whose pane is busy again never reaches the exemption: the busy path, busy_turn_bound_check, and its status_is_paused_or_captain_held gate are untouched, and a busy pane still clears the pause bookkeeping. Secondmate and away-mode branches run before the new branch and are unchanged.

Tests (tests/fm-watch-triage.test.sh):

  • test_done_idle_stale_takes_pause_cadence - done + idle with a 1s wedge threshold across several polls: no wake, no wedge timer, pause flag recorded; a re-rendered footer (new hash) stays silent; past the long cadence one recheck worded awaiting merge authority, never possible wedge.
  • test_working_idle_stale_still_wedge_escalates - working: + idle: immediate stale: <window>, then possible wedge past the threshold, no pause flag.
  • test_done_busy_pane_keeps_turn_bound_escalation - done + busy pane past BUSY_TURN_MAX_SECS: wedge timer starts and escalates as today, no pause flag.
  • Two existing fixtures (test_terminal_stale_surfaced, test_terminal_first_sight_drops_a_finished_write_deferral_chain) used a done: line to reach the plain terminal surface; they now use blocked:, which keeps the path they exercise.

Docs

One sentence each at the single owner: the recovery-wake rule in docs/watcher-continuity.md "Recovery episode acknowledgement", and the done + idle rule in the stale: reason description of the bin/fm-watch.sh header. AGENTS.md and skills untouched (the guidelines' placement tree puts both rules at the mechanism owner).

Maintainer verification (2026-09-02, macOS, stock /bin/bash 3.2.57(1)-release)

Every test below ran under stock bash 3.2.57, which is the only bash on the machine, so the whole run doubles as the 3.2 proof. bin/fm-lint.sh pinned ShellCheck 0.11.0 (fm-lint.sh: ShellCheck 0.11.0 (pinned 0.11.0), full extended analysis).

bin/fm-lint.sh bin/fm-watch.sh bin/fm-watch-arm.sh bin/fm-classify-lib.sh tests/fm-watch-arm.test.sh tests/fm-watch-triage.test.sh
  -> lint rc=0
bin/fm-doc-audience-check.sh
  -> fm-doc-audience-check: ok surfaces=89 local_links=302
bin/fm-test-run.sh tests/fm-watch-triage.test.sh
  -> FM_TEST_SUMMARY total=1 failed=0 ... (96 ok, including the six new tests)
bin/fm-test-run.sh tests/fm-wake-queue.test.sh tests/fm-watcher-lock.test.sh tests/fm-watch-arm.test.sh
  -> fm-watcher-lock exit=0, fm-watch-arm exit=0, fm-wake-queue exit=1 (pre-existing, see below)
bin/fm-test-run.sh tests/fm-watch-recovery-loop.test.sh tests/fm-pr-check-security.test.sh
  -> both exit=0
bin/fm-test-run.sh --all --per-script-timeout-secs 900
  -> FM_TEST_SUMMARY total=176 failed=16 skipped_gate=26

The 16 --all failures were re-run against a pristine git archive origin/main copy in the same environment: 15 fail identically there (fm-wake-queue relies on BASHPID, which bash 3.2 lacks; fm-lint-workflows and fm-lint need actionlint; fm-cursor-harness, fm-composer-lib, fm-muse-harness, fm-extension-binding, fm-gotmp, fm-on, fm-pending-reply, fm-public-followup, fm-teardown, the two remote-secondmate suites and the Herdr presentation e2e are host-environment failures unrelated to the watcher). The sixteenth, fm-bootstrap-network-parallel, passed on origin/main and passes on this branch when run alone (exit=0 duration_ms=13870); it failed only under the full-suite load and touches no file this PR changes. Every watcher-related suite (fm-watch-triage, fm-watch-arm, fm-watcher-lock, fm-watch-recovery-loop, fm-watch-checkpoint, fm-pr-check-security, the fm-wake-drain-* suites) passed on this branch. Tests were run against temporary FM_STATE_OVERRIDE directories only; no watcher was run against a live home.

Note for reviewers: the tests ran with Cursor's own CURSOR_AGENT environment variables unset, because the harness-detection fixtures (fm-watcher-lock's guard banner test) otherwise read the test runner's host as a Cursor primary.

dklKevin added 2 commits September 2, 2026 15:00
…ng to present

A Cursor primary re-arms a watcher cycle at every turn end, and the fresh
cycle reads the gap since the previous close as downtime. The pending
recovery marker then said recover and resurface_after_downtime woke
firstmate with check: rearm-resurface unconditionally, so the handling
turn drained an empty queue (--ack-through 0) and did nothing. Five of
those landed between 10:44 and 12:08 on 2026-09-02 alone.

The generation is still consumed exactly as before (pending -> announced,
same token grammar, same acknowledgement), but the wake is now gated on
presentable work: an unacknowledged durable queue row, a still-open
decision in a status log (the buried-decision case the recovery contract
exists for), or a reclaimed stale lock from a watcher that died without
cleanup. With none of those the cycle logs one
"absorbed rearm-resurface" triage line and keeps supervising.
bin/fm-watch-arm.sh hands the reused-pid clear to its child through
FM_WATCH_LOCK_RECLAIMED so both stale-lock reclaim paths still announce.

Tests: test_rearm_resurface_absorbed_when_nothing_to_present,
test_rearm_resurface_delivered_for_queued_row,
test_rearm_resurface_delivered_for_open_decision
(tests/fm-watch-triage.test.sh); the malformed-marker arm test now seeds
a durable row so its quarantine assertion still has a wake to observe.
A direct-PR worker opens its PR, appends "done: PR <url>", and idles
waiting for review. Its TUI re-renders the idle footer now and then, so
every new pane hash re-ran the terminal stale path and woke firstmate
with another "stale: <window>" for the same finished worker: repeatedly
all day on 2026-09-01 on one finished worker, and again at 11:42 on
2026-09-02 on another. The done: event already surfaced through the
signal path and the PR poll owns the merge outcome, so that idle pane is
an expected wait, not a wedge.

When the last status event is a terminal done: (status_is_done, owned by
bin/fm-classify-lib.sh) and the pane is idle, the stale path now takes
the same bounded cadence as a declared paused: wait: absorbed on first
sight, rechecked once per PAUSE_RESURFACE_SECS with a merge-authority
wording, never fed to the wedge timer, and its pause bookkeeping survives
a re-rendered idle footer. A new hash still lets an actively running
run-step override the log exactly as the terminal path does, so a frozen
validation started after the done: line keeps its wedge escalation. A
done: worker whose pane is busy again, and every other terminal verb
(needs-decision, blocked, failed) plus a working: line followed by
silence, keep today's behaviour unchanged.

Tests: test_done_idle_stale_takes_pause_cadence,
test_working_idle_stale_still_wedge_escalates,
test_done_busy_pane_keeps_turn_bound_escalation
(tests/fm-watch-triage.test.sh). Two existing fixtures that relied on a
done: line reaching the plain terminal surface now use blocked:, which
keeps the path they exercise.
@greptile-apps

greptile-apps Bot commented Sep 2, 2026

Copy link
Copy Markdown

Confidence Score: 4/5

The PR should not merge until the done-idle exemption is constrained to states that are genuinely awaiting a PR merge outcome.

The verb-only branch suppresses ordinary stale supervision for local-only completion and the no-mistakes pre-validation handoff even though neither state has the PR poll assumed by the new behavior.

Files Needing Attention: bin/fm-watch.sh, bin/fm-classify-lib.sh

Reviews (1): Last reviewed commit: "fix(watch): give a done: worker's idle p..." | Re-trigger Greptile

Comment thread bin/fm-watch.sh
printf '%s' "$h" > "$sf"
wake "stale: $w"
fi
elif status_is_done "$last"; then

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Done exemption hides non-PR work

When a local-only worker writes done: ready in branch ... or a no-mistakes worker writes its preliminary done:, the verb-only branch moves its idle pane onto the long merge-wait cadence even though no PR poll owns that state. This suppresses ordinary stale and wedge handling until the long recheck and reports the misleading claim that a PR poll owns the outcome.

Knowledge Base Used:

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