Repository navigation
fix(whatsapp): elide auto-reply text on a UTF-16 boundary - #96580
Conversation
elide truncated text with String.slice(0, limit) on a UTF-16 code-unit index, so an astral character straddling the limit was cut into a lone surrogate; the truncated-char count was also computed from the fixed limit rather than the actual kept length. Truncate with truncateUtf16Safe so a surrogate pair is never split, and derive the truncated-char count from the kept length so the annotation stays accurate. Adds tests asserting no lone surrogate when the limit lands inside an emoji and that a complete astral character is kept when it fits.
|
Codex review: needs maintainer review before merge. Reviewed June 26, 2026, 12:02 AM ET / 04:02 UTC. Summary PR surface: Source +2, Tests +23. Total +25 across 2 files. Reproducibility: yes. Current main uses a raw UTF-16 slice in WhatsApp elide, and a focused Node check with a limit inside an emoji reproduced a dangling high surrogate. Review metrics: none identified. Merge readiness Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch. Next step before merge
Security Review detailsBest possible solution: Land the focused WhatsApp elide fix after normal maintainer review. Do we have a high-confidence way to reproduce the issue? Yes. Current main uses a raw UTF-16 slice in WhatsApp elide, and a focused Node check with a limit inside an emoji reproduced a dangling high surrogate. Is this the best way to solve the issue? Yes. Reusing the existing exported truncateUtf16Safe helper is the narrow maintainable fix; a WhatsApp-local surrogate helper would duplicate a shared text contract already used by sibling channel truncation paths. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning high; reviewed against 0fdfc9f65ff4. Label changesLabel justifications:
Evidence reviewedPR surface: Source +2, Tests +23. Total +25 across 2 files. View PR surface stats
What I checked:
Likely related people:
What the crustacean ranks mean
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics. How this review workflow works
|
elide truncated text with String.slice(0, limit) on a UTF-16 code-unit index, so an astral character straddling the limit was cut into a lone surrogate; the truncated-char count was also computed from the fixed limit rather than the actual kept length. Truncate with truncateUtf16Safe so a surrogate pair is never split, and derive the truncated-char count from the kept length so the annotation stays accurate. Adds tests asserting no lone surrogate when the limit lands inside an emoji and that a complete astral character is kept when it fits.
elide truncated text with String.slice(0, limit) on a UTF-16 code-unit index, so an astral character straddling the limit was cut into a lone surrogate; the truncated-char count was also computed from the fixed limit rather than the actual kept length. Truncate with truncateUtf16Safe so a surrogate pair is never split, and derive the truncated-char count from the kept length so the annotation stays accurate. Adds tests asserting no lone surrogate when the limit lands inside an emoji and that a complete astral character is kept when it fits.
elide truncated text with String.slice(0, limit) on a UTF-16 code-unit index, so an astral character straddling the limit was cut into a lone surrogate; the truncated-char count was also computed from the fixed limit rather than the actual kept length. Truncate with truncateUtf16Safe so a surrogate pair is never split, and derive the truncated-char count from the kept length so the annotation stays accurate. Adds tests asserting no lone surrogate when the limit lands inside an emoji and that a complete astral character is kept when it fits. (cherry picked from commit cb8bc71)
Summary: WhatsApp auto-reply truncation used a raw UTF-16 slice, leaving a lone surrogate (broken glyph) whenever the length limit fell inside an emoji. This truncates on a code-point boundary instead, and recomputes the dropped-char count from the kept length.
What Problem This Solves
elide(text, limit)inextensions/whatsapp/src/auto-reply/util.tstruncates over-long text for WhatsApp auto-replies withtext.slice(0, limit). Because the cut is on the raw UTF-16 string, whenlimitlands in the middle of an astral character (emoji such as 🙂U+1F642, which is a UTF-16 surrogate pair) the truncated body ends with a lone high surrogate. The result is malformed text (a broken/replacement glyph) in the delivered auto-reply.Trigger:
elide("aaaaaaaaa🙂bbb…", 10)— UTF-16 index 10 falls between the emoji's high (0xD83D) and low (0xDE42) halves.Fix
Use the shared
truncateUtf16Safehelper (openclaw/plugin-sdk/text-utility-runtime) instead of a raw slice, so truncation always stops on a code-point boundary and never emits a lone surrogate. The truncated-char count in the annotation is recomputed from the actually-kept length (text.length - truncated.length) so it stays correct when a partial surrogate pair is dropped.Evidence
Ran the real
elidefunction from the PR branch in an isolated worktree (node --import tsx).BEFOREreproduces the oldtext.slice(0, limit);AFTERcalls the shippedelideand asserts the output is free of lone surrogates matching the vitest suite's lone-surrogate assertions.BEFORE: the truncated body ends with a lone high surrogate0xD83Dand the whole elided string fails the lone-surrogate check.AFTER(the real fix): the partial emoji is dropped cleanly, the body ends on a code-point boundary,full output lone-surrogate = false, and the annotation correctly reportstruncated 52 chars(recomputed from the kept length, vs. the stale51the old code would print).Tests
extensions/whatsapp/src/auto-reply/web-auto-reply-utils.test.ts(theelidedescribe block) adds vitest coverage that is red before the fix and green after:does not split surrogate pairs when the limit lands inside an emoji—elide("😀😀😀", 5)→"😀😀… (truncated 2 chars)"withhasLoneSurrogate(output) === false.elide("ab😀cd", 4)→"ab😀… (truncated 2 chars)", no lone surrogate).elide(undefined)→undefined, under-limit text returned unchanged, and plain ASCII truncationelide("abcdef", 3)→"abc… (truncated 3 chars)"(no regression).Each assertion checks the elided output with the suite's
hasLoneSurrogatehelper, which iterates code points and rejects any unpaired surrogate.