Skip to content

fix(core): stop replace from editing the wrong similar block#26976

Open
Yash1927 wants to merge 1 commit into
google-gemini:mainfrom
Yash1927:fix/replace-wrong-similar-block
Open

fix(core): stop replace from editing the wrong similar block#26976
Yash1927 wants to merge 1 commit into
google-gemini:mainfrom
Yash1927:fix/replace-wrong-similar-block

Conversation

@Yash1927
Copy link
Copy Markdown

@Yash1927 Yash1927 commented May 13, 2026

Summary

This fixes a case where replace could edit the wrong similar block when it had to rely on approximate matching.

Details

  • Makes flexible matching fail instead of editing when more than one match is found and allow_multiple is not enabled.
  • Makes regex matching follow the same behavior for multiple matches.
  • Tightens fuzzy matching so it stays on the intended block instead of drifting to a similarly shaped block with different identifiers.
  • Adds regression tests for flexible, regex, and fuzzy matching around similar-block collisions.

Related Issues

Fixes #26975

How to Validate

  1. Run:
    npx vitest run packages/core/src/tools/edit.test.ts

  2. Run:
    npx prettier --check packages/core/src/tools/edit.ts packages/core/src/tools/edit.test.ts

  3. Run:
    npx eslint packages/core/src/tools/edit.ts packages/core/src/tools/edit.test.ts

  4. Run:
    npm run typecheck --workspace @google/gemini-cli-core

Expected result:

  • the edit tool test suite passes
  • formatting, lint, and typecheck pass for the touched files

Pre-Merge Checklist

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

@Yash1927 Yash1927 requested a review from a team as a code owner May 13, 2026 09:32
@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 issues where the 'replace' tool could incorrectly modify multiple similar code blocks when only one was intended. By implementing stricter validation and enforcing 'allow_multiple' constraints across all matching strategies, the tool now provides more predictable and safer code editing behavior.

Highlights

  • Improved Matching Safety: Updated flexible, regex, and fuzzy matching strategies to prevent accidental edits when multiple matches are found, unless 'allow_multiple' is explicitly enabled.
  • Enhanced Fuzzy Matching Precision: Tightened fuzzy matching logic by introducing identifier token validation, ensuring that edits do not drift to similarly shaped blocks with different identifiers.
  • Regression Testing: Added comprehensive test cases for flexible, regex, and fuzzy matching to ensure collisions with similar blocks are handled correctly.
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.

@google-cla
Copy link
Copy Markdown

google-cla Bot commented May 13, 2026

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

Copy link
Copy Markdown
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 implements stricter controls for the file editing tools by enforcing the allow_multiple parameter across flexible, regex, and fuzzy replacement strategies to prevent unintended multiple edits. It also enhances the fuzzy matching algorithm by introducing a 'protected tokens' mechanism to ensure that identifiers, such as function names, are preserved during matching. Feedback was provided regarding the trailing newline logic in flexible replacements, which was identified as too aggressive and potentially problematic for deletions. Additionally, a reminder was given to trim string parameters before checking for emptiness to comply with repository guidelines.

Comment on lines +223 to +226
let replacementText = newBlockWithIndent.join('\n');
if (sourceLines[index + searchLinesStripped.length - 1].endsWith('\n')) {
replacementText += '\n';
}
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

The current logic for adding a trailing newline to the replacement text is too aggressive. It causes issues with deletions and existing newlines. Additionally, per repository rules, when validating string parameters from tools, ensure the string is trimmed before checking for emptiness to prevent whitespace-only values from being accepted.

      let replacementText = newBlockWithIndent.join('\n');
      if (
        normalizedReplace.trim() !== '' &&
        !replacementText.endsWith('\n') &&
        sourceLines[index + searchLinesStripped.length - 1].endsWith('\n')
      ) {
        replacementText += '\n';
      }
References
  1. When validating string parameters from tools, trim the string first and then check for emptiness to prevent whitespace-only values from being accepted.

@gemini-cli gemini-cli Bot added area/agent Issues related to Core Agent, Tools, Memory, Sub-Agents, Hooks, Agent Quality priority/p2 Important but can be addressed in a future release. labels May 13, 2026
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 priority/p2 Important but can be addressed in a future release.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

replace may edit the wrong similar function when approximate matching is used

1 participant