diff --git a/internal/gateway/codex_desktop.go b/internal/gateway/codex_desktop.go index ed5fa9f13..69eeb60c8 100644 --- a/internal/gateway/codex_desktop.go +++ b/internal/gateway/codex_desktop.go @@ -29,6 +29,10 @@ var CodexDesktopChanged func() // codexDeskFetch asks the backend for its list (a test's stands in). var codexDeskFetch = provider.CodexLiveList +// codexDeskWho names the sign-in Codex is on now, whose list the backend +// gives (a test's stands in). +var codexDeskWho = provider.CodexSignInWho + const ( // codexDeskFor is how long a list the backend gave stands before it // is asked for again; codexDeskRetry how long after one it didn't @@ -45,7 +49,10 @@ var codexDesk struct { // revoked: the last ask had the backend call the sign-in revoked // (provider.CodexListRevoked) revoked bool - done chan struct{} // closed as the ask under way ends + // who is the sign-in body and revoked are of (codexDeskWho); asked + // the one the last ask was for; want the one a caller last asked about + who, asked, want string + done chan struct{} // closed as the ask under way ends } // CodexDesktopCatalog is the list a Codex beside its ChatGPT sign-in is @@ -62,14 +69,27 @@ var codexDesk struct { // codexModels answers Codex then (#4308): the backend has said all it // will for that sign-in, and without it the app's picker had none of // magpie's models until the user signed in again. +// +// The list is the list of the account Codex is signed in to, and a Pro +// account lists models a Plus one doesn't. Once Codex is on another +// account, the list from before is not known for it: the backend is asked +// at once, and the file stands as it was until it answers (chzbz on X: a +// 20x account added and switched to, GPT-6.1 Sol not in the app's picker, +// the file kept the other account's list up to an hour). func CodexDesktopCatalog() (b []byte, ok bool) { + who := codexDeskWho() codexDesk.Lock() + codexDesk.want = who body, revoked := codexDesk.body, codexDesk.revoked + mine := codexDesk.who == who + if !mine { + body, revoked = nil, false + } stale := body == nil || time.Since(codexDesk.got) >= codexDeskFor - if stale && !codexDesk.asking && time.Since(codexDesk.tried) >= codexDeskRetry { - codexDesk.asking, codexDesk.tried = true, time.Now() + if stale && !codexDesk.asking && (codexDesk.asked != who || time.Since(codexDesk.tried) >= codexDeskRetry) { + codexDesk.asking, codexDesk.tried, codexDesk.asked = true, time.Now(), who codexDesk.done = make(chan struct{}) - go codexDeskRefresh(codexDesk.done) + go codexDeskRefresh(codexDesk.done, who) } codexDesk.Unlock() if body == nil && revoked { @@ -97,12 +117,17 @@ func CodexDesktopCatalog() (b []byte, ok bool) { return b, true } -func codexDeskRefresh(done chan struct{}) { +// codexDeskRefresh asks the backend for who's list. +func codexDeskRefresh(done chan struct{}, who string) { ctx, cancel := context.WithTimeout(context.Background(), codexDeskWait) defer cancel() b, err := codexDeskFetch(ctx) codexDesk.Lock() changed := false + if codexDesk.who != who { + // another sign-in's list, and what was said of it, is not who's + codexDesk.body, codexDesk.revoked, codexDesk.who = nil, false, who + } if err == nil { changed = !bytes.Equal(b, codexDesk.body) || codexDesk.revoked codexDesk.body, codexDesk.got, codexDesk.revoked = b, time.Now(), false @@ -112,6 +137,11 @@ func codexDeskRefresh(done chan struct{}) { changed = codexDesk.body == nil codexDesk.revoked = revoked } + // Codex went on to another sign-in while this one was asked about: + // told, for the next look to ask about that one + if codexDesk.want != who { + changed = true + } codexDesk.asking = false codexDesk.Unlock() if err != nil { diff --git a/internal/gateway/codex_desktop_test.go b/internal/gateway/codex_desktop_test.go index 5189c5e96..3c04585bb 100644 --- a/internal/gateway/codex_desktop_test.go +++ b/internal/gateway/codex_desktop_test.go @@ -1,10 +1,14 @@ package gateway import ( + "encoding/base64" "encoding/json" "io" "net/http" "net/http/httptest" + "os" + "path/filepath" + "strings" "sync/atomic" "testing" "time" @@ -25,6 +29,7 @@ func codexDeskReset(t *testing.T) { } codexDesk.Lock() codexDesk.body, codexDesk.got, codexDesk.tried, codexDesk.asking, codexDesk.done, codexDesk.revoked = nil, time.Time{}, time.Time{}, false, nil, false + codexDesk.who, codexDesk.asked, codexDesk.want = "", "", "" codexDesk.Unlock() } reset() @@ -122,3 +127,76 @@ func TestCodexDesktopCatalog(t *testing.T) { t.Fatalf("a failed ask told as a change") } } + +// The list is the list of the account Codex is signed in to. Switched to +// another account (chzbz on X: a 20x one, GPT-6.1 Sol not in the Codex +// app's picker), the list from before is not handed out as this one's: +// the backend is asked at once, not an hour on, and the file then has +// the new account's models. +func TestCodexDesktopCatalogFollowsTheSignIn(t *testing.T) { + codexSignedIn(t) + codexDeskReset(t) + up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + // the access token's claims say whose sign-in asked + var tok []byte + if parts := strings.Split(strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer "), "."); len(parts) == 3 { + tok, _ = base64.RawURLEncoding.DecodeString(parts[1]) + } + if strings.Contains(string(tok), `"acct-pro"`) { + io.WriteString(w, `{"models":[{"slug":"gpt-6.1-sol","priority":1,"visibility":"list"},{"slug":"gpt-5.5","priority":2,"visibility":"list"}]}`) + return + } + io.WriteString(w, `{"models":[{"slug":"gpt-5.5","priority":1,"visibility":"list"}]}`) + })) + defer up.Close() + was := provider.CodexBase + provider.CodexBase = up.URL + "/backend-api/codex" + defer func() { provider.CodexBase = was }() + var changed atomic.Int32 + CodexDesktopChanged = func() { changed.Add(1) } + has := func(b []byte, slug string) bool { + var got struct { + Models []map[string]any `json:"models"` + } + json.Unmarshal(b, &got) + for _, m := range got.Models { + if m["slug"] == slug { + return true + } + } + return false + } + + CodexDesktopCatalog() + codexDeskAsked(t) + b, ok := CodexDesktopCatalog() + if !ok || has(b, "gpt-6.1-sol") { + t.Fatalf("the Plus account's list: %v\n%s", ok, b) + } + + // Codex signed in to the Pro account now, as magpie's switch writes it + claims := func(m map[string]any) string { + b, _ := json.Marshal(m) + return "h." + base64.RawURLEncoding.EncodeToString(b) + ".s" + } + pro := map[string]any{"auth_mode": "chatgpt", "tokens": map[string]any{ + "id_token": claims(map[string]any{"email": "pro@example.com", + "https://api.openai.com/auth": map[string]any{"chatgpt_plan_type": "pro"}}), + "access_token": claims(map[string]any{"exp": time.Now().Add(time.Hour).Unix(), "who": "acct-pro"}), + "refresh_token": "r-pro", "account_id": "acct-pro"}} + home, _ := os.UserHomeDir() + if err := os.WriteFile(filepath.Join(home, ".codex", "auth.json"), mustJSON(pro), 0o600); err != nil { + t.Fatal(err) + } + told := changed.Load() + if b, ok := CodexDesktopCatalog(); ok { + t.Fatalf("the other account's list handed out as the Pro account's:\n%s", b) + } + codexDeskAsked(t) + if changed.Load() == told { + t.Fatal("the Pro account's list not told as a change") + } + if b, ok := CodexDesktopCatalog(); !ok || !has(b, "gpt-6.1-sol") { + t.Fatalf("the Pro account's list: %v\n%s", ok, b) + } +} diff --git a/internal/provider/logins.go b/internal/provider/logins.go index ace60bf49..23255612f 100644 --- a/internal/provider/logins.go +++ b/internal/provider/logins.go @@ -732,6 +732,18 @@ func CodexSignInPlan() string { func init() { codexcat.SignInPlan = CodexSignInPlan } +// CodexSignInWho names the ChatGPT account this machine's Codex is signed +// in to now and its plan, "" when it isn't: what the backend's model list +// (CodexLiveList) is the list of. A Pro account lists models a Plus one +// doesn't (chzbz on X: GPT-6.1 Sol). +func CodexSignInWho() string { + l, ok := liveLogin("codex") + if !ok { + return "" + } + return l.User + "\x00" + l.Plan +} + // codexLoginIn is the ChatGPT account Codex is signed in to in its folder // home, as Codex keeps it there (auth.json, or the keychain item named by // the folder).