From cff44e9e939c7a7b17210efba96384aa6b5b7880 Mon Sep 17 00:00:00 2001 From: Claude Code Bot Date: Mon, 23 Nov 2026 19:07:10 -0800 Subject: [PATCH 1/2] fix(swm-macos-runner): end a runner that waits too long for a job A JIT runner that gets no job now ends after SWM_IDLE_MAX_S (default 3600 s, 0 disables) from its handoff, instead of only at SWM_JOB_MAX_S (6 h). bounded_check makes the same DELETE as the update-pending path, on the same SWM_UPDATE_IDLE_CHECK_S cadence. A 2xx ends the VM and is not counted as a fail. A 422 (busy) or 404 keeps the VM; the refusal is logged once per runner. A pending update takes precedence. The runbook lists the new log lines. Advances #297 --- docs/runbooks/swm-macos-runner.md | 1 + scripts/swm-macos-runner/config.sh | 2 + scripts/swm-macos-runner/supervisor.sh | 47 ++++++++++-- .../swm-macos-runner/tests/test-supervisor.sh | 75 +++++++++++++++++++ 4 files changed, 118 insertions(+), 7 deletions(-) diff --git a/docs/runbooks/swm-macos-runner.md b/docs/runbooks/swm-macos-runner.md index 5ffb833..edd6118 100644 --- a/docs/runbooks/swm-macos-runner.md +++ b/docs/runbooks/swm-macos-runner.md @@ -194,6 +194,7 @@ The log is `~/Library/Logs/swm-macos-runner.log`. Key lines: - `runner ... handed its JIT config`: the runner may be idle or busy; it is waiting for a job or running one (`job=1`). - `update pending; waiting ...` / `update installed or withdrawn` / `update still pending after ...`: the supervisor held a pass for the updater. - `update pending and runner N idle ...` / `runner ... ended idle for the pending update`: an idle runner made way for an update. +- `runner N idle for S s (limit L s); ending VM ...` / `runner ... ended idle after SWM_IDLE_MAX_S`: a runner waited `SWM_IDLE_MAX_S` (default 3600, `0` disables) after its JIT handoff with no job, so the supervisor deregistered it and starts a fresh VM. Not a failure. `idle past SWM_IDLE_MAX_S but runner N not deleted (HTTP 422 ...)` means it was running a job and keeps it; that line shows once per runner. The limit is checked every `SWM_UPDATE_IDLE_CHECK_S`, so `SWM_UPDATE_IDLE_CHECK_S=0` turns it off too. - `runner ... finished its job`: a job ended. - `pass failed (, try N): ...`: a failed pass and why. - `fails in a row; next pass in N s`: the supervisor is backing off. diff --git a/scripts/swm-macos-runner/config.sh b/scripts/swm-macos-runner/config.sh index bdeb56d..56ff104 100755 --- a/scripts/swm-macos-runner/config.sh +++ b/scripts/swm-macos-runner/config.sh @@ -42,6 +42,8 @@ export TART_HOME : "${SWM_UPDATE_POLL_S:=5}" # While a runner waits for a job with an update pending: how often to try to end it (DELETE refused while busy). : "${SWM_UPDATE_IDLE_CHECK_S:=300}" +# End a runner with no job this long after its JIT handoff; 0 disables. Checked every SWM_UPDATE_IDLE_CHECK_S. +: "${SWM_IDLE_MAX_S:=3600}" # GitHub: org, runner App (swm-runner), and the pf guard's ok files. : "${SWM_ORG:=smartwatermelon}" diff --git a/scripts/swm-macos-runner/supervisor.sh b/scripts/swm-macos-runner/supervisor.sh index 74cfd98..bad9816 100755 --- a/scripts/swm-macos-runner/supervisor.sh +++ b/scripts/swm-macos-runner/supervisor.sh @@ -56,8 +56,12 @@ SLEEP_PID="" API_CODE=000 # 1 once this pass's update_wait gave up: the update is stuck, so do not end idle runners for it. UPDATE_GAVE_UP=0 -# 1 when bounded_check ended an idle runner for a pending update. +# 1 when bounded_check ended an idle runner; IDLE_WHY says why (update or idle-max). IDLE_ENDED=0 +IDLE_WHY="" +# When this runner got its JIT config, and whether its SWM_IDLE_MAX_S refusal is already logged. +JIT_AT=0 +IDLE_REFUSAL_LOGGED=0 running() { [[ "${STOP}" -eq 0 ]]; } now() { date +%s; } @@ -284,19 +288,40 @@ start_vm() { set_fail pf "IPv6 ok file ${SWM_PF_OK_FILE_V6} not renewed within ${SWM_V6_LIMIT_S} s of the VM start" || return 1 } -# bounded_check: with an update pending, DELETE the runner (422 if busy). 0 = 2xx: end the VM. 404 can mean no access. +# bounded_check: update pending or SWM_IDLE_MAX_S passed: DELETE the runner (422 if busy). 0 = 2xx: end the VM. bounded_check() { - [[ -e "${UPDATE_PENDING}" && "${UPDATE_GAVE_UP}" -eq 0 && -n "${SL_ID}" ]] || return 1 + local why age=0 + [[ -n "${SL_ID}" ]] || return 1 + if [[ -e "${UPDATE_PENDING}" && "${UPDATE_GAVE_UP}" -eq 0 ]]; then + why=update + else + [[ "${SWM_IDLE_MAX_S}" =~ ^[0-9]+$ && "${SWM_IDLE_MAX_S}" -gt 0 && "${JIT_AT}" -gt 0 ]] || return 1 + age=$(($(now) - JIT_AT)) + [[ "${age}" -ge "${SWM_IDLE_MAX_S}" ]] || return 1 + why=idle-max + fi running || return 1 API_CODE=000 + # 404 can mean no access, so it is not proof of idle. One slot: no others_up guard as in nos-runner. if delete_runner "${SL_ID}" && [[ "${API_CODE}" != 404 ]]; then - log "update pending and runner ${SL_ID} idle (DELETE: HTTP ${API_CODE}); ending VM ${VM} so the update installs" + if [[ "${why}" == update ]]; then + log "update pending and runner ${SL_ID} idle (DELETE: HTTP ${API_CODE}); ending VM ${VM} so the update installs" + else + log "runner ${SL_ID} idle for ${age} s (limit ${SWM_IDLE_MAX_S} s); ending VM ${VM}" + fi SL_ID="" slot_save IDLE_ENDED=1 + IDLE_WHY="${why}" return 0 fi - log "update pending but runner ${SL_ID} not deleted (HTTP ${API_CODE}; 422 = it runs a job); waiting for the job" + if [[ "${why}" == update ]]; then + log "update pending but runner ${SL_ID} not deleted (HTTP ${API_CODE}; 422 = it runs a job); waiting for the job" + elif [[ "${IDLE_REFUSAL_LOGGED}" -eq 0 ]]; then + # Log once per runner: a busy runner refuses every SWM_UPDATE_IDLE_CHECK_S until its job ends. + log "idle past SWM_IDLE_MAX_S but runner ${SL_ID} not deleted (HTTP ${API_CODE}; 422 = it runs a job); waiting for the job" + IDLE_REFUSAL_LOGGED=1 + fi return 1 } @@ -326,16 +351,24 @@ run_job() { jit="" ABORT_OK=1 IDLE_ENDED=0 + IDLE_WHY="" + IDLE_REFUSAL_LOGGED=0 + JIT_AT="$(now)" running && BOUNDED_CHECK_S="${SWM_UPDATE_IDLE_CHECK_S}" SWM_IN=/dev/fd/3 \ bounded "${SWM_JOB_MAX_S}" "${TART_BIN}" exec -i "${VM}" /bin/bash "${GUEST_JOB}" || rc=$? ABORT_OK=0 exec 3<&- + JIT_AT=0 SL_JOB=0 slot_save if [[ "${STOP}" -eq 1 ]]; then return 1; fi - # Ended idle for a pending update: neutral, like the SWM_JOB_MAX_S case below. + # Ended idle (pending update or SWM_IDLE_MAX_S): neutral, like the SWM_JOB_MAX_S case below. if [[ "${IDLE_ENDED}" -eq 1 ]]; then - log "runner ${VM} ended idle for the pending update; not counted" + if [[ "${IDLE_WHY}" == update ]]; then + log "runner ${VM} ended idle for the pending update; not counted" + else + log "runner ${VM} ended idle after SWM_IDLE_MAX_S; not counted" + fi return 0 fi # An idle JIT runner reaches SWM_JOB_MAX_S every 6 h: neutral, so it never counts as a fail or a recovery. diff --git a/scripts/swm-macos-runner/tests/test-supervisor.sh b/scripts/swm-macos-runner/tests/test-supervisor.sh index 48e708e..f6255a9 100755 --- a/scripts/swm-macos-runner/tests/test-supervisor.sh +++ b/scripts/swm-macos-runner/tests/test-supervisor.sh @@ -540,6 +540,81 @@ check "gave up: job ran to SWM_JOB_MAX_S" grep -q 'reached SWM_JOB_MAX_S' "${S}/ check_not "gave up: not ended idle" grep -q 'ended idle for the pending update' "${S}/sup.err" check_is "gave up: only the teardown DELETE" 1 "$(dels)" +echo "-- idle_max_ends_idle_runner: idle past SWM_IDLE_MAX_S -> runner DELETEd, VM ended" +reset +SECONDS=0 +STUB_JOB_HANG=1 SWM_UPDATE_IDLE_CHECK_S=1 SWM_IDLE_MAX_S=1 sup once +rc=$? +took="${SECONDS}" +vm="$(vm_name)" +check "idle max: exit 0" test "${rc}" -eq 0 +check "idle max: ended long before SWM_JOB_MAX_S" test "${took}" -lt 10 +check_is "idle max: runner DELETEd before the VM stop" "tart exec-i|curl DELETE 4242|tart stop|tart delete|" \ + "$(sed -n '/exec-i/,$p' "${S}/order.log" | tr '\n' '|')" +check "idle max: VM stopped" grep -qx "stop ${vm}" "${S}/tart.argv" +check "idle max: VM deleted" grep -qx "delete ${vm}" "${S}/tart.argv" +check "idle max: logged" grep -qE "runner 4242 idle for [0-9]+ s \(limit 1 s\); ending VM ${vm}\$" "${S}/sup.err" +check "idle max: not counted logged" grep -q "runner ${vm} ended idle after SWM_IDLE_MAX_S; not counted" "${S}/sup.err" +check_not "idle max: not the update message" grep -q 'ended idle for the pending update' "${S}/sup.err" +check_not "idle max: did not reach SWM_JOB_MAX_S" grep -q 'reached SWM_JOB_MAX_S' "${S}/sup.err" +check_is "idle max: slot cleared" "//0" "$(slot name)/$(slot id)/$(slot job)" + +echo "-- idle_max_not_a_fail: fails and alert unchanged, no alert sent" +reset +mkdir -p "${S}/state" +printf 'name=\nid=\nfails=2\nalert=pf\njob=0\n' >"${S}/state/slot" +STUB_JOB_HANG=1 SWM_UPDATE_IDLE_CHECK_S=1 SWM_IDLE_MAX_S=1 sup once +check "not a fail: ended idle" grep -q 'ended idle after SWM_IDLE_MAX_S' "${S}/sup.err" +check_is "not a fail: fails unchanged" 2 "$(slot fails)" +check_is "not a fail: alert= unchanged" pf "$(slot alert)" +check_is "not a fail: no alert sent" 0 "$(count . "${S}/ntfy.log")" + +echo "-- idle_max_delete_422_keeps_vm: busy runner past SWM_IDLE_MAX_S keeps its VM" +reset +STUB_JOB_HANG=1 STUB_DELETE_BUSY=1 SWM_UPDATE_IDLE_CHECK_S=1 SWM_IDLE_MAX_S=1 SWM_JOB_MAX_S=4 sup once +check "idle 422: job ran to SWM_JOB_MAX_S" grep -q 'reached SWM_JOB_MAX_S' "${S}/sup.err" +check_not "idle 422: not ended idle" grep -q 'ended idle after SWM_IDLE_MAX_S' "${S}/sup.err" +check "idle 422: DELETE tried more than once during the job" test "$(dels)" -ge 3 +check_is "idle 422: refusal logged once per runner" 1 "$(count 'idle past SWM_IDLE_MAX_S but runner 4242 not deleted \(HTTP 422' "${S}/sup.err")" +check_is "idle 422: VM stopped only after the job exec ended" "tart stop" \ + "$(sed -n '/exec-i/,$p' "${S}/order.log" | grep -v 'curl DELETE' | sed -n 2p)" +check_is "idle 422: id= kept for the next sweep" 4242 "$(slot id)" +check_is "idle 422: not a failure" 0 "$(slot fails)" + +echo "-- idle_max_404_keeps_vm: DELETE 404 is not proof of idle" +reset +STUB_JOB_HANG=1 STUB_DELETE_GONE=1 SWM_UPDATE_IDLE_CHECK_S=1 SWM_IDLE_MAX_S=1 SWM_JOB_MAX_S=3 sup once +check "idle 404: job ran to SWM_JOB_MAX_S" grep -q 'reached SWM_JOB_MAX_S' "${S}/sup.err" +check_not "idle 404: not ended idle" grep -q 'ended idle after SWM_IDLE_MAX_S' "${S}/sup.err" +check "idle 404: logged as not deleted" grep -q 'runner 4242 not deleted (HTTP 404' "${S}/sup.err" + +echo "-- idle_max_disabled_at_zero: SWM_IDLE_MAX_S=0 ends no runner" +reset +STUB_JOB_HANG=1 SWM_UPDATE_IDLE_CHECK_S=1 SWM_IDLE_MAX_S=0 SWM_JOB_MAX_S=3 sup once +check "idle 0: job ran to SWM_JOB_MAX_S" grep -q 'reached SWM_JOB_MAX_S' "${S}/sup.err" +check_not "idle 0: not ended idle" grep -q 'ended idle after SWM_IDLE_MAX_S' "${S}/sup.err" +check_is "idle 0: only the teardown DELETE" 1 "$(dels)" + +echo "-- idle_max_update_path_unchanged: with an update pending, the update path and its log win" +reset +# Limit 3 s: the first check (1 s) sees only the update; integer seconds can read 1.0 s as 2. +STUB_JOB_HANG=1 SWM_UPDATE_IDLE_CHECK_S=1 SWM_IDLE_MAX_S=3 sup once & +pid=$! +wait_for "${S}/in-job" +: >"${PEND}" +wait "${pid}" +check "idle+update: update message logged" grep -q 'update pending and runner 4242 idle (DELETE: HTTP 204)' "${S}/sup.err" +check "idle+update: ended for the update" grep -q 'ended idle for the pending update' "${S}/sup.err" +check_not "idle+update: no idle-max message" grep -q 'ended idle after SWM_IDLE_MAX_S' "${S}/sup.err" +/bin/rm -f "${PEND}" + +echo "-- idle_max_young_runner_untouched: a runner younger than SWM_IDLE_MAX_S is left alone" +reset +STUB_JOB_HANG=1 SWM_UPDATE_IDLE_CHECK_S=1 SWM_IDLE_MAX_S=60 SWM_JOB_MAX_S=3 sup once +check "idle young: job ran to SWM_JOB_MAX_S" grep -q 'reached SWM_JOB_MAX_S' "${S}/sup.err" +check_not "idle young: not ended idle" grep -q 'ended idle after SWM_IDLE_MAX_S' "${S}/sup.err" +check_is "idle young: only the teardown DELETE" 1 "$(dels)" + echo "-- exec not ready in time -> VM deleted, no JIT request" reset # 2 s, not 1: the deadline has 1 s granularity, so 1 s can allow a single try. From bad45dc867b8ef7348905286edee5213ec0f7e04 Mon Sep 17 00:00:00 2001 From: Claude Code Bot Date: Mon, 23 Nov 2026 19:12:41 -0800 Subject: [PATCH 2/2] fix(swm-macos-runner): log a non-numeric SWM_IDLE_MAX_S A value such as 1h turned the idle limit off with no log line. The supervisor now logs it at each JIT handoff. install.sh already refuses such a value in the env file, so this covers a direct override only. Advances #297 --- scripts/swm-macos-runner/supervisor.sh | 3 +++ scripts/swm-macos-runner/tests/test-supervisor.sh | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/scripts/swm-macos-runner/supervisor.sh b/scripts/swm-macos-runner/supervisor.sh index bad9816..61ed0e7 100755 --- a/scripts/swm-macos-runner/supervisor.sh +++ b/scripts/swm-macos-runner/supervisor.sh @@ -354,6 +354,9 @@ run_job() { IDLE_WHY="" IDLE_REFUSAL_LOGGED=0 JIT_AT="$(now)" + if [[ ! "${SWM_IDLE_MAX_S}" =~ ^[0-9]+$ ]]; then + log "SWM_IDLE_MAX_S='${SWM_IDLE_MAX_S}' is not whole seconds; no idle limit for runner ${VM}" + fi running && BOUNDED_CHECK_S="${SWM_UPDATE_IDLE_CHECK_S}" SWM_IN=/dev/fd/3 \ bounded "${SWM_JOB_MAX_S}" "${TART_BIN}" exec -i "${VM}" /bin/bash "${GUEST_JOB}" || rc=$? ABORT_OK=0 diff --git a/scripts/swm-macos-runner/tests/test-supervisor.sh b/scripts/swm-macos-runner/tests/test-supervisor.sh index f6255a9..0922748 100755 --- a/scripts/swm-macos-runner/tests/test-supervisor.sh +++ b/scripts/swm-macos-runner/tests/test-supervisor.sh @@ -608,6 +608,12 @@ check "idle+update: ended for the update" grep -q 'ended idle for the pending up check_not "idle+update: no idle-max message" grep -q 'ended idle after SWM_IDLE_MAX_S' "${S}/sup.err" /bin/rm -f "${PEND}" +echo "-- idle_max_non_numeric_logged: a bad SWM_IDLE_MAX_S is logged, never silent" +reset +STUB_JOB_HANG=1 SWM_UPDATE_IDLE_CHECK_S=1 SWM_IDLE_MAX_S=1h SWM_JOB_MAX_S=2 sup once +check "idle bad: logged" grep -q "SWM_IDLE_MAX_S='1h' is not whole seconds" "${S}/sup.err" +check_not "idle bad: not ended idle" grep -q 'ended idle after SWM_IDLE_MAX_S' "${S}/sup.err" + echo "-- idle_max_young_runner_untouched: a runner younger than SWM_IDLE_MAX_S is left alone" reset STUB_JOB_HANG=1 SWM_UPDATE_IDLE_CHECK_S=1 SWM_IDLE_MAX_S=60 SWM_JOB_MAX_S=3 sup once