Files
magpie/internal/agent/ismagpie_cache_test.go
T
yetone f962cfa75b agent: whether a model value is magpie's is kept while the catalog stays the same
isMagpie asked provider.Resolve, which outside a request holding the
catalog built every provider's entries again. The window's drift poll
(/api/drift, every 15s while the window is up) asks it for every
detected agent's field values, and each poll built the catalog again:
in the procmem profile (a sandbox `magpie web` on main with 17
providers, the GUI cycling its pages for 10 minutes) Drift's isMagpie
was 235 MB of the 1.37 GB allocated, ~6 MB a poll, with nothing changed.

Now isMagpie's answers are kept by value while provider.StampNow stays
the same, for a minute at most and 4096 values at most, as the gateway's
stamped lists are: a provider, account or setting saved, a plugin's
change, a catalog sync, another process rewriting providers.json or
settings.json, or a changed environment moves the stamp and they are
asked again. A value with no "/" is no catalog reference and isn't asked
at all, as before.

Tested with TestIsMagpieKeptWhileCatalogStays: two answers already given
take 1924 allocations before (fails), under 200 after; a provider saved
since is magpie's at once. go test -tags nogui ./internal/agent passes
(849s); -race on IsMagpie|Drift|GoProgram|GoMagic|Goose passes; gofmt -l
clean; GOOS=linux and GOOS=windows go vet -tags nogui ./internal/agent
pass. The full suite is run before the push of this batch.
2026-10-21 04:16:26 +08:00

41 lines
1.2 KiB
Go

package agent
import (
"testing"
"github.com/yetone/magpie/internal/provider"
)
// isMagpie answers again from what it kept while nothing the catalog is
// built from changed: the window's drift poll asked it for every agent's
// values every 15s, outside any request holding the catalog, and each
// answer built every provider's entries again (~6 MB a poll). A provider
// saved since is seen at once.
func TestIsMagpieKeptWhileCatalogStays(t *testing.T) {
syncHome(t)
if err := provider.Save(provider.Provider{ID: "oa", Name: "OA", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"gpt-5.5"}}); err != nil {
t.Fatal(err)
}
if !isMagpie("oa/gpt-5.5") {
t.Fatal("oa/gpt-5.5 not magpie's")
}
if isMagpie("nb/x") {
t.Fatal("nb/x magpie's before it is added")
}
allocs := testing.AllocsPerRun(20, func() {
isMagpie("oa/gpt-5.5")
isMagpie("nb/x")
})
// StampNow's look at three files; a catalog built was thousands
if allocs > 200 {
t.Fatalf("%.0f allocations for two answers already given", allocs)
}
if err := provider.Save(provider.Provider{ID: "nb", Name: "NB", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"x"}}); err != nil {
t.Fatal(err)
}
if !isMagpie("nb/x") {
t.Fatal("a provider saved since not seen")
}
}