Skip to content

The agent that grows with you

fix(gateway): replay keeps message identity on plain user and assistant rows

Eva19d54c0611b2
diff --git a/gateway/run.py b/gateway/run.py
index f0f6da121ed..620fe26bfe9 100644
--- a/gateway/run.py
+++ b/gateway/run.py
@@ -39,6 +39,7 @@ from agent.conversation_compression import (
PRE_API_COMPRESSION_STATUS_TEMPLATE, PREFLIGHT_COMPRESSION_STATUS_TEMPLATE)
from agent.conversation_loop import INTERRUPT_WAITING_FOR_MODEL_PREFIX
from agent.interrupt_compat import request_hard_interrupt
+from agent.message_metadata import ABSORBED_MESSAGE_UIDS, MESSAGE_UID, copy_identity_fields
from agent.turn_context import compression_made_progress
from agent.session_activity import ActivityProvenance
from hermes_cli.config import _is_ssh_remote_tilde_cwd, cfg_get
@@ -1147,6 +1148,10 @@ def _build_replay_entry(
entry[_rkey] = _rval
if preserve_timestamp and msg.get("timestamp"):
entry["timestamp"] = msg["timestamp"]
+ # Replay rebuilds the SAME conversation for its next turn: every role keeps its uid and merge witness, so a
+ # context engine sees the uids the store holds. Tool-call uid maps stay with the rows that still carry
+ # their calls (those pass through whole); on a plain row a leftover map would name calls it no longer has.
+ copy_identity_fields({key: msg[key] for key in (MESSAGE_UID, ABSORBED_MESSAGE_UIDS) if key in msg}, entry)
# Replay rewrites are view-only: keep the durable-row stamp so marker-only
# flushes skip rows already in state.db (#121462/#123462).
if msg.get("_db_persisted"):
diff --git a/tests/gateway/test_replay_entry_fields.py b/tests/gateway/test_replay_entry_fields.py
index 32719d6d12c..a2388e064eb 100644
--- a/tests/gateway/test_replay_entry_fields.py
+++ b/tests/gateway/test_replay_entry_fields.py
@@ -149,3 +149,35 @@ def test_gateway_history_keeps_sidecar_only_assistant_row():
assert [m["role"] for m in agent_history] == ["user", "assistant", "user"]
assert agent_history[1]["api_content"] == "The answer is 4."
assert agent_history[1]["reasoning"] == "The answer is 4."
+
+
+def test_replay_rebuilds_the_same_conversation_so_every_role_keeps_its_identity():
+ """Gateway replay turns the stored transcript back into the SAME session's history for its next turn:
+ user, assistant, tool-calling and tool rows all keep ``message_uid`` and the merge witness."""
+ from gateway.run import _build_gateway_agent_history
+
+ call = {"id": "call_1", "type": "function", "function": {"name": "t", "arguments": "{}"}}
+ history = [
+ {"role": "user", "content": "a\n\nb", "message_uid": "1" * 32, "_absorbed_message_uids": ["b" * 32],
+ "timestamp": 1_700_000_000.0},
+ {"role": "assistant", "content": "", "tool_calls": [call], "message_uid": "2" * 32},
+ {"role": "tool", "content": "r", "tool_call_id": "call_1", "message_uid": "3" * 32},
+ {"role": "assistant", "content": "done", "message_uid": "4" * 32, "_absorbed_message_uids": ["c" * 32]},
+ ]
+ replayed, _ = _build_gateway_agent_history(history)
+ assert [(m["role"], m.get("message_uid"), m.get("_absorbed_message_uids")) for m in replayed] == [
+ (m["role"], m["message_uid"], m.get("_absorbed_message_uids")) for m in history]
+
+
+def test_a_plain_replay_row_does_not_carry_a_tool_call_uid_map_without_its_calls():
+ """Repair can prune an assistant's unanswered tool_calls and leave its ``_tool_call_uids`` behind: the
+ replayed plain row keeps its own uid but not a map naming calls it no longer carries."""
+ from gateway.run import _build_gateway_agent_history
+
+ history = [
+ {"role": "user", "content": "q", "message_uid": "1" * 32},
+ {"role": "assistant", "content": "done", "message_uid": "2" * 32, "_tool_call_uids": {"call_1": "5" * 32}},
+ ]
+ replayed, _ = _build_gateway_agent_history(history)
+ assert replayed[1]["message_uid"] == "2" * 32
+ assert "_tool_call_uids" not in replayed[1] and "_tool_call_uid" not in replayed[1]