From a69c4f1283076e12546092e47a172cbcdef25f3a Mon Sep 17 00:00:00 2001 From: Claude Code Bot Date: Wed, 18 Nov 2026 15:11:37 -0800 Subject: [PATCH] fix(runner-updaters): stop the NOS and swm updaters racing on git pull Both updater LaunchAgents pull the shared ~/.local/share/dev-env clone every 600 s, and launchd starts them in the same second. Two concurrent pulls both fail with "Cannot rebase onto multiple branches", because each fetch appends to the shared FETCH_HEAD. Reproduced on MIMOLETTE: 3 of 3 concurrent pairs failed. From 13:14 on 2026-11-18 every pass of both updaters failed, so neither runner got new code. Both now run `git fetch --quiet --no-write-fetch-head origin` and then `git merge --quiet --ff-only origin/main`. Five concurrent pairs of that sequence on the same clone all succeeded. A lost ref race on a new commit fails one side, which retries next interval. The log line is now "git update failed; will retry", and tests cover a merge failure and assert that neither updater runs git pull. Advances #252 --- docs/runbooks/nos-runner.md | 2 +- docs/runbooks/swm-macos-runner.md | 3 ++- scripts/nos-runner/tests/test-install.sh | 13 +++++++++--- scripts/nos-runner/update.sh | 10 ++++++---- scripts/swm-macos-runner/tests/test-update.sh | 20 +++++++++++++++---- scripts/swm-macos-runner/update.sh | 10 ++++++---- 6 files changed, 41 insertions(+), 17 deletions(-) diff --git a/docs/runbooks/nos-runner.md b/docs/runbooks/nos-runner.md index 71cfadf..8f00d4e 100644 --- a/docs/runbooks/nos-runner.md +++ b/docs/runbooks/nos-runner.md @@ -223,4 +223,4 @@ Key lines to look for: - `no IPv4 address in container ls yet`: a VM has no IP yet. Normal for a few seconds after a start. - `ntfy delivery failed`: an alert did not send. Check the topic and the network. - `updater plist changed`: run `install.sh` by hand. -- `git pull failed; will retry` (update log): the clone is behind. The updater retries. +- `git update failed; will retry` (update log): the fetch or the fast-forward failed, so the clone is behind. The updater retries. If it repeats, run `git -C ~/.local/share/dev-env fetch origin` and `git -C ~/.local/share/dev-env merge --ff-only origin/main` by hand to see the error. diff --git a/docs/runbooks/swm-macos-runner.md b/docs/runbooks/swm-macos-runner.md index c6a0161..1d87dde 100644 --- a/docs/runbooks/swm-macos-runner.md +++ b/docs/runbooks/swm-macos-runner.md @@ -76,7 +76,8 @@ Drill the alert path (touches no VM): The updater job runs `update.sh` at load and every 600 s. It always exits 0. -- It runs `git pull --ff-only` in `~/.local/share/dev-env`. A failed pull (network down, or the NOS updater holding the clone's lock) is logged and tried again on the next run. +- It runs `git fetch --no-write-fetch-head origin` and `git merge --ff-only origin/main` in `~/.local/share/dev-env`. A failure (network down, or a lost race with the NOS updater) is logged as `git update failed; will retry` and tried again on the next run. +- Not `git pull`: launchd starts this job and the NOS updater in the same second. Two pulls then both fail with `Cannot rebase onto multiple branches`, because each fetch appends to the shared `FETCH_HEAD`. That stalled both updaters on every pass on 2026-11-18. - The version is the last commit that touched `scripts/swm-macos-runner/` or `scripts/nos-runner/github-app.sh`. The installed version is in `~/.local/state/swm-macos-runner/installed`. - A hand run of `install.sh` records the version it installs, so the updater's first run does not restart the supervisor a second time. diff --git a/scripts/nos-runner/tests/test-install.sh b/scripts/nos-runner/tests/test-install.sh index 32bea0b..310ca20 100755 --- a/scripts/nos-runner/tests/test-install.sh +++ b/scripts/nos-runner/tests/test-install.sh @@ -82,7 +82,8 @@ cat >"${WORK}/bin/git" <<'STUB' #!/usr/bin/env bash echo "$*" >>"${STUB_LOG}/git" case " $* " in -*" pull "*) exit "${STUB_PULL_RC:-0}" ;; +*" fetch "*) exit "${STUB_PULL_RC:-0}" ;; +*" merge "*) exit "${STUB_MERGE_RC:-0}" ;; *" log "*) echo "${STUB_REV:-rev1}" ;; esac STUB @@ -424,7 +425,9 @@ rc=0 STUB_REV=rev1 run_update || rc=$? check "update_skips_install_when_no_change: exit 0" test "${rc}" -eq 0 check_not "update_skips_install_when_no_change: install not run" test -e "${WORK}/log/install-run" -check "update pulls with --ff-only" grep -qF -- 'pull --ff-only' "${WORK}/log/git" +check "update fetches without FETCH_HEAD" grep -qF -- 'fetch --quiet --no-write-fetch-head origin' "${WORK}/log/git" +check "update fast-forwards to origin/main" grep -qF -- 'merge --quiet --ff-only origin/main' "${WORK}/log/git" +check_not "update never runs git pull (it races the swm updater)" grep -qF -- ' pull' "${WORK}/log/git" rc=0 STUB_REV=rev2 run_update || rc=$? check "update_runs_install_on_change: exit 0" test "${rc}" -eq 0 @@ -436,9 +439,13 @@ check_not "update does not rerun install for a recorded rev" test -e "${WORK}/lo rc=0 STUB_PULL_RC=1 STUB_REV=rev3 run_update || rc=$? check "update_pull_failure_exits_0" test "${rc}" -eq 0 -check "update_pull_failure_logs" grep -qF -- 'git pull failed' "${WORK}/err" +check "update_pull_failure_logs" grep -qF -- 'git update failed' "${WORK}/err" check_not "update_pull_failure_skips_install" test -e "${WORK}/log/install-run" rc=0 +STUB_MERGE_RC=1 STUB_REV=rev3 run_update || rc=$? +check "update_merge_failure_exits_0" test "${rc}" -eq 0 +check_not "update_merge_failure_skips_install" test -e "${WORK}/log/install-run" +rc=0 STUB_INSTALL_RC=1 STUB_REV=rev4 run_update || rc=$? check "update_install_failure_exits_0" test "${rc}" -eq 0 check "update_install_failure_keeps_old_rev" grep -qF -- rev2 "${WORK}/home/.local/state/nos-runner/installed-rev" diff --git a/scripts/nos-runner/update.sh b/scripts/nos-runner/update.sh index 1474523..16e39d1 100755 --- a/scripts/nos-runner/update.sh +++ b/scripts/nos-runner/update.sh @@ -1,5 +1,5 @@ #!/bin/bash -# Updater LaunchAgent job (every 600 s): pull the clone, run install.sh when scripts/nos-runner/ changed. Always exits 0. +# Updater LaunchAgent job (every 600 s): update the clone, run install.sh when scripts/nos-runner/ changed. Always exits 0. set -uo pipefail unset CDPATH @@ -15,9 +15,11 @@ log() { printf '%s update: %s\n' "${ts}" "$*" >&2 } -# A pull failure (network down, diverged clone) is not an error: log it and try again next interval. -if ! "${GIT_BIN}" -C "${CLONE}" pull --ff-only >/dev/null 2>&1; then - log "git pull failed; will retry" +# Not `git pull`: the swm updater runs in the same second, and two pulls +# collide on FETCH_HEAD. Any failure retries. +if ! "${GIT_BIN}" -C "${CLONE}" fetch --quiet --no-write-fetch-head origin >/dev/null 2>&1 || + ! "${GIT_BIN}" -C "${CLONE}" merge --quiet --ff-only origin/main >/dev/null 2>&1; then + log "git update failed; will retry" exit 0 fi diff --git a/scripts/swm-macos-runner/tests/test-update.sh b/scripts/swm-macos-runner/tests/test-update.sh index ee14481..52440ca 100755 --- a/scripts/swm-macos-runner/tests/test-update.sh +++ b/scripts/swm-macos-runner/tests/test-update.sh @@ -24,14 +24,15 @@ mkdir -p "${WORK}/bin" # WORK is under TMPDIR, never a real clone or state dir. trap 'rm -rf "${WORK:?}"' EXIT -# git stub: pull fails on STUB_PULL_FAIL; log prints the rev of the first matching " " line. +# git stub: fetch fails on STUB_PULL_FAIL, merge on STUB_MERGE_FAIL; log prints the rev of the first matching " " line. cat >"${WORK}/bin/git" <<'STUB' #!/usr/bin/env bash echo "$*" >>"${STUB_LOG}/git" args=("$@") for ((i = 0; i < ${#args[@]}; i += 1)); do case "${args[i]}" in - pull) if [[ -n "${STUB_PULL_FAIL:-}" ]]; then exit 1; fi; exit 0 ;; + fetch) if [[ -n "${STUB_PULL_FAIL:-}" ]]; then exit 1; fi; exit 0 ;; + merge) if [[ -n "${STUB_MERGE_FAIL:-}" ]]; then exit 1; fi; exit 0 ;; log) while read -r p r; do for a in "${args[@]}"; do @@ -86,7 +87,9 @@ newrev() { printf '%s\n' "scripts/swm-macos-runner REV2" >"${L}/revs"; } echo "== no change" run_update check "exit 0" test "${RC}" -eq 0 -check "pulled the clone" grep -q -- "-C ${WORK}/clone pull --ff-only" "${L}/git" +check "fetched without FETCH_HEAD" grep -q -- "-C ${WORK}/clone fetch --quiet --no-write-fetch-head origin" "${L}/git" +check "fast-forwarded to origin/main" grep -q -- "-C ${WORK}/clone merge --quiet --ff-only origin/main" "${L}/git" +check_not "never git pull (it races the NOS updater)" grep -q -- " pull" "${L}/git" check_not "no install" test -e "${L}/install" check "mark unchanged" test "$(cat "${S}/installed")" = REV1 @@ -166,7 +169,16 @@ again STUB_PULL_FAIL=1 check "exit 0" test "${RC}" -eq 0 check_not "no install" test -e "${L}/install" check "mark unchanged" test "$(cat "${S}/installed")" = REV1 -check "logged" grep -q 'pull failed' <<<"${OUT}" +check "logged" grep -q 'git update failed' <<<"${OUT}" + +echo "== merge fails" +run_update +newrev +again STUB_MERGE_FAIL=1 +check "exit 0" test "${RC}" -eq 0 +check_not "no install" test -e "${L}/install" +check "mark unchanged" test "$(cat "${S}/installed")" = REV1 +check "logged" grep -q 'git update failed' <<<"${OUT}" echo "== install fails" run_update diff --git a/scripts/swm-macos-runner/update.sh b/scripts/swm-macos-runner/update.sh index 4ba78b7..a7f79dd 100755 --- a/scripts/swm-macos-runner/update.sh +++ b/scripts/swm-macos-runner/update.sh @@ -1,5 +1,5 @@ #!/bin/bash -# Updater LaunchAgent job (every 600 s): pull the clone and run install.sh when the runner code changed and no job runs. Always exits 0. +# Updater LaunchAgent job (every 600 s): update the clone and run install.sh when the runner code changed and no job runs. Always exits 0. set -uo pipefail unset CDPATH @@ -20,9 +20,11 @@ log() { } main() { - # A pull failure (network down, the NOS updater holding the clone's lock) is not an error: retry next interval. - if ! "${GIT_BIN}" -C "${CLONE}" pull --ff-only >/dev/null 2>&1; then - log "git pull failed; will retry" + # Not `git pull`: the NOS updater runs in the same second, and two pulls + # collide on FETCH_HEAD. Any failure retries. + if ! "${GIT_BIN}" -C "${CLONE}" fetch --quiet --no-write-fetch-head origin >/dev/null 2>&1 || + ! "${GIT_BIN}" -C "${CLONE}" merge --quiet --ff-only origin/main >/dev/null 2>&1; then + log "git update failed; will retry" return 0 fi