Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 3 additions & 2 deletions .agents/skills/afk/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,9 @@ The single-line format makes submission unambiguous across harnesses; the carrie
### Stale-artifact lifecycle

Treat `state/.subsuper-escalations`, its `.since` sidecar, `state/.subsuper-inject-wedged`, and `state/.subsuper-unknown-acked` as session-scoped delivery artifacts, not as the durable work record.
Always enter through `bin/fm-afk-launch.sh`, which clears prior-session artifacts only for a fresh entry and preserves the current session's buffer on refresh.
Always exit through `bin/fm-afk-launch.sh stop`, which keeps `state/.afk` present through the daemon's shutdown flush, clears it, and archives the posture record last.
The per-task markers `state/.subsuper-stale-<task>`, `.subsuper-reported-stale-<task>`, `.subsuper-dialog-<task>`, `.subsuper-paused-<task>`, and `.subsuper-pause-until-due-<task>` are the same kind of session-scoped episode state: a still-true condition is re-derived fresh by the next session, and a declared wait spanning two away sessions restarts its recheck window at re-entry.
Always enter through `bin/fm-afk-launch.sh`, which clears prior-session artifacts and markers only for a fresh entry and preserves the current session's buffer on refresh.
Always exit through `bin/fm-afk-launch.sh stop`, which keeps `state/.afk` present through the daemon's shutdown flush, clears it with the session's per-task markers, and archives the posture record last.
`docs/herdr-backend.md` "Away-mode supervisor support" and `docs/t3code-backend.md` "Away mode" own the backend mechanisms, and `docs/verification/runtime-backends.md` "Away-mode transport" owns active evidence.

