feat(context): implement observation masking for tool outputs#18389
feat(context): implement observation masking for tool outputs#18389abhipatel12 merged 4 commits intomainfrom
Conversation
Summary of ChangesHello @abhipatel12, 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 context management feature called "Observation Masking" to prevent context window overflow and mitigate "Context Rot" in long-running agentic loops. By intelligently identifying and masking large tool outputs, the system ensures that the agent's context remains efficient and focused on recent, high-fidelity information, while still providing access to full historical data on demand. Highlights
Changelog
Ignored Files
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
|
|
Size Change: +14.7 kB (+0.06%) Total Size: 23.8 MB
ℹ️ View Unchanged
|
There was a problem hiding this comment.
Code Review
This pull request introduces a robust observation masking feature to manage context window efficiency by offloading large tool outputs. The implementation is well-structured, with a new ObservationMaskingService, corresponding configurations, and thorough tests, and the integration into the GeminiClient is logical. However, the ObservationMaskingService has two high-severity security vulnerabilities: a Path Traversal flaw in how offloaded tool output files are named, and a Prompt Injection vulnerability in how masked snippets are formatted for the LLM. Both issues stem from the use of untrusted data from tool outputs and model responses without proper sanitization or escaping.
| private formatMaskedSnippet(params: MaskedSnippetParams): string { | ||
| const { toolName, filePath, fileSizeMB, totalLines, tokens, preview } = | ||
| params; | ||
| return `[Observation Masked] |
There was a problem hiding this comment.
Hmm if the model sees this there's a lot of noise. I'd imagine it doesn't need all of this information / may distract it.
There was a problem hiding this comment.
Totally fair. I flattened the return a little bit.
When i didn't have the preview, there was a drop in quality.
So that may be helpful to keep. tool name and path also are fine i think.
Should i remove total tokens, line count, and file size? It was to give reference back to the model if it went to go read it.
So like "oh that file is 40k tokens, i should use grep or ranged reads"
But that can be a followup if we actually see model behavior where the model goes and checks these files later.
541a0bd to
2f61bee
Compare
abhipatel12
left a comment
There was a problem hiding this comment.
Thanks for the review. LMK what you think!
| private formatMaskedSnippet(params: MaskedSnippetParams): string { | ||
| const { toolName, filePath, fileSizeMB, totalLines, tokens, preview } = | ||
| params; | ||
| return `[Observation Masked] |
There was a problem hiding this comment.
Totally fair. I flattened the return a little bit.
When i didn't have the preview, there was a drop in quality.
So that may be helpful to keep. tool name and path also are fine i think.
Should i remove total tokens, line count, and file size? It was to give reference back to the model if it went to go read it.
So like "oh that file is 40k tokens, i should use grep or ranged reads"
But that can be a followup if we actually see model behavior where the model goes and checks these files later.
2f61bee to
00b7319
Compare
Summary
Implement observation masking for tool outputs to prevent context window overflow and mitigate "Context Rot" in long-running agentic loops.
Details
This PR introduces a robust context management system called Observation Masking. Key features include:
observations/) and replaced with a structured XML metadata snippet.settingsSchema.tsfor fine-tuning thresholds.Related Issues
Fixes #16955
How to Validate
Automated Tests
npm test packages/core/src/services/observationMaskingService.test.tsManual Verification
.gemini/settings.json:{ "experimental": { "observationMasking": { "enabled": true } } }git logon a large repo).Pre-Merge Checklist