Skip to content

fix(bin): make every counted wake queue row presentable or retired - #6

Merged
cisrd merged 6 commits into
mainfrom
fm/fm-wake-drain-counted-row-invariant
Sep 7, 2026
Merged

cisrd merged 6 commits into
mainfrom
fm/fm-wake-drain-counted-row-invariant

Conversation

@cisrd

@cisrd cisrd commented Sep 7, 2026

Copy link
Copy Markdown
Owner

Intent

A wake row that is counted as queued but not presented by bin/fm-wake-drain.sh can never be acknowledged, permanently wedging the queue and repeatedly waking supervision.

This was measured on 2026-09-07 in the fmverif second-mate home.
bin/fm-wake-drain.sh printed only WARNING: queued wakes pending - drain them with bin/fm-wake-drain.sh before anything else. with no presented row and no WAKE_ACK_REQUIRED command.
The queue itself still contained this row:
1788792074<TAB>574<TAB>stale<TAB>fleet:w2:p3<TAB>stale: fleet:w2:p3 (paused 7227s, awaiting external - declared pause, rechecked on a long cadence not a wedge; confirm the wait still holds).

Determine why this counted row is omitted from presentation and make the invariant true: every row counted in the pending-warning condition must either be presented with an executable acknowledgement path or be retired from the queue.
Do not assume the proposed cause; verify whether stale-row type, stopped endpoint, changed recovery generation, supervision-branch ownership, or another filter explains the divergence.
This is distinct from the existing monitoring defects that generate stale rows too often or escalate a declared external wait; this task owns the drain behavior after a row already exists.

What Changed

  • bin/fm-guard.sh now counts queued wakes per actor via a new fm_wake_actor_pending_count helper in bin/fm-wake-lib.sh, so the "queued wakes pending - drain them" warning only fires for rows the calling actor can actually present or retire; a non-empty queue whose rows are reserved by a live supervision-branch grant instead raises a distinct NOTICE naming the branch as holder and saying not to drain from there.
  • bin/fm-wake-drain.sh retires rows that can never be consumed: under the queue lock, a main drain drops rows missing the five appended fields or a numeric sequence, reports the count plus the removed rows verbatim (first 20, then a remainder count), and treats a failed repair as non-fatal so usable rows are still presented; a branch drain never retires. A main drain left with nothing of its own also prints a bounded WAKE ROWS HELD BY SUPERVISION BRANCH line with the held sequences instead of exiting silently.
  • Grant row-list validation and branch owner-record reads were deduplicated out of bin/fm-wake-drain.sh and bin/fm-wake-grant.sh into shared fm_wake_grant_rows_valid / fm_wake_branch_owner_matches / fm_wake_branch_grant_live helpers; tests/fm-wake-queue.test.sh adds two cases driving guard and drain together (branch-held row raises the advisory and becomes presentable and acknowledgeable once the grant clears; unusable rows are retired by main alone while remaining rows stay acknowledgeable), and docs/architecture.md and docs/watcher-continuity.md record the per-actor counting and retirement contract.

Risk Assessment

✅ Low: The change is well-bounded and lock-protected: the riskiest element (irreversible row retirement) is main-only, runs under the queue lock, reports what it removed, and never fails the drain, while the guard narrowing provably cannot silence a non-empty queue since main's and the branch's counted sets partition every queued row — and both behaviors are pinned by new tests that drive the real scripts.

Testing

Exercised the wake-queue, guard-stale-banner, branch-supervision and pi-branch-extension suites (all passing), proved both new tests fail on the base commit and pass on the target, and captured a before/after CLI transcript that replays the reported fmverif field row end-to-end: pre-fix the guard tells the operator to drain while the drain presents nothing and the queue stays wedged; post-fix the branch-held row raises a distinct "do not drain them from here" advisory with the drain naming the holder, and once the grant is released the same row is presented with an executable --ack-through command that empties the queue, while a structurally truncated row is retired with its content reported verbatim. This change has no UI surface — it is entirely stdout/stderr from shell CLIs — so the reviewer-visible evidence is the operator-facing terminal transcript rather than a screenshot.

Evidence: End-to-end CLI transcript: reported field state, before vs after the fix

Source: End-to-end CLI transcript: reported field state, before vs after the fix

Wake-queue counted-row invariant: end-to-end CLI transcript
===========================================================

Reproduction driven through the real CLIs (bin/fm-guard.sh, bin/fm-wake-drain.sh,
bin/fm-wake-grant.sh) against two trees: the base commit and the target commit.
Scenario A replays the reported fmverif field state verbatim: a single stale
fleet:w2:p3 row queued while a live supervision-branch grant holds it.
Scenario B queues a structurally truncated row alongside a usable one.

INVARIANT: any row counted in the pending-warning condition must be presented
with an executable acknowledgement path (or be retired from the queue).

######################################################################
# BEFORE the fix (base commit ffd2c899 - the reported field behavior)
# tree: /tmp/fm-e2e/base
######################################################################

===== A. the reported field state: one stale row queued, held by a LIVE supervision-branch grant =====

