From 8f72f953045d561a80efbe3209858f0f460fa2e6 Mon Sep 17 00:00:00 2001 From: yetone Date: Mon, 9 Nov 2026 23:08:09 +0800 Subject: [PATCH] provider: a group's lookup copies the group found, not every group FindGroup, FindGroupAlso and GroupFinder built every routing group with groupsIn, which copies each group and its rules, to return one. The window's 15-second /api/drift poll asks GroupFinder for each Pi model on a group (piModelJSON), and the gateway asks once per request on a group. They now read the groups held (sharedGroupsIn, heldFrom while Hold is active) and copy only the group found and each group a member comes through (cloneGroup), so what a caller gets is still its own to change. Measured on mote with the real-account copy, GUI page open in Playwright Chromium, network blocked, 2 minutes (acgui.sh): - piModelJSON allocations: 10.6 MB -> 1.5 MB (v0.1.3420 vs this change) - process CPU 0.39 s vs 0.43 s, footprint 187 vs 197 MB: within the run noise of the once-a-minute catalog rebuilds; the drop is in the profile. Test: TestGroupFoundCopiesItselfAlone changes a found group, its member's Via and EffortMap, and checks FindGroup, GroupFinder and Groups still see the saved ones; then 50 looks must allocate less than 50 copies of every group (100 groups with rules). Broken by hand, each red: - A: sharedGroupsIn -> groupsIn: looks 5384832 B >= copies 4796000 B - B: no cloneGroup of the found group: index out of range (shared slice) - C: no clone of a member's sub-group: "a change to a found group reached the next look" Run (mote): go test ./internal/provider ./internal/agent ok; go test ./internal/gateway -timeout 45m ok; go vet native, GOOS=linux, GOOS=windows ok; gofmt -l clean. Not tried: a gateway request through a group with a real account (the path is the same FindGroup the test covers). --- internal/provider/group.go | 24 +++++- internal/provider/group_find_shared_test.go | 88 +++++++++++++++++++++ 2 files changed, 108 insertions(+), 4 deletions(-) create mode 100644 internal/provider/group_find_shared_test.go diff --git a/internal/provider/group.go b/internal/provider/group.go index 674cdbeca..e81b9a701 100644 --- a/internal/provider/group.go +++ b/internal/provider/group.go @@ -473,6 +473,18 @@ func sharedGroups() []Group { return heldFrom("groups", providerEntries(), buildGroupsIn) } +// sharedGroupsIn is groupsIn without the copy, for a caller that only +// reads them: a group found in it is copied alone (cloneGroup). A routing +// group's lookup (FindGroup, GroupFinder) copied every group and its +// rules for the one it wanted, each time: 4 MB of the window's 15-second +// drift poll, and once per request on a group. +func sharedGroupsIn(entries []Entry) []Group { + return heldFrom("groups", entries, buildGroupsIn) +} + +// cloneGroup is g with nothing shared with it (cloneGroups). +func cloneGroup(g Group) Group { return cloneGroups([]Group{g})[0] } + // cloneGroups is gs with nothing shared with it: no slice, map or pointer // of a group or its rules. func cloneGroups(gs []Group) []Group { @@ -1111,9 +1123,10 @@ func GroupFinder() func(id string) (Group, []Member, bool) { } if !read { entries, read = providerEntries(), true - all, find = groupsIn(entries), entriesBy(entries) + all, find = sharedGroupsIn(entries), heldFrom("entries by", entries, entriesBy) } if g, ok := groupOf(all, gid); ok && !g.Disabled { + g = cloneGroup(g) return g, membersFound(entries, find, all, g, disabledNow(), nil), true } return Group{}, nil, false @@ -1141,9 +1154,10 @@ func FindGroupAlso(id string, also func(p Provider, model string) bool) (Group, return Group{}, nil, false } entries := providerEntries() - all := groupsIn(entries) + all := sharedGroupsIn(entries) if g, ok := groupOf(all, gid); ok && !g.Disabled { - return g, membersFound(entries, entriesBy(entries), all, g, disabledNow(), also), true + g = cloneGroup(g) + return g, membersFound(entries, heldFrom("entries by", entries, entriesBy), all, g, disabledNow(), also), true } return Group{}, nil, false } @@ -1189,7 +1203,9 @@ func membersFound(entries []Entry, find entryOf, all []Group, g Group, off func( if !ok || sub.Disabled || slices.Contains(in, gid) || len(via) >= maxNest { continue // gone, switched off, a loop, or deeper than anyone nests } - sub = sub.Within(g) + // its own copy: all may be the groups held (sharedGroupsIn), + // and a member's Via and EffortMap are its caller's + sub = cloneGroup(sub).Within(g) walk(sub, here, append(slices.Clone(via), sub.Live()), append(slices.Clone(in), gid)) continue } diff --git a/internal/provider/group_find_shared_test.go b/internal/provider/group_find_shared_test.go new file mode 100644 index 000000000..c11bd9008 --- /dev/null +++ b/internal/provider/group_find_shared_test.go @@ -0,0 +1,88 @@ +package provider + +import ( + "fmt" + "runtime" + "testing" +) + +// totalAlloc is the bytes allocated by f, run n times. +func totalAlloc(n int, f func()) uint64 { + var a, b runtime.MemStats + runtime.GC() + runtime.ReadMemStats(&a) + for range n { + f() + } + runtime.ReadMemStats(&b) + return b.TotalAlloc - a.TotalAlloc +} + +// A routing group's lookup copied every group and its rules for the one it +// wanted: 4 MB of the window's 15-second drift poll (each Pi model on a +// group asked GroupFinder), and once per request on a group. While a +// request holds the catalog it reads the groups held and copies the one +// found, and the groups its members come through, alone: what a caller +// is given is still its own to change. +func TestGroupFoundCopiesItselfAlone(t *testing.T) { + groupHeldFixture(t) + if err := SaveGroup(Group{ID: "outer", Name: "Outer", Members: []string{"group/g4", "p5/m2"}, Routing: Ordered, + EffortMaps: map[string]map[string]string{"p5/m2": {"high": "low"}}}); err != nil { + t.Fatal(err) + } + // a user's many groups, each with rules: what every look copied + for i := range 100 { + if err := SaveGroup(Group{ID: fmt.Sprintf("x%d", i), Name: fmt.Sprintf("X%d", i), Routing: Ordered, + Members: []string{"p1/m3", "p2/m3", "p3/m3", "p4/m3"}, + Rules: []Rule{{Use: "p2/m3", Tokens: 10, Agents: []string{"codex"}}}}); err != nil { + t.Fatal(err) + } + } + Changed() + release := Hold() + defer release() + g, ms, ok := FindGroup("group/outer") + if !ok || len(ms) < 2 || len(ms[0].Via) == 0 { + t.Fatalf("%v %d %+v", ok, len(ms), ms) + } + // every part a caller may change is its own + g.Members[0] = "changed" + ms[0].Via[0].Members[0] = "changed" + for _, m := range ms { + for k := range m.EffortMap { + m.EffortMap[k] = "changed" + } + } + find := GroupFinder() + for _, look := range []func(string) (Group, []Member, bool){FindGroup, find} { + g2, ms2, _ := look("group/outer") + if g2.Members[0] != "group/g4" || ms2[0].Via[0].Members[0] != "p0/m1" { + t.Fatalf("a change to a found group reached the next look: %v %v", g2.Members, ms2[0].Via[0].Members) + } + for _, m := range ms2 { + if m.EffortMap != nil && m.EffortMap["high"] != "low" { + t.Fatalf("a change to a member's effort map reached the next look: %v", m.EffortMap) + } + } + } + for _, x := range Groups() { + if x.ID == "g4" && x.Members[0] != "p0/m1" || x.ID == "outer" && x.Members[0] != "group/g4" { + t.Fatalf("a change to a found group reached Groups: %s %v", x.ID, x.Members) + } + } + // and a look costs less than one copy of every group + all := sharedGroupsIn(providerEntries()) + if len(all) < 100 { + t.Fatalf("the fixture has %d groups; the test needs the found ones too", len(all)) + } + const n = 50 + copies := totalAlloc(n, func() { _ = cloneGroups(all) }) + looks := totalAlloc(n, func() { + if _, _, ok := GroupFinder()("group/g4"); !ok { + t.Fatal("no group g4") + } + }) + if looks >= copies { + t.Fatalf("%d looks allocated %d bytes, as much as %d copies of every group (%d)", n, looks, n, copies) + } +}