Files
magpie/internal/provider/account_relist_test.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

148 lines
4.5 KiB
Go

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