The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(backup): don't nest state-snapshots/ into full backups
Simonf9849c43a24d
diff --git a/hermes_cli/backup.py b/hermes_cli/backup.pyindex 30e3b6cce39..3dba945e13a 100644--- a/hermes_cli/backup.py+++ b/hermes_cli/backup.py@@ -44,6 +44,11 @@ logger = logging.getLogger(__name__)# Exclusion rules# ---------------------------------------------------------------------------+# Where ``hermes backup --quick`` / ``/snapshot`` / the pre-update safety net+# write their state snapshots (see ``create_quick_snapshot`` below). Defined up+# here because the exclusion set needs it.+_QUICK_SNAPSHOTS_DIR = "state-snapshots"+# Directory names to skip entirely (matched against each path component)# ``hermes-agent`` is special-cased to root level only in ``_should_exclude``# so that skill directories like ``skills/autonomous-ai-agents/hermes-agent/``@@ -66,6 +71,9 @@ _EXCLUDED_DIRS = {".git", # nested git dirs (profiles shouldn't have these, but safety)"node_modules", # js deps — reinstalled on demand"backups", # prior auto-backups — don't nest backups exponentially+ _QUICK_SNAPSHOTS_DIR, # quick/pre-update state snapshots — same reason as+ # ``backups``: each holds a full copy of state.db, so+ # zipping them re-ships the DB once per snapshot"checkpoints", # session-local trajectory caches — regenerated per-session,# session-hash-keyed so they don't port to another machine anyway# Python dependency trees (plugin / MCP-server venvs under HERMES_HOME) —@@ -1309,7 +1317,7 @@ _QUICK_STATE_FILES = ("feishu_comment_pairing.json", # Feishu comment subscription pairings)-_QUICK_SNAPSHOTS_DIR = "state-snapshots"+# ``_QUICK_SNAPSHOTS_DIR`` lives with the exclusion rules at the top of the module._QUICK_DEFAULT_KEEP = 20diff --git a/tests/hermes_cli/test_backup.py b/tests/hermes_cli/test_backup.pyindex 9b12eca4673..ecef05fbef7 100644--- a/tests/hermes_cli/test_backup.py+++ b/tests/hermes_cli/test_backup.py@@ -133,6 +133,19 @@ class TestShouldExclude:from hermes_cli.backup import _should_excludeassert _should_exclude(Path("backups/pre-update-2026-04-27-063400.zip"))+ def test_excludes_state_snapshots_dir(self):+ """state-snapshots/ is excluded for the same reason as backups/: every+ quick / pre-update snapshot holds its own copy of state.db, so zipping+ the tree would ship the DB once per retained snapshot."""+ from hermes_cli.backup import _EXCLUDED_DIRS, _QUICK_SNAPSHOTS_DIR, _should_exclude+ assert _QUICK_SNAPSHOTS_DIR in _EXCLUDED_DIRS+ assert _should_exclude(Path(_QUICK_SNAPSHOTS_DIR) / "20260814-203829-2026-08-15" / "state.db")+ assert _should_exclude(Path(_QUICK_SNAPSHOTS_DIR) / "20260814-203829-2026-08-15" / "manifest.json")+ # Named profiles accumulate snapshots too.+ assert _should_exclude(Path("profiles/coder") / _QUICK_SNAPSHOTS_DIR / "x" / "state.db")+ # The live DB is still backed up.+ assert not _should_exclude(Path("state.db"))+def test_excludes_sqlite_sidecars(self):"""SQLite WAL/SHM/journal sidecars must not ship alongside thesafe-copied .db — pairing a fresh snapshot with stale sidecar state@@ -241,6 +254,35 @@ class TestBackup:assert "skills/outside-link.txt" not in namesassert all(zf.read(name) != b"outside secret\n" for name in names)+ def test_state_snapshots_not_nested_into_backup(self, tmp_path, monkeypatch):+ """A quick snapshot left under state-snapshots/ must not be re-shipped+ by the full backup — each snapshot already holds a copy of state.db, so+ nesting them multiplies the archive by (1 + retained snapshots)."""+ hermes_home = tmp_path / ".hermes"+ hermes_home.mkdir()+ _make_hermes_tree(hermes_home)+ with sqlite3.connect(hermes_home / "state.db") as conn:+ conn.execute("CREATE TABLE sessions (id TEXT PRIMARY KEY)")+ conn.execute("INSERT INTO sessions VALUES ('s1')")++ from hermes_cli.backup import _QUICK_SNAPSHOTS_DIR, create_quick_snapshot, run_backup++ # Real producer, so the layout under state-snapshots/ is whatever the+ # code actually writes (manifest.json + state.db copy + ...).+ snap_id = create_quick_snapshot(hermes_home=hermes_home)+ assert snap_id and (hermes_home / _QUICK_SNAPSHOTS_DIR / snap_id / "state.db").exists()++ monkeypatch.setenv("HERMES_HOME", str(hermes_home))+ monkeypatch.setattr(Path, "home", lambda: tmp_path)+ out_zip = tmp_path / "backup.zip"+ run_backup(Namespace(output=str(out_zip)))++ with zipfile.ZipFile(out_zip, "r") as zf:+ names = zf.namelist()+ assert not any(n.startswith(_QUICK_SNAPSHOTS_DIR + "/") for n in names), names+ # Exactly one state.db in the archive: the live one.+ assert [n for n in names if n == "state.db" or n.endswith("/state.db")] == ["state.db"]+# ---------------------------------------------------------------------------# _validate_backup_zip tests@@ -1410,6 +1452,28 @@ class TestPreUpdateBackup:# pid files excludedassert "gateway.pid" not in names+ def test_pre_update_zip_does_not_nest_the_pre_update_snapshot(self, hermes_home):+ """``hermes update`` in ``full`` mode takes the quick snapshot *before*+ the full zip, so the zip walk sees the snapshot it just made. It must+ skip it — otherwise every pre-update zip ships state.db twice."""+ from hermes_cli.backup import (+ _QUICK_SNAPSHOTS_DIR,+ create_pre_update_backup,+ create_quick_snapshot,+ )+ with sqlite3.connect(hermes_home / "state.db") as conn:+ conn.execute("CREATE TABLE sessions (id TEXT PRIMARY KEY)")++ snap_id = create_quick_snapshot(label="pre-update", hermes_home=hermes_home)+ assert snap_id and (hermes_home / _QUICK_SNAPSHOTS_DIR / snap_id / "state.db").exists()++ out = create_pre_update_backup(hermes_home=hermes_home)+ assert out is not None+ with zipfile.ZipFile(out) as zf:+ names = zf.namelist()+ assert "state.db" in names+ assert not any(n.startswith(_QUICK_SNAPSHOTS_DIR + "/") for n in names), names+def test_rotation_keeps_only_n(self, hermes_home):"""After more than ``keep`` backups are created, older ones arediff --git a/website/docs/reference/cli-commands.md b/website/docs/reference/cli-commands.mdindex 37871c0eea7..a75f2a6e917 100644--- a/website/docs/reference/cli-commands.md+++ b/website/docs/reference/cli-commands.md@@ -930,7 +930,7 @@ hermes debug share --local # Print report to terminal (no upload)hermes backup [options]```-Create a zip archive of your Hermes configuration, skills, sessions, and data. The backup excludes the hermes-agent codebase itself.+Create a zip archive of your Hermes configuration, skills, sessions, and data. The backup excludes the hermes-agent codebase itself, and it does not nest earlier backup artifacts (`backups/`, `state-snapshots/`) — each of those already contains its own copy of `state.db`.| Option | Description ||--------|-------------|