Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/pm-stack-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ jobs:
- name: Test pm-git-sync quarantine
run: bash pm-stack/scripts/pm-git-sync-quarantine.test.sh

# pm-git-sync and local commits (2026-11-22): empty rebuild marker
# commits that block the pull are kept under tags and main follows
# origin; a local commit that changes a file still refuses.
- name: Test pm-git-sync rebuild markers
run: bash pm-stack/scripts/pm-git-sync-markers.test.sh

# Script suite for #5993: pm-stack/scripts/
# tailnet-bindings.test.sh (the #781 regression pin for the
# Pad/Grist tailnet bindings) was run by no workflow. The
Expand Down
2 changes: 1 addition & 1 deletion pkgs/pm-git-sync/package.nix
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
}:
stdenv.mkDerivation {
pname = "pm-git-sync";
version = "0.2.0";
version = "0.3.0";

src = lib.fileset.toSource {
root = ../../pm-stack/scripts;
Expand Down
153 changes: 153 additions & 0 deletions pm-stack/scripts/pm-git-sync-markers.test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
#!/usr/bin/env bash
# pm-git-sync-markers.test.sh — pins what pm-git-sync does when the
# clone's main has commits origin does not (2026-11-22): empty rebuild
# markers that block the pull are kept under tags and main follows
# origin; a local commit that changes a file, or a merge, still refuses
# and moves nothing; a marker that blocks nothing is left alone.
# Self-contained: builds a throwaway origin + clone under a temp dir.
set -euo pipefail

if [ "$#" -gt 0 ]; then
echo "usage: pm-git-sync-markers.test.sh (it takes no arguments)" >&2
exit 2
fi

here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
sync="$here/pm-git-sync.sh"
tmp="$(mktemp -d)"
trap 'rm -rf "$tmp"' EXIT

export GIT_AUTHOR_NAME=test GIT_AUTHOR_EMAIL=test@example.invalid
export GIT_COMMITTER_NAME=test GIT_COMMITTER_EMAIL=test@example.invalid
export GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_NOSYSTEM=1

fail() {
echo "FAIL: $*" >&2
exit 1
}

git init -q --bare -b main "$tmp/origin.git"
git clone -q "$tmp/origin.git" "$tmp/seed" 2>/dev/null
(cd "$tmp/seed" && echo one >tracked.txt && git add . && git commit -qm one && git push -q origin main)
git clone -q "$tmp/origin.git" "$tmp/clone"
clone="$tmp/clone"

export PM_GIT_SYNC_REPO="$clone"
export PM_GIT_SYNC_QUARANTINE="$tmp/quarantine"

upstream() { # one more commit on origin
(cd "$tmp/seed" && git pull -q origin main && echo "$1" >tracked.txt && git commit -qam "$1" && git push -q origin main)
}
head_of() { git -C "$1" rev-parse HEAD; }

# 1. A marker with origin unmoved blocks nothing: it is left where it is
# (the hourly keeper pushes it), and no tag is written.
git -C "$clone" commit -q --allow-empty -m "amd 569"
marker1="$(head_of "$clone")"
out="$(bash "$sync" 2>&1)" || fail "ahead-only exited non-zero: $out"
[ "$(head_of "$clone")" = "$marker1" ] || fail "ahead-only marker was moved: $out"
[ -z "$(git -C "$clone" tag -l)" ] || fail "ahead-only wrote a tag"

# 2. Origin moves: the clone is ahead and behind at once. Two empty
# markers are kept under tags named for them and main follows origin.
git -C "$clone" commit -q --allow-empty -m "amd 570"
marker2="$(head_of "$clone")"
upstream two
out="$(bash "$sync" 2>&1)" || fail "marker case exited non-zero: $out"
[ "$(head_of "$clone")" = "$(git -C "$tmp/seed" rev-parse HEAD)" ] || fail "main did not follow origin: $out"
[ "$(cat "$clone/tracked.txt")" = two ] || fail "the pull did not land"
[ "$(git -C "$clone" rev-parse 'rebuild-marker/amd-569^{commit}')" = "$marker1" ] || fail "first marker not kept: $out"
[ "$(git -C "$clone" rev-parse 'rebuild-marker/amd-570^{commit}')" = "$marker2" ] || fail "second marker not kept: $out"
[ -z "$(git -C "$clone" status --porcelain)" ] || fail "clone not clean"
printf '%s\n' "$out" | grep -q 'moved 2 empty commit(s) aside' || fail "no summary: $out"
# The next run is an ordinary one.
out="$(bash "$sync" 2>&1)" || fail "follow-up run exited non-zero: $out"
printf '%s\n' "$out" | grep -q 'up to date' || fail "follow-up run not up to date: $out"

# 3. A marker whose name is already taken by another commit gets a tag
# of its own; the earlier tag still points at the earlier commit.
git -C "$clone" commit -q --allow-empty -m "amd 570"
marker3="$(head_of "$clone")"
upstream three
out="$(bash "$sync" 2>&1)" || fail "same-name marker exited non-zero: $out"
[ "$(git -C "$clone" rev-parse 'rebuild-marker/amd-570^{commit}')" = "$marker2" ] || fail "earlier tag was overwritten"
[ "$(git -C "$clone" rev-parse "rebuild-marker/$(git -C "$clone" rev-parse --short=12 "$marker3")^{commit}")" = "$marker3" ] ||
fail "same-name marker not kept under its own tag: $out"
[ "$(cat "$clone/tracked.txt")" = three ] || fail "pull after same-name marker did not land"

# 4. A subject that is not a plain name still gives a usable tag.
git -C "$clone" commit -q --allow-empty -m "nixos 12: weird/../subject.lock"
marker4="$(head_of "$clone")"
upstream four
out="$(bash "$sync" 2>&1)" || fail "odd subject exited non-zero: $out"
[ "$(git -C "$clone" for-each-ref --points-at "$marker4" --format='%(refname)' refs/tags | wc -l)" = 1 ] ||
fail "odd-subject marker not kept: $out"
[ "$(cat "$clone/tracked.txt")" = four ] || fail "pull after odd subject did not land"

# 5. A local commit that changes a file refuses, even beside an empty
# one, and nothing is tagged or moved.
tags_before="$(git -C "$clone" tag -l | sort)"
git -C "$clone" commit -q --allow-empty -m "amd 571"
(cd "$clone" && echo lock >flake.lock && git add flake.lock && git commit -qm "amd 572")
local_head="$(head_of "$clone")"
upstream five
if out="$(bash "$sync" 2>&1)"; then fail "a commit with a change did not refuse: $out"; fi
printf '%s\n' "$out" | grep -q 'amd 572 changes files or is a merge' || fail "wrong refusal: $out"
[ "$(head_of "$clone")" = "$local_head" ] || fail "main moved despite refusing"
[ "$(git -C "$clone" tag -l | sort)" = "$tags_before" ] || fail "tagged despite refusing"
[ "$(cat "$clone/flake.lock")" = lock ] || fail "local change lost"

# 6. So does a merge, even one that changes nothing against its first parent.
git -C "$clone" reset -q --hard "$(git -C "$clone" rev-parse origin/main~1)"
git -C "$clone" checkout -q -b side
git -C "$clone" commit -q --allow-empty -m side
git -C "$clone" checkout -q main
git -C "$clone" merge -q --no-ff -m "merge side" side
merge_head="$(head_of "$clone")"
if out="$(bash "$sync" 2>&1)"; then fail "a local merge did not refuse: $out"; fi
printf '%s\n' "$out" | grep -q 'changes files or is a merge' || fail "wrong merge refusal: $out"
[ "$(head_of "$clone")" = "$merge_head" ] || fail "main moved despite a local merge"

# 7. A rebuild that commits while the sync is at work (it does not
# coordinate with it) never loses its commit. A stand-in git makes a
# commit in the clone the first time the sync runs a chosen step.
real_git="$(command -v git)"
mkdir -p "$tmp/bin"
cat >"$tmp/bin/git" <<SHIM
#!/usr/bin/env bash
if [ "\${1:-}" = "\${SHIM_ON:-}" ] && [ ! -e "$tmp/shim-done" ]; then
: >"$tmp/shim-done"
"$real_git" -C "$clone" commit -q --allow-empty -m "amd 600"
fi
exec "$real_git" "\$@"
SHIM
chmod +x "$tmp/bin/git"
fresh() { # back on origin/main with one marker, origin one ahead
"$real_git" -C "$clone" checkout -q main 2>/dev/null || true
"$real_git" -C "$clone" reset -q --hard origin/main
"$real_git" -C "$clone" commit -q --allow-empty -m "amd 599"
upstream "$1"
rm -f "$tmp/shim-done"
}

# 7a. It lands while the markers are being tagged: main is not moved at
# all, and the next run takes both markers aside.
fresh six
if out="$(SHIM_ON=tag PATH="$tmp/bin:$PATH" bash "$sync" 2>&1)"; then fail "a commit during tagging did not refuse: $out"; fi
printf '%s\n' "$out" | grep -q 'main moved while this ran' || fail "wrong refusal for a commit during tagging: $out"
[ "$(git -C "$clone" log -1 --format=%s)" = "amd 600" ] || fail "the commit made during tagging is not on main"
out="$(bash "$sync" 2>&1)" || fail "run after a commit during tagging exited non-zero: $out"
[ "$(git -C "$clone" log -1 --format=%s 'rebuild-marker/amd-600')" = "amd 600" ] || fail "late marker not kept: $out"
[ "$(cat "$clone/tracked.txt")" = six ] || fail "pull after a commit during tagging did not land"

# 7b. It lands in the instant before main is moved: main follows origin,
# the commit is kept under a tag of its own, and the run fails loudly.
fresh seven
if out="$(SHIM_ON=reset PATH="$tmp/bin:$PATH" bash "$sync" 2>&1)"; then fail "a commit at the move did not fail: $out"; fi
printf '%s\n' "$out" | grep -q 'is NOT on main' || fail "wrong message for a commit at the move: $out"
late="$(git -C "$clone" for-each-ref --format='%(refname:short)' 'refs/tags/rebuild-marker/unexpected-*')"
[ -n "$late" ] && [ "$(git -C "$clone" log -1 --format=%s "$late")" = "amd 600" ] || fail "commit at the move not kept: $out"
[ "$(head_of "$clone")" = "$(git -C "$clone" rev-parse origin/main)" ] || fail "main did not follow origin after a commit at the move"
out="$(bash "$sync" 2>&1)" || fail "run after a commit at the move exited non-zero: $out"

echo "pm-git-sync markers suite passed."
65 changes: 65 additions & 0 deletions pm-stack/scripts/pm-git-sync.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@
# three stalls on 2026-11-17/19/20), and every one stopped the laptop
# behind main until someone moved the file by hand. Nothing is deleted:
# the file keeps its content and mtime in the quarantine folder.
# - Empty commits that only this clone has are MOVED ASIDE when they
# are what stops the pull (2026-11-22). A system rebuild leaves an
# empty "<host> <generation>" marker commit on this clone's main
# (lib/rebuild-*.sh) and does not push it. Once origin has moved too,
# main is ahead and behind at once and can never fast-forward again:
# on 2026-11-22 that left the laptop two hours and 28 merges behind
# with nothing deployed and nobody told. When every commit that only
# this clone has changes no file and is not a merge, each is kept
# under a tag rebuild-marker/<its subject> and main is moved to
# origin/main (the same thing done by hand in #2496). Nothing is
# lost: the commits stay reachable from their tags, and the tree was
# already clean. A local commit that changes a file still fails
# loudly and moves nothing: that one needs a person or an agent.
# - Otherwise git fetch + git pull --ff-only.
set -uo pipefail
# Argument contract (#3352): this script takes no command-line
Expand Down Expand Up @@ -94,6 +107,58 @@ if [ "$current" = "$remote" ]; then
echo "pm-git-sync: ok clean main, up to date at $current"
exit 0
fi
ahead="$(git rev-list --count origin/main..HEAD)"
behind="$(git rev-list --count HEAD..origin/main)"
if [ "$ahead" -gt 0 ] && [ "$behind" -gt 0 ]; then
# Ahead and behind at once: a fast-forward is impossible. Only empty,
# single-parent commits are moved aside; anything else is refused
# before a single tag is written.
while IFS= read -r commit; do
parents="$(git rev-list --parents -n 1 "$commit" | wc -w)"
if [ "$parents" -ne 2 ] || [ "$(git rev-parse "$commit^{tree}")" != "$(git rev-parse "$commit^^{tree}")" ]; then
echo "pm-git-sync: FAIL $REPO main has $ahead commit(s) origin does not and is $behind behind — not pulling."
echo "pm-git-sync: commit $(git log -1 --format='%h %s' "$commit") changes files or is a merge; it is not moved aside."
exit 1
fi
done < <(git rev-list origin/main..HEAD)
while IFS= read -r commit; do
slug="$(git log -1 --format=%s "$commit" | tr -c 'A-Za-z0-9\n' '-' | cut -c1-60)"
tag="rebuild-marker/${slug:-marker}"
if ! git check-ref-format "refs/tags/$tag" || { git rev-parse -q --verify "refs/tags/$tag" >/dev/null && [ "$(git rev-parse "refs/tags/$tag^{commit}")" != "$commit" ]; }; then
tag="rebuild-marker/$(git rev-parse --short=12 "$commit")"
fi
if ! git tag -f "$tag" "$commit" >/dev/null || [ "$(git rev-parse "refs/tags/$tag^{commit}")" != "$commit" ]; then
echo "pm-git-sync: FAIL could not keep $(git log -1 --format='%h %s' "$commit") under tag $tag — main not moved"
exit 1
fi
echo "pm-git-sync: kept empty commit $(git log -1 --format='%h "%s"' "$commit") under tag $tag"
done < <(git rev-list --reverse origin/main..HEAD)
# A rebuild commits here whenever it likes and does not coordinate
# with this service. Main is moved only if it is still where it was
# when its commits were looked at; otherwise nothing is moved and the
# next run looks again.
if [ "$(git rev-parse HEAD)" != "$current" ]; then
echo "pm-git-sync: FAIL $REPO main moved while this ran (was $current) — main not moved; the next run looks again"
exit 1
fi
# --keep refuses to touch a file with local changes; there are none.
if ! git reset --quiet --keep origin/main || [ "$(git rev-parse HEAD)" != "$remote" ]; then

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Guard the reset against a concurrently changed HEAD

If a rebuild commits to this checkout after the validation and tagging snapshots, this reset removes the new commit from main without checking or preserving it. This can overlap with normal operation: nixos/services/pm-stack/default.nix:597–602 schedules sync every 30 seconds, while lib/rebuild-simple-script.sh:31 commits in the checkout without coordination. A completed commit leaves clean files, so --keep does not protect it (git reset -h describes it as “reset HEAD but keep local changes”); the previous pull --ff-only would instead refuse the divergence. Coordinate with writers or atomically ensure that the branch being replaced still points to the validated commit before moving it.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Real, and worth closing even though the window is short. Fixed in the next commit: main is moved only if it is still where it was when its commits were examined (otherwise nothing is moved and the next run looks again), and a commit that lands between that look and the move is kept under rebuild-marker/unexpected-<hash> with a loud failure, so it is never left reachable only from the reflog. Both windows are tested with a stand-in git that commits mid-run; three planted faults in the guard are each caught.


Generated by Claude Code

echo "pm-git-sync: FAIL could not move main to origin/main after keeping $ahead empty commit(s) — check $REPO"
exit 1
fi
# And if a commit landed in the instant between that look and the
# move, it is no longer on main. It is never left unreachable: it is
# kept under a tag, and this run fails so that someone looks at it.
moved_from="$(git rev-parse -q --verify ORIG_HEAD || true)"
if [ -n "$moved_from" ] && [ "$moved_from" != "$current" ]; then
tag="rebuild-marker/unexpected-$(git rev-parse --short=12 "$moved_from")"
git tag -f "$tag" "$moved_from" >/dev/null
echo "pm-git-sync: FAIL a commit landed on main while it was being moved: $(git log -1 --format='%h "%s"' "$moved_from") is kept under tag $tag and is NOT on main — check it"
exit 1
fi
echo "pm-git-sync: moved $ahead empty commit(s) aside; updated $current -> $remote"
exit 0
fi
if ! git pull --ff-only origin main; then
echo "pm-git-sync: FAIL git pull --ff-only $current -> $remote in $REPO"
exit 1
Expand Down