Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ state/ runtime records and signals; gitignored
<id>.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)
<id>.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
<id>.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"
<id>.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
<id>.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
<id>.check-trust private content binding created by fm-check-register.sh for an intentional custom check
<id>.pr-poll private validated data sidecar for the byte-static PR merge poll
<id>.pr-poll-registration private transactional provenance record binding the task, canonical metadata identity, sidecar, and static poll publication
Expand All @@ -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-<task> 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)
Expand Down
16 changes: 11 additions & 5 deletions bin/fm-bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
14 changes: 11 additions & 3 deletions bin/fm-watch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down
109 changes: 108 additions & 1 deletion bin/fm-x-lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
# followups to 0
# fmx_meta_followups_set <meta> <n> - rewrite just the follow-up counter
# fmx_meta_link_clear <meta> - 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)"
Expand Down Expand Up @@ -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
Expand Down
84 changes: 84 additions & 0 deletions bin/fm-x-watch-register.sh
Original file line number Diff line number Diff line change
@@ -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 <absolute-exec-target>
#
# 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
# <sha256 of current state/x-watch.check.sh>
# <absolute path of the exec target>
# <sha256 of that target's current bytes>
#
# 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 <absolute-exec-target>" >&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"
5 changes: 3 additions & 2 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -502,15 +502,16 @@ 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.
While away mode is active the daemon owns the watcher and its default cadence applies; away-mode Relay cadence is a deferred follow-up.
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 <FMX_PAIRING_TOKEN>`.
HTTP 204 is silent.
Expand Down
1 change: 1 addition & 0 deletions docs/scripts.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
Loading
Loading