Skip to content

fix(session): resume latest session when last_session_id is missing#2453

Open
logicwu0 wants to merge 1 commit into
MoonshotAI:mainfrom
logicwu0:fix/2222-continue-fallback
Open

fix(session): resume latest session when last_session_id is missing#2453
logicwu0 wants to merge 1 commit into
MoonshotAI:mainfrom
logicwu0:fix/2222-continue-fallback

Conversation

@logicwu0

@logicwu0 logicwu0 commented Jun 15, 2026

Copy link
Copy Markdown

Problem

kimi --continue (-C) reports No previous session found for the working directory even when the working directory clearly has history — plain kimi shows the previous conversation, but --continue fails. (Fixes #2222)

Root cause

Session.continue_ relies entirely on work_dir_meta.last_session_id, returning None when it is unset. But in CLI mode last_session_id is only persisted by _post_run when the process exits with ExitCode.SUCCESS. After a Ctrl-C, crash, or non-zero exit, the id is never written — while the session content still exists on disk, so Session.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 when last_session_id is missing or no longer resolves. Session.list already sorts by updated_at (desc) and filters out empty sessions, so the fallback is a small, well-scoped change in one method. The recorded last_session_id is 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 but last_session_id is unset → --continue resumes 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 check clean.


Open in Devin Review

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

@devin-ai-integration devin-ai-integration Bot left a comment

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.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Open in Devin Review

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

Labels

None yet

Projects

None yet

1 participant