fix(chunk): keep emoji whole when length-splitting a long line in chunkByNewline#96415
Closed
ly-wang19 wants to merge 1 commit into
Closed
fix(chunk): keep emoji whole when length-splitting a long line in chunkByNewline#96415ly-wang19 wants to merge 1 commit into
ly-wang19 wants to merge 1 commit into
Conversation
chunkByNewline length-splits a long line with a raw `lineValue.slice(0, firstLimit)`, which can cut through a surrogate pair when firstLimit lands on an odd UTF-16 offset. The first chunk then ends on a lone high surrogate and the next chunk begins with the orphaned low surrogate, rendering a broken/replacement glyph. Guard the boundary with avoidTrailingHighSurrogateBreak (already used by chunkText/chunkMarkdownText in this file and already imported), so the split lands on a code-point boundary. Only the rare mid-surrogate boundary changes; all other inputs are byte-identical. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Closing this PR because the author has more than 20 active PRs in this repo. Please reduce the active PR queue and reopen or resubmit once it is back under the limit. You can close your own PRs to get back under the limit. |
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.
What Problem This Solves
chunkByNewlineinsrc/auto-reply/chunk.tslength-splits a long line with araw
lineValue.slice(0, firstLimit). WhenfirstLimitlands on an odd UTF-16offset, the slice cuts through a surrogate pair: the first chunk ends on a
lone high surrogate and the next chunk begins with the orphaned low surrogate.
The astral character (emoji, CJK extension, etc.) is split across the boundary
and renders as a broken / replacement glyph.
Every other length-splitter in this file (
chunkText,chunkMarkdownText)already guards this boundary with
avoidTrailingHighSurrogateBreak. This pathwas the lone exception, even though the helper is already imported at the top of
the file.
Repro: a single 8-emoji line (16 UTF-16 units),
maxLineLength = 3:lineValue.slice(0, 3)="😀\uD83D"— onefull 😀 followed by a lone high surrogate; the next chunk starts with the
orphaned low surrogate
\uDE00, so the second 😀 is split and renders broken.first chunk is a single whole
"😀"and the rest is carried into the nextchunk — no lone surrogate is ever emitted.
Fix
Apply
avoidTrailingHighSurrogateBreak(lineValue, 0, firstLimit)before slicing,mirroring the guard already used by
chunkText/chunkMarkdownText. Only therare mid-surrogate boundary changes; every other input is byte-identical (verified
below). A regression test is added to
chunk.test.ts.Evidence
Standalone node red-green proof using the real
avoidTrailingHighSurrogateBreakextracted verbatim from
src/shared/text-chunking.ts, plus a disk-content checkasserting the fix is actually written to source:
The buggy raw slice produces
"😀\uD83D"(lone high surrogate) for the repro;the fixed slice produces a single whole
"😀". Plain ASCII and even-offsetboundaries are byte-identical before and after, confirming the change is scoped to
the mid-surrogate case only.
🤖 Generated with Claude Code