Skip to content

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

usage: one Vias index answers every since, instead of a rebuild from the whole log whenever the since changes (#2776)

yetone2fcd4b33c4c8
diff --git a/internal/usage/kept_strings_test.go b/internal/usage/kept_strings_test.go
index 2c8b24585..f2953b3a7 100644
--- a/internal/usage/kept_strings_test.go
+++ b/internal/usage/kept_strings_test.go
@@ -66,7 +66,7 @@ func TestSessionCachesHoldNoDecodedBlocks(t *testing.T) {
gatewayCache.entry = nil
gatewayCache.Unlock()
viasCache.Lock()
- viasCache.value = nil
+ viasCache.all, viasCache.window.m = nil, nil
viasCache.Unlock()
without := held()
decoded := uint64(blocks * logBlockRows * text)
diff --git a/internal/usage/usage.go b/internal/usage/usage.go
index 116d119d0..c3f26802a 100644
--- a/internal/usage/usage.go
+++ b/internal/usage/usage.go
@@ -1003,8 +1003,9 @@ func Vias(since time.Time) ViaIndex {
return cachedVias(since)
}
-func buildVias(snapshot *logSnapshot, since time.Time) map[string][]Via {
+func buildVias(snapshot *logSnapshot, since time.Time) *viaLists {
out := map[string][]Via{}
+ first := map[string]time.Time{}
keep := keptStrings{}
snapshot.visit(since, func(r Record) {
if r.IsRejected() || r.IsReported() || r.Session == "" || r.Model == "" {
@@ -1013,6 +1014,9 @@ func buildVias(snapshot *logSnapshot, since time.Time) map[string][]Via {
r.Provider, r.Model, r.Effort = keep.of(r.Provider), 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
+ }
i := 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)
@@ -1031,7 +1035,7 @@ func buildVias(snapshot *logSnapshot, since time.Time) map[string][]Via {
for _, vs := range out {
sort.SliceStable(vs, func(i, j int) bool { return vs[i].Calls > vs[j].Calls })
}
- return out
+ return &viaLists{m: out, first: first}
}
// Saw notes a request from an agent arriving now.
diff --git a/internal/usage/vias_cache.go b/internal/usage/vias_cache.go
index 1d821fc4b..656019703 100644
--- a/internal/usage/vias_cache.go
+++ b/internal/usage/vias_cache.go
@@ -6,38 +6,105 @@ import (
"time"
)
+// viaLists is what the gateway sent each session's calls to, and when each
+// session's first call was: a list counted from any time at or before that
+// first call is the same list.
+type viaLists struct {
+ m map[string][]Via
+ first map[string]time.Time
+}
+
+// viasCache keeps one index of the whole log, which answers every since a
+// session's calls all came after, and one window for a session that had
+// calls before its since. The Sessions list, each agent's manage page and
+// a session's detail each ask from a since of their own; one kept since
+// was built again from the whole log at almost every ask, ~280 MB a time
+// over ~720k calls, on every poll of an open page (#2776).
var viasCache struct {
sync.Mutex
version uint64
- since time.Time
clients string // the clients counted as agents (#1958)
- value map[string][]Via
+ all *viaLists
+ window struct {
+ version uint64
+ clients string
+ since time.Time
+ m map[string][]Via
+ }
}
// ViaIndex is Vias' answer: the one copy every caller shares, handed out
// whole and read only through Of, which gives each session's list as a
// copy of its own. Each ask cloned every session's list, ~66 MB a time
// over ~720k calls, where a page reads the few sessions it lists (#2776).
-type ViaIndex struct{ m map[string][]Via }
+type ViaIndex struct {
+ all *viaLists
+ since time.Time
+ snapshot *logSnapshot
+ clients string
+}
-// Of is what the gateway sent one session's calls to ("agent|id"), the
-// most calls first; nil for a session it sent none of.
-func (x ViaIndex) Of(key string) []Via { return slices.Clone(x.m[key]) }
+// Of is what the gateway sent one session's calls to ("agent|id") since
+// the index's time, the most calls first; nil for a session it sent none
+// of.
+func (x ViaIndex) Of(key string) []Via {
+ vs, ok := x.all.m[key]
+ if !ok {
+ return nil
+ }
+ if x.since.IsZero() || !x.all.first[key].Before(x.since) {
+ return slices.Clone(vs)
+ }
+ return slices.Clone(x.window()[key])
+}
// Len is how many sessions have a list.
-func (x ViaIndex) Len() int { return len(x.m) }
+func (x ViaIndex) Len() int {
+ if x.since.IsZero() {
+ return len(x.all.m)
+ }
+ n := 0
+ for _, vs := range x.all.m {
+ if slices.ContainsFunc(vs, func(v Via) bool { return !v.Last.Before(x.since) }) {
+ n++
+ }
+ }
+ return n
+}
+
+// window is the lists counted from the index's since, for a session that
+// had calls before it.
+func (x ViaIndex) window() map[string][]Via {
+ viasCache.Lock()
+ defer viasCache.Unlock()
+ w := &viasCache.window
+ if w.m != nil && w.version == x.snapshot.version && w.clients == x.clients && w.since.Equal(x.since) {
+ return w.m
+ }
+ m := buildVias(x.snapshot, x.since).m
+ if x.snapshot.complete() {
+ w.version, w.clients, w.since, w.m = x.snapshot.version, x.clients, x.since, m
+ }
+ return m
+}
func cachedVias(since time.Time) ViaIndex {
snapshot := readLogSnapshot()
clients := clientAgentsKey()
viasCache.Lock()
defer viasCache.Unlock()
- value := viasCache.value
- if value == nil || viasCache.version != snapshot.version || !viasCache.since.Equal(since) || viasCache.clients != clients {
- value = buildVias(snapshot, since)
- if snapshot.info != nil && snapshot.off == snapshot.info.Size() {
- viasCache.version, viasCache.since, viasCache.value, viasCache.clients = snapshot.version, since, value, clients
+ all := viasCache.all
+ if all == nil || viasCache.version != snapshot.version || viasCache.clients != clients {
+ all = buildVias(snapshot, time.Time{})
+ if snapshot.complete() {
+ viasCache.version, viasCache.clients, viasCache.all = snapshot.version, clients, all
}
}
- return ViaIndex{value}
+ return ViaIndex{all: all, since: since, snapshot: snapshot, clients: clients}
+}
+
+// complete is whether the snapshot reached the end of the log, so what is
+// built from it can be kept for its version.
+func (s *logSnapshot) complete() bool {
+ return s.info != nil && s.off == s.info.Size()
}
diff --git a/internal/usage/vias_since_test.go b/internal/usage/vias_since_test.go
new file mode 100644
index 000000000..c1ad8b7ed
--- /dev/null
+++ b/internal/usage/vias_since_test.go
@@ -0,0 +1,86 @@
+package usage
+
+import (
+ "encoding/json"
+ "fmt"
+ "os"
+ "path/filepath"
+ "runtime"
+ "testing"
+ "time"
+)
+
+// The Sessions list, each agent's manage page and a session's detail each
+// ask Vias from a since of their own. One kept since was built again from
+// the whole log at almost every ask, ~280 MB a time over ~720k calls, on
+// every poll of an open page (#2776). One index now answers every since a
+// session's calls all came after, and a session with calls before its
+// since still counts only those after it.
+func TestViasAnswerEverySinceFromOneIndex(t *testing.T) {
+ pageHome(t)
+ settleLogClock(t)
+ const sessionCount = 20000
+ if err := os.MkdirAll(filepath.Dir(Path()), 0o700); err != nil {
+ t.Fatal(err)
+ }
+ f, err := os.Create(Path())
+ if err != nil {
+ t.Fatal(err)
+ }
+ enc := json.NewEncoder(f)
+ now := Clock()
+ write := func(r Record) {
+ if err := enc.Encode(r); err != nil {
+ t.Fatal(err)
+ }
+ }
+ for i := range sessionCount {
+ write(Record{Time: now.Add(-time.Duration(sessionCount-i) * time.Second), Agent: "pi", Session: fmt.Sprintf("s%d", i), Provider: "p", Model: "m", Input: 10, Status: 200})
+ }
+ // one session with a call on each side of a since
+ write(Record{Time: now.Add(-time.Hour), Agent: "pi", Session: "both", Provider: "p", Model: "m", Input: 10, Status: 200})
+ write(Record{Time: now, Agent: "pi", Session: "both", Provider: "p", Model: "m", Input: 10, Status: 200})
+ if err := f.Close(); err != nil {
+ t.Fatal(err)
+ }
+ if got := Vias(now.Add(-time.Minute)).Of("pi|both"); len(got) != 1 || got[0].Calls != 1 {
+ t.Fatalf("since a minute ago: %+v, want the one call after it", got)
+ }
+ if got := Vias(time.Time{}).Of("pi|both"); len(got) != 1 || got[0].Calls != 2 {
+ t.Fatalf("since ever: %+v, want both calls", got)
+ }
+ // the last 60 sessions, a second apart up to a second ago, and "both"
+ if n := Vias(now.Add(-time.Minute)).Len(); n != 61 {
+ t.Fatalf("%d sessions with calls since a minute ago, want 61", n)
+ }
+ if n := Vias(time.Time{}).Len(); n != sessionCount+1 {
+ t.Fatalf("%d sessions, want %d", n, sessionCount+1)
+ }
+ // the list, a manage page and a detail, asking in turn
+ sinces := []time.Time{
+ now.Add(-(sessionCount + 60) * time.Second),
+ now.Add(-(sessionCount/2 + 60) * time.Second),
+ now.Add(-(sessionCount - 10 + 60) * time.Second),
+ }
+ key := fmt.Sprintf("pi|s%d", sessionCount-10)
+ for _, since := range sinces {
+ if got := Vias(since).Of(key); len(got) != 1 || got[0].Calls != 1 {
+ t.Fatalf("since %v: %+v", since, got)
+ }
+ }
+ const asks = 30
+ var before, after runtime.MemStats
+ runtime.GC()
+ runtime.ReadMemStats(&before)
+ for i := range asks {
+ Vias(sinces[i%len(sinces)]).Of(key)
+ }
+ runtime.ReadMemStats(&after)
+ // building the lists of 20000 sessions is several MB; the log's check
+ // and one list are tens of KB
+ per := (after.TotalAlloc - before.TotalAlloc) / asks
+ t.Logf("an ask allocated %d bytes", per)
+ if per > 256<<10 {
+ t.Fatalf("an ask allocated %d bytes: the lists were built again", per)
+ }
+}