Commit Graph
4 Commits
Author SHA1 Message Date
yetone 30f29564eb sessions: a change to one agent's calls reads that agent's files again, not every shard (#2278)
callCacheVersion named every shard, so raising it for a fix to one
agent's calls threw away every agent's calls and re-read the whole
history on the next start: on a real history of ~4,700 transcripts
that is minutes of CPU and the ~310MB peak, for a change to Claude
alone.

Each agent now has its own number in callParseVersions. A shard frame
records the number its agent had when it was written (callDiskState
.Parser), and loadCalls drops a frame whose number is not the current
one, so only that agent's files are read again. CallCacheVersion()
adds the raised numbers, so the usage caches built on calls
(requestDiskBinary, heat_disk) still start again when any agent's
calls change. callCacheVersion stays for a change to every agent or to
the shards' form.

Existing shards are kept: the map is empty, gob leaves a zero Parser
out, and CallCacheVersion() is still "calls-v15".

Run on mote (zone worktree at this head):
- go vet ./internal/sessions ./internal/usage for darwin, linux and
  windows: ok. go build ./...: ok.
- go test ./internal/sessions ./internal/usage: ok.
- go test -race -count=3 -run 'TestCallParseVersionRaise|Calls|Shard|Frame'
  ./internal/sessions: ok.
- TestCallParseVersionRaiseReadsThatAgentAlone: a Claude transcript and
  a Codex rollout; raising claude scans the Claude file alone, with the
  same calls, and the next start scans nothing.
- Broken by hand (the Parser check in loadCalls removed): red at
  call_versions_test.go:67, "scanned map[], want Claude's file alone".

Not changed: internal/gui's TestCoveredWindowStaysUp is killed at 30s
on mote, as in #4125.
2026-10-31 12:34:40 +08:00
yetone ac149224f9 usage: many rows made since the last write have it written once a run (#3493)
The file of the session files' chunks (requests-v1.bin) was written
only once 64 files' chunks had been made since the last write. Long
sessions that ended since then are few files with many rows: on the
real history (sbpairs) two starts in a row each made 9 files' chunks
again, 84,625 calls (one Claude Code session alone is 52,191), and
neither wrote them, so every start did it again, and the sessions that
ended next joined them until they were 64 files.

noteBuilt also counts the rows made, and writes the file once a run
when they are 20,000 or more. Once, as a session still at work is made
again after each of its calls; the 64-file write is as it was.

Run on 13f907013, and the packages again rebased on 73d17f90c
(usage, gui, sessions ok; vet and gofmt clean):
- real history in a sandbox HOME (the sbpairs copy of the caches), a
  page in each of three processes: before, 9 files (84,625 calls) read
  at each of two starts; after, a start that made every chunk (the
  source changed) and then 0 files read, then 2 (live sessions).
- the rows rule alone on the real history (requestDiskLeast raised past
  15.6k): with no file, a start wrote it (44,275,132 bytes), and the
  next start read it and made 2 files' chunks.
- TestRequestDiskRowsWrittenOnce: red with the rows rule removed ("no
  file of them") and red with the once guard removed ("written again
  for 4 rows").
- go test -count=1 ./internal/usage ./internal/gui ok; go test -race
  -run 'TestRequest|TestQueryPage|TestChunk' ./internal/usage ok;
  GOOS=linux and GOOS=windows go vet -tags nogui on both ok; gofmt -l
  clean.

Not tried: a full go test ./... (one suite per machine).
2026-10-25 07:47:12 +08:00
yetone 42ab935516 usage: requests-v1.bin is of the code that makes rows, not of the binary
The file of the session files' chunks was named by the binary's own
stat key, so every upgrade, and magpie ships several a day, read every
session file again on its first Requests page: on a 15.6k-shard history
warm-up step 2 went from ~0.8-0.9s (read from the file) to 1.6-1.9s and
~1.3GB more allocation. It is named now by the call shards' version
(sessions.CallCacheVersion, which already decides whether a shard of the
calls is read) and a SHA-256 of this package's non-test source, which
the binary carries (go:embed *.go, ~750KB with the tests, which the name
leaves out). Of the last 99 releases, 17 changed that source; the other
82 keep the file. Prices, names and settings are in the file's meta as
before. When the embedded source can't be read the name is the binary's,
as it was.

Measured (real history, sbpairs): one test binary wrote the file, a
second binary at another path (the old key differs) read it: step 2
0.79s and 1001MB alloc, against 1.63s and 2285MB cold.

Run at this tree (89dc16476 + this change):
- go test -race -count=1 ./internal/usage: ok
- go test -count=1 ./internal/sessions ./internal/gui: ok
- GOOS=linux and GOOS=windows go vet -tags nogui ./internal/usage
  ./internal/sessions, and go build -tags nogui .: ok; gofmt -l clean
- TestRequestDiskNamesItsSource hashes the package's files from disk
  and wants the same name. Broken by hand (embed request_*.go only):
  red; restored: green.

Not tried: an upgrade of the real app from one release to the next.
2026-10-25 07:23:45 +08:00
yetone 5d2260d3e6 usage: the Requests page's chunks are kept on disk, so a start doesn't open every call shard again
A started magpie opened every session file's call shard (~15.6k on a busy
machine) and turned each call into a row again before its first Requests
page could show (#3493). requests-v1.bin (beside sessions.json) now keeps
each session file's chunk as the page packs it: an entry stands while its
file's size and mtime are the same, as a call shard does, and the whole
file while the binary that wrote it and the index's root and meta (prices,
names, providers) are the same. Each entry has a length and CRC-32; a bad
one ends the read and the entries before it stand.

- The file is read when 64 or more session files want reading (a start, a
  change of prices), and written behind the lock once 64 or more chunks
  were made since the last write; fewer are a few shard reads next start,
  not worth writing the whole file again. The idle trim does not write.
- A chunk of another computer's day is not kept; times keep their zone
  (Local, UTC, a tz-database name or a fixed offset).

Real data (sandbox HOME with the live ~/.codex/sessions and
~/.claude/projects symlinked read only, 15,603 sources, 420k rows, load
~90 to 130): a cold All page 3.6s; written in 76 to 168ms behind the lock,
44MB. A start from the file: fromDisk 50ms for 15,602 chunks, then the
blocks 100ms, against the shard reads before; the whole page 1.5s, most of
it now readRequestState (0.65 to 1.1s), which is next. Same total, cost
and input as the cold page.

Run at 500aa7096 (origin/main, fetched): go test -timeout 45m of usage
(14.7s), usageapi (6.5s) and gui (143.5s, before the idle-save removal,
which touches no GUI path) ok; TestRequestChunksFromDisk -race -count=3
ok. gofmt clean; GOOS=linux and GOOS=windows go vet -tags nogui
./internal/usage/ clean.

TestRequestChunksFromDisk (new): a start reads no session file, a file
written over to the same size is read again for its mtime, new prices read
every file, a file cut short stands for the entries before the cut, and
garbage reads every file; every page equals the ledger's. Red with the
header compare taken out, with the size/mtime check taken out, and with
the write skipped.
2026-10-25 05:54:24 +08:00