Skip to content

fix(supervision): prevent Copilot watcher arms from blocking chat - #25

Merged
egarlock merged 4 commits into
mainfrom
fm/fm-chat-blocked-watch-diagnosis
Aug 3, 2026
Merged

egarlock merged 4 commits into
mainfrom
fm/fm-chat-blocked-watch-diagnosis

Conversation

@egarlock

@egarlock egarlock commented Aug 3, 2026

Copy link
Copy Markdown
Owner

Intent

Fix an operator-facing Firstmate bug: captain chat input was silently queued while fleet supervision was running. The accepted diagnosis found two coupled causes. First, Copilot CLI queues typed input while a model turn is active, and an attached bash tool call holds that turn open for its whole initial_wait, while bin/fm-watch-arm.sh blocks on wait for the entire unbounded watcher cycle. Second, bin/fm-supervision-instructions.sh selected protocol snippets from a hardcoded claude|codex|opencode|pi|grok case with no copilot arm, so a verified copilot primary rendered as harness unknown and received the permissive generic fallback that told it to arm the watcher without a bounded wait.

Scope is Wave 1 of the accepted report only. Deliberate decisions a reviewer cannot see in the diff:

  • The renderer is now allowlist-driven through fm_harness_is_verified from bin/fm-harness-policy.sh rather than gaining a new copilot case arm, because that file is the documented single source of FM_VERIFIED_ADAPTERS and a newly verified adapter should be picked up automatically. Deliberate side effect: when a verified adapter has no snippet, HARNESS is forced to unknown too, so the heading always matches the body.
  • initial_wait is derived from FM_ARM_CONFIRM_TIMEOUT and then capped at 30. Derived because a fixed small value could return before the arm confirms; capped because that timeout is an operator knob and propagating it uncapped rendered a 305 second attached call, reopening the exact incident in text that simultaneously said the call must return promptly.
  • bin/fm-watch-arm.sh normalizes a malformed FM_ARM_CONFIRM_TIMEOUT because the second deadline computation runs after the watcher child is forked, so a malformed value orphaned a watcher. The guard rejects non-numeric, octal-looking, and 10-or-more-digit overflow values in both the renderer and the arm. Found by direct probe: 08 plus 5 is an octal error that under set -eu aborted the renderer for every harness including claude, and bin/fm-turnend-guard.sh line 92 calls the renderer with stderr suppressed so it would have degraded silently. A 9223372036854775807 input wrapped to a negative value that passed the cap check.
  • detach true is forbidden in the protocol solely because a detached arm outlives the session and breaks the arm contract that killing the arm tears its watcher down. An earlier stop_bash based rationale was contested and was removed rather than defended.
  • bin/fm-watch-checkpoint.sh bounded waiting was deliberately NOT adopted as the copilot default; the attached short-wait arm preserves the completion-notification wake path. FM_CHECKPOINT_TIMEOUT_IMPL is a test seam, needed because PATH manipulation cannot portably hide a binary from command -v. The macOS host has neither timeout nor gtimeout but does have perl, so the perl fallback is the real macOS path and is now gated by command -v with a clear error instead of a 127 passthrough.
  • .agents/skills/harness-adapters/SKILL.md is loaded as live operating instructions before every spawn, so its claim that copilot as primary is unsupported was corrected in the same change.

Explicitly out of scope by captain instruction: Wave 2, and any global Copilot hook install or change. The Copilot primary has no tracked turn-end or session-start hook backstop; that gap is recorded in docs and tracked by decision hold copilot-primary-global-hook-fallback.

Two findings from prior review rounds should not be re-raised without new evidence. A claim that Copilot CLI enforces a 30 second initial_wait floor was disproven by direct probe on this host: initial_wait 15 returned after exactly 15 ticks and initial_wait 5 after exactly 5, on Copilot CLI 1.0.77. The protocol notes it as harmless if some other build differs. Separately, tests/fm-session-start.test.sh fails on any host where COPILOT_CLI is set because the test does not pin the harness; this was proven pre-existing by stashing all changes and re-running on the base commit, and is deliberately not fixed here.

