Skip to content

fix(core): disable truncation when maxChars <= 0 in formatTruncatedToolOutput - #29542

Merged
DavidAPierce merged 2 commits into
google-gemini:mainfrom
diegogodinezr:GH-28620
Sep 29, 2026
Merged

DavidAPierce merged 2 commits into
google-gemini:mainfrom
diegogodinezr:GH-28620

Conversation

@diegogodinezr

Copy link
Copy Markdown
Contributor

Summary

Clarifies behavior and adds a non-positive guard so that maxChars <= 0 disables output truncation and returns contentStr unchanged, preventing index slicing edge cases where output string size inadvertently expanded.

Details

In packages/core/src/utils/fileUtils.ts, formatTruncatedToolOutput calculates head and tail character slices to display output previews when strings exceed maxChars. When maxChars <= 0, negative slice indices cause unexpected boundary behavior where head and tail slices capture nearly the full string, resulting in output size inflation.

This change updates the early return condition to:

if (maxChars <= 0 || contentStr.length <= maxChars) return contentStr;

When maxChars <= 0, truncation is treated as disabled and contentStr is returned untouched without wrapping in truncation headers.

Regression unit tests have been added to packages/core/src/utils/fileUtils.test.ts to verify behavior for maxChars = 0, maxChars < 0 (e.g., -1, -1000), and maxChars > 0.

Related Issues

Fixes #28620

How to Validate

  1. Run unit tests for fileUtils:
    npx vitest run packages/core/src/utils/fileUtils.test.ts
    Verify all tests in formatTruncatedToolOutput pass, including cases for maxChars = 0 and maxChars < 0.
  2. Run type checking and linting:
    npm run typecheck
    npx eslint packages/core/src/utils/fileUtils.ts packages/core/src/utils/fileUtils.test.ts

Pre-Merge Checklist

  • Updated relevant documentation and README (if needed)
  • Added/updated tests (if needed)
  • Noted breaking changes (if any)
  • Validated on required platforms/methods:
    • MacOS
      • npm run
      • npx
      • Docker
      • Podman
      • Seatbelt
    • Windows
      • npm run
      • npx
      • Docker
    • Linux
      • npm run
      • npx
      • Docker

…olOutput

Add a guard condition for maxChars <= 0 in formatTruncatedToolOutput. When maxChars is non-positive, output truncation is disabled and contentStr is returned untouched, preventing negative index slicing edge cases where output string size inadvertently expanded.

Fixes google-gemini#28620
@diegogodinezr
diegogodinezr requested a review from a team as a code owner September 28, 2026 19:38
@github-actions github-actions Bot added the size/s A small PR label Sep 28, 2026
@github-actions

Copy link
Copy Markdown

📊 PR Size: size/S

  • Lines changed: 46
  • Additions: +43
  • Deletions: -3
  • Files changed: 2

@diegogodinezr

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-cli gemini-cli Bot added priority/p1 Important and should be addressed in the near term. area/core Issues related to User Interface, OS Support, Core Functionality labels Sep 28, 2026
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses an issue where negative or zero values for maxChars in formatTruncatedToolOutput led to incorrect string truncation due to negative index slicing. By introducing an early return condition, the function now correctly treats non-positive maxChars as a signal to disable truncation, ensuring the original content is returned intact.

Highlights

  • Logic Correction: Updated the formatTruncatedToolOutput function to include a guard clause that disables truncation when maxChars is 0 or negative.
  • Edge Case Prevention: Prevented unexpected string slicing behavior caused by negative indices when maxChars is non-positive.
  • Test Coverage: Added comprehensive unit tests to verify behavior for maxChars <= 0 and confirmed that content remains unchanged in these scenarios.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the formatTruncatedToolOutput utility in packages/core/src/utils/fileUtils.ts to return the original content string unchanged when maxChars is less than or equal to 0. It also reorganizes the corresponding unit tests in fileUtils.test.ts into separate describe blocks and adds comprehensive test coverage for edge cases, including maxChars values of 0, negative numbers, and when the content length is within the maxChars limit. There are no review comments, so I have no additional feedback to provide.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the formatTruncatedToolOutput function in packages/core/src/utils/fileUtils.ts to return the original content string unmodified when maxChars is less than or equal to zero. It also adds corresponding unit tests in packages/core/src/utils/fileUtils.test.ts to verify this behavior for maxChars values of 0, negative numbers, and when the content length is within the maxChars limit. I have no feedback to provide.

@DavidAPierce
DavidAPierce added this pull request to the merge queue Sep 29, 2026
Merged via the queue into google-gemini:main with commit d75234c Sep 29, 2026
52 of 53 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/core Issues related to User Interface, OS Support, Core Functionality priority/p1 Important and should be addressed in the near term. size/s A small PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

formatTruncatedToolOutput: negative maxChars silently inflates output ~2x

2 participants