provider: a Max output kept for a model no longer listed doesn't block the editor's Save (#3305, LoKi)
LoKi (#3305, Windows, v0.1.1325, Zen Free plugin 0.1.14): settings.json
held "opencode-zen-free/space-bunny-free": 65536 from when Zen Free
listed it. OutputsOf gives the editor every saved limit, the editor
sends them back as `outputs` on every Save, and SetModelOutputs refused
any positive entry for a model the provider doesn't serve, so each Save
failed with "opencode-zen-free has no model space-bunny-free", with no
model picked too.
SetModelOutputs (models.go) and its sibling SetModelCompacts
(compact.go, the editor's Compact at, same check and same round trip)
now refuse an unserved model only when its value is new or changed: an
entry sent back as it is stored is kept, and the rest of the Save goes
through. The user's value isn't deleted, since they set it and the model
may come back on the next refresh. It still shows in Max output as
model=size, and taking it out there clears it, as before. A newly typed
model the provider doesn't serve, or a changed value for a retired one,
is refused as before. SetModelOutput (the CLI's single set) is unchanged.
Siblings checked: the editor's Context window goes through provider.Save,
which never checks it against the list, so a retired model's window
already saved; Names & levels (modelPrefs) sends only the entries
changed in that editor session (modelPrefsOfDraft), so a retired one
isn't resent.
Run at 45593cc1d (origin/main, unchanged at push) plus this change:
- TestRetiredModelLimitsDontBlockTheEditor (provider; outputs and
compacts) and TestProviderSaveKeepsRetiredModelOutput (gui, through
/api/provider/save: the model leaves the list, Select none, a stale
limit beside a new one, a newly typed "typo" refused, cleared): ok.
- Broken by hand (the `!= n` term dropped from both setters): both red,
"relay has no model luna" (each subtest) and 400 "relay has no model
space-bunny-free".
- gofmt -l clean; go vet darwin, GOOS=linux -tags nogui, GOOS=windows: ok.
- go test -tags nogui -timeout 45m ./... (darwin, one suite on the
machine): every package ok (agent 442s, gateway 794s, gui 178s,
provider 218s), no FAIL in the log.
Not tried: the reporter's Windows machine or the real Zen Free plugin
(the fix is in a provider-kind-independent settings comparison); no GUI
change, so no Playwright run.
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user