Skip to content

fix(core): persist compression state in session file for correct resume#21334

Closed
amircodota wants to merge 20 commits intogoogle-gemini:mainfrom
codota:fix/session-resume-compression-history
Closed

fix(core): persist compression state in session file for correct resume#21334
amircodota wants to merge 20 commits intogoogle-gemini:mainfrom
codota:fix/session-resume-compression-history

Conversation

@amircodota
Copy link
Copy Markdown

@amircodota amircodota commented Mar 5, 2026

Summary

Session resume (--resume) ignored compression history, restoring the full uncompressed conversation. This caused context overflow and degraded performance on resumed sessions that had been compressed via /compress.

Details

The root cause: tryCompressChat replaced the in-memory history but never updated the on-disk session file. On --resume, convertSessionToClientHistory read all historical messages from the file, effectively re-inflating the context.

Fix: Introduce a compression_marker message type in ConversationRecord:

  • chatRecordingService.ts — Added compression_marker type with compressedHistory: Content[] payload, and a recordCompressionMarker() method to append it to the session file.
  • client.ts — After successful compression, call recordCompressionMarker(newHistory) before re-initializing the chat.
  • sessionUtils.ts (core)convertSessionToClientHistory now finds the latest compression_marker and uses its compressedHistory as the base, processing only subsequent messages.
  • sessionUtils.ts (cli)convertSessionToHistoryFormats renders compression_marker as an INFO message in the UI.

Related Issues

Fixes #20803

How to Validate

  1. Start a session: gemini
  2. Send a few messages to build up history
  3. Run /compress — should compress successfully
  4. Quit with /quit
  5. Resume: gemini --resume latest
  6. Verify the resumed session uses the compressed history (check token count in verbose mode or send a follow-up message that depends on prior context)

Alternatively, run the new integration test:

npx vitest run integration-tests/compress-resume.test.ts

And the unit tests:

npx vitest run packages/core/src/services/chatRecordingService.test.ts
npx vitest run packages/core/src/utils/sessionUtils.test.ts
npx vitest run packages/core/src/core/client.test.ts
npx vitest run packages/cli/src/ui/hooks/useSessionBrowser.test.ts

Pre-Merge Checklist

  • Updated relevant documentation and README (if needed) — N/A, internal bug fix
  • Added/updated tests (if needed)
  • Noted breaking changes (if any) — none
  • Validated on required platforms/methods:
    • MacOS — no macOS environment available
    • Windows — no Windows environment available
    • Linux
      • npm run

When context compression occurs, the compressed history replaces the
in-memory API history but the session file on disk retains all original
messages. On --resume, convertSessionToClientHistory replays every
message from the file, restoring the full uncompressed history and
potentially overflowing the context window.

Record a compression_marker message in the session file when compression
succeeds. The marker stores the compressed API history (Content[]). On
resume, convertSessionToClientHistory finds the last marker and uses its
compressedHistory as the base, converting only post-marker messages.

Fixes google-gemini#20803
Verifies that a session compressed via /compress can be correctly
resumed with --resume latest. The test checks that:
- A compression_marker is written to the session file on disk
- The resumed session completes without context overflow errors

Golden responses recorded from gemini-2.5-pro.
@amircodota amircodota requested a review from a team as a code owner March 5, 2026 22:21
@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 resolves a critical bug where resuming a chat session after compression would incorrectly restore the full, uncompressed conversation history, leading to context overflow and degraded performance. The solution introduces a mechanism to explicitly record the compressed state within the session file using a new 'compression_marker' message type. This ensures that when a session is resumed, the system accurately reconstructs the chat history from the last known compressed point, thereby maintaining performance and context integrity.

Highlights

  • Persistent Compression State: Introduced a new compression_marker message type to ConversationRecord to persist the compressed chat history in the session file, ensuring that resumed sessions correctly reflect the compressed state.
  • Session Resume Logic Update: Modified convertSessionToClientHistory in sessionUtils.ts (core) to identify and utilize the latest compression_marker. This allows resumed sessions to start from the compressed history, preventing context overflow and performance issues.
  • Compression Marker Recording: Implemented recordCompressionMarker() in chatRecordingService.ts to append the compressed history to the session file after a successful /compress operation, and integrated its call into client.ts.
  • UI Representation: Updated convertSessionToHistoryFormats in sessionUtils.ts (cli) to render compression_marker messages as informational messages in the UI, providing clarity to the user about when compression occurred.
  • Comprehensive Testing: Added a new integration test (compress-resume.test.ts) and updated existing unit tests across chatRecordingService.test.ts, sessionUtils.test.ts, and client.test.ts to validate the new compression marker functionality and session resume behavior.
