Files
magpie/internal/gui/history_agent_test.go
T
yetone 2da173214f routing: a kept request is named by its agent as magpie knows it now, so Kimi Code's old requests show its icon (jiabubu)
jiabubu on Discord, after fa5e86cf2: the Usage page's request list now
shows Kimi Code, but the Context tab's agent cards and agent list, and the
Routing page's request list, still drew Kimi Code with the cube.

The GUI resolves every agent icon one way: by id in /api/state's clients
(context.js agentMeta, routing client.js agentOf, usage page.js). The
difference was in the ids the views were given. The Usage page reads the
ledger, whose records are named again as they are read
(usage.ReportedAs -> AgentOf), so a record kept as "kimi-code-cli" before
fa5e86cf2 reads as "kimi". The Routing page's list and the Context tab
read the gateway's kept routes (gateway.History, HistoryRoute), which
kept the raw "kimi-code-cli": no client has that id, so the row and a
card of its own got the cube and the raw name.

- routedAgain, which both readers of the kept routes already run on each
  route, names its agent again through usage.AgentOf. AgentOf keeps every
  known id (each client's Names begin with its id) and any unknown name as
  it is, so claude-desktop, cursor-local, plugin agents and library names
  are unchanged. The live trace's routes are named at request time already.
- No GUI change: every view that draws an agent icon looks it up by id in
  state.clients (checked: context.js, routing/client.js, usage/page.js,
  app.js account rows), so the id was the one thing to fix. app.js and the
  size budget are untouched.

Guard: TestKeptRoutesNameTheirAgentAgain (internal/gui) writes a day of
kept routes as kimi-code-cli, kimi, kimi-code-desktop, claude-desktop and
Python-urllib, and checks /api/gateway/history gives kimi, kimi,
kimi-code-desktop, claude-desktop, Python-urllib, and /api/context puts both
Kimi requests on one "kimi" card with no "kimi-code-cli" card or session.

Run (mote, at 64197cac1 rebased on origin/main 30f29564e; the guard and History tests run again there):
- Red on the old history.go: "history's agents [kimi-code-cli kimi ...],
  want [kimi kimi ...]".
- go test ./internal/gui -run 'TestKeptRoutesNameTheirAgentAgain|Context|
  History|Trace|Title|ClientHas': ok.
- go test ./internal/gateway -run 'History|Trace|Route|Agent': ok.
- go vet (darwin), GOOS=linux -tags nogui and GOOS=windows vet of
  internal/gateway and internal/gui: ok. gofmt -l: nothing.

Not tried: the GUI suite (no asset changed, so no GUI test can tell old
from new; the routing-client-icons fixture already holds the JS side), a
real Kimi Code against magpie, the full Go suite, Windows/Linux runs of
the tests.
2026-10-31 12:44:16 +08:00

80 lines
2.3 KiB
Go

package gui
import (
"bytes"
"encoding/json"
"os"
"path/filepath"
"testing"
"time"
"github.com/yetone/magpie/internal/gateway"
)
// A route kept before usage.AgentOf knew Kimi Code 2.x's User-Agent
// ("kimi-code-cli/…") carries that raw name. The ledger names its records
// again as it reads them, so the Usage page's request list showed Kimi
// Code; the Routing page's request list and the Context tab's agent cards
// read the kept routes and drew a "kimi-code-cli" of their own with the
// cube (jiabubu on Discord). Both read the routes as AgentOf names them
// now: "kimi", Kimi Code's id, which /api/state's clients give its icon.
func TestKeptRoutesNameTheirAgentAgain(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
mux := keptMux(t)
traceRoutes(mux)
ctxKept.Lock()
ctxKept.days, ctxKept.key = map[string]ctxDay{}, ""
ctxKept.Unlock()
dir := gateway.HistoryDir()
if err := os.MkdirAll(dir, 0o700); err != nil {
t.Fatal(err)
}
now := time.Now()
day := now.Format("2006-01-02")
var b bytes.Buffer
for i, agent := range []string{"kimi-code-cli", "kimi", "kimi-code-desktop", "claude-desktop", "Python-urllib"} {
r := ctxRoute(int64(i+1), agent, "s", 30000, 1000, 200000, 0)
r.Time = now.Add(-time.Duration(5-i) * time.Minute)
j, _ := json.Marshal(r)
b.Write(append(j, '\n'))
}
if err := os.WriteFile(filepath.Join(dir, day+".jsonl"), b.Bytes(), 0o600); err != nil {
t.Fatal(err)
}
var hist struct {
Routes []gateway.Route `json:"routes"`
}
getJSON(t, mux, "/api/gateway/history?day="+day, &hist)
var got []string
for _, r := range hist.Routes {
got = append(got, r.Agent)
}
want := []string{"kimi", "kimi", "kimi-code-desktop", "claude-desktop", "Python-urllib"}
if len(got) != len(want) {
t.Fatalf("history's agents %v, want %v", got, want)
}
for i := range want {
if got[i] != want[i] {
t.Fatalf("history's agents %v, want %v", got, want)
}
}
var ctx ctxJSON
getJSON(t, mux, "/api/context?days=1", &ctx)
cards := map[string]int{}
for _, a := range ctx.Agents {
cards[a.Agent] = a.Calls
}
if cards["kimi-code-cli"] != 0 || cards["kimi"] != 2 {
t.Fatalf("the Context tab's agent cards: %v, want kimi's two requests on one card", cards)
}
for _, s := range ctx.Sessions {
if s.Agent == "kimi-code-cli" {
t.Fatalf("a session under the raw name: %+v", s)
}
}
}