MergeExternalStats grew a copy of every native Summary to append the gateway-only sessions, and cloned them all again, with their days, when the gateway's history began before the session files. Each range the Sessions page keeps (7, 30, every day) held that copy beside the Summaries it was copied from: ~10 MB over the full history, made again on every 10 s refresh. Stats now keeps the gateway's Summaries in their own slice, sorted by Last, and a day shift instead of rewritten days. Overview walks both in the order the merged list had (the most recent first, a native session first of two as recent), adds the shift to each day, gives toolUse the shifted first day, and shifts the Top copies it hands out. AllSessions gives the merged list as one copy for the tests that read it. Overview is the only reader of the merged sessions outside tests: aliasStats and keepDesktop run before the merge, and the stats answer doesn't encode Sessions. Run on mote at86e19b9b7plus this change: - go vet ./internal/sessions ./internal/gui ./internal/cli: ok; GOOS=linux and GOOS=windows go vet ./internal/sessions: ok. - go test ./internal/sessions ./internal/cli: ok. - go test ./internal/gui: ok except TestCoveredWindowStaysUp, killed at 30s. It fails alone too, at load ~21. #4125 covers it: on mote every AppKit test hangs while launchd throttles it. This change doesn't touch window code. - TestGatewayStatsMergedBesideNative (new) goes red three ways by hand. With Sessions cloned in the merge: "the native sessions were copied". With a gateway session first of two as recent: "merged [g2 a g1 b]". With Top and toolUse unshifted: the overview differs from a merged list's. Real history on mote (advov.sh, the full-history HOME, v0.1.2287 = rel2 against this change): - cold peak phys_footprint: 355/352/362 MB against 358/355/358 (noise: the cold peak is the requests warm-up, not this); - warm start: 249/262 MB against 258/274; - /api/sessions/overview and /api/sessions/stats for days 0, 7 and 30 are byte-identical between the two builds in both warm runs. Rebased on origin/main after the run. The 9 commits it brought in (gateway, usage window_holds, gui Long conversations, workbuddy min) touch nothing under internal/sessions. After the rebase, internal/sessions and the gui gateway-session tests were run again (below). - At the rebased head onc272e5582: go vet ./internal/sessions ./internal/gui ok; go test ./internal/sessions ok; go test ./internal/gui -run 'Gateway|Stats|Overview|Desktop|Budget' ok.
61 lines
3.2 KiB
Go
61 lines
3.2 KiB
Go
package sessions
|
|
|
|
import (
|
|
"reflect"
|
|
"slices"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
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: []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}) {
|
|
t.Fatalf("native calendar shifted when gateway history extended range: days=%v output=%v", view.Days, view.Output)
|
|
}
|
|
if native.Sessions[0].Days[0] != 0 || native.Sessions[0].perDay[0].output != 4 {
|
|
t.Fatal("merge mutated the original native snapshot")
|
|
}
|
|
}
|
|
|
|
// The gateway's sessions are merged beside the native ones, not into a copy
|
|
// of them (#2278), and the overview reads them in the order a merged list
|
|
// had: the most recently active first, a native one first of two as
|
|
// recent, every day in the range's calendar.
|
|
func TestGatewayStatsMergedBesideNative(t *testing.T) {
|
|
at := func(s int64) time.Time { return time.Unix(1_790_000_000+s, 0) }
|
|
native := Stats{From: "2026-10-06", To: "2026-10-08", Sessions: []Summary{
|
|
{Key: "claude:a", Agent: "claude", ID: "a", Last: at(30), Tokens: Tokens{Output: 5}, Days: []int{0, 2}, ToolCalls: 2, tools: counts{{name: "Read", n: 2}},
|
|
perDay: []summaryDay{{day: 0, output: 2, tools: counts{{name: "Read", n: 1}}}, {day: 2, output: 3, tools: counts{{name: "Read", n: 1}}}}},
|
|
{Key: "claude:b", Agent: "claude", ID: "b", Last: at(10), Tokens: Tokens{Output: 5}, Days: []int{1}, perDay: []summaryDay{{day: 1, output: 5}}},
|
|
}}
|
|
merged := MergeExternalStats(native, []ExternalDayUsage{{Date: "2026-10-04", Agent: "opencode", Tokens: Tokens{Output: 5}}}, []ExternalSession{
|
|
{Agent: "opencode", ID: "g1", Last: at(10), Tokens: Tokens{Output: 5}, Daily: []ExternalDayUsage{{Date: "2026-10-04", Tokens: Tokens{Output: 5}}}},
|
|
{Agent: "opencode", ID: "g2", Last: at(40), Tokens: Tokens{Output: 5}, Daily: []ExternalDayUsage{{Date: "2026-10-08", Tokens: Tokens{Output: 5}}}},
|
|
})
|
|
if &merged.Sessions[0] != &native.Sessions[0] {
|
|
t.Fatal("the native sessions were copied to merge the gateway's")
|
|
}
|
|
all := merged.AllSessions()
|
|
var order []string
|
|
for _, s := range all {
|
|
order = append(order, s.ID)
|
|
}
|
|
if !slices.Equal(order, []string{"g2", "a", "b", "g1"}) || !slices.Equal(all[1].Days, []int{2, 4}) || !slices.Equal(all[3].Days, []int{0}) {
|
|
t.Fatalf("merged %v, a's days %v, g1's %v", order, all[1].Days, all[3].Days)
|
|
}
|
|
if native.Sessions[0].Days[0] != 0 || native.Sessions[0].perDay[1].day != 2 {
|
|
t.Fatal("merge wrote the native sessions' days")
|
|
}
|
|
got := merged.Overview("", "", "", 8)
|
|
if !reflect.DeepEqual(got.Days, []int{1, 0, 1, 1, 2}) || !reflect.DeepEqual(got.Output, []int{5, 0, 2, 5, 8}) {
|
|
t.Fatalf("calendar %v, output %v", got.Days, got.Output)
|
|
}
|
|
// the same as a list made merged
|
|
want := Stats{From: merged.From, To: merged.To, Days: merged.Days, Sessions: all}.Overview("", "", "", 8)
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Fatalf("overview %+v\nmerged list's %+v", got, want)
|
|
}
|
|
}
|