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
31 changes: 31 additions & 0 deletions bin/backends/herdr.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2322,6 +2322,37 @@ EOF
printf 'shell'
}

# fm_backend_herdr_pane_owns_session: 0 only when <pid> provably runs as
# <pane_id>'s foreground occupant - the pane's own process-info shows a
# shell_pid that is an ancestor-or-self of <pid> AND <pid>'s process group is
# the pane's foreground process group. This is the herdr equivalent of the
# tmux proof bin/fm-supervision-handoff.sh makes from pane_pid and tpgid: an
# inherited pane id is only ever a hint, and delivery authority comes from the
# process table, never from the pane naming a session. Every read failure,
# malformed field, dead link in the ancestry chain, or disagreement between the
# two records refuses.
fm_backend_herdr_pane_owns_session() { # <session> <pane_id> <pid>
local session=$1 pane_id=$2 pid=$3 info shell_pid fg_pgid ancestor pgid
case "$pid" in ''|*[!0-9]*) return 1 ;; esac
info=$(fm_backend_herdr_cli "$session" pane process-info --pane "$pane_id" 2>/dev/null) || return 1
printf '%s' "$info" | jq -e --arg pane "$pane_id" '
.result.type == "pane_process_info"
and .result.process_info.pane_id == $pane
' >/dev/null 2>&1 || return 1
shell_pid=$(printf '%s' "$info" | jq -er \
'.result.process_info.shell_pid | select(type == "number" and . > 1) | floor' 2>/dev/null) || return 1
fg_pgid=$(printf '%s' "$info" | jq -er \
'.result.process_info.foreground_process_group_id | select(type == "number" and . > 1) | floor' 2>/dev/null) || return 1
ancestor=$pid
while [ "$ancestor" != 1 ] && [ "$ancestor" != "$shell_pid" ]; do
ancestor=$(ps -o ppid= -p "$ancestor" 2>/dev/null | tr -d ' ')
case "$ancestor" in ''|*[!0-9]*) return 1 ;; esac
done
[ "$ancestor" = "$shell_pid" ] || return 1
pgid=$(ps -o pgid= -p "$pid" 2>/dev/null | tr -d ' ') || return 1
[ -n "$pgid" ] && [ "$pgid" = "$fg_pgid" ]
}

