Files
magpie/internal/provider/disabled_test.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

171 lines
5.3 KiB
Go

package provider
import (
"os"
"path/filepath"
"slices"
"testing"
"github.com/yetone/magpie/internal/settings"
)
func TestDisabledGlob(t *testing.T) {
for _, c := range []struct {
pat, s string
ok bool
}{
{"*fable*", "claude-fable-5", true},
{"*fable*", "anthropic/claude-fable-5", true},
{"claude-fable-5", "claude-fable-5", true},
{"claude-fable-5", "claude-fable-5-mini", false},
{"claude-fable-?", "claude-fable-5", true},
{"anthropic/*", "anthropic/claude-fable-5", true},
{"anthropic/*", "openrouter/anthropic/claude-fable-5", false},
{"*", "", true},
{"a*b*c", "aXbYc", true},
{"a*b*c", "aXbY", false},
} {
if got := globMatch(c.pat, c.s); got != c.ok {
t.Errorf("%q ~ %q: %v", c.pat, c.s, got)
}
}
if pat, ok := matchDisabled([]string{" ", "*FABLE*"}, namesOf("openrouter", "anthropic/claude-fable-5")...); !ok || pat != "*FABLE*" {
t.Fatal(pat, ok)
}
// an exact id matches the bare model, provider/model and the last segment
for _, pat := range []string{"claude-fable-5", "openrouter/anthropic/claude-fable-5", "anthropic/claude-fable-5"} {
if !DisabledBy(pat, "openrouter", "anthropic/claude-fable-5") {
t.Error(pat)
}
}
if DisabledBy("anthropic/*", "openrouter", "x") {
t.Error("another provider's model")
}
}
func disabledHome(t *testing.T) {
t.Helper()
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
for _, p := range []Provider{
{ID: "a", Name: "A", Key: "k", Chat: "http://a.invalid/v1", Models: []string{"claude-fable-5", "m1"}},
{ID: "b", Name: "B", Key: "k", Chat: "http://b.invalid/v1", Models: []string{"claude-fable-5-mini"}},
} {
if err := Save(p); err != nil {
t.Fatal(err)
}
}
for _, g := range []Group{
{ID: "mix", Name: "Mix", Members: []string{"a/claude-fable-5", "a/m1"}, Routing: Ordered},
{ID: "fables", Name: "Fables", Members: []string{"a/claude-fable-5", "b/claude-fable-5-mini"}, Routing: Ordered},
} {
if err := SaveGroup(g); err != nil {
t.Fatal(err)
}
}
}
// A disabled model is left out of the catalog and of every group's
// members; a group left with none is told apart from an empty one; with
// nothing disabled, nothing changes.
func TestDisabledLeftOutOfTheCatalog(t *testing.T) {
disabledHome(t)
ids := func() []string {
var out []string
for _, e := range Catalog() {
out = append(out, e.ID)
}
return out
}
before := ids()
for _, id := range []string{"a/claude-fable-5", "b/claude-fable-5-mini", "group/mix", "group/fables"} {
if !slices.Contains(before, id) {
t.Fatalf("before: %v", before)
}
}
if _, _, ok := GroupDisabledOut("group/mix"); ok {
t.Fatal("nothing disabled")
}
if err := DisableModels("*fable*"); err != nil {
t.Fatal(err)
}
after := ids()
for _, id := range []string{"a/claude-fable-5", "b/claude-fable-5-mini", "group/fables"} {
if slices.Contains(after, id) {
t.Fatalf("%s still listed: %v", id, after)
}
}
if !slices.Contains(after, "a/m1") || !slices.Contains(after, "group/mix") {
t.Fatalf("after: %v", after)
}
if _, ms, ok := FindGroup("group/mix"); !ok || len(ms) != 1 || ms[0].Model != "m1" {
t.Fatalf("mix members %+v", ms)
}
if _, _, ok := GroupDisabledOut("group/mix"); ok {
t.Fatal("a group with one left")
}
g, off, ok := GroupDisabledOut("group/fables")
if !ok || g.ID != "fables" || !slices.Equal(off, []string{"a/claude-fable-5", "b/claude-fable-5-mini"}) {
t.Fatal(g, off, ok)
}
if hits := DisabledHits("*fable*"); !slices.Equal(hits, []string{"a/claude-fable-5", "b/claude-fable-5-mini"}) {
t.Fatal(hits)
}
if pat, ok := BareDisabled("claude-fable-5"); !ok || pat != "*fable*" {
t.Fatal(pat, ok)
}
// the same pattern twice, or differing only in case, is kept once
if err := DisableModels("*FABLE*", " m9 "); err != nil {
t.Fatal(err)
}
if got := DisabledModels(); !slices.Equal(got, []string{"*fable*", "m9"}) {
t.Fatal(got)
}
if missing, err := EnableModels("*Fable*", "m9", "nope"); err != nil || !slices.Equal(missing, []string{"nope"}) {
t.Fatal(missing, err)
}
if got := ids(); !slices.Equal(got, before) {
t.Fatalf("enabled again: %v, was %v", got, before)
}
}
// settings.json that can't be read is never taken for "none disabled": the
// list read last stays, and with none read, every model is in doubt and
// nothing is written over the file.
func TestDisabledUnreadSettings(t *testing.T) {
disabledHome(t)
if err := DisableModels("m1"); err != nil {
t.Fatal(err)
}
if _, off := ModelDisabled("a", "m1"); !off {
t.Fatal("not disabled")
}
path := settings.Path()
os.Remove(path + ".bak")
if err := os.WriteFile(path, []byte("{not json"), 0o600); err != nil {
t.Fatal(err)
}
if _, err := settings.LoadChecked(); err == nil {
t.Skip("settings recovered the file; nothing unread to test")
}
if _, off := ModelDisabled("a", "m1"); !off || DisabledUnknown() {
t.Fatal("the last list read was dropped")
}
if err := SetDisabledModels(nil); err == nil {
t.Fatal("wrote a list over a file nobody can read")
}
// none read before: unknown
lastDisabled.Lock()
lastDisabled.list, lastDisabled.read = nil, false
lastDisabled.Unlock()
if !DisabledUnknown() {
t.Fatal("an unread list taken for none")
}
if b, _ := os.ReadFile(path); string(b) != "{not json" {
t.Fatal("the file was written over", string(b))
}
}