kimi-code icon indicating copy to clipboard operation
kimi-code copied to clipboard

fix(agent-core): recover sessions interrupted during thinking

Open morluto opened this issue 4 months ago • 1 comments

Related Issue

Resolve #1404

Related prior work: #1882

Problem

If a process exits after recording step.begin and a nonempty thinking part but before step.end, v1 resume reconstructs the unfinished assistant step as ordinary history.

The OpenAI-compatible adapter extracts the thinking part into reasoning_content. Because the interrupted step has no answer text or tool call, the resulting request has neither content nor tool_calls, and strict providers reject every later request from that session.

The vacuous-message repair in #1968 handles empty or whitespace-only thinking. #1882 similarly checks assistant entries whose content array is empty. The record reported in #1404 contains a nonempty ThinkPart, so it does not satisfy that predicate.

step.begin -> nonempty ThinkPart -> process exits
     |
     +-- resume marks the open response partial -> omitted
     |
     +-- adapter rejects contentless/tool-less assistant history

What changed

  • Track tool-free assistant steps that remain open at the end of v1 resume and exclude them from provider projections without changing canonical history.
  • Keep interrupted tool-call steps so the existing synthetic result can close their tool exchange.
  • Prevent the OpenAI-compatible adapter from emitting assistant history with neither content nor tool_calls.
  • Preserve reasoning fields on valid assistant tool-call messages.
  • Add regressions using the nonempty thinking-only record shape from #1404.

The adapter does not copy private reasoning into visible assistant content and does not rely on an empty-string fallback that strict providers may reject.

Validation:

  • Agent resume tests: 29 passed
  • OpenAI legacy provider tests: 78 passed
  • @moonshot-ai/kosong and @moonshot-ai/agent-core typechecks
  • Oxlint on changed TypeScript files
  • git diff --check
  • Changeset validation

Checklist

  • [x] I have read the CONTRIBUTING document.
  • [x] I have linked a related issue, or explained the problem above.
  • [x] I have added tests that prove my feature works.
  • [x] Ran gen-changesets skill, and included a patch changeset.
  • [x] This restores existing resume behavior and needs no documentation update.

morluto avatar Jul 25 '26 02:07 morluto

🦋 Changeset detected

Latest commit: a5b526cb2901c35db2b0859f4f6b6297984e6a01

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
@moonshot-ai/agent-core Patch
@moonshot-ai/kimi-code Patch
@moonshot-ai/kosong Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

changeset-bot[bot] avatar Jul 25 '26 02:07 changeset-bot[bot]