From f37de8cc4ed1f21239bbbdef417f8931aa28cc8a Mon Sep 17 00:00:00 2001 From: yetone Date: Mon, 9 Nov 2026 23:25:15 +0800 Subject: [PATCH] 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. --- .../provider/claude_code_files_held_test.go | 42 +++++++++++++++++++ internal/provider/claude_code_models.go | 8 +++- 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 internal/provider/claude_code_files_held_test.go diff --git a/internal/provider/claude_code_files_held_test.go b/internal/provider/claude_code_files_held_test.go new file mode 100644 index 000000000..a3ff0e729 --- /dev/null +++ b/internal/provider/claude_code_files_held_test.go @@ -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) + } +} diff --git a/internal/provider/claude_code_models.go b/internal/provider/claude_code_models.go index d5e9e0b58..c621455fa 100644 --- a/internal/provider/claude_code_models.go +++ b/internal/provider/claude_code_models.go @@ -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 }