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
28 changes: 24 additions & 4 deletions pkgs/agent-backup/backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -83,15 +84,34 @@ 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"
echo "agent-backup: no change since $PREV" >&2
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
Comment on lines +111 to +114

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 Exclude in-flight snapshots from the latest-pointer guard

When two backups overlap, the newer directory may only be an incomplete reservation. With latest pointing to an older snapshot P, run A can finish copying changed content X while run B reserves a newer timestamp and pauses before copying. A sees B here and skips updating latest; B then copies X, deduplicates against A, and deletes itself. Both runs succeed, but latest remains on P rather than the newest retained snapshot A, and subsequent unchanged runs exit without repairing it. Serialize deduplication/publication or distinguish completed snapshots from reservations before suppressing the update.

Useful? React with 👍 / 👎.

ln -sfn "$STAMP" "$ROOT/latest"
fi
echo "agent-backup: snapshot $STAMP ($copied files)" >&2
37 changes: 37 additions & 0 deletions pkgs/agent-backup/test-backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Loading