usage: an account's reading cut short keeps the account's name, so it joins its provider's card (#5310)

ox01024 on #5310, with 1,440 Antigravity accounts: 768 of the readings
/api/usage/quotas returned had windows and asOf but no user, and the
Usage page drew each of those as an Antigravity card of its own (459
cards and 34,355 elements against 1 card and 8,276 once named, in
their Chromium check; renderQuotas 602-875ms against 231ms).

loginReading made a reading cut short (at its deadline, not asked, left
alone) as a SubscriptionQuota with no User and passed the account to
keepLast beside it. keepLast found the kept windows by that account
and put q.User on them, which was empty. Every account path through
loginReading now names its reading (cutShort, namedFor); keepLast is
unchanged, since keys' balances are kept through it under a tag whose
card has no User (TestRefreshUsageKeepsNewerKeyReading and
TestPlanQuotas went red when keepLast named every reading). Gemini and
Antigravity's fetches on the Usage page name each reading by its
account too, as perLogin does for the others.

Test: TestAccountReadingNamed, for antigravity and claude: a read given
up on at its deadline and one not asked come back with the kept
windows under the account's name; an account with nothing kept says
its error under its name; a reading made with a name keeps it.

Run (mote): go test ./internal/provider -count=1 -timeout 45m ok;
go test ./internal/gui ./internal/usage ok; GOOS=linux and
GOOS=windows go vet ./internal/provider ok; gofmt clean.

Broken by hand: namedFor returning the reading as it came is red,
"antigravity deadline, kept: {... User: ...}".

