Skip to content

fix(core): correct ellipsis logic in EditTool getDescription()#28105

Closed
Megashubham wants to merge 3 commits into
google-gemini:mainfrom
Megashubham:fix/edit-tool-description-snippet-truncation
Closed

fix(core): correct ellipsis logic in EditTool getDescription()#28105
Megashubham wants to merge 3 commits into
google-gemini:mainfrom
Megashubham:fix/edit-tool-description-snippet-truncation

Conversation

@Megashubham

@Megashubham Megashubham commented Jun 23, 2026

Copy link
Copy Markdown

Summary

Fixes a subtle display bug in EditToolInvocation.getDescription() where the ellipsis (...) suffix in the edit description snippet was incorrectly computed.

The Bug

// BEFORE (buggy):
const oldStringSnippet =
  this.params.old_string.split('\n')[0].substring(0, 30) +
  (this.params.old_string.length > 30 ? '...' : ''); // checks TOTAL string length

The code extracts the first line of old_string via .split('\n')[0] and applies .substring(0, 30) to it. However, the ellipsis decision was based on this.params.old_string.length > 30 - the length of the entire string, not the first line.

Example: old_string = "function foo() {\n return 1;\n}" (30 chars total)

  • First line "function foo() {" is only 17 chars - correctly displayed
    • But old_string.length > 30 -> false -> no ... even though the edit spans multiple lines!
      The Fix
// AFTER (correct):
const oldFirstLine = this.params.old_string.split('\n')[0];
const oldStringSnippet =
  oldFirstLine.substring(0, 30) +
  (oldFirstLine.length > 30 || this.params.old_string.includes('\n')
    ? '...'
    : '');

The fix appends ... when: (1) the first line itself exceeds 30 characters (truncated by substring), or (2) the full string contains newlines (multi-line), so more content exists beyond the snippet. Same fix is applied symmetrically to new_string.

Tests

Added 5 new unit tests in edit.test.ts covering all cases: new file (empty old_string) - no ellipsis; short single-line strings - no ellipsis; multi-line string with short first line - shows ... (the regression case); long first line (> 30 chars) - shows ...; same old_string and new_string - "No file changes" message.

All 67 tests pass. TypeScript typecheck passes. ESLint + Prettier pass.

This fix has already been implemented, the tests pass, and it's ready for review.

@Megashubham Megashubham requested a review from a team as a code owner June 23, 2026 04:41
@google-cla

google-cla Bot commented Jun 23, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@github-actions github-actions Bot added the size/m A medium sized PR label Jun 23, 2026
@github-actions

github-actions Bot commented Jun 23, 2026

Copy link
Copy Markdown

📊 PR Size: size/M

  • Lines changed: 119
  • Additions: +113
  • Deletions: -6
  • Files changed: 2

@github-actions

github-actions Bot commented Jun 23, 2026

Copy link
Copy Markdown

🛑 Action Required: Evaluation Approval

Steering changes have been detected in this PR. To prevent regressions, a maintainer must approve the evaluation run before this PR can be merged.

Maintainers:

  1. Go to the Workflow Run Summary.
  2. Click the yellow 'Review deployments' button.
  3. Select the 'eval-gate' environment and click 'Approve'.

Once approved, the evaluation results will be posted here automatically.

@Megashubham

Copy link
Copy Markdown
Author

@googlebot I signed it!

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

Gemini encountered an error creating the summary. You can try again by commenting /gemini summary.

@Megashubham

Copy link
Copy Markdown
Author

/gemini summary

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

This pull request addresses a display bug in the EditTool description generation where the ellipsis suffix was incorrectly determined based on the total string length instead of the visible snippet length. The changes ensure that the UI accurately indicates when content is truncated or spans multiple lines, improving the clarity of edit descriptions.

Highlights

  • Ellipsis Logic Correction: Updated the ellipsis calculation in EditToolInvocation.getDescription() to correctly account for multi-line strings rather than just the total string length.
  • Improved Snippet Accuracy: Modified the logic to append an ellipsis if the first line exceeds 30 characters OR if the string contains multiple lines, ensuring a more accurate representation of the edit.
  • Comprehensive Testing: Added 5 new unit tests to cover various edge cases, including empty strings, short single-line strings, multi-line strings, and long lines.
Activity
  • The CLA check failed initially, but the author confirmed signing the agreement.
  • The PR size was calculated as size/M with 87 lines changed.
  • A maintainer approval is required for the evaluation run due to detected steering changes.
  • The author requested a summary generation via the /gemini summary command.

@Megashubham Megashubham force-pushed the fix/edit-tool-description-snippet-truncation branch from 04fdac1 to 37796db Compare June 23, 2026 05:23
@gemini-cli gemini-cli Bot added the status/need-issue Pull requests that need to have an associated issue. label Jun 23, 2026
@Megashubham

Copy link
Copy Markdown
Author

/gemini review

@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 getDescription method in EditToolInvocation to correctly append an ellipsis when truncating multi-line strings, even if the first line is short. It also adds comprehensive unit tests to verify this behavior under various scenarios. The review feedback highlights a performance concern where using split('\n') on large files can cause high memory usage, and suggests an optimized approach using indexOf('\n') along with proper handling of multi-byte Unicode characters.

Comment thread packages/core/src/tools/edit.ts Outdated
@Megashubham Megashubham force-pushed the fix/edit-tool-description-snippet-truncation branch from 37796db to e530737 Compare June 23, 2026 12:05
@Megashubham

Copy link
Copy Markdown
Author

/gemini review

