Files
magpie/internal/agent/alma_seen_test.go
T
yetone 6247086fe9 agent: Alma's providers are decoded once per answer kept
A heap profile of the live magpie (v0.1.2646, 37 minutes, the owner's
real accounts) had almaProvidersSeen decode Alma's whole /api/providers
answer 390MB worth: the Agents page's model field (almaGet, 221MB) and
Alma's check (alma.func1, 167MB) each decoded the answer almaSend
already kept for 5s, every poll.

almaProvidersSeen keeps the slice it decoded beside the bytes it decoded
it from, and gives it again while almaSend gives the same kept bytes. A
new answer (5s on, or any change sent to Alma, which forgets the kept
ones) is decoded anew; a failed read is an error, never the last good
answer. Its three callers only read what it gives.

Run on mote (macOS 27.2):
- TestAlmaProvidersSeenDecodeOnce (new): 400 known models; 10 allocs or
  fewer a look, one ask of Alma, the new models after a PUT, an error
  with Alma gone. With the bytes check turned off by hand: "a look at the
  answer kept allocates 6833 times", red. Restored: ok.
- The owner's real Alma answer (227KB with keys blanked, 11 providers,
  539 models; probe not committed): a look 1,182,317 B / 30,690 allocs
  before, 48 B / 1 after.
- go test ./internal/agent: ok (161.6s); -race -run TestAlma: ok.
- gofmt -l clean; go vet, GOOS=windows and GOOS=linux go vet: ok.
2026-11-02 07:06:24 +08:00

74 lines
2.2 KiB
Go

package agent
import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"time"
)
// A look at Alma's providers decodes an answer once: the Agents page's
// model field, its check and its options each decoded all of Alma's
// providers again from the answer kept 5s. A new answer (Alma asked again
// after a change) is decoded anew.
func TestAlmaProvidersSeenDecodeOnce(t *testing.T) {
var mu sync.Mutex
gets, model := 0, "gpt-5.4"
asked := func() int { mu.Lock(); defer mu.Unlock(); return gets }
srv := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
return
}
mu.Lock()
gets++
m := model
mu.Unlock()
var b strings.Builder
b.WriteString(`[{"id":"mg","name":"magpie","type":"openai","baseURL":"http://x/v1","enabled":true,"models":["` + m + `"],"availableModels":[`)
for i := range 400 {
if i > 0 {
b.WriteString(",")
}
fmt.Fprintf(&b, `{"id":"m-%d","name":"Model %d","capabilityOverrides":{"vision":true,"functionCalling":true}}`, i, i)
}
b.WriteString(`]}]`)
rw.Write([]byte(b.String()))
}))
t.Cleanup(srv.Close)
oldAPI, oldFor := almaAPI, almaReadFor
almaAPI, almaReadFor = srv.URL, time.Minute
t.Cleanup(func() { almaAPI, almaReadFor = oldAPI, oldFor })
ps, err := almaProvidersSeen()
if err != nil || len(ps) != 1 || ps[0].Name != "magpie" || len(ps[0].Known) != 400 || ps[0].Models[0] != "gpt-5.4" {
t.Fatalf("first look: %v %+v", err, ps)
}
if n := testing.AllocsPerRun(20, func() {
if ps, err := almaProvidersSeen(); err != nil || len(ps) != 1 {
t.Fatal(err, ps)
}
}); n > 10 {
t.Errorf("a look at the answer kept allocates %v times", n)
}
if g := asked(); g != 1 {
t.Fatalf("looks asked Alma %d times", g)
}
mu.Lock()
model = "gpt-5.5"
mu.Unlock()
if err := almaDo("PUT", "/api/providers/mg", map[string]any{}, nil); err != nil {
t.Fatal(err)
}
if ps, err := almaProvidersSeen(); err != nil || ps[0].Models[0] != "gpt-5.5" || asked() != 2 {
t.Fatalf("after a change: %v, %v, %d asks", err, ps, asked())
}
// one that can't be read is an error, not the last good one
almaAPI = "http://127.0.0.1:1"
if _, err := almaProvidersSeen(); err == nil {
t.Error("Alma gone: no error")
}
}