PennyVibe asked for a key limit on a cycle of N days rather than a
calendar day, week or month, and for a way to zero a key's usage by
hand. Both are in.
Every N days (N from 1 to 3650):
- access.Limit gains Days and Since. Since is magpie's own: the
limit-key action always works it out, and a value a client sends is
ignored.
- The cycle starts when the limit is set. Each cycle is N×24h of
absolute time from Since, so a restart, a time-zone change or a DST
shift doesn't move it.
- Changing only the caps or "count cache reads" keeps the cycle.
Changing N or the period, or turning the limit on, starts a new
cycle now.
- A hand-written "days" limit with no Since runs from the Unix epoch.
- The GUI window menu offers "Every N days" with a Days field. The CLI
takes `magpie gateway-key limit <id> 10d`. The refusal and status
name the cycle ("for this 1-day cycle").
Reset usage:
- A new action, reset-limit-key, is on the editor's "Reset usage"
button (after a confirm) and on `magpie gateway-key limit <id> reset`.
- It sets Since to now, so tokens and cost both count from 0 together.
- An N-day key starts a new cycle then.
- A day, week or month key keeps its window's end, so it stays on the
calendar. Only the calls before the reset stop counting.
- The usage log is not touched. Authenticate reloads keys each request,
so the reset takes effect at once.
Keys saved before keep their JSON byte for byte (omitzero, nothing
written on load).
Docs: reference.md documents the cycle and reset rules. The docker page
(en, zh, ja) mentions every-N-days.
Run:
- go test -tags nogui ./... with a temp HOME.
- New Go tests:
- TestLimitDaysCycle covers the NY DST, UTC and UTC+8 boundaries.
- TestLimitCalendarReset.
- TestLimitSetAndReset covers keep/restart, an old key's JSON left
untouched, and errors.
- TestKeyLimitDaysCycleAndReset covers the 429 text and header, a
restart, the cycle boundary, and Reset clearing the token and cost
caps together.
- TestKeyLimitCalendarReset.
- TestGatewayKeyLimitCLI covers 10d, reset and bad args.
- Broken by hand: with reset leaving Since alone,
TestLimitSetAndReset, TestKeyLimitDaysCycleAndReset and
TestKeyLimitCalendarReset go red.
- gofmt -l is clean. GOOS=linux and GOOS=windows go vet -tags nogui
pass on ., access, budget, gateway and gui.
- GUI: the new gateway-key-cycle.test.cjs runs in chromium and webkit ×
en, zh, zh-TW, ja and de, with a narrow-width check. Also run, in both
engines: every test using the caller-keys fixture, i18n-keys,
gui-ja, gui-de and gui-zh-tw (80 pass).
- Real use:
- Sandbox magpie (temp HOME, gateway 127.0.0.1:3691, dev UI 3692)
with a fake OpenAI upstream (300+50 tokens a call).
- `gateway-key limit <id> 1d --tokens 700` allowed 2 calls with curl,
then a 429 "for this 1-day cycle" with X-Magpie-Limit-Reset a day
after it was set.
- Pressed Reset usage in the GUI with Playwright and confirmed. The
badge was no longer spent and showed 0 of 700, and curl passed 2
calls again before the next 429.
142 lines
4.8 KiB
Go
142 lines
4.8 KiB
Go
package access
|
||
|
||
import (
|
||
"errors"
|
||
"fmt"
|
||
"math"
|
||
"time"
|
||
)
|
||
|
||
// Limit is a gateway key's budget for a window: a calendar one in local
|
||
// time (#585), a day from midnight, a week from Monday's midnight, a
|
||
// month from the 1st's; or a cycle of Days days (#1509, period "days"),
|
||
// the first from when the limit was set, each next one Days×24h after
|
||
// the last. Tokens counts each call's uncached input, output (reasoning
|
||
// included) and cache writes, and its cache reads too when CacheReads is
|
||
// set; Cost is an estimate in US dollars at the prices magpie shows on
|
||
// the Usage page, not a vendor's bill. A cap of 0 is no cap; a limit with
|
||
// neither is no limit.
|
||
type Limit struct {
|
||
Period string `json:"period"`
|
||
// Days is a "days" limit's cycle, 1 to MaxDays; 0 for the others.
|
||
Days int `json:"days,omitempty"`
|
||
Tokens int64 `json:"tokens,omitempty"`
|
||
Cost float64 `json:"cost,omitempty"`
|
||
CacheReads bool `json:"cacheReads,omitempty"`
|
||
// Since is magpie's, never the caller's (Update sets it): for "days",
|
||
// when the first cycle began, the limit set or last reset; for a
|
||
// calendar period, when it was last reset, so a call before it in
|
||
// the same window doesn't count. Zero for a calendar limit never
|
||
// reset, as every limit from before #1509 is.
|
||
Since time.Time `json:"since,omitzero"`
|
||
}
|
||
|
||
// Periods are the windows a limit can be set for.
|
||
var Periods = []string{"day", "week", "month", "days"}
|
||
|
||
// MaxDays is the longest "days" cycle, ten years.
|
||
const MaxDays = 3650
|
||
|
||
// Limited: l caps something.
|
||
func (l *Limit) Limited() bool { return l != nil && (l.Tokens > 0 || l.Cost > 0) }
|
||
|
||
// Valid is l as it is kept: nil for no limit, an error for one that
|
||
// can't be. Since is left as it came; Update decides it.
|
||
func (l *Limit) Valid() (*Limit, error) {
|
||
if l == nil {
|
||
return nil, nil
|
||
}
|
||
if l.Tokens < 0 || l.Cost < 0 || math.IsNaN(l.Cost) || math.IsInf(l.Cost, 0) {
|
||
return nil, errors.New("A limit can't be negative")
|
||
}
|
||
if !l.Limited() {
|
||
return nil, nil
|
||
}
|
||
v := *l
|
||
if v.Period == "" {
|
||
v.Period = "day"
|
||
}
|
||
switch v.Period {
|
||
case "day", "week", "month":
|
||
v.Days = 0
|
||
case "days":
|
||
if v.Days < 1 || v.Days > MaxDays {
|
||
return nil, fmt.Errorf("A limit's cycle is a whole number of days from 1 to %d", MaxDays)
|
||
}
|
||
default:
|
||
return nil, errors.New("A limit's period is day, week, month or every N days")
|
||
}
|
||
v.Cost = math.Round(v.Cost*1e4) / 1e4
|
||
return &v, nil
|
||
}
|
||
|
||
// Per is the window as a sentence ends it: "day", "week", "month" or
|
||
// "10-day cycle".
|
||
func (l *Limit) Per() string {
|
||
if l.Period == "days" {
|
||
return fmt.Sprintf("%d-day cycle", l.Days)
|
||
}
|
||
return l.Period
|
||
}
|
||
|
||
// sinceOf is the Since a limit set as next keeps, with prev the key's
|
||
// limit before: its own while the period and cycle stay the same (a new
|
||
// cap doesn't restart the window), else a new cycle from now for "days"
|
||
// and none for a calendar period.
|
||
func sinceOf(prev, next *Limit, now time.Time) time.Time {
|
||
if prev != nil && prev.Period == next.Period && prev.Days == next.Days && (next.Period != "days" || !prev.Since.IsZero()) {
|
||
return prev.Since
|
||
}
|
||
if next.Period == "days" {
|
||
return now
|
||
}
|
||
return time.Time{}
|
||
}
|
||
|
||
// epoch is where a "days" cycle written by hand without a Since runs from.
|
||
var epoch = time.Unix(0, 0)
|
||
|
||
// Window is the window l counts in at now, in now's location: when it
|
||
// began and when the next begins. A "days" cycle is counted in absolute
|
||
// time, 24h a day whatever the clocks do, so a restart or a change of
|
||
// time zone or summer time keeps it; one with no Since runs from the Unix
|
||
// epoch. A calendar window starts at Since instead when the key was reset
|
||
// in it.
|
||
func (l *Limit) Window(now time.Time) (start, reset time.Time) {
|
||
loc := now.Location()
|
||
if l.Period == "days" {
|
||
n := time.Duration(min(max(l.Days, 1), MaxDays)) * 24 * time.Hour
|
||
anchor := l.Since
|
||
if anchor.Before(epoch) {
|
||
anchor = epoch
|
||
}
|
||
if now.Before(anchor) { // the clock was set back past the start
|
||
return anchor.In(loc), anchor.Add(n).In(loc)
|
||
}
|
||
start = anchor.Add(now.Sub(anchor) / n * n)
|
||
return start.In(loc), start.Add(n).In(loc)
|
||
}
|
||
start, reset = Window(l.Period, now)
|
||
if l.Since.After(start) && l.Since.Before(reset) {
|
||
start = l.Since.In(loc)
|
||
}
|
||
return start, reset
|
||
}
|
||
|
||
// Window is the calendar window of period that now is in, in now's
|
||
// location: its start and when the next begins.
|
||
func Window(period string, now time.Time) (start, reset time.Time) {
|
||
y, m, d := now.Date()
|
||
loc := now.Location()
|
||
switch period {
|
||
case "week":
|
||
back := (int(now.Weekday()) + 6) % 7 // days since Monday
|
||
start = time.Date(y, m, d-back, 0, 0, 0, 0, loc)
|
||
return start, time.Date(y, m, d-back+7, 0, 0, 0, 0, loc)
|
||
case "month":
|
||
return time.Date(y, m, 1, 0, 0, 0, 0, loc), time.Date(y, m+1, 1, 0, 0, 0, 0, loc)
|
||
default:
|
||
return time.Date(y, m, d, 0, 0, 0, 0, loc), time.Date(y, m, d+1, 0, 0, 0, 0, loc)
|
||
}
|
||
}
|