@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 refactors the snippet generation logic in packages/core/src/tools/edit.ts to handle multi-line strings and multi-byte characters, and adds comprehensive unit tests in packages/core/src/tools/edit.test.ts. The review feedback correctly identifies a potential issue with Windows-style line endings (\r\n), where the carriage return (\r) is retained in the extracted first line and can cause rendering issues in terminal environments. A code suggestion is provided to normalize line endings before processing.

Comment thread packages/core/src/tools/edit.ts
The description snippet for old_string and new_string was using
the total string length to decide whether to append '...' instead
of checking the first-line length that was actually being displayed.

This caused two issues:
- Multi-line strings with a short first line (< 30 chars) would not
  show '...' even though the edit description was only showing the
  first line, hiding that the edit spans multiple lines.
- The check was inconsistent: substring(0, 30) was applied on the
  first line, but the length guard was on the entire string.

Fix by extracting the first line into a variable and checking:
1. Whether the first line itself exceeds 30 characters (truncated by
   substring), OR
2. Whether the full string is multi-line (\n present), indicating
   more content exists beyond what is shown.

Added unit tests to cover all cases: new file, short single-line,
multi-line with short first line, long first line, and no-op edits.
@Megashubham Megashubham force-pushed the fix/edit-tool-description-snippet-truncation branch from e530737 to 91bd882 Compare June 23, 2026 12:13
@Megashubham

Copy link
Copy Markdown
Author

/gemini review

@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 refactors the snippet generation logic in EditToolInvocation.getDescription to normalize line endings and correctly handle multi-byte characters, accompanied by comprehensive unit tests. The review feedback highlights a potential performance and memory issue where calling Array.from on extremely long lines could cause OOM crashes, and provides an optimized suggestion to check the string length before array conversion.

Comment thread packages/core/src/tools/edit.ts
@Megashubham

Copy link
Copy Markdown
Author

/gemini review

@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 adds comprehensive unit tests for the getDescription method in edit.test.ts and refactors the snippet extraction logic in edit.ts to handle line-ending normalization and truncation. The reviewer provided valuable feedback on simplifying the getSnippet helper function by using Array.from() directly on the first line, which ensures safe truncation of multi-byte Unicode characters without splitting them.

Comment thread packages/core/src/tools/edit.ts
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@Megashubham Megashubham force-pushed the fix/edit-tool-description-snippet-truncation branch from 6bddbc8 to 8f125c6 Compare June 23, 2026 12:25
@Megashubham

Copy link
Copy Markdown
Author

/gemini review

@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 refactors the snippet generation logic in EditToolInvocation.getDescription to normalize CRLF line endings and safely handle multi-byte Unicode characters when truncating strings, accompanied by comprehensive unit tests. Feedback suggests optimizing the truncation logic by using Intl.Segmenter to lazily evaluate grapheme clusters up to the 30-character limit, preventing potential performance and memory issues on extremely large single-line inputs.

Comment thread packages/core/src/tools/edit.ts Outdated
@Megashubham

Copy link
Copy Markdown
Author

/gemini review

@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 introduces a helper function getSnippet in packages/core/src/tools/edit.ts to safely extract and truncate snippets of old and new strings using Intl.Segmenter for grapheme-safe truncation, along with corresponding unit tests in packages/core/src/tools/edit.test.ts. However, a logical bug was identified in the truncation condition where chars.length is capped at 30 before checking if it exceeds 30, making the truncation block unreachable and causing tests for long lines to fail. A refactored implementation using array slicing was suggested to resolve this issue.

Comment thread packages/core/src/tools/edit.ts Outdated
@Megashubham Megashubham force-pushed the fix/edit-tool-description-snippet-truncation branch from ca7fa59 to f011559 Compare June 23, 2026 12:39
@Megashubham

Copy link
Copy Markdown
Author

/gemini review

@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 refactors the snippet generation logic in EditToolInvocation.getDescription to use Intl.Segmenter for grapheme-aware truncation and adds comprehensive unit tests. The reviewer recommends optimizing the getSnippet helper to process large inputs lazily, preventing potential performance degradation or out-of-memory errors from eager string replacement and segmentation.

Comment thread packages/core/src/tools/edit.ts
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@Megashubham Megashubham force-pushed the fix/edit-tool-description-snippet-truncation branch from f011559 to accb71e Compare June 23, 2026 12:55
@Megashubham

Copy link
Copy Markdown
Author

/gemini review

@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 refactors the snippet generation logic in packages/core/src/tools/edit.ts to use Intl.Segmenter for safely segmenting the first line of the edit strings up to 30 characters, handling multi-line strings, and stripping trailing carriage returns (\r). It also adds comprehensive unit tests in packages/core/src/tools/edit.test.ts to verify these behaviors under various scenarios. There are no review comments, and I have no feedback to provide.

@Megashubham

Copy link
Copy Markdown
Author

can you check once :P
@Samee24 !

@gemini-cli

gemini-cli Bot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Hi there! Thank you for your interest in contributing to Gemini CLI.

To ensure we maintain high code quality and focus on our prioritized roadmap, we only guarantee review and consideration of pull requests for issues that are explicitly labeled as 'help wanted'.

This PR will be closed in 7 days if it remains without that designation. We encourage you to find and contribute to existing 'help wanted' issues in our backlog! Thank you for your understanding.

@gemini-cli

gemini-cli Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

This pull request is being closed as it has been open for 14 days without a 'help wanted' designation. We encourage you to find and contribute to existing 'help wanted' issues in our backlog! Thank you for your understanding.

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

Labels

size/m A medium sized PR status/need-issue Pull requests that need to have an associated issue. status/pr-nudge-sent

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant