fix(session): resume latest session when last_session_id is missing#2453
Open
logicwu0 wants to merge 1 commit into
Open
fix(session): resume latest session when last_session_id is missing#2453logicwu0 wants to merge 1 commit into
logicwu0 wants to merge 1 commit into
Conversation
Session.continue_ relied solely on work_dir_meta.last_session_id, which is only persisted on a clean (SUCCESS) exit. After a Ctrl-C/crash or non-zero exit the id is never written, so `kimi --continue` reported "No previous session found" even though the conversation history still existed on disk (plain `kimi` could still list it). Fall back to the most recently updated non-empty session for the work directory when last_session_id is missing or no longer resolves. Session.list already sorts by updated_at and filters empty sessions. Fixes MoonshotAI#2222
This was referenced Jun 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
kimi --continue(-C) reportsNo previous session found for the working directoryeven when the working directory clearly has history — plainkimishows the previous conversation, but--continuefails. (Fixes #2222)Root cause
Session.continue_relies entirely onwork_dir_meta.last_session_id, returningNonewhen it is unset. But in CLI modelast_session_idis only persisted by_post_runwhen the process exits withExitCode.SUCCESS. After a Ctrl-C, crash, or non-zero exit, the id is never written — while the session content still exists on disk, soSession.list(used elsewhere) can still surface the history. This mismatch is exactly the reported symptom.Fix
Make
Session.continue_fall back to the most recently updated non-empty session for the work directory whenlast_session_idis missing or no longer resolves.Session.listalready sorts byupdated_at(desc) and filters out empty sessions, so the fallback is a small, well-scoped change in one method. The recordedlast_session_idis still preferred when present and valid.Tests
Added regression tests in
tests/core/test_session.py:test_continue_falls_back_to_latest_when_last_missing— history exists butlast_session_idis unset →--continueresumes the most recent session.test_continue_fallback_ignores_empty_sessions— empty sessions are not resumable.uv run pytest tests/core/test_session.py→ 36 passed.ruff format/ruff checkclean.