Files
magpie/internal/agent/codex_app_crash_test.go
T
yetone 4f624241f7 codex: the ChatGPT app left on "hit a snag" after an app-server restart is reopened when idle (#4550)
ysm119 on Discord: each Restart of Codex in magpie left the ChatGPT
desktop app on "ChatGPT 遇到了问题 / 请重试" till it was quit and opened
again. magpie ends the app's own app-server cleanly (code 0); a
signed-in ChatGPT 26.1021 then fails "Hosted AppServerManager identity
is not ready" on the one it starts, logs fatal_error_broadcasted and
shows the page. That is the app's bug; magpie now follows it up.

- provider/codex_app_crash.go: for 60s after every end magpie makes
  (the watcher's step, RestartCodexApp, RestartSwitchedCodexApp) the
  app's logs are read for a whole fatal_error_broadcasted line stamped
  after the end. On one, with no request through magpie since or in
  flight and no Codex turn opened since, the app is quit and opened
  once. Otherwise it is left as it is and logged. Where it can't be
  reopened (Linux, an app that didn't quit) the Agents row says
  "The Codex app hit an error · quit it and open it again" (appSnag)
  until the app-servers seen then are gone or the app is closed.
  Unreadable logs reopen nothing.
- agent/codex_app_crash.go: the logs as the app's file logger writes
  them (macOS ~/Library/Logs/com.openai.codex, Windows
  %LOCALAPPDATA%\Codex\Logs and the Store package's LocalCache, Linux
  $XDG_STATE_HOME/codex/logs; YYYY/MM/DD folders, tail-read, half
  lines dropped). The relaunch is the Agents row's: on macOS
  ChatGPT.app found by its bundle id, quit as ⌘Q does and opened once
  it has ended, never forced; on Windows codexAppReopen.
- GUI line in en, zh, zh-TW, ja, de; docs/subsystems/agent-wiring.md.

Run on mote (macOS) at this head on origin/main 08982805e:
- go vet ./internal/agent ./internal/provider ./internal/gui (darwin
  and GOOS=windows), GOOS=linux go vet ./internal/agent
  ./internal/provider; go build ./...; gofmt -l clean.
- go test ./internal/provider ok; go test -run
  'TestCodexApp|Restart|Reopen' ./internal/agent ok.
- go test ./internal/gui: red only in TestMainWindowCorners (#3949) and
  TestPanelHiddenByWailsMarksPageOutOfSight (#5322), AppKit window tests
  red on mote's macOS 27 and filed before this change.
- win-run and wsl-run: go test -run TestCodexApp ./internal/agent
  ./internal/provider ok (the macOS relaunch test skips on Windows, as
  TestWorkBuddySidecarOutlivesQuitOnMac does, #4115).
- node --test codex-app-snag, codex-app-out, codex-app-back, i18n-keys,
  i18n-en-no-cjk: chromium and webkit, every language, 980 and 440px.
  i18n-keys' "a plugin's words are its own" stays red for "View routing"
  (#5327/#5332, red on main since 73c98ebc2); this change added a word
  there and took it out again.
- Core script budget: under it, not raised.

Broken by hand, each red then restored:
- codexAppCrashStep's call removed: 4 provider tests red.
- the watch not armed in RestartSwitchedCodexApp:
  TestCodexAppRestartsWatchForTheErrorPage red; in RestartCodexApp:
  TestCodexAppRowRestartWatchesForTheErrorPage red.
- the turn and request checks off: TestCodexAppInUseAfterItsErrorPageIsLeft red.
- the snag not set: TestCodexAppSnagSaidTillTheAppRestarts red.
- the stamp's >= since check dropped, or a half-written last line kept:
  TestCodexAppCrashedReadsItsOwnLogs red.
- a wrong Store package path: TestCodexAppCrashedReadsEachSystemsFolder red.

Not tried: a real signed-in ChatGPT app reaching the identity-not-ready
page, and a real quit and reopen of the user's ChatGPT.app (the relaunch
is tested with faked process listing, quit and open in a sandbox HOME).
The log line and folders are read from ChatGPT 26.1021's
file-based-logger and the reporters' logs; a later app that renames
them reopens nothing, as before this change.
2026-11-05 17:24:53 +08:00

226 lines
9.0 KiB
Go

package agent
import (
"context"
"os"
"path/filepath"
"regexp"
"runtime"
"slices"
"testing"
"time"
)
// codexLog writes a log file of the Codex app's under root's day folder
// for day, as the app's file logger names it, modified at mod.
func codexLog(t *testing.T, root string, day time.Time, text string, mod time.Time) string {
t.Helper()
dir := filepath.Join(root, day.UTC().Format("2006"), day.UTC().Format("01"), day.UTC().Format("02"))
if err := os.MkdirAll(dir, 0o755); err != nil {
t.Fatal(err)
}
p := filepath.Join(dir, "codex-desktop-581d5b6a-a43b-4ec2-8343-17b4d18217bb-390-t0-i1-000015-0.log")
if err := os.WriteFile(p, []byte(text), 0o644); err != nil {
t.Fatal(err)
}
if err := os.Chtimes(p, mod, mod); err != nil {
t.Fatal(err)
}
return p
}
// the lines as ChatGPT.app 26.1021 writes them (the owner's logs and the
// app's broadcastFatalError)
func exitedLine(at time.Time) string {
return at.UTC().Format("2006-01-02T15:04:05.000Z") + " error [AppServerConnection] Codex CLI process exited classifiedAsExpected=true code=0 connectionId=93 signal=null transport=stdio\n"
}
func fatalLine(at time.Time) string {
return at.UTC().Format("2006-01-02T15:04:05.000Z") + " error [AppServerConnection] fatal_error_broadcasted initialized=true listenerCount=2 pendingRequestCount=0 transportKind=stdio\n"
}
// The page the Codex app shows when it can't go on after magpie restarted
// its app-server (ysm119 on Discord) is read from the app's own logs: a
// complete fatal_error_broadcasted line at or after the end, in a file
// written to since, in the newest day folders (a file opened the day
// before is written on to past midnight UTC). The clean exit it logs
// first, a page from before, a line still being written and another
// app's folder are none of it.
func TestCodexAppCrashedReadsItsOwnLogs(t *testing.T) {
home := restartSandbox(t)
restartOnMac = true
root := filepath.Join(home, "Library", "Logs", "com.openai.codex")
since := time.Date(2026, 11, 5, 23, 59, 58, 0, time.UTC)
if _, crashed, known := codexAppCrashed(since); crashed || !known {
t.Fatalf("no logs at all: crashed %v, known %v (the app never ran)", crashed, known)
}
// the end's clean exit and a page from an hour before
p := codexLog(t, root, since.Add(-2*time.Hour), fatalLine(since.Add(-time.Hour))+exitedLine(since.Add(time.Second)), since.Add(time.Second))
if _, crashed, _ := codexAppCrashed(since); crashed {
t.Fatal("the clean exit or an old page read as the page")
}
// the page, half-written
half := fatalLine(since.Add(3 * time.Second))
os.WriteFile(p, []byte(fatalLine(since.Add(-time.Hour))+exitedLine(since.Add(time.Second))+half[:len(half)-1]), 0o644)
os.Chtimes(p, since.Add(3*time.Second), since.Add(3*time.Second))
if _, crashed, _ := codexAppCrashed(since); crashed {
t.Fatal("a line still being written read as the page")
}
// whole, in the file opened the day before
os.WriteFile(p, []byte(fatalLine(since.Add(-time.Hour))+exitedLine(since.Add(time.Second))+half), 0o644)
os.Chtimes(p, since.Add(3*time.Second), since.Add(3*time.Second))
at, crashed, known := codexAppCrashed(since)
if !crashed || !known || !at.Equal(since.Add(3*time.Second)) {
t.Fatalf("the page: at %v, crashed %v, known %v", at, crashed, known)
}
// a file not written to since the end isn't read
os.Chtimes(p, since.Add(-time.Minute), since.Add(-time.Minute))
if _, crashed, _ := codexAppCrashed(since); crashed {
t.Fatal("a file untouched since the end read")
}
// the next day's folder
codexLog(t, root, since.Add(time.Hour), exitedLine(since.Add(time.Second))+fatalLine(since.Add(4*time.Second)), since.Add(5*time.Second))
if at, crashed, _ := codexAppCrashed(since); !crashed || !at.Equal(since.Add(4*time.Second)) {
t.Fatalf("the next day's folder: at %v, crashed %v", at, crashed)
}
// another app's logs (the beta's bundle id) are not the Codex app's
home = restartSandbox(t)
restartOnMac = true
codexLog(t, filepath.Join(home, "Library", "Logs", "com.openai.codex.beta"), since, fatalLine(since.Add(time.Second)), since.Add(time.Second))
if _, crashed, _ := codexAppCrashed(since); crashed {
t.Fatal("another app's page read as the Codex app's")
}
// a log folder that can't be read is unknown
if runtime.GOOS != "windows" {
root := filepath.Join(home, "Library", "Logs", "com.openai.codex")
os.MkdirAll(root, 0o755)
os.Chmod(root, 0)
t.Cleanup(func() { os.Chmod(root, 0o755) })
if _, err := os.ReadDir(root); err == nil {
t.Skip("running as a user who reads any folder")
}
if _, crashed, known := codexAppCrashed(since); crashed || known {
t.Fatalf("an unreadable folder: crashed %v, known %v", crashed, known)
}
}
}
// Windows and Linux keep the logs where the app's file logger puts them
// there: %LOCALAPPDATA%\Codex\Logs, which the Store app writes under its
// package's LocalCache, and $XDG_STATE_HOME/codex/logs (~/.local/state
// without it).
func TestCodexAppCrashedReadsEachSystemsFolder(t *testing.T) {
since := time.Date(2026, 11, 5, 8, 0, 0, 0, time.UTC)
page := exitedLine(since.Add(time.Second)) + fatalLine(since.Add(2*time.Second))
for _, tc := range []struct {
name string
set func(home string)
root func(home string) string
}{
{"windows", func(home string) {
restartOnWindows = true
t.Setenv("LOCALAPPDATA", filepath.Join(home, "AppData", "Local"))
}, func(home string) string { return filepath.Join(home, "AppData", "Local", "Codex", "Logs") }},
{"windows store", func(home string) {
restartOnWindows = true
t.Setenv("LOCALAPPDATA", filepath.Join(home, "AppData", "Local"))
}, func(home string) string {
return filepath.Join(home, "AppData", "Local", "Packages", "OpenAI.Codex_2p2nqsd0c76g0", "LocalCache", "Local", "Codex", "Logs")
}},
{"linux", func(home string) {
restartOnLinux = true
t.Setenv("XDG_STATE_HOME", filepath.Join(home, "state"))
}, func(home string) string { return filepath.Join(home, "state", "codex", "logs") }},
{"linux default", func(home string) {
restartOnLinux = true
t.Setenv("XDG_STATE_HOME", "")
}, func(home string) string { return filepath.Join(home, ".local", "state", "codex", "logs") }},
} {
t.Run(tc.name, func(t *testing.T) {
home := restartSandbox(t)
tc.set(home)
if _, crashed, _ := codexAppCrashed(since); crashed {
t.Fatal("a page with no logs")
}
codexLog(t, tc.root(home), since, page, since.Add(3*time.Second))
if at, crashed, known := codexAppCrashed(since); !crashed || !known || !at.Equal(since.Add(2*time.Second)) {
t.Fatalf("at %v, crashed %v, known %v", at, crashed, known)
}
})
}
}
// The app is reopened as the Agents row's Restart reopens an app on a Mac:
// ChatGPT.app, known by its bundle id, quit as ⌘Q does and opened once it
// has ended, never another bundle (a Codex.app of another id), and never
// forced (one still open is said). An app not open: nothing. Linux: not done.
func TestCodexAppRelaunchOnlyTheCodexApp(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("the bundle paths are built with filepath, so a Windows run spells them with \\ (#4115)")
}
home := restartSandbox(t)
apps := filepath.Join(home, "Applications")
chatgpt := macApp(t, apps, "ChatGPT.app", "com.openai.codex", "ChatGPT")
other := macApp(t, apps, "Codex.app", "com.openai.codex.beta", "Codex")
restartOnMac = true
appFolders = func() []string { return []string{apps} }
appQuitWait = 100 * time.Millisecond
alive := map[string]bool{chatgpt: true, other: true}
stays := false
running = func(pat string) []runningProc {
var out []runningProc
exe := map[string]string{chatgpt: "ChatGPT", other: "Codex"}
for b, on := range alive {
for _, cmd := range []string{
b + "/Contents/MacOS/" + exe[b],
b + "/Contents/Resources/codex app-server --analytics-default-enabled",
} {
if on && regexp.MustCompile(pat).MatchString(cmd) {
out = append(out, runningProc{time.Hour, cmd})
}
}
}
return out
}
var quit, opened []string
appQuit = func(_ context.Context, b string) error {
quit = append(quit, b)
if !stays {
alive[b] = false
}
return nil
}
appOpen = func(_ context.Context, b string) error {
opened = append(opened, b)
alive[b] = true
return nil
}
if err := codexAppRelaunch(context.Background()); err != nil {
t.Fatal(err)
}
if !slices.Equal(quit, []string{chatgpt}) || !slices.Equal(opened, []string{chatgpt}) || !alive[other] {
t.Fatalf("quit %q, opened %q, the other app running %v", quit, opened, alive[other])
}
// one that doesn't quit: said, not opened again
quit, opened, stays = nil, nil, true
if err := codexAppRelaunch(context.Background()); err == nil || len(opened) > 0 {
t.Fatalf("an app that stayed: %v, opened %q", err, opened)
}
// not running: nothing
quit, alive[chatgpt] = nil, false
if err := codexAppRelaunch(context.Background()); err != nil || len(quit) > 0 {
t.Fatalf("not running: %v, quit %q", err, quit)
}
restartOnMac, restartOnLinux = false, true
alive[chatgpt] = true
if err := codexAppRelaunch(context.Background()); err == nil || len(quit) > 0 {
t.Fatalf("linux: %v, quit %q", err, quit)
}
}