Files
magpie/internal/provider/codex_live_copy_test.go
T
yetone 4683a0382c provider: a saved ChatGPT account Codex is signed in to is refreshed in Codex's auth.json, not in its copy
A ChatGPT account switched in to Codex is one sign-in in two places:
logins.json's copy and Codex's auth.json hold one refresh token.
renewSaved refreshed the copy whenever it was asked to: a provider made
for the account while another was Codex's, held over a switch
(SwitchWhenSpent, keep-first), a usage or models read, keep-alive or a
revoked-token check decided "not the live one" just before a switch.
OpenAI rotates the refresh token as it answers, so auth.json was left
with a spent one. The Codex app of an edu or business workspace fetches
its workspace config bundle (/backend-api/wham/config/bundle) at start
with it; the refresh is refused (refresh_token_reused), app-server
answers configRequirements/read with -32603 "failed to read
configuration layers: Your access token could not be refreshed ...",
and the app stays paused on "无法加载组织设置". Reinstalling the app or
changing the preferred account doesn't help: auth.json stays.

renewSaved now asks, under savedTokenMu (which a switch holds too),
whether the saved account is the one Codex is signed in to (sameLogin,
or the same refresh token) and renews it through codexTokenRenewing on
Codex's own store instead.

Reported by 万贤凯 on Discord (two edu_plus accounts, "Codex 始终登录首选
账号" on, both used up).

Run:
- TestSavedRefreshOfCodexOwnRenewsCodexOwn: red on the old code (auth.json
  kept refresh token "r" after it was spent), green with the fix.
- Codex app-server 0.163.0-alpha.10.2 in a sandbox CODEX_HOME, edu_plus
  auth.json, fake bundle (401) and token endpoint (refresh_token_reused):
  configRequirements/read gives the -32603 above, the reply the desktop
  shows as the org-settings dialog. magpie's openai_base_url and
  [model_providers.magpie] don't move the bundle request (it goes to
  chatgpt_base_url, which magpie never writes).
- gofmt -l; go vet darwin, linux -tags nogui, windows.
- Full suite (go test -tags nogui -timeout 45m ./..., through suite-lock,
  at dd1e8822d plus this change): every package ok except internal/gui's
  TestRestartHandsTheGatewayOver (one request during the handover got
  EOF, 12 of 13). It passes 5 of 5 alone and this change doesn't reach
  it; filed as #3451.

Not tried: the reporter's own auth.json and logins.json, so which caller
spent their token isn't known; a real edu account; the GUI (unchanged).
2026-10-24 14:37:39 +08:00

66 lines
2.5 KiB
Go

package provider
import (
"context"
"path/filepath"
"strings"
"testing"
)
// A saved ChatGPT account switched in to Codex is one sign-in in two
// places: logins.json's copy and Codex's auth.json hold one refresh token.
// A refresh asked for through the copy (an account in use beside Codex's
// own when the switch came, the backend calling its token expired) spent
// that token under Codex, and auth.json was left with a dead one. The
// Codex app of an edu workspace reads its workspace's settings with it at
// start, and stayed paused on "无法加载组织设置" (万贤凯 on Discord).
func TestSavedRefreshOfCodexOwnRenewsCodexOwn(t *testing.T) {
home, refreshes, _ := codexExpiredFakes(t, staleExpired, false)
if err := SwitchLogin("codex", "me@example.com"); err != nil {
t.Fatal(err)
}
var live codexAuth
readJSON(filepath.Join(home, ".codex", "auth.json"), &live)
if live.Tokens.RefreshToken != "r" {
t.Fatalf("switched in: %+v", live.Tokens)
}
// as the provider made for me@ while work@ was Codex's asks, held
// over the switch
tok, accountID, err := renewSavedLoginFrom(context.Background(), "codex", "me@example.com", live.Tokens.AccessToken)
if err != nil || tok != "fresh-r" || accountID != "acct-1" {
t.Fatalf("renewed: %q %q %v", tok, accountID, err)
}
readJSON(filepath.Join(home, ".codex", "auth.json"), &live)
if live.Tokens.AccessToken != "fresh-r" || live.Tokens.RefreshToken != "r-2" {
t.Fatalf("Codex's auth.json kept the refresh token spent: %+v", live.Tokens)
}
if got := refreshes(); len(got) != 1 || got[0] != "r" {
t.Fatalf("refreshed with %q, want r once", got)
}
// and by keep-alive's force: Codex's own is fresh, so nothing is sent
// that would spend it
if _, _, err := renewSavedLogin(context.Background(), "codex", "me@example.com", true); err != nil {
t.Fatal(err)
}
if got := refreshes(); len(got) != 1 {
t.Fatalf("refreshed again with %q", got)
}
// work@, the one switched away from, still renews its own copy, and
// only that
for _, l := range readLogins() {
if l.User == "work@example.com" && !strings.Contains(string(l.Auth), `"r-work"`) {
t.Fatalf("work@'s copy: %s", l.Auth)
}
}
if tok, _, err := renewSavedLogin(context.Background(), "codex", "work@example.com", true); err != nil || tok != "fresh-r-work" {
t.Fatalf("work@: %q %v", tok, err)
}
readJSON(filepath.Join(home, ".codex", "auth.json"), &live)
if live.Tokens.RefreshToken != "r-2" {
t.Fatalf("work@'s refresh reached auth.json: %+v", live.Tokens)
}
}