diff --git a/internal/gui/assets/app.js b/internal/gui/assets/app.js index 759e558fe..eb3b4be05 100644 --- a/internal/gui/assets/app.js +++ b/internal/gui/assets/app.js @@ -17129,6 +17129,10 @@ function quotaRefresh(q) { const kept = quotas || []; quotas = kept.some(x => usageAcctKey(x) === key) ? kept.map(x => usageAcctKey(x) === key ? fresh : x) : [...kept, fresh]; quotasAt = Date.now(); + } else if (Array.isArray(qs)) { + // The card came back under another key (an account first read + // under its name): the reply's list is taken as it was before. + quotas = qs; quotasAt = Date.now(); } } catch (err) { status(err.message, "err"); diff --git a/internal/gui/tests/quota-refresh-order.test.cjs b/internal/gui/tests/quota-refresh-order.test.cjs index ae73bbe39..4d3ac482e 100644 --- a/internal/gui/tests/quota-refresh-order.test.cjs +++ b/internal/gui/tests/quota-refresh-order.test.cjs @@ -88,3 +88,13 @@ test("a failed card refresh leaves background readings usable", async () => { assert.equal(p.cards()[0].windows[0].used, 32); assert.deepEqual(plain(p.errors), ["offline"]); }); + +test("a card that comes back under another key still shows the reply", async () => { + // A card read before its account was known has no user; its refresh + // answers under the account's name. + const p = page(), unnamed = card("", 0); + p.seed([unnamed]); + const refresh = p.refresh(unnamed); + p.answer(0, [card("a@x.com", 32)]); await refresh; + assert.deepEqual(plain(p.cards()).map(q => [q.user, q.windows[0].used]), [["a@x.com", 32]], "the refreshed card's reply was dropped"); +}); diff --git a/internal/provider/quota_deferred_test.go b/internal/provider/quota_deferred_test.go index e89f3a4de..1faeec5bb 100644 --- a/internal/provider/quota_deferred_test.go +++ b/internal/provider/quota_deferred_test.go @@ -40,6 +40,9 @@ func TestQuotaDeferredReadIsNotFailure(t *testing.T) { {"saved", errClaudeSaved.Error(), true}, {"not asked", errClaudeNotAsked.Error(), true}, {"manual only", errNotAsked.Error(), true}, + // holdUnasked stops the vendor's request itself, so the error + // arrives wrapped by the HTTP client. + {"held request", `Get "https://api.example.com/usage": ` + errNotAsked.Error(), true}, {"rate limited", "HTTP 429 Too Many Requests", false}, {"unavailable", errClaudeUsageUnavailable.Error(), false}, {"network", "connection refused", false}, diff --git a/internal/provider/quota_last.go b/internal/provider/quota_last.go index 2e6399612..bc8de38a5 100644 --- a/internal/provider/quota_last.go +++ b/internal/provider/quota_last.go @@ -111,8 +111,7 @@ func keepLast(q SubscriptionQuota, user string) SubscriptionQuota { if !ok { return q } - out.ReadDeferred = q.Error == errNotAsked.Error() || q.Provider == "claude" && - (q.Error == errClaudeNotAsked.Error() || q.Error == errClaudeSaved.Error()) + out.ReadDeferred = readDeferred(q.Error) out.Name, out.Icon, out.User = q.Name, q.Icon, q.User if out.Name == "" { out.Name, out.Icon = c.m[key].Q.Name, c.m[key].Q.Icon @@ -120,6 +119,20 @@ func keepLast(q SubscriptionQuota, user string) SubscriptionQuota { return out } +// readDeferred is whether err says no read was made this time (allowances +// read only when asked, a Claude account's /usage not run) rather than a +// read that failed. A read held back by holdUnasked fails inside the +// vendor's HTTP call, so its error carries errNotAsked wrapped: "Get +// \"https://…\": not read yet: …". +func readDeferred(err string) bool { + for _, e := range []error{errNotAsked, errClaudeNotAsked, errClaudeSaved} { + if strings.Contains(err, e.Error()) { + return true + } + } + return false +} + // load reads the last readings from disk, the first time. Called with c // held. func (c *lastQuotasT) load() {