diff --git a/internal/usage/agent_models_test.go b/internal/usage/agent_models_test.go new file mode 100644 index 000000000..9b381a1ee --- /dev/null +++ b/internal/usage/agent_models_test.go @@ -0,0 +1,71 @@ +package usage + +import ( + "os" + "path/filepath" + "reflect" + "testing" + "time" + + "github.com/yetone/magpie/internal/sessions" +) + +// A page's AgentModels are, for each agent, what the same page with that +// agent picked has as its By["modelAt"]: a day's card reads them off the +// day's own page instead of building a page of each agent (WaiooT on X), +// on the cells' path and on the ledger's (LogCalls) alike. +func TestAgentModelsAreEachAgentsPage(t *testing.T) { + for _, viaLog := range []bool{false, true} { + t.Run(map[bool]string{false: "cells", true: "ledger"}[viaLog], func(t *testing.T) { + home := t.TempDir() + t.Setenv("HOME", home) + t.Setenv("USERPROFILE", home) + t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config")) + t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache")) + os.MkdirAll(filepath.Join(home, ".config"), 0o755) + now := holdClock(t, time.Date(2026, 11, 15, 12, 0, 0, 0, time.Local)) + for i, r := range []Record{ + {Agent: "claude-cli/2.1", Provider: "anthropic", Model: "claude-x", Input: 50, Output: 5}, + {Agent: "claude-cli/2.1", Provider: "relay", Model: "claude-x", Input: 40, Output: 4}, + {Agent: "claude-cli/2.1", Provider: "anthropic", Model: "claude-y", Input: 30, Output: 3}, + {Agent: "codex_cli_rs/0.9", Provider: "openai", Model: "gpt-z", Input: 20, Output: 2}, + {Agent: "", Provider: "openai", Model: "gpt-z", Input: 10, Output: 1}, + // yesterday's, which a page of today leaves out + {Agent: "claude-cli/2.1", Provider: "anthropic", Model: "claude-old", Input: 8, Output: 8, Time: now.AddDate(0, 0, -1)}, + {Agent: "codex_cli_rs/0.9", Provider: "openai", Model: "gpt-old", Input: 9, Output: 9, Time: now.AddDate(0, 0, -1)}, + } { + if r.Time.IsZero() { + r.Time = now.Add(-time.Duration(i+1) * time.Minute) + } + r.Status = 200 + Append(r) + } + if viaLog { + old := LogCalls + LogCalls = func(time.Time) []sessions.Call { + return []sessions.Call{{Time: now.Add(-time.Hour), Agent: "codex", Session: "c1", Model: "gpt-w", Tokens: sessions.Tokens{Input: 7, Output: 7}}} + } + t.Cleanup(func() { LogCalls = old }) + } + today := Filter{Day: now.Format(time.DateOnly), Zone: time.Local} + yesterday := Filter{Day: now.AddDate(0, 0, -1).Format(time.DateOnly), Zone: time.Local} + for _, f := range []Filter{{}, today, yesterday} { + page := QueryPage(Week, f, 0, 1) + if len(page.By["agent"]) < 1 { + t.Fatalf("agents %+v", page.By["agent"]) + } + for _, a := range page.By["agent"] { + if a.ID == "" { + continue + } + af := f + af.Agent = a.ID + want := QueryPage(Week, af, 0, 1).By["modelAt"] + if got := page.AgentModels[a.ID]; !reflect.DeepEqual(got, want) { + t.Errorf("%+v: %s's models %+v, its own page's %+v", f, a.ID, got, want) + } + } + } + }) + } +} diff --git a/internal/usage/ledger.go b/internal/usage/ledger.go index f6705863f..2eb9001a3 100644 --- a/internal/usage/ledger.go +++ b/internal/usage/ledger.go @@ -1126,6 +1126,15 @@ func (r Row) Floor() (tokens int, ms int64) { // so the clients a shared gateway serves can be compared (#2521). var Dimensions = []string{"provider", "agent", "model", "modelAt", "callerKey"} +// modelAtDim is "modelAt"'s place in Dimensions. +const modelAtDim = 3 + +func init() { + if Dimensions[modelAtDim] != "modelAt" { + panic("usage: modelAtDim is not modelAt's place in Dimensions") + } +} + // NoCallerKey is the "callerKey" of a call that carried no gateway key: one // that wasn't counted against any, as this computer's own agents' are, or // that came before keys were recorded. As a filter it keeps only those. diff --git a/internal/usage/request_page.go b/internal/usage/request_page.go index 3fbd4f84b..b97b09ce0 100644 --- a/internal/usage/request_page.go +++ b/internal/usage/request_page.go @@ -55,6 +55,12 @@ type RequestPage struct { Series []SeriesPoint By map[string][]Share ChartBy map[string][]Share // the whole period's legend while a day is selected + // AgentModels is each agent's By["modelAt"], by the agent a filter + // names it by (ReportedAs): what the same page with that agent picked + // would have as its By["modelAt"], counted in the same pass. A day's + // card asked for a page of each agent beside the day's own, a whole + // build each, every time the day was switched (WaiooT on X). + AgentModels map[string][]Share // Computers are the rows told apart by the computer they were made on, // ThisComputer's and each other's by id, of the rows without the // filter's computer, and Names what the others are called: none when no @@ -2542,6 +2548,8 @@ func buildRequestBlocksIn(p Period, f Filter, offset, limit int, now time.Time, chartGroups[d] = map[string]*Share{} seriesGroups[d] = map[string]*Share{} } + agentModels := map[string]map[string]*Share{} + reported := map[string]string{} // ReportedAs, once for each agent // Bound the requested prefix before adding limit: external offsets can // reach MaxInt, and a page beyond all sources needs no heap at all. maxRows := 0 @@ -2667,7 +2675,21 @@ func buildRequestBlocksIn(p Period, f Filter, offset, limit int, now time.Time, add(chartGroups[dim], keys.key(&r, dim), d) } if cell.bits&(cellGroup< 0 { + out.AgentModels[a] = m + } + } for _, d := range Dimensions { g := f.Without(d) out.By[d] = Breakdown(all.Filtered(g).Rows, d) diff --git a/internal/usageapi/day.go b/internal/usageapi/day.go index b79d8fcf9..e900e09c6 100644 --- a/internal/usageapi/day.go +++ b/internal/usageapi/day.go @@ -6,6 +6,7 @@ import ( "time" "github.com/yetone/magpie/internal/provider" + "github.com/yetone/magpie/internal/sessions" "github.com/yetone/magpie/internal/usage" ) @@ -120,6 +121,10 @@ func SpanOf(from, date string, zone *time.Location) (Day, error) { if date > today { return Day{}, fmt.Errorf("%s is after today, %s", date, today) } + // the session folders listed once for the reads below, not once + // each: a long history's thousands of files were listed and stat'd + // for every one of them (WaiooT on X) + defer sessions.OneListing()() f := usage.Filter{Zone: zone} var p usage.Period switch from { @@ -165,16 +170,17 @@ func SpanOf(from, date string, zone *time.Location) (Day, error) { if c := agents[s.ID]; c != nil { a.Name, a.Icon = c.Name, c.Icon } - af := f - af.Agent = s.ID - if s.ID == "" { - // a filter of "" keeps every agent: the calls that named none - // are this group's own, and are listed by model without it - af.Agent = "" - } + // each agent's models are of the same build as the day's own + // (AgentModels): a page of each agent beside it was a build each, + // all of them again on every switch of the day (WaiooT on X) var models []usage.Share - if s.ID != "" || len(all.By["agent"]) == 1 { - models = usage.QueryPage(p, af, 0, 1).By["modelAt"] + switch { + case s.ID != "": + models = all.AgentModels[s.ID] + case len(all.By["agent"]) == 1: + // the calls that named no agent are all there are: a filter of + // "" would keep every agent's, so they are listed only alone + models = all.By["modelAt"] } for j, m := range models { if j == DayModels {