fix(core): stop replace from editing the wrong similar block#26976
fix(core): stop replace from editing the wrong similar block#26976Yash1927 wants to merge 1 commit into
Conversation
Summary of ChangesHello, 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
Using Gemini Code AssistThe 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
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 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
|
|
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. |
🛑 Action Required: Evaluation ApprovalSteering changes have been detected in this PR. To prevent regressions, a maintainer must approve the evaluation run before this PR can be merged. Maintainers:
Once approved, the evaluation results will be posted here automatically. |
There was a problem hiding this comment.
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.
| let replacementText = newBlockWithIndent.join('\n'); | ||
| if (sourceLines[index + searchLinesStripped.length - 1].endsWith('\n')) { | ||
| replacementText += '\n'; | ||
| } |
There was a problem hiding this comment.
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
- When validating string parameters from tools, trim the string first and then check for emptiness to prevent whitespace-only values from being accepted.
Summary
This fixes a case where
replacecould edit the wrong similar block when it had to rely on approximate matching.Details
allow_multipleis not enabled.Related Issues
Fixes #26975
How to Validate
Run:
npx vitest run packages/core/src/tools/edit.test.tsRun:
npx prettier --check packages/core/src/tools/edit.ts packages/core/src/tools/edit.test.tsRun:
npx eslint packages/core/src/tools/edit.ts packages/core/src/tools/edit.test.tsRun:
npm run typecheck --workspace @google/gemini-cli-coreExpected result:
Pre-Merge Checklist