fix(agents): truncate tool-display detail on code-point boundaries#96420
Closed
ly-wang19 wants to merge 1 commit into
Closed
fix(agents): truncate tool-display detail on code-point boundaries#96420ly-wang19 wants to merge 1 commit into
ly-wang19 wants to merge 1 commit into
Conversation
coerceDisplayValue truncated a long first-line detail value with raw UTF-16 slice() at half = floor((maxStringChars-1)/2). When an emoji (surrogate pair) straddles the cut boundary, the head kept a lone high surrogate and the tail could begin on a lone low surrogate, rendering as the replacement character. Use the existing sliceUtf16Safe helper so the whole code point is dropped at the boundary, matching the UTF-16-safe truncation used elsewhere in the repo. Behavior-preserving for non-surrogate input. 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
coerceDisplayValueinsrc/agents/tool-display-common.tsbuilds the compact one-line "detail" shown for tool calls (chat progress lines, CLI activity, UI tool-update streams). When a detail value's first line is longer thanmaxStringChars(default 160, used wheneverresolveToolDisplaypasses nodetailCoerce), it elides the middle:String.prototype.slicecuts on raw UTF-16 code-unit boundaries. When an emoji (a surrogate pair) straddles the cut, the head keeps the emoji's high surrogate alone and the tail can begin on a lone low surrogate — both render as the replacement character (the box/question-mark glyph).This is the only remaining raw-
slicetruncation on this path; sibling code (e.g.embedded-agent-subscribe.tools.ts,chunkByNewline, the LINE/Slack/Matrix adapters) already truncates with the repo's UTF-16-safe helpers.Before / After repro
Route a detail-key string value through the public entry point with the default
maxStringChars = 160:half = Math.floor(159/2) = 79.firstLine.slice(0, 79)keeps the high surrogate\uD83Dat index 78 and drops its low half at index 79 → head ends in a lone high surrogate (renders as the replacement char). The tailslice(-80)can likewise begin on a lone low surrogate\uDE00."a".repeat(78), no trailing lone surrogate, and the tail does not begin mid-pair.Fix
Swap the two raw slices for the existing
sliceUtf16Safehelper fromsrc/utils.ts(same one backingtruncateUtf16Safe):sliceUtf16Safenudges the cut off a dangling surrogate at either edge. For inputs with no surrogate at the boundary (the overwhelming common case, including all-ASCII), it returns byte-identical output, so this is behavior-preserving.Evidence
Node red-green proof (
/tmp/verify-tool-display-common-surrogate-safe.mjs) replicating the truncation branch withsliceUtf16Safecopied verbatim fromsrc/utils.ts, plus ahasLoneSurrogatescanner:slicelogic produces a lone surrogate (bug reproduced).xlong value and a long all-ASCII value both produce byte-identical output before vs after; short (untruncated) values are unchanged.A unit test exercising the same case through the exported public API
resolveToolVerbAndDetailForArgsis added insrc/agents/tool-display-common.test.ts(asserts no lone surrogate, head=== "a".repeat(78), tail not starting mid-pair, plus the ASCII-truncation and short-value behavior-preservation cases). No new export was needed — the existing public function reachescoerceDisplayValueviaresolveDetailFromKeys.🤖 Generated with Claude Code