diff --git a/internal/gui/providers_outputs_retired_test.go b/internal/gui/providers_outputs_retired_test.go new file mode 100644 index 000000000..78fcfa0fa --- /dev/null +++ b/internal/gui/providers_outputs_retired_test.go @@ -0,0 +1,78 @@ +package gui + +import ( + "maps" + "net/http" + "net/http/httptest" + "os" + "strings" + "testing" + + "github.com/yetone/magpie/internal/provider" + "github.com/yetone/magpie/internal/settings" +) + +// The reporter's case through the editor's Save (#3305, LoKi): a Max +// output set on a model the provider has since stopped listing +// (opencode-zen-free/space-bunny-free=65536) is given to the editor and +// sent back with every Save, with no model picked too. The Save goes +// through and keeps it; a newly typed unknown model is still refused. +func TestProviderSaveKeepsRetiredModelOutput(t *testing.T) { + t.Setenv("HOME", t.TempDir()) + t.Setenv("USERPROFILE", os.Getenv("HOME")) + t.Setenv("XDG_CONFIG_HOME", t.TempDir()) + t.Setenv("XDG_CACHE_HOME", t.TempDir()) + mux := http.NewServeMux() + providerRoutes(mux, nil) + post := func(body string) *httptest.ResponseRecorder { + t.Helper() + w := httptest.NewRecorder() + mux.ServeHTTP(w, httptest.NewRequest("POST", "/api/provider/save", strings.NewReader(body))) + return w + } + if w := post(`{"id":"relay","name":"Relay","key":"k","chat":"http://127.0.0.1:1/v1","models":["sol","space-bunny-free"],"new":true}`); w.Code != 200 { + t.Fatalf("%d %s", w.Code, w.Body) + } + const head = `{"id":"relay","from":"relay","name":"Relay","chat":"http://127.0.0.1:1/v1"` + if w := post(head + `,"models":["sol","space-bunny-free"],"outputs":{"space-bunny-free":65536},"compacts":{"space-bunny-free":200000}}`); w.Code != 200 { + t.Fatalf("%d %s", w.Code, w.Body) + } + // the model leaves the list + if w := post(head + `,"models":["sol"],"outputs":{"space-bunny-free":65536},"compacts":{"space-bunny-free":200000}}`); w.Code != 200 { + t.Fatalf("%d %s", w.Code, w.Body) + } + p, err := provider.Find("relay") + if err != nil { + t.Fatal(err) + } + if provider.ServesModel(*p, "space-bunny-free") { + t.Fatal("space-bunny-free is still served; the test needs it gone") + } + info := providerInfo(*p, nil) + if info.Outputs["space-bunny-free"] != 65536 || info.Compacts["space-bunny-free"] != 200000 { + t.Fatalf("the editor is given %v and %v", info.Outputs, info.Compacts) + } + // Select none, the rest as the editor has it + if w := post(head + `,"models":[],"noModels":true,"outputs":{"space-bunny-free":65536},"compacts":{"space-bunny-free":200000}}`); w.Code != 200 { + t.Fatalf("a Save with no model picked: %d %s", w.Code, w.Body) + } + if w := post(head + `,"models":["sol"],"outputs":{"space-bunny-free":65536,"sol":32000},"compacts":{"space-bunny-free":200000}}`); w.Code != 200 { + t.Fatalf("a Save with the stale limit beside a new one: %d %s", w.Code, w.Body) + } + want := map[string]int{"relay/space-bunny-free": 65536, "relay/sol": 32000} + if got := settings.Load().ModelOutputs; !maps.Equal(got, want) { + t.Errorf("outputs %v; want %v", got, want) + } + if got := settings.Load().ModelCompacts; !maps.Equal(got, map[string]int{"relay/space-bunny-free": 200000}) { + t.Errorf("compacts %v", got) + } + if w := post(head + `,"models":["sol"],"outputs":{"space-bunny-free":65536,"typo":5}}`); w.Code == 200 || !strings.Contains(w.Body.String(), "typo") { + t.Fatalf("a newly typed model it doesn't serve: %d %s", w.Code, w.Body) + } + if w := post(head + `,"models":["sol"],"outputs":{"sol":32000},"compacts":{}}`); w.Code != 200 { + t.Fatalf("cleared: %d %s", w.Code, w.Body) + } + if got := settings.Load().ModelOutputs; !maps.Equal(got, map[string]int{"relay/sol": 32000}) || len(settings.Load().ModelCompacts) != 0 { + t.Errorf("cleared: %v %v", got, settings.Load().ModelCompacts) + } +} diff --git a/internal/provider/compact.go b/internal/provider/compact.go index 136cb58de..2fcdfb21c 100644 --- a/internal/provider/compact.go +++ b/internal/provider/compact.go @@ -168,14 +168,16 @@ func CompactsOf(providerID string) map[string]int { // SetModelCompacts makes a provider's thresholds cs, by model id, "*" for // all of them, as its editor's Compact at says: those it leaves out are -// taken away, and every one set has to be a model the provider serves, as -// for a reply limit (SetModelOutputs). Codex's and Claude Code's lists are -// written again when anything changed. +// taken away, and every one set or changed has to be a model the provider +// serves, as for a reply limit (SetModelOutputs); one sent back as it is +// for a model the provider no longer lists is kept, not refused (#3305). +// Codex's and Claude Code's lists are written again when anything changed. func SetModelCompacts(providerID string, cs map[string]int) error { p, err := Find(providerID) if err != nil { return err } + s := settings.Load() for model, n := range cs { if n < 0 { return errorf("a compaction threshold is a number of tokens, not %d", n) @@ -183,11 +185,10 @@ func SetModelCompacts(providerID string, cs map[string]int) error { if err := settings.CheckModelKey("a compaction threshold", p.ID+"/"+model); err != nil { return err } - if model != "*" && n > 0 && !p.serves(model) { + if model != "*" && n > 0 && s.ModelCompacts[p.ID+"/"+model] != n && !p.serves(model) { return errorf("%s has no model %s (magpie provider %s lists them)", p.ID, model, p.ID) } } - s := settings.Load() changed := false for k := range s.ModelCompacts { if model, ok := strings.CutPrefix(k, p.ID+"/"); ok && cs[model] <= 0 { diff --git a/internal/provider/limits_retired_test.go b/internal/provider/limits_retired_test.go new file mode 100644 index 000000000..02350ac9d --- /dev/null +++ b/internal/provider/limits_retired_test.go @@ -0,0 +1,79 @@ +package provider + +import ( + "maps" + "testing" + + "github.com/yetone/magpie/internal/settings" +) + +// A Max output or Compact at the user set on a model the provider has +// since stopped listing comes back from the editor with every Save (#3305, +// LoKi: opencode-zen-free/space-bunny-free=65536 refused each Save of Zen +// Free, even with no model picked). Sent back as it is, it is kept and the +// rest is saved; changed, or typed for a model never listed, it is still +// refused; taken out, it comes off. +func TestRetiredModelLimitsDontBlockTheEditor(t *testing.T) { + limitsHome(t) + if err := Save(Provider{ID: "relay", Name: "relay", Chat: "https://relay.example/v1", Key: "k", Models: []string{"sol", "luna"}}); err != nil { + t.Fatal(err) + } + if err := SetModelOutput("relay/luna", 65536); err != nil { + t.Fatal(err) + } + if err := SetModelCompacts("relay", map[string]int{"luna": 200000}); err != nil { + t.Fatal(err) + } + // luna leaves the provider's list + p, err := Find("relay") + if err != nil { + t.Fatal(err) + } + p.Models = []string{"sol"} + if err := Save(*p); err != nil { + t.Fatal(err) + } + if p, _ = Find("relay"); ServesModel(*p, "luna") { + t.Fatal("luna is still served; the test needs it gone") + } + + for _, c := range []struct { + name string + set func(map[string]int) error + of func(string) map[string]int + read func() map[string]int + }{ + {"outputs", func(m map[string]int) error { return SetModelOutputs("relay", m) }, OutputsOf, func() map[string]int { return settings.Load().ModelOutputs }}, + {"compacts", func(m map[string]int) error { return SetModelCompacts("relay", m) }, CompactsOf, func() map[string]int { return settings.Load().ModelCompacts }}, + } { + t.Run(c.name, func(t *testing.T) { + back := c.of("relay") // what the editor is given and sends again + old := back["luna"] + if old == 0 { + t.Fatalf("the editor isn't given luna's: %v", back) + } + back["sol"] = 32000 + if err := c.set(back); err != nil { + t.Fatalf("a Save with luna's kept as it was: %v", err) + } + if got := c.read(); !maps.Equal(got, map[string]int{"relay/luna": old, "relay/sol": 32000}) { + t.Errorf("saved %v", got) + } + if err := c.set(map[string]int{"luna": old + 1, "sol": 32000}); err == nil { + t.Error("a changed value for a model relay no longer serves was accepted") + } + if err := c.set(map[string]int{"luna": old, "sol": 32000, "typo": 5}); err == nil { + t.Error("a model relay never served was accepted") + } + if got := c.read(); !maps.Equal(got, map[string]int{"relay/luna": old, "relay/sol": 32000}) { + t.Errorf("a refused save changed them: %v", got) + } + if err := c.set(map[string]int{"sol": 32000}); err != nil { + t.Fatal(err) + } + if got := c.read(); !maps.Equal(got, map[string]int{"relay/sol": 32000}) { + t.Errorf("luna's taken out: %v", got) + } + }) + } +} diff --git a/internal/provider/models.go b/internal/provider/models.go index 42b77504d..2f757ca18 100644 --- a/internal/provider/models.go +++ b/internal/provider/models.go @@ -2845,14 +2845,21 @@ func OutputsOf(providerID string) map[string]int { // SetModelOutputs makes a provider's reply limits outs, by model id, "*" // for all of them, as its editor's Max output says (ARNO on Discord: a // model's maxTokens was wrong, and only the context window could be set in -// the editor): those it leaves out are taken away, and every one set has -// to be a model the provider serves, as for SetModelOutput. Nothing is -// saved, nor the agents told, when nothing changed. +// the editor): those it leaves out are taken away, and every one set or +// changed has to be a model the provider serves, as for SetModelOutput. +// One the user set for a model the provider has since stopped listing, +// sent back as it is, is kept and not refused: the editor is given it +// (OutputsOf) and sends it again on every Save, so refusing it stopped +// each Save of that provider, whatever else changed, and the model may be +// listed again at the next refresh (#3305, LoKi). Taking it out of Max +// output clears it. Nothing is saved, nor the agents told, when nothing +// changed. func SetModelOutputs(providerID string, outs map[string]int) error { p, err := Find(providerID) if err != nil { return err } + s := settings.Load() for model, n := range outs { if n < 0 { return errorf("an output limit is a number of tokens, not %d", n) @@ -2860,11 +2867,10 @@ func SetModelOutputs(providerID string, outs map[string]int) error { if err := settings.CheckModelKey("an output limit", p.ID+"/"+model); err != nil { return err } - if model != "*" && n > 0 && !p.serves(model) { + if model != "*" && n > 0 && s.ModelOutputs[p.ID+"/"+model] != n && !p.serves(model) { return errorf("%s has no model %s (magpie provider %s lists them)", p.ID, model, p.ID) } } - s := settings.Load() changed := false for k := range s.ModelOutputs { if model, ok := strings.CutPrefix(k, p.ID+"/"); ok && outs[model] <= 0 {