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.
This commit is contained in:
@@ -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))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user