Repo style constraints in force: one full sentence per line in tracked Markdown, plain dash never em dash, no agent name as commit co-author, bin scripts must pass bin/fm-lint.sh, tests colocated in tests as subject.test.sh running under set -u with errexit deliberately off.

What Changed

  • Add a Copilot primary supervision protocol that arms the watcher through an attached shell call with a bounded initial_wait, so the model turn returns promptly while completion notifications still wake supervision.
  • Make protocol selection follow the verified harness allowlist, and derive and cap the Copilot wait budget while normalizing malformed, octal-looking, or overflow-prone FM_ARM_CONFIRM_TIMEOUT values in both rendering and watcher arming.
  • Harden checkpoint timeout selection across timeout, gtimeout, and perl, and update live operating docs and regression coverage for the Copilot supervision path.

Risk Assessment

✅ Low: The change is well-bounded and closes the reachable Copilot queued-chat path with an allowlist-selected protocol, a capped attached-call wait, consistent timeout normalization, and explicit documentation of the intentionally deferred hook backstop.

Testing

No pre-supplied baseline test command was present; a base-commit render reproduced the unknown-harness fallback, the focused automated tests completed successfully, a hermetic session start rendered Copilot's attached 15-second arm contract, malformed and oversized timeout inputs stayed normalized or capped, and a live arm emitted a healthy status before HUP tore down its watcher child. The CLI-facing change is demonstrated with reviewer-visible transcripts.

Evidence: Base versus target Copilot supervision render
BASE 4363d945:
SUPERVISION OPERATING INSTRUCTIONS - primary harness: unknown
- Ordinary wake: follow the continuation in the harness protocol below; do not use shell &.
Mode: Unknown harness fallback.

TARGET 59ee0797:
SUPERVISION OPERATING INSTRUCTIONS - primary harness: copilot
- Ordinary wake: re-arm exactly one bin/fm-watch-arm.sh attached Copilot shell call with initial_wait 15 as directed below; the call must return promptly so the turn can end.
Mode: Copilot attached short-wait background supervision.
   `bash` with `initial_wait: 15` and `detach` omitted, on:
Evidence: End-to-end Copilot session-start supervision block
================================================================================
SUPERVISION OPERATING INSTRUCTIONS - primary harness: copilot
Current state:
- Lock: held by this session; this session owns normal supervision unless away mode says otherwise.
- Away mode: inactive.
- X mode: inactive; use the default watcher cadence.
- Ordinary wake: re-arm exactly one bin/fm-watch-arm.sh attached Copilot shell call with initial_wait 15 as directed below; the call must return promptly so the turn can end.

Mode: Copilot attached short-wait background supervision.

When this session owns supervision and away mode is not active:
1. Drain first with `bin/fm-wake-drain.sh`.
2. Source `/var/folders/2r/lv20clyn0xbb0mq181bxbmpw0000gn/T/no-mistakes-evidence/01KZ2KQRTQAKDTM3Z1GYQ1NNXM/session-start-fixture/copilot-primary/home/config/x-mode.env` first when X mode is active.
3. First cycle: arm with Copilot's shell tool, as its own call, with a short `initial_wait` and its own `shellId`:

   `bash` with `initial_wait: 15` and `detach` omitted, on:
   `[ -f '/var/folders/2r/lv20clyn0xbb0mq181bxbmpw0000gn/T/no-mistakes-evidence/01KZ2KQRTQAKDTM3Z1GYQ1NNXM/session-start-fixture/copilot-primary/home/config/x-mode.env' ] && . '/var/folders/2r/lv20clyn0xbb0mq181bxbmpw0000gn/T/no-mistakes-evidence/01KZ2KQRTQAKDTM3Z1GYQ1NNXM/session-start-fixture/copilot-primary/home/config/x-mode.env'; exec bin/fm-watch-arm.sh`

