Files
magpie/internal/gateway/codex_dropped_test.go
T
yetone 8029d98b7e gateway: a large Codex request ChatGPT closes on is asked again, not told 413 (#1628, ScarfQian)
ScarfQian (#1628, v0.1.1184, Codex desktop computer use on a ChatGPT
subscription): a 1.1 MiB turn answered with ChatGPT's 503 "upstream
connect error or disconnect/reset before headers. reset reason: remote
connection failure" came back as a2b96139c's 413 "dropped this 1.1 MiB
request ... magpie didn't retry it". Codex doesn't retry a 4xx, and every
later turn carries the same screenshots, so each one got one try and the
same 413 and the conversation was stuck.

a2b96139c took that 503 as deterministic for bodies of 1 MiB or more from
one report (ylorn: every turn past ~1 MiB, also without magpie). The
evidence says it isn't the request's: from here a Pro account answered
bodies of 0.5 to 20 MiB (a2b96139c's own body); Codex's own client sends
>1 MiB computer-use turns to the same endpoint; ScarfQian's other tool got
a 200 on the 9th try after 8 of these 503s; and "remote connection
failure" is Envoy failing to reach the backend, not a size check. So it is
transient, on some networks/times more than others. Nothing was remembered
per session or size: the "session keeps failing" was the history carrying
the images into every next turn, each refused 413 at once.

Now codexDropped leaves the 503 a 503, with a message that says the
backend closed the connection on this N MiB request, that magpie asked
again without resting the account, and, if it keeps happening, /compact or
shrink the images. In the accounts loop a large request's reset moves to
the next account without resting this one (ylorn's acct-1 rest stays
fixed); the last one left is asked again as for any 503 (lastRetries). On
the passthrough (Codex's own sign-in alone) it is asked again up to
lastRetries times after retryPause<<n. Codex then asks again itself for
the 503. A small request's reset is unchanged.

Tests (codex_dropped_test.go, a backend answering the literal 503 to
bodies over 1 MiB the first N times):
- TestCodexResetBigBodyAskedAgain: two accounts, closed on twice then
  served: 200, asked acct-1,acct-2,acct-2, nobody rests. Old code: 413
  after one try.
- TestCodexResetBigBodyStays503: closed on always: 503 with the advice,
  asked acct-1 then acct-2 3x, nobody rests; the next turn as large goes to
  acct-1 and is answered once the backend takes it. Old code: 413.
- TestCodexResetBigBodyRelayed: sign-in alone, closed once: 200 on the
  2nd try; always: 503 after 3 tries. Old code: 413 after one.
Broken by hand: old code -> all three red; the no-rest branch off -> acct-1
rests (and the next turn goes to acct-2); the passthrough retry off ->
Relayed red.

Run: go test -tags nogui ./internal/gateway, gofmt -l, go vet -tags nogui
with GOOS=linux and windows on ./internal/gateway. Not tried: the
reporter's network or a real ChatGPT reset (none reproduces on demand);
what Codex desktop shows after its own retries of the 503.
2026-10-11 22:54:49 +08:00

167 lines
6.2 KiB
Go

package gateway
import (
"crypto/rand"
"encoding/base64"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// edgeDropped is what ylorn's ChatGPT backend answered to a request whose
// screenshot took it past about 1 MiB (Discord, v0.1.1179), and
// ScarfQian's to a 1.1 MiB computer-use turn (#1628): Envoy's own 503
// behind Cloudflare, as text, before any reply.
const edgeDropped = "upstream connect error or disconnect/reset before headers. reset reason: remote connection failure"
// dropsBig stands in for that ChatGPT backend: a body past 1 MiB is
// answered with edgeDropped the first *drops times (every time while it
// is below zero) and served after; a smaller one is served. It notes the
// account asked each time.
func dropsBig(t *testing.T, tried *[]string, drops *int) {
t.Helper()
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, _ := io.ReadAll(r.Body)
*tried = append(*tried, r.Header.Get("chatgpt-account-id"))
if len(b) > 1<<20 && *drops != 0 {
if *drops > 0 {
*drops--
}
w.Header().Set("Content-Type", "text/plain")
w.Header().Set("Server", "cloudflare")
w.WriteHeader(http.StatusServiceUnavailable)
io.WriteString(w, edgeDropped)
return
}
io.WriteString(w, sse(
`data: {"type":"response.created","response":{"id":"r1","model":"gpt-5.5"}}`,
`data: {"type":"response.output_text.delta","delta":"pong"}`,
`data: {"type":"response.completed","response":{"id":"r1","usage":{"input_tokens":7,"output_tokens":1}}}`))
}))
t.Cleanup(up.Close)
was := provider.CodexBase
provider.CodexBase = up.URL + "/backend-api/codex"
t.Cleanup(func() { provider.CodexBase = was })
}
// screenshotTurn is a Responses turn with a PNG screenshot of about n
// bytes inline, base64 as an agent sends it. Its bytes aren't an image
// any decoder reads, so nothing on the way makes it smaller.
func screenshotTurn(t *testing.T, n int) string {
t.Helper()
raw := make([]byte, n*3/4)
rand.Read(raw)
b, _ := json.Marshal(map[string]any{"model": "gpt-5.5", "stream": true, "input": []any{
map[string]any{"type": "message", "role": "user", "content": []any{
map[string]any{"type": "input_text", "text": "what does this screenshot show?"},
map[string]any{"type": "input_image", "image_url": "data:image/png;base64," + base64.StdEncoding.EncodeToString(raw)},
}}}})
return string(b)
}
// ScarfQian (#1628): with two ChatGPT accounts on, a 1.2 MiB turn the
// backend closes on twice, then answers, is answered: asked of acct-1,
// then acct-2, then acct-2 again, and nobody rests, since no account is at
// fault for it. On a2b96139c's code the agent got a 413 after one try,
// which Codex never asks again.
func TestCodexResetBigBodyAskedAgain(t *testing.T) {
codexSignedIn(t, "spare@example.com")
var tried []string
drops := 2
dropsBig(t, &tried, &drops)
code, body := codexPost(t, screenshotTurn(t, 1200<<10))
if code != 200 || !strings.Contains(body, "pong") {
t.Fatalf("status %d, want 200 pong: %s", code, body)
}
if strings.Join(tried, ",") != "acct-1,acct-2,acct-2" {
t.Errorf("asked %v, want acct-1,acct-2,acct-2", tried)
}
if r := restingNow(t); len(r) != 0 {
t.Errorf("rest over a reset of a large request: %v", r)
}
}
// Closed on every time, the turn is asked of each account and the last
// again, as for a 503, and the agent is told the 503 — which Codex asks
// again for — with what may help should it keep happening. Nobody rests,
// and nothing is remembered of it: the next turn, as large, goes to acct-1
// and is answered once the backend takes it.
func TestCodexResetBigBodyStays503(t *testing.T) {
codexSignedIn(t, "spare@example.com")
var tried []string
drops := -1
dropsBig(t, &tried, &drops)
code, body := codexPost(t, screenshotTurn(t, 1200<<10))
if code != http.StatusServiceUnavailable {
t.Fatalf("status %d, want 503: %s", code, body)
}
for _, want := range []string{"MiB", "compact", "image", "reset reason"} {
if !strings.Contains(body, want) {
t.Errorf("the agent isn't told %q: %s", want, body)
}
}
if strings.Join(tried, ",") != "acct-1,acct-2,acct-2,acct-2" {
t.Errorf("asked %v, want acct-1 then acct-2 three times", tried)
}
if r := restingNow(t); len(r) != 0 {
t.Errorf("rest over a reset of a large request: %v", r)
}
tried, drops = nil, 0
code, body = codexPost(t, screenshotTurn(t, 1200<<10))
if code != 200 || !strings.Contains(body, "pong") || strings.Join(tried, ",") != "acct-1" {
t.Fatalf("large turn after: %d %s tried %v", code, body, tried)
}
}
// With Codex's own sign-in alone, relayed as it came: a large request
// closed on once is asked again and answered; closed on every time, Codex
// is answered the 503 after three tries, with what may help.
func TestCodexResetBigBodyRelayed(t *testing.T) {
codexSignedIn(t)
var tried []string
drops := 1
dropsBig(t, &tried, &drops)
code, body := codexPost(t, screenshotTurn(t, 1200<<10))
if code != 200 || !strings.Contains(body, "pong") || len(tried) != 2 {
t.Fatalf("closed on once: %d %s tried %v", code, body, tried)
}
tried, drops = nil, -1
code, body = codexPost(t, screenshotTurn(t, 1200<<10))
if code != http.StatusServiceUnavailable || !strings.Contains(body, "compact") || !strings.Contains(body, "reset reason") {
t.Fatalf("status %d: %s", code, body)
}
if len(tried) != 3 {
t.Errorf("asked %d times, want 3", len(tried))
}
}
// The same 503 to a small request is the backend's trouble, as it is in
// an outage: it is relayed as the 503 it is, for the client to try again.
func TestCodexEdgeResetSmallBodyStays503(t *testing.T) {
codexSignedIn(t)
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusServiceUnavailable)
io.WriteString(w, edgeDropped)
}))
t.Cleanup(up.Close)
was := provider.CodexBase
provider.CodexBase = up.URL + "/backend-api/codex"
t.Cleanup(func() { provider.CodexBase = was })
code, body := codexPost(t, `{"model":"gpt-5.5","stream":true,"input":"ping"}`)
if code != http.StatusServiceUnavailable || !strings.Contains(body, "reset reason") {
t.Fatalf("status %d: %s", code, body)
}
}