Files
magpie/internal/provider/codex_switch_test.go
T
yetone 1dead5dc7b provider: magpie's own Codex account switches leave the Codex app running unless Restart the app is ticked (atom0964)
atom0964 on Discord (#help) has two ChatGPT Plus accounts in magpie, with
Codex always signed in to the first and Only switch Codex's sign-in both
off, on Smart routing. The Codex desktop app kept restarting by itself.
They tried several combinations of the ticks and it still did.

Why: with both off, SwitchWhenSpent signs Codex in to the next account at
98% and back to the first below 90%. Each switch was noted for the Codex
app (codexAppSpentSwitch), and KeepCodexAppOnItsProvider ended its
app-server once no turn ran; the ChatGPT app shows that as an error and
relaunches (#4550). With Codex kept on the first account there is no
switch, but the used-up takeover (magpie made Codex's provider for a
used-up account and given back once it has room; f37742e14 only limits
it to when another account can serve, which two accounts always can)
changed Codex's route, and that ended it too. So every combination
restarted the app.

What changed:
- Provider.RestartApp (restartApp), off by default, set by
  provider/restartapp (SetRestartApp, Codex only). This computer's own,
  like KeepLogin: Mirror keeps it and davsync's hash leaves it out.
- switchLoginBy(agent, user, byMagpie): SwitchWhenSpent marks its
  switch magpie's own (codexApp.auto). In codexAppStep, a change that is
  magpie's own (its switch, the takeover in or out, the first look at an
  app on a used-up account) ends nothing unless RestartApp is on; it
  logs why and the app follows once restarted. The switch notice's
  Restart, the Agents row's Restart, and uninstall are unchanged.
- Still restarted: a switch the user makes (Switch, the Agents row, the
  CLI), a Connect or Disconnect, failover written or taken out (any
  route change while the takeover state is unchanged), and a user's
  switch pending when magpie's comes.
- GUI: under Codex's accounts, with more than one on (or the tick set),
  "Restart the Codex app when magpie switches its account", with a tip
  and toasts in en, zh, zh-TW, ja and de. app.js paid for it with five
  account-row comments cut; TestCoreScriptBudget is not raised.
- docs/subsystems/agent-wiring.md says which changes restart the app.

Known edge: a takeover flip in the same 3s tick as a Connect is taken
as the Connect's (restarted), since the route changed by the user.

Run at the pushed head (on mote, rebased on b967b1620; then rebased on 2a19043af, a Library change touching none of these files, with the core budget checked again: 160 bytes under):
- gofmt -l clean; GOOS=linux (-tags nogui) and GOOS=windows go vet of
  provider, gui, davsync clean.
- go test -count=1 -timeout 45m ./internal/provider ./internal/davsync
  ./internal/backup: ok. ./internal/gui: ok except
  TestMainWindowCorners/macOS_26_SDK, red alone on origin/main 694f4a6d6
  too (#3949). ./internal/cli: ok except TestEveryRouteHasACommandOrIsNamed
  on GET /api/context/reply (#5026).
- GUI (chromium + webkit, 1100 and 420 wide, en/zh/ja/de):
  restart-app (new), switch-only, claude-switch-only, keep-login,
  codex-accounts-via, account-arrange, i18n-keys: 110/110 and 94/94.
  account-arrange's webkit case went red once in a 5-file run on an
  aborted /api/lanes request; it passes alone on origin/main and at
  this head.

Broken by hand:
- codexAppStep's guard turned off: TestCodexAppLeftRunningForMagpiesOwnChanges
  and TestCodexAppFirstLookLeftRunning go red (ended [60]).
- SwitchWhenSpent marking its switch as the user's: TestCodexSwitchedWhenUsedUp
  goes red ("magpie's own switch is counted the user's").

Not tried: a real ChatGPT desktop app on a real used-up account (the
app-server end is stubbed as before); Windows, where the app is quit and
reopened (CodexAppReopen runs only after an end, so leaving it running
skips it too); no CLI command for the tick (switch-only has none either).
2026-11-03 16:34:30 +08:00

276 lines
9.4 KiB
Go

package provider
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
// Codex signed in to an account out of its allowance is signed in to the
// next account that is on and has room; while none has, it stays.
func TestCodexSwitchedWhenUsedUp(t *testing.T) {
home := signIn(t) // me@example.com, acct-1
rememberLogins(true)
codexSignIn(t, home, "spare@example.com", "r-spare")
rememberLogins(true)
codexSignIn(t, home, "work@example.com", "r-work")
rememberLogins(true)
used := map[string]float64{"acct-work@example.com": 100, "acct-1": 100, "acct-spare@example.com": 20}
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{
"primary_window": map[string]any{"used_percent": used[r.Header.Get("chatgpt-account-id")], "limit_window_seconds": 18000}}})
}))
defer fake.Close()
old := CodexBase
CodexBase = fake.URL + "/backend-api/codex"
t.Cleanup(func() { CodexBase = old })
fresh := func() {
loginUsageCache.Lock()
loginUsageCache.m = nil
loginUsageCache.Unlock()
}
switched := func() string {
t.Helper()
fresh()
to, err := SwitchWhenSpent(context.Background(), "codex")
if err != nil {
t.Fatal(err)
}
return to
}
// no other account on: nothing to go to
if to := switched(); to != "" {
t.Fatalf("switched to %s with none on", to)
}
// the one on is out too
if err := SetLoginOn("codex", "me@example.com", true); err != nil {
t.Fatal(err)
}
if to := switched(); to != "" {
t.Fatalf("switched to %s, out as well", to)
}
// one on with room: the first of those, not one that is off
if err := SetLoginOn("codex", "spare@example.com", true); err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
codexApp.Lock()
codexApp.switched, codexApp.spent = false, false
codexApp.Unlock()
})
// the sign-ins above were the user's switches, as if taken already
codexApp.Lock()
codexApp.switched, codexApp.spent, codexApp.auto = false, false, false
codexApp.Unlock()
if to := switched(); to != "spare@example.com" {
t.Fatalf("switched to %q", to)
}
// the Codex app's app-server is restarted past a turn left stuck on
// the account used up (TestCodexAppServerRestartedPastATurnStuckOnTheSpentAccount)
codexApp.Lock()
spent := codexApp.spent
codexApp.Unlock()
if !spent {
t.Fatal("the switch off a used-up account isn't marked spent for the Codex app")
}
// it is magpie's own: the Codex app is left running unless the user
// asked magpie to restart it (RestartApp; atom0964 on Discord)
codexApp.Lock()
auto := codexApp.auto
codexApp.Unlock()
if !auto {
t.Fatal("magpie's own switch is counted the user's: the Codex app would be restarted for it")
}
if codexRestartsApp() {
t.Fatal("the Codex app is restarted for magpie's own switches with RestartApp never set")
}
if err := SetRestartApp("codex", true); err != nil {
t.Fatal(err)
}
if !codexRestartsApp() {
t.Fatal("RestartApp set, and still not read")
}
if err := SetRestartApp("codex", false); err != nil {
t.Fatal(err)
}
var live codexAuth
readJSON(filepath.Join(home, ".codex", "auth.json"), &live)
if live.Tokens.RefreshToken != "r-spare" {
t.Fatalf("Codex is signed in with %+v", live.Tokens)
}
for _, l := range Logins("codex") {
if l.User == "work@example.com" && (!l.On || l.Active) || l.User == "spare@example.com" && !l.Active {
t.Fatalf("after: %+v", l)
}
}
// on one with room it stays
if to := switched(); to != "" {
t.Fatalf("switched again, to %s", to)
}
// an allowance not known is not taken for used up
used["acct-spare@example.com"] = 100
used["acct-work@example.com"] = 0
CodexBase = "http://127.0.0.1:1/backend-api/codex"
if to := switched(); to != "" {
t.Fatalf("switched to %s, not knowing", to)
}
}
// Claude Code signed in to an account Smart counts spent (98%) is signed
// in to the next account that is on and has room — its credentials and
// .claude.json's account both — and not before (#208, #209). What it
// goes by is what Claude Code told as it answered: Anthropic's usage
// endpoint isn't read for it.
func TestClaudeSwitchedWhenSpent(t *testing.T) {
home := claudeHome(t)
cred := claudeSignIn(t, home, time.Now().Add(time.Hour)) // sk-ant-oat01-old
profile := filepath.Join(home, ".claude.json")
writeFile(t, profile, map[string]any{"oauthAccount": map[string]any{"emailAddress": "a@example.com", "accountUuid": "u-a"}})
rememberLogins(true)
loginsMu.Lock()
ls := upsertLogin(readLogins(), savedLogin{Agent: "claude", User: "b@example.com", Plan: "max", On: true, Seen: time.Now().UTC(),
Auth: mustJSONRaw(t, map[string]any{"claudeAiOauth": map[string]any{"accessToken": "sk-ant-oat01-b", "refreshToken": "sk-ant-ort01-b",
"expiresAt": time.Now().Add(time.Hour).UnixMilli(), "subscriptionType": "max", "scopes": []string{"user:inference", "user:profile"}}}),
Profile: mustJSONRaw(t, map[string]any{"emailAddress": "b@example.com", "accountUuid": "u-b"})})
if err := writeLogins(ls); err != nil {
t.Fatal(err)
}
loginsMu.Unlock()
used := map[string]float64{"a@example.com": 97, "b@example.com": 10}
fake := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
t.Errorf("usage endpoint read: %s", r.URL)
}))
defer fake.Close()
claudeBase = fake.URL // isolate puts it back
switched := func() string {
t.Helper()
loginUsageCache.Lock()
loginUsageCache.m = nil
loginUsageCache.Unlock()
claudeUsage.Lock()
claudeUsage.m = nil
claudeUsage.Unlock()
for user, u := range used {
NoteClaudeLimits(user, []ClaudeLimit{{Kind: "five_hour", Used: u / 100, ResetsAt: time.Now().Add(time.Hour).Unix()}})
}
to, err := SwitchWhenSpent(context.Background(), "claude")
if err != nil {
t.Fatal(err)
}
return to
}
// 97%: Smart still counts it low, not spent
if to := switched(); to != "" {
t.Fatalf("switched to %s at 97%%", to)
}
used["a@example.com"] = 98
if to := switched(); to != "b@example.com" {
t.Fatalf("switched to %q at 98%%", to)
}
var c map[string]any
readJSON(cred, &c)
if o, _ := c["claudeAiOauth"].(map[string]any); o["refreshToken"] != "sk-ant-ort01-b" {
t.Fatalf("Claude Code is signed in with %v", o)
}
b, _ := os.ReadFile(profile)
if !strings.Contains(string(b), "b@example.com") {
t.Fatalf(".claude.json: %s", b)
}
for _, l := range Logins("claude") {
if l.User == "a@example.com" && (!l.On || l.Active) || l.User == "b@example.com" && !l.Active {
t.Fatalf("after: %+v", l)
}
}
// on one with room it stays; with none left, too
if to := switched(); to != "" {
t.Fatalf("switched again, to %s", to)
}
used["b@example.com"] = 99
if to := switched(); to != "" {
t.Fatalf("switched to %s, spent as well", to)
}
}
func mustJSONRaw(t *testing.T, v any) json.RawMessage {
t.Helper()
b, err := json.Marshal(v)
if err != nil {
t.Fatal(err)
}
return b
}
// An account the Codex app holds is spent whatever its windows read: a
// Business seat at its workspace owner's limit reads 40% and isn't
// allowed, and the app says "You're out of Codex usage" and sends nothing
// for it. Codex on it is signed in to the next account with room, and
// never to another the app holds (CavillZhang on X: "有的切换不了").
func TestCodexHeldAccountSwitchedUnderTheShare(t *testing.T) {
home := signIn(t) // me@example.com, acct-1
rememberLogins(true)
codexSignIn(t, home, "spare@example.com", "r-spare")
rememberLogins(true)
codexSignIn(t, home, "work@example.com", "r-work")
rememberLogins(true)
held := map[string]bool{"acct-1": true, "acct-spare@example.com": true, "acct-work@example.com": true}
fake := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
allowed := !held[r.Header.Get("chatgpt-account-id")]
why := map[string]any{"type": "workspace_owner_usage_limit_reached"}
if allowed {
why = nil
}
json.NewEncoder(w).Encode(map[string]any{"plan_type": "business",
"credits": map[string]any{"has_credits": false, "unlimited": false, "overage_limit_reached": false},
"rate_limit_reached_type": why,
"rate_limit": map[string]any{"allowed": allowed, "limit_reached": !allowed,
"primary_window": map[string]any{"used_percent": 40, "limit_window_seconds": 18000}}})
}))
defer fake.Close()
old := CodexBase
CodexBase = fake.URL + "/backend-api/codex"
t.Cleanup(func() { CodexBase = old })
switched := func() string {
t.Helper()
loginUsageCache.Lock()
loginUsageCache.m = nil
loginUsageCache.Unlock()
to, err := SwitchWhenSpent(context.Background(), "codex")
if err != nil {
t.Fatal(err)
}
return to
}
for _, u := range []string{"spare@example.com", "work@example.com"} {
if err := SetLoginOn("codex", u, true); err != nil {
t.Fatal(err)
}
}
// Codex is on work@; every other account held too: nothing to go to
if to := switched(); to != "" {
t.Fatalf("switched to %s, held as well", to)
}
// one with room: that one, though the held one it is on reads 40%
held["acct-spare@example.com"] = false
if to := switched(); to != "spare@example.com" {
t.Fatalf("switched to %q, want spare@example.com", to)
}
// and not back to the one it left while the app still holds it
if to := switched(); to != "" {
t.Fatalf("switched again, to %s", to)
}
// once that has room again, back to it
held["acct-work@example.com"] = false
if to := switched(); to != "work@example.com" {
t.Fatalf("switched to %q, want back to work@example.com", to)
}
}