usage: a plan key's window is reckoned from the calls routed on that key (#3747)
A plan bought with an API key (Kimi Code, a GLM Coding Plan, MiniMax's
and the rest of planquota) tells its windows to the key, and when a
provider holds several keys each card names its key: the card's user is
the key's name (PlanQuotas). The ledger names a key's call by the key
alone, ProviderKeyName with no account, since accountOf answers "" for a
key (#557). windowHolds looked a window's calls up by provider and
account, so a key card looked for calls answering as "new" or "old",
found none, and said "not enough data" at 87% used (davisjiahao).
A call now answers as the account it went out as or, when it names no
account, as its key. The key was in the ledger all along, so calls
logged before this fall in too and nothing is rewritten. The same key
also places the call in that card's dry runs, which were keyed by the
card's user already. Every planquota provider's named-key cards go
through this one predicate; the ledger's other Account() readers (the
accounts view, filters, CSV, requests page) keep their meaning.
Not covered, as before: a lone key's card names no user and is never
reckoned (holdsBounds), an unnamed key beside others gets a masked-key
card the ledger can't name, and calls made before a key was renamed
stay under the old name. The lone key is filed as #4507.
Ported from gua2100's branch hunt91-l3-3747-key-card (deb1c0ba0) as
written, reviewed line by line.
Run at this head (rebased on 5c637f17b), on mote:
- go test -tags nogui -count=1 ./internal/usage ok (20.4s).
- go vet -tags nogui ./internal/usage clean on darwin, GOOS=linux and
GOOS=windows; gofmt -l clean.
- In review: -run TestWindowHolds ok with TZ=UTC and TZ=Asia/Shanghai.
Broken by hand: with window_holds.go put back to main's, the new
TestWindowHoldsReckonsAPlanKeysWindowByItsName fails with "new's 5
hours: <nil> (why "none"), want 2500 routed, whole 2874", the
reporter's symptom.
Not tried: a real Kimi Code key's card in the GUI; a full
go test ./....
Co-authored-by: ggbdpq <ggbdpq@gmail.com>
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user