Stats.Overview copied every Summary (~400 bytes) into its working slice, then cloned that slice once per Top ranking. Over 34k sessions the Sessions page's poll allocated ~1.5 GB in two minutes this way while an agent worked (alloc profile of v0.1.1928 on mote: `in = append` 958 MB, slices.Clone 557 MB). It now ranks pointers into st.Sessions and copies only the k sessions each ranking keeps. shapes, toolUse and skillUse take the pointers. TestOverviewCopiesOnlyItsTop: a second Overview over 5000 sessions allocates at most half of their Summaries' size, and its count and rankings are what they were. Red on the old code (14,923,144 bytes over a 1,020,000 bar), green on the new. A/B on mote (34,040 session files, 240k-row usage.jsonl, the feed run of run2278c.sh: 230s, feed from 60s, Usage and Sessions pages polled), v0.1.1928 code vs this change. mote's load was 47 to 66 from other runs throughout, so the numbers are noisy: avg CPU after the feed starts 122.8% -> 69.4% heatmap poll mean 6.97s -> 4.84s all polls mean 3.11s -> 2.16s footprint peak 685.6M -> 626.1M footprint at the end 402.8M -> 337.5M Run on mote, at baseb5d2858f4: go test -count=1 -timeout 45m ./internal/sessions/ ./internal/usage/ ok (30.9s, 18.0s) go test ./internal/gui/: FAIL only in TestMainWindowCorners, the open macOS 27 red #3949 Rebased onto origin/main7a7931833(no change to internal/sessions), and run again at the pushed head on mote: go test -count=1 -timeout 45m ./internal/sessions/ ./internal/usage/ ok (16.8s, 16.1s) GOOS=linux/windows/darwin go vet -tags nogui ./internal/sessions/ ./internal/usage/ ./internal/gui/ ok
130 lines
6.7 KiB
Go
130 lines
6.7 KiB
Go
package sessions
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
// The messages each way, the tools called and the skills called up are
|
|
// counted from Claude Code's and Codex's files, read fast and read whole
|
|
// alike, and the overview sums them up: the shape of the sessions, the
|
|
// tools by category and week, and the skills.
|
|
func TestStatsToolsAndSkills(t *testing.T) {
|
|
inZone(t, 0)
|
|
claude, codex := setup(t)
|
|
id := "44444444-2222-3333-4444-555555555555"
|
|
where := `,"cwd":"/work/tools","sessionId":"` + id + `"}` + "\n"
|
|
user := func(at, content string) string {
|
|
return `{"parentUuid":null,"isSidechain":false,"type":"user","message":{"role":"user","content":` + content + `},"timestamp":"` + at + `"` + where
|
|
}
|
|
reply := func(msg, at, content string) string {
|
|
return `{"parentUuid":null,"isSidechain":false,"message":{"model":"claude-opus-5-5","id":"` + msg + `","type":"message","role":"assistant","content":` + content +
|
|
`,"usage":{"input_tokens":10,"output_tokens":5}},"type":"assistant","timestamp":"` + at + `"` + where
|
|
}
|
|
lines := user("2026-09-27T10:00:00.000Z", `"fix the build"`) +
|
|
reply("m1", "2026-09-27T10:00:05.000Z", `[{"type":"text","text":"on it"}]`) +
|
|
// the same message's next blocks: two tool calls, one reply
|
|
reply("m1", "2026-09-27T10:00:06.000Z", `[{"type":"tool_use","id":"toolu_1","name":"Bash","input":{"command":"go build"}}]`) +
|
|
reply("m1", "2026-09-27T10:00:07.000Z", `[{"type":"tool_use","id":"toolu_2","name":"Skill","input":{"skill":"artifact-design"},"caller":{"type":"direct"}}]`) +
|
|
user("2026-09-27T10:00:08.000Z", `[{"tool_use_id":"toolu_1","type":"tool_result","content":"ok"}]`) +
|
|
// laid out otherwise: read whole
|
|
`{"type":"assistant","message":{"role":"assistant","id":"m2","model":"claude-opus-5-5","content":[{"type":"tool_use","name":"mcp__slack__send","id":"toolu_3","input":{}},{"type":"tool_use","name":"Edit","id":"toolu_4","input":{}}],"usage":{"input_tokens":1,"output_tokens":1}},"timestamp":"2026-09-27T10:00:09.000Z"` + where +
|
|
user("2026-09-27T10:01:00.000Z", `[{"type":"text","text":"<command-name>/clear</command-name>"}]`) +
|
|
user("2026-09-27T10:02:00.000Z", `"[Request interrupted by user]"`)
|
|
path := filepath.Join(claude, "projects", "-work-tools", id+".jsonl")
|
|
os.MkdirAll(filepath.Dir(path), 0o755)
|
|
os.WriteFile(path, []byte(lines), 0o644)
|
|
// a subagent's tool calls count, its messages not
|
|
sub := filepath.Join(claude, "projects", "-work-tools", id, "subagents", "agent-a1.jsonl")
|
|
os.MkdirAll(filepath.Dir(sub), 0o755)
|
|
os.WriteFile(sub, []byte(user("2026-09-27T10:00:10.000Z", `"look around"`)+
|
|
reply("s1", "2026-09-27T10:00:11.000Z", `[{"type":"tool_use","id":"toolu_5","name":"Grep","input":{}}]`)), 0o644)
|
|
|
|
cx := filepath.Join(codex, "sessions", "2026", "09", "27", "rollout-2026-09-27T10-00-00-55555555-2222-3333-4444-555555555555.jsonl")
|
|
os.MkdirAll(filepath.Dir(cx), 0o755)
|
|
os.WriteFile(cx, []byte(
|
|
`{"timestamp":"2026-09-27T10:00:00.000Z","type":"session_meta","payload":{"id":"55555555-2222-3333-4444-555555555555","cwd":"/work/cx"}}`+"\n"+
|
|
`{"timestamp":"2026-09-27T10:00:01.000Z","type":"response_item","payload":{"type":"message","role":"user","content":[{"type":"input_text","text":"<environment_context>x</environment_context>"}]}}`+"\n"+
|
|
`{"timestamp":"2026-09-27T10:00:02.000Z","type":"response_item","payload":{"type":"message","role":"user","content":[{"type":"input_text","text":"add a test"}]}}`+"\n"+
|
|
`{"timestamp":"2026-09-27T10:00:03.000Z","type":"response_item","payload":{"type":"function_call","id":"a","name":"exec_command","arguments":"{}","call_id":"c1"}}`+"\n"+
|
|
`{"timestamp":"2026-09-27T10:00:04.000Z","type":"response_item","payload":{"type":"custom_tool_call","status":"completed","call_id":"c2","name":"apply_patch","input":"x"}}`+"\n"+
|
|
`{"timestamp":"2026-09-27T10:00:05.000Z","type":"response_item","payload":{"type":"message","role":"assistant","content":[{"type":"output_text","text":"done"}]}}`+"\n"+
|
|
`{"timestamp":"2026-09-27T10:00:06.000Z","type":"response_item","payload":{"type":"message","role":"user","content":[{"type":"input_text","text":"thanks"}]}}`+"\n"), 0o644)
|
|
|
|
s := statsAt(0, statsNow)
|
|
var cc, cxs Summary
|
|
for _, x := range s.Sessions {
|
|
switch x.Cwd {
|
|
case "/work/tools":
|
|
cc = x
|
|
case "/work/cx":
|
|
cxs = x
|
|
}
|
|
}
|
|
// the prompt and /clear typed; m1 and m2 back; five calls
|
|
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.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.of("exec_command") != 1 || cxs.tools.of("apply_patch") != 1 {
|
|
t.Fatalf("codex: %+v %v", cxs, cxs.tools)
|
|
}
|
|
|
|
o := s.Overview("", "", "/work/tools", 10)
|
|
if o.Tools.Calls != 5 || o.Tools.Sessions != 1 || len(o.Tools.Top) != 5 {
|
|
t.Fatalf("tools %+v", o.Tools)
|
|
}
|
|
cats := map[string]int{}
|
|
for _, c := range o.Tools.Categories {
|
|
cats[c.Name] = c.Calls
|
|
}
|
|
if cats["Bash"] != 1 || cats["Edit"] != 1 || cats["Grep"] != 1 || cats["Tool"] != 1 || cats["Other"] != 1 {
|
|
t.Fatalf("categories %v", cats)
|
|
}
|
|
if len(o.Tools.Weeks) != 1 || o.Tools.Weeks[0].Start != "2026-09-21" || o.Tools.Weeks[0].Calls["Bash"] != 1 {
|
|
t.Fatalf("weeks %+v", o.Tools.Weeks)
|
|
}
|
|
if o.Skills.Calls != 1 || o.Skills.Count != 1 || o.Skills.Top[0].Name != "artifact-design" || o.Skills.Top[0].Last != "2026-09-27" ||
|
|
o.Skills.Top[0].Agents["claude"] != 1 || o.Skills.Top[0].Projects[0].Name != "/work/tools" {
|
|
t.Fatalf("skills %+v", o.Skills)
|
|
}
|
|
// 4 messages, 2 minutes' work, 5 calls in 2 prompts
|
|
if sh := o.Shape["messages"]; sh.Total != 1 || sh.Counts[0] != 1 {
|
|
t.Fatalf("shape by messages %+v", sh)
|
|
}
|
|
if sh := o.Shape["autonomy"]; sh.Counts[1] != 1 {
|
|
t.Fatalf("shape by autonomy %+v", sh)
|
|
}
|
|
day := -1
|
|
for i := range o.Days {
|
|
if o.Messages[i] > 0 {
|
|
day = i
|
|
}
|
|
}
|
|
if day < 0 || o.Messages[day] != 4 || o.Output[day] != 5+1+5 { // the subagent's reply too
|
|
t.Fatalf("messages %v output %v", o.Messages, o.Output)
|
|
}
|
|
}
|
|
|
|
// TestShapesMinutesWithoutMessages: a session whose agent tells no messages
|
|
// (pi, grok…) still counts by its minutes at work, and only there.
|
|
func TestShapesMinutesWithoutMessages(t *testing.T) {
|
|
sh := shapes([]*Summary{
|
|
{Agent: "claude", Prompts: 2, Replies: 3, ToolCalls: 4, Active: 600},
|
|
{Agent: "pi", Active: 1200},
|
|
{Agent: "grok"},
|
|
})
|
|
if m := sh["minutes"]; m.Total != 2 || m.Counts[1] != 1 || m.Counts[2] != 1 {
|
|
t.Fatalf("minutes %+v", m)
|
|
}
|
|
if m := sh["messages"]; m.Total != 1 || m.Counts[0] != 1 {
|
|
t.Fatalf("messages %+v", m)
|
|
}
|
|
if a := sh["autonomy"]; a.Total != 1 || a.Counts[1] != 1 {
|
|
t.Fatalf("autonomy %+v", a)
|
|
}
|
|
}
|