From cfbd321fc47ea5ba52876f6479b8bc672f7a9649 Mon Sep 17 00:00:00 2001 From: yetone Date: Mon, 19 Oct 2026 11:29:44 +0800 Subject: [PATCH] gui: the start's warm-up reads Codex's per-thread effort when there are Codex sessions (#2776) The first Sessions page after a start that showed Codex rows read usage.CodexModelEffort cold, from the whole ledger, while the page waited. The warm-up now reads it as its last step, after the stats it already warmed, and only when those stats hold a Codex session; every later ask is answered from what CodexModelEffort keeps per period. The steps move into warmSteps() so a test can run them. TestWarmUpReadsCodexEffort: with Codex keys in the stats the step reads the effort once, with none it reads nothing. With the step removed: red, "read 0 times, want 1". Measured on the same sandbox as the commit before (600k gateway calls, ~4,600 sessions), base 1a3b92688 against this branch, back to back, at a load average of 350-480 (nine agents running suites): manage?agent=codex, asked 3.5s after the routes: base 226s / 190s / 232s (cold) new 247s / 222s / 246s (cold) It is not faster here: asked at 3.5s, the page arrives before the warm-up reaches its last step, and both read the ledger at once; the load makes every one of these numbers mostly CPU wait. The step helps a first Codex page opened after the warm-up is done, which is the case in #2776 (a page opened some time after a restart). Not measured at that point under this load. Run on origin/main 2ceff7370 with all four #2776 commits' changes (this one and the three beside it), load average 291-568 (nine agents running suites), macOS: - go test -p 4 ./... -timeout 45m, 09:35-10:49: 62 packages ok, among them agent (2310s), gateway (1707s), provider (764s), usage (83s). Red: - internal/sessions: TestWSLOmpProfiles, TestWSLStoppedDistroKept, TestWSLStoppedSinceNotOpened, TestWSLNoneWithoutHomes, each "TempDir: mkdir ...: no space left on device" (the disk filled mid-run). After space was freed, ./internal/sessions alone: ok (139s). - internal/gui: TestDockJoinLeadsCommandTab, TestPanelCloseHidesIt, TestPanelJoinsFullScreenSpaces, TestPluginPageLoadsItsFilesInTheWindow, TestDockReopenKeepsTheWindowsSpace (each killed at 30s) and TestMainWindowCorners (86s): the real-window AppKit tests under load, already filed as #2645 and #1936. Rerun alone at load ~335: all pass, with TestSetAnswersWhileDevinAndProvidersHang (17s). - gofmt -l: none. go vet: darwin, GOOS=linux -tags nogui and GOOS=windows all exit 0. - No internal/gui/assets file changed, so the GUI Playwright suite was not run. --- internal/gui/sessions.go | 28 +++++++++++++++++------ internal/gui/sessions_warm_test.go | 36 ++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 7 deletions(-) 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) + } + } +}