state/.wake-queue (verbatim):
    1788814215	1	stale	fleet:w2:p3	stale: fleet:w2:p3 (paused 7227s, awaiting external - declared pause, rechecked on a long cadence not a wedge; confirm the wait still holds)
    (live branch grant published for sequence 1)

$ bin/fm-guard.sh   # what the operator is told on every guarded command
    ●━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
    ●  WATCHER DOWN - SUPERVISION IS OFF
    ●  1 task(s) in flight, but no watcher has a fresh beacon (last beat: never, grace 300s).
    ●  Trust the emitted supervision protocol for this harness; do not use shell & for watcher repair.
    ●  This is a supervision warning only; the guarded operation WILL still run.
    ●  After draining queued wakes, watcher supervision needs Stop-owned automatic recovery; inspect the hook registration and startup status before ending the turn.
    ●━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
    WARNING: queued wakes pending - drain them with bin/fm-wake-drain.sh before anything else.

$ bin/fm-wake-drain.sh   # what the operator gets when they follow that instruction
    WARNING: watcher still down (same stale episode; last beat: never, grace 300s) - full banner already printed this episode.
    WARNING: queued wakes pending - drain them with bin/fm-wake-drain.sh before anything else.

    queue holds 1 row(s)
    guard counts a row as drainable-by-main (ordinary warning): yes
    drain presents a row with an executable ack command:        no
    INVARIANT VIOLATED: counted but unpresentable -> queue is wedged

===== A2. same row, same kind, same endpoint - grant released (nothing else changed) =====

$ bin/fm-guard.sh
    WARNING: watcher still down (same stale episode; last beat: never, grace 300s) - full banner already printed this episode.
    WARNING: queued wakes pending - drain them with bin/fm-wake-drain.sh before anything else.

$ bin/fm-wake-drain.sh
    1788814215	1	stale	fleet:w2:p3	stale: fleet:w2:p3 (paused 7227s, awaiting external - declared pause, rechecked on a long cadence not a wedge; confirm the wait still holds)
    WAKE_ACK_REQUIRED: after handling completes run bin/fm-wake-drain.sh --ack-through 1 --recovery-generation 3035104.1788814215.FVhL35
    WARNING: watcher still down (same stale episode; last beat: never, grace 300s) - full banner already printed this episode.
    WARNING: queued wakes pending - drain them with bin/fm-wake-drain.sh before anything else.

$ bin/fm-wake-drain.sh --ack-through 1 --recovery-generation 3035104.1788814215.FVhL35
    queue after acknowledgement: 0 row(s)

===== B. a structurally unusable row (lost its trailing fields) queued alongside a usable one =====

state/.wake-queue (verbatim):
    1788814217<TAB>1<TAB>signal<TAB>task-a.status<TAB>signal: task-a
    1788792074<TAB>574<TAB>stale<TAB>fleet:w2:p3

$ bin/fm-guard.sh
    ●━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
    ●  WATCHER DOWN - SUPERVISION IS OFF
    ●  1 task(s) in flight, but no watcher has a fresh beacon (last beat: never, grace 300s).
    ●  Trust the emitted supervision protocol for this harness; do not use shell & for watcher repair.
    ●  This is a supervision warning only; the guarded operation WILL still run.
    ●  After draining queued wakes, watcher supervision needs Stop-owned automatic recovery; inspect the hook registration and startup status before ending the turn.
    ●━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
    WARNING: queued wakes pending - drain them with bin/fm-wake-drain.sh before anything else.

$ bin/fm-wake-drain.sh
    1788814217	1	signal	task-a.status	signal: task-a
    WAKE_ACK_REQUIRED: after handling completes run bin/fm-wake-drain.sh --ack-through 1 --recovery-generation 3038985.1788814217.mbYZB2
    WARNING: watcher still down (same stale episode; last beat: never, grace 300s) - full banner already printed this episode.
    WARNING: queued wakes pending - drain them with bin/fm-wake-drain.sh before anything else.

    queue now holds 2 row(s):
      1788814217<TAB>1<TAB>signal<TAB>task-a.status<TAB>signal: task-a
      1788792074<TAB>574<TAB>stale<TAB>fleet:w2:p3

$ bin/fm-wake-drain.sh --ack-through 1 --recovery-generation 3038985.1788814217.mbYZB2
    queue after acknowledgement: 1 row(s)

$ bin/fm-guard.sh   # is the queue finally quiet?
    WARNING: watcher still down (same stale episode; last beat: never, grace 300s) - full banner already printed this episode.
    WARNING: queued wakes pending - drain them with bin/fm-wake-drain.sh before anything else.

    queue holds 1 row(s)
    guard counts a row as drainable-by-main (ordinary warning): yes
    drain presents a row with an executable ack command:        no
    INVARIANT VIOLATED: counted but unpresentable -> queue is wedged


######################################################################
# AFTER the fix (target commit 0227b23)
# tree: ~/.no-mistakes/worktrees/a03e7f5d4084/01M1YQBCAYD7E9ZWHV14G6B5ZK
######################################################################

===== A. the reported field state: one stale row queued, held by a LIVE supervision-branch grant =====

