Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.
gateway: a Codex turn chatgpt.com's WebSocket refuses as too big goes over HTTP (Lamrfu)
yetone3e9efe1d51a5
diff --git a/docs/subsystems/providers-accounts.md b/docs/subsystems/providers-accounts.mdindex ba2bc784a..04909f109 100644--- a/docs/subsystems/providers-accounts.md+++ b/docs/subsystems/providers-accounts.md@@ -211,7 +211,7 @@ and `TestHistoricalWindowHoldsUsesEachWindowsOwnReading`.- A Copilot account is known by its GitHub login and host (`CopilotAccountName`, #1220): `mona` on github.com, `[email protected]` on an enterprise's GHE.com. That name is its `logins.json` user, its row in every list, its usage card and its per-account routing settings, and it is what `SwitchLogin`, `SetLoginOn` and `ForgetLogin` take, so one login on two hosts is two accounts, neither written over by the other nor dropped as the editors' own. One saved before on an enterprise's host under its login alone is read under its name with the host (`copilotSavedName`), keeping its id; the editors' own remembered so is renamed in place, still hidden if it was (`copilotRenamed`). Either rename takes the account's per-account settings in `providers.json` (proxy, models, cap, concurrency) from the bare login to the new name before the new name is written to `logins.json` (`renameSettled`): a setting the new name has already stays, and while `providers.json` can't be read or written the account stays saved under its old name, its settings with it. `magpie accounts switch|forget copilot` takes the full name, the login with `--host <name>.ghe.com`, or the login alone when it names one account.- A Codex account is known by its email, with the plan for a workspace seat (`codexUser`), and a second seat of one email in another workspace by that name with its workspace after it (`codexName`): `[email protected] · Team · ws-two`. The account Codex is signed in to goes by the name it is saved under (`codexLiveName`) everywhere: its provider's `Account.User` (`codexAccount`), `liveLogin`, and its session identity (`SessionIdentities`). Its allowance, rests and sticks (`restKey`), cap, proxy, model list, a gateway key's account list (`LoginID`) and the Routing page all go by that name, so two seats of one email are never read as one (#1424).- Every request to the ChatGPT backend (`CodexBase`'s host) goes over HTTP/1.1, as Codex itself speaks to it: each transport [`netproxy.Dispatch`](../../internal/netproxy/netproxy.go) makes (`http.DefaultClient`'s, the gateway's) sends a request `netproxy.HTTP1` names through an HTTP/1.1 copy of itself whose TLS hello offers no h2, and the provider package sets `HTTP1` to `codexBackend` ([codex_http1.go](../../internal/provider/codex_http1.go)). That covers the gateway's turns, the models list, the warm-up, the allowance, its resets and spending one, through the proxy in force, an account's own included. Every other vendor is still offered HTTP/2.-- A streamed turn to the ChatGPT backend (POST `CodexBase`/responses with `"stream":true`) goes over the backend's WebSocket, as Codex's own client sends it, and comes back as the server-sent events the HTTP route answers with (`codexSocket` in [codex_socket.go](../../internal/gateway/codex_socket.go), first in the gateway's `upstreamTransport`, so Codex's own sign-in passed through and magpie's ChatGPT accounts both take it, through the proxy in force). On some networks a streamed HTTP reply from chatgpt.com is cut at about 120 s, before `response.completed`, while the WebSocket runs to the end (brickhao on Discord). The handshake carries the turn's headers with `OpenAI-Beta: responses_websockets=2026-02-06`, without `x-codex-turn-state` and the responses-lite header, which go in the message's `client_metadata` as Codex puts them; the message is the body with `"type":"response.create"` ahead of it (a zstd body is decoded first). `codex.rate_limits` and `codex.response.metadata`, read ahead of the reply, are also told as the `x-codex-*` and `x-codex-turn-state` headers the HTTP route carries; `responsesapi.websocket_timing` is dropped. An `error` frame ahead of the reply is that status with its error as the body; `websocket_connection_limit_reached`, a refused handshake or a body that isn't a streamed turn goes over HTTP instead, and a host that refused the handshake (other than 401, 429 or 5xx) is spoken HTTP for 10 minutes (`codexSocketShun`). A socket closed before the reply's end reads as a cut stream (`io.ErrUnexpectedEOF`). Codex still speaks HTTP to magpie: magpie answers its WebSocket handshake 426, and Codex falls back for the session; only the leg to the vendor changed. The gateway's tests turn it off in TestMain (`codexSocketOff`); codex_socket_test's fakes speak it.+- A streamed turn to the ChatGPT backend (POST `CodexBase`/responses with `"stream":true`) goes over the backend's WebSocket, as Codex's own client sends it, and comes back as the server-sent events the HTTP route answers with (`codexSocket` in [codex_socket.go](../../internal/gateway/codex_socket.go), first in the gateway's `upstreamTransport`, so Codex's own sign-in passed through and magpie's ChatGPT accounts both take it, through the proxy in force). On some networks a streamed HTTP reply from chatgpt.com is cut at about 120 s, before `response.completed`, while the WebSocket runs to the end (brickhao on Discord). The handshake carries the turn's headers with `OpenAI-Beta: responses_websockets=2026-02-06`, without `x-codex-turn-state` and the responses-lite header, which go in the message's `client_metadata` as Codex puts them; the message is the body with `"type":"response.create"` ahead of it (a zstd body is decoded first). `codex.rate_limits` and `codex.response.metadata`, read ahead of the reply, are also told as the `x-codex-*` and `x-codex-turn-state` headers the HTTP route carries; `responsesapi.websocket_timing` is dropped. An `error` frame ahead of the reply is that status with its error as the body; `websocket_connection_limit_reached`, a refused handshake or a body that isn't a streamed turn goes over HTTP instead, and a host that refused the handshake (other than 401, 429 or 5xx) is spoken HTTP for 10 minutes (`codexSocketShun`). A socket that ends, or won't take the message, before any frame of the reply was handed on (any close status, a dropped connection, a normal close with no reply) sends the same body over HTTP (`codexSocketEnded`): Lamrfu's long conversation was closed 1009 (message too big) by chatgpt.com and got a 502. magpie opens a socket per turn and sends the whole body, where Codex's own client sends a delta after `previous_response_id` on a kept socket. A message at least as big as one a host closed 1009 goes to it over HTTP for 10 minutes (`codexTooBig`). A socket closed after the reply began reads as a cut stream (`io.ErrUnexpectedEOF`), and is not asked again. Codex still speaks HTTP to magpie: magpie answers its WebSocket handshake 426, and Codex falls back for the session; only the leg to the vendor changed. The gateway's tests turn it off in TestMain (`codexSocketOff`); codex_socket_test's fakes speak it.- A provider the user added may have its certificate left unchecked (`InsecureTLS`, the editor's Certificate row, #2383: a vLLM on the LAN, Ollama behind a self-signed cert, a company CA). Only one with a base URL of the user's own takes it (`TakesInsecureTLS`: no account, no preset, not Vertex, not a subscription's or plugin's id); `Save` refuses it on any other, and one written into providers.json by hand is ignored. `Provider.Via` then adds `netproxy.WithUncheckedTLS` with the hosts of its own base URLs (`UncheckedHosts`: Chat, Responses, Anthropic, Gemini, Decide), so every request made on its behalf through a `Dispatch` transport (the gateway's turns, count_tokens, models, test and detect, balance) skips the check for those hosts alone, on a transport kept apart for each (`route.unchecked`). A redirect or a models, balance or image URL on another host is checked, and `netproxy.With` drops the leave for a request made for another provider. An `https://` proxy would be dialled with the same TLS config, so its certificate would go unchecked too: such a request is refused (`ErrUncheckedOverHTTPSProxy`); an `http://` or `socks5://` proxy is taken. Backup, export and sync carry the field with the provider; an import link doesn't.- The Copilot account the editors and CLI keep is read by `copilotLogin` ([account.go](../../internal/provider/account.go)): an editor's github.com sign-in in `~/.config/github-copilot/apps.json` or `hosts.json` first, then the Copilot CLI's, then an editor's on GHE.com. When GitHub refuses the editor token's trade for a session token (401 or 403, `copilotRefusals`, retried after an hour), the CLI's sign-in of the same login on the same host takes its place, sent as the CLI sends it, from that request on and on the Usage card (`copilotStandIn`); a CLI signed in to another account is never used for it, and the error names the editors' file (#1238). A sign-in from magpie of the account whose editor token is refused is kept and stands for it (`copilotProbeEditor`, `addCopilotLogin`); while the editor token works, it is let go as the agent's own, as before.- A keychain read that may have shown the user a dialog is not asked again for ten minutes after it was refused (`keychainDeniedTTL` in [account.go](../../internal/provider/account.go)): Copilot's token read backs a denial off (`copilotCLISecret`), and the Cursor token (`cursorKeychainToken`) and Claude Code's item (`readClaudeKeychainItem`, through `claudeCredential`'s cache) do too (#2408). 44, the item not being there, is no refusal and keeps the short TTL, so a sign-in made in the agent is still seen within it; a sign-in or out in magpie reads afresh (`forgetCursorStatus`, `forgetClaudeCredential`), backoff or not.diff --git a/internal/gateway/codex_socket.go b/internal/gateway/codex_socket.goindex 4a9263890..74b27f863 100644--- a/internal/gateway/codex_socket.go+++ b/internal/gateway/codex_socket.go@@ -42,7 +42,16 @@ import (//// Anything the socket can't do falls back to the HTTP request as it was:// a refused handshake (and that host is spoken HTTP for codexSocketShun),-// a connection limit, a body that isn't a streamed turn.+// a connection limit, a body that isn't a streamed turn, and a socket+// that ends, or won't take the message, before any of the reply was+// handed on. That last one is Lamrfu's (Discord): chatgpt.com closed the+// socket 1009 (message too big) on a long conversation's turn, and the+// turn was a 502. Codex's own client sends a turn on a kept socket as a+// delta after previous_response_id, so its messages stay small, and falls+// back to HTTP for the session when the socket keeps failing; magpie+// opens a socket per turn and sends the whole body. A message as big as+// one refused 1009 goes straight over HTTP to that host for+// codexSocketShun (codexTooBig).// codexSocketOff turns the socket off: the gateway's tests' fakes of the// ChatGPT backend speak HTTP, so TestMain sets it, and codex_socket_test@@ -67,8 +76,43 @@ const codexSocketPrelude = 8var (codexShunMu sync.MutexcodexShun = map[string]time.Time{}+ // codexTooBig is the smallest message a host closed 1009 on, and+ // until when messages that big go to it over HTTP; under codexShunMu.+ codexTooBig = map[string]tooBig{})+type tooBig struct {+ size int+ until time.Time+}++// codexMessageTooBig says whether a message of size bytes is as big as+// one host refused, and so goes over HTTP.+func codexMessageTooBig(host string, size int) bool {+ codexShunMu.Lock()+ defer codexShunMu.Unlock()+ tb, ok := codexTooBig[host]+ if ok && time.Now().After(tb.until) {+ delete(codexTooBig, host)+ return false+ }+ return ok && size >= tb.size+}++func refusedTooBig(host string, size int) {+ codexShunMu.Lock()+ tb, was := codexTooBig[host]+ if !was || size < tb.size || time.Now().After(tb.until) {+ tb.size = size+ }+ tb.until = time.Now().Add(codexSocketShun)+ codexTooBig[host] = tb+ codexShunMu.Unlock()+ if !was {+ log.Printf("codex: %s closed the WebSocket on a %d-byte turn as too big; turns that big go over HTTP for %s", host, size, codexSocketShun)+ }+}+func codexShunned(host string) bool {codexShunMu.Lock()defer codexShunMu.Unlock()@@ -100,7 +144,7 @@ func (t codexSocket) RoundTrip(req *http.Request) (*http.Response, error) {return t.next.RoundTrip(req)}msg, ok := codexSocketMessage(req)- if !ok {+ if !ok || codexMessageTooBig(req.URL.Host, len(msg)) {return t.next.RoundTrip(req)}res, err := t.socket(req, msg)@@ -264,7 +308,7 @@ func (t codexSocket) socket(req *http.Request, msg []byte) (*http.Response, erroif req.Context().Err() != nil {return nil, req.Context().Err()}- return nil, fmt.Errorf("codex websocket: %w", err)+ return nil, codexSocketEnded(u.Host, len(msg), err)}header := http.Header{}for k, vs := range hs.Header {@@ -285,10 +329,8 @@ func (t codexSocket) socket(req *http.Request, msg []byte) (*http.Response, erroif req.Context().Err() != nil {return nil, req.Context().Err()}- if errors.Is(err, io.EOF) && len(body.pending) == 0 {- return nil, errors.New("codex websocket: closed before the reply began")- }- return nil, err+ // nothing of the reply has been handed on yet+ return nil, codexSocketEnded(u.Host, len(msg), err)}if frame == nil { // droppedcontinue@@ -332,6 +374,20 @@ func (t codexSocket) socket(req *http.Request, msg []byte) (*http.Response, erro}, nil}+// codexSocketEnded is the turn going over HTTP after the socket ended,+// or failed to take the message, before any of the reply was handed on:+// whatever the close (1009 message too big, 1011, 1012, 1013, a dropped+// connection, a normal close with no reply), nothing has reached the+// caller and the backend has begun no reply, so the same body is asked+// over HTTP, which answers as the route does. A 1009 is remembered for+// that host (refusedTooBig).+func codexSocketEnded(host string, size int, err error) error {+ if websocket.CloseStatus(err) == websocket.StatusMessageTooBig {+ refusedTooBig(host, size)+ }+ return errCodexSocketFallBack+}+// codexSocketError is the HTTP reply an error frame that comes ahead of// the reply stands for, and true when the turn is to go over HTTP// instead; neither for one the stream carries on with.diff --git a/internal/gateway/codex_socket_test.go b/internal/gateway/codex_socket_test.goindex b91027270..17fedd6c0 100644--- a/internal/gateway/codex_socket_test.go+++ b/internal/gateway/codex_socket_test.go@@ -31,8 +31,16 @@ type fakeSocketBackend struct {mu sync.Mutexsockets []socketTurnposts int- refuse int- frames func(socketTurn) []string+ // handshakes counts the sockets opened, a refused message's too+ handshakes int+ refuse int+ frames func(socketTurn) []string+ // limit is the socket's read limit (0: none); a bigger message is+ // closed 1009 as coder/websocket does it+ limit int64+ // closeWith, when set, closes the socket with that status after the+ // frames, instead of a normal close+ closeWith websocket.StatusCode}func (f *fakeSocketBackend) ServeHTTP(w http.ResponseWriter, r *http.Request) {@@ -51,7 +59,13 @@ func (f *fakeSocketBackend) ServeHTTP(w http.ResponseWriter, r *http.Request) {}defer c.CloseNow()c.SetReadLimit(-1)+ if f.limit > 0 {+ c.SetReadLimit(f.limit)+ }_, msg, err := c.Read(r.Context())+ f.mu.Lock()+ f.handshakes+++ f.mu.Unlock()if err != nil {return}@@ -64,6 +78,10 @@ func (f *fakeSocketBackend) ServeHTTP(w http.ResponseWriter, r *http.Request) {return}}+ if f.closeWith != 0 {+ c.Close(f.closeWith, "")+ return+ }c.Close(websocket.StatusNormalClosure, "")return}@@ -92,6 +110,7 @@ func socketOn(t *testing.T) {codexSocketOff.Store(true)codexShunMu.Lock()clear(codexShun)+ clear(codexTooBig)codexShunMu.Unlock()})}@@ -298,3 +317,98 @@ func TestCodexSocketOnlyForStreamedTurns(t *testing.T) {t.Fatalf("%d posts, %d sockets", posts, sockets)}}++// postsOpened are the turns seen over HTTP and the sockets opened,+// a socket whose message was refused too.+func (f *fakeSocketBackend) postsOpened() (posts, opened int) {+ f.mu.Lock()+ defer f.mu.Unlock()+ return f.posts, f.handshakes+}++// Lamrfu on Discord: on a long conversation chatgpt.com closed the socket+// 1009 (message too big) on the turn's message, and Codex got a 502+// ("codex websocket: failed to get reader: received close frame: status =+// StatusMessageTooBig"), retried and got it again. The same turn now goes+// over HTTP, and a message as big doesn't try the socket again.+func TestCodexSocketTooBigGoesOverHTTP(t *testing.T) {+ socketOn(t)+ f := &fakeSocketBackend{frames: wholeReply, limit: 200}+ chatgpt(t, f.ServeHTTP)+ big := `{"model":"gpt-5.5","stream":true,"input":"` + strings.Repeat("long conversation ", 40) + `"}`+ for i := range 2 {+ code, body := codexPost(t, big)+ if code != 200 || !strings.Contains(body, `"delta":"po"`) {+ t.Fatalf("turn %d: %d %s; want the HTTP reply", i, code, body)+ }+ }+ if posts, opened := f.postsOpened(); posts != 2 || opened != 1 {+ t.Fatalf("%d posts, %d sockets; want both turns over HTTP and one socket tried", posts, opened)+ }+ // a small turn still goes over the socket+ code, body := codexPost(t, `{"model":"gpt-5.5","stream":true,"input":"ping"}`)+ if code != 200 || !strings.Contains(body, "pong") || !strings.Contains(body, "response.completed") {+ t.Fatalf("small turn: %d %s", code, body)+ }+ if posts, sockets := f.counts(); posts != 2 || sockets != 1 {+ t.Fatalf("%d posts, %d sockets; want the small turn on the socket", posts, sockets)+ }+}++// Any end of the socket before a frame of the reply was handed on goes+// over HTTP the same way: a close after the message with any status, a+// normal close with no reply, a close after only the headers' frames.+func TestCodexSocketEndedBeforeReplyGoesOverHTTP(t *testing.T) {+ limits := func(socketTurn) []string { return wholeReply(socketTurn{})[:2] }+ none := func(socketTurn) []string { return nil }+ for _, tc := range []struct {+ name string+ frames func(socketTurn) []string+ status websocket.StatusCode+ }{+ {"too big after the message", none, websocket.StatusMessageTooBig},+ {"too big after the limits", limits, websocket.StatusMessageTooBig},+ {"internal error", none, websocket.StatusInternalError},+ {"service restart", none, websocket.StatusServiceRestart},+ {"try again later", none, websocket.StatusTryAgainLater},+ {"policy violation", none, websocket.StatusPolicyViolation},+ {"going away", limits, websocket.StatusGoingAway},+ {"normal, no reply", none, 0},+ } {+ t.Run(tc.name, func(t *testing.T) {+ socketOn(t)+ f := &fakeSocketBackend{frames: tc.frames, closeWith: tc.status}+ chatgpt(t, f.ServeHTTP)+ code, body := codexPost(t, `{"model":"gpt-5.5","stream":true,"input":"ping"}`)+ if code != 200 || !strings.Contains(body, `"delta":"po"`) {+ t.Fatalf("%d %s; want the HTTP reply", code, body)+ }+ if posts, sockets := f.counts(); posts != 1 || sockets != 1 {+ t.Fatalf("%d posts, %d sockets", posts, sockets)+ }+ })+ }+}++// Once a frame of the reply was handed on, a 1009 close is a cut stream,+// and the turn is not asked again over HTTP.+func TestCodexSocketTooBigMidReplyIsCut(t *testing.T) {+ socketOn(t)+ f := &fakeSocketBackend{frames: func(socketTurn) []string {+ return wholeReply(socketTurn{})[:5]+ }, closeWith: websocket.StatusMessageTooBig}+ chatgpt(t, f.ServeHTTP)+ res, err := New().client.Post(provider.CodexBase+"/responses", "application/json",+ strings.NewReader(`{"model":"gpt-5.5","stream":true,"input":"ping"}`))+ if err != nil {+ t.Fatal(err)+ }+ defer res.Body.Close()+ b, err := io.ReadAll(res.Body)+ if err == nil || !strings.Contains(string(b), "pong") {+ t.Fatalf("read %q, %v; want the reply so far and a cut", b, err)+ }+ if posts, _ := f.counts(); posts != 0 {+ t.Fatalf("%d HTTP posts; the turn was asked twice", posts)+ }+}