Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.
sessions: each session folder is read in one listing on macOS, not a stat() a file
yetone7b2a330a560f
diff --git a/internal/dirent/stats.go b/internal/dirent/stats.gonew file mode 100644index 000000000..a41451341--- /dev/null+++ b/internal/dirent/stats.go@@ -0,0 +1,24 @@+package dirent++import (+ "io/fs"+ "time"+)++// Stat is what a folder's listing says of one entry: its kind (Mode's type+// bits, 0 for a regular file), and a regular file's size and modification+// time, as os.Lstat gives them.+type Stat struct {+ Mode fs.FileMode+ Size int64+ Mod time.Time+}++// Stats is each entry of dir by name, as os.Lstat gives it, read in a few+// calls for the whole folder where the system has them (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.+func Stats(dir string) (map[string]Stat, bool) {+ return stats(dir)+}diff --git a/internal/dirent/stats_darwin.go b/internal/dirent/stats_darwin.gonew file mode 100644index 000000000..9e64b8c24--- /dev/null+++ b/internal/dirent/stats_darwin.go@@ -0,0 +1,131 @@+package dirent++import (+ "encoding/binary"+ "io/fs"+ "syscall"+ "time"+ "unsafe"++ "golang.org/x/sys/unix"+)++// getattrlistbulk's attributes, from <sys/attr.h> and <sys/vnode.h>.+const (+ attrBitMapCount = 5+ attrCmnName = 0x00000001+ attrCmnObjType = 0x00000008+ attrCmnModTime = 0x00000400+ attrCmnReturned = 0x80000000+ attrFileDataLen = 0x00000200+ vReg, vDir, vLink = 1, 2, 5+)++type attrList struct {+ bitmapCount uint16+ _ uint16+ common uint32+ vol uint32+ dir uint32+ file uint32+ fork uint32+}++// 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) (map[string]Stat, 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)+ out := map[string]Stat{}+ 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 {+ continue+ }+ if errno != 0 {+ return nil, false+ }+ if n == 0 {+ return out, true+ }+ b := buf+ for range int(n) {+ if !entry(b, out) {+ return nil, false+ }+ b = b[binary.NativeEndian.Uint32(b):]+ }+ }+}++// entry reads one entry of getattrlistbulk's answer into out: its length,+// the attributes it returned, then each in the order of their bits, a+// file's data length after the common ones.+func entry(b []byte, out map[string]Stat) bool {+ if len(b) < 4 {+ return false+ }+ size := int(binary.NativeEndian.Uint32(b))+ if size < 4+20 || size > len(b) {+ return false+ }+ e := b[:size]+ common := binary.NativeEndian.Uint32(e[4:])+ file := binary.NativeEndian.Uint32(e[16:])+ p := e[24:]+ var s Stat+ var name string+ if common&attrCmnName != 0 {+ if len(p) < 8 {+ return false+ }+ off, n := int(int32(binary.NativeEndian.Uint32(p))), int(binary.NativeEndian.Uint32(p[4:]))+ at := len(e) - len(p) + off+ if off < 0 || n < 1 || at+n > len(e) {+ return false+ }+ name = string(e[at : at+n-1])+ p = p[8:]+ }+ typ := uint32(0)+ if common&attrCmnObjType != 0 {+ if len(p) < 4 {+ return false+ }+ typ = binary.NativeEndian.Uint32(p)+ p = p[4:]+ }+ if common&attrCmnModTime != 0 {+ if len(p) < 16 {+ return false+ }+ s.Mod = time.Unix(int64(binary.NativeEndian.Uint64(p)), int64(binary.NativeEndian.Uint64(p[8:])))+ p = p[16:]+ }+ if file&attrFileDataLen != 0 {+ if len(p) < 8 {+ return false+ }+ s.Size = int64(binary.NativeEndian.Uint64(p))+ }+ switch typ {+ case vReg:+ case vDir:+ s.Mode = fs.ModeDir+ case vLink:+ s.Mode = fs.ModeSymlink+ default:+ s.Mode = fs.ModeIrregular+ }+ if name == "" || common&attrCmnName == 0 || common&attrCmnObjType == 0 || common&attrCmnModTime == 0 || typ == vReg && file&attrFileDataLen == 0 {+ return false+ }+ out[name] = s+ return true+}diff --git a/internal/dirent/stats_other.go b/internal/dirent/stats_other.gonew file mode 100644index 000000000..f825fb9a8--- /dev/null+++ b/internal/dirent/stats_other.go@@ -0,0 +1,5 @@+//go:build !darwin++package dirent++func stats(string) (map[string]Stat, bool) { return nil, false }diff --git a/internal/dirent/stats_test.go b/internal/dirent/stats_test.gonew file mode 100644index 000000000..0501b613e--- /dev/null+++ b/internal/dirent/stats_test.go@@ -0,0 +1,93 @@+package dirent++import (+ "fmt"+ "os"+ "path/filepath"+ "runtime"+ "strings"+ "testing"+ "time"+)++// Stats says of each entry what os.Lstat says: a file's size and time to+// the nanosecond, a folder, a link (to a file, a folder, or nothing) as a+// link, in a folder of a few entries and in one too long for one call.+func TestStatsAsLstat(t *testing.T) {+ dir, elsewhere := t.TempDir(), t.TempDir()+ write := func(name string, n int, at time.Time) {+ p := filepath.Join(dir, name)+ if err := os.WriteFile(p, []byte(strings.Repeat("x", n)), 0o644); err != nil {+ t.Fatal(err)+ }+ if err := os.Chtimes(p, at, at); err != nil {+ t.Fatal(err)+ }+ }+ write("a.jsonl", 0, time.Unix(1_700_000_000, 123_456_789))+ write("日本語 session.jsonl", 70_000, time.Unix(1_800_000_000, 1))+ if err := os.Mkdir(filepath.Join(dir, "folder"), 0o755); err != nil {+ t.Fatal(err)+ }+ if runtime.GOOS != "windows" {+ if err := os.WriteFile(filepath.Join(elsewhere, "f"), []byte("abc"), 0o644); err != nil {+ t.Fatal(err)+ }+ for name, to := range map[string]string{"tofile": filepath.Join(elsewhere, "f"), "tofolder": elsewhere, "broken": filepath.Join(elsewhere, "gone")} {+ if err := os.Symlink(to, filepath.Join(dir, name)); err != nil {+ t.Fatal(err)+ }+ }+ }+ check := func(dir string, want int) {+ got, ok := Stats(dir)+ if !ok {+ if runtime.GOOS == "darwin" {+ t.Fatalf("%s: Stats read nothing on macOS", dir)+ }+ return+ }+ if len(got) != want {+ t.Errorf("%s: Stats has %d entries, want %d", dir, len(got), want)+ }+ es, err := os.ReadDir(dir)+ if err != nil {+ t.Fatal(err)+ }+ for _, e := range es {+ fi, err := os.Lstat(filepath.Join(dir, e.Name()))+ if err != nil {+ t.Fatal(err)+ }+ s, ok := got[e.Name()]+ if !ok {+ t.Errorf("%q: not in Stats", e.Name())+ continue+ }+ if s.Mode != fi.Mode().Type() || !s.Mod.Equal(fi.ModTime()) || fi.Mode().IsRegular() && s.Size != fi.Size() {+ t.Errorf("%q: Stats says %v %d %v, Lstat %v %d %v", e.Name(), s.Mode, s.Size, s.Mod, fi.Mode().Type(), fi.Size(), fi.ModTime())+ }+ }+ }+ want := 3+ if runtime.GOOS != "windows" {+ want += 3+ }+ check(dir, want)++ // ~3000 long names are more than one call's 64 KB answer+ many := t.TempDir()+ for i := range 3000 {+ if err := os.WriteFile(filepath.Join(many, fmt.Sprintf("rollout-2026-10-27T06-%04d-%s.jsonl", i, strings.Repeat("n", 60))), []byte(strings.Repeat("y", i)), 0o644); err != nil {+ t.Fatal(err)+ }+ }+ check(many, 3000)++ if _, ok := Stats(filepath.Join(dir, "a.jsonl")); ok {+ t.Error("Stats read a file as a folder")+ }+ if _, ok := Stats(filepath.Join(dir, "gone")); ok {+ t.Error("Stats read a folder that isn't there")+ }+}diff --git a/internal/sessions/sessions.go b/internal/sessions/sessions.goindex 2c1237e91..71782de39 100644--- a/internal/sessions/sessions.go+++ b/internal/sessions/sessions.go@@ -408,40 +408,49 @@ func stat(f *file) bool {return true}-// statFiles fills in each file's size and modification time from info(i),-// or leaves the file out when it is gone or no regular file. Every-// Requests page walks every session folder again, and stat()ing ~16k files-// one after another was most of that walk (#3493), so they are stat()ed on-// a few goroutines.-func statFiles(files []file, info func(i int) (os.FileInfo, error)) []file {- ok := make([]bool, len(files))- one := func(i int) {- if fi, err := info(i); err == nil && fi.Mode().IsRegular() {- files[i].size, files[i].mod, ok[i] = fi.Size(), fi.ModTime(), true+// statFiles fills in each file's size and modification time, as os.Stat+// gives them when follow is set and os.Lstat when it isn't, or leaves the+// file out when it is gone or no regular file. Every Requests page and+// every 15 s Sessions poll walks every session folder again, and stat()ing+// ~21k files one after another was most of that walk (#3493): each folder+// is read in one listing where the system has one (dirent.Stats), and the+// rest are stat()ed on a few goroutines.+func statFiles(files []file, follow bool) []file {+ dirs := map[string]map[string]dirent.Stat{}+ var order []string+ for _, f := range files {+ d := filepath.Dir(f.path)+ if _, seen := dirs[d]; !seen {+ dirs[d] = nil+ order = append(order, d)}}- if workers := min(runtime.GOMAXPROCS(0), 8, len(files)/256); workers < 2 {- for i := range files {- one(i)- }- } else {- var next atomic.Int64- var wg sync.WaitGroup- for range workers {- wg.Go(func() {- for {- i := int(next.Add(64)) - 64- if i >= len(files) {- return- }- for j := i; j < min(i+64, len(files)); j++ {- one(j)- }- }- })+ listed := make([]map[string]dirent.Stat, len(order))+ parallel(len(order), 8, func(i int) {+ if s, ok := dirent.Stats(order[i]); ok {+ listed[i] = s}- wg.Wait()+ })+ for i, d := range order {+ dirs[d] = listed[i]}+ ok := make([]bool, len(files))+ parallel(len(files), 256, func(i int) {+ f := &files[i]+ if in := dirs[filepath.Dir(f.path)]; in != nil {+ s, there := in[filepath.Base(f.path)]+ if !there || s.Mode != 0 && (s.Mode != fs.ModeSymlink || !follow) {+ return+ }+ if s.Mode == 0 {+ f.size, f.mod, ok[i] = s.Size, s.Mod, true+ return+ }+ }+ if fi, err := statOne(f.path, follow); err == nil && fi.Mode().IsRegular() {+ f.size, f.mod, ok[i] = fi.Size(), fi.ModTime(), true+ }+ })kept := files[:0]for i, f := range files {if ok[i] {@@ -451,6 +460,43 @@ func statFiles(files []file, info func(i int) (os.FileInfo, error)) []file {return kept}+// statOne is a file statFiles asks after alone: where no listing said, or+// a link it follows.+var statOne = func(path string, follow bool) (os.FileInfo, error) {+ if follow {+ return os.Stat(path)+ }+ return os.Lstat(path)+}++// parallel runs one(i) for each i below n, on a few goroutines when there+// are more than per for each.+func parallel(n, per int, one func(i int)) {+ workers := min(runtime.GOMAXPROCS(0), 8, n/per)+ if workers < 2 {+ for i := range n {+ one(i)+ }+ return+ }+ var next atomic.Int64+ var wg sync.WaitGroup+ for range workers {+ wg.Go(func() {+ for {+ i := int(next.Add(64)) - 64+ if i >= n {+ return+ }+ for j := i; j < min(i+64, n); j++ {+ one(j)+ }+ }+ })+ }+ wg.Wait()+}+// ccFiles are the session files of an agent that keeps them as Claude Code// does, under its folder's projects/: a session's own <id>.jsonl in its// project's folder, its subagents' in <id>/subagents/, and the agents of@@ -494,7 +540,7 @@ func ccFiles(agent, dir string) []file {}}}- return statFiles(out, func(i int) (os.FileInfo, error) { return os.Stat(out[i].path) })+ return statFiles(out, true)}// allFiles are every agent's session files.@@ -664,9 +710,14 @@ func walkLinked(root string, fn fs.WalkDirFunc) error {// codexFilesIn are the rollouts in a Codex folder.func codexFilesIn(dir string) []file {var out []file- var entries []fs.DirEntryfor _, folder := range []string{"sessions", "archived_sessions"} {- walkLinked(filepath.Join(dir, folder), func(p string, d os.DirEntry, err error) error {+ root := filepath.Join(dir, folder)+ if listed, ok := listedRollouts(root); ok {+ out = append(out, listed...)+ continue+ }+ var found []file+ walkLinked(root, func(p string, d os.DirEntry, err error) error {if err != nil || d.IsDir() {return nil}@@ -674,12 +725,11 @@ func codexFilesIn(dir string) []file {if m == nil {return nil}- out = append(out, file{agent: "codex", key: "codex:" + m[1], path: p, main: true})- entries = append(entries, d)+ found = append(found, file{agent: "codex", key: "codex:" + m[1], path: p, main: true})return nil})+ out = append(out, statFiles(found, false)...)}- out = statFiles(out, func(i int) (os.FileInfo, error) { return entries[i].Info() })// During compression, prefer the readable plain twin in the same// directory: the new compressed file may not have its trailer yet.plain := map[string]bool{}@@ -697,6 +747,50 @@ func codexFilesIn(dir string) []file {return distinctCodexFiles(kept)}+// 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+// folders were listed twice and every rollout lstat()ed on each 15 s+// Sessions poll. ok is false when a folder couldn't be read so, and+// walkLinked and statFiles read root instead.+func listedRollouts(root string) (out []file, ok bool) {+ walk := root+ if fi, err := os.Lstat(root); err != nil {+ return nil, false+ } else if !fi.IsDir() {+ real, err := proc.RealPath(root)+ if err != nil {+ return nil, false+ }+ walk = real+ }+ var in func(real, shown string) bool+ in = func(real, shown string) bool {+ entries, ok := dirent.Stats(real)+ if !ok {+ return false+ }+ for _, name := range slices.Sorted(maps.Keys(entries)) {+ e := entries[name]+ switch e.Mode {+ case fs.ModeDir:+ if !in(filepath.Join(real, name), filepath.Join(shown, name)) {+ return false+ }+ case 0:+ 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})+ }+ }+ }+ return true+ }+ if !in(walk, root) {+ return nil, false+ }+ return out, true+}+// rolloutTwin is a Codex rollout's file in its other form: the compressed// one of a plain one, the plain one of a compressed one.func rolloutTwin(p string) string {diff --git a/internal/sessions/stat_files_listing_test.go b/internal/sessions/stat_files_listing_test.gonew file mode 100644index 000000000..62c2181a0--- /dev/null+++ b/internal/sessions/stat_files_listing_test.go@@ -0,0 +1,177 @@+package sessions++import (+ "fmt"+ "os"+ "path/filepath"+ "runtime"+ "slices"+ "sync/atomic"+ "testing"+ "time"+)++// statFiles gives each file what os.Stat (follow) or os.Lstat gives it, and+// leaves out what is gone or no regular file; on macOS it reads each folder+// in one listing and asks after a file alone only for a link it follows:+// the Sessions tab's every 15 s poll stat()ed all ~21k session files.+func TestStatFilesAsStat(t *testing.T) {+ dir, elsewhere := t.TempDir(), t.TempDir()+ at := time.Unix(1_790_000_000, 987_654_321)+ for i, name := range []string{"a.jsonl", "b.jsonl", "c.jsonl"} {+ p := filepath.Join(dir, name)+ if err := os.WriteFile(p, make([]byte, 10*i), 0o644); err != nil {+ t.Fatal(err)+ }+ if err := os.Chtimes(p, at, at.Add(time.Duration(i))); err != nil {+ t.Fatal(err)+ }+ }+ if err := os.WriteFile(filepath.Join(elsewhere, "real.jsonl"), []byte("linked"), 0o644); err != nil {+ t.Fatal(err)+ }+ names := []string{"a.jsonl", "b.jsonl", "c.jsonl", "gone.jsonl"}+ if runtime.GOOS != "windows" {+ if err := os.Symlink(filepath.Join(elsewhere, "real.jsonl"), filepath.Join(dir, "link.jsonl")); err != nil {+ t.Fatal(err)+ }+ if err := os.Symlink(filepath.Join(elsewhere, "nothing"), filepath.Join(dir, "broken.jsonl")); err != nil {+ t.Fatal(err)+ }+ names = append(names, "link.jsonl", "broken.jsonl")+ }+ if err := os.Mkdir(filepath.Join(dir, "folder.jsonl"), 0o755); err != nil {+ t.Fatal(err)+ }+ names = append(names, "folder.jsonl")++ var alone atomic.Int64+ was := statOne+ statOne = func(path string, follow bool) (os.FileInfo, error) {+ alone.Add(1)+ return was(path, follow)+ }+ t.Cleanup(func() { statOne = was })+ for _, follow := range []bool{true, false} {+ var files []file+ for _, n := range names {+ files = append(files, file{agent: "claude", key: "claude:" + n, path: filepath.Join(dir, n)})+ }+ alone.Store(0)+ got := statFiles(files, follow)+ var want []string+ for _, n := range names {+ info := os.Lstat+ if follow {+ info = os.Stat+ }+ fi, err := info(filepath.Join(dir, n))+ if err != nil || !fi.Mode().IsRegular() {+ continue+ }+ want = append(want, n)+ i := slices.IndexFunc(got, func(f file) bool { return filepath.Base(f.path) == n })+ if i < 0 {+ continue+ }+ if got[i].size != fi.Size() || !got[i].mod.Equal(fi.ModTime()) {+ t.Errorf("follow %v, %s: %d %v, want %d %v", follow, n, got[i].size, got[i].mod, fi.Size(), fi.ModTime())+ }+ }+ var gotNames []string+ for _, f := range got {+ gotNames = append(gotNames, filepath.Base(f.path))+ }+ if !slices.Equal(gotNames, want) {+ t.Errorf("follow %v: kept %v, want %v", follow, gotNames, want)+ }+ if runtime.GOOS == "darwin" {+ links := int64(0)+ if follow {+ links = 2+ }+ if n := alone.Load(); n != links {+ t.Errorf("follow %v: %d files asked after alone, want %d (the links followed)", follow, n, links)+ }+ }+ }+}++// Codex's rollouts read from one listing a folder are the ones walkLinked+// and statFiles read, in the same order with the same sizes and times:+// nested days, a folder or a link named as a rollout, a file that isn't+// one, and a sessions folder that is a link to another drive (#2233).+func TestListedRolloutsAsWalked(t *testing.T) {+ home, elsewhere := t.TempDir(), t.TempDir()+ put := func(p string, n int) {+ if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {+ t.Fatal(err)+ }+ if err := os.WriteFile(p, make([]byte, n), 0o644); err != nil {+ t.Fatal(err)+ }+ }+ for i, day := range []string{"2026/09/30", "2026/10/01", "2025/12/31", "2026/10/01/deeper"} {+ for j := range 3 {+ put(filepath.Join(elsewhere, day, fmt.Sprintf("rollout-2026-10-01T10-00-0%d-d%dr%d.jsonl", j, i, j)), i*10+j)+ }+ put(filepath.Join(elsewhere, day, "notes.txt"), 1)+ }+ put(filepath.Join(elsewhere, "2026", "10", "01", "rollout-2026-10-01T10-00-00-z.jsonl.zst"), 7)+ if err := os.Mkdir(filepath.Join(elsewhere, "2026", "rollout-2026-10-01T10-00-00-dir.jsonl"), 0o755); err != nil {+ t.Fatal(err)+ }+ direct := filepath.Join(home, "direct", "sessions")+ if err := os.CopyFS(direct, os.DirFS(elsewhere)); err != nil {+ t.Fatal(err)+ }+ if runtime.GOOS != "windows" {+ for _, top := range []string{elsewhere, direct} {+ if err := os.Symlink(filepath.Join(top, "2026", "09", "30", "rollout-2026-10-01T10-00-00-d0r0.jsonl"), filepath.Join(top, "2026", "rollout-2026-10-01T10-00-00-link.jsonl")); err != nil {+ t.Fatal(err)+ }+ }+ }+ roots := []string{direct}+ if runtime.GOOS != "windows" {+ linked := filepath.Join(home, "linked", "sessions")+ if err := os.MkdirAll(filepath.Dir(linked), 0o755); err != nil {+ t.Fatal(err)+ }+ if err := os.Symlink(elsewhere, linked); err != nil {+ t.Fatal(err)+ }+ roots = append(roots, linked)+ }+ for _, root := range roots {+ var walked []file+ walkLinked(root, func(p string, d os.DirEntry, err error) error {+ if err == nil && !d.IsDir() {+ if m := rolloutName.FindStringSubmatch(d.Name()); m != nil {+ walked = append(walked, file{agent: "codex", key: "codex:" + m[1], path: p, main: true})+ }+ }+ return nil+ })+ walked = statFiles(walked, false)+ listed, ok := listedRollouts(root)+ if !ok {+ if runtime.GOOS == "darwin" {+ t.Fatalf("%s: not listed on macOS", root)+ }+ continue+ }+ if len(walked) < 13 || len(listed) != len(walked) {+ t.Fatalf("%s: listed %d rollouts, walked %d", root, len(listed), len(walked))+ }+ for i := range walked {+ w, l := walked[i], listed[i]+ if w.path != l.path || w.key != l.key || w.size != l.size || !w.mod.Equal(l.mod) || w.main != l.main {+ t.Errorf("%s: rollout %d listed %+v, walked %+v", root, i, l, w)+ }+ }+ }+ if _, ok := listedRollouts(filepath.Join(home, "none")); ok {+ t.Error("a sessions folder that isn't there was listed")+ }+}