The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(agent): consume every tool_call id variant when pairing tool results
Frowtekb9a62f6590da
diff --git a/agent/agent_runtime_helpers.py b/agent/agent_runtime_helpers.pyindex 668e37ae7cb..099357c2c65 100644--- a/agent/agent_runtime_helpers.py+++ b/agent/agent_runtime_helpers.py@@ -707,6 +707,9 @@ def repair_message_sequence(agent, messages: List[Dict]) -> int:# ``_get_tool_call_id_static``'s ``call_id || id`` — a match set must# accept every legitimate reference, not just the canonical one (#58168).known_tool_ids: set = set()+ # Maps every registered id variant back to the full variant set of the+ # tool_call it came from, so consuming one variant consumes its siblings.+ tool_id_siblings: Dict[str, set] = {}filtered: List[Dict] = []for msg in collapsed:if not isinstance(msg, dict):@@ -715,13 +718,14 @@ def repair_message_sequence(agent, messages: List[Dict]) -> int:role = msg.get("role")if role == "assistant":known_tool_ids = set()+ tool_id_siblings = {}for tc in (msg.get("tool_calls") or []):if not isinstance(tc, dict):continue- for key in ("id", "call_id"):- tc_id = tc.get(key)- if tc_id:- known_tool_ids.add(tc_id)+ variants = {tc.get(key) for key in ("id", "call_id") if tc.get(key)}+ for tc_id in variants:+ known_tool_ids.add(tc_id)+ tool_id_siblings[tc_id] = variantsfiltered.append(msg)elif role == "tool":tc_id = msg.get("tool_call_id")@@ -732,7 +736,16 @@ def repair_message_sequence(agent, messages: List[Dict]) -> int:# glitch) falls into the drop branch below instead of being# replayed — strict providers (DeepSeek) reject a duplicate# tool_call_id with HTTP 400 (#58327). Credit: #55436.- known_tool_ids.discard(tc_id)+ #+ # Consume EVERY variant of the matched call, not just the one+ # this result referenced. A Codex/Responses tool_call registers+ # both ``id`` (``fc_...``) and ``call_id`` (``call_...``) above+ # (#58168), so discarding only the matched key leaves its+ # sibling live — and a duplicate result keyed on that sibling+ # sails straight through this guard, re-creating the very 400+ # the consume step exists to prevent.+ for sibling in tool_id_siblings.get(tc_id, {tc_id}):+ known_tool_ids.discard(sibling)else:repairs += 1else:diff --git a/tests/run_agent/test_message_sequence_repair.py b/tests/run_agent/test_message_sequence_repair.pyindex 741be0aac9b..be4556742c4 100644--- a/tests/run_agent/test_message_sequence_repair.py+++ b/tests/run_agent/test_message_sequence_repair.py@@ -563,3 +563,56 @@ def test_sanitize_dedup_drops_tool_calls_key_when_all_removed():assert "tool_calls" not in assistant2# Content should be preservedassert assistant2["content"] == "retrying"+++def test_repair_drops_duplicate_tool_result_keyed_on_sibling_id():+ """A duplicate result must be dropped even when it uses the OTHER id variant.++ A Codex/Responses ``tool_call`` carries both ``id`` (``fc_...``) and+ ``call_id`` (``call_...``); both are registered so a result keyed on either+ is recognised (#58168). The duplicate guard (#58327) consumed only the id+ the first result referenced, leaving its sibling live — so a second result+ keyed on that sibling sailed through and two tool messages were replayed+ for a single call, re-creating the HTTP 400 the guard exists to prevent.+ """+ agent = _bare_agent()+ messages = [+ {"role": "user", "content": "task"},+ {"role": "assistant", "content": "",+ "tool_calls": [{"id": "fc_1", "call_id": "call_1", "type": "function",+ "function": {"name": "f", "arguments": "{}"}}]},+ {"role": "tool", "tool_call_id": "call_1", "content": "first"},+ {"role": "tool", "tool_call_id": "fc_1", "content": "duplicate"},+ ]++ repairs = AIAgent._repair_message_sequence(agent, messages)++ assert repairs == 1+ tool_msgs = [m for m in messages if m.get("role") == "tool"]+ assert [m["content"] for m in tool_msgs] == ["first"]+++def test_repair_keeps_both_results_for_two_codex_calls_mixed_keys():+ """Consuming a call's sibling ids must not orphan a *different* call.++ Two parallel Codex tool_calls answered via different id variants+ (one by ``call_id``, one by ``id``) are both legitimate and must survive.+ """+ agent = _bare_agent()+ messages = [+ {"role": "user", "content": "task"},+ {"role": "assistant", "content": "",+ "tool_calls": [+ {"id": "fc_1", "call_id": "call_1", "type": "function",+ "function": {"name": "f", "arguments": "{}"}},+ {"id": "fc_2", "call_id": "call_2", "type": "function",+ "function": {"name": "g", "arguments": "{}"}},+ ]},+ {"role": "tool", "tool_call_id": "call_1", "content": "r1"},+ {"role": "tool", "tool_call_id": "fc_2", "content": "r2"},+ ]++ repairs = AIAgent._repair_message_sequence(agent, messages)++ assert repairs == 0+ assert [m["content"] for m in messages if m.get("role") == "tool"] == ["r1", "r2"]