Skip to content

The agent that grows with you

[verified] fix(state): throttle repeated VACUUM rewrites

Hermes agentbd856a02f269
diff --git a/hermes_state.py b/hermes_state.py
index 6f72b1eab07..71a77587fb6 100644
--- a/hermes_state.py
+++ b/hermes_state.py
@@ -8563,12 +8563,16 @@ class SessionDB(SessionSearchMixin, SessionSchemaMixin, SessionPortabilityMixin)
min_interval_hours: int = 24,
vacuum: bool = True,
sessions_dir: Optional[Path] = None,
+ min_vacuum_interval_days: int = 30,
) -> Dict[str, Any]:
"""Idempotent auto-maintenance: prune inactive sessions + optional VACUUM.
Records the last run timestamp in state_meta so subsequent calls
- within ``min_interval_hours`` no-op. Designed to be called once at
- startup from long-lived entrypoints (CLI, gateway, cron scheduler).
+ within ``min_interval_hours`` no-op. VACUUM has its own, typically
+ longer, throttle controlled by ``min_vacuum_interval_days`` so routine
+ pruning does not repeatedly rewrite the database. Designed to be
+ called once at startup from long-lived entrypoints (CLI, gateway, cron
+ scheduler).
When *sessions_dir* is provided, on-disk transcript files
(``.json`` / ``.jsonl`` / ``request_dump_*``) for pruned sessions
@@ -8603,12 +8607,25 @@ class SessionDB(SessionSearchMixin, SessionSchemaMixin, SessionPortabilityMixin)
)
result["pruned"] = pruned
- # Only VACUUM if we actually freed rows — VACUUM on a tight DB
- # is wasted I/O. Threshold keeps small DBs from paying the cost.
- if vacuum and pruned > 0:
+ # Only VACUUM if we actually freed rows, and no more often than
+ # once every min_vacuum_interval_days -- a large prune (e.g. the
+ # first one to cross retention_days on a DB with tens of
+ # thousands of rows) can free enough pages that pruned > 0 fires
+ # on every subsequent startup even though a VACUUM already ran
+ # recently. VACUUM on this DB's size (FTS5 shadow tables) is not
+ # cheap -- it holds an exclusive lock for the full rewrite.
+ last_vacuum_raw = self.get_meta("last_vacuum")
+ vacuum_due = True
+ if last_vacuum_raw:
+ try:
+ vacuum_due = (now - float(last_vacuum_raw)) >= min_vacuum_interval_days * 86400
+ except (TypeError, ValueError):
+ vacuum_due = True
+ if vacuum and pruned > 0 and vacuum_due:
try:
self.vacuum()
result["vacuumed"] = True
+ self.set_meta("last_vacuum", str(now))
except Exception as exc:
logger.warning("state.db VACUUM failed: %s", exc)
diff --git a/tests/test_hermes_state.py b/tests/test_hermes_state.py
index ac2ee422e5a..9e6d75ffb22 100644
--- a/tests/test_hermes_state.py
+++ b/tests/test_hermes_state.py
@@ -1848,6 +1848,45 @@ class TestVacuum:
# Should not raise, even though there's nothing significant to reclaim.
db.vacuum()
+ def test_auto_maintenance_records_successful_vacuum(self, db, monkeypatch):
+ monkeypatch.setattr(db, "prune_sessions", lambda **_kwargs: 3)
+ vacuum_calls = []
+ monkeypatch.setattr(db, "vacuum", lambda: vacuum_calls.append(True))
+
+ result = db.maybe_auto_prune_and_vacuum(min_interval_hours=0)
+
+ assert result["vacuumed"] is True
+ assert vacuum_calls == [True]
+ assert db.get_meta("last_vacuum") is not None
+
+ def test_auto_maintenance_skips_recent_vacuum(self, db, monkeypatch):
+ monkeypatch.setattr(db, "prune_sessions", lambda **_kwargs: 3)
+ db.set_meta("last_vacuum", str(time.time()))
+ vacuum_calls = []
+ monkeypatch.setattr(db, "vacuum", lambda: vacuum_calls.append(True))
+
+ result = db.maybe_auto_prune_and_vacuum(
+ min_interval_hours=0,
+ min_vacuum_interval_days=30,
+ )
+
+ assert result["vacuumed"] is False
+ assert vacuum_calls == []
+
+ def test_auto_maintenance_retries_after_vacuum_interval(self, db, monkeypatch):
+ monkeypatch.setattr(db, "prune_sessions", lambda **_kwargs: 3)
+ db.set_meta("last_vacuum", str(time.time() - 31 * 86400))
+ vacuum_calls = []
+ monkeypatch.setattr(db, "vacuum", lambda: vacuum_calls.append(True))
+
+ result = db.maybe_auto_prune_and_vacuum(
+ min_interval_hours=0,
+ min_vacuum_interval_days=30,
+ )
+
+ assert result["vacuumed"] is True
+ assert vacuum_calls == [True]
+
class TestOptimizeFts:
def test_optimize_returns_index_count(self, db):