diff --git a/internal/provider/login_usage.go b/internal/provider/login_usage.go index d31281d61..895447b45 100644 --- a/internal/provider/login_usage.go +++ b/internal/provider/login_usage.go @@ -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 +} diff --git a/internal/provider/quota_last_user_test.go b/internal/provider/quota_last_user_test.go new file mode 100644 index 000000000..53d32efbc --- /dev/null +++ b/internal/provider/quota_last_user_test.go @@ -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) + } + } +} diff --git a/internal/provider/subscription_usage.go b/internal/provider/subscription_usage.go index 5817323e8..07cb8ab56 100644 --- a/internal/provider/subscription_usage.go +++ b/internal/provider/subscription_usage.go @@ -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)...)