ac4af582301a4e8a29245dbba767eba31334ddf9
6
Commits
| Author | SHA1 | Message | Date | |
|---|---|---|---|---|
|
|
1f439ecc5e |
gateway keys: a limit can count only some models or providers (zola_xynb)
zola_xynb on X asked for a cap per model or per provider under one gateway key. A key limit can now name models (access.Limit.Models, "<provider>/<model>" or "<provider>/*"), so one key can have, for example, a month of $20 on openai/* next to a month for the whole key. Limits without models still cover the whole key, as before. - budget: sums are kept per window and scope, and count a ledger row only when its provider/model matches (any of the model's ids). reserve holds a request to a scoped limit only when the model it names is served there. windowsOf keeps every scope's sums, so the ledger is not read again on each request. - gateway: keyLimited puts the key's spent scopes on the caller (withSpent), count_tokens included. A direct request for a spent model gets a 429 with Retry-After and no X-Magpie-Limit-Reset, so a remote magpie rests only that model. A routing group, a provider's fallback and embedding/image/video/decision groups skip spent members (groupKeeps, allowedCandidates) and get a 429 only when none is left. System One, count_tokens and magpie's own calls for the key are held too. Model lists are unchanged. A refusal turned away before routing now records its ErrType in the ledger. - CLI: magpie gateway-key limit <id> add month --cost 20 --model openai/* (--model repeatable, --models a,b). - GUI: the key limit editor has a Models field that opens the app menu (multi-pick: All models, every model of a provider, each model). It has no <select> and no new screen. The badge, line and tabs name the models. Strings are in every language. - docs: reference.md and subsystems/gateway-routing.md. Run at d52725d32 (this change on |
||
|
|
66b5e4a844 |
provider: every request to a Remote magpie carries the provider's Headers; its key always goes as x-api-key (Yirasumi)
Yirasumi on Discord: with a Remote magpie behind something that wants
its own header, chat worked but the Usage page's read of the remote went
without the provider's Headers. remoteJSON (the remote's cards, a card's
refresh, its quota history) and askRemoteKeyLimit (each gateway key's
limit) set only Authorization: Bearer <key>.
- signRemote puts the provider's Headers on those four, as Sign and
listHeaders already did for API requests and the model list. They are
no turn of a conversation: ${sessionId}/${threadId} are an id of their
own (SentHeaders with no conversation, v0.1.2058's path).
- Auth, decided: the key always reaches a remote magpie as x-api-key
(remoteKey), which a magpie gateway reads beside Authorization. A
user's Authorization (a proxy's own scheme) goes in place of magpie's
Bearer copy, as it already did on API requests and the list, without
taking the key away; a user's x-api-key is never sent to a remote
magpie. Sign now adds x-api-key for a remote magpie on every API (it
had it on Messages only), and listHeaders drops a user's x-api-key, so
a user's Authorization no longer leaves chat or the list without the
key, and each key's limit is read with that key.
- docs/subsystems/providers-accounts.md: a "remote magpie's headers" row.
Other requests to a remote magpie (every API, count_tokens, System One,
images, videos, embeddings, searches) already go through Sign; the model
list through listHeaders. No other request to one was found
(grep of p.Anthropic+, /v1/magpie/ and IsRemoteMagpie callers).
Run on mote at this head (rebased on
|
||
|
|
0bbb2f3ca4 |
provider, gui, gateway: a thousand keys are listed once and read eight at a time (@NetRozee on X)
@NetRozee on X: "when I add a thousand keys for a custom endpoint magpie
just froze or crash by itself".
Cause. The Providers page asked, for each key, what it rests by
(RestKey), and RestKey listed every key on again (KeysOn), which itself
looked along the list for each key: cubic in the keys. /api/providers
and every /api/keys/* answer, an import too, are drawn through it. The
gateway asked, for each key of every request, whether it serves the
model (Serves), and each asking read the provider's lists again (a
filememo stat, and a read while the file is new) and looked along each
model's key ids. fetchPerKey looked along the lists for each key and
model the same way.
Fix.
- KeysOn drops repeats by a set.
- RestKeys finds once whether several keys are on; providerInfo uses it.
- Serving(model) reads the lists once and answers for each key from sets
(keyLists, made again when the lists are read anew); perKeyBarred and
the test pick use it. Serves is Serving for one key.
- fetchPerKey keeps each model's keys as a set and indexes the old list
by key once; a model kept for a failed key is kept once, as before.
- KeysListed and ListsRead count listings and list reads for the tests.
Behaviour is otherwise the same.
Measured (go test in a sandbox HOME, a custom provider, 50 models):
1000 keys 5000 keys
providerInfo 1.54s -> 7.5ms 2m42.9s -> 18.3ms
providersState 1.65s -> 110ms 2m40.3s -> 146ms
POST /api/keys/import 1.64s -> 82ms 2m44.7s -> 288ms
of one more key
gateway perKey 100.9ms -> 8.2ms (old not run) -> 40ms
(each request)
Tests (counts, not time), each red on the old code by hand:
- TestProviderInfoListsKeysOnce: old 1001 listings for 1000 keys, 11 for
10; new 0 and 0.
- TestPerKeyReadsListsOnce: old 1001 list reads for 1000 keys, 11 for
10; new 1 and 1.
- TestServingReadsListsOnce, TestRestKeysIsRestKey,
TestKeysOnDropsRepeats: same answers as before.
And the vendor reads. KeyBalances, PlanQuotas and remoteKeyCards started a goroutine
for each key and asked the vendor with all of them at once: a thousand
keys were a thousand requests to one relay together, each holding a
connection and its body.
Fix. keySlots holds each provider's reads to keyReadsAtOnce (8); other
providers' keys are read beside them. A read whose context is done goes
on without a slot and fails at once, as before.
Tests:
- TestKeyReadsEightAtOnce: a Command Code provider of 31 keys and a
remote magpie of 31 keys behind a server that holds every request.
Old code (the slots taken out by hand): 31 at once for balances, plans
and remote limits; new: 8, every card still there.
- TestKeySlots: a done context takes no slot; another provider's keys
have their own.
Run: gofmt -l (clean); GOOS=linux and GOOS=windows go vet -tags nogui
./... (clean); go test -tags nogui -count=1 -timeout 45m ./... in a sandbox HOME (GOCACHE, GOMODCACHE pinned) on
|
||
|
|
f299faef48 |
gateway keys: a key holds several limits at once, every one must pass, and the 429 names the one that resets last (#2227, oxsean)
A key may now hold up to five limits, each in its own window (5 hours
and a week, say). A request needs every one: budget.Reserve checks
each, holds one reservation per window and settles each, and when some
are spent refuses with the spent limit that resets last, so
X-Magpie-Limit-Reset and Retry-After say when the key is really back.
Two limits in one window are refused (change that one instead).
Saved format: the first limit stays in "limit", which older magpie
reads, the rest go in "moreLimits". A file without moreLimits reads as
before.
- access: Change.Index adds (index = len), replaces (keeps its Since)
or removes (nil) one limit; reset-limit-key with an index resets one,
without resets all; the plain limit-key replaces all, as before.
- /v1/magpie/limit: "limit" is the binding one (what older clients
read), "limits" is each. A remote key card shows a window per cap of
each limit and falls back to "limit" from an older remote.
- CLI: magpie gateway-key limit <id> list | add <period> ... |
remove <n> | reset [<n>]; the single-limit form still replaces all.
- GUI: the key's badge names the limit holding it most and "+N limit(s)",
a line per limit under the row, and the editor has a tab per limit
with Add a limit / Remove this limit. Strings in en, zh, zh-TW, ja, de.
- docs/reference.md and docs/subsystems/gateway-routing.md say so.
Run (worktree on
|
||
|
|
f70c0525aa |
access, gui, cli: a gateway key's limit can run every N hours, a 5-hour cycle among them (#2102)
A key's limit window was a calendar day, week or month, or every N days (#1509). #2102 asks for a shorter cycle, every 5 hours, like a vendor's own 5-hour window. Limit gains period "hours" with Hours (1..8760), counted in absolute time from Since, as the N-day cycle is; a change to N or from days restarts the cycle, a cap change keeps it, Reset starts a new one. Budget status, the 429's wording ("this 5-hour cycle"), X-Magpie-Limit-Reset, /v1/magpie/limit and a remote magpie's key card carry Hours. The GUI's window menu has "Every N hours" with an Hours field beside it (no native select); the CLI takes `<N>h` (magpie gateway-key limit <id> 5h --tokens 200k). Docs: reference.md and docker.html (en, zh, ja). Strings in zh, zh-TW, ja, de. Run: - go test ./internal/access (TestLimitHoursCycle across NY DST, UTC, UTC+8; TestLimitHoursSetAndReset), -tags nogui ./internal/cli (5h, rejects 0h/8761h/xh/1.5h/hours), ./internal/gateway (TestKeyLimitHoursCycle: 429 and reset header, /v1/magpie/limit, Reserve at the boundary, Reset; remote key card 5h), plus the full access, budget, cli, gateway, provider and gui packages. The gateway package hit go test's 10-minute default once with the load average near 270; rerun with -timeout 40m it passed in 1536s. - Broke cycle() for hours by hand: TestLimitHoursSetAndReset and TestKeyLimitHoursCycle went red; restored. - GUI: gateway-key-cycle (extended with the hours case: menu, field, validation, Save body, badge, Reset confirm, one-column at 500px) in chromium and webkit x en/zh/zh-TW/ja/de, 10/10; i18n-keys, gateway-key-limit, gateway-caller-keys, phone-rows, gateway-keys-off, gateway-own-key, api-key-theme 63/63; an ad-hoc check of the hours editor at 1100 and 420 in both engines and 5 languages (no overflow, screenshots looked at). - gofmt -l; go vet darwin, GOOS=linux -tags nogui, GOOS=windows. Not tried: a real gateway under load across a 5-hour boundary; the Windows box. |
||
|
|
1c9747f611 |
usage: a Remote magpie provider's gateway keys each show their own limit, used and left, period and reset on the Usage page (#1547, PennyVibe)
A computer that uses another magpie through gateway keys that magpie gave it saw the other magpie's subscription cards, but not what each key may still spend there. What changed: - provider/remote_key_limits.go: each key of a remote-magpie provider gets a section on one card, "<remote>/~keys", read from the remote's existing GET /v1/magpie/limit asked with that key alone (the endpoint answers only the calling key, from access.Caller; nothing in the request picks another key, so a key never sees another's numbers). A window per cap (tokens, spend) named for its period (today / this week / this month / this cycle), used against the cap, with its reset; SubscriptionQuota.GatewayKey carries what is left, calls, the cycle's days, used up and unpriced calls. A key with no limit shows one "Limit: Unlimited" window. Kept for remoteCardsFor like the remote's cards; the card's refresh reads the named key again. - A failed read keeps the last numbers, dated (AsOf); with nothing read the section says why, never zero and never "no limit". A remote from before /v1/magpie/limit (404) says it is too old to tell. - GUI: the card is named "Gateway keys · <remote>" (main page and the tray panel), a line under each key says the cycle, calls and "used up", and the two new errors are said plainly; strings in zh, zh-TW, ja, de. - magpie quota (CLI) shows the same cards; a window with no cap now reads "Limit unlimited" there, not "Limit 0%" (quotaCell). - docs: providers-accounts (Allowance), gateway-routing (Remote magpie APIs, verification), reference (/v1/magpie/limit). Run: - New tests fail on the old code: TestRemoteMagpieKeyLimits (provider) and TestRemoteMagpieKeyLimitCards (gateway, a real Handler, real chat requests moving the numbers) with the key cards taken out of RemoteCards; usage-remote-key-limits.test.cjs on HEAD's app.js. TestKeyLimitTellsOnlyTheCaller guards what was already so (key A can't read key B by query, header or no key from the LAN), so it passes on both. TestQuotaCell's unlimited case reads "Limit 0%" on the old code. - provider, gateway -count=3; go test -tags nogui ./... (full suite). - GUI: the new test in chromium and webkit x en/zh/zh-TW/ja/de x 900/400px (20 pass); the whole GUI suite. - gofmt -l clean; go vet -tags nogui darwin, linux, windows. - Real two-magpie run, both this build, sandbox HOMEs, 127.0.0.1:38461 (remote) and :38471 (local); the upstream was a fake OpenAI server (fixed 175 tokens a reply). Remote keys laptop (1000 tokens/day), desk (5000/week), spare (no limit); local "office" Remote magpie with all three, routing rotate. After 17 chats through the local gateway, magpie quota on the local read laptop 1050/1000 (100%), desk 1050/5000 (21%), spare unlimited (in --json; the text line read "Limit 0%", which the quotaCell change above fixes), matching the remote's own gateway-key list; laptop's spent key was passed over (every chat 200). With the remote stopped each key said it couldn't be reached, never 0. Not run against a real vendor, nor a real magpie older than /v1/magpie/limit (unit-tested with a 404). |