Files
magpie/internal/provider/claude_code_files_held_test.go
T
yetone f37de8cc4e 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.
2026-11-09 23:25:19 +08:00

43 lines
1.3 KiB
Go

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)
}
}