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.
73 lines
3.1 KiB
Go
73 lines
3.1 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 may 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. It isn't a limit:
|
|
// bodies of 0.5 to 20 MiB were answered from here (a2b96139c), and
|
|
// ScarfQian's (#1628) came back 200 on the ninth try after eight of these
|
|
// 503s through another tool. "remote connection failure" is Envoy failing
|
|
// to reach the backend, which comes and goes.
|
|
const codexDropBody = 1 << 20
|
|
|
|
// codexResetSaid is in what a ChatGPT account's reply says once
|
|
// codexDropped has read it as that 503 to a request this large: what the
|
|
// accounts loop tells it by, through its own error shape, to ask again
|
|
// without resting anybody.
|
|
const codexResetSaid = "This usually passes, so magpie asked again without resting the account."
|
|
|
|
// codexDropped is the reply, still the 503 it is, that a ChatGPT Codex
|
|
// backend gave a request of size bytes when it closed the connection
|
|
// before answering one this large. It says what happened and, should it
|
|
// keep happening, what makes the request smaller. It stays a 503 so
|
|
// magpie and then Codex ask again: told 413, Codex never does, and the
|
|
// next turn carries the same images, so the conversation was stuck on a
|
|
// drop the next try may not get (#1628). A smaller request's reset is left
|
|
// as it came.
|
|
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 closed the connection before answering this %.1f MiB request (503: %s). "+
|
|
codexResetSaid+" "+
|
|
"If it keeps happening in this conversation, its images may be why: run /compact, or remove or shrink the images (a JPEG in place of a large PNG screenshot).",
|
|
float64(size)/(1<<20), bytes.TrimSpace(head))
|
|
b, _ := json.Marshal(map[string]any{"error": map[string]any{"message": msg, "type": "server_error"}})
|
|
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.Header, out.Body, out.ContentLength = h, io.NopCloser(bytes.NewReader(b)), int64(len(b))
|
|
return &out
|
|
}
|
|
|
|
// codexReset says a failure is a large request's reset codexDropped read:
|
|
// no account's fault, and asked again.
|
|
func codexReset(status int, body []byte) bool {
|
|
return status == http.StatusServiceUnavailable && bytes.Contains(body, []byte(codexResetSaid))
|
|
}
|