Skip to content

The agent that grows with you

fix(state): only a malformed-image error gets the FTS offline-repair hint

kshitijk4poor995da31b27a0
diff --git a/hermes_state_search.py b/hermes_state_search.py
index bc66b2bebae..841d1ba8704 100644
--- a/hermes_state_search.py
+++ b/hermes_state_search.py
@@ -12,6 +12,7 @@ import time
from typing import Any, Callable, Collection, Dict, List, Optional, Tuple
from agent.skill_commands import describe_skill_invocation
+from hermes_state_errors import is_malformed_db_error
from hermes_state_common import (
FTS_CJK_STALE_KEY, FTS_SQL, FTS_STALE_KEY, FTS_STORAGE_VERSION, FTS_TOOL_CONTENT_PREFIX_CHARS,
FTS_TRIGRAM_EXCLUDED_SOURCES, FTS_TRIGRAM_SQL,
@@ -1326,15 +1327,14 @@ class SessionSearchMixin:
self._conn.execute(f"INSERT INTO {tbl}({tbl}) VALUES('rebuild')")
self._conn.commit()
rebuilt += 1
- except sqlite3.OperationalError as exc:
+ except sqlite3.DatabaseError as exc: # SQLITE_CORRUPT is not an OperationalError (#133375)
self._conn.rollback()
- logger.warning("FTS rebuild failed for %s: %s", tbl, exc)
- except sqlite3.DatabaseError as exc:
- # SQLITE_CORRUPT is a DatabaseError, not an OperationalError (#133375).
- self._conn.rollback()
- logger.error(
- "FTS rebuild failed with a corruption-class error for %s: %s; "
- "the index needs the offline repair path (repair_state_db_schema)", tbl, exc)
+ if is_malformed_db_error(exc):
+ logger.error(
+ "FTS rebuild failed with a corruption-class error for %s: %s; "
+ "the index needs the offline repair path (repair_state_db_schema)", tbl, exc)
+ else:
+ logger.warning("FTS rebuild failed for %s: %s", tbl, exc)
return rebuilt
def _merge_fts_incrementally(self, *, max_pages: int, max_commands: Optional[int] = None) -> int:
diff --git a/tests/hermes_state/test_fts_rebuild_corruption_class.py b/tests/hermes_state/test_fts_rebuild_corruption_class.py
index 5a7263aef5b..f1583575bfa 100644
--- a/tests/hermes_state/test_fts_rebuild_corruption_class.py
+++ b/tests/hermes_state/test_fts_rebuild_corruption_class.py
@@ -27,14 +27,17 @@ def db(tmp_path):
d.close()
-def _corrupting_execute(match, command="rebuild"):
- """Wrap ``execute`` so FTS *command* statements whose SQL satisfies *match* raise the
- corruption-class error; every other statement passes through."""
+_MALFORMED = sqlite3.DatabaseError("database disk image is malformed")
+
+
+def _corrupting_execute(match, command="rebuild", error=_MALFORMED):
+ """Wrap ``execute`` so FTS *command* statements whose SQL satisfies *match* raise *error*
+ (default: the corruption-class error); every other statement passes through."""
def wrap(real_execute):
def execute(sql, *args, **kwargs):
if f"VALUES('{command}')" in sql and match(sql):
- raise sqlite3.DatabaseError("database disk image is malformed")
+ raise error
return real_execute(sql, *args, **kwargs)
return execute
@@ -42,18 +45,22 @@ def _corrupting_execute(match, command="rebuild"):
return wrap
+ "error,repair_hint",
+ [(_MALFORMED, True), (sqlite3.IntegrityError("UNIQUE constraint failed"), False)],
+)
def test_corruption_class_error_is_caught_rolled_back_and_reported(
- db, monkeypatch, caplog
+ db, monkeypatch, caplog, error, repair_hint
):
"""The exact production failure (#133375): every index rebuild raises DatabaseError.
- The call must return 0 (no progress), roll the connection back, and say the offline
- repair path is needed — not propagate the error out of the loop."""
+ The call must return 0 (no progress) rather than propagate, and only a malformed-image
+ error earns the offline-repair hint — an IntegrityError is not corruption."""
monkeypatch.setattr(
- db._conn, "execute", _corrupting_execute(lambda sql: True)(db._conn.execute)
+ db._conn, "execute", _corrupting_execute(lambda sql: True, error=error)(db._conn.execute)
)
- with caplog.at_level("ERROR"):
+ with caplog.at_level("WARNING"):
assert db.rebuild_fts() == 0
- assert any("offline repair" in rec.message for rec in caplog.records)
+ assert any("offline repair" in rec.message for rec in caplog.records) is repair_hint
@pytest.mark.parametrize("method,command", [("rebuild_fts", "rebuild"), ("optimize_fts", "optimize")])