Skip to content

The agent that grows with you

fix(agent): keep response-item halves and lone surrogates intact in mint-time id normalization

unsupportedpastels5136d31952a0
diff --git a/agent/message_sanitization.py b/agent/message_sanitization.py
index 5b5af1e2f74..853fed61483 100644
--- a/agent/message_sanitization.py
+++ b/agent/message_sanitization.py
@@ -584,14 +584,20 @@ def normalize_provider_tool_call_ids(tool_calls: list) -> list:
if not all(isinstance(raw, str) and raw.split("|", 1)[0].startswith(_PROVIDER_TOOL_ID_PREFIXES) for raw in ids):
return tool_calls
logger.warning("Normalized provider-minted parallel tool-call ids for replay compatibility")
- for tc, raw in zip(tool_calls, ids):
- primary, *rest = raw.split("|", 1)
- replacement = "call_" + hashlib.sha256(primary.encode("utf-8")).hexdigest()[:12]
- value = replacement + ("|" + rest[0] if rest else "")
- if _tc_field(tc, "id") is not None:
- _tc_set(tc, "id", value)
- if _tc_field(tc, "call_id") is not None:
- _tc_set(tc, "call_id", value)
+ for tc in tool_calls:
+ # Rewrite each field's call half separately: ``id`` may carry the response-item
+ # half while ``call_id`` is bare, and that half must survive.
+ for key in ("id", "call_id"):
+ value = _tc_field(tc, key)
+ if not isinstance(value, str):
+ continue
+ primary, sep, item = value.partition("|")
+ if not primary.startswith(_PROVIDER_TOOL_ID_PREFIXES):
+ continue
+ # surrogatepass: provider JSON can carry lone surrogates; strict utf-8 would raise,
+ # and errors=replace would collapse distinct ids onto one digest.
+ digest = hashlib.sha256(primary.encode("utf-8", "surrogatepass")).hexdigest()[:12]
+ _tc_set(tc, key, f"call_{digest}{sep}{item}")
return tool_calls
diff --git a/tests/agent/test_provider_id_mint_normalization.py b/tests/agent/test_provider_id_mint_normalization.py
new file mode 100644
index 00000000000..3c6f4a160a8
--- /dev/null
+++ b/tests/agent/test_provider_id_mint_normalization.py
@@ -0,0 +1,90 @@
+"""Provider-minted parallel tool-call ids are normalized once, at mint time (#130363)."""
+
+from __future__ import annotations
+
+from types import SimpleNamespace
+
+import pytest
+
+from agent.chat_completion_helpers import _assistant_tool_call_dict
+from run_agent import AIAgent
+
+
+def _tool_call(raw_id, n, **extra):
+ return SimpleNamespace(
+ id=raw_id, type="function",
+ function=SimpleNamespace(name="read_file", arguments='{"path": "%s"}' % n), **extra,
+ )
+
+
+def _agent():
+ # Real AIAgent methods (duplicate repair, assistant serialization), no init side effects.
+ agent = AIAgent.__new__(AIAgent)
+ agent.provider, agent.model, agent.session_id, agent.tools = "nous", "m", "s1", []
+ agent.valid_tool_names = {"read_file"}
+ agent.log_prefix = ""
+ agent._invalid_tool_retries = agent._invalid_json_retries = 0
+ return agent
+
+
+def _mint(tool_calls):
+ """Run the real mint path and return the rows that would be stored and replayed."""
+ from agent.turn_tool_validation import validate_tool_calls
+
+ agent = _agent()
+ verdict = validate_tool_calls(
+ agent, SimpleNamespace(content="", tool_calls=tool_calls), "tool_calls",
+ messages=[], conversation_history=[], api_call_count=1, effective_task_id="t1",
+ )
+ assert verdict.action == "ok"
+ stored = [_assistant_tool_call_dict(agent, tc, i) for i, tc in enumerate(tool_calls)]
+ # Tool results take their id from the same object, so call and result agree.
+ assert [row["id"] for row in stored] == [AIAgent._get_tool_call_id_static(tc) for tc in tool_calls]
+ return stored
+
+
[email protected](autouse=True)
+def _no_metrics(monkeypatch):
+ import hermes_cli.observability.shared_metrics_model as metrics
+
+ monkeypatch.setattr(metrics, "record_tool_call_quality", lambda *a, **k: None)
+
+
+def test_parallel_provider_ids_are_rewritten_stably():
+ raw = ["chatcmpl-tool-aaa", "chatcmpl-tool-bbb"]
+ ids = [row["id"] for row in _mint([_tool_call(i, n) for n, i in enumerate(raw)])]
+
+ assert all(i.startswith("call_") for i in ids)
+ assert len(set(ids)) == 2
+ # Byte-stable across a re-mint of the same turn.
+ assert [row["id"] for row in _mint([_tool_call(i, n) for n, i in enumerate(raw)])] == ids
+
+
+ ["chatcmpl-tool-aaa"],
+ ["chatcmpl-tool-aaa", "call_1"],
+ ["call_0", "call_1"],
+])
+def test_single_mixed_and_ordinary_batches_are_untouched(raw):
+ assert [row["id"] for row in _mint([_tool_call(i, n) for n, i in enumerate(raw)])] == raw
+
+
[email protected]("keys", [("a", "b"), ("a", "a")], ids=["distinct", "duplicate"])
+def test_response_item_half_survives_when_only_id_is_composite(keys):
+ calls = [
+ _tool_call(f"chatcmpl-tool-{k}|fc_original_{n}", n, call_id=f"chatcmpl-tool-{k}")
+ for n, k in enumerate(keys)
+ ]
+ stored = _mint(calls)
+
+ assert [row["response_item_id"] for row in stored] == ["fc_original_0", "fc_original_1"]
+ assert all(row["id"].startswith("call_") for row in stored)
+ assert len({row["id"] for row in stored}) == 2
+
+
+def test_unencodable_provider_ids_do_not_crash_and_stay_distinct():
+ raw = ["chatcmpl-tool-\ud800", "chatcmpl-tool-?"]
+ ids = [row["id"] for row in _mint([_tool_call(i, n) for n, i in enumerate(raw)])]
+
+ assert all(i.startswith("call_") for i in ids)
+ assert len(set(ids)) == 2