4. The arming call MUST return promptly so the turn can end.
   Copilot queues everything the captain types while a model turn is active, and an attached shell call holds the turn open for its entire `initial_wait`.
   `bin/fm-watch-arm.sh` blocks for the whole watcher cycle, which is unbounded, so a long `initial_wait` pins the turn open and the captain's chat silently queues until the call ends.
   `initial_wait: 15` is the confirmation budget only: it is just past `FM_ARM_CONFIRM_TIMEOUT` so the arm's honest status line is visible, and short enough that chat stays responsive.
   It is capped at 30 seconds, so raising `FM_ARM_CONFIRM_TIMEOUT` past 25 no longer widens it; past the cap the status line may not have printed when the call returns.
   Some Copilot builds enforce a minimum `initial_wait` and silently raise a smaller value, which is harmless here because the hold stays short and bounded either way.
   Never raise it to cover the watcher cycle.
5. Never set `detach: true` for the arm.
   A detached arm outlives this session, which breaks the arm's own contract that killing the arm tears its watcher down too.
6. Never use shell `&` for firstmate supervision.
7. Never bundle the arm onto another command.
   Copilot has no tracked PreToolUse seatbelt, so nothing rejects a bundled or backgrounded arm before it runs.
8. Trust only the arm's one-line status.
   If the call returns before any `watcher:` line appears, make exactly one short `read_bash` on the arm's `shellId` to collect it.
   Do not poll in a loop and do not use a long delay: `read_bash` holds the turn open the same way the arming call does, so repeated or slow reads recreate the queued-chat symptom this protocol exists to prevent.
   If that single read still shows no status line, treat supervision as unconfirmed, say so, and re-arm rather than assuming either success or failure.
9. `watcher: started ...` or `watcher: attached ...` means a live cycle exists.
   On attach, the arm follows verified identity-matched successors instead of exiting when the first cycle ends.
10. Failure or missing cycle only: `watcher: FAILED ...` means supervision is down; fix and re-arm.
11. After a successful start or attach status, end the turn.
    The arm keeps running in the background as the live wait until it returns an actionable wake or failure.
12. Waiting is silent.

Copilot raises a shell-completion notification when the backgrounded arm exits, and that notification starts a new turn.
When you see the arm's completion notification:
1. Run `bin/fm-wake-drain.sh` first.
2. Optionally read the arm's remaining output with `read_bash` on the arm's `shellId` for the reason line.
3. Handle `signal`, `stale`, `check`, or `heartbeat` using the harness-neutral contract in `AGENTS.md`.
4. Ordinary wake: re-arm the next cycle with the same short-`initial_wait` arm call if work remains in flight or X mode still needs polling.
5. Do not invent a wake from an attach-status line alone.
   Drain the queue and act only on real wake records or a real watcher reason line.
   Re-arm attaches to an existing healthy cycle when one is already present and follows its verified successor chain.
   See [`watcher-continuity.md`](../watcher-continuity.md) for the arm-layer successor and clean-close failure contract.

Copilot has no tracked primary integration: no turn-end guard, no session-start nudge, and no PreToolUse seatbelt.
Its hook surface is `${COPILOT_HOME:-$HOME/.copilot}/hooks`, outside this repository, so wiring those backstops is a global-file trust decision the captain has not made; [`turnend-guard.md`](../turnend-guard.md) records the current gap.
The only push backstop other harnesses get is therefore absent here, so this protocol plus the pull-based `bin/fm-guard.sh` warning is the whole safety net.
The firstmate-owned `agentStop` hook that a copilot crewmate spawn installs is the crewmate turn-end token hook and does not supervise a copilot primary.
Evidence: Copilot initial_wait derivation, cap, and normalization matrix
FM_ARM_CONFIRM_TIMEOUT=default ->    `bash` with `initial_wait: 15` and `detach` omitted, on:
FM_ARM_CONFIRM_TIMEOUT=20 ->    `bash` with `initial_wait: 25` and `detach` omitted, on:
FM_ARM_CONFIRM_TIMEOUT=300 ->    `bash` with `initial_wait: 30` and `detach` omitted, on:
FM_ARM_CONFIRM_TIMEOUT=not-a-number ->    `bash` with `initial_wait: 15` and `detach` omitted, on:
FM_ARM_CONFIRM_TIMEOUT=08 ->    `bash` with `initial_wait: 15` and `detach` omitted, on:
FM_ARM_CONFIRM_TIMEOUT=9223372036854775807 ->    `bash` with `initial_wait: 15` and `detach` omitted, on:
Evidence: Live malformed-timeout arm and teardown transcript
watcher: started pid=43529 (beacon fresh)
cleanup: HUP on the arm stopped its watcher child and removed its temporary output

