From 8b2fb6b082663d3672578d527d0a7bbc394f2223 Mon Sep 17 00:00:00 2001 From: yetone Date: Wed, 11 Nov 2026 14:55:11 +0800 Subject: [PATCH] budget: a kept window is reused only for a limit that starts when it did (TestGatewayKeyLimitCLI) 059d995d5 keeps what AllOf counted of each window, keyed by window{key, start's Unix second, scope}. A 10-day limit reset and then made a 5-hour one a millisecond later share that key, so the 5-hour status took the 10-day prefix: its Start was the 10-day Since, its Reset the 5-hour one, 5h0m0.0017s apart, and the rows it counted were from the old start. CI at 61a04f42a was red on ubuntu and macOS for it. A kept entry is reused now only when its start is the limit's own. Run (mote is out of disk and refuses ssh, so WSL and the Windows box): - TestAllOfKeptWindowIsOfItsOwnStart (new): red on the old code with Start 14:36:23.1, Reset 19:36:23.1017, green with the fix. - go test ./internal/budget ./internal/access: ok on WSL (-count=3) and Windows. - go test -run TestGatewayKey -v ./internal/cli: all 11 pass on Windows, and with -tags nogui on WSL; TestGatewayKeyLimitCLI is the CI red. - go vet ./internal/budget, gofmt -l: clean. - internal/gateway on Windows: TestClaudeAsRootEnv red (#2425, open), and the package hit the 10m default timeout; not on this path. Not tried: a full ./... run. --- internal/budget/all_of_test.go | 19 +++++++++++++++++++ internal/budget/budget.go | 5 ++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/internal/budget/all_of_test.go b/internal/budget/all_of_test.go index 138793c53..3c171ee6c 100644 --- a/internal/budget/all_of_test.go +++ b/internal/budget/all_of_test.go @@ -136,3 +136,22 @@ func BenchmarkReadEach(b *testing.B) { } } } + +// A window AllOf kept is another limit's when that limit started in the +// same second: a 10-day limit reset, then made a 5-hour one a millisecond +// later (TestGatewayKeyLimitCLI). The 5-hour window starts at its own +// Since, not the 10-day one's. +func TestAllOfKeptWindowIsOfItsOwnStart(t *testing.T) { + bigLedger(t, 0, 0, 0) + t0 := time.Unix(time.Now().Unix(), 100_000_000) + ten := access.Key{ID: "k", Limit: &access.Limit{Period: "days", Days: 10, Cost: 800, Since: t0}} + if st := All(ten, t0.Add(time.Minute)); len(st) != 1 || !st[0].Start.Equal(t0) { + t.Fatalf("10-day: %+v", st) + } + t1 := t0.Add(1700 * time.Microsecond) + five := access.Key{ID: "k", Limit: &access.Limit{Period: "hours", Hours: 5, Tokens: 200_000, Since: t1}} + st := All(five, t1.Add(time.Minute)) + if len(st) != 1 || !st[0].Start.Equal(t1) || st[0].Reset.Sub(st[0].Start) != 5*time.Hour { + t.Fatalf("5-hour after a 10-day in the same second: %+v", st) + } +} diff --git a/internal/budget/budget.go b/internal/budget/budget.go index 681f8e06b..fc7e02efa 100644 --- a/internal/budget/budget.go +++ b/internal/budget/budget.go @@ -339,7 +339,10 @@ func AllOf(keys []access.Key, now time.Time) [][]Status { x.prefix.models = scopeModels(l) } kept.Lock() - if e, ok := kept.of[w]; ok { + // window keys a start by its second: what was kept is + // another limit's when that one started in the same + // second (a 10-day limit made a 5-hour one at once) + if e, ok := kept.of[w]; ok && e.prefix.start.Equal(start) { x.prefix, x.cur = e.prefix, e.cur } kept.Unlock()