From bfc7aac1769b856f1c321cd358d18c26773650ef Mon Sep 17 00:00:00 2001 From: max-metaplanet Date: Fri, 6 Nov 2026 06:59:42 -0500 Subject: [PATCH 1/4] feat/no-ticket(supervision): verify a Grok host engine A host takeover now keeps an already-announced recovery from being handed straight back, and Grok headless mode is a verified supervision-host engine with the same live guard as Claude. OpenCode's free provider does not meet the isolation or cost contract, so it is recorded and not shipped. --- .../references/harness/grok.md | 2 +- bin/fm-lock.sh | 8 +- bin/fm-session-lock-lib.sh | 1 + bin/fm-supervision-engine-lib.sh | 244 +++++++++++++++--- bin/fm-supervision-host.sh | 10 +- bin/fm-watch.sh | 6 +- docs/configuration.md | 8 +- docs/supervision-host.md | 69 ++++- docs/verification/supervision.md | 28 ++ docs/watcher-continuity.md | 3 + tests/fm-supervision-host-engine.test.sh | 212 +++++++++++++++ tests/fm-supervision-host-live-e2e.test.sh | 184 +++++++------ tests/fm-supervision-host-stop-hook.test.sh | 39 +++ tests/supervision-host-helpers.sh | 24 +- 14 files changed, 701 insertions(+), 137 deletions(-) diff --git a/.agents/skills/harness-adapters/references/harness/grok.md b/.agents/skills/harness-adapters/references/harness/grok.md index 8779fe49258..28da7e0be4a 100644 --- a/.agents/skills/harness-adapters/references/harness/grok.md +++ b/.agents/skills/harness-adapters/references/harness/grok.md @@ -90,5 +90,5 @@ The exact running Stop payload selects same-process continuation on 0.2.112; 0.2 Grok also loads Claude project settings, so Claude entries for Grok-covered events stand down under `GROK_AGENT` or `GROK_HOOK_EVENT`; that owner records the exact set and why `GROK_SESSION_ID` is excluded. Project-local hooks require launch-time `--trust`; without it the guard steps aside and `../../../bin/fm-guard.sh` is the next-command alarm. Watcher supervision remains tracked background notification around `../../../bin/fm-watch-arm.sh`, not Pi-style extension ownership. -In a home with `config/supervision-host` and no `config/supervision-host-off` the session-start block renders that background call as `../../../bin/fm-supervision-host.sh park`, with Claude's print mode as its headless engine; [`supervision-host.md`](../../../../../docs/supervision-host.md) owns the host. +In a home with `config/supervision-host` and no `config/supervision-host-off` the session-start block renders that background call as `../../../bin/fm-supervision-host.sh park`; [`supervision-host.md`](../../../../../docs/supervision-host.md) owns which headless engine that host runs. PreToolUse blocks directly, but every `$VAR` in a hook command needs inline `:-default` or Grok refuses the hook. diff --git a/bin/fm-lock.sh b/bin/fm-lock.sh index b0f009534b0..e2e939331a2 100755 --- a/bin/fm-lock.sh +++ b/bin/fm-lock.sh @@ -285,8 +285,9 @@ publish_lock_session() { return 0 } -# Record this session's harness, native profile, retained Claude profile and pane, -# bound to the pid now on line 1. Best effort by design: it is evidence for the +# Record this session's harness, native profile, retained Claude profile, Grok +# seat when this harness is Grok, and pane, bound to the pid now on line 1. +# Best effort by design: it is evidence for the # lead-restart path, never ownership, so a failure to write it must not refuse a # lock the session has already verified it owns. The pane is recorded only when # bin/fm-supervisor-target-lib.sh established one; its guessed fallback is left @@ -304,6 +305,9 @@ publish_lock_runtime() { printf 'harness=%s\n' "$harness" printf 'profile=%s\n' "$profile" printf 'claude_profile=%s\n' "${CLAUDE_CONFIG_DIR:-}" + # The Grok engine spends this seat. Other harnesses leave the key absent + # so a legacy record stays a Claude answer rather than an empty Grok one. + [ "$harness" != grok ] || printf 'grok_profile=%s\n' "${GROK_HOME:-}" [ -z "${FM_LEAD_LAUNCH_ARGV:-}" ] || printf 'launch_argv=%s\n' "$FM_LEAD_LAUNCH_ARGV" printf 'ambient=%s\n' "${FM_AMBIENT_CLAUDE_CONFIG_DIR-${CLAUDE_CONFIG_DIR:-}}" if target=$(discover_supervisor_target) && backend=$(discover_supervisor_backend); then diff --git a/bin/fm-session-lock-lib.sh b/bin/fm-session-lock-lib.sh index bb56fe64864..120ad467be5 100644 --- a/bin/fm-session-lock-lib.sh +++ b/bin/fm-session-lock-lib.sh @@ -465,6 +465,7 @@ fm_session_lock_handover_admits() { # [] # harness= # profile= # claude_profile= +# grok_profile= # launch_argv= # ambient= diff --git a/bin/fm-supervision-engine-lib.sh b/bin/fm-supervision-engine-lib.sh index 118268f5872..ce46a985eaf 100644 --- a/bin/fm-supervision-engine-lib.sh +++ b/bin/fm-supervision-engine-lib.sh @@ -24,10 +24,11 @@ # ONE ENGINE TURN (fm_supervision_engine_turn). One prompt to one engine # conversation, bounded, from the tracked code root, with the environment the # caller exported (the host exports the branch actor, the lease holder pid, -# the primary-harness pin, and the report-turn id) and on the Claude seat the -# lead itself is running on, which the caller resolves once per turn -# (fm_supervision_engine_seat) and passes in rather than letting the launch -# inherit it. The runner returns the +# the primary-harness pin, and the report-turn id) and on the seat the caller +# resolves once per turn (fm_supervision_engine_seat), passed in rather than +# inherited. A Claude turn launches on that Claude seat. A Grok turn spends +# that Grok seat's login from a private GROK_HOME under the home's state. +# The runner returns the # process exit status; the host separately requires a complete successful # result, a durable report, and acknowledgement before counting a wake handled. # The turn is bounded by fm_exec_timed @@ -43,14 +44,15 @@ # process group of its own. docs/supervision-host.md "Engines" owns the # verified engine facts each argument list below is built from. # -# Test seams: FM_SUPERVISION_ENGINE_CLAUDE_BIN names the claude executable -# (default: claude on PATH), so a hermetic test can run a stub engine through -# the real argument construction. FM_TEST_HARNESS pins the primary harness +# Test seams: FM_SUPERVISION_ENGINE_CLAUDE_BIN names the claude executable and +# FM_SUPERVISION_ENGINE_GROK_BIN the grok executable (default: that name on +# PATH), so a hermetic test can run a stub engine through the real argument +# construction. FM_TEST_HARNESS pins the primary harness # fm_supervision_host_primary reports when FM_TEST_SEAM=1 and its value is a # known harness token; otherwise detection remains real (tests/lib.sh arms # the marker for isolated suites). -FM_SUPERVISION_ENGINES_VERIFIED='claude' +FM_SUPERVISION_ENGINES_VERIFIED='claude grok' # fm_supervision_host_primary: print the primary harness the home gate judges # (bin/fm-harness.sh, whose supervision-branch pin names the primary inside an @@ -89,6 +91,7 @@ fm_supervision_engine_verified() { # fm_supervision_engine_default_model() { # case "$1" in claude) printf 'sonnet\n' ;; + grok) printf 'grok-4.7\n' ;; *) return 1 ;; esac } @@ -246,6 +249,10 @@ fm_supervision_engine_bin() { bin=${FM_SUPERVISION_ENGINE_CLAUDE_BIN:-} [ -n "$bin" ] || bin=$(command -v claude 2>/dev/null || true) ;; + grok) + bin=${FM_SUPERVISION_ENGINE_GROK_BIN:-} + [ -n "$bin" ] || bin=$(command -v grok 2>/dev/null || true) + ;; *) bin= ;; esac if [ -z "$bin" ] || [ ! -x "$bin" ]; then @@ -331,15 +338,20 @@ _fm_engine_reap() { } # fm_supervision_engine_seat -# The Claude seat the engine's NEXT launch must spend, read from the one -# lock-owner runtime record (bin/fm-session-lock-lib.sh, which must be sourced -# first). A Codex primary carries its retained Claude credentials separately in -# claude_profile; profile names its Codex seat and must never reach Claude. -# Other primaries and legacy records use profile. Sets -# FM_SUPERVISION_ENGINE_PROFILE to that profile -# directory, or to empty for the ambient default login, and returns 0. On no -# verdict it leaves the profile empty, sets -# FM_SUPERVISION_ENGINE_SEAT_PROBLEM to one plain sentence, and returns 1. +# The seat the engine's NEXT launch must spend, read from the one lock-owner +# runtime record (bin/fm-session-lock-lib.sh, which must be sourced first). +# Needs fm_supervision_host_config first, so FM_SUPERVISION_ENGINE is set. +# A Claude engine reads profile, except a Codex primary, which carries its +# retained Claude credentials separately in claude_profile; profile names its +# Codex seat and must never reach Claude. A Grok engine reads grok_profile +# when the primary is Grok, and spends the ambient Grok login when any other +# primary names grok. An empty grok_profile is that ambient login. A missing +# grok_profile key on a Grok primary is no verdict. The recorded profile is +# the seat directory, never the private GROK_HOME the turn launches under. +# Sets FM_SUPERVISION_ENGINE_PROFILE to that profile directory, or to empty +# for the ambient default login, and returns 0. On no verdict it leaves the +# profile empty, sets FM_SUPERVISION_ENGINE_SEAT_PROBLEM to one plain +# sentence, and returns 1. # # WHY THE RECORD AND NEVER THE ENVIRONMENT. A host process is started by a # primary's arm owner and can outlive the session that started it, including @@ -367,6 +379,22 @@ fm_supervision_engine_seat() { FM_SUPERVISION_ENGINE_PROFILE='' FM_SUPERVISION_ENGINE_SEAT_PROBLEM='' harness=$(fm_session_lock_runtime_field "$state_dir" harness) || harness=claude + if [ "${FM_SUPERVISION_ENGINE:-}" = grok ]; then + if [ "$harness" != grok ]; then + FM_SUPERVISION_ENGINE_PROFILE='' + return 0 + fi + if ! profile=$(fm_session_lock_runtime_field "$state_dir" grok_profile); then + FM_SUPERVISION_ENGINE_SEAT_PROBLEM="the Grok session has no grok_profile in its current lock-owner record, so the seat the engine must spend is unknown" + return 1 + fi + if [ -n "$profile" ] && [ ! -d "$profile" ]; then + FM_SUPERVISION_ENGINE_SEAT_PROBLEM="the Grok seat this session runs on has no profile directory at $profile" + return 1 + fi + FM_SUPERVISION_ENGINE_PROFILE=$profile + return 0 + fi [ "$harness" != codex ] || field=claude_profile if ! profile=$(fm_session_lock_runtime_field "$state_dir" "$field"); then if [ "$harness" = codex ]; then @@ -398,14 +426,93 @@ fm_supervision_engine_seat() { # (fm_supervision_engine_seat), empty for the ambient default login. A Claude # turn launches under exactly that profile, so the launch, the conversation # the caller chose for that seat, and the seat it recorded cannot disagree, -# and no launch can inherit the host process's CLAUDE_CONFIG_DIR; a Claude -# turn given no argument returns 127 rather than launching on -# whatever the host holds. +# and no launch can inherit the host process's CLAUDE_CONFIG_DIR. A Grok turn +# keeps that seat as the recorded profile and launches under a private +# GROK_HOME, linking the seat's auth.json rather than copying it. A turn given +# no argument returns 127 rather than launching on whatever the host +# holds. +# +# _fm_supervision_grok_prepare : make that private home and +# set FM_SUPERVISION_GROK_HOME and FM_SUPERVISION_GROK_LEADER_SOCK. A missing +# seat login returns 127 with no launch. The home has no hooks directory. +# shellcheck disable=SC2034 # Output globals, read by the sourcing caller. +_fm_supervision_grok_prepare() { + local profile=$1 errors=$2 auth ghome state_phys + FM_SUPERVISION_GROK_HOME='' + FM_SUPERVISION_GROK_LEADER_SOCK='' + state_phys=$(cd "$STATE" 2>/dev/null && pwd -P) || state_phys=$STATE + ghome="$state_phys/.supervision-host-grok" + if [ -L "$ghome" ]; then + printf 'the Grok engine home %s is a symlink\n' "$ghome" > "$errors" + return 127 + fi + mkdir -p "$ghome" 2>/dev/null || { + printf 'the Grok engine home could not be created\n' > "$errors" + return 127 + } + if [ -e "$ghome/hooks" ] || [ -L "$ghome/hooks" ]; then + rm -rf "$ghome/hooks" 2>/dev/null || { + printf 'the Grok engine home still has a hooks directory\n' > "$errors" + return 127 + } + fi + if [ -n "$profile" ]; then + auth="$profile/auth.json" + else + auth="${HOME:-}/.grok/auth.json" + fi + if [ ! -f "$auth" ]; then + printf 'the Grok seat has no auth.json at %s, so the engine was not launched\n' "$auth" > "$errors" + return 127 + fi + ln -sfn "$auth" "$ghome/auth.json" || { + printf 'the Grok engine home could not link its seat login\n' > "$errors" + return 127 + } + if ! cat > "$ghome/config.toml" <<'EOF' +[skills] +ignore = ["~/.agents", "~/.grok/skills", "~/.grok/commands", "~/.claude", "~/.cursor", ".agents", ".grok", ".claude", ".cursor"] + +[compat.claude] +hooks = false +skills = false +rules = false +agents = false +mcps = false +sessions = false + +[compat.cursor] +hooks = false +skills = false +rules = false +agents = false +mcps = false +sessions = false + +[compat.codex] +hooks = false +skills = false +rules = false +agents = false +mcps = false +sessions = false +EOF + then + printf 'the Grok engine home could not be configured\n' > "$errors" + return 127 + fi + rm -f "$state_phys/.supervision-host-grok-leader.sock" \ + "$state_phys/.supervision-host-grok-leader.sock.daemon" 2>/dev/null || true + FM_SUPERVISION_GROK_HOME=$ghome + FM_SUPERVISION_GROK_LEADER_SOCK=$state_phys/.supervision-host-grok-leader.sock + return 0 +} + fm_supervision_engine_turn() { local engine=$1 model=$2 prompt=$3 message=$4 session=$5 mode=$6 timeout=$7 result=$8 errors=$9 local pid_file=${11:-} bin grace i ledger watched rc home_phys root_phys state_phys identity recorded - local profile='' profile_set=0 - local -a args + local profile='' profile_set=0 prompt_text='' grok_launch=0 + local -a args launch=() bin=$(fm_supervision_engine_bin "$engine" 2>"$errors") || return 127 case "$timeout" in ''|0*|*[!0-9]*) timeout=1200 ;; esac grace=${FM_SUPERVISION_ENGINE_GRACE:-30} @@ -440,6 +547,51 @@ fm_supervision_engine_turn() { profile=${10} profile_set=1 ;; + grok) + # Grok's own headless contract, measured on grok 1.0.77. Do not copy + # Claude's flags: --prompt-file is the prompt by itself, permissions are + # --always-approve with a tool list, and the system prompt is the branch + # prompt text. docs/supervision-host.md "Engines" owns the measured facts. + if [ -z "${10+set}" ]; then + printf 'no Grok seat was passed for this engine turn\n' > "$errors" + return 127 + fi + profile=${10} + _fm_supervision_grok_prepare "$profile" "$errors" || return 127 + if [ ! -r "$prompt" ]; then + printf 'the branch prompt could not be read\n' > "$errors" + return 127 + fi + prompt_text=$(cat "$prompt") || { + printf 'the branch prompt could not be read\n' > "$errors" + return 127 + } + # shellcheck disable=SC2054 # The comma-separated tool lists are single values. + args=(--prompt-file "$message" --system-prompt-override "$prompt_text" --verbatim + --always-approve --output-format json --model "$model" + --tools run_terminal_cmd,read_file + --disallowed-tools Agent,web_search,web_fetch + --deny MCPTool --no-subagents --disable-web-search --no-auto-update --no-leader + --leader-socket "$FM_SUPERVISION_GROK_LEADER_SOCK") + if [ "$mode" = new ]; then + args+=(--session-id "$session") + else + args+=(--resume "$session") + fi + # Keep the caller's actor and the rest of the host's exports. Drop only + # the Claude seat, which is not this engine's login. + launch=(env -u CLAUDE_CONFIG_DIR "GROK_HOME=$FM_SUPERVISION_GROK_HOME" + GROK_CLAUDE_HOOKS_ENABLED=false GROK_CLAUDE_SKILLS_ENABLED=false + GROK_CLAUDE_RULES_ENABLED=false GROK_CLAUDE_AGENTS_ENABLED=false + GROK_CLAUDE_MCPS_ENABLED=false GROK_CLAUDE_SESSIONS_ENABLED=false + GROK_CURSOR_HOOKS_ENABLED=false GROK_CURSOR_SKILLS_ENABLED=false + GROK_CURSOR_RULES_ENABLED=false GROK_CURSOR_AGENTS_ENABLED=false + GROK_CURSOR_MCPS_ENABLED=false GROK_CURSOR_SESSIONS_ENABLED=false + GROK_CODEX_HOOKS_ENABLED=false GROK_CODEX_SKILLS_ENABLED=false + GROK_CODEX_RULES_ENABLED=false GROK_CODEX_AGENTS_ENABLED=false + GROK_CODEX_MCPS_ENABLED=false GROK_CODEX_SESSIONS_ENABLED=false) + grok_launch=1 + ;; *) printf 'no engine turn is defined for %s\n' "$engine" > "$errors" return 127 @@ -449,9 +601,9 @@ fm_supervision_engine_turn() { # to this launch alone and nothing in this shell's environment is rewritten: # an assignment here would also make CLAUDE_CONFIG_DIR a variable ShellCheck # sees modified in a subshell, which it then reports against every other - # reader of that name across the tracked tree. - local -a launch=() - if [ "$profile_set" -eq 1 ]; then + # reader of that name across the tracked tree. The Grok branch fills launch + # itself, with GROK_HOME rather than CLAUDE_CONFIG_DIR. + if [ "$grok_launch" -eq 0 ] && [ "$profile_set" -eq 1 ]; then if [ -n "$profile" ]; then launch=(env "CLAUDE_CONFIG_DIR=$profile") else @@ -500,11 +652,16 @@ fm_supervision_engine_turn() { # machine-readable result, where cost is this turn's and conversation_cost the # conversation's running total (the caller records it and passes it back for # the next turn; 0 for a new conversation). Claude's total_cost_usd is that -# running total on a resumed conversation, while its usage and num_turns are -# per turn. error=0 only for a complete success result: type "result", -# subtype "success", is_error false, and finite total_cost_usd, num_turns, and -# the four usage token counts; any other shape is error=1. Returns 1 when the -# result cannot be read. The host treats both as a failed turn. +# running total on a resumed conversation, so this subtracts the prior total, +# while its usage and num_turns are per turn. Grok's total_cost_usd and +# num_turns are per run, so this adds the prior total. A missing Grok cost is +# unreported, never zero. error=0 only for a complete success. For Claude that +# is type "result", subtype "success", is_error false, and finite +# total_cost_usd, num_turns, and the four usage token counts. For Grok that is +# stopReason "end_turn", a non-empty sessionId, type other than "error", +# neither usage_is_incomplete nor cost_is_partial, and those same finite +# numbers. Any other shape is error=1. Returns 1 when the result cannot be +# read. The host treats both as a failed turn. fm_supervision_engine_result() { case "$1" in claude) @@ -528,6 +685,33 @@ fm_supervision_engine_result() { process.stdout.write(`error=${error} cost=${usd(turn)} conversation_cost=${usd(total)} input=${n(u.input_tokens)} cache_read=${n(u.cache_read_input_tokens)} cache_write=${n(u.cache_creation_input_tokens)} output=${n(u.output_tokens)} turns=${n(j.num_turns)}\n`); ' "$2" "${3:-0}" 2>/dev/null ;; + grok) + # shellcheck disable=SC2016 # A literal Node program; ${...} is JavaScript. + node -e ' + const fs = require("node:fs"); + let j; + try { j = JSON.parse(fs.readFileSync(process.argv[1], "utf8")); } catch { process.exit(1); } + if (!j || typeof j !== "object" || Array.isArray(j)) process.exit(1); + const u = j.usage && typeof j.usage === "object" && !Array.isArray(j.usage) ? j.usage : {}; + const finite = (v) => typeof v === "number" && Number.isFinite(v); + const n = (v) => (finite(v) ? v : 0); + const flagged = (v) => v === true; + const complete = j.type !== "error" && j.stopReason === "end_turn" + && typeof j.sessionId === "string" && j.sessionId.length > 0 + && !flagged(j.usage_is_incomplete) && !flagged(u.usage_is_incomplete) + && !flagged(j.cost_is_partial) && !flagged(u.cost_is_partial) + && finite(j.total_cost_usd) && finite(j.num_turns) && finite(u.input_tokens) + && finite(u.cache_read_input_tokens) && finite(u.cache_creation_input_tokens) && finite(u.output_tokens); + const error = complete ? 0 : 1; + const prior = Number(process.argv[2]); + const usd = (v) => Number(v.toFixed(6)); + const cost = finite(j.total_cost_usd) ? usd(j.total_cost_usd) : "unreported"; + const total = finite(j.total_cost_usd) + ? usd(Number.isFinite(prior) && prior >= 0 ? prior + j.total_cost_usd : j.total_cost_usd) + : "unreported"; + process.stdout.write(`error=${error} cost=${cost} conversation_cost=${total} input=${n(u.input_tokens)} cache_read=${n(u.cache_read_input_tokens)} cache_write=${n(u.cache_creation_input_tokens)} output=${n(u.output_tokens)} turns=${n(j.num_turns)}\n`); + ' "$2" "${3:-0}" 2>/dev/null + ;; *) return 1 ;; esac } diff --git a/bin/fm-supervision-host.sh b/bin/fm-supervision-host.sh index ee6f7d96dbd..59fefa34af5 100755 --- a/bin/fm-supervision-host.sh +++ b/bin/fm-supervision-host.sh @@ -64,7 +64,10 @@ # (detach_successor owns the persistence check and failure path). The # session's next park without --restart requests a take-over of its cycle # rather than an ordinary attach; bin/fm-watch-arm.sh's --take-over header owns the -# conditions under which that restores a single owner and the fallback; +# conditions under which that restores a single owner and the fallback. +# That take-over sets FM_WATCH_KEEP_ANNOUNCED so an episode already +# announced is not reopened into another main hand-back +# (docs/watcher-continuity.md "Generation reuse"). # - away (an away record exists): every close goes to the engine. # Every turn that starts attended meets that rule again at its start, so a # close accepted away whose turn starts attended (the captain returned in @@ -1137,7 +1140,10 @@ if [ "$FIRST_ARM_RESTART" -eq 1 ]; then start_arm "$OWNER_PREDECESSOR" --restart elif [ -n "$LEFT_ARM" ]; then log_line "take-over arm=$LEFT_ARM" - start_arm "$OWNER_PREDECESSOR" --take-over "$LEFT_ARM" + # Keep an already-announced recovery announced. Reopening it minted a fresh + # generation for the same queued work, and that generation's resurface is + # attended main-only, so every following turn end handed it straight back. + FM_WATCH_KEEP_ANNOUNCED=1 start_arm "$OWNER_PREDECESSOR" --take-over "$LEFT_ARM" else start_arm "$OWNER_PREDECESSOR" fi || { echo "watcher: FAILED - the supervision host could not start a watcher cycle"; exit 1; } diff --git a/bin/fm-watch.sh b/bin/fm-watch.sh index 7917e77b4db..263944e0dc8 100755 --- a/bin/fm-watch.sh +++ b/bin/fm-watch.sh @@ -3050,8 +3050,12 @@ WATCHER_RECOVERY_PENDING=0 if [ -n "${FM_LOCK_RECOVERED_PID:-}" ]; then WATCHER_RECOVERY_PENDING=1 fi +# FM_WATCH_KEEP_ANNOUNCED is the supervision host's take-over (docs/watcher-continuity.md +# "Generation reuse"): an episode already announced stays announced, so the +# fresh cycle does not mint another recovery for the same queued work. if [ "${FM_WATCH_HANDLING_SUCCESSOR:-0}" != 1 ] \ - && [ "${FM_WATCH_FOREGROUND_CHECKPOINT:-0}" != 1 ]; then + && [ "${FM_WATCH_FOREGROUND_CHECKPOINT:-0}" != 1 ] \ + && [ "${FM_WATCH_KEEP_ANNOUNCED:-0}" != 1 ]; then if ! fm_recovery_marker_reopen_announced "$WATCHER_DOWNTIME_MARKER"; then echo "watcher: recovery state could not be reopened safely; retaining stale lock evidence" >&2 exit 1 diff --git a/docs/configuration.md b/docs/configuration.md index 268dbd9bb39..6eac8400b9e 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -322,10 +322,12 @@ Grok's arm command is rendered at session start, so a change to its host mode ta `config/supervision-host` may be empty or hold one line ` []`: -- empty or `default` selects the primary harness's own engine at that engine's default model (`sonnet` for the Claude engine); -- ` []` names a verified engine, currently only `claude`, and optionally the engine's own model name or alias; `default ` selects the primary harness's engine with that model. +- empty or `default` selects the primary harness's own engine at that engine's default model (`sonnet` for Claude, `grok-4.7` for Grok); +- ` []` names a verified engine, `claude` or `grok`, and optionally the engine's own model name or alias; `default ` selects the primary harness's engine with that model. -Only Claude has a verified engine of its own, so a Cursor, OpenCode, omp, Grok, or Codex home names `claude` in the file. +A Cursor, OpenCode, omp, or Codex home names `claude` in the file, because those primaries have no verified engine of their own. +A Grok home may name `grok` or `claude`. +[supervision-host.md](supervision-host.md#engines) owns the verified-engine contract. ### Failures, when changes apply, and inheritance diff --git a/docs/supervision-host.md b/docs/supervision-host.md index c1eb526a081..1bb8300da24 100644 --- a/docs/supervision-host.md +++ b/docs/supervision-host.md @@ -112,6 +112,7 @@ On that main-only pass-through the host starts the successor watcher cycle and l That successor, like one a turn hands back at its start (see [Away](#away)), runs in a process group of its own, so the harness tearing down the hook's group after the rewake does not stop it. The host leaves the watcher's recovery marker reading downtime, confirming no handling handoff, because the re-arm owner delivers a close to main only while that marker reads downtime. The session's next park without `--restart` requests a take-over to restore a single host-owned arm; the [host header](../bin/fm-supervision-host.sh) owns successor persistence and cleanup, and the [arm header](../bin/fm-watch-arm.sh) owns take-over eligibility and fallback. +That take-over keeps an already-announced recovery from being handed straight back to main; [Generation reuse](watcher-continuity.md#generation-reuse) owns the hold. OpenCode and omp still launch the host with `--restart`, which takes precedence over recorded take-over and lacks its acknowledgement-preserving handover; changing that first-cycle path remains a follow-up. The host-off Claude Stop hook's detached handling successor is also unchanged; see [Claude handling successor](watcher-continuity.md#claude-handling-successor). It also passes the close through unchanged, with no added line, when any of these holds (`fm_supervision_host_attended_ready` in `bin/fm-supervision-engine-lib.sh` owns the list): @@ -326,9 +327,10 @@ A captain message typed during the park waits for the checkpoint to return, at m ## The engine's seat -The engine spends a Claude account, so which seat it launches on is resolved before every turn and never inherited from the host process. +The engine spends an account, so which seat it launches on is resolved before every turn and never inherited from the host process. The seat is read from the runtime record beside the home's session lock, which is bound to the lock's current owner. -A Claude primary supplies its own seat; a Codex primary supplies the Claude credentials retained for the engine separately from its Codex seat. +A Claude engine reads that session's Claude seat, and a Codex primary supplies the Claude credentials retained for that engine separately from its Codex seat. +A Grok engine reads `grok_profile` when the primary is Grok, and the ambient Grok login when another primary names `grok`. `fm_supervision_engine_seat` in `bin/fm-supervision-engine-lib.sh` owns that distinction and its legacy-record handling. `config/claude-seat` is not that record, because it names the seat new workers get and moves without the lead. @@ -336,7 +338,9 @@ This matters because a host process outlives the turn that started it. When the lead moves onto another Claude seat ([claude-seats.md](claude-seats.md#moving-firstmate-itself)), the successor session records its own seat beside the lock, and the host picks it up at its next engine launch; no host restart is needed, and a record left by the lead that has already gone names a pid the lock no longer owns, so it is no verdict rather than a stale answer. A move to Codex retains the recorded Claude engine account; moving the leader does not move the engine's spending to Codex. -A launch on a seat other than the one the recorded conversation was opened on starts a new conversation, because a conversation lives under the profile directory of the seat it was opened on. +A launch on a seat other than the one the recorded conversation was opened on starts a new conversation. +A Claude conversation lives under the profile directory of the seat it was opened on. +A Grok conversation records that same seat, and the resume itself uses the engine's private home. The host hands the wake to main, with the reason on its `supervision-host:` line, when that record names no current lock owner, lacks the retained Claude account for a Codex primary, or names a profile directory that is not there. No login is probed: a Claude primary demonstrates its own usable login, while retained credentials beside a Codex primary are only an account selection. @@ -368,9 +372,10 @@ Each engine turn's line carries these fields, and this log is where engine cost ## Engines A verified engine is a headless mode of a harness whose isolation, actor propagation, promptless permissions, bounding, and caching were measured. -Today the only verified engine is Claude's print mode, measured on Claude Code 2.1.278 and 2.1.281. -The engine stays on Claude when the primary moves to Codex because `bin/fm-supervision-engine-lib.sh` implements only Claude's launch and result contracts, including hook isolation, promptless tool permissions, conversation reuse, and usage accounting. -There is no architectural requirement that the engine use Claude; changing it requires implementing and verifying another engine adapter. +The verified engines are Claude's print mode, measured on Claude Code 2.1.278 and 2.1.281, and Grok's headless mode, measured on grok 1.0.77. +A Codex primary keeps the Claude engine when `config/supervision-host` names `claude`, using the Claude account that move retains. +There is no architectural requirement that the engine be one of these two. +Changing it requires implementing and verifying another engine adapter. ### Claude print mode behavior @@ -408,8 +413,46 @@ There is no architectural requirement that the engine use Claude; changing it re - `total_cost_usd`, `num_turns`, and the four `usage` token counts (input, cache read, cache creation, output) are finite numbers. - Any other result fails the turn and hands its wake to main. -**Tool process reaping** +### Grok headless mode +Measured on grok 1.0.77. +`bin/fm-supervision-engine-lib.sh` owns the exact argument list. +The flags below are Grok's own headless contract, not Claude's. + +**Isolation** + +The turn launches with `GROK_HOME` set to `state/.supervision-host-grok`. +That directory has no hooks directory. +Its `config.toml` ignores the usual skill roots and turns off Claude, Cursor, and Codex compat hooks, skills, rules, agents, MCP, and sessions. +The launch environment clears those same cells. +The seat's `auth.json` is linked, not copied, because a copied refresh token can be single-use. +The launch does not pass `--trust` and does not set `GROK_FOLDER_TRUST`. + +**Permissions** + +`--always-approve` with `--tools run_terminal_cmd,read_file` is the unattended mode. +`--permission-mode dontAsk` is not that mode: a measured turn refused a command and ended `cancelled` while still reporting a cost. +`--deny MCPTool`, `--disallowed-tools Agent,web_search,web_fetch`, `--no-subagents`, and `--disable-web-search` keep the turn off those tools. + +**Conversation and input** + +`--prompt-file` is the wake prompt and is not combined with a positional prompt. +`--system-prompt-override` carries the branch prompt text. +`--verbatim` sends that wake prompt unchanged. +A new conversation passes `--session-id` and a later turn passes `--resume`. +Stdin is `/dev/null`. + +**Result and cost** + +`--output-format json` carries `stopReason`, `sessionId`, `num_turns`, `total_cost_usd`, and the four usage counts. +Those cost and turn figures are per run, including on resume, so the engine lib adds the prior conversation cost. +A missing `total_cost_usd` stays unreported and is never treated as zero. +The host counts a turn successful only when `stopReason` is `end_turn`, `sessionId` is non-empty, `type` is not `error`, `usage_is_incomplete` and `cost_is_partial` are not true, and the cost, turn count, and four usage counts are finite numbers. +Any other result, including `cancelled` and `max_tokens`, fails the turn and hands its wake to main. + +### Tool process reaping + +Every engine turn uses the same reap. Tool commands run in process groups of their own, which a bound's group signal cannot reach. The engine lib records the engine's descendants while it runs and reaps them by recorded identity after every turn; its [header](../bin/fm-supervision-engine-lib.sh) owns the snapshot cadence. The reap is best-effort for what it observed, not a bound. @@ -418,12 +461,14 @@ Such a process is never recorded and survives the turn, the same residual `bin/f ### Model and engine selection -The default model is `sonnet`, which handled every measured wake correctly at a fraction of a larger model's cost. +The default model is `sonnet` for Claude, which handled every measured wake correctly at a fraction of a larger model's cost, and `grok-4.7` for Grok. `config/supervision-host` can name another. -The Claude engine runs beside any of the six primaries, but only a Claude primary selects it by default when the host is enabled, even with no file. -A Cursor, OpenCode, omp, Grok, or Codex home names it (`claude`, optionally with a model) in `config/supervision-host`. -`/afk` there says so when the file selects no engine. +Only a Claude primary runs the host when `config/supervision-host` is absent. +An empty or `default` file selects the primary's own verified engine, so a Grok primary then uses `grok-4.7`. +A Cursor, OpenCode, omp, or Codex home names `claude` (optionally with a model) until that primary has a verified engine of its own. +A Grok home may name `grok` or `claude`. +`/afk` says so when the file selects no engine. ## Verification @@ -441,7 +486,7 @@ Each arm owner's own suite covers its host mode against a stub host. | `tests/fm-host-mirror.test.sh` | The dialog mirror's writers through the tracked Claude and Cursor registrations, the home gate, the feed, and the verified-writer list. | | `tests/fm-afk-launch.test.sh` | The home gate on each primary, the `/afk` daemon refusal, and `/quiet` on a home that runs the host: the statement, the paused statement, each named missing part, the quiet daemon fallback that carries its recorded mode, a failed quiet start that archives its quiet record, and the refusal under a live away record until the return. | | `tests/fm-afk-return.test.sh` | The return's drain-owned read-cursor advance through the away window on a host home, and none on Pi. | -| `tests/fm-supervision-host-live-e2e.test.sh` | Runs a real engine turn; opt-in because it spends tokens. | +| `tests/fm-supervision-host-live-e2e.test.sh` | Runs a real Claude turn and a real Grok turn; opt-in because it spends tokens. | | `tests/fm-supervision-host-attended-live-e2e.test.sh` | Opt-in credentialed guard for repeated attended main-only hand-backs to an idle Claude primary, the successor's own close, a close that turns main-only at its turn, and a stand-in remote listener; accepts a pre-fix ref for a negative control. | | `tests/fm-host-mirror-live-e2e.test.sh` | Proves the Claude and Cursor mirror writers against the real harnesses; opt-in because it spends tokens. | diff --git a/docs/verification/supervision.md b/docs/verification/supervision.md index e904601eebe..3d4d2139662 100644 --- a/docs/verification/supervision.md +++ b/docs/verification/supervision.md @@ -717,6 +717,33 @@ Claude's `--output-format json` reports `total_cost_usd` as the resumed conversa Five consecutive turns of one conversation, a host restart between the second and third, reported totals of 0.2093, 0.3441, 0.4234, 0.4870, and 0.5408 with per-turn `cache_read_input_tokens` of 423687, 359255, 245302, 174613, and 185598. Each handled away wake cost between $0.05 and $0.21 on `sonnet`. +The same guard now runs one turn pair per verified engine, and `FM_SUPERVISION_HOST_LIVE_ENGINE` names a single engine. +On 2026-11-06, on Linux 7.0.0-38-generic aarch64, grok 1.0.77 (`50ca579fa996`) with model `grok-4.7` handled and resumed an away wake: + +```text +$ FM_SUPERVISION_HOST_LIVE_E2E=1 FM_SUPERVISION_HOST_LIVE_ENGINE=grok FM_SUPERVISION_HOST_LIVE_POLLS=9000 tests/fm-supervision-host-live-e2e.test.sh +# grok first turn: handled turn=host-160530-1793965695.1 posture=away rc=0 +# grok second turn: handled turn=host-160530-1793965695.2 posture=away rc=0 +ok - supervision host live (grok 1.0.77 (50ca579fa996)): a real grok engine handles and resumes away wakes under the branch contract without waking main +``` + +The first handled turn recorded `error=0 cost=0.317848 conversation_cost=0.317848 input=92781 cache_read=172544 cache_write=0 output=7669 turns=11`. +That cost is the run's own `total_cost_usd`, and the cache read is the turn's own, which is the Grok contract in [supervision-host.md](../supervision-host.md#grok-headless-mode). +The guard then required the second turn to resume that same conversation and leave the host's turn count at 2. + +### OpenCode free provider + +OpenCode 2.2.5's free provider is not a verified supervision-host engine. +`opencode models` listed these `opencode/` models on 2026-11-06: `opencode/beam-free`, `opencode/big-pickle`, `opencode/crouton-alpha-free`, `opencode/deepseek-v4.1-flash-free`, `opencode/flint-40b-free`, `opencode/glm-5.3-flash-free`, `opencode/mimo-v2.6-flash-free`, `opencode/nemotron-3.5-lightning-free`, `opencode/nemotron-3.5-super-vl-free`, and `opencode/pulse-alpha-free`. +They share one headless process, `opencode run --standalone --format json`, which prints JSONL events rather than one result object. +One turn of `opencode/glm-5.3-flash-free` finished with `reason=stop`, `cost=0`, and token counts `input=8930`, `output=4`, `reasoning=10`, `cache.read=0`, `cache.write=0`. +A reported cost of 0 on a turn that did spend tokens is not a cost the host can trust, because the same field would look identical if the provider omitted a price. +Private `XDG_CONFIG_HOME`, `XDG_DATA_HOME`, `XDG_STATE_HOME`, and `XDG_CACHE_HOME` relocate config, data, state, cache, the database, and the log. +`opencode debug paths tmp` still prints `/tmp/opencode`. +The standalone turn also watches `~/.opencode` beside the private config directory, so the user's OpenCode home is part of the turn. +Without `--standalone`, a config read attaches to the user's managed service and stops because port 49374 is already in use. +No free-provider model meets the isolation and trusted-cost bar, so this record adds no OpenCode engine. + Before the Claude default-on flip, without `config/supervision-host`, the same live sessions and guards ran on the tree before the host (`ac2ed3b2`) and with it, with identical results: | Check | Before | After | @@ -748,6 +775,7 @@ tests/fm-watch-arm.test.sh ### Non-Pi primaries This supports the per-primary routing in [supervision-host.md](../supervision-host.md): with `config/supervision-host`, the Cursor, OpenCode, Grok, and Codex arm owners run the host with the Claude engine, and without it nothing changes. +A Grok primary can now select that verified Grok engine, which this routing check did not exercise. It was measured on 2026-09-24 on macOS 26.6.2 arm64 with Claude Code 2.1.281 as the engine (`sonnet`), codex-cli 0.155.1, cursor-agent 2026.09.23-86fc751, OpenCode 1.18.32, grok 1.0.41, and Claude Code 2.1.281 as primaries, and Pi 0.87.0 workers on `openai-codex/gpt-5.6-sol`, in disposable lab homes on private tmux sockets. omp is not installed on the measuring machine, so its routing rests on `tests/fm-omp-harness.test.sh` alone. OpenCode's OpenAI login on that machine rejects `gpt-5.6-sol` and has exhausted its `gpt-5.5` usage, so the OpenCode primary ran on OpenCode's free `opencode/big-pickle`. diff --git a/docs/watcher-continuity.md b/docs/watcher-continuity.md index d561624b414..821fcb25295 100644 --- a/docs/watcher-continuity.md +++ b/docs/watcher-continuity.md @@ -245,6 +245,9 @@ A watcher close leaves an announced downtime episode announced, while a successf An announced handling episode becomes pending downtime on the same generation because its handling turn may have been interrupted. That handling republication gives a successor exactly one recovery presentation without orphaning the acknowledgement already printed for that generation. A watcher stopped so an arm can take its cycle over (`bin/fm-watch-arm.sh --take-over`) publishes downtime like any close, but the taking arm restores an acknowledged episode that stop reopened only when the taken-over arm's cycle-ledger row for that exact arm and watcher records the watcher ending by the take-over's TERM and no wake was appended in between. +The supervision host's take-over also sets `FM_WATCH_KEEP_ANNOUNCED`, and the fresh cycle then leaves an already-announced episode announced instead of minting another generation for the same queued work. +A pending episode with queued work still surfaces once through the ordinary arm check. +Without that hold, the fresh cycle's `check: rearm-resurface` is an attended main-only close, the host hands it straight back, and the next turn end takes over and announces again. The taking arm waits within a short bound for that row; a missing row or any other signal leaves downtime for the fresh cycle's ordinary recovery wake, while take-over still proceeds. Any other episode is left for the next cycle's arm check. diff --git a/tests/fm-supervision-host-engine.test.sh b/tests/fm-supervision-host-engine.test.sh index bdfdca9e306..407e0dca12f 100755 --- a/tests/fm-supervision-host-engine.test.sh +++ b/tests/fm-supervision-host-engine.test.sh @@ -460,6 +460,212 @@ test_restarted_host_stops_what_a_killed_predecessor_left() { pass "host: a restarted host stops, by recorded identity, the cycle a killed predecessor left running" } +# A Grok primary's seat, recorded the way bin/fm-lock.sh writes grok_profile, +# with a dummy login so the turn does not depend on this machine's real one. +arm_grok_seat() { # + local home=$1 seat=$2 lock_pid + mkdir -p "$seat" + printf '{}\n' > "$seat/auth.json" + lock_pid=$(cat "$home/state/.lock") + printf 'pid=%s\nharness=grok\nprofile=%s\ngrok_profile=%s\n' \ + "$lock_pid" "$home/seats/seat-a" "$seat" > "$home/state/.lock-runtime" +} + +test_grok_away_wake_is_handled_and_resumed_on_its_own_contract() { + local home seat first second session ghome saved + home=$(make_home grok-away away grok) + seat="$home/seats/grok" + saved=$FM_SUPERVISION_HOST_PRIMARY + FM_SUPERVISION_HOST_PRIMARY=grok start_host "$home" + FM_SUPERVISION_HOST_PRIMARY=$saved + wait_until 150 watcher_live "$home" || fail "grok away: the host never started a watcher cycle: $(cat "$home/host.out")" + arm_grok_seat "$home" "$seat" + append_status "$home" 'step one' + wait_until 250 handled_at_least "$home" 1 \ + || fail "grok away: the wake was not handled: $(cat "$home/state/.supervision-host.log"; cat "$home/host.out")" + first="$home/engine-call.1" + assert_re '^actor=branch$' "$first" "the Grok engine must run as the branch actor" + assert_re '^primary=grok$' "$first" "the Grok engine must carry the primary-harness pin" + assert_re '^profile=$' "$first" "the Grok engine must not inherit the Claude seat" + assert_re '^arg=--always-approve$' "$first" "the Grok engine must auto-approve tools" + assert_re '^arg=--prompt-file$' "$first" "the Grok engine must take its prompt from a file" + assert_re '^arg=--verbatim$' "$first" "the Grok engine must send the wake prompt verbatim" + assert_re '^arg=grok-4\.7$' "$first" "the Grok engine must default to grok-4.7" + assert_re '^arg=--session-id$' "$first" "the first Grok turn must open a new conversation" + assert_re '^arg=--no-subagents$' "$first" "the Grok engine must not spawn subagents" + assert_re '^arg=--disable-web-search$' "$first" "the Grok engine must not search the web" + assert_no_re '^arg=--safe-mode$' "$first" "the Grok engine must not be launched with Claude's safe mode" + assert_no_re '^arg=dontAsk$' "$first" "the Grok engine must not be launched with Claude's permission mode" + assert_no_re '^arg=-p$' "$first" "the Grok engine must not take a positional prompt beside --prompt-file" + assert_no_re '^arg=--trust$' "$first" "the Grok engine must not be told to trust the directory" + ghome=$(sed -n 's/^grok_home=//p' "$first") + [ "$ghome" = "$(cd "$home/state" && pwd -P)/.supervision-host-grok" ] \ + || fail "the Grok engine must launch from its private home under state, got: ${ghome:-empty}" + [ ! -e "$ghome/hooks" ] || fail "the Grok engine home must not have a hooks directory" + [ "$(readlink "$ghome/auth.json")" = "$seat/auth.json" ] \ + || fail "the Grok engine must link the seat login rather than copy it: $(readlink "$ghome/auth.json" 2>/dev/null)" + assert_grep 'hooks = false' "$ghome/config.toml" "the Grok engine home must disable compat hooks" + assert_grep "profile=$seat" "$home/state/.supervision-host-engine" \ + "the conversation must record the Grok seat, not the private home" + [ ! -s "$home/host.rc" ] || fail "a handled Grok away wake reached main: $(cat "$home/host.out")" + + append_status "$home" 'step two' + wait_until 250 handled_at_least "$home" 2 \ + || fail "grok away: the second wake was not handled: $(cat "$home/state/.supervision-host.log")" + session=$(sed -n '/^arg=--session-id$/{n;s/^arg=//p;}' "$first") + second="$home/engine-call.2" + assert_re '^arg=--resume$' "$second" "a later Grok turn must resume the conversation" + assert_re "^arg=$session\$" "$second" "a later Grok turn must resume the same conversation" + assert_no_re '^arg=--session-id$' "$second" "a resumed Grok turn must not also open a session" + assert_re ' handled turn=[^ ]*\.2 .* cost=0\.25 conversation_cost=0\.5 ' "$home/state/.supervision-host.log" \ + "a resumed Grok turn must add its own cost to the conversation" + pass "host: a Grok away wake is handled and resumed on Grok's own headless contract" +} + +test_grok_engine_without_a_recorded_seat_hands_the_wake_to_main() { + local home lock_pid saved + home=$(make_home grok-noseat away grok) + saved=$FM_SUPERVISION_HOST_PRIMARY + FM_SUPERVISION_HOST_PRIMARY=grok start_host "$home" + FM_SUPERVISION_HOST_PRIMARY=$saved + wait_until 150 watcher_live "$home" || fail "grok noseat: the host never started a watcher cycle" + lock_pid=$(cat "$home/state/.lock") + printf 'pid=%s\nharness=grok\nprofile=%s\n' "$lock_pid" "$home/seats/seat-a" > "$home/state/.lock-runtime" + append_status "$home" 'needs a seat' + wait_until 250 host_exited "$home" || fail "grok noseat: the host did not hand the wake to main: $(cat "$home/state/.supervision-host.log")" + assert_grep 'no grok_profile' "$home/host.out" "the handback must name the missing Grok seat" + [ "$(engine_calls "$home")" -eq 0 ] || fail "the Grok engine launched without a recorded seat" + assert_grep 'demo.status' "$home/state/.wake-queue" "the unhandled wake must stay durable for main" + pass "host: a Grok primary with no recorded seat hands the wake to main" +} + +test_grok_engine_without_a_seat_login_does_not_launch() { + local home seat saved lock_pid + home=$(make_home grok-nologin away grok) + seat="$home/seats/grok-empty" + mkdir -p "$seat" + saved=$FM_SUPERVISION_HOST_PRIMARY + FM_SUPERVISION_HOST_PRIMARY=grok start_host "$home" + FM_SUPERVISION_HOST_PRIMARY=$saved + wait_until 150 watcher_live "$home" || fail "grok nologin: the host never started a watcher cycle" + lock_pid=$(cat "$home/state/.lock") + printf 'pid=%s\nharness=grok\nprofile=\ngrok_profile=%s\n' "$lock_pid" "$seat" > "$home/state/.lock-runtime" + append_status "$home" 'needs a login' + wait_until 250 host_exited "$home" \ + || fail "grok nologin: the host launched or hung: $(cat "$home/state/.supervision-host.log")" + assert_grep 'no auth.json' "$home/state/.supervision-host.log" "the failed turn must name the missing login" + assert_grep 'could not run' "$home/host.out" "the handback must say the engine could not run" + [ "$(engine_calls "$home")" -eq 0 ] || fail "the Grok engine launched without a seat login" + pass "host: a Grok seat with no login is not launched" +} + +test_grok_result_adds_each_runs_cost_and_rejects_an_incomplete_one() { + local home dir out rc + home=$(make_home grok-result away grok) + dir="$home/results" + mkdir -p "$dir" + printf '%s\n' '{"stopReason":"end_turn","sessionId":"s","num_turns":1,"total_cost_usd":0.25,"usage":{"input_tokens":5,"cache_read_input_tokens":100,"cache_creation_input_tokens":10,"output_tokens":20}}' > "$dir/run.json" + printf '%s\n' '{"type":"result","subtype":"success","is_error":false,"num_turns":3,"total_cost_usd":0.5,"usage":{"input_tokens":5,"cache_read_input_tokens":100,"cache_creation_input_tokens":10,"output_tokens":20}}' > "$dir/claude.json" + printf '%s\n' '{}' > "$dir/empty.json" + printf '%s\n' '{"stopReason":"cancelled","sessionId":"s","num_turns":1,"total_cost_usd":0.011,"usage":{"input_tokens":1,"cache_read_input_tokens":0,"cache_creation_input_tokens":0,"output_tokens":1}}' > "$dir/cancelled.json" + printf '%s\n' '{"stopReason":"end_turn","sessionId":"s","num_turns":1,"usage":{"input_tokens":1,"cache_read_input_tokens":0,"cache_creation_input_tokens":0,"output_tokens":1}}' > "$dir/nocost.json" + printf '%s\n' '{"stopReason":"end_turn","sessionId":"s","num_turns":1,"total_cost_usd":0.25,"usage_is_incomplete":true,"usage":{"input_tokens":5,"cache_read_input_tokens":100,"cache_creation_input_tokens":10,"output_tokens":20}}' > "$dir/partial.json" + out=$(bash -c ' + . "$1" + fm_supervision_engine_result grok "$2" 0 + fm_supervision_engine_result grok "$2" 0.25 + fm_supervision_engine_result grok "$3" 0 || true + fm_supervision_engine_result grok "$4" 0 + fm_supervision_engine_result grok "$5" 0 + fm_supervision_engine_result grok "$6" 0 + fm_supervision_engine_result claude "$7" 0.25 + ' _ "$ROOT/bin/fm-supervision-engine-lib.sh" "$dir/run.json" "$dir/empty.json" \ + "$dir/cancelled.json" "$dir/nocost.json" "$dir/partial.json" "$dir/claude.json") || rc=$? + [ "${rc:-0}" -eq 0 ] || fail "result: the parser did not run (rc=$rc): $out" + printf '%s\n' "$out" | grep -qx 'error=0 cost=0.25 conversation_cost=0.25 input=5 cache_read=100 cache_write=10 output=20 turns=1' \ + || fail "result: a Grok run's own cost was not reported: $out" + printf '%s\n' "$out" | grep -qx 'error=0 cost=0.25 conversation_cost=0.5 input=5 cache_read=100 cache_write=10 output=20 turns=1' \ + || fail "result: a resumed Grok run did not add its cost: $out" + printf '%s\n' "$out" | grep -qx 'error=1 cost=unreported conversation_cost=unreported input=0 cache_read=0 cache_write=0 output=0 turns=0' \ + || fail "result: an empty Grok result invented a cost: $out" + printf '%s\n' "$out" | grep -qx 'error=1 cost=0.011 conversation_cost=0.011 input=1 cache_read=0 cache_write=0 output=1 turns=1' \ + || fail "result: a cancelled Grok turn was treated as success or dropped its cost: $out" + printf '%s\n' "$out" | grep -qx 'error=1 cost=unreported conversation_cost=unreported input=1 cache_read=0 cache_write=0 output=1 turns=1' \ + || fail "result: a Grok result with no cost became zero: $out" + printf '%s\n' "$out" | grep -q 'error=1 cost=0.25 conversation_cost=0.25 input=5 cache_read=100 cache_write=10 output=20 turns=1' \ + || fail "result: an incomplete Grok usage was accepted: $out" + printf '%s\n' "$out" | grep -qx 'error=0 cost=0.25 conversation_cost=0.5 input=5 cache_read=100 cache_write=10 output=20 turns=3' \ + || fail "result: Claude's running total was no longer subtracted: $out" + pass "host: a Grok result adds each run's cost and rejects an incomplete one" +} + +test_empty_supervision_host_file_on_a_grok_primary_selects_grok() { + local home out + home=$(make_home grok-default away '') + out=$(bash -c ' + . "$1" + fm_supervision_host_config "$2" grok || exit 1 + printf "engine=%s model=%s problem=%s\n" \ + "$FM_SUPERVISION_ENGINE" "$FM_SUPERVISION_ENGINE_MODEL" "$FM_SUPERVISION_ENGINE_PROBLEM" + fm_supervision_host_config "$2" cursor || exit 1 + printf "cursor_problem=%s\n" "$FM_SUPERVISION_ENGINE_PROBLEM" + printf "codex\n" > "$2/supervision-host" + fm_supervision_host_config "$2" grok || exit 1 + printf "named=%s\n" "$FM_SUPERVISION_ENGINE_PROBLEM" + ' _ "$ROOT/bin/fm-supervision-engine-lib.sh" "$home/config") || fail "select: config did not resolve: $out" + printf '%s\n' "$out" | grep -qx 'engine=grok model=grok-4.7 problem=' \ + || fail "select: an empty file on a Grok primary did not select grok-4.7: $out" + printf '%s\n' "$out" | grep -qx "cursor_problem=the primary harness 'cursor' has no verified supervision engine" \ + || fail "select: an empty file on Cursor gained an engine: $out" + printf '%s\n' "$out" | grep -q "named=config/supervision-host names 'codex', which is not a verified supervision engine" \ + || fail "select: naming codex became verified: $out" + pass "host: an empty file on a Grok primary selects grok, and Cursor and Codex stay unverified" +} + +test_grok_turn_refuses_a_missing_login_and_launches_an_ambient_one() { + local home work rc call ghome + home=$(make_home grok-direct away grok) + work="$home/direct" + mkdir -p "$work/empty-home/.grok" "$work/login-home/.grok" "$work/prompt" + printf 'wake\n' > "$work/prompt/wake.txt" + printf 'branch prompt for the direct turn\n' > "$work/prompt/prompt.txt" + printf '{}\n' > "$work/login-home/.grok/auth.json" + echo fail > "$home/stub-mode" + rc=$(HOME="$work/empty-home" FM_HOME="$home" FM_ROOT="$ROOT" STATE="$home/state" \ + CLAUDE_CONFIG_DIR="$home/seats/seat-a" bash -c ' + . "$1/bin/fm-wake-lib.sh" + . "$1/bin/fm-timeout-lib.sh" + . "$1/bin/fm-supervision-engine-lib.sh" + fm_supervision_engine_turn grok grok-4.7 "$2" "$3" \ + 11111111-1111-1111-1111-111111111111 new 30 "$4/out" "$4/err" "" + printf "%s\n" "$?" + ' _ "$ROOT" "$work/prompt/prompt.txt" "$work/prompt/wake.txt" "$work") + [ "$rc" = 127 ] || fail "direct: a missing ambient login must not launch (rc=$rc): $(cat "$work/err" 2>/dev/null)" + assert_grep 'no auth.json' "$work/err" "direct: the refusal must name the missing login" + [ "$(engine_calls "$home")" -eq 0 ] || fail "direct: the engine launched without a login" + + rc=$(HOME="$work/login-home" FM_HOME="$home" FM_ROOT="$ROOT" STATE="$home/state" \ + FM_SUPERVISION_ACTOR=branch CLAUDE_CONFIG_DIR="$home/seats/seat-a" bash -c ' + . "$1/bin/fm-wake-lib.sh" + . "$1/bin/fm-timeout-lib.sh" + . "$1/bin/fm-supervision-engine-lib.sh" + fm_supervision_engine_turn grok grok-4.7 "$2" "$3" \ + 11111111-1111-1111-1111-111111111111 new 30 "$4/out" "$4/err" "" + printf "%s\n" "$?" + ' _ "$ROOT" "$work/prompt/prompt.txt" "$work/prompt/wake.txt" "$work") + [ "$rc" = 3 ] || fail "direct: the ambient login did not reach the stub (rc=$rc): $(cat "$work/err" 2>/dev/null)" + call="$home/engine-call.1" + assert_re '^actor=branch$' "$call" "direct: the launch must keep the branch actor" + assert_re '^profile=$' "$call" "direct: the launch must clear the inherited Claude seat" + assert_re '^arg=--always-approve$' "$call" "direct: the launch must auto-approve" + assert_re '^arg=--prompt-file$' "$call" "direct: the launch must use the prompt file" + ghome=$(sed -n 's/^grok_home=//p' "$call") + [ "$(readlink "$ghome/auth.json")" = "$work/login-home/.grok/auth.json" ] \ + || fail "direct: the private home must link the ambient login: $(readlink "$ghome/auth.json" 2>/dev/null)" + [ ! -e "$ghome/hooks" ] || fail "direct: the private home grew a hooks directory" + pass "host: a Grok turn refuses a missing login and launches an ambient one" +} + # FM_TEST_ONLY runs a single named case, the same entry point the sibling # suites expose. if [ -n "${FM_TEST_ONLY:-}" ]; then @@ -485,3 +691,9 @@ test_return_during_a_failed_turn_still_hands_its_outcomes_to_main test_incomplete_engine_result_hands_the_wake_to_main test_engine_turn_is_bounded_and_its_descendants_reaped test_restarted_host_stops_what_a_killed_predecessor_left +test_grok_away_wake_is_handled_and_resumed_on_its_own_contract +test_grok_engine_without_a_recorded_seat_hands_the_wake_to_main +test_grok_engine_without_a_seat_login_does_not_launch +test_grok_result_adds_each_runs_cost_and_rejects_an_incomplete_one +test_empty_supervision_host_file_on_a_grok_primary_selects_grok +test_grok_turn_refuses_a_missing_login_and_launches_an_ambient_one diff --git a/tests/fm-supervision-host-live-e2e.test.sh b/tests/fm-supervision-host-live-e2e.test.sh index 9e1f60ff9ab..33f1374d6d9 100755 --- a/tests/fm-supervision-host-live-e2e.test.sh +++ b/tests/fm-supervision-host-live-e2e.test.sh @@ -1,80 +1,45 @@ #!/usr/bin/env bash -# Opt-in credentialed live guard for the supervision host's Claude engine +# Opt-in credentialed live guard for the supervision host's verified engines # (bin/fm-supervision-host.sh, bin/fm-supervision-engine-lib.sh, # docs/supervision-host.md "Engines"). # -# Proves against the real installed Claude Code, with no stub anywhere: in an +# Proves against the real installed engine, with no stub anywhere: in an # isolated lab copy of this checkout opted into the host, an away-posture wake # produced by a real status append reaches a real headless engine turn that # drains the wake as the branch actor, records its outcome through # bin/fm-branch-report.sh, and acknowledges the wake, while the host stays # parked on a live successor watcher, main is never woken, and no hook of the # lab home fires inside the engine. A second wake then resumes the same engine -# conversation. Claude keeps its existing managed authentication; the engine's -# own session files land in Claude's project store for the lab directory. -# No live fleet home, worktree, or session is touched. +# conversation. Each engine keeps its existing managed authentication. +# FM_SUPERVISION_HOST_LIVE_ENGINE names one engine; otherwise both verified +# engines run. No live fleet home, worktree, or session is touched. # shellcheck disable=SC2016 # single-quoted scripts expand inside their own shells set -u # shellcheck source=tests/lib.sh . "$(dirname "${BASH_SOURCE[0]}")/lib.sh" -fm_live_gate opt-in FM_SUPERVISION_HOST_LIVE_E2E claude node perl git +fm_live_gate opt-in FM_SUPERVISION_HOST_LIVE_E2E claude grok node perl git -CLAUDE_VERSION=$(claude --version 2>/dev/null | head -n 1) -LAB=$(fm_test_tmproot fm-supervision-host-live) -LAB=$(cd -P "$LAB" && pwd -P) -FM="$LAB/fm" -# The session-lock holder must look like a Claude harness to the ancestry walk -# without shadowing the real claude the engine resolves from PATH. -mkdir -p "$LAB/harness" -ln -s /bin/bash "$LAB/harness/claude" -FAKE_CLAUDE="$LAB/harness/claude" HOST_TIMEOUT_POLLS=${FM_SUPERVISION_HOST_LIVE_POLLS:-3000} +ENGINES=${FM_SUPERVISION_HOST_LIVE_ENGINE:-claude grok} stop_lab() { local pid + [ -n "${FM:-}" ] || return 0 if [ -f "$FM/state/.supervision-host" ]; then pid=$(awk -F '\t' '$1 == "host" { print $2; exit }' "$FM/state/.supervision-host") [ -z "$pid" ] || kill -TERM "$pid" 2>/dev/null || true - sleep 2 + sleep 1 fi pid=$(cat "$FM/state/.watch.lock/pid" 2>/dev/null || true) [ -z "$pid" ] || kill -TERM "$pid" 2>/dev/null || true while IFS= read -r pid; do [ -z "$pid" ] || kill -TERM "$pid" 2>/dev/null || true - done < "$LAB/claude-pids" 2>/dev/null || true + done < "${LAB:-}/claude-pids" 2>/dev/null || true } trap 'stop_lab; fm_test_cleanup' EXIT -# A lab copy of this checkout's current tree (tracked and untracked, never -# ignored), committed on main, so the lab is a genuine primary checkout whose -# code root is its home. -mkdir -p "$FM" -git -C "$ROOT" ls-files -z -co --exclude-standard \ - | (cd "$ROOT" && tar --null -T - -cf -) | (cd "$FM" && tar -xf -) -git -C "$FM" init -q -b main -git -C "$FM" add -A >/dev/null -git -C "$FM" -c user.name=fmtest -c user.email=fmtest@example.invalid commit -q -m lab -mkdir -p "$FM/state" "$FM/config" "$LAB/tmuxbin" -: > "$FM/config/supervision-host" -printf '#!/usr/bin/env bash\nexit 1\n' > "$LAB/tmuxbin/tmux" -chmod +x "$LAB/tmuxbin/tmux" -printf 'project=demo\nwindow=fm-demo\nharness=claude\n' > "$FM/state/demo.meta" -FM_HOME="$FM" "$FM/bin/fm-afk-contract.sh" enter --words 'Watch the fleet. Merge nothing and dispatch nothing.' >/dev/null \ - || fail "could not record the lab's away posture" - -export FM_POLL=1 FM_SIGNAL_GRACE=0 FM_CHECK_INTERVAL=999999 FM_HEARTBEAT=999999 -unset FM_SUPERVISION_ENGINE_CLAUDE_BIN FM_SUPERVISION_ACTOR FM_BRANCH_REPORT_TURN FM_LEASE_HOLDER_PID -unset FM_ROOT_OVERRIDE FM_STATE_OVERRIDE FM_CONFIG_OVERRIDE PI_CODING_AGENT - -FM_HOME="$FM" PATH="$LAB/tmuxbin:$PATH" "$FAKE_CLAUDE" -c ' - printf "%s\n" "$$" > "$FM_HOME/state/.lock" - printf "%s\n" "$$" >> "$1/claude-pids" - "$FM_HOME/bin/fm-supervision-host.sh" park > "$1/host.out" 2>&1 - printf "%s\n" "$?" > "$1/host.rc" -' _ "$LAB" 2>> "$LAB/harness.err" & - wait_until() { # local limit=$1 i=0 shift @@ -98,43 +63,100 @@ diagnose() { "$(cat "$FM/state/.supervision-host.log" 2>/dev/null)" "$(cat "$FM/state/.wake-queue" 2>/dev/null)" } -wait_until 300 watcher_live || fail "the host never started a watcher cycle ($CLAUDE_VERSION)"$'\n'"$(diagnose)" -lock_before=$(cat "$FM/state/.lock") +run_live_engine() { # + local engine=$1 version seat + case "$engine" in + claude) version=$(claude --version 2>/dev/null | head -n 1) ;; + grok) + version=$(grok --version 2>/dev/null | head -n 1) + seat=${GROK_HOME:-$HOME/.grok} + [ -f "$seat/auth.json" ] || fail "the Grok seat has no auth.json ($version)" + ;; + *) fail "live engine '$engine' is not one this guard runs" ;; + esac + stop_lab + LAB=$(fm_test_tmproot "fm-supervision-host-live-$engine") + LAB=$(cd -P "$LAB" && pwd -P) + FM="$LAB/fm" + # The session-lock holder must look like a Claude harness to the ancestry + # walk without shadowing the real claude or grok the engine resolves. + mkdir -p "$LAB/harness" "$FM" + ln -s /bin/bash "$LAB/harness/claude" + git -C "$ROOT" ls-files -z -co --exclude-standard \ + | (cd "$ROOT" && tar --null -T - -cf -) | (cd "$FM" && tar -xf -) + git -C "$FM" init -q -b main + git -C "$FM" add -A >/dev/null + git -C "$FM" -c user.name=fmtest -c user.email=fmtest@example.invalid commit -q -m lab + mkdir -p "$FM/state" "$FM/config" "$LAB/tmuxbin" + case "$engine" in + grok) printf 'grok\n' > "$FM/config/supervision-host" ;; + *) : > "$FM/config/supervision-host" ;; + esac + printf '#!/usr/bin/env bash\nexit 1\n' > "$LAB/tmuxbin/tmux" + chmod +x "$LAB/tmuxbin/tmux" + printf 'project=demo\nwindow=fm-demo\nharness=claude\n' > "$FM/state/demo.meta" + FM_HOME="$FM" "$FM/bin/fm-afk-contract.sh" enter --words 'Watch the fleet. Merge nothing and dispatch nothing.' >/dev/null \ + || fail "could not record the lab's away posture ($version)" + + export FM_POLL=1 FM_SIGNAL_GRACE=0 FM_CHECK_INTERVAL=999999 FM_HEARTBEAT=999999 + unset FM_SUPERVISION_ENGINE_CLAUDE_BIN FM_SUPERVISION_ENGINE_GROK_BIN + unset FM_SUPERVISION_ACTOR FM_BRANCH_REPORT_TURN FM_LEASE_HOLDER_PID + unset FM_ROOT_OVERRIDE FM_STATE_OVERRIDE FM_CONFIG_OVERRIDE PI_CODING_AGENT -printf 'done [at=%s]: the demo cleanup finished; nothing else is needed\n' "$(date +%s)" >> "$FM/state/demo.status" -wait_until "$HOST_TIMEOUT_POLLS" settled_at_least 1 || fail "the first engine turn never settled ($CLAUDE_VERSION)"$'\n'"$(diagnose)" -grep -q ' handled turn=' "$FM/state/.supervision-host.log" \ - || fail "the real engine did not handle the away wake ($CLAUDE_VERSION)"$'\n'"$(diagnose)" -[ ! -s "$LAB/host.rc" ] || fail "a handled away wake reached main ($CLAUDE_VERSION)"$'\n'"$(diagnose)" -grep -q '"task":"demo"' "$FM/state/branch-outcomes.jsonl" \ - || fail "the engine's outcome did not reach the store ($CLAUDE_VERSION)"$'\n'"$(diagnose)" -! grep -q 'demo.status' "$FM/state/.wake-queue" 2>/dev/null \ - || fail "the engine did not acknowledge its wake ($CLAUDE_VERSION)"$'\n'"$(diagnose)" -[ ! -e "$FM/state/.claude-autoarm-epoch" ] || fail "a lab Stop hook fired inside the engine ($CLAUDE_VERSION)" -[ "$(cat "$FM/state/.lock")" = "$lock_before" ] || fail "the engine rewrote the session lock ($CLAUDE_VERSION)" -if FM_HOME="$FM" "$FM/bin/fm-lease.sh" check demo >/dev/null 2>&1; then - fail "a branch lease outlived the engine turn ($CLAUDE_VERSION)" -fi -wait_until 100 watcher_live || fail "the host is not parked on a live successor after handling ($CLAUDE_VERSION)" -printf '# first turn: %s\n' "$(grep ' handled ' "$FM/state/.supervision-host.log" | head -n 1 | cut -f2-5)" -printf '# outcome: %s\n' "$(head -n 1 "$FM/state/branch-outcomes.jsonl")" + FM_HOME="$FM" PATH="$LAB/tmuxbin:$PATH" "$LAB/harness/claude" -c ' + printf "%s\n" "$$" > "$FM_HOME/state/.lock" + if [ "$2" = grok ]; then + printf "pid=%s\nharness=grok\nprofile=\ngrok_profile=%s\n" "$$" "$3" > "$FM_HOME/state/.lock-runtime" + else + printf "pid=%s\nharness=claude\nprofile=%s\n" "$$" "${CLAUDE_CONFIG_DIR:-}" > "$FM_HOME/state/.lock-runtime" + fi + printf "%s\n" "$$" >> "$1/claude-pids" + "$FM_HOME/bin/fm-supervision-host.sh" park > "$1/host.out" 2>&1 + printf "%s\n" "$?" > "$1/host.rc" + ' _ "$LAB" "$engine" "${seat:-}" 2>> "$LAB/harness.err" & -session=$(sed -n 's/^session=//p' "$FM/state/.supervision-host-engine") -printf 'working [at=%s]: started the follow-up check\n' "$(date +%s)" >> "$FM/state/demo.status" -wait_until "$HOST_TIMEOUT_POLLS" settled_at_least 2 || fail "the second engine turn never settled ($CLAUDE_VERSION)"$'\n'"$(diagnose)" -[ "$(grep -c ' handled ' "$FM/state/.supervision-host.log")" -ge 2 ] \ - || fail "the real engine did not handle the second wake ($CLAUDE_VERSION)"$'\n'"$(diagnose)" -[ "$(sed -n 's/^session=//p' "$FM/state/.supervision-host-engine")" = "$session" ] \ - || fail "the second turn did not resume the engine conversation ($CLAUDE_VERSION)" -[ "$(sed -n 's/^turns=//p' "$FM/state/.supervision-host-engine")" = 2 ] \ - || fail "the engine conversation did not count its second turn ($CLAUDE_VERSION)" -printf '# second turn: %s\n' "$(grep ' handled ' "$FM/state/.supervision-host.log" | sed -n 2p | cut -f2-5)" + wait_until 300 watcher_live || fail "the host never started a watcher cycle ($version)"$'\n'"$(diagnose)" + lock_before=$(cat "$FM/state/.lock") -host_pid=$(awk -F '\t' '$1 == "host" { print $2 }' "$FM/state/.supervision-host") -watcher=$(cat "$FM/state/.watch.lock/pid") -kill -TERM "$host_pid" -wait_until 400 test -s "$LAB/host.rc" || fail "the host did not stop on TERM" -wait_until 100 sh -c '! kill -0 "$1" 2>/dev/null' _ "$watcher" || fail "a stopped host left its watcher running" -[ ! -e "$FM/state/.supervision-host" ] || fail "a stopped host left its record" + printf 'done [at=%s]: the demo cleanup finished; nothing else is needed\n' "$(date +%s)" >> "$FM/state/demo.status" + wait_until "$HOST_TIMEOUT_POLLS" settled_at_least 1 || fail "the first engine turn never settled ($version)"$'\n'"$(diagnose)" + grep -q ' handled turn=' "$FM/state/.supervision-host.log" \ + || fail "the real engine did not handle the away wake ($version)"$'\n'"$(diagnose)" + [ ! -s "$LAB/host.rc" ] || fail "a handled away wake reached main ($version)"$'\n'"$(diagnose)" + grep -q '"task":"demo"' "$FM/state/branch-outcomes.jsonl" \ + || fail "the engine's outcome did not reach the store ($version)"$'\n'"$(diagnose)" + ! grep -q 'demo.status' "$FM/state/.wake-queue" 2>/dev/null \ + || fail "the engine did not acknowledge its wake ($version)"$'\n'"$(diagnose)" + [ ! -e "$FM/state/.claude-autoarm-epoch" ] || fail "a lab Stop hook fired inside the engine ($version)" + [ "$(cat "$FM/state/.lock")" = "$lock_before" ] || fail "the engine rewrote the session lock ($version)" + if FM_HOME="$FM" "$FM/bin/fm-lease.sh" check demo >/dev/null 2>&1; then + fail "a branch lease outlived the engine turn ($version)" + fi + wait_until 100 watcher_live || fail "the host is not parked on a live successor after handling ($version)" + printf '# %s first turn: %s\n' "$engine" "$(grep ' handled ' "$FM/state/.supervision-host.log" | head -n 1 | cut -f2-5)" + printf '# %s outcome: %s\n' "$engine" "$(head -n 1 "$FM/state/branch-outcomes.jsonl")" + + session=$(sed -n 's/^session=//p' "$FM/state/.supervision-host-engine") + printf 'working [at=%s]: started the follow-up check\n' "$(date +%s)" >> "$FM/state/demo.status" + wait_until "$HOST_TIMEOUT_POLLS" settled_at_least 2 || fail "the second engine turn never settled ($version)"$'\n'"$(diagnose)" + [ "$(grep -c ' handled ' "$FM/state/.supervision-host.log")" -ge 2 ] \ + || fail "the real engine did not handle the second wake ($version)"$'\n'"$(diagnose)" + [ "$(sed -n 's/^session=//p' "$FM/state/.supervision-host-engine")" = "$session" ] \ + || fail "the second turn did not resume the engine conversation ($version)" + [ "$(sed -n 's/^turns=//p' "$FM/state/.supervision-host-engine")" = 2 ] \ + || fail "the engine conversation did not count its second turn ($version)" + printf '# %s second turn: %s\n' "$engine" "$(grep ' handled ' "$FM/state/.supervision-host.log" | sed -n 2p | cut -f2-5)" + + host_pid=$(awk -F '\t' '$1 == "host" { print $2 }' "$FM/state/.supervision-host") + watcher=$(cat "$FM/state/.watch.lock/pid") + kill -TERM "$host_pid" + wait_until 400 test -s "$LAB/host.rc" || fail "the host did not stop on TERM ($version)" + wait_until 100 sh -c '! kill -0 "$1" 2>/dev/null' _ "$watcher" || fail "a stopped host left its watcher running ($version)" + [ ! -e "$FM/state/.supervision-host" ] || fail "a stopped host left its record ($version)" + pass "supervision host live ($version): a real $engine engine handles and resumes away wakes under the branch contract without waking main" +} -pass "supervision host live ($CLAUDE_VERSION): a real engine handles and resumes away wakes under the branch contract without waking main" +read -r -a engine_list <<< "$ENGINES" +for engine in "${engine_list[@]}"; do + run_live_engine "$engine" +done diff --git a/tests/fm-supervision-host-stop-hook.test.sh b/tests/fm-supervision-host-stop-hook.test.sh index 0f6769a3492..fd53b1d9af1 100755 --- a/tests/fm-supervision-host-stop-hook.test.sh +++ b/tests/fm-supervision-host-stop-hook.test.sh @@ -714,6 +714,44 @@ test_attended_latch_keeps_closes_on_main_and_records_recovery_off_main() { pass "host: attended, two engine errors latch the session, main keeps every close unchanged in the cooldown, a failed probe doubles it up to its cap, and a routine probe's recovery stays in the ledger, off main" } +# The live loop (2026-11-06): the host was on beside a Claude primary, and +# every turn end took over the cycle the previous pass-through had left. +# The fresh watcher reopened the recovery that pass-through had just announced, +# so the close was `check: rearm-resurface` again, attended and main-only, and +# the hook handed it straight back. Main's turn ended and the next arm did the +# same, about every 10 seconds. The queued work is still there for a real +# close to drain; the takeover must not announce it again. +test_take_over_does_not_hand_the_same_resurface_straight_back() { + local home resurfaces + home=$(make_primary_home hook-resurface-loop) + start_hook_session "$home" + FM_HOME="$home" bash -c ' + . "$1" + fm_wake_append check resurface-fixture "check: work stranded before the host parked" + ' _ "$ROOT/bin/fm-wake-lib.sh" \ + || fail "fixture: could not queue the stranded wake" + turn_end "$home" + wait_until 250 hook_exited "$home" \ + || fail "resurface loop: the first close never reached the Stop hook: $(cat "$home/state/.supervision-host.log" 2>/dev/null)" + assert_re ' pass-through attended main-only check: rearm-resurface' "$home/state/.supervision-host.log" \ + "fixture: the stranded queue was not handed back once" + assert_rewoke_main "$home" "resurface loop (first)" + watcher_live "$home" || fail "resurface loop: the first pass-through left no successor" + # Main does not drain or acknowledge. That is the turn that used to end and + # arm again while the same rows were still queued. + turn_end "$home" + wait_until 200 grep -q $'\ttake-over\tarm=' "$home/state/.supervision-host.log" \ + || fail "resurface loop: the next turn end did not take the cycle over: $(cat "$home/state/.supervision-host.log")" + sleep 3 + ! hook_exited "$home" \ + || fail "resurface loop: the takeover handed a wake straight back: $(cat "$home/hook.err" 2>/dev/null)"$'\n'"$(tail -n 8 "$home/state/.supervision-host.log")" + resurfaces=$(grep -c 'check: rearm-resurface' "$home/state/.supervision-host.log" || true) + [ "$resurfaces" -eq 1 ] \ + || fail "resurface loop: the takeover announced recovery again ($resurfaces times): $(cat "$home/state/.supervision-host.log")" + watcher_live "$home" || fail "resurface loop: the takeover left no watcher" + pass "host+hook: a takeover does not hand the same rearm-resurface straight back" +} + # FM_TEST_ONLY runs a single named case, the same entry point the sibling # suites expose. if [ -n "${FM_TEST_ONLY:-}" ]; then @@ -735,6 +773,7 @@ test_claude_stop_hook_notifies_when_at_turn_downtime_write_fails test_successor_close_during_main_turn_is_delivered_at_the_next_turn_end test_pass_through_successor_survives_the_hook_process_group_teardown test_next_park_takes_over_the_cycle_a_pass_through_left_for_main +test_take_over_does_not_hand_the_same_resurface_straight_back test_a_park_stopped_mid_take_over_leaves_the_take_over_to_the_next_park test_unrecorded_successor_is_stopped_rather_than_left_for_main test_primary_without_a_verified_mirror_runs_away_only diff --git a/tests/supervision-host-helpers.sh b/tests/supervision-host-helpers.sh index 5a8b7494c1c..b9f07d24d77 100644 --- a/tests/supervision-host-helpers.sh +++ b/tests/supervision-host-helpers.sh @@ -7,7 +7,8 @@ # The loop cases run the real host, arm, watcher, wake grant, drain, outcome # store, and lease scripts in a fixture home. The host runs as a child of a fake # harness (a bash symlink named "claude") whose pid is the home's session lock, -# and its engine is a stub named by FM_SUPERVISION_ENGINE_CLAUDE_BIN that does +# and its engine is a stub named by FM_SUPERVISION_ENGINE_CLAUDE_BIN (and by +# FM_SUPERVISION_ENGINE_GROK_BIN, the same stub) that does # what a branch turn does through the same scripts, so the real argument # construction, bounding, and reaping are exercised without a model. A real # status append drives each wake through the real watcher. @@ -81,14 +82,26 @@ STATE=${FM_STATE_OVERRIDE:-$FM_HOME/state} mode=$(cat "$FM_HOME/stub-mode" 2>/dev/null || echo handle) n=$(( $(ls "$FM_HOME"/engine-call.* 2>/dev/null | wc -l) + 1 )) { - printf 'mode=%s\nactor=%s\nholder=%s\nprimary=%s\nturn=%s\nprofile=%s\n' "$mode" "${FM_SUPERVISION_ACTOR:-}" \ + printf 'mode=%s\nactor=%s\nholder=%s\nprimary=%s\nturn=%s\nprofile=%s\ngrok_home=%s\n' "$mode" "${FM_SUPERVISION_ACTOR:-}" \ "${FM_LEASE_HOLDER_PID:-}" "${FM_SUPERVISION_PRIMARY_HARNESS:-}" "${FM_BRANCH_REPORT_TURN:-}" \ - "${CLAUDE_CONFIG_DIR:-}" - for a in "$@"; do printf 'arg=%s\n' "$a"; done + "${CLAUDE_CONFIG_DIR:-}" "${GROK_HOME:-}" + grok_turn=0 + for a in "$@"; do + printf 'arg=%s\n' "$a" + case "$a" in + --prompt-file|--always-approve) grok_turn=1 ;; + esac + done } > "$FM_HOME/engine-call.$n" case "$mode" in held|captain-held) printf 'ready\n' > "$FM_HOME/stub-ready" ;; esac -# Like Claude, the reported cost is the conversation's running total. +# Claude's reported cost is the conversation's running total. A Grok launch, +# recognized by its own flags, reports that turn's cost alone. result() { + if [ "${grok_turn:-0}" = 1 ]; then + printf '{"stopReason":"end_turn","sessionId":"stub-grok","num_turns":1,"total_cost_usd":0.25,' + printf '"usage":{"input_tokens":5,"cache_read_input_tokens":100,"cache_creation_input_tokens":10,"output_tokens":20}}\n' + return + fi printf '{"type":"result","subtype":"success","is_error":false,"num_turns":3,"total_cost_usd":%s,' "$(awk -v n="$n" 'BEGIN { print n * 0.25 }')" printf '"usage":{"input_tokens":5,"cache_read_input_tokens":100,"cache_creation_input_tokens":10,"output_tokens":20},"session_id":"stub"}\n' } @@ -165,6 +178,7 @@ chmod +x "$STUB" export FM_REPO="$ROOT" export FM_SUPERVISION_ENGINE_CLAUDE_BIN="$STUB" +export FM_SUPERVISION_ENGINE_GROK_BIN="$STUB" export FM_SUPERVISION_HOST_PRIMARY=claude export FM_POLL=1 FM_SIGNAL_GRACE=0 FM_CHECK_INTERVAL=999999 FM_HEARTBEAT=999999 # Keep the real engine watchdog/reaping path, but not its production grace in fixtures. From 39775fe8e6614076b5c9a2b1768af1f8d7c7c386 Mon Sep 17 00:00:00 2001 From: max-metaplanet Date: Fri, 6 Nov 2026 07:12:09 -0500 Subject: [PATCH 2/4] no-mistakes(review): Refuse stale Grok seats and replaced auth links --- bin/fm-supervision-engine-lib.sh | 12 ++++- docs/supervision-host.md | 2 + tests/fm-supervision-host-engine.test.sh | 53 +++++++++++++++++++++- tests/fm-supervision-host-live-e2e.test.sh | 6 +-- 4 files changed, 66 insertions(+), 7 deletions(-) diff --git a/bin/fm-supervision-engine-lib.sh b/bin/fm-supervision-engine-lib.sh index ce46a985eaf..bbae28580da 100644 --- a/bin/fm-supervision-engine-lib.sh +++ b/bin/fm-supervision-engine-lib.sh @@ -344,8 +344,8 @@ _fm_engine_reap() { # A Claude engine reads profile, except a Codex primary, which carries its # retained Claude credentials separately in claude_profile; profile names its # Codex seat and must never reach Claude. A Grok engine reads grok_profile -# when the primary is Grok, and spends the ambient Grok login when any other -# primary names grok. An empty grok_profile is that ambient login. A missing +# when the primary is Grok, and spends the ambient Grok login when the current +# lock owner's record names any other primary. An empty grok_profile is that ambient login. A missing # grok_profile key on a Grok primary is no verdict. The recorded profile is # the seat directory, never the private GROK_HOME the turn launches under. # Sets FM_SUPERVISION_ENGINE_PROFILE to that profile directory, or to empty @@ -380,6 +380,10 @@ fm_supervision_engine_seat() { FM_SUPERVISION_ENGINE_SEAT_PROBLEM='' harness=$(fm_session_lock_runtime_field "$state_dir" harness) || harness=claude if [ "${FM_SUPERVISION_ENGINE:-}" = grok ]; then + if ! fm_session_lock_runtime_field "$state_dir" pid >/dev/null; then + FM_SUPERVISION_ENGINE_SEAT_PROBLEM="the record of the session this host serves does not name the current lock owner, so the Grok seat the engine must spend is unknown" + return 1 + fi if [ "$harness" != grok ]; then FM_SUPERVISION_ENGINE_PROFILE='' return 0 @@ -465,6 +469,10 @@ _fm_supervision_grok_prepare() { printf 'the Grok seat has no auth.json at %s, so the engine was not launched\n' "$auth" > "$errors" return 127 fi + if [ -e "$ghome/auth.json" ] && [ ! -L "$ghome/auth.json" ]; then + printf 'the Grok engine home holds its own auth.json at %s instead of the link to %s, so a refreshed login may be stranded there; the engine was not launched\n' "$ghome/auth.json" "$auth" > "$errors" + return 127 + fi ln -sfn "$auth" "$ghome/auth.json" || { printf 'the Grok engine home could not link its seat login\n' > "$errors" return 127 diff --git a/docs/supervision-host.md b/docs/supervision-host.md index 1bb8300da24..23ec9cb3bc2 100644 --- a/docs/supervision-host.md +++ b/docs/supervision-host.md @@ -331,6 +331,7 @@ The engine spends an account, so which seat it launches on is resolved before ev The seat is read from the runtime record beside the home's session lock, which is bound to the lock's current owner. A Claude engine reads that session's Claude seat, and a Codex primary supplies the Claude credentials retained for that engine separately from its Codex seat. A Grok engine reads `grok_profile` when the primary is Grok, and the ambient Grok login when another primary names `grok`. +Either way it needs a record that names the current lock owner; without one the wake goes to main, as it does for a Claude engine. `fm_supervision_engine_seat` in `bin/fm-supervision-engine-lib.sh` owns that distinction and its legacy-record handling. `config/claude-seat` is not that record, because it names the seat new workers get and moves without the lead. @@ -426,6 +427,7 @@ That directory has no hooks directory. Its `config.toml` ignores the usual skill roots and turns off Claude, Cursor, and Codex compat hooks, skills, rules, agents, MCP, and sessions. The launch environment clears those same cells. The seat's `auth.json` is linked, not copied, because a copied refresh token can be single-use. +If a turn leaves a regular `auth.json` in that home instead of the link, the next turn refuses to launch rather than relink over a possibly refreshed login. The launch does not pass `--trust` and does not set `GROK_FOLDER_TRUST`. **Permissions** diff --git a/tests/fm-supervision-host-engine.test.sh b/tests/fm-supervision-host-engine.test.sh index 407e0dca12f..069f00cb627 100755 --- a/tests/fm-supervision-host-engine.test.sh +++ b/tests/fm-supervision-host-engine.test.sh @@ -504,7 +504,18 @@ test_grok_away_wake_is_handled_and_resumed_on_its_own_contract() { [ ! -e "$ghome/hooks" ] || fail "the Grok engine home must not have a hooks directory" [ "$(readlink "$ghome/auth.json")" = "$seat/auth.json" ] \ || fail "the Grok engine must link the seat login rather than copy it: $(readlink "$ghome/auth.json" 2>/dev/null)" - assert_grep 'hooks = false' "$ghome/config.toml" "the Grok engine home must disable compat hooks" + python3 -I - "$ghome/config.toml" <<'PY' || fail "the Grok engine home config.toml must ignore the skill roots and turn off every compat cell" +import sys, tomllib +with open(sys.argv[1], "rb") as stream: + config = tomllib.load(stream) +cells = ("hooks", "skills", "rules", "agents", "mcps", "sessions") +for harness in ("claude", "cursor", "codex"): + table = config["compat"][harness] + assert all(table[cell] is False for cell in cells), (harness, table) +ignored = set(config["skills"]["ignore"]) +roots = {"~/.agents", "~/.grok/skills", "~/.claude", "~/.cursor", ".agents", ".grok", ".claude", ".cursor"} +assert roots <= ignored, sorted(roots - ignored) +PY assert_grep "profile=$seat" "$home/state/.supervision-host-engine" \ "the conversation must record the Grok seat, not the private home" [ ! -s "$home/host.rc" ] || fail "a handled Grok away wake reached main: $(cat "$home/host.out")" @@ -539,6 +550,25 @@ test_grok_engine_without_a_recorded_seat_hands_the_wake_to_main() { pass "host: a Grok primary with no recorded seat hands the wake to main" } +test_grok_engine_on_a_departed_owners_record_hands_the_wake_to_main() { + local home seat saved + home=$(make_home grok-stale away grok) + seat="$home/seats/grok" + mkdir -p "$seat" + printf '{}\n' > "$seat/auth.json" + saved=$FM_SUPERVISION_HOST_PRIMARY + FM_SUPERVISION_HOST_PRIMARY=grok start_host "$home" + FM_SUPERVISION_HOST_PRIMARY=$saved + wait_until 150 watcher_live "$home" || fail "grok stale: the host never started a watcher cycle" + printf 'pid=%s\nharness=grok\nprofile=\ngrok_profile=%s\n' 999999 "$seat" > "$home/state/.lock-runtime" + append_status "$home" 'needs a seat' + wait_until 250 host_exited "$home" || fail "grok stale: the host did not hand the wake to main: $(cat "$home/state/.supervision-host.log")" + assert_grep 'does not name the current lock owner' "$home/host.out" "the handback must name the unknown owner as the cause" + [ "$(engine_calls "$home")" -eq 0 ] || fail "the Grok engine launched on a record the lock no longer owns" + assert_grep 'demo.status' "$home/state/.wake-queue" "the unhandled wake must stay durable for main" + pass "host: a Grok engine never falls back to the ambient login on a departed owner's record" +} + test_grok_engine_without_a_seat_login_does_not_launch() { local home seat saved lock_pid home=$(make_home grok-nologin away grok) @@ -663,7 +693,25 @@ test_grok_turn_refuses_a_missing_login_and_launches_an_ambient_one() { [ "$(readlink "$ghome/auth.json")" = "$work/login-home/.grok/auth.json" ] \ || fail "direct: the private home must link the ambient login: $(readlink "$ghome/auth.json" 2>/dev/null)" [ ! -e "$ghome/hooks" ] || fail "direct: the private home grew a hooks directory" - pass "host: a Grok turn refuses a missing login and launches an ambient one" + + rm -f "$ghome/auth.json" + printf '{"refreshed":true}\n' > "$ghome/auth.json" + rc=$(HOME="$work/login-home" FM_HOME="$home" FM_ROOT="$ROOT" STATE="$home/state" \ + FM_SUPERVISION_ACTOR=branch bash -c ' + . "$1/bin/fm-wake-lib.sh" + . "$1/bin/fm-timeout-lib.sh" + . "$1/bin/fm-supervision-engine-lib.sh" + fm_supervision_engine_turn grok grok-4.7 "$2" "$3" \ + 11111111-1111-1111-1111-111111111111 resume 30 "$4/out" "$4/err" "" + printf "%s\n" "$?" + ' _ "$ROOT" "$work/prompt/prompt.txt" "$work/prompt/wake.txt" "$work") + [ "$rc" = 127 ] || fail "direct: a replaced login link must not be relinked and launched (rc=$rc)" + assert_grep 'instead of the link' "$work/err" "direct: the refusal must name the replaced link" + if [ -L "$ghome/auth.json" ] || ! grep -q refreshed "$ghome/auth.json"; then + fail "direct: the replaced login must be left in place, not relinked over" + fi + [ "$(engine_calls "$home")" -eq 1 ] || fail "direct: the engine launched over a replaced login link" + pass "host: a Grok turn refuses a missing login, launches an ambient one, and never relinks over a replaced one" } # FM_TEST_ONLY runs a single named case, the same entry point the sibling @@ -693,6 +741,7 @@ test_engine_turn_is_bounded_and_its_descendants_reaped test_restarted_host_stops_what_a_killed_predecessor_left test_grok_away_wake_is_handled_and_resumed_on_its_own_contract test_grok_engine_without_a_recorded_seat_hands_the_wake_to_main +test_grok_engine_on_a_departed_owners_record_hands_the_wake_to_main test_grok_engine_without_a_seat_login_does_not_launch test_grok_result_adds_each_runs_cost_and_rejects_an_incomplete_one test_empty_supervision_host_file_on_a_grok_primary_selects_grok diff --git a/tests/fm-supervision-host-live-e2e.test.sh b/tests/fm-supervision-host-live-e2e.test.sh index 33f1374d6d9..0e922da817d 100755 --- a/tests/fm-supervision-host-live-e2e.test.sh +++ b/tests/fm-supervision-host-live-e2e.test.sh @@ -19,10 +19,11 @@ set -u # shellcheck source=tests/lib.sh . "$(dirname "${BASH_SOURCE[0]}")/lib.sh" -fm_live_gate opt-in FM_SUPERVISION_HOST_LIVE_E2E claude grok node perl git +ENGINES=${FM_SUPERVISION_HOST_LIVE_ENGINE:-claude grok} +read -r -a engine_list <<< "$ENGINES" +fm_live_gate opt-in FM_SUPERVISION_HOST_LIVE_E2E "${engine_list[@]}" node perl git HOST_TIMEOUT_POLLS=${FM_SUPERVISION_HOST_LIVE_POLLS:-3000} -ENGINES=${FM_SUPERVISION_HOST_LIVE_ENGINE:-claude grok} stop_lab() { local pid @@ -156,7 +157,6 @@ run_live_engine() { # pass "supervision host live ($version): a real $engine engine handles and resumes away wakes under the branch contract without waking main" } -read -r -a engine_list <<< "$ENGINES" for engine in "${engine_list[@]}"; do run_live_engine "$engine" done From a51b2b8a885a451d186baf33fcdacd867788e4e2 Mon Sep 17 00:00:00 2001 From: max-metaplanet Date: Fri, 6 Nov 2026 07:25:13 -0500 Subject: [PATCH 3/4] no-mistakes(review): Limit the Grok engine to away turns --- bin/fm-supervision-engine-lib.sh | 5 ++++- docs/supervision-host.md | 2 ++ tests/fm-supervision-host-engine.test.sh | 17 +++++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/bin/fm-supervision-engine-lib.sh b/bin/fm-supervision-engine-lib.sh index bbae28580da..91c6f78586b 100644 --- a/bin/fm-supervision-engine-lib.sh +++ b/bin/fm-supervision-engine-lib.sh @@ -148,7 +148,8 @@ EOF } # fm_supervision_host_attended_ready -# 0 when the attended host's configured engine, executable, node, jq, turn +# 0 when the attended host's configured engine, verified for attended turns +# (Grok is verified for away turns only), its executable, node, jq, turn # bound (perl, timeout, or gtimeout), and primary's mirror writer are ready; # otherwise 1, with FM_SUPERVISION_HOST_UNREADY naming why. The host's # attended acceptor runs it on every attended close; the mirror's contents are @@ -159,6 +160,8 @@ fm_supervision_host_attended_ready() { FM_SUPERVISION_HOST_UNREADY="the home does not run the supervision host" elif [ -z "$FM_SUPERVISION_ENGINE" ]; then FM_SUPERVISION_HOST_UNREADY="no supervision engine" + elif [ "$FM_SUPERVISION_ENGINE" = grok ]; then + FM_SUPERVISION_HOST_UNREADY="the grok engine is verified for away turns only" elif ! fm_supervision_engine_bin "$FM_SUPERVISION_ENGINE" >/dev/null 2>&1; then FM_SUPERVISION_HOST_UNREADY="the $FM_SUPERVISION_ENGINE engine executable is missing" elif ! command -v node >/dev/null 2>&1; then diff --git a/docs/supervision-host.md b/docs/supervision-host.md index 23ec9cb3bc2..2645e31c296 100644 --- a/docs/supervision-host.md +++ b/docs/supervision-host.md @@ -118,6 +118,7 @@ The host-off Claude Stop hook's detached handling successor is also unchanged; s It also passes the close through unchanged, with no added line, when any of these holds (`fm_supervision_host_attended_ready` in `bin/fm-supervision-engine-lib.sh` owns the list): - The home names no usable engine. +- The engine is Grok, which is verified for away turns only; no attended live check covers it yet. - A tool its turns need is missing: the engine executable, node, jq, or one of perl, timeout, or gtimeout to bound the turn. - The primary has no verified dialog mirror. - The main session's lock holder cannot be identified. @@ -374,6 +375,7 @@ Each engine turn's line carries these fields, and this log is where engine cost A verified engine is a headless mode of a harness whose isolation, actor propagation, promptless permissions, bounding, and caching were measured. The verified engines are Claude's print mode, measured on Claude Code 2.1.278 and 2.1.281, and Grok's headless mode, measured on grok 1.0.77. +Grok is verified for away turns only: an attended close on a home whose engine is Grok reaches main unchanged. A Codex primary keeps the Claude engine when `config/supervision-host` names `claude`, using the Claude account that move retains. There is no architectural requirement that the engine be one of these two. Changing it requires implementing and verifying another engine adapter. diff --git a/tests/fm-supervision-host-engine.test.sh b/tests/fm-supervision-host-engine.test.sh index 069f00cb627..3dbc6566fb9 100755 --- a/tests/fm-supervision-host-engine.test.sh +++ b/tests/fm-supervision-host-engine.test.sh @@ -569,6 +569,22 @@ test_grok_engine_on_a_departed_owners_record_hands_the_wake_to_main() { pass "host: a Grok engine never falls back to the ambient login on a departed owner's record" } +test_grok_engine_hands_an_attended_close_to_main() { + local home + home=$(make_home grok-attended attended grok) + start_host "$home" + wait_until 150 watcher_live "$home" || fail "grok attended: the host never started a watcher cycle: $(cat "$home/host.out")" + append_status "$home" 'while the captain is here' + wait_until 250 host_exited "$home" \ + || fail "grok attended: the attended close did not reach main: $(cat "$home/state/.supervision-host.log")" + assert_re ' pass-through attended the grok engine is verified for away turns only ' "$home/state/.supervision-host.log" \ + "the attended pass-through must say Grok is verified for away turns only" + assert_re '^signal: .*demo\.status' "$home/host.out" "the attended close must reach main unchanged" + assert_no_re '^supervision-host: ' "$home/host.out" "an attended pass-through must add no line to the close" + [ "$(engine_calls "$home")" -eq 0 ] || fail "the Grok engine ran an attended turn" + pass "host: a Grok engine never takes an attended close" +} + test_grok_engine_without_a_seat_login_does_not_launch() { local home seat saved lock_pid home=$(make_home grok-nologin away grok) @@ -742,6 +758,7 @@ test_restarted_host_stops_what_a_killed_predecessor_left test_grok_away_wake_is_handled_and_resumed_on_its_own_contract test_grok_engine_without_a_recorded_seat_hands_the_wake_to_main test_grok_engine_on_a_departed_owners_record_hands_the_wake_to_main +test_grok_engine_hands_an_attended_close_to_main test_grok_engine_without_a_seat_login_does_not_launch test_grok_result_adds_each_runs_cost_and_rejects_an_incomplete_one test_empty_supervision_host_file_on_a_grok_primary_selects_grok From 913c48550974328d3ae736f9b04e85db8289bd2d Mon Sep 17 00:00:00 2001 From: max-metaplanet Date: Fri, 6 Nov 2026 07:40:21 -0500 Subject: [PATCH 4/4] no-mistakes(document): Point harness docs to the owner of Grok engine choice --- .agents/skills/harness-adapters/references/harness/claude.md | 2 +- .agents/skills/harness-adapters/references/harness/codex.md | 2 +- .agents/skills/harness-adapters/references/harness/cursor.md | 2 +- .agents/skills/harness-adapters/references/harness/omp.md | 2 +- .../skills/harness-adapters/references/harness/opencode.md | 2 +- docs/configuration.md | 5 ++--- docs/supervision-host.md | 2 +- 7 files changed, 8 insertions(+), 9 deletions(-) diff --git a/.agents/skills/harness-adapters/references/harness/claude.md b/.agents/skills/harness-adapters/references/harness/claude.md index 06606d227b1..b72c0fcf3f3 100644 --- a/.agents/skills/harness-adapters/references/harness/claude.md +++ b/.agents/skills/harness-adapters/references/harness/claude.md @@ -92,7 +92,7 @@ Hooks still run through cwd-sensitive `/bin/sh`, so tracked commands anchor thro The Stop-owned watcher hook runs every Stop, foregrounds `../../../bin/fm-watch-arm.sh` only when eligible, and uses exit-2 async reawakening as notification. The model handles notifications but never routine re-arm. -Unless `config/supervision-host-off` opts the home out, the hook foregrounds the supervision host instead, which also runs Claude's print mode as its headless engine; [`supervision-host.md`](../../../../../docs/supervision-host.md#engines) owns the verified engine facts. +Unless `config/supervision-host-off` opts the home out, the hook foregrounds the supervision host instead, which by default runs Claude's print mode as its headless engine; [`supervision-host.md`](../../../../../docs/supervision-host.md#engines) owns the verified engine facts. Claude's PreToolUse seatbelt blocks directly, and its deny is honored only with empty stdout; `../../../docs/arm-pretool-check.md` owns that contract. ### Delegation guard diff --git a/.agents/skills/harness-adapters/references/harness/codex.md b/.agents/skills/harness-adapters/references/harness/codex.md index e62fbdb6e16..09aaecca438 100644 --- a/.agents/skills/harness-adapters/references/harness/codex.md +++ b/.agents/skills/harness-adapters/references/harness/codex.md @@ -63,5 +63,5 @@ The tracked hook anchors to `pwd -P`, verifies that root is Firstmate-shaped and Codex's primary watcher protocol is `../../../bin/fm-watch-checkpoint.sh --seconds "${FM_CODEX_WATCH_CHECKPOINT:-180}"`, not `../../../bin/fm-watch-arm.sh`. Codex cannot reason while a foreground tool call is running, so the checkpoint is deliberately foreground and bounded to return control regularly for user messages and queued notifications. -In a home with `config/supervision-host` and no `config/supervision-host-off` the checkpoint runs the supervision host instead of the watcher, with Claude's print mode as its headless engine, and holds for at least an hour while away; [`supervision-host.md`](../../../../../docs/supervision-host.md) owns the host and that bound. +In a home with `config/supervision-host` and no `config/supervision-host-off` the checkpoint runs the supervision host instead of the watcher, and holds for at least an hour while away; [`supervision-host.md`](../../../../../docs/supervision-host.md) owns the host, which headless engine it runs, and that bound. Codex's PreToolUse watcher-arm seatbelt blocks directly through its project hook. diff --git a/.agents/skills/harness-adapters/references/harness/cursor.md b/.agents/skills/harness-adapters/references/harness/cursor.md index 5e1c7932c76..68e25751953 100644 --- a/.agents/skills/harness-adapters/references/harness/cursor.md +++ b/.agents/skills/harness-adapters/references/harness/cursor.md @@ -69,7 +69,7 @@ Example: `../../../bin/fm-spawn.sh --scout --harness cursor ## Primary integration Primary supervision is the stop-hook park in `../../../docs/supervision-protocols/cursor.md` through tracked `.cursor/hooks.json`; primary and secondmate launches require `--trust` or hooks do not load. -In a home with `config/supervision-host` and no `config/supervision-host-off` the park runs the supervision host instead of `../../../bin/fm-watch-arm.sh`, with Claude's print mode as its headless engine; [`supervision-host.md`](../../../../../docs/supervision-host.md) owns the host. +In a home with `config/supervision-host` and no `config/supervision-host-off` the park runs the supervision host instead of `../../../bin/fm-watch-arm.sh`; [`supervision-host.md`](../../../../../docs/supervision-host.md) owns the host and which headless engine it runs. Cursor exposes 20 project events plus a Claude-Code compatibility map that loads `.claude/settings.json`. Tracked hooks register `stop`, `sessionStart`, and two `preToolUse` seatbelts through `$CURSOR_PROJECT_DIR`; Claude entries stand down on Cursor payloads under `../../../docs/turnend-guard.md`. diff --git a/.agents/skills/harness-adapters/references/harness/omp.md b/.agents/skills/harness-adapters/references/harness/omp.md index e9a4443fdad..b675c17341e 100644 --- a/.agents/skills/harness-adapters/references/harness/omp.md +++ b/.agents/skills/harness-adapters/references/harness/omp.md @@ -51,7 +51,7 @@ There is no `agent_settled` event; `agent_end` plus `willContinue` replaces it. The omp primary follows the Pi extension-owned watcher model through `../../../docs/supervision-protocols/omp.md`: `.omp/extensions/fm-primary-omp-watch.ts` arms `bin/fm-watch-arm.sh --restart` through the `fm_watch_arm_omp` tool and owns every successor, and `.omp/extensions/fm-primary-turnend-guard.ts` answers omp's blocking `session_stop` hook by forcing one continuation when `../../../bin/fm-turnend-guard.sh` returns 2, bounded per turn by omp's `stop_hook_active` flag. The same file ports the `tool_call` seatbelts and delivers the session-start digest through `before_agent_start` on the Run tier; omp's `session_start` carries no reason, so the source is derived (first start `startup` or `resume` from the launch line, later in-process starts `clear`, `session_compact` as `compact`). omp has no asynchronous Stop-hook equivalent, so the Claude auto-arm model does not apply; `fm_supervision_model` classifies omp as `extension`, and `fm_omp_extension_owns_supervision` in `../../../bin/fm-wake-lib.sh` is the ownership proof that tolerates the extension's own watcher hand-off. -The Pi supervision branch does not run on omp; without the supervision host every actionable wake is delivered to main, and in a home with `config/supervision-host` and no `config/supervision-host-off` the watch extension spawns the host instead of the arm, with Claude's print mode as its headless engine ([`supervision-host.md`](../../../../../docs/supervision-host.md)). +The Pi supervision branch does not run on omp; without the supervision host every actionable wake is delivered to main, and in a home with `config/supervision-host` and no `config/supervision-host-off` the watch extension spawns the host instead of the arm; [`supervision-host.md`](../../../../../docs/supervision-host.md) owns which headless engine that host runs. Launch a primary with plain `omp` inside the home (`FM_OMP_HARNESS=omp omp` when starting from a Claude pane); `../../../bin/fm-session-start.sh` prints `OMP_WATCH_EXTENSION: not loaded` when the running session has not loaded both tracked extensions. `FM_OMP_LIVE_E2E=1 ../../../tests/fm-omp-primary-live-e2e.test.sh` is the opt-in live guard; `../../../tests/fm-omp-harness.test.sh` is the portable regression. A secondmate registered with `remote=1` in `data/secondmates.md`, spawned through the ordinary `../../../bin/fm-spawn.sh --secondmate` path, is refused on omp until a remote host verifies it, as is `../../../bin/fm-remote-secondmate-control.sh launch`; there is no `--remote` flag. diff --git a/.agents/skills/harness-adapters/references/harness/opencode.md b/.agents/skills/harness-adapters/references/harness/opencode.md index 62a4beda863..c33b9a93534 100644 --- a/.agents/skills/harness-adapters/references/harness/opencode.md +++ b/.agents/skills/harness-adapters/references/harness/opencode.md @@ -43,7 +43,7 @@ The primary integration was verified on 2026-07-08 with OpenCode 1.17.6. `.opencode/plugins/fm-primary-turnend-guard.js` listens for `session.idle`. Throwing from `session.idle` does not block `opencode run`, so the primary adapter treats the event as passive and uses `client.session.promptAsync` to force one follow-up turn when `../../../bin/fm-turnend-guard.sh` returns 2. The follow-up was verified in the interactive TUI. -In a home with `config/supervision-host` and no `config/supervision-host-off` the watch-arm plugin spawns the supervision host instead of `../../../bin/fm-watch-arm.sh`, with Claude's print mode as its headless engine; [`supervision-host.md`](../../../../../docs/supervision-host.md) owns the host. +In a home with `config/supervision-host` and no `config/supervision-host-off` the watch-arm plugin spawns the supervision host instead of `../../../bin/fm-watch-arm.sh`; [`supervision-host.md`](../../../../../docs/supervision-host.md) owns the host and which headless engine it runs. `opencode run` can exit before displaying a queued follow-up, so the adapter steps aside in headless mode. On native Windows, the operational-input adapter runs its Bash helper through `bash`; macOS and Linux invoke it directly. diff --git a/docs/configuration.md b/docs/configuration.md index 6eac8400b9e..f12aaf79b5a 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -325,9 +325,8 @@ Grok's arm command is rendered at session start, so a change to its host mode ta - empty or `default` selects the primary harness's own engine at that engine's default model (`sonnet` for Claude, `grok-4.7` for Grok); - ` []` names a verified engine, `claude` or `grok`, and optionally the engine's own model name or alias; `default ` selects the primary harness's engine with that model. -A Cursor, OpenCode, omp, or Codex home names `claude` in the file, because those primaries have no verified engine of their own. -A Grok home may name `grok` or `claude`. -[supervision-host.md](supervision-host.md#engines) owns the verified-engine contract. +A Cursor, OpenCode, omp, or Codex home must name an engine in the file, because those primaries have no verified engine of their own. +[supervision-host.md](supervision-host.md#model-and-engine-selection) owns which engine each primary can use and the verified-engine contract. ### Failures, when changes apply, and inheritance diff --git a/docs/supervision-host.md b/docs/supervision-host.md index 2645e31c296..cf175b4cf9a 100644 --- a/docs/supervision-host.md +++ b/docs/supervision-host.md @@ -471,7 +471,7 @@ The default model is `sonnet` for Claude, which handled every measured wake corr Only a Claude primary runs the host when `config/supervision-host` is absent. An empty or `default` file selects the primary's own verified engine, so a Grok primary then uses `grok-4.7`. A Cursor, OpenCode, omp, or Codex home names `claude` (optionally with a model) until that primary has a verified engine of its own. -A Grok home may name `grok` or `claude`. +Any home may name `grok` or `claude`; a Grok engine handles away turns only, so on any primary an attended close reaches main unchanged. `/afk` says so when the file selects no engine. ## Verification