400 "role 'system' must not be empty" 导致整个会话无法继续:dynamic tool schema 注入的 system 消息 content 为空数组
What version of Kimi Code is running?
kimi code CLI 版本2.4.0,kimi code Desktop 1.2.1.0
Which open platform/subscription were you using?
通过企业网关API调用
Which model were you using?
切换k2.6, k2.7-code,k2.8-preview,k3.1后会话都报错无法继续
What platform is your computer?
windows 11 x64
What issue are you seeing?
问题描述
长会话中通过 select_tools 动态加载工具后,客户端会注入一条 role: system 的消息用于携带新工具的 schema,但该消息的 content 为空数组 []。此后该会话的每一次模型请求都会被服务端拒绝:
400 Invalid request: the message at position 363 with role 'system' must not be empty
(provider.api_error · HTTP 400)
点「继续」、重试、/compact 均无效(/compact 本身也是一次模型调用,同样被毒化),整个会话只能 /clear 丢弃。
环境
- Kimi Code Desktop 1.2.1.0(Windows 11)
- 引擎(core)版本:2.4.0(复现时;现已自动更新到 2.4.1,changelog 未提及修复此项)
- 复现于普通长会话
定位证据
会话 wire 日志(~/.kimi-code/sessions/<workspace>/<session>/agents/main/wire.jsonl)中,动态加载工具的事件序列:
{"type":"context.append_loop_event","event":{"type":"tool.result","toolCallId":"select_tool..."}} // select_tools 加载 mcp__desktop_browser__run
{"type":"context.append_message","message":{
"role":"system",
"content":[], // <-- 空数组,触发 400 的直接原因
"toolCalls":[],
"tools":[{"name":"mcp__desktop_browser__run", ...}], // schema 放在了 tools 字段
"origin":"{'kind': 'injection', 'variant': 'dynamic_tool_schema'}"
}}
{"type":"context.append_message","message":{"role":"user","content":[{"type":"text","text":"<system-reminder>..."}]}}
- 报错中的
position 363与该注入消息在请求消息数组中的位置吻合; - 该会话历史中共 46 条存储消息、仅这 1 条
role: system,且content为[]; - 完全稳定复现:只要历史里存在这条消息,每次请求必 400。
根因分析
origin.variant = dynamic_tool_schema 的注入逻辑把工具 schema 塞进了消息的 tools 字段,但没有给 content 填任何文本,序列化为 "content": []。而服务端校验 system 角色消息必须非空,于是拒绝。属于客户端请求组装 bug,与 token 超限、配额均无关。
期望行为
动态工具 schema 注入的 system 消息应带有非空 content(例如一段说明文字),或改用其他不触发该校验的消息形式。
影响
一旦发生,该会话历史永久不可用,只能丢上下文重开,长会话损失较大。
附件
- 报错截图(400 Invalid request: the message at position 363 ...)
- wire.jsonl 相关行(如上)
如果官方仓库更接受英文 issue,跟我说一声,我马上给你出英文版。
What steps can reproduce the bug?
长会话中通过 select_tools 动态加载工具后,客户端会注入一条 role: system 的消息用于携带新工具的 schema,但该消息的 content 为空数组 []。此后该会话的每一次模型请求都会被服务端拒绝:
400 Invalid request: the message at position 363 with role 'system' must not be empty
(provider.api_error · HTTP 400)
点「继续」、重试、/compact 均无效(/compact 本身也是一次模型调用,同样被毒化),整个会话只能 /clear 丢弃。
What is the expected behavior?
手动删除会话 wire 日志(~/.kimi-code/sessions/<workspace>/<session>/agents/main/wire.jsonl)中对应行后会话正常了
Additional information
No response
Contribution
- [ ] I am willing to submit a PR for this bug fix myself (please wait for maintainer approval in this issue first)
I'd like to take this one. I verified on current main that SelectToolsTool.resolveExecution() still injects each dynamic tool schema as a role: 'system' message with content: [], which matches the empty-system-message 400 described here.
Proposed minimal fix:
- give the injected dynamic_tool_schema message a non-empty text part while keeping tools: [schema]
- add a focused regression test asserting every injected dynamic_tool_schema system message has non-empty content and still carries the schema
- add a patch changeset for @moonshot-ai/kimi-code
Could a maintainer /approve this bug-fix scope for an external PR? I'll wait for approval before opening the PR.