/api/providers took ~25ms of CPU a call with the owner's real config (25 providers, plugins signed in), polled by every open window. Its profile had file system calls at about half, each repeated for every provider the list shows: - StampNow stat'ed providers.json, settings.json and the catalog for every provider (KeptListing); under a request's Hold it is looked at once. - plugin.ListedAt was asked for every plugin provider's Listed(); once a request (heldOf "plugin-listed"). - pluginLogins took logins.json's lock, a file opened across magpies, for every plugin even with nothing to change. It now settles the list as read first, and takes the lock and settles again from a fresh read only when something changes. - clientShape read and parsed client-shapes.json for every relay a list shows (TestsAs); parsed once until the file changes (filememo), a copy given out. - Logins(agent) took the same lock for every provider, twice with its account models; listed once an agent a request, a copy each, listed anew after a write (writePrivate's filememo.Forget). Run (on mote unless named): - go test ./internal/provider ./internal/gui -timeout 45m: provider ok (102s) with the new tests; gui red on TestMainWindowCorners (macOS 27, #3949) and on TestSettingsSaveKeepsWhatItDoesNotSend, red on main sinceb09349d17, fixed by #4514's commit before this one. - At the pushed head (rebased one84870f17): go test ./internal/provider ok (100.7s), ./internal/plugin ok (102.7s), TestLoginsListedOnceARequest -count=5 ok (it now clears the 30s look another test left), gui TestSettingsSaveKeepsWhatItDoesNotSend and TestCoreScriptBudget ok; GOOS=linux and windows go vet ./internal/provider ok. Then rebased onto3defaf662(internal/agent dsh.go only); not rerun. - New tests: TestStampLooksOnceARequest, TestClientShapeParsedOnceUntilWritten, TestLoginsListedOnceARequest, and TestAccountListsCopyNoLoginsUnchanged now counts the lock. Broken by hand, each goes red: "51 stamps in one request looked 51 times, want 1", "30 asks parsed the file 30 times", "20 plugin lists with nothing to change took the lock 20 times", "20 lists in one request listed 20 times". - GOOS=linux and windows go vet ./internal/provider: ok. gofmt clean. - Real config, on this Mac: a copy of the owner's ~/.config/magpie in an offline sandbox HOME (proxy :9), 200 GET /api/providers each: v0.1.2541 5.04s CPU (25.2ms a call, median 22.5ms wall), this change 4.55s (22.8ms, median 20.3ms). The profile's stats under StampNow fell from 2.17s to 0.34s. Not run: the full ./... suite; GUI (browser) tests, as nothing the page gets changed. What is left in the profile: StepFun's sign-in file, sync.json, plugin icons and agent detection, each read once a request.
72 lines
2.5 KiB
Go
72 lines
2.5 KiB
Go
package provider
|
|
|
|
import (
|
|
"net/http"
|
|
"os"
|
|
"testing"
|
|
)
|
|
|
|
// Two Codex windows each send their own X-Codex-Window-Id. Taken as part
|
|
// of Codex's shape, every request of one after the other's read, marshalled
|
|
// and synced client-shapes.json again (#3394).
|
|
func TestClientShapeWrittenOnceForTwoWindows(t *testing.T) {
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
clientShapes.Lock()
|
|
clientShapes.written = nil
|
|
clientShapes.Unlock()
|
|
window := func(id, ua string) http.Header {
|
|
return http.Header{
|
|
"User-Agent": {ua},
|
|
"Originator": {"codex_exec"},
|
|
"X-Codex-Window-Id": {id},
|
|
"X-Stainless-Retry-Count": {"0"},
|
|
}
|
|
}
|
|
const ua = "codex_exec/0.162.1 (Mac OS 26.2.0; arm64) kitty"
|
|
SawClient(ClientCodex, window("a:0", ua))
|
|
if clientShape(ClientCodex).Get("Originator") != "codex_exec" {
|
|
t.Fatal("Codex's shape was not kept")
|
|
}
|
|
if err := os.Remove(clientShapesPath()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for i := 0; i < 10; i++ {
|
|
SawClient(ClientCodex, window("b:0", ua))
|
|
SawClient(ClientCodex, window("a:0", ua))
|
|
}
|
|
if _, err := os.Stat(clientShapesPath()); err == nil {
|
|
t.Fatal("client-shapes.json was written again for another window's request")
|
|
}
|
|
SawClient(ClientCodex, window("b:0", "codex_exec/0.163.0 (Mac OS 26.2.0; arm64) kitty"))
|
|
if got := clientShape(ClientCodex).Get("User-Agent"); got != "codex_exec/0.163.0 (Mac OS 26.2.0; arm64) kitty" {
|
|
t.Fatalf("a new Codex version's User-Agent was not kept: %q", got)
|
|
}
|
|
}
|
|
|
|
// /api/providers asks the shape of every relay it lists: the file is
|
|
// parsed once until it changes, and what a caller does to its copy is
|
|
// not what the next one gets.
|
|
func TestClientShapeParsedOnceUntilWritten(t *testing.T) {
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
clientShapes.Lock()
|
|
clientShapes.written = nil
|
|
clientShapes.Unlock()
|
|
SawClient(ClientClaudeCode, http.Header{"User-Agent": {"claude-cli/2.1.0 (external, cli)"}})
|
|
clientShape(ClientClaudeCode)
|
|
parses := shapeParses.Load()
|
|
for range 30 {
|
|
h := clientShape(ClientClaudeCode)
|
|
if h.Get("User-Agent") != "claude-cli/2.1.0 (external, cli)" {
|
|
t.Fatalf("shape read back as %v", h)
|
|
}
|
|
h.Set("User-Agent", "changed")
|
|
}
|
|
if n := shapeParses.Load() - parses; n != 0 {
|
|
t.Fatalf("30 asks parsed the file %d times, want 0", n)
|
|
}
|
|
SawClient(ClientClaudeCode, http.Header{"User-Agent": {"claude-cli/2.2.0 (external, cli)"}})
|
|
if got := clientShape(ClientClaudeCode).Get("User-Agent"); got != "claude-cli/2.2.0 (external, cli)" {
|
|
t.Fatalf("after a write: %q", got)
|
|
}
|
|
}
|