fix: preserve multi-byte UTF-8 across sidebar-agent stdout chunks#1007
Open
chappse6 wants to merge 2 commits intogarrytan:mainfrom
Open
fix: preserve multi-byte UTF-8 across sidebar-agent stdout chunks#1007chappse6 wants to merge 2 commits intogarrytan:mainfrom
chappse6 wants to merge 2 commits intogarrytan:mainfrom
Conversation
Korean, Japanese, Chinese, and emoji characters streamed from claude via
sidebar-agent were mojibake'd when a multi-byte UTF-8 code point landed
on a Buffer chunk boundary — e.g. Korean "합니다" rendered as "핣니다"
in the sidebar. Per-chunk Buffer.toString('utf8') replaces partial
sequences with U+FFFD and the next chunk starts mid-sequence, corrupting
both chunks. ASCII-only streams are unaffected (1 byte = 1 code point).
Route proc.stdout and proc.stderr through a small StringDecoder wrapper
that buffers partial code units across chunks, and flush on close.
Adds a unit test that splits Korean, Japanese, Chinese, 4-byte emoji,
and mixed text at every possible byte offset and verifies the decoded
string round-trips the original.
Address review feedback: - Extend utf8-stream-decoder.ts JSDoc to explain why the thin wrapper exists (unit-testable contract vs. inline StringDecoder usage). - Annotate the `end()` flush on process close so the intent is obvious without grepping StringDecoder's docs. No behavior change.
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.
Summary
The sidebar-agent streams
claude -pstdout back to the Chrome extension. When claude emitted non-ASCII text — Korean, Japanese, Chinese, emoji, etc. — characters near chunk boundaries were mojibake'd in the sidebar. For example, Korean "합니다" would render as "핣니다".Root cause
browse/src/sidebar-agent.tsdecoded each stdout/stderr chunk independently:A Hangul syllable is 3 bytes in UTF-8; emoji are 4. If an OS pipe flush happens to split a multi-byte sequence across two
dataevents,Buffer.toString('utf8')replaces the trailing partial bytes of chunk N with U+FFFD and the next chunk begins mid-sequence — corrupting both. The symptom is probabilistic, tied to pipe/stream-json flush timing, which is why it showed up for longer responses with dense Hangul.ASCII-only output is unaffected (1 byte = 1 code point, no boundary can fall inside a character).
Fix
Route stdout/stderr through Node's built-in
StringDecoder, which buffers trailing partial code units across chunks and only emits fully decoded text. Flush any remainder on process close.browse/src/utf8-stream-decoder.ts(thinStringDecoderwrapper, isolated so it's unit-testable)sidebar-agent.tsuses one decoder instance per stream per child processstring_decoderis a Node built-in)Test plan
bun test browse/test/utf8-stream-decoder.test.ts— 7 pass, 424 expectsbun testsuite — same 5 pre-existing failures asmainbaseline (golden-file ship skills, VERSION mismatch, uninstall mock layout); no new failures introducedgrep "stdout.on('data'" browse/src/—sidebar-agent.tsis the only production site using this patternImpact