The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(state): honor _ensure_fts_cjk_schema's never-raises contract
chelsealong47b0e6561efe
diff --git a/hermes_state.py b/hermes_state.pyindex c619e440c2f..d81db6e0740 100644--- a/hermes_state.py+++ b/hermes_state.py@@ -5159,41 +5159,53 @@ class SessionDB(SessionSearchMixin, SessionSchemaMixin, SessionPortabilityMixin)and leave the stale breadcrumb (#self-heal). The table itselfstays for a later capable open to rebuild."""- cjk_present = bool(cursor.execute(- "SELECT 1 FROM sqlite_master WHERE type = 'table' "- "AND name = 'messages_fts_cjk'"- ).fetchone())-- if not self._fts_cjk_loaded:- if cjk_present:- live = [- r[0] for r in cursor.execute(- "SELECT name FROM sqlite_master WHERE type = 'trigger' "- f"AND name IN ({','.join('?' for _ in _FTS_CJK_TRIGGERS)})",- _FTS_CJK_TRIGGERS,- ).fetchall()- ]- if live:- # Self-heal: this process cannot tokenize, so every- # message INSERT would die inside the cjk trigger.- # Breadcrumb FIRST (crash between the two statements is- # merely conservative), then drop.- logger.warning(- "messages_fts_cjk triggers present but the "- "cjk_unicode61 tokenizer is unavailable (%s) — "- "dropping the cjk triggers so message writes keep "- "working. CJK search falls back to trigram/LIKE; "- "run `hermes sessions optimize-storage` on a host "- "with the extension to rebuild.",- fts5_cjk_so_path(),- )- cursor.execute(- "INSERT INTO state_meta (key, value) VALUES (?, '1') "- "ON CONFLICT(key) DO UPDATE SET value = '1'",- (FTS_CJK_STALE_KEY,),- )- for trig in live:- cursor.execute(f"DROP TRIGGER IF EXISTS {trig}")+ try:+ cjk_present = bool(cursor.execute(+ "SELECT 1 FROM sqlite_master WHERE type = 'table' "+ "AND name = 'messages_fts_cjk'"+ ).fetchone())++ if not self._fts_cjk_loaded:+ if cjk_present:+ live = [+ r[0] for r in cursor.execute(+ "SELECT name FROM sqlite_master WHERE type = 'trigger' "+ f"AND name IN ({','.join('?' for _ in _FTS_CJK_TRIGGERS)})",+ _FTS_CJK_TRIGGERS,+ ).fetchall()+ ]+ if live:+ # Self-heal: this process cannot tokenize, so every+ # message INSERT would die inside the cjk trigger.+ # Breadcrumb FIRST (crash between the two statements is+ # merely conservative), then drop.+ logger.warning(+ "messages_fts_cjk triggers present but the "+ "cjk_unicode61 tokenizer is unavailable (%s) — "+ "dropping the cjk triggers so message writes keep "+ "working. CJK search falls back to trigram/LIKE; "+ "run `hermes sessions optimize-storage` on a host "+ "with the extension to rebuild.",+ fts5_cjk_so_path(),+ )+ cursor.execute(+ "INSERT INTO state_meta (key, value) VALUES (?, '1') "+ "ON CONFLICT(key) DO UPDATE SET value = '1'",+ (FTS_CJK_STALE_KEY,),+ )+ for trig in live:+ cursor.execute(f"DROP TRIGGER IF EXISTS {trig}")+ self._fts_cjk_available = False+ return+ except sqlite3.OperationalError:+ # Mirror the tokenizer-loaded except below: the presence check+ # and self-heal above run before the loaded/not-loaded branch is+ # even known to be safe, so they need the same never-raises+ # guarantee the docstring promises for the rest of the method.+ logger.warning(+ "messages_fts_cjk presence check failed; CJK search stays on "+ "trigram/LIKE", exc_info=True,+ )self._fts_cjk_available = Falsereturndiff --git a/tests/test_fts_update_of_narrowing.py b/tests/test_fts_update_of_narrowing.pyindex 2a8e0d504f0..75742a76e2c 100644--- a/tests/test_fts_update_of_narrowing.py+++ b/tests/test_fts_update_of_narrowing.py@@ -265,6 +265,33 @@ def test_cjk_broad_trigger_is_restored_as_update_of(db.close()+def test_cjk_ensure_survives_presence_check_failure_when_tokenizer_unloaded(+ tmp_path: Path,+):+ """``_ensure_fts_cjk_schema`` is documented never to raise (#98834).++ The tokenizer-not-loaded branch runs its sqlite_master presence check+ and self-heal statements before any try/except guards them; a transient+ sqlite3.OperationalError there (e.g. "database is locked") must degrade+ to "no cjk index", not propagate.+ """+ db = SessionDB(db_path=tmp_path / "state.db")+ try:+ db._fts_cjk_loaded = False+ db._fts_cjk_available = True++ class _LockedOnMasterCursor:+ def execute(self, sql, *args, **kwargs):+ if "sqlite_master" in sql:+ raise sqlite3.OperationalError("database is locked")+ raise AssertionError(f"unexpected query: {sql}")++ db._ensure_fts_cjk_schema(_LockedOnMasterCursor())+ assert db._fts_cjk_available is False+ finally:+ db.close()++def test_legacy_migration_does_not_drop_cjk_trigger(tmp_path: Path):db = SessionDB(db_path=tmp_path / "state.db")try: