Skip to content

The agent that grows with you

fix(tools): address reactions by the durable session id

funky-xamarin7f258a1c99f7
diff --git a/tests/tools/test_reaction_session_identity.py b/tests/tools/test_reaction_session_identity.py
new file mode 100644
index 00000000000..7551cce7c6b
--- /dev/null
+++ b/tests/tools/test_reaction_session_identity.py
@@ -0,0 +1,63 @@
+"""Agent reactions address durable session ids, not routing keys (#139102)."""
+
+import json
+
+import pytest
+
+from gateway.session_context import clear_session_vars, set_session_vars
+from hermes_state import SessionDB
+from tools import react_to_message_tool as reactions
+
+
[email protected]("routing_key", ["agent:main:local:dm:chat-a", "chat-b"])
+def test_reaction_uses_durable_session_with_routing_key(tmp_path, monkeypatch, routing_key):
+ path = tmp_path / "state.db"
+ with SessionDB(db_path=path) as db:
+ db.create_session("chat-a", "local")
+ db.append_message("chat-a", "user", "Earlier message")
+ earlier = db.latest_message_row_id("chat-a", role="user")
+ db.append_message("chat-a", "user", "Latest message")
+ latest = db.latest_message_row_id("chat-a", role="user")
+ db.create_session("chat-b", "local")
+ db.append_message("chat-b", "user", "Other conversation")
+ foreign = db.latest_message_row_id("chat-b", role="user")
+ emitted = []
+ monkeypatch.setattr(reactions, "_open_session_db", lambda: SessionDB(db_path=path))
+ monkeypatch.setattr(reactions.desktop_ui, "emit", lambda *args: emitted.append(args))
+ tokens = set_session_vars(session_key=routing_key, session_id="chat-a")
+ try:
+ def react(**args):
+ return json.loads(reactions.registry.dispatch("react_to_message", args))
+
+ result = react(emoji="πŸ‘")
+ assert result.get("success"), result
+ assert result["row_id"] == latest
+ assert react(emoji="❀️", messages_back=1)["row_id"] == earlier
+ assert react(emoji="πŸ˜‚", message_row_id=earlier)["success"]
+ assert "error" in react(emoji="πŸ‘", message_row_id=foreign)
+ assert "error" in react(emoji="πŸ‘", messages_back=99)
+ assert react(emoji="", message_row_id=earlier)["success"]
+ finally:
+ clear_session_vars(tokens)
+ with SessionDB(db_path=path) as reopened:
+ assert reopened.get_message_reactions("chat-a", latest)[0]["emoji"] == "πŸ‘"
+ assert reopened.get_message_reactions("chat-a", earlier) == []
+ assert reopened.get_message_reactions("chat-b", foreign) == []
+ assert len(emitted) == 4
+ assert all(name == "message.reaction" for name, _ in emitted)
+
+
+def test_reaction_keeps_legacy_key_fallback_and_empty_session(tmp_path, monkeypatch):
+ path = tmp_path / "state.db"
+ with SessionDB(db_path=path) as db:
+ db.create_session("legacy", "local")
+ db.append_message("legacy", "user", "Legacy message")
+ monkeypatch.setattr(reactions, "_open_session_db", lambda: SessionDB(db_path=path))
+ monkeypatch.setattr(reactions.desktop_ui, "emit", lambda *_args: None)
+ tokens = set_session_vars(session_key="legacy", session_id="")
+ try:
+ assert json.loads(reactions.registry.dispatch("react_to_message", {"emoji": "πŸ‘"}))["success"]
+ finally:
+ clear_session_vars(tokens)
+ result = json.loads(reactions.registry.dispatch("react_to_message", {"emoji": "πŸ‘"}))
+ assert "No active session" in result["error"]
diff --git a/tools/react_to_message_tool.py b/tools/react_to_message_tool.py
index 0308119ba01..74a30f8b51d 100644
--- a/tools/react_to_message_tool.py
+++ b/tools/react_to_message_tool.py
@@ -24,7 +24,8 @@ def _open_session_db():
def react_to_message_tool(emoji: str, message_row_id=None, messages_back=None) -> str:
"""Attach (or with an empty ``emoji`` retract) the agent's reaction."""
emoji = (emoji or "").strip()
- session_key = get_session_env("HERMES_SESSION_KEY", "") or get_session_env("HERMES_SESSION_ID", "")
+ # Routing keys identify a channel; message rows belong to the live durable session.
+ session_key = get_session_env("HERMES_SESSION_ID", "") or get_session_env("HERMES_SESSION_KEY", "")
if not session_key:
return tool_error("No active session β€” reactions need a persisted conversation.")
db = _open_session_db()