fix(matrix): truncate reply context on code-point boundaries to avoid splitting surrogate pairs#96412
Closed
ly-wang19 wants to merge 1 commit into
Closed
Conversation
truncateReplyBody used a raw value.slice(0, MAX_REPLY_BODY_LENGTH - 3) to shorten long reply bodies. When the cut index fell between the two UTF-16 code units of a surrogate pair (e.g. an emoji), the slice left a lone high surrogate before the ellipsis, which renders as a broken glyph in the reply context shown to the agent. Replace the raw slice with sliceUtf16Safe from the plugin SDK so the truncation never cuts inside a surrogate pair. A normal (non-astral) body is unaffected. 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 was referenced Jun 26, 2026
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
truncateReplyBodyinextensions/matrix/src/matrix/monitor/reply-context.ts(reached via the exportedsummarizeMatrixReplyEvent) shortens long reply bodies with a rawvalue.slice(0, MAX_REPLY_BODY_LENGTH - 3). UTF-16.slicecuts at a code-unit index, not a code-point boundary. When the cut index lands between the two halves of a surrogate pair (any emoji or other astral character), the slice keeps a lone high surrogate, which is appended directly before the...ellipsis. The reply context surfaced to the agent then contains a dangling\uD83Dthat renders as a broken replacement glyph.Repro: a plain
m.room.messagewhosecontent.bodyis 496acharacters, then the emoji 😀 (U+1F600, a surrogate pair at UTF-16 indices 496–497), thenbcd(total length 501):value.slice(0, 497)splits the emoji, returning"a".repeat(496) + "\uD83D" + "..."— a lone high surrogate (broken glyph) before the ellipsis."a".repeat(496) + "...". The half emoji is dropped; no orphaned surrogate.The fix swaps the raw slice for
sliceUtf16Safe(already exported fromopenclaw/plugin-sdk/text-utility-runtimeand used by sibling extensions such as discord and memory-lancedb), which never cuts inside a surrogate pair. Bodies without astral characters are unaffected — a 600-char ASCII body still truncates to"x".repeat(497) + "..."exactly as before.Evidence
Standalone Node red/green proof. It copies
sliceUtf16Safeverbatim fromsrc/utils.tsand runs both the buggy (.slice) and fixed (sliceUtf16Safe) versions oftruncateReplyBodyagainst the repro input plus an unaffected normal body:A regression test is added to
reply-context.test.ts(truncates on a code-point boundary without orphaning a surrogate half) that fails on the raw-slice code and passes with the fix.