provider: Codex's idle check reads a sessions folder in one listing
While Codex is idle each look of KeepOnAnAccountWithRoom asks whether a session was written in the last 30 minutes, and the answer walked every session file of the history: a directory read, a joined path and an lstat() each. With a real-account HOME that walk was the largest thing an idle magpie allocated once dsh's model build went (readdir 3.5 MB, paths and lstat names 2.5 MB of 15 MB in 4 minutes), and a long history (~20k rollouts) is as many syscalls a look. codexSessionsQuietListed reads each folder with dirent.Stats (getattrlistbulk on macOS, Lstat's own times), and the walk stays where there is no bulk listing (Linux, Windows) or a folder can't be read so. The listings are not kept between looks. Run: - mote: go test -count=1 ./internal/provider ok (148s); GOOS=windows and GOOS=linux (-tags nogui) go vet ./internal/provider ok; gofmt clean - xiaomi-gpu (Windows): go test -run TestCodexSessionsQuiet ok (walk path) Broken by hand: the listed path never counting a file new, both TestCodexSessionsQuietFor and TestCodexSessionsQuietListed are red. Not tried: Linux (WSL box down); its path is the unchanged walk.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user