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") + } +}