Pipeline

Updates from git push no-mistakes

✅ **intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

✅ **Review** - passed

✅ No issues found.

✅ **Test** - passed

✅ No issues found.

  • env -u COPILOT_CLI bash tests/fm-supervision-instructions.test.sh
  • env -u COPILOT_CLI bash tests/fm-watch-checkpoint.test.sh
  • env -u COPILOT_CLI bash tests/fm-watcher-lock.test.sh
  • env -u COPILOT_CLI bash tests/fm-turnend-guard.test.sh
  • env -u COPILOT_CLI bash tests/fm-sessionstart-nudge.test.sh
  • Hermetic end-to-end session start via COPILOT_CLI=1 FM_FAKE_HARNESS=copilot run_session_start ..., capturing the actual Copilot supervision block
  • FM_ARM_CONFIRM_TIMEOUT={default,20,300,not-a-number,08,9223372036854775807} bin/fm-supervision-instructions.sh --harness copilot
  • Home-scoped live FM_ARM_CONFIRM_TIMEOUT=08 bin/fm-watch-arm.sh, then kill -HUP 43515 and verification that watcher PID 43529 stopped and temporary output was removed
  • Base-versus-target renderer comparison using git show 4363d945... and the checked-out target renderer
✅ **Document** - passed

✅ No issues found.

✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.

A Copilot primary rendered as harness `unknown` and received the generic
fallback protocol, which does not constrain how the arm is launched. Arming
`bin/fm-watch-arm.sh` through an attached shell call with a long `initial_wait`
then pinned the model turn open for the whole watcher cycle, and Copilot queues
everything the captain types while a turn is active, so chat silently stopped
being delivered until the call was stopped.

- Add `docs/supervision-protocols/copilot.md`: an attached arm with a short
  `initial_wait` that returns promptly, `detach: true` forbidden because a
  detached arm cannot be stopped and outlives the session, and the
  completion-notification wake path.
- Make the renderer's snippet selection allowlist-driven via
  `fm_harness_is_verified` instead of a hardcoded five-name case, so a verified
  adapter can no longer silently fall through to the generic protocol, and
  force the heading to `unknown` when a verified adapter's snippet is missing.
- Derive the rendered `initial_wait` from `FM_ARM_CONFIRM_TIMEOUT` so it clears
  the arm's confirmation budget without covering the watcher cycle, and wire
  copilot into the repair and ordinary-wake lines.
- Generalize `bin/fm-watch-arm.sh`'s header into a harness-agnostic invariant:
  the script blocks for the whole cycle, so the arming tool call must return
  promptly.
- Correct `docs/turnend-guard.md`, `docs/configuration.md`, and
  `docs/architecture.md`, which claimed every verified primary harness has a
  tracked turn-end integration; record Copilot's gap and why wiring it is a
  global-file trust decision.
- Add an `FM_CHECKPOINT_TIMEOUT_IMPL` seam to `bin/fm-watch-checkpoint.sh` so
  the perl fallback, which is the real path on macOS where neither `timeout`
  nor `gtimeout` exists, is testable and unknown values fail closed.
- Add regression coverage: a structural test that every verified adapter
  renders its own protocol, a Copilot renderer test, cross-harness matrix arms,
  a turn-end integration-list test, and three checkpoint fallback tests. Copy
  `bin/fm-harness-policy.sh` into the two test fixtures that hand-pick `bin/`.
Review of the previous commit surfaced four confirmed defects in it.

