The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(sessions): optimize-storage counts backfill rows from an index; no VACUUM over a live writer
Teknium9a2fa5e406f9
diff --git a/hermes_cli/sessions_cmd.py b/hermes_cli/sessions_cmd.pyindex 4881375d607..152d636bd0d 100644--- a/hermes_cli/sessions_cmd.py+++ b/hermes_cli/sessions_cmd.py@@ -1055,7 +1055,7 @@ def _cmd_optimize_storage(db, args):if free_bytes < need_bytes:print("\n⚠ Not enough free disk to complete safely. Free up space, or run with --no-vacuum ""(rebuilds the index but doesn't reclaim space until a later VACUUM).")- return+ return 1if before_mb > 500:print(" This may take a while on a large database. It runs in the foreground with progress below; ""safe to Ctrl-C and re-run (it resumes).")@@ -1087,7 +1087,10 @@ def _cmd_optimize_storage(db, args):return 1print("✓ Search index optimized.")_print_size_change(db, before_mb, prefix=" ")- if result.get("vacuumed") is False:+ if result.get("vacuum_skipped") == "in_use":+ print(" (VACUUM skipped: another process is using the database. Re-run `hermes sessions optimize` "+ "with it closed to reclaim freed space.)")+ elif result.get("vacuumed") is False:print(" (VACUUM was skipped or failed — run `hermes sessions optimize` later to reclaim freed space.)")diff --git a/hermes_state_search.py b/hermes_state_search.pyindex bfc75aecf7a..e4596881040 100644--- a/hermes_state_search.py+++ b/hermes_state_search.py@@ -736,7 +736,9 @@ class SessionSearchMixin:if st is None:return Nonewith self._read_ctx() as conn:- total, done = conn.execute("SELECT COUNT(*), COALESCE(SUM(id <= ?), 0) FROM messages WHERE id <= ?",+ # ``+id`` keeps the planner off the rowid range: counting from a covering index reads+ # far fewer pages (1.97M messages, cold: 73 s on the table b-tree, 1.9 s here).+ total, done = conn.execute("SELECT COUNT(*), COALESCE(SUM(id <= ?), 0) FROM messages WHERE +id <= ?",(st["indexed"], st["total"])).fetchone()return _BackfillProgress(phase=phase, total=int(total), indexed=int(done))@@ -903,8 +905,12 @@ class SessionSearchMixin:reason, still_pending, still_trash, empty_index)return {"ok": False, "reason": reason, "vacuumed": None}- vacuum_ok = None- if vacuum:+ vacuum_ok, vacuum_skipped = None, False+ if vacuum and self._foreign_state_db_holders():+ # VACUUM holds the write lock for minutes; a live writer would time out mid-append.+ logger.info("Skipping VACUUM after FTS optimize: another process is using %s", self.db_path)+ vacuum_ok, vacuum_skipped = False, True+ elif vacuum:vacuum_ok = self._optimize_vacuum(progress_cb)refusal = self._execute_write(self._optimize_settle)if refusal is not None:@@ -913,7 +919,7 @@ class SessionSearchMixin:return {"ok": False, "reason": refusal, "vacuumed": vacuum_ok}emit({"phase": "done", "percent": 100, "indexed": 0, "total": 0})logger.info("FTS storage optimization complete (layout v%d).", FTS_STORAGE_VERSION)- return {"ok": True, "vacuumed": vacuum_ok}+ return {"ok": True, "vacuumed": vacuum_ok, **({"vacuum_skipped": "in_use"} if vacuum_skipped else {})}# ── Read views ─────────────────────────────────────────────────────────diff --git a/tests/hermes_state/test_fts_optimize_progress.py b/tests/hermes_state/test_fts_optimize_progress.pyindex 763d85c8690..6e64201af70 100644--- a/tests/hermes_state/test_fts_optimize_progress.py+++ b/tests/hermes_state/test_fts_optimize_progress.py@@ -157,8 +157,11 @@ def test_writer_that_opens_the_store_mid_run_gets_the_paced_cycle(legacy_db, monreturn morelegacy_db.fts_rebuild_step = step_then_writer_opens- assert legacy_db.optimize_fts_storage(progress_cb=events.append, vacuum=False)["ok"] is True+ result = legacy_db.optimize_fts_storage(progress_cb=events.append, vacuum=True)+ # The writer is still there at the end: no minutes-long VACUUM lock over its appends.+ assert result == {"ok": True, "vacuumed": False, "vacuum_skipped": "in_use"}chunks = [e for e in events if e["phase"] != "done"]+ assert not [e for e in chunks if e["phase"] == "vacuum"]assert chunks[0]["paced"] is False and all(e["paced"] for e in chunks[1:])# Every chunk after the writer appeared yields the write lock for at least the duty-cycle floor.teardown = [e for e in chunks if e["phase"] == "teardown"]