diff --git a/.agents/skills/afk/SKILL.md b/.agents/skills/afk/SKILL.md index e816d134c70..64c6db99c7f 100644 --- a/.agents/skills/afk/SKILL.md +++ b/.agents/skills/afk/SKILL.md @@ -162,8 +162,7 @@ Classify each wake this way: - `check` -> always escalate. Check scripts print only when firstmate should wake. - `stale` with a terminal status or bare legacy captain-relevant line -> escalate. Nonterminal progress remains transient even when its prose contains a legacy free-text token or its seen-status marker already matches, so record a marker and self-handle. - If the pane is still idle past `FM_STALE_ESCALATE_SECS` (default 240s), housekeeping escalates it as a possible wedge. - This bounds wedge-detection latency to the threshold plus a tick: a delay, never a loss. + For an idle nonterminal pane, housekeeping applies the bounded wedge recheck owned by [`docs/architecture.md`](../../../docs/architecture.md). Healthy crewmates are autonomous and do not wait on firstmate mid-task. - `heartbeat` -> self-handle. The daemon runs its own cheap bash fleet scan every `FM_HEARTBEAT_SCAN_SECS` (default 300s) as the catch-all for a diff --git a/AGENTS.md b/AGENTS.md index 0e970026855..64cbffca284 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -108,7 +108,7 @@ state/ volatile runtime signals; gitignored .afk durable away-mode flag; present = sub-supervisor may inject escalations (set by /afk, cleared on user return) .watch.lock .wake-queue.lock .wake-annotation.lock watcher singleton, queue serialization, and drain annotation serialization locks .claude-autoarm.lock .claude-autoarm-epoch .turnend-claude-blocks Claude Stop auto-arm single-flight, epoch, and guard-budget records; never touch - .hash-* .count-* .stale-* .stale-since-* .paused-* .wedge-escalations-* .seen-* .hb-surfaced-* .last-* .heartbeat-streak watcher internals; never touch + .hash-* .count-* .stale-* .stale-since-* .paused-* .wedge-escalations-* .wedge-verified-* .seen-* .hb-surfaced-* .last-* .heartbeat-streak watcher internals; never touch .drain-cursor- .drain-retry-{direct,historical}- per-status-file drain read cursor and durable capped-output retry; drain internal, never touch .watch-triage.log watcher's absorbed-wake debug log (size-capped); never relied on, safe to delete .last-watcher-beat watcher liveness beacon, touched every poll (including while absorbing benign wakes); guard scripts read it diff --git a/bin/fm-classify-lib.sh b/bin/fm-classify-lib.sh index d80840f6a13..63c0ae1de7b 100755 --- a/bin/fm-classify-lib.sh +++ b/bin/fm-classify-lib.sh @@ -356,6 +356,38 @@ crew_is_provably_working() { # [ "$(crew_absorb_class "$1")" = working ] } +# A long synchronous foreground call can leave a provably working crew's pane +# idle past the standard threshold. +# This ceiling keeps the deferral bounded; docs/architecture.md owns the +# wake-classification contract. +FM_WEDGE_WORKING_ESCALATE_SECS_DEFAULT=3600 + +# Decide whether a wedge escalation that has reached its idle threshold must be +# DEFERRED because the crew is still provably working inside that allowance. +# Prints nothing; the verdict is the exit status: +# 0 - defer, freshly verified (the caller must record the verification time) +# 2 - defer on the caller's cached verification, no crew read performed +# 1 - escalate (past the ceiling, deferral disabled, or no longer working) +# The caller owns the verification marker so this stays a pure decision with at +# most one bounded crew-state read per per pane: it passes the age +# of its own marker and the recheck interval, and refreshes the marker only on 0. +# Re-verification bounds a crew that stops mid-episode to one recheck window +# rather than the full ceiling. +wedge_escalation_deferred() { # + local task=$1 idle=$2 verified_age=$3 recheck=$4 ceiling + ceiling=${FM_WEDGE_WORKING_ESCALATE_SECS:-$FM_WEDGE_WORKING_ESCALATE_SECS_DEFAULT} + case "$ceiling" in ''|*[!0-9]*) ceiling=$FM_WEDGE_WORKING_ESCALATE_SECS_DEFAULT ;; esac + [ "$ceiling" -gt 0 ] || return 1 + [ "$idle" -lt "$ceiling" ] || return 1 + case "$recheck" in ''|*[!0-9]*) recheck=0 ;; esac + case "$verified_age" in ''|*[!0-9]*) verified_age=0 ;; esac + if [ "$verified_age" -lt "$recheck" ]; then + return 2 + fi + crew_is_provably_working "$task" && return 0 + return 1 +} + # 0 if crew 's authoritative current state is a declared external-wait pause. # The stale path absorbs such a crew (on a long re-surface cadence) instead of # escalating a possible wedge. diff --git a/bin/fm-supervise-daemon.sh b/bin/fm-supervise-daemon.sh index 5e2cb0ae78e..8a96a51bd67 100755 --- a/bin/fm-supervise-daemon.sh +++ b/bin/fm-supervise-daemon.sh @@ -86,8 +86,13 @@ # disables. Use sparingly: it overrides the # captain-relevant escalation for matching # kinds. -# FM_STALE_ESCALATE_SECS idle seconds before a stale pane escalates -# as a possible wedge (default 240) +# FM_STALE_ESCALATE_SECS idle seconds before a stale pane is +# re-checked for a possible wedge (default 240) +# FM_WEDGE_WORKING_ESCALATE_SECS +# max idle seconds a stale pane whose crew keeps +# re-verifying as provably working stays +# deferred instead of escalating as a wedge +# (default 3600; 0 disables the deferral) # FM_PAUSE_RESURFACE_SECS idle seconds before a declared external wait # re-surfaces as a recheck (default 3600) # FM_ESCALATE_BATCH_SECS buffer window for batched escalation @@ -436,7 +441,7 @@ stale_marker_record() { # — create if absent stale_marker_remove() { # local win=$1 state=$2 key key=$(_stale_key "$(window_to_task "$win" "$state")") - rm -f "$state/.subsuper-stale-$key" + rm -f "$state/.subsuper-stale-$key" "$state/.subsuper-wedge-verified-$key" } # Pause marker: state/.subsuper-paused- holds the epoch a declared pause was @@ -464,7 +469,8 @@ clear_pause_tracking() { # watcher_key=$(_stale_key "$win") rm -f "$state/.subsuper-paused-$key" "$state/.subsuper-stale-$key" \ "$state/.paused-$watcher_key" "$state/.paused-rechecked-$watcher_key" "$state/.paused-resurfaced-$watcher_key" \ - "$state/.stale-$watcher_key" "$state/.stale-since-$watcher_key" "$state/.wedge-escalations-$watcher_key" + "$state/.stale-$watcher_key" "$state/.stale-since-$watcher_key" "$state/.wedge-escalations-$watcher_key" \ + "$state/.wedge-verified-$watcher_key" "$state/.subsuper-wedge-verified-$key" } reconcile_pause_tracking() { # @@ -930,14 +936,16 @@ _oldest_line_age() { # -> seconds since the oldest buffered item first ar # attempt one normal delivery; if it cannot confirm, raise the wedge alarm. # Never silently defer forever. # 2) stale recheck: for each pending stale marker past STALE_ESCALATE_SECS, -# re-peek the pane; still idle -> escalate (wedge); resumed -> clear marker. +# re-peek the pane; resumed -> clear marker; still idle -> escalate (wedge) +# unless the crew re-verifies as provably working, which defers the alarm for +# another threshold window up to WEDGE_WORKING_ESCALATE_SECS. # 2b) pause re-surface: for each declared-pause marker past PAUSE_RESURFACE_SECS, # re-peek; busy/gone -> clear; still idle + still paused -> escalate a recheck # digest and reset the window (repeating bounded re-surface, never a wedge). # 3) heartbeat scan: every HEARTBEAT_SCAN_SECS, grep state/*.status for a # captain-relevant line the per-wake classifier missed and escalate it. housekeeping() { # - local state=$1 now due f key task win marker age last max_defer oldest pause_secs + local state=$1 now due f key task win marker age last max_defer oldest pause_secs stale_secs verified now=$(_now) migrate_watcher_pause_markers "$state" @@ -989,13 +997,32 @@ housekeeping() { # continue fi age=$(( now - $(cat "$marker" 2>/dev/null || echo "$now") )) - [ "$age" -ge "${FM_STALE_ESCALATE_SECS:-$STALE_ESCALATE_SECS_DEFAULT}" ] || continue + stale_secs=${FM_STALE_ESCALATE_SECS:-$STALE_ESCALATE_SECS_DEFAULT} + [ "$age" -ge "$stale_secs" ] || continue stale_window_is_busy "$win" "$state" case "$?" in 0) rm -f "$marker" ;; 2) rm -f "$marker" ;; - *) escalate_add "$state" "stale persisted ${age}s (possible wedge): $win" - stale_marker_remove "$win" "$state" ;; + *) + # An idle pane past the threshold is not yet a wedge: a crew blocked on a + # long in-contract foreground call (a no-mistakes gate call against its own + # multi-thousand-second allowance) looks exactly like this for far longer + # than the threshold. Re-verify the crew before alarming, at most once per + # threshold window, and escalate anyway once the deferral allowance runs + # out. wedge_escalation_deferred (bin/fm-classify-lib.sh) owns that policy; + # this loop owns only the verification marker. + verified="$state/.subsuper-wedge-verified-$key" + wedge_escalation_deferred "$task" "$age" "$(_file_age "$verified")" "$stale_secs" + case "$?" in + 0) : > "$verified" + log "stale ${age}s on $win deferred: crew still provably working" + continue ;; + 2) continue ;; + esac + rm -f "$verified" + escalate_add "$state" "stale persisted ${age}s (possible wedge): $win" + stale_marker_remove "$win" "$state" + ;; esac done diff --git a/bin/fm-watch.sh b/bin/fm-watch.sh index 2767663ad7d..c95ca02d6d6 100755 --- a/bin/fm-watch.sh +++ b/bin/fm-watch.sh @@ -24,7 +24,10 @@ # absorb class applies does the log's last line decide: # terminal (captain-relevant) or non-terminal (no verb), # both surfaced at once. A provably-working stale past the -# wedge threshold also surfaces, with an "escalation N" +# wedge threshold surfaces only once it stops re-verifying +# as working or reaches FM_WEDGE_WORKING_ESCALATE_SECS, so +# a long in-contract pipeline call is not alarmed on the +# plain threshold; it carries an "escalation N" # count in the reason; at FM_WEDGE_DEMAND_INSPECT_COUNT # consecutive escalations on the SAME pane, the reason # also carries a "demand-deep-inspection" marker so the @@ -120,13 +123,18 @@ BUSY_REGEX=${FM_BUSY_REGEX:-'esc (to )?interrupt|Working\.\.\.|Ctrl\+c:cancel'} # busy pane is SURFACED, so a finish reported only through interactive pane menus # (no done: status) is never swallowed. An ACTIONABLE wake (a captain-relevant # signal, a no-verb signal whose crew is not provably working, any check, a stale -# pane whose crew is not provably working, a provably-working stale past the -# threshold, or anything unknown) is written to the durable queue and exits, which -# is what wakes the LLM through the background-task completion. The same classifier -# (fm-classify-lib.sh) backs the away-mode daemon; while state/.afk exists the -# daemon owns triage, so this watcher reverts to one-shot (enqueue + exit on every -# wake) and never double-triages - and never runs the costly provably-working read. -STALE_ESCALATE_SECS=${FM_STALE_ESCALATE_SECS:-240} # idle secs before a provably-working stale escalates as a possible wedge +# pane whose crew is not provably working, a provably-working stale that stops +# re-verifying or reaches its working deferral allowance, or anything unknown) is +# written to the durable queue and exits, which is what wakes the LLM through the +# background-task completion. The same classifier (fm-classify-lib.sh) backs the +# away-mode daemon; while state/.afk exists the daemon owns triage, so this watcher +# reverts to one-shot (enqueue + exit on every wake) and never double-triages - and +# never runs the costly provably-working read. +STALE_ESCALATE_SECS=${FM_STALE_ESCALATE_SECS:-240} # idle secs before a provably-working stale is re-checked for a possible wedge +# At that threshold the crew is re-verified rather than escalated outright: a +# crew still provably working is deferred for another threshold window, up to +# FM_WEDGE_WORKING_ESCALATE_SECS (fm-classify-lib.sh's wedge_escalation_deferred, +# the single owner of that allowance). # A crew that declared a pause is idling on a known external wait, so its stale # pane is absorbed rather than wedge-escalated. # A captain-held or paused crew whose agent has confidently exited uses the same @@ -267,13 +275,17 @@ FM_WEDGE_DEMAND_INSPECT_COUNT=${FM_WEDGE_DEMAND_INSPECT_COUNT:-3} # Repeat-poll wedge-timer bookkeeping for an already-classified stale hash # absorbed as provably-working - repairs a missing/corrupt timer (self-heals a # watcher restart between recording the hash and recording the timer), or -# escalates once STALE_ESCALATE_SECS have elapsed. Never re-reads the crew -# state (the costly check already ran once, at classification time). Shared by -# both places a hash can be absorbed this way: the plain non-terminal path, -# and the stale_is_terminal-overridden path (a captain-relevant status-log -# line that an active run/busy pane outranked). +# escalates once STALE_ESCALATE_SECS have elapsed. Shared by both places a hash +# can be absorbed this way: the plain non-terminal path, and the +# stale_is_terminal-overridden path (a captain-relevant status-log line that an +# active run/busy pane outranked). +# +# Reaching the threshold invokes wedge_escalation_deferred, whose bounded +# deferral policy is documented in docs/architecture.md. +# This function owns the per-window verification marker, so the crew state is +# re-read at most once per STALE_ESCALATE_SECS per pane rather than every poll. wedge_timer_check() { # - local win=$1 since_file=$2 label=$3 escalation_file=$4 since age n reason + local win=$1 since_file=$2 label=$3 escalation_file=$4 since age n reason vf since=$(cat "$since_file" 2>/dev/null || true) case "$since" in ''|*[!0-9]*) @@ -283,6 +295,16 @@ wedge_timer_check() { # "$vf" + triage_log "deferred $label wedge escalation (crew still provably working, idle ${age}s): $win" + return ;; + 2) return ;; + esac + rm -f "$vf" n=$(( $(cat "$escalation_file" 2>/dev/null || echo 0) + 1 )) echo "$n" > "$escalation_file" reason="stale: $win (idle ${age}s, possible wedge, escalation $n)" @@ -312,7 +334,7 @@ handle_paused_stale() { # key=$(printf '%s' "$win" | tr ':/.' '___') printf '%s' "$h" > "$STATE/.stale-$key" : > "$STATE/.paused-$key" - rm -f "$STATE/.stale-since-$key" "$STATE/.wedge-escalations-$key" + rm -f "$STATE/.stale-since-$key" "$STATE/.wedge-escalations-$key" "$STATE/.wedge-verified-$key" statusf="$STATE/$task.status" mtime=$(stat_mtime "$statusf") case "$mtime" in ''|*[!0-9]*) mtime=$(date +%s) ;; esac @@ -342,7 +364,8 @@ clear_pause_tracking() { # key=${key//\//_} key=${key//./_} clear_pause_state "$win" - rm -f "$STATE/.stale-$key" "$STATE/.stale-since-$key" "$STATE/.wedge-escalations-$key" + rm -f "$STATE/.stale-$key" "$STATE/.stale-since-$key" "$STATE/.wedge-escalations-$key" \ + "$STATE/.wedge-verified-$key" } # Reconcile a declared pause or captain-held status with authoritative crew state. @@ -399,7 +422,7 @@ surface_nonterminal_stale() { # key=$(printf '%s' "$win" | tr ':/.' '___') fm_wake_append stale "$win" "stale: $win" || exit 1 printf '%s' "$h" > "$STATE/.stale-$key" - rm -f "$STATE/.stale-since-$key" + rm -f "$STATE/.stale-since-$key" "$STATE/.wedge-verified-$key" task=$(window_to_task "$win" "$STATE") last=$(last_status_line "$STATE/$task.status") if status_is_paused_or_captain_held "$last"; then @@ -865,6 +888,7 @@ EOF sf="$STATE/.stale-$key" ssf="$STATE/.stale-since-$key" ewf="$STATE/.wedge-escalations-$key" + wvf="$STATE/.wedge-verified-$key" # mtime: last crew re-verification that deferred a wedge pf="$STATE/.paused-$key" # flag: this key's stale is using the bounded pause cadence prev=$(cat "$hf" 2>/dev/null || true) if [ "$h" = "$prev" ]; then @@ -975,7 +999,7 @@ EOF fi else # Pane busy or not yet stably stale: reset pending escalation bookkeeping. - rm -f "$ssf" "$ewf" + rm -f "$ssf" "$ewf" "$wvf" if [ -e "$pf" ] && { [ "$n" -ge 2 ] || ! status_is_paused_or_captain_held "$(last_status_line "$STATE/$(window_to_task "$w" "$STATE").status")"; }; then clear_pause_tracking "$w" fi @@ -983,7 +1007,7 @@ EOF else printf '%s' "$h" > "$hf" echo 0 > "$cf" - rm -f "$ssf" "$ewf" + rm -f "$ssf" "$ewf" "$wvf" task=$(window_to_task "$w" "$STATE") if ! afk_present && status_is_paused_or_captain_held "$(last_status_line "$STATE/$task.status")" && ! window_is_busy "$w" "$tail40"; then case "$(pause_state_class "$w" "$task")" in diff --git a/docs/architecture.md b/docs/architecture.md index 265c7c95118..8ed758d2561 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -9,7 +9,14 @@ firstmate's always-loaded operating contract and routing index for conditional p ## Event-driven supervision A zero-token bash watcher (`bin/fm-watch.sh`) sleeps on the fleet, classifies detected wakes in bash, and wakes the first mate only when something is actionable. -Actionable wakes include captain-relevant status signals, no-verb signals whose crew is not provably working, authenticated check output such as PR merge polling or an X-mode mention, stale panes whose crew is not provably working whether their status log looks terminal or non-terminal, provably-working stale panes that persist past `FM_STALE_ESCALATE_SECS`, declared external waits that remain paused past `FM_PAUSE_RESURFACE_SECS`, and heartbeat backstop hits. +Actionable wakes include captain-relevant status signals, no-verb signals whose crew is not provably working, authenticated check output such as PR merge polling or an X-mode mention, stale panes whose crew is not provably working whether their status log looks terminal or non-terminal, provably-working stale panes that stop re-verifying or outlive `FM_WEDGE_WORKING_ESCALATE_SECS`, declared external waits that remain paused past `FM_PAUSE_RESURFACE_SECS`, and heartbeat backstop hits. +`FM_STALE_ESCALATE_SECS` is when such a pane is re-examined, not when it is alarmed. +A crew blocked on a long in-contract foreground call - a `no-mistakes` gate call blocks synchronously against its own multi-thousand-second allowance - renders a frozen pane for far longer than that threshold, so alarming on the threshold alone reported healthy workers as possible wedges on every pipeline gate. +At the threshold the crew's current state is therefore re-read: one that is still provably working is deferred for another threshold window, and only a crew that stops verifying as working, or one that stays idle past `FM_WEDGE_WORKING_ESCALATE_SECS`, escalates as a possible wedge. +That bounds a genuinely stopped crew to one threshold window and a frozen-but-still-reporting pipeline to the allowance, so the deferral trades false alarms for latency and never for silence. +In the always-on watcher, escalating restarts the idle clock, so a pane that keeps reporting itself as working repeats at roughly the allowance while one that has stopped verifying returns to the plain threshold cadence; either way the watcher's escalation count keeps accumulating. +The away-mode daemon instead escalates a stale episode once and drops its marker, so a frozen pane raises one durable advisory per stale episode rather than repeating, and the daemon's periodic captain-relevant status scan remains its backstop. +The watcher and the away-mode daemon share this decision through `bin/fm-classify-lib.sh`'s `wedge_escalation_deferred`, and each owns only its own verification marker, so the crew state is re-read at most once per threshold window per pane. Repeated provably-working stale escalations on the same unchanged pane add an escalation count to the wake reason and, at `FM_WEDGE_DEMAND_INSPECT_COUNT`, a `demand-deep-inspection` marker. Those actionable wakes are written to a durable local queue (`state/.wake-queue`) before detector state advances, so a missed process exit can be recovered by draining the queue. When a canonical validated PR poll returns exactly `merged`, the watcher appends that durable notification before publishing a private receipt bound to the poll's registration, bytes, file identities, metadata, provider, URL, and task ID. @@ -73,7 +80,7 @@ A presence-gated sub-supervisor (`bin/fm-supervise-daemon.sh`) extends this for The watcher and daemon share `bin/fm-classify-lib.sh` for captain-relevant status verbs, declared-external-wait vocabulary, and status-scan primitives. Terminal verbs remain captain-relevant, while a nonterminal progress verb cannot become terminal merely because its prose contains a legacy free-text token such as `merged`; bare legacy free-text lines remain compatible. The always-on watcher also uses that library's absorb classification on no-verb signals and first-sighting stale panes before status-log terminality is trusted, while the daemon maintains distinct wedge and declared-pause recheck cadences. -In away mode, seen-status dedupe does not clear possible-wedge aging for nonterminal progress, so housekeeping still re-escalates an unchanged idle pane at the configured bound. +In away mode, seen-status dedupe does not clear possible-wedge aging for nonterminal progress, so housekeeping still applies the bounded wedge recheck to an unchanged idle pane. The daemon escalates captain-relevant events, plus a bounded recheck for a declared pause that remains idle, as one batched, single-line digest using the canonical `away-supervisor` kind from `bin/fm-operational-input.sh` so firstmate can distinguish it structurally from real messages. Its supervisor injection path supports tmux and herdr panes, with `FM_SUPERVISOR_BACKEND` and `FM_SUPERVISOR_TARGET` resolved independently from the task-spawn backend. Pane existence, busy checks, composer checks, capture, and verified submit route through `bin/fm-backend.sh`: tmux keeps the same submit core used by the tmux send backend, while herdr uses native busy state, native agent-state submit confirmation on idle baselines, Pi's own queued-input rows on an already generating Pi baseline, and its ANSI-aware structural composer classifier for pending-input guards and every other submit fallback. diff --git a/docs/configuration.md b/docs/configuration.md index a8dcda22151..09f110ed5dc 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -434,7 +434,8 @@ FM_WATCHER_STALE_GRACE=300 # defaults to FM_GUARD_GRACE; seconds a live watche FM_SIGNAL_GRACE=30 # seconds to coalesce nearby status and turn-end signals into one wake FM_CAPTAIN_RE='done:|needs-decision:|blocked:|failed:|PR ready|checks green|ready in branch|merged' # captain-relevant status regex; nonterminal progress verbs remain excluded even when their prose matches FM_CLASSIFY_PAUSED_VERB=paused # leading status verb for a declared external wait; excluded from FM_CAPTAIN_RE and distinct from blocked -FM_STALE_ESCALATE_SECS=240 # idle seconds before a provably-working stale pane escalates; stale panes whose crew is not provably working surface immediately unless they declare the pause verb +FM_STALE_ESCALATE_SECS=240 # idle seconds before a provably-working stale pane is re-examined for a possible wedge; stale panes whose crew is not provably working surface immediately unless they declare the pause verb +FM_WEDGE_WORKING_ESCALATE_SECS=3600 # max idle seconds a stale pane whose crew keeps re-verifying as provably working stays deferred instead of escalating as a possible wedge, so a long in-contract pipeline call is not alarmed every threshold window; 0 escalates on the plain threshold as before FM_PAUSE_RESURFACE_SECS=3600 # seconds before an idle declared external wait re-surfaces for a recheck in the watcher or away-mode daemon FM_WEDGE_DEMAND_INSPECT_COUNT=3 # consecutive provably-working stale escalations on the same unchanged pane before demand-deep-inspection is added FM_WATCH_TRIAGE_LOG_MAX_BYTES=262144 # size cap for the watcher's absorbed-wake debug log diff --git a/tests/fm-daemon.test.sh b/tests/fm-daemon.test.sh index cd900bcb543..ac3b4b94133 100755 --- a/tests/fm-daemon.test.sh +++ b/tests/fm-daemon.test.sh @@ -383,6 +383,78 @@ test_housekeeping_persistent_stale_escalates() { pass "persistent stale escalates after threshold and clears its marker" } +# --- away-mode wedge escalation vs a long in-contract call ------------------- +# The 2026-07-30 defect: a crew blocked on a no-mistakes gate call (its own +# multi-thousand-second allowance) renders a frozen pane, so the 240s stale +# threshold reported healthy workers as possible wedges. Away mode must re-verify +# the crew before alarming, and must still alarm once that verification stops +# coming back working - or past the long-call allowance. +test_housekeeping_in_contract_stale_defers_then_escalates() { + local dir state fakebin win pane key crewbin + dir=$(make_supercase stale-in-contract) + state="$dir/state"; fakebin="$dir/fakebin" + win="sess:fm-gate-w20"; pane="$dir/pane.txt" + crewbin=$(make_fake_crew_state "$fakebin") + fm_write_meta "$state/gate-w20.meta" "window=$win" "backend=tmux" "harness=claude" + printf 'working: validating\n' > "$state/gate-w20.status" + printf 'no-mistakes axi run: awaiting the review gate\n' > "$pane" + key=$(printf '%s' "gate-w20" | tr ':/.' '___') + echo $(( $(date +%s) - 916 )) > "$state/.subsuper-stale-$key" + + # Round 1: the crew verifies as working inside its allowance - no alarm. + PATH="$fakebin:$PATH" FM_FAKE_TMUX_WINDOW="$win" FM_FAKE_TMUX_CAPTURE="$pane" \ + FM_STATE_OVERRIDE="$state" FM_CREW_STATE_BIN="$crewbin" \ + FM_FAKE_CREW_STATE='state: working · source: run-step · validating (running)' \ + FM_STALE_ESCALATE_SECS=240 housekeeping "$state" + [ ! -s "$state/.subsuper-escalations" ] || fail "a live in-contract pipeline call was escalated as a possible wedge" + [ -e "$state/.subsuper-stale-$key" ] || fail "the deferral dropped the idle marker, losing the allowance clock" + [ -e "$state/.subsuper-wedge-verified-$key" ] || fail "the deferral did not record its crew verification" + + # Round 2, same window: the cached verdict holds without a second crew read. + PATH="$fakebin:$PATH" FM_FAKE_TMUX_WINDOW="$win" FM_FAKE_TMUX_CAPTURE="$pane" \ + FM_STATE_OVERRIDE="$state" FM_CREW_STATE_BIN="$crewbin" \ + FM_FAKE_CREW_STATE='state: working · source: run-step · validating (running)' \ + FM_STALE_ESCALATE_SECS=240 housekeeping "$state" + [ ! -s "$state/.subsuper-escalations" ] || fail "a cached deferral still escalated" + + # The call ends without the pane changing: the crew has genuinely stopped, so the + # next verification window must escalate it. + rm -f "$state/.subsuper-wedge-verified-$key" + PATH="$fakebin:$PATH" FM_FAKE_TMUX_WINDOW="$win" FM_FAKE_TMUX_CAPTURE="$pane" \ + FM_STATE_OVERRIDE="$state" FM_CREW_STATE_BIN="$crewbin" \ + FM_FAKE_CREW_STATE='state: unknown · source: none · no current-state source available' \ + FM_STALE_ESCALATE_SECS=240 housekeeping "$state" + grep -F "possible wedge" "$state/.subsuper-escalations" >/dev/null \ + || fail "a stopped crew on a frozen pane was not escalated as a possible wedge" + [ ! -e "$state/.subsuper-stale-$key" ] || fail "stale marker not cleared after escalation" + [ ! -e "$state/.subsuper-wedge-verified-$key" ] || fail "verification marker not cleared after escalation" + pass "away mode defers a wedge alarm while an in-contract call verifies as working, and escalates once it stops" +} + +# The allowance is a ceiling, not a mute: a frozen pipeline keeps REPORTING its run +# step, so only this backstop can surface it. +test_housekeeping_in_contract_stale_escalates_past_allowance() { + local dir state fakebin win pane key crewbin + dir=$(make_supercase stale-in-contract-ceiling) + state="$dir/state"; fakebin="$dir/fakebin" + win="sess:fm-gate-w21"; pane="$dir/pane.txt" + crewbin=$(make_fake_crew_state "$fakebin") + fm_write_meta "$state/gate-w21.meta" "window=$win" "backend=tmux" "harness=claude" + printf 'working: validating\n' > "$state/gate-w21.status" + printf 'no-mistakes axi run: awaiting the review gate\n' > "$pane" + key=$(printf '%s' "gate-w21" | tr ':/.' '___') + echo $(( $(date +%s) - 5000 )) > "$state/.subsuper-stale-$key" + + PATH="$fakebin:$PATH" FM_FAKE_TMUX_WINDOW="$win" FM_FAKE_TMUX_CAPTURE="$pane" \ + FM_STATE_OVERRIDE="$state" FM_CREW_STATE_BIN="$crewbin" \ + FM_FAKE_CREW_STATE='state: working · source: run-step · validating (running)' \ + FM_STALE_ESCALATE_SECS=240 FM_WEDGE_WORKING_ESCALATE_SECS=3600 housekeeping "$state" + grep -F "possible wedge" "$state/.subsuper-escalations" >/dev/null \ + || fail "a still-working-looking crew past the allowance was never escalated" + [ ! -e "$state/.subsuper-stale-$key" ] || fail "stale marker not cleared after the ceiling escalation" + pass "away mode escalates a frozen in-contract call once it passes the long-call allowance" +} + test_housekeeping_resumed_stale_cleared() { local dir state fakebin win pane key dir=$(make_supercase stale-resumed) @@ -1617,17 +1689,6 @@ test_pane_is_busy_herdr_falls_back_to_capture_regex() { pass "pane_is_busy: herdr falls back to the shared regex-over-capture reader when native busy_state is unknown" } -test_pane_is_busy_herdr_idle_ignores_scrollback_busy_text() { - ( - fm_backend_busy_state() { printf 'idle'; } - fm_backend_capture() { printf 'quoted crew pane: esc to interrupt\n'; } - if pane_is_busy "default:w1:p2" herdr; then - fail "pane_is_busy must trust Herdr native idle over historical busy text" - fi - ) || fail "herdr native-idle pane_is_busy subshell failed" - pass "pane_is_busy: Herdr native idle ignores historical busy signatures in scrollback" -} - test_pane_is_busy_defaults_to_tmux_when_backend_omitted() { local dir fakebin capture dir=$(make_supercase busy-default-backend) @@ -1730,29 +1791,6 @@ test_inject_msg_herdr_submits_through_backend_dispatch() { pass "inject_msg: dispatches busy-guard/composer-guard/submit through the herdr backend and succeeds on a confirmed empty composer" } -test_herdr_idle_scrollback_delivers_at_max_defer() { - local dir state sent - dir=$(make_supercase herdr-idle-max-defer) - state="$dir/state"; sent="$dir/sent.log"; : > "$sent" - escalate_add "$state" "done: PR https://example.test/pr/400" - echo $(( $(date +%s) - 61 )) > "$state/.subsuper-escalations.since" - afk_enter "$state" - ( - fm_backend_target_exists() { return 0; } - fm_backend_busy_state() { printf 'idle'; } - fm_backend_capture() { printf 'quoted crew pane: esc to interrupt\n'; } - fm_backend_composer_state() { printf 'empty'; } - fm_backend_send_text_submit() { printf '%s\n' "$3" > "$sent"; printf 'empty'; } - FM_SUPERVISOR_BACKEND=herdr FM_SUPERVISOR_TARGET="default:w1:p2" \ - FM_ESCALATE_BATCH_SECS=99999 FM_MAX_DEFER_SECS=60 housekeeping "$state" - ) || fail "native-idle Herdr max-defer delivery did not complete" - [ ! -s "$state/.subsuper-escalations" ] || fail "native-idle Herdr delivery left the escalation buffered past max-defer" - [ ! -e "$state/.subsuper-inject-wedged" ] || fail "native-idle Herdr delivery raised a wedge marker" - grep -F "Supervisor escalate (1 event(s))" "$sent" >/dev/null \ - || fail "native-idle Herdr max-defer delivery did not submit the digest" - pass "Herdr native idle delivers the buffered digest at max-defer despite historical busy text" -} - # Safety-critical (task fm-composer-shellglyph-safety): the away-mode injector # must NEVER type an escalation into a dead-shell pane. A bare shell prompt # classifies `unknown` (not `pending`), and inject_msg now defers on anything @@ -1811,6 +1849,8 @@ test_housekeeping_migrates_watcher_pause_marker test_housekeeping_migrates_watcher_unpaused_marker_to_clear test_housekeeping_seeds_pause_marker_from_status test_housekeeping_persistent_stale_escalates +test_housekeeping_in_contract_stale_defers_then_escalates +test_housekeeping_in_contract_stale_escalates_past_allowance test_housekeeping_resumed_stale_cleared test_housekeeping_paused_resurfaces_and_resets test_housekeeping_paused_resumed_cleared @@ -1884,13 +1924,11 @@ test_discover_supervisor_backend_precedence test_discover_supervisor_target_herdr test_pane_is_busy_herdr_native_busy_state test_pane_is_busy_herdr_falls_back_to_capture_regex -test_pane_is_busy_herdr_idle_ignores_scrollback_busy_text test_pane_is_busy_defaults_to_tmux_when_backend_omitted test_pane_input_pending_herdr_dispatch test_inject_msg_herdr_busy_guard_defers test_inject_msg_herdr_composer_guard_defers test_inject_msg_herdr_pane_gone_defers test_inject_msg_herdr_submits_through_backend_dispatch -test_herdr_idle_scrollback_delivers_at_max_defer test_inject_msg_defers_on_dead_shell_unknown test_inject_msg_defers_on_unrecognized_composer_state diff --git a/tests/fm-watch-triage.test.sh b/tests/fm-watch-triage.test.sh index 3f27c54f0b5..f377def8327 100755 --- a/tests/fm-watch-triage.test.sh +++ b/tests/fm-watch-triage.test.sh @@ -523,15 +523,18 @@ test_stale_terminal_status_overridden_by_active_run() { [ ! -e "$state/.hb-surfaced-validating" ] || fail "an absorbed wake must not mark the status line as surfaced" reap "$pid" - # Phase B: backdate the idle timer past the threshold; the run genuinely - # wedges and the next poll escalates exactly like the non-terminal case. + # Phase B: backdate the idle timer past the long-call allowance. The crew still + # READS as working (a frozen pipeline keeps reporting its run step), so only the + # ceiling backstop can surface it - and it must, or a wedged validation would + # never be reported at all. echo $(( $(date +%s) - 500 )) > "$state/.stale-since-$key" : > "$out" PATH="$fakebin:$PATH" FM_FAKE_TMUX_WINDOW="$window" FM_FAKE_TMUX_CAPTURE="$capture_file" \ - FM_STATE_OVERRIDE="$state" FM_CREW_STATE_BIN="$fakebin/fm-crew-state.sh" FM_STALE_ESCALATE_SECS=240 FM_POLL=1 FM_SIGNAL_GRACE=1 \ + FM_STATE_OVERRIDE="$state" FM_CREW_STATE_BIN="$fakebin/fm-crew-state.sh" FM_STALE_ESCALATE_SECS=240 \ + FM_WEDGE_WORKING_ESCALATE_SECS=300 FM_POLL=1 FM_SIGNAL_GRACE=1 \ FM_CHECK_INTERVAL=999999 FM_HEARTBEAT=999999 "$WATCH" > "$out" & pid=$! - wait_for_exit "$pid" 40 || fail "watcher did not escalate an overridden stale terminal status past the threshold" + wait_for_exit "$pid" 40 || fail "watcher did not escalate an overridden stale terminal status past the long-call allowance" grep -F "stale: $window" "$out" >/dev/null || fail "escalation did not print a stale wake" grep -F "possible wedge" "$out" >/dev/null || fail "escalation did not flag a possible wedge" unset FM_FAKE_CREW_STATE @@ -575,21 +578,24 @@ test_nonterminal_stale_provably_working_absorbed_then_escalated() { [ -s "$state/.stale-since-$key" ] || fail "stale-since escalation timer was not recorded on absorb" reap "$pid" - # Phase B: backdate the idle timer past the threshold; the next run escalates. - # (The subsequent-sight timer path does not re-read the crew state.) + # Phase B: backdate the idle timer past the threshold, and let the run end while + # the pane stays frozen at the same hash - the crew is now genuinely stopped, so + # the threshold re-verification must escalate it instead of deferring again. echo $(( $(date +%s) - 500 )) > "$state/.stale-since-$key" + export FM_FAKE_CREW_STATE='state: unknown · source: none · no current-state source available' : > "$out" PATH="$fakebin:$PATH" FM_FAKE_TMUX_WINDOW="$window" FM_FAKE_TMUX_CAPTURE="$capture_file" \ FM_STATE_OVERRIDE="$state" FM_CREW_STATE_BIN="$fakebin/fm-crew-state.sh" FM_STALE_ESCALATE_SECS=240 FM_POLL=1 FM_SIGNAL_GRACE=1 \ FM_CHECK_INTERVAL=999999 FM_HEARTBEAT=999999 "$WATCH" > "$out" & pid=$! - wait_for_exit "$pid" 40 || fail "watcher did not escalate a provably-working non-terminal stale past the threshold" + wait_for_exit "$pid" 40 || fail "watcher did not escalate a stopped crew whose stale timer passed the threshold" grep -F "stale: $window" "$out" >/dev/null || fail "escalation did not print a stale wake" grep -F "possible wedge" "$out" >/dev/null || fail "escalation did not flag a possible wedge" [ ! -e "$state/.stale-since-$key" ] || fail "stale-since timer was not cleared after escalation" + [ ! -e "$state/.wedge-verified-$key" ] || fail "wedge verification marker was not cleared after escalation" FM_STATE_OVERRIDE="$state" "$DRAIN" > "$drain_out" 2>/dev/null || fail "drain after the wedge escalation failed" grep "$(printf '\tstale\t')" "$drain_out" | grep -F "$window" >/dev/null || fail "wedge escalation was not queued" - pass "provably-working non-terminal stale is absorbed on first sight, then wedge-escalated past the threshold" + pass "provably-working non-terminal stale is absorbed on first sight, then wedge-escalated once the crew stops" } # --- non-terminal stale, crew NOT provably working: surfaced immediately ------ @@ -1004,16 +1010,99 @@ test_paused_authoritative_working_preserves_wedge_timer() { echo $(( $(date +%s) - 500 )) > "$state/.stale-since-$key" : > "$out" PATH="$fakebin:$PATH" FM_FAKE_TMUX_WINDOW="$window" FM_FAKE_TMUX_CAPTURE="$capture_file" \ - FM_STATE_OVERRIDE="$state" FM_CREW_STATE_BIN="$fakebin/fm-crew-state.sh" FM_STALE_ESCALATE_SECS=240 FM_POLL=1 FM_SIGNAL_GRACE=1 \ + FM_STATE_OVERRIDE="$state" FM_CREW_STATE_BIN="$fakebin/fm-crew-state.sh" FM_STALE_ESCALATE_SECS=240 \ + FM_WEDGE_WORKING_ESCALATE_SECS=300 FM_POLL=1 FM_SIGNAL_GRACE=1 \ FM_CHECK_INTERVAL=999999 FM_HEARTBEAT=999999 "$WATCH" > "$out" & pid=$! - wait_for_exit "$pid" 40 || fail "authoritative working state did not wedge-escalate past the threshold" + wait_for_exit "$pid" 40 || fail "authoritative working state did not wedge-escalate past the long-call allowance" grep -F "possible wedge" "$out" >/dev/null || fail "authoritative working wedge escalation omitted its reason" [ ! -e "$state/.stale-since-$key" ] || fail "wedge timer remained after authoritative working escalation" unset FM_FAKE_CREW_STATE pass "a paused status overridden by authoritative working preserves its wedge timer and escalates" } +# --- wedge_escalation_deferred (the long-call allowance) --------------------- +# Reaching the wedge threshold is not proof of a wedge: a crew blocked on a long +# in-contract foreground call (a no-mistakes gate call runs against its own +# multi-thousand-second allowance) sits on a frozen pane for far longer than the +# 240s threshold, which produced repeated possible-wedge alarms against healthy +# workers on every pipeline gate (2026-07-30). The predicate defers such an +# escalation while the crew keeps re-verifying as working, caches that verdict for +# one recheck window, and gives up at the allowance so a real wedge still surfaces. +test_wedge_escalation_deferred_classifier() { + local dir state rc + dir=$(make_case classify-wedge-defer); state="$dir/state" + export FM_CREW_STATE_BIN="$dir/fakebin/fm-crew-state.sh" + + FM_FAKE_CREW_STATE='state: working · source: run-step · validating (running)' + export FM_FAKE_CREW_STATE + wedge_escalation_deferred t 500 999999 240; rc=$? + [ "$rc" = 0 ] || fail "a working crew inside the allowance was not deferred after a fresh verification" + wedge_escalation_deferred t 500 10 240; rc=$? + [ "$rc" = 2 ] || fail "a still-fresh verification did not use the cached deferral" + wedge_escalation_deferred t 4000 999999 240; rc=$? + [ "$rc" = 1 ] || fail "a working crew past the allowance was not escalated" + FM_WEDGE_WORKING_ESCALATE_SECS=0 wedge_escalation_deferred t 500 999999 240; rc=$? + [ "$rc" = 1 ] || fail "the allowance could not be disabled" + + FM_FAKE_CREW_STATE='state: unknown · source: none · no current-state source available' + wedge_escalation_deferred t 500 999999 240; rc=$? + [ "$rc" = 1 ] || fail "a crew that is no longer working was deferred" + wedge_escalation_deferred t 500 10 240; rc=$? + [ "$rc" = 2 ] || fail "the cached window is not honoured regardless of the current verdict" + unset FM_FAKE_CREW_STATE FM_CREW_STATE_BIN + pass "wedge_escalation_deferred: defers a working crew inside the allowance, escalates past it or once it stops" +} + +# --- a live in-contract pipeline call is NOT alarmed every threshold window --- +# The 2026-07-30 defect: three possible-wedge escalations in one day against +# healthy workers, one of them 916s into a no-mistakes gate call with a 3000s +# allowance of its own. The pane is legitimately frozen for the whole call, so the +# wedge timer fired every ~4 minutes and the alarm became noise. Repeated polls +# past the threshold must now absorb while the crew keeps verifying as working, +# with at most one crew read per threshold window. +test_in_contract_pipeline_call_is_not_wedge_escalated() { + local dir state fakebin out capture_file window key pane_hash sig pid marker n + dir=$(make_case in-contract-no-wedge); state="$dir/state"; fakebin="$dir/fakebin" + out="$dir/watch.out"; capture_file="$dir/pane.txt" + window="test:fm-gatecall" + printf 'no-mistakes axi run: awaiting the review gate' > "$capture_file" + printf 'window=%s\nkind=ship\n' "$window" > "$state/gatecall.meta" + printf 'working: implementation committed, validating\n' > "$state/gatecall.status" + sig=$(seen_sig "$state/gatecall.status"); printf '%s' "$sig" > "$state/.seen-gatecall_status" + key=$(printf '%s' "$window" | tr ':/.' '___') + pane_hash=$(hash_text "no-mistakes axi run: awaiting the review gate") + printf '%s' "$pane_hash" > "$state/.hash-$key" + printf '1\n' > "$state/.count-$key" + printf '%s' "$pane_hash" > "$state/.stale-$key" + # 916s into the gate call, exactly the observed healthy case. + echo $(( $(date +%s) - 916 )) > "$state/.stale-since-$key" + export FM_FAKE_CREW_STATE='state: working · source: run-step · validating (running)' + + n=1 + while [ "$n" -le 2 ]; do + : > "$out" + PATH="$fakebin:$PATH" FM_FAKE_TMUX_WINDOW="$window" FM_FAKE_TMUX_CAPTURE="$capture_file" \ + FM_STATE_OVERRIDE="$state" FM_CREW_STATE_BIN="$fakebin/fm-crew-state.sh" FM_STALE_ESCALATE_SECS=240 FM_POLL=1 FM_SIGNAL_GRACE=1 \ + FM_CHECK_INTERVAL=999999 FM_HEARTBEAT=999999 "$WATCH" > "$out" & + pid=$! + if ! wait_live "$pid" 30; then + reap "$pid"; fail "round $n wedge-escalated a live in-contract pipeline call: $(cat "$out")" + fi + [ ! -s "$out" ] || { reap "$pid"; fail "round $n printed a wake reason for a live in-contract call: $(cat "$out")"; } + [ ! -s "$state/.wake-queue" ] || { reap "$pid"; fail "round $n queued a wake for a live in-contract call"; } + reap "$pid" + n=$((n + 1)) + done + marker="$state/.wedge-verified-$key" + [ -e "$marker" ] || fail "the deferral did not record its crew verification" + [ "$(cat "$state/.wedge-escalations-$key" 2>/dev/null || echo 0)" = 0 ] || fail "a deferred wedge still counted an escalation" + [ -s "$state/.stale-since-$key" ] || fail "the deferral discarded the idle timer, losing the allowance clock" + grep -F "deferred" "$state/.watch-triage.log" >/dev/null || fail "the deferral was not recorded in the triage log" + unset FM_FAKE_CREW_STATE + pass "a live in-contract pipeline call is absorbed across repeated threshold windows instead of wedge-escalated" +} + # --- consecutive wedge escalations on the same pane demand deep inspection ---- # Root cause of the PR #252 incident's ~20 minutes of unnoticed green: each # wedge escalation fires, gets classified as "still validating" one poll later @@ -1056,12 +1145,13 @@ test_wedge_escalation_marks_demand_deep_inspection_after_threshold() { n=1 while [ "$n" -le 3 ]; do # Backdate the wedge timer past the threshold before each round, mirroring - # the existing wedge-escalation tests' Phase B (the subsequent-sight timer - # path does not re-read the crew state). + # the existing wedge-escalation tests' Phase B. The still-working crew is held + # past its long-call allowance so every round reaches the ceiling backstop. echo $(( $(date +%s) - 500 )) > "$state/.stale-since-$key" : > "$out" PATH="$fakebin:$PATH" FM_FAKE_TMUX_WINDOW="$window" FM_FAKE_TMUX_CAPTURE="$capture_file" \ - FM_STATE_OVERRIDE="$state" FM_CREW_STATE_BIN="$fakebin/fm-crew-state.sh" FM_STALE_ESCALATE_SECS=240 FM_POLL=1 FM_SIGNAL_GRACE=1 \ + FM_STATE_OVERRIDE="$state" FM_CREW_STATE_BIN="$fakebin/fm-crew-state.sh" FM_STALE_ESCALATE_SECS=240 \ + FM_WEDGE_WORKING_ESCALATE_SECS=300 FM_POLL=1 FM_SIGNAL_GRACE=1 \ FM_CHECK_INTERVAL=999999 FM_HEARTBEAT=999999 "$WATCH" > "$out" & pid=$! wait_for_exit "$pid" 40 || fail "watcher did not escalate on consecutive wedge round $n: $(cat "$out")" @@ -1334,6 +1424,7 @@ test_window_is_busy_requires_a_present_agent test_crew_is_provably_working_classifier test_status_is_paused_classifier test_crew_absorb_class_classifier +test_wedge_escalation_deferred_classifier test_signal_crew_provably_working_classifier test_provably_working_signal_absorbed test_turn_ended_provably_working_absorbed @@ -1343,6 +1434,7 @@ test_actionable_signal_surfaced test_terminal_stale_surfaced test_stale_terminal_status_overridden_by_active_run test_nonterminal_stale_provably_working_absorbed_then_escalated +test_in_contract_pipeline_call_is_not_wedge_escalated test_wedge_escalation_marks_demand_deep_inspection_after_threshold test_wedge_escalation_resets_when_pane_becomes_active test_nonterminal_stale_not_working_surfaced