Skip to content

fix(tool): keep truncated results within maxBytes for small caps#433

Open
Alan-Yu-2077 wants to merge 1 commit into
OpenBMB:mainfrom
Alan-Yu-2077:fix/tool-result-size-limit-small-cap
Open

fix(tool): keep truncated results within maxBytes for small caps#433
Alan-Yu-2077 wants to merge 1 commit into
OpenBMB:mainfrom
Alan-Yu-2077:fix/tool-result-size-limit-small-cap

Conversation

@Alan-Yu-2077

Copy link
Copy Markdown

Summary

applyResultSizeLimit() computed a head/tail budget with Math.max(0, maxBytes - suffixBytes) but still appended the full truncation suffix afterwards. When maxBytes was 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 = 0 returned the full 46-byte notice and metadata.returnedBytes recorded 46 — both larger than the requested maximum.

This fix caps the notice itself when maxBytes cannot fit the suffix, and otherwise reserves room for the suffix as before. The returned text and metadata.returnedBytes now stay within maxBytes for every non-negative limit. UTF-8 safety is preserved via the existing truncateUtf8 helper.

Fixes #426

Test plan

Extended tests/tool/tool-result-size-limit.spec.ts:

  • returned bytes and metadata.returnedBytes never exceed maxBytes for 0, 1, suffixBytes - 1, suffixBytes, and suffixBytes + 1;
  • a multibyte () result stays within the cap and contains no U+FFFD replacement characters at the byte boundary;
  • the existing head/tail truncation test is unchanged.
$ node --test dist/tests/tool/tool-result-size-limit.spec.js
# tests 3
# pass 3
# fail 0

Full suite (npm test) stays green.

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).
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.

Tool result size limit can return output larger than maxBytes for small limits

1 participant