diff --git a/AGENTS.md b/AGENTS.md index 40bb092cb64..cc5d377d10f 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -102,7 +102,7 @@ state/ runtime records and signals; gitignored .inbox/ durable steering inbox: sequenced firstmate instruction records the worker acknowledges by moving them into its handled/ subdirectory; written by fm-send, with ordinary records re-rung and escalated by the watcher while explicit fire-and-forget records are excluded from that ladder, and removed by teardown (bin/fm-task-inbox-lib.sh) .meta task metadata; each producer script's header owns its exact fields and mutation contract, with docs/configuration.md routing operator-facing backend and trace-context details .herdr-presentation quarantinable attempt and restart-binding journal for Herdr's optional visual projection; never task or endpoint authority; see docs/herdr-backend.md "Presentation spaces" - .check.sh authenticated slow poll; the watcher dispatches validated PR data and the byte-identified Relay shim through trusted repository scripts, runs registered custom checks from hash-validated private snapshots, and rejects every other state check without execution + .check.sh authenticated slow poll; the watcher dispatches validated PR data through trusted repository scripts and the byte-bound Relay shim through its stock repository poll script or registered exec target, runs registered custom checks from hash-validated private snapshots, and rejects every other state check without execution .check-trust private content binding created by fm-check-register.sh for an intentional custom check .pr-poll private validated data sidecar for the byte-static PR merge poll .pr-poll-registration private transactional provenance record binding the task, canonical metadata identity, sidecar, and static poll publication @@ -113,6 +113,7 @@ state/ runtime records and signals; gitignored .branch-eligible-rows .branch-eligible-owner .main-eligible-rows per-actor wake-row claims and branch-owner evidence; docs/watcher-continuity.md owns the acknowledgement contract .lease- per-task supervision lease naming which actor (main or branch) may change that task; bin/fm-lease-lib.sh owns the contract the guarded scripts enforce x-watch.check.sh generated Relay poll shim; present only when opted in (section 14) + x-watch.poll-trust optional alternate Relay poll binding written only by bin/fm-x-watch-register.sh tool-updates.check.sh generated watched-tool update poll shim and its .check-trust binding; present only after bin/fm-tool-update-check.sh arm; its report record .tool-updates is what keeps one pending update from being reported on every poll pending-replies/ parent-owned secondmate pending-reply records (correlation id, delivery vs reply, recovery, escalation); fm-pending-reply-lib.sh procevent/ registered process-to-event sources, one private record per canonical source id; written only by bin/fm-procevent.sh, and their presence alone keeps supervision required (section 13) diff --git a/bin/fm-bootstrap.sh b/bin/fm-bootstrap.sh index f04d0cbefae..89fac79f719 100755 --- a/bin/fm-bootstrap.sh +++ b/bin/fm-bootstrap.sh @@ -986,7 +986,9 @@ x_mode_remove_artifact() { # wire the relay poll into the existing authenticated watcher dispatch. # Drops two idempotent, gitignored artifacts: # state/x-watch.check.sh - byte-static identity shim; the watcher validates -# its bytes and invokes bin/fm-x-poll.sh directly +# its bytes and invokes bin/fm-x-poll.sh directly, +# or a still-matching registered alternate (see +# bin/fm-x-watch-register.sh) without clobbering it # config/x-mode.env - exports FM_CHECK_INTERVAL=30, sourced by the watcher # arm so only an X instance polls at the 30s cadence # On opt-out (no token, or empty) it removes any such artifacts so the instance @@ -1067,10 +1069,14 @@ x_mode_setup() { || { fmx_arm_failed; return 0; } ;; esac - shim_body=$(fmx_poll_shim_content "$shim_home" "$FM_ROOT") - x_mode_write_if_changed "$shim" "$shim_body" 700 || { fmx_arm_failed; return 0; } - fmx_poll_shim_valid "$shim" "$shim_home" "$FM_ROOT" \ - || { fmx_arm_failed; return 0; } + if fmx_poll_shim_registered_valid "$shim" "$shim_home" "$FM_ROOT"; then + : + else + shim_body=$(fmx_poll_shim_content "$shim_home" "$FM_ROOT") + x_mode_write_if_changed "$shim" "$shim_body" 700 || { fmx_arm_failed; return 0; } + fmx_poll_shim_stock_valid "$shim" "$shim_home" "$FM_ROOT" \ + || { fmx_arm_failed; return 0; } + fi cadence_body=$(cat <<'EOF' # Auto-generated by fm-bootstrap.sh - X mode watcher cadence. diff --git a/bin/fm-watch.sh b/bin/fm-watch.sh index b9a9f3c10ef..484c9c61cd8 100755 --- a/bin/fm-watch.sh +++ b/bin/fm-watch.sh @@ -1543,16 +1543,21 @@ while :; do # CHECK_INTERVAL, so most cycles skip this block and fall straight through. if [ "$(age_of "$STATE/.last-check")" -ge "$CHECK_INTERVAL" ]; then rejected_checks= + x_watch_reregister_hint= for c in "$STATE"/*.check.sh; do [ -e "$c" ] || continue is_pr_poll=0 if [ "$(basename "$c")" = x-watch.check.sh ]; then - if fmx_poll_shim_valid "$c" "$FM_HOME" "$FM_ROOT" \ - && [ -f "$FM_ROOT/bin/fm-x-poll.sh" ] && [ ! -L "$FM_ROOT/bin/fm-x-poll.sh" ]; then - FM_HOME="$FM_HOME" run_check_capture "$FM_ROOT/bin/fm-x-poll.sh" || exit 1 + x_watch_exec= + if x_watch_exec=$(fmx_poll_exec_target "$c" "$FM_HOME" "$FM_ROOT"); then + FM_HOME="$FM_HOME" run_check_capture "$x_watch_exec" || exit 1 out=$FM_CHECK_RESULT else rejected_checks="$rejected_checks $c" + if ! fmx_poll_shim_stock_valid "$c" "$FM_HOME" "$FM_ROOT" \ + && fmx_poll_shim_trust_present "$c"; then + x_watch_reregister_hint=$(fmx_poll_shim_reregister_hint "$c") + fi continue fi else @@ -1603,6 +1608,9 @@ while :; do done if [ -n "$rejected_checks" ]; then reason="check: rejected unauthenticated state checks:$rejected_checks" + if [ -n "$x_watch_reregister_hint" ]; then + reason="$reason; $x_watch_reregister_hint" + fi fm_wake_append check unauthenticated-state-checks "$reason" || exit 1 touch "$STATE/.last-check" wake "$reason" diff --git a/bin/fm-x-lib.sh b/bin/fm-x-lib.sh index e6976664350..b410e8cb8c4 100644 --- a/bin/fm-x-lib.sh +++ b/bin/fm-x-lib.sh @@ -46,6 +46,8 @@ # followups to 0 # fmx_meta_followups_set - rewrite just the follow-up counter # fmx_meta_link_clear - remove the X-request link entirely +# fmx_poll_shim_* - stock or registered Relay poll-shim identity; +# bin/fm-x-watch-register.sh owns the binding record # Callers must have FM_HOME set before calling fmx_load_config. _FM_X_LIB_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" @@ -235,12 +237,117 @@ fmx_poll_shim_private_identity_valid() { fmx_poll_shim_identity_valid "$1" 700 } -fmx_poll_shim_valid() { +fmx_sha256() { + local file=$1 + if command -v shasum >/dev/null 2>&1; then + shasum -a 256 "$file" 2>/dev/null | awk '{print $1}' + elif command -v sha256sum >/dev/null 2>&1; then + sha256sum "$file" 2>/dev/null | awk '{print $1}' + else + return 1 + fi +} + +fmx_poll_shim_stock_valid() { local file=$1 home=$2 root=$3 fmx_poll_shim_private_identity_valid "$file" || return 1 cmp -s "$file" <(fmx_poll_shim_content "$home" "$root") } +# Binding record path, format, and mutation contract: bin/fm-x-watch-register.sh. +fmx_poll_shim_trust_path() { + local file=$1 dir + [ -n "$file" ] || return 1 + [ "$(basename -- "$file")" = x-watch.check.sh ] || return 1 + dir=$(dirname -- "$file") + printf '%s/x-watch.poll-trust\n' "$dir" +} + +fmx_poll_shim_trust_present() { + local file=$1 trust + trust=$(fmx_poll_shim_trust_path "$file") || return 1 + [ -e "$trust" ] || [ -L "$trust" ] +} + +fmx_poll_exec_target_path_valid() { + local path=$1 + case "$path" in + /*) ;; + *) return 1 ;; + esac + case "$path" in + *$'\n'*|*$'\r'*) return 1 ;; + esac + [ -n "$path" ] +} + +fmx_poll_shim_trust_read() { + local file=$1 trust dir device version shim_hash target target_hash + FMX_POLL_TRUST_SHIM_HASH= + FMX_POLL_TRUST_TARGET= + FMX_POLL_TRUST_TARGET_HASH= + trust=$(fmx_poll_shim_trust_path "$file") || return 1 + dir=$(dirname -- "$file") + [ -d "$dir" ] && [ ! -L "$dir" ] || return 1 + if [ "$(uname)" = Darwin ]; then + device=$(stat -f %d "$dir" 2>/dev/null) || return 1 + else + device=$(stat -c %d "$dir" 2>/dev/null) || return 1 + fi + fmx_single_link_file_mode_valid "$trust" 600 "$device" || return 1 + exec 9< "$trust" || return 1 + IFS= read -r version <&9 || { exec 9<&-; return 1; } + IFS= read -r shim_hash <&9 || { exec 9<&-; return 1; } + IFS= read -r target <&9 || { exec 9<&-; return 1; } + IFS= read -r target_hash <&9 || { exec 9<&-; return 1; } + if IFS= read -r _extra <&9; then + exec 9<&- + return 1 + fi + exec 9<&- + [ "$version" = fm-x-watch-poll-v1 ] || return 1 + [[ "$shim_hash" =~ ^[0-9a-f]{64}$ ]] || return 1 + [[ "$target_hash" =~ ^[0-9a-f]{64}$ ]] || return 1 + fmx_poll_exec_target_path_valid "$target" || return 1 + FMX_POLL_TRUST_SHIM_HASH=$shim_hash + FMX_POLL_TRUST_TARGET=$target + FMX_POLL_TRUST_TARGET_HASH=$target_hash +} + +fmx_poll_shim_registered_valid() { + local file=$1 hash + fmx_poll_shim_private_identity_valid "$file" || return 1 + fmx_poll_shim_trust_read "$file" || return 1 + hash=$(fmx_sha256 "$file") || return 1 + [ "$hash" = "$FMX_POLL_TRUST_SHIM_HASH" ] || return 1 + [ -f "$FMX_POLL_TRUST_TARGET" ] && [ ! -L "$FMX_POLL_TRUST_TARGET" ] || return 1 + hash=$(fmx_sha256 "$FMX_POLL_TRUST_TARGET") || return 1 + [ "$hash" = "$FMX_POLL_TRUST_TARGET_HASH" ] +} + +fmx_poll_exec_target() { + local file=$1 home=$2 root=$3 + if fmx_poll_shim_stock_valid "$file" "$home" "$root" \ + && [ -f "$root/bin/fm-x-poll.sh" ] && [ ! -L "$root/bin/fm-x-poll.sh" ]; then + printf '%s\n' "$root/bin/fm-x-poll.sh" + return 0 + fi + if fmx_poll_shim_registered_valid "$file" "$home" "$root"; then + printf '%s\n' "$FMX_POLL_TRUST_TARGET" + return 0 + fi + return 1 +} + +fmx_poll_shim_reregister_hint() { + local file=$1 + if fmx_poll_shim_trust_read "$file" && [ -n "$FMX_POLL_TRUST_TARGET" ]; then + printf 're-register with bin/fm-x-watch-register.sh %s' "$FMX_POLL_TRUST_TARGET" + else + printf 're-register with bin/fm-x-watch-register.sh' + fi +} + # Resolve the X-mode settings into FMX_TOKEN, FMX_RELAY, FMX_DRY, FMX_MAX, # FMX_DISCORD_MAX, and FMX_THREAD_MAX. An explicit environment variable always # wins over the .env file; the relay URL defaults to the production host so a diff --git a/bin/fm-x-watch-register.sh b/bin/fm-x-watch-register.sh new file mode 100755 index 00000000000..22ca7e95978 --- /dev/null +++ b/bin/fm-x-watch-register.sh @@ -0,0 +1,84 @@ +#!/usr/bin/env bash +# Bind the current alternate Relay poll shim and its exec target. +# Usage: fm-x-watch-register.sh +# +# Homes without a registration keep the stock generated shim and byte-static +# dispatch. This command is the only writer of the binding; bootstrap never +# creates it. The watcher and bootstrap both honor a still-matching binding and +# treat a stale one as unauthenticated (bootstrap then regenerates stock). +# +# Binding record: state/x-watch.poll-trust, mode 0600, never auto-created. +# Four lines and no extra: +# fm-x-watch-poll-v1 +# +# +# +# +# The shim must already be the alternate (not stock). The target must be an +# absolute regular non-symlink file outside state/. Re-running overwrites the +# binding with the current bytes. bin/fm-x-lib.sh is the validator for this +# record. +set -u + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +FM_ROOT="${FM_ROOT_OVERRIDE:-$(cd "$SCRIPT_DIR/.." && pwd)}" +FM_HOME="${FM_HOME:-${FM_ROOT_OVERRIDE:-$FM_ROOT}}" +STATE="${FM_STATE_OVERRIDE:-$FM_HOME/state}" + +# shellcheck source=bin/fm-pr-lib.sh +. "$SCRIPT_DIR/fm-pr-lib.sh" +# shellcheck source=bin/fm-x-lib.sh +. "$SCRIPT_DIR/fm-x-lib.sh" + +usage() { + echo "usage: fm-x-watch-register.sh " >&2 + exit 2 +} + +[ "$#" -eq 1 ] || usage +case "$1" in + -h|--help) usage ;; +esac + +TARGET=$1 +CHECK="$STATE/x-watch.check.sh" +TRUST="$STATE/x-watch.poll-trust" + +[ -d "$STATE" ] && [ ! -L "$STATE" ] || { echo "error: state directory is unavailable" >&2; exit 1; } +[ -f "$CHECK" ] && [ ! -L "$CHECK" ] || { echo "error: poll shim is unavailable" >&2; exit 1; } +fmx_poll_exec_target_path_valid "$TARGET" \ + || { echo "error: exec target must be an absolute path" >&2; exit 1; } +case "$TARGET" in + "$STATE"|"$STATE"/*) + echo "error: exec target must not live under state/" >&2 + exit 1 + ;; +esac +[ "$TARGET" != "$CHECK" ] || { echo "error: exec target must not be the poll shim" >&2; exit 1; } +[ -f "$TARGET" ] && [ ! -L "$TARGET" ] \ + || { echo "error: exec target is unavailable" >&2; exit 1; } + +STATE_DEVICE=$(fm_pr_file_device "$STATE") || exit 1 +fm_pr_private_file_valid "$CHECK" 700 "$STATE_DEVICE" \ + || { echo "error: poll shim is unavailable" >&2; exit 1; } +if fmx_poll_shim_stock_valid "$CHECK" "$FM_HOME" "$FM_ROOT"; then + echo "error: shim matches the stock Relay poll identity; an alternate shim is required" >&2 + exit 1 +fi +fm_pr_regular_destination_on_device_or_absent "$TRUST" "$STATE_DEVICE" \ + || { echo "error: poll trust path is unavailable" >&2; exit 1; } + +SHIM_HASH=$(fmx_sha256 "$CHECK") || { echo "error: poll shim hash is unavailable" >&2; exit 1; } +TARGET_HASH=$(fmx_sha256 "$TARGET") || { echo "error: exec target hash is unavailable" >&2; exit 1; } + +umask 077 +TMP=$(mktemp "$STATE/.fm-x-watch-poll-trust.XXXXXX") || exit 1 +trap '[ -z "$TMP" ] || rm -f -- "$TMP"' EXIT HUP INT TERM +printf '%s\n%s\n%s\n%s\n' fm-x-watch-poll-v1 "$SHIM_HASH" "$TARGET" "$TARGET_HASH" > "$TMP" || exit 1 +chmod 0600 "$TMP" || exit 1 +fm_pr_regular_destination_on_device_or_absent "$TRUST" "$STATE_DEVICE" || exit 1 +mv -f -- "$TMP" "$TRUST" || exit 1 +TMP= +fmx_poll_shim_registered_valid "$CHECK" "$FM_HOME" "$FM_ROOT" \ + || { rm -f -- "$TRUST"; echo "error: poll shim registration is invalid" >&2; exit 1; } +printf 'registered: state/x-watch.check.sh -> %s\n' "$TARGET" diff --git a/docs/configuration.md b/docs/configuration.md index 99e1c1fd608..1ad70c64c47 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -502,7 +502,8 @@ The dashboard owns account creation, identity linking, bot installation, and tok The locked session-start bootstrap step turns the token into local generated state. It writes `state/x-watch.check.sh`, a byte-static identity shim for `bin/fm-x-poll.sh`, and `config/x-mode.env`, which exports `FM_CHECK_INTERVAL=30` for watcher processes in that home. -The watcher accepts the shim only when its bytes match the expected generated content, then invokes the trusted repository poll script directly instead of executing state-file source. +The watcher accepts the stock shim only when its bytes match the expected generated content, then invokes the trusted repository poll script directly instead of executing state-file source. +An explicit `bin/fm-x-watch-register.sh` registration can authenticate an alternate shim and exec target; that command's header owns the binding, and homes without a registration stay on the stock generated shim and byte-static dispatch. This section is the single owner of the Relay cadence contract: a Relay instance polls every 30 seconds instead of the default 300, only a Relay instance speeds up because a non-Relay home has no `config/x-mode.env`, and the session-start supervision operating block includes the cadence instruction when that file exists. The active primary-harness supervision protocol owns how that sourced cadence reaches the watcher process. Because `bin/fm-watch.sh` reads `FM_CHECK_INTERVAL` only at process start, a cadence transition - opt-in while a watcher is already running, or opt-out - is applied by restarting the home-scoped watcher through the emitted harness protocol; bootstrap deliberately never restarts the watcher itself. @@ -510,7 +511,7 @@ While away mode is active the daemon owns the watcher and its default cadence ap When the token is removed or empty, the next locked session-start bootstrap step removes those artifacts. Steady-state off is silent and writes nothing. Relay remains additive to non-Relay lifecycle behavior: homes without the generated artifacts keep the default watcher cadence and do not run the Relay poll. -Its request handling remains in Relay-specific `bin/` scripts and the `fmx-respond` skill, while the watcher owns authenticated dispatch from the generated local identity shim. +Its request handling remains in Relay-specific `bin/` scripts and the `fmx-respond` skill, while the watcher owns authenticated dispatch from the stock or registered local identity shim. `bin/fm-x-poll.sh` calls `GET /connector/poll` with `Authorization: Bearer `. HTTP 204 is silent. diff --git a/docs/scripts.md b/docs/scripts.md index 5bbcebfdf1b..0d22c886363 100644 --- a/docs/scripts.md +++ b/docs/scripts.md @@ -132,6 +132,7 @@ The shared no-mistakes gate refusal for fleet lifecycle entrypoints is summarize | `fm-lock.sh` | Per-home firstmate session lock | | `fm-x-lib.sh` | Shared Relay config, relay, and reply-threading helpers | | `fm-x-poll.sh` | One bounded Relay poll: stash newly offered mentions and emit their once-only wake | +| `fm-x-watch-register.sh` | Bind an alternate Relay poll shim and its exec target; header owns the binding record | | `fm-x-reply.sh` | Post or dry-run preview a composed Relay reply or follow-up | | `fm-x-dismiss.sh` | Dismiss a skipped Relay mention at the relay without replying | | `fm-x-link.sh` | Link a spawned task to its originating Relay mention in task meta | diff --git a/tests/fm-x-watch-register.test.sh b/tests/fm-x-watch-register.test.sh new file mode 100755 index 00000000000..f4164649587 --- /dev/null +++ b/tests/fm-x-watch-register.test.sh @@ -0,0 +1,262 @@ +#!/usr/bin/env bash +# Behavior tests for alternate Relay poll-shim registration: stock remains +# byte-static, a valid binding is honored by the watcher and preserved across +# bootstrap regeneration, and unregistered or stale variants are refused. +set -u + +# shellcheck source=tests/lib.sh +. "$(dirname "${BASH_SOURCE[0]}")/lib.sh" +# shellcheck source=bin/fm-x-lib.sh +. "$ROOT/bin/fm-x-lib.sh" + +REGISTER="$ROOT/bin/fm-x-watch-register.sh" +WATCH="$ROOT/bin/fm-watch.sh" +BOOTSTRAP="$ROOT/bin/fm-bootstrap.sh" +TMP_ROOT=$(fm_test_tmproot fm-x-watch-register) + +path_mode() { + if [ "$(uname)" = Darwin ]; then + stat -f %Lp "$1" + else + stat -c %a "$1" + fi +} + +make_home() { + local name=$1 home + home="$TMP_ROOT/$name" + mkdir -p "$home/state" "$home/data" "$home/config" + printf '%s\n' "$home" +} + +write_stock_shim() { + local home=$1 + mkdir -p "$home/state" + fmx_poll_shim_content "$home" "$ROOT" > "$home/state/x-watch.check.sh" + chmod 700 "$home/state/x-watch.check.sh" +} + +write_alt_shim() { + local home=$1 target=$2 + mkdir -p "$home/state" + { + printf '%s\n' '#!/usr/bin/env bash' + printf '%s\n' '# test alternate Relay poll shim' + printf 'export FM_HOME=%s\n' "$(printf '%q' "$home")" + printf 'exec %s\n' "$(printf '%q' "$target")" + } > "$home/state/x-watch.check.sh" + chmod 700 "$home/state/x-watch.check.sh" +} + +write_wrapper() { + local path=$1 line=${2:-alt-poll-ok} + cat > "$path" < "$fakebin/curl" <<'SH' +#!/usr/bin/env bash +ofile="" +while [ $# -gt 0 ]; do + case "$1" in + -o) ofile=$2; shift 2 ;; + -m|-w|-H) shift 2 ;; + -s) shift ;; + http://*|https://*) shift ;; + *) shift ;; + esac +done +[ -n "$ofile" ] && : > "$ofile" +printf '%s' "${FAKE_POLL_CODE:-401}" +exit 0 +SH + chmod +x "$fakebin/curl" + printf '%s\n' "$fakebin" +} + +run_watch() { + perl -e 'my $pid=fork; die unless defined $pid; if (!$pid) { exec @ARGV } local $SIG{ALRM}=sub { kill "TERM", $pid; waitpid $pid, 0; exit 124 }; alarm 10; waitpid $pid, 0; alarm 0; exit($? >> 8)' \ + env "$@" "$WATCH" +} + +test_stock_shim_validates_through_watcher() { + local home fakebin rc + home=$(make_home stock) + write_stock_shim "$home" + printf 'FMX_PAIRING_TOKEN=tok-stock\n' > "$home/.env" + fakebin=$(make_fake_curl "$home") + set +e + PATH="$fakebin:$PATH" FAKE_POLL_CODE=401 \ + run_watch FM_HOME="$home" FM_ROOT_OVERRIDE="$ROOT" FM_CHECK_INTERVAL=0 \ + FM_CHECK_TIMEOUT=2 FM_POLL=0.02 FM_HEARTBEAT=999999 FM_SIGNAL_GRACE=0 \ + > "$home/watch.out" 2> "$home/watch.err" + rc=$? + set -e + [ "$rc" -eq 0 ] || fail "stock watcher cycle failed (rc=$rc): $(cat "$home/watch.err")" + assert_contains "$(cat "$home/watch.out")" "x-mode-error relay returned HTTP 401" \ + "stock shim must dispatch the trusted poll script" + assert_absent "$home/state/x-watch.poll-trust" \ + "stock path must not auto-create a poll-trust binding" + fmx_poll_shim_stock_valid "$home/state/x-watch.check.sh" "$home" "$ROOT" \ + || fail "stock shim must validate as stock" + pass "stock Relay poll shim validates through the watcher path" +} + +test_registered_variant_validates_through_watcher() { + local home wrapper rc + home=$(make_home registered-watch) + wrapper="$home/fm-x-poll-harbor.sh" + write_wrapper "$wrapper" alt-poll-ok + write_alt_shim "$home" "$wrapper" + FM_HOME="$home" "$REGISTER" "$wrapper" > "$home/register.out" \ + || fail "register failed: $(cat "$home/register.out" 2>/dev/null || true)" + assert_contains "$(cat "$home/register.out")" "registered: state/x-watch.check.sh -> $wrapper" \ + "register must report the bound target" + [ "$(path_mode "$home/state/x-watch.poll-trust")" = 600 ] \ + || fail "binding must be mode 0600" + set +e + run_watch FM_HOME="$home" FM_ROOT_OVERRIDE="$ROOT" FM_CHECK_INTERVAL=0 \ + FM_CHECK_TIMEOUT=2 FM_POLL=0.02 FM_HEARTBEAT=999999 FM_SIGNAL_GRACE=0 \ + > "$home/watch.out" 2> "$home/watch.err" + rc=$? + set -e + [ "$rc" -eq 0 ] || fail "registered watcher cycle failed (rc=$rc): $(cat "$home/watch.err")" + assert_contains "$(cat "$home/watch.out")" "alt-poll-ok" \ + "watcher must dispatch the registered exec target" + assert_not_contains "$(cat "$home/watch.out")" "rejected unauthenticated" \ + "a valid registration must not be rejected" + pass "registered alternate shim validates through the watcher path" +} + +test_unregistered_rewrite_is_rejected() { + local home wrapper rc + home=$(make_home unregistered) + wrapper="$home/fm-x-poll-harbor.sh" + write_wrapper "$wrapper" should-not-run + write_alt_shim "$home" "$wrapper" + set +e + run_watch FM_HOME="$home" FM_ROOT_OVERRIDE="$ROOT" FM_CHECK_INTERVAL=0 \ + FM_CHECK_TIMEOUT=2 FM_POLL=0.02 FM_HEARTBEAT=999999 FM_SIGNAL_GRACE=0 \ + > "$home/watch.out" 2> "$home/watch.err" + rc=$? + set -e + [ "$rc" -eq 0 ] || fail "unregistered watcher cycle failed (rc=$rc): $(cat "$home/watch.err")" + assert_contains "$(cat "$home/watch.out")" "rejected unauthenticated state checks:" \ + "unregistered rewrite must be rejected without execution" + assert_contains "$(cat "$home/watch.out")" "x-watch.check.sh" \ + "rejection must name the unauthenticated shim" + assert_not_contains "$(cat "$home/watch.out")" "should-not-run" \ + "unregistered rewrite must not execute the wrapper" + assert_not_contains "$(cat "$home/watch.out")" "re-register with bin/fm-x-watch-register.sh" \ + "unregistered rewrite must keep today's rejection wording" + pass "unregistered rewrite is rejected without execution" +} + +test_tampered_target_is_rejected_and_named() { + local home wrapper rc + home=$(make_home tampered) + wrapper="$home/fm-x-poll-harbor.sh" + write_wrapper "$wrapper" should-not-run + write_alt_shim "$home" "$wrapper" + FM_HOME="$home" "$REGISTER" "$wrapper" >/dev/null \ + || fail "could not register before tamper" + printf '# tampered\n' >> "$wrapper" + set +e + run_watch FM_HOME="$home" FM_ROOT_OVERRIDE="$ROOT" FM_CHECK_INTERVAL=0 \ + FM_CHECK_TIMEOUT=2 FM_POLL=0.02 FM_HEARTBEAT=999999 FM_SIGNAL_GRACE=0 \ + > "$home/watch.out" 2> "$home/watch.err" + rc=$? + set -e + [ "$rc" -eq 0 ] || fail "tampered watcher cycle failed (rc=$rc): $(cat "$home/watch.err")" + assert_contains "$(cat "$home/watch.out")" "rejected unauthenticated state checks:" \ + "tampered target must be rejected" + assert_not_contains "$(cat "$home/watch.out")" "should-not-run" \ + "tampered target must not execute" + assert_contains "$(cat "$home/watch.out")" "re-register with bin/fm-x-watch-register.sh $wrapper" \ + "stale-target rejection must name the re-register command and bound path" + pass "tampered registered target is rejected and names the re-register command" +} + +test_bootstrap_preserves_valid_registration() { + local home wrapper before after rc + home=$(make_home boot-preserve) + printf 'FMX_PAIRING_TOKEN=tok-preserve\n' > "$home/.env" + wrapper="$home/fm-x-poll-harbor.sh" + write_wrapper "$wrapper" alt-poll-ok + write_alt_shim "$home" "$wrapper" + FM_HOME="$home" "$REGISTER" "$wrapper" >/dev/null \ + || fail "could not register before bootstrap preserve" + before=$(cat "$home/state/x-watch.check.sh") + FM_HOME="$home" FM_BOOTSTRAP_NETWORK=skip "$BOOTSTRAP" >/dev/null 2>"$home/bootstrap.err" \ + || fail "bootstrap failed while preserving registration: $(cat "$home/bootstrap.err")" + after=$(cat "$home/state/x-watch.check.sh") + [ "$before" = "$after" ] || fail "bootstrap clobbered a still-valid registered shim" + fmx_poll_shim_registered_valid "$home/state/x-watch.check.sh" "$home" "$ROOT" \ + || fail "preserved shim must still be a valid registration" + ! fmx_poll_shim_stock_valid "$home/state/x-watch.check.sh" "$home" "$ROOT" \ + || fail "preserved registered shim must not match stock" + set +e + run_watch FM_HOME="$home" FM_ROOT_OVERRIDE="$ROOT" FM_CHECK_INTERVAL=0 \ + FM_CHECK_TIMEOUT=2 FM_POLL=0.02 FM_HEARTBEAT=999999 FM_SIGNAL_GRACE=0 \ + > "$home/watch.out" 2> "$home/watch.err" + rc=$? + set -e + [ "$rc" -eq 0 ] || fail "preserved watcher cycle failed (rc=$rc): $(cat "$home/watch.err")" + assert_contains "$(cat "$home/watch.out")" "alt-poll-ok" \ + "bootstrap-preserved registration must still dispatch the bound target" + pass "bootstrap regeneration preserves a valid registered variant" +} + +test_bootstrap_replaces_stale_registration_with_stock() { + local home wrapper + home=$(make_home boot-stale) + printf 'FMX_PAIRING_TOKEN=tok-stale\n' > "$home/.env" + wrapper="$home/fm-x-poll-harbor.sh" + write_wrapper "$wrapper" alt-poll-ok + write_alt_shim "$home" "$wrapper" + FM_HOME="$home" "$REGISTER" "$wrapper" >/dev/null \ + || fail "could not register before stale bootstrap" + printf '# harbor redeploy\n' >> "$wrapper" + FM_HOME="$home" FM_BOOTSTRAP_NETWORK=skip "$BOOTSTRAP" >/dev/null 2>"$home/bootstrap.err" \ + || fail "bootstrap failed while replacing stale registration: $(cat "$home/bootstrap.err")" + fmx_poll_shim_stock_valid "$home/state/x-watch.check.sh" "$home" "$ROOT" \ + || fail "stale registration must be replaced with the stock shim" + ! fmx_poll_shim_registered_valid "$home/state/x-watch.check.sh" "$home" "$ROOT" \ + || fail "stale binding must not remain valid after bootstrap" + assert_present "$home/state/x-watch.poll-trust" \ + "bootstrap must not auto-delete an operator-written binding" + pass "bootstrap replaces a stale registered variant with stock" +} + +test_register_refuses_stock_shim() { + local home err rc + home=$(make_home refuse-stock) + write_stock_shim "$home" + write_wrapper "$home/wrapper.sh" unused + set +e + FM_HOME="$home" "$REGISTER" "$home/wrapper.sh" >"$home/out" 2>"$home/err" + rc=$? + set -e + err=$(cat "$home/err") + [ "$rc" -ne 0 ] || fail "register accepted a stock shim" + assert_contains "$err" "alternate shim is required" \ + "register must refuse the stock identity" + assert_absent "$home/state/x-watch.poll-trust" \ + "refused register must not create a binding" + pass "register refuses to bind the stock Relay poll shim" +} + +test_stock_shim_validates_through_watcher +test_registered_variant_validates_through_watcher +test_unregistered_rewrite_is_rejected +test_tampered_target_is_rejected_and_named +test_bootstrap_preserves_valid_registration +test_bootstrap_replaces_stale_registration_with_stock +test_register_refuses_stock_shim