From 329a1122461c20c4a7155c0a7e64d15ceb4d8394 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Mon, 2 Nov 2026 16:09:27 -0600 Subject: [PATCH 1/2] fix(agent-backup): PREV is newest-before-stamp and latest never regresses (#4602) PREV compared against the greatest snapshot name, so a future-dated directory became the dedup baseline: a duplicate of the true previous snapshot was kept, and latest repointed backwards. Select the newest snapshot strictly before STAMP instead, and only move latest forward when the new snapshot is the newest on disk. Regression test covers duplicate-of-true-previous, mirror, and newest-run cases with the existing fake-date pattern. --- pkgs/agent-backup/backup.sh | 18 +++++++++++++--- pkgs/agent-backup/test-backup.sh | 37 ++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/pkgs/agent-backup/backup.sh b/pkgs/agent-backup/backup.sh index 82af5603d0..107568ff1b 100644 --- a/pkgs/agent-backup/backup.sh +++ b/pkgs/agent-backup/backup.sh @@ -83,9 +83,14 @@ 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. +# 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 plain string +# comparison orders them chronologically, same-second suffixed names (#3340) +# included: a bare stamp sorts before its suffixed siblings. PREV="$(find "$ROOT" -mindepth 1 -maxdepth 1 -type d -not -name "$STAMP" -printf '%f\n' | - sort | tail -n1 || true)" + awk -v stamp="$STAMP" '$0 < stamp' | sort | tail -n1 || true)" if [ -n "$PREV" ] && diff -rq "$ROOT/$PREV" "$DEST" >/dev/null 2>&1; then rm -rf "$DEST" @@ -93,5 +98,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' | + 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" From 398967bbbc58d34092f7318b972e8669b942939c Mon Sep 17 00:00:00 2001 From: dezren39 Date: Mon, 2 Nov 2026 16:52:19 -0600 Subject: [PATCH 2/2] fix(agent-backup): PREV without awk so the systemd service PATH works (#4602) The service PATH is coreutils+diffutils+findutils only, so the awk filter failed (masked by || true) and left PREV empty. Use a sorted sentinel scan with bash builtins instead; same newest-before-stamp semantics, same-second suffixed names keep in-second order. --- pkgs/agent-backup/backup.sh | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/pkgs/agent-backup/backup.sh b/pkgs/agent-backup/backup.sh index 107568ff1b..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" @@ -86,11 +87,18 @@ fi # 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 plain string -# comparison orders them chronologically, same-second suffixed names (#3340) -# included: a bare stamp sorts before its suffixed siblings. -PREV="$(find "$ROOT" -mindepth 1 -maxdepth 1 -type d -not -name "$STAMP" -printf '%f\n' | - awk -v stamp="$STAMP" '$0 < stamp' | sort | tail -n1 || true)" +# 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" @@ -101,7 +109,7 @@ fi # 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' | - sort | tail -n1 || true)" + LC_ALL=C sort | tail -n1 || true)" if [ -z "$NEWEST" ] || [[ "$STAMP" > "$NEWEST" ]]; then ln -sfn "$STAMP" "$ROOT/latest"