diff --git a/internal/agent/codex_app_crash.go b/internal/agent/codex_app_crash.go index 4a7727957..475262a5b 100644 --- a/internal/agent/codex_app_crash.go +++ b/internal/agent/codex_app_crash.go @@ -106,8 +106,15 @@ func codexLogFiles(root string, from time.Time) (files []string, ok bool) { if e.IsDir() || !strings.HasSuffix(e.Name(), ".log") { continue } - if info, err := e.Info(); err == nil && !info.ModTime().Before(from) { - files = append(files, filepath.Join(d, e.Name())) + // the file's own time, never the folder listing's: Windows + // stamps a file in its folder only as it is closed, and the + // app keeps a day's file open (on the Windows box the + // listing said 10:27Z while the app wrote on at 15:22Z), so + // the file being written to read as older than from and its + // page was never seen (bitssly on X) + p := filepath.Join(d, e.Name()) + if info, err := os.Lstat(p); err == nil && !info.ModTime().Before(from) { + files = append(files, p) } } } diff --git a/internal/agent/codex_app_crash_test.go b/internal/agent/codex_app_crash_test.go index 0586fecdd..5661ee796 100644 --- a/internal/agent/codex_app_crash_test.go +++ b/internal/agent/codex_app_crash_test.go @@ -108,6 +108,46 @@ func TestCodexAppCrashedReadsItsOwnLogs(t *testing.T) { } } +// 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 diff --git a/internal/provider/codex_app_server.go b/internal/provider/codex_app_server.go index cc52be751..57be4386f 100644 --- a/internal/provider/codex_app_server.go +++ b/internal/provider/codex_app_server.go @@ -652,7 +652,7 @@ func codexTurnOpen(home string, now, since time.Time) bool { if d.IsDir() || !strings.HasSuffix(d.Name(), ".jsonl") { return nil } - info, err := d.Info() + info, err := sessionFileInfo(path) if err != nil || now.Sub(info.ModTime()) > codexTurnWindow { return nil } @@ -677,7 +677,10 @@ func codexSessionsQuietFor(home string, now time.Time, d time.Duration) bool { if e.IsDir() || !strings.HasSuffix(e.Name(), ".jsonl") { return nil } - info, err := e.Info() + info, err := sessionFileInfo(path) + if errors.Is(err, fs.ErrNotExist) { + return nil + } if err != nil { return err } @@ -690,6 +693,16 @@ func codexSessionsQuietFor(home string, now time.Time, d time.Duration) bool { return quiet && err == nil } +// sessionFileInfo is the session file at path's own time and size, never +// the folder listing's (a fs.DirEntry's Info): Windows stamps a file in +// its folder only as it is closed, and Codex keeps a session's file open +// while the app-server runs it, so the listing has the time it was +// opened (os.ReadDir on the Windows box: a file held open and written +// to an hour after its stamp still listed the stamp, at its old size). +// A turn running in a thread opened more than codexTurnWindow before +// then counted as no turn, and its app-server was ended under it. +func sessionFileInfo(path string) (fs.FileInfo, error) { return os.Lstat(path) } + // sessionTurnTail is how much of a session file's end sessionTurnOpen reads. const sessionTurnTail = 512 << 10 diff --git a/internal/provider/codex_app_server_test.go b/internal/provider/codex_app_server_test.go index 7fc6d8bdb..6a16a3f14 100644 --- a/internal/provider/codex_app_server_test.go +++ b/internal/provider/codex_app_server_test.go @@ -1059,6 +1059,49 @@ func TestCodexHandSwitchOffAHeldAccountIsSpent(t *testing.T) { } } +// Codex keeps a session's file open while its app-server runs the +// thread, and Windows stamps a file in its folder listing only as it is +// closed. Read by the listing's time, a turn running in a thread opened +// more than codexTurnWindow before counted as none, and the sessions as +// quiet, so magpie ended the app-server under the turn (bitssly on X). +// The file is held open here as Codex 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 TestCodexSessionHeldOpenReadAtItsOwnTime(t *testing.T) { + home := t.TempDir() + dir := filepath.Join(home, "sessions", "2026", "11", "06") + if err := os.MkdirAll(dir, 0o755); err != nil { + t.Fatal(err) + } + p := filepath.Join(dir, "rollout-2026-11-06T09-00-00-held.jsonl") + f, err := os.Create(p) + if err != nil { + t.Fatal(err) + } + defer f.Close() + started := `{"type":"event_msg","payload":{"type":"task_started","turn_id":"1"}}` + "\n" + done := `{"type":"event_msg","payload":{"type":"task_complete","turn_id":"1"}}` + "\n" + if _, err := f.WriteString(started + done); err != nil { + t.Fatal(err) + } + opened := time.Now().Add(-codexTurnWindow - time.Hour) + if err := os.Chtimes(p, opened, opened); err != nil { + t.Fatal(err) + } + if _, err := f.WriteString(started); err != nil { + t.Fatal(err) + } + // quiet first: reading the file, as codexTurnOpen does, closes a + // handle on it, and Windows brings the listing up to date then + now := time.Now() + if codexSessionsQuietFor(home, now, time.Minute) { + t.Fatal("a session Codex wrote to just now, held open: quiet") + } + if !codexTurnOpen(home, now, time.Time{}) { + t.Fatal("a turn running in a file Codex holds open: not open") + } +} + // codexSessionsQuietFor reads the sessions' write times: a file written // within d is not quiet, an older one or none is, and a sessions folder // that can't be read is not.