gateway, gui: a gateway key's limit can run every N days, and Reset usage counts it from 0 again (#1509, PennyVibe)

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.
This commit is contained in:
yetone
2026-10-10 22:53:11 +08:00
parent f5c0626770
commit 3541ec2fd5
17 changed files with 771 additions and 53 deletions
+16 -1
View File
@@ -380,6 +380,8 @@ magpie gateway-key rotate <id> # prints the replacement; identity stays
magpie gateway-key remove <id> # revokes remote access
magpie gateway-key limit <id> week --tokens 2m --cost 5 # its own limit
magpie gateway-key limit <id> # limit, used, left and reset
magpie gateway-key limit <id> 10d --tokens 5m # every 10 days, from now
magpie gateway-key limit <id> reset # count its usage from 0 again
magpie gateway-key limit <id> off # no limit
magpie gateway-key models <id> openai/gpt-5 anthropic/* # only these models
magpie gateway-key models <id> all # every model
@@ -389,7 +391,12 @@ magpie gateway-key accounts <id> all # every account
Each gateway key can have its own **limit**: a token total, an estimated
cost in US$, or both, per day, week or month (calendar windows in local
time: from midnight, from Monday, from the 1st). Set it with **Limit** on
time: from midnight, from Monday, from the 1st), or every N days (#1509;
N from 1 to 3650). An N-day cycle starts when the limit is set and runs
N×24 hours, then the next one starts where it ended, so a restart, a
time-zone change or a daylight-saving shift doesn't move it. Changing only
the caps or **Count cache reads too** keeps the cycle; changing N or the
period, or turning the limit on, starts a new one then. Set it with **Limit** on
the key's row (saved with Save) or `magpie gateway-key limit`; the row shows
what the key has used, what is left and when it resets. Tokens counted are a
call's uncached input, output and cache writes, plus cache reads when **Count
@@ -402,6 +409,14 @@ counts are read from the usage log, so they survive a restart. A request in
flight holds a reservation (its body's size in tokens plus the key's mean
output per call), so requests sent at once overshoot by about one call; a
streamed reply is settled when it ends with the usage its vendor reported.
**Reset usage** in the key's limit editor (or `magpie gateway-key limit
<id> reset`) counts the key's tokens and cost from 0 together, from now. An
N-day key starts a new N-day cycle then. A day, week or month key keeps
its window's end (midnight, Monday, the 1st): only the calls before the
reset stop counting, so it doesn't drift off the calendar. The reset is
stored with the key (`since` in caller-keys.json, magpie's own: a value a
client sends is ignored), so it survives a restart; the usage log itself is
untouched. Keys saved before keep their JSON as it was.
A key can read its own status with `GET /v1/magpie/limit`. Requests from
this computer that send no gateway key are not limited; a gateway key used
from this computer is.