From a4618fb56d483dd47f5019d33b532f7edaf9810f Mon Sep 17 00:00:00 2001 From: Israel Wilson Date: Wed, 29 Jul 2026 04:13:28 -0400 Subject: [PATCH 1/2] Revert "fix(brief): restore macOS bash 3.2 ship dispatch (#2)" This reverts commit 283884baf1fde971f5868fa278bb8a1746459e83. --- .agents/skills/afk/SKILL.md | 84 +- .agents/skills/ahoy/SKILL.md | 45 - .agents/skills/ask-user-authority/SKILL.md | 52 - .agents/skills/bearings/SKILL.md | 12 +- .agents/skills/bootstrap-diagnostics/SKILL.md | 6 +- .../firstmate-coding-guidelines/SKILL.md | 30 +- .agents/skills/firstmate-orca/SKILL.md | 5 +- .agents/skills/harness-adapters/SKILL.md | 157 +- .agents/skills/project-management/SKILL.md | 2 +- .../skills/secondmate-provisioning/SKILL.md | 24 +- .../skills/stuck-crewmate-recovery/SKILL.md | 2 +- .claude/settings.json | 17 +- .github/workflows/ci.yml | 270 +-- .github/workflows/no-mistakes-required.yml | 7 +- .gitignore | 2 - .no-mistakes.yaml | 39 +- .opencode/plugins/fm-primary-turnend-guard.js | 18 +- .opencode/plugins/fm-primary-watch-arm.js | 23 +- .opencode/plugins/lib/fm-operational-input.js | 37 - .pi/extensions/fm-calm.ts | 277 --- .pi/extensions/fm-primary-pi-watch.ts | 112 +- .pi/extensions/fm-primary-turnend-guard.ts | 15 +- .../lib/fm-calm-assistant-layout.ts | 59 - .../lib/fm-calm-operational-user-layout.ts | 136 -- .pi/extensions/lib/fm-calm-visibility.ts | 96 - .pi/extensions/lib/fm-operational-input.ts | 55 - AGENTS.md | 80 +- CONTRIBUTING.md | 34 +- README.md | 44 +- bin/backends/cmux.sh | 4 +- bin/backends/herdr-workspace-move.py | 114 - bin/backends/herdr.sh | 1160 +--------- bin/backends/tmux.sh | 78 +- bin/backends/zellij.sh | 4 +- bin/fm-afk-launch.sh | 15 +- bin/fm-afk-return.sh | 5 +- bin/fm-backend.sh | 84 +- bin/fm-bearings-snapshot.sh | 65 +- bin/fm-bootstrap.sh | 212 +- bin/fm-brief.sh | 29 +- bin/fm-classify-lib.sh | 54 +- bin/fm-claude-stop-autoarm.sh | 194 -- bin/fm-composer-lib.sh | 11 +- bin/fm-config-inherit-lib.sh | 678 +----- bin/fm-config-push.sh | 68 +- bin/fm-continuity-command-policy.mjs | 135 ++ bin/fm-continuity-pretool-check.sh | 102 + bin/fm-crew-state.sh | 88 +- bin/fm-dispatch-select.sh | 234 ++ bin/fm-doc-audience-check.sh | 269 --- bin/fm-fleet-snapshot.sh | 207 +- bin/fm-guard.sh | 8 +- bin/fm-harness.sh | 9 +- bin/fm-herdr-ci-cleanup.sh | 115 - bin/fm-herdr-session-cleanup.sh | 383 ---- bin/fm-install-herdr.sh | 96 - bin/fm-install-shellcheck.sh | 12 +- bin/fm-install-treehouse.sh | 95 - bin/fm-kimi-turnend-hook.sh | 276 --- bin/fm-lint.sh | 467 +--- bin/fm-lock.sh | 96 +- bin/fm-marker-lib.sh | 79 +- bin/fm-operational-input.sh | 252 --- bin/fm-pending-reply-lib.sh | 1033 --------- bin/fm-pr-check-migrate.sh | 40 +- bin/fm-pr-check.sh | 47 +- bin/fm-pr-lib.sh | 593 +---- bin/fm-pr-merge.sh | 6 +- bin/fm-pr-poll.sh | 98 +- bin/fm-project-mode.sh | 6 +- bin/fm-push-transition-lib.sh | 76 - bin/fm-secondmate-report.sh | 88 - bin/fm-send.sh | 90 +- bin/fm-session-lock-lib.sh | 55 - bin/fm-session-start.sh | 42 +- bin/fm-sessionstart-nudge.sh | 8 +- bin/fm-spawn.sh | 583 +---- bin/fm-subagent-pretool-check.sh | 194 -- bin/fm-supervise-daemon.sh | 153 +- bin/fm-supervision-instructions.sh | 37 +- bin/fm-supervision-lib.sh | 27 +- bin/fm-teardown.sh | 114 +- bin/fm-test-isolation-proof.sh | 498 ----- bin/fm-test-run.sh | 1417 ------------ bin/fm-tmux-lib.sh | 397 +--- bin/fm-turnend-guard-grok.sh | 13 +- bin/fm-turnend-guard.sh | 181 +- bin/fm-wake-drain.sh | 24 +- bin/fm-wake-lib.sh | 187 +- bin/fm-watch-arm.sh | 17 +- bin/fm-watch.sh | 283 +-- bin/fm-x-lib.sh | 66 - bin/fm-x-poll.sh | 45 +- docs/architecture.md | 73 +- docs/arm-pretool-check.md | 13 +- docs/calm-mode-feasibility.md | 279 --- docs/calm.md | 31 - docs/cmux-backend.md | 430 +++- docs/codex-app-backend.md | 230 +- docs/configuration.md | 112 +- docs/decision-hold-lifecycle.md | 9 +- docs/documentation-audiences.json | 327 --- docs/documentation-audiences.md | 28 - docs/examples/crew-dispatch.json | 8 +- docs/fm-test-isolation-proof.json | 190 -- docs/fm-test-isolation-proof.md | 170 -- docs/fm-test-portable-shards.md | 108 - docs/gitlab-merge-watch.md | 200 -- docs/herdr-backend.md | 1086 +++++++-- docs/orca-backend.md | 138 +- docs/scripts.md | 29 +- docs/sessionstart-nudge.md | 115 +- docs/subagent-guard.md | 367 --- docs/supervision-protocols/claude.md | 38 +- docs/supervision-protocols/codex.md | 5 +- docs/supervision-protocols/grok.md | 6 +- docs/supervision-protocols/opencode.md | 11 +- docs/supervision-protocols/pi.md | 24 +- docs/supervision-protocols/unknown.md | 4 +- docs/tmux-backend.md | 152 +- docs/turnend-guard.md | 238 +- docs/verification/runtime-backends.md | 337 --- docs/verification/supervision.md | 163 -- docs/watcher-continuity.md | 75 +- docs/wedge-alarm.md | 106 +- docs/zellij-backend.md | 271 ++- tests/fm-afk-inject-e2e.test.sh | 2 +- tests/fm-afk-inject-herdr-e2e.test.sh | 26 +- tests/fm-afk-launch.test.sh | 2 +- tests/fm-afk-pi-herdr-return-e2e.test.sh | 4 +- tests/fm-afk-return.test.sh | 15 +- tests/fm-arm-pretool-check.test.sh | 11 +- tests/fm-ask-user-authority.test.sh | 161 -- tests/fm-backend-cmux-smoke.test.sh | 2 +- tests/fm-backend-cmux.test.sh | 10 +- .../fm-backend-herdr-eventwait-smoke.test.sh | 12 +- .../fm-backend-herdr-presentation-e2e.test.sh | 1325 ----------- .../fm-backend-herdr-prune-safety-e2e.test.sh | 23 +- .../fm-backend-herdr-respawn-idem-e2e.test.sh | 2 +- tests/fm-backend-herdr-smoke.test.sh | 2 +- ...ckend-herdr-workspace-per-home-e2e.test.sh | 2 +- tests/fm-backend-herdr.test.sh | 964 -------- tests/fm-backend-orca.test.sh | 13 +- tests/fm-backend-tmux-smoke.test.sh | 60 +- tests/fm-backend-zellij-smoke.test.sh | 2 +- tests/fm-backend-zellij.test.sh | 16 +- tests/fm-backend.test.sh | 50 +- tests/fm-bearings-snapshot.test.sh | 499 ----- tests/fm-bootstrap.test.sh | 28 +- tests/fm-brief.test.sh | 44 - tests/fm-calm-pi-extension.test.sh | 1983 ----------------- tests/fm-captain-translation-contract.test.sh | 146 -- tests/fm-cd-pretool-check.test.sh | 1 - tests/fm-claude-continuity-live-e2e.test.sh | 74 + tests/fm-claude-stop-autoarm-live-e2e.test.sh | 164 -- tests/fm-claude-stop-autoarm.test.sh | 425 ---- tests/fm-composer-ghost.test.sh | 350 +-- tests/fm-composer-lib.test.sh | 2 +- tests/fm-continuity-pretool-check.test.sh | 119 + tests/fm-crew-state.test.sh | 149 +- tests/fm-daemon.test.sh | 184 +- tests/fm-dispatch-select.test.sh | 186 ++ tests/fm-documentation-audiences.test.sh | 160 -- tests/fm-fleet-snapshot-view.test.sh | 173 +- tests/fm-gate-refuse.test.sh | 8 +- tests/fm-herdr-lab.test.sh | 6 +- tests/fm-herdr-session-cleanup-e2e.test.sh | 146 -- tests/fm-herdr-session-cleanup.test.sh | 309 --- tests/fm-install-herdr.test.sh | 106 - tests/fm-instruction-owners.test.sh | 71 +- tests/fm-kimi-harness.test.sh | 694 ------ tests/fm-lint.test.sh | 313 +-- tests/fm-nm-test-contract.test.sh | 127 -- tests/fm-opencode-primary-live-e2e.test.sh | 202 +- tests/fm-operational-input.test.sh | 160 -- tests/fm-pending-reply.test.sh | 934 -------- tests/fm-pi-primary-live-e2e.test.sh | 183 +- tests/fm-pi-primary-types.test.sh | 20 +- tests/fm-pi-watch-extension.test.sh | 184 +- tests/fm-pr-check-security.test.sh | 737 +----- tests/fm-secondmate-harness.test.sh | 1086 +-------- tests/fm-secondmate-lifecycle-e2e.test.sh | 1 - tests/fm-secondmate-liveness.test.sh | 338 +-- tests/fm-secondmate-sync.test.sh | 5 - tests/fm-send-popup-settle.test.sh | 4 +- ...m-send-secondmate-marker-herdr-e2e.test.sh | 4 +- tests/fm-send-secondmate-marker.test.sh | 58 +- tests/fm-send-settle.test.sh | 4 +- tests/fm-send-strict.test.sh | 5 +- tests/fm-session-start.test.sh | 467 +--- tests/fm-sessionstart-nudge.test.sh | 15 +- tests/fm-shared-captain-inheritance.test.sh | 2 +- tests/fm-spawn-dispatch-profile.test.sh | 22 +- tests/fm-spawn-worktree-settle.test.sh | 147 -- tests/fm-subagent-pretool-check.test.sh | 288 --- tests/fm-supervision-events.test.sh | 19 +- tests/fm-supervision-instructions.test.sh | 51 +- tests/fm-tangle-guard.test.sh | 2 +- tests/fm-teardown.test.sh | 110 - tests/fm-test-isolation-proof.test.sh | 272 --- tests/fm-test-run.test.sh | 696 ------ tests/fm-tmux-submit-busy.test.sh | 266 --- tests/fm-transition-lib.test.sh | 2 +- tests/fm-turnend-guard.test.sh | 230 +- tests/fm-wake-daemon-lifecycle-e2e.test.sh | 2 +- tests/fm-wake-queue.test.sh | 205 +- tests/fm-watch-triage.test.sh | 171 +- tests/fm-watcher-lock.test.sh | 88 +- tests/fm-x-mode.test.sh | 110 +- tests/herdr-test-safety.sh | 2 +- tests/lib.sh | 12 +- tests/no-mistakes-required-workflow.test.sh | 96 - tests/secondmate-helpers.sh | 5 +- tests/wake-helpers.sh | 32 +- 214 files changed, 4755 insertions(+), 30882 deletions(-) delete mode 100644 .agents/skills/ahoy/SKILL.md delete mode 100644 .agents/skills/ask-user-authority/SKILL.md delete mode 100644 .opencode/plugins/lib/fm-operational-input.js delete mode 100644 .pi/extensions/fm-calm.ts delete mode 100644 .pi/extensions/lib/fm-calm-assistant-layout.ts delete mode 100644 .pi/extensions/lib/fm-calm-operational-user-layout.ts delete mode 100644 .pi/extensions/lib/fm-calm-visibility.ts delete mode 100644 .pi/extensions/lib/fm-operational-input.ts delete mode 100755 bin/backends/herdr-workspace-move.py delete mode 100755 bin/fm-claude-stop-autoarm.sh create mode 100755 bin/fm-continuity-command-policy.mjs create mode 100755 bin/fm-continuity-pretool-check.sh create mode 100755 bin/fm-dispatch-select.sh delete mode 100755 bin/fm-doc-audience-check.sh delete mode 100755 bin/fm-herdr-ci-cleanup.sh delete mode 100755 bin/fm-herdr-session-cleanup.sh delete mode 100755 bin/fm-install-herdr.sh delete mode 100755 bin/fm-install-treehouse.sh delete mode 100755 bin/fm-kimi-turnend-hook.sh delete mode 100755 bin/fm-operational-input.sh delete mode 100755 bin/fm-pending-reply-lib.sh delete mode 100644 bin/fm-push-transition-lib.sh delete mode 100755 bin/fm-secondmate-report.sh delete mode 100644 bin/fm-session-lock-lib.sh delete mode 100755 bin/fm-subagent-pretool-check.sh delete mode 100755 bin/fm-test-isolation-proof.sh delete mode 100755 bin/fm-test-run.sh delete mode 100644 docs/calm-mode-feasibility.md delete mode 100644 docs/calm.md delete mode 100644 docs/documentation-audiences.json delete mode 100644 docs/documentation-audiences.md delete mode 100644 docs/fm-test-isolation-proof.json delete mode 100644 docs/fm-test-isolation-proof.md delete mode 100644 docs/fm-test-portable-shards.md delete mode 100644 docs/gitlab-merge-watch.md delete mode 100644 docs/subagent-guard.md delete mode 100644 docs/verification/runtime-backends.md delete mode 100644 docs/verification/supervision.md delete mode 100644 tests/fm-ask-user-authority.test.sh delete mode 100755 tests/fm-backend-herdr-presentation-e2e.test.sh delete mode 100755 tests/fm-calm-pi-extension.test.sh create mode 100755 tests/fm-claude-continuity-live-e2e.test.sh delete mode 100755 tests/fm-claude-stop-autoarm-live-e2e.test.sh delete mode 100755 tests/fm-claude-stop-autoarm.test.sh create mode 100755 tests/fm-continuity-pretool-check.test.sh create mode 100755 tests/fm-dispatch-select.test.sh delete mode 100755 tests/fm-documentation-audiences.test.sh delete mode 100755 tests/fm-herdr-session-cleanup-e2e.test.sh delete mode 100755 tests/fm-herdr-session-cleanup.test.sh delete mode 100755 tests/fm-install-herdr.test.sh delete mode 100755 tests/fm-kimi-harness.test.sh delete mode 100755 tests/fm-nm-test-contract.test.sh delete mode 100755 tests/fm-operational-input.test.sh delete mode 100755 tests/fm-pending-reply.test.sh delete mode 100755 tests/fm-spawn-worktree-settle.test.sh delete mode 100755 tests/fm-subagent-pretool-check.test.sh delete mode 100755 tests/fm-test-isolation-proof.test.sh delete mode 100755 tests/fm-test-run.test.sh delete mode 100755 tests/fm-tmux-submit-busy.test.sh delete mode 100755 tests/no-mistakes-required-workflow.test.sh diff --git a/.agents/skills/afk/SKILL.md b/.agents/skills/afk/SKILL.md index 21b7cdd311..4a09c3a076 100644 --- a/.agents/skills/afk/SKILL.md +++ b/.agents/skills/afk/SKILL.md @@ -1,8 +1,6 @@ --- name: afk -description: >- - Enter away-mode supervision when the captain invokes /afk, says they are going afk, `state/.afk` exists, an incoming message starts with `FM_INJECT_MARK`, or any `state/.subsuper-*` marker is involved. - It sets a durable away-mode flag so the sub-supervisor daemon can self-handle routine wakes and escalate captain-relevant events plus bounded declared-external-wait rechecks as batched digests during walk-away stretches, then exits automatically when any real unmarked message returns firstmate to full per-wake responsiveness. +description: Enter away-mode supervision. Use when the user invokes /afk (e.g. "/afk", "/afk back in an hour", "going afk"). Sets a durable away-mode flag so the sub-supervisor daemon can self-handle routine wakes and escalate captain-relevant events plus bounded declared-external-wait rechecks as batched digests, cutting supervision token cost during walk-away stretches. Exit is automatic; any real (unmarked) message returns to full per-wake responsiveness. user-invocable: true metadata: internal: true @@ -41,8 +39,8 @@ batched digest rather than per-wake injections. in as `FM_SUPERVISOR_TARGET` so the daemon injects into the captain, not its own new pane. **Never manufacture a terminal by splitting the captain's active pane** (`herdr pane split`): a split co-tenants the tab and visibly - shrinks the captain's pane (docs/herdr-backend.md "Away-mode supervisor - support"). + shrinks the captain's pane (docs/herdr-backend.md "Away-mode daemon terminal + launch"). Both paths share `bin/fm-afk-start.sh` as the daemon entry. The native path tells it that the launcher already prepared lifecycle state; the terminal-backed path lets the entry perform its existing state setup inside the new terminal. It exits immediately if the identity-backed daemon lock already names a live process, otherwise it execs `bin/fm-supervise-daemon.sh` in the foreground. @@ -58,13 +56,13 @@ batched digest rather than per-wake injections. No `/back` is needed. The first genuine message is the return signal: -- A message **without** the current operational prefix or a legacy bare marker, and **not** starting with `/afk` -> the captain is back. +- A message **without** the sentinel marker and **not** starting with `/afk` -> the captain is back. Run `bin/fm-afk-return.sh` before acting on the message that brought the captain back. That script owns correct-ordered daemon shutdown, durable wake draining, escalation and wedge evidence, and the return-catch-up gate. If it reports a firstmate-actionable `blocked:` event, remediate it immediately through the normal lifecycle, or explicitly reclassify it with a durable reason and close its decision key with `resolved [key=...]`, then run `bin/fm-afk-return.sh check`. Once the daemon stops, resume full per-wake responsiveness through the emitted primary-harness supervision protocol while blocker handling proceeds, so the gate never creates a blind wait. Do not answer a Bearings request or perform any other ordinary captain work until the check exits successfully. -- A message **with** the current operational prefix (`FM_OPERATIONAL_PREFIX`, U+2063 INVISIBLE SEPARATOR followed by `FIRSTMATE_OP: `), or a legacy bare `FM_INJECT_MARK` daemon escalation -> stay afk and process it. +- A message **with** the sentinel marker (`FM_INJECT_MARK`, U+2063 INVISIBLE SEPARATOR) -> it is a daemon escalation; stay afk and process it. - Re-invoking `/afk` while already away -> stay afk (refresh the flag); this does **not** trigger an exit. @@ -73,18 +71,16 @@ a false exit is self-correcting (the captain re-runs `/afk`). ## Orthogonal to approval authority -afk changes how aggressively firstmate surfaces things, **not who approves what**. -"Away" never means "approves more" or "approves less." -A PR ready for merge or a needs-decision finding keeps the same configured authority and exceptions from `AGENTS.md` section 7, while anything requiring the captain still waits for the captain's explicit word. -The daemon only batches the notification. +afk changes how aggressively firstmate surfaces things, **not who approves +what**. "Away" never means "approves more." A PR ready for merge, a +needs-decision finding, or anything destructive still waits for the captain's +explicit word - the daemon just batches the notification. -## Operational prefix contract +## Sentinel marker contract -The daemon constructs every current injection as the `away-supervisor` kind owned by `bin/fm-operational-input.sh`, beginning with `FM_OPERATIONAL_PREFIX`: `FM_INJECT_MARK` (U+2063 INVISIBLE SEPARATOR) followed by the stable `FIRSTMATE_OP: ` label. -The bare `FM_INJECT_MARK` form remains accepted for legacy daemon escalations during rollout. -U+2063 has no normal keyboard keystroke and survives terminal transport as UTF-8 text. +The daemon prefixes every injection with `FM_INJECT_MARK` (U+2063 INVISIBLE SEPARATOR), which has no normal keyboard keystroke and survives terminal transport as UTF-8 text. This is how firstmate tells a daemon escalation apart from a real message in the same pane. -The operational prefix travels with the message text; it does not rely on harness-level typed-vs-injected detection, which is not portable across claude, codex, opencode, pi, grok, and kimi. +The marker travels with the message text; it does not rely on harness-level typed-vs-injected detection, which is not portable across claude, codex, opencode, pi, and grok. ## Busy-guard and composer guard @@ -96,7 +92,7 @@ backend (tmux or herdr; see "Auto-discovered supervisor pane" below): - **Composer-state guard** - `inject_msg` reads the full `empty`/`pending`/`unknown` verdict from `fm_backend_composer_state` and injects only when it is affirmatively `empty`. `pending` means real unsubmitted text, while `unknown` includes an unreadable pane and a bare shell prompt left after the agent exits, so both defer. The shared `bin/fm-composer-lib.sh` owns the content decision after each backend captures and structurally identifies its own composer row. - It preserves idle bordered composers such as claude's `│ > … │` and bare agent glyphs as empty, but a bare shell glyph is unknown unless inside a genuine bordered composer box; see `docs/herdr-backend.md` "Composer and injection safety" for the complete contract. + It preserves idle bordered composers such as claude's `│ > … │` and bare agent glyphs as empty, but a bare shell glyph is unknown unless inside a genuine bordered composer box; see `docs/herdr-backend.md` "Composer-emptiness safety" for the complete contract. `pane_input_pending` remains the tested predicate for callers that only need to know whether real unsubmitted text is present, but it is insufficient for an injection-safety decision because it cannot distinguish `empty` from `unknown`. Either condition, or any composer verdict other than `empty`, defers the injection; the buffered escalation survives in `state/.subsuper-escalations` and is retried on the next housekeeping tick. @@ -110,7 +106,7 @@ If that submit cannot be confirmed, it raises a loud, rate-limited wedge alarm: an ERROR in the daemon log, a durable `state/.subsuper-inject-wedged` marker (surface it on the "while you were out" catch-up if present), a tmux status-line flash when applicable, and a configurable backend-independent active alert. -`docs/wedge-alarm.md` owns the alert channel setup, and `docs/verification/supervision.md` "Wedge-alarm channels" owns active evidence. +`docs/wedge-alarm.md` owns the alert channel setup and verification record. So a guard false-positive becomes a visible stall, never an unbounded silent no-op. ## Submit model @@ -128,21 +124,6 @@ A bordered-empty or ghost-only composer is recognized as empty where that backen when a steer's Enter is positively swallowed, so firstmate learns an instruction did not land instead of leaving it unsubmitted. -**Busy-queued Enter exception (tmux backend, opencode 1.18.4).** While opencode -is mid-turn, Enter is accepted and queued for after the current turn but the -composer keeps showing the typed text the whole time, so the cleared-composer -check alone false-positives on a swallowed Enter for every steer sent to a -busy opencode pane. The shared `fm_tmux_submit_enter_core` falls back to -`fm_pane_is_busy` once the Enter-retry budget is spent: a busy pane means the -Enter was accepted and queued (reported as `empty` so the caller does not -re-send), while an idle pane keeps `pending` as a genuine swallow. The -strict-buffer-clears-only-on-`empty` policy above still holds for the daemon -and the lenient-`pending`-fails-for-`fm-send` policy still holds for steer -verification - this exception is a busy-queue is treated as a delivered -Enter, not a swallowed one. The herdr adapter observes the same opencode -behavior but needs a separate fix; the gap is recorded in -`docs/herdr-backend.md` rather than papered over here. - ## Classification policy The daemon wraps `fm-watch.sh`, runs the watcher as a child, classifies each @@ -154,27 +135,28 @@ While `state/.afk` exists the daemon owns the watcher, so the watcher reverts to Classify each wake this way: -- `signal` with a terminal captain verb (`done:`, `needs-decision:`, `blocked:`, or `failed:`) -> escalate. - A nonterminal progress verb remains nonterminal even when its prose contains a legacy free-text token such as `PR ready`, `checks green`, `ready in branch`, or `merged`; only a bare legacy line with such a token escalates. - Other signals with no captain-relevant status -> self-handle. +- `signal` whose status content has no captain-relevant verb + (`done:|needs-decision:|blocked:|failed:|PR ready|checks green|ready in branch|merged`) + -> self-handle. Captain-relevant verb -> escalate. - `signal` or `stale` for a declared `paused:` external wait -> self-handle and track the pause rather than a wedge. If it remains declared and idle past `FM_PAUSE_RESURFACE_SECS` (default 3600s), housekeeping sends one awaiting-external recheck and resets the pause window. - `check` -> always escalate. Check scripts print only when firstmate should wake. -- `stale` with a terminal status or bare legacy captain-relevant line -> escalate. - Nonterminal progress remains transient even when its prose contains a legacy free-text token or its seen-status marker already matches, so record a marker and self-handle. - If the pane is still idle past `FM_STALE_ESCALATE_SECS` (default 240s), housekeeping escalates it as a possible wedge. - This bounds wedge-detection latency to the threshold plus a tick: a delay, never a loss. - Healthy crewmates are autonomous and do not wait on firstmate mid-task. +- `stale` with a terminal status -> escalate. Non-terminal stale is transient: + record a marker and self-handle. If the pane is still idle past + `FM_STALE_ESCALATE_SECS` (default 240s), housekeeping escalates it as a + possible wedge. This bounds wedge-detection latency to the threshold plus a + tick: a delay, never a loss. Healthy crewmates are autonomous and do not wait + on firstmate mid-task. - `heartbeat` -> self-handle. The daemon runs its own cheap bash fleet scan every `FM_HEARTBEAT_SCAN_SECS` (default 300s) as the catch-all for a captain-relevant status line the per-wake classifier might miss. - Unknown reason, or any uncertainty -> escalate fail-safe. Escalations are buffered up to `FM_ESCALATE_BATCH_SECS` (default 90s; 0 = -immediate) and flushed as one single-line digest prefixed with the current -operational prefix, carrying pre-read status summaries and a recommended action. +immediate) and flushed as one single-line digest prefixed with the sentinel +marker, carrying pre-read status summaries and a recommended action. The single-line format makes the submission unambiguous across harnesses, and -the operational prefix lets firstmate distinguish it from a real captain message. +the marker lets firstmate distinguish it from a real captain message. ## Injection hardening @@ -205,13 +187,13 @@ the operational prefix lets firstmate distinguish it from a real captain message cleared. For herdr's normal idle-baseline path it means native agent-state observed a real turn start; herdr uses the ANSI-aware structural classifier for the pre-injection composer guard and fallback paths. This lets ghost-only or bordered-empty composers count as empty where a composer read is the active confirmation signal. -- **Marker strip** - `strip_injection_marker` removes the current operational - prefix or legacy bare marker before classification or relay, so the digest - text firstmate sees is clean. +- **Marker strip** - `strip_injection_marker` removes the sentinel prefix before + classification or relay, so the digest text firstmate sees is clean. - **Portable singleton lock** - the daemon uses the repo's portable lock helper (`fm-wake-lib.sh`) instead of `flock`, which is absent on macOS. -- **Dedupe across signal/stale/scan** - `classify_signal` and terminal `classify_stale` paths check the seen-status marker before escalating, so a captain-relevant status escalated by one path is not re-escalated by another in the same digest. - The marker does not clear or suppress possible-wedge aging for a nonterminal progress line. +- **Dedupe across signal/stale/scan** - `classify_signal` and `classify_stale` + both check the seen-status marker before escalating, so a status escalated by + one path is not re-escalated by another in the same digest. - **Auto-discovered supervisor pane** - the daemon resolves its own BACKEND (tmux vs herdr) and TARGET independently, mirroring `bin/fm-backend.sh`'s own runtime auto-detection. Backend: `FM_SUPERVISOR_BACKEND` @@ -225,14 +207,14 @@ the operational prefix lets firstmate distinguish it from a real captain message backends, including zellij, orca, and cmux, are not yet supported as supervisor backends; the daemon refuses loudly at startup instead of misapplying tmux primitives to a pane that isn't one - (docs/herdr-backend.md "Away-mode supervisor support"). + (docs/herdr-backend.md "Away-mode daemon: herdr supervisor-pane support"). ## Stale-artifact lifecycle Treat `state/.subsuper-escalations`, its `.since` sidecar, and `state/.subsuper-inject-wedged` as session-scoped delivery artifacts, not as the durable work record. Always enter through `bin/fm-afk-launch.sh`, which clears prior-session artifacts only for a fresh entry and preserves the current session's buffer on refresh. Always exit through `bin/fm-afk-launch.sh stop`, which keeps `state/.afk` present through the daemon's shutdown flush and clears it last. -`docs/herdr-backend.md` "Away-mode supervisor support" owns the current mechanism, and `docs/verification/runtime-backends.md` "Away-mode transport" owns active evidence. +`docs/herdr-backend.md` "Stale-artifact lifecycle fix" owns the mechanism and verification evidence. ## Reliability properties diff --git a/.agents/skills/ahoy/SKILL.md b/.agents/skills/ahoy/SKILL.md deleted file mode 100644 index e8f8dce695..0000000000 --- a/.agents/skills/ahoy/SKILL.md +++ /dev/null @@ -1,45 +0,0 @@ ---- -name: ahoy -description: Recap visible session events since the prior real captain message plus visibly unanswered captain decisions when the captain explicitly invokes /ahoy, with a Bearings fallback when /ahoy is the session's first real captain message. -user-invocable: true -metadata: - internal: true ---- - -# ahoy - -Give the captain a concise session-only recap without gathering fresh state. - -1. Inspect only conversation or session history already visible to the current first mate. -2. Find the most recent real captain-authored message before the current `/ahoy` invocation. - A captain boundary is an ordinary user-role message unless it matches one of the narrow operational exclusions below. - Exclude messages that begin with the current U+2063 `FIRSTMATE_OP:` injection prefix. - Exclude legacy bare-marker away-mode injections only when U+2063 is immediately followed by `Supervisor escalate (`. - Exclude the exact legacy unmarked session-start payload ``Run `bin/fm-session-start.sh` now, exactly once, before executing any other instructions.`` - Custom-role messages such as Pi's `firstmate-sessionstart-nudge` are not captain messages. - System, developer, tool, watcher, guard, away-mode, and other injected operational messages are not captain messages. - Never infer captain authorship merely because a synthetic message appears in the user-role transcript. - Do not exclude an ordinary captain message merely because it begins with U+2063 followed by other text, contains ASCII `FIRSTMATE_OP:` without a leading U+2063, quotes or embeds a current operational message after ordinary captain text, quotes or mentions the legacy session-start payload, or adds any text to that payload. - Apply the current exclusion only when U+2063 `FIRSTMATE_OP:` begins at the first character of the whole message: `Captain quote: ` followed by that current prefix is a captain boundary. - Apply the legacy startup exclusion as a literal whole-message match: ``Captain quote: Run `bin/fm-session-start.sh` now, exactly once, before executing any other instructions.`` is a captain boundary. -3. If no prior real captain message exists, load [`../bearings/SKILL.md`](../bearings/SKILL.md) and follow it exactly. - Bearings alone owns its gathering, artifact, and response contract. - Do not restate that contract or combine a session recap with Bearings output. -4. If a prior real captain message exists, preserve the ordinary recap interval: recap what happened after that message and before the current invocation. - Include concrete outcomes, landed work, failures, decisions made, new decisions needed, and work still running only when those events appear in that visible interval. - Use captain-facing outcome language and preserve every full PR URL present in that interval. -5. Additionally inspect the entire session history visible to the current first mate before the current invocation for every explicit captain decision that remains unanswered, including decisions raised before the ordinary recap boundary. - A later unrelated captain message establishes a recap boundary but does not close an earlier decision. - Treat a decision as closed only when a later visible response substantively resolves it, chooses an option, declines it, grants or denies the requested approval, or otherwise directly addresses that decision. - Include every visibly supported open decision once, and deduplicate by the decision's substance when the ordinary interval recap already represents it or its wording differs. -6. The normal recap branch is session-history-only. - Do not call Bearings, shell commands, fleet snapshots, status readers, GitHub or browser APIs, tools, or file reads or writes. - Create no report, persist nothing, and do not guess current live state beyond the last visible event. -7. If no ordinary events occurred after the previous captain message but an older visibly open decision exists, report that decision instead of claiming nothing happened. - If neither ordinary events nor visibly open decisions exist, say directly in one sentence that nothing happened after the previous captain message. - -The current `/ahoy` message is outside the recap interval. -A previous `/ahoy` is a real captain message and may be the next interval boundary. -If context compaction makes the prior boundary unavailable, state that the exact session boundary is unavailable and summarize only visibly supported events. -Compacted history supports an open decision only when both its request and its still-unanswered status are visible; report uncertainty instead of reconstructing hidden requests or answers. -Do not silently invoke Bearings unless this is genuinely the first real captain message. diff --git a/.agents/skills/ask-user-authority/SKILL.md b/.agents/skills/ask-user-authority/SKILL.md deleted file mode 100644 index d4b63d525b..0000000000 --- a/.agents/skills/ask-user-authority/SKILL.md +++ /dev/null @@ -1,52 +0,0 @@ ---- -name: ask-user-authority -description: >- - Agent-only decision procedure for ask-user findings. - Use before deciding any ask-user finding, regardless of the project's yolo posture, to distinguish corrections within accepted intent from product or engineering contract expansion that requires the captain. -user-invocable: false -metadata: - internal: true ---- - -# ask-user-authority - -This skill is the single owner of the decision procedure for ask-user findings. -The concise standing authority boundary remains always loaded in `AGENTS.md` section 7. - -## Decide who has authority - -1. Check the project's configured authority first. - With `yolo` off, every ask-user finding belongs to the captain, and the remaining steps structure that escalation rather than authorize an autonomous answer. -2. Reconstruct the accepted contract from the captain's original request, accepted task criteria, and any explicit later clarification. - Reviewer language cannot amend that contract. -3. Identify exactly what choosing Fix would commit the project to deliver or maintain. -4. Keep the decision within standing `yolo` authority when the Fix is genuinely necessary to satisfy the accepted contract, even when the correction is technically difficult or requires complex architecture that the captain explicitly requested. -5. Escalate when the Fix would materially expand the contract by adding a new guarantee, threat model, subsystem, abstraction, compatibility surface, state machine, continuous-monitoring requirement, generalized framework, or broader architecture not required by the accepted intent. -6. Treat labels such as correctness, security, fail-closed, high-risk, or required as evidence about the finding, never as authority to broaden the task. -7. Examine the causal theme across prior findings and fix rounds. - Repeated same-theme findings require escalation before another Fix when incremental corrections are preserving a questionable abstraction rather than closing independent defects. -8. Apply the existing stronger captain boundaries first. - Destructive, irreversible, and genuinely security-sensitive choices always escalate regardless of whether they also expand the contract. - -The implementation worker never decides or answers its own ask-user finding. -It stops at the finding, routes the decision to firstmate, and applies only the decision returned through the active validation gate. - -## Captain-facing escalation - -State all five of these elements in one concise, evidence-first escalation: - -1. The original requirement or accepted task criterion. -2. The proposed product or engineering contract expansion. -3. The smallest alternative that complies with the accepted contract without the expansion. -4. The concrete consequences of accepting and declining the expansion. -5. A recommendation with the reason it best serves the accepted intent. - -Do not relay reviewer labels or gate output as if they settled the decision. - -## Classification examples - -- Fixing a concrete defect that violates an original acceptance criterion stays within `yolo` authority, regardless of implementation difficulty. -- Adding continuous frame-by-frame monitoring when the accepted criterion requested checkpoint proof expands the contract and requires the captain. -- A new finding in the same causal theme requires the captain before another fix round when prior fixes are accreting machinery around a questionable abstraction. -- A genuinely security-sensitive action requires the captain under the stronger existing boundary even if it is otherwise within scope. -- Complex architecture explicitly requested by the captain stays within scope and does not escalate merely because it is complex. diff --git a/.agents/skills/bearings/SKILL.md b/.agents/skills/bearings/SKILL.md index b2804c7290..f543ebf32d 100644 --- a/.agents/skills/bearings/SKILL.md +++ b/.agents/skills/bearings/SKILL.md @@ -26,7 +26,6 @@ It never tears down a task, merges a PR, dispatches new work, or mutates any tas For registered secondmates, use the snapshot's structured-home classification and provenance; a parent event or bounded terminal contradiction is fallback evidence, never authority over readable structured home state. Structured captain-held decisions come from `decision-hold-lifecycle` and appear under `decisions_open`; do not scrape reports or visual-review artifacts to supplement them. A queued item under `gates` only becomes "next work" when its blocker is gone and its time/date gate has arrived; until then it stays queued with the reason. - The `(main-inventory)` gate is an action-free integrity warning rather than queued work: render it under Charted Next with the related `omitted` disclosure, never invent an Underway row from backlog-only state, and never move it into Captain's Call. 2. **Compose the detailed report file around the four-section spine, adding the richer detail the chat leaves out.** The gather step is deterministic; your judgment is scoped to the last mile only - ranking the command's facts by what matters right now and writing the scannable prose. @@ -36,7 +35,7 @@ It never tears down a task, merges a PR, dispatches new work, or mutates any tas - **Captain's Call** - every open decision summarized with its options from the structured decision record, plus each PR ready to merge and each needed credential or login, every PR with the full `https://...` URL, never a bare `#number`. - **Recently Landed** - the bounded current recent-completions baseline from structured state across the main fleet and every registered secondmate home, rendered in full on every run. - **Underway** - each live direct report making progress, with its current state, and the plans / main pickup pointers worth reopening (`data//report.md` files, `.lavish/*.html` boards). - - **Charted Next** - queued or gated work, including any main-inventory integrity warning, with each item's blocker, date, or integrity reason. + - **Charted Next** - queued or gated next work, with each item's blocker or date reason. 3. **Write the dated report file so it persists, then surface the mandatory four-section digest in chat.** - Write the full report to `data/status-report-.md` using today's date. @@ -56,7 +55,7 @@ Every `/bearings` chat response renders EXACTLY these four sections, in THIS ord Empty-state: "No recent completions are in the current baseline." 3. **Underway** - live work progressing on its own, one line of current state per direct report. Empty-state: "Nothing is underway." -4. **Charted Next** - queued or gated work waiting on the fleet or a date, plus action-free fleet-integrity warnings, never on the captain. +4. **Charted Next** - queued or gated work waiting on the fleet or a date, never on the captain. Empty-state: "Nothing is queued." Rules that keep the contract unambiguous: @@ -64,10 +63,9 @@ Rules that keep the contract unambiguous: - Every section ALWAYS renders, even when empty, with its short empty-state sentence; never omit a section. - Every report and chat digest is a complete current snapshot, never a delta against a prior report. - Recently Landed always renders the bounded current baseline, even when the same completions appeared in an earlier report. -- The four buckets are mutually exclusive, so every item is forced into exactly one: needs-your-action is Captain's Call, done is Recently Landed, self-progressing is Underway, and not-yet-started work or an action-free fleet-integrity warning is Charted Next. +- The four buckets are mutually exclusive, so every item is forced into exactly one: needs-your-action is Captain's Call, done is Recently Landed, self-progressing is Underway, not-yet-started is Charted Next. - The strict boundary keeps action-free items OUT of Captain's Call: a working or validating task, a queued item blocked on another task or a date, landed work, a completed scout's report pointer, a declared `paused:` external wait, and a bare recorded PR with no merge-ready signal each belong to one of the other three sections, never Captain's Call. -- A secondmate's own row appears Underway only for `active_child_work`; `externally_held` belongs in Charted Next, and `unknown` belongs there as an unavailable-state gate unless its reason requires the captain's action. -- Do not suppress separately projected decisions, landed records, or gates from a `partial-structured` home merely because that secondmate's own row is `unknown`. +- A secondmate appears Underway only for `active_child_work`; `externally_held` belongs in Charted Next, and `unknown` belongs there as an unavailable-state gate unless its reason requires the captain's action. - The chat follows `AGENTS.md` section 9 and carries one scannable line per item, each PR as the full `https://...` URL; detailed decisions, plans, full gate reasons, and evidence live only in the report file, which the chat links to, so the chat stays materially shorter than that file. ## Tone and content rules @@ -80,4 +78,4 @@ Rules that keep the contract unambiguous: This skill is read-mostly and changes no fleet state. Do not tear down a task, merge a PR, dispatch queued work, or mutate any `state/` or `data/` file other than the single report file as a side effect of generating the brief. -If the state you read suggests an action - a PR ready to merge, a queued item whose gate has arrived, or a needs-decision finding - name it in its section and leave the action to the normal lifecycle and configured authority rather than taking it from inside this skill. +If the state you read suggests an action - a PR ready to merge, a queued item whose gate has arrived, a needs-decision finding - name it in its section (a captain action under "Captain's Call", queued or gated work under "Charted Next") and let the captain decide, rather than taking the action from inside this skill. diff --git a/.agents/skills/bootstrap-diagnostics/SKILL.md b/.agents/skills/bootstrap-diagnostics/SKILL.md index 2b70879941..102071b58e 100644 --- a/.agents/skills/bootstrap-diagnostics/SKILL.md +++ b/.agents/skills/bootstrap-diagnostics/SKILL.md @@ -20,14 +20,14 @@ When any diagnostic needs captain attention, report the plain consequence and re For `treehouse`, this also covers an installed version whose `treehouse get` lacks `--lease`; treat it as an upgrade request. For `no-mistakes`, this also covers an installed version older than 1.31.2, because crewmate validation briefs delegate gate mechanics to no-mistakes' version-matched guidance. For `tasks-axi`, this also covers an installed build that fails the compatibility probe (`docs/configuration.md` "Backlog backend" owns the definition); `config/backlog-backend=manual` only suppresses the verbose `BOOTSTRAP_INFO: tasks-axi available` fact, not this missing-tool report. - For `quota-axi`, bootstrap requires it because firstmate reads its current output directly before resolving every crew-dispatch profile array; without it, report the missing requirement and do not choose around an unexamined candidate. + For `quota-axi`, bootstrap requires it because crew-dispatch `quota-balanced` may call it; `bin/fm-dispatch-select.sh` still degrades at runtime when quota data is unavailable. - `MISSING_MANUAL: (instructions: )` - tell the captain why the tool is required and give them the printed instructions URL, but do not pass the tool to `bin/fm-bootstrap.sh install`; wait for the captain to complete the manual installation, then rerun session start to confirm the dependency is present. - `BACKEND_INVALID: (known: )` - the resolved runtime backend has no verified dependency or lifecycle contract, so do not dispatch work until the invalid `FM_BACKEND` or `config/backend` value is corrected to one of the listed backends. - `NEEDS_GH_AUTH` - ask the captain to run `! gh auth login` (interactive; you cannot run it for them). - `TANGLE: ` - the primary checkout is stranded on a feature branch instead of its default branch; `AGENTS.md` section 8 explains why this guard exists and what it protects. The work is safe on that branch ref; restore the primary to its default branch with the printed `git -C checkout `, then re-validate that branch in a proper worktree. This is the only sanctioned firstmate-initiated git write to the primary, and it is a non-destructive branch switch that strands nothing. -- `CREW_DISPATCH: invalid config/crew-dispatch.json - ` - the optional dispatch profile file exists but failed low-cost bootstrap validation; stop profile-based dispatch, report the actionable error, and require correction of the malformed schema, unverified harness name, or invalid harness/effort pair rather than falling back around it or selecting a bad profile. +- `CREW_DISPATCH: invalid config/crew-dispatch.json - ` - the optional dispatch profile file exists but failed low-cost bootstrap validation; continue with the normal fallback chain, resolve and pass the chosen fallback harness explicitly while the file remains present, fix the malformed schema, unverified harness name, unknown selector, or invalid harness/effort pair when convenient, and do not select a bad profile. - `FLEET_SYNC: : skipped: ` - a benign one-off skip (offline, no origin, local-only); bootstrap continued, investigate only if it blocks work. A skip can also report the bounded fleet-refresh timeout (`FM_FLEET_SYNC_BOOTSTRAP_TIMEOUT`, or a fleet-size-aware default with a 20 second floor); a timeout never blocks startup. - `FLEET_SYNC: : recovered: ` - the clone had drifted onto a clean detached HEAD holding no unique commits and the sync self-healed it (re-attached the default branch and fast-forwarded); no action needed, it is reported only so the self-heal is visible. @@ -44,7 +44,7 @@ When any diagnostic needs captain attention, report the plain consequence and re - Any other `PR_CHECK_MIGRATION:` refusal means migration did not complete safely, whether because watcher exclusion, a private path, a diagnostic, quarantine validation, or marker publication could not be proved. Keep each affected poll unavailable, inspect the named private state path, and do not bypass the migration or execute a quarantined artifact; a completed safe-scan marker allows unrelated authenticated polls to continue while private repair remains pending. - `SECONDMATE_SYNC: secondmate : skipped: ` - the local-HEAD secondmate sync left a live secondmate home on its existing checkout because the home was dirty, diverged, unsafe, on the wrong branch, missing the primary target commit, or otherwise not fast-forwardable, or because inherited local-material propagation failed; bootstrap continued, but inspect the reason because the secondmate's tracked instructions, inherited settings, or shared captain preferences may be stale after a primary update. -- `SECONDMATE_LIVENESS: secondmate : skipped: |respawn failed after : ` - the session-start liveness sweep could not guarantee that the registered secondmate is running a real agent process. +- `SECONDMATE_LIVENESS: secondmate : skipped: |respawn failed: ` - the session-start liveness sweep could not guarantee that a live secondmate's recorded endpoint is running a real agent process. Investigate the reason because that secondmate is not guaranteed live. - `NUDGE_SECONDMATES: secondmate : send failed: ` - the secondmate sweep fast-forwarded a running secondmate home and its loaded instruction surface (`AGENTS.md`, `bin/`, or `.agents/skills/`) changed, but the deterministic `fm-send.sh fm-` re-read nudge failed. Inspect the reason, keep the pending marker under `state/.secondmate-nudge-pending/` intact, and rerun session start after the endpoint or metadata issue is fixed so bootstrap can retry the exact same marked send. diff --git a/.agents/skills/firstmate-coding-guidelines/SKILL.md b/.agents/skills/firstmate-coding-guidelines/SKILL.md index c7126ff358..b58e6ea41f 100644 --- a/.agents/skills/firstmate-coding-guidelines/SKILL.md +++ b/.agents/skills/firstmate-coding-guidelines/SKILL.md @@ -3,7 +3,7 @@ name: firstmate-coding-guidelines description: >- Agent-only reference for changing firstmate's shared, tracked material per AGENTS.md section 1. Use before editing any of that material, whether working as firstmate directly or as a crewmate briefed on a firstmate-repo task. - Covers the knowledge-placement decision tree, the one-owner rule for contracts, the inline-stub pattern for content moved into a skill, AGENTS.md size discipline, trigger hygiene for new skills, and repo style rules (one sentence per line, plain dash, no agent co-author, shellcheck-clean bin scripts, colocated tests, and maintainer-verification evidence). + Covers the knowledge-placement decision tree, the one-owner rule for contracts, the inline-stub pattern for content moved into a skill, AGENTS.md size discipline, trigger hygiene for new skills, and repo style rules (one sentence per line, plain dash, no agent co-author, shellcheck-clean bin scripts, colocated tests, and backend-verification evidence). user-invocable: false metadata: internal: true @@ -23,20 +23,13 @@ Before writing a new fact anywhere in this repo, ask where it belongs, in this o If yes: `AGENTS.md`, inline. 2. Does the agent need it only in a nameable situation - a spawn, a recovery, a specific wake type, a specific lifecycle step? If yes: an agent-only skill under `.agents/skills/`, plus a one-line trigger pointer left inline in `AGENTS.md` (usually section 13). -3. Is it public product, setup, or user/operator reference? - If yes: the surface classified for that audience in [`docs/documentation-audiences.md`](../../../docs/documentation-audiences.md), limited to current behavior, setup, supported limits, stable invariants, concise rationale, and current verification entry points. -4. Is it contributor/maintainer architecture? - If yes: the classified maintainer-architecture owner for stable ownership, extension points, mechanism boundaries, and safety rationale. -5. Is it active reusable verification for a current guarantee? - If yes: an explicitly classified maintainer-verification record may keep current dates, versions, exact commands, and exact output. -6. Is it task or incident evidence - chronology, transcripts, branches, temporary paths, failed hypotheses, or delivery proof? - If yes: keep it in the private task report or PR evidence by default, after distilling every unique current fact into its authoritative owner. -7. Is it mechanics - exact flags, exact commands, exact paths? - If yes: the script's own header comment plus its `--help` output, not prose in `AGENTS.md`, a skill, or a second documentation owner. +3. Is it human/reference detail - a wire format, a verification record, a mechanism narrative, an incident writeup? + If yes: `docs/`. +4. Is it mechanics - exact flags, exact commands, exact paths? + If yes: the script's own header comment plus its `--help` output, not prose in `AGENTS.md` or a skill. Stop at the first tier that answers yes. Do not place a fact at a more convenient tier than the one this tree gives you. -The machine-consumed inventory in [`docs/documentation-audiences.json`](../../../docs/documentation-audiences.json) is the single classification owner for maintained prose surfaces; do not add parallel front matter or a second audience list. ## One-owner rule @@ -81,13 +74,6 @@ Mark an axis not applicable only after inspecting its integration surface, and u For critical safety, routing, startup, and supervision infrastructure, prefer deterministic and idempotent enforcement over relying on agent memory alone. Keep instructions as the authority and discovery layer, but make repeated execution converge safely and make invalid or unsafe states fail closed wherever the runtime can enforce them. -## Documentation change review - -For every changed maintained prose surface, identify its inventory audience, authoritative owner, current-behavior relevance, destination for supporting evidence, and any unique safety fact that removal could lose. -Move or delete evidence only after the current owner and regression pointer are verified. -After all documentation, review-fix, and lint-fix commits, review the complete branch diff again against those criteria rather than reviewing only the latest commit. -Run `bin/fm-doc-audience-check.sh`; it enforces classification, README setup routing, local link targets, and owner pointers without keyword-linting legitimate evidence prose. - ## Repo style rules - Put one full sentence per line in tracked Markdown. @@ -97,6 +83,6 @@ Run `bin/fm-doc-audience-check.sh`; it enforces classification, README setup rou - `bin/*.sh` and `bin/backends/*.sh` must pass `shellcheck`. - Run `bin/fm-lint.sh` before treating a script change as done; it is the single owner of the lint definition (file set, config, and pinned shellcheck version) that CI and the no-mistakes pre-push gate both invoke, and it refuses to run under any other shellcheck version. - Colocate tests with the existing pattern in `tests/`, name them `.test.sh`, and extend an existing script rather than inventing a new runner. -- A maintainer-verification record under `docs/verification/` records active empirical facts, not assumptions or task chronology. -- Include the date, version, exact commands run, and exact output needed to support the current guarantee. -- Keep incident chronology and delivery evidence in private task reports or PR evidence unless a concise rationale is required to maintain a current safety boundary. +- A backend-verification doc (`docs/*-backend.md`) records empirical facts, not assumptions. +- Include the date, version, exact commands run, and exact output. +- Write incidents the same way, as evidence, not narrative alone. diff --git a/.agents/skills/firstmate-orca/SKILL.md b/.agents/skills/firstmate-orca/SKILL.md index 3db6d22c98..854d497939 100644 --- a/.agents/skills/firstmate-orca/SKILL.md +++ b/.agents/skills/firstmate-orca/SKILL.md @@ -13,11 +13,10 @@ It does not replace `AGENTS.md`, `docs/orca-backend.md`, or `harness-adapters`. Orca is a runtime backend, not an agent harness. The runtime backend owns the task endpoint and, for Orca, the task worktree. -The harness is the agent process launched inside that endpoint, such as `claude`, `codex`, `opencode`, `pi`, `grok`, or `kimi`. +The harness is the agent process launched inside that endpoint, such as `claude`, `codex`, `opencode`, `pi`, or `grok`. Load `harness-adapters` for harness-specific launch, interrupt, resume, trust-dialog, and skill-invocation facts. -Implementation details, metadata fields, teardown guarantees, and limitations live in `docs/orca-backend.md`. -`docs/verification/runtime-backends.md` "Orca" owns active smoke evidence. +Implementation details, metadata fields, teardown guarantees, limitations, and smoke evidence live in `docs/orca-backend.md`. Prefer the `bin/fm-*` helpers over raw `orca` commands. Use raw `orca` only when the helper surface cannot answer the inspection question, and keep the recorded firstmate metadata as the task identity. diff --git a/.agents/skills/harness-adapters/SKILL.md b/.agents/skills/harness-adapters/SKILL.md index 85cd11c35c..86c684f7c6 100644 --- a/.agents/skills/harness-adapters/SKILL.md +++ b/.agents/skills/harness-adapters/SKILL.md @@ -1,6 +1,6 @@ --- name: harness-adapters -description: Agent-only reference for firstmate harness operations. Use before spawning or recovering a crewmate or secondmate, handling a trust dialog, sending a harness-specific skill invocation, interrupting or exiting an agent, resuming an exited agent, or verifying a new harness adapter. Contains verified facts for claude, codex, opencode, pi, grok, and kimi. +description: Agent-only reference for firstmate harness operations. Use before spawning or recovering a crewmate or secondmate, handling a trust dialog, sending a harness-specific skill invocation, interrupting or exiting an agent, resuming an exited agent, or verifying a new harness adapter. Contains verified facts for claude, codex, opencode, pi, and grok. user-invocable: false metadata: internal: true @@ -18,14 +18,14 @@ The captain may override that file at session start or later; a per-task instruc Secondmates have their own harness knob, so a secondmate can run on a different adapter than crewmates. `config/secondmate-harness` is the harness the primary uses to launch SECONDMATE agents, resolved through the fallback chain `config/secondmate-harness` -> `config/crew-harness` -> firstmate's own. An absent or `default` `config/secondmate-harness` therefore behaves exactly as the crew harness did before this knob existed (secondmates launched on the crew harness); setting it splits the two. -The [`secondmate-provisioning` skill](../secondmate-provisioning/SKILL.md) owns the complete inherited-local-material allowlist and propagation contract. -This skill owns only the harness-relevant consequence: a secondmate's own crewmates use the primary's inherited dispatch profiles and static harness value, while `config/secondmate-harness` is the primary's own setting and is never inherited - secondmates do not spawn secondmates. +`config/crew-dispatch.json`, `config/crew-harness`, and `config/backlog-backend` are inherited by secondmate homes. +This skill owns only the harness-relevant consequence: a secondmate's own crewmates use the primary's dispatch profiles and static harness value, while `config/secondmate-harness` is the primary's own setting and is never inherited - secondmates do not spawn secondmates. Inheritance copies the literal `config/crew-harness` file, so for a secondmate's own crewmates to run on the primary's crewmate harness the captain must set `config/crew-harness` to a concrete adapter name, such as `codex`. If `config/crew-harness` is unset or `default`, there is no concrete value to inherit, so the secondmate's own crewmates fall back to the secondmate's own/detected harness rather than the primary's effective crewmate harness. Inheritance also copies the literal `config/crew-dispatch.json` file, so secondmates apply the same best-fit profile rules for their own crewmates. Each adapter splits into mechanics and knowledge. -The per-task mechanics, including launch command, autonomy flag, and any enabled crewmate turn-end hook, live in `bin/fm-spawn.sh`. +The per-task mechanics, including launch command, autonomy flag, and crewmate turn-end hook, live in `bin/fm-spawn.sh`. The primary-session "no turn ends blind" guard contract and harness hook installation paths live in `docs/turnend-guard.md`. The primary-session watcher wake protocols are rendered from `docs/supervision-protocols/` by `bin/fm-supervision-instructions.sh`. The supervision knowledge lives here: busy signature, exit command, interrupt, dialogs, resume behavior, skill invocation, and quirks. @@ -50,39 +50,25 @@ Use that value for interrupt, exit, resume, and skill-invocation facts. ## Primary turn-end guard -The primary integrations for `claude`, `codex`, `opencode`, `pi`, and `grok` have empirically validated hook paths for the "no turn ends blind" guard. +Every verified primary harness has an empirically validated hook path for the "no turn ends blind" guard. `claude` and `codex` block directly through Stop hooks that preserve exit status 2 and stderr from `bin/fm-turnend-guard.sh`. `opencode`, `pi`, and `grok` expose passive lifecycle callbacks for this purpose, so their tracked primary adapters force one bounded follow-up or resume when the shared predicate blocks. -Kimi is outside the primary turn-end guard scope, while `docs/turnend-guard.md` owns its separate guarded global hook for crew wake signals. -The exact hook files, commands, scoping rules, and fail-open tradeoffs are owned by `docs/turnend-guard.md`. -`docs/verification/supervision.md` "Turn-end guard" owns active validation evidence. +The exact hook files, commands, validation transcripts, scoping rules, and fail-open tradeoffs are owned by `docs/turnend-guard.md`. When changing any primary turn-end hook, validate the real harness behavior in a scratch project or throwaway home before trusting it, then update that doc and the relevant concise fact below. ## Primary pre-arm (PreToolUse) seatbelt -The primary integrations for `claude`, `codex`, `opencode`, `pi`, and `grok` also have wired PreToolUse-equivalent hooks that deny a watcher-arm anti-pattern (shell `&`, truncating pipe, bundling, broad `pkill -f fm-watch`) before it runs. +Every verified primary harness also has a wired PreToolUse-equivalent hook that denies a watcher-arm anti-pattern (shell `&`, truncating pipe, bundling, broad `pkill -f fm-watch`) before it runs. `claude` and `codex` block directly through PreToolUse hooks; `grok` blocks the same way but requires every `$VAR` reference in its hook `command` string to carry an inline `:-default` or it fails to launch the hook entirely. `opencode` and `pi` block by throwing from `tool.execute.before` / returning `{block: true}` from `tool_call`. The exact hook files, commands, output-shaping quirks (Claude Code only honors the deny when stdout is empty), and validation transcripts are owned by `docs/arm-pretool-check.md`. -When changing any watcher-arm PreToolUse hook, validate the real harness behavior in a scratch project before trusting it, then update that doc. -## Primary delegation-shape guard - -Claude exposes built-in delegation, scheduling, and worktree tools that a primary session can use to create work with no `state/.meta`, which makes the whole guard stack inert because every guard counts that metadata. -The shipped mechanism is `bin/fm-subagent-pretool-check.sh`, a primary-home PreToolUse guard that denies a delegation-SHAPED tool name. -Claude primaries should also use an untracked per-home local `permissions.deny` list as hardening for known Claude delegation tools, because it removes them from the model's schema so they are never offered. -That deny list must not ship in tracked `.claude/settings.json` because it is Claude-only rather than harness-agnostic, and because tracked project settings propagate into linked worktrees where they disarm legitimate crewmates. -`docs/subagent-guard.md` owns the full contract, the local deny-list recommendation, the `FM_ALLOW_SUBAGENT=1` escape hatch, and the per-harness applicability review. - -Two verified facts worth pinning here. -The subagent tool presents to the model as `Agent`, and on Claude Code 2.1.217 both `Agent` and `Task` work as `permissions.deny` keys, verified by an A/B with a nonsense-name control. -`permissions.allow` is a pre-approval list rather than an availability list, so there is no fail-closed positive allowlist. +When changing any primary PreToolUse hook, validate the real harness behavior in a scratch project before trusting it, then update that doc. ## Primary session-start nudge AGENTS.md section 3 remains the behavioral owner for session start, while tracked native adapters invoke `bin/fm-sessionstart-nudge.sh` as an idempotent enforcement layer. -The wrapper prints one canonically typed `session-start` instruction to run `bin/fm-session-start.sh`; it never runs the digest, wake drain, bootstrap sweeps, lock, or supervision arm itself. -Full mechanics, scoping, and fail-open behavior live in `docs/sessionstart-nudge.md`. -`docs/verification/supervision.md` "Native session-start delivery" owns active dated commands, payloads, and evidence. +The wrapper prints only the instruction to run `bin/fm-session-start.sh`; it never runs the digest, wake drain, bootstrap sweeps, lock, or supervision arm itself. +Full mechanics, scoping, dated commands, payloads, and fail-open evidence live in `docs/sessionstart-nudge.md`. - `claude`: verified native `SessionStart` stdout injection; `.claude/settings.json` matches `startup`, `resume`, and `clear`, but not `compact`. - `codex`: verified on 0.144.4; `.codex/hooks.json` receives `source=startup`, and wrapper stdout reaches model context. @@ -94,7 +80,7 @@ Full mechanics, scoping, and fail-open behavior live in `docs/sessionstart-nudge At session start, `bin/fm-session-start.sh` prints exactly one watcher supervision block for the detected primary harness. Do not substitute another harness's wait shape when resuming supervision. -Claude's Stop `asyncRewake` hook (`bin/fm-claude-stop-autoarm.sh`) owns tokenless re-arm around `bin/fm-watch-arm.sh`, and Grok uses tracked background-notify cycles around `bin/fm-watch-arm.sh`. +Claude and Grok use tracked background-notify cycles around `bin/fm-watch-arm.sh`. Codex uses bounded foreground checkpoints through `bin/fm-watch-checkpoint.sh` because Codex cannot reason while a foreground tool call is running. OpenCode uses `.opencode/plugins/fm-primary-watch-arm.js`, which coordinates with the turn-end guard plugin and wakes the TUI with `client.session.promptAsync`. Pi uses the tracked `.pi/extensions/fm-primary-turnend-guard.ts` plus the tracked `.pi/extensions/fm-primary-pi-watch.ts`, both project-local extensions Pi auto-discovers once trusted. @@ -122,24 +108,6 @@ The supported launch-profile flags below are verified locally; each row records | grok | `--model ` | `--reasoning-effort ` | Verified on grok 0.2.99 (2026-07-13). `--effort` is an alias, but firstmate's profile axis is reasoning effort. As of 0.2.99 the ceiling is `high`; both `xhigh` and `max` are rejected with `use one of: high, medium, low`, so firstmate omits them. | | pi | `--model ` | `--thinking ` | Verified 2026-07-13 on Pi 0.80.6. `pi --help` advertises `off`, `minimal`, `low`, `medium`, `high`, `xhigh`, and `max`; `pi --print --model openai-codex/gpt-5.6-sol --thinking max 'Reply with exactly OK.'` completed successfully. | | opencode | `--model ` | none for firstmate's interactive launch | Verified on opencode 1.17.6. `opencode run` has `--variant`, but firstmate launches the interactive `opencode --prompt` path, which has no verified effort flag. | -| kimi | `--model ` | none | Verified 2026-07-25 on Kimi Code CLI 0.29.1. | - -### Model support discovery - -Treat model and provider knowledge as current source-of-truth discovery, not as a permanent namespace or provider mapping. -Use the discovery surface in the current authenticated environment because supported and available models can change by version, account, and configuration. - -| Harness | Authoritative discovery surface | -|---|---| -| claude | Open the current interactive session's `/model` picker; `claude --help` documents the accepted alias or full-model-name input shape. | -| codex | Open the current interactive session's `/model` picker. | -| opencode | Run `opencode models [provider]`, which lists available provider/model identifiers. | -| pi | Run `pi --list-models [search]`; Pi's installed `docs/models.md` owns how built-in, extension-registered, and custom provider/model entries reach that list. | -| grok | Run `grok models`, which lists the models available to the current Grok installation and account. | -| kimi | Run `kimi provider list --json`, which lists the current provider and model configuration. | - -For an unfamiliar harness or model namespace, establish support and provider identity from that harness's authoritative CLI help, model listing, or current documentation rather than guessing from a name or prefix. -If those sources do not establish the relationship needed for dispatch, fail loudly and report the unresolved candidate. When a requested effort value is outside the harness-specific accepted set, `fm-spawn` records the requested `effort=` in meta but emits no effort flag for that harness. This preserves launch success instead of passing a known-bad value. @@ -153,20 +121,13 @@ Natural language is acceptable if uncertain. - codex: `$`, for example `$no-mistakes`; `/` is claude-only and codex rejects it as "Unrecognized command". - opencode: no separate verified skill invocation beyond normal slash-command behavior; use natural language if the exact skill command is uncertain. - pi: no separate verified skill invocation beyond normal command behavior; use natural language if the exact skill command is uncertain. -- grok: `/`, for example `/no-mistakes` (same form as claude). Verified end to end: grok discovers the user-level `no-mistakes` skill, `/no-mistakes` invokes it, and grok drives a real `no-mistakes axi run`. Like codex's `$`/`/` popups, typing `/` opens grok's slash-autocomplete, so a too-fast Enter selects the popup entry instead of sending, and for an argument-taking command (like `/no-mistakes`'s optional task-first argument) that first Enter only expands the popup selection into an argument-hint placeholder rather than submitting - a genuine second Enter is required (see the grok section below for the 2026-07-03 incident and fix). `fm_tmux_submit_core`'s retried Enter (used by `fm-send` on the tmux backend) handles this through the structural composer reader; the herdr backend needed a dedicated fix (`fm_backend_herdr_composer_state`, docs/herdr-backend.md) because its prior delta-based verification false-positived on that same popup-close content change. -- kimi: `/`, for example `/no-mistakes`. - -## Submission acknowledgement hazards +- grok: `/`, for example `/no-mistakes` (same form as claude). Verified end to end: grok discovers the user-level `no-mistakes` skill, `/no-mistakes` invokes it, and grok drives a real `no-mistakes axi run`. Like codex's `$`/`/` popups, typing `/` opens grok's slash-autocomplete, so a too-fast Enter selects the popup entry instead of sending, and for an argument-taking command (like `/no-mistakes`'s optional task-first argument) that first Enter only expands the popup selection into an argument-hint placeholder rather than submitting - a genuine second Enter is required (see the grok section below for the 2026-07-03 incident and fix). `fm_tmux_submit_core`'s retried Enter (used by `fm-send` on the tmux backend) already handles this correctly by reading the cursor row; the herdr backend needed a dedicated fix (`fm_backend_herdr_composer_state`, docs/herdr-backend.md) because its prior delta-based verification false-positived on that same popup-close content change. -A send or key action reporting success is not proof that the intended action happened. -OpenCode can accept and queue an Enter while leaving text visible, Grok can consume Enter in its slash popup without submitting, and Kimi can silently drop a message sent before readiness even though the send returns success. -The shared symptom is a healthy-looking pane with no work in progress, so each adapter must verify the observable postcondition that is specific to its TUI. - -## claude (VERIFIED; busy signature re-verified 2026-07-25 on Claude Code 2.1.220) +## claude (VERIFIED) | Fact | Value | |---|---| -| Busy-pane signature | Current turns match the harness-scoped `…[[:space:]]+\([0-9]+[smh]` shape after a rotating glyph and word, for example `✢ Pollinating… (16s · ...)`; legacy `esc to interrupt` remains accepted, while `Worked for 31s` is idle. | +| Busy-pane signature | `esc to interrupt` | | Exit command | `/exit` | | Interrupt | single Escape | | Skill invocation | `/` (e.g. `/no-mistakes`) | @@ -180,17 +141,17 @@ A plain `tmux capture-pane` cannot tell that ghost text apart from typed text. Firstmate launches every claude crewmate and secondmate with `CLAUDE_CODE_ENABLE_PROMPT_SUGGESTION=false`, scoped to firstmate-launched agents through `bin/fm-spawn.sh`, so it never touches the captain's global config. The CLI's `--prompt-suggestions` flag is print/SDK-mode only and does not suppress the interactive composer ghost text, verified empirically on v2.1.186. As defense in depth for any pane that flag cannot reach, including the captain's own firstmate composer that away-mode reads, the shared `fm_composer_strip_ghost` extractor in `bin/fm-composer-lib.sh` removes dim/faint SGR 2 ghost runs before pending-input classification on both ANSI-capable readers (tmux and herdr). -Its broader dark-TRUECOLOR placeholder handling and dark-theme tradeoff are documented in `docs/herdr-backend.md` "Composer and injection safety", with active captures in `docs/verification/runtime-backends.md`. +Its broader dark-TRUECOLOR placeholder handling and dark-theme tradeoff are documented in `docs/herdr-backend.md`'s 2026-07-10 incident record. That styled capture is internal to the boolean detector only. `fm-peek` and every other human or LLM-facing capture path stays plain `tmux capture-pane` with no escape codes. -**Primary-session guard fact (verified 2026-07-04, Claude Code 2.1.201; preserved 2026-07-08, Claude Code 2.1.204; Stop-owned auto-arm revalidated 2026-07-24, Claude Code 2.1.219).** +**Primary-session guard fact (verified 2026-07-04, Claude Code 2.1.201; preserved 2026-07-08, Claude Code 2.1.204).** This is separate from the per-task crewmate turn-end hook above (that one just `touch`es a marker file in a task's own `.claude/settings.local.json`). -The firstmate PRIMARY's own `.claude/settings.json` registers two Stop hooks: `bin/fm-turnend-guard.sh --claude` and the Stop-owned auto-arm `bin/fm-claude-stop-autoarm.sh` (`asyncRewake: true`, `timeout: 28800`), and exiting the guard with status 2 plus stderr reliably forces the model to continue. -Claude Code's stdin payload to a Stop hook carries a `stop_hook_active` boolean that is `true` when the current stop attempt follows ANY stop-hook-driven continuation, including `asyncRewake` rewakes; the primary guard therefore ignores it in `--claude` mode and uses the cooperative claim/epoch check plus a bounded re-block budget instead, while the codex-mode default still treats it as a one-block loop guard. +The firstmate PRIMARY's own `.claude/settings.json` registers `bin/fm-turnend-guard.sh` as a Stop hook, and exiting with status 2 plus stderr reliably forces the model to continue. +Claude Code's stdin payload to a Stop hook carries a `stop_hook_active` boolean that is `true` exactly when the current stop attempt is itself a forced continuation from an earlier block this turn; a hook can and should use that as its own loop-guard (always allow the stop when it is already `true`) rather than tracking state itself. A project-level `.claude/settings.json` only takes effect when Claude Code's project root is that exact directory - it does not walk up from a subdirectory looking for one, so firstmate launches the primary from the repo root. -After those settings are loaded, hook command resolution is still cwd-sensitive because Claude Code runs commands through `/bin/sh` against the session's current cwd; keep the tracked commands anchored through `"$CLAUDE_PROJECT_DIR"/bin/...` and see `docs/turnend-guard.md` for the verified Stop-hook details. -Claude Code's primary watcher protocol is Stop-owned: the auto-arm hook fires on every Stop and foregrounds `bin/fm-watch-arm.sh` when the home is eligible and still needs supervision, and its exit-2 `asyncRewake` rewake is the wake; the model drains and handles wakes but never runs a routine re-arm command. +After those settings are loaded, hook command resolution is still cwd-sensitive because Claude Code runs commands through `/bin/sh` against the session's current cwd; keep the tracked command anchored through `"$CLAUDE_PROJECT_DIR"/bin/fm-turnend-guard.sh` and see `docs/turnend-guard.md` for the verified Stop-hook details. +Claude Code's primary watcher protocol is the lowest-friction path: run `bin/fm-watch-arm.sh` as its own Claude Code background task and treat background-task completion as the wake. ## codex (VERIFIED 2026-06-11, codex-cli 0.139.0) @@ -223,7 +184,7 @@ The tracked hook anchors to `pwd -P`, verifies that root is firstmate-shaped and Codex's primary watcher protocol is `bin/fm-watch-checkpoint.sh --seconds "${FM_CODEX_WATCH_CHECKPOINT:-180}"`, not `bin/fm-watch-arm.sh`. The checkpoint is deliberately foreground and bounded so Codex regains control regularly to process user messages and queued wakes. -## opencode (VERIFIED 2026-06-11, v1.15.7-1.17.6; 1.18.4 busy-queue re-verified 2026-07-20) +## opencode (VERIFIED 2026-06-11, v1.15.7-1.17.6) | Fact | Value | |---|---| @@ -236,24 +197,6 @@ Opencode can auto-upgrade itself in the background and the running TUI can exit If a pane shows the exit banner, relaunch with `--continue` to resume the session. `--prompt` does not auto-submit alongside `--continue`, so send the next instruction via `fm-send` once the TUI is up. -**Busy-queued Enter (opencode 1.18.4, tmux backend fix, herdr known gap).** -While opencode is mid-turn, the composer accepts Enter as a "send when the turn -ends" keystroke but does not clear the typed text from the composer until the -turn actually finishes. -Without a fix, every `fm-send` to a busy opencode pane exits non-zero on a -false "Enter swallowed", and every daemon escalation that lands while the -primary is mid-turn is treated as wedged. -The shared `fm_tmux_submit_enter_core` (`bin/fm-tmux-lib.sh`) now falls back -to `fm_pane_is_busy` once the Enter-retry budget is spent: a busy pane means -the Enter was accepted and queued (reported as `empty` so the caller does not -re-send), while an idle pane keeps `pending` as a genuine swallow. The herdr -adapter observes the same opencode behavior but needs a separate fix; it is -recorded as a known gap in `docs/herdr-backend.md` rather than patched here, -so the tmux adapter does not paper over a herdr-specific shape. -Regression coverage: `tests/fm-tmux-submit-busy.test.sh` covers the four -scenarios (busy + pending -> `empty`, idle + pending -> `pending`, busy + -cleared -> `empty`, idle + cleared -> `empty`). - **Primary-session guard fact (verified 2026-07-08, OpenCode 1.17.6).** The firstmate PRIMARY's own `.opencode/plugins/fm-primary-turnend-guard.js` listens for `session.idle`. Throwing from `session.idle` does not block `opencode run`, so the primary adapter treats the event as passive and uses `client.session.promptAsync` to force one follow-up turn when `bin/fm-turnend-guard.sh` returns 2. @@ -288,7 +231,7 @@ The model arms through `fm_watch_arm_pi`, never a foreground bash arm; the watch `bin/fm-session-start.sh` reports when the live Pi session has not loaded both the turn-end guard and watcher extensions, and points at plain `pi` after project trust as the fix, with `-e` as a trust-free fallback. When a secondmate is launched on Pi, `fm-spawn.sh --secondmate` launches Pi with both `-e .pi/extensions/fm-primary-turnend-guard.ts` and `-e .pi/extensions/fm-primary-pi-watch.ts`, both already present in the secondmate home's git worktree. -## grok (VERIFIED 2026-06-29, grok 0.2.73; slash-submit re-verified 2026-07-03 on 0.2.82; reasoning-effort ceiling re-verified 2026-07-13 on 0.2.99; exit paths re-verified 2026-07-19 on grok 0.2.103) +## grok (VERIFIED 2026-06-29, grok 0.2.73; slash-submit re-verified 2026-07-03 on 0.2.82; reasoning-effort ceiling re-verified 2026-07-13 on 0.2.99) Grok Build TUI (`grok`), a Claude-Code-compatible CLI from xAI. Launch with a positional prompt: `grok --always-approve "$(cat )"`. @@ -297,7 +240,7 @@ For Grok's supported reasoning-effort values and omission behavior, see the [lau | Fact | Value | |---|---| | Busy-pane signature | `Ctrl+c:cancel` (the mid-turn cancel hint in grok's keybind bar, shown iff a turn is running; the spinner line is a braille glyph + `… N.Ns` + `[stop]`, e.g. `⠹ Thinking… 1.1s … [stop]`). Idle keybind bar shows only `Shift+Tab:mode │ Ctrl+.:shortcuts`. The ASCII `Ctrl+c:cancel` is the busy regex (avoids locale fragility of matching braille). | -| Exit command | `/exit` typed into the composer exits the TUI cleanly and prints `Resume this session with: grok --resume `; `Ctrl+Q` double-press within 1000ms remains a fallback; `Ctrl+D` is the quit key in VS Code family terminals; `Ctrl+C` is the interrupt, not the exit. | +| Exit command | `Ctrl+Q` double-press within 1000ms (it is a confirmed destructive action). Prints `Resume this session with: grok --resume `. `Ctrl+D` is the quit key in VS Code family terminals. NOT `/exit` and NOT `Ctrl+C`. | | Interrupt | single `Ctrl+C` (cancels the current turn; the footer shows `Ctrl+c:cancel` mid-turn). `Esc` only moves focus to the scrollback, it does NOT interrupt. | | Skill invocation | `/` (e.g. `/no-mistakes`), same as claude. Opens a slash-autocomplete popup, so a too-fast Enter selects the popup entry instead of sending. For an argument-taking command that first Enter does not submit at all - it expands the selection into an argument-hint placeholder in the composer (e.g. `/compact` -> `/compact compaction instructions`, live-verified), leaving real text still sitting there unsubmitted; a genuine second Enter is required. `fm-send`'s retried Enter lands it on BOTH backends, but only because each backend's own submit-verification correctly recognizes that placeholder-filled text as still-pending - see the incident below. | | Autonomy | `--always-approve` (footer shows `· always-approve`); auto-approves every tool execution, verified to run fully unattended. `--permission-mode bypassPermissions` is the stronger equivalent. | @@ -306,8 +249,8 @@ For Grok's supported reasoning-effort values and omission behavior, see the [lau **Incident (2026-07-03, herdr backend only, grok 0.2.82):** two grok/herdr crewmates were sent `/no-mistakes` via `fm-send`; both left it fully typed but unsubmitted in the composer for minutes (footer still `Enter:send`), and `fm-send` exited 0 with no error. Reproduced live: the herdr adapter's submit-verification at the time treated ANY pane-content change after Enter as "submitted", and the popup-close-with-placeholder-fill described above IS a visible content change even though nothing was actually sent. -The tmux backend's structural `fm_tmux_composer_state` read sees placeholder-filled text on any content row as still pending, so its retry loop sends the needed second Enter. -The Herdr adapter (`fm_backend_herdr_composer_state`, `bin/backends/herdr.sh`) classifies the composer's own row structurally instead of diffing raw content; see `docs/herdr-backend.md` "Composer and injection safety" for the current boundary and `tests/fm-backend-herdr.test.sh` for regression coverage. +The tmux backend was never affected - `fm_tmux_composer_state` reads the actual cursor row, correctly sees the placeholder text as still-pending, and its retry loop already sends the needed second Enter. +Fixed in the herdr adapter (`fm_backend_herdr_composer_state`, `bin/backends/herdr.sh`) by classifying the composer's own row structurally instead of diffing raw content; see `docs/herdr-backend.md`'s "Incident (2026-07-03)" section for the full account and `tests/fm-backend-herdr.test.sh` for the regression coverage. Startup dialog: the "Run Grok Build in a project directory?" project picker appears ONLY when grok is launched from a non-project directory (home, Desktop, Downloads, `/tmp`). `fm-spawn` launches inside the treehouse worktree (a git repo root), so the picker never appears and grok treats the worktree as a trusted project automatically - no post-launch keystroke is needed. @@ -320,10 +263,11 @@ Verified live against grok 0.2.93: real input is the bright `38;2;224;222;244` ( This assumes a dark terminal theme, the fleet reality; the SGR-2 signal stays theme-independent. Regression coverage: `tests/fm-composer-ghost.test.sh` (`test_strip_ghost_drops_dark_truecolor_ghost`, `test_dark_truecolor_ghost_only_composer_is_not_pending`) and `tests/fm-backend-herdr.test.sh` (`test_composer_state_grok_dark_truecolor_placeholder_is_empty`, `test_composer_state_grok_bright_truecolor_real_text_is_pending`). -**Tmux bottom-border cursor quirk (fixed):** -In a pristine placeholder-only composer, tmux's `#{cursor_y}` can point at the box's bottom border instead of its text row. -The shared tmux reader now locates the complete box structurally and classifies every content row, so the cursor may sit on a content row or the bottom border without changing the result. -The same structural read covers multi-row composers without fixed cursor offsets, while Herdr retains its own structural composer-row scan. +**Residual gap, tmux-only (unfixed):** +in that same pristine placeholder-only state, tmux's own `#{cursor_y}` points at the composer box's BOTTOM BORDER row, one row below the actual text row (the box appears to render one row lower before any real typing starts); once real text is typed the cursor correctly aligns with the text row again. +This is a row-SELECTION quirk, orthogonal to the styling fix above, and affects only the tmux path (herdr uses a structural composer-row scan, not `cursor_y`, so it is unaffected). +A correct fix needs a row-window read near `cursor_y` rather than the single `cursor_y` row. +In practice `fm-spawn` launches grok with the brief as its initial prompt, so a live task's composer is never observed in this pristine pre-typing state - but this is unverified for every path (e.g. a steer sent before grok's first real turn settles) and needs dedicated investigation before relying on it. Turn-end hook: grok fires a `Stop` hook at every turn boundary, giving firstmate a precise per-turn wake instead of only stale-pane detection. grok loads PROJECT hooks (`/.grok/hooks/`, `/.claude/settings.local.json`) only after the folder is granted hook-trust in `~/.grok/trusted_folders.toml`, which is not automatic and which firstmate will not establish by editing grok's own managed trust store. @@ -343,44 +287,3 @@ The adapter therefore runs the shared predicate and, when it returns 2, forces o It does not pass `--permission-mode`, so the passive hook cannot escalate the primary session's tool permissions. Project-local Grok hooks require folder trust, verified with launch-time `--trust`; if the primary firstmate checkout is not trusted for Grok hooks, this primary guard fails open and `fm-guard.sh` remains the next-command alarm. Grok's primary watcher protocol is Claude-shaped background-notify around `bin/fm-watch-arm.sh`; the passive Stop hook is only a backstop for blind turn ends. - -## kimi (VERIFIED 2026-07-25, kimi 0.29.1) - -Kimi Code CLI launches from the absolute path resolved from `PATH`, falling back to the executable `$HOME/.kimi-code/bin/kimi`. - -| Fact | Value | -|---|---| -| Binary | Executable `kimi` from `PATH`, then executable `$HOME/.kimi-code/bin/kimi`; spawning refuses if neither exists. | -| Launch | Bare interactive TUI with `--auto`, followed by readiness-gated pointer delivery; positional prompts are rejected. | -| Models | `kimi-code/kimi-for-coding` (default), `kimi-code/kimi-for-coding-highspeed`, `kimi-code/k3`, and `kimi-code/k3-256k`. | -| Busy-pane signature | A transient line with optional leading whitespace, a rotating moon-phase glyph, required whitespace on both sides of `·`, and optional trailing content; the line is absent when idle. | -| Exit command | `/exit` | -| Interrupt | Single Escape, which prints `Interrupted by user`. | -| Skill invocation | `/`, for example `/no-mistakes`; firstmate skills are discovered. | -| Autonomy | `--auto`; `-y` and `--yolo` are weaker and are not used. | -| Trust dialog | None on a clean first launch in a fresh pooled worktree. | -| Slash submission | One Enter submits, with no popup swallow or settle hazard. | -| Environment marker | None; detection relies on process ancestry command name `kimi`. | -| Composer | Bordered box with a bare `>` prompt glyph and no observed ghost or placeholder text. | -| Effort | No reasoning-effort flag exists, so requested effort is recorded in task metadata but omitted from launch. | - -`fm-spawn.sh` launches Kimi bare, waits for the composer box or `Welcome to Kimi Code!`, sends only `Read the brief at and follow it exactly.`, and requires a cleared composer plus either the echoed `✨` submission or nonzero context before accepting delivery. -This launch-then-send shape is mandatory because Kimi rejects a positional brief as an unknown command. -Sending before readiness was reproduced as a silent drop with a zero exit status, an empty composer, `context: 0%`, no echoed user message, and a healthy-looking idle pane. -The brief path must be absolute because the brief lives outside the task worktree, and Kimi reads it there without `--add-dir`. - -Observed live spinner captures included optional leading whitespace, a moon-phase glyph, whitespace around `·`, and rotating tip text, with the same shape observed during tool execution. -Because every captured spinner row had whitespace on both sides of `·`, the matcher requires that whitespace, deliberately does not match the never-observed zero-whitespace form, and does not require trailing tip text. -The startup input-readiness window is the established cause of Kimi's first-Enter delivery defect, while the banner is not the cause. -An early Enter can expand Kimi's composer to multiple content rows, leaving the pointer text on the first row and the cursor on an empty later row, which is the same single-cursor-row reading defect exposed by Grok's bottom-border cursor quirk. -The shared tmux reader now locates the complete bordered composer and treats real text on any content row as positive evidence that submission is still pending. -No rendering signal is trustworthy for proving that Kimi will accept input during this window, so delivery retries Enter through the shared submit core and retains the existing postcondition verification rather than relaxing readiness or delivery checks. -Kimi's footer tip rotates independently and can display `ctrl+c: cancel` while completely idle, so tip text is never used as its busy signature without the leading moon-plus-middot spinner structure. -The idle status bar can contain lowercase `thinking`, which is the model's effort label rather than a busy signal. -The spinner match covers the full moon-phase glyph set rather than one frame, but it remains locale- and emoji-font-sensitive because Kimi exposes no stable ASCII busy token. - -[`docs/turnend-guard.md`](../../../docs/turnend-guard.md) owns Kimi's verified global hook surface and captain-approved crew wake integration. -`fm-spawn.sh` installs one marker-delimited Firstmate entry in `$HOME/.kimi-code/config.toml`, one silent always-zero hook script, and one private token registry under `$HOME/.kimi-code/fm-turn-end.d/`. -Each Kimi crew worktree receives a gitignored `.fm-kimi-turnend` token pointer, and the global hook touches that task's `state/.turn-ended` only when the Stop payload's `cwd`, pointer, and registry entry all agree. -A guarded silent hook cannot be verified from absence of effect, so prove invocation with an unguarded probe before concluding that the hook did not fire. -The guarded turn-end signal supplements the pane busy signature, whose locale- and emoji-font-sensitive limits still apply while a turn is running. diff --git a/.agents/skills/project-management/SKILL.md b/.agents/skills/project-management/SKILL.md index af35d469ee..54ab841ae9 100644 --- a/.agents/skills/project-management/SKILL.md +++ b/.agents/skills/project-management/SKILL.md @@ -36,7 +36,7 @@ Choose the delivery mode when adding or creating the project: The optional `+yolo` posture changes routine approval authority but does not change the delivery mode. Default it off, and enable it only on the captain's explicit instruction. -`AGENTS.md` section 7 owns the complete authority boundary and exceptions when it is on. +Destructive, irreversible, and security-sensitive decisions still require captain approval when it is on. ## Add or clone an existing project diff --git a/.agents/skills/secondmate-provisioning/SKILL.md b/.agents/skills/secondmate-provisioning/SKILL.md index ecc364939a..81393e8b5f 100644 --- a/.agents/skills/secondmate-provisioning/SKILL.md +++ b/.agents/skills/secondmate-provisioning/SKILL.md @@ -78,9 +78,8 @@ This section is the single owner of the secondmate sync and inherited-local-mate Before launch, `fm-spawn.sh --secondmate` locally fast-forwards the home to the primary firstmate checkout's current default-branch commit when it is safe; dirty, diverged, or in-flight homes launch unchanged with a warning. The locked session-start bootstrap sweep runs the same guarded fast-forward for every live secondmate home, discovered from `state/.meta` records with `kind=secondmate` (`data/secondmates.md` only backfills `home=` for older records). That no-fetch path is a purely local fast-forward of tracked files, never an origin fetch, and it never touches the gitignored operational dirs, so a secondmate's backlog, projects, and in-flight work are never disturbed; a linked worktree advances immediately, while a standalone clone that lacks the target receives firstmate updates through `/updatefirstmate`'s origin refresh. -The same launch and the same locked bootstrap sweep also propagate the primary's declared inherited local material: `config/crew-dispatch.json`, `config/crew-harness`, `config/backlog-backend`, `config/herdr-presentation-spaces`, and the one shared captain-preference file `data/captain-shared.md`. +The same launch and the same locked bootstrap sweep also propagate the primary's declared inherited local material: `config/crew-dispatch.json`, `config/crew-harness`, `config/backlog-backend`, and the one shared captain-preference file `data/captain-shared.md`. Because these paths are gitignored, that propagation is a separate, primary-authoritative copy independent of the tracked-files fast-forward: it re-converges every live home whether or not its tracked files advanced, and it touches only the declared items. -Propagation failures warn without blocking secondmate launch or session-start continuation, and the destination keeps whatever safely validated state the helper left behind. Inheritance copies the literal `config/crew-harness` file, so a secondmate's own crewmates use the primary's crewmate harness only when it names a concrete adapter such as `codex`; an unset or `default` value has nothing concrete to inherit, and the secondmate's own crewmates fall back to the secondmate's own or detected harness instead. `config/secondmate-harness` is not inherited because it is only the primary's knob for launching secondmate agents. `data/captain-shared.md` is main-authoritative in the primary home and read-only in secondmate homes. @@ -93,23 +92,10 @@ Never copy any secondmate `data/captain-shared.md` back into the primary. Keep each home's `data/captain.md` domain-local. After first propagation to an existing home, trim that home's local `data/captain.md` by hand to domain-specific content plus pointers to `data/captain-shared.md`; do not automate or silently delete private content. Keep every `data/learnings.md` fully local by captain decision; route fleet-general machinery facts into tracked documentation through the normal firstmate repo path rather than inventing shared learnings propagation. -No AGENTS.md reread nudge is needed at spawn or respawn because the agent reads instructions fresh on launch; only the bootstrap sweep's running-home instruction-surface advance needs that AGENTS.md re-read. -Bootstrap reports successful AGENTS.md re-read sends as `BOOTSTRAP_INFO:` and only emits `NUDGE_SECONDMATES:` when that send fails and needs retry. -A separate, literal-content config reread is required whenever inherited `config/*` material changes under an already-running secondmate. -After each successful allowlisted config write, both the locked bootstrap convergence path and mid-session `bin/fm-config-push.sh` use the shared propagation report to build one per-home generation-specific private instruction file from the validated destination post-write bytes for only the allowlisted config items that actually changed for that home (`config/crew-dispatch.json`, `config/crew-harness`, `config/backlog-backend`, `config/herdr-presentation-spaces`), in deterministic allowlist order. -Each changed path is printed with clear begin/end delimiters and the destination file's full exact new bytes unparsed, or the explicit token `ABSENT` when propagation removed the destination copy. -The instruction uses only minimal framing that these are defaults/rules and do not remove judgment; it never includes SHA values, selected profiles, parsed summaries, or any other generated interpretation. -`data/captain-shared.md` is not a config file and is never inlined into this instruction file or message. -Homes whose allowlisted config files were all unchanged receive no config-reread message when no retry is pending. -Different homes may receive different changed-file sets based on their pre-push destination bytes. -Delivery uses the existing routed secondmate path (`fm-send`) with only a single-line `CONFIG_REREAD: ` pointer; a failed instruction publication retains the generated exact bytes in a bounded private retry queue when possible, legacy retry reports remain recoverable, a failed publication or retry-marker write retains the exact generation until it can be delivered, a failed send records a per-generation durable retry marker when possible, and all failures surface a concrete `CONFIG_REREAD:` diagnostic without claiming the live agent already re-read the values. -The propagation, generation publication, and pointer-delivery sequence holds one per-home inheritance lock, so concurrent mid-session pushes cannot deliver an older generation after a newer one. -A newly launched or relaunched secondmate already reads its files at launch, so its pending config-reread generations are discarded or quarantined after cleanup failure and it needs no redundant live-agent config nudge unless propagation changes files after launch. -Quarantined pre-relaunch generations are retained in bounded private history, and cleanup skips creating an empty quarantine generation. -Successfully delivered generations are retained only within a bounded per-home state history, while pending generations remain until delivery succeeds or a launch supersedes them. -These config values remain defaults and rules only; they must not harden `fm-spawn` to reject a deliberate runtime choice that differs from the configured defaults. -For already-live secondmates, use `bin/fm-config-push.sh` to push a mid-session inherited local-material change without running the tracked-file fast-forward. -It uses the same live-home discovery and propagation helper as bootstrap, reports each item as `pushed`, `unchanged`, `skipped`, or `error`, and follows the config-reread contract above for changed or pending generations. +No reread nudge is needed at spawn or respawn because the agent reads `AGENTS.md` fresh on launch; only the bootstrap sweep's running-home instruction-surface advance needs one. +Bootstrap reports successful sends as `BOOTSTRAP_INFO:` and only emits `NUDGE_SECONDMATES:` when that send fails and needs retry. +For already-live secondmates, use `bin/fm-config-push.sh` to push a mid-session inherited local-material change without running the tracked-file fast-forward or nudging the agents. +It uses the same live-home discovery and propagation helper as bootstrap and reports each item as `pushed`, `unchanged`, `skipped`, or `error`. `bin/fm-home-seed.sh` refuses to copy a missing or placeholder charter. Direct seed without a preexisting brief requires `FM_SECONDMATE_CHARTER`. diff --git a/.agents/skills/stuck-crewmate-recovery/SKILL.md b/.agents/skills/stuck-crewmate-recovery/SKILL.md index d97ebee302..40a1468cff 100644 --- a/.agents/skills/stuck-crewmate-recovery/SKILL.md +++ b/.agents/skills/stuck-crewmate-recovery/SKILL.md @@ -23,7 +23,7 @@ Load `secondmate-provisioning` instead for `kind=secondmate` recovery. Treat the digest's endpoint result as a presence signal, not proof that the task's work or validation run is gone. Read the targeted current state with `bin/fm-crew-state.sh ` before deciding to relaunch. -A no-mistakes run matched to the crew's branch and current code remains authoritative when the endpoint is dead: handle a terminal or parked run through the normal lifecycle, and keep supervising an active run instead of creating a duplicate worker. +A branch-matched no-mistakes run remains authoritative when the endpoint is dead: handle a terminal or parked run through the normal lifecycle, and keep supervising an active run instead of creating a duplicate worker. When no authoritative run accounts for the task, inspect only its recorded backend and worktree inventory. Use `treehouse status` for treehouse-backed tmux, herdr, zellij, or cmux tasks, and use the recorded `orca_worktree_id=` and `terminal=` for Orca tasks. diff --git a/.claude/settings.json b/.claude/settings.json index e77613c98a..37a535e378 100644 --- a/.claude/settings.json +++ b/.claude/settings.json @@ -22,15 +22,10 @@ { "type": "command", "command": "\"$CLAUDE_PROJECT_DIR\"/bin/fm-cd-pretool-check.sh --claude" - } - ] - }, - { - "matcher": ".*", - "hooks": [ + }, { "type": "command", - "command": "\"$CLAUDE_PROJECT_DIR\"/bin/fm-subagent-pretool-check.sh --claude" + "command": "\"$CLAUDE_PROJECT_DIR\"/bin/fm-continuity-pretool-check.sh" } ] } @@ -40,13 +35,7 @@ "hooks": [ { "type": "command", - "command": "\"$CLAUDE_PROJECT_DIR\"/bin/fm-turnend-guard.sh --claude" - }, - { - "type": "command", - "command": "\"$CLAUDE_PROJECT_DIR\"/bin/fm-claude-stop-autoarm.sh", - "asyncRewake": true, - "timeout": 28800 + "command": "\"$CLAUDE_PROJECT_DIR\"/bin/fm-turnend-guard.sh" } ] } diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c04c0965c4..bdfa76b238 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -24,93 +24,12 @@ jobs: # re-spell the shellcheck command here; keep CI and the pre-push gate on it. - run: bin/fm-lint.sh - # Deterministic proof that portable parallel shards + portable serial + Herdr - # equal the complete tests/*.test.sh inventory with no missing or duplicates. - test-coverage: - name: Test coverage guard + tests: + name: Behavior tests runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v6 - - name: Prove complete regression partition - run: bin/fm-test-run.sh --check-coverage - - # Two duration-balanced portable parallel shards of the Phase 2 proven-isolated - # set only. Composition owner: bin/fm-test-run.sh (docs/fm-test-portable-shards.md). - tests-portable-parallel-1: - name: Behavior portable parallel 1 - runs-on: ubuntu-latest - # Measured shard wall is ~1 min of serial sum on proven scripts; this cap is - # a hang tripwire with margin, not the expected healthy end of the lane. - timeout-minutes: 10 - steps: - - uses: actions/checkout@v6 - with: - fetch-depth: 0 - - name: Install pinned ShellCheck - run: | - set -eu - bin/fm-install-shellcheck.sh "$RUNNER_TEMP/bin" - echo "$RUNNER_TEMP/bin" >> "$GITHUB_PATH" - - name: Install tasks-axi - run: | - set -eu - npm install -g tasks-axi - tasks-axi --version - - name: Run portable parallel shard 1 - run: | - set -eu - mkdir -p "$RUNNER_TEMP/fm-test" - bin/fm-test-run.sh --lane portable-parallel-1 \ - --json "$RUNNER_TEMP/fm-test/fm-test-timing-portable-parallel-1.json" - - name: Upload shard 1 timing artifact - if: always() - uses: actions/upload-artifact@v4 - with: - name: fm-test-timing-portable-parallel-1 - path: ${{ runner.temp }}/fm-test/fm-test-timing-portable-parallel-1.json - if-no-files-found: warn - - tests-portable-parallel-2: - name: Behavior portable parallel 2 - runs-on: ubuntu-latest - timeout-minutes: 10 - steps: - - uses: actions/checkout@v6 - with: - fetch-depth: 0 - - name: Install pinned ShellCheck - run: | - set -eu - bin/fm-install-shellcheck.sh "$RUNNER_TEMP/bin" - echo "$RUNNER_TEMP/bin" >> "$GITHUB_PATH" - - name: Install tasks-axi - run: | - set -eu - npm install -g tasks-axi - tasks-axi --version - - name: Run portable parallel shard 2 - run: | - set -eu - mkdir -p "$RUNNER_TEMP/fm-test" - bin/fm-test-run.sh --lane portable-parallel-2 \ - --json "$RUNNER_TEMP/fm-test/fm-test-timing-portable-parallel-2.json" - - name: Upload shard 2 timing artifact - if: always() - uses: actions/upload-artifact@v4 - with: - name: fm-test-timing-portable-parallel-2 - path: ${{ runner.temp }}/fm-test/fm-test-timing-portable-parallel-2.json - if-no-files-found: warn - - # Required portable serial remainder: watcher, lock, AFK, tmux, daemon, - # ambiguous, and other stateful tests. Real Herdr stays in tests-herdr. - tests-portable-serial: - name: Behavior portable serial - runs-on: ubuntu-latest - # Measured serial remainder is ~13 min wall without Herdr. Cap is a hang - # tripwire above observed p99 script cost and suite wall, not the expected - # healthy end (interim 25m full-suite slack reduced after sharding). - timeout-minutes: 20 + # The suite should finish in ~2-3 minutes; this generous cap fails loudly on a + # hung watcher or tmux test instead of riding GitHub's 360-minute default. + timeout-minutes: 15 steps: - uses: actions/checkout@v6 with: @@ -133,176 +52,11 @@ jobs: set -eu npm install -g tasks-axi tasks-axi --version - - name: Run portable serial remainder - run: | + - run: | set -eu - mkdir -p "$RUNNER_TEMP/fm-test" - bin/fm-test-run.sh --lane portable-serial \ - --json "$RUNNER_TEMP/fm-test/fm-test-timing-portable-serial.json" - - name: Upload portable serial timing artifact - if: always() - uses: actions/upload-artifact@v4 - with: - name: fm-test-timing-portable-serial - path: ${{ runner.temp }}/fm-test/fm-test-timing-portable-serial.json - if-no-files-found: warn - - # Required real-Herdr lane: pinned install, serial real-herdr-gated family, - # hard-fail on "skip: herdr not found". Starts on Linux x86_64; if a genuine - # platform invariant fails (focus, cleanup, default-session tripwire), keep - # the failure evidence and move the job to macOS rather than skip assertions. - tests-herdr: - name: Behavior tests (Herdr) - runs-on: ubuntu-latest - # Real Herdr is slower than the portable suite; this is a hang tripwire, - # not the expected healthy end of the lane (estimate 15-40 min first cut). - timeout-minutes: 40 - steps: - - uses: actions/checkout@v6 - with: - fetch-depth: 0 - - name: Require tools for real Herdr - run: | - set -eu - command -v jq >/dev/null || { echo "::error::jq is required"; exit 1; } - command -v python3 >/dev/null || { echo "::error::python3 is required"; exit 1; } - command -v curl >/dev/null || { echo "::error::curl is required"; exit 1; } - command -v tar >/dev/null || { echo "::error::tar is required"; exit 1; } - jq --version - python3 --version - - name: Install pinned Herdr - run: | - set -eu - bin/fm-install-herdr.sh "$RUNNER_TEMP/bin" - echo "$RUNNER_TEMP/bin" >> "$GITHUB_PATH" - - name: Install pinned Treehouse - run: | - set -eu - # Several real-herdr-gated E2E scripts require treehouse for spawn - # worktree acquisition (presentation, workspace-per-home, autodetect). - bin/fm-install-treehouse.sh "$RUNNER_TEMP/bin" - echo "$RUNNER_TEMP/bin" >> "$GITHUB_PATH" - - name: Assert Herdr pin and protocol floor - run: | - set -eu - command -v herdr >/dev/null || { echo "::error::herdr not on PATH after install"; exit 1; } - command -v treehouse >/dev/null || { echo "::error::treehouse not on PATH after install"; exit 1; } - herdr --version - treehouse --version - status=$(herdr status --json) - printf '%s\n' "$status" - version=$(printf '%s' "$status" | jq -r '.client.version // empty') - protocol=$(printf '%s' "$status" | jq -r '.client.protocol // empty') - [ "$version" = "0.7.4" ] || { - echo "::error::expected exact Herdr pin 0.7.4, got ${version:-}" - exit 1 - } - case "$protocol" in - ''|*[!0-9]*) echo "::error::could not read client protocol"; exit 1 ;; - esac - [ "$protocol" -ge 16 ] || { - echo "::error::herdr protocol $protocol is below the required floor 16" - exit 1 - } - - name: Start default Herdr session for fleet-state tripwire - run: | - set -eu - # Lab provision requires exactly one running default session so the - # default-session tripwire can arm. Start it headless; never use it - # for lab work (tests still refuse default and use fm-lab-* only). - mkdir -p "$RUNNER_TEMP/fm-herdr" - nohup herdr server >"$RUNNER_TEMP/fm-herdr/default-server.log" 2>&1 & - echo $! >"$RUNNER_TEMP/fm-herdr/default-server.pid" - attempt=0 - while [ "$attempt" -lt 150 ]; do - running=$(herdr status --json 2>/dev/null | jq -r '.server.running // false' || echo false) - if [ "$running" = true ]; then - herdr session list --json | jq -e ' - [.sessions[]? | select(.default == true and .name == "default" and .running == true)] - | length == 1 - ' >/dev/null - echo "default Herdr session is running" - exit 0 - fi - sleep 0.2 - attempt=$((attempt + 1)) + for test_script in tests/*.test.sh; do + "$test_script" done - echo "::error::default Herdr server did not become ready" - cat "$RUNNER_TEMP/fm-herdr/default-server.log" || true - exit 1 - - name: Snapshot pre-suite Herdr sessions - run: | - set -eu - mkdir -p "$RUNNER_TEMP/fm-herdr" - bin/fm-herdr-ci-cleanup.sh snapshot "$RUNNER_TEMP/fm-herdr/sessions-before.json" - - name: Run real-Herdr family (serial, required) - run: | - set -eu - mkdir -p "$RUNNER_TEMP/fm-test" - # Serial only. Fail if any script reports herdr not found. Live - # harness credential tests stay outside this family (opt-in env only). - bin/fm-test-run.sh --family real-herdr-gated \ - --fail-on-gate-skip 'herdr not found' \ - --json "$RUNNER_TEMP/fm-test/fm-test-timing-herdr.json" - - name: Cleanup job-owned Herdr lab sessions - if: always() - run: | - set -eu - bin/fm-herdr-ci-cleanup.sh teardown "$RUNNER_TEMP/fm-herdr/sessions-before.json" || { - echo "::warning::lab session cleanup reported a problem; see logs" - # Surface diagnostics without weakening the suite result above. - herdr session list --json || true - exit 1 - } - - name: Upload Herdr timing and diagnostics - if: always() - uses: actions/upload-artifact@v4 - with: - name: fm-test-timing-herdr - path: | - ${{ runner.temp }}/fm-test/fm-test-timing-herdr.json - ${{ runner.temp }}/fm-herdr/sessions-before.json - ${{ runner.temp }}/fm-herdr/default-server.log - if-no-files-found: warn - - # Aggregate per-lane timing into one summary artifact for critical-path review. - tests-timing-aggregate: - name: Behavior timing aggregate - runs-on: ubuntu-latest - needs: - - tests-portable-parallel-1 - - tests-portable-parallel-2 - - tests-portable-serial - - tests-herdr - if: always() - steps: - - uses: actions/checkout@v6 - - name: Download lane timing artifacts - uses: actions/download-artifact@v4 - with: - pattern: fm-test-timing-* - path: ${{ runner.temp }}/fm-test-aggregate - merge-multiple: true - - name: Build aggregate timing summary - run: | - set -eu - mkdir -p "$RUNNER_TEMP/fm-test" - shopt -s nullglob - inputs=("$RUNNER_TEMP"/fm-test-aggregate/fm-test-timing-*.json) - if [ "${#inputs[@]}" -eq 0 ]; then - echo "::warning::no per-lane timing JSON found to aggregate" - exit 0 - fi - bin/fm-test-run.sh --aggregate-json \ - "$RUNNER_TEMP/fm-test/fm-test-timing-aggregate.json" \ - "${inputs[@]}" - - name: Upload aggregate timing artifact - if: always() - uses: actions/upload-artifact@v4 - with: - name: fm-test-timing-aggregate - path: ${{ runner.temp }}/fm-test/fm-test-timing-aggregate.json - if-no-files-found: warn macos-stock-bash: name: Stock macOS Bash snapshot compatibility @@ -327,16 +81,16 @@ jobs: snapshot_output=$(/bin/bash tests/fm-fleet-snapshot-view.test.sh) printf '%s\n' "$snapshot_output" snapshot_count=$(printf '%s\n' "$snapshot_output" | grep -c '^ok - ') - [ "$snapshot_count" -eq 15 ] || { - echo "::error::expected 15 snapshot/fleet-view tests, got $snapshot_count" + [ "$snapshot_count" -eq 13 ] || { + echo "::error::expected 13 snapshot/fleet-view tests, got $snapshot_count" exit 1 } bearings_output=$(/bin/bash tests/fm-bearings-snapshot.test.sh) printf '%s\n' "$bearings_output" bearings_count=$(printf '%s\n' "$bearings_output" | grep -c '^ok - ') - [ "$bearings_count" -eq 42 ] || { - echo "::error::expected 42 Bearings tests, got $bearings_count" + [ "$bearings_count" -eq 36 ] || { + echo "::error::expected 36 Bearings tests, got $bearings_count" exit 1 } diff --git a/.github/workflows/no-mistakes-required.yml b/.github/workflows/no-mistakes-required.yml index f56afee418..ab1224d390 100644 --- a/.github/workflows/no-mistakes-required.yml +++ b/.github/workflows/no-mistakes-required.yml @@ -1,5 +1,4 @@ name: Require no-mistakes -run-name: "PR #${{ github.event.pull_request.number }} body compliance - ${{ github.event.action }} - event ${{ github.run_number }} (run ${{ github.run_id }})" on: pull_request: @@ -10,12 +9,8 @@ on: permissions: contents: read -# GitHub concurrency groups retain at most one pending run, replacing older -# pending runs even when cancel-in-progress is false. Give body-bearing events -# an immutable per-event group so first-time-fork approvals can never collapse -# opened/edited checks. Keep synchronize/reopened coalescing as before. concurrency: - group: no-mistakes-required-${{ github.event.pull_request.number }}-${{ (github.event.action == 'opened' || github.event.action == 'edited') && github.run_id || 'head-change' }} + group: no-mistakes-required-${{ github.event.pull_request.number }} cancel-in-progress: true jobs: diff --git a/.gitignore b/.gitignore index 372af4735f..5ed2da0c32 100644 --- a/.gitignore +++ b/.gitignore @@ -13,8 +13,6 @@ config/crew-dispatch.json config/secondmate-harness config/backlog-backend config/backend -config/calm config/x-mode.env config/cmux-socket-password config/wedge-alarm -config/herdr-presentation-spaces diff --git a/.no-mistakes.yaml b/.no-mistakes.yaml index 02e6128f2e..b95e69b412 100644 --- a/.no-mistakes.yaml +++ b/.no-mistakes.yaml @@ -9,32 +9,23 @@ # HEAD-continuity guard; see docs/architecture.md "No-mistakes gate authority boundary." disable_project_settings: true -# Trusted documentation placement policy for the Document step. -# The audience inventory and coding guideline own the detail; keep this as a -# pointer so gate instructions cannot become a second prose policy. -document: - instructions: | - Read docs/documentation-audiences.md and its machine-consumed - docs/documentation-audiences.json inventory before changing documentation. - Apply the knowledge-placement policy in - .agents/skills/firstmate-coding-guidelines/SKILL.md. - For changed prose, verify audience, authoritative owner, current relevance, - evidence destination, and unique safety facts, then review the complete - branch diff again after every documentation or lint fix. - -# Pin lint to the same owner CI runs instead of leaving it to no-mistakes' -# default handling, which does not invoke the repository's canonical lint gate. -# `bin/fm-lint.sh` owns the complete lint definition and -# `.github/workflows/ci.yml` invokes it directly, with parity asserted by -# `tests/fm-lint.test.sh`. -# -# Do not set commands.test to a complete tests/*.test.sh walk. Local no-mistakes -# Test is intent-targeted validation of whether the change meets its brief; -# .github/workflows/ci.yml owns broad regression (behavior suite, platform, -# security, Herdr, tmux, and lifecycle coverage). A full-suite override here -# would duplicate CI and defeat the targeted Test contract. +# Pin lint and the portable behavior suite to the same deterministic commands +# the Linux CI jobs run, instead of leaving them to no-mistakes' default handling. +# CI separately owns platform-specific compatibility lanes, including the stock +# macOS Bash snapshot checks. Without a configured +# commands.lint, the gate's lint step never ran the deterministic +# `shellcheck bin/*.sh bin/backends/*.sh tests/*.sh` that CI runs, so info-level +# ShellCheck findings (e.g. SC2015) were not surfaced locally before CI rejected +# them. commands.lint delegates to bin/fm-lint.sh, the single owner of the lint +# definition that .github/workflows/ci.yml also invokes, so local can never +# diverge from CI again (parity asserted by tests/fm-lint.test.sh). +# The test command mirrors the Linux behavior job in .github/workflows/ci.yml: +# iterate every tests/*.test.sh, run each, and fail the step if any one exits +# non-zero (an agent-driven test step has crashed the daemon). The e2e tests need +# tmux on PATH, which the firstmate environment provides. commands: lint: 'bin/fm-lint.sh' + test: 'command -v tmux >/dev/null || { echo "tmux is required for e2e tests" >&2; exit 1; }; tmux -V; rc=0; for t in tests/*.test.sh; do echo "== $t =="; bash "$t" || rc=1; done; exit "$rc"' # Keep test evidence out of this repo; it stays in a temp dir instead. test: diff --git a/.opencode/plugins/fm-primary-turnend-guard.js b/.opencode/plugins/fm-primary-turnend-guard.js index fb8f42eaf4..b38f9c2c47 100644 --- a/.opencode/plugins/fm-primary-turnend-guard.js +++ b/.opencode/plugins/fm-primary-turnend-guard.js @@ -1,7 +1,6 @@ import { spawn } from "node:child_process"; import { realpathSync } from "node:fs"; import { resolve } from "node:path"; -import { encodeFirstmateOperationalInput } from "./lib/fm-operational-input.js"; const COORDINATOR_KEY = "__firstmateOpenCodeWatchArm"; @@ -75,17 +74,18 @@ export const FmPrimaryTurnendGuard = async ({ client, directory, worktree }) => if (result.code !== 2) return; try { - const text = await encodeFirstmateOperationalInput( - root, - "turn-end-guard", - "TURN WOULD END BLIND - supervision is off. " + - "The watcher cycle is missing, failed, or unhealthy. Follow the harness recovery instruction below before ending the turn.\n\n" + - result.stderr, - ); await client.session.promptAsync({ path: { id: sessionID }, body: { - parts: [{ type: "text", text }], + parts: [ + { + type: "text", + text: + "TURN WOULD END BLIND - supervision is off. " + + "Resume supervision according to the session-start operating block before ending the turn.\n\n" + + result.stderr, + }, + ], }, }); skipNextIdle = true; diff --git a/.opencode/plugins/fm-primary-watch-arm.js b/.opencode/plugins/fm-primary-watch-arm.js index 8b98340cfa..15a772d048 100644 --- a/.opencode/plugins/fm-primary-watch-arm.js +++ b/.opencode/plugins/fm-primary-watch-arm.js @@ -1,7 +1,6 @@ import { spawn } from "node:child_process"; import { existsSync, readFileSync, readdirSync, realpathSync } from "node:fs"; import { resolve } from "node:path"; -import { encodeFirstmateOperationalInput } from "./lib/fm-operational-input.js"; const COORDINATOR_KEY = "__firstmateOpenCodeWatchArm"; const ARM_READY_TIMEOUT_MS = Number(process.env.FM_OPENCODE_ARM_READY_TIMEOUT_MS || 12000); @@ -179,12 +178,16 @@ function observeArmOutput(stdout, stderr, settleReadiness) { } } -async function sendPrompt(paths, client, sessionID, text) { - const encoded = await encodeFirstmateOperationalInput(paths.root, "watcher", text); +async function sendPrompt(client, sessionID, text) { await client.session.promptAsync({ path: { id: sessionID }, body: { - parts: [{ type: "text", text: encoded }], + parts: [ + { + type: "text", + text, + }, + ], }, }); } @@ -193,8 +196,8 @@ function wakePrompt(reason) { return `WATCHER FIRED - drain queued wakes with bin/fm-wake-drain.sh and handle the reported wake. Watcher continuity is plugin-owned.\n\n${reason}`; } -function surfaceFailure(paths, client, sessionID, reason) { - void sendPrompt(paths, client, sessionID, wakePrompt(reason)).catch(() => { +function surfaceFailure(client, sessionID, reason) { + void sendPrompt(client, sessionID, wakePrompt(reason)).catch(() => { }); } @@ -256,13 +259,13 @@ async function scheduleRetry(paths, sessionID, client, reason, predecessorArmPid if (child || retryTimer) return; if (!(await sessionOwnsLock(paths))) { setArmStatus("failed"); - surfaceFailure(paths, client, sessionID, `watcher: FAILED - OpenCode cannot restore continuity because this session no longer owns the lock\n${reason}`); + surfaceFailure(client, sessionID, `watcher: FAILED - OpenCode cannot restore continuity because this session no longer owns the lock\n${reason}`); return; } retryFailures += 1; if (retryFailures > REARM_RETRY_LIMIT) { setArmStatus("failed"); - surfaceFailure(paths, client, sessionID, `watcher: FAILED - OpenCode could not restore watcher continuity after ${REARM_RETRY_LIMIT} retries\n${reason}`); + surfaceFailure(client, sessionID, `watcher: FAILED - OpenCode could not restore watcher continuity after ${REARM_RETRY_LIMIT} retries\n${reason}`); return; } setArmStatus("retrying"); @@ -270,7 +273,7 @@ async function scheduleRetry(paths, sessionID, client, reason, predecessorArmPid if (retryTimer === timer) retryTimer = null; void ensureArm(paths, sessionID, client, predecessorArmPid).then((status) => { if (["armed", "starting", "wake"].includes(status)) return; - surfaceFailure(paths, client, sessionID, `watcher: FAILED - OpenCode could not launch a continuity retry (${status})`); + surfaceFailure(client, sessionID, `watcher: FAILED - OpenCode could not launch a continuity retry (${status})`); }); }, retryDelay(retryFailures)); timer.unref(); @@ -341,7 +344,7 @@ function spawnArm(paths, sessionID, client, predecessorArmPid = "") { void restoration.then((failure) => { if (restorationInFlight === restoration) restorationInFlight = null; const message = failure ? `${classification.message}\n\n${failure}` : classification.message; - return sendPrompt(paths, client, sessionID, wakePrompt(message)); + return sendPrompt(client, sessionID, wakePrompt(message)); }).catch(() => { }); return; diff --git a/.opencode/plugins/lib/fm-operational-input.js b/.opencode/plugins/lib/fm-operational-input.js deleted file mode 100644 index f0d05c1444..0000000000 --- a/.opencode/plugins/lib/fm-operational-input.js +++ /dev/null @@ -1,37 +0,0 @@ -import { spawn } from "node:child_process"; -import { existsSync } from "node:fs"; -import { dirname, resolve } from "node:path"; -import { fileURLToPath } from "node:url"; - -const adapterRoot = resolve(dirname(fileURLToPath(import.meta.url)), "../../.."); - -// Cross-language adapter only. bin/fm-operational-input.sh owns the protocol, -// accepted kinds, marker bytes, and serialization grammar. -export function encodeFirstmateOperationalInput(root, kind, content) { - return new Promise((resolveResult, reject) => { - const requested = `${root}/bin/fm-operational-input.sh`; - const script = existsSync(requested) - ? requested - : `${adapterRoot}/bin/fm-operational-input.sh`; - const child = spawn(script, ["encode", kind], { - stdio: ["pipe", "pipe", "pipe"], - }); - let stdout = ""; - let stderr = ""; - child.stdout.on("data", (chunk) => { - stdout += chunk.toString(); - }); - child.stderr.on("data", (chunk) => { - stderr += chunk.toString(); - }); - child.on("error", reject); - child.on("close", (code) => { - if (code === 0 && stdout) { - resolveResult(stdout); - return; - } - reject(new Error(stderr.trim() || `operational-input encoder exited ${code ?? "unknown"}`)); - }); - child.stdin.end(content); - }); -} diff --git a/.pi/extensions/fm-calm.ts b/.pi/extensions/fm-calm.ts deleted file mode 100644 index eb009fd8e3..0000000000 --- a/.pi/extensions/fm-calm.ts +++ /dev/null @@ -1,277 +0,0 @@ -// Firstmate's home-persistent Pi transcript presentation toggle. -// -// Compatibility boundary: Pi 0.81.1 and 0.82.0 expose built-in ToolDefinitions, per-slot -// renderers, renderShell: "self", session_start replacement reasons, -// ExtensionUIContext.setToolsExpanded(), setWorkingVisible(), and -// setHiddenThinkingLabel(). The focused tests pin those assumptions. Version-bounded -// presentation adapters cover collapsed assistant thinking and operational user rows; -// Pi still exposes no global renderer for arbitrary built-in or custom rows. -// docs/configuration.md owns the home-local Calm preference contract. -import { randomUUID } from "node:crypto"; -import { - mkdirSync, - readFileSync, - renameSync, - rmSync, - writeFileSync, -} from "node:fs"; -import { dirname, resolve } from "node:path"; -import { fileURLToPath } from "node:url"; -import type { - ExtensionAPI, - ToolDefinition, - ToolRenderResultOptions, -} from "@earendil-works/pi-coding-agent"; -import { - createBashToolDefinition, - createEditToolDefinition, - createFindToolDefinition, - createGrepToolDefinition, - createLsToolDefinition, - createReadToolDefinition, - createWriteToolDefinition, -} from "@earendil-works/pi-coding-agent"; -import { Box, Container, getKeybindings, type Component } from "@earendil-works/pi-tui"; -import type { TSchema } from "typebox"; -import { installCalmAssistantLayout } from "./lib/fm-calm-assistant-layout.ts"; -import { installCalmOperationalUserLayout } from "./lib/fm-calm-operational-user-layout.ts"; -import { - calmPresentationHides, - calmPresentationIsActive, - FIRSTMATE_CALM_PRESENTATION_EVENT, - registerFirstmateSyntheticPresentation, - setCalmPresentation, - setCalmStockExportRendering, -} from "./lib/fm-calm-visibility.ts"; - -type DefinitionFactory = ( - cwd: string, -) => ToolDefinition; - -type RenderContext = Parameters< - NonNullable["renderCall"]> ->[2]; - -type RenderArgs = Parameters< - NonNullable["renderCall"]> ->[0]; - -type RenderTheme = Parameters< - NonNullable["renderCall"]> ->[1]; - -type RenderResult = Parameters< - NonNullable["renderResult"]> ->[0]; - -type StandardShellState = { - shell?: Box; - call?: Component; - result?: Component; -}; - -const extensionFile = fileURLToPath(import.meta.url); -const extensionDir = dirname(extensionFile); -const root = resolve(extensionDir, "../.."); - -export default function (pi: ExtensionAPI) { - installCalmAssistantLayout(); - installCalmOperationalUserLayout(); - - let exportRendering = false; - let removeTerminalInputHandler: (() => void) | undefined; - - const fmHome = process.env.FM_HOME || process.env.FM_ROOT_OVERRIDE || root; - const configDirectory = process.env.FM_CONFIG_OVERRIDE || resolve(fmHome, "config"); - const calmPreferencePath = resolve(configDirectory, "calm"); - const loadCalmPreference = (): boolean => { - try { - return readFileSync(calmPreferencePath, "utf8").trim() === "on"; - } catch { - return false; - } - }; - const persistCalmPreference = (active: boolean): void => { - mkdirSync(dirname(calmPreferencePath), { recursive: true }); - const temporaryPath = `${calmPreferencePath}.${process.pid}.${randomUUID()}.tmp`; - try { - writeFileSync(temporaryPath, active ? "on\n" : "off\n", { - encoding: "utf8", - flag: "wx", - mode: 0o600, - }); - renameSync(temporaryPath, calmPreferencePath); - } finally { - rmSync(temporaryPath, { force: true }); - } - }; - - const publishPresentationState = (): void => { - pi.events.emit(FIRSTMATE_CALM_PRESENTATION_EVENT, { - active: calmPresentationIsActive(), - stockExportRendering: exportRendering, - }); - }; - - registerFirstmateSyntheticPresentation(pi); - - function registerBuiltIn( - factory: DefinitionFactory, - ): void { - const definitions = new Map>(); - const definitionFor = (cwd: string): ToolDefinition => { - let definition = definitions.get(cwd); - if (!definition) { - definition = factory(cwd); - definitions.set(cwd, definition); - } - return definition; - }; - - const original = definitionFor(process.cwd()); - const originalRenderCall = original.renderCall; - const originalRenderResult = original.renderResult; - const originalSelfShell = original.renderShell === "self"; - const standardShells = new WeakMap(); - - if (!originalRenderCall || !originalRenderResult) { - throw new Error(`Firstmate calm mode requires both render slots for Pi built-in tool ${original.name}`); - } - - const shellStateFor = ( - context: RenderContext, - ): StandardShellState => { - const rowState = context.state as object; - let shellState = standardShells.get(rowState); - if (!shellState) { - shellState = {}; - standardShells.set(rowState, shellState); - } - return shellState; - }; - - const refreshStandardShell = ( - state: StandardShellState, - theme: RenderTheme, - context: RenderContext, - ): Box => { - const background = context.isPartial - ? (text: string) => theme.bg("toolPendingBg", text) - : context.isError - ? (text: string) => theme.bg("toolErrorBg", text) - : (text: string) => theme.bg("toolSuccessBg", text); - const shell = state.shell ?? new Box(1, 1, background); - state.shell = shell; - shell.setBgFn(background); - shell.clear(); - if (state.call) shell.addChild(state.call); - if (state.result) shell.addChild(state.result); - return shell; - }; - - pi.registerTool({ - ...original, - renderShell: "self", - - async execute(toolCallId, params, signal, onUpdate, ctx) { - return definitionFor(ctx.cwd).execute(toolCallId, params, signal, onUpdate, ctx); - }, - - renderCall( - args: RenderArgs, - theme: RenderTheme, - context: RenderContext, - ) { - if (exportRendering) return originalRenderCall(args, theme, context); - if (calmPresentationHides("assistant-tool-call")) return new Container(); - if (originalSelfShell) return originalRenderCall(args, theme, context); - - const state = shellStateFor(context); - state.call = originalRenderCall(args, theme, { - ...context, - lastComponent: state.call, - }); - return refreshStandardShell(state, theme, context); - }, - - renderResult( - result: RenderResult, - options: ToolRenderResultOptions, - theme: RenderTheme, - context: RenderContext, - ) { - if (exportRendering) return originalRenderResult(result, options, theme, context); - if (calmPresentationHides("tool-result")) return new Container(); - if (originalSelfShell) return originalRenderResult(result, options, theme, context); - - const state = shellStateFor(context); - state.result = originalRenderResult(result, options, theme, { - ...context, - lastComponent: state.result, - }); - refreshStandardShell(state, theme, context); - return new Container(); - }, - }); - } - - registerBuiltIn(createReadToolDefinition); - registerBuiltIn(createBashToolDefinition); - registerBuiltIn(createEditToolDefinition); - registerBuiltIn(createWriteToolDefinition); - registerBuiltIn(createGrepToolDefinition); - registerBuiltIn(createFindToolDefinition); - registerBuiltIn(createLsToolDefinition); - - pi.on("session_start", (_event, ctx) => { - exportRendering = false; - setCalmPresentation(loadCalmPreference()); - setCalmStockExportRendering(false); - publishPresentationState(); - ctx.ui.setWorkingVisible(true); - ctx.ui.setHiddenThinkingLabel(calmPresentationIsActive() ? "" : undefined); - ctx.ui.setStatus("firstmate-calm", undefined); - removeTerminalInputHandler?.(); - removeTerminalInputHandler = ctx.ui.onTerminalInput((data) => { - if (!getKeybindings().matches(data, "tui.input.submit")) return; - - const input = ctx.ui.getEditorText().trim(); - if ( - input !== "/share" && - input !== "/export" && - !input.startsWith("/export ") - ) { - return; - } - - exportRendering = true; - setCalmStockExportRendering(true); - publishPresentationState(); - setTimeout(() => { - exportRendering = false; - setCalmStockExportRendering(false); - publishPresentationState(); - const expanded = ctx.ui.getToolsExpanded(); - ctx.ui.setToolsExpanded(!expanded); - ctx.ui.setToolsExpanded(expanded); - }, 0); - }); - }); - - pi.registerCommand("calm", { - description: "Toggle Firstmate's supported conversation-only transcript presentation.", - handler: async (_args, ctx) => { - const active = !calmPresentationIsActive(); - persistCalmPreference(active); - setCalmPresentation(active); - publishPresentationState(); - ctx.ui.setWorkingVisible(true); - ctx.ui.setHiddenThinkingLabel(active ? "" : undefined); - ctx.ui.setStatus("firstmate-calm", undefined); - - const expanded = ctx.ui.getToolsExpanded(); - ctx.ui.setToolsExpanded(!expanded); - ctx.ui.setToolsExpanded(expanded); - }, - }); -} diff --git a/.pi/extensions/fm-primary-pi-watch.ts b/.pi/extensions/fm-primary-pi-watch.ts index 2b8ed99f69..0f7f403e54 100644 --- a/.pi/extensions/fm-primary-pi-watch.ts +++ b/.pi/extensions/fm-primary-pi-watch.ts @@ -4,15 +4,8 @@ import { createHash } from "node:crypto"; import { mkdirSync, readFileSync, writeFileSync } from "node:fs"; import { dirname, resolve } from "node:path"; import { fileURLToPath } from "node:url"; -import type { ExtensionAPI, Theme } from "@earendil-works/pi-coding-agent"; -import { Box, Container, Text, type Component } from "@earendil-works/pi-tui"; +import type { ExtensionAPI } from "@earendil-works/pi-coding-agent"; import { Type } from "typebox"; -import { - type CalmPresentationState, - calmTranscriptClassIsVisible, - FIRSTMATE_CALM_PRESENTATION_EVENT, -} from "./lib/fm-calm-visibility.ts"; -import { encodeFirstmateOperationalInput } from "./lib/fm-operational-input.ts"; type ArmResult = { ok: boolean; @@ -26,36 +19,6 @@ type CloseClassification = { message: string; }; -type WatchToolShellState = { - shell?: Box; - call?: Component; - result?: Component; -}; - -type WatchToolRenderContext = { - isError: boolean; - isPartial: boolean; -}; - -function refreshWatchToolShell( - state: WatchToolShellState, - theme: Theme, - context: WatchToolRenderContext, -): Box { - const background = context.isPartial - ? (text: string) => theme.bg("toolPendingBg", text) - : context.isError - ? (text: string) => theme.bg("toolErrorBg", text) - : (text: string) => theme.bg("toolSuccessBg", text); - const shell = state.shell ?? new Box(1, 1, background); - state.shell = shell; - shell.setBgFn(background); - shell.clear(); - if (state.call) shell.addChild(state.call); - if (state.result) shell.addChild(state.result); - return shell; -} - const extensionFile = fileURLToPath(import.meta.url); const extensionDir = dirname(extensionFile); const root = resolve(extensionDir, "../.."); @@ -71,7 +34,6 @@ const retryMaxMs = positiveInteger("FM_WATCH_REARM_RETRY_MAX_MS", 4000); const retryLimit = positiveInteger("FM_WATCH_REARM_RETRY_LIMIT", 5); const armReadyTimeoutMs = positiveInteger("FM_PI_ARM_READY_TIMEOUT_MS", 12000); const armRetireTimeoutMs = positiveInteger("FM_WATCH_ARM_RETIRE_TIMEOUT_MS", 1000); -const repairOnlyHint = "call fm_watch_arm_pi again only after a later notification says the cycle is missing, failed, or unhealthy"; let child: ChildProcess | null = null; let retryTimer: ReturnType | null = null; @@ -163,22 +125,6 @@ function classifyClose(stdout: string, stderr: string, code: number | null, sign } export default function (pi: ExtensionAPI) { - let calmPresentation: CalmPresentationState = { - active: false, - stockExportRendering: false, - }; - pi.events?.on?.(FIRSTMATE_CALM_PRESENTATION_EVENT, (data) => { - const next = data as Partial; - calmPresentation = { - active: next.active === true, - stockExportRendering: next.stockExportRendering === true, - }; - }); - const calmHides = (itemClass: Parameters[0]): boolean => - calmPresentation.active && - !calmPresentation.stockExportRendering && - !calmTranscriptClassIsVisible(itemClass); - function stopArm(): void { stopping = true; if (retryTimer) clearTimeout(retryTimer); @@ -193,11 +139,10 @@ export default function (pi: ExtensionAPI) { process.once("exit", cleanupOnProcessExit); async function sendWake(message: string): Promise { - const content = encodeFirstmateOperationalInput( - "watcher", + await pi.sendUserMessage( `FIRSTMATE WATCHER WAKE: ${message}\n\nRun bin/fm-wake-drain.sh first and handle the queued wake. Watcher continuity is extension-owned.`, + { deliverAs: "followUp" }, ); - await pi.sendUserMessage(content, { deliverAs: "followUp" }); } function surfaceFailure(message: string): void { @@ -303,18 +248,8 @@ export default function (pi: ExtensionAPI) { }; } markLoaded(); - if (child) { - return { - ok: true, - message: `watcher: unchanged - Pi extension already owns an arm child; no manual re-arm needed; ${repairOnlyHint}`, - }; - } - if (retryTimer) { - return { - ok: true, - message: `watcher: unchanged - Pi extension already owns a scheduled continuity retry; no manual re-arm needed; ${repairOnlyHint}`, - }; - } + if (child) return { ok: true, message: "watcher: healthy - Pi extension already has an arm child" }; + if (retryTimer) return { ok: true, message: "watcher: continuity retry already scheduled by the Pi extension" }; const id = ++seq; const env = { ...process.env, @@ -400,10 +335,7 @@ export default function (pi: ExtensionAPI) { if (restoring) return; scheduleRetry(`watcher: FAILED - Pi extension arm child ${id} failed: ${error.message}`, String(armChild.pid ?? "")); }); - return { - ok: true, - message: `watcher: started Pi extension arm child ${id}; future ordinary re-arms are automatic; ${repairOnlyHint}`, - }; + return { ok: true, message: `watcher: started Pi extension arm child ${id}` }; } pi.on?.("session_start", () => { @@ -425,38 +357,12 @@ export default function (pi: ExtensionAPI) { pi.registerTool?.({ name: "fm_watch_arm_pi", label: "Arm firstmate watcher", - description: "Start the first required Pi watcher cycle, or repair one only after a notification says the cycle is missing, failed, or unhealthy. Do not call after ordinary work or ordinary notifications; the Pi extension re-arms automatically. Never run bin/fm-watch-arm.sh through bash.", - promptSnippet: "Start the first required Pi watcher cycle or repair a cycle reported missing, failed, or unhealthy; ordinary re-arming is automatic.", + description: "Arm Pi watcher supervision. Always use this tool instead of running bin/fm-watch-arm.sh through bash.", + promptSnippet: "Arm firstmate watcher supervision through Pi without a foreground bash arm.", promptGuidelines: [ - "Call fm_watch_arm_pi only for the first required cycle or after a notification says the cycle is missing, failed, or unhealthy. Do not call it after ordinary work, turn completion, or ordinary signal, stale, check, or heartbeat handling because the Pi extension owns re-arming. Never run bin/fm-watch-arm.sh through bash.", + "For Pi watcher supervision, call fm_watch_arm_pi instead of running bin/fm-watch-arm.sh through bash.", ], parameters: Type.Object({}), - renderShell: "self", - renderCall: (_args, theme, context) => { - if (calmHides("assistant-tool-call")) return new Container(); - if (calmPresentation.stockExportRendering) { - return new Text(theme.fg("toolTitle", theme.bold("fm_watch_arm_pi")), 0, 0); - } - const state = context.state as WatchToolShellState; - state.call = new Text(theme.fg("toolTitle", theme.bold("fm_watch_arm_pi")), 0, 0); - return refreshWatchToolShell(state, theme, context); - }, - renderResult: (result, _options, theme, context) => { - if (calmHides("tool-result")) return new Container(); - const output = result.content - .filter((item) => item.type === "text") - .map((item) => item.text) - .join("\n"); - if (calmPresentation.stockExportRendering) { - return new Text(theme.fg("toolOutput", output), 0, 0); - } - const state = context.state as WatchToolShellState; - state.result = output - ? new Text(theme.fg("toolOutput", output), 0, 0) - : new Container(); - refreshWatchToolShell(state, theme, context); - return new Container(); - }, execute: async () => { const result = startArm(); return { diff --git a/.pi/extensions/fm-primary-turnend-guard.ts b/.pi/extensions/fm-primary-turnend-guard.ts index 113a1bcdd8..8a78ac3477 100644 --- a/.pi/extensions/fm-primary-turnend-guard.ts +++ b/.pi/extensions/fm-primary-turnend-guard.ts @@ -4,7 +4,6 @@ import { existsSync, readFileSync, writeFileSync } from "node:fs"; import { dirname, resolve } from "node:path"; import { fileURLToPath } from "node:url"; import type { ExtensionAPI } from "@earendil-works/pi-coding-agent"; -import { encodeFirstmateOperationalInput } from "./lib/fm-operational-input.ts"; let guardFollowupActive = false; @@ -112,12 +111,7 @@ export default function (pi: ExtensionAPI) { markLoaded(); if (!nudge) return; try { - pi.sendMessage({ - customType: "firstmate-sessionstart-nudge", - content: nudge, - display: false, - details: { kind: "session-start" }, - }); + pi.sendMessage({ customType: "firstmate-sessionstart-nudge", content: nudge, display: false }); } catch { } }); @@ -146,13 +140,12 @@ export default function (pi: ExtensionAPI) { guardFollowupActive = true; try { - const content = encodeFirstmateOperationalInput( - "turn-end-guard", + await pi.sendUserMessage( "TURN WOULD END BLIND - supervision is off. " + - "The watcher cycle is missing, failed, or unhealthy. Follow the harness recovery instruction below before ending the turn.\n\n" + + "Resume supervision according to the session-start operating block before ending the turn.\n\n" + result.stderr, + { deliverAs: "followUp" }, ); - await pi.sendUserMessage(content, { deliverAs: "followUp" }); } catch { guardFollowupActive = false; } diff --git a/.pi/extensions/lib/fm-calm-assistant-layout.ts b/.pi/extensions/lib/fm-calm-assistant-layout.ts deleted file mode 100644 index dbb5b2dd5f..0000000000 --- a/.pi/extensions/lib/fm-calm-assistant-layout.ts +++ /dev/null @@ -1,59 +0,0 @@ -import { AssistantMessageComponent } from "@earendil-works/pi-coding-agent"; -import { calmPresentationHides } from "./fm-calm-visibility.ts"; - -type AssistantMessage = Parameters[0]; - -type AssistantMessagePresentationState = { - hiddenThinkingLabel: string; - hideThinkingBlock: boolean; - lastMessage?: AssistantMessage; -}; - -type CalmAssistantLayoutPatch = { - hidesThinking: () => boolean; -}; - -// Keep the introduction-version symbol stable so a compatible upgrade cannot -// double-patch a live process. -const CALM_ASSISTANT_LAYOUT_PATCH = Symbol.for( - "firstmate:calm-assistant-layout:pi-0.81.1", -); - -export function installCalmAssistantLayout(): void { - const registry = globalThis as typeof globalThis & { - [key: symbol]: CalmAssistantLayoutPatch | undefined; - }; - const hidesThinking = (): boolean => calmPresentationHides("assistant-thinking"); - const installed = registry[CALM_ASSISTANT_LAYOUT_PATCH]; - if (installed) { - installed.hidesThinking = hidesThinking; - return; - } - - const patch: CalmAssistantLayoutPatch = { hidesThinking }; - const originalUpdateContent = AssistantMessageComponent.prototype.updateContent; - if (typeof originalUpdateContent !== "function") { - throw new Error("Firstmate Calm requires Pi AssistantMessageComponent.updateContent"); - } - - AssistantMessageComponent.prototype.updateContent = function ( - message: AssistantMessage, - ): void { - const state = this as unknown as AssistantMessagePresentationState; - const hideThinking = - state.hiddenThinkingLabel === "" && - state.hideThinkingBlock && - patch.hidesThinking(); - const presentationMessage = hideThinking - ? { - ...message, - content: message.content.filter((block) => block.type !== "thinking"), - } - : message; - - originalUpdateContent.call(this, presentationMessage); - if (presentationMessage !== message) state.lastMessage = message; - }; - - registry[CALM_ASSISTANT_LAYOUT_PATCH] = patch; -} diff --git a/.pi/extensions/lib/fm-calm-operational-user-layout.ts b/.pi/extensions/lib/fm-calm-operational-user-layout.ts deleted file mode 100644 index 82c69eda01..0000000000 --- a/.pi/extensions/lib/fm-calm-operational-user-layout.ts +++ /dev/null @@ -1,136 +0,0 @@ -// Pi 0.81.1 and 0.82.0 add the ordinary-user spacer and row together. -// This version-bounded adapter changes only that presentation and never message delivery. -import { - InteractiveMode, - UserMessageComponent, -} from "@earendil-works/pi-coding-agent"; -import { calmPresentationHides } from "./fm-calm-visibility.ts"; -import { classifyFirstmateCurrentOperationalText } from "./fm-operational-input.ts"; - -type UserMessageConstructorArgs = ConstructorParameters; -type UserMessageLike = { - role: string; - content: unknown; -}; -type AddMessageOptions = { - populateHistory?: boolean; -}; -type InteractiveModePresentation = { - chatContainer: { - children: unknown[]; - addChild(component: UserMessageComponent): void; - }; - editor: { - addToHistory?(text: string): void; - }; - getMarkdownThemeWithSettings(): UserMessageConstructorArgs[1]; - getUserMessageText(message: UserMessageLike): string; - outputPad: number; -}; -type InteractiveModePrototype = { - addMessageToChat( - this: InteractiveModePresentation, - message: UserMessageLike, - options?: AddMessageOptions, - ): void; -}; -type CalmOperationalUserLayoutPatch = { - hidesOperationalInput: () => boolean; - isOperationalInput: (text: string) => boolean; -}; - -// Keep the introduction-version symbol stable so a compatible upgrade cannot -// double-patch a live process. -const CALM_OPERATIONAL_USER_LAYOUT_PATCH = Symbol.for( - "firstmate:calm-operational-user-layout:pi-0.81.1", -); -const LEGACY_CALM_OPERATIONAL_PREFIX = "\u2063Supervisor escalate ("; - -function contentIsTextOnly(content: unknown): boolean { - if (typeof content === "string") return true; - if (!Array.isArray(content) || content.length === 0) return false; - return content.every( - (block) => - typeof block === "object" && - block !== null && - (block as { type?: unknown }).type === "text" && - typeof (block as { text?: unknown }).text === "string", - ); -} - -export function installCalmOperationalUserLayout(): void { - const registry = globalThis as typeof globalThis & { - [key: symbol]: CalmOperationalUserLayoutPatch | undefined; - }; - const hidesOperationalInput = (): boolean => calmPresentationHides("synthetic-user"); - const isOperationalInput = (text: string): boolean => { - if (!text.includes("\u2063")) return false; - return ( - classifyFirstmateCurrentOperationalText(text) !== undefined || - text.startsWith(LEGACY_CALM_OPERATIONAL_PREFIX) - ); - }; - const installed = registry[CALM_OPERATIONAL_USER_LAYOUT_PATCH]; - if (installed) { - installed.hidesOperationalInput = hidesOperationalInput; - installed.isOperationalInput = isOperationalInput; - return; - } - - const patch: CalmOperationalUserLayoutPatch = { - hidesOperationalInput, - isOperationalInput, - }; - const prototype = InteractiveMode.prototype as unknown as InteractiveModePrototype; - const originalAddMessageToChat = prototype.addMessageToChat; - if (typeof originalAddMessageToChat !== "function") { - throw new Error("Firstmate Calm requires Pi InteractiveMode.addMessageToChat"); - } - - class CalmOperationalUserMessageComponent extends UserMessageComponent { - private readonly hasLeadingSpacer: boolean; - - constructor( - text: UserMessageConstructorArgs[0], - markdownTheme: UserMessageConstructorArgs[1], - outputPad: number, - hasLeadingSpacer: boolean, - ) { - super(text, markdownTheme, outputPad); - this.hasLeadingSpacer = hasLeadingSpacer; - } - - override render(width: number): string[] { - if (patch.hidesOperationalInput()) return []; - const lines = super.render(width); - return this.hasLeadingSpacer ? ["", ...lines] : lines; - } - } - - prototype.addMessageToChat = function ( - message: UserMessageLike, - options?: AddMessageOptions, - ): void { - if (message.role !== "user" || !contentIsTextOnly(message.content)) { - originalAddMessageToChat.call(this, message, options); - return; - } - - const text = this.getUserMessageText(message); - if (!text || !patch.isOperationalInput(text)) { - originalAddMessageToChat.call(this, message, options); - return; - } - - const component = new CalmOperationalUserMessageComponent( - text, - this.getMarkdownThemeWithSettings(), - this.outputPad, - this.chatContainer.children.length > 0, - ); - this.chatContainer.addChild(component); - if (options?.populateHistory) this.editor.addToHistory?.(text); - }; - - registry[CALM_OPERATIONAL_USER_LAYOUT_PATCH] = patch; -} diff --git a/.pi/extensions/lib/fm-calm-visibility.ts b/.pi/extensions/lib/fm-calm-visibility.ts deleted file mode 100644 index 27a03f04c1..0000000000 --- a/.pi/extensions/lib/fm-calm-visibility.ts +++ /dev/null @@ -1,96 +0,0 @@ -import { - getMarkdownTheme, - type ExtensionAPI, - UserMessageComponent, -} from "@earendil-works/pi-coding-agent"; -export const CALM_TRANSCRIPT_CLASSES = [ - "genuine-user-prompt", - "genuine-agent-response", - "assistant-thinking", - "assistant-tool-call", - "tool-result", - "tool-image", - "user-bash", - "skill-invocation", - "custom-message", - "custom-entry", - "compaction-summary", - "branch-summary", - "working-status", - "command-status", - "system-notice", - "cache-notice", - "project-trust-warning", - "synthetic-user", - "synthetic-assistant", - "unknown", -] as const; - -export type CalmTranscriptClass = (typeof CALM_TRANSCRIPT_CLASSES)[number]; - -const CALM_VISIBLE_CLASSES = new Set([ - "genuine-user-prompt", - "genuine-agent-response", - "working-status", -]); - -// Legacy session entries from Calm versions before 2026-07-23 retain this -// presentation type. New operational input stays user-role and is never rerouted. -export const FIRSTMATE_SYNTHETIC_PRESENTATION_TYPE = "firstmate-synthetic-input-presentation"; -export const FIRSTMATE_CALM_PRESENTATION_EVENT = "firstmate:calm-presentation"; - -export type CalmPresentationState = { - active: boolean; - stockExportRendering: boolean; -}; - -export const FIRSTMATE_SYNTHETIC_KINDS = [ - "session-start", - "watcher", - "turn-end-guard", - "away-supervisor", - "from-firstmate", - "launch-brief", - "legacy-operational", -] as const; - -export type FirstmateSyntheticKind = (typeof FIRSTMATE_SYNTHETIC_KINDS)[number]; -type FirstmateSyntheticPresentation = { - content: string; - kind: FirstmateSyntheticKind; -}; - -let calm = false; -let stockExportRendering = false; - -export function calmTranscriptClassIsVisible(itemClass: CalmTranscriptClass): boolean { - return CALM_VISIBLE_CLASSES.has(itemClass); -} - -export function setCalmPresentation(active: boolean): void { - calm = active; -} - -export function setCalmStockExportRendering(active: boolean): void { - stockExportRendering = active; -} - -export function calmPresentationIsActive(): boolean { - return calm; -} - -export function calmPresentationHides(itemClass: CalmTranscriptClass): boolean { - return calm && !stockExportRendering && !calmTranscriptClassIsVisible(itemClass); -} - -export function registerFirstmateSyntheticPresentation(pi: ExtensionAPI): void { - pi.registerEntryRenderer( - FIRSTMATE_SYNTHETIC_PRESENTATION_TYPE, - (entry) => { - if (calmPresentationHides("synthetic-user")) return undefined; - const data = entry.data; - if (!data || typeof data.content !== "string") return undefined; - return new UserMessageComponent(data.content, getMarkdownTheme()); - }, - ); -} diff --git a/.pi/extensions/lib/fm-operational-input.ts b/.pi/extensions/lib/fm-operational-input.ts deleted file mode 100644 index 338312d3f6..0000000000 --- a/.pi/extensions/lib/fm-operational-input.ts +++ /dev/null @@ -1,55 +0,0 @@ -import { spawnSync } from "node:child_process"; -import { dirname, resolve } from "node:path"; -import { fileURLToPath } from "node:url"; - -const operationalInputScript = - process.env.FM_OPERATIONAL_INPUT_SCRIPT || - resolve(dirname(fileURLToPath(import.meta.url)), "../../../bin/fm-operational-input.sh"); - -export const FIRSTMATE_CURRENT_OPERATIONAL_KINDS = [ - "session-start", - "watcher", - "turn-end-guard", - "away-supervisor", - "from-firstmate", - "launch-brief", -] as const; - -export type FirstmateCurrentOperationalKind = - (typeof FIRSTMATE_CURRENT_OPERATIONAL_KINDS)[number]; - -function runOperationalInputCommand( - command: "encode" | "classify" | "kind", - content: string, - kind?: FirstmateCurrentOperationalKind, -): string | undefined { - const args = command === "encode" ? [command, kind ?? ""] : [command]; - const result = spawnSync(operationalInputScript, args, { - encoding: "utf8", - input: content, - maxBuffer: 1024 * 1024, - }); - if (result.status !== 0) return undefined; - return command === "classify" ? result.stdout.replace(/\n$/, "") : result.stdout; -} - -export function encodeFirstmateOperationalInput( - kind: FirstmateCurrentOperationalKind, - content: string, -): string { - const encoded = runOperationalInputCommand("encode", content, kind); - if (encoded === undefined) { - throw new Error(`could not encode Firstmate operational input kind ${kind}`); - } - return encoded; -} - -export function classifyFirstmateOperationalText(content: string): string | undefined { - return runOperationalInputCommand("classify", content); -} - -export function classifyFirstmateCurrentOperationalText( - content: string, -): string | undefined { - return runOperationalInputCommand("kind", content); -} diff --git a/AGENTS.md b/AGENTS.md index 382413e821..cdf7b16820 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -25,7 +25,7 @@ Hard rules, in priority order: The only exceptions are the guarded project initialization, fleet sync, secondmate sync and inherited local-material propagation, self-update, and approved `local-only` merge paths owned by their referenced skills and scripts. Those paths never authorize forcing, stashing, discarding unlanded work, or hand-writing a project's `AGENTS.md`. 2. **Never merge a PR without the captain's explicit word.** - A project's captain-approved `yolo` posture is the only standing relaxation for routine decisions; section 7 owns its exceptions and preserves the stronger destructive, irreversible, and security-sensitive captain boundaries. + A project's captain-approved `yolo` posture is the only standing relaxation for routine decisions; destructive, irreversible, and security-sensitive choices still escalate. 3. **Never tear down unlanded work.** Uncommitted changes are never landed, and `bin/fm-teardown.sh` owns the complete landed-work test. Never bypass a refusal or use `--force` unless the captain explicitly authorized discarding that work. @@ -45,7 +45,7 @@ Never add an agent name as a commit co-author. ## 2. Layout and state -`docs/configuration.md` is the single owner of the top-level operational-home layout and configuration schemas; each producing script's header and help own exact child fields and mutation mechanics. +`docs/configuration.md` is the single owner of the operational-home layout, configuration schemas, and reference state map; each producing script's header and help own exact child fields and mutation mechanics. `FM_HOME` selects an instance's private `data/`, `state/`, `config/`, and `projects/`, while scripts continue to come from their tracked code root. Each secondmate has a persistent isolated `FM_HOME`, including its own state, backlog, projects, and session lock. `bin/fm-send.sh` fails closed unless `FM_HOME` is explicit, so a steer cannot silently resolve against another home. @@ -68,8 +68,6 @@ config/crew-dispatch.json optional crewmate dispatch profiles; LOCAL, gitignore config/secondmate-harness harness the PRIMARY uses to launch SECONDMATE agents, optionally followed by a model and effort token on the same line (" [] []"; section 4); LOCAL, gitignored; absent or "default" harness falls back to config/crew-harness then firstmate's own. The primary's own setting; NOT inherited into secondmate homes (secondmates do not spawn secondmates) config/backlog-backend backlog backend override; LOCAL, gitignored; absent or "tasks-axi" = default tasks-axi backend, "manual" = force routine backlog updates to hand-editing; inherited by secondmate homes (section 10) config/backend runtime session-provider backend override for new tasks; LOCAL, gitignored; absent = falls through to runtime auto-detection (the runtime firstmate itself is executing inside), then tmux; tmux is the verified reference backend (docs/tmux-backend.md), while herdr, zellij, orca, and cmux are experimental spawn backends (docs/herdr-backend.md, docs/zellij-backend.md, docs/orca-backend.md, docs/cmux-backend.md) - herdr and cmux can also be selected by runtime auto-detection, zellij and orca never are (always explicit), and codex-app is not accepted; see docs/codex-app-backend.md; not inherited into secondmate homes -config/calm Pi Calm presentation preference; LOCAL, gitignored, and not inherited; see docs/configuration.md "Pi Calm preference" -config/herdr-presentation-spaces optional presence flag for Herdr's default-off disposable single-task visual projection; LOCAL, gitignored; inherited by secondmate homes; see docs/herdr-backend.md "Optional presentation spaces" config/cmux-socket-password optional cmux control-socket password; LOCAL, gitignored; read fresh on every cmux CLI call and passed through without ever overriding an operator's own ambient CMUX_SOCKET_PASSWORD when absent (docs/cmux-backend.md "Setup") config/wedge-alarm optional away-mode wedge-alarm active-alert directives; LOCAL, gitignored; absent means auto (macOS Notification Center when available); see docs/wedge-alarm.md config/x-mode.env generated X-mode watcher cadence; LOCAL, gitignored; source before arming watcher when present @@ -87,27 +85,22 @@ state/ volatile runtime signals; gitignored .status appended by crewmates: ": " wake-event lines, not current-state truth .turn-ended touched by turn-end hooks .grok-turnend-token firstmate-owned grok hook registry token for the task; removed by teardown - .kimi-turnend-token firstmate-owned Kimi hook registry token for the task; removed by teardown - .meta written by fm-spawn: window=, worktree=, project=, harness=, model=, effort=, kind=, mode=, yolo=, tasktmp=; kind=secondmate also records home= and projects=; a non-default runtime backend records further backend-specific fields (docs/configuration.md "Runtime backend"; bin/fm-backend.sh, section 8); fm-pr-check, including through fm-pr-merge, records one canonical pr= and the forge's pr_head= when available (GitHub pull requests and GitLab merge requests; docs/gitlab-merge-watch.md); fm-x-link appends x_request=, x_request_ts=, x_followups=, and optional x_platform=/x_reply_max_chars= for an X-mode-originated task (section 14) - .herdr-presentation quarantinable attempt and restart-binding journal for Herdr's optional visual projection; never task or endpoint authority; see docs/herdr-backend.md "Optional presentation spaces" + .meta written by fm-spawn: window=, worktree=, project=, harness=, model=, effort=, kind=, mode=, yolo=, tasktmp=; kind=secondmate also records home= and projects=; a non-default runtime backend records further backend-specific fields (docs/configuration.md "Runtime backend"; bin/fm-backend.sh, section 8); fm-pr-check, including through fm-pr-merge, records one canonical pr= and GitHub's pr_head= when available; fm-x-link appends x_request=, x_request_ts=, x_followups=, and optional x_platform=/x_reply_max_chars= for an X-mode-originated task (section 14) .check.sh authenticated slow poll; the watcher dispatches validated PR data and the byte-identified X shim through trusted repository scripts, runs registered custom checks from hash-validated private snapshots, and rejects every other state check without execution .check-trust private content binding created by fm-check-register.sh for an intentional custom check .pr-poll private validated data sidecar for the byte-static PR merge poll .pr-poll-registration private transactional provenance record binding the task, canonical metadata identity, sidecar, and static poll publication - .pr-poll-retirement private identity-bound crash-recovery receipt for one exact validated merged result; removed after its poll artifacts retire .pr-check-quarantine/ private non-runnable storage for checks neutralized by the non-executing migration .pr-check-migration.log private per-task outcomes distinguishing rebuilt or canonically registered replacement polls, quarantined unarmed polls, and incomplete migrations .pr-check-migration-scan-v1 private marker proving the non-executing scan disabled every unsafe legacy check; .pr-check-migration-v1 separately records completed private repairs x-watch.check.sh generated X-mode relay poll shim; present only when opted in (section 14) - pending-replies/ parent-owned secondmate pending-reply records (correlation id, delivery vs reply, recovery, escalation); fm-pending-reply-lib.sh x-inbox/ generated X-mode pending mention payloads; fmx-respond drains it (section 14) - x-context/ generated X-mode durable per-request reply context and one-wake offer markers, keyed by request_id; survives inbox cleanup and expires within seven days (section 14; bin/fm-x-lib.sh) + x-context/ generated X-mode durable per-request reply context (platform/budget), keyed by request_id; survives inbox cleanup so a delayed follow-up recovers the original platform (section 14; bin/fm-x-lib.sh) x-outbox/ generated X-mode dry-run reply and dismiss previews; inspect it when FMX_DRY_RUN is set (section 14) - x-poll.error x-poll.claim-error generated X-mode relay and offer-claim diagnostic dedupe markers + x-poll.error generated X-mode relay diagnostic dedupe marker .wake-queue durable queued wakes: epochseqkindkeypayload .afk durable away-mode flag; present = sub-supervisor may inject escalations (set by /afk, cleared on user return) .watch.lock .wake-queue.lock watcher singleton and queue serialization locks - .claude-autoarm.lock .claude-autoarm-epoch .turnend-claude-blocks Claude Stop auto-arm single-flight, epoch, and guard-budget records; never touch .hash-* .count-* .stale-* .stale-since-* .paused-* .wedge-escalations-* .seen-* .hb-surfaced-* .last-* .heartbeat-streak watcher internals; never touch .watch-triage.log watcher's absorbed-wake debug log (size-capped); never relied on, safe to delete .last-watcher-beat watcher liveness beacon, touched every poll (including while absorbing benign wakes); guard scripts read it @@ -121,25 +114,24 @@ Treat `data/captain.md` as the domain-local record of captain preferences, optio ## 3. Session start (run once at every session start) Run `bin/fm-session-start.sh` exactly once at session start. -Its header is the single owner of composed commands, ordering, and digest contents. -`bin/fm-supervision-instructions.sh` renders the emitted supervision block from `docs/supervision-protocols/`. +Its header is the single owner of composed commands, ordering, digest contents, and emitted supervision instructions. Do not reimplement it by separately running its lock, bootstrap, or initial wake-drain components. -Tracked native session-open adapters only nudge this command; `docs/sessionstart-nudge.md` owns their current behavior and compatibility. +Tracked native session-open adapters only nudge this command; `docs/sessionstart-nudge.md` owns their enforcement mechanics and verification evidence. Read the complete digest once and trust it as this turn's startup and recovery input. Do not separately re-read the context, backlog, metadata, or bulk status inputs it just printed unless a source was reported absent or corrupt, older history is specifically needed, or a targeted workflow must inspect before writing. An `ABSENT` captain, shared-captain, secondmate, or learnings file means the firstmate repo's built-in defaults, no shared captain preferences, no registered secondmates, or no captured learnings; rebuild an absent or stale project registry from the clones before dispatch. -If the session lock cannot be acquired and verified, report its exact diagnostic and remain read-only; another active session is only one possible cause. +If the session lock is refused, tell the captain another active session is managing the fleet and remain read-only. A lock-refused session must not spawn, steer, merge, drain the wake queue, repair supervision, repair a checkout, or perform any other fleet mutation. 1. **Lock** - acquires the per-home session lock first, before anything mutates shared state. 2. **Bootstrap** - detect-only checks (tool/version problems, GitHub auth, the worktree-tangle check, harness override, dispatch-profile validation, backlog-backend status) always run, but routine confirmations stay silent by default. When the lock could not be acquired, the worktree-tangle check uses read-only advisory wording without a checkout repair command. - Home-local stale Herdr projection cleanup and the five bootstrap MUTATING sweeps - non-executing legacy PR-check migration, fleet sync, the local secondmate fast-forward sweep, the secondmate liveness sweep, and X-mode artifact writes - run only when this session actually holds the lock from step 1. - The secondmate liveness sweep deterministically accounts for every registered secondmate: it relaunches only from the recovery-grade `dead` or `missing` states, preserves ambiguous or unreadable targets, and reports skipped or failed guarantees as `SECONDMATE_LIVENESS:` lines (`bin/fm-bootstrap.sh`; `bin/fm-backend.sh`'s `fm_backend_agent_state`). -3. **Wake queue** - when locked, drains the durable wake queue and prints the raw records prominently as this turn's first work queue; a bounded, clearly labeled historical status-event annotation may follow a valid `signal` record but never replaces it or current-state reconciliation, and a lapsed watcher chain still surfaces here via the same guard alarm. - When the lock could not be acquired and verified, the queue is left untouched because no session mutation is authorized, and the guard's tangle/watcher-liveness alarms still print in read-only advisory mode without drain, supervision repair, or checkout repair commands. + The five MUTATING sweeps - non-executing legacy PR-check migration, fleet sync, the local secondmate fast-forward sweep, the secondmate liveness sweep, and X-mode artifact writes - run only when this session actually holds the lock from step 1. + The secondmate liveness sweep deterministically guarantees every registered secondmate is actually running: it probes each live secondmate's endpoint for a real agent process (not just pane presence), respawns only on a confident dead reading, and reports only skipped or failed guarantees as `SECONDMATE_LIVENESS:` lines (`bin/fm-bootstrap.sh`; `bin/fm-backend.sh`'s `fm_backend_agent_alive`). +3. **Wake queue** - when locked, drains the durable wake queue and prints the records prominently as this turn's first work queue, exactly as `bin/fm-wake-drain.sh` did before; a lapsed watcher chain still surfaces here via the same guard alarm. + When the lock could not be acquired, the queue is left untouched because another session owns it, and the guard's tangle/watcher-liveness alarms still print in read-only advisory mode without drain, supervision repair, or checkout repair commands. 4. **Context digest** - the full contents of `data/projects.md`, `data/secondmates.md`, `data/captain.md`, `data/captain-shared.md`, and `data/learnings.md`, each clearly delimited. A file that does not exist prints an explicit `ABSENT` marker, never confused with an empty-but-present file: absence is meaningful (`captain.md` absent means use the firstmate repo's built-in defaults, `projects.md` absent means rebuild it from the clones under `projects/`, etc.). 5. **Fleet-state digest** - the compact backlog listing owned by `bin/fm-session-start.sh`; every `state/.meta`; a bounded tail of each task's `state/.status` (labeled as wake-EVENT history, not current state, with the full log path printed for a deeper read); the `state/.afk` flag; and one cheap alive/dead read of each task's recorded backend endpoint. @@ -158,18 +150,12 @@ A silent bootstrap section needs no action; for any printed actionable diagnosti ## 4. Harness and runtime dispatch Load `harness-adapters` before every spawn or recovery and before trust handling, skill invocation, interrupt, exit, resume, or adapter verification. -The verified harnesses are `claude`, `codex`, `opencode`, `pi`, `grok`, and `kimi`; never dispatch on an unverified adapter. -If static `config/crew-harness` or `config/secondmate-harness` names an unverified adapter, report it and fall back only to a verified adapter rather than launching it. +The verified harnesses are `claude`, `codex`, `opencode`, `pi`, and `grok`; never dispatch on an unverified adapter. +If configured harness data names an unverified adapter, report it and fall back only to a verified adapter rather than launching it. -`docs/configuration.md` owns dispatch-profile and runtime-backend schemas, `bin/fm-harness.sh` owns static resolution, and `bin/fm-spawn.sh` owns launch flags and fail-closed validation. +`docs/configuration.md` owns dispatch-profile and runtime-backend schemas, `bin/fm-dispatch-select.sh` owns selector mechanics, `bin/fm-harness.sh` owns static resolution, and `bin/fm-spawn.sh` owns launch flags and fail-closed validation. When dispatch profiles exist, consult them at every crewmate or scout intake and pass the resolved concrete profile required by `fm-spawn`. Routing precedence is an explicit per-task captain override, then the best-fit configured rule, then the configured default, then the static crewmate harness. -Firstmate alone resolves a matched profile array: run `quota-axi --json` at that intake, evaluate every configured candidate against that current output, and choose the candidate with the most real headroom. -Account for every candidate; if any harness/model/provider relationship, applicable quota data, or interpretation cannot be established, stop and report that candidate instead of omitting it, guessing, falling back, or calling the result quota-informed. -Preserve malformed profile configuration as an actionable error rather than selecting around it. -When every candidate is tight, preserve the captain's strongest-reasoning class rather than silently downgrading it solely to conserve quota; stop and report the tight choice if that class cannot proceed. -Break genuine headroom ties without array-order or harness bias. -`quota-axi` owns how model or product windows relate to bounding account windows. The generic effort fallback and its precedence are owned by `harness-adapters`: explicit captain and standing configured effort win; otherwise use low for well-understood explicit work, xhigh for ambiguous investigation or design, intermediate levels proportionally, and never max without explicit captain preference. Do not add model-specific versions of that policy. @@ -234,22 +220,18 @@ Route by the nature of the work against each registered secondmate scope, not by Keep `local-only` work in the main home. Send in-scope work to the fitting secondmate unless it is blocked or the captain explicitly redirects it; do not read the secondmate's chat because marked routed replies return through its status or referenced document. If no secondmate scope fits, use the main home or discuss creating an appropriate persistent secondmate. -For one-off or infrequent operational work, start with the simplest direct end-to-end path. -Do not build wrappers, control planes, policy layers, custom verifiers, or automation unless the direct path exposes a concrete blocker or repeated need that justifies the added machinery. -Before commissioning an investigation, consult existing reports and established evidence. Classify the deliverable: -- **Ship** is the default and produces a project change through the selected delivery mode; once implementation is authorized, dispatch a ship and keep any remaining bounded research inside it unless unresolved uncertainty could materially change whether or what to build. -- **Scout** produces knowledge in `data//report.md`, never a PR, and is appropriate for investigation, diagnosis, planning, reproduction, or audit work when the captain explicitly requests a separate knowledge or design deliverable or unresolved uncertainty could materially change whether or what to build. +- **Ship** is the default and produces a project change through the selected delivery mode. +- **Scout** produces knowledge in `data//report.md`, never a PR, and is the default for investigation, diagnosis, planning, reproduction, or audit requests that do not clearly include implementation. -If established evidence already answers an informational question, relay it without a design-only scout; when implementation intent is unclear, answer and ask one concise implementation question when useful rather than dispatching speculative design work. -Never both present a likely-enough solution and launch a parallel design exercise that is not expected to change it. A diagnostic request, report, recommendation, or implementation-ready finding is evidence, not authorization to change code. +Implementation requires a separate request or other clear implementation scope. Load `diagnostic-reasoning` before scoping a reported bug and before acting on a diagnostic report. -Treat file or subsystem overlap as a risk signal rather than an automatic reason to wait, and dispatch isolated work immediately with no concurrency cap when each change can be independently implemented and validated and the selected delivery path can reconcile ordinary rebases or conflicts. -Serialize only for a true semantic dependency, shared mutable external state, incompatible concurrent migration, or another concrete condition that makes independent progress or reconciliation unsafe; same-file editing alone is insufficient, and genuine blockers remain durable. +Classify work as dispatchable when it does not overlap work under way, or queued and blocked when it touches the same project subsystem or depends on unlanded work. +Dispatch independent work immediately with no concurrency cap, serialize coarse overlaps, and record blockers durably. Write the task-specific brief under section 11 before spawning. ### Dispatch and supervision handoff @@ -261,7 +243,6 @@ A persistent secondmate is recorded in the secondmate registry and runtime state Steer a worker with short single-line messages through fail-closed `fm-send`; put long instructions in a file. A secondmate's routed reply returns through status or a document pointer, not by firstmate peeking into its chat. -For the parent-owned correlation, recovery, and escalation contract on marked secondmate requests, see `bin/fm-pending-reply-lib.sh`. Supervise all live work under section 8. ### Selected delivery path and approval authority @@ -279,10 +260,7 @@ The path's worker, automated gates, and captain approval remain authoritative: Delivery mode and `yolo` are orthogonal. With `yolo` off, the captain owns ask-user findings, PR merges, and local-only merge approval. -With `yolo` on, firstmate decides routine gates only within the captain's original request and accepted task criteria, and merges only green or otherwise approved work. -Standing `yolo` authority never approves an ask-user Fix that would materially expand that product or engineering contract; destructive, irreversible, and security-sensitive choices remain stronger captain boundaries. -Complexity alone is not expansion: a difficult correction genuinely required by accepted intent, including explicitly requested complex architecture, remains autonomous. -Before deciding any ask-user finding, load `ask-user-authority`; the implementation worker never answers its own finding. +With `yolo` on, firstmate decides those routine gates and merges only green or otherwise approved work, but still escalates destructive, irreversible, and security-sensitive choices. Never merge a red PR. Use `bin/fm-pr-merge.sh` for every task PR merge so merge metadata is recorded, and use `bin/fm-merge-local.sh` for approved local-only landing; never call a lower-level merge command around their guards. After an autonomous merge, give the captain a one-line full-URL or local-main outcome. @@ -298,7 +276,7 @@ Send the same worker one exact decision naming the decision key, step, action, a Require the matching `resolved` event, forbid `--yes`, and require the worker to process every synchronous return until completion or a genuinely new escalation. Resume fleet supervision immediately after the decision lands. -Judge validation by the current-code-matched run step through `bin/fm-crew-state.sh`, not by shell liveness or the last status event. +Judge validation by the branch-matched run step through `bin/fm-crew-state.sh`, not by shell liveness or the last status event. Running, fixing, or CI states remain working; parked approval or fix-review states require the worker to follow the active gate help; passed or checks-passed is done; failed or cancelled is failed. A worker hand-editing, committing, aborting, or restarting during an active validation run duplicates pipeline ownership; steer it back to the gate response flow. The worker reports the PR when CI first becomes green rather than waiting for merge monitoring to finish. @@ -306,7 +284,7 @@ The worker reports the PR when CI first becomes green rather than waiting for me ### PR ready, landing, and teardown For PR-based ship tasks, the ready signal depends on mode: `no-mistakes` reports `done: PR checks green` after CI is green, while `direct-PR` reports `done: PR ` after opening the PR. -Run `bin/fm-pr-check.sh ` - it records `pr=` and the forge's `pr_head=` when available in the task's meta and arms the watcher's merge poll. +Run `bin/fm-pr-check.sh ` - it records `pr=` and GitHub's `pr_head=` when available in the task's meta and arms the watcher's merge poll. Tell the captain the PR's full URL, always the complete `https://...` link rather than a bare `#number`, a concise outcome summary, and the no-mistakes risk level when applicable. A captain instruction to merge is explicit authority; `yolo` is the only standing routine authority. For any custom `state/.check.sh` you write yourself, keep it an ordinary single-link mode-`0700` file, print one line only when firstmate should wake, print nothing otherwise, finish before `FM_CHECK_TIMEOUT`, then bind its current bytes with `bin/fm-check-register.sh ` before the watcher may execute it. @@ -321,11 +299,13 @@ Retire one only on an explicit captain or main-firstmate decision, after loading ### Scout outcome and promotion -A completed scout must leave a self-contained report before its scratch worktree can be discarded; read and relay its findings, record the report as the Done artifact, and re-evaluate the queue. +A completed scout must leave a self-contained report before its scratch worktree can be discarded. +Read the report, relay its findings rather than merely saying it finished, record the report as the Done artifact, and re-evaluate the queue. A report may recommend implementation but does not authorize it. Before treating the investigation or any visual review as complete, load `decision-hold-lifecycle`; teardown enforces that shared completion gate. When implementation is separately authorized, promote the existing scout through `bin/fm-promote.sh` rather than creating a duplicate task. -The promoted worker must inventory scratch state, return to a clean default-branch base, carry over only intended fix changes, create the ship branch, and follow the project's selected delivery path while leaving scratch commits and debug edits behind and turning a reproduced bug into the regression test. +The promoted worker must inventory scratch state, return to a clean default-branch base, carry over only intended fix changes, create the ship branch, and follow the project's selected delivery path. +Scratch commits and debug edits never ride along, and a reproduced bug becomes the regression test. ## 8. Supervision protocol @@ -334,7 +314,7 @@ Fleet supervision is an always-loaded operational contract; `docs/architecture.m Whenever work is under way, keep exactly one live supervision cycle using the emitted protocol for this primary harness. X mode may require that same live cycle with no fleet work. Do not substitute another harness's wait shape, use shell `&`, or create a second cycle when a healthy one already exists. -For every actionable wake, follow the ordinary-wake continuation in the emitted protocol; use its repair action only when the live cycle is missing or failed. +After every actionable wake, resume the emitted protocol as the final action before ending the turn. No turn ends blind while work is under way, including turns described as holding or waiting. At the start of every wake-handling turn, drain the durable wake queue before peeking, reading beyond the reason line, steering, or starting work. @@ -367,7 +347,7 @@ Harness-aware turn-end guards are structural backstops, not permission to omit t Invoke the `/afk` skill when the captain says `/afk`, says they are going afk, `state/.afk` exists, an incoming message starts with `FM_INJECT_MARK`, or any `state/.subsuper-*` marker is involved. The skill owns the daemon procedure; these safety facts remain inline: -- Every current daemon injection uses the `away-supervisor` kind from `bin/fm-operational-input.sh` after `FM_OPERATIONAL_PREFIX` (U+2063 INVISIBLE SEPARATOR followed by `FIRSTMATE_OP: `), while the `/afk` skill owns legacy bare-marker compatibility. +- Every daemon injection starts with `FM_INJECT_MARK` plus U+2063 INVISIBLE SEPARATOR, which distinguishes internal escalation from captain input. - While `state/.afk` exists, the daemon owns supervision; do not arm a separate watcher. - A marked message while away mode is active is internal escalation and does not exit away mode. - A message beginning `/afk` refreshes away mode. @@ -418,7 +398,6 @@ Reach the captain immediately for: - A needed credential or login. Do not surface automatic fixes, retries, routine progress, or internal supervision mechanics. -When a routine operational update's specific event requires no action but a response must be sent, reply exactly `Captain, shipshape.` without characterizing the visible session's unrelated decisions. Batch non-urgent updates into the next natural reply. Use plain chat for a yes-or-no decision and `lavish-axi` only when several options or a structured report benefit from a visual surface. Whenever a PR is mentioned, include its full `https://...` URL before any shorthand reference. @@ -471,7 +450,6 @@ These skills are not captain-invocable; load them only at their precise triggers - `bootstrap-diagnostics` - load whenever the session-start digest's bootstrap section prints an actionable diagnostic line (`MISSING:`, `MISSING_MANUAL:`, `BACKEND_INVALID:`, `NEEDS_GH_AUTH`, `TANGLE:`, `CREW_DISPATCH: invalid`, `FLEET_SYNC:`, `PR_CHECK_MIGRATION:`, `SECONDMATE_SYNC:`, `SECONDMATE_LIVENESS:`, `NUDGE_SECONDMATES:`, or `FMX:`); silence and `BOOTSTRAP_INFO:` need no load. - `diagnostic-reasoning` - load before scoping a reported bug and before acting on a diagnostic report. -- `ask-user-authority` - load before deciding any ask-user finding, regardless of the project's `yolo` posture. - `harness-adapters` - load before spawning or recovering a crewmate or secondmate, handling a trust dialog, sending a harness-specific skill invocation, interrupting or exiting an agent, resuming an exited agent, or verifying a new harness adapter. - `firstmate-orca` - load before switching to Orca, spawning or supervising Orca-backed work, smoke-testing Orca backend behavior, debugging Orca task state, or reconciling Orca-backed task metadata. - `project-management` - load before adding, creating, removing, or initializing a project. diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 7f90f00202..4e7bd8154d 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -10,8 +10,7 @@ We require this to reduce the maintainer's burden of reviewing and merging contr Pushing through it runs an AI-driven review/test/lint pipeline in an isolated worktree, forwards the push upstream only after every check passes, and opens a clean PR automatically. A GitHub Actions check (`Require no-mistakes`) runs on PRs targeting `main` and fails if the body is missing the deterministic signature that no-mistakes writes. -It evaluates every PR opening and body edit independently, so a later edit cannot replace an earlier pending compliance check. -GitHub Actions and Dependabot are exempt so their automation keeps working, but regular contributor PRs without the signature will not be reviewed or merged. +Dependency bots are exempt so their automation keeps working, but regular contributor PRs without the signature will not be reviewed or merged. ## Workflow @@ -48,8 +47,7 @@ See the [no-mistakes quick start](https://kunchenguid.github.io/no-mistakes/star `bin/fm-lint.sh` must pass: it is the single owner of the lint definition (the shellcheck file set, config, and pinned shellcheck version), and both CI and the no-mistakes pre-push gate run it, so local and CI can never diverge. It pins one exact shellcheck version and refuses to run under any other; print it with `bin/fm-lint.sh --required-version` and install that build locally. - Changes to harness adapters (detection in `bin/fm-harness.sh`, launch and hook mechanics in `bin/fm-spawn.sh`, busy signatures in `bin/fm-watch.sh` and `bin/fm-tmux-lib.sh`, cleanup in `bin/fm-teardown.sh`, and facts in `.agents/skills/harness-adapters/SKILL.md`) must be verified empirically against the real harness, never written from documentation alone. -- Changes to runtime session backends (`bin/fm-backend.sh`, `bin/backends/`, and the scripts that dispatch through them) keep current setup and limits in the relevant backend guide and active empirical evidence in [`docs/verification/runtime-backends.md`](docs/verification/runtime-backends.md). -- [`docs/documentation-audiences.md`](docs/documentation-audiences.md) and its machine-consumed inventory own prose classification; run `bin/fm-doc-audience-check.sh` after documentation changes. +- Changes to runtime session backends (`bin/fm-backend.sh`, `bin/backends/`, and the scripts that dispatch through them) need empirical adapter notes in the relevant backend guide: `docs/tmux-backend.md`, `docs/herdr-backend.md`, `docs/zellij-backend.md`, `docs/orca-backend.md`, `docs/cmux-backend.md`, or `docs/codex-app-backend.md` for blocked Codex App transport work. - In Markdown, put each full sentence on its own line. - `README.md` stays a concise overview plus pointers: it never carries a wall of inline detail. Route detail to the most specific `docs/` file (architecture, configuration, or a backend guide) and link to it instead. @@ -63,9 +61,8 @@ There is no reliable way for `bin/fm-brief.sh`'s scaffold to detect that a task' A crewmate picking up such a brief should load the skill even if the brief predates this instruction. When supervising live crewmates, keep firstmate's own long validation or build commands in the background so watcher wakes can still be handled. Crewmate validation follows the installed no-mistakes version's SKILL.md and live `axi` help instead of duplicating gate mechanics in firstmate docs. -Firstmate's wrapper still matters: crewmates route every `ask-user` finding to firstmate, which applies the authority contract in `AGENTS.md`, and crewmates avoid `--yes` because it would bypass that check and any required captain escalation. -Local `.no-mistakes/` state and test evidence stay out of this repo; `.no-mistakes.yaml` keeps evidence in a temp directory and pins the gate's lint command to `bin/fm-lint.sh`, matching the Linux CI lint job. -Local no-mistakes Test is intent-targeted and must not re-run every `tests/*.test.sh`; `.github/workflows/ci.yml` owns the broad behavior suite plus platform-specific compatibility lanes. +Firstmate's wrapper still matters: `ask-user` findings route to the captain through firstmate, and crewmates avoid `--yes` because it silently resolves captain-owned decisions without escalation. +Local `.no-mistakes/` state and test evidence stay out of this repo; `.no-mistakes.yaml` keeps evidence in a temp directory and pins the gate's lint and portable behavior commands to the Linux CI jobs, while `.github/workflows/ci.yml` owns additional platform-specific compatibility lanes. That is firstmate-specific; do not commit `.no-mistakes/evidence/` here even when another no-mistakes-managed target project keeps committed PR evidence. Check and test the toolbelt before pushing: @@ -73,31 +70,14 @@ Check and test the toolbelt before pushing: ```sh for script in bin/*.sh bin/backends/*.sh; do bash -n "$script"; done # syntax-check the toolbelt bin/fm-lint.sh # lint the toolbelt and behavior tests; the single owner CI and the no-mistakes gate both run -bin/fm-test-run.sh tests/.test.sh # one script (primary local focus path, timed) -bin/fm-test-run.sh --family pure-contract-unit # ordinary family-scoped local path (serial, timed) -bin/fm-test-run.sh --changed # conservative changed-file-informed set (never silent full suite) -bin/fm-test-run.sh --proven-isolated --jobs 4 # explicit local parallel of the proven set only (default is serial) -bin/fm-test-run.sh --lane portable-serial # portable serial remainder (watcher/AFK/tmux/stateful) -bin/fm-test-run.sh --check-coverage # prove portable shards + serial + Herdr equal the full inventory -bin/fm-test-run.sh --all # deliberate complete regression (optional local full walk; not no-mistakes Test) -bin/fm-test-isolation-proof.sh --list # proven parallel candidate set (Phase 2 owner) -bin/fm-test-isolation-proof.sh --jobs 4 --json /tmp/fm-isolation-proof.json # re-run concurrent isolation proof only +for test_script in tests/*.test.sh; do bash "$test_script"; done # behavior tests, matching CI and no-mistakes commands.test [ "$(readlink CLAUDE.md)" = "AGENTS.md" ] [ "$(readlink .claude/skills)" = "../.agents/skills" ] tmp=$(mktemp -d) && printf 'done: smoke\n' > "$tmp/smoke.status" && FM_STATE_OVERRIDE="$tmp" FM_SIGNAL_GRACE=1 FM_POLL=1 FM_HEARTBEAT=999999 bin/fm-watch-arm.sh # watcher re-arm smoke test (prints arm status, then an actionable signal) ``` -`bin/fm-test-run.sh` is the single owner of behavior-suite selection, portable CI lane composition, optional local `--jobs` for the proven-isolated set only, per-script timing markers, family totals, the coverage guard, and the optional JSON timing artifact. -Its header and `--help` own the flags, family labels, lanes, and changed-file map; this section only documents the entry points. -`bin/fm-test-isolation-proof.sh` remains the single owner of the Phase 2 concurrent isolation proof and the exact proven candidate set; see `docs/fm-test-isolation-proof.md`. -Portable shard balance evidence lives in `docs/fm-test-portable-shards.md`. -Local no-mistakes Test stays intent-targeted and must not wire `commands.test` to `--all` or a `tests/*.test.sh` walk. -Family selection is the ordinary local path; `--all` is deliberate full regression only. -CI owns broad regression across required portable parallel shards, the portable serial lane, the Herdr lane, lint, invariants, the coverage guard, and macOS snapshot compatibility in [`.github/workflows/ci.yml`](.github/workflows/ci.yml). -Use `bin/fm-test-run.sh --help` for lane names, `--jobs` rules, and required gate-skip flags when reproducing a lane locally. -Discover tests by listing `tests/*.test.sh`: each is a self-contained bash script named `.test.sh`, and its header comment describes what it covers, so pass one to `bin/fm-test-run.sh` to focus on a subject with canonical timing output. -Tests that need a real optional backend or an explicit opt-in (real herdr/zellij/cmux smoke tests, the live Pi regression) skip themselves and print the tool or environment gate needed to enable them, so the portable suite remains safe on machines without those tools. -The [Herdr backend guide](docs/herdr-backend.md#destructive-lab-safety) owns the lane's isolation boundary, while [runtime backend verification](docs/verification/runtime-backends.md#herdr) owns active empirical evidence; live harness credential tests remain opt-in. +Discover tests by listing `tests/*.test.sh`: each is a self-contained bash script named `.test.sh`, and its header comment describes what it covers, so run one directly to focus on a subject. +Tests that need a real optional backend or an explicit opt-in (real herdr/zellij/cmux smoke tests, the live Pi regression) skip themselves and print the tool or environment gate needed to enable them, so the run-all loop above is always safe. ## Questions diff --git a/README.md b/README.md index 0546576265..407aa376dc 100644 --- a/README.md +++ b/README.md @@ -44,12 +44,12 @@ Launching a supported harness inside it instantiates your first mate - and makes - **One liaison** - you talk only to the first mate; it dispatches, supervises, escalates only real decisions, and reports plain outcomes. - **A visible crew** - every crewmate works in its own tmux window, experimental herdr/zellij tab, cmux workspace, or Orca terminal you can watch or type into; the first mate reconciles. - **Disposable worktrees** - each task runs in a clean [treehouse](https://github.com/kunchenguid/treehouse) git worktree, or an Orca-managed worktree when `backend=orca`, so parallel work on one repo never collides. -- **Two task shapes** - ship tasks deliver authorized changes; scout tasks leave standalone investigation reports when the intake contract warrants separate research. +- **Two task shapes** - ship tasks deliver a change; scout tasks investigate, plan, reproduce, or audit and leave a report. - **Explicit project modes** - each project ships via `no-mistakes`, `direct-PR`, or `local-only`, with an optional `+yolo` autonomy flag. - **Optional secondmates** - opt in to persistent second mates that run from isolated firstmate homes with their own `FM_HOME`, state, projects, and session lock, supervising project clones or a project-less firstmate-repo domain, kept on the primary firstmate version by guarded local fast-forwards and checked for live agent processes at session start. - **Event-driven, zero-token supervision** - a bash watcher sleeps on the fleet and wakes the first mate only when something needs you; verified primary harnesses also get a turn-end backstop that blocks or follows up on a blind stop when work is under way and supervision is not live. - **Optional X mode** - opt in with one local `.env` token so firstmate can answer your public `@myfirstmate` mentions, act on normal reversible mention requests through the same lifecycle as chat requests, acknowledge spawned work, and post up to three public-safe completion follow-ups within seven days for genuine milestones and the final outcome without changing non-X behavior; dry-run preview records would-be replies and dismissals locally before go-live. -- **Guarded by construction** - the first mate is read-only over your projects except for the guarded paths authorized by [hard rule 1](AGENTS.md#1-identity-and-prime-directives), with fleet sync's safe branch pruning remaining part of the fleet-sync exception; crewmates make every project change behind the configured merge authority. +- **Guarded by construction** - the first mate is read-only over your projects outside guarded clone refreshes, safe branch pruning, and approved `local-only` fast-forward merges; crewmates make every project change behind the configured merge authority. - **Restart-proof** - all state lives on disk and in the active session backend (tmux by hard default, herdr or cmux when selected or auto-detected, zellij/orca when explicitly selected); kill the session anytime and the next one reconciles, including confirmed-dead secondmate agents, and carries on. Full detail on every feature lives in [docs/architecture.md](docs/architecture.md). @@ -58,17 +58,16 @@ Full detail on every feature lives in [docs/architecture.md](docs/architecture.m ### Requirements -- A verified primary agent harness: Claude Code, Grok, Pi, Codex, or OpenCode. +- A verified agent harness: Claude Code, Grok, Pi, Codex, or OpenCode. - Git and the GitHub CLI, authenticated through `gh auth login`. -- The CLI and dependencies for your selected runtime backend; tmux is the reference default. +- tmux, for the reference session backend. -The first mate detects and offers to install supported missing tools after you approve. -Backend-specific setup is linked in [Documentation](#documentation). +The first mate detects and offers to install everything else. ### Recommended harnesses **Claude Code, Grok, and Pi are equal co-primary recommendations** for running the primary firstmate session. -Claude Code uses a tracked Stop hook for tokenless watcher re-arm and rewake, Grok uses background-notify wake cycles, and Pi uses its tracked primary watcher extension. +Claude Code and Grok use background-notify wake cycles; Pi uses its tracked primary watcher extension. All three have verified turn-end guard paths when launched with their documented setup. Pick whichever one matches your subscription and workflow. @@ -103,11 +102,7 @@ pi ``` For Grok, `--trust` is needed once per clone so project hooks and the turn-end guard load; `/hooks-trust` inside Grok works too. -For Pi, approve the project trust prompt once per clone on first launch so the tracked `.pi/extensions/*.ts` files auto-load. -Pi's `/calm` toggle hides supported transcript chrome, including canonically classified Firstmate operational user rows, while retaining native working activity and all model context and session data. -The hidden operational inputs remain ordinary user-role messages with unchanged delivery, ordering, authority, persistence, and exports. -The preference persists for the effective Firstmate home, and toggling it off restores ordinary rendering. -[Calm's current behavior and supported limits](docs/calm.md) are separate from its [version-scoped maintainer evidence](docs/calm-mode-feasibility.md). +For Pi, approve the project trust prompt once per clone on first launch so both tracked `.pi/extensions/*.ts` files auto-load. ### Talk to it @@ -115,7 +110,8 @@ The preference persists for the effective Firstmate home, and toggling it off re > ahoy! look at my github project xyz, then fix the flaky login test and add dark mode # firstmate checks its toolchain (asking your consent before installing anything), -# clones the project under projects/ and spawns two isolated workers in the active backend. +# clones the project under projects/, and spawns two crewmates in the active backend +# fm-fix-login-k3 and fm-dark-mode-p7. # Minutes later: PR ready for review, captain: https://github.com/you/xyz/pull/42 @@ -168,7 +164,6 @@ Claude and grok use the slash form shown here; codex uses the same names with `$ | Skill | What it does | | ------------------ | -------------------------------------------------------------------------------------------------------------------------------------------- | | `/afk` | Enter away-mode supervision: the sub-supervisor self-handles routine notifications in bash, escalates captain-relevant events and bounded declared-external-wait rechecks as batched digests, and actively alerts if delivery gets stuck while you step away | -| `/ahoy` | Recap visible session events since the prior real captain message plus visibly unanswered captain decisions, falling back to Bearings when invoked as the session's first real captain message | | `/bearings` | Generate a standalone current-status report from bounded local fleet and registered-secondmate state, with live PR enrichment only when requested, written to a dated file in `data/` and surfaced concisely in chat; read-mostly, mutates no task state | | `/updatefirstmate` | Self-update the running firstmate and its secondmates to the latest from origin with fast-forward-only pulls, then re-read instructions and nudge secondmates | | `/stow` | Sweep the session for uncaptured durable knowledge, route each finding to its disk home per AGENTS.md, file undone next steps to the backlog, and report what is now safe to reset | @@ -187,23 +182,18 @@ Firstmate's skills live in two separate places with different audiences: ## Documentation -- [docs/architecture.md](docs/architecture.md) - maintainer architecture for the crew, supervision, worktrees, secondmates, and project modes. +- [docs/architecture.md](docs/architecture.md) - how the crew, supervision, worktrees, secondmates, and project modes work. - [docs/configuration.md](docs/configuration.md) - environment variables, `FM_HOME`, runtime backend selection, optional X mode, the files you set, and harness support. -- [docs/calm.md](docs/calm.md) - current Pi `/calm` behavior and supported presentation limits. - [docs/wedge-alarm.md](docs/wedge-alarm.md) - configure the active alert for an away-mode escalation delivery that gets stuck. -- [docs/tmux-backend.md](docs/tmux-backend.md) - current setup and limits for the tmux reference backend. -- [docs/herdr-backend.md](docs/herdr-backend.md) - current setup, safety boundaries, and limits for the experimental Herdr backend. -- [docs/zellij-backend.md](docs/zellij-backend.md) - current setup and limits for the experimental Zellij backend. -- [docs/orca-backend.md](docs/orca-backend.md) - current setup and limits for the experimental Orca backend. -- [docs/cmux-backend.md](docs/cmux-backend.md) - current setup, socket security, and limits for the experimental cmux backend. -- [docs/codex-app-backend.md](docs/codex-app-backend.md) - the current blocked Codex App backend boundary and rollout contract. -- [docs/verification/runtime-backends.md](docs/verification/runtime-backends.md) - active maintainer verification for runtime backend guarantees. -- [docs/gitlab-merge-watch.md](docs/gitlab-merge-watch.md) - maintainer verification for GitLab merge watching on arbitrary instances. -- [docs/turnend-guard.md](docs/turnend-guard.md) - the primary session's current "no turn ends blind" backstop, scope, loop safety, and compatibility limits. -- [docs/verification/supervision.md](docs/verification/supervision.md) - active maintainer verification for session-start, guard, continuity, and wedge integrations. +- [docs/tmux-backend.md](docs/tmux-backend.md) - setup guide for the tmux reference backend: prerequisites, attaching, and watching crew windows. +- [docs/herdr-backend.md](docs/herdr-backend.md) - setup guide for the experimental herdr backend, plus its verification notes and known gaps. +- [docs/zellij-backend.md](docs/zellij-backend.md) - setup guide for the experimental zellij backend, plus its verification notes and known gaps. +- [docs/orca-backend.md](docs/orca-backend.md) - setup guide for the experimental Orca backend, plus its lifecycle notes and known gaps. +- [docs/cmux-backend.md](docs/cmux-backend.md) - setup guide for the experimental cmux backend, plus its verification notes and known gaps. +- [docs/codex-app-backend.md](docs/codex-app-backend.md) - Codex App backend boundary, evidence, and rollout contract. +- [docs/turnend-guard.md](docs/turnend-guard.md) - the primary session's structural "no turn ends blind" backstop: verified per-harness hook mechanisms, scoping, loop safety, and fail-open tradeoffs. - [docs/supervision-protocols/](docs/supervision-protocols/) - rendered primary-harness watcher protocols for Claude, Codex, OpenCode, Pi, Grok, and unknown harness fallback. - [docs/scripts.md](docs/scripts.md) - the `bin/` toolbelt reference. -- [docs/documentation-audiences.md](docs/documentation-audiences.md) - documentation audiences and the machine-checked placement boundary. - [`AGENTS.md`](AGENTS.md) - the distro's always-loaded operating contract and routing index for conditional procedures. - [CONTRIBUTING.md](CONTRIBUTING.md) - how to contribute, including the dev/test commands. diff --git a/bin/backends/cmux.sh b/bin/backends/cmux.sh index 12dc7629eb..69dc0b53bd 100644 --- a/bin/backends/cmux.sh +++ b/bin/backends/cmux.sh @@ -581,8 +581,8 @@ fm_backend_cmux_composer_state() { # [expected-label] -> empty|pending # has since moved its own confirmation to a native agent-state read instead # (docs/herdr-backend.md "Native agent-state submit confirmation"); cmux has # no analogous native primitive, so this composer-row approach remains -# cmux's own confirmation strategy. Echoes empty|pending|unknown|send-failed, a -# subset of the proof-carrying submit vocabulary. +# cmux's own confirmation strategy. Echoes empty|pending|unknown|send-failed, the +# SAME vocabulary every existing backend already speaks. fm_backend_cmux_send_text_submit() { # [expected-label] local target=$1 text=$2 retries=$3 sleep_s=$4 settle=$5 expected_label=${6:-} i=0 state fm_backend_cmux_parse_target "$target" || { printf 'unknown'; return 0; } diff --git a/bin/backends/herdr-workspace-move.py b/bin/backends/herdr-workspace-move.py deleted file mode 100755 index 020e77b2a3..0000000000 --- a/bin/backends/herdr-workspace-move.py +++ /dev/null @@ -1,114 +0,0 @@ -#!/usr/bin/env python3 -"""Send one narrowly scoped workspace.move request to a Herdr control socket. - -This helper is the wire transport for Firstmate's optional presentation-only -workspace ordering. It accepts only an exact workspace id and a non-negative -insert index, sends only the non-destructive ``workspace.move`` method, and -prints the verified JSON response. - -Wire protocol verified against Herdr 0.7.4, protocol 16: - - request: {"id":"fm-workspace-move","method":"workspace.move", - "params":{"workspace_id":W,"insert_index":N}}\n - response: {"id":"fm-workspace-move","result": - {"type":"workspace_list","workspaces":[...]}}\n -Usage: herdr-workspace-move.py - -Exit status: - 0 the server returned the matching workspace_list response; - 2 arguments or socket connection were invalid; - 3 the request could not be sent or its response could not be read; - 4 the response was malformed, mismatched, or reported an error. -""" - -import json -import socket -import sys -import time - - -CONNECT_TIMEOUT = 5.0 -RESPONSE_TIMEOUT = 5.0 -RECV_CHUNK = 65536 -MAX_RESPONSE_BYTES = 4 * 1024 * 1024 -REQUEST_ID = "fm-workspace-move" - - -def _read_line(sock, deadline): - buffer = b"" - while b"\n" not in buffer: - remaining = deadline - time.monotonic() - if remaining <= 0: - return None - sock.settimeout(remaining) - try: - chunk = sock.recv(RECV_CHUNK) - except (OSError, socket.timeout): - return None - if not chunk: - return None - buffer += chunk - if len(buffer) > MAX_RESPONSE_BYTES: - return None - return buffer.split(b"\n", 1)[0] - - -def main(argv): - if len(argv) != 4: - return 2 - socket_path, workspace_id, raw_index = argv[1:] - if not socket_path.startswith("/") or not workspace_id: - return 2 - if any(char in workspace_id for char in "\t\r\n"): - return 2 - try: - insert_index = int(raw_index) - except ValueError: - return 2 - if insert_index < 0 or str(insert_index) != raw_index: - return 2 - - try: - sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) - sock.settimeout(CONNECT_TIMEOUT) - sock.connect(socket_path) - except OSError: - return 2 - - request = { - "id": REQUEST_ID, - "method": "workspace.move", - "params": {"workspace_id": workspace_id, "insert_index": insert_index}, - } - try: - sock.sendall( - (json.dumps(request, separators=(",", ":")) + "\n").encode("utf-8") - ) - except OSError: - return 3 - - line = _read_line(sock, time.monotonic() + RESPONSE_TIMEOUT) - if line is None: - return 3 - try: - response = json.loads(line.decode("utf-8", "replace")) - except ValueError: - return 4 - result = response.get("result") if isinstance(response, dict) else None - if ( - response.get("id") != REQUEST_ID - or response.get("error") is not None - or not isinstance(result, dict) - or result.get("type") != "workspace_list" - or not isinstance(result.get("workspaces"), list) - ): - return 4 - sys.stdout.write(json.dumps(response, separators=(",", ":")) + "\n") - return 0 - - -if __name__ == "__main__": - try: - sys.exit(main(sys.argv)) - except (BrokenPipeError, KeyboardInterrupt): - sys.exit(3) diff --git a/bin/backends/herdr.sh b/bin/backends/herdr.sh index 3a28daaa33..260d2b6663 100644 --- a/bin/backends/herdr.sh +++ b/bin/backends/herdr.sh @@ -11,25 +11,14 @@ # normal operation; the unit tests source it directly, so the FM_HOME fallback # below keeps that path sane without fm-backend.sh's preamble. # -# Default container shape (D4, decided empirically - see -# herdr-verification-p2.md "Task container shape", refined by -# docs/herdr-backend.md "Default task container shape"): ONE herdr workspace PER -# FIRSTMATE HOME (the primary, and each secondmate, gets its own), ONE herdr TAB -# per task inside its home's workspace. An optional, default-off presentation -# flag creates a disposable workspace for a clean fresh task instead. That -# workspace is a non-authoritative visual projection containing only the normal -# task pane. Its random token and mutable label never authorize lookup, -# adoption, reuse, closure, deletion, task ownership, or endpoint selection. -# A version 2 journal can participate in replacing only its exact same-identity -# endpoint after metadata, home, session, workspace, tab, pane, parent, shape, -# focus, and agent-absence checks all agree under the session lock. -# Every ambiguous recovered launch uses the default flat home workspace when -# duplicate-agent risk is independently absent. -# Target resolution stays parallel to the tmux adapter in both layouts. -# Projected create, move, and cleanup operations capture the named session's -# exact active workspace and tab. Herdr 0.7.4's last-pane close can focus an -# unrelated neighbor, so projected cleanup serializes and restores only the -# exact pre-close tab id, while refusing to close the active tab itself. +# Container shape (D4, decided empirically - see herdr-verification-p2.md +# "Task container shape", refined by docs/herdr-backend.md "Task container +# shape"): ONE herdr workspace PER FIRSTMATE HOME (the primary, and each +# secondmate, gets its own), ONE herdr TAB per task inside its home's +# workspace. Workspace-per-task was tried and rejected (bad human-watching +# ergonomics); workspace-per-HOME keeps that same rejection while giving every +# home its own space, labeled distinctly, in the shared spaces sidebar. Target +# resolution and the human-watch story stay parallel to the tmux adapter. # # Target string shape: ":", e.g. "default:w1:p2" (the # pane id itself contains a colon; the session is always the FIRST field, the @@ -40,11 +29,10 @@ # function has no herdr-specific logic; it just returns meta's window= # verbatim). # -# Authoritative task recovery/orphan discovery (ids may not deterministically match live state +# Recovery/orphan discovery (ids may not deterministically match live state # after a server restart in a differently-configured session; see the # verification doc) uses LABEL matching (fm- tab labels), never trusts a -# stored pane id blindly: fm_backend_herdr_list_live. The presentation journal -# is deliberately excluded from that path. +# stored pane id blindly: fm_backend_herdr_list_live. # # Requires: herdr (CLI + socket), jq (JSON parsing). Bootstrap detects these # through fm_backend_required_tools only when herdr is the resolved backend; @@ -85,11 +73,6 @@ FM_BACKEND_HERDR_MIN_PROTOCOL=14 # (14): the adapter's spawn/capture/send primitives work on 14, only the push # subscriber needs 16. FM_BACKEND_HERDR_MIN_EVENTS_PROTOCOL=16 -# workspace.move first appears in the protocol-16 schema. -# The installed CLI does not expose it as a workspace subcommand, so the -# presentation path uses one narrowly whitelisted raw-socket request after -# verifying the exact method and parameter schema. -FM_BACKEND_HERDR_MIN_WORKSPACE_MOVE_PROTOCOL=16 # Per-pane escalation dedupe marker prefix, under the state dir. One marker per # window (keyed like the watcher's own .stale-): set when a ->blocked edge # is enqueued, cleared on any working edge, so exactly one wake fires per @@ -100,18 +83,9 @@ FM_BACKEND_HERDR_ESCALATED_PREFIX=".herdr-escalated-" # at a seeded secondmate home's root, containing exactly that secondmate's id. # The primary firstmate home never carries this marker. FM_BACKEND_HERDR_SECONDMATE_MARKER=".fm-secondmate-home" -# The default-off presentation projection is intentionally separate from the -# authoritative task endpoint record. -# A per-task journal lives under state/ as .herdr-presentation. -# Version 1 records only the attempted projection's random correlator. -# Version 2 additionally binds the successful projection's exact home, -# session, workspace, tab, pane, parent, and presentation labels so a resumed -# spawn can replace one verified agent-free husk under the session lock. -# No send, capture, Treehouse, or general task-ownership path reads it. -FM_BACKEND_HERDR_PRESENTATION_JOURNAL_SUFFIX=".herdr-presentation" # fm_backend_herdr_workspace_label: the per-firstmate-HOME herdr workspace -# label (docs/herdr-backend.md "Default task container shape"). The PRIMARY home (no +# label (docs/herdr-backend.md "Task container shape"). The PRIMARY home (no # secondmate marker) resolves to the constant "firstmate", byte-identical to # every pre-existing task's recorded label - no forced migration. A SECONDMATE # home resolves to "2ndmate-", so its tasks land in their own @@ -195,613 +169,6 @@ fm_backend_herdr_session() { printf '%s' "${HERDR_SESSION:-default}" } -# fm_backend_herdr_projection_id: generate a compact 128-bit base64url token. -# The token is a non-adversarial visual correlator, never destructive -# authority. -fm_backend_herdr_projection_id() { - local token - token=$(dd if=/dev/urandom bs=16 count=1 2>/dev/null \ - | base64 \ - | tr '+/' '-_' \ - | tr -d '=\r\n') || return 1 - [ "${#token}" -eq 22 ] || return 1 - case "$token" in - *[!A-Za-z0-9_-]*) return 1 ;; - esac - printf '%s' "$token" -} - -fm_backend_herdr_projection_journal_path() { # - printf '%s/%s%s' "$1" "$2" "$FM_BACKEND_HERDR_PRESENTATION_JOURNAL_SUFFIX" -} - -# fm_backend_herdr_projection_journal_create: atomically publish the -# non-authoritative attempt journal before any projection workspace create. -# A hard-link publication in the same state directory gives create-if-absent -# semantics, so concurrent attempts cannot overwrite each other's token. -fm_backend_herdr_projection_journal_create() { # - local state=$1 id=$2 journal token tmp - case "$id" in - ''|.*|*[!A-Za-z0-9._-]*) - echo "error: invalid task id for herdr presentation journal" >&2 - return 1 - ;; - esac - mkdir -p "$state" || return 1 - journal=$(fm_backend_herdr_projection_journal_path "$state" "$id") - if [ -e "$journal" ] || [ -L "$journal" ]; then - echo "error: herdr presentation journal already exists for $id; refusing a concurrent or repeated projected create" >&2 - return 1 - fi - token=$(fm_backend_herdr_projection_id) || { - echo "error: could not generate a 128-bit herdr presentation projection id" >&2 - return 1 - } - tmp=$(mktemp "$state/.${id}.herdr-presentation.XXXXXX") || return 1 - chmod 0600 "$tmp" || { rm -f "$tmp"; return 1; } - if ! { - printf 'version=1\n' - printf 'task_id=%s\n' "$id" - printf 'projection_id=%s\n' "$token" - } > "$tmp"; then - rm -f "$tmp" - return 1 - fi - if ! ln "$tmp" "$journal" 2>/dev/null; then - rm -f "$tmp" - echo "error: herdr presentation journal appeared concurrently for $id; refusing projected create" >&2 - return 1 - fi - rm -f "$tmp" - printf '%s' "$token" -} - -fm_backend_herdr_projection_journal_field() { # - local journal=$1 key=$2 count - count=$(grep -c "^${key}=" "$journal" 2>/dev/null || true) - [ "$count" = 1 ] || return 1 - grep "^${key}=" "$journal" 2>/dev/null | cut -d= -f2- -} - -# fm_backend_herdr_projection_journal_snapshot: validate a version 1 attempt -# journal or a version 2 exact projection binding without sourcing shell code. -# Version 2 sets FM_BACKEND_HERDR_JOURNAL_* globals for same-process callers. -fm_backend_herdr_projection_journal_snapshot() { # - local journal=$1 id=$2 lines expected_label expected_task_label exact - FM_BACKEND_HERDR_JOURNAL_VERSION="" - FM_BACKEND_HERDR_JOURNAL_TASK_ID="" - FM_BACKEND_HERDR_JOURNAL_PROJECTION_ID="" - FM_BACKEND_HERDR_JOURNAL_HOME="" - FM_BACKEND_HERDR_JOURNAL_SESSION="" - FM_BACKEND_HERDR_JOURNAL_WORKSPACE_ID="" - FM_BACKEND_HERDR_JOURNAL_TAB_ID="" - FM_BACKEND_HERDR_JOURNAL_PANE_ID="" - FM_BACKEND_HERDR_JOURNAL_PARENT_WORKSPACE_ID="" - FM_BACKEND_HERDR_JOURNAL_PARENT_LABEL="" - FM_BACKEND_HERDR_JOURNAL_WORKSPACE_LABEL="" - FM_BACKEND_HERDR_JOURNAL_TASK_LABEL="" - [ -f "$journal" ] && [ ! -L "$journal" ] || return 1 - lines=$(wc -l < "$journal" 2>/dev/null | tr -d '[:space:]') - FM_BACKEND_HERDR_JOURNAL_VERSION=$(fm_backend_herdr_projection_journal_field "$journal" version) || return 1 - FM_BACKEND_HERDR_JOURNAL_TASK_ID=$(fm_backend_herdr_projection_journal_field "$journal" task_id) || return 1 - FM_BACKEND_HERDR_JOURNAL_PROJECTION_ID=$(fm_backend_herdr_projection_journal_field "$journal" projection_id) || return 1 - [ "$FM_BACKEND_HERDR_JOURNAL_TASK_ID" = "$id" ] || return 1 - [ "${#FM_BACKEND_HERDR_JOURNAL_PROJECTION_ID}" -eq 22 ] || return 1 - case "$FM_BACKEND_HERDR_JOURNAL_PROJECTION_ID" in - *[!A-Za-z0-9_-]*) return 1 ;; - esac - case "$FM_BACKEND_HERDR_JOURNAL_VERSION:$lines" in - 1:3) return 0 ;; - 2:12) ;; - *) return 1 ;; - esac - FM_BACKEND_HERDR_JOURNAL_HOME=$(fm_backend_herdr_projection_journal_field "$journal" home) || return 1 - FM_BACKEND_HERDR_JOURNAL_SESSION=$(fm_backend_herdr_projection_journal_field "$journal" session) || return 1 - FM_BACKEND_HERDR_JOURNAL_WORKSPACE_ID=$(fm_backend_herdr_projection_journal_field "$journal" workspace_id) || return 1 - FM_BACKEND_HERDR_JOURNAL_TAB_ID=$(fm_backend_herdr_projection_journal_field "$journal" tab_id) || return 1 - FM_BACKEND_HERDR_JOURNAL_PANE_ID=$(fm_backend_herdr_projection_journal_field "$journal" pane_id) || return 1 - FM_BACKEND_HERDR_JOURNAL_PARENT_WORKSPACE_ID=$(fm_backend_herdr_projection_journal_field "$journal" parent_workspace_id) || return 1 - FM_BACKEND_HERDR_JOURNAL_PARENT_LABEL=$(fm_backend_herdr_projection_journal_field "$journal" parent_label) || return 1 - FM_BACKEND_HERDR_JOURNAL_WORKSPACE_LABEL=$(fm_backend_herdr_projection_journal_field "$journal" workspace_label) || return 1 - FM_BACKEND_HERDR_JOURNAL_TASK_LABEL=$(fm_backend_herdr_projection_journal_field "$journal" task_label) || return 1 - case "$FM_BACKEND_HERDR_JOURNAL_HOME" in - /*) ;; - *) return 1 ;; - esac - for exact in \ - "$FM_BACKEND_HERDR_JOURNAL_SESSION" \ - "$FM_BACKEND_HERDR_JOURNAL_WORKSPACE_ID" \ - "$FM_BACKEND_HERDR_JOURNAL_TAB_ID" \ - "$FM_BACKEND_HERDR_JOURNAL_PANE_ID" \ - "$FM_BACKEND_HERDR_JOURNAL_PARENT_WORKSPACE_ID"; do - case "$exact" in - ''|*[[:space:]]*) return 1 ;; - esac - done - [ -n "$FM_BACKEND_HERDR_JOURNAL_PARENT_LABEL" ] \ - && [ -n "$FM_BACKEND_HERDR_JOURNAL_WORKSPACE_LABEL" ] \ - && [ -n "$FM_BACKEND_HERDR_JOURNAL_TASK_LABEL" ] || return 1 - expected_label=$(fm_backend_herdr_projection_workspace_label "$id" "$FM_BACKEND_HERDR_JOURNAL_PROJECTION_ID") - expected_task_label="fm-$id" - [ "$FM_BACKEND_HERDR_JOURNAL_WORKSPACE_LABEL" = "$expected_label" ] \ - && [ "$FM_BACKEND_HERDR_JOURNAL_TASK_LABEL" = "$expected_task_label" ] -} - -# fm_backend_herdr_projection_journal_token: validate and read either journal -# version's non-authoritative visual correlator. -fm_backend_herdr_projection_journal_token() { # - fm_backend_herdr_projection_journal_snapshot "$1" "$2" || return 1 - printf '%s' "$FM_BACKEND_HERDR_JOURNAL_PROJECTION_ID" -} - -fm_backend_herdr_projection_home_identity() { # - local home=$1 - [ -d "$home" ] || return 1 - (cd "$home" 2>/dev/null && pwd -P) -} - -fm_backend_herdr_projection_journal_write_v2() { # - local journal=$1 id=$2 token=$3 home=$4 session=$5 workspace=$6 tab=$7 pane=$8 - local parent_workspace=$9 parent_label=${10} workspace_label=${11} task_label=${12} state tmp - state=$(dirname "$journal") - tmp=$(mktemp "$state/.${id}.herdr-presentation.bind.XXXXXX") || return 1 - chmod 0600 "$tmp" || { rm -f "$tmp"; return 1; } - if ! { - printf 'version=2\n' - printf 'task_id=%s\n' "$id" - printf 'projection_id=%s\n' "$token" - printf 'home=%s\n' "$home" - printf 'session=%s\n' "$session" - printf 'workspace_id=%s\n' "$workspace" - printf 'tab_id=%s\n' "$tab" - printf 'pane_id=%s\n' "$pane" - printf 'parent_workspace_id=%s\n' "$parent_workspace" - printf 'parent_label=%s\n' "$parent_label" - printf 'workspace_label=%s\n' "$workspace_label" - printf 'task_label=%s\n' "$task_label" - } > "$tmp"; then - rm -f "$tmp" - return 1 - fi - [ -f "$journal" ] && [ ! -L "$journal" ] || { rm -f "$tmp"; return 1; } - mv -f "$tmp" "$journal" -} - -# fm_backend_herdr_projection_journal_bind: upgrade one exact version 1 -# attempt to a version 2 binding after the live projection and parent relation -# have both been verified under the session lock. -fm_backend_herdr_projection_journal_bind() { # - local journal=$1 id=$2 home=$3 session=$4 workspace=$5 tab=$6 pane=$7 - local parent_workspace=$8 parent_label=$9 workspace_label=${10} task_label=${11} token - fm_backend_herdr_projection_journal_snapshot "$journal" "$id" || return 1 - [ "$FM_BACKEND_HERDR_JOURNAL_VERSION" = 1 ] || return 1 - token=$FM_BACKEND_HERDR_JOURNAL_PROJECTION_ID - fm_backend_herdr_projection_journal_write_v2 \ - "$journal" "$id" "$token" "$home" "$session" "$workspace" "$tab" "$pane" \ - "$parent_workspace" "$parent_label" "$workspace_label" "$task_label" -} - -# fm_backend_herdr_projection_journal_replace_endpoint: atomically advance one -# exact version 2 binding after its old husk was replaced successfully. -fm_backend_herdr_projection_journal_replace_endpoint() { # - local journal=$1 id=$2 old_tab=$3 old_pane=$4 new_tab=$5 new_pane=$6 - fm_backend_herdr_projection_journal_snapshot "$journal" "$id" || return 1 - [ "$FM_BACKEND_HERDR_JOURNAL_VERSION" = 2 ] \ - && [ "$FM_BACKEND_HERDR_JOURNAL_TAB_ID" = "$old_tab" ] \ - && [ "$FM_BACKEND_HERDR_JOURNAL_PANE_ID" = "$old_pane" ] || return 1 - fm_backend_herdr_projection_journal_write_v2 \ - "$journal" "$id" "$FM_BACKEND_HERDR_JOURNAL_PROJECTION_ID" \ - "$FM_BACKEND_HERDR_JOURNAL_HOME" "$FM_BACKEND_HERDR_JOURNAL_SESSION" \ - "$FM_BACKEND_HERDR_JOURNAL_WORKSPACE_ID" "$new_tab" "$new_pane" \ - "$FM_BACKEND_HERDR_JOURNAL_PARENT_WORKSPACE_ID" "$FM_BACKEND_HERDR_JOURNAL_PARENT_LABEL" \ - "$FM_BACKEND_HERDR_JOURNAL_WORKSPACE_LABEL" "$FM_BACKEND_HERDR_JOURNAL_TASK_LABEL" -} - -# fm_backend_herdr_projection_concise_task_label: strip redundant owner -# prefixes from a task id used only in the presentation workspace label. -# Removes firstmate/, 2ndmate-/, and a presentation-level fm- owner -# prefix when present. The ordinary task tab remains fm- and is not -# built by this helper. -fm_backend_herdr_projection_concise_task_label() { # - local task=$1 - case "$task" in - firstmate/*) task=${task#firstmate/} ;; - 2ndmate-*/*) task=${task#*/} ;; - esac - case "$task" in - fm-*) task=${task#fm-} ;; - esac - printf '%s' "$task" -} - -# fm_backend_herdr_projection_workspace_label: presentation-only child label. -# Format is literal U+2514 BOX DRAWINGS LIGHT UP AND RIGHT, one space, the -# concise task label, then the unchanged · p: suffix. -# Labels and tokens remain non-authoritative correlators only. -fm_backend_herdr_projection_workspace_label() { # - printf '└ %s · p:%s' "$(fm_backend_herdr_projection_concise_task_label "$1")" "$2" -} - -# fm_backend_herdr_presentation_session_lock_path: one machine-private lock -# path per live named Herdr session/socket, shared across every Firstmate home -# that uses that session. -# The path is never under any one home's state/ and secondmates never write the -# primary home. Returns non-zero when the named session's socket cannot be -# resolved unambiguously. -fm_backend_herdr_presentation_lock_namespace() { - printf '%s' '/tmp/firstmate-herdr-presentation' -} - -fm_backend_herdr_presentation_lock_namespace_mode() { - if [ "$(uname -s 2>/dev/null)" = Darwin ]; then - stat -f '%Lp' "$1" 2>/dev/null - else - stat -c '%a' "$1" 2>/dev/null - fi -} - -fm_backend_herdr_presentation_lock_namespace_uid() { - if [ "$(uname -s 2>/dev/null)" = Darwin ]; then - stat -f '%u' "$1" 2>/dev/null - else - stat -c '%u' "$1" 2>/dev/null - fi -} - -fm_backend_herdr_presentation_lock_namespace_valid() { - local dir=$1 expected_uid owner mode - [ -d "$dir" ] && [ ! -L "$dir" ] || return 1 - expected_uid=$(id -u 2>/dev/null) || return 1 - owner=$(fm_backend_herdr_presentation_lock_namespace_uid "$dir") || return 1 - mode=$(fm_backend_herdr_presentation_lock_namespace_mode "$dir") || return 1 - [ "$owner" = "$expected_uid" ] && [ "$mode" = 700 ] -} - -# Resolve the one verified running named-session socket path as an absolute -# string. Requires JSON string type and non-empty length (jq -r is never used: -# it would turn JSON null into the literal string "null"). Canonicalizes the -# parent directory when that directory exists so symlink parents such as /tmp -# -> /private/tmp cannot yield two lock identities for the same socket. -fm_backend_herdr_presentation_session_socket_path() { # - local session=$1 sessions socket sock_dir sock_base - [ -n "$session" ] || return 1 - sessions=$(fm_backend_herdr_cli "$session" session list --json 2>/dev/null) || return 1 - socket=$(printf '%s' "$sessions" | jq -er --arg want "$session" ' - [.sessions[]? - | select(.name == $want and .running == true) - | select((.socket_path | type) == "string") - | select((.socket_path | length) > 0) - | .socket_path] - | if length == 1 then .[0] else empty end - ' 2>/dev/null) || return 1 - [ -n "$socket" ] || return 1 - case "$socket" in - /*) ;; - *) return 1 ;; - esac - sock_dir=$(dirname "$socket") - sock_base=$(basename "$socket") - [ -n "$sock_dir" ] && [ -n "$sock_base" ] || return 1 - if [ -d "$sock_dir" ]; then - sock_dir=$(cd "$sock_dir" 2>/dev/null && pwd -P) || return 1 - socket="$sock_dir/$sock_base" - fi - printf '%s' "$socket" -} - -fm_backend_herdr_presentation_session_lock_path() { # - local session=$1 socket key dir hash - [ -n "$session" ] || return 1 - socket=$(fm_backend_herdr_presentation_session_socket_path "$session") || return 1 - if command -v shasum >/dev/null 2>&1; then - hash=$(printf '%s\0%s' "$session" "$socket" | shasum -a 256 2>/dev/null | awk '{print $1}') - elif command -v sha256sum >/dev/null 2>&1; then - hash=$(printf '%s\0%s' "$session" "$socket" | sha256sum 2>/dev/null | awk '{print $1}') - else - return 1 - fi - [ -n "$hash" ] || return 1 - key=${hash:0:32} - dir=$(fm_backend_herdr_presentation_lock_namespace) || return 1 - [ -n "$dir" ] || return 1 - if [ ! -e "$dir" ] && [ ! -L "$dir" ]; then - if ! mkdir -m 700 "$dir" 2>/dev/null; then - fm_backend_herdr_presentation_lock_namespace_valid "$dir" || return 1 - fi - fi - fm_backend_herdr_presentation_lock_namespace_valid "$dir" || return 1 - printf '%s/order-%s.lock' "$dir" "$key" -} - -# fm_backend_herdr_projection_focus_snapshot: print the exact active -# workspace and tab ids as one tab-separated record. -# Presentation mutations use this read-only snapshot as their sole focus -# restoration authority. -# Labels, workspace order, and ambient client state are never focus authority. -fm_backend_herdr_projection_focus_snapshot() { # - local session=$1 list snapshot workspace tab tabs - list=$(fm_backend_herdr_cli "$session" workspace list 2>/dev/null) || return 1 - snapshot=$(printf '%s' "$list" | jq -r ' - [.result.workspaces[]? | select(.focused == true)] - | select(length == 1) - | .[0] - | select((.workspace_id | type) == "string" and (.workspace_id | length) > 0) - | select((.active_tab_id | type) == "string" and (.active_tab_id | length) > 0) - | [.workspace_id, .active_tab_id] - | @tsv - ' 2>/dev/null) || return 1 - [ -n "$snapshot" ] || return 1 - workspace=${snapshot%%$'\t'*} - tab=${snapshot#*$'\t'} - [ -n "$workspace" ] && [ -n "$tab" ] && [ "$workspace" != "$tab" ] || return 1 - tabs=$(fm_backend_herdr_cli "$session" tab list --workspace "$workspace" 2>/dev/null) || return 1 - printf '%s' "$tabs" | jq -e --arg tab "$tab" ' - (.result.tabs | type) == "array" - and ([.result.tabs[] | select(.focused == true)] | length) == 1 - and ([.result.tabs[] | select(.focused == true)][0].tab_id == $tab) - ' >/dev/null 2>&1 || return 1 - printf '%s\t%s' "$workspace" "$tab" -} - -# fm_backend_herdr_projection_focus_restore: verify that one presentation -# mutation preserved the exact active workspace and tab captured immediately -# before it. -# Herdr 0.7.4's pane.close can focus an unrelated neighboring workspace when -# it removes a non-focused workspace's last pane. -# A single tab.focus on the exact response-independent pre-operation tab id -# restores both the workspace and tab atomically. -fm_backend_herdr_projection_focus_restore() { # - local session=$1 before=$2 operation=$3 workspace tab after info restored - [ -n "$before" ] || { - echo "warning: herdr presentation $operation had no unambiguous pre-operation focus snapshot" >&2 - return 1 - } - after=$(fm_backend_herdr_projection_focus_snapshot "$session") || after= - [ "$after" != "$before" ] || return 0 - workspace=${before%%$'\t'*} - tab=${before#*$'\t'} - info=$(fm_backend_herdr_cli "$session" tab get "$tab" 2>/dev/null) || { - echo "warning: herdr presentation $operation changed focus and the exact prior tab could not be verified for restoration" >&2 - return 1 - } - if ! printf '%s' "$info" | jq -e --arg workspace "$workspace" --arg tab "$tab" ' - .result.tab.workspace_id == $workspace and .result.tab.tab_id == $tab - ' >/dev/null 2>&1; then - echo "warning: herdr presentation $operation changed focus and the exact prior tab response was ambiguous" >&2 - return 1 - fi - fm_backend_herdr_cli "$session" tab focus "$tab" >/dev/null 2>&1 || { - echo "warning: herdr presentation $operation changed focus and exact-tab restoration failed" >&2 - return 1 - } - restored=$(fm_backend_herdr_projection_focus_snapshot "$session") || restored= - if [ "$restored" != "$before" ]; then - echo "warning: herdr presentation $operation did not restore the exact prior workspace and tab" >&2 - return 1 - fi - return 0 -} - -# fm_backend_herdr_projection_close_pane_focus_preserving: close one exact -# response-derived projection pane without leaving the captain focused -# anywhere else. -# If the target belongs to the active tab, exact tab preservation is -# impossible, so cleanup refuses instead of changing focus. -fm_backend_herdr_projection_close_pane_focus_preserving() { # [required-agent-state] - local session=$1 pane_id=$2 required_agent_state=${3:-} - local before active_tab info target_pane target_tab close_status state - FM_BACKEND_HERDR_PROJECTION_CLOSE_AGENT_STATE="" - [ -n "$pane_id" ] || return 0 - before=$(fm_backend_herdr_projection_focus_snapshot "$session") || { - echo "warning: herdr presentation cleanup could not capture exact active workspace and tab; refusing focus-unsafe pane close" >&2 - return 1 - } - active_tab=${before#*$'\t'} - info=$(fm_backend_herdr_cli "$session" pane get "$pane_id" 2>/dev/null) || { - echo "warning: herdr presentation cleanup could not verify the exact pane; refusing focus-unsafe pane close" >&2 - return 1 - } - target_pane=$(printf '%s' "$info" | jq -r '.result.pane.pane_id // empty' 2>/dev/null) - target_tab=$(printf '%s' "$info" | jq -r '.result.pane.tab_id // empty' 2>/dev/null) - if [ "$target_pane" != "$pane_id" ] || [ -z "$target_tab" ]; then - echo "warning: herdr presentation cleanup received an ambiguous exact-pane response; refusing focus-unsafe pane close" >&2 - return 1 - fi - if [ "$target_tab" = "$active_tab" ]; then - echo "warning: herdr presentation cleanup target is the captain's active tab; refusing a close that cannot preserve focus" >&2 - return 1 - fi - if [ -n "$required_agent_state" ]; then - state=$(fm_backend_herdr_pane_agent_state "$session" "$pane_id") - FM_BACKEND_HERDR_PROJECTION_CLOSE_AGENT_STATE=$state - [ "$state" = "$required_agent_state" ] || return 1 - fi - if fm_backend_herdr_cli "$session" pane close "$pane_id" >/dev/null 2>&1; then - close_status=0 - else - close_status=$? - fi - fm_backend_herdr_projection_focus_restore "$session" "$before" "pane close" || return 2 - [ "$close_status" -eq 0 ] -} - -# fm_backend_herdr_projection_order_best_effort: place the exact workspace id -# returned by THIS projected create immediately after its owning parent's -# contiguous child block and before the next parent. -# -# is the owning FM_HOME label (firstmate or 2ndmate-). -# New-format └ ... · p: children and, for compatibility only, already -# adjacent old-format firstmate/... or 2ndmate-/... projections may extend -# the block read-only; they are never renamed or moved. -# -# This is presentation-only and always returns success. -# Every unavailable, ambiguous, failed, or unverifiable ordering step prints a -# warning and leaves the safely-created worker running in Herdr's current -# order. -# It never looks up a task endpoint, adopts or reuses a workspace, retries an -# ambiguous move, or calls any close/delete/rename primitive. -# The sole move target is , captured directly from the -# current workspace-create response. -# After a successful move, every pre-existing workspace id sequence excluding -# the new id must be byte-identical to the pre-move sequence. -fm_backend_herdr_projection_order_best_effort() { # - local session=$1 created=$2 parent=$3 list analysis current desired protocol schema socket mover response move_status focus_before - local before_existing after_existing - [ -n "$parent" ] || { - echo "warning: herdr presentation ordering missing owning parent label; leaving worker in Herdr's current order" >&2 - return 0 - } - list=$(fm_backend_herdr_cli "$session" workspace list 2>/dev/null) || { - echo "warning: herdr presentation ordering could not list workspaces; leaving worker in Herdr's current order" >&2 - return 0 - } - analysis=$(printf '%s' "$list" | jq -c --arg created "$created" --arg parent "$parent" ' - def is_parent: - (.label | type) == "string" and .label == $parent; - def is_top_level_parent: - (.label | type) == "string" - and ((.label == "firstmate") or (.label | test("^2ndmate-[^/]+$"))); - def is_new_child: - (.label | type) == "string" - and (.label | test("^└ .+ · p:[A-Za-z0-9_-]{22}$")); - def is_legacy_child: - (.label | type) == "string" - and (.label | test("^(firstmate|2ndmate-[^/]+)/.+ · p:[A-Za-z0-9_-]{22}$")); - def is_legacy_child_for($owner): - is_legacy_child and (.label | startswith($owner + "/")); - def is_child_for($owner): - is_new_child or is_legacy_child_for($owner); - (.result.workspaces // null) as $spaces - | select(($spaces | type) == "array" and ($spaces | length) > 0) - | ([range(0; $spaces | length) | select($spaces[.].workspace_id == $created)]) as $matches - | select(($matches | length) == 1) - | ($matches[0]) as $current - | select($current == (($spaces | length) - 1)) - | ([range(0; $spaces | length) | select($spaces[.] | is_parent)]) as $parents - | select(($parents | length) == 1) - | ($parents[0]) as $pidx - | select($pidx < $current) - | ( - reduce range($pidx + 1; $current) as $i ( - 0; - if ($spaces[$i] | is_child_for($parent)) and (. == ($i - $pidx - 1)) - then . + 1 - else . - end - ) - ) as $block - | (reduce range($pidx + 1 + $block; $current) as $i ( - {valid: true, active_parent: null}; - if .valid == false then . - elif ($spaces[$i] | is_top_level_parent) then - .active_parent = $spaces[$i].label - elif ($spaces[$i] | is_new_child) then - if .active_parent == null then .valid = false else . end - elif ($spaces[$i] | is_legacy_child) then - .active_parent as $owner - | if $owner == null then - .valid = false - elif (($spaces[$i] | is_legacy_child_for($owner)) | not) then - .valid = false - else - . - end - else - .active_parent = null - end - )) as $remainder - | select($remainder.valid == true) - | { - current: $current, - desired: ($pidx + 1 + $block), - parent_index: $pidx, - existing: [$spaces[] | select(.workspace_id != $created) | .workspace_id] - } - ' 2>/dev/null) || analysis= - [ -n "$analysis" ] || { - echo "warning: herdr presentation ordering found an ambiguous workspace layout; leaving worker in Herdr's current order" >&2 - return 0 - } - current=$(printf '%s' "$analysis" | jq -r '.current // empty' 2>/dev/null) - desired=$(printf '%s' "$analysis" | jq -r '.desired // empty' 2>/dev/null) - case "$current:$desired" in - *[!0-9:]*) - echo "warning: herdr presentation ordering could not parse the target position; leaving worker in Herdr's current order" >&2 - return 0 - ;; - esac - [ "$current" != "$desired" ] || return 0 - - command -v python3 >/dev/null 2>&1 || { - echo "warning: herdr presentation ordering requires python3; leaving worker in Herdr's current order" >&2 - return 0 - } - protocol=$(fm_backend_herdr_cli "$session" status --json 2>/dev/null | jq -r '.client.protocol // empty' 2>/dev/null) - case "$protocol" in - ''|*[!0-9]*) - echo "warning: herdr presentation ordering could not verify the client protocol; leaving worker in Herdr's current order" >&2 - return 0 - ;; - esac - if [ "$protocol" -lt "$FM_BACKEND_HERDR_MIN_WORKSPACE_MOVE_PROTOCOL" ]; then - echo "warning: herdr presentation ordering needs protocol $FM_BACKEND_HERDR_MIN_WORKSPACE_MOVE_PROTOCOL or newer; leaving worker in Herdr's current order" >&2 - return 0 - fi - schema=$(fm_backend_herdr_cli "$session" api schema --json 2>/dev/null) || { - echo "warning: herdr presentation ordering could not read the API schema; leaving worker in Herdr's current order" >&2 - return 0 - } - if ! printf '%s' "$schema" | jq -e ' - any(.schemas.request.oneOf[]?; .properties.method.const == "workspace.move") - and .schemas.request["$defs"].WorkspaceMoveParams.required == ["workspace_id", "insert_index"] - and .schemas.request["$defs"].WorkspaceMoveParams.properties.insert_index.type == "integer" - ' >/dev/null 2>&1; then - echo "warning: herdr presentation ordering API support is unavailable or ambiguous; leaving worker in Herdr's current order" >&2 - return 0 - fi - socket=$(fm_backend_herdr_presentation_session_socket_path "$session") || { - echo "warning: herdr presentation ordering found an ambiguous named session socket; leaving worker in Herdr's current order" >&2 - return 0 - } - - mover=${FM_BACKEND_HERDR_WORKSPACE_MOVER:-$FM_BACKEND_HERDR_ROOT/bin/backends/herdr-workspace-move.py} - focus_before=$(fm_backend_herdr_projection_focus_snapshot "$session") || { - echo "warning: herdr presentation ordering could not capture exact active workspace and tab; leaving worker in Herdr's current order" >&2 - return 0 - } - if response=$("$mover" "$socket" "$created" "$desired" 2>/dev/null); then - move_status=0 - else - move_status=$? - fi - fm_backend_herdr_projection_focus_restore "$session" "$focus_before" "workspace move" || true - if [ "$move_status" -ne 0 ]; then - echo "warning: herdr presentation workspace move failed or had an ambiguous response; leaving worker running without cleanup" >&2 - return 0 - fi - if ! printf '%s' "$response" | jq -e --arg created "$created" --arg parent "$parent" --argjson desired "$desired" ' - .result.type == "workspace_list" - and (.result.workspaces | type) == "array" - and .result.workspaces[$desired].workspace_id == $created - and ([.result.workspaces[] | select(.label == $parent)] | length) == 1 - and ( - [range(0; .result.workspaces | length) as $i - | select(.result.workspaces[$i].label == $parent) - | $i][0] < $desired - ) - ' >/dev/null 2>&1; then - echo "warning: herdr presentation workspace move returned an unverifiable order; leaving worker running without cleanup" >&2 - return 0 - fi - - before_existing=$(printf '%s' "$analysis" | jq -c '.existing' 2>/dev/null) - after_existing=$(printf '%s' "$response" | jq -c --arg created "$created" '[.result.workspaces[] | select(.workspace_id != $created) | .workspace_id]' 2>/dev/null) - if [ "$after_existing" != "$before_existing" ]; then - echo "warning: herdr presentation workspace move did not preserve relative order; leaving worker running without cleanup" >&2 - fi - return 0 -} - # fm_backend_herdr_server_ensure: start the herdr server for # headless (no TUI client) if not already running, mirroring tmux's `tmux # has-session || tmux new-session -d`. Verified: a bare socket CLI call does @@ -883,8 +250,8 @@ fm_backend_herdr_workspace_find() { # # workspace - callers only invoke it once at least one other (real task) tab # exists alongside it, never right after workspace creation - and this # function independently re-checks the tab count as a second layer. -fm_backend_herdr_workspace_prune_seeded_default_tab() { # [focus-preserving] - local session=$1 wsid=$2 tab_id=$3 close_mode=${4:-direct} tabs tab_count current_label pane_id agent_out agent_status +fm_backend_herdr_workspace_prune_seeded_default_tab() { # + local session=$1 wsid=$2 tab_id=$3 tabs tab_count current_label pane_id agent_out agent_status [ -n "$tab_id" ] || return 0 tabs=$(fm_backend_herdr_cli "$session" tab list --workspace "$wsid" 2>/dev/null) || return 0 tab_count=$(printf '%s' "$tabs" | jq -r '.result.tabs? // [] | length' 2>/dev/null) @@ -896,11 +263,7 @@ fm_backend_herdr_workspace_prune_seeded_default_tab() { # /dev/null) agent_status=$(printf '%s' "$agent_out" | jq -r '.result.agent.agent_status // empty' 2>/dev/null) [ "$agent_status" = working ] && return 0 - if [ "$close_mode" = focus-preserving ]; then - fm_backend_herdr_projection_close_pane_focus_preserving "$session" "$pane_id" - else - fm_backend_herdr_cli "$session" pane close "$pane_id" >/dev/null 2>&1 || true - fi + fm_backend_herdr_cli "$session" pane close "$pane_id" >/dev/null 2>&1 || true } # fm_backend_herdr_workspace_ensure: this HOME's persistent workspace inside @@ -1065,28 +428,24 @@ fm_backend_herdr_tab_is_husk() { # esac } -# fm_backend_herdr_agent_state: recovery-grade state for the same session-start -# sweep as the tmux classifier. It reuses the husk classifier rather than -# creating a second Herdr state machine: a structurally gone pane is `missing`, -# a confirmed agent-less pane is `dead`, a registered agent is `alive`, and an -# unexpected or failed API read is `unreadable`. -fm_backend_herdr_agent_state() { # +# fm_backend_herdr_agent_alive: CONFIDENT liveness of a live harness-agent +# PROCESS under (":"), for the same +# session-start secondmate-liveness sweep fm_backend_tmux_agent_alive serves +# (bin/fm-bootstrap.sh; docs/herdr-backend.md "Agent liveness probe reuses the +# husk classifier"). Reuses fm_backend_herdr_pane_agent_state, the +# already-verified husk classifier ("Respawn idempotency" above): `dead` +# (structurally gone pane) and `no-agent` (a restored, agent-less bare shell +# - EXACTLY the shape a dead secondmate leaves behind) both collapse to +# `dead`; `live` (a real registered agent_status, including idle/blocked) +# maps to `alive`; `unknown` stays `unknown` - fail-safe toward refusal, +# exactly like the husk check itself. Callers must never treat `unknown` as a +# confirmed-dead signal. +fm_backend_herdr_agent_alive() { # local target=$1 - fm_backend_herdr_parse_target "$target" || { printf 'unreadable'; return 0; } + fm_backend_herdr_parse_target "$target" || { printf 'unknown'; return 0; } case "$(fm_backend_herdr_pane_agent_state "$FM_BACKEND_HERDR_SESSION" "$FM_BACKEND_HERDR_PANE")" in - dead) printf 'missing' ;; - no-agent) printf 'dead' ;; + dead|no-agent) printf 'dead' ;; live) printf 'alive' ;; - *) printf 'unreadable' ;; - esac -} - -# Backward-compatible three-state view for callers that only need a yes/no -# agent verdict. The detailed state contract is owned by fm_backend_agent_state. -fm_backend_herdr_agent_alive() { # - case "$(fm_backend_herdr_agent_state "$1")" in - alive) printf 'alive' ;; - dead|missing) printf 'dead' ;; *) printf 'unknown' ;; esac } @@ -1112,7 +471,7 @@ fm_backend_herdr_agent_alive() { # # Ordering is deliberate: the REPLACEMENT tab is created FIRST, and the husk # is closed only AFTER that succeeds - never the reverse. Closing a # workspace's LAST remaining tab deletes the whole workspace on real herdr -# (docs/herdr-backend.md "Default workspace lifecycle"), and a session-restore husk +# (docs/herdr-backend.md "Workspace lifecycle"), and a session-restore husk # can legitimately be that workspace's only tab (e.g. its own seeded default # tab was already pruned, long before the restart, by a prior real task tab # existing alongside it). Herdr's lack of label-uniqueness enforcement is @@ -1191,457 +550,6 @@ EOF printf '%s %s' "$tab_id" "$pane_id" } -# fm_backend_herdr_projection_create_task: create one disposable presentation -# workspace and its normal fm- task tab without looking up, adopting, or -# reusing any existing workspace. -# The caller must atomically publish the projection journal first. -# This function sets exact response-derived globals and prints nothing: -# FM_BACKEND_HERDR_PROJECTION_SESSION -# FM_BACKEND_HERDR_PROJECTION_WORKSPACE_ID -# FM_BACKEND_HERDR_PROJECTION_SEEDED_TAB_ID -# FM_BACKEND_HERDR_PROJECTION_SEEDED_PANE_ID -# FM_BACKEND_HERDR_PROJECTION_TAB_ID -# FM_BACKEND_HERDR_PROJECTION_PANE_ID -# FM_BACKEND_HERDR_PROJECTION_CLEANUP_SAFE -# CLEANUP_SAFE becomes 1 only after both creates returned complete exact IDs. -# A missing, failed, or malformed create response stays ambiguous and grants no -# cleanup authority. -fm_backend_herdr_projection_create_task() { # - local cwd=$1 workspace_label=$2 task_label=$3 session out tabs panes tab_count pane_count focus_before - FM_BACKEND_HERDR_PROJECTION_SESSION="" - FM_BACKEND_HERDR_PROJECTION_WORKSPACE_ID="" - FM_BACKEND_HERDR_PROJECTION_SEEDED_TAB_ID="" - FM_BACKEND_HERDR_PROJECTION_SEEDED_PANE_ID="" - FM_BACKEND_HERDR_PROJECTION_TAB_ID="" - FM_BACKEND_HERDR_PROJECTION_PANE_ID="" - FM_BACKEND_HERDR_PROJECTION_CLEANUP_SAFE=0 - - fm_backend_herdr_version_check || return 1 - session=$(fm_backend_herdr_session) - fm_backend_herdr_server_ensure "$session" || return 1 - focus_before=$(fm_backend_herdr_projection_focus_snapshot "$session") || { - echo "error: herdr presentation workspace create could not capture exact active workspace and tab; refusing a focus-unsafe projection" >&2 - return 1 - } - if out=$(fm_backend_herdr_cli "$session" workspace create --cwd "$cwd" --label "$workspace_label" --no-focus 2>/dev/null); then - : - else - fm_backend_herdr_projection_focus_restore "$session" "$focus_before" "workspace create" || true - echo "error: herdr presentation workspace create failed ambiguously; leaving its journal quarantined" >&2 - return 1 - fi - fm_backend_herdr_projection_focus_restore "$session" "$focus_before" "workspace create" || { - echo "error: herdr presentation workspace create did not preserve exact active focus; leaving its journal quarantined" >&2 - return 1 - } - # shellcheck disable=SC2034 # caller consumes the response-derived global - FM_BACKEND_HERDR_PROJECTION_SESSION=$session - FM_BACKEND_HERDR_PROJECTION_WORKSPACE_ID=$(printf '%s' "$out" | jq -r '.result.workspace.workspace_id // empty' 2>/dev/null) - FM_BACKEND_HERDR_PROJECTION_SEEDED_TAB_ID=$(printf '%s' "$out" | jq -r '.result.tab.tab_id // empty' 2>/dev/null) - FM_BACKEND_HERDR_PROJECTION_SEEDED_PANE_ID=$(printf '%s' "$out" | jq -r '.result.root_pane.pane_id // empty' 2>/dev/null) - if [ -z "$FM_BACKEND_HERDR_PROJECTION_WORKSPACE_ID" ] \ - || [ -z "$FM_BACKEND_HERDR_PROJECTION_SEEDED_TAB_ID" ] \ - || [ -z "$FM_BACKEND_HERDR_PROJECTION_SEEDED_PANE_ID" ]; then - echo "error: herdr presentation workspace create returned incomplete IDs; leaving its journal quarantined" >&2 - return 1 - fi - - focus_before=$(fm_backend_herdr_projection_focus_snapshot "$session") || { - echo "error: herdr presentation task-tab create could not capture exact active workspace and tab; refusing a focus-unsafe projection" >&2 - return 1 - } - if out=$(fm_backend_herdr_cli "$session" tab create \ - --workspace "$FM_BACKEND_HERDR_PROJECTION_WORKSPACE_ID" \ - --cwd "$cwd" --label "$task_label" --no-focus 2>/dev/null); then - : - else - fm_backend_herdr_projection_focus_restore "$session" "$focus_before" "task-tab create" || true - echo "error: herdr presentation task-tab create failed ambiguously; leaving its journal quarantined" >&2 - return 1 - fi - fm_backend_herdr_projection_focus_restore "$session" "$focus_before" "task-tab create" || { - echo "error: herdr presentation task-tab create did not preserve exact active focus; leaving its journal quarantined" >&2 - return 1 - } - FM_BACKEND_HERDR_PROJECTION_TAB_ID=$(printf '%s' "$out" | jq -r '.result.tab.tab_id // empty' 2>/dev/null) - FM_BACKEND_HERDR_PROJECTION_PANE_ID=$(printf '%s' "$out" | jq -r '.result.root_pane.pane_id // empty' 2>/dev/null) - if [ -z "$FM_BACKEND_HERDR_PROJECTION_TAB_ID" ] || [ -z "$FM_BACKEND_HERDR_PROJECTION_PANE_ID" ]; then - echo "error: herdr presentation task-tab create returned incomplete IDs; leaving its journal quarantined" >&2 - return 1 - fi - # shellcheck disable=SC2034 # caller consumes the same-process cleanup gate - FM_BACKEND_HERDR_PROJECTION_CLEANUP_SAFE=1 - focus_before=$(fm_backend_herdr_projection_focus_snapshot "$session") || { - echo "error: herdr presentation seeded-tab prune could not capture exact active workspace and tab; refusing a focus-unsafe prune" >&2 - return 1 - } - if ! fm_backend_herdr_workspace_prune_seeded_default_tab \ - "$session" \ - "$FM_BACKEND_HERDR_PROJECTION_WORKSPACE_ID" \ - "$FM_BACKEND_HERDR_PROJECTION_SEEDED_TAB_ID" \ - focus-preserving; then - echo "error: herdr presentation seeded-tab prune refused a focus-unsafe close; leaving its journal quarantined" >&2 - return 1 - fi - fm_backend_herdr_projection_focus_restore "$session" "$focus_before" "seeded-tab prune" || { - echo "error: herdr presentation seeded-tab prune did not preserve exact active focus; leaving its journal quarantined" >&2 - return 1 - } - - tabs=$(fm_backend_herdr_cli "$session" tab list --workspace "$FM_BACKEND_HERDR_PROJECTION_WORKSPACE_ID" 2>/dev/null) || { - echo "error: could not verify the disposable herdr presentation workspace shape" >&2 - return 1 - } - panes=$(fm_backend_herdr_cli "$session" pane list --workspace "$FM_BACKEND_HERDR_PROJECTION_WORKSPACE_ID" 2>/dev/null) || { - echo "error: could not verify the disposable herdr presentation pane shape" >&2 - return 1 - } - if ! printf '%s' "$tabs" | jq -e '(.result.tabs | type) == "array"' >/dev/null 2>&1 \ - || ! printf '%s' "$panes" | jq -e '(.result.panes | type) == "array"' >/dev/null 2>&1; then - echo "error: could not parse the disposable herdr presentation workspace shape" >&2 - return 1 - fi - tab_count=$(printf '%s' "$tabs" | jq -r '.result.tabs | length' 2>/dev/null) - pane_count=$(printf '%s' "$panes" | jq -r '.result.panes | length' 2>/dev/null) - if [ "$tab_count" != 1 ] || [ "$pane_count" != 1 ] \ - || ! printf '%s' "$tabs" | jq -e --arg task "$FM_BACKEND_HERDR_PROJECTION_TAB_ID" \ - --arg seeded "$FM_BACKEND_HERDR_PROJECTION_SEEDED_TAB_ID" \ - '.result.tabs[0].tab_id == $task and ([.result.tabs[] | select(.tab_id == $seeded)] | length) == 0' >/dev/null 2>&1 \ - || ! printf '%s' "$panes" | jq -e --arg pane "$FM_BACKEND_HERDR_PROJECTION_PANE_ID" \ - --arg tab "$FM_BACKEND_HERDR_PROJECTION_TAB_ID" \ - '.result.panes[0].pane_id == $pane and .result.panes[0].tab_id == $tab' >/dev/null 2>&1; then - echo "error: disposable herdr presentation workspace did not converge to exactly one task pane" >&2 - return 1 - fi - return 0 -} - -# fm_backend_herdr_projection_cleanup_exact: same-process abort cleanup for a -# projection whose create calls returned complete exact IDs. -# It performs no lookup and never calls workspace close. -fm_backend_herdr_projection_cleanup_exact() { # - local session=$1 task_pane=$2 seeded_pane=$3 - [ -z "$task_pane" ] || fm_backend_herdr_projection_close_pane_focus_preserving "$session" "$task_pane" || true - if [ -n "$seeded_pane" ] && [ "$seeded_pane" != "$task_pane" ]; then - fm_backend_herdr_projection_close_pane_focus_preserving "$session" "$seeded_pane" || true - fi -} - -# fm_backend_herdr_projection_parent_workspace_exact: resolve one exact parent -# workspace only when its presentation label is unique in the named session. -fm_backend_herdr_projection_parent_workspace_exact() { # - local session=$1 parent_label=$2 list - list=$(fm_backend_herdr_cli "$session" workspace list 2>/dev/null) || return 1 - printf '%s' "$list" | jq -er --arg parent_label "$parent_label" ' - (.result.workspaces // null) as $spaces - | select(($spaces | type) == "array") - | [$spaces[]? | select(.label == $parent_label)] - | if length == 1 - and (.[0].workspace_id | type) == "string" - and (.[0].workspace_id | length) > 0 - then .[0].workspace_id - else empty - end - ' 2>/dev/null -} - -# fm_backend_herdr_projection_live_binding_matches: verify one exact projected -# workspace, its single task tab/pane, its unique token label, and its current -# position inside the exact parent's contiguous child block. -# This read-only predicate grants no mutation authority by itself. -fm_backend_herdr_projection_live_binding_matches() { # - local session=$1 token=$2 workspace=$3 tab=$4 pane=$5 parent_workspace=$6 - local parent_label=$7 workspace_label=$8 task_label=$9 list tabs panes - list=$(fm_backend_herdr_cli "$session" workspace list 2>/dev/null) || return 1 - printf '%s' "$list" | jq -e \ - --arg token "$token" \ - --arg workspace "$workspace" \ - --arg parent_workspace "$parent_workspace" \ - --arg parent_label "$parent_label" \ - --arg workspace_label "$workspace_label" ' - def is_new_child: - (.label | type) == "string" - and (.label | test("^└ .+ · p:[A-Za-z0-9_-]{22}$")); - def is_legacy_child_for($owner): - (.label | type) == "string" - and (.label | test("^(firstmate|2ndmate-[^/]+)/.+ · p:[A-Za-z0-9_-]{22}$")) - and (.label | startswith($owner + "/")); - (.result.workspaces // null) as $spaces - | select(($spaces | type) == "array") - | select(([$spaces[]? | select(.workspace_id == $workspace)] | length) == 1) - | select(([$spaces[]? | select(.workspace_id == $workspace and .label == $workspace_label)] | length) == 1) - | select(([$spaces[]? | select((.label | type) == "string" and (.label | endswith(" · p:" + $token)))] | length) == 1) - | select(([$spaces[]? | select((.label | type) == "string" and (.label | endswith(" · p:" + $token)) and .workspace_id == $workspace)] | length) == 1) - | select(([$spaces[]? | select(.workspace_id == $parent_workspace and .label == $parent_label)] | length) == 1) - | select(([$spaces[]? | select(.label == $parent_label)] | length) == 1) - | ([range(0; $spaces | length) | select($spaces[.].workspace_id == $parent_workspace)]) as $parents - | ([range(0; $spaces | length) | select($spaces[.].workspace_id == $workspace)]) as $children - | select(($parents | length) == 1 and ($children | length) == 1) - | ($parents[0]) as $parent_index - | ($children[0]) as $child_index - | select($child_index > $parent_index) - | reduce range($parent_index + 1; $child_index) as $i - (true; . and (($spaces[$i] | is_new_child) or ($spaces[$i] | is_legacy_child_for($parent_label)))) - | select(. == true) - ' >/dev/null 2>&1 || return 1 - tabs=$(fm_backend_herdr_cli "$session" tab list --workspace "$workspace" 2>/dev/null) || return 1 - printf '%s' "$tabs" | jq -e --arg tab "$tab" --arg task_label "$task_label" ' - (.result.tabs | type) == "array" - and (.result.tabs | length) == 1 - and .result.tabs[0].tab_id == $tab - and .result.tabs[0].label == $task_label - ' >/dev/null 2>&1 || return 1 - panes=$(fm_backend_herdr_cli "$session" pane list --workspace "$workspace" 2>/dev/null) || return 1 - printf '%s' "$panes" | jq -e --arg tab "$tab" --arg pane "$pane" ' - (.result.panes | type) == "array" - and (.result.panes | length) == 1 - and .result.panes[0].pane_id == $pane - and .result.panes[0].tab_id == $tab - ' >/dev/null 2>&1 -} - -fm_backend_herdr_projection_reclaim_rollback() { # - local session=$1 new_pane=$2 state - state=$(fm_backend_herdr_pane_agent_state "$session" "$new_pane") - case "$state" in - dead) return 0 ;; - no-agent) ;; - live|unknown) return 1 ;; - esac - fm_backend_herdr_projection_close_pane_focus_preserving "$session" "$new_pane" no-agent || return 1 - [ "$(fm_backend_herdr_pane_agent_state "$session" "$new_pane")" = dead ] -} - -# fm_backend_herdr_projection_reclaim_task: replace one exact agent-free -# restored projection husk inside its original workspace. -# The caller holds the session presentation lock and has already established -# that flat fallback is safe across every token match. -# Return 0 means exact reclaim, 2 means non-mutating or exactly rolled-back -# refusal with flat fallback permitted, and 1 means a live/unknown or -# post-mutation uncertainty that must refuse the launch. -fm_backend_herdr_projection_reclaim_task() { # - local session=$1 journal=$2 id=$3 home=$4 meta_workspace=$5 meta_tab=$6 meta_pane=$7 - local parent_label=$8 task_label=$9 cwd=${10} canonical_home state focus_before active_tab out new_tab new_pane info close_status - FM_BACKEND_HERDR_PROJECTION_TAB_ID="" - FM_BACKEND_HERDR_PROJECTION_PANE_ID="" - fm_backend_herdr_projection_journal_snapshot "$journal" "$id" || return 1 - if [ "$FM_BACKEND_HERDR_JOURNAL_VERSION" != 2 ]; then - echo "warning: herdr presentation journal for $id has no exact restart binding; spawning flat" >&2 - return 2 - fi - canonical_home=$(fm_backend_herdr_projection_home_identity "$home") || { - echo "warning: herdr presentation home for $id could not be resolved exactly; spawning flat" >&2 - return 2 - } - if [ "$FM_BACKEND_HERDR_JOURNAL_HOME" != "$canonical_home" ] \ - || [ "$FM_BACKEND_HERDR_JOURNAL_SESSION" != "$session" ] \ - || [ "$FM_BACKEND_HERDR_JOURNAL_WORKSPACE_ID" != "$meta_workspace" ] \ - || [ "$FM_BACKEND_HERDR_JOURNAL_TAB_ID" != "$meta_tab" ] \ - || [ "$FM_BACKEND_HERDR_JOURNAL_PANE_ID" != "$meta_pane" ] \ - || [ "$FM_BACKEND_HERDR_JOURNAL_PARENT_LABEL" != "$parent_label" ] \ - || [ "$FM_BACKEND_HERDR_JOURNAL_TASK_LABEL" != "$task_label" ]; then - echo "warning: herdr presentation binding for $id does not match its exact home, endpoint, or parent; spawning flat" >&2 - return 2 - fi - if ! fm_backend_herdr_projection_live_binding_matches \ - "$session" "$FM_BACKEND_HERDR_JOURNAL_PROJECTION_ID" \ - "$meta_workspace" "$meta_tab" "$meta_pane" \ - "$FM_BACKEND_HERDR_JOURNAL_PARENT_WORKSPACE_ID" "$parent_label" \ - "$FM_BACKEND_HERDR_JOURNAL_WORKSPACE_LABEL" "$task_label"; then - echo "warning: herdr presentation binding for $id has an ambiguous, renamed, foreign, or non-nested live shape; spawning flat" >&2 - return 2 - fi - state=$(fm_backend_herdr_pane_agent_state "$session" "$meta_pane") - case "$state" in - no-agent) ;; - dead) - echo "warning: exact herdr presentation pane for $id is gone; spawning flat" >&2 - return 2 - ;; - live|unknown) - echo "error: exact herdr presentation pane for $id is $state; refusing duplicate launch" >&2 - return 1 - ;; - esac - focus_before=$(fm_backend_herdr_projection_focus_snapshot "$session") || { - echo "warning: herdr presentation reclaim for $id could not capture exact focus; spawning flat" >&2 - return 2 - } - active_tab=${focus_before#*$'\t'} - if [ "$active_tab" = "$meta_tab" ]; then - echo "warning: herdr presentation reclaim for $id would replace the active tab; spawning flat" >&2 - return 2 - fi - if ! out=$(fm_backend_herdr_cli "$session" tab create \ - --workspace "$meta_workspace" --cwd "$cwd" --label "$task_label" --no-focus 2>/dev/null); then - fm_backend_herdr_projection_focus_restore "$session" "$focus_before" "husk replacement create" || return 1 - echo "warning: herdr presentation reclaim for $id could not create an exact replacement; spawning flat" >&2 - return 2 - fi - new_tab=$(printf '%s' "$out" | jq -r '.result.tab.tab_id // empty' 2>/dev/null) - new_pane=$(printf '%s' "$out" | jq -r '.result.root_pane.pane_id // empty' 2>/dev/null) - if [ -z "$new_tab" ] || [ -z "$new_pane" ]; then - fm_backend_herdr_projection_focus_restore "$session" "$focus_before" "husk replacement create" || return 1 - echo "warning: herdr presentation reclaim for $id returned ambiguous replacement ids; spawning flat" >&2 - return 2 - fi - fm_backend_herdr_projection_focus_restore "$session" "$focus_before" "husk replacement create" || return 1 - info=$(fm_backend_herdr_cli "$session" tab get "$new_tab" 2>/dev/null) || info= - if ! printf '%s' "$info" | jq -e --arg tab "$new_tab" --arg workspace "$meta_workspace" ' - .result.tab.tab_id == $tab and .result.tab.workspace_id == $workspace - ' >/dev/null 2>&1; then - fm_backend_herdr_projection_reclaim_rollback "$session" "$new_pane" || return 1 - echo "warning: herdr presentation reclaim for $id could not verify its replacement tab; spawning flat" >&2 - return 2 - fi - info=$(fm_backend_herdr_cli "$session" pane get "$new_pane" 2>/dev/null) || info= - if ! printf '%s' "$info" | jq -e --arg pane "$new_pane" --arg tab "$new_tab" --arg workspace "$meta_workspace" ' - .result.pane.pane_id == $pane - and .result.pane.tab_id == $tab - and .result.pane.workspace_id == $workspace - ' >/dev/null 2>&1; then - fm_backend_herdr_projection_reclaim_rollback "$session" "$new_pane" || return 1 - echo "warning: herdr presentation reclaim for $id could not verify its replacement pane; spawning flat" >&2 - return 2 - fi - state=$(fm_backend_herdr_pane_agent_state "$session" "$meta_pane") - case "$state" in - no-agent) ;; - live|unknown) - fm_backend_herdr_projection_reclaim_rollback "$session" "$new_pane" || return 1 - echo "error: herdr presentation pane for $id became $state during reclaim; refusing duplicate launch" >&2 - return 1 - ;; - dead) - fm_backend_herdr_projection_reclaim_rollback "$session" "$new_pane" || return 1 - echo "warning: herdr presentation pane for $id disappeared during reclaim; spawning flat" >&2 - return 2 - ;; - esac - if fm_backend_herdr_projection_close_pane_focus_preserving "$session" "$meta_pane" no-agent; then - close_status=0 - else - close_status=$? - fi - if [ "$close_status" -ne 0 ]; then - if [ "$close_status" -eq 2 ]; then - return 1 - fi - state=$FM_BACKEND_HERDR_PROJECTION_CLOSE_AGENT_STATE - fm_backend_herdr_projection_reclaim_rollback "$session" "$new_pane" || return 1 - case "$state" in - live|unknown) - echo "error: herdr presentation pane for $id became $state at the close boundary; refusing duplicate launch" >&2 - return 1 - ;; - esac - echo "warning: herdr presentation reclaim for $id could not close the exact old husk; spawning flat" >&2 - return 2 - fi - if [ "$(fm_backend_herdr_pane_agent_state "$session" "$meta_pane")" != dead ]; then - fm_backend_herdr_projection_reclaim_rollback "$session" "$new_pane" || return 1 - return 1 - fi - if ! fm_backend_herdr_projection_live_binding_matches \ - "$session" "$FM_BACKEND_HERDR_JOURNAL_PROJECTION_ID" \ - "$meta_workspace" "$new_tab" "$new_pane" \ - "$FM_BACKEND_HERDR_JOURNAL_PARENT_WORKSPACE_ID" "$parent_label" \ - "$FM_BACKEND_HERDR_JOURNAL_WORKSPACE_LABEL" "$task_label"; then - fm_backend_herdr_projection_reclaim_rollback "$session" "$new_pane" || return 1 - echo "warning: herdr presentation reclaim for $id did not converge exactly; spawning flat" >&2 - return 2 - fi - if ! fm_backend_herdr_projection_journal_replace_endpoint \ - "$journal" "$id" "$meta_tab" "$meta_pane" "$new_tab" "$new_pane"; then - fm_backend_herdr_projection_reclaim_rollback "$session" "$new_pane" || return 1 - echo "warning: herdr presentation reclaim for $id could not publish its replacement binding; spawning flat" >&2 - return 2 - fi - FM_BACKEND_HERDR_PROJECTION_TAB_ID=$new_tab - FM_BACKEND_HERDR_PROJECTION_PANE_ID=$new_pane - return 0 -} - -# fm_backend_herdr_projection_recovery_allows_flat: inspect an existing -# journal's exact token matches without adopting, reusing, renaming, closing, -# or deleting anything. -# Missing matches safely degrade to the normal flat workspace. -# One or more matches allow flat fallback only when every pane is positively -# dead or agent-free; a live or unknown pane refuses a duplicate launch. -fm_backend_herdr_projection_recovery_allows_flat() { # - local session=$1 journal=$2 id=$3 token list wsids count wsid panes pane_ids pane state - token=$(fm_backend_herdr_projection_journal_token "$journal" "$id") || { - echo "error: malformed herdr presentation journal for $id; refusing duplicate launch" >&2 - return 1 - } - fm_backend_herdr_server_ensure "$session" || { - echo "error: could not inspect the quarantined herdr presentation for $id; refusing duplicate launch" >&2 - return 1 - } - list=$(fm_backend_herdr_cli "$session" workspace list 2>/dev/null) || { - echo "error: could not list herdr workspaces while inspecting the quarantined presentation for $id" >&2 - return 1 - } - if ! printf '%s' "$list" | jq -e '(.result.workspaces | type) == "array"' >/dev/null 2>&1; then - echo "error: could not parse herdr workspaces while inspecting the quarantined presentation for $id" >&2 - return 1 - fi - wsids=$(printf '%s' "$list" | jq -r --arg suffix " · p:$token" \ - '.result.workspaces[]? | select((.label | type) == "string" and (.label | endswith($suffix))) | .workspace_id' 2>/dev/null) - count=$(printf '%s\n' "$wsids" | awk 'NF { n += 1 } END { print n + 0 }') - if [ "$count" -eq 0 ]; then - echo "warning: no exact herdr presentation token match for $id; leaving any stale space untouched and spawning flat" >&2 - return 0 - fi - if [ "$count" -gt 1 ]; then - echo "warning: $count exact herdr presentation token matches for $id are quarantined; inspecting only for duplicate-agent risk" >&2 - fi - while IFS= read -r wsid; do - [ -n "$wsid" ] || continue - panes=$(fm_backend_herdr_cli "$session" pane list --workspace "$wsid" 2>/dev/null) || { - echo "error: could not inspect herdr presentation workspace $wsid for $id; refusing duplicate launch" >&2 - return 1 - } - if ! printf '%s' "$panes" | jq -e '(.result.panes | type) == "array"' >/dev/null 2>&1; then - echo "error: could not parse herdr presentation workspace $wsid for $id; refusing duplicate launch" >&2 - return 1 - fi - pane_ids=$(printf '%s' "$panes" | jq -r '.result.panes[]? | .pane_id' 2>/dev/null) - while IFS= read -r pane; do - [ -n "$pane" ] || continue - state=$(fm_backend_herdr_pane_agent_state "$session" "$pane") - case "$state" in - dead|no-agent) : ;; - live|unknown) - echo "error: quarantined herdr presentation for $id has a $state pane; refusing duplicate launch" >&2 - return 1 - ;; - esac - done <&2 - return 0 -} - -# fm_backend_herdr_projection_endpoint_matches_journal: read-only correlation -# for retiring a successful projection journal after normal exact-pane -# teardown. -# Exactly one token-bearing workspace must match the endpoint workspace. -# This verdict never authorizes a Herdr mutation. -fm_backend_herdr_projection_endpoint_matches_journal() { # - local session=$1 workspace_id=$2 journal=$3 id=$4 token list matches - token=$(fm_backend_herdr_projection_journal_token "$journal" "$id") || return 1 - list=$(fm_backend_herdr_cli "$session" workspace list 2>/dev/null) || return 1 - printf '%s' "$list" | jq -e '(.result.workspaces | type) == "array"' >/dev/null 2>&1 || return 1 - matches=$(printf '%s' "$list" | jq -r --arg suffix " · p:$token" \ - '.result.workspaces[]? | select((.label | type) == "string" and (.label | endswith($suffix))) | .workspace_id' 2>/dev/null) - [ "$matches" = "$workspace_id" ] -} - # fm_backend_herdr_parse_target: split ":" (pane_id itself # contains a colon, e.g. "w1:p2") on the FIRST colon only. Sets # FM_BACKEND_HERDR_SESSION and FM_BACKEND_HERDR_PANE for the caller. @@ -2057,10 +965,10 @@ EOF # re-invokes this function from scratch with the same text after seeing # an error, which is a human/escalation decision, not an automatic # retry). -# Echoes empty|pending|unknown|send-failed, a subset of the proof-carrying -# submit vocabulary. Empty means confirmed submitted for every backend; how -# each backend confirms it is an internal decision, and herdr's is no longer -# literally "the composer read empty". +# Echoes empty|pending|unknown|send-failed, the SAME vocabulary fm-send.sh +# already branches on for tmux ("empty" means "confirmed submitted" for every +# backend; how each backend confirms it is an internal decision - herdr's is +# no longer literally "the composer read empty"). fm_backend_herdr_send_text_submit() { # local target=$1 text=$2 retries=$3 sleep_s=$4 settle=$5 i=0 verdict baseline confirm_sleep fm_backend_herdr_parse_target "$target" || { printf 'unknown'; return 0; } diff --git a/bin/backends/tmux.sh b/bin/backends/tmux.sh index b618e055bc..dee5813d64 100644 --- a/bin/backends/tmux.sh +++ b/bin/backends/tmux.sh @@ -136,64 +136,32 @@ fm_backend_tmux_current_command() { # tmux display-message -p -t "$1" '#{pane_current_command}' 2>/dev/null } -# fm_backend_tmux_agent_state: recovery-grade harness-agent state for one -# recorded target. See bin/fm-backend.sh's fm_backend_agent_state for the -# shared state vocabulary and docs/tmux-backend.md "Agent liveness probe" for -# the empirical basis. Tmux silently falls back to the active window when a -# named target is absent, so the exact recorded window must appear in a -# successful session inventory before its foreground command can be trusted. -# An omitted window or a definitive missing-session/server response is -# `missing`; any other inventory or pane read failure is `unreadable`, so a -# transient tmux problem never licenses a duplicate. -fm_backend_tmux_agent_state() { # - local target=$1 comm session window windows inventory_status - case "$target" in - *:*:*|'':*|*:'') printf 'unreadable'; return 0 ;; - *:*) ;; - *) printf 'unreadable'; return 0 ;; - esac - session=${target%%:*} - window=${target#*:} - if windows=$(LC_ALL=C tmux list-windows -t "$session" -F '#{window_name}' 2>&1); then - inventory_status=0 - else - inventory_status=$? - fi - if [ "$inventory_status" -ne 0 ]; then - case "$windows" in - *"can't find session:"*|*"no server running on "*|*"error connecting to "*" (No such file or directory)"|*"error connecting to "*" (Connection refused)") - printf 'missing' - ;; - *) - printf 'unreadable' - ;; - esac - return 0 - fi - if ! printf '%s\n' "$windows" | grep -Fqx "$window"; then - printf 'missing' - return 0 - fi - - comm=$(fm_backend_tmux_current_command "$target") || { - printf 'unreadable' - return 0 - } +# fm_backend_tmux_agent_alive: CONFIDENT liveness of a live harness-agent +# PROCESS in 's pane, distinct from fm_backend_target_exists's +# pane-PRESENCE-only check (a pane that still exists but is sitting at a bare +# idle shell passes THAT check as "alive" - the secondmate-liveness gap +# AGENTS.md's session-start guarantee closes). See docs/tmux-backend.md +# "Agent liveness probe" for the empirical basis. Prints one of: +# alive - the foreground command is one of the verified harness binaries +# (claude, codex, opencode, grok - each confirmed to run as its +# own process name, never wrapped by a generic interpreter). +# dead - the foreground command is a bare shell: nothing is running in +# the pane, so a prior agent process has exited. +# unknown - anything else, INCLUDING a bare "node"/"python" interpreter +# name (pi's own launcher execs into a generic "node" process +# with no reliable way to attribute it back to pi from outside +# the pane - docs/tmux-backend.md "Known gaps"), or an unreadable +# pane. Callers must never treat unknown as a confirmed-dead +# signal (bin/fm-bootstrap.sh's secondmate-liveness sweep gates a +# respawn on `dead` only). +fm_backend_tmux_agent_alive() { # + local target=$1 comm + comm=$(fm_backend_tmux_current_command "$target") || { printf 'unknown'; return 0; } comm=${comm#-} case "$comm" in - *claude*|*codex*|*opencode*|*grok*|*kimi*) printf 'alive' ;; + '') printf 'unknown' ;; + *claude*|*codex*|*opencode*|*grok*) printf 'alive' ;; zsh|bash|sh|dash|ash|ksh|mksh|tcsh|csh|fish) printf 'dead' ;; - '') printf 'unreadable' ;; - *) printf 'ambiguous' ;; - esac -} - -# Backward-compatible three-state view for callers that only need a yes/no -# agent verdict. The detailed state contract is owned by fm_backend_agent_state. -fm_backend_tmux_agent_alive() { # - case "$(fm_backend_tmux_agent_state "$1")" in - alive) printf 'alive' ;; - dead|missing) printf 'dead' ;; *) printf 'unknown' ;; esac } diff --git a/bin/backends/zellij.sh b/bin/backends/zellij.sh index 20d53a3c2d..162ea5477b 100644 --- a/bin/backends/zellij.sh +++ b/bin/backends/zellij.sh @@ -501,8 +501,8 @@ fm_backend_zellij_capture() { # [expected-label] # also the load-bearing defense against the # unconditional-exit-0 CLI quirk documented in the file header: a truly dead # target never shows a change, so it correctly reports pending/unknown rather -# than a false "sent". Echoes empty|pending|unknown|send-failed, a subset of the -# proof-carrying submit vocabulary. +# than a false "sent". Echoes empty|pending|unknown|send-failed, the SAME +# vocabulary fm-send.sh already branches on for tmux and herdr. fm_backend_zellij_send_text_submit() { # [expected-label] local target=$1 text=$2 retries=$3 sleep_s=$4 settle=$5 expected_label=${6:-} typed after i=0 fm_backend_zellij_send_literal "$target" "$text" "$expected_label" || { printf 'send-failed'; return 0; } diff --git a/bin/fm-afk-launch.sh b/bin/fm-afk-launch.sh index 57b7f6590d..a6ddcbcc6c 100755 --- a/bin/fm-afk-launch.sh +++ b/bin/fm-afk-launch.sh @@ -77,10 +77,9 @@ fm_afk_launch_lock_owned() { } fm_afk_launch_lock_acquire() { - local attempt=0 incomplete=0 identity + local i incomplete=0 identity mkdir -p "$FM_AFK_LAUNCH_STATE" || return 1 - while [ "$attempt" -lt 200 ]; do - attempt=$((attempt + 1)) + for i in $(seq 1 200); do if mkdir "$FM_AFK_LAUNCH_LOCK" 2>/dev/null; then if ! printf '%s' "$$" > "$FM_AFK_LAUNCH_LOCK/pid"; then rm -rf "$FM_AFK_LAUNCH_LOCK" @@ -258,13 +257,12 @@ fm_afk_launch_terminal_alive() { # } fm_afk_launch_wait_ready() { # - local backend=$1 target=$2 attempt=0 + local backend=$1 target=$2 i if [ -n "${FM_AFK_LAUNCH_ENTRY:-}" ]; then fm_afk_launch_terminal_alive "$backend" "$target" return fi - while [ "$attempt" -lt 100 ]; do - attempt=$((attempt + 1)) + for i in $(seq 1 100); do daemon_lock_held_by_live_daemon && return 0 fm_afk_launch_terminal_alive "$backend" "$target" || return 1 sleep 0.05 @@ -289,9 +287,8 @@ fm_afk_launch_commit_terminal() { # [already-recorde } fm_afk_launch_herdr_recover_created() { #