feat: production-grade retry/backoff with error classification and partial output protection - #10
Open
danielxxomg wants to merge 4 commits into
Open
Conversation
Replace naive exponential backoff with production-grade retry engine: - Error classification: 18 non-retryable patterns (auth/quota/validation) and 24 retryable patterns (network/server/timeout) via extractMessage() that handles Error, string, and SSE plain-object shapes - Fixed backoff schedule [1s, 2.5s, 5s] with ±25% jitter replaces unbounded exponential 2^n growth - fetchWithRetry: retries 5xx/429 with backoff, fails fast on 4xx - fetchOnce: single fetch for mid-stream reconnects (no double counting) - streamWithReconnect: emittedContent tracking gates reconnect to prevent duplicate content generation; pendingReconnect state machine handles clean reconnection flow - shouldRetry: centralized decision — checks aborted, emittedContent, attempt count, and error classification - buildHttpError: parses JSON error bodies with model ID annotation - partialOutputError: clear error when reconnect is unsafe - wrapAsError: normalizes any error shape into Error with ccError/code
SSE error events were silently enqueued as stream parts ({type:"error"})
which the AI SDK treats as ignorable — failures went undetected and the
retry layer never saw them.
Changes:
- Add wrapError() to normalize SSE error objects, strings, and Error
instances into real Error with ccError and code properties
- toStreamPart("error") now returns null (errors handled at parse level)
- parseStreamEvents: SSE error events call controller.error(wrapError(...))
instead of controller.enqueue({type:"error"})
- Network read failures in catch block also use controller.error(wrapError(...))
- SSE errors in final buffer chunks also terminate via controller.error()
This ensures the retry layer in model.ts sees real Error objects it can
classify (retryable vs non-retryable) and the AI SDK surfaces failures
to users instead of silently swallowing them.
…nment Tests that expect 'no key found' behavior were failing when ~/.commandcode/auth.json exists in the test environment. Mock fs.existsSync to return false for auth file paths (.commandcode, .pi) while preserving real fs for other paths. Uses mock.module() with dynamic import so the mock is applied before auth.ts loads.
fix(stream): terminate stream on SSE errors via controller.error()
Author
|
Hi @brent-weatherall! Friendly check-in on this PR and repository maintenance status. Given that upstream has been inactive for a few months and multiple community members are relying on Command Code provider improvements, we've integrated this production-grade retry/backoff engine (along with SSE error propagation and the full 50-model Command Code catalog) into an actively maintained fork: If you'd like to resume maintenance and merge these upstream, we'd be delighted to collaborate. In the meantime, users and contributors can leverage the fork. |
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.
Summary
Replaces the naive retry logic in
src/model.tswith a production-grade retry engine that handles transient network errors, server failures, and mid-stream disconnects gracefully.Problems Solved
2s * 2^n) without jitter — all clients retry at the same timeChanges
src/model.ts(327 → 532 lines)extractMessage()— handlesError,string, and plain SSE error objects for classificationNON_RETRYABLE_PATTERNS(18 patterns) — auth/quota/validation errors, never retryRETRYABLE_PATTERNS(24 patterns) — network/server/transient errorsisRetryableStatus()— retries HTTP 429 and 5xxbackoffDelay()— fixed schedule[1s, 2.5s, 5s]with ±25% jitter (replaces exponential2s * 2^n)fetchOnce()— single fetch for mid-stream reconnects (no double retry counting)emittedContenttracking — gates reconnect: if text/reasoning/tool-call already emitted, aborts reconnect to prevent DUPLICATE contentshouldRetry()— centralized decision: checks aborted, emittedContent, attempt count, and retryable classificationpartialOutputError()— clear error message when reconnect would cause duplicateswrapAsError()— normalizes any error shape into a real Error objectbuildHttpError()— parses JSON error bodies properlyTests
tests/unit/model.test.tsmockFetchRetrySequence()helper intests/helpers/mocks.tsBackoff Schedule Comparison
Test Results