Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.
provider: Codex and ChatGPT API refreshes lock across magpies
yetone85cde4a4deb7
diff --git a/docs/subsystems/providers-accounts.md b/docs/subsystems/providers-accounts.mdindex c01c94204..b8b57dc76 100644--- a/docs/subsystems/providers-accounts.md+++ b/docs/subsystems/providers-accounts.md@@ -236,6 +236,19 @@ and `TestHistoricalWindowHoldsUsesEachWindowsOwnReading`.- The Vertex AI preset's `HeaderHints` are Google's Priority PayGo and Flex PayGo headers, `X-Vertex-AI-LLM-Request-Type` and `X-Vertex-AI-LLM-Shared-Request-Type`: the editor offers to add them, the values are the user's, and they go as `Headers` do. Priority PayGo couldn't be confirmed: on the project it was tried on, Vertex AI answered with `trafficType` `ON_DEMAND`.- A usage cap is kept per account (`AccountCaps`, 1–99) and, beside it, per window (`AccountWindowCaps`, by the account and the window's `capId`, its name in lower case: `WindowCapID`). A window's own share wins over the account's; 100 is no cap on that window. `CapsOf` gives both (`WindowCaps`), `CapHeld` and `capReached` judge each window by its own, and `SetWindowCap` (GUI `provider/windowcap`, `magpie provider account-cap <id> <account> --window <name>`) sets one. A config without window caps reads as before.- A window whose reset time has passed has started again, whatever share it was read at (`resetPassed`). That holds for a reading kept from before (`AsOf`) and for a fresh one: Claude Code's `/usage` gives a reset to the minute and can lag it, so a run just after a reset can still say 100% with that reset. `claudeWindows` hands such a window back empty on every path, and `usedPast`, `usedUp`, `BackAt` and `capReached` leave it out when they judge an account spent, for Codex, Claude Code and `magpie quota wait` alike. A card can still show a kept reading as it was read (`reported`, for a Claude card while `/usage` is unavailable); that changes what it shows, never which account is used. A reset `/usage` gives as a time alone ("resets 3pm") can be read as its next one, a day on (`claudeResetTime`), and is then not seen as passed here.+- A restart to update can run two magpies for up to the old one's drain+ ([GUI shell](gui-shell.md#a-restart-to-update-hands-the-gateway-over)), each+ able to refresh a sign-in. A refresh that rotates its refresh token holds+ a file lock beside `logins.json`, so the second waits and takes the+ first's tokens instead of spending the used one: Codex's `auth.json`+ (`codexMu`, `.codex.lock`) and the ChatGPT API's sign-in (`siwcRefreshing`,+ `.siwc.lock`) since this change; Claude's saved tokens (`savedTokenMu`),+ MCP servers' (`mcpauth`'s renew lock) and plugins' were already held so.+ Google's, Vertex AI's and Copilot's refresh tokens don't rotate. Claude+ Code run in its own folder (`claudeDirRun`) is held in this process only;+ the bridge's turns hold the restart back, so the two don't overlap there.+ `TestCodexRefreshWaitsForAnotherMagpie`,+ `TestChatGPTAPIRefreshWaitsForAnotherMagpie`.## Verificationdiff --git a/internal/provider/account.go b/internal/provider/account.goindex 6793e07f7..0ce7ff4fd 100644--- a/internal/provider/account.go+++ b/internal/provider/account.go@@ -36,6 +36,7 @@ import ("github.com/yetone/magpie/internal/appdir""github.com/yetone/magpie/internal/catalog""github.com/yetone/magpie/internal/claudecode"+ "github.com/yetone/magpie/internal/filelock""github.com/yetone/magpie/internal/plugin""github.com/yetone/magpie/internal/proc""github.com/yetone/magpie/internal/settings"@@ -955,7 +956,13 @@ func CodexTokenURLForTest(u string) func() {// can point it elsewhere.var CodexBase = "https://chatgpt.com/backend-api/codex"-var codexMu sync.Mutex+// codexMu keeps two refreshes of a Codex sign-in from running at once,+// in this magpie and across magpies: the refresh token rotates, and the+// second would send the one the first spent (refresh_token_reused). Two+// magpies run at once while a restart hands the gateway over (xuanwo on+// X), and beside a magpie run from a terminal. The sign-in is read after+// it is taken, so one refreshed meanwhile is used as it is.+var codexMu = filelock.Mutex{Path: func() string { return loginsPath() + ".codex.lock" }}type codexAuth struct {AuthMode string `json:"auth_mode"`diff --git a/internal/provider/chatgpt_api.go b/internal/provider/chatgpt_api.goindex 856c46866..0d1ae165f 100644--- a/internal/provider/chatgpt_api.go+++ b/internal/provider/chatgpt_api.go@@ -32,11 +32,11 @@ import ("path/filepath""slices""strings"- "sync""time""github.com/yetone/magpie/internal/appdir""github.com/yetone/magpie/internal/catalog"+ "github.com/yetone/magpie/internal/filelock")// ChatGPTAPIID is the subscription's id, as a provider and in logins.json.@@ -373,9 +373,11 @@ func siwcFromToken(tok siwcTokenReply, c siwcCreds) (siwcCreds, error) {var siwcDead = []string{"invalid_grant", "invalid_refresh_token", "token_expired", "refresh_token_expired","refresh_token_invalidated", "refresh_token_reused"}-// siwcRefreshing keeps two requests from refreshing at once: the refresh-// token rotates, and the second would send the one the first used up.-var siwcRefreshing sync.Mutex+// siwcRefreshing keeps two requests from refreshing at once, in this+// magpie and across magpies (two run at once while a restart hands the+// gateway over): the refresh token rotates, and the second would send the+// one the first used up. The account is looked up again once it is held.+var siwcRefreshing = filelock.Mutex{Path: func() string { return loginsPath() + ".siwc.lock" }}// siwcFresh is an account's credentials with an access token that isn't// about to expire, refreshed when it is, or when force is set (OpenAIdiff --git a/internal/provider/refresh_across_magpies_test.go b/internal/provider/refresh_across_magpies_test.gonew file mode 100644index 000000000..af735732b--- /dev/null+++ b/internal/provider/refresh_across_magpies_test.go@@ -0,0 +1,121 @@+package provider++import (+ "context"+ "encoding/json"+ "net/http"+ "net/http/httptest"+ "path/filepath"+ "sync/atomic"+ "testing"+ "time"++ "github.com/yetone/magpie/internal/filelock"+)++// anotherMagpieRefreshing holds lock as another magpie mid-refresh would+// (a restart handing the gateway over runs two at once, xuanwo on X),+// runs refresh, gives it the time it takes to get past an in-process+// lock, then has the other magpie's refresh land and let go. What+// refresh returned.+func anotherMagpieRefreshing[T any](t *testing.T, lock string, refresh func() T, lands func()) T {+ t.Helper()+ unlock, held := filelock.Lock(lock, time.Second)+ if !held {+ t.Fatal("the lock wasn't to be had")+ }+ got := make(chan T, 1)+ go func() { got <- refresh() }()+ select {+ case v := <-got: // refreshed alongside the other magpie+ unlock()+ return v+ case <-time.After(300 * time.Millisecond):+ }+ lands()+ unlock()+ select {+ case v := <-got:+ return v+ case <-time.After(15 * time.Second):+ t.Fatal("never refreshed")+ }+ panic("unreachable")+}++// A Codex sign-in another magpie is refreshing is waited for, and its+// rotated tokens used, rather than refreshed again with the refresh token+// it spent.+func TestCodexRefreshWaitsForAnotherMagpie(t *testing.T) {+ isolate(t)+ var refreshes atomic.Int32+ fake := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ refreshes.Add(1)+ json.NewEncoder(w).Encode(map[string]any{+ "access_token": fakeJWT(map[string]any{"exp": float64(time.Now().Add(time.Hour).Unix())}),+ "refresh_token": "r-reused"})+ }))+ t.Cleanup(fake.Close)+ old := codexTokenURL+ codexTokenURL = fake.URL+ t.Cleanup(func() { codexTokenURL = old })++ path, _ := dueCodexAuth(t)+ theirs := fakeJWT(map[string]any{"exp": float64(time.Now().Add(time.Hour).Unix()), "by": "the other magpie"})+ type answer struct {+ tok string+ err error+ }+ got := anotherMagpieRefreshing(t, loginsPath()+".codex.lock", func() answer {+ tok, _, err := codexToken(context.Background(), filepath.Dir(path))+ return answer{tok, err}+ }, func() {+ writeFile(t, path, map[string]any{"tokens": map[string]any{+ "access_token": theirs, "refresh_token": "r-rotated", "account_id": "acct-1"}})+ })+ if got.err != nil || got.tok != theirs || refreshes.Load() != 0 {+ t.Fatalf("refreshed %d times beside another magpie: %q, %v", refreshes.Load(), got.tok, got.err)+ }+}++// The same for a ChatGPT API account (siwcFresh).+func TestChatGPTAPIRefreshWaitsForAnotherMagpie(t *testing.T) {+ claudeHome(t)+ f := newFakeOpenAI(t)+ if st := signInVia(t, f, "oaiapp_9", same); st.State != "done" {+ t.Fatalf("sign-in %+v", st)+ }+ user := "[email protected]"+ set := func(edit func(*siwcCreds)) {+ if err := editSideLogin(ChatGPTAPIID, user, func(ls []savedLogin, i int) ([]savedLogin, error) {+ c, _ := siwcSaved(ls[i])+ edit(&c)+ ls[i].Auth, _ = json.Marshal(c)+ return ls, nil+ }); err != nil {+ t.Fatal(err)+ }+ }+ set(func(c *siwcCreds) { c.Expires = time.Now().Add(time.Minute) })+ f.mu.Lock()+ n := len(f.forms)+ f.mu.Unlock()+ type answer struct {+ c siwcCreds+ err error+ }+ got := anotherMagpieRefreshing(t, loginsPath()+".siwc.lock", func() answer {+ c, err := siwcFresh(context.Background(), user, false)+ return answer{c, err}+ }, func() {+ set(func(c *siwcCreds) {+ c.Access, c.Refresh, c.Expires = "at-theirs", "rt-theirs", time.Now().Add(time.Hour)+ })+ })+ f.mu.Lock()+ sent := len(f.forms) - n+ f.mu.Unlock()+ if got.err != nil || got.c.Access != "at-theirs" || sent != 0 {+ t.Fatalf("refreshed %d times beside another magpie: %+v, %v", sent, got.c, got.err)+ }+}