Files
magpie/internal/agent/codex_usedup_test.go
T
yetone b3a57b3d8f codex: turning the takeover off restarts the Codex app, so its ChatGPT projects come back
Herzz____ on X, the Windows Codex app: with Codex through magpie the
sidebar's project list is empty; they use magpie only when their
subscription runs out, and want to switch between magpie and the native
sign-in with the projects whole each way.

What happens. When the ChatGPT account is used up, magpie becomes
Codex's provider (codex.out) without requires_openai_auth: on that
account the app's composer gate greys Send for every model, magpie's
too (#540). Checked again in ChatGPT.app 26.1029's bundle: the gate
picks the account's own snapshot (limitName null) whenever it is
blocked, before any per-model additional_rate_limits, so keeping the
account during the takeover would still grey Send. The app-server then
answers account/read with no account and the app hides its ChatGPT
projects and chats (72ca3d593). magpie never touches projects,
project_roots or a thread's cwd: its thread moves only rewrite
threads.model_provider (sessions/codex_provider.go), so path case or
backslashes on Windows play no part.

What was wrong. Turning the takeover off by hand (the takeover note's
Keep on ChatGPT, the sign-in's Always ChatGPT, or Disconnect) looked
the same to codexAppStep as the reset, magpie's own end of it: with
Restart the Codex app to follow off (the default) the app's app-server
was left running on no account, so the projects stayed gone after the
user switched back to have them, until they restarted the app (the
Windows app has no Restart of its own).

Now Agent.AppOutOff (Always ChatGPT, or Codex not routed through
magpie) reaches the step through provider.CodexTakeoverOff, asked only
as out goes false, and a takeover ended so counts as the user's change:
the app-server is restarted once no Codex turn runs, as for a Connect,
and the Windows app reopened if none comes back (existing path). The
reset itself still leaves the app running and says so (CodexAppBack);
that note now names the ChatGPT projects and chats it lacks (en, zh,
zh-TW, ja, de). agent-wiring.md says the same.

Switching back to native is already in magpie: Agents > Codex > sign-in
> Always ChatGPT (`magpie codex login chatgpt`), or Keep on ChatGPT on
the takeover note; ChatGPT (the default) lets magpie take over again.

Tests: TestCodexAppRestartedWhenTheTakeoverIsTurnedOff (turned off with
a request in flight: held, then restarted; CodexAppBack not said; the
hook asked once; the reset still left running and said);
TestCodexOwnModelOutThenKeptOnChatGPT and
TestCodexOwnModelGoesThroughMagpieWhileUsedUp assert AppOutOff for Keep
on ChatGPT, Disconnect and the reset; codex-app-back.test.cjs checks
the note names the projects in every language.
Broken by hand: offByHand out of byHand, the provider test red (ended
[], want [100]); the login half of AppOutOff false, the Keep on ChatGPT
test red; the routed half false, the Disconnect check red.

Run (mote-run at b87783fbb (this change's tree) on origin/main 547585cd9): gofmt -l clean;
go build ./...; go vet linux -tags nogui ./... and windows ./...,
darwin ./internal/agent ./internal/provider ./internal/gui;
go test -tags nogui ./internal/provider ./internal/agent ok;
GUI codex-app-back, codex-app-out, i18n-keys, i18n-en-no-cjk, gui-ja,
Chromium and WebKit, every language, 155 of 155 pass.
Windows (win-run on xiaomi-gpu): the provider and agent Codex app tests
ok; the box's running Windows Codex app (26.1029) app-server command
line has the shape TestCodexAppOwnServer already holds.
Not run: a real used-up ChatGPT account (none at hand), so the restart
after Keep on ChatGPT is tried with the stubbed step, not the real app;
the full go test ./... waits behind mote's suite lock and runs after the
push, any red filed.
2026-11-10 04:14:40 +08:00

405 lines
15 KiB
Go

package agent
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// A ChatGPT account that runs out of its allowance after a magpie model was
// picked beside its sign-in leaves the Codex app sending nothing, a magpie
// model's turn included (#540): Sync makes magpie Codex's provider then, as
// a pick does for an account already out, and puts magpie back beside the
// sign-in once the allowance is back. A provider the user asked for (api)
// or one for a Codex not signed in to ChatGPT stays.
func TestCodexRunsOutAfterPick(t *testing.T) {
home, read := codexHome(t, `{"tokens":{"access_token":"x","id_token":"x.e30.x"}}`, "")
cx := codex(home)
if err := cx.Fields[0].Set("fake/m1"); err != nil {
t.Fatal(err)
}
if cfg := read(); strings.Contains(cfg, "model_provider =") || !strings.Contains(cfg, "openai_base_url") {
t.Fatalf("beside the sign-in:\n%s", cfg)
}
codexUsedUp = func() bool { return true }
if err := cx.Sync(); err != nil {
t.Fatal(err)
}
if cfg := read(); !strings.Contains(cfg, `model_provider = "magpie"`) || !strings.Contains(cfg, "model_catalog_json") ||
!strings.Contains(cfg, "openai_base_url") || !strings.Contains(cfg, `model = "fake/m1"`) {
t.Fatalf("used up:\n%s", cfg)
}
if err := cx.Sync(); err != nil { // still out: stays
t.Fatal(err)
}
if cfg := read(); !strings.Contains(cfg, `model_provider = "magpie"`) {
t.Fatalf("used up, synced again:\n%s", cfg)
}
codexUsedUp = func() bool { return false }
if err := cx.Sync(); err != nil {
t.Fatal(err)
}
if cfg := read(); strings.Contains(cfg, "model_provider =") || strings.Contains(cfg, "model_catalog_json") ||
!strings.Contains(cfg, "openai_base_url") || !strings.Contains(cfg, `model = "fake/m1"`) {
t.Fatalf("allowance back:\n%s", cfg)
}
// magpie as the provider because the user asked for it stays so
if err := cx.Field("login").Set("api"); err != nil {
t.Fatal(err)
}
if cfg := read(); !strings.Contains(cfg, `model_provider = "magpie"`) {
t.Fatalf("api:\n%s", cfg)
}
if err := cx.Sync(); err != nil {
t.Fatal(err)
}
if cfg := read(); !strings.Contains(cfg, `model_provider = "magpie"`) {
t.Fatalf("api, allowance there, synced:\n%s", cfg)
}
}
// Kept on ChatGPT (Always ChatGPT), a ChatGPT account that runs out leaves
// magpie beside the sign-in: Codex isn't moved to magpie as its provider,
// by a pick or by Sync, and one moved before the user chose it is put back
// beside it at once.
func TestCodexKeptOnChatGPT(t *testing.T) {
home, read := codexHome(t, `{"tokens":{"access_token":"x","id_token":"x.e30.x"}}`, "")
cx := codex(home)
if err := cx.Fields[0].Set("fake/m1"); err != nil {
t.Fatal(err)
}
codexUsedUp = func() bool { return true }
t.Cleanup(func() { codexUsedUp = func() bool { return false } })
if err := cx.Sync(); err != nil {
t.Fatal(err)
}
if cfg := read(); !strings.Contains(cfg, `model_provider = "magpie"`) {
t.Fatalf("ChatGPT, used up: magpie its provider:\n%s", cfg)
}
beside := func(when string) {
t.Helper()
if cfg := read(); strings.Contains(cfg, "model_provider =") || strings.Contains(cfg, "model_catalog_json") ||
!strings.Contains(cfg, "openai_base_url") || !strings.Contains(cfg, `model = "fake/m1"`) {
t.Fatalf("%s:\n%s", when, cfg)
}
}
if err := cx.Field("login").Set("chatgpt"); err != nil {
t.Fatal(err)
}
beside("kept on ChatGPT, used up")
if err := cx.Sync(); err != nil {
t.Fatal(err)
}
beside("kept on ChatGPT, used up, synced")
if err := cx.Fields[0].Set("fake/m1"); err != nil {
t.Fatal(err)
}
beside("kept on ChatGPT, used up, picked")
if got := cx.Field("login").Get(); got != "chatgpt" {
t.Fatalf("login %q", got)
}
// back to ChatGPT, it is moved again while the account is out
if err := cx.Field("login").Set(""); err != nil {
t.Fatal(err)
}
if cfg := read(); !strings.Contains(cfg, `model_provider = "magpie"`) {
t.Fatalf("ChatGPT again, used up:\n%s", cfg)
}
}
// The same on one of Codex's own models picked beside the sign-in
// (MasterYi on Discord: the Codex app greyed Send on a used-up account,
// and the third-party models it was joined with couldn't be used either).
// Sync makes magpie Codex's provider on that model as magpie serves it,
// and once the account has room puts Codex back on the one picked, beside
// its sign-in.
func TestCodexOwnModelGoesThroughMagpieWhileUsedUp(t *testing.T) {
home, read := codexHome(t, `{"tokens":{"access_token":"x","id_token":"x.e30.x"}}`, "model = \"gpt-a\"\n")
// the Codex subscription falls back to another provider while the
// account is out: magpie has something to serve the turn on
if err := provider.Save(provider.Provider{ID: "codex", Fallback: []string{"fake/m1"}}); err != nil {
t.Fatal(err)
}
os.WriteFile(filepath.Join(home, ".codex", "models_cache.json"),
[]byte(`{"models":[{"slug":"gpt-a","display_name":"A","priority":1}]}`), 0o644)
cx := codex(home)
if ok, err := cx.Join(); err != nil || !ok {
t.Fatalf("join: %v %v", ok, err)
}
if cfg := read(); strings.Contains(cfg, "model_provider =") || !strings.Contains(cfg, `model = "gpt-a"`) {
t.Fatalf("joined:\n%s", cfg)
}
if cx.AppOut() {
t.Fatal("joined with room: AppOut")
}
codexUsedUp = func() bool { return true }
if err := cx.Sync(); err != nil {
t.Fatal(err)
}
if cfg := read(); !strings.Contains(cfg, `model_provider = "magpie"`) || !strings.Contains(cfg, `model = "codex/gpt-a"`) {
t.Fatalf("used up:\n%s", cfg)
}
if !cx.AppOut() {
t.Fatal("used up, magpie its provider: not AppOut")
}
if err := cx.Sync(); err != nil { // still out: stays
t.Fatal(err)
}
if cfg := read(); !strings.Contains(cfg, `model = "codex/gpt-a"`) {
t.Fatalf("used up, synced again:\n%s", cfg)
}
codexUsedUp = func() bool { return false }
if err := cx.Sync(); err != nil {
t.Fatal(err)
}
if cfg := read(); strings.Contains(cfg, "model_provider =") || strings.Contains(cfg, "model_catalog_json") ||
!strings.Contains(cfg, "openai_base_url") || !strings.Contains(cfg, `model = "gpt-a"`) {
t.Fatalf("allowance back:\n%s", cfg)
}
if cx.AppOut() || !cx.Joined() {
t.Fatalf("allowance back: AppOut %v, Joined %v", cx.AppOut(), cx.Joined())
}
// the reset ended the takeover, not the user (CodexTakeoverOff)
if cx.AppOutOff() {
t.Fatal("allowance back: the takeover reads as turned off by the user")
}
// a model the user picked while out stays theirs when room comes back
codexUsedUp = func() bool { return true }
if err := cx.Sync(); err != nil {
t.Fatal(err)
}
if err := cx.Fields[0].Set("fake/m1"); err != nil {
t.Fatal(err)
}
codexUsedUp = func() bool { return false }
if err := cx.Sync(); err != nil {
t.Fatal(err)
}
if cfg := read(); !strings.Contains(cfg, `model = "fake/m1"`) {
t.Fatalf("picked while out, allowance back:\n%s", cfg)
}
// taken over again, then Codex disconnected: the user's end of it
codexUsedUp = func() bool { return true }
t.Cleanup(func() { codexUsedUp = func() bool { return false } })
if err := cx.Sync(); err != nil {
t.Fatal(err)
}
if !cx.AppOut() || cx.AppOutOff() {
t.Fatalf("taken over again: AppOut %v, AppOutOff %v:\n%s", cx.AppOut(), cx.AppOutOff(), read())
}
if err := cx.Disconnect(); err != nil {
t.Fatal(err)
}
if cx.AppOut() || !cx.AppOutOff() {
t.Fatalf("disconnected: AppOut %v, AppOutOff %v:\n%s", cx.AppOut(), cx.AppOutOff(), read())
}
}
// One ChatGPT account, out, on one of Codex's own models joined beside the
// sign-in, and nothing else in magpie to offer: no fallback on the Codex
// subscription, no other account and no model of another provider
// (xiaoq6285 on Discord: a Plus account in the Windows Codex app, the menu
// down to "magpie" with no quota after the five-hour limit, the app
// erroring and restarting at the limit and again at the reset). magpie
// leaves Codex on its sign-in: no turn it took over could be sent, so the
// app keeps the account, its usage and its reset, and nothing restarts.
func TestCodexOwnModelOneSpentAccountStaysOnItsSignIn(t *testing.T) {
home, read := codexHome(t, `{"tokens":{"access_token":"x","id_token":"x.e30.x"}}`, "model = \"gpt-a\"\n")
// codexHome's other provider is the one thing magpie could offer
if err := provider.Delete("fake"); err != nil {
t.Fatal(err)
}
os.WriteFile(filepath.Join(home, ".codex", "models_cache.json"),
[]byte(`{"models":[{"slug":"gpt-a","display_name":"A","priority":1}]}`), 0o644)
cx := codex(home)
if ok, err := cx.Join(); err != nil || !ok {
t.Fatalf("join: %v %v", ok, err)
}
if codexOwnViaMagpie("gpt-a") == "" {
t.Fatal("magpie lists no codex/gpt-a: the test proves nothing")
}
if codexOtherModel() != "" {
t.Fatalf("magpie has another model %q: the test proves nothing", codexOtherModel())
}
route, _, known := codexAppRoute()
if !known {
t.Fatal("route not known")
}
// the route is what the app's app-server is ended over
// (KeepCodexAppOnItsProvider): unchanged, the app keeps running
same := func(when string) {
t.Helper()
cfg := read()
if !strings.Contains(cfg, `model = "gpt-a"`) || strings.Contains(cfg, "model_provider =") || !strings.Contains(cfg, "openai_base_url") {
t.Fatalf("%s: Codex moved off its sign-in:\n%s", when, cfg)
}
if r, out, _ := codexAppRoute(); r != route || out || cx.AppOut() {
t.Fatalf("%s: the app's app-server would be ended (out %v):\n%s\nwas:\n%s", when, out, r, route)
}
}
codexUsedUp = func() bool { return true }
for _, when := range []string{"used up", "used up, synced again"} {
if err := cx.Sync(); err != nil {
t.Fatal(err)
}
same(when)
}
codexUsedUp = func() bool { return false }
if err := cx.Sync(); err != nil {
t.Fatal(err)
}
same("reset")
if !cx.Joined() {
t.Fatal("reset: not joined")
}
}
// The same account, with a model of another provider in magpie (fake/m1)
// but nothing to serve Codex's own model on (CodexServesWhileOut false):
// the Codex app greys Send for every model while the account is out, so
// magpie takes Codex over on that model (shamen, chenhan on Discord),
// once, and puts Codex back on its own model at the reset, once. A steady
// held state writes nothing and ends no app-server. This reverses
// f37742e14, which stayed on the sign-in.
func TestCodexOwnModelOneSpentAccountTakenOverForOtherModels(t *testing.T) {
home, read := codexHome(t, `{"tokens":{"access_token":"x","id_token":"x.e30.x"}}`, "model = \"gpt-a\"\n")
os.WriteFile(filepath.Join(home, ".codex", "models_cache.json"),
[]byte(`{"models":[{"slug":"gpt-a","display_name":"A","priority":1}]}`), 0o644)
cx := codex(home)
if ok, err := cx.Join(); err != nil || !ok {
t.Fatalf("join: %v %v", ok, err)
}
if provider.CodexServesWhileOut("gpt-a") {
t.Fatal("magpie serves gpt-a while out: the test proves nothing")
}
if codexOtherModel() != "fake/m1" {
t.Fatalf("other model %q", codexOtherModel())
}
codexUsedUp = func() bool { return true }
if err := cx.Sync(); err != nil {
t.Fatal(err)
}
cfg := read()
if !strings.Contains(cfg, `model_provider = "magpie"`) || !strings.Contains(cfg, `model = "fake/m1"`) || !cx.AppOut() {
t.Fatalf("used up:\n%s", cfg)
}
route, _, _ := codexAppRoute()
// steady: no write, no route change (an app-server end), however often
for i := 0; i < 3; i++ {
if err := cx.Sync(); err != nil {
t.Fatal(err)
}
if got := read(); got != cfg {
t.Fatalf("held, synced again (%d): config written:\n%s\nwas:\n%s", i, got, cfg)
}
if r, _, _ := codexAppRoute(); r != route {
t.Fatalf("held, synced again (%d): route changed:\n%s\nwas:\n%s", i, r, route)
}
}
codexUsedUp = func() bool { return false }
if err := cx.Sync(); err != nil {
t.Fatal(err)
}
if got := read(); strings.Contains(got, "model_provider =") || strings.Contains(got, "model_catalog_json") ||
!strings.Contains(got, `model = "gpt-a"`) || !strings.Contains(got, "openai_base_url") {
t.Fatalf("reset:\n%s", got)
}
if cx.AppOut() || !cx.Joined() {
t.Fatalf("reset: AppOut %v, Joined %v", cx.AppOut(), cx.Joined())
}
back := read()
if err := cx.Sync(); err != nil || read() != back {
t.Fatalf("reset, synced again: %v\n%s", err, read())
}
}
// Kept on ChatGPT, a used-up account on one of Codex's own models leaves
// Codex as it is: the user chose the sign-in.
func TestCodexOwnModelKeptOnChatGPT(t *testing.T) {
home, read := codexHome(t, `{"tokens":{"access_token":"x","id_token":"x.e30.x"}}`, "model = \"gpt-a\"\n")
os.WriteFile(filepath.Join(home, ".codex", "models_cache.json"),
[]byte(`{"models":[{"slug":"gpt-a","display_name":"A","priority":1}]}`), 0o644)
cx := codex(home)
if ok, err := cx.Join(); err != nil || !ok {
t.Fatalf("join: %v %v", ok, err)
}
if err := cx.Field("login").Set("chatgpt"); err != nil {
t.Fatal(err)
}
codexUsedUp = func() bool { return true }
if err := cx.Sync(); err != nil {
t.Fatal(err)
}
if cfg := read(); strings.Contains(cfg, "model_provider =") || !strings.Contains(cfg, `model = "gpt-a"`) {
t.Fatalf("kept on ChatGPT, used up:\n%s", cfg)
}
if cx.AppOut() {
t.Fatal("kept on ChatGPT: AppOut")
}
}
// Moved onto magpie on one of Codex's own models while the account is
// used up, then kept on ChatGPT from the Agents row's takeover line
// (eddie.zhan on Discord: the Codex app's browser use said it had no
// permission): Codex goes back on its sign-in at once, on the model it
// picked, beside it as joined, and no later Sync moves it again.
func TestCodexOwnModelOutThenKeptOnChatGPT(t *testing.T) {
home, read := codexHome(t, `{"tokens":{"access_token":"x","id_token":"x.e30.x"}}`, "model = \"gpt-a\"\n")
os.WriteFile(filepath.Join(home, ".codex", "models_cache.json"),
[]byte(`{"models":[{"slug":"gpt-a","display_name":"A","priority":1}]}`), 0o644)
cx := codex(home)
if ok, err := cx.Join(); err != nil || !ok {
t.Fatalf("join: %v %v", ok, err)
}
// a fallback for magpie to serve the turn on (TestCodexOwnModelOneSpentAccountStaysOnItsSignIn)
if err := provider.Save(provider.Provider{ID: "codex", Fallback: []string{"fake/m1"}}); err != nil {
t.Fatal(err)
}
codexUsedUp = func() bool { return true }
t.Cleanup(func() { codexUsedUp = func() bool { return false } })
if err := cx.Sync(); err != nil {
t.Fatal(err)
}
if !cx.AppOut() {
t.Fatalf("used up: not AppOut:\n%s", read())
}
if cx.AppOutOff() {
t.Fatal("taken over: the takeover reads as turned off")
}
if err := cx.Field("login").Set("chatgpt"); err != nil {
t.Fatal(err)
}
// the user's own end of the takeover, which restarts the Codex app's
// app-server so its ChatGPT projects come back (CodexTakeoverOff)
if !cx.AppOutOff() {
t.Fatal("kept on ChatGPT: the takeover doesn't read as turned off by the user")
}
onSignIn := func(when string) {
t.Helper()
if cfg := read(); strings.Contains(cfg, "model_provider =") || strings.Contains(cfg, "model_catalog_json") || !strings.Contains(cfg, `model = "gpt-a"`) {
t.Fatalf("%s:\n%s", when, cfg)
}
if cx.AppOut() {
t.Fatalf("%s: AppOut", when)
}
}
onSignIn("kept on ChatGPT while used up")
if err := cx.Sync(); err != nil {
t.Fatal(err)
}
onSignIn("kept on ChatGPT, synced")
codexUsedUp = func() bool { return false }
if err := cx.Sync(); err != nil {
t.Fatal(err)
}
onSignIn("kept on ChatGPT, allowance back")
}