A found routing group is called by its public id (auto/<model>) and is kept as group/auto-<slug>; calls recorded under either were counted as two models, so Usage › Sessions' model filter listed the same group twice and the ledger, summaries and session lists split its calls. Ported from NorethSea's 30d6261cd on their fork: UsageAlias names a found group by its public id before the user's alias (both spellings, old session files too), sessions' lists, the ledger keyer, the summary cache's key and Codex's model/effort read through it, and a cache keyed on the group names (UsageNamesKey) is read again when a group is renamed. A user's own provider named "auto" keeps auto/<model> (autoRefsOn). Changed in review: the patch's ledger filter compared every record with provider.SameRef, which reads the providers (customIDs, a stat of providers.json) for each record a filter passes over. FilterOf, the one place a Filter is made from a query, now canonicalizes the model once, and keepsRecord asks the providers only for an auto/ record under a group/ filter (sameGroupModel). Run (mote's sshd reset every connection all afternoon, so on the Linux box through wsl-run; the package run at base720200092, and the -run tests, quota-holds and i18n-keys again at this head on 208b89422; rebased since onto44732325e, a gateway test only): - go test -count=1 -tags nogui ./internal/gui ./internal/provider ./internal/sessions ./internal/usage ./internal/usageapi: ok - go vet -tags nogui (linux), GOOS=darwin CGO_ENABLED=0 go vet (provider, usage, usageapi, sessions), and go vet on Windows (win-run, those and internal/gui): clean. gofmt -l: clean. - GUI: usage-ledger-model-pick, usage-ledger, quota-fit-batched, i18n-keys in chromium and webkit. usage-ledger's "newest live route needs no return-to-live button" (and zh "request routing links") is red the same on origin/main987eac0aaon that box: #3104, #4078. Broken by hand: - keepsRecord back to r.Model != f.Model: TestUsageFoundGroupNames and TestLedgerFoundGroupNames red. - usageGroupNames returning nothing: TestUsageFoundGroupNames (provider, usage), TestSessionsFoundGroupNames, TestCodexEffortFoundGroupNames, TestLedgerFoundGroupNames red. Not tried: a macOS run (mote unreachable), the darwin gui package's vet, and the reporter's own usage log. Co-authored-by: NorethSea <963979204@qq.com>
109 lines
3.6 KiB
Go
109 lines
3.6 KiB
Go
package gui
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/sessions"
|
|
"github.com/yetone/magpie/internal/usage"
|
|
)
|
|
|
|
// The model menu takes stats' model names, while the list and overview
|
|
// filter their own models. All three must agree for old and new group ids.
|
|
func TestSessionsAPIFoundGroupNames(t *testing.T) {
|
|
home := sandboxHome(t)
|
|
proj := filepath.Join(home, ".claude", "projects", "-work-auto")
|
|
t.Setenv("CLAUDE_CONFIG_DIR", filepath.Join(home, ".claude"))
|
|
if err := os.MkdirAll(proj, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, id := range []string{"ds-a", "ds-b"} {
|
|
if err := provider.Save(provider.Provider{ID: id, Name: id, Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"deepseek-flash"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
at := time.Now().Add(-time.Minute)
|
|
for i, model := range []string{"group/auto-deepseek-flash", "auto/deepseek-flash"} {
|
|
id := fmt.Sprintf("abcdef0%d-abcd-abcd-abcd-abcdefabcdef", i+1)
|
|
body := fmt.Sprintf(`{"type":"assistant","isSidechain":false,"message":{"model":%q,"id":%q,"role":"assistant","content":[],"usage":{"input_tokens":10,"output_tokens":2}},"timestamp":%q,"cwd":"/work/auto","sessionId":%q}`+"\n", model, id, at.Format(time.RFC3339Nano), id)
|
|
if err := os.WriteFile(filepath.Join(proj, id+".jsonl"), []byte(body), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
usage.Append(usage.Record{Time: at, Agent: "pi", Session: "gateway-auto", Provider: "relay", Model: "group/auto-deepseek-flash", Input: 10, Output: 2, Status: 200})
|
|
sessions.Reset()
|
|
forgetStats()
|
|
t.Cleanup(func() { sessions.Reset(); forgetStats() })
|
|
mux := http.NewServeMux()
|
|
sessionRoutes(mux, folderOnly{})
|
|
get := func(path, tag string) *httptest.ResponseRecorder {
|
|
t.Helper()
|
|
w := httptest.NewRecorder()
|
|
r := httptest.NewRequest("GET", path, nil)
|
|
r.Header.Set("X-Magpie-Had", tag)
|
|
mux.ServeHTTP(w, r)
|
|
if w.Code != 200 {
|
|
t.Fatalf("%s: %d %s", path, w.Code, w.Body)
|
|
}
|
|
return w
|
|
}
|
|
first := get("/api/sessions", "")
|
|
var list sessionsJSON
|
|
if err := json.Unmarshal(first.Body.Bytes(), &list); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(list.Sessions) != 3 {
|
|
t.Fatalf("list has %d sessions", len(list.Sessions))
|
|
}
|
|
for _, s := range list.Sessions {
|
|
if len(s.Models) != 1 || s.Models[0].Model != "auto/deepseek-flash" {
|
|
t.Fatalf("list's group names: %+v", s.Models)
|
|
}
|
|
}
|
|
var stats struct {
|
|
Days []sessions.Day `json:"days"`
|
|
}
|
|
if err := json.Unmarshal(get("/api/sessions/stats?days=0", "").Body.Bytes(), &stats); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
total := 0
|
|
for _, d := range stats.Days {
|
|
for _, u := range d.Usage {
|
|
if u.Model != "auto/deepseek-flash" {
|
|
t.Fatalf("duplicate model option: %q", u.Model)
|
|
}
|
|
total += u.Input
|
|
}
|
|
}
|
|
if total != 30 {
|
|
t.Fatalf("stats input %d, want 30", total)
|
|
}
|
|
for _, model := range []string{"auto/deepseek-flash", "group/auto-deepseek-flash"} {
|
|
var o sessions.Overview
|
|
if err := json.Unmarshal(get("/api/sessions/overview?days=0&model="+url.QueryEscape(model), "").Body.Bytes(), &o); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if o.Count != 3 {
|
|
t.Fatalf("%s filter lost sessions: %+v", model, o)
|
|
}
|
|
}
|
|
tag := first.Header().Get("X-Magpie-Tag")
|
|
if w := get("/api/sessions", tag); w.Header().Get("X-Magpie-Same") != "1" {
|
|
t.Fatal("unchanged names did not keep the list tag")
|
|
}
|
|
if err := provider.SetAutoGroups(false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if w := get("/api/sessions", tag); w.Header().Get("X-Magpie-Same") == "1" {
|
|
t.Fatal("group catalog changed but list kept old names")
|
|
}
|
|
}
|