Files
magpie/internal/gateway/claude_codex_fallback_test.go
T
yetone b14784c03f gateway: a fallback too small for the request is asked once every account with room is out of its allowance (Kai on Discord)
Kai on Discord (0.1.1278): Claude Code to claude/claude-opus-5-5, the
Claude provider's two subscription accounts (Max, Pro) both at their
limit ("resets 5:58am", "resets Oct 18 at 3pm"), its Fallback
codex/gpt-6.1-sol. Routing said the second account's 429 had nobody left
to try, and Claude Code had the 429. The fallback was never in the
route's order.

Cause: roomy (0f398c489, 10-15) leaves out, before anyone is asked, a
candidate whose known window is no larger than the request's estimate.
A Codex account's gpt-6.1-sol is 272K (its live list's context_window);
Opus holds 1M. A Claude Code conversation past ~272K tokens left the
Codex fallback out of the candidates and the trace's order, silently,
and when both Claude accounts failed for their allowance the agent was
told to wait hours. "It worked yesterday": the conversation was shorter.
A short request on current main already fell back (the new test's first
cases pass on the old code).

Fix: roomy hands back what it left out (small, with its order rows).
When the last candidate with room fails out of its allowance or credit
(failQuota/failCredit), nothing before it failed otherwise (other ==
nil), no account is pinned, and it isn't an overflow, refusal or
turned-away prompt, those left out that aren't resting are appended and
asked, their rows added to the trace's order, and the failed one rests
as one before the last does. The too-small fallback's overflow then
reaches the agent through the existing outgrew path, to compact, after
which it holds the conversation. Any other failure (a bad key, a busy
vendor) reaches the agent as before, the small member not asked, so
dgqyushen's free-200K-first group keeps its behaviour. Streaming and
non-streaming share the loop; count_tokens and System One's decision
models don't go through roomy. The Routing page still shows a member
left out and not asked only by its absence (no GUI change).

Tests:
- TestClaudeAccountsSpentFallBackToCodex (new): fake `claude` CLI that
  answers every account with rate_limit_event rejected + "You've hit
  your limit", a fake Codex backend, Claude's Fallback codex/gpt-6.1-sol,
  Codex's window 272K. Short request streamed / not / with accounts
  already resting: Codex answers (green before and after). A ~375K-token
  request streamed and not: red on the old code (429, codex asked 0,
  order = the two Claude accounts), green now.
- TestMemberTooSmallIsTheLastResort (new): group paid(10K) then
  free(2K), a 2.2K request. paid 401: the agent gets the 401, free not
  asked. paid 429 insufficient_quota: free answers, paid rests, free in
  the order.
- TestRoomyLeavesWhatItCantJudge checks the handed-back candidates.

Broken by hand:
- the new block disabled (false &&): both stream cases of the Claude
  test red (429, codex asked 0), TestMemberTooSmallIsTheLastResort red
  (429 insufficient_quota, free 0). Restored: green.
- the quota condition forced true: TestMemberTooSmallIsTheLastResort's
  401 case red (free asked, 200). Restored: green.
- the other == nil condition removed: still green; no test has an
  earlier member failing otherwise before a quota failure.

Run:
- gofmt -l: clean. go vet ./internal/gateway (darwin), GOOS=linux go vet
  -tags nogui ./..., GOOS=windows go vet ./...: clean.
