Skip to content

The agent that grows with you

fix(state): leave v1 trigram layouts to optimize-storage in the cron-exclusion migration

kshitijk4poor0de34c1f13c3
diff --git a/hermes_state_schema.py b/hermes_state_schema.py
index d1152a366b8..38d7ba3cd0a 100644
--- a/hermes_state_schema.py
+++ b/hermes_state_schema.py
@@ -426,6 +426,15 @@ class SessionSchemaMixin:
"""
if self._db_has_legacy_inline_fts(cursor):
return True
+ if self._db_has_trigram_tool_calls_projection(cursor):
+ # The existing vtable still declares the FTS_STORAGE_VERSION 1
+ # ``tool_calls`` column. Replacing the view underneath it would
+ # make the 'rebuild' read ``T.tool_calls`` from a view that no
+ # longer has it. Changing vtable columns is the opt-in
+ # ``hermes sessions optimize-storage`` path (it recreates the
+ # vtable from FTS_TRIGRAM_SQL, cron-filtered view included), so
+ # leave this install to that path instead of half-migrating it.
+ return True
trigram_exists = self._fts_table_probe(cursor, "messages_fts_trigram")
if trigram_exists is not True:
# Let the normal ensure path create/backfill a missing optional
diff --git a/tests/state/test_fts_trigram_cron_exclusion.py b/tests/state/test_fts_trigram_cron_exclusion.py
index 7fe99dfc7d5..5e115cc67d3 100644
--- a/tests/state/test_fts_trigram_cron_exclusion.py
+++ b/tests/state/test_fts_trigram_cron_exclusion.py
@@ -28,22 +28,37 @@ def _trigram_rowids(db: SessionDB) -> set[int]:
}
-def _install_pre_v27_trigram(db: SessionDB) -> None:
+def _install_pre_v27_trigram(db: SessionDB, *, with_tool_calls: bool = False) -> None:
+ """Recreate the pre-cron-exclusion external-content trigram boundary.
+
+ ``with_tool_calls=True`` reproduces the FTS_STORAGE_VERSION 1 vtable
+ (``tool_calls`` projected) that installs upgraded before #88217 carry;
+ the default is the v2 column set with only the view/trigger predicates
+ behind, which is what the in-place v29 migration handles.
+ """
+ cols = "content, tool_name" + (", tool_calls" if with_tool_calls else "")
+ vals = "new.content, new.tool_name" + (", new.tool_calls" if with_tool_calls else "")
db._conn.executescript(
- """
+ f"""
DROP TRIGGER messages_fts_trigram_insert;
DROP TRIGGER messages_fts_trigram_delete;
DROP TRIGGER messages_fts_trigram_update;
+ DROP TABLE messages_fts_trigram;
DROP VIEW messages_fts_trigram_src;
CREATE VIEW messages_fts_trigram_src AS
SELECT id, role, content, tool_name, tool_calls
FROM messages WHERE role <> 'tool';
+ CREATE VIRTUAL TABLE messages_fts_trigram USING fts5(
+ {cols},
+ content='messages_fts_trigram_src',
+ content_rowid='id',
+ tokenize='trigram'
+ );
CREATE TRIGGER messages_fts_trigram_insert AFTER INSERT ON messages
WHEN new.role <> 'tool'
BEGIN
- INSERT INTO messages_fts_trigram(
- rowid, content, tool_name, tool_calls
- ) VALUES (new.id, new.content, new.tool_name, new.tool_calls);
+ INSERT INTO messages_fts_trigram(rowid, {cols})
+ VALUES (new.id, {vals});
END;
"""
)
@@ -161,6 +176,42 @@ def test_install_already_at_v28_still_gets_the_cron_exclusion_migration(tmp_path
migrated.close()
+def test_v1_tool_calls_layout_is_left_for_optimize_storage(tmp_path):
+ """A FTS_STORAGE_VERSION 1 trigram vtable (``tool_calls`` projected) must
+ survive the v29 startup migration untouched and be finished by the opt-in
+ ``optimize_fts_storage`` path — not half-migrated into a view/vtable
+ column mismatch (which used to fail the rebuild with
+ ``no such column: T.tool_calls``)."""
+ db_path = tmp_path / "state.db"
+ old = SessionDB(db_path=db_path)
+ if not old._trigram_available:
+ old.close()
+ pytest.skip("trigram tokenizer unavailable in this SQLite build")
+ _install_pre_v27_trigram(old, with_tool_calls=True)
+ old.create_session("cli", source="cli")
+ old.create_session("cron", source="cron")
+ cli_id = old.append_message("cli", role="user", content="交互迁移内容")
+ cron_id = old.append_message("cron", role="user", content="定时迁移内容")
+ assert _trigram_rowids(old) == {cli_id, cron_id}
+ old._conn.execute("UPDATE schema_version SET version = 28")
+ old._conn.commit()
+ old.close()
+
+ migrated = SessionDB(db_path=db_path) # must not raise
+ try:
+ # Startup left the v1 layout alone (cron row still there) …
+ assert _trigram_rowids(migrated) == {cli_id, cron_id}
+ assert migrated.fts_optimize_available() is True
+ # … and the opt-in path completes the transition: v2 columns,
+ # cron-filtered view, cron row purged.
+ migrated.optimize_fts_storage()
+ cols = [r[1] for r in migrated._conn.execute("PRAGMA table_info(messages_fts_trigram)")]
+ assert "tool_calls" not in cols
+ assert _trigram_rowids(migrated) == {cli_id}
+ finally:
+ migrated.close()
+
+
def test_partial_upgrade_view_does_not_skip_historical_rebuild(tmp_path):
db_path = tmp_path / "state.db"
old = SessionDB(db_path=db_path)