Wakkana on Discord (macOS, Codex CLI 0.162.1) keeps Codex in a CODEX_HOME
of their own. Picking a model in magpie wrote ~/.codex/config.toml and
~/.codex/magpie-models.json, which Codex never reads, while magpie read the
pick back from the same file and showed it applied.
Checked each claim in the source: codexIn built its folder from
filepath.Join(at.home, ".codex"); so did StandIn, the compact-window tune
and KeepCodexDaemonCurrent; codexAuthPath (logins.go), codexAccount
(account.go) and the Usage card's auth.json (subscription_usage.go) read a
hardcoded ~/.codex, and since the app-server restart takes its home from
codexAuthPath, it restarted ~/.codex's daemon too. sessions.CodexDir,
catalog.CodexHome, library's codexDir and import_codex's config path
already read CODEX_HOME, each with its own copy of the rule.
Now there is one rule, appdir.CodexHome / CodexHomeIn: $CODEX_HOME through
appdir.LookupEnv (so the value proc.UserPath lends the desktop app from the
login shell counts, and a relative one reads as unset), else ~/.codex.
Every place that reads or writes Codex's files goes through it:
config.toml and the catalog (codexIn, so detection, Dir, models_cache,
history, logs, threads too), StandIn, tune, the daemon check, auth.json
read/switch/sign-in/sign-out, the import's own-catalog check, sessions,
the models cache, library targets (AGENTS.md, skills, MCP, rtk). A WSL
distro's Codex keeps ~/.codex (place.codexHome), since magpie can't read
a distro's variables. Left as they are, on purpose: library's project-level
.codex/config.toml (Codex reads that in the project whatever CODEX_HOME
is), sessions/wsl.go and wsl.go's distro probes, cliupdate's standalone
install path, and gateway/prompt.go's "/.codex/" path heuristic for a
Claude Code memory file. Codex itself doesn't expand "~" in CODEX_HOME
(0.162.0: 'CODEX_HOME points to "~/x", but that path does not exist').
Tests:
- TestCodexHomeEnvTakesThePick (agent): CODEX_HOME set, pick fakeup/m1:
config.toml and magpie-models.json land in CODEX_HOME, Get reads it back,
StandIn and tune use it, nothing exists under ~/.codex. Old codexIn:
FAIL "Codex's files at …/001/.codex/config.toml, want under
…/codex-home"; old tune.go: FAIL "tune didn't write
$CODEX_HOME/config.toml"; old StandIn: FAIL "stand-in … is """.
- TestCodexHomeEnvSignIn (provider): auth.json, config import path and
daemon home are CODEX_HOME's; two accounts listed from it; SwitchLogin
writes it; ~/.codex never created. Old account.go: FAIL "sign-in in
$CODEX_HOME not read"; old codexAuthPath: FAIL "auth.json at
…/.codex/auth.json".
- TestRestartCodexDaemon and gui's TestCodexDaemonRestartRoute asserted the
old ~/.codex daemon under CODEX_HOME=/somewhere/else; they now want
/somewhere/else. TestFolderVarsAreListed scans appdir/codex.go too, so
its CODEX_HOME canary still finds the read.
Real use, sandbox HOME + CODEX_HOME=<sb>/custom-codex, MAGPIE_ADDR on a
free port with a stand-in listener: `magpie codex fakeup/m1` (this build)
wrote custom-codex/config.toml + magpie-models.json, no <sb>/home/.codex;
real codex 0.162.0 with that CODEX_HOME: `codex debug models` listed
fakeup/m1, `codex exec` sent POST /v1/responses model=fakeup/m1 to magpie's
address. The origin/main build in the same sandbox wrote
<sb>/home/.codex/{config.toml,magpie-models.json} and left
custom-codex/config.toml at model = "gpt-5.5" (the report). The GUI app
was not run.
#1513 (sunxwei, native Windows 0.1.1157, CODEX_HOME=F:\codex-home) is the
same bug. Windows has no shell to borrow from: a user-level variable is in
the environment Explorer hands the Start-menu app, which is what
appdir.LookupEnv reads, and a drive-letter path is rooted. On the Windows
box: the agent, provider and appdir test binaries (GOOS=windows) pass
TestCodexHomeEnvTakesThePick and TestCodexHomeEnvSignIn with CODEX_HOME a
C:\ temp folder; this build's magpie.exe codex fakeup/m1 in a sandbox
USERPROFILE with CODEX_HOME=D:\codexhome-sb\custom-codex wrote config.toml
(model, magpie provider, catalog) and magpie-models.json there and created
no %USERPROFILE%\.codex. Codex itself isn't installed there, so it was
not run on Windows.
Run: go test -tags nogui ./... in a temp HOME (all ok after the three test
updates above; re-ran agent, provider, gui, catalog, sessions, library,
appdir, usage, codexcat after rebase; gateway after rebase hit the 10m
default timeout at load ~150 and passed with -timeout 25m in 656s),
gofmt -l clean, go vet +
GOOS=linux + GOOS=windows go vet -tags nogui on the touched packages.
123 lines
4.5 KiB
Go
123 lines
4.5 KiB
Go
package agent
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// Codex's background app-server (the daemon codex sessions attach to)
|
|
// builds its model list once, as it starts, and runs on per CODEX_HOME,
|
|
// across terminals, with no idle exit. So a change magpie makes to what
|
|
// Codex reads at start — wiring it in, taking it out, a new catalog —
|
|
// reaches no codex session started after it until the daemon restarts:
|
|
// luci (Discord, Codex 0.162) took Codex off magpie, opened codex, which
|
|
// started the daemon on Codex's own models, and after wiring Codex in
|
|
// again every new codex showed the official list. With no codex session
|
|
// attached magpie restarts it itself, right after the change and every
|
|
// so often after (a session open then is one the user closes later);
|
|
// with one attached the Agents page offers the restart.
|
|
|
|
// codexRestartAll is what is said after a change while copies of Codex
|
|
// run that magpie leaves to the user.
|
|
const codexRestartAll = "Codex builds its model list at start-up — restart the Codex app, open codex sessions and the app-server they share (" + provider.CodexDaemonRestart + ") to see this."
|
|
|
|
// keepCodexDaemon is provider.KeepCodexDaemonCurrent; a var so tests can
|
|
// see what is asked of it without a codex.
|
|
var keepCodexDaemon = provider.KeepCodexDaemonCurrent
|
|
|
|
// codexDaemonSeen is the last check of this machine's daemon, for the
|
|
// Agents row where its processes aren't listed on each look (Windows,
|
|
// where listing them is a PowerShell run).
|
|
var codexDaemonSeen struct {
|
|
sync.Mutex
|
|
at time.Time
|
|
check provider.CodexDaemonCheck
|
|
}
|
|
|
|
// codexDaemonCheck checks the daemon of the Codex whose config is path
|
|
// against when magpie last changed what Codex reads at start, and
|
|
// restarts it when no codex session is on it.
|
|
func codexDaemonCheck(path string) provider.CodexDaemonCheck {
|
|
a := &Agent{ID: "codex", Path: path}
|
|
_, files, reads := a.startsWith()
|
|
changed := a.changedAt(files, reads)
|
|
ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second)
|
|
defer cancel()
|
|
c := keepCodexDaemon(ctx, filepath.Dir(path), changed)
|
|
if c.Err != nil {
|
|
log.Println("codex app-server:", c.Err)
|
|
}
|
|
codexDaemonSeen.Lock()
|
|
codexDaemonSeen.at, codexDaemonSeen.check = time.Now(), c
|
|
codexDaemonSeen.Unlock()
|
|
return c
|
|
}
|
|
|
|
// codexDaemonNotice is what is said of the daemon after a change: ok is
|
|
// false when there is none behind it.
|
|
func codexDaemonNotice(path string) (string, bool) {
|
|
c := codexDaemonCheck(path)
|
|
switch {
|
|
case c.Restarted:
|
|
return "magpie restarted the app-server codex sessions share, which none was on, so new codex sessions have this; restart the Codex app, if it is open, to see it there.", true
|
|
case c.Behind && c.Attached > 0:
|
|
return "Codex builds its model list at start-up, and the app-server open codex sessions share started before this: restart it (Restart on magpie's Agents page, or " + provider.CodexDaemonRestart + ") and the Codex app to see this.", true
|
|
}
|
|
return "", false
|
|
}
|
|
|
|
// codexDaemonEvery is how often KeepCodexDaemonCurrent looks.
|
|
const codexDaemonEvery = 15 * time.Second
|
|
|
|
// KeepCodexDaemonCurrent restarts this machine's Codex daemon whenever it
|
|
// is behind what Codex reads at start and no codex session is on it,
|
|
// looking every so often until ctx ends.
|
|
func KeepCodexDaemonCurrent(ctx context.Context) {
|
|
home, err := os.UserHomeDir()
|
|
if err != nil {
|
|
return
|
|
}
|
|
path := filepath.Join(here(home).codexHome(), "config.toml")
|
|
t := time.NewTicker(codexDaemonEvery)
|
|
defer t.Stop()
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case <-t.C:
|
|
}
|
|
if _, err := os.Stat(path); err == nil {
|
|
if c := codexDaemonCheck(path); c.Restarted {
|
|
log.Println("codex app-server: restarted, it had the model list from before", c.Since.Format(time.RFC3339))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// CodexDaemonRestarted forgets the daemon last found behind, once the user
|
|
// has restarted it.
|
|
func CodexDaemonRestarted() {
|
|
codexDaemonSeen.Lock()
|
|
codexDaemonSeen.check = provider.CodexDaemonCheck{}
|
|
codexDaemonSeen.Unlock()
|
|
}
|
|
|
|
// codexDaemonBehind is the daemon as last checked, while that check is
|
|
// recent: a copy left on the old list, for StaleCopies where processes
|
|
// aren't listed on each look.
|
|
func codexDaemonBehind() []StaleCopy {
|
|
codexDaemonSeen.Lock()
|
|
defer codexDaemonSeen.Unlock()
|
|
c := codexDaemonSeen.check
|
|
if !c.Behind || c.Restarted || time.Since(codexDaemonSeen.at) > 3*codexDaemonEvery {
|
|
return nil
|
|
}
|
|
return []StaleCopy{{Kind: "daemon", Since: c.Since}}
|
|
}
|