diff --git a/internal/sessions/claude_homes_test.go b/internal/sessions/claude_homes_test.go index e19b40383..f63bcb788 100644 --- a/internal/sessions/claude_homes_test.go +++ b/internal/sessions/claude_homes_test.go @@ -54,3 +54,33 @@ func TestListReadsEveryClaudeFolderOnce(t *testing.T) { t.Fatalf("rows = %v, want main1 and work1 once each", n) } } + +// Every Claude folder's files are listed into one slice made at their +// size, not grown file by file on every pass (a pass every 10 s while +// the Sessions page is open), and a session kept in two folders is +// still listed once. +func TestClaudeFilesMadeAtTheirSize(t *testing.T) { + d := setupCalls(t) + home, _ := os.UserHomeDir() + work := filepath.Join(home, ".claude-work") + user := func(id string) string { + return `{"type":"user","timestamp":"` + stamp(0) + `","message":{"role":"user","content":"hello"},"cwd":"/work/app","sessionId":"` + id + `"}` + } + for i := range 20 { + id := "main" + itoa(i) + writeLines(t, filepath.Join(d.claude, "projects", "-a", id+".jsonl"), user(id)) + id = "work" + itoa(i) + writeLines(t, filepath.Join(work, "projects", "-b", id+".jsonl"), user(id)) + } + writeLines(t, filepath.Join(work, "projects", "-a", "main0.jsonl"), user("main0")) + if len(ClaudeDirs()) != 2 { + t.Fatalf("folders %v", ClaudeDirs()) + } + fs := claudeFiles() + if len(fs) != 40 { + t.Fatalf("%d files, want 40", len(fs)) + } + if cap(fs) != 41 { + t.Fatalf("listed into room for %d, want the 41 listed", cap(fs)) + } +} diff --git a/internal/sessions/sessions.go b/internal/sessions/sessions.go index d788249a4..af79aa1a0 100644 --- a/internal/sessions/sessions.go +++ b/internal/sessions/sessions.go @@ -645,13 +645,24 @@ func ClaudeDirs() []string { return appdir.ClaudeHomes() } // copied to the other) is counted under the first. func claudeFiles() []file { dirs := ClaudeDirs() - if len(dirs) == 1 { + switch len(dirs) { + case 0: + return nil + case 1: return ccFiles("claude", dirs[0]) } - var out []file - owner := map[string]string{} - for _, d := range dirs { - for _, f := range ccFiles("claude", d) { + // listed first, so the answer is made at its size: grown file by file + // it was ~1 MB a pass with a few folders, a pass every 10 s while the + // Sessions page is open + lists, n := make([][]file, len(dirs)), 0 + for i, d := range dirs { + lists[i] = ccFiles("claude", d) + n += len(lists[i]) + } + out := make([]file, 0, n) + owner := make(map[string]string, len(lists[0])) + for i, d := range dirs { + for _, f := range lists[i] { // a subagent's key is its session's: it goes where its session is if o, ok := owner[f.key]; ok && o != d { continue