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

This commit is contained in:
Crypto Intern 2026-08-07 17:44:07 +00:00 committed by kshitij
parent d6511aecb6
commit 6433d5723f
2 changed files with 17 additions and 1 deletions

View File

@ -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()

View File

@ -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",
[