gui: the Usage page's session-stats memo keeps the 8 most recently asked ?days ranges, not one per number a request sends

statsFor keeps a whole sessions.Stats per distinct ?days= value, and
only forgetStats ever emptied the map. Each new number a caller sent
added a snapshot that stayed until a session was deleted, restored or
purged. Now the memo keeps the 8 most recently used ranges.

Run before this commit (Linux, WSL2 Ubuntu 24.04, official go1.26.8):
the new test fails with the fix reverted and passes with it;
go test -tags nogui ./internal/gui ./internal/plugin is ok. Both
builds were compared with magpie web on a sandbox copy of a real
~/.codex/sessions: all 10 stats/overview answers byte-identical, and
300 distinct ?days values left HEAD's memory up 21-25 MB against 6 MB
for this change.

macOS was not run here; CI's macOS job runs the new test.

Reviewed for magpie (CaramelizedCUDA/magpie pr/stats-memo-cap at 05f0afa4, cherry-picked onto f340aecab, then rebased onto 6ccf23efa and run again there): on macOS, go1.26.8, sandbox HOME, TestStatsMemoKeepsAFewDays fails with the cap turned off ("the memo holds 64, want at most 8") and passes with it. The next commits add a test for which range goes and stamp by ask count.

Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
CaramelizedCUDA
2026-10-12 23:33:56 +08:00
committed by yetone
co-authored by Claude Code
parent 3c4f8f9aed
commit bbe8825c04
2 changed files with 41 additions and 1 deletions
+18 -1
View File
@@ -352,12 +352,19 @@ type statsKept struct {
at time.Time
st sessions.Stats
busy bool
used time.Time
}
// statsMemoMost is how many distinct ?days values the memo keeps: each one is
// a whole sessions.Stats, and ?days takes any number a caller sends, so the
// least recently asked one is let go when a new value comes past it.
const statsMemoMost = 8
func statsFor(days int) sessions.Stats {
days = max(0, days)
statsMemo.Lock()
if k := statsMemo.m[days]; k != nil {
k.used = time.Now()
if !k.busy && time.Since(k.at) > 10*time.Second {
k.busy = true
go func() {
@@ -378,7 +385,17 @@ func statsFor(days int) sessions.Stats {
statsMemo.m = map[int]*statsKept{}
}
if statsMemo.m[days] == nil {
statsMemo.m[days] = &statsKept{at: time.Now(), st: st}
if len(statsMemo.m) >= statsMemoMost {
oldest := -1
for d, k := range statsMemo.m {
if oldest == -1 || k.used.Before(statsMemo.m[oldest].used) {
oldest = d
}
}
delete(statsMemo.m, oldest)
}
now := time.Now()
statsMemo.m[days] = &statsKept{at: now, st: st, used: now}
}
statsMemo.Unlock()
return st
+23
View File
@@ -0,0 +1,23 @@
package gui
import "testing"
// ?days is any number a caller sends, and each distinct value kept a whole
// sessions.Stats in statsMemo for good: 64 values, 64 snapshots. The memo
// keeps a few, the least recently asked let go, and the newest one stays.
func TestStatsMemoKeepsAFewDays(t *testing.T) {
forgetStats()
t.Cleanup(forgetStats)
for d := 1; d <= 64; d++ {
statsFor(d)
}
statsMemo.Lock()
n, newest := len(statsMemo.m), statsMemo.m[64] != nil
statsMemo.Unlock()
if n > 8 {
t.Fatalf("after 64 distinct ?days values the memo holds %d, want at most 8", n)
}
if !newest {
t.Fatal("the value asked last was let go")
}
}