Files
magpie/internal/sessions/cache_idle_test.go
T
yetone 75f872ef19 sessions: a page gone from view lets the parsed session files go in 30 s, not 3 minutes
v0.1.4150's away timer let the Sessions stats and the Requests chunks go
30 s after the app's page went from view, but a web page open 70 s and
closed still held 92-93 MB 45 and 90 s on. A heap taken 45 s after the
close (liveHeap 57 MB, 15 MB at +180 s) showed why: opening the page
starts the warm-up (warmStart), which parses every session file, and
those parses (decodeCache, the listings, Codex identities: ~40 MB live
for the acctp history) go only by the sessions package's own cacheIdle,
3 minutes after their last read.

forgetAwayPage now also calls sessions.ForgetParses, which is
dropIdleCache without the 3-minute wait (dropCache(0)): it still keeps
the parses while a save isn't on disk, the last failed, or a file is
being parsed, and the next read has them back from sessions.json with
no session file read. It runs only when the stats went too, so a range
being read keeps them.

Test: TestForgetParsesLetsGoAtOnce (red with ForgetParses waiting
cacheIdle, broken by hand).
Measured on mote (pageclose2.sh, acctp, page open 70 s and closed,
footprint-overlay build like the release):
  v0.1.4150 (w90): close 122 MB, +45 s 93, +90 s 90, +180 s 55, +600 s 52
  this (w92):      close 125 MB, +45 s 64, +90 s 61, +180 s 56, +600 s 53
A debug build (w91) logged the away timer firing with no read since,
so the 90 MB left was this cache, not a skipped let-go.

Run: go test ./internal/sessions ./internal/gui ./internal/usage on mote
(ok); GOOS=linux go vet -tags nogui and GOOS=windows go vet of the two
packages (ok).
2026-11-14 17:45:41 +08:00

115 lines
3.5 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
}
ccDirs := func() (n int) { ccListed.Range(func(any, any) bool { n++; return true }); return }
if ccDirs() == 0 {
t.Fatal("before: no Claude Code folder's listing kept; the test lists none")
}
// 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 || ccDirs() != 0 {
t.Fatalf("idle: %d Codex identity lists, %d folders' rollouts and %d Claude Code listings kept", i, r, ccDirs())
}
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])
}
}
}
// A page gone from view lets the parses go at once (ForgetParses), read
// just now or not, and the next read has them back from sessions.json; a
// save not on disk yet still keeps them.
func TestForgetParsesLetsGoAtOnce(t *testing.T) {
setup(t)
before := List(0)
if len(before) == 0 {
t.Fatal("no sessions listed")
}
saved()
cacheKept.last.Store(time.Now().UnixNano())
saving.Lock()
saving.failed = true
saving.Unlock()
if ForgetParses() || !loaded {
t.Fatal("the parses were let go while the last save had failed")
}
saving.Lock()
saving.failed = false
saving.Unlock()
if !ForgetParses() || loaded || len(cache) != 0 {
t.Fatalf("parses read just now kept: loaded %v, %d in memory", loaded, len(cache))
}
if ForgetParses() {
t.Fatal("ForgetParses said it let go with nothing loaded")
}
var parses atomic.Int32
parseHook = func() { parses.Add(1) }
t.Cleanup(func() { parseHook = nil })
if after := List(0); len(after) != len(before) || parses.Load() != 0 {
t.Fatalf("%d sessions after (%d before), %d files read again", len(after), len(before), parses.Load())
}
}