Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .agents/skills/afk/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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-<id> .drain-retry-{direct,historical}-<id> 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
Expand Down
32 changes: 32 additions & 0 deletions bin/fm-classify-lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,38 @@ crew_is_provably_working() { # <id>
[ "$(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 <recheck-secs> 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() { # <task> <idle-secs> <verified-age-secs> <recheck-secs>
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 <id>'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.
Expand Down
45 changes: 36 additions & 9 deletions bin/fm-supervise-daemon.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -436,7 +441,7 @@ stale_marker_record() { # <window> <state> — create if absent
stale_marker_remove() { # <window> <state>
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-<key> holds the epoch a declared pause was
Expand Down Expand Up @@ -464,7 +469,8 @@ clear_pause_tracking() { # <window> <state>
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() { # <window> <state> <last-status-line>
Expand Down Expand Up @@ -930,14 +936,16 @@ _oldest_line_age() { # <buf> -> 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() { # <state>
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"

Expand Down Expand Up @@ -989,13 +997,32 @@ housekeeping() { # <state>
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

Expand Down
62 changes: 43 additions & 19 deletions bin/fm-watch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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() { # <window> <since-file> <triage-label> <escalation-count-file>
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]*)
Expand All @@ -283,6 +295,16 @@ wedge_timer_check() { # <window> <since-file> <triage-label> <escalation-count-
*)
age=$(( $(date +%s) - since ))
if [ "$age" -ge "$STALE_ESCALATE_SECS" ]; then
vf="$STATE/.wedge-verified-$(printf '%s' "$win" | tr ':/.' '___')"
wedge_escalation_deferred "$(window_to_task "$win" "$STATE")" "$age" \
"$(age_of "$vf")" "$STALE_ESCALATE_SECS"
case "$?" in
0) : > "$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)"
Expand Down Expand Up @@ -312,7 +334,7 @@ handle_paused_stale() { # <window> <task> <hash>
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
Expand Down Expand Up @@ -342,7 +364,8 @@ clear_pause_tracking() { # <window>
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.
Expand Down Expand Up @@ -399,7 +422,7 @@ surface_nonterminal_stale() { # <window> <hash>
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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -975,15 +999,15 @@ 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
fi
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
Expand Down
Loading