MoonshotAI / MoonshotAI/kimi-code
400 provider error on session resume with open tool calls
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 7.5k
- Forks
- 1.2k
- Avg merge
- 11h 53m
- Merged PRs (30d)
- 350
Description
Bug Description
When resuming a Kimi Code session where the last turn has open (unresponded) tool calls, the LLM provider fails with a 400 [provider.api_error] stating:
an assistant message with 'tool_calls' must be followed by tool messages responding to each 'tool_call_id'.
This happens because the new user prompt/steer is appended to the history after the open tool calls, preventing trimTrailingOpenToolExchange from cleaning them since they are no longer "trailing" at the very end of the history. Additionally, the new user prompt gets deferred if pendingToolResultIds is not cleared from the aborted turn.
Solution
We resolved this by:
- Clearing
pendingToolResultIdsat the start and end of turn boundaries (TurnFlow.runOneTurn). - Clearing
pendingToolResultIdsafter replaying session records during resume (Agent.resume). - Reverting the
step.endcleanup to correctly preserve system reminder deferral during active tool execution.
The commit with the fix on the fork is:
https://github.com/thecannabisapp/kimi-code/commit/0291e891f0d304add982598e6bcba992dd27042e
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 with packages/agent-core/src/agent/turn/index.ts and packages/agent-core/src/agent/index.ts, then compare the resume and turn-boundary handling with commit 0291e891f0d304add982598e6bcba992dd27042e. Reproduce a resumed session with open tool calls and a new prompt; done means the provider no longer returns the 400 error and the prompt is not deferred.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- cli
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 35/100