ac4af582301a4e8a29245dbba767eba31334ddf9
6
Commits
| Author | SHA1 | Message | Date | |
|---|---|---|---|---|
|
|
b68218f43e |
gateway: a Responses reply to a client that isn't Codex carries only the Responses enum's events (#4482)
Grok Build, reading /v1/responses through async-openai's typed ResponseStreamEvent, lost whole turns on a ChatGPT account: the backend speaks part of the Codex protocol (codex.rate_limits, codex.response.metadata, response.metadata, a keepalive event), and the client read each as `unknown variant` and ended the turn. passthrough now runs responsesEnumTidy (responses_enum.go) last in its chain for a Responses stream to a client that isn't Codex (the fromCodex / agentOf pair codex_own_v1.go uses): every event whose `event:` name, or data `type` where it names none, is outside the OpenAI Responses enum is dropped; a keepalive goes on as an SSE comment so a quiet upstream still keeps the client's idle timer alive. sniff and codexLimitsTap read the upstream bytes before it, so failover and the account's windows are unchanged. Codex itself gets every event as it came. A plugin's Responses reply goes through the same passthrough, so it is covered too. The WebSocket route's synthesized header frames (socketTurnWriter.headerFrames) go to Codex alone too. The response.failed shape from the report (missing created_at / sequence_number) was already fixed on main by |
||
|
|
acd789d452 |
gateway: a finished route drops its tool scan, so socket turn bodies are freed
casyubaji (Discord) saw magpie at ~2.8 GB after 6 hours on v0.1.2909
with Claude Code, Codex and DeepSeek. The cause is Codex's Responses
socket. Each turn is served from a body built in memory (turnBody), the
whole conversation, through a POST whose Body and GetBody keep it. The
trace keeps its last 60 routes. Each turn's route kept its tool scan
(toolScan), which held the request's context. That context holds the
inflight (inflightKey), the inflight holds its unrouted watch
(unroutedHold.r), and the watch holds the request and so the body. So
60 conversations stayed in memory after every socket had closed: with
Codex's 20 MB ones, over 1.2 GB.
The scan is read only for turnFinal, when the route is done. trace.update
now drops r.tools right after that read. The scan itself still lives with
the request's capture writer while the reply goes.
Soak on mote (fake upstream, sandbox HOME, 300 codexws turns, 3 at a
time, conversations of 20000 to 60000 items, then 2 minutes idle, all
sockets closed):
- main
|
||
|
|
694f4a6d65 |
gateway: a Codex turn of its own reads past 512 MB, to a sixteenth of the machine's memory (dallascao)
dallascao on Discord: generating images in Codex and revising them again
and again, magpie answered 413 "request body is over magpie's gateway
limit of 512 MB (decoded); the provider was not asked" on
127.0.0.1:3425/backend-api/codex/responses, while Codex signed in to
OpenAI directly went on. The count was right: Codex keeps each image it
generated in the conversation (a function_call_output input_image, a
~3 MB PNG as ~4 MB base64; codex-rs ext/image-generation to_response_item,
and history keeps image_generation_call results), and over HTTP sends the
conversation whole every turn, so ~130 images is 512 MB decoded. To
OpenAI directly Codex uses its WebSocket and sends a delta.
- Codex's own path (codexBackend, the Codex relay, the Responses
WebSocket's read limit, keyLimited on CodexPath) reads to
codexBodyLimit: a sixteenth of the machine's memory (memLimit, else
proc.MemTotal), 512 MB at least, 2 GB at most. 16 GB: 1024 MB,
24 GB: 1536 MB, 32 GB+: 2048 MB, 8 GB or unknown: 512 MB as before.
It is still the bomb guard: nothing decodes past it, and the zstd
decoder keeps its window bound.
- A turn for a magpie model ("provider/model") on that path is held to
the 512 MB of every other route once its model is read: it is
translated, at several times its size. Its 413 says one of Codex's own
models takes up to codexBodyLimit; writeError's /compact hint stays.
- provider.codexNamesBad read tools/input/tool_choice with
GetManyBytes, which copied the whole input (the body's size: 300 MB of
a 300 MB turn in the alloc profile). It reads in place now.
Cost at the largest input (mote, a relayed gpt-5.5 turn of random
~3 MB images, heap objects sampled every 5ms, load ~350): 300 MB took
4.7s, 600 MB 7.7s, 1000 MB 11.9s, peak heap 1.5 to 2.1 GB past the
body's own bytes. The rest of the cost is io.ReadAll growing the decoded
body and fillJSONIn/exchangeOf; bodyRoom still runs a body bigger than
its room alone.
Run at the pushed head (mote): gofmt -l clean; GOOS=linux and
GOOS=windows go vet ./internal/gateway ./internal/provider ok;
go test -timeout 45m ./internal/provider ok, ./internal/gateway ok but
TestCompactionOverByteCapEveryTime, red in the full package run only
(#4862, filed before this; passes alone 3 of 3 here).
TestRequestBodyTooLarge/codex now sends codexBodyLimit+1.
- TestCodexGeneratedImagesPastOldLimit (16 GB machine fixture, a 516 MB
turn of generated images in Codex's shape): reaches the fake ChatGPT
backend whole in ~6s; the same turn for x/gpt-5 is 413 naming
"one of Codex's own models takes up to 1024 MB".
- TestCodexBodyLimitFromMemory: 2/8/16/24/64 GiB and unknown memory.
Broken by hand: codexBackend reading with limit 0 (the old code) makes
TestCodexGeneratedImagesPastOldLimit red with the reporter's 413; the
magpie-model check turned off makes it red (404 from routing, not 413).
Not tried: the reporter's own session or a real Codex image-generation
run through magpie; a turn past 1 GB on a 16 GB machine; the WebSocket's
raised read limit has no test of its own.
|
||
|
|
ec0a11d47f |
gateway: a Codex socket's held reply keeps its input, not the frame it came in
A held turn's fresh input items were views into the response.create frame they were decoded from, so each held reply kept that whole frame alive: Codex's tools and instructions with it, for as long as the socket held the reply (~39 MB of a long session's live heap). keep now copies the fresh items into one buffer of their own; the conversation held before is already owned, and the reply's items were already copied. Run on mote: go vet ./internal/gateway; go test -run 'TestHeldTurnOwnsItsInput|TestResponsesSocket|TestCodexSocket' ./internal/gateway ok. Broken by hand (keep appends fresh as views): TestHeldTurnOwnsItsInput fails, held items read back as the overwritten frame. Not run: the full gateway package before the push, and a real Codex session over the socket. |
||
|
|
a51776f9c3 |
gateway: a socket's turn writes its conversation once, not through json.Marshal twice
Codex sends each turn on its Responses WebSocket with only what came after the reply before it, and the gateway put the conversation back together and wrote the body the HTTP route would have had: json.Marshal of the list, then json.Marshal of the body around it. encoding/json writes a json.RawMessage through its scanner a byte at a time into a buffer that doubles and is then copied out, so a 3.2 MB conversation allocated 9.6 MB on every turn. marshalRaw (#1532) had fixed this for the HTTP routes' rewrites; the socket's turn was left on json.Marshal. turnBody writes the same bytes, keys sorted, each value checked by jsonCheck and compacted with <, > and & and U+2028/2029 escaped, into one slice sized for them: its length plus the five bytes each <, > and & grows by, counted with bytes.Count, since code is full of them. A value encoding/json would refuse still goes to json.Marshal, which says why. Live heap on the owner's running app had 8.8 MB under this json.Marshal between collections. Run on mote (M5 Pro), at this tree: - TestTurnBodyIsJSONMarshal: byte for byte json.Marshal's for 0, 1 and 40 pretty-printed items with literal <, >, &, U+2028, U+2029, tabs and CJK; nil input is null; a broken item is refused. - TestTurnBodyWrittenOnce: a 3,225,123-byte turn allocates 3,252,336 bytes (1.01x); json.Marshal twice, 9,605,256. Broken by hand in the mote copy, each red: the escape count dropped (2.9x, the slice grew twice); values written without HTML escaping (bytes differ for all three sizes); json.Marshal back inside turnBody (3.0x). - TestResponsesSocket* and TestCodexSocket*: ok. - go vet ./internal/gateway/ for darwin, linux and windows; gofmt clean. - The whole internal/gateway package at the parent, v0.1.2406: ok (772s). Real use: this build (go build -tags nogui) in a sandbox HOME with the owner's DeepSeek key only, magpie serve on 127.0.0.1:3581, and Node 22's WebSocket on /v1/responses: a 30 KB first turn of 400 lines with <ZEBRA&n> and code, asked for the last one, answered <ZEBRA&399>; the second turn, previous_response_id and one new message, asked for the first, answered <ZEBRA&0>. The log: 2 turns, 1 continued, 88003 bytes sent on as whole requests. Not tried: a real Codex CLI session on the socket (the client here sends what Codex sends: response.create, previous_response_id, input). |
||
|
|
ffa07395eb |
gateway: Codex talks Responses to magpie over a WebSocket, turns after the first as a delta (#4058)
Codex names previous_response_id and sends only the new input on its WebSocket path alone; magpie answered the upgrade 426 ( |