Skip to content

feat(core): Unified Context Management and Tool Distillation.#24157

Merged
joshualitt merged 2 commits intomainfrom
jl/auto-dis-norm
Mar 30, 2026
Merged

feat(core): Unified Context Management and Tool Distillation.#24157
joshualitt merged 2 commits intomainfrom
jl/auto-dis-norm

Conversation

@joshualitt
Copy link
Copy Markdown
Contributor

@joshualitt joshualitt commented Mar 29, 2026

This commit introduces a comprehensive, multi-tiered approach to managing the
agent's context window, ensuring stability and long-term continuity during
complex multi-turn workflows.

Key Changes:

  1. Unified Configuration: Consolidates history and distillation settings into a new contextManagement schema, configurable via CLI settings.
  2. Progressive Message Normalization: Introduces normalTokenLimit and maximumTokenLimit to dynamically bound message sizes. Messages are kept at full fidelity within a "grace zone" and proportionally compressed as they age or if they exceed extreme limits.
  3. Tool Distillation: ToolOutputDistillationService intercepts massive tool outputs (e.g., heavy compiler logs, raw web fetches), saving the full content to disk and providing the agent with a structurally truncated version. Extremely large outputs trigger a secondary LLM to generate an intent/factual summary.
  4. Intelligent Truncation: Calculates truncation boundaries based on a precise token budget (targetRetainedTokens), falling back to an LLM-generated state summary ("Agent Continuity") to prevent the agent from losing its strategic context when the oldest messages are dropped.

Addresses #21889

@gemini-cli
Copy link
Copy Markdown
Contributor

gemini-cli Bot commented Mar 29, 2026

Hi @joshualitt, 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!

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Mar 29, 2026

🧠 Model Steering Guidance

