Commit Graph
3 Commits
Author SHA1 Message Date
yetone 8d80f6bc0a sessions: the kept stats and listing hold an untitled id once, not a map entry
keptStats and keptList noted every answer Codex's titles gave in a
map[id]string, to ask each again before the kept ranges are given out.
Nearly every id has no title, and the map held 3.12MB on an 18k-file
history. titleAnswers keeps the named ones in a map and the untitled ids
in a slice, sorted, without repeats or slack once settled; still() asks
each again as before. Both caches use it.

Run (mote, real-account copy, idle 180s, network blocked):
- statsKept.keptTitle.func4 3.12MB -> under the profile's cut;
  total inuse 107.40MB -> 104.23MB.
- TestStatsKeptBetweenPolls now also names a Codex thread after the
  ranges were kept and checks the title reaches them.
  TestTitleAnswersSettled checks repeats and slack go.
- Broken by hand (still() skipping untitled ids, settle not compacting):
  TestListKeptBetweenPolls, TestStatsKeptBetweenPolls and
  TestTitleAnswersSettled red; green on this code.
- go test ./internal/sessions/ ok; go vet darwin, linux, windows ok;
  gofmt clean.

Not tried: the full suite (only internal/sessions reads these caches).
2026-11-09 21:18:44 +08:00
yetone e55bab72df sessions: the gateway's sessions are merged beside the native ones, not into a copy of them (#2278)
MergeExternalStats grew a copy of every native Summary to append the
gateway-only sessions, and cloned them all again, with their days, when
the gateway's history began before the session files. Each range the
Sessions page keeps (7, 30, every day) held that copy beside the
Summaries it was copied from: ~10 MB over the full history, made again on
every 10 s refresh.

Stats now keeps the gateway's Summaries in their own slice, sorted by
Last, and a day shift instead of rewritten days. Overview walks both in
the order the merged list had (the most recent first, a native session
first of two as recent), adds the shift to each day, gives toolUse the
shifted first day, and shifts the Top copies it hands out. AllSessions
gives the merged list as one copy for the tests that read it.
Overview is the only reader of the merged sessions outside tests:
aliasStats and keepDesktop run before the merge, and the stats answer
doesn't encode Sessions.

Run on mote at 86e19b9b7 plus this change:
- go vet ./internal/sessions ./internal/gui ./internal/cli: ok;
  GOOS=linux and GOOS=windows go vet ./internal/sessions: ok.
- go test ./internal/sessions ./internal/cli: ok.
- go test ./internal/gui: ok except TestCoveredWindowStaysUp, killed at
  30s. It fails alone too, at load ~21. #4125 covers it: on mote every
  AppKit test hangs while launchd throttles it. This change doesn't touch
  window code.
- TestGatewayStatsMergedBesideNative (new) goes red three ways by hand.
  With Sessions cloned in the merge: "the native sessions were copied".
  With a gateway session first of two as recent: "merged [g2 a g1 b]".
  With Top and toolUse unshifted: the overview differs from a merged
  list's.

Real history on mote (advov.sh, the full-history HOME, v0.1.2287 =
rel2 against this change):
- cold peak phys_footprint: 355/352/362 MB against 358/355/358 (noise:
  the cold peak is the requests warm-up, not this);
- warm start: 249/262 MB against 258/274;
- /api/sessions/overview and /api/sessions/stats for days 0, 7 and 30
  are byte-identical between the two builds in both warm runs.

Rebased on origin/main after the run. The 9 commits it brought in
(gateway, usage window_holds, gui Long conversations, workbuddy min)
touch nothing under internal/sessions. After the rebase, internal/sessions
and the gui gateway-session tests were run again (below).
- At the rebased head on c272e5582: go vet ./internal/sessions
  ./internal/gui ok; go test ./internal/sessions ok; go test
  ./internal/gui -run 'Gateway|Stats|Overview|Desktop|Budget' ok.
2026-10-31 09:41:13 +08:00
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