fix(kosong): retry when a response stream is terminated mid-flight - #201
Merged
Conversation
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 detectedLatest commit: 4f625af The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
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 |
commit: |
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Related Issue
Related to #149 — this addresses the
Error: terminatedhalf of the report. TheAPIEmptyResponseError(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-codingstreaming responses, the SSE connection is sometimes dropped mid-flight. Node's built-in fetch (undici) raises a rawTypeError: terminated. Because that raw error is not an OpenAI SDK error type,convertOpenAIErrorfell through to its generic branch and produced a non-retryableChatProviderError. 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
terminatedto the connection-error patterns.terminatednow becomes a retryableAPIConnectionErrorand is retried transparently within the existing retry budget, instead of failing the turn.AbortError. A regression test locks this in.Checklist
gen-changesetsskill, or this PR needs no changeset. (Added a changeset:@moonshot-ai/kosong+@moonshot-ai/kimi-code, patch.)gen-docsskill, or this PR needs no doc update. (N/A — internal error-classification fix, no user-facing docs surface.)