gemini-cli icon indicating copy to clipboard operation
gemini-cli copied to clipboard

resume latest reopens most recently started session, not most recently used

Open soroush5 opened this issue 2 months ago • 7 comments

Hey! Found that bare gemini --resume reopens the most recently started session instead of the most recently active one — with a long-lived main session plus a newer spike session, resume lands in the stale spike and follow-up chat misfiles there. Happy to PR a fix if that helps.

soroush5 avatar Sep 19 '26 17:09 soroush5

Fix is open as #29411

soroush5 avatar Sep 19 '26 17:09 soroush5

This issue describes a bug in the session resumption logic. Please provide your CLI version to help us investigate.

gemini-cli[bot] avatar Sep 19 '26 18:09 gemini-cli[bot]

Reproduced on current main (upstream @ cfbcaa8df, Sep 19). The bug is in SessionSelector.resolveSession in packages/cli/src/utils/sessionUtils.ts — it picks max startTime instead of max lastUpdated.

soroush5 avatar Sep 19 '26 18:09 soroush5

Effort Analysis: The session resumption logic is localized in packages/cli/src/utils/sessionUtils.ts. Specifically, the resolveSession method handles the RESUME_LATEST constant by calling listSessions() and then explicitly sorting the results by startTime (line 504). While getSessionFiles also sorts by startTime to provide stable 1-based indexing for the --list-sessions command, the 'latest' alias should prioritize activity. The ChatRecordingService in packages/core/src/services/chatRecordingService.ts already maintains a lastUpdated field in the ConversationRecord (line 550), which is updated via updateMetadata every time a message is recorded (line 544). The fix involves a trivial change in sessionUtils.ts to sort by lastUpdated when the latest keyword is provided, making it a low-complexity, single-file logic adjustment with a clear root cause and established metadata support.

gemini-cli[bot] avatar Sep 19 '26 19:09 gemini-cli[bot]

Effort Analysis: The bug resides in the dual model resolution architecture of packages/core. In packages/core/src/config/models.ts, the resolveModel function uses a switch statement that groups PREVIEW_GEMINI_MODEL ('gemini-3-pro-preview') with PREVIEW_GEMINI_MODEL_AUTO, causing explicit model requests to be caught in the same upgrade logic as aliases when useGemini3_1 is true. Additionally, in packages/core/src/config/defaultModelConfigs.ts, the modelIdResolutions for gemini-3-pro-preview explicitly includes contexts that target gemini-3.1-pro-preview under the same condition. A fix requires decoupling these explicit IDs in both the legacy static resolution and the new dynamic ModelConfigService configuration, while ensuring that convenience aliases like 'pro' and 'auto' still benefit from the upgrade. The effort is rated as medium due to the risks associated with core routing logic, the need to maintain consistency with the existing Flash model promotion fix (PR #29252), and the requirement for comprehensive testing across multiple feature flags and model permutations.

gemini-cli[bot] avatar Sep 20 '26 01:09 gemini-cli[bot]

I’d like to work on this. The cause is that --resume latest sorts sessions by startTime in packages/cli/src/utils/sessionUtils.ts, so it picks the most recently started session rather than the most recently used one. My plan is to select by lastUpdated for latest and keep numeric indexes ordered by startTime, so /resume 3 still means the same session. I’ll add a unit test for a long-lived session that has newer activity than a recently started one.

An earlier PR (#29411) was auto-closed only because the issue wasn’t labeled help wanted. Could a maintainer add that label so I can open a PR?

aniyow07 avatar Nov 04 '26 13:11 aniyow07

Go for it. My PR #29411 took the same approach (lastUpdated for latest, numeric indexes unchanged) but was auto-closed for missing the help wanted label, so feel free to reuse anything from it.

soroush5 avatar Nov 11 '26 23:11 soroush5