diff --git a/internal/sessions/external.go b/internal/sessions/external.go index 80e5c5001..f632f516b 100644 --- a/internal/sessions/external.go +++ b/internal/sessions/external.go @@ -96,8 +96,11 @@ type ExternalDayUsage struct { Priced bool } +// st itself is not written: its Days and Sessions may be kept for the next +// read (keptStats), so what is changed is copied first. func MergeExternalStats(st Stats, days []ExternalDayUsage, external []ExternalSession) Stats { oldFirst, _ := time.ParseInLocation(time.DateOnly, st.From, time.Local) + sessionsOwn := false for _, d := range days { if d.Date <= st.To && (st.From == "" || d.Date < st.From) { st.From = d.Date @@ -106,7 +109,7 @@ func MergeExternalStats(st Stats, days []ExternalDayUsage, external []ExternalSe first, _ := time.ParseInLocation(time.DateOnly, st.From, time.Local) if !oldFirst.IsZero() && !first.Equal(oldFirst) { shift := int(math.Round(oldFirst.Sub(first).Hours() / 24)) - st.Sessions = slices.Clone(st.Sessions) + st.Sessions, sessionsOwn = slices.Clone(st.Sessions), true for i := range st.Sessions { s := &st.Sessions[i] s.Days = slices.Clone(s.Days) @@ -119,7 +122,8 @@ func MergeExternalStats(st Stats, days []ExternalDayUsage, external []ExternalSe } } } - byDate := map[string]int{} + st.Days = slices.Clone(st.Days) + byDate, own := map[string]int{}, map[int]bool{} for i := range st.Days { byDate[st.Days[i].Date] = i } @@ -131,9 +135,13 @@ func MergeExternalStats(st Stats, days []ExternalDayUsage, external []ExternalSe if !ok { st.Days = append(st.Days, Day{Date: u.Date, Usage: []Usage{}, Active: []Active{}}) i = len(st.Days) - 1 - byDate[u.Date] = i + byDate[u.Date], own[i] = i, true } d := &st.Days[i] + if !own[i] { + // a day of st's: its rows are copied before one is added to + d.Usage, own[i] = slices.Clone(d.Usage), true + } found := false for i := range d.Usage { if d.Usage[i].Agent == u.Agent && d.Usage[i].Cwd == u.Cwd && d.Usage[i].Model == u.Model { @@ -180,9 +188,15 @@ func MergeExternalStats(st Stats, days []ExternalDayUsage, external []ExternalSe s.Models = append(s.Models, m.Model) s.Priced = s.Priced && m.Priced } + if !sessionsOwn { + st.Sessions, sessionsOwn = slices.Clip(st.Sessions), true + } st.Sessions = append(st.Sessions, s) } sort.Slice(st.Days, func(i, j int) bool { return st.Days[i].Date < st.Days[j].Date }) - sort.SliceStable(st.Sessions, func(i, j int) bool { return st.Sessions[i].Last.After(st.Sessions[j].Last) }) + if sessionsOwn { + // st's own come sorted so + sort.SliceStable(st.Sessions, func(i, j int) bool { return st.Sessions[i].Last.After(st.Sessions[j].Last) }) + } return st } diff --git a/internal/sessions/sessions.go b/internal/sessions/sessions.go index 77550f007..a8fc1c736 100644 --- a/internal/sessions/sessions.go +++ b/internal/sessions/sessions.go @@ -1388,6 +1388,7 @@ func Reset() { saved() cache, loaded = nil, false cacheGeneration++ + forgetKeptStats() } // List reads the latest sessions of every agent, the most recently active diff --git a/internal/sessions/stats.go b/internal/sessions/stats.go index cfd35f269..63adfa36d 100644 --- a/internal/sessions/stats.go +++ b/internal/sessions/stats.go @@ -2,6 +2,7 @@ package sessions import ( "cmp" + "maps" "math" "slices" "sort" @@ -158,9 +159,8 @@ func StatsAndNative(days int) (Stats, map[string]bool) { // are the same whatever the range, and the page keeps 7, 30 and every // day, each read again behind it (statsFor in internal/gui, #2278). func StatsAndNativeAll(days []int) ([]Stats, map[string]bool) { - native := map[string]bool{} alias := provider.UsageAlias() - out := statsReadAll(days, time.Now(), native) + out, native := statsKept(days, time.Now(), true) if alias != nil { for i := range out { out[i] = aliasStats(out[i], alias) @@ -189,6 +189,17 @@ func statsRead(days int, now time.Time, native map[string]bool) Stats { // statsReadAll is statsRead for each of days, sharing what doesn't // depend on the range. func statsReadAll(ranges []int, now time.Time, native map[string]bool) []Stats { + out, listed := statsKept(ranges, now, native != nil) + if native != nil { + maps.Copy(native, listed) + } + return out +} + +// statsKept is statsReadAll, and the agent|id of every session listed when +// native is asked for. The ranges' Stats and the map are kept for the +// next read (keptStats), so neither is written. +func statsKept(ranges []int, now time.Time, wantNative bool) ([]Stats, map[string]bool) { now = now.In(time.Local) today := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local) @@ -202,15 +213,30 @@ func statsReadAll(ranges []int, now time.Time, native map[string]bool) []Stats { // every changed file, whatever the range, so a wider range picked later // finds them read rather than starting another run refresh(files, files) + price, titles := pricer(), codexTitleOf() + keptFor(files, today, price, titles) + out := make([]Stats, len(ranges)) + var todo []int + for i, days := range ranges { + st, ok := keptStats.ranges[days] + if !ok { + todo = append(todo, i) + } + out[i] = st + } + if len(todo) == 0 && (!wantNative || keptStats.native != nil) { + return out, keptStats.native + } + price, titles = keptPrice(price), keptTitle(titles) identities := codexIdentitySnapshot(files) taskTitles := codexSubagentTaskTitles(files) - price, titles := pricer(), codexTitleOf() // each session as listed, by key: what native is made of, and what a // range's sessions are summed up from var listed map[string]Session - if native != nil { + if wantNative { groups := byKey(files, nil) listed = make(map[string]Session, len(groups)) + native := make(map[string]bool, len(groups)) for key, fs := range groups { // as List: a session nothing was said in is not listed if s, ok := assembleListed(fs, price, titles); ok { @@ -218,6 +244,7 @@ func statsReadAll(ranges []int, now time.Time, native map[string]bool) []Stats { listed[key] = s } } + keptStats.native = native } // a copy of a conversation is counted in the session it is kept in // (ccCopies): its subagents' files, and what its own file holds that @@ -231,11 +258,13 @@ func statsReadAll(ranges []int, now time.Time, native map[string]bool) []Stats { folder[f.key] = st.Cwd } } - out := make([]Stats, len(ranges)) - for i, days := range ranges { - out[i] = statsOfRange(days, today, files, copies, copied, folder, listed, identities, taskTitles, price, titles) + for _, i := range todo { + st := statsOfRange(ranges[i], today, files, copies, copied, folder, listed, identities, taskTitles, price, titles) + keepDesktop(st) + keepRange(ranges[i], st, ranges) + out[i] = st } - return out + return out, keptStats.native } // statsOfRange is one range's Stats from what statsReadAll read, with the diff --git a/internal/sessions/stats_kept.go b/internal/sessions/stats_kept.go new file mode 100644 index 000000000..a00295f22 --- /dev/null +++ b/internal/sessions/stats_kept.go @@ -0,0 +1,155 @@ +package sessions + +import ( + "reflect" + "slices" + "time" + + "github.com/yetone/magpie/internal/catalog" +) + +// keptStats are the ranges statsReadAll last read, and what they were read +// from. The Usage page asks for its range every 10 s and the Sessions page +// for 7, 30 and every day every 15 s; between two polls the files mostly +// haven't changed, and each read put every session of every range together +// again: ~1.1 GB a minute and a third, a fifth of all magpie allocated +// (#2278). Guarded by mu. +var keptStats struct { + files []keptFile + today time.Time + gen uint64 + ranges map[int]Stats + // native is every listed session's agent|id, nil when no read asked + // for it; shared, so read only + native map[string]bool + // the answers the reads got from the prices, Codex's names and Claude + // Desktop's, asked again before the ranges are given out + prices map[string]*catalog.Price + titles map[string]string + desktop map[string]string +} + +// keptFile is what of a listed file a range's stats are made from: its +// parse (a published parse is never written, a changed file gets a new +// one), and what the listing says of it. +type keptFile struct { + path, key, agent, wsl string + size int64 + mod int64 + main, readOnly bool + st *state +} + +func keptFileOf(f file) keptFile { + return keptFile{path: f.path, key: f.key, agent: f.agent, wsl: f.m().wsl, size: f.size, mod: f.mod.UnixNano(), + main: f.main, readOnly: f.readOnly, st: cache[f.path]} +} + +// keptFor reports whether the kept ranges were read from files as they +// are now, today, with the prices and names they got still the same. When +// they weren't, what was kept is let go. +func keptFor(files []file, today time.Time, price func(string) *catalog.Price, titles func(string) string) bool { + k := &keptStats + same := k.ranges != nil && k.gen == cacheGeneration && k.today.Equal(today) && len(k.files) == len(files) + for i := 0; same && i < len(files); i++ { + same = k.files[i] == keptFileOf(files[i]) + } + for model, p := range k.prices { + if !same { + break + } + same = reflect.DeepEqual(price(model), p) + } + for id, t := range k.titles { + if !same { + break + } + same = titles(id) == t + } + if same && len(k.desktop) > 0 { + ids := make([]string, 0, len(k.desktop)) + for id := range k.desktop { + ids = append(ids, id) + } + same = reflect.DeepEqual(DesktopTitles(ids), desktopKnown(k.desktop)) + } + if !same { + forgetKeptStats() + k.files, k.today, k.gen = make([]keptFile, len(files)), today, cacheGeneration + for i, f := range files { + k.files[i] = keptFileOf(f) + } + k.ranges, k.prices, k.titles, k.desktop = map[int]Stats{}, map[string]*catalog.Price{}, map[string]string{}, map[string]string{} + } + return same +} + +// keptRanges is how many ranges are kept: the page asks for three, and a +// days= of any other number is not kept beside them for ever. +const keptRanges = 8 + +// keepRange keeps st as days' Stats, letting go of a range asked is not +// among when there are keptRanges already. +func keepRange(days int, st Stats, asked []int) { + if len(keptStats.ranges) >= keptRanges { + for d := range keptStats.ranges { + if !slices.Contains(asked, d) { + delete(keptStats.ranges, d) + break + } + } + } + keptStats.ranges[days] = st +} + +// desktopKnown is names without the ids that have none, as DesktopTitles +// gives them. +func desktopKnown(names map[string]string) map[string]string { + out := map[string]string{} + for id, n := range names { + if n != "" { + out[id] = n + } + } + return out +} + +// keptPrice and keptTitle answer as price and titles do, and note the +// answer for keptFor. +func keptPrice(price func(string) *catalog.Price) func(string) *catalog.Price { + return func(model string) *catalog.Price { + p := price(model) + keptStats.prices[model] = p + return p + } +} + +func keptTitle(titles func(string) string) func(string) string { + return func(id string) string { + t := titles(id) + keptStats.titles[id] = t + return t + } +} + +// keepDesktop notes the Claude Desktop names a range's sessions got. +func keepDesktop(st Stats) { + var ids []string + for _, s := range st.Sessions { + if s.Agent == "claude-desktop" { + ids = append(ids, s.ID) + } + } + if len(ids) == 0 { + return + } + names := DesktopTitles(ids) + for _, id := range ids { + keptStats.desktop[id] = names[id] + } +} + +func forgetKeptStats() { + keptStats.files, keptStats.ranges, keptStats.native = nil, nil, nil + keptStats.prices, keptStats.titles, keptStats.desktop = nil, nil, nil +} diff --git a/internal/sessions/stats_kept_test.go b/internal/sessions/stats_kept_test.go new file mode 100644 index 000000000..b97233134 --- /dev/null +++ b/internal/sessions/stats_kept_test.go @@ -0,0 +1,112 @@ +package sessions + +import ( + "os" + "path/filepath" + "reflect" + "testing" + "time" + + "github.com/yetone/magpie/internal/catalog" + "github.com/yetone/magpie/internal/settings" +) + +func statsOutput(st Stats) (n int, cost float64) { + for _, s := range st.Sessions { + n += s.Tokens.Output + cost += s.Cost + } + return +} + +// TestStatsKeptBetweenPolls: a poll over files that haven't changed gives +// the ranges the last one read, not each put together again (#2278), and a +// file that changed, or a price, is read into them. +func TestStatsKeptBetweenPolls(t *testing.T) { + claude, _ := setup(t) + ranges := []int{0, 7, 30} + first, native := StatsAndNativeAll(ranges) + if len(first[0].Sessions) == 0 || len(native) == 0 { + t.Fatalf("no stats: %+v %v", first[0], native) + } + again, _ := StatsAndNativeAll(ranges) + if &again[0].Sessions[0] != &first[0].Sessions[0] { + t.Fatal("the unchanged files' stats were put together again") + } + if !reflect.DeepEqual(again, first) { + t.Fatalf("kept %+v\nread %+v", again, first) + } + + out, _ := statsOutput(first[0]) + f, err := os.OpenFile(filepath.Join(claude, "projects", "-work-app", "11111111-2222-3333-4444-555555555555.jsonl"), os.O_APPEND|os.O_WRONLY, 0) + if err != nil { + t.Fatal(err) + } + _, err = f.WriteString(`{"parentUuid":"a1","isSidechain":false,"message":{"model":"claude-opus-5-5","id":"msg_kept","type":"message","role":"assistant","content":[{"type":"text","text":"more"}],"usage":{"input_tokens":1,"output_tokens":7000}},"requestId":"req_kept","type":"assistant","uuid":"a-kept","timestamp":"2026-09-20T10:30:00.000Z","cwd":"/work/app","sessionId":"11111111-2222-3333-4444-555555555555"}` + "\n") + f.Close() + if err != nil { + t.Fatal(err) + } + grown, _ := StatsAndNativeAll(ranges) + if n, _ := statsOutput(grown[0]); n != out+7000 { + t.Fatalf("output %d after the file grew, %d before", n, out) + } + + _, cost := statsOutput(grown[0]) + PriceOf = func(_ settings.Settings, m string) (catalog.Price, bool) { + return catalog.Price{Input: 40, Output: 200, CacheRead: 2, CacheWrite: 50}, true + } + repriced, _ := StatsAndNativeAll(ranges) + if _, c := statsOutput(repriced[0]); c <= cost { + t.Fatalf("cost %v after the prices went up, %v before", c, cost) + } +} + +// TestStatsKeptNewDay: the day turning over reads the ranges again, as +// "today" and the ranges' first days moved. +func TestStatsKeptNewDay(t *testing.T) { + setup(t) + now := time.Now() + a := statsReadAll([]int{7}, now, nil)[0] + b := statsReadAll([]int{7}, now.AddDate(0, 0, 1), nil)[0] + if a.To == b.To { + t.Fatalf("the next day's range is today's: %s", b.To) + } +} + +// TestStatsKeptRangesBounded: a days= of every number is not kept. +func TestStatsKeptRangesBounded(t *testing.T) { + setup(t) + now := time.Now() + for d := 1; d <= 40; d++ { + statsReadAll([]int{0, d}, now, nil) + } + mu.Lock() + n, all := len(keptStats.ranges), keptStats.ranges[0].To != "" + mu.Unlock() + if n > keptRanges || !all { + t.Fatalf("%d ranges kept, every day's among them: %v", n, all) + } +} + +// TestMergeExternalStatsWritesNoInput: the gateway's sessions are merged +// into a copy, as the Stats merged into are kept for the next poll. +func TestMergeExternalStatsWritesNoInput(t *testing.T) { + usage := make([]Usage, 1, 4) + usage[0] = Usage{Agent: "claude", Model: "m", Tokens: Tokens{Output: 1}} + sums := make([]Summary, 1, 4) + sums[0] = Summary{Key: "claude:a", Agent: "claude", ID: "a", Last: time.Unix(10, 0)} + st := Stats{From: "2026-10-01", To: "2026-10-02", Days: []Day{{Date: "2026-10-02", Usage: usage}}, Sessions: sums} + keep := Stats{From: st.From, To: st.To, Days: []Day{{Date: "2026-10-02", Usage: slices1(usage)}}, Sessions: slices1(sums)} + merged := MergeExternalStats(st, []ExternalDayUsage{{Date: "2026-10-02", Agent: "claude", Model: "m", Tokens: Tokens{Output: 5}}, {Date: "2026-10-02", Agent: "opencode", Model: "x"}}, + []ExternalSession{{Agent: "opencode", ID: "g", Last: time.Unix(20, 0)}}) + if len(merged.Sessions) != 2 || merged.Sessions[0].ID != "g" || merged.Days[0].Usage[0].Tokens.Output != 6 || len(merged.Days[0].Usage) != 2 { + t.Fatalf("merged %+v", merged) + } + if !reflect.DeepEqual(st.Days[0].Usage, keep.Days[0].Usage) || !reflect.DeepEqual(st.Sessions, keep.Sessions) || + !reflect.DeepEqual(usage[:2], []Usage{keep.Days[0].Usage[0], {}}) || !reflect.DeepEqual(sums[:2], []Summary{keep.Sessions[0], {}}) { + t.Fatalf("the merge wrote what it was given: %+v %+v", usage[:2], sums[:2]) + } +} + +func slices1[T any](s []T) []T { return append([]T(nil), s...) } diff --git a/internal/sessions/stats_native_test.go b/internal/sessions/stats_native_test.go index 82bfb5e8f..1bdeca7e8 100644 --- a/internal/sessions/stats_native_test.go +++ b/internal/sessions/stats_native_test.go @@ -52,6 +52,10 @@ func TestStatsAllRangesOnePass(t *testing.T) { t.Fatalf("no stats: %+v", all[0]) } for i, days := range ranges { + // read again, not the ranges kept + mu.Lock() + forgetKeptStats() + mu.Unlock() if one := statsRead(days, now, nil); !reflect.DeepEqual(all[i], one) { t.Fatalf("days=%d: one pass %+v\nalone %+v", days, all[i], one) }