Skip to content

The agent that grows with you

fix(state): route session-resume reads through the WAL read-only connection

pierrenode67d4bbb812cc
diff --git a/hermes_state.py b/hermes_state.py
index 129de1326e8..0396567c80b 100644
--- a/hermes_state.py
+++ b/hermes_state.py
@@ -7024,9 +7024,9 @@ class SessionDB(SessionSearchMixin, SessionSchemaMixin, SessionPortabilityMixin)
session_ids = self._session_lineage_root_to_tip(session_id)
active_clause = "" if include_inactive else " AND active = 1"
- with self._lock:
+ with self._read_ctx() as conn:
placeholders = ",".join("?" for _ in session_ids)
- rows = self._conn.execute(
+ rows = conn.execute(
f"SELECT {self._CONVERSATION_ROW_COLUMNS} "
f"FROM messages WHERE session_id IN ({placeholders})"
# Order by AUTOINCREMENT id (true insertion order), NOT timestamp:
@@ -7205,9 +7205,9 @@ class SessionDB(SessionSearchMixin, SessionSchemaMixin, SessionPortabilityMixin)
output (see test_get_resume_conversations_matches_separate_reads).
"""
session_ids = self._session_lineage_root_to_tip(session_id)
- with self._lock:
+ with self._read_ctx() as conn:
placeholders = ",".join("?" for _ in session_ids)
- rows = self._conn.execute(
+ rows = conn.execute(
f"SELECT session_id, {self._CONVERSATION_ROW_COLUMNS} "
f"FROM messages WHERE session_id IN ({placeholders}) AND active = 1 "
# ORDER BY id (insertion order) — see get_messages_as_conversation
@@ -7259,9 +7259,9 @@ class SessionDB(SessionSearchMixin, SessionSchemaMixin, SessionPortabilityMixin)
session_ids = self._session_lineage_root_to_tip(session_id)
if len(session_ids) <= 1:
return []
- with self._lock:
+ with self._read_ctx() as conn:
placeholders = ",".join("?" for _ in session_ids)
- rows = self._conn.execute(
+ rows = conn.execute(
f"SELECT session_id, {self._CONVERSATION_ROW_COLUMNS} "
f"FROM messages WHERE session_id IN ({placeholders}) AND active = 1 "
"ORDER BY id",
@@ -7298,13 +7298,13 @@ class SessionDB(SessionSearchMixin, SessionSchemaMixin, SessionPortabilityMixin)
chain = []
current = session_id
seen = set()
- with self._lock:
+ with self._read_ctx() as conn:
for _ in range(100):
if not current or current in seen:
break
seen.add(current)
chain.append(current)
- row = self._conn.execute(
+ row = conn.execute(
"SELECT parent_session_id FROM sessions WHERE id = ?",
(current,),
).fetchone()
diff --git a/tests/test_session_db_read_path_split.py b/tests/test_session_db_read_path_split.py
index 35c54228301..1b28b905894 100644
--- a/tests/test_session_db_read_path_split.py
+++ b/tests/test_session_db_read_path_split.py
@@ -129,3 +129,41 @@ def test_anchored_view_and_around_use_read_path(db):
assert done["view"]["window"]
finally:
db._lock.release()
+
+
+def test_session_resume_reads_do_not_take_writer_lock(db):
+ """session.resume's three read paths must not convoy behind writer flushes.
+
+ get_messages_as_conversation / get_resume_conversations /
+ get_ancestor_display_prefix are the hottest reads in the file — every
+ resume across the gateway, CLI, and ACP adapter goes through one of
+ them — so they must use the same per-thread read-only connection as
+ get_messages, not the legacy self._lock path.
+ """
+ db.create_session(session_id="parent1", source="cli", model="m")
+ db.append_message("parent1", role="user", content="parent turn")
+ db.append_message("parent1", role="assistant", content="parent reply")
+ db.create_session(session_id="child1", source="cli", model="m", parent_session_id="parent1")
+ db.append_message("child1", role="user", content="child turn")
+ db.append_message("child1", role="assistant", content="child reply")
+
+ acquired = db._lock.acquire()
+ try:
+ done = {}
+
+ def reader():
+ done["conversation"] = db.get_messages_as_conversation("s1")
+ done["resume"] = db.get_resume_conversations("child1")
+ done["ancestor_prefix"] = db.get_ancestor_display_prefix("child1")
+
+ t = threading.Thread(target=reader)
+ t.start(); t.join(timeout=5.0)
+ assert not t.is_alive(), "session resume reads blocked on writer lock"
+ assert len(done["conversation"]) == 2
+ model_history, display_history = done["resume"]
+ assert len(model_history) == 2
+ assert len(display_history) == 4
+ assert len(done["ancestor_prefix"]) == 2
+ finally:
+ db._lock.release()