Files
hermes-agent/hermes_cli/cli_auto_maintenance.py
alt-glitch 782e0b42c9 refactor(code-files): delete unreached sandbox gate, checkpoint helpers, py shell linter
Removed. A search for callers by name, attribute and string (Python, TypeScript,
docs, the plugin API list and the frozen old-updater list) finds no supported
path that reaches any of them:

- tools/code_execution_tool.py::SANDBOX_AVAILABLE (constant True) and its
  two `if not SANDBOX_AVAILABLE` branches; the duplicated gateway-lifecycle
  comment in execute_code
- tests: test_windows_returns_error (only flipped the constant) and the
  constant's monkeypatch in test_execute_code_lifecycle_guard_deadline
- tools/checkpoint_manager.py: the no-op `store = _store_path(base)` /
  `_ = store` pair in _migrate_legacy_store; unused imports
  clear_stale_tmp_packs, rmtree_readonly, Dict, List, Set, Tuple;
  _list_project_refs (test-only) and its one assertion
- hermes_cli/cli_auto_maintenance.py::_run_checkpoint_auto_maintenance
  (imported by cli.py, never called), its cli.py import, its orphaned
  `threading` import and its no-op test monkeypatch
- tools/file_operations.py::ShellFileOperations._run_python_snippet
- tools/file_operations_lint.py: the '.py' entry in LINTERS (the in-process
  LINTERS_INPROC['.py'] always answers first)

- test_profile_rename_checkpoints: assert the migrate-identity retry deletes the leftover old checkpoint ref (via _ref_tip), replacing the deleted _list_project_refs check.
- docs(checkpoints-and-rollback): auto-prune runs only on the gateway housekeeping tick; the CLI never runs it.
2026-11-16 15:22:48 +05:30

74 lines
3.1 KiB
Python

"""Startup auto-maintenance for the classic CLI: state-db pack/vacuum.
Split out of ``cli.py``; ``cli`` re-exports every public name and moved bodies late-bind
cli-level names through ``from cli import ...`` at call time so facade monkeypatch seams hold.
"""
from __future__ import annotations
import logging
import sqlite3
# Log-record parity with the origin module.
logger = logging.getLogger("cli")
def _run_state_db_auto_maintenance(session_db) -> None:
"""One-time repairs + auto-archive/prune/vacuum per the ``sessions:`` config. Never raises."""
if session_db is None:
return
try:
from hermes_cli.config import load_config as _load_full_config
from hermes_constants import get_hermes_home as _get_hermes_home # lazy: tests patch it
_hermes_home_maint = _get_hermes_home()
# One-time repairs, each latched in state_meta once it has run.
for meta_key, repair, done_msg, skip_msg in (
(
"ghost_session_prune_v1",
lambda: session_db.prune_empty_ghost_sessions(sessions_dir=_hermes_home_maint / "sessions"),
"Pruned %d empty TUI ghost sessions", "Ghost session prune skipped: %s",
),
(
"orphaned_compression_finalize_v1",
session_db.finalize_orphaned_compression_sessions,
"Finalized %d orphaned compression sessions", "Orphan compression finalize skipped: %s",
),
):
if session_db.get_meta(meta_key):
continue
try:
count = repair()
# Every SessionDB refusal: sqlite/IO, RuntimeStoreError (ValueError), and the
# hermes_state_errors busy/replaced family (RuntimeError).
except (sqlite3.Error, OSError, ValueError, RuntimeError) as _exc:
# Latched below regardless: a repair this store refuses (live ledger work,
# locked file) must surface once, not retry silently on every start.
logger.warning(skip_msg, _exc)
count = 0
session_db.set_meta(meta_key, "1")
if count:
logger.info(done_msg, count)
cfg = (_load_full_config().get("sessions") or {})
# Auto-archive is independent of auto_prune: run it before prune's early return.
if cfg.get("auto_archive", False):
session_db.maybe_auto_archive(
idle_days=float(cfg.get("auto_archive_days", 3)),
min_interval_hours=int(cfg.get("min_interval_hours", 24)),
)
if not cfg.get("auto_prune", False):
return
session_db.maybe_auto_prune_and_vacuum(
retention_days=int(cfg.get("retention_days", 90)),
min_interval_hours=int(cfg.get("min_interval_hours", 24)),
min_vacuum_interval_days=int(cfg.get("min_vacuum_interval_days", 30)),
vacuum=bool(cfg.get("vacuum_after_prune", True)),
sessions_dir=_hermes_home_maint / "sessions",
)
except Exception as exc:
logger.debug("state.db auto-maintenance skipped: %s", exc)