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).
This commit is contained in:
yetone
2026-10-27 04:04:55 +08:00
parent d4699c00a6
commit 8ab4fd6120
2 changed files with 23 additions and 4 deletions
+11 -4
View File
@@ -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])) + "…"
}
+12
View File
@@ -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) {