Commit Graph
5 Commits
Author SHA1 Message Date
yetone 67405d5609 gui: the management API leaves a key let use Magpie Memory alone
4e24994a6 let a gateway key the user picks use Magpie Memory from another
computer (access.Key.Memory). memory-key is left out of the management
API, but rotate-key is not: an admin key, which may manage magpie and
nothing more, could rotate a Memory key through POST
/v1/magpie/admin/gateway-keys/rotate-key and get its new secret back (or
set a secret of its own). With it, it read and wrote memory, whose digest
goes into every agent's instructions on this computer.

adminMayChangeKey now refuses any action on a key with Memory, as it
already did for a key that may manage magpie; the caller's own key stays
its to change. TestAdminAPILeavesMemoryKeys covers rotate-key, with and
without a chosen secret, and memory-key.

Run on mica at this tree:
- go test ./internal/gui ./internal/access: access ok; gui red once on
  the first run, and the log of that run wasn't kept, so the failing test
  isn't named. Two more runs of ./internal/gui, logged whole: ok, ok.
- go vet ./internal/gui ./internal/access on darwin, GOOS=windows on
  mica, and -tags gtk3 on Linux (wsl-run, magpie-desktop): clean.
- gofmt -l: clean.
Broken by hand: with the new check off, the test is red. rotate-key
answers 200 with the Memory key's secret, and the caller's chosen secret
opens "Remote box".
Not tried: a running gateway with a real remote admin key.
2026-11-19 03:06:55 +08:00
yetone 6256d9956a gateway keys: a limit can follow a subscription account's own window (ljayic on X)
A key's limit gains the period "account": it names one subscription
account ("<provider>/<user>") and one of its windows (5h, Weekly, Monthly,
...), and starts and resets when that account's window does, at the reset
time in the account's last good reading (quotas.json), never asked of the
vendor on the request's path (provider.AccountWindow).

When the window can't be read (no reading yet, no such window, or its reset
has passed with no reading since), the limit is "reset unknown": it is
neither reset nor lifted. It counts from the latest time the window can't
have begun before (the reset that passed, else when the limit was set), so
it counts at least the window's calls; a refused call gets Retry-After 300
and a 429 saying the window can't be read now, and the accounts are read
again in the background at most once a minute.

GUI: Gateway keys -> a key's limit -> Window -> "An account's window", then
an app-menu picker of the accounts' windows as read (GET
/api/caller-keys/account-windows); the row reads "$x / $y · 5h of
codex/...", and an unknown reset reads "resets with the account's window".
CLI: magpie gateway-key limit <id> account --account codex/me@example.com
--window 5h --tokens 2m. i18n zh, zh-TW, ja, de. reference.md and
gateway-routing.md say it, with the fallback.

Run (at this tree, base ec8125e68, rebased onto 1ac137e10 which touches
none of these files):
- gofmt -l clean; go vet darwin, linux (-tags nogui), windows clean (mica).
- go test -count=1: internal/access, internal/budget, internal/provider,
  internal/cli, internal/gui ok (mica). TestEveryRouteHasACommandOrIsNamed
  was red until the new route was named (magpie quota).
- New: TestAccountWindowLimit (budget), TestAccountWindowFromLastReading and
  TestReadAccountWindowsOnceAMinute (provider), TestParseAccountWindowLimit
  (cli), TestCallerKeyAccountWindowsRoute (gui). Broken by hand: the unknown
  branch made to guess a 5h window from now -> TestAccountWindowLimit red
  ("let through on a window not read").
