Files
magpie/internal/provider/account_relist.go
T
yetone 2badacaad8 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)
cliff_26651 (Discord #general, 10-27): with magpie routing on, Codex
didn't list gpt-6.1-terra. OpenAI added it on 10-26 (openai/codex
126a0ee92 "Add GPT-6.1 Terra to model catalogs (#57293)", in
rust-v0.166.0-alpha.1; models.json: minimal_client_version 0.153.0,
context 500000 / max 872000, low..ultra). The ChatGPT backend's /models
serves it to a Pro account at client_version 0.159.0 through 0.166.0
alike (checked read-only), so magpie's version floor isn't it.

Cause: FetchNew fetches an account's list only when magpie has none, and
nothing fetched it again but Refresh. The owner's own ~/.cache/magpie
models/codex.json was fetched 2026-10-12 and still listed 7 models. Every
built-in account (Codex, ChatGPT API, Copilot, Gemini CLI, Antigravity,
Claude's static one) had the same gap; plugins already relist hourly
(KeepPluginsListed) and own providers by RefreshEvery.

KeepAccountsListed (account_relist.go), started by the gateway beside
KeepPluginsListed, looks 2 minutes after start and then hourly, and
fetches again each ready, not-switched-off built-in account's list that
is 45 minutes old, through the same Fetch as Refresh. A plugin's account
and one with no list yet (FetchNew's) are left alone. A failed fetch
keeps the list there was (each fetch saves only what it got). No static
catalog entry is needed: the window, efforts and image input come from
the backend's own list.

Run:
- TestAccountListFollowsTheVendor, TestKeepAccountsListedAsks: ok, also
  -race -count=5 and TZ=UTC.
- Broken by hand: relistAccounts returning nil (the old behaviour) makes
  both fail: "a list 46m0s old not asked for again: []" and "the loop
  asked the vendor 0 times in 30s".
- Real: magpie serve (this tree) in a sandbox HOME with a copy of the
  owner's ChatGPT Pro auth.json and the 10-12 codex.json: start kept 7
  models; at +2m "codex: fetched its list again, 8 models", codex.json
  now has gpt-6.1-terra (500000/872000) and /v1/models lists
  codex/gpt-6.1-terra. The copied auth.json was unchanged; sandbox removed.
- go test -tags nogui ./internal/provider/: ok with
  TestUnreadLoginsWriteErrorsKeepCredentials skipped; that test crashes
  the package alone on origin/main too (synctest bubble in
  filememo.Forget), #3632, open with patches.
- go test -tags nogui ./internal/gateway/: ok (588s).
- gofmt -l clean; go vet darwin, GOOS=linux -tags nogui, GOOS=windows on
  internal/provider and internal/gateway: ok.

Not tried: the full ./... suite (other sessions' suites were running,
load ~50); Copilot, Gemini and Antigravity accounts relisted for real
(same Fetch as Refresh, unchanged).
2026-10-27 10:02:22 +08:00

74 lines
2.4 KiB
Go

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
}