xyenon (Discord #feedback, 10-29): Volcengine Ark lists
kimi-k3-1-pro-261024 where models.dev lists kimi-k3.1-pro, so magpie
knew neither its context window nor its reply limit. The ids differ only
in what the provider package's sameModel already folds when it groups
models: a version's dot written as "-" (or Fireworks' "p"), and Ark's
six-digit YYMMDD date.
Design
- sameModel moves to internal/catalog as SameModel, word for word
(snapshotDate, modelGaps and atoi2 with it). provider's sameModel calls
it, so grouping and catalog lookups fold ids by one rule.
- Exact matches go first and are unchanged: found (exact, Bedrock
profile, "(variant)", ":tag"), then renamed (relay prefixes, window
marks). Only when both miss does normalFor look up the id models.dev
spells as SameModel(id) folds it. When several spellings fold into one
key, the spelling most providers use wins, and then the lowest id, so
Venice's kimi-k3-1-pro keeps its own 1000000/131072 and the other
spelling gets the majority's 1048576/1048576.
- Decorate (a vendor's live list against the provider's own models.dev
entries) tries the same provider's entry under another spelling before
anything else. It fills only what the vendor's list leaves at zero and
never copies a price, so a limit the upstream states always wins.
ListedBy (a provider's own levels) gets the same last pass, per
provider, after its exact passes.
- What is not borrowed: a price (PricedBy has no such pass, so nothing is
priced from another provider's or another spelling's row) and a display
name (NameOf; the upstream id stays the id).
- What is not matched: -pro-max, -mini, another version (k3.2, k3), a
four-digit release (-2610), a six-digit number that isn't a date
(261324), and OpenAI's dashed date. SameModel already kept those apart.
- Re-derived: everything that reads these tables goes through
ContextOf/OutputOf/lookup, so ListedWindow, replyLimit, the Codex
catalog, efforts and thinks get the fallback with them. Knows now takes
other spellings too, so the usage report (stats modelLabel), which may
send only ids models.dev lists, reads the new KnowsAsWritten, which
doesn't.
- docs/subsystems/providers-accounts.md says all this.
Tests (internal/catalog/spelling_test.go, a fixture from models.dev's
rows for these ids, no network)
- TestOtherSpellingLimits: the report's literal ids
(kimi-k3-1-pro-261024, volcengine/…, Kimi-K3.1-Pro-20261024) and
twelve near misses; levels; Knows vs KnowsAsWritten; no name.
- TestOtherSpellingIsNotPriced: no provider prices the dated id, and the
plan's price isn't taken for pay-as-you-go's id.
- TestDecorateOtherSpelling: the vendor's 200000 stays, the reply limit,
levels and name are filled, no price; a -max id and kimi-k3 vs
kimi-k3.1-pro are kept apart; ListedBy likewise.
- TestSameModelReportIDs.
- stats_test: a dated id models.dev knows only under another spelling is
sent as "other".
Red on the old code (catalog.go and live.go reverted, KnowsAsWritten
stubbed): TestOtherSpellingLimits "ContextOf("kimi-k3-1-pro-261024") = 0,
want 1048576" and the rest; TestDecorateOtherSpelling Output 0, ListedBy
false.
Broken by hand
- The majority vote inverted: Venice's spelling wins, and the test asks
for 1048576 and gets 1000000/131072. Red.
- KnowsAsWritten taking other spellings: TestOtherSpellingLimits red.
- stats reading Knows: "deepseek/DeepSeek_V4-260101", want
"deepseek/other". Red.
Real catalog (models.dev cache on the WSL box, a throwaway test since
deleted): kimi-k3-1-pro-261024 gets 1048576/1048576 and levels
low,high,max, is not priced by volcengine, and isn't KnowsAsWritten;
kimi-k3-1-pro-max-261024 gets 0; Decorate of glm-5-3-flash-260828
against volcengine-coding-plan gets 1000000/131072 and the name
GLM-5.3-Flash with no price; claude-opus-5-5-20260801 gets
1000000/128000; gpt-4o-2024-08-06 is unchanged.
Run
- Full suite on the Linux box (wsl-run, Go 1.26.8) at 242e5c68d on base
be8b0f381: go vet -tags nogui ./..., go build -tags nogui ., go test
-tags nogui ./... -timeout 45m -count=1. 77 packages ok (catalog,
provider, stats, gateway and gui among them). One red package,
internal/agent: TestInstalledVersion, TestGrokInstalled and
TestOmpDetectedWithPiDir, the three install-detection reds #4033 and
#3548 already name for that box at main. They don't read the catalog.
- On mote at 242e5c68d: GOOS=windows go vet and darwin go vet of
catalog, provider and stats, and a GOOS=windows build of magpie. All ok.
- Rebased on origin/main 4d9058a98, whose Ark plan list change touches
none of these files: gofmt -l (clean), go vet, and go test -count=1 of
internal/catalog, internal/provider and internal/stats on the Linux
box. All ok.
Not tried: a request on a real Volcengine Ark account; the report's ids
come from xyenon's message and models.dev.
224 lines
8.4 KiB
Go
224 lines
8.4 KiB
Go
package stats
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/catalog"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
// One event a day, under the same id, with nothing but the id, the version
|
|
// and the system, and what magpie is used with by built-in ids; none once
|
|
// the user turns it off.
|
|
func TestSend(t *testing.T) {
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
t.Setenv("XDG_CACHE_HOME", t.TempDir())
|
|
t.Setenv("DO_NOT_TRACK", "")
|
|
t.Setenv("MAGPIE_NO_STATS", "")
|
|
usage = func() Usage {
|
|
return Usage{Agents: []string{"claude", "codex"}, Providers: []string{"deepseek", "custom"}, Models: []string{"deepseek/deepseek-v4"}, Groups: 2}
|
|
}
|
|
defer func() { usage = readUsage }()
|
|
var got [][]map[string]any
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
var b struct {
|
|
Key string `json:"api_key"`
|
|
Batch []map[string]any `json:"batch"`
|
|
}
|
|
json.NewDecoder(r.Body).Decode(&b)
|
|
if r.URL.Path != "/batch/" || b.Key != Key {
|
|
t.Errorf("sent %s %v", r.URL.Path, b)
|
|
}
|
|
got = append(got, b.Batch)
|
|
}))
|
|
defer srv.Close()
|
|
t.Setenv("MAGPIE_STATS_HOST", srv.URL)
|
|
day := time.Date(2026, 9, 28, 9, 0, 0, 0, time.UTC)
|
|
for _, at := range []time.Time{day, day.Add(3 * time.Hour), day.Add(24 * time.Hour)} {
|
|
if err := Send(context.Background(), "0.1.300", "app", at); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if len(got) != 2 {
|
|
t.Fatalf("%d sends for two days", len(got))
|
|
}
|
|
if got[0][0]["distinct_id"] != got[1][0]["distinct_id"] || len(got[0][0]["distinct_id"].(string)) != 32 {
|
|
t.Fatalf("ids: %v %v", got[0][0]["distinct_id"], got[1][0]["distinct_id"])
|
|
}
|
|
a := got[0][0]
|
|
p := a["properties"].(map[string]any)
|
|
if a["event"] != "magpie active" || p["version"] != "0.1.300" || p["kind"] != "app" || p["agents"] != 2.0 || p["providers"] != 2.0 || p["models"] != 1.0 || p["groups"] != 2.0 || len(p) != 10 {
|
|
t.Fatalf("active: %v", a)
|
|
}
|
|
var uses []string
|
|
for _, e := range got[0][1:] {
|
|
p := e["properties"].(map[string]any)
|
|
if e["event"] != "magpie uses" || e["distinct_id"] != a["distinct_id"] || p["$process_person_profile"] != false || len(p) != 4 {
|
|
t.Fatalf("use: %v", e)
|
|
}
|
|
uses = append(uses, p["kind"].(string)+" "+p["id"].(string))
|
|
}
|
|
if want := "agent claude,agent codex,provider deepseek,provider custom,model deepseek/deepseek-v4"; strings.Join(uses, ",") != want {
|
|
t.Fatalf("uses %q, want %q", uses, want)
|
|
}
|
|
|
|
// what it is used with turned off: the count of the user only
|
|
settings.Save(settings.Settings{NoUsageStats: true})
|
|
Send(context.Background(), "0.1.300", "app", day.Add(48*time.Hour))
|
|
if len(got) != 3 || len(got[2]) != 1 || len(got[2][0]["properties"].(map[string]any)) != 6 {
|
|
t.Fatalf("sent with usage off: %v", got[2:])
|
|
}
|
|
|
|
settings.Save(settings.Settings{NoStats: true})
|
|
Send(context.Background(), "0.1.300", "app", day.Add(72*time.Hour))
|
|
t.Setenv("MAGPIE_NO_STATS", "1")
|
|
settings.Save(settings.Settings{})
|
|
Send(context.Background(), "0.1.300", "app", day.Add(96*time.Hour))
|
|
if len(got) != 3 {
|
|
t.Fatalf("sent while off: %d", len(got))
|
|
}
|
|
}
|
|
|
|
// A provider is named by what magpie built in, never by what the user
|
|
// typed: a preset's id, a subscription's agent, a known plugin's id;
|
|
// anything else is "custom" or "plugin".
|
|
func TestProviderLabel(t *testing.T) {
|
|
for _, c := range []struct {
|
|
p provider.Provider
|
|
want string
|
|
}{
|
|
{provider.Provider{ID: "my-deepseek", Name: "My DeepSeek", Preset: "deepseek", Chat: "https://api.deepseek.com/v1"}, "deepseek"},
|
|
{provider.Provider{ID: "secret-relay", Name: "Secret Relay", Chat: "https://relay.example/v1"}, "custom"},
|
|
{provider.Provider{ID: "odd", Name: "Odd", Preset: "no-such-preset"}, "custom"},
|
|
{provider.Provider{ID: "codex", Account: &provider.Account{Agent: "codex", User: "someone@example.com"}}, "codex"},
|
|
} {
|
|
if got := providerLabel(c.p); got != c.want {
|
|
t.Errorf("%s: %q, want %q", c.p.ID, got, c.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Nothing the user named goes as an agent: an omp profile, a WSL distro.
|
|
func TestAgentLabel(t *testing.T) {
|
|
for id, want := range map[string]string{
|
|
"claude": "claude",
|
|
"claude@wsl:Ubuntu": "claude",
|
|
"omp#acme-secret": "omp",
|
|
"omp#acme-secret@wsl:Work": "omp",
|
|
} {
|
|
if got := agentLabel(id); got != want {
|
|
t.Errorf("%s: %q, want %q", id, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A model on a key's provider goes by its id only when models.dev knows
|
|
// it: one the user named (an Ollama model, a fine-tune naming their
|
|
// organisation, or a known one spelt their way) is "other".
|
|
func TestModelLabelKeepsTheUsersOwnModelIDs(t *testing.T) {
|
|
cache := map[string]any{"deepseek": map[string]any{"id": "deepseek", "models": map[string]any{
|
|
"deepseek-v4": map[string]any{"id": "deepseek-v4", "limit": map[string]any{"context": 128000}},
|
|
}}}
|
|
b, _ := json.Marshal(cache)
|
|
if err := os.MkdirAll(filepath.Dir(catalog.CachePath()), 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(catalog.CachePath(), b, 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
catalog.Reset()
|
|
t.Cleanup(func() { os.Remove(catalog.CachePath()); catalog.Reset() })
|
|
for _, p := range []provider.Provider{
|
|
{ID: "my-deepseek", Name: "DS", Preset: "deepseek", Chat: "https://api.deepseek.com/v1", Key: "sk-x", Models: []string{"deepseek-v4", "DeepSeek_V4-260101"}},
|
|
{ID: "my-openai", Name: "OA", Preset: "openai", Chat: "https://api.openai.com/v1", Key: "sk-y", Models: []string{"ft:gpt-4o:acme-corp::abc123"}},
|
|
{ID: "my-ollama", Name: "Local", Preset: "ollama", Chat: "http://127.0.0.1:11434/v1", Models: []string{"acme-internal-llm"}},
|
|
} {
|
|
if err := provider.Save(p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
for ref, want := range map[string]string{
|
|
"my-deepseek/deepseek-v4": "deepseek/deepseek-v4",
|
|
// known only under another spelling (catalog.SameModel): the
|
|
// user's own spelling isn't sent
|
|
"my-deepseek/DeepSeek_V4-260101": "deepseek/other",
|
|
"my-openai/ft:gpt-4o:acme-corp::abc123": "openai/other",
|
|
"my-ollama/acme-internal-llm": "ollama/other",
|
|
} {
|
|
if got := modelLabel(ref); got != want {
|
|
t.Errorf("%s: %q, want %q", ref, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// The partners' counts of the days ended go with the day's event, each as
|
|
// one "magpie partner" at its own day, and are sent once; with what magpie
|
|
// is used with turned off they don't go.
|
|
func TestSendPartnerCounts(t *testing.T) {
|
|
home := t.TempDir()
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
|
|
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
|
|
t.Setenv("DO_NOT_TRACK", "")
|
|
t.Setenv("MAGPIE_NO_STATS", "")
|
|
usage = func() Usage { return Usage{} }
|
|
defer func() { usage = readUsage }()
|
|
counts := `{"days":{"2026-09-27":{"acme":{"shown":12,"opened":2}},"2026-09-28":{"acme":{"shown":3}}}}`
|
|
write := func() {
|
|
f := filepath.Join(home, ".cache", "magpie", "partner-counts.json")
|
|
if err := os.MkdirAll(filepath.Dir(f), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(f, []byte(counts), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
write()
|
|
var got [][]map[string]any
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
var b struct {
|
|
Batch []map[string]any `json:"batch"`
|
|
}
|
|
json.NewDecoder(r.Body).Decode(&b)
|
|
got = append(got, b.Batch)
|
|
}))
|
|
defer srv.Close()
|
|
t.Setenv("MAGPIE_STATS_HOST", srv.URL)
|
|
day := time.Date(2026, 9, 28, 9, 0, 0, 0, time.UTC)
|
|
partners := func(batch []map[string]any) []string {
|
|
var out []string
|
|
for _, e := range batch {
|
|
if e["event"] != "magpie partner" {
|
|
continue
|
|
}
|
|
p := e["properties"].(map[string]any)
|
|
out = append(out, fmt.Sprintf("%s %s %s %v %v", e["timestamp"], p["id"], p["what"], p["count"], p["$process_person_profile"]))
|
|
}
|
|
return out
|
|
}
|
|
Send(context.Background(), "0.1.300", "app", day)
|
|
if want := "2026-09-27T12:00:00Z acme opened 2 false,2026-09-27T12:00:00Z acme shown 12 false"; strings.Join(partners(got[0]), ",") != want {
|
|
t.Fatalf("partner events %q, want %q", partners(got[0]), want)
|
|
}
|
|
Send(context.Background(), "0.1.300", "app", day.Add(24*time.Hour))
|
|
if want := "2026-09-28T12:00:00Z acme shown 3 false"; strings.Join(partners(got[1]), ",") != want {
|
|
t.Fatalf("the next day sent %q, want %q", partners(got[1]), want)
|
|
}
|
|
|
|
write()
|
|
settings.Save(settings.Settings{NoUsageStats: true})
|
|
Send(context.Background(), "0.1.300", "app", day.Add(48*time.Hour))
|
|
if len(got) != 3 || len(partners(got[2])) != 0 {
|
|
t.Fatalf("sent with usage off: %v", got[2:])
|
|
}
|
|
}
|