From d50ae6fcb1fed8ef054029009b2b53c77709f4e9 Mon Sep 17 00:00:00 2001 From: yetone Date: Sun, 15 Nov 2026 00:44:12 +0800 Subject: [PATCH] usage: an agent group says when its last call was The Usage page will fold a small client idle for days into one Other row (WJaunita45604 on X: dozens of randomly named test clients). It needs each agent's newest call in the period, so Group gets Last (json "last", omitted when zero), set for agents only by summaryCounts.add and by direct(). Run: go test ./internal/usage -run 'AgentsSayTheirLastCall|LastCallMovesOn' on mica, ok. Broken by hand: without the Last assignments both tests go red. --- internal/usage/agent_last_test.go | 62 +++++++++++++++++++++++++++++++ internal/usage/direct.go | 3 ++ internal/usage/summary_fold.go | 6 ++- internal/usage/usage.go | 4 ++ 4 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 internal/usage/agent_last_test.go diff --git a/internal/usage/agent_last_test.go b/internal/usage/agent_last_test.go new file mode 100644 index 000000000..4af3000d9 --- /dev/null +++ b/internal/usage/agent_last_test.go @@ -0,0 +1,62 @@ +package usage + +import ( + "encoding/json" + "strings" + "testing" + "time" +) + +// An agent's group says when its newest call in the period was made +// (Group.Last), for the Usage page to fold the clients idle for days +// (WJaunita45604 on X): a summary of the records, one counted on from the +// log's kept prefix after a later call, and the session files' own +// (direct). The other lists say nothing of it. +func TestSummaryAgentsSayTheirLastCall(t *testing.T) { + now := time.Date(2026, 11, 15, 12, 0, 0, 0, time.Local) + early, late := now.AddDate(0, 0, -5), now.Add(-time.Hour) + recs := []Record{ + {Time: late, Agent: "bun/1.3", Provider: "relay", Model: "m", Input: 10, Status: 200}, + {Time: early, Agent: "bun/1.3", Provider: "relay", Model: "m", Input: 10, Status: 200}, + {Time: early, Agent: "1340C", Provider: "relay", Model: "m", Input: 10, Status: 200}, + } + last := func(s Summary) map[string]time.Time { + out := map[string]time.Time{} + for _, g := range s.Agents { + out[g.ID] = g.Last + } + return out + } + s := summarize(Month, now, recs) + got := last(s) + if !got["bun"].Equal(late) || !got["1340C"].Equal(early) { + t.Fatalf("last calls: %v", got) + } + b, _ := json.Marshal(s.Models) + if strings.Contains(string(b), `"last"`) { + t.Fatalf("a model says a last call: %s", b) + } + + rows := []Row{} + for _, r := range recs { + rows = append(rows, Row{Record: r, Source: "log"}) + } + got = last(direct(Month, now, rows)) + if !got["bun"].Equal(late) || !got["1340C"].Equal(early) { + t.Fatalf("session files' last calls: %v", got) + } +} + +func TestLoggedSummaryAgentsLastCallMovesOn(t *testing.T) { + pageHome(t) + now := time.Date(2026, 9, 30, 12, 0, 0, 0, time.Local) + holdClock(t, now) + Append(Record{Time: now.Add(-2 * time.Hour), Agent: "1340C", Input: 3, Status: 200}) + if g := Summarize(Today).Agents; len(g) != 1 || !g[0].Last.Equal(now.Add(-2*time.Hour)) { + t.Fatalf("first: %+v", g) + } + Append(Record{Time: now.Add(-time.Minute), Agent: "1340C", Input: 3, Status: 200}) + if g := Summarize(Today).Agents; len(g) != 1 || !g[0].Last.Equal(now.Add(-time.Minute)) || g[0].Calls != 2 { + t.Fatalf("after another call: %+v", g) + } +} diff --git a/internal/usage/direct.go b/internal/usage/direct.go index 9a8c452d9..32de53783 100644 --- a/internal/usage/direct.go +++ b/internal/usage/direct.go @@ -101,6 +101,9 @@ func direct(p Period, now time.Time, rows []Row) Summary { s.Totals.addRow(r) id := ReportedAs(r.Agent) put(agents, id, Group{ID: id}, r) + if a := agents[id]; r.Time.After(a.Last) { + a.Last = r.Time + } model := r.Model if alias != nil { model = alias(model) diff --git a/internal/usage/summary_fold.go b/internal/usage/summary_fold.go index 0842243b6..6465f09a7 100644 --- a/internal/usage/summary_fold.go +++ b/internal/usage/summary_fold.go @@ -238,7 +238,11 @@ func (c *summaryCounts) add(u *summaryRun, k *summaryKeying, r Record) { c.series[i].add(r, pr) } id := ReportedAs(r.Agent) // one kept before magpie knew the agent by name - c.agents.at(id, func() Group { return Group{ID: id} }).add(r, pr) + a := c.agents.at(id, func() Group { return Group{ID: id} }) + a.add(r, pr) + if r.Time.After(a.Last) { + a.Last = r.Time + } model := r.Model if u.alias != nil { model = u.alias(model) diff --git a/internal/usage/usage.go b/internal/usage/usage.go index ed81c1df6..55d03f7a1 100644 --- a/internal/usage/usage.go +++ b/internal/usage/usage.go @@ -1014,6 +1014,10 @@ type Group struct { Host string `json:"host,omitempty"` // Agent is the agent a session is of (sessions only). Agent string `json:"agent,omitempty"` + // Last is when the group's newest call in the period was made (agents + // only): the Usage page folds a small client idle for days into one + // "Other" row (WJaunita45604 on X: dozens of test clients). + Last time.Time `json:"last,omitzero"` Totals // Own is the share of these calls magpie made by itself (Record.ByMagpie): // a routing classifier's, a web search's or an image description's,