Files
magpie/internal/provider/codex_app_out_test.go
T
yetone f37742e14d agent: a used-up ChatGPT account magpie can't serve past leaves the Codex app on its sign-in (xiaoq6285)
xiaoq6285 on Discord (magpie 0.1.2798, the Windows Codex app, one
ChatGPT Plus account, Codex on its own model joined beside the sign-in):
at the five-hour limit the app showed an error and had to be restarted,
then its account menu showed only "magpie" with no quota; at the reset
it restarted by itself and the account came back.

Cause. When the active account was held (used up, no credits), Sync's
own-model takeover made magpie Codex's provider on codex/<model>
whenever magpie's catalog listed that model on any Codex account -- the
same used-up one included. With one account and no fallback every turn
it took over is refused on that account anyway, while the takeover:
- has account/read answer no account, so the menu drops the user, Plus
  and the usage meter until the reset;
- changes the route twice (into it, back at the reset), and
  KeepCodexAppOnItsProvider ends the app's app-server each time, which
  ChatGPT desktop 26.1027.x shows as "ChatGPT 遇到了问题" and relaunches
  (#4550, still open, the app's own reaction to an ended app-server).
Separately, magpie's [model_providers.magpie] requires_openai_auth
follows the account out and back (openaiAuth), and codexAppRoute
counted it even when that table was not Codex's model_provider, so
staying beside the sign-in would still have ended the app-server at
the limit and at the reset.

Change.
- provider.CodexServesWhileOut(model): the Codex subscription has
  fallbacks for codex/<model> (FallbackModels), or another ChatGPT
  account is not known held and not lapsed.
- Sync's own-model takeover runs only when that holds. Otherwise Codex
  stays on its sign-in: the app keeps the account, its quota and reset,
  and shows its own limit message. With a fallback set it moves as
  before (affa979d5 serves the fallback through the gateway too).
- codexAppRoute counts magpie.requires_openai_auth only while
  model_provider is magpie; the table is still written as before.
- docs/subsystems/agent-wiring.md says both.

Tests.
- TestCodexOwnModelOneSpentAccountStaysOnItsSignIn (new): one account,
  no fallback, held: config stays on the sign-in, codexAppRoute and
  AppOut unchanged across two Syncs and the reset.
- TestCodexServesWhileOut (new): one account none; another not known
  out yes; every account out none; a codex fallback yes.
- TestCodexOwnModelGoesThroughMagpieWhileUsedUp and
  TestCodexOwnModelOutThenKeptOnChatGPT now set a codex fallback, the
  case where the takeover still serves something.

Broken by hand: the CodexServesWhileOut gate taken out -> the new agent
test red (Codex moved to model_provider = "magpie", codex/gpt-a);
codexAppRoute counting requires_openai_auth always -> red (route
changed with model_provider empty). Each put back, green.

Run on mote at this head (rebased on e41ef6288), -tags nogui -timeout 45m:
- internal/agent ok (381s), internal/provider ok (254s);
- internal/cli FAIL in TestEveryRouteHasACommandOrIsNamed alone: GET
  /api/context/reply has no command, from 0872bc2a0, filed as #5026,
  nothing of this change;
- gofmt -l clean; go vet of both packages, GOOS=linux go vet -tags
  nogui ./... and GOOS=windows go vet ./... ok.
- internal/gateway ok (1152s) at the earlier base 3088872e6, not run
  again after the rebase (it imports provider; this change adds a
  function there and changes nothing it calls).

Not tried: the real Windows Codex app signed in to a used-up Plus
account (the Windows box's app is not signed in; #4550's crash was not
reproduced there); a real codex app-server against a fake 429 upstream.
Users with a fallback or a second account still get the takeover and
its two app-server restarts, which #4550 tracks.
2026-11-03 15:46:42 +08:00

159 lines
5.2 KiB
Go

package provider
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
// A reply that says the account Codex is signed in to is out has its
// allowance read again at once, not at the next look five minutes on
// (MasterYi on Discord: the Codex app greyed Send meanwhile): once per
// codexRecheckEvery, for the active account alone, and only in a magpie
// that keeps Codex on an account with room.
func TestCodexMayBeUsedUpReadsAtOnce(t *testing.T) {
signIn(t) // me@example.com, active
rememberLogins(true)
reads := make(chan string, 8)
oldRead, oldEvery := codexReadNow, codexRecheckEvery
codexReadNow = func(user string) { reads <- user }
t.Cleanup(func() {
codexReadNow, codexRecheckEvery = oldRead, oldEvery
codexLiveChecks.Store(0)
codexRecheck.Lock()
codexRecheck.at = time.Time{}
codexRecheck.Unlock()
})
none := func(when string) {
t.Helper()
select {
case u := <-reads:
t.Fatalf("%s: read %q", when, u)
case <-time.After(50 * time.Millisecond):
}
}
read := func(when string) {
t.Helper()
select {
case u := <-reads:
if u != "me@example.com" {
t.Fatalf("%s: read %q", when, u)
}
case <-time.After(10 * time.Second):
t.Fatalf("%s: not read", when)
}
}
full := []CodexLimit{{Used: 100, Span: 5 * time.Hour}}
NoteCodexLimits("codex", "me@example.com", full)
none("live checks off")
codexLiveChecks.Store(1)
NoteCodexLimits("codex", "me@example.com", []CodexLimit{{Used: 99, Span: 5 * time.Hour}})
none("a window with room")
NoteCodexLimits("codex", "other@example.com", full)
none("an account Codex isn't on")
NoteCodexLimits("codex", "me@example.com", full)
read("a window full")
CodexMayBeUsedUp("me@example.com")
none("again at once")
codexRecheckEvery = 0
CodexMayBeUsedUp("ME@example.com")
read("usage_limit_reached")
}
// The reading a reply's word asks for signs Codex in to the next account
// with room as soon as the one it is on is out, not at the loop's next
// look up to five minutes on: the Codex app greys Send once it is out, so
// no request comes to tell magpie again (danzi002 on Discord restarted the
// app every time instead).
func TestCodexReadNowSwitchesAtOnce(t *testing.T) {
home := signIn(t) // me@example.com, acct-1
rememberLogins(true)
codexSignIn(t, home, "new@example.com", "r-new") // signed in to now
rememberLogins(true)
used := map[string]float64{"acct-new@example.com": 100, "acct-1": 20}
reset := time.Now().Add(3 * time.Hour).Unix()
fake := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(map[string]any{"plan_type": "plus", "rate_limit": map[string]any{
"allowed": used[r.Header.Get("chatgpt-account-id")] < 100,
"primary_window": map[string]any{"used_percent": used[r.Header.Get("chatgpt-account-id")], "limit_window_seconds": 18000, "reset_at": reset}}})
}))
defer fake.Close()
old := CodexBase
CodexBase = fake.URL + "/backend-api/codex"
t.Cleanup(func() { CodexBase = old })
if err := SetLoginOn("codex", "me@example.com", true); err != nil {
t.Fatal(err)
}
t.Cleanup(func() { codexWasUsedUp.Store(false) })
codexReadNow("new@example.com")
for _, l := range Logins("codex") {
if l.Active && !strings.EqualFold(l.User, "me@example.com") {
t.Fatalf("out, with me@example.com at 20%%: Codex left on %s", l.User)
}
}
}
// Whether magpie has anything to serve Codex's own model on while the
// account Codex is signed in to is out (xiaoq6285 on Discord, one Plus
// account): with that account alone and no fallback, nothing; another
// ChatGPT account not known out, or a fallback on the Codex
// subscription, is something. Another account known out is not.
func TestCodexServesWhileOut(t *testing.T) {
home := signIn(t) // me@example.com
rememberLogins(true)
if CodexServesWhileOut("gpt-5.5") {
t.Fatal("one account, no fallback: serves")
}
if CodexServesWhileOut("") {
t.Fatal("no model: serves")
}
codexSignIn(t, home, "spare@example.com", "r-spare")
rememberLogins(true)
var active, other string
for _, l := range Logins("codex") {
if l.Active {
active = l.User
} else {
other = l.User
}
}
if other == "" {
t.Fatalf("no second account: %+v", Logins("codex"))
}
loginUsageCache.Lock()
oldCache := loginUsageCache.m
loginUsageCache.m = map[string]loginUsageEntry{
"codex/" + active: {at: time.Now(), q: SubscriptionQuota{Provider: "codex", User: active, Held: true}},
}
loginUsageCache.Unlock()
t.Cleanup(func() {
loginUsageCache.Lock()
loginUsageCache.m = oldCache
loginUsageCache.Unlock()
})
if !CodexServesWhileOut("gpt-5.5") {
t.Fatalf("another account (%s), not known out: doesn't serve", other)
}
loginUsageCache.Lock()
loginUsageCache.m["codex/"+other] = loginUsageEntry{at: time.Now(), q: SubscriptionQuota{Provider: "codex", User: other, Held: true}}
loginUsageCache.Unlock()
if CodexServesWhileOut("gpt-5.5") {
t.Fatal("every account out, no fallback: serves")
}
if err := Save(Provider{ID: "fb", Name: "FB", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"m"}}); err != nil {
t.Fatal(err)
}
if err := Save(Provider{ID: "codex", Fallback: []string{"fb/m"}}); err != nil {
t.Fatal(err)
}
if !CodexServesWhileOut("gpt-5.5") {
t.Fatal("a fallback on the Codex subscription: doesn't serve")
}
}