Skip to content

The agent that grows with you

fix(compression): make clarify summaries UTF-8 safe

Crypto Intern6433d5723f8d
diff --git a/agent/context_compressor.py b/agent/context_compressor.py
index 4c7e28b4024..e967d978f0d 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 ec702c6fffc..76a0afbd55f 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",
[