Files
magpie/internal/provider/codex_hold_left_alone_test.go
T
yetone adc618bbf9 codex: a Codex switched off in magpie still has its held ChatGPT account read, so the Codex app sends magpie's models (sherlock_23333 on X)
sherlock_23333 on X: once the ChatGPT account Codex is signed in to ran
out, the Codex app's Send stayed greyed even on a magpie model, through
restarts and reconnects.

The gate is the Codex app's own (ChatGPT.app 26.1021, composer gM):
Send is blocked while its authMethod is chatgpt and its own /wham/usage
says rate_limit.allowed false with no credits. magpie's one lever is
the takeover (codex.out): magpie becomes Codex's provider without
requires_openai_auth, so the app-server reports no ChatGPT account and
the gate lifts. That runs only when provider.CodexUsedUp says the
account is held, and CodexUsedUp read LoginUsage, which returns nothing
for a provider switched off or removed in magpie (leftAlone). So a user
who turned Codex off in magpie once its allowance ran out never got the
takeover, and one already taken over was put back on the held account.

Now, with Codex left alone, CodexUsedUp reads the active account alone
(codexHoldReading) while Codex takes magpie's models beside its sign-in
or magpie is its provider for the held account (Agent.HoldMatters, via
the new provider.CodexHoldMatters hook). At most once a minute (the
loginReading cache), with the token as Codex keeps it, never renewed or
written (codexTokenAsKept); the Usage page still shows nothing of it.
Codex's prompts and base_instructions are untouched.

Run at f41b63232 + this change, on mote via mote-run:
gofmt -l clean; go vet ./...; GOOS=linux go vet -tags nogui ./...;
GOOS=windows go vet ./...; go build ./... all ok.
go test -count=1 -timeout 45m ./internal/provider ./internal/agent: ok
(219s, 340s).
Rebased on 9f25de56c (a Cursor mover min and a GUI test), then at 9f25de56c +
this change: go vet and go test -count=1 -timeout 45m of
./internal/provider ./internal/agent again: ok (178s, 273s).
8c63c5b78 (plugin speech models; no file in common) landed on main
after that run and this is rebased onto it; the two packages were not
run again with it.

Broken by hand: without the leftAlone branch in CodexUsedUp,
TestCodexHoldReadLeftAlone fails ("switched off: CodexUsedUp = false on
a held account"); with the left-alone token read sent back through
codexToken, it fails with the expired sign-in refreshed once.

Not tried: the reporter's own config (whether their Codex was off in
magpie is inferred, not confirmed); the real Codex app against a held
account; internal/gateway's suite (no gateway code changed); Linux and
Windows test runs (vet only).
2026-10-29 18:20:50 +08:00

103 lines
3.3 KiB
Go

package provider
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"sync/atomic"
"testing"
"time"
)
// sherlock_23333 on X: the ChatGPT account Codex is signed in to ran out,
// and the Codex app greyed Send for magpie's models too, through restarts.
// With Codex switched off or removed in magpie nothing read the account,
// so magpie never became Codex's provider while the app held it. Its hold
// is read still, while Codex takes magpie's models, and only then: the
// account Codex is on alone, its sign-in never renewed by magpie.
func TestCodexHoldReadLeftAlone(t *testing.T) {
home := signIn(t)
rememberLogins(true)
body := heldUsage(t)
var usage, refresh atomic.Int64
fake := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/backend-api/wham/usage":
usage.Add(1)
json.NewEncoder(w).Encode(body)
case "/oauth/token":
refresh.Add(1)
w.WriteHeader(500)
default:
w.WriteHeader(404)
}
}))
t.Cleanup(fake.Close)
oldBase, oldTok := CodexBase, codexTokenURL
CodexBase, codexTokenURL = fake.URL+"/backend-api/codex", fake.URL+"/oauth/token"
matters := false
oldHook := CodexHoldMatters
CodexHoldMatters = func() bool { return matters }
t.Cleanup(func() { CodexBase, codexTokenURL, CodexHoldMatters = oldBase, oldTok, oldHook })
fresh := func() {
loginUsageCache.Lock()
loginUsageCache.m, loginUsageCache.pending = nil, nil
loginUsageCache.Unlock()
}
for _, c := range []struct {
name string
p map[string]any
}{
{"switched off", map[string]any{"id": "codex", "off": true}},
{"removed from magpie", map[string]any{"id": "codex", "hidden": true}},
} {
writeFile(t, Path(), map[string]any{"providers": []map[string]any{c.p}})
// not taking magpie's models: left alone, nothing read
matters = false
fresh()
usage.Store(0)
if CodexUsedUp(t.Context()) || usage.Load() != 0 {
t.Fatalf("%s, Codex not on magpie: read %d times", c.name, usage.Load())
}
// taking them: the app's hold is read, and known after
matters = true
fresh()
if !CodexUsedUp(t.Context()) {
t.Fatalf("%s: CodexUsedUp = false on a held account", c.name)
}
if usage.Load() != 1 {
t.Fatalf("%s: read %d times, want 1", c.name, usage.Load())
}
if !CodexUsedUpKnown() {
t.Fatalf("%s: CodexUsedUpKnown = false after the read", c.name)
}
// the Usage page still reads nothing of it
if u := LoginUsage(t.Context(), "codex"); len(u) != 0 {
t.Fatalf("%s: LoginUsage = %v, want nothing", c.name, u)
}
}
// a sign-in about to expire is left to Codex: not refreshed, not
// written, the hold unknown
auth := filepath.Join(home, ".codex", "auth.json")
var a map[string]any
b, _ := os.ReadFile(auth)
json.Unmarshal(b, &a)
a["tokens"].(map[string]any)["access_token"] = fakeJWT(map[string]any{"exp": float64(time.Now().Add(-time.Minute).Unix())})
writeFile(t, auth, a)
before, _ := os.ReadFile(auth)
fresh()
usage.Store(0)
if CodexUsedUp(t.Context()) {
t.Fatal("expired sign-in: CodexUsedUp = true")
}
after, _ := os.ReadFile(auth)
if refresh.Load() != 0 || usage.Load() != 0 || !bytes.Equal(before, after) {
t.Fatalf("expired sign-in left alone: refreshed %d, read %d, auth.json rewritten %v", refresh.Load(), usage.Load(), !bytes.Equal(before, after))
}
}