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)