Skip to content

The agent that grows with you

fix(state): quarantine CJK when ensure soft-fails after OF migration

Jasmine Naderib66111fc5827
diff --git a/hermes_state_schema.py b/hermes_state_schema.py
index ce9c48e857b..2bf0f28490e 100644
--- a/hermes_state_schema.py
+++ b/hermes_state_schema.py
@@ -131,26 +131,26 @@ class SessionSchemaMixin:
cursor, "messages_fts_trigram", FTS_TRIGRAM_SQL
)
# CJK triggers live on the host SessionDB; only recreate one that
- # this migration actually dropped. Unexpected ensure failures
- # must not leave the host advertising a now-triggerless index.
- if (
- "messages_fts_cjk_update" in to_drop
- ):
+ # this migration actually dropped. ``_ensure_fts_cjk_schema`` is
+ # documented never-raises and soft-fails OperationalError by
+ # clearing availability — raise-path handling alone is not
+ # enough. After ensure, require a narrowed CJK UPDATE trigger or
+ # durable quarantine (stale breadcrumb + unavailable).
+ if "messages_fts_cjk_update" in to_drop:
try:
self._ensure_fts_cjk_schema(cursor)
except Exception:
- self._fts_cjk_available = False
- try:
- self.set_meta(FTS_CJK_STALE_KEY, "1", cursor=cursor)
- except Exception:
- logger.debug(
- "Could not persist CJK FTS stale breadcrumb",
- exc_info=True,
- )
+ self._quarantine_cjk_after_update_of_migration(cursor)
logger.exception(
"CJK FTS re-ensure after UPDATE OF migration failed"
)
raise
+ if not self._cjk_update_trigger_is_narrowed(cursor):
+ self._quarantine_cjk_after_update_of_migration(cursor)
+ logger.warning(
+ "CJK FTS UPDATE trigger missing or still broad after "
+ "UPDATE OF migration; marked stale and unavailable"
+ )
logger.info(
"Migrated %d broad FTS UPDATE trigger(s) to AFTER UPDATE OF "
@@ -159,6 +159,43 @@ class SessionSchemaMixin:
)
return len(to_drop)
+ def _cjk_update_trigger_is_narrowed(self, cursor: sqlite3.Cursor) -> bool:
+ """True when messages_fts_cjk_update exists with AFTER UPDATE OF."""
+ row = cursor.execute(
+ "SELECT sql FROM sqlite_master "
+ "WHERE type = 'trigger' AND name = ?",
+ ("messages_fts_cjk_update",),
+ ).fetchone()
+ if not row:
+ return False
+ sql = row[0] if not isinstance(row, sqlite3.Row) else row["sql"]
+ return not self._fts_update_trigger_needs_narrowing(sql)
+
+ def _quarantine_cjk_after_update_of_migration(
+ self, cursor: sqlite3.Cursor
+ ) -> None:
+ """Fail-closed after dropping CJK UPDATE during OF migration.
+
+ Clears availability, persists ``fts_cjk_stale``, and drops any
+ residual broad/partial CJK UPDATE trigger so a later open cannot
+ ``CREATE TRIGGER IF NOT EXISTS`` a gap without rebuild.
+ """
+ self._fts_cjk_available = False
+ try:
+ self.set_meta(FTS_CJK_STALE_KEY, "1", cursor=cursor)
+ except Exception:
+ logger.debug(
+ "Could not persist CJK FTS stale breadcrumb",
+ exc_info=True,
+ )
+ try:
+ cursor.execute("DROP TRIGGER IF EXISTS messages_fts_cjk_update")
+ except Exception:
+ logger.debug(
+ "Could not drop residual CJK UPDATE trigger after quarantine",
+ exc_info=True,
+ )
+
@staticmethod
def _rebuild_fts_indexes(
diff --git a/tests/test_fts_update_of_narrowing.py b/tests/test_fts_update_of_narrowing.py
index f66c10e7eed..2a8e0d504f0 100644
--- a/tests/test_fts_update_of_narrowing.py
+++ b/tests/test_fts_update_of_narrowing.py
@@ -200,6 +200,43 @@ def test_cjk_ensure_failure_marks_unavailable_and_propagates(
db.close()
+def test_cjk_soft_fail_ensure_without_raise_marks_stale(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+):
+ """Production ensure swallows OperationalError — still must quarantine."""
+ path = tmp_path / "state.db"
+ db = SessionDB(db_path=path)
+ try:
+ db._conn.execute("DROP TRIGGER IF EXISTS messages_fts_cjk_update")
+ db._conn.execute(
+ "CREATE TRIGGER messages_fts_cjk_update "
+ "AFTER UPDATE ON messages BEGIN SELECT 1; END"
+ )
+ db._fts_cjk_available = True
+
+ def _soft_fail_cjk_ensure(_cursor):
+ # Mirrors real _ensure_fts_cjk_schema OperationalError path:
+ # clear availability, do not raise, do not recreate triggers.
+ db._fts_cjk_available = False
+
+ monkeypatch.setattr(db, "_ensure_fts_cjk_schema", _soft_fail_cjk_ensure)
+
+ dropped = db._migrate_broad_fts_update_triggers(db._conn)
+ assert dropped >= 1
+ assert db._fts_cjk_available is False
+ assert _trigger_sql(db._conn, "messages_fts_cjk_update") is None
+
+ with sqlite3.connect(path) as observer:
+ stale = observer.execute(
+ "SELECT value FROM state_meta WHERE key = ?",
+ (FTS_CJK_STALE_KEY,),
+ ).fetchone()
+ assert stale == ("1",)
+ assert _trigger_sql(observer, "messages_fts_cjk_update") is None
+ finally:
+ db.close()
+
+
def test_cjk_broad_trigger_is_restored_as_update_of(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
):