diff --git a/pkgs/agent-backup/backup.sh b/pkgs/agent-backup/backup.sh index 82af5603d0..9faa902a33 100644 --- a/pkgs/agent-backup/backup.sh +++ b/pkgs/agent-backup/backup.sh @@ -20,6 +20,7 @@ # protects against accidental deletion and bad edits, not against disk theft. set -euo pipefail +export LC_ALL=C ROOT="${AGENT_BACKUP_DIR:-$HOME/.local/share/agent-backups}" mkdir -p "$ROOT" @@ -83,9 +84,21 @@ if [ "$copied" -eq 0 ]; then fi # Drop the snapshot if it is identical to the previous one. `latest` is a -# symlink, so exclude it when looking for the prior directory. -PREV="$(find "$ROOT" -mindepth 1 -maxdepth 1 -type d -not -name "$STAMP" -printf '%f\n' | - sort | tail -n1 || true)" +# symlink, so -type d already excludes it when looking for prior directories. +# PREV is the newest snapshot strictly before this run's STAMP: a future-dated +# directory (clock skew, manual copy) must not stand in for the true previous +# snapshot (#4602). Names are fixed-width timestamps, so byte order (LC_ALL=C) +# is chronological, same-second suffixed names (#3340) included: a bare stamp +# sorts before its suffixed siblings. The sentinel trick keeps this to +# findutils + coreutils + bash builtins only: the systemd service PATH has no +# awk/grep/sed, so no filter pipeline may assume them. +PREV="" +while IFS= read -r name; do + if [[ "$name" == "$STAMP" ]]; then + break + fi + PREV="$name" +done < <({ find "$ROOT" -mindepth 1 -maxdepth 1 -type d -not -name "$STAMP" -printf '%f\n'; printf '%s\n' "$STAMP"; } | LC_ALL=C sort -u) if [ -n "$PREV" ] && diff -rq "$ROOT/$PREV" "$DEST" >/dev/null 2>&1; then rm -rf "$DEST" @@ -93,5 +106,12 @@ if [ -n "$PREV" ] && diff -rq "$ROOT/$PREV" "$DEST" >/dev/null 2>&1; then exit 0 fi -ln -sfn "$STAMP" "$ROOT/latest" +# Only move `latest` forward. A newer snapshot already on disk (future-dated +# or concurrent) keeps the pointer; this run must never regress it (#4602). +NEWEST="$(find "$ROOT" -mindepth 1 -maxdepth 1 -type d -not -name "$STAMP" -printf '%f\n' | + LC_ALL=C sort | tail -n1 || true)" + +if [ -z "$NEWEST" ] || [[ "$STAMP" > "$NEWEST" ]]; then + ln -sfn "$STAMP" "$ROOT/latest" +fi echo "agent-backup: snapshot $STAMP ($copied files)" >&2 diff --git a/pkgs/agent-backup/test-backup.sh b/pkgs/agent-backup/test-backup.sh index 185515ee90..dd933f2b18 100644 --- a/pkgs/agent-backup/test-backup.sh +++ b/pkgs/agent-backup/test-backup.sh @@ -48,4 +48,41 @@ latest="$(readlink "${AGENT_BACKUP_DIR}/latest")" [[ ${latest} == "2026-01-01T00-00-00-0000000001" ]] [[ -e "${AGENT_BACKUP_DIR}/latest" ]] +# A future-dated snapshot must neither become the dedup baseline nor lose the +# `latest` pointer (#4602). Seed a true previous snapshot and a newer +# future-dated one, with `latest` already on the future snapshot. +mkdir -p "${AGENT_BACKUP_DIR}/2026-01-02T00-00-00/.claude" \ + "${AGENT_BACKUP_DIR}/2027-01-01T00-00-00/.claude" +printf 'OLD\n' >"${AGENT_BACKUP_DIR}/2026-01-02T00-00-00/.claude/settings.json" +printf 'FUTURE\n' >"${AGENT_BACKUP_DIR}/2027-01-01T00-00-00/.claude/settings.json" +ln -sfn "2027-01-01T00-00-00" "${AGENT_BACKUP_DIR}/latest" + +# Content matching the true previous snapshot is a duplicate even though a +# newer snapshot exists: the new directory is removed and `latest` is kept. +export AGENT_BACKUP_TEST_STAMP="2026-06-01T00-00-00" +printf 'OLD\n' >"${HOME}/.claude/settings.json" +bash "${SCRIPT_DIR}/backup.sh" >/dev/null 2>&1 +[[ ! -e "${AGENT_BACKUP_DIR}/2026-06-01T00-00-00" ]] +latest="$(readlink "${AGENT_BACKUP_DIR}/latest")" +[[ ${latest} == "2027-01-01T00-00-00" ]] + +# Content matching the future snapshot but differing from the true previous +# one is a real transition: the new snapshot is kept, and `latest` still does +# not regress to it. +export AGENT_BACKUP_TEST_STAMP="2026-06-02T00-00-00" +printf 'FUTURE\n' >"${HOME}/.claude/settings.json" +bash "${SCRIPT_DIR}/backup.sh" >/dev/null 2>&1 +actual="$(cat "${AGENT_BACKUP_DIR}/2026-06-02T00-00-00/.claude/settings.json")" +[[ ${actual} == FUTURE ]] +latest="$(readlink "${AGENT_BACKUP_DIR}/latest")" +[[ ${latest} == "2027-01-01T00-00-00" ]] + +# A run newer than everything on disk moves `latest` forward again. +export AGENT_BACKUP_TEST_STAMP="2028-01-01T00-00-00" +printf 'NEWER\n' >"${HOME}/.claude/settings.json" +bash "${SCRIPT_DIR}/backup.sh" >/dev/null 2>&1 +latest="$(readlink "${AGENT_BACKUP_DIR}/latest")" +[[ ${latest} == "2028-01-01T00-00-00" ]] + echo "agent-backup same-second collision regression: PASS" +echo "agent-backup future-dated PREV/latest regression: PASS"