From cd0ba05c216c924ef7f962c25111c0293814b45b Mon Sep 17 00:00:00 2001 From: yetone Date: Mon, 9 Nov 2026 20:50:24 +0800 Subject: [PATCH] sessions: each day's usage and active rows are made to size statsOfRange grew every day's Usage and Active rows by append, and the overview keeps three ranges (7, 30, every day), each holding the slack of every doubling. The rows are now counted per date first and made at their length. Run (mote, real-account copy, idle 180s, network blocked): - statsOfRange's own inuse 9.90MB -> 8.37MB (-1.54MB in the diff against the v0.1.3411 build's profile). - TestStatsDayRowsHoldNoSlack: green; red on the old code by hand ("usage 5 of 8, active 5 of 8"). - go test ./internal/sessions/ ok; go vet darwin, linux, windows ok; gofmt clean. Not tried: the full suite (one package changed, nothing outside it reads the rows' capacity). --- internal/sessions/stats.go | 11 ++++++- internal/sessions/stats_day_size_test.go | 42 ++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 internal/sessions/stats_day_size_test.go diff --git a/internal/sessions/stats.go b/internal/sessions/stats.go index f8667c50a..1ef933e49 100644 --- a/internal/sessions/stats.go +++ b/internal/sessions/stats.go @@ -493,9 +493,18 @@ func statsOfRange(days int, today time.Time, files []file, copies map[string]str } } + // each day's rows made to size: grown by append, every range kept + // held the slack of each doubling (#2278) + nUse, nAct := map[string]int{}, map[string]int{} + for k := range use { + nUse[k.date]++ + } + for k := range act { + nAct[k.date]++ + } byDate := map[string]*Day{} for date := range dates { - byDate[date] = &Day{Date: date, Usage: []Usage{}, Active: []Active{}} + byDate[date] = &Day{Date: date, Usage: make([]Usage, 0, nUse[date]), Active: make([]Active, 0, nAct[date])} } for k, t := range use { u := Usage{Agent: k.agent, Cwd: k.cwd, Model: k.model, Tokens: t} diff --git a/internal/sessions/stats_day_size_test.go b/internal/sessions/stats_day_size_test.go new file mode 100644 index 000000000..896beacc2 --- /dev/null +++ b/internal/sessions/stats_day_size_test.go @@ -0,0 +1,42 @@ +package sessions + +import ( + "os" + "path/filepath" + "strings" + "testing" +) + +// Each day's usage and active rows are held at their own length: every +// range the overview keeps holds them, and rows grown by append kept the +// slack of each doubling (#2278). Five folders on one day grow to eight. +func TestStatsDayRowsHoldNoSlack(t *testing.T) { + inZone(t, 0) + claude, _ := setup(t) + for i, dir := range []string{"a", "b", "c", "d", "e"} { + id := "4444444" + itoa(i) + "-2222-3333-4444-555555555555" + path := filepath.Join(claude, "projects", "-work-"+dir, id+".jsonl") + os.MkdirAll(filepath.Dir(path), 0o755) + line := ccMsg("m"+dir, "2026-09-27T10:00:00.000Z", 100, 10) + ccMsg("n"+dir, "2026-09-27T10:01:00.000Z", 100, 10) + line = strings.ReplaceAll(line, "/work/night", "/work/"+dir) + line = strings.ReplaceAll(line, "33333333-2222-3333-4444-555555555555", id) + os.WriteFile(path, []byte(line), 0o644) + } + s := statsAt(0, statsNow) + found := false + for _, d := range s.Days { + if d.Date != "2026-09-27" { + continue + } + found = true + if len(d.Usage) < 5 || len(d.Active) < 5 { + t.Fatalf("the 27th has %d usage and %d active rows, want 5 of each", len(d.Usage), len(d.Active)) + } + if cap(d.Usage) != len(d.Usage) || cap(d.Active) != len(d.Active) { + t.Fatalf("usage %d of %d, active %d of %d", len(d.Usage), cap(d.Usage), len(d.Active), cap(d.Active)) + } + } + if !found { + t.Fatal("no 2026-09-27 in the stats") + } +}