Skip to content

feat(provider): bounded cancel-aware retry with backoff and Retry-After - #491

Merged
ajianaz merged 1 commit into
developfrom
fix/provider-bounded-retry
Nov 3, 2026
Merged

ajianaz merged 1 commit into
developfrom
fix/provider-bounded-retry

Conversation

@ajianaz

@ajianaz ajianaz commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

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-After hint (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 the CancelToken, so a cancel ends the wait promptly and the loop-top checkpoint settles the turn. A durable provider transient failure, retrying entry (leading error + planned wait + retries left) is written before every wait. Transient classes extend to 5xx (parsed code only), connection errors, stream read errors and empty completions; 4xx others stay fail-loud. Empty completions now error the same way in both modes: the non-stream path returns malformed response: empty stream (no content, no tool calls) instead of a silent empty Final. 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)

  • New crates/tole-core/tests/provider_retry.rs (scripted mock + real OpenAiProvider + real turn loop), written FIRST: RED against develop failed 10 of 11 (429/503 x1/x2/x3 all ProviderFailed; the 429 x1 wait was 15 ms against Retry-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 honor Retry-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.
  • Two workspace REDs found and fixed along the way: a case-sensitive connection match against the OS text Connection refused, and a config_apply fixture 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 in turn_loop.rs use (retry-after: 0) so they stay instant.
  • Local cora pre-commit gave no verdict both times it ran (error, non-blocking) — the CI Cora Review job is the check.

Deliberate scope notes (owner-visible decisions from the issue)

  • Retry-vs-failover order: FailoverProvider runs inside one complete() 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.
  • An empty completion that outlasts its retries fails the turn WITHOUT failover (is_failover_error untouched, per the issue's classification note): retries absorb it, failover does not.
  • The mission deadline is checked between turns, as before; backoff sleeps count towards it without a bypass. Not run: cora scan and any live-provider check.

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
@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown

🔍 Cora AI Code Review

✅ No issues found. Code looks good!


Review powered by cora-code · BYOK · MIT

@ajianaz
ajianaz merged commit 67069d5 into develop Nov 3, 2026
16 checks passed
@ajianaz
ajianaz deleted the fix/provider-bounded-retry branch November 3, 2026 03:02
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.

provider: bounded retry with backoff + Retry-After for 429/5xx/empty completions (today: one immediate retry, 429/timeout only)

1 participant