Files
magpie/internal/provider/account_caps.go
T
yetone 08b02cf18c providers: a pool stops at its caps and reads usage again near them (plugins#349)
What was adopted on magpie-community/plugins#349 (1939869736luosi): one
place, in the GUI and the CLI, for a subscription's account pool, and an
opt-in conservative mode.

- Stop at the caps (Provider.CapStop, off by default): once every account
  in use is held at its cap, the request is answered 429 rather than going
  on to the provider's fallbacks (plan returns restLast). Off, as before.
- Check usage again near a cap (Provider.CapCareful, off by default,
  internal/gateway/cap_careful.go): before planning, a capped account's
  usage is read again when its reading is over 15s old within 5 points of
  a cap, or over 5m old at all; the request waits at most 10s, and a
  failed account is asked again at most every 15s. An account whose usage
  was never read, whose last read failed, or whose reading is still that
  old is then held as one at its cap is (capHold "unread", traced as
  Weighed.Unread, named in the 429 with "--careful off"). Generic: it
  goes by provider.UsageRead/ReadUsageNow, so every subscription that
  reports usage windows (Codex, Claude, plugins) gets it.
- provider: usedCache records each account's read state; a failed read
  keeps the last good reading's time. ReadUsageNow no longer reports
  false when the read had already finished on the first ask.
- GUI: under the provider's account list, once an account has a cap (or
  either is on), two ticks for the above, and a note naming the accounts
  in use with no cap ("used to 100% of each window"). Accounts, order and
  caps stay where they were in that list. zh, zh-TW, ja, de added.
  app.js 1,309,658 within its 1,310,460 budget, unraised: addRow and a
  per-row forgetOf replace 3 add-button and 5 Remove-button builds.
- CLI: magpie provider pool <id> [--accounts a,b,…] [--cap percent|off]
  [--stop on|off] [--careful on|off] shows and sets the whole pool.
- docs: gateway-routing.md and providers-accounts.md.

Run (mote, before the rebase at 8bade6c5e + this change):
- go test -tags nogui -timeout 45m ./...: exit 0, 76 packages ok, no
  FAIL (log grepped).
- GOOS=linux and GOOS=windows go vet of cli, gateway, provider, gui: ok.
- pool-ends.test.cjs: 32/32, Chromium and WebKit, en zh ja de, 1100 and
  420 wide.