- The derived `initial_wait` was unbounded above. FM_ARM_CONFIRM_TIMEOUT is a
  documented operator knob for slow hosts, and the renderer propagated it
  uncapped, so FM_ARM_CONFIRM_TIMEOUT=300 rendered a 305 second attached call
  into the protocol, the repair line, and the wake line, reopening the exact
  blocked-chat incident in text that simultaneously said the call must return
  promptly. The rendered value is now capped at 30 seconds.
- The renderer normalized a malformed FM_ARM_CONFIRM_TIMEOUT to 10 while
  `bin/fm-watch-arm.sh` fed the same value straight into integer arithmetic
  under `set -u`. A non-integer aborted the arm with "unbound variable" and a
  decimal, idiomatic here because FM_ARM_ATTACH_POLL defaults to 0.5, aborted
  with an arithmetic error. The post-fork deadline runs after the watcher child
  exists, so the arm died with no status line and orphaned a live watcher while
  the protocol told the model to trust only that status line. The arm now
  applies the same normalization.
- `FM_CHECKPOINT_TIMEOUT_IMPL=perl` skipped the installed-check that the header
  and usage promised, and `auto` fell through to perl unprobed, so a host
  without perl got an opaque 127 passthrough instead of the intended exit 2.
  Both paths are now gated.
- `.agents/skills/harness-adapters/SKILL.md` still told agents not to run a
  primary session on copilot and stated that the renderer deliberately falls
  back to the unknown-harness protocol for it, which the previous commit made
  false. Because that file is loaded as operating instructions before every
  spawn and recovery, the stale text was live guidance contradicting the new
  protocol.

Also drops a contested rationale from the copilot protocol, keeping the
uncontested reason a detached arm is forbidden, and tells the model to read the
arm's output rather than assume a result when the call returns before a status
line appears.

Regression coverage: renderer cap assertions across the snippet, repair line,
and wake line; a real-process arm test proving a malformed confirm timeout still
yields a status line, verified to fail without the guard; PATH-scoped checkpoint
tests that assert the uninstalled and no-implementation paths deterministically
on any host; and an allowlist-driven session-start integration test mirroring
the turn-end one, with the copilot gap recorded in docs/sessionstart-nudge.md.

A reviewer claim that Copilot enforces a 30 second minimum initial_wait was
tested and does not hold on Copilot CLI 1.0.77: initial_wait 15 and 5 each
returned after exactly that many seconds. The protocol now notes that some
builds may raise a smaller value, which is harmless because the hold stays
bounded either way.
A second review round found two more recurrence paths in the numeric
normalization added by the previous commit, both confirmed by direct probe.

- A leading zero passes an all-digits guard but bash reads it as octal, so
  FM_ARM_CONFIRM_TIMEOUT=08, a natural way to write 8, made `$((08 + 5))` an
  arithmetic error. Under `set -eu` that aborted the renderer outright with
  "unbound variable", and because the derivation runs unconditionally it took
  down the supervision block for EVERY harness, not just copilot. Verified:
  `--harness claude` exited 1 as well. bin/fm-turnend-guard.sh calls the
  renderer with `2>/dev/null || <generic fallback>`, so this would have
  degraded silently rather than surfacing.
- A value large enough to overflow the addition wrapped negative, and a
  negative value passes the cap's `-le` test, so
  FM_ARM_CONFIRM_TIMEOUT=9223372036854775807 rendered
  `initial_wait -9223372036854775804` into the protocol and repair line.

Both shapes are now rejected to the default in the renderer and in
bin/fm-watch-arm.sh, which had inherited the same weak guard.

Also bounds the post-cap recovery guidance. Telling the model to read the arm's
output "until" a status line appears invited an unbounded read_bash loop, and
read_bash holds the turn open exactly like the arming call does, so that
reintroduced the queued-chat symptom through a different path. It is now one
short read, then treat supervision as unconfirmed and re-arm.

Regression coverage asserts the octal blast radius on an unrelated harness as
well as copilot, and that no negative wait can be rendered.
@egarlock
egarlock merged commit ad5b7d9 into main Aug 3, 2026
10 of 11 checks passed
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