xsw reported v0.1.1222 sitting at ~15% CPU while idle, with no gateway
requests. Since 8c5b9f928 (#1535, v0.1.1176), provider.All asks
settings.AccountDiscoveryOff for each account provider, and that reads
settings.json through Load, which caches the bytes but runs json.Unmarshal
on every call. agent.magpieModels prices each model under one Hold, and
each price asks All. So one pricing pass parsed settings.json models ×
accounts times. That pass runs on the GUI's drift poll (every 15s) and on
KeepDshWired (every 30s), per agent.
Evidence: `sample` of the owner's live dev build (26% average over 11.5h,
6–10% while sampled) had its top stacks in settings.loadChecked →
encoding/json under provider.All ← EntryPriceIn ← magpieModels, reached
from the drift handler and keepDshWired. A sandbox serve with an empty
config sat at 0.0%. A throwaway bench (not committed) priced 300 models
with 2 accounts: 81ms and 639 parses before, 11.5ms and 40 parses after.
All now asks once per call, from the settings the held look has already
read (heldSettings), through the new settings.AccountDiscoveryOffIn.
settings.Parses counts parses for the test, and loadChecked's comment no
longer claims the file is "parsed once".
Test: TestAllReadsNoSettingsPerLookWhileHeld prices 200 looks under one
Hold with 2 signed-in accounts and fails at >= 200 parses. Putting back
the per-account call by hand: "settings.json parsed 840 times for 200
looks at 2 accounts". With the fix: ~40, all from the first provider
build. It also checks MAGPIE_NO_ACCOUNTS=1 still hides account providers.
Run: gofmt -l; go vet -tags nogui on darwin, GOOS=linux and GOOS=windows
for internal/settings, internal/provider, internal/agent; sandbox-HOME
go test -tags nogui of those three packages, before and after rebasing
on origin/main. Other short tickers were checked (plugin host stop,
sessions readSlot, gui glide, update/claude sign-in polls): all bounded,
none run while idle.
Not run: a reproduction on xsw's machine or OS (unknown yet), a Linux or
Windows runtime CPU check, the GUI suite (no assets touched). Accounts()
still parses settings ~40 times per provider build; that is once per held
second, left for later.
323 lines
12 KiB
Go
323 lines
12 KiB
Go
package provider
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/plugin"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
"github.com/yetone/magpie/internal/testenv"
|
|
)
|
|
|
|
// A request that only reads builds the catalog once for all its look-ups
|
|
// (/api/state took 4s, resolving each agent's models anew); outside one,
|
|
// and after a write, it is built again.
|
|
func TestHoldBuildsOnce(t *testing.T) {
|
|
n := 0
|
|
build := func() int { n++; return n }
|
|
heldOf("t", build)
|
|
heldOf("t", build)
|
|
if n != 2 {
|
|
t.Fatalf("not held: built %d times, want 2", n)
|
|
}
|
|
release := Hold()
|
|
heldOf("t", build)
|
|
heldOf("t", build)
|
|
if n != 3 {
|
|
t.Fatalf("held: built %d times, want 3", n)
|
|
}
|
|
Changed()
|
|
if heldOf("t", build); n != 4 {
|
|
t.Fatalf("after a write: built %d times, want 4", n)
|
|
}
|
|
release()
|
|
release() // a second release is no second one
|
|
if heldOf("t", build); n != 5 {
|
|
t.Fatalf("released: built %d times, want 5", n)
|
|
}
|
|
if held.holds != 0 {
|
|
t.Fatalf("holds left: %d", held.holds)
|
|
}
|
|
}
|
|
|
|
// The settings are read once while a request holds the catalog (a look at
|
|
// the agents read settings.json for every model of every agent), and a
|
|
// setting saved meanwhile is seen at once.
|
|
func TestHoldReadsSettingsOnce(t *testing.T) {
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir()) // a run starts from no files, not the last run's
|
|
defer Hold()()
|
|
if _, ok := VisibleTo("held-agent"); ok {
|
|
t.Fatal("a visibility before any was set")
|
|
}
|
|
// written by something else: the held read stands
|
|
path := settings.Path()
|
|
os.MkdirAll(filepath.Dir(path), 0o755)
|
|
os.WriteFile(path, []byte(`{"visible":{"held-agent":["a"]}}`), 0o600)
|
|
if _, ok := VisibleTo("held-agent"); ok {
|
|
t.Fatal("settings.json read again while held")
|
|
}
|
|
s := settings.Load()
|
|
s.Visible = map[string][]string{"held-agent": {"b"}}
|
|
if err := settings.Save(s); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if names, ok := VisibleTo("held-agent"); !ok || len(names) != 1 || names[0] != "b" {
|
|
t.Fatalf("a setting saved while held: %v %v", names, ok)
|
|
}
|
|
}
|
|
|
|
// A provider saved while a request holds the catalog is in it at once.
|
|
func TestHoldSeesWrites(t *testing.T) {
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir()) // a run starts from no files, not the last run's
|
|
if err := Save(Provider{ID: "held", Name: "Held", Chat: "https://held.example/v1", Key: "k", Models: []string{"a"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer Hold()()
|
|
ids := func() []string {
|
|
var out []string
|
|
for _, e := range Catalog() {
|
|
out = append(out, e.ID)
|
|
}
|
|
return out
|
|
}
|
|
if !slices.Contains(ids(), "held/a") || slices.Contains(ids(), "held/b") {
|
|
t.Fatalf("before: %v", ids())
|
|
}
|
|
if err := Save(Provider{ID: "held", Name: "Held", Chat: "https://held.example/v1", Key: "k", Models: []string{"a", "b"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !slices.Contains(ids(), "held/b") {
|
|
t.Fatalf("a model picked meanwhile is missing: %v", ids())
|
|
}
|
|
if p, m, ok := Resolve("held/b"); !ok || p.ID != "held" || m != "b" {
|
|
t.Fatalf("Resolve: %v %q %v", p.ID, m, ok)
|
|
}
|
|
}
|
|
|
|
// The providers are built once while a request holds them (#746, sperwe:
|
|
// Codex's /models built them seven times, each reading every agent's
|
|
// sign-in, and ran past the 5 s Codex waits); a provider saved or an
|
|
// account written meanwhile has them built again, and a caller changing
|
|
// its list changes no one else's.
|
|
func TestHoldBuildsProvidersOnce(t *testing.T) {
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir()) // a run starts from no files, not the last run's
|
|
// nor from the plugins' list an earlier test's folder had: seen last,
|
|
// it reads as changed here, and the change, told in the background,
|
|
// has the providers built again while held
|
|
plugin.UseCached(nil)
|
|
if err := Save(Provider{ID: "once", Name: "Once", Chat: "https://once.example/v1", Key: "k", Models: []string{"a"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer Hold()()
|
|
n := AllBuilt.Load()
|
|
ps := All()
|
|
All()
|
|
if got := AllBuilt.Load() - n; got != 1 {
|
|
t.Fatalf("held: built %d times, want 1", got)
|
|
}
|
|
i := slices.IndexFunc(ps, func(p Provider) bool { return p.ID == "once" })
|
|
if i < 0 {
|
|
t.Fatalf("saved provider missing: %v", ps)
|
|
}
|
|
ps[i].Name = "changed"
|
|
if p, _ := findIn(All(), "once"); p.Name != "Once" {
|
|
t.Fatalf("a caller's change leaked: %q", p.Name)
|
|
}
|
|
if err := Save(Provider{ID: "twice", Name: "Twice", Chat: "https://twice.example/v1", Key: "k", Models: []string{"a"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := findIn(All(), "twice"); err != nil {
|
|
t.Fatal("a provider saved while held is missing")
|
|
}
|
|
n = AllBuilt.Load()
|
|
if err := writeLogins(readLogins()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if All(); AllBuilt.Load() == n {
|
|
t.Fatal("accounts written while held: not built again")
|
|
}
|
|
}
|
|
|
|
// cursor-agent's token is read from the Keychain once a while, not on
|
|
// each look at the accounts (#746: some 200 `security` runs a request); a
|
|
// sign-in or out in magpie, and CursorToken renewing it, read it afresh.
|
|
func TestCursorKeychainReadOnce(t *testing.T) {
|
|
if runtime.GOOS == "windows" {
|
|
t.Skip("a shell script stands in for security")
|
|
}
|
|
dir := t.TempDir()
|
|
log := filepath.Join(dir, "runs")
|
|
script := "#!/bin/sh\necho run >> " + log + "\necho tok-$(wc -l < " + log + " | tr -d ' ')\n"
|
|
testenv.Program(t, filepath.Join(dir, "security"), script)
|
|
t.Setenv("PATH", dir+string(os.PathListSeparator)+os.Getenv("PATH"))
|
|
forgetCursorStatus()
|
|
t.Cleanup(forgetCursorStatus)
|
|
runs := func() int {
|
|
b, _ := os.ReadFile(log)
|
|
return strings.Count(string(b), "run")
|
|
}
|
|
for range 3 {
|
|
if tok := cursorKeychainToken(false); tok != "tok-1" {
|
|
t.Fatalf("token %q", tok)
|
|
}
|
|
}
|
|
if runs() != 1 {
|
|
t.Fatalf("security ran %d times, want 1", runs())
|
|
}
|
|
forgetCursorStatus() // signed in or out in magpie
|
|
if tok := cursorKeychainToken(false); tok != "tok-2" || runs() != 2 {
|
|
t.Fatalf("after a sign-in: %q, %d runs", tok, runs())
|
|
}
|
|
if tok := cursorKeychainToken(true); tok != "tok-3" {
|
|
t.Fatalf("fresh: %q", tok)
|
|
}
|
|
if tok := cursorKeychainToken(false); tok != "tok-3" || runs() != 3 {
|
|
t.Fatalf("after a fresh read: %q, %d runs", tok, runs())
|
|
}
|
|
}
|
|
|
|
// migrations.json is read once while a request holds the catalog (the
|
|
// GUI's state looked at it thousands of times, one per provider's API),
|
|
// and a move written meanwhile, even one read from the file as it was
|
|
// before, is seen at once.
|
|
func TestHoldReadsMigrationsOnce(t *testing.T) {
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir()) // a run starts from no files, not the last run's
|
|
defer Hold()()
|
|
if Moved("held-sub") {
|
|
t.Fatal("moved before any move")
|
|
}
|
|
os.MkdirAll(filepath.Dir(migrationsPath()), 0o755)
|
|
os.WriteFile(migrationsPath(), []byte(`{"other-sub":{"state":"plugin"}}`), 0o600)
|
|
if Moved("other-sub") {
|
|
t.Fatal("migrations.json read again while held")
|
|
}
|
|
if err := setMigration("held-sub", func(m *Migration) { m.State = MovePlugin }); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !Moved("held-sub") || !Moved("other-sub") {
|
|
t.Fatalf("a move written while held: %v %v", Moved("held-sub"), Moved("other-sub"))
|
|
}
|
|
}
|
|
|
|
// A group read while a request holds the catalog is the caller's own: the
|
|
// TUI changes its group in place before saving it, and a save that fails
|
|
// left that change in every look-up's providers.json for the rest of the
|
|
// hold (SyncCatalog and the gateway's routes read it meanwhile); a group
|
|
// handed to SaveGroup isn't cleaned in the caller's lists either.
|
|
func TestHeldGroupsAreTheCallers(t *testing.T) {
|
|
t.Setenv("HOME", t.TempDir())
|
|
t.Setenv("USERPROFILE", t.TempDir())
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
for _, id := range []string{"a", "b"} {
|
|
if err := Save(Provider{ID: id, Name: id, Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"m", "big"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
saved := Group{ID: "pair", Name: "Pair", Members: []string{"a/m", "b/big"}, Off: []string{"b/big"}, Levels: []string{"low", "high"},
|
|
Rules: []Rule{{Use: "a/m", Tokens: 5}, {Use: "b/big", Agents: []string{"codex"}, Folders: []string{filepath.Join(t.TempDir(), "in")}, ExceptFolders: []string{filepath.Join(t.TempDir(), "out")},
|
|
Time: &TimeWindow{From: "09:00", To: "18:00", Days: []string{"mon"}}}}}
|
|
if err := SaveGroup(saved); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer Hold()()
|
|
var folders, except []string
|
|
pair := func() Group {
|
|
t.Helper()
|
|
for _, g := range Groups() {
|
|
if g.ID == "pair" {
|
|
return g
|
|
}
|
|
}
|
|
t.Fatal("no group pair")
|
|
return Group{}
|
|
}
|
|
same := func(why string) {
|
|
t.Helper()
|
|
g := pair()
|
|
if !slices.Equal(g.Members, saved.Members) || !slices.Equal(g.Off, saved.Off) || !slices.Equal(g.Levels, saved.Levels) ||
|
|
len(g.Rules) != 2 || g.Rules[0].Use != "a/m" || g.Rules[1].Use != "b/big" ||
|
|
!slices.Equal(g.Rules[1].Agents, []string{"codex"}) || g.Rules[1].Time == nil || !slices.Equal(g.Rules[1].Time.Days, []string{"mon"}) ||
|
|
!slices.Equal(g.Rules[1].Folders, folders) || !slices.Equal(g.Rules[1].ExceptFolders, except) {
|
|
t.Fatalf("%s: %+v", why, g)
|
|
}
|
|
}
|
|
// the folders as saved, cleaned
|
|
g := pair()
|
|
folders, except = slices.Clone(g.Rules[1].Folders), slices.Clone(g.Rules[1].ExceptFolders)
|
|
if len(folders) != 1 || len(except) != 1 {
|
|
t.Fatalf("folders saved as %q, %q", folders, except)
|
|
}
|
|
// taking a/m out, as the TUI's d does: b/big alone is off, so it fails
|
|
g = pair()
|
|
g.Members = slices.DeleteFunc(g.Members, func(m string) bool { return m == "a/m" })
|
|
g.Rules = slices.DeleteFunc(g.Rules, func(r Rule) bool { return r.Use == "a/m" })
|
|
if err := SaveGroup(g); err == nil || !strings.Contains(err.Error(), "switched off") {
|
|
t.Fatalf("saved: %v", err)
|
|
}
|
|
same("a failed save's change is in the held groups")
|
|
g = pair()
|
|
g.Levels[0], g.Rules[1].Agents[0], g.Rules[1].Time.Days[0] = "max", "x", "sun"
|
|
g.Rules[1].Folders[0], g.Rules[1].ExceptFolders[0] = "/elsewhere", "/elsewhere"
|
|
same("a caller's change is in the held groups")
|
|
|
|
// the caller's group, cleaned for saving: as it gave it, saved or not
|
|
in := Group{Name: "R", Members: []string{"a/m", "b/big"}, Rules: []Rule{{Use: " b/big ", Intent: "tests"}}}
|
|
if err := SaveGroup(in); err == nil || !strings.Contains(err.Error(), "classifier") {
|
|
t.Fatalf("saved: %v", err)
|
|
}
|
|
if in.Rules[0].Use != " b/big " {
|
|
t.Fatalf("SaveGroup changed the caller's rule: %q", in.Rules[0].Use)
|
|
}
|
|
}
|
|
|
|
// Pricing an agent's models (agent.magpieModels) asks All for each model,
|
|
// under one Hold: settings.json is read with the providers, not once per
|
|
// signed-in account per model. Asked for each account, a magpie at rest
|
|
// parsed it hundreds of times on every drift look the GUI makes each 15s
|
|
// and every dsh round each 30s, most of its idle CPU (xsw on Discord).
|
|
func TestAllReadsNoSettingsPerLookWhileHeld(t *testing.T) {
|
|
signIn(t)
|
|
t.Setenv("MAGPIE_NO_ACCOUNTS", "")
|
|
if err := settings.Save(settings.Settings{Proxy: "direct"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
accounts := 0
|
|
for _, p := range All() {
|
|
if p.Account != nil {
|
|
accounts++
|
|
}
|
|
}
|
|
if accounts < 2 {
|
|
t.Fatalf("the fixture signs in %d accounts; the test needs at least two", accounts)
|
|
}
|
|
// The first look under the Hold builds the providers, which reads the
|
|
// settings a number of times of its own; what is counted is that the
|
|
// looks after it, each of every model's prices, don't add a read per
|
|
// account each. Looks is far above what the build reads, so a second
|
|
// build (the held catalog is kept 1s) stays under it.
|
|
const looks = 200
|
|
release := Hold()
|
|
defer release()
|
|
before := settings.Parses()
|
|
for range looks {
|
|
if len(All()) == 0 {
|
|
t.Fatal("no providers")
|
|
}
|
|
EffectivePriceIn(HeldSettings(), "codex", "gpt-5.5")
|
|
}
|
|
if n := settings.Parses() - before; n >= looks {
|
|
t.Fatalf("settings.json parsed %d times for %d looks at %d accounts under one Hold: once a look or more", n, looks, accounts)
|
|
}
|
|
// turned off, the accounts are still left out
|
|
t.Setenv("MAGPIE_NO_ACCOUNTS", "1")
|
|
for _, p := range All() {
|
|
if p.Account != nil {
|
|
t.Fatalf("%s's account listed with discovery off", p.ID)
|
|
}
|
|
}
|
|
}
|