Files
magpie/internal/gateway/pool_model_refusal_test.go
T
yetone 25c36e862a gateway: a relay's pooled refusal of one model rests that model, not the key (Jray05343207 on X)
Jray05343207 (Windows) connected Codex through magpie and got
"auth_unavailable: no auth available (providers=codex, model=gpt-6-sol;
last upstream error: model_not_found: The 'gpt-6-sol' model is not
supported when using Codex with a ChatGPT account." That text is not
magpie's: it is CLIProxyAPI's (sdk/api/handlers/handlers_errors.go,
enrichAuthSelectionError), a relay pooling ChatGPT accounts. It lists
gpt-6-sol whatever its accounts' plans serve, and once every account
refused it answers 503 with the last refusal in its words.

modelRefused read only 400/403/404/422, so the 503 was a plain outage:
the whole key rested on the backoff ladder, every model on the relay
with it, and the model stayed offered. Now a 5xx whose body is that pool
answer ("no auth available (providers=…; last upstream error: …)",
poolLastError) is judged by the last upstream error's own words, as a
404 would be: a model refusal rests only that model on that key
(restID), and the key's other models are asked straight. A last error of
quota, or none named, reads as before. plainUnavailable leaves such a
refusal to modelRefused too. docs/subsystems/gateway-routing.md says so.

Run (WSL magpie-desktop, Linux; mote's sshd reset every connection):
- TestPoolModelRefusalRestsTheModelNotTheKey (two keys on a relay
  answering the reporter's exact body as 503) and TestPoolLastErrorRead:
  pass.
- Broken by hand (the poolLastError branch removed): both red; each key
  rested whole ("Why:other Status:503 By:backoff") and gpt-6-sol didn't
  rest on its own.
- TestModelNotInPlan*, TestFallback*, TestEscapedWords*: pass.
- go vet ./internal/gateway for linux, windows and darwin; gofmt -l clean.
- go test -count=1 -timeout 45m ./internal/gateway: ok (1025s, Linux)

Not tried: a real CLIProxyAPI with a plan lacking gpt-6-sol (fixture is
the reporter's literal text in CLIProxyAPI's body shape); the user's
Codex still gets the 503 if gpt-6-sol is the model it asks for and no
other provider serves it, so the reply asks them to pick another model.
2026-11-10 17:42:14 +08:00

110 lines
4.3 KiB
Go

package gateway
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
)
// cpaNoAuth is CLIProxyAPI's answer, word for word as Jray05343207 quoted
// it on X, once every ChatGPT account it pools for Codex refused
// gpt-6-sol: a 503 whose last upstream error is the model's refusal.
const cpaNoAuth = `{"error":{"message":"auth_unavailable: no auth available (providers=codex, model=gpt-6-sol; last upstream error: model_not_found: The 'gpt-6-sol' model is not supported when using Codex with a ChatGPT account.)","type":"server_error","code":"internal_server_error"}}`
// A relay pooling ChatGPT accounts (CLIProxyAPI) lists gpt-6-sol, which
// its accounts' plans don't serve, and answers it 503 auth_unavailable.
// Read as an outage, that rested the whole key with every model on it.
// Only the model rests: the key's other models are asked straight, and
// no key rests for it.
func TestPoolModelRefusalRestsTheModelNotTheKey(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
restingUntil.Lock()
restingUntil.m, restingUntil.note = map[string]time.Time{}, map[string]Rest{}
restingUntil.Unlock()
routed.Lock()
routed.failures = map[string]int{}
routed.Unlock()
var seen []string
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var req struct {
Model string `json:"model"`
}
json.NewDecoder(r.Body).Decode(&req)
key := strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer ")
seen = append(seen, key+":"+req.Model)
w.Header().Set("Content-Type", "application/json")
if req.Model == "gpt-6-sol" {
w.WriteHeader(http.StatusServiceUnavailable)
io.WriteString(w, cpaNoAuth)
return
}
io.WriteString(w, `{"id":"from-`+key+`","choices":[]}`)
}))
defer up.Close()
p := provider.Provider{ID: "cpa", Name: "cpa", Chat: up.URL + "/v1",
Models: []string{"gpt-6.1-terra", "gpt-6-sol"},
Key: "k-one", KeyName: "One", Keys: []provider.KeyAccount{{Name: "Two", Key: "k-two"}}}
if err := provider.Save(p); err != nil {
t.Fatal(err)
}
saved, err := provider.Find("cpa")
if err != nil {
t.Fatal(err)
}
s := New()
send := func(model string) (int, string) {
rec := httptest.NewRecorder()
body := `{"model":"cpa/` + model + `","messages":[{"role":"user","content":"hi"}]}`
s.Handler().ServeHTTP(rec, httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(body)))
return rec.Code, rec.Body.String()
}
for range 3 {
if code, body := send("gpt-6-sol"); code != http.StatusServiceUnavailable || !strings.Contains(body, "not supported when using Codex with a ChatGPT account") {
t.Fatalf("gpt-6-sol: %d %s", code, body)
}
}
for _, k := range saved.KeyList() {
if r, ok := RestOf(saved.RestKey(k)); ok {
t.Errorf("key %s rests for a model its accounts lack: %+v", k.Name, r)
}
if _, ok := RestOf(saved.RestKey(k) + "/gpt-6-sol"); !ok {
t.Errorf("key %s: gpt-6-sol doesn't rest", k.Name)
}
}
seen = nil
if code, body := send("gpt-6.1-terra"); code != 200 || !strings.Contains(body, "from-k-one") {
t.Fatalf("gpt-6.1-terra: %d %s", code, body)
}
if strings.Join(seen, ",") != "k-one:gpt-6.1-terra" {
t.Fatalf("gpt-6.1-terra tried %v; want the first key, straight", seen)
}
}
// The pool's words are read for what the last refusal said: a model's
// refusal is the model's, its quota or a plain outage is not.
func TestPoolLastErrorRead(t *testing.T) {
for _, tc := range []struct {
name string
status int
body string
want bool
}{
{"model refused", 503, cpaNoAuth, true},
{"model refused, 500", 500, cpaNoAuth, true},
{"quota", 503, `{"error":{"message":"auth_unavailable: no auth available (providers=codex, model=gpt-6-sol; last upstream error: usage_limit_reached: The usage limit has been reached)","type":"server_error"}}`, false},
{"none named", 503, `{"error":{"message":"auth_unavailable: no auth available (providers=codex, model=gpt-6-sol)","type":"server_error"}}`, false},
{"plain 503 naming a model", 503, `{"error":{"message":"model gpt-6-sol not supported right now, upstream down"}}`, false},
} {
if got := modelRefused(tc.status, []byte(tc.body)); got != tc.want {
t.Errorf("%s: modelRefused %v, want %v", tc.name, got, tc.want)
}
}
}