diff --git a/internal/sessions/by_key_test.go b/internal/sessions/by_key_test.go new file mode 100644 index 000000000..a91f4efe9 --- /dev/null +++ b/internal/sessions/by_key_test.go @@ -0,0 +1,35 @@ +package sessions + +import ( + "fmt" + "reflect" + "testing" +) + +// byKey groups as appending file by file did: the kept files of each +// session, in their order, and a group grown later doesn't write into +// another's files. +func TestByKeyGroupsAsAppending(t *testing.T) { + var files []file + for i := range 40 { + files = append(files, file{agent: []string{"codex", "claude"}[i%2], key: fmt.Sprint("k", i%7), path: fmt.Sprint("p", i)}) + } + for _, keep := range []func(*file) bool{nil, func(f *file) bool { return f.agent == "codex" }} { + want := map[string][]file{} + for _, f := range files { + if keep == nil || keep(&f) { + want[f.key] = append(want[f.key], f) + } + } + got := byKey(files, keep) + if !reflect.DeepEqual(got, want) { + t.Fatalf("byKey = %v\nwant %v", got, want) + } + for k := range got { + _ = append(got[k], file{key: "grown", path: "grown"}) + } + if !reflect.DeepEqual(got, want) { + t.Fatalf("after each group grew, byKey's groups = %v\nwant %v", got, want) + } + } +} diff --git a/internal/sessions/codex_provider.go b/internal/sessions/codex_provider.go index 152932da0..c8d2b4f73 100644 --- a/internal/sessions/codex_provider.go +++ b/internal/sessions/codex_provider.go @@ -203,11 +203,10 @@ func MoveCodexProvider(id, to string) (CodexMove, error) { loadCache() defer useDBs()() all := allFiles() - groups := map[string][]file{} + groups := byKey(all, func(f *file) bool { return f.agent == "codex" }) var mine []file for _, f := range all { if f.agent == "codex" { - groups[f.key] = append(groups[f.key], f) mine = append(mine, f) } } diff --git a/internal/sessions/manage.go b/internal/sessions/manage.go index 56daf168f..ab383f0dd 100644 --- a/internal/sessions/manage.go +++ b/internal/sessions/manage.go @@ -180,13 +180,12 @@ func idInFile(agent string) bool { } func nativeIDs(agents map[string]bool, all []file) map[string]map[string]bool { - groups := map[string][]file{} + groups := byKey(all, func(f *file) bool { return agents[f.agent] }) var want []file for _, f := range all { if !agents[f.agent] { continue } - groups[f.key] = append(groups[f.key], f) if idInFile(f.agent) { want = append(want, f) } @@ -203,11 +202,10 @@ func nativeIDs(agents map[string]bool, all []file) map[string]map[string]bool { } func listAgent(agent string, all []file) []Managed { - groups := map[string][]file{} + groups := byKey(all, func(f *file) bool { return f.agent == agent }) var mine []file for _, f := range all { if f.agent == agent { - groups[f.key] = append(groups[f.key], f) mine = append(mine, f) } } @@ -313,11 +311,10 @@ func Delete(agent, id string) (Trashed, error) { loadCache() defer useDBs()() all := allFiles() - groups := map[string][]file{} + groups := byKey(all, func(f *file) bool { return f.agent == agent }) var mine []file for _, f := range all { if f.agent == agent { - groups[f.key] = append(groups[f.key], f) mine = append(mine, f) } } diff --git a/internal/sessions/sessions.go b/internal/sessions/sessions.go index 117952471..65fe779ff 100644 --- a/internal/sessions/sessions.go +++ b/internal/sessions/sessions.go @@ -393,6 +393,35 @@ type file struct { cold bool } +// byKey groups the files keep takes by session, each group in the files' +// order. The groups are cut from one copy of the kept files rather than +// grown an append at a time: a file is a few hundred bytes, and a Sessions +// poll grouped every one of ~16k files three times, ~150 MB in eight polls. +// A nil keep takes every file. +func byKey(files []file, keep func(*file) bool) map[string][]file { + count := map[string]int{} + n := 0 + for i := range files { + if keep == nil || keep(&files[i]) { + count[files[i].key]++ + n++ + } + } + kept := make([]file, n) + groups := make(map[string][]file, len(count)) + at := 0 + for k, c := range count { + groups[k] = kept[at : at : at+c] + at += c + } + for i := range files { + if f := &files[i]; keep == nil || keep(f) { + groups[f.key] = append(groups[f.key], *f) + } + } + return groups +} + // ClaudeDir is Claude Code's folder: $CLAUDE_CONFIG_DIR, else ~/.claude. func ClaudeDir() string { return appdir.ClaudeDir() } @@ -1308,10 +1337,9 @@ func listSessions(limit int) []Session { defer useDBs()() files := allFiles() - groups := map[string][]file{} + groups := byKey(files, nil) latest := map[string]time.Time{} for _, f := range files { - groups[f.key] = append(groups[f.key], f) if f.mod.After(latest[f.key]) { latest[f.key] = f.mod } @@ -2267,11 +2295,10 @@ func Titles(keys []string) map[string]string { loadCache() defer useDBs()() files := allFiles() - groups := map[string][]file{} + groups := byKey(files, func(f *file) bool { return want[f.key] }) var fs []file for _, f := range files { if want[f.key] { - groups[f.key] = append(groups[f.key], f) fs = append(fs, f) } } diff --git a/internal/sessions/stats.go b/internal/sessions/stats.go index f603b7dee..07203308e 100644 --- a/internal/sessions/stats.go +++ b/internal/sessions/stats.go @@ -190,10 +190,7 @@ func statsRead(days int, now time.Time, native map[string]bool) Stats { identities := codexIdentitySnapshot(files) taskTitles := codexSubagentTaskTitles(files) if native != nil { - groups := map[string][]file{} - for _, f := range files { - groups[f.key] = append(groups[f.key], f) - } + groups := byKey(files, nil) price, titles := pricer(), codexTitleOf() for _, fs := range groups { // as List: a session nothing was said in is not listed @@ -375,12 +372,7 @@ func statsRead(days int, now time.Time, native map[string]bool) Stats { } } - groups := map[string][]file{} - for _, f := range files { - if shares[f.key] != nil { - groups[f.key] = append(groups[f.key], f) - } - } + groups := byKey(files, func(f *file) bool { return shares[f.key] != nil }) first, _ := time.ParseInLocation(time.DateOnly, out.From, time.Local) for key, sh := range shares { if len(sh.dates) == 0 {