sessions: a summary's tools, skills and days are sorted slices, not maps

Each range statsFor keeps (all days, and 30, warmed at start) holds a
Summary for every session, and each had four maps (tools, skills,
skillLast, perDay) plus a tools map in every day. Most hold one or two
names, and a Go map of one is a few hundred bytes. They are now
counts (name, calls, a skill's last date) in the names' order, and
perDay a slice of days in order; the build still sums into maps and
hands those over once.

Run at this head (8ab4fd612 + this change), in a sandbox HOME with the
real ~/.codex and ~/.claude sessions (15,921 files), warm start, two
alternating runs each, 40 s after settling:
  before: footprint 174 / 178 MB, live heap 104 / 104 MB
  after:  footprint 165 / 170 MB, live heap 97 / 96 MB
Both builds side by side on the same files, /api/sessions/overview at
days 30, 0, 30&agent=codex and 0&agent=claude, and /api/sessions/stats
at 30: stats identical; the overviews differ only in the title length
8ab4fd612 changed and in today's counts of the Claude sessions being
written between the two reads (codex-only: titles alone).
go test ./internal/sessions ./internal/gui ok; ./internal/usage red
once on TestUsageReadAfterAppendHashesNothingOnAFineClock's whole-second
case (#3511, #3551, untouched here), 5 of 5 ok rerun. gofmt clean;
GOOS=linux and GOOS=windows go vet -tags nogui ./internal/sessions ok.
This commit is contained in:
yetone
2026-10-27 04:15:48 +08:00
parent 8ab4fd6120
commit 3393e61d00
6 changed files with 78 additions and 36 deletions
+3 -3
View File
@@ -200,11 +200,11 @@ func TestAlmaSessions(t *testing.T) {
t.Errorf("chat A stats: prompts %d replies %d tools %d", sum.Prompts, sum.Replies, sum.ToolCalls)
}
for _, tool := range []string{"Bash", "Skill", "Task", "mcp_search", "Read"} {
if sum.tools[tool] != 1 {
t.Errorf("tool %s called %d times, want 1 (%v)", tool, sum.tools[tool], sum.tools)
if sum.tools.of(tool) != 1 {
t.Errorf("tool %s called %d times, want 1 (%v)", tool, sum.tools.of(tool), sum.tools)
}
}
if sum.skills["image-gen"] != 1 {
if sum.skills.of("image-gen") != 1 {
t.Errorf("skills %v", sum.skills)
}
+11 -8
View File
@@ -110,11 +110,10 @@ func MergeExternalStats(st Stats, days []ExternalDayUsage, external []ExternalSe
for j := range s.Days {
s.Days[j] += shift
}
pd := map[int]*summaryDay{}
for d, v := range s.perDay {
pd[d+shift] = v
s.perDay = slices.Clone(s.perDay)
for j := range s.perDay {
s.perDay[j].day += shift
}
s.perDay = pd
}
}
byDate := map[string]int{}
@@ -151,7 +150,8 @@ func MergeExternalStats(st Stats, days []ExternalDayUsage, external []ExternalSe
if e.ID == "" || e.Agent == "" {
continue
}
s := Summary{Key: e.Agent + ":" + e.ID, Agent: e.Agent, ID: e.ID, Title: cmp.Or(names[e.Agent+"|"+e.ID], e.Title), Gateway: true, Last: e.Last, Tokens: e.Tokens, Cost: e.Cost, Priced: true, Models: []string{}, Days: []int{}, perDay: map[int]*summaryDay{}}
s := Summary{Key: e.Agent + ":" + e.ID, Agent: e.Agent, ID: e.ID, Title: cmp.Or(names[e.Agent+"|"+e.ID], e.Title), Gateway: true, Last: e.Last, Tokens: e.Tokens, Cost: e.Cost, Priced: true, Models: []string{}, Days: []int{}}
perDay := map[int]*summaryDay{}
for _, u := range e.Daily {
day, err := time.ParseInLocation(time.DateOnly, u.Date, time.Local)
if err != nil {
@@ -161,15 +161,18 @@ func MergeExternalStats(st Stats, days []ExternalDayUsage, external []ExternalSe
if first.IsZero() || u.Date < st.From || u.Date > st.To {
continue
}
pd := s.perDay[d]
pd := perDay[d]
if pd == nil {
pd = &summaryDay{}
s.perDay[d] = pd
pd = &summaryDay{day: d}
perDay[d] = pd
s.Days = append(s.Days, d)
}
pd.output += u.Output
}
sort.Ints(s.Days)
for _, d := range s.Days {
s.perDay = append(s.perDay, *perDay[d])
}
for _, m := range e.Models {
s.Models = append(s.Models, m.Model)
s.Priced = s.Priced && m.Priced
+1 -1
View File
@@ -6,7 +6,7 @@ import (
)
func TestGatewayStatsRebaseNativeCalendar(t *testing.T) {
native := Stats{From: "2026-10-06", To: "2026-10-07", Sessions: []Summary{{Agent: "claude", ID: "native", Days: []int{0}, perDay: map[int]*summaryDay{0: {output: 4}}}}}
native := Stats{From: "2026-10-06", To: "2026-10-07", Sessions: []Summary{{Agent: "claude", ID: "native", Days: []int{0}, perDay: []summaryDay{{day: 0, output: 4}}}}}
merged := MergeExternalStats(native, []ExternalDayUsage{{Date: "2026-10-04", Agent: "opencode"}}, nil)
view := merged.Overview("claude", "", "", 8)
if !reflect.DeepEqual(view.Days, []int{0, 0, 1, 0}) || !reflect.DeepEqual(view.Output, []int{0, 0, 4, 0}) {
+59 -20
View File
@@ -60,16 +60,48 @@ type Summary struct {
Replies int `json:"replies"`
ToolCalls int `json:"tool_calls"`
tools map[string]int // calls by tool
skills map[string]int // calls by skill
skillLast map[string]string // the last date each skill was called
perDay map[int]*summaryDay // by day after From
// slices and not maps: each range statsFor keeps holds them for every
// session, and a map of one or two names is a few hundred bytes
tools counts // calls by tool
skills counts // calls by skill, with the last date of each
perDay []summaryDay // by day after From, in order
}
// summaryDay is a session's day, for the overview's calendar and trends.
type summaryDay struct {
day int // after From
messages, output int
tools map[string]int // calls by tool category
tools counts // calls by tool category
}
// counts are calls by name, in the names' order.
type counts []nameCount
type nameCount struct {
name string
n int
last string // a skill's: the last date it was called
}
// of is name's calls.
func (c counts) of(name string) int {
if i, ok := slices.BinarySearchFunc(c, name, func(c nameCount, name string) int { return strings.Compare(c.name, name) }); ok {
return c[i].n
}
return 0
}
// countsOf is m in the names' order, with last's date for each when given.
func countsOf(m map[string]int, last map[string]string) counts {
if len(m) == 0 {
return nil
}
out := make(counts, 0, len(m))
for name, n := range m {
out = append(out, nameCount{name: name, n: n, last: last[name]})
}
slices.SortFunc(out, func(a, b nameCount) int { return strings.Compare(a.name, b.name) })
return out
}
// Day is one local date's usage, cut fine enough for the page to filter
@@ -193,6 +225,10 @@ func statsRead(days int, now time.Time, native map[string]bool) Stats {
act := map[ak]*act0{}
dates := map[string]bool{}
// each session's share: its files' models and dates in range
type dayShare struct {
messages, output int
tools map[string]int
}
type share struct {
models map[string]Tokens
active int64
@@ -200,7 +236,7 @@ func statsRead(days int, now time.Time, native map[string]bool) Stats {
prompts, replies int
tools, skills map[string]int
skillLast map[string]string
perDate map[string]*summaryDay
perDate map[string]*dayShare
}
shares := map[string]*share{}
for _, f := range want {
@@ -247,12 +283,12 @@ func statsRead(days int, now time.Time, native map[string]bool) Stats {
sh := shares[f.key]
if sh == nil {
sh = &share{models: map[string]Tokens{}, dates: map[string]bool{}, tools: map[string]int{},
skills: map[string]int{}, skillLast: map[string]string{}, perDate: map[string]*summaryDay{}}
skills: map[string]int{}, skillLast: map[string]string{}, perDate: map[string]*dayShare{}}
shares[f.key] = sh
}
pd := sh.perDate[date]
if pd == nil {
pd = &summaryDay{tools: map[string]int{}}
pd = &dayShare{tools: map[string]int{}}
sh.perDate[date] = pd
}
for model, t := range d.Models {
@@ -358,16 +394,17 @@ func statsRead(days int, now time.Time, native map[string]bool) Stats {
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,
perDay: map[int]*summaryDay{}}
Prompts: sh.prompts, Replies: sh.replies, tools: countsOf(sh.tools, nil), skills: countsOf(sh.skills, sh.skillLast)}
for _, n := range sh.tools {
sum.ToolCalls += n
}
sum.perDay = make([]summaryDay, 0, len(sh.perDate))
for date, pd := range sh.perDate {
if t, err := time.ParseInLocation(time.DateOnly, date, time.Local); err == nil {
sum.perDay[int(math.Round(t.Sub(first).Hours()/24))] = pd
sum.perDay = append(sum.perDay, summaryDay{day: int(math.Round(t.Sub(first).Hours() / 24)), messages: pd.messages, output: pd.output, tools: countsOf(pd.tools, nil)})
}
}
slices.SortFunc(sum.perDay, func(a, b summaryDay) int { return cmp.Compare(a.day, b.day) })
for model, t := range sh.models {
sum.Tokens.add(t)
sum.Models = append(sum.Models, model)
@@ -522,8 +559,8 @@ func (st Stats) Overview(agent, model, cwd string, n int) Overview {
out.Days[d]++
}
}
for d, pd := range s.perDay {
if d >= 0 && d < len(out.Days) {
for _, pd := range s.perDay {
if d := pd.day; d >= 0 && d < len(out.Days) {
out.Messages[d] += pd.messages
out.Output[d] += pd.output
}
@@ -603,7 +640,8 @@ func toolUse(in []Summary, first time.Time, n int) ToolUse {
out.Sessions++
out.Calls += s.ToolCalls
seen := map[string]bool{}
for name, c := range s.tools {
for _, tc := range s.tools {
name, c := tc.name, tc.n
t := tools[name]
if t == nil {
t = &ToolStat{Name: name, Category: ToolCategory(name)}
@@ -625,16 +663,16 @@ func toolUse(in []Summary, first time.Time, n int) ToolUse {
if first.IsZero() {
continue
}
for d, pd := range s.perDay {
day := first.AddDate(0, 0, d)
for _, pd := range s.perDay {
day := first.AddDate(0, 0, pd.day)
mon := day.AddDate(0, 0, -(int(day.Weekday())+6)%7).Format(time.DateOnly)
w := weeks[mon]
if w == nil {
w = map[string]int{}
weeks[mon] = w
}
for cat, c := range pd.tools {
w[cat] += c
for _, c := range pd.tools {
w[c.name] += c.n
}
}
}
@@ -661,7 +699,8 @@ func skillUse(in []Summary, n int) SkillUse {
skills := map[string]*Skill{}
projects := map[string]map[string]int{}
for _, s := range in {
for name, c := range s.skills {
for _, sc := range s.skills {
name, c := sc.name, sc.n
k := skills[name]
if k == nil {
k = &Skill{Name: name, Agents: map[string]int{}, Projects: []ToolStat{}}
@@ -671,7 +710,7 @@ func skillUse(in []Summary, n int) SkillUse {
k.Calls += c
k.Sessions++
k.Agents[s.Agent] += c
k.Last = max(k.Last, s.skillLast[name])
k.Last = max(k.Last, sc.last)
projects[name][s.Cwd] += c
out.Calls += c
}
+2 -2
View File
@@ -58,10 +58,10 @@ func TestPiToolsAndMessages(t *testing.T) {
if p.Prompts != 2 || p.Replies != 4 || p.ToolCalls != 7 {
t.Fatalf("pi: %d prompts, %d replies, %d calls (%v)", p.Prompts, p.Replies, p.ToolCalls, p.tools)
}
if p.tools["bash"] != 1 || p.tools["read"] != 2 || p.tools["codemode"] != 1 || p.tools["mcp__opencli_mcp__open"] != 1 || p.tools["edit"] != 1 {
if p.tools.of("bash") != 1 || p.tools.of("read") != 2 || p.tools.of("codemode") != 1 || p.tools.of("mcp__opencli_mcp__open") != 1 || p.tools.of("edit") != 1 {
t.Fatalf("pi tools %v", p.tools)
}
if p.skills["pdf"] != 1 || p.skills["review"] != 1 || len(p.skills) != 2 {
if p.skills.of("pdf") != 1 || p.skills.of("review") != 1 || len(p.skills) != 2 {
t.Fatalf("pi skills %v", p.skills)
}
// the fork's own prompt and reply; what it copied is the first's
+2 -2
View File
@@ -66,10 +66,10 @@ func TestStatsToolsAndSkills(t *testing.T) {
if cc.Prompts != 2 || cc.Replies != 2 || cc.ToolCalls != 5 {
t.Fatalf("claude: %d prompts, %d replies, %d calls (%v)", cc.Prompts, cc.Replies, cc.ToolCalls, cc.tools)
}
if cc.tools["Bash"] != 1 || cc.tools["Grep"] != 1 || cc.tools["mcp__slack__send"] != 1 || cc.skills["artifact-design"] != 1 {
if cc.tools.of("Bash") != 1 || cc.tools.of("Grep") != 1 || cc.tools.of("mcp__slack__send") != 1 || cc.skills.of("artifact-design") != 1 {
t.Fatalf("claude tools %v skills %v", cc.tools, cc.skills)
}
if cxs.Prompts != 2 || cxs.Replies != 1 || cxs.ToolCalls != 2 || cxs.tools["exec_command"] != 1 || cxs.tools["apply_patch"] != 1 {
if cxs.Prompts != 2 || cxs.Replies != 1 || cxs.ToolCalls != 2 || cxs.tools.of("exec_command") != 1 || cxs.tools.of("apply_patch") != 1 {
t.Fatalf("codex: %+v %v", cxs, cxs.tools)
}