diff --git a/.agents/skills/harness-adapters/references/harness/claude.md b/.agents/skills/harness-adapters/references/harness/claude.md index e72d0958c54..fa64df4ed9d 100644 --- a/.agents/skills/harness-adapters/references/harness/claude.md +++ b/.agents/skills/harness-adapters/references/harness/claude.md @@ -25,7 +25,7 @@ Every Claude spawn therefore pre-registers its launch directory before launch, a A second, separate dialog - "Allow external CLAUDE.md file imports?" - renders whenever a loaded CLAUDE.md chain reaches outside the project tree, which every crewmate's does through the captain's own `~/.claude/CLAUDE.md` importing `~/.claude/RTK.md`. `--setting-sources project,local`, which only `../../../bin/fm-live-lab.sh` passes (an `fm-spawn.sh` worker loads the user settings too), does not suppress it either, and it gates the session exactly like the trust dialog: cursor on "No, disable external imports", no way to move the selection from firstmate's steering plane. -`../../../bin/fm-claude-trust.sh` records `hasTrustDialogAccepted` for both the worktree and its primary checkout in `${CLAUDE_CONFIG_DIR:-$HOME}/.claude.json`, where a home's worker account pin decides `CLAUDE_CONFIG_DIR` (`../../../docs/configuration.md` "Worker account pin"), for a ship or scout spawn; a secondmate spawn registers only its own home entry, since a secondmate home has no separate primary-checkout entry to carry import consent forward from. +`../../../bin/fm-claude-trust.sh` records `hasTrustDialogAccepted` for both the worktree and its primary checkout in `${CLAUDE_CONFIG_DIR:-$HOME}/.claude.json`, where a home's worker account pin or the declared account a spawn names decides `CLAUDE_CONFIG_DIR` (`../../../docs/configuration.md` "Worker account pin"), for a ship or scout spawn; a secondmate spawn registers only its own home entry, since a secondmate home has no separate primary-checkout entry to carry import consent forward from. For a ship or scout spawn, the external-imports flags (`hasClaudeMdExternalIncludesApproved`, `hasClaudeMdExternalIncludesWarningShown`) are carried forward alongside the trust flag only when the primary checkout's project entry already carries an explicit `hasClaudeMdExternalIncludesApproved===true` from a prior interactive session - the common first-spawn case is a project claude has never been asked about, so those two flags are left unwritten and the import dialog still renders, even though trust registers normally. When the project entry instead already carries an explicit decline (`hasClaudeMdExternalIncludesApproved===false` with `hasClaudeMdExternalIncludesWarningShown===true`), the whole registration refuses - including the trust flag - rather than manufacture consent the human never gave, so that spawn wedges on the trust dialog before it would even reach the import one. Both flags `false` is Claude Code's default entry for a project never asked, not a decline, and is treated like an absent flag: trust registers and the import dialog still renders. diff --git a/.agents/skills/harness-adapters/references/harness/codex.md b/.agents/skills/harness-adapters/references/harness/codex.md index 7dd042385a2..b162ad03c00 100644 --- a/.agents/skills/harness-adapters/references/harness/codex.md +++ b/.agents/skills/harness-adapters/references/harness/codex.md @@ -44,13 +44,9 @@ This is why `$no-mistakes` reaches a Codex worker instead of being consumed by t ## Primary integration -The primary integration was verified on 2026-07-08 with codex-cli 0.142.1. -The firstmate primary's `.codex/hooks.json` registers a Stop hook that pipes Codex's payload to `../../../bin/fm-turnend-guard.sh`, through `../../../bin/fm-codex-stop-park.sh --guard` when that script is present. -Codex Stop hooks preserve exit status 2 and stderr to block, and expose `stop_hook_active` for the same one-block loop safety used by the guard's default mode. +[`turnend-guard.md`](../../../../../docs/turnend-guard.md#harness-integrations) owns the primary Stop wrapper's scoping, response streams, and bounded continuation; [runtime backend verification](../../../../../docs/verification/runtime-backends.md#stop-response-parsing) owns the current parser evidence. -The Stop payload includes `cwd`, but the tracked hook does not use it to choose the guard executable. -Codex runs the Stop command with process PWD set to the hook-loaded project root, while no `CODEX_PROJECT_DIR`, `CODEX_WORKSPACE_ROOT`, or `CODEX_CWD` root variable is set. -The tracked hook anchors to `pwd -P`, verifies that root is Firstmate-shaped and hook-bearing, and then invokes the guard with the original payload. +Verified on 2026-07-08 with codex-cli 0.142.1: Codex runs the Stop command with process PWD set to the hook-loaded project root, while no `CODEX_PROJECT_DIR`, `CODEX_WORKSPACE_ROOT`, or `CODEX_CWD` root variable is set. Codex CLI 0.163.0 supports the native asynchronous Stop park registered by `bin/fm-codex-stop-park.sh` in a home that opts in with [`config/codex-stop-park`](../../../../../docs/configuration.md#codex-native-stop-park-configcodex-stop-park), verified in an isolated real CLI lab on 2026-10-19. The hook waits outside the model and delivers actionable wakes with `codex queue --thread`, including in embedded mode without a shared app-server daemon. diff --git a/.agents/skills/operational-home-layout/SKILL.md b/.agents/skills/operational-home-layout/SKILL.md index 237d89a1362..e831e75be87 100644 --- a/.agents/skills/operational-home-layout/SKILL.md +++ b/.agents/skills/operational-home-layout/SKILL.md @@ -23,7 +23,8 @@ bin/ helper scripts, committed; read each script's header before config/crew-harness crewmate harness override; LOCAL, gitignored; absent or "default" = same as firstmate. Inherited as the literal file: a concrete primary adapter value also controls a secondmate home's own crewmates (section 4) config/claude-permission-mode optional worker permission posture; LOCAL, gitignored; schema and failover: docs/configuration.md "Claude permission mode" and "Persistent managed-policy failover" config/codex-approval-mode optional worker approval posture; LOCAL, gitignored; schema and failover: docs/configuration.md "Codex automatic approval review" and "Persistent managed-policy failover" -config/claude-account config/pi-account optional per-home worker account pin for Claude and Pi launches; LOCAL, gitignored, not inherited; absent keeps today's ambient account; present refuses a launch unless the pinned account resolves and is signed in (section 4 owns the refusal rule); see docs/configuration.md "Worker account pin" +config/claude-account config/pi-account optional per-home worker account pin for Claude and Pi launches; LOCAL, gitignored, not inherited; docs/configuration.md "Worker account pin" owns defaults and launch refusals +config/claude-accounts optional list of Claude accounts a home's ships and scouts may launch on, one per line; LOCAL, gitignored, not inherited; absent changes nothing; present needs --account on every Claude ship or scout spawn and excludes config/claude-account; see docs/configuration.md "Several Claude accounts" config/secondmate-accounts//claude-account config/secondmate-accounts//pi-account optional PRIMARY-home account pin for one local secondmate's own agent, in the worker account pin format; LOCAL, gitignored, not inherited; absent keeps the home-wide pin; see docs/configuration.md "Per-secondmate account pins" config/secondmate-accounts//omp-profile optional PRIMARY-home omp profile name for one local omp secondmate's own agent (`omp --profile `); LOCAL, gitignored, not inherited; absent adds no flag; see docs/configuration.md "Per-secondmate omp profile" config/crew-dispatch.json optional crewmate dispatch profiles; LOCAL, gitignored; firstmate-maintained but human-editable natural-language rules that choose a per-task harness/model/effort profile (section 4). Inherited by secondmate homes @@ -49,6 +50,7 @@ config/rex-tint optional "off" (default when absent), "subtle", or "subtle-ligh config/trace-context optional presence flag enabling default-off native W3C trace-context propagation to spawned agents; LOCAL, gitignored; inherited by secondmate homes; see docs/configuration.md "Trace context propagation" and docs/trace-context.md config/lavish-axi-host optional one-line per-machine Lavish server address; LOCAL, gitignored, inherited by secondmate homes, and exported into every worker launch; see docs/configuration.md "Lavish server address" for opening versus polling config/brief-include.md optional standing worker instructions appended verbatim as the last section of every ship and scout scaffold; LOCAL, gitignored, and not inherited; keep its text out of `## Firstmate spec`; see docs/configuration.md "Home brief include" +config/branch-prompt-include.md optional standing home rules appended verbatim as the last section of the supervision branch prompt; LOCAL, gitignored, and not inherited; see docs/configuration.md "Home branch prompt include" config/herdr-lifecycle-spawn-guard optional presence flag opting this home into the default-off spawn refusal of a ship or scout brief that names a Herdr lifecycle command without the Herdr isolation contract; LOCAL, gitignored, and not inherited; see docs/configuration.md "Herdr lifecycle spawn guard" config/fleet-ledger optional presence flag opting this home in to the default-off fleet activity ledger state/fleet-ledger.jsonl that outside tools can follow; LOCAL, gitignored, and not inherited; see docs/fleet-ledger.md config/firstmate-role optional LOCAL, gitignored firstmate-repository publication role; see docs/configuration.md "Firstmate role" for operator opt-in and secondmate inheritance diff --git a/.agents/skills/quota-array-dispatch/SKILL.md b/.agents/skills/quota-array-dispatch/SKILL.md index a8b18c89c91..9d8ba88c830 100644 --- a/.agents/skills/quota-array-dispatch/SKILL.md +++ b/.agents/skills/quota-array-dispatch/SKILL.md @@ -1,7 +1,7 @@ --- name: quota-array-dispatch description: >- - Load when a crew-dispatch rule or default resolves to more than one profile candidate. + Load when a crew-dispatch rule or default resolves to more than one profile candidate, or a Claude ship or scout is dispatched from a home that declares config/claude-accounts. user-invocable: false metadata: internal: true @@ -41,6 +41,18 @@ Firstmate can optionally arm `bin/fm-procevent-quota.sh` for a recurring mid-tas The opt-in [typed resolver](../../../docs/configuration.md#typed-dispatch-resolution-env-fm_dispatch_provider-fm_dispatch_model) has its own documented gates. It never removes this skill's authority, and its `ambiguous`, `escalate`, and `error` outcomes return here. +## Claude account list + +When the home declares `config/claude-accounts`, each Claude candidate is one candidate per declared account, and the spawn needs the chosen account as `--account `. +The [shared-quota opt-out](#shared-quota-opt-out) also disables this section's measurement and quota ranking; still name a declared usable account explicitly. +Otherwise measure them with `bin/fm-claude-account-quota.sh [--model ]` instead of the default TOON's single `claude` row, which reads only the ambient account; its header owns the per-account read and output. +It prints data only, never a choice: each account's applicable scopes and any uncertainty it could not measure. +Apply the three gates below to each account's rows, taking a projected runway's time from the row's own `usableRunwaySeconds`, rank the eligible ones by their limiting scope's `spendPriority` exactly as for profiles, and pass the chosen entry as `--account `. +A genuine tie or no rankable account is the same terminal case as below: decide in the open from the rows or ask the captain, never by file order. +Account for every declared account in the rationale, including unmeasured ones. +A typed-resolver `clear` profile for Claude still needs this step's `--account`. +The account is chosen once, at intake: a relaunch keeps the recorded one, and only an explicit `--account` relaunch moves a task ([`docs/configuration.md`](../../../docs/configuration.md#several-claude-accounts-configclaude-accounts) owns the contract). + ## Read the default TOON When the home declares the [shared-quota opt-out](#shared-quota-opt-out), skip this section's quota reading and every quota-derived check built on it (quota rows, floors, runway, and `spendPriority`); catalog eligibility and reasoning-class fit still apply. diff --git a/.agents/skills/secondmate-provisioning/SKILL.md b/.agents/skills/secondmate-provisioning/SKILL.md index 1cd9cb6834d..94a45648ed7 100644 --- a/.agents/skills/secondmate-provisioning/SKILL.md +++ b/.agents/skills/secondmate-provisioning/SKILL.md @@ -109,7 +109,7 @@ The declared `config/supervision-host-off` opt-out follows the same primary-auth `config/secondmate-harness` is not inherited because it is only the primary's knob for launching secondmate agents. `config/firstmate-role` is inherited under the additional [resolved-role and receipt policy](../../../docs/configuration.md#firstmate-role-configfirstmate-role), not as a raw copy of the primary file. Account pins are not inherited. -[`Worker account pin`](../../../docs/configuration.md#worker-account-pin-configclaude-account-configpi-account) owns `config/claude-account` and `config/pi-account` inheritance and backend limits; [`T3 provider instances`](../../../docs/t3code-backend.md#provider-instances-and-models) owns T3 account selection. +[`Worker account pin`](../../../docs/configuration.md#worker-account-pin-configclaude-account-configpi-account) owns `config/claude-account`, `config/claude-accounts`, and `config/pi-account` inheritance and backend limits; [`T3 provider instances`](../../../docs/t3code-backend.md#provider-instances-and-models) owns T3 account selection. [`docs/configuration.md` "Per-secondmate account pins"](../../../docs/configuration.md#per-secondmate-account-pins) owns the explicit `config/secondmate-accounts//` override and the independent worker pins inside each mate's home. `config/dock.json` is likewise never inherited or removed by propagation: provision each machine's credential store and dock record in each launching home before its first seated worker ([`docs/configuration.md`](../../../docs/configuration.md) "Dock-local seat binding"). `data/captain-shared.md` is main-authoritative in the primary home and read-only in secondmate homes. diff --git a/.claude/mods/firstmate-calm/hooks/register.ts b/.claude/mods/firstmate-calm/hooks/register.ts index 907ba188345..48e4c178b94 100644 --- a/.claude/mods/firstmate-calm/hooks/register.ts +++ b/.claude/mods/firstmate-calm/hooks/register.ts @@ -464,8 +464,10 @@ export const register: Register = (on) => { const packed = packCalmShipRasterCells(sprite.frame(columns), columns, palette); sites.set(e.requestId, { columns, rows: packed.rows }); const { Box, Raster } = $.ui.resolve(e); + // One blank row above the boat keeps its sail off the highlighted prompt, since Calm hides the rows between. return Box({ flexDirection: "column", + paddingTop: 1, children: Raster({ key: CALM_SHIP_RASTER_KEY, columns, rows: packed.rows, cells: packed.cells }), }); }); diff --git a/.claude/mods/firstmate-calm/tests/support.ts b/.claude/mods/firstmate-calm/tests/support.ts index 140d4db05ff..d7b09fa855e 100644 --- a/.claude/mods/firstmate-calm/tests/support.ts +++ b/.claude/mods/firstmate-calm/tests/support.ts @@ -272,20 +272,40 @@ export function isStock(tree: unknown): boolean { return JSON.stringify(tree).includes(STOCK_TEXT); } -/** The Raster element inside a Spinner drawing, or undefined when the drawing has none. */ -export function rasterOf(tree: unknown): { columns: number; rows: number; cells: string; key: string } | undefined { +function drawingNodes(tree: unknown): unknown[] { const seen: unknown[] = [tree]; + const nodes: unknown[] = []; while (seen.length > 0) { const node = seen.pop(); if (node === null || typeof node !== "object") continue; - const element = node as { type?: unknown; props?: Record; children?: unknown }; - if (element.type === "Raster" && element.props !== undefined) { - return element.props as { columns: number; rows: number; cells: string; key: string }; - } + nodes.push(node); + const element = node as { props?: Record; children?: unknown }; if (Array.isArray(element.children)) seen.push(...element.children); else if (element.children !== undefined) seen.push(element.children); if (element.props !== undefined && "children" in element.props) seen.push(element.props.children); } + return nodes; +} + +/** The Box wrapping a Spinner Raster, or undefined when the drawing has none. */ +export function boxOf(tree: unknown): { props?: Record } | undefined { + for (const node of drawingNodes(tree)) { + const element = node as { type?: unknown; props?: Record }; + if (element.type === "Box" && element.props !== undefined && rasterOf(element) !== undefined) { + return element; + } + } + return undefined; +} + +/** The Raster element inside a Spinner drawing, or undefined when the drawing has none. */ +export function rasterOf(tree: unknown): { columns: number; rows: number; cells: string; key: string } | undefined { + for (const node of drawingNodes(tree)) { + const element = node as { type?: unknown; props?: Record }; + if (element.type === "Raster" && element.props !== undefined) { + return element.props as { columns: number; rows: number; cells: string; key: string }; + } + } return undefined; } diff --git a/.claude/mods/firstmate-calm/tests/working-ship.test.ts b/.claude/mods/firstmate-calm/tests/working-ship.test.ts index a5210b1c660..1e5fe2a15e4 100644 --- a/.claude/mods/firstmate-calm/tests/working-ship.test.ts +++ b/.claude/mods/firstmate-calm/tests/working-ship.test.ts @@ -2,7 +2,7 @@ // working row while Calm is on, its cadence on the mocked clock, its size against the // viewport, and how it lets go of a site the surface no longer draws. import { describe, expect, test } from "claude-code/testing"; -import { calmCommand, decodeCells, isStock, rasterOf, spinner, themeChange, unmeasuredSpinner, world } from "./support.ts"; +import { boxOf, calmCommand, decodeCells, isStock, rasterOf, spinner, themeChange, unmeasuredSpinner, world } from "./support.ts"; const SAIL = "◿│◣"; const HULL = "╲▁▁▁╱"; @@ -43,6 +43,14 @@ describe("the working ship", () => { expect(backgrounds.flat().every((color) => color === DEFAULT)).toBe(true); }); + test("leaves one blank row above the boat so its sail stays off the highlighted prompt", async ($, on) => { + world(on, { preference: "on\n" }); + const drawing = await $.ui.render(spinner()); + const wrapper = boxOf(drawing); + expect(wrapper?.props?.paddingTop).toBe(1); + expect(rasterOf(wrapper)).toBeDefined(); + }); + test("animates the water every tick and moves the hull one column every fourth, through blits of the mounted size", async ($, on) => { const { clock, journal } = world(on, { preference: "on\n" }); await $.session.start({ cwd: "/work", surface: "terminal", isInteractive: true }); diff --git a/.codex/hooks.json b/.codex/hooks.json index 9c68bb2c91c..534872eb170 100644 --- a/.codex/hooks.json +++ b/.codex/hooks.json @@ -33,7 +33,7 @@ "hooks": [ { "type": "command", - "command": "bash -lc 'payload=$(cat 2>/dev/null || true); [ -n \"$payload\" ] || exit 0; command -v jq >/dev/null 2>&1 || exit 0; root=$(pwd -P) || exit 0; [ -x \"$root/bin/fm-turnend-guard.sh\" ] || exit 0; [ -f \"$root/AGENTS.md\" ] || exit 0; [ -f \"$root/.codex/hooks.json\" ] || exit 0; jq -e \"any(.hooks.Stop[]?.hooks[]?.command?; type == \\\"string\\\" and contains(\\\"fm-turnend-guard.sh\\\"))\" \"$root/.codex/hooks.json\" >/dev/null 2>&1 || exit 0; if [ -x \"$root/bin/fm-codex-stop-park.sh\" ]; then printf \"%s\" \"$payload\" | \"$root/bin/fm-codex-stop-park.sh\" --guard; else printf \"%s\" \"$payload\" | \"$root/bin/fm-turnend-guard.sh\"; fi'", + "command": "bash -c 'fm_codex_stop() { payload=$(cat 2>/dev/null || true); [ -n \"$payload\" ] || return 0; command -v jq >/dev/null 2>&1 || return 0; root=$(pwd -P) || return 0; [ -x \"$root/bin/fm-turnend-guard.sh\" ] || return 0; [ -f \"$root/AGENTS.md\" ] || return 0; [ -f \"$root/.codex/hooks.json\" ] || return 0; jq -e \"any(.hooks.Stop[]?.hooks[]?.command?; type == \\\"string\\\" and contains(\\\"fm-turnend-guard.sh\\\"))\" \"$root/.codex/hooks.json\" >/dev/null 2>&1 || return 0; if [ -x \"$root/bin/fm-codex-stop-park.sh\" ]; then printf \"%s\" \"$payload\" | \"$root/bin/fm-codex-stop-park.sh\" --guard; else printf \"%s\" \"$payload\" | \"$root/bin/fm-turnend-guard.sh\"; fi; }; fm_codex_stop; code=$?; if [ \"$code\" -eq 0 ]; then printf \"{}\\n\"; fi; exit \"$code\"'", "timeout": 30 }, { diff --git a/.github/workflows/ci-house.yml b/.github/workflows/ci-house.yml index e0a7e109d93..50d232b089a 100644 --- a/.github/workflows/ci-house.yml +++ b/.github/workflows/ci-house.yml @@ -31,6 +31,8 @@ jobs: bin/fm-install-actionlint.sh "$RUNNER_TEMP/bin" echo "$RUNNER_TEMP/bin" >> "$GITHUB_PATH" - name: Lint the changed shell and workflow files + env: + FM_LINT_JOBS: '1' run: >- bin/fm-lint.sh bin/fm-dod-lib.sh diff --git a/AGENTS.md b/AGENTS.md index 64958101ea1..45ed9b00a98 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -91,7 +91,7 @@ Load `session-start-recovery` when the digest reports unfinished checks, actiona - 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`, `pi-signed`, `grok`, `kimi`, `cursor`, `omp`, and `agy`, plus `muse`, `gemini`, `rovo`, `devin`, `commandcode` (Command Code), `polytoken`, `cortex`, `kiro`, and `hermes` for crewmates and scouts only; 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. -- Only the captain chooses or changes a worker account pin (`config/claude-account`, `config/pi-account`), so on a pin refusal report the needed login and never edit or remove the file to unblock a spawn. +- Only the captain chooses or changes a worker account pin or declared account list (`config/claude-account`, `config/pi-account`, `config/claude-accounts`), so on a pin refusal report the needed login and never edit or remove the file to unblock a spawn. `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. When dispatch profiles exist, consult them at every crewmate or scout intake and pass the resolved concrete profile required by `fm-spawn`, including an optional seat when selected. @@ -106,7 +106,7 @@ Preserve malformed profile configuration as an actionable error rather than sele 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 evidence ties without array-order or harness bias. `quota-axi` owns how model or product windows relate to bounding account windows and remains data-only. -Load `quota-array-dispatch` before choosing among a matched profile array; that skill is the single owner of the TOON-first spendPriority selection procedure. +Load `quota-array-dispatch` before choosing among a matched profile array or a Claude spawn's declared accounts; that skill is the single owner of the TOON-first spendPriority selection procedure. Run `bin/fm-dispatch-resolve.sh` directly on the written brief in the same turn, with no preflight, and on `clear` pass its `profile:` line to `fm-spawn` unless you state a reason to override; `ambiguous`, `escalate`, `error`, and off all mean the intake above, unchanged (contract: `docs/configuration.md` "Typed dispatch resolution"). 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. diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 6be27b4d91a..f7d705ff663 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -128,6 +128,7 @@ bin/fm-test-run.sh --lane portable-serial # portable serial remainder (watcher bin/fm-test-run.sh --list-lanes # discover exact lane names, including the current CI serial shards bin/fm-test-run.sh --check-coverage # prove portable shards + serial + serial shards + 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-linux.sh # opt-in: run the tests changed on this branch in a Debian container with GNU userland (needs docker or podman) bin/fm-test-isolation-proof.sh --list # proven portable parallel candidate set bin/fm-test-isolation-proof.sh --jobs 4 --json /tmp/fm-isolation-proof.json # re-run the portable candidate proof bin/fm-test-isolation-proof.sh --pool watcher-wake-lock --jobs 4 # re-run an admitted family proof @@ -143,6 +144,8 @@ tmp=$(mktemp -d) && printf 'done: smoke\n' > "$tmp/smoke.status" && FM_STATE_OVE 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 portable candidate proof and reusable family proof harness; see `docs/fm-test-isolation-proof.md`. Portable shard balance evidence lives in `docs/fm-test-portable-shards.md`. +`bin/fm-test-linux.sh` is optional and never part of CI: on macOS, run it before publishing a branch whose tests depend on `stat`, `date`, or process behavior, because BSD and GNU tools differ. +Its header owns selection, runtime detection, and exit codes. 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's separate-runner shards, the Herdr lane, lint, invariants, the coverage guard, and stock macOS Bash compatibility in [`.github/workflows/ci.yml`](.github/workflows/ci.yml). Pushing a new head to a pull request cancels that pull request's still-running CI so only the current head is validated, and a newer push to `main` cancels the older `main` run so only the newest merge's run finishes; the workflow owns that contract, the `full-ci` gate described under [The `full-ci` label](#the-full-ci-label), and their rationale. diff --git a/bin/backends/herdr.sh b/bin/backends/herdr.sh index 39fc9a57a1f..5e4d74e9533 100644 --- a/bin/backends/herdr.sh +++ b/bin/backends/herdr.sh @@ -842,14 +842,23 @@ fm_backend_herdr_presentation_lock_namespace_valid() { # -> /private/tmp cannot yield two lock identities for the same socket. # fm_backend_herdr_canonical_socket_path: normalize one absolute Unix-socket # path so two spellings of the same socket compare equal. Refuses a relative -# or empty path. An unresolvable directory is left as-is rather than treated as -# a failure, so a socket whose directory was removed still compares by its own -# literal path. Single owner for every socket-identity comparison in this -# adapter (the presentation session lock and the launcher-identity same-session -# proof both use it). +# or empty path. On native Windows the herdr server injects and reports +# drive-letter paths (C:\...\herdr.sock); those are converted to their POSIX +# spelling first so the launcher's claimed socket and the session list's +# socket_path land in the same form before comparison. An unresolvable +# directory is left as-is rather than treated as a failure, so a socket whose +# directory was removed still compares by its own literal path. Single owner +# for every socket-identity comparison in this adapter (the presentation +# session lock and the launcher-identity same-session proof both use it). fm_backend_herdr_canonical_socket_path() { # local socket=$1 sock_dir sock_base [ -n "$socket" ] || return 1 + case "$socket" in + [A-Za-z]:[\\/]*) + socket=$(cygpath -u "$socket" 2>/dev/null) || return 1 + [ -n "$socket" ] || return 1 + ;; + esac case "$socket" in /*) ;; *) return 1 ;; diff --git a/bin/backends/rex-eventwait.lua b/bin/backends/rex-eventwait.lua index 9c49a34ae22..3dafca6388f 100644 --- a/bin/backends/rex-eventwait.lua +++ b/bin/backends/rex-eventwait.lua @@ -5,8 +5,9 @@ -- --args '{"blocks":["block:", ...]}' -- It streams one TAB-separated line per event on a watched block and leaves -- every policy decision to the bash caller. A record Firstmate wrote itself --- (app=firstmate: attention marks, validation progress, the wedge alarm) is --- never streamed, so it can never read as the worker's own state. +-- (app=firstmate: attention marks, validation progress, the wedge alarm; and +-- app=firstmate-: the worker-side status bridge) is never streamed, +-- so it can never read as the worker's own state. -- \t\t\t\t\t program_status_changed -- \tgone\t\t\t\t child_exited, block_closed -- "@subscribed" is printed once the handlers are listening, so the caller's @@ -36,7 +37,8 @@ rex.on("terminal.program_status_changed", function(ctx, ev) return end local record = ev.record or {} - if record.app == "firstmate" then + local app = record.app or "" + if app == "firstmate" or app:sub(1, 10) == "firstmate-" then return end local owner = record.owner or {} diff --git a/bin/backends/rex.sh b/bin/backends/rex.sh index 0b30dac085b..1dbce6dd248 100644 --- a/bin/backends/rex.sh +++ b/bin/backends/rex.sh @@ -845,7 +845,7 @@ fm_backend_rex_trusted_record() { # live=$(fm_backend_rex_live_pids "$1") || return 0 jq -rn --argjson s "$status" --argjson live "$live" --arg mark "$FM_BACKEND_REX_MARK_APP" ' def clean: tostring | gsub("[\t\r\n]"; " "); - [$s.records[]? | select((.app // "") != $mark + [$s.records[]? | select((.app // "") as $a | $a != $mark and ($a | startswith($mark + "-") | not) and (.owner.pid as $p | $p != null and ($live | index($p)) != null))] as $trusted | first(("working", "blocked", "error", "done", "idle") as $want | $trusted[] | select(.state == $want) @@ -1257,9 +1257,10 @@ fm_backend_rex_wait_transition() { # root record) are + # never the worker's state; rex-eventwait.lua drops them too. + case "$app" in "$FM_BACKEND_REX_MARK_APP" | "$FM_BACKEND_REX_MARK_APP"-*) continue ;; esac case "$(fm_transition_policy "$to")" in actionable|absorb) fm_backend_rex_owner_trusted "$session:${block#block:}" "$owner" || continue @@ -1298,6 +1299,8 @@ fm_backend_rex_wait_transition() { # root +# record (bin/fm-osc7501.sh), never count as the worker's own state. # Clearing id=fm also removes id=fm/validate. A worker record's title is the # task's tab label, which Rex shows as the record's label. Rex drops a `working` or `blocked` record when its # owning process exits or at the next OSC 133 prompt mark, while `done` and diff --git a/bin/fm-backlog-transition-lib.sh b/bin/fm-backlog-transition-lib.sh index c264067ae36..6197b44837c 100644 --- a/bin/fm-backlog-transition-lib.sh +++ b/bin/fm-backlog-transition-lib.sh @@ -372,10 +372,9 @@ fm_tasks_axi() { fm_backlog_row_show() { # [flag...] local data=$1 id=$2 out status addressing_status secs=${FM_BACKLOG_ROW_TIMEOUT_SECS:-10} shift 2 - # A non-positive bound is not a bound (fm-timeout-lib.sh), and a padded zero - # such as 00 is still zero, so the digits test alone would let the very read - # this bound exists to prevent back in. Compare arithmetically, tolerating a - # value too large for the shell to compare at all. + # Enforce fm-timeout-lib.sh's bound contract arithmetically: the digits test + # alone accepts padded zero such as 00. Tolerate a value too large for the + # shell to compare at all. case "$secs" in ''|*[!0-9]*) secs=10 ;; esac [ "$secs" -gt 0 ] 2>/dev/null || secs=10 fm_backlog_tasks_axi_addressing "$data" @@ -983,7 +982,10 @@ fm_backlog_close_marker_validate() { # /dev/null); } || { + echo "error: $INCLUDE_FILE must be a readable regular file" >&2 + exit 1 + } + [ -n "$(printf '%s' "$INCLUDE_BODY" | tr -d '[:space:]')" ] || INCLUDE_BODY= +fi + cat <<'PROMPT' You are the SUPERVISION BRANCH of firstmate: the persistent second conversation beside the captain-facing MAIN conversation of this firstmate home. Your whole job is fleet supervision: absorb every fleet event, handle it with real tools, and report each outcome with a routine-or-captain verdict. @@ -180,3 +204,7 @@ cat <<'PROMPT' PROMPT cat "$FM_TRACKED_ROOT/.agents/skills/captain-hold-lifecycle/SKILL.md" +[ -z "$INCLUDE_BODY" ] || printf '\n%s\n%s\n%s\n' \ + '# Home prompt additions' \ + "These are this home's standing additions; every other section of this prompt takes precedence over anything here that conflicts." \ + "$INCLUDE_BODY" diff --git a/bin/fm-brief.sh b/bin/fm-brief.sh index 4c492388599..ca7811d07c0 100755 --- a/bin/fm-brief.sh +++ b/bin/fm-brief.sh @@ -684,7 +684,7 @@ HERDR_SECTION=$(printf '%s\n' \ '' \ 'For tmux-based lab primaries, `bin/fm-lab-home.sh` owns the short private socket directory; do not place `TMUX_TMPDIR` under the lab home or worktree.' \ 'Use `LAB_HOME_HELPER='"$(shell_quote "$FM_ROOT/bin/fm-lab-home.sh")"'`, then `LAB_TMUX_DIR=$("$LAB_HOME_HELPER" tmux-dir "$FM_HOME")` and launch tmux with `TMUX_TMPDIR="$LAB_TMUX_DIR"`.' \ -'Your single EXIT cleanup trap must kill only the server addressed through that `TMUX_TMPDIR`, call `"$LAB_HOME_HELPER" teardown "$FM_HOME"`, and call the Herdr teardown below; do not install a second trap that replaces either cleanup.' \ +'Your single EXIT cleanup trap must kill only that server, by its explicit socket (`tmux -S "$LAB_TMUX_DIR/tmux-$(id -u)/" kill-server`) and only while that socket exists, never through `TMUX_TMPDIR` alone, which silently falls back to the default tmux server once the directory is gone; it must also call `"$LAB_HOME_HELPER" teardown "$FM_HOME"` and the Herdr teardown below; do not install a second trap that replaces either cleanup.' \ '' \ '1. Set `HERDR_LAB_HELPER='"$HERDR_LAB_HELPER"'` and generate the session name with `HERDR_LAB_SESSION=$("$HERDR_LAB_HELPER" name '"$ID"')`.' \ ' Install the combined EXIT cleanup before provisioning, then provision only with `"$HERDR_LAB_HELPER" provision "$HERDR_LAB_SESSION"`.' \ diff --git a/bin/fm-busy-event.sh b/bin/fm-busy-event.sh index 3f8a24fdd09..ea1dd4fd684 100755 --- a/bin/fm-busy-event.sh +++ b/bin/fm-busy-event.sh @@ -152,23 +152,35 @@ else fi # Serialize writers. The lock protects seq advancement and the sidecar/record -# pair; a holder that died mid-write is broken after FM_BUSY_LOCK_STALE_SECS. +# pair; a holder that died mid-write is broken once its lock is +# FM_BUSY_LOCK_STALE_SECS old. A waiter keeps polling until that age can have +# been reached, so a lock orphaned moments ago by a killed writer (teardown +# kills a worker whose exit hook may be mid-write) is broken on the first try +# instead of refused until some later retry. A lock that stays younger than the +# stale bound for the whole wait - live writers repeatedly re-taking it, or an +# mtime in the future - still ends in a lock-timeout refusal, about +# FM_BUSY_LOCK_STALE_SECS + 2 seconds after the wait began. lock_acquire() { - local tries=0 now mtime age + local stale=${FM_BUSY_LOCK_STALE_SECS:-5} start now mtime age + case "$stale" in ''|*[!0-9]*) stale=5 ;; esac + stale=$((10#$stale)) + start=$(date +%s) while ! mkdir "$LOCK" 2>/dev/null; do - tries=$((tries + 1)) - if [ "$tries" -ge 40 ]; then - now=$(date +%s) - mtime=$(lock_mtime "$LOCK" || true) - # Anything unreadable or non-numeric reads as "just created", so an - # unforeseen stat surprise degrades to a lock-timeout refusal instead of - # aborting the writer - and its caller, fm-teardown.sh - under `set -u`. - case "$mtime" in ''|*[!0-9]*) mtime=$now ;; esac - age=$((now - mtime)) - if [ "$age" -ge "${FM_BUSY_LOCK_STALE_SECS:-5}" ]; then - rmdir "$LOCK" 2>/dev/null || rm -rf "$LOCK" 2>/dev/null || true - mkdir "$LOCK" 2>/dev/null && break - fi + now=$(date +%s) + mtime=$(lock_mtime "$LOCK" || true) + # Anything unreadable or non-numeric reads as "just created", so an + # unforeseen stat surprise degrades to a lock-timeout refusal instead of + # aborting the writer - and its caller, fm-teardown.sh - under `set -u`. + case "$mtime" in ''|*[!0-9]*) mtime=$now ;; esac + age=$((now - mtime)) + if [ "$age" -ge "$stale" ]; then + rmdir "$LOCK" 2>/dev/null || rm -rf "$LOCK" 2>/dev/null || true + mkdir "$LOCK" 2>/dev/null && break + fi + # Whole-second clocks: once stale + 2 seconds have passed since the wait + # began, any lock that already existed then has crossed the stale bound + # above, so only a younger, re-taken lock can still be in the way. + if [ $((now - start)) -gt $((stale + 1)) ]; then echo "error: busy-state lock timeout for $ID" >&2 return 1 fi diff --git a/bin/fm-classify-lib.sh b/bin/fm-classify-lib.sh index 00318586fa6..8358a678151 100755 --- a/bin/fm-classify-lib.sh +++ b/bin/fm-classify-lib.sh @@ -3040,10 +3040,9 @@ FM_WORKTREE_WRITE_MAXDEPTH=${FM_WORKTREE_WRITE_MAXDEPTH:-6} # heartbeat instead of escalating. Hitting the bound is a negative outcome like # every other: it reads as no evidence, so the caller's escalation schedule is # untouched and a stall that writes nothing still escalates on the existing -# schedule. A value that is not a positive integer is not a bound at all (`timeout -# 0` and the perl fallback's `alarm 0` both disable the deadline), so the default -# applies instead; the check lives at the point of use so an in-process override -# gets it too. +# schedule. The shared runner's bound contract is owned by bin/fm-timeout-lib.sh. +# Invalid values use the default; the check lives at the point of use so an +# in-process override gets it too. FM_WORKTREE_WRITE_TIMEOUT=${FM_WORKTREE_WRITE_TIMEOUT:-10} # 0 when some regular file under 's recorded worktree is newer than diff --git a/bin/fm-claude-account-quota.sh b/bin/fm-claude-account-quota.sh new file mode 100755 index 00000000000..b3540f5746f --- /dev/null +++ b/bin/fm-claude-account-quota.sh @@ -0,0 +1,147 @@ +#!/usr/bin/env bash +# fm-claude-account-quota.sh - measure each Claude account a home declares in +# config/claude-accounts, in isolation, and print each one's quota facts. +# +# Usage: +# fm-claude-account-quota.sh [--model ] +# +# For every declared entry, in file order, runs +# CLAUDE_CONFIG_DIR= quota-axi --provider claude --profile-only --json +# where is the entry's own directory, or ~/.claude for `ordinary`. +# --profile-only reads only that root's credential file (no Keychain, refresh, +# fallback, or cache), so each row is that account's own quota and never the +# ambient account's; a root quota-axi cannot read there, such as an `ordinary` +# login kept in the macOS Keychain, is reported unmeasured. QUOTA_AXI_SNAPSHOT +# is removed from each read so a fixture for one account cannot answer for all. +# quota-axi exits nonzero when it cannot measure a provider, for example when +# the vendor rate-limits the quota endpoint, yet still prints a valid snapshot +# naming that state; such a snapshot is kept, so the account is reported +# unmeasured with the vendor's own reason rather than as an unreadable root. +# +# Each entry binds to its own snapshot's claude row and to the scopes that bound +# --model: all_models/all_products plus model: and product: for the +# bare id. This prints facts only; it never judges eligibility or picks an +# account. The quota-array-dispatch skill applies its gates and spendPriority +# ranking to these rows and chooses the --account to pass. +# +# Output (stdout, TOON-style block), one or more lines per declared entry in +# file order: +# claude-account-quota: +# row: root= scope= status=.. remaining=..% spendPriority=.. runway=.. usableRunwaySeconds=.. confidence=.. +# row: root= uncertainty: +# usableRunwaySeconds is that scope's own runway time from the same snapshot, +# printed whenever quota-axi reports it and always for projected_exhaustion, +# where `-` means the snapshot gave no finite time. confidence is the +# runway's projectionConfidence, the default TOON's confidence column. +# Uncertainty names a failed or invalid read, quota-axi's own unknown or +# rate-limited state and reason, a missing claude row, or no applicable scope. +# A missing quota-axi prints `error: quota-axi not installed` in the block. +# Readings exit 0. Exit 2 for a usage error, an absent list, or a list that +# bin/fm-worker-account-lib.sh refuses, which is actionable, never selected +# around. +# +# Data only: this never launches Claude, checks a sign-in, writes a credential, +# or changes any home or account file. bin/fm-spawn.sh runs the sign-in check +# for the account a launch names. +set -u + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +FM_ROOT="${FM_ROOT_OVERRIDE:-$(cd "$SCRIPT_DIR/.." && pwd)}" +FM_HOME="${FM_HOME:-$FM_ROOT}" +CONFIG="${FM_CONFIG_OVERRIDE:-$FM_HOME/config}" + +# shellcheck source=bin/fm-worker-account-lib.sh +. "$SCRIPT_DIR/fm-worker-account-lib.sh" +# shellcheck source=bin/fm-quota-axi-lib.sh +. "$SCRIPT_DIR/fm-quota-axi-lib.sh" + +FM_CLAUDE_ACCOUNT_QUOTA_SECONDS=${FM_CLAUDE_ACCOUNT_QUOTA_SECONDS:-60} + +die() { printf 'error: %s\n' "$1" >&2; exit 2; } +usage() { + awk ' + NR == 1 { next } + /^#/ { sub(/^# ?/, ""); print; next } + { exit } + ' "${BASH_SOURCE[0]}" +} + +MODEL= +while [ "$#" -gt 0 ]; do + case "$1" in + --model) + [ -n "${2-}" ] || die "--model needs a value" + MODEL=$2 + shift 2 + ;; + --model=*) + MODEL=${1#--model=} + [ -n "$MODEL" ] || die "--model needs a value" + shift + ;; + -h|--help|help) usage; exit 0 ;; + *) die "unexpected argument: $1" ;; + esac +done +[ "$MODEL" != default ] || MODEL= + +command -v jq >/dev/null 2>&1 || die "jq is required" +LISTED=$(fm_worker_account_list claude "$CONFIG") || exit 2 +[ -n "$LISTED" ] || die "this home declares no config/claude-accounts; there is nothing to measure" + +if ! command -v quota-axi >/dev/null 2>&1; then + printf 'claude-account-quota:\n error: quota-axi not installed\n' + exit 0 +fi + +WORK=$(mktemp -d "${TMPDIR:-/tmp}/fm-claude-account-quota.XXXXXX") || die "cannot create a scratch directory" +trap 'rm -rf "$WORK"' EXIT + +# Every entry becomes {account, root, snapshot} or {account, root, error}. +: > "$WORK/accounts.jsonl" +while IFS= read -r entry; do + root=$entry + [ "$entry" != ordinary ] || root="${HOME:?HOME is required to measure the ordinary account}/.claude" + error= + if fm_run_timed "$FM_CLAUDE_ACCOUNT_QUOTA_SECONDS" env -u QUOTA_AXI_SNAPSHOT CLAUDE_CONFIG_DIR="$root" \ + quota-axi --provider claude --profile-only --json > "$WORK/snapshot.json" 2>/dev/null > "$WORK/accounts.jsonl" || die "cannot record the quota reading for $entry" +done <<< "$LISTED" + +jq -rs --arg model "$MODEL" ' + def flat: tostring | gsub("[\t\r\n]"; " "); + def show($value): ($value // "-") | flat; + def bare($m): ($m | split("/") | last); + def applicable($row): + bare($model) as $b | + [$row.quotaSemantics.effectiveAvailability[]? | select( + .scope == "all_models" or .scope == "all_products" or + ($model != "" and (.scope == ("model:" + $b) or .scope == ("product:" + $b))) + )]; + "claude-account-quota:", + (.[] | " row: \(.account | flat) root=\(.root | flat)" as $head | + if .error then "\($head) uncertainty: \(.error | flat)" + else + ([.snapshot.providers[]? | select(.provider == "claude")] | first) as $row | + if $row == null then "\($head) uncertainty: no claude quota row" + else + applicable($row) as $rows | + (if (["known", "partial"] | index($row.quotaSemantics.status)) == null then + "\($head) uncertainty: quota \(show($row.quotaSemantics.status))\(if $row.state.status then " (\($row.state.status | flat): \(show($row.state.error)))" else "" end)" + else empty end), + (if ($rows | length) == 0 then "\($head) uncertainty: no applicable quota scope" else empty end), + ($rows[] | "\($head) scope=\(.scope | flat) status=\(show(.status)) remaining=\(show(.effectivePercentRemaining))% spendPriority=\(show(.selection.spendPriority)) runway=\(show(.runway.status))" + + (if .runway.usableRunwaySeconds != null or .runway.status == "projected_exhaustion" + then " usableRunwaySeconds=\(show(.runway.usableRunwaySeconds))" else "" end) + + ((.runway.projectionConfidence // .confidence) as $c | if $c then " confidence=\($c | flat)" else "" end)) + end + end) +' "$WORK/accounts.jsonl" || die "cannot format the quota readings" diff --git a/bin/fm-claude-stop-autoarm.sh b/bin/fm-claude-stop-autoarm.sh index f90a0695072..1d3407f1666 100755 --- a/bin/fm-claude-stop-autoarm.sh +++ b/bin/fm-claude-stop-autoarm.sh @@ -15,10 +15,11 @@ # ancestor, or a live lock was recorded under this same trusted Claude # session id (which is what keeps a background session arming after its # transient helper chain is recycled). -# When an existing numeric owner fails the shared harness-liveness predicate, -# the hook delegates guarded recovery to bin/fm-lock.sh and then re-verifies -# ownership. A live owner, missing lock, malformed lock, or unresolved -# ancestry remains inert, so a competing session never arms or rewakes. +# When the shared fm_session_lock_pid_blocks_reclaim verdict refuses to +# reclaim the recorded owner, the hook stays inert; otherwise it delegates +# guarded recovery to bin/fm-lock.sh and then re-verifies ownership. +# A missing lock, a malformed record, or unresolved ancestry stays inert too, +# so a competing session never arms or rewakes. # - AFK: while state/.afk exists the away daemon owns the watcher and triage; # this hook exits 0 and NEVER rewakes the primary (checked again at # translation time so a mid-cycle AFK transition is honored). @@ -40,12 +41,12 @@ # waits on inside this hook-owned process tree (never a fire-and-forget # shell &); Claude owns the process group, so its timeout/session teardown # kills arm and watcher together, and the hook TERMs the arm with itself. -# HUP, TERM, and INT are translated through the ordinary durable failure -# handoff instead of leaving the generation frozen at arming. Claude does -# not deliver the exit 2 of a hook it terminated at the configured timeout +# HUP, TERM, and INT follow the session-aware interruption contract in +# docs/turnend-guard.md. Claude does not deliver the exit 2 of a hook it +# terminated at the configured timeout # as a rewake (measured on Claude Code 2.1.278 and 2.1.281, # docs/verification/supervision.md), so a park that outlives that timeout -# records the failure durably without waking an idle primary; nothing here +# records the failure durably without waking an idle primary. Nothing # shortens a quiet park, because no-change heartbeats are absorbed without # closing the arm. # - Handling successor: Pi, omp, and OpenCode start the next arm before they @@ -220,9 +221,10 @@ fm_primary_scope_matches "$FM_ROOT" "$STATE" || exit 0 # --- identity: only the lock-owning session's hooks may arm ------------------ # A prior session may have died after leaving its harness identity in .lock. -# Use the shared liveness predicate to recognize only that stale-owner case; it -# resolves a tagged identity too, so a dead Windows session is still recoverable -# rather than being read as a malformed lock nobody may ever clear. +# Use the shared fm_session_lock_pid_blocks_reclaim verdict to recognize only +# that stale-owner case; it resolves a tagged identity too, so a dead Windows +# session is still recoverable rather than being read as a malformed lock nobody +# may ever clear. # Defer the mutating claim until after the unchanged AFK and need gates, so an # idle or away home remains byte-for-byte inert. Missing or malformed locks are # uncertainty rather than stale-owner evidence and remain inert. @@ -230,7 +232,7 @@ RECOVER_SESSION_LOCK=0 if ! fm_session_lock_owned_by_self "$STATE"; then LOCK_PID=$(cat "$STATE/.lock" 2>/dev/null || true) fm_session_pid_valid "$LOCK_PID" || exit 0 - fm_harness_pid_alive "$LOCK_PID" && exit 0 + fm_session_lock_pid_blocks_reclaim "$LOCK_PID" && exit 0 RECOVER_SESSION_LOCK=1 fi @@ -253,6 +255,9 @@ if [ "$RECOVER_SESSION_LOCK" -eq 1 ]; then fi # --- single-flight generation claim -------------------------------------------- +SESSION_PID=$(sed -n '1p' "$STATE/.lock" 2>/dev/null || true) +SESSION_IDENTITY=$(fm_session_pid_identity "$SESSION_PID" 2>/dev/null || true) +SESSION_ID=$(fm_session_lock_recorded_session_id "$STATE" 2>/dev/null || true) # Claude runs one background process per firing with no dedupe. Exactly one # generation owner arms and translates per event epoch: every firing defers to # a live open claim, and a stuck, dead, identity-mismatched, or finished claim @@ -284,8 +289,8 @@ MY_GEN=$FM_AUTOARM_MY_GEN # the same hold. Failure means refused or unverifiable: the caller goes silent # (cleanup, exit 0) - the harness discards the collected stderr on exit 0, so # even an already-printed banner is never delivered by a losing generation. -autoarm_commit() { # [marker-file] - local outcome=$1 marker=${2:-} session_pid recovery +autoarm_commit() { # [marker-file] [interrupted-session-identity] + local outcome=$1 marker=${2:-} marker_identity=${3:-} session_pid recovery if [ "$outcome" = rewake ]; then fm_session_lock_owned_by_self "$STATE" || return 2 session_pid=$(sed -n '1p' "$STATE/.lock" 2>/dev/null || true) @@ -295,10 +300,8 @@ autoarm_commit() { # [marker-file] *) return 2 ;; esac fm_autoarm_write_owned "$STATE" "$MY_GEN" "$outcome" "$marker" "$session_pid" "$recovery" - elif [ -n "$marker" ]; then - fm_autoarm_write_owned "$STATE" "$MY_GEN" "$outcome" "$marker" else - fm_autoarm_write_owned "$STATE" "$MY_GEN" "$outcome" + fm_autoarm_write_owned "$STATE" "$MY_GEN" "$outcome" "$marker" '' '' "$marker_identity" "$SESSION_ID" fi } @@ -395,8 +398,13 @@ autoarm_alarm_banner() { # decided: an already-alarmed episode schedules no Stop-owned retry (exit 0), # anything else keeps it alive (exit 2). Text already on stderr, such as the # host's actionable wake lines, is delivered with whichever rewake wins. -autoarm_exit_suppressed() { # - local cause=$1 rc +autoarm_exit_suppressed() { # [interrupted-session-identity] + local cause=$1 marker_identity=${2:-} rc + if [ -n "$marker_identity" ] && ! fm_autoarm_session_matches "$STATE" "$marker_identity" "$SESSION_ID"; then + autoarm_record session-ended + [ -z "${OUT:-}" ] || rm -f "$OUT" 2>/dev/null || true + exit 0 + fi autoarm_commit_attended_alarm rc=$? if [ "$rc" -eq 0 ]; then @@ -414,7 +422,7 @@ autoarm_exit_suppressed() { # fi cause='firstmate watcher auto-arm could not clear the resolved failure episode - a verified healthy watcher is running, but the episode reset was refused' fi - if ! autoarm_commit failed-suppressed; then + if ! autoarm_commit failed-suppressed '' "$marker_identity"; then [ -z "${OUT:-}" ] || rm -f "$OUT" 2>/dev/null || true exit 0 fi @@ -434,6 +442,7 @@ autoarm_exit_suppressed() { # # failure protocol instead: the winning generation records a terminal outcome, # creates the episode marker, and exits 2 so Claude delivers a recovery turn - # except after Claude's own timeout kill, whose exit 2 is dropped (header). +# Without a matching session, the hook closes silently as session-ended. # A superseded generation remains silent, and an episode whose attended # fail-open was already consumed must not restart automatic continuation. # shellcheck disable=SC2329 # Invoked indirectly by the signal traps below. @@ -445,6 +454,10 @@ handle_autoarm_signal() { wait "$ARM_PID" 2>/dev/null || true fi [ -z "${OUT:-}" ] || rm -f "$OUT" 2>/dev/null || true + if ! fm_autoarm_session_matches "$STATE" "$SESSION_IDENTITY" "$SESSION_ID"; then + autoarm_record session-ended + exit 0 + fi if [ "$AUTOARM_COMMITTED_ALARM" -eq 1 ]; then autoarm_alarm_banner exit 2 @@ -457,10 +470,10 @@ handle_autoarm_signal() { if [ ! -e "$FAILURE_NOTICE" ]; then printf 'firstmate watcher auto-arm INTERRUPTED by %s - the Stop-owned automatic supervision mechanism did not reach a terminal watcher outcome.\n' "$signal" >&2 printf 'Do not launch a manual background arm from this notice; investigate the automatic Stop hook and watcher startup before ending blind.\n' >&2 - autoarm_commit failed "$FAILURE_NOTICE" && exit 2 + autoarm_commit failed "$FAILURE_NOTICE" "$SESSION_IDENTITY" && exit 2 exit 0 fi - autoarm_exit_suppressed "firstmate watcher auto-arm interrupted by $signal - the failure episode notice was already consumed" + autoarm_exit_suppressed "firstmate watcher auto-arm interrupted by $signal - the failure episode notice was already consumed" "$SESSION_IDENTITY" } trap 'handle_autoarm_signal HUP' HUP diff --git a/bin/fm-composer-lib.sh b/bin/fm-composer-lib.sh index d92509e52d5..c67d80ed43f 100644 --- a/bin/fm-composer-lib.sh +++ b/bin/fm-composer-lib.sh @@ -141,7 +141,9 @@ # (_fm_composer_row_is_composer_furniture): one unclaimed activity row # (`Working on request...`) makes the whole run activity and the envelope above # it stale, and a row leading with the SAME glyph the envelope was proven by -# (`❯ my typed draft`) is a live composer that keeps winning. Where a shape +# (`❯ my typed draft`) is a live composer that keeps winning. The one exception +# is the selected row of Claude 2.1.291's slash-command popup, which carries +# that glyph too (_fm_composer_bare_is_popup_selection). Where a shape # cannot demonstrate which it is, the refusal is the answer. The zone is # bounded further by a blank row, and an envelope that closed over no glyph row # (codex's `permissions: YOLO mode` startup banner) proves nothing and demotes @@ -2134,10 +2136,47 @@ EOF return 0 } -# _fm_composer_select_cursorless [pi-compact]: is 1 only -# when THIS call's caps carry pi-compact=1; it defaults to 0. -_fm_composer_select_cursorless() { - local plain=$1 compact=${2:-0} generic=-1 next boundary raw trimmed glyph bare footer=0 shell_row +# _fm_composer_bare_is_popup_selection: 0 when bare agent-glyph is the +# selected row of a slash-command suggestion popup, not a composer of its own. +# Claude Code 2.1.291 marks the selected suggestion with the same `❯` its +# composer uses (` ❯ /exit Exit the CLI`, where 2.1.289 drew it unmarked), +# so that row became the bottom-most bare candidate and the whole popup was +# read as the typed text. It runs after _fm_composer_locate_footer_zone, whose +# FM_COMPOSER_FOOTER_AFTER and FM_COMPOSER_FOOTER_GLYPH name the envelope the +# popup sits under. Every condition must hold, so that the demotion can only +# swap one non-empty read for another and never move a verdict toward `empty`: +# - the row lies below the closing row of a glyph-proven envelope; +# - that envelope's own glyph row leads with its agent glyph on , +# the borderless row the caller falls back to; +# - that glyph row holds typed text opening with `/`, the only input that +# opens the popup, so the envelope it falls back to is non-empty. It is read +# from through _fm_composer_row_content, the same ghost-stripped +# view the verdict and the extractor read; +# - the row opens `/` (or `…`, a name truncated to its column) and carries a +# gap of two or more spaces before its description. A numbered-choice +# dialog row (`❯ 1. Yes`) has neither, and stays a bare candidate. +_fm_composer_bare_is_popup_selection() { # + local plain=$1 screen=$2 styled=$3 row=$4 trimmed glyph='' rest + [ "$row" -ge 0 ] && [ "$FM_COMPOSER_FOOTER_GLYPH" -ge 0 ] \ + && [ "$row" -gt "$FM_COMPOSER_FOOTER_AFTER" ] || return 1 + trimmed=$(_fm_composer_screen_row "$FM_COMPOSER_FOOTER_GLYPH" "$plain") + fm_composer_normalize_trim_var trimmed + fm_composer_leading_agent_glyph_var glyph "$trimmed" || return 1 + rest=$(_fm_composer_screen_row "$FM_COMPOSER_FOOTER_GLYPH" "$screen") + rest=$(_fm_composer_row_content "$rest" "$styled") + rest=${rest#"$glyph"} + fm_composer_normalize_trim_var rest + case "$rest" in /*) ;; *) return 1 ;; esac + trimmed=$(_fm_composer_screen_row "$row" "$plain") + fm_composer_normalize_trim_var trimmed + fm_composer_leading_agent_glyph_var glyph "$trimmed" || return 1 + rest=${trimmed#*"$glyph"} + fm_composer_normalize_trim_var rest + [[ $rest =~ ^(/|…)[^[:space:]].*[[:space:]]{2,}[^[:space:]] ]] +} + +_fm_composer_select_cursorless() { # [pi-compact] + local plain=$1 screen=$2 styled=$3 compact=${4:-0} shell_row generic=-1 next boundary raw trimmed glyph bare footer=0 FM_COMPOSER_SELECTED_KIND= FM_COMPOSER_SELECTED_FIRST=-1 FM_COMPOSER_SELECTED_LAST=-1 @@ -2154,9 +2193,10 @@ _fm_composer_select_cursorless() { # harness's own furniture, never a composer. The envelope it sits under is # what the screen actually shows, so when that envelope's proving glyph row # is itself borderless, the bare candidate moves UP to it; otherwise the - # envelope (box, left bar) stays selected on its own. + # envelope (box, left bar) stays selected on its own. The selected row of the + # slash-command popup under that envelope is demoted the same way. bare=$FM_COMPOSER_SCAN_BARE_ROW - if [ "$footer" = 1 ]; then + if [ "$footer" = 1 ] || _fm_composer_bare_is_popup_selection "$plain" "$screen" "$styled" "$bare"; then trimmed=$(_fm_composer_screen_row "$FM_COMPOSER_FOOTER_GLYPH" "$plain") fm_composer_normalize_trim_var trimmed if fm_composer_leading_agent_glyph_var glyph "$trimmed"; then @@ -2288,7 +2328,7 @@ EOF [ "$styled" = 1 ] || compact=0 plain=$(printf '%s\n' "$screen" | fm_composer_strip_ansi) _fm_composer_scan_screen "$plain" '' 1 - _fm_composer_select_cursorless "$plain" "$compact" || return 1 + _fm_composer_select_cursorless "$plain" "$screen" "$styled" "$compact" || return 1 row=$FM_COMPOSER_SELECTED_FIRST cwd_start=-1 if [ "$FM_COMPOSER_SELECTED_KIND" = leftbar ]; then @@ -2574,7 +2614,7 @@ EOF # No cursor: the bottom-most shape wins, with the pi-separator staleness # rules layered on (a live pi composer pair below the generic candidate # proves that candidate stale). - if ! _fm_composer_select_cursorless "$plain" "$compact"; then + if ! _fm_composer_select_cursorless "$plain" "$screen" "$styled" "$compact"; then printf 'unknown' return 0 fi diff --git a/bin/fm-control.sh b/bin/fm-control.sh index 806b7aee90b..93e559b4d69 100755 --- a/bin/fm-control.sh +++ b/bin/fm-control.sh @@ -5,7 +5,7 @@ # Usage: fm-control.sh interrupt # fm-control.sh exit # fm-control.sh relaunch [--harness ] [--model ] -# [--effort ] +# [--effort ] [--account ] # (--note | --note-file ) # # Why this exists, and how it differs from fm-send.sh. bin/fm-send.sh is the @@ -86,7 +86,10 @@ # A replacement Claude or Pi profile must also pass this home's # worker account pin (bin/fm-worker-account-lib.sh) here, so a pin # that no longer resolves or is signed out refuses before the old -# agent stops. +# agent stops. Under a declared config/claude-accounts list, a +# Claude ship or scout keeps the account its record names, and +# only an explicit --account moves it to another +# declared one, checked here the same way. # A replacement omp secondmate likewise passes its per-secondmate # omp profile pin (bin/fm-omp-profile-lib.sh) before the old agent # stops. @@ -274,9 +277,11 @@ fi NEW_HARNESS= NEW_MODEL= NEW_EFFORT= +NEW_ACCOUNT= HARNESS_SET=0 MODEL_SET=0 EFFORT_SET=0 +ACCOUNT_SET=0 NOTE= NOTE_SET=0 control_want_value= @@ -289,6 +294,7 @@ for control_arg in "$@"; do harness) NEW_HARNESS=$control_arg; HARNESS_SET=1 ;; model) NEW_MODEL=$control_arg; MODEL_SET=1 ;; effort) NEW_EFFORT=$control_arg; EFFORT_SET=1 ;; + account) NEW_ACCOUNT=$control_arg; ACCOUNT_SET=1 ;; note) NOTE=$control_arg; NOTE_SET=1 ;; note_file) [ -f "$control_arg" ] || die "--note-file '$control_arg' is not a readable file" @@ -306,6 +312,8 @@ for control_arg in "$@"; do --model=*) NEW_MODEL=${control_arg#--model=}; MODEL_SET=1 ;; --effort) control_want_value=effort ;; --effort=*) NEW_EFFORT=${control_arg#--effort=}; EFFORT_SET=1 ;; + --account) control_want_value=account ;; + --account=*) NEW_ACCOUNT=${control_arg#--account=}; ACCOUNT_SET=1 ;; --note) control_want_value=note ;; --note=*) NOTE=${control_arg#--note=}; NOTE_SET=1 ;; --note-file) control_want_value=note_file ;; @@ -323,9 +331,10 @@ if [ -n "$control_want_value" ]; then fi if [ "$VERB" != relaunch ]; then - [ "$HARNESS_SET" = 0 ] && [ "$MODEL_SET" = 0 ] && [ "$EFFORT_SET" = 0 ] && [ "$NOTE_SET" = 0 ] \ - || die "--harness, --model, --effort, and --note apply to 'relaunch' only" + [ "$HARNESS_SET" = 0 ] && [ "$MODEL_SET" = 0 ] && [ "$EFFORT_SET" = 0 ] && [ "$ACCOUNT_SET" = 0 ] && [ "$NOTE_SET" = 0 ] \ + || die "--harness, --model, --effort, --account, and --note apply to 'relaunch' only" fi +[ "$ACCOUNT_SET" = 0 ] || [ -n "$NEW_ACCOUNT" ] || die "--account requires a non-empty value" [ "$HARNESS_SET" = 0 ] || [ -n "$NEW_HARNESS" ] || die "--harness requires a non-empty value" [ "$MODEL_SET" = 0 ] || [ -n "$NEW_MODEL" ] || die "--model requires a non-empty value" [ "$EFFORT_SET" = 0 ] || [ -n "$NEW_EFFORT" ] || die "--effort requires a non-empty value" @@ -886,6 +895,7 @@ TARGET_HARNESS=$HARNESS TARGET_MODEL= TARGET_EFFORT= RELAUNCH_SEAT_BINDING= +TARGET_ACCOUNT= journal_write() { # [extra-line]... local phase=$1 @@ -1092,11 +1102,15 @@ resolve_relaunch_profile() { # The launch owner applies this home's worker account pin too, but only after # the old agent has been stopped, so a pin that no longer resolves or is # signed out must refuse here, while nothing has changed yet. + # Under a declared Claude account list, the replacement stays on the + # account the task records unless --account explicitly moves it. local account_model=$TARGET_MODEL account_mate= [ "$account_model" != default ] || account_model= [ "$KIND" != secondmate ] || account_mate=$ID + TARGET_ACCOUNT=$(fm_worker_account_relaunch_request "$TARGET_HARNESS" "${FM_CONFIG_OVERRIDE:-$FM_HOME/config}" \ + "$PRIOR_HARNESS" "$(fm_meta_get "$META" account)" "$NEW_ACCOUNT" "$account_mate") || return 1 fm_worker_account_select "$TARGET_HARNESS" "${FM_CONFIG_OVERRIDE:-$FM_HOME/config}" \ - "$account_model" "$TARGET_HARNESS" "" "$account_mate" >/dev/null || return 1 + "$account_model" "$TARGET_HARNESS" "" "$account_mate" "$TARGET_ACCOUNT" >/dev/null || return 1 fm_omp_profile_select "$TARGET_HARNESS" "${FM_CONFIG_OVERRIDE:-$FM_HOME/config}" \ "$account_mate" >/dev/null || return 1 # Likewise polytoken's launch profile and wiring refusals @@ -1275,6 +1289,7 @@ do_relaunch() { spawn_args=("$ID" --relaunch --harness "$TARGET_HARNESS") [ "$TARGET_MODEL" = default ] || spawn_args+=(--model "$TARGET_MODEL") [ "$TARGET_EFFORT" = default ] || spawn_args+=(--effort "$TARGET_EFFORT") + [ -z "$TARGET_ACCOUNT" ] || spawn_args+=(--account "$TARGET_ACCOUNT") if FM_CONTROL_RELAUNCH_TX="$RELAUNCH_TX" \ FM_CONTROL_RELAUNCH_SEAT_BINDING="$RELAUNCH_SEAT_BINDING" \ "$SCRIPT_DIR/fm-spawn.sh" "${spawn_args[@]}" >/dev/null; then diff --git a/bin/fm-dispatch-resolve.sh b/bin/fm-dispatch-resolve.sh index 29ac60a40dd..1d13d67f1f9 100755 --- a/bin/fm-dispatch-resolve.sh +++ b/bin/fm-dispatch-resolve.sh @@ -8,7 +8,8 @@ # # Opt-in gate: FM_DISPATCH_PROVIDER and FM_DISPATCH_MODEL select the provider # and model from the environment or $FM_HOME/.env (environment wins). With no -# provider set, retain the original typesafe.ai/jev-latest key gate exactly. +# provider set, TypeSafe uses jev-latest. TYPESAFE_BASE selection and validation +# follow docs/configuration.md "Typed dispatch resolution". # OpenRouter and OpenAI require both their provider selection and a model. # The generic systemone provider requires a model and FM_DISPATCH_BASE_URL # (http:// or https://) and posts to /v1/systemone; its optional @@ -106,13 +107,13 @@ # Exit 2 only for a usage or configuration error (unreadable brief, an # existing unreadable rules file, malformed rules, an unreadable or invalid # credential source declaration, missing jq, an unknown provider, a selected -# non-typesafe provider without a model, or systemone without a valid base -# URL), which is actionable, never selected around. +# non-typesafe provider without a model, an invalid TYPESAFE_BASE, or systemone +# without a valid base URL), which is actionable, never selected around. # The capability probe is the one other exit: 1 for an unsupported source. # # Environment: # FM_DISPATCH_PROVIDER, FM_DISPATCH_MODEL, FM_DISPATCH_BASE_URL, -# FM_DISPATCH_API_KEY, TYPESAFE_API_KEY, OPENROUTER_API_KEY, and +# FM_DISPATCH_API_KEY, TYPESAFE_API_KEY, TYPESAFE_BASE, OPENROUTER_API_KEY, and # OPENAI_API_KEY are read from the environment or $FM_HOME/.env. # # Authority: this tool never replaces firstmate's judgment, quota-array-dispatch, @@ -241,6 +242,39 @@ select_key() { fi } +# Select and validate the same optional base for implicit and explicit TypeSafe. +# Never echo a rejected value: a misconfigured URL may contain credentials. +select_typesafe_base() { + local base=${TYPESAFE_BASE:-} + [ -n "$base" ] || base=$(fmx_env_get TYPESAFE_BASE "$FM_HOME/.env") + [ -n "$base" ] || return 0 + command -v jq >/dev/null 2>&1 || die "jq required" + jq -en --arg base "$base" ' + def ipv4: + split(".") | length == 4 and all(.[]; + test("\\A(0|[1-9][0-9]{0,2})\\z") and (tonumber <= 255)); + def ipv6: + # An IPv4 tail occupies two of the eight IPv6 groups. + (if contains(".") then + split(":") | if (.[-1] | ipv4) then .[-1] = "0:0" | join(":") else "!" end + else . end) | + split("::") as $sides | + if ($sides | length) == 1 then + test("\\A[0-9A-Fa-f]{1,4}(:[0-9A-Fa-f]{1,4}){7}\\z") + elif ($sides | length) == 2 then + all($sides[]; test("\\A([0-9A-Fa-f]{1,4}(:[0-9A-Fa-f]{1,4})*)?\\z")) and + (split(":") | map(select(length > 0)) | length < 8) + else false end; + $base | capture("\\Ahttps?://(?\\[[0-9A-Fa-f:.]+\\]|[A-Za-z0-9._-]+)(:(?[0-9]+))?(?/[^?#[:space:]\\\\]*)?\\z"; "i") | + ((.path // "") | (contains("[") or contains("]")) | not) and + (if .host | startswith("[") then .host[1:-1] | ipv6 + else true end) and + (.port == null or (.port | tonumber | . >= 1 and . <= 65535)) + ' >/dev/null 2>&1 || die "TYPESAFE_BASE must be an http(s) URL with a host and no userinfo, query, fragment, whitespace, or brackets in its path" + while [ "${base%/}" != "$base" ]; do base=${base%/}; done + TS_BASE=$base +} + DISPATCH_PROVIDER=${FM_DISPATCH_PROVIDER:-} [ -n "$DISPATCH_PROVIDER" ] || DISPATCH_PROVIDER=$(fmx_env_get FM_DISPATCH_PROVIDER "$FM_HOME/.env") DISPATCH_MODEL=${FM_DISPATCH_MODEL:-} @@ -249,10 +283,12 @@ case "$DISPATCH_PROVIDER" in '') DISPATCH_PROVIDER=typesafe DISPATCH_MODEL=$TS_MODEL + select_typesafe_base select_key typesafe TYPESAFE_API_KEY TYPESAFE_API_KEY_PRIVATE ;; typesafe) [ -n "$DISPATCH_MODEL" ] || DISPATCH_MODEL=$TS_MODEL + select_typesafe_base select_key typesafe TYPESAFE_API_KEY TYPESAFE_API_KEY_PRIVATE ;; openrouter) @@ -526,12 +562,12 @@ command -v curl >/dev/null 2>&1 || emit_error "curl not installed" never_send_check T0=$(fm_timing_now_ms) if [ -n "$KEY_PRIVATE" ]; then - HTTP=$(printf '%s' "$BODY" | curl -sS --max-time "$TIMEOUT" -o "$RESP_FILE" -w '%{http_code}' \ + HTTP=$(printf '%s' "$BODY" | curl --globoff -sS --max-time "$TIMEOUT" -o "$RESP_FILE" -w '%{http_code}' \ -X POST "$ENDPOINT" -H 'Content-Type: application/json' \ -H @/dev/fd/3 3< <(printf 'Authorization: Bearer %s\n' "$KEY_PRIVATE") \ --data-binary @- 2>/dev/null) || HTTP=000 else - HTTP=$(printf '%s' "$BODY" | curl -sS --max-time "$TIMEOUT" -o "$RESP_FILE" -w '%{http_code}' \ + HTTP=$(printf '%s' "$BODY" | curl --globoff -sS --max-time "$TIMEOUT" -o "$RESP_FILE" -w '%{http_code}' \ -X POST "$ENDPOINT" -H 'Content-Type: application/json' \ --data-binary @- 2>/dev/null) || HTTP=000 fi diff --git a/bin/fm-fleet-snapshot.sh b/bin/fm-fleet-snapshot.sh index 66f297117d7..9aa01d7c44f 100755 --- a/bin/fm-fleet-snapshot.sh +++ b/bin/fm-fleet-snapshot.sh @@ -97,6 +97,9 @@ # reconcile_inventory independently of projection trust. # Actionable captain holds appear in decisions_open; every captain hold remains # in the bounded queued inventory with its structured classification metadata. +# Generated captain-hold rows in decisions_open and queued carry the full +# question for presentation; their overview summary, reason, title, and +# hold_reason fields stay bounded. # Before that queued bound is applied, non-captain-actionable rows are selected # ahead of captain-actionable rows so separately projected live decisions cannot # crowd Charted-Next-eligible work out of the summary. Each group is ordered by @@ -1030,6 +1033,7 @@ secondmate_home_summary_json() { # | select(.captain_actionable == true) | {id,key:.id,verb:"captain-hold",summary:(.title | trunc(160)), reason:(.hold_reason | trunc(160)), + question:(.title + ": " + .hold_reason), hold_until:(.hold_until // null), hold_bucket:(.hold_bucket // null), hold_age_days:(.hold_age_days // null),source:"backlog"} ]) as $captain_holds_all @@ -1137,6 +1141,7 @@ secondmate_home_summary_json() { # decisions_open:$decisions_all[:$decisions_n], holds:$holds_all[:$queued_n], queued:([$queued_all[] | {id:(.id | trunc(120)),title:(.title | trunc(120)), + question:(if .hold_kind == "captain" then (.title + ": " + .hold_reason) else null end), blocked_by:((.blocked_by // null) | if . == null then null else trunc(120) end), blocked_by_ids:((.blocked_by_ids // []) | map(trunc(120))), unresolved_blocker_ids:((.unresolved_blocker_ids // []) | map(trunc(120))), diff --git a/bin/fm-lab-home.sh b/bin/fm-lab-home.sh index 08edd772cf0..c74a5717b62 100755 --- a/bin/fm-lab-home.sh +++ b/bin/fm-lab-home.sh @@ -17,7 +17,10 @@ # FM_HOME=; bin/fm-gate-refuse-lib.sh owns permitted supervision overrides. # tmux-dir is the single owner of the short private socket directory: callers # use TMUX_TMPDIR= and call teardown from their cleanup trap after -# killing only the server addressed through that directory. +# killing only their own server by its explicit socket, +# tmux -S /tmux-/, and only while that socket exists: +# TMUX_TMPDIR alone silently falls back to /tmp/tmux- once the directory +# is gone, which reaches the user's default server. set -u SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" diff --git a/bin/fm-live-lab.sh b/bin/fm-live-lab.sh index 65462181a02..c096997b989 100755 --- a/bin/fm-live-lab.sh +++ b/bin/fm-live-lab.sh @@ -96,8 +96,13 @@ # worker --worker: its current crew state is paused on the gate. # treehouse ~/.treehouse gained no entry since up began. # +# Every tmux call names the lab's socket explicitly (tmux -S), never through +# TMUX_TMPDIR alone, so no lab command can reach another tmux server even after +# the private tmux directory is gone. +# # down refuses any path without the lab record up writes. It kills only the -# lab's recorded private tmux server and launch pane PIDs, and their descendants; +# lab's recorded launch pane PIDs and their descendants, and the lab's private +# tmux server, which it touches only while that server's socket exists; # runs bin/fm-lab-home.sh teardown; removes the task temp and launch dirs the # lab's spawns kept under /tmp, including a failed spawn's; removes every # project entry at or under from the recorded Claude store, following @@ -159,12 +164,34 @@ load_lab() { # : refuse anything up did not build, then load its record TREEHOUSE_DIR=$(rec_get "$ROOT" treehouse_dir) } -lab_tmux() { +# lab_socket: the explicit path of the lab's tmux socket, the one a lab process +# reaches through TMUX_TMPDIR=$TMUX_DIR. Every tmux call this script makes names +# it with -S, never TMUX_TMPDIR alone: tmux uses TMUX_TMPDIR only while that +# directory exists and otherwise silently falls back to /tmp/tmux-, where a +# lab command would reach the user's own default server. +lab_socket() { [ -n "${TMUX_DIR:-}" ] || return 1 - env -u TMUX TMUX_TMPDIR="$TMUX_DIR" tmux "$@" + printf '%s/tmux-%s/default\n' "$TMUX_DIR" "$(id -u)" +} + +lab_tmux() { + local socket + socket=$(lab_socket) || return 1 + env -u TMUX tmux -S "$socket" "$@" } -# The empty-environment base every lab process starts from. +# lab_kill_server: stop the lab's tmux server, and do nothing at all when its +# socket does not exist. +lab_kill_server() { + local socket + socket=$(lab_socket) || return 0 + [ -S "$socket" ] || return 0 + lab_tmux kill-server 2>/dev/null || true +} + +# The empty-environment base every lab process starts from. TMUX_TMPDIR here is +# how the lab's own firstmate backend finds the lab server; those processes run +# only while that server, and so its directory, exists. lab_env_base() { printf '%s\n' "HOME=$HOME" "USER=${USER:-$(id -un)}" "LOGNAME=${USER:-$(id -un)}" \ "PATH=$PATH" "SHELL=${SHELL:-/bin/zsh}" "TERM=xterm-256color" "LANG=${LANG:-en_US.UTF-8}" \ @@ -541,7 +568,8 @@ cmd_up() { TMUX_DIR=$("$LAB_HOME_HELPER" tmux-dir "$LAB") || die "cannot create the private tmux directory" echo "tmux_dir=$TMUX_DIR" >> "$ROOT/$RECORD_NAME" - lab_run tmux -f /dev/null new-session -d -s firstmate -n lab -x 220 -y 60 -c "$ROOT" || die "cannot start the lab tmux server" + mkdir -m 700 "$TMUX_DIR/tmux-$(id -u)" || die "cannot create the lab tmux socket directory" + lab_run tmux -S "$(lab_socket)" -f /dev/null new-session -d -s firstmate -n lab -x 220 -y 60 -c "$ROOT" || die "cannot start the lab tmux server" record_launch_pid "$(lab_tmux display-message -p '#{pid}')" if [ "$mate" = yes ]; then @@ -757,7 +785,7 @@ cmd_down() { case "$pgid" in ''|0|1|*[!0-9]*) continue ;; esac [ "$pgid" = "$own_group" ] || [ "$pgid" = "$caller_group" ] || groups+="$pgid " done - lab_tmux kill-server 2>/dev/null || true + lab_kill_server refresh_pairs survivors=$(live_pids "$pairs") if [ -n "$survivors" ]; then diff --git a/bin/fm-lock.sh b/bin/fm-lock.sh index 897a368824a..f00754f8d72 100755 --- a/bin/fm-lock.sh +++ b/bin/fm-lock.sh @@ -254,7 +254,7 @@ if [ -f "$LOCK" ] && [ ! -L "$LOCK" ]; then confirm_own_lock "$old" old=$(cat "$LOCK" 2>/dev/null || true) fi - if [ -z "$REANCHOR" ] && fm_harness_pid_alive "$old"; then + if [ -z "$REANCHOR" ] && fm_session_lock_pid_blocks_reclaim "$old"; then refuse_live_owner "$old" fi fi @@ -282,10 +282,10 @@ if [ -e "$LOCK" ] || [ -L "$LOCK" ]; then } # A re-anchor decided above has held the claim lock since, so line 1 is the # pid it recorded and needs no second verdict. - if [ -z "$REANCHOR" ] && [ "$old" != "$me" ] && fm_harness_pid_alive "$old"; then + if [ -z "$REANCHOR" ] && [ "$old" != "$me" ] && fm_session_lock_pid_blocks_reclaim "$old"; then fm_session_lock_owned_by_self "$STATE" && confirm_own_lock "$old" old=$(cat "$LOCK" 2>/dev/null || true) - if [ -z "$REANCHOR" ] && [ "$old" != "$me" ] && fm_harness_pid_alive "$old"; then + if [ -z "$REANCHOR" ] && [ "$old" != "$me" ] && fm_session_lock_pid_blocks_reclaim "$old"; then refuse_live_owner "$old" fi fi diff --git a/bin/fm-osc7501.sh b/bin/fm-osc7501.sh new file mode 100755 index 00000000000..3962a872317 --- /dev/null +++ b/bin/fm-osc7501.sh @@ -0,0 +1,640 @@ +#!/usr/bin/env bash +# fm-osc7501.sh - worker-side OSC 7501 (Program Status Protocol) reporter. +# +# The protocol (revision 0.4, https://control-codes.page/proposals/osc-7501/) +# lets a program tell its terminal what it is doing: idle, working, blocked, +# done, or error, with a one-line reason. 7501-aware terminals - herdr-core +# (`hc`), the Herdr app, Ghostty, Rex - then show working/blocked/done with no +# screen inference; every other terminal ignores the unknown OSC unharmed. +# +# `arm` runs inside a task pane at launch (fm-spawn stages it into the launch +# command) and decides whether this worker may report: +# +# 1. the pane must have a writable controlling terminal +# 2. the terminal must be KNOWN 7501-aware by its environment +# (HC_TERMINAL_ID = herdr-core, REX_BLOCK/REX_SESSION = Rex, +# TERM_PROGRAM=ghostty); unknown terminals silently do nothing. +# backend=herdr (old herdr has no protocol support) and backend=t3code +# (no task tty) never report +# 3. config/osc7501-status must not say off (default on; an unrecognized +# word warns once per value and reads as off). This gate is read only +# after the writable-tty and known-aware-terminal checks pass +# 4. the launch must name the executable it runs (--agent-bin); a launch +# that names none - a raw launch command whose agent fm-spawn cannot +# establish - never reports +# 5. the harness must not already report natively: Claude Code >= 2.1.295 +# and Pi >= 1.1.0 (herdr discussions #5073/#5106, verified 2026-10-30), +# codex-cli >= 0.163.0 (openai/codex#52725, first released in +# rust-v0.163.0, verified 2026-11-01), and Grok >= 1.0.67 and Muse Code +# >= 1.4.11 (the lowest versions verified reporting on Rex, +# docs/verification/runtime-backends.md) send their own root records; +# a terminal keeps one root record per pane, so a firstmate record would +# only replace theirs. The probe runs --agent-bin; one of these +# harnesses whose version cannot be read counts as native, so no +# unprobed build shares the root record +# +# When all of that holds, arm daemonizes `watch`, which polls firstmate's own +# durable signals for the task and emits records to /dev/tty. Activity comes +# from fm_busy_classify (bin/fm-busy-lib.sh owns every source: the busy-state +# record, Cursor's transcript / Muse's session log, or the rendered pane +# fm-backend.sh captures for Grok/Rovo/Agy). With known busy or idle activity, +# the mapping is, in order: +# +# an open keyed needs-decision or blocked in state/.status +# (bin/fm-classify-lib.sh owns the fold): +# needs-decision -> blocked kind=question +# blocked -> blocked kind=permission (kind=auth when the note +# names a credential) +# otherwise the latest status line is captain-held -> blocked kind=question +# otherwise busy -> working +# otherwise the latest status line, when it is terminal: +# failed -> error +# done -> done ("PR ready" when the note has a URL) +# otherwise idle +# A paused declaration has no separate mapping; activity decides as above. +# Unknown activity proves nothing about the worker and nothing about who +# owns the root record, so it never triggers a heartbeat. Once the bridge +# has written a record of its own, though, the durable signals alone keep +# it honest: they fold the same way with idle activity and the result is +# written once per change - an open decision or standing captain hold +# still reads blocked, a fresh terminal result reads done/error, and +# anything else parks at idle - so a working/blocked record cannot +# outlive the signals that produced it. A done/failed line only counts +# when it was appended to the status file after the last reading that +# proved a turn was running; one already standing then belongs to an +# earlier phase and parks at idle while that turn is still in flight. The +# exit write drops that check: the agent is gone, so its done/error stands. +# arm's idle clear is the bridge's own first write, so the watcher it +# starts owns the root record from launch; +# a watcher with no write of its own stays silent instead: the root record +# may belong to a native writer. Harnesses whose activity only resolves +# busy or unknown (Rovo, Antigravity) thereby still reach idle, blocked, +# done, and error through the signals. +# +# Records go to the ROOT record (no id) with app=firstmate- and +# title=fm- so `hc get .status` and the app sidebar classify the worker; +# firstmate's own id=fm attention marks on Rex are a separate supervisor-side +# feature (bin/fm-rex-marks.sh) and compose unchanged. Rex's activity readers +# skip every app=firstmate-* record (bin/backends/rex.sh), so a record this +# bridge wrote never comes back as the activity it reads. The watcher re-emits +# only on change plus a heartbeat (records outlive resends, but a terminal +# that dropped one is repaired). Its lifetime is the agent incarnation: the +# launch runs `disarm` right after the agent command returns, and the watcher +# also exits when its recorded parent - the shell running the launch - dies. +# Either way a final emit keeps done/error and otherwise parks the record at +# idle - including when activity is unknown, provided the watcher has a +# prior write of its own; a TERM/HUP retires it the same way. A watcher can +# also be gone without that write - killed, or TERMed before its trap was +# installed - so retirement (disarm, or a relaunch's arm) always writes that +# same final record itself once the watcher is confirmed gone, before the +# pidfile goes, and a dead watcher's working/blocked record is retired even +# when the next launch arms nothing. One watcher per controlling terminal +# (/.osc7501watch-): every arm retires the pane's previous +# watcher before any gate, so a relaunch rebinds to the new agent and a +# natively reporting agent never shares the pane with a stale writer. A +# leftover pidfile marks a watcher that may have died without its final +# write and without disarm having run, so once the pane's terminal proves +# 7501-aware arm writes one idle record to clear it - on every relaunch, +# including off and native ones; panes that never armed get no write. When +# all gates pass, that write (or a fresh one when there was nothing to +# clear) is the new watcher's own first record. +# +# Safety: the reporter never reads terminal input or changes terminal modes. +# Eligibility uses passive environment markers only. Every record write is +# one small write() to the +# pane's tty in the output direction - the same channel fm-rex-marks.sh uses - +# so it can never be read as typed input. +# +# Subcommands: +# arm --task --harness --backend --kind --state +# --config [--target ] [--agent-bin ] +# detect + spawn watcher; exit 0 +# watch --task --kind --state --harness --parent +# [--backend --target ] [--owned 1] +# reporter loop; owned carries +# arm's successful write (internal) +# disarm --state retire this terminal's watcher +# +# Test seams: FM_OSC7501_TTY replaces /dev/tty as the output device (a +# regular file then captures exactly what a terminal would receive), and +# FM_OSC7501_INTERVAL/_HEARTBEAT/_PROBE_SECS tune the loop and version probe. +set -u + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +# shellcheck source=bin/fm-timeout-lib.sh +. "$SCRIPT_DIR/fm-timeout-lib.sh" + +FM_OSC7501_MSG_MAX=${FM_OSC7501_MSG_MAX:-120} +FM_OSC7501_TITLE_MAX=${FM_OSC7501_TITLE_MAX:-64} +FM_OSC7501_INTERVAL=${FM_OSC7501_INTERVAL:-2} +FM_OSC7501_HEARTBEAT=${FM_OSC7501_HEARTBEAT:-45} +FM_OSC7501_PROBE_SECS=${FM_OSC7501_PROBE_SECS:-3} +FM_OSC7501_APP_MAX=32 +FM_OSC7501_AUTH_RE='credential|login|log in|sign[ -]?in|auth|token|password|api key' +FM_OSC7501_CONFIG=osc7501-status +FM_OSC7501_UNKNOWN_PREFIX=.osc7501-gate- + +usage() { + { + echo "usage: fm-osc7501.sh arm --task --harness --backend --kind --state --config [--target ] [--agent-bin ]" + echo " fm-osc7501.sh watch --task --kind --state --harness --parent [--backend --target ]" + echo " fm-osc7501.sh disarm --state " + } >&2 + exit 2 +} + +warn() { printf 'warning: osc7501: %s\n' "$*" >&2; } + +task_ok() { # + case "$1" in '' | .* | *[!A-Za-z0-9._-]*) return 1 ;; esac + [ "${#1}" -le 64 ] +} + +# --- record formatting -------------------------------------------------------- + +osc_clean() { # -> one line, control characters as spaces + printf '%s' "$1" | tr '\000-\037\177' ' ' +} + +osc_b64() { # -> base64 on one line + printf '%s' "$1" | base64 | tr -d '\n' +} + +# osc_seq [kind] [title] [msg] -> one OSC 7501 root report. +# msg/title are clipped and base64-encoded. FM_OSC_APP must name the +# reporting program. +osc_seq() { + local f="state=$1" + case "${2:-}" in '') ;; *) f="$f:kind=$2" ;; esac + f="$f:app=$FM_OSC_APP" + case "${3:-}" in '') ;; *) + local t + t=$(osc_clean "${3:0:$FM_OSC7501_TITLE_MAX}") + f="$f:title=$(osc_b64 "$t")" ;; + esac + case "${4:-}" in '') ;; *) + local m + m=$(osc_clean "${4:0:$FM_OSC7501_MSG_MAX}") + f="$f:msg=$(osc_b64 "$m")" ;; + esac + printf '\033]7501;%s\033\134' "$f" +} + +# --- gates -------------------------------------------------------------------- + +# config gate: first non-comment word of config/osc7501-status; absent -> on. +# An unrecognized word warns once per value (marker under the state dir). +osc_gate() { # + local file="$1/$FM_OSC7501_CONFIG" line word marker + [ -f "$file" ] || return 0 + while IFS= read -r line || [ -n "$line" ]; do + line=${line#"${line%%[![:space:]]*}"} + case "$line" in '' | '#'*) continue ;; esac + word=$(printf '%s' "${line%%[[:space:]]*}" | tr '[:upper:]' '[:lower:]') + case "$word" in + on) return 0 ;; + off) return 1 ;; + esac + case "$word" in *[!a-z0-9_-]*) word=invalid ;; esac + marker="$2/$FM_OSC7501_UNKNOWN_PREFIX$word" + if [ ! -e "$marker" ]; then + warn "$file: unknown value; using off (on or off)" + : >"$marker" 2>/dev/null || true + fi + return 1 + done <"$file" + return 0 +} + +# known-aware environment: the pane runs inside a terminal whose 7501 support +# is established by its own marker; unknown terminals never report. +osc_env_aware() { + [ -n "${HC_TERMINAL_ID:-}" ] && return 0 + [ -n "${REX_BLOCK:-}${REX_SESSION:-}" ] && return 0 + [ "${TERM_PROGRAM:-}" = ghostty ] && return 0 + return 1 +} + +# --- native reporting --------------------------------------------------------- + +# osc_probe_version -> first x.y.z in ` --version`, bounded by +# FM_OSC7501_PROBE_SECS and run at every launch. +osc_probe_version() { # + local rp tmp ver + rp=$(command -v "$1" 2>/dev/null) || rp=$1 + case "$rp" in /*) ;; *) return 1 ;; esac + tmp=$(mktemp "${TMPDIR:-/tmp}/fm-osc7501.XXXXXX") || return 1 + if fm_run_timed "$FM_OSC7501_PROBE_SECS" "$rp" --version >"$tmp" 2>/dev/null; then + ver=$(grep -Eo '[0-9]+\.[0-9]+\.[0-9]+' "$tmp" 2>/dev/null | head -1) + else + ver= + fi + rm -f "$tmp" + [ -n "${ver:-}" ] || return 1 + printf '%s' "$ver" +} + +# osc_version_ge +osc_version_ge() { + local v1 v2 v3 f1 f2 f3 rest + IFS=. read -r v1 v2 v3 rest < : 0 when the launched agent reports +# OSC 7501 itself at the probed version (floors: the header's gate 5). A +# harness with a native floor reads as native when its probe fails. +osc_native() { + local floor ver + case "$1" in + claude) floor=2.1.295 ;; + pi | pi-signed) floor=1.1.0 ;; + codex) floor=0.163.0 ;; + grok) floor=1.0.67 ;; + muse) floor=1.4.11 ;; + *) return 1 ;; + esac + ver=$(osc_probe_version "$2") || return 0 + osc_version_ge "$ver" "$floor" +} + +# --- signal -> state ---------------------------------------------------------- + +LIBS_LOADED=0 +osc_load_libs() { + [ "$LIBS_LOADED" = 1 ] && return 0 + # shellcheck source=bin/fm-classify-lib.sh + . "$SCRIPT_DIR/fm-classify-lib.sh" || return 1 + # shellcheck source=bin/fm-busy-lib.sh + . "$SCRIPT_DIR/fm-busy-lib.sh" || return 1 + # shellcheck source=bin/fm-backend.sh + . "$SCRIPT_DIR/fm-backend.sh" || return 1 + LIBS_LOADED=1 +} + +# osc_activity -> busy or idle from fm_busy_classify; 1 when unknown. Backend +# and target let the classifier reach a harness whose only activity source is +# its rendered pane. +osc_activity() { # [backend] [target] + local act + act=$(fm_busy_classify "${4:-}" "${5:-}" "$1" "$2" "$3" 2>/dev/null) || return 1 + act=${act%% *} + case "$act" in busy | idle) printf '%s' "$act" ;; *) return 1 ;; esac +} + +# osc_mark -> its line count, the append position a later +# line must pass to be newer than this reading; 0 when absent or empty. +osc_mark() { + local n + n=$(grep -c '' "$1" 2>/dev/null) || n=${n:-0} + printf '%s' "${n:-0}" +} + +# osc_plan sets W_STATE, W_KIND, W_MSG from the task's signals and its +# activity. act=unknown folds the same signals as idle - no unproven working +# claim - except that a terminal line then proves the worker finished only +# when it was appended past , the status file's osc_mark taken +# just after the reading that last proved a turn was running: a done/failed +# standing by then can belong to an earlier phase while the turn that +# superseded it is still in flight. Append order, unlike a whole-second +# stamp, orders a line against a reading in the same second. 0 means no +# line preceded any busy reading. +osc_plan() { # [busy-mark] + local task=$1 kind=$2 state_dir=$3 act=$4 busy_mark=${5:-0} file line verb note held at + file="$state_dir/$task.status" + W_STATE= + W_KIND= + W_MSG= + held=${FM_CLASSIFY_CAPTAIN_HELD_VERB:-$FM_CLASSIFY_CAPTAIN_HELD_VERB_DEFAULT} + line=$(status_open_decisions "$file" "$kind" 2>/dev/null | tail -n 1) + verb=$(printf '%s' "$line" | cut -f2) + note=$(printf '%s' "$line" | cut -f3- | tr '\t\r\n' ' ') + case "$verb" in + needs-decision) + W_STATE=blocked + W_KIND=question + W_MSG="Needs your decision: $note" + return 0 + ;; + blocked) + W_STATE=blocked + W_KIND=permission + if printf '%s' "$note" | grep -Eiq "$FM_OSC7501_AUTH_RE"; then W_KIND=auth; fi + W_MSG="Blocked: $note" + return 0 + ;; + esac + line=$(last_status_line "$file" 2>/dev/null) + verb=$(status_line_verb "$line" 2>/dev/null) || verb= + note=$(status_line_note "$line" 2>/dev/null) || note= + note=$(printf '%s' "$note" | tr '\t\r\n' ' ') + if [ "$verb" = "$held" ]; then + W_STATE=blocked + W_KIND=question + W_MSG="Needs your decision: $note" + return 0 + fi + if [ "$act" = busy ]; then + W_STATE=working + return 0 + fi + case "$verb" in + failed | done) + if [ "$act" = unknown ] && [ "$busy_mark" -gt 0 ] 2>/dev/null; then + at=$(grep -nxF -- "$line" "$file" 2>/dev/null | tail -n 1 | cut -d: -f1) + [ "${at:-0}" -gt "$busy_mark" ] 2>/dev/null || { W_STATE=idle; return 0; } + fi + if [ "$verb" = failed ]; then + W_STATE=error + W_MSG="Failed: $note" + else + W_STATE='done' + case "$note" in + *https://*) W_MSG="PR ready: $note" ;; + *) W_MSG="Done: $note" ;; + esac + fi + return 0 + ;; + esac + W_STATE=idle +} + +# --- watcher ------------------------------------------------------------------ + +# final record before exit: the agent is gone, so no turn is in flight and +# its latest done/error is its outcome whatever activity last read; keep that +# result and park everything else at idle. Unknown activity does not silence +# it - its own working/blocked must never outlive the agent - but a watcher +# that never wrote stays silent, leaving a possible native owner of the root +# record untouched. +osc_final() { + if [ -z "${last:-}" ] \ + && ! osc_activity "$WATCH_HARNESS" "$WATCH_TASK" "$WATCH_STATE" "$WATCH_BACKEND" "$WATCH_TARGET" >/dev/null; then + return 0 + fi + osc_plan "$WATCH_TASK" "$WATCH_KIND" "$WATCH_STATE" idle + case "$W_STATE" in + done | error) ;; + *) W_STATE=idle W_KIND='' W_MSG='' ;; + esac + printf '%s' "$(osc_seq "$W_STATE" "$W_KIND" "$WATCH_TITLE" "$W_MSG")" >&3 2>/dev/null +} + +osc_watch() { # --task --kind --state --harness [--backend] [--target] [--owned 1] --parent + local opt + WATCH_TASK= + WATCH_KIND= + WATCH_STATE= + WATCH_HARNESS= + WATCH_BACKEND= + WATCH_TARGET= + WATCH_PARENT= + WATCH_OWNED= + while [ $# -gt 0 ]; do + opt=$1 + shift + case "$opt" in + --task) WATCH_TASK=$1 ;; + --kind) WATCH_KIND=$1 ;; + --state) WATCH_STATE=$1 ;; + --harness) WATCH_HARNESS=$1 ;; + --backend) WATCH_BACKEND=$1 ;; + --target) WATCH_TARGET=$1 ;; + --parent) WATCH_PARENT=$1 ;; + --owned) WATCH_OWNED=$1 ;; + *) usage ;; + esac + shift + done + task_ok "$WATCH_TASK" || exit 0 + case "$WATCH_PARENT" in '' | *[!0-9]*) exit 0 ;; esac + [ -d "$WATCH_STATE" ] || exit 0 + WATCH_KIND=${WATCH_KIND:-ship} + FM_OSC_APP="firstmate-$WATCH_HARNESS" + FM_OSC_APP="${FM_OSC_APP:0:$FM_OSC7501_APP_MAX}" + WATCH_TITLE="fm-$WATCH_TASK" + local dev=${FM_OSC7501_TTY:-/dev/tty} hb=0 hb_ticks last='' seq act busy_mark=0 + [ "$WATCH_OWNED" = 1 ] && last=$(osc_seq idle '' "$WATCH_TITLE" '') + exec 3>>"$dev" 2>/dev/null || exit 0 + osc_load_libs || exit 0 + trap 'osc_final; exit 0' TERM INT HUP + [ "${FM_OSC7501_INTERVAL:-1}" -ge 1 ] 2>/dev/null || FM_OSC7501_INTERVAL=1 + hb_ticks=$((FM_OSC7501_HEARTBEAT / FM_OSC7501_INTERVAL)) + [ "$hb_ticks" -ge 1 ] || hb_ticks=1 + while :; do + if ! kill -0 "$WATCH_PARENT" 2>/dev/null; then + osc_final + break + fi + if act=$(osc_activity "$WATCH_HARNESS" "$WATCH_TASK" "$WATCH_STATE" "$WATCH_BACKEND" "$WATCH_TARGET"); then + [ "$act" = busy ] && busy_mark=$(osc_mark "$WATCH_STATE/$WATCH_TASK.status") + osc_plan "$WATCH_TASK" "$WATCH_KIND" "$WATCH_STATE" "$act" + seq=$(osc_seq "$W_STATE" "$W_KIND" "$WATCH_TITLE" "$W_MSG") + if [ "$seq" != "$last" ]; then + printf '%s' "$seq" >&3 2>/dev/null + last=$seq + hb=0 + else + hb=$((hb + 1)) + if [ "$hb" -ge "$hb_ticks" ]; then + printf '%s' "$seq" >&3 2>/dev/null + hb=0 + fi + fi + else + hb=0 + # Unknown activity cannot prove the root record is still the bridge's, + # so there is no hold and no heartbeat. While the bridge's own last + # write stands, keep it honest: fold the durable signals alone (no + # unproven working claim) and write the result once per change, + # retiring whatever they no longer justify. A done/failed line still + # has to be appended after the last reading that proved a turn was + # running. + if [ -n "$last" ]; then + osc_plan "$WATCH_TASK" "$WATCH_KIND" "$WATCH_STATE" unknown "$busy_mark" + seq=$(osc_seq "$W_STATE" "$W_KIND" "$WATCH_TITLE" "$W_MSG") + if [ "$seq" != "$last" ]; then + printf '%s' "$seq" >&3 2>/dev/null + last=$seq + fi + fi + fi + sleep "$FM_OSC7501_INTERVAL" 2>/dev/null & + wait $! 2>/dev/null + done +} + +# --- arm / disarm ------------------------------------------------------------- + +# osc_tty_key -> pidfile key naming this process's controlling terminal (the +# device /dev/tty opens), or the FM_OSC7501_TTY stand-in; 1 without one. +osc_tty_key() { + local key + if [ -n "${FM_OSC7501_TTY:-}" ]; then + key=$FM_OSC7501_TTY + else + key=$(ps -o tty= -p "$$" 2>/dev/null) || return 1 + key=${key//[[:space:]]/} + fi + case "$key" in '' | *'?'*) return 1 ;; esac + key=${key#/dev/} + printf '%s' "${key//\//_}" +} + +# osc_gone : 0 once has exited, polling every 0.1s. +osc_gone() { + local i=0 + while kill -0 "$1" 2>/dev/null; do + [ "$i" -ge "$2" ] && return 1 + sleep 0.1 + i=$((i + 1)) + done +} + +# The final record retirement writes for a watcher it has confirmed gone - +# exactly osc_final's emit: the signals fold with idle activity, done/error +# survive, everything else parks at idle. The pidfile proves the record is +# the bridge's own. +osc_dead_final() { # + local wtask=$1 wkind=$2 wharness=$3 wstate=$4 dev + task_ok "$wtask" || return 0 + osc_load_libs || return 0 + FM_OSC_APP="firstmate-$wharness" + FM_OSC_APP="${FM_OSC_APP:0:$FM_OSC7501_APP_MAX}" + FM_OSC_APP="${FM_OSC_APP%-}" + osc_plan "$wtask" "${wkind:-ship}" "$wstate" idle + case "$W_STATE" in + done | error) ;; + *) W_STATE=idle W_KIND='' W_MSG='' ;; + esac + dev=${FM_OSC7501_TTY:-/dev/tty} + printf '%s' "$(osc_seq "$W_STATE" "$W_KIND" "fm-$wtask" "$W_MSG")" >>"$dev" 2>/dev/null || true +} + +# osc_retire : stop the watcher it records - TERM a live +# one, killing it when it is still busy past the grace so it can never write +# after the next owner - and once it is confirmed gone, by whatever route, +# write its final record and forget it. Whether the watcher managed its own +# final write is never inferred: a repeat of it is identical and harmless. +# Returns 1, keeping the pidfile, when the watcher survives. +osc_retire() { + local opid='' wtask='' wharness='' wkind='' + [ -f "$1" ] || return 0 + read -r opid wtask _ wharness wkind <"$1" 2>/dev/null || true + case "$opid" in '' | *[!0-9]*) opid='' ;; esac + if [ -n "$opid" ] && kill -0 "$opid" 2>/dev/null \ + && ps -o command= -p "$opid" 2>/dev/null | grep -q 'fm-osc7501\.sh'; then + kill "$opid" 2>/dev/null || true + if ! osc_gone "$opid" 50; then + kill -KILL "$opid" 2>/dev/null || true + osc_gone "$opid" 10 || return 1 + fi + fi + osc_dead_final "$wtask" "$wkind" "$wharness" "$2" + rm -f "$1" +} + +osc_disarm() { # --state + local opt state='' + while [ $# -gt 0 ]; do + opt=$1 + shift + case "$opt" in + --state) state=$1 ;; + *) usage ;; + esac + shift + done + [ -d "$state" ] || exit 0 + local key + key=$(osc_tty_key) || exit 0 + osc_retire "$state/.osc7501watch-$key" "$state" + exit 0 +} + +osc_arm() { # --task --harness --backend --kind --state --config [--target] [--agent-bin] + local opt task='' harness='' backend='' kind='' state='' config='' target='' agent_bin='' + while [ $# -gt 0 ]; do + opt=$1 + shift + case "$opt" in + --task) task=$1 ;; + --harness) harness=$1 ;; + --backend) backend=$1 ;; + --kind) kind=$1 ;; + --state) state=$1 ;; + --config) config=$1 ;; + --target) target=$1 ;; + --agent-bin) agent_bin=$1 ;; + *) usage ;; + esac + shift + done + task_ok "$task" || exit 0 + [ -d "$state" ] || exit 0 + local key pidfile + key=$(osc_tty_key) || exit 0 + pidfile="$state/.osc7501watch-$key" + # a leftover pidfile marks a watcher that may have died without its final + # write, leaving its record standing on this pane + local had_watcher='' + [ -f "$pidfile" ] && had_watcher=1 + osc_retire "$pidfile" "$state" || exit 0 + # a pane without a writable tty can never report + local dev=${FM_OSC7501_TTY:-/dev/tty} + [ -w "$dev" ] || exit 0 + case "$backend" in herdr | t3code) exit 0 ;; esac + osc_env_aware || exit 0 + FM_OSC_APP="firstmate-$harness" + FM_OSC_APP="${FM_OSC_APP:0:$FM_OSC7501_APP_MAX}" + # a leftover only a previous watcher could have written is the bridge's + # own to retire, whatever the new launch is - a fresh worker, a natively + # reporting agent, or reporting switched off. Panes that never armed stay + # untouched. + local owned='' + if [ -n "$had_watcher" ]; then + printf '%s' "$(osc_seq idle '' "fm-$task" '')" >>"$dev" 2>/dev/null && owned=1 + fi + osc_gate "$config" "$state" || exit 0 + # a natively reporting agent never sees a competing record + [ -n "$agent_bin" ] || exit 0 + osc_native "$harness" "$agent_bin" && exit 0 + # every gate passed, so this pane's reporting belongs to the bridge: this + # write is the one the new watcher inherits as its own first record + if [ -z "$owned" ]; then + printf '%s' "$(osc_seq idle '' "fm-$task" '')" >>"$dev" 2>/dev/null && owned=1 + fi + FM_OSC7501_WATCHER=1 nohup bash "$SCRIPT_DIR/fm-osc7501.sh" watch \ + --task "$task" --kind "$kind" --state "$state" --harness "$harness" \ + --backend "$backend" --target "$target" --owned "$owned" \ + --parent "$PPID" /dev/null 2>&1 & + printf '%s %s %s %s %s\n' "$!" "$task" "$$" "$harness" "$kind" >"$pidfile" 2>/dev/null || true + exit 0 +} + +case "${1:-}" in + -h | --help | help) + sed -n '2,/^set -u/p' "$0" | sed '$d; s/^# \{0,1\}//' + exit 0 ;; + arm) + shift + osc_arm "$@" ;; + watch) + shift + osc_watch "$@" ;; + disarm) + shift + osc_disarm "$@" ;; + *) usage ;; +esac diff --git a/bin/fm-pr-merge.sh b/bin/fm-pr-merge.sh index 2039896715d..fe0cab33f4d 100755 --- a/bin/fm-pr-merge.sh +++ b/bin/fm-pr-merge.sh @@ -13,8 +13,10 @@ # parse below owns why. # # Usage: -# fm-pr-merge.sh [--attended-override] [--allow-red ] -# [--allow-missing ] [--allow-no-pipeline] [-- ] +# fm-pr-merge.sh [--attended-override] [--update-branch] +# [--expected-head ] +# [--allow-red ] [--allow-missing ] [--allow-no-pipeline] +# [-- ] # fm-pr-merge.sh -h | --help # # When the caller passes none of --squash, --merge, --rebase, or --method after @@ -57,8 +59,15 @@ # Every failing condition is reported, not just the first. # The verified head is then passed to gh as # --match-head-commit, so a push that lands between that read and the merge -# fails the merge instead of landing commits nothing verified. Reading that -# state needs gh and jq, and either one absent stops the merge before any +# fails the merge instead of landing commits nothing verified. +# An optional --expected-head pins the commit the caller already +# reviewed. It must be one full lowercase hex commit of 40 or 64 characters, +# given once as its own argument. The initial live head must equal it or the +# request is refused before any forge write. The forge head match receives that same +# supplied value on GitHub, GitLab, and Forgejo. A later preflight in this run +# does not replace it with a newly read head. Omitting the flag keeps the +# previous behavior: the live head read at verification is the matched value. +# Reading that state needs gh and jq, and either one absent stops the merge before any # state is recorded. An attended --allow-red may be passed once, # with the name as a separate argument; it waives only checks with that exact # name, still requires every other check green, and still binds the head. Its @@ -78,11 +87,33 @@ # own view still proves a landed merge, and every outcome it cannot prove # refuses, reporting the failed gh read and naming both failed reads when the # gh-axi view could not prove the outcome either. -# A green PR that is BEHIND updates its branch whatever merge method applies. -# The update uses GitHub's merge-commit default, records the new head, then -# waits up to ten minutes for checks at that head, and for a transient UNKNOWN -# mergeable state to settle, and repeats this full preflight before the -# exact-head merge. A failed check still refuses. +# A source-branch update is only a substep of an already-authorized merge +# request: fm-pr-merge resolves the merge authority, passes the captain-hold +# check and full live preflight, then re-resolves authority under the merge +# locks before it updates. This helper has no standalone branch-update path; +# any refused pre-update guard stops before the update. +# Updating a green BEHIND GitHub PR requires explicit --update-branch consent on the +# authorized merge request; without it, refusal leaves the source branch alone. +# Consent includes retaining the update if checks on the new head later fail. +# With that opt-in, any merge method may update the branch. +# A supplied --expected-head remains pinned through the update: the source +# branch may advance under explicit consent, but merging that new head needs +# a new authorized request naming it as the expected head. +# The REST update is bound to the verified old head with expected_head_sha; +# a concurrent push refuses the update without changing that replacement head, +# and this request never retries against the newly observed head. +# An accepted update uses GitHub's merge-commit default. Under the merge and +# away locks, a shared polling budget (15 seconds by default) covers waiting for +# the new head to appear, recording it, and waiting for its checks and a transient +# UNKNOWN mergeable state to settle. Every poll repeats the full preflight, +# even before the updated head appears. A draft, closed PR, conflict, unreadable +# required-check source or producer, or any other non-transient refusal stops +# the wait immediately, even while checks are pending or missing. Any unwaived +# failed check also refuses immediately. These refusals release both locks and +# report the actual reason without advice to retry once checks pass. Only an +# unseen updated head, unsettled checks, or UNKNOWN mergeability are awaited. +# If that budget expires, the helper stops without merging and says to run +# the merge again once checks pass. # If the pull request remains open and the base branch has an effective # merge_queue rule, an attended refusal names the queue's configured merge # method and exact --attended-override -- --auto -- retry flags. While @@ -179,8 +210,9 @@ # # Extra args must not include --repo or -R in any form, including a bundled # short-option cluster such as -yR, because the repository comes only from the -# URL, nor --sha or --match-head-commit because the head comes only from the -# live read. An existing task-meta pr= must equal the requested canonical URL, +# URL, nor --sha or --match-head-commit. The head match is the live read, or +# the supplied --expected-head when the caller passed one. An existing task-meta +# pr= must equal the requested canonical URL, # unless that bound PR has already merged - proven by its recorded merge # notification - in which case the task's next PR is accepted so several PRs # from one task can each merge in turn; while the bound PR is still unmerged a @@ -192,7 +224,7 @@ # explicit captain instruction and never skips the live green check, the # away-record read, or a captain hold. # -# Usage: fm-pr-merge.sh [--attended-override] [--allow-red ] [--allow-missing ] [--allow-no-pipeline] [-- ] +# Usage: fm-pr-merge.sh [--attended-override] [--update-branch] [--expected-head ] [--allow-red ] [--allow-missing ] [--allow-no-pipeline] [-- ] # # On GitLab, this script confirms the MR is actually merged before reporting it; # an auto-merge-queued or unconfirmed request leaves the poll armed and records @@ -270,15 +302,25 @@ if [ "$PROVIDER" = gerrit ]; then fi shift 2 ATTENDED_OVERRIDE=false +UPDATE_BRANCH=false ALLOW_NO_PIPELINE=false ALLOW_RED=() ALLOW_MISSING=() +EXPECTED_HEAD= while [ "$#" -gt 0 ]; do case "$1" in --attended-override) ATTENDED_OVERRIDE=true shift ;; + --update-branch) + UPDATE_BRANCH=true + shift + ;; + --update-branch=*) + echo "error: --update-branch takes no value" >&2 + exit 2 + ;; --attended-override=*) echo "error: --attended-override takes no value" >&2 exit 2 @@ -311,10 +353,36 @@ while [ "$#" -gt 0 ]; do echo "error: --allow-no-pipeline takes no value" >&2 exit 2 ;; + --expected-head) + if [ -n "$EXPECTED_HEAD" ]; then + echo "error: --expected-head may be specified only once" >&2 + exit 2 + fi + case "${2:-}" in + ''|--*) + echo "error: --expected-head requires a full commit sha" >&2 + exit 2 + ;; + esac + EXPECTED_HEAD=$2 + shift 2 + ;; + --expected-head=*) + echo "error: --expected-head requires a separate full commit sha argument" >&2 + exit 2 + ;; --) shift; break ;; *) break ;; esac done +if [ "$UPDATE_BRANCH" = true ] && [ "$PROVIDER" != github ]; then + echo "error: --update-branch applies only to GitHub" >&2 + exit 2 +fi +if [ -n "$EXPECTED_HEAD" ] && ! fm_pr_head_valid "$EXPECTED_HEAD"; then + echo "error: --expected-head requires a full lowercase commit sha" >&2 + exit 2 +fi # --allow-red names one check run to waive, which only GitHub's per-check # reading can match. GitLab and Forgejo each collapse their checks into a # single head verdict - a head pipeline status and a combined commit status - @@ -588,6 +656,16 @@ fi # returns non-zero after reporting every condition that failed. FM_PR_MERGE_HEAD= FM_PR_GITLAB_ASYNC_CONFIGURED=false +FM_PR_EXPECTED_HEAD_REFUSAL= + +note_expected_head() { + if [ -n "$EXPECTED_HEAD" ] && [ "$1" != "$EXPECTED_HEAD" ]; then + FM_PR_EXPECTED_HEAD_REFUSAL=" - the live head is \"$1\", not the expected head $EXPECTED_HEAD" + else + FM_PR_EXPECTED_HEAD_REFUSAL= + fi +} + gitlab_verify_mergeable() { local json fields line pipeline_waived local total=0 named=0 refusals='' @@ -686,11 +764,18 @@ FIELDS " fi + note_expected_head "$live_head" + if [ -n "$FM_PR_EXPECTED_HEAD_REFUSAL" ]; then + refusals="$refusals$FM_PR_EXPECTED_HEAD_REFUSAL +" + fi + if [ -n "$refusals" ]; then printf 'error: refusing to merge %s\n' "$URL" >&2 printf '%s' "$refusals" >&2 return 1 fi + if [ "$pipeline_waived" = true ]; then printf 'verified: %s is open and mergeable at head %s, with no head pipeline, waived by --allow-no-pipeline\n' \ "$URL" "$live_head" >&2 @@ -826,6 +911,12 @@ FIELDS || refusals="$refusals - the combined commit status is \"$ci_state\", not success " + note_expected_head "$live_head" + if [ -n "$FM_PR_EXPECTED_HEAD_REFUSAL" ]; then + refusals="$refusals$FM_PR_EXPECTED_HEAD_REFUSAL +" + fi + if [ -n "$refusals" ]; then printf 'error: refusing to merge %s\n' "$URL" >&2 printf '%s' "$refusals" >&2 @@ -908,7 +999,7 @@ forgejo_default_merge_style() { # unnamed checks must not be treated as one. github_checks_not_green() { local json=$1 - printf '%s' "$json" | jq -r ' + printf '%s' "$json" | jq -r --argjson settled_only "${2:-false}" ' def settled_at: if type == "string" and test("^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$") then . else null end; @@ -917,6 +1008,9 @@ github_checks_not_green() { | to_entries[] | .key as $i | .value + | select($settled_only == false or + (if .__typename == "CheckRun" then .status == "COMPLETED" + else .state != "PENDING" and .state != "EXPECTED" end)) | if .__typename == "CheckRun" then { kind: "check_run", @@ -1048,11 +1142,13 @@ github_required_checks_missing() { # the usual 1, when mergeable=UNKNOWN is the only failing condition, so the # caller can retry a still-computing mergeability read instead of refusing. github_verify_mergeable() { - local json fields line red name covered missing unreported producers runs pending + local json fields line red failed name covered missing unreported producers runs pending local total=0 named=0 refusals='' mergeable_refusal='' local state='' draft='' mergeable='' merge_state='' live_head='' base='' FM_PR_GITHUB_BEHIND=false FM_PR_GITHUB_CHECKS_PENDING=false + FM_PR_GITHUB_CHECKS_FAILED=false + FM_PR_GITHUB_OTHER_REFUSAL=false FM_PR_GITHUB_UNREPORTED=false FM_PR_GITHUB_LIVE_HEAD= FM_PR_GITHUB_LIVE_BASE= @@ -1106,6 +1202,24 @@ FIELDS echo "error: could not read the GitHub pull request state before merging" >&2 return 1 fi + # Reuse current-run supersession when separating settled failures from waits. + if ! failed=$(github_checks_not_green "$json" true); then + echo "error: could not read the GitHub pull request checks before merging" >&2 + return 1 + fi + while IFS= read -r name; do + [ -n "$name" ] || continue + covered=0 + if [ "${#ALLOW_RED[@]}" -gt 0 ]; then + for check in "${ALLOW_RED[@]}"; do + [ "$check" != "$name" ] || covered=1 + done + fi + [ "$covered" -eq 0 ] || continue + FM_PR_GITHUB_CHECKS_FAILED=true + done </dev/null); then producers='[]' + FM_PR_GITHUB_OTHER_REFUSAL=true refusals="$refusals - required check producers at head $live_head could not be read " fi fi if ! missing=$(github_required_checks_missing "$json" "$FM_PR_GITHUB_REQUIRED" "$producers"); then + FM_PR_GITHUB_OTHER_REFUSAL=true refusals="$refusals - the GitHub pull request check rollup could not be read " else @@ -1185,6 +1309,13 @@ $missing EOF fi + note_expected_head "$live_head" + if [ -n "$FM_PR_EXPECTED_HEAD_REFUSAL" ]; then + FM_PR_GITHUB_OTHER_REFUSAL=true + refusals="$refusals$FM_PR_EXPECTED_HEAD_REFUSAL +" + fi + if [ -n "$mergeable_refusal" ]; then if [ -z "$refusals" ] && [ "$mergeable" = UNKNOWN ]; then return 3 @@ -1206,47 +1337,41 @@ EOF } github_update_behind_branch() { - local old_head=$FM_PR_MERGE_HEAD old_base=$FM_PR_GITHUB_BASE new_head attempt_error verify_status - local deadline=$((SECONDS + 600)) + local old_head=$FM_PR_MERGE_HEAD old_base=$FM_PR_GITHUB_BASE new_head='' attempt_error status + local poll=${mergeable_retry_delay:-3} deadline + deadline=$((SECONDS + poll * 5)) attempt_error=$(mktemp "${TMPDIR:-/tmp}/fm-pr-merge-update.XXXXXX") || return 1 - if ! gh pr update-branch "$PR_NUMBER" --repo "$PR_OWNER/$PR_REPO"; then + if ! gh api --method PUT "repos/$PR_OWNER/$PR_REPO/pulls/$PR_NUMBER/update-branch" \ + -f "expected_head_sha=$old_head"; then rm -f "$attempt_error" return 1 fi while :; do - new_head=$(gh pr view "$URL" --json headRefOid -q .headRefOid 2>/dev/null) || new_head= - if fm_pr_head_valid "$new_head" && [ "$new_head" != "$old_head" ]; then - break - fi - if [ "$SECONDS" -ge "$deadline" ]; then - echo "error: timed out verifying a new GitHub head after updating $URL" >&2 + status=0 + github_verify_mergeable 2> "$attempt_error" || status=$? + if [ -z "$FM_PR_GITHUB_LIVE_HEAD" ]; then + cat "$attempt_error" >&2 rm -f "$attempt_error" return 1 fi - sleep 5 - done - if ! record_pr_metadata || ! grep -qxF "pr_head=$new_head" "$META"; then - echo "error: could not record the updated GitHub head $new_head" >&2 - rm -f "$attempt_error" - return 1 - fi - while :; do - verify_status=0 - github_verify_mergeable 2> "$attempt_error" || verify_status=$? - if [ "$verify_status" -eq 0 ]; then - if [ "$FM_PR_MERGE_HEAD" = "$new_head" ] \ - && [ "$FM_PR_GITHUB_BASE" = "$old_base" ] \ - && [ "$FM_PR_GITHUB_BEHIND" = false ]; then - cat "$attempt_error" >&2 + if [ -z "$new_head" ] && [ "$FM_PR_GITHUB_LIVE_HEAD" != "$old_head" ]; then + new_head=$FM_PR_GITHUB_LIVE_HEAD + if ! record_pr_metadata || ! grep -qxF "pr_head=$new_head" "$META"; then + echo "error: could not record the updated GitHub head $new_head" >&2 rm -f "$attempt_error" - return 0 + return 1 fi - echo "error: the GitHub pull request head or base moved again after its branch update" >&2 + fi + if [ -n "$new_head" ] && [ "$FM_PR_GITHUB_LIVE_HEAD" != "$new_head" ]; then + echo "error: the GitHub pull request head moved again after its branch update" >&2 rm -f "$attempt_error" return 1 fi - if [ "$FM_PR_GITHUB_LIVE_HEAD" != "$new_head" ]; then - echo "error: the GitHub pull request head moved again after its branch update" >&2 + if [ "$status" -ne 0 ] && [ "$status" -ne 3 ] && { [ "$FM_PR_GITHUB_CHECKS_FAILED" = true ] \ + || [ "$FM_PR_GITHUB_OTHER_REFUSAL" = true ] || { + [ "$FM_PR_GITHUB_CHECKS_PENDING" != true ] \ + && [ "$FM_PR_GITHUB_UNREPORTED" != true ]; }; }; then + cat "$attempt_error" >&2 rm -f "$attempt_error" return 1 fi @@ -1255,20 +1380,27 @@ github_update_behind_branch() { rm -f "$attempt_error" return 1 fi - if [ "$FM_PR_GITHUB_CHECKS_PENDING" != true ] \ - && [ "$FM_PR_GITHUB_UNREPORTED" != true ] \ - && [ "$verify_status" -ne 3 ]; then - cat "$attempt_error" >&2 + if [ "$status" -eq 0 ] && [ -n "$new_head" ]; then + if [ "$FM_PR_GITHUB_BEHIND" = false ]; then + cat "$attempt_error" >&2 + rm -f "$attempt_error" + return 0 + fi + echo "error: the GitHub pull request head or base moved again after its branch update" >&2 rm -f "$attempt_error" return 1 fi if [ "$SECONDS" -ge "$deadline" ]; then cat "$attempt_error" >&2 - echo "error: timed out waiting for required checks on updated head $new_head" >&2 + if [ -z "$new_head" ]; then + echo "error: $URL was updated but its new head is not visible yet; run the merge again once its checks pass" >&2 + else + echo "error: $URL was updated to $new_head but its checks are not ready; run the merge again once its checks pass" >&2 + fi rm -f "$attempt_error" return 1 fi - sleep 5 + sleep "$poll" done } @@ -1928,6 +2060,10 @@ case "$PROVIDER" in [ "$away_status" -eq 0 ] || exit "$away_status" refuse_github_queue_while_away || exit 2 if [ "$FM_PR_GITHUB_BEHIND" = true ]; then + if [ "$UPDATE_BRANCH" != true ]; then + echo "error: $URL is behind its base; updating its source branch requires explicit --update-branch consent on this authorized merge request (the update remains if new checks prevent merging)" >&2 + exit 1 + fi github_update_behind_branch || exit 1 fi merge_status=0 diff --git a/bin/fm-procevent.sh b/bin/fm-procevent.sh index 77b8cabff81..3deeafa3362 100755 --- a/bin/fm-procevent.sh +++ b/bin/fm-procevent.sh @@ -1698,6 +1698,9 @@ cmd_owner_watchdog() { # + # Earlier publication or cleanup can outlast the lease. Refresh at the owning + # caller's launch boundary, still excluding runners and their descendants. + owner_lease_refresh isolate_runner detach "$1" } diff --git a/bin/fm-procinfo.py b/bin/fm-procinfo.py new file mode 100755 index 00000000000..22d195a34df --- /dev/null +++ b/bin/fm-procinfo.py @@ -0,0 +1,185 @@ +#!/usr/bin/env python3 +"""fm-procinfo.py - ps-free process facts for bin/fm-session-lock-lib.sh. + +Prints one line: TABTAB for the pid given as argv[1], +using only sysctl(3) - the same kernel state `ps` reads, just without +executing /bin/ps. It exists because an EDR-style policy can deny the +ps binary's exec outright (even `ps -V`), and a session lock whose +identity walk needs ps can then never be acquired at all. + +Identity keeps ps's exact shape on macOS: comm is argv[0] and args is +the full argv joined with spaces, both taken from the kernel's per- +process argv region - the same region ps itself reports. That is a +correctness requirement, not a preference: the session lock compares a +process's identity across routes, and any process ps calls a verified +harness while this route calls it a non-harness turns a foreign +owner's fail-closed refusal into a lock takeover. A resolved exec path +(libproc proc_pidpath) is deliberately NOT reported - it is a +different fact that disagrees with ps for renamed, symlinked, or +argv[0]-wrapping harnesses, which is precisely the population the +denied-ps hosts are expected to run. + +Fail-closed by design: every read is verified against the requested pid +before anything is printed, and an argv that cannot be read to exactly +its declared length yields no identity rather than a partial one. No +pid, no verification, no output - exit 1. + +Usage: fm-procinfo.py +""" +import ctypes +import ctypes.util +import os +import struct +import sys + +CTL_KERN = 1 +KERN_PROC = 14 # CTL_KERN, KERN_PROC, KERN_PROC_PID: one kinfo_proc +KERN_PROC_PID = 1 +KERN_PROCARGS2 = 49 # CTL_KERN, KERN_PROCARGS2, pid: exec path + argv + env + +# kinfo_proc LP64 offsets: p_pid lives in kp_proc, the real parent pid in +# kp_eproc.e_ppid. These are assumptions, so _self_kinfo() proves them +# against this very process (os.getpid() / os.getppid()) before they are +# ever used on a requested pid, and a mismatch ends the run instead of +# reporting a guess. +P_PID_OFF = 40 +E_PPID_OFF = 560 + + +def _libc(): + return ctypes.CDLL(ctypes.util.find_library("c") or "libc.dylib", + use_errno=True) + + +_libc_cache = None + + +def libc(): + global _libc_cache + if _libc_cache is None: + _libc_cache = _libc() + return _libc_cache + + +def _sysctl(mib): + """Run a sysctl(3) mib, return the raw buffer, or None if it fails.""" + mib_arr = (ctypes.c_int * len(mib))(*mib) + size = ctypes.c_size_t(0) + ret = libc().sysctl(mib_arr, len(mib), None, ctypes.byref(size), None, 0) + if ret != 0 or size.value == 0: + return None + buf = ctypes.create_string_buffer(size.value) + ret = libc().sysctl(mib_arr, len(mib), buf, ctypes.byref(size), None, 0) + if ret != 0: + return None + return buf.raw[: size.value] + + +def _self_kinfo(): + """Return this process's own kinfo_proc, only if both offsets verify. + + The buffer read for os.getpid() must carry os.getpid() at P_PID_OFF and + os.getppid() at E_PPID_OFF. If either read disagrees, the layout + assumption is wrong on this system and every ppid_of() call that uses + these offsets returns None instead. + """ + buf = _sysctl((CTL_KERN, KERN_PROC, KERN_PROC_PID, os.getpid())) + if buf is None or len(buf) < E_PPID_OFF + 4: + return None + if struct.unpack_from(" 4096: + return None + strings = buf[4:].split(b"\0") + if not strings or not strings[0]: + return None + argv = [] + collecting = False + for raw in strings[1:]: + if not collecting: + if not raw: + continue # padding between the exec path and argv[0] + collecting = True + argv.append(raw) + if len(argv) == argc: + return argv + return None + + +def _ps_visible(raw): + result = [] + for byte in raw: + if byte >= 128: + result.append("M-") + byte -= 128 + if byte < 32 or byte == 127: + result.append("^" + chr(byte ^ 64)) + else: + result.append(chr(byte)) + return "".join(result) + + +def main(): + if len(sys.argv) != 2 or not sys.argv[1].isdigit(): + print("usage: fm-procinfo.py ", file=sys.stderr) + return 2 + pid = int(sys.argv[1]) + verified = _self_kinfo() + ppid = ppid_of(pid, verified) + if ppid is None: + return 1 + argv = _procargs(pid) + if not argv or not argv[0]: + # No argv means no identity. ps gets its comm/args from this + # same region, so a process unreadable here is unreadable + # there - substituting a resolved path from elsewhere would + # hand the two routes different facts about one process. + return 1 + comm = _ps_visible(argv[0]) + while argv and not argv[-1]: + argv.pop() + args = _ps_visible(b" ".join(argv)) + sys.stdout.write("%d\t%s\t%s\n" % (ppid, comm, args)) + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/bin/fm-record-export.py b/bin/fm-record-export.py index 0d85c4baaa8..b010a66e0aa 100644 --- a/bin/fm-record-export.py +++ b/bin/fm-record-export.py @@ -465,6 +465,8 @@ def collect_state_sources(state_fd: int) -> list[Source]: add_source(sources, state_fd, state_fd, "state/fleet-ledger.jsonl") names = sorted(os.listdir(state_fd)) for name in names: + if name.startswith("."): + continue if name.endswith(".meta"): task = name[: -len(".meta")] safe_component(task, "task metadata name") @@ -1160,16 +1162,20 @@ def sync_records(home: str, config: str, data: str, state: str) -> None: os.close(data_fd) +def env_or(name: str, default: str) -> str: + return os.environ.get(name) or default + + def main(argv: list[str]) -> int: if not argv or argv[0] not in {"sync", "archive-task"}: print("usage: fm-record-export.py sync | archive-task ", file=sys.stderr) return 2 script_dir = os.path.dirname(os.path.realpath(__file__)) - root = os.environ.get("FM_ROOT_OVERRIDE", os.path.dirname(script_dir)) - home = os.environ.get("FM_HOME", root) - config = os.environ.get("FM_CONFIG_OVERRIDE", os.path.join(home, "config")) - data = os.environ.get("FM_DATA_OVERRIDE", os.path.join(home, "data")) - state = os.environ.get("FM_STATE_OVERRIDE", os.path.join(home, "state")) + root = env_or("FM_ROOT_OVERRIDE", os.path.dirname(script_dir)) + home = env_or("FM_HOME", root) + config = env_or("FM_CONFIG_OVERRIDE", os.path.join(home, "config")) + data = env_or("FM_DATA_OVERRIDE", os.path.join(home, "data")) + state = env_or("FM_STATE_OVERRIDE", os.path.join(home, "state")) if argv == ["sync"]: sync_records(home, config, data, state) elif len(argv) == 2 and argv[0] == "archive-task": diff --git a/bin/fm-remote-inherit-push.sh b/bin/fm-remote-inherit-push.sh index c3979ce9faf..fec8438b486 100755 --- a/bin/fm-remote-inherit-push.sh +++ b/bin/fm-remote-inherit-push.sh @@ -12,6 +12,12 @@ # snapshotted locally before any is sent; the sends then overlap up to # FM_REMOTE_INHERIT_PUSH_JOBS (default 4, 1 for strictly one at a time), and # their output is replayed in allowlist order. +# The caller holds the sender transaction lock. Before sending, read the +# receiver's committed high-water mark and durably advance a lost or restored +# sender counter beyond it. For older receivers, a query usage refusal (exit 2) +# uses floor zero and leaves write-time generation checks intact; counter +# recovery requires a receiver that supports the query. Other query failures +# and malformed replies fail closed. set -eu SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" @@ -19,11 +25,14 @@ FM_ROOT="${FM_ROOT_OVERRIDE:-$(cd "$SCRIPT_DIR/.." && pwd)}" FM_HOME="${FM_HOME:-${FM_ROOT_OVERRIDE:-$FM_ROOT}}" CONFIG="${FM_CONFIG_OVERRIDE:-$FM_HOME/config}" DATA="${FM_DATA_OVERRIDE:-$FM_HOME/data}" +STATE="${FM_STATE_OVERRIDE:-$FM_HOME/state}" # shellcheck source=bin/fm-secondmate-registry-lib.sh . "$SCRIPT_DIR/fm-secondmate-registry-lib.sh" # shellcheck source=bin/fm-config-inherit-lib.sh . "$SCRIPT_DIR/fm-config-inherit-lib.sh" +# shellcheck source=bin/fm-secondmate-nudge-lib.sh +. "$SCRIPT_DIR/fm-secondmate-nudge-lib.sh" die() { printf 'error: %s\n' "$1" >&2; exit 1; } file_link_count() { @@ -96,6 +105,21 @@ done < < stdin # fm-remote-inherit.sh absent 0 +# fm-remote-inherit.sh generation # # Only the inherited-material allowlist is writable or removable. Writes are # atomic ordinary-file replacements. data/captain-shared.md is read-only and is # quarantined before removal or before replacing bytes not last published here. +# The generation query prints the maximum committed generation across this code +# root's allowlist, or zero when no records exist. It locks and validates each +# record and refuses unsafe, unreadable, or malformed state. set -eu FM_HOME=${FM_HOME:?FM_HOME is required} @@ -40,6 +44,62 @@ EOF return 1 } +# Read the committed record under its item lock. Both the read-only high-water +# query and the write path use this validation, so corrupt state cannot reset +# the recovery floor. Variables belong to the calling function's local scope. +read_generation_record() { + existing_generation=0 + if [ -e "$GENERATION_FILE" ] || [ -L "$GENERATION_FILE" ]; then + [ -f "$GENERATION_FILE" ] && [ ! -L "$GENERATION_FILE" ] \ + && [ -r "$GENERATION_FILE" ] || die "inheritance generation record is unsafe or unreadable" + { + IFS= read -r existing_generation \ + && IFS= read -r existing_bytes \ + && IFS= read -r existing_hash \ + && IFS= read -r existing_command \ + && { ! IFS= read -r extra && [ -z "$extra" ]; } + } < "$GENERATION_FILE" || die "inheritance generation record is malformed" + case "$existing_generation" in ''|*[!0-9]*) die "inheritance generation record is malformed" ;; esac + [ "${#existing_generation}" -le 18 ] && [ "$existing_generation" -ge 1 ] \ + || die "inheritance generation record is malformed" + case "$existing_bytes" in ''|*[!0-9]*) die "inheritance generation record is malformed" ;; esac + case "$existing_hash" in ''|*[!A-Fa-f0-9]*) die "inheritance generation record is malformed" ;; esac + [ "${#existing_hash}" -eq 64 ] || die "inheritance generation record is malformed" + case "$existing_command" in put|absent) ;; *) die "inheritance generation record is malformed" ;; esac + fi +} + +generation_max() { + local home parent rel value maximum=0 + local existing_generation existing_bytes existing_hash existing_command extra + home=$(CDPATH='' cd -- "$FM_HOME" 2>/dev/null && pwd -P) || die "FM_HOME is unavailable" + while IFS= read -r rel; do + parent="$home/${rel%/*}" + [ ! -L "$parent" ] || die "inherited destination parent is a symlink" + if [ ! -d "$parent" ]; then + [ ! -e "$parent" ] || die "inherited destination parent is not a directory" + continue + fi + value=$( + LOCK="$parent/.fm-inherit-${rel##*/}.lock" + GENERATION_FILE="$parent/.fm-inherit-${rel##*/}.generation" + fm_lock_acquire_wait "$LOCK" || die "cannot lock inherited destination" + trap 'fm_lock_release "$LOCK" || true' EXIT + read_generation_record + printf '%s\n' "$existing_generation" + ) || return 1 + [ "$value" -le "$maximum" ] || maximum=$value + done < printf '%s/.remote-inherit-%s.lock\n' "$state" "$id" } -fm_remote_inherit_generation_next() { # - local state=$1 id=$2 path current next tmp +fm_remote_inherit_generation_next() { # [committed-remote-floor] + # The caller holds fm_remote_inherit_transaction_lock_path across allocation + # and transport. Recovery advances beyond both durable sequence owners. + local state=$1 id=$2 floor=${3:-0} path current next tmp extra case "$id" in *[!/A-Za-z0-9._-]*|''|*/*) return 1 ;; esac [ -d "$state" ] && [ ! -L "$state" ] || return 1 path="$state/.remote-inherit-$id.generation" current=0 if [ -e "$path" ] || [ -L "$path" ]; then [ -f "$path" ] && [ ! -L "$path" ] || return 1 - IFS= read -r current < "$path" || return 1 + { + IFS= read -r current && { ! IFS= read -r extra && [ -z "$extra" ]; } + } < "$path" || return 1 case "$current" in ''|*[!0-9]*) return 1 ;; esac - [ "${#current}" -le 17 ] || return 1 + [ "${#current}" -le 18 ] || return 1 fi + case "$floor" in ''|*[!0-9]*) return 1 ;; esac + [ "${#floor}" -le 18 ] || return 1 + current=$((10#$current)) + floor=$((10#$floor)) + [ "$floor" -le "$current" ] || current=$floor + [ "$current" -lt 999999999999999999 ] || return 1 next=$((current + 1)) tmp=$(umask 077; mktemp "$state/.remote-inherit-generation.XXXXXX") || return 1 printf '%s\n' "$next" > "$tmp" || { rm -f -- "$tmp"; return 1; } diff --git a/bin/fm-session-lock-lib.sh b/bin/fm-session-lock-lib.sh index cdf95c3d80e..b97491d02fa 100644 --- a/bin/fm-session-lock-lib.sh +++ b/bin/fm-session-lock-lib.sh @@ -11,6 +11,9 @@ # Claude session id below matches the id recorded beside a live lock. Neither # signal ever fails open: no id, no sidecar, an untrusted id, or a different # recorded id leaves the ancestry verdict exactly as it was. +# A second decision is owned here too: fm_session_lock_pid_blocks_reclaim +# answers whether a recorded owner may be reclaimed, so recovery callers +# delegate that question instead of re-deriving liveness from the fields. # This file is sourced by scripts and has no side effects on source. # Cursor process identity is NOT expressible as a command-name pattern and is @@ -272,6 +275,284 @@ fm_win_harness_ancestry_pids() { return 1 } +# --- one field reader, ps primary, ps-free route as fallback ----------------- +# Every identity read of a live process in THIS FILE (comm, args, ppid) +# goes through fm_proc_info below, so the denied-ps fallback has exactly +# one place to apply here. The verdict rules never change: the fallback +# changes only which route the same kernel facts arrive by, and both +# routes report the same fields - macOS identity comes from the argv +# region because that is what `ps -o comm=` and `ps -o args=` report +# there, and a route that read a different fact would let the two +# routes hand one process two different verdicts. +# +# This guarantee is per-file on purpose. The sibling walkers in +# bin/fm-harness.sh, bin/fm-backend.sh, bin/fm-sessionstart-nudge.sh, +# and bin/fm-branch-outcome.sh own their own ancestry reads and still +# require an executable ps; a denied-ps host stays degraded there +# until they route through a shared provider (follow-up). + +# One stderr line per shell that runs identity reads when the denied-ps +# route cannot answer. AGENTS.md session-start contract: a lock refusal +# owes its exact diagnostic - but a 16-hop walk over a dead provider +# must not print 48 lines, so the first cause wins and the rest stay +# silent in that shell. The provider is read through a command +# substitution, so a flag it sets dies with that subshell; fm_proc_info +# spends this guard in the shell that owns the walk, which is the only +# place it can persist. A walk that itself runs inside $(...) therefore +# keeps a private guard, so one run whose reads span several subshells +# can report the same cause once per such shell. +FM_OS_PROC_DIAG_SHOWN='' +fm_os_proc_diag_once() { # + if [ -z "$FM_OS_PROC_DIAG_SHOWN" ]; then + FM_OS_PROC_DIAG_SHOWN=1 + printf 'fm-session-lock: %s\n' "$1" >&2 + fi + return 0 +} + +# Parse one Linux "/proc//stat" line ($1, format " () +# ...") plus its already-NUL-flattened cmdline ($2) into +# "TABTAB". Pure seam: a saved stat line drives it +# identically on any host, which is why the parsing lives here. +fm_linux_stat_triple() { # + local line=$1 args=$2 rest comm ppid + # stat_fields, like the /proc//stat readers in + # bin/fm-remote-job-lib.sh and bin/fm-teardown.sh: ShellCheck follows + # sources, and bin/backends/rex.sh assigns a string to `fields`, so an + # array of that name here trips SC2178/SC2128 over there. + case $line in + *"("*") "*) ;; + *) return 1 ;; + esac + # comm may contain spaces and parentheses, so cut the rest off the + # LAST ") ", not the first. TAB/CR/LF are the record's delimiters, + # so flatten them out of the field the kernel lets name anything. + # Only C0 controls and DEL become '?': procps preserves valid UTF-8, + # and a live owner installed under a non-ASCII path (for example + # /home/José/.local/bin/agent) must keep the bytes ps reports, or its + # identity stops matching and the lock becomes reclaimable. + comm=${line#*(} + comm=${comm%*) *} + comm=$(printf '%s' "$comm" | LC_ALL=C tr '\012' ' ' | LC_ALL=C tr -c '\040-\176\200-\377' '?') + rest=${line##*") "} + local -a stat_fields + IFS=' ' read -r -a stat_fields <<< "$rest" + ppid=${stat_fields[1]:-} + case $ppid in + '' | *[!0-9]*) return 1 ;; + esac + printf '%s\t%s\t%s\n' "$ppid" "$comm" "$args" +} + +# OS provider for one pid: prints "TABTAB", or returns 1. +# Called only when the ps binary itself cannot execute. Tests override this +# whole function to drive a deterministic table. +fm_os_proc_triple() { # + local proc_root stat_line args element count rc=0 + local -a argv + case "$(uname -s 2>/dev/null)" in + Linux) + # The repo's other /proc readers (bin/fm-cursor-lib.sh, + # bin/fm-wake-lib.sh, bin/fm-teardown.sh) all take + # FM_PROC_ROOT_OVERRIDE so a fixture tree can drive them; this + # reader honors the same hook for the same reason. + proc_root=${FM_PROC_ROOT_OVERRIDE:-/proc} + [ -r "$proc_root/$1/stat" ] || return 1 + stat_line=$(cat "$proc_root/$1/stat" 2>/dev/null) || return 1 + [ -r "$proc_root/$1/cmdline" ] || return 1 + args=$( + argv=() + count=0 + while IFS= read -r -d '' element; do + argv[count]=$element + count=$((count + 1)) + done < "$proc_root/$1/cmdline" || exit 1 + while [ "$count" -gt 0 ] && [ -z "${argv[$((count - 1))]}" ]; do + unset 'argv[count - 1]' + count=$((count - 1)) + done + if [ "$count" -gt 0 ]; then + IFS=' ' + # Same byte rule as fm_linux_stat_triple: flatten only the + # record's control delimiters, keep the bytes ps preserves. + printf '%s' "${argv[*]}" | LC_ALL=C tr '\012' ' ' | LC_ALL=C tr -c '\040-\176\200-\377' '?' + fi + ) || return 1 + fm_linux_stat_triple "$stat_line" "$args" + ;; + Darwin) + # bin/fm-procinfo.py reads ppid through a self-verified + # KERN_PROC_PID chain and identity (comm/args) through the + # KERN_PROCARGS2 argv region - the same region ps reports, which + # is what keeps the two routes one fact per field. python3 is an + # optional toolchain member (Herdr ordering already depends on + # it); without it the denied-ps route degrades to today's + # fail-closed refusal rather than to a guess. + if ! command -v python3 >/dev/null 2>&1; then + fm_os_proc_diag_once 'ps cannot execute and no python3 is available for the ps-free identity provider; identity reads fail closed' + return 1 + fi + python3 "$(dirname -- "${BASH_SOURCE[0]}")/fm-procinfo.py" "$1" 2>/dev/null || rc=$? + if [ "$rc" -ne 0 ]; then + fm_os_proc_diag_once "ps cannot execute and the ps-free identity provider exited $rc; identity reads fail closed. Reproduce with: python3 $(dirname -- "${BASH_SOURCE[0]}")/fm-procinfo.py $1" + fi + return "$rc" + ;; + *) + return 1 + ;; + esac +} + +# Confirm once per process run that the ps BINARY cannot execute, the +# only environment this fallback exists for. A lone call's 126/127 can +# also come from a sandbox acting on that single call, and an actual +# binary block refuses every call including `ps -V`; only the second +# justifies routing identity off ps. The probe result is cached, so a +# refused walk of 16 pids costs one probe. (Busybox-era ps -V failures +# would read as "ps works" and keep the stop - the price of requiring +# positive proof of the denial.) +_fm_ps_exec_denied() { + local rc=0 + if [ -n "${_FM_PS_EXEC_DENIED+x}" ]; then + if [ "$_FM_PS_EXEC_DENIED" = 1 ]; then + return 0 + fi + return 1 + fi + ps -V >/dev/null 2>&1 || rc=$? + case $rc in + 126 | 127) _FM_PS_EXEC_DENIED=1 ;; + *) _FM_PS_EXEC_DENIED=0 ;; + esac + if [ "$_FM_PS_EXEC_DENIED" = 1 ]; then + return 0 + fi + return 1 +} + +# Read the identity fields of pid $1 into FM_PROC_COMM, FM_PROC_ARGS, and +# FM_PROC_PPID. +# +# ps stays primary everywhere it runs: while it executes, its output is +# used verbatim, so behavior is byte-identical to every session that ever +# worked. Only a ps that cannot execute at all - bash exits 126 for a +# refused exec, 127 for a missing binary - routes one read to the OS provider. +# Status 1 is platform-specific: Linux/macOS mean ps ran and named nothing; +# on Cygwin-family hosts it means ps refused the unsupported -o option and the +# fixed-column readers below are used instead. This exists because an +# EDR-style policy can deny the ps binary's exec itself (observed: +# /bin/ps refused even for `ps -V`, while kill, head, sysctl, pgrep, and +# lsof all ran) while every fact the identity walk needs remains readable +# through /proc or the kernel syscalls. +# +# Returning 1 means "no route can read this pid", which stops a walk +# exactly as a dead pid always has: nothing here ever fabricates a field. +fm_proc_info() { # + local pid=$1 rc=0 out comm args ppid + FM_PROC_COMM='' FM_PROC_ARGS='' FM_PROC_PPID='' + out=$(ps -o comm= -p "$pid" 2>/dev/null) || rc=$? + case $rc in + 0) ;; + 1) + # Linux/macOS: ps ran and named nothing. Cygwin-family: ps refused + # -o, so use its fixed-column readers only on that platform route. + fm_win_boundary_applies || return 1 + comm=$(fm_ps_comm "$pid") || return 1 + args=$(fm_ps_args "$pid") || return 1 + ppid=$(fm_ps_ppid "$pid") || return 1 + case "$ppid" in ''|*[!0-9]*) return 1 ;; esac + FM_PROC_COMM=$comm FM_PROC_ARGS=$args FM_PROC_PPID=$ppid + return 0 + ;; + 126 | 127) + # Confirmed, once per process: only a ps binary that cannot + # execute at all justifies the second provider. + _fm_ps_exec_denied || return 1 + local triple f1 f2 f3 rest tab + tab=$'\t' + # Spend the diagnostic guard for this shell here, not in the + # command-substitution subshell that prints it: a cause line lives + # only on the provider's failure routes, so a failed read is exactly + # where that budget is used up in the shell the walk runs in. + triple=$(fm_os_proc_triple "$pid") || { + FM_OS_PROC_DIAG_SHOWN=1 + return 1 + } + # The provider record is one line with exactly three TAB-separated + # fields: . The field count is proved by + # rebuilding the record from the parsed fields and comparing it to + # the original, because a TAB is IFS whitespace: a record with a + # MISSING field would otherwise parse exactly like a record whose + # field is legitimately empty, and a truncated record would be + # half-parsed into a plausible but wrong identity. + # + # A comm or args field that is legitimately empty is a real answer + # and is reported as one. A live process whose comm was emptied, and + # a kernel thread with no cmdline, are both PRESENT processes; the + # routes must agree about that, because a present pid that reads as + # gone is a reclaimable lock. + case $triple in + *$'\n'*) return 1 ;; + esac + # Split on literal TABs, then compare the rebuilt record literally. + f1=${triple%%"$tab"*} + rest=${triple#*"$tab"} + f2=${rest%%"$tab"*} + f3=${rest#*"$tab"} + case $f3 in + *"$tab"*) return 1 ;; + esac + if [ "$triple" = "$f1$tab$f2$tab$f3" ]; then + : + else + return 1 + fi + case $f1 in + '' | *[!0-9]*) return 1 ;; + esac + FM_PROC_PPID=$f1 FM_PROC_COMM=$f2 FM_PROC_ARGS=$f3 + return 0 + ;; + *) + # 126/127 above are the statuses that prove "ps could not + # execute at all". Anything else, including signal-killed 137, + # means ps gave no answer, and callers read + # a non-zero exactly as "this pid cannot be verified". + return 1 + ;; + esac + # ps ran and named nothing. That is a real answer about a real + # process - a live process whose comm was emptied is still a live + # process - so it must NOT read as "the pid is gone". The callers + # below each need this distinction: the ancestry walk stops either + # way, but fm_session_lock_inspect separates "gone" (stale) from + # "present but not classifiable" (unknown), and collapsing the two + # here would report a live, unreadable pid as reclaimable and let a + # fresh process take over a lock whose owner is still running. + # + # The empty comm is still reported, not invented: FM_PROC_COMM stays + # empty and the matcher below rejects it, exactly as it rejects a + # comm it does not recognise. + comm=$out + args=$(ps -o args= -p "$pid" 2>/dev/null) || return 1 + # The ppid is a walk input, not an identity field. The identity the + # matcher reads is comm and args, and both of those are fail-closed + # above; a ppid that cannot be read only ends the ancestry walk one + # hop early, which is the conservative direction - the walk finds no + # harness and the session refuses the lock rather than taking one it + # cannot prove. + # + # The base read the ppid the same tolerant way, and the repository's + # grok and kimi harness suites pin that: their stubbed ps answers + # comm= and args= and refuses every other question, so a ppid read + # that failed had to leave the identity intact. + ppid=$(ps -o ppid= -p "$pid" 2>/dev/null | tr -d ' ') + case "$ppid" in ''|*[!0-9]*) ppid='' ;; esac + FM_PROC_COMM=$comm FM_PROC_ARGS=$args FM_PROC_PPID=$ppid + return 0 +} + # Walk the current process ancestry (up to 16 hops) and print this session's # contiguous verified-harness ancestry, innermost pid first. # @@ -291,11 +572,10 @@ fm_win_harness_ancestry_pids() { # session cannot be read off the ancestry at all, so the whole contiguous run is # reported and the callers below decide what they need from it. fm_harness_ancestry_pids() { - local pid=$$ comm args extending=0 printed=0 + local pid=$$ extending=0 printed=0 for _ in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16; do - comm=$(fm_ps_comm "$pid") || break - args=$(fm_ps_args "$pid") - if fm_harness_process_matches "$comm" "$args"; then + fm_proc_info "$pid" || break + if fm_harness_process_matches "$FM_PROC_COMM" "$FM_PROC_ARGS"; then printf '%s\n' "$pid" printed=1 [ "$FM_HARNESS_IS_CLAUDE" -eq 1 ] || break @@ -303,7 +583,7 @@ fm_harness_ancestry_pids() { elif [ "$extending" -eq 1 ]; then break fi - pid=$(fm_ps_ppid "$pid") + pid=$FM_PROC_PPID # Examine the top of the chain before stopping. Inside a PID namespace the # harness itself is pid 1, so stopping as soon as the next pid is 1 hides the # very process this walk exists to find. A host's real pid 1 (init, systemd, @@ -356,21 +636,63 @@ EOF return 1 } +# Classify session-lock pid $1, setting FM_SESSION_LOCK_PID_VERDICT to one of: +# readable - the identity was read into FM_PROC_COMM and FM_PROC_ARGS, plus +# FM_PROC_PPID for a numeric pid (a tagged pid has no local parent). +# dead - nothing can see the pid, or the Windows table proves it absent. +# unknown - the process exists but this session cannot signal it. +# unreadable - it may well be alive but its identity could not be read; also +# the value left for a local pid that never gets classified +# (empty, non-numeric, or the literal zero). +# Only "readable" answers an identity question; the other three carry no +# identity, and the two rules below own what each one means for ownership and +# for recovery: fm_harness_pid_alive and fm_session_lock_pid_blocks_reclaim. +fm_session_lock_pid_verdict() { + local pid=$1 winpid rc=0 comm + FM_SESSION_LOCK_PID_VERDICT=unreadable + if winpid=$(fm_win_untag_pid "$pid"); then + comm=$(fm_win_command "$winpid") || rc=$? + case $rc in + 0) FM_PROC_COMM=$comm FM_PROC_ARGS=$comm FM_PROC_PPID=; FM_SESSION_LOCK_PID_VERDICT=readable ;; + 1) FM_SESSION_LOCK_PID_VERDICT=dead ;; + esac + return 0 + fi + case "$pid" in ''|*[!0-9]*|0) return 0 ;; esac + # Liveness first, identity second. fm_session_lock_pid_blocks_reclaim + # owns how an unsignalable or unreadable owner affects recovery. + # Asking the identity + # first would let a stale process-table entry report a dead owner as + # live and keep a stale lock from ever being reclaimed - the same + # permanent read-only wedge this change exists to remove. + if kill -0 "$pid" 2>/dev/null; then + # Alive. Its identity may still be unreadable, and that is the case + # this verdict exists for: the owner is live, so its lock must not + # be reclaimed. + if fm_proc_info "$pid"; then + FM_SESSION_LOCK_PID_VERDICT=readable + fi + elif fm_proc_info "$pid"; then + # The process exists - this session simply may not signal it - so + # it is not a dead owner, but it is not a harness this session can + # prove either. The base contract treats that as "no live harness + # owner", and the lock stays reclaimable. + FM_SESSION_LOCK_PID_VERDICT=unknown + else + # Nothing can see the pid: it is gone. + FM_SESSION_LOCK_PID_VERDICT=dead + fi + return 0 +} + # True if $1 is a live process that looks like a verified harness. # A tagged Windows pid is answered from the Windows process table, because # kill -0 cannot see across that boundary and would report a live harness as # dead - which would hand a running session's home to a second one. fm_harness_pid_alive() { - local pid=$1 comm args winpid - if winpid=$(fm_win_untag_pid "$pid"); then - comm=$(fm_win_command "$winpid") || return 1 - fm_harness_process_matches "$comm" "$comm" - return - fi - kill -0 "$pid" 2>/dev/null || return 1 - comm=$(fm_ps_comm "$pid") || return 1 - args=$(fm_ps_args "$pid") - fm_harness_process_matches "$comm" "$args" + fm_session_lock_pid_verdict "$1" + [ "$FM_SESSION_LOCK_PID_VERDICT" = readable ] || return 1 + fm_harness_process_matches "$FM_PROC_COMM" "$FM_PROC_ARGS" } # Liveness and identity of a session-lock pid for readers outside this file: @@ -401,6 +723,36 @@ fm_session_pid_identity() { # fm_pid_identity "$1" } +# True when session-lock pid $1 must not be reclaimed: a readable harness owner +# that still matches holds the lock, and an unreadable-but-live owner is refused +# rather than guessed at. A dead record, a reused pid whose readable identity is +# not a verified harness, a malformed or all-zero record, and an owner this +# session can read but cannot signal are all reclaimable. +# +# The all-zero form is refused here rather than in the shared +# fm_session_pid_valid, because that predicate answers "is this value usable at +# all" for readers that must keep accepting any numeric record, and because zero +# is not a process identity: `kill -0 00` signals process group 0, so treating it +# as an owner wedges the home read-only instead of reclaiming it. This test is +# string-only on purpose - arithmetic or numeric normalization would let octal +# interpretation, integer overflow, or an arbitrarily long digit string change a +# verdict. +fm_session_lock_pid_blocks_reclaim() { + fm_session_pid_valid "$1" || return 1 + case "$1" in + "$FM_WIN_PID_PREFIX"*) ;; + *[1-9]*) ;; + *) return 1 ;; + esac + fm_session_lock_pid_verdict "$1" + case "$FM_SESSION_LOCK_PID_VERDICT" in + dead) return 1 ;; + unreadable) return 0 ;; + unknown) return 1 ;; + readable) fm_harness_process_matches "$FM_PROC_COMM" "$FM_PROC_ARGS" ;; + esac +} + # --- trusted same-session identity ------------------------------------------- # Claude Code hands every hook and tool shell CLAUDE_CODE_SESSION_ID (the # session's conversation id) and CLAUDE_PID (the pid of the process running the @@ -430,7 +782,7 @@ fm_session_pid_identity() { # # ancestry list an earlier walk already produced, so a caller that walked once # need not walk again. fm_session_lock_trusted_session_id() { # [] - local id=${CLAUDE_CODE_SESSION_ID:-} claude_pid=${CLAUDE_PID:-} pids=${1:-} pid comm args + local id=${CLAUDE_CODE_SESSION_ID:-} claude_pid=${CLAUDE_PID:-} pids=${1:-} pid [ -n "$id" ] || return 1 case "$id" in *$'\n'*|*$'\r'*) return 1 ;; esac case "$claude_pid" in ''|*[!0-9]*) return 1 ;; esac @@ -442,9 +794,8 @@ fm_session_lock_trusted_session_id() { # [] # from CLAUDE_PID and this same id, so it is matched in that form. case "$pid" in "$claude_pid") - comm=$(ps -o comm= -p "$pid" 2>/dev/null) || return 1 - args=$(ps -o args= -p "$pid" 2>/dev/null) - fm_harness_process_matches "$comm" "$args" || return 1 + fm_proc_info "$pid" || return 1 + fm_harness_process_matches "$FM_PROC_COMM" "$FM_PROC_ARGS" || return 1 ;; "$FM_WIN_PID_PREFIX$claude_pid") fm_harness_pid_alive "$pid" || return 1 @@ -525,9 +876,11 @@ fm_session_lock_owned_by_self() { fm_harness_pid_alive "$lock_pid" } -# True when state dir $1 records a live verified harness outside this process's -# contiguous harness ancestry that was not recorded by this same trusted Claude -# session. Sets FM_SESSION_LOCK_FOREIGN_OWNER_PID for a diagnostic caller. +# True when state dir $1 records a lock owner outside this process's contiguous +# harness ancestry that this session must not reclaim and did not record under +# its own trusted Claude session id - a live verified harness, or a live owner +# whose identity cannot be read. Sets FM_SESSION_LOCK_FOREIGN_OWNER_PID for a +# diagnostic caller. # Malformed, missing, dead, and ancestry-uncertain locks are not foreign-owner # evidence. # shellcheck disable=SC2034 # Output global, read by the sourcing guard caller. @@ -538,7 +891,7 @@ fm_session_lock_foreign_owner_live() { [ -f "$state/.lock" ] && [ ! -L "$state/.lock" ] || return 1 lock_pid=$(cat "$state/.lock" 2>/dev/null || true) fm_session_pid_valid "$lock_pid" || return 1 - fm_harness_pid_alive "$lock_pid" || return 1 + fm_session_lock_pid_blocks_reclaim "$lock_pid" || return 1 pids=$(fm_harness_ancestry_pids) || return 1 fm_harness_pid_listed "$lock_pid" "$pids" && return 1 fm_session_lock_same_session "$state" "$pids" && return 1 @@ -566,7 +919,7 @@ FM_LOCK_INSPECT_STATE=unknown FM_LOCK_INSPECT_PID= FM_LOCK_INSPECT_LIVE_HARNESS=unknown fm_session_lock_inspect() { # - local state=$1 lock pid winpid win_rc + local state=$1 lock pid # shellcheck disable=SC2034 # Output globals, read by lock status and inbox ready. FM_LOCK_INSPECT_STATE=unknown # shellcheck disable=SC2034 # Output globals, read by lock status and inbox ready. @@ -591,40 +944,28 @@ fm_session_lock_inspect() { # # shellcheck disable=SC2034 # Output global, read by lock status and inbox ready. FM_LOCK_INSPECT_PID=$pid fm_session_pid_valid "$pid" || return 0 - if winpid=$(fm_win_untag_pid "$pid"); then - # The same verdicts as a local pid below, read from the Windows table. - if fm_harness_pid_alive "$pid"; then - FM_LOCK_INSPECT_STATE=held - FM_LOCK_INSPECT_LIVE_HARNESS=true - return 0 - fi - win_rc=0 - fm_win_command "$winpid" >/dev/null || win_rc=$? - case "$win_rc" in - 0) FM_LOCK_INSPECT_LIVE_HARNESS=false ;; - 1) - FM_LOCK_INSPECT_STATE=stale - FM_LOCK_INSPECT_LIVE_HARNESS=false - ;; - esac - return 0 - fi - if kill -0 "$pid" 2>/dev/null; then - if fm_harness_pid_alive "$pid"; then - FM_LOCK_INSPECT_STATE=held - FM_LOCK_INSPECT_LIVE_HARNESS=true - else - FM_LOCK_INSPECT_STATE=unknown + # One verdict for every identity form. A tagged Windows pid is resolved by + # this same call through the Windows table, so the match below is the single + # place "live verified harness" is decided; deciding it a second time ahead + # of this one is what let a holder read as held there and unknown here. + fm_session_lock_pid_verdict "$pid" + case "$FM_SESSION_LOCK_PID_VERDICT" in + dead) + # shellcheck disable=SC2034 # Output globals, read by lock status and inbox ready. + FM_LOCK_INSPECT_STATE=stale + # shellcheck disable=SC2034 # Output globals, read by lock status and inbox ready. FM_LOCK_INSPECT_LIVE_HARNESS=false - fi - return 0 - fi - if ps -o comm= -p "$pid" >/dev/null 2>&1; then - FM_LOCK_INSPECT_STATE=unknown - return 0 - fi - # shellcheck disable=SC2034 # Output global, read by lock status and inbox ready. - FM_LOCK_INSPECT_STATE=stale - # shellcheck disable=SC2034 # Output global, read by lock status and inbox ready. - FM_LOCK_INSPECT_LIVE_HARNESS=false + ;; + readable) + if fm_harness_process_matches "$FM_PROC_COMM" "$FM_PROC_ARGS"; then + # shellcheck disable=SC2034 # Output globals, read by lock status and inbox ready. + FM_LOCK_INSPECT_STATE=held + # shellcheck disable=SC2034 # Output globals, read by lock status and inbox ready. + FM_LOCK_INSPECT_LIVE_HARNESS=true + else + # shellcheck disable=SC2034 # Output global, read by lock status and inbox ready. + FM_LOCK_INSPECT_LIVE_HARNESS=false + fi + ;; + esac } diff --git a/bin/fm-spawn.sh b/bin/fm-spawn.sh index d06446474e0..92acae1cc64 100755 --- a/bin/fm-spawn.sh +++ b/bin/fm-spawn.sh @@ -1,8 +1,8 @@ #!/usr/bin/env bash # Spawn a direct report: a crewmate in a treehouse or Orca worktree, or a # secondmate in its isolated firstmate home. -# Usage: fm-spawn.sh --mode --yolo [--branch-prefix ] [--base-branch ] [--harness |harness|launch-command] [--model ] [--effort ] [--seat ] [--hermes-profile ] [--backend ] [--herdr-resume-lock-wait] -# fm-spawn.sh --scout [--base-branch ] [--harness |harness|launch-command] [--model ] [--effort ] [--seat ] [--hermes-profile ] [--backend ] [--herdr-resume-lock-wait] +# Usage: fm-spawn.sh --mode --yolo [--branch-prefix ] [--base-branch ] [--harness |harness|launch-command] [--model ] [--effort ] [--hermes-profile ] [--seat ] [--account ] [--backend ] [--herdr-resume-lock-wait] +# fm-spawn.sh --scout [--base-branch ] [--harness |harness|launch-command] [--model ] [--effort ] [--hermes-profile ] [--seat ] [--account ] [--backend ] [--herdr-resume-lock-wait] # fm-spawn.sh [] [--harness |harness|launch-command] [--model ] [--effort ] [--seat ] [--backend ] --secondmate # --mode and --yolo are this task's delivery contract, REQUIRED for every ship # spawn and refused on --scout and --secondmate spawns. Firstmate resolves both @@ -64,7 +64,7 @@ # first in the private launch-brief overlay, including the exact task-owned # steering inbox. This never rewrites a project's instruction files or a # secondmate's charter. -# fm-spawn.sh --relaunch [--harness ] [--model ] [--effort ] [--seat ] +# fm-spawn.sh --relaunch [--harness ] [--model ] [--effort ] [--seat ] [--account ] # --relaunch launches a replacement agent for an EXISTING task into that # task's own recorded worktree, reusing its recorded endpoint when that # endpoint still exists, instead of creating either from scratch. It is @@ -75,13 +75,14 @@ # backend, kind, project or home, worktree, endpoint - comes from the task's # validated state/.meta, so --backend, --scout, --secondmate, a project # positional, and batch pairs are all refused alongside it; only harness, -# model, and effort may change, which is what makes a harness switch one -# ordinary relaunch. It refuses unless the recorded endpoint is positively -# agent-free on a backend with both a recovery-grade agent-state classifier -# and replacement-agent support (tmux or herdr), and clears the previous -# harness's per-task wiring before arming the new incarnation. T3 Code has -# the classifier but refuses relaunch because a thread keeps its conversation -# across turns and provider changes. Two verdicts are agent-free: a `dead` +# model, effort, and a declared Claude account may change, which is what +# makes a harness switch one ordinary relaunch. It refuses unless the +# recorded endpoint is positively agent-free on a backend with both a +# recovery-grade agent-state classifier and replacement-agent support +# (tmux or herdr), and clears the previous harness's per-task wiring +# before arming the new incarnation. T3 Code has the classifier but refuses +# relaunch because a thread keeps its conversation across turns and provider +# changes. Two verdicts are agent-free: a `dead` # endpoint is ADOPTED as-is, while an endpoint PROVEN gone is RE-CREATED in the recorded # worktree and the republished record rebinds the task to it. That proof is # its own step, because a backend's `missing` also covers an endpoint that is @@ -511,6 +512,13 @@ # Pins are never inherited; remote per-mate overrides refuse locally. # T3 Code refuses a pin because its provider instance owns the account. # bin/fm-worker-account-lib.sh owns parsing, the check, and the shed list. +# Claude account list (config/claude-accounts, --account ): +# Opt-in and Claude-only. When the list is present, a Claude ship or scout +# spawn must name one declared entry with --account, chosen at intake, and +# that entry is checked, launched, and recorded exactly like a pin; a +# relaunch reuses the recorded entry unless --account moves it. Without the +# list, --account refuses and nothing else changes. docs/configuration.md +# "Several Claude accounts" owns the contract. # Per-secondmate omp profile (config/secondmate-accounts//omp-profile): # Opt-in, and read only for a local secondmate's own omp agent; # bin/fm-omp-profile-lib.sh owns the file and its refusals. A pinned launch @@ -1001,6 +1009,8 @@ BASE_BRANCH_SET=0 UPSTREAM_CONSENT= UPSTREAM_CONSENT_SET=0 TRACEPARENT_SET=0 +ACCOUNT_ARG= +ACCOUNT_SET=0 RELAUNCH=0 # Opt-in only: exact-resume presentation-order lock waits instead of refusing. # Absent/unset keeps upstream refuse-on-contention. See header. @@ -1067,6 +1077,10 @@ for a in "$@"; do TRACEPARENT_ARG=$a TRACEPARENT_SET=1 ;; + account) + ACCOUNT_ARG=$a + ACCOUNT_SET=1 + ;; *) echo "error: internal parser state for --$want_value" >&2 exit 1 @@ -1142,6 +1156,11 @@ for a in "$@"; do TRACEPARENT_ARG=${a#--traceparent=} TRACEPARENT_SET=1 ;; + --account) want_value=account ;; + --account=*) + ACCOUNT_ARG=${a#--account=} + ACCOUNT_SET=1 + ;; *) POS+=("$a") ;; esac done @@ -1197,6 +1216,16 @@ fi echo "error: --traceparent requires a non-empty value" >&2 exit 1 } +[ "$ACCOUNT_SET" -eq 0 ] || [ -n "$ACCOUNT_ARG" ] || { + echo "error: --account requires a non-empty value" >&2 + exit 1 +} +# A secondmate, local or remote, launches on its own per-mate pin, so --account +# is refused here, before the remote route could ignore it. +[ "$ACCOUNT_SET" -eq 0 ] || [ "$KIND" != secondmate ] || { + echo "error: --account applies to ships and scouts; secondmate ${POS[0]:-} launches on its own config/secondmate-accounts//claude-account pin" >&2 + exit 1 +} # A parent-delivered carrier replaces this home's own resolution, so it is # refused unless it is a secondmate spawn carrying a strictly valid W3C value. # Nothing else may reach the pane's TRACEPARENT export. @@ -2211,6 +2240,7 @@ if [ "${#POS[@]}" -gt 0 ] && [ "${POS[0]}" != "$idpart" ] && case "$idpart" in * [ "$BRANCH_PREFIX_SET" -eq 0 ] || shared_args+=(--branch-prefix "$BRANCH_PREFIX") [ "$BASE_BRANCH_SET" -eq 0 ] || shared_args+=(--base-branch "$BASE_BRANCH") [ "$HERDR_RESUME_LOCK_WAIT" -eq 0 ] || shared_args+=(--herdr-resume-lock-wait) + [ "$ACCOUNT_SET" -eq 0 ] || shared_args+=(--account "$ACCOUNT_ARG") for pair in "${POS[@]}"; do case "$pair" in *=*) : ;; @@ -2620,6 +2650,36 @@ if [ "$RELAUNCH" -eq 1 ]; then echo "error: task $ID's recorded worktree '${RELAUNCH_WT:-none}' is missing; refusing to relaunch without the local copy its work lives in" >&2 exit 1 } + if [ "$KIND" != secondmate ]; then + # A worker must come back in its own disposable worktree, never in a + # primary home or a repository's primary checkout: refuse + # rather than fall back to wherever the restored pane happens to sit. + relaunch_wt_real=$(cd "$RELAUNCH_WT" 2>/dev/null && pwd -P) || relaunch_wt_real=$RELAUNCH_WT + relaunch_primary= + for relaunch_cand in "$FM_HOME" "$FM_ROOT"; do + relaunch_cand_real=$(cd "$relaunch_cand" 2>/dev/null && pwd -P) || continue + [ "$relaunch_wt_real" = "$relaunch_cand_real" ] && relaunch_primary=$relaunch_cand + done + if [ -z "$relaunch_primary" ]; then + # Probe the directory's own layout: an inherited GIT_DIR naming a linked + # worktree with GIT_WORK_TREE naming the primary checkout would otherwise + # make the two answers differ and pass the primary checkout. + relaunch_gdc=$( + unset GIT_DIR GIT_WORK_TREE GIT_COMMON_DIR GIT_OBJECT_DIRECTORY GIT_INDEX_FILE \ + GIT_ALTERNATE_OBJECT_DIRECTORIES GIT_CEILING_DIRECTORIES GIT_NAMESPACE \ + GIT_DISCOVERY_ACROSS_FILESYSTEM GIT_CONFIG GIT_CONFIG_GLOBAL \ + GIT_CONFIG_SYSTEM GIT_CONFIG_NOSYSTEM GIT_CONFIG_COUNT + git -C "$RELAUNCH_WT" rev-parse --path-format=absolute --git-dir --git-common-dir 2>/dev/null + ) || relaunch_gdc= + relaunch_gd=${relaunch_gdc%%$'\n'*} + relaunch_gc=${relaunch_gdc#*$'\n'} + [ -z "$relaunch_gd" ] || [ "$relaunch_gd" != "$relaunch_gc" ] || relaunch_primary="its repository's primary checkout" + fi + [ -z "$relaunch_primary" ] || { + echo "error: task $ID's recorded worktree '$RELAUNCH_WT' is the primary home or checkout ($relaunch_primary), not an isolated task worktree; refusing to relaunch a worker there" >&2 + exit 1 + } + fi if [ "$KIND" = secondmate ]; then FIRSTMATE_HOME=$(fm_meta_get "$RELAUNCH_META" home) [ -n "$FIRSTMATE_HOME" ] || FIRSTMATE_HOME=$RELAUNCH_WT @@ -3536,7 +3596,15 @@ RAW_COMMAND= [ "$RAW_LAUNCH" = 0 ] || RAW_COMMAND=$ARG3 WORKER_ACCOUNT_MATE= [ "$KIND" != secondmate ] || WORKER_ACCOUNT_MATE=$ID -WORKER_ACCOUNT=$(fm_worker_account_select "$HARNESS" "$CONFIG" "$MODEL" "${PI_BIN:-$HARNESS}" "$RAW_COMMAND" "$WORKER_ACCOUNT_MATE") || exit 1 +# A declared Claude account list (config/claude-accounts) needs the entry this +# launch spends: --account on a fresh spawn, and on a relaunch that flag or +# else the account the task already records. +WORKER_ACCOUNT_REQUEST=$ACCOUNT_ARG +if [ "$RELAUNCH" -eq 1 ]; then + WORKER_ACCOUNT_REQUEST=$(fm_worker_account_relaunch_request "$HARNESS" "$CONFIG" "$RELAUNCH_PRIOR_HARNESS" \ + "$(fm_meta_get "$RELAUNCH_META" account)" "$ACCOUNT_ARG" "$WORKER_ACCOUNT_MATE") || exit 1 +fi +WORKER_ACCOUNT=$(fm_worker_account_select "$HARNESS" "$CONFIG" "$MODEL" "${PI_BIN:-$HARNESS}" "$RAW_COMMAND" "$WORKER_ACCOUNT_MATE" "$WORKER_ACCOUNT_REQUEST") || exit 1 WORKER_ACCOUNT_DECLARED=${WORKER_ACCOUNT%%$'\t'*} WORKER_ACCOUNT_ROOT=${WORKER_ACCOUNT#*$'\t'} WORKER_ACCOUNT_PROVIDER=${WORKER_ACCOUNT_ROOT#*$'\t'} @@ -5644,18 +5712,29 @@ spawn_enter_recorded_worktree() { # starts. Zellij and cmux implement this read with a shell probe, so keeping it # before launch prevents the probe from becoming input to a live worker. spawn_assert_agent_worktree() { - local expected seen i + local expected seen i last_poll=19 agent_status [ "$KIND" = secondmate ] && return 0 case "$BACKEND" in orca|t3code) return 0 ;; esac expected=$(real_path_or_raw "$WT") - for i in $(seq 1 20); do + # The out-of-pane Herdr lease no longer waits for the new shell to process + # treehouse get. Allow its former minute of startup tolerance here, and + # repeat the idempotent cd once in case input arrived before shell readiness. + [ "$BACKEND" != herdr ] || last_poll=120 + for i in $(seq 0 "$last_poll"); do + [ "$i" -eq 0 ] || sleep 0.5 seen=$(spawn_current_path "$WT_TARGET" || true) if [ -n "$seen" ] && [ "$(real_path_or_raw "$seen")" = "$expected" ]; then return 0 fi - [ "$i" -ge 20 ] || sleep 0.5 + if [ "$BACKEND" = herdr ] && [ "$i" -eq 20 ]; then + spawn_enter_recorded_worktree + fi done echo "error: task $ID's worker started in '${seen:-unknown}', not its recorded worktree '$WT'; refusing to continue outside the copy holding its work" >&2 + if [ "$BACKEND" = herdr ] && fm_backend_herdr_parse_target "$WT_TARGET"; then + agent_status=$(fm_backend_herdr_agent_status_raw "$FM_BACKEND_HERDR_SESSION" "$FM_BACKEND_HERDR_PANE") + echo "error: Herdr pane '$WT_TARGET': last foreground_cwd='${seen:-unknown}', agent_status='${agent_status:-unknown}'" >&2 + fi exit 1 } @@ -7903,6 +7982,40 @@ fi # LAUNCH_ENV_PREFIX construction below sets it again at the `env -i` boundary, # so under an enabled allowlist the switch is established before the wrapping # `/bin/sh` starts rather than only inside the command that shell runs. +# Arm the worker-side OSC 7501 status reporter (bin/fm-osc7501.sh) inside the +# launch before the agent command, so the reporter's parent is the pane shell +# that runs the agent. The reporter header owns eligibility and native-version +# gates; pass the executable the launch runs so the native probe reads its +# version. A raw launch command establishes it only as a simple command whose +# first word is a plain path to a harness with a launch template; a wrapper, +# assignment prefix, quoted or expanded path, compound command, or unknown +# adapter hides what actually runs, so it passes none and gets no reporter. +# Its stderr stays on the pane so a config warning reaches the user. The `disarm` after the agent +# command retires the reporter when the agent exits, because the pane shell +# that sourced the launch outlives it. Newlines, not `;`, delimit the agent +# command, so a raw launch ending in a separator or a comment stays intact. +OSC7501_AGENT_BIN= +if [ "$RAW_LAUNCH" = 0 ]; then + case "$HARNESS" in + pi | pi-signed) OSC7501_AGENT_BIN=${PI_BIN:-} ;; + muse) OSC7501_AGENT_BIN=${MUSE_BIN:-} ;; + *) OSC7501_AGENT_BIN=$HARNESS ;; + esac +else + read -r osc7501_word _ <<<"$RAW_COMMAND" + case "$RAW_COMMAND" in + *[\;\&\|]* | *$'\n'*) ;; + *) + case "$osc7501_word" in + '' | *[!A-Za-z0-9._/+-]*) ;; + *) (launch_template "$HARNESS" "$KIND") >/dev/null 2>&1 && OSC7501_AGENT_BIN=$osc7501_word ;; + esac + ;; + esac +fi +LAUNCH="$(shell_quote "$FM_ROOT/bin/fm-osc7501.sh") arm --task $(shell_quote "$ID") --harness $(shell_quote "$HARNESS") --backend $(shell_quote "$BACKEND") --kind $(shell_quote "$KIND") --state $(shell_quote "$STATE_REAL") --config $(shell_quote "$CONFIG") --target $(shell_quote "$T") --agent-bin $(shell_quote "$OSC7501_AGENT_BIN") >/dev/null || true +$LAUNCH +$(shell_quote "$FM_ROOT/bin/fm-osc7501.sh") disarm --state $(shell_quote "$STATE_REAL") >/dev/null 2>/dev/null || true" if [ "$LAVISH_AXI_HOST_CONFIG_PRESENT" = 1 ]; then LAUNCH="export LAVISH_AXI_HOST=$(shell_quote "$LAVISH_AXI_HOST"); $LAUNCH" fi diff --git a/bin/fm-supervise-daemon.sh b/bin/fm-supervise-daemon.sh index 281cba85bb4..d98ab058203 100755 --- a/bin/fm-supervise-daemon.sh +++ b/bin/fm-supervise-daemon.sh @@ -171,6 +171,8 @@ FM_HOME="${FM_HOME:-${FM_ROOT_OVERRIDE:-$FM_ROOT}}" # shellcheck source=bin/fm-backend.sh . "$FM_DAEMON_DIR/fm-backend.sh" +# shellcheck source=bin/fm-watch-key-lib.sh +. "$FM_DAEMON_DIR/fm-watch-key-lib.sh" # Canonical construction and parsing for every Firstmate operational input. # shellcheck source=bin/fm-operational-input.sh @@ -592,7 +594,7 @@ clear_pause_tracking() { # local win=$1 state=$2 task key watcher_key task=$(window_to_task "$win" "$state") key=$(_stale_key "$task") - watcher_key=$(_stale_key "$win") + watcher_key=$(window_key "$win") # This list mirrors the watcher's own per-window markers, cap markers included: # releasing a window's wedge timer and escalation count releases them with it, # which is what bin/fm-watch.sh's wedge_episode_end does on the routes that @@ -618,7 +620,7 @@ reconcile_pause_tracking() { # task=$(window_to_task "$win" "$state") key=$(_stale_key "$task") marker="$state/.subsuper-paused-$key" - watcher_key=$(_stale_key "$win") + watcher_key=$(window_key "$win") if status_is_paused_or_captain_held "$last"; then stale_marker_remove "$win" "$state" pause_marker_record "$win" "$state" @@ -635,7 +637,7 @@ migrate_watcher_pause_markers() { # [ -n "$win" ] || continue task=$(basename "$meta"); task=${task%.meta} key=$(_stale_key "$task") - watcher_key=$(_stale_key "$win") + watcher_key=$(window_key "$win") last=$(status_declared_wait_line "$state/$task.status") if status_is_paused_or_captain_held "$last" || [ -e "$state/.subsuper-paused-$key" ] || [ -e "$state/.paused-$watcher_key" ]; then reconcile_pause_tracking "$win" "$state" "$last" diff --git a/bin/fm-teardown.sh b/bin/fm-teardown.sh index 05072b1f51b..3f2e488111c 100755 --- a/bin/fm-teardown.sh +++ b/bin/fm-teardown.sh @@ -15,8 +15,8 @@ # Removing state/.meta and landing the backlog transition are one step, not # two: bin/fm-backlog-transition-lib.sh owns that invariant, and both halves run # under the task's own meta lock before this script reports success. Because the -# completion links (the PR, the report path, a local-main note) live only in the -# record being removed, the intended transition is recorded in +# completion links (the PR, the report path, a local-main or discarded note) live +# only in the record being removed, the intended transition is recorded in # state/.backlog-close first, so a process killed between the halves leaves # the next session start enough to finish it; a landed close removes that record. # A close that fails is fatal and loud, preserves its pending-close record, and @@ -43,8 +43,11 @@ # that closes the call. # REFUSES if the worktree holds work that has not LANDED, because cleanup # hard-resets/removes the worktree and kills its processes. Work has landed when it is -# reachable from any remote-tracking branch (a fork counts as a remote, so -# upstream-contribution PRs pushed to a fork satisfy this in any mode), OR - for a +# reachable from any remote-tracking branch other than the local no-mistakes +# mirror (a fork counts as a remote, so upstream-contribution PRs pushed to a +# fork satisfy this in any mode; the mirror is not a forge), after +# refresh_remote_refs has updated those refs from each remote when the local ones +# do not show the work, OR - for a # normal ship task whose commits are not so reachable - when its PR is merged and # GitHub reports a PR head that contains the current local work, or its content is # already present in the up-to-date default branch. This recognizes the common @@ -207,8 +210,9 @@ # leased home and state in place instead of hiding a still-held lease. # Usage: fm-teardown.sh [--force] [--legacy-record] # --force skips ordinary-task dirty and landed-work checks, skips scout report -# checks, and discards secondmate child work for kind=secondmate. Only use it -# when the captain has explicitly said to discard the work. +# checks and closes the scout as discarded with no report recorded, and +# discards secondmate child work for kind=secondmate. Only use it when the +# captain has explicitly said to discard the work. # --legacy-record accepts a task record that predates the spawn_gen field: # teardown then proceeds only when the recorded endpoint is confirmed dead or # agent-less (bin/fm-backend.sh's recovery-grade classifier), and without @@ -1704,7 +1708,7 @@ unpushed_patches_are_in_pr_head() { | sort -u ) || return 1 [ -n "$pr_patch_ids" ] || return 1 - unpushed=$(git -C "$WT" log --format=%H HEAD --not --remotes -- 2>/dev/null) || return 1 + unpushed=$(git -C "$WT" log --format=%H HEAD --not --exclude='no-mistakes/*' --remotes -- 2>/dev/null) || return 1 [ -n "$unpushed" ] || return 1 while IFS= read -r commit; do [ -n "$commit" ] || continue @@ -1813,17 +1817,27 @@ work_is_landed() { content_in_default } -# The completion links this teardown already holds locally. A scout's -# deliverable is its report, a local-only ship lands on local main, and every -# other ship carries the PR recorded on its own record. +# The completion links this teardown already holds locally. A retained scout +# keeps its report as the held deliverable, while a force-closed scout is +# recorded as discarded with no report. A local-only ship lands on local main, +# and every other ship carries the PR recorded on its own record. BACKLOG_DONE_ARGS=() backlog_done_args() { local data_relative BACKLOG_DONE_ARGS=() case "$KIND" in scout) - data_relative=$(fm_backlog_data_relative "$DATA") || return 1 - BACKLOG_DONE_ARGS=(--report "$data_relative/$ID/report.md") + if [ "$BACKLOG_TRANSITION" = retain ]; then + if [ -f "$DATA/$ID/report.md" ]; then + data_relative=$(fm_backlog_data_relative "$DATA") || return 1 + BACKLOG_DONE_ARGS=(--report "$data_relative/$ID/report.md") + fi + elif [ "$FORCE" = "--force" ]; then + BACKLOG_DONE_ARGS=(--note "discarded") + else + data_relative=$(fm_backlog_data_relative "$DATA") || return 1 + BACKLOG_DONE_ARGS=(--report "$data_relative/$ID/report.md") + fi ;; *) if [ "$MODE" = local-only ]; then @@ -2124,14 +2138,41 @@ teardown_note_dirty_return() { printf '%s\n' "$residue" | sed 's/^/ /' >&2 } -validate_worktree_teardown_safety() { - local dirty_raw dirty unpushed_raw unpushed DEFAULT unmerged_raw unmerged branch - [ -d "$WT" ] || return 0 - [ "$FORCE" != "--force" ] || return 0 - case "$KIND" in - secondmate|scout) return 0 ;; - esac +# A worker may have pushed from another checkout, so tracking refs that do not +# show the work prove nothing until they are refreshed. Runs at most once per +# teardown. Each fetch is bounded to 30 seconds and non-interactive, and +# --no-prune keeps a branch a remote has dropped visible, whatever fetch.prune +# says. The explicit +# refspec covers every branch even when a clone tracks only some of them. A failed +# fetch only leaves the last-known refs in place, so the unchanged checks below +# still refuse work those refs do not show; REMOTE_REFRESH_FAILED names it for the +# refusal. The no-mistakes gate is skipped: it is a local validation copy, and +# fetching it would count work as landed that no real remote holds. +REMOTE_REFS_REFRESHED=0 +REMOTE_REFRESH_FAILED= +refresh_remote_refs() { + local remote + [ "$REMOTE_REFS_REFRESHED" = 0 ] || return 0 + REMOTE_REFS_REFRESHED=1 + while IFS= read -r remote; do + case "$remote" in ''|no-mistakes) continue ;; esac + GIT_TERMINAL_PROMPT=0 fm_run_timed 30 git -C "$WT" fetch --quiet --no-prune --no-tags --no-recurse-submodules \ + "$remote" "+refs/heads/*:refs/remotes/$remote/*" /dev/null 2>&1 \ + || REMOTE_REFRESH_FAILED="$REMOTE_REFRESH_FAILED${REMOTE_REFRESH_FAILED:+, }$remote" + done </dev/null) +EOF +} +report_remote_refresh_failure() { + [ -n "$REMOTE_REFRESH_FAILED" ] || return 0 + echo "Could not refresh remote: $REMOTE_REFRESH_FAILED. This verdict used the last-known refs, so the work may be there; retry once the remote is reachable." >&2 +} + +# Sets the caller's $dirty from a fresh status read, or refuses when the copy +# cannot be inspected. +read_worktree_dirt() { + local dirty_raw if ! dirty_raw=$(git -C "$WT" status --porcelain --untracked-files=all 2>/dev/null); then if worktree_safety_blocked_by_lock "uncommitted changes"; then return "$TEARDOWN_WORKTREE_SAFETY_LOCK_BLOCKED" @@ -2141,8 +2182,19 @@ validate_worktree_teardown_safety() { return 1 fi dirty=$(printf '%s\n' "$dirty_raw" | grep -vE '^\?\? (\.claude/|\.fm-(grok|kimi)-turnend$)' || true) +} - if ! unpushed_raw=$(git -C "$WT" log --oneline HEAD --not --remotes -- 2>/dev/null); then +validate_worktree_teardown_safety() { + local dirty unpushed_raw unpushed DEFAULT unmerged_raw unmerged branch + [ -d "$WT" ] || return 0 + [ "$FORCE" != "--force" ] || return 0 + case "$KIND" in + secondmate|scout) return 0 ;; + esac + + read_worktree_dirt || return + + if ! unpushed_raw=$(git -C "$WT" log --oneline HEAD --not --exclude='no-mistakes/*' --remotes -- 2>/dev/null); then if worktree_safety_blocked_by_lock "commits not on a remote"; then return "$TEARDOWN_WORKTREE_SAFETY_LOCK_BLOCKED" fi @@ -2150,6 +2202,21 @@ validate_worktree_teardown_safety() { echo "Restore the git index state, or get the captain's explicit OK to discard, then --force." >&2 return 1 fi + # Dirty work refuses either way, so only a clean copy is worth the network. + if [ -n "$unpushed_raw" ] && [ -z "$dirty" ] && [ "$REMOTE_REFS_REFRESHED" = 0 ]; then + refresh_remote_refs + # The task's processes are still running, so edits may have arrived during + # the wait; the refusals below act on this fresh read. + read_worktree_dirt || return + if ! unpushed_raw=$(git -C "$WT" log --oneline HEAD --not --exclude='no-mistakes/*' --remotes -- 2>/dev/null); then + if worktree_safety_blocked_by_lock "commits not on a remote"; then + return "$TEARDOWN_WORKTREE_SAFETY_LOCK_BLOCKED" + fi + echo "REFUSED: cannot inspect worktree $WT for commits not on a remote." >&2 + echo "Restore the git index state, or get the captain's explicit OK to discard, then --force." >&2 + return 1 + fi + fi unpushed=$(printf '%s\n' "$unpushed_raw" | head -5) if [ -n "$unpushed" ] && [ "$MODE" = local-only ]; then @@ -2167,6 +2234,7 @@ validate_worktree_teardown_safety() { echo "REFUSED: local-only worktree $WT has work not yet merged into $DEFAULT and not on any remote." >&2 [ -n "$dirty" ] && report_worktree_dirt "$dirty" [ -n "$unmerged" ] && printf 'commits not yet on %s:\n%s\n' "$DEFAULT" "$unmerged" >&2 + report_remote_refresh_failure echo "Merge the branch into local $DEFAULT first (bin/fm-merge-local.sh after the captain approves), or push to a fork/remote, or get the captain's explicit OK to discard, then --force." >&2 return 1 fi @@ -2189,6 +2257,7 @@ validate_worktree_teardown_safety() { echo "REFUSED: worktree $WT has work not on any remote and not landed." >&2 fi printf 'unpushed commits:\n%s\n' "$unpushed" >&2 + report_remote_refresh_failure echo "Push the branch, land its PR, or get the captain's explicit OK to discard, then --force." >&2 return 1 fi diff --git a/bin/fm-test-linux.sh b/bin/fm-test-linux.sh new file mode 100755 index 00000000000..dbd6a357c5f --- /dev/null +++ b/bin/fm-test-linux.sh @@ -0,0 +1,121 @@ +#!/usr/bin/env bash +# fm-test-linux.sh - opt-in: run the shell tests changed on this branch inside a +# Linux container with GNU userland. +# +# macOS ships BSD stat, date, sed and friends, so a fixture that passes locally +# can fail on Linux CI (for example `stat -f ... || stat -c ...`, where GNU +# `stat -f` prints filesystem output before failing). Run this before +# publishing a branch whose tests touch stat, date, or process behavior. +# +# Usage: +# fm-test-linux.sh [--base ] [--list] [tests/.test.sh ...] +# fm-test-linux.sh -h|--help +# +# With no test paths, the tests are the tests/*.test.sh files that differ from +# the merge base with --base (default: origin/main, else main), including +# uncommitted and untracked ones. A local clone of the repo, overlaid with the +# working tree files, is mounted read-write from a scratch directory, so tests +# see git history and the checkout is never modified. Tests run as the host +# user, not root, to match CI file-mode behavior. +# --list prints the selected tests and exits without a container runtime. +# +# Runtime: FM_CONTAINER_RUNTIME, else docker, else podman. With none installed +# the script stops with exit 2. The image is built from tests/linux/Dockerfile +# (Debian bookworm, GNU coreutils) and tagged fm-test-linux:bookworm. +# +# Exit status: 0 all selected tests passed (or none changed), 1 a test failed, +# 2 usage error or no container runtime. +set -eu + +usage() { + sed -n '2,/^set -eu/p' "$0" | sed '$d' | sed 's/^# \{0,1\}//' +} + +die() { + printf 'fm-test-linux: %s\n' "$1" >&2 + exit 2 +} + +base="" +list_only=0 +tests=() +while [ $# -gt 0 ]; do + case "$1" in + -h|--help) usage; exit 0 ;; + --base) [ $# -ge 2 ] || die "--base needs a ref"; base=$2; shift 2 ;; + --list) list_only=1; shift ;; + -*) die "unknown option: $1" ;; + *) tests+=("$1"); shift ;; + esac +done + +root=$(git rev-parse --show-toplevel 2>/dev/null) || die "run inside a git checkout" +script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) +dockerfile_dir="$script_dir/../tests/linux" + +if [ ${#tests[@]} -eq 0 ]; then + if [ -z "$base" ]; then + for ref in origin/main main; do + if git -C "$root" rev-parse --verify --quiet "$ref^{commit}" >/dev/null; then base=$ref; break; fi + done + fi + [ -n "$base" ] || die "no origin/main or main to compare against; pass --base " + merge_base=$(git -C "$root" merge-base HEAD "$base") || die "no merge base with $base" + while IFS= read -r path; do + [ -f "$root/$path" ] && tests+=("$path") + done < <({ + git -C "$root" diff --name-only --diff-filter=ACMR "$merge_base" -- + git -C "$root" ls-files --others --exclude-standard + } | sort -u | grep -E '^tests/[^/]+\.test\.sh$' || true) +fi + +if [ ${#tests[@]} -eq 0 ]; then + echo "fm-test-linux: no changed shell tests" + exit 0 +fi + +if [ "$list_only" = 1 ]; then + printf '%s\n' "${tests[@]}" + exit 0 +fi + +runtime=${FM_CONTAINER_RUNTIME:-} +if [ -z "$runtime" ]; then + for candidate in docker podman; do + if command -v "$candidate" >/dev/null 2>&1; then runtime=$candidate; break; fi + done +fi +{ [ -n "$runtime" ] && command -v "$runtime" >/dev/null 2>&1; } || + die "no container runtime found; install docker or podman, or set FM_CONTAINER_RUNTIME" + +image=fm-test-linux:bookworm +"$runtime" build -q -t "$image" "$dockerfile_dir" >/dev/null || + die "$runtime could not build $image; is the container engine running?" + +scratch=$(mktemp -d "${TMPDIR:-/tmp}/fm-test-linux.XXXXXX") +trap 'rm -rf "$scratch"' EXIT +git clone -q --local --no-hardlinks "$root" "$scratch" || die "could not clone $root into the scratch directory" +files=() +while IFS= read -r -d '' path; do + [ -e "$root/$path" ] || [ -L "$root/$path" ] && files+=("$path") +done < <(git -C "$root" ls-files -z --cached --others --exclude-standard) +(cd "$root" && printf '%s\0' "${files[@]}" | tar --null -T - -cf -) | tar -C "$scratch" -xf - +while IFS= read -r -d '' path; do + [ -e "$root/$path" ] || [ -L "$root/$path" ] || rm -f "$scratch/$path" +done < <(git -C "$scratch" ls-files -z) + +printf 'fm-test-linux: %s on %s\n' "${#tests[@]}" "$("$runtime" run --rm "$image" sh -c 'uname -sr')" +case "$(basename "$runtime")" in + podman*) identity=(--userns=keep-id) ;; + *) identity=(--user "$(id -u):$(id -g)") ;; +esac +failed=() +for t in "${tests[@]}"; do + if "$runtime" run --rm "${identity[@]}" -v "$scratch:/work" -w /work -e HOME=/tmp -e TMPDIR=/tmp "$image" bash "$t"; then + printf 'PASS %s\n' "$t" + else + printf 'FAIL %s\n' "$t" + failed+=("$t") + fi +done +[ ${#failed[@]} -eq 0 ] || { printf 'fm-test-linux: %s failed\n' "${#failed[@]}" >&2; exit 1; } diff --git a/bin/fm-test-run.sh b/bin/fm-test-run.sh index cf02dce8937..857c9ad6884 100755 --- a/bin/fm-test-run.sh +++ b/bin/fm-test-run.sh @@ -293,7 +293,7 @@ family_for_basename() { fm-crew-state.test.sh|fm-captain-hold-lifecycle.test.sh|\ fm-documentation-audiences.test.sh|fm-ensure-agents-md.test.sh|fm-forge-detect.test.sh|fm-grok-harness.test.sh|\ fm-fork-free-helpers.test.sh|\ - fm-harness-precedence.test.sh|\ + fm-harness-precedence.test.sh|fm-install-herdr.test.sh|\ fm-kimi-harness.test.sh|fm-devin-harness.test.sh|fm-commandcode-harness.test.sh|fm-cortex-harness.test.sh|fm-hermes-harness.test.sh|fm-muse-harness.test.sh|fm-rovo-harness.test.sh|fm-agy-harness.test.sh|fm-kiro-harness.test.sh|fm-omp-harness.test.sh|fm-polytoken-harness.test.sh|fm-herdr-lab.test.sh|fm-lint.test.sh|\ fm-lint-workflows.test.sh|\ fm-operational-input.test.sh|fm-pi-primary-types.test.sh|fm-read-repetition.test.sh|\ @@ -302,10 +302,11 @@ family_for_basename() { fm-send-popup-settle.test.sh|fm-send-settle.test.sh|\ fm-subagent-pretool-check.test.sh|\ fm-supervision-instructions.test.sh|fm-task-delivery.test.sh|\ - fm-timeout-lib.test.sh|\ + fm-timeout-lib.test.sh|fm-remote-inherit-generation.test.sh|\ fm-tmux-submit-busy.test.sh|fm-trace-context-lib.test.sh|\ fm-transition-lib.test.sh|\ - fm-test-run.test.sh|fm-test-isolation-proof.test.sh|fm-test-tap-verify.test.sh) + fm-test-run.test.sh|fm-test-isolation-proof.test.sh|fm-test-tap-verify.test.sh|\ + fm-test-linux.test.sh) printf '%s\n' pure-contract-unit ;; fm-daemon.test.sh|fm-guard-stale-banner.test.sh|fm-pi-watch-extension.test.sh|\ @@ -375,6 +376,7 @@ family_for_basename() { fm-backend-t3code-pr-tools-live-e2e.test.sh|\ fm-pi-seeded-home-trust-live-e2e.test.sh|\ fm-herdr-version-floor-live-e2e.test.sh|\ + fm-herdr-projected-cd-live-e2e.test.sh|\ fm-herdr-pi-stale-registration-live-e2e.test.sh|\ fm-worker-account-live-e2e.test.sh|fm-dock-auth-live-e2e.test.sh|\ fm-opencode-primary-live-e2e.test.sh|fm-pi-branch-live-e2e.test.sh|\ @@ -448,6 +450,7 @@ family_for_basename() { ;; fm-branch-supervision.test.sh|fm-busy-adapter-wiring.test.sh|\ fm-busy-state.test.sh|fm-capacity.test.sh|fm-classify-corr-token.test.sh|\ + fm-claude-account-quota.test.sh|\ fm-claude-stop-autoarm.test.sh|fm-cursor-harness.test.sh|\ fm-dispatch-resolve.test.sh|\ fm-extension-binding.test.sh|fm-gitignore-config.test.sh|\ @@ -777,6 +780,7 @@ tests/fm-check-unregister.test.sh 469 tests/fm-ci-workflow.test.sh 5833 tests/fm-classify-corr-token.test.sh 23085 tests/fm-classify-decision-key.test.sh 4362 +tests/fm-claude-account-quota.test.sh 2492 tests/fm-claude-stop-autoarm-live-e2e.test.sh 73 tests/fm-claude-stop-autoarm.test.sh 61189 tests/fm-claude-trust.test.sh 12010 @@ -824,6 +828,7 @@ tests/fm-harness-liveness-drift-live-e2e.test.sh 1309 tests/fm-harness-precedence.test.sh 4083 tests/fm-herdr-groups.test.sh 5418 tests/fm-herdr-pi-stale-registration-live-e2e.test.sh 55 +tests/fm-herdr-projected-cd-live-e2e.test.sh 28000 tests/fm-herdr-session-cleanup.test.sh 7425 tests/fm-herdr-submit-confirm-live-e2e.test.sh 51 tests/fm-herdr-version-floor-live-e2e.test.sh 50 @@ -837,6 +842,7 @@ tests/fm-house-board.test.sh 4632 tests/fm-housefeature-cut.test.sh 623 tests/fm-inactive-reconcile.test.sh 60823 tests/fm-inbox.test.sh 6062 +tests/fm-install-herdr.test.sh 1000 tests/fm-jev-mem-guard.test.sh 336 tests/fm-kimi-harness.test.sh 58917 tests/fm-kimi-trust-prompt-live-e2e.test.sh 60 @@ -844,6 +850,7 @@ tests/fm-kiro-signals-live-e2e.test.sh 75 tests/fm-launch-prompt-signals-live-e2e.test.sh 50 tests/fm-lint-workflows.test.sh 872 tests/fm-live-gate.test.sh 7452 +tests/fm-live-lab-isolation.test.sh 6459 tests/fm-live-lab-up-mate.test.sh 17363 tests/fm-live-lab.test.sh 79639 tests/fm-mail-check.test.sh 7524 @@ -857,6 +864,7 @@ tests/fm-omp-primary-live-e2e.test.sh 74 tests/fm-on.test.sh 11473 tests/fm-opencode-primary-live-e2e.test.sh 47 tests/fm-operational-input.test.sh 2404 +tests/fm-osc7501.test.sh 14000 tests/fm-parent-channel-scan-exclusion.test.sh 4814 tests/fm-peek-remote.test.sh 1082 tests/fm-pending-reply.test.sh 41090 @@ -920,6 +928,7 @@ tests/fm-send-resolve-key.test.sh 47317 tests/fm-send-secondmate-marker-herdr-e2e.test.sh 80 tests/fm-send-secondmate-marker.test.sh 7574 tests/fm-session-lock-ancestry.test.sh 18918 +tests/fm-session-lock-ps-denied.test.sh 3012 tests/fm-session-lock-windows-consumers.test.sh 4584 tests/fm-session-start.test.sh 363574 tests/fm-sessionstart-hook-live-e2e.test.sh 50 @@ -956,6 +965,7 @@ tests/fm-teardown.test.sh 202132 tests/fm-test-fixture-cleanup.test.sh 866 tests/fm-test-fixtures.test.sh 1802 tests/fm-test-isolation-proof.test.sh 2866 +tests/fm-test-linux.test.sh 1500 tests/fm-test-tap-verify.test.sh 1000 tests/fm-timeout-lib.test.sh 10750 tests/fm-tmux-agent-liveness.test.sh 3770 @@ -1512,6 +1522,11 @@ families_for_unmapped_bin() { # Never expands to the complete suite. families_for_changed_path() { local path=$1 fixture_ref capture_ref + case "$path" in + bin/fm-remote*|bin/fm-secondmate*) + printf '%s\n' "__script__:fm-remote-inherit-generation.test.sh" + ;; + esac case "$path" in tests/fm-backend-herdr-eventwait.test.py) printf '%s\n' real-herdr-gated @@ -1541,6 +1556,9 @@ families_for_changed_path() { bin/fm-test-tap-verify.sh) printf '%s\n' "__script__:fm-test-tap-verify.test.sh" ;; + bin/fm-test-linux.sh|tests/linux/Dockerfile) + printf '%s\n' "__script__:fm-test-linux.test.sh" + ;; bin/backends/herdr*|bin/fm-herdr-lab.sh|tests/herdr-test-safety.sh|tests/herdr-client-pair-fixture.sh) printf '%s\n' real-herdr-gated printf '%s\n' backend-dispatch @@ -1663,6 +1681,7 @@ families_for_changed_path() { printf '%s\n' "__script__:fm-procevent-quota.test.sh" printf '%s\n' "__script__:fm-quota-choose.test.sh" printf '%s\n' "__script__:fm-dispatch-resolve.test.sh" + printf '%s\n' "__script__:fm-claude-account-quota.test.sh" ;; bin/fm-procevent-quota.sh) printf '%s\n' "__script__:fm-procevent-quota.test.sh" @@ -1670,6 +1689,9 @@ families_for_changed_path() { bin/fm-quota-choose.sh) printf '%s\n' "__script__:fm-quota-choose.test.sh" ;; + bin/fm-claude-account-quota.sh) + printf '%s\n' "__script__:fm-claude-account-quota.test.sh" + ;; bin/fm-dispatch-resolve.sh) printf '%s\n' "__script__:fm-dispatch-resolve.test.sh" ;; diff --git a/bin/fm-timeout-lib.sh b/bin/fm-timeout-lib.sh index 9d58cbc172a..99d1ab51e2b 100644 --- a/bin/fm-timeout-lib.sh +++ b/bin/fm-timeout-lib.sh @@ -14,7 +14,7 @@ # executable path, and that path is what fm_run_timed and fm_exec_timed # invoke, so a builtin or function of the same name cannot shadow the # runner. Watcher-owned calls use the owner-aware selection described -# under fm_run_timed. The perl and bash fallbacks are unchanged. +# under fm_run_timed. # # fm_run_timed [args...] # Runs the command with a hard bound, preserving the caller's stdin. Exit @@ -28,7 +28,16 @@ # exit, and is reported as 124 too. Only 137 raised by GNU/BSD timeout's # own KILL escalation, with no status recorded by the bounded command, # also collapses into 124: there it means the bound fired, not that the -# command chose to die. +# command chose to die. A pipe or file on standard input reaches the +# command on every mechanism. The timeout and bash mechanisms background +# the command and restore that redirected stdin afterwards; they do not +# attach a terminal, and a closed stdin does not fail the call. +# On caller INT/HUP/TERM, the Perl fallback reaps its command group with +# up to 0.2s TERM-to-KILL grace, then restores traps active in the current +# process and redelivers the signal. In a command substitution, inherited +# but inactive parent handlers stay disabled. If the caller continues, +# it returns 128+n. +# Its deadline and cleanup grace use a monotonic clock. # With FM_TIMEOUT_OWNER_PID set by the watcher, use fm_exec_timed's # owner-aware Perl runner with a one-second grace, or the Bash fallback # when forced or Perl is absent. The Bash watchdog checks both the watcher @@ -75,14 +84,13 @@ # fm_timed_out # 0 iff is how fm_run_timed or fm_exec_timed reports the bound. # -# A non-positive bound is not a bound: `timeout 0` and the perl fallback's -# `alarm 0` both disable the deadline, so callers must reject 0 before calling. +# Callers must supply a positive integer bound; `timeout 0` disables the deadline. # # All four fm_run_timed mechanisms terminate the whole process GROUP, not just # the direct child, so a hung grandchild (a vendor CLI spawned by a wrapper # script, a git fetch spawned by a sweep) cannot outlive the bound. GNU/BSD # `timeout` does this by default because it does not run the command in the -# foreground process group; the perl fallback does it explicitly with setpgrp +# foreground process group; the perl fallback does it explicitly with setpgid # plus a negative pid, and the bash fallback uses monitor mode to give the # bounded child its own process group before signaling its negative pid. set -u @@ -113,6 +121,36 @@ fm_timeout_mechanism() { fi } +# True when stdin is a pipe or file. A terminal is not a redirected payload, +# and a closed stdin cannot be duplicated. +fm_timeout_stdin_is_redirected() { + [ ! -t 0 ] && ( exec 3<&0 ) 2>/dev/null +} + +# Background "$@" and keep a redirected stdin. A non-interactive bash assigns +# /dev/null to an async command's stdin, then applies explicit redirections, so +# 0<&3 restores the dup. The group redirection is undone when the group ends, +# and 3<&- keeps the spare fd out of the child. A terminal or closed stdin gets +# an explicit /dev/null: under set -m (the bash mechanism) bash skips its +# implicit one, which would hand the terminal to a background process group. +fm_timeout_background() { + if fm_timeout_stdin_is_redirected; then + { "$@" 0<&3 3<&- & } 3<&0 + else + "$@" "$status_file" + exit "$command_rc" +} + fm_run_bash_timeout() { local seconds=$1 command_status deadline_status child_pid watchdog_pid command_rc recorded_rc monitor_was_on=0 timeout_parent timer_pid shift @@ -122,13 +160,7 @@ fm_run_bash_timeout() { [ -z "${FM_TIMEOUT_OWNER_PID:-}" ] || timeout_parent=${FM_EXEC_TIMED_OWNER_PID:-$FM_TIMEOUT_OWNER_PID} case $- in *m*) monitor_was_on=1 ;; esac set -m - ( - set +m - "$@" - command_rc=$? - printf '%s\n' "$command_rc" > "$command_status" - exit "$command_rc" - ) <&0 & + fm_timeout_background fm_bash_timeout_child "$command_status" "$@" child_pid=$! ( set +m @@ -179,21 +211,22 @@ fm_run_external_timeout() { # Run the executable path asynchronously (never a bare name: that is the omp # timeout builtin, and backgrounding it leaves $! empty) so its pid - also # the process-group id created by GNU/BSD timeout without --foreground - - # remains available for cleanup. + # remains available for cleanup. fm_timeout_background keeps a redirected + # stdin across that async assignment. # A shell wrapper can exit promptly on TERM while one of its descendants # ignores TERM; timeout then considers the command finished and does not send # its configured KILL. Explicitly reap that leftover group on a real timeout. # Forward stdin explicitly: a noninteractive background command otherwise # receives /dev/null when job control is off. # shellcheck disable=SC2016 # Expansion is deliberately deferred to the child shell. - "$runner" -k 1 "$seconds" bash -c ' + fm_timeout_background "$runner" -k 1 "$seconds" bash -c ' status_file=$1 shift "$@" command_rc=$? printf "%s\n" "$command_rc" > "$status_file" exit "$command_rc" - ' _ "$status_file" "$@" <&0 & + ' _ "$status_file" "$@" runner_pid=$! if wait "$runner_pid"; then runner_rc=0 @@ -249,8 +282,81 @@ fm_run_timed() { # fm_run_external_timeout "$timeout_path" "$seconds" "$@" ;; perl) - perl -e 'my $t = shift; my $pid = fork; die "fork failed" unless defined $pid; if (!$pid) { setpgrp(0, 0); exec { $ARGV[0] } @ARGV; exit 127 } local $SIG{ALRM} = sub { kill "TERM", -$pid; select undef, undef, undef, 0.2; kill "KILL", -$pid; exit 124 }; alarm $t; waitpid $pid, 0; exit(($? & 127) ? 128 + ($? & 127) : $? >> 8)' \ - "$seconds" "$@" + local saved_traps='' caller_trap signal runner_pid='' interrupted='' runner_rc monitor_was_on=0 + # Bash 4.3+ drops inherited inactive trap strings on any trap change. + # Older Bash retains them across a reset, so save only strings it clears. + trap - KILL + for signal in INT HUP TERM; do + caller_trap=$(trap -p "$signal") + trap - "$signal" + if [ "$caller_trap" != "$(trap -p "$signal")" ]; then + saved_traps="$saved_traps +$caller_trap" + fi + done + # wait on an asynchronous runner so Bash handles a process-directed signal + # immediately instead of deferring the trap until a foreground Perl exits. + trap 'interrupted=INT; [ -z "$runner_pid" ] || kill -INT "$runner_pid" 2>/dev/null || true' INT + trap 'interrupted=HUP; [ -z "$runner_pid" ] || kill -HUP "$runner_pid" 2>/dev/null || true' HUP + trap 'interrupted=TERM; [ -z "$runner_pid" ] || kill -TERM "$runner_pid" 2>/dev/null || true' TERM + case $- in *m*) monitor_was_on=1 ;; esac + set -m + perl -MPOSIX=WNOHANG,setpgid -MTime::HiRes=clock_gettime,CLOCK_MONOTONIC -e ' + my $bound = shift; + my ($signal, $kill_at, $result) = (0, 0, 0); + for my $entry ([HUP => 1], [INT => 2], [TERM => 15]) { + my ($name, $number) = @$entry; + $SIG{$name} = sub { $signal ||= $number }; + } + my $pid = fork; + exit 127 unless defined $pid; + if (!$pid) { + $SIG{$_} = "DEFAULT" for qw(INT HUP TERM); + setpgid(0, 0); + exec { $ARGV[0] } @ARGV; + exit 127; + } + # Both sides establish the group before the parent can signal it. + setpgid($pid, $pid); + my $deadline = clock_gettime(CLOCK_MONOTONIC) + $bound; + while (1) { + if (!$kill_at && ($signal || clock_gettime(CLOCK_MONOTONIC) >= $deadline)) { + $result = $signal ? 128 + $signal : 124; + $kill_at = clock_gettime(CLOCK_MONOTONIC) + 0.2; + kill "TERM", -$pid; + } + my $done = waitpid $pid, WNOHANG; + if ($done == $pid) { + my $status = $?; + kill "KILL", -$pid if $kill_at; + exit($kill_at ? $result : ($status & 127) ? 128 + ($status & 127) : $status >> 8); + } + exit 127 if $done == -1; + if ($kill_at && clock_gettime(CLOCK_MONOTONIC) >= $kill_at) { + kill "KILL", -$pid; + waitpid $pid, 0; + exit $result; + } + select undef, undef, undef, 0.02; + } + ' -- "$seconds" "$@" <&0 & + runner_pid=$! + [ "$monitor_was_on" -eq 1 ] || set +m + [ -z "$interrupted" ] || kill -"$interrupted" "$runner_pid" 2>/dev/null || true + while :; do + if wait "$runner_pid"; then runner_rc=0; else runner_rc=$?; fi + # A trap interrupts wait before the runner has finished its cleanup. + kill -0 "$runner_pid" 2>/dev/null || break + done + trap - INT HUP TERM + eval "$saved_traps" + [ -z "$interrupted" ] || kill -s "$interrupted" "$(exec perl -e 'print getppid()')" + case "$interrupted" in + INT) return 130 ;; + HUP) return 129 ;; + TERM) return 143 ;; + esac + return "$runner_rc" ;; bash) fm_run_bash_timeout "$seconds" "$@" ;; *) return 124 ;; diff --git a/bin/fm-tool-update-check.sh b/bin/fm-tool-update-check.sh index 10a833d1ab4..e8ddd075bec 100755 --- a/bin/fm-tool-update-check.sh +++ b/bin/fm-tool-update-check.sh @@ -35,6 +35,9 @@ # manager's "latest" directory can hold an older build. A copy that will not # report a version is reported as a check failure rather than assumed current. # +# Published versions are read from the public npm registry or GitHub latest +# release API with curl and jq. No vendor CLI update command is invoked. +# # What this script never does: it reports, and it repairs nothing. It does not # install, update, uninstall, reorder PATH, or touch any version manager's # configuration, and it never fetches into a watched git repository. Every git @@ -160,8 +163,7 @@ if [ "${#BUDGET_SECS}" -gt 3 ] || [ "$BUDGET_SECS" -lt 1 ] || [ "$BUDGET_SECS" - exit 2 fi -# The smallest bound a probe can be given, because fm_run_timed treats a -# non-positive bound as no bound. +# The smallest probe bound under bin/fm-timeout-lib.sh's bound contract. PROBE_MIN_SECS=1 # Both clocks here count whole seconds, so a probe can start when the arithmetic # says a second is left while almost none of it really is, and it still gets a @@ -241,7 +243,7 @@ budget_allows() { # The bound for one probe: the probe bound, cut down to whatever the sweep # budget has left, so no probe can run past the end of the sweep. Never below -# PROBE_MIN_SECS, because fm_run_timed treats a non-positive bound as no bound. +# PROBE_MIN_SECS, per bin/fm-timeout-lib.sh's bound contract. probe_bound() { local left left=$((DEADLINE - $(real_epoch))) @@ -273,11 +275,15 @@ version_newer() { IFS=. read -r -a bp <<< "$b" i=0 while [ "$i" -lt "${#ap[@]}" ] || [ "$i" -lt "${#bp[@]}" ]; do - left=$((10#${ap[i]:-0})) - right=$((10#${bp[i]:-0})) - if [ "$left" -gt "$right" ]; then + left=${ap[i]:-0} + right=${bp[i]:-0} + # Compare decimal strings so untrusted large components cannot overflow + # shell arithmetic. Leading zeroes and omitted components are insignificant. + while [ "${#left}" -gt 1 ] && [ "${left#0}" != "$left" ]; do left=${left#0}; done + while [ "${#right}" -gt 1 ] && [ "${right#0}" != "$right" ]; do right=${right#0}; done + if [ "${#left}" -gt "${#right}" ] || { [ "${#left}" -eq "${#right}" ] && [[ "$left" > "$right" ]]; }; then return 0 - elif [ "$left" -lt "$right" ]; then + elif [ "$left" != "$right" ]; then return 1 fi i=$((i + 1)) @@ -351,6 +357,16 @@ config_validate() { elif ($t | has("git")) and (($t.git.repo | type) != "string" or ($t.git.repo | startswith("/") | not) or ($t.git.repo | test("[[:cntrl:]]"))) then "tool \($t.name) git.repo must be an absolute path on one line" elif ($t | has("git")) and ($t.git | has("remote")) and (($t.git.remote | type) != "string" or ($t.git.remote | test("\\A[A-Za-z0-9._-]+\\z") | not)) then "tool \($t.name) git.remote must be a simple remote name" elif ($t | has("git")) and ($t.git | has("branch")) and (($t.git.branch | type) != "string" or ($t.git.branch | test("\\A[A-Za-z0-9._/-]+\\z") | not)) then "tool \($t.name) git.branch must be a simple branch name" + elif ($t | has("published")) and (($t.published | type) != "object") then "tool \($t.name) published must be an object" + elif ($t | has("published")) and (($t | has("command")) | not) then "tool \($t.name) published needs command to report the installed version" + elif ($t | has("published")) and ($t | has("version_url")) then "tool \($t.name) may set published or version_url, not both" + elif ($t | has("published")) and ($t.published.source != "npm" and $t.published.source != "github") then "tool \($t.name) published.source must be npm or github" + elif ($t | has("published")) and $t.published.source == "npm" and + (($t.published.package | type) != "string" or ($t.published.package | test("^(@[a-z0-9][a-z0-9._-]*/)?[a-z0-9][a-z0-9._-]*$") | not)) then "tool \($t.name) published.package must be an npm package name" + elif ($t | has("published")) and $t.published.source == "github" and + (($t.published.repo | type) != "string" or ($t.published.repo | test("^[A-Za-z0-9][A-Za-z0-9-]*/[A-Za-z0-9_.-]+$") | not)) then "tool \($t.name) published.repo must be a GitHub owner/repo" + elif ($t | has("published")) and + (($t.published | keys) - (if $t.published.source == "npm" then ["source", "package"] else ["source", "repo"] end) | length) > 0 then "tool \($t.name) published has unsupported fields" else empty end; def problems: @@ -392,6 +408,8 @@ config_records() { (.git.repo // ""), (.git.remote // "origin"), (.git.branch // ""), + (.published.source // ""), + (.published.package // .published.repo // ""), (.version_url // "") ] | join("\u001f") ' "$CONFIG" 2>/dev/null @@ -426,11 +444,14 @@ probe_output() { fm_run_timed "$(probe_bound)" "$path" "$@" 2>&1 } +COMMAND_VERSION= + command_findings() { - local name=$1 command_name=$2 args_joined=$3 announce=$4 announce_args=$5 version_url=${6:-} - local hit out version matched announce_out status matched_line announced_version published + local name=$1 command_name=$2 args_joined=$3 announce=$4 announce_args=$5 published=$6 version_url=${7:-} + local hit out version matched announce_out status matched_line announced_version url_version local resolved_path='' resolved_version='' resolved_out='' resolved_failed=0 - local best_path='' best_version='' unreadable='' hits='' + local best_path='' best_version='' best_status=0 unreadable='' hits='' + COMMAND_VERSION= # This tool's announcement source is dead if its pattern cannot be used, which # is reported here, for this tool alone, so the rest of the sweep still runs. @@ -475,6 +496,7 @@ command_findings() { if [ -z "$best_version" ] || version_newer "$version" "$best_version"; then best_version=$version best_path=$hit + best_status=$status fi done </dev/null) status=$? - published=$(parse_version "$out") - if [ "$status" -ne 0 ] || [ -z "$published" ]; then + url_version=$(parse_version "$out") + if [ "$status" -ne 0 ] || [ -z "$url_version" ]; then emit "$name check failed: $version_url did not report a version" - elif version_newer "$published" "$best_version"; then - emit "$name update available: $published published at $version_url, PATH resolves $resolved_version" + elif version_newer "$url_version" "$best_version"; then + emit "$name update available: $url_version published at $version_url, PATH resolves $resolved_version" fi fi fi @@ -566,6 +594,59 @@ EOF return 0 } +# --- published release probes ----------------------------------------------- + +published_findings() { + local name=$1 source=$2 package=$3 installed=$4 + local url field version status bound + # command_findings already reports a missing or unreadable resolved command. + [ -n "$installed" ] || return 0 + budget_allows "$name published source" || return 0 + if ! command -v curl >/dev/null 2>&1; then + emit "$name check failed: curl is required to read its published source" + return 0 + fi + case "$source" in + npm) + # Encode scoped package names as one URL path component. + url="https://registry.npmjs.org/$(jq -rn --arg package "$package" '$package | @uri')/latest" + field=version + ;; + github) + url="https://api.github.com/repos/$package/releases/latest" + field=tag_name + ;; + esac + bound=$(probe_bound) + # -q must be first: ignore curlrc (credentials, output files, or write methods). + # No credentials, redirects, retries, or stdin; HTTP errors are failed probes. + # Keep HTTP and JSON parsing in the same bound, including blocking DNS builds. + # Only the documented version field counts, never dotted text in release notes + # or errors. Exit 65 distinguishes an unsupported response from a failed read. + # shellcheck disable=SC2016 # Expanded by the bounded child shell and jq. + version=$(fm_run_timed "$bound" bash -c ' + body=$(curl -q --fail --silent --show-error \ + --proto "=https" --connect-timeout "$1" --max-time "$1" \ + --header "Accept: application/json" --url "$2") || exit "$?" + version=$(printf "%s" "$body" | jq -ser --arg field "$3" "$4") || exit 65 + [ -n "$version" ] || exit 65 + printf "%s\n" "$version" + ' published-probe "$bound" "$url" "$field" \ + 'select(length == 1) | .[0][$field] | strings | select(test("^v?[0-9]+(\\.[0-9]+)+$"))' /dev/null) + status=$? + if [ "$status" -eq 65 ]; then + emit "$name check failed: published source $url did not report a supported version" + return 0 + elif [ "$status" -ne 0 ]; then + emit "$name check failed: published source $url could not be reached or read (exit $status)" + return 0 + fi + version=${version#v} + if version_newer "$version" "$installed"; then + emit "$name update available: installed $installed, published $version at $url" + fi +} + # --- git probes ------------------------------------------------------------- # A probe the sweep budget can no longer afford is never issued, and says so with @@ -754,7 +835,7 @@ record_write() { action_check() { local name command_name args_joined announce announce_args repo remote branch version_url - local line now + local line now source package [ -f "$CONFIG" ] || return 0 @@ -774,10 +855,12 @@ action_check() { if ! config_validate; then emit "watched tool registry: $CONFIG_PROBLEM" else - while IFS=$FIELD_SEP read -r name command_name args_joined announce announce_args repo remote branch version_url; do + while IFS=$FIELD_SEP read -r name command_name args_joined announce announce_args repo remote branch source package version_url; do [ -n "$name" ] || continue budget_allows "$name" || break - [ -z "$command_name" ] || command_findings "$name" "$command_name" "$args_joined" "$announce" "$announce_args" "$version_url" + COMMAND_VERSION= + [ -z "$command_name" ] || command_findings "$name" "$command_name" "$args_joined" "$announce" "$announce_args" "$source" "$version_url" + [ -z "$source" ] || published_findings "$name" "$source" "$package" "$COMMAND_VERSION" [ -z "$repo" ] || git_findings "$name" "$repo" "$remote" "$branch" done < <(config_records) fi diff --git a/bin/fm-turnend-guard-cursor.sh b/bin/fm-turnend-guard-cursor.sh index 1baf52f9add..613a8e36b1e 100755 --- a/bin/fm-turnend-guard-cursor.sh +++ b/bin/fm-turnend-guard-cursor.sh @@ -262,14 +262,14 @@ current_session_still_ours() { fm_session_lock_owned_by_self "$STATE" } -# Only the lock-owning session may arm or wake. A prior session that died -# leaving its harness identity behind, a local pid or a Windows-tagged one, is -# the one recoverable case, delegated to bin/fm-lock.sh so acquisition keeps its -# single owner. +# Only the lock-owning session may arm or wake. The shared +# fm_session_lock_pid_blocks_reclaim verdict decides whether a recorded owner may +# be reclaimed, and any reclaim is delegated to bin/fm-lock.sh so acquisition +# keeps its single owner. if ! fm_session_lock_owned_by_self "$STATE"; then LOCK_PID=$(cat "$STATE/.lock" 2>/dev/null || true) fm_session_pid_valid "$LOCK_PID" || exit 0 - fm_harness_pid_alive "$LOCK_PID" && exit 0 + fm_session_lock_pid_blocks_reclaim "$LOCK_PID" && exit 0 "$SCRIPT_DIR/fm-lock.sh" >/dev/null 2>&1 || exit 0 fm_session_lock_owned_by_self "$STATE" || exit 0 fi diff --git a/bin/fm-turnend-guard.sh b/bin/fm-turnend-guard.sh index 4648d711387..ec22382df1b 100755 --- a/bin/fm-turnend-guard.sh +++ b/bin/fm-turnend-guard.sh @@ -66,7 +66,7 @@ # auto-arm (bin/fm-claude-stop-autoarm.sh), which fires on the same Stop event: # 1. a live identity-matched watcher with a fresh beacon - or, in away mode, a # live identity-matched daemon with a fresh beacon - allows immediately; -# 2. an unhealthy session with a verified live session-lock owner it does not +# 2. an unhealthy session with a live session-lock owner it does not # own under the shared ancestry-or-trusted-id verdict exits with a read-only # diagnostic instead of blocking a session that cannot repair supervision # without stealing ownership; @@ -319,9 +319,10 @@ block_stop() { exit 2 } -# Another verified live session owns the home lock under the shared -# ancestry-or-trusted-id verdict. This session is read-only and cannot arm or -# repair supervision without +# Another live session owns the home lock under the shared +# `fm_session_lock_pid_blocks_reclaim` verdict and is neither this session's +# harness ancestry nor its trusted id. +# This session is read-only and cannot arm or repair supervision without # stealing ownership, so blocking its Stop would create an impossible loop. # Report the ownership conflict as a diagnostic and let this turn end safely; # the owning session remains responsible for restoring the watcher. diff --git a/bin/fm-vendor-auth-probe.sh b/bin/fm-vendor-auth-probe.sh index c4edc0e45ba..9f0e4512faf 100755 --- a/bin/fm-vendor-auth-probe.sh +++ b/bin/fm-vendor-auth-probe.sh @@ -59,8 +59,8 @@ # Environment: # FM_VENDOR_AUTH_PROBE_TIMEOUT hard per-command bound in seconds; must be a # positive integer, otherwise the default 20 is -# used. Zero is rejected because `timeout 0` and -# `alarm 0` both mean "no deadline". +# used. See bin/fm-timeout-lib.sh for the shared +# bound contract. set -u VERIFIED_GROK_VERSION=0.2.117 @@ -122,8 +122,7 @@ done [ -n "$PROBE" ] || die_usage "a probe name is required" -# A non-positive bound is not a bound: `timeout 0` and the Perl fallback's -# `alarm 0` both disable the deadline, so a hung vendor CLI would run unbounded. +# Apply the shared bound contract from bin/fm-timeout-lib.sh. TIMEOUT=${FM_VENDOR_AUTH_PROBE_TIMEOUT:-20} case "$TIMEOUT" in ''|*[!0-9]*|0*) TIMEOUT=20 ;; diff --git a/bin/fm-wake-drain.sh b/bin/fm-wake-drain.sh index 6a27a68f5aa..0610b11d943 100755 --- a/bin/fm-wake-drain.sh +++ b/bin/fm-wake-drain.sh @@ -533,8 +533,7 @@ print_record_divergence_section() { local diverged task origin key title line shown=0 omitted=0 bound local output='' used=0 bytes item_bytes=220 global_bytes=2000 - # A non-positive bound is not a bound (bin/fm-timeout-lib.sh), so a bad - # override falls back to the default rather than disabling the deadline. + # Apply bin/fm-timeout-lib.sh's bound contract, defaulting a bad override. bound=${FM_DIVERGENCE_TIMEOUT:-20} case "$bound" in ''|*[!0-9]*|0) bound=20 ;; esac diff --git a/bin/fm-wake-lib.sh b/bin/fm-wake-lib.sh index ce88da0d71d..e735ad78ee4 100755 --- a/bin/fm-wake-lib.sh +++ b/bin/fm-wake-lib.sh @@ -1655,8 +1655,8 @@ _fm_lock_acquire_wait_handoff() { # # fm_lock_acquire_wait_bounded # # Bounded acquire variant. It preserves the ordinary wait/reclaim behavior -# until fm-timeout-lib.sh's hard deadline, returns 124 when a live holder still -# owns the lock, and leaves FM_LOCK_HELD_PID naming that holder. +# until fm-timeout-lib.sh's hard deadline, returns 124 for retriable contention, +# and leaves FM_LOCK_HELD_PID naming a live holder when one is known. # Use it where a caller must refuse rather than block: wake presentation, and # the guarded remote link clear, whose whole contract is to return a # reconciliation refusal instead of wedging an unattended close. @@ -1696,18 +1696,21 @@ fm_lock_acquire_wait_bounded() { fi if [ "$rc" -eq 124 ]; then owner_pid=$(cat "$lockdir/pid" 2>/dev/null || true) + # shellcheck disable=SC2034 # Output read by callers after bounded acquisition. + FM_LOCK_HELD_PID= case "$owner_pid" in - ''|*[!0-9]*|0) ;; + '') + fm_lock_parent_gone "$lockdir" && return 1 + if [ -e "$lockdir" ] || [ -L "$lockdir" ]; then [ -d "$lockdir" ] || return 1; fi + ;; + *[!0-9]*|0) return 1 ;; *) if [ "$owner_pid" -gt 0 ] 2>/dev/null && fm_lock_owner_alive "$lockdir" "$owner_pid"; then FM_LOCK_HELD_PID=$owner_pid - return 124 fi ;; esac - # shellcheck disable=SC2034 # Output read by callers after bounded acquisition. - FM_LOCK_HELD_PID= - return 1 + return 124 fi return "$rc" } @@ -2302,13 +2305,29 @@ fm_autoarm_midturn_healthy() { # [grace] [ "$epoch_mtime" -ge "$beacon_mtime" ] } +# Match an interrupted episode to the current lock's process identity AND +# recorded session id. A changed anchor is still the same session only when +# the existing trusted-session ownership proof verifies that recorded id. +fm_autoarm_session_matches() { # [recorded-session-id] + local state=$1 identity=$2 session_id=${3:-} lock_pid current recorded + [ -n "$identity" ] || return 1 + _fm_wake_require_session_lock + lock_pid=$(sed -n '1p' "$state/.lock" 2>/dev/null || true) + current=$(fm_session_pid_identity "$lock_pid" 2>/dev/null) || return 1 + [ -n "$current" ] || return 1 + recorded=$(fm_session_lock_recorded_session_id "$state" 2>/dev/null || true) + [ "$session_id" = "$recorded" ] || return 1 + [ "$identity" = "$current" ] && return 0 + [ -n "$session_id" ] && fm_session_lock_same_session "$state" +} + # Atomically publish this process as the owner of generation N+1, under one # short micro-mutex hold. Returns 0 with FM_AUTOARM_MY_GEN set on success, 2 # when a competing claimant won the race (the ledger holds an open claim), and # 1 when the micro-mutex is contended, the mandatory identity cannot be # computed, or the write failed. fm_autoarm_claim_next() { # [grace] - local state=$1 grace=${2:-${FM_GUARD_GRACE:-300}} lock epoch pid gen identity session_id lock_pid lock_identity tmp + local state=$1 grace=${2:-${FM_GUARD_GRACE:-300}} lock epoch pid gen identity session_id lock_pid lock_identity tmp interrupted_identity interrupted_session lock="$state/.claude-autoarm.lock" epoch="$state/.claude-autoarm-epoch" FM_AUTOARM_MY_GEN= @@ -2331,6 +2350,18 @@ fm_autoarm_claim_next() { # [grace] fm_lock_release "$lock" return 2 fi + # Only interrupted hooks bind their notice to a session. A new lock holder + # retires that episode before its first claim, so a teardown cannot consume + # the replacement's real failure notice. Empty legacy markers stay unchanged. + interrupted_identity=$(sed -n 's/^session_identity=//p' "$state/.claude-autoarm-failure-notified" 2>/dev/null || true) + interrupted_session=$(sed -n 's/^session_id=//p' "$state/.claude-autoarm-failure-notified" 2>/dev/null || true) + if [ -n "$interrupted_identity" ] && [ -n "$lock_identity" ] \ + && ! fm_autoarm_session_matches "$state" "$interrupted_identity" "$interrupted_session"; then + if ! fm_failure_episode_reset "$state"; then + fm_lock_release "$lock" + return 1 + fi + fi gen=$(_fm_autoarm_epoch_field "$epoch" epoch 2>/dev/null || true) case "$gen" in ''|*[!0-9]*) gen=0 ;; @@ -2360,10 +2391,13 @@ fm_autoarm_claim_next() { # [grace] # owned critical section (the once-per-episode failure notice). A marker failure # refuses the commit even though its terminal ledger entry remains; marker-first # ordering could permanently suppress a notice whose ledger write never won. +# The optional interrupted-session identity and recorded id are checked under +# the hold and written into the marker, allowing claim_next to retire it when +# another session takes the lock without resetting a verified same-session move. # Returns 0 committed, 2 refused (superseded or required-marker failure), and 1 # unable (bounded contention or ledger-write failure). -fm_autoarm_write_owned() { # [marker-file] [session-pid] [recovery-generation] - local state=$1 gen=$2 outcome=$3 marker=${4:-} session=${5:-} recovery=${6:-} lock i rc +fm_autoarm_write_owned() { # [marker-file] [session-pid] [recovery-generation] [interrupted-session-identity] [interrupted-session-id] + local state=$1 gen=$2 outcome=$3 marker=${4:-} session=${5:-} recovery=${6:-} marker_identity=${7:-} marker_session=${8:-} lock i rc lock="$state/.claude-autoarm.lock" i=0 while ! fm_lock_try_acquire "$lock"; do @@ -2371,7 +2405,7 @@ fm_autoarm_write_owned() { # [marker-file] [session sleep 0.02 i=$((i + 1)) done - _fm_autoarm_write_owned_locked "$state" "$gen" "$outcome" "$marker" "$session" "$recovery" + _fm_autoarm_write_owned_locked "$state" "$gen" "$outcome" "$marker" "$session" "$recovery" "$marker_identity" "$marker_session" rc=$? fm_lock_release "$lock" return "$rc" @@ -2382,8 +2416,8 @@ fm_autoarm_write_owned() { # [marker-file] [session # ledger write and marker create share one owned critical section with # whatever the caller already serialized. Returns 0 on commit, 1 on a failed # write, 2 on refusal (superseded generation or failed required marker). -_fm_autoarm_write_owned_locked() { # [marker-file] [session-pid] [recovery-generation] - local state=$1 gen=$2 outcome=$3 marker=${4:-} session=${5:-} recovery=${6:-} epoch pid identity tmp +_fm_autoarm_write_owned_locked() { # [marker-file] [session-pid] [recovery-generation] [interrupted-session-identity] [interrupted-session-id] + local state=$1 gen=$2 outcome=$3 marker=${4:-} session=${5:-} recovery=${6:-} marker_identity=${7:-} marker_session=${8:-} epoch pid identity tmp marker_content epoch="$state/.claude-autoarm-epoch" pid=${BASHPID:-$$} if ! fm_autoarm_ledger_read "$state" \ @@ -2391,6 +2425,9 @@ _fm_autoarm_write_owned_locked() { # [marker-file] return 2 fi identity=$FM_AUTOARM_IDENTITY + if [ -n "$marker_identity" ] && ! fm_autoarm_session_matches "$state" "$marker_identity" "$marker_session"; then + return 2 + fi tmp="$epoch.tmp.$pid" if ! { printf 'epoch=%s owner_pid=%s outcome=%s updated_at=%s' \ @@ -2403,7 +2440,9 @@ _fm_autoarm_write_owned_locked() { # [marker-file] rm -f "$tmp" 2>/dev/null || true return 1 fi - [ -z "$marker" ] || : > "$marker" 2>/dev/null || return 2 + marker_content= + [ -z "$marker_identity" ] || printf -v marker_content 'session_identity=%s\nsession_id=%s\n' "$marker_identity" "$marker_session" + [ -z "$marker" ] || printf '%s' "$marker_content" > "$marker" 2>/dev/null || return 2 return 0 } @@ -2846,6 +2885,12 @@ fm_wake_print_deduped() { if ($3 == "heartbeat") { dedupe = "heartbeat" } + # A check payload is the event itself, not a newer view of one state, + # and the acknowledgement consumes every row it hides: collapse only + # identical check rows. + if ($3 == "check") { + dedupe = dedupe SUBSEP $5 + } if (!(dedupe in seen)) { order[++count] = dedupe seen[dedupe] = 1 diff --git a/bin/fm-watch-key-lib.sh b/bin/fm-watch-key-lib.sh new file mode 100644 index 00000000000..3588e7873ae --- /dev/null +++ b/bin/fm-watch-key-lib.sh @@ -0,0 +1,32 @@ +#!/usr/bin/env bash +# Shared watcher marker identity. Source this library, then call window_key +# with one backend endpoint to obtain its filename suffix. +# +# Every per-window watcher marker consumes this ONE derivation. SHA-256 of the +# endpoint bytes with a `v3.` prefix bounds the suffix to 67 ASCII bytes, leaving +# room for marker prefixes within a 255-byte filename. The dot cannot occur in a +# legacy key; legacy and v2 evidence is ignored, never migrated or revived. +# Identity depends only on endpoint bytes, never on other recorded windows. +# docs/configuration.md's watcher-marker upgrade note owns the intentional +# one-time cache reset and its notification-only effects. +# +# Obsolete markers are not garbage-collected automatically. For optional cleanup, +# stop both the watcher and away daemon, then archive the obsolete files outside +# STATE (or remove the reviewed files). Limit this to per-endpoint marker families: +# .hash-, .count-, .stale-, .stale-since-, .churn-since-, .dead-reported-, +# .wedge-escalations-, .wedge-permanent-, .wedge-terminal-queued-, +# .wedge-rollback-failed-, .paused-, .paused-rechecked-, .paused-resurfaced-, +# .waiting-resurfaced-, .writing-since-, .writing-resurfaced-, .validating-since-, +# .validating-resurfaced-, and .guard-deferrals-. Preserve every current filename +# containing -v3.; only legacy/v2 suffixes in those families are obsolete. +# Do not bulk-clear STATE: task records, wake queues, and task-keyed .subsuper-* +# markers are outside this cleanup. Leaving obsolete files in place is safe. +window_key() { # + local digest + if command -v shasum >/dev/null 2>&1; then + digest=$(printf '%s' "$1" | shasum -a 256) + else + digest=$(printf '%s' "$1" | sha256sum) + fi + printf 'v3.%s' "${digest%% *}" +} diff --git a/bin/fm-watch.sh b/bin/fm-watch.sh index c02a1bb3f21..d10b39b9dd6 100755 --- a/bin/fm-watch.sh +++ b/bin/fm-watch.sh @@ -500,21 +500,9 @@ window_label() { [ -n "$task" ] && printf 'fm-%s' "$task" } -# The ONE derivation of a window's per-window marker key: `:`, `/` and `.` become -# `_` so a window name is usable as a filename suffix. Every per-window file the -# watcher keeps is named by it (.hash-, .count-, .stale-, .stale-since-, -# .wedge-escalations-, .wedge-permanent-, .wedge-terminal-queued-, -# .wedge-rollback-failed-, .paused-*, .writing-*, .validating-*, -# .waiting-*), and live homes -# hold those markers on disk under the current format, so the format lives here -# alone: a second copy is how a future change to it silently orphans a window's -# markers instead of clearing them. The helpers below take the derived key -# rather than re-deriving it, so one poll of one window derives it once. -window_key() { # - local key=${1//:/_} - key=${key//\//_} - printf '%s' "${key//./_}" -} +# All per-window marker identities come from this shared owner. +# shellcheck source=bin/fm-watch-key-lib.sh +. "$SCRIPT_DIR/fm-watch-key-lib.sh" inbox_steer_escalate_unavailable() { # local w=$1 task=$2 rec=$3 reason @@ -692,8 +680,8 @@ inbox_steer_check_active() { # # The evidence is the one the pane-staleness backbone below already trusts for # liveness: this compares a fresh capture against the .hash- marker that backbone -# recorded on the previous poll, which is why the derivation lives here with the -# marker format rather than in the shared classifier. Absorbing here DEFERS a wake +# recorded on the previous poll; fm-watch-key-lib.sh owns its marker identity. +# Absorbing here DEFERS a wake # rather than swallowing it, and the deferral is BOUNDED: a task's turn-ends may # ride churn evidence for at most FM_TURNEND_CHURN_ABSORB_SECS, tracked per window # in .churn-since-, after which the wake surfaces and the window restarts. The @@ -4192,7 +4180,9 @@ EOF continue fi fi - reason="check: $c: $out" + # One line, as the queued row stores it: wake consumers keep only + # prefixed lines, so a later output line would otherwise be dropped. + reason="check: $c: $(printf '%s' "$out" | fm_wake_clean_field)" if [ "$is_pr_poll" -eq 1 ] && [ "$out" = merged ]; then if [ "$(fm_meta_get "$STATE/$id.meta" kind)" = secondmate ]; then # A merge poll armed on a secondmate is residue: the mate is a diff --git a/bin/fm-worker-account-lib.sh b/bin/fm-worker-account-lib.sh index 657cb9b7cd7..868887686a1 100644 --- a/bin/fm-worker-account-lib.sh +++ b/bin/fm-worker-account-lib.sh @@ -13,8 +13,8 @@ # claude CLAUDE_CONFIG_DIR config/claude-account # pi, pi-signed PI_CODING_AGENT_DIR config/pi-account # -# The pin is opt-in: an absent file is no pin, and the launch keeps today's -# ambient behavior byte for byte. A present file must resolve, or the launch +# With neither a pin nor the Claude account list described below, the launch +# keeps ambient behavior byte for byte. A present pin must resolve, or the launch # refuses; nothing falls back to an ambient or vendor-default login once a # home has declared one. `ordinary` selects the vendor default: for Claude # that is CLAUDE_CONFIG_DIR unset, because Claude reads $CLAUDE_CONFIG_DIR/ @@ -53,6 +53,20 @@ # the pin. Pi ranks a root's stored credentials above environment variables, # and the check refuses a provider the root has not stored, so a pinned Pi # launch unsets nothing. +# +# A home that spends several Claude subscriptions declares them instead in +# config/claude-accounts: one entry per line, each `ordinary` or an absolute +# path exactly as config/claude-account would hold it, with no blank lines, +# duplicates, or control characters, and an optional final newline. The list is +# opt-in in the same way: absent, nothing here changes. Present, it replaces the +# home-wide pin, which must then be absent, and every Claude ship or scout +# launch names one declared entry with --account; Firstmate chooses it at +# intake (bin/fm-claude-account-quota.sh measures each entry) and never +# defaults to the first entry, another entry, or the ambient account. The named +# entry passes the same root, raw-command, and sign-in refusals as a pin and is +# recorded as account=, which a ship or scout relaunch reuses unless an +# explicit --account moves it. A local secondmate's agent never draws from the +# list: it needs its own per-secondmate pin. # shellcheck source=bin/fm-timeout-lib.sh . "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/fm-timeout-lib.sh" @@ -259,6 +273,70 @@ fm_worker_account_resolve() { printf '%s\t%s\t%s\n' "$declared" "$root" "${token#*$'\t'}" } +# fm_worker_account_list +# Prints the entries config/claude-accounts declares, one per line, for a +# Claude launch. Prints nothing and returns 0 for another runner or when the +# list is absent. On a malformed or unreadable list prints one error naming the +# file and returns 1. +fm_worker_account_list() { + local harness=$1 cfg="$2/claude-accounts" out rc + [ "$harness" = claude ] || return 0 + out=$(perl -MErrno=ENOENT -e ' + my ($f) = @ARGV; + unless (lstat $f) { + exit 3 if $! == ENOENT; + print STDERR "error: cannot inspect configuration source at $f: $!\n"; + exit 4; + } + (-f $f && -r _) or exit 5; + open(my $fh, "<", $f) or exit 5; + my $body = do { local $/; <$fh> } // ""; + $body =~ /\A(?:(?:ordinary|\/[^\x00-\x1f\x7f]*)\n)*(?:ordinary|\/[^\x00-\x1f\x7f]*)\n?\z/ or exit 6; + my %seen; + for my $entry (split /\n/, $body) { + exit 7 if $seen{$entry}++; + print $entry, "\n"; + } + ' -- "$cfg") + rc=$? + case "$rc" in + 0) printf '%s\n' "$out" ;; + 3) return 0 ;; + 4) return 1 ;; + 5) + echo "error: config/claude-accounts must be a readable regular file: $cfg" >&2 + return 1 + ;; + 7) + echo "error: config/claude-accounts declares the same account twice: $cfg" >&2 + return 1 + ;; + *) + echo "error: config/claude-accounts must hold one account per line, each 'ordinary' or an absolute path, with no blank lines or control characters: $cfg" >&2 + return 1 + ;; + esac +} + +# fm_worker_account_relaunch_request [] +# Prints the declared account a relaunch asks fm_worker_account_select for: +# the explicit one when given, otherwise the task's recorded account when the +# home declares config/claude-accounts and a Claude ship or scout stays on +# Claude. Prints nothing otherwise, so a relaunch without a list re-reads the +# pin exactly as before. Returns 1 when the explicit account holds a control +# character or the list cannot be read. +fm_worker_account_relaunch_request() { + local harness=$1 config=$2 prior=$3 recorded=$4 explicit=$5 mate=${6:-} listed + fm_worker_account_request_valid "$explicit" || return 1 + if [ -n "$explicit" ] || [ -n "$mate" ]; then + printf '%s\n' "$explicit" + return 0 + fi + listed=$(fm_worker_account_list "$harness" "$config") || return 1 + [ -n "$listed" ] && [ "$prior" = claude ] || return 0 + printf '%s\n' "$recorded" +} + # fm_worker_account_pi_provider # Prints the provider an explicit Pi --model / names. Returns 1, # silently, for anything else, so no caller can fall back to a guess. @@ -325,30 +403,91 @@ fm_worker_account_check() { return 0 } -# fm_worker_account_select [] [] +# fm_worker_account_request_valid +# Refuses an --account value holding a control character. Declared entries +# never hold one, and a newline would otherwise split the value before the +# whole-entry membership check or the account= record sees it. +fm_worker_account_request_valid() { + case "$1" in + *[[:cntrl:]]*) + echo "error: --account must name one config/claude-accounts entry exactly, with no control characters" >&2 + return 1 + ;; + esac +} + +# fm_worker_account_select [] [] [] # The whole launch-time decision. Prints nothing for an unpinned runner, so -# the caller keeps today's launch unchanged. For a pinned one prints -# "declaredrootprovider", where provider is the Pi launch model's -# own (empty for Claude), after the model guard and the sign-in check pass. On -# refusal prints one error and returns 1. bin/fm-spawn.sh runs it before any -# endpoint exists, and bin/fm-control.sh before a relaunch stops the live -# agent. +# the caller keeps today's launch unchanged. For a pinned one, or a Claude +# ship or scout launching on a declared config/claude-accounts entry named by +# , prints "declaredrootprovider", where provider is the +# Pi launch model's own (empty for Claude), after the model guard and the +# sign-in check pass. On refusal prints one error and returns 1. +# bin/fm-spawn.sh runs it before any endpoint exists, and bin/fm-control.sh +# before a relaunch stops the live agent. fm_worker_account_select() { - local harness=$1 config=$2 model=$3 executable=$4 raw=${5:-} mate=${6:-} - local selection declared root providers word provider='' selected_config setting + local harness=$1 config=$2 model=$3 executable=$4 raw=${5:-} mate=${6:-} account=${7:-} + local selection declared root providers word provider='' selected_config setting listed count + fm_worker_account_request_valid "$account" || return 1 + if [ -n "$account" ] && [ "$harness" != claude ]; then + echo "error: --account names one of config/claude-accounts and applies to Claude workers only, not $harness" >&2 + return 1 + fi + if [ -n "$account" ] && [ -n "$mate" ]; then + echo "error: --account applies to ships and scouts; secondmate $mate launches on its own config/secondmate-accounts/$mate/claude-account pin" >&2 + return 1 + fi + listed=$(fm_worker_account_list "$harness" "$config") || return 1 + if [ -z "$listed" ] && [ -n "$account" ]; then + echo "error: --account '$account' needs config/claude-accounts, which this home does not declare" >&2 + return 1 + fi + if [ -n "$listed" ] && { [ -e "$config/claude-account" ] || [ -L "$config/claude-account" ]; }; then + echo "error: config/claude-accounts and config/claude-account are both present; keep the account list or the single pin, not both" >&2 + return 1 + fi selected_config=$(fm_worker_account_config "$harness" "$config" "$mate") || return 1 setting="config/$(fm_worker_account_file "$harness")" [ "$selected_config" = "$config" ] || setting="$selected_config/$(fm_worker_account_file "$harness")" - selection=$(fm_worker_account_resolve "$harness" "$selected_config" "$setting") || return 1 - if [ -z "$selection" ]; then - [ "$selected_config" = "$config" ] && return 0 - echo "error: secondmate account pin disappeared during selection: $setting" >&2 + if [ -n "$listed" ] && [ -n "$mate" ] && [ "$selected_config" = "$config" ]; then + echo "error: config/claude-accounts chooses accounts for ships and scouts only; secondmate $mate needs its own pin in config/secondmate-accounts/$mate/claude-account" >&2 return 1 + elif [ -n "$listed" ] && [ -z "$mate" ]; then + setting=config/claude-accounts + if [ -z "$account" ]; then + count=$(printf '%s\n' "$listed" | wc -l | tr -d ' ') + echo "error: config/claude-accounts declares $count Claude accounts, so a Claude ship or scout launch must name one with --account; bin/fm-claude-account-quota.sh measures each, and Firstmate never picks a default" >&2 + return 1 + fi + declared= + while IFS= read -r word; do + [ "$word" != "$account" ] || { declared=$account; break; } + done <<< "$listed" + [ -n "$declared" ] || { + echo "error: account '$account' is not declared in config/claude-accounts; name a declared entry with --account" >&2 + return 1 + } + declared=$account + root=$account + [ "$declared" != ordinary ] || root= + if [ -n "$root" ] && { [ ! -d "$root" ] || [ ! -r "$root" ] || [ ! -x "$root" ]; }; then + # shellcheck disable=SC2088 # Literal text for the refusal. + echo "error: $setting must name readable, searchable existing directories (ordinary means ~/.claude with CLAUDE_CONFIG_DIR unset): $root" >&2 + return 1 + fi + providers= + else + selection=$(fm_worker_account_resolve "$harness" "$selected_config" "$setting") || return 1 + if [ -z "$selection" ]; then + [ "$selected_config" = "$config" ] && return 0 + echo "error: secondmate account pin disappeared during selection: $setting" >&2 + return 1 + fi + declared=${selection%%$'\t'*} + root=${selection#*$'\t'} + providers=${root#*$'\t'} + root=${root%%$'\t'*} fi - declared=${selection%%$'\t'*} - root=${selection#*$'\t'} - providers=${root#*$'\t'} - root=${root%%$'\t'*} if [ "$harness" = claude ]; then for word in $raw; do case "$word" in diff --git a/docs/agent-control.md b/docs/agent-control.md index 8979c43e17b..83eaaba74a0 100644 --- a/docs/agent-control.md +++ b/docs/agent-control.md @@ -84,6 +84,7 @@ A relaunch does take one session reference when the endpoint's own runtime recor A recorded raw-command basename that differs from its resolved adapter cannot reproduce the command actually running, so relaunch refuses before the checkpoint unless the caller passes an explicit `--harness` to choose the replacement runtime deliberately. A harness change resets model and effort unless they are named too, because a model chosen for one adapter does not transfer to another. A Claude or Pi replacement must also pass the home's [worker account pin](configuration.md#worker-account-pin-configclaude-account-configpi-account), or a secondmate's own [per-secondmate account pin](configuration.md#per-secondmate-account-pins) when one is set, so a pin that no longer resolves or is signed out refuses before the old agent stops. + Under a declared [Claude account list](configuration.md#several-claude-accounts-configclaude-accounts), a Claude ship or scout keeps its recorded account, and `--account ` moves it to another declared one after the same check. An omp secondmate replacement likewise passes its [per-secondmate omp profile](configuration.md#per-secondmate-omp-profile) file check before the old agent stops. Ship and scout replacements also pass the [worker tool exclusion checks](configuration.md#worker-tool-exclusions-configcrew-exclude-tools) at this step. 2. **Safe checkpoint.** @@ -199,7 +200,9 @@ The worktree and the task's records are unaffected either way. - `exit`'s composer-empty check, above, is itself a fail-closed boundary that `relaunch` inherits by stopping the old agent through `exit`. - `fm-spawn --relaunch` independently refuses unless the endpoint is positively agent-free - either a `dead` endpoint that survives, or a Herdr or Rex endpoint proven gone by the absence proof above - so a replacement can never join a live agent. An `alive`, `ambiguous`, or `unreadable` verdict all refuse (a Rex `unreadable` after the absence proof cannot settle it), and so does any endpoint whose absence is not provable, which on tmux is every `missing`; absence is claimed only from positive evidence of it. - It also requires the shell to be in the recorded worktree: every backend but Orca (which owns its own task worktree with no current-path probe) gets one explicit `cd` to the recorded path, then a pre-launch path read that refuses before any harness starts unless it confirms the endpoint is sitting in the recorded copy. + For a ship or scout it also refuses, by name, a recorded worktree that is the home, the code root, or a repository's primary checkout (its git dir equals its git common dir), so a worker only ever comes back in its own task worktree. + This check lives only in the launch owner, so on the `relaunch` verb it fires after the old agent has stopped and the transaction ends as a failed launch. + It also requires the shell to be in the recorded worktree: every backend but Orca (which owns its own task worktree with no current-path probe) gets one explicit `cd` to the recorded path (Herdr, whose shell may not be ready for it, gets one resend if the path has not moved after about 10 seconds), then a pre-launch path read that refuses before any harness starts unless it confirms the endpoint is sitting in the recorded copy. ## Capability matrix diff --git a/docs/architecture.md b/docs/architecture.md index 9a18a6f1278..40f06fc88bf 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -72,7 +72,8 @@ A wake naming any status file remains governed solely by the strict authoritativ An unresolvable endpoint, an ambiguous marker key, a missing or malformed prior hash, a capture that fails or returns empty, an invalid deferral bound or deadline, or an unwritable deferral marker surfaces without clearing prior stale classification. The deferral is bounded per endpoint by `FM_TURNEND_CHURN_ABSORB_SECS`, tracked in `state/.churn-since-*`, after which the turn-end surfaces and the window restarts. That bound is load-bearing rather than cosmetic: churn and staleness read the same pane, so a pane that renders continuously - a clock, a spinner, a shell heartbeat, or a harness that leaves a background renderer alive after its agent yields - never reaches the staleness backbone's two-identical-hashes test either, and an unbounded churn absorb would leave a genuinely stopped worker behind such a renderer with no path left to surface it. -If two metadata records derive the same per-window marker key, including two records that name the same endpoint, that marker is not attributable churn evidence for either task, so the bare turn-ended wake surfaces without changing or migrating existing marker state. +[`bin/fm-watch-key-lib.sh`'s header](../bin/fm-watch-key-lib.sh) owns per-window marker identity and legacy-marker handling for both the watcher and away daemon. +If two metadata records name the same endpoint, its marker is not attributable churn evidence for either task, so the bare turn-ended wake surfaces without changing or migrating existing marker state. A `kind=secondmate` task's status stream doubles as its parent-directed reply channel, so its lines new since the last classification are read before busy evidence counts: a decision, blocker, terminal outcome, `note:`, correlation-marked line, or unknown verb always surfaces, while unmarked routine `working:` and `paused:` progress is absorbed only by the same provably-working proof an ordinary crewmate gets. Its bare turn-ended signal uses only the ordinary authoritative working proof; secondmates remain ineligible for pane-churn deferral regardless of their idle-check-in opt-in. Declared-wait episode identity, deadline handling, and marker reset mechanics are owned by [bin/fm-watch.sh](../bin/fm-watch.sh). @@ -393,6 +394,7 @@ This repo uses that setting, and its own `.no-mistakes/` directory remains local PR-based task merges go through `bin/fm-pr-merge.sh`, which records `pr=` and any available `pr_head=` through `bin/fm-pr-check.sh` before calling the forge CLI. The helper requires a full canonical URL and rejects malformed URLs or repo override flags before recording merge state. A `https://///pull/` URL ([accepted GitHub hosts](configuration.md#github-enterprise-hosts-configgithub-hosts)) requires `gh` and `jq`, and is merged only after live reads confirm the pull request is open, not a draft, mergeable, conflict-free, every unwaived check is green at the current head, and every unwaived check the base branch requires has reported at that head, then `gh pr merge` binds that verified head with `--match-head-commit`. +[`bin/fm-pr-merge.sh`](../bin/fm-pr-merge.sh)'s header owns reviewed-head pinning and explicit consent for updating a behind GitHub source branch. A required check that never reported is absent from the checks list rather than red; [`bin/fm-pr-merge.sh`](../bin/fm-pr-merge.sh)'s header owns required-context sources, producer identity, partial-read refusals, and attended check waivers. A check run is green when its current run is green, because GitHub leaves a cancelled run in the rollup beside the passing re-run it triggered when the base branch advanced; `bin/fm-pr-merge.sh`'s `github_checks_not_green` owns the rule, which uses `startedAt` to clear only an older completed check run that a passing run with the same name provably replaced, while unfinished check runs and non-green status contexts stay red. `--auto`, `--admin`, and branch-deletion flags are refused unless `--attended-override` is passed for an explicit captain instruction; that override never skips the live green check, the away-record read, or a captain hold. diff --git a/docs/calm.md b/docs/calm.md index ce63557c5db..37e9fcaca4b 100644 --- a/docs/calm.md +++ b/docs/calm.md @@ -207,7 +207,7 @@ Toggling Calm redraws every hooked row already on screen, so rows drawn before t ### Working sailboat on Claude Code While Calm is on, the stock working row (`Sauteing... (12s · 300 tokens)`) becomes the same two-row sailboat Pi draws, from the same shared sprite geometry. -The sailboat fills the row inside the transcript margin. +The sailboat fills the row inside the transcript margin, under one blank row so its sail never touches the highlighted prompt above it. It repaints on the boat's 220ms cadence, with the hull moving every 880ms. It reflows on resize, and appears and disappears exactly where the stock row would. diff --git a/docs/captain-hold-lifecycle.md b/docs/captain-hold-lifecycle.md index 57edab05d49..a4e3b307706 100644 --- a/docs/captain-hold-lifecycle.md +++ b/docs/captain-hold-lifecycle.md @@ -167,6 +167,7 @@ After cleanup, and still under the task's own lock, teardown does three things: - It records one `Deliverable of the finished work: ...` line at the end of the task body. - It copies a supported pull request or canonical `data//report.md` into the row's structured artifact fields. + A forced retention of a scout whose report was never written records no report, so the held row cannot read as a delivery. A Gerrit change or GitLab merge request URL is not a pull request tasks-axi accepts, so it appears only in the deliverable line. - It runs `tasks-axi reopen`. @@ -407,6 +408,9 @@ It preserves every captain hold in the bounded queued inventory of the owning ho An available hold is therefore never in both Captain's Call and Charted Next. An actively worked held task may also appear in Underway, which reports running work independently of those decision buckets. +Decision presentation uses the full-text field documented in [`bin/fm-bearings-snapshot.sh`](../bin/fm-bearings-snapshot.sh), preserving the recorded question and options while compact summaries remain overview-only. +Older secondmate summaries without that field fall back to the title and reason text they retained. + ### Accepted limits Three accepted limits remain deliberate: @@ -652,12 +656,13 @@ That suite drives its Lavish session through a protocol-shaped stub. It separates a resolution from the durable-transfer close and from a still-open key. It reports the last real transition across re-openings and both key positions, and treats a prose mention as no transition. -Projection regressions live in two suites: +Projection and decision-text rendering regressions live in three suites: | Suite | What it covers | | --- | --- | | `tests/fm-fleet-snapshot-view.test.sh` | The total structured-only bucket classifier, hold-until parsing, kind-independent captain actionability, undated-hold aging, and title stripping. | -| `tests/fm-bearings-snapshot.test.sh` | Default and expanded decision-bucket membership, deferral explanations, blocker-overflow disclosure, working-hold dual surfaces, remote-summary schema invalidation, exact leading-kind inference, artifact-kind mismatch and answered-question exclusion, kind-bearing and kindless local-only landings publishing their recorded note, and scout-report precedence over competing pull-request links. | +| `tests/fm-bearings-snapshot.test.sh` | Default and expanded decision-bucket membership, deferral explanations, blocker-overflow disclosure, working-hold dual surfaces, remote-summary schema invalidation, exact leading-kind inference, artifact-kind mismatch and answered-question exclusion, kind-bearing and kindless local-only landings publishing their recorded note, scout-report precedence over competing pull-request links, and full local and secondmate question transport with Unicode, report pointers, and legacy optional-field fallbacks. | +| `tests/fm-bearings-board-render.test.sh` | Complete snapshot question and option text reaching the rendered decision card, including Unicode. | ### Refreshing this record @@ -665,7 +670,7 @@ The exact commands and their summarized outputs are recorded in the shipping PR' To refresh this record, run: - The four suites above: `tests/fm-captain-hold-lifecycle.test.sh`, `tests/fm-classify-decision-key.test.sh`, `tests/fm-fleet-snapshot-view.test.sh`, and `tests/fm-bearings-snapshot.test.sh`. -- `tests/fm-send-resolve-key.test.sh`, `tests/fm-bearings-board.test.sh`, and `tests/fm-procevent.test.sh`. +- `tests/fm-send-resolve-key.test.sh`, `tests/fm-bearings-board.test.sh`, `tests/fm-bearings-board-render.test.sh`, and `tests/fm-procevent.test.sh`. - `bin/fm-lint.sh`. After a lavish-axi upgrade, run `FM_BEARINGS_LAVISH_LIVE=1 tests/fm-bearings-board-lavish-live-e2e.test.sh`. diff --git a/docs/configuration.md b/docs/configuration.md index 768bceddb5c..0ae8f1d696e 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -9,7 +9,7 @@ Start with the directory layout, then use the setting reference for the behavior | --- | --- | | Firstmate's code, private files, or project location | [FM_HOME](#fm_home) and [operational home layout](#operational-home-layout-and-state) | | Task windows and worker tools | [Runtime backend](#runtime-backend-configbackend--fm_backend) and [harness support](#harness-support) | -| Worker permissions, accounts, or environment | [Codex approval review](#codex-automatic-approval-review), [Claude permission mode](#claude-permission-mode-configclaude-permission-mode), [Codex permission mode](#codex-permission-mode-configcodex-permission-mode), [persistent managed-policy failover](#persistent-managed-policy-failover), [worker account pin](#worker-account-pin-configclaude-account-configpi-account), [worker tool exclusions](#worker-tool-exclusions-configcrew-exclude-tools), [worker launch environment](#worker-launch-environment-configlaunch-env-allowlist), and its [Orca environment source](#orca-environment-source-configlaunch-env-tmux-session) | +| Worker permissions, accounts, or environment | [Codex approval review](#codex-automatic-approval-review), [Claude permission mode](#claude-permission-mode-configclaude-permission-mode), [Codex permission mode](#codex-permission-mode-configcodex-permission-mode), [persistent managed-policy failover](#persistent-managed-policy-failover), [worker account pin](#worker-account-pin-configclaude-account-configpi-account), [several Claude accounts](#several-claude-accounts-configclaude-accounts), [worker tool exclusions](#worker-tool-exclusions-configcrew-exclude-tools), [worker launch environment](#worker-launch-environment-configlaunch-env-allowlist), and its [Orca environment source](#orca-environment-source-configlaunch-env-tmux-session) | | Backlog, preferences, and memory | [Backlog backend](#backlog-backend-taskstoml--configbacklog-backend), [captain preferences](#captain-preferences-datacaptainmd--datacaptain-sharedmd), [external memory tiers](#external-memory-tiers-memory-layering-section), [record export](#record-export-configrecord-export-root), and [startup memory budget](#startup-memory-budget-configstartup-memory-budget) | | Supervision and presentation | [Pi supervision branch](#pi-supervision-branch), [supervision host](#supervision-host-configsupervision-host), [Codex native Stop park](#codex-native-stop-park-configcodex-stop-park), [warm-cache compaction](#warm-cache-compaction-configwarm-compact), [ready-session timeout](#ready-session-timeout-configready-session-timeout), and [Calm preference](#calm-preference-configcalm) | | Persistent secondmates | [Secondmate routes](#secondmate-routes-datasecondmatesmd) | @@ -680,14 +680,20 @@ The local `config/herdr-presentation-spaces` file instead opts a home out of, or The setting is inherited into secondmate homes under the primary-authoritative contract owned by [`secondmate-provisioning`](../.agents/skills/secondmate-provisioning/SKILL.md). The local `config/herdr-project-groups` file opts a home in to display-only Herdr project groups; [Project groups](herdr-backend.md#project-groups) owns its values, rules, tags, inheritance, and limits. +For normal herdr operations, `HERDR_SESSION` selects the named session, but destructive test cleanup must not rely on `HERDR_SESSION` alone. +Use the explicit guarded cleanup path described in [`docs/herdr-backend.md`](herdr-backend.md) instead of `herdr server stop`. + ### Rex attention marks The local `config/rex-status-marks` file opts a home in to Rex attention marks: `on` sets a program-status badge and a notification on the pane of a Rex worker that needs the captain, plus validation progress in its hover card, and `off` (the default when absent) shows none of them. [Attention marks](rex-backend.md#attention-marks) owns the status mapping and its limits. The local `config/rex-tint` file opts in to a subtle background tint on waiting and failed workers: `off` (the default when absent), `subtle`, or `subtle-light`; [State tint](rex-backend.md#state-tint) owns its behavior and caveats. -For normal herdr operations, `HERDR_SESSION` selects the named session, but destructive test cleanup must not rely on `HERDR_SESSION` alone. -Use the explicit guarded cleanup path described in [`docs/herdr-backend.md`](herdr-backend.md) instead of `herdr server stop`. +### OSC 7501 worker status reporting + +Firstmate launches a worker-side OSC 7501 Program Status Protocol bridge so supporting terminals can display task status even when the harness does not report it natively. +[`bin/fm-osc7501.sh`](../bin/fm-osc7501.sh)'s header owns passive terminal eligibility and input safety, native-reporter exclusion, signal precedence and activity sources, record fields, and watcher lifecycle. +The local `config/osc7501-status` file gates the feature per home: `on` (the default when absent) or `off`; an unrecognized word reads as off and warns once per value in the next worker pane that passes the header's writable-tty and known-aware-terminal gates. ### Zellij sessions @@ -955,6 +961,7 @@ state/.{meta,status} state/.inbox/{,handled/}NNN.msg ``` +Sync skips hidden names in `state/`, such as `.startup-network.status`, because they are Firstmate records rather than task records. The command is one-shot and starts no network transfer, daemon, timer, or watcher. It does not recursively copy `data/` or `state/`, accept arbitrary sources, discover secrets, or redact content. The exporter opens source and destination components relative to verified directory descriptors with no-follow semantics, then rechecks source identity and content before publication. @@ -1651,7 +1658,7 @@ Each relaunch installs a fresh worker extension with the home's current list, so ## Worker account pin (config/claude-account, config/pi-account) A home that mixes accounts for one runner, such as a work login and a personal one, can pin the account its own Claude and Pi workers launch on. -The pin is opt-in: without a home-wide or per-secondmate pin, pane-backed Claude launches keep receiving firstmate's own `CLAUDE_CONFIG_DIR` when it is set. +The pin is opt-in: without a home-wide or per-secondmate pin or a [declared Claude account list](#several-claude-accounts-configclaude-accounts), pane-backed Claude launches keep receiving firstmate's own `CLAUDE_CONFIG_DIR` when it is set. Unpinned Pi launches use the destination pane's ambient account. Both files are local and gitignored. @@ -1701,6 +1708,37 @@ A remote secondmate is launched on its host from its own home's configuration, s [`bin/fm-worker-account-lib.sh`](../bin/fm-worker-account-lib.sh) owns parsing, the sign-in check, and the full list of credentials a Claude launch unsets; [runtime backend verification](verification/runtime-backends.md#worker-account-pin-sign-in-check) records the check against the real runners. +### Several Claude accounts (config/claude-accounts) + +A home with more than one Claude subscription can let each ship and scout launch on whichever declared account has quota to spare, instead of pinning every Claude worker to one account. +Create `config/claude-accounts` with one account per line, each `ordinary` or the absolute path of an existing Claude config directory, written exactly as `config/claude-account` would hold it: + +```text +ordinary +/home/me/.claude-second +``` + +A final newline is optional; a blank line, a relative path, a repeated entry, or a control character refuses. +The list is opt-in: without it, every launch, including the single pin above, behaves exactly as before, and `--account` refuses. +A home keeps either the list or `config/claude-account`, never both; with both present, every Claude launch refuses. + +While the list is present, every Claude ship or scout spawn must name one declared entry with `--account `. +Firstmate chooses that entry at dispatch intake under the [`quota-array-dispatch`](../.agents/skills/quota-array-dispatch/SKILL.md) procedure, and [`bin/fm-claude-account-quota.sh`](../bin/fm-claude-account-quota.sh) measures each entry for it by running `quota-axi --provider claude --profile-only` under that entry's own config directory (`~/.claude` for `ordinary`). +Firstmate never picks an entry by default, by file order, or from the ambient account, and never discovers, adds, or copies a login. + +The named entry passes the same refusals as a pin before anything launches: its directory must exist, a raw Claude command must not override it, and `claude auth status` must say it is signed in, with the stripped environment and credential unsetting described above. +An undeclared, missing, or signed-out entry refuses rather than falling back to another entry or the ambient account. +The spawn line and the task record carry the entry as `account=`. + +A relaunch or recovery of that task keeps its recorded account, re-checking that it is still declared and signed in before the running worker stops. +To move a task to another declared account, for example when its own account is exhausted, relaunch it explicitly with `bin/fm-control.sh relaunch --account --note `; nothing moves a running task automatically. +A task with no recorded account, or one whose account is no longer declared, refuses a plain relaunch and needs that explicit `--account`. + +The list covers Claude ships and scouts only. +A local secondmate's own agent never draws from it: under a list it needs its own [per-secondmate account pin](#per-secondmate-account-pins), and `--account` refuses for every secondmate, local or remote. +Other runners ignore the list, and `--account` refuses for them. +Like the single pin, the list is not inherited into secondmate homes. + ### Per-secondmate account pins To choose an account for one local secondmate's own agent, create `config/secondmate-accounts//claude-account` or `config/secondmate-accounts//pi-account` in the primary home. @@ -1771,6 +1809,17 @@ The text is static and never executed or expanded; secondmate charters never tak `bin/fm-brief.sh`'s header owns the placement rule and its safety argument. +## Home branch prompt include (config/branch-prompt-include.md) + +The optional local, gitignored `config/branch-prompt-include.md` adds standing home rules to the supervision branch's system prompt, the way `config/brief-include.md` extends worker briefs. +The branch's headless engine loads none of the home's own hooks, `CLAUDE.md`, skills, or plugins, so this file is how a home gives the branch a home-specific rule. +When the file exists, `bin/fm-branch-prompt.sh` appends its text verbatim as the prompt's last section, `# Home prompt additions`, which defers to every other section of the prompt. + +An absent or blank file leaves the prompt byte-identical, while a present path that is not a readable regular file stops the generator before it emits anything, so the branch does not start. +The text is static and never executed or expanded, and an edit takes effect when the branch next builds its prompt for a new conversation. + +`bin/fm-branch-prompt.sh`'s header owns the placement rule and how it keeps the prompt cache stable. + ## Herdr lifecycle spawn guard (config/herdr-lifecycle-spawn-guard) The optional local, gitignored `config/herdr-lifecycle-spawn-guard` presence flag opts this home into refusing, at spawn, a ship or scout brief that drives Herdr lifecycle without the Herdr isolation contract. @@ -2128,8 +2177,16 @@ Dispatch profiles remain inherited and contain seat names only. Each setting comes from the calling environment or the home's gitignored `.env` through `bin/fm-env-lib.sh`; a non-empty environment value wins. OpenRouter, `systemone`, and OpenAI each require their explicit `FM_DISPATCH_PROVIDER` value and a non-empty `FM_DISPATCH_MODEL`; an omitted model is an actionable configuration error, and the presence of a provider's key never selects that provider by itself. `systemone` also requires `FM_DISPATCH_BASE_URL`, an `http://` or `https://` base such as a local llama.cpp server or a hosted System One-compatible service, and posts to `/v1/systemone`; a missing or scheme-less base URL is an actionable configuration error. -With no provider setting, the original typesafe.ai behavior is unchanged: `jev-latest` is used when `TYPESAFE_API_KEY` is available, `FM_DISPATCH_MODEL` is ignored, and the resolver is off when no key is available and no `typesafe` credential source is declared. +With no provider setting and a valid base, TypeSafe uses `jev-latest` when `TYPESAFE_API_KEY` is available, ignores `FM_DISPATCH_MODEL`, and is off when no key is available and no `typesafe` credential source is declared. Explicit `FM_DISPATCH_PROVIDER=typesafe` also uses `jev-latest` when no model is set. +`TYPESAFE_BASE` optionally selects a different base for the TypeSafe request, from the environment or `.env` with the same non-empty-environment precedence. +If neither source supplies a non-empty value, the base defaults to `https://api.typesafe.ai`. +It must be an `http://` or `https://` URL with a host and no userinfo, query, fragment, whitespace, or backslash. +An explicit port must be between 1 and 65535, and a bracketed host must be a valid IPv6 address. +Its path must not contain literal `[` or `]` characters; IPv6 host brackets remain supported. +An invalid value exits 2 before credential lookup and sends nothing, even when no key is available. +Trailing slashes are removed before appending `/v1/systemone`, so `TYPESAFE_BASE=https://ai-gateway.vercel.sh/typesafe/` sends to `https://ai-gateway.vercel.sh/typesafe/v1/systemone`. +To select a gateway-specific model ID, set `FM_DISPATCH_PROVIDER=typesafe` and `FM_DISPATCH_MODEL` to that ID. The selected provider needs a non-empty key from the environment, else `.env`, else, for `typesafe` and `openrouter` only, the credential source the home declares for that provider; `systemone` is the exception, where an absent `FM_DISPATCH_API_KEY` means the request carries no `Authorization` header. Off means one `dispatch-resolve: off` line on stderr, nothing on stdout, exit 0, and no network call, so firstmate dispatches exactly as it does without the tool. @@ -2232,7 +2289,7 @@ No qualifying option, or two equally probable qualifying options, produces `ambi Every result above exits 0. - Response probabilities must contain exactly every offered choice, use numeric values from 0 through 1, and sum to approximately 1 within 0.01. -- Only a usage or configuration error exits 2: an unreadable brief, an existing but unreadable or malformed canonical rules file, an unreadable or invalid credential source declaration, a missing `jq`, an unknown provider, a non-typesafe provider without a model, or `systemone` without a valid base URL, each reported and never selected around. +- Only a usage or configuration error exits 2: an unreadable brief, an existing but unreadable or malformed canonical rules file, an unreadable or invalid credential source declaration, a missing `jq`, an unknown provider, a non-typesafe provider without a model, an invalid `TYPESAFE_BASE` for TypeSafe, or `systemone` without a valid base URL, each reported and never selected around. - Missing `curl` is a normal structured `error` outcome with exit 0 so firstmate uses today's routing. **Firstmate retains the dispatch decision** @@ -2296,7 +2353,7 @@ An installer should treat any status other than 0 as a refusal, because a resolv - The resolver copies every environment-provided key into a non-exported private variable and unsets `TYPESAFE_API_KEY`, `OPENROUTER_API_KEY`, `OPENAI_API_KEY`, and `FM_DISPATCH_API_KEY` before launching child processes; bootstrap also protects the typesafe key. - A key read from a declared source is captured into the same private variable, so it is never exported either. - The resolver sends only the selected key to `curl` as a header read from a file descriptor, never on argv, and never prints, logs, or writes any key. -- The typesafe.ai endpoint is `https://api.typesafe.ai/v1/systemone`, the OpenRouter endpoint is `https://openrouter.ai/api/alpha/decisions`, the OpenAI endpoint is `https://api.openai.com/v1/decisions`, and the `systemone` endpoint is `/v1/systemone`. +- TypeSafe endpoint selection follows `TYPESAFE_BASE` above; the OpenRouter endpoint is `https://openrouter.ai/api/alpha/decisions`, the OpenAI endpoint is `https://api.openai.com/v1/decisions`, and the `systemone` endpoint is `/v1/systemone`. - The default confidence floor remains 0.6; request timeouts are 5 seconds for typesafe.ai and 10 seconds for OpenRouter, `systemone`, and OpenAI. - A CPU-only local `systemone` host may not answer within its 10-second timeout; that is an ordinary `error` outcome, and firstmate keeps its existing intake. @@ -2544,6 +2601,10 @@ This section is the single owner of the canonical schema. "version_args": [""], "announce_pattern": "", "announce_args": [""], + "published": { + "source": "npm", + "package": "" + }, "version_url": "", "git": { "repo": "", @@ -2557,7 +2618,8 @@ This section is the single owner of the canonical schema. **Entry fields and probe behavior** -- Each entry needs a `name` and at least one of `command` or `git`; an entry may carry both. +- Each entry needs a unique non-empty `name` and at least one of `command` or `git`; an entry may carry both. +- A `published` probe also requires `command` to supply the installed version; it can accompany `git` and an announcement on the same entry, but not `version_url`, so an entry has at most one published-version source. - A `command` entry gives the `PATH` comparison above, and adding `announce_pattern` also reads the tool's own update announcement, which is how a tool that already reports its own updates is read rather than reimplemented. - The announcement counts as `update available` only when the version it names is newer than the newest installed copy found; a version already installed is reported only as `update not in effect`, so one completed install does not report both in the same sweep. An announcement naming no readable version is reported as an available update as before. - A tool does not always announce a new release on the command that prints its version: `no-mistakes --version` prints only the version, while its other commands carry the announcement. @@ -2569,7 +2631,24 @@ This section is the single owner of the canonical schema. - A `git` entry reports how many commits the local clone is behind its remote branch, and stays silent when the clone is current or ahead. - An omitted `branch` uses the remote's default branch, taken from the clone's own record of it and otherwise asked of the remote directly, so a `--single-branch` clone still resolves. -Both probe kinds are read-only and bounded, and a probe that cannot answer is reported as a check failure rather than assumed current. +**Published release sources** + +A `published` object selects one public release source: + +- `{"source":"npm","package":"tasks-axi"}` reads the public npm registry version selected by the package's `latest` dist-tag; scoped names such as `@scope/tool` are supported. +- `{"source":"github","repo":"herdrdev/herdr"}` reads the `tag_name` of GitHub's latest published release for that public repository; draft releases, prereleases, and tags without a release are not selected. + +`published` accepts only the fields shown for its source, and requires a package name or `owner/repo`, without a URL, query, or credentials. +It compares that source with the newest installed copy found, and reports an update only when the published version is numerically newer; a published version already installed is reported only as `update not in effect`. +For this comparison, the installed version is the first dotted number in the output of a version command that exits successfully, so `0.1.49`, `v0.8.2`, and `herdr 0.8.2` work; a version command that fails is reported as a check failure. +The published version must be a dotted numeric version with an optional leading `v`; other formats, including prerelease suffixes, produce a check failure. +Components are compared numerically, with leading zeroes and missing trailing zero components ignored. +These queries use `curl` and the existing `jq` parser, without an npm installation or a GitHub login; `curl` is required only for a `published` or `version_url` probe. +The `published` HTTP reads ignore curl configuration and send no credentials, follow no redirects, and fail within the probe bound on an unavailable source, HTTP error, or unreadable version. + +All probe kinds are read-only and bounded, and a probe that cannot answer is reported as a check failure rather than assumed current. +Malformed configuration stops `arm` with a diagnostic. +During a sweep, malformed configuration is reported as a registry failure and no tools are checked. See [`docs/examples/watched-tools.json`](examples/watched-tools.json) for a starting point to copy into local `config/watched-tools.json`. **Arm, edit, and disarm** @@ -2595,7 +2674,8 @@ Arm the check once per home with `bin/fm-tool-update-check.sh arm`. | `FM_TOOL_UPDATE_PROBE_SECS` | 5 | Bounds one probe. | | `FM_TOOL_UPDATE_BUDGET_SECS` | 20 | Bounds a whole sweep. | -- A sweep that runs out of budget says which tool it did not reach rather than reporting the rest as current. +- If the budget runs out before the next tool, the sweep records its partial findings and says which check it did not reach rather than reporting the remaining tools as current. +- It waits until the next configured interval before probing again, while a final probe that finishes after the deadline still completes the sweep. - The sweep must finish inside `FM_CHECK_TIMEOUT` (default 30), because a run the watcher kills records nothing and would then surface as a `timed out` check wake on every poll. - So a budget larger than that timeout allows is cut down to what fits instead of being refused, and the cut is reported in the report line. - A budget that is not a whole number from 1 to 120 is still refused outright. @@ -3357,7 +3437,7 @@ KNOWN LIMIT: while any activity continues in a home whose original owning sessio Detaching a runner into its own process group is what lets a persistent source outlive the turn that armed it, and on its own it is also what lets a runner outlive its whole home: reparented to init, it keeps its blocking child - and every process that child spawns - running with nothing left to reap it. -- So a home's process-event state carries a lease that registration, attached start, reconciliation, acknowledgement, and listing refresh, and the watcher's reconcile cycle is what keeps it fresh in a live home. +- So a home's process-event state carries a lease that registration, attached start, reconciliation, acknowledgement, listing, and detached launch by a non-runner caller through `reconcile` or `ensure-listening` refresh, and the watcher's reconcile cycle is what keeps it fresh in a live home. - An attached public `start` continues refreshing the lease while its caller remains attached. - Each runner fails closed unless a small guard starts successfully beside it in a separate process group. - That guard accepts the lease only while the state root retains the device/inode identity recorded by the runner's claim, and initiates the verified stop after two consecutive reads cannot prove that identity and lease freshness, so one unreadable read cannot kill a live runner. @@ -3507,7 +3587,7 @@ FM_STATE_OVERRIDE= # alternate state dir, mainly for tests FM_DATA_OVERRIDE= # alternate data dir, mainly for tests FM_PROJECTS_OVERRIDE= # alternate projects dir, mainly for tests FM_CONFIG_OVERRIDE= # alternate config dir, mainly for tests -FM_PROC_ROOT_OVERRIDE= # alternate /proc root for Linux process-identity reads in fm-wake-lib.sh and fm-teardown.sh, mainly for tests +FM_PROC_ROOT_OVERRIDE= # alternate /proc root for Linux process-identity readers, mainly for tests FM_BACKEND= # optional runtime backend override for new spawns; see "Runtime backend" for accepted values and limits FM_T3CODE_ORIGIN= # optional T3 Code server origin for bin/fm-t3-mcp.mjs login; the default is read from ~/.t3/userdata/server-runtime.json FM_TRACE_CONTEXT= # optional trace-context override; see "Trace context propagation" @@ -3595,6 +3675,7 @@ FM_DISPATCH_MODEL= # explicit model for the selected provider; required for FM_DISPATCH_BASE_URL= # http(s) base of a System One-compatible host; required for systemone FM_DISPATCH_API_KEY= # optional systemone key; absent sends no Authorization header TYPESAFE_API_KEY= # typesafe dispatch key, from the environment, .env, or the declared credential source ("Typed dispatch resolution") +TYPESAFE_BASE= # optional TypeSafe base; see "Typed dispatch resolution" OPENROUTER_API_KEY= # OpenRouter dispatch key, from the environment, .env, or the declared credential source OPENAI_API_KEY= # OpenAI dispatch key, from the environment or .env FMX_DISCORD_REPLY_MAX_CHARS=1900 # Discord reply per-message split budget; values below 50 clamp to 50, values above 2000 reset to 1900 @@ -3727,6 +3808,14 @@ With the cap off, a failed counter write is logged without changing the unconfig All three cap settings accept whole numbers up to 999999999999999999; invalid or out-of-range values use their documented defaults, and a zero horizon or sentinel TTL also uses its default. An operator can re-engage a capped window by deleting `state/.wedge-permanent-`, or a window silenced by a failed state write by deleting `state/.wedge-rollback-failed-`. Turning the cap off takes effect at once because leftover cap markers and sentinels are ignored while the cap is disabled. +[`bin/fm-watch-key-lib.sh`'s header](../bin/fm-watch-key-lib.sh) owns `` derivation and the optional cleanup procedure for obsolete per-endpoint markers. + +Upgrading a live home to the bounded `v3.` watcher keys intentionally resets its per-endpoint observation caches once, including stale timers, escalation counts, cap markers, and pause/wait notification throttles. +A previously capped window can start a fresh escalation ladder under the configured cap, and stale or declared-wait notifications can repeat before their new throttles are established. +This reset is harmless to running tasks and durable recovery state: task status, metadata, and wake queues are preserved, and a wedge notification never automatically interrupts, signals, or restarts a worker. +Declared pauses and waits are classified again from their existing evidence. +Their old notification throttles are not carried forward. +Legacy keys can conflate different endpoints, so importing their history could suppress a current endpoint using another endpoint's evidence. `fm-teardown.sh` retries only Git's `Unable to create '...index.lock': File exists` return failure up to `FM_TREEHOUSE_RETURN_LOCK_RETRIES` times. `FM_TREEHOUSE_RETURN_LOCK_RETRIES` accepts a nonnegative integer, and an unset, blank, or invalid value uses the default of 3. diff --git a/docs/examples/watched-tools.json b/docs/examples/watched-tools.json index 92ec314c37e..edc26541764 100644 --- a/docs/examples/watched-tools.json +++ b/docs/examples/watched-tools.json @@ -11,7 +11,27 @@ { "name": "herdr", "command": "herdr", - "version_args": ["--version"] + "version_args": ["--version"], + "published": { + "source": "github", + "repo": "herdrdev/herdr" + } + }, + { + "name": "tasks-axi", + "command": "tasks-axi", + "published": { + "source": "npm", + "package": "tasks-axi" + } + }, + { + "name": "gnhf", + "command": "gnhf", + "published": { + "source": "npm", + "package": "gnhf" + } }, { "name": "claude", diff --git a/docs/fm-test-portable-shards.md b/docs/fm-test-portable-shards.md index c9853357ce8..e9e31b747d5 100644 --- a/docs/fm-test-portable-shards.md +++ b/docs/fm-test-portable-shards.md @@ -32,6 +32,9 @@ The native-Windows-only `tests/fm-pi-windows-shell-invocation.test.sh` retains i The 2026-10-07 polytoken adapter tests carry provisional local measurements instead - 21090 ms for `tests/fm-polytoken-harness.test.sh` and the 103 ms live-guard skip for `tests/fm-polytoken-signals-live-e2e.test.sh`, both measured on the adapter-verification macOS host - until the next CI refresh replaces them with Ubuntu measurements, because they postdate the 2026-09-30 baseline. The 2026-10-23 `tests/fm-approval-failover.test.sh` serial hint is a provisional local measurement of 5410 ms, the slowest of five local `bin/fm-test-run.sh` samples (4991-5410 ms), until the next CI refresh replaces it with an Ubuntu timing-artifact maximum. The 2026-10-23 Kimi folder-prompt live guard, `tests/fm-kimi-trust-prompt-live-e2e.test.sh`, carries a provisional 60 ms skip measured on the Linux host that recorded its captures, until the next CI refresh replaces it with an Ubuntu measurement. +The 2026-11-03 `tests/fm-herdr-projected-cd-live-e2e.test.sh` serial hint is provisionally 28000 ms, above a successful 27692 ms local runner sample exercising Herdr 0.9.5 on macOS arm64, until the next CI refresh replaces it with an Ubuntu timing-artifact maximum. +The 2026-10-27 `tests/fm-claude-account-quota.test.sh` serial hint is a provisional local measurement of 2492 ms, the slowest of five local `bin/fm-test-run.sh` samples (1930-2492 ms) on a Linux host, until the next CI refresh replaces it with an Ubuntu timing-artifact maximum. +The 2026-11-04 `tests/fm-install-herdr.test.sh` serial hint is a provisional local measurement of 1000 ms, above the slowest of five `bin/fm-test-run.sh` samples (155-223 ms) on a Linux host, until the next CI refresh replaces it with an Ubuntu timing-artifact maximum. The 2026-10-26 `tests/fm-test-tap-verify.test.sh` serial hint is a provisional local measurement of 1000 ms, above a 238 ms `bin/fm-test-run.sh` sample on this Linux host, until the next CI refresh replaces it with an Ubuntu timing-artifact maximum. The 2026-10-28 house-merge serial hints use successful local macOS `bin/fm-test-run.sh` samples: 17229 ms for `tests/fm-trace-span-lib.test.sh`, 1997 ms for `tests/fm-project-mode.test.sh`, 64410 ms for `tests/fm-pr-body-preflight.test.sh`, and 7039 ms for `tests/fm-review-check.test.sh`. The same local run supplied 11826 ms for `tests/fm-record-export.test.sh`, 9310 ms for `tests/fm-record-export-remote-home.test.sh`, 4584 ms for `tests/fm-session-lock-windows-consumers.test.sh`, 5707 ms for `tests/fm-spawn-brief-argv.test.sh`, and 24257 ms for `tests/fm-capacity.test.sh`, replacing their unmeasured fallback weights. @@ -42,6 +45,7 @@ These are provisional weights until the next green Ubuntu timing-artifact refres The 2026-10-27 read-repetition suites carry provisional Linux x86_64 local runner maxima: 602 ms for `tests/fm-read-repetition.test.sh` (six successful samples, 528-602 ms) and 1110 ms for `tests/fm-read-repetition-live.test.sh` (five successful samples, 949-1110 ms). The live samples exercised installed Pi 1.4.0 RPC without capability skips; they are not CI timing evidence or timings for unavailable harnesses. Both suites retain serial placement and full coverage; replace these provisional hints with Ubuntu timing-artifact maxima at the next CI refresh. +The 2026-10-29 `tests/fm-live-lab-isolation.test.sh` serial hint is a provisional local measurement of 6459 ms, the slowest of six `bin/fm-test-run.sh` samples (4308-6459 ms) taken inside a private `unshare -Urm` /tmp on this Linux host, until the next CI refresh replaces it with an Ubuntu timing-artifact maximum. The 2026-10-27 `tests/fm-codex-stop-park.test.sh` serial hint is a provisional local measurement of 35000 ms, above the slowest of the `bin/fm-test-run.sh` samples (20170-34700 ms) on a macOS arm64 host, until the next CI refresh replaces it with an Ubuntu timing-artifact maximum. The 2026-10-28 Antigravity primary tests carry provisional serial hints - 45000 ms for `tests/fm-agy-primary.test.sh` and the 75 ms live-guard skip for `tests/fm-agy-primary-live-e2e.test.sh` - until the next CI refresh replaces them with Ubuntu timing-artifact maxima. The wedge-cap suite carries the Ubuntu measurements from CI run [39597689178](https://github.com/kunchenguid/firstmate/actions/runs/39597689178) - 374225 ms for `tests/fm-watch-wedge-cap.test.sh` and the 74 ms live-guard skip for `tests/fm-watch-wedge-cap-live.test.sh` - in place of the provisional local measurements the suite first carried (1003000 ms and the 155 ms skip), which overstated the CI cost of both. diff --git a/docs/pi-supervision-branch.md b/docs/pi-supervision-branch.md index 822ccc15aba..9243cc00cd3 100644 --- a/docs/pi-supervision-branch.md +++ b/docs/pi-supervision-branch.md @@ -510,7 +510,7 @@ That includes watcher-failure alarms, which are never offered to the branch. The captain accepted the normal provider prompt-caching strategy: -- A byte-identical branch prefix generated once per firstmate version. +- A stable branch prefix governed by [`bin/fm-branch-prompt.sh`'s prefix-stability contract](../bin/fm-branch-prompt.sh). - The same tool set in the same order on every request. - One shared `prompt_cache_key` per home for all branch sessions. It is set in a `before_provider_request` hook, and only for providers whose requests already carry that field. @@ -613,7 +613,7 @@ Each relocated script keeps its own gate, enforcing exactly what a script can ch | Script | Gate while away | | --- | --- | -| `bin/fm-pr-merge.sh` | Merges any pull request green at its live head, synchronously, under the record lock, and refuses `--allow-red`, `--allow-missing`, and `--allow-no-pipeline` while away, so the green gate is absolute in this posture; which pull request the words meant is the branch's reading. | +| `bin/fm-pr-merge.sh` | Merges a pull request synchronously under the record lock after the [merge helper's guards](../bin/fm-pr-merge.sh) pass, and refuses `--allow-red`, `--allow-missing`, and `--allow-no-pipeline` while away, so the green gate is absolute in this posture; which pull request the words meant is the branch's reading. | | `bin/fm-spawn.sh` | Dispatches only queued work whose blockers cleared - already queued, or filed by the branch because the words explicitly call for it; refuses a fresh ordinary spawn for either actor once the home holds as many ordinary workers still able to spend (`bin/fm-afk-spend-count.sh`) as the record's spend cap (relaunches and secondmates exempt). | | `bin/fm-send.sh --resolve-key` | Answers a decision the words pre-answer, or one `ask-user-authority`'s judgment (carried verbatim in the branch prompt) lets firstmate decide. | | `bin/fm-merge-local.sh` | Never relocated. | diff --git a/docs/remote-secondmates.md b/docs/remote-secondmates.md index f08e06416eb..0bd89e8678e 100644 --- a/docs/remote-secondmates.md +++ b/docs/remote-secondmates.md @@ -704,6 +704,11 @@ Locked startup convergence and `bin/fm-config-push.sh` transfer only the declare The receiver accepts only the items its own remote Firstmate code root declares, so a remote secondmate must run code at least as new as its primary before the primary pushes newly declared inherited material it actually has: an older receiver refuses a present `config/t3code-instances`, and the remaining items of that push are skipped. When the primary does not have a newly declared item, the older receiver's refusal of its `absent` record counts as `unchanged` and the push continues, so a home that never configures `config/t3code-instances` converges as before. That item still costs one extra `absent` call on every inheritance push to a remote secondmate, on every home, including one that never selects `config/backend=t3code`: the remote job worker runs one home's commands one after another, and the lifecycle test legs that push inheritance measure about 5-12% slower; an older remote code root tolerates the call as above. +Lost or restored sender counters recover automatically before inherited material is sent, without relaxing the receiver's rejection of delayed older writes or same-generation conflicting payloads. +Malformed or unreadable generation state stops the push before inherited payloads change. +Counter recovery requires a receiver that supports the generation query, so synchronize an older receiver's code before relying on recovery. +The [`fm-remote-inherit-push.sh`](../bin/fm-remote-inherit-push.sh) header owns generation negotiation, transaction locking, and the compatibility fallback that keeps ordinary pushes to older receivers working. +The synthetic transport regression is [`fm-remote-inherit-generation.test.sh`](../tests/fm-remote-inherit-generation.test.sh). The [`secondmate-provisioning` skill](../.agents/skills/secondmate-provisioning/SKILL.md#charter-and-seed) owns the placement-specific re-read contract, including changed transfers, unchanged retries, and partial or unknown completion. ### Pause and resume a remote second mate diff --git a/docs/rex-backend.md b/docs/rex-backend.md index 45c1bd56322..87a71c2ead2 100644 --- a/docs/rex-backend.md +++ b/docs/rex-backend.md @@ -119,7 +119,7 @@ An ordinary spawn refuses any block carrying the task's scoped label. ## Attention marks Opt-in: with `config/rex-status-marks` set to `on`, the captain sees which worker needs them from Rex itself. -`bin/fm-rex-marks.sh` turns each Rex task's latest status into one program-status record (OSC 7501, `app=firstmate`, `id=fm`) on that worker's pane, so Rex badges its tab, session, and Activity entry while the worker's own agent keeps the pane's root status. +`bin/fm-rex-marks.sh` turns each Rex task's latest status into one program-status record (OSC 7501, `app=firstmate`, `id=fm`) on that worker's pane, so Rex badges its tab, session, and Activity entry without replacing the pane's root status. | Worker status | Record | Message | | --- | --- | --- | @@ -133,8 +133,8 @@ Opt-in: with `config/rex-status-marks` set to `on`, the captain sees which worke Each record's title is the task's tab label, `fm-`, so Rex labels it with the name Firstmate gave the tab rather than a branch or the agent's terminal title. Each new record that needs the captain also carries an OSC 777 desktop notification titled with the tab label, because Rex sounds and notifications follow each pane's root program status, not a child record. -Firstmate never writes the root record: the harness owns it, and a second root report for the same turn makes Rex notify twice. -Firstmate's own records never count as the worker's state, for busy detection or for push escalation. +The attention-mark writer never writes the root record: the harness or the [worker-side status bridge](configuration.md#osc-7501-worker-status-reporting) owns it, and a second root report for the same turn makes Rex notify twice. +Firstmate's own records - attention marks and the bridge's `app=firstmate-` root record - never count as the worker's state, for busy detection or for push escalation. The notification fires once per change, never on a repeated sync. Rex has no API to set a record, so the mark is one small write to the pane's terminal device; a `working` or `blocked` record is dropped when the agent's shell prints its next prompt or exits, and the next sync writes it again. The watcher runs `fm-rex-marks.sh sync` once per poll only while `config/rex-status-marks` or `config/rex-tint` exists or a cached record remains, and cleanup clears a cached mark before closing the pane. diff --git a/docs/scripts.md b/docs/scripts.md index 1b6e49cc81c..0eb6411be6e 100644 --- a/docs/scripts.md +++ b/docs/scripts.md @@ -51,6 +51,7 @@ The shared no-mistakes gate lifecycle boundary is summarized in [architecture.md | `fm-herdr-ci-cleanup.sh` | Snapshot and tear down only job-owned `fm-lab-*` sessions in the Herdr CI lane | | `fm-hc-ci-cleanup.sh` | Snapshot and tear down only job-owned `fm-hclab-*` sessions in the herdr-core CI lane | | `fm-rex-marks.sh` | Opt-in: turn each Rex task's latest status into one badge and notification on its pane, plus validation progress and an optional tint (`plan`, `sync`, `clear`); the watcher runs `sync` each poll while the option or a cached record exists, and cleanup runs `clear` | +| `fm-osc7501.sh` | Launch-driven [OSC 7501 worker status bridge](configuration.md#osc-7501-worker-status-reporting); its header owns commands and reporting rules | | `fm-test-run.sh` | Behavior-test runner: selection, portable lanes, bounded concurrency, budgets, coverage guard, timing/JSON; refuses to execute in the repository primary checkout when `FM_TASK_ID` marks a task worker | | `fm-test-isolation-proof.sh` | Concurrent isolation harness and portable candidate set owner | | `fm-ensure-agents-md.sh` | Manually initialize project agent-memory files (see the helper's header and help) | @@ -62,7 +63,8 @@ The shared no-mistakes gate lifecycle boundary is summarized in [architecture.md | `fm-context-restart.sh` | Validate a budget or publish a reset-safe handoff request | | `fm-context-restart-supervise.sh` | Wrapper-owned watcher transfer across Claude replacement | | `fm-primary-scope-lib.sh` | Shared marker-or-plain-checkout primary-home predicate for tracked hooks | -| `fm-session-lock-lib.sh` | Shared session-lock ownership from harness ancestry, a trusted Claude session id, or on Windows/Git Bash a published harness pid, for fm-lock.sh and the Claude Stop auto-arm, plus the read-only lock inspection behind `fm-lock.sh status` and `fm-inbox.sh ready` | +| `fm-session-lock-lib.sh` | Shared session-lock ownership from harness ancestry, a trusted Claude session id, or on Windows/Git Bash a published harness pid, for fm-lock.sh and the Claude Stop auto-arm, plus the reclaim verdict recovery callers apply, the ps-free identity fallback behind it, and the read-only lock inspection behind `fm-lock.sh status` and `fm-inbox.sh ready` | +| `fm-procinfo.py` | Read one pid's ppid, comm, and args through macOS kernel syscalls when the `ps` binary cannot execute | | `fm-claude-stop-autoarm.sh` | Claude Stop `asyncRewake` hook owning tokenless watcher continuity with single-flight exit-2 rewake (docs/watcher-continuity.md) | | `fm-turnend-guard.sh` | Shared primary turn-end guard predicate so no turn ends blind (docs/turnend-guard.md) | | `fm-turnend-guard-grok.sh` | Grok Stop-hook adapter for the primary turn-end guard | @@ -117,6 +119,7 @@ The shared no-mistakes gate lifecycle boundary is summarized in [architecture.md | `fm-codex-stop-park.sh` | On an opted-in home, register verified native Codex sessions and park supervision in the asynchronous Stop hook, delivering wakes through the native thread queue | | `fm-watch-checkpoint.sh` | Run one bounded foreground watcher checkpoint for Codex-style supervision | | `fm-watch.sh` | Singleton-safe watcher: absorb benign wakes, detect stalled local-secondmate wake queues, and exit on actionable ones | +| `fm-watch-key-lib.sh` | Shared per-window watcher marker identity for the watcher and away daemon | | `fm-inactive-reconcile.sh` | Reconcile long-inactive direct crewmate terminal outcomes and validation handoffs, validating reported PR bodies through bounded forge reads | | `fm-afk-contract.sh` | Own the away-or-quiet record's posture, schema, entry, read-back, archive, and cross-subsystem authority lock | | `fm-afk-start.sh` | Run the common sourceable away-mode daemon entry in the foreground | diff --git a/docs/sessionstart-nudge.md b/docs/sessionstart-nudge.md index 36e293fb415..e62e99cf01a 100644 --- a/docs/sessionstart-nudge.md +++ b/docs/sessionstart-nudge.md @@ -212,6 +212,7 @@ It does this in its own separate, hard-coded loop, independent of two other owne - Pi's `lockOwnership()`. If the lock names a live pid in that ancestry, session start already ran in this harness session and the wrapper stays silent. +That loop reads each parent link through the `ps` binary, so a host that refuses `ps` cannot confirm a numeric lock's ancestry and the wrapper prints the nudge even for a session that already holds the lock. On Windows/Git Bash, a lock holding a tagged `win:` identity is outside this process table, so the wrapper instead asks the library's `fm_session_lock_owned_by_self`, whose Windows process-boundary bridge owns that identity. ### Exit codes diff --git a/docs/supervision-host.md b/docs/supervision-host.md index 51bcf83b196..ab18bb06485 100644 --- a/docs/supervision-host.md +++ b/docs/supervision-host.md @@ -401,6 +401,7 @@ Today the only verified engine is Claude's print mode, measured on Claude Code 2 - `--safe-mode` loads none of the home's hooks, `CLAUDE.md`, skills, plugins, or MCP servers. So the engine can never fire the home's own Stop or SessionStart hooks. + A home's own rules reach the engine only through the optional `config/branch-prompt-include.md` ([configuration](configuration.md#home-branch-prompt-include-configbranch-prompt-includemd)). - `--bare` is unusable because it never reads claude.ai OAuth. - From inside the engine's shell the primary is not in the harness ancestry, so the engine can never act as the session-lock owner. diff --git a/docs/turnend-guard.md b/docs/turnend-guard.md index 8ebfee95c25..37cc8e81c2a 100644 --- a/docs/turnend-guard.md +++ b/docs/turnend-guard.md @@ -93,7 +93,7 @@ The guard cooperates with that arm rather than trusting a beacon left by the cyc ### Foreign session-lock owner -When an active home instead has a live session lock held by a verified harness that the current session does not own, the Claude guard emits a read-only ownership diagnostic and allows the turn to end safely. +When an active home instead has a session lock that the current session does not own and the shared `fm_session_lock_pid_blocks_reclaim` verdict refuses to reclaim, the Claude guard emits a read-only ownership diagnostic and allows the turn to end safely. Ownership is the shared `fm_session_lock_owned_by_self` verdict in `bin/fm-session-lock-lib.sh`. The current session owns the lock when either of these holds: @@ -269,7 +269,10 @@ The registrations in detail: Rejected read-only sessions and foreign-host payloads leave the parent record untouched; `tests/fm-context-restart.test.sh` covers these exits and context-refresh continuation, while `tests/fm-busy-adapter-wiring.test.sh` covers allowed and blocked Stops in different hook orders. A home whose guard predates `.fm-busy-stop` cannot close the turn, so that launch keeps the ordinary `Stop` idle hook instead. [Runtime backend verification](verification/runtime-backends.md#secondmate-busy-contract) owns the live evidence. -- Codex registers a `Stop` hook in `.codex/hooks.json`, anchors the executable to the hook process working directory, verifies a Firstmate-shaped hook-bearing root, and passes the original payload to the shared guard. +- Codex registers a synchronous `Stop` hook in `.codex/hooks.json`, anchors the executable to the hook process working directory, verifies a Firstmate-shaped hook-bearing root, and passes the original payload to the shared guard through `bin/fm-codex-stop-park.sh --guard` when that script is present. + Its inner wrapper uses non-login Bash with the inherited PATH to avoid login/logout output contaminating the hook response. + Allowed stops return one literal `{}` object; nonzero exits and stderr retain the shared guard's error or bounded-continuation behavior. + [Runtime backend verification](verification/runtime-backends.md#stop-response-parsing) owns the parser evidence and its portable and token-free live regression entry points. - OpenCode subscribes to the terminal `session.execution.succeeded`, `session.execution.failed`, and `session.execution.interrupted` events through `ctx.event.subscribe` in `.opencode/plugins/fm-primary-turnend-guard.js`, latched to the root worker session so child-session turns do not trip it, lets the watcher coordinator act first, and calls `ctx.session.prompt` once when the guard returns 2. The legacy OpenCode 1 `session.idle` + `client.session.promptAsync` path is retained in the plugin's `server` export. - Pi listens for `agent_settled` in `.pi/extensions/fm-primary-turnend-guard.ts`, runs once per logical agent run, and calls `pi.sendUserMessage(..., { deliverAs: "followUp" })` once when the guard returns 2. - omp answers its blocking `session_stop` hook in `.omp/extensions/fm-primary-turnend-guard.ts`, passing the payload's own `stop_hook_active` to the shared guard. @@ -402,7 +405,13 @@ Fresh `failed` and `failed-suppressed` outcomes enter or advance the failure pro The auto-arm itself rechecks the healthy watcher predicate and retries a bounded number of times before reporting a genuine failure. The foreground arm legitimately follows a healthy watcher until its next wake. -The hook therefore catches HUP, TERM, and INT from host timeout or teardown and routes interruptions through the same failure-episode decisions as other terminal failures, including the [attended exhaustion decision](#attended-fail-open). +The hook catches HUP, TERM, and INT from host timeout or teardown. +If the producing session no longer matches the lock's process identity and recorded session id, the hook records `session-ended` and exits 0 silently without creating a failure notice. +The existing trusted-session ownership proof preserves that match across a verified same-session lock re-anchor. +While that session remains current, interruptions use the ordinary failure progression, including its notice deduplication, attended-alarm limits, and the [attended exhaustion decision](#attended-fail-open). +An interruption notice carries the producing session's lock-holder identity and recorded session id, which are rechecked under the auto-arm owner lock before committing the failure. +Before taking its first generation, a replacement that no longer matches that session clears the interrupted episode's notice, attended alarm, and block budget, so its first genuine failure can notify even if a process number and executable path are reused. +Empty legacy markers and ordinary failure notices retain their existing behavior. Claude drops that exit 2 when it terminated the hook at the configured timeout itself, so a park that outlives the timeout ends without a rewake (`bin/fm-claude-stop-autoarm.sh` header). The first fresh exhausted-failure epoch preserves its handoff without consuming a blocked-stop count. @@ -418,7 +427,7 @@ The block budget is charged by two rules: That second rule still bounds an inert auto-arm when a hook never fires or fails before its generation claim and therefore leaves the ledger frozen at its last outcome. Charging only epoch changes let the count freeze with that ledger, so the remaining inert-hook cases could re-block without limit and make the attended fail-open unreachable. `budget_account_current_epoch` in `bin/fm-turnend-guard.sh` owns the rule. -A verified live foreign session-lock owner takes the earlier diagnostic safe exit instead and never reaches this budget path. +A live foreign session-lock owner takes the earlier diagnostic safe exit instead and never reaches this budget path. Whenever both coordination locks are needed, positive auto-arm recovery, the terminal check, and the auto-arm's attended-alarm commit acquire the auto-arm owner lock before the budget lock. #### Attended fail-open @@ -436,10 +445,10 @@ Two writers can commit `state/.claude-autoarm-failure-alarmed`, both under the s - The Stop auto-arm's own terminal failure: the exhausted continuation is the one participant that knows no automatic continuation remains, so when this session's consumed block count is already over budget it commits the alarm in the same owned critical section as its `failed-suppressed` ledger write and delivers the one loud final rewake. Every failed-suppressed continuation - an ordinary exhausted failure, a lost supervision-host hand-back, an interrupted hook, or a refused healthy-watcher reset - takes this one exhaustion decision before it commits another retry, because the guard's claim-open step-aside would otherwise leave the alarm unwritten on every Stop. -After that alarm, the Stop auto-arm suppresses further exit-2 continuations until positive watcher recovery, so the final fail-open remains reachable. +After that alarm, the Stop auto-arm suppresses further exit-2 continuations until the episode resets, so the final fail-open remains reachable. +[Failure progression and block budget](#failure-progression-and-block-budget) owns the reset boundaries, including positive watcher recovery and replacement-session isolation of an interrupted episode. The alarm cannot repeat during that failure episode, and a later unhealthy stop blocks again. Every suppressed continuation in the episode emits one reason line instead of an empty rewake body, stating why no operator notice repeats and whether a Stop-owned retry stays scheduled. -A positively verified healthy watcher clears the failure notice, alarm, and block budget for a future independent episode. A Claude failure notice describes the automatic mechanism as broken and does not direct a routine manual background arm. ### Passive adapters diff --git a/docs/verification/agy.md b/docs/verification/agy.md index 95ac5af68ec..c3a35afebf6 100644 --- a/docs/verification/agy.md +++ b/docs/verification/agy.md @@ -84,7 +84,7 @@ gemini-3.8-flash-low Gemini 3.8 Flash (Low) `agy --help` documents `--effort` as `low|medium|high` and `--model` as the model for the session. The bare `gemini-3.8-flash` id from this home's previous config is not listed; only the suffixed `-high`, `-medium`, and `-low` variants are. `bin/fm-spawn.sh`'s `agy_model_validate` refuses a requested id a reachable `agy models` listing omits, and launches unvalidated with a stderr notice when the listing is unreachable. -The listing is a remote fetch (`Fetching available models...`), so the probe runs with stdin detached under the shared hard bound from `bin/fm-timeout-lib.sh` (15 seconds by default, `FM_AGY_MODELS_TIMEOUT`; a non-positive or non-numeric value clamps back to that default, because a non-positive bound is not a bound); a stalled fetch or a sign-in prompt is cut off and falls through to the unvalidated launch instead of blocking the spawn before any pane exists. +The listing is a remote fetch (`Fetching available models...`), so the probe runs with stdin detached under the shared hard bound from `bin/fm-timeout-lib.sh` (15 seconds by default, `FM_AGY_MODELS_TIMEOUT`; a non-positive or non-numeric value clamps back to that default); a stalled fetch or a sign-in prompt is cut off and falls through to the unvalidated launch instead of blocking the spawn before any pane exists. Print mode (`agy -p "Reply with exactly: AGY_PRINT_PROBE_OK" --model gemini-3.8-flash-low`) returned the exact reply with exit 0 in about 8 seconds, proving the credential path without a pane. ## Busy state: the pinned status row, unknown on absence diff --git a/docs/verification/dispatch-resolve.md b/docs/verification/dispatch-resolve.md index 19b4409dd4d..da0b7ff3b53 100644 --- a/docs/verification/dispatch-resolve.md +++ b/docs/verification/dispatch-resolve.md @@ -295,11 +295,11 @@ The remaining behavior is covered only by the offline tests below: a fenced head `tests/fm-dispatch-resolve.test.sh` drives the public interface with a fake `curl` that records argv, the request body, the header read from file descriptor 3, and whether the secret reached its environment, plus a fake `quota-axi` that performs the same environment check. It proves firstmate can invoke the resolve path without a preflight, rules are snapshotted once from the isolated home's canonical `config/crew-dispatch.json`, and dynamic output fields are flattened to one line. -It proves the absent key (environment and `.env`) prints one stderr line, nothing on stdout, exits 0, and never invokes `curl` or `quota-axi`. +With the default base, it proves the absent key (environment and `.env`) prints one stderr line, nothing on stdout, exits 0, and never invokes `curl` or `quota-axi`. It proves absent, default-only, and empty-rules files return `no rules to match` without a model or quota request, while a broken rules-file symlink exits 2 as unreadable. It proves the documented starter configuration resolves its Pi default through the declared Claude provider, a `.env` key turns the tool on, and the environment wins over it. It proves the key is absent from child environments, never appears on `curl` argv, and arrives only as the bearer header on the descriptor. -It proves the default typesafe.ai request keeps its fixed endpoint and model, carries only the project, the brief's task sections read by the shared brief-heading parser with a scout line only for a scout brief and never a ship brief's delivery mode (or the whole brief when it has neither section), and rule Choice with one option per rule plus the fixed neutral none option, and never carries `why`, `use`, or quota. +It proves the default typesafe.ai request keeps its default endpoint and model, carries only the project, the brief's task sections read by the shared brief-heading parser with a scout line only for a scout brief and never a ship brief's delivery mode (or the whole brief when it has neither section), and rule Choice with one option per rule plus the fixed neutral none option, and never carries `why`, `use`, or quota. It also proves OpenRouter requires an explicit provider and model, accepts the environment or `.env` key, sends the same Choice request to the Decisions API, preserves clear and ambiguous output, withholds a never-send match, and handles malformed responses and HTTP errors. The fake `curl` and `quota-axi` prove that the OpenRouter key stays out of their argv and environments. It proves the credential source declaration with a fake `security` standing in for the macOS Keychain and a fake `uname`, so no case reads the real Keychain. diff --git a/docs/verification/process-event-sources.md b/docs/verification/process-event-sources.md index 05e256cf662..d22866439a4 100644 --- a/docs/verification/process-event-sources.md +++ b/docs/verification/process-event-sources.md @@ -190,6 +190,7 @@ Exercised by `tests/fm-procevent.test.sh` against a fake blocking source whose c | guarded runner startup | the source command does not launch when the detached owner guard rejects an invalid lease configuration, proving the runner waits for positive guard readiness and fails closed when initialization fails | | attached owner continuity | a foreground `start` with a one-second lease remains alive beyond that lease while its caller stays attached, then captures normally when the blocking source completes | | owner-home lifetime and scope | a detached runner and its spawning descendant are observed reparented before an expired owner lease stops their whole process group and process churn; replacing the state directory at the same path cannot keep the old runner alive with a new lease because its recorded device/inode no longer matches, while an identical runner in an unchanged home whose reconcile cycle keeps its lease fresh remains alive | +| detached startup owner presence | `tests/fm-procevent.test.sh` holds reconcile inside a fixture adapter's publication until its two-second lease is stale, then proves its source command starts and retains a live owner; `ensure-listening` likewise starts from an expired lease, both runners still exit after renewal stops, and an inherited runner caller cannot refresh the lease or start a source under its expired lease | | launch pacing during owner-loss grace | an immediately returning source that attempts detached self-relaunches is held to the configured minimum interval between command launches, sampled over three launches under a long lease and then retired, so the case pins pacing rather than lease expiry; replacement starts a fresh pacing generation, prunes prior pacing state, and prevents a superseded sleeping runner from recreating it | | stale reclaim without displacement | concurrent contenders replacing one stale claim start exactly one runner, cross-home replacement removes the old generation's staging file from its recorded state directory, and a generation whose stale owner and independently empty process group prove it gone remains reclaimable when its recorded state-root identity can no longer be revalidated or its recorded registry directory no longer resolves to a directory, so `reconcile` reclaims it once, the replacement runs the source, and later cycles report nothing to do | | confirmed launches only | `reconcile` counts a launch as `started` only after the source is observed owned or its launch-pacing stamp has moved: a registration that cannot start is reported `failed=` with a non-zero exit and its source still listed `none`, a source that claimed, ran and exited before confirmation looked is still `started`, a zero-padded confirm window reads as base 10, and an unusable `FM_PROCEVENT_LAUNCH_CONFIRM_SECONDS` is refused by name before any runner is launched | @@ -267,6 +268,16 @@ The 2026-08-27 review inspected `bin/fm-harness.sh`, `bin/fm-supervision-instruc The [operating contract](../configuration.md#process-to-event-sources-stateprocevent) owns stop authority, the guard's lease and two-read debounce, claim reclamation, and the permanent leak and silent loss of listening after an unrelated leader death. +Detached startup lease checks were verified on 2026-11-05 in a disposable Debian 13 CI-box guest with Bash 5.2.37 against candidate `1a34c1f2b918a020d321c804fb9c006cc1bae3d7` using `bin/fm-test-run.sh tests/fm-procevent.test.sh`. +The command exited 0 with 146 passing checks and `failed=0`. +The focused run included: + +```text +ok - reconcile refreshes at detached launch and still stops after owner lease expiry +ok - ensure-listening refreshes at detached launch and still stops after owner lease expiry +ok - inherited runner callers cannot renew owner presence at detached startup +``` + Measured on 2026-09-08 on macOS (Darwin 25.5.0) against a stand-in poll child that traps TERM, INT, and HUP and keeps blocking: before the repair the guard signalled, lost the leader to that signal, and exited leaving the child running past 70 seconds. The guard caused the permanent leak by destroying the leader needed to prove ownership; a guard that causes that leak is worse than no guard. After the repair, the guard cleared that child in 7.7 seconds with a 5-second lease and 1-second check, and `retire` cleared the same shape in about 2.4 seconds. diff --git a/docs/verification/runtime-backends.md b/docs/verification/runtime-backends.md index 7666dcde2ae..b65fe3fb3a6 100644 --- a/docs/verification/runtime-backends.md +++ b/docs/verification/runtime-backends.md @@ -953,6 +953,30 @@ It captures the launch `bin/fm-spawn.sh` actually builds and confirms that the i It does not claim that a managed policy cannot re-enable hooks after launch. The portable half, `tests/fm-spawn-dispatch-profile.test.sh`, pins the requested hook-disablement flag for crewmates and its omission for secondmates, which run primary sessions on this repository's own project hooks. +### Stop response parsing + +Verified 2026-11-03 on Linux x86_64 with codex-cli 0.162.1. +The existing live hook-layer guard exercises the installed app-server's `hook/completed` notifications against a loopback Responses fixture that returns fixed text and no tool calls. +Each app-server has isolated config, hook trust, login files, and data; it neither uses a shared daemon nor contacts a model provider. +The fixture trusts only its synthetic Stop handler's exact key/hash. + +```sh +bash bin/fm-test-run.sh --jobs 1 tests/fm-turnend-guard.test.sh tests/fm-codex-hook-layer-live-e2e.test.sh +``` + +```text +ok - .codex Stop wrapper: clean success JSON, inherited PATH, unchanged errors and continuation without logout output +ok - codex codex-cli 0.162.1: Stop parser checked 12 isolated cases with clean wrapper JSON and bounded continuation +FM_TEST_SUMMARY total=2 failed=0 skipped_gate=0 duration_ms=97428 +``` + +The portable regression makes its login-shell logout emit terminal escape bytes and confirms that the fixture is effective before checking the tracked command's actual output. +It also checks primary, child-worktree, and non-Firstmate scope, empty input, missing jq, malformed registration, a non-executable guard, inherited PATH, and unchanged error status/stderr. +The optional native park's synchronous guard dispatch preserves its `--guard` argument, original payload, success JSON, and exit-2 stderr in an isolated executable fixture without registering a park. +The live parser accepts both empty output and `{}`, rejects mixed diagnostics/JSON, malformed syntax, multiple objects, and a wrong field type, and preserves an ordinary nonzero error. +The real copied supervision guard blocks once with its stderr reason, then allows the continuation's Stop; the fixture asserts both native hook verdicts and the two-request bound. +These checks refresh the Codex adapter evidence; [the integration owner](../turnend-guard.md#harness-integrations) describes its current behavior. + ## Codex automatic approval review Verified 2026-10-06 with codex-cli 0.160.1. @@ -1093,6 +1117,7 @@ The footer zone is a property of any envelope a glyph row inside it proves, not The demotion is deliberately ASYMMETRIC: `empty` is the only verdict that authorizes `fm-send` to type into a pane, so the rule may move a verdict toward refusing but never toward `empty`. It therefore counts a footer zone only when every row in it is demonstrably furniture - omp's status row, a braille animation row, claude's permission-mode hint row (`⏵⏵ bypass permissions on`), or a row leading with an agent glyph OTHER than the one that proved the envelope, which is what the `→` statusLine is on a `❯` claude pane. A run containing unclaimed activity (`Working on request...`, `→ ran npm test (3 failures)`) is not furniture in either row order and keeps invalidating the envelope above it, and a row leading with the SAME glyph the envelope was proven by (`❯ my typed draft`) is a live composer that keeps winning, so a visible draft is never overwritten. +The one exception, the selected row of Claude 2.1.291's slash-command popup, which leads with that same `❯`, is recorded in [Claude Code 2.1.291 marks the popup's selected row](#claude-code-21291-marks-the-popups-selected-row). `test_composer_footer_zone_refuses_rather_than_allows` pins both directions on the bordered-box and separator-pair shapes. Coverage is the bordered box and the separator pair, the two shapes claude 2.x renders. The opencode left bar is wired into the same rule but is **unexercised**: every left-bar row this repo records leads with plain text, and opencode's own prompt character is `>`, a shell glyph deliberately outside the agent set, so no opencode shape recorded here can prove a left-bar envelope or open a footer zone beneath one. @@ -1520,6 +1545,23 @@ The whole real-Herdr lane's latest active verification uses both Herdr 0.7.4 pro Protocol 17 keeps every protocol-16 feature gate satisfied; the event and workspace-move floors remain 16. Default-on presentation projection has its own floor at Herdr 0.8.0, protocol 19, verified below. +Projected worktree entry was verified on 2026-11-03 with Herdr 0.9.5 on macOS arm64: + +```sh +bash bin/fm-test-run.sh tests/fm-herdr-projected-cd-live-e2e.test.sh +``` + +```text +ok - Herdr 0.9.5 projected spawn enters its recorded copy (delayed=0) +ok - Herdr 0.9.5 projected spawn enters its recorded copy (delayed=1) +``` + +This token-free guard provisions only a named non-default Herdr lab session, drives the real projected spawn, cd, and cwd assertion, and verifies the exact foreground cwd against the recorded copy. +On a supported Herdr below the presentation floor, where `fm_backend_herdr_presentation_release_supported` keeps spawns flat, it prints a skip naming the release and exits 0 instead of running the projected cases. +Its delayed case holds the first real cd behind a 15-second shell command and verifies exactly one resend; its ordinary case verifies no redundant cd. +The lease fixture returns a disposable clone without accessing a shared Treehouse pool. +The portable delayed, dropped, and never-moving entry regressions are in `tests/fm-spawn-herdr-lease.test.sh`. + Core read-only probes: ```sh @@ -1707,7 +1749,7 @@ The pre-Enter payload proof then judged the typed command unsent, pressed Ctrl+U The fix captures the FULL VISIBLE VIEWPORT for every herdr adapter composer read (`pane read --source visible [--format ansi]`, `fm_backend_herdr_composer_state` and `fm_backend_herdr_composer_content`): the composer is by definition inside the viewport, and the viewport is the one bound that always contains it. The shared inbox pending-line confirmation read (`bin/fm-task-inbox-lib.sh`) stays a bounded tail on every backend, herdr included; its payloads are task lines, not slash commands, so the popup shape does not arise there. -The popup rows sit below the composer's closing rule, which is a structural edge row, so the shared classifier still selects only the composer and the menu rows never read as typed text. +The popup rows sit below the composer's closing rule, which is a structural edge row, so the shared classifier still selects only the composer and the unmarked menu rows never read as typed text (Claude Code 2.1.283 through 2.1.289; 2.1.291 marks the selected row, see below). Verified live in the lab: with the popup up the state read answers `pending` (previously `empty`) and the payload proof returns `/exit` (previously empty), the submit presses Enter, and the Claude process exits, leaving the shell prompt. Growing the window only adds rows above the composer, so the bottom-most-shape selection, the footer zone, and every previously passing verdict are unchanged. @@ -1722,7 +1764,7 @@ ok - fm_backend_herdr_composer_state: a slash-command popup cannot hide a typed ok - fm_backend_herdr_send_text_submit: a typed slash command hidden behind its popup is still proven and submitted ``` -Live guard (third scenario of the opt-in guard, verifying the agent actually exited): +Live guard (the `/exit` scenario of the opt-in guard, verifying the agent actually exited): ```sh FM_HERDR_SUBMIT_CONFIRM_LIVE=1 tests/fm-herdr-submit-confirm-live-e2e.test.sh @@ -1732,6 +1774,66 @@ FM_HERDR_SUBMIT_CONFIRM_LIVE=1 tests/fm-herdr-submit-confirm-live-e2e.test.sh ok - live Herdr submit confirm: Claude Code (2.1.283 (Claude Code)) on herdr 0.9.0 proves and submits a typed /exit behind its command popup ``` +#### Claude Code 2.1.291 marks the popup's selected row + +Measured 2026-10-06 against Herdr 0.8.2 and Claude Code 2.1.289 and 2.1.291 on Haiku 4.5 in isolated `fm-lab-` sessions. + +Claude Code 2.1.283, 2.1.284, 2.1.285, 2.1.286, 2.1.288 and 2.1.289 draw the popup's selected row unmarked. +Claude Code 2.1.291 (2.1.290 is not installed here) draws it as ` ❯ /exit Exit the CLI`, with the `❯` its composer uses, and indents the other rows to match. +That row lies below the composer's closing rule, so it became the bottom-most bare agent-glyph row. +The footer-zone rule deliberately keeps a row that leads with the envelope's own glyph as a live composer, so the popup's first row won the selection and the typed command's read-back was the whole popup list. +The pre-Enter payload proof then judged `/exit` unsent, pressed Ctrl+U, and reported `send-failed`, so `bin/fm-control.sh exit` and every slash command sent through `bin/fm-send.sh` failed on Herdr. +The notification row Claude draws under the composer when it runs inside another Claude session is not involved: the failure reproduces without it. + +`_fm_composer_select_cursorless` now demotes that row to the envelope above it, through `_fm_composer_bare_is_popup_selection`, only when every condition holds. +The row lies below the closing row of a glyph-proven envelope. +That envelope's own glyph row leads with the agent glyph, with no side border to strip (a bordered box keeps its previous reading), and holds typed text opening with `/`, read under the ghost-strip threshold of the calling read (the state read or the payload read), so the read it falls back to is never empty: typed text that read strips as ghost cannot license the demotion. +The row opens with `/` (or `…`, a name truncated to its column) and carries a gap of two or more spaces before its description, which a numbered-choice dialog row lacks. +The selected popup row is not always the typed text (typing `/` selects the first command, and a long plugin command name is truncated), so no equality check is made. +Any other row keeps its previous reading, and none of these can read `empty`. +Unverified follow-up: a bordered-box composer with the marked popup row below it is out of scope here. +Its state read still refuses with `pending`, but its payload read still returns the popup list, so a slash command typed into a boxed Claude 2.1.291 composer would still fail the pre-Enter proof; no capture or live run here shows 2.1.291 drawing the box with the popup up. +The other cursorless backends (cmux, orca, tern, zellij) share this classifier and inherit the change without a live check here. + +Portable tests run against real captures (`tests/captures/claude-2.1.291-slash-popup`): + +```sh +tests/fm-composer-lib.test.sh +tests/fm-backend-herdr.test.sh +``` + +These three are before-and-after regressions: each fails against the previous classifier and passes against this one. + +```text +ok - fm_composer_extract_selected_content: Claude 2.1.291's marked popup row is not read as the composer +ok - fm_backend_herdr_composer_content: Claude 2.1.291's marked popup row is not read as the typed command +ok - fm_backend_herdr_send_text_submit: a slash command behind Claude 2.1.291's marked popup row is proven and submitted +``` + +These two are guards, not regressions: they already pass against the previous classifier, and check that the demotion keeps the refusals that classifier made. + +```text +ok - fm_composer_classify_screen: the popup demotion needs typed slash text and the popup row shape, and never reads empty +ok - fm_backend_herdr_send_text_submit: with Claude 2.1.291's popup on screen a suffix, a placeholder plus remainder, or a prefix is still refused and cleared +``` + +Live guard, which fails against the previous classifier on 2.1.291 at the `/context` scenario (`send-failed`), with `ANTHROPIC_MODEL=claude-haiku-4-5-20251001` selecting the model (the pane banner reads `Haiku 4.5`): + +```sh +FM_HERDR_SUBMIT_CONFIRM_LIVE=1 tests/fm-herdr-submit-confirm-live-e2e.test.sh +``` + +```text +ok - live Herdr submit confirm: Claude Code (2.1.291 (Claude Code)) on herdr 0.8.2 reports empty and renders the requested reply in isolated session fm-lab-herdr-submit-con-1098793-23839 +ok - live Herdr submit confirm: Claude Code (2.1.291 (Claude Code)) on herdr 0.8.2 submits a U+2063 away-supervisor payload whose read-back drops the mark +ok - live Herdr submit confirm: Claude Code (2.1.291 (Claude Code)) on herdr 0.8.2 proves and submits a typed /context behind its command popup and renders its report +ok - live Herdr submit confirm: Claude Code (2.1.291 (Claude Code)) on herdr 0.8.2 proves and submits a typed /exit behind its command popup +``` + +The real control and steer paths, run by hand against Claude Code 2.1.291 in the lab with a scratch home whose task record names the pane, answered as follows. +`bin/fm-control.sh exit` answered `error: the exit command could not be sent to task on herdr` before the change and `stopped harness=claude backend=herdr ...` after it. +`bin/fm-send.sh /context` answered `error: text not sent to (herdr send failed; ...)` with exit 1 before the change, and exit 0 after it with Claude's `/context` output rendered. + ### Claude background-task exit picker Measured 2026-10-05 against Claude Code 2.1.289 in an isolated tmux session. diff --git a/docs/watcher-continuity.md b/docs/watcher-continuity.md index d858dbcbd10..6e1dab73794 100644 --- a/docs/watcher-continuity.md +++ b/docs/watcher-continuity.md @@ -94,8 +94,11 @@ While supervision is still needed and away mode remains inactive, an actionable The hook handles the session lock as follows: -- A session-lock owner, including a tagged `win:` holder on Windows/Git Bash, that fails the shared `fm_harness_pid_alive` predicate is reclaimed through `bin/fm-lock.sh` before auto-arm state changes. -- A live owner the session does not own, an absent lock, or an unusable lock keeps the competing hook inert. +- A well-formed session-lock owner is a numeric PID, or a tagged `win:` value on a Cygwin-family Git Bash host. + The shared `fm_session_lock_pid_blocks_reclaim` verdict in `bin/fm-session-lock-lib.sh` is the one decision every recovery caller applies to that identity: an all-zero, proven-dead, reused non-harness, or live-but-unsignalable record stays reclaimable, while a readable verified harness and a live owner whose identity cannot be read keep recovery inert. + That library also owns the process-info fallback used when the `ps` binary cannot execute. +- An absent lock keeps the competing hook inert: with no recorded owner there is nothing stale to recover. +- A malformed, empty, or otherwise unusable record is not an identity, so it never reaches that verdict: the competing hook stays inert, and a later `bin/fm-lock.sh` acquisition replaces the record. Whether the session owns that lock is the shared `fm_session_lock_owned_by_self` verdict in `bin/fm-session-lock-lib.sh`. That verdict accepts either of two cases: @@ -115,7 +118,7 @@ The beacon is `state/.last-watcher-beat`, which only the watcher process touches - A cycle-end failure is benign when that live-watcher predicate is true. In that case the hook suppresses the arm output and continues silently. -[`turnend-guard.md`](turnend-guard.md#failure-progression-and-block-budget) owns the failure-notice commit contract, monotonic failure progression, attended-alarm writers, suppression reasons, and positive recovery reset. +[`turnend-guard.md`](turnend-guard.md#failure-progression-and-block-budget) owns the failure-notice commit contract, monotonic failure progression, attended-alarm writers, suppression reasons, positive recovery reset, and session-bound interruption isolation. ### Supervision host @@ -345,7 +348,7 @@ Main is woken for it on that check's own triggering close. Under the away-posture record the exclusion lifts and a check row is offered to and claimed by the branch like every other actionable row. `fm-wake-drain.sh` never reclassifies a row itself. -It filters the queue to the current actor's opaque claim before same-key deduplication, then presents and acknowledges only that actor-local view. +It filters the queue to the current actor's opaque claim before presentation deduplication through [`fm_wake_print_deduped`](../bin/fm-wake-lib.sh), then presents and acknowledges only that actor-local view. A missing or empty branch snapshot is refused loudly rather than read as "nothing eligible". With a live branch owner, a missing snapshot means the branch's own acknowledgement already consumed the grant, and the refusal says so; otherwise reaching the drain without the non-empty handoff promised by the extension is a wiring bug. A branch acknowledgement retires the check-row receipts - inactive-outcome, inactive-reconcile notice, and secondmate stall - of exactly the granted sequences it consumes, so a branch-consumed check is never re-queued by its producer. @@ -535,13 +538,14 @@ It checks that a newly appended keyed decision is classified without rereading e - Supersession of a dead session's orphaned claim by the replacement session's first Stop, including a resumed session that carries the dead one's conversation id, and a claim with no readable session lock. - A recycled background bridge keeping its live claim on the trusted session id pinned to the lock holder's recorded session-lock identity. - The Windows/Git Bash session-lock readers for the supervision-host key, claim read-back, and mid-turn health, plus untagged-value parity with the numeric helpers, in `tests/fm-session-lock-windows-consumers.test.sh`. +- The ps-free identity route and its fail-closed refusals when the `ps` binary cannot execute, in `tests/fm-session-lock-ps-denied.test.sh`. - Bounded failure retries. - Benign live-watcher cycle ends. - One-notice failure episodes. - Exit-2 translation. - The handling successor an ended attached cycle starts with the closed arm as its predecessor and that outlives the rewake. - An unconfirmed successor reported in the banner without withholding the wake. -- Host-timeout HUP/TERM/INT translation into the same durable failure handoff. +- The [session-bound HUP/TERM/INT failure handoff](turnend-guard.md#failure-progression-and-block-budget) and replacement-session notice isolation. It also covers generation-claim single-flight, stuck-claim supersession, superseded-owner silence, notice-marker refusal and retry, ownership-atomic episode reset, and the legacy upgrade shim. [`turnend-guard.md`](turnend-guard.md) owns those behavior contracts. diff --git a/tests/assets/board-render-harness.mjs b/tests/assets/board-render-harness.mjs index c181aed5c88..425c77c7a4d 100644 --- a/tests/assets/board-render-harness.mjs +++ b/tests/assets/board-render-harness.mjs @@ -4,7 +4,7 @@ // // Usage: node board-render-harness.mjs // Prints one JSON document: -// { stats:[{n,label}], underway:[{title,sub,badges}], +// { stats:[{n,label}], captains_call:[text], underway:[{title,sub,badges}], // charted:[{title,sub,badges,pickable}], empty, more, error } import { readFileSync } from "node:fs"; @@ -27,6 +27,13 @@ class Node { this.classList = { add: (c) => { this.className = (this.className + " " + c).trim(); }, contains: (c) => this.className.split(/\s+/).includes(c), + toggle: (c, force) => { + const classes = new Set(this.className.split(/\s+/).filter(Boolean)); + const enabled = force ?? !classes.has(c); + if (enabled) classes.add(c); else classes.delete(c); + this.className = [...classes].join(" "); + return enabled; + }, }; } get textContent() { @@ -110,6 +117,9 @@ const rowsOf = (container) => const uw = byId.get("bb-underway") || new Node("div"); const underway = rowsOf(uw); +const captains_call = (byId.get("bb-call") || new Node("div")).children + .filter((n) => n.className.split(/\s+/).includes("bb-decision")) + .map((n) => n.textContent); const ch = byId.get("bb-charted") || new Node("div"); const charted = rowsOf(ch); @@ -123,4 +133,4 @@ const empty = ch.children.filter((c) => c.className.includes("bb-empty")).map((c const more = ch.children.filter((c) => c.className.includes("bb-morechip")).map((c) => c.textContent); process.stdout.write( - JSON.stringify({ stats, underway, charted, empty, more, error: errorText }) + "\n"); + JSON.stringify({ stats, captains_call, underway, charted, empty, more, error: errorText }) + "\n"); diff --git a/tests/captures/claude-2.1.291-slash-popup/exit-typed-with-notice.ansi b/tests/captures/claude-2.1.291-slash-popup/exit-typed-with-notice.ansi new file mode 100644 index 00000000000..5679e5cb187 --- /dev/null +++ b/tests/captures/claude-2.1.291-slash-popup/exit-typed-with-notice.ansi @@ -0,0 +1,10 @@ +────────────────────────────────────────────────────────────────────────────────────────────── +❯ /exit +────────────────────────────────────────────────────────────────────────────────────────────── + ⚠ Transcript saving is off — inherited CLAUDE_CODE_CHILD_SESSION marker · restart with CL… + ❯ /exit Exit the CLI +  /edit-article Edit and improve articles by restructuring + sections, improving clarity, and tightening prose.… +  /context Visualize current context usage as a colored grid +  /usage-credits Configure usage credits or request them from your + admin when you hit a limit diff --git a/tests/captures/claude-2.1.291-slash-popup/exit-typed.ansi b/tests/captures/claude-2.1.291-slash-popup/exit-typed.ansi new file mode 100644 index 00000000000..bbd8d635102 --- /dev/null +++ b/tests/captures/claude-2.1.291-slash-popup/exit-typed.ansi @@ -0,0 +1,11 @@ +────────────────────────────────────────────────────────────────────────────────────────────── +❯ /exit +────────────────────────────────────────────────────────────────────────────────────────────── + ❯ /exit Exit the CLI +  /edit-article Edit and improve articles by restructuring + sections, improving clarity, and tightening prose.… +  /context Visualize current context usage as a colored grid +  /usage-credits Configure usage credits or request them from your + admin when you hit a limit +  /quiet Enter quiet supervision mode when the captain + invokes /quiet or asks for quiet mode, quiet-while… diff --git a/tests/captures/claude-2.1.291-slash-popup/idle.ansi b/tests/captures/claude-2.1.291-slash-popup/idle.ansi new file mode 100644 index 00000000000..29082189215 --- /dev/null +++ b/tests/captures/claude-2.1.291-slash-popup/idle.ansi @@ -0,0 +1,5 @@ +────────────────────────────────────────────────────────────────────────────────────────────── +❯  +────────────────────────────────────────────────────────────────────────────────────────────── + ⏵⏵ bypass permissions on (shift+tab to cycle) · ← for agents + Advisor Tool (experimental) is on and may use more tokens · /advisor diff --git a/tests/captures/claude-2.1.291-slash-popup/no-mistakes-typed.ansi b/tests/captures/claude-2.1.291-slash-popup/no-mistakes-typed.ansi new file mode 100644 index 00000000000..9828e267089 --- /dev/null +++ b/tests/captures/claude-2.1.291-slash-popup/no-mistakes-typed.ansi @@ -0,0 +1,5 @@ +────────────────────────────────────────────────────────────────────────────────────────────── +❯ /no-mistakes +────────────────────────────────────────────────────────────────────────────────────────────── + ❯ /no-mistakes Validate your code changes through the no-mistakes + pipeline - automated code review, tests, lint, doc… diff --git a/tests/captures/claude-2.1.291-slash-popup/slash-only.ansi b/tests/captures/claude-2.1.291-slash-popup/slash-only.ansi new file mode 100644 index 00000000000..0f143d22562 --- /dev/null +++ b/tests/captures/claude-2.1.291-slash-popup/slash-only.ansi @@ -0,0 +1,9 @@ +────────────────────────────────────────────────────────────────────────────────────────────── +❯ / +────────────────────────────────────────────────────────────────────────────────────────────── + ❯ /no-mistakes Validate your code changes through the no-mistakes + pipeline - automated code review, tests, lint, doc… +  /handoff Compact the current conversation into a handoff + document for another agent to pick up. (user) +  /dataviz Use this skill whenever you are about to create ANY + chart, graph, plot, dashboard, or data visualizati… diff --git a/tests/captures/claude-2.1.291-slash-popup/truncated-name.ansi b/tests/captures/claude-2.1.291-slash-popup/truncated-name.ansi new file mode 100644 index 00000000000..a0e18820057 --- /dev/null +++ b/tests/captures/claude-2.1.291-slash-popup/truncated-name.ansi @@ -0,0 +1,7 @@ +────────────────────────────────────────────────────────────────────────────────────────────── +❯ /claude-md-management:claude-md-improver +────────────────────────────────────────────────────────────────────────────────────────────── + ❯ …e-md-management:claude-md-improver (claude-md-management) Audit and improve CLAUDE.md + files in repositories. Use when user asks to check… +  …ude-md-management:revise-claude-md (claude-md-management) Update CLAUDE.md with + learnings from this session diff --git a/tests/fm-backend-herdr.test.sh b/tests/fm-backend-herdr.test.sh index 2c200a1c446..c5828f1f9f6 100755 --- a/tests/fm-backend-herdr.test.sh +++ b/tests/fm-backend-herdr.test.sh @@ -5702,7 +5702,9 @@ herdr_wrapped_composer() { # # sits inside a solid-rule pair (rule above, rule below), exactly as live # Claude draws it, with the menu rows below the closing rule; the rules are # structural edge rows, so the composer's content block ends there and the -# menu rows never read as typed text. +# unmarked menu rows never read as typed text. Claude Code 2.1.291 marks the +# popup's selected row with a `❯` of its own, which is a different shape; the +# real captures under tests/captures/claude-2.1.291-slash-popup cover it. herdr_popup_composer_screen() { # local i typed=$1 rule rule=$(printf '%0.s\xe2\x94\x80' $(seq 1 60)) @@ -5947,6 +5949,98 @@ test_send_text_submit_claude_slash_popup_composer_is_still_proven_and_submitted( pass "fm_backend_herdr_send_text_submit: a typed slash command hidden behind its popup is still proven and submitted" } +# Claude Code 2.1.291 draws the selected row of its slash-command popup with the +# same `❯` marker as its composer, so that row was read as a second composer and +# the popup list as the typed command: fm-control exit and every slash command +# sent through fm-send judged the payload unsent, cleared it, and reported +# send-failed. The screens are real captures (Herdr 0.8.2, Haiku 4.5). +herdr_popup_capture() { # + cat "$ROOT/tests/captures/claude-2.1.291-slash-popup/$1.ansi" +} + +# herdr_popup_capture_with_composer: the real capture with its composer +# row replaced by , the popup rows below it left exactly as +# captured. Used for the refusals that must hold while a popup is on screen. +herdr_popup_capture_with_composer() { # + local name=$1 text=$2 + { + herdr_popup_capture "$name" | sed -n 1p + printf '\xe2\x9d\xaf\xc2\xa0%s\n' "$text" + herdr_popup_capture "$name" | sed -n '3,$p' + } +} + +test_composer_content_claude_2_1_291_marked_popup_row_is_not_the_composer() { + local dir log resp fb out pair name want + for pair in exit-typed:/exit exit-typed-with-notice:/exit no-mistakes-typed:/no-mistakes; do + name=${pair%%:*} + want=${pair#*:} + dir="$TMP_ROOT/composer-claude-2-1-291-popup-$name"; mkdir -p "$dir/responses"; log="$dir/log"; resp="$dir/responses"; : > "$log" + herdr_popup_capture "$name" > "$resp/1.out" + fb=$(make_herdr_fakebin "$dir") + out=$( PATH="$fb:$PATH" FM_HERDR_LOG="$log" FM_HERDR_RESPONSES="$resp" \ + bash -c '. "$0/bin/backends/herdr.sh"; fm_backend_herdr_composer_content default:w1:p2' "$ROOT" ) + [ "$out" = "$want" ] || fail "$name: the composer read must be '$want', not the popup list, got '$out'" + printf '%s\n' "$out" | grep -F 'Exit the CLI' >/dev/null && fail "$name: the popup list leaked into the composer read" + dir="$TMP_ROOT/composer-state-claude-2-1-291-popup-$name"; mkdir -p "$dir/responses"; log="$dir/log"; resp="$dir/responses"; : > "$log" + herdr_popup_capture "$name" > "$resp/1.out" + fb=$(make_herdr_fakebin "$dir") + out=$( PATH="$fb:$PATH" FM_HERDR_LOG="$log" FM_HERDR_RESPONSES="$resp" \ + bash -c '. "$0/bin/backends/herdr.sh"; fm_backend_herdr_composer_state default:w1:p2' "$ROOT" ) + [ "$out" = pending ] || fail "$name: a typed command above its popup must read pending, got '$out'" + done + pass "fm_backend_herdr_composer_content: Claude 2.1.291's marked popup row is not read as the typed command" +} + +test_send_text_submit_claude_2_1_291_marked_popup_row_is_proven_and_submitted() { + local dir log resp fb out enter_count pair name text + for pair in exit-typed:/exit exit-typed-with-notice:/exit no-mistakes-typed:/no-mistakes; do + name=${pair%%:*} + text=${pair#*:} + dir="$TMP_ROOT/submit-claude-2-1-291-popup-$name"; mkdir -p "$dir/responses"; log="$dir/log"; resp="$dir/responses"; : > "$log" + herdr_submit_claude_prefix "$resp" "$text" + printf '{"result":{"agent":{"agent":"claude","agent_status":"idle"}}}\n' > "$resp/5.out" + printf '{"result":{"agent":{"agent_status":"working"}}}\n' > "$resp/7.out" + herdr_popup_capture "$name" > "$resp/4.out" + fb=$(make_herdr_fakebin "$dir") + out=$( PATH="$fb:$PATH" FM_HERDR_LOG="$log" FM_HERDR_RESPONSES="$resp" FM_BACKEND_HERDR_SUBMIT_POLLS=1 \ + bash -c '. "$0/bin/backends/herdr.sh"; fm_backend_herdr_send_text_submit default:w1:p2 "$1" 3 0.01 0.01' "$ROOT" "$text" ) + [ "$out" = empty ] || fail "$name: a typed $text proven above its marked popup row must be submitted, got '$out'" + enter_count=$(grep -c $'\x1f''pane'$'\x1f''send-keys'$'\x1f''w1:p2'$'\x1f''enter' "$log") + [ "$enter_count" -eq 1 ] || fail "$name: the proven typed command should be submitted once, sent $enter_count Enter(s)" + [ "$(herdr_ctrl_u_count "$log")" -eq 0 ] || fail "$name: a proven composer must not be cleared" + done + pass "fm_backend_herdr_send_text_submit: a slash command behind Claude 2.1.291's marked popup row is proven and submitted" +} + +# The marked popup row must not weaken the head-truncation protection: a +# composer holding only a suffix, a paste placeholder followed by a literal +# remainder, or only a prefix of the payload is still refused and cleared while +# the same popup is on screen. +test_send_text_submit_claude_2_1_291_popup_still_refuses_a_partial_payload() { + local dir log resp fb out enter_count name capture text composer + while IFS='|' read -r name capture text composer; do + dir="$TMP_ROOT/submit-claude-2-1-291-popup-refuse-$name"; mkdir -p "$dir/responses"; log="$dir/log"; resp="$dir/responses"; : > "$log" + herdr_submit_claude_prefix "$resp" "$text" + herdr_popup_capture_with_composer "$capture" "$composer" > "$resp/4.out" + printf ' \xe2\x9d\xaf\n' > "$resp/6.out" + printf '{"result":{"agent":{"agent_status":"working"}}}\n' > "$resp/7.out" + fb=$(make_herdr_fakebin "$dir") + out=$( PATH="$fb:$PATH" FM_HERDR_LOG="$log" FM_HERDR_RESPONSES="$resp" FM_BACKEND_HERDR_SUBMIT_POLLS=1 \ + bash -c '. "$0/bin/backends/herdr.sh"; fm_backend_herdr_send_text_submit default:w1:p2 "$1" 3 0.01 0.01' "$ROOT" "$text" ) + [ "$out" = send-failed ] || fail "$name: a partial payload ('$composer') under the popup must report send-failed, got '$out'" + enter_count=$(grep -c $'\x1f''pane'$'\x1f''send-keys'$'\x1f''w1:p2'$'\x1f''enter' "$log") + [ "$enter_count" -eq 0 ] || fail "$name: a partial payload must not be submitted, sent $enter_count Enter(s)" + [ "$(herdr_ctrl_u_count "$log")" -eq 1 ] || fail "$name: the refused partial payload should be cleared with one Ctrl+U" + done <<'EOF' +suffix|no-mistakes-typed|/no-mistakes|mistakes +placeholder-remainder|no-mistakes-typed|/no-mistakes|[Pasted text #1] mistakes +prefix|no-mistakes-typed|/no-mistakes now|/no-mistakes +popup-suffix|exit-typed|/exit|xit +EOF + pass "fm_backend_herdr_send_text_submit: with Claude 2.1.291's popup on screen a suffix, a placeholder plus remainder, or a prefix is still refused and cleared" +} + # Live Claude Code 2.1.283 draws a recognized typed slash command in muted # truecolor grey (38;2;112;112;112, luminance 112), below the grok-tuned # dark-foreground ghost threshold. Claude's own ghost suggestion is SGR-2 dim, @@ -6679,6 +6773,64 @@ test_wait_transition_clean_timeout_returns_1() { # shellcheck source=bin/fm-backend.sh . "$ROOT/bin/fm-backend.sh" +# fm_backend_herdr_canonical_socket_path folds a Windows drive-letter spelling +# into the POSIX spelling of the same socket, so the launcher's claimed socket +# and the session list's socket_path compare equal on native Windows. `cygpath` +# ships only with Git for Windows, so these cases stub it and therefore run +# everywhere; the guard in the adapter matches only `[A-Za-z]:[\/]*`, a shape +# that cannot occur on Linux or macOS. +# make_cygpath_fakebin [mapped-path] -> echoes fakebin dir +make_cygpath_fakebin() { + local dir=$1 mode=$2 mapped=${3-} fb="$1/cygpath-fakebin" + mkdir -p "$fb" + case "$mode" in + map) printf '#!/usr/bin/env bash\nprintf %%s %s\n' "$(printf '%q' "$mapped")" > "$fb/cygpath" ;; + fail) printf '#!/usr/bin/env bash\nexit 1\n' > "$fb/cygpath" ;; + empty) printf '#!/usr/bin/env bash\nprintf ""\n' > "$fb/cygpath" ;; + esac + chmod +x "$fb/cygpath" + printf '%s' "$fb" +} + +canonical_socket_path_via() { # -> stdout, status + local fb=$1 path=$2 + if [ -n "$fb" ]; then + PATH="$fb:$PATH" bash -c '. "$0/bin/backends/herdr.sh"; fm_backend_herdr_canonical_socket_path "$1"' "$ROOT" "$path" + else + bash -c '. "$0/bin/backends/herdr.sh"; fm_backend_herdr_canonical_socket_path "$1"' "$ROOT" "$path" + fi +} + +test_canonical_socket_path_folds_a_windows_drive_letter_spelling() { + local dir sock fb posix_id win_id + dir="$TMP_ROOT/cygpath-map"; mkdir -p "$dir" + sock="$dir/herdr.sock"; : > "$sock" + fb=$(make_cygpath_fakebin "$dir" map "$sock") + posix_id=$(canonical_socket_path_via '' "$sock") + [ -n "$posix_id" ] || fail "the POSIX spelling must canonicalize to a non-empty identity" + win_id=$(canonical_socket_path_via "$fb" 'C:\fake\herdr.sock') + [ "$win_id" = "$posix_id" ] || \ + fail "a drive-letter spelling must normalize to the same identity as its POSIX spelling (got '$win_id', want '$posix_id')" +} + +test_canonical_socket_path_refuses_a_failed_cygpath() { + local dir fb status + dir="$TMP_ROOT/cygpath-fail"; mkdir -p "$dir" + fb=$(make_cygpath_fakebin "$dir" fail) + status=0 + canonical_socket_path_via "$fb" 'C:\fake\herdr.sock' >/dev/null || status=$? + expect_code 1 "$status" "a drive-letter path whose cygpath conversion fails must be refused, not compared half-converted" +} + +test_canonical_socket_path_refuses_empty_cygpath_output() { + local dir fb status + dir="$TMP_ROOT/cygpath-empty"; mkdir -p "$dir" + fb=$(make_cygpath_fakebin "$dir" empty) + status=0 + canonical_socket_path_via "$fb" 'C:\fake\herdr.sock' >/dev/null || status=$? + expect_code 1 "$status" "a drive-letter path whose cygpath output is empty must be refused" +} + test_version_check_accepts_current_protocol test_version_check_refuses_old_protocol @@ -6916,6 +7068,9 @@ test_send_text_submit_accepts_marked_payloads_whose_read_back_drops_u2063 test_send_text_submit_refuses_marked_digest_missing_its_head test_composer_state_claude_slash_popup_pushes_composer_above_tail_window test_send_text_submit_claude_slash_popup_composer_is_still_proven_and_submitted +test_composer_content_claude_2_1_291_marked_popup_row_is_not_the_composer +test_send_text_submit_claude_2_1_291_marked_popup_row_is_proven_and_submitted +test_send_text_submit_claude_2_1_291_popup_still_refuses_a_partial_payload test_send_text_submit_claude_grey_slash_command_is_proven_and_submitted test_send_text_submit_lone_paste_placeholder_submits_the_long_payload test_send_text_submit_multiline_paste_placeholder_submits_the_long_payload @@ -6942,3 +7097,6 @@ test_wait_transition_stream_absorb_clears_then_timeout test_wait_transition_reader_failure_returns_2 test_wait_transition_bad_ack_returns_2_and_cleans_up test_wait_transition_clean_timeout_returns_1 +test_canonical_socket_path_folds_a_windows_drive_letter_spelling +test_canonical_socket_path_refuses_a_failed_cygpath +test_canonical_socket_path_refuses_empty_cygpath_output diff --git a/tests/fm-backend-rex.test.sh b/tests/fm-backend-rex.test.sh index 750d504ebfa..2ce3d11f989 100644 --- a/tests/fm-backend-rex.test.sh +++ b/tests/fm-backend-rex.test.sh @@ -939,10 +939,16 @@ test_firstmate_records_never_count_as_worker_state() { assert_equals unknown "$(rex_env "$PUSH_DIR" "fm_backend_busy_state rex '$PUSH_T'")" "a Firstmate record must not decide busy state" res=$(push_wait 1) assert_equals "1|" "$res" "the level reconcile must ignore a Firstmate record" + printf '{"records":[{"state":"working","app":"firstmate-grok","title":"fm-t1","owner":{"pid":4242}}]}\n' \ + >"$PUSH_DIR/fake/status/${PUSH_BID#block:}.json" + assert_equals unknown "$(rex_env "$PUSH_DIR" "fm_backend_busy_state rex '$PUSH_T'")" "the status bridge's own root record must not decide busy state" rm -f "$PUSH_DIR/fake/status/${PUSH_BID#block:}.json" rex_event "$PUSH_BID" blocked question firstmate 4242 "Needs your decision" >"$PUSH_DIR/fake/events" res=$(push_wait 1) assert_equals "1|" "$res" "a streamed Firstmate record must never escalate" + rex_event "$PUSH_BID" blocked question firstmate-grok 4242 "Needs your decision" >"$PUSH_DIR/fake/events" + res=$(push_wait 1) + assert_equals "1|" "$res" "a streamed status-bridge record must never escalate" rex_event "$PUSH_BID" blocked question claude-code 4242 "Continue?" >"$PUSH_DIR/fake/events" res=$(push_wait 1) [ "${res%%|*}" = 0 ] || fail "the harness's own blocked edge must still escalate, got $res" diff --git a/tests/fm-backend-t3code.test.sh b/tests/fm-backend-t3code.test.sh index 154c578e73f..d83fa4a8a91 100755 --- a/tests/fm-backend-t3code.test.sh +++ b/tests/fm-backend-t3code.test.sh @@ -16,6 +16,7 @@ unset GIT_CONFIG_COUNT GIT_CONFIG_KEY_0 GIT_CONFIG_VALUE_0 GIT_CONFIG_PARAMETERS . "$(dirname "${BASH_SOURCE[0]}")/lib.sh" # shellcheck source=tests/t3-fake-lib.sh . "$(dirname "${BASH_SOURCE[0]}")/t3-fake-lib.sh" +. "$ROOT/bin/fm-watch-key-lib.sh" command -v node >/dev/null 2>&1 || { echo "skip: node absent"; exit 0; } @@ -1105,8 +1106,7 @@ test_t3_stale_watcher() { # [harness] [fr local session=$1 expected=$2 harness=${3:-codex} state fb hash out _ local fresh=${4:-} turn=${5:-} busy_bound=3600 turn_at local thread=mcp:6a0e1f2b-3c4d-4a5b-8c6d-0123456789ab key - # The watcher's per-window marker key turns `:` into `_` (fm-watch.sh window_key). - key=${thread//:/_} + key=$(window_key "$thread") t3_case "watch-$session-$harness-$fresh-$turn" "$session" t3_world "$(t3_thread_json "$thread" "$session" false)" # The spawn record below is aged past the bound, so only T3's own run diff --git a/tests/fm-backlog-atomicity.test.sh b/tests/fm-backlog-atomicity.test.sh index c240aa08c00..5400b51719f 100755 --- a/tests/fm-backlog-atomicity.test.sh +++ b/tests/fm-backlog-atomicity.test.sh @@ -1628,6 +1628,32 @@ test_completion_closes_a_scout_with_its_report() { pass "completion closes a scout item against its report" } +test_forced_scout_discard_records_no_report() { + local case_dir id report out + for report in missing partial; do + id="atomic-discard-scout-$report" + case_dir=$(make_home "discard-scout-$report") + add_item "$case_dir" "$id" scout + start_item "$case_dir" "$id" + write_task_meta "$case_dir" "$id" scout '' "spawn_gen=spawn-discard-$report" + if [ "$report" = partial ]; then + mkdir -p "$(home_of "$case_dir")/data/$id" + printf 'unfinished findings\n' > "$(home_of "$case_dir")/data/$id/report.md" + fi + + out=$(run_teardown "$case_dir" "$id" --force) \ + || fail "forced scout discard failed for $report report: $out" + [ "$(row_state "$case_dir" "$id")" = "done" ] \ + || fail "forced scout discard did not close the backlog item" + assert_grep 'discarded' "$(backlog_of "$case_dir")" \ + "forced scout discard did not record its discarded outcome" + if grep -Fq "data/$id/report.md" "$(backlog_of "$case_dir")"; then + fail "forced scout discard recorded a report path for $report report" + fi + done + pass "forced scout discards record no report for missing or partial reports" +} + test_completion_refuses_a_legacy_record_without_an_incarnation() { local case_dir id meta out rc=0 id=atomic-close-legacy-no-incarnation-b7 @@ -2063,6 +2089,27 @@ test_recovery_replays_a_close_an_interrupted_cleanup_left_open() { pass "session start finishes a close an interrupted cleanup recorded but never landed" } +test_recovery_replays_a_discarded_scout_close_as_discarded() { + local case_dir id out + id=atomic-heal-discarded-scout-b9 + case_dir=$(make_home heal-pending-discarded-scout) + add_item "$case_dir" "$id" scout + start_item "$case_dir" "$id" + printf 'id=%s\ndata=%s\nspawn_gen=spawn-heal-discard\narg=--note\narg=discarded\n' \ + "$id" "$(home_of "$case_dir")/data" \ + > "$(home_of "$case_dir")/state/$id.backlog-close" + + out=$(run_bootstrap "$case_dir") + [ "$(row_state "$case_dir" "$id")" = "done" ] \ + || fail "session start left a recorded scout discard at $(row_state "$case_dir" "$id"): $out" + tasks-axi show "$id" --file "$(backlog_of "$case_dir")" --full \ + | grep -Fxq ' body: discarded' \ + || fail "the replayed scout discard did not record discarded as its note" + assert_absent "$(home_of "$case_dir")/state/$id.backlog-close" \ + "a replayed scout discard left its record behind" + pass "session start replays a recorded scout discard as discarded" +} + test_recovery_replays_a_gerrit_close_with_its_change_url_as_a_note() { local case_dir id out real_tasks_axi gerrit_url=https://gerrit.example.com/c/project/+/12345 id=atomic-heal-gerrit-b9 @@ -3215,6 +3262,7 @@ test_dispatch_does_not_resurrect_a_row_closed_after_preflight test_dispatch_fails_when_its_row_vanishes_after_preflight test_completion_closes_a_local_only_ship_before_reporting_success test_completion_closes_a_scout_with_its_report +test_forced_scout_discard_records_no_report test_completion_refuses_a_legacy_record_without_an_incarnation test_completion_refuses_ambiguous_incarnation_metadata test_completion_records_a_relative_report_for_relocated_data @@ -3233,6 +3281,7 @@ test_recovery_marks_an_owned_record_in_flight test_recovery_rejects_an_internal_worker_record_symlink test_recovery_ignores_a_symlinked_worker_record test_recovery_replays_a_close_an_interrupted_cleanup_left_open +test_recovery_replays_a_discarded_scout_close_as_discarded test_recovery_replays_a_gerrit_close_with_its_change_url_as_a_note test_recovery_backfills_a_recorded_link_on_an_already_done_item test_recovery_preserves_a_close_when_the_backlog_cannot_be_read diff --git a/tests/fm-bearings-board-render.test.sh b/tests/fm-bearings-board-render.test.sh index 544c4c27544..d5c24793104 100755 --- a/tests/fm-bearings-board-render.test.sh +++ b/tests/fm-bearings-board-render.test.sh @@ -94,12 +94,12 @@ require_listener_reached_poll() { # # Build the board from plus and return what the # renderer produced. -render_board() { # [charted_more] [charted_warning_more] +render_board() { # [charted_more] [charted_warning_more] [calls-json] local home=$1 underway=$2 charted=$3 more=${4:-0} warning_more=${5:-0} data="$1/payload.json" jq -n --argjson underway "$underway" --argjson charted "$charted" \ - --argjson more "$more" --argjson warning_more "$warning_more" '{ + --argjson more "$more" --argjson warning_more "$warning_more" --argjson calls "${6:-[]}" '{ schema:"fm-bearings-board.v1", home:"render-home", generated:"2026-08-26T00:00Z", - prs_live:false, captains_call:[], underway:$underway, landed:[], + prs_live:false, captains_call:$calls, underway:$underway, landed:[], charted:$charted, charted_more:$more, charted_warning_more:$warning_more}' > "$data" PATH="$home/fakebin:$PATH" FM_HOME="$home" \ FM_STATE_OVERRIDE="$home/state" FM_DATA_OVERRIDE="$home/data" \ @@ -266,6 +266,36 @@ test_charted_rows_without_a_filed_date_follow_the_dated_rows_in_payload_order() pass "charted rows with no filed date follow the dated rows in payload order" } +test_full_snapshot_question_reaches_the_rendered_decision() { + local home a b reason snapshot calls out + home=$(make_home full-decision-question) + a='A: retain the existing workflow through the complete staged verification and preserve all current integrations until each dependency has reported its result.' + b='B: defer the independent migration until the remaining review has finished, keeping the full fallback route and the final café / 東京 approval.' + reason="Which route should proceed? $a $b" + cat > "$home/data/backlog.md" </dev/null || fail "the rendered call lost its full question or final option: $out" + pass "the complete snapshot question and options reach rendered decision text" +} + +test_full_snapshot_question_reaches_the_rendered_decision test_an_underway_row_leads_with_the_task_name_and_keeps_its_run_status test_an_underway_identifier_label_is_not_replaced_by_run_status test_charted_next_reads_newest_filed_first diff --git a/tests/fm-bearings-snapshot.test.sh b/tests/fm-bearings-snapshot.test.sh index 2b462877d76..f0dd815a2f2 100755 --- a/tests/fm-bearings-snapshot.test.sh +++ b/tests/fm-bearings-snapshot.test.sh @@ -1801,6 +1801,12 @@ test_landed_accepts_only_kind_owned_delivery_artifacts() { "$TASKS_AXI_BIN" 'done' shipping-scout --report "$shipping_report" \ --file "$main_backlog" >/dev/null \ || fail "could not complete the longer-word scout" + "$TASKS_AXI_BIN" add discarded-scout "Discarded investigation" --kind scout \ + --repo firstmate --start --file "$main_backlog" >/dev/null \ + || fail "could not create the discarded scout" + "$TASKS_AXI_BIN" 'done' discarded-scout --note "discarded" \ + --file "$main_backlog" >/dev/null \ + || fail "could not record the discarded scout outcome" report_path="data/reported-scout/report.md" report_pr="https://github.com/o/r/pull/8" @@ -1842,6 +1848,11 @@ EOF # exclusion assertion fails when report ownership is not enforced. printf '%s' "$json" | jq -e '.landed | any(.id == "scout-pr-no-report") | not' >/dev/null \ || failures="${failures}reportless scout PR was listed; " + printf '%s' "$json" | jq -e '.landed | any(.id == "discarded-scout") | not' >/dev/null \ + || failures="${failures}discarded scout was listed as delivered; " + "$TASKS_AXI_BIN" show discarded-scout --full --file "$main_backlog" \ + | grep -Fxq ' body: discarded' \ + || failures="${failures}discarded scout note was not retained in backlog; " # At the base commit a ship can fall through to a report path, so this # exclusion assertion fails when artifact-kind ownership is not enforced. printf '%s' "$json" | jq -e '.landed | any(.id == "ship-reported-path") | not' >/dev/null \ @@ -3568,8 +3579,93 @@ test_a_remote_home_without_any_ledger_is_explicitly_unreadable_without_remote_co pass "a missing remote ledger stays explicitly unreadable without remote summary computation" } +test_full_captain_questions_survive_local_and_secondmate_projection() { + local home mate fakebin title reason json expanded summary toon target + home=$(make_home full-captain-questions) + mate="$TMP_ROOT/full-question-mate" + make_valid_secondmate_home questions "$mate" + append_secondmate_registry "$home" questions "$mate" + title='Choose the rollout scope for the next release while preserving the complete operator question and every explicitly recorded alternative' + reason='Which route should proceed? A: retain the existing workflow until the staged verification has finished and every downstream dependency has reported its result. B: start the separate migration after the remaining review, preserving the fallback and the complete audit record. C: defer until the final café / 東京 review.' + for target in "$home" "$mate"; do + cat > "$target/data/backlog.md" </dev/null || fail "full local/secondmate question was lost: $json" + printf '%s' "$json" | jq -e ' + [.decisions_open[].id] == ["full-call", "report-call", "questions/full-call", "questions/report-call"] + and ([.decisions_open[] | select(.id | endswith("report-call")) | .question] + == ["Review the proposal: data/proposal/report.md", "Review the proposal: data/proposal/report.md"]) + ' >/dev/null || fail "question transport changed placement or report-only text: $json" + summary=$(PATH="$fakebin:$PATH" FM_HOME="$mate" FM_SNAPSHOT_NOW=2026-07-11T18:00:00Z \ + "$ROOT/bin/fm-fleet-snapshot.sh" --secondmate-home-summary) + printf '%s' "$summary" | jq -e --arg question "$title: $reason" ' + (.decisions_open | any(.id == "full-call" and .question == $question + and (.summary | length) <= 161 and (.reason | length) <= 161)) + and (.queued | any(.id == "dated-call" and .question == $question + and (.title | length) <= 121 and (.hold_reason | length) <= 161)) + ' >/dev/null || fail "secondmate transport discarded the full question or expanded compact fields: $summary" + expanded=$(run "$home" "$fakebin" --json --all-decisions) + printf '%s' "$expanded" | jq -e --arg question "$title: $reason" ' + [.decisions_open[] | select(.id == "dated-call" or .id == "questions/dated-call")] + | length == 2 and all(.[]; .question == $question) + ' >/dev/null || fail "revealed deferred questions were lost: $expanded" + toon=$(run "$home" "$fakebin" --all-decisions) + printf '%s' "$toon" | grep -F "$title: $reason" >/dev/null \ + || fail "TOON decision presentation lost the full question: $toon" + pass "full captain questions survive local and generated secondmate projections" +} + +test_supplied_secondmate_questions_and_legacy_optional_fields() { + local home remote fakebin reason json + home=$(make_home supplied-question-parent) + make_remote_ledger_fleet "$home" 1 + remote="$TMP_ROOT/remote-ledger-home-1" + reason='A: keep all currently supported workflows and their existing contracts while the release verification runs to completion. B: wait for the independent migration and retain the rollback route through its full observation period. C: choose the final Unicode option café / 東京.' + jq --arg reason "$reason" ' + .decisions_open = [ + {id:"supplied",key:"supplied",verb:"captain-hold",source:"backlog",hold_bucket:"live", + summary:"Supplied question",reason:$reason}, + {id:"minimal",verb:"captain-hold",source:"backlog",hold_bucket:"live"}, + {id:"report",key:"report",verb:"captain-hold",source:"backlog",hold_bucket:"live", + summary:"Review report",reason:"data/supplied/report.md"}] + | .queued = [{id:"queued-only",title:"Deferred supplied question",hold_kind:"captain", + hold_bucket:"dated",hold_until:"2026-10-01",hold_reason:$reason}] + | .counts.decisions_open = 3 | .counts.queued = 1 + ' "$remote/state/home-summary.json" > "$remote/state/full-summary.json" + mv "$remote/state/full-summary.json" "$remote/state/home-summary.json" + fakebin=$(make_remote_ledger_ssh "$home") + json=$(FM_HOME="$home" FM_ROOT_OVERRIDE="$ROOT" FM_SSH_BIN="$fakebin/fake-ssh" \ + FM_TEST_LEDGER_CALL_LOG="$home/ledger-calls.log" FM_SNAPSHOT_NOW_EPOCH=1000 \ + "$BEARINGS" --json --all-decisions) + printf '%s' "$json" | jq -e --arg reason "$reason" ' + [.decisions_open[].id] == ["ledger-1/supplied", "ledger-1/minimal", "ledger-1/report", "ledger-1/queued-only"] + and (.decisions_open | any(.id == "ledger-1/supplied" and .question == ("Supplied question: " + $reason))) + and (.decisions_open | any(.id == "ledger-1/minimal" and .question == "minimal: captain decision pending")) + and (.decisions_open | any(.id == "ledger-1/report" and .question == "Review report: data/supplied/report.md")) + and (.decisions_open | any(.id == "ledger-1/queued-only" and .question == ("Deferred supplied question: " + $reason))) + ' >/dev/null || fail "supplied secondmate question or legacy fallback was lost: $json" + pass "supplied secondmate questions preserve text with missing optional fields" +} + fm_test_run \ test_task_assembly_overlaps_decision_reads_without_projection_drift \ + test_full_captain_questions_survive_local_and_secondmate_projection \ + test_supplied_secondmate_questions_and_legacy_optional_fields \ test_task_teardown_during_metadata_capture_does_not_abort_snapshot \ test_current_state_uses_captured_status_observation \ test_relaunched_task_does_not_inherit_reused_endpoint_state \ diff --git a/tests/fm-bootstrap-network-parallel.test.sh b/tests/fm-bootstrap-network-parallel.test.sh index e0b3f786995..1826ec9ec39 100755 --- a/tests/fm-bootstrap-network-parallel.test.sh +++ b/tests/fm-bootstrap-network-parallel.test.sh @@ -125,6 +125,7 @@ case "$command_name" in ;; fm-remote-inherit.sh) case "$subcommand" in + generation) printf '0\n' ;; put) printf 'unchanged: %s\n' "$item_rel" ;; esac exit 0 @@ -455,6 +456,12 @@ case "$command_name:$subcommand" in ;; fm-remote-secondmate-control.sh:state) printf 'END %s\n' "$host" >> "$log"; printf 'alive\n'; exit 0 ;; fm-remote-secondmate-control.sh:route) printf 'END %s\n' "$host" >> "$log"; printf 'backend=herdr\n'; exit 0 ;; + fm-remote-inherit.sh:generation) + sleep "${FM_FAKE_QUERY_SLEEP:-0}" + printf 'END %s %s %s\n' "$host" "$command_name" "$subcommand" >> "$log" + printf '0\n' + exit 0 + ;; fm-remote-inherit.sh:*) if [ "$item_rel" = "${FM_FAKE_ITEM_UNKNOWN:-none}" ]; then sleep 0.2 @@ -769,7 +776,7 @@ exec '$REAL_MKTEMP' "\$@" SH chmod +x "$CF_FAKEBIN/mktemp" else - started='fm-remote-inherit.sh' + started='^START host-sm fm-remote-inherit[.]sh (put|absent) ' fi set -m PATH="$CF_FAKEBIN:$BASE_PATH" \ @@ -778,17 +785,21 @@ SH FM_FAKE_SSH_LOG="$CF_LOG" FM_INHERITABLE_CONFIG='item-a item-b' \ FM_FAKE_TREEHOUSE_LEASE_HELP=1 FM_SEND_SETTLE=0 \ FM_BOOTSTRAP_VERBOSE_FACTS=1 \ - FM_FAKE_SYNC_HANG_HOST=host-sm FM_FAKE_ITEM_SLEEP=120 \ + FM_FAKE_SYNC_HANG_HOST=host-sm FM_FAKE_ITEM_SLEEP=120 FM_FAKE_QUERY_SLEEP=2 \ "$ROOT/bin/$script" >/dev/null 2>&1 & pid=$! set +m waited=0 - while ! grep -q "$started" "$CF_LOG" && [ "$waited" -lt 200 ]; do + while ! grep -Eq "$started" "$CF_LOG" && [ "$waited" -lt 200 ]; do sleep 0.05 waited=$((waited + 1)) done - grep -q "$started" "$CF_LOG" \ + grep -Eq "$started" "$CF_LOG" \ || fail "$script never started a remote mutation"$'\n'"$(cat "$CF_LOG")" + if [ "$script" = fm-config-push.sh ]; then + grep -Eq '^START host-sm fm-remote-inherit[.]sh (put|absent) ' "$CF_LOG" \ + || fail "config-push kill was triggered by the query before any payload write"$'\n'"$(cat "$CF_LOG")" + fi kill -TERM -- "-$pid" 2>/dev/null || kill -TERM "$pid" 2>/dev/null || true sleep 1 kill -KILL -- "-$pid" 2>/dev/null || true diff --git a/tests/fm-branch-supervision.test.sh b/tests/fm-branch-supervision.test.sh index 133ec9909eb..a3986d80fcc 100644 --- a/tests/fm-branch-supervision.test.sh +++ b/tests/fm-branch-supervision.test.sh @@ -31,7 +31,7 @@ test_branch_prompt_is_byte_stable_and_above_cache_floor() { out_b=$(cd / && FM_HOME="$home_b" TZ=Australia/Eucla "$ROOT/bin/fm-branch-prompt.sh") \ || fail "branch prompt generator failed for home B" sleep 1 - out_c=$("$ROOT/bin/fm-branch-prompt.sh") || fail "branch prompt generator failed on re-run" + out_c=$(FM_HOME="$home_a" "$ROOT/bin/fm-branch-prompt.sh") || fail "branch prompt generator failed on re-run" [ "$out_a" = "$out_b" ] || fail "branch prompt differs across homes/cwd/timezone: prefix stability broken" [ "$out_a" = "$out_c" ] || fail "branch prompt differs across runs at different times: prefix stability broken" @@ -72,6 +72,50 @@ test_branch_prompt_is_byte_stable_and_above_cache_floor() { pass "branch prompt is byte-stable across homes, cwd, timezone, and time, above the cache floor" } +# A home adds its own rules through config/branch-prompt-include.md: absent or +# blank leaves the prompt byte-identical, present text lands verbatim as the +# last section, and an unusable path refuses with no prompt on stdout. +test_branch_prompt_home_include_is_appended_last() { + local home config base out rc last_heading + home="$TMP_ROOT/prompt-include-home" + config="$home/config" + mkdir -p "$config" + base=$(FM_HOME="$TMP_ROOT/prompt-include-none" "$ROOT/bin/fm-branch-prompt.sh") || fail "branch prompt failed without a home" + + out=$(FM_HOME="$home" "$ROOT/bin/fm-branch-prompt.sh") || fail "branch prompt failed without an include" + [ "$out" = "$base" ] || fail "an absent include changed the branch prompt" + printf ' \n\n' > "$config/branch-prompt-include.md" + out=$(FM_HOME="$home" "$ROOT/bin/fm-branch-prompt.sh") || fail "branch prompt failed with a blank include" + [ "$out" = "$base" ] || fail "a blank include changed the branch prompt" + + # shellcheck disable=SC2016 # The include is literal text and must never expand. + printf '%s\n' 'Relaunch workers with `house-tool $(id)`.' > "$config/branch-prompt-include.md" + out=$(FM_HOME="$home" "$ROOT/bin/fm-branch-prompt.sh") || fail "branch prompt failed with an include" + case "$out" in + "$base"*) ;; + *) fail "the include did not leave the tracked prompt as an unchanged prefix" ;; + esac + # shellcheck disable=SC2016 # Literal include text. + assert_contains "$out" 'Relaunch workers with `house-tool $(id)`.' "branch prompt did not carry the include verbatim" + assert_contains "$out" "every other section of this prompt takes precedence" "include section lost its precedence line" + last_heading=$(printf '%s\n' "$out" | grep '^# ' | tail -n 1) + [ "$last_heading" = '# Home prompt additions' ] || fail "include was not the last section (got: $last_heading)" + [ "$(FM_HOME="$home" "$ROOT/bin/fm-branch-prompt.sh")" = "$out" ] || fail "branch prompt with an include is not byte-stable across runs" + + out=$(FM_HOME="$home" FM_CONFIG_OVERRIDE="$TMP_ROOT/prompt-include-empty-config" "$ROOT/bin/fm-branch-prompt.sh") \ + || fail "branch prompt failed under FM_CONFIG_OVERRIDE" + [ "$out" = "$base" ] || fail "FM_CONFIG_OVERRIDE did not select the config directory" + + rm -f "$config/branch-prompt-include.md" + mkdir "$config/branch-prompt-include.md" + out=$(FM_HOME="$home" "$ROOT/bin/fm-branch-prompt.sh" 2>/dev/null); rc=$? + expect_code 1 "$rc" "an unusable include path must stop the branch prompt" + [ -z "$out" ] || fail "an unusable include still emitted a partial prompt" + out=$(FM_HOME="$home" "$ROOT/bin/fm-branch-prompt.sh" 2>&1 >/dev/null) + assert_contains "$out" "branch-prompt-include.md must be a readable regular file" "unusable include refusal did not name the file" + pass "fm-branch-prompt.sh: the home prompt include lands last, verbatim, and refuses an unusable path" +} + test_branch_prompt_routes_completed_scouts_through_guarded_cleanup() { local prompt skill prompt=$(bash "$ROOT/bin/fm-branch-prompt.sh") || fail "branch prompt generator failed" @@ -1621,6 +1665,7 @@ WRAPPER } test_branch_prompt_is_byte_stable_and_above_cache_floor +test_branch_prompt_home_include_is_appended_last test_branch_prompt_routes_completed_scouts_through_guarded_cleanup test_outcome_store_is_append_only_with_cursor_reads test_outcome_append_keeps_a_bounded_display_tail diff --git a/tests/fm-busy-state.test.sh b/tests/fm-busy-state.test.sh index 03be8133cd8..e718480d20e 100755 --- a/tests/fm-busy-state.test.sh +++ b/tests/fm-busy-state.test.sh @@ -206,6 +206,112 @@ SH pass "the writer breaks a stale lock instead of dying on GNU stat output" } +# A writer killed inside its critical section leaves a FRESH orphan lock behind; +# teardown kills a worker whose exit hook may be mid-write and then retires the +# busy state moments later. The writer must outwait the stale bound and break +# that lock on the first retire, rather than give up while the lock is still +# young and leave cleanup to fail until some later retry. +test_fresh_orphan_lock_is_broken_on_the_first_retire() { + local state gen out status started elapsed + state=$(new_state_dir fresh-orphan) + gen=$("$EV" arm "$state" t1) + mkdir "$state/t1.busy-state.lock" + started=$SECONDS + out=$(env -u FM_BUSY_LOCK_STALE_SECS "$EV" retire "$state" t1 --gen "$gen" 2>&1) && status=0 || status=$? + elapsed=$((SECONDS - started)) + [ "$status" = 0 ] || fail "the first retire over a fresh orphan lock was refused: $out" + [ ! -e "$state/t1.busy-state" ] || fail "retire left the record behind" + [ ! -e "$state/t1.busy-gen" ] || fail "retire left the gen sidecar behind" + [ ! -e "$state/t1.busy-state.lock" ] || fail "retire left the orphan lock behind" + # Non-vacuity: with the default five-second bound a fresh lock cannot read as + # stale for about four seconds, so a quick success means something other than + # the stale window removed it. + [ "$elapsed" -ge 3 ] \ + || fail "retire succeeded after ${elapsed}s, before a fresh lock could reach the stale bound" + [ "$elapsed" -le 8 ] || fail "retire took ${elapsed}s to break a fresh orphan lock" + pass "the first retire outwaits the stale bound and breaks a fresh orphan lock" +} + +# The longer wait must not turn into breaking a live writer's lock: a holder +# that releases inside the stale bound is waited for, never broken, and the +# waiter then applies its change. +test_live_writer_lock_is_waited_for_not_broken() { + local state gen retire_pid + state=$(new_state_dir live-writer) + gen=$("$EV" arm "$state" t1) + mkdir "$state/t1.busy-state.lock" + : > "$state/t1.busy-state.lock/holder" + env -u FM_BUSY_LOCK_STALE_SECS "$EV" retire "$state" t1 --gen "$gen" >/dev/null 2>&1 & + retire_pid=$! + sleep 1 + kill -0 "$retire_pid" 2>/dev/null || fail "retire did not wait for a live writer's lock" + [ -e "$state/t1.busy-state.lock/holder" ] || fail "retire broke a live writer's lock" + [ -e "$state/t1.busy-state" ] || fail "retire bypassed a live writer's lock" + rm -f "$state/t1.busy-state.lock/holder" + rmdir "$state/t1.busy-state.lock" + wait "$retire_pid" || fail "retire failed after the live writer released its lock" + [ ! -e "$state/t1.busy-state" ] || fail "retire left the record behind" + pass "a live writer's lock is waited for and never broken" +} + +# Digit-only stale settings retain decimal meaning, including 08. +test_leading_zero_stale_setting_waits_for_live_writer() { + local state gen retire_pid + state=$(new_state_dir leading-zero-stale) + gen=$("$EV" arm "$state" t1) + mkdir "$state/t1.busy-state.lock" + : > "$state/t1.busy-state.lock/holder" + env FM_BUSY_LOCK_STALE_SECS=08 "$EV" retire "$state" t1 --gen "$gen" >/dev/null 2>&1 & + retire_pid=$! + sleep 1 + kill -0 "$retire_pid" 2>/dev/null || fail "retire did not wait for a live writer's lock" + [ -e "$state/t1.busy-state.lock/holder" ] || fail "retire broke a live writer's lock" + [ -e "$state/t1.busy-state" ] || fail "retire bypassed a live writer's lock" + rm -f "$state/t1.busy-state.lock/holder" + rmdir "$state/t1.busy-state.lock" + wait "$retire_pid" || fail "retire failed after the live writer released its lock" + [ ! -e "$state/t1.busy-state" ] || fail "retire left the record behind" + pass "a decimal leading-zero stale setting waits for a live writer and completes retirement" +} + +# The wait stays bounded: a lock that never ages past the stale bound - live +# writers re-taking it back to back - still ends in a lock-timeout refusal that +# leaves the record untouched. +test_lock_that_stays_young_is_refused_within_a_bound() { + local state gen stop out status started elapsed toucher i + state=$(new_state_dir young-lock) + gen=$("$EV" arm "$state" t1) + stop="$state/../toucher-running" + mkdir "$state/t1.busy-state.lock" + : > "$stop" + ( + i=0 + while [ -e "$stop" ] && [ "$i" -lt 100 ]; do + touch "$state/t1.busy-state.lock" 2>/dev/null || true + sleep 0.2 + i=$((i + 1)) + done + ) & + toucher=$! + started=$SECONDS + out=$(FM_BUSY_LOCK_STALE_SECS=2 "$EV" retire "$state" t1 --gen "$gen" 2>&1) && status=0 || status=$? + elapsed=$((SECONDS - started)) + rm -f "$stop" + wait "$toucher" 2>/dev/null || true + [ "$status" = 1 ] || fail "retire over a lock that never went stale returned $status: $out" + case "$out" in + *'busy-state lock timeout for t1'*) : ;; + *) fail "the bounded refusal did not name the lock timeout: $out" ;; + esac + [ -e "$state/t1.busy-state" ] || fail "a refused retire removed the record" + [ "$elapsed" -ge 2 ] || fail "retire gave up after ${elapsed}s, inside the stale bound" + [ "$elapsed" -le 6 ] || fail "retire waited ${elapsed}s, past its bound" + rmdir "$state/t1.busy-state.lock" + FM_BUSY_LOCK_STALE_SECS=2 "$EV" retire "$state" t1 --gen "$gen" \ + || fail "retire failed once the lock was released" + pass "a lock that stays younger than the stale bound is refused within a bound" +} + test_retire_missing_sidecar_is_idempotent() { local state gen state=$(new_state_dir retire-missing) @@ -798,6 +904,10 @@ test_apply_unarmed_refused test_retire_serializes_and_rejects_stale_gen test_retire_missing_sidecar_is_idempotent test_stale_lock_broken_under_gnu_stat +test_fresh_orphan_lock_is_broken_on_the_first_retire +test_live_writer_lock_is_waited_for_not_broken +test_leading_zero_stale_setting_waits_for_live_writer +test_lock_that_stays_young_is_refused_within_a_bound test_stale_gen_event_rejected test_stale_gen_record_unknown test_missing_record_unknown_not_idle diff --git a/tests/fm-ci-workflow.test.sh b/tests/fm-ci-workflow.test.sh index 930baed837e..1f0251cf888 100755 --- a/tests/fm-ci-workflow.test.sh +++ b/tests/fm-ci-workflow.test.sh @@ -725,7 +725,7 @@ RUBY } test_house_workflow_adds_a_focused_check_set() { - ruby -ryaml - "$HOUSE_WORKFLOW" "$CI_WORKFLOW" "$ROOT/.github/workflows/no-mistakes-required.yml" <<'RUBY' || fail "house workflow contract" + ruby -ryaml -rshellwords - "$HOUSE_WORKFLOW" "$CI_WORKFLOW" "$ROOT/.github/workflows/no-mistakes-required.yml" <<'RUBY' || fail "house workflow contract" def events(path) document = YAML.load_file(path) document.fetch("on") { document.fetch(true) } @@ -742,6 +742,10 @@ job = jobs.fetch("house") raise "house check must use the standard Linux runner" unless job.fetch("runs-on") == "ubuntu-latest" raise "house check must use the normal 30-minute tripwire" unless job.fetch("timeout-minutes") == 30 steps = job.fetch("steps") +lint_steps = steps.select { |step| Shellwords.split(step["run"].to_s).first == "bin/fm-lint.sh" } +raise "house check must have one shell lint step" unless lint_steps.length == 1 +lint_env = house.fetch("env", {}).merge(job.fetch("env", {})).merge(lint_steps.first.fetch("env", {})) +raise "house lint must analyze one root at a time, like canonical CI" unless lint_env.fetch("FM_LINT_JOBS", nil) == "1" checkout = steps.find { |step| step["uses"] == "actions/checkout@v6" } raise "house workflow must fetch history for base comparison" unless checkout&.dig("with", "fetch-depth") == 0 commands = steps.map { |step| step["run"].to_s }.join("\n") diff --git a/tests/fm-claude-account-quota.test.sh b/tests/fm-claude-account-quota.test.sh new file mode 100644 index 00000000000..aed186448b0 --- /dev/null +++ b/tests/fm-claude-account-quota.test.sh @@ -0,0 +1,194 @@ +#!/usr/bin/env bash +# Behavior tests for bin/fm-claude-account-quota.sh: each account declared in +# config/claude-accounts is measured under its own CLAUDE_CONFIG_DIR and its +# quota facts and uncertainty are printed, never a choice. +# +# A fake quota-axi answers from /quota.json, the way the real +# --profile-only read answers from that root's own credential file, and logs +# the root and arguments of every read. +set -u + +# shellcheck source=tests/lib.sh +. "$(dirname "${BASH_SOURCE[0]}")/lib.sh" + +TMP_ROOT=$(fm_test_tmproot fm-claude-account-quota) +TOOL="$ROOT/bin/fm-claude-account-quota.sh" + +# new_case -> sets CASE HOME_DIR FAKEBIN +new_case() { + CASE="$TMP_ROOT/$1" + HOME_DIR="$CASE/home" + FAKEBIN="$CASE/fakebin" + mkdir -p "$HOME_DIR/config" "$FAKEBIN" "$CASE/user-home" + cat > "$FAKEBIN/quota-axi" <> '$CASE/reads' +[ -f "\$CLAUDE_CONFIG_DIR/quota.json" ] || exit 1 +cat "\$CLAUDE_CONFIG_DIR/quota.json" +[ ! -f "\$CLAUDE_CONFIG_DIR/exit-1" ] || exit 1 +SH + chmod +x "$FAKEBIN/quota-axi" +} + +# account [runway] [model-scope-spendPriority] [usableRunwaySeconds] +# A Claude root whose profile-only read reports these numbers. +account() { + local dir=$1 pct=$2 sp=$3 runway=${4:-through_reset} model_sp=${5:-} seconds=${6:-} + mkdir -p "$dir" + jq -n --argjson pct "$pct" --argjson sp "$sp" --arg runway "$runway" --arg model_sp "$model_sp" --arg seconds "$seconds" '{ + schemaVersion: 5, + providers: [{ + provider: "claude", + quotaSemantics: { + status: "known", + effectiveAvailability: ([ + {scope: "all_models", status: "known", effectivePercentRemaining: $pct, + runway: ({status: $runway} + (if $seconds == "" then {} else {usableRunwaySeconds: ($seconds | tonumber), projectionConfidence: "established"} end)), + selection: {spendPriority: $sp}} + ] + (if $model_sp == "" then [] else [ + {scope: "model:opus", status: "known", effectivePercentRemaining: 50, + runway: {status: "through_reset"}, selection: {spendPriority: ($model_sp | tonumber)}} + ] end)) + } + }] + }' > "$dir/quota.json" +} + +# rate_limited_account : a root whose read the vendor rate-limits, which +# quota-axi reports with exit 1 and a snapshot naming that state. +rate_limited_account() { + mkdir -p "$1" + jq -n '{ + schemaVersion: 5, + providers: [{ + provider: "claude", + state: {status: "rate_limited", error: "Claude quota endpoint rate limited"}, + quotaSemantics: {status: "unknown", effectiveAvailability: []} + }] + }' > "$1/quota.json" + : > "$1/exit-1" +} + +run_tool() { + env -i HOME="$CASE/user-home" PATH="$FAKEBIN:$PATH" FM_HOME="$HOME_DIR" \ + QUOTA_AXI_SNAPSHOT="$CASE/shared-fixture.json" "$TOOL" "$@" 2>&1 +} + +assert_no_choice() { + assert_not_contains "$1" "--account" "the tool must not print a chosen account" + assert_not_contains "$1" "status: " "the tool must not print a verdict status" + assert_not_contains "$1" "eligible" "the tool must not judge eligibility" +} + +test_two_accounts_print_their_own_rows() { + local out rc + new_case two + account "$CASE/a" 70 0.2 + account "$CASE/b" 60 0.5 + printf '%s\n%s\n' "$CASE/a" "$CASE/b" > "$HOME_DIR/config/claude-accounts" + out=$(run_tool); rc=$? + expect_code 0 "$rc" "measuring two declared accounts should succeed: $out" + assert_contains "$out" "row: $CASE/a root=$CASE/a scope=all_models status=known remaining=70% spendPriority=0.2 runway=through_reset" "account a's own row should be shown" + assert_contains "$out" "row: $CASE/b root=$CASE/b scope=all_models status=known remaining=60% spendPriority=0.5 runway=through_reset" "account b's own row should be shown" + assert_no_choice "$out" + assert_grep "$CASE/a unset --provider claude --profile-only --json" "$CASE/reads" "account a should be read in isolation under its own root" + assert_grep "$CASE/b unset --provider claude --profile-only --json" "$CASE/reads" "account b should be read in isolation under its own root" + pass "each declared account prints its own quota row, read under its own root" +} + +test_the_model_scope_is_printed() { + local out + new_case model + account "$CASE/a" 70 0.2 through_reset 0.9 + printf '%s\n' "$CASE/a" > "$HOME_DIR/config/claude-accounts" + out=$(run_tool --model opus) + assert_contains "$out" "row: $CASE/a root=$CASE/a scope=all_models status=known remaining=70% spendPriority=0.2" "the all_models scope applies to the model" + assert_contains "$out" "row: $CASE/a root=$CASE/a scope=model:opus status=known remaining=50% spendPriority=0.9" "the model scope should be printed" + out=$(run_tool) + assert_not_contains "$out" "scope=model:opus" "a model scope does not apply without --model" + assert_no_choice "$out" + pass "a model-specific scope is printed only for that model" +} + +test_exhausted_and_unreadable_accounts_print_facts() { + local out + new_case gates + account "$CASE/spent" 0 0.9 + account "$CASE/dry" 40 0.8 exhausted_now + mkdir -p "$CASE/unread" + printf '%s\n' "$CASE/spent" "$CASE/dry" "$CASE/unread" > "$HOME_DIR/config/claude-accounts" + out=$(run_tool) + assert_contains "$out" "row: $CASE/spent root=$CASE/spent scope=all_models status=known remaining=0% spendPriority=0.9 runway=through_reset" "a 0% account prints its facts" + assert_contains "$out" "row: $CASE/dry root=$CASE/dry scope=all_models status=known remaining=40% spendPriority=0.8 runway=exhausted_now" "an exhausted account prints its runway" + assert_contains "$out" "row: $CASE/unread root=$CASE/unread uncertainty: quota-axi could not read this account" "an unreadable account is disclosed uncertainty" + assert_no_choice "$out" + pass "exhausted, 0%, and unreadable accounts print facts and uncertainty without a verdict" +} + +test_projected_runway_prints_its_seconds() { + local out + new_case runway + account "$CASE/long" 80 0.4 projected_exhaustion "" 241920 + account "$CASE/short" 30 0.9 projected_exhaustion "" 2700 + account "$CASE/unknown" 50 0.5 projected_exhaustion + printf '%s\n' "$CASE/long" "$CASE/short" "$CASE/unknown" > "$HOME_DIR/config/claude-accounts" + out=$(run_tool) + assert_contains "$out" "row: $CASE/long root=$CASE/long scope=all_models status=known remaining=80% spendPriority=0.4 runway=projected_exhaustion usableRunwaySeconds=241920 confidence=established" \ + "a projected exhaustion should carry its runway seconds" + assert_contains "$out" "row: $CASE/short root=$CASE/short scope=all_models status=known remaining=30% spendPriority=0.9 runway=projected_exhaustion usableRunwaySeconds=2700 confidence=established" \ + "each account should carry its own runway seconds" + assert_contains "$out" "row: $CASE/unknown root=$CASE/unknown scope=all_models status=known remaining=50% spendPriority=0.5 runway=projected_exhaustion usableRunwaySeconds=-" \ + "a projection without seconds should show that it is missing" + assert_not_contains "$out" "runway=through_reset usableRunwaySeconds" "through_reset carries no runway seconds" + assert_no_choice "$out" + pass "a projected exhaustion prints each account's own runway seconds and confidence without a verdict" +} + +test_a_rate_limited_account_names_the_vendor_reason() { + local out + new_case rate-limited + rate_limited_account "$CASE/limited" + printf '%s\n' "$CASE/limited" > "$HOME_DIR/config/claude-accounts" + out=$(run_tool) + assert_contains "$out" "row: $CASE/limited root=$CASE/limited uncertainty: quota unknown (rate_limited: Claude quota endpoint rate limited)" \ + "a rate-limited read should keep quota-axi's own reason, not report an unreadable account" + assert_not_contains "$out" "could not read" "a rate-limited snapshot is not an unreadable root" + assert_no_choice "$out" + pass "a rate-limited account names the vendor's reason" +} + +test_ordinary_is_measured_from_the_default_root() { + local out + new_case ordinary + account "$CASE/user-home/.claude" 50 0.3 + printf 'ordinary\n' > "$HOME_DIR/config/claude-accounts" + out=$(run_tool) + assert_contains "$out" "row: ordinary root=$CASE/user-home/.claude scope=all_models status=known remaining=50% spendPriority=0.3" "the ordinary account should be measurable" + assert_no_choice "$out" + assert_grep "$CASE/user-home/.claude unset" "$CASE/reads" "ordinary should be read from ~/.claude" + pass "ordinary is measured from the default ~/.claude root" +} + +test_absent_or_malformed_lists_refuse() { + local out rc + new_case refuse + out=$(run_tool); rc=$? + expect_code 2 "$rc" "an absent list must refuse" + assert_contains "$out" "declares no config/claude-accounts" "the refusal should name the missing list" + printf 'relative\n' > "$HOME_DIR/config/claude-accounts" + out=$(run_tool); rc=$? + expect_code 2 "$rc" "a malformed list must refuse" + assert_contains "$out" "config/claude-accounts must hold one account per line" "the refusal should name the format" + assert_absent "$CASE/reads" "a refused list must not read any quota" + pass "an absent or malformed list refuses before any quota read" +} + +test_two_accounts_print_their_own_rows +test_the_model_scope_is_printed +test_exhausted_and_unreadable_accounts_print_facts +test_projected_runway_prints_its_seconds +test_a_rate_limited_account_names_the_vendor_reason +test_ordinary_is_measured_from_the_default_root +test_absent_or_malformed_lists_refuse + +echo "# all fm-claude-account-quota tests passed" diff --git a/tests/fm-claude-stop-autoarm.test.sh b/tests/fm-claude-stop-autoarm.test.sh index 913597716ca..0f66cd0d81c 100755 --- a/tests/fm-claude-stop-autoarm.test.sh +++ b/tests/fm-claude-stop-autoarm.test.sh @@ -35,6 +35,7 @@ install_autoarm_scripts() { cp "$ROOT/bin/fm-cursor-lib.sh" "$dir/bin/fm-cursor-lib.sh" cp "$ROOT/bin/fm-hook-host-lib.sh" "$dir/bin/fm-hook-host-lib.sh" cp "$ROOT/bin/fm-lock.sh" "$dir/bin/fm-lock.sh" + cp "$ROOT/bin/fm-lease-lib.sh" "$dir/bin/fm-lease-lib.sh" cp "$ROOT/bin/fm-afk-contract.sh" "$dir/bin/fm-afk-contract.sh" cp "$ROOT/bin/fm-classify-lib.sh" "$dir/bin/fm-classify-lib.sh" cp "$ROOT/bin/fm-timeout-lib.sh" "$dir/bin/fm-timeout-lib.sh" @@ -251,12 +252,22 @@ epoch_outcome() { # caller can `wait` on it for the hook's exit status). RUN_AUTOARM_BG_PID= run_autoarm_bg() { - local dir=$1 out=$2 + local dir=$1 out=$2 session_id=${3:-} printf '%s\n' '{"session_id":"sess-autoarm","stop_hook_active":false}' \ - | FM_HOME="$dir" "$FAKE_CLAUDE" -c ' + | FM_HOME="$dir" python3 -c ' +import os, signal, sys +# Noninteractive bash backgrounds inherit ignored SIGINT. Restore the host +# signal disposition before exec so the fixture can exercise the INT trap too. +signal.signal(signal.SIGINT, signal.SIG_DFL) +os.execv(sys.argv[1], sys.argv[1:]) +' "$FAKE_CLAUDE" -c ' printf "%s\n" "$$" > "$FM_HOME/state/.lock" + if [ -n "$1" ]; then + export CLAUDE_CODE_SESSION_ID=$1 CLAUDE_PID=$$ + printf "%s\n" "$1" > "$FM_HOME/state/.lock-session" + fi "$FM_HOME/bin/fm-claude-stop-autoarm.sh" - ' > "$out" 2>&1 & + ' _ "$session_id" > "$out" 2>&1 & RUN_AUTOARM_BG_PID=$! } @@ -857,6 +868,211 @@ test_term_mid_arm_commits_failure_and_rewakes() { pass "auto-arm: TERM mid-arm commits a durable failure and exits 2 for rewake" } +# Session teardown can signal the hook before its harness dies. The replacement +# must retire that notice rather than spend its own first real failure silently. +test_replacement_retires_interrupted_session_episode() { + local signal dir out hook_pid i status replacement + for signal in HUP TERM INT; do + dir=$(make_primary_dir "$TMP_ROOT/replaced-$signal") + : > "$dir/state/task.meta" + write_arm_fixture "$dir" blocking-actionable + out="$dir/state/interrupted.out" + run_autoarm_bg "$dir" "$out" sess-producing + hook_pid= + i=0 + while [ "$i" -lt 100 ]; do + hook_pid=$(epoch_field "$dir" owner_pid) + [ -n "$hook_pid" ] && [ -e "$dir/state/arm-ran" ] && break + sleep 0.02 + i=$((i + 1)) + done + [ -n "$hook_pid" ] && [ -e "$dir/state/arm-ran" ] || fail "$signal hook did not park" + kill -"$signal" "$hook_pid" || fail "could not signal $signal hook" + status=0 + wait "$RUN_AUTOARM_BG_PID" || status=$? + expect_code 2 "$status" "$signal in a live session must retain timeout recovery" + assert_contains "$(cat "$out")" 'auto-arm INTERRUPTED' "$signal omitted its current-session notice" + assert_present "$dir/state/.claude-autoarm-failure-notified" "$signal lost its durable notice" + assert_contains "$(cat "$dir/state/.claude-autoarm-failure-notified")" 'session_id=sess-producing' \ + "$signal did not bind its notice to the recorded session id" + + write_arm_fixture "$dir" actionable + replacement=$(printf '%s\n' '{"session_id":"replacement"}' \ + | FM_HOME="$dir" "$FAKE_CLAUDE" -c ' + printf "%s\n" "$$" > "$FM_HOME/state/.lock" + "$FM_HOME/bin/fm-claude-stop-autoarm.sh"; rc=$? + . "$FM_HOME/bin/fm-wake-lib.sh" + fm_autoarm_midturn_healthy "$FM_HOME/state" || exit 11 + exit "$rc" + ' 2>&1); status=$? + expect_code 2 "$status" "$signal replacement inherited a phantom mid-turn failure" + assert_absent "$dir/state/.claude-autoarm-failure-notified" "$signal replacement retained the old notice" + [ "$(epoch_outcome "$dir")" = rewake ] || fail "$signal replacement retained the old failure" + + # Its first real failure must still notify and retain bounded retries. + write_arm_fixture "$dir" failed + replacement=$(run_autoarm "$dir" 2>/dev/null); status=$? + expect_code 2 "$status" "$signal replacement must report its real failure" + assert_contains "$replacement" 'automatic supervision mechanism is broken' \ + "$signal replacement inherited the previous session's consumed notice" + [ "$(epoch_outcome "$dir")" = failed ] || fail "$signal replacement suppressed its first failure" + [ "$(wc -l < "$dir/state/arm-ran" | tr -d ' ')" -eq 4 ] || fail "$signal replacement lost bounded retries" + done + pass "auto-arm: HUP, TERM and INT episodes do not consume the replacement session's notice" +} + +# An interrupted failure belongs to the session, including when fm-lock moves +# its live anchor to the current model process within that same session. +test_same_session_lock_move_preserves_interrupted_episode() { + local dir status=0 + dir=$(make_primary_dir "$TMP_ROOT/interrupted-lock-move") + FM_HOME="$dir" "$FAKE_CLAUDE" -c ' + export CLAUDE_CODE_SESSION_ID=sess-moving CLAUDE_PID=$$ + . "$FM_HOME/bin/fm-wake-lib.sh" + . "$FM_HOME/bin/fm-session-lock-lib.sh" + "$FAKE_CLAUDE" -c "while :; do sleep 1; done" & + holder=$! + trap '\''kill "$holder" 2>/dev/null; wait "$holder" 2>/dev/null'\'' EXIT + printf "%s\n" "$holder" > "$FM_HOME/state/.lock" + printf "%s\n" "$CLAUDE_CODE_SESSION_ID" > "$FM_HOME/state/.lock-session" + fm_autoarm_claim_next "$FM_HOME/state" || exit 90 + identity=$(fm_session_pid_identity "$holder") || exit 91 + fm_autoarm_write_owned "$FM_HOME/state" "$FM_AUTOARM_MY_GEN" failed \ + "$FM_HOME/state/.claude-autoarm-failure-notified" "" "" "$identity" "$CLAUDE_CODE_SESSION_ID" || exit 92 + printf "alarmed\n" > "$FM_HOME/state/.claude-autoarm-failure-alarmed" + printf "2\n" > "$FM_HOME/state/.turnend-claude-blocks" + "$FAKE_CLAUDE" -c '\'' + export CLAUDE_PID=$$ + old_pid=$(cat "$FM_HOME/state/.lock") + "$FM_HOME/bin/fm-lock.sh" || exit 93 + [ "$(cat "$FM_HOME/state/.lock")" = "$$" ] || exit 94 + [ "$old_pid" != "$$" ] || exit 95 + . "$FM_HOME/bin/fm-wake-lib.sh" + fm_autoarm_claim_next "$FM_HOME/state" || exit 96 + [ -e "$FM_HOME/state/.claude-autoarm-failure-notified" ] || exit 11 + [ "$(cat "$FM_HOME/state/.claude-autoarm-failure-alarmed")" = alarmed ] || exit 12 + [ "$(cat "$FM_HOME/state/.turnend-claude-blocks")" = 2 ] || exit 13 + '\'' + ' > "$dir/state/probe.out" 2>&1 || status=$? + expect_code 0 "$status" "ci-1: verified same-session lock move must preserve the interrupted notice, alarm and budget" + pass "auto-arm: verified same-session lock moves preserve the interrupted episode" +} + +# Model the Windows identity collision: the same process identity answers for +# a replacement, but the lock sidecar and trusted session id name a new session. +test_reused_identity_with_new_session_retires_interrupted_episode() { + local dir status=0 + dir=$(make_primary_dir "$TMP_ROOT/interrupted-reused-identity") + FM_HOME="$dir" "$FAKE_CLAUDE" -c ' + export CLAUDE_CODE_SESSION_ID=sess-old CLAUDE_PID=$$ + . "$FM_HOME/bin/fm-wake-lib.sh" + . "$FM_HOME/bin/fm-session-lock-lib.sh" + printf "%s\n" "$$" > "$FM_HOME/state/.lock" + printf "%s\n" "$CLAUDE_CODE_SESSION_ID" > "$FM_HOME/state/.lock-session" + fm_autoarm_claim_next "$FM_HOME/state" || exit 90 + identity=$(fm_session_pid_identity "$$") || exit 91 + fm_autoarm_write_owned "$FM_HOME/state" "$FM_AUTOARM_MY_GEN" failed \ + "$FM_HOME/state/.claude-autoarm-failure-notified" "" "" "$identity" "$CLAUDE_CODE_SESSION_ID" || exit 92 + printf "alarmed\n" > "$FM_HOME/state/.claude-autoarm-failure-alarmed" + printf "2\n" > "$FM_HOME/state/.turnend-claude-blocks" + export CLAUDE_CODE_SESSION_ID=sess-replacement + printf "%s\n" "$CLAUDE_CODE_SESSION_ID" > "$FM_HOME/state/.lock-session" + [ "$identity" = "$(fm_session_pid_identity "$$")" ] || exit 93 + fm_autoarm_claim_next "$FM_HOME/state" || exit 94 + [ ! -e "$FM_HOME/state/.claude-autoarm-failure-notified" ] || exit 11 + [ ! -e "$FM_HOME/state/.claude-autoarm-failure-alarmed" ] || exit 12 + [ ! -e "$FM_HOME/state/.turnend-claude-blocks" ] || exit 13 + ' > "$dir/state/probe.out" 2>&1 || status=$? + expect_code 0 "$status" "ci-2: replacement session with an identical process identity must retire the old interrupted episode" + pass "auto-arm: a new session id retires an interrupted episode despite process identity reuse" +} + +test_missing_lock_interruption_is_silent() { + local signal dir out hook_pid i status + for signal in HUP TERM INT; do + dir=$(make_primary_dir "$TMP_ROOT/ended-$signal") + : > "$dir/state/task.meta" + write_arm_fixture "$dir" blocking-actionable + out="$dir/state/interrupted.out" + run_autoarm_bg "$dir" "$out" + hook_pid= + for i in $(seq 1 100); do + hook_pid=$(epoch_field "$dir" owner_pid) + [ -n "$hook_pid" ] && [ -e "$dir/state/arm-ran" ] && break + sleep 0.02 + done + [ -n "$hook_pid" ] && [ -e "$dir/state/arm-ran" ] || fail "$signal hook did not park before lock loss" + mv "$dir/state/.lock" "$dir/state/retired-lock" + kill -"$signal" "$hook_pid" || fail "could not signal ended $signal hook" + status=0 + wait "$RUN_AUTOARM_BG_PID" || status=$? + expect_code 0 "$status" "$signal after lock loss must exit silently" + [ ! -s "$out" ] || fail "$signal after lock loss produced output: $(cat "$out")" + [ "$(epoch_outcome "$dir")" = session-ended ] || fail "$signal after lock loss left a nonterminal session" + assert_absent "$dir/state/.claude-autoarm-failure-notified" "$signal after lock loss consumed a notice" + assert_absent "$dir/state/.claude-autoarm-failure-alarmed" "$signal after lock loss created an alarm" + done + pass "auto-arm: HUP, TERM and INT after lock loss close silently as session-ended" +} + +# Exercise both sides of the session proof in the real signal handler and its +# owned notice commit, while the process identities remain live and inspectable. +test_signal_session_identity_proof() { + local mode signal dir out hook_pid i status + for mode in reanchor replacement-id; do + for signal in HUP TERM INT; do + dir=$(make_primary_dir "$TMP_ROOT/signal-$mode-$signal") + : > "$dir/state/task.meta" + write_arm_fixture "$dir" blocking-actionable + out="$dir/state/interrupted.out" + run_autoarm_bg "$dir" "$out" sess-signal + hook_pid= + for i in $(seq 1 100); do + hook_pid=$(epoch_field "$dir" owner_pid) + [ -n "$hook_pid" ] && [ -e "$dir/state/arm-ran" ] && break + sleep 0.02 + done + [ -n "$hook_pid" ] && [ -e "$dir/state/arm-ran" ] || fail "$mode $signal hook did not park" + if [ "$mode" = reanchor ]; then + FM_HOME="$dir" "$FAKE_CLAUDE" -c ' + export CLAUDE_CODE_SESSION_ID=sess-signal CLAUDE_PID=$$ + old=$(cat "$FM_HOME/state/.lock") + "$FM_HOME/bin/fm-lock.sh" || exit 90 + [ "$(cat "$FM_HOME/state/.lock")" = "$$" ] && [ "$old" != "$$" ] || exit 91 + kill -"$2" "$1" || exit 92 + for _ in $(seq 1 500); do + kill -0 "$1" 2>/dev/null || exit 0 + sleep 0.02 + done + exit 93 + ' _ "$hook_pid" "$signal" > "$dir/state/reanchor.out" 2>&1 \ + || fail "$signal could not hold a verified same-session re-anchor" + else + # Keep the lock pid and its identity unchanged, varying only the + # independently recorded session id, as on Windows process-number reuse. + printf 'sess-replacement\n' > "$dir/state/.lock-session" + kill -"$signal" "$hook_pid" || fail "could not signal replacement-id $signal hook" + fi + status=0 + wait "$RUN_AUTOARM_BG_PID" || status=$? + if [ "$mode" = reanchor ]; then + expect_code 2 "$status" "$signal after verified same-session re-anchor must retain timeout recovery" + assert_contains "$(cat "$out")" 'auto-arm INTERRUPTED' "$signal re-anchor lost its failure notice" + assert_contains "$(cat "$dir/state/.claude-autoarm-failure-notified")" 'session_id=sess-signal' \ + "$signal re-anchor lost its recorded session id" + [ "$(epoch_outcome "$dir")" = failed ] || fail "$signal re-anchor did not commit its failure" + else + expect_code 0 "$status" "$signal after session-id replacement must close silently" + [ ! -s "$out" ] || fail "$signal after session-id replacement produced output" + [ "$(epoch_outcome "$dir")" = session-ended ] || fail "$signal session-id replacement did not end its session" + assert_absent "$dir/state/.claude-autoarm-failure-notified" "$signal replacement consumed a notice" + assert_absent "$dir/state/.claude-autoarm-failure-alarmed" "$signal replacement created an alarm" + fi + done + done + pass "auto-arm: all interruption signals preserve verified re-anchors and reject replacement session ids" +} + # --- abandoned single-flight claim recovery (legacy shim) ---------------------- # The 2026-08-14 lapse: one cycle armed, beat its beacon, delivered a single # rewake, and exited, leaving its owner lock behind with a live pid. The single @@ -2023,6 +2239,11 @@ test_arms_for_x_mode_poll_need_without_inflight test_arms_for_registered_custom_check_without_inflight test_single_flight_admits_exactly_one_owner test_term_mid_arm_commits_failure_and_rewakes +test_replacement_retires_interrupted_session_episode +test_reused_identity_with_new_session_retires_interrupted_episode +test_same_session_lock_move_preserves_interrupted_episode +test_missing_lock_interruption_is_silent +test_signal_session_identity_proof test_abandoned_owner_claim_is_reclaimed_and_rearms test_abandoned_claim_reclaim_reaps_dead_steal_without_nesting test_arming_claim_with_fresh_beacon_is_never_reclaimed diff --git a/tests/fm-codex-hook-layer-live-e2e.test.sh b/tests/fm-codex-hook-layer-live-e2e.test.sh index ec468e20165..fae10df0ecb 100755 --- a/tests/fm-codex-hook-layer-live-e2e.test.sh +++ b/tests/fm-codex-hook-layer-live-e2e.test.sh @@ -16,14 +16,17 @@ # and capability rather than falsely treating the effective setting as a # portable invariant. # -# It spends no model tokens (`codex features list` resolves configuration only), -# so it runs by default wherever codex is installed. +# The Stop-output cases use the installed app-server and a loopback Responses +# fixture with fixed text and no tool calls. They trust only the fixture hook +# and preserve the wrapper's inherited PATH without loading operator config. +# Neither layer spends model tokens, so the guard runs by default wherever +# its tools are installed. set -u # shellcheck source=tests/fixtures.sh . "$(dirname "${BASH_SOURCE[0]}")/fixtures.sh" -fm_live_gate default-on FM_CODEX_HOOK_LAYER_LIVE codex +fm_live_gate default-on FM_CODEX_HOOK_LAYER_LIVE codex python3 jq CODEX_VERSION=$(codex --version 2>&1) TMP_ROOT=$(fm_test_tmproot fm-codex-hook-layer-live) @@ -101,7 +104,202 @@ test_installed_codex_still_reports_the_hook_feature() { printf 'ok - codex %s still publishes the hook feature flag firstmate disables\n' "$CODEX_VERSION" } +test_installed_codex_accepts_the_stop_wrapper_and_rejects_invalid_output() { + python3 - "$ROOT" "$TMP_ROOT/stop-parser" "$CODEX_VERSION" <<'PY' +import http.server +import json +import os +from pathlib import Path +import queue +import shlex +import shutil +import subprocess +import sys +import threading +import time + +root, lab, version = Path(sys.argv[1]), Path(sys.argv[2]), sys.argv[3] +lab.mkdir() +wrapper = json.loads((root / '.codex/hooks.json').read_text())['hooks']['Stop'][0]['hooks'][0]['command'] +emitter = lab / 'emit.py' +emitter.write_text('''import json,sys +mode=sys.argv[1] +active=json.load(sys.stdin).get('stop_hook_active',False) +outputs={ + 'empty': ('','',0), 'json': ('{}\\n','',0), + 'mixed': ('diagnostic\\n{}\\n','',0), 'syntax': ('{','',0), + 'schema': ('{"continue":"yes"}','',0), 'two-objects': ('{}\\n{}','',0), + 'stderr': ('{}\\n','diagnostic\\n',0), 'error': ('','fixture error\\n',7)} +if mode=='continue': + out,err,code=('', 'fixture continuation\\n', 2) if not active else ('{}\\n','',0) +else: out,err,code=outputs[mode] +sys.stdout.write(out);sys.stderr.write(err);sys.exit(code) +''') +primary = lab / 'primary' +for name in ['bin', 'state', 'docs', '.codex']: + (primary / name).mkdir(parents=True, exist_ok=True) +(primary / 'AGENTS.md').write_text('Isolated Stop parser fixture.\n') +(primary / '.codex/hooks.json').write_text(json.dumps({'hooks': {'Stop': [{'hooks': [ + {'type': 'command', 'command': 'fm-turnend-guard.sh'}]}]}})) +for name in ['fm-turnend-guard.sh', 'fm-operational-input.sh', 'fm-supervision-instructions.sh', + 'fm-harness.sh', 'fm-primary-scope-lib.sh', 'fm-supervision-lib.sh', + 'fm-supervision-engine-lib.sh', 'fm-wake-lib.sh', 'fm-path-lib.sh', + 'fm-hook-host-lib.sh', 'fm-session-lock-lib.sh', 'fm-cursor-lib.sh', + 'fm-hermes-process-lib.sh', 'fm-timeout-lib.sh']: + shutil.copy2(root / 'bin' / name, primary / 'bin' / name) +shutil.copytree(root / 'docs/supervision-protocols', primary / 'docs/supervision-protocols') +subprocess.run(['git', 'init', '-q', str(primary)], check=True) + +class Responses(http.server.BaseHTTPRequestHandler): + requests = 0 + + def log_message(self, *args): + pass + + def do_POST(self): + self.rfile.read(int(self.headers.get('Content-Length', '0'))) + Responses.requests += 1 + message = {'id': 'msg_fixture', 'type': 'message', 'role': 'assistant', + 'status': 'completed', 'phase': 'final_answer', 'content': [ + {'type': 'output_text', 'text': 'fixture complete', 'annotations': []}]} + events = [ + {'type': 'response.created', 'response': {'id': 'resp_fixture', 'status': 'in_progress', 'output': []}}, + {'type': 'response.output_item.added', 'output_index': 0, + 'item': {**message, 'status': 'in_progress', 'content': []}}, + {'type': 'response.output_item.done', 'output_index': 0, 'item': message}, + {'type': 'response.completed', 'response': {'id': 'resp_fixture', 'status': 'completed', + 'output': [message], 'usage': {'input_tokens': 1, 'output_tokens': 1, 'total_tokens': 2}}}, + ] + data = ''.join('event: ' + event['type'] + '\ndata: ' + json.dumps(event) + '\n\n' + for event in events).encode() + self.send_response(200) + self.send_header('Content-Type', 'text/event-stream') + self.send_header('Content-Length', str(len(data))) + self.end_headers() + self.wfile.write(data) + +server = http.server.ThreadingHTTPServer(('127.0.0.1', 0), Responses) +server_thread = threading.Thread(target=server.serve_forever, daemon=True) +server_thread.start() + +def check(mode, expected): + case = lab / mode + case.mkdir() + fixture_home = case / 'login' + fixture_home.mkdir() + (fixture_home / '.bash_profile').write_text(':\n') + (fixture_home / '.bash_logout').write_text("printf '\\033[H\\033[2J\\033[3J'\n") + codex_data = case / 'codex' + codex_data.mkdir() + command = wrapper if mode.startswith('wrapper-') else shlex.join([sys.executable, str(emitter), mode]) + (codex_data / 'hooks.json').write_text(json.dumps({'hooks': {'Stop': [{'hooks': [ + {'type': 'command', 'command': command, 'timeout': 10}]}]}})) + (codex_data / 'config.toml').write_text(f'''model="fixture" +model_provider="fixture" +[model_providers.fixture] +name="Loopback Stop fixture" +base_url="http://127.0.0.1:{server.server_port}/v1" +wire_api="responses" +requires_openai_auth=false +[features] +hooks=true +shell_snapshot=false +[analytics] +enabled=false +''') + # Keep the inherited PATH, but no operator config, tokens, hook startup + # overrides, or fleet paths enter the child app-server or its hooks. + env = {key: os.environ[key] for key in ['PATH', 'LANG', 'LC_ALL', 'DISABLE_AUTOUPDATER'] if key in os.environ} + env.update({'HOME': str(fixture_home), 'CODEX_HOME': str(codex_data), + 'SHELL': '/bin/sh', 'TMPDIR': str(lab), 'OTEL_SDK_DISABLED': 'true'}) + cwd = primary if mode in ['wrapper-idle', 'wrapper-block'] else case + (primary / 'state/task.meta').unlink(missing_ok=True) + if mode == 'wrapper-block': + (primary / 'state/task.meta').touch() + notifications = [] + messages = queue.Queue() + deadline = time.monotonic() + 40 + before = Responses.requests + with (case / 'stderr').open('w') as stderr: + process = subprocess.Popen(['codex', 'app-server', '--stdio'], cwd=case, env=env, + stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=stderr, text=True) + def read_lines(): + for line in process.stdout: + messages.put(json.loads(line)) + messages.put(None) + reader = threading.Thread(target=read_lines, daemon=True) + reader.start() + def send(message): + process.stdin.write(json.dumps(message) + '\n') + process.stdin.flush() + def receive(): + message = messages.get(timeout=max(0.1, deadline - time.monotonic())) + assert message is not None, 'app-server exited before its hook verdict' + notifications.append(message) + return message + def rpc(number, method, params): + send({'id': number, 'method': method, 'params': params}) + while True: + message = receive() + if message.get('id') == number: + assert 'error' not in message, message + return message['result'] + try: + rpc(1, 'initialize', {'clientInfo': {'name': 'isolated-stop-test', 'version': '1'}, + 'capabilities': {'experimentalApi': True}}) + send({'method': 'initialized'}) + listed = rpc(2, 'hooks/list', {'cwds': [str(case)]}) + hooks = [hook for entry in listed['data'] for hook in entry['hooks'] + if hook['eventName'] == 'stop' and hook['sourcePath'] == str(codex_data / 'hooks.json')] + assert len(hooks) == 1, listed + hook = hooks[0] + thread = rpc(3, 'thread/start', {'cwd': str(cwd), 'model': 'fixture', 'modelProvider': 'fixture', + 'approvalPolicy': 'never', 'sandbox': 'read-only', 'ephemeral': True, + 'config': {'hooks.state': {hook['key']: {'trusted_hash': hook['currentHash']}}}}) + rpc(4, 'turn/start', {'threadId': thread['thread']['id'], + 'input': [{'type': 'text', 'text': 'fixture', 'text_elements': []}]}) + while receive().get('method') != 'turn/completed': + pass + runs = [message['params']['run'] for message in notifications + if message.get('method') == 'hook/completed' and message['params']['run']['eventName'] == 'stop'] + assert [run['status'] for run in runs] == expected, (mode, runs) + assert Responses.requests - before == len(expected), (mode, Responses.requests - before) + if mode in ['mixed', 'syntax', 'schema', 'two-objects']: + assert runs[0]['entries'] == [{'kind': 'error', 'text': 'hook returned invalid stop hook JSON output'}], runs + if mode == 'error': + assert runs[0]['entries'] == [{'kind': 'error', 'text': 'hook exited with code 7'}], runs + if mode == 'wrapper-block': + assert any('TURN WOULD END BLIND' in entry['text'] for entry in runs[0]['entries']), runs + finally: + process.terminate() + try: + process.wait(timeout=3) + except subprocess.TimeoutExpired: + process.kill() + process.wait() + reader.join(timeout=1) + +try: + for mode in ['empty', 'json', 'stderr', 'wrapper-noop', 'wrapper-idle']: + check(mode, ['completed']) + for mode in ['mixed', 'syntax', 'schema', 'two-objects', 'error']: + check(mode, ['failed']) + for mode in ['continue', 'wrapper-block']: + check(mode, ['blocked', 'completed']) + print(f'ok - codex {version}: Stop parser checked 12 isolated cases with clean wrapper JSON and bounded continuation') +except Exception as error: + raise SystemExit(f'not ok - codex {version}: isolated Stop parser failed: {error!r}') from error +finally: + server.shutdown() + server.server_close() + server_thread.join() +PY + local status=$? + [ "$status" -eq 0 ] || fail "codex $CODEX_VERSION: installed Stop parser regression failed" +} + test_installed_codex_still_reports_the_hook_feature test_installed_codex_accepts_requested_hook_disablement_for_the_captured_crewmate_launch +test_installed_codex_accepts_the_stop_wrapper_and_rejects_invalid_output echo "# all fm-codex-hook-layer-live-e2e tests passed" diff --git a/tests/fm-composer-lib.test.sh b/tests/fm-composer-lib.test.sh index 57f4d78c142..09bdb2b1669 100755 --- a/tests/fm-composer-lib.test.sh +++ b/tests/fm-composer-lib.test.sh @@ -466,6 +466,102 @@ test_composer_footer_zone_refuses_rather_than_allows() { pass "fm_composer_classify_screen: the footer zone only ever refuses, never allows" } +# Claude Code 2.1.291 marks the selected row of its slash-command popup with the +# same `❯` its composer uses, where 2.1.289 drew that row unmarked. The popup +# renders below the composer's closing rule, so the marked row became a second +# bare composer and the typed command's own read-back was the whole popup list. +# The screens are real captures (Herdr 0.8.2, Claude Code 2.1.291 on Haiku 4.5), +# reduced to the composer, its rules, and the first popup rows with their SGR +# bytes intact. `slash-only` is the case where the selected row is NOT the typed +# text, `truncated-name` the one where the row opens with `…` instead of `/`, +# and `exit-typed-with-notice` carries the notification row Claude draws between +# the rule and the popup when it runs inside another Claude session. +test_claude_slash_popup_marked_row_is_not_a_second_composer() { + local dir=$ROOT/tests/captures/claude-2.1.291-slash-popup pair name want screen out + local claude_idle + claude_idle=$(printf 'claude\tidle') + for pair in \ + 'exit-typed:/exit' \ + 'exit-typed-with-notice:/exit' \ + 'no-mistakes-typed:/no-mistakes' \ + 'slash-only:/' \ + 'truncated-name:/claude-md-management:claude-md-improver' + do + name=${pair%%:*} + want=${pair#*:} + screen=$(cat "$dir/$name.ansi") + out=$(FM_COMPOSER_GHOST_LUMA_MAX=0 fm_composer_extract_selected_content "$CAPS_STYLED_NOID" "$screen") + [ "$out" = "$want" ] || fail "$name: the composer content must be '$want', not the popup list, got '$out'" + out=$(LC_ALL=C FM_COMPOSER_GHOST_LUMA_MAX=0 fm_composer_extract_selected_content "$CAPS_STYLED_NOID" "$screen") + [ "$out" = "$want" ] || fail "$name under LC_ALL=C: the composer content must be '$want', got '$out'" + assert_screen "$name typed on herdr" pending "$CAPS_STYLED" "$screen" '' "$claude_idle" + assert_screen "$name typed on zellij" pending "$CAPS_STYLED_NOID" "$screen" + done + screen=$(cat "$dir/idle.ansi") + assert_screen "an idle composer with no popup stays empty" empty "$CAPS_STYLED" "$screen" '' "$claude_idle" + pass "fm_composer_extract_selected_content: Claude 2.1.291's marked popup row is not read as the composer" +} + +test_claude_slash_popup_demotion_only_ever_refuses() { + # The popup demotion falls back to the envelope above the marked row, so it + # may only fire when that envelope holds typed `/` text and the row has the + # popup shape (`/name` or a truncated `…name`, a gap, then a description). + # Anything else keeps the old reading, and none of these may ever read empty. + local screen out style claude_idle original + local rule='────────────────────────' + local dir=$ROOT/tests/captures/claude-2.1.291-slash-popup + local typed=$'\e[38;2;177;185;249m/exit\e[0m' + claude_idle=$(printf 'claude\tidle') + original=$(cat "$dir/exit-typed.ansi") + # 1. An EMPTY composer with a popup-shaped row below it: a popup cannot be + # open over an empty composer, so the row is not demoted to the empty + # envelope above it. + screen=$rule$'\n❯'"$NBSP"$'\n'"$rule"$'\n ❯ /exit Exit the CLI\n /context Visualize context' + assert_screen "an empty composer above a popup-shaped row" pending "$CAPS_STYLED_NOID" "$screen" + out=$(fm_composer_extract_selected_content "$CAPS_STYLED_NOID" "$screen") + case "$out" in + '/exit Exit the CLI'*) ;; + *) fail "the lower row must stay the composer read when the envelope is empty, got '$out'" ;; + esac + # 2. A numbered-choice dialog row has neither the `/` opening nor the gap. + screen=$rule$'\n❯'"$NBSP"$'/exit\n'"$rule"$'\n ❯ 1. Yes\n 2. No' + assert_screen "a numbered choice below a typed command" pending "$CAPS_STYLED_NOID" "$screen" + out=$(fm_composer_extract_selected_content "$CAPS_STYLED_NOID" "$screen") + case "$out" in + '1. Yes'*) ;; + *) fail "a numbered-choice row must stay a bare candidate, got '$out'" ;; + esac + # 3. A single-spaced `/draft` row has no description gap: a live draft. + screen=$rule$'\n❯'"$NBSP"$'/exit\n'"$rule"$'\n ❯ /draft' + assert_screen "a one-word slash draft below a typed command" pending "$CAPS_STYLED_NOID" "$screen" + out=$(fm_composer_extract_selected_content "$CAPS_STYLED_NOID" "$screen") + [ "$out" = '/draft' ] || fail "a one-word slash draft must stay the composer read, got '$out'" + # 4. The envelope's typed text must itself open with `/`. + screen=$rule$'\n❯ fix the login bug\n'"$rule"$'\n ❯ /exit Exit the CLI' + assert_screen "typed prose above a popup-shaped row" pending "$CAPS_STYLED_NOID" "$screen" + out=$(fm_composer_extract_selected_content "$CAPS_STYLED_NOID" "$screen") + case "$out" in + '/exit Exit the CLI'*) ;; + *) fail "typed prose must not license the popup demotion, got '$out'" ;; + esac + # 5. The envelope's `/` must survive the ghost strip the verdict reads. The + # real 2.1.291 capture with its typed `/exit` restyled dark grey (the + # 112;112;112 Claude 2.1.283 drew it in) or SGR 2 dim strips to a bare + # `❯`, so that envelope is no fallback and the marked row keeps its read. + for style in $'\e[38;2;112;112;112m' $'\e[2m'; do + screen=${original/"$typed"/"$style/exit"$'\e[0m'} + [ "$screen" != "$original" ] || fail "the capture's typed /exit was not restyled" + assert_screen "a ghost-styled typed command on herdr" pending "$CAPS_STYLED" "$screen" '' "$claude_idle" + assert_screen "a ghost-styled typed command on zellij" pending "$CAPS_STYLED_NOID" "$screen" + done + # 6. A bordered box is no borderless row to fall back to, so a marked row + # directly below one still reads pending. + screen=$'transcript line\n╭───────────────────────────╮\n│ ❯ /exit │\n╰───────────────────────────╯\n ❯ /exit Exit the CLI\n /context Visualize context' + assert_screen "a typed command in a box above a marked row on herdr" pending "$CAPS_STYLED" "$screen" '' "$claude_idle" + assert_screen "a typed command in a box above a marked row on zellij" pending "$CAPS_STYLED_NOID" "$screen" + pass "fm_composer_classify_screen: the popup demotion needs typed slash text and the popup row shape, and never reads empty" +} + test_matrix_codex_dim_hint_row() { # Real idle codex: bold `›`, reset, then an SGR-2 dim hint. Styled captures # strip the ghost and prove empty; plain captures must defer as unknown - @@ -2049,6 +2145,8 @@ test_matrix_rejected_titled_composer_retires_prior_pair test_composer_footer_demotion_needs_a_proven_pair test_composer_footer_zone_is_shape_independent test_composer_footer_zone_refuses_rather_than_allows +test_claude_slash_popup_marked_row_is_not_a_second_composer +test_claude_slash_popup_demotion_only_ever_refuses test_matrix_codex_dim_hint_row test_matrix_muse_truecolor_glyph_survives_signal_loss test_claude_saturated_slash_command_is_typed_content diff --git a/tests/fm-control-relaunch.test.sh b/tests/fm-control-relaunch.test.sh index 2ff1ab7c720..9e34e08f8f6 100755 --- a/tests/fm-control-relaunch.test.sh +++ b/tests/fm-control-relaunch.test.sh @@ -1159,6 +1159,91 @@ test_secondmate_relaunch_picks_up_per_mate_override() { pass "fm-control relaunch resolves the selected mate's override profile" } +# add_listed_task : a live Claude ship +# recorded on in a home declaring two signed-in accounts, +# first and second, plus a signed-out one. +add_listed_task() { + local dir=$1 id=$2 recorded=$3 + add_ship_task "$dir" "$id" claude + make_claude_auth_stub "$dir" + mkdir -p "$dir/home/config" "$dir/first" "$dir/second" "$dir/signed-out" + : > "$dir/first/.credentials.json" + : > "$dir/second/.credentials.json" + printf '%s\n' "$dir/first" "$dir/second" "$dir/signed-out" > "$dir/home/config/claude-accounts" + [ -z "$recorded" ] || echo "account=$recorded" >> "$dir/home/state/$id.meta" +} + +test_account_list_relaunch_reuses_the_recorded_account() { + local dir out rc id=rl-list + dir=$(new_case list-reuse "$id") + add_listed_task "$dir" "$id" "$dir/first" + out=$(run_control "$dir" "$id" relaunch --note "same account"); rc=$? + expect_code 0 "$rc" "a relaunch under a list should reuse the recorded account"$'\n'"$out" + [ "$(meta_field "$dir" "$id" account)" = "$dir/first" ] || fail "the relaunched record should keep the recorded account" + assert_contains "$(cat "$dir/fake/literal")" "CLAUDE_CONFIG_DIR='$dir/first'" \ + "the replacement should launch under the recorded account" + : > "$dir/fake/literal" + printf 'zsh' > "$dir/fake/command" + out=$(run_spawn "$dir" "$id" --relaunch); rc=$? + expect_code 0 "$rc" "a direct fm-spawn --relaunch under a list should reuse the recorded account"$'\n'"$out" + assert_contains "$out" "account=$dir/first" "the direct relaunch should report the recorded account" + pass "fm-control relaunch: a declared account list keeps the account the task records" +} + +test_account_list_relaunch_moves_only_on_explicit_account() { + local dir out rc id=rl-list-move + dir=$(new_case list-move "$id") + add_listed_task "$dir" "$id" "$dir/first" + out=$(run_control "$dir" "$id" relaunch --account "$dir/second" --note "first is exhausted"); rc=$? + expect_code 0 "$rc" "an explicit --account relaunch should move the task"$'\n'"$out" + [ "$(meta_field "$dir" "$id" account)" = "$dir/second" ] || fail "the relaunched record should name the new account" + assert_contains "$(cat "$dir/fake/literal")" "CLAUDE_CONFIG_DIR='$dir/second'" \ + "the replacement should launch under the explicitly chosen account" + : > "$dir/fake/literal" + out=$(run_control "$dir" "$id" relaunch --note "stay"); rc=$? + expect_code 0 "$rc" "a later plain relaunch should succeed"$'\n'"$out" + [ "$(meta_field "$dir" "$id" account)" = "$dir/second" ] || fail "a later relaunch should keep the moved account" + pass "fm-control relaunch: only an explicit --account moves a task to another declared account" +} + +test_account_list_relaunch_refusals_happen_before_stop() { + local dir out rc id=rl-list-refuse case_name recorded flag needle + for case_name in undeclared newline signed-out unrecorded removed wrong-harness; do + dir=$(new_case "list-refuse-$case_name" "$id") + recorded="$dir/first" + flag=() + case "$case_name" in + undeclared) + flag=(--account "$dir/elsewhere") + needle="account '$dir/elsewhere' is not declared in config/claude-accounts" ;; + newline) + flag=(--account "$dir/elsewhere"$'\n'"$dir/second") + needle="--account must name one config/claude-accounts entry exactly, with no control characters" ;; + signed-out) + flag=(--account "$dir/signed-out") + needle="config/claude-accounts pins Claude workers to $dir/signed-out, which is not signed in" ;; + unrecorded) + recorded= + needle="must name one with --account" ;; + removed) + recorded="$dir/retired" + needle="account '$dir/retired' is not declared in config/claude-accounts" ;; + wrong-harness) + flag=(--harness codex --account "$dir/second") + needle="applies to Claude workers only, not codex" ;; + esac + add_listed_task "$dir" "$id" "$recorded" + cp "$dir/home/state/$id.meta" "$dir/meta-before" + out=$(run_control "$dir" "$id" relaunch ${flag[@]+"${flag[@]}"} --note "refuse $case_name"); rc=$? + expect_code 1 "$rc" "the $case_name relaunch must refuse"$'\n'"$out" + assert_contains "$out" "$needle" "the $case_name refusal should say why" + [ "$(cat "$dir/fake/command")" = claude ] || fail "the $case_name refusal must happen before the running agent stops" + [ ! -s "$dir/fake/literal" ] || fail "the $case_name refusal must send no lifecycle input" + cmp -s "$dir/meta-before" "$dir/home/state/$id.meta" || fail "the $case_name refusal must leave the task record untouched" + done + pass "fm-control relaunch: undeclared, newline-split, signed-out, unrecorded, retired, and wrong-runner accounts refuse before the old agent stops" +} + test_secondmate_account_pin_follows_relaunch_and_refuses_before_stop() { local dir out rc id=rl-sm-acct harness file variable pin for harness in claude pi; do @@ -1947,6 +2032,51 @@ test_missing_worktree_refuses_before_stopping_anything() { pass "fm-control relaunch: an unaccountable local copy refuses before the agent is touched" } +# A restart must never put a worker back in a primary home or a +# repository's primary checkout; the recorded worktree is the only launch cwd. +test_relaunch_refuses_a_recorded_worktree_that_is_the_primary_home() { + local dir out rc + dir=$(new_case homewt rl11) + add_ship_task "$dir" rl11 claude + sed -i.bak "s#^worktree=.*#worktree=$dir/home#" "$dir/home/state/rl11.meta" + printf 'zsh' > "$dir/fake/command" + out=$(run_spawn "$dir" rl11 --relaunch 2>&1); rc=$? + expect_code 1 "$rc" "a worktree that is the primary home should refuse" + assert_contains "$out" "primary home or checkout" "the refusal should name the primary home" + [ -z "$(cat "$dir/fake/literal")" ] || fail "a refused relaunch must launch nothing" + pass "fm-spawn --relaunch: a recorded worktree that is the primary home refuses" +} + +test_relaunch_refuses_a_recorded_worktree_that_is_the_project_primary_checkout() { + local dir out rc + dir=$(new_case projwt rl12) + add_ship_task "$dir" rl12 claude + sed -i.bak "s#^worktree=.*#worktree=$dir/proj#" "$dir/home/state/rl12.meta" + printf 'zsh' > "$dir/fake/command" + out=$(run_spawn "$dir" rl12 --relaunch 2>&1); rc=$? + expect_code 1 "$rc" "a worktree that is the project primary checkout should refuse" + assert_contains "$out" "primary home or checkout" "the refusal should name the primary checkout" + [ -z "$(cat "$dir/fake/literal")" ] || fail "a refused relaunch must launch nothing" + pass "fm-spawn --relaunch: a recorded worktree that is the project primary checkout refuses" +} + +# Inherited Git location overrides must not disguise the primary checkout: +# a linked worktree's GIT_DIR plus the primary checkout as GIT_WORK_TREE would +# make the git-dir and common-dir probes disagree. +test_relaunch_refuses_the_primary_checkout_under_inherited_git_overrides() { + local dir out rc linked_gd + dir=$(new_case projwtenv rl13) + add_ship_task "$dir" rl13 claude + linked_gd=$(git -C "$dir/wt" rev-parse --absolute-git-dir) + sed -i.bak "s#^worktree=.*#worktree=$dir/proj#" "$dir/home/state/rl13.meta" + printf 'zsh' > "$dir/fake/command" + out=$(GIT_DIR="$linked_gd" GIT_WORK_TREE="$dir/proj" run_spawn "$dir" rl13 --relaunch 2>&1); rc=$? + expect_code 1 "$rc" "the primary checkout should refuse even under inherited GIT_DIR/GIT_WORK_TREE" + assert_contains "$out" "primary home or checkout" "the refusal should name the primary checkout" + [ -z "$(cat "$dir/fake/literal")" ] || fail "a refused relaunch must launch nothing" + pass "fm-spawn --relaunch: inherited Git overrides do not hide the project primary checkout" +} + test_missing_instructions_refuse_before_stopping_anything() { local dir out rc dir=$(new_case nobrief rl11) @@ -3211,6 +3341,9 @@ test_native_ultra_relaunch_preserves_profile_and_rejects_before_stop test_signed_out_worker_account_pin_refuses_before_stop test_seated_codex_relaunch_preflights_current_dock_before_stop test_worker_account_pin_follows_the_relaunch +test_account_list_relaunch_reuses_the_recorded_account +test_account_list_relaunch_moves_only_on_explicit_account +test_account_list_relaunch_refusals_happen_before_stop test_secondmate_account_pin_follows_relaunch_and_refuses_before_stop test_pi_exclude_tools_follow_the_relaunch test_exclude_tools_refusals_happen_before_the_agent_stops @@ -3237,6 +3370,9 @@ test_prefixed_prior_harness_wiring_is_still_retired test_muse_session_binding_is_retired_on_a_harness_switch test_cursor_session_binding_is_retired_on_a_harness_switch test_missing_worktree_refuses_before_stopping_anything +test_relaunch_refuses_a_recorded_worktree_that_is_the_primary_home +test_relaunch_refuses_a_recorded_worktree_that_is_the_project_primary_checkout +test_relaunch_refuses_the_primary_checkout_under_inherited_git_overrides test_missing_instructions_refuse_before_stopping_anything test_checkpoint_refusal_leaves_the_record_byte_identical test_checkpoint_refuses_uninspectable_head_and_status diff --git a/tests/fm-cortex-harness.test.sh b/tests/fm-cortex-harness.test.sh index b133cda1062..433b2dcb94d 100755 --- a/tests/fm-cortex-harness.test.sh +++ b/tests/fm-cortex-harness.test.sh @@ -253,7 +253,7 @@ fm_git_worktree "$proj" "$wt" cortex-test fm_test_spawn_brief "$home" cortex-worker if ! out=$(FM_FAKE_LAUNCH_LOG="$case_dir/launch" fm_test_run_spawn "$home" "$wt" "$fakebin" cortex-worker "$proj" --scout --harness cortex --model claude-sonnet-5-5 --effort high 2>&1) then fail "spawn failed: $out"; fi -launch=$(cat "$case_dir/launch") +launch=$(fm_test_launch_unframe "$(cat "$case_dir/launch")") assert_contains "$launch" '--dangerously-allow-all-tool-calls' 'autonomy flag missing' assert_contains "$launch" "--plugin-dir '$home/state/cortex-worker.cortex-plugin'" 'private plugin missing' assert_contains "$launch" "--model 'claude-sonnet-5-5'" 'model lost' @@ -274,7 +274,7 @@ fm_git_worktree "$proj2" "$wt2" cortex-xhigh fm_test_spawn_brief "$home" cortex-xhigh if ! out=$(FM_FAKE_LAUNCH_LOG="$case_dir/launch-xhigh" fm_test_run_spawn "$home" "$wt2" "$fakebin" cortex-xhigh "$proj2" --scout --harness cortex --effort xhigh 2>&1) then fail "xhigh spawn failed: $out"; fi -case "$(cat "$case_dir/launch-xhigh")" in *--effort*) fail 'xhigh reached Cortex argv, which has no such level' ;; esac +case "$(fm_test_launch_unframe "$(cat "$case_dir/launch-xhigh")")" in *--effort*) fail 'xhigh reached Cortex argv, which has no such level' ;; esac assert_grep 'effort=xhigh' "$home/state/cortex-xhigh.meta" 'effort not recorded' pass "scout launch carries autonomy, plugin, typed brief and hooks; xhigh effort omitted" diff --git a/tests/fm-daemon.test.sh b/tests/fm-daemon.test.sh index 81f11ac5784..96ed58cef47 100755 --- a/tests/fm-daemon.test.sh +++ b/tests/fm-daemon.test.sh @@ -1167,7 +1167,7 @@ test_handle_wake_terminal_signal_clears_pause_tracking() { printf 'window=%s\nkind=ship\n' "$win" > "$state/held-w10-terminal.meta" printf 'done: upstream landed\n' > "$state/held-w10-terminal.status" key=$(printf '%s' "held-w10-terminal" | tr '.:/' '___') - watcher_key=$(printf '%s' "$win" | tr '.:/' '___') + watcher_key=$(watch_window_key "$win") date +%s > "$state/.subsuper-paused-$key" date +%s > "$state/.subsuper-stale-$key" : > "$state/.paused-$watcher_key" @@ -1191,7 +1191,7 @@ test_housekeeping_migrates_watcher_pause_marker() { win="sess:fm-held-w10-migrate" printf 'window=%s\nkind=ship\n' "$win" > "$state/held-w10-migrate.meta" printf 'paused: awaiting the upstream release\n' > "$state/held-w10-migrate.status" - key=$(printf '%s' "$win" | tr '.:/' '___') + key=$(watch_window_key "$win") : > "$state/.paused-$key" FM_STATE_OVERRIDE="$state" housekeeping "$state" key=$(printf '%s' "held-w10-migrate" | tr '.:/' '___') @@ -1207,7 +1207,7 @@ test_housekeeping_migrates_watcher_unpaused_marker_to_clear() { win="sess:fm-held-w10-migrate-unpaused" printf 'window=%s\nkind=ship\n' "$win" > "$state/held-w10-migrate-unpaused.meta" printf 'working: upstream landed, resuming\n' > "$state/held-w10-migrate-unpaused.status" - watcher_key=$(printf '%s' "$win" | tr '.:/' '___') + watcher_key=$(watch_window_key "$win") : > "$state/.paused-$watcher_key" FM_STATE_OVERRIDE="$state" housekeeping "$state" key=$(printf '%s' "held-w10-migrate-unpaused" | tr '.:/' '___') diff --git a/tests/fm-dispatch-resolve.test.sh b/tests/fm-dispatch-resolve.test.sh index 7df7d7291d0..4df0366c7df 100755 --- a/tests/fm-dispatch-resolve.test.sh +++ b/tests/fm-dispatch-resolve.test.sh @@ -9,6 +9,7 @@ # No case touches the network, and the absent-key case proves the tool makes # no call at all. set -u +unset TYPESAFE_BASE # shellcheck source=tests/lib.sh . "$(dirname "${BASH_SOURCE[0]}")/lib.sh" @@ -428,7 +429,7 @@ assert_contains "$out" ' note: 1 eligible candidate(s) unranked (kimi)' "clear assert_not_contains "$out" '--effort' "cursor profile without effort emits no --effort" argv=$(cat "$LOG/argv") assert_not_contains "$argv" "$KEY" "the key never appears on curl argv" -assert_contains "$argv" 'https://api.typesafe.ai/v1/systemone' "the request uses the fixed typesafe.ai endpoint" +assert_contains "$argv" 'https://api.typesafe.ai/v1/systemone' "the request defaults to the typesafe.ai endpoint" assert_contains "$argv" $'--max-time\n5' "the request uses the fixed five-second timeout" assert_contains "$argv" '@/dev/fd/3' "the header is read from a file descriptor" assert_equals "Authorization: Bearer $KEY" "$(cat "$LOG/header")" "curl receives the bearer header on fd 3" @@ -447,6 +448,127 @@ assert_not_contains "$body" 'spendPriority' "quota never leaves the machine" assert_not_contains "$body" 'cursor-grok' "use profiles never leave the machine" pass "clear: one rule Choice request, key on the fd header only, spendPriority argmax over every candidate" +# --- TypeSafe base: default, environment/.env precedence, and safe URLs ------ +while IFS='|' read -r base endpoint; do + reset_log + TYPESAFE_BASE=$base TYPESAFE_API_KEY=$KEY run code out err "$BRIEF" + expect_code 0 "$code" "custom TypeSafe base resolves" + assert_contains "$out" ' status: clear' "custom TypeSafe base keeps the typed response contract" + assert_equals "$endpoint" "$(grep -E '^https?://' "$LOG/argv")" "custom base has exactly one endpoint suffix" + # curl argv is the external request contract: --globoff makes punctuation literal. + assert_contains "$(cat "$LOG/argv")" $'--globoff\n' "custom base disables curl URL expansion" + assert_equals "Authorization: Bearer $KEY" "$(cat "$LOG/header")" "custom base keeps the fd header transport" + assert_not_contains "$(cat "$LOG/argv")" "$KEY" "custom base keeps the key out of argv" + assert_equals $'curl:clean\nquota-axi:clean' "$(cat "$LOG/child-env")" "custom base keeps the key out of child environments" +done <<'BASE_ENDPOINTS' +https://gateway.example/typesafe/|https://gateway.example/typesafe/v1/systemone +http://localhost:8080/proxy///|http://localhost:8080/proxy/v1/systemone +http://typesafe_gateway:8080|http://typesafe_gateway:8080/v1/systemone +https://[::1]:8443/typesafe/|https://[::1]:8443/typesafe/v1/systemone +https://gateway.example:1/tenant/{a,b}/|https://gateway.example:1/tenant/{a,b}/v1/systemone +https://gateway.example:65535/|https://gateway.example:65535/v1/systemone +http://127.0.0.1:8080/|http://127.0.0.1:8080/v1/systemone +https://[2001:db8::abcd]/|https://[2001:db8::abcd]/v1/systemone +https://[2001:db8:0:1:2:3:4:5]/|https://[2001:db8:0:1:2:3:4:5]/v1/systemone +https://[::ffff:192.0.2.1]/|https://[::ffff:192.0.2.1]/v1/systemone +BASE_ENDPOINTS +printf '%s\n' 'TYPESAFE_BASE=https://dotenv.example/typesafe/' > "$HOME_DIR/.env" +reset_log +FM_DISPATCH_PROVIDER=typesafe TYPESAFE_API_KEY=$KEY run code out err "$BRIEF" +assert_equals 'https://dotenv.example/typesafe/v1/systemone' "$(grep -E '^https?://' "$LOG/argv")" ".env supplies the TypeSafe base" +reset_log +TYPESAFE_BASE=https://environment.example/gateway/ TYPESAFE_API_KEY=$KEY run code out err "$BRIEF" +assert_equals 'https://environment.example/gateway/v1/systemone' "$(grep -E '^https?://' "$LOG/argv")" "environment base wins over .env" +rm -f "$HOME_DIR/.env" +for base in \ + ftp://gateway.example/typesafe \ + gateway.example/typesafe \ + https:///typesafe \ + https://user:password@gateway.example/typesafe \ + https://gateway.example/typesafe?key=value \ + https://gateway.example/typesafe? \ + https://gateway.example/typesafe#fragment \ + https://gateway.example/typesafe# \ + https://gateway.example:0 \ + https://gateway.example:65536 \ + https://gateway.example:999999999999999999999999999999 \ + https://gateway.example:abc \ + https://gateway.example: \ + 'https://[abc]' \ + 'https://[1:2:3:4:5:6:7]' \ + 'https://[1:2:3:4:5:6:7:8:9]' \ + 'https://[1:2:3:4:5:6:7::8]' \ + 'https://[1::2::3]' \ + 'https://[:::1]' \ + 'https://[12345::1]' \ + 'https://[::ffff:192.0.2.999]' \ + https://::1 \ + 'https://gateway.example/tenant[1-2]' \ + 'https://gateway.example/tenant[' \ + 'https://gateway.example/tenant]' \ + 'https://gateway.example/bad path' \ + $'https://gateway.example/\n' \ + $'https://gateway.example/\npath'; do + reset_log + TYPESAFE_BASE=$base TYPESAFE_API_KEY=$KEY run code out err "$BRIEF" + expect_code 2 "$code" "invalid TypeSafe base is a configuration error" + assert_contains "$err" 'TYPESAFE_BASE' "invalid base names the setting" + assert_equals '' "$out" "invalid base never produces a fallback profile" + assert_absent "$LOG/argv" "invalid base never sends a request" + assert_absent "$LOG/quota-axi.calls" "invalid base never reads quota" +done +printf '%s\n' 'TYPESAFE_BASE=https://user:password@dotenv.example' > "$HOME_DIR/.env" +reset_log +TYPESAFE_API_KEY=$KEY run code out err "$BRIEF" +expect_code 2 "$code" "invalid .env base is a configuration error" +assert_contains "$err" 'TYPESAFE_BASE' "invalid .env base names the setting" +assert_not_contains "$err" 'password' "invalid base diagnostic does not echo credentials" +assert_absent "$LOG/argv" "invalid .env base never sends a request" +reset_log +run code out err "$BRIEF" +expect_code 2 "$code" "invalid base is actionable even without a key" +assert_absent "$LOG/argv" "invalid base without a key never sends a request" +reset_log +TYPESAFE_BASE=https://environment.example TYPESAFE_API_KEY=$KEY run code out err "$BRIEF" +assert_equals 'https://environment.example/v1/systemone' "$(grep -E '^https?://' "$LOG/argv")" "valid environment base overrides an invalid .env base" +rm -f "$HOME_DIR/.env" + +# The configuration contract precedes both off and declared-key selection. +# Invalid addresses must never reach Keychain, curl, or quota, for either +# provider spelling and either configuration source. Literal errors are the +# oracle; returning off or an HTTP error would violate that contract. +for base in https://gateway.example:99999 'https://[abc]' \ + 'https://gateway.example/tenant[1-2]' 'https://gateway.example/tenant[' 'https://gateway.example/tenant]'; do + for provider in '' typesafe; do + for source in environment dotenv; do + for credential in absent environment declared; do + reset_log + configured_base=$base + configured_key='' + [ "$credential" != environment ] || configured_key=$KEY + if [ "$source" = dotenv ]; then + printf 'TYPESAFE_BASE=%s\n' "$base" > "$HOME_DIR/.env" + configured_base='' + fi + if [ "$credential" = declared ]; then + printf '%s\n' '{"typesafe":{"source":"macos-keychain","service":"must-not-be-read"}}' > "$HOME_DIR/config/dispatch-credential-sources.json" + fi + FM_DISPATCH_PROVIDER=$provider TYPESAFE_BASE=$configured_base TYPESAFE_API_KEY=$configured_key run code out err "$BRIEF" + expect_code 2 "$code" "invalid address fails before credential selection" + assert_contains "$err" 'TYPESAFE_BASE' "invalid address reports a configuration error" + assert_equals '' "$out" "invalid address emits no resolution" + assert_not_contains "$err" "$base" "invalid address is not echoed" + assert_not_contains "$err" "$KEY" "invalid address error keeps credentials private" + assert_absent "$LOG/security.argv" "invalid address never looks up credentials" + assert_absent "$LOG/argv" "invalid address never sends a request" + assert_absent "$LOG/quota-axi.calls" "invalid address never reads quota" + rm -f "$HOME_DIR/.env" "$HOME_DIR/config/dispatch-credential-sources.json" + done + done + done +done +pass "TypeSafe base supports environment and .env overrides and rejects unsafe URLs before sending" + # --- OpenRouter uses the same Choice validation and resolution path --------- OR_KEY='test-openrouter-key-never-in-child-env-or-argv' OR_RESPONSE="$TMP_ROOT/openrouter-response.json" @@ -462,7 +584,7 @@ assert_equals '' "$out" "an OpenRouter key alone does not select the provider" assert_contains "$err" 'TYPESAFE_API_KEY absent' "unset provider retains the typesafe off gate" assert_absent "$LOG/argv" "an OpenRouter key alone sends nothing" reset_log -FM_DISPATCH_PROVIDER=openrouter FM_DISPATCH_MODEL=typesafe/jev-1.13 \ +TYPESAFE_BASE=invalid-but-unused FM_DISPATCH_PROVIDER=openrouter FM_DISPATCH_MODEL=typesafe/jev-1.13 \ OPENROUTER_API_KEY=$OR_KEY FAKE_CURL_RESPONSE=$OR_RESPONSE run code out err "$BRIEF" --project pager expect_code 0 "$code" "OpenRouter clear exits 0" assert_contains "$out" ' status: clear' "OpenRouter clear status" @@ -836,6 +958,7 @@ assert_contains "$out" ' status: clear' "systemone clear status" assert_contains "$out" " profile: --harness 'cursor' --model 'cursor-grok-4.6-medium'" "systemone uses the shared quota argmax" assert_contains "$(cat "$LOG/argv")" 'http://127.0.0.1:8080/v1/systemone' "systemone posts to /v1/systemone with a trailing slash trimmed" assert_contains "$(cat "$LOG/argv")" $'--max-time\n10' "systemone timeout" +assert_contains "$(cat "$LOG/argv")" $'--globoff\n' "the keyless request path also disables curl URL expansion" assert_not_contains "$(cat "$LOG/argv")" '@/dev/fd/3' "systemone without a key sends no Authorization header" assert_equals 'fd3 unreadable' "$(cat "$LOG/header")" "systemone without a key opens no header descriptor" assert_equals "$(jq -Sc 'del(.model)' "$TMP_ROOT/typesafe-body.json")" "$(jq -Sc 'del(.model)' "$LOG/body")" "systemone sends the unchanged System One request" diff --git a/tests/fm-extension-binding.test.sh b/tests/fm-extension-binding.test.sh index 04be3a785d0..10d13499bd5 100644 --- a/tests/fm-extension-binding.test.sh +++ b/tests/fm-extension-binding.test.sh @@ -439,19 +439,23 @@ run_extension_section_lane() { return "$section_rc" } +# The coordinator stops any section lane, including the aggregate run's, that +# outlives this bound, so no wait inside a section can usefully run longer. +EXTENSION_SECTION_LANE_MAX_SECONDS=90 + run_extension_section_lanes() { - local section result_file section_rc timeout_seconds deadline index remaining launched total maximum_sections + local section result_file section_rc timeout_seconds deadline deadline_epoch index remaining launched total maximum_sections local active maximum_concurrent local -a sections=("$@") local -a section_pids=() local -a section_results=() local -a section_complete=() local section_result_root - timeout_seconds=${FM_EXTENSION_BINDING_COORDINATOR_TIMEOUT_SECONDS:-90} + timeout_seconds=${FM_EXTENSION_BINDING_COORDINATOR_TIMEOUT_SECONDS:-$EXTENSION_SECTION_LANE_MAX_SECONDS} case "$timeout_seconds" in ''|*[!0-9]*) return 64 ;; esac - [ "$timeout_seconds" -gt 0 ] && [ "$timeout_seconds" -le 90 ] || return 64 + [ "$timeout_seconds" -gt 0 ] && [ "$timeout_seconds" -le "$EXTENSION_SECTION_LANE_MAX_SECONDS" ] || return 64 section_result_root=$(mktemp -d "$TMP_ROOT/section-lanes.XXXXXX") || return 1 total=${#sections[@]} # Sixteen selectors are validated here. The bounded aggregate keeps its @@ -463,17 +467,18 @@ run_extension_section_lanes() { [ "$total" -le "$maximum_sections" ] || return 64 launched=0 active=0 + deadline=$((SECONDS + timeout_seconds)) + deadline_epoch=$(($(date +%s) + timeout_seconds)) while [ "$launched" -lt "$total" ] && [ "$active" -lt "$maximum_concurrent" ]; do section=${sections[$launched]} result_file="$section_result_root/$launched.result" - run_extension_section_lane "$result_file" "$section" & + FM_EXTENSION_BINDING_LANE_DEADLINE_EPOCH="$deadline_epoch" run_extension_section_lane "$result_file" "$section" & section_pids+=("$!") section_results+=("$result_file") section_complete+=("") launched=$((launched + 1)) active=$((active + 1)) done - deadline=$((SECONDS + timeout_seconds)) remaining=$total while [ "$remaining" -gt 0 ]; do for index in "${!section_pids[@]}"; do @@ -505,7 +510,7 @@ run_extension_section_lanes() { while [ "$launched" -lt "$total" ] && [ "$active" -lt "$maximum_concurrent" ]; do section=${sections[$launched]} result_file="$section_result_root/$launched.result" - run_extension_section_lane "$result_file" "$section" & + FM_EXTENSION_BINDING_LANE_DEADLINE_EPOCH="$deadline_epoch" run_extension_section_lane "$result_file" "$section" & section_pids+=("$!") section_results+=("$result_file") section_complete+=("") @@ -2063,6 +2068,7 @@ if section_enabled lifecycle-flow lifecycle-orphan-readiness; then P_INVOCATION_CLEANUP="$PACKAGES/orphan-cleanup" make_package "$P_INVOCATION_CLEANUP" org.example.invocation-cleanup ext-invocation-cleanup H_ORPHAN_READY="$HOMES/orphan-ready"; new_home "$H_ORPHAN_READY" +fm_test_track_procevent_home "$H_ORPHAN_READY" "$FM_PROCEVENT_CLAIM_ROOT" bind_package "$H_ORPHAN_READY" "$P_INVOCATION_CLEANUP" ext-invocation-cleanup >/dev/null for orphan_source in orphan-first orphan-second; do FM_HOME="$H_ORPHAN_READY" "$PROCEVENT" register-extension ext-invocation-cleanup "$orphan_source" --config-ref silent-result >/dev/null @@ -2138,18 +2144,43 @@ write_orphan_ready "$orphan_token" "$orphan_dead_pid" "barrier-token:$orphan_tok orphan_reconcile=$(FM_HOME="$H_ORPHAN_READY" "$PROCEVENT" reconcile) assert_contains "$orphan_reconcile" "started=2" "dead orphan readiness blocked recovery of two extension sources" assert_contains "$orphan_reconcile" "uncertain=0" "recovered extension sources remained uncertain" +# Reconcile confirms each launch, not its completion. Each recovered runner owns +# its poll, capture, silent verdict, and terminal retirement, and the two take +# their verdicts in turn under this home's extension lifecycle lock, so a loaded +# host stretches the pair past any fixed marker poll. Wait for the recorded +# runner groups to exit instead, and stop any survivor before failing. +orphan_runners=() for orphan_source in orphan-first orphan-second; do - for _ in $(seq 1 200); do - [ -f "$H_ORPHAN_READY/state/procevent-inbox/$orphan_source.1.handled" ] && break - sleep 0.05 + orphan_runner=$(sed -n '2p' "$FM_PROCEVENT_CLAIM_ROOT/$orphan_source.claim" 2>/dev/null || true) + [ -z "$orphan_runner" ] || orphan_runners+=("$orphan_source:$orphan_runner") +done +orphan_wait_seconds=$EXTENSION_SECTION_LANE_MAX_SECONDS +if [ -n "${FM_EXTENSION_BINDING_LANE_DEADLINE_EPOCH:-}" ]; then + orphan_remaining=$((FM_EXTENSION_BINDING_LANE_DEADLINE_EPOCH - $(date +%s) - 3)) + [ "$orphan_remaining" -ge "$orphan_wait_seconds" ] || orphan_wait_seconds=$orphan_remaining + [ "$orphan_wait_seconds" -ge 1 ] || orphan_wait_seconds=1 +fi +orphan_deadline=$((SECONDS + orphan_wait_seconds)) +while :; do + orphan_live=() + for orphan_runner in "${orphan_runners[@]+"${orphan_runners[@]}"}"; do + kill -0 -"${orphan_runner#*:}" 2>/dev/null && orphan_live+=("$orphan_runner") done + [ "${#orphan_live[@]}" -gt 0 ] && [ "$SECONDS" -lt "$orphan_deadline" ] || break + sleep 0.1 +done +if [ "${#orphan_live[@]}" -gt 0 ]; then + # Killing the runner groups strands their detached invocation groups, which + # the home sweep's invocation cleanup then reclaims as host-crash residue. + for orphan_runner in "${orphan_live[@]}"; do + kill -KILL -"${orphan_runner#*:}" 2>/dev/null || true + done + FM_HOME="$H_ORPHAN_READY" "$PROCEVENT" sweep-home >/dev/null 2>&1 || true + fail "recovered source ${orphan_live[*]%%:*} did not finish within ${orphan_wait_seconds}s" +fi +for orphan_source in orphan-first orphan-second; do assert_present "$H_ORPHAN_READY/state/procevent-inbox/$orphan_source.1.handled" \ "recovered source $orphan_source never handled its private result" - for _ in $(seq 1 200); do - [ ! -e "$H_ORPHAN_READY/state/procevent/$orphan_source.source" ] \ - && [ ! -e "$FM_PROCEVENT_CLAIM_ROOT/$orphan_source.claim" ] && break - sleep 0.05 - done assert_absent "$H_ORPHAN_READY/state/procevent/$orphan_source.source" "recovered terminal source did not retire" assert_absent "$FM_PROCEVENT_CLAIM_ROOT/$orphan_source.claim" "recovered terminal source retained its claim" done diff --git a/tests/fm-gate-refuse.test.sh b/tests/fm-gate-refuse.test.sh index 85ddb6f2d3b..b5f9eeca212 100755 --- a/tests/fm-gate-refuse.test.sh +++ b/tests/fm-gate-refuse.test.sh @@ -243,7 +243,9 @@ test_lab_home_private_tmux_socket_survives_deep_paths() { [ "${socket_dir#/tmp/fml.}" != "$socket_dir" ] || fail "socket directory is not under the short /tmp/fml prefix" cleanup_deep_lab() { - env TMUX_TMPDIR="$socket_dir" "$real_tmux" -L fm-lab kill-server >/dev/null 2>&1 || true + # The explicit socket, only while it exists: TMUX_TMPDIR alone falls back + # to /tmp/tmux- once the private directory is gone. + [ ! -S "$socket_path" ] || "$real_tmux" -S "$socket_path" kill-server >/dev/null 2>&1 || true "$LABHOME" teardown "$lab" >/dev/null 2>&1 || true fm_test_cleanup } @@ -256,13 +258,13 @@ test_lab_home_private_tmux_socket_survives_deep_paths() { local attempts=0 while [ ! -f "$ready" ] && [ "$attempts" -lt 20 ]; do sleep 0.05; attempts=$((attempts + 1)); done [ -f "$ready" ] || fail "fake primary did not start" - env TMUX_TMPDIR="$socket_dir" "$real_tmux" -L fm-lab has-session -t primary \ - || fail "primary session is not reachable through the lab's TMUX_TMPDIR" + "$real_tmux" -S "$socket_path" has-session -t primary \ + || fail "primary session is not reachable through the lab socket" if "$LABHOME" teardown "$lab" >/dev/null 2>&1; then fail "lab teardown removed the directory while its server was running" fi [ -d "$socket_dir" ] || fail "refused active-server teardown removed the socket directory" - env TMUX_TMPDIR="$socket_dir" "$real_tmux" -L fm-lab kill-server \ + "$real_tmux" -S "$socket_path" kill-server \ || fail "could not stop the isolated lab tmux server" mkdir -p "$TMP/failing-tmux-bin" printf '#!/bin/sh\necho "tmux: probe failed" >&2\nexit 1\n' > "$TMP/failing-tmux-bin/tmux" diff --git a/tests/fm-herdr-projected-cd-live-e2e.test.sh b/tests/fm-herdr-projected-cd-live-e2e.test.sh new file mode 100755 index 00000000000..c3a8a1f84e8 --- /dev/null +++ b/tests/fm-herdr-projected-cd-live-e2e.test.sh @@ -0,0 +1,114 @@ +#!/usr/bin/env bash +# Real projected spawn guard: only a named Herdr lab session is ever touched. +# A disposable clone stands in for the already leased copy, so the guard never +# allocates, returns, or changes the developer's shared Treehouse pool. +set -eu +ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +# shellcheck source=tests/fixtures.sh +. "$ROOT/tests/fixtures.sh" +fm_live_gate default-on FM_HERDR_PROJECTED_CD_LIVE_E2E herdr jq +HERDR_LAB_HELPER=${HERDR_LAB_HELPER:-$ROOT/bin/fm-herdr-lab.sh} +HERDR_LAB_SESSION=$("$HERDR_LAB_HELPER" name fm-herdr-projected-cd-fix) +export HERDR_LAB_HELPER HERDR_LAB_SESSION +ORIGINAL_PATH=$PATH +export ORIGINAL_PATH + +TMP_ROOT=$(mktemp -d /tmp/fm-projected-cd.XXXXXX) +cleanup() { + local status=$? + "$HERDR_LAB_HELPER" teardown "$HERDR_LAB_SESSION" || status=1 + find "$TMP_ROOT" -type d -exec chmod u+rwx {} + + rm -rf "$TMP_ROOT" + exit "$status" +} +trap cleanup EXIT +"$HERDR_LAB_HELPER" provision "$HERDR_LAB_SESSION" +mkdir -p "$TMP_ROOT/fakebin" +cat > "$TMP_ROOT/fakebin/herdr" <<'EOF' +#!/usr/bin/env bash +set -eu +args=("$@") +if [ "${1:-}" = --version ]; then + PATH="$ORIGINAL_PATH" "$HERDR_LAB_HELPER" run "$HERDR_LAB_SESSION" status --json | jq -r '"herdr " + .client.version' + exit +fi +n=${#args[@]} +[ "$n" -lt 2 ] || { + [ "${args[n-2]}" != --session ] || { + [ "${args[n-1]}" = "$HERDR_LAB_SESSION" ] || exit 1 + unset 'args[n-1]' 'args[n-2]' + } +} +if [ "${args[0]:-} ${args[1]:-}" = 'pane run' ] && [[ "${args[3]:-}" == 'cd -- '* ]]; then + printf 'cd\n' >> "${TEST_SLOT}.entry-sends" + if [ "$DELAY_ENTRY" = 1 ] && [ ! -e "${TEST_SLOT}.entry-delayed" ]; then + : > "${TEST_SLOT}.entry-delayed" + args[3]="sleep 15; ${args[3]}" + fi +fi +exec env PATH="$ORIGINAL_PATH" "$HERDR_LAB_HELPER" run "$HERDR_LAB_SESSION" "${args[@]}" +EOF +cat > "$TMP_ROOT/fakebin/treehouse" <<'EOF' +#!/usr/bin/env bash +case "$1" in +get) printf '%s\n' "$TEST_SLOT" ;; +*) echo 'test refuses pool administration' >&2; exit 1 ;; +esac +EOF +chmod +x "$TMP_ROOT/fakebin/"* +lab() { "$HERDR_LAB_HELPER" run "$HERDR_LAB_SESSION" "$@"; } +VERSION=$(lab status --json | jq -er '.client.version') +# Projection is a default only at or above the presentation floor, so ask the +# same classifier fm-spawn consults; a supported release below it spawns flat. +FLOOR=$(PATH="$TMP_ROOT/fakebin:$PATH" bash -c ' + . "$1/bin/backends/herdr.sh" + status=0 + fm_backend_herdr_presentation_release_supported "$2" || status=$? + printf "%s %s %s\n" "$status" "$FM_BACKEND_HERDR_MIN_PRESENTATION_VERSION" "$FM_BACKEND_HERDR_PRESENTATION_RELEASE" +' _ "$ROOT" "$HERDR_LAB_SESSION") +FLOOR_STATUS=${FLOOR%% *} +FLOOR=${FLOOR#* } +FLOOR_VERSION=${FLOOR%% *} +FLOOR_RELEASE=${FLOOR#* } +case "$FLOOR_STATUS" in + 0) ;; + 1) + printf 'skip: Herdr %s is below the %s presentation floor, so spawns stay flat\n' \ + "$FLOOR_RELEASE" "$FLOOR_VERSION" + exit 0 + ;; + *) fail "Herdr $FLOOR_RELEASE could not be classified against the presentation floor" ;; +esac +HERDR_SOCKET_PATH=$(lab session list --json | jq -er --arg session "$HERDR_LAB_SESSION" '.sessions[] | select(.name == $session) | .socket_path') +export HERDR_SOCKET_PATH +fm_git_init_commit "$TMP_ROOT/project" +PARENT=$(lab workspace create --cwd "$TMP_ROOT/project" --label firstmate --focus) +HERDR_PANE_ID=$(printf '%s' "$PARENT" | jq -er '.result.root_pane.pane_id') +export HERDR_PANE_ID +for DELAY_ENTRY in 0 1; do + export DELAY_ENTRY + CASE="$TMP_ROOT/case-$DELAY_ENTRY" + mkdir -p "$CASE" + git clone -q "$TMP_ROOT/project" "$CASE/slot" + export TEST_SLOT="$CASE/slot" + fm_test_spawn_home "$CASE/home" codex + fm_test_spawn_brief "$CASE/home" projected-cd 'Exercise a projected shell worktree entry.' + if ! PATH="$TMP_ROOT/fakebin:$PATH" FM_HOME="$CASE/home" FM_ROOT_OVERRIDE="$ROOT" FM_SPAWN_NO_GUARD=1 HERDR_SESSION="$HERDR_LAB_SESSION" \ + bash "$ROOT/bin/fm-spawn.sh" projected-cd "$TMP_ROOT/project" "sh -c 'while :; do sleep 60; done'" --backend herdr --mode no-mistakes --yolo off > "$CASE/spawn.log" 2>&1; then + cat "$CASE/spawn.log" >&2 + fail "Herdr $VERSION projected spawn failed (delayed=$DELAY_ENTRY)" + fi + [ -s "$CASE/home/state/projected-cd.herdr-presentation" ] \ + || fail "Herdr $VERSION guard fell back to a flat spawn" + PANE=$(sed -n 's/^herdr_pane_id=//p' "$CASE/home/state/projected-cd.meta") + [ -n "$PANE" ] || fail "Herdr $VERSION spawn recorded no pane" + ACTUAL=$(lab pane get "$PANE" | jq -er '.result.pane.foreground_cwd') + EXPECTED=$(cd "$TEST_SLOT" && pwd -P) + [ "$ACTUAL" = "$EXPECTED" ] || fail "Herdr $VERSION pane cwd $ACTUAL != $EXPECTED" + [ "$DELAY_ENTRY" = 0 ] || [ -e "${TEST_SLOT}.entry-delayed" ] \ + || fail "Herdr $VERSION delayed entry was not exercised" + SENDS=$(wc -l < "${TEST_SLOT}.entry-sends" | tr -d '[:space:]') + [ "$SENDS" = "$((DELAY_ENTRY + 1))" ] \ + || fail "Herdr $VERSION expected $((DELAY_ENTRY + 1)) cd sends, observed $SENDS" + pass "Herdr $VERSION projected spawn enters its recorded copy (delayed=$DELAY_ENTRY)" +done diff --git a/tests/fm-herdr-submit-confirm-live-e2e.test.sh b/tests/fm-herdr-submit-confirm-live-e2e.test.sh index 45f121ff1ab..3ca9bf40525 100755 --- a/tests/fm-herdr-submit-confirm-live-e2e.test.sh +++ b/tests/fm-herdr-submit-confirm-live-e2e.test.sh @@ -5,11 +5,13 @@ # a busy-queued Enter can keep proven pending text visible. A stub cannot prove # either signal. This guard launches real Claude Code in an isolated Herdr lab # and requires fm_backend_herdr_send_text_submit to report empty for a landed -# idle steer. It then requires the same submit path to prove and submit a -# typed /exit slash command behind the command popup Claude renders below the -# composer (the fm-control exit breakage on 2.1.283) and verifies the agent -# actually exited. It fails naming the harness and version rather than -# degrading quietly. +# idle steer. It then requires the same submit path to prove and submit typed +# slash commands behind the command popup Claude renders below the composer +# (the fm-control exit breakage on 2.1.283, and on 2.1.291 the popup's selected +# row carrying a `❯` marker of its own): /context must confirm delivered, +# render its report, and leave the agent alive, and /exit must actually exit +# the agent. It fails naming the harness and version rather than degrading +# quietly. # # Run explicitly with FM_HERDR_SUBMIT_CONFIRM_LIVE=1 after a Herdr or Claude # upgrade, and before trusting a refreshed docs/verification/runtime-backends.md @@ -178,6 +180,46 @@ done || fail "Claude Code ($VERSION) on $HERDR_VER: operational submit reported '$verdict' but the expected reply never rendered" pass "live Herdr submit confirm: Claude Code ($VERSION) on $HERDR_VER submits a U+2063 away-supervisor payload whose read-back drops the mark" +# fm-send routes any text with a leading `/` through this same typed submit, so +# a slash command that does not end the session must also be proven behind the +# popup and confirm delivered. /context is a local command that spends no model +# tokens; its report must render, the agent must stay registered afterwards, +# and its composer must be empty again. Claude Code 2.1.291 marks the popup's +# selected row with a `❯` of its own, which the composer read must not take for +# the typed command. +i=0 +while [ "$i" -lt 45 ]; do + st=$(lab agent get "$PANE" 2>/dev/null | jq -r '.result.agent.agent_status // empty') + case "$st" in idle|done) break ;; esac + i=$((i + 1)) + sleep 1 +done +verdict=$(fm_backend_herdr_send_text_submit "$TARGET" '/context' 3 0.4 1.2) \ + || fail "send_text_submit failed to run the /context submission against Claude Code ($VERSION) on $HERDR_VER" +[ "$verdict" = empty ] \ + || fail "Claude Code ($VERSION) on $HERDR_VER: a typed /context behind its command popup must confirm empty, got '$verdict'" +# Confirm /context ran, not merely that the composer cleared: its report opens +# with a `Context Usage` heading, which the popup's lowercase description does +# not match. The report can run to hundreds of rows, so the read reaches far +# enough back to keep its heading. +rendered=0 +i=0 +while [ "$i" -lt 45 ]; do + screen=$(lab pane read "$PANE" --source recent --lines 1000 2>/dev/null || true) + case "$screen" in + *'Context Usage'*) rendered=1; break ;; + esac + i=$((i + 1)) + sleep 1 +done +[ "$rendered" = 1 ] \ + || fail "Claude Code ($VERSION) on $HERDR_VER: /context reported '$verdict' but its Context Usage report never rendered" +lab agent get "$PANE" >/dev/null 2>&1 \ + || fail "Claude Code ($VERSION) on $HERDR_VER: the agent is no longer registered after /context" +[ "$(fm_backend_herdr_composer_state "$TARGET")" = empty ] \ + || fail "Claude Code ($VERSION) on $HERDR_VER: the composer did not read empty after the /context submission" +pass "live Herdr submit confirm: Claude Code ($VERSION) on $HERDR_VER proves and submits a typed /context behind its command popup and renders its report" + # The fm-control exit regression: a typed slash command (/exit) makes Claude # Code 2.1.283 render its command popup between the composer and the pane # bottom, which pushed the composer above the old bounded proof read - the diff --git a/tests/fm-install-herdr.test.sh b/tests/fm-install-herdr.test.sh new file mode 100755 index 00000000000..7fa3e7b7e0e --- /dev/null +++ b/tests/fm-install-herdr.test.sh @@ -0,0 +1,30 @@ +#!/usr/bin/env bash +# fm-install-herdr.test.sh - the pinned Herdr installer downloads from Herdr's +# current repository. A PATH curl stub records the requested URL and refuses +# the download, so nothing is fetched or installed. +set -eu +# shellcheck source=tests/lib.sh +. "$(dirname "${BASH_SOURCE[0]}")/lib.sh" + +TMP=$(fm_test_tmproot fm-install-herdr) +FAKEBIN=$(fm_fakebin "$TMP") +cat > "$FAKEBIN/curl" <<'SH' +#!/usr/bin/env bash +for arg in "$@"; do + case "$arg" in https://*) printf '%s\n' "$arg" > "$FM_CURL_URL_LOG" ;; esac +done +exit 22 +SH +chmod +x "$FAKEBIN/curl" + +code=0 +FM_CURL_URL_LOG="$TMP/url" PATH="$FAKEBIN:$PATH" RUNNER_TEMP="$TMP" \ + bash "$ROOT/bin/fm-install-herdr.sh" "$TMP/dest" 2>"$TMP/err" || code=$? +[ "$code" -ne 0 ] || fail "installer succeeded although the download was refused" +[ -s "$TMP/url" ] || fail "installer requested no release URL: $(cat "$TMP/err")" +case "$(cat "$TMP/url")" in + https://github.com/herdrdev/herdr/releases/download/v*/herdr-*) ;; + *) fail "installer requested $(cat "$TMP/url"), not a herdrdev/herdr release asset" ;; +esac +[ ! -e "$TMP/dest/herdr" ] || fail "installer installed a binary after a refused download" +pass "installer downloads the pinned asset from herdrdev/herdr" diff --git a/tests/fm-kimi-harness.test.sh b/tests/fm-kimi-harness.test.sh index 8fe1b43fbda..12e8d7724dd 100755 --- a/tests/fm-kimi-harness.test.sh +++ b/tests/fm-kimi-harness.test.sh @@ -139,7 +139,7 @@ case "${1:-}" in ". '"*"'") staged=${literal#". '"}; staged=${staged%"'"}; [ ! -f "$staged" ] || literal=$(cat "$staged") ;; esac case "$literal" in - *' --auto') + *' --auto'|*' --auto'$'\n'*) printf '%s\n' "$literal" >> "$FM_FAKE_LAUNCH_LOG" printf 'launched\n' > "$FM_FAKE_KIMI_STATE" ;; @@ -345,7 +345,7 @@ test_kimi_launch_then_send_is_verified() { expect_code 0 "$rc" "verified kimi launch-then-send should succeed" assert_contains "$out" "spawned $id harness=kimi" "kimi spawn did not report success" - launch=$(cat "$CASE_DIR/launch.log") + launch=$(fm_test_launch_unframe "$(cat "$CASE_DIR/launch.log")") [ "$launch" = "unset FM_TIMEOUT_OWNER_PID FM_EXEC_TIMED_OWNER_PID; export COMPACT_ADVISER_DISABLE=1; $(task_inbox_export "$HOME_DIR" "$id")$(fm_test_launch_git_prefix "$launch")env -u CURSOR_AGENT -u CURSOR_INVOKED_AS -u GEMINI_CLI '$FAKEBIN_DIR/kimi' --model 'kimi-code/k3' --auto" ] \ || fail "kimi launch did not use the absolute binary, model, and --auto only: $launch" assert_not_contains "$launch" "--effort" "kimi launch emitted a nonexistent effort flag" @@ -721,7 +721,7 @@ test_kimi_falls_back_to_expanded_home_binary() { out=$(run_spawn "$CASE_DIR" "$HOME_DIR" "$PROJ_DIR" "$WT_DIR" "$FAKEBIN_DIR" "$id") rc=$? expect_code 0 "$rc" "Kimi HOME fallback spawn should succeed" - launch=$(cat "$CASE_DIR/launch.log") + launch=$(fm_test_launch_unframe "$(cat "$CASE_DIR/launch.log")") [ "$launch" = "unset FM_TIMEOUT_OWNER_PID FM_EXEC_TIMED_OWNER_PID; export COMPACT_ADVISER_DISABLE=1; $(task_inbox_export "$HOME_DIR" "$id")$(fm_test_launch_git_prefix "$launch")env -u CURSOR_AGENT -u CURSOR_INVOKED_AS -u GEMINI_CLI '$fallback' --auto" ] \ || fail "Kimi fallback did not expand HOME into an absolute executable: $launch" pass "fm-spawn: Kimi fallback expands the active HOME" diff --git a/tests/fm-lint.test.sh b/tests/fm-lint.test.sh index bc1932adc04..ab50246d39d 100755 --- a/tests/fm-lint.test.sh +++ b/tests/fm-lint.test.sh @@ -181,8 +181,9 @@ test_list_files_reports_the_shell_inventory() { # CI=true forces the full canonical set regardless of the ambient branch or # working-tree diff a local test run happens to have, so this stays a pure # inventory check independent of fm-lint.sh's own changed-file mode below. + # Hidden scripts are outside the documented shell-glob inventory. listed=$(CI=true "$LINT" --list-files) - expected=$(find bin bin/backends tests -maxdepth 1 -type f -name '*.sh' -print | LC_ALL=C sort) + expected=$(find bin bin/backends tests -maxdepth 1 -type f -name '*.sh' ! -name '.*' -print | LC_ALL=C sort) [ "$(printf '%s\n' "$listed" | LC_ALL=C sort)" = "$expected" ] \ || fail "fm-lint.sh --list-files did not return the complete shell inventory" pass "fm-lint.sh --list-files reports the complete shell inventory" @@ -539,7 +540,7 @@ test_ci_forces_full_lint_even_with_empty_diff() { # No git stub: CI=true must short-circuit fm-lint.sh's mode selection before # it ever consults git, so this proves CI wins regardless of local diff state. listed=$(CI=true "$LINT" --list-files) - expected=$(find bin bin/backends tests -maxdepth 1 -type f -name '*.sh' -print | LC_ALL=C sort) + expected=$(find bin bin/backends tests -maxdepth 1 -type f -name '*.sh' ! -name '.*' -print | LC_ALL=C sort) [ "$(printf '%s\n' "$listed" | LC_ALL=C sort)" = "$expected" ] \ || fail "CI=true did not force the full canonical file set" pass "fm-lint.sh forces a full lint in CI even when the local diff would be empty" @@ -555,7 +556,7 @@ test_main_branch_forces_full_lint() { # not the ambient CI signal a real CI run would otherwise supply. listed=$(PATH="$fakebin:$PATH" GITHUB_ACTIONS='' CI='' \ FM_TEST_GIT_BRANCH=main "$LINT" --list-files) - expected=$(find bin bin/backends tests -maxdepth 1 -type f -name '*.sh' -print | LC_ALL=C sort) + expected=$(find bin bin/backends tests -maxdepth 1 -type f -name '*.sh' ! -name '.*' -print | LC_ALL=C sort) [ "$(printf '%s\n' "$listed" | LC_ALL=C sort)" = "$expected" ] \ || fail "fm-lint.sh did not force a full lint when HEAD is on main" pass "fm-lint.sh forces a full lint when HEAD is on main" diff --git a/tests/fm-live-lab-isolation.test.sh b/tests/fm-live-lab-isolation.test.sh new file mode 100755 index 00000000000..3c001d626be --- /dev/null +++ b/tests/fm-live-lab-isolation.test.sh @@ -0,0 +1,299 @@ +#!/usr/bin/env bash +# Behavior tests for the tmux isolation the live lab depends on: +# fm_tmux_isolation_gate (tests/lib.sh) and bin/fm-live-lab.sh's explicit lab +# socket. tmux resolves its socket under TMUX_TMPDIR only while that directory +# exists and otherwise silently falls back to /tmp/tmux-, so a lab command +# aimed at a removed lab directory would reach whatever server lives there. +# Here a decoy server stands on that default socket, and every lab command +# aimed at a missing lab directory must leave it alone. +# +# The decoy never runs on the host /tmp: the suite re-executes itself under +# `unshare -Urm` with a fresh tmpfs on /tmp, confirms there that /tmp is not the +# host one, and skips the decoy cases where no such namespace is available or +# the checkout itself lives under /tmp, where that mount would hide it. +set -u + +if [ -n "${FM_LAB_ISO_HOST_MARKER:-}" ]; then + if [ -e "$FM_LAB_ISO_HOST_MARKER" ] || [ -n "$(ls -A /tmp)" ]; then + echo "not ok - /tmp is not a fresh private mount; refusing to start the decoy" >&2 + exit 1 + fi +fi + +# shellcheck source=tests/fixtures.sh +. "$(dirname "${BASH_SOURCE[0]}")/fixtures.sh" +# shellcheck source=bin/fm-timeout-lib.sh +. "$ROOT/bin/fm-timeout-lib.sh" + +command -v tmux >/dev/null 2>&1 || { echo "skip: tmux not found"; exit 0; } +unset TMUX TMUX_TMPDIR + +TMP_ROOT=$(fm_test_tmproot fm-lab-iso) +LIVE_LAB="$ROOT/bin/fm-live-lab.sh" +GATE_SOCKET="$TMP_ROOT/base/tmux-$(id -u)/default" +DEFAULT_SOCKET="/tmp/tmux-$(id -u)/default" +STUCK_SOCKET="$TMP_ROOT/stuck/tmux-$(id -u)/default" +DECOY_PID='' +STUCK_PID='' + +# Poll steps (0.05s each) a process gets to exit after each escalation step. +DECOY_POLLS=100 + +# signal_decoy : one seam, so a case can stand in an unkillable process. +signal_decoy() { + kill -"$1" "$2" 2>/dev/null +} + +# pid_running : succeeds while the process exists and has not exited. +# An exited but unreaped process (a zombie, state Z) still answers kill -0 +# when the runner's PID 1 does not reap orphans, so it counts as gone. A ps +# that errors fails closed: the process counts as still running. +pid_running() { + local state + kill -0 "$1" 2>/dev/null || return 1 + state=$(ps -o stat= -p "$1" 2>/dev/null) || return 0 + case $state in + Z*) return 1 ;; + esac +} + +# wait_gone : succeeds once the process is gone, after at most polls. +wait_gone() { + local pid=$1 polls=$2 + while pid_running "$pid"; do + [ "$polls" -gt 0 ] || return 1 + polls=$((polls - 1)) + sleep 0.05 + done +} + +# stop_decoy [polls]: stop the decoy server recorded at start. +# kill-server on its own explicit socket first, then TERM, then KILL on the +# recorded pid, each step bounded (kill-server too, against a wedged server). +# A decoy that survives all three is reported and fails the call instead of +# stalling the suite on its pane's 600s sleep. +stop_decoy() { + local pid=$1 socket=$2 polls=${3:-$DECOY_POLLS} + [ ! -S "$socket" ] || fm_run_timed 5 tmux -S "$socket" kill-server 2>/dev/null + wait_gone "$pid" "$polls" && return 0 + signal_decoy TERM "$pid" + wait_gone "$pid" "$polls" && return 0 + signal_decoy KILL "$pid" + wait_gone "$pid" "$polls" && return 0 + echo "not ok - cleanup: decoy server $pid survived kill-server, TERM and KILL" >&2 + return 1 +} + +isolation_cleanup() { + # Only the servers this suite started, each by its own explicit socket, and + # only while it is still there; the decoy is gone before the suite returns. + local rc=0 + [ ! -S "$GATE_SOCKET" ] || tmux -S "$GATE_SOCKET" kill-server 2>/dev/null + if [ -n "$STUCK_PID" ]; then + kill -CONT "$STUCK_PID" 2>/dev/null + stop_decoy "$STUCK_PID" "$STUCK_SOCKET" || rc=1 + fi + if [ -n "$DECOY_PID" ]; then + stop_decoy "$DECOY_PID" "$DEFAULT_SOCKET" || rc=1 + fi + fm_test_cleanup + [ "$rc" -eq 0 ] || exit 1 +} +trap isolation_cleanup EXIT + +# gate_run [NAME=VALUE...]: the gate's output and exit, in a subshell, +# followed by RAN when the gate let the script continue. +gate_run() { + local base=$1 + shift + ( + for assignment in "$@"; do export "${assignment?}"; done + fm_tmux_isolation_gate "$base" + echo RAN + ) 2>&1 +} + +# ---- the gate --------------------------------------------------------------- + +# FM_LAB_ISO_TMP_COPY marks the copy of this suite under /tmp (below), which +# needs only the decoy preflight. +if [ -z "${FM_LAB_ISO_HOST_MARKER:-}" ] && [ -z "${FM_LAB_ISO_TMP_COPY:-}" ]; then + out=$(gate_run "$TMP_ROOT/base") + expect_code 0 "$?" "the gate passes with no socket directory: $out" + assert_equals RAN "$out" "the gate runs the suite where no tmux server can answer" + + mkdir -p "$TMP_ROOT/base" + mkdir -m 700 "$TMP_ROOT/base/tmux-$(id -u)" + tmux -S "$GATE_SOCKET" -f /dev/null new-session -d -s live 'exec sleep 600' || fail "cannot start the gate's own server" + out=$(gate_run "$TMP_ROOT/base") + expect_code 0 "$?" "the gate skips cleanly beside a live server: $out" + assert_contains "$(printf '%s\n' "$out" | head -n 1)" "skip: tmux isolation: a live tmux server answers on $GATE_SOCKET" "the skip is the first line and names the live socket" + assert_contains "$out" "unshare -Urm" "the skip names the isolation it needs" + assert_not_contains "$out" RAN "the gate stops the suite beside a live server" + + out=$(gate_run "$TMP_ROOT/base" CI=true) + expect_code 0 "$?" "under CI the gate skips the same way: $out" + assert_contains "$out" "skip: tmux isolation" "under CI the gate still says why it skips" + assert_not_contains "$out" RAN "under CI the gate stops the suite beside a live server" + + tmux -S "$GATE_SOCKET" kill-server 2>/dev/null + out=$(gate_run "$TMP_ROOT/base") + assert_equals RAN "$out" "the gate runs again once only a dead socket is left" + pass "fm_tmux_isolation_gate runs isolated and skips beside a live server" + + # A server that accepts the connection but never answers (stopped here) must + # not hold the gate: it gives up at its probe deadline and skips. + mkdir -p "$TMP_ROOT/stuck" + mkdir -m 700 "$TMP_ROOT/stuck/tmux-$(id -u)" + tmux -S "$STUCK_SOCKET" -f /dev/null new-session -d -s stuck 'exec sleep 600' || fail "cannot start the stuck server" + STUCK_PID=$(tmux -S "$STUCK_SOCKET" display-message -p '#{pid}') + [ -n "$STUCK_PID" ] || fail "the stuck server has no pid" + kill -STOP "$STUCK_PID" || fail "cannot stop the stuck server" + start=$(date +%s) + # shellcheck disable=SC2016 # $1 and $2 expand in the bounded shell. + out=$(FM_TMUX_ISOLATION_PROBE_SECONDS=1 fm_run_timed 30 bash -c '. "$1/tests/lib.sh"; fm_tmux_isolation_gate "$2"; echo RAN' _ "$ROOT" "$TMP_ROOT/stuck" 2>&1) + code=$? + kill -CONT "$STUCK_PID" 2>/dev/null + stop_decoy "$STUCK_PID" "$STUCK_SOCKET" || fail "cannot stop the stuck server" + STUCK_PID='' + expect_code 0 "$code" "the gate returns from a server that never answers: $out" + assert_contains "$out" "skip: tmux isolation: the tmux server on $STUCK_SOCKET did not answer within 1s" "the skip names the silent socket and the deadline" + assert_not_contains "$out" RAN "the gate stops the suite beside a server that never answers" + [ $(($(date +%s) - start)) -lt 10 ] || fail "the gate waited past its probe deadline" + pass "fm_tmux_isolation_gate skips at its deadline when a tmux server does not answer" + + # ---- the decoy cleanup is bounded ------------------------------------------ + + # No socket for kill-server to reach: TERM stops a plain process. + victim=$(bash -c 'sleep 600 /dev/null 2>&1 & echo $!') + start=$(date +%s) + stop_decoy "$victim" "$TMP_ROOT/no-such-socket" 4 || fail "cleanup could not stop a process that exits on TERM" + wait_gone "$victim" 0 || fail "cleanup returned success with the process still alive" + [ $(($(date +%s) - start)) -lt 5 ] || fail "cleanup of a TERM-able process was not prompt" + + # A process that ignores TERM is stopped by the KILL step. + victim=$(bash -c 'trap "" TERM; exec sleep 600 /dev/null 2>&1 & echo $!') + stop_decoy "$victim" "$TMP_ROOT/no-such-socket" 4 || fail "cleanup could not stop a process that ignores TERM" + wait_gone "$victim" 0 || fail "cleanup returned success with the TERM-ignoring process alive" + + # kill-server cannot stop it and neither signal lands: cleanup must return + # within its poll budget, name the survivor, and report failure. + victim=$(bash -c 'sleep 600 /dev/null 2>&1 & echo $!') + start=$(date +%s) + out=$(signal_decoy() { :; }; stop_decoy "$victim" "$TMP_ROOT/no-such-socket" 3 2>&1) + code=$? + kill -KILL "$victim" 2>/dev/null + expect_code 1 "$code" "cleanup reports a decoy it cannot stop: $out" + assert_contains "$out" "decoy server $victim survived" "the failure names the surviving decoy" + [ $(($(date +%s) - start)) -lt 5 ] || fail "cleanup of an unstoppable decoy waited past its bound" + # An exited decoy its parent never reaps stays a zombie that still answers + # kill -0: cleanup must count it as gone rather than report a failure. + bash -c 'sleep 0.2 /dev/null 2>&1 & echo $! $$; exec sleep 30 &3 2>/dev/null' 3>/dev/null >"$TMP_ROOT/zombie.pids" & + until [ -s "$TMP_ROOT/zombie.pids" ]; do sleep 0.05; done + read -r victim zombie_parent <"$TMP_ROOT/zombie.pids" + sleep 0.5 + kill -0 "$victim" 2>/dev/null || fail "the zombie fixture was reaped, so the case would be vacuous" + stop_decoy "$victim" "$TMP_ROOT/no-such-socket" 3 || fail "cleanup reported failure for an exited, unreaped decoy" + { kill -KILL "$zombie_parent"; wait "$zombie_parent"; } 2>/dev/null + + pass "decoy cleanup is bounded, escalates to TERM and KILL, and reports a decoy it cannot stop" +fi + +if [ -z "${FM_LAB_ISO_HOST_MARKER:-}" ]; then + # The decoy cases run only in a private mount namespace with a fresh /tmp. + if ! unshare -Urm sh -c 'mount -t tmpfs tmpfs /tmp' >/dev/null 2>&1; then + echo "skip: tmux isolation decoy: needs a private /tmp (unshare -Urm with a tmpfs mount on /tmp)" + exit 0 + fi + # bash reopens this script after the tmpfs is mounted on /tmp, so a checkout + # under /tmp would be hidden by the very mount that isolates the decoy. + suite=$(cd -P -- "$ROOT/tests" && pwd -P)/fm-live-lab-isolation.test.sh + case $suite in + "$(cd -P /tmp && pwd -P)"/*) + echo "skip: tmux isolation decoy: the checkout is under /tmp ($suite), which the private tmpfs on /tmp would hide" + exit 0 + ;; + esac + # A checkout under /tmp skips the decoy cases rather than failing on a script + # the tmpfs mount has hidden. The copy holds the suite and what it sources; + # its bin links back here. + if [ -z "${FM_LAB_ISO_TMP_COPY:-}" ]; then + copy=$(mktemp -d /tmp/fm-lab-iso-copy.XXXXXX) || fail "cannot make a checkout copy under /tmp" + FM_TEST_CLEANUP_DIRS+=("$copy") + mkdir -p "$copy/tests" + ln -s "$ROOT/bin" "$copy/bin" || fail "cannot link the copy's bin" + cp "$ROOT/tests/lib.sh" "$ROOT/tests/fixtures.sh" "$ROOT/tests/git-config-helpers.sh" \ + "$ROOT/tests/fm-live-lab-isolation.test.sh" "$copy/tests/" || fail "cannot copy the suite" + out=$(FM_LAB_ISO_TMP_COPY=1 fm_run_timed 120 bash "$copy/tests/fm-live-lab-isolation.test.sh" 2>&1) + expect_code 0 "$?" "a checkout under /tmp ends cleanly: $out" + assert_contains "$out" "skip: tmux isolation decoy: the checkout is under /tmp" "a checkout under /tmp says why it skips the decoy cases" + pass "a checkout under /tmp skips the decoy cases instead of losing its script to the mount" + fi + + host_marker=$(mktemp /tmp/fm-lab-iso-host.XXXXXX) || fail "cannot mark the host /tmp" + FM_TEST_CLEANUP_DIRS+=("$host_marker") + # shellcheck disable=SC2016 # $0 expands in the namespaced shell. + FM_LAB_ISO_HOST_MARKER=$host_marker unshare -Urm bash -c 'mount -t tmpfs tmpfs /tmp && exec env -u TMPDIR -u TMP -u TMUX_TMPDIR bash "$0"' "$suite" + exit $? +fi + +# ---- a lab aimed at a missing tmux directory -------------------------------- + +# /tmp is a fresh private tmpfs, so the decoy is a server of this suite's own +# that no one outside this namespace can reach. +env -u TMUX -u TMUX_TMPDIR tmux -f /dev/null new-session -d -s firstmate -n main \ + "printf 'DECOY-PANE\n'; exec sleep 600" || fail "cannot start the decoy default server" +DECOY_PID=$(tmux -S "$DEFAULT_SOCKET" display-message -p '#{pid}') +[ -n "$DECOY_PID" ] || fail "the decoy is not on the default socket" + +# shellcheck disable=SC2119 # No bases: the gate checks the real default ones. +out=$( (fm_tmux_isolation_gate; echo RAN) 2>&1) +assert_contains "$out" "skip: tmux isolation: a live tmux server answers on $DEFAULT_SOCKET" "the gate's default bases see a server on the default socket" +assert_not_contains "$out" RAN "the gate stops a suite beside the default server" + +HOME="$TMP_ROOT/home" +mkdir -p "$HOME/.pi/agent" "$HOME/.treehouse" +printf '{}\n' > "$HOME/.pi/agent/trust.json" +printf '{"projects":{}}\n' > "$HOME/.claude.json" +LAB_ROOT="$TMP_ROOT/lab" +MISSING_DIR=$(mktemp -d /tmp/fml.XXXXXX) || fail "cannot name a missing lab tmux directory" +rmdir "$MISSING_DIR" || fail "cannot remove the lab tmux directory" +mkdir -p "$LAB_ROOT/home/state" +: > "$LAB_ROOT/.treehouse-before" +{ + echo 'fm-live-lab v1' + echo "harness=claude" + echo "home=$LAB_ROOT/home" + echo "expect_host=no" + echo "host_off=no" + echo "mate=no" + echo "worker=no" + echo "nonce=deadbeef0000" + echo "mate_id=labdeadbeef0000-mate" + echo "worker_id=labdeadbeef0000-worker" + echo "pi_trust=$(shasum -a 256 "$HOME/.pi/agent/trust.json" | awk '{print $1}')" + echo "claude_config_dir=" + echo "claude_store=$HOME/.claude.json" + echo "pi_trust_store=$HOME/.pi/agent/trust.json" + echo "treehouse_dir=$HOME/.treehouse" + echo "tmux_dir=$MISSING_DIR" +} > "$LAB_ROOT/.fm-live-lab" + +# The divergence this suite exists for: the TMUX_TMPDIR-only form reaches the +# decoy once the lab directory is gone. +env -u TMUX TMUX_TMPDIR="$MISSING_DIR" tmux has-session -t firstmate 2>/dev/null \ + || fail "this tmux no longer falls back to the default socket; the decoy cases would prove nothing" + +out=$("$LIVE_LAB" pane "$LAB_ROOT" 2>&1) +assert_not_contains "$out" DECOY-PANE "pane never reads the default server's window" +"$LIVE_LAB" say "$LAB_ROOT" "LAB-TYPED-TEXT" >/dev/null 2>&1 +sleep 0.2 +assert_not_contains "$(tmux -S "$DEFAULT_SOCKET" capture-pane -p -t firstmate:=main)" LAB-TYPED-TEXT "say never types into the default server" +out=$("$LIVE_LAB" check "$LAB_ROOT" 2>&1) +assert_not_contains "$out" "ok primary" "check never reads the default server as the lab" +out=$("$LIVE_LAB" down "$LAB_ROOT" 2>&1) +expect_code 0 "$?" "down cleans a lab whose tmux directory is gone: $out" +assert_absent "$LAB_ROOT" "down removed the lab root" +kill -0 "$DECOY_PID" 2>/dev/null || fail "down killed the default tmux server" +tmux -S "$DEFAULT_SOCKET" has-session -t firstmate 2>/dev/null || fail "the default server lost its session" +pass "a lab aimed at a missing tmux directory never reaches the default tmux server" diff --git a/tests/fm-live-lab-up-mate.test.sh b/tests/fm-live-lab-up-mate.test.sh index 016b79b8cf5..040a667952e 100644 --- a/tests/fm-live-lab-up-mate.test.sh +++ b/tests/fm-live-lab-up-mate.test.sh @@ -5,6 +5,8 @@ set -u . "$(dirname "${BASH_SOURCE[0]}")/fixtures.sh" command -v tmux >/dev/null 2>&1 || { echo 'ok - skipped: tmux is not installed'; exit 0; } +# shellcheck disable=SC2119 # No bases: the gate checks the real default ones. +fm_tmux_isolation_gate TMP_ROOT=$(fm_test_tmproot fm-live-up-mate) export HOME="$TMP_ROOT/user" mkdir -p "$HOME/.pi/agent" "$HOME/.treehouse" "$TMP_ROOT/source/bin" "$TMP_ROOT/fakebin" @@ -25,8 +27,11 @@ fi SH cat > "$TMP_ROOT/source/bin/fm-spawn.sh" <<'SH' #!/usr/bin/env bash -tmux new-window -d -t firstmate: -n "fm-$1" -c "$FM_HOME/../mate" 'exec sleep 45' || exit 1 -pid=$(tmux display-message -p -t "firstmate:=fm-$1" '#{pane_pid}') +# The lab's explicit socket, never TMUX_TMPDIR alone, which falls back to the +# user's default server when its directory is missing. +lab_tmux() { tmux -S "${TMUX_TMPDIR:?}/tmux-$(id -u)/default" "$@"; } +lab_tmux new-window -d -t firstmate: -n "fm-$1" -c "$FM_HOME/../mate" 'exec sleep 45' || exit 1 +pid=$(lab_tmux display-message -p -t "firstmate:=fm-$1" '#{pane_pid}') printf '%s\n' "$pid" > "$FM_HOME/../mate/state/.lock" printf 'window=firstmate:fm-%s\n' "$1" > "$FM_HOME/state/$1.meta" SH diff --git a/tests/fm-live-lab.test.sh b/tests/fm-live-lab.test.sh index 7186d19e754..cfb22608b6a 100755 --- a/tests/fm-live-lab.test.sh +++ b/tests/fm-live-lab.test.sh @@ -17,6 +17,9 @@ TMP_ROOT=$(fm_test_tmproot fm-live-lab) LIVE_LAB="$ROOT/bin/fm-live-lab.sh" TRUST="$ROOT/bin/fm-claude-trust.sh" +# lab_socket_of : the explicit socket a lab's TMUX_TMPDIR names. +lab_socket_of() { printf '%s/tmux-%s/default\n' "$1" "$(id -u)"; } + live_lab_cleanup() { local dir pid marker for marker in "$TMP_ROOT/orphan-child" "$TMP_ROOT/late-child" "$TMP_ROOT/reused-child"; do @@ -26,7 +29,9 @@ live_lab_cleanup() { while read -r dir; do [ -n "$dir" ] || continue [ -d "$dir" ] || continue - env -u TMUX TMUX_TMPDIR="$dir" tmux kill-server 2>/dev/null + # Only the explicit lab socket, and only while it exists: a missing + # TMUX_TMPDIR would make tmux fall back to the user's default server. + [ ! -S "$(lab_socket_of "$dir")" ] || env -u TMUX tmux -S "$(lab_socket_of "$dir")" kill-server 2>/dev/null case "$dir" in /tmp/fml.*) rm -rf "$dir" ;; esac done < "$TMP_ROOT/tmux-dirs" rm -rf "/tmp/fm-labt$$-mate" "/tmp/fm-labt$$-worker" "/tmp/fm-labt$$-other" /tmp/fm-labt"$$"-*+* @@ -35,6 +40,8 @@ live_lab_cleanup() { trap live_lab_cleanup EXIT command -v tmux >/dev/null 2>&1 || { echo "ok - skipped: tmux is not installed"; exit 0; } +# shellcheck disable=SC2119 # No bases: the gate checks the real default ones. +fm_tmux_isolation_gate FAKE_HOME="$TMP_ROOT/fakehome" mkdir -p "$FAKE_HOME/.pi/agent" "$FAKE_HOME/.treehouse/existing-pool" @@ -65,6 +72,7 @@ make_lab() { git -C "$home" -c user.name=t -c user.email=t@example.invalid commit -qm lab tmux_dir=$("$ROOT/bin/fm-lab-home.sh" tmux-dir "$home") || fail "lab tmux dir" printf '%s\n' "$tmux_dir" >> "$TMP_ROOT/tmux-dirs" + mkdir -m 700 "$tmux_dir/tmux-$(id -u)" || fail "lab tmux socket dir" find "$HOME/.treehouse" -mindepth 1 -maxdepth 1 -exec basename {} \; | sort > "$root/.treehouse-before" { echo 'fm-live-lab v1' @@ -121,7 +129,8 @@ lab_tmux() { # local dir dir=$(sed -n 's/^tmux_dir=//p' "$1/.fm-live-lab") shift - env -u TMUX TMUX_TMPDIR="$dir" tmux "$@" + [ -n "$dir" ] || return 1 + env -u TMUX tmux -S "$(lab_socket_of "$dir")" "$@" } start_sleeper() { @@ -638,11 +647,14 @@ SH cat > "$WORKSRC/bin/fm-spawn.sh" <<'SH' #!/usr/bin/env bash id=$1 -tmux new-window -d -t firstmate: -n "fm-$id" -c "$FM_HOME" 'exec sleep 45 >/dev/null 2>&1' || exit 1 +# The lab's explicit socket, never TMUX_TMPDIR alone, which falls back to the +# user's default server when its directory is missing. +lab_tmux() { tmux -S "${TMUX_TMPDIR:?}/tmux-$(id -u)/default" "$@"; } +lab_tmux new-window -d -t firstmate: -n "fm-$id" -c "$FM_HOME" 'exec sleep 45 >/dev/null 2>&1' || exit 1 # A missing =name can silently resolve to the current window: verify the name. for (( n=0; n<30; n++ )); do - if tmux list-windows -t firstmate -F '#{window_name}' | grep -Fxq "fm-$id"; then - pid=$(tmux display-message -p -t "firstmate:=fm-$id" '#{pane_pid}') + if lab_tmux list-windows -t firstmate -F '#{window_name}' | grep -Fxq "fm-$id"; then + pid=$(lab_tmux display-message -p -t "firstmate:=fm-$id" '#{pane_pid}') [ -z "$pid" ] || break fi sleep 0.1 diff --git a/tests/fm-osc7501.test.sh b/tests/fm-osc7501.test.sh new file mode 100755 index 00000000000..3cc9e9dfff3 --- /dev/null +++ b/tests/fm-osc7501.test.sh @@ -0,0 +1,1214 @@ +#!/usr/bin/env bash +# tests/fm-osc7501.test.sh - behavioral tests for the worker-side OSC 7501 +# reporter (bin/fm-osc7501.sh). Every assertion is on observable terminal +# behavior: the bytes a real terminal would receive across a pty, the +# presence or absence of the watcher process, and the records emitted as +# firstmate's durable signals (status file, busy-state, inbox) change. The +# pty fake-terminal captures output without sending capability replies. +set -u + +# shellcheck source=tests/lib.sh +. "$(dirname "${BASH_SOURCE[0]}")/lib.sh" + +command -v python3 >/dev/null 2>&1 || { echo "skip: python3 not found (pty fake-terminal)"; exit 0; } + +TMP_ROOT=$(fm_test_tmproot fm-osc7501-tests) +SCRIPT=$ROOT/bin/fm-osc7501.sh +DRIVER=$TMP_ROOT/fake-term.py + +# fake-term.py [cmd...] - a terminal emulator +# on a pty: writes the slave path and captures output. A command runs as the +# pane's shell: its own session with the pty as controlling terminal, so its +# /dev/tty is this terminal. Runs until killed. +cat >"$DRIVER" <<'PY' +import fcntl, os, pty, select, sys, termios, time + +cap_path, slave_path = sys.argv[1:3] +master, slave = pty.openpty() +with open(slave_path, "w") as f: + f.write(os.ttyname(slave)) +if len(sys.argv) > 3 and os.fork() == 0: + os.close(master) + os.setsid() + fcntl.ioctl(slave, termios.TIOCSCTTY, 0) + for fd in (0, 1, 2): + os.dup2(slave, fd) + os.execvp(sys.argv[3], sys.argv[3:]) +cap = open(cap_path, "ab", buffering=0) +while True: + r, _, _ = select.select([master], [], [], 0.1) + if not r: + continue + try: + data = os.read(master, 65536) + except OSError: + break + if not data: + break + cap.write(data) +PY + +# start_term [cmd...] -> sets TERM_SLAVE + TERM_CAP, leaves the +# emulator running. TERM_PID kills it; run from a test's subshell scope. +start_term() { # [cmd...] + local dir=$1 + shift + : >"$dir/cap" + python3 "$DRIVER" "$dir/cap" "$dir/slave" "$@" & + TERM_PID=$! + local i=0 + while [ ! -s "$dir/slave" ] && [ "$i" -lt 50 ]; do sleep 0.1; i=$((i + 1)); done + [ -s "$dir/slave" ] || fail "fake terminal did not create the pty" + TERM_SLAVE=$(cat "$dir/slave") +} +stop_term() { kill "$TERM_PID" 2>/dev/null; wait "$TERM_PID" 2>/dev/null || true; } + +# Strip every env var that could make the pane look known-aware or +# known-unaware, so the tests below control detection explicitly. Callers run +# `env "${CLEAN_UNSET[@]}" ...` directly so a backgrounded watcher's $! is the +# watcher itself. +CLEAN_UNSET=(-u HC_TERMINAL_ID -u HC_TERMINAL_SOCKET -u HC_SCOPE -u HC_SESSION + -u HC_SOCKET -u HERDR_ENV -u HERDR_PANE_ID -u HERDR_SESSION + -u HERDR_SOCKET_PATH -u HERDR_TAB_ID -u HERDR_WORKSPACE_ID + -u REX_BLOCK -u REX_SESSION -u TERM_PROGRAM) + +mkhome() { # : a minimal config/state pair + mkdir -p "$1/config" "$1/state" +} + +busy_rec() { # [ts]: one armed busy-state record + printf 'g1\n' >"$1/$2.busy-gen" + printf 'v1 gen=g1 seq=1 state=%s source=fm-spawn event=test ts=%s\n' \ + "$3" "${4:-$(date +%s)}" >"$1/$2.busy-state" +} + +wait_cap() { # : fixed string appears + local needle=$1 cap=$2 tries=${3:-40} i=0 + while ! grep -F -- "$needle" "$cap" >/dev/null 2>&1; do + [ "$i" -ge "$tries" ] && return 1 + sleep 0.1; i=$((i + 1)) + done +} + +wait_last_state() { # : the newest emitted record is X + local want=$1 cap=$2 tries=${3:-40} i=0 + while [ "$(grep -o 'state=[a-z]*' "$cap" 2>/dev/null | tail -1)" != "$want" ]; do + [ "$i" -ge "$tries" ] && return 1 + sleep 0.1; i=$((i + 1)) + done +} + +# --- passive terminal eligibility ------------------------------------------- + +# Contract: unknown terminals get no bytes, no watcher, and no input reads. +# Queue actual worker input before arm; the next consumer must receive it +# unchanged. This rejects the former capability probe consuming typed input. +t_unknown_terminal_preserves_input() { + local d=$TMP_ROOT/unaware + mkhome "$d" + if ! env "${CLEAN_UNSET[@]}" python3 - "$SCRIPT" "$d" <<'PYTEST' +import os, pty, select, subprocess, sys, termios +script, home = sys.argv[1:] +master, slave = pty.openpty() +mode = termios.tcgetattr(slave) +mode[3] &= ~termios.ECHO +termios.tcsetattr(slave, termios.TCSANOW, mode) +typed = b"worker input must survive startup\n" +os.write(master, typed) +env = dict(os.environ, FM_OSC7501_TTY=os.ttyname(slave)) +subprocess.run(["bash", script, "arm", "--task", "t1", "--harness", "opencode", "--agent-bin", "opencode", + "--backend", "tmux", "--kind", "ship", "--state", home + "/state", + "--config", home + "/config"], env=env, check=True, timeout=5) +assert termios.tcgetattr(slave) == mode, "arm changed terminal modes" +assert not select.select([master], [], [], 0.3)[0], "unknown terminal received output" +assert select.select([slave], [], [], 0.3)[0], "arm consumed queued worker input" +assert os.read(slave, 4096) == typed, "arm corrupted queued worker input" +os.close(slave) +os.close(master) +PYTEST + then + fail "unknown terminal: output or input contract violated" + fi + [ -z "$(ls "$d/state"/.osc7501watch-* 2>/dev/null)" ] || fail "unaware terminal spawned a watcher" + pass "unknown terminal: no output or watcher, queued worker input untouched" +} + +# A passive Ghostty marker arms the watcher, which reports the worker's +# state derived from the busy-state signal without a terminal query. +t_ghostty_arms_watcher() { + local d=$TMP_ROOT/aware + mkhome "$d" + start_term "$d" + env "${CLEAN_UNSET[@]}" TERM_PROGRAM=ghostty FM_OSC7501_TTY="$TERM_SLAVE" bash "$SCRIPT" arm \ + --task t2 --harness opencode --agent-bin opencode --backend tmux --kind ship \ + --state "$d/state" --config "$d/config" + assert_present "$(echo "$d/state"/.osc7501watch-* | head -1)" "aware: watcher pidfile recorded" + busy_rec "$d/state" t2 busy + wait_cap 'state=working' "$d/cap" 60 || fail "aware: no working record emitted" + grep -F -- 'state=working:app=firstmate-opencode' "$d/cap" >/dev/null \ + || fail "aware: working record missing app=firstmate-opencode" + wait_cap "$(printf 'title=%s' "$(printf 'fm-t2' | base64 | tr -d '\n')")" "$d/cap" 10 \ + || fail "aware: working record missing title=fm-t2" + stop_term + pass "passive detection: Ghostty arms watcher emitting working" +} + +# --- emission / state mapping ------------------------------------------------ + +# The durable status line drives the record: needs-decision -> blocked +# kind=question, then done survives worker exit as the final record. +t_status_transitions_and_final_done() { + local d=$TMP_ROOT/trans + mkhome "$d" + start_term "$d" + local holder + sleep 300 & + holder=$! + busy_rec "$d/state" t3 idle + env "${CLEAN_UNSET[@]}" FM_OSC7501_TTY="$TERM_SLAVE" FM_OSC7501_INTERVAL=1 \ + bash "$SCRIPT" watch --task t3 --kind ship --state "$d/state" --harness opencode \ + --parent "$holder" & + local wpid=$! + printf 'needs-decision [at=1]: ship it?\n' >"$d/state/t3.status" + wait_cap 'state=blocked:kind=question' "$d/cap" 60 \ + || fail "transition: needs-decision did not emit blocked kind=question" + printf 'done [at=2]: PR https://example.test/pr/1\n' >"$d/state/t3.status" + wait_cap 'state=done' "$d/cap" 60 || fail "transition: done did not emit state=done" + kill "$holder" 2>/dev/null; wait "$holder" 2>/dev/null || true + local i=0 + while kill -0 "$wpid" 2>/dev/null && [ "$i" -lt 60 ]; do sleep 0.1; i=$((i + 1)); done + kill -0 "$wpid" 2>/dev/null && fail "watcher did not exit after parent death" + wait "$wpid" 2>/dev/null || true + local tail + tail=$(tail -c 4000 "$d/cap" | grep -o 'state=[a-z]*' | tail -1) + assert_equals "state=done" "$tail" "final record keeps the done result" + stop_term + pass "status transitions: blocked->done, final record survives parent death" +} + +# A worker that dies mid-task leaves no working/blocked ghost: the watcher's +# final record parks the pane at idle. +t_final_idle_after_working() { + local d=$TMP_ROOT/park + mkhome "$d" + start_term "$d" + sleep 300 & + local holder=$! + busy_rec "$d/state" t4 busy + env "${CLEAN_UNSET[@]}" FM_OSC7501_TTY="$TERM_SLAVE" FM_OSC7501_INTERVAL=1 \ + bash "$SCRIPT" watch --task t4 --kind ship --state "$d/state" --harness opencode \ + --parent "$holder" & + local wpid=$! + wait_cap 'state=working' "$d/cap" 60 || fail "park: no working record" + kill "$holder" 2>/dev/null; wait "$holder" 2>/dev/null || true + local i=0 + while kill -0 "$wpid" 2>/dev/null && [ "$i" -lt 60 ]; do sleep 0.1; i=$((i + 1)); done + wait "$wpid" 2>/dev/null || true + local tail + tail=$(tail -c 4000 "$d/cap" | grep -o 'state=[a-z]*' | tail -1) + assert_equals "state=idle" "$tail" "worker exit parks the record at idle" + stop_term + pass "cleanup: worker exit emits a final idle, no stale working ghost" +} + +# --- native reporting gate ---------------------------------------------------- + +mkfake() { # + mkdir -p "$1/bin" + printf '#!/usr/bin/env bash\necho "%s"\n' "$3" >"$1/bin/$2" + chmod +x "$1/bin/$2" +} + +# A claude new enough to report natively must not trigger the generic +# reporter - not even the capability probe reaches the terminal. +t_native_claude_skips_everything() { + local d=$TMP_ROOT/native + mkhome "$d"; mkfake "$d" claude 'claude 2.1.295' + start_term "$d" + env "${CLEAN_UNSET[@]}" HC_TERMINAL_ID=x FM_OSC7501_TTY="$TERM_SLAVE" \ + PATH="$d/bin:$PATH" bash "$SCRIPT" arm \ + --task t5 --harness claude --backend tmux --kind ship \ + --state "$d/state" --config "$d/config" --agent-bin claude + sleep 0.3 + stop_term + local cap + cap=$(cat "$d/cap" 2>/dev/null) + assert_equals "" "$cap" "native claude: terminal must see zero bytes (no query, no record)" + [ -z "$(ls "$d/state"/.osc7501watch-* 2>/dev/null)" ] || fail "native claude spawned a watcher" + pass "native gate: claude >= 2.1.295 emits no probe and arms no watcher" +} + +# Contract: arm must not delay the worker indefinitely on a version probe. +# A TERM-resistant executable and child reject the old TERM-then-wait hang; +# the worker marker proves the ordinary launch sequence actually continued. +t_term_resistant_probe_allows_worker_launch() { + local d=$TMP_ROOT/stubbornprobe + mkhome "$d" + if ! env "${CLEAN_UNSET[@]}" python3 - "$SCRIPT" "$d" <<'PYTEST' +import os, signal, subprocess, sys, time +from pathlib import Path +script, home = sys.argv[1:] +root = Path(home) +agent = root / "claude" +agent.write_text("#!" + sys.executable + "\n" + '''import os, signal, sys +from pathlib import Path +root = Path(__file__).parent +if "--version" not in sys.argv: + (root / "worker-started").touch() + sys.exit(0) +signal.signal(signal.SIGTERM, signal.SIG_IGN) +child = os.fork() +with (root / "probe-pids").open("a") as f: + f.write(str(os.getpid()) + "\\n") +while True: + signal.pause() +''') +agent.chmod(0o755) +tty = root / "tty" +tty.touch() +env = dict(os.environ, HC_TERMINAL_ID="test", FM_OSC7501_TTY=str(tty), + FM_OSC7501_PROBE_SECS="1") +args = ["bash", script, "arm", "--task", "stubborn", "--harness", "claude", + "--agent-bin", str(agent), "--backend", "tmux", "--kind", "ship", + "--state", str(root / "state"), "--config", str(root / "config")] +launch = subprocess.Popen(["bash", "-c", '"$@"; exec "$AGENT"', "_", *args], + env=dict(env, AGENT=str(agent)), start_new_session=True) +try: + started = time.monotonic() + assert launch.wait(timeout=6) == 0, "worker launch failed" + assert time.monotonic() - started < 6, "probe exceeded launch bound" + assert (root / "worker-started").exists(), "worker never started" + pids = (root / "probe-pids").read_text().splitlines() + assert len(pids) == 2, "probe did not exercise a descendant" + assert not list((root / "state").glob(".osc7501watch-*")), "failed probe armed a reporter" + assert tty.read_bytes() == b"", "failed native probe wrote terminal records" +finally: + try: + os.killpg(launch.pid, signal.SIGKILL) + except ProcessLookupError: + pass + pidfile = root / "probe-pids" + if pidfile.exists(): + for pid in pidfile.read_text().splitlines(): + try: + os.kill(int(pid), signal.SIGKILL) + except ProcessLookupError: + pass + launch.wait() +PYTEST + then + fail "TERM-resistant version probe prevented bounded worker launch" + fi + pass "native gate: TERM-resistant probe and child cannot block worker launch" +} + +# An old claude below the native floor falls through to the generic reporter. +t_old_claude_uses_generic() { + local d=$TMP_ROOT/oldclaude + mkhome "$d"; mkfake "$d" claude 'claude 1.9.9' + start_term "$d" + env "${CLEAN_UNSET[@]}" HC_TERMINAL_ID=x FM_OSC7501_TTY="$TERM_SLAVE" \ + PATH="$d/bin:$PATH" bash "$SCRIPT" arm \ + --task t6 --harness claude --backend tmux --kind ship \ + --state "$d/state" --config "$d/config" --agent-bin claude + assert_present "$(echo "$d/state"/.osc7501watch-* | head -1)" "old claude armed the generic watcher" + stop_term + pass "native gate: claude below the floor uses the generic reporter" +} + +# A launch that names no executable has no established agent - fm-spawn's +# raw launch commands, such as `env claude ...` with harness env - so it gets +# no reporter, whatever its harness word. +t_launch_without_agent_bin_skips() { + local d=$TMP_ROOT/noagentbin h + mkhome "$d" + start_term "$d" + for h in env claude; do + env "${CLEAN_UNSET[@]}" HC_TERMINAL_ID=x FM_OSC7501_TTY="$TERM_SLAVE" bash "$SCRIPT" arm \ + --task t29 --harness "$h" --backend tmux --kind ship \ + --state "$d/state" --config "$d/config" + [ -z "$(ls "$d/state"/.osc7501watch-* 2>/dev/null)" ] \ + || fail "a $h launch naming no executable armed a watcher" + done + stop_term + pass "eligibility: a launch without --agent-bin arms nothing" +} + +# A harness upgraded in place across the native floor - the same symlink, its +# target rewritten with a newer binary - must be re-probed at the next launch, +# so the generic reporter stands down instead of competing with native records. +t_inplace_upgrade_reprobes_native() { + local d=$TMP_ROOT/upgrade + mkhome "$d" + mkdir -p "$d/bin" "$d/install" + printf '#!/usr/bin/env bash\necho "pi 1.0.9"\n' >"$d/install/pi" + chmod +x "$d/install/pi" + ln -s "$d/install/pi" "$d/bin/pi" + start_term "$d" + env "${CLEAN_UNSET[@]}" FM_OSC7501_TTY="$TERM_SLAVE" HC_TERMINAL_ID=x PATH="$d/bin:$PATH" \ + bash "$SCRIPT" arm --task t22 --harness pi --backend tmux --kind ship \ + --state "$d/state" --config "$d/config" --agent-bin pi + local first + first=$(watcher_pid "$d/state") || fail "upgrade: pi below the floor armed no generic watcher" + printf '#!/usr/bin/env bash\necho "pi 1.1.0"\n' >"$d/install/pi" + env "${CLEAN_UNSET[@]}" FM_OSC7501_TTY="$TERM_SLAVE" HC_TERMINAL_ID=x PATH="$d/bin:$PATH" \ + bash "$SCRIPT" arm --task t22 --harness pi --backend tmux --kind ship \ + --state "$d/state" --config "$d/config" --agent-bin pi + wait_gone "$first" || fail "upgrade: the old generic reporter survived the relaunch" + [ -z "$(ls "$d/state"/.osc7501watch-* 2>/dev/null)" ] \ + || fail "upgrade: a native pi after an in-place upgrade still got a generic reporter" + stop_term + pass "native gate: an in-place harness upgrade is re-probed at the next launch" +} + +# codex-cli first shipped native OSC 7501 records in 0.163.0 +# (openai/codex#52725, absent from rust-v0.162.1): 0.163.0 arms nothing, +# 0.162.1 still gets the generic reporter, and a codex whose version cannot be +# read is treated as native. +t_native_codex_threshold() { + local d=$TMP_ROOT/nativecodex + mkhome "$d"; mkfake "$d" codex 'codex-cli 0.163.0' + start_term "$d" + env "${CLEAN_UNSET[@]}" REX_BLOCK=b1 FM_OSC7501_TTY="$TERM_SLAVE" \ + PATH="$d/bin:$PATH" bash "$SCRIPT" arm \ + --task t26 --harness codex --backend rex --kind ship \ + --state "$d/state" --config "$d/config" --agent-bin codex + sleep 0.3 + assert_equals "" "$(cat "$d/cap" 2>/dev/null)" "native codex: terminal sees zero bytes" + [ -z "$(ls "$d/state"/.osc7501watch-* 2>/dev/null)" ] || fail "native codex 0.163.0 armed a watcher" + mkfake "$d" codex 'codex-cli 0.162.1' + env "${CLEAN_UNSET[@]}" REX_BLOCK=b1 FM_OSC7501_TTY="$TERM_SLAVE" \ + PATH="$d/bin:$PATH" bash "$SCRIPT" arm \ + --task t26 --harness codex --backend rex --kind ship \ + --state "$d/state" --config "$d/config" --agent-bin codex + local wpid + wpid=$(watcher_pid "$d/state") || fail "codex 0.162.1 armed no generic watcher" + mkfake "$d" codex 'codex-cli unknown build' + env "${CLEAN_UNSET[@]}" REX_BLOCK=b1 FM_OSC7501_TTY="$TERM_SLAVE" \ + PATH="$d/bin:$PATH" bash "$SCRIPT" arm \ + --task t26 --harness codex --backend rex --kind ship \ + --state "$d/state" --config "$d/config" --agent-bin codex + wait_gone "$wpid" || fail "codex relaunch left the old generic reporter running" + [ -z "$(ls "$d/state"/.osc7501watch-* 2>/dev/null)" ] || fail "a codex with an unreadable version armed a watcher" + stop_term + pass "native gate: codex >= 0.163.0 or of unreadable version arms nothing, older codex keeps the generic reporter" +} + +# --- shared root record -------------------------------------------------------- + +# A native writer and the bridge share one root record. Codex's own working +# record must stay the root record: with codex activity unknown, an open +# decision and an old done line give the bridge no grounds to write, the +# heartbeat never replaces the native record, and neither do the ordinary +# exits - a TERM (what disarm sends) or the launch shell's death. +t_unknown_activity_leaves_native_root() { + local d=$TMP_ROOT/sharedroot native exit holder wpid + mkhome "$d" + : >"$d/cap" + native=$(printf '\033]7501;state=working:app=codex\033\134') + printf 'done [at=1]: PR https://example.test/pr/7\nneeds-decision [at=2]: ship it?\n' >"$d/state/t27.status" + for exit in term parent; do + sleep 300 & + holder=$! + printf '%s' "$native" >>"$d/cap" + env "${CLEAN_UNSET[@]}" FM_OSC7501_TTY="$d/cap" FM_OSC7501_INTERVAL=1 FM_OSC7501_HEARTBEAT=1 \ + bash "$SCRIPT" watch --task t27 --kind ship --state "$d/state" --harness codex \ + --parent "$holder" & + wpid=$! + sleep 3 + printf '%s' "$native" >>"$d/cap" + sleep 2 + case "$exit" in + term) kill "$wpid" 2>/dev/null ;; + parent) kill "$holder" 2>/dev/null ;; + esac + wait_gone "$wpid" || fail "shared root: the watcher did not exit on $exit" + wait "$wpid" 2>/dev/null || true + kill "$holder" 2>/dev/null; wait "$holder" 2>/dev/null || true + assert_equals "app=codex " "$(grep -o 'app=[a-z-]*' "$d/cap" | sort -u | tr '\n' ' ')" \ + "the bridge wrote no record over the native root record through $exit" + done + pass "shared root: unknown activity never overwrites a native record, live or at exit" +} + +# Once activity turns unknown the bridge never holds or heartbeats its last +# record, but while that last write is still its own the durable signals keep +# it honest: a still-open decision stays blocked without rewriting, a +# resolved one retires to idle exactly once, a running-turn record parks at +# idle, and TERM still lands the final record. Known activity resumes +# reporting. +t_unknown_activity_keeps_record_honest() { + local d=$TMP_ROOT/unknownhonest n + mkhome "$d" + : >"$d/cap" + sleep 300 & + local holder=$! + busy_rec "$d/state" t28 idle + printf 'needs-decision [at=1] [key=k1]: ship it?\n' >"$d/state/t28.status" + env "${CLEAN_UNSET[@]}" FM_OSC7501_TTY="$d/cap" FM_OSC7501_INTERVAL=1 FM_OSC7501_HEARTBEAT=1 \ + bash "$SCRIPT" watch --task t28 --kind ship --state "$d/state" --harness opencode \ + --parent "$holder" & + local wpid=$! + wait_cap 'state=blocked:kind=question' "$d/cap" 60 || fail "unknown: no blocked record while activity was known" + rm -f "$d/state/t28.busy-state" "$d/state/t28.busy-gen" + sleep 3 + n=$(grep -o 'state=' "$d/cap" | wc -l | tr -d ' ') + sleep 2 + assert_equals "$n" "$(grep -o 'state=' "$d/cap" | wc -l | tr -d ' ')" \ + "unknown: a still-justified blocked record is never rewritten" + : >"$d/cap" + printf 'resolved [at=2] [key=k1]: yes\n' >>"$d/state/t28.status" + wait_cap 'state=idle' "$d/cap" 60 || fail "unknown: the resolved decision did not retire to idle" + sleep 2 + assert_equals "1" "$(grep -o 'state=' "$d/cap" | wc -l | tr -d ' ')" \ + "unknown: the retirement write happens once - no heartbeat while unknown" + : >"$d/cap" + busy_rec "$d/state" t28 busy + wait_cap 'state=working' "$d/cap" 60 || fail "unknown: known activity did not resume reporting" + : >"$d/cap" + rm -f "$d/state/t28.busy-state" "$d/state/t28.busy-gen" + wait_cap 'state=idle' "$d/cap" 60 || fail "unknown: a running-turn record did not retire to idle" + kill "$wpid" 2>/dev/null + wait_gone "$wpid" || fail "unknown: the watcher did not exit on TERM" + wait "$wpid" 2>/dev/null || true + assert_equals "2" "$(grep -o 'state=' "$d/cap" | wc -l | tr -d ' ')" \ + "unknown: TERM lands the final record on top of the retirement" + assert_equals "state=idle" "$(tail -c 4000 "$d/cap" | grep -o 'state=[a-z]*' | tail -1)" \ + "unknown: the final record parks at idle" + kill "$holder" 2>/dev/null; wait "$holder" 2>/dev/null || true + pass "unknown activity: no hold or heartbeat; signals retire the record once; TERM still finals" +} + +# The exit write is exactly where unknown activity must not matter: the +# final record still lands on the agent's death, and a done result survives +# - the pane keeps its outcome instead of a stale working ghost. +t_final_record_with_unknown_activity() { + local d=$TMP_ROOT/unknownfinal + mkhome "$d" + : >"$d/cap" + sleep 300 & + local holder=$! + busy_rec "$d/state" t30 busy + env "${CLEAN_UNSET[@]}" FM_OSC7501_TTY="$d/cap" FM_OSC7501_INTERVAL=1 \ + bash "$SCRIPT" watch --task t30 --kind ship --state "$d/state" --harness opencode \ + --parent "$holder" & + local wpid=$! + wait_cap 'state=working' "$d/cap" 60 || fail "final: no working record" + rm -f "$d/state/t30.busy-state" "$d/state/t30.busy-gen" + wait_cap 'state=idle' "$d/cap" 60 || fail "final: working outlived its turn once activity read unknown" + printf 'done [at=%s]: PR https://example.test/pr/9\n' "$(date +%s)" >"$d/state/t30.status" + wait_cap 'state=done' "$d/cap" 60 || fail "final: done did not report while activity was unknown" + kill "$holder" 2>/dev/null; wait "$holder" 2>/dev/null || true + wait_gone "$wpid" || fail "final: the watcher did not exit on parent death" + wait "$wpid" 2>/dev/null || true + local tail + tail=$(tail -c 4000 "$d/cap" | grep -o 'state=[a-z]*' | tail -1) + assert_equals "state=done" "$tail" "final: the last record keeps done across an unknown-activity exit" + pass "unknown activity: the exit write still lands and keeps done" +} + +# Rovo's rendered marker is its only activity source and resolves busy or +# unknown, never idle: a long turn can scroll the marker out of the captured +# tail. The durable signals still retire working when the marker leaves and +# still report a decision and a done while it is gone. +t_rovo_reports_through_unknown() { + local d=$TMP_ROOT/rovo + mkhome "$d" + mkdir -p "$d/bin" + : >"$d/cap" + : >"$d/pane" + # shellcheck disable=SC2016 + printf '#!/usr/bin/env bash\n[ "$1" = capture-pane ] && cat %q\n' "$d/pane" >"$d/bin/tmux" + chmod +x "$d/bin/tmux" + sleep 300 & + local holder=$! + printf 'Rovo is thinking...\n' >"$d/pane" + env "${CLEAN_UNSET[@]}" FM_OSC7501_TTY="$d/cap" FM_OSC7501_INTERVAL=1 PATH="$d/bin:$PATH" \ + bash "$SCRIPT" watch --task t31 --kind ship --state "$d/state" --harness rovo \ + --backend tmux --target '%9' --parent "$holder" & + local wpid=$! + wait_cap 'state=working:app=firstmate-rovo' "$d/cap" 60 \ + || fail "rovo: the busy marker did not read as working" + : >"$d/pane" + wait_cap 'state=idle' "$d/cap" 60 || fail "rovo: working outlived the marker while activity read unknown" + printf 'needs-decision [at=1] [key=k1]: pick one\n' >"$d/state/t31.status" + wait_cap 'state=blocked:kind=question' "$d/cap" 60 || fail "rovo: an open decision did not report while unknown" + printf 'resolved [at=2] [key=k1]: go\ndone [at=%s]: PR https://example.test/pr/31\n' "$(date +%s)" >>"$d/state/t31.status" + wait_cap 'state=done' "$d/cap" 60 || fail "rovo: a done stamped after the last busy read did not report while unknown" + printf 'Rovo is thinking...\n' >"$d/pane" + wait_last_state 'state=working' "$d/cap" 60 || fail "rovo: a follow-up turn did not read as working" + : >"$d/pane" + wait_last_state 'state=idle' "$d/cap" 60 \ + || fail "rovo: the earlier done reported over the follow-up turn while unknown" + kill "$wpid" 2>/dev/null + wait_gone "$wpid" || fail "rovo: the watcher did not exit on TERM" + wait "$wpid" 2>/dev/null || true + local tail + tail=$(tail -c 4000 "$d/cap" | grep -o 'state=[a-z]*' | tail -1) + assert_equals "state=done" "$tail" "rovo: the exit write keeps the done that predates the last busy read" + kill "$holder" 2>/dev/null; wait "$holder" 2>/dev/null || true + pass "rovo: the busy marker drives working; unknown activity still retires it and reports decisions and done" +} + +# Antigravity is the same shape as Rovo - its `esc to cancel` status row is +# the only activity source and only proves busy - so the signal fold carries +# it through idle, blocked, and done once the marker leaves the pane. +t_agy_reports_through_unknown() { + local d=$TMP_ROOT/agy + mkhome "$d" + mkdir -p "$d/bin" + : >"$d/cap" + : >"$d/pane" + # shellcheck disable=SC2016 + printf '#!/usr/bin/env bash\n[ "$1" = capture-pane ] && cat %q\n' "$d/pane" >"$d/bin/tmux" + chmod +x "$d/bin/tmux" + sleep 300 & + local holder=$! + printf 'working\n esc to cancel\n' >"$d/pane" + env "${CLEAN_UNSET[@]}" FM_OSC7501_TTY="$d/cap" FM_OSC7501_INTERVAL=1 PATH="$d/bin:$PATH" \ + bash "$SCRIPT" watch --task t33 --kind ship --state "$d/state" --harness agy \ + --backend tmux --target '%9' --parent "$holder" & + local wpid=$! + wait_cap 'state=working:app=firstmate-agy' "$d/cap" 60 \ + || fail "agy: the busy status row did not read as working" + : >"$d/pane" + wait_cap 'state=idle' "$d/cap" 60 || fail "agy: working outlived the status row while activity read unknown" + printf 'done [at=%s]: PR https://example.test/pr/33\n' "$(date +%s)" >"$d/state/t33.status" + wait_cap 'state=done' "$d/cap" 60 || fail "agy: a done stamped after the last busy read did not report while unknown" + kill "$wpid" 2>/dev/null + wait_gone "$wpid" || fail "agy: the watcher did not exit on TERM" + wait "$wpid" 2>/dev/null || true + kill "$holder" 2>/dev/null; wait "$holder" 2>/dev/null || true + pass "agy: the busy status row drives working; unknown activity still retires it and reports done" +} + +# A watcher killed without its final write leaves its last record on the +# pane. The next arm's retirement writes the dead watcher's final record, +# and arm writes one idle record once the pane proves 7501-aware and a +# leftover pidfile marks the record as the bridge's own - proven while the +# fresh watcher, activity unknown and the signals still folding to that +# same idle, adds nothing on top of them. +t_arm_clears_stale_record() { + local d=$TMP_ROOT/armclear + mkhome "$d" + start_term "$d" + busy_rec "$d/state" t32 busy + env "${CLEAN_UNSET[@]}" HC_TERMINAL_ID=x FM_OSC7501_TTY="$TERM_SLAVE" FM_OSC7501_INTERVAL=1 \ + bash "$SCRIPT" arm --task t32 --harness opencode --agent-bin opencode --backend tmux \ + --kind ship --state "$d/state" --config "$d/config" + local wpid + wpid=$(watcher_pid "$d/state") || fail "clear: no first watcher" + wait_cap 'state=working' "$d/cap" 60 || fail "clear: the first watcher never reported working" + kill -KILL "$wpid" 2>/dev/null + wait_gone "$wpid" || true + : >"$d/cap" + rm -f "$d/state/t32.busy-state" "$d/state/t32.busy-gen" + env "${CLEAN_UNSET[@]}" HC_TERMINAL_ID=x FM_OSC7501_TTY="$TERM_SLAVE" FM_OSC7501_INTERVAL=1 \ + bash "$SCRIPT" arm --task t32 --harness opencode --agent-bin opencode --backend tmux \ + --kind ship --state "$d/state" --config "$d/config" + wait_cap 'state=idle:app=firstmate-opencode' "$d/cap" 60 || fail "clear: arm left the stale record on the pane" + sleep 2 + assert_equals "state=idle state=idle " "$(grep -o 'state=[a-z]*' "$d/cap" | tr '\n' ' ')" \ + "clear: only the retirement and arm-time writes land - the fresh watcher does not repeat them while unknown" + stop_term + pass "lifecycle: arm clears a dead watcher's stale record once the gates pass" +} + +# A launch whose activity never reads known - a short Rovo/Antigravity turn +# that finishes between polls - still owns the root record through arm's +# idle clear: a decision, a done result, and the ordinary disarm's final +# record all land with activity unknown from the first poll on. +t_initial_unknown_launch_reports() { + local d=$TMP_ROOT/initunknown + mkhome "$d" + start_term "$d" + env "${CLEAN_UNSET[@]}" HC_TERMINAL_ID=x FM_OSC7501_TTY="$TERM_SLAVE" FM_OSC7501_INTERVAL=1 \ + bash "$SCRIPT" arm --task t34 --harness opencode --agent-bin opencode --backend tmux \ + --kind ship --state "$d/state" --config "$d/config" + local wpid + wpid=$(watcher_pid "$d/state") || fail "initial unknown: no watcher armed" + wait_cap 'state=idle:app=firstmate-opencode' "$d/cap" 60 || fail "initial unknown: no arm-time record" + printf 'needs-decision [at=1] [key=k1]: pick one\n' >"$d/state/t34.status" + wait_cap 'state=blocked:kind=question' "$d/cap" 60 \ + || fail "initial unknown: an open decision did not report" + printf 'resolved [at=2] [key=k1]: go\ndone [at=3]: PR https://example.test/pr/34\n' >>"$d/state/t34.status" + wait_cap 'state=done' "$d/cap" 60 || fail "initial unknown: done did not report" + : >"$d/cap" + env "${CLEAN_UNSET[@]}" FM_OSC7501_TTY="$TERM_SLAVE" bash "$SCRIPT" disarm --state "$d/state" + wait_gone "$wpid" || fail "initial unknown: disarm left the reporter running" + wait_cap 'state=done' "$d/cap" 60 || fail "initial unknown: disarm skipped the final record" + stop_term + pass "initial unknown: arm's record carries ownership - decisions, done, and the exit write all land" +} + +# A done/failed line carries the phase that wrote it, not the turn now in +# flight: once activity is unknown, a terminal line only reports when it was +# appended after the reading that last proved the worker busy. The stale +# lines are stamped no earlier than any busy reading, so a whole-second +# stamp cannot order them - the same-second case - and both an earlier done +# and an earlier failed must still park at idle. The agent's death is +# different: no turn is in flight, so the parent-exit write restores that +# standing result. A done appended after the last busy read reports live +# even when its stamp is no later than that reading's second. +t_stale_terminal_line_parks_idle() { + local d=$TMP_ROOT/staleline verb wpid holder suppressed t0 + mkhome "$d" + for verb in 'done' 'failed'; do + suppressed='done' + [ "$verb" = 'done' ] || suppressed=error + : >"$d/cap" + sleep 300 & + holder=$! + printf '%s [at=%s]: earlier phase result\n' "$verb" "$(($(date +%s) + 60))" >"$d/state/t42.status" + busy_rec "$d/state" t42 busy + env "${CLEAN_UNSET[@]}" FM_OSC7501_TTY="$d/cap" FM_OSC7501_INTERVAL=1 \ + bash "$SCRIPT" watch --task t42 --kind ship --state "$d/state" --harness opencode \ + --parent "$holder" & + wpid=$! + wait_cap 'state=working' "$d/cap" 60 || fail "stale $verb: no working record" + rm -f "$d/state/t42.busy-state" "$d/state/t42.busy-gen" + wait_last_state 'state=idle' "$d/cap" 60 \ + || fail "stale $verb: an earlier-phase $verb reported over a turn still in flight" + sleep 2 + assert_equals "0" "$(grep -o "state=$suppressed" "$d/cap" | wc -l | tr -d ' ')" \ + "stale $verb: the earlier result never reported while activity was unknown" + kill "$holder" 2>/dev/null; wait "$holder" 2>/dev/null || true + wait_gone "$wpid" || fail "stale $verb: the watcher did not exit on parent death" + wait "$wpid" 2>/dev/null || true + assert_equals "state=$suppressed" "$(grep -o 'state=[a-z]*' "$d/cap" | tail -1)" \ + "stale $verb: the exit write with unknown activity keeps the standing result" + done + : >"$d/cap" + sleep 300 & + holder=$! + t0=$(date +%s) + busy_rec "$d/state" t42 busy + env "${CLEAN_UNSET[@]}" FM_OSC7501_TTY="$d/cap" FM_OSC7501_INTERVAL=1 \ + bash "$SCRIPT" watch --task t42 --kind ship --state "$d/state" --harness opencode \ + --parent "$holder" & + wpid=$! + wait_cap 'state=working' "$d/cap" 60 || fail "fresh done: no working record" + rm -f "$d/state/t42.busy-state" "$d/state/t42.busy-gen" + wait_last_state 'state=idle' "$d/cap" 60 || fail "fresh done: working did not retire" + printf 'done [at=%s]: PR https://example.test/pr/42\n' "$t0" >>"$d/state/t42.status" + wait_last_state 'state=done' "$d/cap" 60 \ + || fail "fresh done: a line appended after the last busy read did not report" + kill "$wpid" 2>/dev/null; wait "$wpid" 2>/dev/null || true + kill "$holder" 2>/dev/null; wait "$holder" 2>/dev/null || true + pass "unknown activity: a stale done/failed parks at idle until exit keeps it; a fresh done still reports" +} + +# A watcher killed without its final write leaves its record standing. +# fm-spawn's real order runs disarm after the agent command returns, so +# disarm finds the dead watcher's pidfile and writes the final record +# itself; the next launch on that pane - reporting off, or a natively +# reporting agent - then keeps the parked record without writing. +t_unarmed_relaunch_clears_leftover() { + local d=$TMP_ROOT/deadleft kind wpid n0 + mkhome "$d"; mkfake "$d" claude 'claude 2.1.295' + start_term "$d" + for kind in off native; do + rm -f "$d/config/osc7501-status" + busy_rec "$d/state" t43 busy + env "${CLEAN_UNSET[@]}" HC_TERMINAL_ID=x FM_OSC7501_TTY="$TERM_SLAVE" FM_OSC7501_INTERVAL=1 \ + bash "$SCRIPT" arm --task t43 --harness opencode --agent-bin opencode --backend tmux --kind ship \ + --state "$d/state" --config "$d/config" + wpid=$(watcher_pid "$d/state") || fail "leftover $kind: no watcher armed" + wait_cap 'state=working' "$d/cap" 60 || fail "leftover $kind: no working record" + kill -KILL "$wpid" 2>/dev/null; wait "$wpid" 2>/dev/null || true + n0=$(grep -o 'state=' "$d/cap" | wc -l | tr -d ' ') + # the real launch order runs disarm after the agent command returns; + # the pidfile names a watcher that is already gone, so disarm writes + # the final record itself before removing the pidfile + env "${CLEAN_UNSET[@]}" FM_OSC7501_TTY="$TERM_SLAVE" bash "$SCRIPT" disarm --state "$d/state" + wait_last_state 'state=idle' "$d/cap" 60 \ + || fail "leftover $kind: disarm left the dead watcher's record standing" + [ -z "$(ls "$d/state"/.osc7501watch-* 2>/dev/null)" ] \ + || fail "leftover $kind: disarm left the pidfile behind" + if [ "$kind" = off ]; then + printf 'off\n' >"$d/config/osc7501-status" + env "${CLEAN_UNSET[@]}" HC_TERMINAL_ID=x FM_OSC7501_TTY="$TERM_SLAVE" bash "$SCRIPT" arm \ + --task t43 --harness opencode --agent-bin opencode --backend tmux --kind ship \ + --state "$d/state" --config "$d/config" + else + env "${CLEAN_UNSET[@]}" HC_TERMINAL_ID=x FM_OSC7501_TTY="$TERM_SLAVE" PATH="$d/bin:$PATH" \ + bash "$SCRIPT" arm --task t43 --harness claude --agent-bin claude --backend tmux --kind ship \ + --state "$d/state" --config "$d/config" + fi + sleep 1 + [ -z "$(ls "$d/state"/.osc7501watch-* 2>/dev/null)" ] \ + || fail "leftover $kind: the unarmed relaunch started a watcher" + assert_equals "$((n0 + 1))" "$(grep -o 'state=' "$d/cap" | wc -l | tr -d ' ')" \ + "leftover $kind: only disarm's final write landed - the relaunch added nothing" + assert_equals "state=idle" "$(grep -o 'state=[a-z]*' "$d/cap" | tail -1)" \ + "leftover $kind: nothing rewrote the cleared record" + done + stop_term + pass "lifecycle: disarm retires a dead watcher's record; off and native relaunches stay parked" +} + +# A watcher still alive at disarm but stuck past the TERM grace - here in a +# pane capture that never returns - cannot run its trap, so retirement kills +# it. The kill must not lose the final write: retirement writes it for the +# watcher before forgetting it, and the next launch on that pane - reporting +# off, or a natively reporting agent - then keeps the parked record. +t_killed_watcher_still_gets_final() { + local d=$TMP_ROOT/killedfinal kind wpid n0 stuck + mkhome "$d"; mkfake "$d" claude 'claude 2.1.295' + # shellcheck disable=SC2016 + printf '#!/usr/bin/env bash\n[ "$1" = capture-pane ] || exit 0\nif [ -f %q ]; then echo $$ >%q; exec sleep 60; fi\ncat %q\n' \ + "$d/stall" "$d/stuck" "$d/pane" >"$d/bin/tmux" + chmod +x "$d/bin/tmux" + start_term "$d" + for kind in off native; do + rm -f "$d/config/osc7501-status" "$d/stall" "$d/stuck" + printf 'Rovo is thinking...\n' >"$d/pane" + env "${CLEAN_UNSET[@]}" HC_TERMINAL_ID=x FM_OSC7501_TTY="$TERM_SLAVE" FM_OSC7501_INTERVAL=1 PATH="$d/bin:$PATH" \ + bash "$SCRIPT" arm --task t44 --harness rovo --agent-bin rovo --backend tmux --target '%9' --kind ship \ + --state "$d/state" --config "$d/config" + wpid=$(watcher_pid "$d/state") || fail "killed $kind: no watcher armed" + wait_last_state 'state=working' "$d/cap" 60 || fail "killed $kind: no working record" + : >"$d/stall" + wait_cap '' "$d/stuck" 60 && [ -s "$d/stuck" ] || fail "killed $kind: the watcher never reached the stuck capture" + stuck=$(cat "$d/stuck") + n0=$(grep -o 'state=' "$d/cap" | wc -l | tr -d ' ') + env "${CLEAN_UNSET[@]}" FM_OSC7501_TTY="$TERM_SLAVE" bash "$SCRIPT" disarm --state "$d/state" + kill -0 "$wpid" 2>/dev/null && fail "killed $kind: disarm returned with the stuck watcher still running" + kill "$stuck" 2>/dev/null || true + assert_equals "$((n0 + 1))" "$(grep -o 'state=' "$d/cap" | wc -l | tr -d ' ')" \ + "killed $kind: retirement wrote exactly one final record for the killed watcher" + assert_equals "state=idle" "$(grep -o 'state=[a-z]*' "$d/cap" | tail -1)" \ + "killed $kind: the killed watcher's working record was parked at idle" + [ -z "$(ls "$d/state"/.osc7501watch-* 2>/dev/null)" ] \ + || fail "killed $kind: disarm left the pidfile behind" + if [ "$kind" = off ]; then + printf 'off\n' >"$d/config/osc7501-status" + env "${CLEAN_UNSET[@]}" HC_TERMINAL_ID=x FM_OSC7501_TTY="$TERM_SLAVE" bash "$SCRIPT" arm \ + --task t44 --harness opencode --agent-bin opencode --backend tmux --kind ship \ + --state "$d/state" --config "$d/config" + else + env "${CLEAN_UNSET[@]}" HC_TERMINAL_ID=x FM_OSC7501_TTY="$TERM_SLAVE" PATH="$d/bin:$PATH" \ + bash "$SCRIPT" arm --task t44 --harness claude --agent-bin claude --backend tmux --kind ship \ + --state "$d/state" --config "$d/config" + fi + sleep 1 + [ -z "$(ls "$d/state"/.osc7501watch-* 2>/dev/null)" ] \ + || fail "killed $kind: the unarmed relaunch started a watcher" + assert_equals "$((n0 + 1))" "$(grep -o 'state=' "$d/cap" | wc -l | tr -d ' ')" \ + "killed $kind: the relaunch added nothing - the stale record never returns" + done + stop_term + pass "lifecycle: a watcher killed at disarm still gets its final record; off and native relaunches stay parked" +} + +# A short worker can finish and exit while its asynchronously started +# watcher is still initializing - here held in its library load, before its +# TERM trap exists - so disarm's TERM ends it without any write of its own. +# Retirement still writes the final record, keeping the worker's done, and +# the next launch on that pane - reporting off, or a natively reporting +# agent - keeps it. +t_early_term_watcher_still_gets_final() { + local d=$TMP_ROOT/earlyterm kind wpid n0 f + mkhome "$d"; mkfake "$d" claude 'claude 2.1.295' + mkdir -p "$d/sbin" + for f in "$ROOT"/bin/*; do ln -s "$f" "$d/sbin/${f##*/}"; done + rm -f "$d/sbin/fm-classify-lib.sh" + # shellcheck disable=SC2016 # Expansions belong to the generated script at runtime. + printf 'if [ -n "${FM_OSC7501_WATCHER:-}" ] && [ -f %q ]; then echo $$ >%q; sleep 5; fi\n. %q\n' \ + "$d/slow" "$d/initializing" "$ROOT/bin/fm-classify-lib.sh" >"$d/sbin/fm-classify-lib.sh" + printf 'done [at=%s]: PR https://example.test/pr/45\n' "$(date +%s)" >"$d/state/t45.status" + start_term "$d" + for kind in off native; do + rm -f "$d/config/osc7501-status" "$d/initializing" + : >"$d/slow" + env "${CLEAN_UNSET[@]}" HC_TERMINAL_ID=x FM_OSC7501_TTY="$TERM_SLAVE" FM_OSC7501_INTERVAL=1 \ + bash "$d/sbin/fm-osc7501.sh" arm --task t45 --harness opencode --agent-bin opencode --backend tmux \ + --kind ship --state "$d/state" --config "$d/config" + wpid=$(watcher_pid "$d/state") || fail "early term $kind: no watcher armed" + wait_cap "$wpid" "$d/initializing" 60 || fail "early term $kind: the watcher never reached its library load" + env "${CLEAN_UNSET[@]}" FM_OSC7501_TTY="$TERM_SLAVE" bash "$SCRIPT" disarm --state "$d/state" + kill -0 "$wpid" 2>/dev/null && fail "early term $kind: disarm returned with the watcher still running" + rm -f "$d/slow" + assert_equals "state=done" "$(grep -o 'state=[a-z]*' "$d/cap" | tail -1)" \ + "early term $kind: retirement kept the worker's done although the watcher never wrote" + [ -z "$(ls "$d/state"/.osc7501watch-* 2>/dev/null)" ] \ + || fail "early term $kind: disarm left the pidfile behind" + n0=$(grep -o 'state=' "$d/cap" | wc -l | tr -d ' ') + if [ "$kind" = off ]; then + printf 'off\n' >"$d/config/osc7501-status" + env "${CLEAN_UNSET[@]}" HC_TERMINAL_ID=x FM_OSC7501_TTY="$TERM_SLAVE" bash "$SCRIPT" arm \ + --task t45 --harness opencode --agent-bin opencode --backend tmux --kind ship \ + --state "$d/state" --config "$d/config" + else + env "${CLEAN_UNSET[@]}" HC_TERMINAL_ID=x FM_OSC7501_TTY="$TERM_SLAVE" PATH="$d/bin:$PATH" \ + bash "$SCRIPT" arm --task t45 --harness claude --agent-bin claude --backend tmux --kind ship \ + --state "$d/state" --config "$d/config" + fi + sleep 1 + [ -z "$(ls "$d/state"/.osc7501watch-* 2>/dev/null)" ] \ + || fail "early term $kind: the unarmed relaunch started a watcher" + assert_equals "$n0" "$(grep -o 'state=' "$d/cap" | wc -l | tr -d ' ')" \ + "early term $kind: the relaunch added nothing over the final record" + done + stop_term + pass "lifecycle: a watcher TERMed before its trap still gets its final record; off and native relaunches keep it" +} + +# --- config gate --------------------------------------------------------------- + +t_config_off_disables() { + local d=$TMP_ROOT/cfgoff + mkhome "$d"; printf 'off\n' >"$d/config/osc7501-status" + start_term "$d" + env "${CLEAN_UNSET[@]}" HC_TERMINAL_ID=x FM_OSC7501_TTY="$TERM_SLAVE" bash "$SCRIPT" arm \ + --task t8 --harness opencode --agent-bin opencode --backend tmux --kind ship \ + --state "$d/state" --config "$d/config" + sleep 0.3 + stop_term + local cap + cap=$(cat "$d/cap" 2>/dev/null) + assert_equals "" "$cap" "config off: terminal sees nothing at all" + pass "config gate: osc7501-status=off disables arm entirely" +} + +# A known-aware environment (herdr-core's HC_TERMINAL_ID) arms the watcher +# without sending any probe bytes on the wire. +t_hc_marker_arms_watcher() { + local d=$TMP_ROOT/hc + mkhome "$d" + start_term "$d" + env -u HERDR_ENV -u HERDR_PANE_ID -u HERDR_SESSION -u HERDR_SOCKET_PATH \ + -u HERDR_TAB_ID -u HERDR_WORKSPACE_ID -u REX_BLOCK -u REX_SESSION \ + -u TERM_PROGRAM \ + HC_TERMINAL_ID=t-1 FM_OSC7501_TTY="$TERM_SLAVE" bash "$SCRIPT" arm \ + --task t9 --harness opencode --agent-bin opencode --backend tmux --kind ship \ + --state "$d/state" --config "$d/config" + sleep 0.3 + assert_present "$(echo "$d/state"/.osc7501watch-* | head -1)" "hc env: watcher armed without a query" + assert_equals "state=idle " "$(grep -o 'state=[a-z]*' "$d/cap" | sort -u | tr '\n' ' ')" \ + "hc env: the only bytes are the arm-time idle clear - no capability probe" + stop_term + pass "known-aware: HC_TERMINAL_ID arms with no capability probe" +} + +# --- watcher lifecycle ------------------------------------------------------- + +watcher_pid() { # -> the one recorded watcher pid + local f + for f in "$1"/.osc7501watch-*; do + [ -f "$f" ] || continue + read -r pid _ <"$f" && printf '%s' "$pid" + return 0 + done + return 1 +} + +wait_gone() { # + local i=0 + while kill -0 "$1" 2>/dev/null && [ "$i" -lt 60 ]; do sleep 0.1; i=$((i + 1)); done + ! kill -0 "$1" 2>/dev/null +} + +# Two panes of one home, each arming through its own real controlling +# terminal (/dev/tty, no stand-in): the second launch must not retire the +# first pane's reporter, and each terminal reports its own task. +t_concurrent_panes_keep_their_reporters() { + local d=$TMP_ROOT/twopanes a b pane + mkhome "$d" + busy_rec "$d/state" ta busy + busy_rec "$d/state" tb busy + for pane in a b; do + mkdir -p "$d/$pane" + # shellcheck disable=SC2016 + start_term "$d/$pane" env "${CLEAN_UNSET[@]}" HC_TERMINAL_ID="$pane" FM_OSC7501_INTERVAL=1 \ + bash -c 'echo $$ >"$5"; bash "$1" arm --task "$4" --harness opencode --agent-bin opencode --backend tmux --kind ship --state "$2" --config "$3"; exec sleep 300' \ + _ "$SCRIPT" "$d/state" "$d/config" "t$pane" "$d/$pane/shell" + eval "$pane=\$TERM_PID" + wait_cap "$(printf 'fm-t%s' "$pane" | base64 | tr -d '\n')" "$d/$pane/cap" 60 || fail "pane $pane: no record for t$pane" + done + local pids pid + pids=$(cat "$d/state"/.osc7501watch-* 2>/dev/null | awk '{print $1}') + assert_equals 2 "$(printf '%s\n' "$pids" | grep -c .)" "one reporter per controlling terminal" + for pid in $pids; do kill -0 "$pid" 2>/dev/null || fail "a pane's reporter was retired by the other pane's launch"; done + for pid in $pids; do kill "$pid" 2>/dev/null; done + kill "$(cat "$d/a/shell")" "$(cat "$d/b/shell")" 2>/dev/null || true + TERM_PID=$a stop_term + TERM_PID=$b stop_term + pass "concurrent panes: each controlling terminal keeps its own reporter" +} + +# The pane shell outlives the agent: the launch's disarm after the agent +# returns must retire the reporter and park the record, not leave working. +t_disarm_after_agent_exit_parks_idle() { + local d=$TMP_ROOT/disarm + mkhome "$d" + start_term "$d" + busy_rec "$d/state" t10 busy + env "${CLEAN_UNSET[@]}" HC_TERMINAL_ID=x FM_OSC7501_TTY="$TERM_SLAVE" FM_OSC7501_INTERVAL=1 bash "$SCRIPT" arm \ + --task t10 --harness opencode --agent-bin opencode --backend tmux --kind ship \ + --state "$d/state" --config "$d/config" + local wpid + wpid=$(watcher_pid "$d/state") || fail "disarm: no watcher armed" + wait_cap 'state=working' "$d/cap" 60 || fail "disarm: no working record" + env "${CLEAN_UNSET[@]}" FM_OSC7501_TTY="$TERM_SLAVE" bash "$SCRIPT" disarm --state "$d/state" + wait_gone "$wpid" || fail "disarm left the reporter running" + local tail + tail=$(tail -c 4000 "$d/cap" | grep -o 'state=[a-z]*' | tail -1) + assert_equals "state=idle" "$tail" "disarm parks the record at idle" + [ -z "$(ls "$d/state"/.osc7501watch-* 2>/dev/null)" ] || fail "disarm left the pidfile behind" + stop_term + pass "lifecycle: disarm after the agent exits retires the reporter" +} + +# A relaunch on the same pane retires the old reporter before any gate: a +# same-task relaunch rebinds a fresh reporter, and a natively reporting agent +# gets the pane with no firstmate writer left. +t_rearm_retires_previous_reporter() { + local d=$TMP_ROOT/rearm + mkhome "$d"; mkfake "$d" claude 'claude 2.1.295' + start_term "$d" + env "${CLEAN_UNSET[@]}" FM_OSC7501_TTY="$TERM_SLAVE" HC_TERMINAL_ID=x bash "$SCRIPT" arm \ + --task t11 --harness opencode --agent-bin opencode --backend tmux --kind ship \ + --state "$d/state" --config "$d/config" + local first second + first=$(watcher_pid "$d/state") || fail "rearm: no first watcher" + env "${CLEAN_UNSET[@]}" FM_OSC7501_TTY="$TERM_SLAVE" HC_TERMINAL_ID=x bash "$SCRIPT" arm \ + --task t11 --harness opencode --agent-bin opencode --backend tmux --kind ship \ + --state "$d/state" --config "$d/config" + second=$(watcher_pid "$d/state") || fail "rearm: same-task relaunch armed no watcher" + [ "$first" != "$second" ] || fail "rearm: same-task relaunch kept the old reporter" + wait_gone "$first" || fail "rearm: old reporter still running after same-task relaunch" + env "${CLEAN_UNSET[@]}" FM_OSC7501_TTY="$TERM_SLAVE" HC_TERMINAL_ID=x PATH="$d/bin:$PATH" \ + bash "$SCRIPT" arm --task t11 --harness claude --backend tmux --kind ship \ + --state "$d/state" --config "$d/config" --agent-bin claude + wait_gone "$second" || fail "rearm: native relaunch left the firstmate reporter running" + [ -z "$(ls "$d/state"/.osc7501watch-* 2>/dev/null)" ] || fail "rearm: native relaunch armed a watcher" + stop_term + pass "lifecycle: relaunch retires the previous reporter before native/off gates" +} + +# Cursor arms no busy-state record; its own conversation transcript is the +# activity source. +t_cursor_transcript_drives_activity() { + local d=$TMP_ROOT/cursor proj conv + mkhome "$d" + start_term "$d" + sleep 300 & + local holder=$! + proj=$d/cursor-projects/p1 + conv=$proj/agent-transcripts/c1/c1.jsonl + mkdir -p "${conv%/*}" + printf '{"workspacePath":"%s"}\n' "$d/ws" >"$proj/.workspace-trusted" + printf 'projects_root=%s\nworkspace_root=%s\n' "$d/cursor-projects" "$d/ws" >"$d/state/t16.cursor-session" + printf '{"role":"user"}\n' >"$conv" + env "${CLEAN_UNSET[@]}" FM_OSC7501_TTY="$TERM_SLAVE" FM_OSC7501_INTERVAL=1 \ + bash "$SCRIPT" watch --task t16 --kind ship --state "$d/state" --harness cursor \ + --parent "$holder" & + local wpid=$! + wait_cap "state=working:app=firstmate-cursor" "$d/cap" 60 \ + || fail "cursor: an open transcript turn did not read as working" + printf '{"type":"turn_ended"}\n' >>"$conv" + wait_cap 'state=idle' "$d/cap" 60 || fail "cursor: a closed transcript turn did not read as idle" + kill "$wpid" 2>/dev/null; wait "$wpid" 2>/dev/null || true + kill "$holder" 2>/dev/null; wait "$holder" 2>/dev/null || true + stop_term + pass "activity: a cursor worker reports working and idle from its transcript" +} + +# Grok has no busy-state writer; the classifier reads its rendered pane through +# the backend, so a follow-up turn showing Grok's busy footer reads as working +# over an earlier done, and done returns once the footer clears. +t_grok_pane_drives_activity() { + local d=$TMP_ROOT/grok + mkhome "$d" + mkdir -p "$d/bin" + : >"$d/cap" + : >"$d/pane" + # shellcheck disable=SC2016 + printf '#!/usr/bin/env bash\n[ "$1" = capture-pane ] && cat %q\n' "$d/pane" >"$d/bin/tmux" + chmod +x "$d/bin/tmux" + sleep 300 & + local holder=$! + printf 'done [at=%s]: PR https://example.test/pr/6\n' "$(date +%s)" >"$d/state/t25.status" + printf 'working on it\n Ctrl+c:cancel\n' >"$d/pane" + env "${CLEAN_UNSET[@]}" FM_OSC7501_TTY="$d/cap" FM_OSC7501_INTERVAL=1 PATH="$d/bin:$PATH" \ + bash "$SCRIPT" watch --task t25 --kind ship --state "$d/state" --harness grok \ + --backend tmux --target '%9' --parent "$holder" & + local wpid=$! + wait_cap 'state=working:app=firstmate-grok' "$d/cap" 300 \ + || fail "grok: a pane showing the busy footer did not read as working" + printf 'ready\n> \n' >"$d/pane" + wait_cap 'state=done' "$d/cap" 300 || fail "grok: done did not return once the footer cleared" + kill "$wpid" 2>/dev/null; wait "$wpid" 2>/dev/null || true + kill "$holder" 2>/dev/null; wait "$holder" 2>/dev/null || true + pass "activity: a grok worker reports working from its rendered pane" +} + +# Single-source precedence: a running turn reads as working even after a done +# line, the done returns once the turn ends, and the final record keeps it +# even when the agent died with its busy record still open. +t_turn_then_terminal_line() { + local d=$TMP_ROOT/precedence + mkhome "$d" + : >"$d/cap" + sleep 300 & + local holder=$! + busy_rec "$d/state" t23 busy + printf 'done [at=%s]: PR https://example.test/pr/5\n' "$(date +%s)" >"$d/state/t23.status" + env "${CLEAN_UNSET[@]}" FM_OSC7501_TTY="$d/cap" FM_OSC7501_INTERVAL=1 \ + bash "$SCRIPT" watch --task t23 --kind ship --state "$d/state" --harness opencode \ + --parent "$holder" & + local wpid=$! + wait_cap 'state=working' "$d/cap" 300 || fail "precedence: a running turn did not read as working" + busy_rec "$d/state" t23 idle + wait_cap 'state=done' "$d/cap" 300 || fail "precedence: the done line did not return after the turn" + : >"$d/cap" + busy_rec "$d/state" t23 busy + wait_cap 'state=working' "$d/cap" 300 || fail "precedence: a second turn did not read as working" + : >"$d/cap" + kill "$wpid" 2>/dev/null; wait "$wpid" 2>/dev/null || true + assert_equals "state=done " "$(grep -o 'state=[a-z]*' "$d/cap" | sort -u | tr '\n' ' ')" \ + "the final record keeps done although the busy record was left open" + kill "$holder" 2>/dev/null; wait "$holder" 2>/dev/null || true + pass "mapping: working while a turn runs, done after it, done as the final record" +} + +# Contract: a standing captain hold waits on the captain even though its +# keyed decision is closed. Observable OSC records must stay question-blocked +# for idle and busy activity, then return to activity once the hold is superseded. +# This rejects treating the closed decision as permission to report idle/working. +t_captain_hold_stays_question_blocked() { + local d=$TMP_ROOT/captainhold act wpid + mkhome "$d" + for act in idle busy; do + : >"$d/cap" + busy_rec "$d/state" t26 "$act" + printf 'needs-decision [key=choice]: choose\ncaptain-held [key=choice]: captain will decide\n' >"$d/state/t26.status" + env "${CLEAN_UNSET[@]}" FM_OSC7501_TTY="$d/cap" FM_OSC7501_INTERVAL=1 \ + bash "$SCRIPT" watch --task t26 --kind ship --state "$d/state" --harness opencode \ + --parent "$$" & + wpid=$! + if ! wait_cap 'state=blocked:kind=question:app=firstmate-opencode' "$d/cap" 100; then + kill "$wpid" 2>/dev/null; wait "$wpid" 2>/dev/null || true + fail "captain hold: $act activity did not report question-blocked" + fi + printf 'working: continuing\n' >>"$d/state/t26.status" + local expected=idle + [ "$act" != busy ] || expected=working + if ! wait_cap "state=$expected:app=firstmate-opencode" "$d/cap" 100; then + kill "$wpid" 2>/dev/null; wait "$wpid" 2>/dev/null || true + fail "captain hold: superseding status did not restore $expected" + fi + kill "$wpid" 2>/dev/null; wait "$wpid" 2>/dev/null || true + done + pass "mapping: captain-held stays question-blocked until superseded" +} + +# A paused worker reports from its busy state like any other worker. +t_paused_reads_from_busy_state() { + local d=$TMP_ROOT/paused + mkhome "$d" + : >"$d/cap" + sleep 300 & + local holder=$! + busy_rec "$d/state" t24 idle + printf 'paused [at=%s]: waiting on CI\n' "$(date +%s)" >"$d/state/t24.status" + env "${CLEAN_UNSET[@]}" FM_OSC7501_TTY="$d/cap" FM_OSC7501_INTERVAL=1 \ + bash "$SCRIPT" watch --task t24 --kind ship --state "$d/state" --harness opencode \ + --parent "$holder" & + local wpid=$! + wait_cap 'state=idle' "$d/cap" 300 || fail "paused: an idle paused worker did not read as idle" + busy_rec "$d/state" t24 busy + wait_cap 'state=working' "$d/cap" 300 || fail "paused: a busy paused worker did not read as working" + kill "$wpid" 2>/dev/null; wait "$wpid" 2>/dev/null || true + assert_equals "state=idle state=working " "$(grep -o 'state=[a-z]*' "$d/cap" | sort -u | tr '\n' ' ')" \ + "a paused worker never reports a separate waiting state" + kill "$holder" 2>/dev/null; wait "$holder" 2>/dev/null || true + pass "mapping: a paused worker reports working or idle from its busy state" +} + +# A reporter still folding a long status history when its pane is relaunched +# finishes that fold and its final record late; the replacement must not arm +# until it is gone, so none of its writes can land after the handoff. +t_retire_waits_for_slow_reporter() { + local d=$TMP_ROOT/slow first second i + mkhome "$d" + start_term "$d" + for i in $(seq 1 100); do + printf 'needs-decision [at=%s] [key=k%s]: q%s\nresolved [at=%s] [key=k%s]: a%s\n' "$i" "$i" "$i" "$i" "$i" "$i" + done >"$d/state/t17.status" + busy_rec "$d/state" t17 busy + env "${CLEAN_UNSET[@]}" FM_OSC7501_TTY="$TERM_SLAVE" HC_TERMINAL_ID=x FM_OSC7501_INTERVAL=1 bash "$SCRIPT" arm \ + --task t17 --harness opencode --agent-bin opencode --backend tmux --kind ship \ + --state "$d/state" --config "$d/config" + first=$(watcher_pid "$d/state") || fail "slow: no first watcher" + wait_cap 'state=working' "$d/cap" 200 || fail "slow: the first reporter never reported" + env "${CLEAN_UNSET[@]}" FM_OSC7501_TTY="$TERM_SLAVE" HC_TERMINAL_ID=x FM_OSC7501_INTERVAL=1 bash "$SCRIPT" arm \ + --task t17 --harness opencode --agent-bin opencode --backend tmux --kind ship \ + --state "$d/state" --config "$d/config" + if kill -0 "$first" 2>/dev/null; then + kill -KILL "$first" 2>/dev/null + fail "slow: replacement armed while the old reporter could still write" + fi + second=$(watcher_pid "$d/state") || fail "slow: no replacement watcher" + [ "$first" != "$second" ] || fail "slow: replacement reused the old reporter" + kill -KILL "$second" 2>/dev/null + wait_gone "$second" || true + stop_term + pass "lifecycle: relaunch confirms the old reporter is gone before arming" +} + +# An hc terminal opened from inside a herdr session inherits HERDR_* markers; +# its own HC_TERMINAL_ID still makes it a known-aware reporter. +t_hc_terminal_with_inherited_herdr_markers() { + local d=$TMP_ROOT/hcinherit + mkhome "$d" + start_term "$d" + env "${CLEAN_UNSET[@]}" HC_TERMINAL_ID=t-2 HERDR_ENV=1 HERDR_PANE_ID=p-1 \ + FM_OSC7501_TTY="$TERM_SLAVE" bash "$SCRIPT" arm \ + --task t14 --harness opencode --agent-bin opencode --backend hc --kind ship \ + --state "$d/state" --config "$d/config" + local wpid + wpid=$(watcher_pid "$d/state") || fail "hc inside herdr: no watcher armed" + assert_equals "state=idle " "$(grep -o 'state=[a-z]*' "$d/cap" | sort -u | tr '\n' ' ')" \ + "hc inside herdr: the only bytes are the arm-time idle clear - no capability probe" + kill "$wpid" 2>/dev/null + wait_gone "$wpid" || true + stop_term + pass "known-aware: an hc terminal reports despite inherited HERDR markers" +} + +fm_test_run \ + t_unknown_terminal_preserves_input \ + t_ghostty_arms_watcher \ + t_status_transitions_and_final_done \ + t_final_idle_after_working \ + t_native_claude_skips_everything \ + t_term_resistant_probe_allows_worker_launch \ + t_old_claude_uses_generic \ + t_launch_without_agent_bin_skips \ + t_inplace_upgrade_reprobes_native \ + t_native_codex_threshold \ + t_unknown_activity_leaves_native_root \ + t_unknown_activity_keeps_record_honest \ + t_final_record_with_unknown_activity \ + t_rovo_reports_through_unknown \ + t_agy_reports_through_unknown \ + t_arm_clears_stale_record \ + t_initial_unknown_launch_reports \ + t_stale_terminal_line_parks_idle \ + t_unarmed_relaunch_clears_leftover \ + t_killed_watcher_still_gets_final \ + t_early_term_watcher_still_gets_final \ + t_config_off_disables \ + t_hc_marker_arms_watcher \ + t_concurrent_panes_keep_their_reporters \ + t_disarm_after_agent_exit_parks_idle \ + t_rearm_retires_previous_reporter \ + t_cursor_transcript_drives_activity \ + t_grok_pane_drives_activity \ + t_turn_then_terminal_line \ + t_paused_reads_from_busy_state \ + t_captain_hold_stays_question_blocked \ + t_retire_waits_for_slow_reporter \ + t_hc_terminal_with_inherited_herdr_markers diff --git a/tests/fm-pr-check-security.test.sh b/tests/fm-pr-check-security.test.sh index 83903b4b428..7d0a0ac3c75 100755 --- a/tests/fm-pr-check-security.test.sh +++ b/tests/fm-pr-check-security.test.sh @@ -2112,10 +2112,8 @@ SH FM_TEST_CHECK_TIMEOUT=1 FM_CHECK_FORCE_FALLBACK="$force_fallback" \ run_watcher_bounded "$dir/home" "$dir/fakebin" > "$dir/watch.out" 2> "$dir/watch.err" || rc=$? [ "$rc" -eq 0 ] || fail "$backend watcher did not stop on the timed-out check (rc=$rc): $(cat "$dir/watch.err")" - grep -qxF "check: $state/custom.check.sh: finding before the cut" "$dir/watch.out" \ - || fail "$backend watcher dropped output printed before the timeout: $(cat "$dir/watch.out")" - grep -qxF 'timed out after 1s' "$dir/watch.out" \ - || fail "$backend watcher kept a timed-out check silent: $(cat "$dir/watch.out")" + grep -qxF "check: $state/custom.check.sh: finding before the cut timed out after 1s" "$dir/watch.out" \ + || fail "$backend watcher dropped earlier output or the timeout from its one wake line: $(cat "$dir/watch.out")" ! grep -qF 'finding after the cut' "$dir/watch.out" || fail "$backend watcher let the check run past its timeout" done pass "a timed-out custom check surfaces its timeout and earlier output on installed and fallback timeout paths" diff --git a/tests/fm-pr-merge.test.sh b/tests/fm-pr-merge.test.sh index 8bd25d246d8..579e07fabeb 100755 --- a/tests/fm-pr-merge.test.sh +++ b/tests/fm-pr-merge.test.sh @@ -187,23 +187,31 @@ case "${1:-} ${2:-}" in "pr view") case " $* " in *statusCheckRollup*) + view_json=$FM_TEST_GH_VIEW_JSON if [ -f "${FM_TEST_GH_UPDATE_VIEW:-}" ] && [ -f "${FM_TEST_GH_UPDATE_DONE:-}" ]; then - cat "$FM_TEST_GH_UPDATE_VIEW" - if [ -f "${FM_TEST_GH_UPDATE_FINAL_VIEW:-}" ]; then - cp "$FM_TEST_GH_UPDATE_FINAL_VIEW" "$FM_TEST_GH_UPDATE_VIEW" - fi - elif [ -n "${FM_TEST_GH_MERGEABLE_SEQUENCE:-}" ]; then + view_json=$FM_TEST_GH_UPDATE_VIEW + fi + if [ -n "${FM_TEST_GH_MERGEABLE_SEQUENCE:-}" ]; then call_n=$(( $(cat "$FM_TEST_GH_MERGEABLE_CALLS" 2>/dev/null || echo 0) + 1 )) printf '%s\n' "$call_n" > "$FM_TEST_GH_MERGEABLE_CALLS" call_m=$(sed -n "${call_n}p" "$FM_TEST_GH_MERGEABLE_SEQUENCE") [ -n "$call_m" ] || call_m=$(tail -n1 "$FM_TEST_GH_MERGEABLE_SEQUENCE") # An optional second word overrides the first check's conclusion. read -r call_m call_c <<< "$call_m" - jq -c --arg m "$call_m" --arg c "${call_c:-}" \ - '.mergeable = $m | if $c != "" then .statusCheckRollup[0].conclusion = $c else . end' \ - "$FM_TEST_GH_VIEW_JSON" + call_h= + view_dir=$(dirname "$FM_TEST_GH_VIEW_JSON") + if [ -f "$view_dir/github-head-by-call" ]; then + call_h=$(sed -n "${call_n}p" "$view_dir/github-head-by-call") + fi + jq -c --arg m "$call_m" --arg c "${call_c:-}" --arg h "$call_h" \ + '.mergeable = $m | if $h != "" then .headRefOid = $h else . end | if $c != "" then .statusCheckRollup[0].conclusion = $c else . end' \ + "$view_json" else - cat "$FM_TEST_GH_VIEW_JSON" + cat "$view_json" + fi + if [ -f "${FM_TEST_GH_UPDATE_VIEW:-}" ] && [ -f "${FM_TEST_GH_UPDATE_DONE:-}" ] \ + && [ -f "${FM_TEST_GH_UPDATE_FINAL_VIEW:-}" ]; then + cp "$FM_TEST_GH_UPDATE_FINAL_VIEW" "$FM_TEST_GH_UPDATE_VIEW" fi if [ -f "${FM_TEST_AWAY_RECORD_AFTER_VIEW:-}" ]; then if [ -s "${FM_TEST_AWAY_RECORD_AFTER_VIEW}" ]; then @@ -212,6 +220,11 @@ case "${1:-} ${2:-}" in rm -f "$FM_STATE_OVERRIDE/.afk-contract" fi fi + view_dir=$(dirname "$FM_TEST_GH_VIEW_JSON") + if [ -f "$view_dir/github-head-after-view" ]; then + cp "$view_dir/github-head-after-view" "$view_dir/github-server-head" + rm -f "$view_dir/github-head-after-view" + fi exit 0 ;; *headRefOid*) @@ -245,6 +258,21 @@ case "${1:-} ${2:-}" in > "$FM_TEST_AWAY_WORDS_AT_MERGE" 2>/dev/null \ || printf 'no-live-record\n' > "$FM_TEST_AWAY_WORDS_AT_MERGE" fi + view_dir=$(dirname "$FM_TEST_GH_VIEW_JSON") + if [ -f "$view_dir/github-server-head" ]; then + match= + prev= + for arg in "$@"; do + if [ "$prev" = "--match-head-commit" ]; then + match=$arg + fi + prev=$arg + done + if [ -n "$match" ] && [ "$match" != "$(cat "$view_dir/github-server-head")" ]; then + printf 'error: head out of date\n' >&2 + exit 1 + fi + fi if [ -n "${FM_TEST_GH_MERGE_OUTPUT:-}" ]; then printf '%s\n' "$FM_TEST_GH_MERGE_OUTPUT" else @@ -280,6 +308,25 @@ case "${1:-} ${2:-}" in # merge-method rules read answers from its own fixture file so the # merge-queue fixture below keeps driving only the queue read. case " $* " in + *" repos/"*"/pulls/"*"/update-branch "*) + [ "${2:-}" = --method ] && [ "${3:-}" = PUT ] || exit 2 + expected= + for arg in "$@"; do + case "$arg" in expected_head_sha=*) expected=${arg#expected_head_sha=} ;; esac + done + [ -n "$expected" ] || exit 2 + server_head="$(dirname "$FM_TEST_GH_VIEW_JSON")/github-server-head" + current=$(cat "$server_head" 2>/dev/null || cat "$FM_TEST_GH_HEAD") + if [ "$current" != "$expected" ]; then + printf 'gh: Head branch was modified. Review and try again. (HTTP 422)\n' >&2 + exit 1 + fi + cp "$FM_TEST_GH_UPDATE_HEAD" "$FM_TEST_GH_HEAD" + cp "$FM_TEST_GH_UPDATE_HEAD" "$server_head" + : > "$FM_TEST_GH_UPDATE_DONE" + printf '{"message":"Updating pull request branch."}\n' + exit 0 + ;; *" repos/"*"/commits/"*"/check-runs"*) case "$*" in *"/commits/$(cat "$FM_TEST_GH_HEAD")/check-runs"*) ;; @@ -310,6 +357,10 @@ case "${1:-} ${2:-}" in exit 0 ;; *" repos/"*"/branches/"*) + if [ -f "$FM_TEST_GH_BRANCH_FAIL-after-update" ] && [ -f "$FM_TEST_GH_UPDATE_DONE" ]; then + cat "$FM_TEST_GH_BRANCH_FAIL-after-update" >&2 + exit 1 + fi if [ -f "${FM_TEST_GH_BRANCH_FAIL:-}" ]; then cat "$FM_TEST_GH_BRANCH_FAIL" >&2 exit 1 @@ -398,10 +449,28 @@ case "${1:-} ${2:-}" in cat "$case_dir/mr-post.json" else cat "$FM_TEST_GLAB_JSON" + if [ -f "$case_dir/glab-head-after-view" ]; then + cp "$case_dir/glab-head-after-view" "$case_dir/glab-server-head" + rm -f "$case_dir/glab-head-after-view" + fi fi exit 0 ;; "mr merge") + if [ -f "$case_dir/glab-server-head" ]; then + sha= + prev= + for arg in "$@"; do + if [ "$prev" = "--sha" ]; then + sha=$arg + fi + prev=$arg + done + if [ -n "$sha" ] && [ "$sha" != "$(cat "$case_dir/glab-server-head")" ]; then + echo "error: head out of date" >&2 + exit 1 + fi + fi [ ! -e "$case_dir/glab-merge-fails" ] || { echo "error: mr merge failed" >&2 ; exit 1 ; } : > "$case_dir/glab-merge-called" exit 0 @@ -555,6 +624,10 @@ case "$endpoint" in cat "$log_dir/pr-post.json" else cat "$log_dir/pr.json" + if [ -f "$log_dir/tea-head-after-view" ]; then + cp "$log_dir/tea-head-after-view" "$log_dir/tea-expected-head" + rm -f "$log_dir/tea-head-after-view" + fi fi ;; esac @@ -2986,6 +3059,423 @@ test_github_still_forwards_sha_arg() { pass "fm-pr-merge refuses a caller --sha on GitHub because the head comes from the live read" } +# --expected-head is optional. These cases pin the caller-supplied commit on +# every forge the script merges, and they refuse when that commit is not the +# one the forge write would bind. +test_expected_head_matching_reaches_each_forge() { + local case_dir rc merge_line old long + old=bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb + long=0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef + [ "${#old}" -eq 40 ] || fail "expected-head-match: fixture sha is not 40 characters" + [ "${#long}" -eq 64 ] || fail "expected-head-match: fixture sha is not 64 characters" + + case_dir=$(make_case expected-head-gh-match) + mkdir -p "$case_dir/wt" + add_gh_mocks "$case_dir" "$old" + : > "$case_dir/gh-axi.log" + run_pr_merge "$case_dir" task-x1 https://github.com/example/repo/pull/201 \ + --expected-head "$old" \ + > "$case_dir/stdout" 2> "$case_dir/stderr" \ + || fail "expected-head-gh-match: a matching head should merge: $(cat "$case_dir/stderr")" + assert_logged_gh_merge "$case_dir" 201 example/repo --squash + assert_grep "green at head $old" "$case_dir/stderr" \ + "expected-head-gh-match: the verified line did not name the supplied head" + + case_dir=$(make_case expected-head-gh-match-64) + mkdir -p "$case_dir/wt" + add_gh_mocks "$case_dir" "$long" + : > "$case_dir/gh-axi.log" + run_pr_merge "$case_dir" task-x1 https://github.com/example/repo/pull/202 \ + --expected-head "$long" \ + > "$case_dir/stdout" 2> "$case_dir/stderr" \ + || fail "expected-head-gh-match-64: a 64-character head should merge: $(cat "$case_dir/stderr")" + assert_logged_gh_merge "$case_dir" 202 example/repo --squash + + case_dir=$(make_gitlab_case expected-head-gl-match) + run_pr_merge "$case_dir" task-x1 "$MR_URL" --expected-head "$MR_HEAD" \ + > "$case_dir/stdout" 2> "$case_dir/stderr" \ + || fail "expected-head-gl-match: a matching head should merge: $(cat "$case_dir/stderr")" + merge_line=$(glab_merge_line "$case_dir/glab.log") + [ "$merge_line" = "GITLAB_HOST=$MR_HOST mr merge 7 -R $MR_PROJECT_URL --sha $MR_HEAD --yes" ] \ + || fail "expected-head-gl-match: the forge was not bound to the supplied head: '$merge_line'" + + case_dir=$(make_forgejo_case expected-head-fj-match) + run_pr_merge "$case_dir" task-x1 "$FJ_URL" --expected-head "$FJ_HEAD" \ + > "$case_dir/stdout" 2> "$case_dir/stderr" \ + || fail "expected-head-fj-match: a matching head should merge: $(cat "$case_dir/stderr")" + merge_line=$(tea_merge_line "$case_dir/tea.log") + case "$merge_line" in + *"head_commit_id=$FJ_HEAD"*) : ;; + *) fail "expected-head-fj-match: the forge was not bound to the supplied head: '$merge_line'" ;; + esac + pass "fm-pr-merge sends a matching --expected-head to each forge" +} + +test_expected_head_mismatch_refuses_before_merge() { + local case_dir rc old new + old=bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb + new=dddddddddddddddddddddddddddddddddddddddd + + case_dir=$(make_case expected-head-gh-before) + mkdir -p "$case_dir/wt" + add_gh_mocks "$case_dir" "$new" + : > "$case_dir/gh-axi.log" + set +e + run_pr_merge "$case_dir" task-x1 https://github.com/example/repo/pull/203 \ + --expected-head "$old" \ + > "$case_dir/stdout" 2> "$case_dir/stderr" + rc=$? + set -e + expect_code 1 "$rc" "expected-head-gh-before: a replaced head must refuse" + assert_grep "the live head is \"$new\", not the expected head $old" "$case_dir/stderr" \ + "expected-head-gh-before: the refusal did not name both heads" + assert_no_grep 'pr merge' "$case_dir/gh.log" \ + "expected-head-gh-before: gh pr merge ran after the live head moved" + + case_dir=$(make_gitlab_case expected-head-gl-before \ + head="$MR_STALE_HEAD" pipeline_sha="$MR_STALE_HEAD") + set +e + run_pr_merge "$case_dir" task-x1 "$MR_URL" --expected-head "$MR_HEAD" \ + > "$case_dir/stdout" 2> "$case_dir/stderr" + rc=$? + set -e + expect_code 1 "$rc" "expected-head-gl-before: a replaced head must refuse" + assert_grep "the live head is \"$MR_STALE_HEAD\", not the expected head $MR_HEAD" \ + "$case_dir/stderr" "expected-head-gl-before: the refusal did not name both heads" + [ -z "$(glab_merge_line "$case_dir/glab.log")" ] \ + || fail "expected-head-gl-before: glab mr merge ran after the live head moved" + + case_dir=$(make_forgejo_case expected-head-fj-before head="$FJ_STALE_HEAD") + set +e + run_pr_merge "$case_dir" task-x1 "$FJ_URL" --expected-head "$FJ_HEAD" \ + > "$case_dir/stdout" 2> "$case_dir/stderr" + rc=$? + set -e + expect_code 1 "$rc" "expected-head-fj-before: a replaced head must refuse" + assert_grep "the live head is \"$FJ_STALE_HEAD\", not the expected head $FJ_HEAD" \ + "$case_dir/stderr" "expected-head-fj-before: the refusal did not name both heads" + [ -z "$(tea_merge_line "$case_dir/tea.log")" ] \ + || fail "expected-head-fj-before: a Forgejo merge ran after the live head moved" + pass "fm-pr-merge refuses before any merge write when the live head differs from --expected-head" +} + +test_expected_head_push_after_verify_is_refused_by_the_forge() { + local case_dir rc merge_line old new + old=bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb + new=dddddddddddddddddddddddddddddddddddddddd + + case_dir=$(make_case expected-head-gh-after) + mkdir -p "$case_dir/wt" + add_gh_mocks "$case_dir" "$old" + printf '%s\n' "$new" > "$case_dir/github-head-after-view" + write_github_outcome "$case_dir" OPEN false false main + : > "$case_dir/gh-axi.log" + set +e + run_pr_merge "$case_dir" task-x1 https://github.com/example/repo/pull/204 \ + --expected-head "$old" \ + > "$case_dir/stdout" 2> "$case_dir/stderr" + rc=$? + set -e + expect_code 1 "$rc" "expected-head-gh-after: the forge must refuse the stale supplied head" + assert_logged_gh_merge "$case_dir" 204 example/repo --squash + assert_grep 'head out of date' "$case_dir/stderr" \ + "expected-head-gh-after: the forge refusal was not surfaced" + assert_no_grep "--match-head-commit $new" "$case_dir/gh.log" \ + "expected-head-gh-after: the merge was bound to the head that landed after verification" + + case_dir=$(make_gitlab_case expected-head-gl-after) + printf '%s\n' "$MR_STALE_HEAD" > "$case_dir/glab-head-after-view" + set +e + run_pr_merge "$case_dir" task-x1 "$MR_URL" --expected-head "$MR_HEAD" \ + > "$case_dir/stdout" 2> "$case_dir/stderr" + rc=$? + set -e + expect_code 1 "$rc" "expected-head-gl-after: the forge must refuse the stale supplied head" + merge_line=$(glab_merge_line "$case_dir/glab.log") + [ "$merge_line" = "GITLAB_HOST=$MR_HOST mr merge 7 -R $MR_PROJECT_URL --sha $MR_HEAD --yes" ] \ + || fail "expected-head-gl-after: the forge was not asked for the supplied head: '$merge_line'" + assert_grep 'head out of date' "$case_dir/stderr" \ + "expected-head-gl-after: the forge refusal was not surfaced" + + case_dir=$(make_forgejo_case expected-head-fj-after) + printf '%s\n' "$FJ_STALE_HEAD" > "$case_dir/tea-head-after-view" + set +e + run_pr_merge "$case_dir" task-x1 "$FJ_URL" --expected-head "$FJ_HEAD" \ + > "$case_dir/stdout" 2> "$case_dir/stderr" + rc=$? + set -e + expect_code 1 "$rc" "expected-head-fj-after: the forge must refuse the stale supplied head" + merge_line=$(tea_merge_line "$case_dir/tea.log") + case "$merge_line" in + *"head_commit_id=$FJ_HEAD"*) : ;; + *) fail "expected-head-fj-after: the forge was not asked for the supplied head: '$merge_line'" ;; + esac + assert_no_grep "head_commit_id=$FJ_STALE_HEAD" "$case_dir/tea.log" \ + "expected-head-fj-after: the merge was bound to the head that landed after verification" + assert_grep 'Forgejo did not confirm' "$case_dir/stderr" \ + "expected-head-fj-after: a rejected forge match was treated as merged" + pass "fm-pr-merge binds the forge write to the supplied head after that head moves" +} + +test_expected_head_later_preflight_does_not_adopt_a_new_head() { + local case_dir rc old new + old=bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb + new=dddddddddddddddddddddddddddddddddddddddd + case_dir=$(make_case expected-head-gh-preflight) + mkdir -p "$case_dir/wt" + add_gh_mocks "$case_dir" "$old" + printf '%s\n' UNKNOWN MERGEABLE > "$case_dir/mergeable-sequence" + printf '%s\n' "$old" "$new" > "$case_dir/github-head-by-call" + : > "$case_dir/gh-axi.log" + : > "$case_dir/gh.log" + + set +e + FM_TEST_GH_MERGEABLE_SEQUENCE="$case_dir/mergeable-sequence" \ + FM_PR_GITHUB_MERGEABLE_RETRY_DELAY=0 \ + run_pr_merge "$case_dir" task-x1 https://github.com/example/repo/pull/205 \ + --expected-head "$old" \ + > "$case_dir/stdout" 2> "$case_dir/stderr" + rc=$? + set -e + expect_code 1 "$rc" "expected-head-gh-preflight: a head that moves between preflight reads must refuse" + assert_grep "the live head is \"$new\", not the expected head $old" "$case_dir/stderr" \ + "expected-head-gh-preflight: the later read was not compared with the supplied head" + assert_no_grep 'pr merge' "$case_dir/gh.log" \ + "expected-head-gh-preflight: a later preflight adopted the new head and merged it" + [ "$(grep -c '^pr view .*statusCheckRollup' "$case_dir/gh.log")" -eq 2 ] \ + || fail "expected-head-gh-preflight: expected exactly 2 live reads, got $(grep -c '^pr view .*statusCheckRollup' "$case_dir/gh.log")" + pass "fm-pr-merge does not replace --expected-head when a later preflight reads a new head" +} + +test_expected_head_bad_inputs_refuse_before_effects() { + local case_dir rc name code needle full other + full=bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb + other=dddddddddddddddddddddddddddddddddddddddd + for name in malformed short7 short12 upper missing equals equals-full twice \ + conflict-sha conflict-match; do + case_dir=$(make_case "expected-head-bad-$name") + mkdir -p "$case_dir/wt" + add_gh_mocks "$case_dir" "$full" + : > "$case_dir/gh-axi.log" + case "$name" in + malformed) + code=2 + needle='full lowercase commit sha' + set -- --expected-head 'zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz' + ;; + short7) + code=2 + needle='full lowercase commit sha' + set -- --expected-head abc1234 + ;; + short12) + code=2 + needle='full lowercase commit sha' + set -- --expected-head abcdef123456 + ;; + upper) + code=2 + needle='full lowercase commit sha' + set -- --expected-head BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB + ;; + missing) + code=2 + needle='requires a full commit sha' + set -- --expected-head + ;; + equals) + code=2 + needle='separate full commit sha argument' + set -- --expected-head= + ;; + equals-full) + code=2 + needle='separate full commit sha argument' + set -- "--expected-head=$full" + ;; + twice) + code=2 + needle='may be specified only once' + set -- --expected-head "$full" --expected-head "$other" + ;; + conflict-sha) + code=1 + needle='must not override the head commit' + set -- --expected-head "$full" -- --sha "$other" + ;; + conflict-match) + code=1 + needle='must not override the head commit' + set -- --expected-head "$full" -- --match-head-commit "$other" + ;; + esac + set +e + run_pr_merge "$case_dir" task-x1 https://github.com/example/repo/pull/210 "$@" \ + > "$case_dir/stdout" 2> "$case_dir/stderr" + rc=$? + set -e + expect_code "$code" "$rc" "expected-head-bad-$name" + assert_grep "$needle" "$case_dir/stderr" \ + "expected-head-bad-$name: refusal did not name the bad input" + assert_no_grep 'pr=' "$case_dir/state/task-x1.meta" \ + "expected-head-bad-$name: state was recorded before the bad input was refused" + assert_absent "$case_dir/state/task-x1.check.sh" \ + "expected-head-bad-$name: a bad input armed a merge poll" + assert_no_grep 'pr merge' "$case_dir/gh.log" \ + "expected-head-bad-$name: gh pr merge ran for a bad input" + done + pass "fm-pr-merge refuses a malformed, abbreviated, missing, or conflicting --expected-head before any effect" +} + +test_expected_head_keeps_existing_refusals() { + local case_dir rc head + head=bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb + + case_dir=$(make_case expected-head-draft) + mkdir -p "$case_dir/wt" + add_gh_mocks "$case_dir" "$head" + jq -c '.isDraft = true' "$case_dir/github-view.json" > "$case_dir/github-view.tmp" + mv "$case_dir/github-view.tmp" "$case_dir/github-view.json" + set +e + run_pr_merge "$case_dir" task-x1 https://github.com/example/repo/pull/211 \ + --expected-head "$head" \ + > "$case_dir/stdout" 2> "$case_dir/stderr" + rc=$? + set -e + expect_code 1 "$rc" "expected-head-draft: a draft must still refuse" + assert_grep 'the pull request is a draft' "$case_dir/stderr" \ + "expected-head-draft: the draft refusal was skipped" + assert_no_grep 'pr merge' "$case_dir/gh.log" \ + "expected-head-draft: gh pr merge ran on a draft" + + case_dir=$(make_case expected-head-red) + mkdir -p "$case_dir/wt" + add_gh_mocks "$case_dir" "$head" + write_github_red_json "$case_dir" "$head" lint + set +e + run_pr_merge "$case_dir" task-x1 https://github.com/example/repo/pull/212 \ + --expected-head "$head" \ + > "$case_dir/stdout" 2> "$case_dir/stderr" + rc=$? + set -e + expect_code 1 "$rc" "expected-head-red: a red check must still refuse" + assert_grep "check 'lint' is not green" "$case_dir/stderr" \ + "expected-head-red: the red check was skipped" + assert_no_grep 'pr merge' "$case_dir/gh.log" \ + "expected-head-red: gh pr merge ran on a red check" + + case_dir=$(make_case expected-head-required) + mkdir -p "$case_dir/wt" + add_gh_mocks "$case_dir" "$head" + write_github_required "$case_dir" classic:ci classic:validate + set +e + run_pr_merge "$case_dir" task-x1 https://github.com/example/repo/pull/213 \ + --expected-head "$head" \ + > "$case_dir/stdout" 2> "$case_dir/stderr" + rc=$? + set -e + expect_code 1 "$rc" "expected-head-required: a missing required check must still refuse" + assert_grep "required check 'validate' has not reported at head $head" "$case_dir/stderr" \ + "expected-head-required: the missing required check was skipped" + assert_no_grep 'pr merge' "$case_dir/gh.log" \ + "expected-head-required: gh pr merge ran with a required check unreported" + + case_dir=$(make_case expected-head-hold) + mkdir -p "$case_dir/wt" + add_gh_mocks "$case_dir" "$head" + # Queued is enough. The hold check reads the captain-hold mark, and + # fm-tasks-axi.sh refuses add --start without a dispatch record. + FM_HOME="$case_dir/home" FM_STATE_OVERRIDE="$case_dir/state" \ + "$ROOT/bin/fm-tasks-axi.sh" add task-x1 "Held merge" --kind ship --repo example \ + > "$case_dir/hold-add.out" 2> "$case_dir/hold-add.err" \ + || fail "expected-head-hold: could not add the task: $(cat "$case_dir/hold-add.err")" + FM_HOME="$case_dir/home" FM_STATE_OVERRIDE="$case_dir/state" \ + "$ROOT/bin/fm-captain-hold.sh" hold task-x1 --reason "captain merge approval pending" \ + > "$case_dir/hold.out" 2> "$case_dir/hold.err" \ + || fail "expected-head-hold: could not hold the task: $(cat "$case_dir/hold.err")" + set +e + run_pr_merge "$case_dir" task-x1 https://github.com/example/repo/pull/214 \ + --expected-head "$head" \ + > "$case_dir/stdout" 2> "$case_dir/stderr" + rc=$? + set -e + expect_code 1 "$rc" "expected-head-hold: a captain hold must still refuse" + assert_grep 'still held for the captain' "$case_dir/stderr" \ + "expected-head-hold: the hold refusal was skipped" + assert_no_grep 'pr merge' "$case_dir/gh.log" \ + "expected-head-hold: gh pr merge ran while the task was held" + + case_dir=$(make_gitlab_case expected-head-review discussions=false) + set +e + run_pr_merge "$case_dir" task-x1 "$MR_URL" --expected-head "$MR_HEAD" \ + > "$case_dir/stdout" 2> "$case_dir/stderr" + rc=$? + set -e + expect_code 1 "$rc" "expected-head-review: unresolved review discussion must still refuse" + assert_grep 'blocking_discussions_resolved is "false", not true' "$case_dir/stderr" \ + "expected-head-review: the discussion refusal was skipped" + [ -z "$(glab_merge_line "$case_dir/glab.log")" ] \ + || fail "expected-head-review: glab mr merge ran with review discussion open" + + case_dir=$(make_forgejo_case expected-head-fj-draft draft=true) + set +e + run_pr_merge "$case_dir" task-x1 "$FJ_URL" --expected-head "$FJ_HEAD" \ + > "$case_dir/stdout" 2> "$case_dir/stderr" + rc=$? + set -e + expect_code 1 "$rc" "expected-head-fj-draft: a Forgejo draft must still refuse" + assert_grep 'the pull request is a draft' "$case_dir/stderr" \ + "expected-head-fj-draft: the draft refusal was skipped" + [ -z "$(tea_merge_line "$case_dir/tea.log")" ] \ + || fail "expected-head-fj-draft: a Forgejo merge ran on a draft" + + case_dir=$(make_forgejo_case expected-head-fj-status) + write_forgejo_status_json "$case_dir/tea-status.json" state=failure + set +e + run_pr_merge "$case_dir" task-x1 "$FJ_URL" --expected-head "$FJ_HEAD" \ + > "$case_dir/stdout" 2> "$case_dir/stderr" + rc=$? + set -e + expect_code 1 "$rc" "expected-head-fj-status: a failed combined status must still refuse" + assert_grep 'the combined commit status is "failure", not success' "$case_dir/stderr" \ + "expected-head-fj-status: the status refusal was skipped" + [ -z "$(tea_merge_line "$case_dir/tea.log")" ] \ + || fail "expected-head-fj-status: a Forgejo merge ran on a failed status" + + case_dir=$(make_case expected-head-away) + mkdir -p "$case_dir/wt" + add_gh_mocks "$case_dir" "$head" + write_away_record "$case_dir" --words 'merge task-x1 when green' + set +e + run_pr_merge "$case_dir" task-x1 https://github.com/example/repo/pull/215 \ + --expected-head "$head" --allow-red lint \ + > "$case_dir/stdout" 2> "$case_dir/stderr" + rc=$? + set -e + expect_code 2 "$rc" "expected-head-away: an attended-only waiver while away must still refuse" + assert_grep '--allow-red is attended-only' "$case_dir/stderr" \ + "expected-head-away: the authority refusal was skipped" + assert_no_grep 'pr merge' "$case_dir/gh.log" \ + "expected-head-away: gh pr merge ran against the away authority" + + case_dir=$(make_case expected-head-identity) + mkdir -p "$case_dir/wt" + add_gh_mocks "$case_dir" "$head" + printf '\npr=https://github.com/example/repo/pull/99\n' >> "$case_dir/state/task-x1.meta" + set +e + run_pr_merge "$case_dir" task-x1 https://github.com/example/repo/pull/216 \ + --expected-head "$head" \ + > "$case_dir/stdout" 2> "$case_dir/stderr" + rc=$? + set -e + expect_code 1 "$rc" "expected-head-identity: a different recorded PR must still refuse" + assert_grep 'is bound to https://github.com/example/repo/pull/99' "$case_dir/stderr" \ + "expected-head-identity: the task identity refusal was skipped" + assert_no_grep 'pr merge' "$case_dir/gh.log" \ + "expected-head-identity: gh pr merge ran for a task bound to another PR" + pass "fm-pr-merge keeps draft, hold, review, check, authority, and identity refusals when --expected-head matches" +} + # --- durable merge outcome --------------------------------------------------- # A merge that lands must leave a record outside the merging agent's memory. # bin/fm-merge-outcome-lib.sh owns where that record goes; these cases pin the @@ -3300,11 +3790,113 @@ test_secondmate_without_parent_binding_is_loud() { pass "a secondmate home that cannot report upward says so instead of merging in silence" } +test_github_behind_update_refuses_concurrent_head_change() { + local case_dir rc scenario old_head raced_head updated_head actual_head + old_head=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + raced_head=cccccccccccccccccccccccccccccccccccccccc + updated_head=bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb + for scenario in no-pin expected-pin; do + case_dir=$(make_case "github-behind-concurrent-$scenario") + add_gh_mocks "$case_dir" "$old_head" + jq '.mergeStateStatus = "BEHIND"' "$case_dir/github-view.json" > "$case_dir/view.tmp" + mv "$case_dir/view.tmp" "$case_dir/github-view.json" + jq --arg head "$updated_head" '.mergeStateStatus = "CLEAN" | .headRefOid = $head' \ + "$case_dir/github-view.json" > "$case_dir/github-view-after-update.json" + printf '%s\n' "$raced_head" > "$case_dir/github-head-after-view" + printf '%s\n' "$updated_head" > "$case_dir/github-head-after-update" + set -- --update-branch + [ "$scenario" != expected-pin ] || set -- "$@" --expected-head "$old_head" + set +e + run_pr_merge "$case_dir" task-x1 https://github.com/example/repo/pull/6189 "$@" \ + > "$case_dir/stdout" 2> "$case_dir/stderr" + rc=$? + set -e + actual_head=$(cat "$case_dir/github-server-head") + [ "$actual_head" = "$raced_head" ] \ + || fail "github-behind-concurrent-$scenario: unchecked replacement head was changed to $actual_head; expected $raced_head" + expect_code 1 "$rc" "github-behind-concurrent-$scenario: a concurrent push must refuse" + assert_grep 'HTTP 422' "$case_dir/stderr" \ + "github-behind-concurrent-$scenario: refusal must surface the head-mismatch response" + assert_absent "$case_dir/github-update-done" \ + "github-behind-concurrent-$scenario: the rejected update changed the source branch" + assert_no_grep 'pr merge' "$case_dir/gh.log" \ + "github-behind-concurrent-$scenario: merged after the head mismatch" + [ "$(grep -c '/pulls/6189/update-branch' "$case_dir/gh.log")" -eq 1 ] \ + || fail "github-behind-concurrent-$scenario: must make one bound request without retrying the replacement head" + assert_grep "api --method PUT repos/example/repo/pulls/6189/update-branch -f expected_head_sha=$old_head" \ + "$case_dir/gh.log" "github-behind-concurrent-$scenario: update did not name the preflight head" + done + pass "fm-pr-merge refuses a concurrent source-head change before updating, with or without --expected-head" +} + +test_github_updated_head_other_refusals_stop_waiting() { + local case_dir rc blocker waiting visibility visible_head reason reads advice violations='' + local old_head=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + local new_head=bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb + for visibility in updated delayed; do + visible_head=$new_head + [ "$visibility" != delayed ] || visible_head=$old_head + for blocker in draft closed conflict required-source; do + for waiting in pending missing; do + case_dir=$(make_case "github-behind-blocker-$visibility-$blocker-$waiting") + add_gh_mocks "$case_dir" "$old_head" + write_github_required "$case_dir" ruleset:ci + jq '.mergeStateStatus = "BEHIND"' "$case_dir/github-view.json" > "$case_dir/view.tmp" + mv "$case_dir/view.tmp" "$case_dir/github-view.json" + jq --arg head "$visible_head" --arg waiting "$waiting" --arg blocker "$blocker" ' + .mergeStateStatus = "CLEAN" | .headRefOid = $head | + .statusCheckRollup = (if $waiting == "pending" then + [{"__typename":"CheckRun","name":"ci","status":"IN_PROGRESS","conclusion":null}] + else [] end) | + if $blocker == "draft" then .isDraft = true + elif $blocker == "closed" then .state = "CLOSED" + elif $blocker == "conflict" then .mergeable = "CONFLICTING" | .mergeStateStatus = "DIRTY" + else . end' "$case_dir/github-view.json" > "$case_dir/github-view-after-update.json" + printf '%s\n' "$visible_head" > "$case_dir/github-head-after-update" + case "$blocker" in + draft) reason='the pull request is a draft' ;; + closed) reason='state is "CLOSED", not open' ;; + conflict) reason='mergeStateStatus is DIRTY' ;; + required-source) + reason='the branch protection summary for base branch main could not be read' + printf '%s\n' 'HTTP 502: required-check source unavailable' > "$case_dir/github-branch-fail-after-update" + ;; + esac + set +e + FM_PR_GITHUB_MERGEABLE_RETRY_DELAY=1 \ + run_pr_merge "$case_dir" task-x1 https://github.com/example/repo/pull/6190 --update-branch \ + > "$case_dir/stdout" 2> "$case_dir/stderr" + rc=$? + set -e + expect_code 1 "$rc" "$visibility-$blocker-$waiting: the updated head must refuse" + assert_grep "$reason" "$case_dir/stderr" "$visibility-$blocker-$waiting: missing the actual refusal reason" + assert_no_grep 'pr merge' "$case_dir/gh.log" "$visibility-$blocker-$waiting: merged despite a blocker" + assert_absent "$case_dir/state/.control-task-x1.lock" "$visibility-$blocker-$waiting: task control lock remained held" + assert_absent "$case_dir/state/.afk-contract.lock" "$visibility-$blocker-$waiting: away-authority lock remained held" + [ "$(grep -c '/pulls/6190/update-branch' "$case_dir/gh.log")" -eq 1 ] \ + || fail "$visibility-$blocker-$waiting: the update request was repeated" + reads=$(grep -c 'statusCheckRollup' "$case_dir/gh.log") + advice=false + if grep -q 'checks are not ready\|once its checks pass' "$case_dir/stderr"; then advice=true; fi + printf 'observation: visibility=%s blocker=%s waiting=%s preflight_reads=%s retry_advice=%s\n' \ + "$visibility" "$blocker" "$waiting" "$reads" "$advice" + # Collect every masking combination before reporting a regression failure. + [ "$reads" -eq 2 ] || violations="$violations $visibility-$blocker-$waiting retried after the blocker;" + [ "$advice" = false ] || violations="$violations $visibility-$blocker-$waiting suggested waiting for checks;" + done + done + done + [ -z "$violations" ] || fail "$violations" + pass "fm-pr-merge immediately refuses draft, closed, conflicting, or unreadable updated heads despite pending or missing checks" +} + if [ "$#" -gt 0 ]; then for test_case in "$@"; do "$test_case"; done exit 0 fi +test_github_updated_head_other_refusals_stop_waiting +test_github_behind_update_refuses_concurrent_head_change test_github_zero_exit_queue_required_refuses_with_exact_retry test_github_closed_unqueued_outcome_omits_retry_flags test_github_agreeing_queue_rules_keep_retry_guidance @@ -3554,6 +4146,133 @@ test_backend_override_bypasses_unreadable_user_config() { pass "fm-pr-merge honors a backend override over an unreadable user configuration" } +test_github_behind_update_requires_opt_in_and_passing_guards() { + local case_dir rc scenario head + head=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + for scenario in no-consent red draft unreadable-authority away-queue; do + case_dir=$(make_case "github-behind-guard-$scenario") + add_gh_mocks "$case_dir" "$head" + jq '.mergeStateStatus = "BEHIND"' "$case_dir/github-view.json" > "$case_dir/view.tmp" + mv "$case_dir/view.tmp" "$case_dir/github-view.json" + case "$scenario" in + red) + jq '.statusCheckRollup[0].conclusion = "FAILURE"' "$case_dir/github-view.json" > "$case_dir/view.tmp" + mv "$case_dir/view.tmp" "$case_dir/github-view.json" + ;; + draft) + jq '.isDraft = true' "$case_dir/github-view.json" > "$case_dir/view.tmp" + mv "$case_dir/view.tmp" "$case_dir/github-view.json" + ;; + unreadable-authority) chmod 000 "$case_dir/home/data/backlog.md" ;; + away-queue) + printf 'merge_method=MERGE\n' > "$case_dir/github-rules" + write_away_record "$case_dir" --words 'merge task-x1 when green' + ;; + esac + set +e + if [ "$scenario" = no-consent ]; then + run_pr_merge "$case_dir" task-x1 https://github.com/example/repo/pull/6184 \ + > "$case_dir/stdout" 2> "$case_dir/stderr" + else + run_pr_merge "$case_dir" task-x1 https://github.com/example/repo/pull/6184 --update-branch \ + > "$case_dir/stdout" 2> "$case_dir/stderr" + fi + rc=$? + set -e + chmod 644 "$case_dir/home/data/backlog.md" + [ "$rc" -ne 0 ] || fail "github-behind-guard-$scenario: the merge must refuse" + assert_no_grep '/update-branch' "$case_dir/gh.log" \ + "github-behind-guard-$scenario: a refused merge changed the source branch" + assert_no_grep 'pr merge' "$case_dir/gh.log" \ + "github-behind-guard-$scenario: a refused merge reached the forge" + [ "$(cat "$case_dir/github-head")" = "$head" ] \ + || fail "github-behind-guard-$scenario: the source head changed" + if [ "$scenario" = no-consent ]; then + assert_grep 'requires explicit --update-branch consent' "$case_dir/stderr" \ + "github-behind-guard-no-consent: refusal did not explain the opt-in" + fi + done + pass "fm-pr-merge updates behind branches only with explicit opt-in after all guards pass" +} + +test_github_behind_update_keeps_expected_head_pin() { + local case_dir rc scenario expected old_head new_head + old_head=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + new_head=bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb + for scenario in initial-mismatch updated-mismatch; do + case_dir=$(make_case "github-behind-expected-$scenario") + add_gh_mocks "$case_dir" "$old_head" + jq '.mergeStateStatus = "BEHIND"' "$case_dir/github-view.json" > "$case_dir/view.tmp" + mv "$case_dir/view.tmp" "$case_dir/github-view.json" + jq --arg head "$new_head" '.mergeStateStatus = "CLEAN" | .headRefOid = $head' \ + "$case_dir/github-view.json" > "$case_dir/github-view-after-update.json" + printf '%s\n' "$new_head" > "$case_dir/github-head-after-update" + expected=$old_head + [ "$scenario" != initial-mismatch ] || expected=cccccccccccccccccccccccccccccccccccccccc + set +e + run_pr_merge "$case_dir" task-x1 https://github.com/example/repo/pull/6188 \ + --update-branch --expected-head "$expected" \ + > "$case_dir/stdout" 2> "$case_dir/stderr" + rc=$? + set -e + expect_code 1 "$rc" "github-behind-expected-$scenario: a mismatched head must refuse" + assert_grep "not the expected head $expected" "$case_dir/stderr" \ + "github-behind-expected-$scenario: refusal must retain the caller's head pin" + assert_no_grep 'pr merge' "$case_dir/gh.log" \ + "github-behind-expected-$scenario: merged a head the caller did not pin" + if [ "$scenario" = initial-mismatch ]; then + assert_no_grep '/update-branch' "$case_dir/gh.log" \ + "github-behind-expected-$scenario: updated before the caller's head matched" + [ "$(cat "$case_dir/github-head")" = "$old_head" ] \ + || fail "github-behind-expected-$scenario: changed the unapproved source head" + else + assert_grep "api --method PUT repos/example/repo/pulls/6188/update-branch -f expected_head_sha=$old_head" "$case_dir/gh.log" \ + "github-behind-expected-$scenario: explicit consent should permit the update" + [ "$(cat "$case_dir/github-head")" = "$new_head" ] \ + || fail "github-behind-expected-$scenario: the consented update did not advance" + fi + done + pass "fm-pr-merge retains --expected-head before and after a consented behind-branch update" +} + +test_github_updated_head_failure_refuses_without_waiting_for_pending() { + local case_dir rc kind old_head new_head + old_head=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + new_head=bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb + for kind in check-run status-context; do + case_dir=$(make_case "github-behind-failed-and-pending-$kind") + add_gh_mocks "$case_dir" "$old_head" + jq '.mergeStateStatus = "BEHIND"' "$case_dir/github-view.json" > "$case_dir/view.tmp" + mv "$case_dir/view.tmp" "$case_dir/github-view.json" + jq --arg head "$new_head" --arg kind "$kind" ' + .mergeStateStatus = "CLEAN" | .headRefOid = $head | + .statusCheckRollup = (if $kind == "check-run" then + [{"__typename":"CheckRun","name":"ci","status":"COMPLETED","conclusion":"FAILURE"}, + {"__typename":"CheckRun","name":"slow","status":"IN_PROGRESS","conclusion":null}] + else + [{"__typename":"StatusContext","context":"ci","state":"FAILURE"}, + {"__typename":"StatusContext","context":"slow","state":"PENDING"}] + end)' "$case_dir/github-view.json" > "$case_dir/github-view-after-update.json" + printf '%s\n' "$new_head" > "$case_dir/github-head-after-update" + set +e + FM_PR_GITHUB_MERGEABLE_RETRY_DELAY=1 \ + run_pr_merge "$case_dir" task-x1 https://github.com/example/repo/pull/6185 --update-branch \ + > "$case_dir/stdout" 2> "$case_dir/stderr" + rc=$? + set -e + expect_code 1 "$rc" "github-behind-failed-and-pending-$kind: a failed check must refuse" + assert_grep "check 'ci' is not green" "$case_dir/stderr" \ + "github-behind-failed-and-pending-$kind: refusal did not name the failure" + assert_no_grep 'checks are not ready' "$case_dir/stderr" \ + "github-behind-failed-and-pending-$kind: refusal obscured failure with a timeout" + [ "$(grep -c 'statusCheckRollup' "$case_dir/gh.log")" -eq 2 ] \ + || fail "github-behind-failed-and-pending-$kind: retried after a known failure" + assert_no_grep 'pr merge' "$case_dir/gh.log" \ + "github-behind-failed-and-pending-$kind: merged the failed head" + done + pass "fm-pr-merge refuses failed updated checks immediately while other checks are pending" +} + test_github_behind_updates_and_rechecks_before_merge() { local case_dir rc old_head new_head old_head=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa @@ -3567,15 +4286,35 @@ test_github_behind_updates_and_rechecks_before_merge() { "$case_dir/github-view.json" > "$case_dir/github-view-after-update.json" printf '%s\n' "$new_head" > "$case_dir/github-head-after-update" - run_pr_merge "$case_dir" task-x1 https://github.com/example/repo/pull/6178 -- --merge \ + run_pr_merge "$case_dir" task-x1 https://github.com/example/repo/pull/6178 --update-branch -- --merge \ > "$case_dir/stdout" 2> "$case_dir/stderr" \ || fail "github-behind-green: green updated head should merge: $(cat "$case_dir/stderr")" - assert_grep 'pr update-branch 6178 --repo example/repo' "$case_dir/gh.log" \ + assert_grep "api --method PUT repos/example/repo/pulls/6178/update-branch -f expected_head_sha=$old_head" "$case_dir/gh.log" \ "github-behind-green: the branch was not updated" assert_logged_gh_merge "$case_dir" 6178 example/repo --merge grep -qxF "pr_head=$new_head" "$case_dir/meta-at-merge" \ || fail "github-behind-green: the updated head was not recorded before the merge" + case_dir=$(make_case github-behind-delayed-head) + add_gh_mocks "$case_dir" "$old_head" + write_github_required "$case_dir" classic:ci + jq '.mergeStateStatus = "BEHIND"' "$case_dir/github-view.json" > "$case_dir/view.tmp" + mv "$case_dir/view.tmp" "$case_dir/github-view.json" + cp "$case_dir/github-view.json" "$case_dir/github-view-after-update.json" + jq --arg head "$new_head" '.mergeStateStatus = "CLEAN" | .headRefOid = $head' \ + "$case_dir/github-view.json" > "$case_dir/github-view-after-update-final.json" + printf '%s\n' "$new_head" > "$case_dir/github-head-after-update" + FM_PR_GITHUB_MERGEABLE_RETRY_DELAY=1 \ + run_pr_merge "$case_dir" task-x1 https://github.com/example/repo/pull/6178 --update-branch -- --merge \ + > "$case_dir/stdout" 2> "$case_dir/stderr" \ + || fail "github-behind-delayed-head: the old visible head should be awaited: $(cat "$case_dir/stderr")" + [ "$(grep -c 'statusCheckRollup' "$case_dir/gh.log")" -eq 3 ] \ + || fail "github-behind-delayed-head: did not wait for the new head" + assert_grep "--match-head-commit $new_head" "$case_dir/gh.log" \ + "github-behind-delayed-head: did not merge the verified new head" + grep -qxF "pr_head=$new_head" "$case_dir/meta-at-merge" \ + || fail "github-behind-delayed-head: the updated head was not recorded before merging" + case_dir=$(make_case github-behind-pending-after-update) add_gh_mocks "$case_dir" "$old_head" write_github_required "$case_dir" classic:ci @@ -3586,7 +4325,7 @@ test_github_behind_updates_and_rechecks_before_merge() { jq '.statusCheckRollup[0].status = "COMPLETED" | .statusCheckRollup[0].conclusion = "SUCCESS"' \ "$case_dir/github-view-after-update.json" > "$case_dir/github-view-after-update-final.json" printf '%s\n' "$new_head" > "$case_dir/github-head-after-update" - run_pr_merge "$case_dir" task-x1 https://github.com/example/repo/pull/6180 -- --merge \ + run_pr_merge "$case_dir" task-x1 https://github.com/example/repo/pull/6180 --update-branch -- --merge \ > "$case_dir/stdout" 2> "$case_dir/stderr" \ || fail "github-behind-pending-after-update: pending new check should be awaited: $(cat "$case_dir/stderr")" assert_logged_gh_merge "$case_dir" 6180 example/repo --merge @@ -3601,12 +4340,12 @@ test_github_behind_updates_and_rechecks_before_merge() { printf '%s\n' "$new_head" > "$case_dir/github-head-after-update" set +e - run_pr_merge "$case_dir" task-x1 https://github.com/example/repo/pull/6179 -- --merge \ + run_pr_merge "$case_dir" task-x1 https://github.com/example/repo/pull/6179 --update-branch -- --merge \ > "$case_dir/stdout" 2> "$case_dir/stderr" rc=$? set -e expect_code 1 "$rc" "github-behind-red-after-update: failed new check must refuse" - assert_grep 'pr update-branch 6179 --repo example/repo' "$case_dir/gh.log" \ + assert_grep "api --method PUT repos/example/repo/pulls/6179/update-branch -f expected_head_sha=$old_head" "$case_dir/gh.log" \ "github-behind-red-after-update: the branch was not updated" assert_grep "check 'ci' is not green" "$case_dir/stderr" \ "github-behind-red-after-update: the failed new check was not named" @@ -3622,10 +4361,10 @@ test_github_behind_updates_and_rechecks_before_merge() { jq --arg head "$new_head" '.mergeStateStatus = "CLEAN" | .headRefOid = $head' \ "$case_dir/github-view.json" > "$case_dir/github-view-after-update.json" printf '%s\n' "$new_head" > "$case_dir/github-head-after-update" - run_pr_merge "$case_dir" task-x1 https://github.com/example/repo/pull/6181 \ + run_pr_merge "$case_dir" task-x1 https://github.com/example/repo/pull/6181 --update-branch \ > "$case_dir/stdout" 2> "$case_dir/stderr" \ || fail "github-behind-default-squash: default method should update and merge: $(cat "$case_dir/stderr")" - assert_grep 'pr update-branch 6181 --repo example/repo' "$case_dir/gh.log" \ + assert_grep "api --method PUT repos/example/repo/pulls/6181/update-branch -f expected_head_sha=$old_head" "$case_dir/gh.log" \ "github-behind-default-squash: the branch was not updated" assert_logged_gh_merge "$case_dir" 6181 example/repo --squash @@ -3639,11 +4378,66 @@ test_github_behind_updates_and_rechecks_before_merge() { jq '.mergeable = "MERGEABLE"' "$case_dir/github-view-after-update.json" \ > "$case_dir/github-view-after-update-final.json" printf '%s\n' "$new_head" > "$case_dir/github-head-after-update" - run_pr_merge "$case_dir" task-x1 https://github.com/example/repo/pull/6182 -- --squash \ + run_pr_merge "$case_dir" task-x1 https://github.com/example/repo/pull/6182 --update-branch -- --squash \ > "$case_dir/stdout" 2> "$case_dir/stderr" \ || fail "github-behind-unknown-after-update: transient UNKNOWN should be awaited: $(cat "$case_dir/stderr")" assert_logged_gh_merge "$case_dir" 6182 example/repo --squash + case_dir=$(make_case github-behind-unknown-after-waived-red) + add_gh_mocks "$case_dir" "$old_head" + jq '.mergeStateStatus = "BEHIND"' "$case_dir/github-view.json" > "$case_dir/github-view-after-update.json" + mv "$case_dir/github-view-after-update.json" "$case_dir/github-view.json" + jq --arg head "$new_head" '.mergeStateStatus = "CLEAN" | .headRefOid = $head | .mergeable = "UNKNOWN" | .statusCheckRollup[0].conclusion = "FAILURE"' \ + "$case_dir/github-view.json" > "$case_dir/github-view-after-update.json" + jq '.mergeable = "MERGEABLE"' "$case_dir/github-view-after-update.json" \ + > "$case_dir/github-view-after-update-final.json" + printf '%s\n' "$new_head" > "$case_dir/github-head-after-update" + run_pr_merge "$case_dir" task-x1 https://github.com/example/repo/pull/6186 --update-branch --allow-red ci -- --squash \ + > "$case_dir/stdout" 2> "$case_dir/stderr" \ + || fail "github-behind-unknown-after-waived-red: UNKNOWN beside a waived failure should be awaited: $(cat "$case_dir/stderr")" + assert_logged_gh_merge "$case_dir" 6186 example/repo --squash + + case_dir=$(make_case github-behind-unreadable-after-update) + add_gh_mocks "$case_dir" "$old_head" + jq '.mergeStateStatus = "BEHIND"' "$case_dir/github-view.json" > "$case_dir/github-view-after-update.json" + mv "$case_dir/github-view-after-update.json" "$case_dir/github-view.json" + printf '[]\n' > "$case_dir/github-view-after-update.json" + printf '%s\n' "$new_head" > "$case_dir/github-head-after-update" + set +e + FM_PR_GITHUB_MERGEABLE_RETRY_DELAY=0 \ + run_pr_merge "$case_dir" task-x1 https://github.com/example/repo/pull/6187 --update-branch -- --merge \ + > "$case_dir/stdout" 2> "$case_dir/stderr" + rc=$? + set -e + expect_code 1 "$rc" "github-behind-unreadable-after-update: an unreadable updated state must refuse" + assert_grep 'could not read the GitHub pull request state' "$case_dir/stderr" \ + "github-behind-unreadable-after-update: the read failure was not reported" + assert_no_grep 'head moved again' "$case_dir/stderr" \ + "github-behind-unreadable-after-update: a read failure was reported as a head change" + assert_no_grep 'pr merge' "$case_dir/gh.log" \ + "github-behind-unreadable-after-update: an unverified head was merged" + + case_dir=$(make_case github-behind-pending-cap) + add_gh_mocks "$case_dir" "$old_head" + write_github_required "$case_dir" classic:ci + jq '.mergeStateStatus = "BEHIND"' "$case_dir/github-view.json" > "$case_dir/github-view-after-update.json" + mv "$case_dir/github-view-after-update.json" "$case_dir/github-view.json" + jq --arg head "$new_head" '.mergeStateStatus = "CLEAN" | .headRefOid = $head | .statusCheckRollup[0].status = "IN_PROGRESS" | .statusCheckRollup[0].conclusion = null' \ + "$case_dir/github-view.json" > "$case_dir/github-view-after-update.json" + printf '%s\n' "$new_head" > "$case_dir/github-head-after-update" + set +e + FM_PR_GITHUB_MERGEABLE_RETRY_DELAY=1 \ + run_pr_merge "$case_dir" task-x1 https://github.com/example/repo/pull/6183 --update-branch -- --merge \ + > "$case_dir/stdout" 2> "$case_dir/stderr" + rc=$? + set -e + expect_code 1 "$rc" "github-behind-pending-cap: checks still pending past the cap must stop" + assert_grep "api --method PUT repos/example/repo/pulls/6183/update-branch -f expected_head_sha=$old_head" "$case_dir/gh.log" \ + "github-behind-pending-cap: the branch was not updated" + assert_grep "was updated to $new_head but its checks are not ready; run the merge again once its checks pass" "$case_dir/stderr" \ + "github-behind-pending-cap: the stop message was not printed" + assert_no_grep 'pr merge' "$case_dir/gh.log" \ + "github-behind-pending-cap: an unverified head was merged" case_dir=$(make_case github-behind-base-moved) add_gh_mocks "$case_dir" "$old_head" write_github_required "$case_dir" classic:ci @@ -3653,12 +4447,12 @@ test_github_behind_updates_and_rechecks_before_merge() { "$case_dir/github-view.json" > "$case_dir/github-view-after-update.json" printf '%s\n' "$new_head" > "$case_dir/github-head-after-update" set +e - run_pr_merge "$case_dir" task-x1 https://github.com/example/repo/pull/6183 -- --merge \ + run_pr_merge "$case_dir" task-x1 https://github.com/example/repo/pull/6183 --update-branch -- --merge \ > "$case_dir/stdout" 2> "$case_dir/stderr" rc=$? set -e expect_code 1 "$rc" "github-behind-base-moved: changed base must refuse" - assert_grep 'head or base moved again' "$case_dir/stderr" \ + assert_grep 'base moved after its branch update' "$case_dir/stderr" \ "github-behind-base-moved: the changed base was not named" assert_no_grep 'pr merge' "$case_dir/gh.log" \ "github-behind-base-moved: changed base reached the merge call" @@ -5084,6 +5878,9 @@ test_unreadable_user_backend_config_refuses_the_merge test_untraversable_user_backend_config_directory_refuses_the_merge test_absent_user_backend_config_directory_and_backlog_still_merge test_backend_override_bypasses_unreadable_user_config +test_github_behind_update_requires_opt_in_and_passing_guards +test_github_behind_update_keeps_expected_head_pin +test_github_updated_head_failure_refuses_without_waiting_for_pending test_github_behind_updates_and_rechecks_before_merge test_github_red_checks_refuse_and_allow_red_waives_named test_github_draft_or_unreadable_draft_state_refuses @@ -5124,3 +5921,9 @@ test_allow_red_refused_on_forgejo test_allow_missing_refused_on_forgejo test_allow_no_pipeline_merges_only_a_null_head_pipeline test_allow_no_pipeline_is_attended_only_and_gitlab_only +test_expected_head_matching_reaches_each_forge +test_expected_head_mismatch_refuses_before_merge +test_expected_head_push_after_verify_is_refused_by_the_forge +test_expected_head_later_preflight_does_not_adopt_a_new_head +test_expected_head_bad_inputs_refuse_before_effects +test_expected_head_keeps_existing_refusals diff --git a/tests/fm-procevent.test.sh b/tests/fm-procevent.test.sh index 500649ccd41..73b6620a241 100755 --- a/tests/fm-procevent.test.sh +++ b/tests/fm-procevent.test.sh @@ -155,6 +155,108 @@ wait_for() { # [tries] return 1 } +# --- detached startup refreshes owner presence after slow caller work ------- +LEASE_ADAPTERS="$TMP_ROOT/lease-adapters" +mkdir -p "$LEASE_ADAPTERS/bin" +cat > "$LEASE_ADAPTERS/bin/fm-procevent-leaseplain.sh" <<'SH' +#!/usr/bin/env bash +exit 2 +SH +cat > "$LEASE_ADAPTERS/bin/fm-procevent-leasepublish.sh" <<'SH' +#!/usr/bin/env bash +case "${1-}" in + silent) + printf 'publishing\n' > "$LEASE_PUBLISH_READY" + while [ ! -e "$LEASE_PUBLISH_RELEASE" ]; do + [ "$SECONDS" -lt 30 ] || exit 2 + sleep 0.05 + done + exit 1 + ;; +esac +exit 2 +SH +chmod +x "$LEASE_ADAPTERS/bin/"*.sh + +lease_pe() { + FM_ROOT_OVERRIDE="$LEASE_ADAPTERS" FM_PROCEVENT_OWNER_LEASE_SECONDS=2 \ + FM_PROCEVENT_OWNER_CHECK_SECONDS=1 pe "$@" +} +wait_stale_lease() { # + local lease="$1/state/procevent/.owner-lease" n + for n in $(seq 1 100); do + if perl -MTime::HiRes=clock_gettime,CLOCK_MONOTONIC -e ' + open my $in, "<", $ARGV[0] or exit 1; + my $stamp = <$in>; + exit(clock_gettime(CLOCK_MONOTONIC) - $stamp >= 3 ? 0 : 1); + ' "$lease"; then + return 0 + fi + sleep 0.1 + done + return 1 +} + +for lease_entry in reconcile ensure-listening; do + LEASE_HOME="$TMP_ROOT/lease-$lease_entry"; new_home "$LEASE_HOME" + fm_test_track_procevent_home "$LEASE_HOME" + lease_id="lease-$lease_entry-src" + lease_started="$TMP_ROOT/$lease_id.started" + lease_pe "$LEASE_HOME" register leaseplain "$lease_id" -- \ + "$STARTED_BLOCKER" "$lease_started" "$BLOCKER" "$TMP_ROOT/$lease_id.trigger" payload >/dev/null + if [ "$lease_entry" = reconcile ]; then + mkdir -p "$LEASE_HOME/state/procevent-inbox" + printf 'old payload\n' > "$LEASE_HOME/state/procevent-inbox/lease-old.1.result" + printf 'leasepublish\n' > "$LEASE_HOME/state/procevent-inbox/lease-old.1.adapter" + chmod 600 "$LEASE_HOME/state/procevent-inbox/lease-old.1."* + export LEASE_PUBLISH_READY="$TMP_ROOT/lease-publish.ready" + export LEASE_PUBLISH_RELEASE="$TMP_ROOT/lease-publish.release" + lease_pe "$LEASE_HOME" reconcile > "$TMP_ROOT/lease-reconcile.out" 2>&1 & + lease_caller=$! + wait_for "$LEASE_PUBLISH_READY" || fail "reconcile never reached pre-launch publication" + wait_stale_lease "$LEASE_HOME" || fail "publication did not outlast the fixture lease" + touch "$LEASE_PUBLISH_RELEASE" + wait "$lease_caller" || fail "reconcile failed after slow publication" + else + wait_stale_lease "$LEASE_HOME" || fail "the readiness fixture lease never expired" + lease_pe "$LEASE_HOME" ensure-listening "$lease_id" \ + > "$TMP_ROOT/lease-ensure-listening.out" 2>&1 \ + || fail "ensure-listening failed with an old caller lease" + fi + wait_for "$lease_started" \ + || fail "$lease_entry confirmed a detached start without running the source command" + assert_contains "$(lease_pe "$LEASE_HOME" list)" "live" \ + "$lease_entry leaves a live source owner after refreshing at launch" + # No further owner-presence operation runs. The launch refresh must not keep + # renewing itself after the caller has returned. + lease_runner=$(cat "$LEASE_HOME/state/procevent/$lease_id.runner") + lease_deadline=$((SECONDS + 10)) + while kill -0 "$lease_runner" 2>/dev/null; do + [ "$SECONDS" -lt "$lease_deadline" ] \ + || fail "$lease_entry kept its runner alive after the owning home's lease expired" + sleep 0.1 + done + lease_pe "$LEASE_HOME" retire "$lease_id" >/dev/null + pass "$lease_entry refreshes at detached launch and still stops after owner lease expiry" +done + +# An ordinary runner descendant may request a detached launch, but may never +# turn that launch into new owner-presence activity for its missing home. +LEASE_INHERITED="$TMP_ROOT/lease-inherited"; new_home "$LEASE_INHERITED" +fm_test_track_procevent_home "$LEASE_INHERITED" +lease_pe "$LEASE_INHERITED" register leaseplain lease-inherited-src -- \ + "$STARTED_BLOCKER" "$TMP_ROOT/lease-inherited.started" /bin/sleep 30 >/dev/null +wait_stale_lease "$LEASE_INHERITED" || fail "the inherited caller fixture lease never expired" +lease_before=$(cat "$LEASE_INHERITED/state/procevent/.owner-lease") +FM_PROCEVENT_IN_RUNNER=1 lease_pe "$LEASE_INHERITED" ensure-listening lease-inherited-src \ + > "$TMP_ROOT/lease-inherited.out" 2>&1 || true +[ "$(cat "$LEASE_INHERITED/state/procevent/.owner-lease")" = "$lease_before" ] \ + || fail "a runner descendant refreshed the owning lease at detached launch" +assert_absent "$TMP_ROOT/lease-inherited.started" \ + "a runner descendant cannot start a source under its expired owning lease" +lease_pe "$LEASE_INHERITED" retire lease-inherited-src >/dev/null +pass "inherited runner callers cannot renew owner presence at detached startup" + # Arm now starts the listener, so a later start would poll again. Wait for the # capture that listener is already producing, and for its runner to release the # claim: the result lands before the runner publishes and exits, and a retire or diff --git a/tests/fm-record-export.test.sh b/tests/fm-record-export.test.sh index 0d12636beed..1b13cf3829e 100644 --- a/tests/fm-record-export.test.sh +++ b/tests/fm-record-export.test.sh @@ -82,6 +82,35 @@ test_sync_copies_only_the_v1_allowlist() { pass "record export sync copies only the fixed v1 allowlist with restrictive modes" } +test_sync_treats_empty_path_overrides_as_unset() { + local home export_root rc=0 + home=$(make_home sync-empty-overrides) + export_root="$TMP_ROOT/export-empty-overrides" + printf '%s\n' "$export_root" > "$home/config/record-export-root" + printf 'projects\n' > "$home/data/projects.md" + printf 'working: underway\n' > "$home/state/task-a.status" + FM_HOME="$home" FM_ROOT_OVERRIDE='' FM_CONFIG_OVERRIDE='' FM_DATA_OVERRIDE='' FM_STATE_OVERRIDE='' \ + "$EXPORTER" sync > "$home/stdout" 2> "$home/stderr" || rc=$? + expect_code 0 "$rc" "sync with empty path overrides: $(cat "$home/stderr")" + assert_grep 'projects' "$export_root/live/data/projects.md" "sync with empty overrides skipped the home data tree" + assert_grep 'underway' "$export_root/live/state/task-a.status" "sync with empty overrides skipped the home state tree" + pass "record export sync treats empty FM_*_OVERRIDE variables as unset" +} + +test_sync_skips_hidden_state_records() { + local home export_root rc=0 + home=$(make_home sync-hidden-state) + export_root="$TMP_ROOT/export-hidden-state" + printf '%s\n' "$export_root" > "$home/config/record-export-root" + printf 'status\n' > "$home/state/task-a.status" + printf 'network checks\n' > "$home/state/.startup-network.status" + run_export "$home" sync > "$home/stdout" 2> "$home/stderr" || rc=$? + expect_code 0 "$rc" "sync with a hidden state status file: $(cat "$home/stderr")" + assert_grep 'status' "$export_root/live/state/task-a.status" "sync dropped the task status beside a hidden record" + assert_absent "$export_root/live/state/.startup-network.status" "sync exported a hidden non-task status file" + pass "record export sync skips hidden state records instead of failing" +} + test_sync_replaces_files_atomically() { local home export_root rc=0 home=$(make_home sync-atomic) @@ -904,6 +933,8 @@ test_large_histories_fit_a_child_descriptor_limit test_reopened_sources_reject_replaced_ancestors test_sync_is_off_when_config_is_absent test_sync_copies_only_the_v1_allowlist +test_sync_treats_empty_path_overrides_as_unset +test_sync_skips_hidden_state_records test_sync_replaces_files_atomically test_sync_accepts_an_absolute_export_root_with_spaces test_sync_refuses_malformed_config_and_unsafe_paths diff --git a/tests/fm-remote-inherit-generation.test.sh b/tests/fm-remote-inherit-generation.test.sh new file mode 100755 index 00000000000..782f55dac4f --- /dev/null +++ b/tests/fm-remote-inherit-generation.test.sh @@ -0,0 +1,166 @@ +#!/usr/bin/env bash +# Inheritance counter recovery through synthetic homes and the fake SSH boundary. +set -eu +# shellcheck source=tests/lib.sh +. "$(dirname "${BASH_SOURCE[0]}")/lib.sh" +# shellcheck source=bin/fm-wake-lib.sh +. "$ROOT/bin/fm-wake-lib.sh" +# shellcheck source=bin/fm-secondmate-nudge-lib.sh +. "$ROOT/bin/fm-secondmate-nudge-lib.sh" + +TMP_ROOT=$(fm_test_tmproot fm-remote-inherit-generation) +PARENT="$TMP_ROOT/parent" +REMOTE_HOME="$TMP_ROOT/receiver" +mkdir -p "$PARENT/config" "$PARENT/data" "$PARENT/state" "$REMOTE_HOME/config" "$REMOTE_HOME/data" +cat > "$PARENT/data/secondmates.md" < "$TMP_ROOT/fake-ssh" <<'SH' +#!/usr/bin/env bash +set -eu +while [ "$#" -gt 0 ]; do + case "$1" in -o) shift 2 ;; --) shift; break ;; *) exit 90 ;; esac +done +[ "$1" = fixture-host ] && [ "$2" = fm-remote-entrypoint.sh ] || exit 91 +shift 2 +[ "$1" = 1 ] || exit 92 +decode() { perl -MMIME::Base64=decode_base64 -e 'print decode_base64($ARGV[0])' "$1"; } +root=$(decode "$2") +receiver=$(decode "$3") +args=() +while IFS= read -r -d '' arg; do args+=("$arg"); done < <(decode "$4") +[ "${args[0]}" = fm-remote-inherit.sh ] || exit 93 +[ -d "$FM_FIXTURE_SENDER_LOCK" ] || { echo 'sender transaction is unlocked' >&2; exit 94; } +if [ -n "${FM_FIXTURE_PROBE_MODE:-}" ] && [ "${args[1]}" = generation ]; then + case "$FM_FIXTURE_PROBE_MODE" in + old) printf 'Usage: fm-remote-inherit.sh put|absent \n'; exit 2 ;; + malformed) printf 'invalid\n'; exit 0 ;; + empty) exit 0 ;; + refused) exit 1 ;; + failed) exit 3 ;; + unreadable) exit 255 ;; + esac +fi +if [ -n "${FM_FIXTURE_BLOCK:-}" ] && [ "${args[1]}" = generation ]; then + touch "$FM_FIXTURE_BLOCK.entered" + for ((i=0; i<600; i++)); do + [ ! -f "$FM_FIXTURE_BLOCK.release" ] || break + sleep 0.05 + done + [ -f "$FM_FIXTURE_BLOCK.release" ] || exit 95 +fi +exec env FM_HOME="$receiver" FM_INHERITABLE_CONFIG=crew-harness bash "$root/bin/${args[0]}" "${args[@]:1}" +SH +chmod +x "$TMP_ROOT/fake-ssh" + +sha256_file() { + if command -v shasum >/dev/null 2>&1; then shasum -a 256 "$1" | awk '{print $1}'; else sha256sum "$1" | awk '{print $1}'; fi +} +receive() { # + local bytes hash + bytes=$(LC_ALL=C wc -c < "$2" | tr -d ' ') + hash=$(sha256_file "$2") + FM_HOME="$REMOTE_HOME" FM_INHERITABLE_CONFIG=crew-harness \ + bash "$ROOT/bin/fm-remote-inherit.sh" put config/crew-harness "$bytes" "$hash" "$1" < "$2" +} +push_fixture() ( + local lock generation + lock=$(fm_remote_inherit_transaction_lock_path "$PARENT/state" fixture) + fm_lock_acquire_wait "$lock" || exit 1 + trap 'fm_lock_release "$lock"' EXIT + generation=$(fm_remote_inherit_generation_next "$PARENT/state" fixture) || exit 1 + FM_HOME="$PARENT" FM_ROOT_OVERRIDE="$ROOT" FM_INHERITABLE_CONFIG=crew-harness \ + FM_FIXTURE_SENDER_LOCK="$lock" FM_SSH_BIN="$TMP_ROOT/fake-ssh" \ + bash "$ROOT/bin/fm-remote-inherit-push.sh" fixture "$generation" +) +COUNTER="$PARENT/state/.remote-inherit-fixture.generation" +RECORD="$REMOTE_HOME/config/.fm-inherit-crew-harness.generation" +printf 'receiver original\n' > "$TMP_ROOT/original" +printf 'current sender\n' > "$PARENT/config/crew-harness" +if ! FM_FIXTURE_PROBE_MODE=old push_fixture > "$TMP_ROOT/old.out" 2>&1; then + fail "older receiver usage refusal blocked inheritance: $(cat "$TMP_ROOT/old.out")" +fi +cmp -s "$PARENT/config/crew-harness" "$REMOTE_HOME/config/crew-harness" || fail 'older receiver push did not converge' +read -r committed < "$RECORD" +[ "$committed" = 1 ] && [ "$(cat "$COUNTER")" = 1 ] || fail 'older receiver did not use generation floor zero' +read -r absent_committed < "$REMOTE_HOME/data/.fm-inherit-captain-shared.md.generation" +[ "$absent_committed" = 1 ] || fail 'older receiver absent item did not use generation floor zero' +assert_not_contains "$(cat "$TMP_ROOT/old.out")" 'Usage:' 'older receiver usage was replayed as push output' +rm -f -- "$COUNTER" +pass 'older receiver generation usage refusal preserves inheritance at floor zero' + +receive 40 "$TMP_ROOT/original" >/dev/null +: > "$TMP_ROOT/empty" +FM_HOME="$REMOTE_HOME" FM_INHERITABLE_CONFIG=crew-harness \ + bash "$ROOT/bin/fm-remote-inherit.sh" absent data/captain-shared.md 0 "$(sha256_file "$TMP_ROOT/empty")" 100 >/dev/null +if ! push_fixture > "$TMP_ROOT/missing.out" 2>&1; then + fail "missing sender counter did not recover beyond receiver generation 40: $(cat "$TMP_ROOT/missing.out")" +fi +cmp -s "$PARENT/config/crew-harness" "$REMOTE_HOME/config/crew-harness" || fail 'missing-counter push did not converge' +read -r committed < "$RECORD" +[ "$committed" -gt 100 ] && [ "$(cat "$COUNTER")" = "$committed" ] || fail 'sender did not recover above every receiver item' +pass 'missing sender counter recovers above the receiver under the sender transaction lock' + +for old in 1 20 "$committed"; do + printf '%s\n' "$old" > "$COUNTER" + previous=$committed + printf 'sender after restore %s\n' "$old" > "$PARENT/config/crew-harness" + push_fixture > "$TMP_ROOT/restored-$old.out" 2>&1 || fail "restored counter $old did not recover" + read -r committed < "$RECORD" + [ "$committed" -gt "$previous" ] || fail 'restored push did not advance the receiver' + [ "$(cat "$COUNTER")" = "$committed" ] || fail 'recovered sender counter did not persist' + cmp -s "$PARENT/config/crew-harness" "$REMOTE_HOME/config/crew-harness" || fail 'restored push did not converge' +done +pass 'lower, restored and same-generation conflicting sender counters recover' + +if receive 40 "$TMP_ROOT/original" > "$TMP_ROOT/delayed.out" 2>&1; then fail 'delayed older write was accepted'; fi +assert_grep 'superseded' "$TMP_ROOT/delayed.out" 'delayed write did not retain the stale-generation refusal' +if receive "$committed" "$TMP_ROOT/original" > "$TMP_ROOT/conflict.out" 2>&1; then fail 'same-generation different payload was accepted'; fi +assert_grep 'conflicts' "$TMP_ROOT/conflict.out" 'same-generation conflict was not refused' +cmp -s "$PARENT/config/crew-harness" "$REMOTE_HOME/config/crew-harness" || fail 'refused write changed current payload' +pass 'delayed older and conflicting same-generation payloads remain refused' + +# Make the first push hold the existing sender transaction while a second waits. +previous=$committed +printf '1\n' > "$COUNTER" +FM_FIXTURE_BLOCK="$TMP_ROOT/race" push_fixture > "$TMP_ROOT/race-first.out" 2>&1 & +first=$! +for ((i=0; i<600; i++)); do + [ ! -f "$TMP_ROOT/race.entered" ] || break + sleep 0.05 +done +assert_present "$TMP_ROOT/race.entered" 'first push did not reach its generation query' +push_fixture > "$TMP_ROOT/race-second.out" 2>&1 & +second=$! +touch "$TMP_ROOT/race.release" +wait "$first" || fail 'first racing push failed' +wait "$second" || fail 'second racing push failed' +read -r committed < "$RECORD" +[ "$committed" -ge "$((previous + 2))" ] || fail 'racing pushes lost a generation' +[ "$(cat "$COUNTER")" = "$committed" ] || fail 'racing sender and receiver counters diverged' +cmp -s "$PARENT/config/crew-harness" "$REMOTE_HOME/config/crew-harness" || fail 'racing push changed the payload' +pass 'racing pushes commit increasing generations through the shared sender transaction' + +cp "$RECORD" "$TMP_ROOT/record-good" +cp "$REMOTE_HOME/config/crew-harness" "$TMP_ROOT/payload-good" +for mode in malformed empty refused failed unreadable; do + if FM_FIXTURE_PROBE_MODE="$mode" push_fixture > "$TMP_ROOT/probe-$mode.out" 2>&1; then fail "$mode remote generation was accepted"; fi + cmp -s "$RECORD" "$TMP_ROOT/record-good" || fail "$mode probe changed the receiver generation" + cmp -s "$REMOTE_HOME/config/crew-harness" "$TMP_ROOT/payload-good" || fail "$mode probe changed the receiver payload" +done +printf 'not-a-generation\n' > "$RECORD" +if push_fixture > "$TMP_ROOT/record-malformed.out" 2>&1; then fail 'malformed receiver record was accepted'; fi +cmp -s "$REMOTE_HOME/config/crew-harness" "$TMP_ROOT/payload-good" || fail 'malformed record changed the payload' +cp "$TMP_ROOT/record-good" "$RECORD" +if [ "$(id -u)" -ne 0 ]; then + chmod 000 "$RECORD" + unreadable_rc=0 + push_fixture > "$TMP_ROOT/record-unreadable.out" 2>&1 || unreadable_rc=$? + chmod 600 "$RECORD" + [ "$unreadable_rc" -ne 0 ] || fail 'unreadable receiver record was accepted' + cmp -s "$REMOTE_HOME/config/crew-harness" "$TMP_ROOT/payload-good" || fail 'unreadable record changed the payload' +fi +printf 'not-a-generation\n' > "$COUNTER" +if push_fixture > "$TMP_ROOT/counter-malformed.out" 2>&1; then fail 'malformed sender counter was accepted'; fi +cmp -s "$RECORD" "$TMP_ROOT/record-good" || fail 'malformed sender counter changed the receiver generation' +pass 'malformed or unreadable generation state fails closed without changing inherited content' diff --git a/tests/fm-session-lock-ancestry.test.sh b/tests/fm-session-lock-ancestry.test.sh index 44a18352e72..bea82f5d8dc 100755 --- a/tests/fm-session-lock-ancestry.test.sh +++ b/tests/fm-session-lock-ancestry.test.sh @@ -626,6 +626,84 @@ test_windows_tagged_holder_is_a_live_foreign_owner() { pass "session-lock: a live tagged lock holder is a live foreign owner to a second Windows session" } +test_windows_tagged_lock_acquisition_uses_the_shared_owner_verdict() { + local dir fakebin home current_row live_table dead_table reused_table out status record + dir="$TMP_ROOT/win-lock-acquire" + fakebin=$(cygwin_fakebin "$dir") + home="$dir/home" + mkdir -p "$home/state" "$home/endpoints" "$home/bin" "$home/config" + git init -q "$home" + git -C "$home" commit -q --allow-empty -m fixture + : > "$home/AGENTS.md" + : > "$home/state/task.meta" + current_row=' 4201600 0 0 7300 ? 0 22:30:00 C:\Users\u\.local\bin\claude.exe' + live_table="$WIN_TABLE +$current_row" + dead_table=$current_row + reused_table=" 4201508 0 0 7204 ? 0 22:24:48 C:\\Windows\\explorer.exe +$current_row" + + attempt_lock() { # + local caller_pid=$1 caller_session=$2 table=$3 table_fails=$4 local_ppid=$5 + local -a assignments=() + assignments+=("PATH=$fakebin:$PATH" "FM_HOME=$home" "FM_TEST_WIN_TABLE=$table") + [ -z "$local_ppid" ] || assignments+=("FM_TEST_CYG_PPID=$local_ppid") + [ -z "$caller_pid" ] || assignments+=("CLAUDE_PID=$caller_pid" "CLAUDE_CODE_SESSION_ID=$caller_session") + [ "$table_fails" != 1 ] || assignments+=("FM_TEST_WIN_PS_FAIL=1") + env -u CLAUDE_PID -u CLAUDE_CODE_SESSION_ID -u FM_TEST_WIN_PS_FAIL -u FM_TEST_CYG_PPID \ + "${assignments[@]}" "$ROOT/bin/fm-lock.sh" + } + attempt_hook() { # + local script=$1 payload=$2 + printf '%s' "$payload" | env -u CLAUDE_PID -u CLAUDE_CODE_SESSION_ID \ + -u FM_TEST_WIN_PS_FAIL -u FM_TEST_CYG_PPID -u PI_CODING_AGENT -u CURSOR_AGENT -u CURSOR_INVOKED_AS \ + PATH="$fakebin:$PATH" FM_HOME="$home" FM_ROOT_OVERRIDE="$home" \ + FM_TEST_WIN_TABLE="$live_table" CLAUDE_PID=7300 CLAUDE_CODE_SESSION_ID=S2 \ + "$ROOT/bin/$script" + } + + printf 'win:7204\n' > "$home/state/.lock" + printf 'S1\n' > "$home/state/.lock-session" + if out=$(attempt_lock 7300 S2 "$live_table" 0 '' 2>&1); then status=0; else status=$?; fi + [ "$status" -eq 1 ] || fail "fm-lock.sh acquired a live tagged harness lock: $out" + case "$out" in *'another live firstmate session holds the lock'*) ;; *) fail "live tagged owner refusal was not reported: $out" ;; esac + [ "$(cat "$home/state/.lock")" = 'win:7204' ] || fail "a live tagged owner lock was replaced" + + if out=$(attempt_hook fm-claude-stop-autoarm.sh '' 2>&1); then status=0; else status=$?; fi + [ "$status" -eq 0 ] || fail "Stop recovery did not stand down for a live tagged owner: $out" + [ "$(cat "$home/state/.lock")" = 'win:7204' ] || fail "Stop recovery replaced a live tagged owner lock" + if out=$(attempt_hook fm-turnend-guard-cursor.sh '{"loop_count":0,"session_id":"S2"}' 2>&1); then status=0; else status=$?; fi + [ "$status" -eq 0 ] || fail "turn-end recovery did not stand down for a live tagged owner: $out" + [ "$(cat "$home/state/.lock")" = 'win:7204' ] || fail "turn-end recovery replaced a live tagged owner lock" + + printf 'win:7204\n' > "$home/state/.lock" + printf 'S1\n' > "$home/state/.lock-session" + if out=$(attempt_lock '' '' "$live_table" 1 700 2>&1); then status=0; else status=$?; fi + [ "$status" -eq 1 ] || fail "fm-lock.sh acquired through an unreadable Windows process table: $out" + [ "$(cat "$home/state/.lock")" = 'win:7204' ] || fail "an unreadable Windows table allowed the tagged owner lock to be replaced" + + printf 'win:7204\n' > "$home/state/.lock" + printf 'S1\n' > "$home/state/.lock-session" + if out=$(attempt_lock 7300 S2 "$dead_table" 0 '' 2>&1); then status=0; else status=$?; fi + [ "$status" -eq 0 ] || fail "fm-lock.sh refused a tagged owner absent from the Windows table: $out" + [ "$(cat "$home/state/.lock")" = 'win:7300' ] || fail "a dead tagged owner was not replaced by the current session" + + printf 'win:7204\n' > "$home/state/.lock" + printf 'S1\n' > "$home/state/.lock-session" + if out=$(attempt_lock 7300 S2 "$reused_table" 0 '' 2>&1); then status=0; else status=$?; fi + [ "$status" -eq 0 ] || fail "fm-lock.sh refused a reused tagged pid naming a non-harness: $out" + [ "$(cat "$home/state/.lock")" = 'win:7300' ] || fail "a reused non-harness pid was not reclaimed" + + for record in '' ' ' not-a-pid 0 00 000 0000 win: win:7abc; do + printf '%s\n' "$record" > "$home/state/.lock" + printf 'S1\n' > "$home/state/.lock-session" + if out=$(attempt_lock 7300 S2 "$dead_table" 0 '' 2>&1); then status=0; else status=$?; fi + [ "$status" -eq 0 ] || fail "fm-lock.sh refused non-owner record '$record': $out" + [ "$(cat "$home/state/.lock")" = 'win:7300' ] || fail "non-owner record '$record' was not replaced" + done + pass "session-lock: fm-lock.sh protects valid tagged owners and reclaims dead or malformed records" +} + test_windows_published_pid_needs_its_claude_session_id() { local dir fakebin got dir="$TMP_ROOT/win-session-id" @@ -1576,6 +1654,7 @@ test_windows_published_pid_is_confirmed_before_it_is_trusted test_windows_pid_is_never_resolved_as_a_cygwin_pid test_a_published_identity_is_accepted_by_the_gates_that_read_the_lock test_windows_tagged_holder_is_a_live_foreign_owner +test_windows_tagged_lock_acquisition_uses_the_shared_owner_verdict test_windows_published_pid_needs_its_claude_session_id test_windows_clear_rekeys_the_lock_under_the_same_pid test_tagged_lock_is_inert_off_windows diff --git a/tests/fm-session-lock-ps-denied.test.sh b/tests/fm-session-lock-ps-denied.test.sh new file mode 100644 index 00000000000..6e49799c1ee --- /dev/null +++ b/tests/fm-session-lock-ps-denied.test.sh @@ -0,0 +1,979 @@ +#!/usr/bin/env bash +# tests/fm-session-lock-ps-denied.test.sh - the ps-free identity route. +# +# An EDR-style policy can deny the ps BINARY's exec (verified on a hardened +# macOS host: /bin/ps refused even `ps -V`, while kill/head/sysctl/pgrep/lsof +# ran), and before bin/fm-session-lock-lib.sh could fall back to an OS +# provider, that turned every session into a permanent lock-refused +# read-only one, because identity proof walked ancestry through ps. +# +# The unit cases drive the library behind a deterministic fake ps that +# refuses to run (exit 126, bash's refused-exec status) plus a fake OS +# provider, covering the fallback decision table on any host. The e2e case +# blocks ps from a REAL process tree (fake ps on PATH) and resolves a real +# pid through the REAL platform provider, so a regression that only fakes +# would miss is caught here. +# shellcheck disable=SC2016 # single quotes are deliberate where they expand inside the fixture child +set -u + +# shellcheck source=tests/lib.sh +. "$(dirname "${BASH_SOURCE[0]}")/lib.sh" + +TMP_ROOT=$(fm_test_tmproot fm-session-lock-ps-denied) + +LIB="$ROOT/bin/fm-session-lock-lib.sh" + +# A denied ps: refuses to run at all, exactly like the blocked binary. +make_denied_ps() { # + local fakebin=$1 + cat > "$fakebin/ps" <<'SH' +#!/usr/bin/env bash +echo "$0: Operation not permitted" >&2 +exit 126 +SH + chmod +x "$fakebin/ps" +} + +# Run one library expression with a denied ps shadowing PATH and a table +# provider. The table reads FM_TEST_TRIPLE_N records (fake table rows +# "|||"); a pid absent from the table is unreadable, +# which is how a dead pid behaves. FM_TEST_KILL_RC decides kill as before. +# CLAUDE identity envs are scrubbed the same way the ancestry suite scrubs +# them, so a suite running inside a real Claude session cannot leak in. +lib_eval_denied() { # + env -u CLAUDE_CODE_SESSION_ID -u CLAUDE_PID \ + FM_PROVIDER_FILE="$2" PATH="$1:$PATH" bash -c " + . \"\$0\" + kill() { return \${FM_TEST_KILL_RC:-0}; } + _emit_row() { # + local ppid rest comm args + ppid=\${1#*|}; ppid=\${ppid%%|*} + rest=\${1#*|}; rest=\${rest#*|} + comm=\${rest%%|*} + args=\${rest#*|} + printf '%s\t%s\t%s\n' \"\$ppid\" \"\$comm\" \"\$args\" + } + fm_os_proc_triple() { + local row + while IFS= read -r row; do + case \"\$row\" in + \"\$1|\"*) _emit_row \"\$row\"; return 0 ;; + esac + done < \"\$FM_PROVIDER_FILE\" + while IFS= read -r row; do + case \"\$row\" in + \"default|\"*) _emit_row \"\$row\"; return 0 ;; + esac + done < \"\$FM_PROVIDER_FILE\" + return 1 + } + $3 + " "$LIB" +} + +make_table() { # a version-free claude session at 700, under launchd; any + # other pid (the child bash the walk starts from) reports bash -> 700. + # Args stay neutral: a real-looking bash -c command naming python3 and a + # lib path would be matched as "interpreter running a harness script", + # which is the matcher being honest, not a bug. + local file="$TMP_ROOT/table.$$" + cat > "$file" <<'ROWS' +700|1|claude|claude --resume +1|0|launchd|/sbin/launchd +default|700|bash|bash run.sh +ROWS + printf '%s\n' "$file" +} + +# --- the fallback decision table --------------------------------------------- + +test_denied_route_fields_parse_exactly() { + # Pin every field the 126-route parse produces. The original table rows + # named claude in both identity fields, so a parse that swapped comm + # and args, or shifted a field, still passed the walk tests. An + # asymmetric record plus per-field assertions is what fails such a + # regression here. + local dir fakebin table got + dir="$TMP_ROOT/fields" + fakebin=$(fm_fakebin "$dir"); make_denied_ps "$fakebin" + table="$dir/t" + cat > "$table" <<'ROWS' +700|1|claude|/opt/tools/x --resume +ROWS + got=$(lib_eval_denied "$fakebin" "$table" \ + 'fm_proc_info 700 && printf "%s|%s|%s\n" "$FM_PROC_PPID" "$FM_PROC_COMM" "$FM_PROC_ARGS"') \ + || fail "126-route fm_proc_info refused a well-formed record" + [ "$got" = "1|claude|/opt/tools/x --resume" ] \ + || fail "126-route parse produced '$got', expected '1|claude|/opt/tools/x --resume'" + # A record whose ppid field is empty is malformed even when the rest + # reads fine, and must be refused rather than carried as a blank id. + cat > "$table" <<'ROWS' +5||bash|bash x +ROWS + if lib_eval_denied "$fakebin" "$table" 'fm_proc_info 5' >/dev/null 2>&1; then + fail "126-route accepted a record with an empty ppid field" + fi + pass "126-route parse assigns each field and refuses a blank ppid" +} + +check_shape_rejected() { #