Commit Graph
2 Commits
Author SHA1 Message Date
yetone 83137bcbe9 sessions: TestCCListingKeepsItsNames counts the names' allocations, not Linux's stats (#4925)
Red on ubuntu CI at 4a9cfb52e (run 41865226839) and on the Linux box
alone: "listing 3000 unchanged files took 12159 allocations", bar 3n =
9000.

Cause: dirent.Stats reads a folder's stats in one call on macOS
(getattrlistbulk) and has no such call elsewhere, so on Linux statFiles
stats each file alone through statOne (os.Stat), two allocations a
file: 6000 for the test's 3000 files. The names are kept the same on
both systems. A probe of the second listing (not committed) gave Linux
12115 allocations, 6110 with statOne answered without allocating, and
macOS 6128 and 6124; one os.Stat is 2 allocations on each. The 3n bar
was set on macOS and charged Linux's stats to the names.

Change (test only): the measured listing's statOne is answered from a
table of os.Stat read before it, so what is counted is the listing's
own work on every system. The bar is unchanged.

What was run, at this tree on origin/main fd85c0a1e:
- Linux (wsl-run, magpie-desktop, go1.26.8, -tags nogui): the old
  test red, 12159 allocations. The new test ok. The new test with
  ccNames.of never taking the last listing's names (broken by hand):
  red, 12159.
- macOS (mote): the new test ok; with the names broken by hand red,
  12198.
- go test -count=3 ./internal/sessions: ok on Linux (-tags nogui) and
  macOS. gofmt -l clean; go vet, GOOS=linux go vet -tags nogui,
  GOOS=windows go vet of internal/sessions: clean.
Not tried: Windows (no bulk stat there either, so it would have been
red the same way; Windows CI only compiles tests).
Not changed: Linux still pays os.Stat's two allocations a session file
each poll; that cost is the system's stat, not this test's guarantee.
2026-11-05 01:08:08 +08:00
yetone 1829cd52ff sessions: a poll while a session grows makes only the rows it shows
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.
2026-11-02 17:19:23 +08:00