From d5062fca72bd39214936c0c4366cea645e75b935 Mon Sep 17 00:00:00 2001 From: Alan Domio Date: Wed, 9 Sep 2026 22:17:38 +0200 Subject: [PATCH 1/6] fix(bin): throttle idle heartbeat and reap cadence in the remote job worker The serve loop wrote a heartbeat (mktemp+chmod+mv) and rescanned the job queue for reaping on every 50ms tick even when idle, while the only heartbeat reader tolerates 10s of staleness and the reap throttle was already dead code (reset and tested in the same iteration). Gate both to their own SECONDS-based cadence (default 3s heartbeat, 60s reap) and leave job pickup on the unchanged fast poll tick. --- bin/fm-remote-job-lib.sh | 11 ++++++- bin/fm-remote-job-worker.sh | 30 ++++++++++++------- tests/fm-remote-job.test.sh | 59 +++++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+), 11 deletions(-) diff --git a/bin/fm-remote-job-lib.sh b/bin/fm-remote-job-lib.sh index 73bffa54c70..8b794e3187a 100755 --- a/bin/fm-remote-job-lib.sh +++ b/bin/fm-remote-job-lib.sh @@ -57,6 +57,12 @@ FM_REMOTE_JOB_TIMEOUT=${FM_REMOTE_JOB_TIMEOUT:-360} FM_REMOTE_JOB_WAIT_GRACE=${FM_REMOTE_JOB_WAIT_GRACE:-30} FM_REMOTE_JOB_POLL_SECONDS=${FM_REMOTE_JOB_POLL_SECONDS:-0.05} FM_REMOTE_JOB_REAP_SECONDS=${FM_REMOTE_JOB_REAP_SECONDS:-3600} +FM_REMOTE_JOB_HEARTBEAT_INTERVAL_SECONDS=${FM_REMOTE_JOB_HEARTBEAT_INTERVAL_SECONDS:-3} +FM_REMOTE_JOB_REAP_INTERVAL_SECONDS=${FM_REMOTE_JOB_REAP_INTERVAL_SECONDS:-60} +# The single owner of fm_remote_job_probe's freshness window, so the heartbeat +# interval bound below cannot silently drift out of sync with what a prober +# actually tolerates. +FM_REMOTE_JOB_PROBE_FRESHNESS_SECONDS=10 # shellcheck disable=SC2034 # Shared protocol constant consumed by the worker and sourcing callers. FM_REMOTE_JOB_PREEMPTED_EXIT=76 FM_REMOTE_JOB_OPERATOR_PATH= @@ -93,6 +99,9 @@ fm_remote_job_validate_settings() { case "$FM_REMOTE_JOB_WAIT_GRACE" in ''|*[!0-9]*) return 1 ;; esac [ "$FM_REMOTE_JOB_WAIT_GRACE" -le 300 ] || return 1 case "$FM_REMOTE_JOB_REAP_SECONDS" in ''|*[!0-9]*|0) return 1 ;; esac + case "$FM_REMOTE_JOB_HEARTBEAT_INTERVAL_SECONDS" in ''|*[!0-9]*|0) return 1 ;; esac + [ "$FM_REMOTE_JOB_HEARTBEAT_INTERVAL_SECONDS" -lt "$FM_REMOTE_JOB_PROBE_FRESHNESS_SECONDS" ] || return 1 + case "$FM_REMOTE_JOB_REAP_INTERVAL_SECONDS" in ''|*[!0-9]*|0) return 1 ;; esac return 0 } @@ -881,7 +890,7 @@ fm_remote_job_probe() { # ; a fresh worker heartbeat or active job mtime=$(fm_remote_job_path_mtime "$ready" 2>/dev/null || true) case "$mtime" in ''|*[!0-9]*) return 1 ;; esac now=$(date +%s) - [ $((now - mtime)) -le 10 ] + [ $((now - mtime)) -le "$FM_REMOTE_JOB_PROBE_FRESHNESS_SECONDS" ] } fm_remote_job_wait_for_probe() { # diff --git a/bin/fm-remote-job-worker.sh b/bin/fm-remote-job-worker.sh index edd123c859e..6cdfb8e11c8 100755 --- a/bin/fm-remote-job-worker.sh +++ b/bin/fm-remote-job-worker.sh @@ -677,7 +677,7 @@ worker_process_once() { # } main() { - local account_home lock_status + local account_home lock_status next_heartbeat next_reap account_home=$(worker_account_home) || { worker_error "cannot resolve account home"; exit 1; } FM_ROOT=$(fm_remote_job_canonical_existing_dir "$FM_ROOT") || { worker_error "configured FM_ROOT is unsafe"; exit 1; } [ -f "$FM_ROOT/AGENTS.md" ] && [ ! -L "$FM_ROOT/AGENTS.md" ] || { worker_error "FM_ROOT is not a Firstmate checkout"; exit 1; } @@ -695,18 +695,28 @@ main() { trap worker_shutdown HUP INT TERM worker_publish_identity "$account_home" || { worker_error "cannot publish worker code identity"; exit 1; } worker_publish_pid || { worker_error "cannot publish worker pid"; exit 1; } + worker_write_heartbeat || { worker_error "cannot update worker heartbeat"; exit 1; } + next_heartbeat=$((SECONDS + FM_REMOTE_JOB_HEARTBEAT_INTERVAL_SECONDS)) + next_reap=$SECONDS while :; do - worker_write_heartbeat || { worker_error "cannot update worker heartbeat"; exit 1; } - # Checked right after a fresh heartbeat, so the grace window cannot make a - # still-healthy worker read as unready to a concurrent probe. - if worker_code_root_abandoned; then - worker_error "configured FM_ROOT $FM_ROOT no longer exists; stopping the abandoned worker" - exit 0 + if [ "$SECONDS" -ge "$next_heartbeat" ]; then + worker_write_heartbeat || { worker_error "cannot update worker heartbeat"; exit 1; } + next_heartbeat=$((SECONDS + FM_REMOTE_JOB_HEARTBEAT_INTERVAL_SECONDS)) + fi + if ! fm_remote_job_root_is_live "$FM_ROOT"; then + # A prober's freshness window must not lapse during the abandoned-root + # grace check below, which can block for seconds confirming a transient + # disappearance instead of a real one. + worker_write_heartbeat || { worker_error "cannot update worker heartbeat"; exit 1; } + next_heartbeat=$((SECONDS + FM_REMOTE_JOB_HEARTBEAT_INTERVAL_SECONDS)) + if worker_code_root_abandoned; then + worker_error "configured FM_ROOT $FM_ROOT no longer exists; stopping the abandoned worker" + exit 0 + fi fi - worker_reap=0 - if [ "$worker_reap" -eq 0 ]; then + if [ "$SECONDS" -ge "$next_reap" ]; then fm_remote_job_reap_stale "$account_home" || true - worker_reap=1 + next_reap=$((SECONDS + FM_REMOTE_JOB_REAP_INTERVAL_SECONDS)) fi worker_process_once "$account_home" sleep "$FM_REMOTE_JOB_POLL_SECONDS" diff --git a/tests/fm-remote-job.test.sh b/tests/fm-remote-job.test.sh index 0b6dede4d65..de3da5287dc 100755 --- a/tests/fm-remote-job.test.sh +++ b/tests/fm-remote-job.test.sh @@ -19,6 +19,7 @@ REAL_GIT=$(command -v git) OTHER_PID= RECOVERY_WORKER_PID= REPEAT_WORKER_PID= +IDLE_WORKER_PID= mkdir -p "$REMOTE_ROOT/bin" "$REMOTE_HOME" "$ACCOUNT_HOME" "$RUNTIME_BIN" # worker.pid records the serving child, not its restart supervisor, so stopping # that pid alone leaves the supervisor to respawn - the leak @@ -27,6 +28,7 @@ cleanup_remote_job_fixture() { [ -z "$OTHER_PID" ] || kill "$OTHER_PID" 2>/dev/null || true [ -z "$RECOVERY_WORKER_PID" ] || kill "$RECOVERY_WORKER_PID" 2>/dev/null || true [ -z "$REPEAT_WORKER_PID" ] || kill "$REPEAT_WORKER_PID" 2>/dev/null || true + [ -z "$IDLE_WORKER_PID" ] || kill "$IDLE_WORKER_PID" 2>/dev/null || true if [ -f "$STATE_ROOT/worker.pid" ]; then fm_remote_job_stop_worker_tree "$(cat "$STATE_ROOT/worker.pid")" || true fi @@ -691,4 +693,61 @@ wait "$REPEAT_WORKER_PID" 2>/dev/null || true REPEAT_WORKER_PID= pass "a repeatedly signalled shutdown still releases ownership for the next worker" +# Fake mktemp/date shadow the real ones on PATH to count calls, still exec'ing +# through. Idle mktemp comes only from the heartbeat write; idle date comes +# only from reap_stale - each isolates one cadence from the fast poll tick. +IDLE_HOME="$TMP_ROOT/idle-cadence-account" +IDLE_STATE="$TMP_ROOT/idle-cadence-jobs" +IDLE_BIN="$TMP_ROOT/idle-cadence-bin" +IDLE_MKTEMP_LOG="$TMP_ROOT/idle-cadence-mktemp.log" +IDLE_DATE_LOG="$TMP_ROOT/idle-cadence-date.log" +mkdir -p "$IDLE_HOME" "$IDLE_BIN" +chmod 700 "$IDLE_HOME" +: > "$IDLE_MKTEMP_LOG" +: > "$IDLE_DATE_LOG" +REAL_MKTEMP=$(command -v mktemp) +REAL_DATE=$(command -v date) +cat > "$IDLE_BIN/mktemp" <> "$IDLE_MKTEMP_LOG" +exec "$REAL_MKTEMP" "\$@" +SH +cat > "$IDLE_BIN/date" <> "$IDLE_DATE_LOG" +exec "$REAL_DATE" "\$@" +SH +chmod +x "$IDLE_BIN/mktemp" "$IDLE_BIN/date" +HOME="$IDLE_HOME" PATH="$IDLE_BIN:/usr/bin:/bin:/usr/sbin:/sbin" FM_ROOT_OVERRIDE="$REMOTE_ROOT" \ + FM_REMOTE_JOB_STATE_ROOT="$IDLE_STATE" FM_REMOTE_JOB_PLATFORM_OVERRIDE=Linux \ + FM_REMOTE_JOB_HEARTBEAT_INTERVAL_SECONDS=1 FM_REMOTE_JOB_REAP_INTERVAL_SECONDS=1 \ + FM_REMOTE_JOB_POLL_SECONDS=0.02 \ + "$REMOTE_ROOT/bin/fm-remote-job-worker.sh" --serve \ + > "$TMP_ROOT/idle-cadence.out" 2> "$TMP_ROOT/idle-cadence.err" & +IDLE_WORKER_PID=$! +for _ in $(seq 1 100); do + [ -f "$IDLE_STATE/worker.ready" ] && break + sleep 0.05 +done +assert_present "$IDLE_STATE/worker.ready" "the idle-cadence worker did not become ready" +# Startup (lock acquisition, identity, pid, the first heartbeat) already forked +# mktemp/date before this point; only counts from here measure the idle +# cadence itself, not one-time startup cost. +: > "$IDLE_MKTEMP_LOG" +: > "$IDLE_DATE_LOG" +sleep 3 +kill -TERM "$IDLE_WORKER_PID" +wait "$IDLE_WORKER_PID" 2>/dev/null || true +IDLE_WORKER_PID= +MKTEMP_CALLS=$(wc -l < "$IDLE_MKTEMP_LOG" | tr -d ' ') +DATE_CALLS=$(wc -l < "$IDLE_DATE_LOG" | tr -d ' ') +# A 20ms poll tick over 3 idle seconds would fork each of these roughly 150 +# times if the heartbeat or reap ran on every tick instead of its own ~1s +# cadence; a real ~1s cadence forks each only a handful of times. +[ "$MKTEMP_CALLS" -ge 2 ] && [ "$MKTEMP_CALLS" -le 10 ] \ + || fail "idle heartbeat forked mktemp $MKTEMP_CALLS times in 3s at a 1s interval; expected the write gated to its own cadence" +[ "$DATE_CALLS" -ge 2 ] && [ "$DATE_CALLS" -le 10 ] \ + || fail "idle reap forked date $DATE_CALLS times in 3s at a 1s interval; the dead reap throttle regressed to running every poll tick" +pass "idle heartbeat and reap run on their own cadence, not on every poll tick" + echo "ALL TESTS PASSED" From 20584a0f6b845d8ba81fc450d64ee2ae417191bb Mon Sep 17 00:00:00 2001 From: Alan Domio Date: Wed, 9 Sep 2026 22:29:13 +0200 Subject: [PATCH 2/6] no-mistakes(review): share probe freshness constant with doctor; widen idle test wait --- bin/fm-remote-doctor.sh | 2 +- tests/fm-remote-job.test.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/fm-remote-doctor.sh b/bin/fm-remote-doctor.sh index aad9ce44aa7..cc874c65f13 100755 --- a/bin/fm-remote-doctor.sh +++ b/bin/fm-remote-doctor.sh @@ -244,7 +244,7 @@ remote_job_probe_ok() { mtime=$(fm_remote_job_path_mtime "$ready" 2>/dev/null || true) case "$mtime" in ''|*[!0-9]*) return 1 ;; esac now=$(date +%s) - [ $((now - mtime)) -le 10 ] + [ $((now - mtime)) -le "$FM_REMOTE_JOB_PROBE_FRESHNESS_SECONDS" ] } remote_job_identity_ok() { diff --git a/tests/fm-remote-job.test.sh b/tests/fm-remote-job.test.sh index de3da5287dc..191fa2d95af 100755 --- a/tests/fm-remote-job.test.sh +++ b/tests/fm-remote-job.test.sh @@ -725,7 +725,7 @@ HOME="$IDLE_HOME" PATH="$IDLE_BIN:/usr/bin:/bin:/usr/sbin:/sbin" FM_ROOT_OVERRID "$REMOTE_ROOT/bin/fm-remote-job-worker.sh" --serve \ > "$TMP_ROOT/idle-cadence.out" 2> "$TMP_ROOT/idle-cadence.err" & IDLE_WORKER_PID=$! -for _ in $(seq 1 100); do +for _ in $(seq 1 300); do [ -f "$IDLE_STATE/worker.ready" ] && break sleep 0.05 done From e11449769abaeb826f2b68ad311a6d12ed61d848 Mon Sep 17 00:00:00 2001 From: Alan Domio Date: Wed, 9 Sep 2026 22:51:40 +0200 Subject: [PATCH 3/6] no-mistakes(review): force heartbeat before reap so deleted state root fails fast --- bin/fm-remote-job-worker.sh | 15 ++++++++------ tests/fm-remote-job.test.sh | 40 +++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/bin/fm-remote-job-worker.sh b/bin/fm-remote-job-worker.sh index 6cdfb8e11c8..0e2eacbd7a7 100755 --- a/bin/fm-remote-job-worker.sh +++ b/bin/fm-remote-job-worker.sh @@ -75,6 +75,11 @@ worker_write_heartbeat() { mv -f -- "$tmp" "$ready" } +worker_refresh_heartbeat() { + worker_write_heartbeat || { worker_error "cannot update worker heartbeat"; exit 1; } + next_heartbeat=$((SECONDS + FM_REMOTE_JOB_HEARTBEAT_INTERVAL_SECONDS)) +} + worker_publish_pid() { local pid_file tmp pid_file=$(fm_remote_job_worker_pid_path) @@ -695,26 +700,24 @@ main() { trap worker_shutdown HUP INT TERM worker_publish_identity "$account_home" || { worker_error "cannot publish worker code identity"; exit 1; } worker_publish_pid || { worker_error "cannot publish worker pid"; exit 1; } - worker_write_heartbeat || { worker_error "cannot update worker heartbeat"; exit 1; } - next_heartbeat=$((SECONDS + FM_REMOTE_JOB_HEARTBEAT_INTERVAL_SECONDS)) + worker_refresh_heartbeat next_reap=$SECONDS while :; do if [ "$SECONDS" -ge "$next_heartbeat" ]; then - worker_write_heartbeat || { worker_error "cannot update worker heartbeat"; exit 1; } - next_heartbeat=$((SECONDS + FM_REMOTE_JOB_HEARTBEAT_INTERVAL_SECONDS)) + worker_refresh_heartbeat fi if ! fm_remote_job_root_is_live "$FM_ROOT"; then # A prober's freshness window must not lapse during the abandoned-root # grace check below, which can block for seconds confirming a transient # disappearance instead of a real one. - worker_write_heartbeat || { worker_error "cannot update worker heartbeat"; exit 1; } - next_heartbeat=$((SECONDS + FM_REMOTE_JOB_HEARTBEAT_INTERVAL_SECONDS)) + worker_refresh_heartbeat if worker_code_root_abandoned; then worker_error "configured FM_ROOT $FM_ROOT no longer exists; stopping the abandoned worker" exit 0 fi fi if [ "$SECONDS" -ge "$next_reap" ]; then + worker_refresh_heartbeat fm_remote_job_reap_stale "$account_home" || true next_reap=$((SECONDS + FM_REMOTE_JOB_REAP_INTERVAL_SECONDS)) fi diff --git a/tests/fm-remote-job.test.sh b/tests/fm-remote-job.test.sh index 191fa2d95af..2d1cc5b32e2 100755 --- a/tests/fm-remote-job.test.sh +++ b/tests/fm-remote-job.test.sh @@ -20,6 +20,7 @@ OTHER_PID= RECOVERY_WORKER_PID= REPEAT_WORKER_PID= IDLE_WORKER_PID= +TRIPWIRE_WORKER_PID= mkdir -p "$REMOTE_ROOT/bin" "$REMOTE_HOME" "$ACCOUNT_HOME" "$RUNTIME_BIN" # worker.pid records the serving child, not its restart supervisor, so stopping # that pid alone leaves the supervisor to respawn - the leak @@ -29,6 +30,7 @@ cleanup_remote_job_fixture() { [ -z "$RECOVERY_WORKER_PID" ] || kill "$RECOVERY_WORKER_PID" 2>/dev/null || true [ -z "$REPEAT_WORKER_PID" ] || kill "$REPEAT_WORKER_PID" 2>/dev/null || true [ -z "$IDLE_WORKER_PID" ] || kill "$IDLE_WORKER_PID" 2>/dev/null || true + [ -z "$TRIPWIRE_WORKER_PID" ] || kill "$TRIPWIRE_WORKER_PID" 2>/dev/null || true if [ -f "$STATE_ROOT/worker.pid" ]; then fm_remote_job_stop_worker_tree "$(cat "$STATE_ROOT/worker.pid")" || true fi @@ -750,4 +752,42 @@ DATE_CALLS=$(wc -l < "$IDLE_DATE_LOG" | tr -d ' ') || fail "idle reap forked date $DATE_CALLS times in 3s at a 1s interval; the dead reap throttle regressed to running every poll tick" pass "idle heartbeat and reap run on their own cadence, not on every poll tick" +TRIPWIRE_HOME="$TMP_ROOT/tripwire-home" +TRIPWIRE_STATE="$TMP_ROOT/tripwire-jobs" +mkdir -p "$TRIPWIRE_HOME" +chmod 700 "$TRIPWIRE_HOME" +HOME="$TRIPWIRE_HOME" FM_ROOT_OVERRIDE="$REMOTE_ROOT" \ + FM_REMOTE_JOB_STATE_ROOT="$TRIPWIRE_STATE" FM_REMOTE_JOB_PLATFORM_OVERRIDE=Linux \ + FM_REMOTE_JOB_HEARTBEAT_INTERVAL_SECONDS=9 FM_REMOTE_JOB_REAP_INTERVAL_SECONDS=1 \ + FM_REMOTE_JOB_POLL_SECONDS=0.02 \ + "$REMOTE_ROOT/bin/fm-remote-job-worker.sh" --serve \ + > "$TMP_ROOT/tripwire.out" 2> "$TMP_ROOT/tripwire.err" & +TRIPWIRE_WORKER_PID=$! +for _ in $(seq 1 300); do + [ -f "$TRIPWIRE_STATE/worker.ready" ] && break + sleep 0.05 +done +assert_present "$TRIPWIRE_STATE/worker.ready" "the state-root tripwire worker did not become ready" +# The reap sweep re-prepares the state tree, so a worker whose root is deleted +# must fail fast on its heartbeat before that sweep can silently recreate an +# unowned tree and leave two workers serving one queue. +rm -rf -- "$TRIPWIRE_STATE" +for _ in $(seq 1 200); do + kill -0 "$TRIPWIRE_WORKER_PID" 2>/dev/null || break + sleep 0.05 +done +if kill -0 "$TRIPWIRE_WORKER_PID" 2>/dev/null; then + kill -KILL "$TRIPWIRE_WORKER_PID" 2>/dev/null || true + wait "$TRIPWIRE_WORKER_PID" 2>/dev/null || true + TRIPWIRE_WORKER_PID= + fail "the worker kept serving after its state root was deleted" +fi +TRIPWIRE_STATUS=0 +wait "$TRIPWIRE_WORKER_PID" 2>/dev/null || TRIPWIRE_STATUS=$? +TRIPWIRE_WORKER_PID= +[ "$TRIPWIRE_STATUS" -ne 0 ] \ + || fail "a worker whose state root vanished exited cleanly instead of failing for its supervisor to restart" +assert_absent "$TRIPWIRE_STATE" "the reap sweep recreated the deleted worker state root behind an unowned worker" +pass "a deleted state root fails the worker fast instead of being recreated by the reap sweep" + echo "ALL TESTS PASSED" From 2621c0f189048ad7a1354e5a05fb5dada2789810 Mon Sep 17 00:00:00 2001 From: Alan Domio Date: Wed, 9 Sep 2026 23:26:41 +0200 Subject: [PATCH 4/6] no-mistakes(review): measure idle cadence against poll ticks, not absolute forks --- tests/fm-remote-job.test.sh | 42 ++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/tests/fm-remote-job.test.sh b/tests/fm-remote-job.test.sh index 2d1cc5b32e2..98ce427635d 100755 --- a/tests/fm-remote-job.test.sh +++ b/tests/fm-remote-job.test.sh @@ -695,20 +695,23 @@ wait "$REPEAT_WORKER_PID" 2>/dev/null || true REPEAT_WORKER_PID= pass "a repeatedly signalled shutdown still releases ownership for the next worker" -# Fake mktemp/date shadow the real ones on PATH to count calls, still exec'ing -# through. Idle mktemp comes only from the heartbeat write; idle date comes -# only from reap_stale - each isolates one cadence from the fast poll tick. +# Fake mktemp/date/sleep shadow the real ones on PATH to count calls, still +# exec'ing through. Idle mktemp comes only from heartbeat writes, idle date +# only from reap_stale, and idle sleep only from the poll tick itself. IDLE_HOME="$TMP_ROOT/idle-cadence-account" IDLE_STATE="$TMP_ROOT/idle-cadence-jobs" IDLE_BIN="$TMP_ROOT/idle-cadence-bin" IDLE_MKTEMP_LOG="$TMP_ROOT/idle-cadence-mktemp.log" IDLE_DATE_LOG="$TMP_ROOT/idle-cadence-date.log" +IDLE_SLEEP_LOG="$TMP_ROOT/idle-cadence-sleep.log" mkdir -p "$IDLE_HOME" "$IDLE_BIN" chmod 700 "$IDLE_HOME" : > "$IDLE_MKTEMP_LOG" : > "$IDLE_DATE_LOG" +: > "$IDLE_SLEEP_LOG" REAL_MKTEMP=$(command -v mktemp) REAL_DATE=$(command -v date) +REAL_SLEEP=$(command -v sleep) cat > "$IDLE_BIN/mktemp" <> "$IDLE_MKTEMP_LOG" @@ -719,10 +722,15 @@ cat > "$IDLE_BIN/date" <> "$IDLE_DATE_LOG" exec "$REAL_DATE" "\$@" SH -chmod +x "$IDLE_BIN/mktemp" "$IDLE_BIN/date" +cat > "$IDLE_BIN/sleep" <> "$IDLE_SLEEP_LOG" +exec "$REAL_SLEEP" "\$@" +SH +chmod +x "$IDLE_BIN/mktemp" "$IDLE_BIN/date" "$IDLE_BIN/sleep" HOME="$IDLE_HOME" PATH="$IDLE_BIN:/usr/bin:/bin:/usr/sbin:/sbin" FM_ROOT_OVERRIDE="$REMOTE_ROOT" \ FM_REMOTE_JOB_STATE_ROOT="$IDLE_STATE" FM_REMOTE_JOB_PLATFORM_OVERRIDE=Linux \ - FM_REMOTE_JOB_HEARTBEAT_INTERVAL_SECONDS=1 FM_REMOTE_JOB_REAP_INTERVAL_SECONDS=1 \ + FM_REMOTE_JOB_HEARTBEAT_INTERVAL_SECONDS=2 FM_REMOTE_JOB_REAP_INTERVAL_SECONDS=2 \ FM_REMOTE_JOB_POLL_SECONDS=0.02 \ "$REMOTE_ROOT/bin/fm-remote-job-worker.sh" --serve \ > "$TMP_ROOT/idle-cadence.out" 2> "$TMP_ROOT/idle-cadence.err" & @@ -733,23 +741,27 @@ for _ in $(seq 1 300); do done assert_present "$IDLE_STATE/worker.ready" "the idle-cadence worker did not become ready" # Startup (lock acquisition, identity, pid, the first heartbeat) already forked -# mktemp/date before this point; only counts from here measure the idle -# cadence itself, not one-time startup cost. +# these before this point; only counts from here measure the idle cadence +# itself, not one-time startup cost. : > "$IDLE_MKTEMP_LOG" : > "$IDLE_DATE_LOG" -sleep 3 +: > "$IDLE_SLEEP_LOG" +sleep 6 kill -TERM "$IDLE_WORKER_PID" wait "$IDLE_WORKER_PID" 2>/dev/null || true IDLE_WORKER_PID= MKTEMP_CALLS=$(wc -l < "$IDLE_MKTEMP_LOG" | tr -d ' ') DATE_CALLS=$(wc -l < "$IDLE_DATE_LOG" | tr -d ' ') -# A 20ms poll tick over 3 idle seconds would fork each of these roughly 150 -# times if the heartbeat or reap ran on every tick instead of its own ~1s -# cadence; a real ~1s cadence forks each only a handful of times. -[ "$MKTEMP_CALLS" -ge 2 ] && [ "$MKTEMP_CALLS" -le 10 ] \ - || fail "idle heartbeat forked mktemp $MKTEMP_CALLS times in 3s at a 1s interval; expected the write gated to its own cadence" -[ "$DATE_CALLS" -ge 2 ] && [ "$DATE_CALLS" -le 10 ] \ - || fail "idle reap forked date $DATE_CALLS times in 3s at a 1s interval; the dead reap throttle regressed to running every poll tick" +# The poll tick is the only per-iteration sleep while idle, so counting it +# measures the cadence as a ratio instead of an absolute fork count that a +# loaded runner (or the regression's own forking) would silently rescale. +TICK_CALLS=$(wc -l < "$IDLE_SLEEP_LOG" | tr -d ' ') +[ "$TICK_CALLS" -ge 20 ] \ + || fail "the idle worker polled only $TICK_CALLS times; the cadence window is too short to measure" +[ "$DATE_CALLS" -ge 2 ] && [ $((DATE_CALLS * 3)) -le "$TICK_CALLS" ] \ + || fail "idle reap forked date $DATE_CALLS times across $TICK_CALLS poll ticks; the dead reap throttle regressed to running every poll tick" +[ "$MKTEMP_CALLS" -ge 2 ] && [ $((MKTEMP_CALLS * 3)) -le "$TICK_CALLS" ] \ + || fail "idle heartbeat forked mktemp $MKTEMP_CALLS times across $TICK_CALLS poll ticks; expected the write gated to its own cadence" pass "idle heartbeat and reap run on their own cadence, not on every poll tick" TRIPWIRE_HOME="$TMP_ROOT/tripwire-home" From 0e9ff4cc6887a0b37e6b3ba8b4282414e8b52c6c Mon Sep 17 00:00:00 2001 From: Alan Domio Date: Thu, 10 Sep 2026 01:01:01 +0200 Subject: [PATCH 5/6] no-mistakes(document): document worker heartbeat and reap cadence contract --- bin/fm-remote-job-worker.sh | 12 ++++++++++++ tests/fm-remote-job.test.sh | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/bin/fm-remote-job-worker.sh b/bin/fm-remote-job-worker.sh index 0e2eacbd7a7..259d013a277 100755 --- a/bin/fm-remote-job-worker.sh +++ b/bin/fm-remote-job-worker.sh @@ -15,6 +15,15 @@ # have been committed. The library header owns the exact record fields and # lifecycle. # +# Job pickup stays on the fast FM_REMOTE_JOB_POLL_SECONDS tick, while the two +# loop-maintenance duties run on their own cadence so an idle worker does not +# fork a heartbeat write and a full queue rescan every tick: worker.ready is +# refreshed every FM_REMOTE_JOB_HEARTBEAT_INTERVAL_SECONDS and stale done +# records are swept every FM_REMOTE_JOB_REAP_INTERVAL_SECONDS. That heartbeat +# interval is validated strictly below FM_REMOTE_JOB_PROBE_FRESHNESS_SECONDS, +# the window the library's probe tolerates, so a healthy worker between writes +# never reads as unready. +# # The worker is abandoned when its configured FM_ROOT stops being a genuine # Firstmate checkout - the state a pruned no-mistakes gate worktree, a returned # pooled worktree, or a removed test fixture root leaves behind. It can never @@ -717,6 +726,9 @@ main() { fi fi if [ "$SECONDS" -ge "$next_reap" ]; then + # The sweep re-prepares the state tree, so refresh first: a deleted state + # root must fail this write and stop the worker for its supervisor rather + # than be silently recreated behind an owner that holds no lock. worker_refresh_heartbeat fm_remote_job_reap_stale "$account_home" || true next_reap=$((SECONDS + FM_REMOTE_JOB_REAP_INTERVAL_SECONDS)) diff --git a/tests/fm-remote-job.test.sh b/tests/fm-remote-job.test.sh index 98ce427635d..b056ea8b22d 100755 --- a/tests/fm-remote-job.test.sh +++ b/tests/fm-remote-job.test.sh @@ -802,4 +802,38 @@ TRIPWIRE_WORKER_PID= assert_absent "$TRIPWIRE_STATE" "the reap sweep recreated the deleted worker state root behind an unowned worker" pass "a deleted state root fails the worker fast instead of being recreated by the reap sweep" +# A prober reads worker.ready as fresh for FM_REMOTE_JOB_PROBE_FRESHNESS_SECONDS, +# so a heartbeat cadence at or past that window would let a healthy worker read +# as unready between writes. Settings validation refuses it before any state. +for BAD_HEARTBEAT in '' abc 0 "$FM_REMOTE_JOB_PROBE_FRESHNESS_SECONDS" \ + "$((FM_REMOTE_JOB_PROBE_FRESHNESS_SECONDS + 1))"; do + ( FM_REMOTE_JOB_HEARTBEAT_INTERVAL_SECONDS=$BAD_HEARTBEAT + fm_remote_job_validate_settings ) \ + && fail "a heartbeat interval of '$BAD_HEARTBEAT' was accepted" +done +( FM_REMOTE_JOB_HEARTBEAT_INTERVAL_SECONDS=$((FM_REMOTE_JOB_PROBE_FRESHNESS_SECONDS - 1)) + fm_remote_job_validate_settings ) \ + || fail "the widest heartbeat interval still inside the freshness window was rejected" +for BAD_REAP in '' abc 0; do + ( FM_REMOTE_JOB_REAP_INTERVAL_SECONDS=$BAD_REAP + fm_remote_job_validate_settings ) \ + && fail "a reap interval of '$BAD_REAP' was accepted" +done +BOUNDS_HOME="$TMP_ROOT/bounds-home" +BOUNDS_STATE="$TMP_ROOT/bounds-jobs" +mkdir -p "$BOUNDS_HOME" +chmod 700 "$BOUNDS_HOME" +BOUNDS_STATUS=0 +HOME="$BOUNDS_HOME" FM_ROOT_OVERRIDE="$REMOTE_ROOT" \ + FM_REMOTE_JOB_STATE_ROOT="$BOUNDS_STATE" FM_REMOTE_JOB_PLATFORM_OVERRIDE=Linux \ + FM_REMOTE_JOB_HEARTBEAT_INTERVAL_SECONDS="$FM_REMOTE_JOB_PROBE_FRESHNESS_SECONDS" \ + "$REMOTE_ROOT/bin/fm-remote-job-worker.sh" --serve \ + > "$TMP_ROOT/bounds.out" 2> "$TMP_ROOT/bounds.err" || BOUNDS_STATUS=$? +[ "$BOUNDS_STATUS" -ne 0 ] \ + || fail "the worker served with a heartbeat cadence outside the probe freshness window" +grep -q 'bounds or timeout are invalid' "$TMP_ROOT/bounds.err" \ + || fail "the refused worker did not report invalid bounds: $(cat "$TMP_ROOT/bounds.err")" +assert_absent "$BOUNDS_STATE/worker.ready" "a worker with a refused heartbeat cadence still published a heartbeat" +pass "a heartbeat cadence outside the probe freshness window is refused before the worker serves" + echo "ALL TESTS PASSED" From 105b52327079a82b2716ebebc381024239780f1c Mon Sep 17 00:00:00 2001 From: Alan Domio Date: Thu, 10 Sep 2026 01:27:54 +0200 Subject: [PATCH 6/6] =?UTF-8?q?no-mistakes(ci):=20Fixed=20the=20"Behavior?= =?UTF-8?q?=20portable=20serial=203"=20failure,=20which=20was=20caused=20b?= =?UTF-8?q?y=20this=20PR.=20Root=20cause:=20commit=2020584a0=20edited=20bi?= =?UTF-8?q?n/fm-remote-doctor.sh=20(replacing=20the=20hardcoded=2010s=20fr?= =?UTF-8?q?eshness=20literal=20with=20FM=5FREMOTE=5FJOB=5FPROBE=5FFRESHNES?= =?UTF-8?q?S=5FSECONDS)=20but=20did=20not=20update=20the=20pinned=20DOCTOR?= =?UTF-8?q?=5FSHA256=20bootstrap=20identity=20in=20bin/fm-remote-entrypoin?= =?UTF-8?q?t.sh:25.=20When=20git=20is=20unavailable=20on=20the=20remote=20?= =?UTF-8?q?operator=20PATH,=20the=20entrypoint=20authorizes=20fm-remote-do?= =?UTF-8?q?ctor.sh=20by=20comparing=20its=20sha256=20against=20that=20pin;?= =?UTF-8?q?=20the=20stale=20pin=20made=20it=20die=20with=20"required=20too?= =?UTF-8?q?l=20git=20is=20unavailable=20and=20the=20doctor=20does=20not=20?= =?UTF-8?q?match=20the=20trusted=20bootstrap=20identity"=20=E2=80=94=20exa?= =?UTF-8?q?ctly=20the=20tests/fm-on.test.sh=20assertion=20(`mode=3Dcheck`,?= =?UTF-8?q?=20"the=20trusted=20doctor=20could=20not=20bootstrap=20while=20?= =?UTF-8?q?git=20was=20unavailable")=20that=20failed=20in=20CI.=20Fix=20(1?= =?UTF-8?q?=20line):=20repinned=20DOCTOR=5FSHA256=20from=207bb13d9f...=20t?= =?UTF-8?q?o=20f0e9c423a1deeb1a6bfcbc5f595e958b9d6fb620606f3804dafdc3d837f?= =?UTF-8?q?088c6,=20the=20sha256=20of=20bin/fm-remote-doctor.sh=20at=20HEA?= =?UTF-8?q?D.=20No=20behavior=20change,=20no=20new=20machinery.=20Verifica?= =?UTF-8?q?tion=20(local=20macOS):=20-=20tests/fm-on.test.sh:=20the=20prev?= =?UTF-8?q?iously=20failing=20case=20now=20passes=20("ok=20-=20doctor=20bo?= =?UTF-8?q?otstrap=20remains=20authenticated=20when=20git=20is=20unavailab?= =?UTF-8?q?le")=20and=20the=20suite=20reaches=20ALL=20TESTS=20PASSED.=20Tw?= =?UTF-8?q?o=20assertions=20in=20that=20suite=20fail=20only=20on=20this=20?= =?UTF-8?q?machine=20=E2=80=94=20the=20test=20builds=20its=20expected=20ch?= =?UTF-8?q?ild=20PATH=20with=20a=20sorted=20shell=20glob=20over=20~/.asdf/?= =?UTF-8?q?installs/*/*/bin=20while=20the=20implementation=20emits=20readd?= =?UTF-8?q?ir=20order;=20the=20CI=20runner=20has=20no=20asdf=20installs=20?= =?UTF-8?q?and=20both=20passed=20there.=20Verified=20via=20a=20scratch=20c?= =?UTF-8?q?opy=20of=20the=20suite=20with=20only=20those=20two=20local-orde?= =?UTF-8?q?ring=20assertions=20neutralized,=20deleted=20afterwards.=20Unto?= =?UTF-8?q?uched=20(unrelated=20to=20this=20PR).=20-=20tests/fm-remote-ent?= =?UTF-8?q?rypoint.test.sh,=20tests/fm-remote-doctor.test.sh:=20exit=200.?= =?UTF-8?q?=20-=20tests/fm-remote-job.test.sh:=20passes,=20but=20intermitt?= =?UTF-8?q?ently=20flaky=20here=20on=20two=20pre-existing=20timing=20asser?= =?UTF-8?q?tions=20("the=20blocking=20job=20did=20not=20begin=20running",?= =?UTF-8?q?=20"queue=20time=20consumed=20the=20second=20job's=20execution?= =?UTF-8?q?=20timeout").=20Reproduced=20the=20same=20flake=20with=20the=20?= =?UTF-8?q?base=20commit's=20lib/worker/test=20files=20checked=20out,=20so?= =?UTF-8?q?=20it=20predates=20this=20change=20and=20is=20not=20the=20repor?= =?UTF-8?q?ted=20failure.=20-=20bin/fm-lint.sh:=20clean=20(ShellCheck=200.?= =?UTF-8?q?11.0,=20actionlint=201.7.12).=20Changed=20file:=20bin/fm-remote?= =?UTF-8?q?-entrypoint.sh=20(working=20tree;=20not=20committed)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/fm-remote-entrypoint.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/fm-remote-entrypoint.sh b/bin/fm-remote-entrypoint.sh index 6763e8c955d..53969280fcb 100755 --- a/bin/fm-remote-entrypoint.sh +++ b/bin/fm-remote-entrypoint.sh @@ -22,7 +22,7 @@ set -eu PROTOCOL=1 -DOCTOR_SHA256=7bb13d9fad8455978bf109d4681a3aa3cb170565c8a74be4ec7b520427db14c2 +DOCTOR_SHA256=f0e9c423a1deeb1a6bfcbc5f595e958b9d6fb620606f3804dafdc3d837f088c6 REAL_SOURCE=$(python3 -c 'import os, sys; print(os.path.realpath(sys.argv[1]))' "${BASH_SOURCE[0]}" 2>/dev/null) || REAL_SOURCE=$(realpath "${BASH_SOURCE[0]}" 2>/dev/null) || REAL_SOURCE=${BASH_SOURCE[0]}