Commit Graph
3 Commits
Author SHA1 Message Date
yetone 536a98316d sessions: a poll over unchanged files gets the stats the last one read (#2278)
The Usage page asks for its range every 10 s and the Sessions page for
7, 30 and every day every 15 s. Each read put every session of every
range together again, though between two polls the files mostly
haven't changed: statsReadAll allocated ~1.1 GB per 80 s of polling,
a fifth of all magpie allocated.

statsKept keeps each range's Stats (at most 8 ranges) and the native
keys, with what they were read from: every listed file's path, key,
agent, size, mod time, main/read-only, WSL distro and its parse pointer
(a published parse is never written; a changed file gets a new one),
today, and the cache generation. It also keeps the answers the read
got from the prices, Codex's thread names and Claude Desktop's names,
and asks them again before it hands anything out. When any of these
differ, everything is read again. The listing and the refresh of
changed files still run on every poll.

MergeExternalStats wrote into the Stats it was given (a day's usage
rows, an append into spare room in Sessions, sorts in place). It now
copies whatever it changes, so a kept range is never written.

What this doesn't help: while an agent is writing a session, every
poll sees a changed file and reads everything again. Reading only the
changed sessions is the next change.

Run at the pushed head on mote:
- GOOS=linux/windows/darwin go vet -tags nogui ./internal/sessions/
  ./internal/gui/: ok.
- go test -count=1 -timeout 45m ./internal/sessions/ ./internal/gui/
  ./internal/usage/: sessions ok, usage ok, gui red only
  TestMainWindowCorners/macOS_26_SDK (#3949, open, same as main).
- New tests: TestStatsKeptBetweenPolls (the same backing array on an
  unchanged poll; a grown file and a price change are read in),
  TestStatsKeptNewDay, TestStatsKeptRangesBounded,
  TestMergeExternalStatsWritesNoInput. TestStatsAllRangesOnePass now
  forgets the kept ranges before each single read.
- Broken by hand, each red: keptFor always missing
  (TestStatsKeptBetweenPolls, "put together again"); the price check
  dropped (repriced cost unchanged); MergeExternalStats without its
  copies (TestMergeExternalStatsWritesNoInput).
- Real data (a copy of the owner's history, ~34k sessions), v0.1.1978
  against this: /api/sessions/stats?days=0/7/30 and both overviews
  asked 4 times 12 s apart, each pair byte-identical (1.97 MB for
  days=0).
- A/B, 80 s of GUI polling, v0.1.1978 against this: allocated 5512 to
  4480 MB, statsReadAll's share 1108 to 261 MB (the listing);
  GC 3.90 to 3.42 s, madvise 1.05 to 0.61 s, CPU 28.6 to 25.3%, avg
  RSS 1061 to 1019 MiB. mote's load was 20-40, so latencies aren't
  compared. The A/B binary was built before the 8-range bound was added.
- Rebased onto b9f1fcddd (redact only), then again: vet on the three
  systems over sessions, gui and redact, and go test of sessions, usage
  and redact: ok.
- Rebased again onto a4e929212 (GUI context map only), then at this
  head: the same vet and tests ok. TestCoreScriptBudget is red at
  a4e929212 itself (#4133); this commit touches no GUI file.
2026-10-29 08:52:32 +08:00
yetone fa2dfc1458 sessions: the page's 7, 30 and all-time stats are read again in one pass (#2278)
statsFor keeps each ?days range the Usage page asks for and reads it
again behind the page once it is 10s old. Each of the three ranges
(7, 30, 0) was its own combinedSessionStats: a listing of every Claude
Code and Codex file, a refresh, the Codex identities and task titles,
every session assembled for the native keys, and the ccCopies, all
identical whatever the range. With 25k Codex rollouts that was ~4.2s of
CPU a minute on a polled page.

Now a stale range takes every other stale range with it, and
statsReadAll does the shared part once and sums each range from it
(statsOfRange). The sessions assembled for the native keys are reused
for each range's list instead of assembling them again.

Rebased onto b4267de59; the tests and vets below ran again on mote at
the rebased head, same results. The A/B ran before the rebase:
- TestStatsAllRangesOnePass (new): ranges 0, 1, 7, 30 from one pass
  are each DeepEqual to statsRead of that range alone, and the files
  are listed once.
- GOOS=linux/windows/darwin go vet -tags nogui ./internal/sessions
  ./internal/gui: clean.
- go test ./internal/sessions ./internal/usage: ok. ./internal/gui: ok
  but for TestMainWindowCorners/macOS_26_SDK, the macOS 27 red in #3949
  (titlebar, untouched here).
- A/B on mote, sandbox HOME with a copy of the real 115MB usage.jsonl
  (240k rows) and 25k Codex rollouts, the page's 10 reads every 5s and
  4 appends a second, 240s each, v0.1.1959 (d7e9c3745) then this:
    stats refresh (statsFor.func1) CPU  4.17s -> 2.22s a minute
    process CPU in the 60s profile      33.8s -> 30.7s
    stats?days=0/30/7 mean              0.76/0.77/0.78s -> 0.39/0.36/0.39s
    /api/sessions/overview mean         1.29s -> 0.72s
    /api/sessions mean                  2.01s -> 1.37s
    every read's mean                   0.99s -> 0.62s
    peak RSS                            985 -> 1033 MiB (mote at load
                                        ~35 from other sessions; peak is
                                        next)
2026-10-29 05:29:36 +08:00
yetone 81029fc63c sessions: the overview's stats and their native sessions come of one pass over the files (#2776)
combinedSessionStats, behind /api/sessions/stats and /overview (each
range read again in the background when its memo is over 10 s old, so
on every 15 s poll of the Sessions page), took sessions.StatsFor and then
sessions.List(All) only for the agent|id set that keeps a gateway session
the agent keeps too from counting twice: two passes over every agent's
files, each refreshing them all. sessions.StatsAndNative gives both from
StatsFor's one pass: the same set, a session List would list (one
nothing was said in still left out, as before), but with no 5000 limit,
so a gateway session whose native one is among the oldest of a long
history is no longer counted again.

Test: TestStatsAndNativeOnePass: one pass over the files (filesHook, new),
and the same keys as List(All) on the fixtures. Red with StatsAndNative
written as the old pair of calls: "2 passes over the files".

Measured on the synthetic sandbox HOME (3000 Claude Code, 1500 Codex,
2000 Hermes sessions), combinedSessionStats warm after a restart, CPU
time (load average ~250, wall times noisy), parent 8e2b1dce6 against
this change, 4 rounds each:
  days=30  94-99 ms -> 57-62 ms CPU
  days=0  107-130 ms -> 68-74 ms CPU
  first after a restart (days=30)  558 -> 510 ms CPU

Run at this commit (the series' last; it covers 0179cef63, bbee24810
and 8e2b1dce6 too), on a machine at load average ~250-500:
- go test ./... -timeout 45m: 60 packages ok, sessions (869.8s), usage
  ok. Red only under that load, all green when run again:
  - internal/agent and internal/gateway timed out at 45m in tests that
    don't touch sessions (TestPiOffersItsOwnOpenAICodexModels,
    TestVisionSettingPicksTheDescriber). Run again with -timeout 90m:
    agent ok (1394.2s), gateway ok (1546.9s).
  - internal/gui TestSetAnswersWhileDevinAndProvidersHang: set took
    44.6s under load; it is #1375. Alone: ok.
  - internal/plugin TestGatewayHookMixedPiPackage: "starting plugins:
    context deadline exceeded" after 718s (bun's install cache was slow).
    Alone: ok (163.6s). No issue found for it.
- gofmt -l: clean.
- GOOS=windows go vet ./...: ok.
- GOOS=linux go vet -tags nogui ./...: ok. Without the tag, vet stops in
  wails v3's own linux files (undefined: pointer, cgo off when vetting
  across OSes from macOS); go.mod is untouched here.
- No GUI assets changed, so the GUI suite was not run.
2026-10-19 03:57:52 +08:00