diff --git a/sdk/cliproxy/auth/codex_refresh_selection.go b/sdk/cliproxy/auth/codex_refresh_selection.go new file mode 100644 index 000000000..b9fdf4281 --- /dev/null +++ b/sdk/cliproxy/auth/codex_refresh_selection.go @@ -0,0 +1,52 @@ +package auth + +import ( + "context" + "net/http" + "strings" + "time" +) + +// refreshAccessError keeps an expired access token blocked without turning an +// uncertain refresh exchange into evidence of an upstream access-token rejection. +// wasTerminalUnauthorized describes the state before applying this refresh result. +func refreshAccessError(auth *Auth, message string, accessTokenRejected, wasTerminalUnauthorized bool) *Error { + if auth != nil && strings.EqualFold(strings.TrimSpace(auth.Provider), "codex") && + !accessTokenRejected && !wasTerminalUnauthorized && + (auth.RejectedAccessToken == "" || auth.RejectedAccessToken != authAccessToken(auth)) && + auth.RefreshError != nil && auth.RefreshError.Code == "refresh_outcome_unknown" { + outcome := cloneError(auth.RefreshError) + if outcome.HTTPStatus == http.StatusUnauthorized { + // A diagnostic 401 must not turn this recovery state into access rejection. + outcome.HTTPStatus = http.StatusServiceUnavailable + } + return outcome + } + return &Error{Code: "unauthorized", Message: message, HTTPStatus: http.StatusUnauthorized} +} + +// codexRefreshMissingAccessForRequest keeps model-level availability from +// admitting a missing token while its grant still requires recovery. +func codexRefreshMissingAccessForRequest(ctx context.Context, auth *Auth, headers ...http.Header) bool { + if !strings.EqualFold(strings.TrimSpace(auth.Provider), "codex") || !auth.RequiresSignIn || + auth.RefreshError == nil || auth.RefreshError.Code != "refresh_outcome_unknown" { + return false + } + return authAccessTokenForRequest(ctx, auth, headers...) == "" +} + +// codexRefreshUnavailableForRequest lets a request use a healthy outer token +// while SIWC requires recovery. Ordinary cooldowns and model failures still apply. +func codexRefreshUnavailableForRequest(ctx context.Context, auth *Auth, now time.Time, headers ...http.Header) bool { + if !auth.Unavailable || !strings.EqualFold(strings.TrimSpace(auth.Provider), "codex") || + auth.LastError == nil || auth.LastError.Code != "refresh_outcome_unknown" || !auth.RequiresSignIn || + !IsCodexUseSIWC(auth) || IsCodexUseSIWCForRequest(ctx, auth, headers...) || auth.NextRetryAfter.After(now) || + auth.RejectedSIWCRefreshToken == "" || auth.RejectedSIWCRefreshToken != authSIWCTokenField(auth, "refresh_token") { + return auth.Unavailable + } + if authOuterAccessToken(auth) == "" { + return true + } + expiry, hasExpiry := auth.OuterExpirationTime() + return hasExpiry && !expiry.After(now) +} diff --git a/sdk/cliproxy/auth/codex_refresh_selection_test.go b/sdk/cliproxy/auth/codex_refresh_selection_test.go new file mode 100644 index 000000000..fb0ee8df7 --- /dev/null +++ b/sdk/cliproxy/auth/codex_refresh_selection_test.go @@ -0,0 +1,258 @@ +package auth + +import ( + "context" + "net/http" + "testing" + "time" + + "github.com/router-for-me/CLIProxyAPI/v8/internal/auth/codex" +) + +func TestCodexUnknownSIWCRefreshKeepsHealthyOuterAccess(t *testing.T) { + now := time.Now() + manager := NewManager(nil, nil, nil) + manager.RegisterExecutor(&unauthorizedRefreshExecutor{id: "codex", refreshErr: codex.WrapSIWCRefreshError(&codex.RefreshOutcomeUnknownError{Stage: "transport"})}) + auth := &Auth{ID: t.Name(), Provider: "codex", Status: StatusActive, Metadata: map[string]any{ + "access_token": "outer", "refresh_token": "outer-refresh", "expired": now.Add(time.Hour).Format(time.RFC3339), "use_siwc": true, + "siwc": map[string]any{"access_token": "siwc", "refresh_token": "siwc-refresh", "client_id": "client", "expires_at": now.Add(-time.Hour).Format(time.RFC3339)}, + }} + if _, errRegister := manager.Register(context.Background(), auth); errRegister != nil { + t.Fatal(errRegister) + } + if _, errRefresh := manager.ForceRefreshAuth(context.Background(), auth.ID); !codex.IsRefreshOutcomeUnknown(errRefresh) { + t.Fatalf("refresh outcome = %v, want unknown", errRefresh) + } + base, ok := manager.GetByID(auth.ID) + if !ok || !base.RequiresSignIn || !base.Unavailable || base.RejectedSIWCRefreshToken != "siwc-refresh" { + t.Fatalf("SIWC recovery state was not retained: %#v", base) + } + for _, tc := range []struct { + name string + native bool + mutate func(*Auth) + blocked bool + }{ + {name: "healthy outer", native: true}, + {name: "expired SIWC", blocked: true}, + {name: "disabled", native: true, blocked: true, mutate: func(auth *Auth) { auth.Disabled = true }}, + {name: "expired outer", native: true, blocked: true, mutate: func(auth *Auth) { auth.Metadata["expired"] = now.Add(-time.Hour).Format(time.RFC3339) }}, + {name: "rejected outer", native: true, blocked: true, mutate: func(auth *Auth) { auth.RejectedAccessToken = "outer" }}, + {name: "other cooldown", native: true, blocked: true, mutate: func(auth *Auth) { auth.NextRetryAfter = now.Add(time.Hour) }}, + {name: "credential quota", native: true, blocked: true, mutate: func(auth *Auth) { + auth.Quota = QuotaState{Exceeded: true, Reason: "credential_quota", NextRecoverAt: now.Add(time.Hour)} + }}, + {name: "model cooldown", native: true, blocked: true, mutate: func(auth *Auth) { + auth.ModelStates = map[string]*ModelState{"model": {Status: StatusError, Unavailable: true, NextRetryAfter: now.Add(time.Hour)}} + }}, + {name: "unrelated error", native: true, blocked: true, mutate: func(auth *Auth) { auth.LastError = &Error{Code: "unrelated"} }}, + {name: "explicit unauthorized", native: true, blocked: true, mutate: func(auth *Auth) { auth.LastError = &Error{Code: "unauthorized", HTTPStatus: http.StatusUnauthorized} }}, + {name: "no SIWC fence", native: true, blocked: true, mutate: func(auth *Auth) { auth.RejectedSIWCRefreshToken = "" }}, + {name: "other provider", native: true, blocked: true, mutate: func(auth *Auth) { auth.Provider = "claude" }}, + } { + t.Run(tc.name, func(t *testing.T) { + current := base.Clone() + if tc.mutate != nil { + tc.mutate(current) + } + var headers http.Header + if tc.native { + headers = http.Header{"User-Agent": {"codex_cli_rs/1.0.0"}} + } + blocked, _, _ := isAuthBlockedForModelForRequest(context.Background(), current, "model", now, headers) + if blocked != tc.blocked { + t.Fatalf("blocked = %v, want %v", blocked, tc.blocked) + } + }) + } +} + +func TestCodexUnknownSIWCRefreshCacheKeepsHealthyOuterAccess(t *testing.T) { + now := time.Now() + manager := NewManager(nil, nil, nil) + executor := &unauthorizedRefreshExecutor{id: "codex", refreshErr: codex.WrapSIWCRefreshError(&codex.RefreshOutcomeUnknownError{Stage: "transport"})} + manager.RegisterExecutor(executor) + auth := &Auth{ID: t.Name(), Provider: "codex", Status: StatusActive, Metadata: map[string]any{ + "access_token": "outer", "expired": now.Add(time.Hour).Format(time.RFC3339), "use_siwc": true, + "siwc": map[string]any{"access_token": "siwc", "refresh_token": "siwc-refresh", "client_id": "client", "expires_at": now.Add(-time.Hour).Format(time.RFC3339)}, + }} + if _, errRegister := manager.Register(context.Background(), auth); errRegister != nil { + t.Fatal(errRegister) + } + if _, errRefresh := manager.ForceRefreshAuth(context.Background(), auth.ID); !codex.IsRefreshOutcomeUnknown(errRefresh) { + t.Fatalf("refresh outcome = %v, want unknown", errRefresh) + } + manager.refreshAuth(context.Background(), auth.ID) + current, _ := manager.GetByID(auth.ID) + if executor.RefreshCalls() != 1 || current.LastError == nil || current.LastError.Code != "refresh_outcome_unknown" { + t.Fatal("cached recovery state lost its classification or resubmitted the grant") + } + headers := http.Header{"User-Agent": {"codex_cli_rs/1.0.0"}} + if blocked, _, _ := isAuthBlockedForModelForRequest(context.Background(), current, "model", now, headers); blocked { + t.Fatal("cached SIWC recovery state blocked healthy outer access") + } +} + +type codexPartialRefreshExecutor struct { + unauthorizedRefreshExecutor + refresh func(*Auth) (*Auth, error) +} + +func (e *codexPartialRefreshExecutor) Refresh(_ context.Context, auth *Auth) (*Auth, error) { + return e.refresh(auth) +} + +func TestCodexPartialRefreshKeepsUnknownSelectedPartitionBlocked(t *testing.T) { + for _, useSIWC := range []bool{false, true} { + name := "outer" + if useSIWC { + name = "siwc" + } + t.Run(name, func(t *testing.T) { + now := time.Now() + auth := &Auth{ID: t.Name(), Provider: "codex", Status: StatusActive, Metadata: map[string]any{ + "access_token": "outer", "refresh_token": "outer-refresh", "expired": now.Add(-time.Hour).Format(time.RFC3339), "use_siwc": useSIWC, + "siwc": map[string]any{"access_token": "siwc", "refresh_token": "siwc-refresh", "client_id": "client", "expires_at": now.Add(-time.Hour).Format(time.RFC3339)}, + }} + manager := NewManager(nil, nil, nil) + manager.RegisterExecutor(&codexPartialRefreshExecutor{ + unauthorizedRefreshExecutor: unauthorizedRefreshExecutor{id: "codex"}, + refresh: func(before *Auth) (*Auth, error) { + updated := before.Clone() + var outerErr, siwcErr error + outcome := &codex.RefreshOutcomeUnknownError{Stage: "transport"} + if useSIWC { + updated.Metadata["access_token"] = "outer-new" + updated.Metadata["expired"] = now.Add(time.Hour).Format(time.RFC3339) + siwcErr = codex.WrapSIWCRefreshError(outcome) + } else { + siwc := updated.Metadata["siwc"].(map[string]any) + siwc["access_token"] = "siwc-new" + siwc["expires_at"] = now.Add(time.Hour).Format(time.RFC3339) + outerErr = outcome + } + updated.CodexRefreshPatch = codex.NewCredentialRefreshPatch(before.Metadata, updated.Metadata, useSIWC, !useSIWC) + updated.CodexRefreshPatch.SetPartitionErrors(outerErr, siwcErr) + return updated, nil + }, + }) + if _, errRegister := manager.Register(context.Background(), auth); errRegister != nil { + t.Fatal(errRegister) + } + if _, errRefresh := manager.ForceRefreshAuth(context.Background(), auth.ID); errRefresh != nil { + t.Fatal(errRefresh) + } + current, _ := manager.GetByID(auth.ID) + if !current.Unavailable || !current.RequiresSignIn || current.LastError == nil || current.LastError.Code != "refresh_outcome_unknown" { + t.Fatalf("partial refresh lost the unresolved selected partition: unavailable=%v sign_in=%v last_error=%#v refresh_error=%#v", current.Unavailable, current.RequiresSignIn, current.LastError, current.RefreshError) + } + if blocked, _, _ := isAuthBlockedForModelForRequest(context.Background(), current, "model", now); !blocked { + t.Fatal("partial refresh restored an expired selected partition") + } + headers := http.Header{"User-Agent": {"codex_cli_rs/1.0.0"}} + if blocked, _, _ := isAuthBlockedForModelForRequest(context.Background(), current, "model", now, headers); blocked == useSIWC { + t.Fatal("native request availability does not match its outer access token") + } + }) + } +} + +func TestCodexMissingRecoveryAccessBlocksBeforeHealthyModelState(t *testing.T) { + now := time.Now() + for _, useSIWC := range []bool{false, true} { + for _, unavailable := range []bool{false, true} { + auth := &Auth{Provider: "codex", Status: StatusError, RequiresSignIn: true, Unavailable: unavailable, + RefreshError: &Error{Code: "refresh_outcome_unknown"}, + Metadata: map[string]any{ + "refresh_token": "outer-refresh", "use_siwc": useSIWC, + "siwc": map[string]any{"refresh_token": "siwc-refresh", "client_id": "client"}, + }, + ModelStates: map[string]*ModelState{"model": {Status: StatusActive}}, + } + if useSIWC { + auth.Metadata["access_token"] = "outer-valid" + auth.Metadata["expired"] = now.Add(time.Hour).Format(time.RFC3339) + auth.RejectedSIWCRefreshToken = "siwc-refresh" + } else { + siwc := auth.Metadata["siwc"].(map[string]any) + siwc["access_token"] = "siwc-valid" + siwc["expires_at"] = now.Add(time.Hour).Format(time.RFC3339) + auth.RejectedRefreshToken = "outer-refresh" + } + blocked, _, _ := isAuthBlockedForModelForRequest(context.Background(), auth, "model", now) + if !blocked || !isAuthBlockedByQuotaOrAuthForModelForRequest(context.Background(), auth, "model", now) { + t.Fatalf("healthy model state admitted a missing token: use_siwc=%v unavailable=%v", useSIWC, unavailable) + } + } + } +} + +func TestCodexUnknownDiagnostic401UsesPriorUnauthorizedState(t *testing.T) { + for _, mode := range []string{"refresh", "cached"} { + for _, tokenState := range []string{"expired", "missing"} { + for _, priorUnauthorized := range []bool{false, true} { + priorState := "active" + if priorUnauthorized { + priorState = "unauthorized" + } + t.Run(mode+"/"+tokenState+"/"+priorState, func(t *testing.T) { + now := time.Now() + outcome := &codex.RefreshOutcomeUnknownError{Stage: "transport", Cause: oauthStatusError{code: http.StatusUnauthorized, msg: "transport diagnostic"}} + executor := &unauthorizedRefreshExecutor{id: "codex", refreshErr: codex.WrapSIWCRefreshError(outcome)} + manager := NewManager(nil, nil, nil) + manager.RegisterExecutor(executor) + siwc := map[string]any{"refresh_token": "siwc-refresh", "client_id": "client"} + if tokenState == "expired" { + siwc["access_token"] = "siwc-expired" + siwc["expires_at"] = now.Add(-time.Hour).Format(time.RFC3339) + } + auth := &Auth{ID: t.Name(), Provider: "codex", Status: StatusActive, Metadata: map[string]any{ + "access_token": "outer-valid", "expired": now.Add(time.Hour).Format(time.RFC3339), "use_siwc": true, "siwc": siwc, + }} + if mode == "cached" { + auth.RequiresSignIn = true + auth.RejectedSIWCRefreshToken = "siwc-refresh" + auth.RefreshError = &Error{Code: "refresh_outcome_unknown", Message: outcome.Error(), HTTPStatus: http.StatusUnauthorized} + auth.LastError = &Error{Code: "unauthorized", Message: outcome.Error(), HTTPStatus: http.StatusUnauthorized} + } + if priorUnauthorized { + auth.Unavailable = true + auth.Status = StatusError + auth.LastError = &Error{Code: "unauthorized", Message: "access token rejected", HTTPStatus: http.StatusUnauthorized} + } + registered, errRegister := manager.Register(context.Background(), auth) + if errRegister != nil || hasUnauthorizedAuthFailure(registered) != priorUnauthorized { + t.Fatalf("incorrect prior state: err=%v terminal=%v", errRegister, hasUnauthorizedAuthFailure(registered)) + } + wantCalls := 0 + if mode == "cached" { + manager.refreshAuth(context.Background(), auth.ID) + } else { + wantCalls = 1 + if _, errRefresh := manager.ForceRefreshAuth(context.Background(), auth.ID); !codex.IsRefreshOutcomeUnknown(errRefresh) { + t.Fatalf("refresh error = %v, want unknown", errRefresh) + } + } + current, _ := manager.GetByID(auth.ID) + if executor.RefreshCalls() != wantCalls || current.RefreshError == nil || current.RefreshError.Code != "refresh_outcome_unknown" || !current.RequiresSignIn { + t.Fatalf("recovery state or replay protection changed: calls=%d refresh_error=%#v", executor.RefreshCalls(), current.RefreshError) + } + if priorUnauthorized { + if !hasUnauthorizedAuthFailure(current) || current.LastError.Code != "unauthorized" { + t.Fatal("unknown refresh cleared the prior terminal rejection") + } + } else if current.LastError == nil || current.LastError.Code != "refresh_outcome_unknown" || current.LastError.HTTPStatus == http.StatusUnauthorized || hasUnauthorizedAuthFailure(current) { + t.Fatalf("diagnostic 401 created a terminal rejection: %#v", current.LastError) + } + headers := http.Header{"User-Agent": {"codex_cli_rs/1.0.0"}} + if blocked, _, _ := isAuthBlockedForModelForRequest(context.Background(), current, "model", now, headers); blocked != priorUnauthorized { + t.Fatalf("native outer blocked=%v, want %v", blocked, priorUnauthorized) + } + if blocked, _, _ := isAuthBlockedForModelForRequest(context.Background(), current, "model", now); !blocked { + t.Fatal("unusable SIWC access was admitted") + } + }) + } + } + } +} diff --git a/sdk/cliproxy/auth/conductor_lifecycle.go b/sdk/cliproxy/auth/conductor_lifecycle.go index 117c15c0d..fd78b1e66 100644 --- a/sdk/cliproxy/auth/conductor_lifecycle.go +++ b/sdk/cliproxy/auth/conductor_lifecycle.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "math" - "net/http" "strings" "sync" "time" @@ -478,7 +477,10 @@ func (m *Manager) updateInternalLocked(ctx context.Context, base, auth *Auth, mo if existing.LastError != nil { auth.LastError = cloneError(existing.LastError) } else if auth.RefreshError != nil { - auth.LastError = &Error{Code: "unauthorized", Message: auth.RefreshError.Message, HTTPStatus: http.StatusUnauthorized} + auth.LastError = refreshAccessError(auth, auth.RefreshError.Message, false, hasUnauthorizedAuthFailure(existing)) + if auth.LastError.Code == "refresh_outcome_unknown" { + auth.StatusMessage = auth.LastError.Message + } } } } diff --git a/sdk/cliproxy/auth/conductor_refresh.go b/sdk/cliproxy/auth/conductor_refresh.go index 350a74cf4..9fedd9420 100644 --- a/sdk/cliproxy/auth/conductor_refresh.go +++ b/sdk/cliproxy/auth/conductor_refresh.go @@ -1346,6 +1346,7 @@ func (m *Manager) refreshAuthForRequestAtEpoch(ctx context.Context, id, failedAc current.RegistrationEpoch == auth.RegistrationEpoch && current.CredentialVersion == auth.CredentialVersion && authHasUnchangedRejectedRefreshToken(current) { + wasTerminalUnauthorized := hasUnauthorizedAuthFailure(current) current.Generation++ current.UpdatedAt = checkNow current.Unavailable = true @@ -1366,7 +1367,10 @@ func (m *Manager) refreshAuthForRequestAtEpoch(ctx context.Context, id, failedAc if current.RefreshError != nil && strings.TrimSpace(current.RefreshError.Message) != "" { msg = current.RefreshError.Message } - current.LastError = &Error{Code: "unauthorized", Message: msg, HTTPStatus: http.StatusUnauthorized} + current.LastError = refreshAccessError(current, msg, accessTokenRejected, wasTerminalUnauthorized) + if current.LastError.Code == "refresh_outcome_unknown" { + current.StatusMessage = current.LastError.Message + } m.auths[id] = current if m.scheduler != nil { m.scheduler.upsertAuth(current.Clone()) @@ -1551,8 +1555,10 @@ func (m *Manager) refreshAuthForRequestAtEpoch(ctx context.Context, id, failedAc current.NextRefreshAfter = time.Time{} current.NextRetryAfter = time.Time{} current.RefreshFailures = 0 - current.LastError = &Error{Code: "unauthorized", Message: sanitizedRefreshErr.Message, HTTPStatus: http.StatusUnauthorized} - if unauthorized { + current.LastError = refreshAccessError(current, sanitizedRefreshErr.Message, accessTokenRejected, wasTerminalUnauthorized) + if current.LastError.Code == "refresh_outcome_unknown" { + current.StatusMessage = current.LastError.Message + } else if unauthorized { current.StatusMessage = "unauthorized" } else { current.StatusMessage = "unauthorized (refresh token invalid)" @@ -1658,7 +1664,10 @@ func (m *Manager) refreshAuthForRequestAtEpoch(ctx context.Context, id, failedAc updated.StatusMessage = "unauthorized (refresh token invalid)" } if updated.LastError == nil && updated.RefreshError != nil { - updated.LastError = &Error{Code: "unauthorized", Message: updated.RefreshError.Message, HTTPStatus: http.StatusUnauthorized} + updated.LastError = refreshAccessError(updated, updated.RefreshError.Message, failedAccessToken != "" && authMatchesTokenPartition(updated, failedAccessToken), hasUnauthorizedAuthFailure(base)) + if updated.LastError.Code == "refresh_outcome_unknown" { + updated.StatusMessage = updated.LastError.Message + } } } updated.UpdatedAt = now diff --git a/sdk/cliproxy/auth/selector.go b/sdk/cliproxy/auth/selector.go index 9d88187de..ff1ff298b 100644 --- a/sdk/cliproxy/auth/selector.go +++ b/sdk/cliproxy/auth/selector.go @@ -846,6 +846,9 @@ func isAuthBlockedForModelForRequest(ctx context.Context, auth *Auth, model stri if auth.Disabled || auth.Status == StatusDisabled { return true, blockReasonDisabled, time.Time{} } + if codexRefreshMissingAccessForRequest(ctx, auth, clientHeaders...) { + return true, blockReasonOther, time.Time{} + } if hasUnauthorizedAuthFailure(auth) { return true, blockReasonOther, time.Time{} } @@ -855,6 +858,7 @@ func isAuthBlockedForModelForRequest(ctx context.Context, auth *Auth, model stri if auth.Quota.Exceeded && auth.Quota.Reason == "credential_quota" && auth.Quota.NextRecoverAt.After(now) { return true, blockReasonCooldown, auth.Quota.NextRecoverAt } + unavailable := codexRefreshUnavailableForRequest(ctx, auth, now, clientHeaders...) if model != "" { if len(auth.ModelStates) > 0 { modelKey := canonicalModelKey(model) @@ -888,17 +892,17 @@ func isAuthBlockedForModelForRequest(ctx context.Context, auth *Auth, model stri } return false, blockReasonNone, time.Time{} } - return availabilityBlock(auth.Unavailable, auth.Quota.Exceeded, auth.NextRetryAfter, auth.Quota.NextRecoverAt, now) + return availabilityBlock(unavailable, auth.Quota.Exceeded, auth.NextRetryAfter, auth.Quota.NextRecoverAt, now) } quotaExceeded := auth.Quota.Exceeded // When model is empty and the credential has individual model states, auth.Quota.Exceeded // is an aggregate of single-model quota cooldowns (reason "quota"). As long as the credential // itself is not unavailable (not all models failed) and not under a credential-wide quota, // do not treat individual model cooldowns as blocking the entire credential. - if len(auth.ModelStates) > 0 && auth.Quota.Reason != "credential_quota" && !auth.Unavailable { + if len(auth.ModelStates) > 0 && auth.Quota.Reason != "credential_quota" && !unavailable { quotaExceeded = false } - return availabilityBlock(auth.Unavailable, quotaExceeded, auth.NextRetryAfter, auth.Quota.NextRecoverAt, now) + return availabilityBlock(unavailable, quotaExceeded, auth.NextRetryAfter, auth.Quota.NextRecoverAt, now) } func availabilityBlock(unavailable, quotaExceeded bool, nextRetryAfter, nextRecoverAt, now time.Time) (bool, blockReason, time.Time) { @@ -999,6 +1003,9 @@ func isAuthBlockedByQuotaOrAuthForModelForRequest(ctx context.Context, auth *Aut if auth.Disabled || auth.Status == StatusDisabled { return true } + if codexRefreshMissingAccessForRequest(ctx, auth, clientHeaders...) { + return true + } if hasUnauthorizedAuthFailure(auth) { return true } @@ -1024,10 +1031,11 @@ func isAuthBlockedByQuotaOrAuthForModelForRequest(ctx context.Context, auth *Aut return false } quota := auth.Quota - if len(auth.ModelStates) > 0 && auth.Quota.Reason != "credential_quota" && !auth.Unavailable { + unavailable := codexRefreshUnavailableForRequest(ctx, auth, now, clientHeaders...) + if len(auth.ModelStates) > 0 && auth.Quota.Reason != "credential_quota" && !unavailable { quota.Exceeded = false } - return isStateBlockedByQuotaOrAuth(auth.Unavailable, quota, auth.NextRetryAfter, auth.LastError, now) + return isStateBlockedByQuotaOrAuth(unavailable, quota, auth.NextRetryAfter, auth.LastError, now) } func isStateBlockedByQuotaOrAuth(unavailable bool, quota QuotaState, nextRetryAfter time.Time, lastErr *Error, now time.Time) bool { diff --git a/test/codex_refresh_recovery_test.go b/test/codex_refresh_recovery_test.go new file mode 100644 index 000000000..508166a3b --- /dev/null +++ b/test/codex_refresh_recovery_test.go @@ -0,0 +1,154 @@ +package test + +import ( + "context" + "errors" + "fmt" + "io" + "net/http" + "net/http/httptest" + "net/url" + "sync/atomic" + "testing" + "time" + + "github.com/router-for-me/CLIProxyAPI/v8/internal/config" + "github.com/router-for-me/CLIProxyAPI/v8/internal/registry" + runtimeexecutor "github.com/router-for-me/CLIProxyAPI/v8/internal/runtime/executor" + _ "github.com/router-for-me/CLIProxyAPI/v8/internal/translator" + coreauth "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/auth" + cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/executor" + sdktranslator "github.com/router-for-me/CLIProxyAPI/v8/sdk/translator" +) + +type codexRecoveryTransport struct { + target *url.URL + base http.RoundTripper +} + +func (t codexRecoveryTransport) RoundTrip(req *http.Request) (*http.Response, error) { + local := req.Clone(req.Context()) + local.URL.Scheme = t.target.Scheme + local.URL.Host = t.target.Host + return t.base.RoundTrip(local) +} + +func TestCodexSIWCRecoveryPreservesRequestPartition(t *testing.T) { + for _, tc := range []struct { + name string + missingSIWC bool + siwcStatus int + nativeAllowed bool + }{ + {name: "expired SIWC", siwcStatus: http.StatusServiceUnavailable, nativeAllowed: true}, + {name: "missing SIWC with healthy model state", missingSIWC: true, siwcStatus: http.StatusServiceUnavailable, nativeAllowed: true}, + {name: "explicit SIWC rejection", siwcStatus: http.StatusBadRequest}, + } { + t.Run(tc.name, func(t *testing.T) { + const model = "gpt-5.4" + var outerCalls, siwcCalls, inferenceCalls atomic.Int32 + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + w.Header().Set("Content-Type", "application/json") + switch req.URL.Path { + case "/oauth/token": + outerCalls.Add(1) + _, _ = io.WriteString(w, `{"access_token":"outer-rotated","refresh_token":"outer-next","expires_in":86400}`) + case "/api/accounts/oauth/token": + siwcCalls.Add(1) + w.WriteHeader(tc.siwcStatus) + body := `{"error":"server_error"}` + if tc.siwcStatus == http.StatusBadRequest { + body = `{"error":"invalid_grant","code":"refresh_token_reused"}` + } + _, _ = io.WriteString(w, body) + default: + inferenceCalls.Add(1) + if got := req.Header.Get("Authorization"); got != "Bearer outer-rotated" { + t.Errorf("inference used %q, want the refreshed outer token", got) + w.WriteHeader(http.StatusUnauthorized) + return + } + w.Header().Set("Content-Type", "text/event-stream") + _, _ = fmt.Fprint(w, "data: {\"type\":\"response.completed\",\"response\":{\"id\":\"recovery-response\",\"status\":\"completed\",\"model\":\"gpt-5.4\",\"output\":[],\"usage\":{\"input_tokens\":1,\"output_tokens\":1,\"total_tokens\":2}}}\n\n") + } + })) + t.Cleanup(server.Close) + target, errParse := url.Parse(server.URL) + if errParse != nil { + t.Fatal(errParse) + } + transport := &http.Transport{} + t.Cleanup(transport.CloseIdleConnections) + originalTransport := http.DefaultTransport + http.DefaultTransport = codexRecoveryTransport{target: target, base: transport} + t.Cleanup(func() { http.DefaultTransport = originalTransport }) + + cfg := &config.Config{AuthDir: t.TempDir()} + manager := coreauth.NewManager(nil, &coreauth.RoundRobinSelector{}, nil) + manager.SetRetryConfig(0, 0, 0) + manager.RegisterExecutor(runtimeexecutor.NewCodexExecutor(cfg)) + authID := t.Name() + registry.GetGlobalRegistry().RegisterClient(authID, "codex", []*registry.ModelInfo{{ID: model}}) + t.Cleanup(func() { registry.GetGlobalRegistry().UnregisterClient(authID) }) + now := time.Now() + siwc := map[string]any{"refresh_token": authID + "-siwc", "client_id": "issued-client"} + if !tc.missingSIWC { + siwc["access_token"] = "siwc-expired" + siwc["expires_at"] = now.Add(-time.Hour).Format(time.RFC3339) + } + auth := &coreauth.Auth{ID: authID, Provider: "codex", Status: coreauth.StatusActive, + Attributes: map[string]string{"base_url": server.URL}, + Metadata: map[string]any{ + "access_token": "outer-before", "refresh_token": authID + "-outer", "expired": now.Add(time.Hour).Format(time.RFC3339), + "use_siwc": true, "siwc": siwc, + }, + } + if tc.missingSIWC { + auth.ModelStates = map[string]*coreauth.ModelState{model: {Status: coreauth.StatusActive}} + } + if _, errRegister := manager.Register(context.Background(), auth); errRegister != nil { + t.Fatal(errRegister) + } + if _, errRefresh := manager.ForceRefreshAuth(context.Background(), authID); errRefresh != nil { + t.Fatal(errRefresh) + } + if outerCalls.Load() != 1 || siwcCalls.Load() != 1 { + t.Fatalf("refresh HTTP calls = outer %d / SIWC %d, want 1 / 1", outerCalls.Load(), siwcCalls.Load()) + } + recovered, _ := manager.GetByID(authID) + if !recovered.RequiresSignIn || recovered.RefreshError == nil { + t.Fatal("the unresolved or rejected SIWC grant lost its recovery requirement") + } + request := cliproxyexecutor.Request{Model: model, Payload: []byte(`{"model":"gpt-5.4","input":"hello"}`)} + assertSIWCBlocked := func() { + t.Helper() + _, errExecute := manager.Execute(context.Background(), []string{"codex"}, request, cliproxyexecutor.Options{SourceFormat: sdktranslator.FormatOpenAIResponse}) + var authErr *coreauth.Error + if !errors.As(errExecute, &authErr) || authErr.Code != "auth_unavailable" { + t.Fatalf("unusable SIWC credential reached execution: %v", errExecute) + } + } + assertSIWCBlocked() + _, errNative := manager.Execute(context.Background(), []string{"codex"}, request, cliproxyexecutor.Options{ + SourceFormat: sdktranslator.FormatOpenAIResponse, + Headers: http.Header{"User-Agent": {"codex_cli_rs/1.0.0"}}, + }) + if (errNative == nil) != tc.nativeAllowed { + t.Fatalf("native outer inference: error=%v, want allowed=%v", errNative, tc.nativeAllowed) + } + assertSIWCBlocked() + _, _ = manager.ForceRefreshAuth(context.Background(), authID) + if siwcCalls.Load() != 1 { + t.Fatal("a later refresh resubmitted the fenced SIWC grant") + } + assertSIWCBlocked() + wantInference := int32(0) + if tc.nativeAllowed { + wantInference = 1 + } + if inferenceCalls.Load() != wantInference { + t.Fatalf("inference calls = %d, want %d", inferenceCalls.Load(), wantInference) + } + }) + } +}