Files
magpie/internal/usage/usage_alias_test.go
T
yetone ea9454d2d8 usage: count two ids of one model as one, magpie model alias (SlouchyQuill507 on Discord)
A vendor names one model two ways (deepseek-v4.1-flash and deepseek-flash),
and Usage, Usage > Sessions and `magpie usage` showed two rows. Now:

  magpie model alias deepseek-v4.1-flash deepseek-flash
  magpie model alias deepseek-v4.1-flash            (what it is counted as)
  magpie model alias deepseek-v4.1-flash --reset
  magpie model aliases

kept as settings.json "usageAliases" {from: to}. Off until one is written.
Ids match as routing groups match them (provider.sameModel: vendor/ prefix,
case, "_", snapshot date), at every provider; a chain is followed to its
end; an alias to itself, a loop, or a flag as an id is refused. A loop
written into the file by hand counts each model under its own id.

Display only: requests, agents' model lists, /v1/models, routing groups,
Same as and prices never read it. Each call is priced by the id it was
made under and only then counted under the alias; the alias's own price
is never put on the other id's calls, and a merged row is priced only
when every id in it is. Applied in usage summarize/direct (Models
groups), the gateway sessions projection (alias read before
gatewayCache's lock; the cache key hashes usageAliases), and
sessions.List/Get/Find/Stats (read before storeMu/mu, merged into new
slices so the memo is untouched; ModelEffort rows merged too). A failed
settings.json read keeps the last aliases read, and SetUsageAlias refuses
to write over an unreadable file (EditUsageAliases under fileMu). The GUI
settings save carries usageAliases. No GUI control.

Not aliased: the Requests ledger's per-request model and its model
filter, which show each call's own id.

Run (worktree at f4640c0ae, rebased onto 2b4c0664e with no overlap):
- gofmt -l: clean. go vet ./... darwin, GOOS=linux -tags nogui, GOOS=windows: ok.
- go test -count=1 -timeout 45m (sandbox HOME) internal/usage,
  sessions, provider, cli, settings, gui, tui: all ok.
- Real binary in a sandbox HOME: two usage.jsonl calls priced $2 and
  $100 per M: `magpie usage` showed two model rows ($2.00, $100), and
  after `magpie model alias` one row, 2.0M, 2 calls, $102; the session
  row too. alias/aliases/--reset/loop/self output checked by hand.

Broken by hand, each red then restored:
- no alias in summarizeFrom: TestUsageAlias* red
- nil alias into the gateway projection: TestUsageAlias* red
- statsIn without aliasStats / List without aliasSession:
  TestSessionsCountAliasedModelsAsOne red
- merged usage row keeps Priced true: TestAliasStatsLeavesItsInputAlone red
- loop check off / self check by == / last-good read off / flag check off:
  TestUsageAliasRefusesSelfAndLoops, TestUsageAliasUnreadSettings,
  TestModelAliasCmd red
- GUI save drops UsageAliases: TestSettingsSaveKeepsWhatItDoesNotSend red

Not tried: the GUI pages in a browser (no app.js change; the Usage and
Sessions pages read the same rows), the full go test ./... before push,
Windows box.
2026-10-24 02:48:50 +08:00

99 lines
3.9 KiB
Go

package usage
import (
"math"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/settings"
)
// A model counted under another id (magpie model alias) is one row in the
// usage summary and in a gateway session, and each call keeps the price of
// the id it was made under: the alias's own price is never put on the
// other id's calls (SlouchyQuill507 on Discord: deepseek-v4.1-flash and
// deepseek-flash).
func TestUsageAliasCountsTwoIdsAsOne(t *testing.T) {
resetUsageTest(t)
cfg := settings.Load()
cfg.ModelPrices = map[string]settings.ModelPrice{
"*/deepseek-v4.1-flash": {Input: new(2.0), Output: new(0.0), CacheRead: new(0.0), CacheWrite: new(0.0)},
"*/deepseek-flash": {Input: new(100.0), Output: new(0.0), CacheRead: new(0.0), CacheWrite: new(0.0)},
}
if err := settings.Save(cfg); err != nil {
t.Fatal(err)
}
at := time.Now().Add(-time.Minute)
Append(Record{Time: at, Agent: "pi", Session: "s", Provider: "p", Model: "deepseek-v4.1-flash", Input: 1000000, Status: 200})
Append(Record{Time: at, Agent: "pi", Session: "s", Provider: "p", Model: "deepseek-flash", Input: 1000000, Status: 200})
// off by default: two rows, as before
if s := Summarize(All); len(s.Models) != 2 {
t.Fatalf("with no alias: %+v", s.Models)
}
if got := GatewaySessions(time.Time{}, nil); len(got) != 1 || len(got[0].Models) != 2 {
t.Fatalf("with no alias: %+v", got)
}
if _, err := provider.SetUsageAlias("deepseek-v4.1-flash", "deepseek-flash"); err != nil {
t.Fatal(err)
}
s := Summarize(All)
if len(s.Models) != 1 || s.Models[0].ID != "p/deepseek-flash" || s.Models[0].Model != "deepseek-flash" || s.Models[0].Input != 2000000 {
t.Fatalf("aliased summary: %+v", s.Models)
}
// $2 for the v4.1 call at its own price, $100 for the other: not $200
if c := s.Models[0].Cost; math.Abs(c-102) > 1e-9 || math.Abs(s.Cost-102) > 1e-9 {
t.Fatalf("aliased cost %v (total %v), want 102: each call at its own id's price", c, s.Cost)
}
got := GatewaySessions(time.Time{}, nil)
if len(got) != 1 || len(got[0].Models) != 1 || got[0].Models[0].Model != "deepseek-flash" || got[0].Models[0].Input != 2000000 {
t.Fatalf("aliased session: %+v", got)
}
if m := got[0].Models[0]; !m.Priced || math.Abs(m.Cost-102) > 1e-9 {
t.Fatalf("aliased session cost: %+v", m)
}
if _, days := GatewaySessionWindow(time.Time{}); len(days) != 1 || days[0].Model != "deepseek-flash" || math.Abs(days[0].Cost-102) > 1e-9 {
t.Fatalf("aliased days: %+v", days)
}
// --reset: two rows again
if _, err := provider.SetUsageAlias("deepseek-v4.1-flash", ""); err != nil {
t.Fatal(err)
}
if s := Summarize(All); len(s.Models) != 2 {
t.Fatalf("after reset: %+v", s.Models)
}
}
// An unpriced id counted under a priced one leaves the merged row unpriced:
// the alias's price is not borrowed for it.
func TestUsageAliasKeepsUnpricedCallsUnpriced(t *testing.T) {
resetUsageTest(t)
cfg := settings.Load()
cfg.ModelPrices = map[string]settings.ModelPrice{
"*/known-model": {Input: new(5.0), Output: new(0.0), CacheRead: new(0.0), CacheWrite: new(0.0)},
}
if err := settings.Save(cfg); err != nil {
t.Fatal(err)
}
if _, err := provider.SetUsageAlias("zz-unknown-model-x", "known-model"); err != nil {
t.Fatal(err)
}
at := time.Now().Add(-time.Minute)
Append(Record{Time: at, Agent: "pi", Session: "s", Provider: "p", Model: "zz-unknown-model-x", Input: 1000000, Status: 200})
Append(Record{Time: at, Agent: "pi", Session: "s", Provider: "p", Model: "known-model", Input: 1000000, Status: 200})
got := GatewaySessions(time.Time{}, nil)
if len(got) != 1 || len(got[0].Models) != 1 {
t.Fatalf("session: %+v", got)
}
if m := got[0].Models[0]; m.Priced || math.Abs(m.Cost-5) > 1e-9 {
t.Fatalf("merged row %+v: want unpriced, $5 from the priced id only", m)
}
s := Summarize(All)
if len(s.Models) != 1 || math.Abs(s.Models[0].Cost-5) > 1e-9 || s.Models[0].Unpriced != 1 {
t.Fatalf("summary: %+v", s.Models)
}
}