Files
magpie/go.mod
T
yetone a2e98cd799 gateway: a streamed Codex turn goes to ChatGPT over its WebSocket (brickhao)
brickhao on Discord: long Codex turns through magpie died with "stream
closed before response.completed". Their network (FlClash) cuts a
streamed HTTP reply from chatgpt.com at about 120 s: 120.7 s through
magpie, 121.5 s over HTTP without magpie. Codex's own WebSocket to the
same backend ran to the end twice (124.5 s, 131.4 s). magpie has no
120 s timer of its own (no WriteTimeout, 10 min to the first header).
The cut is on the leg from magpie to chatgpt.com, which was always
HTTP. Codex answers magpie's 426 by falling back to HTTP, and over
loopback that is harmless.

What changed: codexSocket, first in upstreamTransport, sends a POST to
CodexBase/responses with "stream":true over the backend's WebSocket, as
codex-rs does (ResponseCreateWsRequest, build_websocket_headers).
- Handshake: the turn's headers plus OpenAI-Beta:
  responses_websockets=2026-02-06. x-codex-turn-state and the
  responses-lite header go in client_metadata instead. The message is
  the body (zstd decoded) with "type":"response.create" ahead of it.
- Frames come back as the SSE the HTTP route sends.
  codex.rate_limits and codex.response.metadata are also given as the
  x-codex-* and x-codex-turn-state headers. websocket_timing is dropped.
- An error frame ahead of the reply becomes its HTTP status and body,
  so account rests and failover work as before.
- These go over HTTP: a refused handshake (and the host stays on HTTP
  for 10 min, unless the refusal was 401/429/5xx),
  websocket_connection_limit_reached, and a body that isn't a streamed
  turn.
- A socket closed before the end reads as a cut stream.
- Both Codex's own sign-in passthrough and magpie's ChatGPT accounts
  take this path. Codex identity, instructions and prompts are
  unchanged. coder/websocket is now a direct dependency (it was
  indirect).

Run:
- gofmt -l clean; go vet darwin, GOOS=linux -tags nogui, GOOS=windows ok.
- go test -race -count=3 of the five new tests: ok.
- go test -timeout 45m at 1a5911612 (rebased on origin/main c551a2299),
  sandbox HOME: internal/gateway ok 760.9s, internal/agent ok 596.8s,
  internal/provider ok 172.5s. The rest of ./... was not run before the push.
- Real: a sandbox magpie (this tree, 127.0.0.1:3488, sandbox HOME and
  CODEX_HOME, a copy of the Codex access token with the refresh token
  blanked so nothing could rotate). A curl turn on gpt-6-luna came back
  200 with codex.rate_limits in the SSE (which only the socket sends)
  and the x-codex-* headers. Real Codex CLI 0.162.1 (codex exec,
  xhigh) through it: a 6121-word reply over 165 s ended whole, exit 0.
  Codex logged its own 426 from magpie and fell back, as expected.

Broken by hand:
- socket off in the tests: all four socket tests red (the reply cut
  before response.completed, no handshake, 200 not 429).
- error-frame mapping off: TestCodexSocketErrorIsItsStatus red.
- turn-state into client_metadata off: TestCodexTurnGoesOverWebSocket
  red.

Not tried:
- brickhao's network: this network doesn't cut at 120 s, so the 165 s
  turn shows the socket carries a long turn whole, not that it gets
  past their cut.
- A real turn on a magpie-saved ChatGPT account (fake only).
- Through an HTTP or SOCKS proxy for real.
- Windows/Linux runs.
2026-10-23 23:59:55 +08:00

60 lines
2.3 KiB
AMPL

module github.com/yetone/magpie
go 1.26.8
require (
github.com/Calcium-Ion/moejs v0.1.0-alpha.4
github.com/charmbracelet/bubbles v0.21.1
github.com/charmbracelet/bubbletea v1.3.10
github.com/charmbracelet/lipgloss v1.1.1-0.20250404203927-76690c660834
github.com/coder/websocket v1.8.14
github.com/go-ole/go-ole v1.3.0
github.com/godbus/dbus/v5 v5.2.2
github.com/google/uuid v1.6.0
github.com/klauspost/compress v1.18.3
github.com/pelletier/go-toml/v2 v2.2.4
github.com/tidwall/gjson v1.19.0
github.com/tidwall/jsonc v0.3.3
github.com/wailsapp/wails/v3 v3.0.0-beta.24
golang.org/x/net v0.56.0
golang.org/x/sys v0.46.0
golang.org/x/term v0.44.0
golang.org/x/text v0.39.0
gopkg.in/yaml.v3 v3.0.1
modernc.org/libc v1.67.6
modernc.org/sqlite v1.44.3
)
require (
git.sr.ht/~jackmordaunt/go-toast/v2 v2.0.3 // indirect
github.com/adrg/xdg v0.5.3 // indirect
github.com/atotto/clipboard v0.1.4 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/charmbracelet/colorprofile v0.4.1 // indirect
github.com/charmbracelet/x/ansi v0.11.5 // indirect
github.com/charmbracelet/x/cellbuf v0.0.15 // indirect
github.com/charmbracelet/x/term v0.2.2 // indirect
github.com/clipperhouse/displaywidth v0.9.0 // indirect
github.com/clipperhouse/stringish v0.1.1 // indirect
github.com/clipperhouse/uax29/v2 v2.5.0 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
github.com/lucasb-eyer/go-colorful v1.3.0 // indirect
github.com/mattn/go-colorable v0.1.14 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
github.com/mattn/go-runewidth v0.0.19 // indirect
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect
github.com/muesli/cancelreader v0.2.2 // indirect
github.com/muesli/termenv v0.16.0 // indirect
github.com/ncruces/go-strftime v1.0.0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
golang.org/x/exp v0.0.0-20260410095643-746e56fc9e2f // indirect
modernc.org/mathutil v1.7.1 // indirect
modernc.org/memory v1.11.0 // indirect
)