Skip to content

fix(watch): require agent presence before trusting a busy frame - #7

Merged
knowttl merged 1 commit into
mainfrom
fm/fm-fix-busy-frame-dead-agent
Jul 27, 2026
Merged

knowttl merged 1 commit into
mainfrom
fm/fm-fix-busy-frame-dead-agent

Conversation

@knowttl

@knowttl knowttl commented Jul 27, 2026

Copy link
Copy Markdown
Owner

Problem

When an agent process exits, its final rendered screen frame stays painted in the pane. That residual frame still matches the harness busy signature, so stale detection classified the dead, agent-less pane as a busy worker on every poll: it never went stale, never woke supervision, and never reached recovery.

Reproduced 2026-07-27 on the herdr backend. A claude crewmate's process died overnight in a host resource-pressure termination wave. Its pane kept the last frame, and the task sat "working" for ~16 hours with zero stale wakes until the captain noticed the abandoned tab. Ground truth was visible the whole time: herdr pane list returned no agent field and agent_status: "unknown" for that pane, while live worker panes carry agent: claude|codex with working|idle|done. The task's uncommitted work was intact - only detection failed.

Fix

Liveness classification now verifies agent presence, not just frame appearance.

  • bin/fm-backend.sh (backend dispatch owner) gains two primitives:
    • fm_backend_has_agent_presence <backend> - does this backend answer "is an agent registered here right now" from a registration surface independent of both the rendered frame and the pane's foreground process?
    • fm_backend_agent_confirmed_absent <backend> <target> - 0 only when the backend has that surface and two reads separated by FM_AGENT_ABSENT_CONFIRM_SECS (default 2) both report the endpoint agent-less. Any ambiguous or unreadable verdict returns 1, so an inconclusive read never overrides the caller's existing classification.
  • bin/fm-watch.sh (stale-detection cadence owner): window_is_busy consults it only on the frame-matched branch. A live busy pane pays one presence read; a plainly idle pane pays none; a dead pane loses to the frame and goes stale on the normal cadence.
  • bin/fm-crew-state.sh (current-state reconciliation owner): an agent-less endpoint is reported gone (state: unknown · source: none) instead of reading working · pane off its residual frame, so the resulting stale wake is not absorbed as provably working.

The two-read confirmation is what satisfies "no new false stales": a live agent already loses on the first read (costing nothing extra), while a momentary deregistration mid-frame-repaint re-reads alive and is rejected.

Backend compatibility review

Every supported backend was reviewed against its actual integration surface:

Backend Presence surface Applied? Why
herdr agent get answers from herdr's own agent registry (agent_not_found for an agent-less pane, via fm_backend_herdr_pane_agent_state) yes Registration is independent of both frame content and process identity.
tmux (reference) fm_backend_tmux_agent_state exists, but its dead verdict reads the pane's foreground command being a bare shell no - not applicable A live harness running a shell tool call legitimately shows a shell as the pane's foreground command. That is accurate enough for the session-start recovery sweep (where a wrong answer is reconciled against recorded work) but would mint false stales in the per-poll busy path. tmux keeps its frame-signature behavior unchanged. Related upstream issue kunchenguid#1130 (fm_backend_target_exists reporting a gone tmux window as alive) is not touched here and is not made worse - that function is untouched.
zellij none (fm_backend_agent_state -> unverified) no - not applicable No recovery classifier; frame-signature behavior unchanged.
orca none (unverified) no - not applicable Same.
cmux none (unverified) no - not applicable Same.

Other busy-state call sites reviewed and left alone:

  • bin/fm-composer-lib.sh classifies composer/pending-input, not liveness - not applicable.
  • fm_pending_reply_backend_observation (bin/fm-pending-reply-lib.sh) uses a busy observation to mark a secondmate turn as seen; a residual frame there shortens the grace and escalates earlier rather than masking, so it is not the same defect.
  • stale_window_is_busy in bin/fm-supervise-daemon.sh (away-mode supervision) does carry the same hole, but that file was deliberately reset to match upstream byte-for-byte in c4ec3b2 ("match upstream exactly in fm-supervise-daemon"). Closing it there means a fork-only edit to that file, so it is left out of this PR as a captain decision rather than taken silently. Consequence: while away mode is active, the dead-agent pane can still stay suppressed.

Docs

docs/herdr-backend.md "Restart and liveness behavior" gains two sentences: the liveness probe now also backs stale detection and current-state reconciliation, and absence must survive a confirming re-read. One-owner rule respected - the probe's state vocabulary is still stated once, in that section.

Tests

