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
41 changes: 37 additions & 4 deletions bin/fm-crew-state.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
# Output is one stable, parseable, token-tight line firstmate can read every
# heartbeat:
#
# state: <working|parked|done|blocked|paused|failed|unknown> · source: <run-step|pane|status-log|remote-endpoint|none> · <detail>
# state: <working|parked|done|blocked|paused|failed|stopped|unknown> · source: <run-step|pane|status-log|remote-endpoint|endpoint|none> · <detail>
#
# Logic, in order:
# 1. Resolve worktree + backend target + kind from state/<id>.meta. A meta
Expand Down Expand Up @@ -132,11 +132,25 @@
# backend's pane busy state, then the resolved status declaration
# when its verb maps to a recognized run-state. Decision-only events such as
# `resolved` never become current state or detail.
# stopped is the one state that is positive evidence of ABSENCE: the
# recorded tmux or herdr endpoint exists and the backend's recovery-grade
# classifier (fm_backend_agent_state) says dead - no agent process, only a
# shell - while the semantic busy source could not answer (an adapter with no
# verified source, such as Codex, or a missing or mismatched record), or the
# run-step read is unknown only because the selected run is TERMINAL and its
# code identity cannot be verified here. It says the agent is not running; it
# does not say the work is finished, delivered or accepted, that no other
# process runs on the host or in the no-mistakes daemon, or that the
# unverified run belongs to this worktree (its run ids stay in the detail).
# A live or competing run, an unreachable endpoint and a running agent keep
# the unknown or working readings above.
# 5. Missing meta or torn-down worktree: report unknown · none. If no run is
# attributed to this crew, a dead endpoint also reports unknown · none rather
# than trusting a stale status log. On tmux and herdr, which own a
# recovery-grade classifier, only its positive death evidence reads as gone
# (the endpoint is authoritatively absent, or its pane holds no agent); an
# (the endpoint is authoritatively absent, or its pane holds no agent); a
# pane that holds no agent (dead) reads stopped · endpoint, as in step 4, and
# only an authoritatively absent endpoint reads unknown · none. An
# endpoint that merely failed to answer reports unknown · none as
# unreachable, and an alive endpoint whose scrollback read failed is still
# classified by step 4. Backends with no classifier keep reading a failed
Expand Down Expand Up @@ -308,6 +322,17 @@ crew_busy_verdict() { # <target>
fm_busy_classify "$TASK_BACKEND" "$1" "$HARNESS" "$ID" "$STATE" "$tail40"
}

# endpoint_agent_stopped: 0 only on POSITIVE evidence that the recorded endpoint
# exists with no agent process in it (fm_backend_agent_state dead, which is
# independent of any harness busy source). tmux and herdr own that classifier;
# every other backend and every non-dead verdict (alive, missing, ambiguous,
# unreadable) is not evidence of a stop.
endpoint_agent_stopped() {
case "$TASK_BACKEND" in tmux|herdr) ;; *) return 1 ;; esac
[ -n "$BACKEND_TARGET" ] || return 1
[ "$(fm_backend_agent_state "$TASK_BACKEND" "$BACKEND_TARGET")" = dead ]
}

# --- no-mistakes run lookup (authoritative when a run matches this branch) --
# trim, strip_quotes, the bounded nm_run call, nm_field's TOON parse, and the
# attribution helpers below are thin wrappers over the ONE owner in
Expand Down Expand Up @@ -902,6 +927,9 @@ if [ "$KIND" = ship ] && [ -n "$CREW_BRANCH" ] && command -v no-mistakes >/dev/n
RUN_DEAD_DAEMON="no-mistakes daemon unreachable; last run record $(strip_quotes "$(nm_field status)") - unverified"
fi
else
if ! fm_nm_run_is_active "$RUN_OUT" && endpoint_agent_stopped; then
emit stopped endpoint "agent process absent, endpoint shell retained; terminal run code identity unverified; run ids: $candidate_ids"
fi
emit unknown run-step "selected run code identity unverified; run ids: $candidate_ids"
fi
fi
Expand Down Expand Up @@ -1183,7 +1211,7 @@ if ! pane_readable "$BACKEND_TARGET"; then
emit unknown none "backend target gone: $BACKEND_TARGET"
;;
tmux:dead|herdr:dead)
emit unknown none "backend target gone: $BACKEND_TARGET (agent gone, pane shell remains)"
emit stopped endpoint "agent process absent, endpoint shell retained (${HARNESS:-unknown harness}; pane unreadable)"
;;
tmux:*|herdr:*)
emit unknown none "backend unreachable ($TASK_BACKEND endpoint state: $AGENT_STATE)"
Expand All @@ -1204,7 +1232,12 @@ if [ "$KIND" != secondmate ]; then
case "${BUSY_VERDICT%% *}" in
busy) emit working pane "harness busy (${BUSY_VERDICT#* })" ;;
idle) ;;
*) emit unknown pane "harness state unavailable ($BUSY_VERDICT)" ;;
*)
if endpoint_agent_stopped; then
emit stopped endpoint "agent process absent, endpoint shell retained (${HARNESS:-unknown harness}; busy source: $BUSY_VERDICT)"
fi
emit unknown pane "harness state unavailable ($BUSY_VERDICT)"
;;
esac
fi

