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.
Projecthongdangmoo49/Trinity
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
| 58 | 58 | return "" | |
| 59 | 59 | return self.path.read_text(encoding="utf-8") | |
| 60 | 60 | ||
| 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 | + | ||
| 61 | 75 | def write(self, content: str) -> None: | |
| 62 | 76 | """Overwrite the entire shared.md.""" | |
| 63 | 77 | 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") | |
| 65 | 80 | ||
| 66 | 81 | def read_section(self, section_name: str) -> str | None: | |
| 67 | 82 | """Read a specific ## section by name. Returns None if not found.""" |
The check that tells the two apart
fail→pass·tests/test_shared_context.py::TestSharedContextEngine::test_append_opinion_with_surrogates
fail→pass·tests/test_shared_context.py::TestSharedContextEngine::test_write_section_with_surrogates
fail→pass·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
- 2026-06-17fix: avoid missing workflow events traceback
- 2026-06-06fix: reuse target workspace for workflow followups
- 2026-06-05fix(tui): address code quality review for SacredGeometryAnimator
- 2026-06-03fix: make rotation/warning thresholds configurable instead of hardcoded
- 2026-06-03fix(security): prevent path traversal in ManagedHome read/write_config