Skip to content

Session switch crashes: _system_prompt role not handled in Context.restore() #1443

@pablopda

Description

@pablopda

Bug Description

Switching sessions via /sessions crashes with a Pydantic validation error when the target session's context.jsonl contains a {"role": "_system_prompt", ...} entry.

Steps to Reproduce

  1. Start a kimi session and use it normally
  2. Run /sessions and select a different session to switch to
  3. Crash occurs during session restore

Error

pydantic_core._pydantic_core.ValidationError: 1 validation error for Message
role
  Input should be 'system', 'user', 'assistant' or 'tool' [type=literal_error, input_value='_system_prompt', input_type=str]

Root Cause

In kimi_cli/soul/context.py, the restore() method (line ~41-48) filters out internal metadata roles _usage and _checkpoint before passing messages to Message.model_validate(), but does not filter _system_prompt:

# Current code in restore()
if line_json["role"] == "_usage":
    self._token_count = line_json["token_count"]
    continue
if line_json["role"] == "_checkpoint":
    self._next_checkpoint_id = line_json["id"] + 1
    continue
message = Message.model_validate(line_json)  # crashes on _system_prompt

The same issue exists in revert_to() (~line 128-134).

Proposed Fix

Add a catch-all for any internal role (prefixed with _) so that future internal roles are also handled gracefully:

# In restore()
if line_json["role"] == "_usage":
    self._token_count = line_json["token_count"]
    continue
if line_json["role"] == "_checkpoint":
    self._next_checkpoint_id = line_json["id"] + 1
    continue
if line_json["role"].startswith("_"):
    continue
message = Message.model_validate(line_json)
self._history.append(message)
# In revert_to()
if line_json["role"] == "_usage":
    self._token_count = line_json["token_count"]
elif line_json["role"] == "_checkpoint":
    self._next_checkpoint_id = line_json["id"] + 1
elif not line_json["role"].startswith("_"):
    message = Message.model_validate(line_json)
    self._history.append(message)

Environment

  • kimi-cli version: 1.12.0
  • kosong version: 0.42.0
  • Python: 3.13.5
  • OS: Linux (Ubuntu)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions