Sitelet https://github.com/google-gemini/gemini-cli/pull/29004
Skip to content

fix(core): guard formatTruncatedToolOutput against non-positive maxCh… - #29004

Open
Eswar809 wants to merge 1 commit into
google-gemini:mainfrom
Eswar809:fix/truncate-tool-output-non-positive-budget
Open

fix(core): guard formatTruncatedToolOutput against non-positive maxCh…#29004
Eswar809 wants to merge 1 commit into
google-gemini:mainfrom
Eswar809:fix/truncate-tool-output-non-positive-budget

Conversation

@Eswar809

@Eswar809 Eswar809 commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

Summary

Guards formatTruncatedToolOutput against non-positive maxChars values, such as 0 or negative budgets, to prevent truncated output from unexpectedly inflating to roughly twice the original size due to negative slice offsets.

Problem

When formatTruncatedToolOutput was called with maxChars <= 0, the truncation logic behaved incorrectly:

  • headChars = Math.floor(maxChars * 0.2) could evaluate to a negative number.
    • This caused slice(0, headChars) to return almost the full string instead of a small prefix.
  • tailChars = maxChars - headChars could evaluate to 0 or a negative value.
    • This caused slice(-tailChars), such as slice(-0), to return the entire string.
  • The head and tail segments were then concatenated inside the truncation wrapper.
    • Instead of truncating the content, this approximately doubled the output size.

Fix

Added an early guard in formatTruncatedToolOutput to handle invalid or unnecessary truncation cases:

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

This ensures that when there is no valid truncation budget, or the content already fits within the allowed budget, the original string is returned unchanged.

Related Issues

Fixes #28620

Validation

Run the fileUtils unit tests:

npm test -w @google/gemini-cli-core -- src/utils/fileUtils.test.ts

Verify that all test cases pass, including the new cases for:

  • maxChars = 0
  • maxChars = -1000
  • Content already within the budget

Pre-Merge Checklist

  • Added or updated tests where needed
  • Validated on required platforms/methods
  • Windows
  • Verified relevant npm scripts pass

@Eswar809
Eswar809 requested a review from a team as a code owner August 24, 2026 07:26
@github-actions github-actions Bot added the size/s A small PR label Aug 24, 2026
@github-actions

github-actions Bot commented Aug 24, 2026

Copy link
Copy Markdown

📊 PR Size: size/S

  • Lines changed: 28
  • Additions: +28
  • Deletions: -0
  • Files changed: 2

@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 a bug in the formatTruncatedToolOutput utility where invalid maxChars inputs caused unintended string concatenation and size inflation. By introducing an early return for non-positive budgets or when truncation is unnecessary, the function now maintains the integrity of the original content.

Highlights

  • Bug Fix: Added a guard clause to formatTruncatedToolOutput to handle non-positive maxChars values, preventing incorrect string slicing and output inflation.
  • Test Coverage: Introduced new unit tests to verify behavior when maxChars is zero, negative, or when the content length is already within the allowed budget.
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-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 Aug 24, 2026

@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 to return the content unchanged when maxChars is zero or negative, along with adding corresponding unit tests. The reviewer noted that returning the full, untruncated content when maxChars is non-positive defeats the purpose of the utility and could lead to memory or context window issues. They suggested instead performing a full truncation (omitting all characters) when maxChars is less than or equal to 0.

Comment thread packages/core/src/utils/fileUtils.ts Outdated
maxChars: number,
): string {
if (contentStr.length <= maxChars) return contentStr;
if (maxChars <= 0 || contentStr.length <= maxChars) return contentStr;

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.

high

When maxChars is 0 or negative, returning the entire contentStr unchanged defeats the purpose of the truncation utility. If the tool output is extremely large (e.g., several megabytes), returning it untruncated can cause downstream failures such as context window overflow or out-of-memory errors.

Instead of returning the full content, we should treat any non-positive maxChars as a budget of 0 and perform a full truncation (showing 0 characters of head/tail and omitting the rest). This safely truncates the output while avoiding the slice(-0) bug.

Please also update the corresponding unit tests in packages/core/src/utils/fileUtils.test.ts to assert that the output is correctly truncated (showing 0 characters and omitting the rest) rather than returned unchanged.

Suggested change
if (maxChars <= 0 || contentStr.length <= maxChars) return contentStr;
if (contentStr.length <= maxChars) return contentStr;
if (maxChars <= 0) {
return `Output too large. Showing first 0 and last 0 characters. For full output see: ${outputFile}\n\n\n... [${contentStr.length.toLocaleString()} characters omitted] ...\n\n`;
}
References
  1. When implementing truncation logic, a simpler approach is preferred over a more complex one if the potential inaccuracy is trivial compared to the overall buffer size.

@Eswar809
Eswar809 force-pushed the fix/truncate-tool-output-non-positive-budget branch from 5d541cb to fdbbd26 Compare August 24, 2026 07:33
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

1 participant