sessions: a session's cost is summed in its models' order, so it reads the same each time

assembleListed summed a session's cost over a map of its models, and
statsRead summed a summary's the same way, so the float came out a last
digit apart from read to read (15225.3933882, then 15225.393388199998,
on a real Claude Code session with nothing new written). Each read of
the Sessions list then had a changed row, and the stats a changed
summary, though nothing had changed. Both sum in name order now.

Run at this head (macOS, go1.27):
- TestSessionCostSummedInOneOrder (assembleListed, 7 models, 200 reads)
  and TestSummaryCostSummedInOneOrder (statsRead, 6 models priced, 50
  reads): red on the old code (2 different numbers each), green after,
  -count=3.
- go test -tags nogui -timeout 45m ./internal/gui/ ./internal/sessions/
  ./internal/usage/ (provider's #3632 skip): ok.
- gofmt -l clean; GOOS=linux and GOOS=windows go vet -tags nogui
  ./internal/gui/ ./internal/sessions/: ok.
- Real history (~15.9k sessions, a live session being written): six
  /api/sessions reads 15 s apart, each the same as a whole read taken
  just after it; before, the 506 KB row of one session flipped its cost
  between reads.

Not tried: Windows and Linux runs of the package tests.
This commit is contained in:
yetone
2026-10-27 05:39:12 +08:00
parent d03779cc10
commit eee5719991
3 changed files with 78 additions and 2 deletions
+70
View File
@@ -0,0 +1,70 @@
package sessions
import (
"math"
"os"
"path/filepath"
"testing"
"time"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/settings"
)
// A session's cost is its models' costs summed in one order, so it reads
// the same each time: summed in a map's order, a session of several models
// came out 15225.3933882 one read and 15225.393388199998 the next, and the
// Sessions tab was sent its row again on every poll for it.
func TestSessionCostSummedInOneOrder(t *testing.T) {
f := file{agent: "claude", key: "claude:s", path: "/s.jsonl", main: true, mod: time.Unix(1790000000, 0)}
var s state
for i, name := range []string{"a", "b", "c", "d", "e", "f", "g"} {
*s.Models.at(name) = Tokens{Input: 100000 + 33333*i}
}
cache = map[string]*state{f.path: &s}
t.Cleanup(func() { cache = nil })
price := func(string) *catalog.Price { return &catalog.Price{Input: 0.1} }
costs := map[uint64]bool{}
for range 200 {
got, ok := assembleListed([]file{f}, price, func(string) string { return "" })
if !ok {
t.Fatal("the session isn't listed")
}
costs[math.Float64bits(got.Cost)] = true
}
if len(costs) != 1 {
t.Errorf("one session's cost read as %d different numbers", len(costs))
}
}
// So is a session's summary in the stats, which the Sessions tab reads
// again every poll too.
func TestSummaryCostSummedInOneOrder(t *testing.T) {
inZone(t, 0)
claude, _ := setup(t)
was := PriceOf
PriceOf = func(settings.Settings, string) (catalog.Price, bool) { return catalog.Price{Input: 0.1}, true }
t.Cleanup(func() { PriceOf = was })
dir := filepath.Join(claude, "projects", "-work-alias")
if err := os.MkdirAll(dir, 0o755); err != nil {
t.Fatal(err)
}
var body string
for i, model := range []string{"claude-opus-5-5", "claude-sonnet-5-5", "claude-haiku-5-5", "claude-fable-5-1", "claude-opus-4-1", "claude-sonnet-4-5"} {
body += aliasMsg("m"+itoa(i), model, "2026-09-25T10:0"+itoa(i)+":00Z", 100003+33331*i)
}
if err := os.WriteFile(filepath.Join(dir, "44444444-2222-3333-4444-555555555555.jsonl"), []byte(body), 0o600); err != nil {
t.Fatal(err)
}
costs := map[uint64]bool{}
for range 50 {
for _, sum := range statsAt(0, statsNow).Sessions {
if sum.Cwd == "/work/alias" {
costs[math.Float64bits(sum.Cost)] = true
}
}
}
if len(costs) != 1 {
t.Errorf("one session's summary cost read as %d different numbers", len(costs))
}
}
+4 -1
View File
@@ -1336,7 +1336,10 @@ func assembleListed(fs []file, price func(string) *catalog.Price, codexTitle fun
custom = cmp.Or(DesktopTitles([]string{s.ID})[s.ID], custom)
}
s.Title = cmp.Or(custom, named, s.Title, first)
for _, m := range models {
// in the models' order: summed in the map's, the cost came out a last
// digit apart from read to read, and the session's row read as changed
for _, name := range slices.Sorted(maps.Keys(models)) {
m := models[name]
if p := price(m.Model); p != nil {
// a sum of the session's calls of the model, at its base price:
// which call went over a long-context tier isn't known here
+4 -1
View File
@@ -2,6 +2,7 @@ package sessions
import (
"cmp"
"maps"
"math"
"slices"
"sort"
@@ -405,7 +406,9 @@ func statsRead(days int, now time.Time, native map[string]bool) Stats {
}
}
slices.SortFunc(sum.perDay, func(a, b summaryDay) int { return cmp.Compare(a.day, b.day) })
for model, t := range sh.models {
// in the models' order, as assembleListed sums a session's cost
for _, model := range slices.Sorted(maps.Keys(sh.models)) {
t := sh.models[model]
sum.Tokens.add(t)
sum.Models = append(sum.Models, model)
if p := price(model); p != nil {