feat(core): enhanced anchored iterative context compression with self-verification#15710
Conversation
Summary of ChangesHello @rmedranollamas, 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 significantly upgrades the core context compression capabilities for AI agents, introducing a more robust and intelligent system for managing conversational state. It integrates a self-correction mechanism and anchored updates to existing snapshots, ensuring critical information is retained and refined over time. Additionally, it provides new configuration options for automatic management of MCP servers and experimental model routing, enhancing the system's operational resilience and flexibility. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. 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 introduces significant enhancements to context compression, including a structured XML snapshot, anchored iteration, and self-verification. It also adds auto-restarting for MCP servers and an experimental model router. My review focuses on potential race conditions and resource management. I've identified a critical issue in the MCP client manager related to unsafe iteration and a high-severity issue in the chat compression service concerning uncancellable background tasks. Addressing these will improve the robustness and efficiency of the new features.
b14ada6 to
5cf14a8
Compare
|
Very interesting PR! Can you please open a corresponding issue for tracking? Also, it seems that the PR combines a few different issues: memory compression, mcp auto-restart, model router configuration. It would be easier to review if they are split to individual PR's. |
611b187 to
b3f1342
Compare
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces an enhanced context compression mechanism with a two-turn self-verification process and anchored iterative logic, which is a significant improvement. However, a high-severity prompt injection vulnerability has been identified in the new chat compression logic, where direct use of chat history in prompts could allow an attacker to manipulate the agent's memory and behavior. Additionally, there are two high-severity issues: one related to handling potentially empty summaries that could corrupt chat history, and another concerning the lack of cancellation support for the compression process, potentially leading to resource leaks and race conditions. Addressing these points will make the new feature more robust, reliable, and secure.
|
filled #16500 for this work, and cleaned the PR up. rebased and passed tests. |
|
Hi @rmedranollamas, thank you so much for your contribution to Gemini CLI! We really appreciate the time and effort you've put into this. We're making some updates to our contribution process to improve how we track and review changes. Please take a moment to review our recent discussion post: Improving Our Contribution Process & Introducing New Guidelines. Key Update: Starting January 26, 2026, the Gemini CLI project will require all pull requests to be associated with an existing issue. Any pull requests not linked to an issue by that date will be automatically closed. Thank you for your understanding and for being a part of our community! |
|
attached to #16500 |
|
Looks like you may have some test failures here too |
ffbc0ef to
380afed
Compare
|
/gemini review |
- Add security warnings against prompt injection in compression prompt. - Clarify artifact_trail purpose in compression prompt. - Fix unsafe iteration in McpClientManager.restart. - Ensure all chatCompressionService tests use correct tokenLimit mocks.
|
I've addressed the review comments:
All tests passed locally. |
There was a problem hiding this comment.
Code Review
This pull request introduces a sophisticated enhancement to the chat history compression mechanism, incorporating an anchored, iterative approach with a self-verification step. The changes are well-implemented, with a more structured XML snapshot format and corresponding updates to the compression service and tests. The two-turn verification process is a clever way to improve summary quality. I've identified one high-severity issue in how the final summary is determined, which could lead to discarding a valid summary if the verification step returns only whitespace. Overall, this is a great improvement.
|
Fixed the subtle bug in |
- Add security warnings against prompt injection in compression prompt. - Clarify artifact_trail purpose in compression prompt. - Fix unsafe iteration in McpClientManager.restart. - Ensure all chatCompressionService tests use correct tokenLimit mocks.
50fbdce to
f3b47eb
Compare
|
Fixed integration test failures by:
|
Head branch was pushed to by a user without write access
… with self-verification turn based on Factory.ai research
- Add security warnings against prompt injection in compression prompt. - Clarify artifact_trail purpose in compression prompt. - Fix unsafe iteration in McpClientManager.restart. - Ensure all chatCompressionService tests use correct tokenLimit mocks.
…ication is empty - Address gemini-code-assist review comment regarding subtle fallback bug. - Add regression test for empty verification response.
|
Rebased onto main and verified. Ready for another look! @NTaylorMullen |
db5d831 to
a5ce377
Compare
…ve context compression with self-verification (google-gemini#15710)
Summary
This PR implements an enhanced context compression mechanism inspired by Factory.ai's research on evaluating context compression for AI agents. It shifts from simple summarization to a structured, anchored, and self-verifying distillation process.
Details
<state_snapshot>schema inpackages/core/src/core/prompts.tsto include:<artifact_trail>: Tracks the evolution of critical files and symbols.<active_constraints>: Explicitly preserves user-defined or discovered constraints.<task_state>: Refined from 'plan' to emphasize continuity and the immediate next focus.ChatCompressionServicenow detects existing snapshots in history and instructs the model to integrate and update them rather than starting from scratch, preventing 'information drift'.Related Issues
Closes: #16500
How to Validate
Run the core unit tests:
Pre-Merge Checklist