Files
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

76 lines
2.8 KiB
Go

package cli
import (
"encoding/base64"
"encoding/json"
"os"
"path/filepath"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
)
// magpie provider pool (plugins#349) sets a subscription's caps, Stop at
// the caps and Check usage near the cap from one command, and shows them;
// a bad value or an account the subscription hasn't changes nothing.
func TestPoolCmd(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)
if err := providerCmd([]string{"provider", "pool", "codex"}); err != nil {
t.Fatal(err)
}
if p, _ := provider.Find("codex"); p.CapStop || p.CapCareful {
t.Fatalf("on by default: stop %v careful %v", p.CapStop, p.CapCareful)
}
if err := providerCmd([]string{"provider", "pool", "codex", "--cap", "85%", "--stop=on", "--careful", "on"}); err != nil {
t.Fatal(err)
}
p, _ := provider.Find("codex")
if c := provider.AccountCapOf("codex", "me@example.com"); c != 85 || !p.CapStop || !p.CapCareful {
t.Fatalf("cap %d stop %v careful %v", c, p.CapStop, p.CapCareful)
}
for _, bad := range [][]string{
{"provider", "pool", "codex", "--stop", "maybe"},
{"provider", "pool", "codex", "--cap", "150"},
{"provider", "pool", "codex", "--accounts", "nobody@example.com", "--careful", "off"},
{"provider", "pool", "codex", "--careful"},
{"provider", "pool", "codex", "--fast", "on"},
{"provider", "pool"},
} {
if err := providerCmd(bad); err == nil {
t.Fatalf("%v taken", bad)
}
}
p, _ = provider.Find("codex")
if c := provider.AccountCapOf("codex", "me@example.com"); c != 85 || !p.CapStop || !p.CapCareful {
t.Fatalf("a refused command changed it: cap %d stop %v careful %v", c, p.CapStop, p.CapCareful)
}
if err := providerCmd([]string{"provider", "pool", "codex", "--accounts", "Me@Example.com", "--cap", "off", "--stop", "off"}); err != nil {
t.Fatal(err)
}
p, _ = provider.Find("codex")
if c := provider.AccountCapOf("codex", "me@example.com"); c != 0 || p.CapStop || !p.CapCareful {
t.Fatalf("cap %d stop %v careful %v", c, p.CapStop, p.CapCareful)
}
}