From a2413495ad0a545c80deed7600e7b52c99e8d6c6 Mon Sep 17 00:00:00 2001 From: Teknium <127238744+teknium1@users.noreply.github.com> Date: Fri, 11 Sep 2026 03:32:19 -0700 Subject: [PATCH] fix(state): fts_trigram_session_sql qualifies model_config under the json_valid guard The _sql_json_extract wrapper removed the `COALESCE(model_config` text the alias string-replace keyed on, so the deferred-backfill SELECT joined an unqualified `model_config`. Build the predicate from the alias directly and derive the unaliased constant from it, so the two can never disagree. --- hermes_state_common.py | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/hermes_state_common.py b/hermes_state_common.py index b1f4ec28aa..441e8f39c2 100644 --- a/hermes_state_common.py +++ b/hermes_state_common.py @@ -756,24 +756,22 @@ END; # ``parent_session_id`` but NOT the marker, so they stay trigram-indexed. FTS_TRIGRAM_EXCLUDED_SOURCES = ("cron", "subagent") -# Predicate over a ``sessions`` row (unqualified column names) selecting -# sessions whose rows belong in the trigram index. Shared by the view, the -# sync triggers, and the deferred-backfill INSERT ... SELECTs so they can -# never disagree about the index boundary. -FTS_TRIGRAM_SESSION_SQL = ( - "source NOT IN (" - + ", ".join(f"'{src}'" for src in FTS_TRIGRAM_EXCLUDED_SOURCES) - + f") AND {_sql_json_extract('model_config', '$._delegate_from')} IS NULL" -) - - -def fts_trigram_session_sql(alias: str) -> str: - """``FTS_TRIGRAM_SESSION_SQL`` with every column qualified by ``alias``.""" - return FTS_TRIGRAM_SESSION_SQL.replace("source ", f"{alias}.source ").replace( - "COALESCE(model_config", f"COALESCE({alias}.model_config" +def fts_trigram_session_sql(alias: str = "") -> str: + """Predicate over a ``sessions`` row selecting sessions whose rows belong in + the trigram index; ``alias`` qualifies every column for joins. Shared by the + view, the sync triggers, and the deferred-backfill INSERT ... SELECTs so they + can never disagree about the index boundary.""" + q = f"{alias}." if alias else "" + return ( + f"{q}source NOT IN (" + + ", ".join(f"'{src}'" for src in FTS_TRIGRAM_EXCLUDED_SOURCES) + + f") AND {_sql_json_extract(q + 'model_config', '$._delegate_from')} IS NULL" ) +FTS_TRIGRAM_SESSION_SQL = fts_trigram_session_sql() + + FTS_TRIGRAM_SQL = f""" CREATE VIEW IF NOT EXISTS messages_fts_trigram_src AS SELECT m.id, m.role, m.content, m.tool_name