Skip to content

The agent that grows with you

fix(agent): clone the /refine snapshot too, not just the automatic review

kshitijk4poor26f0de23cfd0
diff --git a/gateway/slash_commands.py b/gateway/slash_commands.py
index 5493e849dcb..092feb696d5 100644
--- a/gateway/slash_commands.py
+++ b/gateway/slash_commands.py
@@ -3019,7 +3019,12 @@ class GatewaySlashCommandsMixin:
if agent is None:
return "Nothing to refine yet — send a message first."
- snapshot = list(getattr(agent, "_session_messages", None) or [])
+ # Structural clone; see _clone_background_review_messages (#100795).
+ from agent.turn_finalizer import _clone_background_review_messages
+
+ snapshot = _clone_background_review_messages(
+ getattr(agent, "_session_messages", None) or []
+ )
if not snapshot:
return "Nothing to refine yet — the conversation is empty."
diff --git a/hermes_cli/cli_commands_mixin.py b/hermes_cli/cli_commands_mixin.py
index 7fe57373193..df82c2100c8 100644
--- a/hermes_cli/cli_commands_mixin.py
+++ b/hermes_cli/cli_commands_mixin.py
@@ -3007,7 +3007,12 @@ class CLICommandsMixin:
_cprint(f" {_DIM}Nothing to refine yet — send a message first.{_RST}")
return
- snapshot = list(getattr(self, "conversation_history", None) or [])
+ # Structural clone; see _clone_background_review_messages (#100795).
+ from agent.turn_finalizer import _clone_background_review_messages
+
+ snapshot = _clone_background_review_messages(
+ getattr(self, "conversation_history", None) or []
+ )
if not snapshot:
_cprint(f" {_DIM}Nothing to refine yet — the conversation is empty.{_RST}")
return
diff --git a/tests/agent/test_refine_snapshot_isolation.py b/tests/agent/test_refine_snapshot_isolation.py
new file mode 100644
index 00000000000..c1abfa9d26b
--- /dev/null
+++ b/tests/agent/test_refine_snapshot_isolation.py
@@ -0,0 +1,87 @@
+"""/refine hands the review fork a snapshot that cannot alias the live transcript.
+
+The automatic post-turn review already clones structurally
+(``_clone_background_review_messages``); the two explicit ``/refine`` entry
+points (CLI mixin + gateway slash command) build their own snapshot and must
+use the same clone — a shallow ``list()`` shares the nested ``tool_calls`` /
+``content`` containers with the persisted history, so the fork's in-place
+transcript sanitization would rewrite the parent's messages (#100795).
+"""
+
+import threading
+from unittest.mock import MagicMock
+
+import pytest
+
+
+def _nested_history():
+ return [
+ {"role": "user", "content": [{"type": "text", "text": "ask"}]},
+ {
+ "role": "assistant",
+ "content": "ok",
+ "tool_calls": [{
+ "id": "call-1",
+ "function": {"name": "read_file", "arguments": '{"path":"x"}'},
+ }],
+ },
+ ]
+
+
+def _assert_isolated(live, snapshot):
+ assert snapshot == live # same shape/bytes …
+ assert snapshot is not live
+ for live_msg, snap_msg in zip(live, snapshot):
+ assert snap_msg is not live_msg # … but no shared containers
+ for key in ("content", "tool_calls"):
+ if isinstance(live_msg.get(key), (dict, list)):
+ assert snap_msg[key] is not live_msg[key]
+ # Mutating the snapshot the way the fork's sanitizers do must not leak.
+ snapshot[0]["content"][0]["text"] = "mutated"
+ snapshot[1]["tool_calls"][0]["function"]["arguments"] = "{}"
+ assert live[0]["content"][0]["text"] == "ask"
+ assert live[1]["tool_calls"][0]["function"]["arguments"] == '{"path":"x"}'
+
+
+def test_cli_refine_snapshot_does_not_alias_live_history(monkeypatch):
+ from hermes_cli.cli_commands_mixin import CLICommandsMixin
+
+ monkeypatch.setattr("cli._cprint", lambda *a, **k: None, raising=False)
+ agent = MagicMock()
+ agent.valid_tool_names = {"memory"}
+ cli = object.__new__(CLICommandsMixin)
+ cli.agent = agent
+ cli.conversation_history = _nested_history()
+
+ cli._handle_refine_command("/refine")
+
+ agent._spawn_background_review.assert_called_once()
+ snapshot = agent._spawn_background_review.call_args.kwargs["messages_snapshot"]
+ _assert_isolated(cli.conversation_history, snapshot)
+
+
+async def test_gateway_refine_snapshot_does_not_alias_live_history():
+ from gateway.run import GatewayRunner
+
+ key = "agent:main:test:dm:1"
+ agent = MagicMock()
+ agent.valid_tool_names = {"memory"}
+ agent._session_messages = _nested_history()
+
+ runner = object.__new__(GatewayRunner)
+ runner._running_agents = {}
+ runner._agent_cache = {key: agent}
+ runner._agent_cache_lock = threading.Lock()
+ runner._session_key_for_source = lambda source: key
+
+ event = MagicMock()
+ event.source = object()
+ event.get_command_args.return_value = ""
+
+ out = await runner._handle_refine_command(event)
+
+ assert out.startswith("⚗")
+ agent._spawn_background_review.assert_called_once()
+ snapshot = agent._spawn_background_review.call_args.kwargs["messages_snapshot"]
+ _assert_isolated(agent._session_messages, snapshot)