Skip to content
Merged
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
63 changes: 56 additions & 7 deletions bin/fm-procevent-when.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,24 @@
# never held this long after arming (default 604800)
# --condition-timeout <secs> per-poll bound on one condition run (default 60)
# --action-timeout <secs> bound on the action run (default 1800)
# --error-budget <n> consecutive condition errors tolerated
# before waking firstmate (default 3)
# The condition argv must exit 0 for true, 1 for a clean false;
# any other exit (or a per-poll timeout) is an error, never a true.
# --error-budget <n> consecutive transient condition errors
# tolerated before waking firstmate
# (default 3); a permanent error wakes
# at once, on its first observation
# The condition argv signals FOUR outcomes through its exit status, and
# a failure is never a false - only exit 1 means "not yet":
# 0 true: the condition holds for this poll
# 1 clean false: the condition is legitimately not met; keep waiting
# 2 permanent error: the binding cannot heal by waiting (a moved
# branch or HEAD, a missing tool or worktree, unreadable input);
# wake firstmate at once and never treat it as a false
# any other exit, or the per-poll timeout: transient error; keep
# polling with bounded backoff, and wake once --error-budget
# consecutive transient errors have accumulated
# The alert boundary is the terminal condition-error outcome: a
# permanent error and an exhausted transient budget both emit it, the
# action never runs, and the watch is left for firstmate to decide on
# rather than re-armed on its own.
# POLICY, not enforceable here: both halves must be exact and
# deterministic, and the action must be safe and reversible. Anything
# needing judgment, and anything destructive, irreversible, or
Expand Down Expand Up @@ -105,9 +119,11 @@ FM_ROOT_REAL=$(cd "$FM_ROOT" 2>/dev/null && pwd -P) || FM_ROOT_REAL=$FM_ROOT

WHEN_DIR="$STATE/when"
OUTPUT_TAIL_BYTES=${FM_WHEN_OUTPUT_TAIL_BYTES:-8192}
# Ceiling on the transient-error poll backoff, as a multiple of --interval.
WHEN_BACKOFF_CAP_FACTOR=8

die() { printf 'error: %s\n' "$1" >&2; exit 1; }
usage() { sed -n '2,72p' "${BASH_SOURCE[0]}" | sed 's/^# \{0,1\}//'; exit 2; }
usage() { sed -n '2,89p' "${BASH_SOURCE[0]}" | sed 's/^# \{0,1\}//'; exit 2; }

spec_file() { printf '%s/%s.spec\n' "$WHEN_DIR" "$1"; }
trust_file() { printf '%s/%s.trust\n' "$WHEN_DIR" "$1"; }
Expand Down Expand Up @@ -339,6 +355,29 @@ bounded_run() {
return "$rc"
}

# retry_sleep <consecutive-transient-errors>
# The wait before the next poll. A clean false keeps the registered cadence; a
# transient error backs off by doubling per consecutive error, capped at
# WHEN_BACKOFF_CAP_FACTOR times the cadence, so a flapping dependency is polled
# more gently than a healthy one and a sustained outage does not spin. A
# permanent error never reaches here: it wakes instead of waiting.
retry_sleep() {
local err=${1:-0} factor=1 i=1
if [ "$err" -le 0 ]; then
sleep "$SPEC_INTERVAL"
return 0
fi
while [ "$i" -lt "$err" ]; do
factor=$((factor * 2))
if [ "$factor" -ge "$WHEN_BACKOFF_CAP_FACTOR" ]; then
factor=$WHEN_BACKOFF_CAP_FACTOR
break
fi
i=$((i + 1))
done
sleep "$(awk -v i="$SPEC_INTERVAL" -v f="$factor" 'BEGIN { printf "%.3f", i * f }')"
}

