Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

agent: the Codex app-server magpie restarts gets the terminal's proxy and PATH

yetone5b01938776b9
diff --git a/internal/provider/codex_daemon.go b/internal/provider/codex_daemon.go
index 7d5b70504..ab2f96aaa 100644
--- a/internal/provider/codex_daemon.go
+++ b/internal/provider/codex_daemon.go
@@ -323,12 +323,59 @@ func codexDaemonCommand(ctx context.Context, home, verb string) (*exec.Cmd, erro
return nil, errors.New("the codex CLI isn't installed, or magpie can't find it")
}
cmd := proc.CommandContext(ctx, exe, "app-server", "daemon", verb)
- env := slices.DeleteFunc(os.Environ(), func(kv string) bool { return strings.HasPrefix(kv, "CODEX_HOME=") })
+ env := slices.DeleteFunc(codexDaemonEnv(os.Environ()), func(kv string) bool { return strings.HasPrefix(kv, "CODEX_HOME=") })
cmd.Env = append(env, "CODEX_HOME="+home)
cmd.Stdin = nil // os/exec reads it from the null device
return cmd, nil
}
+// daemonProxyVars are the proxy variables Codex reads, as proc.ProxyVars
+// names them for the login shell.
+var daemonProxyVars = []string{"HTTP_PROXY", "http_proxy", "HTTPS_PROXY", "https_proxy", "ALL_PROXY", "all_proxy", "NO_PROXY", "no_proxy"}
+
+// daemonLoginEnv and daemonLoginPath are the login shell's variables and
+// PATH ("" for none); vars so tests can say.
+var (
+ daemonLoginEnv = proc.LoginEnv
+ daemonLoginPath = func() string { return strings.Join(proc.LoginPath(), string(os.PathListSeparator)) }
+)
+
+// codexDaemonEnv is env with the PATH and proxy variables a terminal opened
+// now has, from the login shell: the daemon outlives the restart and
+// serves every codex started after it, so it runs as one started from a
+// terminal would. magpie opened from the desktop has neither: on Linux,
+// with the proxy set in .zshrc, the daemon magpie restarted after a
+// ChatGPT sign-in reached chatgpt.com directly, and every codex after it
+// stopped at "account/read failed: workspace routing discovery timed out"
+// until the daemon was restarted from a terminal (guotianci6 on X, Codex
+// 0.164). The proxy variables are taken as a set: when the login shell
+// names any, magpie's own are dropped, so a NO_PROXY only magpie has
+// doesn't ride along; when it names none, env's stay.
+func codexDaemonEnv(env []string) []string {
+ if runtime.GOOS == "windows" {
+ return env // a terminal's environment is the registry's, as magpie's is
+ }
+ var login []string
+ for _, k := range daemonProxyVars {
+ if v := daemonLoginEnv(k); v != "" {
+ login = append(login, k+"="+v)
+ }
+ }
+ path := daemonLoginPath()
+ out := make([]string, 0, len(env)+len(login))
+ for _, kv := range env {
+ k, _, _ := strings.Cut(kv, "=")
+ if (len(login) > 0 && slices.Contains(daemonProxyVars, k)) || (path != "" && k == "PATH") {
+ continue
+ }
+ out = append(out, kv)
+ }
+ if path != "" {
+ out = append(out, "PATH="+path)
+ }
+ return append(out, login...)
+}
+
// The daemon also builds Codex's model list once, as it starts, from
// config.toml and the catalog it names (codex-rs app-server's models
// manager): a codex session started after magpie wires Codex in, or takes
diff --git a/internal/provider/codex_daemon_test.go b/internal/provider/codex_daemon_test.go
index 511dac3c4..4881befec 100644
--- a/internal/provider/codex_daemon_test.go
+++ b/internal/provider/codex_daemon_test.go
@@ -6,6 +6,7 @@ import (
"os"
"path/filepath"
"runtime"
+ "slices"
"strings"
"testing"
"time"
@@ -175,6 +176,7 @@ func fakeCodex(t *testing.T, exit int) (out string) {
script := "#!/bin/sh\n" +
"echo \"$@\" > '" + out + ".args'\n" +
"echo \"$CODEX_HOME\" > '" + out + ".home'\n" +
+ "echo \"$HTTPS_PROXY\" > '" + out + ".proxy'\n" +
"if [ -p /dev/stdin ]; then echo pipe; else echo other; fi > '" + out + ".stdin'\n" +
"echo 'starting…'\n"
if exit != 0 {
@@ -182,9 +184,12 @@ func fakeCodex(t *testing.T, exit int) (out string) {
}
exe := filepath.Join(dir, "codex")
testenv.Program(t, exe, script)
- old := codexExecutable
- t.Cleanup(func() { codexExecutable = old })
+ old, oldEnv, oldPath := codexExecutable, daemonLoginEnv, daemonLoginPath
+ t.Cleanup(func() { codexExecutable, daemonLoginEnv, daemonLoginPath = old, oldEnv, oldPath })
codexExecutable = func() string { return exe }
+ // no login shell is asked
+ daemonLoginEnv = func(string) string { return "" }
+ daemonLoginPath = func() string { return "" }
return out
}
@@ -192,6 +197,13 @@ func TestRestartCodexDaemon(t *testing.T) {
pid := fakeDaemon(t)
t.Setenv("CODEX_HOME", "/somewhere/else")
out := fakeCodex(t, 0)
+ t.Setenv("HTTPS_PROXY", "")
+ daemonLoginEnv = func(k string) string {
+ if k == "HTTPS_PROXY" {
+ return "http://127.0.0.1:7890"
+ }
+ return ""
+ }
*pid = 50
noteCodexSwitch("a@x", "b@x")
if err := RestartCodexDaemon(context.Background()); err != nil {
@@ -209,6 +221,10 @@ func TestRestartCodexDaemon(t *testing.T) {
if got, want := read(".home"), "/somewhere/else"; got != want || filepath.Clean(got) != filepath.Dir(codexAuthPath()) {
t.Errorf("CODEX_HOME %q, want %q (where magpie writes auth.json)", got, want)
}
+ // the proxy a terminal has, which magpie opened from the desktop hasn't
+ if got := read(".proxy"); got != "http://127.0.0.1:7890" {
+ t.Errorf("HTTPS_PROXY %q, want the login shell's", got)
+ }
if got := read(".stdin"); got != "other" {
t.Errorf("stdin is a %s, want the null device", got)
}
@@ -516,3 +532,26 @@ func TestCodexAppStale(t *testing.T) {
t.Fatalf("dismissed: %q", got)
}
}
+
+// The daemon magpie restarts runs with the login shell's proxy and PATH,
+// as one a terminal started would (guotianci6 on X): magpie opened from
+// the desktop has neither.
+func TestCodexDaemonEnvIsTheTerminals(t *testing.T) {
+ oldEnv, oldPath := daemonLoginEnv, daemonLoginPath
+ t.Cleanup(func() { daemonLoginEnv, daemonLoginPath = oldEnv, oldPath })
+ login := map[string]string{"https_proxy": "http://127.0.0.1:7890", "NO_PROXY": "localhost"}
+ daemonLoginEnv = func(k string) string { return login[k] }
+ daemonLoginPath = func() string { return "/home/u/.local/bin:/usr/bin" }
+ got := codexDaemonEnv([]string{"HOME=/home/u", "PATH=/usr/bin", "no_proxy=*", "LANG=C"})
+ want := []string{"HOME=/home/u", "LANG=C", "PATH=/home/u/.local/bin:/usr/bin", "https_proxy=http://127.0.0.1:7890", "NO_PROXY=localhost"}
+ if !slices.Equal(got, want) {
+ t.Errorf("env %q, want %q", got, want)
+ }
+ // a login shell with no proxy and no PATH to say leaves magpie's own
+ login = nil
+ daemonLoginPath = func() string { return "" }
+ in := []string{"PATH=/usr/bin", "HTTPS_PROXY=http://p:1"}
+ if got := codexDaemonEnv(in); !slices.Equal(got, in) {
+ t.Errorf("env %q, want %q", got, in)
+ }
+}