Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.
package gateway
import (
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
)
// Weekly pace 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: "[email protected]"}}
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["[email protected]"] = week(40, 5*24*time.Hour) // 60 left / 120 h = 0.5
share["[email protected]"] = week(80, time.Hour) // 20 left / 1 h = 20
if got := order(acct("[email protected]"), acct("[email protected]")); got != "ba" {
t.Fatalf("imminent renewal first: %s", got)
}
// renewing the same hour, pace is most left first
share["[email protected]"] = week(30, 76*time.Hour)
share["[email protected]"] = week(70, 76*time.Hour)
if got := order(acct("[email protected]"), acct("[email protected]")); got != "ab" {
t.Fatalf("same deadline, most left first: %s", got)
}
// within a tenth alike: their order, for the cache
share["[email protected]"] = week(50, 76*time.Hour)
share["[email protected]"] = week(54, 76*time.Hour)
if got := order(acct("[email protected]"), acct("[email protected]")); got != "ba" {
t.Fatalf("alike keep their order: %s", got)
}
// all but used up: last, though its pace is the highest
share["[email protected]"] = week(40, 5*24*time.Hour)
share["[email protected]"] = week(98, time.Hour) // 2 left / 1 h = 2 > 0.5
if got := order(acct("[email protected]"), acct("[email protected]")); 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["[email protected]"] = 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["[email protected]"] = week(0, 7*24*time.Hour)
if got := order(acct("[email protected]"), acct("[email protected]")); got != "ba" {
t.Fatalf("five hours running low wait: %s", got)
}
// two running low go by their share, ahead of one spent
share["[email protected]"] = 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["[email protected]"] = week(99, time.Hour)
if got := order(acct("[email protected]"), acct("[email protected]"), acct("[email protected]")); got != "cab" {
t.Fatalf("low by share, then the spent: %s", got)
}
delete(share, "[email protected]")
// Opus's own week decides an Opus request; Sonnet's goes by the general
share["[email protected]"] = 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["[email protected]"] = 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("[email protected]"), opus("[email protected]")}, "claude-opus-5-5", provider.Anthropic); got[0].p.Account.User != "[email protected]" {
t.Fatalf("Opus request goes by the Opus week: %s first", got[0].p.Account.User)
}
if got := order(acct("[email protected]"), acct("[email protected]")); 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, "[email protected]")
share["[email protected]"] = week(40, 5*24*time.Hour)
share["[email protected]"] = week(80, time.Hour)
if got := order(acct("[email protected]"), acct("[email protected]"), acct("[email protected]")); 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("[email protected]"), claude("[email protected]"), claude("[email protected]")}, "gpt-5.6-sol", provider.Chat); got[0].p.Account.User != "[email protected]" {
t.Fatalf("one that learns goes first: %s", got[0].p.Account.User)
}
// ...and by its pace once its answer has told
share["[email protected]"] = week(50, 5*24*time.Hour) // 0.42, behind a's 0.5
if got, _ := weigh(p, []candidate{claude("[email protected]"), claude("[email protected]"), claude("[email protected]")}, "gpt-5.6-sol", provider.Chat); got[0].p.Account.User != "[email protected]" || got[1].p.Account.User != "[email protected]" {
t.Fatalf("known, it goes by its pace: %s %s", got[0].p.Account.User, got[1].p.Account.User)
}
delete(share, "[email protected]")
// at 90% exactly, running low; just under, not
share["[email protected]"] = 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["[email protected]"] = 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("[email protected]"), acct("[email protected]")); 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["[email protected]"] = 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["[email protected]"] = week(60, 5*24*time.Hour)
if got := order(acct("[email protected]"), acct("[email protected]")); got != "ba" {
t.Fatalf("five hours spent: %s", got)
}
// both spent: the one with the least used first, it may yet answer
share["[email protected]"] = 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("[email protected]"), acct("[email protected]")); 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["[email protected]"] = provider.Allowance{{Used: 80, Resets: now.Add(time.Hour), Span: 5 * time.Hour}} // 20 / 1 h
share["[email protected]"] = provider.Allowance{{Used: 5, Resets: now.Add(time.Hour), Span: 5 * time.Hour}} // 95 / 1 h
share["[email protected]"] = provider.Allowance{{Used: 0, Resets: now.Add(time.Hour), Span: 5 * time.Hour}} // 100 / 1 h
delete(share, "[email protected]")
if got := order(acct("[email protected]"), acct("[email protected]"), acct("[email protected]"), acct("[email protected]")); 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["[email protected]"] = provider.Allowance{{Used: 0, Span: 5 * time.Hour}}
share["[email protected]"] = week(20, 5*24*time.Hour)
if got := order(acct("[email protected]"), acct("[email protected]")); got != "ab" {
t.Fatalf("five hours alone ahead of a far week: %s", got)
}
share["[email protected]"] = week(60, time.Hour)
if got := order(acct("[email protected]"), acct("[email protected]")); 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["[email protected]"] = week(0, 100*time.Hour) // 1.00
share["[email protected]"] = week(9, 100*time.Hour) // 0.91
share["[email protected]"] = 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("[email protected]"), 3000)
sent(acct("[email protected]"), 2000)
sent(acct("[email protected]"), 1000)
if got := order(acct("[email protected]"), acct("[email protected]"), acct("[email protected]")); got != "bac" {
t.Fatalf("bands from the top, tokens within: %s", got)
}
if got := order(acct("[email protected]"), acct("[email protected]"), acct("[email protected]")); 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["[email protected]"] = 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("[email protected]"), acct("[email protected]"), acct("[email protected]")); 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("[email protected]")}, "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["[email protected]"] = week(40, 5*24*time.Hour)
share["[email protected]"] = week(80, time.Hour)
got, wg := weigh(p, []candidate{acct("[email protected]"), acct("[email protected]")}, "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["[email protected]"][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("[email protected]"), acct("[email protected]")); got != "ab" {
t.Fatalf("least used is most left first: %s", got)
}
}