Files
magpie/internal/agent/codex_app_crash_test.go
T
yetone f7cf17290c codex: the app's logs and sessions are read at each file's own time, not the Windows folder listing's
bitssly on X (tweet 2118722838249234609, v0.1.3177, Store Codex
26.1029.7803.0, app-server 0.166.0-alpha.6): when Codex's usage ran out
the Codex app flickered, showed "Hosted AppServerManager identity is not
ready" (fatal_error_broadcasted) and had to be quit and opened again,
four times in a day.

The page is the app's own bug after its app-server is ended (#4550), and
magpie ends it on purpose when it becomes Codex's provider for the
used-up account. 4f624241f reads the app's log for the page and reopens
the app, but on Windows it never saw it: codexLogFiles kept only files
whose fs.DirEntry Info() time was after the end, and on Windows that is
the folder listing's time, which NTFS brings up to date only as a handle
on the file is closed. The app keeps its day's log open, so the file the
page went to listed hours old and was skipped. On the Windows box the
Store app's own log listed 10:27Z (os.ReadDir and Get-ChildItem) while
the app wrote to it at 15:22Z (os.Lstat, File.GetLastWriteTimeUtc).

The same listing time judged Codex's session files, which the
app-server holds open: codexTurnOpen skipped a turn running in a thread
opened over 30 minutes before, and codexSessionsQuietFor counted such a
session quiet, so on Windows the app-server could be ended under a
running turn.

- agent/codex_app_crash.go: codexLogFiles reads os.Lstat per file.
- provider/codex_app_server.go: sessionFileInfo (os.Lstat) for
  codexTurnOpen and codexSessionsQuietFor; a file gone between the
  listing and the stat is skipped.
- The other readers that judge a file another program may hold open by
  its listing time (sessions, claude_out_soon, subscription_usage,
  claude_subscription's idle sweep) are #5549. magpie's first look after
  its own restart ending an app-server that already started under the
  takeover (one of bitssly's four pages came a minute after an
  auto-update) is #5550.

Run:
- Windows box (win-run), Store Codex 26.1029.7803.0 installed there:
  the two new tests red on the old code
  (TestCodexAppCrashedReadsAFileTheAppHoldsOpen: crashed false;
  TestCodexSessionHeldOpenReadAtItsOwnTime: "not open"), green with the
  change; go test -run 'TestCodexApp|TestCodexSession|TestCodexTurn'
  ./internal/agent ./internal/provider ok. A Go probe on the box read
  the live Store log at listing 15:22:00Z, own 15:32:02Z.
- Broken by hand on Windows: codexSessionsQuietFor back on e.Info()
  makes TestCodexSessionHeldOpenReadAtItsOwnTime red ("quiet"); the
  test checks quiet before the turn, since reading the file closes a
  handle and Windows then brings the listing up to date.
- mote at this head on origin/main 80ae904c2: go test ./... -count=1
  -timeout 45m: 86 packages ok, red only in internal/gui
  TestMainWindowCorners (#3949, open, AppKit on mote's macOS 27).
- Rebased onto origin/main f9a88c4fb (1fd0bb967 update, f9a88c4fb wsl
  touched agent and provider): at this head go test ./internal/agent
  ./internal/provider ./internal/netproxy ./internal/update
  ./internal/plugin ./internal/pii ok on mote, GOOS=windows go vet of
  agent and provider ok.
- go vet ./... (darwin), GOOS=windows go vet ./... ok; GOOS=linux go
  vet ./internal/agent ./internal/provider ok; gofmt -l clean.
- On macOS and Linux the listing already carries the file's own time,
  so the tests pass there on old and new code alike.

Not tried: a real used-up ChatGPT account reaching the page and magpie
reopening the Store app (the reopen itself is #4550's, unchanged).
2026-11-07 00:07:00 +08:00

266 lines
11 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)
}
}
}
// The app keeps a day's log file open and writes on to it, and Windows
// stamps a file in its folder listing only as it is closed: the Codex
// app's file on the Windows box listed 10:27Z while the app wrote to it at
// 15:22Z. Read by the listing's time, the file the page was written to
// counted as untouched since the end, the page was never seen, and the
// app was left on it till the user quit and opened it (bitssly on X,
// Store Codex 26.1029). The file is held open here as the app holds it,
// its stamp set back and written to after, which on Windows leaves the
// listing on the old stamp (red there on a listing's time).
func TestCodexAppCrashedReadsAFileTheAppHoldsOpen(t *testing.T) {
home := restartSandbox(t)
restartOnMac = true
root := filepath.Join(home, "Library", "Logs", "com.openai.codex")
since := time.Now()
dir := filepath.Join(root, since.UTC().Format("2006"), since.UTC().Format("01"), since.UTC().Format("02"))
if err := os.MkdirAll(dir, 0o755); err != nil {
t.Fatal(err)
}
p := filepath.Join(dir, "codex-desktop-a57d47b4-b287-48b4-9ecb-f9671478ca87-33908-t0-i1-000219-0.log")
f, err := os.Create(p)
if err != nil {
t.Fatal(err)
}
defer f.Close()
if _, err := f.WriteString(exitedLine(since)); err != nil {
t.Fatal(err)
}
opened := since.Add(-5 * time.Hour)
if err := os.Chtimes(p, opened, opened); err != nil {
t.Fatal(err)
}
if _, err := f.WriteString(fatalLine(since.Add(time.Second))); err != nil {
t.Fatal(err)
}
at, crashed, known := codexAppCrashed(since)
if !crashed || !known || !at.Equal(since.Add(time.Second).UTC().Truncate(time.Millisecond)) {
t.Fatalf("the page in the file the app holds open: at %v, crashed %v, known %v", at, 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)
}
}