diff --git a/internal/gui/sessions.go b/internal/gui/sessions.go index 17ce06eca..27d306bf6 100644 --- a/internal/gui/sessions.go +++ b/internal/gui/sessions.go @@ -481,16 +481,30 @@ func warmSessions() { time.Sleep(3 * time.Second) // with few readers, as nobody waits on it yet: after an update // that reads every file again, it held every core (#1665) - sessions.Warm(func() { - warmUp( - func() { statsFor(0) }, - func() { statsFor(30) }, - func() { usage.QueryPage(usage.All, usage.Filter{}, 0, 50) }, - ) - }) + sessions.Warm(func() { warmUp(warmSteps()...) }) }() } +// warmSteps are the reads the warm-up makes before any page asks for them. +// The last is Codex's per-thread effort, when there are Codex sessions: a +// first Codex page after a start read it from the whole ledger while it +// waited (#2776), and every later ask is answered from what it kept. +func warmSteps() []func() { + return []func(){ + func() { statsFor(0) }, + func() { statsFor(30) }, + func() { usage.QueryPage(usage.All, usage.Filter{}, 0, 50) }, + func() { + if slices.ContainsFunc(statsFor(0).Keys, func(k string) bool { return strings.HasPrefix(k, "codex:") }) { + warmCodexEffort() + } + }, + } +} + +// warmCodexEffort reads what the Sessions page's Codex rows ask for. +var warmCodexEffort = func() { usage.CodexModelEffort(usage.All) } + // warmUp runs the warm-up's reads, then hands what they threw away back to // the system at once. Reading a long history allocates many times what it // keeps (a 13k-session Codex and Claude Code history: ~830 MB allocated to diff --git a/internal/gui/sessions_warm_test.go b/internal/gui/sessions_warm_test.go index 97b68f1a6..ebf3464ba 100644 --- a/internal/gui/sessions_warm_test.go +++ b/internal/gui/sessions_warm_test.go @@ -3,6 +3,9 @@ package gui import ( "runtime" "testing" + "time" + + "github.com/yetone/magpie/internal/sessions" ) // The warm-up's garbage goes back to the system when it ends, not over the @@ -26,3 +29,36 @@ func TestWarmUpReturnsItsGarbage(t *testing.T) { } runtime.KeepAlive(keep) } + +// With Codex sessions on this computer the warm-up also reads Codex's +// per-thread effort, which a first Codex page after a start otherwise read +// from the whole ledger while it waited (#2776); without any it doesn't. +func TestWarmUpReadsCodexEffort(t *testing.T) { + statsMemo.Lock() + saved := statsMemo.m + statsMemo.Unlock() + old := warmCodexEffort + t.Cleanup(func() { + statsMemo.Lock() + statsMemo.m = saved + statsMemo.Unlock() + warmCodexEffort = old + }) + for _, c := range []struct { + keys []string + want int + }{{[]string{"claude:a", "codex:b"}, 1}, {[]string{"claude:a"}, 0}} { + statsMemo.Lock() + statsMemo.m = map[int]*statsKept{} + for _, d := range []int{0, 30} { + statsMemo.m[d] = &statsKept{at: time.Now(), st: sessions.Stats{Keys: c.keys}} + } + statsMemo.Unlock() + read := 0 + warmCodexEffort = func() { read++ } + warmUp(warmSteps()...) + if read != c.want { + t.Fatalf("with sessions %v Codex's effort was read %d times, want %d", c.keys, read, c.want) + } + } +}