diff --git a/nixos/home/laptop-backups-watchdog.sh b/nixos/home/laptop-backups-watchdog.sh index eb74f0b37d..d1b57435c7 100644 --- a/nixos/home/laptop-backups-watchdog.sh +++ b/nixos/home/laptop-backups-watchdog.sh @@ -37,12 +37,19 @@ # repo-archive-backup is covered via its timer's last-trigger time # only: its script and restic credentials live outside this repo, so # no run record is required of it (stated in laptop-backups.nix too). +# cold-store-nightly is covered via its timer's last-trigger time +# PLUS completion evidence (f): its service's last Result must be +# success and the cold-store manifest's transfer ledger must hold a +# fresh backup-upload row. A trigger alone cannot see the 2026-11-12 +# failure shape (code#9093): the timer fired and the service started, +# but the 30-minute start timeout killed the every-DB Turso export +# mid-flight, filing almost nothing. # # Usage: laptop-backups-watchdog [max-age-hours] [jobs] # max-age-hours (default 26, a little over one daily period; the # module option services.laptop-backups.watchdog # .maxAgeHours feeds this argument). -# jobs comma-separated subset of dashboard,repo-archive,state,opencode-db +# jobs comma-separated subset of dashboard,repo-archive,state,opencode-db,cold-store # (default dashboard,repo-archive). The module passes only the jobs # whose enable flag is on: a disabled job has no # record and no timer, so requiring them would fail @@ -53,6 +60,7 @@ MAX_AGE_HOURS="${1:-26}" JOBS="${2:-dashboard,repo-archive}" want_job() { case ",$JOBS," in *",$1,"*) return 0 ;; *) return 1 ;; esac } RECORD="${LAPTOP_BACKUPS_RECORD:-$HOME/backups/dashboard/.last-run.json}" +MANIFEST="${COLD_STORE_MANIFEST:-$HOME/.config/cold-store/manifest.sqlite}" NOW_EPOCH="$(date +%s)" MAX_AGE_SEC=$((MAX_AGE_HOURS * 3600)) # A timestamp further in the future than this tolerance cannot be a @@ -108,6 +116,7 @@ want_job dashboard && timers+=(pm-dashboard-backup.timer) want_job repo-archive && timers+=(repo-archive-backup.timer) want_job state && timers+=(laptop-state-backup.timer) want_job opencode-db && timers+=(opencode-db-backup.timer) +want_job cold-store && timers+=(cold-store-nightly.timer) for timer in "${timers[@]}"; do job="${timer%.timer}" raw="$(systemctl --user show -p LastTriggerUSec --value "$timer" 2>/dev/null || true)" @@ -266,6 +275,52 @@ if want_job state; then fi fi +# --- (f) cold-store-nightly completion evidence ------------------------------ +if want_job cold-store; then + cs_result="$(systemctl --user show -p Result --value cold-store-nightly.service 2>/dev/null || true)" + if [ "$cs_result" != "success" ]; then + complain "cold-store-nightly: last run result '${cs_result:-unknown}' is not success" + fi + if [ ! -f "$MANIFEST" ]; then + complain "cold-store-nightly: no cold-store manifest at $MANIFEST" + else + cs_verdict="$( + MANIFEST="$MANIFEST" NOW_EPOCH="$NOW_EPOCH" MAX_AGE_SEC="$MAX_AGE_SEC" SKEW_TOLERANCE_SEC="$SKEW_TOLERANCE_SEC" \ + python3 - <<'PYEOF' 2>/dev/null +import os, sqlite3 +from datetime import datetime, timezone +try: + con = sqlite3.connect(f"file:{os.environ['MANIFEST']}?mode=ro", uri=True) + row = con.execute("select max(ts) from transfer_ledger where kind = 'backup-upload'").fetchone() + latest = row[0] if row else None +except Exception: + print("unreadable") + raise SystemExit +if latest is None: + print("no backup-upload rows in transfer ledger") + raise SystemExit +try: + ts = datetime.strptime(latest, "%Y-%m-%d %H:%M:%S").replace(tzinfo=timezone.utc) +except Exception: + print("unreadable") + raise SystemExit +age = int(os.environ["NOW_EPOCH"]) - int(ts.timestamp()) +if age < -int(os.environ["SKEW_TOLERANCE_SEC"]): + print(f"future last backup-upload {latest} is {-age}s ahead of now (skew tolerance {os.environ['SKEW_TOLERANCE_SEC']}s)") +elif age > int(os.environ["MAX_AGE_SEC"]): + print(f"stale last backup-upload {latest} age_hours={age // 3600}") +else: + print("ok") +PYEOF + )" + case "$cs_verdict" in + ok) ;; + unreadable | "") complain "cold-store-nightly: transfer ledger in $MANIFEST is unreadable" ;; + *) complain "cold-store-nightly: transfer ledger $cs_verdict (threshold ${MAX_AGE_HOURS}h)" ;; + esac + fi +fi + if [ "$violations" -gt 0 ]; then echo "laptop-backups-watchdog: $violations violation(s)" >&2 exit 1 diff --git a/nixos/home/laptop-backups-watchdog.test.sh b/nixos/home/laptop-backups-watchdog.test.sh index cc128fc912..3bd3db16b2 100644 --- a/nixos/home/laptop-backups-watchdog.test.sh +++ b/nixos/home/laptop-backups-watchdog.test.sh @@ -85,6 +85,24 @@ STUB } >"$BIN/rclone" chmod +x "$BIN/rclone" +set_cs_trigger() { trig_ago "$1" >"$TRIG/cold-store-nightly.timer"; } +set_cs_result() { printf '%s\n' "$1" >"$TRIG/cold-store-nightly.service"; } +write_manifest() { # $1 latest backup-upload date spec, or "none" + mkdir -p "$HOME_DIR/.config/cold-store" + HOME_DIR="$HOME_DIR" python3 - "$1" <<'PYEOF' +import os, sqlite3, subprocess, sys +path = os.path.join(os.environ["HOME_DIR"], ".config", "cold-store", "manifest.sqlite") +if os.path.exists(path): + os.remove(path) +con = sqlite3.connect(path) +con.execute("create table transfer_ledger (id integer primary key autoincrement, ts text not null default (datetime('now')), direction text, bytes integer, kind text, ref text)") +if sys.argv[1] != "none": + ts = subprocess.check_output(["date", "-u", "-d", sys.argv[1], "+%Y-%m-%d %H:%M:%S"]).decode().strip() + con.execute("insert into transfer_ledger (ts, direction, bytes, kind, ref) values (?, 'upload', 100, 'backup-upload', 'fleet-queue')", (ts,)) +con.commit() +PYEOF +} + RECORD="$TMP_ROOT/record.json" run_watchdog() { HOME="$HOME_DIR" LAPTOP_BACKUPS_RECORD="$RECORD" WATCHDOG_STUB_TRIGGERS="$TRIG" \ @@ -277,6 +295,45 @@ set_triggers "1 hour ago" "1 hour ago" run_watchdog_jobs dashboard,repo-archive expect_ok state-not-enabled +# cold-store job: fresh trigger + success result + fresh ledger -> 0 +set_cs_trigger "1 hour ago" +set_cs_result success +write_manifest "1 hour ago" +run_watchdog_jobs cold-store +expect_ok cs-fresh + +# cold-store: stale ledger (nothing filed recently) -> named failure +write_manifest "40 hours ago" +run_watchdog_jobs cold-store +expect_named_failure cs-stale-ledger cold-store-nightly + +# cold-store: failed last result, however fresh the ledger -> named +# failure (the 2026-11-12 shape: fired, started, killed mid-export) +set_cs_result failure +write_manifest "10 minutes ago" +run_watchdog_jobs cold-store +expect_named_failure cs-failed-result cold-store-nightly + +# cold-store: missing manifest -> named failure +set_cs_result success +rm -f "$HOME_DIR/.config/cold-store/manifest.sqlite" +run_watchdog_jobs cold-store +expect_named_failure cs-missing-manifest cold-store-nightly + +# cold-store: stale timer -> named failure +set_cs_trigger "40 hours ago" +write_manifest "1 hour ago" +run_watchdog_jobs cold-store +expect_named_failure cs-stale-timer cold-store-nightly + +# cold-store is not in the default job set: its violations stay +# silent for hosts/jobs that did not opt in +RECORD_OUT="$RECORD" write_record complete "1 hour ago" +set_triggers "1 hour ago" "1 hour ago" +set_cs_result failure +run_watchdog +expect_ok cs-not-in-default-jobs + if ((failures > 0)); then echo "laptop-backups-watchdog tests: $failures failure(s)" >&2 exit 1 diff --git a/nixos/home/laptop-backups.nix b/nixos/home/laptop-backups.nix index 6e37203b72..cbb16e4dbd 100644 --- a/nixos/home/laptop-backups.nix +++ b/nixos/home/laptop-backups.nix @@ -123,6 +123,7 @@ let ++ (lib.optional cfg.repoArchive.enable "repo-archive") ++ (lib.optional cfg.state.enable "state") ++ (lib.optional cfg.opencodeDb.enable "opencode-db") + ++ (lib.optional cfg.coldStoreNightly.enable "cold-store") ); watchdogWanted = cfg.watchdog.enable && watchdogJobs != ""; servicePath = lib.makeBinPath [