- GUI: gateway-key-account-window.test.cjs, chromium + webkit, en zh zh-TW
  ja de, 500px, no native select: 10 of 10 (red without the keys.js and
  i18n change). Every GUI file that loads the caller-keys fixture or the
  Gateway keys page, plus i18n*: 1064 tests, 1046 pass, 18 red, the same
  18 red on origin/main ec8125e68 alone: api-key-theme (6, #6444),
  gateway-key-arrange 420px (10, #6268), text-size webkit (a subtest and its test, #6561).
Not tried: a real Codex/Claude account's window through a live gateway;
the full GUI suite (mote and mica each had a full suite running).
2026-11-15 03:50:46 +08:00
yetone 261b84ba38 gui: the management API serves the Routing page's groups (#6307, ljayic on X)
ljayic asked again for routing groups through /v1/magpie/admin/. They are
served now as the other routes are, by the app's own handlers behind the
same guard (gateway/admin.go, by path prefix, nothing new there):

- GET routing-groups: the Routing page's /api/groups answer.
- GET routing-groups/{id}: one group as that list has it, by its id or the
  ref agents pick it by (group/<id>, auto/<model>); 404 for none.
- POST routing-groups/create: the page's save, refused with 409 when the
  id is a group's already (save would write over it).
- POST routing-groups/update: the page's save over the group as it is,
  the fields sent put over its own, so one field changes alone; 404 for no
  such group; "from" renames, as the page's editor does.
- POST routing-groups/{save,delete,copy,switch,arrange,pick,defaults,
  template,share,import,show}: the page's actions as they are.

Members, combos (a group in a group) and each member's account
("provider/model#<account>") go through provider.SaveGroup, so what the
page refuses the API refuses: an account the provider has none of, an
account on a group, an empty group, a bad id, deleting a group a combo
holds. Not served: "found" and "skip" (the found-groups switches, which
move the agents set to a found group, rewriting this computer's agents).

Run (mica; mote was at load 62 with a GUI suite): go test -count=1
-run 'TestAdmin|Group' ./internal/gui ok; go vet ./internal/gui and
GOOS=windows go vet ./internal/gui ok; gofmt -l empty.
Red on the old admin.go (origin/main's, with the new tests): both new
tests 404. Broken by hand, each red in TestAdminAPIRoutingGroups: create's
409 check off (create over team served), update's merge off (a name-only
update refused as an empty group), "found" added to the actions (served).
Tests: TestAdminAPIRoutingGroups (create, combo, 409, four refusals the
page makes, get by id and ref, update keeps fields, member change, rename
carried into the combo, switch, arrange, found/skip 404, delete refused in
a combo then both at once, no provider key told);
TestAdminAPIRoutingGroupsGuarded (through gateway.New().Front(): no key
401 from loopback and the network, a key that may not manage 403 from
both, a local key from the network and through a proxy 403, Origin 403,
five routes each, nothing made; then local from loopback and any from the
network 200).
Not tried: the full suite (both boxes had a GUI suite running; it runs
before the push), a Linux vet of gui (needs WebKitGTK; wsl-run before
the push), a real request from a second computer.
2026-11-15 00:13:05 +08:00
yetone 648cd3fdbb gateway: the management API leaves other keys that manage magpie alone
727e23637 added /v1/magpie/admin/ and left admin-key out of it so that "a
key can't give another more than it was given". But rotate-key was served
on any key: a key allowed to manage magpie from this computer only
(Admin local) could rotate a key allowed from anywhere (Admin any) and was
handed its new secret, or set one of its own choosing ("secret" in the
body). It then held a key that manages magpie from the network. It could
also turn off or remove that key, locking its owner out.

- internal/gui/admin.go adminMayChangeKey: a POST /gateway-keys/{action}
  naming another key that has Admin is 403. The caller's own key, keys
  without Admin and add-key go on as before. A gateway-keys file that
  can't be read is refused (unknown), not let through.
- docs/subsystems/gateway-routing.md says so.

Run (mote, at 2bcbae35e + this change):
- go test -count=1 -timeout 45m ./internal/gui/: ok 138.6s
- go vet ./internal/gui/ darwin, GOOS=linux -tags nogui, GOOS=windows: clean
- gofmt -l clean
Broken by hand: with the check skipped, TestAdminAPILeavesOtherManagingKeys
is red. A local key rotated the any key and got its secret
(sk-magpie-key-…), set "chosen-by-the-caller-1234" as its secret, turned it
off and removed it.
Not tried: a real gateway run over HTTP. The test drives serveAdmin with
the caller's identity in the context, as the gateway's admin() passes it on.
2026-11-13 03:49:41 +08:00
yetone 727e236378 gateway: a management API for providers, accounts and gateway keys, for a key set to manage (ljayic on X)
ljayic asked for a complete, stable management API so nothing needs a
hand-edited file. /v1/magpie/admin/ now serves the app's own /api handlers
(providers and their keys and accounts, OAuth sign-in start/poll/cancel,
gateway keys, account groups) to a gateway key set with
`magpie gateway-key admin <id> local|any`. Routes and actions not in
gui/admin.go's adminRoutes are 404, so a provider's saved key, a gateway
key's secret, sign-in export and actions on this computer's apps are not
served; "admin-key" is not served either, so the API can't raise its own
caller. Sign-in returns the vendor's page and code, opens nothing, takes no
password.

Guard (gateway/admin.go), in order: any Origin or Sec-Fetch-Site/-Mode is
403 with or without a key (Origin null, foreign, listed, *.localhost, an
app scheme); no key 401; a key without Key.Admin 403; from loopback the Host
must be ownHost (a rebound page's GET has no Origin); from the network the
key must be "any"; a proxied request counts as remote. Key.Admin is dropped
(adminOf) for a read-only, Browser, issued, expiring or default key, and
setting read-only clears it. The route is in readOnlyRefused and keySlotFree.

Docs: docs/reference.md "Management API", gateway-routing.md, and the
remote page in en, zh and ja. Routing Groups, sign-in export and a GUI
switch for the level are left to #6307.

Run (mica, the worktree before the rebase onto 9 newer commits): go vet of
access, cli, gateway, gui (darwin), GOOS=windows vet of the same four,
GOOS=linux -tags nogui vet of access, cli, gateway; gofmt -l empty;
go test -count=1 -timeout 45m ./internal/access ./internal/cli
./internal/gateway ./internal/gui: all ok but TestKeySlotEveryRoute, which
named the new route and is fixed here, then it and the read-only, admin and
guard tests pass again. After the rebase the same tests ran again.
Broken by hand, each red in TestAdminAPIGuards: browser check off (a
browser case served), Admin check off (a key without it served), Host check
off (a rebound name served), the "any" requirement off (a local-only key
from the network served, and a proxied request).
Tests: TestAdminAPIGuards (no key, unknown key, non-admin, read-only,
local/any, x-api-key, 8 browser headers x 3 callers, rebound Host, proxy,
no handler), TestAdminKey (access: levels, read-only, Browser, issued),
TestGatewayKeyCLIAdmin, TestAdminAPIGatewayKeys / Providers (a canary key
is never told) / SignIn, TestAdminRoutesEveryAPIRoute.
Not tried: a real sign-in against a vendor through the API, a request from
a second computer on the LAN, the GUI suite (no page or app.js changed), a
Linux/Windows run of the new tests.
2026-11-12 17:47:11 +08:00