Skip to content

The agent that grows with you

fix: restore generic corruption match in FTS self-heal

kshitij987064caa4f8
diff --git a/hermes_state.py b/hermes_state.py
index 24834f59014..a6a0dc7441e 100644
--- a/hermes_state.py
+++ b/hermes_state.py
@@ -4611,12 +4611,18 @@ class SessionDB(SessionSearchMixin, SessionSchemaMixin, SessionPortabilityMixin)
def _is_fts_write_corruption_error(exc: sqlite3.DatabaseError) -> bool:
"""True for the error class a corrupt FTS index raises on writes.
- Only an FTS5-specific error is safe to recover online. SQLite's
- generic ``database disk image is malformed`` does not identify the
- damaged object and may represent canonical B-tree or freelist damage;
- treating it as FTS-only corruption would continue writing to a
- structurally damaged database.
- """
+ SQLite's message for a corrupt FTS index varies by version: older
+ builds raise the generic ``database disk image is malformed`` (covered
+ by :func:`is_malformed_db_error`); newer builds raise the FTS5-specific
+ ``fts5: corrupt structure record for table "messages_fts"``. Both mean
+ the same thing for the write path: the canonical rows are fine, the
+ FTS shadow tables are not. The FTS-only rebuild and fail-open
+ detach are safe here because they only touch derived indexes; if the
+ damage is actually in a canonical B-tree, the rebuild itself fails and
+ the write propagates.
+ """
+ if is_malformed_db_error(exc):
+ return True
msg = str(exc).lower()
return "fts5" in msg and "corrupt" in msg
diff --git a/tests/state/test_fts_runtime_rebuild.py b/tests/state/test_fts_runtime_rebuild.py
index 68aa917d988..b511acf7b7d 100644
--- a/tests/state/test_fts_runtime_rebuild.py
+++ b/tests/state/test_fts_runtime_rebuild.py
@@ -222,9 +222,11 @@ class TestRuntimeFtsRebuild:
# Cleanup
os.chmod(proc_root / "222" / "fd", 0o755)
- def test_corruption_error_classification_requires_fts_provenance(self):
- """Generic SQLITE_CORRUPT cannot prove only derived data is damaged."""
- assert not SessionDB._is_fts_write_corruption_error(
+ def test_corruption_error_classification_covers_both_sqlite_messages(self):
+ """SQLite's message for a corrupt FTS index varies by version: older
+ builds raise the generic malformed-image error, newer builds raise an
+ FTS5-specific one. Both must trigger the self-heal."""
+ assert SessionDB._is_fts_write_corruption_error(
sqlite3.DatabaseError("database disk image is malformed")
)
assert SessionDB._is_fts_write_corruption_error(
@@ -236,21 +238,6 @@ class TestRuntimeFtsRebuild:
sqlite3.DatabaseError("no such table: nothing_fts_related")
)
- def test_generic_malformed_write_fails_closed(self, db, monkeypatch):
- db.create_session("s1", source="test")
- monkeypatch.setattr(
- db, "rebuild_fts", lambda: pytest.fail("must not rebuild FTS")
- )
-
- def _structural_corruption(_conn):
- raise sqlite3.DatabaseError("database disk image is malformed")
-
- with pytest.raises(sqlite3.DatabaseError, match="disk image is malformed"):
- db._execute_write(_structural_corruption)
-
- assert db._fts_runtime_rebuild_attempted is False
- assert db._fts_enabled is True
-
def test_append_self_heals_after_fts_corruption(self, db, tmp_path):
if not db._fts_enabled:
pytest.skip("FTS5 unavailable in this build")