state/.wake-queue (verbatim):
    1788814219	1	stale	fleet:w2:p3	stale: fleet:w2:p3 (paused 7227s, awaiting external - declared pause, rechecked on a long cadence not a wedge; confirm the wait still holds)
    (live branch grant published for sequence 1)

$ bin/fm-guard.sh   # what the operator is told on every guarded command
    ●━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
    ●  WATCHER DOWN - SUPERVISION IS OFF
    ●  1 task(s) in flight, but no watcher has a fresh beacon (last beat: never, grace 300s).
    ●  Trust the emitted supervision protocol for this harness; do not use shell & for watcher repair.
    ●  This is a supervision warning only; the guarded operation WILL still run.
    ●  watcher supervision needs Stop-owned automatic recovery; inspect the hook registration and startup status before ending the turn.
    ●━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
    NOTICE: wake rows held by the live supervision branch - it presents and acknowledges them; do not drain them from here.

$ bin/fm-wake-drain.sh   # what the operator gets when they follow that instruction
    WAKE ROWS HELD BY SUPERVISION BRANCH: 1 queued row(s) (1) are granted to the live supervision branch, which presents and acknowledges them.
    WARNING: watcher still down (same stale episode; last beat: never, grace 300s) - full banner already printed this episode.
    NOTICE: wake rows held by the live supervision branch - it presents and acknowledges them; do not drain them from here.

    queue holds 1 row(s)
    guard counts a row as drainable-by-main (ordinary warning): no
    drain presents a row with an executable ack command:        no
    INVARIANT HOLDS

===== A2. same row, same kind, same endpoint - grant released (nothing else changed) =====

$ bin/fm-guard.sh
    WARNING: watcher still down (same stale episode; last beat: never, grace 300s) - full banner already printed this episode.
    WARNING: queued wakes pending - drain them with bin/fm-wake-drain.sh before anything else.

$ bin/fm-wake-drain.sh
    1788814219	1	stale	fleet:w2:p3	stale: fleet:w2:p3 (paused 7227s, awaiting external - declared pause, rechecked on a long cadence not a wedge; confirm the wait still holds)
    WAKE_ACK_REQUIRED: after handling completes run bin/fm-wake-drain.sh --ack-through 1 --recovery-generation 3041492.1788814219.nAZMDp
    WARNING: watcher still down (same stale episode; last beat: never, grace 300s) - full banner already printed this episode.
    WARNING: queued wakes pending - drain them with bin/fm-wake-drain.sh before anything else.

$ bin/fm-wake-drain.sh --ack-through 1 --recovery-generation 3041492.1788814219.nAZMDp
    queue after acknowledgement: 0 row(s)

===== B. a structurally unusable row (lost its trailing fields) queued alongside a usable one =====

state/.wake-queue (verbatim):
    1788814221<TAB>1<TAB>signal<TAB>task-a.status<TAB>signal: task-a
    1788792074<TAB>574<TAB>stale<TAB>fleet:w2:p3

$ bin/fm-guard.sh
    ●━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
    ●  WATCHER DOWN - SUPERVISION IS OFF
    ●  1 task(s) in flight, but no watcher has a fresh beacon (last beat: never, grace 300s).
    ●  Trust the emitted supervision protocol for this harness; do not use shell & for watcher repair.
    ●  This is a supervision warning only; the guarded operation WILL still run.
    ●  After draining queued wakes, watcher supervision needs Stop-owned automatic recovery; inspect the hook registration and startup status before ending the turn.
    ●━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
    WARNING: queued wakes pending - drain them with bin/fm-wake-drain.sh before anything else.

$ bin/fm-wake-drain.sh
    wake drain: retired 1 unusable queue row(s) that carried no sequence to present or acknowledge:
    wake drain:   1788792074	574	stale	fleet:w2:p3
    1788814221	1	signal	task-a.status	signal: task-a
    WAKE_ACK_REQUIRED: after handling completes run bin/fm-wake-drain.sh --ack-through 1 --recovery-generation 3044355.1788814221.7RuLMS
    WARNING: watcher still down (same stale episode; last beat: never, grace 300s) - full banner already printed this episode.
    WARNING: queued wakes pending - drain them with bin/fm-wake-drain.sh before anything else.

    queue now holds 1 row(s):
      1788814221<TAB>1<TAB>signal<TAB>task-a.status<TAB>signal: task-a

$ bin/fm-wake-drain.sh --ack-through 1 --recovery-generation 3044355.1788814221.7RuLMS
    queue after acknowledgement: 0 row(s)

$ bin/fm-guard.sh   # is the queue finally quiet?
    WARNING: watcher still down (same stale episode; last beat: never, grace 300s) - full banner already printed this episode.

    queue holds 0 row(s)
    guard counts a row as drainable-by-main (ordinary warning): no
    drain presents a row with an executable ack command:        no
    INVARIANT HOLDS
Evidence: Regression proof: new tests fail on base commit, pass on target commit

Source: Regression proof: new tests fail on base commit, pass on target commit

Regression proof: the two new tests fail on the base commit and pass on the target commit
=======================================================================================

