fix(core): preserve dollar sequences in prompt template substitutions#28055
fix(core): preserve dollar sequences in prompt template substitutions#28055nramanath wants to merge 2 commits 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 a template substitution bug where content containing dollar-sign sequences was being incorrectly parsed by JavaScript's replace method. By switching to functional replacements, the system now treats injected descriptions as literal strings, ensuring that prompt templates remain intact regardless of their content. 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. |
|
📊 PR Size: size/M
|
🛑 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 updates applySubstitutions in packages/core/src/prompts/utils.ts to use replacer functions instead of replacement strings in String.prototype.replace(), ensuring that special dollar sequences (like $$ and $&) in substituted content are preserved verbatim. Corresponding unit tests are added to verify this behavior. The review feedback points out an issue with the dynamic RegExp construction for tool-specific placeholders (e.g., ${toolName}_ToolName). Specifically, if a tool name contains special characters, the unescaped interpolation, the use of word boundaries (\b), and unescaped braces can cause matching failures or regex syntax errors. A suggestion is provided to escape the dynamically generated variable name and remove the word boundaries.
|
@googlebot I signed it. |
Summary
This PR fixes a bug in system prompt template substitutions that corrupts content containing
$sequences (e.g.$$,$',$&) inside skill, sub-agent, or tool descriptions.Details
applySubstitutions()inpackages/core/src/prompts/utils.tswas performing template substitutions using the string form ofString.prototype.replace()(e.g.result.replace(/\${AgentSkills}/g, skillsPrompt)). Because the replacement values are strings, JavaScript parses special sequences like$'(duplicates the tail of the prompt),$$(collapses to$), and$&(inserts the match itself) inside the descriptions.We fixed this by passing callback functions instead (e.g.
() => skillsPrompt), which forces JavaScript to treat the replacement strings literally.We also added regression unit tests in
packages/core/src/prompts/utils.test.tsto ensure this behavior remains robust in the future.Related Issues
Fixes #27993
How to Validate
Run the prompt utility unit tests:
npm test -w @google/gemini-cli-core -- src/prompts/utils.test.tsRun the workspace-wide validation checks:
npm run build && npm run lintPre-Merge Checklist