From 85b45b4d7157fd2f14271dfe5b4706e5dbd18973 Mon Sep 17 00:00:00 2001 From: ggbdpq Date: Fri, 16 Oct 2026 13:40:30 +0800 Subject: [PATCH] usage: a window's dollar whole prices the model the reply named (#2488) The ledger prices a call whose own id has no price as the model the reply named (Served, #1498). A window's whole priced through apiPricer, which looked only at Record.Model, so one such call in a window had no API price, the window counted as unpriced and its dollar whole was left out while its tokens stayed. apiPricer now tries Served through the same chain (maker, the provider's list, Same as) when the asked id has none. The asked id keeps its own price when it has one; a price the user set or a rate still never enters the whole. Past cycles use the same apiPricer and take it too. Patch by ggbdpq (Discord gua2100), as posted in #2488. Review: the pricer is a closure built once per query with its own cache, keyed now on (provider, model, served); no shared or stateful call added. The only conflict on current main was the test file, where the Antigravity pools test (TestWindowHoldsReckonsAntigravityPools) landed in the same spot; both tests are kept. Run (sandbox HOME, at this tree on origin/main 02cbdf5bd): - go test -tags nogui -count=1 ./internal/usage/: ok - Broken by hand: window_holds.go put back to origin/main, test kept: TestHoldsPriceTheModelTheReplyNamed FAIL "the model the reply named wasn't its API price: ". Restored: ok. - gofmt -l internal/usage: clean. go vet ./internal/usage/ on darwin, GOOS=windows, GOOS=linux: ok. Not tried: the GUI's Usage page with a real relay account whose reply names another model; go test ./... (one suite per machine). --- docs/subsystems/providers-accounts.md | 3 +- internal/usage/window_holds.go | 42 +++++++++++++++++---------- internal/usage/window_holds_test.go | 35 ++++++++++++++++++++++ 3 files changed, 64 insertions(+), 16 deletions(-) diff --git a/docs/subsystems/providers-accounts.md b/docs/subsystems/providers-accounts.md index 056f8d537..f3f83116e 100644 --- a/docs/subsystems/providers-accounts.md +++ b/docs/subsystems/providers-accounts.md @@ -205,7 +205,8 @@ the range, with that reading's percentage and bounds, the cached card's model scope, and only calls and readings before that reading. A card that is gated live (a balance, a remote, an error) is gated for past cycles too, and a window no longer on the cached card is left out. Past prices aren't kept, so -the cost is at today's API list prices. See +the cost is at today's API list prices. A call whose own id has no API price +is costed as the model the reply named (Served), as the ledger is (#1498). See `TestWindowHoldsDryAgainAfterReset`, `TestWindowHoldsUsesObservedRefillStart` and `TestHistoricalWindowHoldsUsesEachWindowsOwnReading`. diff --git a/internal/usage/window_holds.go b/internal/usage/window_holds.go index 532e163dd..ea5b68d47 100644 --- a/internal/usage/window_holds.go +++ b/internal/usage/window_holds.go @@ -298,29 +298,41 @@ func windowHolds(qs []provider.SubscriptionQuota, now, first time.Time, priceOf // apiPricer is each call's model at its API list price: its maker's, else // the provider's own list, else the maker's of the model the user said it // is the same as (provider.SamePriceIn), never a price the user set for the -// subscription (which may well be nothing) nor a rate it bills at. A price -// of nothing at all is no API price. +// subscription (which may well be nothing) nor a rate it bills at. A model +// none of those prices is priced as the model the reply named (Served), as +// the ledger is (#1498). A price of nothing at all is no API price. func apiPricer() func(Record) *catalog.Price { st := settings.Load() - prices := map[[2]string]*catalog.Price{} - return func(r Record) *catalog.Price { - k := [2]string{r.Provider, r.Model} - if pr, ok := prices[k]; ok { - return pr - } - var pr *catalog.Price - v, ok := provider.MakerPrice(r.Model) + prices := map[[3]string]*catalog.Price{} + one := func(providerID, model string) *catalog.Price { + v, ok := provider.MakerPrice(model) if !ok || v.Cost(1e6, 1e6, 0, 0) == 0 { ok = false - if p, err := provider.Find(r.Provider); err == nil { - v, ok = p.ListPrice(r.Model) + if p, err := provider.Find(providerID); err == nil { + v, ok = p.ListPrice(model) } } if !ok || v.Cost(1e6, 1e6, 0, 0) == 0 { // the model the user said it is (#2344) - v, ok = provider.SamePriceIn(st, r.Provider, r.Model) + v, ok = provider.SamePriceIn(st, providerID, model) } - if ok && v.Cost(1e6, 1e6, 0, 0) > 0 { - pr = &v + if !ok || v.Cost(1e6, 1e6, 0, 0) == 0 { + return nil + } + pr := v + return &pr + } + return func(r Record) *catalog.Price { + served := strings.TrimSpace(r.Served) + if served == r.Model { + served = "" + } + k := [3]string{r.Provider, r.Model, served} + if pr, ok := prices[k]; ok { + return pr + } + pr := one(r.Provider, r.Model) + if pr == nil && served != "" { + pr = one(r.Provider, served) } prices[k] = pr return pr diff --git a/internal/usage/window_holds_test.go b/internal/usage/window_holds_test.go index ad4d6de8b..0baba2ad9 100644 --- a/internal/usage/window_holds_test.go +++ b/internal/usage/window_holds_test.go @@ -457,3 +457,38 @@ func TestWindowHoldsReckonsAntigravityPools(t *testing.T) { } } } + +// The ledger prices a call whose own id has no price as the model the +// reply named (Served, #1498). A window's whole is priced the same way: +// one such call otherwise has no API price, and that drops the dollar +// whole for every other call in the window. +func TestHoldsPriceTheModelTheReplyNamed(t *testing.T) { + holdsHome(t) + priceOf := apiPricer() + named := Record{Provider: "relay", Model: "moonshot-kimi-k3", Served: "gpt-5.5"} + if pr := priceOf(named); pr == nil || pr.Input != 1.25 { + t.Fatalf("the model the reply named wasn't its API price: %+v", pr) + } + same := named + same.Served = named.Model + if pr := priceOf(same); pr != nil { + t.Fatalf("a reply that names the same unpriced id was priced: %+v", pr) + } + own := Record{Provider: "relay", Model: "claude-opus-4-7", Served: "gpt-5.5"} + if pr := priceOf(own); pr == nil || pr.Input != 5 { + t.Fatalf("the asked model's own price lost: %+v", pr) + } + + now := holdClock(t, time.Date(2026, 10, 8, 12, 0, 0, 0, time.UTC)) + reset := now.Add(2 * time.Hour) + Append(Record{Time: now.Add(-8 * 24 * time.Hour), Agent: "codex", Provider: "codex", ProviderAccount: "sam@example.com", + Model: "gpt-5.5", Input: 1000, Status: 200}) + Append(Record{Time: now.Add(-time.Hour), Agent: "codex", Provider: "codex", ProviderAccount: "sam@example.com", + Model: "moonshot-kimi-k3", Served: "gpt-5.5", Input: 1000, Status: 200}) + qs := WithWindowHolds([]provider.SubscriptionQuota{{Provider: "codex", User: "sam@example.com", Windows: []provider.QuotaWindow{ + {Name: "5 hours", Used: 10, ResetsAt: &reset, Span: 5 * time.Hour}, + }}}, now) + if h := qs[0].Windows[0].Holds; h == nil || !h.Priced || h.Cost <= 0 { + t.Fatalf("5 hours: %+v, a priced reply left the dollar whole out", h) + } +}