lcyksp on Discord: the 「重置前用完」 strategy (pace) ranked a WorkBuddy
account by its monthly 500 credits ("积分 0 / 500 · 已用 0% · 28 天后重置").
WorkBuddy spends check-in credits first because they expire sooner
("70.85 积分 将于 2026/11/3 09:28:52 过期"), and the account was ranked as
if nothing would be lost for 28 days.
What changed:
- provider.Allowance.Pace takes a budget window's soonest expiry into
account (Limit.RunsOut, from QuotaWindow.RunsOutAt, which the
WorkBuddy plugin and others set on the credits window). When that
expiry comes before the window's reset, the window's pace is the
higher of:
- what is left per hour until the reset;
- the share that expires (QuotaWindow.RunsOut, now carried as
Limit.RunsOutLeft) per hour until it expires. When the amount isn't
told, all that is left counts. A share is capped at 100%, and the
hours at no less than one.
The account whose credits are lost soonest per hour goes first.
Expiry at or after the reset, or already past, changes nothing.
Zero-config: nothing new to set.
- The trace says DueBy "expiry" when the expiry set the deadline. The
Routing page's reason then reads "Use before expiry: {who} has the most
credits expiring per hour until they expire — {n} of its allowance
expires in {d}."
- Label: "Weekly pace" / 重置前用完 / 週ペース / Wochentempo becomes
"Use before expiry" / 到期前用完 (zh, zh-TW) / 期限前に使い切る /
Vor Ablauf aufbrauchen. Product call: the strategy's goal was always
"lose nothing at the deadline". A reset and an expiry are both such a
deadline, and "到期" covers both, where 「重置」 names only one. The id
stays pace, so saved groups are unchanged.
- The descriptions (Routing, Settings, group editor), docs/reference.md,
docs/subsystems/gateway-routing.md and the site's start page (en, zh,
ja) say the same.
Go, run on mote at base f95bacdfb:
- gofmt -l internal plugins: clean.
- go test -count=1 -timeout 45m on ./internal/provider,
./internal/gateway/..., ./internal/routes, ./plugins/..., ./internal/gui,
./internal/tui and ./internal/cli: all ok except three reds that are
filed and don't touch routing or pace:
- TestCodexPoolSaysTheClientsVersion (#4807)
- TestMainWindowCorners (#3949)
- TestEveryRouteHasACommandOrIsNamed (#4826)
- GOOS=linux and GOOS=windows go vet on the same packages: ok.
- GUI: the touched files (routing-pace-expiry, routing-save,
routing-group-strategy, routing-reset-deadline,
routing-wallet-balance, builtins), chromium and webkit: 90 pass, 0
fail.
- GUI on WSL magpie-desktop (Linux), after the rebase onto 9c58c87e8:
the 387 files that name routing, i18n, strategy or reason text,
chromium and webkit: 6211 tests, 6012 pass, 199 fail in 54 files.
Another session's full suite was running beside it.
- Those 54 files again on mote, with this change and at 9c58c87e8
without it: 100 and 99 reds. They are the same tests, differing only in
which language went red. Filed as #4932. routing-group-strategy, whose
one WSL red was a 9px webkit position, passed both times.
- After the last rebase, onto d29f02d65 (it changed gateway/routing.go,
i18n.js and app.js), on mote: gofmt clean.
- go test ./internal/provider ./internal/routes ./plugins/rotation/...:
ok.
- go test -run "Pace|Reset|Deadline|Expir|Routing|Weekly|Strategy|Order|Allowance"
./internal/gateway: ok.
- linux and windows vet: ok.
- GUI routing-pace-expiry, routing-save, routing-group-strategy,
routing-reset-deadline, routing-wallet-balance, builtins and
i18n-keys: 94 pass, 0 fail.
- c47fc516a (Tencent Cloud's Token Plan) landed after that run and was
not run again with this change.
Broken by hand, each red:
- The expiry branch in Pace taken out: TestPaceCreditsExpiring and
TestPaceGoesByCreditsExpiring.
- dueExpiry left unset: TestPaceGoesByCreditsExpiring's trace check.
- RunsOutLeft not carried: TestAllowanceCarriesRunsOutLeft.
- client.js's expiry reason line taken out: routing-pace-expiry, 0 of 10
pass in chromium.
Not tried:
- A live WorkBuddy account with check-in credits about to expire. The
fixtures use lcyksp's numbers (500 monthly, 70.85 expiring).
- The site pages are not deployed by this commit.
234 lines
11 KiB
Go
234 lines
11 KiB
Go
package gateway
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// Use before expiry puts first the account with the most of its week left per
|
|
// hour until it renews — the one with the most to lose at its reset — not
|
|
// the one with the most left: 20% left and an hour to go beats 60% left
|
|
// and five days. Alike within a tenth go by the tokens sent lately, then
|
|
// keep their order; one at 90% or more of any window waits behind the
|
|
// rest, one all but used up behind those, each by how far; one not known
|
|
// counts as a fresh week; one with the five hours alone goes by what
|
|
// they have left per hour until they renew (#576); one that tells as it
|
|
// answers goes first.
|
|
//
|
|
// PIN (2026-10-01): weekly pace is its own option, "pace"; Least used
|
|
// keeps its most-left-first meaning and Smart is left as it is (yetone,
|
|
// PR #517 review).
|
|
func TestWeeklyPace(t *testing.T) {
|
|
old := allowances
|
|
defer func() { allowances = old }()
|
|
now := time.Now()
|
|
share := map[string]provider.Allowance{}
|
|
allowances = func(string) map[string]provider.Allowance { return share }
|
|
p := provider.Provider{ID: "codex", Routing: provider.Pace, Account: &provider.Account{Agent: "codex", User: "a@x.com"}}
|
|
acct := func(user string) candidate {
|
|
return candidate{p: provider.Provider{ID: "codex", Account: &provider.Account{Agent: "codex", User: user}}, model: "gpt-5.6-sol", rest: "codex#" + user}
|
|
}
|
|
week := func(used float64, in time.Duration) provider.Allowance {
|
|
return provider.Allowance{
|
|
{Used: 10, Resets: now.Add(time.Hour), Span: 5 * time.Hour},
|
|
{Used: used, Resets: now.Add(in), Span: 7 * 24 * time.Hour},
|
|
}
|
|
}
|
|
order := func(cs ...candidate) string {
|
|
got, _ := weigh(p, cs, "gpt-5.6-sol", provider.Chat)
|
|
var s []string
|
|
for _, c := range got {
|
|
s = append(s, c.p.Account.User[:1])
|
|
}
|
|
return strings.Join(s, "")
|
|
}
|
|
|
|
// the owner's case: 80% used renewing in an hour has the most to lose
|
|
share["a@x.com"] = week(40, 5*24*time.Hour) // 60 left / 120 h = 0.5
|
|
share["b@x.com"] = week(80, time.Hour) // 20 left / 1 h = 20
|
|
if got := order(acct("a@x.com"), acct("b@x.com")); got != "ba" {
|
|
t.Fatalf("imminent renewal first: %s", got)
|
|
}
|
|
// renewing the same hour, pace is most left first
|
|
share["a@x.com"] = week(30, 76*time.Hour)
|
|
share["b@x.com"] = week(70, 76*time.Hour)
|
|
if got := order(acct("b@x.com"), acct("a@x.com")); got != "ab" {
|
|
t.Fatalf("same deadline, most left first: %s", got)
|
|
}
|
|
// within a tenth alike: their order, for the cache
|
|
share["a@x.com"] = week(50, 76*time.Hour)
|
|
share["b@x.com"] = week(54, 76*time.Hour)
|
|
if got := order(acct("b@x.com"), acct("a@x.com")); got != "ba" {
|
|
t.Fatalf("alike keep their order: %s", got)
|
|
}
|
|
// all but used up: last, though its pace is the highest
|
|
share["a@x.com"] = week(40, 5*24*time.Hour)
|
|
share["b@x.com"] = week(98, time.Hour) // 2 left / 1 h = 2 > 0.5
|
|
if got := order(acct("b@x.com"), acct("a@x.com")); got != "ab" {
|
|
t.Fatalf("spent last: %s", got)
|
|
}
|
|
// its five hours at 90%, its week healthy and renewing soon: behind
|
|
// the fresh account all the same — the five hours are a rate cap it
|
|
// would be sent into before its allowance was read again
|
|
share["a@x.com"] = provider.Allowance{
|
|
{Used: 95, Resets: now.Add(time.Hour), Span: 5 * time.Hour},
|
|
{Used: 10, Resets: now.Add(2 * time.Hour), Span: 7 * 24 * time.Hour},
|
|
}
|
|
share["b@x.com"] = week(0, 7*24*time.Hour)
|
|
if got := order(acct("a@x.com"), acct("b@x.com")); got != "ba" {
|
|
t.Fatalf("five hours running low wait: %s", got)
|
|
}
|
|
// two running low go by their share, ahead of one spent
|
|
share["c@x.com"] = provider.Allowance{{Used: 92, Resets: now.Add(time.Hour), Span: 5 * time.Hour}, {Used: 10, Resets: now.Add(2 * time.Hour), Span: 7 * 24 * time.Hour}}
|
|
share["b@x.com"] = week(99, time.Hour)
|
|
if got := order(acct("b@x.com"), acct("a@x.com"), acct("c@x.com")); got != "cab" {
|
|
t.Fatalf("low by share, then the spent: %s", got)
|
|
}
|
|
delete(share, "c@x.com")
|
|
// Opus's own week decides an Opus request; Sonnet's goes by the general
|
|
share["a@x.com"] = provider.Allowance{
|
|
{Used: 10, Resets: now.Add(2 * 24 * time.Hour), Span: 7 * 24 * time.Hour},
|
|
{Used: 95, Resets: now.Add(2 * 24 * time.Hour), Span: 7 * 24 * time.Hour, Model: "opus"},
|
|
}
|
|
share["b@x.com"] = provider.Allowance{
|
|
{Used: 60, Resets: now.Add(2 * 24 * time.Hour), Span: 7 * 24 * time.Hour},
|
|
{Used: 60, Resets: now.Add(2 * 24 * time.Hour), Span: 7 * 24 * time.Hour, Model: "opus"},
|
|
}
|
|
opus := func(user string) candidate {
|
|
c := acct(user)
|
|
c.model = "claude-opus-5-5"
|
|
return c
|
|
}
|
|
if got, _ := weigh(p, []candidate{opus("a@x.com"), opus("b@x.com")}, "claude-opus-5-5", provider.Anthropic); got[0].p.Account.User != "b@x.com" {
|
|
t.Fatalf("Opus request goes by the Opus week: %s first", got[0].p.Account.User)
|
|
}
|
|
if got := order(acct("b@x.com"), acct("a@x.com")); got != "ab" {
|
|
t.Fatalf("a Sonnet request goes by the general week: %s", got)
|
|
}
|
|
// one not known counts as a fresh week: ahead of one half through its,
|
|
// behind one about to lose what it has
|
|
delete(share, "c@x.com")
|
|
share["a@x.com"] = week(40, 5*24*time.Hour)
|
|
share["b@x.com"] = week(80, time.Hour)
|
|
if got := order(acct("a@x.com"), acct("c@x.com"), acct("b@x.com")); got != "bca" {
|
|
t.Fatalf("not known as a fresh week: %s", got)
|
|
}
|
|
// one not known that tells what it has left as it answers (a Claude
|
|
// account) goes first, once: else it would never be known
|
|
claude := func(user string) candidate {
|
|
c := acct(user)
|
|
c.p.Account.Agent, c.rest = "claude", "claude#"+user
|
|
return c
|
|
}
|
|
if got, _ := weigh(p, []candidate{claude("a@x.com"), claude("b@x.com"), claude("c@x.com")}, "gpt-5.6-sol", provider.Chat); got[0].p.Account.User != "c@x.com" {
|
|
t.Fatalf("one that learns goes first: %s", got[0].p.Account.User)
|
|
}
|
|
// ...and by its pace once its answer has told
|
|
share["c@x.com"] = week(50, 5*24*time.Hour) // 0.42, behind a's 0.5
|
|
if got, _ := weigh(p, []candidate{claude("c@x.com"), claude("a@x.com"), claude("b@x.com")}, "gpt-5.6-sol", provider.Chat); got[0].p.Account.User != "b@x.com" || got[1].p.Account.User != "a@x.com" {
|
|
t.Fatalf("known, it goes by its pace: %s %s", got[0].p.Account.User, got[1].p.Account.User)
|
|
}
|
|
delete(share, "c@x.com")
|
|
// at 90% exactly, running low; just under, not
|
|
share["a@x.com"] = provider.Allowance{{Used: 90, Resets: now.Add(time.Hour), Span: 5 * time.Hour}, {Used: 10, Resets: now.Add(2 * time.Hour), Span: 7 * 24 * time.Hour}}
|
|
share["b@x.com"] = provider.Allowance{{Used: 89.9, Resets: now.Add(time.Hour), Span: 5 * time.Hour}, {Used: 60, Resets: now.Add(5 * 24 * time.Hour), Span: 7 * 24 * time.Hour}}
|
|
if got := order(acct("a@x.com"), acct("b@x.com")); got != "ba" {
|
|
t.Fatalf("90%% is running low, 89.9%% is not: %s", got)
|
|
}
|
|
// its five hours all but used up, its week healthy: last all the same
|
|
share["a@x.com"] = provider.Allowance{
|
|
{Used: 98, Resets: now.Add(time.Hour), Span: 5 * time.Hour},
|
|
{Used: 10, Resets: now.Add(time.Hour), Span: 7 * 24 * time.Hour},
|
|
}
|
|
share["b@x.com"] = week(60, 5*24*time.Hour)
|
|
if got := order(acct("a@x.com"), acct("b@x.com")); got != "ba" {
|
|
t.Fatalf("five hours spent: %s", got)
|
|
}
|
|
// both spent: the one with the least used first, it may yet answer
|
|
share["b@x.com"] = provider.Allowance{{Used: 100, Resets: now.Add(time.Hour), Span: 5 * time.Hour}, {Used: 10, Resets: now.Add(time.Hour), Span: 7 * 24 * time.Hour}}
|
|
if got := order(acct("b@x.com"), acct("a@x.com")); got != "ab" {
|
|
t.Fatalf("spent by how far: %s", got)
|
|
}
|
|
// the account has the five hours alone (Claude Enterprise): what they
|
|
// have left is lost within the hour, so it goes by that per hour,
|
|
// ahead of the one not known (a fresh week); alike within a tenth
|
|
// keep their order (#576)
|
|
share["a@x.com"] = provider.Allowance{{Used: 80, Resets: now.Add(time.Hour), Span: 5 * time.Hour}} // 20 / 1 h
|
|
share["b@x.com"] = provider.Allowance{{Used: 5, Resets: now.Add(time.Hour), Span: 5 * time.Hour}} // 95 / 1 h
|
|
share["c@x.com"] = provider.Allowance{{Used: 0, Resets: now.Add(time.Hour), Span: 5 * time.Hour}} // 100 / 1 h
|
|
delete(share, "d@x.com")
|
|
if got := order(acct("a@x.com"), acct("d@x.com"), acct("c@x.com"), acct("b@x.com")); got != "cbad" {
|
|
t.Fatalf("five hours alone go by what they lose at their reset: %s", got)
|
|
}
|
|
// five hours alone, not started, against a week with 80% left and
|
|
// five days to go: the five hours first (100 / 5 h beats 80 / 120 h);
|
|
// a week with 40% left renewing in an hour beats them (40 / 1 h)
|
|
share["a@x.com"] = provider.Allowance{{Used: 0, Span: 5 * time.Hour}}
|
|
share["b@x.com"] = week(20, 5*24*time.Hour)
|
|
if got := order(acct("b@x.com"), acct("a@x.com")); got != "ab" {
|
|
t.Fatalf("five hours alone ahead of a far week: %s", got)
|
|
}
|
|
share["b@x.com"] = week(60, time.Hour)
|
|
if got := order(acct("a@x.com"), acct("b@x.com")); got != "ba" {
|
|
t.Fatalf("a week renewing sooner with more to lose first: %s", got)
|
|
}
|
|
// three paces a twelfth apart: alike pairwise all round, yet an order
|
|
// — bands drawn from the top, so the first two are alike and the
|
|
// third is not; within a band, the tokens sent lately decide
|
|
share["a@x.com"] = week(0, 100*time.Hour) // 1.00
|
|
share["b@x.com"] = week(9, 100*time.Hour) // 0.91
|
|
share["c@x.com"] = week(17, 100*time.Hour) // 0.83
|
|
sent := func(c candidate, n float64) {
|
|
routed.Lock()
|
|
defer routed.Unlock()
|
|
if _, had := routed.used[c.restKey()]; !had {
|
|
t.Cleanup(func() { routed.Lock(); delete(routed.used, c.restKey()); routed.Unlock() })
|
|
}
|
|
routed.used[c.restKey()] = tokenUse{n, now}
|
|
}
|
|
sent(acct("a@x.com"), 3000)
|
|
sent(acct("b@x.com"), 2000)
|
|
sent(acct("c@x.com"), 1000)
|
|
if got := order(acct("a@x.com"), acct("b@x.com"), acct("c@x.com")); got != "bac" {
|
|
t.Fatalf("bands from the top, tokens within: %s", got)
|
|
}
|
|
if got := order(acct("c@x.com"), acct("b@x.com"), acct("a@x.com")); got != "bac" {
|
|
t.Fatalf("the same from the other end: %s", got)
|
|
}
|
|
// one running low doesn't draw the bands for those ahead of it: its
|
|
// pace 1.05 would part a (1.00) from b (0.91), and a's tokens lose
|
|
share["c@x.com"] = provider.Allowance{{Used: 95, Resets: now.Add(time.Hour), Span: 5 * time.Hour}, {Used: 16, Resets: now.Add(80 * time.Hour), Span: 7 * 24 * time.Hour}}
|
|
if got := order(acct("a@x.com"), acct("b@x.com"), acct("c@x.com")); got != "bac" {
|
|
t.Fatalf("the bands are the healthy ones' alone: %s", got)
|
|
}
|
|
// keys have no week: by the tokens sent lately
|
|
key := func(id string) candidate {
|
|
return candidate{p: provider.Provider{ID: id, Key: "k"}, model: "gpt-5.6-sol", rest: id}
|
|
}
|
|
sent(key("k1"), 500)
|
|
sent(key("k2"), 100)
|
|
kp := provider.Provider{ID: "keys", Routing: provider.Pace}
|
|
if got, _ := weigh(kp, []candidate{key("k1"), key("k2")}, "gpt-5.6-sol", provider.Chat); got[0].p.ID != "k2" {
|
|
t.Fatalf("keys by tokens: %s first", got[0].p.ID)
|
|
}
|
|
// a key follows the subscriptions with a week
|
|
if got, _ := weigh(p, []candidate{key("k2"), acct("a@x.com")}, "gpt-5.6-sol", provider.Chat); got[0].p.Account == nil {
|
|
t.Fatal("a key before a subscription")
|
|
}
|
|
// the trace tells the pace and the window it went by, the week's
|
|
share["a@x.com"] = week(40, 5*24*time.Hour)
|
|
share["b@x.com"] = week(80, time.Hour)
|
|
got, wg := weigh(p, []candidate{acct("a@x.com"), acct("b@x.com")}, "gpt-5.6-sol", provider.Chat)
|
|
if w := weighed(got[0], p, wg, false, provider.Chat); w.Pace < 19 || w.Pace > 21 || w.Due == nil || !w.Due.Equal(share["b@x.com"][1].Resets) {
|
|
t.Fatalf("the trace: pace %v due %v", w.Pace, w.Due)
|
|
}
|
|
// Least used keeps its meaning: the most left first, whatever renews when
|
|
p.Routing = provider.LeastUsed
|
|
if got := order(acct("b@x.com"), acct("a@x.com")); got != "ab" {
|
|
t.Fatalf("least used is most left first: %s", got)
|
|
}
|
|
}
|