Skip to content

fix(kosong): retry when a response stream is terminated mid-flight - #201

Merged
wbxl2000 merged 1 commit into
mainfrom
fix/retry-terminated-stream
May 29, 2026
Merged

fix(kosong): retry when a response stream is terminated mid-flight#201
wbxl2000 merged 1 commit into
mainfrom
fix/retry-terminated-stream

Conversation

@wbxl2000

Copy link
Copy Markdown
Collaborator

Related Issue

Related to #149 — this addresses the Error: terminated half of the report. The APIEmptyResponseError (think-only) half needs the reporter's full session log to confirm root cause and is left for follow-up.

Problem

On long kimi-for-coding streaming responses, the SSE connection is sometimes dropped mid-flight. Node's built-in fetch (undici) raises a raw TypeError: terminated. Because that raw error is not an OpenAI SDK error type, convertOpenAIError fell through to its generic branch and produced a non-retryable ChatProviderError. The retry layer only retries known transient types, so the turn failed on the first attempt — even though an intermittent connection drop is exactly the kind of transient failure that should be retried. (This is an HTTP/1.1 connection close mid-body, not an HTTP/2 reset — undici's global fetch defaults to HTTP/1.1.)

What changed

  • Route raw (non-SDK) errors through the existing transport-layer heuristic instead of wrapping them as a generic error, and add terminated to the connection-error patterns.
  • A mid-stream terminated now becomes a retryable APIConnectionError and is retried transparently within the existing retry budget, instead of failing the turn.
  • User-aborted (ESC) requests are unaffected: the retry loop checks the abort signal before retrying, so a cancelled request is never retried and still surfaces as a clean AbortError. A regression test locks this in.

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue, or explained the problem above.
  • I have added tests that prove my feature works.
  • Ran gen-changesets skill, or this PR needs no changeset. (Added a changeset: @moonshot-ai/kosong + @moonshot-ai/kimi-code, patch.)
  • Ran gen-docs skill, or this PR needs no doc update. (N/A — internal error-classification fix, no user-facing docs surface.)

A mid-stream SSE connection drop surfaces as a raw undici TypeError:
terminated, which is not an OpenAI SDK error type. convertOpenAIError fell
through to the generic branch and produced a non-retryable ChatProviderError,
so the turn failed on the first attempt instead of retrying a transient
connection drop.

Route raw (non-SDK) errors through the transport-layer heuristic and add
terminated to the connection-error patterns, so a dropped stream becomes a
retryable APIConnectionError. User-aborted (ESC) requests are unaffected: the
retry loop checks the abort signal before retrying.

Related to #149 (the Error: terminated half).
@changeset-bot

changeset-bot Bot commented May 29, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 4f625af

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

This PR includes changesets to release 2 packages
Name Type
@moonshot-ai/kosong Patch
@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

@pkg-pr-new

pkg-pr-new Bot commented May 29, 2026

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

commit: 4f625af

@wbxl2000
wbxl2000 merged commit 3da4dae into main May 29, 2026
7 checks passed
@wbxl2000
wbxl2000 deleted the fix/retry-terminated-stream branch May 29, 2026 07:18
@github-actions github-actions Bot mentioned this pull request May 29, 2026
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.

1 participant