refactor(core): prompt snippets into layered architecture#23307
refactor(core): prompt snippets into layered architecture#23307gundermanc wants to merge 3 commits intomainfrom
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 introduces a significant refactoring of how LLM prompts are constructed within the 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 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. Footnotes
|
|
Size Change: -4 B (0%) Total Size: 26.1 MB
ℹ️ View Unchanged
|
There was a problem hiding this comment.
Code Review
This pull request introduces a DSL for prompt templating, enhancing maintainability with a type-safe approach and layered architecture. However, it's critical to address high-severity prompt injection vulnerabilities due to the lack of sanitization for dynamic data. Specifically, the xmlSection, section, and renderSnippet functions pose a risk. Additionally, ensure that prompt snippets are joined with newlines instead of commas in renderTemplate and renderSnippet to avoid malformed prompts, adhering to the guideline that variables should ideally not be empty to prevent extra newlines. After fixing these issues, update the unit tests in promptTemplating.test.ts to reflect the corrected output format.
| return Object.values(implementation) | ||
| .map((eachSnippet) => renderSnippet<TOption>(options, eachSnippet)) | ||
| .join(); |
There was a problem hiding this comment.
Using .join() without an argument defaults to a comma separator, which will incorrectly format the final prompt by joining template parts with commas. It's better to use a newline separator.
| return Object.values(implementation) | |
| .map((eachSnippet) => renderSnippet<TOption>(options, eachSnippet)) | |
| .join(); | |
| return Object.values(implementation) | |
| .map((eachSnippet) => renderSnippet<TOption>(options, eachSnippet)) | |
| .join('\n'); |
References
- This rule advises against using conditional rendering logic like
filter(Boolean)to remove empty variables, instead preferring to ensure variables are never empty to prevent extra newlines.
| return snippet | ||
| .map((eachSnippet) => renderSnippet<TOption>(options, eachSnippet)) | ||
| .join(); |
There was a problem hiding this comment.
When rendering an array of snippets (from promptComponent), .join() is used without a separator, which defaults to a comma. This will lead to incorrectly formatted prompts (e.g., part1,part2). These snippets should be joined by newlines.
| return snippet | |
| .map((eachSnippet) => renderSnippet<TOption>(options, eachSnippet)) | |
| .join(); | |
| return snippet | |
| .map((eachSnippet) => renderSnippet<TOption>(options, eachSnippet)) | |
| .join('\n'); |
References
- This rule advises against using conditional rendering logic like
filter(Boolean)to remove empty variables, instead preferring to ensure variables are never empty to prevent extra newlines.
| return Object.values(snippet) | ||
| .map((eachSnippet) => renderSnippet<TOption>(options, eachSnippet)) | ||
| .join(); |
There was a problem hiding this comment.
When rendering a nested template object, .join() is used without a separator, which defaults to a comma. This will lead to incorrectly formatted prompts. These snippets should be joined by newlines.
| return Object.values(snippet) | |
| .map((eachSnippet) => renderSnippet<TOption>(options, eachSnippet)) | |
| .join(); | |
| return Object.values(snippet) | |
| .map((eachSnippet) => renderSnippet<TOption>(options, eachSnippet)) | |
| .join('\n'); |
References
- This rule advises against using conditional rendering logic like
filter(Boolean)to remove empty variables, instead preferring to ensure variables are never empty to prevent extra newlines.
| ): Snippet<TOption> { | ||
| return (options: TOption) => { | ||
| const content = renderSnippet(options, snippet); | ||
| return content ? `<${name}>\n${content}\n</${name}>` : ''; |
There was a problem hiding this comment.
The xmlSection helper wraps the rendered content in XML tags without any sanitization or escaping. If the content (derived from the options object) contains characters that can break out of the XML structure (e.g., </rules>), an attacker can perform prompt injection. This is particularly dangerous if the resulting prompt is used by an agent with sensitive tools (like shell access).
References
- This rule emphasizes the need to escape HTML-like tag characters to prevent prompt injection, which is relevant when content is wrapped in XML tags.
| const content = renderSnippet(options, snippet); | ||
| const level = sectionOptions?.headerLevel ?? 1; | ||
| const hashes = '#'.repeat(level); | ||
| return content ? `${hashes} ${name}\n\n${content}` : ''; |
There was a problem hiding this comment.
The section helper prepends a Markdown header to the rendered content without any sanitization. If the content contains Markdown control characters (e.g., #, \n#), an attacker can inject new headers or manipulate the prompt structure, leading to prompt injection.
References
- This rule highlights the general principle of avoiding user-provided input in LLM content without sanitization to prevent prompt injection, which applies to content used in Markdown sections.
| .map((eachSnippet) => renderSnippet<TOption>(options, eachSnippet)) | ||
| .join(); | ||
| } else if (typeof snippet === 'function') { | ||
| return snippet(options); |
There was a problem hiding this comment.
The renderSnippet function calls dynamic snippet functions with the options object. These functions typically perform string interpolation (e.g., `Your name is ${opt.name}`) using data from options. Since the DSL does not provide any built-in sanitization, untrusted data in options can lead to prompt injection. Developers using this DSL must be explicitly warned to sanitize all dynamic data before interpolation.
References
- This rule directly addresses the risk of prompt injection when user-provided input from the
optionsobject is passed to the LLM without sanitization.
Summary
Refactoring
packages/core/src/prompts/snippets.tsandsnippets.legacy.tsinto a modular, type-safe, and model-specific architecture using thepromptTemplating.tsDSL. This change introduces a layered approach to separate core identity, environmental refinements, and specific feature sets.Details
Proposed Directory Structure
Key Components & Snippets
Root Harness (
root-harness/common.ts)Refinements (
refinements/common.ts)Features (
features/common.ts)Demonstrates complex composition using list interpolation (
each), structural helpers (xmlSection), and multi-line snippets.Implementation Strategy
snippets.tstotemplates/options.ts.templates/system.tsto define the common structure for prompts.snippets.tsinto the three layers (root-harness,refinements,features).gemini-cli.tscomposition.renderTemplateoutput matches the existinggetCoreSystemPromptoutput for equivalent options to prevent regressions.Related Issues
Related to #123 (hypothetical)
How to Validate
promptTemplating.ts.renderTemplatewith the previousgetCoreSystemPromptfor various option sets.Pre-Merge Checklist