agent: Pi's group entry keeps whether a member speaks Messages across polls

Pi's Drift is asked on every /api/drift poll, about every 12s on
every page. Each poll wrote the entry of the model the agent is set
to. For a routing group, piModelJSON found all of the group's members
again (GroupFinder, membersFound, resolveIn) only to see whether one
speaks Anthropic's Messages API, for #5037's short promptCache. With
the real-account copy on mote, an alloc_space diff of 55s with the
Sessions page open showed 21.7MB allocated, 17.1MB of it under Drift
(5 polls, ~3.4MB each), 16.1MB of that in this look-up.

provider.GroupHasNative answers it through keptOf: kept across
requests while the files' stamp stays the same, a minute at most, as
the catalog's other kept builds are. Outside a request it is found
afresh, as before. The entry is the same.

Test: TestPiGroupEntryKeepsItsMembers. Over successive requests, a
group's entry allocates under half of one provider.FindGroup: 3,600
bytes against 71,040 for a 40-member group. With the old agents.go it
is red: 72,672 against 71,040. TestPiGroupPromptCache and
TestPiGroupThinkingIsWhatPiRuns pass.
This commit is contained in:
yetone
2026-11-11 22:26:07 +08:00
parent 7cd8bfc56f
commit efdca11be3
3 changed files with 95 additions and 7 deletions
+2 -7
View File
@@ -926,13 +926,8 @@ func piModelJSON(m catalog.Model, gw string, native bool) map[string]any {
// (addedMark). That short lifetime the group declares; long stays
// out, the gateway marks an hour only where a session's turns are
// often far apart, so it isn't always true.
if _, ms, ok := provider.GroupFinder()(m.ID); ok {
for _, g := range ms {
if g.Provider.Native(g.Model) == provider.Anthropic {
e["promptCache"] = map[string]any{"short": 300}
break
}
}
if provider.GroupHasNative(m.ID, provider.Anthropic) {
e["promptCache"] = map[string]any{"short": 300}
}
}
return e
+73
View File
@@ -0,0 +1,73 @@
package agent
import (
"fmt"
"os"
"runtime"
"testing"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/provider"
)
// Pi's Drift is asked on every /api/drift poll, each its own request, and
// 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.
func TestPiGroupEntryKeepsItsMembers(t *testing.T) {
syncHome(t)
models := ""
var ids, members []string
for i := range 40 {
id := fmt.Sprintf("claude-opus-5-%d", i)
if i > 0 {
models += ","
}
models += fmt.Sprintf(`%q: {"id":%q,"name":"Claude %d","limit":{"context":200000}}`, id, id, i)
ids = append(ids, id)
members = append(members, "ap/"+id)
}
if err := os.WriteFile(catalog.CachePath(), []byte(`{"ap": {"models": {`+models+`}}}`), 0o644); err != nil {
t.Fatal(err)
}
catalog.Reset()
if err := provider.Save(provider.Provider{ID: "ap", Name: "Anthropic Plugin", Key: "k", Anthropic: "http://127.0.0.1:2", Models: ids}); err != nil {
t.Fatal(err)
}
if err := provider.SaveGroup(provider.Group{ID: "auto", Name: "Auto", Members: members}); err != nil {
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
}
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)
}
if e := piModelJSON(group, "", true); e["promptCache"] == nil {
t.Fatalf("group/auto: no promptCache kept: %v", e)
}
}
+20
View File
@@ -1050,6 +1050,26 @@ func FindGroup(id string) (Group, []Member, bool) {
return GroupFinder()(id)
}
// GroupHasNative says whether the group id has a member found now that
// speaks api natively, kept across requests while the files stay the same
// (keptOf, a minute at most). Pi's Drift asks it of the model an agent is
// set to on every /api/drift poll, and finding the members was ~3.4 MB a
// poll with real accounts, four of five bytes the window allocated a minute.
func GroupHasNative(id string, api Protocol) bool {
return keptOf("group native "+string(api)+" "+id, func() bool {
_, ms, ok := GroupFinder()(id)
if !ok {
return false
}
for _, m := range ms {
if m.Provider.Native(m.Model) == api {
return true
}
}
return false
})
}
// FallbackGroup is the routing group a provider's fallback entry names
// ("group/<id>", 01huadalang on Discord), as the gateway routes it (Live),
// with its members ready now. ok is false for an entry naming a model,