Files
magpie/internal/gateway/month_reach_test.go
T
yetone 3b541a7f36 routing: Smart ranks an account by its week once its month can't be used up before it renews (#5639)
Smart ranked a Volcengine Agent Plan (5 hours 2,000, week 7,000, month
20,000) by its month, the biggest window, so it always stood behind
accounts renewing weekly, and its month could reach a point where it
could no longer be used up before it renewed.

The rule keeps #1778's reasoning: a month holding less than its weeks add
up to is the real cap while the weeks can still carry it. Once what the
biggest window has left is more than the next window (a day or longer,
same unit, counts known) can let through between its own reset and the
biggest one's (Of x time between / Span), what the week leaves at its
reset is lost, and the week ranks the account.

- provider: Allowance.Unreached counts the biggest windows to pass over.
  Renewal and Seat.Soon are unchanged for other strategies and plugins.
- routes: Seat.Unreached; Smart compares Soon past it.
- gateway: weighRouted fills it; the trace's Weighed carries `unreached`.
- GUI Routing: renewsBy skips those windows, and a new story line says the
  shorter window decides. The Smart header and the group hint say so in
  en, zh, zh-TW, ja and de.
- docs: reference.md (How magpie picks an account) and
  subsystems/gateway-routing.md.

With the issue's numbers as reported (month 696 h away, 18,372 left),
four weeks carry 28,017, so the order is unchanged. Two weeks before the
month renews, 14,017 < 18,372, and the plan goes first, ahead of the
point where a week's leftover is lost.

Run on mote at this tree (base 2dac9b84e; origin/main bb33142ea touches
none of these files):
- go test -tags nogui ./internal/provider ./internal/routes: ok
- go test of TestMonthOutOfReachRanksByWeek,
  TestSmartSpendsAWeekTheMonthCantWait, TestSmartPassesAnUnreachedWindow
  and the Smart/Renewal/Weighed tests in provider, routes, gateway: ok
- GOOS=linux go vet -tags nogui ./... and GOOS=windows go vet ./...: ok
- gofmt -l internal/: clean
- node --test routing-key-windows.test.cjs: 52/52 in Chromium and WebKit,
  en/zh/ja/de, the new #5639 scene included

Broken by hand:
- Unreached returning 0: TestMonthOutOfReachRanksByWeek ("two weeks to
  go", "the last week") and TestSmartSpendsAWeekTheMonthCantWait went red.
- Smart comparing a.Soon, b.Soon whole: TestSmartPassesAnUnreachedWindow
  and TestSmartSpendsAWeekTheMonthCantWait went red.
- client.js without the slice and the story line: the #5639 GUI case went
  red (Chromium en).

Still running when pushed (the owner's rule not to hold a verified commit
behind the suite): the full internal/gateway package; all routing-*,
panel-routing*, gui-zh-tw/ja/de and i18n GUI files in both engines; and
the full go test ./... -timeout 45m. Any red they show gets its own issue.

Not tried: a real Volcengine Agent Plan account. Ark replies that send
only Percent carry no counts, so the rule can't apply to them. Not
changed: Use before expiry (Pace) still ranks this plan by its month;
filed as #5641.

Reported-by: louisgreen0726
Refs #5639
2026-11-08 02:08:08 +08:00

69 lines
3.0 KiB
Go

package gateway
import (
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
)
// In a group routed Smart, a Volcengine Agent Plan key (5 hours of 2,000,
// a week of 7,000, a month of 20,000) stands behind a Codex account whose
// week renews in three days while its month can still be used up — and
// goes first once the weeks left before the month renews can't carry
// what the month has left, as what its week leaves is lost at its reset
// then (#5639, louisgreen0726's numbers). The Routing page is told which
// window ranks it.
func TestSmartSpendsAWeekTheMonthCantWait(t *testing.T) {
now := time.Now()
old := allowances
t.Cleanup(func() { allowances = old })
allowances = func(string) map[string]provider.Allowance {
return map[string]provider.Allowance{"me@example.com": {{Used: 10, Resets: now.Add(3 * 24 * time.Hour), Span: 7 * 24 * time.Hour}}}
}
weekRenews := now.Add(23*time.Hour + 36*time.Minute)
agentPlan := func(weekUsed, monthUsed float64, month time.Time) provider.Allowance {
return provider.Allowance{
{Used: 0, Span: 5 * time.Hour, Of: 2000, Name: "5 hours"},
{Used: weekUsed / 70, Amount: weekUsed, Of: 7000, Resets: weekRenews, Span: 7 * 24 * time.Hour, Name: "Weekly"},
{Used: monthUsed / 200, Amount: monthUsed, Of: 20000, Resets: month, Span: 30 * 24 * time.Hour, Name: "Month"},
}
}
of := map[string]provider.Allowance{}
keyWindows(t, of)
codex := provider.Provider{ID: "codex", Account: &provider.Account{Agent: "codex", User: "me@example.com"}}
ark := provider.Provider{ID: "volcengine", Key: "ark-key", Anthropic: "https://ark.cn-beijing.volces.com/api/plan"}
heads := []candidate{
{p: codex, model: "gpt-6-astra", rest: "codex"},
{p: ark, model: "doubao-seed-2.0-pro", rest: "volcengine"},
}
g := provider.Provider{ID: provider.GroupPrefix + "g"}
// as reported: 18,372 of the month left, four weeks to carry it
of["ark-key"] = agentPlan(1628, 1628, now.Add(696*time.Hour))
got, wg := weigh(g, heads, "", provider.Responses)
if restsOf(got) != "codex volcengine " {
t.Fatalf("the month within reach: %s", restsOf(got))
}
if w := weighed(got[1], ark, wg, false, provider.Responses); w.Unreached != 0 {
t.Fatalf("the month within reach, traced: %+v", w)
}
// two weeks on, the plan not used: two weeks hold 14,000 of 18,372
of["ark-key"] = agentPlan(0, 1628, weekRenews.Add(2*7*24*time.Hour+24*time.Minute))
got, wg = weigh(g, heads, "", provider.Responses)
if restsOf(got) != "volcengine codex " {
t.Fatalf("the month out of reach: %s", restsOf(got))
}
if w := weighed(got[0], ark, wg, false, provider.Responses); w.Unreached != 1 || len(w.Renews) != 3 {
t.Fatalf("the month out of reach, traced: %+v", w)
}
// a month holding less than its weeks add up to, with 9,000 left
// that two weeks carry: the month ranks it, as before (#1778)
of["ark-key"] = agentPlan(0, 11000, weekRenews.Add(2*7*24*time.Hour))
if got, _ := weigh(g, heads, "", provider.Responses); restsOf(got) != "codex volcengine " {
t.Fatalf("the month the real cap: %s", restsOf(got))
}
}