Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

Open
Nas01010101 wants to merge 3 commits into
MoonshotAI:mainMoonshotAI/kimi-cli:mainfrom
Nas01010101:fix-2420-stale-system-promptNas01010101/kimi-cli:fix-2420-stale-system-promptCopy head branch name to clipboard
Open

fix(app): refresh stale frozen system prompt on session resume#2519
Nas01010101 wants to merge 3 commits into
MoonshotAI:mainMoonshotAI/kimi-cli:mainfrom
Nas01010101:fix-2420-stale-system-promptNas01010101/kimi-cli:fix-2420-stale-system-promptCopy head branch name to clipboard

Conversation

@Nas01010101

Copy link
Copy Markdown

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

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

Resuming a session unconditionally adopted the _system_prompt record
frozen in context.jsonl, so skills added to ~/.kimi/skills, AGENTS.md
edits, and prompt-affecting config changes silently never reached
resumed sessions. Adopt the freshly generated prompt when it differs and
persist it; write_system_prompt now drops the previous _system_prompt
record when rewriting, since restoration takes the last record and a
leftover stale one would win again on the next restore.

Resolve MoonshotAI#2420
Copilot AI review requested due to automatic review settings July 19, 2026 22:19

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

prepare_soul froze the persisted subagent system prompt the same way
KimiCLI.create did for root sessions; apply the same refresh-on-diff so
resumed subagents also pick up skills and AGENTS.md changes.
@Nas01010101

Copy link
Copy Markdown
Author

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.

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

Labels

None yet

Projects

None yet

2 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.