ylorn (X, v0.1.1202) sent a text-only gpt-6-luna turn of 1,100,224 bytes
straight to chatgpt.com/backend-api/codex/responses. Sent plain it got 503.
The same turn zstd-compressed (259 B) got 200. magpie decodes Codex's
inbound zstd and sent the backend plain JSON, so the turn got 503 through
magpie too.
What Codex does (openai/codex a9b08151, 2026-10-11):
- codex-rs/core/src/client.rs:1704-1712 responses_request_compression:
Zstd only with the enable_request_compression feature, a sign-in that
uses_codex_backend, and the OpenAI provider.
- client.rs:1780: used for the POST /responses stream only.
/responses/compact and /models go plain.
- codex-rs/features/src/lib.rs:1411-1416: the feature is Stable and on by
default.
- codex-rs/protocol/src/auth.rs:54-63: uses_codex_backend is true for the
ChatGPT sign-ins and false for ApiKey and Bedrock.
- codex-rs/codex-api/src/endpoint/responses.rs:130-147 sets it for
/responses.
- codex-rs/http-client/src/request.rs:196-217 compresses with
zstd encode_all at level 3, sets Content-Encoding: zstd, and never
compresses twice.
What changed:
- provider.CodexCompressed compresses a body sent to CodexBase/responses
with klauspost zstd, already a dependency. It uses SpeedDefault (about
level 3) and no frame checksum, like libzstd's default. It sets
Content-Encoding, Body, GetBody and ContentLength to the compressed
bytes, and returns the wire size.
- It is called once the body is final and signed: in forwardOnce for a
codex account, in codexUpstreamOn (the relay) on every try (so a try
again or a body without an unreadable image is compressed afresh), and
in warmCodex.
- Unchanged: /responses/compact, an sk- key's requests to api.openai.com,
every key provider (even one at the same URL), and a body already
encoded.
- The plain body is still what magpie keeps, masks, traces and shows.
- codexDropped (#1628, a2b96139c/8029d98b7) judges "large request" by the
bytes sent on the wire. A text turn that compresses to a few KB and is
reset is a small request's 503, not a "MiB" hint asked again. A
screenshot's random bytes stay past 1 MiB, and those are still asked
again.
- The gateway and provider TestMains leave turns plain
(CodexZstdForTests), since their fake backends read the body as JSON.
The new tests turn it on.
- TestAsksOwnBackendAgreesWithTheGatewayDispatch read any call whose sixth
letter is upper case (CodexCompressed) as a serveX dispatch. It now
needs the "serve" prefix. Removing "kiro" from asksOwnBackend still
turns it red.
- docs/subsystems/gateway-routing.md: the "sent plain JSON" line now names
this exception.
Tests: internal/gateway/codex_zstd_test.go covers the accounts loop, the
relay (inbound zstd and plain), a try again after a drop, compact and an
API key plain, a key provider at the same URL plain, and a reset judged by
wire bytes. internal/provider/codex_zstd_test.go covers the round trip,
GetBody, Content-Length and the URLs left alone. Broken by hand, each went
red:
- no compression in forwardOnce: Account and AskedAgain
- no compression in the relay: Relayed, AskedAgain and JudgedByBytes
- no account guard: KeyProviderTurnStaysPlain
- compact compressed: CompactAndAPIKeyStayPlain
- the drop judged by len(body): JudgedByTheBytesSent
Real account (the owner's ChatGPT sign-in, gpt-6-luna, 1,100,279 B
text-only body, 391 B zstd):
- Direct to chatgpt.com, twice: plain 200/200, zstd 200/200. ylorn's 503
did NOT reproduce from here (a2b96139c also saw 0.5-20 MiB served). The
change rests on the official client's behaviour.
- Through a sandbox magpie (own HOME, :3486) via a byte-counting CONNECT
proxy:
- Old build: relay-zstd, relay plain and the account path were 200, 200,
200, with 3,321,579 B uploaded to chatgpt.com.
- New build: 200, 200, 200, with 13,362 B uploaded. Each 1.1 MB turn now
goes as a few KB, as Codex sends it.
Run at origin/main e21ea07e4:
- go test ./... was all ok after the dispatch-test fix. The provider,
agent and gateway packages were rerun.
- go test -race -count=3 on the new tests.
- gofmt -l is clean.
- go vet: darwin and windows ./... ok. linux ok for the changed packages.
linux ./... fails the same on clean e21ea07e4 (wails v3's cgo-only Linux
files, cross-vet from macOS).
80 lines
2.9 KiB
Go
80 lines
2.9 KiB
Go
package provider
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/klauspost/compress/zstd"
|
|
)
|
|
|
|
// A ChatGPT turn goes as Codex's client sends it (openai/codex
|
|
// codex-rs/http-client/src/request.rs: zstd level 3, Content-Encoding:
|
|
// zstd), and only a turn: CodexBase's /responses, POSTed, not already
|
|
// encoded. Anything else is left as it was.
|
|
func TestCodexCompressed(t *testing.T) {
|
|
t.Cleanup(CodexZstdForTests(true))
|
|
was := CodexBase
|
|
CodexBase = "https://chatgpt.example/backend-api/codex"
|
|
t.Cleanup(func() { CodexBase = was })
|
|
body := bytes.Repeat([]byte(`{"model":"gpt-6-luna","input":"the same words again "}`), 20000)
|
|
|
|
req, _ := http.NewRequest("POST", CodexBase+"/responses", bytes.NewReader(body))
|
|
n := CodexCompressed(req, body)
|
|
wire, _ := io.ReadAll(req.Body)
|
|
if req.Header.Get("Content-Encoding") != "zstd" || n != len(wire) || req.ContentLength != int64(n) || n >= len(body)/10 {
|
|
t.Fatalf("sent %d of %d bytes as %q, Content-Length %d", n, len(body), req.Header.Get("Content-Encoding"), req.ContentLength)
|
|
}
|
|
d, _ := zstd.NewReader(nil)
|
|
defer d.Close()
|
|
if out, err := d.DecodeAll(wire, nil); err != nil || !bytes.Equal(out, body) {
|
|
t.Fatalf("decoded %d bytes, err %v", len(out), err)
|
|
}
|
|
again, _ := req.GetBody()
|
|
if b, _ := io.ReadAll(again); !bytes.Equal(b, wire) {
|
|
t.Error("GetBody gives another body")
|
|
}
|
|
|
|
for _, u := range []string{CodexBase + "/responses/compact", CodexBase + "/models", "https://api.openai.com/v1/responses", "http://127.0.0.1:1/backend-api/codex/responses"} {
|
|
req, _ := http.NewRequest("POST", u, bytes.NewReader(body))
|
|
if n := CodexCompressed(req, body); n != len(body) || req.Header.Get("Content-Encoding") != "" {
|
|
t.Errorf("%s compressed", u)
|
|
}
|
|
}
|
|
req, _ = http.NewRequest("POST", CodexBase+"/responses", bytes.NewReader(body))
|
|
req.Header.Set("Content-Encoding", "gzip")
|
|
if n := CodexCompressed(req, body); n != len(body) || req.Header.Get("Content-Encoding") != "gzip" {
|
|
t.Error("an encoded body encoded again")
|
|
}
|
|
}
|
|
|
|
// The warm-up "hi" is a turn as Codex's are, and goes compressed too.
|
|
func TestCodexWarmGoesZstd(t *testing.T) {
|
|
t.Cleanup(CodexZstdForTests(true))
|
|
var enc string
|
|
var got map[string]any
|
|
fake := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
enc = r.Header.Get("Content-Encoding")
|
|
d, _ := zstd.NewReader(r.Body)
|
|
defer d.Close()
|
|
json.NewDecoder(d).Decode(&got)
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
io.WriteString(w, "event: response.completed\ndata: {\"type\":\"response.completed\"}\n\n")
|
|
}))
|
|
defer fake.Close()
|
|
was := CodexBase
|
|
CodexBase = fake.URL + "/backend-api/codex"
|
|
t.Cleanup(func() { CodexBase = was })
|
|
|
|
if err := warmCodex(context.Background(), func(context.Context, *http.Request, []byte) error { return nil }, "gpt-6-luna", ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if enc != "zstd" || got["model"] != "gpt-6-luna" || got["input"] == nil {
|
|
t.Fatalf("warmed %q: %v", enc, got)
|
|
}
|
|
}
|