Files
magpie/internal/provider/allowance_renewed.go
T
yetone 34a598372c providers, gateway, gui, cli: each usage window of an account can have a cap of its own beside the account's (willz on Discord)
willz asked for a cap per window: a friend's account should stop at 50%
of its five hours, while its week has its own 40%. Until now an account
had one usage cap, the same share of every window.

What the user sees: the account's pill still sets the cap for all its
windows and stays the default. Each window's meter on the account's row
is now a button: a click opens the app's own menu headed "Cap for
<window>" with the account's cap, No cap on this window, 50–90%, and
Other… typed in place. A window with its own cap says it beside its
figure ("Cap 50%", or "No cap"), its mark sits there, and the account's
pill says "Caps by window" when only windows have caps. The account is
held while any window is past its own cap, and the row's note, the
gateway's 429 and the Routing trace name that window's cap. CLI:
magpie provider account-cap <id> <account> --window "5 hours" 50
(none: no cap on that window; default: back to the account's), and
`account-cap <id>` lists the overrides.

How: Provider.AccountWindowCaps (providers.json, by account and the
window's capId, its name in lower case; 1–99, or 100 for none) beside
AccountCaps. provider.WindowCaps gives a window its share (its own, else
the account's); Allowance.CapHeld and capReached judge each window by
its own share and report the fullest holding window's cap.
provider.HoldShare becomes HoldCaps, used by gateway capHeld (plan and
group filtering, cappedError, trace) and codex_backend's route-or-relay
choice; codex_switch's NextLogin moves the agent off at per-window caps.
QuotaWindow carries capId; Limit.name is exported as Limit.Name. A
config with no window caps reads and routes exactly as before
(TestSetWindowCap reads an old one). The meter is a span with
role=button, Enter/Space: a <button> as a flex item of the wrapping
meter line kept WebKit's row at its one-line height until the next
layout, so the row grew under the pointer at the first click.

Verified:
- go test -tags nogui ./... in a sandbox HOME: all ok. New
  TestWindowCapHeld and TestSetWindowCap (provider), TestWindowCapsHoldTheAccount
  through the real gateway (5h 60%/cap 50 goes to the spare; 20/30 under
  50/40 is used; 20/45 with the spare past its own cap is a 429 naming
  "past its 40% cap"), TestAccountCapCmd --window cases.
- Break check: with WindowCaps.Of ignoring the window's own share,
  TestWindowCapHeld fails ("5 hours at 60% of 50%: <nil>") and
  TestWindowCapsHoldTheAccount fails (the request is answered 200 by the
  capped account).
- internal/gui/tests/window-cap.test.cjs: Chromium and WebKit, 420 and
  1100 wide, en, zh, zh-TW, ja, de (60 tests): held note per window,
  marks, menu from the meter with no native select, keyboard, posts of
  50, typed 45, 100 and 0, no page scroll on a click, no sideways scroll,
  no left border stripes, every new string translated. With
  windowOwnCapOf returning 0 it fails.
- account-cap, gui-zh-tw, gui-ja, gui-de, i18n-keys pass; full GUI suite
  in both engines (483 files, concurrency 6): 4315 of 4323
  pass; the 8 that failed (library-servers-bulk, purpose-filter,
  routing-proxy, routing-zcode-blocked) touch no account row and all 32
  of their tests pass when run alone
- gofmt -l clean; go vet -tags nogui ./... on darwin, linux and windows,
  and windows without the tag for ., internal/gui, internal/provider,
  internal/gateway; go build ./...
- Not tried in the real window with a live gateway or real accounts.

Docs: gateway-routing.md (HoldCaps, window caps, tests),
providers-accounts.md (AccountWindowCaps, tests), reference.md.
2026-10-09 12:00:26 +08:00

83 lines
2.9 KiB
Go

package provider
// An account resting out of its allowance is back as soon as a reading of
// its windows finds the one it was out of started again, rather than when
// its rest runs out: a Claude account's rest is as long as Claude Code's
// refusal said, and a new /usage, what Claude Code says as it answers, or
// the reset passing (elapsed) can show the window renewed before then.
// Allowances, which routing reads each account's windows by, tells
// OnRenewed's hooks so, for every subscription: the built-ins' and a
// plugin's (as "plugin:<id>") alike.
import (
"strings"
"time"
)
// reading is an account's windows as Allowances last read them, and when.
type reading struct {
a Allowance
at time.Time
}
// renewedFrom says whether a, read at now, finds the account full till
// sooner than was, read at wasAt, did (the rule a key's reading is told
// renewed by, keyAllowances' keep): for the whole account, or for a pool
// of some models only with the whole account's windows over it. A window
// used below share, or whose reset passed, is full no more; one full with
// its reset not known is full for good. Only the windows both readings
// have are weighed, so one not read this time tells nothing; nor does one
// read alike, nor one renewed while another over the same models is
// still full: the five hours started again in a week used up.
func (a Allowance) renewedFrom(was Allowance, wasAt time.Time, share float64, now time.Time) bool {
type key struct{ name, model string }
keyOf := func(l Limit) key { return key{l.Name, l.Model} }
read := map[key]bool{}
for _, l := range a {
read[keyOf(l)] = true
}
whole := func(l Limit) bool { return l.Model == "" && l.matches == nil }
sooner := func(in func(Limit) bool) bool {
in2 := func(l Limit) bool { return read[keyOf(l)] && in(l) }
return was.fullTill(in2, share, wasAt).After(a.fullTill(in2, share, now))
}
if sooner(whole) {
return true
}
for _, w := range was {
if k := keyOf(w); !whole(w) && sooner(func(l Limit) bool { return whole(l) || keyOf(l) == k }) {
return true
}
}
return false
}
// fullTill is when the windows in leaves full at share at now renew: the
// last of their resets, zero when none is full, and far off when one is
// full with its reset not known.
func (a Allowance) fullTill(in func(Limit) bool, share float64, now time.Time) time.Time {
var t time.Time
for _, l := range a {
switch {
case !in(l) || l.Used < share:
case l.Resets.IsZero():
return time.Unix(1<<62, 0)
case l.Resets.After(now) && l.Resets.After(t):
t = l.Resets
}
}
return t
}
// renewalShare is the share at which routing counts the windows of
// agent's accounts full (SpentShareOf its provider's routing): a plugin's
// ("plugin:<id>") by the provider its accounts are listed under.
func renewalShare(agent string) float64 {
id := agent
if p, ok := strings.CutPrefix(agent, "plugin:"); ok {
id = PluginID(p)
}
share, _ := loginSwitching(id)
return share
}