Files
magpie/internal/agent/codex_proxy_test.go
T
yetone f156d90460 agent, gui: Codex isn't handed to a magpie it can't reach, through a proxy or after magpie quits (Muma102kMichael, Leon920717, thisisvainxx on X)
Muma102kMichael (Windows): after magpie took Codex over, every project
and every new chat got 502. Codex's reqwest sends its requests for
http://127.0.0.1:3425 through HTTP_PROXY/ALL_PROXY or the system's
proxy, and only an exact IP or a CIDR in NO_PROXY (on Windows, with no
NO_PROXY, the bypass list with "*." removed) keeps 127.0.0.1 off it:
"127.*" and "localhost" don't, and macOS' exceptions aren't read. A
proxy in global mode answers 502 for every request. With [features]
respect_system_proxy Codex asks the system alone and goes direct with
no proxy set there. Account failover, which magpie writes uninvited
when 2+ ChatGPT accounts are on, put all of Codex's traffic, its own
models included, behind that proxy.

- codex_proxy.go works out Codex's proxy the way Codex does and probes
  GET / through it (must answer as magpie). Failover waits for that
  probe and isn't written while the proxy doesn't pass Codex on; one
  written before goes again.
- New drift kind "proxied": the Agents pill says Codex reaches magpie
  through a proxy that doesn't pass its requests on, How to fix names
  the fix (DIRECT rule for 127.0.0.0/8, NO_PROXY=127.0.0.1, or
  127.0.0.1 in Windows' bypass list), and the dialog has a one-click
  Disconnect from magpie (also on unreachable drift).
- Leon920717: after quitting magpie Codex kept retrying. The app that
  serves the gateway now takes failover out in OnShutdown
  (EndCodexFailover); the next start's Sync writes it again. A
  connected Codex is kept (its model is magpie's; Disconnect restores).
  A kill or crash still leaves failover in place.
- thisisvainxx: "several restarts". Codex's app-server keeps the base
  URL it started with, and KeepCodexDaemonCurrent ends with magpie, so
  EndCodexFailover restarts it when no codex session is on it.

Run:
- real codex-cli 0.162.1 in a sandbox CODEX_HOME against a fake 502
  proxy and a fake gateway: NO_PROXY=localhost,127.* -> 30 POSTs to the
  proxy; NO_PROXY=localhost -> proxy; NO_PROXY=127.0.0.1 -> gateway;
  respect_system_proxy with HTTP_PROXY set -> gateway directly.
- go test -tags nogui ./internal/agent/ (full package, 465s, ok) and
  the new tests after the daemon change; each new test went red with
  its guarantee broken by hand (failover gate, drift, EndFailover,
  daemon restart on quit).
- node --test agent-proxied/agent-unreachable/agent-failover/
  agent-disconnect-preview: 104 passed, chromium + webkit, en zh zh-TW
  ja de, window and panel.
- gofmt -l clean; GOOS=linux and GOOS=windows go vet -tags nogui clean.
Not tried: the quit path in the real app, on the Windows box, or
whether computer use / remote connection broke for this same reason.
2026-10-11 23:00:00 +08:00

273 lines
11 KiB
Go

package agent
import (
"context"
"encoding/base64"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"net/url"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/gateway"
"github.com/yetone/magpie/internal/netproxy"
"github.com/yetone/magpie/internal/provider"
)
// proxyAs has codexProxyFor read env, sys and goos, and proxyProbe answer
// with *works, until the test ends.
func proxyAs(t *testing.T, env map[string]string, sys netproxy.Proxy, goos string, works *bool) {
t.Helper()
e, s, o, p := codexProxyEnv, codexSystemProxy, codexProxyOS, proxyProbe
codexProxyEnv = func(k string) string { return env[k] }
codexSystemProxy = func() netproxy.Proxy { return sys }
codexProxyOS = goos
proxyProbe = func(string, string) bool { return works != nil && *works }
reachForget()
t.Cleanup(func() {
codexProxyEnv, codexSystemProxy, codexProxyOS, proxyProbe = e, s, o, p
reachForget()
})
}
// Which proxy Codex sends a request for magpie's 127.0.0.1 through, the way
// its reqwest does (hyper-util's Matcher::from_system) and, with
// respect_system_proxy on, the way codex-rs outbound_proxy does.
func TestCodexProxyFor(t *testing.T) {
const base = "http://127.0.0.1:3425/v1"
clash := "http://127.0.0.1:7890"
plain := filepath.Join(t.TempDir(), "config.toml")
os.WriteFile(plain, []byte("model = \"gpt-5.5\"\n"), 0o600)
respect := filepath.Join(t.TempDir(), "config.toml")
os.WriteFile(respect, []byte("model = \"gpt-5.5\"\n\n[features]\nrespect_system_proxy = true\n"), 0o600)
win := netproxy.Proxy{URL: clash, Bypass: []string{"localhost", "127.*", "<local>"}}
cases := []struct {
name string
cfg string
env map[string]string
sys netproxy.Proxy
goos string
want string
}{
{"none", plain, nil, netproxy.Proxy{}, "darwin", ""},
{"HTTP_PROXY", plain, map[string]string{"HTTP_PROXY": clash}, netproxy.Proxy{}, "darwin", clash},
{"https_proxy only", plain, map[string]string{"HTTPS_PROXY": clash}, netproxy.Proxy{}, "darwin", ""},
{"ALL_PROXY", plain, map[string]string{"ALL_PROXY": clash}, netproxy.Proxy{}, "linux", clash},
{"NO_PROXY 127.0.0.1", plain, map[string]string{"HTTP_PROXY": clash, "NO_PROXY": "localhost,127.0.0.1"}, netproxy.Proxy{}, "darwin", ""},
{"NO_PROXY CIDR", plain, map[string]string{"HTTP_PROXY": clash, "NO_PROXY": "127.0.0.0/8"}, netproxy.Proxy{}, "darwin", ""},
{"NO_PROXY localhost is not 127.0.0.1", plain, map[string]string{"HTTP_PROXY": clash, "NO_PROXY": "localhost"}, netproxy.Proxy{}, "darwin", clash},
{"NO_PROXY * is names only", plain, map[string]string{"HTTP_PROXY": clash, "NO_PROXY": "*"}, netproxy.Proxy{}, "darwin", clash},
// the reporter's: Windows' proxy on, with 127.* in its bypass list,
// which reqwest doesn't read as 127.0.0.1
{"Windows 127.*", plain, nil, win, "windows", clash},
{"Windows 127.0.0.1", plain, nil, netproxy.Proxy{URL: clash, Bypass: []string{"127.0.0.1"}}, "windows", ""},
{"Windows NO_PROXY wins over its list", plain, map[string]string{"NO_PROXY": "example.com"}, netproxy.Proxy{URL: clash, Bypass: []string{"127.0.0.1"}}, "windows", clash},
{"macOS exceptions not read", plain, nil, netproxy.Proxy{URL: clash, Bypass: []string{"127.0.0.1"}}, "darwin", clash},
{"respect: Windows 127.* goes around", respect, nil, win, "windows", ""},
{"respect: system proxy", respect, nil, netproxy.Proxy{URL: clash, Bypass: []string{"*.local"}}, "darwin", clash},
// the system says direct, and the environment isn't asked (codex-cli
// 0.162.1 with HTTP_PROXY set sent nothing to it)
{"respect: no system proxy, env not read", respect, map[string]string{"HTTP_PROXY": clash}, netproxy.Proxy{}, "darwin", ""},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
proxyAs(t, c.env, c.sys, c.goos, nil)
if got, from := codexProxyFor(c.cfg, base); got != c.want || (got != "" && from == "") {
t.Fatalf("got %q from %q, want %q", got, from, c.want)
}
})
}
}
// A proxy's own 502 isn't magpie; a proxy that passes the request on is.
func TestRealProxyProbe(t *testing.T) {
gw := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, `{"name":"magpie"}`)
}))
defer gw.Close()
dead := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "upstream", http.StatusBadGateway)
}))
defer dead.Close()
pass := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
u, _ := url.Parse(r.RequestURI)
res, err := http.Get(u.String())
if err != nil {
http.Error(w, err.Error(), http.StatusBadGateway)
return
}
defer res.Body.Close()
w.WriteHeader(res.StatusCode)
io.Copy(w, res.Body)
}))
defer pass.Close()
if realProxyProbe(gw.URL+"/v1", dead.URL) {
t.Fatal("a proxy answering 502 counted as reaching magpie")
}
if !realProxyProbe(gw.URL+"/v1", pass.URL) {
t.Fatal("a proxy passing the request on didn't count")
}
}
// Muma102kMichael on X: Codex 502 on every project and every new chat once
// magpie took it over, its requests for 127.0.0.1 sent through a proxy that
// didn't pass them on. Failover, which magpie writes uninvited, is never
// written then, and one written before goes again; once the proxy passes
// them on, it is.
func TestCodexFailoverHeldWhileProxyDoesNotPassOn(t *testing.T) {
claims := func(m map[string]any) string {
b, _ := json.Marshal(m)
return "h." + base64.RawURLEncoding.EncodeToString(b) + ".s"
}
auth := func(email, acct string) map[string]any {
return map[string]any{"auth_mode": "chatgpt", "tokens": map[string]any{
"id_token": claims(map[string]any{"email": email}),
"access_token": claims(map[string]any{"exp": time.Now().Add(time.Hour).Unix()}),
"refresh_token": "r-" + acct, "account_id": acct}}
}
me, _ := json.Marshal(auth("me@example.com", "acct-1"))
home, read := codexHome(t, string(me), "model = \"gpt-5.5\"\n")
b, _ := json.Marshal([]map[string]any{{"agent": "codex", "user": "spare@example.com", "on": true,
"seen": time.Now(), "auth": auth("spare@example.com", "acct-2")}})
os.MkdirAll(filepath.Dir(provider.Path()), 0o755)
os.WriteFile(filepath.Join(filepath.Dir(provider.Path()), "logins.json"), b, 0o600)
works := false
proxyAs(t, nil, netproxy.Proxy{URL: "http://127.0.0.1:7890", Bypass: []string{"localhost", "127.*"}}, "windows", &works)
cx := codex(home)
sync := func() string {
t.Helper()
reachForget()
if err := cx.Sync(); err != nil {
t.Fatal(err)
}
return read()
}
if cfg := sync(); strings.Contains(cfg, "openai_base_url") {
t.Fatalf("written while the proxy doesn't pass it on:\n%s", cfg)
}
works = true
if cfg := sync(); !strings.Contains(cfg, `openai_base_url = "`+gateway.URL()) {
t.Fatalf("not written once the proxy passes it on:\n%s", cfg)
}
works = false
if cfg := sync(); strings.Contains(cfg, "openai_base_url") || !strings.Contains(cfg, `model = "gpt-5.5"`) {
t.Fatalf("kept once the proxy stopped passing it on:\n%s", cfg)
}
}
// Connected to magpie, with the gateway up but Codex's way to it through a
// proxy that answers 502: the row says so, which proxy and where it is set,
// rather than connected (kind "proxied"); once the proxy passes it on, or
// 127.0.0.1 goes around it, connected again.
func TestCodexProxiedDrift(t *testing.T) {
fakeReach(t, map[string]bool{gateway.URL(): true})
home, _ := codexAt(t, gateway.URL())
works := false
env := map[string]string{"HTTP_PROXY": "http://127.0.0.1:7890"}
proxyAs(t, env, netproxy.Proxy{}, "darwin", &works)
d := driftSettled(t, codex(home))
if d == nil || d.Kind != "proxied" || d.Addr != "http://127.0.0.1:7890" {
t.Fatalf("drift %+v", d)
}
if !strings.Contains(d.Detail, "HTTP_PROXY") || !strings.Contains(d.Detail, "NO_PROXY") || !strings.Contains(d.Detail, "Disconnect") {
t.Fatalf("detail: %s", d.Detail)
}
works = true
reachForget()
if d := driftSettled(t, codex(home)); d != nil {
t.Fatalf("proxy passes it on: %+v", d)
}
works = false
env["NO_PROXY"] = "127.0.0.1"
reachForget()
if d := driftSettled(t, codex(home)); d != nil {
t.Fatalf("127.0.0.1 around the proxy: %+v", d)
}
}
// Leon920717 on X: with magpie quit, Codex CLI kept retrying the gateway
// till magpie was open again. The failover base URL magpie wrote goes as
// the magpie serving the gateway quits (EndCodexFailover), and the next
// start's Sync writes it again; a base URL the user wrote stays.
func TestCodexFailoverEndsWithMagpie(t *testing.T) {
claims := func(m map[string]any) string {
b, _ := json.Marshal(m)
return "h." + base64.RawURLEncoding.EncodeToString(b) + ".s"
}
auth := func(email, acct string) map[string]any {
return map[string]any{"auth_mode": "chatgpt", "tokens": map[string]any{
"id_token": claims(map[string]any{"email": email}),
"access_token": claims(map[string]any{"exp": time.Now().Add(time.Hour).Unix()}),
"refresh_token": "r-" + acct, "account_id": acct}}
}
me, _ := json.Marshal(auth("me@example.com", "acct-1"))
home, read := codexHome(t, string(me), "model = \"gpt-5.5\"\n")
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
logins := func(on bool) {
b, _ := json.Marshal([]map[string]any{{"agent": "codex", "user": "spare@example.com", "on": on,
"seen": time.Now(), "auth": auth("spare@example.com", "acct-2")}})
os.MkdirAll(filepath.Dir(provider.Path()), 0o755)
os.WriteFile(filepath.Join(filepath.Dir(provider.Path()), "logins.json"), b, 0o600)
}
logins(true)
cx := codex(home)
if err := cx.Sync(); err != nil {
t.Fatal(err)
}
if cfg := read(); !strings.Contains(cfg, "openai_base_url") {
t.Fatalf("failover not written:\n%s", cfg)
}
wasKeep := keepCodexDaemon
t.Cleanup(func() { keepCodexDaemon = wasKeep })
var asked []string
keepCodexDaemon = func(_ context.Context, h string, _ time.Time) provider.CodexDaemonCheck {
asked = append(asked, h)
return provider.CodexDaemonCheck{}
}
if err := EndCodexFailover(); err != nil {
t.Fatal(err)
}
if cfg := read(); strings.Contains(cfg, "openai_base_url") || !strings.Contains(cfg, `model = "gpt-5.5"`) {
t.Fatalf("magpie quit, Codex still pointed at it:\n%s", cfg)
}
// the app-server keeps the base URL it started with: asked to restart
if len(asked) != 1 || asked[0] != filepath.Dir(cx.Path) {
t.Fatalf("app-server not brought up to date on quit: %q", asked)
}
asked = nil
if err := cx.Sync(); err != nil {
t.Fatal(err)
}
if cfg := read(); !strings.Contains(cfg, "openai_base_url") {
t.Fatalf("not written again on the next start:\n%s", cfg)
}
// the user's own base URL, with no failover on, stays
logins(false)
if err := cx.Sync(); err != nil {
t.Fatal(err)
}
base := gateway.URL() + gateway.CodexPath
if err := edit.SetTOMLTop(cx.Path, edit.KV{Path: "openai_base_url", Value: base}); err != nil {
t.Fatal(err)
}
if err := EndCodexFailover(); err != nil {
t.Fatal(err)
}
if cfg := read(); !strings.Contains(cfg, `openai_base_url = "`+base+`"`) {
t.Fatalf("the user's base URL was taken:\n%s", cfg)
}
if len(asked) != 0 {
t.Fatalf("app-server restarted with nothing changed: %q", asked)
}
}