Files
magpie/internal/gateway/disabled.go
T
yetone 841af36bfc gateway: models the user disabled are refused on every route, and leave every list (#1867)
oxsean asked for one place to turn a model (claude-fable-5) off for good:
not reached by a direct request, an old session, a routing group member,
a fallback, a sub-agent, count_tokens or a decision model.

What changed:
- settings.DisabledModels: patterns, an exact id or a glob (* any run, /
  included; ? one), matched ignoring case against provider/model, the
  bare id and its last segment (provider/disabled.go). Empty by default,
  so nothing changes for anyone who doesn't set it.
- Lists: buildEntries (entriesOff) leaves a disabled model out of the
  catalog, so /v1/models, agents' model files and pickers, Codex's list
  and every group go without it; membersIn skips it as a member, Deciders
  as a decision model; codexModels drops Codex's own disabled slugs and
  codexListTag changes so Codex refetches.
- Requests (gateway/disabled.go): 403 "model <id> is disabled in magpie"
  in each API's error shape, at #882's choke points: serve (idOff),
  perKeyBarred (no candidate: group next member, provider Fallback),
  count_tokens and Gemini :countTokens, serveSystemOne,
  serveDecisionGroup and postDecide, draw/video/embeddings, and
  codexBackend for Codex's own bare models. A group with a member left
  goes on without it; one with none left is refused naming them. A bare
  id the catalog no longer has is refused as disabled, not 404. A Codex
  sub-agent pick onto a disabled model stays on its lead's model.
- An unreadable settings.json is never "none disabled": the list read
  last stays; with none read, every request is refused; nothing is
  written over the file.
- CLI: magpie model disable <model|glob>..., enable ..., disabled
  (list-disabled).
- GUI: Settings > Models > Disabled models: each pattern with the models
  it matches now and Enable; Disable... opens the model picker, which
  also takes a typed glob. Says only requests through magpie are covered.
  POST /api/settings/disabled-models; POST /api/settings keeps the list.
  Strings in zh, zh-TW, ja, de.
- docs/subsystems/gateway-routing.md: Disabled models section.

What was run (worktree at origin/main 091593f7d, private GOCACHE, temp HOME):
- gofmt -l clean; go vet on darwin (touched pkgs), GOOS=linux -tags nogui
  ./..., GOOS=windows ./... clean; go build ./...
- go test -count=1 ./internal/provider ./internal/gateway ./internal/cli
  ./internal/gui ./internal/settings: all ok (gui after adding the field to
  TestSettingsSaveKeepsWhatItDoesNotSend's fixture, which it asked for).
- node --test internal/gui/tests/disabled-models.test.cjs: 16/16 (chromium
  + webkit, en/zh/ja/de, 1100/420); neighbouring Settings > Models tests
  and i18n-keys: 131/131.
- Broken by hand, each turned TestDisabled* red: serve's idOff (404
  instead of 403), perKeyBarred's filter (fallback reached spare), the
  catalog filter (/v1/models and Codex list), countOff (count_tokens 200),
  postDecide's check (decision call went out).
- Not tried with a real vendor account or a real agent.

Ref #1867
2026-10-13 05:34:13 +08:00

128 lines
4.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package gateway
import (
"fmt"
"net/http"
"strings"
"github.com/yetone/magpie/internal/provider"
)
// Disabled models (#1867, provider/disabled.go): a model the user turned
// off is refused on every route before anything is sent upstream, with a
// 403 in the API's own shape. A routing group goes on without it; one left
// with none is refused the same way. The catalog leaves a disabled model
// out, so the lists and the groups never offer it; the checks here are for
// the requests that name it anyway — an agent's old config, a session that
// was on it, a fallback, a sub-agent's pick, count_tokens, a decision
// model — and candidates() never plans one (perKeyBarred).
// disabledError is what a request for a disabled model is told.
func disabledError(id, pat string) string {
return fmt.Sprintf("model %s is disabled in magpie (it matches %q in Settings › Models › Disabled models); enable it there or with `magpie model enable %s`. Only requests through magpie are refused", id, pat, pat)
}
// unknownDisabledError is what every request is told while settings.json
// can't be read and no list of disabled models was read before it.
func unknownDisabledError(id string) string {
return fmt.Sprintf("model %s is disabled in magpie for now: settings.json can't be read, so magpie doesn't know which models are disabled; fix or restore settings.json", id)
}
// groupDisabledError is what a request for a group whose every model is
// disabled is told.
func groupDisabledError(id string, g provider.Group, off []string) string {
return fmt.Sprintf("model %s is disabled in magpie: every model of the routing group %s is (%s); enable one in Settings › Models › Disabled models or with `magpie model enable`. Only requests through magpie are refused", id, g.Name, strings.Join(off, ", "))
}
// modelOff is the refusal for p's model, resolved, as id names it to the
// caller; ok is false when it isn't disabled.
func modelOff(p provider.Provider, model, id string) (string, bool) {
if provider.DisabledUnknown() {
return unknownDisabledError(id), true
}
if pat, off := provider.ModelDisabled(p.ID, model); off {
return disabledError(id, pat), true
}
return "", false
}
// idOff is the refusal for a model id as asked, resolved or not: a group's
// when every model it would have is disabled, else the model's.
func idOff(id string) (string, bool) {
id = strings.TrimSpace(id)
if id == "" {
return "", false
}
if provider.DisabledUnknown() {
return unknownDisabledError(id), true
}
if len(provider.DisabledModels()) == 0 {
return "", false
}
gid := id
if g, ok := provider.GroupFor(id); ok {
gid = g // a group asked by its bare id or name, as serve takes it
}
if g, off, ok := provider.GroupDisabledOut(gid); ok {
return groupDisabledError(id, g, off), true
}
if _, _, isGroup := provider.FindGroup(gid); isGroup {
return "", false // a group with a model left: it goes on without the rest
}
if pat, off := provider.IDDisabled(id); off {
return disabledError(id, pat), true
}
if p, model, ok := provider.Resolve(id); ok {
return modelOff(p, model, id)
}
// a bare id that resolves to nothing because the catalog left it out:
// one of the providers' models the user disabled ("embed-1" with
// lib/embed-1 disabled), refused as itself, not as a model magpie
// doesn't know
if pat, off := provider.BareDisabled(id); off {
return disabledError(id, pat), true
}
return "", false
}
// refuseOff answers a request for a disabled model with its 403 in proto's
// shape; false when the model isn't disabled.
func refuseOff(w http.ResponseWriter, proto provider.Protocol, id string) bool {
msg, off := idOff(id)
if off {
writeError(w, proto, http.StatusForbidden, msg)
}
return off
}
// countOff is refuseOff for count_tokens, id resolved as the count is: a
// bare group's name, an auto group's stand-in.
func countOff(w http.ResponseWriter, proto provider.Protocol, id string) bool {
if gid, ok := provider.GroupFor(id); ok {
id = gid
}
if sid, ok := provider.AutoStandIn(id); ok {
id = sid
}
return refuseOff(w, proto, id)
}
// codexNativeOff is the refusal for one of Codex's own models, asked at
// the ChatGPT backend's route by its bare id: disabled by that id, or as
// the codex provider's ("codex/gpt-5.5").
func codexNativeOff(model string) (string, bool) {
if model == "" {
return "", false
}
if provider.DisabledUnknown() {
return unknownDisabledError(model), true
}
if pat, off := provider.IDDisabled(model); off {
return disabledError(model, pat), true
}
if pat, off := provider.ModelDisabled("codex", model); off {
return disabledError(model, pat), true
}
return "", false
}