Files
magpie/internal/gui/agent_models_kept_test.go
T
yetone 331057743d agents: a provider an agent's visibility keeps is in its model list, off, and ticked is shown it (whysugarsweet on X)
whysugarsweet on X (v0.1.4066): OpenCode Go is in Pi's model list but
not in Claude Desktop's. Desktop's list showed Google Gemini, DeepSeek,
Anthropic, Codex and Grok only.

Nothing leaves OpenCode Go out by protocol. What can narrow one agent's
catalog to some providers is settings.Visible: `magpie visible`, a
profile or settings sync writes it. A provider added later is kept out
of that agent's /v1/models, its config and the Agents page's list. The
page showed no sign of it and had no way to undo it, so the user could
only see Go missing. I haven't seen the reporter's settings, so this is
the likely cause, not a confirmed one.

- GET /api/agent-models/{id} now lists the models the visibility keeps,
  after the rest, as hidden and `kept`. They sit under their provider
  in the list and its rail, so they can be found and ticked.
- POST takes `widen`: kept models that were ticked. provider.Widen adds
  each one's provider (or group) to the agent's visibility. When nothing
  is kept after that, the agent is no longer narrowed at all. The
  provider's other models are then listed and off, so ticking one adds
  just that model.
- A kept model is never saved as hidden or picked one by one. The row's
  count leaves kept models out of "listed".
- Every agent's list shares this code, so the fix applies to every
  narrowed agent, not only Claude Desktop. The gateway's model list and
  Desktop's inferenceModels both come from CatalogFor, so a widened
  provider is served and routed too.

Run, at db9d6507b plus this commit:
- go test ./internal/gui ./internal/provider (mote), whole packages:
  ok. The new tests:
  - TestAgentModelsKeptByVisibility: Go's models are kept and off;
    ticking Kimi widens Desktop to opencode-go; CatalogFor then has it;
    widening the last kept one deletes Visible.
  - TestAgentModelsKeptPicked: a widen is a pick in only-picks mode.
- go test ./internal/gateway with -run on the model, catalog and Desktop
  tests: ok.
- go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet
  ./..., the three builds, gofmt -l: clean.
- New GUI file desktop-model-kept.test.cjs: chromium and webkit; en, zh,
  zh-TW, ja and de; window (1100px, with the rail) and tray panel
  (420px). 20/20 ok.
- GUI files around the list (agent-model*, agent-models*, desktop-*,
  agent-menu-label, agent-connect-*, cursor-local-models, codex-model-*,
  zcode-models, grip-inset, popover-mac-chrome, provider-*picked*,
  i18n-*) on mica: 597/608 at 65f745dd4 plus this change. Every red was
  red the same, alone, on origin/main ea9f2639b:
  - agent-menu-label, webkit panel en/ja/de: #6371
  - desktop-tiers: #6475
  - grip-inset: #6293
  - zcode-models, webkit de panel: filed as #6566
  At the rebased head I reran desktop-model-kept, desktop-model-menu,
  desktop-twin, agent-models, agent-models-provider-ticks,
  agent-models-only-picked and i18n-keys: 114/114 ok (mote).

Broken by hand:
- Kept models left out of agentModelList: the Go test is red ("no
  opencode-go/kimi-k2.6").
- The provider.Widen call removed: red ("visible: [deepseek-x]",
  "picks: [relay/a1]").
- The old app.js save(): the GUI test is red, with no widen posted.

Not tried:
- the reporter's own settings.json;
- the real Claude Desktop app reading a widened config;
- a real OpenCode Go account.
2026-11-14 02:22:28 +08:00

153 lines
5.5 KiB
Go

package gui
import (
"encoding/json"
"net/http"
"net/http/httptest"
"path/filepath"
"slices"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/settings"
)
// whysugarsweet on X: Claude Desktop narrowed to some providers (magpie
// visible, from the terminal) never listed OpenCode Go added after, while
// Pi listed it, and the Agents page's list had no way to it. The models
// the visibility keeps are in the list, off; ticking one shows the agent
// its provider, and with nothing kept after, the agent is narrowed no more.
func TestAgentModelsKeptByVisibility(t *testing.T) {
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.Provider{
{ID: "deepseek-x", Name: "DeepSeek X", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"d1"}},
{ID: "opencode-go", Name: "OpenCode Go", Preset: "opencode-go", Key: "k", Anthropic: provider.OpenCodeGoAnthropic, Chat: provider.OpenCodeGoChat, Models: []string{"kimi-k2.6", "glm-5.1"}},
{ID: "zen-x", Name: "Zen X", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"z1"}},
} {
if err := provider.Save(p); err != nil {
t.Fatal(err)
}
}
s := settings.Load()
s.Visible = map[string][]string{"claude-desktop": {"deepseek-x"}}
if err := settings.Save(s); err != nil {
t.Fatal(err)
}
mux := http.NewServeMux()
agentModelsAPI(mux)
call := func(method, body string) (out struct {
Models []agentModelJSON
Count *modelCountJSON
}) {
t.Helper()
w := httptest.NewRecorder()
mux.ServeHTTP(w, httptest.NewRequest(method, "/api/agent-models/claude-desktop", strings.NewReader(body)))
if w.Code != 200 {
t.Fatalf("%s %d %s", method, w.Code, w.Body)
}
json.Unmarshal(w.Body.Bytes(), &out)
return out
}
find := func(ms []agentModelJSON, id string) agentModelJSON {
t.Helper()
for _, m := range ms {
if m.ID == id {
return m
}
}
t.Fatalf("no %s in %+v", id, ms)
return agentModelJSON{}
}
got := call("GET", "")
if m := find(got.Models, "deepseek-x/d1"); m.Kept || m.Hidden {
t.Fatalf("listed one: %+v", m)
}
for _, id := range []string{"opencode-go/kimi-k2.6", "opencode-go/glm-5.1", "zen-x/z1"} {
if m := find(got.Models, id); !m.Kept || !m.Hidden || m.Group == "" {
t.Fatalf("kept one not in the list, off: %+v", m)
}
}
// a list sending the kept ones among what it hides takes none of them
// out one by one, and narrows the agent no differently
got = call("POST", `{"hidden":["opencode-go/kimi-k2.6","opencode-go/glm-5.1","zen-x/z1"]}`)
if h := settings.Load().HiddenModels["claude-desktop"]; len(h) != 0 {
t.Fatalf("kept ones saved as taken out: %v", h)
}
if got.Count == nil || got.Count.Shown != 1 || got.Count.Listed != 1 {
t.Fatalf("count: %+v", got.Count)
}
// Kimi ticked: OpenCode Go is shown, GLM listed and off
got = call("POST", `{"hidden":["opencode-go/glm-5.1","zen-x/z1"],"widen":["opencode-go/kimi-k2.6"]}`)
if v := settings.Load().Visible["claude-desktop"]; !slices.Equal(v, []string{"deepseek-x", "opencode-go"}) {
t.Fatalf("visible: %v", v)
}
if m := find(got.Models, "opencode-go/kimi-k2.6"); m.Kept || m.Hidden {
t.Fatalf("ticked: %+v", m)
}
if m := find(got.Models, "opencode-go/glm-5.1"); m.Kept || !m.Hidden {
t.Fatalf("its provider's other: %+v", m)
}
if m := find(got.Models, "zen-x/z1"); !m.Kept {
t.Fatalf("another provider widened too: %+v", m)
}
if got.Count == nil || got.Count.Shown != 2 || got.Count.Listed != 3 {
t.Fatalf("count: %+v", got.Count)
}
shown, _ := provider.CatalogFor("claude-desktop")
if !slices.ContainsFunc(shown, func(e provider.Entry) bool { return e.ID == "opencode-go/kimi-k2.6" }) {
t.Fatalf("Desktop's catalog lacks it: %v", shown)
}
// the last kept one ticked: nothing is kept, so it is narrowed no more
call("POST", `{"hidden":["opencode-go/glm-5.1"],"widen":["zen-x/z1"]}`)
if v, ok := settings.Load().Visible["claude-desktop"]; ok {
t.Fatalf("still narrowed: %v", v)
}
if h := settings.Load().HiddenModels["claude-desktop"]; !slices.Equal(h, []string{"opencode-go/glm-5.1"}) {
t.Fatalf("taken out: %v", h)
}
}
// Shown only its picks, a kept one ticked is a pick of the agent's.
func TestAgentModelsKeptPicked(t *testing.T) {
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.Provider{
{ID: "relay", Name: "Relay", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"a1"}},
{ID: "late", Name: "Late", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"b1", "b2"}},
} {
if err := provider.Save(p); err != nil {
t.Fatal(err)
}
}
s := settings.Load()
s.Visible = map[string][]string{"pi": {"relay"}}
s.PickedModels = map[string][]string{"pi": {"relay/a1"}}
if err := settings.Save(s); err != nil {
t.Fatal(err)
}
mux := http.NewServeMux()
agentModelsAPI(mux)
w := httptest.NewRecorder()
mux.ServeHTTP(w, httptest.NewRequest("POST", "/api/agent-models/pi", strings.NewReader(`{"shown":["relay/a1","late/b2"],"widen":["late/b2"]}`)))
if w.Code != 200 {
t.Fatalf("%d %s", w.Code, w.Body)
}
if p := settings.Load().PickedModels["pi"]; !slices.Equal(p, []string{"late/b2", "relay/a1"}) {
t.Fatalf("picks: %v", p)
}
if _, ok := settings.Load().Visible["pi"]; ok {
t.Fatal("still narrowed with nothing kept")
}
}