Skip to content

fix(core-tools): bypass LLM correction for JSON and IPYNB files in write_file and replace#28223

Open
amelidev wants to merge 3 commits into
google-gemini:mainfrom
amelidev:b_446760218
Open

fix(core-tools): bypass LLM correction for JSON and IPYNB files in write_file and replace#28223
amelidev wants to merge 3 commits into
google-gemini:mainfrom
amelidev:b_446760218

Conversation

@amelidev

Copy link
Copy Markdown
Contributor

Summary

This is a surgical PR that resolves a critical failure where both the write_file and replace tools corrupted or failed to modify .ipynb (Jupyter Notebook) and .json files. This fix is designed to be highly focused and target only this issue to avoid regressions.

Details

  • Surgical Focus: This implementation strictly solves the .ipynb / .json file corruption issue and does nothing else. Any secondary or automatic reviews (including other Gemini reviews) must take this explicit surgical design constraint into account.
  • Root Cause resolved in write_file: Bypassed calling ensureCorrectFileContent (which invokes unescapeStringForGeminiBug) for .json, .ipynb, .jsonc, and .json5 extensions. This prevents unescaping of JSON strings (which corrupts backslashes like \\n and \\").
  • Root Cause resolved in replace: Bypassed invoking attemptSelfCorrection (which calls LLM-based FixLLMEditWithInstruction) for .json, .ipynb, .jsonc, and .json5 extensions. This prevents the LLM from trying to output search/replace strings on structured files with incorrect JSON-level escaping, avoiding file corruption on failure.

Related Issues

Resolves the issue with .ipynb and .json corruption reported in issue.md.

How to Validate

Run relevant unit tests

Ensure both test files run and pass perfectly:

npx vitest run packages/core/src/tools/write-file.test.ts
npx vitest run packages/core/src/tools/edit.test.ts

Run build and lint

npm run build
npx eslint packages/core/src/tools/edit.ts packages/core/src/tools/edit.test.ts packages/core/src/tools/write-file.ts packages/core/src/tools/write-file.test.ts

Pre-Merge Checklist

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

@amelidev amelidev requested a review from a team as a code owner June 30, 2026 21:57
@github-actions github-actions Bot added the size/m A medium sized PR label Jun 30, 2026
@github-actions

Copy link
Copy Markdown

📊 PR Size: size/M

  • Lines changed: 101
  • Additions: +92
  • Deletions: -9
  • Files changed: 4

@github-actions

github-actions Bot commented Jun 30, 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.

@amelidev

Copy link
Copy Markdown
Contributor Author

/gemini review

@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 introduces a targeted fix to prevent file corruption in .json and .ipynb files when using the write_file and replace tools. By bypassing LLM-based correction and unescaping logic for these specific file types, the system now preserves the integrity of structured data that was previously being incorrectly modified.

Highlights

  • Prevention of File Corruption: Bypassed LLM-based correction logic for .json, .ipynb, .jsonc, and .json5 files to prevent unintended unescaping and structural corruption.
  • Tool-Specific Fixes: Updated both write_file and replace tools to explicitly skip correction steps for structured data files.
  • Test Coverage: Added new unit tests in edit.test.ts and write-file.test.ts to verify that correction logic is bypassed for the specified file extensions.
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-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 disables LLM correction for JSON and Jupyter Notebook files (specifically targeting .json, .ipynb, .jsonc, and .json5 extensions) during file editing and writing tasks. This is implemented in edit.ts and write-file.ts by checking the file extension and skipping the correction steps if a match is found. Corresponding unit tests have been added to edit.test.ts and write-file.test.ts to verify this behavior. No review comments were provided, and I have no additional feedback.

@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 skips LLM correction for JSON and Jupyter Notebook files (.json, .ipynb, .jsonc, and .json5) in both the edit and write-file tools to prevent potential corruption of structured data formats. Unit tests have been added to verify that the correction functions are not called for these file extensions. I have no feedback to provide as there are no review comments.

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

The only minor nit I might have is that since the list(s) of isJsonOrIpynb fileExt(s) are identical and re-used, it might be better to store and export the list as a const, or export the check for the lists inclusion and import them. Then the logic for determining which files do or do not qualify for this check, should it ever need to be changed, can be updated in a singular location without fear of de-synchronizing the logic of the checks.

LGTM either way.

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants