Files
magpie/internal/provider/codex_daemon.go
T
yetone e611fc65d6 codex: magpie restarts the codex app-server a change left on the old model list when no codex session is on it, and the Agents page offers Restart when one is (luci on Discord)
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.
2026-10-10 10:05:10 +08:00

406 lines
13 KiB
Go

package provider
// Codex's background app-server. Codex CLI (0.156 on) can keep one running
// for its sessions to attach to — `codex app-server --managed-daemon`,
// started by the CLI and left running — and it reads the ChatGPT sign-in
// once, when it starts. So when magpie switches the account Codex is signed
// in to, a Codex session opened after that still runs on the account before
// until the daemon is restarted. magpie doesn't restart it on its own:
// that ends the sessions running on it. It says so instead, and restarts it
// when asked (RestartCodexDaemon).
//
// The daemon is told apart by --managed-daemon: the app-servers other apps
// run for themselves (the ChatGPT app's own codex, an editor's) are started
// without it, and they are none of magpie's business.
import (
"bytes"
"context"
"encoding/json"
"errors"
"os"
"os/exec"
"path/filepath"
"runtime"
"slices"
"strconv"
"strings"
"sync"
"time"
"github.com/yetone/magpie/internal/proc"
)
// CodexDaemonRestart is the command that restarts Codex's app-server, for
// the user to run themselves.
const CodexDaemonRestart = "codex app-server daemon restart"
// listProcesses lists the processes that may be Codex's; a var so tests
// can fake it.
var listProcesses = func(ctx context.Context) ([]proc.Process, error) {
return proc.List(ctx, "codex")
}
// processCodexHome is the CODEX_HOME a process was started with, when it
// can be read (Linux, a process of the user's): "" is unset. A var so tests
// can fake it.
var processCodexHome = func(pid int) (home string, ok bool) {
if runtime.GOOS != "linux" {
return "", false
}
b, err := os.ReadFile("/proc/" + strconv.Itoa(pid) + "/environ")
if err != nil {
return "", false
}
for _, kv := range bytes.Split(b, []byte{0}) {
if v, found := bytes.CutPrefix(kv, []byte("CODEX_HOME=")); found {
return string(v), true
}
}
return "", true
}
// codexHome is the Codex home magpie signs Codex in through.
func codexHome() string { return filepath.Dir(codexAuthPath()) }
// isManagedDaemon reports whether a command line is Codex's managed
// app-server: `codex [-c k=v…] app-server --managed-daemon …`. Its helpers
// (`codex app-server daemon pid-update-loop`), other apps' app-servers and
// other programs given those words aren't.
func isManagedDaemon(args string) bool {
f := strings.Fields(args)
i := slices.Index(f, "app-server")
if i < 1 || !slices.ContainsFunc(f[:i], func(a string) bool {
base := strings.ToLower(a[strings.LastIndexAny(a, `/\`)+1:])
return base == "codex" || base == "codex.exe"
}) {
return false
}
return slices.ContainsFunc(f[i+1:], func(a string) bool {
return a == "--managed-daemon" || strings.HasPrefix(a, "--managed-daemon=")
})
}
// daemonPIDs are the ids Codex wrote down for the app-server it runs out of
// home: app-server-daemon/app-server.pid (daemon.pid before), a PidRecord
// ({"pid": …, …}) or a bare number.
func daemonPIDs(home string) []int {
var ids []int
for _, name := range []string{"app-server.pid", "daemon.pid"} {
b, err := os.ReadFile(filepath.Join(home, "app-server-daemon", name))
if err != nil {
continue
}
if id, ok := parsePIDFile(b); ok {
ids = append(ids, id)
}
}
return ids
}
func parsePIDFile(b []byte) (int, bool) {
b = bytes.TrimSpace(b)
if id, err := strconv.Atoi(string(b)); err == nil && id > 0 {
return id, true
}
var rec struct {
PID int `json:"pid"`
}
if json.Unmarshal(b, &rec) == nil && rec.PID > 0 {
return rec.PID, true
}
return 0, false
}
// findCodexDaemon picks, among the running processes, Codex's managed
// app-server for home: 0 when none runs. One started for another
// CODEX_HOME is left out where that can be read; where it can't, the one
// home's pid file names is taken, else the first.
func findCodexDaemon(ps []proc.Process, home string) int {
var ids []int
for _, p := range ps {
if !isManagedDaemon(p.Args) {
continue
}
if h, ok := processCodexHome(p.PID); ok && h != "" && filepath.Clean(h) != filepath.Clean(home) {
continue
}
ids = append(ids, p.PID)
}
if len(ids) == 0 {
return 0
}
for _, id := range daemonPIDs(home) {
if slices.Contains(ids, id) {
return id
}
}
return ids[0]
}
// codexDaemon is the process id of Codex's managed app-server, 0 when none
// runs; known is false when the processes couldn't be listed.
func codexDaemon(ctx context.Context) (pid int, known bool) {
ps, err := listProcesses(ctx)
if err != nil {
return 0, false
}
return findCodexDaemon(ps, codexHome()), true
}
// codexDaemonLeft is the app-server that was running when Codex's account
// last changed, and the account it was started on: it stays on that one
// until it restarts.
type leftDaemon struct {
pid int
user string
}
var (
codexDaemonMu sync.Mutex
codexDaemonLeft leftDaemon
codexDaemonChecked time.Time
)
// codexDaemonRecheck is how long CodexDaemonStale trusts what it last saw.
const codexDaemonRecheck = 5 * time.Second
// noteCodexSwitch remembers, when Codex's account changes from one to
// another, the app-server left running on the one before.
func noteCodexSwitch(from, to string) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
pid, known := codexDaemon(ctx)
codexDaemonMu.Lock()
defer codexDaemonMu.Unlock()
codexDaemonChecked = time.Now()
switch {
case !known:
case pid == 0:
codexDaemonLeft = leftDaemon{}
case pid == codexDaemonLeft.pid:
// the daemon is on the account it started with, whatever came between
if strings.EqualFold(to, codexDaemonLeft.user) {
codexDaemonLeft = leftDaemon{}
}
default:
codexDaemonLeft = leftDaemon{pid, from}
}
}
// CodexDaemonStale answers the account Codex's app-server is still signed
// in to when Codex has been switched to another since it started, "" when
// it isn't running or is on the account Codex is.
func CodexDaemonStale() string {
codexDaemonMu.Lock()
defer codexDaemonMu.Unlock()
if codexDaemonLeft.pid == 0 {
return ""
}
if time.Since(codexDaemonChecked) > codexDaemonRecheck {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
pid, known := codexDaemon(ctx)
cancel()
codexDaemonChecked = time.Now()
if known && pid != codexDaemonLeft.pid {
// restarted, or stopped, since: it reads the sign-in afresh
codexDaemonLeft = leftDaemon{}
}
}
return codexDaemonLeft.user
}
// DismissCodexDaemon stops saying the app-server is on another account.
func DismissCodexDaemon() {
codexDaemonMu.Lock()
defer codexDaemonMu.Unlock()
codexDaemonLeft = leftDaemon{}
}
// RestartCodexDaemon has Codex restart its app-server, which reads the
// sign-in again: `codex app-server daemon restart`, run with the codex CLI
// magpie finds and the Codex home it signs Codex in through. Codex 0.162's
// sessions attached to it reconnect to the new one, a turn running is let
// finish first (for up to a minute); an older Codex's are ended.
func RestartCodexDaemon(ctx context.Context) error {
codexRestarting.Lock()
defer codexRestarting.Unlock()
if err := restartCodexDaemon(ctx, codexHome()); err != nil {
return err
}
DismissCodexDaemon()
return nil
}
func restartCodexDaemon(ctx context.Context, home string) error {
cmd, err := codexDaemonCommand(ctx, home, "restart")
if err != nil {
return err
}
if out, err := cmd.CombinedOutput(); err != nil {
if msg := lastLine(out); msg != "" {
return errors.New("codex app-server daemon restart: " + msg)
}
return errors.New("codex app-server daemon restart: " + err.Error())
}
return nil
}
// codexDaemonCommand is `codex app-server daemon <verb>`, with CODEX_HOME
// set to the home magpie switches, and nothing to read on stdin (Codex
// waits on a stdin left open).
func codexDaemonCommand(ctx context.Context, home, verb string) (*exec.Cmd, error) {
exe := codexExecutable()
if exe == "" {
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=") })
cmd.Env = append(env, "CODEX_HOME="+home)
cmd.Stdin = nil // os/exec reads it from the null device
return cmd, nil
}
// 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
// it out, attaches to the daemon left running and shows the list from
// before — Codex's own models, or magpie's — until it restarts (luci on
// Discord, Codex 0.162). It runs per CODEX_HOME, not per terminal, and
// has no idle exit. Restarting it with no codex session attached ends
// nothing, so magpie does that itself (KeepCodexDaemonCurrent); with one
// attached, the Agents page offers it.
// A CodexDaemonCheck is what KeepCodexDaemonCurrent found and did.
type CodexDaemonCheck struct {
// Behind: a daemon runs that started before what it reads changed
Behind bool
// Since is when that daemon started
Since time.Time
// Attached is how many codex sessions may be on it, which magpie
// doesn't restart it under
Attached int
// Restarted: magpie restarted it, and new sessions get the new list
Restarted bool
Err error
}
// codexRestarting keeps one restart of the daemon at a time.
var codexRestarting sync.Mutex
// KeepCodexDaemonCurrent restarts the managed app-server of the Codex home
// home when it started before changed (when magpie last changed what Codex
// reads at start) and no codex session is attached to it. One that can't
// be found or timed, or a restart already under way, is left as it is.
func KeepCodexDaemonCurrent(ctx context.Context, home string, changed time.Time) CodexDaemonCheck {
var c CodexDaemonCheck
if changed.IsZero() {
return c
}
ps, err := listProcesses(ctx)
if err != nil {
return c
}
pid := findCodexDaemon(ps, home)
if pid == 0 {
return c
}
since, ok := daemonStarted(home, pid)
// a second's slack: the pid file and the config are written by
// different programs, whose clocks round differently
if !ok || !since.Before(changed.Add(-time.Second)) {
return c
}
c.Behind, c.Since, c.Attached = true, since, codexClients(ps, home)
if c.Attached > 0 || !codexRestarting.TryLock() {
return c
}
defer codexRestarting.Unlock()
if c.Err = restartCodexDaemon(ctx, home); c.Err == nil {
c.Restarted = true
}
return c
}
// daemonStarted is when the daemon pid started: the time Codex wrote its
// pid file, which it does once, as the daemon comes up, on every OS. ok is
// false when no pid file of home's names pid.
func daemonStarted(home string, pid int) (time.Time, bool) {
for _, name := range []string{"app-server.pid", "daemon.pid"} {
p := filepath.Join(home, "app-server-daemon", name)
b, err := os.ReadFile(p)
if err != nil {
continue
}
if id, ok := parsePIDFile(b); !ok || id != pid {
continue
}
if st, err := os.Stat(p); err == nil {
return st.ModTime(), true
}
}
return time.Time{}, false
}
// codexClients counts the codex sessions that may be attached to home's
// daemon: codex run in a terminal (its TUI, exec, resume, an app-server
// proxy); not the daemon, its helpers and the commands that drive it, nor
// an app's or an editor's codex, which serve themselves. One started for
// another CODEX_HOME is left out where that can be read.
func codexClients(ps []proc.Process, home string) int {
n := 0
for _, p := range ps {
if !isCodexClient(p.Args) {
continue
}
if h, ok := processCodexHome(p.PID); ok && h != "" && filepath.Clean(h) != filepath.Clean(home) {
continue
}
n++
}
return n
}
// isCodexClient reports whether a command line is a codex session that
// may attach to the daemon.
func isCodexClient(args string) bool {
exe, rest := splitExe(args)
base := strings.ToLower(exe[strings.LastIndexAny(exe, `/\`)+1:])
if base != "codex" && base != "codex.exe" {
return false
}
// an app's (ChatGPT.app's CodexCLI.app) or an editor extension's; ps
// leaves a path's spaces unquoted, so "…/Codex Computer Use.app/
// Contents/MacOS/…" reads as a program named Codex: the whole line
// is looked at
if strings.Contains(args, ".app/Contents/") || strings.Contains(exe, "/extensions/") || strings.Contains(exe, `\extensions\`) {
return false
}
f := strings.Fields(rest)
if slices.Contains(f, "exec-server") || slices.Contains(f, "mcp-server") {
return false
}
if i := slices.Index(f, "app-server"); i >= 0 {
// the daemon, its helpers, `daemon restart` and other apps'
// app-servers serve; a proxy is a client of the daemon
return i+1 < len(f) && f[i+1] == "proxy"
}
return true
}
// splitExe parts a command line into the program and its arguments, the
// program quoted as Windows lists it (`"C:\Program Files\…\codex.exe" …`).
func splitExe(args string) (exe, rest string) {
args = strings.TrimSpace(args)
if q, ok := strings.CutPrefix(args, `"`); ok {
if exe, rest, ok = strings.Cut(q, `"`); ok {
return exe, rest
}
}
exe, rest, _ = strings.Cut(args, " ")
return exe, rest
}
// CodexHome is the Codex home magpie wires and signs Codex in through.
func CodexHome() string { return codexHome() }