Expand Down
10 changes: 9 additions & 1 deletion bin/fm-fleet-snapshot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,14 @@ metadata still pair, so the home stays valid, and the child is listed in
retained_children (never in active_children) with step=completed,
delivery=unconfirmed, and the state source and detail that name where the work
stands. A failed child with an In flight row remains terminal_in_flight.
A child whose agent process is positively absent (current state stopped: the
endpoint exists with no agent, whatever the harness busy source says) is a held,
owned, NOT-active obligation: it appears in holds with its detail and never in
active_children, so a home whose only unreadable children are stopped is valid
and reads externally_held. Stopped means the agent is not running, not that the
work is finished, delivered or accepted. A stopped hold is owned, non-active
work that needs attention: it is not a deliberate standdown, readiness,
completion or restart clearance.
A child whose current state cannot be read (for example a harness with no
verified semantic busy source) stays unknown and invalidates the home as
child_current_unavailable; unavailable_children names each one with the source
Expand Down Expand Up @@ -1086,7 +1094,7 @@ secondmate_home_summary_json() { # <backlog-json-file> <tasks-json-file>
reason:((.hold_reason // .blocked_reason // "blocked") | trunc(120)),source:"backlog"} ]
+ [ $owned_in_flight[] as $work
| $tasks[]
| select(.id == $work.id and (.current_state.state == "parked" or .current_state.state == "paused" or .current_state.state == "blocked"))
| select(.id == $work.id and (.current_state.state == "parked" or .current_state.state == "paused" or .current_state.state == "blocked" or .current_state.state == "stopped"))
| select(($work.hold_reason != null and $work.hold_kind != null) | not)
| {id,title:((.backlog.title // .id) | trunc(90)),blocked_by:null,
blocked_by_ids:[],unresolved_blocker_ids:[],
Expand Down
1 change: 1 addition & 0 deletions docs/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ A run executing on the crew's own branch is current regardless of head, because
During no-mistakes' `ci` monitor phase, it also reads the ci step log tail because `axi status` reports both "still waiting on checks" and "checks green, waiting on merge" as `ci,running`.
The most recent recognized ci log marker wins, so checks-green monitoring reports done while a later re-arm, failed-check, or issue marker returns the crew to working.
`bin/fm-crew-state.sh` owns the evidence guard that recognizes ended CI monitors after green checks, including cancelled runs and skipped rebase steps; a passed run alone never proves a forge merge.
`bin/fm-crew-state.sh` reports `stopped` only on positive absence evidence: a tmux or herdr endpoint whose recovery-grade classifier says no agent process remains while the semantic busy source cannot answer or the pane is unreadable, or whose unverified run is terminal; the script header owns the rule, and `stopped` never means finished, delivered, accepted or that nothing else runs.
In the coarse runs-ledger fallback, which has no steps table and no ci log, a terminal failed record whose daemon an explicit `daemon status` probe proves down reports unknown as unverified instead: an instrument failure must never read as work failure.
The same instrument rule covers the ledger-anchored continuation of a selected run whose head this copy cannot resolve: once the probe answers down, that still-executing record reports unknown as unverified, while a run parked at a gate keeps its gate and findings because an open decision stays open when the instrument dies, and a `needs-decision` or `blocked` event the crew observed first hand stays open with the unverified record named as the reason rather than superseded by it.
Only when no matching run exists does it consult semantic busy state; exact busy reports working, exact idle permits fallback to the log's resolved current declaration - the newest decision the fold still holds open, otherwise the latest recognized event - when its verb maps to a recognized run-state, and unknown or a dead pane stays unknown instead of trusting a stale log.
Expand Down
102 changes: 102 additions & 0 deletions tests/fm-crew-state-stopped.test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#!/usr/bin/env bash
# tests/fm-crew-state-stopped.test.sh - the `stopped` current state of
# bin/fm-crew-state.sh against a REAL tmux server on a private socket.
#
# A worker whose agent process exited leaves its endpoint as a live shell. For an
# adapter with no verified semantic busy source (Codex) the busy classifier can
# only answer unknown, which made every stopped worker unreadable. The backend's
# recovery-grade classifier (fm_backend_agent_state) separately proves the agent
# is absent, so the reader reports stopped - agent not running, nothing more -
# and keeps every other endpoint verdict as it was.
set -u
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
command -v tmux >/dev/null 2>&1 || { echo "skip: tmux not found"; exit 0; }
REAL_TMUX=$(command -v tmux)
SOCKET="fm-crew-stopped-$$"
LAB=$(mktemp -d "${TMPDIR:-/tmp}/fm-crew-stopped.XXXXXX")
cleanup() { "$REAL_TMUX" -L "$SOCKET" kill-server >/dev/null 2>&1 || true; rm -rf "$LAB"; }
trap cleanup EXIT
fail() { printf 'not ok - %s\n' "$1" >&2; exit 1; }
pass() { printf 'ok - %s\n' "$1"; }

mkdir -p "$LAB/shim" "$LAB/state" "$LAB/wt"
cat > "$LAB/shim/tmux" <<SH
#!/usr/bin/env bash
exec "$REAL_TMUX" -L "$SOCKET" "\$@"
SH
chmod +x "$LAB/shim/tmux"
PATH="$LAB/shim:$PATH"

# A live agent stand-in (a long-running foreground program) and a bare shell.
tmux new-session -d -s lab -n running "sleep 300" || fail "session"
tmux new-window -t lab -n shelled "bash --norc --noprofile -i" || fail "shell window"
sleep 1

write_meta() { # <id> <window> <harness> <kind>
cat > "$LAB/state/$1.meta" <<EOM
window=$2
worktree=$LAB/wt
project=$LAB/wt
harness=$3
kind=$4
mode=no-mistakes
EOM
}
read_state() { # <id>
FM_STATE_OVERRIDE="$LAB/state" FM_HOME="$LAB" "$ROOT/bin/fm-crew-state.sh" "$1" 2>&1
}
contains() { case "$1" in *"$2"*) return 0 ;; esac; return 1; }

write_meta codex-stopped lab:shelled codex scout
out=$(read_state codex-stopped)
contains "$out" "state: stopped · source: endpoint" || fail "a Codex worker whose agent exited must read stopped: $out"
contains "$out" "agent process absent, endpoint shell retained" || fail "the stopped reading must name what is absent: $out"
contains "$out" "codex-unverified" || fail "the stopped reading must keep the unverified busy source visible: $out"
pass "a Codex endpoint holding only a shell reads stopped, not unknown"

write_meta codex-running lab:running codex scout
out=$(read_state codex-running)
contains "$out" "state: unknown · source: pane" || fail "a Codex endpoint with a live foreground program must stay unknown: $out"
contains "$out" "codex-unverified" || fail "an unverified running worker keeps its unverified reason: $out"
contains "$out" "stopped" && fail "a running endpoint must never read stopped: $out"
pass "a Codex endpoint with a live foreground program stays unknown"

write_meta codex-gone lab:nothere codex scout
out=$(read_state codex-gone)
contains "$out" "state: stopped" && fail "a missing window is not a stopped agent: $out"
contains "$out" "state: unknown" || fail "a missing window stays unknown: $out"
pass "a missing window never reads stopped"

write_meta claude-shelled lab:shelled claude scout
printf 'paused: waiting on primary\n' > "$LAB/state/claude-shelled.status"
out=$(read_state claude-shelled)
contains "$out" "state: stopped · source: endpoint" || fail "an unarmed Claude worker (busy source missing) with only a shell reads stopped: $out"
contains "$out" "busy source: unknown missing" || fail "the missing busy source stays in the detail: $out"
pass "a harness whose busy source is missing also reads stopped on positive absence evidence"

cat > "$LAB/shim/tmux" <<SH
#!/usr/bin/env bash
[ "\${1:-}" = capture-pane ] && exit 1
exec "$REAL_TMUX" -L "$SOCKET" "\$@"
SH
write_meta codex-unreadable lab:shelled codex scout
out=$(read_state codex-unreadable)
contains "$out" "state: stopped · source: endpoint" || fail "an unreadable pane holding only a shell must read stopped: $out"
write_meta claude-unreadable lab:shelled claude scout
out=$(read_state claude-unreadable)
contains "$out" "state: stopped · source: endpoint" || fail "an unreadable pane holding only a shell reads stopped for any harness: $out"
write_meta codex-unreadable-gone lab:nothere codex scout
out=$(read_state codex-unreadable-gone)
contains "$out" "state: unknown" || fail "an unreadable pane at a missing window stays unknown: $out"
contains "$out" "state: stopped" && fail "a missing window is not stopped: $out"
pass "an unreadable pane reads stopped only on positive agent-absent evidence"
cat > "$LAB/shim/tmux" <<SH
#!/usr/bin/env bash
exec "$REAL_TMUX" -L "$SOCKET" "\$@"
SH

tmux kill-server
out=$(read_state codex-stopped)
contains "$out" "state: stopped" && fail "an unreachable server proves nothing and must not read stopped: $out"
contains "$out" "state: unknown" || fail "an unreachable server stays unknown: $out"
pass "an unreachable server stays unknown, never stopped"
Loading