routing: a renamed group still answers to its old id until a new group takes it

xinvxueyuan on Discord asked that renaming a routing group keep the old
group/<id> working (agents and scripts still name it), and that the old
id stop working only when a new group takes that id.

A Group now keeps Former, the ids it had before RenameGroup, newest
last. FindGroup, FindGroupAlso and Resolve (the gateway's known-model
check) fall back to the group whose Former has the id (routedGroupOf).
A group saved or renamed onto an id drops it from every other group's
Former (dropFormer). A save that doesn't send Former keeps the old list.

Run (mote): go test ./internal/provider -run 'Rename|Group' ok;
go test ./internal/gateway -run TestRoutingGroups ok, with a new
check: rename mine->ours, POST group/mine answers 200 through ours.
Broken by hand: routedGroupOf without the Former loop turns
TestRenamedGroupKeepsItsOldID red; Resolve without the fallback turns
TestRoutingGroups red with 404 "knows no model group/mine".
Full provider/gateway/gui/agent run and Linux/Windows vet started on
mote after push; reds will be filed.
Not tried: the GUI showing the old id anywhere (it doesn't list it).

Reported-by: xinvxueyuan (Discord)
This commit is contained in:
yetone
2026-11-10 01:08:04 +08:00
parent 37c247d1fc
commit 00af08e7b8
4 changed files with 145 additions and 8 deletions
+15
View File
@@ -267,6 +267,21 @@ func TestRoutingGroups(t *testing.T) {
t.Fatalf("trace: %+v", r)
}
// renamed, the group still takes requests on its old id (xinvxueyuan
// on Discord); renamed back, it is its own id again
if err := provider.RenameGroup("mine", "ours"); err != nil {
t.Fatal(err)
}
if code, body := postAs(t, s, "", `{"model":"group/mine","messages":[{"role":"user","content":"hi"}]}`); code != 200 || !strings.Contains(body, "from ka") {
t.Fatalf("old id: %d %s", code, body)
}
if r := s.trace.routes[len(s.trace.routes)-1]; r.Group == nil || r.Group.ID != "ours" {
t.Fatalf("old id trace: %+v", r)
}
if err := provider.RenameGroup("ours", "mine"); err != nil {
t.Fatal(err)
}
// removing one magpie found hides it; showing it brings it back
if err := provider.DeleteGroup("auto-m"); err != nil {
t.Fatal(err)
+44 -4
View File
@@ -198,6 +198,10 @@ type Group struct {
ID string `json:"id"`
Name string `json:"name"`
Members []string `json:"members"` // "provider/model[:effort]" or "group/<id>", in order (see MemberEffort)
// Former are the ids it had before a rename (RenameGroup), newest
// last: a request still asking group/<old> is routed to it, until a
// group takes that id for its own (xinvxueyuan on Discord).
Former []string `json:"former,omitempty"`
// Match are patterns its models are found by as well (#766): a glob
// over the "provider/model" id with * in it ("openrouter/*:free"), or
// a regular expression after "re:". Kept apart from Members, so the
@@ -513,6 +517,7 @@ func cloneGroups(gs []Group) []Group {
g.Members, g.Match, g.Matched, g.Placed = slices.Clone(g.Members), slices.Clone(g.Match), slices.Clone(g.Matched), slices.Clone(g.Placed)
g.Off, g.Levels, g.Fast = slices.Clone(g.Off), slices.Clone(g.Levels), slices.Clone(g.Fast)
g.Accounts, g.Seen, g.Keys = slices.Clone(g.Accounts), slices.Clone(g.Seen), slices.Clone(g.Keys)
g.Former = slices.Clone(g.Former)
g.Weights = maps.Clone(g.Weights)
g.EffortMaps = renamedEffortMaps(g.EffortMaps, func(m string) string { return m })
if g.IntentSure != nil {
@@ -708,6 +713,7 @@ func (g Group) own() Group {
g.Accounts = slices.Clone(g.Accounts)
g.Keys = slices.Clone(g.Keys)
g.Seen = slices.Clone(g.Seen)
g.Former = slices.Clone(g.Former)
g.Weights = maps.Clone(g.Weights)
g.EffortMaps = renamedEffortMaps(g.EffortMaps, func(m string) string { return m })
g.Rules = slices.Clone(g.Rules)
@@ -1143,7 +1149,7 @@ func GroupFinder() func(id string) (Group, []Member, bool) {
entries, read = providerEntries(), true
all, find = sharedGroupsIn(entries), heldFrom("entries by", entries, entriesBy)
}
if g, ok := groupOf(all, gid); ok && !g.Disabled {
if g, ok := routedGroupOf(all, gid); ok && !g.Disabled {
g = cloneGroup(g)
return g, membersFound(entries, find, all, g, disabledNow(), nil), true
}
@@ -1173,7 +1179,7 @@ func FindGroupAlso(id string, also func(p Provider, model string) bool) (Group,
}
entries := providerEntries()
all := sharedGroupsIn(entries)
if g, ok := groupOf(all, gid); ok && !g.Disabled {
if g, ok := routedGroupOf(all, gid); ok && !g.Disabled {
g = cloneGroup(g)
return g, membersFound(entries, heldFrom("entries by", entries, entriesBy), all, g, disabledNow(), also), true
}
@@ -1190,6 +1196,33 @@ func groupOf(all []Group, id string) (Group, bool) {
return Group{}, false
}
// routedGroupOf is the group a request for group/<id> goes to: the group
// of that id, or else the one renamed from it (Former), so an agent or a
// script still on the old id keeps working after a rename. A group of
// that id, switched off too, holds the id; one magpie found that is
// removed (Hidden, as a rename leaves a found group's old id) does not.
func routedGroupOf(all []Group, id string) (Group, bool) {
if g, ok := groupOf(all, id); ok {
return g, true
}
for _, g := range all {
if !g.Hidden && slices.Contains(g.Former, id) {
return g, true
}
}
return Group{}, false
}
// dropFormer takes id out of every group's Former: a group took it for
// its own, and group/<id> is that group's now.
func dropFormer(gs []Group, id string) {
for i := range gs {
if slices.Contains(gs[i].Former, id) {
gs[i].Former = slices.DeleteFunc(slices.Clone(gs[i].Former), func(x string) bool { return x == id })
}
}
}
// membersIn are a group's models, ready now, in its order: a group in it
// gives its own there, as deep as they go. A model met again is left
// where it was first — the same model at another effort is another
@@ -1699,10 +1732,14 @@ func SaveGroup(g Group) error {
if err := cleanEffortMaps(entries, &g); err != nil {
return err
}
var was, keysWas []string
var was, keysWas, formerWas []string
if i := slices.IndexFunc(f.Groups, func(x Group) bool { return x.ID == g.ID }); i >= 0 {
was, keysWas = f.Groups[i].Accounts, f.Groups[i].Keys
was, keysWas, formerWas = f.Groups[i].Accounts, f.Groups[i].Keys, f.Groups[i].Former
}
if g.Former == nil {
g.Former = slices.Clone(formerWas) // a caller that doesn't know of them
}
g.Former = slices.DeleteFunc(cleanList(g.Former), func(id string) bool { return id == g.ID })
if g.Keys == nil {
g.Keys = slices.Clone(keysWas) // a caller that doesn't know of them
} else if g.Keys, err = cleanGroupKeys(g.Keys); err != nil {
@@ -1791,6 +1828,7 @@ func SaveGroup(g Group) error {
g.Seen = seenIn(g.ID, was, own, entries)
}
g.Disabled = false
dropFormer(f.Groups, g.ID)
for i := range f.Groups {
if f.Groups[i].ID == g.ID {
g.Disabled = f.Groups[i].Disabled && !f.Groups[i].Hidden
@@ -2177,7 +2215,9 @@ func RenameGroup(from, to string) error {
}
found := slices.ContainsFunc(foundGroups(providerEntries()), func(o Group) bool { return o.ID == from })
g.ID, g.Auto, g.Hidden = to, false, false
g.Former = append(slices.DeleteFunc(slices.Clone(g.Former), func(id string) bool { return id == from || id == to }), from)
f.Groups = slices.DeleteFunc(f.Groups, func(o Group) bool { return o.ID == from })
dropFormer(f.Groups, to)
if found {
f.Groups = append(f.Groups, Group{ID: from, Hidden: true})
}
+78 -4
View File
@@ -30,8 +30,9 @@ func TestRenameGroup(t *testing.T) {
if err := RenameGroup("auto-gpt-6-astra", "gpt-6-astra"); err != nil {
t.Fatal(err)
}
if _, _, ok := FindGroup("group/auto-gpt-6-astra"); ok {
t.Fatal("the found group came back beside the renamed one")
// the old id asks the renamed group, never the found one back beside it
if g, _, ok := FindGroup("group/auto-gpt-6-astra"); !ok || g.ID != "gpt-6-astra" {
t.Fatalf("the old id found %v %q", ok, g.ID)
}
g, ms, ok := FindGroup("group/gpt-6-astra")
if !ok || g.Auto || len(ms) != 2 {
@@ -48,8 +49,8 @@ func TestRenameGroup(t *testing.T) {
if err := RenameGroup("top", "best"); err != nil {
t.Fatal(err)
}
if _, _, ok := FindGroup("group/top"); ok || slices.Contains(RemovedGroups(), "top") {
t.Fatal("top is still there")
if g, _, ok := FindGroup("group/top"); !ok || g.ID != "best" || slices.Contains(RemovedGroups(), "top") {
t.Fatalf("top is still there, or doesn't lead to best: %v %q", ok, g.ID)
}
if _, _, ok := FindGroup("group/best"); !ok {
t.Fatal("no best")
@@ -58,3 +59,76 @@ func TestRenameGroup(t *testing.T) {
t.Fatal("renamed a group that isn't")
}
}
// A renamed group keeps answering to its old id (xinvxueyuan on Discord),
// through more renames, until a group takes that id for its own; renamed
// back, it holds the id again and lists it no more as former.
func TestRenamedGroupKeepsItsOldID(t *testing.T) {
t.Setenv("HOME", t.TempDir())
t.Setenv("USERPROFILE", t.TempDir())
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
if err := Save(Provider{ID: "a", Name: "a", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"x", "y"}}); err != nil {
t.Fatal(err)
}
if err := SaveGroup(Group{ID: "work", Name: "Work", Members: []string{"a/x"}}); err != nil {
t.Fatal(err)
}
if err := RenameGroup("work", "job"); err != nil {
t.Fatal(err)
}
if err := RenameGroup("job", "day"); err != nil {
t.Fatal(err)
}
for _, id := range []string{"work", "job", "day"} {
if g, ms, ok := FindGroup("group/" + id); !ok || g.ID != "day" || len(ms) != 1 {
t.Fatalf("group/%s: %v %q %d", id, ok, g.ID, len(ms))
}
}
// saved again by a caller that doesn't send Former, it keeps them
g, _, _ := FindGroup("group/day")
g.Former = nil
if err := SaveGroup(g); err != nil {
t.Fatal(err)
}
if g, _, ok := FindGroup("group/work"); !ok || g.ID != "day" {
t.Fatalf("a save dropped the old ids: %v %q", ok, g.ID)
}
// a new group takes "work": it is that group's from then on, also after
// the new one is gone
if err := SaveGroup(Group{ID: "work", Name: "Work", Members: []string{"a/y"}}); err != nil {
t.Fatal(err)
}
if g, ms, ok := FindGroup("group/work"); !ok || g.ID != "work" || ms[0].Model != "y" {
t.Fatalf("group/work: %v %q", ok, g.ID)
}
if g, _, _ := FindGroup("group/day"); slices.Contains(g.Former, "work") {
t.Fatalf("day still lists work: %v", g.Former)
}
if err := DeleteGroup("work"); err != nil {
t.Fatal(err)
}
if _, _, ok := FindGroup("group/work"); ok {
t.Fatal("work came back to day after it was given away")
}
// renamed back to an old id, it is its own id, not a former one
if err := RenameGroup("day", "job"); err != nil {
t.Fatal(err)
}
g, _, _ = FindGroup("group/job")
if g.ID != "job" || slices.Contains(g.Former, "job") || !slices.Contains(g.Former, "day") {
t.Fatalf("%q %v", g.ID, g.Former)
}
// a rename onto a former id of another group takes it from that group
if err := SaveGroup(Group{ID: "other", Name: "Other", Members: []string{"a/y"}}); err != nil {
t.Fatal(err)
}
if err := RenameGroup("other", "day"); err != nil {
t.Fatal(err)
}
if g, _, _ := FindGroup("group/day"); g.ID != "day" {
t.Fatalf("group/day: %q", g.ID)
}
if g, _, _ := FindGroup("group/job"); slices.Contains(g.Former, "day") {
t.Fatalf("job still lists day: %v", g.Former)
}
}
+8
View File
@@ -3489,6 +3489,14 @@ func Resolve(id string) (Provider, string, bool) {
if e, ok := entryIn(Catalog(), id); ok {
return e.Provider, e.Model, true
}
// a group renamed from it (Former) answers for it, as routing does
if gid, ok := GroupIDOf(id); ok {
if g, ok := routedGroupOf(sharedGroupsIn(entries), gid); ok && g.ID != gid {
if e, ok := entryIn(groupEntries(entries), "group/"+g.ID); ok {
return e.Provider, e.Model, true
}
}
}
return Provider{}, "", false
}
return resolveIn(providerEntries(), id)