From 8266ce661f385ceaa6c28fd39b8b6685d2992ac1 Mon Sep 17 00:00:00 2001 From: kewe63 Date: Wed, 25 Mar 2026 23:10:55 +0300 Subject: [PATCH] fix(state): release lock between context queries in search_messages The context-window queries (one per FTS5 match) were running inside the same lock acquisition as the primary FTS5 query, holding the lock for O(N) sequential SQLite round-trips. Move per-match context fetches outside the outer lock block so each acquires the lock independently, keeping critical sections short and allowing other threads to interleave. --- hermes_state.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/hermes_state.py b/hermes_state.py index 8f651750b6..3c06f1010f 100644 --- a/hermes_state.py +++ b/hermes_state.py @@ -886,9 +886,11 @@ class SessionDB: return [] matches = [dict(row) for row in cursor.fetchall()] - # Add surrounding context (1 message before + after each match) - for match in matches: - try: + # Add surrounding context (1 message before + after each match). + # Done outside the lock so we don't hold it across N sequential queries. + for match in matches: + try: + with self._lock: ctx_cursor = self._conn.execute( """SELECT role, content FROM messages WHERE session_id = ? AND id >= ? - 1 AND id <= ? + 1 @@ -899,9 +901,9 @@ class SessionDB: {"role": r["role"], "content": (r["content"] or "")[:200]} for r in ctx_cursor.fetchall() ] - match["context"] = context_msgs - except Exception: - match["context"] = [] + match["context"] = context_msgs + except Exception: + match["context"] = [] # Remove full content from result (snippet is enough, saves tokens) for match in matches: