Skip to content

fix(agent-core-v2): reject incomplete model streams - #5006

Merged
sailist merged 1 commit into
release/2026-11-16from
bug-386-11-16-openai-stream-premature-eof
Nov 17, 2026
Merged

sailist merged 1 commit into
release/2026-11-16from
bug-386-11-16-openai-stream-premature-eof

Conversation

@sailist

@sailist sailist commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator

Requirement or Bug

OpenAI-compatible streaming can end cleanly at the transport layer before a terminal finish_reason arrives. When text or a tool call has already been emitted, Kimi Code currently accepts that partial response as successful: a CLI turn may finish or execute a truncated tool call, and direct generation may emit a finish event whose reasons are undefined.

This PR makes the affected CLI, print, desktop/web, VS Code, ACP, and SDK request paths reject the incomplete response as a retryable failure instead, allowing the existing normal or infinite retry policy to recover when the provider completes a later attempt. No separate issue was created; the change comes from a directly reported streaming failure.

Bug Reproduction Steps

  1. Return an OpenAI-compatible stream containing Let me write the report. with finish_reason: null.
  2. End the response body without ever sending a non-null finish_reason. The SDK treats this clean EOF as normal iteration completion whether or not [DONE] is present.
  3. Before this PR, the core result is done, and the direct-generate adapter emits a normal finish event with undefined reasons. The same regression is covered for whitespace-only content and a tool call whose JSON arguments are truncated.

Root Cause

The OpenAI decoder only emits a finish fact for a non-null finish_reason, while its end() hook merely closes an active tool call. settleLlmRequest therefore reaches its final outcome synthesis with finish === undefined, but previously checks only repeated, empty, and think-only responses. Any other accumulated text or tool call returns done.

The CLI consumes that outcome directly, and the direct-generate adapter converts it into a synthetic finish event. This is a fundamental fix at their shared settlement boundary rather than another adapter-side workaround.

Code Changes

 settle request
   preserve abort and transport-failure handling
   preserve repeated, empty, and think-only classification
+  if streaming and no finish event was received:
+    return a retryable provider failure with provider/model details
   return done

The new check runs only when request.protocol.stream !== false, after the existing empty-response classification. Empty and think-only responses therefore keep their specialized empty_response errors, non-streaming behavior is unchanged, and aborts or transport failures retain priority.

The scripted requester used by adapter tests mirrors the same success contract. Existing test cases were rewritten without increasing test counts to cover partial text, whitespace, truncated tool calls, normal completion, adapter rejection, and recovery through infinite retry.

Behavior Changes and Affected Users

Behavior Before After Who relies on the old behavior Escape hatch
A streaming response emits text, whitespace, or a tool call but no finish event, with no transport/decode failure Accepted as done; a CLI turn may finish or process a truncated tool call, and direct generation emits a synthetic finish with undefined reasons Returns a retryable provider failure so normal or infinite retry can handle the incomplete response OpenAI-compatible providers or gateways that terminate SSE early, and equivalent malformed Anthropic, Google GenAI, or OpenAI Responses streams across TUI, print, desktop/web, VS Code, ACP, and SDK consumers None for invalid partial output; responses with a valid finish reason are unchanged

Only the missing-finish streaming branch changes. Empty and think-only responses retain empty_response; non-streaming responses, aborts, transport failures, repeated-output handling, and valid completed responses are unchanged. No configuration, public API shape, wire format, or persisted-session migration is involved.

Coverage for these boundaries lives in empty-response.test.ts and modelRequester.test.ts. The test declarations remain 3 and 14 respectively, with no net test-count increase. A user-approved @moonshot-ai/kimi-code patch changeset is included.

