Files
magpie/internal/agent/spell_test.go
T
yetone 39b9703e3f agent: a remote magpie's model an agent isn't shown, or isn't on the remote's list yet, is refused, not written as the agent's own and taken out of magpie (#2376, moonshardx)
Reproduced with two magpies under sandbox HOMEs: B on 127.0.0.1:3592 with
a relay and routing groups, and A on 3593 with B added as Remote magpie
(renamed to "home"), plus a second remote with the id "remote". Codex
(codex-cli 0.162.1) and Pi (1.1.0) were wired through A, and both ran real
turns on remote ids (group/, provider/ and multi-slash ones).

The cause: Spell left a value the field's picker didn't offer as the
agent's own. A remote's model is missing from the picker when magpie
visible keeps it from the agent, or when the remote added it after A last
fetched the remote's list (a key provider's list is fetched on Refresh or
RefreshEvery). For Pi, `magpie pi remote/group/smart` then wrote
defaultProvider "remote", defaultModel "group/smart", said "disconnected
from magpie: … is Pi's own", and Pi went to its own endpoint ("Your account
is not active"). Codex got model = "remote/group/…" with no catalog entry.
The rename itself was fine: `magpie provider set remote-magpie id=home`
moved Codex's and Pi's values, and the old id still routes through Was.

Spell now refuses (notOffered) a value that names one of magpie's models
the agent isn't shown, a model of one of magpie's providers not on its
list, or a group/… magpie has no group for, and says how to fix it
(magpie models <agent>, magpie visible <agent> all, magpie provider
refresh <id>). Every agent's model field goes through Spell (CLI set,
GUI /api agent set, aside, profiles). A model shown to the agent that a
field offers fewer of (Claude Desktop's tiers) is still taken, and so is a
value whose provider part one of the agent's own models uses.

Run:
- TestSpellRefusesMagpieModelNotOffered (Pi and Codex, visibility on the
  relay: hidden, unlisted and unknown-group values refused; shown and own
  values spelled as before). It fails with notOffered disabled by hand:
  all 8 refusals were spelled as given.
- go test -tags nogui -p 1 ./internal/cli/ (whole package; it caught
  Claude Desktop's tiers, so the hidden check is on the agent's shown
  catalog, not the field's options), ./internal/profile/ (whole package),
  internal/agent -run 'Spell|Pick|Connect|Visible|Rename|Pi|Codex|Set|Apply|Own'
  plus every test in the files that call Spell or Pick, and internal/gui's
  agent_connect, agent_models*, agent_model_order, agent_disconnect,
  agent_nomodels, aside_native, aside_offline and codex_agents_v1 tests:
  all pass.
- gofmt -l; go vet -tags nogui ./internal/agent for darwin, linux and windows.
- The built binary in the sandbox: Pi and Codex narrowed to "home" refuse
  remote/group/smart with the message and keep their config; an unlisted
  remote/group/newpool is refused; shown every model again, both take it;
  Pi's own anthropic/… still disconnects as before.

Not tried: the GUI picker (it only offers shown models; the GUI's set
endpoint now gets the error text). No GUI file changed.

Full suite: SUITE_LINE
2026-10-15 23:08:04 +08:00

87 lines
3.2 KiB
Go

package agent
import (
"os"
"testing"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/settings"
)
func TestSpell(t *testing.T) {
opts := func(prefix string) func(map[string]string) []Option {
return func(map[string]string) []Option {
return []Option{
{Value: "gpt-6"},
{Value: prefix + "copilot/gpt-6-sol", Ref: "copilot/gpt-6-sol"},
}
}
}
get := func() string { return "" }
bare := &Agent{Fields: []Field{{Key: "model", Get: get, Options: opts("")}}}
prefixed := &Agent{Fields: []Field{{Key: "model", Get: get, Options: opts("magpie/")}}}
for _, c := range []struct {
a *Agent
in, out string
err bool
}{
{bare, "copilot/gpt-6-sol", "copilot/gpt-6-sol", false},
{bare, "magpie/copilot/gpt-6-sol", "copilot/gpt-6-sol", false},
{prefixed, "copilot/gpt-6-sol", "magpie/copilot/gpt-6-sol", false},
{prefixed, "magpie/copilot/gpt-6-sol", "magpie/copilot/gpt-6-sol", false},
{bare, "gpt-6", "gpt-6", false},
{bare, "some-unlisted-model", "some-unlisted-model", false},
{bare, "", "", false},
{bare, "magpie/copilot/nope", "", true},
{prefixed, "magpie/copilot/nope", "", true},
} {
got, err := c.a.Spell("model", c.in)
if got != c.out || (err != nil) != c.err {
t.Errorf("%q: got %q, %v; want %q, err %v", c.in, got, err, c.out, c.err)
}
}
}
// #2376: a model of a remote magpie that the agent's picker doesn't offer,
// kept from it by magpie visible or not on the remote's list yet, was
// taken as the agent's own: Pi read remote/group/smart as its provider
// "remote" and model "group/smart", and was taken out of magpie. It is
// refused, and one the picker offers, or the agent's own, is spelled as
// before.
func TestSpellRefusesMagpieModelNotOffered(t *testing.T) {
syncHome(t)
if err := provider.Save(provider.Provider{ID: "remote", Name: "Remote magpie", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"group/smart", "codex/gpt-5.5"}}); err != nil {
t.Fatal(err)
}
if err := settings.Save(settings.Settings{Visible: map[string][]string{"pi": {"relay"}, "codex": {"relay"}}}); err != nil {
t.Fatal(err)
}
t.Cleanup(func() { settings.Save(settings.Settings{}) })
home := os.Getenv("HOME")
for _, a := range []*Agent{pi(home), codex(home)} {
for _, v := range []string{"remote/group/smart", "remote/codex/gpt-5.5", "remote/group/new-pool", "group/nope"} {
if got, err := a.Spell(a.Fields[0].Key, v); err == nil {
t.Errorf("%s: %s spelled %q, want refused", a.ID, v, got)
}
}
want := "relay/glm-4.6"
if a.ID == "pi" {
want = "magpie/" + want
}
if got, err := a.Spell(a.Fields[0].Key, "relay/glm-4.6"); err != nil || got != want {
t.Errorf("%s: relay/glm-4.6 spelled %q, %v; want %q", a.ID, got, err, want)
}
}
// a provider of the agent's own that magpie hasn't is the agent's
if got, err := pi(home).Spell("model", "my-own/some-model"); err != nil || got != "my-own/some-model" {
t.Errorf("pi's own: %q, %v", got, err)
}
// shown it every model, the remote's are offered and spelled as magpie's
if err := settings.Save(settings.Settings{}); err != nil {
t.Fatal(err)
}
if got, err := pi(home).Spell("model", "remote/group/smart"); err != nil || got != "magpie/remote/group/smart" {
t.Errorf("pi, shown every model: %q, %v", got, err)
}
}