agent tests: Pi's group entry counts the group look-ups, not the process's bytes (#6178)

TestPiGroupEntryKeepsItsMembers (efdca11be) compared runtime.MemStats
TotalAlloc across five polls, which counts every goroutine the earlier
tests left running. Alone it passed; in a full ./internal/agent run, with
~1,000 tests before it, it was red on macOS CI and on mote. A bisect on
mote found no single test behind it: the red needs tests from both halves
of a 128-test set, so it is cumulative, not one leak.

provider.GroupsFound counts how often GroupFinder found a group's
members, and the test asks that five kept polls found none.

Run (mica): -run TestPiGroupEntryKeepsItsMembers green; with agents.go's
old provider.GroupFinder() loop put back by hand it is red ("found the
group's members 5 times"); ./internal/agent whole ok (200s);
./internal/provider's only reds were the three on main (fixed in the
next commit). gofmt clean.
This commit is contained in:
yetone
2026-11-12 00:17:23 +08:00
parent e7fc84e964
commit 7b9ae7cf9c
2 changed files with 29 additions and 27 deletions
+20 -27
View File
@@ -3,7 +3,6 @@ package agent
import (
"fmt"
"os"
"runtime"
"testing"
"github.com/yetone/magpie/internal/catalog"
@@ -14,8 +13,8 @@ import (
// writing the entry of a routing group found the group's members each
// time, to see whether one speaks Anthropic's Messages API (#5037's
// promptCache): ~3.4 MB a poll with real accounts. What it found is kept
// across requests while the files stay the same, so a poll's entry
// allocates less than half of one look-up of the group.
// across requests while the files stay the same, so a poll after the
// first finds no group's members.
func TestPiGroupEntryKeepsItsMembers(t *testing.T) {
syncHome(t)
models := ""
@@ -40,34 +39,28 @@ func TestPiGroupEntryKeepsItsMembers(t *testing.T) {
t.Fatal(err)
}
group := catalog.Model{ID: "group/auto", Name: "Auto"}
// one request: what it allocates, the least of a few tries (other
// goroutines allocate meanwhile too)
request := func(fn func()) uint64 {
least := ^uint64(0)
for range 5 {
release := provider.Hold()
var a, b runtime.MemStats
runtime.ReadMemStats(&a)
fn()
runtime.ReadMemStats(&b)
release()
least = min(least, b.TotalAlloc-a.TotalAlloc)
}
return least
// one request, as an /api/drift poll is
request := func(fn func()) {
release := provider.Hold()
defer release()
fn()
}
if e := piModelJSON(group, "", true); e["promptCache"] == nil {
t.Fatalf("group/auto: no promptCache: %v", e)
}
// what is held within a request (the entries) is warm in both
request(func() { provider.FindGroup("group/auto") })
request(func() { piModelJSON(group, "", true) })
find := request(func() { provider.FindGroup("group/auto") })
entry := request(func() { piModelJSON(group, "", true) })
t.Logf("a look-up of the group %d bytes, the group's entry %d", find, entry)
if entry*2 > find {
t.Fatalf("a poll's group entry allocated %d bytes, a look-up of its members %d: the members are found again", entry, find)
request(func() { piModelJSON(group, "", true) }) // the first request finds them
// counted, not measured in bytes: the process's allocations include
// every goroutine earlier tests left running (#6178)
before := provider.GroupsFound()
for range 5 {
request(func() { piModelJSON(group, "", true) })
}
if e := piModelJSON(group, "", true); e["promptCache"] == nil {
t.Fatalf("group/auto: no promptCache kept: %v", e)
if n := provider.GroupsFound() - before; n != 0 {
t.Fatalf("5 polls' group entries found the group's members %d times: they are found again on every poll", n)
}
request(func() {
if e := piModelJSON(group, "", true); e["promptCache"] == nil {
t.Fatalf("group/auto: no promptCache kept: %v", e)
}
})
}
+9
View File
@@ -1213,6 +1213,7 @@ func GroupFinder() func(id string) (Group, []Member, bool) {
all, find = sharedGroupsIn(entries), heldFrom("entries by", entries, entriesBy)
}
if g, ok := routedGroupOf(all, gid); ok && !g.Disabled {
groupsFound.Add(1)
g = cloneGroup(g)
return g, membersFound(entries, find, all, g, disabledNow(), nil), true
}
@@ -1220,6 +1221,14 @@ func GroupFinder() func(id string) (Group, []Member, bool) {
}
}
// groupsFound counts the groups GroupFinder found members for, for a test
// that counts them.
var groupsFound atomic.Int64
// GroupsFound is how many times a group's members were found, for another
// package's test that counts them.
func GroupsFound() int64 { return groupsFound.Load() }
// FindDrawingGroup is FindGroup for a drawing (the gateway's images API):
// a member that is one of its provider's image models, as draws says, is
// the group's even where the provider's picks leave it out (unpicked).