Skip to content

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

Closed

Description

@ajianaz

Summary

Today a provider failure gets one automatic retry per turn (turn.rs, PROVIDER_TIMEOUT_RETRIES = 1), fired immediately, and only when the error text contains timeout or 429 (is_transient_provider_error). There is no backoff, Retry-After is never read, and 5xx or empty completions are not retried. Unattended callers (mission, serve, ACP, cron) then stop and wait for a manual resume. The one-retry design (#58) made sense while resume was the backstop; it is thin for unattended use.

Measured (develop @ 438ad0d vs nca 0.5.5; mock server fails the first N chat requests, then succeeds; Retry-After: 1 is sent)

  • 429 x1: tole OK (retry fires after 0.02 s); nca OK (waits 1.0 s = Retry-After)
  • 429 x2: tole FAIL rc 4; nca OK (two waits of about 1 s)
  • 429 x3: tole FAIL rc 4; nca OK
  • 503 x1: tole FAIL rc 4, no retry at all; nca OK (1.2 s)
  • 503 x2: tole FAIL; nca OK (1.05 s, 2.1 s)
  • 503 x3: tole FAIL; nca OK (1.05 s, 2.1 s, 4.2 s)
  • empty completion once (stream): tole FAIL rc 4 malformed response: empty stream (no content, no tool calls); nca retries and succeeds
  • empty completion, non-stream: tole exits rc 0 with empty text while stream mode fails. The two modes disagree.

The single 429 retry lands 20 ms later, so against a real rate limiter it will usually be rejected again.

Dependency

Reading Retry-After needs the response headers, which are dropped today together with the error body (see the provider-error-body issue: #474). The retry work should land after, or together with, that fix.

Proposal

Bounded retry for the transient classes: 429, 5xx, connect/timeout, empty completion, stream cut before any output.

  • Up to 3 retries, exponential backoff 1/2/4 s with jitter; honor Retry-After (seconds form) capped at 30 s. nca uses 4 attempts and the same 30 s cap.
  • 4xx (auth, model, invalid request) stays fail-loud.

Constraints

  • Sleeps must be cancel-aware (slice on the CancelToken, as the ACP permission wait does). A --timeout or Esc must not wait out a 30 s sleep.
  • Sleeps count against --max-minutes / --timeout.
  • Every retry decision is written durably BEFORE it fires (existing contract, replay-visible).
  • ProviderError is a public String newtype: carry the retry hint without breaking embedders (message-encoded retry-after=N, or an additive trait method).
  • Order versus provider failover chain — reserve provider for mission resilience #209 failover is an owner decision. Suggested: exhaust retries on the primary, then fail over to the reserve.
  • Empty completion needs a decision first: the non-stream path returns Final with empty text (rc 0, openai.rs:482-487) while the stream path errors with malformed response: empty stream (openai.rs:621-625); say which side moves. Also is_failover_error treats malformed... as fail-loud (failover.rs:64), so a retried empty completion that keeps failing would not fail over: the order above does not cover it unless that classification changes.

Acceptance

  • Mock-server E2E for each row above, including Retry-After honored and a durable record per retry.
  • Cancel during a backoff sleep returns promptly.
  • Stream / non-stream parity for empty completions.
  • Docs.

Found by

Competitor research 2026-11-02 (nca retry_delay / MAX_PROVIDER_ATTEMPTS; fx retries until success with visible waits), measured live against develop @ 438ad0d.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementreliability

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions