provider: a Codex account held in another Codex folder offers Sign in again when its refresh is refused (charliepu)
charliepu on Discord (#general 1566029465107173518, Windows): a ChatGPT account whose sign-in had lapsed had no button to sign in again in the Providers editor. The account was one Codex keeps in another of its folders (CODEX_HOME=C:\Users\...\.codex-db-backup-...). OpenAI refused its refresh, but renewSaved only wrapped the refusal in "run CODEX_HOME=... codex login", and codexSignInRead skipped marking it: a saved account's refused refresh is marked by savedRefreshFailed, which never runs for a folder-held one. So the row never got a lapse, and the GUI's Sign in again (renderAccounts' signedOut, SIGN_IN_GONE) never showed. - logins_on.go: renewSaved wraps such a refusal as codexHeldRefusal and names no command to run. - login_usage.go: codexSignInRead marks a codexHeldRefusal signed out (markRevoked), as it does Codex's own, so the row reads "Sign-in required" with Sign in again, which runs magpie's Codex sign-in for that user (startSignIn), the flow every other revoked Codex account uses. - signin.go: addLogin writes the new tokens back to the Codex folder that holds the account (codexHeldFolder, saveCodexLogin) and clears its gone mark, or the next look would read the dead tokens back over them. ~/.codex/auth.json is not touched. - account.go: codexRefresh's failure says "sign in to this account again", not "run codex login". - chatgpt_api.go (sibling): a ChatGPT API account's kept lapse is listed on its row too, so it gets the same Sign in again. No GUI change: the row already shows Sign in again for a Codex lapse (revoked-signin-account.test.cjs covers it); the backend never set one. Run at origin/main4f00a32ffplus this change; rebased after onto8680ffa41, which touches internal/gateway only (not run again): - mote: go test -timeout 45m -count=1 ./internal/provider: ok (114s) - mote: GOOS=windows and GOOS=linux go vet ./internal/provider: ok - gofmt -l internal/provider: empty At the earlier base0379f2610(mote rebooted mid-run, so the rest went to the WSL box magpie-desktop): - WSL: go vet ./internal/provider (linux, windows, darwin): ok - WSL: go test ./internal/provider ./internal/gateway: ok (gateway 488s); ./internal/gui doesn't build there (no webkitgtk-6.0 dev package) - mote: go test ./internal/gui: two reds, both filed and untouched here: TestSettingsSaveKeepsWhatItDoesNotSend (#4514) and TestMainWindowCorners/macOS_26_SDK on macOS 27 (#3949) - WSL, Playwright 1.62.0, Chromium and WebKit, every language, 420 and 1100 wide: revoked-signin-account and codex-signout pass; i18n-keys passes except "a plugin's words are its own", the known red #4524 (plugins/routing "Unmarked") Broken by hand, each red with the new tests: - codexSignInRead without the codexHeldRefusal exception: TestCodexHeldInOtherFolderSignedOutThenSignedInAgain fails "the row isn't signed out, so it has no Sign in again". - addLogin with held = "": the same test fails "the new sign-in didn't reach the account's Codex folder". - siwcLoginList without the saved lapse: TestSIWCLapsedListedOnItsRow fails. Not tried: a real lapsed account, on Windows or anywhere (the refusal is a fake token endpoint answering 400 invalid_grant); a real OpenAI sign-in written back into a CODEX_HOME folder; the full ./... suite. Not changed: codexTokenAsKept and codexTokenRenewing still say "run codex login" when a folder has no access token at all (Codex itself signed out, not a refused refresh).
This commit is contained in:
@@ -1242,7 +1242,7 @@ func codexRefresh(ctx context.Context, raw map[string]any, reason string) (strin
|
||||
RefreshToken string `json:"refresh_token"`
|
||||
}
|
||||
if res.StatusCode != 200 || json.Unmarshal(b, &fresh) != nil || fresh.AccessToken == "" {
|
||||
err := refreshFailed(res.StatusCode, "Codex", "Codex is signed out (token refresh failed); run codex login")
|
||||
err := refreshFailed(res.StatusCode, "Codex", "Codex is signed out (token refresh failed); sign in to this account again")
|
||||
if res.StatusCode != 200 && codexRefreshGone(res.StatusCode, b) {
|
||||
// refused for good, by Codex's own rule (#4308)
|
||||
err = codexRefreshGoneError{err}
|
||||
|
||||
@@ -105,7 +105,18 @@ func siwcSide() []sideLogin {
|
||||
})
|
||||
}
|
||||
|
||||
func siwcLoginList() []Login { return loginsOf(siwcSide()) }
|
||||
// siwcLoginList lists the accounts, one OpenAI no longer refreshes said
|
||||
// on its row (siwcRefresh's Lapsed), which then offers Sign in again: the
|
||||
// lapse was kept, and shown nowhere but on a failed request.
|
||||
func siwcLoginList() []Login {
|
||||
ls := siwcSide()
|
||||
for i := range ls {
|
||||
if ls[i].Lapsed == "" {
|
||||
ls[i].Lapsed = ls[i].saved.Lapsed
|
||||
}
|
||||
}
|
||||
return loginsOf(ls)
|
||||
}
|
||||
|
||||
func switchSIWCLogin(user string) error { return switchSideLogin(ChatGPTAPIID, user, siwcSide()) }
|
||||
|
||||
|
||||
@@ -0,0 +1,120 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// charliepu on Discord (Windows, 10-31): a ChatGPT Pro account Codex is
|
||||
// signed in to in another of its folders (C:\Users\charl\.codex-db-backup-
|
||||
// 20260803) was refused its refresh. Its row had no Sign in again, and its
|
||||
// allowance line said "run CODEX_HOME=… codex login". Now the row says
|
||||
// signed out, nothing tells the user to run a command, and magpie's own
|
||||
// sign-in writes the new tokens back to that folder, the same account.
|
||||
func TestCodexHeldInOtherFolderSignedOutThenSignedInAgain(t *testing.T) {
|
||||
home := signIn(t)
|
||||
t.Setenv("CODEX_HOME", "")
|
||||
resetLoginUsage(t)
|
||||
resetRevoked()
|
||||
t.Cleanup(resetRevoked)
|
||||
backup := filepath.Join(home, ".codex-db-backup-20260803")
|
||||
soon := float64(time.Now().Add(time.Minute).Unix())
|
||||
claims := map[string]any{"email": "pro@example.com",
|
||||
"https://api.openai.com/auth": map[string]any{"chatgpt_plan_type": "pro", "chatgpt_account_id": "acct-pro"}}
|
||||
idTok := fakeJWT(claims)
|
||||
writeFile(t, filepath.Join(backup, "auth.json"), map[string]any{
|
||||
"auth_mode": "chatgpt", "OPENAI_API_KEY": nil, "last_refresh": "2026-08-03T00:00:00Z",
|
||||
"tokens": map[string]any{"id_token": idTok, "access_token": fakeJWT(map[string]any{"exp": soon}),
|
||||
"refresh_token": "r-dead", "account_id": "acct-pro"},
|
||||
})
|
||||
mainAuth, _ := os.ReadFile(filepath.Join(home, ".codex", "auth.json"))
|
||||
|
||||
fresh := fakeJWT(map[string]any{"exp": float64(time.Now().Add(time.Hour).Unix()), "n": "again"})
|
||||
fake := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/backend-api/wham/usage" {
|
||||
if r.Header.Get("Authorization") != "Bearer "+fresh {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
io.WriteString(w, `{"plan_type":"pro","rate_limit":{"allowed":true,"primary_window":{"used_percent":5,"limit_window_seconds":18000,"reset_after_seconds":600}}}`)
|
||||
return
|
||||
}
|
||||
// every refresh refused for good, as OpenAI refused charliepu's
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
io.WriteString(w, `{"error":"invalid_grant"}`)
|
||||
}))
|
||||
defer fake.Close()
|
||||
oldTok, oldBase := codexTokenURL, CodexBase
|
||||
t.Cleanup(func() { codexTokenURL, CodexBase = oldTok, oldBase })
|
||||
codexTokenURL, CodexBase = fake.URL, fake.URL+"/backend-api/codex"
|
||||
|
||||
ForgetAccounts()
|
||||
q := LoginUsage(context.Background(), "codex")["pro@example.com"]
|
||||
if q.Error == "" {
|
||||
t.Fatalf("the refused account read: %+v", q)
|
||||
}
|
||||
for _, cmd := range []string{"codex login", "CODEX_HOME"} {
|
||||
if strings.Contains(q.Error, cmd) {
|
||||
t.Errorf("the allowance line tells the user to run a command: %q", q.Error)
|
||||
}
|
||||
}
|
||||
lapsed := codexLapsed(t)["pro@example.com"]
|
||||
if !strings.Contains(lapsed, "sign in again") {
|
||||
t.Fatalf("the row isn't signed out, so it has no Sign in again: %q (read %q)", lapsed, q.Error)
|
||||
}
|
||||
|
||||
// Sign in again: magpie's own sign-in (the callback's addLogin)
|
||||
again, err := codexLogin(idTok, fresh, "r-again", "acct-pro")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if using, err := addLogin(again); err != nil || using {
|
||||
t.Fatalf("signed in again: using %v, %v", using, err)
|
||||
}
|
||||
b, _ := os.ReadFile(filepath.Join(backup, "auth.json"))
|
||||
var a codexAuth
|
||||
if json.Unmarshal(b, &a) != nil || a.Tokens.RefreshToken != "r-again" || a.Tokens.AccessToken != fresh {
|
||||
t.Fatalf("the new sign-in didn't reach the account's Codex folder: %s", b)
|
||||
}
|
||||
if now, _ := os.ReadFile(filepath.Join(home, ".codex", "auth.json")); string(now) != string(mainAuth) {
|
||||
t.Errorf("~/.codex/auth.json was rewritten: %s", now)
|
||||
}
|
||||
// the next look reads the folder again, and keeps the new sign-in
|
||||
ForgetAccounts()
|
||||
rememberLogins(true)
|
||||
users, _ := loginUsers(Logins("codex"))
|
||||
if strings.Join(users, ",") != "me@example.com,pro@example.com" {
|
||||
t.Fatalf("after signing in again: %v", users)
|
||||
}
|
||||
if l := codexLapsed(t)["pro@example.com"]; l != "" {
|
||||
t.Fatalf("signed in again, still said signed out: %q", l)
|
||||
}
|
||||
freshUsage()
|
||||
if q := LoginUsage(context.Background(), "codex")["pro@example.com"]; q.Error != "" || len(q.Windows) != 1 {
|
||||
t.Fatalf("read after signing in again: %+v", q)
|
||||
}
|
||||
}
|
||||
|
||||
// A ChatGPT API account OpenAI no longer refreshes says so on its row,
|
||||
// which then offers Sign in again: the lapse was kept and listed nowhere.
|
||||
func TestSIWCLapsedListedOnItsRow(t *testing.T) {
|
||||
signIn(t)
|
||||
c := siwcCreds{ClientID: "c", HostID: "h", Email: "api@example.com", Access: "a", Refresh: "r", Expires: time.Now().Add(time.Hour)}
|
||||
auth, _ := json.Marshal(c)
|
||||
msg := "api@example.com's ChatGPT sign-in has expired — sign in again"
|
||||
if err := writeLogins([]savedLogin{{Agent: ChatGPTAPIID, User: "api@example.com", Auth: auth, Lapsed: msg}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ls := Logins(ChatGPTAPIID)
|
||||
if len(ls) != 1 || ls[0].Lapsed != msg {
|
||||
t.Fatalf("ChatGPT API rows: %+v", ls)
|
||||
}
|
||||
}
|
||||
@@ -461,7 +461,9 @@ func loginQuota(ctx context.Context, l Login) SubscriptionQuota {
|
||||
// signed out as a revoked one is (signin_revoked.go): its row says Sign in
|
||||
// again, which runs magpie's sign-in for it and replaces its tokens, and
|
||||
// the gateway passes over it till then. A saved account's refused refresh
|
||||
// is marked lapsed already (savedRefreshFailed). A read that went through
|
||||
// is marked lapsed already (savedRefreshFailed), but not one Codex is
|
||||
// signed in to in another of its folders (codexHeldRefusal: charliepu's
|
||||
// C:\Users\…\.codex-db-backup-…), which is marked here. A read that went through
|
||||
// clears a mark it had. Anything else is unknown and marks nothing.
|
||||
func codexSignInRead(l Login, tok string, err error) error {
|
||||
if err == nil {
|
||||
@@ -469,7 +471,7 @@ func codexSignInRead(l Login, tok string, err error) error {
|
||||
return nil
|
||||
}
|
||||
var refused refreshRefused
|
||||
if !l.Active && errors.As(err, &refused) {
|
||||
if !l.Active && errors.As(err, &refused) && !errors.As(err, new(codexHeldRefusal)) {
|
||||
return err
|
||||
}
|
||||
m := bearerMark(codexGoneFor(err, tok, func() []byte { return codexLoginAuth(l) }))
|
||||
|
||||
@@ -252,7 +252,15 @@ func renewSaved(ctx context.Context, agent, user string, force bool, stale strin
|
||||
// whose refresh token would sign that Codex out
|
||||
tok, accountID, err := codexTokenRenewing(ctx, home, stale)
|
||||
if err != nil {
|
||||
return "", "", fmt.Errorf("%s, signed in to Codex in %s: %w; to sign in again there, run CODEX_HOME=%s codex login", user, home, err, home)
|
||||
// said with no command to run (charliepu on Discord, Windows:
|
||||
// "run CODEX_HOME=… codex login"): a refusal marks the account
|
||||
// signed out on its row (codexSignInRead), whose Sign in again
|
||||
// runs magpie's own sign-in and writes it back to home (addLogin)
|
||||
err = fmt.Errorf("%s, signed in to Codex in %s: %w", user, home, err)
|
||||
if errors.As(err, new(refreshRefused)) {
|
||||
err = codexHeldRefusal{err}
|
||||
}
|
||||
return "", "", err
|
||||
}
|
||||
return tok, accountID, nil
|
||||
}
|
||||
@@ -318,6 +326,14 @@ func heldByCodex(l savedLogin) bool {
|
||||
return sameRefresh(live.Auth, l.Auth)
|
||||
}
|
||||
|
||||
// codexHeldRefusal is a refresh OpenAI refused for an account Codex is
|
||||
// signed in to in another of its folders (codexHeldIn). savedRefreshFailed
|
||||
// doesn't mark it, magpie's copy not being the sign-in, so codexSignInRead
|
||||
// marks it signed out as it does Codex's own.
|
||||
type codexHeldRefusal struct{ error }
|
||||
|
||||
func (e codexHeldRefusal) Unwrap() error { return e.error }
|
||||
|
||||
// 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
|
||||
|
||||
@@ -915,12 +915,27 @@ func addLogin(l savedLogin) (using bool, err error) {
|
||||
live.Seen = l.Seen
|
||||
ls = upsertLogin(ls, live)
|
||||
}
|
||||
// signed in again to an account Codex keeps in another of its folders
|
||||
// (CODEX_HOME=…, charliepu on Discord): the new tokens go there, where
|
||||
// Codex and magpie both read it, or the next look would read the old
|
||||
// ones back over them
|
||||
held := ""
|
||||
if l.Agent == "codex" && !using {
|
||||
held = codexHeldFolder(ls, l)
|
||||
l.Home = held
|
||||
}
|
||||
// a second account is in use beside the first straight away, as a
|
||||
// second key is: it takes over when the first runs out
|
||||
l.On = !using
|
||||
if err := writeLogins(upsertLogin(ls, l)); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if held != "" {
|
||||
if err := saveCodexLogin(held, l.Auth); err != nil {
|
||||
return false, err
|
||||
}
|
||||
forgetCodexSignInGone(held)
|
||||
}
|
||||
if using {
|
||||
switch l.Agent {
|
||||
case "codex":
|
||||
@@ -943,6 +958,20 @@ func addLogin(l savedLogin) (using bool, err error) {
|
||||
return using, nil
|
||||
}
|
||||
|
||||
// codexHeldFolder is the other Codex folder that holds l's account now,
|
||||
// as saved in ls (Home, codexHomeLogins): "" when none does.
|
||||
func codexHeldFolder(ls []savedLogin, l savedLogin) string {
|
||||
for _, x := range ls {
|
||||
if x.Agent != "codex" || x.Home == "" || !sameLogin(x, l) {
|
||||
continue
|
||||
}
|
||||
if in, ok := codexLoginIn(x.Home); ok && sameLogin(in, l) {
|
||||
return x.Home
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// signInPage is what the browser shows at the end: magpie's, in its own
|
||||
// quiet black and white, never the vendor's "return to the CLI".
|
||||
// magpieLogo is magpie's mark (build/icon/magpie-small.svg), signing the
|
||||
|
||||
Reference in New Issue
Block a user