New regression coverage, colocated in the existing suites:

  • tests/fm-backend-herdr.test.sh
    • dead agent behind a busy frame -> confirmed absent, and the confirming re-read actually happens (asserted by call count)
    • live agent -> never absent, and short-circuits before the re-read
    • transient flap (absent then alive) -> rejected
    • inconclusive read -> never confirms absence
    • capability is herdr-only; tmux/zellij/orca/cmux/unknown all decline
  • tests/fm-watch-triage.test.sh - window_is_busy: live+busy -> busy, live+idle -> idle, dead+busy-frame -> not busy, missing+busy-frame -> not busy, unreadable -> frame still wins, tmux -> frame-only behavior preserved
  • tests/fm-crew-state.test.sh - dead agent with a residual busy frame reads gone (not working · pane) and is not crew_is_provably_working; the herdr fake now models pane get and the real agent_not_found error shape so pane presence and agent presence are separable

Local verification

GitHub Actions is billing-disabled fleet-wide, so local runs are the gate.

$ bash tests/fm-backend-herdr.test.sh            -> exit 0 (all pass)
$ bash tests/fm-crew-state.test.sh               -> exit 0 ("all fm-crew-state tests passed")
$ bash tests/fm-backend.test.sh                  -> exit 0
$ bash tests/fm-secondmate-liveness.test.sh      -> exit 0
$ bash tests/fm-composer-lib.test.sh             -> exit 0
$ bin/fm-doc-audience-check.sh                   -> ok surfaces=56 local_links=150
$ bin/fm-lint.sh bin/fm-backend.sh bin/fm-crew-state.sh bin/fm-watch.sh \
    tests/fm-backend-herdr.test.sh tests/fm-crew-state.test.sh tests/fm-watch-triage.test.sh
  -> exit 0 (ShellCheck 0.11.0, pinned 0.11.0)

An earlier full canonical bin/fm-lint.sh run over the whole file set also passed (exit 0); a later full re-run hit my own 900s wrapper timeout because of host load, so the changed-file run above is the clean current evidence at the same pinned version and config.

One suite could not be run green on this host, and it is not green on origin/main either

tests/fm-watch-triage.test.sh drives real fm-watch.sh subprocesses with 3-second liveness budgets. This host is at load average ~80 on 6 cores (sibling worktrees running their own lint/test sweeps), and those subprocess-timing tests fail there.

  • With this branch: fails at stale suppressor not advanced on absorb.
  • On unmodified origin/main, two runs: stale suppressor not advanced on absorb, then provably-working signal did not advance its .seen-* suppressor.

Different failure points across runs, and present without my changes, so this is host-load flakiness rather than a regression. The failing cases are all tmux-backend/no-backend paths where fm_backend_has_agent_presence returns false and the new code is inert. My new test in that suite, window_is_busy: a residual busy frame needs a present agent on a presence-capable backend, passed in every run. Worth a green re-run on a quiet host before merge.

Pre-existing failure spotted, not fixed here

tests/fm-daemon.test.sh fails at pane_is_busy must trust Herdr native idle over historical busy text. Cause: commit c4ec3b2 dropped the fork-only idle) return 1 short-circuit from pane_is_busy to match upstream byte-for-byte, but the fork-only test asserting that behavior stayed. Fixing it means either re-diverging from upstream or removing the test - a captain call, so it is reported rather than decided here.

An agent process that exits leaves its last rendered frame painted in the
pane, and that residual frame keeps matching the harness busy signature on
every poll. Stale detection therefore classified the dead, agent-less
endpoint as a busy worker forever: it never went stale, never woke
supervision, and never reached recovery (reproduced 2026-07-27 on herdr - a
crewmate died overnight and the task showed as working for ~16h with the
pane's own `agent` field absent the whole time).

Liveness now verifies agent PRESENCE, not just frame appearance. Where a
backend can answer presence authoritatively, a confirmed agent-less endpoint
overrides a busy-looking frame:

- bin/fm-backend.sh gains fm_backend_has_agent_presence (the per-backend
  capability) and fm_backend_agent_confirmed_absent (two reads, so a
  transient status flap mid-repaint is not mistaken for an exit).
- bin/fm-watch.sh's window_is_busy consults it only on the frame-matched
  branch, so the dead pane goes stale on the normal cadence while a live
  busy pane pays one presence read and an idle pane pays none.
- bin/fm-crew-state.sh reports an agent-less endpoint as gone rather than
  reading `working` off its residual frame, so the resulting stale wake is
  not absorbed as provably working.

Only herdr qualifies: `agent get` answers from herdr's own agent registry.
tmux is deliberately excluded even though it has an agent-state classifier -
its `dead` verdict reads the pane's foreground command, which a live harness
running a shell tool call legitimately makes a bare shell, so trusting it in
the per-poll busy path would mint false stales. zellij, orca, and cmux have
no recovery classifier at all. All four keep their existing behavior.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant