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).
113 lines
4.1 KiB
Go
113 lines
4.1 KiB
Go
package gateway
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/agentenv"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/testenv"
|
|
)
|
|
|
|
// standInsEnv names the folder of a test binary's stand-in CLIs, for a
|
|
// test binary it starts again.
|
|
const standInsEnv = "MAGPIE_GATEWAY_TEST_STAND_INS"
|
|
|
|
func TestMain(m *testing.M) {
|
|
code, err := isolatedTests(m)
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, err)
|
|
code = 1
|
|
}
|
|
os.Exit(code)
|
|
}
|
|
|
|
// Isolate every process, including the cold-start redaction tests. HOME
|
|
// alone does not isolate the macOS Keychain, and account caches eventually
|
|
// expire: discovery must keep finding only the test's sign-ins and CLIs.
|
|
func isolatedTests(m *testing.M) (int, error) {
|
|
home, err := os.MkdirTemp("", "magpie-gateway-test-")
|
|
if err != nil {
|
|
return 1, err
|
|
}
|
|
defer os.RemoveAll(home)
|
|
for name, value := range map[string]string{
|
|
"HOME": home, "USERPROFILE": home,
|
|
"XDG_CONFIG_HOME": filepath.Join(home, ".config"),
|
|
"XDG_CACHE_HOME": filepath.Join(home, ".cache"),
|
|
"XDG_DATA_HOME": filepath.Join(home, ".local", "share"),
|
|
"APPDATA": filepath.Join(home, "AppData", "Roaming"),
|
|
"LOCALAPPDATA": filepath.Join(home, "AppData", "Local"),
|
|
} {
|
|
if err := os.Setenv(name, value); err != nil {
|
|
return 1, err
|
|
}
|
|
}
|
|
// The package isolates itself rather than through testenv.Run, so it
|
|
// says so as testenv does: what reads the marker (a CLI identity kept
|
|
// on disk, provider.keepsIdentities; the machine's own folders,
|
|
// proc.SystemDirs) then acts as in any isolated test binary. Without it
|
|
// an ask still going when its test ended wrote cli-identity.json into
|
|
// the next test's folder as that was removed (#1524, again after
|
|
// 9c377231 dropped this package's own switch).
|
|
if err := os.Setenv(testenv.Marker, home); err != nil {
|
|
return 1, err
|
|
}
|
|
for _, name := range agentenv.Vars {
|
|
if err := os.Unsetenv(name); err != nil {
|
|
return 1, err
|
|
}
|
|
}
|
|
// The machine's proxy variables too: net/http freezes them into its
|
|
// first routed request, so a test that sets its own afterwards (search's
|
|
// Auto, by ALL_PROXY alone) reads the machine's instead, and its
|
|
// searcher goes out through a proxy that cannot reach the test's
|
|
// servers.
|
|
testenv.ClearProxy()
|
|
// The fakes of ChatGPT's backend read turns plain; codex_zstd_test's
|
|
// send them zstd, as the real one is sent.
|
|
provider.CodexZstdForTests(false)
|
|
// Finding an inert CLI also prevents discovery from falling back to
|
|
// one installed at an absolute system path. Tests can prepend their
|
|
// own fakes, as fakeClaude does, and still use ordinary shell tools.
|
|
// A test binary this one starts again (the cold-start redaction tests)
|
|
// runs the stand-ins its parent made: on macOS a newly written program
|
|
// waits for the system's first-run check, and under a full suite's load
|
|
// a child's account discovery spent its whole 20s on new stand-ins.
|
|
bin := os.Getenv(standInsEnv)
|
|
if bin == "" {
|
|
bin = filepath.Join(home, "bin")
|
|
if err := os.Mkdir(bin, 0o755); err != nil {
|
|
return 1, err
|
|
}
|
|
if err := testenv.StandIns(bin, []string{"security", "secret-tool", "claude", "codex", "cursor-agent", "devin", "grok", "kiro-cli"}); err != nil {
|
|
return 1, err
|
|
}
|
|
defer testenv.RemovePrograms()
|
|
if err := os.Setenv(standInsEnv, bin); err != nil {
|
|
return 1, err
|
|
}
|
|
}
|
|
if err := os.Setenv("PATH", bin+string(os.PathListSeparator)+os.Getenv("PATH")); err != nil {
|
|
return 1, err
|
|
}
|
|
// no route is written to disk behind a test's back; the history's own
|
|
// tests call saveRoute themselves
|
|
keepRoutes = false
|
|
// no test asks a vendor: a provider with a real base URL and a made-up
|
|
// key would, in the background (a plan key's windows, #1016), and a
|
|
// plugin host's first start fetched models.dev
|
|
testenv.Offline()
|
|
return m.Run(), nil
|
|
}
|
|
|
|
// The package's tests run as an isolated test binary: nothing the marker
|
|
// turns off (a CLI identity kept on disk, #1524) runs behind a test's back.
|
|
func TestRunsAsAnIsolatedTestBinary(t *testing.T) {
|
|
if os.Getenv(testenv.Marker) == "" {
|
|
t.Fatalf("TestMain left %s unset", testenv.Marker)
|
|
}
|
|
}
|