# fm_backend_herdr_pane_agent_state: classify <pane_id> in <session> as one of
# dead|no-agent|stale-agent|live|unknown, from the JSON body of two read-only
# calls plus, for a registered agent, the pane's process-level view - never
Expand Down
99 changes: 74 additions & 25 deletions bin/fm-supervision-handoff.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@
# host stops it before taking over the arm recorded in .supervision-host-left.
# Ack and TERM both leave that verified arm for the next park, not a second arm.
#
# Automatic delivery is restricted to the inherited tmux pane on Claude. Its
# pane pid must be an ancestor of the pinned session-lock pid, that session
# must still be in the terminal's foreground group, and its composer must be
# empty with no busy signature. All other endpoints retain the queued wake
# and keep watching but need a normal turn or human message to resume main.
# No terminal text proves acknowledgement. Busy, unknown, or pending input is
# never overwritten. The existing backend submit primitive owns Enter retries.
# Automatic delivery is restricted to the inherited pane on Claude, proven
# through the transport the pane itself declares: TMUX_PANE for tmux, or
# HERDR_ENV+HERDR_PANE_ID+HERDR_SESSION for herdr (a named session's pane,
# including the fm-remote session remote homes run under). On tmux the pane's
# pid must be an ancestor of the pinned session-lock pid and that session must
# still be in the terminal's foreground group; on herdr the pane's own
# process-info must show the same ancestor-or-self and foreground-group facts
# for the pinned pid, and the pane's registered agent identity must be claude.
# In both transports the agent must read idle and the composer must be
# positively empty. All other endpoints retain the queued wake and keep
# watching but need a normal turn or human message to resume main. No terminal
# text proves acknowledgement. Busy, unknown, or pending input is never
# overwritten. The existing backend submit primitive owns Enter retries.
# Removing the opt-in, losing the pinned session, or entering daemon-backed
# away mode stops retries and leaves the current arm for its next owner.
set -u
Expand All @@ -34,6 +40,19 @@ CONFIG="${FM_CONFIG_OVERRIDE:-$FM_HOME/config}"
# shellcheck source=bin/fm-session-lock-lib.sh
. "$SCRIPT_DIR/fm-session-lock-lib.sh"
fm_backend_source tmux || exit 1
# Delivery transport: only the pane identity the pane runtime injected is
# trusted, mirroring bin/fm-supervisor-target-lib.sh's discovery order. An
# explicit target variable is deliberately NOT honored - the handoff must
# never deliver to a pane another home or a stray environment configured.
TRANSPORT=
HERDR_TARGET=
if [ -n "${TMUX_PANE:-}" ]; then
TRANSPORT=tmux
elif [ "${HERDR_ENV:-}" = 1 ] && [ -n "${HERDR_PANE_ID:-}" ] && [ -n "${HERDR_SESSION:-}" ]; then
fm_backend_source herdr || exit 1
TRANSPORT=herdr
HERDR_TARGET="$HERDR_SESSION:$HERDR_PANE_ID"
fi
TOKEN=${1:-}
PREDECESSOR=${2:-}
case "$TOKEN" in ''|*[!A-Za-z0-9._-]*) exit 2 ;; esac
Expand Down Expand Up @@ -82,25 +101,55 @@ session_owned() {

retry_delivery() {
local pane_pid ancestor message verdict=deferred tmp pgid tpgid
local target proven=0 agent_out h_session h_pane
# No fallback target: a guessed pane can belong to another home.
if [ -n "${TMUX_PANE:-}" ] && session_owned; then
pane_pid=$(tmux display-message -p -t "$TMUX_PANE" '#{pane_pid}' 2>/dev/null) || pane_pid=
ancestor=$SESSION
case "$pane_pid" in ''|*[!0-9]*) ancestor= ;; esac
while [ -n "$ancestor" ] && [ "$ancestor" != 1 ] && [ "$ancestor" != "$pane_pid" ]; do
ancestor=$(ps -o ppid= -p "$ancestor" 2>/dev/null | tr -d ' ')
done
read -r pgid tpgid < <(ps -o pgid=,tpgid= -p "$SESSION" 2>/dev/null) || pgid=
if [ -n "$ancestor" ] && [ "$ancestor" = "$pane_pid" ] \
&& [ -n "$pgid" ] && [ "$pgid" = "$tpgid" ] && [ "$pgid" -gt 0 ] 2>/dev/null \
&& fm_harness_pid_alive "$SESSION" \
&& [ "$(fm_pane_busy_state "$TMUX_PANE" claude 2>/dev/null)" = idle ] \
&& [ "$(fm_backend_composer_state tmux "$TMUX_PANE" 2>/dev/null)" = empty ] \
&& session_owned && [ "$(cat "$PENDING" 2>/dev/null)" = "$TOKEN" ]; then
if fm_operational_record_write "$STATE" watcher \
'check: supervision-handoff - the host hand-back is still unacknowledged. Run bin/fm-wake-drain.sh, handle its queued work, and run its exact WAKE_ACK_REQUIRED command. This is automatic supervision, not a captain return.' message; then
verdict=$(fm_backend_send_text_submit tmux "$TMUX_PANE" "$message" 2 0.2 0.2 2>/dev/null) || verdict=unconfirmed
fi
if session_owned; then
case "$TRANSPORT" in
tmux)
target=$TMUX_PANE
pane_pid=$(tmux display-message -p -t "$TMUX_PANE" '#{pane_pid}' 2>/dev/null) || pane_pid=
ancestor=$SESSION
case "$pane_pid" in ''|*[!0-9]*) ancestor= ;; esac
while [ -n "$ancestor" ] && [ "$ancestor" != 1 ] && [ "$ancestor" != "$pane_pid" ]; do
ancestor=$(ps -o ppid= -p "$ancestor" 2>/dev/null | tr -d ' ')
done
read -r pgid tpgid < <(ps -o pgid=,tpgid= -p "$SESSION" 2>/dev/null) || pgid=
if [ -n "$ancestor" ] && [ "$ancestor" = "$pane_pid" ] \
&& [ -n "$pgid" ] && [ "$pgid" = "$tpgid" ] && [ "$pgid" -gt 0 ] 2>/dev/null \
&& fm_harness_pid_alive "$SESSION" \
&& [ "$(fm_pane_busy_state "$TMUX_PANE" claude 2>/dev/null)" = idle ] \
&& [ "$(fm_backend_composer_state tmux "$TMUX_PANE" 2>/dev/null)" = empty ]; then
proven=1
fi
;;
herdr)
target=$HERDR_TARGET
if fm_backend_herdr_parse_target "$target" \
&& [ "$(fm_backend_herdr_server_running_state "$FM_BACKEND_HERDR_SESSION")" = running ]; then
h_session=$FM_BACKEND_HERDR_SESSION
h_pane=$FM_BACKEND_HERDR_PANE
# One read carries both halves of the registered-identity proof: the
# pane's agent must be claude, and any session binding it carries
# must be claude's own - a foreign label or binding never delivers.
agent_out=$(fm_backend_herdr_cli "$h_session" agent get "$h_pane" 2>/dev/null) || agent_out=
if fm_backend_herdr_pane_owns_session "$h_session" "$h_pane" "$SESSION" \
&& [ "$(printf '%s' "$agent_out" | jq -r '.result.agent.agent // empty' 2>/dev/null)" = claude ] \
&& printf '%s' "$agent_out" | jq -er '(.result.agent.agent_session.agent // "claude") == "claude"' >/dev/null 2>&1 \
&& fm_harness_pid_alive "$SESSION" \
&& [ "$(fm_backend_herdr_busy_state "$target" 2>/dev/null)" = idle ] \
&& [ "$(fm_backend_herdr_rendered_busy_state "$target" claude 2>/dev/null)" = idle ] \
&& [ "$(fm_backend_composer_state herdr "$target" 2>/dev/null)" = empty ]; then
proven=1
fi
fi
;;
esac
fi
if [ "$proven" -eq 1 ] \
&& session_owned && [ "$(cat "$PENDING" 2>/dev/null)" = "$TOKEN" ]; then
if fm_operational_record_write "$STATE" watcher \
'check: supervision-handoff - the host hand-back is still unacknowledged. Run bin/fm-wake-drain.sh, handle its queued work, and run its exact WAKE_ACK_REQUIRED command. This is automatic supervision, not a captain return.' message; then
verdict=$(fm_backend_send_text_submit "$TRANSPORT" "$target" "$message" 2 0.2 0.2 2>/dev/null) || verdict=unconfirmed
fi
fi
printf '%s\tretry\t%s\t%s\n' "$(date +%s)" "$TOKEN" "$verdict" >> "$LOG"
Expand Down
14 changes: 8 additions & 6 deletions docs/supervision-host.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,16 +247,17 @@ The helper's header owns the records, retry interval, process cleanup, and trans

