Files
magpie/internal/gateway/qoder_busy_test.go
T
yetone 5b87087423 gateway: Qoder's queue (code 10605) is a 503 with Retry-After for users still on the built-in, not "sign in again" (plugins #98)
What changed: qoderFailure (internal/gateway/qoder.go) read every 401/403
as "Qoder: the sign-in lapsed — sign in again". It now reads Qoder's own
code first, whatever the HTTP status, the way Qoder.app's client does
(the QoderErrorCode table in its qoder-agent-sdk worker,
qoder-worker-runtime.obf.mjs) and as @magpie-community/opencode-qoder-auth
0.2.14 does (plugins f573cfd):
- 10605, or a queue state (in "queue" or in the message as JSON) with
  isQueued true or serviceAvailable false: 503 "Qoder is busy for
  <model> (this plan's queue: <tier>); try again in <n>s", with
  Retry-After from retryAfterSeconds rounded up (30s when absent).
- 110, 112-119, 122 (usage or credits used up): 429.
- 406, 416, 430, 80411, 80412, 100400, 100401, 100403 (refusals of the
  request; Qoder's table has no 100402): 400 with Qoder's words.
- 105 or no code on a 401/403: the 401 "sign in again" as before.
Both the HTTP status path (askQoder) and the in-stream statusCodeValue
path (decodeQoder) go through it. Event gets a Wait field for a KError,
and relayStatus's fail and serveQoder's first-round failure send it as
Retry-After (retryAfterOn); askQoderWait is askQoder that also returns
the wait, and askQoder (the round searchRounds uses) wraps it. Zed and
Command Code share relayStatus and never set Wait, so they are
unchanged.

Why: a real Qoder trial account had every chat answered
403 {"code":"10605",...} (Qoder's model queue) and was told to sign in
again, which can't help. The plugin was fixed in 0.2.14 for moved users;
this is the same fix for users still signed in through the built-in. The
built-in never marked an account lapsed from a refused chat (only from a
refused refresh, provider/qoder.go), and now a 10605 doesn't even return
the 401: the gateway's busy() reads the 503 as busy (failOther), so a
group asks it again after the pause, no shorter than its Retry-After
(noteBusy/retryAfter), and the agent gets the Retry-After too.

Run:
- New tests in internal/gateway/qoder_busy_test.go, the fixture
  testdata/qoder_queued_10605.json taken byte for byte from plugins #98:
  TestQoderQueuedIsBusy (serveQoder, HTTP 403 and in-stream 403, stream
  and not: 503, Retry-After 30, the busy words, no "sign in", busy() true,
  retryAfter 30s), TestQoderQueueWait, TestQoderErrorCodes. With the old
  401/403 rule put back at the top of qoderFailure all three FAIL
  ("got 401 ... want 503"); with retryAfterOn made a no-op
  TestQoderQueuedIsBusy FAILs on Retry-After "" in both subtests. Restored,
  they pass.
- go test -tags nogui ./internal/provider ok (458s). go test -tags nogui
  -timeout 40m ./internal/gateway (the default 10m ran out under the
  machine's load): every test passed except TestCodexHeardWithinItsIdleTimeout,
  which failed with the body #1800 already describes (Codex got member
  a's server_is_overloaded under load); it passes 10 of 10 alone
  (-count=10) and is on the Codex passthrough path, which this doesn't
  touch. Noted on #1800.
- gofmt -l clean; go vet ./..., GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./... ok; go build, GOOS=linux go build -tags nogui,
  GOOS=windows go build ok.
- Not run: the rest of go test ./... (only the two packages touched);
  a real 10605 through the built-in (the queue had reopened when the
  plugin shipped); no Qoder parity test exists in
  internal/gateway/plugin_*_test.go to extend.
2026-10-12 23:06:24 +08:00

172 lines
7.1 KiB
Go

package gateway
import (
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"time"
"github.com/tidwall/gjson"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/qoder"
)
// plugins #98: Qoder's queue refusing a chat (code 10605) is Qoder being
// busy for the plan, not a lapsed sign-in. The body is the one Qoder gave a
// real trial account on 2026-10-12, byte for byte from the issue
// (testdata/qoder_queued_10605.json). The other codes are read as Qoder's
// own client reads them (QoderErrorCode in Qoder.app's qoder-agent-sdk
// worker), as the plugin reads them since 0.2.14: only 105 is loginExpired.
func qoderQueuedBody(t *testing.T) string {
t.Helper()
b, err := os.ReadFile("testdata/qoder_queued_10605.json")
if err != nil {
t.Fatal(err)
}
return strings.TrimSpace(string(b))
}
// qoderRefusing stands in for api3.qoder.sh answering every chat with
// status and body, as qoderUpstream does with a stream.
func qoderRefusing(t *testing.T, status int, body string) {
t.Helper()
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
io.Copy(io.Discard, r.Body)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
io.WriteString(w, body)
}))
auth, url, model := qoderAuth, qoderURL, qoderModel
qoderAuth = func(context.Context, string, string) (*qoder.Credential, error) {
return &qoder.Credential{UID: "uid-test", Token: "jt-test", MachineID: "machine-test"}, nil
}
qoderModel = func(context.Context, string, string, string) (qoder.ModelInfo, error) { return testQoderModel(t), nil }
qoderURL = func(*qoder.Credential) string { return up.URL }
t.Cleanup(func() { up.Close(); qoderAuth, qoderURL, qoderModel = auth, url, model })
}
const qoderBusyWant = "Qoder is busy for efficient (this plan's queue: p3); try again in 30s"
// serveQoderOnce sends one chat through serveQoder, as the gateway does.
func serveQoderOnce(t *testing.T, stream bool) *httptest.ResponseRecorder {
t.Helper()
body := `{"model":"qfmodel","stream":` + map[bool]string{true: "true", false: "false"}[stream] + `,"messages":[{"role":"user","content":"hi"}]}`
w := httptest.NewRecorder()
r := httptest.NewRequest("POST", "/", strings.NewReader(body))
var u Usage
s := New()
s.serveQoder(w, r, provider.Chat, provider.Provider{Account: &provider.Account{Agent: "qoder", User: "one@x"}}, "qfmodel", []byte(body), &u)
return w
}
// checkQoderBusy: a 503 with Qoder's own wait as Retry-After, worded as
// busy, never "sign in again" — and the gateway's busy handling takes it
// as busy (asked again after the pause) and waits its Retry-After.
func checkQoderBusy(t *testing.T, w *httptest.ResponseRecorder) {
t.Helper()
msg := gjson.Get(w.Body.String(), "error.message").String()
if w.Code != 503 || w.Header().Get("Retry-After") != "30" || msg != qoderBusyWant {
t.Fatalf("got %d Retry-After %q %q, want 503 Retry-After 30 %q", w.Code, w.Header().Get("Retry-After"), msg, qoderBusyWant)
}
if strings.Contains(strings.ToLower(w.Body.String()), "sign in") {
t.Fatalf("a queued request asks to sign in again: %s", w.Body.String())
}
if !busy(w.Code, w.Body.Bytes()) {
t.Fatalf("the gateway doesn't take it as busy (failure %q): %s", failure(w.Code, w.Body.Bytes()), w.Body.String())
}
if d := retryAfter(w.Header(), time.Now()); d != 30*time.Second {
t.Fatalf("the gateway waits %s, want Qoder's 30s", d)
}
}
func TestQoderQueuedIsBusy(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
queued := qoderQueuedBody(t)
t.Run("http 403", func(t *testing.T) {
qoderRefusing(t, 403, queued)
for _, stream := range []bool{false, true} {
checkQoderBusy(t, serveQoderOnce(t, stream))
}
})
t.Run("in the stream before any answer", func(t *testing.T) {
env, _ := json.Marshal(map[string]any{"headers": map[string]any{}, "body": queued, "statusCodeValue": 403, "statusCode": "FORBIDDEN"})
done := qoderUpstream(t, []string{string(env)})
defer done()
for _, stream := range []bool{false, true} {
checkQoderBusy(t, serveQoderOnce(t, stream))
}
})
}
// The queue's own wait is used, rounded up; a queued request says so; with
// no wait it is 30s; and a queue state under another status is the same.
func TestQoderQueueWait(t *testing.T) {
q := func(status int, code string, queue map[string]any) (int, string, time.Duration) {
m, _ := json.Marshal(queue)
b, _ := json.Marshal(map[string]any{"code": code, "message": string(m)})
return qoderFailure(status, b)
}
for _, tt := range []struct {
name string
status int
code string
queue map[string]any
want string
wait time.Duration
}{
{"queued", 403, "10605", map[string]any{"isQueued": true, "modelKey": "ultimate", "retryAfterSeconds": 12.2}, "Qoder queued the request for ultimate; try again in 13s", 13 * time.Second},
{"no wait", 403, "10605", map[string]any{"serviceAvailable": false}, "Qoder is busy; try again in 30s", 30 * time.Second},
{"queue state on a 429, no code", 429, "", map[string]any{"serviceAvailable": false, "retryAfterSeconds": 5}, "Qoder is busy; try again in 5s", 5 * time.Second},
} {
status, msg, wait := q(tt.status, tt.code, tt.queue)
if status != 503 || msg != tt.want || wait != tt.wait {
t.Errorf("%s: %d %q %s, want 503 %q %s", tt.name, status, msg, wait, tt.want, tt.wait)
}
}
// the code alone, the message plain text; a numeric code too
for _, b := range []string{`{"code":"10605","message":"busy"}`, `{"code":10605,"message":"busy"}`} {
if status, _, wait := qoderFailure(403, []byte(b)); status != 503 || wait != 30*time.Second {
t.Errorf("%s: %d %s, want 503 30s", b, status, wait)
}
}
}
// Qoder's other codes on a 401/403 aren't a lapsed sign-in either; 105
// and no code are.
func TestQoderErrorCodes(t *testing.T) {
f := func(status int, code, message string) (int, string, time.Duration) {
b, _ := json.Marshal(map[string]any{"code": code, "message": message})
return qoderFailure(status, b)
}
for _, status := range []int{401, 403} {
for _, code := range []string{"110", "112", "113", "114", "115", "116", "117", "118", "119", "122"} {
if got, msg, _ := f(status, code, "credits used up"); got != 429 || msg != "usage limit reached: credits used up" {
t.Errorf("%d code %s: %d %q, want 429", status, code, got, msg)
}
}
for _, code := range []string{"406", "416", "430", "80411", "80412", "100400", "100401", "100403"} {
if got, msg, _ := f(status, code, "no"); got != 400 || msg != "Qoder refused the request ("+code+"): no" {
t.Errorf("%d code %s: %d %q, want 400", status, code, got, msg)
}
}
for _, b := range []string{`{"code":"105","message":"login expired"}`, `{"message":"forbidden"}`, ``} {
if got, msg, wait := qoderFailure(status, []byte(b)); got != 401 || msg != "Qoder: the sign-in lapsed — sign in again" || wait != 0 {
t.Errorf("%d %s: %d %q %s, want the 401", status, b, got, msg, wait)
}
}
}
// a quota without a code reads as before
if got, _, _ := qoderFailure(429, []byte(`{"message":"额度已耗尽"}`)); got != 429 {
t.Errorf("a 429: %d", got)
}
}