- go test -timeout 45m ./... at de671719a (load ~240-470): every package ok,
  internal/gateway included (2451s), except three reds in packages this
  doesn't touch, each already filed and each a timing bound under load:
  internal/gui TestMainWindowCorners/macOS_26_SDK killed at its deadline
  (#1936, #2645; red alone too on this loaded machine), internal/library
  TestCheckStdio/timeout_ends_the_tree (#2765; -count=3 alone: ok),
  internal/plugin TestStopLetsARenewalEnd (#2743; -count=2 alone: ok).
- After rebasing on origin/main: go test ./internal/gateway -run with the
  new and roomy/overflow tests: ok.

Not done: Routing still shows a member left out and not asked only by
its absence; a row saying "too small for this request" would be a GUI
and i18n change of its own. Not tried against Kai's real accounts.
2026-10-18 02:17:02 +08:00

160 lines
7.1 KiB
Go

package gateway
import (
"encoding/base64"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/testenv"
)
// Kai on Discord (0.1.1278): Claude Code asks claude/claude-opus-5-5 at
// high effort, both Claude accounts are out of their limit, and the Claude
// provider's own Fallback, a Codex subscription model, takes the request —
// streamed or not.
func TestClaudeAccountsSpentFallBackToCodex(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("a shell script stands in for Claude Code")
}
fresh(t)
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
far := time.Now().Add(24 * time.Hour).UnixMilli()
oauth := func(tok, plan string) map[string]any {
return map[string]any{"claudeAiOauth": map[string]any{"accessToken": tok, "refreshToken": "r-" + tok, "expiresAt": far, "subscriptionType": plan}}
}
os.MkdirAll(filepath.Join(home, ".claude"), 0o755)
os.WriteFile(filepath.Join(home, ".claude", ".credentials.json"), mustJSON(oauth("tok-primary", "max")), 0o600)
os.WriteFile(filepath.Join(home, ".claude.json"), mustJSON(map[string]any{
"oauthAccount": map[string]any{"emailAddress": "me@example.com", "accountUuid": "u-me"}}), 0o600)
claims := func(m map[string]any) string {
b, _ := json.Marshal(m)
return "h." + base64.RawURLEncoding.EncodeToString(b) + ".s"
}
os.MkdirAll(filepath.Join(home, ".codex"), 0o755)
os.WriteFile(filepath.Join(home, ".codex", "auth.json"), mustJSON(map[string]any{"auth_mode": "chatgpt", "tokens": map[string]any{
"id_token": claims(map[string]any{"email": "me@example.com"}),
"access_token": claims(map[string]any{"exp": time.Now().Add(time.Hour).Unix(), "who": "acct-1"}),
"refresh_token": "r-acct-1", "account_id": "acct-1"}}), 0o600)
os.MkdirAll(filepath.Dir(provider.Path()), 0o755)
os.WriteFile(filepath.Join(filepath.Dir(provider.Path()), "logins.json"), mustJSON([]map[string]any{
{"agent": "claude", "user": "me@example.com", "plan": "max", "on": true, "seen": time.Now(), "auth": oauth("tok-me", "max")},
{"agent": "claude", "user": "pro@example.com", "plan": "pro", "on": true, "seen": time.Now(), "auth": oauth("tok-pro", "pro")},
}), 0o600)
dir := t.TempDir()
log := filepath.Join(dir, "log")
// every account is out of its limit: the CLI says so in a result
script := `#!/bin/sh
if [ "$1" = auth ]; then echo '{"loggedIn":true,"authMethod":"oauth_token","apiProvider":"firstParty"}'; exit 0; fi
TOK=; [ -n "$CLAUDE_CONFIG_DIR" ] && TOK=$(sed -n 's/.*"accessToken": *"\([^"]*\)".*/\1/p' "$CLAUDE_CONFIG_DIR/.credentials.json")
echo "${TOK:-own}" >> ` + log + `
while read -r line; do
echo '{"type":"rate_limit_event","rate_limit_info":{"status":"rejected","rateLimitType":"five_hour","resetsAt":1790700000}}'
echo '{"type":"assistant","message":{"id":"x","model":"<synthetic>","role":"assistant","content":[{"type":"text","text":"You'"'"'ve hit your limit · resets 5:58am"}]},"error":"rate_limit"}'
echo '{"type":"result","subtype":"success","is_error":true,"result":"You'"'"'ve hit your limit · resets 5:58am"}'
done
`
testenv.Program(t, filepath.Join(dir, "claude"), script)
t.Setenv("PATH", dir+string(os.PathListSeparator)+os.Getenv("PATH"))
var mu sync.Mutex
var codex []string
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, _ := io.ReadAll(r.Body)
mu.Lock()
codex = append(codex, string(b))
mu.Unlock()
w.Header().Set("Content-Type", "text/event-stream")
io.WriteString(w, sse(
`data: {"type":"response.created","response":{"id":"r1","model":"gpt-6.1-sol"}}`,
`data: {"type":"response.output_item.added","output_index":0,"item":{"type":"message","id":"m1","role":"assistant","content":[]}}`,
`data: {"type":"response.output_text.delta","item_id":"m1","output_index":0,"content_index":0,"delta":"pong"}`,
`data: {"type":"response.output_item.done","output_index":0,"item":{"type":"message","id":"m1","role":"assistant","content":[{"type":"output_text","text":"pong"}]}}`,
`data: {"type":"response.completed","response":{"id":"r1","status":"completed","output":[{"type":"message","id":"m1","role":"assistant","content":[{"type":"output_text","text":"pong"}]}],"usage":{"input_tokens":7,"output_tokens":1}}}`))
}))
defer up.Close()
old := provider.CodexBase
provider.CodexBase = up.URL + "/backend-api/codex"
defer func() { provider.CodexBase = old }()
provider.ForgetAccounts()
t.Cleanup(provider.ForgetAccounts)
if err := provider.Save(provider.Provider{ID: "claude", Fallback: []string{"codex/gpt-6.1-sol"}}); err != nil {
t.Fatal(err)
}
p, err := provider.Find("claude")
if err != nil || len(p.Fallback) != 1 {
t.Fatalf("claude's fallback not kept: %+v %v", p.Fallback, err)
}
// Codex holds a ChatGPT plan's conversation to 272K; Opus holds 1M
if err := provider.Save(provider.Provider{ID: "codex", Contexts: map[string]int{"gpt-6.1-sol": 272000}}); err != nil {
t.Fatal(err)
}
long := strings.Repeat("word ", 300000) // ~375K tokens by estimate
for _, tc := range []struct {
stream, text string
again bool
}{
{"true", "ping", false}, {"false", "ping", false}, {"true", "ping", true},
// a conversation past Codex's window: the fallback is still asked
// when every account with room is out of its limit, for the agent
// to be told to compact rather than wait hours (Kai)
{"true", long, false}, {"false", long, false},
} {
stream := tc.stream
restingUntil.Lock()
restingUntil.m = map[string]time.Time{}
restingUntil.Unlock()
os.Remove(log)
mu.Lock()
codex = nil
mu.Unlock()
s := New()
if tc.again {
for k := 0; k < 1; k++ {
rq := httptest.NewRequest("POST", "/v1/messages", strings.NewReader(`{"model":"claude/claude-opus-5-5","max_tokens":50,"stream":true,"messages":[{"role":"user","content":"ping"}]}`))
rq.Header.Set("User-Agent", "claude-cli/2.1.0 (external, cli)")
s.Handler().ServeHTTP(httptest.NewRecorder(), rq)
}
mu.Lock()
codex = nil
mu.Unlock()
}
done := make(chan struct{})
rec := httptest.NewRecorder()
go func() {
defer close(done)
req := httptest.NewRequest("POST", "/v1/messages", strings.NewReader(`{"model":"claude/claude-opus-5-5","max_tokens":50,"stream":`+stream+`,"thinking":{"type":"adaptive"},"output_config":{"effort":"high"},"messages":[{"role":"user","content":"`+tc.text+`"}]}`))
req.Header.Set("User-Agent", "claude-cli/2.1.0 (external, cli)")
s.Handler().ServeHTTP(rec, req)
}()
select {
case <-done:
case <-time.After(30 * time.Second):
t.Fatalf("stream=%s: no answer", stream)
}
b, _ := os.ReadFile(log)
mu.Lock()
got := len(codex)
mu.Unlock()
if rec.Code != 200 || !strings.Contains(rec.Body.String(), "pong") || got == 0 {
j, _ := json.Marshal(s.Trace(t.Context(), 0, 0))
route := string(j)
t.Errorf("stream=%s %d bytes again=%v: status %d: %.400s (claude runs %q, codex asked %d)\nroute %.3000s", stream, len(tc.text), tc.again, rec.Code, rec.Body.String(), b, got, route)
}
}
}