diff --git a/internal/provider/codex_warmup.go b/internal/provider/codex_warmup.go index d0e984091..e1d7ce04d 100644 --- a/internal/provider/codex_warmup.go +++ b/internal/provider/codex_warmup.go @@ -6,7 +6,9 @@ package provider // its next week end a day later too. So, while the setting is on, whichever // magpie runs the gateway looks at each account's windows every few minutes // and, when one has started over — its reset time passed, or its use fell -// back (OpenAI resetting everyone's limits early) — and nothing has used it +// back (OpenAI resetting everyone's limits early), or, unused, it is no +// longer started and resets later than it ran to (a reset that follows a +// reset card's with nothing used in between) — and nothing has used it // since, sends that account one tiny request, as the gateway sends any. // Once per reset: what it saw and did is kept in codex-warmup.json, so a // restart doesn't send it again. A window still not started after one — @@ -319,7 +321,9 @@ func asOf(w QuotaWindow, now time.Time) QuotaWindow { // warmDue says whether a window seen as p (seen false the first time) and // now as cur wants starting: it is unused, and has started over since p — // or is seen for the first time. Use falling back is a reset whatever the -// window says, OpenAI resetting everyone's limits early among them. One +// window says, OpenAI resetting everyone's limits early among them; so is +// a window seen running, unused, that now isn't started and resets later +// than it was to, which has no use to fall from. One // whose warm-up failed is still due, and one still not started is due // again once its Retry comes, even with a reset time reported. func warmDue(p warmWindow, seen bool, cur QuotaWindow, now time.Time) bool { @@ -332,6 +336,12 @@ func warmDue(p warmWindow, seen bool, cur QuotaWindow, now time.Time) bool { return !now.Before(p.Retry) case p.Used-cur.Used >= 1: return true + case !p.ResetsAt.IsZero() && idle(cur, now) && movedOn(p, cur): + // not started now, its reset gone from the one it ran to: started + // over with nothing to fall from, as a window a reset was spent on + // and a warm-up started, unused since, when OpenAI then resets + // everyone's limits early + return true case !p.ResetsAt.IsZero() && !now.Before(p.ResetsAt): return idle(cur, now) case p.ResetsAt.IsZero() && p.Used == 0: @@ -340,6 +350,21 @@ func warmDue(p warmWindow, seen bool, cur QuotaWindow, now time.Time) bool { return false } +// movedOn says whether a window seen as p now resets (cur) later than +// both the reset it was seen running to and the end of a window magpie's +// last warm-up started: one that has started over since. A window left +// out of the read, or with no reset, hasn't started at all. +func movedOn(p warmWindow, cur QuotaWindow) bool { + if cur.ResetsAt == nil { + return true + } + if cur.ResetsAt.Sub(p.ResetsAt) < warmSlack { + return false + } + // a read that kept the old reset, then the window the warm-up started + return p.Warmed.IsZero() || cur.ResetsAt.Sub(p.Warmed.Add(cur.Span)) >= warmSlack +} + // idle says whether a window (as of now) hasn't started: nothing used, // and its reset a whole window away, as the backend reports one not yet // begun — or not known. diff --git a/internal/provider/codex_warmup_test.go b/internal/provider/codex_warmup_test.go index df0fc6917..acd21c2b7 100644 --- a/internal/provider/codex_warmup_test.go +++ b/internal/provider/codex_warmup_test.go @@ -51,6 +51,9 @@ func TestWarmDue(t *testing.T) { win("7 days", week, 0, now.Add(2*24*time.Hour)), true}, {"reset early: no window running", warmWindow{ResetsAt: now.Add(2 * 24 * time.Hour), Used: 70}, true, idleNow, true}, {"an unused window sliding on", warmWindow{ResetsAt: now.Add(week - 5*time.Minute)}, true, idleNow, false}, + {"reset early, nothing used before", warmWindow{ResetsAt: now.Add(5 * 24 * time.Hour)}, true, idleNow, true}, + {"reset early, nothing used before, left out", warmWindow{ResetsAt: now.Add(5 * 24 * time.Hour)}, true, win("7 days", week, 0, time.Time{}), true}, + {"running, nothing used yet", warmWindow{ResetsAt: now.Add(5 * 24 * time.Hour)}, true, win("7 days", week, 0, now.Add(5*24*time.Hour)), false}, {"a stale read past its reset", warmWindow{ResetsAt: now.Add(-time.Minute), Used: 50}, true, win("7 days", week, 50, now.Add(-time.Minute)), true}, {"a failed warm-up", warmWindow{Pending: true, Failed: 1}, true, running, true}, @@ -133,6 +136,7 @@ func TestCodexWarmOncePerReset(t *testing.T) { // OpenAI resets b early: its use falls to nothing before its reset f.ws["b@example.com"][0].Used = 0 f.now = f.now.Add(5 * time.Minute) + bWarmed := f.now if rs := f.run(t, path, "week"); len(rs) != 1 || rs[0].User != "b@example.com" { t.Fatalf("early reset: %+v", rs) } @@ -149,8 +153,10 @@ func TestCodexWarmOncePerReset(t *testing.T) { if rs := f.run(t, path, "week"); len(rs) != 0 { t.Fatalf("the 5-hour window, weekly only: %+v", rs) } - f.ws["a@example.com"][1] = win("7 days", week, 0, f.now.Add(week)) - f.ws["b@example.com"][0] = win("7 days", week, 0, f.now.Add(week)) + // both weeks running from their warm-ups, nothing used: each reset a + // week from its warm-up, as the backend reports a started window + f.ws["a@example.com"][1] = win("7 days", week, 0, resets.Add(time.Minute+week)) + f.ws["b@example.com"][0] = win("7 days", week, 0, bWarmed.Add(week)) var hours []string for _, r := range f.run(t, path, "all") { hours = append(hours, r.User+":"+strings.Join(r.Windows, "+")) @@ -161,6 +167,89 @@ func TestCodexWarmOncePerReset(t *testing.T) { } } +// shangyuan0818 (10-24): a reset card spent on a weekly window, the +// warm-up after it, nothing used since, then OpenAI resets everyone's +// limits. The window had no use to fall from and its old reset hadn't +// come, so the official reset went without a warm-up. +func TestWarmAfterResetCardThenOfficialReset(t *testing.T) { + for _, c := range []struct { + name string + which string + window string + span time.Duration + expect []QuotaWindow + // left out: the backend leaves a window not started out of its read + // (Anthropic), rather than reporting its reset a whole span away + leftOut bool + }{ + {"codex week", "week", "7 days", week, nil, false}, + {"codex 5 hours", "all", "5 hours", fiveHours, nil, false}, + {"claude week, left out", "week", "7 days", week, claudeWindowsExpected, true}, + } { + t.Run(c.name, func(t *testing.T) { + path := filepath.Join(t.TempDir(), "codex-warmup.json") + start := time.Date(2026, 10, 22, 9, 0, 0, 0, time.UTC) + f := &fakeWarm{now: start, errs: map[string]error{}} + idleAt := func(now time.Time) QuotaWindow { + if c.leftOut { + return QuotaWindow{} + } + return win(c.window, c.span, 0, now.Add(c.span)) + } + set := func(w QuotaWindow) { + if w.Span == 0 { + f.ws["a@example.com"] = nil + } else { + f.ws["a@example.com"] = []QuotaWindow{w} + } + } + f.ws = map[string][]QuotaWindow{ + "a@example.com": {win(c.window, c.span, 55, start.Add(c.span/2))}, + // another account, running all along: never sent one + "b@example.com": {win(c.window, c.span, 20, start.Add(c.span*9/10))}, + } + w := f.warmer(path) + w.expect = c.expect + run := func() { w.warmNow(context.Background(), c.which, nil) } + run() + // a reset card is spent: the window starts over, at nothing used + f.now = start.Add(5 * time.Minute) + set(idleAt(f.now)) + run() + if strings.Join(f.sent, ",") != "a@example.com" { + t.Fatalf("after the reset card, sent %v", f.sent) + } + // the warm-up started it; nothing is used after + warmed := f.now + set(win(c.window, c.span, 0, warmed.Add(c.span))) + for f.now.Before(warmed.Add(c.span / 3)) { + f.now = f.now.Add(codexWarmEvery) + run() + } + if len(f.sent) != 1 { + t.Fatalf("a running window sent again: %v", f.sent) + } + // OpenAI resets everyone's limits: not started again + official := f.now + f.now = official.Add(codexWarmEvery) + set(idleAt(f.now)) + run() + if strings.Join(f.sent, ",") != "a@example.com,a@example.com" { + t.Fatalf("after the official reset, sent %v", f.sent) + } + // that one started it: once + set(win(c.window, c.span, 0, f.now.Add(c.span))) + for range 12 { + f.now = f.now.Add(codexWarmEvery) + run() + } + if len(f.sent) != 2 { + t.Fatalf("sent again after the window started: %v", f.sent) + } + }) + } +} + func TestCodexWarmFailureRetried(t *testing.T) { path := filepath.Join(t.TempDir(), "codex-warmup.json") now := time.Date(2026, 9, 27, 12, 0, 0, 0, time.UTC)