- The 81 GUI files whose fixtures have accounts or caps, plus i18n*,
  6 at a time at load ~130: 76 pass. i18n-keys is red on the base too
  (#3897). account-mask (#3951), account-alias (#2984),
  signin-closes-sheet and signin-again timed out under load and pass
  run again, as on the base; signin-again filed as #3960.
- Real plugin host: TestPluginCarefulPool runs the fake plugin under Bun
  as Factory with two accounts capped at 90%: a failing usage call gives
  429 with nothing sent upstream while careful is on, the last reading
  is used with it off, a good read sends to A again, and with both at
  their caps the fallback serves unless Stop is on.
- CLI binary in a sandbox HOME on mote: unknown provider, a bad --stop
  value and no id each refuse and change nothing.
At the rebased head (0a7633136 + this): gofmt -l
clean, go build ./..., the new Go tests and the budget tests, and
pool-ends.test.cjs 32/32.

Broken by hand, each seen red, then restored:
- the CapStop line in plan: TestCapStopSkipsTheFallbacks and
  TestPluginCarefulPool red;
- the careful hold in capHeld: the three careful tests and
  TestPluginCarefulPool red;
- careful always on: TestCarefulOffByDefault red;
- readCareful not called: the failed, never-read, stale and re-read
  subtests red;
- a failed read not keeping the good time, or not counted as failed,
  and ReadUsageNow's early return forced false: TestUsageReadAndReadAgain
  red;
- the ticks' onchange emptied: pool-ends.test.cjs 16 of 16 red.

Not tried: a real Factory or any real account (fake plugin only); the
whole GUI suite (932 files, ~5h at this load), only the 81 files that
reach the account list; Linux WebKitGTK and Windows; the 15s/5m/5-point
figures against a real vendor's usage endpoint latency.
2026-10-28 09:29:44 +08:00

384 lines
11 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package provider
// An account's usage cap: the share of its usage windows a subscription
// account is used to at most (a Discord ask: a new Claude account kept to
// 70% of its five hours and its week, so it isn't run hard). Once a window
// that counts the model is at or past it, routing takes the account for
// used up — as at 100% — and goes on to the user's other accounts; the
// account is free again when that window renews. The gateway leaves it out
// of a request (gateway/fallback.go), and Codex or Claude Code signed in
// to it are signed in to another (NextLogin).
//
// One share for every window by default, and a share of its own for any
// window the user sets one on (willz on Discord: a friend's account stops
// at 50% of its five hours while its week may go to 40%). A window is known
// by its name as the vendor's reading gives it, in lower case
// (WindowCapID): "5 hours", "weekly", "7 days · opus", a pool's "gemini ·
// 5 hours" — the name routing already tells one reading's windows from the
// next by. The windows differ by vendor, so the overrides are kept per
// account (AccountWindowCaps), set where the window is shown. A window's
// own share is 1–99, or 100 for "no cap on this window" under an account
// cap; one with none of its own takes the account's. The account is held
// while any window is at or past its own share. The on-demand windows
// (Aside) are not the allowance and never count.
//
// The cap is the user's, not the vendor's: what it holds back never spends
// a Codex reset. The resets are spent on the vendor's own 100% (weekUsedUp,
// usedUp, BackAt, heldUp), which the cap doesn't touch.
import (
"fmt"
"maps"
"strconv"
"strings"
"time"
)
// MinCap and MaxCap bound an account's cap, in percent: none is no cap.
const (
MinCap = 1
MaxCap = 99
)
// AccountCap is the share (1–99) of its windows the account user of p is
// used to at most, 0 when it has no cap. An account not had from All or
// AlsoOn (made afresh by its sign-in) carries none of the provider's
// settings: they are read from providers.json then.
func (p Provider) AccountCap(user string) int {
caps := p.AccountCaps
if caps == nil {
if s, ok := storedPicks(p.ID); ok {
caps = s.AccountCaps
}
}
return caps[accountKey(user)]
}
// AccountCapOf is AccountCap of the provider id, 0 when there is none.
func AccountCapOf(id, user string) int {
if s, ok := storedPicks(id); ok {
return s.AccountCaps[accountKey(user)]
}
return 0
}
// WindowCaps is what holds an account at a share of its windows: its usage
// cap for every window (All), the share of each window the user set one
// on (Windows, by WindowCapID; 100 is none on that window), and Credits
// for a Codex account set not to spend its credits, which holds any window
// with no cap at the vendor's own 100%.
type WindowCaps struct {
All int
Windows map[string]int
Credits bool
}
// WindowCapID is how a window is known to its cap: its name, trimmed, in
// lower case.
func WindowCapID(name string) string { return strings.ToLower(strings.TrimSpace(name)) }
// Of is the share at which the window named name holds the account, 0 for
// none, and whether it is the credits that hold it there.
func (c WindowCaps) Of(name string) (share int, credits bool) {
share = c.All
if v := c.Windows[WindowCapID(name)]; v > 0 {
share = v
}
if share >= 100 {
share = 0
}
if share <= 0 && c.Credits {
return 100, true
}
return share, false
}
// Holds says whether any window can hold the account before the vendor
// does.
func (c WindowCaps) Holds() bool {
if c.All > 0 || c.Credits {
return true
}
for _, v := range c.Windows {
if v >= MinCap && v <= MaxCap {
return true
}
}
return false
}
// CapsOf is the caps of the account user of p: its usage cap
// and its windows' own. As with AccountCap, an account made afresh beside
// the agent's own reads them from providers.json.
func (p Provider) CapsOf(user string) WindowCaps {
caps, wins := p.AccountCaps, p.AccountWindowCaps
if caps == nil && wins == nil {
if s, ok := storedPicks(p.ID); ok {
caps, wins = s.AccountCaps, s.AccountWindowCaps
}
}
k := accountKey(user)
return WindowCaps{All: caps[k], Windows: wins[k]}
}
// AccountCapsOf is CapsOf of the provider id.
func AccountCapsOf(id, user string) WindowCaps {
if s, ok := storedPicks(id); ok {
k := accountKey(user)
return WindowCaps{All: s.AccountCaps[k], Windows: s.AccountWindowCaps[k]}
}
return WindowCaps{}
}
// SetWindowCap sets the share of the window named window of the account ref
// of the subscription id: 1–99, 100 for no cap on that window whatever the
// account's cap, or 0 to have it follow the account's cap again.
func SetWindowCap(id, ref, window string, cap int) error {
if cap != 0 && (cap < MinCap || cap > 100) {
return fmt.Errorf("a window's cap is a share from %d%% to %d%%, or 100%% for none on it, not %d%%", MinCap, MaxCap, cap)
}
w := WindowCapID(window)
if w == "" {
return fmt.Errorf("name the usage window to cap")
}
p, err := Find(id)
if err != nil {
return err
}
if p.Account == nil {
return fmt.Errorf("%s has keys, not subscription accounts with usage windows to cap", p.Name)
}
r, ok := p.accountRef(ref)
if !ok {
return fmt.Errorf("%s has no account %q", p.Name, ref)
}
k := accountKey(r)
if p.AccountWindowCaps[k][w] == cap {
return nil
}
all := map[string]map[string]int{}
for u, m := range p.AccountWindowCaps {
all[u] = maps.Clone(m)
}
if all[k] == nil {
all[k] = map[string]int{}
}
if cap == 0 {
delete(all[k], w)
} else {
all[k][w] = cap
}
if len(all[k]) == 0 {
delete(all, k)
}
p.AccountWindowCaps = all
return Save(*p)
}
// SetAccountCap caps the account ref of the subscription id at cap percent
// of its windows; 0 lifts the cap.
func SetAccountCap(id, ref string, cap int) error {
if cap != 0 && (cap < MinCap || cap > MaxCap) {
return fmt.Errorf("a cap is a share from %d%% to %d%% of each window, not %d%%", MinCap, MaxCap, cap)
}
p, err := Find(id)
if err != nil {
return err
}
if p.Account == nil {
return fmt.Errorf("%s has keys, not subscription accounts with usage windows to cap", p.Name)
}
r, ok := p.accountRef(ref)
if !ok {
return fmt.Errorf("%s has no account %q", p.Name, ref)
}
if cap == 0 && p.AccountCaps[accountKey(r)] == 0 {
return nil
}
caps := map[string]int{}
for k, v := range p.AccountCaps {
caps[k] = v
}
if cap == 0 {
delete(caps, accountKey(r))
} else {
caps[accountKey(r)] = cap
}
p.AccountCaps = caps
return Save(*p)
}
// ParseCap reads a cap as the CLI takes it: "70", "70%", or off (none, 0,
// -) for no cap.
func ParseCap(s string) (int, error) {
s = strings.TrimSpace(strings.TrimSuffix(strings.TrimSpace(s), "%"))
switch strings.ToLower(s) {
case "off", "none", "no", "-", "0", "100":
return 0, nil
}
n, err := strconv.Atoi(s)
if err != nil || n < MinCap || n > MaxCap {
return 0, fmt.Errorf("a cap is a share from %d to %d (percent), or off, not %q", MinCap, MaxCap, s)
}
return n, nil
}
// normalAccountCaps is m as it is kept: keys in lower case, caps within
// MinCap–MaxCap, accounts without one left out; nil for none.
func normalAccountCaps(m map[string]int) map[string]int {
var out map[string]int
for u, c := range m {
u = accountKey(u)
if u == "" || c < MinCap || c > MaxCap {
continue
}
if out == nil {
out = map[string]int{}
}
out[u] = c
}
return out
}
// normalWindowCaps is m as it is kept: accounts and windows in lower case,
// shares within MinCap–100, empty ones left out; nil for none.
func normalWindowCaps(m map[string]map[string]int) map[string]map[string]int {
var out map[string]map[string]int
for u, ws := range m {
u = accountKey(u)
for w, c := range ws {
w = WindowCapID(w)
if u == "" || w == "" || c < MinCap || c > 100 {
continue
}
if out == nil {
out = map[string]map[string]int{}
}
if out[u] == nil {
out[u] = map[string]int{}
}
out[u][w] = c
}
}
return out
}
// CapHold is how an account is held by its caps for a model: the share
// used of the fullest window holding it, that window's cap, when the last
// of those windows renews (zero when one doesn't say), and Credits when
// only the credits hold it, at 100%.
type CapHold struct {
Used float64
Cap int
Back time.Time
Credits bool
}
// CapHeld says how an allowance, as last known, holds the account for
// model at now: some window counting model at or past its own share of
// caps (WindowCaps.Of), not renewed since; nil when none is. A window
// whose reset has passed is empty again; no caps hold nothing.
func (a Allowance) CapHeld(model string, caps WindowCaps, now time.Time) *CapHold {
if !caps.Holds() {
return nil
}
model = strings.ToLower(model)
var h *CapHold
unknown, byCap := false, false
for _, l := range a {
share, credits := caps.Of(l.Name)
if share <= 0 || !l.applies(model) || l.Used < float64(share) {
continue
}
if !l.Resets.IsZero() && !l.Resets.After(now) {
continue // renewed since it was read
}
if h == nil {
h = &CapHold{}
}
if l.Used > h.Used || h.Cap == 0 {
h.Used, h.Cap = l.Used, share
}
byCap = byCap || !credits
if l.Resets.IsZero() {
unknown = true
} else if l.Resets.After(h.Back) {
h.Back = l.Resets
}
}
if h == nil {
return nil
}
if unknown {
h.Back = time.Time{}
}
h.Credits = !byCap
return h
}
// NearCap says whether an allowance, as last known, has some window
// counting model within points of its own share of caps (or past it) at
// now, not renewed since: a careful provider reads such an account again
// before it is sent a request (Provider.CapCareful). No caps are near
// nothing.
func (a Allowance) NearCap(model string, caps WindowCaps, points float64, now time.Time) bool {
if !caps.Holds() {
return false
}
model = strings.ToLower(model)
for _, l := range a {
share, _ := caps.Of(l.Name)
if share <= 0 || !l.applies(model) || l.Used < float64(share)-points {
continue
}
if !l.Resets.IsZero() && !l.Resets.After(now) {
continue // renewed since it was read
}
return true
}
return false
}
// capReached says whether an account's windows that stop it for every
// model are, one of them, at or past its own share of caps at now —
// usedPast at the share, those renewed since they were read left out. No
// caps reach nothing.
func capReached(q SubscriptionQuota, caps WindowCaps, now time.Time) bool {
for _, w := range q.Windows {
share, _ := caps.Of(w.Name)
if share <= 0 || w.Aside || w.Model != "" || w.Used < float64(share) {
continue
}
if w.ResetsAt != nil && !w.ResetsAt.After(now) {
continue
}
return true
}
return false
}
// WithCapped is m with Capped set on each window an account's usage cap
// counts, for the GUI to mark the cap and say an account is held at it as
// routing does — a window one model's own counts for that model, so it is
// marked as the rest are; the windows are copied, the cache's left as they
// are.
func WithCapped(m map[string]SubscriptionQuota) map[string]SubscriptionQuota {
out := make(map[string]SubscriptionQuota, len(m))
for u, q := range m {
ws := make([]QuotaWindow, len(q.Windows))
for i, w := range q.Windows {
w.Capped = !w.Aside && !w.Unlimited
w.CapsSome = w.Capped && (w.Model != "" || w.matches != nil)
if w.Capped {
w.CapID = WindowCapID(w.Name)
}
ws[i] = w
}
if q.Windows != nil {
q.Windows = ws
}
out[u] = q
}
return out
}