# emit_doc <source-id> <status> <detail> <polls> <action-exit-or-empty> <output-file-or-empty>
# The single stdout writer of `run`: everything the generic runner captures.
emit_doc() {
Expand Down Expand Up @@ -410,17 +449,27 @@ cmd_run() {
consecutive_true=0
consecutive_err=0
;;
2)
# A permanent condition break cannot heal by waiting, so it wakes on its
# FIRST observation instead of burning the transient budget. This is the
# boundary that keeps a real failure from being read as an ordinary "not
# yet": the outcome is terminal, the action never runs, and the watch is
# left for firstmate to decide on rather than re-armed on its own.
emit_doc "$sid" condition-error \
"the condition reported a permanent failure (exit 2) on poll $polls; the action was not run" "$polls" '' "$out"
exit 0
;;
*)
consecutive_true=0
consecutive_err=$((consecutive_err + 1))
if [ "$consecutive_err" -ge "$SPEC_ERROR_BUDGET" ]; then
emit_doc "$sid" condition-error \
"the condition exited $rc on $consecutive_err consecutive polls; the action was not run" "$polls" '' "$out"
"the condition errored (exit $rc) on $consecutive_err consecutive polls; the action was not run" "$polls" '' "$out"
exit 0
fi
;;
esac
sleep "$SPEC_INTERVAL"
retry_sleep "$consecutive_err"
done

now=$(date +%s)
Expand Down
4 changes: 3 additions & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -2101,7 +2101,9 @@ One terminal outcome is then durably captured and published as a wake, which rem
The (condition, action) spec is stored privately under `state/when/` and hash-bound by a trust record the same way `bin/fm-check-register.sh` binds a custom check, while the spec separately binds the resolved action executable's bytes; a mutated or unregistered spec or a changed action executable is refused before the action runs, and that binding is reloaded from disk immediately before each fire rather than trusted from when polling started.
A repo update that fast-forwards an in-repo action's bytes in place would otherwise desync every already-armed watch's trust binding with no tampering involved; `bin/fm-procevent-when.sh rebind-all` re-hashes and republishes the binding for every registered watch whose action lives under `FM_ROOT`, including one already polling, so it keeps firing across such an update instead of being refused on its next fire.

Every failure path - a mutated spec or action executable, a condition error past its budget, an expired deadline, a failed action, or an earlier fire whose outcome was never captured - produces a terminal captured outcome that wakes firstmate rather than a silent retry, and a durable single-fire marker claimed before the action makes restarts and re-polls unable to fire it twice.
Every failure path - a mutated spec or action executable, a condition error, an expired deadline, a failed action, or an earlier fire whose outcome was never captured - produces a terminal captured outcome that wakes firstmate rather than a silent retry, and a durable single-fire marker claimed before the action makes restarts and re-polls unable to fire it twice.
The condition's exit status is the whole contract: 0 is a true, 1 is a clean false that keeps waiting, 2 is a permanent error that wakes firstmate on its first observation, and any other exit or a per-poll timeout is a transient error that keeps polling with bounded backoff until `--error-budget` consecutive transient errors have accumulated.
A failure is therefore never read as a condition that is merely not met: a permanent break and an exhausted transient budget both end in one terminal `condition-error` outcome that never runs the action, and the watch is left for firstmate to decide on rather than re-armed on its own.
The adapter automates only the exact deterministic subset: anything needing judgment, and anything destructive, irreversible, or security-sensitive, keeps the ordinary check-fires-then-firstmate-decides flow, and the adapter's header and `--help` own its commands, flags, and outcome document.

