From ccc96b4a73ffe153e6983f7c34e8bdf0a2df08d0 Mon Sep 17 00:00:00 2001 From: yetone Date: Tue, 10 Nov 2026 18:00:16 +0800 Subject: [PATCH] sessions: Codex's identities, rollout listings and folder listings go with the idle parses When nobody has read the session parses for three minutes they are let go (v0.1.3540), but the end heap of a real history still held ~10 MB made from them or from the folders, kept for the life of the process: - Codex's thread identities (codexIdentityMemo, ~3.5 MB); - each Codex day folder's rollouts as last listed (rolloutsSeen, ~1.5 MB); - every folder's listing given back to dirent (~4 MB of names and entries). dropIdleCache now lets them go too, and dirent.Forget empties the kept listings. Each is made again on the next read, from the parses and the folders, with no session file parsed. CC's per-file UUIDs (ccUUIDMemo) stay: letting them go would read every Claude Code file again. Run: - mote (macOS): go test ./internal/sessions ./internal/dirent ./internal/gui ./internal/usage ok; GOOS=linux and windows go vet ok. - Broken by hand (the three drops and Forget's body removed): red, "idle: 1 Codex identity lists and 2 folders' rollouts kept" and "9 kept after Forget". On Linux dirent.Stats lists nothing, so the test asks for rollouts kept before the drop only on macOS. - Real accounts (a copy of the owner's history on mote, 300 s each, the web app opened cold, order t53 t54 t54 t53): idle footprint 117 / 120 MB before, 103 / 85 MB after; end heap 53.3 / 50.9 MB before, 43.4 / 44.6 MB after. Peak unchanged (it is the start). Not tried: Windows and Linux at runtime (the change is the same code). --- internal/dirent/stats.go | 10 +++++++++ internal/dirent/stats_test.go | 31 ++++++++++++++++++++++++++++ internal/sessions/cache_idle.go | 10 +++++++++ internal/sessions/cache_idle_test.go | 16 ++++++++++++++ 4 files changed, 67 insertions(+) diff --git a/internal/dirent/stats.go b/internal/dirent/stats.go index ed7b207e8..bee81334b 100644 --- a/internal/dirent/stats.go +++ b/internal/dirent/stats.go @@ -172,3 +172,13 @@ func (l Listing) Done(dir string) { listings.last[dir] = l listings.entries += cap(l) } + +// Forget lets go of every listing kept for the next Stats, and the room +// kept for them: once the session folders go unread, the listings and +// their names were held for the life of the process (~4 MB for a real +// history). A listing given back later is kept again. +func Forget() { + listings.Lock() + listings.last, listings.spare, listings.room, listings.entries = nil, nil, nil, 0 + listings.Unlock() +} diff --git a/internal/dirent/stats_test.go b/internal/dirent/stats_test.go index eea5eac91..e6b6de9c4 100644 --- a/internal/dirent/stats_test.go +++ b/internal/dirent/stats_test.go @@ -223,3 +223,34 @@ func TestListingsOfDifferentSizesMadeOnce(t *testing.T) { t.Errorf("ten polls of unchanged folders allocated %d bytes, want next to none", got) } } + +// Forget lets go of every listing given back, and Stats still answers +// what the folder has after it. +func TestForgetLetsListingsGo(t *testing.T) { + dir := t.TempDir() + for i := range 5 { + if err := os.WriteFile(filepath.Join(dir, fmt.Sprint(i)), nil, 0o644); err != nil { + t.Fatal(err) + } + } + l, ok := Stats(dir) + if !ok { + t.Skip("Stats reads nothing here") + } + l.Done(dir) + kept := func() int { + listings.Lock() + defer listings.Unlock() + return len(listings.last) + len(listings.spare) + len(listings.room) + listings.entries + } + if kept() == 0 { + t.Fatal("nothing kept of a listing given back") + } + Forget() + if n := kept(); n != 0 { + t.Fatalf("%d kept after Forget", n) + } + if l, _ := Stats(dir); len(l) != 5 { + t.Fatalf("Stats after Forget has %d entries, want 5", len(l)) + } +} diff --git a/internal/sessions/cache_idle.go b/internal/sessions/cache_idle.go index 473652fce..2e4c147e4 100644 --- a/internal/sessions/cache_idle.go +++ b/internal/sessions/cache_idle.go @@ -5,6 +5,8 @@ import ( "sync/atomic" "testing" "time" + + "github.com/yetone/magpie/internal/dirent" ) // cacheIdle is how long the parses of every session file stay in memory @@ -80,6 +82,14 @@ func dropIdleCache() bool { cacheGeneration++ // a refresh still holding the old generation drops its parses forgetKeptStats() forgetKeptList() + // what is made again from the parses or the folders on the next read: + // Codex's thread identities (~3.5 MB), and each Codex day folder's + // rollouts and every folder's listing as last read (~5.5 MB) + codexIdentityMemo.Lock() + codexIdentityMemo.kept = nil + codexIdentityMemo.Unlock() + rolloutsSeen.Clear() + dirent.Forget() cacheDrops.Add(1) return true } diff --git a/internal/sessions/cache_idle_test.go b/internal/sessions/cache_idle_test.go index 18f48cd08..7b9664abc 100644 --- a/internal/sessions/cache_idle_test.go +++ b/internal/sessions/cache_idle_test.go @@ -1,6 +1,7 @@ package sessions import ( + "runtime" "sync/atomic" "testing" "time" @@ -34,11 +35,26 @@ func TestIdleParsesLetGoAndComeBackFromDisk(t *testing.T) { t.Fatal("the parses were let go though read just now") } + memos := func() (identities, rollouts int) { + codexIdentityMemo.Lock() + identities = len(codexIdentityMemo.kept) + codexIdentityMemo.Unlock() + rolloutsSeen.Range(func(any, any) bool { rollouts++; return true }) + return + } + // folders are listed so only on macOS (dirent.Stats) + if i, r := memos(); i == 0 || r == 0 && runtime.GOOS == "darwin" { + t.Fatalf("before: %d Codex identity lists and %d folders' rollouts kept; the test reads none", i, r) + } + idle() drops := cacheDrops.Load() if !dropIdleCache() || loaded || len(cache) != 0 || cacheDrops.Load() != drops+1 { t.Fatalf("idle parses kept: loaded %v, %d in memory", loaded, len(cache)) } + if i, r := memos(); i != 0 || r != 0 { + t.Fatalf("idle: %d Codex identity lists and %d folders' rollouts kept", i, r) + } var parses atomic.Int32 parseHook = func() { parses.Add(1) }