Files
magpie/internal/gui/trace_publicref_test.go
T
yetone 0eb5cbe81a routing: the live view and Usage's Requested name a found group auto/<model>, not group/auto-<model> (未寒 on Discord)
Reported: 未寒 picked auto/claude-opus-5-5, and the Routing live view and
Usage's Requested column named it group/auto-claude-opus-5-5, an internal
id that agents never see.

Changed:
  - internal/gateway/trace.go: GroupRef and SubGroup carry Ref
    (Group.Ref(), the id agents pick the group by).
  - internal/gui/trace.go: publicRoute rewrites a route's Model,
    For.Model, Subagent.Asked/To and the group's and subs' Ref through
    provider.PublicRef (memoised per answer). It copies the pointers
    before writing, so the gateway's trace is left as it was.
  - internal/gui/context.go: the Context page's model too.
  - internal/usageapi/usageapi.go: the ledger page's Requested.
  - routing/client.js: useName falls back to a sub's ref.

Test: TestRoutesNameFoundGroupsByTheirPublicID
(internal/gui/trace_publicref_test.go). Broken by hand, each one red:
  - publicRoute made a no-op: red on model, group, subagent, For, Subs
    and route.
  - usageapi mapping removed: `the ledger's Requested:
    ["group/auto-claude-opus-5-5" "group/mine"]`.
  - contextRoutes mapping removed: `the Context page's model:
    "group/auto-claude-opus-5-5:high"`.
  - Ref dropped from gateway/trace.go alone stays green, as expected:
    publicRoute fills an empty Ref.
Not covered: the ledger's text search (usage.Filter.Query) still matches
the stored group/auto-… form, not the shown auto/… (package usage can't
import provider). client.js's ref fallback has no GUI test of its own.

