Skip to content

The agent that grows with you

fix(state): report finished optimize-storage work as done on an unrecognised layout

yoniebans28b98c56c493
diff --git a/hermes_state_search.py b/hermes_state_search.py
index 5b2a1ddd801..a7993f49d16 100644
--- a/hermes_state_search.py
+++ b/hermes_state_search.py
@@ -19,7 +19,7 @@ from hermes_state_common import (
MAX_FTS5_QUERY_CHARS, SCHEMA_VERSION, _FTS_CJK_TRIGGERS,
escape_like as _escape_like, fts_rebuild_admission, fts_trigram_session_sql, routed_sessions_setting,
)
-from hermes_state_fts import fts_layout_current
+from hermes_state_fts import fts_layout_current, fts_layout_drift
# Pre-split logger identity so log filtering/capture is unchanged.
logger = logging.getLogger("hermes_state")
@@ -644,9 +644,13 @@ class SessionSearchMixin:
return "teardown_incomplete"
if self._fts_external_index_empty_with_messages(conn):
return "backfill_incomplete"
- if not fts_layout_current(conn):
+ current = fts_layout_current(conn)
+ if not current and fts_layout_drift(conn):
return "layout_drift"
- self.set_meta("fts_storage_version", str(FTS_STORAGE_VERSION), cursor=conn)
+ # Not current with no older layout left means a definition this code does not recognise (a newer
+ # release's): the work is done, but the stamp is not this release's to write.
+ if current:
+ self.set_meta("fts_storage_version", str(FTS_STORAGE_VERSION), cursor=conn)
_delete_meta(conn, "fts_optimize_available")
conn.execute("UPDATE schema_version SET version = ? WHERE version < ?", (SCHEMA_VERSION, SCHEMA_VERSION))
return None
diff --git a/tests/hermes_state/test_fts_layout_drift.py b/tests/hermes_state/test_fts_layout_drift.py
index e7a2552d8b6..6bddb8f32ed 100644
--- a/tests/hermes_state/test_fts_layout_drift.py
+++ b/tests/hermes_state/test_fts_layout_drift.py
@@ -457,6 +457,28 @@ def test_a_stale_source_view_does_not_replace_a_newer_base_index(tmp_path):
assert _stamp(db_path) == str(FTS_STORAGE_VERSION + 1)
+def test_optimize_storage_on_a_newer_layout_finishes_its_work_without_restamping(tmp_path):
+ """Leftover old-index tables on a downgraded store are torn down and reported as done; the stamp stays."""
+ from hermes_state_common import FTS_STORAGE_VERSION
+
+ db_path = tmp_path / "state.db"
+ db = SessionDB(db_path=db_path)
+ _seed(db)
+ _install_newer_base_layout(db)
+ db._conn.execute("CREATE TABLE fts_v22_trash_messages_fts_data (docid INTEGER PRIMARY KEY, block BLOB)")
+ db._conn.commit()
+ db.close()
+
+ db = SessionDB(db_path=db_path)
+ try:
+ result = db.optimize_fts_storage(vacuum=False)
+ assert result["ok"] is True, result
+ assert not db._has_fts_trash(db._conn)
+ finally:
+ db.close()
+ assert _stamp(db_path) == str(FTS_STORAGE_VERSION + 1)
+
+
def test_doctor_offers_the_resume_after_backfill_before_teardown(tmp_path, monkeypatch):
"""optimize-storage interrupted after backfill but before trash teardown: doctor agrees with the update notice."""
from hermes_cli import doctor_state