diff --git a/internal/sessions/discovery.go b/internal/sessions/discovery.go index ba504f9e3..cc85ca378 100644 --- a/internal/sessions/discovery.go +++ b/internal/sessions/discovery.go @@ -5,6 +5,7 @@ import ( "io/fs" "os" "path/filepath" + "runtime" "strings" "sync" "time" @@ -63,6 +64,7 @@ func readDirectory(path string) []os.DirEntry { } return nil } + entries = compactEntries(path, entries) directoryCache.Lock() if len(directoryCache.entries) >= 16384 { directoryCache.entries = map[string]directoryEntry{} @@ -72,6 +74,36 @@ func readDirectory(path string) []os.DirEntry { return entries } +// keptEntry is a kept listing's entry. A listing is kept as long as its +// folder doesn't change, ~16k of them across a long Claude Code history, so +// they sit in one array its entries point into: os.ReadDir gives each its +// own allocation, and the slice it returns room to grow (#2278). +type keptEntry struct { + dir, name string + typ fs.FileMode +} + +func (e *keptEntry) Name() string { return e.name } +func (e *keptEntry) IsDir() bool { return e.typ.IsDir() } +func (e *keptEntry) Type() fs.FileMode { return e.typ } +func (e *keptEntry) Info() (fs.FileInfo, error) { return os.Lstat(filepath.Join(e.dir, e.name)) } +func (e *keptEntry) String() string { return fs.FormatDirEntry(e) } + +// compactEntries is entries, read from dir, held as keptEntry. Not on +// Windows, whose entries carry what Info answers and keptEntry would stat for. +func compactEntries(dir string, entries []os.DirEntry) []os.DirEntry { + if runtime.GOOS == "windows" { + return entries + } + all := make([]keptEntry, len(entries)) + out := make([]os.DirEntry, len(entries)) + for i, e := range entries { + all[i] = keptEntry{dir, e.Name(), e.Type()} + out[i] = &all[i] + } + return out +} + // unreadDirectory is what a folder lists as when reading it failed with // err: nothing when it is gone, else the last good listing, if there is // one. diff --git a/internal/sessions/discovery_compact_test.go b/internal/sessions/discovery_compact_test.go new file mode 100644 index 000000000..44c017f7a --- /dev/null +++ b/internal/sessions/discovery_compact_test.go @@ -0,0 +1,70 @@ +package sessions + +import ( + "os" + "path/filepath" + "runtime" + "testing" + "time" + "unsafe" +) + +// A kept listing's entries sit in one array and answer as os.ReadDir's do. +func TestKeptListingIsCompact(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("Windows keeps os.ReadDir's entries, which carry their info") + } + dir := t.TempDir() + for _, n := range []string{"a.jsonl", "b.jsonl", "c.json"} { + if err := os.WriteFile(filepath.Join(dir, n), []byte(n), 0o644); err != nil { + t.Fatal(err) + } + } + if err := os.Mkdir(filepath.Join(dir, "sub"), 0o755); err != nil { + t.Fatal(err) + } + if err := os.Symlink(filepath.Join(dir, "sub"), filepath.Join(dir, "link")); err != nil { + t.Fatal(err) + } + old := time.Now().Add(-time.Minute) + if err := os.Chtimes(dir, old, old); err != nil { + t.Fatal(err) + } + got, want := readDirectory(dir), mustReadDir(t, dir) + if len(got) != len(want) { + t.Fatalf("%d entries, want %d", len(got), len(want)) + } + for i, e := range got { + w := want[i] + if e.Name() != w.Name() || e.IsDir() != w.IsDir() || e.Type() != w.Type() { + t.Errorf("entry %d = %s %v %v, want %s %v %v", i, e.Name(), e.IsDir(), e.Type(), w.Name(), w.IsDir(), w.Type()) + } + gi, err1 := e.Info() + wi, err2 := w.Info() + if err1 != nil || err2 != nil || gi.Size() != wi.Size() || gi.Mode() != wi.Mode() || !gi.ModTime().Equal(wi.ModTime()) { + t.Errorf("entry %s info = %v %v, want %v %v", e.Name(), gi, err1, wi, err2) + } + } + first, ok := got[0].(*keptEntry) + if !ok { + t.Fatalf("kept entry is %T, not listed", got[0]) + } + for i, e := range got { + p, _ := e.(*keptEntry) + if p == nil || uintptr(unsafe.Pointer(p))-uintptr(unsafe.Pointer(first)) != uintptr(i)*unsafe.Sizeof(keptEntry{}) { + t.Fatalf("entry %d has an allocation of its own", i) + } + } + if again := readDirectory(dir); &again[0] != &got[0] { + t.Error("an unchanged folder was listed again") + } +} + +func mustReadDir(t *testing.T, dir string) []os.DirEntry { + t.Helper() + es, err := os.ReadDir(dir) + if err != nil { + t.Fatal(err) + } + return es +}