Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

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

yetoneeee571999139
diff --git a/internal/sessions/cost_order_test.go b/internal/sessions/cost_order_test.go
new file mode 100644
index 000000000..16c16690b
--- /dev/null
+++ b/internal/sessions/cost_order_test.go
@@ -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))
+ }
+}
diff --git a/internal/sessions/sessions.go b/internal/sessions/sessions.go
index 7f41c5a8e..2c1237e91 100644
--- a/internal/sessions/sessions.go
+++ b/internal/sessions/sessions.go
@@ -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
diff --git a/internal/sessions/stats.go b/internal/sessions/stats.go
index e417d85d3..f603b7dee 100644
--- a/internal/sessions/stats.go
+++ b/internal/sessions/stats.go
@@ -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 {