Skip to content

The agent that grows with you

fix(backup): an incomplete archive never prunes the last complete ones

kshitijk4poor1250a3e4eb66
diff --git a/hermes_cli/backup.py b/hermes_cli/backup.py
index a5918f18a7d..b948f573587 100644
--- a/hermes_cli/backup.py
+++ b/hermes_cli/backup.py
@@ -742,12 +742,13 @@ def _run_backup_locked(args, hermes_root: Path) -> bool:
if skipped_dirs:
print("\n Excluded directories:\n" + "\n".join(f" {d}/" for d in sorted(skipped_dirs)))
if errors:
- _print_capped(f"\n Archive kept, but {len(errors)} file(s) could not be added (exit status 1):",
- errors, " ")
+ _print_capped(f"\n Archive kept, but {len(errors)} file(s) could not be added:", errors, " ")
else:
print(f"\nRestore with: hermes import {out_path.name}")
+ # Prune only after a complete archive: a timer hitting the same unreadable file every run must
+ # not rotate the last good backups out in favour of incomplete ones.
keep = getattr(args, "keep", 0) # 0 / absent: never prune (non-CLI callers)
- if keep and out_path.name.startswith(_RUN_BACKUP_PREFIX):
+ if keep and not errors and out_path.name.startswith(_RUN_BACKUP_PREFIX):
pruned = _prune_prefixed_zips(out_path.parent, _RUN_BACKUP_PREFIX, keep, "backup")
if pruned:
print(f" Pruned {pruned} older {_RUN_BACKUP_PREFIX}*.zip (keeping {keep}).")
diff --git a/hermes_cli/main.py b/hermes_cli/main.py
index 3eee4d8c5b5..581491f0ed9 100644
--- a/hermes_cli/main.py
+++ b/hermes_cli/main.py
@@ -2226,7 +2226,7 @@ def cmd_backup(args):
if getattr(args, "quick", False):
backup.run_quick_backup(args)
- elif backup.run_backup(args) is False:
+ elif not backup.run_backup(args):
raise SystemExit(1) # archive written but incomplete: never shell-success for a timer
diff --git a/tests/hermes_cli/test_backup.py b/tests/hermes_cli/test_backup.py
index b22f68a5f63..fe31dfac7e4 100644
--- a/tests/hermes_cli/test_backup.py
+++ b/tests/hermes_cli/test_backup.py
@@ -749,13 +749,18 @@ class TestBackupEdgeCases:
pytest.skip("running as root: chmod 0 does not make the file unreadable")
monkeypatch.setenv("HERMES_HOME", str(hermes_home))
monkeypatch.setattr(Path, "home", lambda: tmp_path)
- out_zip = tmp_path / "out.zip"
-
- from hermes_cli.backup import run_backup
+ from hermes_cli.backup import _RUN_BACKUP_PREFIX, run_backup
from hermes_cli.main import cmd_backup
- assert run_backup(Namespace(output=str(out_zip))) is False
+ out_dir = tmp_path / "b"
+ out_dir.mkdir()
+ good_old = out_dir / f"{_RUN_BACKUP_PREFIX}old.zip"
+ good_old.write_bytes(b"PK")
+ out_zip = out_dir / f"{_RUN_BACKUP_PREFIX}new.zip"
+
+ assert run_backup(Namespace(output=str(out_zip), keep=1)) is False
assert out_zip.exists()
+ assert good_old.exists(), "an incomplete run must not rotate the last complete backup out"
assert "Backup incomplete" in capsys.readouterr().out
with pytest.raises(SystemExit) as exc:
cmd_backup(Namespace(output=str(tmp_path / "out2.zip"), quick=False))
diff --git a/website/docs/reference/cli-commands.md b/website/docs/reference/cli-commands.md
index 992de4694cc..f9a9089cf81 100644
--- a/website/docs/reference/cli-commands.md
+++ b/website/docs/reference/cli-commands.md
@@ -1039,7 +1039,7 @@ Create a zip archive of your Hermes configuration, skills, sessions, and data. T
The backup uses SQLite's `backup()` API for safe copying, so it works correctly even when Hermes is running (WAL-mode safe).
-**Exit status:** `0` only when every selected file landed in the archive. If some files could not be added (`Backup incomplete: …`), the zip is kept so the rest can still be restored, but the command exits `1` — a cron or systemd timer will not report a partial archive as success. `2` means another backup was already running.
+**Exit status:** `0` only when every selected file landed in the archive. If some files could not be added (`Backup incomplete: …`), the zip is kept so the rest can still be restored, but the command exits `1` — a cron or systemd timer will not report a partial archive as success, and `--keep` pruning is skipped so older complete archives survive. `2` means another backup was already running.
**What's excluded from the zip:**