### Reliability properties
Expand Down
24 changes: 20 additions & 4 deletions bin/fm-afk-launch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -633,11 +633,15 @@ fm_afk_launch_restore_backup() { # <backup> <had-afk>
"$FM_AFK_LAUNCH_STATE/.subsuper-escalations.since" \
"$FM_AFK_LAUNCH_STATE/.subsuper-inject-wedged" \
"$FM_AFK_LAUNCH_STATE/.subsuper-unknown-acked" \
"$FM_AFK_LAUNCH_STATE"/.subsuper-reported-stale-* || result=1
"$FM_AFK_LAUNCH_STATE"/.subsuper-stale-* \
"$FM_AFK_LAUNCH_STATE"/.subsuper-reported-stale-* \
"$FM_AFK_LAUNCH_STATE"/.subsuper-dialog-* \
"$FM_AFK_LAUNCH_STATE"/.subsuper-paused-* \
"$FM_AFK_LAUNCH_STATE"/.subsuper-pause-until-due-* || result=1
if [ "$had_afk" -eq 1 ]; then
cp "$backup/.afk" "$FM_AFK_LAUNCH_STATE/.afk" || result=1
fi
for artifact in .subsuper-escalations .subsuper-escalations.since .subsuper-inject-wedged .subsuper-unknown-acked "$backup"/.subsuper-reported-stale-*; do
for artifact in .subsuper-escalations .subsuper-escalations.since .subsuper-inject-wedged .subsuper-unknown-acked "$backup"/.subsuper-stale-* "$backup"/.subsuper-reported-stale-* "$backup"/.subsuper-dialog-* "$backup"/.subsuper-paused-* "$backup"/.subsuper-pause-until-due-*; do
artifact=${artifact##*/}
if [ -e "$backup/$artifact" ]; then
cp -p "$backup/$artifact" "$FM_AFK_LAUNCH_STATE/$artifact" || result=1
Expand Down Expand Up @@ -799,7 +803,7 @@ fm_afk_launch_start() {
had_afk=1
cp "$FM_AFK_LAUNCH_STATE/.afk" "$backup/.afk" || { rm -rf "$backup"; return 1; }
fi
for artifact in .subsuper-escalations .subsuper-escalations.since .subsuper-inject-wedged .subsuper-unknown-acked "$FM_AFK_LAUNCH_STATE"/.subsuper-reported-stale-*; do
for artifact in .subsuper-escalations .subsuper-escalations.since .subsuper-inject-wedged .subsuper-unknown-acked "$FM_AFK_LAUNCH_STATE"/.subsuper-stale-* "$FM_AFK_LAUNCH_STATE"/.subsuper-reported-stale-* "$FM_AFK_LAUNCH_STATE"/.subsuper-dialog-* "$FM_AFK_LAUNCH_STATE"/.subsuper-paused-* "$FM_AFK_LAUNCH_STATE"/.subsuper-pause-until-due-*; do
artifact=${artifact##*/}
if [ -e "$FM_AFK_LAUNCH_STATE/$artifact" ]; then
cp -p "$FM_AFK_LAUNCH_STATE/$artifact" "$backup/$artifact" || { rm -rf "$backup"; return 1; }
Expand Down Expand Up @@ -858,7 +862,7 @@ fm_afk_launch_start_native() {
had_afk=1
cp "$FM_AFK_LAUNCH_STATE/.afk" "$backup/.afk" || { rm -rf "$backup"; return 1; }
fi
for artifact in .subsuper-escalations .subsuper-escalations.since .subsuper-inject-wedged .subsuper-unknown-acked "$FM_AFK_LAUNCH_STATE"/.subsuper-reported-stale-*; do
for artifact in .subsuper-escalations .subsuper-escalations.since .subsuper-inject-wedged .subsuper-unknown-acked "$FM_AFK_LAUNCH_STATE"/.subsuper-stale-* "$FM_AFK_LAUNCH_STATE"/.subsuper-reported-stale-* "$FM_AFK_LAUNCH_STATE"/.subsuper-dialog-* "$FM_AFK_LAUNCH_STATE"/.subsuper-paused-* "$FM_AFK_LAUNCH_STATE"/.subsuper-pause-until-due-*; do
artifact=${artifact##*/}
if [ -e "$FM_AFK_LAUNCH_STATE/$artifact" ]; then
cp -p "$FM_AFK_LAUNCH_STATE/$artifact" "$backup/$artifact" || { rm -rf "$backup"; return 1; }
Expand Down Expand Up @@ -937,6 +941,18 @@ fm_afk_launch_stop() {
fm_afk_launch_log "failed to clear away-mode flag"
result=1
fi
# The session's per-task supervision markers are episode state that ends with
# it: left behind, the NEXT session's first housekeeping reads the stale/pause
# clocks as its own and reports a possible wedge whose duration never
# happened. A wait that still stands is re-declared and re-windowed fresh.
if ! rm -f "$FM_AFK_LAUNCH_STATE"/.subsuper-stale-* \
"$FM_AFK_LAUNCH_STATE"/.subsuper-reported-stale-* \
"$FM_AFK_LAUNCH_STATE"/.subsuper-dialog-* \
"$FM_AFK_LAUNCH_STATE"/.subsuper-paused-* \
"$FM_AFK_LAUNCH_STATE"/.subsuper-pause-until-due-*; then
fm_afk_launch_log "failed to clear the session's per-task supervision markers"
result=1
fi
if [ "$result" -eq 0 ] && fm_afk_contract_present "$FM_AFK_LAUNCH_STATE"; then
if archived=$("$FM_AFK_CONTRACT_CMD" archive); then
fm_afk_launch_log "away-posture record archived at $archived"
Expand Down
46 changes: 14 additions & 32 deletions bin/fm-afk-start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# FM_AFK_STATE_PREPARED=1, checks state/.supervise-daemon.lock, and:
# - prints "afk: daemon already running pid=<pid>" then exits 0 when that
# lock is held by a live daemon (a REFRESH: no stale-artifact clear);
# - otherwise clears any prior away session's stale escalation artifacts
# - otherwise clears prior-session delivery caches and per-task markers
# (fm_afk_clear_stale_artifacts) for a direct, non-prepared start, then
# execs bin/fm-supervise-daemon.sh in the foreground. A prepared start was
# already cleared transactionally by bin/fm-afk-launch.sh.
Expand Down Expand Up @@ -46,39 +46,22 @@ fm_afk_start_usage() {
sed -n '2,14p' "${BASH_SOURCE[0]}" | sed 's/^# \{0,1\}//'
}

# fm_afk_clear_stale_artifacts: on a FRESH away-session entry (the daemon is not
# already running), drop the previous away session's leftover escalation-delivery
# artifacts so they cannot surface as stale escalations under the new session.
# These are session-scoped by timing: a fresh entry owns a new supervision
# session and the new daemon has not produced anything yet, so anything present
# here belongs to a PRIOR session. This never drops a genuinely-pending
# escalation - the delivery buffer is a transient cache, and any condition still
# true (a crew still blocked, a check still firing) is re-derived and re-escalated
# fresh by the daemon's heartbeat catch-all scan and the durable
# state/.wake-queue replay (see docs/herdr-backend.md "Away-mode stale-artifact
# lifecycle" and bin/fm-supervise-daemon.sh's escalate_add/inject_wedge_alarm).
# NOT called on a refresh (daemon already alive), so the current session's own
# buffered escalations are preserved.
# Call only before a fresh daemon can produce state, never on a live refresh:
# timing is what proves these caches and episode clocks belong to a prior session.
# Durable wake-queue rows and classification positions must remain intact for
# replay and catch-all reconciliation by bin/fm-supervise-daemon.sh.
# .agents/skills/afk/SKILL.md "Stale-artifact lifecycle" owns the session contract.
fm_afk_clear_stale_artifacts() { # <state-dir>
local state=$1 line win key
# Unknown-state warnings suppress repeats even after delivery. Re-arm only
# warnings still in the buffer being discarded, preserving delivered ones.
if [ -s "$state/.subsuper-escalations" ] && compgen -G "$state/.subsuper-reported-stale-*" >/dev/null; then
_fm_wake_require_classify || return 1
while IFS= read -r line || [ -n "$line" ]; do
case "$line" in
stale\ persisted\ *s\ \(possible\ wedge\):\ *)
win=${line#* (possible wedge): }
key=$(window_to_task "$win" "$state" | tr ':/.' '___')
rm -f "$state/.subsuper-reported-stale-$key" || return 1
;;
esac
done < "$state/.subsuper-escalations"
fi
local state=$1
rm -f "$state/.subsuper-escalations" \
"$state/.subsuper-escalations.since" \
"$state/.subsuper-inject-wedged" \
"$state/.subsuper-unknown-acked" 2>/dev/null
"$state/.subsuper-unknown-acked" \
"$state"/.subsuper-stale-* \
"$state"/.subsuper-reported-stale-* \
"$state"/.subsuper-dialog-* \
"$state"/.subsuper-paused-* \
"$state"/.subsuper-pause-until-due-* 2>/dev/null
}

daemon_lock_owner() {
Expand Down Expand Up @@ -182,8 +165,7 @@ fm_afk_start_main() {
fm_lock_remove_path "$FM_AFK_LOCK" 2>/dev/null || true
fi

# Fresh start: clear the previous away session's stale delivery artifacts
# before the new daemon can surface them (fix for the leaked-artifact defect).
# Clear prior-session state before the new daemon can observe or produce it.
if [ "${FM_AFK_STATE_PREPARED:-0}" != 1 ]; then
fm_afk_clear_stale_artifacts "$FM_AFK_STATE"
fi
Expand Down
6 changes: 3 additions & 3 deletions docs/t3code-backend.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,9 @@ Resolve ambiguity by explicitly setting both `FM_SUPERVISOR_BACKEND=t3code` and
Injection is a `t3_thread_send` only into a settled supervisor thread, one the [thread status table](#restart-and-liveness-behavior) reads idle/alive with no pending request; any other row, including `blocked` and an unreadable thread, defers the digest exactly as a busy pane does on every other backend.
An unknown native busy verdict defers new-turn delivery but does not prove that work resumed.
Away housekeeping keeps an overdue stale alert pending and buffers one possible-wedge report while the verdict remains unknown, including a failed run or a transient thread-read failure.
Delivered warnings stay suppressed while the same unknown condition persists.
If a fresh daemon entry discards a queued, undelivered warning, it clears that warning's reported marker and preserves its stale timer, so the next housekeeping pass queues exactly one replacement.
Refreshing a live daemon preserves the queue; a failed start restores the previous queue and reported markers.
Within the same away session, delivered warnings stay suppressed while the same unknown condition persists.
The [stale-artifact lifecycle](../.agents/skills/afk/SKILL.md#stale-artifact-lifecycle) owns session-boundary cleanup and recheck-window resets.
Refreshing a live daemon preserves the queue and episode markers; a failed start restores the previous queue and episode markers.
Confirmed busy activity, a missing thread, or an idle verdict after the unknown condition was reported clears the stale and reported markers without another report, so a later unknown condition can report again.
Declared external-wait rechecks also survive native uncertainty; the worker's declaration still controls their cadence.
`stale_window_is_busy` and `housekeeping` in `bin/fm-supervise-daemon.sh` own stale tracking; `fm_afk_clear_stale_artifacts` in `bin/fm-afk-start.sh` and the launcher in `bin/fm-afk-launch.sh` own queue discard and startup rollback.
Expand Down
113 changes: 112 additions & 1 deletion tests/fm-afk-launch.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,8 @@ unit_stop_archives_the_record_last() {
}

# ---------------------------------------------------------------------------
# UNIT 1: fm_afk_clear_stale_artifacts removes exactly the four stale artifacts.
# UNIT 1: fm_afk_clear_stale_artifacts removes exactly the four delivery
# artifacts plus the prior session's five per-task marker families.
# ---------------------------------------------------------------------------
unit_clear_stale() {
local st
Expand All @@ -266,6 +267,12 @@ unit_clear_stale() {
: > "$st/state/.subsuper-escalations.since"
: > "$st/state/.subsuper-inject-wedged"
: > "$st/state/.subsuper-unknown-acked"
: > "$st/state/.subsuper-stale-task1"
: > "$st/state/.subsuper-reported-stale-task1"
: > "$st/state/.subsuper-dialog-task1"
: > "$st/state/.subsuper-paused-task1"
: > "$st/state/.subsuper-pause-until-due-task1"
: > "$st/state/.subsuper-seen-status-task1" # classification position must be kept
: > "$st/state/.wake-queue" # durable queue must be untouched
# Source fm-afk-start.sh inside a child bash (it sets `set -eu` and would
# otherwise leak that into this test shell) and call the clear helper.
Expand All @@ -279,6 +286,20 @@ unit_clear_stale() {
else
fail "clear-stale: stale artifacts survived"
fi
if ! compgen -G "$st/state/.subsuper-stale-*" >/dev/null \
&& ! compgen -G "$st/state/.subsuper-reported-stale-*" >/dev/null \
&& ! compgen -G "$st/state/.subsuper-dialog-*" >/dev/null \
&& ! compgen -G "$st/state/.subsuper-paused-*" >/dev/null \
&& ! compgen -G "$st/state/.subsuper-pause-until-due-*" >/dev/null; then
pass "clear-stale: removes the prior session's five per-task marker families"
else
fail "clear-stale: per-task session markers survived"
fi
if [ -e "$st/state/.subsuper-seen-status-task1" ]; then
pass "clear-stale: keeps the durable classification position"
else
fail "clear-stale: removed the classification position"
fi
if [ -e "$st/state/.wake-queue" ]; then
pass "clear-stale: leaves the durable wake-queue intact (no pending work dropped)"
else
Expand All @@ -287,6 +308,95 @@ unit_clear_stale() {
rm -rf "$st"
}

# ---------------------------------------------------------------------------
# The per-task away markers are session-scoped episode state: the return path
# (fm_afk_launch_stop) and the fresh-entry cleanup must both drop all five
# families, or the next session's first housekeeping reads a dead session's
# idle clock as its own and reports a wedge whose duration never happened.
# The last pass replays the implicit-state-machines audit's reproduction: a
# hand-written prior-session clock, an idle pane read, delivery stubbed off.
# ---------------------------------------------------------------------------
unit_per_task_markers_session_scoped() {
local st state now
st=$(mktemp -d "${TMPDIR:-/tmp}/fm-afk-marker-scope.XXXXXX")
state="$st/state"
mkdir -p "$state"
now=$(date +%s)
# One finished ship task, as the audit's scratch home: a terminal status and
# a meta that still maps its window.
printf 'done [at=%s]: PR https://example.test/o/r/pull/1 checks green\n' "$now" > "$state/demo.status"
printf 'window=%s\nbackend=tmux\nharness=claude\n' 'fmaudit-none:fm-demo' > "$state/demo.meta"
printf '0:fixture\n' > "$state/.subsuper-seen-status-demo"
printf '%s\n' "$now" > "$state/.subsuper-last-scan"
seed_markers() {
# Same path and content as the daemon's stale_marker_record: the epoch the
# pane was first seen idle, mid-episode when the session ended.
printf '%s\n' $(( now - 172801 )) > "$state/.subsuper-stale-demo"
: > "$state/.subsuper-reported-stale-demo"
: > "$state/.subsuper-dialog-demo"
: > "$state/.subsuper-paused-demo"
: > "$state/.subsuper-pause-until-due-demo"
}
markers_gone() {
! compgen -G "$state/.subsuper-stale-*" >/dev/null \
&& ! compgen -G "$state/.subsuper-reported-stale-*" >/dev/null \
&& ! compgen -G "$state/.subsuper-dialog-*" >/dev/null \
&& ! compgen -G "$state/.subsuper-paused-*" >/dev/null \
&& ! compgen -G "$state/.subsuper-pause-until-due-*" >/dev/null
}

# The return path drops the session's markers with the away flag.
seed_markers
: > "$state/.afk"
FM_HOME="$st" FM_STATE_OVERRIDE="$state" "$LAUNCH" stop >/dev/null 2>&1
if markers_gone && [ ! -e "$state/.afk" ] && [ -e "$state/.subsuper-seen-status-demo" ]; then
pass "return: stop drops all five per-task marker families, keeps the classification position"
else
fail "return: per-task markers survived stop ($(compgen -G "$state/.subsuper-*" | tr '\n' ' '))"
fi

# The fresh-entry cleanup drops them alongside the delivery artifacts.
seed_markers
FM_HOME="$st" FM_STATE_OVERRIDE="$state" \
bash -c '. "$1"; fm_afk_clear_stale_artifacts "$2"' _ "$START" "$state"
if markers_gone; then
pass "entry: fresh-session cleanup drops all five per-task marker families"
else
fail "entry: per-task markers survived fresh-session cleanup ($(compgen -G "$state/.subsuper-*" | tr '\n' ' '))"
Comment on lines +358 to +365

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Marker preservation lacks test coverage

The new test checks that all five marker families are deleted, but not that a live refresh preserves them or a failed launch restores their contents. Existing refresh and rollback tests check delivery artifacts; the updated native test restores only a reported-stale marker.

Seed distinct contents for all five families and assert they survive both paths. Otherwise, an accidental cleanup or missing backup entry could pass these tests.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

fi

# The audit narrative end to end: session one's clock goes through stop and
# the next entry's cleanup, then the new daemon's first housekeeping pass.
# Reset to the audit's repro shape first: ONLY the stale clock, because
# housekeeping's pause migration retires the episode before the wedge check
# whenever a paused marker is also present.
rm -f "$state"/.subsuper-stale-* "$state"/.subsuper-reported-stale-* \
"$state"/.subsuper-dialog-* "$state"/.subsuper-paused-* \
"$state"/.subsuper-pause-until-due-*
printf '%s\n' $(( now - 172801 )) > "$state/.subsuper-stale-demo"
: > "$state/.afk"
FM_HOME="$st" FM_STATE_OVERRIDE="$state" "$LAUNCH" stop >/dev/null 2>&1
: > "$state/.afk" # a fresh entry, two days later in the audit's telling
printf '%s\n' "$(date +%s)" > "$state/.subsuper-last-scan"
FM_HOME="$st" FM_STATE_OVERRIDE="$state" \
bash -c '. "$1"; fm_afk_clear_stale_artifacts "$2"' _ "$START" "$state"
FM_HOME="$st" FM_STATE_OVERRIDE="$state" FM_DAEMON_PRIMARY_HARNESS=claude \
bash -c '
. "$1"
stale_window_is_busy() { return 1; }
escalate_flush() { return 0; }
inject_msg() { return 0; }
inject_wedge_alarm() { return 0; }
housekeeping "$2"
' _ "$ROOT/bin/fm-supervise-daemon.sh" "$state"
if ! grep -q 'possible wedge' "$state/.subsuper-escalations" 2>/dev/null; then
pass "session scope: a dead session's idle clock cannot report 'possible wedge' in the next session"
else
fail "session scope: first housekeeping reported a wedge that never happened ($(cat "$state/.subsuper-escalations"))"
fi
rm -rf "$st"
}

unit_relative_paths_are_absolute_before_daemon_launch() {
local root home state out status linked_home
root=$(mktemp -d "${TMPDIR:-/tmp}/fm-afk-relative-home.XXXXXX")
Expand Down Expand Up @@ -1787,6 +1897,7 @@ e2e_rex() {
}

unit_clear_stale
unit_per_task_markers_session_scoped
unit_enter_records_the_posture_in_one_step_without_a_daemon
unit_retired_two_step_entry_is_refused
unit_pi_never_launches_the_daemon
Expand Down
Loading
Loading