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.
88 lines
3.0 KiB
Go
88 lines
3.0 KiB
Go
package sessions
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"runtime"
|
|
"testing"
|
|
)
|
|
|
|
// TestCCListingKeepsItsNames: each Sessions poll made every Claude Code
|
|
// file's path and key again, ~10 MB of a poll over ~15k sessions, though
|
|
// the listing is the same but for a file or two. A listing like the last
|
|
// makes none of them; a file new, gone or moved since is listed as a
|
|
// fresh listing lists it.
|
|
func TestCCListingKeepsItsNames(t *testing.T) {
|
|
dir := t.TempDir()
|
|
write := func(rel string) {
|
|
t.Helper()
|
|
p := filepath.Join(dir, "projects", rel)
|
|
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(p, []byte("{}\n"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
const n = 3000
|
|
for i := range n {
|
|
write(fmt.Sprintf("-Users-someone-work-a-project-p%d/%08d-1111-2222-3333-444444444444.jsonl", i%3, i))
|
|
}
|
|
// a session's folder, sorted between files, with its subagents
|
|
write("-Users-someone-work-a-project-p1/00000500-aaaa/subagents/agent-1.jsonl")
|
|
write("-Users-someone-work-a-project-p1/00000500-aaaa/subagents/workflows/run-1/agent-2.jsonl")
|
|
write("-Users-someone-work-a-project-p1/00000500-aaaa/subagents/workflows/run-1/note.jsonl")
|
|
ccListed.Clear()
|
|
t.Cleanup(func() { ccListed.Clear() })
|
|
fresh := func() []file {
|
|
ccListed.Clear()
|
|
return listCCFiles("claude", dir)
|
|
}
|
|
same := func(what string, got []file) {
|
|
t.Helper()
|
|
want := fresh()
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Fatalf("%s: %d files, a fresh listing %d", what, len(got), len(want))
|
|
}
|
|
}
|
|
|
|
first := listCCFiles("claude", dir)
|
|
if len(first) != n+2 {
|
|
t.Fatalf("listed %d files, want %d", len(first), n+2)
|
|
}
|
|
runtime.GC()
|
|
var before, after runtime.MemStats
|
|
runtime.ReadMemStats(&before)
|
|
again := listCCFiles("claude", dir)
|
|
runtime.ReadMemStats(&after)
|
|
if !reflect.DeepEqual(again, first) {
|
|
t.Fatal("an unchanged folder listed otherwise")
|
|
}
|
|
// a path and a key a file made again are 2n more
|
|
if m := after.Mallocs - before.Mallocs; m > 3*n {
|
|
t.Fatalf("listing %d unchanged files took %d allocations", n, m)
|
|
}
|
|
|
|
write("-p0/00000001-new.jsonl")
|
|
same("a file new", listCCFiles("claude", dir))
|
|
listCCFiles("claude", dir)
|
|
os.Remove(filepath.Join(dir, "projects", "-Users-someone-work-a-project-p0", "00000003-1111-2222-3333-444444444444.jsonl"))
|
|
os.Remove(filepath.Join(dir, "projects", "-Users-someone-work-a-project-p2", "00000005-1111-2222-3333-444444444444.jsonl"))
|
|
os.Remove(filepath.Join(dir, "projects", "-Users-someone-work-a-project-p2", "00000008-1111-2222-3333-444444444444.jsonl"))
|
|
same("files gone", listCCFiles("claude", dir))
|
|
listCCFiles("claude", dir)
|
|
os.RemoveAll(filepath.Join(dir, "projects", "-Users-someone-work-a-project-p1"))
|
|
write("-p9/00000500-aaaa/subagents/agent-1.jsonl")
|
|
same("a folder gone and one new", listCCFiles("claude", dir))
|
|
listCCFiles("claude", dir)
|
|
// the same names under another agent's key
|
|
got := listCCFiles("qoder", dir)
|
|
for _, f := range got {
|
|
if f.key[:6] != "qoder:" {
|
|
t.Fatalf("key %q under qoder", f.key)
|
|
}
|
|
}
|
|
}
|