VeryBTC on X (tweet 2117104179454091396) reported RSS that rises and
stays up while the window is open and a long-context session is active.
In that case every Sessions poll sees a changed file and rebuilds the
list. That rebuild copied every gateway-only session three times
(GatewaySessions, listedGateway, gatewayListed) and made each one a
Session row before EachMerged stopped at its 5000-row limit. It also
made every Claude Code file's path and key again.
- usage.EachGatewaySession walks the projection without copying it.
gui's listedExternal stops at the list's limit and skips hidden
sessions and sessions without an agent or id, as listedGateway did.
- sessions.EachMerged sorts indexes and makes a row only when the merge
reaches it. The order is unchanged: newest first, stable on ties.
- listCCFiles reuses the last listing's path and key strings while a
folder lists the same names. One file new or gone is stepped over.
This holds ~32 B per file (~480 KB at 15k files), and the strings are
shared with the listing it already held.
- page_memory.go's ~330 MB comment now has current numbers.
gateway-sessions.md describes the lazy rows.
Per poll, on mote (go test probe through the real /api/sessions
handler, TotalAlloc):
15k sessions x 10 turns, 400k ledger rows
after a session file grows 150.6 MB, 201 ms -> 109.9 MB, 170 ms
unchanged (warm) 17.2 MB -> 12.0 MB
3k sessions x 40 turns, 150k rows (no gateway-only rows past the cap)
after a session file grows 31.3 MB -> 29.6 MB
unchanged (warm) 4.5 MB -> 3.4 MB
/api/usage is unchanged at 0.4 MB.
Tests:
- TestSessionsListMakesOnlyTheGatewayRowsItShows (gui) uses 3 natives,
6000 gateway sessions in tied pairs, one hidden and one carrying a
native's id. At limits 5, 40 and 6010, and with no limit, the rows
equal the old pipeline's (listedGateway + gatewayListed +
MergeExternal) after the JSON round trip. A grown native comes first
with its new Last. The poll's allocation stays under 6000 rows' size.
- TestEachMergedMakesOnlyTheRowsItReaches (sessions): 15000 externals
and 20 rows give MergeExternal's first 20, with allocation under a
tenth of the rows.
- TestCCListingKeepsItsNames (sessions): a second listing of 3000 files
is equal to the first, in at most 3n mallocs. A file added, files
removed, and a folder gone with another new each list as a fresh
listing does. Another agent's keys keep their own prefix.
Broken by hand, each red:
- EachMerged building every row first: 8.8 MB, the sessions test fails.
- The GUI handler back on the listedGateway(GatewaySessions) loop, with
the lazy merge kept: 13.7 MB, the gui test fails.
- The gui test on origin/main's code: 23 MB, fails.
- The ccNames reuse turned off: 12197 mallocs, the listing test fails.
Run on mote (mote-run) at 73a1b8769, rebased on origin/main 07f4d46c5:
gofmt -l internal (empty), go vet ./..., GOOS=linux go vet -tags nogui
./..., GOOS=windows go vet ./... all clean. go test -count=1 -timeout
45m ./internal/sessions ./internal/usage ./internal/gui: sessions ok,
usage ok. gui is ok except TestMainWindowCorners/macOS_26_SDK, which is
#3949 (red on mote's macOS 27, an AppKit title bar test this change
doesn't reach). It was red the same way in the run before the rebase.
Not tried:
- A real app window with RSS measured over time. The numbers are
allocation per poll from a probe.
- The rest of a changed poll: statsKept re-sums every session (~26 MB at
15k), listing (~24 MB) and byKey (~9 MB). None was a cheap, safe
change.
- Windows and Linux test runs (vet only).
- A gateway title store in the fixture. GatewayTitler's ReadDirs now run
only for the rows shown.
115 lines
4.3 KiB
Go
115 lines
4.3 KiB
Go
package sessions
|
|
|
|
import (
|
|
"fmt"
|
|
"reflect"
|
|
"runtime"
|
|
"testing"
|
|
"time"
|
|
"unsafe"
|
|
)
|
|
|
|
// TestEachMergedListsWhatMergeExternalDid: the Sessions page makes its rows
|
|
// as the list and the gateway's sessions merge, in MergeExternal's order:
|
|
// newest first, a native before an external of the same moment, and a list
|
|
// not newest first sorted before. Its external are the gateway's sessions
|
|
// native doesn't hold (GatewaySessions), so a native's id isn't among them.
|
|
func TestEachMergedListsWhatMergeExternalDid(t *testing.T) {
|
|
at := time.Date(2026, 11, 1, 12, 0, 0, 0, time.UTC)
|
|
native := []Session{
|
|
{Agent: "claude", ID: "c1", Last: at.Add(5 * time.Minute), Copies: []string{"c1-copy"}},
|
|
{Agent: "codex", ID: "x1", Last: at.Add(3 * time.Minute)},
|
|
{Agent: "claude", ID: "c2", Last: at.Add(3 * time.Minute)},
|
|
{Agent: "pi", ID: "p1", Last: at},
|
|
}
|
|
external := []ExternalSession{
|
|
{Agent: "opencode", ID: "g-old", Last: at.Add(-time.Hour)},
|
|
{Agent: "opencode", ID: "g-tie", Last: at.Add(3 * time.Minute)},
|
|
{Agent: "opencode", ID: "g-new", Last: at.Add(time.Hour)},
|
|
{Agent: "", ID: "nobody", Last: at},
|
|
{Agent: "opencode", ID: "g-tie2", Last: at.Add(3 * time.Minute)},
|
|
}
|
|
unsorted := []Session{native[2], native[0], native[3], native[1]}
|
|
for _, in := range [][]Session{native, unsorted, nil} {
|
|
for _, ext := range [][]ExternalSession{external, nil} {
|
|
want := MergeExternal(in, ext)
|
|
var got []Session
|
|
EachMerged(in, ext, func(s *Session) bool { got = append(got, *s); return true })
|
|
if !reflect.DeepEqual(mergedIDs(got), mergedIDs(want)) || len(got) != len(want) {
|
|
t.Fatalf("merged\n got %v\nwant %v", mergedIDs(got), mergedIDs(want))
|
|
}
|
|
for i := range want {
|
|
if !reflect.DeepEqual(got[i], want[i]) {
|
|
t.Fatalf("row %d\n got %+v\nwant %+v", i, got[i], want[i])
|
|
}
|
|
}
|
|
if len(want) > 2 {
|
|
n := 0
|
|
EachMerged(in, ext, func(*Session) bool { n++; return n < 2 })
|
|
if n != 2 {
|
|
t.Fatalf("went on after false: %d calls", n)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if fmt.Sprint(mergedIDs(unsorted)) != "[claude:c2 claude:c1 pi:p1 codex:x1]" {
|
|
t.Fatalf("the caller's list was sorted in place: %v", mergedIDs(unsorted))
|
|
}
|
|
}
|
|
|
|
// TestEachMergedCopiesNoList: a poll's merge was a copy of every session.
|
|
func TestEachMergedCopiesNoList(t *testing.T) {
|
|
native := make([]Session, 5000)
|
|
at := time.Now()
|
|
for i := range native {
|
|
native[i] = Session{Agent: "claude", ID: fmt.Sprint("s", i), Last: at.Add(-time.Duration(i) * time.Second)}
|
|
}
|
|
ext := []ExternalSession{{Agent: "opencode", ID: "g", Last: at.Add(-time.Hour)}}
|
|
list := uint64(len(native)) * uint64(unsafe.Sizeof(Session{}))
|
|
var before, after runtime.MemStats
|
|
runtime.ReadMemStats(&before)
|
|
n := 0
|
|
EachMerged(native, ext, func(*Session) bool { n++; return true })
|
|
runtime.ReadMemStats(&after)
|
|
if n != len(native)+1 {
|
|
t.Fatalf("%d sessions, want %d", n, len(native)+1)
|
|
}
|
|
if got := after.TotalAlloc - before.TotalAlloc; got > list/20 {
|
|
t.Fatalf("merging a %d-byte list allocated %d bytes", list, got)
|
|
}
|
|
}
|
|
|
|
func mergedIDs(list []Session) []string {
|
|
var out []string
|
|
for _, s := range list {
|
|
out = append(out, s.Agent+":"+s.ID)
|
|
}
|
|
return out
|
|
}
|
|
|
|
// TestEachMergedMakesOnlyTheRowsItReaches: every gateway session was made
|
|
// into a row before the merge, ~22 MB of each poll over ~15k sessions
|
|
// while one of them grew, though the page stops at sessions.All rows.
|
|
func TestEachMergedMakesOnlyTheRowsItReaches(t *testing.T) {
|
|
at := time.Now()
|
|
ext := make([]ExternalSession, 15000)
|
|
for i := range ext {
|
|
// oldest first, so the order is the merge's to make
|
|
ext[i] = ExternalSession{Agent: "opencode", ID: fmt.Sprint("g", i), Last: at.Add(time.Duration(i-len(ext)) * time.Second),
|
|
Models: []ExternalModel{{Model: "m"}}}
|
|
}
|
|
native := []Session{{Agent: "claude", ID: "c", Last: at.Add(-10 * time.Second)}}
|
|
var before, after runtime.MemStats
|
|
runtime.ReadMemStats(&before)
|
|
var got []Session
|
|
EachMerged(native, ext, func(s *Session) bool { got = append(got, *s); return len(got) < 20 })
|
|
runtime.ReadMemStats(&after)
|
|
if want := MergeExternal(native, ext)[:20]; !reflect.DeepEqual(got, want) {
|
|
t.Fatalf("rows\n got %v\nwant %v", mergedIDs(got), mergedIDs(want))
|
|
}
|
|
rows := uint64(len(ext)) * uint64(unsafe.Sizeof(Session{}))
|
|
if a := after.TotalAlloc - before.TotalAlloc; a > rows/10 {
|
|
t.Fatalf("20 rows of %d gateway sessions allocated %d bytes; every row is %d", len(ext), a, rows)
|
|
}
|
|
}
|