Files
magpie/internal/settings/accounts.go
T
yetone c3dc7aefba provider, settings: an idle magpie no longer parses settings.json for every account of every model's price (xsw on Discord)
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.
2026-10-13 16:34:15 +08:00

26 lines
800 B
Go

package settings
import (
"os"
"strings"
)
// AccountDiscoveryOff is whether magpie leaves agents' sign-ins unread
// (#1535): NoAccountDiscovery saved, or MAGPIE_NO_ACCOUNTS set to anything
// but "", "0" or "false" for this process alone. The variable only ever
// turns discovery off; it never turns on what the user saved off.
func AccountDiscoveryOff() bool {
return noAccountsEnv() || Load().NoAccountDiscovery
}
// AccountDiscoveryOffIn is AccountDiscoveryOff with the settings already
// read, for a caller asking it many times in one look.
func AccountDiscoveryOffIn(s Settings) bool {
return noAccountsEnv() || s.NoAccountDiscovery
}
func noAccountsEnv() bool {
v := strings.ToLower(strings.TrimSpace(os.Getenv("MAGPIE_NO_ACCOUNTS")))
return v != "" && v != "0" && v != "false"
}