From c0e99a9055cc7cbf6e74b9b3fd93bfe2657b8bb0 Mon Sep 17 00:00:00 2001 From: Mandeep Date: Fri, 6 Nov 2026 17:52:29 +0000 Subject: [PATCH 1/2] Skip refresh for account-pilot managed Codex credentials account-pilot keeps the Codex access token fresh from the Pi and writes a placeholder refresh token plus a trayManagedAccount marker. CLIProxyAPI's 5-day Codex refresh lead made it send that placeholder to auth.openai.com on every start, log a 401 and mark the auth unauthorized until the next write. Marked credentials are now never scheduled for background refresh and never refreshed after a 401; a request 401 still surfaces. Co-Authored-By: Claude Opus 5.5 --- sdk/cliproxy/auth/auto_refresh_loop.go | 2 +- sdk/cliproxy/auth/conductor.go | 10 ++++ sdk/cliproxy/auth/external_refresh_test.go | 68 ++++++++++++++++++++++ 3 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 sdk/cliproxy/auth/external_refresh_test.go diff --git a/sdk/cliproxy/auth/auto_refresh_loop.go b/sdk/cliproxy/auth/auto_refresh_loop.go index b4217b31636..034cccc562b 100644 --- a/sdk/cliproxy/auth/auto_refresh_loop.go +++ b/sdk/cliproxy/auth/auto_refresh_loop.go @@ -343,7 +343,7 @@ func nextRefreshCheckAt(now time.Time, auth *Auth, interval time.Duration) (time return time.Time{}, false } - if auth.AuthKind() == AuthKindAPIKey { + if auth.AuthKind() == AuthKindAPIKey || authRefreshedExternally(auth) { return time.Time{}, false } diff --git a/sdk/cliproxy/auth/conductor.go b/sdk/cliproxy/auth/conductor.go index b492cdf6fd1..774754cf98a 100644 --- a/sdk/cliproxy/auth/conductor.go +++ b/sdk/cliproxy/auth/conductor.go @@ -6015,7 +6015,17 @@ func authAccessToken(auth *Auth) string { return authMetadataString(auth, "accessToken") } +// authRefreshedExternally reports a credential that account-pilot keeps fresh from +// the Pi. Its file carries a placeholder refresh token, so refreshing it here can +// only fail and mark the auth unauthorized. +func authRefreshedExternally(auth *Auth) bool { + return authMetadataString(auth, "trayManagedAccount") != "" +} + func authHasRefreshCredential(auth *Auth) bool { + if authRefreshedExternally(auth) { + return false + } if authMetadataString(auth, "refresh_token") != "" { return true } diff --git a/sdk/cliproxy/auth/external_refresh_test.go b/sdk/cliproxy/auth/external_refresh_test.go new file mode 100644 index 00000000000..37d9d1d7590 --- /dev/null +++ b/sdk/cliproxy/auth/external_refresh_test.go @@ -0,0 +1,68 @@ +package auth + +import ( + "context" + "testing" + "time" + + "github.com/router-for-me/CLIProxyAPI/v7/internal/registry" + cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/executor" +) + +func pilotManagedCodexAuth(id string, expiry time.Time, managed bool) *Auth { + meta := map[string]any{ + "type": "codex", + "access_token": "stale-access-token", + "refresh_token": "managed-by-usage-display-tray", + "expired": expiry.Format(time.RFC3339), + } + if managed { + meta["trayManagedAccount"] = "codex" + } + return &Auth{ID: id, Provider: "codex", Metadata: meta} +} + +func TestPilotManagedAuthIsNeverScheduledForBackgroundRefresh(t *testing.T) { + now := time.Date(2026, 11, 6, 17, 0, 0, 0, time.UTC) + lead := 5 * 24 * time.Hour + setRefreshLeadFactory(t, "codex", func() *time.Duration { return &lead }) + expiry := now.Add(4 * 24 * time.Hour) + + plain := pilotManagedCodexAuth("plain", expiry, false) + if _, ok := nextRefreshCheckAt(now, plain, 15*time.Minute); !ok { + t.Fatalf("control: an unmanaged auth inside the refresh lead must be scheduled") + } + + managed := pilotManagedCodexAuth("managed", expiry, true) + if _, ok := nextRefreshCheckAt(now, managed, 15*time.Minute); ok { + t.Fatalf("nextRefreshCheckAt scheduled a pilot-managed auth") + } +} + +func TestPilotManagedAuthSurfacesUnauthorizedWithoutRefreshing(t *testing.T) { + model := "gpt-5.5" + auth := pilotManagedCodexAuth("aa-managed", time.Now().Add(96*time.Hour), true) + executor := &unauthorizedRefreshExecutor{ + id: "codex", + tokenInvalid: map[string]struct{}{"stale-access-token": {}}, + } + m := NewManager(nil, nil, nil) + m.RegisterExecutor(executor) + reg := registry.GetGlobalRegistry() + reg.RegisterClient(auth.ID, "codex", []*registry.ModelInfo{{ID: model}}) + t.Cleanup(func() { reg.UnregisterClient(auth.ID) }) + if _, err := m.Register(context.Background(), auth); err != nil { + t.Fatalf("register: %v", err) + } + + _, err := m.Execute(context.Background(), []string{"codex"}, cliproxyexecutor.Request{Model: model}, cliproxyexecutor.Options{}) + if err == nil || !isUnauthorizedError(err) { + t.Fatalf("Execute error = %v, want the upstream 401", err) + } + if got := executor.RefreshCalls(); got != 0 { + t.Fatalf("Refresh calls = %d, want 0 for a pilot-managed auth", got) + } + if got := executor.ExecuteCalls(); len(got) != 1 { + t.Fatalf("Execute calls = %v, want exactly one attempt", got) + } +} From f32498225937116021a4266208735299e96d89a0 Mon Sep 17 00:00:00 2001 From: Mandeep Date: Fri, 6 Nov 2026 18:01:26 +0000 Subject: [PATCH 2/2] Also skip refresh for logins carrying the Pi sentinel refresh token The PC gateway writes codex-tray.json with the placeholder refresh token but no trayManagedAccount marker, so treat the exact sentinel as managed too. Co-Authored-By: Claude Opus 5.5 --- sdk/cliproxy/auth/conductor.go | 15 ++-- sdk/cliproxy/auth/external_refresh_test.go | 81 ++++++++++++++-------- 2 files changed, 62 insertions(+), 34 deletions(-) diff --git a/sdk/cliproxy/auth/conductor.go b/sdk/cliproxy/auth/conductor.go index 774754cf98a..583b50e65f2 100644 --- a/sdk/cliproxy/auth/conductor.go +++ b/sdk/cliproxy/auth/conductor.go @@ -6015,11 +6015,18 @@ func authAccessToken(auth *Auth) string { return authMetadataString(auth, "accessToken") } -// authRefreshedExternally reports a credential that account-pilot keeps fresh from -// the Pi. Its file carries a placeholder refresh token, so refreshing it here can -// only fail and mark the auth unauthorized. +// pilotSentinelRefreshToken is the placeholder refresh token that account-pilot and +// the PC gateway write in place of the Pi's real one. +const pilotSentinelRefreshToken = "managed-by-usage-display-tray" + +// authRefreshedExternally reports a credential that is kept fresh from the Pi. +// Refreshing it here can only fail and mark the auth unauthorized. func authRefreshedExternally(auth *Auth) bool { - return authMetadataString(auth, "trayManagedAccount") != "" + if authMetadataString(auth, "trayManagedAccount") != "" { + return true + } + return authMetadataString(auth, "refresh_token") == pilotSentinelRefreshToken || + authMetadataString(auth, "refreshToken") == pilotSentinelRefreshToken } func authHasRefreshCredential(auth *Auth) bool { diff --git a/sdk/cliproxy/auth/external_refresh_test.go b/sdk/cliproxy/auth/external_refresh_test.go index 37d9d1d7590..97103f9306d 100644 --- a/sdk/cliproxy/auth/external_refresh_test.go +++ b/sdk/cliproxy/auth/external_refresh_test.go @@ -9,14 +9,29 @@ import ( cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/executor" ) -func pilotManagedCodexAuth(id string, expiry time.Time, managed bool) *Auth { +type pilotAuthShape struct { + name string + marker bool + sentinel bool +} + +var pilotManagedShapes = []pilotAuthShape{ + {name: "marker", marker: true}, + {name: "sentinel", sentinel: true}, +} + +func pilotCodexAuth(id string, expiry time.Time, shape pilotAuthShape) *Auth { + refresh := "real-refresh-token" + if shape.sentinel { + refresh = pilotSentinelRefreshToken + } meta := map[string]any{ "type": "codex", "access_token": "stale-access-token", - "refresh_token": "managed-by-usage-display-tray", + "refresh_token": refresh, "expired": expiry.Format(time.RFC3339), } - if managed { + if shape.marker { meta["trayManagedAccount"] = "codex" } return &Auth{ID: id, Provider: "codex", Metadata: meta} @@ -28,41 +43,47 @@ func TestPilotManagedAuthIsNeverScheduledForBackgroundRefresh(t *testing.T) { setRefreshLeadFactory(t, "codex", func() *time.Duration { return &lead }) expiry := now.Add(4 * 24 * time.Hour) - plain := pilotManagedCodexAuth("plain", expiry, false) + plain := pilotCodexAuth("plain", expiry, pilotAuthShape{}) if _, ok := nextRefreshCheckAt(now, plain, 15*time.Minute); !ok { t.Fatalf("control: an unmanaged auth inside the refresh lead must be scheduled") } - managed := pilotManagedCodexAuth("managed", expiry, true) - if _, ok := nextRefreshCheckAt(now, managed, 15*time.Minute); ok { - t.Fatalf("nextRefreshCheckAt scheduled a pilot-managed auth") + for _, shape := range pilotManagedShapes { + managed := pilotCodexAuth("managed-"+shape.name, expiry, shape) + if _, ok := nextRefreshCheckAt(now, managed, 15*time.Minute); ok { + t.Fatalf("%s: nextRefreshCheckAt scheduled a pilot-managed auth", shape.name) + } } } func TestPilotManagedAuthSurfacesUnauthorizedWithoutRefreshing(t *testing.T) { - model := "gpt-5.5" - auth := pilotManagedCodexAuth("aa-managed", time.Now().Add(96*time.Hour), true) - executor := &unauthorizedRefreshExecutor{ - id: "codex", - tokenInvalid: map[string]struct{}{"stale-access-token": {}}, - } - m := NewManager(nil, nil, nil) - m.RegisterExecutor(executor) - reg := registry.GetGlobalRegistry() - reg.RegisterClient(auth.ID, "codex", []*registry.ModelInfo{{ID: model}}) - t.Cleanup(func() { reg.UnregisterClient(auth.ID) }) - if _, err := m.Register(context.Background(), auth); err != nil { - t.Fatalf("register: %v", err) - } + for _, shape := range pilotManagedShapes { + t.Run(shape.name, func(t *testing.T) { + model := "gpt-5.5" + auth := pilotCodexAuth("aa-managed-"+shape.name, time.Now().Add(96*time.Hour), shape) + executor := &unauthorizedRefreshExecutor{ + id: "codex", + tokenInvalid: map[string]struct{}{"stale-access-token": {}}, + } + m := NewManager(nil, nil, nil) + m.RegisterExecutor(executor) + reg := registry.GetGlobalRegistry() + reg.RegisterClient(auth.ID, "codex", []*registry.ModelInfo{{ID: model}}) + t.Cleanup(func() { reg.UnregisterClient(auth.ID) }) + if _, err := m.Register(context.Background(), auth); err != nil { + t.Fatalf("register: %v", err) + } - _, err := m.Execute(context.Background(), []string{"codex"}, cliproxyexecutor.Request{Model: model}, cliproxyexecutor.Options{}) - if err == nil || !isUnauthorizedError(err) { - t.Fatalf("Execute error = %v, want the upstream 401", err) - } - if got := executor.RefreshCalls(); got != 0 { - t.Fatalf("Refresh calls = %d, want 0 for a pilot-managed auth", got) - } - if got := executor.ExecuteCalls(); len(got) != 1 { - t.Fatalf("Execute calls = %v, want exactly one attempt", got) + _, err := m.Execute(context.Background(), []string{"codex"}, cliproxyexecutor.Request{Model: model}, cliproxyexecutor.Options{}) + if err == nil || !isUnauthorizedError(err) { + t.Fatalf("Execute error = %v, want the upstream 401", err) + } + if got := executor.RefreshCalls(); got != 0 { + t.Fatalf("Refresh calls = %d, want 0 for a pilot-managed auth", got) + } + if got := executor.ExecuteCalls(); len(got) != 1 { + t.Fatalf("Execute calls = %v, want exactly one attempt", got) + } + }) } }