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).
This commit is contained in:
yetone
2026-11-07 00:07:00 +08:00
parent f9a88c4fbe
commit f7cf17290c
4 changed files with 107 additions and 4 deletions
+9 -2
View File
@@ -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)
}
}
}
+40
View File
@@ -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
+15 -2
View File
@@ -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
@@ -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.