groups: a found group the user changed takes in a new provider with its model
Kxsg79bEYFP4 on X: magpie found auto/deepseek-v4.1-flash over two
providers, the user changed the group, then added a third provider (a
plugin's) with the model, and it never joined.
Cause: SaveGroup stores a changed found group under its auto-… id with
the members it had then, and buildGroupsIn skips the found group of
that id ("the user changed it: theirs now"). Nothing recorded which of
the found models the user had taken out, so a stored group could only
stay frozen. Plugin providers, id spelling and a one-time creation were
ruled out: a plugin's models are in providerEntries like any provider's,
and the found group itself has all three.
Fix: SaveGroup stores Group.Seen for an auto-… id: the models magpie
found in it by that save plus those the user names. Reading a changed
found group (joined) appends the found members it neither names nor has
seen, after the user's own. A model the user took out was seen and stays
out (magpie's default never overrides the user's value); a provider kept
out of found groups (AutoGroupSkip) doesn't join; with found groups off
nothing joins. Seen follows a provider's rename/SetModelID
(mapModelRefs) and a fold, and ShareGroup leaves it behind. A group
changed before this (no Seen) takes in nothing, since nothing says what
the user took out; its next save sees what is found then, and providers
added after that join. docs/subsystems/providers-accounts.md says so.
Tests: TestChangedFoundGroupTakesInANewProvider (join, order, routed by
FindGroup, the user's name/routing kept, taken out stays out before and
after joining, rename, skip, share), TestChangedFoundGroupFromBefore-
KeepsItsMembers (old stored group), TestChangedFoundGroupTakesInA-
PluginProvider (the reporter's surface: the fake plugin under real Bun).
Run on mote at base f280b12e5 plus this change:
- gofmt -l: clean
- GOOS=windows go vet ./...: ok; GOOS=linux go vet -tags nogui ./...: ok
- go test -tags nogui -count=1 -timeout 45m ./internal/provider/
./internal/cli/ ./internal/gui/ ./internal/tui/ ./internal/agent/
./internal/gateway/: all ok (provider 214s, cli 86s, gui 127s, tui 23s,
agent 372s, gateway 876s)
Broken by hand (each red, then restored):
- joined returning g unchanged (the old behaviour): all three new tests red
- dropping the Seen check and the legacy no-Seen guard: c came back at
once, and the pre-Seen group took c in
- not mapping Seen in mapModelRefs: c came back as cee/… after Rename
- not clearing Seen in ShareGroup: the shared text carried it
Not tried: a real magpie with the reporter's providers; the GUI suite
(app.js is unchanged, /api/groups only gains a "seen" field); an editor
opened before a provider was added and saved after it treats that
provider as taken out.
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -343,6 +343,7 @@ func foldWhere(candidates func([]Provider) [][]int) (int, error) {
|
||||
}
|
||||
g.Fast = compact(mapEach(g.Fast, ref))
|
||||
g.Placed = compact(mapEach(g.Placed, ref))
|
||||
g.Seen = compact(mapEach(g.Seen, ref))
|
||||
g.Off = compact(mapEach(g.Off, ref))
|
||||
g.Weights = renamedWeights(g.Weights, ref)
|
||||
g.Classifier = ref(g.Classifier)
|
||||
|
||||
@@ -279,6 +279,14 @@ type Group struct {
|
||||
Spelled string `json:"-"`
|
||||
// Hidden is stored for a found group the user removed.
|
||||
Hidden bool `json:"hidden,omitempty"`
|
||||
// Seen is stored for a found group the user changed (an auto-… id):
|
||||
// the models, as "provider/model", magpie had found in it by the
|
||||
// user's last save, and those the user named. A provider that serves
|
||||
// the model since joins the group as it is read (joined), as it joins
|
||||
// one magpie finds; one seen and not a member is one the user took
|
||||
// out, and stays out. Empty, the group was changed before magpie
|
||||
// kept this, and gets no member it doesn't name (Kxsg79bEYFP4 on X).
|
||||
Seen []string `json:"seen,omitempty"`
|
||||
// Disabled is a group of the user's switched off (PAMI on Discord):
|
||||
// kept on the Routing page as it is, but not offered to agents, a
|
||||
// request to it turned away with a word of why, and skipped where
|
||||
@@ -425,7 +433,7 @@ func cloneGroups(gs []Group) []Group {
|
||||
for i, g := range gs {
|
||||
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 = slices.Clone(g.Accounts)
|
||||
g.Accounts, g.Seen = slices.Clone(g.Accounts), slices.Clone(g.Seen)
|
||||
g.Weights = maps.Clone(g.Weights)
|
||||
if g.Rules != nil {
|
||||
rs := make([]Rule, len(g.Rules))
|
||||
@@ -455,6 +463,10 @@ func buildGroupsIn(entries []Entry) []Group {
|
||||
var out []Group
|
||||
hidden := map[string]bool{}
|
||||
var have map[string]bool // the providers magpie has, switched off or not
|
||||
var found []Group // the groups magpie finds, for a changed one to follow
|
||||
if !f.NoAutoGroups {
|
||||
found = foundGroups(entries)
|
||||
}
|
||||
for _, g := range f.Groups {
|
||||
if g.Hidden {
|
||||
hidden[g.ID] = true
|
||||
@@ -467,7 +479,7 @@ func buildGroupsIn(entries []Entry) []Group {
|
||||
have[p.ID] = true
|
||||
}
|
||||
}
|
||||
g = withoutRemoved(g, have)
|
||||
g = joined(withoutRemoved(g, have), found, entries)
|
||||
}
|
||||
out = append(out, withMatches(entries, g.own()))
|
||||
}
|
||||
@@ -475,7 +487,7 @@ func buildGroupsIn(entries []Entry) []Group {
|
||||
out = slices.DeleteFunc(out, func(g Group) bool { return !g.Auto })
|
||||
}
|
||||
if !f.NoAutoGroups {
|
||||
for _, g := range foundGroups(entries) {
|
||||
for _, g := range found {
|
||||
if slices.ContainsFunc(out, func(o Group) bool { return o.ID == g.ID }) {
|
||||
continue // the user changed it: theirs now
|
||||
}
|
||||
@@ -530,6 +542,66 @@ func withoutRemoved(g Group, have map[string]bool) Group {
|
||||
return g
|
||||
}
|
||||
|
||||
// joined is a found group the user changed (an "auto-…" id kept in
|
||||
// providers.json) with the providers that serve its model since the
|
||||
// user's last save: found's members it neither names nor has seen
|
||||
// (Group.Seen), after its own. A third provider with the model, a
|
||||
// plugin's among them, joins it as it joins one magpie finds
|
||||
// (Kxsg79bEYFP4 on X: changed, the group had stayed at the two it was
|
||||
// found with). One the user took out was seen, and stays out; so does
|
||||
// every provider of a group changed before Seen was kept, as nothing
|
||||
// says which of its models the user took out. It is how the group is
|
||||
// read, not stored: a save names the joined, and sees them.
|
||||
func joined(g Group, found []Group, entries []Entry) Group {
|
||||
if len(g.Seen) == 0 {
|
||||
return g
|
||||
}
|
||||
i := slices.IndexFunc(found, func(o Group) bool { return o.ID == g.ID })
|
||||
if i < 0 {
|
||||
return g
|
||||
}
|
||||
names := make(map[string]bool, len(g.Members))
|
||||
for _, m := range g.Members {
|
||||
base, _ := memberEffortIn(entries, m)
|
||||
names[base] = true
|
||||
}
|
||||
var add []string
|
||||
for _, m := range found[i].Members {
|
||||
if !names[m] && !slices.Contains(g.Seen, m) {
|
||||
add = append(add, m)
|
||||
}
|
||||
}
|
||||
if len(add) == 0 {
|
||||
return g
|
||||
}
|
||||
g.Members = slices.Concat(g.Members, add)
|
||||
return g
|
||||
}
|
||||
|
||||
// seenIn is the Seen a found group the user changed is saved with: what
|
||||
// it had seen (was), the models magpie finds in it now, and those the
|
||||
// user names (own), each "provider/model" once.
|
||||
func seenIn(id string, was, own []string, entries []Entry) []string {
|
||||
seen := slices.Clone(was)
|
||||
see := func(m string) {
|
||||
if m != "" && !strings.HasPrefix(m, GroupPrefix) && !IsPattern(m) && !slices.Contains(seen, m) {
|
||||
seen = append(seen, m)
|
||||
}
|
||||
}
|
||||
for _, g := range foundGroups(entries) {
|
||||
if g.ID == id {
|
||||
for _, m := range g.Members {
|
||||
see(m)
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, m := range own {
|
||||
base, _ := memberEffortIn(entries, m)
|
||||
see(base)
|
||||
}
|
||||
return seen
|
||||
}
|
||||
|
||||
// own is the group with lists of its own. f.Groups is the providers.json
|
||||
// every look-up shares while a request holds the catalog (heldOf), so a
|
||||
// caller changing its group in place — the TUI deleting a rule, SaveGroup
|
||||
@@ -544,6 +616,7 @@ func (g Group) own() Group {
|
||||
g.Levels = slices.Clone(g.Levels)
|
||||
g.Fast = slices.Clone(g.Fast)
|
||||
g.Accounts = slices.Clone(g.Accounts)
|
||||
g.Seen = slices.Clone(g.Seen)
|
||||
g.Weights = maps.Clone(g.Weights)
|
||||
g.Rules = slices.Clone(g.Rules)
|
||||
for i, r := range g.Rules {
|
||||
@@ -1547,6 +1620,17 @@ func SaveGroup(g Group) error {
|
||||
}
|
||||
g.Auto, g.Hidden = false, false
|
||||
g.Members = own // what the patterns match is found again as it is read
|
||||
// a found group changed keeps following its model: what magpie had
|
||||
// found in it is seen, so a model taken out stays out and one a new
|
||||
// provider serves joins it (joined)
|
||||
g.Seen = nil
|
||||
if strings.HasPrefix(g.ID, "auto-") {
|
||||
var was []string
|
||||
if i := slices.IndexFunc(f.Groups, func(x Group) bool { return x.ID == g.ID }); i >= 0 {
|
||||
was = f.Groups[i].Seen
|
||||
}
|
||||
g.Seen = seenIn(g.ID, was, own, entries)
|
||||
}
|
||||
g.Disabled = false
|
||||
for i := range f.Groups {
|
||||
if f.Groups[i].ID == g.ID {
|
||||
|
||||
@@ -0,0 +1,186 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/yetone/magpie/internal/plugin"
|
||||
"github.com/yetone/magpie/internal/testenv"
|
||||
)
|
||||
|
||||
// groupMembers are the members of the group id as Groups reads it, nil
|
||||
// when there is none (or it is hidden).
|
||||
func groupMembers(t *testing.T, id string) []string {
|
||||
t.Helper()
|
||||
for _, g := range Groups() {
|
||||
if g.ID == id && !g.Hidden {
|
||||
return g.Members
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func saveKeyProvider(t *testing.T, id string, models ...string) {
|
||||
t.Helper()
|
||||
if err := Save(Provider{ID: id, Name: id, Key: "k", Chat: "http://127.0.0.1:1/v1", Models: models}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// A found group the user changed follows its model as one magpie finds
|
||||
// does (Kxsg79bEYFP4 on X: auto/deepseek-v4.1-flash, changed, stayed at
|
||||
// the two providers it was found with when a third with the model was
|
||||
// added): a provider that serves the model since joins it, after the
|
||||
// members the user named. A model the user took out stays out, the one
|
||||
// taken out before the new provider came and the new one taken out
|
||||
// after it joined alike; a provider kept out of found groups doesn't
|
||||
// join; and a provider's rename carries what was seen, so the renamed
|
||||
// provider's model the user took out doesn't come back by its new id.
|
||||
func TestChangedFoundGroupTakesInANewProvider(t *testing.T) {
|
||||
claudeHome(t)
|
||||
saveKeyProvider(t, "a", "deepseek-v4.1-flash")
|
||||
saveKeyProvider(t, "b", "deepseek-v4-1-flash")
|
||||
saveKeyProvider(t, "c", "deepseek-v4.1-flash")
|
||||
if got := groupMembers(t, "auto-deepseek-v4-1-flash"); !slices.Equal(got, []string{"a/deepseek-v4.1-flash", "b/deepseek-v4-1-flash", "c/deepseek-v4.1-flash"}) {
|
||||
t.Fatalf("found: %v", got)
|
||||
}
|
||||
// the user changes it: routing by turns, without c
|
||||
if err := SaveGroup(Group{ID: "auto-deepseek-v4-1-flash", Name: "DS Flash", Members: []string{"b/deepseek-v4-1-flash", "a/deepseek-v4.1-flash:high"}, Routing: Rotate}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := []string{"b/deepseek-v4-1-flash", "a/deepseek-v4.1-flash:high"}
|
||||
if got := groupMembers(t, "auto-deepseek-v4-1-flash"); !slices.Equal(got, want) {
|
||||
t.Fatalf("changed: %v", got)
|
||||
}
|
||||
// a fourth provider with the model joins it, last; c stays out
|
||||
saveKeyProvider(t, "d", "DeepSeek-V4.1-Flash")
|
||||
want = append(want, "d/DeepSeek-V4.1-Flash")
|
||||
if got := groupMembers(t, "auto-deepseek-v4-1-flash"); !slices.Equal(got, want) {
|
||||
t.Fatalf("d added: %v, want %v", got, want)
|
||||
}
|
||||
// the group the gateway routes has it too
|
||||
if _, ms, ok := FindGroup("group/auto-deepseek-v4-1-flash"); !ok || !slices.ContainsFunc(ms, func(m Member) bool { return m.Provider.ID == "d" }) {
|
||||
t.Fatalf("routed without d: %v %v", ms, ok)
|
||||
}
|
||||
// saved as read, it is named; the user's name and routing stay
|
||||
gs := Groups()
|
||||
g := gs[slices.IndexFunc(gs, func(g Group) bool { return g.ID == "auto-deepseek-v4-1-flash" })]
|
||||
if g.Name != "DS Flash" || g.Routing != Rotate {
|
||||
t.Fatalf("the user's settings: %q %q", g.Name, g.Routing)
|
||||
}
|
||||
// the user takes d out: it stays out, as c does
|
||||
g.Members = slices.DeleteFunc(slices.Clone(g.Members), func(m string) bool { return strings.HasPrefix(m, "d/") })
|
||||
if err := SaveGroup(g); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want = []string{"b/deepseek-v4-1-flash", "a/deepseek-v4.1-flash:high"}
|
||||
if got := groupMembers(t, "auto-deepseek-v4-1-flash"); !slices.Equal(got, want) {
|
||||
t.Fatalf("d taken out: %v", got)
|
||||
}
|
||||
// renamed, c is still one the user took out
|
||||
if err := Rename("c", "cee"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := groupMembers(t, "auto-deepseek-v4-1-flash"); !slices.Equal(got, want) {
|
||||
t.Fatalf("c renamed came back: %v", got)
|
||||
}
|
||||
// one kept out of found groups doesn't join; let back in, it does
|
||||
saveKeyProvider(t, "e", "deepseek-v4.1-flash")
|
||||
if err := SetAutoGroupSkip([]string{"e"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := groupMembers(t, "auto-deepseek-v4-1-flash"); !slices.Equal(got, want) {
|
||||
t.Fatalf("e kept out joined: %v", got)
|
||||
}
|
||||
if err := SetAutoGroupSkip(nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := groupMembers(t, "auto-deepseek-v4-1-flash"); !slices.Equal(got, append(slices.Clone(want), "e/deepseek-v4.1-flash")) {
|
||||
t.Fatalf("e let back in: %v", got)
|
||||
}
|
||||
// what this magpie saw is its own: a shared group doesn't carry it
|
||||
text, err := ShareGroup("auto-deepseek-v4-1-flash")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var shared SharedGroup
|
||||
if err := json.Unmarshal([]byte(text), &shared); err != nil || len(shared.Group.Seen) != 0 {
|
||||
t.Fatalf("shared with what was seen: %v %v", shared.Group.Seen, err)
|
||||
}
|
||||
}
|
||||
|
||||
// A found group changed before magpie kept what it had seen has no
|
||||
// record of which of its models the user took out: none joins it, as
|
||||
// before, so one taken out by hand never comes back. Its next save
|
||||
// sees what is found then, and from that on a new provider joins.
|
||||
func TestChangedFoundGroupFromBeforeKeepsItsMembers(t *testing.T) {
|
||||
claudeHome(t)
|
||||
saveKeyProvider(t, "a", "glm-5.3")
|
||||
saveKeyProvider(t, "b", "glm-5.3")
|
||||
saveKeyProvider(t, "c", "glm-5.3")
|
||||
f, err := read()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// as an older magpie stored it: c taken out, nothing seen
|
||||
f.Groups = append(f.Groups, Group{ID: "auto-glm-5-3", Name: "GLM", Members: []string{"a/glm-5.3", "b/glm-5.3"}, Routing: Ordered})
|
||||
if err := store(f); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := []string{"a/glm-5.3", "b/glm-5.3"}
|
||||
if got := groupMembers(t, "auto-glm-5-3"); !slices.Equal(got, want) {
|
||||
t.Fatalf("an older change: %v", got)
|
||||
}
|
||||
saveKeyProvider(t, "d", "glm-5.3")
|
||||
if got := groupMembers(t, "auto-glm-5-3"); !slices.Equal(got, want) {
|
||||
t.Fatalf("an older change took a model in: %v", got)
|
||||
}
|
||||
gs := Groups()
|
||||
if err := SaveGroup(gs[slices.IndexFunc(gs, func(g Group) bool { return g.ID == "auto-glm-5-3" })]); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := groupMembers(t, "auto-glm-5-3"); !slices.Equal(got, want) {
|
||||
t.Fatalf("saved again: %v", got)
|
||||
}
|
||||
saveKeyProvider(t, "e", "glm-5.3")
|
||||
if got := groupMembers(t, "auto-glm-5-3"); !slices.Equal(got, append(want, "e/glm-5.3")) {
|
||||
t.Fatalf("a provider after the save: %v", got)
|
||||
}
|
||||
}
|
||||
|
||||
// The reporter's case on its own surface: a provider a plugin serves,
|
||||
// added after the user changed a found group of two key providers,
|
||||
// joins it.
|
||||
func TestChangedFoundGroupTakesInAPluginProvider(t *testing.T) {
|
||||
bun := testenv.Bun(t)
|
||||
claudeHome(t)
|
||||
t.Setenv("MAGPIE_BUN", bun)
|
||||
t.Setenv("FAKE_DEEPSEEK", "1")
|
||||
t.Cleanup(plugin.Settle)
|
||||
saveKeyProvider(t, "a", "deepseek-v4-pro")
|
||||
saveKeyProvider(t, "b", "deepseek-v4-pro")
|
||||
if err := SaveGroup(Group{ID: "auto-deepseek-v4-pro", Name: "DS Pro", Members: []string{"b/deepseek-v4-pro", "a/deepseek-v4-pro"}, Routing: Ordered}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
||||
defer cancel()
|
||||
abs, _ := filepath.Abs("../plugin/testdata/fake/index.js")
|
||||
if _, err := plugin.Add(ctx, abs); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := plugin.Providers(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := plugin.APIKey(ctx, "fakeco", 0, nil, "k1", plugin.NewAccount); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got := groupMembers(t, "auto-deepseek-v4-pro")
|
||||
if len(got) != 3 || !slices.Equal(got[:2], []string{"b/deepseek-v4-pro", "a/deepseek-v4-pro"}) || !strings.HasSuffix(got[2], "/deepseek-v4-pro") || strings.HasPrefix(got[2], "a/") || strings.HasPrefix(got[2], "b/") {
|
||||
t.Fatalf("the plugin's provider didn't join: %v", got)
|
||||
}
|
||||
}
|
||||
@@ -53,6 +53,7 @@ func ShareGroup(id string) (string, error) {
|
||||
g.Matched = nil
|
||||
g.Auto, g.Hidden, g.Disabled = false, false, false
|
||||
g.Accounts = nil // this magpie's accounts: another has its own
|
||||
g.Seen = nil // what this magpie found: another finds its own
|
||||
g.Rules = slices.DeleteFunc(g.Rules, Rule.ByFolder)
|
||||
out, err := json.Marshal(SharedGroup{Magpie: ShareMark, Version: ShareVersion, Group: g})
|
||||
return string(out), err
|
||||
|
||||
@@ -119,7 +119,7 @@ func Rename(from, to string) error {
|
||||
}
|
||||
|
||||
// mapModelRefs gives every model ref the file keeps for routing — the
|
||||
// providers' fallbacks, and the routing groups' members, rules, fast and
|
||||
// providers' fallbacks, and the routing groups' members, rules, fast, seen and
|
||||
// placed members, those turned off, weights, classifiers and picks — as
|
||||
// to has it: what a provider's rename (Rename) and a model given another
|
||||
// id at its provider (SetModelID) both rewrite.
|
||||
@@ -143,6 +143,9 @@ func mapModelRefs(f *file, to func(string) string) {
|
||||
for k, m := range g.Placed {
|
||||
g.Placed[k] = to(m)
|
||||
}
|
||||
for k, m := range g.Seen {
|
||||
g.Seen[k] = to(m)
|
||||
}
|
||||
for k, m := range g.Off {
|
||||
g.Off[k] = to(m)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user