From 646e9438d46e8bf5eb9902ab3779d1bac7fcdbb6 Mon Sep 17 00:00:00 2001 From: jerryfane Date: Sat, 26 Sep 2026 10:14:45 +0200 Subject: [PATCH] jev: retry a 403 once before failing closed (same as gitmoot#2267) --- internal/jev/client.go | 19 +++++++++++++++++-- internal/jev/client_test.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/internal/jev/client.go b/internal/jev/client.go index e072055..bb6d0c6 100644 --- a/internal/jev/client.go +++ b/internal/jev/client.go @@ -74,6 +74,7 @@ func (c *Client) Evaluate(ctx context.Context, request Request) (Exchange, error // without ever contacting the server. attempts := max(c.MaxRetries, 0) + 1 var last error + forbiddenRetried := false for attempt := range attempts { if err := c.pace(ctx); err != nil { return exchange, &APIError{Message: "cancelled: " + err.Error()} @@ -87,9 +88,20 @@ func (c *Client) Evaluate(ctx context.Context, request Request) (Exchange, error last = err var apiErr *APIError - if !errors.As(err, &apiErr) || !apiErr.Retryable || attempt == attempts-1 { + if !errors.As(err, &apiErr) || attempt == attempts-1 { break } + // A 403 is usually a real refusal, but the provider behind OpenRouter + // has also returned a transient "RBAC: access denied" for a valid key. + // Retry it exactly once, after a short pause; a second 403 is final. + retryForbidden := apiErr.Status == http.StatusForbidden && !forbiddenRetried + if !apiErr.Retryable && !retryForbidden { + break + } + if retryForbidden { + forbiddenRetried = true + wait = forbiddenRetryWait + } if wait <= 0 { wait = c.backoff(attempt) } @@ -103,6 +115,9 @@ func (c *Client) Evaluate(ctx context.Context, request Request) (Exchange, error return exchange, last } +// forbiddenRetryWait is the pause before the single retry of a 403. +const forbiddenRetryWait = 2 * time.Second + // once performs a single attempt. The returned duration is a server-requested // wait, when one was given. func (c *Client) once(ctx context.Context, body []byte) (Response, time.Duration, error) { @@ -156,7 +171,7 @@ func (c *Client) once(ctx context.Context, body []byte) (Response, time.Duration // retryableStatus: 429 and 529 ask the client to back off and retry, and a 5xx // is a server fault worth another try. A 401 or 422 fails the same way every -// time. +// time. A 403 is not retryable here; Evaluate gives it one extra attempt. func retryableStatus(status int) bool { switch { case status == http.StatusTooManyRequests, status == 529: diff --git a/internal/jev/client_test.go b/internal/jev/client_test.go index f40d29c..90d895b 100644 --- a/internal/jev/client_test.go +++ b/internal/jev/client_test.go @@ -118,6 +118,39 @@ func TestClientDoesNotRetryAuthFailureAndRedactsKey(t *testing.T) { } } +// A transient provider 403 ("RBAC: access denied" for a valid key) gets one +// retry; a lasting 403 fails after exactly two attempts, never more. +func TestClientRetriesOneForbiddenOnly(t *testing.T) { + transient := newFakeServer(t, func(n int) (int, http.Header, string) { + if n == 1 { + return http.StatusForbidden, nil, `{"error":{"message":"HTTP 403: RBAC: access denied","code":403}}` + } + return http.StatusOK, nil, okAnswer + }) + var waits []time.Duration + client := testClient(transient.server.URL) + client.Sleep = func(_ context.Context, d time.Duration) error { waits = append(waits, d); return nil } + exchange, err := client.Evaluate(context.Background(), Request{Model: DefaultModel}) + if err != nil || exchange.Attempts != 2 { + t.Fatalf("transient 403: attempts = %d, err = %v; want success on attempt 2", exchange.Attempts, err) + } + if len(waits) != 1 || waits[0] != forbiddenRetryWait { + t.Fatalf("waits = %v, want [%v]", waits, forbiddenRetryWait) + } + + lasting := newFakeServer(t, func(int) (int, http.Header, string) { + return http.StatusForbidden, nil, "denied" + }) + exchange, err = testClient(lasting.server.URL).Evaluate(context.Background(), Request{Model: DefaultModel}) + var apiErr *APIError + if !errors.As(err, &apiErr) || apiErr.Status != http.StatusForbidden { + t.Fatalf("lasting 403: err = %v", err) + } + if exchange.Attempts != 2 { + t.Fatalf("lasting 403: attempts = %d, want 2 (MaxRetries is %d)", exchange.Attempts, testClient("").MaxRetries) + } +} + func TestClientCapsRetryAfterAtMaxBackoff(t *testing.T) { server := newFakeServer(t, func(n int) (int, http.Header, string) { if n == 1 {