From ee9ec6164ced44838282017a3b3d3b8fe841b148 Mon Sep 17 00:00:00 2001 From: Adolanium <94890352+Adolanium@users.noreply.github.com> Date: Sun, 2 Aug 2026 21:55:04 +0300 Subject: [PATCH] perf(state): stop selecting full message content in session search Every search route in _search_messages_impl (FTS, CJK bigram, trigram, LIKE fallback, rebuild-gap supplement) selected m.content, then the result tail popped it unread. On DBs with multi-MB tool rows, each search read and materialized up to `limit` full rows only to discard them. Snippets come from snippet()/substr() in SQL and the context window is re-fetched by id, so no code path ever read the column. Drop the column from all six SELECT lists. Returned dicts are unchanged: content was never part of the public result (the pop ran before return), and tests/test_hermes_state.py already documents that contract. (cherry picked from commit d0c3af167e7dd4eb18e1bea29ba107a94911ea24) --- hermes_state_search.py | 17 +++++++++-------- tests/test_hermes_state.py | 3 +++ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/hermes_state_search.py b/hermes_state_search.py index b1cf669210..e8d29f413e 100644 --- a/hermes_state_search.py +++ b/hermes_state_search.py @@ -1390,7 +1390,6 @@ class SessionSearchMixin: m.session_id, m.role, snippet({table}, -1, '>>>', '<<<', '...', 40) AS snippet, - m.content, m.timestamp, m.tool_name, s.source, @@ -1583,7 +1582,7 @@ class SessionSearchMixin: sql = f""" SELECT m.id, m.session_id, m.role, substr(m.content, max(1, instr(m.content, ?) - 40), 120) AS snippet, - m.content, m.timestamp, m.tool_name, + m.timestamp, m.tool_name, s.source, s.model, s.started_at AS session_started FROM messages m JOIN sessions s ON s.id = m.session_id @@ -1689,7 +1688,12 @@ class SessionSearchMixin: except Exception: match["context"] = [] - # Remove full content from result (snippet is enough, saves tokens) + # Full message content is never selected by any search route: every + # SELECT returns snippet + metadata only (saves I/O on multi-MB tool + # rows and the tokens a content column would cost downstream). The + # context query above re-fetches its 3-message window by id, so + # nothing reads content from the match rows themselves. The pop stays + # as a belt-and-braces guard for any future route that selects it. for match in matches: match.pop("content", None) @@ -1823,7 +1827,6 @@ class SessionSearchMixin: m.session_id, m.role, snippet(messages_fts, -1, '>>>', '<<<', '...', 40) AS snippet, - m.content, m.timestamp, m.tool_name, s.source, @@ -1913,7 +1916,6 @@ class SessionSearchMixin: m.session_id, m.role, snippet(messages_fts_cjk, -1, '>>>', '<<<', '...', 40) AS snippet, - m.content, m.timestamp, m.tool_name, s.source, @@ -2002,7 +2004,6 @@ class SessionSearchMixin: m.session_id, m.role, snippet(messages_fts_trigram, -1, '>>>', '<<<', '...', 40) AS snippet, - m.content, m.timestamp, m.tool_name, s.source, @@ -2095,7 +2096,7 @@ class SessionSearchMixin: substr(m.content, max(1, instr(m.content, ?) - 40), 120) AS snippet, - m.content, m.timestamp, m.tool_name, + m.timestamp, m.tool_name, s.source, s.model, s.started_at AS session_started FROM messages m JOIN sessions s ON s.id = m.session_id @@ -2272,7 +2273,7 @@ class SessionSearchMixin: substr(m.content, max(1, instr(m.content, ?) - 40), 120) AS snippet, - m.content, m.timestamp, m.tool_name, + m.timestamp, m.tool_name, s.source, s.model, s.started_at AS session_started FROM messages m JOIN sessions s ON s.id = m.session_id diff --git a/tests/test_hermes_state.py b/tests/test_hermes_state.py index 1b2f688ae7..2d72066978 100644 --- a/tests/test_hermes_state.py +++ b/tests/test_hermes_state.py @@ -473,6 +473,7 @@ class TestSessionLifecycle: results = db.search_messages("大别山") assert len(results) == 1 # Note: search_messages strips 'content' from results; use 'snippet'. + assert "content" not in results[0] assert "大别山" in results[0]["snippet"] finally: db.close() @@ -739,6 +740,8 @@ class TestFTS5Search: # At least one result should mention docker snippets = [r.get("snippet", "") for r in results] assert any("docker" in s.lower() or "Docker" in s for s in snippets) + # Results never carry full content; snippet + metadata only. + assert all("content" not in r for r in results)