diff --git a/internal/usage/window_holds.go b/internal/usage/window_holds.go index 84152cae8..a1aaaf1e6 100644 --- a/internal/usage/window_holds.go +++ b/internal/usage/window_holds.go @@ -223,7 +223,14 @@ func windowHolds(qs []provider.SubscriptionQuota, now, first time.Time, priceOf } // who answered, not who a merge shows it under (Renames.Own) r.Provider = renamed.Own(r.Provider, r.Time) - acct := r.Provider + "\x00" + strings.ToLower(r.Account()) + // the call answers as the account it went out as, or, when it went + // on a key (which names no account, #557), as the key: a plan's + // card is on the key, named as the ledger names it (#3747) + who := r.Account() + if who == "" { + who = r.ProviderKeyName + } + acct := r.Provider + "\x00" + strings.ToLower(who) var runs []runKey for i, d := range dry[acct] { if !d.holds(r.Time) { diff --git a/internal/usage/window_holds_test.go b/internal/usage/window_holds_test.go index 0baba2ad9..a57ecb92c 100644 --- a/internal/usage/window_holds_test.go +++ b/internal/usage/window_holds_test.go @@ -175,6 +175,56 @@ func TestWindowHoldsNeedsTheWholeWindowLogged(t *testing.T) { } } +// A plan key's calls answer to the key, not to an account: the ledger +// names the key (ProviderKeyName) and no account (#557), and the card a +// plan key is on names the key too (#3747) — so a window is reckoned from +// the calls routed on that key, including calls logged before the card +// could tell a whole (the key was named all along; nothing is backfilled). +func TestWindowHoldsReckonsAPlanKeysWindowByItsName(t *testing.T) { + now := time.Date(2026, 10, 26, 12, 0, 0, 0, time.UTC) + five, week := now.Add(3*time.Hour), now.Add(3*24*time.Hour) + qs := []provider.SubscriptionQuota{ + {Provider: "kimi-code", User: "new", Windows: []provider.QuotaWindow{ + {Name: "5 hours", Used: 87, ResetsAt: &five, Span: 5 * time.Hour}, + }}, + {Provider: "kimi-code", User: "old", Windows: []provider.QuotaWindow{ + {Name: "5 hours", Used: 74, ResetsAt: &five, Span: 5 * time.Hour}, + {Name: "7 days", Used: 18, ResetsAt: &week, Span: 7 * 24 * time.Hour}, + }}, + } + call := func(ago time.Duration, key string, in, out int) Record { + return Record{Time: now.Add(-ago), Agent: "claude-code", Provider: "kimi-code", ProviderKeyName: key, Model: "kimi-k2", Input: in, Output: out, Status: 200} + } + recs := []Record{ + call(time.Hour, "new", 1500, 1000), // in the 5 hours of new + call(3*time.Hour, "new", 9000, 1000), // before the 5 hours began: not its whole's calls + call(2*time.Hour, "old", 2000, 2000), // in the 5 hours of old + call(2*24*time.Hour, "old", 1000, 500), // in the week of old, and the 5 hours' call in it too + call(time.Hour, "third", 5000, 1000), // a key no card names: none of the cards' + } + read := func(fn func(Record)) { + for _, r := range recs { + fn(r) + } + } + got := windowHolds(qs, now, now.Add(-30*24*time.Hour), func(Record) *catalog.Price { return nil }, provider.Renames{}, nil, read) + newHolds, oldFive, oldWeek := got[0].Windows[0], got[1].Windows[0], got[1].Windows[1] + if h := newHolds.Holds; h == nil || h.Routed.Tokens != 2500 || h.Tokens != 2874 { + t.Fatalf("new's 5 hours: %+v (why %q), want 2500 routed, whole 2874", newHolds.Holds, newHolds.HoldsWhy) + } + if h := oldFive.Holds; h == nil || h.Routed.Tokens != 4000 || h.Tokens != 5405 { + t.Fatalf("old's 5 hours: %+v (why %q), want 4000 routed, whole 5405", oldFive.Holds, oldFive.HoldsWhy) + } + if h := oldWeek.Holds; h == nil || h.Routed.Tokens != 5500 || h.Tokens != 30556 { + t.Fatalf("old's week: %+v (why %q), want 5500 routed, whole 30556", oldWeek.Holds, oldWeek.HoldsWhy) + } + for _, w := range []*provider.QuotaWindow{&newHolds, &oldFive, &oldWeek} { + if w.HoldsWhy != "" { + t.Errorf("%s: reckoned, and why %q", w.Name, w.HoldsWhy) + } + } +} + // A month's window began on the same day a month before its reset, not 30 // days before: Kiro's credits reset on the 1st. func TestWindowBoundsOfAMonth(t *testing.T) {