The new tests were extracted verbatim from tests/fm-wake-queue.test.sh and run against
a clean export of the base commit (ffd2c899) so only the registration list differs.

--- BASE ffd2c899: test_main_is_never_told_to_drain_rows_only_the_branch_owns ---
ok - historical annotations replay nothing already announced and keep everything new
not ok - main drain went silent instead of naming who holds the queued rows
--- BASE ffd2c899: test_unconsumable_rows_are_retired_instead_of_wedging_the_queue ---
ok - historical annotations replay nothing already announced and keep everything new
not ok - main drain did not report the rows it retired

--- TARGET 0227b23: bash tests/fm-wake-queue.test.sh (full suite) ---
ok - a branch-held row raises no queued-wake warning for main, and the same row is presented and acknowledged once the grant clears
ok - structurally unusable rows are retired by main alone, leaving every remaining row presentable and acknowledgeable
suite exit code: 0
Evidence: Reproduction script used to generate the transcript (runnable against any tree)

Source: Reproduction script used to generate the transcript (runnable against any tree)

#!/usr/bin/env bash
# End-to-end reproduction of the reported field defect, driven through the real
# CLIs (bin/fm-guard.sh, bin/fm-wake-drain.sh, bin/fm-wake-grant.sh) of the tree
# passed as $1. Prints an operator-visible transcript.
set -u
TREE=$1
LABEL=$2
. "$TREE/tests/wake-helpers.sh"
TMP_ROOT=$(fm_test_tmproot fm-e2e-repro)
DRAIN="$ROOT/bin/fm-wake-drain.sh"
GRANT="$ROOT/bin/fm-wake-grant.sh"
GUARD="$ROOT/bin/fm-guard.sh"

PAYLOAD='stale: fleet:w2:p3 (paused 7227s, awaiting external - declared pause, rechecked on a long cadence not a wedge; confirm the wait still holds)'

hr() { printf '\n===== %s =====\n' "$1"; }
run() { # <label> <cmd...>
  local label=$1; shift
  printf '\n$ %s\n' "$label"
  "$@" 2>&1 | sed 's/^/    /'
  return 0
}

printf '######################################################################\n'
printf '# %s\n' "$LABEL"
printf '# tree: %s\n' "$ROOT"
printf '######################################################################\n'

# ---------------------------------------------------------------- scenario A
dir=$(make_case field-repro); state="$dir/state"
printf 'window=test:fm-w2\nkind=ship\n' > "$state/x.meta"
FM_STATE_OVERRIDE="$state" bash -c '. "$1"; fm_wake_append stale "fleet:w2:p3" "$2"' _ "$ROOT/bin/fm-wake-lib.sh" "$PAYLOAD"

hr "A. the reported field state: one stale row queued, held by a LIVE supervision-branch grant"
printf '\nstate/.wake-queue (verbatim):\n'
sed 's/^/    /' "$state/.wake-queue"
FM_STATE_OVERRIDE="$state" "$GRANT" activate "$$" gen-live >/dev/null || { echo "GRANT ACTIVATE FAILED"; exit 1; }
seq0=$(awk -F '\t' 'END{print $2}' "$state/.wake-queue")
FM_STATE_OVERRIDE="$state" "$GRANT" publish gen-live "$seq0" >/dev/null || { echo "GRANT PUBLISH FAILED"; exit 1; }
printf '    (live branch grant published for sequence %s)\n' "$seq0"

run 'bin/fm-guard.sh   # what the operator is told on every guarded command' \
  env FM_STATE_OVERRIDE="$state" "$GUARD"
run 'bin/fm-wake-drain.sh   # what the operator gets when they follow that instruction' \
  env FM_STATE_OVERRIDE="$state" "$DRAIN"
invariant() { # <state> - INVARIANT: a row counted in the pending-warning condition
                # must be presented with an executable acknowledgement path.
  local st=$1 g d counted presented
  g=$(FM_STATE_OVERRIDE="$st" "$GUARD" 2>&1); d=$(FM_STATE_OVERRIDE="$st" "$DRAIN" 2>&1)
  case "$g" in *"queued wakes pending"*) counted=yes ;; *) counted=no ;; esac
  case "$d" in *WAKE_ACK_REQUIRED*) presented=yes ;; *) presented=no ;; esac
  printf '\n    queue holds %s row(s)\n' "$(awk 'END{print NR+0}' "$st/.wake-queue" 2>/dev/null || echo 0)"
  printf '    guard counts a row as drainable-by-main (ordinary warning): %s\n' "$counted"
  printf '    drain presents a row with an executable ack command:        %s\n' "$presented"
  if [ "$counted" = yes ] && [ "$presented" = no ]; then
    printf '    INVARIANT VIOLATED: counted but unpresentable -> queue is wedged\n'
  else
    printf '    INVARIANT HOLDS\n'
  fi
}
invariant "$state"

