From 10d4ac35a240c23712aec415c5b693e7cf1ab39d Mon Sep 17 00:00:00 2001 From: repo-worker-code-z Date: Fri, 6 Nov 2026 22:48:26 -0600 Subject: [PATCH] fix(pm-stack): install.sh symlink-managed unit skip + pm-git-sync deployed-head baseline (#5396) --- pm-stack/aggregator/install.sh | 6 ++- pm-stack/aggregator/test/install-rehearsal.sh | 32 +++++++++++-- pm-stack/scripts/deploy-install-smoke.test.sh | 23 ++++++--- pm-stack/scripts/pm-git-sync.sh | 48 ++++++++++++++++--- pm-stack/scripts/pm-git-sync.test.sh | 18 +++++++ 5 files changed, 109 insertions(+), 18 deletions(-) diff --git a/pm-stack/aggregator/install.sh b/pm-stack/aggregator/install.sh index 400e1f7b18..c24d83ed15 100755 --- a/pm-stack/aggregator/install.sh +++ b/pm-stack/aggregator/install.sh @@ -120,7 +120,11 @@ if [ "$CHANGED" -eq 1 ]; then } >"$ENV_FILE" chmod 600 "$ENV_FILE" - cp "$UNIT_SRC" "$UNIT_DST" + if [ -L "$UNIT_DST" ]; then + echo "pm-aggregator.service is symlink-managed at $UNIT_DST; skipping unit copy" + else + cp "$UNIT_SRC" "$UNIT_DST" + fi cd "$SRC" bun install --silent diff --git a/pm-stack/aggregator/test/install-rehearsal.sh b/pm-stack/aggregator/test/install-rehearsal.sh index b08fd7070e..2cdf575448 100644 --- a/pm-stack/aggregator/test/install-rehearsal.sh +++ b/pm-stack/aggregator/test/install-rehearsal.sh @@ -12,11 +12,19 @@ HOME_TEST="$TMP/home" STATE="$HOME_TEST/.local/state/pm-aggregator" INSTALL_SRC="$HOME_TEST/git/code/pm-stack/aggregator" LOG="$TMP/commands.log" +REAL_CP="$(command -v cp)" mkdir -p "$BIN" "$HOME_TEST/pm-stack" "$STATE" mkdir -p "$(dirname "$INSTALL_SRC")" ln -s "$PKG" "$INSTALL_SRC" ln -s "$PKG/../systemd" "$HOME_TEST/git/code/pm-stack/systemd" ln -s "$STATE" "$HOME_TEST/pm-stack/aggregator" +UNIT_DST="$HOME_TEST/.config/systemd/user/pm-aggregator.service" +STORE_UNIT="$TMP/nix/store/home-manager-files/.config/systemd/user/pm-aggregator.service" +mkdir -p "$(dirname "$UNIT_DST")" "$(dirname "$STORE_UNIT")" +cp "$PKG/../systemd/pm-aggregator.service" "$STORE_UNIT" +printf '%s\n' '# preserved Home Manager unit' >>"$STORE_UNIT" +chmod 444 "$STORE_UNIT" +ln -s "$STORE_UNIT" "$UNIT_DST" printf '{"kept":"existing feed"}\n' >"$STATE/fleet-feed.json" cat >"$BIN/systemctl" <<'STUB' @@ -55,17 +63,32 @@ cat >"$BIN/sops" <<'STUB' printf 'test-only-placeholder\n' STUB -chmod +x "$BIN/systemctl" "$BIN/bun" "$BIN/curl" "$BIN/sops" -if TEST_LOG="$LOG" HOME="$HOME_TEST" PATH="$BIN:$PATH" \ +cat >"$BIN/cp" <<'STUB' +#!/usr/bin/env bash +set -euo pipefail +dest="${@: -1}" +if [[ "$dest" == "${TEST_UNIT_DST:?}" ]]; then + echo "unexpected attempt to copy over Home Manager unit symlink: $dest" >&2 + exit 1 +fi +exec "${TEST_REAL_CP:?}" "$@" +STUB + +chmod +x "$BIN/systemctl" "$BIN/bun" "$BIN/curl" "$BIN/sops" "$BIN/cp" +if TEST_LOG="$LOG" TEST_UNIT_DST="$UNIT_DST" TEST_REAL_CP="$REAL_CP" HOME="$HOME_TEST" PATH="$BIN:$PATH" \ bash "$PKG/install.sh" >"$TMP/worktree-refused.out" 2>&1; then echo 'install unexpectedly accepted a non-primary checkout' >&2 exit 1 fi TEST_LOG="$LOG" \ + TEST_UNIT_DST="$UNIT_DST" \ + TEST_REAL_CP="$REAL_CP" \ TEST_LIVE_VERSION="$(git -C "$PKG" rev-parse HEAD)" \ HOME="$HOME_TEST" PATH="$BIN:$PATH" \ bash "$INSTALL_SRC/install.sh" >"$TMP/install.out" TEST_LOG="$LOG" \ + TEST_UNIT_DST="$UNIT_DST" \ + TEST_REAL_CP="$REAL_CP" \ TEST_LIVE_VERSION="$(git -C "$PKG" rev-parse HEAD)" \ HOME="$HOME_TEST" PATH="$BIN:$PATH" \ bash "$INSTALL_SRC/install.sh" >"$TMP/install-second.out" @@ -86,7 +109,10 @@ grep -Fx "PM_AGG_COVERAGE_SNAPSHOT_FILE=$STATE/coverage-snapshot.json" "$STATE/. grep -Fx 'WorkingDirectory=%h/git/code/pm-stack/aggregator' \ "$HOME_TEST/.config/systemd/user/pm-aggregator.service" >/dev/null grep -Fx 'EnvironmentFile=%h/.local/state/pm-aggregator/.env' \ - "$HOME_TEST/.config/systemd/user/pm-aggregator.service" >/dev/null + "$UNIT_DST" >/dev/null +grep -Fx '# preserved Home Manager unit' "$UNIT_DST" >/dev/null +test -L "$UNIT_DST" +test "$(readlink "$UNIT_DST")" = "$STORE_UNIT" grep -Fx 'WorkingDirectory=%h/git/code/pm-stack/aggregator' \ "$HOME_TEST/.config/systemd/user/quota-gatherer.service" >/dev/null grep -Fx 'Environment=QUOTA_CURRENT_PATH=%h/.local/state/pm-aggregator/quota-current.json' \ diff --git a/pm-stack/scripts/deploy-install-smoke.test.sh b/pm-stack/scripts/deploy-install-smoke.test.sh index 56ad93add0..85a1813238 100755 --- a/pm-stack/scripts/deploy-install-smoke.test.sh +++ b/pm-stack/scripts/deploy-install-smoke.test.sh @@ -112,15 +112,22 @@ printf 'systemctl %s\n' "$*" >>"${PM_TEST_COMMAND_LOG:?}" # Model ExecMainStartTimestampMonotonic: a value that changes on restart, # so installers that prove a fresh process start (ready-for-agent) can pass. start_ts_file="${PM_TEST_ROOT:?}/.systemctl-start-ts" +main_pid_file="${PM_TEST_ROOT:?}/.systemctl-main-pid" case " $* " in *' restart '*) cur=1000 if [[ -f "$start_ts_file" ]]; then cur="$(cat "$start_ts_file")"; fi printf '%s\n' "$((cur + 1000))" >"$start_ts_file" + pid=1000 + if [[ -f "$main_pid_file" ]]; then pid="$(cat "$main_pid_file")"; fi + printf '%s\n' "$((pid + 1))" >"$main_pid_file" ;; *' show '*ExecMainStartTimestampMonotonic*) if [[ -f "$start_ts_file" ]]; then cat "$start_ts_file"; else printf '1000\n'; fi ;; + *' show '*MainPID*) + if [[ -f "$main_pid_file" ]]; then cat "$main_pid_file"; else printf '1000\n'; fi + ;; *' is-active '*|*' status '*) printf 'active\n' ;; esac exit 0 @@ -295,7 +302,8 @@ if "$BASH_BIN" -n "$READY_INSTALL"; then pass 'ready-for-agent/install.sh syntax # runs entirely inside a disposable HOME. Side-effecting tools are stubbed. AGG_SRC="$TMP/aggregator-src" AGG_HOME="$TMP/aggregator-home" -mkdir -p "$AGG_SRC" "$AGG_HOME/secrets" "$AGG_HOME/pm-stack/systemd" +AGG_EXPECTED_SRC="$AGG_HOME/git/code/pm-stack/aggregator" +mkdir -p "$AGG_SRC" "$AGG_HOME/secrets" "$AGG_HOME/git/code/pm-stack/systemd" cp "$AGG_INSTALL" "$AGG_SRC/install.sh" printf '%s\n' '{"name":"smoke-aggregator","version":"1.0.0"}' >"$AGG_SRC/package.json" printf '%s\n' 'fake lockfile' >"$AGG_SRC/bun.lock" @@ -308,30 +316,31 @@ printf '%s\n' '# fixture' >"$AGG_SRC/scripts/smoke.sh" printf '%s\n' '// fixture' >"$AGG_SRC/opencode-tools/smoke.ts" printf '%s\n' '[Service]' >"$AGG_SRC/systemd/quota-gatherer.service" printf '%s\n' '[Timer]' >"$AGG_SRC/systemd/quota-gatherer.timer" -printf '%s\n' '[Service]' >"$AGG_HOME/pm-stack/systemd/pm-aggregator.service" +printf '%s\n' '[Service]' >"$AGG_HOME/git/code/pm-stack/systemd/pm-aggregator.service" printf '%s\n' 'fake encrypted aggregator fixture' >"$AGG_HOME/secrets/pm-stack.sops.yaml" +ln -s "$AGG_SRC" "$AGG_EXPECTED_SRC" AGG_SOPS_LOG="$TMP/aggregator-sops.log" AGG_OUTPUT="$TMP/aggregator-install.out" if env HOME="$AGG_HOME" PATH="$STUB_BIN:$SYSTEM_PATH" \ PM_TEST_ROOT="$TMP" PM_TEST_SECRETS="$AGG_HOME/secrets/pm-stack.sops.yaml" \ PM_TEST_COMMAND_LOG="$COMMAND_LOG" PM_TEST_SOPS_LOG="$AGG_SOPS_LOG" \ PM_TEST_BUILD_VERSION=smoke-build-v1 TAILNET_IP=100.64.20.21 \ - "$BASH_BIN" "$AGG_SRC/install.sh" >"$AGG_OUTPUT" 2>&1; then + "$BASH_BIN" "$AGG_EXPECTED_SRC/install.sh" >"$AGG_OUTPUT" 2>&1; then pass 'aggregator install smoke completes using temporary HOME and stubs' else cat "$AGG_OUTPUT" fail 'aggregator install smoke completes using temporary HOME and stubs' fi -if contains "$AGG_HOME/pm-stack/aggregator/.env" 'KANEO_ADMIN_EMAIL=smoke-admin@example.invalid' && - contains "$AGG_HOME/pm-stack/aggregator/.env" 'GRIST_API_KEY=fake-grist-key' && - contains "$AGG_HOME/pm-stack/aggregator/.env" 'PM_AGG_BUILD_VERSION=smoke-build-v1'; then +if contains "$AGG_HOME/.local/state/pm-aggregator/.env" 'KANEO_ADMIN_EMAIL=smoke-admin@example.invalid' && + contains "$AGG_HOME/.local/state/pm-aggregator/.env" 'GRIST_API_KEY=fake-grist-key' && + contains "$AGG_HOME/.local/state/pm-aggregator/.env" 'PM_AGG_BUILD_VERSION=smoke-build-v1'; then pass 'aggregator renders environment from stubbed SOPS fields' else fail 'aggregator renders environment from stubbed SOPS fields' fi if [[ -f "$AGG_HOME/.config/systemd/user/pm-aggregator.service" && -f "$AGG_HOME/.config/systemd/user/quota-gatherer.service" && - -f "$AGG_HOME/pm-stack/aggregator/.deploy-tree-hash" ]]; then + -f "$AGG_HOME/.local/state/pm-aggregator/.deploy-tree-hash" ]]; then pass 'aggregator copies package and unit files only under temporary HOME' else fail 'aggregator copies package and unit files only under temporary HOME' diff --git a/pm-stack/scripts/pm-git-sync.sh b/pm-stack/scripts/pm-git-sync.sh index 4164355215..91e6de8483 100755 --- a/pm-stack/scripts/pm-git-sync.sh +++ b/pm-stack/scripts/pm-git-sync.sh @@ -50,6 +50,28 @@ if ! git fetch origin --quiet; then exit 1 fi +# The baseline is the last HEAD for which every changed subtree's +# redeploy completed. Keeping it across runs also catches a hand-pulled +# HEAD: in that case HEAD already equals origin/main, but still differs +# from this deployed baseline. +DEPLOYED_HEAD_FILE="$STATE_DIR/deployed-head" +current="$(git rev-parse HEAD)" +if [ -f "$DEPLOYED_HEAD_FILE" ]; then + old="$(cat "$DEPLOYED_HEAD_FILE")" + if ! git cat-file -e "$old^{commit}" 2>/dev/null; then + echo "pm-git-sync: FAIL deployed-head baseline '$old' is not a commit in $REPO" + exit 1 + fi +else + # On the first run, assume the currently checked-out tree is the + # deployed one; subsequent runs retain the exact deployed baseline. + old="$current" + if ! mkdir -p "$STATE_DIR" || ! printf '%s\n' "$old" >"$DEPLOYED_HEAD_FILE"; then + echo "pm-git-sync: FAIL could not initialize deployed-head baseline in $STATE_DIR" + exit 1 + fi +fi + # --- owed redeploys (#2391) ------------------------------------------------- # One record per unit: $STATE_DIR/owed-, line 1 = subtree, # line 2 = dependency step (may be empty). Records are written only @@ -142,20 +164,32 @@ finish() { echo "pm-git-sync: FAIL redeploy(s) still owed for: $(echo "$owed" | tr '\n' ' ')" exit 1 fi + local baseline_tmp="$DEPLOYED_HEAD_FILE.tmp.$$" + if ! printf '%s\n' "$new" >"$baseline_tmp" || ! mv -f "$baseline_tmp" "$DEPLOYED_HEAD_FILE"; then + rm -f "$baseline_tmp" + echo "pm-git-sync: FAIL could not persist deployed-head baseline $new in $STATE_DIR" + exit 1 + fi echo "$1" exit 0 } -old="$(git rev-parse HEAD)" -new="$(git rev-parse origin/main)" -if [ "$old" = "$new" ]; then - finish "pm-git-sync: ok clean main, up to date at $old" +remote="$(git rev-parse origin/main)" +if [ "$current" != "$remote" ]; then + if ! git pull --ff-only origin main; then + echo "pm-git-sync: FAIL git pull --ff-only $current -> $remote in $REPO" + exit 1 + fi fi -if ! git pull --ff-only origin main; then - echo "pm-git-sync: FAIL git pull --ff-only $old -> $new in $REPO" +new="$(git rev-parse HEAD)" +if ! git merge-base --is-ancestor "$old" "$new"; then + echo "pm-git-sync: FAIL deployed-head baseline $old is not an ancestor of current HEAD $new" exit 1 fi -echo "pm-git-sync: updated $old -> $new" +if [ "$old" = "$new" ]; then + finish "pm-git-sync: ok clean main, up to date at $new" +fi +echo "pm-git-sync: updated deployed baseline $old -> $new" redeploy() { # local subtree="$1" unit="$2" dep="$3" diff --git a/pm-stack/scripts/pm-git-sync.test.sh b/pm-stack/scripts/pm-git-sync.test.sh index 5903c877bf..e278348b60 100755 --- a/pm-stack/scripts/pm-git-sync.test.sh +++ b/pm-stack/scripts/pm-git-sync.test.sh @@ -80,6 +80,7 @@ export PM_TEST_BUN_FAIL="$SCRATCH/bun-fail" export PM_TEST_RESTART_FAIL="$SCRATCH/restart-fail" export PM_TEST_PROJ_DEP_FAIL="$SCRATCH/proj-dep-fail" STATE_DIR="$SCRATCH/state" +DEPLOYED_HEAD="$STATE_DIR/deployed-head" run_sync() { PM_GIT_SYNC_REPO="$REPO" \ @@ -103,16 +104,32 @@ OWED_DISPATCH="$STATE_DIR/owed-pm-dispatch.service" run_sync >/dev/null 2>&1 check "up-to-date run exits 0" test $? -eq 0 check "up-to-date run restarts nothing" log_lacks "restart" +check "up-to-date run records deployed HEAD" test "$(cat "$DEPLOYED_HEAD")" = "$(git -C "$REPO" rev-parse HEAD)" + +# --- 0b. a hand-pulled HEAD still owes its subtree redeploy ------------------- +push_change pm-stack/dispatch/index.ts 'dispatch hand-pulled' +git -C "$REPO" pull --ff-only origin main >/dev/null 2>&1 +: >"$STUB_LOG" +run_sync >/dev/null 2>&1 +check "hand-pulled HEAD run exits 0" test $? -eq 0 +check "hand-pulled subtree change redeploys dispatch" log_has "restart pm-dispatch" +check "successful hand-pulled deploy advances baseline" test "$(cat "$DEPLOYED_HEAD")" = "$(git -C "$REPO" rev-parse HEAD)" +: >"$STUB_LOG" +run_sync >/dev/null 2>&1 +check "repeat run after hand-pulled deploy exits 0" test $? -eq 0 +check "repeat run does not redeploy the same HEAD" log_lacks "restart" # --- 1. dep step fails: no restart, owed recorded, loud exit ------------------ push_change pm-stack/aggregator/package.json '{"name":"agg","version":"2"}' : >"$STUB_LOG" +BASELINE_BEFORE_FAILURE="$(cat "$DEPLOYED_HEAD")" touch "$PM_TEST_BUN_FAIL" run_sync >/dev/null 2>&1 check "dep-fail run exits non-zero" test $? -ne 0 check "dep step was attempted" log_has "bun install" check "aggregator NOT restarted after dep failure" log_lacks "restart pm-aggregator" check "owed record exists for aggregator" test -e "$OWED_AGG" +check "failed redeploy does not advance deployed baseline" test "$(cat "$DEPLOYED_HEAD")" = "$BASELINE_BEFORE_FAILURE" # --- 2. old==new run retries the owed redeploy -------------------------------- : >"$STUB_LOG" @@ -139,6 +156,7 @@ run_sync >/dev/null 2>&1 check "recovery run exits 0" test $? -eq 0 check "owed retry restarted the aggregator" log_has "restart pm-aggregator" check "owed record cleared" test ! -e "$OWED_AGG" +check "recovery advances deployed baseline" test "$(cat "$DEPLOYED_HEAD")" = "$(git -C "$REPO" rev-parse HEAD)" # --- 4. restart failure is owed and retried on old==new ------------------------ push_change pm-stack/dispatch/index.ts 'dispatch v2'