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 (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).
This commit is contained in:
yetone
2026-10-30 08:51:03 +08:00
parent 4300057264
commit ffa07395eb
11 changed files with 1600 additions and 67 deletions
+1 -1
View File
@@ -1973,7 +1973,7 @@ It exposes:
| Path | API |
| ------------------------ | -------------------------- |
| `/v1/chat/completions` | OpenAI chat completions |
| `/v1/responses` | OpenAI Responses (HTTP, streamed as SSE; a WebSocket upgrade is answered 426) |
| `/v1/responses` | OpenAI Responses (HTTP, streamed as SSE, or a WebSocket as Codex speaks it) |
| `/v1/messages` | Anthropic Messages |
| `/v1/messages/count_tokens` | Anthropic token counting |
| `/v1beta/models/{model}:generateContent` | Google Gemini (also `:streamGenerateContent`, `:countTokens`) |
File diff suppressed because one or more lines are too long
+1 -1
View File
@@ -256,7 +256,7 @@ and `TestHistoricalWindowHoldsUsesEachWindowsOwnReading`.
- A Copilot account is known by its GitHub login and host (`CopilotAccountName`, #1220): `mona` on github.com, `mona@acme.ghe.com` 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`): `me@example.com · 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 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 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 speaks to magpie over its own WebSocket since #4058 ([A Responses WebSocket](gateway-routing.md#a-responses-websocket)), sending a turn as a delta after `previous_response_id`; magpie gives each turn the whole conversation before it goes on, so this leg still sends the whole body. 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.
- A provider the user added may be set never to rest for a limit (`LimitRestOff`, `limitRestOff`; the editor's Advanced fold, "Set it aside when out of quota or rate limited", ticked by default; zoecats on Discord: a local aggregator's "quota exhausted" while it switches upstreams). Off, a quota, credit or rate refusal and any 429 rest none of its keys and the next request is sent to it as usual (`restAfterMarked` in the gateway, see [gateway routing](gateway-routing.md)). The editor offers it only to a provider the user added (`custom`); a save that leaves it out keeps it (`TestProviderSaveLimitRestOff`, `provider-limit-rest.test.cjs`).
- 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.
+8 -3
View File
@@ -64,9 +64,14 @@ Be concise, structured, and focused on helping the next LLM seamlessly continue
const codexSummaryPrefix = codexsummary.Prefix
func (s *Server) codexBackend(w http.ResponseWriter, r *http.Request) {
// Responses over a WebSocket: 426 sends Codex to plain HTTP at once
if strings.EqualFold(r.Header.Get("Upgrade"), "websocket") {
http.Error(w, "magpie speaks HTTP", http.StatusUpgradeRequired)
// Responses over a WebSocket, Codex's own way to its provider: each
// turn is served as its POST is (responses_ws.go), and Codex sends
// only a turn's new input after the reply it follows (#4058)
if wantsSocket(r) {
if strings.TrimPrefix(r.URL.Path, CodexPath) == "/responses" && s.serveResponsesSocket(w, r) {
return
}
http.Error(w, "magpie serves a WebSocket on "+CodexPath+"/responses alone", http.StatusUpgradeRequired)
return
}
// the pool's accounts and their model lists say they are this Codex, or
-13
View File
@@ -869,19 +869,6 @@ func TestCodexModelsEtagTagged(t *testing.T) {
}
}
// Responses over a WebSocket are turned away so Codex uses HTTP at once.
func TestCodexWebSocketUpgradeRequired(t *testing.T) {
chatgpt(t, func(w http.ResponseWriter, r *http.Request) { t.Error("sent to ChatGPT") })
rec := httptest.NewRecorder()
req := httptest.NewRequest("GET", CodexPath+"/responses", nil)
req.Header.Set("Upgrade", "websocket")
req.Header.Set("Connection", "Upgrade")
New().Handler().ServeHTTP(rec, req)
if rec.Code != http.StatusUpgradeRequired {
t.Errorf("%d", rec.Code)
}
}
// A routing group in Codex's model list is named as one, not as its first
// member's provider, which would read as that provider's own model.
func TestCodexModelsNameGroups(t *testing.T) {
+18 -10
View File
@@ -460,6 +460,8 @@ type Server struct {
// the connections taken whose first request isn't read yet, which a
// handover's end waits for (handover.go)
unread unreadConns
// sockets are the Responses WebSockets being served (responses_ws.go)
sockets clientSockets
// what it serves other computers TLS with, nil without (tls.go)
tls *tls.Config
// the images described for models that can't see them (vision.go)
@@ -720,7 +722,12 @@ func (s *Server) ListenAndServe(ctx context.Context) error {
s.stopAccepting()
s.unread.wait(c)
}
// a socket's connection was taken from the server (hijacked), so
// Shutdown doesn't wait for it: each ends after its turn in flight
sockets := make(chan struct{})
go func() { defer close(sockets); s.sockets.drain(c) }()
srv.Shutdown(c)
<-sockets
s.subscription.abortAll()
provider.FlushServed() // what answered last, kept for the next start and the CLI
flushSticks() // who answered which conversation, for the next start or the magpie handed over to
@@ -800,8 +807,8 @@ func (s *Server) Handler() http.Handler {
mux.HandleFunc("POST /chat/completions", s.handle(provider.Chat))
mux.HandleFunc("POST /v1/responses", s.handle(provider.Responses))
mux.HandleFunc("POST /responses", s.handle(provider.Responses))
mux.HandleFunc("GET /v1/responses", responsesOverHTTP)
mux.HandleFunc("GET /responses", responsesOverHTTP)
mux.HandleFunc("GET /v1/responses", s.responsesOverHTTP)
mux.HandleFunc("GET /responses", s.responsesOverHTTP)
mux.HandleFunc("POST /v1/messages", keepsSignIn(s.handle(provider.Anthropic)))
mux.HandleFunc("POST /messages", keepsSignIn(s.handle(provider.Anthropic)))
mux.HandleFunc("POST /v1/systemone", s.serveSystemOne)
@@ -849,16 +856,17 @@ func (s *Server) Handler() http.Handler {
})
// the goroutine dump first, ahead of every lock the rest can be stuck
// on (dumpFirst)
return s.dumpFirst(s.counted(holdCatalog(corsGuard(callerGuard(withCaller(keyLimited(mux)))))))
// a socket's turns go through all of it again (socketEntry)
return socketEntry(s.dumpFirst(s.counted(holdCatalog(corsGuard(callerGuard(withCaller(keyLimited(mux))))))))
}
// responsesOverHTTP answers a GET on /v1/responses, which is how a client
// opens Responses over a WebSocket (#1005): magpie relays Responses over
// HTTP alone, its stream as SSE, so the upgrade is told 426 — the answer a
// client falls back to HTTP on at once (Codex does, as on CodexPath) — in
// place of the 404 every unknown path gets, which read as a broken
// gateway. A plain GET is told to POST.
func responsesOverHTTP(w http.ResponseWriter, r *http.Request) {
// responsesOverHTTP answers a GET on /v1/responses. A WebSocket upgrade
// is Responses over a WebSocket (#1005, #4058), each turn served as its
// POST is (responses_ws.go). A plain GET is told to POST.
func (s *Server) responsesOverHTTP(w http.ResponseWriter, r *http.Request) {
if wantsSocket(r) && s.serveResponsesSocket(w, r) {
return
}
if strings.EqualFold(r.Header.Get("Upgrade"), "websocket") {
writeError(w, provider.Responses, http.StatusUpgradeRequired, "magpie relays Responses over HTTP only: send POST /v1/responses (streamed as SSE); Responses over a WebSocket is not served")
return
+5
View File
@@ -17,6 +17,11 @@ import (
// and a file another program writes is seen within the second.
func holdCatalog(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// a WebSocket lasts a session: each of its turns holds it instead
if wantsSocket(r) {
h.ServeHTTP(w, r)
return
}
defer provider.Hold()()
defer filememo.Hold()()
h.ServeHTTP(w, r)
+2 -1
View File
@@ -15,7 +15,8 @@ import (
func (s *Server) Front() http.Handler {
// the goroutine dump ahead of lanGuard too, whose allow list reads
// settings.json under its lock (dumpFirst)
s.frontOnce.Do(func() { s.front = s.dumpFirst(lanGuard(s.Handler())) })
// (socketEntry: a socket's turns go through all of it again)
s.frontOnce.Do(func() { s.front = socketEntry(s.dumpFirst(lanGuard(s.Handler()))) })
return s.front
}
-34
View File
@@ -1,34 +0,0 @@
package gateway
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// A client opening Responses over a WebSocket (Pi's Responses WS
// extension, #1005) is told 426 with the reason, not the 404 of an unknown
// path, and nothing goes upstream; a plain GET is told to POST.
func TestResponsesWebSocketUpgradeRequired(t *testing.T) {
setup(t, provider.Responses, &fake{t: t})
for _, path := range []string{"/v1/responses", "/responses"} {
rec := httptest.NewRecorder()
req := httptest.NewRequest("GET", path, nil)
req.Header.Set("Upgrade", "websocket")
req.Header.Set("Connection", "Upgrade")
req.Header.Set("Sec-WebSocket-Version", "13")
req.Header.Set("Sec-WebSocket-Key", "dGhlIHNhbXBsZSBub25jZQ==")
New().Handler().ServeHTTP(rec, req)
if rec.Code != http.StatusUpgradeRequired || !strings.Contains(rec.Body.String(), "POST /v1/responses") {
t.Errorf("%s upgrade: %d %s", path, rec.Code, rec.Body)
}
rec = httptest.NewRecorder()
New().Handler().ServeHTTP(rec, httptest.NewRequest("GET", path, nil))
if rec.Code != http.StatusMethodNotAllowed || rec.Header().Get("Allow") != "POST" {
t.Errorf("%s GET: %d %q", path, rec.Code, rec.Header().Get("Allow"))
}
}
}
+822
View File
@@ -0,0 +1,822 @@
package gateway
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net/http"
"strconv"
"strings"
"sync"
"time"
"unicode/utf8"
"github.com/coder/websocket"
"github.com/tidwall/gjson"
)
// Responses over a WebSocket, as Codex speaks it to its own provider
// (codex-rs codex-api/src/endpoint/responses_websocket.rs and
// core/src/client.rs at openai/codex 2f1e4f9). Codex opens one socket
// for a session and sends each turn on it as a text frame, the HTTP
// body with "type":"response.create" added; the reply comes back as one
// text frame for each event the HTTP route streams. On a socket it has
// had a reply on, Codex names that reply (previous_response_id) and
// sends only what came after it, where over HTTP it sends the whole
// conversation every turn, images and all (#4058, kevinzhow and
// neighbads on X). magpie answered the upgrade 426 (a87dc1ff2), which
// has Codex use HTTP for the session.
//
// Each response.create is served as the POST it stands for: the request
// the socket was opened with is made again, with the frame as its body,
// and sent through the whole handler the upgrade came in on (socketEntry):
// the allow list, the key and its limits, the Origin and Host checks,
// routing, failover, usage and the log judge every turn as they judge an
// HTTP one, and a key turned off ends the next turn, not the socket.
//
// The conversation a turn continues is the socket's own: each reply's
// input and output, kept for the socket's life (Codex says store false on
// every request, and OpenAI's socket keeps the last response for the
// connection all the same). A turn naming one is given the conversation
// whole before it is sent, so what serves it, a provider or another
// account, sees what it would over HTTP. An id the socket doesn't hold
// is answered previous_response_not_found, on which Codex opens a new
// socket and sends the conversation whole.
// socketOriginKey holds the request a socket was opened with.
type socketOriginKey struct{}
type socketOrigin struct {
ctx context.Context // the request's own, before any guard's
req *http.Request // as it arrived: URL, Host, RemoteAddr, headers
via http.Handler // where it arrived, which each turn goes through
}
// wantsSocket: r asks for a WebSocket.
func wantsSocket(r *http.Request) bool {
return r.Method == http.MethodGet && strings.EqualFold(r.Header.Get("Upgrade"), "websocket")
}
// socketEntry is where a request reaches the gateway (Front, Handler): a
// socket's turns are sent through it again, as they arrived.
func socketEntry(next http.Handler) http.Handler {
var self http.Handler
self = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if wantsSocket(r) && r.Context().Value(socketOriginKey{}) == nil {
o := &socketOrigin{ctx: r.Context(), req: r.Clone(r.Context()), via: self}
r = r.WithContext(context.WithValue(r.Context(), socketOriginKey{}, o))
}
next.ServeHTTP(w, r)
})
return self
}
const (
// socketKeep is how many replies a socket holds: Codex names only the
// last, and a warmup's before its first turn
socketKeep = 2
// socketErrMax is the most of an error reply sent on
socketErrMax = 1 << 20
)
// clientSockets are the sockets being served, which end with the gateway.
type clientSockets struct {
mu sync.Mutex
m map[*responsesSocket]bool
closing bool
done chan struct{} // closed when the last one ends while closing
}
func (cs *clientSockets) add(s *responsesSocket) bool {
cs.mu.Lock()
defer cs.mu.Unlock()
if cs.closing {
return false
}
if cs.m == nil {
cs.m = map[*responsesSocket]bool{}
}
cs.m[s] = true
return true
}
func (cs *clientSockets) ending() bool {
cs.mu.Lock()
defer cs.mu.Unlock()
return cs.closing
}
func (cs *clientSockets) remove(s *responsesSocket) {
cs.mu.Lock()
defer cs.mu.Unlock()
delete(cs.m, s)
if cs.closing && len(cs.m) == 0 && cs.done != nil {
close(cs.done)
cs.done = nil
}
}
// drain ends every socket once its turn in flight is answered, and waits
// for them until ctx ends, when the rest are cut. Codex opens a new one
// for its next turn, to the gateway serving the port then, and sends the
// conversation whole on it.
func (cs *clientSockets) drain(ctx context.Context) {
cs.mu.Lock()
cs.closing = true
var done chan struct{}
if len(cs.m) > 0 {
done = make(chan struct{})
cs.done = done
}
all := make([]*responsesSocket, 0, len(cs.m))
for s := range cs.m {
all = append(all, s)
}
cs.mu.Unlock()
for _, s := range all {
s.goingAway()
}
if done == nil {
return
}
select {
case <-done:
case <-ctx.Done():
for _, s := range all {
s.c.CloseNow()
}
}
}
// heldTurn is one reply a socket holds: the conversation up to and
// with it.
type heldTurn struct {
id string
items []json.RawMessage
}
type responsesSocket struct {
s *Server
o *socketOrigin
path string
c *websocket.Conn
ctx context.Context
live context.Context // the socket's, for writes (send)
writeMu sync.Mutex
turns []heldTurn // the newest last
mu sync.Mutex
away bool // the gateway is ending: close after this turn
turning bool
// for the log
frames, continued int
got, sent int64
}
// serveResponsesSocket serves a WebSocket upgrade on a Responses route,
// and false when r didn't come through socketEntry.
func (s *Server) serveResponsesSocket(w http.ResponseWriter, r *http.Request) bool {
o, _ := r.Context().Value(socketOriginKey{}).(*socketOrigin)
if o == nil {
return false
}
// a browser's page from another site is turned away (the Origin
// check here is the library's: Origin's host must be the Host); the
// guards the upgrade passed judged it already
if s.sockets.ending() {
http.Error(w, "magpie is restarting", http.StatusServiceUnavailable)
return true
}
c, err := websocket.Accept(w, r, &websocket.AcceptOptions{CompressionMode: websocket.CompressionNoContextTakeover})
if err != nil {
log.Printf("responses socket from %s: %v", r.RemoteAddr, err)
return true
}
c.SetReadLimit(defaultBodyLimit)
sock := &responsesSocket{s: s, o: o, path: r.URL.Path, c: c, ctx: o.ctx}
if !s.sockets.add(sock) {
c.Close(websocket.StatusGoingAway, "magpie is restarting")
return true
}
defer s.sockets.remove(sock)
sock.serve()
if sock.frames > 0 {
log.Printf("responses socket from %s ended: %d turns, %d continued, %d bytes received, %d sent on as whole requests",
r.RemoteAddr, sock.frames, sock.continued, sock.got, sock.sent)
}
return true
}
func (rs *responsesSocket) goingAway() {
rs.mu.Lock()
rs.away = true
idle := !rs.turning
rs.mu.Unlock()
if idle {
// Close waits for the client's reply: not one socket after another
go rs.c.Close(websocket.StatusGoingAway, "magpie is restarting")
}
}
type socketMsg struct {
data []byte
err error
}
func (rs *responsesSocket) serve() {
ctx, cancel := context.WithCancel(rs.ctx)
defer cancel()
defer rs.c.CloseNow()
rs.live = ctx
in := make(chan socketMsg)
go func() {
for {
typ, data, err := rs.c.Read(ctx)
if err == nil && typ != websocket.MessageText {
err = errors.New("binary frame")
rs.c.Close(websocket.StatusUnsupportedData, "text frames only")
}
select {
case in <- socketMsg{data, err}:
case <-ctx.Done():
return
}
if err != nil {
return
}
}
}()
var queued []socketMsg
for {
var m socketMsg
if len(queued) > 0 {
m, queued = queued[0], queued[1:]
} else {
select {
case m = <-in:
case <-ctx.Done():
return
}
}
if m.err != nil {
return
}
kind := gjson.GetBytes(m.data, "type").String()
switch kind {
case "response.create":
case "response.interrupt":
// nothing in flight: it ended before the ask came
rs.write(ctx, interruptReply("response.interrupt.failed", gjson.GetBytes(m.data, "response_id").String()))
continue
default:
rs.writeError(ctx, http.StatusBadRequest, "invalid_request_error", "unsupported_message",
fmt.Sprintf("magpie serves response.create on this socket, not %q", kind), "type")
continue
}
rs.mu.Lock()
rs.turning = true
rs.mu.Unlock()
turnCtx, stop := context.WithCancel(ctx)
t := &socketTurnRun{}
done := make(chan struct{})
go func() {
defer close(done)
rs.turn(turnCtx, t, m.data)
}()
for waiting := true; waiting; {
select {
case <-done:
waiting = false
case n := <-in:
if n.err != nil {
// the client went: what is in flight ends with it
stop()
<-done
return
}
if gjson.GetBytes(n.data, "type").String() != "response.interrupt" {
queued = append(queued, n)
continue
}
id := gjson.GetBytes(n.data, "response_id").String()
if !t.interrupt(id) {
rs.write(ctx, interruptReply("response.interrupt.failed", id))
continue
}
stop()
<-done
waiting = false
rs.write(ctx, interruptReply("response.interrupt.accepted", id))
// the reply ends as interrupted: Codex keeps none of it
// (discard_partial_items) and waits for its end
end, _ := json.Marshal(map[string]any{"type": "response.incomplete", "response": map[string]any{
"id": id, "object": "response", "status": "incomplete",
"incomplete_details": map[string]any{"reason": "interrupted"}, "output": []any{},
}})
rs.write(ctx, end)
}
}
stop()
rs.mu.Lock()
rs.turning = false
away := rs.away
rs.mu.Unlock()
if t.cut {
// the reply ended before its end: as over HTTP, Codex asks
// again, on a new socket, with the conversation whole
rs.c.Close(websocket.StatusInternalError, "the reply ended before response.completed")
return
}
if away {
rs.c.Close(websocket.StatusGoingAway, "magpie is restarting")
return
}
}
}
func interruptReply(kind, id string) []byte {
b, _ := json.Marshal(map[string]string{"type": kind, "response_id": id})
return b
}
// write sends frame unless ctx, a turn's, has ended (interrupted, or the
// client gone). The write itself is under the socket's context: one whose
// context ends mid-write closes the socket, which an interrupt mustn't.
func (rs *responsesSocket) write(ctx context.Context, frame []byte) error {
if err := ctx.Err(); err != nil {
return err
}
rs.writeMu.Lock()
defer rs.writeMu.Unlock()
return rs.c.Write(rs.live, websocket.MessageText, frame)
}
// writeError is an error frame as Codex reads one: the HTTP status and
// the error object an HTTP reply would have carried.
func (rs *responsesSocket) writeError(ctx context.Context, status int, typ, code, msg, param string) error {
e := map[string]any{"type": typ, "code": code, "message": msg}
if param != "" {
e["param"] = param
}
b, _ := json.Marshal(map[string]any{"type": "error", "status": status, "error": e})
return rs.write(ctx, b)
}
// socketTurnRun is what a turn in flight has said so far.
type socketTurnRun struct {
mu sync.Mutex
ids []string // the response ids it gave
cut bool // its reply ended before its end
cancelled bool
}
func (t *socketTurnRun) interrupt(id string) bool {
t.mu.Lock()
defer t.mu.Unlock()
for _, have := range t.ids {
if have == id {
t.cancelled = true
return true
}
}
return false
}
// held is the conversation a reply the socket holds ends.
func (rs *responsesSocket) held(id string) ([]json.RawMessage, bool) {
for i := len(rs.turns) - 1; i >= 0; i-- {
if rs.turns[i].id == id {
return rs.turns[i].items, true
}
}
return nil, false
}
func (rs *responsesSocket) keep(t heldTurn) {
rs.turns = append(rs.turns, t)
if len(rs.turns) > socketKeep {
rs.turns = append(rs.turns[:0:0], rs.turns[len(rs.turns)-socketKeep:]...)
}
}
// turn serves one response.create.
func (rs *responsesSocket) turn(ctx context.Context, t *socketTurnRun, frame []byte) {
rs.frames++
rs.got += int64(len(frame))
var q map[string]json.RawMessage
if decodeJSONView(frame, &q) != nil {
rs.writeError(ctx, http.StatusBadRequest, "invalid_request_error", "invalid_json", "response.create is not a JSON object", "")
return
}
_, fresh := prevInput(q["input"])
if fresh == nil {
rs.writeError(ctx, http.StatusBadRequest, "invalid_request_error", "invalid_value", "input is neither text nor a list of items", "input")
return
}
input := fresh
if raw := q["previous_response_id"]; len(raw) > 0 && !bytes.Equal(raw, []byte("null")) {
var id string
json.Unmarshal(raw, &id)
prior, ok := rs.held(id)
if !ok {
// Codex opens a new socket and sends the conversation whole
rs.writeError(ctx, http.StatusBadRequest, "invalid_request_error", "previous_response_not_found",
"Previous response with id '"+id+"' not found.", "previous_response_id")
return
}
input = append(prior[:len(prior):len(prior)], fresh...)
rs.continued++
}
// a warmup (generate false) asks for no reply: Codex sends it with
// the conversation ahead of its first turn, and that turn follows it
var generate bool
if raw := q["generate"]; len(raw) > 0 && json.Unmarshal(raw, &generate) == nil && !generate {
id := "resp_" + newID()
t.mu.Lock()
t.ids = append(t.ids, id)
t.mu.Unlock()
resp := map[string]any{"id": id, "object": "response", "created_at": time.Now().Unix(), "status": "in_progress", "output": []any{}}
if m := q["model"]; len(m) > 0 {
resp["model"] = m
}
b, _ := json.Marshal(map[string]any{"type": "response.created", "response": resp})
if rs.write(ctx, b) != nil {
return
}
resp["status"] = "completed"
resp["usage"] = map[string]any{"input_tokens": 0, "output_tokens": 0, "total_tokens": 0}
b, _ = json.Marshal(map[string]any{"type": "response.completed", "response": resp})
if rs.write(ctx, b) == nil {
rs.keep(heldTurn{id: id, items: input})
}
return
}
// the body the HTTP route would have had
delete(q, "type")
delete(q, "previous_response_id")
delete(q, "generate")
in, err := json.Marshal(input)
if err != nil {
rs.writeError(ctx, http.StatusBadRequest, "invalid_request_error", "invalid_value", err.Error(), "input")
return
}
q["input"] = in
body, err := json.Marshal(q)
if err != nil {
rs.writeError(ctx, http.StatusBadRequest, "invalid_request_error", "invalid_json", err.Error(), "")
return
}
rs.sent += int64(len(body))
req := rs.request(ctx, body)
out := &socketTurnWriter{rs: rs, ctx: ctx, run: t, h: http.Header{}}
rs.o.via.ServeHTTP(out, req)
out.finish()
if ctx.Err() != nil {
return
}
if !out.ended {
t.cut = true
return
}
if out.id != "" && out.completed {
rs.keep(heldTurn{id: out.id, items: append(input[:len(input):len(input)], out.items...)})
}
}
// request is the POST a turn stands for: the upgrade's own request, its
// URL, Host, address and headers, with the turn as its body.
func (rs *responsesSocket) request(ctx context.Context, body []byte) *http.Request {
o := rs.o.req
req := o.Clone(ctx)
req.Method = http.MethodPost
req.Header = http.Header{}
for k, vs := range o.Header {
ck := http.CanonicalHeaderKey(k)
if hopHeader(ck) || strings.HasPrefix(ck, "Sec-Websocket-") {
continue
}
switch ck {
case "Content-Type", "Content-Encoding", "Accept", "Accept-Encoding":
continue
case "Openai-Beta":
// the socket's own beta (responses_websockets=…) isn't the turn's
var keep []string
for _, v := range vs {
if !strings.Contains(v, "responses_websockets") {
keep = append(keep, v)
}
}
if len(keep) > 0 {
req.Header[ck] = keep
}
continue
}
req.Header[ck] = append([]string(nil), vs...)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "text/event-stream")
// what Codex says in client_metadata here and in a header over HTTP
// (codexSocketMessage, the other way)
if v := gjson.GetBytes(body, `client_metadata.x-codex-turn-state`).String(); v != "" {
req.Header.Set("X-Codex-Turn-State", v)
}
if strings.EqualFold(gjson.GetBytes(body, `client_metadata.ws_request_header_x_openai_internal_codex_responses_lite`).String(), "true") {
req.Header.Set("X-OpenAI-Internal-Codex-Responses-Lite", "true")
}
req.Body = io.NopCloser(bytes.NewReader(body))
req.GetBody = func() (io.ReadCloser, error) { return io.NopCloser(bytes.NewReader(body)), nil }
req.ContentLength = int64(len(body))
req.Header.Set("Content-Length", strconv.Itoa(len(body)))
req.RequestURI = ""
return req
}
// socketTurnWriter takes the HTTP reply a turn is given and sends it on
// as the socket's frames: each event of a stream as one, an error as an
// error frame.
type socketTurnWriter struct {
rs *responsesSocket
ctx context.Context
run *socketTurnRun
h http.Header
status int
stream bool
pw *io.PipeWriter
read chan struct{}
buf bytes.Buffer // a reply that isn't a stream
// what the reply said
id string
items []json.RawMessage
ended, completed bool
wroteErr bool
}
func (w *socketTurnWriter) Header() http.Header { return w.h }
func (w *socketTurnWriter) WriteHeader(code int) {
if w.status != 0 || code < 200 {
return
}
w.status = code
ct := w.h.Get("Content-Type")
if code/100 == 2 && strings.Contains(ct, "event-stream") {
w.startStream()
}
}
func (w *socketTurnWriter) Write(b []byte) (int, error) {
if w.status == 0 {
if w.h.Get("Content-Type") == "" && (bytes.HasPrefix(b, []byte("event:")) || bytes.HasPrefix(b, []byte("data:"))) {
w.h.Set("Content-Type", "text/event-stream")
}
w.WriteHeader(http.StatusOK)
}
if w.stream {
return w.pw.Write(b)
}
if w.buf.Len() < socketErrMax || w.status/100 == 2 {
w.buf.Write(b)
}
return len(b), nil
}
func (w *socketTurnWriter) Flush() {}
func (w *socketTurnWriter) startStream() {
w.stream = true
for _, f := range w.headerFrames() {
w.send(f)
}
pr, pw := io.Pipe()
w.pw, w.read = pw, make(chan struct{})
go func() {
defer close(w.read)
err := readSSE(pr, func(_, data string) error {
data = strings.TrimSpace(data)
if data == "" || data == "[DONE]" {
return nil
}
return w.event([]byte(data))
})
pr.CloseWithError(err)
if err == nil {
io.Copy(io.Discard, pr)
}
}()
}
// headerFrames say what the HTTP route says in the reply's headers, as
// Codex reads it on a socket: the turn state and the model in
// response.metadata, the model list's tag in codex.response.metadata,
// and the account's windows in codex.rate_limits.
func (w *socketTurnWriter) headerFrames() [][]byte {
var out [][]byte
meta := map[string]string{}
for _, k := range []string{"x-codex-turn-state", "openai-model", "x-reasoning-included"} {
if v := w.h.Get(k); v != "" {
meta[k] = v
}
}
if len(meta) > 0 {
b, _ := json.Marshal(map[string]any{"type": "response.metadata", "headers": meta})
out = append(out, b)
}
if v := w.h.Get("X-Models-Etag"); v != "" {
b, _ := json.Marshal(map[string]any{"type": "codex.response.metadata", "headers": map[string]string{"x-models-etag": v}})
out = append(out, b)
}
if f := codexLimitsFrame(w.h); f != nil {
out = append(out, f)
}
return out
}
// codexLimitsFrame is the codex.rate_limits event the x-codex-* headers
// of a reply say (codexLimitHeaders, the other way), nil for none.
func codexLimitsFrame(h http.Header) []byte {
windows := map[string]any{}
for _, which := range []string{"primary", "secondary"} {
used := h.Get("x-codex-" + which + "-used-percent")
p, err := strconv.ParseFloat(used, 64)
if err != nil {
continue
}
w := map[string]any{"used_percent": p}
if v, err := strconv.ParseInt(h.Get("x-codex-"+which+"-window-minutes"), 10, 64); err == nil {
w["window_minutes"] = v
}
if v, err := strconv.ParseInt(h.Get("x-codex-"+which+"-reset-at"), 10, 64); err == nil {
w["reset_at"] = v
}
windows[which] = w
}
if len(windows) == 0 {
return nil
}
ev := map[string]any{"type": "codex.rate_limits", "rate_limits": windows}
if p := h.Get("x-codex-plan-type"); p != "" {
ev["plan_type"] = p
}
has, unl := h.Get("x-codex-credits-has-credits"), h.Get("x-codex-credits-unlimited")
if has != "" && unl != "" {
cr := map[string]any{"has_credits": strings.EqualFold(has, "true"), "unlimited": strings.EqualFold(unl, "true")}
if b := h.Get("x-codex-credits-balance"); b != "" {
cr["balance"] = b
}
ev["credits"] = cr
}
b, _ := json.Marshal(ev)
return b
}
// event is one event of the reply's stream.
func (w *socketTurnWriter) event(data []byte) error {
var e struct {
Type string `json:"type"`
Response json.RawMessage `json:"response"`
Item json.RawMessage `json:"item"`
}
if json.Unmarshal(data, &e) != nil {
return nil // not JSON: Codex would skip it too
}
switch e.Type {
case "response.created", "response.in_progress":
w.note(gjson.GetBytes(e.Response, "id").String())
case "response.output_item.done":
// in the order they came, as Codex adds them to its side
if len(e.Item) > 0 {
w.items = append(w.items, append(json.RawMessage(nil), e.Item...))
}
case "response.completed":
w.note(gjson.GetBytes(e.Response, "id").String())
w.ended, w.completed = true, true
case "response.incomplete", "response.failed", "response.cancelled", "error":
w.ended = true
case "codex.response.metadata":
// the model list's tag as magpie gives it (modelsEtag), which the
// reply's header has
if v := w.h.Get("X-Models-Etag"); v != "" && gjson.GetBytes(data, "headers.x-models-etag").Exists() {
if b, ok := withModelsEtag(data, v); ok {
data = b
}
}
}
return w.send(data)
}
func (w *socketTurnWriter) note(id string) {
if id == "" {
return
}
w.id = id
w.run.mu.Lock()
for _, have := range w.run.ids {
if have == id {
w.run.mu.Unlock()
return
}
}
w.run.ids = append(w.run.ids, id)
w.run.mu.Unlock()
}
func (w *socketTurnWriter) send(frame []byte) error { return w.rs.write(w.ctx, frame) }
// finish is the end of the handler's reply.
func (w *socketTurnWriter) finish() {
if w.status == 0 {
w.WriteHeader(http.StatusOK)
}
if w.stream {
w.pw.Close()
<-w.read
return
}
if w.ctx.Err() != nil {
return
}
if w.status/100 != 2 {
w.sendHTTPError()
return
}
// a whole reply: what its stream would have said
body := bytes.TrimSpace(w.buf.Bytes())
r := gjson.ParseBytes(body)
if r.Get("object").String() != "response" || r.Get("id").String() == "" {
w.status = http.StatusBadGateway
w.buf.Reset()
w.buf.WriteString(`{"error":{"type":"server_error","message":"the reply was neither a stream nor a response"}}`)
w.sendHTTPError()
return
}
for _, f := range w.headerFrames() {
w.send(f)
}
w.event([]byte(`{"type":"response.created","response":` + string(body) + `}`))
for i, it := range r.Get("output").Array() {
ev, _ := json.Marshal(map[string]any{"type": "response.output_item.done", "output_index": i, "item": json.RawMessage(it.Raw)})
w.event(ev)
}
kind := "response.completed"
switch r.Get("status").String() {
case "incomplete":
kind = "response.incomplete"
case "failed":
kind = "response.failed"
}
w.event([]byte(`{"type":"` + kind + `","response":` + string(body) + `}`))
}
// sendHTTPError is an error reply as Codex reads it on a socket: the
// status, the error object and the headers it acts on (Retry-After, the
// account's x-codex-* windows).
func (w *socketTurnWriter) sendHTTPError() {
w.ended = true
body := bytes.TrimSpace(w.buf.Bytes())
var e json.RawMessage
if v := gjson.GetBytes(body, "error"); v.IsObject() {
e = json.RawMessage(v.Raw)
} else {
msg := string(body)
if !utf8.Valid(body) || msg == "" {
msg = http.StatusText(w.status)
}
e, _ = json.Marshal(map[string]string{"type": "server_error", "message": msg})
}
headers := map[string]string{}
for k := range w.h {
lk := strings.ToLower(k)
if lk == "retry-after" || lk == "retry-after-ms" || strings.HasPrefix(lk, "x-codex-") ||
strings.HasPrefix(lk, "x-ratelimit-") || lk == "x-request-id" || lk == "x-error-json" {
headers[lk] = w.h.Get(k)
}
}
frame := map[string]any{"type": "error", "status": w.status, "error": e}
if len(headers) > 0 {
frame["headers"] = headers
}
b, _ := json.Marshal(frame)
w.send(b)
}
// withModelsEtag is a codex.response.metadata event with its
// x-models-etag set to v, every other member as it was.
func withModelsEtag(data []byte, v string) ([]byte, bool) {
var top map[string]json.RawMessage
var h map[string]json.RawMessage
if json.Unmarshal(data, &top) != nil || json.Unmarshal(top["headers"], &h) != nil {
return nil, false
}
h["x-models-etag"], _ = json.Marshal(v)
top["headers"], _ = json.Marshal(h)
b, err := json.Marshal(top)
return b, err == nil
}
+674
View File
@@ -0,0 +1,674 @@
package gateway
import (
"context"
"fmt"
"io"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"time"
"github.com/coder/websocket"
"github.com/tidwall/gjson"
"github.com/yetone/magpie/internal/access"
"github.com/yetone/magpie/internal/budget"
"github.com/yetone/magpie/internal/codexcat"
"github.com/yetone/magpie/internal/provider"
)
// respUp is a Responses provider that answers each turn with one message
// and a usage of 300 in and 50 out, keeping the bodies it was sent.
type respUp struct {
mu sync.Mutex
bodies [][]byte
head []http.Header
reply func(w http.ResponseWriter, r *http.Request, n int) // nil: the usual
}
func (u *respUp) ServeHTTP(w http.ResponseWriter, r *http.Request) {
b, _ := io.ReadAll(r.Body)
u.mu.Lock()
u.bodies = append(u.bodies, b)
u.head = append(u.head, r.Header.Clone())
n := len(u.bodies)
reply := u.reply
u.mu.Unlock()
if reply != nil {
reply(w, r, n)
return
}
w.Header().Set("Content-Type", "text/event-stream")
id := fmt.Sprintf("resp_up%d", n)
item := fmt.Sprintf(`{"type":"message","id":"msg_%d","role":"assistant","status":"completed","content":[{"type":"output_text","text":"answer %d"}]}`, n, n)
io.WriteString(w, sse(
`data: {"type":"response.created","response":{"id":"`+id+`","object":"response","status":"in_progress","output":[]}}`,
`data: {"type":"response.output_item.done","output_index":0,"item":`+item+`}`,
`data: {"type":"response.completed","response":{"id":"`+id+`","object":"response","status":"completed","output":[`+item+`],"usage":{"input_tokens":300,"output_tokens":50,"total_tokens":350}}}`))
}
func (u *respUp) calls() int {
u.mu.Lock()
defer u.mu.Unlock()
return len(u.bodies)
}
func (u *respUp) body(i int) []byte {
u.mu.Lock()
defer u.mu.Unlock()
return u.bodies[i]
}
// sockUp saves a Responses provider served by u.
func sockUp(t *testing.T, u *respUp) {
t.Helper()
up := httptest.NewServer(u)
t.Cleanup(up.Close)
if err := provider.Save(provider.Provider{ID: "fake", Name: "Fake", Key: "k", Responses: up.URL + "/v1", Models: []string{"m1"}}); err != nil {
t.Fatal(err)
}
}
// sockServer serves the gateway as the real server does (Front), where a
// request's address and Host can be set by the test (X-Test-Remote,
// X-Test-Host): a test's dial comes from 127.0.0.1 to the server's own.
func sockServer(t *testing.T) (*Server, *httptest.Server) {
t.Helper()
s := New()
front := s.Front()
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if v := r.Header.Get("X-Test-Remote"); v != "" {
r.RemoteAddr = v
}
if v := r.Header.Get("X-Test-Host"); v != "" {
r.Host = v
}
r.Header.Del("X-Test-Remote")
r.Header.Del("X-Test-Host")
front.ServeHTTP(w, r)
}))
t.Cleanup(ts.Close)
// a socket's handler outlives ts.Close (hijacked): it ends before
// the test's folders go
t.Cleanup(func() {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
s.sockets.drain(ctx)
})
return s, ts
}
type sockClient struct {
t *testing.T
c *websocket.Conn
}
func dialSock(t *testing.T, ts *httptest.Server, path string, h http.Header) (*sockClient, *http.Response, error) {
t.Helper()
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
c, resp, err := websocket.Dial(ctx, "ws"+strings.TrimPrefix(ts.URL, "http")+path, &websocket.DialOptions{HTTPHeader: h})
if err != nil {
return nil, resp, err
}
c.SetReadLimit(64 << 20)
t.Cleanup(func() { c.CloseNow() })
return &sockClient{t: t, c: c}, resp, nil
}
func (s *sockClient) send(frame string) {
s.t.Helper()
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
if err := s.c.Write(ctx, websocket.MessageText, []byte(frame)); err != nil {
s.t.Fatal(err)
}
}
// until reads frames up to and with one of the given types, or until the
// socket ends (err).
func (s *sockClient) until(kinds ...string) ([]gjson.Result, error) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
var out []gjson.Result
for {
typ, b, err := s.c.Read(ctx)
if err != nil {
return out, err
}
if typ != websocket.MessageText {
s.t.Fatalf("a %v frame", typ)
}
r := gjson.ParseBytes(b)
out = append(out, r)
for _, k := range kinds {
if r.Get("type").String() == k {
return out, nil
}
}
}
}
func (s *sockClient) turn(frame string) []gjson.Result {
s.t.Helper()
s.send(frame)
got, err := s.until("response.completed", "response.failed", "response.incomplete", "error")
if err != nil {
s.t.Fatalf("%v after %v", err, got)
}
return got
}
func last(rs []gjson.Result) gjson.Result { return rs[len(rs)-1] }
const userHi = `{"type":"message","role":"user","content":[{"type":"input_text","text":"hi"}]}`
const userMore = `{"type":"message","role":"user","content":[{"type":"input_text","text":"and more"}]}`
func create(prev, input string, extra ...string) string {
f := `{"type":"response.create","model":"fake/m1","stream":true,"store":false,"instructions":"be brief","input":[` + input + `]`
if prev != "" {
f += `,"previous_response_id":"` + prev + `"`
}
for _, e := range extra {
f += "," + e
}
return f + "}"
}
// Codex's socket is served (#4058): the upgrade is taken on each
// Responses route; a turn naming the reply before it, with only what
// came after, reaches the provider as the whole conversation, the reply
// in its place, and none of the socket's own members. Over HTTP Codex
// sends the conversation whole each turn; magpie answered the upgrade
// 426 (a87dc1ff2), so it never sent less.
func TestResponsesSocketContinues(t *testing.T) {
fresh(t)
for _, path := range []string{"/v1/responses", "/responses"} {
u := &respUp{}
sockUp(t, u)
_, ts := sockServer(t)
c, resp, err := dialSock(t, ts, path, nil)
if err != nil {
code := 0
if resp != nil {
code = resp.StatusCode
}
t.Fatalf("%s: %v (%d)", path, err, code)
}
got := c.turn(create("", userHi))
end := last(got)
if end.Get("type").String() != "response.completed" {
t.Fatalf("%s turn 1: %v", path, got)
}
id := end.Get("response.id").String()
if id == "" || got[0].Get("type").String() != "response.created" {
t.Fatalf("%s turn 1: %v", path, got)
}
var done bool
for _, f := range got {
done = done || f.Get("type").String() == "response.output_item.done"
}
if !done {
t.Errorf("%s: no item: %v", path, got)
}
got = c.turn(create(id, userMore))
if last(got).Get("type").String() != "response.completed" {
t.Fatalf("%s turn 2: %v", path, got)
}
if u.calls() != 2 {
t.Fatalf("%s: %d calls", path, u.calls())
}
b := u.body(1)
in := gjson.GetBytes(b, "input").Array()
if len(in) != 3 || in[0].Get("content.0.text").String() != "hi" || in[1].Get("role").String() != "assistant" ||
in[1].Get("content.0.text").String() != "answer 1" || in[2].Get("content.0.text").String() != "and more" {
t.Errorf("%s turn 2 upstream input: %s", path, b)
}
for _, k := range []string{"type", "previous_response_id", "generate"} {
if gjson.GetBytes(b, k).Exists() {
t.Errorf("%s: %s went upstream: %s", path, k, b)
}
}
if gjson.GetBytes(b, "instructions").String() != "be brief" {
t.Errorf("%s: %s", path, b)
}
// a third turn continues the second
got = c.turn(create(last(got).Get("response.id").String(), userHi))
if last(got).Get("type").String() != "response.completed" || len(gjson.GetBytes(u.body(2), "input").Array()) != 5 {
t.Errorf("%s turn 3: %v %s", path, got, u.body(2))
}
}
}
// A plain GET on a Responses route is still told to POST.
func TestResponsesSocketPlainGET(t *testing.T) {
fresh(t)
sockUp(t, &respUp{})
for _, path := range []string{"/v1/responses", "/responses"} {
rec := httptest.NewRecorder()
New().Handler().ServeHTTP(rec, httptest.NewRequest("GET", path, nil))
if rec.Code != http.StatusMethodNotAllowed || rec.Header().Get("Allow") != "POST" {
t.Errorf("%s GET: %d %q", path, rec.Code, rec.Header().Get("Allow"))
}
}
}
// A reply the socket doesn't hold is previous_response_not_found, on
// which Codex opens a new socket with the conversation whole; nothing goes
// upstream. So is one from another socket: each holds its own.
func TestResponsesSocketUnknownPrevious(t *testing.T) {
fresh(t)
u := &respUp{}
sockUp(t, u)
_, ts := sockServer(t)
a, _, err := dialSock(t, ts, "/v1/responses", nil)
if err != nil {
t.Fatal(err)
}
got := a.turn(create("", userHi))
id := last(got).Get("response.id").String()
b, _, err := dialSock(t, ts, "/v1/responses", nil)
if err != nil {
t.Fatal(err)
}
for _, prev := range []string{"resp_never", id} {
got = b.turn(create(prev, userMore))
e := last(got)
if e.Get("type").String() != "error" || e.Get("status").Int() != 400 || e.Get("error.code").String() != "previous_response_not_found" {
t.Errorf("%s: %v", prev, got)
}
}
if u.calls() != 1 {
t.Errorf("%d calls", u.calls())
}
// the socket goes on
if got = b.turn(create("", userHi)); last(got).Get("type").String() != "response.completed" {
t.Errorf("after: %v", got)
}
}
// A warmup (generate false) is answered without a provider; the turn
// after it names it and sends only what came after, and the provider gets
// the whole.
func TestResponsesSocketWarmup(t *testing.T) {
fresh(t)
u := &respUp{}
sockUp(t, u)
_, ts := sockServer(t)
c, _, err := dialSock(t, ts, "/v1/responses", nil)
if err != nil {
t.Fatal(err)
}
got := c.turn(create("", userHi, `"generate":false`))
if len(got) != 2 || got[0].Get("type").String() != "response.created" || last(got).Get("type").String() != "response.completed" {
t.Fatalf("warmup: %v", got)
}
if u.calls() != 0 {
t.Fatalf("a warmup went upstream")
}
got = c.turn(create(last(got).Get("response.id").String(), userMore))
if last(got).Get("type").String() != "response.completed" || u.calls() != 1 {
t.Fatalf("turn: %v", got)
}
in := gjson.GetBytes(u.body(0), "input").Array()
if len(in) != 2 || in[0].Get("content.0.text").String() != "hi" || in[1].Get("content.0.text").String() != "and more" {
t.Errorf("upstream: %s", u.body(0))
}
}
// A refused turn is an error frame with the HTTP status and error object,
// as Codex reads one; the socket goes on, and the refused turn isn't one a
// later turn can name.
func TestResponsesSocketUpstreamError(t *testing.T) {
fresh(t)
u := &respUp{}
u.reply = func(w http.ResponseWriter, r *http.Request, n int) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("X-Request-Id", "req-bad")
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, `{"error":{"type":"invalid_request_error","code":"context_length_exceeded","message":"too long for this model"}}`)
}
sockUp(t, u)
_, ts := sockServer(t)
c, _, err := dialSock(t, ts, "/v1/responses", nil)
if err != nil {
t.Fatal(err)
}
got := c.turn(create("", userHi))
e := last(got)
if e.Get("type").String() != "error" || e.Get("status").Int() != 400 || !strings.Contains(e.Get("error.message").String(), "too long") {
t.Fatalf("%v", got)
}
u.mu.Lock()
u.reply = nil
u.mu.Unlock()
if got = c.turn(create("", userHi)); last(got).Get("type").String() != "response.completed" {
t.Errorf("after: %v", got)
}
}
// An interrupt of the reply in flight stops it: response.interrupt
// .accepted and an interrupted response.incomplete, as OpenAI's socket
// answers Codex; the provider's request is cancelled, and the socket
// serves the next turn. One naming another reply fails.
func TestResponsesSocketInterrupt(t *testing.T) {
fresh(t)
u := &respUp{}
gone := make(chan struct{}, 1)
u.reply = func(w http.ResponseWriter, r *http.Request, n int) {
w.Header().Set("Content-Type", "text/event-stream")
io.WriteString(w, sse(`data: {"type":"response.created","response":{"id":"resp_long","object":"response","status":"in_progress","output":[]}}`,
`data: {"type":"response.output_item.added","output_index":0,"item":{"type":"message","id":"msg_long","role":"assistant","status":"in_progress","content":[]}}`,
`data: {"type":"response.output_text.delta","output_index":0,"content_index":0,"item_id":"msg_long","delta":"wor"}`))
w.(http.Flusher).Flush()
<-r.Context().Done()
gone <- struct{}{}
}
sockUp(t, u)
_, ts := sockServer(t)
c, _, err := dialSock(t, ts, "/v1/responses", nil)
if err != nil {
t.Fatal(err)
}
c.send(create("", userHi))
if got, err := c.until("response.created"); err != nil || last(got).Get("response.id").String() != "resp_long" {
t.Fatalf("%v %v", got, err)
}
c.send(`{"type":"response.interrupt","response_id":"resp_other","mode":"discard_partial_items"}`)
got, err := c.until("response.interrupt.failed")
if err != nil || last(got).Get("response_id").String() != "resp_other" {
t.Fatalf("%v %v", got, err)
}
c.send(`{"type":"response.interrupt","response_id":"resp_long","mode":"discard_partial_items"}`)
got, err = c.until("response.incomplete")
if err != nil {
t.Fatalf("%v %v", got, err)
}
var accepted bool
for _, f := range got {
accepted = accepted || (f.Get("type").String() == "response.interrupt.accepted" && f.Get("response_id").String() == "resp_long")
}
end := last(got)
if !accepted || end.Get("response.id").String() != "resp_long" || end.Get("response.incomplete_details.reason").String() != "interrupted" {
t.Fatalf("%v", got)
}
select {
case <-gone:
case <-time.After(30 * time.Second):
t.Fatal("the provider's request went on")
}
u.mu.Lock()
u.reply = nil
u.mu.Unlock()
if got = c.turn(create("", userHi)); last(got).Get("type").String() != "response.completed" {
t.Errorf("after: %v", got)
}
}
// A reply cut before its end is a failed turn, as the HTTP route tells
// it; the socket goes on, and the failed reply isn't one to name.
func TestResponsesSocketCutReplyFails(t *testing.T) {
fresh(t)
u := &respUp{}
u.reply = func(w http.ResponseWriter, r *http.Request, n int) {
w.Header().Set("Content-Type", "text/event-stream")
io.WriteString(w, sse(`data: {"type":"response.created","response":{"id":"resp_cut","object":"response","status":"in_progress","output":[]}}`,
`data: {"type":"response.output_text.delta","delta":"par"}`))
}
sockUp(t, u)
_, ts := sockServer(t)
c, _, err := dialSock(t, ts, "/v1/responses", nil)
if err != nil {
t.Fatal(err)
}
got := c.turn(create("", userHi))
if last(got).Get("type").String() != "response.failed" {
t.Fatalf("%v", got)
}
got = c.turn(create("resp_cut", userMore))
if last(got).Get("error.code").String() != "previous_response_not_found" {
t.Errorf("%v", got)
}
}
// Everything that judges an HTTP turn judges each turn on the socket: a
// remote caller needs a key to open it, a key turned off refuses the next
// turn, a key's limit counts each turn; a web page elsewhere, a sandboxed
// frame (Origin null), a localhost subdomain and a rebound name can't open
// one at all.
func TestResponsesSocketGuards(t *testing.T) {
fresh(t)
t.Setenv("MAGPIE_ADDR", "")
t.Setenv("MAGPIE_PUBLIC_URL", "")
budget.Forget()
pinLimitClock(t)
u := &respUp{}
sockUp(t, u)
keys, secrets := newCaller(t, "Laptop", "Capped")
_, ts := sockServer(t)
remote := func(secret string) http.Header {
h := http.Header{"X-Test-Remote": {"192.168.1.9:5000"}}
if secret != "" {
h.Set("Authorization", "Bearer "+secret)
}
return h
}
refused := func(what string, h http.Header) {
t.Helper()
c, resp, err := dialSock(t, ts, "/v1/responses", h)
if err == nil {
// opened: its turn must be refused, never served
got := c.turn(create("", userHi))
t.Errorf("%s opened a socket: %v", what, got)
return
}
if resp == nil || resp.StatusCode < 400 {
t.Errorf("%s: %v %v", what, err, resp)
}
}
before := u.calls()
refused("a remote caller without a key", remote(""))
refused("a remote caller with a wrong key", remote("sk-magpie-wrong"))
for _, origin := range []string{"https://evil.example", "null", "http://evil.localhost:3425"} {
refused("Origin "+origin, http.Header{"Origin": {origin}})
}
refused("a rebound page", http.Header{
"X-Test-Host": {"rebind.attacker.example:3425"},
"User-Agent": {"Mozilla/5.0 (Macintosh) Chrome/140"}, "Sec-Fetch-Site": {"same-origin"}, "Sec-Fetch-Mode": {"websocket"},
})
if u.calls() != before {
t.Fatalf("a refused socket reached the provider")
}
// a key: served, until it is turned off
c, _, err := dialSock(t, ts, "/v1/responses", remote(secrets[0]))
if err != nil {
t.Fatal(err)
}
got := c.turn(create("", userHi))
if last(got).Get("type").String() != "response.completed" {
t.Fatalf("keyed: %v", got)
}
if h := u.head[len(u.head)-1].Get("Authorization"); h != "Bearer k" {
t.Errorf("the provider got %q", h)
}
if _, err := access.Update("off-key", access.Change{Key: keys[0].ID}); err != nil {
t.Fatal(err)
}
got = c.turn(create(last(got).Get("response.id").String(), userMore))
if e := last(got); e.Get("type").String() != "error" || e.Get("status").Int() != http.StatusUnauthorized {
t.Errorf("a key turned off: %v", got)
}
if u.calls() != 1 {
t.Errorf("%d calls", u.calls())
}
// a limit counts each turn: 350 tokens a turn, 700 allowed
setLimit(t, keys[1].ID, &access.Limit{Period: "day", Tokens: 700})
c, _, err = dialSock(t, ts, "/v1/responses", remote(secrets[1]))
if err != nil {
t.Fatal(err)
}
prev := ""
for i := 0; i < 2; i++ {
got = c.turn(create(prev, userHi))
if last(got).Get("type").String() != "response.completed" {
t.Fatalf("turn %d: %v", i, got)
}
prev = last(got).Get("response.id").String()
}
got = c.turn(create(prev, userMore))
if e := last(got); e.Get("type").String() != "error" || e.Get("status").Int() != http.StatusTooManyRequests {
t.Errorf("past the limit: %v", got)
}
if u.calls() != 3 {
t.Errorf("%d calls", u.calls())
}
}
// When the gateway ends, a socket is closed going away after its turn,
// and Codex opens a new one to the gateway serving the port then.
func TestResponsesSocketDrains(t *testing.T) {
fresh(t)
sockUp(t, &respUp{})
s, ts := sockServer(t)
c, _, err := dialSock(t, ts, "/v1/responses", nil)
if err != nil {
t.Fatal(err)
}
if got := c.turn(create("", userHi)); last(got).Get("type").String() != "response.completed" {
t.Fatal(got)
}
// Codex reads all along, the close's answer too
read := make(chan error, 1)
go func() {
_, err := c.until("never")
read <- err
}()
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
s.sockets.drain(ctx)
if ctx.Err() != nil {
t.Fatal("drain waited out")
}
if err := <-read; websocket.CloseStatus(err) != websocket.StatusGoingAway {
t.Errorf("%v", err)
}
if _, resp, err := dialSock(t, ts, "/v1/responses", nil); err == nil || resp == nil || resp.StatusCode != http.StatusServiceUnavailable {
t.Errorf("a socket while the gateway ends: %v %v", err, resp)
}
}
// Codex's own route serves its socket too: each turn goes to the ChatGPT
// backend as Codex's HTTP turn would, the sign-in, the turn state and the
// whole conversation with it, and the reply's headers come back as the
// frames Codex reads them from on a socket (response.metadata,
// codex.response.metadata with magpie's model-list tag, codex.rate_limits).
func TestCodexSocketServed(t *testing.T) {
setup(t, provider.Chat, &fake{t: t})
var mu sync.Mutex
var bodies [][]byte
var heads []http.Header
n := 0
chatgpt(t, func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
t.Errorf("%s %s", r.Method, r.URL.Path)
return
}
b, _ := io.ReadAll(r.Body)
mu.Lock()
bodies = append(bodies, b)
heads = append(heads, r.Header.Clone())
n++
id := fmt.Sprintf("resp_c%d", n)
mu.Unlock()
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("X-Models-Etag", `W/"v1"`)
w.Header().Set("X-Codex-Turn-State", "ts-"+id)
w.Header().Set("X-Codex-Primary-Used-Percent", "12.5")
w.Header().Set("X-Codex-Primary-Window-Minutes", "300")
item := `{"type":"message","role":"assistant","content":[{"type":"output_text","text":"ok"}]}`
io.WriteString(w, sse(
`data: {"type":"response.created","response":{"id":"`+id+`"}}`,
`data: {"type":"codex.response.metadata","headers":{"x-models-etag":"W/\"v1\"","x-other":"kept"}}`,
`data: {"type":"response.output_item.done","output_index":0,"item":`+item+`}`,
`data: {"type":"response.completed","response":{"id":"`+id+`","usage":{"input_tokens":9,"output_tokens":2}}}`))
})
_, ts := sockServer(t)
h := http.Header{"Authorization": {"Bearer chatgpt-token"}, "Chatgpt-Account-Id": {"acct-1"},
"Openai-Beta": {"responses_websockets=2026-02-06"}}
c, resp, err := dialSock(t, ts, CodexPath+"/responses", h)
if err != nil {
code := 0
if resp != nil {
code = resp.StatusCode
}
t.Fatalf("%v (%d)", err, code)
}
frame := func(prev, input string) string {
f := `{"type":"response.create","model":"gpt-5.5","stream":true,"store":false,"input":[` + input + `],` +
`"client_metadata":{"x-codex-turn-state":"ts-prev","ws_request_header_x_openai_internal_codex_responses_lite":"true"}`
if prev != "" {
f += `,"previous_response_id":"` + prev + `"`
}
return f + "}"
}
got := c.turn(frame("", userHi))
if last(got).Get("type").String() != "response.completed" {
t.Fatalf("%v", got)
}
tag := codexcat.Tag(provider.CodexListed())
var meta, etag, limits bool
for _, f := range got {
switch f.Get("type").String() {
case "response.metadata":
meta = f.Get(`headers.x-codex-turn-state`).String() == "ts-resp_c1"
case "codex.response.metadata":
v := f.Get(`headers.x-models-etag`).String()
if !codexcat.Tagged(v, tag) {
t.Errorf("x-models-etag %q", v)
}
etag = etag || f.Get(`headers.x-other`).String() == "kept"
case "codex.rate_limits":
limits = f.Get("rate_limits.primary.used_percent").Float() == 12.5 && f.Get("rate_limits.primary.window_minutes").Int() == 300
}
}
if !meta || !etag || !limits {
t.Errorf("meta %v etag %v limits %v: %v", meta, etag, limits, got)
}
got = c.turn(frame(last(got).Get("response.id").String(), userMore))
if last(got).Get("type").String() != "response.completed" {
t.Fatalf("%v", got)
}
mu.Lock()
defer mu.Unlock()
if len(bodies) != 2 {
t.Fatalf("%d calls", len(bodies))
}
in := gjson.GetBytes(bodies[1], "input").Array()
if len(in) != 3 || in[1].Get("role").String() != "assistant" || gjson.GetBytes(bodies[1], "previous_response_id").Exists() || gjson.GetBytes(bodies[1], "type").Exists() {
t.Errorf("turn 2: %s", bodies[1])
}
hd := heads[1]
if hd.Get("Authorization") != "Bearer chatgpt-token" || hd.Get("Chatgpt-Account-Id") != "acct-1" ||
hd.Get("X-Codex-Turn-State") != "ts-prev" || strings.Contains(hd.Get("Openai-Beta"), "responses_websockets") {
t.Errorf("headers %v", hd)
}
}
// Only Codex's Responses route takes a socket; another of its paths is
// still told 426.
func TestCodexSocketOtherPaths(t *testing.T) {
chatgpt(t, func(w http.ResponseWriter, r *http.Request) { t.Error("sent to ChatGPT") })
rec := httptest.NewRecorder()
req := httptest.NewRequest("GET", CodexPath+"/models", nil)
req.Header.Set("Upgrade", "websocket")
req.Header.Set("Connection", "Upgrade")
New().Handler().ServeHTTP(rec, req)
if rec.Code != http.StatusUpgradeRequired {
t.Errorf("%d", rec.Code)
}
}