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).
82 lines
2.7 KiB
Go
82 lines
2.7 KiB
Go
package cli
|
||
|
||
import (
|
||
"encoding/base64"
|
||
"encoding/json"
|
||
"os"
|
||
"path/filepath"
|
||
"testing"
|
||
"time"
|
||
|
||
"github.com/yetone/magpie/internal/provider"
|
||
)
|
||
|
||
// magpie provider account-weight gives a subscription account its share
|
||
// of the requests under routing=weight (rubbzha on X: a Max account 5, a
|
||
// Pro one 1), default takes it back to 1; a weight out of 1–1000, an
|
||
// account the subscription hasn't, or a provider with keys is an error.
|
||
func TestAccountWeightCmd(t *testing.T) {
|
||
home := t.TempDir()
|
||
t.Setenv("HOME", home)
|
||
t.Setenv("USERPROFILE", home)
|
||
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
||
t.Setenv("XDG_CACHE_HOME", t.TempDir())
|
||
t.Setenv("DSH_HOME", "")
|
||
jwt := func(m map[string]any) string {
|
||
b, _ := json.Marshal(m)
|
||
return "h." + base64.RawURLEncoding.EncodeToString(b) + ".s"
|
||
}
|
||
auth, _ := json.Marshal(map[string]any{"auth_mode": "chatgpt", "tokens": map[string]any{
|
||
"id_token": jwt(map[string]any{"email": "me@example.com"}),
|
||
"access_token": jwt(map[string]any{"exp": time.Now().Add(time.Hour).Unix()}),
|
||
"refresh_token": "r", "account_id": "acct-1"}})
|
||
os.MkdirAll(filepath.Join(home, ".codex"), 0o755)
|
||
if err := os.WriteFile(filepath.Join(home, ".codex", "auth.json"), auth, 0o600); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
provider.ForgetAccounts()
|
||
t.Cleanup(provider.ForgetAccounts)
|
||
weight := func() int {
|
||
p, err := provider.Find("codex")
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
return p.AccountWeight("me@example.com")
|
||
}
|
||
|
||
if err := providerCmd([]string{"provider", "account-weight", "codex", "Me@Example.com", "5"}); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if w := weight(); w != 5 {
|
||
t.Fatalf("weight %d", w)
|
||
}
|
||
for _, args := range [][]string{{"provider", "account-weight", "codex"}, {"provider", "account-weight", "codex", "me@example.com"}} {
|
||
if err := providerCmd(args); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
}
|
||
for _, bad := range []string{"1.5", "1001", "-2", "lots"} {
|
||
if err := providerCmd([]string{"provider", "account-weight", "codex", "me@example.com", bad}); err == nil {
|
||
t.Fatalf("%s taken", bad)
|
||
}
|
||
}
|
||
if w := weight(); w != 5 {
|
||
t.Fatalf("a refused weight changed it to %d", w)
|
||
}
|
||
if err := providerCmd([]string{"provider", "account-weight", "codex", "nobody@example.com", "2"}); err == nil {
|
||
t.Fatal("an account it hasn't was weighed")
|
||
}
|
||
if err := providerCmd([]string{"provider", "account-weight", "codex", "me@example.com", "default"}); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if w := weight(); w != 1 {
|
||
t.Fatalf("default left %d", w)
|
||
}
|
||
if err := provider.Save(provider.Provider{ID: "relay", Name: "Relay", Key: "k1", Chat: "http://127.0.0.1:1/v1"}); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if err := providerCmd([]string{"provider", "account-weight", "relay", "k1", "3"}); err == nil {
|
||
t.Fatal("a key provider's account was weighed")
|
||
}
|
||
}
|