Files
hermes-agent/tests/test_session_vacuum_config.py
Teknium 73f68362b3 fix(sessions): auto-prune state.db by default (90d) and gate VACUUM on freelist ratio (#54189)
Flip the state.db retention defaults per Teknium's decision on #54189:

- sessions.auto_prune: false -> true. A stock install now prunes ENDED
  sessions inactive for retention_days at CLI/gateway/cron startup
  (at most once per min_interval_hours). Open, pinned and mid-turn
  sessions are never deleted; the only open rows touched are stale
  automation sessions (#100903 sweep), which are closed, not deleted,
  and aged a further full window before removal.
- sessions.retention_days stays 90 (already the default; verified).
- Auto-VACUUM is now additionally gated on the reclaimable fraction of
  the file: PRAGMA freelist_count / page_count must exceed 25%
  (AUTO_VACUUM_MIN_FREELIST_RATIO) on top of the existing
  min_vacuum_interval_days throttle. Pruning a few small sessions on a
  dense multi-GB DB no longer rewrites the whole file to reclaim a few MB.
  Unknown ratio (pragma read failure) falls back to the time throttle.

Existing installs that explicitly set any sessions.* key keep their
values (load_config deep-merges DEFAULT_CONFIG under user YAML); only
unset keys pick up the new defaults. No _config_version bump needed.
cli-config.yaml.example documents the section commented-out so
installers that copy it verbatim never pin these as explicit settings.

Tests: ratio gate (below/above/at-threshold/unknown/override), real-DB
freelist ratio, default assertions, fresh-config startup hook reaches
the prune call, explicit opt-out respected, template-does-not-pin-keys.
2026-09-02 07:26:52 -07:00

123 lines
4.1 KiB
Python

from pathlib import Path
from unittest.mock import MagicMock
def test_default_config_exposes_vacuum_interval():
from hermes_cli.config import DEFAULT_CONFIG
assert DEFAULT_CONFIG["sessions"]["min_vacuum_interval_days"] == 30
def test_default_config_auto_prune_on_with_90_day_retention():
"""#54189: state.db retention is ON by default (ended sessions, 90 days)."""
from hermes_cli.config import DEFAULT_CONFIG
sessions = DEFAULT_CONFIG["sessions"]
assert sessions["auto_prune"] is True
assert sessions["retention_days"] == 90
assert sessions["vacuum_after_prune"] is True
def test_fresh_config_runs_auto_prune_at_startup(monkeypatch, tmp_path: Path):
"""A config.yaml with NO ``sessions:`` keys must reach the prune call with the
new defaults (the loader deep-merges DEFAULT_CONFIG)."""
import cli
import hermes_cli.config
import hermes_constants
from hermes_cli.config import DEFAULT_CONFIG
session_db = MagicMock()
session_db.get_meta.return_value = "already-done"
# Simulate load_config() on a fresh home: only defaults for the section.
monkeypatch.setattr(
hermes_cli.config,
"load_config",
lambda: {"sessions": dict(DEFAULT_CONFIG["sessions"])},
)
monkeypatch.setattr(hermes_constants, "get_hermes_home", lambda: tmp_path)
cli._run_state_db_auto_maintenance(session_db)
session_db.maybe_auto_prune_and_vacuum.assert_called_once_with(
retention_days=90,
min_interval_hours=24,
min_vacuum_interval_days=30,
vacuum=True,
sessions_dir=tmp_path / "sessions",
)
def test_explicit_auto_prune_false_is_respected(monkeypatch, tmp_path: Path):
"""Migration guard: an install that explicitly opted out keeps its choice."""
import cli
import hermes_cli.config
import hermes_constants
session_db = MagicMock()
session_db.get_meta.return_value = "already-done"
monkeypatch.setattr(
hermes_cli.config,
"load_config",
lambda: {"sessions": {"auto_prune": False, "retention_days": 90}},
)
monkeypatch.setattr(hermes_constants, "get_hermes_home", lambda: tmp_path)
cli._run_state_db_auto_maintenance(session_db)
session_db.maybe_auto_prune_and_vacuum.assert_not_called()
def test_shipped_template_does_not_pin_sessions_keys():
"""Installers copy cli-config.yaml.example verbatim into config.yaml, so any
uncommented ``sessions:`` value there becomes an EXPLICIT user setting that
would freeze the retention defaults. The template must leave them commented
so code defaults (and future flips) apply."""
import yaml
template = Path(__file__).resolve().parents[1] / "cli-config.yaml.example"
data = yaml.safe_load(template.read_text(encoding="utf-8")) or {}
assert "sessions" not in data
def test_loader_yields_new_defaults_for_fresh_home(monkeypatch, tmp_path: Path):
"""Real load_config() against an empty HERMES_HOME → auto_prune on, 90 days."""
monkeypatch.setenv("HERMES_HOME", str(tmp_path))
from hermes_cli.config import load_config
sessions = load_config().get("sessions") or {}
assert sessions.get("auto_prune") is True
assert sessions.get("retention_days") == 90
def test_cli_auto_maintenance_forwards_vacuum_interval(monkeypatch, tmp_path: Path):
import cli
import hermes_cli.config
import hermes_constants
session_db = MagicMock()
session_db.get_meta.return_value = "already-done"
monkeypatch.setattr(
hermes_cli.config,
"load_config",
lambda: {
"sessions": {
"auto_prune": True,
"retention_days": 90,
"vacuum_after_prune": True,
"min_interval_hours": 24,
"min_vacuum_interval_days": 17,
}
},
)
monkeypatch.setattr(hermes_constants, "get_hermes_home", lambda: tmp_path)
cli._run_state_db_auto_maintenance(session_db)
session_db.maybe_auto_prune_and_vacuum.assert_called_once_with(
retention_days=90,
min_interval_hours=24,
min_vacuum_interval_days=17,
vacuum=True,
sessions_dir=tmp_path / "sessions",
)