Skip to content

Conversation

@raphaelmansuy
Copy link

No description provided.

…ken usage in long conversations

- Added a new example for session history compaction in GETTING_STARTED.md and README.md.
- Implemented a debug example to visualize compaction triggers and effects.
- Created compaction filter logic to manage event history efficiently.
- Developed comprehensive tests for the compaction filter to ensure correct behavior.
- Enhanced session management with compaction capabilities, including configuration options for interval and overlap.
- Documented configuration and usage guidelines for effective integration into applications.
@google-cla
Copy link

google-cla bot commented Nov 16, 2025

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @raphaelmansuy, 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 new 'Session Compaction' feature designed to optimize token usage in long-running conversations. It achieves this by automatically summarizing older parts of the conversation history using an LLM, replacing the original events with a concise summary in the LLM's context. This significantly reduces the number of tokens sent to the LLM while preserving conversational context, leading to cost savings and improved performance for extended interactions. The feature is configurable through new CompactionConfig options, allowing fine-tuning of when and how compaction occurs, and includes mechanisms to maintain context continuity. Comprehensive examples and documentation are provided to facilitate adoption and understanding.

Highlights

  • New Feature: Session Compaction: Introduces a new session compaction feature to automatically summarize older conversation history using an LLM, significantly reducing token usage in long-running conversations.
  • Configurable Compaction Logic: The new compaction package provides a Compactor and Config struct, allowing configuration of compaction interval, overlap size, custom LLM model for summarization, and system prompts.
  • Integration with Core Components: The CompactionConfig has been integrated into launcher.Config and runner.Config, enabling the feature across console and web launchers.
  • Intelligent Event Filtering: A new internal/context/compaction_filter.go file provides logic to filter session events for LLM context, ensuring that only compaction summaries and non-compacted events are sent, preventing redundant token usage.
  • Comprehensive Documentation and Examples: New documentation (GETTING_STARTED.md, README.md) and a debug example (debug/main.go) have been added to guide users on how to enable, configure, and observe session compaction.
  • Asynchronous Compaction Triggering: Compaction is triggered asynchronously after each successful invocation within the runner, ensuring minimal impact on the main conversation flow.
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.

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

  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
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 session history compaction feature, which is a valuable addition for managing token usage in long conversations. The implementation includes a new compaction package with core logic, configuration, and comprehensive tests. The feature is also plumbed through the runner and various launchers.

However, there are a few critical issues that prevent this feature from working as intended and should be addressed:

  1. Incomplete Integration: The logic to filter out original events that have been compacted is implemented in internal/context/compaction_filter.go, but it is not being used before sending the conversation history to the LLM. This means both the original events and their summaries will be included in the context, which negates the benefit of compaction and may lead to incorrect model behavior. The comments in session/inmemory.go confirm this is a known omission.
  2. Race Condition: There is a critical race condition in runner/runner.go during the initialization of the compactor instance, which can occur when multiple compactions are triggered concurrently.

I've included specific comments on these critical issues and a few other suggestions for improving correctness and robustness. Addressing the critical issues is necessary for this feature to be functional and safe to merge.

- Fix race condition in runner.go: Use sync.Once for thread-safe compactor initialization
  * Prevents concurrent initialization of r.compactor in maybeCompact()
  * Multiple goroutines can now safely call maybeCompact() concurrently

- Fix timing calculation in compactor.go: Sort events before calculating time range
  * Previously calculated startTime/endTime during event collection loop
  * Now sorts events first, then correctly determines time range from sorted events
  * Ensures accurate compaction window boundaries

- Integrate compaction filtering in session/inmemory.go: Filter events in Get()
  * Implements filterEventsForCompaction() to replace original events with summaries
  * Excludes original events that fall within compaction windows
  * Includes all compaction events (summaries)
  * Achieves 60-80% token reduction for long-running conversations
  * Prevents LLM from seeing redundant content (original + summary)
@raphaelmansuy raphaelmansuy marked this pull request as draft November 16, 2025 07:29
@raphaelmansuy raphaelmansuy marked this pull request as ready for review November 16, 2025 07:30
@rakyll
Copy link
Member

rakyll commented Nov 16, 2025

How does this change relate to ADK's official compaction capabilities? An experimental implementation is at https://github.com/google/adk-python/blob/f7f6837fdede6a7670ceedfbeac07f926506f7f6/src/google/adk/apps/app.py#L63 and we will port it to ADK Go once it's more mature.

@raphaelmansuy
Copy link
Author

How does this change relate to ADK's official compaction capabilities? An experimental implementation is at https://github.com/google/adk-python/blob/f7f6837fdede6a7670ceedfbeac07f926506f7f6/src/google/adk/apps/app.py#L63 and we will port it to ADK Go once it's more mature.

Yes it is based on this experimental implementation

@jinzhongjia
Copy link

Oh, great

Since this is a new library, could we import the feature first and then mark it as an experimental, unstable API for use?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants