Skip to content

fix(core): add uniqueness guard to edit tool#19890

Merged
bdmorgan merged 3 commits intogoogle-gemini:mainfrom
Shivangisharma4:fix/edit-tool-uniqueness-guard
Feb 22, 2026
Merged

fix(core): add uniqueness guard to edit tool#19890
bdmorgan merged 3 commits intogoogle-gemini:mainfrom
Shivangisharma4:fix/edit-tool-uniqueness-guard

Conversation

@Shivangisharma4
Copy link
Contributor

@Shivangisharma4 Shivangisharma4 commented Feb 22, 2026

Fixes #18917 by adding a check in calculateExactReplacement to prevent multiple occurrences from being incorrectly replaced if they exceed 'expectedReplacements'.

This adds a check in `calculateExactReplacement` to prevent multiple occurrences from being incorrectly replaced if they exceed 'expectedReplacements'.

Fixes google-gemini#18917
@Shivangisharma4 Shivangisharma4 requested a review from a team as a code owner February 22, 2026 09:07
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @Shivangisharma4, 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 resolves an issue where the edit tool could incorrectly replace multiple instances of a string when only a specific number of replacements were intended. By introducing a check for expected_replacements, the tool now ensures that replacements only occur if the number of exact matches does not exceed the expected count, thus preventing erroneous modifications.

Highlights

  • Bug Fix: Implemented a uniqueness guard in the calculateExactReplacement function to prevent unintended multiple replacements when the number of exact occurrences exceeds the expected_replacements parameter.
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 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 counter productive. 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.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

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.

Copy link
Contributor

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

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 safety guard in the edit tool's calculateExactReplacement function. The change correctly prevents replacements when the number of found occurrences exceeds the expected number, avoiding unintended modifications. The implementation is clear and effectively addresses the issue described, improving the robustness of the exact replacement strategy.

@gemini-cli gemini-cli bot added the area/agent Issues related to Core Agent, Tools, Memory, Sub-Agents, Hooks, Agent Quality label Feb 22, 2026
@bdmorgan
Copy link
Collaborator

/gemini I am concerned that this fix only guards the exact matching strategy. If the exact match fails (e.g., due to a single space difference), the tool falls back to the flexible strategy, which still applies all matches it finds without a guard. It seems to me that the PR should apply the same uniqueness guard to the flexible and regex matching strategies. This would ensure that the tool doesn't "fail open" by falling back to an unguarded strategy when a guarded one found too many matches. Please re-review with this in mind

CC: @Shivangisharma4

Copy link
Contributor

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

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 fix to the edit tool to prevent incorrect replacements when the number of found occurrences exceeds the expected number. The change adds a guard in calculateExactReplacement to check for this condition and return the original content, which correctly prevents the generation of a misleading diff for user confirmation. The fix is well-targeted and effectively resolves the described issue.

Copy link
Collaborator

@bdmorgan bdmorgan left a comment

Choose a reason for hiding this comment

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

LGTM

@bdmorgan bdmorgan added this pull request to the merge queue Feb 22, 2026
Merged via the queue into google-gemini:main with commit a91bc60 Feb 22, 2026
27 checks passed
@Shivangisharma4 Shivangisharma4 deleted the fix/edit-tool-uniqueness-guard branch February 23, 2026 02:06
SH20RAJ added a commit to SH20RAJ/gemini-cli that referenced this pull request Feb 23, 2026
Co-authored-by: Bryan Morgan <bryanmorgan@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/agent Issues related to Core Agent, Tools, Memory, Sub-Agents, Hooks, Agent Quality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Recursive Logic Failure: Model-Induced Data Truncation via replace Tool Heuristics

2 participants