Changelog
  • integration-tests/compress-resume.responses
    • Added new test responses to support the compress-resume integration test.
  • integration-tests/compress-resume.test.ts
    • Added a new integration test to verify that compressed sessions can be correctly resumed using the new compression marker.
  • packages/cli/src/ui/hooks/useSessionBrowser.test.ts
    • Added a unit test to confirm that compression_marker messages are correctly rendered as info messages in the UI history.
  • packages/cli/src/utils/sessionUtils.ts
    • Modified convertSessionToHistoryFormats to map the new compression_marker type to MessageType.INFO for UI display.
  • packages/core/src/core/client.test.ts
    • Updated mock ChatRecordingService to include recordCompressionMarker.
    • Added a test case to ensure recordCompressionMarker is invoked with the correct history after successful chat compression.
  • packages/core/src/core/client.ts
    • Integrated a call to currentRecordingService.recordCompressionMarker(newHistory) within tryCompressChat to persist the compressed state.
  • packages/core/src/services/chatRecordingService.test.ts
    • Added unit tests for recordCompressionMarker to verify it correctly records the marker and compressed history to the session file.
    • Added a test to ensure recordCompressionMarker does not throw an error if the conversation file is not initialized.
  • packages/core/src/services/chatRecordingService.ts
    • Defined a new compression_marker type within ConversationRecordExtra to store compressed history.
    • Implemented recordCompressionMarker method to create and append a compression_marker message to the conversation file.
  • packages/core/src/utils/sessionUtils.test.ts
    • Added test cases for convertSessionToClientHistory to validate correct handling of compression markers, including using the latest marker and processing only subsequent messages.
    • Added a test for handling a compression marker with no subsequent messages.
  • packages/core/src/utils/sessionUtils.ts
    • Refactored convertSessionToClientHistory to find the latest compression_marker and use its compressedHistory as the base for client history.
    • Ensured that only messages occurring after the latest compression_marker are processed when reconstructing client history.
Activity
  • The pull request introduces a significant fix for session resume functionality related to chat compression.
  • The author provided a detailed summary and explanation of the problem and the proposed solution.
  • New integration tests were added to cover the compress-then-resume scenario, demonstrating a commitment to robust testing.
  • Existing unit tests were updated and new ones added to ensure the new compression_marker logic functions as expected across different components.
  • The pre-merge checklist indicates that tests were added/updated and validated on Linux.
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
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 correctly addresses the issue of session resume ignoring compression history by introducing a compression_marker in the session file. The implementation is robust, with changes to persist the marker upon compression and correctly reconstruct the history from it upon resume. The code is well-tested with new unit and integration tests. I have one suggestion to improve the assertion in the new integration test to make it more robust.

Comment on lines +81 to +82
expect(result).toBeDefined();
expect(result.length).toBeGreaterThan(0);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

The current assertions only verify that the resumed command produces some output, not that the context was correctly restored from the compressed state. To make this test more robust and directly validate the fix, you should assert that the model's response contains content from the original conversation, which would only be possible if the compressed history was used.

Based on the mocked responses, the model should reply with a summary of the robot story. You can assert that the output includes keywords like 'robot' or 'Unit 734'.

Suggested change
expect(result).toBeDefined();
expect(result.length).toBeGreaterThan(0);
expect(result).toContain('robot');
expect(result).toContain('Unit 734');

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good catch — updated the assertions to verify the resumed session actually recalls content from the compressed history (robot and Unit 734). Fixed in fbedf72.

@gemini-cli gemini-cli bot added the area/agent Issues related to Core Agent, Tools, Memory, Sub-Agents, Hooks, Agent Quality label Mar 5, 2026
Assert that the resumed session output contains 'robot' and 'Unit 734'
from the compressed history, not just that it produced any output.
@gemini-cli
Copy link
Copy Markdown
Contributor

gemini-cli bot commented Mar 20, 2026

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!

@gemini-cli gemini-cli bot closed this Mar 20, 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Session resume ignores compression history, restoring full uncompressed conversation

1 participant