Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.
provider: a ChatGPT account whose usage read says OpenAI signed it out reads as signed out, with Sign in again (charliepu on Discord)
yetone88466f8bf2f1
diff --git a/docs/subsystems/providers-accounts.md b/docs/subsystems/providers-accounts.mdindex f49eaffdb..935eb334d 100644--- a/docs/subsystems/providers-accounts.md+++ b/docs/subsystems/providers-accounts.md@@ -38,8 +38,8 @@ keys and accounts per request is in [Gateway routing and fallback](gateway-routi| Token refresh | Each account refreshes through its vendor. For example, `codexToken` refreshes within 5 minutes of expiry and writes the rotated tokens back where it read them (`codexStore.write`: `~/.codex/auth.json`, or Codex's keychain item), every other field kept, for Codex CLI. Before refreshing a sign-in read from the keychain it asks the item again, so a rotation Codex made since isn't spent twice. A 400 or 401 from the vendor's refresh endpoint means it was refused (`refreshFailed`). | [`account.go`](../../internal/provider/account.go) || Codex's credential store | Codex 0.162 keeps its ChatGPT sign-in where `cli_auth_credentials_store` in `$CODEX_HOME/config.toml` says (`codex-rs/login/src/auth/storage.rs`): `file` (default) is `auth.json`; `keyring` the system keychain alone; `auto` the keychain, else `auth.json`, on read and on save; `ephemeral` Codex's memory. Saved to the keychain, Codex removes `auth.json` (#2312, #2367, Dony2050). On macOS magpie reads that item through `security`, as it reads Claude Code's: service `Codex Auth`, account `cli|` + the first 16 hex digits of SHA-256 of the canonical CODEX_HOME (`codexKeychainAccount`), the value auth.json's JSON on one line. `readCodexLogin` is the one reader: `codexAccount`, `codexToken`, `liveLogin`, `CodexAPIKeySignedIn`, `codexLoginAuth`, the usage card's `codexUntil`, session identities and the agent's wiring (`provider.CodexSignIn`: `codexChatGPT`, `Agent.Source`) go through it. `keyring` with no item is signed out; `auto` with no item, or one the keychain wouldn't give, is `auth.json`, as Codex does. An item the keychain wouldn't give under `keyring` is not known (`codexStoreError`), never signed out. With `[features] secret_auth_storage` on (Codex's default on Windows) keyring and auto mean Codex's encrypted auth storage, which magpie doesn't read; nor does it read the Linux or Windows keychain. There `keyring` and `ephemeral` are not known, `auto` is `auth.json` when Codex left one, and the saved accounts' reason says to set `file` (`codexSignedOutWhy`). Requirements an admin manages (`requirements.cli_auth_credentials_store`) aren't read. Reading the secret can put up to two macOS prompts in front of the user, who may type their password, so one read per item is in flight at a time and waits up to 2 minutes for them (`keychainAsk`, `askCodexKeychain`), outside the cache's lock. A reader waits at most 5s from when that ask started (`keychainHold`) and is otherwise told magpie is waiting for an answer (`errKeychainWaiting`); no request or page waits out the prompt. `security` exit 44 is no item; 51 (errSecAuthFailed) or 128 (errSecUserCanceled) is denied or cancelled (`errKeychainRefused`); the 2 minutes running out is no answer (`errKeychainNoAnswer`); anything else is `errKeychainUnread`. Each says so in the saved accounts' reason, and none of them is signed out. A read the keychain gave is kept while the item is unchanged: after 10s a reader asks only the item's modification date (`Stat`, `find-generic-password` without `-w`, which shows no prompt) and reads the secret again only when Codex or magpie changed it (#2367: with "Allow" once, background polls asked again every ~30s). A refusal or no answer is kept 10 minutes (#2408). magpie's own writes (`codexStore.write`, `keepCodexKeychain`) keep the date of what they wrote; a switch, sign-out or refresh forgets a read (`forgetCodexKeychain`) so the next one checks the date, but keeps a refusal; switching and refreshing read afresh. Beside a refusal, no answer or waiting, the saved accounts' line has Try again (`Exclusion.Retry`, `keychain-retry`, `RetryCodexKeychain`), which reads once past the refusal, and the reason says "Always Allow" lets `/usr/bin/security` read the item, so macOS stops asking each time Codex changes it. A switch or a first sign-in (`saveCodexLogin`) writes the store Codex reads, and refuses when that store couldn't be read, when config.toml doesn't parse, or when it is one magpie doesn't read; it leaves an `auth.json` beside the item to Codex. Signing the last account out (`deleteCodexLogin`) removes the item, then `auth.json`, as `codex logout`. Not tried against an item Codex itself wrote: macOS may ask whether magpie can read it, and Codex whether it can read one magpie rewrote. | [`codex_store.go`](../../internal/provider/codex_store.go), [`codex_store_test.go`](../../internal/provider/codex_store_test.go), [`codex_keychain_ask_unix_test.go`](../../internal/provider/codex_keychain_ask_unix_test.go) || Codex's other folders | Codex keeps one ChatGPT sign-in a folder (`codex-rs/login/src/auth/storage.rs`: `$CODEX_HOME/auth.json`, or the keychain item named by the folder) and writes its sessions there (`rollout/src/lib.rs`); `--profile` only lays `$CODEX_HOME/<name>.config.toml` over `config.toml`, the sign-in shared (`config/src/loader/mod.rs`). So a second account is Codex run with `CODEX_HOME=~/.codex-work`. `appdir.CodexHomes` finds those folders: `CodexHome` first, then folders in the home folder or beside `CodexHome` named `codex*` or `.codex*`, or one folder down in one (`~/.codex-profiles/work`), that hold `auth.json` or `sessions/`. Sessions and the usage ledger read every one (`sessions.codexFiles`, `Dirs`). `rememberLogins` saves each folder's account with `Home` set to it (`codexHomeLogins`); the one `~/.codex` is signed in to is that one's. Such an account is renewed in its folder, as Codex's own (`codexHeldIn` in `renewSaved`, as `heldByCodex` does for `~/.codex`), so the quota, the gateway and the keep-alive never spend a copy's refresh token under that folder's Codex. It isn't switched into `~/.codex` (`switchSavedLogin`, `NextLogin`'s spares) nor removed (`forgetLogin`): each error names the folder and the `CODEX_HOME=… codex` command. phantom3792 on X: only the default folder's sessions counted, and an account added from another folder read no quota (`TestCodexOtherHome*`, `TestCodexHomesIn`). Not done: Codex's thread names (`session_index.jsonl`) and a `sqlite_home` are read from `CodexHome` only (#3530). | [`codex.go`](../../internal/appdir/codex.go), [`logins.go`](../../internal/provider/logins.go), [`codex_homes_test.go`](../../internal/provider/codex_homes_test.go) |-| A revoked sign-in | A 401 whose words say the sign-in itself is gone (`SignInRevoked`: ChatGPT's `token_revoked`, `invalid_grant`, `refresh_token_reused`; never `token_expired`, a 403, a 5xx or a network error) marks the account for the credential it was sent with (`NoteRevokedSignIn`, #1793). A saved ChatGPT account is refreshed first, as Codex does on a 401, and a refresh that goes through marks nothing. The mark is a hash of the header, never the credential, kept in `logins.json` as `Refused: "revoked:<mark>"` with the account's `Lapsed`, and read back after a restart. While the account has that credential, the gateway doesn't send its requests (`RevokedSignIn`); its row says Sign-in required with Sign in again. Signed in anew (`upsertLogin` with another credential, `addSideLogin`), refreshed, or served a request (`ServedSignIn`), it is clear. Claude (`claude_auth.go`), Copilot (`copilot_lapsed.go`: GitHub's 401/403 on the token exchange, the CLI's stand-in or the usage read, cleared when a token is served) and plugins keep their own marks. | [`signin_revoked.go`](../../internal/provider/signin_revoked.go), [`copilot_lapsed.go`](../../internal/provider/copilot_lapsed.go) |-| A token called expired | A ChatGPT 401 whose words say the access token has expired and ask for a refresh (`SignInExpired`: `token_expired`, "Provided authentication token is expired. Please try refreshing it."; never `refresh_token_expired`, which is revoked) while the token's own `exp` is still ahead (willz on Discord, #2973: two saved Team accounts) is mended by one refresh. `codexToken` refreshes only as `exp` nears, so before this the token went out as it was until the user signed in again, and the card said only Unauthorized. The usage read (`codexWindowsRenewing`: `loginQuota`, `codexSubscriptionUsage`, and the reset spenders through `codexUserWindows`) refreshes once and reads once more, never again: a refresh refused says to sign in again (a saved account is marked lapsed, `savedRefreshFailed`; Codex's own says `codex login`), and a token refreshed for it and still called expired says so too (`errCodexStillExpired`). The gateway renews the account a request went with (`NoteExpiredSignIn` from `noteSignIn`, 20s at most); the request itself fails over as before, and the next goes with the new token. Each refresh is keyed on the token turned away (`codexTokenRenewing`, `renewSavedLoginFrom`), under the lock signing waits on: a sign-in holding another token by then, renewed by Codex or another reply, is used as it is, so two replies for one token spend one rotation. Codex's own sign-in is read from its store afresh for it and written back through `codexStore.write` (temp and rename), a saved one's through `writeLogins`. Not tried against the real backend. | [`codex_expired.go`](../../internal/provider/codex_expired.go), [`account.go`](../../internal/provider/account.go), [`logins_on.go`](../../internal/provider/logins_on.go) |+| A revoked sign-in | A 401 whose words say the sign-in itself is gone (`SignInRevoked`: ChatGPT's `token_revoked`, `invalid_grant`, `refresh_token_reused`; never `token_expired`, a 403, a 5xx or a network error) marks the account for the credential it was sent with (`NoteRevokedSignIn`, #1793). A saved ChatGPT account is refreshed first, as Codex does on a 401, and a refresh that goes through marks nothing. The mark is a hash of the header, never the credential, kept in `logins.json` as `Refused: "revoked:<mark>"` with the account's `Lapsed`, and read back after a restart. While the account has that credential, the gateway doesn't send its requests (`RevokedSignIn`); its row says Sign-in required with Sign in again. A ChatGPT account's usage read marks it too (`codexSignInRead` in `loginQuota`, charliepu on Discord: an account OpenAI signed out showed only a blank allowance): `/wham/usage` answering 401 with those words is refreshed once (`codexWindowsRenewing`), and the account is marked for the token turned away when the refreshed read is turned away the same (`codexSignInGone`), or, for the one Codex is signed in to, when its refresh is refused (its row used to say nothing, its allowance line "run codex login"; a saved one's refused refresh is `savedRefreshFailed`'s). Any other 401, a 403, a 5xx or a network error is unknown and marks nothing. Sign in again runs magpie's own sign-in for the account (`addLogin`), which replaces its tokens, in Codex's `auth.json` too when Codex is signed in to it. Signed in anew (`upsertLogin` with another credential, `addSideLogin`), refreshed, served a request (`ServedSignIn`), or its usage read going through, it is clear. Claude (`claude_auth.go`), Copilot (`copilot_lapsed.go`: GitHub's 401/403 on the token exchange, the CLI's stand-in or the usage read, cleared when a token is served) and plugins keep their own marks. | [`signin_revoked.go`](../../internal/provider/signin_revoked.go), [`copilot_lapsed.go`](../../internal/provider/copilot_lapsed.go) |+| A token called expired | A ChatGPT 401 whose words say the access token has expired and ask for a refresh (`SignInExpired`: `token_expired`, "Provided authentication token is expired. Please try refreshing it."; never `refresh_token_expired`, which is revoked) while the token's own `exp` is still ahead (willz on Discord, #2973: two saved Team accounts) is mended by one refresh. `codexToken` refreshes only as `exp` nears, so before this the token went out as it was until the user signed in again, and the card said only Unauthorized. The usage read (`codexWindowsRenewing`: `loginQuota`, `codexSubscriptionUsage`, and the reset spenders through `codexUserWindows`) refreshes once and reads once more, never again: a refresh refused says to sign in again (a saved account is marked lapsed, `savedRefreshFailed`; Codex's own is marked revoked, see A revoked sign-in, through `loginQuota`, while `codexSubscriptionUsage`'s card still says `codex login`), and a token refreshed for it and still called expired says so too (`errCodexStillExpired`) and is marked revoked the same. The gateway renews the account a request went with (`NoteExpiredSignIn` from `noteSignIn`, 20s at most); the request itself fails over as before, and the next goes with the new token. Each refresh is keyed on the token turned away (`codexTokenRenewing`, `renewSavedLoginFrom`), under the lock signing waits on: a sign-in holding another token by then, renewed by Codex or another reply, is used as it is, so two replies for one token spend one rotation. Codex's own sign-in is read from its store afresh for it and written back through `codexStore.write` (temp and rename), a saved one's through `writeLogins`. Not tried against the real backend. | [`codex_expired.go`](../../internal/provider/codex_expired.go), [`account.go`](../../internal/provider/account.go), [`logins_on.go`](../../internal/provider/logins_on.go) || Saved accounts | `logins.json` remembers every Codex and Claude Code account seen signed in. A switch moves the credentials into the agent's own store and saves the ones it replaces. A saved account that is on (`SetLoginOn`) stands behind the agent's own. Its tokens stay in `logins.json`. While Claude Code is signed out, a saved Claude account stands in for its own (`claudeStandIn`); with every saved account's sign-in gone, Claude Code is still listed, on the one seen last, signed out (`claudeListed`), its accounts each saying to sign in again, so it never leaves the Providers list (LeeHom on Discord). Claude Code's own account while still `Held` is never listed that way, since the sign-in it may have left on disk would be served. An account whose every login in use has lapsed is `Provider.SignedOut`: `magpie providers` and the TUI say "<user> is signed out", as the app's row does. | [`logins.go`](../../internal/provider/logins.go), [`logins_on.go`](../../internal/provider/logins_on.go), [`claude_dirs.go`](../../internal/provider/claude_dirs.go) || Sign-in in magpie | `StartSignIn` runs the vendor's own browser OAuth with its client and takes the tokens at a callback on this machine. A browser that can't reach that callback (magpie on a server or in Docker) ends on a page that won't load; its address, pasted (`SubmitSignInCallback`), goes through the same callback handler (`pastedCallback`). ChatGPT's sign-in comes back only to port 1455; when that port can't be opened, the sign-in still starts and waits for the pasted address alone, and says why in `CallbackBlocked` with the port in `CallbackPort`: `reserved` for Windows' WSAEACCES on a port in a range it excludes for Hyper-V, WSL or Docker, `busy` for anything else, such as another program holding it (`callbackBlocked`, #1782). A plugin's browser sign-in (`auto`) whose page names a port on this machine to come back to takes its pasted address too: magpie opens it there on 127.0.0.1, for the plugin's own listener (`pluginCallback`, `loopbackPorts`); Command Code's page posts its key from script, so it offers the API key way instead. A Claude account is signed in by Claude Code itself. Signing in brings back an account removed from magpie (`ShowAccount`). A sign-in started in magpie (`StartSignIn`, `StartPluginSignIn`) reads as done (`SignInStatus`, `WaitSignIn`) only after `finish` has run `ShowAccount` for its account, so the window, which opens the account as soon as it sees done, finds it listed. If providers.json can't be written then, the account stays hidden and the sign-in still ends done. Signing in to a plugin's provider from the command line (`pluginLogin`, with a key or the browser) brings it back too. It also takes off the mark a sign-in its vendor turned away left on the account (`Lapsed`) and shows again the agent's own account removed in magpie (`clearPluginLapse`). The window's plugin sign-in does the same with the browser (`pluginDone`, then `finish`) or with a key (`PluginAPIKey`); a key and the command line share these steps in `PluginSignedIn`. A sign-in leaves through the proxy the account's own requests will use (#1663): `ProxyOfLogin` (the account's, else the subscription's, else the global one) for the account being signed in again, the subscription's for a new one. Every request of the flow goes through `signInFlow.via`, Claude Code's own login gets it as *_PROXY (`netproxy.EnvWith`), and a plugin's sign-in runs its fetch under that proxy (`via` in `host.js`; `plugins_cli.go` for the command line). The window's sign-in box shows it as a "Proxy: …" link (`signInProxyLink`, the app menu, no userinfo shown) with Global, Direct, the subscription's and each account's proxy, or a typed one; picking one starts the sign-in again through it (`SignInOptions.Proxy`), and when it ends the pick is kept as the account's (`keepSignInProxy`: the subscription's own when it is its only account, else the account's entry). Global isn't offered while the subscription has a proxy and other accounts, since nothing could keep it. The browser's own leg goes through the browser's network, not magpie's. Signing out leaves through the same exit: a ChatGPT account's refresh token is revoked through its own proxy, read before the account is forgotten (`forgetSIWCLogin`), and Devin's CLI, asked who is signed in or for its models, runs with the account's proxy (`devinCommand`, `devinProxy`). | [`signin.go`](../../internal/provider/signin.go), [`claude_signin.go`](../../internal/provider/claude_signin.go), [`pluginsignin.go`](../../internal/provider/pluginsignin.go), [`pluginsignin_paste.go`](../../internal/provider/pluginsignin_paste.go), [`proxy.go`](../../internal/provider/proxy.go), [`plugins_cli.go`](../../internal/cli/plugins_cli.go) || Import from files | `ImportLogins` (ChatGPT, Claude), `ImportGoogleAccounts` (Antigravity) and `ImportFactoryKeys` take other tools' files, from the window (`signin/import`) or `magpie accounts import` (#1453). ChatGPT reads Codex's `auth.json`, CLIProxyAPI's and Cockpit Tools' files, Sub2API's export (`sub2apiEntry`) and codexbar's `~/.codexbar/config.json` (`codexbarEntries`: the `openai_oauth` provider's accounts and `openAI.remoteConnectionAccounts`, the workspace from `openAIAccountId`). Each ChatGPT and Google sign-in is refreshed before it is added, so the tool the file came from is signed out of it; both surfaces say so first, and the command imports nothing without `--yes` or a yes. Claude reads CLIProxyAPI's files and Sub2API's export, whose Claude account says its email as Anthropic does, `email_address`, in `credentials` or `extra`, and keeps its `scope`, `account_uuid` and `org_uuid` as Claude Code's scopes, `accountUuid` and `organizationUuid` (#1975); a file with no email is refused, as Claude Code's own `.credentials.json` is. Claude's are kept as the file has them. A refresh token magpie holds already is `exists`, never refreshed; accounts are matched by email and workspace, or for Claude organization (`sameLogin`). An entry with an access token and no refresh token is refused (`accessOnly`). One file of several that holds no account is a failed row with its place (`ImportedAccount.File`), never skipped. The files are only read. | [`login_import.go`](../../internal/provider/login_import.go), [`google_import.go`](../../internal/provider/google_import.go), [`factory_key.go`](../../internal/provider/factory_key.go), [`accounts_cli.go`](../../internal/cli/accounts_cli.go) |diff --git a/internal/provider/codex_expired.go b/internal/provider/codex_expired.goindex c2b516284..a5593c198 100644--- a/internal/provider/codex_expired.go+++ b/internal/provider/codex_expired.go@@ -10,6 +10,7 @@ package providerimport ("context"+ "encoding/json""errors""log""net/http"@@ -45,12 +46,35 @@ func codexExpired(err error) bool {// the token refreshed just for it: nothing magpie holds will do.var errCodexStillExpired = errors.New("ChatGPT turned the refreshed sign-in away too; sign in to this account again")+// codexRevoked says err is the backend calling the sign-in itself gone+// (SignInRevoked: token_revoked, an invalidated oauth token).+func codexRevoked(err error) bool {+ var se *accountStatusError+ return errors.As(err, &se) && SignInRevoked(se.status, se.body)+}++// codexSignInGone is a usage read turned away for the sign-in itself with+// a refresh tried for it (charliepu on Discord: an account showing no+// allowance until it was signed in again): the account is to be marked+// signed out for tok, the access token turned away, and signed in again+// in magpie. A refresh refused is told by refreshRefused instead.+type codexSignInGone struct {+ tok string+ err error+}++func (e *codexSignInGone) Error() string { return e.err.Error() }+func (e *codexSignInGone) Unwrap() error { return e.err }+// codexWindowsRenewing is codexWindows, read once more with the sign-in-// renewed when the backend calls token expired. A refresh refused says-// to sign in again (codexRefresh, savedRefreshFailed).+// renewed when the backend calls the token expired or the sign-in+// revoked, as Codex refreshes on a 401. A refresh refused says to sign in+// again (codexRefresh, savedRefreshFailed); a refreshed token turned away+// the same is a codexSignInGone. Any other 401, a 403, a 5xx or a network+// error is returned as it is: what it says of the sign-in is unknown.func codexWindowsRenewing(ctx context.Context, tok, accountID string, renew codexRenew) (plan string, out []QuotaWindow, resets *ResetCredits, credits string, held bool, err error) {plan, out, resets, credits, held, err = codexWindows(ctx, tok, accountID)- if renew == nil || !codexExpired(err) {+ if renew == nil || !codexExpired(err) && !codexRevoked(err) {return}fresh, id, rerr := renew(ctx, tok)@@ -61,12 +85,39 @@ func codexWindowsRenewing(ctx context.Context, tok, accountID string, renew codeaccountID = id}plan, out, resets, credits, held, err = codexWindows(ctx, fresh, accountID)- if codexExpired(err) {- err = errCodexStillExpired+ switch {+ case codexExpired(err):+ err = &codexSignInGone{tok: fresh, err: errCodexStillExpired}+ case codexRevoked(err):+ err = &codexSignInGone{tok: fresh, err: err}}return}+// codexGoneFor is the access token a ChatGPT account's usage read says is+// signed out for good: one a refreshed read was turned away for, or, for+// a refresh refused, the token held (heldTok, read again from auth when+// the refresh came before any read). "" when err says nothing of the+// sign-in: a failed read is unknown, not signed out.+func codexGoneFor(err error, heldTok string, auth func() []byte) string {+ var gone *codexSignInGone+ if errors.As(err, &gone) {+ return gone.tok+ }+ var refused refreshRefused+ if !errors.As(err, &refused) {+ return ""+ }+ if heldTok != "" {+ return heldTok+ }+ var a codexAuth+ if b := auth(); b != nil && json.Unmarshal(b, &a) == nil {+ return a.Tokens.AccessToken+ }+ return ""+}+// NoteExpiredSignIn renews the ChatGPT sign-in of a, whose request req the// backend answered 401 token expired while the token had time left by// its exp, so the next request goes with a new one. The request itselfdiff --git a/internal/provider/codex_expired_test.go b/internal/provider/codex_expired_test.goindex 0f651b813..3ba1506ac 100644--- a/internal/provider/codex_expired_test.go+++ b/internal/provider/codex_expired_test.go@@ -24,6 +24,8 @@ const expiredBody = `{"error":{"message":"Provided authentication token is expirfunc codexExpiredFakes(t *testing.T, refused func(tok string) (int, string), refuseRefresh bool) (home string, refreshes func() []string, reads func() map[string]int) {t.Helper()home = signIn(t)+ resetRevoked() // marks are read from this HOME's logins.json+ t.Cleanup(resetRevoked)rememberLogins(true)codexSignIn(t, home, "[email protected]", "r-work")rememberLogins(true)@@ -143,7 +145,8 @@ func TestCodexUsageExpiredOnce(t *testing.T) {if e := u["[email protected]"].Error; !strings.Contains(e, "add the account again") {t.Fatalf("saved account: %q", e)}- if e := u["[email protected]"].Error; !strings.Contains(e, "codex login") {+ // signed in again in magpie, not with codex login (charliepu)+ if e := u["[email protected]"].Error; e != revokedText("[email protected]") {t.Fatalf("Codex's own account: %q", e)}if n := len(refreshes()); n != 2 {@@ -160,7 +163,8 @@ func TestCodexUsageExpiredOnce(t *testing.T) {_, refreshes, reads := codexExpiredFakes(t, always, false)u := LoginUsage(context.Background(), "codex")for _, user := range []string{"[email protected]", "[email protected]"} {- if e := u[user].Error; e != errCodexStillExpired.Error() {+ // signed out for the refreshed token too (charliepu)+ if e := u[user].Error; e != revokedText(user) {t.Fatalf("%s: %q", user, e)}}diff --git a/internal/provider/codex_signed_out_test.go b/internal/provider/codex_signed_out_test.gonew file mode 100644index 000000000..ad943f215--- /dev/null+++ b/internal/provider/codex_signed_out_test.go@@ -0,0 +1,226 @@+package provider++import (+ "context"+ "net/http"+ "strings"+ "sync"+ "testing"+ "time"+)++// A ChatGPT account OpenAI signed out (charliepu on Discord: an account+// showing no allowance until it was signed in again) reads as signed out+// on its row once its usage read says so, with a refresh tried first, and+// a sign-in anew in magpie gives it back. A read that fails for anything+// else marks nothing.++// revokedUsageBody is ChatGPT's 401 for a sign-in it revoked (#1793).+const revokedUsageBody = `{"error":{"message":"Encountered invalidated oauth token for user, failing request","type":null,"code":"token_revoked","param":null},"status":401}`++// deadTokens answers every token in dead with revokedUsageBody.+type deadTokens struct {+ sync.Mutex+ dead map[string]bool+ all bool // every token, a refreshed one too+}++func (d *deadTokens) refuse(tok string) (int, string) {+ d.Lock()+ defer d.Unlock()+ if d.all || d.dead[tok] {+ return http.StatusUnauthorized, revokedUsageBody+ }+ return 0, ""+}++func codexLapsed(t *testing.T) map[string]string {+ t.Helper()+ out := map[string]string{}+ for _, l := range Logins("codex") {+ out[l.User] = l.Lapsed+ }+ return out+}++func freshUsage() {+ loginUsageCache.Lock()+ loginUsageCache.m = nil+ loginUsageCache.Unlock()+}++// codexAccessTokens are the access tokens each account is signed in with+// now: the saved one's in logins.json, Codex's own in its auth.json.+func codexAccessTokens(t *testing.T) map[string]string {+ t.Helper()+ out := map[string]string{}+ for _, l := range Logins("codex") {+ out[l.User] = codexAccessToken(codexLoginAuth(l))+ }+ return out+}++func codexAccessToken(b []byte) string {+ return codexGoneFor(refreshRefused("x"), "", func() []byte { return b })+}++// Revoked with a refresh that goes through: the fresh token is read, and+// nobody is signed out.+func TestCodexUsageRevokedRefreshedFirst(t *testing.T) {+ d := &deadTokens{}+ _, refreshes, _ := codexExpiredFakes(t, d.refuse, false)+ d.dead = map[string]bool{}+ for _, tok := range codexAccessTokens(t) {+ d.dead[tok] = true+ }+ u := LoginUsage(context.Background(), "codex")+ for _, user := range []string{"[email protected]", "[email protected]"} {+ if q := u[user]; q.Error != "" || len(q.Windows) != 1 {+ t.Fatalf("%s: %+v", user, q)+ }+ }+ if n := len(refreshes()); n != 2 {+ t.Fatalf("%d refreshes, want one each", n)+ }+ for user, l := range codexLapsed(t) {+ if l != "" {+ t.Fatalf("%s said signed out though its refresh went through: %q", user, l)+ }+ }+}++// Revoked and its refresh refused: both accounts' rows say to sign in+// again, Codex's own too, which used to say "run codex login" on its+// allowance line and nothing on its row. Signed in again in magpie, the+// account is back, its tokens replaced, not a second one added.+func TestCodexUsageRevokedSignedOutTillSignedInAgain(t *testing.T) {+ d := &deadTokens{}+ home, _, _ := codexExpiredFakes(t, d.refuse, true)+ d.dead = map[string]bool{}+ for _, tok := range codexAccessTokens(t) {+ d.dead[tok] = true+ }+ stale := codexAccessTokens(t)["[email protected]"]++ u := LoginUsage(context.Background(), "codex")+ lapsed := codexLapsed(t)+ if e := u["[email protected]"].Error; !strings.Contains(e, "add the account again") {+ t.Fatalf("saved account: %q", e)+ }+ if !strings.Contains(lapsed["[email protected]"], "add the account again") {+ t.Fatalf("saved account's row: %q", lapsed["[email protected]"])+ }+ if e := u["[email protected]"].Error; e != revokedText("[email protected]") {+ t.Fatalf("Codex's own account: %q", e)+ }+ if l := lapsed["[email protected]"]; l != revokedText("[email protected]") {+ t.Fatalf("Codex's own account's row isn't signed out: %q", l)+ }+ if _, ok := RevokedSignIn(&Account{Agent: "codex", User: "[email protected]"}, revokedReq(t, stale)); !ok {+ t.Fatal("the gateway would still send the refused sign-in")+ }++ // signed in again through magpie's own sign-in: the same account, its+ // tokens replaced where Codex keeps them+ exp := float64(time.Now().Add(time.Hour).Unix())+ again, err := codexLogin(fakeJWT(map[string]any{"email": "[email protected]", "https://api.openai.com/auth": map[string]any{"chatgpt_plan_type": "plus"}}),+ fakeJWT(map[string]any{"exp": exp, "n": "again"}), "r-again", "[email protected]")+ if err != nil {+ t.Fatal(err)+ }+ if using, err := addLogin(again); err != nil || !using {+ t.Fatalf("signed in again: using %v, %v", using, err)+ }+ users, active := loginUsers(Logins("codex"))+ if strings.Join(users, ",") != "[email protected],[email protected]" || active != "[email protected]" {+ t.Fatalf("after signing in again: %v, active %q", users, active)+ }+ if got := codexAccessTokens(t)["[email protected]"]; got == stale || got == "" {+ t.Fatalf("Codex's auth.json kept the refused token")+ }+ if l := codexLapsed(t)["[email protected]"]; l != "" {+ t.Fatalf("signed in again, still said signed out: %q", l)+ }+ freshUsage()+ if q := LoginUsage(context.Background(), "codex")["[email protected]"]; q.Error != "" || len(q.Windows) != 1 {+ t.Fatalf("read after signing in again: %+v", q)+ }+ _ = home+}++// A refreshed token turned away the same: the account is signed out for+// that token, and a read that goes through later clears it.+func TestCodexUsageRevokedAfterRefreshSignedOut(t *testing.T) {+ d := &deadTokens{all: true}+ _, refreshes, _ := codexExpiredFakes(t, d.refuse, false)+ u := LoginUsage(context.Background(), "codex")+ for _, user := range []string{"[email protected]", "[email protected]"} {+ if e := u[user].Error; e != revokedText(user) {+ t.Fatalf("%s: %q", user, e)+ }+ if l := codexLapsed(t)[user]; l != revokedText(user) {+ t.Fatalf("%s's row: %q", user, l)+ }+ }+ if n := len(refreshes()); n != 2 {+ t.Fatalf("%d refreshes, want one each", n)+ }+ if _, ok := RevokedSignIn(&Account{Agent: "codex", User: "[email protected]"}, revokedReq(t, "fresh-r")); !ok {+ t.Fatal("the refreshed token turned away isn't marked")+ }++ d.Lock()+ d.all = false+ d.Unlock()+ freshUsage()+ u = LoginUsage(context.Background(), "codex")+ for _, user := range []string{"[email protected]", "[email protected]"} {+ if q := u[user]; q.Error != "" {+ t.Fatalf("%s: %+v", user, q)+ }+ if l := codexLapsed(t)[user]; l != "" {+ t.Fatalf("%s served, still said signed out: %q", user, l)+ }+ }+}++// A read that fails for anything but the sign-in is unknown: a plain 401,+// a 403, a 5xx and a network error mark no one.+func TestCodexUsageOtherFailuresMarkNobody(t *testing.T) {+ for _, c := range []struct {+ name string+ code int+ body string+ }{+ {"plain 401", 401, `{"error":{"message":"Unauthorized"}}`},+ {"403 with the words", 403, revokedUsageBody},+ {"502", 502, `bad gateway token_revoked`},+ } {+ t.Run(c.name, func(t *testing.T) {+ resetRevoked()+ t.Cleanup(resetRevoked)+ _, refreshes, _ := codexExpiredFakes(t, func(string) (int, string) { return c.code, c.body }, true)+ u := LoginUsage(context.Background(), "codex")+ for user, l := range codexLapsed(t) {+ if l != "" || strings.Contains(u[user].Error, "sign in again") {+ t.Fatalf("%s: lapsed %q, error %q", user, l, u[user].Error)+ }+ }+ if n := len(refreshes()); n != 0 {+ t.Fatalf("%d refreshes for a failure that isn't the sign-in's", n)+ }+ })+ }+ t.Run("network error", func(t *testing.T) {+ resetRevoked()+ t.Cleanup(resetRevoked)+ codexExpiredFakes(t, func(string) (int, string) { return 0, "" }, true)+ CodexBase = "http://127.0.0.1:1/backend-api/codex"+ u := LoginUsage(context.Background(), "codex")+ for user, l := range codexLapsed(t) {+ if l != "" || strings.Contains(u[user].Error, "sign in again") {+ t.Fatalf("%s: lapsed %q, error %q", user, l, u[user].Error)+ }+ }+ })+}diff --git a/internal/provider/login_usage.go b/internal/provider/login_usage.goindex c24fece61..d7c50030a 100644--- a/internal/provider/login_usage.go+++ b/internal/provider/login_usage.go@@ -6,6 +6,7 @@ package providerimport ("context"+ "errors""net/http""strings""sync"@@ -365,12 +366,42 @@ func loginQuota(ctx context.Context, l Login) SubscriptionQuota {q.Until = codexUntil(codexLoginAuth(l), time.Now())}}+ if l.Agent == "codex" {+ err = codexSignInRead(l, tok, err)+ }if err != nil {q.Error = err.Error()}return q}+// codexSignInRead keeps what a ChatGPT account's usage read, made with+// access token tok, said of its sign-in (charliepu on Discord: an account+// showing no allowance, OpenAI having signed it out, till it was signed in+// again). Turned away for the sign-in itself with a refresh tried, or the+// refresh of the one Codex is signed in to refused, the account is marked+// 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+// clears a mark it had. Anything else is unknown and marks nothing.+func codexSignInRead(l Login, tok string, err error) error {+ if err == nil {+ ServedSignIn(&Account{Agent: l.Agent, User: l.User})+ return nil+ }+ var refused refreshRefused+ if !l.Active && errors.As(err, &refused) {+ return err+ }+ m := bearerMark(codexGoneFor(err, tok, func() []byte { return codexLoginAuth(l) }))+ if m == "" {+ return err+ }+ markRevoked(l.Agent, l.User, m)+ return errors.New(revokedText(l.User))+}+// CodexUsedUp reports whether the ChatGPT account Codex is signed in to is// out for the Codex app, which then sends nothing for it (codexHeld): out// of its allowance with no credits to go on with. A window at 100% withdiff --git a/internal/provider/signin_revoked.go b/internal/provider/signin_revoked.goindex f527493de..17ca04810 100644--- a/internal/provider/signin_revoked.go+++ b/internal/provider/signin_revoked.go@@ -157,22 +157,40 @@ func NoteRevokedSignIn(ctx context.Context, a *Account, req *http.Request) {return}}- why := revokedText(a.User)+ markRevoked(a.Agent, a.User, m)+}++// markRevoked marks the account of agent and user revoked for the+// credential whose mark is m: the gateway passes over it while it has+// that credential, and its row says to sign in again.+func markRevoked(agent, user, m string) {+ why := revokedText(user)revoked.Lock()revokedLoad()- k := revokedKey(a.Agent, a.User)+ k := revokedKey(agent, user)if was, ok := revoked.accounts[k]; ok && was != m {delete(revoked.marks, was) // logins.json keeps one mark an account}revoked.marks[m] = whyrevoked.accounts[k] = mrevoked.Unlock()- _ = editSideLogin(a.Agent, a.User, func(ls []savedLogin, i int) ([]savedLogin, error) {+ _ = editSideLogin(agent, user, func(ls []savedLogin, i int) ([]savedLogin, error) {ls[i].Lapsed, ls[i].Refused = why, revokedPrefix+mreturn ls, nil})}+// bearerMark is credentialMark of a request sent with access token tok,+// as the gateway and a usage read send a ChatGPT account's.+func bearerMark(tok string) string {+ if tok == "" {+ return ""+ }+ h := http.Header{}+ h.Set("Authorization", "Bearer "+tok)+ return credentialMark(h)+}+// ServedSignIn notes a request account a's vendor served: a mark of a// revoked sign-in it has is gone, as the account was signed in again or// its sign-in refreshed.