Commit Graph
5 Commits
Author SHA1 Message Date
yetone df3734b959 proc: the login shell's variables count only when exported, and Codex's proxy detail names the variable it read (#2627)
ReneXiong: Codex showed the "proxied" drift, "through the proxy at
http://127.0.0.1:7890, set in HTTP_PROXY", while Codex went direct. The
value was fish's universal http_proxy from an old Clash setup, never
exported (`set -S` says "unexported"), so no program fish starts has it.
shellEnv asks fish beside the login shell (zsh), and shellProbe printed
"$http_proxy" in the shell itself, which expands unexported variables too;
mergeShellEnv then filled zsh's empty value with fish's.

Fix at the shared reader: every shell is now asked to run
/bin/sh -c 'printf ... "$PATH" "$CODEX_HOME" ...', the form nushell already
used (still with its ^). A child sees only exported variables, so each
value is what an agent started from that shell gets. This covers every
reader of the probe: Codex's proxy (codexEnv), Claude Code's provider
switches (claudeShellEnv), and agentenv.Vars such as CODEX_HOME and
CLAUDE_CONFIG_DIR that magpie takes into its own environment; an
unexported zsh or bash variable no longer counts either, which matches what
an agent run from that terminal sees. Proxy variables are still merged from
fish when the login shell has none: a fish user who exports one runs Codex
with it from fish, so taking proxies from the login shell only (the
report's other suggestion) would miss that case.

codexProxyFor named HTTP_PROXY (or HTTPS_PROXY, ALL_PROXY) for a value in
the lowercase variable. It now names the one it read, in both the reqwest
and respect_system_proxy paths, and the detail's NO_PROXY advice matches a
lowercase name too (and not "the system's proxy settings").

Other proxy readers checked: the daemon's carried env, wslrun's proxyVars,
netproxy and the plugin host read magpie's own environment, not a shell;
the WSL probe scripts ask for no proxy variable.

Tests: TestLoginEnvUnexportedNotCounted, a login shell with the reporter's
literal values unexported beside an exported NO_PROXY; TestShellEnvRealFish
now writes the reporter's literal fish_variables lines and checks that
http_proxy, https_proxy and all_proxy read empty;
TestCodexProxyNamesItsVariable. Broken by hand, each went red: the old
in-shell printf (unexported values read back, in the fake shell and in real
fish 3.7.1), and env() naming keys[0].

Run: go test ./internal/proc and the codex proxy tests on darwin;
TestShellEnvRealFish, TestShellEnvFishLogin and
TestLoginEnvUnexportedNotCounted on real fish 3.7.1 in an alpine container
(linux/arm64), green with the fix and red without it; gofmt -l clean; go vet
-tags nogui ./... for darwin, linux and windows; the full go test -tags
nogui -timeout 45m ./... under pipefail, both fixes together on
origin/main ec6454395: exit 0, 66 packages ok, no FAIL.

Not tried: the reporter's Mac with fish itself, and nushell (not installed
here; TestShellEnvNushell's real-nu part skipped, its probe-shape check
passes).
2026-10-17 05:35:45 +08:00
yetone ffd0778696 agent: an agent reaching magpie through a reverse proxy isn't drift, and a sync keeps the proxy's path (#1985)
davisjiahao put magpie behind a company nginx (https://ai.corp.example/magpie)
and pointed their agents there with magpie's key. Every agent whose Check
compares its URL exactly called that "unwired", offered Reconnect (which
writes 127.0.0.1 back), and each start's sync replaced the origin but
dropped the /magpie path.

Decision: a URL that isn't magpie's is still magpie when it ends with
magpie's path, the rest is an http/https address on another machine, and
GET <rest>/ answers as the gateway's GET / ({"name":"magpie"}). Something
else there (a relay CC Switch wrote) stays drift. Loopback on another port
is magpie on an older port: drift, never probed. An unknown answer is not
drift (as unreachable/proxied), so Reconnect isn't offered before the
probe has answered.

- reach.go: magpieAt / realMagpieProbe / reachesMagpie, probed in the
  background through the existing cache; answersAsMagpie shared with
  codex_proxy.go's realProxyProbe. offLoopback takes https too.
- applied.go: wiringOff (the choke point for ~40 agents) accepts it;
  Codex's "bypassed" isn't told for a request to the host the config
  keeps magpie at, or one the gateway saw from it after.
- claude.go (managed settings) and reasonix.go: their own exact checks.
- codex.go: codexKeptGateway keeps https, default ports and a path prefix
  (#816's kept address, trusted by magpie's key); viaBase matches the kept
  codexURL, since at.host() falls back to loopback for https.
- keptaway.go: awayPath keeps the path every URL at the away address had.
- Port moves need nothing: Rewire/OnGateway re-set only unwired agents.
- Picking a model in magpie still points the agent at this magpie
  (documented, unchanged).

Run (sandbox HOME, worktree):
- go test -tags nogui ./internal/agent: ok (297s) rebased on 12ecb6d8e.
  Before the rebase only TestFolderVarsAreListed failed, fixed on main by
  c6de0d2a5.
- New proxy_kept_test.go: every agent behind the proxy has no Check/
  unwired drift, at a relay all but Codex are told, SyncCatalog keeps
  https://ai.corp.example/magpie with its path in every file; Claude
  managed settings; unknown-then-not-magpie; loopback not probed; the
  real probe against httptest serving the gateway's handler under
  /magpie/; Codex https kept address, Reached and lastSeen; signed-in
  Codex via openai_base_url; awayPath table.
- Broke each fix by hand, each went red: wiringOff (claude, claude-desktop,
  gemini, opencode... told), claude managed, reasonix, awayPath (every
  synced file lost /magpie), bypassed host and lastSeen, unknown-as-drift,
  codexKeptGateway http-only/no-path, viaBase.
- gofmt -l; go vet darwin, GOOS=linux -tags nogui, GOOS=windows.

Not tried: a real nginx (Host rewriting, auth in front of GET /), the
Windows box, a real CC Switch. No GUI change.
2026-10-13 18:07:03 +08:00
yetone fa44fde461 netproxy: a proxy's user and password stay out of every log line, error and status that names it (#1886)
What: netproxy.Redact is the one place a proxy address is made fit to
show. It is the rule 708ae4144 (#852) gave the Settings status
(proxyAddress, now Redact): the userinfo is left out, the rest kept as
written (no scheme added to one written without), a malformed address
"". Every place a proxy value reached text now goes through it:
- provider/logins.go: the switch's "Claude Code sessions ... go through
  <user>'s proxy" log line (#1886's first).
- provider/claude_proxy_env.go: "<user>'s proxy is SOCKS" (#1886's
  second).
- agent/codex_proxy.go + reach.go: the proxied drift's Detail and Addr,
  shown in the GUI's pill and dialog; Codex's proxy comes from
  HTTP_PROXY/ALL_PROXY/the system, which can carry a user:password.
- update/mirror.go: CheckProxy's error echoed the raw --proxy value
  ("not %q"); it now names it redacted, or not at all when malformed.
- netproxy.Describe already went through it.

Why: an account proxy like http://alice:s3cret@10.0.0.2:8080 landed in
magpie's log, which users paste into issues and Discord.

Swept and left alone: settings.CheckProxy's error names no value;
netproxy.DialSOCKS's error names the host only; Go's transport errors
(proxyconnect, socks connect) name host:port, not userinfo; the GUI's
provider editor and Settings field return the proxy whole as the
user's own editable value; env injection (EnvWith, ForBun, wslrun)
must carry it whole.

Tests place the credential where the user writes it: http, https,
socks5, socks5h, with and without a scheme, percent-encoded user and
password, user alone, malformed. TestRedactLeavesOutUserAndPassword,
TestClaudeAccountProxyLogsNoCredentials (captures the log over real
switches; Claude Code's HTTPS_PROXY still gets the proxy whole),
TestCodexProxiedDriftShowsNoCredentials (the probe still goes through
the whole proxy), TestCheckProxyErrorShowsNoCredentials.

Run: each site reverted alone turns its test red (logins.go,
claude_proxy_env.go, reach.go, codex_proxy.go, mirror.go; Redact
keeping the userinfo turns Redact and Describe's tests red). gofmt -l
clean; go vet ./... on darwin, GOOS=linux -tags nogui, GOOS=windows.
go test ./... at 0dd821c13 + this, sandbox HOME: 47 packages ok;
agent, gateway and library timed out at 10m on a machine at load ~20,
then on rerun hit ENOSPC (the disk filled from other sessions). Rerun
with space: library ok in full; agent's TestClaudeOutputTokens and
gateway's TestGroupFirstTokenFailsOver (the ENOSPC ones) ok, with
every Proxy/Proxied test in both. Not tried in the GUI by hand.
2026-10-13 10:49:44 +08:00
yetone e4134d1fb7 agent: Codex's proxy is worked out from the login shell's NO_PROXY and HTTP_PROXY and macOS' HTTP entry alone, as its reqwest reads them, so "Codex 经代理连 magpie" no longer shows while Codex goes direct (liu_dyj on X)
liu_dyj: with the VPN on, magpie said Codex reached magpie through a
proxy that didn't pass its requests on; versions before f156d9046 had
no such check, which is why upgrading brought it.

Cause: codexProxyFor didn't read the proxy the way Codex does.
- It read magpie's own environment. Codex in a terminal has the login
  shell's, and the ChatGPT app's Codex too (ChatGPT.app 26.1007 runs
  $SHELL -ilc at start and Object.assign()s its env into process.env).
  magpie opened from the Dock has neither, so a NO_PROXY=127.0.0.1 in
  .zshrc, which keeps Codex direct, wasn't seen, and magpie probed the
  system proxy and warned.
- For the system proxy it took netproxy's one URL (HTTPS over HTTP over
  SOCKS). hyper-util 0.1.20's Matcher::from_system, which reqwest
  0.12.28 uses for Codex, takes only macOS' HTTP entry for an http://
  URL; with only the HTTPS or SOCKS entry set (ShadowsocksX-NG's global
  mode is SOCKS only) Codex goes direct and magpie warned. On Windows
  it takes ProxyServer as one address, which a per-protocol list
  ("http=..;https=..") isn't (http's Authority::parse refuses two
  colons), so Codex goes direct there too.
- With respect_system_proxy on macOS, CFNetwork's SOCKS entry is
  UnsupportedProxyScheme, and Codex falls back to HTTP_PROXY/ALL_PROXY
  with NO_PROXY (codex-rs outbound_proxy.rs resolve_env_proxy_route);
  magpie used the SOCKS URL.
- The probe spoke real SOCKS5 to a socks5:// proxy. Codex's reqwest is
  built without SOCKS (no tokio-socks in codex-rs Cargo.lock) and sends
  an absolute-URI HTTP request to it, so the probe now does the same:
  it passes a Clash mixed port as Codex does and fails a SOCKS-only
  port as Codex does.

So netproxy.Proxy carries the HTTP and SOCKS entries alone beside URL
(parseScutil, parseWindows), proc asks the login shell for the
*_PROXY variables with agentenv.Vars in the same probe and only reads
them (proc.LoginEnv; never set in magpie's env, whose requests use
netproxy), and codexProxyFor uses both. Failover's gate
(codexProxyOK) and the drift share codexProxyFor and proxyProbe, so
both follow. The pill's How to fix now names the shell profile for
NO_PROXY. Clash/mihomo rule mode with a 127.0.0.0/8 DIRECT rule never
warned and still doesn't; global mode or a rule set without it still
warns, rightly (real codex 502s there). TUN mode sets no proxy and
never warned.

Run:
- real codex-cli 0.162.1 in a sandbox HOME/CODEX_HOME against a fake
  gateway and mihomo v1.19.32 (rule, no-LAN-rule, global ports):
  direct, rule, and ALL_PROXY=socks5:// on the rule port reach the
  gateway; no-LAN-rule and global 502; global with NO_PROXY=127.0.0.1
  reaches it. The Go probe agrees on every one.
- go test -tags nogui ./internal/agent, ./internal/netproxy,
  ./internal/proc; gofmt -l; GOOS=linux and GOOS=windows go vet
  -tags nogui on the three packages.
- Broke each part by hand: sys.URL back in codexProxyFor (Windows per
  protocol, macOS HTTPS/SOCKS alone red), the probe's socks->http
  (TestRealProxyProbe red), codexEnv without the login shell
  (TestCodexEnvTakesLoginShell red), the respect-path SOCKS fallback
  (three respect cases red), setUnset of every shell var and the probe
  without ProxyVars (TestLoginEnvProxyVarsNotSet red).

Not tried: the macOS system proxy cases with real Codex (changing the
system's proxy settings is off limits here; read from hyper-util,
reqwest and codex-rs sources), the Windows per-protocol ProxyServer
with real Codex (from the http crate's source; no Windows run), and
the reporter's own app and mode, which are asked on X.
2026-10-12 22:33:50 +08:00
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