This PR modifies files that affect the model's behavior (prompts, tools, or instructions).

  • ⚠️ Consider adding Evals: No behavioral evaluations (evals/*.eval.ts) were added or updated in this PR. Consider adding a test case to verify the new behavior and prevent regressions.
  • 🚀 Maintainer Reminder: Please ensure that these changes do not regress results on benchmark evals before merging.

This is an automated guidance message triggered by steering logic signatures.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Mar 29, 2026

Size Change: +19.5 kB (+0.07%)

Total Size: 26.5 MB

Filename Size Change
./bundle/chunk-FW6HO7I7.js 0 B -3.78 MB (removed) 🏆
./bundle/chunk-MI3CKH5U.js 0 B -14.7 MB (removed) 🏆
./bundle/core-347B6MO3.js 0 B -44.5 kB (removed) 🏆
./bundle/devtoolsService-HU7SMKJU.js 0 B -28.4 kB (removed) 🏆
./bundle/interactiveCli-37AAD3D5.js 0 B -1.63 MB (removed) 🏆
./bundle/oauth2-provider-DJFNS4TO.js 0 B -9.16 kB (removed) 🏆
./bundle/chunk-DWQV7A54.js 14.7 MB +14.7 MB (new file) 🆕
./bundle/chunk-YV3EO3SX.js 3.78 MB +3.78 MB (new file) 🆕
./bundle/core-RDSD2CKG.js 44.5 kB +44.5 kB (new file) 🆕
./bundle/devtoolsService-QZCKTJHC.js 28.4 kB +28.4 kB (new file) 🆕
./bundle/interactiveCli-RTLISPLW.js 1.63 MB +1.63 MB (new file) 🆕
./bundle/oauth2-provider-QLUSHMFK.js 9.16 kB +9.16 kB (new file) 🆕
ℹ️ View Unchanged
Filename Size Change
./bundle/chunk-34MYV7JD.js 2.45 kB 0 B
./bundle/chunk-5AUYMPVF.js 858 B 0 B
./bundle/chunk-664ZODQF.js 124 kB 0 B
./bundle/chunk-DAHVX5MI.js 206 kB 0 B
./bundle/chunk-IUUIT4SU.js 56.5 kB 0 B
./bundle/chunk-QGLNP2IW.js 1.96 MB 0 B
./bundle/chunk-RJTRUG2J.js 39.8 kB 0 B
./bundle/chunk-U4FACSVX.js 1.13 kB 0 B
./bundle/devtools-36NN55EP.js 696 kB 0 B
./bundle/dist-T73EYRDX.js 356 B 0 B
./bundle/events-CLX3JQHP.js 418 B 0 B
./bundle/gemini.js 532 kB -258 B (-0.05%)
./bundle/getMachineId-bsd-TXG52NKR.js 1.55 kB 0 B
./bundle/getMachineId-darwin-7OE4DDZ6.js 1.55 kB 0 B
./bundle/getMachineId-linux-SHIFKOOX.js 1.34 kB 0 B
./bundle/getMachineId-unsupported-5U5DOEYY.js 1.06 kB 0 B
./bundle/getMachineId-win-6KLLGOI4.js 1.72 kB 0 B
./bundle/memoryDiscovery-XKQPPGVK.js 922 B 0 B
./bundle/multipart-parser-KPBZEGQU.js 11.7 kB 0 B
./bundle/node_modules/@google/gemini-cli-devtools/dist/client/main.js 222 kB 0 B
./bundle/node_modules/@google/gemini-cli-devtools/dist/src/_client-assets.js 229 kB 0 B
./bundle/node_modules/@google/gemini-cli-devtools/dist/src/index.js 13.4 kB 0 B
./bundle/node_modules/@google/gemini-cli-devtools/dist/src/types.js 132 B 0 B
./bundle/sandbox-macos-permissive-open.sb 890 B 0 B
./bundle/sandbox-macos-permissive-proxied.sb 1.31 kB 0 B
./bundle/sandbox-macos-restrictive-open.sb 3.36 kB 0 B
./bundle/sandbox-macos-restrictive-proxied.sb 3.56 kB 0 B
./bundle/sandbox-macos-strict-open.sb 4.82 kB 0 B
./bundle/sandbox-macos-strict-proxied.sb 5.02 kB 0 B
./bundle/src-QVCVGIUX.js 47 kB 0 B
./bundle/tree-sitter-7U6MW5PS.js 274 kB 0 B
./bundle/tree-sitter-bash-34ZGLXVX.js 1.84 MB 0 B

compressed-size-action

@gemini-cli gemini-cli Bot added priority/p2 Important but can be addressed in a future release. area/agent Issues related to Core Agent, Tools, Memory, Sub-Agents, Hooks, Agent Quality 🔒 maintainer only ⛔ Do not contribute. Internal roadmap item. labels Mar 29, 2026
@joshualitt joshualitt force-pushed the jl/auto-dis-norm branch 3 times, most recently from b2ba95f to 90334df Compare March 29, 2026 18:10
@joshualitt joshualitt changed the title feat(core): Improvements to the AgentHistoryProvider. feat(core): Unified Context Management and Tool Distillation. Mar 29, 2026
This commit introduces a comprehensive, multi-tiered approach to managing the
agent's context window, ensuring stability and long-term continuity during
complex multi-turn workflows.

Key Changes:
1. Unified Configuration: Consolidates history and distillation settings into a
   new `contextManagement` schema, configurable via CLI settings.
2. Progressive Message Normalization: Introduces `normalTokenLimit` and
   `maximumTokenLimit` to dynamically bound message sizes. Messages are kept
   at full fidelity within a "grace zone" and proportionally compressed as
   they age or if they exceed extreme limits.
3. Tool Distillation: `ToolOutputDistillationService` intercepts massive tool
   outputs (e.g., heavy compiler logs, raw web fetches), saving the full
   content to disk and providing the agent with a structurally truncated
   version. Extremely large outputs trigger a secondary LLM to generate an
   intent/factual summary.
4. Intelligent Truncation: Calculates truncation boundaries based on a precise
   token budget (`targetRetainedTokens`), falling back to an LLM-generated
   state summary ("Agent Continuity") to prevent the agent from losing its
   strategic context when the oldest messages are dropped.
@joshualitt joshualitt marked this pull request as ready for review March 30, 2026 15:19
@joshualitt joshualitt requested review from a team as code owners March 30, 2026 15:19
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, 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 robust, multi-tiered system for managing the agent's context window. By centralizing configuration and implementing advanced techniques like progressive message normalization and tool output distillation, the changes ensure stability and long-term continuity during complex, multi-turn workflows while optimizing token usage.

Highlights

  • Unified Context Management: Consolidated history and distillation settings into a new contextManagement schema, replacing individual experimental flags.
  • Progressive Message Normalization: Implemented dynamic token bounding for messages, allowing for full fidelity in a 'grace zone' and proportional compression for older messages.
  • Tool Distillation Service: Added ToolOutputDistillationService to intercept large tool outputs, save them to disk, and provide structurally truncated versions with optional LLM-generated summaries.
  • Intelligent Truncation: Enhanced truncation logic to use a precise token budget and generate state summaries to maintain agent continuity.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a comprehensive Context Management system that replaces the previous experimental agent history truncation logic. Key enhancements include the implementation of a ToolOutputDistillationService, which handles oversized tool outputs by offloading them to disk and providing summarized previews to the LLM, and a refactored AgentHistoryProvider that uses token-based budgeting and proportional message normalization. Configuration schemas and documentation have been updated to support these new settings. Review feedback identifies critical security vulnerabilities related to prompt injection in the summarization logic of the history provider and distillation service, as well as within the web-fetch tool's handling of untrusted content.

Comment thread packages/core/src/services/agentHistoryProvider.ts
Comment thread packages/core/src/services/toolDistillationService.ts
Comment thread packages/core/src/tools/web-fetch.ts
Comment thread packages/core/src/services/toolDistillationService.ts Outdated
Comment thread packages/core/src/config/config.ts Outdated
@joshualitt joshualitt force-pushed the jl/auto-dis-norm branch 3 times, most recently from 730eaf1 to 448f289 Compare March 30, 2026 21:12
@joshualitt joshualitt enabled auto-merge March 30, 2026 21:30
@joshualitt joshualitt added this pull request to the merge queue Mar 30, 2026
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Mar 30, 2026
@joshualitt joshualitt added this pull request to the merge queue Mar 30, 2026
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Mar 30, 2026
@joshualitt joshualitt added this pull request to the merge queue Mar 30, 2026
Merged via the queue into main with commit dfba0e9 Mar 30, 2026
27 of 28 checks passed
@joshualitt joshualitt deleted the jl/auto-dis-norm branch March 30, 2026 22:52
kalenkevich pushed a commit to kalenkevich/gemini-cli that referenced this pull request Apr 3, 2026
afanty2021 pushed a commit to afanty2021/gemini-cli that referenced this pull request Apr 4, 2026
warrenzhu25 pushed a commit to warrenzhu25/gemini-cli that referenced this pull request Apr 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/agent Issues related to Core Agent, Tools, Memory, Sub-Agents, Hooks, Agent Quality 🔒 maintainer only ⛔ Do not contribute. Internal roadmap item. priority/p2 Important but can be addressed in a future release.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants