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
306 lines
8.4 KiB
Go
306 lines
8.4 KiB
Go
package provider
|
|
|
|
import (
|
|
"log"
|
|
"slices"
|
|
"strings"
|
|
"sync"
|
|
|
|
"github.com/yetone/magpie/internal/catalog"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
// Disabled models (#1867): models the user turned off for every request
|
|
// through magpie. Settings' DisabledModels holds them as patterns, an exact
|
|
// id or a glob ("*fable*", "anthropic/claude-fable-5*"), each matched,
|
|
// ignoring case, against a model's "<provider>/<model>", its bare id and
|
|
// the bare id's last segment. A disabled model is left out of the catalog
|
|
// (so every list, agent file and routing group goes without it), and the
|
|
// gateway turns away a request for it on every route, before any upstream
|
|
// call: a request that names it, a group member, a fallback, a sub-agent's
|
|
// model, count_tokens, a decision model.
|
|
|
|
// lastDisabled is the list as last read whole. settings.json that can't be
|
|
// read, or doesn't parse with no last good generation, holds a list nobody
|
|
// knows: it is never taken for "none disabled". The one read last stays in
|
|
// force; with none read since magpie started, Disabled says it is unknown.
|
|
var lastDisabled struct {
|
|
sync.Mutex
|
|
list []string
|
|
read bool
|
|
warned bool
|
|
}
|
|
|
|
// disabledList is the patterns in force, and unknown when settings.json
|
|
// couldn't be read and none was read before.
|
|
func disabledList() (list []string, unknown bool) {
|
|
s, err := settings.LoadChecked()
|
|
lastDisabled.Lock()
|
|
defer lastDisabled.Unlock()
|
|
if err == nil {
|
|
lastDisabled.list, lastDisabled.read, lastDisabled.warned = s.DisabledModels, true, false
|
|
return s.DisabledModels, false
|
|
}
|
|
if !lastDisabled.warned {
|
|
lastDisabled.warned = true
|
|
log.Printf("disabled models: %v; the list read last stays in force", err)
|
|
}
|
|
return lastDisabled.list, !lastDisabled.read
|
|
}
|
|
|
|
// DisabledModels are the patterns the user disabled, as saved.
|
|
func DisabledModels() []string {
|
|
list, _ := disabledList()
|
|
return slices.Clone(list)
|
|
}
|
|
|
|
// DisabledUnknown reports that settings.json can't be read and no list was
|
|
// read before it: the gateway then refuses every model, since any of them
|
|
// may be one the user disabled.
|
|
func DisabledUnknown() bool {
|
|
_, unknown := disabledList()
|
|
return unknown
|
|
}
|
|
|
|
// matchDisabled is the pattern of list that disables a model, if any.
|
|
// names are what the model answers to.
|
|
func matchDisabled(list []string, names ...string) (string, bool) {
|
|
for _, pat := range list {
|
|
lp := strings.ToLower(strings.TrimSpace(pat))
|
|
if lp == "" {
|
|
continue
|
|
}
|
|
for _, n := range names {
|
|
if n == "" {
|
|
continue
|
|
}
|
|
if globMatch(lp, strings.ToLower(n)) {
|
|
return pat, true
|
|
}
|
|
}
|
|
}
|
|
return "", false
|
|
}
|
|
|
|
// globMatch is a simple glob: * is any run of characters, "/" among them
|
|
// (a model id has slashes of its own, openrouter's anthropic/claude-fable-5,
|
|
// and "*fable*" must cross them), ? is any one, and everything else is
|
|
// itself.
|
|
func globMatch(pat, s string) bool {
|
|
if !strings.ContainsAny(pat, "*?") {
|
|
return pat == s
|
|
}
|
|
star, mark := -1, 0
|
|
pi, si := 0, 0
|
|
for si < len(s) {
|
|
switch {
|
|
case pi < len(pat) && pat[pi] == '*':
|
|
star, mark = pi, si
|
|
pi++
|
|
case pi < len(pat) && (pat[pi] == '?' || pat[pi] == s[si]):
|
|
pi++
|
|
si++
|
|
case star >= 0:
|
|
mark++
|
|
pi, si = star+1, mark
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
for pi < len(pat) && pat[pi] == '*' {
|
|
pi++
|
|
}
|
|
return pi == len(pat)
|
|
}
|
|
|
|
// namesOf are what a provider's model answers to: "<provider>/<model>",
|
|
// its bare id, and the bare id's last segment (claude-fable-5 for
|
|
// anthropic/claude-fable-5 at OpenRouter).
|
|
func namesOf(pid, model string) []string {
|
|
model = strings.TrimSpace(model)
|
|
out := []string{model}
|
|
if pid != "" {
|
|
out = append(out, pid+"/"+model)
|
|
}
|
|
if i := strings.LastIndex(model, "/"); i >= 0 && i+1 < len(model) {
|
|
out = append(out, model[i+1:])
|
|
}
|
|
return out
|
|
}
|
|
|
|
// ModelDisabled reports whether the user disabled pid's model, and the
|
|
// pattern that did. A list nobody knows (DisabledUnknown) disables none
|
|
// here: the gateway asks DisabledUnknown itself.
|
|
func ModelDisabled(pid, model string) (string, bool) {
|
|
list, _ := disabledList()
|
|
if len(list) == 0 {
|
|
return "", false
|
|
}
|
|
return matchDisabled(list, namesOf(pid, model)...)
|
|
}
|
|
|
|
// disabledNow is ModelDisabled with the list read once, for a loop over
|
|
// many models.
|
|
func disabledNow() func(pid, model string) bool {
|
|
list, _ := disabledList()
|
|
if len(list) == 0 {
|
|
return func(string, string) bool { return false }
|
|
}
|
|
return func(pid, model string) bool {
|
|
_, off := matchDisabled(list, namesOf(pid, model)...)
|
|
return off
|
|
}
|
|
}
|
|
|
|
// IDDisabled reports whether a model id as asked ("<provider>/<model>" or a
|
|
// bare id) is one the user disabled, and the pattern that did.
|
|
func IDDisabled(id string) (string, bool) {
|
|
list, _ := disabledList()
|
|
if len(list) == 0 {
|
|
return "", false
|
|
}
|
|
id = strings.TrimSpace(id)
|
|
names := []string{id}
|
|
if i := strings.LastIndex(id, "/"); i >= 0 && i+1 < len(id) {
|
|
names = append(names, id[i+1:])
|
|
}
|
|
if pid, m, ok := strings.Cut(id, "/"); ok && pid != "" && m != "" {
|
|
names = append(names, m)
|
|
}
|
|
return matchDisabled(list, names...)
|
|
}
|
|
|
|
// normalDisabled trims, drops empties and repeats (ignoring case), and
|
|
// keeps the order given.
|
|
func normalDisabled(list []string) []string {
|
|
var out []string
|
|
for _, p := range list {
|
|
p = strings.TrimSpace(p)
|
|
if p == "" || slices.ContainsFunc(out, func(o string) bool { return strings.EqualFold(o, p) }) {
|
|
continue
|
|
}
|
|
out = append(out, p)
|
|
}
|
|
return out
|
|
}
|
|
|
|
// SetDisabledModels saves the disabled list whole.
|
|
func SetDisabledModels(list []string) error {
|
|
s, err := settings.LoadChecked()
|
|
if err != nil {
|
|
return err // never write a list over one nobody can read
|
|
}
|
|
keep := normalDisabled(list)
|
|
if slices.Equal(s.DisabledModels, keep) {
|
|
return nil
|
|
}
|
|
s.DisabledModels = keep
|
|
if err := settings.Save(s); err != nil {
|
|
return err
|
|
}
|
|
Changed()
|
|
catalog.Touched()
|
|
return nil
|
|
}
|
|
|
|
// DisableModels adds patterns to the disabled list.
|
|
func DisableModels(pats ...string) error {
|
|
s, err := settings.LoadChecked()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return SetDisabledModels(append(slices.Clone(s.DisabledModels), pats...))
|
|
}
|
|
|
|
// EnableModels takes patterns off the disabled list (ignoring case); it
|
|
// says which of them weren't on it.
|
|
func EnableModels(pats ...string) (missing []string, err error) {
|
|
s, err := settings.LoadChecked()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
keep := slices.Clone(s.DisabledModels)
|
|
for _, p := range pats {
|
|
p = strings.TrimSpace(p)
|
|
n := len(keep)
|
|
keep = slices.DeleteFunc(keep, func(o string) bool { return strings.EqualFold(o, p) })
|
|
if len(keep) == n && p != "" {
|
|
missing = append(missing, p)
|
|
}
|
|
}
|
|
return missing, SetDisabledModels(keep)
|
|
}
|
|
|
|
// GroupDisabledOut is the group id names when it has no model left only
|
|
// because the user disabled every one it would have (#1867), and those
|
|
// models: the gateway says so, not that the group is empty.
|
|
func GroupDisabledOut(id string) (Group, []string, bool) {
|
|
g, ms, ok := FindGroup(id)
|
|
if !ok || len(ms) > 0 {
|
|
return Group{}, nil, false
|
|
}
|
|
none := func(string, string) bool { return false }
|
|
entries := entriesOff(none)
|
|
all := groupsIn(entries)
|
|
whole, ok := groupOf(all, g.ID)
|
|
if !ok {
|
|
return Group{}, nil, false
|
|
}
|
|
var off []string
|
|
for _, m := range membersOff(entries, all, whole, none) {
|
|
off = append(off, m.Provider.ID+"/"+m.Model)
|
|
}
|
|
if len(off) == 0 {
|
|
return Group{}, nil, false
|
|
}
|
|
return g, off, true
|
|
}
|
|
|
|
// DisabledBy reports whether the one pattern pat disables pid's model.
|
|
func DisabledBy(pat, pid, model string) bool {
|
|
_, off := matchDisabled([]string{pat}, namesOf(pid, model)...)
|
|
return off
|
|
}
|
|
|
|
// DisabledHits are the providers' models, on and listed, that pat turns
|
|
// off now, as "<provider>/<model>".
|
|
func DisabledHits(pat string) []string {
|
|
var out []string
|
|
for _, p := range All() {
|
|
if !p.On() {
|
|
continue
|
|
}
|
|
for _, m := range p.Exposed() {
|
|
if DisabledBy(pat, p.ID, m.ID) {
|
|
out = append(out, p.ID+"/"+m.ID)
|
|
}
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// BareDisabled reports whether a bare id (no provider in it) is the id of
|
|
// a model of a provider that is on and the user disabled, and the pattern
|
|
// that did: the catalog leaves it out, so Resolve doesn't find it.
|
|
func BareDisabled(id string) (string, bool) {
|
|
list, _ := disabledList()
|
|
id = strings.TrimSpace(id)
|
|
if len(list) == 0 || id == "" {
|
|
return "", false
|
|
}
|
|
for _, p := range All() {
|
|
if !p.On() {
|
|
continue
|
|
}
|
|
for _, m := range p.Exposed() {
|
|
if !strings.EqualFold(m.ID, id) {
|
|
continue
|
|
}
|
|
if pat, off := matchDisabled(list, namesOf(p.ID, m.ID)...); off {
|
|
return pat, true
|
|
}
|
|
}
|
|
}
|
|
return "", false
|
|
}
|