Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

sessions: a Codex listing reuses the last one's names, and dirent its room (#2278)

yetone7fd65aad5682
diff --git a/internal/dirent/stats.go b/internal/dirent/stats.go
index 7bca64aeb..a2167732b 100644
--- a/internal/dirent/stats.go
+++ b/internal/dirent/stats.go
@@ -4,6 +4,7 @@ import (
"cmp"
"io/fs"
"slices"
+ "sync"
"time"
)
@@ -41,11 +42,33 @@ func (l Listing) Get(name string) (Stat, bool) {
// (getattrlistbulk on macOS), rather than one per entry. ok is false where
// the system has none or the folder couldn't be read, and the caller asks
// after each entry itself.
+//
+// A caller done reading the listing gives it back (Done) for the next.
func Stats(dir string) (Listing, bool) {
- l, ok := stats(dir)
+ var l Listing
+ if p, _ := listings.Get().(*Listing); p != nil {
+ l = *p
+ }
+ l, ok := stats(dir, l)
if !ok {
return nil, false
}
slices.SortFunc(l, func(a, b Entry) int { return cmp.Compare(a.Name, b.Name) })
return l, true
}
+
+// listings are Stats' answers given back: a listing made new for each
+// folder, grown an entry at a time, was ~95 MB of a minute of the page's
+// polls (#2278).
+var listings sync.Pool
+
+// Done gives l back to be the next Stats' answer: whoever had it reads it
+// no more. A Get of it before is only a copy of its entry.
+func (l Listing) Done() {
+ if cap(l) == 0 || cap(l) > 1<<14 {
+ return // none, or a folder of more files than the pool keeps room for
+ }
+ clear(l[:cap(l)])
+ l = l[:0]
+ listings.Put(&l)
+}
diff --git a/internal/dirent/stats_darwin.go b/internal/dirent/stats_darwin.go
index 4c1cac4d4..fd0cb5b0c 100644
--- a/internal/dirent/stats_darwin.go
+++ b/internal/dirent/stats_darwin.go
@@ -3,6 +3,7 @@ package dirent
import (
"encoding/binary"
"io/fs"
+ "sync"
"syscall"
"time"
"unsafe"
@@ -31,18 +32,26 @@ type attrList struct {
fork uint32
}
+// bufs are getattrlistbulk's answers, one a folder being read, kept for
+// the next of a poll's few hundred Codex day folders and Claude Code
+// projects (#2278). Nothing is kept of one once its folder's entries are
+// read: each name is copied.
+var bufs = sync.Pool{New: func() any { b := make([]byte, 64<<10); return &b }}
+
// stats reads dir's entries with getattrlistbulk: a Sessions read stats
// each of ~21k session files on every 15 s poll, ~10 µs a file on APFS,
// and this is one call for every few hundred.
-func stats(dir string) (Listing, bool) {
+func stats(dir string, out Listing) (Listing, bool) {
fd, err := unix.Open(dir, unix.O_RDONLY|unix.O_DIRECTORY|unix.O_CLOEXEC, 0)
if err != nil {
return nil, false
}
defer unix.Close(fd)
al := attrList{bitmapCount: attrBitMapCount, common: attrCmnReturned | attrCmnName | attrCmnObjType | attrCmnModTime, file: attrFileDataLen}
- buf := make([]byte, 64<<10)
- var out Listing
+ bp := bufs.Get().(*[]byte)
+ defer bufs.Put(bp)
+ buf := *bp
+ out = out[:0]
for {
n, _, errno := syscall.Syscall6(unix.SYS_GETATTRLISTBULK, uintptr(fd), uintptr(unsafe.Pointer(&al)), uintptr(unsafe.Pointer(&buf[0])), uintptr(len(buf)), 0, 0)
if errno == syscall.EINTR {
diff --git a/internal/dirent/stats_other.go b/internal/dirent/stats_other.go
index 0a8c67d9d..f7775c4fd 100644
--- a/internal/dirent/stats_other.go
+++ b/internal/dirent/stats_other.go
@@ -2,4 +2,4 @@
package dirent
-func stats(string) (Listing, bool) { return nil, false }
+func stats(string, Listing) (Listing, bool) { return nil, false }
diff --git a/internal/dirent/stats_test.go b/internal/dirent/stats_test.go
index aadee9592..a7ee75d69 100644
--- a/internal/dirent/stats_test.go
+++ b/internal/dirent/stats_test.go
@@ -95,3 +95,35 @@ func TestStatsAsLstat(t *testing.T) {
t.Error("Stats read a folder that isn't there")
}
}
+
+// A listing given back (Done) is the next one's room, and holds nothing of
+// its folder: a smaller folder read into it has its own entries only.
+func TestStatsAfterDone(t *testing.T) {
+ if runtime.GOOS != "darwin" {
+ t.Skip("listed in one call a folder on macOS only")
+ }
+ big, small := t.TempDir(), t.TempDir()
+ for i := range 40 {
+ if err := os.WriteFile(filepath.Join(big, fmt.Sprintf("b%02d", i)), nil, 0o644); err != nil {
+ t.Fatal(err)
+ }
+ }
+ if err := os.WriteFile(filepath.Join(small, "s"), []byte("abc"), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ for range 3 {
+ l, ok := Stats(big)
+ if !ok || len(l) != 40 {
+ t.Fatalf("big folder: %d entries, ok %t", len(l), ok)
+ }
+ l.Done()
+ s, ok := Stats(small)
+ if !ok || len(s) != 1 || s[0].Name != "s" || s[0].Size != 3 {
+ t.Fatalf("small folder after a big one given back: %+v, ok %t", s, ok)
+ }
+ if rest := s[1:cap(s)]; slices.ContainsFunc(rest, func(e Entry) bool { return e.Name != "" }) {
+ t.Fatalf("a listing given back kept its folder's entries: %+v", rest)
+ }
+ s.Done()
+ }
+}
diff --git a/internal/sessions/codex_discovery.go b/internal/sessions/codex_discovery.go
index c7984a244..c76e08fd8 100644
--- a/internal/sessions/codex_discovery.go
+++ b/internal/sessions/codex_discovery.go
@@ -6,6 +6,7 @@ import (
"os"
"path/filepath"
"reflect"
+ "slices"
"strings"
"sync"
"time"
@@ -99,12 +100,21 @@ func sameCodexSnapshot(a, b os.FileInfo) bool {
// Only a proven decoded prefix is a redundant copy. A shared thread/name
// alone cannot establish ownership of divergent logs or distinct segments.
func distinctCodexFiles(files []file) []file {
- groups := map[string][]int{}
+ // a name's first file, and the rest of its files, which few names
+ // have: a slice made for every name was ~45 MB of a minute of the
+ // page's polls (#2278)
+ first := make(map[string]int, len(files))
+ var more map[string][]int
keep := make([]bool, len(files))
for i, f := range files {
keep[i] = true
name := strings.TrimSuffix(filepath.Base(f.path), zstSuffix)
- for _, j := range groups[name] {
+ j0, seen := first[name]
+ if !seen {
+ first[name] = i
+ continue
+ }
+ for _, j := range slices.Concat([]int{j0}, more[name]) {
if !keep[j] {
continue
}
@@ -118,7 +128,10 @@ func distinctCodexFiles(files []file) []file {
break
}
}
- groups[name] = append(groups[name], i)
+ if more == nil {
+ more = map[string][]int{}
+ }
+ more[name] = append(more[name], i)
}
out := files[:0]
for i, f := range files {
diff --git a/internal/sessions/sessions.go b/internal/sessions/sessions.go
index 2b25591dd..564223330 100644
--- a/internal/sessions/sessions.go
+++ b/internal/sessions/sessions.go
@@ -519,6 +519,9 @@ func statFiles(files []file, follow bool) []file {
f.size, f.mod, ok[i] = fi.Size(), fi.ModTime(), true
}
})
+ for _, l := range listed {
+ l.Done()
+ }
kept := files[:0]
for i, f := range files {
if ok[i] {
@@ -883,6 +886,12 @@ func codexFilesIn(dir string) []file {
// rolloutsListed is how many rollouts each root's last listing found.
var rolloutsListed sync.Map
+// rolloutsSeen are each folder's rollouts as its last listing found them,
+// in name order: their paths and keys, made again for each of ~20k
+// rollouts on every listing, were ~50 MB of a minute of the page's polls
+// (#2278).
+var rolloutsSeen sync.Map // shown folder → []file
+
// listedRollouts are the rollouts under root, in walkLinked's order and
// with their sizes and times, read with one listing a folder
// (dirent.Stats) rather than a listing and an lstat() a file: Codex's day
@@ -916,19 +925,41 @@ func listedRollouts(root string) (out []file, ok bool) {
if !ok {
return false
}
+ defer entries.Done()
+ prev, _ := rolloutsSeen.Load(shown)
+ seen, _ := prev.([]file)
+ start, made, leaf := len(out), false, true
for _, e := range entries {
name := e.Name
switch e.Mode {
case fs.ModeDir:
+ leaf = false
if !in(filepath.Join(real, name), filepath.Join(shown, name)) {
return false
}
case 0:
+ // both in name order: a rollout listed before keeps its
+ // path and key, and only its size and time are new
+ for len(seen) > 0 && filepath.Base(seen[0].path) < name {
+ seen = seen[1:]
+ }
+ if len(seen) > 0 && filepath.Base(seen[0].path) == name {
+ f := seen[0]
+ f.size, f.mod = e.Size, e.Mod
+ out = append(out, f)
+ continue
+ }
if m := rolloutName.FindStringSubmatch(name); m != nil {
out = append(out, file{agent: "codex", key: "codex:" + m[1], path: filepath.Join(shown, name), main: true, size: e.Size, mod: e.Mod})
+ made = true
}
}
}
+ // a day folder's only: in a folder with folders in it, out has
+ // their rollouts among its own
+ if n, _ := prev.([]file); leaf && (made || len(out)-start != len(n)) {
+ rolloutsSeen.Store(shown, slices.Clone(out[start:]))
+ }
return true
}
if !in(walk, root) {
diff --git a/internal/sessions/stat_files_listing_test.go b/internal/sessions/stat_files_listing_test.go
index 62c2181a0..871db3221 100644
--- a/internal/sessions/stat_files_listing_test.go
+++ b/internal/sessions/stat_files_listing_test.go
@@ -6,9 +6,11 @@ import (
"path/filepath"
"runtime"
"slices"
+ "strings"
"sync/atomic"
"testing"
"time"
+ "unsafe"
)
// statFiles gives each file what os.Stat (follow) or os.Lstat gives it, and
@@ -175,3 +177,56 @@ func TestListedRolloutsAsWalked(t *testing.T) {
t.Error("a sessions folder that isn't there was listed")
}
}
+
+// A listing after the first keeps each rollout's path and key from it
+// (#2278), and still gives each its size and time now, a new rollout and
+// none that went.
+func TestListedRolloutsKeepTheirNames(t *testing.T) {
+ if runtime.GOOS != "darwin" {
+ t.Skip("listed in one call a folder on macOS only")
+ }
+ root := filepath.Join(t.TempDir(), "sessions")
+ day := filepath.Join(root, "2026", "10", "30")
+ if err := os.MkdirAll(day, 0o700); err != nil {
+ t.Fatal(err)
+ }
+ name := func(i int) string {
+ return filepath.Join(day, fmt.Sprintf("rollout-2026-10-30T10-00-0%d-0190aaaa-1111-7222-8333-44445555666%d.jsonl", i, i))
+ }
+ for i := range 3 {
+ if err := os.WriteFile(name(i), []byte("{}\n"), 0o600); err != nil {
+ t.Fatal(err)
+ }
+ }
+ first, ok := listedRollouts(root)
+ if !ok || len(first) != 3 {
+ t.Fatalf("first listing: %d rollouts, ok %t", len(first), ok)
+ }
+ if err := os.WriteFile(name(0), []byte("{}\n{}\n"), 0o600); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.Remove(name(1)); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(name(5), []byte("{}\n"), 0o600); err != nil {
+ t.Fatal(err)
+ }
+ again, ok := listedRollouts(root)
+ if !ok || len(again) != 3 {
+ t.Fatalf("second listing: %d rollouts, ok %t", len(again), ok)
+ }
+ for i, want := range []string{name(0), name(2), name(5)} {
+ if again[i].path != want || again[i].key != "codex:"+strings.TrimSuffix(filepath.Base(want)[28:], ".jsonl") {
+ t.Errorf("rollout %d: %s %s, want %s", i, again[i].path, again[i].key, want)
+ }
+ }
+ if again[0].size != 6 {
+ t.Errorf("a grown rollout's size: %d, want 6", again[0].size)
+ }
+ if unsafe.StringData(again[0].path) != unsafe.StringData(first[0].path) || unsafe.StringData(again[1].key) != unsafe.StringData(first[2].key) {
+ t.Error("a rollout listed before had its path and key made again")
+ }
+ if unsafe.StringData(again[2].path) == unsafe.StringData(first[2].path) {
+ t.Error("a new rollout took another's path")
+ }
+}