groups: a found group's id and name come from models.dev's one spelling (XYenon)

XYenon on Discord (#feedback, 10-30): autoGroups() and spelledIn() picked
a found group's name and its public id apart, so a group read
glm-5-3-flash beside auto/glm-5.3-flash. The id also followed the order
the providers were added in.

Now:
- spelledIn takes the spelling models.dev gives the model most
  (catalog.Spelling, the normal index normalFor already reads): every
  member spelling of GLM-5.3 Flash (glm-5-3-flash, Ark's
  glm-5-3-flash-260828, glm-5.3-flash) gives auto/glm-5.3-flash. A model
  models.dev doesn't know keeps the old pick (first listed member
  spelling, else the first member's).
- autoGroups names the group from the same choice: catalog.NameOf of
  that spelling, else a vendor's own name, else the spelling itself.
  A found group the user changed keeps the name they saved.
- Old ids keep working. group/auto-<slug> doesn't change, and any
  auto/<spelling> is still the group (CanonicalRef: gateway, key
  allow-lists, fallbacks, members). PublicRef now takes an old
  auto/<spelling> to the current one too, so hidden, picked and ordered
  models saved by it hold, and a pick of it written now (Agent.Apply)
  is written as listed. An agent field magpie set to an old spelling
  moves on the next SyncCatalog (renameFoundGroups), as group/auto-
  values already did, because OpenCode checks its model against the list
  magpie writes. A value the user set stays as it is and is still served.

Tests (on mote, via mote-run):
- TestFoundGroupIDAndNameAreOneSpelling (5 member orders/spellings, the
  user's name kept), TestFoundGroupUnknownToModelsDevNamedByItsID,
  TestFoundGroupMovedOffAnOldSpelling. The catalog fixtures use
  models.dev's real spellings and names (glm-5.3-flash "GLM-5.3-Flash",
  volcengine's glm-5-3-flash "GLM 5.3 Flash").
- Broken by hand, one file reverted at a time: group.go reverted, the
  test is red (named "GLM 5.3 Flash" beside auto/glm-5.3-flash);
  provider/autoref.go reverted, red (id auto/glm-5-3-flash with Ark first,
  PublicRef of the old spelling unchanged); agent/autoref.go reverted,
  TestFoundGroupMovedOffAnOldSpelling red (field stays
  magpie/auto/glm-5-3-flash).
- go test -timeout 45m: internal/catalog, provider, agent, tui, cli,
  usageapi ok. internal/gui: everything ok except TestMainWindowCorners/
  macOS_26_SDK, which is #3949 (AppKit on macOS 27, nothing to do with
  this change). internal/gateway, codexcat, sessions ok.
- gofmt -l clean; go vet darwin, linux -tags nogui, windows clean.
- No GUI file changed, so the GUI suite wasn't run. Names reach it
  through /api/groups.

Not tried: a real models.dev sync with real providers in a sandbox app.
Groups whose first provider spelled a model differently from models.dev
(e.g. claude-opus-4.8 from a relay vs Anthropic's claude-opus-4-8) get
the models.dev spelling once. Magpie-set agent fields move with it, and
values the user set keep resolving. A key or fallback chip saved under
the old spelling still shows it in the editor (it still works).
This commit is contained in:
yetone
2026-10-30 03:09:29 +08:00
parent 2d9a89b9f6
commit 9b833205f7
7 changed files with 273 additions and 19 deletions
File diff suppressed because one or more lines are too long
+17 -9
View File
@@ -13,10 +13,14 @@ import (
// checks its model against its list (OpenCode) would find it gone. On its
// next write of the agent's lists (SyncCatalog), magpie moves each field
// it set to the old id onto the new one; a field the user set themselves,
// or changed since magpie set it, stays as they left it.
// or changed since magpie set it, stays as they left it. So does a field
// magpie set to an auto/<model> spelt as the group is listed no longer
// (auto/glm-5-3-flash, now auto/glm-5.3-flash: the spelling follows
// models.dev's, Group.Spelled), which the gateway serves as ever.
// setToFoundGroups are the agents magpie set a field of to a found
// group's group/auto-<slug>, by id: those renameFoundGroups looks at, so
// group's group/auto-<slug> or auto/<model>, by id: those
// renameFoundGroups looks at, so
// a sync reads no other agent's files for it. None while a provider of
// the user's is called auto, and auto/<model> is its.
func setToFoundGroups() map[string]bool {
@@ -28,7 +32,7 @@ func setToFoundGroups() map[string]bool {
var out map[string]bool
for id, ap := range appliedLoad() {
for _, v := range ap.Fields {
if strings.Contains(v, provider.GroupPrefix+"auto-") {
if strings.Contains(v, provider.GroupPrefix+"auto-") || strings.Contains(v, provider.AutoPrefix) {
if out == nil {
out = map[string]bool{}
}
@@ -41,8 +45,8 @@ func setToFoundGroups() map[string]bool {
}
// renameFoundGroups moves a's fields that magpie set to a found group's
// group/auto-<slug> onto the group's auto/<model>, the agent's prefix and
// suffix kept. It answers whether it moved any.
// group/auto-<slug>, or to an auto/<model> it is listed by no longer,
// onto the group's auto/<model> now, the agent's prefix and suffix kept. It answers whether it moved any.
func (a *Agent) renameFoundGroups() bool {
if a.Native != nil || len(a.Fields) == 0 {
return false
@@ -73,16 +77,20 @@ func (a *Agent) renameFoundGroups() bool {
}
// publicValue is v, an agent's value for a field, with a found group's
// group/auto-<slug> in it as the group's auto/<model>: its prefix
// (magpie/), an effort or [1m] after the id, and the agent's own suffix
// kept. ok is false when v names no such group, or one magpie has no
// group for now.
// group/auto-<slug>, or an auto/<model> spelt otherwise than the group is
// listed now, in it as the group's auto/<model>: its prefix (magpie/), an
// effort or [1m] after the id, and the agent's own suffix kept. ok is
// false when v names no such group, one magpie has no group for now, or
// the group as it is listed already.
func (a *Agent) publicValue(v string) (string, bool) {
model, suffix, one := a.split(v)
if !one {
return v, false
}
i := strings.Index(model, provider.GroupPrefix+"auto-")
if i < 0 {
i = strings.Index(model, provider.AutoPrefix)
}
if i < 0 || i > 0 && model[i-1] != '/' {
return v, false
}
+72
View File
@@ -2,8 +2,10 @@ package agent
import (
"encoding/json"
"os"
"testing"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/provider"
)
@@ -80,3 +82,73 @@ func TestFoundGroupRenamedOnlyWhereMagpieSetIt(t *testing.T) {
t.Fatalf("the user's own %q became %q", old, got)
}
}
// A found group's auto/<model> follows the spelling models.dev gives the
// model most (XYenon on Discord), so one magpie listed before as
// auto/glm-5-3-flash is auto/glm-5.3-flash now. An agent magpie set to the
// old spelling is moved onto the new one on the next catalog sync, as one
// set to group/auto-<slug> is: OpenCode checks its model against the list
// magpie writes, which names the group by its new spelling only. One the
// user set to it themselves keeps their value, which is still the group.
func TestFoundGroupMovedOffAnOldSpelling(t *testing.T) {
syncHome(t)
os.WriteFile(catalog.CachePath(), []byte(`{
"zai": {"models": {"glm-5.3-flash": {"id":"glm-5.3-flash","name":"GLM-5.3-Flash"}}},
"fireworks": {"models": {"glm-5.3-flash": {"id":"glm-5.3-flash","name":"GLM-5.3-Flash"}}}}`), 0o644)
catalog.Reset()
for id, m := range map[string]string{"ark": "glm-5-3-flash", "spare": "glm-5.3-flash"} {
if err := provider.Save(provider.Provider{ID: id, Name: id, Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{m}}); err != nil {
t.Fatal(err)
}
}
if got := provider.PublicRef("group/auto-glm-5-3-flash"); got != "auto/glm-5.3-flash" {
t.Fatalf("the found group is listed as %q", got)
}
a, err := Find("opencode")
if err != nil {
t.Fatal(err)
}
if err := a.Apply("model", "spare/glm-5.3-flash"); err != nil {
t.Fatal(err)
}
// what magpie wrote and recorded while the group was listed by the
// first provider's spelling
old := "magpie/auto/glm-5-3-flash"
if err := edit.SetJSON(a.Path, edit.KV{Path: "model", Value: old}); err != nil {
t.Fatal(err)
}
record(a.ID, "model", old)
SyncCatalog()
if got := a.Values()["model"]; got != "magpie/auto/glm-5.3-flash" {
t.Fatalf("magpie's own value became %q, want magpie/auto/glm-5.3-flash\n%s", got, readFile(a.Path))
}
if got := appliedOf(a.ID).Fields["model"]; got != "magpie/auto/glm-5.3-flash" {
t.Errorf("applied.json holds %q", got)
}
// set as it is listed now: left alone
SyncCatalog()
if got := a.Values()["model"]; got != "magpie/auto/glm-5.3-flash" {
t.Errorf("the group as it is listed became %q", got)
}
// the user's own: no record of the field
appliedMu.Lock()
m := appliedLoad()
delete(m, a.ID)
appliedSave(m)
appliedMu.Unlock()
if err := edit.SetJSON(a.Path, edit.KV{Path: "model", Value: old}); err != nil {
t.Fatal(err)
}
SyncCatalog()
if got := a.Values()["model"]; got != old {
t.Fatalf("the user's own %q became %q", old, got)
}
// and a pick of the old spelling written now is the group as listed
if err := a.Apply("model", "magpie/auto/glm-5-3-flash"); err != nil {
t.Fatal(err)
}
if got := a.Values()["model"]; got != "magpie/auto/glm-5.3-flash" {
t.Errorf("a pick of the old spelling was written as %q", got)
}
}
+10
View File
@@ -1189,6 +1189,16 @@ func NameOf(id string) string {
return n
}
// Spelling is the spelling models.dev gives a model most, of every bare
// id it knows that is the same model (SameModel): glm-5.3-flash for
// glm-5-3-flash, GLM-5.3-Flash or Ark's glm-5-3-flash-260828, as most of
// its providers spell it (the normal index normalFor reads). Lowercase,
// without a vendor's prefix; "" when it knows the model by no spelling.
func Spelling(id string) string {
load()
return normal[SameModel(id)]
}
// Named gives a model the list names by its id alone the name models.dev
// knows it by (NameOf), so one model reads the same under every provider:
// a GLM Coding Plan's glm-5-turbo, which Zhipu's catalog doesn't list, is
+24 -9
View File
@@ -129,11 +129,15 @@ func (g Group) Ref() string {
}
// PublicRef is the id agents are given for ref: a found group's
// group/auto-<slug> as its auto/<model> (Group.Ref), its effort and [1m]
// mark kept; any other ref as it is, a found group magpie has none of now
// too.
// group/auto-<slug>, or an auto/<model> spelt otherwise than it is listed
// now (auto/glm-5-3-flash for auto/glm-5.3-flash), as its auto/<model>
// (Group.Ref), its effort and [1m] mark kept; any other ref as it is, a
// found group magpie has none of now too.
func PublicRef(ref string) string {
s := strings.TrimSpace(ref)
if strings.HasPrefix(s, AutoPrefix) {
s = CanonicalRef(s)
}
s, marked := strings.CutSuffix(s, "[1m]")
gid, ok := strings.CutPrefix(s, GroupPrefix)
if !ok || !strings.HasPrefix(gid, "auto-") || !autoRefsOn() {
@@ -155,12 +159,18 @@ func PublicRef(ref string) string {
}
// spelledIn is how the model of a found group's id is spelt by a vendor
// (Group.Spelled): of the members whose model merges into the group by its
// own id (mergeKey), the first models.dev lists by that very spelling, else
// the first; failing those, the model the user said a member is the same
// as (settings' ModelSameAs). Without a vendor's prefix ("anthropic/").
// "" for a group whose id is not auto-…, or none of whose members is its
// model: one the user made, whatever it is called, stays group/<id>.
// (Group.Spelled): the spelling models.dev gives the model most
// (catalog.Spelling: glm-5.3-flash whether the members serve glm-5-3-flash
// or glm-5.3-flash, XYenon on Discord), so the id doesn't follow the
// order the providers were added in, and the group's name is the one
// models.dev gives that spelling (autoGroups). Failing that, of the
// members whose model merges into the group by its own id (mergeKey), the
// first models.dev lists by that very spelling, else the first; failing
// those, the model the user said a member is the same as (settings'
// ModelSameAs). Without a vendor's prefix ("anthropic/"). "" for a group
// whose id is not auto-…, or none of whose members is its model: one the
// user made, whatever it is called, stays group/<id>. Every spelling it
// had before is still the group (CanonicalRef).
func spelledIn(g Group, same map[string]string) string {
if !strings.HasPrefix(g.ID, "auto-") {
return ""
@@ -188,6 +198,11 @@ func spelledIn(g Group, same map[string]string) string {
// an id a relay lists as its display name ("GPT-6 Sol") is no id to
// put in an agent's config: the one vendors agree on is, below
spaced := func(s string) bool { return strings.ContainsFunc(s, unicode.IsSpace) }
for _, m := range slices.Concat(own, alike) {
if s := catalog.Spelling(m); s != "" && !spaced(s) && "auto-"+Slug(sameModel(s)) == g.ID {
return s
}
}
spacedOnly := len(own)+len(alike) > 0
own, alike = slices.DeleteFunc(own, spaced), slices.DeleteFunc(alike, spaced)
for _, s := range own {
+138
View File
@@ -0,0 +1,138 @@
package provider
import (
"os"
"path/filepath"
"testing"
"github.com/yetone/magpie/internal/catalog"
)
// glmCatalog lists GLM-5.3 Flash as models.dev does: glm-5.3-flash under
// most providers, named GLM-5.3-Flash, and Volcengine Ark's dashed
// glm-5-3-flash under one, named otherwise.
const glmCatalog = `{
"zai": {"models": {"glm-5.3-flash": {"id":"glm-5.3-flash","name":"GLM-5.3-Flash"}}},
"fireworks": {"models": {"glm-5.3-flash": {"id":"glm-5.3-flash","name":"GLM-5.3-Flash"}}},
"openrouter": {"models": {"z-ai/glm-5.3-flash": {"id":"z-ai/glm-5.3-flash","name":"GLM-5.3-Flash"}}},
"volcengine": {"models": {"glm-5-3-flash": {"id":"glm-5-3-flash","name":"GLM 5.3 Flash"}}}}`
// glmDottedOnly lists it under the dotted spelling alone.
const glmDottedOnly = `{
"zai": {"models": {"glm-5.3-flash": {"id":"glm-5.3-flash","name":"GLM-5.3-Flash"}}},
"fireworks": {"models": {"glm-5.3-flash": {"id":"glm-5.3-flash","name":"GLM-5.3-Flash"}}}}`
// spellingHome is a sandbox HOME with the models.dev catalog cat, and
// models the providers' own, one provider each, in the order they are
// added.
func spellingHome(t *testing.T, cat string, models ...string) {
t.Helper()
isolate(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"))
if err := os.MkdirAll(filepath.Dir(catalog.CachePath()), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(catalog.CachePath(), []byte(cat), 0o644); err != nil {
t.Fatal(err)
}
catalog.Reset()
t.Cleanup(catalog.Reset)
for i, m := range models {
id := string(rune('a' + i))
if err := Save(Provider{ID: id, Name: id, Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{m}}); err != nil {
t.Fatal(err)
}
}
}
func foundGroup(t *testing.T, id string) Group {
t.Helper()
for _, g := range Groups() {
if g.ID == id {
return g
}
}
t.Fatalf("no group %s in %+v", id, Groups())
return Group{}
}
// A found group's id and its name come from one choice (XYenon on
// Discord): the spelling models.dev gives the model most, and the name it
// gives that spelling, whichever provider was added first and however it
// spells the model. They were picked apart: the name was the first
// provider's id (glm-5-3-flash) beside auto/glm-5.3-flash, and the id
// itself followed the providers' order. Every spelling it was listed by
// before is still the group, and a name the user gave it stays theirs.
func TestFoundGroupIDAndNameAreOneSpelling(t *testing.T) {
for _, tc := range []struct {
name string
cat string
models []string
}{
// the reporter's: the first provider's spelling, which models.dev
// doesn't list, was the name, and the one it lists the id
{"a spelling models.dev lacks first", glmDottedOnly, []string{"glm-5-3-flash", "glm-5.3-flash"}},
{"Ark's dashed spelling first", glmCatalog, []string{"glm-5-3-flash", "glm-5.3-flash"}},
{"Ark's dated id first", glmCatalog, []string{"glm-5-3-flash-260828", "glm-5.3-flash"}},
{"the dotted spelling first", glmCatalog, []string{"glm-5.3-flash", "glm-5-3-flash"}},
{"no provider serves models.dev's spelling", glmCatalog, []string{"glm-5-3-flash", "GLM-5.3-Flash-260828"}},
} {
t.Run(tc.name, func(t *testing.T) {
models := tc.models
spellingHome(t, tc.cat, models...)
g := foundGroup(t, "auto-glm-5-3-flash")
if g.Ref() != "auto/glm-5.3-flash" || g.Name != "GLM-5.3-Flash" {
t.Fatalf("the group is %q named %q, want auto/glm-5.3-flash named GLM-5.3-Flash", g.Ref(), g.Name)
}
// what agents may hold from before: each still the group
for _, old := range []string{"auto/glm-5-3-flash", "auto/glm-5.3-flash", "group/auto-glm-5-3-flash"} {
if gid, ok := GroupIDOf(old); !ok || gid != "auto-glm-5-3-flash" {
t.Errorf("GroupIDOf(%q) = %q %v", old, gid, ok)
}
if _, _, ok := Resolve(old); !ok {
t.Errorf("%s isn't served", old)
}
}
for in, want := range map[string]string{
"auto/glm-5-3-flash": "auto/glm-5.3-flash",
"auto/glm-5-3-flash:high": "auto/glm-5.3-flash:high",
"auto/glm-5-3-flash[1m]": "auto/glm-5.3-flash[1m]",
"group/auto-glm-5-3-flash": "auto/glm-5.3-flash",
"auto/glm-5.3-flash": "auto/glm-5.3-flash",
"auto/no-such-model": "auto/no-such-model",
"a/" + models[0]: "a/" + models[0],
} {
if got := PublicRef(in); got != want {
t.Errorf("PublicRef(%q) = %q, want %q", in, got, want)
}
}
})
}
t.Run("the user's name", func(t *testing.T) {
spellingHome(t, glmCatalog, "glm-5-3-flash", "glm-5.3-flash")
g := foundGroup(t, "auto-glm-5-3-flash")
g.Name = "Flash"
if err := SaveGroup(g); err != nil {
t.Fatal(err)
}
g = foundGroup(t, "auto-glm-5-3-flash")
if g.Name != "Flash" || g.Ref() != "auto/glm-5.3-flash" {
t.Errorf("the user's group is %q named %q", g.Ref(), g.Name)
}
})
}
// A model models.dev doesn't know keeps the spelling the members give it,
// and its name is that id, not another provider's spelling of it.
func TestFoundGroupUnknownToModelsDevNamedByItsID(t *testing.T) {
spellingHome(t, glmCatalog, "acme-9-1", "acme-9.1")
g := foundGroup(t, "auto-acme-9-1")
if g.Ref() != "auto/acme-9-1" || g.Name != "acme-9-1" {
t.Errorf("the group is %q named %q", g.Ref(), g.Name)
}
}
+11
View File
@@ -713,6 +713,17 @@ func autoGroups(entries []Entry, same map[string]string) []Group {
g.Name = own(e) // a vendor that names it, over one that only lists its id
}
}
// the name goes with the id agents are given (auto/<Spelled>): the
// one models.dev gives that spelling, else, where no vendor names
// it, that spelling itself, not the first provider's (XYenon on
// Discord: glm-5-3-flash beside auto/glm-5.3-flash). A group the
// user changed is stored with their name, never this one.
g.Spelled = spelledIn(g, same)
if n := catalog.NameOf(g.Spelled); g.Spelled != "" && n != "" {
g.Name = n
} else if g.Spelled != "" && g.Name == es[0].Model {
g.Name = g.Spelled
}
out = append(out, g)
}
return out