The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(agent): run the send path's surrogate pass on max-iteration summary messages
kshitijk4poor372ce345b34e
diff --git a/agent/chat_completion_helpers.py b/agent/chat_completion_helpers.pyindex 73ebabbea8e..76183eff729 100644--- a/agent/chat_completion_helpers.py+++ b/agent/chat_completion_helpers.py@@ -43,8 +43,8 @@ from agent.model_metadata import is_local_endpointfrom agent.message_content import flatten_message_textfrom agent.message_metadata import PERSISTENCE_ONLY_MESSAGE_FIELDS, append_message, stamp_message_timestampfrom agent.message_sanitization import (- _sanitize_surrogates, _repair_tool_call_arguments, normalize_finish_reason as _normalize_finish_reason,- sanitize_outbound_kwargs, strip_images_for_rejecting_model,+ _sanitize_messages_surrogates, _sanitize_surrogates, _repair_tool_call_arguments,+ normalize_finish_reason as _normalize_finish_reason, sanitize_outbound_kwargs, strip_images_for_rejecting_model,)from agent.reasoning_summaries import append_streamed_reasoning_detail, separate_glued_reasoning_blocksfrom agent.repetition_guard import is_repetition_dominated@@ -2273,8 +2273,13 @@ def _iteration_summary_api_messages(agent, messages: list) -> list:for api_msg in api_messages:if isinstance(api_msg.get("content"), str):api_msg["content"] = api_msg["content"].strip()- from agent.conversation_loop import _canonicalize_api_tool_calls+ from agent.conversation_loop import _canonicalize_api_tool_calls, _clone_message_for_send_canonicalize_api_tool_calls(api_messages)+ # Third closing pass of the main path: lone surrogates -> U+FFFD (else the SDK's utf-8+ # wire encode raises and burns the summary retries). The sanitizer is in-place and these+ # rows still share nested dicts with history, so clone first like the main path does.+ api_messages = [_clone_message_for_send(m) for m in api_messages]+ _sanitize_messages_surrogates(api_messages)return api_messagesdiff --git a/tests/agent/test_iteration_summary_reasoning_details.py b/tests/agent/test_iteration_summary_reasoning_details.pyindex 53a63cfadbb..611fc208655 100644--- a/tests/agent/test_iteration_summary_reasoning_details.py+++ b/tests/agent/test_iteration_summary_reasoning_details.py@@ -76,3 +76,27 @@ class TestSummaryPrefixParity:assert by_role["user"]["content"] == "q"assert by_role["tool"]["content"] == "r"assert history == snapshot # send-path rewrites never reach the transcript++ def test_strips_lone_surrogates_like_the_send_path(self, make_agent):+ agent = make_agent("https://api.groq.com/openai/v1", "custom")+ history = [+ {"role": "user", "content": "clip \ud800 paste"},+ {"role": "assistant", "content": "ok \ud83d", "tool_calls": [+ {"id": "t1", "type": "function",+ "function": {"name": "f", "arguments": '{"k": "v\ud800"}'}},+ ]},+ {"role": "tool", "tool_call_id": "t1", "content": "r"},+ ]+ out = _iteration_summary_api_messages(agent, history)+ # Main path's third pass rewrites lone surrogates to U+FFFD; anything else diverges the+ # prefix or makes the SDK's ensure_ascii=False utf-8 encode raise.+ assert next(m for m in out if m["role"] == "user")["content"] == "clip \ufffd paste"+ assistant = next(m for m in out if m["role"] == "assistant")+ assert assistant["content"] == "ok \ufffd"+ # Canonicalization runs first, so argument surrogates are already ASCII \udXXX escapes.+ assert assistant["tool_calls"][0]["function"]["arguments"] == '{"k":"v\\ud800"}'+ json.dumps(out, ensure_ascii=False).encode("utf-8")+ # The sanitizer is in-place: history must keep its stored bytes.+ assert history[0]["content"] == "clip \ud800 paste"+ assert history[1]["content"] == "ok \ud83d"+ assert history[1]["tool_calls"][0]["function"]["arguments"] == '{"k": "v\ud800"}'