diff --git a/internal/provider/codex_live_copy_test.go b/internal/provider/codex_live_copy_test.go new file mode 100644 index 000000000..91529ba0d --- /dev/null +++ b/internal/provider/codex_live_copy_test.go @@ -0,0 +1,65 @@ +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) + } +} diff --git a/internal/provider/logins_on.go b/internal/provider/logins_on.go index b52ed5476..dbc1d65f4 100644 --- a/internal/provider/logins_on.go +++ b/internal/provider/logins_on.go @@ -233,6 +233,18 @@ func renewSaved(ctx context.Context, agent, user string, force bool, stale strin if l == nil { return "", "", fmt.Errorf("no saved %s account %q", agent, user) } + if agent == "codex" && heldByCodex(*l) { + // the account Codex is signed in to now (switched to since this + // was asked for, or the copy kept of it as it was): one sign-in in + // two places, whose refresh token a refresh here would spend under + // Codex. Codex's own auth.json then holds a dead one, and the app + // of an edu or business workspace, which reads its workspace's + // settings with it at start, stays paused on "无法加载组织设置" + // (万贤凯 on Discord). So it is renewed where Codex keeps it, as + // Codex's own (codexTokenRenewing), and the copy is left as it is: + // a switch away saves it afresh from Codex's. + return codexTokenRenewing(ctx, codexHome(), stale) + } var auth []byte switch agent { case "codex": @@ -274,6 +286,23 @@ func renewSaved(ctx context.Context, agent, user string, force bool, stale strin return tok, accountID, writeLogins(ls) } +// heldByCodex says the saved ChatGPT account l is the one Codex is signed +// in to: the same login, or the same refresh token. Asked under +// savedTokenMu, which a switch holds too, so it can't change before the +// refresh it decides is done. +func heldByCodex(l savedLogin) bool { + live, ok := liveLogin("codex") + if !ok { + return false + } + if sameLogin(live, l) { + return true + } + var a, b codexAuth + return json.Unmarshal(live.Auth, &a) == nil && json.Unmarshal(l.Auth, &b) == nil && + a.Tokens.RefreshToken != "" && a.Tokens.RefreshToken == b.Tokens.RefreshToken +} + // savedRefreshFailed words a saved account's failed refresh. One the vendor // refused is marked lapsed on the account: that sign-in is gone and has to be // made again, in magpie; the agent's own login command would sign the agent