MoonshotAI / MoonshotAI/kimi-code

400 provider error on session resume with open tool calls

Open
#701 0 comments 0 reactions 0 assignees View on GitHub

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:

  1. Clearing pendingToolResultIds at the start and end of turn boundaries (TurnFlow.runOneTurn).
  2. Clearing pendingToolResultIds after replaying session records during resume (Agent.resume).
  3. Reverting the step.end cleanup 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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.