Verification details
  • Red reproduction on the old implementation: the core and adapter assertions both failed; the adapter produced a finish event with undefined reasons.
  • Final rebased tree (release/2026-11-16 at 1590a95bb059): 17/17 focused tests passed; agent-core-v2 TypeScript checking passed.
  • The same product diff before the non-overlapping release rebase: all agent-core-v2 tests passed on m0-dev (376 files passed, 1 skipped; 7857 tests passed, 13 skipped), and the root build completed with exit code 0.
  • Focused oxlint: 0 errors and 13 pre-existing warnings. Import-boundary checking still reports the same four pre-existing violations in untouched files.
  • The network origin of any particular production EOF was not tested; deterministic chunk streams exercise the complete core settlement and adapter behavior.

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue (external PRs: issue must have a maintainer's /approve).
  • I have added tests that prove my feature works.
  • The behavior-change table above is complete, and every removed behavior or flipped default is named in the changeset and either has an escape hatch or was explicitly approved by a maintainer in this PR.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

@changeset-bot

changeset-bot Bot commented Nov 17, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 2d18c66

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

This PR includes changesets to release 1 package
Name Type
@moonshot-ai/kimi-code 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

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-17T02:32:44.203412Z 2d18c66 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@pkg-pr-new

pkg-pr-new Bot commented Nov 17, 2026

Copy link
Copy Markdown
pnpm dlx https://pkg.pr.new/@moonshot-ai/kimi-code@2d18c66
npx https://pkg.pr.new/@moonshot-ai/kimi-code@2d18c66

commit: 2d18c66

@sailist
sailist merged commit 1311098 into release/2026-11-16 Nov 17, 2026
15 checks passed
@sailist
sailist deleted the bug-386-11-16-openai-stream-premature-eof branch November 17, 2026 03:30
@github-actions github-actions Bot mentioned this pull request Nov 17, 2026
@Shbinging

Copy link
Copy Markdown

CLOSES #4997

wbxl2000 pushed a commit that referenced this pull request Nov 17, 2026
* fix: resume sessions with removed default models (#4982)

* fix(node-sdk): resume sessions with missing default model

* fix(cli): check auth for the resumed session model

* chore: add changeset for unavailable model recovery

* fix(agent-core-v2): stop rebuilding mismatched session indexes (#4980)

* fix(agent-core-v2): reject repeated model responses (#4977)

* fix(agent-core-v2): reject repeated model responses

* chore: add changeset for repeated model responses

---------

Co-authored-by: 7Sageer <[email protected]>

* fix(agent-core-v2): inline deferred tools without dynamic loading (#4971)

* fix(agent-core-v2): record restored task notifications without model requests (#4949) (#4989)

* fix(agent-core-v2): record restored task notifications without model requests

- queue restored task notifications as prompt-only turns after loop initialization
- preserve interrupted and queued turns during resume and fork
- complete reconciliation before initialization and reset return to keep Wire reads ordered
- run loop and container cleanup when reconciliation fails
- surface restored notification persistence failures

* chore: add changeset for restored task notifications

* fix(agent-core-v2): restore overflow recovery after LLM retries (#4950) (#4990)

* fix(agent-core-v2): restore step state after LLM retries

* test(agent-core-v2): scope retry event to overflow regression

* fix(config): allow sessions with invalid user config (#4951) (#4991)

* fix(agent-core-v2): tolerate config read failures for permission rules

* chore: add changeset for permission config recovery

* docs: document permission config recovery

* docs: qualify permission config recovery scope

* fix(config): surface read error diagnostics

* fix(config): allow sessions with invalid user config

* fix(config): preserve valid values and replay diagnostics

* fix(config): remove startup diagnostic replay

* fix(agent-core-v2): ignore non-user prompts during Tower wake (#4952) (#4992)

* fix(agent-core-v2): ignore non-user prompts during Tower wake

* chore: add Tower notification fix changeset

* fix(agent-core-v2): preserve slash prompts during Tower wake

* fix(agent-core-v2): route foreground shell events by origin (#4954) (#4994)

* fix(agent-core-v2): route foreground shell events by origin

* test(agent-core-v2): expect tool event origins in snapshots

* fix(klient): accept tool events from pre-origin hosts

* fix(agent-core-v2): run external Stop hooks after turn completion (#4956) (#4995)

* fix(agent-core-v2): run external Stop hooks after turn completion

* chore: add changeset for external Stop hooks

* docs: clarify post-turn Stop hook timing

* fix(agent-core-v2): wait for subagent quiescence (#4953) (#4993)

* fix(agent-core-v2): reject incomplete model streams (#5006)

* fix(agent-core-v2): preserve choice-level OpenAI token usage (#5017)

* fix(capability): confirm stopping WebBridge before Windows updates (#4999)

* fix(capability): confirm stopping WebBridge before Windows updates

* fix(capability): harden WebBridge update recovery

* fix(capability): restore WebBridge after startup failure

* fix(capability): wait for WebBridge process exit

* fix(capability): handle WebBridge confirmation edge cases

---------

Co-authored-by: kimi-agent-bot <[email protected]>
Co-authored-by: kimi-agent-bot <[email protected]>

* chore: sync release 2.6.3 back to main

* chore: finish release 2.6.3 backsync

* chore: sync node-sdk lockfile importer

* fix(sdk): preserve main core type dependencies

* fix: complete release 2.6.3 backsync

---------

Co-authored-by: Haozhe <[email protected]>
Co-authored-by: 7Sageer <[email protected]>
Co-authored-by: kimi-agent-bot <[email protected]>
Co-authored-by: kimi-agent-bot <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants