Skip to content

The agent that grows with you

fix(backup): stop zipping managed runtime trees; unify the two backup walks

emozilladee40c0420db
diff --git a/hermes_cli/backup.py b/hermes_cli/backup.py
index c9cee26ffc0..41a8171c73a 100644
--- a/hermes_cli/backup.py
+++ b/hermes_cli/backup.py
@@ -96,6 +96,34 @@ _EXCLUDED_DIRS = {
".ruff_cache",
}
+# Hermes-managed runtime downloads that only exist at the top of a profile
+# home: local GGUF models, llama.cpp runtime binaries, and the managed Node
+# installation. All of them are re-downloaded on demand (model catalog,
+# runtime bootstrap, node installer) and routinely reach tens to hundreds of
+# GB, so zipping them turns a backup into an hours-long compress of
+# incompressible weights (the "backup stuck at N files" symptom). Matched
+# ONLY at the root of HERMES_HOME and at ``profiles/<name>/`` — a deeper
+# directory that happens to share one of these names (a skill's ``models/``,
+# a user checkout) is user data and stays in the backup.
+_EXCLUDED_ROOT_DIRS = {
+ "models",
+ "runtimes",
+ "node",
+}
+
+
+def _in_excluded_root_dir(rel_path: Path) -> bool:
+ """True when *rel_path* (relative to HERMES_HOME) is, or sits inside, a
+ Hermes-managed runtime tree at the top of a profile home."""
+ parts = rel_path.parts
+ if not parts:
+ return False
+ if parts[0] in _EXCLUDED_ROOT_DIRS:
+ return True
+ # Named profiles are profile homes too: profiles/<name>/models etc.
+ return len(parts) >= 3 and parts[0] == "profiles" and parts[2] in _EXCLUDED_ROOT_DIRS
+
+
# File-name suffixes to skip
_EXCLUDED_SUFFIXES = (
".pyc",
@@ -117,6 +145,16 @@ _EXCLUDED_NAMES = {
"cron.pid",
}
+# File-name prefixes to skip. The desktop updater's pre-flight drops
+# ``state.db.pre-update-emergency-<timestamp>.bak`` at the HERMES_HOME root
+# (apps/desktop/electron/main.ts preflightStateDb) — a backup artifact in
+# the same class as ``backups/`` and ``state-snapshots/``, so a full backup
+# must not re-ship it. Matched by prefix because the name carries a
+# timestamp; a plain ``.bak`` suffix rule would drop user files.
+_EXCLUDED_PREFIXES = (
+ "state.db.pre-update-emergency-",
+)
+
# File names that ``hermes import`` must never overwrite, matched by basename so
# they're caught for the root profile (``gateway_state.json``) and for named
# profiles alike (``profiles/<name>/gateway_state.json``).
@@ -324,6 +362,9 @@ def _should_exclude(rel_path: Path) -> bool:
"""Return True if *rel_path* (relative to hermes root) should be skipped."""
parts = rel_path.parts
+ if _in_excluded_root_dir(rel_path):
+ return True
+
for part in parts:
if part not in _EXCLUDED_DIRS:
continue
@@ -339,6 +380,9 @@ def _should_exclude(rel_path: Path) -> bool:
if name in _EXCLUDED_NAMES:
return True
+ if name.startswith(_EXCLUDED_PREFIXES):
+ return True
+
if name.endswith(_EXCLUDED_SUFFIXES):
return True
@@ -361,6 +405,48 @@ def _should_skip_backup_file(abs_path: Path, rel_path: Path, out_path: Path) ->
return False
+def _iter_backup_files(
+ hermes_root: Path,
+ out_path: Path,
+ skipped_dirs: Optional[set] = None,
+):
+ """Yield ``(abs_path, rel_path)`` for every file a full backup should hold.
+
+ The one owner of the backup walk policy: directory pruning (so os.walk
+ never descends a multi-GB excluded tree), the root-only ``hermes-agent``
+ carve-out, profile-home-root runtime trees, and the per-file exclusion
+ rules — shared by the manual ``hermes backup`` path and the automatic
+ pre-update/pre-migration path so the two can never drift.
+
+ ``skipped_dirs``, when given, collects pruned directories (root-relative,
+ as strings) for the end-of-run summary.
+ """
+ for dirpath, dirnames, filenames in os.walk(hermes_root, followlinks=False):
+ rel_dir = Path(dirpath).relative_to(hermes_root)
+
+ # ``hermes-agent`` is only pruned at the root level; nested dirs
+ # with the same name (e.g. in skills/) must be preserved. Managed
+ # runtime trees (models/, runtimes/, node/) are pruned only at a
+ # profile-home root — see _EXCLUDED_ROOT_DIRS.
+ is_root = rel_dir == Path(".")
+ orig_dirnames = dirnames[:]
+ dirnames[:] = [
+ d for d in dirnames
+ if (d not in _EXCLUDED_DIRS or (d == "hermes-agent" and not is_root))
+ and not _in_excluded_root_dir(rel_dir / d)
+ ]
+ if skipped_dirs is not None:
+ for removed in set(orig_dirnames) - set(dirnames):
+ skipped_dirs.add(str(rel_dir / removed))
+
+ for fname in filenames:
+ rel = rel_dir / fname
+ fpath = hermes_root / rel
+ if _should_skip_backup_file(fpath, rel, out_path):
+ continue
+ yield fpath, rel
+
+
# ---------------------------------------------------------------------------
# SQLite safe copy
# ---------------------------------------------------------------------------
@@ -688,33 +774,10 @@ def _run_backup_locked(args, hermes_root: Path) -> None:
scan_started = time.monotonic()
logger.info("backup phase=scan status=started")
print(f"Scanning {display_hermes_home()} ...")
- files_to_add: list[tuple[Path, Path]] = [] # (absolute, relative)
- skipped_dirs = set()
-
- for dirpath, dirnames, filenames in os.walk(hermes_root, followlinks=False):
- dp = Path(dirpath)
- rel_dir = dp.relative_to(hermes_root)
-
- # Prune excluded directories in-place so os.walk doesn't descend
- # ``hermes-agent`` is only pruned at the root level; nested dirs
- # with the same name (e.g. in skills/) must be preserved.
- is_root = rel_dir == Path(".")
- orig_dirnames = dirnames[:]
- dirnames[:] = [
- d for d in dirnames
- if d not in _EXCLUDED_DIRS or (d == "hermes-agent" and not is_root)
- ]
- for removed in set(orig_dirnames) - set(dirnames):
- skipped_dirs.add(str(rel_dir / removed))
-
- for fname in filenames:
- fpath = dp / fname
- rel = fpath.relative_to(hermes_root)
-
- if _should_skip_backup_file(fpath, rel, out_path):
- continue
-
- files_to_add.append((fpath, rel))
+ skipped_dirs: set = set()
+ files_to_add: list[tuple[Path, Path]] = list(
+ _iter_backup_files(hermes_root, out_path, skipped_dirs)
+ )
# External memory-provider state (e.g. ~/.honcho, ~/.hindsight) lives
# outside HERMES_HOME, so the walk above never sees it. Ask the active
@@ -1958,24 +2021,8 @@ def _write_full_zip_backup_locked(out_path: Path, hermes_root: Path) -> Optional
"""
scan_started = time.monotonic()
logger.info("automatic backup phase=scan status=started")
- files_to_add: list[tuple[Path, Path]] = []
try:
- for dirpath, dirnames, filenames in os.walk(hermes_root, followlinks=False):
- dp = Path(dirpath)
- # Prune excluded directories in-place so os.walk doesn't descend
- dirnames[:] = [d for d in dirnames if d not in _EXCLUDED_DIRS]
-
- for fname in filenames:
- fpath = dp / fname
- try:
- rel = fpath.relative_to(hermes_root)
- except ValueError:
- continue
-
- if _should_skip_backup_file(fpath, rel, out_path):
- continue
-
- files_to_add.append((fpath, rel))
+ files_to_add = list(_iter_backup_files(hermes_root, out_path))
except OSError as exc:
logger.warning("Full-zip backup: walk failed: %s", exc)
return None
diff --git a/tests/hermes_cli/test_backup.py b/tests/hermes_cli/test_backup.py
index 6451b5080b4..d218e8c424e 100644
--- a/tests/hermes_cli/test_backup.py
+++ b/tests/hermes_cli/test_backup.py
@@ -158,6 +158,94 @@ class TestShouldExclude:
# The .db itself is still included (and safe-copied separately)
assert not _should_exclude(Path("state.db"))
+ def test_excludes_managed_runtime_trees_at_root(self):
+ """models/, runtimes/, and node/ at a profile-home root hold
+ re-downloadable GGUF weights and runtime binaries that reach
+ hundreds of GB — zipping them is the 20-minute-hang symptom."""
+ from hermes_cli.backup import _should_exclude
+ assert _should_exclude(Path("models/Qwen3.6-27B-Q4_K_M.gguf"))
+ assert _should_exclude(Path("models/assets/mmproj.gguf"))
+ assert _should_exclude(Path("runtimes/llamacpp/b10362/cuda/ggml-cuda.dll"))
+ assert _should_exclude(Path("node/node.exe"))
+ # Named profiles download their own copies.
+ assert _should_exclude(Path("profiles/clean/models/big.gguf"))
+ assert _should_exclude(Path("profiles/clean/runtimes/llamacpp/x.dll"))
+
+ def test_keeps_nested_dirs_named_like_runtime_trees(self):
+ """A deeper directory that happens to be called models/ or node/ is
+ user data (a skill's assets, project files) and must survive."""
+ from hermes_cli.backup import _should_exclude
+ assert not _should_exclude(Path("skills/mlops/models/notes.md"))
+ assert not _should_exclude(Path("scratch/node/index.js"))
+ assert not _should_exclude(Path("profiles/clean/skills/x/models/a.txt"))
+
+ def test_excludes_desktop_emergency_state_db_baks(self):
+ """The desktop updater's pre-flight drops timestamped
+ state.db.pre-update-emergency-*.bak files at the HERMES_HOME root —
+ backup artifacts in the same class as backups/, so a full backup
+ must not re-ship them."""
+ from hermes_cli.backup import _should_exclude
+ assert _should_exclude(
+ Path("state.db.pre-update-emergency-2026-08-15T04-55-33-619Z.bak")
+ )
+ assert _should_exclude(
+ Path("profiles/coder/state.db.pre-update-emergency-2026-08-15T04-55-33-619Z.bak")
+ )
+ # Other .bak files are user data and stay.
+ assert not _should_exclude(Path("config.yaml.bak"))
+
+
+# ---------------------------------------------------------------------------
+# _iter_backup_files tests
+# ---------------------------------------------------------------------------
+
+class TestIterBackupFiles:
+ def test_manual_and_automatic_paths_share_one_walk(self, tmp_path):
+ """Both backup entry points must select the identical file set.
+
+ Before the walks were unified, the automatic pre-update path pruned
+ ``hermes-agent`` at ANY depth, silently dropping nested skill dirs
+ like ``skills/autonomous-ai-agents/hermes-agent/`` that the manual
+ path preserved. One shared iterator makes that drift impossible;
+ this test pins the contract."""
+ from hermes_cli.backup import _iter_backup_files
+
+ root = tmp_path / ".hermes"
+ root.mkdir()
+ _make_hermes_tree(root)
+
+ # The case the old automatic walk got wrong: a nested dir named
+ # hermes-agent holding real skill content.
+ nested = root / "skills" / "autonomous-ai-agents" / "hermes-agent"
+ nested.mkdir(parents=True)
+ (nested / "SKILL.md").write_text("# nested skill\n")
+
+ # A root-level managed runtime tree that both paths must prune.
+ (root / "models").mkdir()
+ (root / "models" / "big.gguf").write_bytes(b"\x00" * 64)
+
+ out_path = tmp_path / "out.zip"
+ selected = {str(rel) for _, rel in _iter_backup_files(root, out_path)}
+
+ rel_nested = str(Path("skills/autonomous-ai-agents/hermes-agent/SKILL.md"))
+ assert rel_nested in selected
+ assert str(Path("models/big.gguf")) not in selected
+ assert not any(s.startswith("hermes-agent") for s in selected)
+
+ def test_skipped_dirs_collected_for_summary(self, tmp_path):
+ from hermes_cli.backup import _iter_backup_files
+
+ root = tmp_path / ".hermes"
+ root.mkdir()
+ _make_hermes_tree(root)
+ (root / "models").mkdir()
+ (root / "models" / "big.gguf").write_bytes(b"\x00")
+
+ skipped: set = set()
+ list(_iter_backup_files(root, tmp_path / "out.zip", skipped))
+ assert "models" in skipped
+ assert "hermes-agent" in skipped
+
# ---------------------------------------------------------------------------
# Backup tests