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.
132 lines
4.8 KiB
Go
132 lines
4.8 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): 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, as ylorn's was, and a smaller one is served.
|
|
// It notes the account asked each time.
|
|
func dropsBig(t *testing.T, tried *[]string) {
|
|
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 {
|
|
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)
|
|
}
|
|
|
|
// With two ChatGPT accounts on, a turn the backend drops for its size is
|
|
// asked of one account once: the other would be dropped the same, a retry
|
|
// sends the same bytes again, and neither account is at fault, so nobody
|
|
// rests. The agent is told the request is too large, at a status it
|
|
// doesn't retry, and what to do about it. A small turn after it goes to
|
|
// the first account as before.
|
|
func TestCodexDroppedBigBodyIsTheRequests(t *testing.T) {
|
|
codexSignedIn(t, "spare@example.com")
|
|
var tried []string
|
|
dropsBig(t, &tried)
|
|
|
|
code, body := codexPost(t, screenshotTurn(t, 1200<<10))
|
|
if code != http.StatusRequestEntityTooLarge {
|
|
t.Fatalf("status %d, want 413: %s", code, body)
|
|
}
|
|
for _, want := range []string{"MiB", "compact", "image"} {
|
|
if !strings.Contains(body, want) {
|
|
t.Errorf("the agent isn't told %q: %s", want, body)
|
|
}
|
|
}
|
|
if strings.Join(tried, ",") != "acct-1" {
|
|
t.Errorf("asked %v, want acct-1 once", tried)
|
|
}
|
|
if r := restingNow(t); len(r) != 0 {
|
|
t.Errorf("rest over the request's size: %v", r)
|
|
}
|
|
|
|
tried = nil
|
|
code, body = codexPost(t, `{"model":"gpt-5.5","stream":true,"input":"ping"}`)
|
|
if code != 200 || !strings.Contains(body, "pong") || strings.Join(tried, ",") != "acct-1" {
|
|
t.Fatalf("small turn after: %d %s tried %v", code, body, tried)
|
|
}
|
|
}
|
|
|
|
// With Codex's own sign-in alone, relayed as it came: Codex is answered
|
|
// the same 413 and words, not the 503 it would send the same bytes again
|
|
// for, five times over.
|
|
func TestCodexDroppedBigBodyRelayed(t *testing.T) {
|
|
codexSignedIn(t)
|
|
var tried []string
|
|
dropsBig(t, &tried)
|
|
|
|
code, body := codexPost(t, screenshotTurn(t, 1200<<10))
|
|
if code != http.StatusRequestEntityTooLarge || !strings.Contains(body, "compact") {
|
|
t.Fatalf("status %d: %s", code, body)
|
|
}
|
|
if strings.Join(tried, ",") != "acct-1" {
|
|
t.Errorf("asked %v, want acct-1 once", 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)
|
|
}
|
|
}
|