fix(parser): sanitize lone UTF-16 surrogates before JSONL parsing (fixes #85)#88
Merged
Conversation
JSONL files written by Claude Code before v2.1.132 may contain lone UTF-16 surrogate code units (e.g. `\uD83D` without a matching low surrogate) when the tool-error truncation logic split a multi-byte emoji at an offset boundary. serde_json rejects lone surrogates per RFC 8259, causing parse_entry to silently discard those lines. Add sanitize_lone_surrogates() which scans the raw JSONL string for `\uXXXX` escape sequences in the surrogate range (U+D800-U+DFFF) and replaces lone surrogates with `�` before the JSON deserializer sees them. Valid surrogate pairs (\uD8xx followed immediately by \uDCxx) are preserved unchanged. Allocation is deferred: strings with no surrogates return Cow::Borrowed with zero copies. Update parse_entry to convert the input bytes to str (failing fast on non-UTF-8) and apply the sanitizer before serde_json::from_str. Closes #85
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
JSONL files written by Claude Code before v2.1.132 may contain lone UTF-16 surrogate code units (e.g.
\uD83Dwithout a matching\uDCxxlow surrogate) insidetool_resultcontent block strings. This happens when Claude Code's tool-error truncation logic split a multi-byte emoji at an offset boundary.serde_jsonrejects lone surrogates per RFC 8259, causingparse_entryto silently discard any JSONL line that contains one.Fix
Add a
sanitize_lone_surrogates()pre-pass that runs on the raw JSONL text before handing it toserde_json::from_str. It scans for\uXXXXescape sequences in the surrogate range (U+D800–U+DFFF) and:\uD8xxnot followed by\uDCxx–\uDFxx): replaced with�\uDCxx–\uDFxxnot preceded by a valid high surrogate): replaced with�\uD8xx\uDCxx): preserved as-isAllocation is deferred: strings containing no surrogates return
Cow::Borrowedwith zero copies.parse_entryis updated to convert the raw bytes to&strfirst (failing fast on non-UTF-8, whichserde_json::from_slicewould also reject) and then apply the sanitizer before deserialization.Files Changed
src-tauri/src/parser/entry.rs— addedhex4_to_u16,sanitize_lone_surrogates, updatedparse_entryTests Added
10 new tests in
parser::entry::tests:sanitize_lone_surrogates_no_surrogates_returns_borrowedsanitize_lone_high_surrogate_replaced_with_fffd\uD83D→�sanitize_lone_low_surrogate_replaced_with_fffd\uDC36→�sanitize_valid_surrogate_pair_unchangedsanitize_multiple_lone_surrogates_all_replacedsanitize_high_surrogate_at_end_of_string_replacedparse_entry_with_lone_high_surrogate_succeedsNoneparse_entry_with_lone_low_surrogate_succeedsparse_entry_with_valid_surrogate_pair_succeedsAll 390 Rust tests and 352 frontend tests pass.
Fixes #85