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)
This commit is contained in:
Adolanium
2026-08-02 21:55:04 +03:00
committed by Teknium
parent 29ce482047
commit ee9ec6164c
2 changed files with 12 additions and 8 deletions
+9 -8
View File
@@ -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
+3
View File
@@ -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)