**Capture and publish results**
Expand Down
2 changes: 1 addition & 1 deletion docs/verification/process-event-sources.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ Exercised by `tests/fm-procevent.test.sh` against a fake blocking source whose c
| argv integrity | an argument containing spaces survives as one argument, a shell-looking argument is passed literally with no interpretation, and an unrepresentable newline is rejected at registration |
| bounded output | output beyond `FM_PROCEVENT_MAX_OUTPUT_BYTES` is drained while only the bound is staged, then truncated and captured |
| condition->action single-fire and trust | `tests/fm-procevent-when.test.sh` drives the public `when` adapter and generic runner with real commands, proving stable true fires once, a claimed fire restarts as ambiguous without a second action, concurrent arms publish one complete watch, and mutated specs or action executables are refused before execution |
| condition->action terminal outcomes | the same suite proves flapping true polls do not fire, action failure, condition error budget, deadline expiry, and a true poll completing after its deadline each produce the expected terminal captured result without an unsafe action |
| condition->action terminal outcomes | the same suite proves flapping true polls do not fire, action failure, a condition error budget, a permanent condition error waking on its first poll, transient errors backing off until their budget, deadline expiry, and a true poll completing after its deadline each produce the expected terminal captured result without an unsafe action |
| condition->action process bounds | the same suite proves action timeout terminates descendants and command-output staging remains within `FM_WHEN_OUTPUT_TAIL_BYTES` while the command runs |
| silent failure handling | a nonzero exit with no output publishes nothing and leaves the source registered for retry |
| inertness | a home with no registered source generates no state, starts no process, and does not need supervision |
Expand Down
9 changes: 6 additions & 3 deletions tests/fm-backend-herdr-presentation-e2e.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1308,7 +1308,10 @@ teardown_task "$CROSS_RESTART_ID" "$SECOND_HOME_A" > "$TMP_ROOT/cross-restart-te
pass "real Herdr lab: secondmate restart binding and reclaim stay isolated to the exact child home and parent"

# Two homes recovering concurrently serialize on the named session lock and
# each replace only their own exact husk.
# each replace only their own exact husk. Both resume spawns opt into waiting
# for that lock so one genuinely waits out the other's recovery instead of
# racing the bounded default acquire window; the default refusal under
# contention is proven separately by the lock-refuse case below.
PRIMARY_WAVE_ID=resume-wave-primary
BRAVO_WAVE_ID=resume-wave-bravo
mkdir -p "$HOME_DIR/data/$PRIMARY_WAVE_ID" "$SECOND_HOME_B/data/$BRAVO_WAVE_ID"
Expand All @@ -1331,9 +1334,9 @@ PATH="$HERDR_ORIGINAL_PATH" "$HERDR_LAB_HELPER" stop "$HERDR_LAB_SESSION" >/dev/
PATH="$HERDR_ORIGINAL_PATH" "$HERDR_LAB_HELPER" provision "$HERDR_LAB_SESSION" \
|| fail "could not reprovision the isolated session for concurrent recovery"
CONCURRENT_RECOVERY_FOCUS=$(focus_snapshot)
spawn_task "$PRIMARY_WAVE_ID" "$HOME_DIR" "$RECOVERY_PROJECT_DIR" > "$TMP_ROOT/primary-wave-resume.out" 2> "$TMP_ROOT/primary-wave-resume.err" &
spawn_task "$PRIMARY_WAVE_ID" "$HOME_DIR" "$RECOVERY_PROJECT_DIR" --herdr-resume-lock-wait > "$TMP_ROOT/primary-wave-resume.out" 2> "$TMP_ROOT/primary-wave-resume.err" &
PRIMARY_WAVE_PID=$!
spawn_task "$BRAVO_WAVE_ID" "$SECOND_HOME_B" "$RECOVERY_PROJECT_DIR" > "$TMP_ROOT/bravo-wave-resume.out" 2> "$TMP_ROOT/bravo-wave-resume.err" &
spawn_task "$BRAVO_WAVE_ID" "$SECOND_HOME_B" "$RECOVERY_PROJECT_DIR" --herdr-resume-lock-wait > "$TMP_ROOT/bravo-wave-resume.out" 2> "$TMP_ROOT/bravo-wave-resume.err" &
BRAVO_WAVE_PID=$!
wait "$PRIMARY_WAVE_PID" || fail "concurrent primary recovery failed: $(cat "$TMP_ROOT/primary-wave-resume.err")"
wait "$BRAVO_WAVE_PID" || fail "concurrent secondmate recovery failed: $(cat "$TMP_ROOT/bravo-wave-resume.err")"
Expand Down
Loading
Loading