Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.
provider: a signed-in account's model list is fetched again each hour, so a model the vendor adds (GPT-6.1 Terra) is served without Refresh (cliff_26651 on Discord)
yetone2badacaad8d8
diff --git a/docs/subsystems/providers-accounts.md b/docs/subsystems/providers-accounts.mdindex 4d80bd13b..13ee309f8 100644--- a/docs/subsystems/providers-accounts.md+++ b/docs/subsystems/providers-accounts.md@@ -134,7 +134,7 @@ no decision model was discovered (`Test` inAccount discovery can be turned off (#1535): Settings → Privacy → *Subscription accounts*, `magpie accounts discovery off`, or `MAGPIE_NO_ACCOUNTS=1` for one process. The first two save `noAccountDiscovery: true`, which this computer keeps through a sync or a restored backup (`KeepOwn`); the variable only ever turns discovery off, never on what the user saved off (`settings.AccountDiscoveryOff`). It is on by default. In the GUI, Off asks first and names the subscriptions in use that it stops (`discoveryOffWarning` in `app.js`, #1761: a user turned it off without meaning to and was left with no subscription working). While it is off, magpie reads no agent's sign-in: no credential file (`~/.claude/.credentials.json`, `~/.claude.json`'s account, `~/.codex/auth.json` or Codex's keychain item, Copilot's, Cursor's, Devin's, Kiro's, Gemini's, ZCode's, Command Code's, OpenCode's `auth.json`), no Keychain or Secret Service item (`security`, `secret-tool`), and no agent CLI asked who is signed in. The gates sit at each reader (`readAccountFile`, `discoveryOff` in [`discovery.go`](../../internal/provider/discovery.go)) and at each route that would read or write a sign-in: `Accounts` and `All`, `Logins`, usage, renewal, warm-up, session identities, check-ins, switching, forgetting, `SetLoginOn`, `StartSignIn`, the imports, and moves onto plugins (`MoveCandidates`, `HandOver`, `KeepRetiringMoved`). Those that write refuse with `errAccountDiscoveryOff`, which says how to turn it back on. The agents' side is gated too: the Agents page's *runs on* line (`Agent.Source`), OpenCode's own providers (`readOwnAuth`), and wiring Codex, which goes by magpie's stash instead of `auth.json` (`chatGPT` in [`codex.go`](../../internal/agent/codex.go)). Subscriptions aren't served; API-key providers, middleware plugins and Pi extensions run as before. What magpie kept of its own (`logins.json`, `plugin-auth.json`, `migrations.json`) is left as it is, unread for accounts, and serves again once discovery is on. A change takes effect at once (`ForgetAccounts`); no restart is needed.-A signed-in account's model list exists only at its vendor (the ChatGPT backend's `/models` for a Codex sign-in, …), so `FetchNew` asks for the lists not fetched yet. Every front end calls it at start: the window (`startBackend`, then `FetchNewSoon` behind `/api/state`), the TUI (`fetchModelsCmd` in `Init`) and `magpie serve` (#1789, reyoung: the terminal and a headless gateway had offered the built-in Cursor's fallback `auto` alone until the list was fetched by hand). Listing the accounts asks a CLI who is signed in, and a look waits `firstAsk` (3s) for that; `cursor-agent about` asks Cursor's servers and can take longer, so `FetchNew` waits for each CLI asked for the first time (`awaitIdentities` in [`cli_identity.go`](../../internal/provider/cli_identity.go)) and lists the accounts again. It reports whether an account was left without its list (failed, or waiting `newFetchRetry` after a failure): the TUI then asks again a minute later (`modelsRetry`) and `serve` loops the same way, while the window asks again from its pages. A TUI model picker open when the list arrives is filled in place (`relist`), keeping its filter and the model under the cursor. `TestFetchNewAccount` and the TUI's `TestTUIFetchesSubscriptionModels` / `TestTUIAsksAgainForAMissingList` cover it.+A signed-in account's model list exists only at its vendor (the ChatGPT backend's `/models` for a Codex sign-in, …), so `FetchNew` asks for the lists not fetched yet. Every front end calls it at start: the window (`startBackend`, then `FetchNewSoon` behind `/api/state`), the TUI (`fetchModelsCmd` in `Init`) and `magpie serve` (#1789, reyoung: the terminal and a headless gateway had offered the built-in Cursor's fallback `auto` alone until the list was fetched by hand). Listing the accounts asks a CLI who is signed in, and a look waits `firstAsk` (3s) for that; `cursor-agent about` asks Cursor's servers and can take longer, so `FetchNew` waits for each CLI asked for the first time (`awaitIdentities` in [`cli_identity.go`](../../internal/provider/cli_identity.go)) and lists the accounts again. It reports whether an account was left without its list (failed, or waiting `newFetchRetry` after a failure): the TUI then asks again a minute later (`modelsRetry`) and `serve` loops the same way, while the window asks again from its pages. A TUI model picker open when the list arrives is filled in place (`relist`), keeping its filter and the model under the cursor. `TestFetchNewAccount` and the TUI's `TestTUIFetchesSubscriptionModels` / `TestTUIAsksAgainForAMissingList` cover it. A list fetched once is not left as it was: the magpie serving the gateway looks every hour ([`KeepAccountsListed`](../../internal/provider/account_relist.go), first two minutes after it starts) and fetches again each signed-in built-in account's list that is 45 minutes old (`accountListAge`, `relistAccounts`), so a model the vendor adds to the plan is served without Refresh or a magpie release (cliff_26651 on Discord: GPT-6.1 Terra, served to ChatGPT accounts from 10-26, missing from Codex through a magpie whose Codex list was fetched on 10-12). A plugin's accounts are `KeepPluginsListed`'s, one switched off isn't asked, and a fetch that fails keeps the list there was. `TestAccountListFollowsTheVendor` and `TestKeepAccountsListedAsks` cover it.## Runtime pathdiff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.goindex 947de8517..71ea0e615 100644--- a/internal/gateway/gateway.go+++ b/internal/gateway/gateway.go@@ -648,6 +648,8 @@ func (s *Server) ListenAndServe(ctx context.Context) error {// and fetches the partner list every hour, asked for or notgo provider.KeepPartnersFresh(ctx)go provider.KeepPluginsListed(ctx) // models a plugin's vendor drops or adds, while magpie runs+ // and those a signed-in account's vendor adds (a ChatGPT plan's new GPT)+ go provider.KeepAccountsListed(ctx)// and fetches the lists of the providers the user asked to keep fresh (#2135)go provider.KeepListsFresh(ctx)// and keeps the community's plugins up to date, noting others' updates, and the Bun they run ondiff --git a/internal/provider/account_relist.go b/internal/provider/account_relist.gonew file mode 100644index 000000000..8c9855850--- /dev/null+++ b/internal/provider/account_relist.go@@ -0,0 +1,73 @@+package provider++// A signed-in account's model list is fetched once by FetchNew, when magpie+// has none for it yet, and after that only when Refresh is pressed. A model+// the vendor adds to the plan later never reached a magpie left running, nor+// one restarted, as the list it had was not new: OpenAI served GPT-6.1 Terra+// to ChatGPT accounts on 10-26, and Codex routed through magpie still listed+// what the ChatGPT backend had on 10-12 (cliff_26651 on Discord). The magpie+// serving the gateway now asks each account's vendor again once the list it+// has is accountListAge old, as KeepPluginsListed does for a plugin's.++import (+ "context"+ "log"+ "time"+)++// accountRelistEvery is how often the gateway's magpie looks for an+// account's list that is due; accountRelistFirst is how long after it+// starts it first does (FetchNew lists the new accounts meanwhile);+// accountListAge is how old a list is before it is asked for again.+var (+ accountRelistEvery = time.Hour+ accountRelistFirst = 2 * time.Minute+ accountListAge = 45 * time.Minute+)++// KeepAccountsListed fetches each signed-in account's list again once it+// is accountListAge old, in the magpie serving the gateway.+func KeepAccountsListed(ctx context.Context) {+ t := time.NewTimer(accountRelistFirst)+ defer t.Stop()+ for {+ select {+ case <-ctx.Done():+ return+ case <-t.C:+ }+ relistAccounts(ctx, time.Now())+ t.Reset(accountRelistEvery)+ }+}++// relistAccounts fetches the list of each account whose list is older than+// accountListAge at now, and answers the ids it asked. An account with no+// list yet is FetchNew's; a plugin's is listed by KeepPluginsListed; one+// switched off isn't asked. A fetch that fails keeps the list there was+// (the fetch saves only what it got), and is tried at the next look.+func relistAccounts(ctx context.Context, now time.Time) []string {+ var asked []string+ for _, p := range All() {+ if p.Off || p.Account == nil || p.Account.fetch == nil || p.IsPlugin() || !p.Ready() {+ continue+ }+ at, ok := p.Fetched()+ if !ok || now.Sub(at) < accountListAge {+ continue+ }+ if ctx.Err() != nil {+ return asked+ }+ asked = append(asked, p.ID)+ fctx, cancel := context.WithTimeout(ctx, time.Minute)+ ms, err := p.Fetch(fctx)+ cancel()+ if err != nil {+ log.Printf("%s: fetching its list again failed: %s (the last list stays)", p.ID, err)+ continue+ }+ log.Printf("%s: fetched its list again, %d models", p.ID, len(ms))+ }+ return asked+}diff --git a/internal/provider/account_relist_test.go b/internal/provider/account_relist_test.gonew file mode 100644index 000000000..b9de6e78f--- /dev/null+++ b/internal/provider/account_relist_test.go@@ -0,0 +1,147 @@+package provider++import (+ "context"+ "net/http"+ "net/http/httptest"+ "path/filepath"+ "slices"+ "sync/atomic"+ "testing"+ "time"+)++// A model the vendor adds to a signed-in account's plan reaches magpie+// without Refresh: the gateway's magpie asks for the account's list again+// once it is accountListAge old (cliff_26651 on Discord: GPT-6.1 Terra,+// served to ChatGPT accounts on 10-26, missing from Codex through magpie,+// whose list of the account was fetched on 10-12). A list still fresh+// isn't asked for, and one that can't be had keeps the last.+func TestAccountListFollowsTheVendor(t *testing.T) {+ isolate(t)+ home := t.TempDir()+ t.Setenv("HOME", home)+ t.Setenv("USERPROFILE", home)+ t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))+ t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))+ newFetches.Lock()+ newFetches.m = map[string]time.Time{}+ newFetches.Unlock()++ var asked atomic.Int32+ var added, down atomic.Bool+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ if r.URL.Path != "/backend-api/codex/models" {+ w.WriteHeader(404)+ return+ }+ asked.Add(1)+ switch {+ case down.Load():+ w.WriteHeader(502)+ case added.Load():+ w.Write(codexListOf("gpt-6.1-sol", "gpt-6-astra", "gpt-6.1-terra", "gpt-6-luna"))+ default:+ w.Write(codexListOf("gpt-6.1-sol", "gpt-6-astra", "gpt-6-luna"))+ }+ }))+ defer srv.Close()+ codexListedAt(t, srv.URL)+ codexSignedIn(t, home)++ served := func() []string {+ p, err := Find("codex")+ if err != nil {+ t.Fatal(err)+ }+ var out []string+ for _, m := range p.Available() {+ out = append(out, m.ID)+ }+ return out+ }+ FetchNew(5 * time.Second)+ if n := asked.Load(); n != 1 || !slices.Contains(served(), "gpt-6-astra") {+ t.Fatalf("FetchNew: asked %d, serves %v", n, served())+ }++ // OpenAI adds a model; the list magpie has is a minute old+ added.Store(true)+ ctx := context.Background()+ if got := relistAccounts(ctx, time.Now()); len(got) != 0 || asked.Load() != 1 {+ t.Fatalf("a fresh list asked for again: %v (%d asks)", got, asked.Load())+ }+ later := time.Now().Add(accountListAge + time.Minute)+ if got := relistAccounts(ctx, later); !slices.Equal(got, []string{"codex"}) {+ t.Fatalf("a list %s old not asked for again: %v", accountListAge+time.Minute, got)+ }+ if !slices.Contains(served(), "gpt-6.1-terra") {+ t.Fatalf("the model the vendor added isn't served: %v", served())+ }++ // the vendor can't be reached: the list there was stays+ down.Store(true)+ n := asked.Load()+ relistAccounts(ctx, later.Add(accountListAge+time.Minute))+ if asked.Load() != n+1 {+ t.Fatal("the vendor wasn't asked")+ }+ if !slices.Contains(served(), "gpt-6.1-terra") || !slices.Contains(served(), "gpt-6-astra") {+ t.Fatalf("a failed fetch lost the list: %v", served())+ }++ // switched off, it isn't asked+ down.Store(false)+ if err := SetOff("codex", true); err != nil {+ t.Fatal(err)+ }+ n = asked.Load()+ if got := relistAccounts(ctx, later.Add(3*accountListAge)); len(got) != 0 || asked.Load() != n {+ t.Fatalf("an account switched off asked: %v", got)+ }+}++// The gateway's loop does it by itself, and stops with its context.+func TestKeepAccountsListedAsks(t *testing.T) {+ isolate(t)+ home := t.TempDir()+ t.Setenv("HOME", home)+ t.Setenv("USERPROFILE", home)+ t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))+ t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))+ newFetches.Lock()+ newFetches.m = map[string]time.Time{}+ newFetches.Unlock()++ var asked atomic.Int32+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ asked.Add(1)+ w.Write(codexListOf("gpt-6.1-sol", "gpt-6.1-terra"))+ }))+ defer srv.Close()+ codexListedAt(t, srv.URL)+ codexSignedIn(t, home)+ FetchNew(5 * time.Second)+ if asked.Load() != 1 {+ t.Fatalf("FetchNew asked %d times", asked.Load())+ }++ oldFirst, oldEvery, oldAge := accountRelistFirst, accountRelistEvery, accountListAge+ accountRelistFirst, accountRelistEvery, accountListAge = time.Millisecond, time.Millisecond, 0+ defer func() { accountRelistFirst, accountRelistEvery, accountListAge = oldFirst, oldEvery, oldAge }()+ ctx, cancel := context.WithCancel(context.Background())+ done := make(chan struct{})+ go func() {+ defer close(done)+ KeepAccountsListed(ctx)+ }()+ deadline := time.Now().Add(30 * time.Second)+ for asked.Load() < 3 && time.Now().Before(deadline) {+ <-time.After(5 * time.Millisecond)+ }+ cancel()+ <-done+ if n := asked.Load(); n < 3 {+ t.Fatalf("the loop asked the vendor %d times in 30s", n-1)+ }+}diff --git a/internal/provider/autolist.go b/internal/provider/autolist.goindex 2a070f68b..c01eaf25d 100644--- a/internal/provider/autolist.go+++ b/internal/provider/autolist.go@@ -18,8 +18,8 @@ package provider//// Both are the user's own settings, kept by every save that doesn't set// them (the window's editor among them), and only a provider of the user's-// own has them: an account's list is fetched for it (FetchNew), and a-// plugin's by the plugin (KeepPluginsListed).+// own has them: an account's list is fetched for it (FetchNew, then+// KeepAccountsListed), and a plugin's by the plugin (KeepPluginsListed).import ("context"