Skip to content

fix(dsh-weknora): avoid splitting UTF-16 surrogate pairs in clip() (fixes #5519) - #5707

Open
Crazy-ChenMiLin wants to merge 1 commit into
Tencent:mainfrom
Crazy-ChenMiLin:fix/issue-5519-dsh-clip-surrogate-pair
Open

Crazy-ChenMiLin wants to merge 1 commit into
Tencent:mainfrom
Crazy-ChenMiLin:fix/issue-5519-dsh-clip-surrogate-pair

Conversation

@Crazy-ChenMiLin

Copy link
Copy Markdown

Description

Fixes #5519.

When clip() in packages/dsh-weknora/src/render.ts truncates text to maxChars using normalized.slice(0, maxChars), slicing between the two code units of a UTF-16 surrogate pair (such as emojis or mathematical italic symbols extracted from documents) leaves a lone high surrogate at the end of the string.
When serialized to JSON in session history (\ud835), model API gateways reject the malformed surrogate pair with 400 Bad Request, permanently breaking the client session.

This change inspects whether normalized.charCodeAt(end - 1) falls in the high-surrogate range [0xD800, 0xDBFF]. If so, it decrements end by 1 before slicing so the surrogate pair is never split into a lone high surrogate.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Performance improvement
  • Refactoring (no functional changes)

Verification

  • Added comprehensive unit tests in packages/dsh-weknora/test/render.test.mjs verifying that astral characters (mathematical italic symbols, emojis) are not split across surrogate boundaries and JSON parsing / round-trip remains valid.
  • Executed node --test "test/*.test.mjs": all 65 unit tests passed cleanly.
  • Verified git diff --check origin/main...HEAD is clean.

Checklist

  • My code follows the code style of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Copilot AI balanced review requested due to automatic review settings November 19, 2026 09:46

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: dsh-weknora clip() can split a surrogate pair, and one such passage permanently breaks the client session

2 participants