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 on63be47717, 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 on63be47717and here). - The same two packages on63be47717alone, 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.
60 lines
2.0 KiB
Go
60 lines
2.0 KiB
Go
package plugin
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/lastgood"
|
|
)
|
|
|
|
// plugin-auth.json is parsed once for each version of the file, however
|
|
// many times the sign-ins are asked for: every build of the providers
|
|
// asked once for each plugin, and parsed the whole file twice each time
|
|
// (#2502). A new version is read at once; one that doesn't parse is its
|
|
// last good generation, still unknown rather than none (#1505).
|
|
func TestAuthParsedOncePerVersion(t *testing.T) {
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
os.MkdirAll(filepath.Dir(AuthPath()), 0o700)
|
|
write := func(s string) {
|
|
t.Helper()
|
|
if err := os.WriteFile(AuthPath(), []byte(s), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
write(`{"fakeco":{"type":"api","key":"one"},"other#2":{"type":"oauth","accountId":"b@x"}}`)
|
|
if a := Auths("fakeco"); a["fakeco"]["key"] != "one" || len(a) != 1 {
|
|
t.Fatalf("Auths = %v", a)
|
|
}
|
|
SignedIn("other")
|
|
n := authParses.Load()
|
|
for range 100 {
|
|
Auths("fakeco")
|
|
Auths("other")
|
|
SignedIn("other")
|
|
listingProxies()
|
|
}
|
|
if got := authParses.Load() - n; got != 0 {
|
|
t.Fatalf("an unchanged plugin-auth.json was parsed %d more times", got)
|
|
}
|
|
// what a caller is given is its own
|
|
Auths("fakeco")["fakeco"]["key"] = "changed"
|
|
if a := Auths("fakeco"); a["fakeco"]["key"] != "one" {
|
|
t.Fatalf("a caller's change reached the next: %v", a)
|
|
}
|
|
// rewritten at once, the same size: read again
|
|
write(`{"fakeco":{"type":"api","key":"two"},"other#2":{"type":"oauth","accountId":"b@x"}}`)
|
|
if a := Auths("fakeco"); a["fakeco"]["key"] != "two" {
|
|
t.Fatalf("after a rewrite Auths = %v", a)
|
|
}
|
|
// zeroed by a crash: the last good generation
|
|
os.WriteFile(lastgood.Bak(AuthPath()), []byte(`{"fakeco":{"type":"api","key":"kept"}}`), 0o600)
|
|
write("\x00\x00\x00\x00")
|
|
if a := Auths("fakeco"); a["fakeco"]["key"] != "kept" {
|
|
t.Fatalf("a zeroed file read as %v, want its last good generation", a)
|
|
}
|
|
if !SignedIn("fakeco") {
|
|
t.Fatal("a zeroed file's sign-ins read as none")
|
|
}
|
|
}
|