Skip to content

fix(core): don't cut an astral character in half when truncating telemetry strings - #6868

Open
kevin9327 wants to merge 1 commit into
heygen-com:mainfrom
kevin9327:fix/telemetry-truncate-surrogate
Open

kevin9327 wants to merge 1 commit into
heygen-com:mainfrom
kevin9327:fix/telemetry-truncate-surrogate

Conversation

@kevin9327

Copy link
Copy Markdown
Contributor

What

redactTelemetryString no longer leaves half of a surrogate pair at the cut.

truncateTelemetryString cut at maxLength - 1 UTF-16 code units. When that position fell between the two halves of an astral character (emoji, many CJK extension characters), the kept text ended in a lone high surrogate before the …. That string is not well-formed UTF-16: encodeURIComponent throws URIError on it, and TextEncoder/JSON serialisation turn it into U+FFFD, so a telemetry or observation message with an emoji near the 240-character limit either breaks the code that sends it or reaches the backend with a replacement character.

redactTelemetryString(`render failed ${"🎬".repeat(20)}`, 18)
// before: "render failed 🎬\uD83C…"   (lone high surrogate)
// after:  "render failed 🎬…"

How

If the last kept code unit is a high surrogate, the cut moves back by one, so the pair is dropped whole. Strings under the limit and cuts that land on a character boundary are unchanged; the result is never longer than maxLength.

Test

New case in packages/core/src/telemetryRedaction.test.ts: a message of emoji truncated at a limit that falls inside a pair keeps only whole characters and is isWellFormed(). The whole file passes (50 tests). Formatted with oxfmt 0.41.0.

fails without the fix: packages/core/src/telemetryRedaction.test.ts › "does not cut an astral character in half when truncating" (ends in \uD83C…)

This PR was prepared with AI assistance; I reviewed the change and ran the tests.

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.

1 participant