fix(tool): keep truncated results within maxBytes for small caps#433
Open
Alan-Yu-2077 wants to merge 1 commit into
Open
fix(tool): keep truncated results within maxBytes for small caps#433Alan-Yu-2077 wants to merge 1 commit into
Alan-Yu-2077 wants to merge 1 commit into
Conversation
applyResultSizeLimit reserved room for the truncation suffix with Math.max(0, maxBytes - suffixBytes) but still appended the full suffix when maxBytes was smaller than it. A one-byte result with maxBytes=0 returned the full 46-byte notice and metadata.returnedBytes recorded 46, both exceeding the requested cap. When maxBytes cannot fit the suffix, cap the notice itself so the returned text and metadata.returnedBytes stay within maxBytes. Add boundary regression tests (maxBytes 0, around the suffix length, and a multibyte UTF-8 boundary).
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.
Summary
applyResultSizeLimit()computed a head/tail budget withMath.max(0, maxBytes - suffixBytes)but still appended the full truncation suffix afterwards. WhenmaxByteswas smaller than the suffix ("\n[Tool output truncated: head and tail shown.]", 46 bytes), the returned text exceeded the requested cap.Per the report, a one-byte result with
maxBytes = 0returned the full 46-byte notice andmetadata.returnedBytesrecorded46— both larger than the requested maximum.This fix caps the notice itself when
maxBytescannot fit the suffix, and otherwise reserves room for the suffix as before. The returned text andmetadata.returnedBytesnow stay withinmaxBytesfor every non-negative limit. UTF-8 safety is preserved via the existingtruncateUtf8helper.Fixes #426
Test plan
Extended
tests/tool/tool-result-size-limit.spec.ts:metadata.returnedBytesnever exceedmaxBytesfor0,1,suffixBytes - 1,suffixBytes, andsuffixBytes + 1;€) result stays within the cap and contains no U+FFFD replacement characters at the byte boundary;Full suite (
npm test) stays green.