Skip to content

fix: clean up /clear and /resume#22007

Merged
mattKorwel merged 2 commits intomainfrom
fix-clear-session-state
Mar 11, 2026
Merged

fix: clean up /clear and /resume#22007
mattKorwel merged 2 commits intomainfrom
fix-clear-session-state

Conversation

@jackwotherspoon
Copy link
Copy Markdown
Collaborator

@jackwotherspoon jackwotherspoon commented Mar 11, 2026

Summary

This PR fixes critical session management bugs in the Gemini CLI where /clear would inadvertently overwrite previous session data and /resume would fail to reset UI telemetry, leading to aggregated metrics.

New footer helped me noticed session was not updating during /clear and that session was getting aggregated...

clear.race.condition.mp4

After fix:

clear.fix.mp4

Details

  • Fix /clear overwriting sessions: In clearCommand.ts, the session ID generation was moved before the chat reset. This ensures that the newly instantiated GeminiChat (and its internal ChatRecordingService) uses the new session ID immediately, creating a distinct file on disk instead of overwriting the previous one.
  • Fix UI aggregation on /resume: Added a uiTelemetryService.clear() call in useSessionBrowser.ts. This triggers a full reset of token counts and updates the session ID in the React UI footer when an older session is loaded.
  • Service & Context Updates:
    • ChatRecordingService: Updated to refresh its local sessionId from the configuration during initialization.
    • UiTelemetryService: Implemented a clear() and hydrate() method to reset accumulated metrics and broadcast the change. Hydrate repopulates metrics on resume so that /stats and footer field are loaded.
    • SessionContext: Added an event listener to respond to telemetry clear events, ensuring the UI stays in sync.

Related Issues

Fixes the reported issue where /clear caused old sessions to vanish from the /resume list and token counts were aggregated across sessions.

How to Validate

  1. Start a session and run a command (e.g., hello). Note the session ID and token count in the footer.
  2. Run /clear.
  3. Start a new session and run another command.
  4. Run /resume.
    • Verification 1: Both the pre-clear and post-clear sessions should appear as distinct entries in the list.
  5. Select the first (older) session to resume.
    • Verification 2: The footer's session ID should immediately update to the old ID.
    • Verification 3: The token count should reset/refresh correctly, no longer showing the aggregate of both sessions.

Pre-Merge Checklist

  • Updated relevant documentation and README (if needed)
  • Added/updated tests (if needed)
  • Noted breaking changes (if any)
  • Validated on required platforms/methods:
    • MacOS
      • npm run
      • npx
      • Docker
      • Podman
      • Seatbelt

@jackwotherspoon jackwotherspoon requested a review from a team as a code owner March 11, 2026 11:16
@gemini-cli gemini-cli Bot added the status/need-issue Pull requests that need to have an associated issue. label Mar 11, 2026
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Mar 11, 2026

Size Change: +3.37 kB (+0.01%)

Total Size: 26.5 MB

Filename Size Change
./bundle/gemini.js 26 MB +3.37 kB (+0.01%)
ℹ️ View Unchanged
Filename Size
./bundle/node_modules/@google/gemini-cli-devtools/dist/client/main.js 221 kB
./bundle/node_modules/@google/gemini-cli-devtools/dist/src/_client-assets.js 227 kB
./bundle/node_modules/@google/gemini-cli-devtools/dist/src/index.js 11.5 kB
./bundle/node_modules/@google/gemini-cli-devtools/dist/src/types.js 132 B
./bundle/sandbox-macos-permissive-open.sb 890 B
./bundle/sandbox-macos-permissive-proxied.sb 1.31 kB
./bundle/sandbox-macos-restrictive-open.sb 3.36 kB
./bundle/sandbox-macos-restrictive-proxied.sb 3.56 kB
./bundle/sandbox-macos-strict-open.sb 4.82 kB
./bundle/sandbox-macos-strict-proxied.sb 5.02 kB

compressed-size-action

@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 addresses critical bugs in the Gemini CLI's session management, specifically preventing /clear from overwriting previous session data and ensuring that /resume correctly resets UI telemetry to avoid aggregated metrics. The changes ensure that each session is properly isolated and that UI statistics accurately reflect the current session.

