From 8ab4fd6120ad46f16e54d609072e4d01c3772818 Mon Sep 17 00:00:00 2001 From: yetone Date: Tue, 27 Oct 2026 04:04:55 +0800 Subject: [PATCH] sessions: the overview keeps each session's own title, not a copy cut to 160 title() keeps 160 characters and an ellipsis, 161 in all, and the overview cut every such title again to 160. So each range statsFor keeps (all days, and 30) held a new copy of nearly every title, and clip made a []rune of each title to count it. On a 15,921-file history, 12,220 of the 12,289 titles are 161 long. The overview now clips at titleKept (161), and clip counts runes without converting, returning s itself when it fits. Run at this head (d4699c00a + this change), in a sandbox HOME with the real ~/.codex and ~/.claude sessions, warm start (KEEP=1, the parse cache kept), two alternating runs each, at 40 s after settling: before: footprint 191 / 193 MB, live heap 112 / 112 MB after: footprint 185 / 179 MB, live heap 107 / 102 MB TestStatsTitleIsTheSessionsOwn is red with titleKept set back to 160, green with it. go test ./internal/sessions ./internal/gui ./internal/usage ok; gofmt clean; GOOS=linux and GOOS=windows go vet -tags nogui ./internal/sessions ok. Not run: the full suite (two packages' worth of change, the GUI package passes). --- internal/sessions/stats.go | 15 +++++++++++---- internal/sessions/stats_test.go | 12 ++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/internal/sessions/stats.go b/internal/sessions/stats.go index 7d3cd66e7..f741c9f57 100644 --- a/internal/sessions/stats.go +++ b/internal/sessions/stats.go @@ -7,6 +7,7 @@ import ( "sort" "strings" "time" + "unicode/utf8" "github.com/yetone/magpie/internal/provider" ) @@ -354,7 +355,7 @@ func statsRead(days int, now time.Time, native map[string]bool) Stats { } setSessionCodexIdentity(&s, identities) setSessionCodexTaskTitle(&s, taskTitles) - sum := Summary{Key: key, Agent: s.Agent, ID: s.ID, Cwd: cmp.Or(folder[key], s.Cwd), Title: clip(s.Title, 160), + sum := Summary{Key: key, Agent: s.Agent, ID: s.ID, Cwd: cmp.Or(folder[key], s.Cwd), Title: clip(s.Title, titleKept), Nickname: s.Nickname, Last: s.Last, Role: s.Role, ParentSession: s.ParentSession, RootSession: s.RootSession, Priced: true, Active: sh.active / 1000, Models: []string{}, Days: []int{}, Prompts: sh.prompts, Replies: sh.replies, tools: sh.tools, skills: sh.skills, skillLast: sh.skillLast, @@ -406,12 +407,18 @@ func statsRead(days int, now time.Time, native map[string]bool) Stats { return out } -// clip cuts s to at most n characters, with an ellipsis. +// titleKept is as long as a title gets from title(): 160 characters and +// the ellipsis. +const titleKept = 161 + +// clip cuts s to at most n characters, with an ellipsis. One that fits is +// s itself: the overview's titles are the cached sessions' own strings, not +// a copy of each in every range kept (statsFor). func clip(s string, n int) string { - r := []rune(s) - if len(r) <= n { + if utf8.RuneCountInString(s) <= n { return s } + r := []rune(s) return strings.TrimSpace(string(r[:n-1])) + "…" } diff --git a/internal/sessions/stats_test.go b/internal/sessions/stats_test.go index f1629a8a1..337dbdb3d 100644 --- a/internal/sessions/stats_test.go +++ b/internal/sessions/stats_test.go @@ -5,8 +5,10 @@ import ( "path/filepath" "slices" "strconv" + "strings" "testing" "time" + "unsafe" "github.com/yetone/magpie/internal/testenv" ) @@ -293,6 +295,16 @@ func TestStatsHoursAndSessions(t *testing.T) { } } +// The overview's title of a long first prompt is the session's own string: +// cut again to 160, it was a new copy of every title in each range kept. +func TestStatsTitleIsTheSessionsOwn(t *testing.T) { + long := title(strings.Repeat("写一个很长的提示词", 40)) + got := clip(long, titleKept) + if got != long || unsafe.StringData(got) != unsafe.StringData(long) { + t.Fatalf("clipped again: %q", got) + } +} + func sortedByLast(ss []Summary) bool { for i := 1; i < len(ss); i++ { if ss[i].Last.After(ss[i-1].Last) {