From 23cec6c4fbd88619de79427144b83550bf8bb212 Mon Sep 17 00:00:00 2001 From: yetone Date: Sat, 24 Oct 2026 22:22:21 +0800 Subject: [PATCH] warm-up: an official reset after a spent reset card, nothing used between, is warmed (shangyuan0818) shangyuan0818 on Discord (10-24): "Warm up on reset" on for the Codex week. magpie spent a Codex reset card about to run out, the warm-up after it started the week, nothing was used, then OpenAI reset everyone's limits. No warm-up went for that second reset. Cause: warmDue saw a reset only when the window's use fell (by 1 or more) or its kept reset time passed. After the card and its warm-up the window was running at 0% used, so the official reset had no use to fall from, and the reset time kept (a week from the warm-up) was still days off. The read after it shows the window not started (0%, its reset a whole window from now), which nothing compared with what was kept. Fix: warmDue also counts a window as started over when it is not started now (idle) and its reported reset is at least warmSlack later than both the reset it was seen running to and the end of the window magpie's last warm-up started (movedOn), or no reset is reported at all. The second bound keeps a read that still carried an old reset before showing the warm-up's own window from looking like a reset. After the upgrade an account already in this state is warmed on the next look: nothing for the user to do. Siblings: the same warmDue runs for every ChatGPT account, the 5-hour window ("all"), and Claude's warm-up (claude_warmup.go, same codexWarmer), whose not-started windows Anthropic leaves out of the read; each has a subtest. No other provider has a warm-up. Tests: TestWarmAfterResetCardThenOfficialReset drives fake usage reads through the reported sequence (card spent, warm-up, a third of the window unused, official reset, then 12 more looks) for the Codex week, the Codex 5 hours and a Claude week left out of the read, with a second account running all along that must never be sent one. TestWarmDue gets three cases. TestCodexWarmOncePerReset's last step set two weeks warmed 10 and 35 minutes before to reset a full week from now, which is what a not-started window reports; they now reset a week from their warm-ups, as the backend reports a started window. Broken by hand: - old codex_warmup.go with the new tests: TestWarmDue "reset early, nothing used before" and "... left out" due false, want true; TestWarmAfterResetCardThenOfficialReset all three subtests "after the official reset, sent [a@example.com]". - movedOn's nil-reset branch returning false: the Claude subtest and the "left out" case go red. - movedOn without the warm-up bound: TestCodexWarmOncePerReset goes red ("all: [a@example.com:5 hours b@example.com:7 days]"). Run at 4b9694c34 plus this change (rebased onto dcfda5f16, which changes internal/gateway only): - gofmt -l on both files: nothing. - go vet ./internal/provider/, GOOS=linux go vet -tags nogui ./internal/provider/, GOOS=windows go vet ./internal/provider/: ok. - go build ./..., GOOS=linux go build -tags nogui ./..., GOOS=windows go build ./...: ok. - go test -tags nogui -timeout 45m ./internal/provider/ in a sandbox HOME: ok (113s), no reds. Not run: the full go test ./... (other sessions' full suites are queued on this machine, one suite per machine). Not tried with a real account: an official OpenAI reset can't be made on demand, so the backend's reads after it come from the report and from what an idle window reports today (0% used, reset a whole window from now). If OpenAI's reset instead keeps the old reset time with 0% used, the read can't tell it from a running unused window, and this change doesn't catch it. --- internal/provider/codex_warmup.go | 29 +++++++- internal/provider/codex_warmup_test.go | 93 +++++++++++++++++++++++++- 2 files changed, 118 insertions(+), 4 deletions(-) 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)