Highlights

  • Corrected /clear command behavior: The session ID is now generated before chat reset, ensuring new sessions are saved distinctly instead of overwriting previous ones.
  • Resolved UI aggregation on /resume: The UI telemetry is now explicitly cleared when resuming an older session, preventing token counts and other metrics from accumulating across different sessions.
  • Enhanced service synchronization: ChatRecordingService now correctly initializes with the current session ID, UiTelemetryService includes a new clear() method, and SessionContext listens for telemetry clear events to maintain UI consistency.
Changelog
  • packages/cli/src/ui/commands/clearCommand.test.ts
    • Updated clearCommand tests to use the new uiTelemetryService.clear() method.
  • packages/cli/src/ui/commands/clearCommand.ts
    • Moved session ID generation to occur before chat reset to ensure proper session isolation.
    • Introduced a call to uiTelemetryService.clear() with the new session ID.
    • Removed an unnecessary chatRecordingService.initialize() call.
  • packages/cli/src/ui/contexts/SessionContext.test.tsx
    • Added a new test case to verify that the SessionStatsProvider correctly updates the session ID and resets statistics when a uiTelemetryService 'clear' event is emitted.
  • packages/cli/src/ui/contexts/SessionContext.tsx
    • Implemented a handleClear event listener for uiTelemetryService to reset session statistics and update the session ID in the UI.
  • packages/cli/src/ui/hooks/useSessionBrowser.test.ts
    • Modified tests to mock and assert calls to uiTelemetryService.clear() when a session is resumed.
  • packages/cli/src/ui/hooks/useSessionBrowser.ts
    • Added a call to uiTelemetryService.clear() when resuming an existing session to reset UI telemetry.
  • packages/core/src/services/chatRecordingService.ts
    • Ensured ChatRecordingService retrieves the current session ID from the configuration during its initialization process.
  • packages/core/src/telemetry/uiTelemetry.test.ts
    • Added comprehensive tests for the new clear method in UiTelemetryService, confirming it resets internal metrics and emits appropriate events.
  • packages/core/src/telemetry/uiTelemetry.ts
    • Introduced a clear method to reset all accumulated UI telemetry metrics and emit 'clear' and 'update' events.
Activity
  • No human activity (comments, reviews, etc.) has been recorded on this pull request yet.
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 effectively addresses two critical bugs related to session management. The fix for the /clear command preventing session data from being overwritten is well-reasoned, correctly reordering session ID generation before the chat reset. The addition of uiTelemetryService.clear() on session resume is a clean solution to the metric aggregation issue. The related changes in ChatRecordingService, UiTelemetryService, and SessionContext are all necessary and correctly implemented to support these fixes. The test files have also been updated appropriately to cover the new logic. Overall, this is a solid contribution that improves the stability and correctness of the CLI.

@jackwotherspoon jackwotherspoon changed the title fix: session management and UI aggregation fix: clean up /clear and /resume Mar 11, 2026
Copy link
Copy Markdown
Collaborator

@mattKorwel mattKorwel left a comment

Choose a reason for hiding this comment

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

This change looks solid, thanks for adding. I think we need to follow on and figure out how we relate lifetime of terminal stats now that this is properly cleared out.

@mattKorwel mattKorwel added this pull request to the merge queue Mar 11, 2026
Merged via the queue into main with commit b804fe9 Mar 11, 2026
48 of 49 checks passed
@mattKorwel mattKorwel deleted the fix-clear-session-state branch March 11, 2026 15:36
e-kotov pushed a commit to e-kotov/gemini-cli that referenced this pull request Mar 11, 2026
JaisalJain pushed a commit to JaisalJain/gemini-cli that referenced this pull request Mar 11, 2026
liamhelmer pushed a commit to badal-io/gemini-cli that referenced this pull request Mar 12, 2026
ruomengz pushed a commit that referenced this pull request Mar 13, 2026
SUNDRAM07 pushed a commit to SUNDRAM07/gemini-cli that referenced this pull request Mar 30, 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

status/need-issue Pull requests that need to have an associated issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants