fix(models): keep Retry-After on HTTPError and honour it in agent LLM retries
Endpoint.Do turns a non-2xx reply into &HTTPError{StatusCode, Body} and
drops the response headers. A caller that retries a 429 or 503 therefore
cannot see the provider's Retry-After and falls back to its own guess: the
agent retry loop waits 1s, 2s, 3s, which against a provider asking for, say,
20s spends every attempt inside the rate-limit window and then fails the
turn.
- HTTPError carries the reply's Header, and RetryAfter() parses the header
in both forms (delay-seconds and HTTP-date); absent, malformed or past
values give zero.
- The agent retry loop waits max(linear backoff, Retry-After), with the
vendor's value capped at 30s so a large Retry-After cannot hold a turn
indefinitely.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-26 07:04:17 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"errors"
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/http/httptest"
|
|
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestDoKeepsHeadersOnHTTPError(t *testing.T) {
|
2026-09-29 11:32:08 +00:00
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
fix(models): keep Retry-After on HTTPError and honour it in agent LLM retries
Endpoint.Do turns a non-2xx reply into &HTTPError{StatusCode, Body} and
drops the response headers. A caller that retries a 429 or 503 therefore
cannot see the provider's Retry-After and falls back to its own guess: the
agent retry loop waits 1s, 2s, 3s, which against a provider asking for, say,
20s spends every attempt inside the rate-limit window and then fails the
turn.
- HTTPError carries the reply's Header, and RetryAfter() parses the header
in both forms (delay-seconds and HTTP-date); absent, malformed or past
values give zero.
- The agent retry loop waits max(linear backoff, Retry-After), with the
vendor's value capped at 30s so a large Retry-After cannot hold a turn
indefinitely.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-26 07:04:17 +00:00
|
|
|
w.Header().Set("Retry-After", "12")
|
|
|
|
|
w.WriteHeader(http.StatusTooManyRequests)
|
|
|
|
|
_, _ = w.Write([]byte(`{"error":"rate limited"}`))
|
|
|
|
|
}))
|
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
|
|
req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, srv.URL, nil)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
_, err = Endpoint{Client: srv.Client()}.Do(req)
|
|
|
|
|
|
|
|
|
|
var httpErr *HTTPError
|
|
|
|
|
require.True(t, errors.As(err, &httpErr))
|
|
|
|
|
require.Equal(t, http.StatusTooManyRequests, httpErr.StatusCode)
|
|
|
|
|
require.Equal(t, 12*time.Second, httpErr.RetryAfter())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestParseRetryAfter(t *testing.T) {
|
|
|
|
|
now := time.Date(2026, 9, 26, 12, 0, 0, 0, time.UTC)
|
|
|
|
|
for _, tc := range []struct {
|
|
|
|
|
name string
|
|
|
|
|
value string
|
|
|
|
|
want time.Duration
|
|
|
|
|
}{
|
|
|
|
|
{"absent", "", 0},
|
|
|
|
|
{"seconds", "30", 30 * time.Second},
|
|
|
|
|
{"seconds with spaces", " 5 ", 5 * time.Second},
|
|
|
|
|
{"zero", "0", 0},
|
|
|
|
|
{"negative", "-3", 0},
|
|
|
|
|
{"http date", now.Add(90 * time.Second).Format(http.TimeFormat), 90 * time.Second},
|
|
|
|
|
{"http date in the past", now.Add(-time.Minute).Format(http.TimeFormat), 0},
|
|
|
|
|
{"garbage", "tomorrow", 0},
|
|
|
|
|
} {
|
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
|
require.Equal(t, tc.want, parseRetryAfter(tc.value, now))
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestRetryAfterOnNilError(t *testing.T) {
|
|
|
|
|
var e *HTTPError
|
|
|
|
|
require.Zero(t, e.RetryAfter())
|
|
|
|
|
require.Zero(t, (&HTTPError{StatusCode: 503}).RetryAfter())
|
|
|
|
|
}
|