Files
magpie/internal/provider/window_held.go
T
yetone 16ce5ed232 usage: an account whose longer window is used up adds no headroom to the all-accounts sum (#4791)
hayden4608 (Discord) and handong66 (#4791) have four Codex accounts. Three
show 5 hours 100% left with 7 days 0% left, and one shows 5 hours 0% left
with 7 days 22% left. The All accounts strip read "5 hours 75% left", but
none of those three can spend its 5 hours until its week starts again.
The right figure is 0%.

mergeQuotas (quotaall.js) is the one place that sums windows across
accounts. It feeds the Usage page's strip, the tray panel's strip and the
Routing groups' line. The native menu-bar tray, the TUI, the CLI and
/v1/magpie/quotas show one account at a time and are unchanged. The GUI's
JSON doesn't carry Aside, Model or the matches, so the call is made in Go.
provider.WithHeldWindows marks a window heldBy another window of the same
account when that other window:
- stops the account for every model, by routing's rule: not aside, no
  model, no matches, used >= 100, reset not passed, and not Codex credits
  that go on; and
- starts again after this one (by reset times, else by span).
So 7 days used up holds 5 hours, but 5 hours used up doesn't hold a week
with 22% left. /api/usage/quotas and its refresh apply it. mergeQuotas
counts a held share window as 100 used and a held counted one as its
limit, and the strip's tooltip says how many were counted that way
(en/zh/zh-TW/ja/de). The account cards still show the vendor's own figure.

Run on mote (macOS) at this head's parent tree:
- go vet for darwin, linux (nogui) and windows: ok; gofmt -l: none.
- go test -count=1 ./internal/provider ./internal/gui, before the
  quota_span leftOut line and an i18n comment fix. The provider package's
  only red was TestQuotaSpanCarriesEveryWindowField, which wanted heldBy
  named, and it is now named. internal/gui's only red was
  TestMainWindowCorners/macOS_26_SDK, which is #3949.
- GUI, chromium and webkit, every language, 440px and 360px: quota-*,
  usage-* (all-accounts, merge, accounts, fold, refresh and more),
  panel-usage/quota/switch-accounts/balance-order, routing-quota-left,
  routing-group, tray-usage, i18n-keys, gui-de/ja/zh-tw, old-webkit:
  704 of 707. The reds were i18n-keys (a quoted "All accounts" in
  window_held.go's comment, fixed here) and usage-fold's "17
  subscriptions and 29 accounts" in both engines, which is #4790 and is
  red at origin/main too (see below).
At the pushed head, rebased on eb3b9376e, on mote: gofmt none, vet ok.
internal/provider ok. internal/gui is red only on TestMainWindowCorners
(#3949). GUI usage-merge, usage-all-accounts, i18n-keys and usage-fold,
both engines: 44 of 46. The 2 reds are usage-fold's 29 accounts, which
fail the same at origin/main eb3b9376e on mote (noted on #4790).

Broken by hand, each red, then restored (cmp):
- Go, no HeldBy set: both TestHeldWindows tests red (a, b, c not held).
- Go, outlasts always true: red on d and on "a week that starts again
  first".
- JS, held forced false: usage-merge plus all 5 languages of
  usage-all-accounts red in chromium, showing the reporter's
  "5 hours · 75% left".

Not tried: real accounts. The issue names no provider; Codex is assumed
from the cards' 5 hours / 7 days. Not done: marking a card as held by its
week, the reporter's optional second ask. Not written: a handler-level
test that /api/usage/quotas carries heldBy. Not run on Linux or Windows.
2026-11-02 03:46:01 +08:00

81 lines
2.7 KiB
Go

package provider
import "time"
// WithHeldWindows is qs with HeldBy set on each window whose remainder an
// account can't spend: another of its windows that stops it for every
// model (usedPast's rule: not aside, no model of its own, not past its
// reset) is used up and starts again only after this one does, so what
// this one has left is lost at its own reset, unspent. An account with 0%
// of its 7 days left and 100% of its 5 hours has no 5 hours to spend, and
// the Usage page's all-accounts strip, the tray panel's and the Routing
// groups' (mergeQuotas) count it as none left rather than as a full window
// (hayden4608, handong66: three such accounts and one with its 5 hours
// used up read 75% of 5 hours left in all, #4791). It isn't so the other
// way: a week with 22% left behind five hours used up still has that 22%
// once the five hours start again. A ChatGPT account that goes on on its
// credits isn't held. Another magpie's cards are as it sent them; qs and
// its windows, which the caches share, are left as they were.
func WithHeldWindows(qs []SubscriptionQuota, now time.Time) []SubscriptionQuota {
out := make([]SubscriptionQuota, len(qs))
for i, q := range qs {
out[i] = q
if q.From != "" || q.Error != "" {
continue
}
var stops []int
for j, w := range q.Windows {
if stopsAll(q, w, now) {
stops = append(stops, j)
}
}
if len(stops) == 0 {
continue
}
ws := make([]QuotaWindow, len(q.Windows))
for j, w := range q.Windows {
if !w.Unlimited && !w.Text && w.Balance == nil {
for _, s := range stops {
if s != j && outlasts(q.Windows[s], w, now) {
w.HeldBy = q.Windows[s].Name
break
}
}
}
ws[j] = w
}
out[i].Windows = ws
}
return out
}
// stopsAll says whether w, one of q's windows, is used up at now and stops
// the account for every model while it is.
func stopsAll(q SubscriptionQuota, w QuotaWindow, now time.Time) bool {
if w.Aside || w.Text || w.Unlimited || w.Model != "" || w.matches != nil || w.Used < 100 || resetPassed(w, now) {
return false
}
return !w.Credits || !CodexCredits(q.User) // else the vendor spends the credits instead
}
// outlasts says whether used-up window h starts again only after w does,
// by their resets where both are known, else by how long each runs.
func outlasts(h, w QuotaWindow, now time.Time) bool {
hr, wr := heldReset(h, now), heldReset(w, now)
if !hr.IsZero() && !wr.IsZero() {
return hr.After(wr)
}
return windowSpan(h) > windowSpan(w)
}
// heldReset is when w starts again; zero when it doesn't say.
func heldReset(w QuotaWindow, now time.Time) time.Time {
switch {
case w.ResetsAt != nil:
return *w.ResetsAt
case w.ResetSecs > 0:
return now.Add(time.Duration(w.ResetSecs) * time.Second)
}
return time.Time{}
}