Not tried: a real account set of that size; the reporter's other two
leads (googleLoginQuota listing every account again per account, and
keepLast writing the whole cache on each good read) are not measured
here.
This commit is contained in:
yetone
2026-11-13 12:31:46 +08:00
parent 72facf72e2
commit 5eac086aec
3 changed files with 78 additions and 5 deletions
+22 -4
View File
@@ -125,7 +125,7 @@ func LoginUsageShown(ctx context.Context, agent string) map[string]SubscriptionQ
func keptLoginUsage(logins []Login) map[string]SubscriptionQuota {
out := map[string]SubscriptionQuota{}
for _, l := range logins {
out[l.User] = keepLast(SubscriptionQuota{Provider: loginProvider(l), Plan: l.Plan, Windows: []QuotaWindow{}, Error: errLeftAlone.Error()}, l.User)
out[l.User] = keepLast(cutShort(l, errLeftAlone), l.User)
}
return out
}
@@ -299,7 +299,7 @@ func cachedLoginReading(ctx context.Context, l Login) loginUsageEntry {
// nothing read since magpie started: the reading kept on disk,
// as of when it was made
c.Unlock()
return entry(keepLast(SubscriptionQuota{Provider: loginProvider(l), Plan: l.Plan, Windows: []QuotaWindow{}, Error: errNotAsked.Error()}, l.User))
return entry(keepLast(cutShort(l, errNotAsked), l.User))
}
if r == nil {
if stale {
@@ -317,7 +317,7 @@ func cachedLoginReading(ctx context.Context, l Login) loginUsageEntry {
// it is bounded as the Usage page's refresh is
rctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), subscriptionTimeout)
defer cancel()
r.e = entry(keepReading(rctx, readNow(loginQuota(rctx, l)), l.User))
r.e = entry(keepReading(rctx, readNow(namedFor(l, loginQuota(rctx, l))), l.User))
c.Lock()
// one dropped meanwhile (StaleAllowance) read too soon, and a
// Claude account the user asked to see meanwhile is read again
@@ -341,7 +341,7 @@ func cachedLoginReading(ctx context.Context, l Login) loginUsageEntry {
return r.e
case <-ctx.Done():
// given up on: as a reading cut short would have said
return entry(keepReading(ctx, SubscriptionQuota{Provider: loginProvider(l), Plan: l.Plan, Windows: []QuotaWindow{}, Error: ctx.Err().Error()}, l.User))
return entry(keepReading(ctx, cutShort(l, ctx.Err()), l.User))
}
}
@@ -659,3 +659,21 @@ func codexLoginAuth(l Login) []byte {
}
return nil
}
// cutShort is account l's reading when none was made: the error says
// why, and keepLast puts the last reading kept under it.
func cutShort(l Login, err error) SubscriptionQuota {
return namedFor(l, SubscriptionQuota{Provider: loginProvider(l), Plan: l.Plan, Windows: []QuotaWindow{}, Error: err.Error()})
}
// namedFor names a reading for the account l it was asked for. keepLast
// puts the name it is given on what it kept, so an unnamed reading came
// back unnamed even with the windows kept, and on Usage each one drew a
// card of its own instead of a row on its provider's (#5310: 768 of
// 1,440 Antigravity accounts, after reads given up on at their deadline).
func namedFor(l Login, q SubscriptionQuota) SubscriptionQuota {
if q.User == "" {
q.User = l.User
}
return q
}
+49
View File
@@ -0,0 +1,49 @@
package provider
import (
"context"
"testing"
"time"
)
// An account's reading is named for the account it was asked for,
// whichever way it comes back (#5310: a read given up on at its deadline
// came back with the kept windows and no User, and 768 of 1,440
// Antigravity accounts each drew a card of its own on the Usage page).
func TestAccountReadingNamed(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
restart := func() {
lastQuotas.Lock()
lastQuotas.m, lastQuotas.loaded = nil, false
lastQuotas.Unlock()
}
restart()
defer restart()
later := time.Now().Add(time.Hour)
for _, agent := range []string{"antigravity", "claude"} {
l := Login{Agent: agent, User: "a@x.com"}
keepLast(SubscriptionQuota{Provider: loginProvider(l), User: "a@x.com", Windows: []QuotaWindow{
{Name: "5 hours", Used: 40, ResetsAt: &later, Span: 5 * time.Hour},
}}, "")
// given up on at its deadline, as loginReading does: the kept
// reading, under the account's name
if q := keepReading(context.Background(), cutShort(l, context.DeadlineExceeded), l.User); q.User != "a@x.com" || len(q.Windows) != 1 || q.Windows[0].Used != 40 {
t.Fatalf("%s deadline, kept: %+v", agent, q)
}
if q := keepLast(cutShort(l, errNotAsked), l.User); q.User != "a@x.com" || len(q.Windows) != 1 {
t.Fatalf("%s not asked: %+v", agent, q)
}
// an account with nothing kept says its error under its own name
b := Login{Agent: agent, User: "b@x.com"}
if q := keepReading(context.Background(), cutShort(b, context.DeadlineExceeded), b.User); q.User != "b@x.com" || q.Error == "" {
t.Fatalf("%s nothing kept: %+v", agent, q)
}
// a reading made with no name is named; one with a name keeps it
if q := namedFor(b, SubscriptionQuota{Provider: agent}); q.User != "b@x.com" {
t.Fatalf("%s unnamed: %+v", agent, q)
}
if q := namedFor(b, SubscriptionQuota{Provider: agent, User: "B@X.com"}); q.User != "B@X.com" {
t.Fatalf("%s named: %+v", agent, q)
}
}
}
+7 -1
View File
@@ -582,7 +582,13 @@ func fetchSubscriptionUsage(ctx context.Context) []SubscriptionQuota {
} else if ok {
forgetLoginReading(l.Login)
}
fetches = append(fetches, func() SubscriptionQuota { return loginReading(viaLogin(agent, l.User), l.Login).read })
fetches = append(fetches, func() SubscriptionQuota {
q := loginReading(viaLogin(agent, l.User), l.Login).read
if q.User == "" {
q.User = l.User // as perLogin names it
}
return q
})
}
}
fetches = append(fetches, pluginUsageFetches(via, hidden, placed)...)