Files
magpie/internal/gateway/codex_overload_shape_test.go
T
yetone b58669eb09 gateway: an overload told in Codex's stream says server_is_overloaded (wjh66)
wjh66 on Discord (Windows, v0.1.2710, Codex on the ChatGPT sign-in,
gpt-6.1-sol): every turn showed "reconnecting 5/5", and app.log had the
backend's "Our servers are currently overloaded" again and again.

With one ChatGPT account, magpie asks an overloaded turn again
(lastRetries) and keeps Codex alive past keepHeldAfter with the stream's
200 and comments. When the retries ran out, the backend's busy status
(529, or a 503 saying overloaded) went to Codex in that stream as
response.failed with code server_error. Codex reads server_error as
Retryable (codex-rs codex-api sse/responses_error.rs) and sent the turn
five more times, each one asked three times by magpie: 18 asks of an
overloaded model, then the last error alone. The backend itself says
an overload in a stream as server_is_overloaded (#1418), which Codex
reads as ServerOverloaded: "Selected model is at capacity. Please try a
different model.", without the reconnects.

streamError now gives a Responses client server_is_overloaded when the
status is 529, or a 5xx whose words say overloaded (saidOverloaded).
Other failures keep server_error, which Codex retries. A routing group
or the codex subscription's fallbacks still take an overload before any
of this, as before.

This does not explain the earlier "error decoding response body".
Codex says that only when the body of a 2xx fails to read. magpie's
streams read clean over real TCP in every overload shape tried (see
07f4d46c5, which logs a reply cut early).

Ran on mote at this change rebased on 1cb4d4301: gofmt; go vet
darwin, GOOS=linux -tags nogui, GOOS=windows; go test -count=1
-timeout 45m ./internal/gateway: ok but for TestCodexPoolSaysTheClientsVersion,
the known #4807 (the machine's own Codex 0.165.0 identity reaches it).
At the pushed head (rebased on f95bacdfb, whose new commits add
hub_mcp.go to the gateway, nothing of streamError): go vet darwin,
linux, windows, and the overload, StreamError and Hub tests: ok. On Windows (win-run):
TestCodexKeptAliveOverload*, TestStreamErrorOverloadOnly,
TestCodexOneAccount*, TestCodexOwnModel*, *Overload*, *StreamError*,
TestCodexReplyBrokenOffLogged, TestCodexClientGoneLogged: ok, 50 tests.

Broken by hand: with the else-if turned off,
TestCodexKeptAliveOverloadSaysServerIsOverloaded is red in both
subtests (503 message, 529): code "server_error".

Not tried: a real ChatGPT account or the real Codex client against
this build. Whether gpt-6.1-sol is busy for this account only, or the
same request without magpie. The Windows system proxy between Codex
and 127.0.0.1 as the cause of the decode error.
2026-11-02 18:50:12 +08:00

78 lines
2.6 KiB
Go

package gateway
import (
"encoding/json"
"strings"
"testing"
"time"
)
// The ChatGPT backend too busy for a turn, said as a status, once Codex
// has been kept alive with the stream's headers, reaches it as the
// backend says an overload in a stream: response.failed with
// server_is_overloaded, which Codex shows as the model at capacity. As
// server_error, Codex took it for a dropped stream and sent the turn
// five more times ("reconnecting 5/5", wjh66 on Discord).
func TestCodexKeptAliveOverloadSaysServerIsOverloaded(t *testing.T) {
for name, failed := range map[string]reply{
"503 message": {503, "application/json", `{"message":"Our servers are currently overloaded. Please try again later."}`},
"529": {529, "application/json", overloadedStatus},
} {
t.Run(name, func(t *testing.T) {
k, w := keepHeldAfter, watchEvery
keepHeldAfter, watchEvery = time.Millisecond, time.Millisecond
t.Cleanup(func() { keepHeldAfter, watchEvery = k, w })
o := &overloadedBackend{replies: []reply{failed}}
oneCodexAccount(t, o)
retryPause = 20 * time.Millisecond // past keepHeldAfter: Codex is sent the headers
code, body := codexPost(t, oneAccountTurn)
if code != 200 || o.asked() != 1+lastRetries {
t.Fatalf("%d %q, asked %d times", code, body, o.asked())
}
var got string
for _, l := range strings.Split(body, "\n") {
data, ok := strings.CutPrefix(l, "data: ")
if !ok {
continue
}
var ev struct {
Type string `json:"type"`
Response struct {
Error struct{ Code, Message string } `json:"error"`
} `json:"response"`
}
if json.Unmarshal([]byte(data), &ev) == nil && ev.Type == "response.failed" {
got = ev.Response.Error.Code
if !strings.Contains(ev.Response.Error.Message, "overloaded") {
t.Errorf("message %q", ev.Response.Error.Message)
}
}
}
if got != "server_is_overloaded" {
t.Fatalf("response.failed code %q, want server_is_overloaded:\n%s", got, body)
}
})
}
}
// Only an overload is said as one: another failure told in a stream
// keeps server_error, which Codex retries.
func TestStreamErrorOverloadOnly(t *testing.T) {
for _, c := range []struct {
status int
msg string
want bool
}{
{529, "busy", true},
{503, "Our servers are currently overloaded.", true},
{503, "Service Unavailable", false},
{502, "upstream connect error", false},
{429, "overloaded with requests", false},
{400, "overloaded", false},
} {
if got := saidOverloaded(c.status, c.msg); got != c.want {
t.Errorf("%d %q: %v, want %v", c.status, c.msg, got, c.want)
}
}
}