budget: a window two limits share counts a call once (#2942)

A key with a day and a week limit has both windows start at 00:00 on a
Monday; a day and a month do on the 1st. sumsFor gave both limits one
*sums, but once it was kept from an earlier call it was appended to
the kept list once per limit, and Append adds a call to each kept
sums. From the second call of such a day each call counted twice in
the day and the week (three times on a Monday the 1st): calls, tokens
and cost, so the key was refused at half its day while limit list and
Usage, which read the file, showed the right sum (oxsean). A restart
hid it until the next call.

sumsFor now looks first in the list it is building, then in what was
kept, so each window start is kept once. Reserve's holds already took
one a window. JoeyWu-tech's 119a09f, as posted; xiedekai's repro
(calls and tokens double too) added its checks to the test.

TestKeyLimitWindowsThatStartTogetherCountACallOnce runs a day limit
of 400 tokens ($4) beside a week, a month, or both, on 2026-10-19 (a
Monday), 2026-10-01 and 2026-06-01 (a Monday the 1st), by tokens and by
cost, 100 tokens ($1) a call: four calls are let in, the fifth refused
on the day at 400, with 4 calls and 400 tokens, and budget.All (what
limit list shows) agrees with the refusal.

Red on origin/main's budget.go, all six subtests: "call 4 refused with
300 of 400 used; it counts 500", and on a Monday the 1st "call 3
refused with 200 of 400 used; it counts 400". Green with the change.

Run:
- Full suite, `go test -timeout 45m ./...` (pipefail, log kept whole),
  darwin arm64, go1.26.8, at 31136c446 (86254d404 + this and the #2932
  change): 68 packages ok, no FAIL.
- At the pushed head (rebased on ec7e50b82, a redact-only change):
  TestKeyLimit|TestRemoteKeyLimit ok; earlier -race -count=3 ok, and
  the new test under TZ=UTC, America/Los_Angeles, Pacific/Kiritimati.
- Windows 11 box (cross-compiled gateway test binary): TestKeyLimit and
  TestRemoteKeyLimit PASS.
- gofmt -l clean; go vet darwin, GOOS=linux -tags nogui, GOOS=windows:
  ok.

Not tried: a real gateway key through the app on a Monday; the test
drives budget.Reserve and budget.Append as the gateway does.

Co-authored-by: JoeyWu-tech <71250493+JoeyWu-tech@users.noreply.github.com>
Co-authored-by: xiedekai <285581061+xiedekai@users.noreply.github.com>
This commit is contained in:
yetone
2026-10-22 18:06:07 +08:00
co-authored by JoeyWu-tech xiedekai
parent 572a1757cc
commit 61b92529e4
2 changed files with 105 additions and 10 deletions
+13 -10
View File
@@ -102,23 +102,26 @@ func counts(r usage.Record) bool { return !r.IsRejected() && !r.IsReported() &&
// sumsFor is keyID's sums for each window starting at starts, read from
// usage.jsonl the first time; those of windows no limit counts in now
// are let go. Called with state held.
// are let go. A window two limits share (a day and a week on a Monday,
// a day and a month on the 1st) is kept once, as Append adds a call to
// each one kept (#2942). Called with state held.
func sumsFor(keyID string, starts []time.Time) []*sums {
k := slot(keyID)
kept := state.sums[k]
out := make([]*sums, len(starts))
var next []*sums
at := func(ss []*sums, start time.Time) int {
return slices.IndexFunc(ss, func(s *sums) bool { return s.start.Equal(start) })
}
for i, start := range starts {
j := slices.IndexFunc(kept, func(s *sums) bool { return s.start.Equal(start) })
if j < 0 {
j = slices.IndexFunc(next, func(s *sums) bool { return s.start.Equal(start) })
if j >= 0 {
out[i] = next[j]
continue
}
out[i] = read(keyID, start)
} else {
if j := at(next, start); j >= 0 {
out[i] = next[j]
continue
}
if j := at(kept, start); j >= 0 {
out[i] = kept[j]
} else {
out[i] = read(keyID, start)
}
next = append(next, out[i])
}
+92
View File
@@ -597,3 +597,95 @@ func TestKeyLimitSeveralLimits(t *testing.T) {
t.Fatal("the day alone has room:", w.Code, w.Body.String())
}
}
// Limits whose windows start at the same moment (a day and a week on a
// Monday, a day and a month on the 1st) count a call once in the window
// they share. budget kept such a window once on the key's first request,
// and once a limit from its second on, and Append added each call to it
// that many times: a day and week key was refused on a Monday at half its
// day, while limit list and Usage showed the right sum (#2942).
func TestKeyLimitWindowsThatStartTogetherCountACallOnce(t *testing.T) {
zone := time.FixedZone("UTC+8", 8*3600)
for _, c := range []struct {
name string
at time.Time
more []string
}{
{"monday", time.Date(2026, 10, 19, 12, 0, 0, 0, zone), []string{"week"}},
{"the 1st", time.Date(2026, 10, 1, 12, 0, 0, 0, zone), []string{"month"}},
{"monday the 1st", time.Date(2026, 6, 1, 12, 0, 0, 0, zone), []string{"week", "month"}},
} {
// the reporter's caps are a cost: 100 input tokens are $1 here
for _, cost := range []bool{false, true} {
name := c.name + "/tokens"
if cost {
name = c.name + "/cost"
}
t.Run(name, func(t *testing.T) {
fresh(t)
budget.Forget()
t.Cleanup(budget.Forget)
input, zero := 10_000.0, 0.0
if err := settings.Save(settings.Settings{ModelPrices: map[string]settings.ModelPrice{
"plan/m1": {Input: &input, Output: &zero, CacheRead: &zero, CacheWrite: &zero},
}}); err != nil {
t.Fatal(err)
}
// each call is 100 tokens, $1: the day lets in 400 ($4), the others plenty
cap := func(period string, n int) access.Limit {
if cost {
return access.Limit{Period: period, Cost: float64(n) / 100}
}
return access.Limit{Period: period, Tokens: int64(n)}
}
first := cap("day", 400)
who := access.Identity{KeyID: "key", KeyName: "Key", Limit: &first}
day, _ := access.Window("day", c.at)
for _, p := range c.more {
if start, _ := access.Window(p, c.at); !start.Equal(day) {
t.Fatalf("the %s starts %v, the day %v", p, start, day)
}
who.More = append(who.More, cap(p, 100_000))
}
used := func(r *budget.Refusal) float64 {
if cost {
return r.Cost * 100
}
return float64(r.Tokens)
}
call := func() *budget.Refusal {
release, refused := budget.Reserve(who, 0, "plan/m1", c.at)
if refused != nil {
return refused
}
budget.Append(usage.Record{Time: c.at, Provider: "plan", Model: "m1", Input: 100, Status: 200, CallerKeyID: "key"})
release()
return nil
}
for i := range 4 {
if refused := call(); refused != nil {
t.Fatalf("call %d refused with %d of 400 used; it counts %.0f", i+1, 100*i, used(refused))
}
}
// the day is spent now, and says the 400 it holds
refused := call()
if refused == nil {
t.Fatal("a fifth call let in past the day's 400")
}
if refused.Period != "day" || used(refused) < 399.5 || used(refused) > 400.5 {
t.Fatalf("refused on the %s at %.0f, want the day's 400", refused.Period, used(refused))
}
// the calls and tokens are counted once too, not only the
// cost (xiedekai), as limit list reads them from the file
if refused.Calls != 4 || refused.Tokens != 400 {
t.Fatalf("refused with %d calls and %d tokens, want 4 and 400", refused.Calls, refused.Tokens)
}
for _, st := range budget.All(access.Key{ID: who.KeyID, Limit: who.Limit, More: who.More}, c.at) {
if st.Calls != refused.Calls || st.Tokens != refused.Tokens {
t.Fatalf("the %s's list says %d calls and %d tokens, the gateway %d and %d", st.Period, st.Calls, st.Tokens, refused.Calls, refused.Tokens)
}
}
})
}
}
}