Linka on Discord (#feedback): in a gateway key's "Models this key can
use", ticking only an account and no model let the key list and use
every model of every provider.
Cause: Key.Models empty meant "every model", and Key.Accounts (#905)
held only the providers it names, so every other provider stayed open.
Change: access.HeldModels gives a key with accounts and no models the
models "<provider>/*" of each account's provider. Authenticate builds
the Identity with it, so every route's existing model gate narrows:
/v1/models, /v1beta/models, chat, responses, messages, Gemini,
count_tokens, embeddings, images, the Codex backend path, System One,
groups and fallbacks. A key that lists models is unchanged (#905: its
accounts still hold only the providers they name); a key with neither
is unchanged. The CLI's MODELS column and `gateway-key models` say so,
and the GUI editor's hint, the "not used" notes on providers no model
or account reaches, and the save status follow (en, zh, zh-TW, ja, de).
Tests:
- TestGatewayKeyAccountsOnlyHoldTheModels: each route above is 403 for
the accounts-only key and no upstream call reaches the other
provider, the decider or the image provider; its own provider, a
group of it and a key that also lists models work. Red on the old
code on every route (list showed spare/m2, chat/count_tokens/
embeddings/systemone 200).
- TestHeldModels (access).
- gateway-key-accounts.test.cjs: hint, notes and status; 4/4 red on
the old assets, green on the new.
- TestGatewayKeyAccountsListModels / SystemOne / OfANamedProvider and
TestHeldKeyToldItsOwnQuotas now encode the new rule (the held key is
told only its own provider's quotas).
Run on mote: gofmt -l clean; go vet darwin, linux -tags nogui, windows
on access, cli, gateway, gui ok; go test -timeout 45m ./internal/access
./internal/cli ./internal/gui ./internal/gateway: ok except gui's
TestMainWindowCorners/macOS_26_SDK (#3949, not this change) and
gateway's TestHeldKeyToldItsOwnQuotas, updated and rerun ok. After the
rebase on 1f03d7546 the key tests ran again ok. GUI: gateway-key*,
i18n-keys, gui-de, gui-ja, gui-zh-tw in Chromium and WebKit: 131 pass.
Not tried: a plugin-served provider (same model gate), a real
account, Windows and Linux test runs (vet only).
LumenXY's /v1/models over Tailscale took 12.4s with a gateway key held to
some models and 1.5s with an unheld one, so apps that list models timed
out and showed an empty list.
Cause (pprof, 1,000 models, 500 found groups): keyAllowed checked each
catalog entry on its own. Each group entry called provider.FindGroup,
which builds a new GroupFinder, so every provider's models were read again
(providerEntries, its files stat'ed and settings loaded) once a group.
That was 96% of the request. Each glob in the key's models was also
compiled to a regexp on every check, and a key held to accounts listed
each provider's accounts (Logins, from disk) once a model.
What changed:
- gateway/key_models.go: keyAllowed weighs a list through a keyList made
for that request alone. It has one GroupFinder and each provider's
AccountIDs read once. entryAllowed and accountServes are the same
checks over them. accountServes(who, p, model) stays for decide.go's
single check.
- access/models.go: globRe keeps each pattern's compiled expression. A
pattern compiles to the same expression whatever key holds it, so
nothing kept goes stale. The map is let go of whole past 4096 patterns.
- gui/groups.go: groupsState (the Routing page) called FindGroup once a
group too, the same class. It now uses one GroupFinder.
Every route that lists models goes through keyAllowed: /v1/models,
/v1/models/{id}, /v1beta/models, /v1/codex/models, Muse's list and the
deciders. The single-model checks (serve, count_tokens, embeddings,
System One, draw, video) ask one group and are unchanged. Nothing is kept
past a request, so a change to a key's models or accounts, a group's
members or an account's own models shows on the next request. No key
limit is loosened. The documented behaviour is unchanged, so no subsystem
page changes.
Tests:
- TestKeyListShowsWhatItDid compares keyAllowed with the old per-entry
check, kept in the test, for 14 keys over a catalog with found groups,
named and nested groups, a no-key provider and an account's own models.
The keys cover exact ids in any case, globs, group/<id>, group/*, a
group inside one named, accounts, and models with accounts.
- TestKeyListSeesChangesNextRequest goes through /v1/models and
/v1beta/models on one server while the key's models, the group's members,
the key's accounts and an account's models change.
- TestModelPatternsKeptCompiled checks each pattern keeps its own
expression and that past the cap works.
- Broken by hand, each went red: the accounts memo shared across providers
(ShowsWhatItDid), the GroupFinder kept across requests
(SeesChangesNextRequest), and the glob cache keyed by the pattern's first
letter (KeptCompiled).
Numbers, BenchmarkHeldKeyListsModels (2 providers x 500 models, 500 found
groups, M4 Pro):
- held to models: 2.36s before, 55ms after
- held to accounts: 2.35s before, 60ms after
- held to none: 43ms either way
Real use: sandbox magpie on 127.0.0.1:3486 with its own HOME, the same
1,500-entry catalog, a key held to 5 patterns, and curl 3x each:
- held key, /v1/models and /v1beta/models: 27.8-28.7s before, 0.18s after
- unheld key: 0.12s either way
- The held list (212 entries) and the unheld one (1500) were
byte-identical between the old and new builds.
- Changing the key's models while the server ran showed on the next
request (10 entries, then 212 again).
Run at origin/main 8f2a314cf:
- go test ./...: every package ok (49), no FAIL
- go test -race -count=3 on the access tests and -count=2 on TestKeyList*,
TestGatewayKeyModels* and TestGatewayKeyAccounts*
- gofmt -l and go vet ./... (darwin, GOOS=windows); GOOS=linux go vet
-tags nogui on access, gateway and gui