provider: the saved Claude accounts' files are listed once per request

claudeCodePrice and claudeModelsOf listed claude-accounts/*/.claude.json
with filepath.Glob on every call, and each Claude model's price asks.
Building an agent's model list (the window's drift poll rebuilds it each
minute, buildClaudeVia and Pi's list) read the folder once per model. The
listing is now held while a request holds the catalog (heldOf, 1 s), so a
build lists it once; outside a hold it is listed afresh, as before.

Measured on mote, real-account copy, GUI page open in Playwright
Chromium, network blocked, 115 s CPU profile (acgui.sh), v0.1.3425-era
build vs this change:
- filepath.Glob: 50 ms -> gone; agent.buildModels 90 ms -> 50 ms
- total samples 320 ms -> 290 ms; footprint 197 MB -> 172 MB (one run each)

Test: TestClaudeCodeFilesListedOncePerHold saves a folder during a hold
and checks the hold's second listing is the first one (same array), and
that after the hold the new folder is seen. Broken by hand (the old
claudeCodeFiles): red, "listed again within one hold".

Run (mote): go test ./internal/provider ./internal/agent ok; go vet
native (provider, agent), GOOS=linux and GOOS=windows (provider) ok;
gofmt clean.
This commit is contained in:
yetone
2026-11-09 23:25:19 +08:00
parent 623e797fde
commit f37de8cc4e
2 changed files with 49 additions and 1 deletions
@@ -0,0 +1,42 @@
package provider
import (
"os"
"path/filepath"
"testing"
"time"
)
// Each Claude model's price listed the saved accounts' folders again, so
// a build of an agent's models (the window's drift poll, every minute)
// read the folder once per model. While a request holds the catalog they
// are listed once; outside one, and after it, a folder saved since is seen.
func TestClaudeCodeFilesListedOncePerHold(t *testing.T) {
claudeHome(t)
defer func(d time.Duration) { heldFor = d }(heldFor)
heldFor = time.Hour // a loaded machine's pause isn't the bound
saved := func(name string) {
t.Helper()
dir := filepath.Join(claudeDirsRoot(), name)
if err := os.MkdirAll(dir, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, ".claude.json"), []byte("{}"), 0o644); err != nil {
t.Fatal(err)
}
}
saved("a")
release := Hold()
first := claudeCodeFiles()
if len(first) != 2 {
t.Fatalf("listed %v", first)
}
saved("b") // written behind magpie's back: not a write the hold sees
if again := claudeCodeFiles(); len(again) != len(first) || &again[0] != &first[0] {
t.Fatalf("listed again within one hold: %v then %v", first, again)
}
release()
if after := claudeCodeFiles(); len(after) != 3 {
t.Fatalf("a folder saved during the hold isn't seen after it: %v", after)
}
}
+7 -1
View File
@@ -129,8 +129,14 @@ func readClaudeCodeModels(path string) (claudeCodeModels, bool) {
// claudeCodeFiles are the state files of every Claude Code magpie knows of
// an account in: the one Claude Code itself is signed in to, then each
// saved account's own config directory (claude_dirs.go).
// saved account's own config directory (claude_dirs.go). Listed once
// while a request holds the catalog: each Claude model's price asked for
// them, so a build of an agent's models read the folder once per model.
func claudeCodeFiles() []string {
return slices.Clip(heldOf("claude code files", listClaudeCodeFiles))
}
func listClaudeCodeFiles() []string {
if settings.AccountDiscoveryOff() {
return nil
}