Skip to content

The agent that grows with you

refactor(state): one visibility clause for every reaction path; SQL-side reaction prefilter

kshitijk4poord695359d9e7a
diff --git a/hermes_state_messages.py b/hermes_state_messages.py
index 13458d6acf2..856e012ca60 100644
--- a/hermes_state_messages.py
+++ b/hermes_state_messages.py
@@ -36,7 +36,7 @@ _BUMP_GENERATION_SQL = """
_TURN_LEASE_ROW_SQL = "SELECT holder, expires_at FROM session_turn_leases WHERE conversation_id = ?"
_DELETE_COMPRESSION_LOCK_SQL = "DELETE FROM compression_locks WHERE session_id = ? AND holder = ?"
_DISPLAY_ACTIVE_CLAUSE = " AND (active = 1 OR compacted = 1)"
-_DISPLAY_META_ROW_SQL = "SELECT display_metadata FROM messages WHERE id = ? AND session_id IN ({ids})"
+_DISPLAY_META_ROW_SQL = "SELECT display_metadata FROM messages WHERE id = ? AND session_id IN ({ids})" + _DISPLAY_ACTIVE_CLAUSE
_ACTIVE_IDS_SQL = "SELECT id FROM messages WHERE session_id = ? AND active = 1 ORDER BY id"
_SET_COUNTERS_SQL = "UPDATE sessions SET message_count = ?, tool_call_count = ?"
_RESET_COUNTERS_SQL = "UPDATE sessions SET message_count = 0, tool_call_count = 0 WHERE id = ?"
@@ -391,7 +391,7 @@ class SessionMessagesMixin:
author: str = "user") -> Optional[List[Dict[str, Any]]]:
"""Set (``emoji=None``: clear) *author*'s reaction. Tapback semantics: one per author per message;
the same emoji again clears, a different one replaces. Returns the list after the write, or
- ``None`` for a foreign row."""
+ ``None`` for a row outside the session's visible resume lineage (see ``_reaction_row_query``)."""
if not session_id or message_row_id is None:
return None
sql, params = self._reaction_row_query(session_id, message_row_id)
@@ -422,8 +422,9 @@ class SessionMessagesMixin:
def _reaction_row_query(self, session_id: str, message_row_id: int) -> Tuple[str, tuple]:
"""A reaction addresses a row the client can SEE, and a display resume materializes the whole
- compression lineage with row ids — so a row is "in this session" when its owner is any lineage
- segment, not only the tip. Explicit ``/branch`` copies keep their own rows (``_resume_lineage_ids``)."""
+ compression lineage (active + compacted rows, with row ids) — so a row is "in this session" when
+ its owner is any lineage segment, not only the tip, and a rewound row is not. Explicit ``/branch``
+ copies keep their own rows (``_resume_lineage_ids``)."""
lineage = self._resume_lineage_ids(session_id)
return _DISPLAY_META_ROW_SQL.format(ids=_placeholders(lineage)), (message_row_id, *lineage)
@@ -436,10 +437,12 @@ class SessionMessagesMixin:
lineage = self._resume_lineage_ids(session_id)
def _do(conn):
pending = []
+ # Only reaction-bearing rows cross into Python: display_metadata also carries delivery /
+ # attachment markers on most rows, and the lineage scan grows with the session's age.
for row in conn.execute("SELECT id, role, content, display_metadata FROM messages "
- f"WHERE session_id IN ({_placeholders(lineage)}) AND (active = 1 OR compacted = 1) "
- "AND display_metadata IS NOT NULL ORDER BY id",
- tuple(lineage)).fetchall():
+ f"WHERE session_id IN ({_placeholders(lineage)}){_DISPLAY_ACTIVE_CLAUSE} "
+ f"AND {_sql_json_extract('display_metadata', '$.' + self.REACTIONS_METADATA_KEY)} IS NOT NULL "
+ "ORDER BY id", tuple(lineage)).fetchall():
meta = self._decode_display_metadata(row["display_metadata"])
reactions = meta.get(self.REACTIONS_METADATA_KEY) if meta else None
if not isinstance(reactions, list):
diff --git a/tests/agent/test_message_reactions.py b/tests/agent/test_message_reactions.py
index 0419c5e4193..d04de9348f9 100644
--- a/tests/agent/test_message_reactions.py
+++ b/tests/agent/test_message_reactions.py
@@ -189,32 +189,35 @@ def test_row_id_is_opt_in_and_never_reaches_the_provider(session, db):
)
-def test_ancestor_rows_react_through_the_continuation_key(session, db):
- """A display resume shows the whole compression lineage with row ids, so a row in an ended parent
- segment must react, read back and announce through the CONTINUATION key the client holds (#80670)."""
+def compacted(session, db):
+ """The parent segment ended by compaction; ``react-tip`` is the continuation the client now holds."""
parent, rows = session
assert db.try_acquire_compression_lock(parent, "w", ttl_seconds=60)
db.publish_compression_child(parent_session_id=parent, child_session_id="react-tip", source="test",
messages=[{"role": "user", "content": "summary"}], compression_lock_holder="w")
- display_rows = [m["_row_id"] for m in db.get_resume_conversations("react-tip")[1]]
- assert rows[1] in display_rows
+ return parent, rows, "react-tip"
- assert db.set_message_reaction("react-tip", rows[1], "👍") == db.get_message_reactions("react-tip", rows[1])
- assert db.get_message_reactions("react-tip", rows[1])[0]["emoji"] == "👍"
- assert [p["row_id"] for p in db.take_unseen_reactions("react-tip")] == [rows[1]]
+def test_ancestor_rows_react_through_the_continuation_key(compacted, db):
+ """A display resume shows the whole compression lineage with row ids, so a row in an ended parent
+ segment must react, read back and announce through the CONTINUATION key the client holds (#80670)."""
+ _parent, rows, tip = compacted
+ assert rows[1] in [m["_row_id"] for m in db.get_resume_conversations(tip)[1]]
+
+ assert db.set_message_reaction(tip, rows[1], "👍") == db.get_message_reactions(tip, rows[1])
+ assert db.get_message_reactions(tip, rows[1])[0]["emoji"] == "👍"
+ assert [p["row_id"] for p in db.take_unseen_reactions(tip)] == [rows[1]]
-def test_lineage_scope_still_rejects_foreign_and_branch_rows(session, db):
+
+def test_lineage_scope_still_rejects_foreign_and_branch_rows(compacted, db):
"""Lineage widening must not turn row ids into a global lookup: an unrelated session's row and a row
of an explicit /branch copy (which keeps its own rows) stay foreign to the continuation."""
- parent, rows = session
- assert db.try_acquire_compression_lock(parent, "w", ttl_seconds=60)
- db.publish_compression_child(parent_session_id=parent, child_session_id="react-tip", source="test",
- messages=[{"role": "user", "content": "summary"}], compression_lock_holder="w")
+ parent, rows, tip = compacted
other = db.create_session("elsewhere", "test")
foreign = db.append_message(other, "user", "other conversation")
branch = db.create_session("branch", "test", parent_session_id=parent, model_config={"_branched_from": parent})
- assert db.set_message_reaction("react-tip", foreign, "👍") is None
+ assert db.set_message_reaction(tip, foreign, "👍") is None
assert db.set_message_reaction(branch, rows[0], "👍") is None
assert db.get_message_reactions(branch, rows[0]) == []