Files
magpie/internal/gateway/codex_dropped.go
T
yetone a2b96139c9 gateway: a Codex request ChatGPT drops for its size is told as too large, not retried or rested (ylorn on Discord)
ylorn (Discord #general, v0.1.1179): through a ChatGPT account, every
turn a PNG screenshot took past about 1 MiB was answered

  HTTP/2 503  content-type: text/plain  server: cloudflare
  upstream connect error or disconnect/reset before headers. reset reason: remote connection failure

also without magpie, and the same screenshot as a JPEG went through.
magpie took that 503 as the backend's trouble: with two accounts on, the
turn was asked of acct-1, then of acct-2 three more times, acct-1 rested
for it, and the next turn moved to acct-2. The same bytes are dropped
the same way on every account, so none of that could help, and the
agent was handed a 503 it retries again with the same body.

codexDropped (codex_dropped.go) makes a 503 whose body says "upstream
connect error or disconnect/reset before headers", to a request of
1 MiB or more, the 413 it stands for, with a message saying the request
was dropped for its size (in MiB), that magpie didn't retry it or rest
the account, and to /compact or remove or shrink the images (a JPEG in
place of a large PNG screenshot). It runs on a ChatGPT account's replies
in forwardOnce (the accounts loop, any agent) and on the ChatGPT
passthrough in codexUpstreamOn (Codex's own sign-in alone). A 413
already skips the other accounts of the same provider and rests nobody,
and Codex doesn't retry a 4xx. The same 503 to a smaller request stays
the 503 it is, retried as before.

1 MiB isn't the backend's limit for everyone: from here, on a Pro
account, gpt-6-astra direct to chatgpt.com/backend-api/codex/responses
answered 200 to bodies of 0.5 to 20 MiB, plain and zstd, and to
3456x2234 PNG screenshots up to 5.6 MiB. So nothing is refused before
sending; only this drop, on a body this large, is taken as the
request's.

Tests (codex_dropped_test.go), with an httptest backend that answers
ylorn's literal 503 (text/plain, Server: cloudflare) to bodies over
1 MiB:
- TestCodexDroppedBigBodyIsTheRequests: two accounts, a 1.2 MiB turn is
  413 with the message, asked of acct-1 once, nobody rests, and the next
  small turn goes to acct-1. On the old code: 503, asked
  [acct-1 acct-2 acct-2 acct-2], acct-1 rested, next turn on acct-2.
- TestCodexDroppedBigBodyRelayed: Codex's sign-in alone, 413 with the
  message (old code: the 503 relayed).
- TestCodexEdgeResetSmallBodyStays503: a small body's 503 is relayed.

Run: go test -tags nogui ./internal/gateway (new tests red before, green
after), gofmt -l, go vet -tags nogui with GOOS=darwin, linux and windows
on ./internal/gateway. Not tried: ylorn's own network or account, where
the drop happens.
2026-10-11 14:32:57 +08:00

58 lines
2.4 KiB
Go

package gateway
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"regexp"
)
// edgeReset is the 503 ChatGPT's front (Cloudflare, then Envoy) answers,
// as text, when the backend behind it closed the connection before any
// reply.
var edgeReset = regexp.MustCompile(`(?i)upstream connect error or disconnect/reset before headers`)
// codexDropBody is the request size from which that 503 is taken to be
// about the request. ylorn (Discord, v0.1.1179) got it for every Codex
// turn a PNG screenshot took past about 1 MiB, also without magpie, and
// never once the same image was a smaller JPEG. It isn't the backend's
// limit for everyone: bodies of 1 to 20 MiB were answered from here.
const codexDropBody = 1 << 20
// codexDropped is the 413 a ChatGPT Codex reply stands for when the
// backend dropped a request of size bytes this large. Sent again — to
// the same account or another one — the same bytes are dropped the same
// way, so it isn't a 503 to retry, and no account is at fault for it. A
// smaller request dropped the same way is the backend's own trouble and
// stays the 503 it is.
func codexDropped(res *http.Response, size int) *http.Response {
if res == nil || res.StatusCode != http.StatusServiceUnavailable || size < codexDropBody {
return res
}
head, _ := io.ReadAll(io.LimitReader(res.Body, 4<<10))
if !edgeReset.Match(head) {
res.Body = struct {
io.Reader
io.Closer
}{io.MultiReader(bytes.NewReader(head), res.Body), res.Body}
return res
}
res.Body.Close()
msg := fmt.Sprintf("ChatGPT's Codex backend dropped this %.1f MiB request before answering (503: %s). "+
"It does this to requests this large, most often from images, on any account, so magpie didn't retry it or rest the account. "+
"Run /compact, or remove or shrink the images (a JPEG in place of a large PNG screenshot), then try again.",
float64(size)/(1<<20), bytes.TrimSpace(head))
b, _ := json.Marshal(map[string]any{"error": map[string]any{"message": msg, "type": "request_too_large", "code": "request_too_large"}})
h := res.Header.Clone()
for _, k := range []string{"Content-Type", "Content-Length", "Content-Encoding", "Transfer-Encoding"} {
h.Del(k)
}
h.Set("Content-Type", "application/json")
out := *res
out.StatusCode, out.Status = http.StatusRequestEntityTooLarge, "413 Request Entity Too Large"
out.Header, out.Body, out.ContentLength = h, io.NopCloser(bytes.NewReader(b)), int64(len(b))
return &out
}