Skip to content

The agent that grows with you

fix(agent): normalize provider-minted parallel tool ids

Yuan Lie631b6bd82a9
diff --git a/agent/message_sanitization.py b/agent/message_sanitization.py
index dbd2d75a6e6..5b5af1e2f74 100644
--- a/agent/message_sanitization.py
+++ b/agent/message_sanitization.py
@@ -455,7 +455,7 @@ __all__ = [
"strip_images_for_rejecting_model",
# call_id policy owners
"deterministic_call_id", "coalesce_tool_call_id", "tool_call_id_variants",
- "tool_result_id_variants", "uniquify_tool_call_ids",
+ "tool_result_id_variants", "uniquify_tool_call_ids", "normalize_provider_tool_call_ids",
# reasoning_content policy owners
"reasoning_echo_family", "matches_reasoning_echo_family", "needs_reasoning_echo",
"stale_thinking_reaches_wire", "apply_reasoning_content_policy", "reapply_reasoning_echo",
@@ -570,6 +570,31 @@ def uniquify_tool_call_ids(tool_calls: list) -> list:
return tool_calls
+_PROVIDER_TOOL_ID_PREFIXES = ("chatcmpl-tool-",)
+
+def normalize_provider_tool_call_ids(tool_calls: list) -> list:
+ """Rewrite known provider ids when a parallel batch would be rejected on replay.
+
+ The digest is deterministic so persisted messages and prompt-cache prefixes remain
+ stable. Composite Responses ids retain their response-item half.
+ """
+ if len(tool_calls or []) < 2:
+ return tool_calls
+ ids = [(_tc_field(tc, "call_id") or _tc_field(tc, "id") or "") for tc in tool_calls]
+ 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)
+ return tool_calls
+
+
# -- reasoning_content policy: single owner of strip-vs-re-pad; adapters keep only SYNTAX --
# Require side (echo-back enforced; replays 400 without the field): the families below. Kimi
# is host-driven on purpose (aggregators re-exporting kimi reject it); DeepSeek V4 rejects
diff --git a/agent/turn_tool_validation.py b/agent/turn_tool_validation.py
index 27d10e5e52c..6ec490606c6 100644
--- a/agent/turn_tool_validation.py
+++ b/agent/turn_tool_validation.py
@@ -15,7 +15,8 @@ from dataclasses import dataclass
from typing import Any, Dict, List, Optional
from agent.message_metadata import append_message
-from agent.message_sanitization import close_interrupted_tool_sequence, coalesce_tool_call_id
+from agent.message_sanitization import (close_interrupted_tool_sequence, coalesce_tool_call_id,
+ normalize_provider_tool_call_ids)
from agent.turn_failure_copy import site_copy, stamp_failure
from hermes_constants import FINISH_REASON_LENGTH
@@ -88,6 +89,7 @@ def validate_tool_calls(
# Uniquify duplicate tool-call ids BEFORE any downstream consumer: the
# pre-API sanitizer keeps only the first call/result per id.
agent._uniquify_tool_call_ids(tool_calls)
+ normalize_provider_tool_call_ids(tool_calls)
# Repair mismatched tool names before validating (model hallucinations).
repaired_ids = set()
diff --git a/tests/agent/test_message_sanitization_policy.py b/tests/agent/test_message_sanitization_policy.py
index 7548f038900..01149f2ddca 100644
--- a/tests/agent/test_message_sanitization_policy.py
+++ b/tests/agent/test_message_sanitization_policy.py
@@ -411,3 +411,27 @@ class TestPerProviderReasoningEcho:
# Flag should be restored from snapshot
assert agent._reasoning_echo_flag is True
assert agent.model == "glm-5.2"
+
+
+from agent.message_sanitization import normalize_provider_tool_call_ids
+
+def test_normalize_provider_parallel_ids_is_deterministic_and_preserves_composite():
+ calls = [
+ {"id": "chatcmpl-tool-alpha|item-a", "call_id": "chatcmpl-tool-alpha|item-a"},
+ {"id": "chatcmpl-tool-beta", "call_id": "chatcmpl-tool-beta"},
+ ]
+ normalize_provider_tool_call_ids(calls)
+ first = [c.copy() for c in calls]
+ normalize_provider_tool_call_ids(calls)
+ assert calls == first
+ assert calls[0]["id"].endswith("|item-a")
+ assert all(c["id"].startswith("call_") for c in calls)
+
+def test_normalize_provider_ids_leaves_single_and_mixed_batches_unchanged():
+ for calls in [
+ [{"id": "chatcmpl-tool-alpha"}],
+ [{"id": "chatcmpl-tool-alpha"}, {"id": "call_1"}],
+ ]:
+ before = [c.copy() for c in calls]
+ normalize_provider_tool_call_ids(calls)
+ assert calls == before