fix(compression): preserve saved memory in state_snapshot contract#21812
fix(compression): preserve saved memory in state_snapshot contract#21812fsgeek wants to merge 15 commits intogoogle-gemini:mainfrom
Conversation
Problem:
- The compression snapshot schema did not define a saved-memory field.
- The compression prompt path did not receive loaded user memory from config.
- This created a contract gap: compression could not be instructed to preserve
durable memory explicitly.
Changes:
- Add <saved_memory> to the required <state_snapshot> schema in modern and
legacy compression prompts.
- Extend compression prompt generation to accept saved-memory context.
- Pass flattened config user memory into PromptProvider.getCompressionPrompt().
- Add/upgrade tests to assert:
- compression schema includes <saved_memory>
- compression prompt includes user-memory context
- chat compression service passes user memory into systemInstruction
Validation:
- npm run test --workspace @google/gemini-cli-core -- \
src/prompts/promptProvider.test.ts \
src/services/chatCompressionService.test.ts
- Result: 2 files passed, 38 tests passed.
Context:
- Complements prior compression-loop fixes by addressing a separate
persistence-contract seam rather than control-flow behavior.
Discovered as part of [Project Arbiter](https://github.com/fsgeek/arbiter),
a system-prompt analysis framework used to evaluate prompt contracts in tools
such as gemini-cli.
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 critical gap in the compression snapshot contract by ensuring that user-defined persistent memory is properly preserved. Previously, the compression mechanism lacked a dedicated field for saved memory and did not receive user memory from the configuration, leading to potential loss of important context during chat history compression. The changes introduce the necessary schema updates, extend prompt generation to include user memory, and integrate this memory into the compression process, thereby strengthening the system's ability to maintain durable user preferences and context. 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
This pull request aims to preserve user memory during chat history compression by adding a <saved_memory> field to the state snapshot contract. However, it introduces a prompt injection vulnerability due to direct interpolation of untrusted user memory content from GEMINI.md files into the system prompt, which could allow an attacker to manipulate the agent's internal state. Additionally, an inconsistent indentation in the legacy prompt snippet could negatively impact model behavior.
| ### SAVED MEMORY CONTEXT | ||
| The following persistent user memory was loaded from context files (for example, global GEMINI memory). You MUST preserve this in <saved_memory> unless newer user instructions in the chat history explicitly supersede it. | ||
| <saved_memory_context> | ||
| ${savedMemoryContext.trim()} |
There was a problem hiding this comment.
The savedMemoryContext variable on this line, which contains untrusted user-defined memory from GEMINI.md files, is directly interpolated into the system prompt for chat history compression without any sanitization or escaping. This creates a high-severity prompt injection vulnerability, as an attacker could inject malicious instructions that manipulate the resulting <state_snapshot>, potentially compromising the agent's behavior. It is recommended to sanitize or escape savedMemoryContext before interpolation, ensuring it cannot break out of the <saved_memory_context> XML tag. Additionally, the template literal for savedMemoryPreservation (which includes this line) has an unintentional leading indentation of 4 spaces, which will add extra whitespace to the prompt for legacy models and could negatively impact model behavior. This indentation should be removed for consistency.
…olation and added an explicit instruction to treat <saved_memory_context> as inert data (not directives). I also added a regression test with a tag-breakout payload to ensure it is escaped and cannot break prompt structure.
|
Hi there! Thank you for your interest in contributing to Gemini CLI. To ensure we maintain high code quality and focus on our prioritized roadmap, we have updated our contribution policy (see Discussion #17383). We only guarantee review and consideration of pull requests for issues that are explicitly labeled as 'help wanted'. All other community pull requests are subject to closure after 14 days if they do not align with our current focus areas. For this reason, we strongly recommend that contributors only submit pull requests against issues explicitly labeled as 'help-wanted'. This pull request is being closed as it has been open for 14 days without a 'help wanted' designation. We encourage you to find and contribute to existing 'help wanted' issues in our backlog! Thank you for your understanding and for being part of our community! |
Problem:
Changes:
Validation:
Context:
Discovered as part of Project Arbiter,
a system-prompt analysis framework used to evaluate prompt contracts in tools
such as gemini-cli.
Summary
Details
Related Issues
How to Validate
Pre-Merge Checklist