Commit Graph
4 Commits
Author SHA1 Message Date
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
yetone 936bdccfab gui: the page reads hand memory back only after 30s at rest, past every poll (#2278)
An open Usage or Sessions window polls every 10s and 15s. With a 3s rest,
every poll's pages were handed back (ReleaseScratch + FreeOSMemory) and
taken back by the next poll: in the CPU profile, 1.88s of 1.95s madvise
was runtime.sysUsed re-committing them, and the matcher's scratch was
made again on every poll. 30s outlasts both polls, so the pages go back
once a window stops polling, not between an open window's polls.

Measured on mote (~/r2278 run2278d: 10 endpoints every 5s, gateway feed
from 60s, real ~/.codex and ~/.claude copy), 60s CPU profile, 3s vs 30s:
  GC mark workers  6.92s -> 4.76s
  madvise          1.81s -> 1.18s
  avg RSS after 60s  893 -> 925 MiB (+32 MiB while polling)
Latency was not comparable (mote load 40-73 during the run).
The binary measured was this constant at f193c3865. Rebased onto
91a2f603c (a masking commit and a .cjs GUI test only), and the vet and
test below were run again after it.

Run at the pushed head on mote: GOOS=linux/windows/darwin go vet -tags
nogui ./internal/gui/ ok. go test ./internal/gui/ red only in
TestMainWindowCorners/macOS_26_SDK, the open #3949.
Not tried: an open window left polling for an hour (RSS stays at the
poll's working set by design).
2026-10-29 07:58:26 +08:00
yetone 49d4d0f7ab usage: the request matcher keeps its maps for the next call, and gives them back once the window rests (#2278)
Every poll while an agent works matches a year's history again:
matchedBlocks and codexChunkResponsePairs grew their maps (byID,
reserved, used, groupOf, picks, picked, ...) and slices from nothing on
each call. They were ~3.0 GB of the ~40 GB a poll-heavy 80 s allocated
on the real-history harness (d1951 heap diff, 120 s to 200 s).

A sync.Pool was tried first and kept nothing in real use: under
tightenGC a poll collects 4-5 times a second (1029 GCs in 230 s), each
collection empties the pool, and the matcher's flat allocation went
only from 2241 to 1840 MB. So one scratch of each is kept in an
atomic.Pointer, emptied after a call (clear() keeps the maps' buckets,
and drops every chunk the call saw), and let go by usage.ReleaseScratch,
which releaseIfPagesRest calls before FreeOSMemory: once the window's
reads have rested 3 s, what the matching kept goes back with the rest.
A call that finds the scratch taken makes its own, sized as before.
matchedBlocks' result map is now made once the id matches are known,
not grown from the response pairs alone.

Measured on mote (Apple M5 Pro), at aeacc6358 + this change:
- BenchmarkPairRows (2000 files x 100 calls), -count 3: 100.8 MB/op,
  7,056 allocs/op at aeacc6358; 48.4 MB/op, 5,458 allocs/op after.
- Real history (copy of the user's usage.jsonl, sandbox HOME, polls +
  appended calls, run2278d.sh 230 s), heap alloc 120 s to 200 s:
  matchedBlocks flat 2241 -> 1295 MB, codexChunkResponsePairs flat
  806 -> 143 MB, of a total about the same (39.8 vs 39.3 GB). That run
  was before the result-map change. Its CPU and peak RSS (974 MiB vs
  936) aren't comparable: mote's load went from ~25 to 50-88 from other
  sessions during it.

Tests:
- New TestPairScratchKeepsNothingBetweenCalls (pairRows on one history
  after another matches what a first call does; ReleaseScratch lets go)
  and TestMatchScratchForgetsTheLastCall (a gateway call one call used by
  id or reserved by a response pair is matched in the next; ids the last
  call looked up in another gateway aren't).
- Broken by hand, each red: without clear of byID (index out of range),
  used ("the last call's id match held the gateway's call"), reserved
  ("the last call's response pair held"), groupOf and picks (index out
  of range). ids, named and picked only choose which chunks are opened,
  so a stale entry there costs a read, not a match.
- go test -count=1 -timeout 45m ./internal/usage/ ./internal/usageapi/
  ./internal/sessions/ ./internal/gui/ at aeacc6358 + this change: usage,
  usageapi, sessions ok; gui red in TestMainWindowCorners/macOS_26_SDK
  alone, mote's macOS 27 (#3949). An earlier gui run there had ten
  AppKit window tests killed at their 20-40 s deadlines at load ~40-88
  (#2645).
- GOOS=linux, windows, darwin go vet -tags nogui ./internal/usage/
  ./internal/gui/ ./internal/usageapi/: ok.

Rebased onto origin/main fe05f0a72 (library and Claude-account changes,
nothing in internal/usage or page_memory.go); after the rebase
./internal/usage/ and ./internal/usageapi/ ran again at the pushed
tree: ok, and go build . ok.
2026-10-29 04:33:14 +08:00
yetone 94a70f1a0d gui: what a Sessions or Usage poll threw away goes back once the page rests
An open window's Sessions and Usage polls allocate ~330 MB every 15 s to
keep ~105 MB, and Go's scavenger hands the freed pages back only slowly,
never while the next collection's goal covers them: between polls the
footprint sat at 336-392 MB. The gateway already hands its requests'
pages back once it rests (#3472), but the window's reads go through the
GUI's handler, not the gateway's, so nothing ever did that for them.

held now counts the /api/sessions and /api/usage reads (readingPage),
and once none has been in flight for 3 s (a poll's reads come within a
second of one another) it calls debug.FreeOSMemory, as the warm-up does
after its reads.

Measured on a copy of the owner's real history (rss-home, 3581/3582,
8 GUI-like poll cycles 15 s apart, footprint 10 s into each rest), at
c2c08850f vs this change:
- footprint at rest: 336-392 MB -> 164-193 MB
- footprint right after a poll: 336-392 MB -> 181-276 MB (358 on the
  first)
- process CPU over the 8 cycles: 6.55 s -> 6.27 s
- /api/sessions mean 256 -> 305 ms, /api/usage/requests 232 -> 154 ms
  (one run each, the live history changing under them)

Tests: TestPageReadsReleasedAtRest (no release while a read is in
flight, one when the last ends, a cut-short rest waited out again) and
TestHeldCountsPageReads (sessions and usage counted, /api/state not).
Broken by hand: without the in-flight check the first fails ("released
while a read was in flight"); without readingPage in held the second
fails for both paths.

Run at this head (rebased on 8fba65d0b): go test ./internal/gui/ ok (94s); the two new tests
-count=20 ok; gofmt clean; GOOS=linux and GOOS=windows go vet -tags
nogui ./internal/gui/ ok.
Not run: the GUI JS suite (no script changed), the full ./... suite.
2026-10-27 08:29:11 +08:00