From e7dbab2955f9ff8a7f9a8ad0707f9cc8d5da5655 Mon Sep 17 00:00:00 2001 From: Bryan Travis Smith Date: Mon, 24 Aug 2026 09:19:32 -0700 Subject: [PATCH 01/28] feat(harness): add verified agy worker adapter with gemini-3.7-flash-high default --- .agents/skills/harness-adapters/SKILL.md | 34 +++- bin/backends/tmux.sh | 3 +- bin/fm-bootstrap.sh | 5 +- bin/fm-composer-lib.sh | 4 +- bin/fm-harness.sh | 6 +- bin/fm-session-lock-lib.sh | 2 +- bin/fm-spawn.sh | 45 ++++- bin/fm-test-run.sh | 2 +- bin/fm-tmux-lib.sh | 1 - docs/verification/runtime-backends.md | 3 + tests/fm-agy-harness.test.sh | 205 +++++++++++++++++++++++ tests/lib.sh | 3 + 12 files changed, 297 insertions(+), 16 deletions(-) create mode 100755 tests/fm-agy-harness.test.sh diff --git a/.agents/skills/harness-adapters/SKILL.md b/.agents/skills/harness-adapters/SKILL.md index d20a7dfaa62..8433cdce650 100644 --- a/.agents/skills/harness-adapters/SKILL.md +++ b/.agents/skills/harness-adapters/SKILL.md @@ -3,7 +3,7 @@ name: harness-adapters description: >- Agent-only reference for firstmate harness operations. Use before spawning or recovering a crewmate or secondmate, handling a trust dialog, sending a harness-specific skill invocation, interrupting or exiting an agent, resuming an exited agent, or verifying a new harness adapter. - Contains verified facts for claude, codex, opencode, pi, pi-signed, grok, kimi, cursor, and muse. + Contains verified facts for claude, codex, opencode, pi, pi-signed, grok, kimi, cursor, muse, and agy. user-invocable: false metadata: internal: true @@ -133,6 +133,7 @@ The supported launch-profile flags below are verified locally; each row records | kimi | `--model ` | none | Verified 2026-07-25 on Kimi Code CLI 0.29.1. | | cursor | `--model ` | none | Verified 2026-08-11 on Cursor Agent CLI 2026.08.11-e8db854. No effort flag exists, so firstmate records the requested effort in task metadata and omits it from the launch. Validate ids against `cursor-agent --list-models` rather than assuming a low/medium/high family: the live catalog carries only `-high` Grok ids. | | muse | `--model ` | `--reasoning-effort `, and `ultra` only for an explicit `max` | Verified 2026-08-05 on Muse Code 0.1.0-R708.1. The flag accepts `none\|minimal\|low\|medium\|high\|xhigh\|ultra` and defaults to `high`. `ultra` is muse's max-class level, so it is reachable only through an explicit captain `max`, never from the generic fallback; `none` and `minimal` sit below the shared vocabulary and stay unreachable. | +| agy | `--model ` | `--effort ` | Verified 2026-08-24 on Antigravity CLI (`agy`) 1.1.19. Default model is `gemini-3.7-flash-high`. Model variants like `gemini-3.7-flash-high` already encode effort, so `--effort` is omitted by firstmate to avoid CLI conflict errors. `xhigh` and `max` are capped to `high`. | The concrete `harness` field owns adapter identity independently of the model provider: `harness=pi` with `model=xai/grok-*` is Pi using xAI, not `harness=grok`, and does not require Grok CLI login; `harness=grok` remains the standalone Grok Build CLI adapter. Likewise, `harness=cursor` with `model=cursor-grok-4.5-*` is Cursor Agent CLI routing a Grok model, not the xAI Grok Build `grok` harness. @@ -152,6 +153,7 @@ Use the discovery surface in the current authenticated environment because suppo | grok | Run `grok models`, which lists the models available to the current Grok installation and account. | | kimi | Run `kimi provider list --json`, which lists the current provider and model configuration. | | cursor | Run `cursor-agent --list-models` (or the legacy `agent --list-models`), which lists the ids available to the current Cursor account. `cursor` is not the CLI name. | +| agy | Run `agy models`, which lists the models available to the authenticated account. | For an unfamiliar harness or model namespace, establish support and provider identity from that harness's authoritative CLI help, model listing, or current documentation rather than guessing from a name or prefix. A listing that reaches the account and does not contain the model is concrete evidence the model is unsupported: block that candidate and quote the result. @@ -173,6 +175,7 @@ Natural language is acceptable if uncertain. - grok: `/`, for example `/no-mistakes` (same form as claude). Verified end to end: grok discovers the user-level `no-mistakes` skill, `/no-mistakes` invokes it, and grok drives a real `no-mistakes axi run`. Like codex's `$`/`/` popups, typing `/` opens grok's slash-autocomplete, so a too-fast Enter selects the popup entry instead of sending, and for an argument-taking command (like `/no-mistakes`'s optional task-first argument) that first Enter only expands the popup selection into an argument-hint placeholder rather than submitting - a genuine second Enter is required (see the grok section below for the 2026-07-03 incident and fix). `fm_tmux_submit_core`'s retried Enter (used by `fm-send` on the tmux backend) handles this through the shared structural composer classifier; the herdr backend needed a dedicated fix (`fm_backend_herdr_composer_state`, docs/herdr-backend.md) because its prior delta-based verification false-positived on that same popup-close content change. - kimi: `/`, for example `/no-mistakes`. - cursor: `/`, for example `/no-mistakes`. Cursor discovers firstmate's user-level skills. Its slash popup swallows the first Enter, so a genuine second Enter submits; the shared submit retry handles it. +- agy: `/`, for example `/no-mistakes`. ## Submission acknowledgement hazards @@ -534,3 +537,32 @@ A teardown refusal naming muse scratch is therefore correct behavior: inspect it muse is a day-0 `0.1.0` beta whose launcher polls a release channel hourly and can replace the running binary underneath the fleet, changing the process name with it. The captain accepted that risk, so firstmate does NOT set `MUSE_NO_AUTO_UPDATE=1`; a fleet that later wants stability can set it in the launch environment without any adapter change. Its plugin/hook engine reports `plugins are not available in this build` unless `MUSE_EXPERIMENTAL_PLUGINS=on`, which is why the busy source reads the session log instead of installing a hook. + +## agy (VERIFIED 2026-08-24, Antigravity CLI 1.1.19) + +Antigravity CLI launches from `PATH` (typically `/opt/homebrew/bin/agy`). + +| Fact | Value | +|---|---| +| Binary | Executable `agy` from `PATH`; verified on version 1.1.19. | +| Launch | `agy --dangerously-skip-permissions __MODELFLAG____EFFORTFLAG__--prompt-interactive "$(__OPINPUT__ encode launch-brief < __BRIEF__)"`; positional prompts are rejected with exit code 2. | +| Models | `gemini-3.7-flash-high` (default), `gemini-3.7-flash-medium`, `gemini-3.7-flash-low`, Pro models, Claude Sonnet/Opus models. Authoritative discovery: `agy models`. | +| Busy-pane signature | `esc to cancel` on bottom left, Braille spinners `[⣾⣽⣻⢿⡿⣟⣯⣷]`, and step headers (`● Bash(...)`, `⣾ Loading...`). | +| Exit command | `/exit` or `/quit` or `Ctrl+D` (exits cleanly with status 0). | +| Interrupt | Single Escape or `Ctrl+C` cancels active turn and returns to composer. | +| Skill invocation | `/`, for example `/no-mistakes`; skills in `.agents/skills/` are discovered. Typing `/` opens the interactive autocomplete menu, and typing the full skill name with Enter submits and activates the skill directly. | +| Autonomy | `--dangerously-skip-permissions` auto-approves all tool permission requests without prompting once project trust is established. | +| Trust dialog | Interactive launches (`-i`) in untrusted workspaces display a project trust dialog (`Do you trust the contents of this project?`) with `Yes, I trust this folder` as default. Firstmate peek handles this with `fm-send.sh --key Enter`. Trust persists per canonical path in `~/.gemini/antigravity-cli/settings.json` under `trustedWorkspaces`. | +| Hooks | Natively supports `.agents/hooks.json` events including `Stop`, `PreToolUse`, `PostToolUse`, `PreInvocation`, `PostInvocation`. Project hooks are strictly gated on workspace trust. | +| Environment markers | Child tool processes receive `ANTIGRAVITY_LS_VERSION=cli-1.1.19` and `ANTIGRAVITY_SOURCE_METADATA`. | +| Composer | Horizontal rule borders (`───`) enclosing prompt glyph `>`. Bordered empty composer correctly classifies as `empty`. | +| Effort | `--effort low\|medium\|high`. Firstmate omits `--effort` for variant models (e.g. `gemini-3.7-flash-high`) and caps `xhigh`/`max` to `high` for generic model names. | +| Resumption | Resumes prior conversations via `agy --conversation ` or `--continue` / `-c`. | + +`agy` delivers the launch brief through `--prompt-interactive` (`-i`) as a single interactive turn. +Positional arguments are rejected by `agy` with exit code 2, so `--prompt-interactive` is required. +Tool execution is fully autonomous once `--dangerously-skip-permissions` is passed and project trust is granted. +On first interactive launch in an untrusted directory, `agy` renders a project trust confirmation prompt. +Firstmate supervisors handle this dialog by sending `Enter` upon spawn peek. +Accepted trust is saved to `~/.gemini/antigravity-cli/settings.json` under `trustedWorkspaces` and persists across subsequent launches. +Project-local `.agents/hooks.json` hooks (including `Stop` and `PreToolUse`) are gated on workspace trust and fire normally once trust is granted. diff --git a/bin/backends/tmux.sh b/bin/backends/tmux.sh index 9eed5f3ec3e..97df3b61f08 100644 --- a/bin/backends/tmux.sh +++ b/bin/backends/tmux.sh @@ -170,7 +170,7 @@ fm_backend_tmux_classify_process_name() { # [argv0] -> agent|shell|other # cannot carry it either: ~/.local/bin/muse-bin- has no `muse` path # COMPONENT, so the fm_harness_path_name fallback below never fires for it. muse|muse-bin-*) printf 'agent' ;; - *claude*|*codex*|*opencode*|*grok*|*kimi*|pi|pi-signed|pi-launcher|Pi) printf 'agent' ;; + *claude*|*codex*|*opencode*|*grok*|*kimi*|*agy*|pi|pi-signed|pi-launcher|Pi) printf 'agent' ;; zsh|bash|sh|dash|ash|ksh|mksh|tcsh|csh|fish) printf 'shell' ;; *) if fm_harness_path_name "$path" >/dev/null || fm_harness_path_name "$argv0" >/dev/null; then @@ -341,6 +341,7 @@ EOF '') printf 'unreadable'; return 0 ;; esac case "$(fm_backend_tmux_classify_process_name "$comm")" in + agent) printf 'alive' ;; shell) printf 'dead' ;; *) printf 'ambiguous' ;; esac diff --git a/bin/fm-bootstrap.sh b/bin/fm-bootstrap.sh index 62568cf5771..2edf84a48e3 100755 --- a/bin/fm-bootstrap.sh +++ b/bin/fm-bootstrap.sh @@ -768,7 +768,7 @@ secondmate_liveness_one() { # [ -n "$target" ] || target="$window" agent_state=$(fm_backend_agent_state "$backend" "$target" 2>/dev/null) || agent_state=unreadable case "$harness" in - claude|codex|opencode|pi|pi-signed|grok|kimi) ;; + claude|codex|opencode|pi|pi-signed|grok|kimi|cursor|muse|agy) ;; *) case "$agent_state" in dead|missing) agent_state=unverified-harness ;; esac ;; @@ -1080,7 +1080,7 @@ crew_dispatch_validate() { return 0 fi err=$(jq -r ' - def verified($h): ["claude","codex","opencode","pi","pi-signed","grok","kimi","cursor","muse"] | index($h); + def verified($h): ["claude","codex","opencode","pi","pi-signed","grok","kimi","cursor","muse","agy"] | index($h); def effort_ok($h; $e): if $e == null then true elif ($e | type) != "string" then false @@ -1089,6 +1089,7 @@ crew_dispatch_validate() { elif $h == "grok" then (["low","medium","high"] | index($e)) elif $h == "pi" or $h == "pi-signed" then (["low","medium","high","xhigh","max"] | index($e)) elif $h == "muse" then (["low","medium","high","xhigh","max"] | index($e)) + elif $h == "agy" then (["low","medium","high"] | index($e)) elif $h == "opencode" or $h == "kimi" or $h == "cursor" then false else true end; diff --git a/bin/fm-composer-lib.sh b/bin/fm-composer-lib.sh index 07b3b02fffb..9c6e885044a 100644 --- a/bin/fm-composer-lib.sh +++ b/bin/fm-composer-lib.sh @@ -311,7 +311,7 @@ fm_composer_strip_ghost() { # part of that union for the same reason the others are: without it a cursor # submit could never be acknowledged, because cursor parks its terminal cursor # outside its composer and the composer verdict is therefore always `unknown`. -FM_DELIVERY_BUSY_REGEX_DEFAULT='esc (to )?interrupt|Working\.\.\.|Ctrl\+c:cancel|ctrl\+c to stop' +FM_DELIVERY_BUSY_REGEX_DEFAULT='esc (to )?(interrupt|cancel)|Working\.\.\.|Ctrl\+c:cancel|ctrl\+c to stop' FM_DELIVERY_CLAUDE_BUSY_REGEX_DEFAULT='esc to interrupt|…[[:space:]]+\([0-9]+[smh]' FM_DELIVERY_CODEX_BUSY_REGEX_DEFAULT='esc to interrupt' FM_DELIVERY_OPENCODE_BUSY_REGEX_DEFAULT='esc interrupt' @@ -326,6 +326,7 @@ FM_DELIVERY_GROK_BUSY_REGEX_DEFAULT='Ctrl\+c:cancel' # bin/fm-busy-lib.sh, never from this row. FM_DELIVERY_CURSOR_BUSY_REGEX_DEFAULT='ctrl\+c to stop' FM_DELIVERY_KIMI_BUSY_REGEX_DEFAULT='^[[:space:]]*(🌑|🌒|🌓|🌔|🌕|🌖|🌗|🌘)[[:space:]]+·[[:space:]]+' +FM_DELIVERY_AGY_BUSY_REGEX_DEFAULT='esc to cancel|⣾|⣽|⣻|⢿|⡿|⣟|⣯|⣷|Loading\.\.\.' fm_busy_lines_match() { # [harness] local harness=${1:-} lines regex @@ -341,6 +342,7 @@ fm_busy_lines_match() { # [harness] grok) regex=$FM_DELIVERY_GROK_BUSY_REGEX_DEFAULT ;; kimi) regex=$FM_DELIVERY_KIMI_BUSY_REGEX_DEFAULT ;; cursor) regex=$FM_DELIVERY_CURSOR_BUSY_REGEX_DEFAULT ;; + agy) regex=$FM_DELIVERY_AGY_BUSY_REGEX_DEFAULT ;; '') regex=$FM_DELIVERY_BUSY_REGEX_DEFAULT ;; *) # A supplied harness must never borrow another harness's signature. diff --git a/bin/fm-harness.sh b/bin/fm-harness.sh index 1683df796f2..c04f7be21ee 100755 --- a/bin/fm-harness.sh +++ b/bin/fm-harness.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash # Detect the agent harness this process tree runs on. -# Usage: fm-harness.sh print own harness: claude|codex|opencode|pi|pi-signed|grok|kimi|cursor|muse|unknown +# Usage: fm-harness.sh print own harness: claude|codex|opencode|pi|pi-signed|grok|kimi|cursor|muse|agy|unknown # fm-harness.sh crew print the effective CREWMATE harness # (config/crew-harness; "default" resolves to own) # fm-harness.sh secondmate print the harness the PRIMARY uses to launch @@ -72,6 +72,8 @@ detect_own() { # by ancestry alone below. Do NOT promote MUSE_CURRENT_SESSION_LOG to a marker # without verifying it reaches children AND that it cannot survive in a # multiplexer's stored environment, which is the precedence hazard above. + # agy sets ANTIGRAVITY_LS_VERSION and ANTIGRAVITY_SOURCE_METADATA for child/tool processes. + [ -n "${ANTIGRAVITY_LS_VERSION:-}" ] || [ -n "${ANTIGRAVITY_SOURCE_METADATA:-}" ] && { echo agy; return; } # Layer 2: walk the parent chain and match the command name. local pid=$$ comm args argv0 for _ in 1 2 3 4 5 6 7 8; do @@ -86,6 +88,7 @@ detect_own() { *codex*) echo codex; return ;; *opencode*) echo opencode; return ;; *grok*) echo grok; return ;; + *agy*) echo agy; return ;; kimi) echo kimi; return ;; # muse's installed launcher ~/.local/bin/muse execs ~/.local/bin/muse-bin- # (verified in the published launcher, muse 0.1.0-R708.1), so the live process @@ -103,6 +106,7 @@ detect_own() { *codex*) echo codex; return ;; *opencode*) echo opencode; return ;; *grok*) echo grok; return ;; + *agy*) echo agy; return ;; *" pi "*|*/pi) echo pi; return ;; esac ;; esac diff --git a/bin/fm-session-lock-lib.sh b/bin/fm-session-lock-lib.sh index d77e563f0b4..85af8626052 100644 --- a/bin/fm-session-lock-lib.sh +++ b/bin/fm-session-lock-lib.sh @@ -17,7 +17,7 @@ . "$(dirname -- "${BASH_SOURCE[0]}")/fm-cursor-lib.sh" # Known harness command names; extend when a new adapter is verified. -FM_HARNESS_RE='claude|codex|opencode|grok|kimi|^pi$|^pi-signed$' +FM_HARNESS_RE='claude|codex|opencode|grok|kimi|agy|^pi$|^pi-signed$' # The same harnesses as exact executable names. Keep in sync with # FM_HARNESS_RE. Used only for the stricter path evidence below, where the diff --git a/bin/fm-spawn.sh b/bin/fm-spawn.sh index 325eefd389c..594f16c894a 100755 --- a/bin/fm-spawn.sh +++ b/bin/fm-spawn.sh @@ -104,7 +104,7 @@ # profile consultation. A --secondmate spawn is exempt and resolves the SECONDMATE # harness (config/secondmate-harness -> config/crew-harness -> own), so the # secondmate-vs-crewmate split is DURABLE across every respawn (recovery, -# /updatefirstmate, restart). A bare adapter name (claude|codex|opencode|pi|pi-signed|grok|kimi|cursor|muse) +# /updatefirstmate, restart). A bare adapter name (claude|codex|opencode|pi|pi-signed|grok|kimi|cursor|muse|agy) # overrides it for this spawn (either kind). A non-flag string containing # whitespace is treated as a RAW launch command - the escape hatch for verifying # new adapters. For pi and pi-signed, fm-spawn resolves the selected executable @@ -1061,7 +1061,7 @@ if [ "$RELAUNCH" -eq 1 ]; then } elif [ "$KIND" = secondmate ]; then case "${POS[1]:-}" in - ''|claude|codex|opencode|pi|pi-signed|grok|kimi|cursor|muse) + ''|claude|codex|opencode|pi|pi-signed|grok|kimi|cursor|muse|agy) ARG3=${POS[1]:-} ;; *' '*) @@ -1073,8 +1073,12 @@ elif [ "$KIND" = secondmate ]; then fi ;; *) - FIRSTMATE_HOME=${POS[1]} - ARG3=${POS[2]:-} + if [ "${#POS[@]}" -gt 2 ] || [ -d "${POS[1]}" ]; then + FIRSTMATE_HOME=${POS[1]} + ARG3=${POS[2]:-} + else + ARG3=${POS[1]} + fi ;; esac else @@ -1191,6 +1195,10 @@ launch_template() { # written below. Nothing to place in the template for it. # codex, opencode, and kimi are also markerless and share this inherited-marker hazard; changing their verified launch boundaries belongs in follow-up work. muse) printf '%s' 'env -u CLAUDECODE -u PI_CODING_AGENT -u GROK_AGENT -u FM_PI_HARNESS XDG_CONFIG_HOME=__MUSECONFIG__ XDG_DATA_HOME=__MUSEDATA__ MUSE_EXPERIMENTAL_FOREIGN_PERSONAL_CONTEXT_KILL=on __MUSEBIN__ --yolo __MODELFLAG____EFFORTFLAG__"$(__OPINPUT__ encode launch-brief < __BRIEF__)"' ;; + # agy (Antigravity CLI): --prompt-interactive (-i) delivers the brief to start + # the supervised interactive session. --dangerously-skip-permissions auto-approves + # tool execution without per-tool confirmation prompts. + agy) printf '%s' 'agy --dangerously-skip-permissions __MODELFLAG____EFFORTFLAG__--prompt-interactive "$(__OPINPUT__ encode launch-brief < __BRIEF__)"' ;; *) return 1 ;; esac } @@ -1295,6 +1303,12 @@ if [ "$KIND" = secondmate ] && [ -z "$ARG3" ]; then fi fi +if [ "$HARNESS" = agy ]; then + if [ "$MODEL_SET" -eq 0 ] || [ -z "$MODEL" ] || [ "$MODEL" = default ]; then + MODEL="gemini-3.7-flash-high" + fi +fi + secondmate_registry_value() { secondmate_registry_field "$DATA/secondmates.md" "$1" "$2" } @@ -1376,14 +1390,14 @@ model_flag_for_harness() { local harness=$1 model=$2 [ -n "$model" ] && [ "$model" != default ] || return 0 case "$harness" in - claude|codex|opencode|pi|pi-signed|grok|kimi|cursor|muse) + claude|codex|opencode|pi|pi-signed|grok|kimi|cursor|muse|agy) printf -- '--model %s ' "$(shell_quote "$model")" ;; esac } effort_flag_for_harness() { - local harness=$1 effort=$2 + local harness=$1 effort=$2 model=${3:-} [ -n "$effort" ] && [ "$effort" != default ] || return 0 case "$harness" in claude) @@ -1429,6 +1443,23 @@ effort_flag_for_harness() { max) printf -- '--reasoning-effort %s ' "$(shell_quote ultra)" ;; esac ;; + agy) + # agy accepts --effort low|medium|high. + # When the selected model already encodes reasoning effort (e.g. *-low, + # *-medium, *-high), agy rejects a conflicting --effort flag and redundant + # effort flags are unnecessary, so omit --effort. + # For base/generic models (e.g. gemini-3.7-flash), map requested effort; + # cap unsupported xhigh/max to high. + case "$model" in + *-low|*-medium|*-high) ;; + *) + case "$effort" in + low|medium|high) printf -- '--effort %s ' "$(shell_quote "$effort")" ;; + xhigh|max) printf -- '--effort %s ' "$(shell_quote "high")" ;; + esac + ;; + esac + ;; # opencode's interactive `opencode --prompt` launch has a verified --model # flag but no verified effort flag. Its `opencode run --variant` flag belongs # to a different, non-interactive launch mode, so fm-spawn does not pass it. @@ -2728,7 +2759,7 @@ sq_piwatch=$(shell_quote "$PROJ_ABS/.pi/extensions/fm-primary-pi-watch.ts") sq_opinput=$(shell_quote "$FM_ROOT/bin/fm-operational-input.sh") sq_worktree=$(shell_quote "$WT") MODELFLAG=$(model_flag_for_harness "$HARNESS" "$MODEL") -EFFORTFLAG=$(effort_flag_for_harness "$HARNESS" "$EFFORT") +EFFORTFLAG=$(effort_flag_for_harness "$HARNESS" "$EFFORT" "$MODEL") LAUNCH=${LAUNCH//__MODELFLAG__/$MODELFLAG} LAUNCH=${LAUNCH//__EFFORTFLAG__/$EFFORTFLAG} LAUNCH=${LAUNCH//__BRIEF__/$sq_brief} diff --git a/bin/fm-test-run.sh b/bin/fm-test-run.sh index 2d3a64bcadb..2ad1f69c37b 100755 --- a/bin/fm-test-run.sh +++ b/bin/fm-test-run.sh @@ -132,7 +132,7 @@ now_ms() { # unclassified so new tests are still runnable and visible in summaries. family_for_basename() { case "$1" in - fm-arm-pretool-check.test.sh|fm-ask-user-authority.test.sh|\ + fm-agy-harness.test.sh|fm-arm-pretool-check.test.sh|fm-ask-user-authority.test.sh|\ fm-bearings-board.test.sh|\ fm-brief.test.sh|fm-vendor-auth-probe.test.sh|\ fm-calm-pi-extension.test.sh|fm-cd-pretool-check.test.sh|\ diff --git a/bin/fm-tmux-lib.sh b/bin/fm-tmux-lib.sh index 7523d8b1c36..a1bf3dc6102 100755 --- a/bin/fm-tmux-lib.sh +++ b/bin/fm-tmux-lib.sh @@ -46,7 +46,6 @@ # shellcheck source=bin/fm-cursor-lib.sh . "$(dirname -- "${BASH_SOURCE[0]}")/fm-cursor-lib.sh" - # fm_tmux_strip_ghost: thin adapter over the shared, fleet-wide ghost extractor # fm_composer_strip_ghost (bin/fm-composer-lib.sh). It drops de-emphasised # ghost/placeholder runs - dim/faint (SGR 2, claude's/codex's/cursor's ghost) AND a diff --git a/docs/verification/runtime-backends.md b/docs/verification/runtime-backends.md index 0e918ad5c46..0c9a59a79a2 100644 --- a/docs/verification/runtime-backends.md +++ b/docs/verification/runtime-backends.md @@ -28,6 +28,9 @@ zsh ``` A persistent parent shell waiting for a child remained reported as the parent process, while a shell that directly execed a simple command changed identity with the process itself. +Claude, Codex, OpenCode, and Grok were observed under their own process names. +Kimi Code CLI 0.29.1 was observed under `kimi` on 2026-07-25. +Antigravity CLI (`agy`) 1.1.19 was observed under `agy` on 2026-08-24. Pi and pi-signed 0.82.0 were reverified on 2026-07-27 through real isolated `fm-spawn.sh` launches. ### Agent liveness name sources diff --git a/tests/fm-agy-harness.test.sh b/tests/fm-agy-harness.test.sh new file mode 100755 index 00000000000..36b00e5f17b --- /dev/null +++ b/tests/fm-agy-harness.test.sh @@ -0,0 +1,205 @@ +#!/usr/bin/env bash +# Behavior tests for the verified Antigravity CLI (agy) crewmate adapter. +set -u + +# shellcheck source=tests/lib.sh +. "$(dirname "${BASH_SOURCE[0]}")/lib.sh" + +SPAWN="$ROOT/bin/fm-spawn.sh" +HARNESS_SH="$ROOT/bin/fm-harness.sh" +BOOTSTRAP_SH="$ROOT/bin/fm-bootstrap.sh" +TMUX_LIB="$ROOT/bin/fm-tmux-lib.sh" +BACKEND_SH="$ROOT/bin/fm-backend.sh" +TMP_ROOT=$(fm_test_tmproot fm-agy-harness) + +make_spawn_fakebin() { + local dir=$1 fakebin + fakebin=$(fm_fakebin "$dir") + cat > "$fakebin/tmux" <<'SH' +#!/usr/bin/env bash +set -u +case "$*" in + *"#{pane_current_path}"*) printf "%s\n" "${FM_FAKE_PANE_PATH:-}"; exit 0 ;; +esac +case "${1:-}" in + display-message) printf "firstmate\n"; exit 0 ;; + list-windows) exit 0 ;; + has-session|new-session|new-window|kill-window) exit 0 ;; + send-keys) + if [ -n "${FM_FAKE_LAUNCH_LOG:-}" ]; then + prev= + for a in "$@"; do + if [ "$prev" = "-l" ]; then + printf "%s\n" "$a" >> "$FM_FAKE_LAUNCH_LOG" + fi + prev=$a + done + fi + exit 0 + ;; +esac +exit 0 +SH + chmod +x "$fakebin/tmux" + fm_fake_exit0 "$fakebin" treehouse gh-axi gh agy + printf "%s\n" "$fakebin" +} + +make_spawn_case() { + local name=$1 case_dir home proj wt fakebin launchlog id + case_dir="$TMP_ROOT/$name" + home="$case_dir/home" + proj="$case_dir/project" + wt="$case_dir/wt" + launchlog="$case_dir/launch.log" + fakebin=$(make_spawn_fakebin "$case_dir/fake") + id="agy-$name-x1" + mkdir -p "$home/data/$id" "$home/projects" "$home/state" "$home/config" + printf "brief for %s\n" "$id" > "$home/data/$id/brief.md" + fm_git_worktree "$proj" "$wt" "fm/$id" + touch "$home/state/.last-watcher-beat" + printf "%s|%s|%s|%s|%s|%s|%s\n" "$case_dir" "$home" "$proj" "$wt" "$fakebin" "$launchlog" "$id" +} + +run_agy_spawn() { + local home=$1 proj=$2 wt=$3 fakebin=$4 launchlog=$5 id=$6 + shift 6 + : > "$launchlog" + FM_ROOT_OVERRIDE="" FM_HOME="$home" \ + FM_STATE_OVERRIDE="$home/state" FM_DATA_OVERRIDE="$home/data" \ + FM_PROJECTS_OVERRIDE="$home/projects" FM_CONFIG_OVERRIDE="$home/config" \ + FM_SPAWN_NO_GUARD=1 FM_FAKE_PANE_PATH="$wt" TMUX="fake,1,0" \ + FM_FAKE_LAUNCH_LOG="$launchlog" PATH="$fakebin:$PATH" \ + "$SPAWN" "$id" "$proj" agy --mode no-mistakes --yolo off "$@" 2>&1 +} + +test_agy_harness_detection() { + local out + out=$(ANTIGRAVITY_LS_VERSION=cli-1.1.19 "$HARNESS_SH") + [ "$out" = "agy" ] || fail "harness detection failed on ANTIGRAVITY_LS_VERSION marker: $out" + + out=$(ANTIGRAVITY_SOURCE_METADATA='{"conversationId":"123"}' "$HARNESS_SH") + [ "$out" = "agy" ] || fail "harness detection failed on ANTIGRAVITY_SOURCE_METADATA marker: $out" + + # shellcheck source=bin/fm-session-lock-lib.sh + . "$ROOT/bin/fm-session-lock-lib.sh" + printf "agy\n" | grep -qE "$FM_HARNESS_RE" || fail "FM_HARNESS_RE did not match agy" + printf "foo-agy-bar\n" | grep -qE "$FM_HARNESS_RE" || fail "FM_HARNESS_RE did not match *agy*" + pass "fm-harness and session-lock: detects agy markers and process identity" +} + +test_agy_default_model_and_launch_template() { + local rec case_dir home proj wt fakebin launchlog id out launched meta_file + rec=$(make_spawn_case default-model) + IFS="|" read -r case_dir home proj wt fakebin launchlog id < + rec=$(make_spawn_case base-effort) + IFS="|" read -r case_dir home proj wt fakebin launchlog id < "$config/crew-dispatch.json" + out=$(FM_HOME="$home" "$BOOTSTRAP_SH" 2>&1) + assert_not_contains "$out" "CREW_DISPATCH: invalid" "crew-dispatch was rejected" + + # Invalid effort for agy + printf "%s\n" '{"default":{"harness":"agy","effort":"bad-effort"}}' > "$config/crew-dispatch.json" + out=$(FM_HOME="$home" "$BOOTSTRAP_SH" 2>&1) + assert_contains "$out" "CREW_DISPATCH: invalid config/crew-dispatch.json - invalid effort: agy:bad-effort" \ + "bootstrap did not reject invalid agy effort: $out" + pass "fm-bootstrap: validates agy harness and accepted efforts in crew-dispatch.json" +} + +test_agy_harness_detection +test_agy_default_model_and_launch_template +test_agy_effort_flag_handling +test_agy_busy_matching_and_liveness +test_agy_crew_dispatch_validation diff --git a/tests/lib.sh b/tests/lib.sh index 915741ba0d5..ab67f8a5130 100644 --- a/tests/lib.sh +++ b/tests/lib.sh @@ -34,6 +34,9 @@ FM_TEST_LIB_SOURCED=1 # strips this to verify real refusal. export FM_GATE_REFUSE_BYPASS=1 +# Clean ambient harness markers so test assertions remain isolated and deterministic. +unset -v CLAUDECODE GROK_AGENT ANTIGRAVITY_LS_VERSION ANTIGRAVITY_SOURCE_METADATA PI_CODING_AGENT FM_PI_HARNESS 2>/dev/null || true + # Resolve the repo root from this library's own location. Consumed by sourcing # test files, not by this library, so it reads as "unused" here. # shellcheck disable=SC2034 From 8592829c7b7ad6c60fa07cda610dfbbc9631fba6 Mon Sep 17 00:00:00 2001 From: Bryan Travis Smith Date: Mon, 24 Aug 2026 09:52:37 -0700 Subject: [PATCH 02/28] no-mistakes(review): Complete agy worker lifecycle integration --- .agents/skills/harness-adapters/SKILL.md | 18 +-- AGENTS.md | 2 +- bin/fm-bootstrap.sh | 4 +- bin/fm-busy-lib.sh | 2 + bin/fm-composer-lib.sh | 2 +- bin/fm-control-lib.sh | 38 +++++-- bin/fm-control.sh | 2 +- bin/fm-session-lock-lib.sh | 2 +- bin/fm-spawn.sh | 78 ++++++++++++- bin/fm-teardown.sh | 16 ++- docs/agent-control.md | 4 +- docs/architecture.md | 4 +- docs/configuration.md | 3 + docs/trace-context.md | 2 +- docs/verification/supervision.md | 1 + tests/fm-agy-harness.test.sh | 133 +++++++++++++++++++++-- tests/fm-control-relaunch.test.sh | 17 +++ tests/fm-control.test.sh | 23 +++- 18 files changed, 302 insertions(+), 49 deletions(-) diff --git a/.agents/skills/harness-adapters/SKILL.md b/.agents/skills/harness-adapters/SKILL.md index 8433cdce650..c9d98cefeda 100644 --- a/.agents/skills/harness-adapters/SKILL.md +++ b/.agents/skills/harness-adapters/SKILL.md @@ -65,7 +65,8 @@ The primary integrations for `claude`, `codex`, `opencode`, `pi`, `pi-signed`, ` Grok selects native blocking or its pre-native bounded resume fallback from the exact running Stop payload; [`docs/turnend-guard.md`](../../../docs/turnend-guard.md) owns that contract. Kimi is outside the primary turn-end guard scope, while `docs/turnend-guard.md` owns its separate guarded global hook for crew wake signals. muse is CREWMATE/SCOUT ONLY and has no primary integration at all: its plugin engine (its only hook surface) is disabled in the default build, and its Claude-compatible hook dialect names `asyncRewake` and model reawakening as explicitly unsupported, which is exactly what a firstmate primary's turn-end supervision needs. -`bin/fm-spawn.sh` refuses a `--secondmate` launch on muse for that reason. +agy is also CREWMATE/SCOUT ONLY because no primary session-start, turn-end, or watcher supervision protocol has been verified for it. +`bin/fm-spawn.sh` refuses a `--secondmate` launch on either adapter for that reason. cursor HAS a full hooks system: 20 lifecycle events configurable at project scope in `.cursor/hooks.json`, plus a Claude-Code compatibility name map that also loads `/.claude/settings.json`. Its `stop` step cannot block - exit 2 there is a silent no-op - so `bin/fm-turnend-guard-cursor.sh` parks the turn boundary on the watcher and returns one bounded `followup_message` instead. Because Cursor loads the tracked Claude settings too, every Claude-shaped entrypoint whose event Cursor covers stands down on a Cursor-delivered payload. @@ -133,7 +134,7 @@ The supported launch-profile flags below are verified locally; each row records | kimi | `--model ` | none | Verified 2026-07-25 on Kimi Code CLI 0.29.1. | | cursor | `--model ` | none | Verified 2026-08-11 on Cursor Agent CLI 2026.08.11-e8db854. No effort flag exists, so firstmate records the requested effort in task metadata and omits it from the launch. Validate ids against `cursor-agent --list-models` rather than assuming a low/medium/high family: the live catalog carries only `-high` Grok ids. | | muse | `--model ` | `--reasoning-effort `, and `ultra` only for an explicit `max` | Verified 2026-08-05 on Muse Code 0.1.0-R708.1. The flag accepts `none\|minimal\|low\|medium\|high\|xhigh\|ultra` and defaults to `high`. `ultra` is muse's max-class level, so it is reachable only through an explicit captain `max`, never from the generic fallback; `none` and `minimal` sit below the shared vocabulary and stay unreachable. | -| agy | `--model ` | `--effort ` | Verified 2026-08-24 on Antigravity CLI (`agy`) 1.1.19. Default model is `gemini-3.7-flash-high`. Model variants like `gemini-3.7-flash-high` already encode effort, so `--effort` is omitted by firstmate to avoid CLI conflict errors. `xhigh` and `max` are capped to `high`. | +| agy | `--model ` | `--effort ` | Verified 2026-08-24 on Antigravity CLI (`agy`) 1.1.19. Default model is `gemini-3.7-flash-high`; an explicit low or medium effort without a model selects the matching variant. Model variants already encode effort, so matching `--effort` is omitted and conflicting combinations are refused. `xhigh` and `max` are capped to `high`. | The concrete `harness` field owns adapter identity independently of the model provider: `harness=pi` with `model=xai/grok-*` is Pi using xAI, not `harness=grok`, and does not require Grok CLI login; `harness=grok` remains the standalone Grok Build CLI adapter. Likewise, `harness=cursor` with `model=cursor-grok-4.5-*` is Cursor Agent CLI routing a Grok model, not the xAI Grok Build `grok` harness. @@ -538,25 +539,27 @@ muse is a day-0 `0.1.0` beta whose launcher polls a release channel hourly and c The captain accepted that risk, so firstmate does NOT set `MUSE_NO_AUTO_UPDATE=1`; a fleet that later wants stability can set it in the launch environment without any adapter change. Its plugin/hook engine reports `plugins are not available in this build` unless `MUSE_EXPERIMENTAL_PLUGINS=on`, which is why the busy source reads the session log instead of installing a hook. -## agy (VERIFIED 2026-08-24, Antigravity CLI 1.1.19) +## agy (VERIFIED CREWMATE/SCOUT 2026-08-24, Antigravity CLI 1.1.19) Antigravity CLI launches from `PATH` (typically `/opt/homebrew/bin/agy`). +It is not verified for secondmate or primary work. | Fact | Value | |---|---| | Binary | Executable `agy` from `PATH`; verified on version 1.1.19. | | Launch | `agy --dangerously-skip-permissions __MODELFLAG____EFFORTFLAG__--prompt-interactive "$(__OPINPUT__ encode launch-brief < __BRIEF__)"`; positional prompts are rejected with exit code 2. | | Models | `gemini-3.7-flash-high` (default), `gemini-3.7-flash-medium`, `gemini-3.7-flash-low`, Pro models, Claude Sonnet/Opus models. Authoritative discovery: `agy models`. | -| Busy-pane signature | `esc to cancel` on bottom left, Braille spinners `[⣾⣽⣻⢿⡿⣟⣯⣷]`, and step headers (`● Bash(...)`, `⣾ Loading...`). | +| Busy state | Firstmate's generated plugin uses `PreInvocation` to open the turn and `Stop` to close it. | +| Delivery busy token | `esc to cancel`, the live footer present only while a turn is running. | | Exit command | `/exit` or `/quit` or `Ctrl+D` (exits cleanly with status 0). | | Interrupt | Single Escape or `Ctrl+C` cancels active turn and returns to composer. | | Skill invocation | `/`, for example `/no-mistakes`; skills in `.agents/skills/` are discovered. Typing `/` opens the interactive autocomplete menu, and typing the full skill name with Enter submits and activates the skill directly. | | Autonomy | `--dangerously-skip-permissions` auto-approves all tool permission requests without prompting once project trust is established. | | Trust dialog | Interactive launches (`-i`) in untrusted workspaces display a project trust dialog (`Do you trust the contents of this project?`) with `Yes, I trust this folder` as default. Firstmate peek handles this with `fm-send.sh --key Enter`. Trust persists per canonical path in `~/.gemini/antigravity-cli/settings.json` under `trustedWorkspaces`. | -| Hooks | Natively supports `.agents/hooks.json` events including `Stop`, `PreToolUse`, `PostToolUse`, `PreInvocation`, `PostInvocation`. Project hooks are strictly gated on workspace trust. | +| Hooks | Natively supports `.agents/hooks.json` and project plugin hooks including `Stop`, `PreToolUse`, `PostToolUse`, `PreInvocation`, `PostInvocation`. Project hooks are strictly gated on workspace trust. | | Environment markers | Child tool processes receive `ANTIGRAVITY_LS_VERSION=cli-1.1.19` and `ANTIGRAVITY_SOURCE_METADATA`. | | Composer | Horizontal rule borders (`───`) enclosing prompt glyph `>`. Bordered empty composer correctly classifies as `empty`. | -| Effort | `--effort low\|medium\|high`. Firstmate omits `--effort` for variant models (e.g. `gemini-3.7-flash-high`) and caps `xhigh`/`max` to `high` for generic model names. | +| Effort | `--effort low\|medium\|high`. Without an explicit model, low and medium select matching Flash variants while all other levels select the high default. Firstmate omits `--effort` for variant models, refuses a conflicting explicit combination, and caps `xhigh`/`max` to `high` for generic model names. | | Resumption | Resumes prior conversations via `agy --conversation ` or `--continue` / `-c`. | `agy` delivers the launch brief through `--prompt-interactive` (`-i`) as a single interactive turn. @@ -565,4 +568,5 @@ Tool execution is fully autonomous once `--dangerously-skip-permissions` is pass On first interactive launch in an untrusted directory, `agy` renders a project trust confirmation prompt. Firstmate supervisors handle this dialog by sending `Enter` upon spawn peek. Accepted trust is saved to `~/.gemini/antigravity-cli/settings.json` under `trustedWorkspaces` and persists across subsequent launches. -Project-local `.agents/hooks.json` hooks (including `Stop` and `PreToolUse`) are gated on workspace trust and fire normally once trust is granted. +Firstmate writes an isolated project plugin under `.agents/plugins/` so existing project hooks remain untouched. +Its `PreInvocation` and `Stop` hooks are gated on workspace trust and fire normally once trust is granted. diff --git a/AGENTS.md b/AGENTS.md index a50f6afe5cf..4c051ecc344 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -192,7 +192,7 @@ A silent bootstrap section needs no action; for any printed actionable diagnosti ## 4. Harness and runtime dispatch Load `harness-adapters` before every spawn or recovery and before trust handling, skill invocation, interrupt, exit, resume, or adapter verification. -The verified harnesses are `claude`, `codex`, `opencode`, `pi`, `pi-signed`, `grok`, `kimi`, and `cursor`, plus `muse` for crewmates and scouts only; never dispatch on an unverified adapter. +The verified harnesses are `claude`, `codex`, `opencode`, `pi`, `pi-signed`, `grok`, `kimi`, and `cursor`, plus `muse` and `agy` for crewmates and scouts only; never dispatch on an unverified adapter or outside an adapter's verified task kinds. 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. `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. diff --git a/bin/fm-bootstrap.sh b/bin/fm-bootstrap.sh index 2edf84a48e3..7f5d9b65534 100755 --- a/bin/fm-bootstrap.sh +++ b/bin/fm-bootstrap.sh @@ -768,7 +768,7 @@ secondmate_liveness_one() { # [ -n "$target" ] || target="$window" agent_state=$(fm_backend_agent_state "$backend" "$target" 2>/dev/null) || agent_state=unreadable case "$harness" in - claude|codex|opencode|pi|pi-signed|grok|kimi|cursor|muse|agy) ;; + claude|codex|opencode|pi|pi-signed|grok|kimi|cursor) ;; *) case "$agent_state" in dead|missing) agent_state=unverified-harness ;; esac ;; @@ -1089,7 +1089,7 @@ crew_dispatch_validate() { elif $h == "grok" then (["low","medium","high"] | index($e)) elif $h == "pi" or $h == "pi-signed" then (["low","medium","high","xhigh","max"] | index($e)) elif $h == "muse" then (["low","medium","high","xhigh","max"] | index($e)) - elif $h == "agy" then (["low","medium","high"] | index($e)) + elif $h == "agy" then (["low","medium","high","xhigh","max"] | index($e)) elif $h == "opencode" or $h == "kimi" or $h == "cursor" then false else true end; diff --git a/bin/fm-busy-lib.sh b/bin/fm-busy-lib.sh index 489ba99bfca..4f81fec95c7 100755 --- a/bin/fm-busy-lib.sh +++ b/bin/fm-busy-lib.sh @@ -31,6 +31,7 @@ # pi-ext Pi/pi-signed per-task extension (agent_start/agent_settled) # opencode-plugin OpenCode per-task plugin (session.status) # claude-hook Claude lifecycle hooks (UserPromptSubmit/Stop/StopFailure/SessionEnd) +# agy-hook Antigravity CLI plugin hooks (PreInvocation/Stop) # codex-hook, codex-appserver reserved: Codex, gated by # fm_busy_codex_semantic_source # kimi-wire, kimi-hook reserved: standalone Kimi, gated by fm_busy_kimi_verified @@ -192,6 +193,7 @@ fm_busy_sources_for_harness() { # ;; opencode*) adapter=opencode-plugin ;; pi|pi-signed) adapter=pi-ext ;; + agy*) adapter=agy-hook ;; kimi*) fm_busy_kimi_verified || { printf ''; return 0; } adapter='kimi-wire kimi-hook' diff --git a/bin/fm-composer-lib.sh b/bin/fm-composer-lib.sh index 9c6e885044a..1dfe7ead6c2 100644 --- a/bin/fm-composer-lib.sh +++ b/bin/fm-composer-lib.sh @@ -326,7 +326,7 @@ FM_DELIVERY_GROK_BUSY_REGEX_DEFAULT='Ctrl\+c:cancel' # bin/fm-busy-lib.sh, never from this row. FM_DELIVERY_CURSOR_BUSY_REGEX_DEFAULT='ctrl\+c to stop' FM_DELIVERY_KIMI_BUSY_REGEX_DEFAULT='^[[:space:]]*(🌑|🌒|🌓|🌔|🌕|🌖|🌗|🌘)[[:space:]]+·[[:space:]]+' -FM_DELIVERY_AGY_BUSY_REGEX_DEFAULT='esc to cancel|⣾|⣽|⣻|⢿|⡿|⣟|⣯|⣷|Loading\.\.\.' +FM_DELIVERY_AGY_BUSY_REGEX_DEFAULT='esc to cancel' fm_busy_lines_match() { # [harness] local harness=${1:-} lines regex diff --git a/bin/fm-control-lib.sh b/bin/fm-control-lib.sh index 820444f58d5..1f5520efa09 100644 --- a/bin/fm-control-lib.sh +++ b/bin/fm-control-lib.sh @@ -37,8 +37,9 @@ # `resume` is deliberately NOT a verb. It is not deterministic across the # verified adapters: codex and grok resume only from a session id printed at # exit, opencode resumes the most recent session for the cwd with --continue, -# and claude, pi, pi-signed, and kimi have no verified pane-resume contract at -# all. `relaunch` covers the same need deterministically for every adapter, +# agy exposes both id-based and continue modes without a verified exact-selection +# contract, and claude, pi, pi-signed, and kimi have no verified pane-resume +# contract at all. `relaunch` covers the same need deterministically for every adapter, # because the brief on disk - not a harness-private session - is the durable # instruction. @@ -63,7 +64,7 @@ fm_control_verb_allowed() { # # than guessed at, exactly as a spawn on it would be. fm_control_harness_supported() { # case "${1-}" in - claude|codex|opencode|pi|pi-signed|grok|kimi|cursor|muse) return 0 ;; + claude|codex|opencode|pi|pi-signed|grok|kimi|cursor|muse|agy) return 0 ;; esac return 1 } @@ -87,13 +88,14 @@ fm_control_harness_family() { # kimi*) printf 'kimi' ;; cursor*) printf 'cursor' ;; muse*) printf 'muse' ;; + agy*) printf 'agy' ;; *) return 1 ;; esac } -# Which task kinds an adapter is verified to run. muse is a crewmate/scout -# adapter only: it has no primary supervision protocol, and bin/fm-spawn.sh -# refuses a --secondmate launch on it. The control plane +# Which task kinds an adapter is verified to run. muse and agy are crewmate/scout +# adapters only: neither has a primary supervision protocol, and bin/fm-spawn.sh +# refuses a --secondmate launch on either. The control plane # asks this BEFORE it stops anything, so an incompatible relaunch target is # refused while the current agent is still running rather than after it has # been stopped. @@ -101,7 +103,7 @@ fm_control_harness_supports_kind() { # local harness=${1-} kind=${2-} fm_control_harness_supported "$harness" || return 1 case "$harness" in - muse) [ "$kind" != secondmate ] || return 1 ;; + muse|agy) [ "$kind" != secondmate ] || return 1 ;; esac return 0 } @@ -110,7 +112,7 @@ fm_control_harness_supports_kind() { # # whose Esc only moves focus to the scrollback; grok cancels on Ctrl+C. fm_control_interrupt_key() { # case "${1-}" in - claude|codex|opencode|pi|pi-signed|kimi|cursor|muse) printf 'Escape' ;; + claude|codex|opencode|pi|pi-signed|kimi|cursor|muse|agy) printf 'Escape' ;; grok) printf 'C-c' ;; *) return 1 ;; esac @@ -121,7 +123,7 @@ fm_control_interrupt_key() { # fm_control_interrupt_repeat() { # case "${1-}" in opencode) printf '2' ;; - claude|codex|pi|pi-signed|grok|kimi|cursor|muse) printf '1' ;; + claude|codex|pi|pi-signed|grok|kimi|cursor|muse|agy) printf '1' ;; *) return 1 ;; esac } @@ -139,7 +141,7 @@ fm_control_interrupt_repeat() { # fm_control_interrupt_clear_key() { # case "${1-}" in muse) printf 'C-u' ;; - claude|codex|opencode|pi|pi-signed|grok|kimi|cursor) ;; + claude|codex|opencode|pi|pi-signed|grok|kimi|cursor|agy) ;; *) return 1 ;; esac } @@ -151,7 +153,7 @@ fm_control_interrupt_ack_source() { # # after an interrupt was measured as variable - sometimes seconds, sometimes # not within 20 - so a cancellation claim built on it would be unreliable. # Normal turn completion is prompt, which is what the busy fold depends on. - claude|codex|opencode|pi|pi-signed|grok|kimi|cursor) printf 'none' ;; + claude|codex|opencode|pi|pi-signed|grok|kimi|cursor|agy) printf 'none' ;; *) return 1 ;; esac } @@ -159,7 +161,7 @@ fm_control_interrupt_ack_source() { # # The command that exits the agent from its own composer. fm_control_exit_command() { # case "${1-}" in - claude|opencode|grok|kimi|cursor|muse) printf '/exit' ;; + claude|opencode|grok|kimi|cursor|muse|agy) printf '/exit' ;; codex|pi|pi-signed) printf '/quit' ;; *) return 1 ;; esac @@ -224,6 +226,18 @@ fm_control_harness_wiring_paths() { # printf '%s\n' "$state/$id.muse-session-current" ;; cursor) printf '%s\n' "$state/$id.cursor-session" ;; + agy) + printf '%s\n' "$wt/.agents/plugins/fm-firstmate-busy-$id/plugin.json" + printf '%s\n' "$wt/.agents/plugins/fm-firstmate-busy-$id/hooks.json" + ;; + esac +} + +fm_control_harness_wiring_dirs() { # + local harness=${1-} wt=${2-} state=${3-} id=${4-} + [ -n "$wt" ] && [ -n "$state" ] && [ -n "$id" ] || return 1 + case "$harness" in + agy) printf '%s\n' "$wt/.agents/plugins/fm-firstmate-busy-$id" ;; esac } diff --git a/bin/fm-control.sh b/bin/fm-control.sh index 251cc679912..22cbc828b0c 100755 --- a/bin/fm-control.sh +++ b/bin/fm-control.sh @@ -177,7 +177,7 @@ shift 2 if ! fm_control_verb_allowed "$VERB"; then { if [ "$VERB" = resume ]; then - echo "error: 'resume' is not a control verb: resuming an exited agent is not deterministic across the verified adapters (codex and grok need a session id printed at exit, opencode continues the most recent session for the cwd, and claude, pi, pi-signed, and kimi have no verified pane-resume contract). Use 'relaunch', which carries the brief plus a progress note into a fresh agent on any adapter." + echo "error: 'resume' is not a control verb: resuming an exited agent is not deterministic across the verified adapters (codex and grok need a session id printed at exit, opencode continues the most recent session for the cwd, agy exposes id-based and continue modes without a verified exact-selection contract, and claude, pi, pi-signed, and kimi have no verified pane-resume contract). Use 'relaunch', which carries the brief plus a progress note into a fresh agent on any adapter." else echo "error: '$VERB' is not a control verb" fi diff --git a/bin/fm-session-lock-lib.sh b/bin/fm-session-lock-lib.sh index 85af8626052..9ceaf0ce6f6 100644 --- a/bin/fm-session-lock-lib.sh +++ b/bin/fm-session-lock-lib.sh @@ -23,7 +23,7 @@ FM_HARNESS_RE='claude|codex|opencode|grok|kimi|agy|^pi$|^pi-signed$' # FM_HARNESS_RE. Used only for the stricter path evidence below, where the # loose regex would also match ordinary firstmate paths such as # bin/fm-claude-stop-autoarm.sh. -FM_HARNESS_NAMES=(claude codex opencode grok kimi pi-signed pi) +FM_HARNESS_NAMES=(claude codex opencode grok kimi agy pi-signed pi) # Print the exact harness name carried by executable path $1 - its own basename # or any directory component - or return 1. diff --git a/bin/fm-spawn.sh b/bin/fm-spawn.sh index 594f16c894a..63b61cffd6c 100755 --- a/bin/fm-spawn.sh +++ b/bin/fm-spawn.sh @@ -831,6 +831,12 @@ clear_relaunch_harness_wiring() { rm -f -- "$path" || return 1 done </dev/null || [ ! -e "$path" ] || return 1 + done <&2 exit 1 fi +if [ "$KIND" = secondmate ] && [ "$HARNESS" = agy ]; then + echo "error: agy is a verified crewmate/scout adapter only and cannot run a secondmate; it has no primary supervision protocol. Select a harness verified for secondmates." >&2 + exit 1 +fi case "$HARNESS" in pi|pi-signed) @@ -1304,9 +1314,21 @@ if [ "$KIND" = secondmate ] && [ -z "$ARG3" ]; then fi if [ "$HARNESS" = agy ]; then - if [ "$MODEL_SET" -eq 0 ] || [ -z "$MODEL" ] || [ "$MODEL" = default ]; then - MODEL="gemini-3.7-flash-high" + if [ -z "$MODEL" ] || [ "$MODEL" = default ]; then + case "$EFFORT" in + low) MODEL=gemini-3.7-flash-low ;; + medium) MODEL=gemini-3.7-flash-medium ;; + *) MODEL=gemini-3.7-flash-high ;; + esac fi + case "$MODEL:$EFFORT" in + *-low:medium|*-low:high|*-low:xhigh|*-low:max|\ + *-medium:low|*-medium:high|*-medium:xhigh|*-medium:max|\ + *-high:low|*-high:medium) + echo "error: agy model '$MODEL' conflicts with requested effort '$EFFORT'; select the matching low, medium, or high model variant" >&2 + exit 1 + ;; + esac fi secondmate_registry_value() { @@ -1356,6 +1378,25 @@ resolve_muse_binary() { return 1 } +resolve_agy_binary() { + local candidate dir + candidate=$(command -v agy 2>/dev/null || true) + if [ -n "$candidate" ] && [ -x "$candidate" ]; then + case "$candidate" in + /*) printf '%s\n' "$candidate"; return 0 ;; + *) + dir=$(cd "$(dirname "$candidate")" 2>/dev/null && pwd -P) || dir= + if [ -n "$dir" ]; then + printf '%s/%s\n' "$dir" "$(basename "$candidate")" + return 0 + fi + ;; + esac + fi + echo "error: agy executable not found on PATH; install Antigravity CLI or select a different verified harness" >&2 + return 1 +} + # muse_credential_present: 0 when a launched muse pane can reach its provider # without an interactive login. muse offers exactly two credential paths # (verified, muse 0.1.0-R708.1): the META_API_KEY environment variable, which @@ -1503,6 +1544,13 @@ case "$LAUNCH" in ;; esac +case "$LAUNCH" in + *__AGYBIN__*) + AGY_BIN=$(resolve_agy_binary) || exit 1 + LAUNCH=${LAUNCH//__AGYBIN__/$(shell_quote "$AGY_BIN")} + ;; +esac + json_escape() { printf '%s' "$1" | sed 's/\\/\\\\/g; s/"/\\"/g' } @@ -2365,7 +2413,7 @@ if [ "$KIND" != secondmate ]; then ;; esac case "$HARNESS" in - claude*|opencode*|pi|pi-signed) + claude*|opencode*|pi|pi-signed|agy*) BUSY_GEN=$("$FM_ROOT/bin/fm-busy-event.sh" arm "$STATE_REAL" "$ID") || { echo "error: failed to arm the busy-state contract for $ID" >&2 exit 1 @@ -2492,6 +2540,22 @@ export default function (pi: any) { } EOF ;; + agy*) + AGY_PLUGIN_DIR="$WT/.agents/plugins/fm-firstmate-busy-$ID" + mkdir -p "$AGY_PLUGIN_DIR" + busy_cmd_prefix="$(shell_quote "$FM_ROOT/bin/fm-busy-event.sh") apply $(shell_quote "$STATE_REAL") $(shell_quote "$ID")" + busy_suffix="--gen $(shell_quote "$BUSY_GEN") --source agy-hook" + j_submit=$(json_escape "$busy_cmd_prefix busy $busy_suffix --event pre-invocation >/dev/null 2>&1 || true; printf '%s\\n' '{}'") + j_stop=$(json_escape "touch $(shell_quote "$TURNEND"); $busy_cmd_prefix idle $busy_suffix --event stop >/dev/null 2>&1 || true; printf '%s\\n' '{\"decision\":\"allow\"}'") + cat > "$AGY_PLUGIN_DIR/plugin.json" < "$AGY_PLUGIN_DIR/hooks.json" </dev/null || return 1 rm -f "$child_wt/.claude/settings.local.json" "$child_wt/.opencode/plugins/fm-turn-end.js" \ - "$child_wt/.fm-grok-turnend" "$child_wt/.fm-kimi-turnend" + "$child_wt/.fm-grok-turnend" "$child_wt/.fm-kimi-turnend" \ + "$child_wt/.agents/plugins/fm-firstmate-busy-$child_id/plugin.json" \ + "$child_wt/.agents/plugins/fm-firstmate-busy-$child_id/hooks.json" fi fm_backend_remove_worktree "$child_backend" "$child_orca_worktree_id" || return 1 elif [ -n "$child_wt" ] && [ -d "$child_wt" ]; then validate_child_worktree_for_removal "$child_wt" "$child_proj" >/dev/null || return 1 rm -f "$child_wt/.claude/settings.local.json" "$child_wt/.opencode/plugins/fm-turn-end.js" \ "$child_wt/.opencode/plugins/fm-busy-state.js" \ - "$child_wt/.fm-grok-turnend" "$child_wt/.fm-kimi-turnend" + "$child_wt/.fm-grok-turnend" "$child_wt/.fm-kimi-turnend" \ + "$child_wt/.agents/plugins/fm-firstmate-busy-$child_id/plugin.json" \ + "$child_wt/.agents/plugins/fm-firstmate-busy-$child_id/hooks.json" if [ -n "$child_proj" ] && [ -d "$child_proj" ] && command -v treehouse >/dev/null 2>&1; then if teardown_treehouse_return "$child_wt" "$child_proj" "child worktree"; then : @@ -2685,7 +2689,9 @@ if [ "$BACKEND" = orca ] && [ "$KIND" != secondmate ]; then fi rm -f "$WT/.claude/settings.local.json" "$WT/.opencode/plugins/fm-turn-end.js" \ "$WT/.opencode/plugins/fm-busy-state.js" \ - "$WT/.fm-grok-turnend" "$WT/.fm-kimi-turnend" + "$WT/.fm-grok-turnend" "$WT/.fm-kimi-turnend" \ + "$WT/.agents/plugins/fm-firstmate-busy-$ID/plugin.json" \ + "$WT/.agents/plugins/fm-firstmate-busy-$ID/hooks.json" fi [ -z "$T_ORCA" ] || fm_backend_kill "$BACKEND" "$T" "$(meta_value "$META" zellij_tab_id)" "fm-$ID" 2>/dev/null || true fm_backend_remove_worktree "$BACKEND" "$ORCA_WORKTREE_ID" @@ -2698,7 +2704,9 @@ elif [ -d "$WT" ] && [ "$KIND" != secondmate ]; then fi # Remove our hook file so a reused pool worktree cannot fire signals for a dead task. rm -f "$WT/.claude/settings.local.json" "$WT/.opencode/plugins/fm-turn-end.js" \ - "$WT/.fm-grok-turnend" "$WT/.fm-kimi-turnend" + "$WT/.fm-grok-turnend" "$WT/.fm-kimi-turnend" \ + "$WT/.agents/plugins/fm-firstmate-busy-$ID/plugin.json" \ + "$WT/.agents/plugins/fm-firstmate-busy-$ID/hooks.json" # Kills remaining processes in the worktree (including the agent), resets, returns # to pool. treehouse resolves the pool from the working directory, so run it from # the project. teardown_treehouse_return tolerates transient and stale git locks diff --git a/docs/agent-control.md b/docs/agent-control.md index af50ab75058..8194b20fa1c 100644 --- a/docs/agent-control.md +++ b/docs/agent-control.md @@ -48,7 +48,7 @@ The clear is refused before anything is sent when the recorded backend cannot de Removing a worktree, closing an endpoint, or discarding work stays with [`bin/fm-teardown.sh`](../bin/fm-teardown.sh), which owns the landed-work test. **`resume` is not a verb.** -It is not deterministic across the verified adapters: codex and grok resume only from a session id printed at exit, opencode continues the most recent session for the cwd, and claude, pi, pi-signed, and kimi have no verified pane-resume contract. +It is not deterministic across the verified adapters: codex and grok resume only from a session id printed at exit, opencode continues the most recent session for the cwd, agy exposes id-based and continue modes without a verified exact-selection contract, and claude, pi, pi-signed, and kimi have no verified pane-resume contract. `relaunch` covers the same need on every adapter, because the brief on disk - not a harness-private session - is the durable instruction. ## Transactional relaunch @@ -91,7 +91,7 @@ Switching harness is therefore one ordinary relaunch rather than a separate mech - An unverified harness is refused rather than guessed at. - An implicit relaunch from a prefixed raw-command basename is refused before the agent or durable state is touched because its original launch command cannot be reconstructed. - An adapter that is not verified for this task's kind is refused **before** the running agent is stopped, not after. - Muse is a crewmate and scout adapter only, so relaunching a secondmate onto it refuses while its agent is still up rather than leaving that secondmate with no agent when the launch owner refuses. + Muse and agy are crewmate and scout adapters only, so relaunching a secondmate onto either refuses while its agent is still up rather than leaving that secondmate with no agent when the launch owner refuses. - A backend that cannot deliver the harness's interrupt key, or the composer clear that key needs, is refused rather than sent a different key. Orca's terminal API exposes only an interrupt and an Enter, so it can deliver neither Escape nor Ctrl+U. - `exit` and `relaunch` require a backend with a recovery-grade agent-state classifier - tmux and herdr - because without one the "the agent stopped" postcondition cannot be proven. diff --git a/docs/architecture.md b/docs/architecture.md index a25bb20430f..0f27cb191c4 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -129,7 +129,7 @@ Text for a worker to read and commands that drive a worker's process are separat `bin/fm-busy-lib.sh` is the single owner of what "this worker is busy" means, and `bin/fm-busy-event.sh` is the only writer of the per-task records it reads. Every classification returns a verdict of busy, idle, unknown, or dead together with the source that produced it, so a consumer or a diagnostic can never confuse semantic state with a fallback. -Each converted adapter reports its own turn lifecycle through a machine-readable contract the vendor already exposes, rather than through rendered footer text: Pi and pi-signed through the Firstmate-owned extension's `agent_start` and `agent_settled` confirmed by `ctx.isIdle()`, OpenCode through its plugin's semantic `session.status`, Claude through owned `UserPromptSubmit`, `Stop`, `StopFailure`, and `SessionEnd` hooks, Muse through its session log, and Cursor through its conversation transcript. +Each converted adapter reports its own turn lifecycle through a machine-readable contract the vendor already exposes, rather than through rendered footer text: Pi and pi-signed through the Firstmate-owned extension's `agent_start` and `agent_settled` confirmed by `ctx.isIdle()`, OpenCode through its plugin's semantic `session.status`, Claude through owned `UserPromptSubmit`, `Stop`, `StopFailure`, and `SessionEnd` hooks, Antigravity CLI through plugin `PreInvocation` and `Stop` hooks, Muse through its session log, and Cursor through its conversation transcript. Kimi behind Pi inherits Pi's lifecycle. Codex and standalone Kimi classify unknown behind explicit probes until a semantic source is live-verified for them, and Grok keeps one clearly isolated rendered-tail fallback that can only ever classify a Grok task. @@ -207,7 +207,7 @@ The session-start bootstrap step keeps valid dispatch configuration silent unles When the file exists, `fm-spawn.sh` refuses crewmate and scout launches without an explicit harness, so `config/crew-harness` is only automatic when no dispatch profile file is active. Secondmate launches are exempt because they resolve the secondmate harness and any optional secondmate model or effort tokens instead. Unsupported effort values are still recorded in task meta when passed to `fm-spawn.sh`, but the launch template omits any effort flag that the selected harness does not accept. -That keeps spawn launch compatible across claude, codex, opencode, pi, pi-signed, grok, kimi, cursor, and muse while preserving the requested profile for later audit. +That keeps spawn launch compatible across claude, codex, opencode, pi, pi-signed, grok, kimi, cursor, muse, and agy while preserving the requested profile for later audit. ## Optional secondmates diff --git a/docs/configuration.md b/docs/configuration.md index df27bcbda26..00fa2f9b2a9 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -239,6 +239,8 @@ Cursor typed-submit confirmation is verified on tmux and Herdr only. On Zellij, cmux, and Orca a typed-plane Cursor send (a harness-native invocation or an explicit backend target; ordinary text steers ride the durable inbox and exit 0 at enqueue) lands, but `fm-send` reports delivery unconfirmed and exits non-zero because their shared submit core does not consult the busy footer; [runtime backend verification](verification/runtime-backends.md#cursor-agent-cli) owns the evidence and transcript-state boundary. muse is verified for crewmate and scout launches ONLY, and `fm-spawn.sh` refuses it for a secondmate, because muse ships no usable hook surface for a primary session's turn-end supervision; [`docs/verification/muse.md`](verification/muse.md) owns that evidence. muse also needs a worker-reachable credential before spawning, and the portable fleet path is the `/muse/auth.json` credential stored by `muse login`, because a caller-only `META_API_KEY` does not cross a long-lived backend daemon. +agy is verified for crewmate and scout launches ONLY, and `fm-spawn.sh` refuses it for a secondmate because no primary supervision protocol has been verified for it. +agy defaults to `gemini-3.7-flash-high`; an explicit low or medium effort without a model selects the matching Flash variant, and a model variant that conflicts with an explicit effort is refused. New harnesses get verified through a supervised trial task before joining the set. The verified adapter evidence - each harness's busy-state source, interrupt and exit behavior, skill-invocation syntax, and per-harness quirks - lives in [`.agents/skills/harness-adapters/SKILL.md`](../.agents/skills/harness-adapters/SKILL.md). The executable interrupt and exit mechanics live in [`bin/fm-control-lib.sh`](../bin/fm-control-lib.sh), and [`docs/agent-control.md`](agent-control.md) owns their lifecycle-control architecture. @@ -271,6 +273,7 @@ Kimi continues to use the captain's normal Kimi home, including the existing con The Kimi installer requires an existing regular non-symlink `~/.kimi-code/config.toml`, `python3` with `tomllib`, and `jq`; it validates but never serializes the captain's TOML and refuses before writing when the config is missing, malformed, or surprising or when either tool requirement is unavailable. Its `remove` action excises only the marker-delimited Firstmate region and removes Firstmate's hook files. For Pi and pi-signed secondmate launches, `fm-spawn.sh` starts the selected executable with `-e` pointed at the secondmate home's own tracked `.pi/extensions/fm-primary-pi-watch.ts` and `.pi/extensions/fm-primary-turnend-guard.ts`, both already present from the secondmate home's git worktree. +For agy crews, `fm-spawn.sh` writes an isolated per-task plugin under `.agents/plugins/` whose lifecycle hooks update semantic busy state without replacing an existing project `.agents/hooks.json`. ## Crew dispatch profiles (config/crew-dispatch.json) diff --git a/docs/trace-context.md b/docs/trace-context.md index 83e1019a8d7..15c75e5521d 100644 --- a/docs/trace-context.md +++ b/docs/trace-context.md @@ -23,7 +23,7 @@ When enabled, for each spawn Firstmate resolves one W3C `traceparent` carrier fo This feature parents no SDK span by itself. Because the injected carrier and the recorded carrier are the same string, an observer that reads the metadata reconstructs exactly the identity the child received. -The injection sits at the unconditional pre-launch export site, so it covers ship and scout spawns across `claude`, `codex`, `opencode`, `pi`, `pi-signed`, `grok`, `kimi`, `cursor`, and `muse`, plus Secondmate spawns across that same set except the deliberately crewmate-only `muse` adapter. +The injection sits at the unconditional pre-launch export site, so it covers ship and scout spawns across `claude`, `codex`, `opencode`, `pi`, `pi-signed`, `grok`, `kimi`, `cursor`, `muse`, and `agy`, plus Secondmate spawns across that same set except the deliberately crewmate-only `muse` and `agy` adapters. This is the same coverage `GOTMPDIR` already has and requires no trace-specific `launch_template()` behavior. Ship and scout spawns reach that site on every spawn backend (`tmux`, `herdr`, `zellij`, `orca`, `cmux`); a Secondmate reaches it on every backend that accepts a Secondmate spawn (`tmux`, `herdr`, `zellij`), because `bin/fm-spawn.sh` rejects a Secondmate on `orca` and `cmux`. diff --git a/docs/verification/supervision.md b/docs/verification/supervision.md index 8ae889f30fe..ed42ed9a25e 100644 --- a/docs/verification/supervision.md +++ b/docs/verification/supervision.md @@ -181,6 +181,7 @@ Each pass polled `state/.busy-state` while a real turn ran. | Pi | 0.82.0 | Extension `agent_start` / `agent_settled` with `ctx.isIdle()` | The spawn seed `busy source=fm-spawn`, then `busy source=pi-ext event=agent-start`, then `idle source=pi-ext event=agent-settled`; the turn-end marker was still touched. | | OpenCode | 1.17.18 | Plugin `session.status` | In a real TUI pane: seed, then `busy source=opencode-plugin event=session-busy`, then `idle source=opencode-plugin event=session-status-idle`. | | Claude | 2.1.220 (Claude Code) | Hooks `UserPromptSubmit`, `Stop`, `StopFailure`, `SessionEnd` | `UserPromptSubmit` fired for the argv launch prompt and each steer, and `Stop` closed every completed turn. A mid-stream Escape interrupt fired no closing hook, which is why the firstmate-controlled clear exists. `StopFailure` and `SessionEnd` are wired from the four hook names present in the installed binary; only the abnormal paths they cover were not reproduced live. | +| Antigravity CLI | 1.1.19 | Plugin hooks `PreInvocation` / `Stop` | `PreInvocation` opened the launch and steered turns, and `Stop` closed the execution loop and touched the turn-end notification. The delivery acknowledgement separately uses only the live `esc to cancel` footer. | | Codex | codex-cli 0.145.0 | None usable | See below; classifies `unknown codex-unverified`. | | Kimi (standalone) | not installed | None usable | No binary on `PATH`, so the gate stays closed and it classifies `unknown kimi-unverified`. | | Grok | 0.2.112 | Isolated rendered-tail fallback | Retained unconverted; the approved audit could not credit a live structured-lifecycle run. | diff --git a/tests/fm-agy-harness.test.sh b/tests/fm-agy-harness.test.sh index 36b00e5f17b..93733520ace 100755 --- a/tests/fm-agy-harness.test.sh +++ b/tests/fm-agy-harness.test.sh @@ -5,6 +5,9 @@ set -u # shellcheck source=tests/lib.sh . "$(dirname "${BASH_SOURCE[0]}")/lib.sh" +# shellcheck source=/dev/null +. "$ROOT/bin/fm-busy-lib.sh" + SPAWN="$ROOT/bin/fm-spawn.sh" HARNESS_SH="$ROOT/bin/fm-harness.sh" BOOTSTRAP_SH="$ROOT/bin/fm-bootstrap.sh" @@ -24,7 +27,11 @@ esac case "${1:-}" in display-message) printf "firstmate\n"; exit 0 ;; list-windows) exit 0 ;; - has-session|new-session|new-window|kill-window) exit 0 ;; + has-session|kill-window) exit 0 ;; + new-session|new-window) + [ -z "${FM_FAKE_ENDPOINT_LOG:-}" ] || printf '%s\n' "$*" >> "$FM_FAKE_ENDPOINT_LOG" + exit 0 + ;; send-keys) if [ -n "${FM_FAKE_LAUNCH_LOG:-}" ]; then prev= @@ -62,14 +69,16 @@ make_spawn_case() { } run_agy_spawn() { - local home=$1 proj=$2 wt=$3 fakebin=$4 launchlog=$5 id=$6 + local home=$1 proj=$2 wt=$3 fakebin=$4 launchlog=$5 id=$6 launch_path shift 6 : > "$launchlog" + : > "$launchlog.endpoints" + launch_path=${FM_AGY_TEST_PATH:-$fakebin:$PATH} FM_ROOT_OVERRIDE="" FM_HOME="$home" \ FM_STATE_OVERRIDE="$home/state" FM_DATA_OVERRIDE="$home/data" \ FM_PROJECTS_OVERRIDE="$home/projects" FM_CONFIG_OVERRIDE="$home/config" \ FM_SPAWN_NO_GUARD=1 FM_FAKE_PANE_PATH="$wt" TMUX="fake,1,0" \ - FM_FAKE_LAUNCH_LOG="$launchlog" PATH="$fakebin:$PATH" \ + FM_FAKE_LAUNCH_LOG="$launchlog" FM_FAKE_ENDPOINT_LOG="$launchlog.endpoints" PATH="$launch_path" \ "$SPAWN" "$id" "$proj" agy --mode no-mistakes --yolo off "$@" 2>&1 } @@ -85,6 +94,7 @@ test_agy_harness_detection() { . "$ROOT/bin/fm-session-lock-lib.sh" printf "agy\n" | grep -qE "$FM_HARNESS_RE" || fail "FM_HARNESS_RE did not match agy" printf "foo-agy-bar\n" | grep -qE "$FM_HARNESS_RE" || fail "FM_HARNESS_RE did not match *agy*" + [ "$(fm_harness_path_name /opt/homebrew/bin/agy)" = agy ] || fail "session-lock exact path table did not identify agy" pass "fm-harness and session-lock: detects agy markers and process identity" } @@ -98,8 +108,12 @@ EOF assert_contains "$out" "spawned $id harness=agy" "agy spawn did not report success: $out" launched=$(cat "$launchlog") - assert_contains "$launched" "agy --dangerously-skip-permissions --model 'gemini-3.7-flash-high' --prompt-interactive" \ + assert_contains "$launched" "'$fakebin/agy' --dangerously-skip-permissions --model 'gemini-3.7-flash-high' --prompt-interactive" \ "agy launch command did not match expected template with default model: $launched" + assert_contains "$launched" "env -u CURSOR_AGENT -u CURSOR_INVOKED_AS" \ + "agy launch did not clear inherited Cursor markers: $launched" + assert_contains "$launched" "env -u CLAUDECODE -u PI_CODING_AGENT -u GROK_AGENT -u FM_PI_HARNESS" \ + "agy launch did not clear inherited Claude, Pi, and Grok markers: $launched" meta_file="$home/state/$id.meta" assert_contains "$(cat "$meta_file")" "model=gemini-3.7-flash-high" "meta file did not record default model: $(cat "$meta_file")" @@ -117,7 +131,7 @@ EOF out=$(run_agy_spawn "$home" "$proj" "$wt" "$fakebin" "$launchlog" "$id" --model gemini-3.7-flash-high --effort high) launched=$(cat "$launchlog") assert_contains "$launched" "--model 'gemini-3.7-flash-high'" "launch did not include model: $launched" - assert_no_grep "\-\effort" "$launchlog" "launch command emitted redundant/conflicting --effort for variant model ID" + assert_not_contains "$launched" "--effort" "launch command emitted redundant/conflicting --effort for variant model ID" # 2. Base model ID with effort: emits --effort rec=$(make_spawn_case base-effort) @@ -144,21 +158,119 @@ EOF out=$(run_agy_spawn "$home" "$proj" "$wt" "$fakebin" "$launchlog" "$id" --model gemini-3.7-flash --effort max) launched=$(cat "$launchlog") assert_contains "$launched" "--model 'gemini-3.7-flash' --effort 'high'" "launch command did not cap max to high: $launched" + + rec=$(make_spawn_case implicit-low) + IFS="|" read -r case_dir home proj wt fakebin launchlog id </dev/null || fail "agy lifecycle plugin is not valid JSON" + [ "$(fm_busy_classify tmux fake:w agy "$id" "$state")" = "busy fm-spawn" ] \ + || fail "agy launch turn was not seeded busy" + rm -f "$state/$id.turn-ended" + out=$(run_agy_hook "$hooks" Stop) || fail "agy Stop hook failed: $out" + printf '%s' "$out" | jq -e '.decision == "allow"' >/dev/null \ + || fail "agy Stop hook did not return its required JSON contract: $out" + [ -f "$state/$id.turn-ended" ] || fail "agy Stop hook did not touch the turn-end notification" + [ "$(fm_busy_classify tmux fake:w agy "$id" "$state")" = "idle agy-hook" ] \ + || fail "agy Stop hook did not settle semantic state" + out=$(run_agy_hook "$hooks" PreInvocation) || fail "agy PreInvocation hook failed: $out" + printf '%s' "$out" | jq -e 'type == "object"' >/dev/null \ + || fail "agy PreInvocation hook did not return JSON: $out" + [ "$(fm_busy_classify tmux fake:w agy "$id" "$state")" = "busy agy-hook" ] \ + || fail "agy PreInvocation hook did not open semantic state" + pass "fm-spawn and fm-busy-lib: agy lifecycle hooks drive semantic busy state" +} + +test_agy_missing_binary_refuses_before_endpoint_creation() { + local rec case_dir home proj wt fakebin launchlog id out + rec=$(make_spawn_case missing-binary) + IFS="|" read -r case_dir home proj wt fakebin launchlog id < "$home/data/$id/brief.md" + out=$(FM_ROOT_OVERRIDE='' FM_HOME="$home" \ + FM_STATE_OVERRIDE="$home/state" FM_DATA_OVERRIDE="$home/data" \ + FM_PROJECTS_OVERRIDE="$home/projects" FM_CONFIG_OVERRIDE="$home/config" \ + FM_SPAWN_NO_GUARD=1 TMUX='fake,1,0' PATH="$fakebin:$PATH" \ + "$SPAWN" "$id" agy --secondmate 2>&1) + expect_code 1 $? "agy should be refused for secondmate work" + assert_contains "$out" "crewmate/scout adapter only" "agy secondmate refusal did not explain the capability boundary: $out" + pass "fm-spawn: agy is restricted to crewmate and scout work" +} + test_agy_busy_matching_and_liveness() { # shellcheck source=bin/fm-tmux-lib.sh . "$TMUX_LIB" # Busy samples - printf "● Bash(ls -la)\n⣾ Loading...\nesc to cancel\n" | fm_busy_lines_match agy || fail "agy busy lines did not match" - printf "Thought for 4s\n⣽ Thinking...\nesc to cancel\n" | fm_busy_lines_match agy || fail "agy thinking lines did not match" + printf "● Bash(ls -la)\n⣾ Loading...\nesc to cancel\n" | fm_busy_lines_match agy || fail "agy busy footer did not match" # Idle samples if printf "? for shortcuts Gemini 3.7 Flash · high\n" | fm_busy_lines_match agy; then fail "agy idle footer falsely matched as busy" fi + if printf "Completed output mentions ⣾ Loading... as ordinary transcript text\n" | fm_busy_lines_match agy; then + fail "agy transcript text falsely matched as a live delivery-busy footer" + fi # shellcheck source=bin/backends/tmux.sh . "$BACKEND_SH" @@ -190,6 +302,10 @@ test_agy_crew_dispatch_validation() { out=$(FM_HOME="$home" "$BOOTSTRAP_SH" 2>&1) assert_not_contains "$out" "CREW_DISPATCH: invalid" "crew-dispatch was rejected" + printf "%s\n" '{"default":{"harness":"agy","model":"gemini-3.7-flash","effort":"xhigh"}}' > "$config/crew-dispatch.json" + out=$(FM_HOME="$home" "$BOOTSTRAP_SH" 2>&1) + assert_not_contains "$out" "CREW_DISPATCH: invalid" "crew-dispatch rejected an effort that agy caps to high" + # Invalid effort for agy printf "%s\n" '{"default":{"harness":"agy","effort":"bad-effort"}}' > "$config/crew-dispatch.json" out=$(FM_HOME="$home" "$BOOTSTRAP_SH" 2>&1) @@ -201,5 +317,8 @@ test_agy_crew_dispatch_validation() { test_agy_harness_detection test_agy_default_model_and_launch_template test_agy_effort_flag_handling +test_agy_semantic_busy_lifecycle +test_agy_missing_binary_refuses_before_endpoint_creation +test_agy_refuses_secondmate test_agy_busy_matching_and_liveness test_agy_crew_dispatch_validation diff --git a/tests/fm-control-relaunch.test.sh b/tests/fm-control-relaunch.test.sh index 9a7b4285bab..1547d5172b4 100755 --- a/tests/fm-control-relaunch.test.sh +++ b/tests/fm-control-relaunch.test.sh @@ -430,6 +430,22 @@ test_harness_switch_moves_the_record_and_clears_prior_wiring() { pass "fm-control relaunch: switching harness is one ordinary relaunch, and the old wiring goes with the old agent" } +test_agy_harness_switch_removes_the_plugin_directory() { + local dir plugin out rc + dir=$(new_case agy-switch rl36) + add_ship_task "$dir" rl36 agy + printf 'agy' > "$dir/fake/command" + plugin="$dir/wt/.agents/plugins/fm-firstmate-busy-rl36" + mkdir -p "$plugin" + printf '%s\n' '{"name":"fm-firstmate-busy-rl36"}' > "$plugin/plugin.json" + printf '%s\n' '{"fm-firstmate-busy":{}}' > "$plugin/hooks.json" + printf 'claude' > "$dir/fake/becomes" + out=$(run_control "$dir" rl36 relaunch --harness claude --note "switching runtime"); rc=$? + expect_code 0 "$rc" "switching away from agy should succeed"$'\n'"$out" + [ ! -e "$plugin" ] || fail "switching away from agy left its plugin directory behind" + pass "fm-control relaunch: agy plugin wiring retires with its agent" +} + test_harness_switch_does_not_carry_the_old_profile_axes() { local dir out rc dir=$(new_case profile rl5) @@ -1319,6 +1335,7 @@ test_disabled_relaunch_clears_prior_trace_context test_relaunch_appends_the_progress_note_to_the_instructions test_relaunch_requires_a_note_for_a_ship_task test_harness_switch_moves_the_record_and_clears_prior_wiring +test_agy_harness_switch_removes_the_plugin_directory test_harness_switch_does_not_carry_the_old_profile_axes test_harness_switch_resolves_a_prefixed_recorded_harness test_prefixed_recorded_harness_requires_explicit_replacement diff --git a/tests/fm-control.test.sh b/tests/fm-control.test.sh index 2b2f73e3797..a2056d89e6a 100755 --- a/tests/fm-control.test.sh +++ b/tests/fm-control.test.sh @@ -35,7 +35,7 @@ mkdir -p "$TMP_ROOT" TMP_ROOT=$(cd "$TMP_ROOT" && pwd) trap 'rm -rf "$TMP_ROOT"' EXIT -VERIFIED_HARNESSES="claude codex opencode pi pi-signed grok kimi cursor muse" +VERIFIED_HARNESSES="claude codex opencode pi pi-signed grok kimi cursor muse agy" # The expectation table, written out independently of the implementation so a # silent change to either side shows up here. The fourth field is the composer @@ -52,6 +52,7 @@ verified_adapter_contract() { # -> exit command, interrupt key, repea kimi) printf '/exit\tEscape\t1\t\n' ;; cursor) printf '/exit\tEscape\t1\t\n' ;; muse) printf '/exit\tEscape\t1\tC-u\n' ;; + agy) printf '/exit\tEscape\t1\t\n' ;; *) return 1 ;; esac } @@ -267,7 +268,7 @@ test_harness_family_resolution() { for pair in claude:claude claude-latest:claude codex:codex codex-cli:codex \ opencode:opencode grok:grok grok-2:grok kimi:kimi cursor:cursor \ cursor-agent:cursor muse:muse muse-bin-0.1.0:muse pi:pi \ - pi-signed:pi-signed; do + pi-signed:pi-signed agy:agy agy-cli:agy; do recorded=${pair%%:*} want=${pair#*:} got=$(fm_control_harness_family "$recorded") \ @@ -375,7 +376,9 @@ test_harness_kind_capability() { done fm_control_harness_supports_kind muse secondmate \ && fail "muse has no primary supervision protocol and must not claim a secondmate" - for harness in claude codex opencode pi pi-signed grok kimi; do + fm_control_harness_supports_kind agy secondmate \ + && fail "agy has no primary supervision protocol and must not claim a secondmate" + for harness in claude codex opencode pi pi-signed grok kimi cursor; do fm_control_harness_supports_kind "$harness" secondmate \ || fail "$harness should be able to run a secondmate" done @@ -384,6 +387,19 @@ test_harness_kind_capability() { pass "fm-control-lib: adapter capability is per task kind, not per adapter alone" } +test_agy_wiring_contract() { + local paths dir + paths=$(fm_control_harness_wiring_paths agy /tmp/wt /tmp/state task-x1) + assert_contains "$paths" "/tmp/wt/.agents/plugins/fm-firstmate-busy-task-x1/plugin.json" \ + "agy wiring contract omitted its plugin manifest" + assert_contains "$paths" "/tmp/wt/.agents/plugins/fm-firstmate-busy-task-x1/hooks.json" \ + "agy wiring contract omitted its hooks" + dir=$(fm_control_harness_wiring_dirs agy /tmp/wt /tmp/state task-x1) + [ "$dir" = "/tmp/wt/.agents/plugins/fm-firstmate-busy-task-x1" ] \ + || fail "agy wiring contract did not own its plugin directory" + pass "fm-control-lib: agy lifecycle wiring has one cleanup owner" +} + test_orca_refuses_an_escape_harness_interrupt() { local dir out rc dir=$(new_case orca-escape) @@ -881,6 +897,7 @@ test_harness_family_resolution test_prefixed_recorded_harness_reaches_each_control_verb test_backend_key_capability_matrix test_harness_kind_capability +test_agy_wiring_contract test_orca_refuses_an_escape_harness_interrupt test_unverified_state_backends_refuse_stop_verbs test_state_verified_backends_are_exactly_tmux_and_herdr From cf3243511b097c63c8ef739551e10a35b7b1aa20 Mon Sep 17 00:00:00 2001 From: Bryan Travis Smith Date: Mon, 24 Aug 2026 10:10:53 -0700 Subject: [PATCH 03/28] no-mistakes(review): Harden agy lifecycle plugin installation --- .agents/skills/harness-adapters/SKILL.md | 2 +- bin/fm-spawn.sh | 74 ++++++++++++++++++++++-- tests/fm-agy-harness.test.sh | 71 +++++++++++++++++++++-- 3 files changed, 135 insertions(+), 12 deletions(-) diff --git a/.agents/skills/harness-adapters/SKILL.md b/.agents/skills/harness-adapters/SKILL.md index c9d98cefeda..2e521c48faa 100644 --- a/.agents/skills/harness-adapters/SKILL.md +++ b/.agents/skills/harness-adapters/SKILL.md @@ -549,7 +549,7 @@ It is not verified for secondmate or primary work. | Binary | Executable `agy` from `PATH`; verified on version 1.1.19. | | Launch | `agy --dangerously-skip-permissions __MODELFLAG____EFFORTFLAG__--prompt-interactive "$(__OPINPUT__ encode launch-brief < __BRIEF__)"`; positional prompts are rejected with exit code 2. | | Models | `gemini-3.7-flash-high` (default), `gemini-3.7-flash-medium`, `gemini-3.7-flash-low`, Pro models, Claude Sonnet/Opus models. Authoritative discovery: `agy models`. | -| Busy state | Firstmate's generated plugin uses `PreInvocation` to open the turn and `Stop` to close it. | +| Busy state | Firstmate's generated plugin uses `PreInvocation` to open the turn and closes it only when a `Stop` payload reports `fullyIdle: true`; false or malformed payloads leave the task busy. | | Delivery busy token | `esc to cancel`, the live footer present only while a turn is running. | | Exit command | `/exit` or `/quit` or `Ctrl+D` (exits cleanly with status 0). | | Interrupt | Single Escape or `Ctrl+C` cancels active turn and returns to composer. | diff --git a/bin/fm-spawn.sh b/bin/fm-spawn.sh index 63b61cffd6c..baa6816ed59 100755 --- a/bin/fm-spawn.sh +++ b/bin/fm-spawn.sh @@ -670,6 +670,8 @@ RELAUNCH_REPLACEMENT_BUSY_GEN= RELAUNCH_REPLACEMENT_HARNESS= RELAUNCH_REPLACEMENT_STATE= RELAUNCH_REPLACEMENT_WT= +AGY_PLUGIN_INSTALL_PENDING=0 +AGY_PLUGIN_DIR= CONFIG_INHERIT_LOCK= CONFIG_INHERIT_LOCK_HELD=0 @@ -692,6 +694,13 @@ parse_orca_worktree_result() { spawn_abort_cleanup() { local status=$? + if [ "$AGY_PLUGIN_INSTALL_PENDING" = 1 ]; then + AGY_PLUGIN_INSTALL_PENDING=0 + if [ -d "$AGY_PLUGIN_DIR" ] && [ ! -L "$AGY_PLUGIN_DIR" ]; then + rm -f -- "$AGY_PLUGIN_DIR/plugin.json" "$AGY_PLUGIN_DIR/hooks.json" 2>/dev/null || true + rmdir -- "$AGY_PLUGIN_DIR" 2>/dev/null || true + fi + fi if [ "$RELAUNCH_REPLACEMENT_PENDING" = 1 ] \ && [ "$SPAWN_META_PUBLISH_STARTED" = 1 ] \ && [ -n "$SPAWN_META_TMP" ] \ @@ -840,6 +849,45 @@ $(fm_control_harness_wiring_dirs "$harness" "$wt" "$state" "$id") EOF } +agy_plugin_prepare() { + local wt=$1 state=$2 id=$3 agents_dir plugins_dir plugin_dir path + plugin_dir=$(fm_control_harness_wiring_dirs agy "$wt" "$state" "$id") || return 1 + plugins_dir=${plugin_dir%/*} + agents_dir=${plugins_dir%/*} + for path in "$agents_dir" "$plugins_dir"; do + if [ -L "$path" ] || { [ -e "$path" ] && [ ! -d "$path" ]; }; then + echo "error: refusing agy spawn because plugin parent is not a real directory: $path" >&2 + return 1 + fi + done + if [ -L "$plugin_dir" ]; then + echo "error: refusing agy spawn because plugin path is a symlink: $plugin_dir" >&2 + return 1 + fi + if [ -e "$plugin_dir" ]; then + echo "error: refusing agy spawn because plugin path already exists: $plugin_dir" >&2 + return 1 + fi + for path in "$agents_dir" "$plugins_dir"; do + if [ ! -e "$path" ]; then + mkdir -- "$path" || { + echo "error: could not create agy plugin parent: $path" >&2 + return 1 + } + fi + if [ -L "$path" ] || [ ! -d "$path" ]; then + echo "error: refusing agy spawn because plugin parent is not a real directory: $path" >&2 + return 1 + fi + done + mkdir -- "$plugin_dir" || { + echo "error: could not create exclusive agy plugin directory: $plugin_dir" >&2 + return 1 + } + AGY_PLUGIN_DIR=$plugin_dir + AGY_PLUGIN_INSTALL_PENDING=1 +} + spawn_herdr_presentation_order_lock_release() { [ "$HERDR_PRESENTATION_ORDER_LOCK_HELD" = 1 ] || return 0 HERDR_PRESENTATION_ORDER_LOCK_HELD=0 @@ -1547,6 +1595,10 @@ esac case "$LAUNCH" in *__AGYBIN__*) AGY_BIN=$(resolve_agy_binary) || exit 1 + AGY_JQ_BIN=$(command -v jq) || { + echo "error: jq executable not found on PATH; agy lifecycle hooks require jq" >&2 + exit 1 + } LAUNCH=${LAUNCH//__AGYBIN__/$(shell_quote "$AGY_BIN")} ;; esac @@ -2395,6 +2447,9 @@ if [ "$RELAUNCH" -eq 1 ]; then RELAUNCH_REPLACEMENT_STATE=$STATE_REAL RELAUNCH_REPLACEMENT_WT=$WT fi +case "$HARNESS" in + agy*) agy_plugin_prepare "$WT" "$STATE_REAL" "$ID" || exit 1 ;; +esac if [ "$KIND" != secondmate ]; then # Arm the semantic busy-state contract (bin/fm-busy-lib.sh) for every # adapter with a verified semantic source. The launch brief sent below IS a @@ -2541,18 +2596,24 @@ export default function (pi: any) { EOF ;; agy*) - AGY_PLUGIN_DIR="$WT/.agents/plugins/fm-firstmate-busy-$ID" - mkdir -p "$AGY_PLUGIN_DIR" busy_cmd_prefix="$(shell_quote "$FM_ROOT/bin/fm-busy-event.sh") apply $(shell_quote "$STATE_REAL") $(shell_quote "$ID")" busy_suffix="--gen $(shell_quote "$BUSY_GEN") --source agy-hook" j_submit=$(json_escape "$busy_cmd_prefix busy $busy_suffix --event pre-invocation >/dev/null 2>&1 || true; printf '%s\\n' '{}'") - j_stop=$(json_escape "touch $(shell_quote "$TURNEND"); $busy_cmd_prefix idle $busy_suffix --event stop >/dev/null 2>&1 || true; printf '%s\\n' '{\"decision\":\"allow\"}'") - cat > "$AGY_PLUGIN_DIR/plugin.json" </dev/null 2>&1; then touch $(shell_quote "$TURNEND"); $busy_cmd_prefix idle $busy_suffix --event stop >/dev/null 2>&1 || true; fi; printf '%s\\n' '{\"decision\":\"allow\"}'") + if ! (set -o noclobber; cat > "$AGY_PLUGIN_DIR/plugin.json" < "$AGY_PLUGIN_DIR/hooks.json" <&2 + exit 1 + fi + if ! (set -o noclobber; cat > "$AGY_PLUGIN_DIR/hooks.json" <&2 + exit 1 + fi exclude_path ".agents/plugins/fm-firstmate-busy-$ID/plugin.json" exclude_path ".agents/plugins/fm-firstmate-busy-$ID/hooks.json" ;; @@ -2963,4 +3024,5 @@ fi SPAWN_DELIVERY= [ -z "$MODE" ] || SPAWN_DELIVERY=" mode=$MODE yolo=$YOLO" +AGY_PLUGIN_INSTALL_PENDING=0 echo "spawned $ID harness=$HARNESS kind=$KIND$SPAWN_DELIVERY window=$META_WINDOW worktree=$WT" diff --git a/tests/fm-agy-harness.test.sh b/tests/fm-agy-harness.test.sh index 93733520ace..9a18dc06cbe 100755 --- a/tests/fm-agy-harness.test.sh +++ b/tests/fm-agy-harness.test.sh @@ -53,14 +53,14 @@ SH } make_spawn_case() { - local name=$1 case_dir home proj wt fakebin launchlog id + local name=$1 explicit_id=${2:-} case_dir home proj wt fakebin launchlog id case_dir="$TMP_ROOT/$name" home="$case_dir/home" proj="$case_dir/project" wt="$case_dir/wt" launchlog="$case_dir/launch.log" fakebin=$(make_spawn_fakebin "$case_dir/fake") - id="agy-$name-x1" + id=${explicit_id:-"agy-$name-x1"} mkdir -p "$home/data/$id" "$home/projects" "$home/state" "$home/config" printf "brief for %s\n" "$id" > "$home/data/$id/brief.md" fm_git_worktree "$proj" "$wt" "fm/$id" @@ -188,10 +188,10 @@ EOF } run_agy_hook() { - local hooks=$1 event=$2 cmd + local hooks=$1 event=$2 payload=${3:-'{}'} cmd cmd=$(jq -r ".[\"fm-firstmate-busy\"][\"$event\"][0].command" "$hooks") [ -n "$cmd" ] && [ "$cmd" != null ] || fail "agy plugin lacks $event" - sh -c "$cmd" + printf '%s\n' "$payload" | sh -c "$cmd" } test_agy_semantic_busy_lifecycle() { @@ -209,9 +209,17 @@ EOF [ "$(fm_busy_classify tmux fake:w agy "$id" "$state")" = "busy fm-spawn" ] \ || fail "agy launch turn was not seeded busy" rm -f "$state/$id.turn-ended" - out=$(run_agy_hook "$hooks" Stop) || fail "agy Stop hook failed: $out" + out=$(run_agy_hook "$hooks" Stop '{"fullyIdle":false}') || fail "agy active Stop hook failed: $out" printf '%s' "$out" | jq -e '.decision == "allow"' >/dev/null \ || fail "agy Stop hook did not return its required JSON contract: $out" + assert_absent "$state/$id.turn-ended" "agy Stop hook published a turn end while background work remained active" + [ "$(fm_busy_classify tmux fake:w agy "$id" "$state")" = "busy fm-spawn" ] \ + || fail "agy Stop hook cleared semantic state while fullyIdle was false" + out=$(run_agy_hook "$hooks" Stop '{}') || fail "agy malformed Stop hook failed: $out" + assert_absent "$state/$id.turn-ended" "agy Stop hook published a turn end without fullyIdle true" + [ "$(fm_busy_classify tmux fake:w agy "$id" "$state")" = "busy fm-spawn" ] \ + || fail "agy Stop hook cleared semantic state without fullyIdle true" + out=$(run_agy_hook "$hooks" Stop '{"fullyIdle":true}') || fail "agy fully-idle Stop hook failed: $out" [ -f "$state/$id.turn-ended" ] || fail "agy Stop hook did not touch the turn-end notification" [ "$(fm_busy_classify tmux fake:w agy "$id" "$state")" = "idle agy-hook" ] \ || fail "agy Stop hook did not settle semantic state" @@ -223,6 +231,57 @@ EOF pass "fm-spawn and fm-busy-lib: agy lifecycle hooks drive semantic busy state" } +test_agy_manifest_name_accepts_dotted_task_id() { + local rec case_dir home proj wt fakebin launchlog id out manifest + rec=$(make_spawn_case dotted-id agy.fix.v1) + IFS="|" read -r case_dir home proj wt fakebin launchlog id </dev/null \ + || fail "agy plugin manifest name is not schema-safe for a dotted task ID: $(cat "$manifest")" + pass "fm-spawn: agy plugin manifest supports dotted task IDs" +} + +test_agy_plugin_collisions_are_refused() { + local rec case_dir home proj wt fakebin launchlog id out plugin exclude + rec=$(make_spawn_case plugin-collision) + IFS="|" read -r case_dir home proj wt fakebin launchlog id < "$plugin/plugin.json" + exclude=$(git -C "$wt" rev-parse --git-path info/exclude) + printf '/.agents/plugins/fm-firstmate-busy-%s/\n' "$id" >> "$exclude" + out=$(run_agy_spawn "$home" "$proj" "$wt" "$fakebin" "$launchlog" "$id") + expect_code 1 $? "agy spawn should refuse an existing plugin directory" + assert_contains "$out" "plugin path already exists" "agy collision refusal was not actionable: $out" + [ "$(cat "$plugin/plugin.json")" = project-owned ] || fail "agy spawn overwrote a project-owned plugin manifest" + assert_absent "$plugin/hooks.json" "agy spawn wrote hooks into a project-owned plugin directory" + assert_absent "$home/state/$id.meta" "agy plugin collision published task metadata" + + rec=$(make_spawn_case plugin-symlink) + IFS="|" read -r case_dir home proj wt fakebin launchlog id < "$wt/agy-plugin-target/sentinel" + plugin="$wt/.agents/plugins/fm-firstmate-busy-$id" + ln -s ../../agy-plugin-target "$plugin" + exclude=$(git -C "$wt" rev-parse --git-path info/exclude) + printf '/.agents/plugins/fm-firstmate-busy-%s\n/agy-plugin-target/\n' "$id" >> "$exclude" + out=$(run_agy_spawn "$home" "$proj" "$wt" "$fakebin" "$launchlog" "$id") + expect_code 1 $? "agy spawn should refuse a symlinked plugin directory" + assert_contains "$out" "plugin path is a symlink" "agy symlink refusal was not actionable: $out" + [ "$(cat "$wt/agy-plugin-target/sentinel")" = target-owned ] || fail "agy spawn changed the symlink target" + assert_absent "$wt/agy-plugin-target/plugin.json" "agy spawn followed the plugin symlink for its manifest" + assert_absent "$wt/agy-plugin-target/hooks.json" "agy spawn followed the plugin symlink for its hooks" + pass "fm-spawn: agy plugin installation refuses collisions and symlinks" +} + test_agy_missing_binary_refuses_before_endpoint_creation() { local rec case_dir home proj wt fakebin launchlog id out rec=$(make_spawn_case missing-binary) @@ -318,6 +377,8 @@ test_agy_harness_detection test_agy_default_model_and_launch_template test_agy_effort_flag_handling test_agy_semantic_busy_lifecycle +test_agy_manifest_name_accepts_dotted_task_id +test_agy_plugin_collisions_are_refused test_agy_missing_binary_refuses_before_endpoint_creation test_agy_refuses_secondmate test_agy_busy_matching_and_liveness From f21ea70ad2aafc359acec81f6e5da3796ea5d2f5 Mon Sep 17 00:00:00 2001 From: Bryan Travis Smith Date: Mon, 24 Aug 2026 10:34:35 -0700 Subject: [PATCH 04/28] no-mistakes(review): Harden agy lifecycle and plugin cleanup --- .agents/skills/harness-adapters/SKILL.md | 2 +- bin/fm-control-lib.sh | 35 ++++++++++++ bin/fm-spawn.sh | 72 +++++++++++++++++------- bin/fm-teardown.sh | 34 ++++++----- tests/fm-agy-harness.test.sh | 11 +++- tests/fm-control.test.sh | 23 ++++++++ tests/fm-teardown.test.sh | 26 +++++++++ 7 files changed, 167 insertions(+), 36 deletions(-) diff --git a/.agents/skills/harness-adapters/SKILL.md b/.agents/skills/harness-adapters/SKILL.md index 2e521c48faa..03192a29eb3 100644 --- a/.agents/skills/harness-adapters/SKILL.md +++ b/.agents/skills/harness-adapters/SKILL.md @@ -549,7 +549,7 @@ It is not verified for secondmate or primary work. | Binary | Executable `agy` from `PATH`; verified on version 1.1.19. | | Launch | `agy --dangerously-skip-permissions __MODELFLAG____EFFORTFLAG__--prompt-interactive "$(__OPINPUT__ encode launch-brief < __BRIEF__)"`; positional prompts are rejected with exit code 2. | | Models | `gemini-3.7-flash-high` (default), `gemini-3.7-flash-medium`, `gemini-3.7-flash-low`, Pro models, Claude Sonnet/Opus models. Authoritative discovery: `agy models`. | -| Busy state | Firstmate's generated plugin uses `PreInvocation` to open the turn and closes it only when a `Stop` payload reports `fullyIdle: true`; false or malformed payloads leave the task busy. | +| Busy state | Firstmate's generated plugin uses `PreInvocation` to open the turn; `Stop` with `fullyIdle: false` keeps the execution loop running, `fullyIdle: true` closes the turn, and malformed payloads allow termination but retain busy state as version-drift containment. | | Delivery busy token | `esc to cancel`, the live footer present only while a turn is running. | | Exit command | `/exit` or `/quit` or `Ctrl+D` (exits cleanly with status 0). | | Interrupt | Single Escape or `Ctrl+C` cancels active turn and returns to composer. | diff --git a/bin/fm-control-lib.sh b/bin/fm-control-lib.sh index 1f5520efa09..f9dbdcf6634 100644 --- a/bin/fm-control-lib.sh +++ b/bin/fm-control-lib.sh @@ -241,6 +241,41 @@ fm_control_harness_wiring_dirs() { # esac } +fm_control_harness_wiring_cleanup() { # + local harness=${1-} wt=${2-} state=${3-} id=${4-} dir parent path + [ -n "$wt" ] && [ -n "$state" ] && [ -n "$id" ] || return 1 + harness=$(fm_control_harness_family "$harness") || return 0 + if [ "$harness" = agy ]; then + dir=$(fm_control_harness_wiring_dirs "$harness" "$wt" "$state" "$id") || return 1 + for parent in "$wt/.agents" "$wt/.agents/plugins"; do + if [ -L "$parent" ] || { [ -e "$parent" ] && [ ! -d "$parent" ]; }; then + echo "error: refusing agy wiring cleanup through an unsafe plugin parent: $parent" >&2 + return 1 + fi + done + if [ -L "$dir" ]; then + rm -f -- "$dir" + return + fi + if [ -e "$dir" ] && [ ! -d "$dir" ]; then + echo "error: refusing agy wiring cleanup through a non-directory plugin path: $dir" >&2 + return 1 + fi + fi + while IFS= read -r path; do + [ -n "$path" ] || continue + rm -f -- "$path" || return 1 + done </dev/null || [ ! -e "$path" ] || return 1 + done </dev/null || true - rmdir -- "$AGY_PLUGIN_DIR" 2>/dev/null || true - fi + fm_control_harness_wiring_cleanup agy "$WT" "${STATE_REAL:-$STATE}" "$ID" 2>/dev/null || true fi if [ "$RELAUNCH_REPLACEMENT_PENDING" = 1 ] \ && [ "$SPAWN_META_PUBLISH_STARTED" = 1 ] \ @@ -817,7 +814,7 @@ spawn_herdr_presentation_order_lock_acquire() { } clear_relaunch_harness_wiring() { - local harness=$1 wt=$2 state=$3 id=$4 token_path token auth_path path + local harness=$1 wt=$2 state=$3 id=$4 token_path token auth_path # The wiring arms above match on harness PREFIXES, because a task launched # from a raw command records that command's basename rather than the exact # adapter name. The retirement tables are keyed by the exact adapter, so the @@ -835,18 +832,7 @@ clear_relaunch_harness_wiring() { if [ -n "$auth_path" ]; then rm -f -- "$auth_path" || return 1 fi - while IFS= read -r path; do - [ -n "$path" ] || continue - rm -f -- "$path" || return 1 - done </dev/null || [ ! -e "$path" ] || return 1 - done < "$tmp" && mv -f -- "$tmp" "$STATE/$ID.meta" || { + rm -f -- "$tmp" + return 1 + } +} + spawn_herdr_presentation_order_lock_release() { [ "$HERDR_PRESENTATION_ORDER_LOCK_HELD" = 1 ] || return 0 HERDR_PRESENTATION_ORDER_LOCK_HELD=0 @@ -2448,7 +2471,18 @@ if [ "$RELAUNCH" -eq 1 ]; then RELAUNCH_REPLACEMENT_WT=$WT fi case "$HARNESS" in - agy*) agy_plugin_prepare "$WT" "$STATE_REAL" "$ID" || exit 1 ;; + agy*) + if ! agy_plugin_prepare "$WT" "$STATE_REAL" "$ID"; then + if [ "$RELAUNCH" -eq 0 ] && [ "$BACKEND" != orca ]; then + if publish_spawn_recovery_meta; then + echo "error: agy setup stopped after endpoint creation; recoverable metadata was preserved at $STATE/$ID.meta" >&2 + else + echo "error: agy setup stopped after endpoint creation and recovery metadata could not be published" >&2 + fi + fi + exit 1 + fi + ;; esac if [ "$KIND" != secondmate ]; then # Arm the semantic busy-state contract (bin/fm-busy-lib.sh) for every @@ -2599,7 +2633,7 @@ EOF busy_cmd_prefix="$(shell_quote "$FM_ROOT/bin/fm-busy-event.sh") apply $(shell_quote "$STATE_REAL") $(shell_quote "$ID")" busy_suffix="--gen $(shell_quote "$BUSY_GEN") --source agy-hook" j_submit=$(json_escape "$busy_cmd_prefix busy $busy_suffix --event pre-invocation >/dev/null 2>&1 || true; printf '%s\\n' '{}'") - j_stop=$(json_escape "if $(shell_quote "$AGY_JQ_BIN") -e '.fullyIdle == true' >/dev/null 2>&1; then touch $(shell_quote "$TURNEND"); $busy_cmd_prefix idle $busy_suffix --event stop >/dev/null 2>&1 || true; fi; printf '%s\\n' '{\"decision\":\"allow\"}'") + j_stop=$(json_escape "agy_fully_idle=\$($(shell_quote "$AGY_JQ_BIN") -r 'if (.fullyIdle | type) == \"boolean\" then (.fullyIdle | tostring) else \"invalid\" end' 2>/dev/null) || agy_fully_idle=invalid; if [ \"\$agy_fully_idle\" = true ]; then touch $(shell_quote "$TURNEND"); $busy_cmd_prefix idle $busy_suffix --event stop >/dev/null 2>&1 || true; printf '%s\\n' '{\"decision\":\"allow\"}'; elif [ \"\$agy_fully_idle\" = false ]; then printf '%s\\n' '{\"decision\":\"continue\",\"reason\":\"Background work remains active\"}'; else printf '%s\\n' '{\"decision\":\"allow\"}'; fi") if ! (set -o noclobber; cat > "$AGY_PLUGIN_DIR/plugin.json" </dev/null || true) fi +HARNESS=$(fm_meta_get "$META" harness) +HARNESS_FAMILY=$(fm_control_harness_family "$HARNESS" 2>/dev/null || true) ORCA_WORKTREE_ID=$(fm_meta_get "$META" orca_worktree_id) ORCA_PATH_MATCH_VERIFIED=0 @@ -2412,7 +2414,7 @@ preflight_firstmate_home_herdr_children() { # } cleanup_firstmate_home_children() { - local home=$1 sub_state child_meta child_id child_t child_wt child_proj child_kind child_home child_backend child_orca_worktree_id child_return_rc child_busy_gen + local home=$1 sub_state child_meta child_id child_t child_wt child_proj child_kind child_home child_backend child_harness child_harness_family child_orca_worktree_id child_return_rc child_busy_gen sub_state="$home/state" [ -d "$sub_state" ] || return 0 for child_meta in "$sub_state"/*.meta; do @@ -2422,6 +2424,8 @@ cleanup_firstmate_home_children() { child_proj=$(meta_value "$child_meta" project) child_kind=$(meta_value "$child_meta" kind) [ -n "$child_kind" ] || child_kind=ship + child_harness=$(meta_value "$child_meta" harness) + child_harness_family=$(fm_control_harness_family "$child_harness" 2>/dev/null || true) child_backend=$(fm_backend_of_meta "$child_meta") if [ "$child_backend" = orca ]; then child_t=$(meta_value "$child_meta" terminal) @@ -2465,18 +2469,20 @@ cleanup_firstmate_home_children() { if [ -n "$child_wt" ] && [ -d "$child_wt" ]; then validate_child_worktree_for_removal "$child_wt" "$child_proj" >/dev/null || return 1 rm -f "$child_wt/.claude/settings.local.json" "$child_wt/.opencode/plugins/fm-turn-end.js" \ - "$child_wt/.fm-grok-turnend" "$child_wt/.fm-kimi-turnend" \ - "$child_wt/.agents/plugins/fm-firstmate-busy-$child_id/plugin.json" \ - "$child_wt/.agents/plugins/fm-firstmate-busy-$child_id/hooks.json" + "$child_wt/.fm-grok-turnend" "$child_wt/.fm-kimi-turnend" + [ "$child_harness_family" != agy ] \ + || fm_control_harness_wiring_cleanup agy "$child_wt" "$sub_state" "$child_id" \ + || return 1 fi fm_backend_remove_worktree "$child_backend" "$child_orca_worktree_id" || return 1 elif [ -n "$child_wt" ] && [ -d "$child_wt" ]; then validate_child_worktree_for_removal "$child_wt" "$child_proj" >/dev/null || return 1 rm -f "$child_wt/.claude/settings.local.json" "$child_wt/.opencode/plugins/fm-turn-end.js" \ "$child_wt/.opencode/plugins/fm-busy-state.js" \ - "$child_wt/.fm-grok-turnend" "$child_wt/.fm-kimi-turnend" \ - "$child_wt/.agents/plugins/fm-firstmate-busy-$child_id/plugin.json" \ - "$child_wt/.agents/plugins/fm-firstmate-busy-$child_id/hooks.json" + "$child_wt/.fm-grok-turnend" "$child_wt/.fm-kimi-turnend" + [ "$child_harness_family" != agy ] \ + || fm_control_harness_wiring_cleanup agy "$child_wt" "$sub_state" "$child_id" \ + || return 1 if [ -n "$child_proj" ] && [ -d "$child_proj" ] && command -v treehouse >/dev/null 2>&1; then if teardown_treehouse_return "$child_wt" "$child_proj" "child worktree"; then : @@ -2689,9 +2695,10 @@ if [ "$BACKEND" = orca ] && [ "$KIND" != secondmate ]; then fi rm -f "$WT/.claude/settings.local.json" "$WT/.opencode/plugins/fm-turn-end.js" \ "$WT/.opencode/plugins/fm-busy-state.js" \ - "$WT/.fm-grok-turnend" "$WT/.fm-kimi-turnend" \ - "$WT/.agents/plugins/fm-firstmate-busy-$ID/plugin.json" \ - "$WT/.agents/plugins/fm-firstmate-busy-$ID/hooks.json" + "$WT/.fm-grok-turnend" "$WT/.fm-kimi-turnend" + [ "$HARNESS_FAMILY" != agy ] \ + || fm_control_harness_wiring_cleanup agy "$WT" "$STATE" "$ID" \ + || exit 1 fi [ -z "$T_ORCA" ] || fm_backend_kill "$BACKEND" "$T" "$(meta_value "$META" zellij_tab_id)" "fm-$ID" 2>/dev/null || true fm_backend_remove_worktree "$BACKEND" "$ORCA_WORKTREE_ID" @@ -2704,9 +2711,10 @@ elif [ -d "$WT" ] && [ "$KIND" != secondmate ]; then fi # Remove our hook file so a reused pool worktree cannot fire signals for a dead task. rm -f "$WT/.claude/settings.local.json" "$WT/.opencode/plugins/fm-turn-end.js" \ - "$WT/.fm-grok-turnend" "$WT/.fm-kimi-turnend" \ - "$WT/.agents/plugins/fm-firstmate-busy-$ID/plugin.json" \ - "$WT/.agents/plugins/fm-firstmate-busy-$ID/hooks.json" + "$WT/.fm-grok-turnend" "$WT/.fm-kimi-turnend" + [ "$HARNESS_FAMILY" != agy ] \ + || fm_control_harness_wiring_cleanup agy "$WT" "$STATE" "$ID" \ + || exit 1 # Kills remaining processes in the worktree (including the agent), resets, returns # to pool. treehouse resolves the pool from the working directory, so run it from # the project. teardown_treehouse_return tolerates transient and stale git locks diff --git a/tests/fm-agy-harness.test.sh b/tests/fm-agy-harness.test.sh index 9a18dc06cbe..4c45b51a3d7 100755 --- a/tests/fm-agy-harness.test.sh +++ b/tests/fm-agy-harness.test.sh @@ -210,12 +210,14 @@ EOF || fail "agy launch turn was not seeded busy" rm -f "$state/$id.turn-ended" out=$(run_agy_hook "$hooks" Stop '{"fullyIdle":false}') || fail "agy active Stop hook failed: $out" - printf '%s' "$out" | jq -e '.decision == "allow"' >/dev/null \ - || fail "agy Stop hook did not return its required JSON contract: $out" + printf '%s' "$out" | jq -e '.decision == "continue" and (.reason | length > 0)' >/dev/null \ + || fail "agy active Stop hook did not keep the execution loop running: $out" assert_absent "$state/$id.turn-ended" "agy Stop hook published a turn end while background work remained active" [ "$(fm_busy_classify tmux fake:w agy "$id" "$state")" = "busy fm-spawn" ] \ || fail "agy Stop hook cleared semantic state while fullyIdle was false" out=$(run_agy_hook "$hooks" Stop '{}') || fail "agy malformed Stop hook failed: $out" + printf '%s' "$out" | jq -e '.decision == "allow"' >/dev/null \ + || fail "agy malformed Stop hook did not terminate defensively: $out" assert_absent "$state/$id.turn-ended" "agy Stop hook published a turn end without fullyIdle true" [ "$(fm_busy_classify tmux fake:w agy "$id" "$state")" = "busy fm-spawn" ] \ || fail "agy Stop hook cleared semantic state without fullyIdle true" @@ -261,7 +263,9 @@ EOF assert_contains "$out" "plugin path already exists" "agy collision refusal was not actionable: $out" [ "$(cat "$plugin/plugin.json")" = project-owned ] || fail "agy spawn overwrote a project-owned plugin manifest" assert_absent "$plugin/hooks.json" "agy spawn wrote hooks into a project-owned plugin directory" - assert_absent "$home/state/$id.meta" "agy plugin collision published task metadata" + assert_present "$home/state/$id.meta" "agy plugin collision did not preserve recovery metadata" + assert_contains "$(cat "$home/state/$id.meta")" "worktree=$wt" "agy recovery metadata omitted the allocated worktree" + assert_contains "$(cat "$home/state/$id.meta")" "harness=unknown" "agy collision recovery metadata claimed ownership of the colliding plugin" rec=$(make_spawn_case plugin-symlink) IFS="|" read -r case_dir home proj wt fakebin launchlog id < "$target/plugin.json" + printf 'target hooks\n' > "$target/hooks.json" + ln -s "$target" "$plugin" + fm_control_harness_wiring_cleanup agy "$wt" "$state" task-x1 \ + || fail "agy wiring cleanup refused its replaceable leaf symlink" + [ ! -e "$plugin" ] && [ ! -L "$plugin" ] \ + || fail "agy wiring cleanup did not unlink the replaceable plugin symlink" + [ "$(cat "$target/plugin.json")" = "target manifest" ] \ + || fail "agy wiring cleanup followed the symlink to the target manifest" + [ "$(cat "$target/hooks.json")" = "target hooks" ] \ + || fail "agy wiring cleanup followed the symlink to the target hooks" + pass "fm-control-lib: agy wiring cleanup unlinks symlinks without traversal" +} + test_orca_refuses_an_escape_harness_interrupt() { local dir out rc dir=$(new_case orca-escape) @@ -898,6 +920,7 @@ test_prefixed_recorded_harness_reaches_each_control_verb test_backend_key_capability_matrix test_harness_kind_capability test_agy_wiring_contract +test_agy_wiring_cleanup_does_not_follow_symlinks test_orca_refuses_an_escape_harness_interrupt test_unverified_state_backends_refuse_stop_verbs test_state_verified_backends_are_exactly_tmux_and_herdr diff --git a/tests/fm-teardown.test.sh b/tests/fm-teardown.test.sh index a0815a967e8..6a7c1399d93 100755 --- a/tests/fm-teardown.test.sh +++ b/tests/fm-teardown.test.sh @@ -1326,6 +1326,31 @@ test_teardown_missing_busy_sidecar_completes() { pass "teardown completes when an exact busy-state sidecar is already absent" } +test_agy_teardown_does_not_follow_replaced_plugin_symlink() { + local case_dir plugin target rc + case_dir=$(make_case agy-plugin-symlink) + write_meta "$case_dir" local-only ship + printf 'harness=agy\n' >> "$case_dir/state/task-x1.meta" + plugin="$case_dir/wt/.agents/plugins/fm-firstmate-busy-task-x1" + target="$case_dir/plugin-target" + mkdir -p "${plugin%/*}" "$target" + printf 'target manifest\n' > "$target/plugin.json" + printf 'target hooks\n' > "$target/hooks.json" + ln -s "$target" "$plugin" + + rc=0 + run_teardown "$case_dir" --force > "$case_dir/stdout" 2> "$case_dir/stderr" || rc=$? + + expect_code 0 "$rc" "agy-plugin-symlink: forced teardown should complete safely" + [ ! -e "$plugin" ] && [ ! -L "$plugin" ] \ + || fail "agy-plugin-symlink: teardown did not unlink the replaced plugin directory" + [ "$(cat "$target/plugin.json")" = "target manifest" ] \ + || fail "agy-plugin-symlink: teardown followed the symlink to the target manifest" + [ "$(cat "$target/hooks.json")" = "target hooks" ] \ + || fail "agy-plugin-symlink: teardown followed the symlink to the target hooks" + pass "agy teardown unlinks a replaced plugin directory without traversal" +} + test_herdr_teardown_clears_escalation_marker() { local case_dir marker case_dir=$(make_case herdr-marker-cleanup) @@ -2600,6 +2625,7 @@ test_no_mistakes_origin_remote_allows test_no_mistakes_truly_unpushed_refuses test_local_only_force_overrides_unpushed test_teardown_missing_busy_sidecar_completes +test_agy_teardown_does_not_follow_replaced_plugin_symlink test_herdr_teardown_clears_escalation_marker test_herdr_flat_teardown_refuses_orphaning_records_then_retry_completes test_herdr_flat_teardown_refuses_records_on_unparseable_presence From 36642362ae1ef0c881e41be51c4457a616d9f20e Mon Sep 17 00:00:00 2001 From: Bryan Travis Smith Date: Mon, 24 Aug 2026 10:50:11 -0700 Subject: [PATCH 05/28] no-mistakes(review): Harden agy ownership and abort recovery --- bin/fm-control-lib.sh | 4 ++++ bin/fm-session-lock-lib.sh | 6 +++--- bin/fm-spawn.sh | 25 +++++++++++++++--------- tests/fm-agy-harness.test.sh | 37 ++++++++++++++++++++++++++++++++---- tests/fm-control.test.sh | 23 ++++++++++++++++++++++ 5 files changed, 79 insertions(+), 16 deletions(-) diff --git a/bin/fm-control-lib.sh b/bin/fm-control-lib.sh index f9dbdcf6634..b90ad4d5114 100644 --- a/bin/fm-control-lib.sh +++ b/bin/fm-control-lib.sh @@ -246,6 +246,10 @@ fm_control_harness_wiring_cleanup() { # [ -n "$wt" ] && [ -n "$state" ] && [ -n "$id" ] || return 1 harness=$(fm_control_harness_family "$harness") || return 0 if [ "$harness" = agy ]; then + if [ -L "$wt" ] || { [ -e "$wt" ] && [ ! -d "$wt" ]; }; then + echo "error: refusing agy wiring cleanup through an unsafe worktree path: $wt" >&2 + return 1 + fi dir=$(fm_control_harness_wiring_dirs "$harness" "$wt" "$state" "$id") || return 1 for parent in "$wt/.agents" "$wt/.agents/plugins"; do if [ -L "$parent" ] || { [ -e "$parent" ] && [ ! -d "$parent" ]; }; then diff --git a/bin/fm-session-lock-lib.sh b/bin/fm-session-lock-lib.sh index 9ceaf0ce6f6..95802eb6266 100644 --- a/bin/fm-session-lock-lib.sh +++ b/bin/fm-session-lock-lib.sh @@ -16,14 +16,14 @@ # shellcheck source=bin/fm-cursor-lib.sh . "$(dirname -- "${BASH_SOURCE[0]}")/fm-cursor-lib.sh" -# Known harness command names; extend when a new adapter is verified. -FM_HARNESS_RE='claude|codex|opencode|grok|kimi|agy|^pi$|^pi-signed$' +# Known primary-harness command names; extend when a new primary adapter is verified. +FM_HARNESS_RE='claude|codex|opencode|grok|kimi|^pi$|^pi-signed$' # The same harnesses as exact executable names. Keep in sync with # FM_HARNESS_RE. Used only for the stricter path evidence below, where the # loose regex would also match ordinary firstmate paths such as # bin/fm-claude-stop-autoarm.sh. -FM_HARNESS_NAMES=(claude codex opencode grok kimi agy pi-signed pi) +FM_HARNESS_NAMES=(claude codex opencode grok kimi pi-signed pi) # Print the exact harness name carried by executable path $1 - its own basename # or any directory component - or return 1. diff --git a/bin/fm-spawn.sh b/bin/fm-spawn.sh index c58ecd9c42b..70c2d13c284 100755 --- a/bin/fm-spawn.sh +++ b/bin/fm-spawn.sh @@ -672,6 +672,7 @@ RELAUNCH_REPLACEMENT_STATE= RELAUNCH_REPLACEMENT_WT= AGY_PLUGIN_INSTALL_PENDING=0 AGY_PLUGIN_DIR= +AGY_RECOVERY_META_PENDING=0 CONFIG_INHERIT_LOCK= CONFIG_INHERIT_LOCK_HELD=0 @@ -698,6 +699,17 @@ spawn_abort_cleanup() { AGY_PLUGIN_INSTALL_PENDING=0 fm_control_harness_wiring_cleanup agy "$WT" "${STATE_REAL:-$STATE}" "$ID" 2>/dev/null || true fi + if [ "$AGY_RECOVERY_META_PENDING" = 1 ]; then + AGY_RECOVERY_META_PENDING=0 + if [ "$status" -ne 0 ] && [ "$RELAUNCH" -eq 0 ] && [ "$BACKEND" != orca ] \ + && [ ! -e "$STATE/$ID.meta" ] && [ ! -L "$STATE/$ID.meta" ]; then + if publish_spawn_recovery_meta; then + echo "error: agy setup stopped after endpoint creation; recoverable metadata was preserved at $STATE/$ID.meta" >&2 + else + echo "error: agy setup stopped after endpoint creation and recovery metadata could not be published" >&2 + fi + fi + fi if [ "$RELAUNCH_REPLACEMENT_PENDING" = 1 ] \ && [ "$SPAWN_META_PUBLISH_STARTED" = 1 ] \ && [ -n "$SPAWN_META_TMP" ] \ @@ -2472,16 +2484,10 @@ if [ "$RELAUNCH" -eq 1 ]; then fi case "$HARNESS" in agy*) - if ! agy_plugin_prepare "$WT" "$STATE_REAL" "$ID"; then - if [ "$RELAUNCH" -eq 0 ] && [ "$BACKEND" != orca ]; then - if publish_spawn_recovery_meta; then - echo "error: agy setup stopped after endpoint creation; recoverable metadata was preserved at $STATE/$ID.meta" >&2 - else - echo "error: agy setup stopped after endpoint creation and recovery metadata could not be published" >&2 - fi - fi - exit 1 + if [ "$RELAUNCH" -eq 0 ] && [ "$BACKEND" != orca ]; then + AGY_RECOVERY_META_PENDING=1 fi + agy_plugin_prepare "$WT" "$STATE_REAL" "$ID" || exit 1 ;; esac if [ "$KIND" != secondmate ]; then @@ -2901,6 +2907,7 @@ if [ "$RELAUNCH" -eq 1 ]; then fm_lock_release "$SPAWN_META_LOCK" SPAWN_META_LOCK_HELD=0 fi +AGY_RECOVERY_META_PENDING=0 if [ "$SPAWN_TASK_SET_LOCK_HELD" = 1 ]; then # The record is published, so this task is now part of the set a teardown # enumerates and locks per task. The set lock is only needed across that diff --git a/tests/fm-agy-harness.test.sh b/tests/fm-agy-harness.test.sh index 4c45b51a3d7..ac6ffb3a7ab 100755 --- a/tests/fm-agy-harness.test.sh +++ b/tests/fm-agy-harness.test.sh @@ -92,10 +92,13 @@ test_agy_harness_detection() { # shellcheck source=bin/fm-session-lock-lib.sh . "$ROOT/bin/fm-session-lock-lib.sh" - printf "agy\n" | grep -qE "$FM_HARNESS_RE" || fail "FM_HARNESS_RE did not match agy" - printf "foo-agy-bar\n" | grep -qE "$FM_HARNESS_RE" || fail "FM_HARNESS_RE did not match *agy*" - [ "$(fm_harness_path_name /opt/homebrew/bin/agy)" = agy ] || fail "session-lock exact path table did not identify agy" - pass "fm-harness and session-lock: detects agy markers and process identity" + if fm_harness_process_matches agy agy; then + fail "worker-only agy was accepted as a primary session-lock owner" + fi + if fm_harness_path_name /opt/homebrew/bin/agy >/dev/null; then + fail "worker-only agy was present in the primary path-identity table" + fi + pass "fm-harness detects agy without granting primary session ownership" } test_agy_default_model_and_launch_template() { @@ -287,6 +290,31 @@ EOF pass "fm-spawn: agy plugin installation refuses collisions and symlinks" } +test_agy_post_allocation_failure_preserves_recovery_metadata() { + local rec case_dir home proj wt fakebin launchlog id out plugin meta + rec=$(make_spawn_case busy-arm-failure) + IFS="|" read -r case_dir home proj wt fakebin launchlog id < "$plugin/plugin.json" + printf 'target hooks\n' > "$plugin/hooks.json" + ln -s "$target" "$wt" + out=$(fm_control_harness_wiring_cleanup agy "$wt" "$state" task-x1 2>&1); rc=$? + expect_code 1 "$rc" "agy wiring cleanup should reject a symlinked worktree" + assert_contains "$out" "unsafe worktree path" \ + "agy wiring cleanup did not explain the symlinked-worktree refusal" + [ "$(cat "$plugin/plugin.json")" = "target manifest" ] \ + || fail "agy wiring cleanup followed the worktree symlink to the target manifest" + [ "$(cat "$plugin/hooks.json")" = "target hooks" ] \ + || fail "agy wiring cleanup followed the worktree symlink to the target hooks" + pass "fm-control-lib: agy wiring cleanup rejects symlinked worktrees" +} + test_orca_refuses_an_escape_harness_interrupt() { local dir out rc dir=$(new_case orca-escape) @@ -921,6 +943,7 @@ test_backend_key_capability_matrix test_harness_kind_capability test_agy_wiring_contract test_agy_wiring_cleanup_does_not_follow_symlinks +test_agy_wiring_cleanup_rejects_symlinked_worktree test_orca_refuses_an_escape_harness_interrupt test_unverified_state_backends_refuse_stop_verbs test_state_verified_backends_are_exactly_tmux_and_herdr From d1104937440252a938b7c584fe2efab3b9abff65 Mon Sep 17 00:00:00 2001 From: Bryan Travis Smith Date: Mon, 24 Aug 2026 11:05:45 -0700 Subject: [PATCH 06/28] no-mistakes(review): Preflight agy worktrees before teardown --- bin/fm-control-lib.sh | 15 +++++++++++---- bin/fm-teardown.sh | 11 +++++++---- tests/fm-teardown.test.sh | 28 ++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 8 deletions(-) diff --git a/bin/fm-control-lib.sh b/bin/fm-control-lib.sh index b90ad4d5114..8d9956a5952 100644 --- a/bin/fm-control-lib.sh +++ b/bin/fm-control-lib.sh @@ -241,15 +241,22 @@ fm_control_harness_wiring_dirs() { # esac } +fm_control_harness_worktree_preflight() { # + local harness=${1-} wt=${2-} + harness=$(fm_control_harness_family "$harness") || return 0 + [ "$harness" = agy ] || return 0 + if [ -L "$wt" ] || { [ -e "$wt" ] && [ ! -d "$wt" ]; }; then + echo "error: refusing agy operation through an unsafe worktree path: $wt" >&2 + return 1 + fi +} + fm_control_harness_wiring_cleanup() { # local harness=${1-} wt=${2-} state=${3-} id=${4-} dir parent path [ -n "$wt" ] && [ -n "$state" ] && [ -n "$id" ] || return 1 harness=$(fm_control_harness_family "$harness") || return 0 if [ "$harness" = agy ]; then - if [ -L "$wt" ] || { [ -e "$wt" ] && [ ! -d "$wt" ]; }; then - echo "error: refusing agy wiring cleanup through an unsafe worktree path: $wt" >&2 - return 1 - fi + fm_control_harness_worktree_preflight "$harness" "$wt" || return 1 dir=$(fm_control_harness_wiring_dirs "$harness" "$wt" "$state" "$id") || return 1 for parent in "$wt/.agents" "$wt/.agents/plugins"; do if [ -L "$parent" ] || { [ -e "$parent" ] && [ ! -d "$parent" ]; }; then diff --git a/bin/fm-teardown.sh b/bin/fm-teardown.sh index 08cb366e6c8..6e0f86d0107 100755 --- a/bin/fm-teardown.sh +++ b/bin/fm-teardown.sh @@ -673,6 +673,9 @@ BACKEND=$FM_BACKEND_VALIDATED_BACKEND T=$FM_BACKEND_VALIDATED_TARGET WT=$(fm_meta_get "$META" worktree) PROJ=$(fm_meta_get "$META" project) +HARNESS=$(fm_meta_get "$META" harness) +HARNESS_FAMILY=$(fm_control_harness_family "$HARNESS" 2>/dev/null || true) +fm_control_harness_worktree_preflight "$HARNESS_FAMILY" "$WT" || exit 1 T_ORCA= [ "$BACKEND" != orca ] || T_ORCA=$T if [ "${FM_TEARDOWN_GUARD_DONE:-0}" != 1 ]; then @@ -687,8 +690,6 @@ BUSY_GEN=$(fm_meta_get "$META" busy_gen) if [ -z "$BUSY_GEN" ]; then BUSY_GEN=$(cat "$STATE/$ID.busy-gen" 2>/dev/null || true) fi -HARNESS=$(fm_meta_get "$META" harness) -HARNESS_FAMILY=$(fm_control_harness_family "$HARNESS" 2>/dev/null || true) ORCA_WORKTREE_ID=$(fm_meta_get "$META" orca_worktree_id) ORCA_PATH_MATCH_VERIFIED=0 @@ -2247,15 +2248,17 @@ preflight_descendant_task_locks() { } validate_firstmate_home_children_removal() { - local home=$1 sub_state child_meta child_id child_wt child_proj child_kind child_home child_backend child_orca_worktree_id + local home=$1 sub_state child_meta child_id child_wt child_proj child_kind child_home child_backend child_harness child_orca_worktree_id sub_state="$home/state" [ -d "$sub_state" ] || return 0 for child_meta in "$sub_state"/*.meta; do [ -e "$child_meta" ] || continue child_id=$(basename "$child_meta" .meta) fm_backend_validate_task_endpoint "$child_meta" "$child_id" || return 1 - validate_pr_poll_cleanup "$sub_state" "$child_id" || return 1 child_wt=$(meta_value "$child_meta" worktree) + child_harness=$(meta_value "$child_meta" harness) + fm_control_harness_worktree_preflight "$child_harness" "$child_wt" || return 1 + validate_pr_poll_cleanup "$sub_state" "$child_id" || return 1 child_kind=$(meta_value "$child_meta" kind) [ -n "$child_kind" ] || child_kind=ship child_backend=$(fm_backend_of_meta "$child_meta") diff --git a/tests/fm-teardown.test.sh b/tests/fm-teardown.test.sh index 6a7c1399d93..c78b78b9070 100755 --- a/tests/fm-teardown.test.sh +++ b/tests/fm-teardown.test.sh @@ -1351,6 +1351,33 @@ test_agy_teardown_does_not_follow_replaced_plugin_symlink() { pass "agy teardown unlinks a replaced plugin directory without traversal" } +test_agy_teardown_rejects_replaced_worktree_before_mutation() { + local case_dir hook branch rc + case_dir=$(make_case agy-worktree-symlink) + write_meta "$case_dir" local-only ship + printf 'harness=agy\n' >> "$case_dir/state/task-x1.meta" + mv "$case_dir/wt" "$case_dir/wt-original" + ln -s "$case_dir/project" "$case_dir/wt" + hook="$case_dir/project/.claude/settings.local.json" + mkdir -p "${hook%/*}" + printf 'foreign hook\n' > "$hook" + + rc=0 + run_teardown "$case_dir" --force > "$case_dir/stdout" 2> "$case_dir/stderr" || rc=$? + + expect_code 1 "$rc" "agy-worktree-symlink: forced teardown should refuse" + assert_grep "unsafe worktree path" "$case_dir/stderr" \ + "agy-worktree-symlink: teardown did not explain the refusal" + [ "$(cat "$hook")" = "foreign hook" ] \ + || fail "agy-worktree-symlink: teardown removed a hook through the worktree symlink" + branch=$(git -C "$case_dir/project" symbolic-ref --short HEAD 2>/dev/null || true) + [ "$branch" = main ] \ + || fail "agy-worktree-symlink: teardown detached the symlink target checkout" + assert_present "$case_dir/state/task-x1.meta" \ + "agy-worktree-symlink: teardown removed task metadata after refusing" + pass "agy teardown rejects a replaced worktree before mutation" +} + test_herdr_teardown_clears_escalation_marker() { local case_dir marker case_dir=$(make_case herdr-marker-cleanup) @@ -2626,6 +2653,7 @@ test_no_mistakes_truly_unpushed_refuses test_local_only_force_overrides_unpushed test_teardown_missing_busy_sidecar_completes test_agy_teardown_does_not_follow_replaced_plugin_symlink +test_agy_teardown_rejects_replaced_worktree_before_mutation test_herdr_teardown_clears_escalation_marker test_herdr_flat_teardown_refuses_orphaning_records_then_retry_completes test_herdr_flat_teardown_refuses_records_on_unparseable_presence From 4c36ea40e4399b8ed1aac96b77f993aa70dc92db Mon Sep 17 00:00:00 2001 From: Bryan Travis Smith Date: Mon, 24 Aug 2026 11:39:46 -0700 Subject: [PATCH 07/28] no-mistakes(review): Observe agy completion and harden teardown quiescence --- .agents/skills/harness-adapters/SKILL.md | 4 +- bin/fm-busy-lib.sh | 2 +- bin/fm-control-lib.sh | 28 ++++ bin/fm-spawn.sh | 84 +++++++++++- bin/fm-teardown.sh | 162 +++++++++++++++++------ docs/architecture.md | 2 +- docs/configuration.md | 3 +- docs/verification/supervision.md | 2 +- tests/fm-agy-harness.test.sh | 43 ++++-- tests/fm-control.test.sh | 2 + tests/fm-teardown.test.sh | 45 ++++++- 11 files changed, 317 insertions(+), 60 deletions(-) diff --git a/.agents/skills/harness-adapters/SKILL.md b/.agents/skills/harness-adapters/SKILL.md index 03192a29eb3..daf268f5bf4 100644 --- a/.agents/skills/harness-adapters/SKILL.md +++ b/.agents/skills/harness-adapters/SKILL.md @@ -549,7 +549,7 @@ It is not verified for secondmate or primary work. | Binary | Executable `agy` from `PATH`; verified on version 1.1.19. | | Launch | `agy --dangerously-skip-permissions __MODELFLAG____EFFORTFLAG__--prompt-interactive "$(__OPINPUT__ encode launch-brief < __BRIEF__)"`; positional prompts are rejected with exit code 2. | | Models | `gemini-3.7-flash-high` (default), `gemini-3.7-flash-medium`, `gemini-3.7-flash-low`, Pro models, Claude Sonnet/Opus models. Authoritative discovery: `agy models`. | -| Busy state | Firstmate's generated plugin uses `PreInvocation` to open the turn; `Stop` with `fullyIdle: false` keeps the execution loop running, `fullyIdle: true` closes the turn, and malformed payloads allow termination but retain busy state as version-drift containment. | +| Busy state | Firstmate's generated plugin uses `PreInvocation` to open the turn. `Stop` with `fullyIdle: true` closes it immediately. When `fullyIdle` is false, the synchronous Stop hook reads the conversation's SQLite task rows and waits for every running background command or subtrajectory to become terminal before closing the turn. Malformed payloads or an unreadable store allow termination but retain busy state as version-drift containment. | | Delivery busy token | `esc to cancel`, the live footer present only while a turn is running. | | Exit command | `/exit` or `/quit` or `Ctrl+D` (exits cleanly with status 0). | | Interrupt | Single Escape or `Ctrl+C` cancels active turn and returns to composer. | @@ -570,3 +570,5 @@ Firstmate supervisors handle this dialog by sending `Enter` upon spawn peek. Accepted trust is saved to `~/.gemini/antigravity-cli/settings.json` under `trustedWorkspaces` and persists across subsequent launches. Firstmate writes an isolated project plugin under `.agents/plugins/` so existing project hooks remain untouched. Its `PreInvocation` and `Stop` hooks are gated on workspace trust and fire normally once trust is granted. +The lifecycle observer requires `jq` and `sqlite3`, resolves the conversation database from the hook's vendor-owned `conversationId` and `transcriptPath`, and opens that database read-only. +Antigravity CLI 1.1.9 and newer cap consecutive `decision: continue` responses, so Firstmate does not use continuation retries as completion state. diff --git a/bin/fm-busy-lib.sh b/bin/fm-busy-lib.sh index 4f81fec95c7..23245788260 100755 --- a/bin/fm-busy-lib.sh +++ b/bin/fm-busy-lib.sh @@ -31,7 +31,7 @@ # pi-ext Pi/pi-signed per-task extension (agent_start/agent_settled) # opencode-plugin OpenCode per-task plugin (session.status) # claude-hook Claude lifecycle hooks (UserPromptSubmit/Stop/StopFailure/SessionEnd) -# agy-hook Antigravity CLI plugin hooks (PreInvocation/Stop) +# agy-hook Antigravity CLI hooks plus conversation task state # codex-hook, codex-appserver reserved: Codex, gated by # fm_busy_codex_semantic_source # kimi-wire, kimi-hook reserved: standalone Kimi, gated by fm_busy_kimi_verified diff --git a/bin/fm-control-lib.sh b/bin/fm-control-lib.sh index 8d9956a5952..6818d39f03e 100644 --- a/bin/fm-control-lib.sh +++ b/bin/fm-control-lib.sh @@ -229,6 +229,7 @@ fm_control_harness_wiring_paths() { # agy) printf '%s\n' "$wt/.agents/plugins/fm-firstmate-busy-$id/plugin.json" printf '%s\n' "$wt/.agents/plugins/fm-firstmate-busy-$id/hooks.json" + printf '%s\n' "$wt/.agents/plugins/fm-firstmate-busy-$id/fm-stop.sh" ;; esac } @@ -251,6 +252,33 @@ fm_control_harness_worktree_preflight() { # fi } +fm_control_harness_worktree_identity() { # + local harness=${1-} wt=${2-} + harness=$(fm_control_harness_family "$harness") || return 1 + [ "$harness" = agy ] || return 1 + fm_control_harness_worktree_preflight "$harness" "$wt" || return 1 + if [ ! -e "$wt" ]; then + printf 'missing\n' + elif [ "$(uname 2>/dev/null)" = Darwin ]; then + LC_ALL=C stat -f '%d:%i' "$wt" 2>/dev/null + else + LC_ALL=C stat -c '%d:%i' "$wt" 2>/dev/null + fi +} + +fm_control_harness_worktree_identity_verify() { # + local harness=${1-} wt=${2-} expected=${3-} current + [ -n "$expected" ] || return 1 + current=$(fm_control_harness_worktree_identity "$harness" "$wt") || { + echo "error: refusing agy operation because the worktree identity changed or became unsafe: $wt" >&2 + return 1 + } + if [ "$current" != "$expected" ]; then + echo "error: refusing agy operation because the worktree identity changed: $wt" >&2 + return 1 + fi +} + fm_control_harness_wiring_cleanup() { # local harness=${1-} wt=${2-} state=${3-} id=${4-} dir parent path [ -n "$wt" ] && [ -n "$state" ] && [ -n "$id" ] || return 1 diff --git a/bin/fm-spawn.sh b/bin/fm-spawn.sh index 70c2d13c284..af92919b497 100755 --- a/bin/fm-spawn.sh +++ b/bin/fm-spawn.sh @@ -1634,6 +1634,10 @@ case "$LAUNCH" in echo "error: jq executable not found on PATH; agy lifecycle hooks require jq" >&2 exit 1 } + AGY_SQLITE_BIN=$(command -v sqlite3) || { + echo "error: sqlite3 executable not found on PATH; agy lifecycle hooks require sqlite3" >&2 + exit 1 + } LAUNCH=${LAUNCH//__AGYBIN__/$(shell_quote "$AGY_BIN")} ;; esac @@ -2639,7 +2643,82 @@ EOF busy_cmd_prefix="$(shell_quote "$FM_ROOT/bin/fm-busy-event.sh") apply $(shell_quote "$STATE_REAL") $(shell_quote "$ID")" busy_suffix="--gen $(shell_quote "$BUSY_GEN") --source agy-hook" j_submit=$(json_escape "$busy_cmd_prefix busy $busy_suffix --event pre-invocation >/dev/null 2>&1 || true; printf '%s\\n' '{}'") - j_stop=$(json_escape "agy_fully_idle=\$($(shell_quote "$AGY_JQ_BIN") -r 'if (.fullyIdle | type) == \"boolean\" then (.fullyIdle | tostring) else \"invalid\" end' 2>/dev/null) || agy_fully_idle=invalid; if [ \"\$agy_fully_idle\" = true ]; then touch $(shell_quote "$TURNEND"); $busy_cmd_prefix idle $busy_suffix --event stop >/dev/null 2>&1 || true; printf '%s\\n' '{\"decision\":\"allow\"}'; elif [ \"\$agy_fully_idle\" = false ]; then printf '%s\\n' '{\"decision\":\"continue\",\"reason\":\"Background work remains active\"}'; else printf '%s\\n' '{\"decision\":\"allow\"}'; fi") + if ! (set -o noclobber; cat > "$AGY_PLUGIN_DIR/fm-stop.sh" </dev/null) || agy_fully_idle=invalid +if [ "\$agy_fully_idle" = true ]; then + touch $(shell_quote "$TURNEND") + $busy_cmd_prefix idle $busy_suffix --event stop >/dev/null 2>&1 || true + printf '%s\n' '{"decision":"allow"}' + exit 0 +fi +if [ "\$agy_fully_idle" != false ]; then + printf '%s\n' '{"decision":"allow"}' + exit 0 +fi +agy_conversation=\$(printf '%s' "\$agy_payload" | $(shell_quote "$AGY_JQ_BIN") -r 'if (.conversationId | type) == "string" then .conversationId else "" end' 2>/dev/null) || agy_conversation= +agy_transcript=\$(printf '%s' "\$agy_payload" | $(shell_quote "$AGY_JQ_BIN") -r 'if (.transcriptPath | type) == "string" then .transcriptPath else "" end' 2>/dev/null) || agy_transcript= +case "\$agy_conversation" in + ''|*[!A-Za-z0-9-]*) printf '%s\n' '{"decision":"allow"}'; exit 0 ;; +esac +case "\$agy_transcript" in + */brain/"\$agy_conversation"/*) agy_store=\${agy_transcript%%/brain/"\$agy_conversation"/*} ;; + *) printf '%s\n' '{"decision":"allow"}'; exit 0 ;; +esac +agy_db="\$agy_store/conversations/\$agy_conversation.db" +agy_attempt=0 +while [ "\$agy_attempt" -lt 50 ] && { [ ! -f "\$agy_db" ] || [ -L "\$agy_db" ]; }; do + sleep 0.2 + agy_attempt=\$((agy_attempt + 1)) +done +if [ ! -f "\$agy_db" ] || [ -L "\$agy_db" ]; then + printf '%s\n' '{"decision":"allow"}' + exit 0 +fi +agy_seen_active=0 +agy_unreadable=0 +agy_unseen=0 +while :; do + agy_running=\$($(shell_quote "$AGY_SQLITE_BIN") -readonly -cmd '.timeout 1000' "\$agy_db" 'SELECT count(*) FROM steps WHERE status = 2 AND (task_details IS NOT NULL OR has_subtrajectory != 0);' 2>/dev/null) || agy_running=invalid + case "\$agy_running" in + ''|*[!0-9]*) + agy_unreadable=\$((agy_unreadable + 1)) + if [ "\$agy_unreadable" -ge 50 ]; then + printf '%s\n' '{"decision":"allow"}' + exit 0 + fi + sleep 0.2 + continue + ;; + esac + agy_unreadable=0 + if [ "\$agy_running" -gt 0 ]; then + agy_seen_active=1 + elif [ "\$agy_seen_active" -eq 1 ]; then + touch $(shell_quote "$TURNEND") + $busy_cmd_prefix idle $busy_suffix --event background-complete >/dev/null 2>&1 || true + printf '%s\n' '{"decision":"allow"}' + exit 0 + else + agy_unseen=\$((agy_unseen + 1)) + if [ "\$agy_unseen" -ge 50 ]; then + printf '%s\n' '{"decision":"allow"}' + exit 0 + fi + fi + sleep 0.2 +done +EOF + ); then + echo "error: refusing to replace an existing agy lifecycle observer: $AGY_PLUGIN_DIR/fm-stop.sh" >&2 + exit 1 + fi + chmod 700 "$AGY_PLUGIN_DIR/fm-stop.sh" || { + echo "error: could not make the agy lifecycle observer executable: $AGY_PLUGIN_DIR/fm-stop.sh" >&2 + exit 1 + } + j_stop=$(json_escape "$(shell_quote "$AGY_PLUGIN_DIR/fm-stop.sh")") if ! (set -o noclobber; cat > "$AGY_PLUGIN_DIR/plugin.json" < "$AGY_PLUGIN_DIR/hooks.json" <&2 @@ -2656,6 +2735,7 @@ EOF fi exclude_path ".agents/plugins/fm-firstmate-busy-$ID/plugin.json" exclude_path ".agents/plugins/fm-firstmate-busy-$ID/hooks.json" + exclude_path ".agents/plugins/fm-firstmate-busy-$ID/fm-stop.sh" ;; codex*) # Semantic busy-state source negotiation (bin/fm-busy-lib.sh owns the diff --git a/bin/fm-teardown.sh b/bin/fm-teardown.sh index 6e0f86d0107..1da03d7a160 100755 --- a/bin/fm-teardown.sh +++ b/bin/fm-teardown.sh @@ -2416,8 +2416,47 @@ preflight_firstmate_home_herdr_children() { # done } +teardown_endpoint_exists_in_home() { #