Skip to content

fix(chunk): keep emoji whole when length-splitting a long line in chunkByNewline#96415

Closed
ly-wang19 wants to merge 1 commit into
openclaw:mainfrom
ly-wang19:fix/chunk-newline-surrogate-safe
Closed

fix(chunk): keep emoji whole when length-splitting a long line in chunkByNewline#96415
ly-wang19 wants to merge 1 commit into
openclaw:mainfrom
ly-wang19:fix/chunk-newline-surrogate-safe

Conversation

@ly-wang19

Copy link
Copy Markdown
Contributor

What Problem This Solves

chunkByNewline in src/auto-reply/chunk.ts length-splits a long line with a
raw lineValue.slice(0, firstLimit). When firstLimit lands on an odd UTF-16
offset, 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 path
was 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:

chunkByNewline("😀😀😀😀😀😀😀😀", 3)
  • Before (wrong): first chunk = lineValue.slice(0, 3) = "😀\uD83D" — one
    full 😀 followed by a lone high surrogate; the next chunk starts with the
    orphaned low surrogate \uDE00, so the second 😀 is split and renders broken.
  • After (fixed): the boundary is pulled back to a code-point boundary, so the
    first chunk is a single whole "😀" and the rest is carried into the next
    chunk — no lone surrogate is ever emitted.

Fix

Apply avoidTrailingHighSurrogateBreak(lineValue, 0, firstLimit) before slicing,
mirroring the guard already used by chunkText / chunkMarkdownText. Only the
rare 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 avoidTrailingHighSurrogateBreak
extracted verbatim from src/shared/text-chunking.ts, plus a disk-content check
asserting the fix is actually written to source:

PASS: fix present in worktree source on disk
PASS: repro = 16 UTF-16 units
PASS: expectedOutput: first chunk = single whole 😀
PASS: first chunk does NOT end on lone high surrogate
PASS: remaining does NOT start on lone low surrogate
PASS: lossless: first + remaining == input
PASS: UNAFFECTED ascii: matches naive slice(0,20)
PASS: UNAFFECTED even boundary (limit 4): two whole emoji

ALL CHECKS PASSED

The buggy raw slice produces "😀\uD83D" (lone high surrogate) for the repro;
the fixed slice produces a single whole "😀". Plain ASCII and even-offset
boundaries are byte-identical before and after, confirming the change is scoped to
the mid-surrogate case only.

🤖 Generated with Claude Code

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]>
@openclaw-barnacle openclaw-barnacle Bot added size: XS r: too-many-prs Auto-close: author has more than twenty active PRs. labels Jun 24, 2026
@openclaw-barnacle

Copy link
Copy Markdown

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

r: too-many-prs Auto-close: author has more than twenty active PRs. size: XS

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant