feat: production-grade retry/backoff with error classification and partial output protection - #10
Open
danielxxomg wants to merge 1 commit 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
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