Skip to content

fix(tools): snap truncation boundaries outside UTF-16 surrogate pairs (#1979) - #2029

Merged
akramcodez merged 3 commits into
Nano-Collective:mainfrom
dhruvil-codes:fix/truncate-tool-result-surrogate-pairs
Nov 18, 2026
Merged

akramcodez merged 3 commits into
Nano-Collective:mainfrom
dhruvil-codes:fix/truncate-tool-result-surrogate-pairs

Conversation

@dhruvil-codes

Copy link
Copy Markdown
Contributor

Description

Closes #1979

\ runcateToolResult\ and \ etch_url\ sliced strings by UTF-16 index with a hard cut when no whitespace token was nearby, cutting UTF-16 surrogate pairs (such as emojis or other astral code points) in half and producing invalid lone surrogates in model context.

Changes

  • Implemented and exported \snapOutsideSurrogate(content, index, bias)\ in \source/utils/truncate-tool-result.ts:
    • When cutting before (\�ias: 'before'), steps back 1 if the index lands right after a high surrogate (\

@github-actions github-actions Bot added the area:tools Tool implementations and tool-calling label Nov 11, 2026
@github-actions

Copy link
Copy Markdown
Contributor

nc-review: comments — 2 important, 2 nits

@dhruvil-codes — a few things worth a look, none blocking.

The change adds a snapOutsideSurrogate helper and applies it to both cut boundaries in truncateToolResult and to the fetch_url hard cut, which is the fix #1979 asked for. I hand-traced every branch of the helper and of the composed snapOutsidePlaceholder -> snapOutsideToken -> snapOutsideSurrogate chain: the surrogate snap only fires in the no-whitespace-nearby case that #1979 describes, it moves boundaries into the elided middle so the result still fits the cap, and it cannot regress the existing placeholder/secret guarantee (a token-snapped boundary always lands on a whitespace character, never a surrogate). The new tests are genuine regression tests — on the base commit, truncateToolResult('😀'.repeat(3000), 1001) leaves a lone high surrogate in the head and a lone low surrogate in the tail, and the new loneSurrogatePattern assertions fail. Two things to address before merge: the fetch_url half of the fix ships with no test, and PR #2004 appears to be solving the same problem in the same function.

🟠 important · tests · source/tools/fetch-url.tsx:123

The fetch_url half of this fix changes behaviour and ships with no test. The diff swaps content.substring(0, MAX_URL_CONTENT_BYTES) for a surrogate-snapped cut, but the only new specs are in source/utils/truncate-tool-result.spec.ts; source/tools/fetch-url.spec.tsx is untouched.

fetch_url cuts at a hard index with no snapOutsideToken in front of it, so it is a distinct code path from truncateToolResult — it is exactly the path where the snap is load-bearing, and it is not covered by the helper's unit test either. fetch-url.spec.tsx already stubs globalThis.fetch and calls the handler directly, so a regression test is straightforward: return >100,000 characters of emoji from the stub and assert the returned text contains no lone surrogate and that the emoji at the cut point is not split.

The project rubric asks for a regression test with a bug fix, and this half of the diff currently has none.

🟠 important · duplicate · source/utils/truncate-tool-result.ts:39

PR #2004 ("fix: never split surrogate pairs in truncateToolResult", branch fix/truncate-surrogate-pair-clean, by HarshRajSinghania) appears to be solving the same problem against the same function: preventing truncateToolResult from cutting a surrogate pair in half. Both PRs touch source/utils/truncate-tool-result.ts, both target the same defect, and #2029's title names issue #1979, which is the issue this behaviour was reported against.

I could not read #2004's diff — it is not in the supplied corpus — so I cannot confirm it is byte-for-byte the same fix, and the implementations may differ (this PR composes the snap with the existing placeholder and token snaps; #2004's branch name suggests a standalone cleanup). Please compare against #2004 and coordinate rather than landing both.

If the two are equivalent, one should close in favour of the other rather than merging both.

⚪ nit · design · source/utils/truncate-tool-result.ts:39

This is now the third implementation of surrogate-safe cutting in the codebase, and the module name does not match its new role:

  • sliceWholeCharacters in source/services/lifecycle-hooks.ts:411
  • snapToCharBoundary in source/components/text-input.tsx:89
  • the new snapOutsideSurrogate in source/utils/truncate-tool-result.ts

They are close enough that a future fix to one will not reach the others, and snapOutsideSurrogate is now imported by source/tools/fetch-url.tsx, so a generic string helper lives in a module named after a specific tool-result concern. Moving it to a shared source/utils/unicode.ts (or similar) and having the three sites use it would keep the surrogate-boundary logic in one place. Purely optional — the new helper is correct on its own.

⚪ nit · completeness · source/tools/file-ops/diff-edit.tsx:55

Two other tool-result caps still cut by raw UTF-16 index and can leave a lone surrogate, in the same way #1979 describes:

  • capDiffEditResult here: content.slice(0, headLength) and content.slice(-tailLength)
  • clip() in source/tools/bash-failure-capsule.ts:60: trimmed.slice(0, MAX_LINE_CHARS - 1)

This is outside the scope #1979 asked for (it named only truncateToolResult and fetch_url), so not fixing them here is defensible — worth noting so the gap is tracked rather than forgotten, especially since snapOutsideSurrogate now exists and could be reused.


🔴 blocking · 🟠 a reviewer would ask for a change · ⚪ optional

Automated code review — correctness, security, design, tests, plus duplicates and scope. A human still decides; this is not a substitute for review and is not exhaustive. The required status checks separately cover lint, formatting, types, unused dependencies, the test suite and the build. This bot never merges. Maintainers can rerun with /re-review.

@github-actions github-actions Bot added the agent:comments nc-review left non-blocking findings label Nov 11, 2026
@dhruvil-codes

Copy link
Copy Markdown
Contributor Author

Thanks @nc-review!

  1. **Regression test for \ etch_url**: Added in \source/tools/fetch-url.spec.tsx\ (\handler snaps truncation boundary outside surrogate pairs when content exceeds limit). It stubs \globalThis.fetch\ with oversized content where \MAX_URL_CONTENT_BYTES\ splits directly across a surrogate pair, verifying that the boundary snaps cleanly, no lone surrogates are produced, and the result respects the cap.
  2. Regarding PR fix: never split surrogate pairs in truncateToolResult #2004: Maintainer @akramcodez officially assigned issue [Bug] truncateToolResult can cut a surrogate pair in half and return invalid text #1979 to me. This PR (fix(tools): snap truncation boundaries outside UTF-16 surrogate pairs (#1979) #2029) comprehensively covers the full scope of [Bug] truncateToolResult can cut a surrogate pair in half and return invalid text #1979 (both \ runcateToolResult\ and \ etch_url), includes thorough test coverage across odd/even astral boundaries in both suites, and provides the required changeset.

@akramcodez
akramcodez merged commit be2c848 into Nano-Collective:main Nov 18, 2026
18 checks passed
@akramcodez

Copy link
Copy Markdown
Member

Thanks @dhruvil-codes, merged! I merged main into the branch to get it over the line. The fetch_url part was already covered on main, so what landed is the surrogate-safe truncation in truncateToolResult with its regression specs. Appreciate the fix.

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

Labels

agent:comments nc-review left non-blocking findings area:tools Tool implementations and tool-calling

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] truncateToolResult can cut a surrogate pair in half and return invalid text

2 participants