Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.
usage: a session's model lists count only the log's blocks since the last
yetone2be04305fd60
diff --git a/internal/usage/usage.go b/internal/usage/usage.goindex d3ef41ac4..571791665 100644--- a/internal/usage/usage.go+++ b/internal/usage/usage.go@@ -10,6 +10,7 @@ import ("errors""fmt""io/fs"+ "maps""math""os""path/filepath"@@ -979,19 +980,37 @@ func Vias(since time.Time) ViaIndex {// A provider renamed since is listed by the id it has now (renames): the// Sessions tab named a renamed one by its old id (cybercaius).func buildVias(snapshot *logSnapshot, since time.Time, renames provider.Renames) *viaLists {- out := map[string][]Via{}- first := map[string]time.Time{}- keep := keptStrings{}- snapshot.visit(since, func(r Record) {+ counted := map[string]viaAcc{}+ countVias(snapshot.blocks, since, renames, nil, counted, keptStrings{})+ return listedVias(&viaLists{m: map[string][]Via{}, first: map[string]time.Time{}}, counted)+}++// viaAcc is one session's lists as counted, in the order its calls first+// named each, and when its first call was.+type viaAcc struct {+ vs []Via+ first time.Time+}++// countVias counts blocks' calls into own, a session's counts begun from+// a copy of base's: base is never written.+func countVias(blocks []*rowChunk, since time.Time, renames provider.Renames, base, own map[string]viaAcc, keep keptStrings) {+ visitBlocks(blocks, since, func(r Record) {if r.IsRejected() || r.IsReported() || r.Session == "" || r.Model == "" {return}r.Provider, r.Model, r.Effort = keep.of(renames.Of(r.Provider, r.Time)), keep.of(r.Model), keep.of(r.Effort)k := ReportedAs(r.Agent) + "|" + r.Session- vs := out[k]- if t, seen := first[k]; !seen || r.Time.Before(t) {- first[k] = r.Time+ a, seen := own[k]+ if !seen {+ if b, ok := base[k]; ok {+ a, seen = viaAcc{vs: slices.Clone(b.vs), first: b.first}, true+ }}+ if !seen || r.Time.Before(a.first) {+ a.first = r.Time+ }+ vs := a.vsi := slices.IndexFunc(vs, func(v Via) bool { return v.Provider == r.Provider && v.Model == r.Model && v.Effort == r.Effort })if i < 0 {vs, i = append(vs, Via{Provider: r.Provider, Model: r.Model, Effort: r.Effort}), len(vs)@@ -1005,12 +1024,24 @@ func buildVias(snapshot *logSnapshot, since time.Time, renames provider.Renames)if r.Time.After(vs[i].Last) {vs[i].Last = r.Time}- out[k] = vs+ a.vs = vs+ own[k] = a})- for _, vs := range out {+}++// listedVias is lists with each session counted in counted listed in+// place of its own, the most calls first; lists itself is not written.+func listedVias(lists *viaLists, counted map[string]viaAcc) *viaLists {+ if len(counted) == 0 {+ return lists+ }+ out := &viaLists{m: maps.Clone(lists.m), first: maps.Clone(lists.first)}+ for k, a := range counted {+ vs := slices.Clone(a.vs)sort.SliceStable(vs, func(i, j int) bool { return vs[i].Calls > vs[j].Calls })+ out.m[k], out.first[k] = vs, a.first}- return &viaLists{m: out, first: first}+ return out}// Saw notes a request from an agent arriving now.diff --git a/internal/usage/vias_cache.go b/internal/usage/vias_cache.goindex 147e965c5..73de38d5e 100644--- a/internal/usage/vias_cache.go+++ b/internal/usage/vias_cache.go@@ -1,6 +1,7 @@package usageimport (+ "maps""slices""sync""time"@@ -27,7 +28,10 @@ var viasCache struct {version uint64clients string // the clients counted as agents (#1958), and the renamesall *viaLists- window struct {+ // what all counted over the log's full blocks, which a call logged+ // counts on from: each call was ~280 MB of the whole log again (#2278)+ prefix *viaPrefix+ window struct {version uint64clients stringsince time.Time@@ -101,9 +105,10 @@ func cachedVias(since time.Time) ViaIndex {defer viasCache.Unlock()all := viasCache.allif all == nil || viasCache.version != snapshot.version || viasCache.clients != clients {- all = buildVias(snapshot, time.Time{}, renames)+ var prefix *viaPrefix+ all, prefix = foldVias(snapshot, renames, clients, viasCache.prefix)if snapshot.complete() {- viasCache.version, viasCache.clients, viasCache.all = snapshot.version, clients, all+ viasCache.version, viasCache.clients, viasCache.all, viasCache.prefix = snapshot.version, clients, all, prefix}}return ViaIndex{all: all, since: since, snapshot: snapshot, clients: clients, renames: renames}@@ -114,3 +119,43 @@ func cachedVias(since time.Time) ViaIndex {func (s *logSnapshot) complete() bool {return s.info != nil && s.off == s.info.Size()}++// viaPrefix is the whole log's lists counted over its first n blocks, all+// of them full: a full block is never written again (logSnapshot), and a+// log read afresh has blocks of its own, so the last of them still being+// blocks[n-1] says the n are the same.+type viaPrefix struct {+ n int+ last *rowChunk+ clients string+ counted map[string]viaAcc // never written: a count copies what it touches+ lists *viaLists // counted's, listed+}++// viaFolds counts the lists counted on from a prefix, for the tests.+var viaFolds int++// foldVias is buildVias of the whole log, counted on from pre where it+// fits, and the prefix of the log's full blocks to count on from next.+// Its calls are counted in the log's order, as buildVias' are, so every+// sum is the same to the last bit. Called under viasCache's lock.+func foldVias(snapshot *logSnapshot, renames provider.Renames, clients string, pre *viaPrefix) (*viaLists, *viaPrefix) {+ blocks := snapshot.blocks+ cut := max(len(blocks)-1, 0)+ if pre == nil || pre.clients != clients || pre.n > cut || pre.n > 0 && blocks[pre.n-1] != pre.last {+ pre = &viaPrefix{clients: clients, counted: map[string]viaAcc{}, lists: &viaLists{m: map[string][]Via{}, first: map[string]time.Time{}}}+ } else if pre.n > 0 {+ viaFolds+++ }+ keep := keptStrings{}+ next := pre+ if pre.n < cut {+ own := map[string]viaAcc{}+ countVias(blocks[pre.n:cut], time.Time{}, renames, pre.counted, own, keep)+ next = &viaPrefix{n: cut, last: blocks[cut-1], clients: clients, counted: maps.Clone(pre.counted), lists: listedVias(pre.lists, own)}+ maps.Copy(next.counted, own)+ }+ tail := map[string]viaAcc{}+ countVias(blocks[cut:], time.Time{}, renames, next.counted, tail, keep)+ return listedVias(next.lists, tail), next+}diff --git a/internal/usage/vias_fold_test.go b/internal/usage/vias_fold_test.gonew file mode 100644index 000000000..3b28e9483--- /dev/null+++ b/internal/usage/vias_fold_test.go@@ -0,0 +1,51 @@+package usage++import (+ "fmt"+ "reflect"+ "testing"+ "time"++ "github.com/yetone/magpie/internal/provider"+)++// The whole log's model lists, counted on from the full blocks counted+// before, are the lists counted afresh: as calls come in over a block's+// edge, and when the log is written over.+func TestViasCountedOnAreCountedAfresh(t *testing.T) {+ pageHome(t)+ now := holdClock(t, time.Date(2026, 9, 30, 12, 0, 0, 0, time.Local))+ writeFoldLog(t, now, 3*logBlockRows+100)+ check := func(step string) {+ t.Helper()+ got := cachedVias(time.Time{}).all+ want := buildVias(readLogSnapshot(), time.Time{}, provider.UsageRenames())+ if len(got.m) == 0 {+ t.Fatalf("%s: no lists", step)+ }+ if !reflect.DeepEqual(got.m, want.m) || !reflect.DeepEqual(got.first, want.first) {+ t.Fatalf("%s: counted on %d lists, afresh %d, or they differ", step, len(got.m), len(want.m))+ }+ }+ check("first")+ before := viaFolds+ for i := 0; i < logBlockRows+50; i++ {+ r := foldRecord(now, i)+ r.Time = now.Add(-time.Duration(i) * time.Second)+ r.Effort = []string{"", "high"}[i%2]+ Append(r)+ if i == 0 || i == 600 {+ check(fmt.Sprint("appended ", i+1))+ }+ }+ check("appended a block")+ if viaFolds-before < 2 {+ t.Fatalf("only %d lists counted on from what they had counted", viaFolds-before)+ }+ // a session first seen in the last block, then a model it never used+ Append(Record{Time: now, Agent: "codex", Session: "new", Provider: "openai", Model: "m3", Input: 5, Status: 200})+ Append(Record{Time: now, Agent: "codex", Session: "s0-0", Provider: "openai", Model: "m3", Input: 5, Status: 200})+ check("a new model")+ writeFoldLog(t, now, 2*logBlockRows+7)+ check("written over")+}