Skip to content

The agent that grows with you

fix(cli): reject negative full-backup retention before pruning

Rudy Celekli447b9ee7d67e
diff --git a/hermes_cli/backup.py b/hermes_cli/backup.py
index 66ad325be78..99c120df5e4 100644
--- a/hermes_cli/backup.py
+++ b/hermes_cli/backup.py
@@ -591,6 +591,8 @@ def run_backup(args) -> bool:
the caller turns False into exit status 1 so a cron/systemd timer never publishes a "successful"
archive that is missing state.db. Hard failures keep raising ``SystemExit``.
"""
+ if getattr(args, "keep", 0) < 0:
+ raise ValueError("--keep must be non-negative (0 keeps everything)")
hermes_root = get_default_hermes_root()
if not hermes_root.is_dir():
diff --git a/hermes_cli/subcommands/backup.py b/hermes_cli/subcommands/backup.py
index 282b3773f8d..402c5343f2d 100644
--- a/hermes_cli/subcommands/backup.py
+++ b/hermes_cli/subcommands/backup.py
@@ -2,9 +2,17 @@
from __future__ import annotations
+from argparse import ArgumentTypeError
from typing import Callable
+def _non_negative_keep(value: str) -> int:
+ keep = int(value)
+ if keep < 0:
+ raise ArgumentTypeError("--keep must be non-negative (0 keeps everything)")
+ return keep
+
+
def build_backup_parser(subparsers, *, cmd_backup: Callable) -> None:
"""Attach the ``backup`` subcommand to ``subparsers``."""
backup_parser = subparsers.add_parser(
@@ -21,7 +29,7 @@ def build_backup_parser(subparsers, *, cmd_backup: Callable) -> None:
backup_parser.add_argument(
"-l", "--label", help="Label for the snapshot (only used with --quick)")
backup_parser.add_argument(
- "-k", "--keep", type=int, default=3, metavar="N",
+ "-k", "--keep", type=_non_negative_keep, default=3, metavar="N",
help="After a full backup, delete older hermes-backup-*.zip files in the output "
"directory beyond the newest N (default 3; 0 keeps everything)")
backup_parser.set_defaults(func=cmd_backup)
diff --git a/tests/hermes_cli/test_backup_retention_validation.py b/tests/hermes_cli/test_backup_retention_validation.py
new file mode 100644
index 00000000000..f5ee0c50156
--- /dev/null
+++ b/tests/hermes_cli/test_backup_retention_validation.py
@@ -0,0 +1,42 @@
+"""Invalid full-backup retention must never delete a recovery archive."""
+
+import argparse
+import zipfile
+
+import pytest
+
+
+def test_full_backup_refuses_negative_retention_before_touching_archives(tmp_path, monkeypatch):
+ from hermes_cli.backup import run_backup
+
+ home = tmp_path / "home"
+ home.mkdir()
+ (home / "config.yaml").write_text("memory:\n provider: null\n", encoding="utf-8")
+ monkeypatch.setenv("HERMES_HOME", str(home))
+ monkeypatch.setenv("HERMES_MANAGED_DIR", str(tmp_path / "managed"))
+ output = tmp_path / "exports"
+ output.mkdir()
+ archive = output / "hermes-backup-2000-01-01-000000.zip"
+ with zipfile.ZipFile(archive, "w") as zf:
+ zf.writestr("config.yaml", "previous recovery settings")
+ original = archive.read_bytes()
+
+ with pytest.raises(ValueError, match="keep"):
+ run_backup(argparse.Namespace(output=str(output), keep=-1))
+
+ assert list(output.iterdir()) == [archive]
+ assert archive.read_bytes() == original
+
+
+def test_backup_parser_rejects_negative_retention_and_preserves_supported_values():
+ from hermes_cli.subcommands.backup import build_backup_parser
+
+ parser = argparse.ArgumentParser()
+ build_backup_parser(parser.add_subparsers(dest="command"), cmd_backup=lambda args: None)
+
+ with pytest.raises(SystemExit) as exc:
+ parser.parse_args(["backup", "--keep", "-1"])
+ assert exc.value.code == 2
+ assert parser.parse_args(["backup", "--keep", "0"]).keep == 0
+ assert parser.parse_args(["backup", "--keep", "1"]).keep == 1
+ assert parser.parse_args(["backup"]).keep == 3