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

fix(app): refresh stale frozen system prompt on session resume

Open Nas01010101 opened this issue 4 months ago • 1 comments

Related Issue

Resolve #2420

Description

Resuming a session unconditionally adopts the _system_prompt record frozen in context.jsonl (KimiCLI.create, introduced in #1417), so:

  • skills added to ~/.kimi/skills/ never appear in resumed sessions,
  • AGENTS.md edits never reach resumed sessions,
  • prompt-affecting config changes are silently ignored after resume,
  • and /fork copies the stale prompt along, so forking can't recover either.

This implements Option A from #2420: when the freshly generated prompt differs from the frozen one, adopt the fresh prompt and persist it; when they're equal, the frozen record is left untouched. The context.jsonl invariant from #1417 — the file always carries the prompt actually in use, readable by frontends/vis tools — is preserved.

Latent bug fixed along the way: Context.write_system_prompt() prepended a new _system_prompt record without removing the existing one, while restore() applies the last _system_prompt record seen — so a refreshed prompt would silently lose to the stale leftover on the very next restore. The rewrite now drops previous _system_prompt records (streaming, byte-preserving for non-prompt lines, atomic via temp file as before).

Real-behavior proof (deterministic, local mock provider)

  1. Create ~/.kimi/skills/skill-one/, run kimi --print -p "first session" → request system prompt lists skill-one. ✅
  2. Add ~/.kimi/skills/skill-two/, run kimi --print -C -p "resumed":
    • on main: skill-one: True, skill-two: False ❌ (bug reproduced)
    • on this branch: skill-one: True, skill-two: True ✅

Verification

  • New tests (fail on main):
    • tests/core/test_context.py::test_write_system_prompt_replaces_existing_record
    • tests/core/test_resume_system_prompt.py — stale prompt refreshed & persisted / unchanged prompt untouched / new session written.
  • uv run pytest tests/core → 862 passed; full tests/ suite green.
  • ruff / pyright clean.

Notes for reviewers

The generated prompt includes volatile builtins (KIMI_NOW, KIMI_WORK_DIR_LS), so in practice the prompt is rewritten on most resumes. That refreshes the stale timestamp/directory listing too (previously the model was told the session's original creation time as "now"). If you'd rather compare only the stable sections to preserve cross-restart provider prompt caching, happy to adjust — see #2420 discussion.

Checklist

  • [x] I have read the CONTRIBUTING document.
  • [x] I have linked the related issue.
  • [x] I have added tests that prove my fix is effective.
  • [x] Changelog updated (manual ## Unreleased entry; make gen-changelog needs kimi API auth).
  • [x] make gen-docs — N/A: no user-facing docs change.

Nas01010101 avatar Jul 19 '26 22:07 Nas01010101

Pushed a follow-up commit extending the fix to subagents: prepare_soul() in src/kimi_cli/subagents/core.py had the same freeze pattern, so a resumed background/foreground subagent kept its stale frozen prompt even after this PR fixed the root session path. It now applies the same refresh-on-diff and persists the fresh prompt; the existing test_prepare_soul resume test was updated to the new contract (refresh + persistence assertion).

Full suite re-run after the change: pytest tests 2960 passed (4 skipped, 1 xfailed), pytest tests_e2e 52 passed, ruff/pyright clean.

Nas01010101 avatar Jul 20 '26 14:07 Nas01010101