From 71a68639b0c936746192c8041292af3af1b958b2 Mon Sep 17 00:00:00 2001 From: yetone Date: Wed, 11 Nov 2026 09:46:58 +0800 Subject: [PATCH] sessions: every Claude folder's files are listed into a slice made at their size With more than one Claude folder (CLAUDE_CONFIG_DIR per account), claudeFiles appended each folder's files to a slice grown file by file. It runs on every pass over the session files, and the Sessions page's ranges are refreshed every 10 s, so on the owner's real history it was the largest single allocation of that refresh. The folders are listed first now, and the answer made once at their total; with no Claude folder the answer is nil, as before. Run: TestClaudeFilesMadeAtTheirSize (new) passes; on the old code it fails ("listed into room for 76, want the 41 listed"). go test ./internal/sessions ./internal/gui and go vet (darwin, GOOS=windows, GOOS=linux -tags nogui) ok at 9cd4ef6a1 plus this change. Real account (mote, ~/r2278/acct, /api/sessions/stats polled every 10 s for days 0, 7 and 30 with X-Magpie-Had, as the page does; 4 minutes of alloc_space diffed): claudeFiles 41.7 MB flat / 57.5 MB cum before, 17.7 / 39.6 MB after. --- internal/sessions/claude_homes_test.go | 30 ++++++++++++++++++++++++++ internal/sessions/sessions.go | 21 +++++++++++++----- 2 files changed, 46 insertions(+), 5 deletions(-) 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