hr "A2. same row, same kind, same endpoint - grant released (nothing else changed)"
FM_STATE_OVERRIDE="$state" "$GRANT" release gen-live >/dev/null || echo "(release failed)"
run 'bin/fm-guard.sh' env FM_STATE_OVERRIDE="$state" "$GUARD"
run 'bin/fm-wake-drain.sh' env FM_STATE_OVERRIDE="$state" "$DRAIN"
err=$dir/a2.err
FM_STATE_OVERRIDE="$state" "$DRAIN" >/dev/null 2>"$err"
s=$(sed -n 's/^WAKE_ACK_REQUIRED:.*--ack-through \([0-9][0-9]*\) --recovery-generation [A-Za-z0-9._-][A-Za-z0-9._-]*$/\1/p' "$err")
g=$(sed -n 's/^WAKE_ACK_REQUIRED:.*--ack-through [0-9][0-9]* --recovery-generation \([A-Za-z0-9._-][A-Za-z0-9._-]*\)$/\1/p' "$err")
if [ -n "$s" ] && [ -n "$g" ]; then
  run "bin/fm-wake-drain.sh --ack-through $s --recovery-generation $g" \
    env FM_STATE_OVERRIDE="$state" "$DRAIN" --ack-through "$s" --recovery-generation "$g"
  printf '    queue after acknowledgement: %s row(s)\n' "$(awk 'END{print NR}' "$state/.wake-queue" 2>/dev/null || echo 0)"
else
  printf '    no acknowledgement command was presented\n'
fi

# ---------------------------------------------------------------- scenario B
dir=$(make_case truncated-repro); state="$dir/state"
printf 'window=test:fm-w2\nkind=ship\n' > "$state/x.meta"
FM_STATE_OVERRIDE="$state" bash -c '. "$1"; fm_wake_append signal "task-a.status" "signal: task-a"' _ "$ROOT/bin/fm-wake-lib.sh"
printf '1788792074\t574\tstale\tfleet:w2:p3\n' >> "$state/.wake-queue"

hr "B. a structurally unusable row (lost its trailing fields) queued alongside a usable one"
printf '\nstate/.wake-queue (verbatim):\n'
cat -A "$state/.wake-queue" | sed 's/\$$//; s/\^I/<TAB>/g; s/^/    /'
run 'bin/fm-guard.sh' env FM_STATE_OVERRIDE="$state" "$GUARD"
run 'bin/fm-wake-drain.sh' env FM_STATE_OVERRIDE="$state" "$DRAIN"
printf '\n    queue now holds %s row(s):\n' "$(awk 'END{print NR}' "$state/.wake-queue")"
cat -A "$state/.wake-queue" | sed 's/\$$//; s/\^I/<TAB>/g; s/^/      /'
err=$dir/b.err
FM_STATE_OVERRIDE="$state" "$DRAIN" >/dev/null 2>"$err"
s=$(sed -n 's/^WAKE_ACK_REQUIRED:.*--ack-through \([0-9][0-9]*\) --recovery-generation [A-Za-z0-9._-][A-Za-z0-9._-]*$/\1/p' "$err")
g=$(sed -n 's/^WAKE_ACK_REQUIRED:.*--ack-through [0-9][0-9]* --recovery-generation \([A-Za-z0-9._-][A-Za-z0-9._-]*\)$/\1/p' "$err")
if [ -n "$s" ] && [ -n "$g" ]; then
  run "bin/fm-wake-drain.sh --ack-through $s --recovery-generation $g" \
    env FM_STATE_OVERRIDE="$state" "$DRAIN" --ack-through "$s" --recovery-generation "$g"
else
  printf '    no acknowledgement command was presented\n'
fi
printf '    queue after acknowledgement: %s row(s)\n' "$(awk 'END{print NR}' "$state/.wake-queue" 2>/dev/null || echo 0)"
run 'bin/fm-guard.sh   # is the queue finally quiet?' env FM_STATE_OVERRIDE="$state" "$GUARD"
invariant "$state"
printf '\n'
Evidence: Key contrast — the reported field state, one stale row held by a live supervision-branch grant
BEFORE (base ffd2c899)
$ bin/fm-guard.sh
WARNING: queued wakes pending - drain them with bin/fm-wake-drain.sh before anything else.
$ bin/fm-wake-drain.sh
WARNING: watcher still down (same stale episode; last beat: never, grace 300s) - full banner already printed this episode.
WARNING: queued wakes pending - drain them with bin/fm-wake-drain.sh before anything else.
queue holds 1 row(s)
guard counts a row as drainable-by-main (ordinary warning): yes
drain presents a row with an executable ack command: no
INVARIANT VIOLATED: counted but unpresentable -> queue is wedged

AFTER (target 0227b23)
$ bin/fm-guard.sh
NOTICE: wake rows held by the live supervision branch - it presents and acknowledges them; do not drain them from here.
$ bin/fm-wake-drain.sh
WAKE ROWS HELD BY SUPERVISION BRANCH: 1 queued row(s) (1) are granted to the live supervision branch, which presents and acknowledges them.
queue holds 1 row(s)
guard counts a row as drainable-by-main (ordinary warning): no
drain presents a row with an executable ack command: no
INVARIANT HOLDS

