From 6433d5723f8df44ca44097f0f0904d076d18b4b4 Mon Sep 17 00:00:00 2001 From: Crypto Intern <167513440+asdasdadhj@users.noreply.github.com> Date: Fri, 7 Aug 2026 17:44:07 +0000 Subject: [PATCH] fix(compression): make clarify summaries UTF-8 safe --- agent/context_compressor.py | 9 ++++++++- tests/agent/test_context_compressor.py | 9 +++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/agent/context_compressor.py b/agent/context_compressor.py index 4c7e28b4024e0..e967d978f0df4 100644 --- a/agent/context_compressor.py +++ b/agent/context_compressor.py @@ -1342,7 +1342,14 @@ def _summarize_tool_result_unguarded(tool_name: str, tool_args: str, tool_conten and all(isinstance(item, str) and item for item in response) ) if resolved: - summary = response_prefix + json.dumps(response, ensure_ascii=False) + # Keep ordinary Unicode intact while escaping lone UTF-16 + # surrogates so the compacted message remains UTF-8/SQLite safe. + serialized_response = ( + json.dumps(response, ensure_ascii=False) + .encode("utf-8", errors="backslashreplace") + .decode("utf-8") + ) + summary = response_prefix + serialized_response if len(summary) > max_summary_chars: summary = ( summary[: max_summary_chars - len(truncation_marker)].rstrip() diff --git a/tests/agent/test_context_compressor.py b/tests/agent/test_context_compressor.py index ec702c6fffc37..76a0afbd55f20 100644 --- a/tests/agent/test_context_compressor.py +++ b/tests/agent/test_context_compressor.py @@ -99,6 +99,15 @@ class TestSummarizeToolResultClarify: assert first_summary.endswith("...[truncated]") assert second_summary == first_summary + def test_unpaired_surrogates_are_safe_for_utf8_persistence(self): + content = json.dumps({"user_response": "\ud83d" * 1_000}) + + summary = _summarize_tool_result("clarify", "{}", content) + + assert len(summary) <= 200 + assert summary.encode("utf-8") + assert "\\ud83d" in summary + @pytest.mark.parametrize( "content", [