The first delivery remains the Stop hook's exit-2 feedback.
A later retry does not depend on that hook or on another Stop firing.
The detached owner writes a durable operational-input record and submits its watcher doorbell through the existing tmux submit primitive.
It uses only the inherited pane, proves that pane's process contains the pinned session owner, and requires an idle agent with an empty composer.
The detached owner writes a durable operational-input record and submits its watcher doorbell through the backend's existing submit primitive: the tmux pane the session inherited as `TMUX_PANE`, else the herdr pane the environment itself names through `HERDR_ENV`, `HERDR_SESSION`, and `HERDR_PANE_ID`.
On herdr that identity comes only from the injected environment, so a remote home's `fm-remote` session hands its scripts the same path and remote homes need no extra plumbing.
It uses only the inherited pane, proves the pinned session owner lives in that pane's foreground process tree, and requires an idle agent with an empty composer.
On herdr the pane's registered agent and any registered session binding must also identify `claude`, and idle must hold in both the native agent state and the rendered footer, because a live Claude turn can leave the native status reading idle.
Busy, unreadable, foreign, and partially typed panes defer delivery without changing the queued check.
The default retry interval is five minutes, and successful submission still does not acknowledge the hand-back.

Automatic retries currently reach Claude only through that verified tmux pane.
A primary outside tmux, an unavailable pane, or a blocked composer keeps its watcher and queued work but needs another normal turn or a human message.
Automatic retries currently reach Claude only through that verified tmux or herdr pane.
A primary outside both, an unavailable pane, or a blocked composer keeps its watcher and queued work but needs another normal turn or a human message.
A failed watcher restart or a terminated retry owner is not a guarantee of continued supervision.
The portable tests prove the process hand-off and transport guards, not a real Claude session accepting the retried input.
No new live-harness verification is claimed.
[verification/supervision.md](verification/supervision.md#supervision-host) owns the dated live results and verification entry points for both transports.
All other primaries and the host-off Stop path retain their existing behavior.

## Failure direction
Expand Down Expand Up @@ -467,6 +468,7 @@ Each arm owner's own suite covers its host mode against a stub host.
| `tests/fm-afk-return.test.sh` | The return's drain-owned read-cursor advance through the away window on a host home, and none on Pi. |
| `tests/fm-supervision-host-live-e2e.test.sh` | Runs a real engine turn; opt-in because it spends tokens. |
| `tests/fm-supervision-host-attended-live-e2e.test.sh` | Opt-in credentialed guard for repeated attended main-only hand-backs to an idle Claude primary, the successor's own close, a close that turns main-only at its turn, and a stand-in remote listener; accepts a pre-fix ref for a negative control. |
| `tests/fm-supervision-host-herdr-live-e2e.test.sh` | Opt-in credentialed guard that a real Claude primary in an isolated herdr session loses its hand-back continuation, receives the detached retry through the inherited pane, drains, and acknowledges, with one watcher retained. |
| `tests/fm-host-mirror-live-e2e.test.sh` | Proves the Claude and Cursor mirror writers against the real harnesses; opt-in because it spends tokens. |

[verification/supervision.md](verification/supervision.md#supervision-host) records the dated live results.
29 changes: 29 additions & 0 deletions docs/verification/supervision.md
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,35 @@ tests/fm-afk-return.test.sh
tests/fm-branch-supervision.test.sh
```

### Acknowledged hand-back retry over herdr, 2026-11-09

This supports [Reaching a lost Claude continuation](../supervision-host.md#reaching-a-lost-claude-continuation): the detached hand-back retry delivers to a Claude primary running in herdr through the inherited `HERDR_SESSION`/`HERDR_PANE_ID` pane identity, with the same ownership, idle, and composer guards as the verified tmux pane.
It was measured on 2026-11-09 on macOS arm64 with Claude Code 2.1.321 (model `haiku`) in an isolated `fm-lab-*` herdr session; the default and `fm-remote` sessions were not touched.

```text
$ FM_SUPERVISION_HOST_HERDR_LIVE_E2E=1 tests/fm-supervision-host-herdr-live-e2e.test.sh
# step 3: no Stop-hook rewake reached the transcript; owner 87432 alive on watcher 87805; pending host-83156-1794258417 unchanged
# step 4: retry confirmed delivery - handoff log: 1794258429 retry host-83156-1794258417 empty
# step 5: pending marker removed only after the acknowledgement
# step 6: turn end re-armed: 1794258437 start gen=host-6155-1794258437; watcher 87805 under host 6155
ok - herdr live e2e: a lost Claude hand-back is recovered by the detached retry, acknowledged, and supervision retained
```

| Case | Observed |
| --- | --- |
| Lost continuation | superseding the owning Stop hook's generation made it exit 0 with no exit-2, no banner, and no failure notice; the transcript shows no `firstmate watcher wake` between the hand-back and the doorbell |
| Detached owner | stayed alive on the same pending token and kept a verified watcher |
| Retry delivery | `retry <token> empty` in `.supervision-handoff.log`; the operational doorbell reached the inherited pane as a primary message |
| Acknowledgement | the primary ran `bin/fm-wake-drain.sh` then `--ack-through 3 --recovery-generation <gen>`; the pending marker disappeared only after the ack |
| After the acknowledged turn | the next Stop claimed a new generation, parked a host, and took over the detached owner's arm - one watcher retained |

Deterministic entry points:

```sh
tests/fm-supervision-host-hook.test.sh
tests/fm-backend-herdr.test.sh
```

## Wedge-alarm channels

The two real notification channels were bounded manually on 2026-07-10 on macOS 26.5.2 with Herdr 0.7.3.
Expand Down
2 changes: 1 addition & 1 deletion docs/watcher-continuity.md
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ OpenCode support targets persistent TUI sessions rather than headless `opencode
The other harnesses rely on these mechanisms:

- Claude normally depends on the [auto-arm hook's rewake](#claude-stop-hook).
The host's opt-in [acknowledged hand-back](supervision-host.md#acknowledged-hand-back) adds guarded tmux retries when that continuation is lost.
The host's opt-in [acknowledged hand-back](supervision-host.md#acknowledged-hand-back) owns guarded retries when that continuation is lost.
- Cursor depends on its awaited stop-hook park.
- Grok retains native background-completion notifications.
- Codex retains bounded foreground checkpoints unless the home opts in to its [native Stop park](supervision-protocols/codex.md).
Expand Down
50 changes: 50 additions & 0 deletions tests/fm-backend-herdr.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,55 @@ test_registered_agent_with_a_live_foreground_process_stays_alive() {
pass "herdr stale registration: a registered agent with a live Pi foreground process still reads alive"
}

# --- the foreground-occupant session proof ----------------------------------
#
# fm_backend_herdr_pane_owns_session is the herdr equivalent of the tmux
# pane_pid ancestor + tpgid proof the hand-back retry makes before delivery:
# the pane's own process-info must name a shell pid that is an ancestor-or-self
# of the pinned pid AND that pid's process group must be the pane's foreground
# group. The canned `pane process-info` body is paired with a real child
# process so the ancestry walk and pgid read run against the real process
# table, and every foreign, malformed, or unreadable half refuses.

owns_session_process_info() { # <shell-pid> <fg-pgid>
printf '{"result":{"type":"pane_process_info","process_info":{"pane_id":"w1:p2","shell_pid":%s,"foreground_process_group_id":%s,"foreground_processes":[]}}}' "$1" "$2"
}

owns_session_case() { # <process-info-body|-> <pid> [process-info-exit]
local dir resp log fb
dir=$(mktemp -d "$TMP_ROOT/owns-session.XXXXXX")
mkdir -p "$dir/responses"; resp="$dir/responses"; log="$dir/log"; : > "$log"
[ "$1" = - ] || printf '%s\n' "$1" > "$resp/1.out"
[ -z "${3:-}" ] || printf '%s\n' "$3" > "$resp/1.exit"
fb=$(make_herdr_fakebin "$dir")
PATH="$fb:$PATH" FM_HERDR_LOG="$log" FM_HERDR_RESPONSES="$resp" \
bash -c '. "$0/bin/backends/herdr.sh"; fm_backend_herdr_pane_owns_session fmtest w1:p2 "$1"' "$ROOT" "$2"
}

test_pane_owns_session_proves_the_foreground_occupant() {
local sleep_bin pid pgid
sleep_bin=$(command -v sleep) || fail "sleep not found"
"$sleep_bin" 300 &
pid=$!
pgid=$(ps -o pgid= -p "$pid" | tr -d ' ')
# shell_pid = the suite's own pid $$ - a real ancestor of the child.
owns_session_case "$(owns_session_process_info "$$" "$pgid")" "$pid" \
|| { kill "$pid" 2>/dev/null; fail "a real ancestor shell pid plus a matching foreground group must prove ownership"; }
# shell_pid = the pinned pid itself - the pane's top process IS the session.
owns_session_case "$(owns_session_process_info "$pid" "$pgid")" "$pid" \
|| { kill "$pid" 2>/dev/null; fail "a pane whose top process is the session must prove ownership"; }
if owns_session_case "$(owns_session_process_info 999999 "$pgid")" "$pid" \
|| owns_session_case "$(owns_session_process_info "$$" 999999)" "$pid" \
|| owns_session_case - "$pid" 1 \
|| owns_session_case "$(owns_session_process_info 1 "$pgid")" "$pid" \
|| owns_session_case "$(owns_session_process_info "$$" "$pgid")" not-a-pid; then
kill "$pid" 2>/dev/null
fail "a foreign shell pid, foreign foreground group, unreadable read, invalid shell pid, or invalid pid must all refuse"
fi
kill "$pid" 2>/dev/null || true
pass "herdr pane owns session: the foreground-occupant proof accepts only the real ancestry plus foreground group"
}

# --- the bound agent session reference (relaunch session continuity) --------
#
# Herdr applies only reports carrying the session identity it bound to a pane,
Expand Down Expand Up @@ -6500,6 +6549,7 @@ test_stale_registration_ignores_status_and_reads_the_process
test_pane_agent_session_ref_reports_a_resumable_reference_with_its_agent
test_pane_agent_session_ref_degrades_to_nothing_when_not_resumable
test_registered_agent_with_a_live_foreground_process_stays_alive
test_pane_owns_session_proves_the_foreground_occupant
test_registered_agent_with_a_non_shell_foreground_process_stays_alive
test_transient_prompt_helper_settles_into_stale_agent
test_exhausted_settle_window_keeps_a_non_shell_foreground_live
Expand Down
Loading
Loading