Files
magpie/internal/provider/codex_reset_deadline_test.go
T
yetone 95d0c97bf9 routing: "Use before expiry" counts credits that expire before the reset (lcyksp)
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.
2026-11-02 21:52:57 +08:00

152 lines
6.1 KiB
Go

package provider
import (
"context"
"testing"
"time"
)
// An account that spends a Codex reset about to run out by itself has its
// windows started again then, resetLead before the reset runs out:
// Use before expiry goes by the hours until that, when it is sooner than the week
// renews, and Smart's renewals do too (#717, #718). A reset that runs out
// after the week renews, or has run out, changes nothing.
func TestPaceUntilAutoUsedReset(t *testing.T) {
now := time.Now()
near := func(got, want float64) bool { return got > want*0.99 && got < want*1.01 }
week := func(used float64, renews, runsOut time.Duration) Allowance {
a := Allowance{
{Used: 10, Resets: now.Add(2 * time.Hour), Span: 5 * time.Hour},
{Used: used, Resets: now.Add(renews), Span: 7 * 24 * time.Hour},
}
if runsOut != 0 {
a = a.restartedBy(now.Add(runsOut))
}
return a
}
// the issue's A: 80% left, the week renewing in five days, a reset
// running out in an hour and a half — spent in one
a := week(20, 120*time.Hour, 90*time.Minute)
if p, due := a.Pace("m", now); !near(p, 80) || !due.Equal(now.Add(time.Hour)) {
t.Fatalf("by the reset spent: %v %v", p, due)
}
if r := a.Renewal("m", now); len(r) != 2 || !r[0].Equal(now.Add(time.Hour)) || !r[1].Equal(now.Add(time.Hour)) {
t.Fatalf("renewals by the reset spent: %v", r)
}
// none to spend: by the week, as before
if p, due := week(20, 120*time.Hour, 0).Pace("m", now); !near(p, 80.0/120) || !due.Equal(now.Add(120*time.Hour)) {
t.Fatalf("no reset: %v %v", p, due)
}
// the week renews first: by the week
b := week(50, 48*time.Hour, 20*24*time.Hour)
if p, due := b.Pace("m", now); !near(p, 50.0/48) || !due.Equal(now.Add(48*time.Hour)) {
t.Fatalf("week sooner: %v %v", p, due)
}
if r := b.Renewal("m", now); !r[0].Equal(now.Add(48 * time.Hour)) {
t.Fatalf("week sooner, renewals: %v", r)
}
// within the lead already: spent on the next look
if p, due := week(20, 120*time.Hour, 20*time.Minute).Pace("m", now); !near(p, 80) || !due.Equal(now) {
t.Fatalf("within the lead: %v %v", p, due)
}
// run out already: nothing to spend
if p, _ := week(20, 120*time.Hour, -time.Minute).Pace("m", now); !near(p, 80.0/120) {
t.Fatalf("ran out: %v", p)
}
// five hours alone are cut short too: the reset runs out in two and a
// half hours, spent in two
five := Allowance{{Used: 40, Resets: now.Add(4 * time.Hour), Span: 5 * time.Hour}}.restartedBy(now.Add(150 * time.Minute))
if p, due := five.Pace("m", now); !near(p, 60.0/2) || !due.Equal(now.Add(2*time.Hour)) {
t.Fatalf("five hours alone: %v %v", p, due)
}
}
// A reset counts only for an account the user lets spend its resets, that
// holds one that runs out, and whose windows have been used — as the
// spending itself (check). The allowances routing reads carry it.
func TestResetRunsOutForRouting(t *testing.T) {
signIn(t)
now := time.Now()
at := func(d time.Duration) *time.Time { u := now.Add(d); return &u }
until := now.Add(4 * time.Hour)
used := []QuotaWindow{{Span: 7 * 24 * time.Hour, Used: 20, ResetsAt: at(120 * time.Hour)}}
unused := []QuotaWindow{{Span: 7 * 24 * time.Hour, Used: 0, ResetsAt: at(120 * time.Hour)}}
aside := []QuotaWindow{{Span: 30 * 24 * time.Hour, Used: 50, Aside: true}}
held := &ResetCredits{Count: 1, Until: &until}
if got := resetRunsOut("codex", "me@example.com", used, held); !got.IsZero() {
t.Fatalf("auto-use off: %v", got)
}
if err := SetCodexAutoReset("me@example.com", true); err != nil {
t.Fatal(err)
}
for name, c := range map[string]struct {
agent string
windows []QuotaWindow
resets *ResetCredits
want time.Time
}{
"on, used": {"codex", used, held, until},
"none held": {"codex", used, nil, time.Time{}},
"count none": {"codex", used, &ResetCredits{Until: &until}, time.Time{}},
"never runs out": {"codex", used, &ResetCredits{Count: 1}, time.Time{}},
"windows unused": {"codex", unused, held, time.Time{}},
"on-demand only used": {"codex", aside, held, time.Time{}},
"another vendor's": {"claude", used, held, time.Time{}},
} {
if got := resetRunsOut(c.agent, "Me@example.com", c.windows, c.resets); !got.Equal(c.want) {
t.Errorf("%s: %v, want %v", name, got, c.want)
}
}
LoginUsageVia(func(context.Context, string) map[string]SubscriptionQuota {
return map[string]SubscriptionQuota{
"me@example.com": {Windows: used, Resets: held},
"other@example.com": {Windows: used, Resets: held},
}
})
reset := func() {
usedCache.Lock()
usedCache.m, usedCache.at, usedCache.loading = map[string]map[string]Allowance{}, map[string]time.Time{}, map[string]chan struct{}{}
usedCache.stale = nil
usedCache.Unlock()
}
reset()
t.Cleanup(func() { LoginUsageVia(nil); reset() })
oldWait := firstWait
firstWait = 5 * time.Second
t.Cleanup(func() { firstWait = oldWait })
all := Allowances("codex")
if got := all["me@example.com"].Restarts(now); !got.Equal(until.Add(-resetLead())) {
t.Fatalf("allowance read: restarts %v", got)
}
if got := all["other@example.com"].Restarts(now); !got.IsZero() {
t.Fatalf("an account not let spend its resets: restarts %v", got)
}
}
// Routing takes the reset as spent when check spends it: half an hour
// before it runs out, or now when the account is held up past then (#718).
func TestRestartsWhenHeldUp(t *testing.T) {
now := time.Now()
acct := func(fiveUsed float64, fiveBack time.Duration) Allowance {
five := Limit{Used: fiveUsed, Span: 5 * time.Hour}
if fiveBack != 0 {
five.Resets = now.Add(fiveBack)
}
return Allowance{five, {Used: 32, Resets: now.Add(120 * time.Hour), Span: 7 * 24 * time.Hour}}.restartedBy(now.Add(3 * time.Hour))
}
for name, c := range map[string]struct {
a Allowance
want time.Time
}{
"free again before it is spent": {acct(100, 110*time.Minute), now.Add(150 * time.Minute)},
"held up past it": {acct(100, 160*time.Minute), now},
"held up, not saying until when": {acct(100, 0), now},
"not held up": {acct(60, 110*time.Minute), now.Add(150 * time.Minute)},
} {
if got := c.a.Restarts(now); !got.Equal(c.want) {
t.Errorf("%s: restarts %v, want %v", name, got, c.want)
}
}
}