AFTER, grant released (same row, same kind, same stopped endpoint)
$ bin/fm-wake-drain.sh
1788814191 1 stale fleet:w2:p3 stale: fleet:w2:p3 (paused 7227s, awaiting external - declared pause, ...)
WAKE_ACK_REQUIRED: after handling completes run bin/fm-wake-drain.sh --ack-through 1 --recovery-generation 3009737.1788814191.NkvT26
WARNING: queued wakes pending - drain them with bin/fm-wake-drain.sh before anything else.
$ bin/fm-wake-drain.sh --ack-through 1 --recovery-generation 3009737.1788814191.NkvT26
queue after acknowledgement: 0 row(s)

Pipeline

Updates from git push no-mistakes

✅ **intent** - passed

✅ No issues found.

🔧 **Rebase** - 6 issues found → auto-fixed ✅
  • ⚠️ bin/fm-wake-lib.sh - merge conflict rebasing onto origin/main
  • ⚠️ bin/fm-watch.sh - merge conflict rebasing onto origin/main
  • ⚠️ docs/architecture.md - merge conflict rebasing onto origin/main
  • ⚠️ docs/configuration.md - merge conflict rebasing onto origin/main
  • ⚠️ tests/fm-pi-branch-extension.test.sh - merge conflict rebasing onto origin/main
  • ⚠️ tests/fm-wake-queue.test.sh - merge conflict rebasing onto origin/main

🔧 Fix applied.
✅ Re-checked - no issues remain.

🔧 **Review** - 1 issue found → auto-fixed ✅
  • ⚠️ bin/fm-guard.sh:189 - Suppressing branch-held rows from the guard count removes the only always-on alarm for a grant that is never released, so such a row can stay unacknowledged indefinitely with no automatic signal. Concrete sequence: the watcher wakes, the Pi extension calls activateEligibleRowsOwner(process.pid, gen) and writeEligibleRowsSnapshot for seq N (.pi/extensions/fm-branch-supervision.ts:879,1402), then await session.prompt(...) (line 1416) never settles - there is no timeout or abort anywhere in that file, and releaseEligibleRowsSnapshot runs only after a settled prompt (line 1437) or in the rejection handler (line 1442), neither of which fires. The owner pid is the live Pi process, so fm_wake_branch_grant_live stays true forever. From then on every guarded command in the captain's main session takes this line, fm_wake_actor_pending_count main returns 0 because seq N is reserved, and the queued-wakes warning that previously fired on every guarded command goes silent. The branch actor's own warning is suppressed by design (line 267), and bin/fm-watch.sh's secondmate_wake_stall_tick only inspects foreign secondmate homes ($home/state/.wake-queue), not the captain's own queue - so no channel alarms. print_branch_held_notice (bin/fm-wake-drain.sh:119) surfaces it only if someone manually runs a drain. This is a deliberate consequence of the stated invariant (uncounted rows satisfy it vacuously) and is documented in the header here and in docs/watcher-continuity.md, but it trades an always-on alarm for a pull-only notice. Flagging as ask-user rather than auto-fix because the remedy is not a correction of what the change does: bounding a grant's lifetime, or alarming on a grant older than some threshold, needs new durable state and a new timer, which extends the change beyond its stated intent. Confirm whether losing the automatic alarm for an unreleasable grant is acceptable.

🔧 Fix: add guard advisory for branch-held wake rows
✅ Re-checked - no issues remain.

✅ **Test** - passed

✅ No issues found.

  • bash tests/fm-wake-queue.test.sh (full suite, exit 0) — includes the two new cases test_main_is_never_told_to_drain_rows_only_the_branch_owns and test_unconsumable_rows_are_retired_instead_of_wedging_the_queue
  • bash tests/fm-guard-stale-banner.test.sh (exit 0) — fm-guard.sh changed
  • bash tests/fm-branch-supervision.test.sh (exit 0)
  • bash tests/fm-pi-branch-extension.test.sh (exit 0)
  • Regression proof: extracted both new test functions verbatim and ran them against a clean git archive ffd2c899 export — both fail pre-fix (not ok - main drain went silent instead of naming who holds the queued rows, not ok - main drain did not report the rows it retired)
  • Manual end-to-end CLI reproduction (wake-drain-invariant-repro.sh) driving bin/fm-wake-grant.sh, bin/fm-guard.sh and bin/fm-wake-drain.sh against base and target trees: scenario A replays the reported stale fleet:w2:p3 (paused 7227s, awaiting external ...) row under a live branch grant; scenario A2 releases the grant leaving the row otherwise identical; scenario B queues a structurally truncated row beside a usable one, then acknowledges through to an empty queue
⚠️ **Document** - 1 info
  • ℹ️ AGENTS.md:135 - Judgment call, no action taken: AGENTS.md:135 still describes state/.wake-queue as "durable queued wakes retained until post-handling acknowledgement", while a main drain now also retires structurally unusable rows without an acknowledgement. I left it unchanged deliberately - AGENTS.md is always-loaded and its size discipline forbids conditional detail, the line's own five-field format already implies the shape a retired row lost, and the exception's authoritative owner (docs/watcher-continuity.md "Per-actor acknowledgement") now states it in full. Flagging only so the omission is a visible decision rather than an oversight.
✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.

cisrd and others added 6 commits September 7, 2026 12:41
…gress (kunchenguid#3943)

* fix(watch): detect stalled secondmate queue progress

* no-mistakes(review): gate secondmate stall on active turns and progress episodes

* no-mistakes(document): align secondmate wake-stall docs with progress-episode detector

* no-mistakes(document): clarify active-turn gate in wake-stall config docs

* no-mistakes(ci): Fixed the one real defect behind the failing checks. ROOT CAUSE (Greptile P1, real code defect in this PR): `secondmate_wake_stall_tick` in bin/fm-watch.sh reset the no-progress timer only when the oldest actionable queue sequence INCREASED (`[ "$seq" -gt "$observed_seq" ]`). When a secondmate is retired and reprovisioned under the same task ID, its fresh home's queue sequence restarts BELOW the recorded position, so the comparison is false, no reset happens, and the new queue inherits the retired generation's already-expired idle interval — emitting a false `secondmate wake-loop stalled` on its very first observation. That is precisely the false-alarm class the user intent requires this PR to remove. FIX (smallest, removal-first): bin/fm-watch.sh:749 now resets when the drain position MOVES AT ALL (`-ne` instead of `-gt`). Draining moves it up, reprovisioning moves it down; neither is a continued no-progress episode. The asymmetric `-gt` branch is removed rather than special-cased or hardened. Updated the function header comment plus the two doc sentences in docs/architecture.md and docs/configuration.md that stated the old advance-only semantics. REGRESSION TEST: added `test_secondmate_reprovisioned_queue_starts_a_fresh_interval` to tests/fm-wake-queue.test.sh (registered in the invocation list). It drives the real watcher through retired generation (seq 9) -> reprovision (seq 3, later clock) -> freeze, asserting observable wake-queue output, no source-text inspection. VERIFICATION: - Fails before / passes after: with the fix reverted the suite aborts on `not ok - a reprovisioned queue generation inherited the retired generation's idle interval and alerted`; with the fix it passes, and its third leg confirms the restarted generation still escalates on a genuine freeze (row=3 idle=2s), so the fix does not merely mute the alarm. - `bash tests/fm-wake-queue.test.sh`: exit 0, 38/38 pass, all five secondmate cases green. - `bin/fm-lint.sh`: clean (ShellCheck 0.11.0, actionlint 1.7.12). CHECKS NOT CAUSED BY THE CODE: the CI and "Require no-mistakes" runs (34152740610, 34152740604) both ended with conclusion `action_required` — workflow approval pending, not a test/build failure. Separately, `bin/fm-test-run.sh --check-coverage` exits 1 in this environment, but I confirmed by stashing my changes that it fails identically on the unmodified base tree (locale-related `comm: input is not in sorted order`); it is pre-existing and this change adds no new test file for the partition to account for. Changes are left uncommitted in the worktree

* no-mistakes(ci): Fixed the one real code defect behind the failing checks. ROOT CAUSE (Greptile P1, second round, on the head commit e1304e6): `secondmate_wake_stall_tick` in bin/fm-watch.sh identified the queue's drain position by the sequence number ALONE. The previous round changed the comparison from `-gt` to `-ne`, which handles a reprovisioned queue that restarts BELOW the recorded position, but not one that restarts ON it. A mate retired and reprovisioned under the same task id gets a fresh home whose wake-queue sequence counter restarts at 1 — and the retained parent progress marker very plausibly holds a low sequence too (a queue frozen on its first row records seq 1). Equal sequence ⇒ no reset ⇒ the brand-new queue inherits the retired generation's long-expired idle interval and emits a false `secondmate wake-loop stalled` on its very first observation. That is exactly the false-alarm class this PR exists to remove. FIX (smallest, removal-first): the file already defines the identity of a queue row once, as `row_key="$epoch-$seq"` (used for stall receipts, the stall marker, and the notify key). The progress marker's separate, weaker seq-only identity is removed: `row_key` is now computed once right after the row is parsed, stored in the progress marker, and compared with `!=`. Across generations the epoch differs (the new generation's rows are appended later), so no sequence collision can carry a stale interval; within a generation the key is stable exactly while the position does not move. bin/fm-wake-lib.sh's `fm_wake_secondmate_progress_marker_write` now takes `<oldest-row-key>` and validates it the same way the two neighbouring row-key writers do. Updated the function header comment and the two doc sentences (docs/architecture.md, docs/configuration.md) that described the old sequence-only semantics. REGRESSION TEST: `test_secondmate_reprovisioned_queue_starts_a_fresh_interval` in tests/fm-wake-queue.test.sh now drives the reported case — the reprovisioned generation restarts on the SAME sequence 9 (epoch 200) that the retired generation recorded (epoch 100), at a later clock — and asserts observable watcher output only. Its third leg still confirms the restarted generation escalates on a genuine freeze (row=9 idle=2s), so the fix does not merely mute the alarm. Three seeded progress markers in the symlink, crash-window and prefix-receipt tests were updated to the epoch-sequence form. VERIFICATION: - Fails before / passes after: with bin/fm-watch.sh and bin/fm-wake-lib.sh reverted to HEAD and the new test in place, the suite aborts on `not ok - a reprovisioned queue generation inherited the retired generation's idle interval and alerted` (exit 1); with the fix, `bash tests/fm-wake-queue.test.sh` exits 0 with 38/38 pass, all six secondmate cases green. - `bin/fm-lint.sh`: clean (ShellCheck 0.11.0, actionlint 1.7.12). - I also started `bin/fm-test-run.sh tests/fm-watch-checkpoint.test.sh tests/fm-watch-triage.test.sh tests/fm-watch-recovery-loop.test.sh` as a blast-radius check; it was still running when this phase had to return, so its result is not included. No other suite references the stall detector or the progress marker (grep over tests/ for `wake-loop stall|SECONDMATE_WAKE_STALL|secondmate-wake-progress` matches only fm-wake-queue.test.sh), and the changed lib function has exactly one caller. CHECKS NOT CAUSED BY THE CODE: the CI and "Require no-mistakes" runs on the head commit (34154091191, 34154091236, 34154091945) all ended with conclusion `action_required` — pending workflow approval, not a test/build failure. `bin/fm-test-run.sh --check-coverage` still exits 1 in this environment for the pre-existing locale reason recorded in the previous phase (`comm: input is not in sorted order` on the unmodified base tree); this change adds no new test file. Changes are left uncommitted in the worktree: bin/fm-watch.sh, bin/fm-wake-lib.sh, docs/architecture.md, docs/configuration.md, tests/fm-wake-queue.test.sh

---------

Co-authored-by: Alex William <[email protected]>
A wake row could be counted as queued while no drain would ever present
it, leaving the operator told to "drain them before anything else" by a
command that printed nothing and offered no acknowledgement.

Two independent paths produced that state.
A row reserved by a live supervision-branch grant is excluded from a main
drain by design, but fm-guard.sh counted the whole queue, so main was
warned about rows only the branch could present, on every guarded command
for as long as the grant was held.
A row that lost its five appended fields or its numeric sequence can never
be claimed, presented, or named by an --ack-through cutoff, yet it still
counted as queued, wedging the queue permanently.

The guard now counts only the rows the calling actor can itself present or
retire, and a main drain retires unusable rows under the queue lock,
reporting them in bounded escaped form before removal so the evidence
survives for the separate row-generating defects. A retirement failure is
reported loudly and never suppresses unrelated consumable work. A main
drain whose remaining rows are all branch-held says so in one bounded line
instead of exiting silently. Grant row-list and owner-record reads move
into fm-wake-lib.sh so the drain, the grant publisher, and the guard share
one implementation.

Ownership is unchanged: a branch drain still touches nothing outside its
grant and never retires a row, and main still cannot present or acknowledge
an active grant.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01GghGvsa4JDB1E5FuznX2i1
… ("Unreadable queue suppresses alarms"). Root cause: fm_wake_actor_pending_count inferred "the queue could not be counted" only from awk's printed output (`case "$count" in ''|*[!0-9]*) count=1`). That relies on awk aborting before its END rule when the input cannot be opened. An awk that reaches END after a failed open prints `0`, which the fallback accepts as a genuine count; both actor counts then read zero and bin/fm-guard.sh emits neither the queued-wake warning nor the branch-held advisory for a queue nobody proved empty. Fix (bin/fm-wake-lib.sh:1828,1835): both counting awk invocations now set `count=''` on a non-zero awk exit status, so the existing "cannot be counted => report a pending row" fallback is driven by awk's exit status instead of an implementation-defined detail of what it printed. No new code path or behavior; the pre-existing fallback just becomes unconditional. Comment updated to state why. Regression test (tests/fm-wake-queue.test.sh: test_uncountable_queue_still_raises_the_pending_alarm, registered in the run list): runs the real bin/fm-guard.sh against a non-empty, unreadable queue with a PATH-injected awk emulating an END-running implementation (prints 0, exits 2; execs the real awk otherwise) and asserts "queued wakes pending" is still emitted; disconfirming half asserts the same fake awk over a readable, provably empty queue stays silent. Fails on the pre-fix library ("not ok - a queue that could not be counted silenced the queued-wake alarm"), passes after. Verified locally: bin/fm-test-run.sh tests/fm-wake-queue.test.sh -> 0 failed; tests/fm-guard-stale-banner.test.sh + tests/fm-watcher-lock.test.sh -> 0 failed; bin/fm-lint.sh (ShellCheck 0.11.0 + actionlint 1.7.12) clean. Caveat reported honestly: on the awks available/known here (mawk locally, plus gawk and BWK/macOS awk, all of which treat an unopenable input as fatal and skip END) the alarm was not actually suppressed - I reproduced the unreadable-queue case and the warning fired. The change removes the code's dependence on that awk detail rather than repairing an outage observed on this platform. Both intent constraints still hold: every counted row remains presentable or retirable, and no alarm-suppressing path was added
@cisrd
cisrd merged commit d42016a into main Sep 7, 2026
26 of 27 checks passed
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