budget: a kept window is reused only for a limit that starts when it did (TestGatewayKeyLimitCLI)
059d995d5keeps 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 at61a04f42awas 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.
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user