Files
magpie/internal/gui/sessions_codex_test.go
T
yetone c93d9e0c0b 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).
2026-10-29 06:30:58 +08:00

24 lines
829 B
Go

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)
}
}