diff --git a/pkgs/agent-backup/backup.sh b/pkgs/agent-backup/backup.sh index 85a9d580f4..faa6483061 100644 --- a/pkgs/agent-backup/backup.sh +++ b/pkgs/agent-backup/backup.sh @@ -25,9 +25,25 @@ ROOT="${AGENT_BACKUP_DIR:-$HOME/.local/share/agent-backups}" mkdir -p "$ROOT" chmod 700 "$ROOT" -STAMP="$(date -u +%Y-%m-%dT%H-%M-%S)" +BASE_STAMP="$(date -u +%Y-%m-%dT%H-%M-%S)" +STAMP="$BASE_STAMP" DEST="$ROOT/$STAMP" +# Reserve the destination atomically before copying. Two invocations can +# receive the same second-resolution timestamp; only one may own the bare +# timestamp, and later invocations get deterministic suffixes. The no-change +# cleanup below is then limited to a directory this invocation created. +suffix=0 +while ! mkdir -m 700 -- "$DEST" 2>/dev/null; do + if [[ ! -e "${DEST}" ]] && [[ ! -L "${DEST}" ]]; then + echo "agent-backup: cannot create snapshot directory ${DEST}" >&2 + exit 1 + fi + suffix=$((suffix + 1)) + STAMP="${BASE_STAMP}-$(printf '%010d' "${suffix}")" + DEST="$ROOT/$STAMP" +done + # Small, irreplaceable things only. Missing paths are skipped silently -- not # every agent is signed in on every machine. CANDIDATES=( diff --git a/pkgs/agent-backup/test-backup.sh b/pkgs/agent-backup/test-backup.sh new file mode 100644 index 0000000000..b15ee5b1dc --- /dev/null +++ b/pkgs/agent-backup/test-backup.sh @@ -0,0 +1,51 @@ +#!/usr/bin/env bash +set -euo pipefail + +SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)" +TMP="$(mktemp -d)" +trap 'rm -rf "$TMP"' EXIT + +mkdir -p "${TMP}/bin" "${TMP}/home/.claude" "${TMP}/backups/2025-12-31T23-59-59/.claude" +printf 'OLD\n' > "${TMP}/backups/2025-12-31T23-59-59/.claude/settings.json" +cat > "${TMP}/bin/date" <<'EOF' +#!/usr/bin/env bash +printf '%s\n' "${AGENT_BACKUP_TEST_STAMP:?}" +EOF +chmod +x "${TMP}/bin/date" + +export HOME="${TMP}/home" +export AGENT_BACKUP_DIR="${TMP}/backups" +export AGENT_BACKUP_TEST_STAMP="2026-01-01T00-00-00" +export PATH="${TMP}/bin:${PATH}" + +# The first run owns the bare timestamp and must preserve its snapshot. +printf 'NEW\n' > "${HOME}/.claude/settings.json" +bash "${SCRIPT_DIR}/backup.sh" >/dev/null 2>&1 +actual="$(cat "${AGENT_BACKUP_DIR}/2026-01-01T00-00-00/.claude/settings.json")" +latest="$(readlink "${AGENT_BACKUP_DIR}/latest")" +[[ "${actual}" = NEW ]] +[[ "${latest}" = "2026-01-01T00-00-00" ]] + +# A second run with the same timestamp gets its own directory. Even when its +# content matches the older snapshot, it must not delete the first run's data. +printf 'OLD\n' > "${HOME}/.claude/settings.json" +bash "${SCRIPT_DIR}/backup.sh" >/dev/null 2>&1 +second="${AGENT_BACKUP_DIR}/2026-01-01T00-00-00-0000000001" +actual="$(cat "${second}/.claude/settings.json")" +first="$(cat "${AGENT_BACKUP_DIR}/2026-01-01T00-00-00/.claude/settings.json")" +latest="$(readlink "${AGENT_BACKUP_DIR}/latest")" +[[ "${actual}" = OLD ]] +[[ "${first}" = NEW ]] +[[ "${latest}" = "2026-01-01T00-00-00-0000000001" ]] +[[ -e "${AGENT_BACKUP_DIR}/latest" ]] + +# A later unique run identical to the previous snapshot removes only its own +# newly reserved directory and leaves latest resolving to a real snapshot. +export AGENT_BACKUP_TEST_STAMP="2026-01-01T00-00-01" +bash "${SCRIPT_DIR}/backup.sh" >/dev/null 2>&1 +[[ ! -e "${AGENT_BACKUP_DIR}/2026-01-01T00-00-01" ]] +latest="$(readlink "${AGENT_BACKUP_DIR}/latest")" +[[ "${latest}" = "2026-01-01T00-00-00-0000000001" ]] +[[ -e "${AGENT_BACKUP_DIR}/latest" ]] + +echo "agent-backup same-second collision regression: PASS"