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 29bfd5dc8 (plugins#852);
crayonlu's test for it is kept.
Ported from crayonlu's codex-lead-events branch (917878484, four
commits), with the keepalive comment and OpenAI's audio events added to
the enum.
Run (mote, macOS arm64, Go 1.26.8):
- focused: TestResponsesEventsOutsideTheEnumStayWithCodex,
TestResponsesEnumAllowed, TestPassthroughResponsesEndsShort,
TestSocketHeaderFramesOnlyForCodex, TestCodexSocketServed,
TestResponsesEventsAreClientShaped: ok.
- broken by hand: gateway.go and responses_ws.go back to origin/main
with the tests kept: both foreign clients red (codex.rate_limits,
codex.response.metadata, response.metadata, keepalive there=true, no
keepalive comment) and the socket test red with the leaked frames;
the Codex cases stay green.
- gofmt -l clean; go vet -tags nogui ./internal/gateway on darwin,
GOOS=linux and GOOS=windows ok.
- go test -tags nogui -timeout 45m -count=1 ./internal/gateway: ok
(2186s), at this tree on origin/main c9d253e4a.
- rebased on 44d6dd75d (gateway.go changed upstream): the focused
tests above with the new cap/group tests (TestCapGroup*,
TestCodexOneAccountSpent*, TestPluginCapPool*) ok; vet ok.
Not tried: a real ChatGPT account with Grok Build (a second sign-in can
rotate the live refresh token); the full ./... suite (mote at load ~760
with a long suite-lock queue).
Reported-by: crayonlu
Reported-by: Jeo-Meek
Co-authored-by: crayon <hicrayon@outlook.com>
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.
Codex names previous_response_id and sends only the new input on its
WebSocket path alone; magpie answered the upgrade 426 (a87dc1ff2), so
Codex fell back to HTTP and re-sent the whole conversation, images
included, every turn (kevinzhow and neighbads on X).
/v1/responses, /responses and /backend-api/codex/responses now accept the
socket (responses_ws.go), with Codex's protocol (codex-rs 2f1e4f9,
responses_websocket.rs):
- each response.create frame is replayed as the POST it stands for through
the outermost handler the upgrade came in on (socketEntry wraps Front and
Handler), so the allow list, rebound Host check, key, corsGuard,
keyLimited, routing, failover, usage and log judge every turn;
- previous_response_id resolves to the socket's own last two replies
(input + output items); the provider gets the whole conversation, so a
switch of account or provider mid-chain loses nothing. An id the socket
doesn't hold is previous_response_not_found (Codex reconnects with
everything);
- generate:false (warmup) is answered locally; response.interrupt cancels
the turn (accepted + incomplete/interrupted, or failed); errors are
Codex's error frame with status and the headers it acts on;
response.metadata, codex.response.metadata (x-models-etag, magpie's tag)
and codex.rate_limits are sent as the backend does;
- shutdown drains sockets with 1001 and refuses new ones 503.
Codex needs no config change: magpie's wiring points the built-in openai
provider at /backend-api/codex, which Codex already speaks WS to. Other
Codex backend paths still answer 426. Docs: gateway-routing (new section
"A Responses WebSocket"), providers-accounts, reference.
Run at the pushed head (rebased on origin/main 430005726), on mote:
gofmt -l clean; go vet ./internal/gateway for darwin, linux, windows;
go test -timeout 45m ./internal/gateway: ok (480s).
Real Codex (codex-cli 0.165.0, sandbox HOME/CODEX_HOME with a copy of the
owner's ChatGPT auth, sandbox magpie on 127.0.0.1:3481, gpt-5.6-luna, a
task with three tool calls). At the pushed head, built on mote: exit 0,
right answer, 5 turns on one socket, 4 continued, 75480 B received vs
278210 B sent on as whole requests, no fallback to HTTP. Before the
rebase, with a temporary per-turn log: exit 0, right answer. 5 turns on one socket,
4 continued with previous_response_id; frames 58073 B (first), then 4465,
3051, 3093 B, against 58.7-66.6 KB had each been a whole request. 70185 B
received over the socket vs 255379 B sent on as whole requests.
Broken by hand:
- serveResponsesSocket returning false (the old 426): 9 of the new tests
red.
- turns sent straight to the Responses handler with the upgrade's
post-guard context: TestResponsesSocketGuards red (a key turned off
still served; a 700-token limit not held).
Not tried: images in a real run; Codex on Windows/Linux. Not done: the
leg magpie -> vendor still sends the whole conversation each turn (for
ChatGPT, keeping an upstream socket per client socket would save that);
the saving is Codex's upload to magpie. A socket's conversation does not
survive the socket or a restart (Codex then resends everything).