MoonshotAI / MoonshotAI/kimi-cli
Refresh system prompt template variables on context compaction
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 11.4k
- Forks
- 1.3k
- Avg merge
- 9h 47m
- Merged PRs (30d)
- 2
Description
What feature would you like to see?
Summary
Currently, BuiltinSystemPromptArgs is rendered once at session start (agent.py::Runtime.create) and stored as an immutable string on the frozen Agent dataclass. This means edits made during a long session to files that feed the system prompt — most notably AGENTS.md — do not take effect until the user restarts the session.
I'd like to propose refreshing these template variables when context compaction occurs. Compaction is a natural "reset point": the context is rebuilt and the system prompt is rewritten via KimiSoul.compact_context → Context.write_system_prompt anyway, which already invalidates the KV cache, so re-rendering at that moment is almost free conceptually.
Motivation
AGENTS.mdis the file users most actively edit during a session to steer the agent. The current "edit, then restart to see the effect" flow is friction-heavy.KIMI_NOWis frozen at session start, so multi-day sessions show a stale date in the system prompt.- Other fields (
KIMI_WORK_DIR_LS,KIMI_ADDITIONAL_DIRS_INFO) also drift over time. - This would likely also mitigate https://github.com/MoonshotAI/kimi-cli/issues/2168, since that issue is in the same family of "state captured at session start becomes stale".
Open design questions
I'd like to align on the direction before writing a PR. The main forks:
-
Where to store the re-render inputs (
system_prompt_path,system_prompt_args)?- (a) Add fields to
Agent(keeps the spec close to the loaded agent, butAgentis frozen and arguably should stay "loaded snapshot"). - (b) Add fields to
Runtime(mutable already; subagents inherit naturally viacopy_for_subagent). - My preference: (b), so that subagents stay in sync.
- (a) Add fields to
-
Partial refresh vs. full refresh of
BuiltinSystemPromptArgs.- Refreshing only
KIMI_AGENTS_MD+KIMI_NOWis the smallest change but leavesKIMI_WORK_DIR_LS/KIMI_ADDITIONAL_DIRS_INFO/KIMI_SKILLSinconsistent. - Full refresh is a few tens of ms (one
list_directory+ possibly skills rescan).
- Refreshing only
-
Failure semantics. Compaction is invoked when the context is near the limit; any new I/O on this path must be wrapped so a stale-but-working prompt is preferred over a hard failure.
Rough scope estimate
About two files (soul/agent.py, soul/kimisoul.py), well under 100 LoC if option (b) + partial-refresh-with-AGENTS.md-only is chosen; closer to ~80 LoC with full refresh. Tests would cover: AGENTS.md edit → compact → new prompt; read failure → compact still succeeds with old prompt; subagent inheritance behavior.
Happy to take this on if the direction is approved. Per CONTRIBUTING.md I'm raising an issue first since the change touches Agent / Runtime lifecycle and I'd rather not write a PR that gets reorganized in review.
Related: https://github.com/MoonshotAI/kimi-cli/issues/2168 — same root cause (state captured once at session start).
概述
目前 SystemPrompt 在会话开始时一次性渲染(见 agent.py::Runtime.create),并以字符串形式存放在 frozen 的 Agent 上。长会话中用户对 AGENTS.md 等文件的修改在当前会话内不会生效,必须重启会话。
希望能在 context compaction 触发时刷新这些模板变量。compaction 本身就会重写 system prompt(KimiSoul.compact_context → Context.write_system_prompt)导致 KV cache 失效,刚好可以用来刷新SystemPrompt。
动机
AGENTS.md是用户在会话过程中最常编辑、用于引导 agent 行为的文件。当前"改完要重启会话才生效"的流程体验割裂。KIMI_NOW在会话开始时被冻结,跨天的长会话里 system prompt 中的日期就是错的。- 其他字段(
KIMI_WORK_DIR_LS、KIMI_ADDITIONAL_DIRS_INFO)也会随时间漂移。 - 此修改可能也会缓解 https://github.com/MoonshotAI/kimi-cli/issues/2168,因为该 issue 与本提案属于同一类问题——"会话开始时捕获的状态会过期"。
待讨论的设计点
希望先就方向对齐再动手写 PR。主要的分歧点:
-
重新渲染所需的输入(
system_prompt_path、system_prompt_args)放在哪里?- (a) 加在
Agent上: agent自带从哪个模版上渲染的提示词,但Agent是 frozen 的,修改会破坏"加载即不变"的语义,而且subagent的system prompt不会同步更新。 - (b) 加在
Runtime上(本身就可变;subagent 通过copy_for_subagent天然继承),但是会让subagent的kv cache失效。
- (a) 加在
-
BuiltinSystemPromptArgs部分刷新还是全量刷新?- 只刷新
KIMI_AGENTS_MD+KIMI_NOW改动最小,但会让KIMI_WORK_DIR_LS/KIMI_ADDITIONAL_DIRS_INFO/KIMI_SKILLS处于不一致状态。 - 全量刷新成本约几十毫秒(一次
list_directory,可能加 skills 重扫)。
- 只刷新
-
失败语义。 compaction 是在上下文濒临爆掉时触发的;该路径上新增的 I/O 必须做兜底,使"用旧 prompt 但 compact 成功"优先于"硬失败"。
改动量预估
约改动 2 个文件(soul/agent.py、soul/kimisoul.py),如果选 (b) + 只刷新 AGENTS.md 则远少于 100 行;全量刷新约 80 行。测试覆盖三个核心场景:AGENTS.md 编辑后 compact 生效、读失败时 compact 仍成功并降级为旧 prompt、subagent 继承行为。
Additional information
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading soul/agent.py, especially Runtime.create and copy_for_subagent, then trace compaction through soul/kimisoul.py and Context.write_system_prompt. The design choices for stored inputs, refresh scope, and failure handling still need maintainer agreement. Done would include agreed behavior and tests for AGENTS.md edits, refresh failures, and subagent inheritance.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- cli
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Quiet
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100