Run, go1.26.8 darwin/arm64, sandbox HOME, private GOCACHE:
  At this stack on origin/main df6bd3efe (head 48522d851):
    gofmt -l: nothing
    go vet ./... (darwin), GOOS=linux go vet -tags nogui ./...,
      GOOS=windows go vet ./...: ok
    set -o pipefail; go test -timeout 45m ./...: ok, EXIT=0
      (68 ok, 19 with no test files; agent 528s, gateway 628s,
      library 482s)
    GUI, playwright 1.62.0, chromium and webkit, concurrency 3: the
      171 files near this change (routing*, panel-routing*,
      window-mask, privacy*, usage*, quota*, providers*): 2217 tests,
      2183 pass, 34 fail. Every red is also red alone on origin/main:
      quota-count-wrap and quota-name-translated (#2228),
      usage-agents-hide (#2276), usage-checkin (#3091), usage-fold
      (#2163), usage-ledger (#3104, red since df6bd3efe, green at its
      parent). window-mask: 20 of 20.
  At this stack on origin/main c922225f1 (head 91baa7adb), before
  df6bd3efe's routing trace change:
    set -o pipefail; go test -timeout 45m ./...: ok, EXIT=0
      (66 ok, 20 with no test files)
    the whole GUI suite (823 files, both engines, concurrency 3):
      11589 tests, 11554 pass, 35 fail. The 9 files red were run again
      alone at --test-concurrency=1 on the head and on origin/main
      a8ecf4020. The same 7 files were red on both: the five issues
      above plus s3-sync (#2448) and library-row-remove (#3099).
      nav-sub-unroll (#1950) and plugin-updates (#3103) were red only
      under the full run. codex-predictions (#2624) was 20 of 20.
2026-10-21 08:40:49 +08:00

138 lines
5.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package gui
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"slices"
"testing"
"time"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/gateway"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/usage"
)
// The Routing page names a group magpie found as agents pick it,
// auto/<model>, wherever a route names one: the model the agent asked
// for, the group and the groups in it, the request a search was for and a
// subagent's pick (未寒 on Discord: the live view still said
// group/auto-<model> after the rename). The gateway's trace and history
// keep group/auto-<slug>; a group the user made stays group/<id>.
func TestRoutesNameFoundGroupsByTheirPublicID(t *testing.T) {
sandboxHome(t)
old := served.Swap(nil)
t.Cleanup(func() { served.Store(old) })
if err := os.MkdirAll(filepath.Dir(catalog.CachePath()), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(catalog.CachePath(), []byte(`{"anthropic": {"models": {
"claude-opus-5.5": {"id":"claude-opus-5.5","name":"Claude Opus 5.5","limit":{"context":400000,"output":96000}}}}}`), 0o644); err != nil {
t.Fatal(err)
}
catalog.Reset()
t.Cleanup(catalog.Reset)
for id, m := range map[string]string{"a": "claude-opus-5-5", "b": "claude-opus-5.5"} {
if err := provider.Save(provider.Provider{ID: id, Name: id, Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{m, "x"}}); err != nil {
t.Fatal(err)
}
}
if err := provider.SaveGroup(provider.Group{Name: "Mine", Members: []string{"a/x", "b/x"}}); err != nil {
t.Fatal(err)
}
if err := os.MkdirAll(gateway.HistoryDir(), 0o700); err != nil {
t.Fatal(err)
}
// as the gateway wrote them, before the trace kept a group's ref
lines := `{"id":1,"model":"group/auto-claude-opus-5-5:high","provider":"a","done":true,` +
`"group":{"id":"auto-claude-opus-5-5","name":"Claude Opus 5.5","routing":"","affinity":"","members":["a/claude-opus-5-5"]},` +
`"subagent":{"asked":"group/auto-claude-opus-5-5","to":"group/mine"}}` + "\n" +
`{"id":2,"model":"group/mine","provider":"a","done":true,"kind":"web_search","for":{"agent":"claude","model":"group/auto-claude-opus-5-5"},` +
`"group":{"id":"mine","name":"Mine","routing":"","affinity":"","members":["a/x"],"subs":[{"id":"auto-claude-opus-5-5","name":"Claude Opus 5.5","routing":"","in":"mine"}]}}` + "\n"
if err := os.WriteFile(filepath.Join(gateway.HistoryDir(), "2026-10-20.jsonl"), []byte(lines), 0o600); err != nil {
t.Fatal(err)
}
mux := http.NewServeMux()
traceRoutes(mux)
w := httptest.NewRecorder()
mux.ServeHTTP(w, httptest.NewRequest("GET", "/api/gateway/history?day=2026-10-20", nil))
var got struct {
Routes []gateway.Route `json:"routes"`
}
if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil || len(got.Routes) != 2 {
t.Fatalf("history: %v %s", err, w.Body)
}
byID := map[int64]gateway.Route{}
for _, r := range got.Routes {
byID[r.ID] = r
}
one, two := byID[1], byID[2]
if one.Model != "auto/claude-opus-5.5:high" {
t.Errorf("the model asked for: %q", one.Model)
}
if one.Group == nil || one.Group.Ref != "auto/claude-opus-5.5" || one.Group.ID != "auto-claude-opus-5-5" {
t.Errorf("the group: %+v", one.Group)
}
if s := one.Subagent; s == nil || s.Asked != "auto/claude-opus-5.5" || s.To != "group/mine" {
t.Errorf("the subagent's pick: %+v", s)
}
if two.Model != "group/mine" || two.Group == nil || two.Group.Ref != "group/mine" {
t.Errorf("the user's group: %q %+v", two.Model, two.Group)
}
if two.For == nil || two.For.Model != "auto/claude-opus-5.5" {
t.Errorf("what the search was for: %+v", two.For)
}
if two.Group == nil || len(two.Group.Subs) != 1 || two.Group.Subs[0].Ref != "auto/claude-opus-5.5" {
t.Errorf("the group in the group: %+v", two.Group)
}
// the request's own route, as the panel's link opens it
w = httptest.NewRecorder()
mux.ServeHTTP(w, httptest.NewRequest("GET", "/api/gateway/route?id=1&day=2026-10-20", nil))
var r gateway.Route
if err := json.Unmarshal(w.Body.Bytes(), &r); err != nil || r.Model != "auto/claude-opus-5.5:high" {
t.Errorf("one route: %q %v", r.Model, err)
}
// the Context page's sessions and agents, of the same routes (today's)
h := filepath.Join(gateway.HistoryDir(), time.Now().Format("2006-01-02")+".jsonl")
if err := os.WriteFile(h, []byte(lines), 0o600); err != nil {
t.Fatal(err)
}
found := false
for _, r := range contextRoutes(httptest.NewRequest("GET", "/api/context", nil), 1) {
if r.ID == 1 {
found = true
if r.Model != "auto/claude-opus-5.5:high" {
t.Errorf("the Context page's model: %q", r.Model)
}
}
}
if !found {
t.Error("the Context page has no route 1")
}
// Usage › Requests' Requested column, which the ledger keeps the same way
for _, req := range []string{"group/auto-claude-opus-5-5", "group/mine"} {
usage.Append(usage.Record{Time: time.Now(), Provider: "a", Model: "claude-opus-5-5", Requested: req, Status: 200, Input: 10})
}
usageRoutes(mux, folderOnly{})
w = httptest.NewRecorder()
mux.ServeHTTP(w, httptest.NewRequest("GET", "/api/usage/requests?period=all", nil))
var ledger ledgerJSON
if err := json.Unmarshal(w.Body.Bytes(), &ledger); err != nil {
t.Fatal(err, w.Body)
}
var asked []string
for _, row := range ledger.Rows {
asked = append(asked, row.Requested)
}
slices.Sort(asked)
if !slices.Equal(asked, []string{"auto/claude-opus-5.5", "group/mine"}) {
t.Errorf("the ledger's Requested: %q", asked)
}
}