From c93d9e0c0bf47c05c450f1ebdf9a47bd0b17eaf2 Mon Sep 17 00:00:00 2001 From: yetone Date: Thu, 29 Oct 2026 06:30:58 +0800 Subject: [PATCH] usage: an unchanged Codex effort ask is told the kept answer, not a copy (#2278) CodexModelEffort copied every thread's consumption for each ask (cloneEffort), though the kept answer is only replaced by a later build, never written, and its callers (the Sessions list, overview and a session's details) only index it and copy their thread's slice (addCodexModelEffort, addCodexSummaryModelEffort). On the #2278 bench (15G .codex) the copies were ~230 MB allocated every 80 s of an open Sessions page. Asks are now told the kept map itself; the doc comment says it is read-only. TestCodexModelEffortReadsLedgerOnlyWhenCallsChange wrote into the answer to check a caller's copy was its own. That guarantee is now the GUI's: TestCodexModelEffortCopiedPerSession checks a session and a summary get their own slice. TestCodexModelEffortAsksShareTheKeptAnswer checks two unchanged asks get the same map. Run at this tree on mote (macOS 27, Go 1.26.8): - go test -count=1 ./internal/usage ./internal/sessions ./internal/gui: usage ok, sessions ok; gui red only in TestMainWindowCorners/ macOS_26_SDK, which is #3949 (filed, red on macOS 27 on main). - GOOS=linux,windows,darwin go vet -tags nogui ./internal/usage ./internal/gui: ok. Broken by hand: maps.Clone back in the kept-answer return makes TestCodexModelEffortAsksShareTheKeptAnswer red ("told a copy"); dropping the GUI's slices.Clone makes TestCodexModelEffortCopiedPerSession red ("written through a session's copy: 999"). Not run: the full ./... suite, and the bench A/B (the 230 MB is from the v0.1.1967 heap profile, not yet re-measured). --- internal/gui/sessions_codex_test.go | 23 ++++++++++++++++++++++ internal/usage/codex_effort_shared_test.go | 17 ++++++++++++++++ internal/usage/codex_identity.go | 22 +++++++++------------ internal/usage/codex_identity_test.go | 3 +-- 4 files changed, 50 insertions(+), 15 deletions(-) create mode 100644 internal/gui/sessions_codex_test.go diff --git a/internal/gui/sessions_codex_test.go b/internal/gui/sessions_codex_test.go new file mode 100644 index 000000000..bd026ae81 --- /dev/null +++ b/internal/gui/sessions_codex_test.go @@ -0,0 +1,23 @@ +package gui + +import ( + "testing" + + "github.com/yetone/magpie/internal/sessions" +) + +// usage.CodexModelEffort's answer is shared by every ask: a session or a +// summary is given its own copy of its thread's consumption, so what the +// page does with it never reaches the kept answer. +func TestCodexModelEffortCopiedPerSession(t *testing.T) { + byThread := map[string][]sessions.ModelEffort{"t": {{Model: "m", Effort: "low", Tokens: sessions.Tokens{Input: 20}}}} + s := sessions.Session{Agent: "codex", ID: "t"} + addCodexModelEffort(&s, byThread) + s.ModelEffort[0].Input = 999 + sum := sessions.Summary{Agent: "codex", ID: "t"} + addCodexSummaryModelEffort(&sum, byThread) + sum.ModelEffort[0].Input = 999 + if got := byThread["t"][0].Input; got != 20 { + t.Fatalf("the shared answer was written through a session's copy: %d", got) + } +} diff --git a/internal/usage/codex_effort_shared_test.go b/internal/usage/codex_effort_shared_test.go index 4573a864c..74d6ff3d0 100644 --- a/internal/usage/codex_effort_shared_test.go +++ b/internal/usage/codex_effort_shared_test.go @@ -239,3 +239,20 @@ func TestCodexModelEffortIsTheWholeLedgers(t *testing.T) { } } } + +// Asks within the minute are told the kept answer itself, not a copy of +// every thread's consumption each: over a large history the copies were +// ~230 MB every 80 s of an open Sessions page (#2278). +func TestCodexModelEffortAsksShareTheKeptAnswer(t *testing.T) { + effortFixture(t) + effortFresh = time.Hour + t.Cleanup(func() { effortFresh = 0 }) + first := CodexModelEffort(All) + again := CodexModelEffort(All) + if len(first) == 0 { + t.Fatal("no consumption") + } + if reflect.ValueOf(first).UnsafePointer() != reflect.ValueOf(again).UnsafePointer() { + t.Fatal("an unchanged ask was told a copy of the kept answer") + } +} diff --git a/internal/usage/codex_identity.go b/internal/usage/codex_identity.go index 8507aa983..b9d7ea09a 100644 --- a/internal/usage/codex_identity.go +++ b/internal/usage/codex_identity.go @@ -584,6 +584,10 @@ var effortBehind = true var effortBuilt func() // CodexModelEffort reports each Codex thread's own priced ledger consumption. +// The answer is the kept one, shared by every ask until the next build +// replaces it: callers read it and copy a thread's slice before keeping it, +// never write it. A copy of every thread's for each ask was ~230 MB every +// 80 s of a Sessions page open over a large history (#2278). func CodexModelEffort(p Period) map[string][]sessions.ModelEffort { if LogCalls != nil { // synthetic logs carry no state to key on return codexModelEffort(codexEffortRows(p, Clock(), LogCalls)) @@ -594,7 +598,7 @@ func CodexModelEffort(p Period) map[string][]sessions.ModelEffort { e.used = codexEffort.built value := e.value codexEffort.Unlock() - return cloneEffort(value) // read before the state is: that is most of an unchanged ask's cost + return value // read before the state is: that is most of an unchanged ask's cost } codexEffort.Unlock() now := Clock() @@ -611,7 +615,7 @@ func CodexModelEffort(p Period) map[string][]sessions.ModelEffort { if e.have && (e.key == st.key || e.building != nil && e.root == st.root) { value := e.value // current, or the last one while the next is built codexEffort.Unlock() - return cloneEffort(value) + return value } if wait := e.building; wait != nil { // the first answer is on its way codexEffort.Unlock() @@ -619,7 +623,7 @@ func CodexModelEffort(p Period) map[string][]sessions.ModelEffort { codexEffort.Lock() value := e.value codexEffort.Unlock() - return cloneEffort(value) + return value } done := make(chan struct{}) e.building = done @@ -627,10 +631,10 @@ func CodexModelEffort(p Period) map[string][]sessions.ModelEffort { value := e.value codexEffort.Unlock() go buildEffort(e, st, p, now, done) - return cloneEffort(value) + return value } codexEffort.Unlock() - return cloneEffort(buildEffort(e, st, p, now, done)) + return buildEffort(e, st, p, now, done) } // buildEffort builds p's answer under st into e, and closes done when it @@ -737,14 +741,6 @@ func forgetEffortPeriods(p Period) { } } -func cloneEffort(values map[string][]sessions.ModelEffort) map[string][]sessions.ModelEffort { - out := make(map[string][]sessions.ModelEffort, len(values)) - for actor, v := range values { - out[actor] = slices.Clone(v) - } - return out -} - func codexModelEffort(rows []Row) map[string][]sessions.ModelEffort { sums := effortSums{} for i := range rows { diff --git a/internal/usage/codex_identity_test.go b/internal/usage/codex_identity_test.go index 510672d67..56fcaf61a 100644 --- a/internal/usage/codex_identity_test.go +++ b/internal/usage/codex_identity_test.go @@ -470,13 +470,12 @@ func TestCodexModelEffortReadsLedgerOnlyWhenCallsChange(t *testing.T) { if got, ok := findModelEffort(first["main-a"], "m", "low"); !ok || got.Input != 20 { t.Fatalf("first read = %+v", first) } - first["main-a"][0].Input = 999 // a caller's copy is its own again := CodexModelEffort(All) if built != 1 { t.Fatalf("ledger read %d times for an unchanged history, want 1", built) } if got, _ := findModelEffort(again["main-a"], "m", "low"); got.Input != 20 { - t.Fatalf("cached answer was changed through a caller's copy: %+v", again) + t.Fatalf("cached answer = %+v", again) } writeCodexCall(t, path, "main-a", "main-a", `"vscode"`, "resp-1", "m", "low", now.Add(-time.Hour), 2000, 30)