* provider, codex: a ChatGPT account at 100% with credits left keeps Codex signed in — magpie becomes its provider only while the Codex app itself holds the account magpie made itself Codex's provider (model_provider = "magpie", #540) whenever any of the active ChatGPT account's windows read 100%. A Pro account at 100% on its week with credits left keeps working in the Codex app: the backend answers every turn (about 490 200s in one day's usage log at 100%), yet magpie moved Codex off it, and as a provider without requires_openai_auth the app lost its ChatGPT state ("Sign in to ChatGPT to start a durable thread", no remote control). CodexUsedUp now reads codexHeld, which mirrors the Codex app's own composer block (ChatGPT.app webview bundle, hP and its helpers): the backend says the account isn't allowed (rate_limit.allowed = false), it has no credits (has_credits / unlimited), and it isn't a workspace still within its overage (overage_limit_reached = false, no spend cap, an ordinary rate_limit_reached). Window percent never decides it. Where the app goes by what /wham/usage can't show (a statsig gate, the gpt-reserve fallback, Team's reserve experiment that ignores overage), it counts as held: a wrong move costs ChatGPT extras, a missed one every turn. A reading kept through a failed read (keepLast) stops saying held once its used-up window has started again, as its usage does. NextLogin's account rotation and its thresholds are unchanged. A user stuck on the old move (codex.out = 1) is put back beside the sign-in by the next Sync, as #540 already does once the account has room. Verification: - TestCodexUsedUpIsWhatTheAppHolds (real /wham/usage replies, ids replaced) fails on the old trigger: 7 subcases, "CodexUsedUp = true, want false". Each workspace guard (spend cap, reason, overage, plan list, Team reserve) was mutated and fails its own subcase. - TestCodexHeldKeptOnlyTillTheReset fails without the quota_last change. - The real Pro account (7-day window 100%, ~28k credits, allowed false): CodexUsedUp true before, false after. - go vet ./...; go build (darwin), GOOS=linux -tags nogui, GOOS=windows; go test -tags nogui ./... under a sandbox HOME; -race -count=20 on the new and related Codex tests. - Not run: a live Codex app turn on an account truly held (none at hand), Windows/WSL runtime, GUI suites (no GUI change). * provider: a Codex account at a spend cap or past its overage is held, credits or not; limit_reached counts as not allowed codexHeld let credits through before the spend cap, so a Team seat at its cap (spend_control.reached, allowed false) with has_credits read not held and magpie left Codex on an account the backend refuses. The app's readiness check (26.930.61225, app-primary) returns limited on spend_control.reached or credits.overage_limit_reached before it looks at credits, and treats rate_limit.limit_reached like allowed false; held now reads the same. A spend cap with the rate limit still allowed (a workspace's included usage) stays not held.
130 lines
4.6 KiB
Go
130 lines
4.6 KiB
Go
package provider
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"slices"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
// The credits a ChatGPT account holds besides its windows are its balance
|
|
// (#571), shown when it has some to spend and they are not unlimited.
|
|
func TestCodexCreditsBalance(t *testing.T) {
|
|
for _, c := range []struct {
|
|
name string
|
|
credits any
|
|
want string
|
|
}{
|
|
{"none said", nil, ""},
|
|
{"held", map[string]any{"has_credits": true, "unlimited": false, "balance": "1234.5"}, "1234.5 credits"},
|
|
{"a few", map[string]any{"has_credits": true, "unlimited": false, "balance": "42"}, "42 credits"},
|
|
{"none held", map[string]any{"has_credits": false, "unlimited": false, "balance": "0"}, ""},
|
|
{"spent", map[string]any{"has_credits": true, "unlimited": false, "balance": "0"}, ""},
|
|
{"unlimited", map[string]any{"has_credits": true, "unlimited": true, "balance": ""}, ""},
|
|
{"unreadable", map[string]any{"has_credits": true, "balance": "lots"}, ""},
|
|
} {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
fake := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
body := map[string]any{"plan_type": "plus", "rate_limit": map[string]any{
|
|
"primary_window": map[string]any{"used_percent": 100, "limit_window_seconds": 18000}}}
|
|
if c.credits != nil {
|
|
body["credits"] = c.credits
|
|
}
|
|
json.NewEncoder(w).Encode(body)
|
|
}))
|
|
defer fake.Close()
|
|
old := CodexBase
|
|
CodexBase = fake.URL + "/backend-api/codex"
|
|
defer func() { CodexBase = old }()
|
|
plan, windows, _, credits, _, err := codexWindows(context.Background(), "tok", "acct-1")
|
|
if err != nil || plan != "plus" || len(windows) != 1 {
|
|
t.Fatalf("plan %q, windows %v, err %v", plan, windows, err)
|
|
}
|
|
if credits != c.want {
|
|
t.Errorf("credits %q, want %q", credits, c.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// Whether an account spends its credits is kept by account, whatever its
|
|
// case: on unless turned off. Turned off, a Codex account is held at 100%
|
|
// of its windows; a cap holds it sooner, and nothing holds another
|
|
// vendor's account for its credits.
|
|
func TestCodexCreditsSwitch(t *testing.T) {
|
|
signIn(t)
|
|
if !CodexCredits("me@example.com") {
|
|
t.Fatal("off before it was turned off")
|
|
}
|
|
if err := SetCodexCredits(" Me@Example.com ", false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := settings.Load().CodexNoCredits; !slices.Equal(got, []string{"me@example.com"}) {
|
|
t.Fatalf("kept %v", got)
|
|
}
|
|
if CodexCredits("ME@example.com") || !CodexCredits("other@example.com") {
|
|
t.Fatal("read back wrong")
|
|
}
|
|
p := Provider{ID: "codex", AccountCaps: map[string]int{"capped@example.com": 70}}
|
|
for _, c := range []struct {
|
|
agent, user string
|
|
share int
|
|
noCredits bool
|
|
}{
|
|
{"codex", "me@example.com", 100, true},
|
|
{"codex", "other@example.com", 0, false},
|
|
{"claude", "me@example.com", 0, false},
|
|
{"codex", "capped@example.com", 70, false},
|
|
} {
|
|
if share, no := HoldShare(p, c.agent, c.user); share != c.share || no != c.noCredits {
|
|
t.Errorf("%s %s: held at %d (credits %v)", c.agent, c.user, share, no)
|
|
}
|
|
}
|
|
if err := SetCodexCredits("me@example.com", true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !CodexCredits("me@example.com") || len(settings.Load().CodexNoCredits) != 0 {
|
|
t.Fatal("still off")
|
|
}
|
|
}
|
|
|
|
// A reset spent starts an account's windows again: what was read of them
|
|
// before is forgotten, so what holds the account at a share of them (its
|
|
// cap, its credits not spent) holds it no more, and they are read again
|
|
// at once; the other accounts' readings stay.
|
|
func TestRenewedAccountForgetsItsAllowance(t *testing.T) {
|
|
signIn(t)
|
|
usedCache.Lock()
|
|
usedCache.m, usedCache.at, usedCache.loading = map[string]map[string]Allowance{}, map[string]time.Time{}, map[string]chan struct{}{}
|
|
usedCache.m["codex"] = map[string]Allowance{
|
|
"me@example.com": {{Used: 100, Span: 5 * time.Hour}},
|
|
"spare@example.com": {{Used: 10, Span: 5 * time.Hour}},
|
|
}
|
|
usedCache.at["codex"] = time.Now()
|
|
handed := usedCache.m["codex"]
|
|
usedCache.Unlock()
|
|
t.Cleanup(func() {
|
|
usedCache.Lock()
|
|
usedCache.m, usedCache.at, usedCache.loading = map[string]map[string]Allowance{}, map[string]time.Time{}, map[string]chan struct{}{}
|
|
usedCache.Unlock()
|
|
})
|
|
renewedNow("codex", "Me@Example.com")
|
|
usedCache.Lock()
|
|
m, at := usedCache.m["codex"], usedCache.at["codex"]
|
|
usedCache.Unlock()
|
|
if _, ok := m["me@example.com"]; ok {
|
|
t.Fatalf("still read as before: %v", m["me@example.com"])
|
|
}
|
|
if _, ok := m["spare@example.com"]; !ok || !at.IsZero() {
|
|
t.Fatalf("spare kept %v, read again at once %v", ok, at.IsZero())
|
|
}
|
|
if _, ok := handed["me@example.com"]; !ok {
|
|
t.Fatal("the map handed out before was changed")
|
|
}
|
|
}
|