Skip to content

fix(whatsapp): elide auto-reply text on a UTF-16 boundary - #96580

Merged
vincentkoc merged 1 commit into
openclaw:mainfrom
WeeLi-009:fix/whatsapp-elide-surrogate
Jun 27, 2026
Merged

vincentkoc merged 1 commit into
openclaw:mainfrom
WeeLi-009:fix/whatsapp-elide-surrogate

Conversation

@WeeLi-009

@WeeLi-009 WeeLi-009 commented Jun 24, 2026 •

Copy link
Copy Markdown
Contributor

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) in extensions/whatsapp/src/auto-reply/util.ts truncates over-long text for WhatsApp auto-replies with text.slice(0, limit). Because the cut is on the raw UTF-16 string, when limit lands 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 truncateUtf16Safe helper (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 elide function from the PR branch in an isolated worktree (node --import tsx). BEFORE reproduces the old text.slice(0, limit); AFTER calls the shipped elide and asserts the output is free of lone surrogates matching the vitest suite's lone-surrogate assertions.

$ node --import tsx demo.mts

=== INPUT ===
text.length : 61 (UTF-16 code units)
limit       : 10
emoji        : 🙂 U+1F642 at units 9..10 -> high 0xD83D low 0xDE42

=== BEFORE (raw text.slice(0, limit)) ===
truncated body          : "aaaaaaaaa\ud83d"
body last code unit     : 0xD83D
body lone-surrogate     : true
full elide output        : "aaaaaaaaa\ud83d… (truncated 51 chars)"
full output lone-surrogate: true

=== AFTER (real elide output) ===
full elide output        : "aaaaaaaaa… (truncated 52 chars)"
truncated body          : "aaaaaaaaa"
body lone-surrogate     : false
full output lone-surrogate: false
emoji dropped cleanly (not torn): true
body ends on code-point boundary: true

BEFORE: the truncated body ends with a lone high surrogate 0xD83D and 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 reports truncated 52 chars (recomputed from the kept length, vs. the stale 51 the old code would print).

Tests

extensions/whatsapp/src/auto-reply/web-auto-reply-utils.test.ts (the elide describe 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)" with hasLoneSurrogate(output) === false.
  • limit landing inside a mid-string emoji (elide("ab😀cd", 4) → "ab😀… (truncated 2 chars)", no lone surrogate).
  • Existing cases preserved: elide(undefined) → undefined, under-limit text returned unchanged, and plain ASCII truncation elide("abcdef", 3) → "abc… (truncated 3 chars)" (no regression).

Each assertion checks the elided output with the suite's hasLoneSurrogate helper, which iterates code points and rejects any unpaired surrogate.

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.
@openclaw-barnacle openclaw-barnacle Bot added channel: whatsapp-web Channel integration: whatsapp-web size: XS triage: needs-pr-context Candidate: external PR body lacks required problem context or evidence. labels Jun 24, 2026
@clawsweeper

clawsweeper Bot commented Jun 24, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed June 26, 2026, 12:02 AM ET / 04:02 UTC.

Summary
The PR changes WhatsApp auto-reply elision to use the SDK UTF-16-safe truncation helper and adds emoji-boundary regression tests.

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: 🦞 diamond lobster
Proof: 🦞 diamond lobster
Patch quality: 🦞 diamond lobster
Result: ready for maintainer review.

Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch.

Next step before merge

  • [P2] No repair job is needed; maintainers should perform ordinary review and land the PR if they accept the focused fix.

Security
Cleared: The diff only changes TypeScript helper usage and focused tests; it adds no dependency, workflow, secret, package, or code-execution surface.

Review details

Best 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 changes

Label justifications:

  • P3: This is a low-blast-radius WhatsApp text elision bug fix with no config, persistence, auth, provider routing, or delivery-contract change.
  • rating: 🦞 diamond lobster: Overall readiness is 🦞 diamond lobster; proof is 🦞 diamond lobster and patch quality is 🦞 diamond lobster.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (terminal): The PR body includes terminal before/after output from running the real elide function with lone-surrogate checks, and the latest Real behavior proof checks for this head succeeded.
  • proof: sufficient: Contributor real behavior proof is sufficient. The PR body includes terminal before/after output from running the real elide function with lone-surrogate checks, and the latest Real behavior proof checks for this head succeeded.
Evidence reviewed

PR surface:

Source +2, Tests +23. Total +25 across 2 files.

View PR surface stats
Area Files Added Removed Net
Source 1 3 1 +2
Tests 1 23 0 +23
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 26 1 +25

What I checked:

Likely related people:

  • scoootscooob: Merged refactor: move WhatsApp channel implementation to extensions/ #45725, which moved the WhatsApp auto-reply implementation and adjacent tests into extensions/whatsapp. (role: WhatsApp extension migration contributor; confidence: high; commits: 16505718e827; files: extensions/whatsapp/src/auto-reply/util.ts, extensions/whatsapp/src/auto-reply/web-auto-reply-utils.test.ts)
  • mcaxtr: Commit 458a526 recently touched the same process-message and web-auto-reply utility test area. (role: recent WhatsApp auto-reply area contributor; confidence: medium; commits: 458a52610a4d; files: extensions/whatsapp/src/auto-reply/monitor/process-message.ts, extensions/whatsapp/src/auto-reply/web-auto-reply-utils.test.ts)
  • Peter Steinberger: Commit 63f5fa4 introduced the UTF-16-safe truncation helpers that this PR reuses. (role: shared helper introducer; confidence: medium; commits: 63f5fa47deb6; files: src/utils.ts)
What the crustacean ranks mean
  • 🦀 challenger crab: rare, exceptional readiness with strong proof, clean implementation, and convincing validation.
  • 🦞 diamond lobster: very strong readiness with only minor maintainer review expected.
  • 🐚 platinum hermit: good normal PR, likely mergeable with ordinary maintainer review.
  • 🦐 gold shrimp: useful signal, but proof or patch confidence is still limited.
  • 🦪 silver shellfish: thin signal; proof, validation, or implementation needs work.
  • 🧂 unranked krab: not merge-ready because proof is missing/unusable or there are serious correctness or safety concerns.
  • 🌊 off-meta tidepool: rating does not apply to this item.

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
  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

@clawsweeper clawsweeper Bot added rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. P3 Low-priority cleanup, docs, polish, ergonomics, or speculative work. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Jun 24, 2026
@openclaw-barnacle openclaw-barnacle Bot removed the triage: needs-pr-context Candidate: external PR body lacks required problem context or evidence. label Jun 25, 2026
@clawsweeper clawsweeper Bot added rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. and removed rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. labels Jun 26, 2026
@vincentkoc
vincentkoc merged commit cb8bc71 into openclaw:main Jun 27, 2026
189 of 211 checks passed
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Jun 28, 2026
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.
chenyangjun-xy pushed a commit to chenyangjun-xy/openclaw that referenced this pull request Jul 1, 2026
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.
Rorqualx pushed a commit to Rorqualx/cortex that referenced this pull request Jul 2, 2026
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

channel: whatsapp-web Channel integration: whatsapp-web P3 Low-priority cleanup, docs, polish, ergonomics, or speculative work. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. size: XS status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants