diff --git a/pm-stack/ready-for-agent/install.sh b/pm-stack/ready-for-agent/install.sh index 56b10e6586..87038e0d52 100644 --- a/pm-stack/ready-for-agent/install.sh +++ b/pm-stack/ready-for-agent/install.sh @@ -29,6 +29,18 @@ PINNED_VERSION="1.1.0" cd "$(dirname "$0")" DEPLOY_DIR="${PWD}" +# Pending-restart marker (#4839): a package upgrade that never reached +# its restart (e.g. the clone step fails under `set -e` after npm +# replaced the binary) must not be forgotten — the next run would see +# matching on-disk versions and report the pinned version running while +# the old process serves. Written when the package changes, cleared +# only after a verified restart/start. +PENDING_RESTART_FILE="${XDG_STATE_HOME:-${HOME}/.local/state}/ready-for-agent.pending-restart" +restart_pending=false +if [[ -f ${PENDING_RESTART_FILE} ]]; then + restart_pending=true +fi + # Capture the serving process before changing its executable or unit. service_was_active=false previous_main_pid="" @@ -51,6 +63,8 @@ if [[ ${current} != "${PINNED_VERSION}" ]]; then package_changed=true echo "installing ready-for-agent@${PINNED_VERSION} (found: ${current:-none})" npm install -g --prefix "${HOME}/.local" "ready-for-agent@${PINNED_VERSION}" + mkdir -p "$(dirname "${PENDING_RESTART_FILE}")" + printf '%s\n' "${PINNED_VERSION}" >"${PENDING_RESTART_FILE}" else echo "ready-for-agent@${PINNED_VERSION} already installed" fi @@ -90,7 +104,7 @@ if ! cmp -s "${source_unit}" "${installed_unit}"; then fi service_changed=false -if [[ ${package_changed} == true || ${unit_changed} == true ]]; then +if [[ ${package_changed} == true || ${unit_changed} == true || ${restart_pending} == true ]]; then service_changed=true fi @@ -103,7 +117,7 @@ if [[ ${service_was_active} == true ]]; then systemctl --user restart ready-for-agent.service service_action="restarted" else - echo "no package or unit changes detected; leaving ready-for-agent.service running" + echo "no package or unit changes detected and no restart pending; leaving ready-for-agent.service running" service_action="unchanged" fi else @@ -153,6 +167,13 @@ for ((i = 1; i <= 30; i++)); do exit 1 ;; esac + # The new process is verified serving the installed package: any + # pending restart from an earlier interrupted deployment is now + # satisfied. The unchanged path never clears — it is only reached + # with no marker pending, so a marker always forces a restart first. + if [[ ${service_action} == restarted || ${service_action} == started ]]; then + rm -f "${PENDING_RESTART_FILE}" + fi systemctl --user --no-pager status ready-for-agent.service | head -5 exit 0 fi diff --git a/pm-stack/ready-for-agent/test-install.sh b/pm-stack/ready-for-agent/test-install.sh index a89f94f6b0..f9ad1808e1 100755 --- a/pm-stack/ready-for-agent/test-install.sh +++ b/pm-stack/ready-for-agent/test-install.sh @@ -14,6 +14,7 @@ fail() { run_case() { local name="$1" initial_version="$2" active="$3" unit_matches="$4" + local extra_args="${5:-}" git_fail_once="${6:-0}" local case_root="${TEST_ROOT}/${name}" local home="${case_root}/home" deploy="${case_root}/deploy" local stub_bin="${case_root}/bin" state="${case_root}/state" @@ -47,6 +48,7 @@ LAUNCHER printf '0\n' >"${state}/pid" printf '0\n' >"${state}/start" fi + printf '%s\n' "${git_fail_once}" >"${state}/git_fail_once" : >"${log}" cat >"${stub_bin}/systemctl" <<'SYSTEMCTL' @@ -101,6 +103,20 @@ printf 'ready-for-agent %s\n' "$(cat "${HOME}/.ready-agent-version")" LAUNCHER chmod +x "${HOME}/.local/bin/ready-for-agent" NPM + cat >"${stub_bin}/git" <<'GIT' +#!/usr/bin/env bash +set -euo pipefail +# Fail once when the case arms it (npm-ok / git-fail sequence), then +# succeed; clone creates its target dir like the real thing. +if [[ $(cat "${STUB_STATE}/git_fail_once") == 1 ]]; then + printf '0\n' > "${STUB_STATE}/git_fail_once" + exit 1 +fi +if [[ ${1:-} == clone ]]; then + mkdir -p "${@: -1}" +fi +exit 0 +GIT cat >"${stub_bin}/curl" <<'CURL' #!/usr/bin/env bash exit 0 @@ -109,10 +125,36 @@ CURL #!/usr/bin/env bash exit 0 SLEEP - chmod +x "${stub_bin}/systemctl" "${stub_bin}/npm" "${stub_bin}/curl" "${stub_bin}/sleep" + chmod +x "${stub_bin}/systemctl" "${stub_bin}/npm" "${stub_bin}/git" "${stub_bin}/curl" "${stub_bin}/sleep" + + if [[ ${name} == clone-fail-retry ]]; then + # #4839: npm upgrades the package, then the clone step fails; the + # retry must restart (pending marker), not report the pinned + # version already running, and must clear the marker on success. + local marker="${home}/.local/state/ready-for-agent.pending-restart" retry_output + if output="$(HOME="${home}" XDG_STATE_HOME="${home}/.local/state" PATH="${stub_bin}:${ORIGINAL_PATH}" STUB_STATE="${state}" STUB_LOG="${log}" \ + bash "${deploy}/install.sh" ${extra_args} 2>&1)"; then + printf '%s\n' "${output}" >&2 + fail "${name}: first run (failing clone) unexpectedly succeeded" + fi + [[ -f ${marker} ]] || fail "${name}: pending-restart marker missing after failed run" + ! grep -Fxq restart "${log}" || fail "${name}: restarted before the clone failure" + : >"${log}" + if ! retry_output="$(HOME="${home}" XDG_STATE_HOME="${home}/.local/state" PATH="${stub_bin}:${ORIGINAL_PATH}" STUB_STATE="${state}" STUB_LOG="${log}" \ + bash "${deploy}/install.sh" ${extra_args} 2>&1)"; then + printf '%s\n' "${retry_output}" >&2 + fail "${name}: retry exited unsuccessfully" + fi + grep -Fxq restart "${log}" || fail "${name}: retry did not restart" + printf '%s\n' "${retry_output}" | grep -Fq 'after restart' || fail "${name}: retry missing after-restart report" + printf '%s\n' "${retry_output}" | grep -Fq 'already running' && fail "${name}: retry falsely reported no-op" + [[ ! -f ${marker} ]] || fail "${name}: marker not cleared after verified restart" + echo "PASS: ${name}" + return 0 + fi - if ! output="$(HOME="${home}" PATH="${stub_bin}:${ORIGINAL_PATH}" STUB_STATE="${state}" STUB_LOG="${log}" \ - bash "${deploy}/install.sh" 2>&1)"; then + if ! output="$(HOME="${home}" XDG_STATE_HOME="${home}/.local/state" PATH="${stub_bin}:${ORIGINAL_PATH}" STUB_STATE="${state}" STUB_LOG="${log}" \ + bash "${deploy}/install.sh" ${extra_args} 2>&1)"; then printf '%s\n' "${output}" >&2 fail "${name}: installer exited unsuccessfully" fi @@ -152,3 +194,4 @@ run_case unit-change-active 1.1.0 active false run_case unchanged-active 1.1.0 active true run_case inactive 1.1.0 inactive true run_case first-install missing inactive missing +run_case clone-fail-retry 1.0.0 active true clone 1