Skip to content

The agent that grows with you

fix(state): deliver unseen reactions from visible compacted history

liuzikaiie07bc09a3f39
diff --git a/hermes_state_messages.py b/hermes_state_messages.py
index 38d9a6bcbbe..51ff31545d1 100644
--- a/hermes_state_messages.py
+++ b/hermes_state_messages.py
@@ -421,13 +421,15 @@ class SessionMessagesMixin:
def take_unseen_reactions(self, session_id: str, *, author: str = "user") -> List[Dict[str, Any]]:
"""Return *author*'s not-yet-surfaced reactions and mark them seen. Reactions are announced on the
- NEXT user turn (never by rewriting the reacted message: cache-safe); ``seen`` makes it exactly once."""
+ NEXT user turn (never by rewriting the reacted message: cache-safe); ``seen`` makes it exactly once.
+ Include compaction-archived history that remains visible, but exclude rewound/superseded rows."""
if not session_id:
return []
def _do(conn):
pending = []
for row in conn.execute("SELECT id, role, content, display_metadata FROM messages "
- "WHERE session_id = ? AND active = 1 AND display_metadata IS NOT NULL ORDER BY id",
+ "WHERE session_id = ? AND (active = 1 OR compacted = 1) "
+ "AND display_metadata IS NOT NULL ORDER BY id",
(session_id,)).fetchall():
meta = self._decode_display_metadata(row["display_metadata"])
reactions = meta.get(self.REACTIONS_METADATA_KEY) if meta else None
diff --git a/tests/hermes_state/test_compacted_message_reactions.py b/tests/hermes_state/test_compacted_message_reactions.py
new file mode 100644
index 00000000000..f1fceaaa574
--- /dev/null
+++ b/tests/hermes_state/test_compacted_message_reactions.py
@@ -0,0 +1,53 @@
+"""Visible archived messages keep the same once-only reaction contract."""
+
+import pytest
+
+from hermes_state import SessionDB
+
+
[email protected]("react_before_compaction", [False, True])
+def test_compacted_reactions_are_delivered_once_without_reviving_rewound_rows(
+ tmp_path, monkeypatch, react_before_compaction,
+):
+ monkeypatch.setenv("HERMES_HOME", str(tmp_path))
+ db_path = tmp_path / "state.db"
+ db = SessionDB(db_path=db_path)
+ try:
+ sid = db.create_session("compacted-reactions", "test")
+ db.append_message(sid, "user", "remember this answer")
+ target = db.append_message(sid, "assistant", "the original answer")
+ db.append_message(sid, "user", "continue")
+ tail = db.get_messages_as_conversation(sid)[-1]
+ if react_before_compaction:
+ db.set_message_reaction(sid, target, "πŸ‘")
+ db.archive_and_compact(
+ sid, [{"role": "user", "content": "summary"}, tail], tail_count=1,
+ )
+ visible = db.get_messages(sid, include_compacted=True)
+ assert target in {row["id"] for row in visible}
+ if not react_before_compaction:
+ assert db.set_message_reaction(sid, target, "πŸ‘")
+
+ rewind_target = db.append_message(sid, "user", "discard this turn")
+ discarded = db.append_message(sid, "assistant", "discarded answer")
+ db.set_message_reaction(sid, discarded, "πŸ‘Ž")
+ db.rewind_to_message(sid, rewind_target)
+ before = db.get_messages_as_conversation(sid)
+
+ pending = db.take_unseen_reactions(sid)
+ assert [(item["row_id"], item["emoji"], item["text"]) for item in pending] == [
+ (target, "πŸ‘", "the original answer"),
+ ]
+ assert db.take_unseen_reactions(sid) == []
+ assert db.get_messages_as_conversation(sid) == before
+ archived = {row["id"]: row for row in db.get_messages(sid, include_compacted=True)}
+ assert archived[target]["content"] == "the original answer"
+ assert discarded not in archived
+ finally:
+ db.close()
+
+ reopened = SessionDB(db_path=db_path)
+ try:
+ assert reopened.take_unseen_reactions(sid) == []
+ finally:
+ reopened.close()