Files
magpie/internal/provider/account_weight.go
T
yetone 88ecae1738 routing: By weight spreads a subscription's requests over its accounts
rubbzha on X (tweet 2116346330625036794): a Claude Max account and a Pro
one signed in to one subscription should share its requests by how much
each allows. The provider editor's Routing now offers By weight for
subscription accounts as it does for keys. With it picked, each account
in use shows "Weight N · S%" beside its name, the key weight's chip, and
a click edits it in place (POST /api/provider/accountweight {id, account,
weight}). CLI: `magpie provider account-weight <id> [account [n|default]]`.

The weight is kept per account by its lower-case name
(Provider.AccountWeights), and is moved with a rename (moveAccountSettings).
The gateway's keyWeight reads it from the candidate, with no file read, so
the smooth weighted pick treats accounts as it treats keys: Stays keeps a
conversation on its account, and a resting account is passed over while
the others share its requests. The default is 1. Nothing is prefilled from
the plan, and 1 is stored as no entry, so a value the user set is never
overridden.

Re-derived: ROUTINGS' weight text and routingsOf (the editor), the Routing
page's MODES, provider-row options and its "why first" text for an account,
the routing and accounts subsystem pages, the start docs (en, zh, ja), and
i18n in zh, zh-TW, ja and de (i18n.js and plugins/routing/i18n.js).
app.js is 74 bytes smaller than before (a long comment cut, keyWeight
reused), so neither script budget is raised.

key-weight.test.cjs read #status before accountAction's second round trip
(/api/state) had written it. The same race is now waited on in both tests:
with a 400ms delay injected on /api/state, the old read is red and the
new one is green.

Run (rebased on 03f3e1528; then on ef8dfaa46, where the budget, i18n and
own tests were run again and passed: Budget|I18n|Weight Go tests,
account-weight, key-weight and i18n-keys GUI files 10/10):
- New tests, each red on the old code: TestSetAccountWeight,
  TestAccountWeightMovesWithRename (red with the moveKey line removed),
  TestAccountWeightSharesRequests (6/2 for weights 3/1; alternating with
  keyWeight forced to 1), TestAccountWeightCmd, and
  account-weight.test.cjs (chromium + webkit, widths 1000/560 and web 390,
  en/zh/zh-TW/ja/de; red on 3e5aa1151's assets: no By weight for accounts).
- account-weight, key-weight, routing-weight-chip, account-row-line GUI
  files on the Linux box: 24/24 after the status wait (23/24 before, the
  race above). On mote, 13 of 24 timed out launching a browser at load 24
  with 116 Playwright processes from other sessions (#4125).
- mote `go test -timeout 45m ./...`: every package ok except
  internal/proc's TestNoCommandBypassesProc (claudetui on main, #4491) and
  internal/gui's ten real-window AppKit tests killed at their deadlines
  under load (#2645, #4125). Neither touches this change.
- gofmt -l clean; go vet ./... (darwin), GOOS=windows go vet ./... and
  GOOS=linux go vet -tags nogui ./... ok on mote.
- Whole GUI suite on the Linux box: still running at push; reds are filed
  after.

Not tried: real Max and Pro accounts through the gateway; the Routing
page's own account row under By weight has no GUI test; the whole GUI
suite on macOS WebKit (mote was saturated by other sessions' suites).
2026-10-31 11:03:35 +08:00

92 lines
2.7 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 weight (rubbzha on X): a subscription routed by weight
// spreads its requests over its accounts as a provider's keys are (#841),
// each by the weight set beside it — a Max account allowing several times
// what a Pro one does takes that many requests for each the Pro one takes,
// so both are used to the end rather than the first run out mid-task. It
// is kept per account by its name in lower case (AccountWeights), as its
// other settings are; one with none weighs 1. Nothing sets one for the
// user, whatever the plan: the plans' allowances aren't told in requests.
import (
"fmt"
"strconv"
"strings"
)
// AccountWeight is the weight of the account user of p under Weighted
// routing, 1 when it has none. The accounts All and AlsoOn give carry it,
// as the gateway routes them; it is read from p alone, with no file read,
// as it is asked for every account of every request weighed.
func (p Provider) AccountWeight(user string) int {
return max(p.AccountWeights[accountKey(user)], 1)
}
// SetAccountWeight gives the account ref of the subscription id its weight
// under Weighted routing, 1 to MaxKeyWeight; 0 or 1 is the default, kept
// as none.
func SetAccountWeight(id, ref string, weight int) error {
if weight < 0 || weight > MaxKeyWeight {
return fmt.Errorf("an account's weight is 1 to %d, not %d", MaxKeyWeight, weight)
}
p, err := Find(id)
if err != nil {
return err
}
if p.Account == nil {
return fmt.Errorf("%s has keys, not subscription accounts: a key's weight is set on the key", p.Name)
}
r, ok := p.accountRef(ref)
if !ok {
return fmt.Errorf("%s has no account %q", p.Name, ref)
}
k := accountKey(r)
if max(p.AccountWeights[k], 1) == max(weight, 1) {
return nil
}
m := map[string]int{}
for u, w := range p.AccountWeights {
m[u] = w
}
if weight <= 1 {
delete(m, k)
} else {
m[k] = weight
}
p.AccountWeights = m
return Save(*p)
}
// ParseWeight reads a weight as the CLI takes it: a whole number from 1
// to MaxKeyWeight, or default (-, 0) for 1.
func ParseWeight(s string) (int, error) {
s = strings.ToLower(strings.TrimSpace(s))
switch s {
case "default", "-", "0":
return 1, nil
}
n, err := strconv.Atoi(s)
if err != nil || n < 1 || n > MaxKeyWeight {
return 0, fmt.Errorf("a weight is a whole number from 1 to %d, or default, not %q", MaxKeyWeight, s)
}
return n, nil
}
// normalAccountWeights is m as it is kept: keys in lower case, weights
// within 2–MaxKeyWeight, the default 1 left out; nil for none.
func normalAccountWeights(m map[string]int) map[string]int {
var out map[string]int
for u, w := range m {
u = accountKey(u)
if u == "" || w <= 1 {
continue
}
if out == nil {
out = map[string]int{}
}
out[u] = min(w, MaxKeyWeight)
}
return out
}