luci (Codex CLI 0.162, magpie 0.1.1150): after Codex was wired back to
magpie, codex in a new terminal listed Codex's own models although
config.toml was magpie's.
Cause, reproduced with real codex 0.162.0 in a sandbox CODEX_HOME:
codex sessions attach to a background app-server (the managed daemon)
that is per CODEX_HOME, not per terminal (socket under
$CODEX_HOME/app-server-control). It is detached, has no idle exit and
builds its model list once, at start. A daemon started while Codex was
off magpie keeps the official list for every later session, in this
terminal or a new one, until it restarts. magpie writes only PATH to
shell rc files and doesn't unwire Codex on quit, so nothing
per-terminal is involved. luci's "a new terminal fixed it" isn't
explained by the daemon. Codex's hourly updater restarting it is a
guess, not proven.
What changed:
- provider.KeepCodexDaemonCurrent(ctx, home, changed) compares the
daemon's start time with when magpie last changed what Codex reads at
start. The start time is the mtime of a pid file in that home that
names the running daemon's pid, so another home's daemon (ChatGPT.app,
other sandboxes) is never acted on.
- With no codex session attached, it runs
`codex app-server daemon restart` with CODEX_HOME set to that home.
- Otherwise it reports Behind and how many sessions are attached.
- isCodexClient leaves out apps' and editors' own codex, exec-server,
mcp-server and the daemon's own processes. It reads the whole ps line,
since ps leaves a path's spaces unquoted ("Codex Computer
Use.app/...").
- agent: Codex's Notice after a change runs this check first. It says
either that magpie restarted the daemon, or that the daemon is behind
and how to restart it (Agents page or the command).
- The GUI backend and `magpie serve` re-check every 15s. A session that
was open at the change is usually closed later, and that is when the
daemon can be restarted.
- Agents page: the stale "daemon" copy line has a Restart button that
POSTs /api/codex/daemon/restart (timeout now 2 min, as a running turn
is let finish). It reuses translated strings. On Windows, where
processes aren't listed on each look, the row shows the last check.
- Restarts are serialized by one mutex.
- WSL's Codex is not handled automatically. Its row keeps the advice
text, which names the restart command.
What was run:
- Real codex 0.162.0 in a sandbox CODEX_HOME, with a fake gateway, on
luci's sequence: the daemon started on the official config, then
magpie's config and catalog were written.
- With a TUI open, /model listed GPT-6.x. The check said Behind with
a session attached and did not restart.
- With the TUI quit, the check restarted the daemon. A new codex in
the same terminal listed Magpie Alpha and Beta.
- Earlier in the sandbox: a daemon restart under an idle attached 0.162
TUI reconnects, and /model then shows magpie's models. A running turn
is waited for up to 60s.
- New tests:
- TestKeepCodexDaemonCurrent(Fails), TestIsCodexClient (with the
space-in-path case from the real process list) and
TestCodexNoticeKeepsTheDaemonCurrent. They don't build or fail on
the old code. The space case failed before the ps-line fix.
- The GUI test "the daemon's line restarts it" times out on the old
app.js.
- Results:
- agent-stale-copies.test.cjs: 60/60 in Chromium and WebKit, in en,
zh, zh-TW, ja and de, at widths 980 and 440.
- go test (temp HOME) for ./internal/agent, ./internal/provider,
./internal/gui and the root package: ok.
- gofmt -l: clean.
- GOOS=linux and GOOS=windows go vet -tags nogui: ok.
- The Windows path is checked only by vet and not tried on a Windows
box.
101 lines
4.2 KiB
Go
101 lines
4.2 KiB
Go
package agent
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// Codex 0.162's TUI runs on a background app-server it starts once and
|
|
// leaves running, which built its model list when it started: after magpie
|
|
// changes Codex's list, a new codex session still shows Codex's own models
|
|
// until that app-server is restarted, while the desktop app, restarted,
|
|
// shows magpie's (TJHHHH, luci on Discord). Restarting the app and the open
|
|
// sessions, as the advice said, leaves it; the advice after a change names
|
|
// it and the command that restarts it, here and in WSL, where the Agents
|
|
// row can't tell a stale copy.
|
|
func TestCodexAdviceNamesTheSharedAppServer(t *testing.T) {
|
|
home, _ := codexHome(t, "", "")
|
|
was, wasKeep := codexRunning, keepCodexDaemon
|
|
codexRunning = func() bool { return true }
|
|
keepCodexDaemon = func(context.Context, string, time.Time) provider.CodexDaemonCheck { return provider.CodexDaemonCheck{} }
|
|
t.Cleanup(func() { codexRunning, keepCodexDaemon = was, wasKeep })
|
|
if n := codex(home).Notice(); !strings.Contains(n, provider.CodexDaemonRestart) {
|
|
t.Errorf("Codex's advice leaves out its app-server: %q", n)
|
|
}
|
|
for _, k := range wslKinds {
|
|
if k.id == "codex" && !strings.Contains(k.restart, provider.CodexDaemonRestart) {
|
|
t.Errorf("Codex in WSL's advice leaves out its app-server: %q", k.restart)
|
|
}
|
|
}
|
|
// nothing running, nothing to restart
|
|
codexRunning = func() bool { return false }
|
|
if n := codex(home).Notice(); strings.Contains(n, provider.CodexDaemonRestart) {
|
|
t.Errorf("advice with no Codex running: %q", n)
|
|
}
|
|
}
|
|
|
|
// luci (Discord, Codex 0.162): after Codex was wired to magpie every new
|
|
// codex showed Codex's own models, since the background app-server they
|
|
// attach to was started before, on the official config. Said after a
|
|
// change (the GUI's and the CLI's), the advice first checks that
|
|
// app-server against magpie's change in this Codex's home: restarted by
|
|
// magpie when no session is on it, named with the Agents page's Restart
|
|
// when one is.
|
|
func TestCodexNoticeKeepsTheDaemonCurrent(t *testing.T) {
|
|
home, _ := codexHome(t, "", "")
|
|
dir := filepath.Join(home, ".codex")
|
|
cfg, list := filepath.Join(dir, "config.toml"), filepath.Join(dir, "magpie-models.json")
|
|
os.WriteFile(cfg, []byte("model_provider = \"magpie\"\nmodel_catalog_json = \""+list+"\"\n"), 0o600)
|
|
os.WriteFile(list, []byte(`{"models":[{"slug":"magpie/a"}]}`), 0o644)
|
|
then := time.Now().Add(-2 * time.Hour)
|
|
os.Chtimes(cfg, then, then)
|
|
os.Chtimes(list, then, then)
|
|
wasRunning, wasKeep := codexRunning, keepCodexDaemon
|
|
t.Cleanup(func() { codexRunning, keepCodexDaemon = wasRunning, wasKeep })
|
|
codexRunning = func() bool { return true }
|
|
var asked []string
|
|
var changed time.Time
|
|
answer := provider.CodexDaemonCheck{}
|
|
keepCodexDaemon = func(_ context.Context, h string, c time.Time) provider.CodexDaemonCheck {
|
|
asked, changed = append(asked, h), c
|
|
return answer
|
|
}
|
|
|
|
// nothing behind: the advice as before
|
|
if n := codex(home).Notice(); n != codexRestartAll {
|
|
t.Fatalf("no daemon behind: %q", n)
|
|
}
|
|
if len(asked) != 1 || asked[0] != dir || changed.Sub(then).Abs() > time.Second {
|
|
t.Fatalf("asked %v at %v, want %s at %v", asked, changed, dir, then)
|
|
}
|
|
// magpie's list changes: the daemon is checked against now
|
|
os.WriteFile(list, []byte(`{"models":[{"slug":"magpie/a"},{"slug":"magpie/b"}]}`), 0o644)
|
|
answer = provider.CodexDaemonCheck{Behind: true, Restarted: true, Since: then}
|
|
n := codex(home).Notice()
|
|
if time.Since(changed) > time.Minute {
|
|
t.Fatalf("checked against %v, not magpie's change", changed)
|
|
}
|
|
if !strings.Contains(n, "magpie restarted") {
|
|
t.Fatalf("restarted: %q", n)
|
|
}
|
|
// a session on it: the user is told where to restart it
|
|
answer = provider.CodexDaemonCheck{Behind: true, Attached: 1, Since: then}
|
|
if n := codex(home).Notice(); !strings.Contains(n, "Agents page") || !strings.Contains(n, provider.CodexDaemonRestart) {
|
|
t.Fatalf("session attached: %q", n)
|
|
}
|
|
// as last checked, for the row on Windows
|
|
if got := codexDaemonBehind(); len(got) != 1 || got[0].Kind != "daemon" || !got[0].Since.Equal(then) {
|
|
t.Fatalf("behind: %+v", got)
|
|
}
|
|
CodexDaemonRestarted()
|
|
if got := codexDaemonBehind(); got != nil {
|
|
t.Fatalf("after a restart: %+v", got)
|
|
}
|
|
}
|