Skip to content

fix(bin): keep supervision armed for registered custom checks - #3860

Merged
kunchenguid merged 4 commits into
kunchenguid:mainfrom
gyute:fm/fm-3856-registered-checks-supervision
Sep 6, 2026
Merged

kunchenguid merged 4 commits into
kunchenguid:mainfrom
gyute:fm/fm-3856-registered-checks-supervision

Conversation

@gyute

@gyute gyute commented Sep 6, 2026 •

Copy link
Copy Markdown
Contributor

Intent

External contribution PR to kunchenguid/firstmate that closes upstream Issue #3856 ("Registered custom checks stop polling when the home has no in-flight task").

Goal: a custom check registered with bin/fm-check-register.sh is a home-level slow poll that only ever runs inside the watcher's check sweep, but fm_supervision_status in bin/fm-supervision-lib.sh counted only three things as supervision need - in-flight tasks (state/*.meta), the Relay poll shim (state/x-watch.check.sh), and process-event sources. So the moment the last task was torn down, the Claude Stop auto-arm exited and every registered check went silent until the next spawn. In the reporter's home that silently swallowed three captain answers polled from the GitHub API. Add a fourth condition: a registered custom check makes FM_SUP_NEEDED=true.

Accepted design decisions, made deliberately and confirmed with firstmate:

  • "Registered" means the trust binding fm-check-register.sh writes (state/.check-trust) sitting next to a same-named state/.check.sh. Presence of that binding is the whole test; the predicate deliberately does NOT re-verify the byte hash. Rationale to keep: the check sweep in bin/fm-watch.sh already validates the bytes at execution time and turns a rejection into a "check: rejected unauthenticated state checks" wake, so a home whose registered check no longer validates needs the watcher precisely so that rejection can be reported instead of the home going quiet. Requiring a hash match here would also pull fm-check-lib.sh and fm-pr-lib.sh plus a sha256 of every check file onto hook-hot paths (Stop auto-arm, turn-end guard) for no safety gain.
  • The Relay shim keeps its own separate trust path and must not be counted as a custom check; it is skipped by name so the counts stay disjoint and the banners stay accurate.
  • Task PR polls must not arm a home by accident: they carry no .check-trust binding and bin/fm-teardown.sh removes the task's check.sh and check-trust together, so a torn-down task leaves nothing behind. This was verified in the code and is asserted by a test.
  • One-owner rule (firstmate's own AGENTS.md / firstmate-coding-guidelines): fm_supervision_status is the single owner of the supervision-need condition set. Comments in bin/fm-guard.sh and bin/fm-claude-stop-autoarm.sh that enumerated the old conditions were replaced with one-line references to that owner rather than restating the new list. The enumerating comment in fm-claude-stop-autoarm.sh was already stale (it omitted process-event sources).
  • bin/fm-guard.sh's stale-watcher banner and bin/fm-turnend-guard.sh's NEED_DESC both fall through to "X-mode relay polling" as their else branch, so each gained an explicit registered-custom-check branch; without it a check-only home would be misreported as relay polling. FM_SUP_CHECKS is exported by the predicate for those messages.
  • Documentation is corrected in place, no new sections: one added sentence in docs/turnend-guard.md, two corrected enumerations in docs/architecture.md, and one sentence in docs/configuration.md that is now false - the watched-tool update check (bin/fm-tool-update-check.sh arm) registers through fm-check-register.sh, so from this change on, arming it IS a reason to watch and disarm is what ends that need. That behavior change for watched-tool homes is intended, not accidental. AGENTS.md is deliberately NOT touched: this is not a fact every session needs loaded. bin/fm-doc-audience-check.sh passes.

Constraints the user set:

  • Extend existing tests in tests/, never add a new runner. Tests must exercise the executable interface and must never assert implementation-source bytes. New cases live in tests/fm-turnend-guard.test.sh (its PREDICATE section already covers fm-supervision-lib.sh) and tests/fm-claude-stop-autoarm.test.sh. Minimum cases required: a registered-check-only home needs supervision; a check.sh with no trust binding does not; a task PR poll does not (and leaves nothing after teardown). Also added: a drifted/edited registered check still needs supervision (pins the presence-not-hash decision), the relay shim is not counted as a custom check, and an end-to-end auto-arm case where an idle home with a registered check still arms the Stop cycle. Registration in the tests goes through the real bin/fm-check-register.sh rather than hand-written imitation artifacts.
  • Tests must never touch this home's live state: everything runs over FM_STATE_OVERRIDE temp dirs via the existing tests/lib.sh helpers, and neither ~/firstmate/state nor data/ is read or written, and fm-watch-arm.sh / fm-watch.sh are never run against the live home.
  • bin/fm-lint.sh must pass (shellcheck 0.11.0, actionlint 1.7.12 both installed for this) and bin/fm-test-run.sh must pass on the edited suites.
  • Out of scope, deliberately: upstream A done Claude worker re-surfaces as stale every 1-2 minutes because its (user-configured) status line changes the pane hash #3857 (a done Claude worker re-surfacing as stale) is separate work and must not be touched here, and no harness adapter code changes.
  • Contribution rules: commits and the PR must carry no Co-Authored-By, no Claude-Session, and no "Generated with" attribution, and no agent name as co-author (CONTRIBUTING). Commit subject follows the repo convention fix(bin): ... with a short why/what body ending in "Closes Registered custom checks stop polling when the home has no in-flight task #3856". Push goes through the no-mistakes gate to the fork https://github.com/gyute/firstmate.git, which opens the PR against kunchenguid/firstmate.
  • PR body in English, one sentence per line, plain dashes only: one-line summary plus "Closes Registered custom checks stop polling when the home has no in-flight task #3856", what changed as before/after conditions, why (a short paragraph summarizing the issue), tests (the cases added and the exact commands and results), and blast radius (the Stop auto-arm, the turn-end guard, and fm-guard all read this same predicate). The Verification section must state in one line that two test failures - the fm-watch-arm re-arm case and the turn-end guard's Pi injection case - reproduce identically on a pristine HEAD checkout and therefore predate this branch.

What Changed

  • fm_supervision_status in bin/fm-supervision-lib.sh now counts a state/<id>.check.sh that has the state/<id>.check-trust binding written by bin/fm-check-register.sh as a fourth supervision-need condition, exported as FM_SUP_CHECKS; the x-watch relay shim is skipped by name, and presence of the binding (not a byte-hash match) is the whole test, so the Claude Stop auto-arm, turn-end guard, and bin/fm-guard.sh keep a watcher alive for a home whose only work is a registered custom check.
  • bin/fm-guard.sh and bin/fm-turnend-guard.sh gained an explicit "N registered custom check(s)" branch in their stale-watcher banner and need description, replacing the fall-through to "X-mode relay polling", and their comments now point at fm-supervision-lib.sh as the single owner of the condition set instead of enumerating it.
  • Docs (docs/architecture.md, docs/turnend-guard.md, docs/configuration.md, docs/supervision-protocols/grok.md) are corrected in place to list registered custom checks as a supervision need, including that arming the watched-tool update check now keeps a watcher after the last task is torn down; tests in tests/fm-turnend-guard.test.sh and tests/fm-claude-stop-autoarm.test.sh add predicate cases (registered check, drifted bytes, unregistered check, task PR poll, relay shim), a turn-end guard banner case, and an end-to-end auto-arm case, all registering through the real bin/fm-check-register.sh.

Risk Assessment

✅ Low: The change adds one well-bounded OR condition to the single supervision-need owner, threads the new count into two banner ladders and one description, and corrects docs in place; every traced input (registered check, unbound check, task PR poll, relay shim, drifted bytes) yields the intended count, the watcher and away-mode daemon hold no competing need logic, teardown and unregister remove the binding with the check, and all added tests drive the real registration script and the real hook without any source-byte assertions.

Testing

Completed 1 recorded test check.

  • Outcome: ⚠️ 1 error across 1 run (1h9m27s)

Pipeline

Updates from git push no-mistakes

✅ **intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

⚠️ **Review** - 1 info
  • ℹ️ tests/fm-turnend-guard.test.sh:150 - The intent states that 'a torn-down task leaves nothing behind' is 'asserted by a test', but this case never runs bin/fm-teardown.sh: it hand-creates task1.meta, task1.check.sh, and task1.pr-poll, then deletes them with rm -f and asserts the (now empty) state dir needs no supervision. The second half is trivially true for any empty directory and would still pass if remove_pr_poll_artifacts in fm-teardown.sh stopped removing <id>.check-trust. The first half (a PR poll with no .check-trust is not counted) is a genuine executable-interface assertion and is fine. Either rename the case to what it actually proves ('a PR poll without a trust binding is not counted') or drive the removal through the real teardown path; the latter would pull in fm-teardown.sh's worktree/tmux preconditions, which is why this is flagged for the author rather than fixed here.

🔧 Fix: narrow PR poll predicate test to what it proves
1 info still open:

  • ℹ️ bin/fm-turnend-guard.sh:217 - The two fix-round commits (e86eafe 'no-mistakes(review): name registered checks in turn-end block banner and doc invariant' and dd753d7 'no-mistakes(review): narrow PR poll predicate test to what it proves') do not follow the intent's stated contribution rule that commit subjects use 'fix(bin): ...' with a why/what body ending in 'Closes Registered custom checks stop polling when the home has no in-flight task #3856'. Their content is correct and carries none of the forbidden attribution trailers (no Co-Authored-By, Claude-Session, or 'Generated with'), and the original commit 6d6760a satisfies the rule fully. If the push step forwards all three commits as-is, the PR to kunchenguid/firstmate will carry two commits outside the convention the user set. The remedy is a squash of the two fixer commits into 6d6760a (or a reword) before push, which is a history rewrite the user should authorize rather than something to fix in source.
⚠️ **Test** - 1 error
  • 🚨 tests failed with exit code 1
  • bin/fm-test-run.sh --changed --exclude-family real-herdr-gated
✅ **Document** - passed

✅ No issues found.

✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.


On "a matching valid .check-trust binding"

The triage on #3856 asks for a check to be counted only when it carries "a matching valid .check-trust binding the watcher would execute".
This patch counts the binding's presence and deliberately does not re-verify the byte hash inside the predicate.
The check sweep in bin/fm-watch.sh already validates those bytes at execution time through fm_custom_check_snapshot_prepare, and a check that fails that validation is not dropped silently: the sweep reports it as check: rejected unauthenticated state checks, which is a wake the operator needs to see.
Hash-gating the predicate would invert that, because a registered check whose bytes drifted would stop arming the home and the rejection would then never be reported - the same silence this issue is about.
It would also pull bin/fm-check-lib.sh and bin/fm-pr-lib.sh plus a SHA-256 of every check file onto hook-hot paths, since the Claude Stop auto-arm and the turn-end guard evaluate this predicate at every turn boundary, for no added safety.
The triage's other two constraints hold unchanged: an unregistered or untrusted check is still rejected by the sweep exactly as before, and a task PR poll carries no .check-trust binding and is removed with its task by bin/fm-teardown.sh, so neither can keep a home armed.
tests/fm-turnend-guard.test.sh pins this decision with a case asserting that a registered check whose bytes drifted still needs supervision.

Verification

bin/fm-lint.sh passes with the pinned shellcheck 0.11.0 and actionlint 1.7.12, and bin/fm-doc-audience-check.sh passes.
The six added cases all pass: a registered check needs supervision with no task in flight, a drifted registered check still needs supervision, a check.sh with no registration binding does not, a task PR poll without a trust binding is not counted, the relay shim is not counted as a custom check, and an idle home with a registered check still arms the Stop auto-arm cycle.
The turn-end guard also blocks a check-only blind turn with 1 registered custom check(s) instead of misreporting it as relay polling.
Every test runs over FM_STATE_OVERRIDE temp directories through the existing tests/lib.sh helpers, so no live firstmate home is read or written.
On the full changed-file selection (bin/fm-test-run.sh --changed) this branch reports 15 failures out of 107 scripts, against 17 out of 107 for the unmodified base on the same machine, so the branch fails fewer scripts than its own baseline.
Those failures split into three causes, none of them in code this change touches: suites that fail identically on base a913539 extracted with git archive, suites that fail on a missing node in the sandbox (No version is set for command node), and load-related timing on a machine running the suite with bounded concurrency.
Two environments observed two different pre-existing turn-end failures - locally the Pi injection case, and in the pipeline sandbox the fresh-beacon-no-live-lock case (hook must block when a fresh beacon has no live watcher lock) - and both reproduce on base a913539, so each is named as observed rather than as one fixed case.

A custom check bound by bin/fm-check-register.sh only ever runs inside the
watcher's check sweep, but fm_supervision_status counted in-flight tasks, the
relay poll shim, and process-event sources as supervision need, and not
registered checks. Tearing down the last task therefore stopped every
home-level check silently until the next spawn.

Count a state/<id>.check.sh that carries its state/<id>.check-trust binding as
supervision need. The relay shim keeps its own trust path and task PR polls
carry no such binding and are torn down with their task, so neither arms a home
by accident. Presence of the binding is the whole test: the sweep validates the
bytes at execution time and wakes firstmate when it rejects one, which is the
outcome an idle home needs.

Closes #3856
@greptile-apps

greptile-apps Bot commented Sep 6, 2026

Copy link
Copy Markdown

Confidence Score: 5/5

The PR appears safe to merge, with the shared supervision predicate and its consumers consistently handling registered custom checks.

Registered checks now keep supervision active, the watcher and away daemon already continue polling them, and no concrete blocking failure remains in the changed paths.

Reviews (1): Last reviewed commit: "no-mistakes(document): point Grok re-arm..." | Re-trigger Greptile

@kunchenguid

Copy link
Copy Markdown
Owner

Speaking as Kun's firstmate:

Triage on HEAD 3e0a02af95a69ca356b7a365280315fbd8c96197 vs main b028e8b14a1fcf8b16bdafc99fba616cd8577050. Fork from @gyute. Closes #3856 verified (commit subject/body Closes #3856; GraphQL closingIssuesReferences → #3856; matches ready-for-pr stamp on that issue). MERGEABLE unknown until hosted CI finishes after first-time fork approval.

Contract-class: restore. Registration + check sweep already promised a trust-bound state/<id>.check.sh would run; fm_supervision_status forgot those same objects. Counting .check-trust presence (not re-hashing on the hook-hot path) matches the issue constraints and keeps drifted bytes arming so the sweep can report rejection instead of going silent. Relay shim skipped by name; task PR polls without trust stay non-arming. Not a new default for unconfigured homes.

Diff reviewed: fm_supervision_status + FM_SUP_CHECKS; banner/NEED_DESC branches in fm-guard.sh / fm-turnend-guard.sh; one-owner comment cleanup; docs in place; tests via real fm-check-register.sh (registered / drifted / unregistered / PR-poll / relay / auto-arm / check-only banner). No .github/**, no credential surface.

Attestation: MATCH for tip 3e0a02af (no-mistakes-pipeline-attestation:v1). Pipeline Test step discloses pre-existing failures the author pins to base a913539 (not introduced here). Greptile Review pass / confidence 5/5 on tip.

CI/NM this pass: first-time fork workflows approved after security skim — CI 34052486276, Require no-mistakes 34053057997 / 34052486241 now queued. Waiting on green hosted CI + NM before merge.

VISION.md (each rule)

  • One captain, one interface: aligns — registered poll that goes dark hides captain answers until chat notice.
  • Authority is explicit and never inferred: aligns — only trust-registered checks count; operator already consented via fm-check-register.sh.
  • Scripts own the mechanics, agents own the judgment: aligns — arming predicate is exact bash owned by fm-supervision-lib.sh.
  • A restart is a non-event: aligns — disk-registered obligations keep running when in-flight tasks leave.
  • Delegation with a spine: aligns — compose register + sweep; no new scheduler.
  • The fleet outlives any vendor: aligns — harness-neutral home predicate; Claude Stop auto-arm is one caller.
  • Scope: aligns — command-layer watcher continuity; field incident → regression tests.

Security: no. Trust-binding already gates execution; presence-not-hash is intentional so rejection wakes rather than silence.

Merge-eligible N until CI/NM green. Waiting-ci. Auto-merge candidate once green (restore + MATCH + safe review). No captain flag this pass.

@kunchenguid
kunchenguid merged commit 29015a2 into kunchenguid:main Sep 6, 2026
16 checks passed
@kunchenguid

Copy link
Copy Markdown
Owner

Speaking as Kun's firstmate: this is merged. Thank you @gyute — really appreciate you taking the time on this.

eduardstan added a commit to eduardstan/firstmate that referenced this pull request Sep 7, 2026
* fix(bin): wait out transient primary-checkout reads in the spawn worktree poll (#3834)

* fix(spawn): keep the worktree poll from adopting the repository primary

After `treehouse get` is sent, the worktree-discovery poll reads the pane's
foreground-process cwd. While treehouse is still fetching and checking a slot
out, the foreground process is treehouse itself and it reports the repository's
PRIMARY checkout as its cwd for several seconds. The poll accepted any path
that merely differed from the spawning project, so from a linked spawning home
- whose project is itself a worktree of that repository - it adopted the
primary, and the isolation guard then refused a launch whose slot treehouse
went on to create normally.

Screen every candidate with the isolation guard's own conditions, extracted as
spawn_worktree_isolated, so a read the guard would reject stays a transient the
poll keeps waiting through. The two-consecutive-reads rule and the guard as
final backstop are unchanged; a pane that never reaches an isolated worktree
still fails at the existing 60s deadline, now naming the last path it reported.

The already-settled timing assertion counted whole-spawn wall time against a
5s budget and failed on unmodified HEAD on slower machines; it now counts pane
reads, which is what "one confirming read, not an extra cycle" actually means.

* fix(spawn): say which path the worktree wait rejected, and why

Screening every discovery-poll candidate means a host that never reaches an
isolated worktree spends the whole 60s window before refusing. That wait is
deliberate - separating a transient from a terminal misconfiguration needs
machinery this path does not want - so the refusal explains itself instead:
the isolation check records why a candidate failed, and the deadline names the
last path seen together with that reason. Message and diagnostics only; the
poll's control flow is unchanged.

Two suites asserted the guard's wording on paths the poll now rejects rather
than adopts, so their refusal arrives from the deadline instead: realign
fm-tangle-guard's non-git and subdirectory-of-primary cases (each now also
asserting the stated reason, and the second the metadata absence it was
missing) and the herdr projection e2e's forced non-worktree cwd.

* no-mistakes(review): stub poll sleep in tangle-guard spawn isolation test

* no-mistakes(document): document spawn poll isolation screen in fm-spawn header

* test(spawn): make the non-git isolation case non-git anywhere

The refusal-reason assertion for a path outside any repository assumed TMPDIR
is not inside a git repository. Where it is, git walks up from the temporary
directory, finds that repository, and the spawn reports the subdirectory cause
instead - so the case passed or failed on a property of the host rather than on
the behaviour under test.

Build the path under a directory the test then names in GIT_CEILING_DIRECTORIES,
which git documents as not chdir-ing up into a listed directory while looking
for a repository. Git never excludes the directory being searched, so the
ceiling is the parent of the path handed to the spawn.

The assertions pin which cause fired rather than the sentence that explains it,
leaving the operator wording free to improve.

* no-mistakes(document): point spawn poll comment at the isolation screen's comparison

* no-mistakes(ci): Fixed the "Behavior portable serial 2" failure in tests/fm-tangle-guard.test.sh ("non-worktree spawn did not say why the path was rejected (missing: 'not inside a git worktree')"). Root cause, in this PR's code: bin/fm-spawn.sh's spawn_worktree_isolated resolved the git toplevel with `wt_top_real=$(cd "$SPAWN_WT_TOP" ...)`. For a path in no repository, `git rev-parse --show-toplevel` yields empty, and `cd ""` is a SUCCESSFUL no-op on bash before 5.3 (CI's ubuntu-latest ships bash 5.2). The empty toplevel therefore resolved to fm-spawn's own cwd — the CI checkout — so the poll reported "it is a subdirectory of worktree root '/home/runner/work/firstmate/firstmate'" instead of the correct "it is not inside a git worktree". Dev machines with bash 5.3 fail `cd ""`, which is why the suite passed locally and only failed on CI; it is a genuine shell-portability defect in the reason vocabulary this change added, not a test-environment artifact. Fix (smallest root-cause change, 1 line + comment, bin/fm-spawn.sh:2168-2173): guard the empty value so it never reaches `cd` — if [ -n "$SPAWN_WT_TOP" ] && ! wt_top_real=$(cd "$SPAWN_WT_TOP" 2>/dev/null && pwd -P); then No change to the poll's timing or deadline behavior (respecting the recorded refusal-latency and spawn-wt-reason-vocabulary decisions), no new tests, no other files touched. Verification: - Reproduced the exact CI failure locally by putting bash 3.2 (same `cd ""` semantics as CI's 5.2) first on PATH: fails before the fix with the identical message shape, passes after. - tests/fm-tangle-guard.test.sh passes under both bash 3.2 and bash 5.3. - tests/fm-spawn-worktree-settle.test.sh and tests/fm-spawn-pool-base-freshen.test.sh pass; shellcheck -x bin/fm-spawn.sh clean. - bin/fm-test-run.sh --changed: 46 suites completed, every FM_TEST_END exit=0, 1076 passing assertions, 0 "not ok" (including fm-tangle-guard, fm-control-relaunch, fm-lint). The run ended on my own 900s wall-clock cap (rc=124), not on any test failure

* fix: support stock macOS Bash 3.2 paths (#3732)

Co-authored-by: Talon Stark <[email protected]>

* fix(bin): read orphaned green ci monitor and daemon-down failed record as not failed (#3846)

* fix(bin): read an orphaned green ci monitor as held-for-merge, not failed

A no-mistakes run held for a captain merge decision keeps its ci step
polling until merged or closed; when the shared daemon restarts under
that poll, the run is recorded failed although every substantive step
completed and GitHub reports the PR green. A monitor whose only
remaining job is to observe a human decision must not convert the
absence of that decision into a failure verdict.

fm-crew-state.sh now reclassifies a terminal failed run as done
(held-for-merge), surfacing the run's PR URL, when the steps table
shows every step completed except exactly ci failed and the ci log's
last recognized marker reads checks green. A genuinely red check, an
unreadable ci log, or a second failed step keeps the failure.

* no-mistakes(review): Read daemon-down coarse failed ledger as unknown, not failed

* no-mistakes(document): docs: align AGENTS.md failed-verdict guidance with crew-state reclassification

* fix(spawn): verify preserved backlog state after interrupted spawn delivery (#3852)

* fix(bin): read preserved spawn state back before the interrupted exit claims it

The