Repository navigation
feat(provider): bounded cancel-aware retry with backoff and Retry-After - #491
Merged
Merged
Conversation
Retry transient provider failures up to 3 times per turn (429, 5xx, connection/timeout, stream read, empty completion) instead of one immediate retry for timeout/429 only. Waits are 1/2/4 s with jitter, or the Retry-After seconds carried in the error text, capped at 30 s. The wait is sliced on the CancelToken and each retry is recorded durably before it fires. Other 4xx stay fail-loud. The non-stream path now returns the same empty-completion error as the stream path instead of an empty Final. Closes #478
🔍 Cora AI Code Review✅ No issues found. Code looks good! Review powered by cora-code · BYOK · MIT |
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.
What (description of the change)
Turn-level provider retries go from 1 immediate attempt to 3 (4 attempts total) with exponential backoff 1/2/4 s plus up to 25% jitter. A
Retry-Afterhint (seconds form, parsed from the(retry-after: N)marker #474 puts in the error text) replaces the backoff wait, capped at 30 s; the HTTP-date form falls back to plain backoff. Waits are sliced at 1 s on theCancelToken, so a cancel ends the wait promptly and the loop-top checkpoint settles the turn. A durableprovider transient failure, retryingentry (leading error + planned wait + retries left) is written before every wait. Transient classes extend to 5xx (parsed code only), connection errors,stream readerrors and empty completions; 4xx others stay fail-loud. Empty completions now error the same way in both modes: the non-stream path returnsmalformed response: empty stream (no content, no tool calls)instead of a silent emptyFinal. README gains a "Provider retries" section; CHANGELOG under [Unreleased].Why (the problem you're solving)
Closes #478. Unattended callers (mission, serve, ACP, cron) died to a second consecutive 429, to any 5xx, or to one empty completion, and waited for a manual
resume. Measured in the issue: the single retry fired ~20 ms after the failure, so a real rate limiter just rejected it again.Testing (how you verified this works)
crates/tole-core/tests/provider_retry.rs(scripted mock + realOpenAiProvider+ real turn loop), written FIRST: RED against develop failed 10 of 11 (429/503 x1/x2/x3 allProviderFailed; the 429 x1 wait was 15 ms againstRetry-After: 1; empty completion failed both modes; cancel and the 3-retry bound failed), GREEN 11/11 after the change (~8 s wall: the tests really honorRetry-After). Covers every row of the issue's Measured table, Retry-After honored, one durable record per retry, cancel during a backoff sleep returns promptly, and the 3-attempt bound.connectionmatch against the OS textConnection refused, and aconfig_applyfixture answering 500 that now (correctly) gets retried — it answers 401, failing loud on the first request.cargo fmt --all -- --check,cargo clippy --workspace --all-targets -- -D warnings,cargo test --workspace --no-fail-fast(923 passed / 0 failed),cargo test -p tole-core --no-default-features(355 passed / 0 failed),cargo build -p tole-cli --no-default-features: all pass. The mock tests inturn_loop.rsuse(retry-after: 0)so they stay instant.Deliberate scope notes (owner-visible decisions from the issue)
FailoverProviderruns inside onecomplete()call, so a transient failure fails over to the reserve immediately and turn-level retries start only after every provider in the chain has failed. The issue suggested the opposite order (exhaust primary, then fail over); doing that without widening the public provider trait is not possible today, so this PR takes the no-trait-change side. Flipping it later needs a small trait hook — say the word and it becomes its own issue.is_failover_erroruntouched, per the issue's classification note): retries absorb it, failover does not.cora scanand any live-provider check.