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).
75 lines
2.2 KiB
Go
75 lines
2.2 KiB
Go
package sessions
|
|
|
|
import (
|
|
"runtime"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// Once nothing has read the parses for cacheIdle they are let go, and the
|
|
// next read has them back from sessions.json, reading no session file.
|
|
// They stay while a save is not on disk yet, or the last one failed.
|
|
func TestIdleParsesLetGoAndComeBackFromDisk(t *testing.T) {
|
|
setup(t)
|
|
before := List(0)
|
|
if len(before) == 0 {
|
|
t.Fatal("no sessions listed")
|
|
}
|
|
saved()
|
|
idle := func() { cacheKept.last.Store(time.Now().Add(-2 * cacheIdle).UnixNano()) }
|
|
|
|
saving.Lock()
|
|
saving.failed = true
|
|
saving.Unlock()
|
|
idle()
|
|
if dropIdleCache() || !loaded {
|
|
t.Fatal("the parses were let go while the last save had failed")
|
|
}
|
|
saving.Lock()
|
|
saving.failed = false
|
|
saving.Unlock()
|
|
|
|
cacheKept.last.Store(time.Now().UnixNano())
|
|
if dropIdleCache() || !loaded {
|
|
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) }
|
|
t.Cleanup(func() { parseHook = nil })
|
|
after := List(0)
|
|
if n := parses.Load(); n != 0 {
|
|
t.Fatalf("%d session files read again after the let-go; want the parses from disk", n)
|
|
}
|
|
if len(after) != len(before) {
|
|
t.Fatalf("%d sessions after the let-go, %d before", len(after), len(before))
|
|
}
|
|
for i := range before {
|
|
if after[i].ID != before[i].ID || after[i].Tokens != before[i].Tokens || after[i].Title != before[i].Title {
|
|
t.Fatalf("session %d after the let-go = %+v, before %+v", i, after[i], before[i])
|
|
}
|
|
}
|
|
}
|