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 }