Files
magpie/internal/agent/codex_app_test.go
T
yetone 791e27ec34 agent: the Codex app's Restart restarts only its app-server, its window stays
yoshi037070 (Discord, 10-27): when magpie restarted Codex, a new Codex
window opened. The switch paths (Connect, Disconnect, failover) already end
only the app's own app-server (codexAppStep, proc.End: SIGTERM on macOS,
exit 0 on Windows), and the app starts another and keeps its window. The
Agents row's Restart beside a Codex app left on the old list did not: on
macOS ReopenCodexApp ran osascript "quit" and then `open` on the bundle,
and that open is what brought up a new window.

Now ReopenCodexApp keeps its check (only a bundle the row lists as left on
the old list) and calls provider.RestartCodexApp, which ends each process
whose command line starts with <bundle>/Contents/ and is the app's own
app-server (isCodexAppOwnServer: an app-server, not --listen), waits up to
10s for them to be gone, and errs when none ran. The app, its window and
another program's --listen server are left alone. The confirm says only the
app-server restarts and the window stays; the status says "Codex app-server
restarted" (zh, zh-TW, ja, de). app.js stays under its budget, with no raise (1,310,458 of 1,310,460
after rebasing on 97332140a): the "asked first" comment went and
appReopen's shrank. The
agent-wiring page says the same.

The model picker: the app's restart recovery doesn't refetch its models
query; it refetches on the next window focus once the list is over 5
minutes old (staleTime), or when the picker is mounted again.

Run (rebased on origin/main, at this commit's tree):
- go test ./internal/agent: ok (280s).
- go test ./internal/provider -run 'Codex|Restart': ok. Before the rebase,
  the whole package with -skip TestUnreadLoginsWriteErrorsKeepCredentials
  (#3632 crashes the binary) was ok except TestFetchAsksASharedBaseOnce and
  its two siblings (#3854).
- go test ./internal/gui -run TestCoreScriptBudget: ok.
- GUI: agent-stale-copies in chromium and webkit, 5 languages, 980/440px:
  80 pass. i18n-keys: its only red is #3897 ("Runs as"). Before the
  rebase codex-app-stale passed too.
- gofmt -l clean; go vet darwin and windows for agent, provider, gui;
  linux for agent and provider (gui's linux vet needs wails' cgo here).
- Real app: a clone of ChatGPT.app 26.1021.31345 (APFS clone in a sandbox
  HOME, so the bundle is unique to it) run under that HOME. The real
  RestartCodexApp ended its app-server 32922 in 3.1s; the same main
  process (31894) started app-server 34143, and its one window (2273016)
  stayed the only window. The user's own ChatGPT was untouched.

Broken by hand:
- reopenApp back to appQuit + appOpen: TestCodexAppReopen red ("did quit
  …, open …").
- isCodexAppServer in place of isCodexAppOwnServer: TestRestartCodexApp…
  red (ended [2 3], the --listen one too).
- the bundle prefix dropped: red (ended [2 4], another bundle's).

Not tried: the picker refreshing in a signed-in app (the sandbox app isn't
signed in); Windows has no such Restart (the row's Restart for the Codex
app is macOS only; Windows switches already end codex.exe with exit 0 and
the app keeps its window, 3aead9c62).
2026-10-28 00:20:30 +08:00

70 lines
2.4 KiB
Go

package agent
import (
"context"
"errors"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
)
// The Codex app's Restart restarts only the app-server of the app the row
// lists as left on the old list. It never quits the app and opens it
// again, which opened a new window (yoshi037070, 10-27).
func TestCodexAppReopen(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip(staleOnWindows)
}
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
t.Setenv("CODEX_HOME", "")
dir := filepath.Join(home, ".codex")
os.MkdirAll(dir, 0o755)
os.WriteFile(filepath.Join(dir, "config.toml"), []byte("model_provider = \"magpie\"\n"), 0o600)
const bundle = "/Applications/Codex.app"
wasRunning, wasQuit, wasOpen, wasRestart := running, appQuit, appOpen, restartAppServer
t.Cleanup(func() { running, appQuit, appOpen, restartAppServer = wasRunning, wasQuit, wasOpen, wasRestart })
running = func(pat string) []runningProc {
ps := []runningProc{
{3 * time.Hour, bundle + "/Contents/MacOS/Codex"},
{3 * time.Hour, bundle + "/Contents/Resources/codex app-server"},
}
var out []runningProc
for _, p := range ps {
if strings.Contains(pat, "codex") && strings.Contains(p.cmd, "codex ") || strings.Contains(pat, `Codex\.app/Contents/`) {
out = append(out, p)
}
}
return out
}
var did []string
appQuit = func(_ context.Context, b string) error { did = append(did, "quit "+b); return nil }
appOpen = func(_ context.Context, b string) error { did = append(did, "open "+b); return nil }
restartAppServer = func(_ context.Context, b string) error { did = append(did, "restart "+b); return nil }
a := codex(home)
if err := reopenApp(context.Background(), a, "/Applications/Other.app"); err == nil || len(did) > 0 {
t.Fatalf("an app not listed: %v, did %v", err, did)
}
if err := reopenApp(context.Background(), a, bundle); err != nil {
t.Fatal(err)
}
if got := strings.Join(did, ", "); got != "restart "+bundle {
t.Fatalf("did %q, want only its app-server restarted", got)
}
// a restart that fails is said
did = nil
restartAppServer = func(context.Context, string) error { return errors.New("didn't end") }
if err := reopenApp(context.Background(), a, bundle); err == nil || len(did) > 0 {
t.Fatalf("failed restart: %v, did %v", err, did)
}
}