diff --git a/internal/provider/codex_app_server.go b/internal/provider/codex_app_server.go index a21b60def..e79073068 100644 --- a/internal/provider/codex_app_server.go +++ b/internal/provider/codex_app_server.go @@ -15,6 +15,7 @@ import ( "sync" "time" + "github.com/yetone/magpie/internal/dirent" "github.com/yetone/magpie/internal/proc" ) @@ -711,6 +712,9 @@ func codexTurnOpen(home string, now, since time.Time) bool { // written in the last d. A sessions folder that can't be read counts as // written: a turn cut short costs more than a restart a little later. func codexSessionsQuietFor(home string, now time.Time, d time.Duration) bool { + if quiet, ok := codexSessionsQuietListed(filepath.Join(home, "sessions"), now, d); ok { + return quiet + } quiet := true err := filepath.WalkDir(filepath.Join(home, "sessions"), func(path string, e fs.DirEntry, err error) error { if err != nil { @@ -738,6 +742,46 @@ func codexSessionsQuietFor(home string, now time.Time, d time.Duration) bool { return quiet && err == nil } +// codexSessionsQuietListed is codexSessionsQuietFor read with one listing +// a folder (dirent.Stats) where the system has one, not a listing and an +// lstat() a file: Codex idle runs it on every look while Codex is idle, +// over every session file of the history, and each walk allocated a +// listing, a path and an lstat's answer a file. ok is false where a folder +// can't be read so (no bulk listing, Windows, an error), and the caller +// walks the folder itself. The listings are not kept for the next read: +// held between looks minutes apart they cost more than they save. +func codexSessionsQuietListed(dir string, now time.Time, d time.Duration) (quiet, ok bool) { + if _, err := os.Lstat(dir); errors.Is(err, fs.ErrNotExist) { + return true, true + } + quiet = true + var in func(dir string) bool + in = func(dir string) bool { + entries, ok := dirent.Stats(dir) + if !ok { + return false + } + for _, e := range entries { + switch { + case e.Mode == fs.ModeDir: + if !in(filepath.Join(dir, e.Name)) { + return false + } + case strings.HasSuffix(e.Name, ".jsonl") && now.Sub(e.ModTime()) < d: + quiet = false + } + if !quiet { + return true + } + } + return true + } + if !in(dir) { + return false, false + } + return quiet, true +} + // 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 diff --git a/internal/provider/codex_quiet_listed_test.go b/internal/provider/codex_quiet_listed_test.go new file mode 100644 index 000000000..dfd84c693 --- /dev/null +++ b/internal/provider/codex_quiet_listed_test.go @@ -0,0 +1,74 @@ +package provider + +import ( + "os" + "path/filepath" + "runtime" + "testing" + "time" +) + +// Codex's idle check reads each sessions folder in one listing on macOS, +// and answers as the walk with an lstat() a file does: a file written +// within d deep in an old day folder is not quiet, older ones are, and a +// folder that can't be read is not quiet either. +func TestCodexSessionsQuietListed(t *testing.T) { + home := t.TempDir() + root := filepath.Join(home, "sessions") + now := time.Now() + old := now.Add(-time.Hour) + write := func(rel string, at time.Time) string { + p := filepath.Join(root, rel) + if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(p, []byte("{}\n"), 0o644); err != nil { + t.Fatal(err) + } + if err := os.Chtimes(p, at, at); err != nil { + t.Fatal(err) + } + return p + } + both := func(want bool, what string) { + t.Helper() + if got := codexSessionsQuietFor(home, now, time.Minute); got != want { + t.Fatalf("%s: quiet %v, want %v", what, got, want) + } + got, ok := codexSessionsQuietListed(root, now, time.Minute) + if runtime.GOOS == "darwin" && !ok { + t.Fatalf("%s: no one-listing read on macOS", what) + } + if ok && got != want { + t.Fatalf("%s: listed quiet %v, want %v", what, got, want) + } + } + both(true, "no sessions folder") + for _, day := range []string{"2026/09/01", "2026/10/30", "2026/11/11"} { + write(day+"/rollout-a.jsonl", old) + write(day+"/rollout-b.jsonl", old) + } + write("2026/11/11/notes.txt", now) + both(true, "only old sessions, a new file that is no session") + resumed := write("2026/09/01/rollout-c.jsonl", now) + both(false, "a session in an old day folder written just now") + if err := os.Chtimes(resumed, old, old); err != nil { + t.Fatal(err) + } + both(true, "that session an hour old") + + if runtime.GOOS == "windows" || os.Geteuid() == 0 { + return + } + locked := filepath.Join(root, "2026", "08") + if err := os.MkdirAll(locked, 0o000); err != nil { + t.Fatal(err) + } + t.Cleanup(func() { os.Chmod(locked, 0o755) }) + if codexSessionsQuietFor(home, now, time.Minute) { + t.Fatal("a folder that can't be read: quiet") + } + if _, ok := codexSessionsQuietListed(root, now, time.Minute); ok { + t.Fatal("a folder that can't be read: the one-listing read answered for it") + } +}