Whole file

hongdangmoo49/Trinity

The author described this change as fix: sanitize surrogate characters in SharedContextEngine.write(). It counts as a record because the checks below fail on the code as it stood at 63073a4c4 and pass on c94df0954, with nothing else changed between the two runs.

Fix saved2026-06-02
Sharing licenceMIT · LICENSE
Change size+16 1

What the code was meant to do, written into the code itself as a save note

fix: sanitize surrogate characters in SharedContextEngine.write()

The change

5858 return ""
5959 return self.path.read_text(encoding="utf-8")
6060
61+ @staticmethod
62+ def _sanitize(text: str) -> str:
63+ """Remove surrogate characters that can arise from tmux/terminal input.
64+
65+ When Python reads bytes with the 'surrogateescape' error handler
66+ (the default for os.fsdecode and some terminal I/O), invalid UTF-8
67+ bytes become surrogate code points (U+D800–U+DFFF). These cannot be
68+ re-encoded as UTF-8, causing UnicodeEncodeError on write.
69+
70+ Round-tripping through encode/decode with 'replace' swaps each
71+ unencodable character for '?' (encode) then cleanly decodes back.
72+ """
73+ return text.encode("utf-8", errors="replace").decode("utf-8")
74+
6175 def write(self, content: str) -> None:
6276 """Overwrite the entire shared.md."""
6377 self.path.parent.mkdir(parents=True, exist_ok=True)
64- self.path.write_text(content, encoding="utf-8")
78+ sanitized = self._sanitize(content)
79+ self.path.write_text(sanitized, encoding="utf-8")
6580
6681 def read_section(self, section_name: str) -> str | None:
6782 """Read a specific ## section by name. Returns None if not found."""

The check that tells the two apart

failpass·tests/test_shared_context.py::TestSharedContextEngine::test_append_opinion_with_surrogates
failpass·tests/test_shared_context.py::TestSharedContextEngine::test_write_section_with_surrogates
failpass·tests/test_shared_context.py::TestSharedContextEngine::test_write_surrogate_characters

Check file tests/test_shared_context.py, taken without changes from the fix and copied onto the older code, so the exact same check runs against both versions.

Origin and history

The code before it63073a4c4c64663b9fe0c51dac9e7d57b5b0bff1
Broken version dated2026-06-02
Moduletrinity.context.shared
Units changedSharedContextEngine
Fingerprint53505d3cc82c06be
Checked2026-08-18 by goldset/0.1

Every field above is generated by our program. None of it is written by hand.

Other bugs found in hongdangmoo49/Trinity