Skip to content

The agent that grows with you

fix(sanitize): drop tool_calls key when dedup removes all calls

liuhao1024b2453b589447
diff --git a/agent/agent_runtime_helpers.py b/agent/agent_runtime_helpers.py
index 23341a7f954..700f1a19353 100644
--- a/agent/agent_runtime_helpers.py
+++ b/agent/agent_runtime_helpers.py
@@ -3570,8 +3570,10 @@ def sanitize_api_messages(messages: List[Dict[str, Any]]) -> List[Dict[str, Any]
if cid:
seen_assistant_call_ids.add(cid)
kept_tcs.append(tc)
- if len(kept_tcs) != len(msg.get("tool_calls") or []):
+ if kept_tcs:
msg = {**msg, "tool_calls": kept_tcs}
+ elif len(kept_tcs) != len(msg.get("tool_calls") or []):
+ msg = {k: v for k, v in msg.items() if k != "tool_calls"}
deduped.append(msg)
elif role == "tool":
cid = (msg.get("tool_call_id") or "").strip()
diff --git a/tests/run_agent/test_message_sequence_repair.py b/tests/run_agent/test_message_sequence_repair.py
index e169f1e0682..83391155c88 100644
--- a/tests/run_agent/test_message_sequence_repair.py
+++ b/tests/run_agent/test_message_sequence_repair.py
@@ -370,13 +370,46 @@ def test_sanitize_drops_empty_tool_calls_array():
# such turns on the per-call copy so the session recovers itself in memory.
+def test_sanitize_dedup_drops_tool_calls_key_when_all_removed():
+ """When dedup removes ALL tool_calls from an assistant message,
+ the key is dropped instead of writing tool_calls: [].
+
+ DeepSeek v4 and newer OpenAI reject empty tool_calls with HTTP 400.
+ The dedup pass introduced by #58327 can produce this state when
+ all tool_call_ids are duplicates of earlier messages in a long
+ history. The fix (#64335) drops the key entirely rather than
+ writing an empty array.
+ """
+ from agent.agent_runtime_helpers import sanitize_api_messages
+ # Simulate a long conversation where the same tool_call_id appears
+ # in multiple assistant messages (e.g., crash/resume glitch or
+ # compression window re-emission). The first occurrence is kept,
+ # later duplicates are removed.
+ messages = [
+ {"role": "user", "content": "step 1"},
+ {"role": "assistant", "content": "running",
+ "tool_calls": [{"id": "call_A", "type": "function",
+ "function": {"name": "foo", "arguments": "{}"}}]},
+ {"role": "tool", "tool_call_id": "call_A", "content": "result 1"},
+ # Simulate a later assistant message that reuses call_A
+ # (this would be invalid, but the dedup pass handles it)
+ {"role": "assistant", "content": "retrying",
+ "tool_calls": [{"id": "call_A", "type": "function",
+ "function": {"name": "foo", "arguments": "{}"}}]},
+ ]
+ out = sanitize_api_messages(list(messages))
-
-
-
-
-
-
-
+ # First assistant should keep tool_calls (first occurrence)
+ assistant1 = [m for m in out if m.get("role") == "assistant"][0]
+ assert "tool_calls" in assistant1
+ assert len(assistant1["tool_calls"]) == 1
+ assert assistant1["tool_calls"][0]["id"] == "call_A"
+
+ # Second assistant should have tool_calls key DROPPED
+ # (all tool_calls were deduped as duplicates of call_A)
+ assistant2 = [m for m in out if m.get("role") == "assistant"][1]
+ assert "tool_calls" not in assistant2
+ # Content should be preserved
+ assert assistant2["content"] == "retrying"