diff --git a/internal/provider/autoref.go b/internal/provider/autoref.go index 6a30ecbd3..2a4a41a9b 100644 --- a/internal/provider/autoref.go +++ b/internal/provider/autoref.go @@ -147,7 +147,7 @@ func PublicRef(ref string) string { if i := strings.LastIndex(gid, ":"); i > 0 { gid, level = gid[:i], gid[i:] } - g, ok := groupOf(Groups(), gid) + g, ok := groupOf(sharedGroups(), gid) if !ok || g.Spelled == "" { return ref } diff --git a/internal/provider/group.go b/internal/provider/group.go index 2866097c5..861cf1f41 100644 --- a/internal/provider/group.go +++ b/internal/provider/group.go @@ -399,6 +399,14 @@ func groupsIn(entries []Entry) []Group { return cloneGroups(heldFrom("groups", entries, buildGroupsIn)) } +// sharedGroups is Groups for a caller that only reads them: no copy, so +// nothing it is given may be changed. PublicRef, asked for every model a +// page lists, copied every group and its rules each time (115 MB of a live +// magpie's 2.6 GB in a minute and a half). +func sharedGroups() []Group { + return heldFrom("groups", providerEntries(), buildGroupsIn) +} + // cloneGroups is gs with nothing shared with it: no slice, map or pointer // of a group or its rules. func cloneGroups(gs []Group) []Group { @@ -1760,8 +1768,8 @@ func DisabledGroup(id string) (Group, bool) { } gid = strings.TrimPrefix(gid, GroupPrefix) } - if g, ok := groupOf(Groups(), gid); ok && g.Disabled { - return g, true + if g, ok := groupOf(sharedGroups(), gid); ok && g.Disabled { + return cloneGroups([]Group{g})[0], true } return Group{}, false } diff --git a/internal/provider/publicref_shared_test.go b/internal/provider/publicref_shared_test.go new file mode 100644 index 000000000..1c79e13d1 --- /dev/null +++ b/internal/provider/publicref_shared_test.go @@ -0,0 +1,48 @@ +package provider + +import ( + "fmt" + "testing" + + "github.com/yetone/magpie/internal/filememo" +) + +// PublicRef reads the groups magpie holds without copying them: it is asked +// for every model a page lists, and each copied every group and its rules +// (115 MB of a live magpie's 2.6 GB in a minute and a half). A group saved +// since is seen, and DisabledGroup's caller still gets a group of its own. +func TestPublicRefCopiesNoGroups(t *testing.T) { + autoRefHome(t) + if err := SaveGroup(Group{Name: "Mine", Members: []string{"a/x", "b/x"}}); err != nil { + t.Fatal(err) + } + // the groups a user with many keeps: each with members and a rule + for i := range 40 { + if err := SaveGroup(Group{Name: fmt.Sprintf("G%d", i), Members: []string{"a/x", "b/x", "c/x"}, + Rules: []Rule{{Use: "a/x", Agents: []string{"claude"}, Folders: []string{"/w"}}}}); err != nil { + t.Fatal(err) + } + } + defer Hold()() + defer filememo.Hold()() + if got := PublicRef("group/auto-claude-opus-5-5:high[1m]"); got != "auto/claude-opus-5.5:high[1m]" { + t.Fatalf("the found group's ref: %q", got) + } + copied := testing.AllocsPerRun(5, func() { Groups() }) + asked := testing.AllocsPerRun(20, func() { PublicRef("group/auto-claude-opus-5-5") }) + if asked*10 > copied { + t.Fatalf("PublicRef took %.0f allocations, a copy of the groups %.0f", asked, copied) + } + + if err := SwitchGroup("mine", false); err != nil { + t.Fatal(err) + } + d, ok := DisabledGroup("group/mine") + if !ok { + t.Fatal("a group switched off since is not found off") + } + d.Members[0] = "changed" + if g, _ := groupOf(sharedGroups(), "mine"); g.Members[0] == "changed" { + t.Fatal("DisabledGroup's caller wrote into the groups held") + } +} diff --git a/internal/provider/visible.go b/internal/provider/visible.go index dc0de8563..99b2b08ea 100644 --- a/internal/provider/visible.go +++ b/internal/provider/visible.go @@ -315,7 +315,7 @@ func Families() []string { for _, p := range All() { add(p.Family) } - for _, g := range Groups() { + for _, g := range sharedGroups() { add(g.Family) } slices.Sort(out)