Files
magpie/internal/gateway/codex_relay_test.go
T
yetone 4534561f6b codex: a thread open when magpie quits goes on through a relay, not a 503
idkatbro on X: a Codex session started while magpie was open keeps
failing after magpie quits with "unexpected status 503 Service
Unavailable: Unknown error, url:
http://127.0.0.1:3425/backend-api/codex/responses", until magpie is
opened again.

Cause: Codex signed in to ChatGPT, with more of its accounts on in
magpie, gets openai_base_url = the gateway's /backend-api/codex
(failover). At quit EndCodexFailover takes it out of config.toml, but
a thread already open, in the Codex app and its app-server above all,
keeps the base URL it started with. Every turn after went to a closed
port. With a system or env proxy in between, the proxy answers 503
with an empty body, which Codex shows as "Unknown error"; without one
Codex says "Connection refused" and reconnects. Reproduced on mote
with codex 0.162.1 and a proxy answering 503 for the closed port.
Masking plays no part: it runs only in the gateway, is opt-in, and its
own 503 carries a readable message.

Fix: when quit really ends failover (EndCodexFailover now says so),
the gateway is on loopback, and Codex processes run
(provider.CodexPIDs), magpie starts a detached `magpie codex-relay`
(internal/codexrelay). It waits up to 60s for the gateway's port, then
passes /backend-api/codex/* on to the ChatGPT backend with the same
transforms the gateway applies to Codex's own turns, signed by the
caller's own Authorization; it adds no credential
(gateway.CodexRelay).
- Refused with a readable 400 (Codex retries a 5xx five times over):
  a magpie model, a turn naming one of magpie's Codex accounts, and a
  turn masking would have masked, which is never sent unmasked.
- 403 for any Origin (null too) or a non-loopback Host, as corsGuard.
- Any other path gets 503 "magpie isn't running", never readable as
  magpie by ServedBy.
- It ends once every Codex process it was left for is gone and
  nothing is in flight.
- The next magpie (GUI or `magpie serve`) calls codexrelay.Leave
  first: a POST with the token from a 0600 state file. The relay
  closes its listener at once, so the port is free when Leave returns,
  and finishes its streams (up to 30 minutes). No running turn is cut.

Run (at 6df7dc287 on origin/main 4d9058a98, on mote):
- go vet of the six touched packages; gofmt -l clean.
- go test ./internal/codexrelay and the CodexRelay, CodexFailover,
  CodexProxy, RelayOpenCodex and CodexPIDs tests: ok.
- codexrelay tests on Windows (win-run) and Linux (wsl-run,
  magpie-desktop): ok.
- Windows smoke of the real binary (nogui build): `codex-relay`
  answers / with the readable 503, stays while its watched process
  lives, ends after it.
- Real codex 0.162.1 e2e on mote, sandbox CODEX_HOME, API-key sign-in,
  fake upstream: the relay waited out a port held 2s, then turn 1
  completed (RELAYED-OK); a turn on openrouter/kimi-k2 showed the
  refusal's message; turn 2 was mid-stream when Leave ran, the port
  was free at once, the turn completed, and the relay ended.
- GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./... and a
  Windows build: ok.
- Full suite, go test -count=1 -timeout 45m ./... on mote: every
  package ok but internal/gui, where 10 real-window AppKit tests
  (dock, panel, tray, titlebar, covered window, plugin page) are
  killed at their 20-80s bars. The same 10 fail the same way at
  origin/main 4d9058a98 on mote, run alone: mote's AppKit hang, #4125
  (and #2645, #1936). None of them touches the quit path changed here.
- Rebased on origin/main 1198cff60 to push. Still running there when
  pushed: go vet ./..., Windows and Linux vet of the touched packages,
  and go test of codexrelay, gateway, agent, provider and cli. A red
  it shows gets its own issue.

Broken by hand, each red, then restored:
- Leave without ln.Close(), with a 1s delay before Shutdown injected:
  "the port isn't free after Leave" (with ln.Close: green).
- No pid watch: "relay outlived the Codex it was left for".
- Leave without the token check: an empty token got 204.
- No Origin/Host guard: Origin null got 200.
- No masking refusal, no magpie-model refusal: 200, sent upstream.
- An empty 503 at "/": TestCodexRelayIsNotMagpie red.
- RelayOpenCodex starting nothing: "no relay for the open Codex".
- EndCodexFailover never saying ended: TestCodexFailoverEndsWithMagpie.
- CodexPIDs missing "codex": the Codex app and CLI not found.

Not tried:
- the real Codex desktop app, and a real ChatGPT account. With a fake
  ChatGPT token codex 0.162.1 stops at its own "workspace routing
  discovery" against chatgpt.com before sending a turn, so the
  ChatGPT path is covered by unit tests only;
- a Codex in a WSL distro, and a connected (not failover) Codex: both
  left as they were;
- a magpie that ends without quitting (killed, crashed): still leaves
  failover in place and no relay;
- the relay's memory use as the full binary.
2026-10-29 17:13:05 +08:00

186 lines
6.8 KiB
Go

package gateway
import (
"bytes"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/settings"
)
// relayUp is a fake ChatGPT backend for the relay: what it was sent.
type relayUp struct {
mu sync.Mutex
got []relayGot
srv *httptest.Server
code int
}
type relayGot struct {
path, auth, body, encoding string
}
func newRelayUp(t *testing.T) *relayUp {
t.Helper()
up := &relayUp{code: 200}
up.srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, _ := io.ReadAll(r.Body)
up.mu.Lock()
up.got = append(up.got, relayGot{r.URL.Path, r.Header.Get("Authorization"), string(b), r.Header.Get("Content-Encoding")})
up.mu.Unlock()
w.Header().Set("Content-Type", "text/event-stream")
w.WriteHeader(up.code)
io.WriteString(w, "event: response.completed\ndata: {\"type\":\"response.completed\"}\n\n")
}))
t.Cleanup(up.srv.Close)
was := provider.CodexBase
provider.CodexBase = up.srv.URL + "/backend-api/codex"
t.Cleanup(func() { provider.CodexBase = was })
return up
}
func (up *relayUp) sent() []relayGot {
up.mu.Lock()
defer up.mu.Unlock()
return append([]relayGot(nil), up.got...)
}
func relayDo(h http.Handler, method, host, path, body string, hdr map[string]string) *httptest.ResponseRecorder {
req := httptest.NewRequest(method, path, strings.NewReader(body))
req.Host = host
for k, v := range hdr {
req.Header.Set(k, v)
}
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
return rec
}
// A Codex thread open when magpie quit keeps sending its turns to the
// gateway's address (idkatbro on X: 503 "Unknown error" until magpie was
// opened again). The relay passes one on Codex's own model to the ChatGPT
// backend with the caller's own sign-in, and its reply comes back whole.
func TestCodexRelayPassesOwnTurnOn(t *testing.T) {
fresh(t)
up := newRelayUp(t)
h := CodexRelay()
rec := relayDo(h, "POST", "127.0.0.1:3425", CodexPath+"/responses", `{"model":"gpt-5.5","input":"hi","stream":true}`,
map[string]string{"Authorization": "Bearer chatgpt-token", "Content-Type": "application/json"})
if rec.Code != 200 || !strings.Contains(rec.Body.String(), "response.completed") {
t.Fatalf("%d %s", rec.Code, rec.Body.String())
}
got := up.sent()
if len(got) != 1 || got[0].path != "/backend-api/codex/responses" || got[0].auth != "Bearer chatgpt-token" {
t.Fatalf("upstream was sent %+v", got)
}
// the model list Codex asks for, and localhost by name, go on too
rec = relayDo(h, "GET", "localhost:3425", CodexPath+"/models?client_version=0.162.1", "", map[string]string{"Authorization": "Bearer chatgpt-token"})
if rec.Code != 200 || len(up.sent()) != 2 || up.sent()[1].path != "/backend-api/codex/models" {
t.Fatalf("%d %+v", rec.Code, up.sent())
}
}
// What only the running magpie serves is refused with a reason Codex
// shows at once (a 4xx; a 5xx it retries five times over), and nothing
// goes to OpenAI: a magpie model, a turn on one of
// magpie's accounts, and one masking would have masked, never sent
// unmasked.
func TestCodexRelayRefusesWhatNeedsMagpie(t *testing.T) {
fresh(t)
up := newRelayUp(t)
h := CodexRelay()
auth := map[string]string{"Authorization": "Bearer chatgpt-token"}
for _, c := range []struct {
name, body string
hdr map[string]string
mask bool
want string
}{
{"magpie model", `{"model":"openrouter/kimi-k2","input":"hi"}`, auth, false, "one of magpie's models"},
{"magpie account", `{"model":"gpt-5.5","input":"hi"}`, map[string]string{"Authorization": "Bearer t", AccountHeader: "a@b.c"}, false, "magpie's Codex accounts"},
{"masking", `{"model":"gpt-5.5","input":"DB_PASSWORD=hunter2"}`, auth, true, "not sent unmasked"},
} {
if err := settings.Save(settings.Settings{Redact: c.mask}); err != nil {
t.Fatal(err)
}
rec := relayDo(h, "POST", "127.0.0.1:3425", CodexPath+"/responses", c.body, c.hdr)
var e struct {
Error struct{ Message string } `json:"error"`
}
json.Unmarshal(rec.Body.Bytes(), &e)
if rec.Code != http.StatusBadRequest || !strings.Contains(e.Error.Message, c.want) || !strings.Contains(e.Error.Message, "magpie isn't running") {
t.Errorf("%s: %d %s", c.name, rec.Code, rec.Body.String())
}
}
if got := up.sent(); len(got) != 0 {
t.Fatalf("OpenAI was sent %+v", got)
}
}
// Only Codex on this computer is served: a page (any Origin, null too)
// and a name rebound to 127.0.0.1 are turned away before anything goes
// on, as corsGuard turns them from the gateway.
func TestCodexRelayOnlyThisComputer(t *testing.T) {
fresh(t)
up := newRelayUp(t)
h := CodexRelay()
body := `{"model":"gpt-5.5","input":"hi"}`
for _, c := range []struct{ host, origin string }{
{"127.0.0.1:3425", "null"},
{"127.0.0.1:3425", "https://evil.example"},
{"evil.example:3425", ""},
{"magpie.localhost:3425", ""},
} {
hdr := map[string]string{"Authorization": "Bearer chatgpt-token"}
if c.origin != "" {
hdr["Origin"] = c.origin
}
if rec := relayDo(h, "POST", c.host, CodexPath+"/responses", body, hdr); rec.Code != 403 {
t.Errorf("%+v: %d %s", c, rec.Code, rec.Body.String())
}
}
if got := up.sent(); len(got) != 0 {
t.Fatalf("OpenAI was sent %+v", got)
}
if rec := relayDo(h, "POST", "[::1]:3425", CodexPath+"/responses", body, map[string]string{"Authorization": "Bearer t"}); rec.Code != 200 {
t.Fatalf("::1: %d %s", rec.Code, rec.Body.String())
}
}
// Anything but Codex's turns says magpie isn't running, readably; and
// the relay's answer at "/" is never taken for magpie's (ServedBy), so
// the next magpie starting still serves the gateway. An OpenAI that
// can't be reached is said too, never an empty body Codex shows as
// "Unknown error".
func TestCodexRelayIsNotMagpie(t *testing.T) {
fresh(t)
newRelayUp(t)
h := CodexRelay()
for _, p := range []string{"/", "/v1/models", "/v1/chat/completions"} {
rec := relayDo(h, "GET", "127.0.0.1:3425", p, "", nil)
b := rec.Body.Bytes()
var info struct {
Name string `json:"name"`
}
json.Unmarshal(b, &info)
if rec.Code != 503 || info.Name == "magpie" || bytes.Contains(b, []byte(`"magpie"`)) || !strings.Contains(string(b), "magpie isn't running") {
t.Errorf("%s: %d %s", p, rec.Code, b)
}
}
provider.CodexBase = "http://127.0.0.1:1/backend-api/codex"
rec := relayDo(h, "POST", "127.0.0.1:3425", CodexPath+"/responses", `{"model":"gpt-5.5","input":"hi"}`, map[string]string{"Authorization": "Bearer t"})
if rec.Code != 502 || !strings.Contains(rec.Body.String(), "OpenAI can't be reached") {
t.Fatalf("%d %s", rec.Code, rec.Body.String())
}
// a WebSocket is sent to plain HTTP, as the gateway does
if rec := relayDo(h, "GET", "127.0.0.1:3425", CodexPath+"/responses", "", map[string]string{"Upgrade": "websocket"}); rec.Code != http.StatusUpgradeRequired {
t.Fatalf("websocket: %d", rec.Code)
}
}