Skip to content

The agent that grows with you

fix(doctor): detect stale FTS layout structurally

Yuan Li9cd1ea2711a3
diff --git a/hermes_cli/doctor_state.py b/hermes_cli/doctor_state.py
index 55a21d34061..5d8000888cb 100644
--- a/hermes_cli/doctor_state.py
+++ b/hermes_cli/doctor_state.py
@@ -99,7 +99,8 @@ def _render_state_db_stats(stats: dict, holders=None, host_note: str = "") -> li
detail = "consider enabling sessions.auto_prune in config.yaml to bound growth"
stale_trigram = (fts is not None and fts.get("messages_fts_trigram")
and (stats.get("fts_storage_version") or 0) < FTS_STORAGE_VERSION)
- if stats.get("fts_rebuild_pending") or stale_trigram:
+ structural_fts_upgrade = bool(stats.get("fts_storage_upgrade_needed"))
+ if stats.get("fts_rebuild_pending") or stale_trigram or structural_fts_upgrade:
detail += "; run 'hermes sessions optimize-storage' offline (with the host gateway stopped) to compact FTS storage"
lines.append(("warn", f"state.db is large ({_human_bytes(logical)})", f"({detail})"))
# WAL runaway is deliberately NOT warned here: _state_db_wal already warns above 50 MB and offers --fix.
diff --git a/hermes_state_dbfile.py b/hermes_state_dbfile.py
index 5d19aba9bee..483683ffb85 100644
--- a/hermes_state_dbfile.py
+++ b/hermes_state_dbfile.py
@@ -752,7 +752,7 @@ def collect_state_db_stats(db_path: Path) -> dict[str, Any]:
from hermes_state import _connect_tracked_db
stats: dict[str, Any] = dict.fromkeys((
"page_count", "page_size", "freelist_count", "logical_size_bytes", "wal_size_bytes", "journal_mode",
- "messages", "sessions", "fts_tables", "fts_storage_version", "fts_rebuild_pending",
+ "messages", "sessions", "fts_tables", "fts_storage_version", "fts_storage_upgrade_needed", "fts_rebuild_pending",
"fts_rebuild_high_water", "fts_rebuild_progress", "fts_rebuild_deferral"))
# WAL sidecar size needs no connection at all.
with contextlib.suppress(OSError):
@@ -800,6 +800,9 @@ def collect_state_db_stats(db_path: Path) -> dict[str, Any]:
stats["fts_tables"] = {t: (t in names) for t in _FTS_TABLE_NAMES}
# Raw state_meta reads — cheap, and independent of SessionDB.
stats["fts_storage_version"] = _meta_int("fts_storage_version")
+ with contextlib.suppress(Exception):
+ from hermes_state_fts import SessionFtsSetupMixin
+ stats["fts_storage_upgrade_needed"] = SessionFtsSetupMixin._db_needs_fts_storage_upgrade(conn.cursor())
stats["fts_rebuild_high_water"] = high_water = _meta_int("fts_rebuild_high_water")
stats["fts_rebuild_progress"] = progress = _meta_int("fts_rebuild_progress")
stats["fts_rebuild_pending"] = False if high_water is None else (progress or 0) < high_water
diff --git a/tests/hermes_cli/test_fts_optimize_notice.py b/tests/hermes_cli/test_fts_optimize_notice.py
index 8ba415c0a30..4f64554a2b2 100644
--- a/tests/hermes_cli/test_fts_optimize_notice.py
+++ b/tests/hermes_cli/test_fts_optimize_notice.py
@@ -50,3 +50,16 @@ def test_update_notice_offers_v1_trigram_tool_calls_rebuild(tmp_path, monkeypatc
assert "hermes sessions optimize-storage" in capsys.readouterr().out
conn.close()
+
+
+def test_doctor_notice_uses_structural_fts_upgrade_status():
+ from hermes_cli.doctor_state import _render_state_db_stats
+
+ rows = _render_state_db_stats({
+ "logical_size_bytes": 2 * 1024 ** 3,
+ "fts_tables": {"messages_fts_trigram": True},
+ "fts_storage_version": 3,
+ "fts_storage_upgrade_needed": True,
+ })
+
+ assert "hermes sessions optimize-storage" in rows[-1][2]