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.
This commit is contained in:
yetone
2026-11-15 02:50:48 +08:00
parent 9afec644cc
commit d50ae6fcb1
4 changed files with 74 additions and 1 deletions
+62
View File
@@ -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)
}
}
+3
View File
@@ -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)
+5 -1
View File
@@ -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)
+4
View File
@@ -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,