Files
magpie/internal/settings/accounts.go
T
yetone f54e76dbdd gateway: a request builds the providers once, and plugin-auth.json and the discovery setting are parsed once a version (#2502)
XcodeFish (#2502): with several agents at once the gateway sat at
100-350% CPU. Counting provider.AllBuilt around one streamed chat
request gave 3 full catalogue builds per request, and each build re-read
and re-parsed plugin-auth.json and settings.json (AccountDiscoveryOff
called Load() on every provider list).

- holdCatalog wraps the gateway's handler in provider.Hold() and
  filememo.Hold(), so one request builds the providers once and stats
  each file at most once a second. Hold already drops the cache on
  provider.Changed and on magpie's own writes; another program's write
  is seen within the second.
- plugin-auth.json is read through filememo (mtime, size, and the raw
  bytes while the file is under 2s old), so it is parsed once per
  version. A file that doesn't parse falls back to the last good .bak
  through lastgood, as settings does. Auths still hands each caller its
  own decoded map. The host looks at the file again before it reports a
  sign-in it just saved.
- AccountDiscoveryOff reads the setting through filememo with the same
  parse settings.Load uses (now settings.parse), under fileMu.

What was run:
- TestRequestBuildsProvidersOnce: two streamed requests, one build each
  (3 each before). Red with holdCatalog taken out.
- TestAuthParsedOncePerVersion: 100 rounds of Auths, SignedIn and the
  listing proxies parse plugin-auth.json zero more times; a caller's
  edit doesn't leak; a same-size rewrite is read; a zeroed file reads
  the .bak and stays signed in. Red with authRaw reading past filememo.
- TestAccountDiscoveryOffParsedOnce: 100 calls, no parse; a Save is
  read. Red with AccountDiscoveryOff back on Load().
- gofmt -l clean; go vet -tags nogui on darwin, linux and windows ok.
- Full suite, set -o pipefail; go test -p 4 -timeout 45m ./... at this
  pair rebased on 63be47717, machine load average 170-330 from other
  work: 63 packages ok; internal/agent and internal/gateway ran out of
  45m (#2544, #2191, #2108); internal/gui TestMainWindowCorners killed
  at its 40s deadline (#1936). Run alone afterwards, gateway and agent
  ran out of 45m again, and gateway's
  TestQuietCodexStreamKeptAliveThroughReplyWriters/a_middleware got its
  reply unchanged once, the middleware failing open on its time limit
  (filed #2565; 6/6 alone on 63be47717 and here).
- The same two packages on 63be47717 alone, side by side with this
  head under the same load: both ran out of 45m there too, in other
  tests, so the timeouts are not this change.

Not tried: a benchmark against a real many-agent load, or the
reporter's machine. The count of builds per request is the measure.
2026-10-16 20:05:11 +08:00

43 lines
1.3 KiB
Go

package settings
import (
"os"
"strings"
"github.com/yetone/magpie/internal/filememo"
)
// 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 {
if noAccountsEnv() {
return true
}
fileMu.RLock()
defer fileMu.RUnlock()
// the one setting, parsed once for each version of the file: it is
// asked for each agent and each plugin every time the providers are
// built, a whole parse of settings.json each (#2502)
off, err := filememo.Read("settings:no-accounts", Path(), func(b []byte) (bool, error) {
s, err := parse(b)
return s.NoAccountDiscovery, err
})
if err != nil {
return load().NoAccountDiscovery
}
return off
}
// 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"
}