Files
magpie/internal/usage/session_price_test.go
T
ggbdpq d1b62f4604 usage: price a local session's call at the provider its file names
What changed

A call read from an agent's own session file (omp's model_usage, say) has
no billing provider of ours — its row reads session-unknown — so Pricer
priced it only through the maker lookups, which answer nothing for models
no vendor catalog lists as its own, even when the models.dev catalog
lists the model under the provider the file recorded (openrouter listing
typesafe/jev-1.14 at $0.042/M in, #4278). When the effective price is
unknown and the record is one of those, its session provider now prices
the call — but only what the catalog itself affirms: the very model id,
listed under that provider, with a price. A provider models.dev doesn't
know, a model it lists that provider without, and an entry with no price
stay unpriced, the CSV cost empty, rather than priced at another entry's
guess; the pricer's memo is keyed by the session provider too, so two
files naming different routes price apart.

Run

- go test -tags nogui -run 'TestUnknownProviderPricedBySessionProvider' ./internal/usage — red before (the named route stayed unpriced), green after
- go test -tags nogui ./internal/usage/... — ok
- gofmt -l internal/usage — clean

Generated-by: GLM-5.3-FlashX (ZCode)

Reviewed and applied from #4278 (ggbdpq, 645e80ab2). It differs from
the patch hashed on the issue only in its git-version trailer.

The reporter's own call stays unpriced for now. models.dev's default
api.json, which magpie fetches, does not list openrouter
typesafe/jev-1.14; only api.json?type=all does. #4278 stays open for
that. The Sessions page prices a session-unknown call through its own
priceOf, by model only: #4591.

Broken by hand: ledger.go back at main with the test kept, and
TestUnknownProviderPricedBySessionProvider fails.

Run (on mote, through mote-run):
- Full `go test -tags nogui -count=1 -timeout 45m ./...` with this stack
  on c30d3b7b6: 82 packages ok. internal/gui was red only on
  TestSettingsSaveKeepsWhatItDoesNotSend (#4514, SearchAll from
  b09349d17) and TestColdTurnsTaken (#4510). Both are filed, and neither
  touches this stack.
- Rebased onto 29d0ab39f, which changed internal/gateway since then:
  gofmt -l, go vet (darwin, GOOS=linux -tags nogui, GOOS=windows) and
  go test -count=1 ./internal/gateway ./internal/usage ./internal/provider
  ./internal/sessions all ok at the pushed head. go build ./... ok for
  darwin, linux (nogui) and windows.
- go test -race of the gateway tests this stack adds or touches, on
  the stack at fb29cd30f: ok.
2026-10-31 23:35:27 +08:00

120 lines
4.2 KiB
Go

package usage
import (
"encoding/csv"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/sessions"
)
// A local session's usage entry names the provider the call went through
// (omp's model_usage: provider openrouter, model typesafe/jev-1.14,
// #4278), which its billing provider — the unknown one — never says. That
// name prices only what the catalog itself lists under it: the very model
// id, at that provider, with a price. A provider the catalog doesn't know,
// a model it lists that provider without, and a listing with no price are
// all left unpriced rather than priced at another entry's guess.
func TestUnknownProviderPricedBySessionProvider(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
os.MkdirAll(filepath.Dir(catalog.CachePath()), 0o755)
os.WriteFile(catalog.CachePath(), []byte(`{`+
`"openrouter":{"id":"openrouter","models":{"typesafe/jev-1.14":{"id":"typesafe/jev-1.14","cost":{"input":0.042,"output":0}}}},`+
`"nano-gpt":{"id":"nano-gpt","models":{"typesafe/jev-1.13":{"id":"typesafe/jev-1.13","cost":{"input":0.042,"output":0}}}},`+
`"vivgrid":{"id":"vivgrid","models":{"typesafe/jev-1.15":{"id":"typesafe/jev-1.15"}}}}`), 0o644)
catalog.Reset()
t.Cleanup(catalog.Reset)
now := holdClock(t, time.Date(2026, 9, 30, 12, 0, 0, 0, time.Local))
old := LogCalls
LogCalls = func(time.Time) []sessions.Call {
return []sessions.Call{
// the reporter's: the file names a provider the catalog lists
// this very model under, at its input price
{Time: now.Add(-time.Hour), Agent: "omp", Session: "o1", Model: "typesafe/jev-1.14", Upstream: "openrouter",
Tokens: sessions.Tokens{Input: 100000, Output: 20000}},
// the file names a provider the catalog knows, but not this model of it
{Time: now.Add(-2 * time.Hour), Agent: "omp", Session: "o2", Model: "typesafe/jev-1.14", Upstream: "nano-gpt",
Tokens: sessions.Tokens{Input: 100000, Output: 20000}},
// the file names a provider the catalog doesn't know at all
{Time: now.Add(-3 * time.Hour), Agent: "omp", Session: "o3", Model: "typesafe/jev-1.14", Upstream: "home-relay",
Tokens: sessions.Tokens{Input: 100000, Output: 20000}},
// the catalog lists the model at that provider without a price
{Time: now.Add(-4 * time.Hour), Agent: "omp", Session: "o4", Model: "typesafe/jev-1.15", Upstream: "vivgrid",
Tokens: sessions.Tokens{Input: 100000, Output: 20000}},
}
}
t.Cleanup(func() { LogCalls = old })
rows, _, _ := Ledger(Month, Filter{})
if len(rows) != 4 {
t.Fatalf("rows %d", len(rows))
}
bySession := map[string]Row{}
for _, r := range rows {
if r.Source != "log" || r.Provider != UnknownProvider {
t.Fatalf("not a session-unknown log row: %+v", r.Record)
}
bySession[r.Session] = r
}
priced := bySession["o1"]
if priced.SessionProvider != "openrouter" {
t.Fatalf("the file's provider isn't the row's: %+v", priced.Record)
}
if !priced.Priced {
t.Fatalf("the named route stayed unpriced: %+v", priced.Record)
}
if want := (100000*0.042 + 20000*0) / 1e6; priced.Cost != want {
t.Fatalf("cost %v, want %v", priced.Cost, want)
}
for _, s := range []string{"o2", "o3", "o4"} {
if r := bySession[s]; r.Priced {
t.Fatalf("%s priced at a guess: %+v", s, r.Record)
}
}
// the CSV says the same: a cost where the catalog affirmed the pair,
// empty where it didn't (#4278's cost_usd)
var b strings.Builder
if err := WriteCSV(&b, rows); err != nil {
t.Fatal(err)
}
rec, err := csv.NewReader(strings.NewReader(b.String())).ReadAll()
if err != nil {
t.Fatal(err)
}
cost := -1
for i, h := range rec[0] {
if h == "cost_usd" {
cost = i
}
}
session := -1
for i, h := range rec[0] {
if h == "session" {
session = i
}
}
if cost < 0 || session < 0 {
t.Fatalf("no cost_usd or session column: %v", rec[0])
}
for _, r := range rec[1:] {
id, got := r[session], r[cost]
want := ""
if id == "o1" {
want = "0.004200"
}
if got != want {
t.Fatalf("%s cost_usd %q, want %q", id, got, want)
}
}
}