The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(state): key display identity of tool rows on stable tool identity, not payload bytes (#117750)
salch-cred912c9ded3589
diff --git a/hermes_state_messages.py b/hermes_state_messages.pyindex bf8a01a1a9e..05d92a888c5 100644--- a/hermes_state_messages.py+++ b/hermes_state_messages.py@@ -1174,7 +1174,16 @@ class SessionMessagesMixin:(session_id, _scrub_surrogates(display_kind)))def _display_dedupe_key(self, row) -> Tuple[Any, ...]:- """Historical display identity, including normalized live content from user handoff carriers."""+ """Historical display identity, including normalized live content from user handoff carriers.++ Tool-payload rows are keyed on their STABLE tool identity, not the payload bytes: a+ carried-forward row whose result content was summarized or whose call arguments were+ truncated by a prune is the same logical event (#117750) and must still collapse with+ its durable original — payload bytes would split it, re-projecting the archived+ original after the rewritten copy. A tool row names its call (``tool_call_id``); an+ assistant row's calls each carry an id. Rows without a stable tool identity keep the+ full content key, so unrelated same-content turns never merge.+ """dedupe_content = row["content"]if row["role"] == "user":handoff, live_view = split_user_originated_turn({@@ -1183,6 +1192,21 @@ class SessionMessagesMixin:"display_metadata": self._decode_display_metadata(row["display_metadata"])})if handoff is not None and live_view is not None:dedupe_content = self._encode_content(live_view.get("content"))+ if row["role"] == "tool":+ if row["tool_call_id"]:+ return (row["role"], None, row["timestamp"],+ row["tool_call_id"], row["tool_calls"], row["tool_name"])+ return (row["role"], dedupe_content, row["timestamp"],+ row["tool_call_id"], row["tool_calls"], row["tool_name"])+ if row["role"] == "assistant" and row["tool_calls"]:+ try:+ calls = _parse_tool_calls(row["tool_calls"])+ except Exception:+ calls = None+ call_ids = tuple(coalesce_tool_call_id(tc) for tc in calls or ())+ if calls and all(call_ids):+ return (row["role"], None, row["timestamp"],+ row["tool_call_id"], call_ids, row["tool_name"])return (row["role"], dedupe_content, row["timestamp"],row["tool_call_id"], row["tool_calls"], row["tool_name"])diff --git a/tests/hermes_state/test_display_dedupe_pruned_tool_payload.py b/tests/hermes_state/test_display_dedupe_pruned_tool_payload.pynew file mode 100644index 00000000000..105f782fe7c--- /dev/null+++ b/tests/hermes_state/test_display_dedupe_pruned_tool_payload.py@@ -0,0 +1,90 @@+"""#117750: a prune rewrite of a carried-forward tool payload must not split the+logical event into a second display identity.++``_display_dedupe_key`` includes the payload bytes (tool-result content, call+arguments). The proactive prune shortens exactly those bytes on the carried+copies it publishes, so the rewritten copy and its archived durable original+used to project as two logical messages — the older one reappearing after a+later answer. Tool rows are now keyed on their stable tool identity; these+tests pin both directions (rewritten copies collapse, genuinely distinct calls+do not merge).+"""+import json+import os+import tempfile+from pathlib import Path++import pytest++from hermes_state import SessionDB+++def db(tmp_path, monkeypatch):+ monkeypatch.setenv("HERMES_HOME", str(tmp_path))+ instance = SessionDB(tmp_path / "state.db")+ yield instance+ instance.close()+++def _seed_session(db, sid):+ db.create_session(sid, source="test")+ db.append_messages_batch(sid, [+ {"role": "user", "content": "start", "timestamp": 100.0},+ {"role": "assistant", "content": "Earlier progress", "timestamp": 101.0,+ "tool_calls": [{"id": "stable-call", "type": "function", "function":+ {"name": "demo_tool", "arguments": json.dumps({"value": "L" * 4000})}}]},+ {"role": "tool", "content": "R" * 5000, "tool_call_id": "stable-call",+ "tool_name": "demo_tool", "timestamp": 102.0},+ {"role": "assistant", "content": "Later answer", "timestamp": 200.0},+ ])+++def test_pruned_tool_payload_keeps_one_display_identity(db):+ """The issue's synthetic repro: shorten the carried tool args + result, commit,+ and the assistant sequence must not gain a duplicate of the earlier message."""+ sid = "pruned-payload"+ _seed_session(db, sid)+ history = db.get_messages_as_conversation(sid, include_row_ids=True)+ history[1]["tool_calls"][0]["function"]["arguments"] = json.dumps({"value": "short"})+ history[2]["content"] = "short result"+ db.archive_and_compact(sid, history)+ visible = db.get_messages_as_conversation(sid, include_row_ids=True, include_compacted=True)+ assert [m.get("content") for m in visible if m["role"] == "assistant"] == \+ ["Earlier progress", "Later answer"]+ tool_rows = [m for m in visible if m["role"] == "tool"]+ assert len(tool_rows) == 1+++def test_distinct_assistant_calls_with_same_args_are_not_merged(db):+ """The widened key keys assistant rows on their call IDS: two different calls+ that happen to share arguments and timestamp stay separate display events."""+ sid = "distinct-calls"+ db.create_session(sid, source="test")+ db._execute_write(lambda conn: [+ conn.execute(+ "INSERT INTO messages (session_id, role, content, tool_calls, timestamp,"+ " active, compacted) VALUES (?, ?, ?, ?, ?, 1, 0)",+ (sid, "assistant", "working",+ json.dumps([{"id": cid, "type": "function",+ "function": {"name": "demo_tool",+ "arguments": json.dumps({"q": "same"})}}]),+ 1700000000.0))+ for cid in ("call-1", "call-2")])+ msgs = db.get_messages(sid, include_compacted=True)+ assert len(msgs) == 2+++def test_tool_row_without_call_id_keeps_content_key(db):+ """A legacy tool row with no tool_call_id must not lose its content key:+ two such rows with different payloads stay separate."""+ sid = "legacy-no-call-id"+ db.create_session(sid, source="test")+ db._execute_write(lambda conn: [+ conn.execute(+ "INSERT INTO messages (session_id, role, content, tool_name, timestamp,"+ " active, compacted) VALUES (?, ?, ?, ?, ?, 1, 0)",+ (sid, "tool", content, "demo_tool", 1700000000.0))+ for content in ("result one", "result two")])+ msgs = db.get_messages(sid, include_compacted=True)+ assert len(msgs) == 2