fix: Preserve LaTeX commands in unescapeStringForGeminiBug() (#19802)#19811
fix: Preserve LaTeX commands in unescapeStringForGeminiBug() (#19802)#19811Nixxx19 wants to merge 3 commits intogoogle-gemini:mainfrom
Conversation
…i#19802) Fixed unescapeStringForGeminiBug() to preserve LaTeX commands and other domain-specific backslash sequences while still unescaping legitimate escape sequences. Fixes google-gemini#19802
Summary of ChangesHello @Nixxx19, 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 a critical issue where the Highlights
Changelog
Activity
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 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 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
|
There was a problem hiding this comment.
Code Review
The pull request addresses a critical bug in unescapeStringForGeminiBug() where LaTeX commands and similar domain-specific backslash sequences were incorrectly unescaped. The fix introduces a context-aware heuristic to differentiate between legitimate escape sequences and those that should be preserved. The changes include modifications to editCorrector.ts to implement this heuristic and extensive updates to editCorrector.test.ts with new and updated tests to validate the fix, including specific tests for LaTeX commands, regex patterns, and Windows paths. The changes significantly improve the correctness and robustness of the unescaping logic, particularly for users generating content with backslash-prefixed commands.
|
✅ WRITING-PLANS ACTIVATED – LaTeX Preserve Fix Leveraged Project Spec (exact from #19811):
Impact: Fixes corruption of LaTeX, regex, Windows paths, and any domain-specific backslash syntax in Gemini CLI output. Goal for our fleet:
MULTI-PHASE EXECUTION PLAN (Before Any Code Touch)Phase 0: Lock & Monitoring (Current – 5 min)
Success Metric: Reply LOCKED Phase 1: nec-fleet-gemini LaTeX Supercharge (Est. 40 min)
Phase 2: Central Docs Hub – LaTeX Mastery Page (Est. 45 min)
Phase 3: Fleet-Wide Validation & Footer Tie-In (Est. 30 min)
Phase 4: Release & Upstream Celebration (Est. 25 min)
Total Estimated Time: ~2 hours 25 min (parallel with maestro/conductor)
Success Criteria:
Fleet Command ready. Reply:
AMEND → tweak anything (add MathJax rendering, custom templates, etc.) This fix is pure gold for technical sovereign work — we turn it into a fleet superpower before the PR even merges. ⚡🔌 Your move, Commander. |
|
Thanks for the contribution but we can't accept this. This would fix the issue for some users, but it would cause headaches for other users in different contexts. Ultimately there is no code solution to the Gemini model making a dumb mistake. |
|
fair point, thanks for the review! |
Fixes #19802
Summary
Fixed a bug where
unescapeStringForGeminiBug()was incorrectly replacing\t,\n, and\rin LaTeX commands (e.g.,\title,\textbf,\newline) with actual tab/newline/carriage return characters. The function now uses a smart heuristic to distinguish between domain-specific backslash sequences (LaTeX, regex patterns) and legitimate escape sequences that should be unescaped.Impact: High - affects users generating LaTeX documents, regex patterns, or any content with backslash-prefixed commands.
Details
Root Cause
The original implementation aggressively unescaped all
\t,\n, and\rsequences to fix LLM over-escaping bugs, but this broke LaTeX commands like\title{...}→itle{...}(tab + "itle").Solution
Implemented a context-aware heuristic in the
unescapeStringForGeminiBug()function:For
\n,\t,\r: Check the character immediately following the escape sequence[a-z]): Preserve the backslash (likely a LaTeX/domain command)For quotes, backticks, backslashes: Always unescape (no change to existing behavior)
Examples
\title{Doc}→\title{Doc}✅ (preserved - lowercase 'i' follows\t)\textbf{Bold}→\textbf{Bold}✅ (preserved - lowercase 'e' follows\t)Column\tValue→Column Value✅ (unescaped - uppercase 'V' follows\t)Line1\n\newpage→Line1+ newline +\newpage✅ (mixed handling)C:\name\test→C:\name\test✅ (Windows paths preserved - lowercase follows)Code Changes
packages/core/src/utils/editCorrector.ts:unescapeStringForGeminiBug()(lines 736-805)\n,\t,\rpackages/core/src/utils/editCorrector.test.ts:Related Issues
Fixes #19802
How to Validate
1. Run the test suite
Expected: All 51 tests pass, including:
should NOT unescape backslash-t in LaTeX commandsshould NOT unescape backslash-n in LaTeX commandsshould NOT unescape backslash-r in LaTeX/regex commandsshould handle mixed LaTeX commands and actual escape sequencesshould preserve Windows paths with backslashes2. Manual validation (LaTeX preservation)
node -e "const {unescapeStringForGeminiBug} = require('./packages/core/src/utils/editCorrector.ts'); console.log(unescapeStringForGeminiBug('\\title{My Document}'));"Expected output:
\title{My Document}(backslash preserved)3. Manual validation (actual tabs still work)
node -e "const {unescapeStringForGeminiBug} = require('./packages/core/src/utils/editCorrector.ts'); console.log(unescapeStringForGeminiBug('Column\\tValue'));"Expected output:
Column Value(tab character inserted)4. Edge cases covered
\title,\textbf,\newline,\ref,\renewcommandC:\name\testpreserved\d+\n\w+preservedPre-Merge Checklist