Agent Zero AI framework
Fix compaction backups for malformed Unicode
Alessandro98e51fbb2a3f
diff --git a/plugins/_chat_compaction/AGENTS.md b/plugins/_chat_compaction/AGENTS.mdindex c1d7b418a..88a4f8d45 100644--- a/plugins/_chat_compaction/AGENTS.md+++ b/plugins/_chat_compaction/AGENTS.md@@ -16,6 +16,7 @@## Local Contracts- Preserve chat history integrity and persistence after compaction.+- Backup JSON and transcript artifacts must remain UTF-8 writable when chat content contains malformed Unicode such as lone surrogates.- Keep generated summaries bounded by configured model and token limits.- Do not discard original context data unless the compaction flow explicitly owns that behavior.diff --git a/plugins/_chat_compaction/helpers/compactor.py b/plugins/_chat_compaction/helpers/compactor.pyindex 8561dc620..b61cd7eed 100644--- a/plugins/_chat_compaction/helpers/compactor.py+++ b/plugins/_chat_compaction/helpers/compactor.py@@ -4,7 +4,7 @@ from collections import dequeimport models as models_modulefrom agent import Agent-from helpers import tokens+from helpers import files, tokensfrom helpers.history import History, output_textfrom helpers.persist_chat import (export_json_chat,@@ -42,11 +42,8 @@ def _save_pre_compaction_backup(context, full_text: str) -> dict[str, str]:txt_path = os.path.join(backup_dir, f"pre-compact-{timestamp}.txt")json_content = export_json_chat(context)- with open(json_path, "w", encoding="utf-8") as f:- f.write(json_content)-- with open(txt_path, "w", encoding="utf-8") as f:- f.write(full_text)+ files.write_file(json_path, json_content)+ files.write_file(txt_path, full_text)return {"json": json_path, "txt": txt_path}diff --git a/tests/test_chat_compaction.py b/tests/test_chat_compaction.pyindex 39c4169d7..fa8358c57 100644--- a/tests/test_chat_compaction.py+++ b/tests/test_chat_compaction.py@@ -1,4 +1,5 @@import sys+from types import SimpleNamespacefrom pathlib import Pathimport pytest@@ -42,6 +43,32 @@ class _RecordingModel:return f"summary-{len(self.user_messages)}", None+def test_pre_compaction_backup_sanitizes_surrogate_text(tmp_path, monkeypatch):+ monkeypatch.setattr(+ compactor,+ "get_chat_folder_path",+ lambda _ctxid: str(tmp_path),+ )+ monkeypatch.setattr(+ compactor,+ "export_json_chat",+ lambda _context: '{"content":"before\ud83dafter"}',+ )++ paths = compactor._save_pre_compaction_backup(+ SimpleNamespace(id="surrogate-chat"),+ "transcript before\ud83dafter",+ )++ json_backup = Path(paths["json"]).read_text(encoding="utf-8")+ text_backup = Path(paths["txt"]).read_text(encoding="utf-8")++ assert "\ud83d" not in json_backup+ assert "\ud83d" not in text_backup+ assert "before?after" in json_backup+ assert "before?after" in text_backup++def test_compaction_splitter_wraps_single_line_85k_payload(monkeypatch):monkeypatch.setattr(compactor.tokens, "approximate_tokens", lambda text: len(text or "")