From 88b4c00ea11c8653cada22e11edf20ab1594de43 Mon Sep 17 00:00:00 2001 From: yetone Date: Wed, 14 Oct 2026 13:32:27 +0800 Subject: [PATCH] provider: a Codex token refresh, once sent, is waited for even when its caller gives up willz on Discord: a Codex account that isn't the current one stops showing its quota after a day or two and asks to be signed in again, while requests on it still work; moving it to the top "fixes" it. codexRefresh sent the refresh on the caller's context. OpenAI rotates the refresh token as it answers, so a refresh whose caller went away mid-flight (an agent's cancelled request, a usage read's deadline, the keep-alive's 2-minute run) dropped the answer and left magpie holding the spent refresh token. The next refresh (the keep-alive forces one each day) is refused as refresh_token_reused, the account is marked lapsed and its row says to sign in, while the access token it still holds keeps serving requests. Moving it to the top makes it the active account, whose lapse isn't shown. Like siwcFresh (the ChatGPT API's refresh) already does, a refresh that has been sent now runs on context.WithoutCancel with its own 30 s; a caller already gone sends nothing. The cause can't be confirmed without willz's log; this is the one path found where a working account's refresh token is lost. Run: TestCodexRefreshOutlivesItsCaller fails without the change ("context canceled") and passes with it; -race -count=10; the whole internal/provider package (sandbox HOME); gofmt -l; go vet darwin, GOOS=linux -tags nogui, GOOS=windows. Not tried: a real Codex account. --- internal/provider/account.go | 12 ++++ internal/provider/codex_refresh_wait_test.go | 60 ++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 internal/provider/codex_refresh_wait_test.go diff --git a/internal/provider/account.go b/internal/provider/account.go index 59084bf47..8e55fd0f2 100644 --- a/internal/provider/account.go +++ b/internal/provider/account.go @@ -1039,6 +1039,18 @@ func codexRefresh(ctx context.Context, raw map[string]any) (string, error) { toks = map[string]any{} } refresh, _ := toks["refresh_token"].(string) + // Once sent, a refresh is waited for even when whoever asked gives up + // (a request the agent cancelled, a usage read's or the keep-alive's + // deadline): OpenAI rotates the refresh token as it answers, and an + // answer dropped leaves magpie holding the spent one, which the next + // refresh is refused for (refresh_token_reused). The account then + // read as signed out while its access token still served (willz on + // Discord). The ChatGPT API's refresh waits so already (siwcFresh). + if err := ctx.Err(); err != nil { + return "", err + } + ctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 30*time.Second) + defer cancel() body, _ := json.Marshal(map[string]string{"client_id": codexClientID, "grant_type": "refresh_token", "refresh_token": refresh, "scope": "openid profile email"}) req, err := http.NewRequestWithContext(ctx, http.MethodPost, codexTokenURL, bytes.NewReader(body)) diff --git a/internal/provider/codex_refresh_wait_test.go b/internal/provider/codex_refresh_wait_test.go new file mode 100644 index 000000000..2c6c5844a --- /dev/null +++ b/internal/provider/codex_refresh_wait_test.go @@ -0,0 +1,60 @@ +package provider + +import ( + "context" + "encoding/json" + "net/http" + "net/http/httptest" + "strings" + "testing" + "time" +) + +// A Codex refresh OpenAI is answering is waited for though whoever asked +// gave up: the answer holds the rotated refresh token, and dropping it +// leaves the saved account with the spent one, refused on the next +// refresh (willz on Discord: a saved account read as signed out while its +// requests still went through). +func TestCodexRefreshOutlivesItsCaller(t *testing.T) { + home := signIn(t) + rememberLogins(true) // me@ saved… + codexSignIn(t, home, "work@example.com", "r-work") + rememberLogins(true) // …and codex is on work@ now + + arrived, release := make(chan struct{}), make(chan struct{}) + fake := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + var body map[string]string + json.NewDecoder(r.Body).Decode(&body) + close(arrived) + <-release // OpenAI has rotated the token; its answer is on the way + json.NewEncoder(w).Encode(map[string]any{"access_token": fakeJWT(map[string]any{"exp": float64(time.Now().Add(time.Hour).Unix())}), + "refresh_token": body["refresh_token"] + "-2"}) + })) + defer fake.Close() + defer CodexTokenURLForTest(fake.URL)() + + ctx, cancel := context.WithCancel(context.Background()) + done := make(chan error, 1) + go func() { + _, _, err := renewSavedLogin(ctx, "codex", "me@example.com", true) + done <- err + }() + <-arrived + cancel() // the request that asked for it was cancelled + close(release) + if err := <-done; err != nil { + t.Fatalf("refresh: %v", err) + } + for _, l := range readLogins() { + if l.User == "me@example.com" && !strings.Contains(string(l.Auth), `"r-2"`) { + t.Fatalf("the rotated refresh token was dropped: %s", l.Auth) + } + } + + // a caller already gone sends nothing + gone, stop := context.WithCancel(context.Background()) + stop() + if _, _, err := renewSavedLogin(gone, "codex", "me@example.com", true); err == nil { + t.Fatal("a caller already gone refreshed") + } +}