Commit Graph
5 Commits
Author SHA1 Message Date
yetone 2ae8e61a4f usage: an upgrade reads session files again only when the code of their rows changed (#2278)
requests-v1.bin and log-v1.bin were keyed on a hash of every .go file in
internal/usage, so nearly every release that touched the package threw
both away. The first start after such an upgrade read all ~24k call
shards again: on the owner's real accounts (mote, acctp sandbox) that is
13.8s of CPU and ~3.4GB allocated before the page settles, against 2.4s
warm.

Each file now keys on the source its rows are made, written and read
through: rowSource (15 files) and logSource (23 files), embedded by name.
The row pricer and a file's chunk builder are lifted out of
readRequestBlocks (sessionRowPricer, sessionChunk) so they can be roots.
TestRequestDiskNamesTheCodeOfItsRows type-checks the package and walks
every declaration reached from those roots (uses, package vars assigned
elsewhere, interface calls, encoding/String/sort methods); it fails when
a reached file is not embedded or an embedded one is not reached.
Of the last six usage-changing releases, four (3962, 3958, 3941, 3908)
would have kept the cache.

Run:
- go test -count=1 ./internal/usage on mote: ok 51.9s.
- go vet ./internal/usage for darwin, windows, linux on mica: ok.
- Broken by hand: session_provider.go left out of rowSource's embed ->
  "rowSource: session_provider.go is reached but isn't in its go:embed",
  FAIL; restored.
- Real accounts on mote (pageheap.sh, page open, CPU at mark):
  w47v first start after an older binary 13.80s, numGC 217 (rebuild,
  expected); w47v again 2.45s; w48v (w47v plus a live init() in
  effort_split.go, a different binary) 2.59s, numGC 56; back to w47v
  2.43s. Before, the same swap cost ~14s.
Not tried: Windows or Linux real start.
2026-11-13 10:09:27 +08:00
yetone 6af6ae9885 usage: a log of MCP calls and probes keeps its blocks on disk too (#2278)
log-v1.bin was written only once a gateway block had filled. A log of
MCP tool calls and probes has none (the real-account sandbox's 8.3 MB
log is 25,223 MCP lines and 2,539 probes, 54 probe blocks, 0 gateway
blocks), so full.n stayed 0, the file was never written, and every
start parsed all 27k lines again: readLogSnapshot took 60 MB of the
start's allocations. The probes that were kept went as JSON and were
parsed back a record at a time.

A full point is now marked too when a probe block fills while no
gateway row stands after the full blocks, and it records how many probe
blocks were full there (logFull.probes). Those go to the file as binary
entries, as the gateway blocks do, read from its mapped pages; only the
probes after them, up to the full point, still go as records. The file
is written again when its full point reaches further (by offset, not
the gateway block count). The magic is magpie-log-v2, so a v1 file is
parsed over once.

Run:
- go vet ./internal/usage; go test ./internal/usage ./internal/gateway
  (mica, -timeout 45m): ok usage 31s, ok gateway 859s.
- TestStartReadsKeptProbeBlocks (new): an MCP-only log of 3 blocks + 100
  is kept, a start reads 3 blocks from the file, and reads as a whole
  parse. Broken by hand (no probe full point): red, "a log of probes
  wasn't kept".
- Real-account sandbox on mote (acctp, page open 2.5 min, version
  0.1.3961 builds): log-v1.bin written on the first start (645 KB);
  warm starts, w45v (v0.1.3961) against this:
    CPU at mark  3.21s, 3.00s  ->  2.50s, 2.39s
    allocated    701 MB        ->  646 MB, 629 MB
    readLogSnapshot cum 60 MB  ->  2.6 MB
    numGC        66            ->  56
  Footprint 128-133 MB -> 132-135 MB, liveHeap ~80 MB both: the same
  within noise.

Not tried: a log with both gateway and probe blocks across a v1 -> v2
start on a real account (the unit test's mixed log covers the format).
2026-11-13 08:51:24 +08:00
yetone d85ee9e53f usage: the probes are kept in packed blocks, as the gateway rows are
The request log's probes (magpie's own tests, model lists, MCP tool calls
and searches) were a slice of whole Records, 952 B each, and every read
that continued the log appended to a copy of it. The owner's real log has
25,379 of them (25,223 MCP tool calls): ~24 MB of live heap in every
running app, the tray's included, for rows only the Requests page lists.

Now they are rowChunk blocks of logBlockRows, frozen (packed) when full,
the last one reopened as a copy for the next append (probeRows), the way
readLogSnapshot keeps gateway rows. probeChunk and log-v1.bin read them
back through chunkRows; a block whose Latest is before since is skipped
whole. log-v1.bin's format is unchanged.

Tests: TestProbeBlocksContinued (3 blocks + an appended block: the earlier
snapshot reads as before, the continued read equals a fresh parse, since
cuts at the right row). Broken by hand: dropping the continued snapshot's
probe blocks turns it red. TestStartReadsKeptLogBlocks covers the disk
round trip with probes.

Run at this head on mica: go vet ./internal/usage; go test ./internal/usage
./internal/gateway/... ./internal/gui, all ok (gateway 869s).

Real app on mote (sandbox HOME from the real-account sandbox, plus the
25,379 probe lines of the owner's real log), tray idle at 150s, cold:
  before (w36, v0.1.3938's code): footprint 105 MB, live heap 48.4 MB,
    readLogSnapshot 26.5 MB of it
  after  (w37): footprint 70 MB, live heap 16.0 MB, readLogSnapshot out
    of the top; warm start (log-v1.bin read back): live heap 15.6 MB
Requests page, web mode, same HOME: requests.csv byte for byte the same
on both builds; /api/usage/requests' 100 rows the same apart from the 4
probe rows the first run itself wrote (token refreshes through the dead
proxy).

Not tried: Windows and Linux (no OS-specific code here).
2026-11-13 02:35:15 +08:00
yetone 62a1dcdd79 usage: a start's log blocks are of log-v1.bin's pages, not the heap (#2278)
logDiskRead read log-v1.bin whole with os.ReadFile, and the kept blocks'
Archives were slices of that copy, so a 240k-call history held ~7 MB of
file bytes in the heap for good. GOGC then let the heap grow by about as
much again. Where the system maps the file, the blocks are now of its
pages, the same way requests-v1.bin's blocks already are (diskMapping,
readMapped, m.read()). Elsewhere it is read into the heap as before.

Run at the pushed head, on mote, through mote-run:
- go vet ./internal/usage: ok. GOOS=linux and GOOS=windows go vet
  ./internal/usage: ok.
- go test ./internal/usage: ok. go test ./internal/usageapi: ok.
- New TestLogDiskBlocksMapped: 2 full blocks plus a tail are written,
  the log is restarted and read again, and each full block's Archive
  must be inside the mapping. Broken by hand (c.mapped = m dropped): red,
  "block 0: read from the file into the heap". Green again restored.
- go test ./internal/gui: FAIL on the AppKit window tests only
  (TestDockJoinLeadsCommandTab, TestPanelCloseHidesIt,
  TestMainWindowCorners and 7 others), killed at their deadline on mote
  under load ~24. These are #4125 and #2645, and the same tests fail
  without this change.

Bench on mote, with the real 240k-row history: a warm restart while
the Sessions tab polls, and the heap profiled at 200 s.
- In use: 174.1 and 175.0 MB with this change, 187.6 MB without.
- Footprint at 200 s: 290 and 297 MB with it, 297 MB without. mote's
  load (~24) makes this ±20 MB, so the footprint gain is not resolved.

Not tried: Windows, where the file is still read into the heap.
2026-10-31 00:28:43 +08:00
yetone 8f0d21a009 usage: a start reads the gateway log's full blocks from log-v1.bin, not the whole log again (#2278)
Every start parsed the whole of usage.jsonl with json.Unmarshal a line at
a time. On the owner's real history (109 MB) that was 622 MB of the first
18s's 1765 MB allocated, and most of the GC work of a cold start.

The full blocks the log index freezes are now kept in log-v1.bin beside
the sessions cache, with where they end in the log (offset, line count),
the SHA-256 of the log up to there and the hasher's state, first/firstOwn,
the provider keys and hosts seen, and the probe rows before that offset.
A start reads them and parses only the lines after. They stand only
while the log's bytes up to the offset hash the same and the code that
reads them is the same (requestDiskBinary); otherwise the whole log is
parsed as before. The file is written through a temp file and a rename,
at most every 10 minutes and only when it gains blocks.

Run on mote at this tree, rebased on origin/main:
- go test ./internal/usage ./internal/usageapi: ok
- GOOS=linux|windows|darwin go vet -tags nogui ./internal/usage: ok
- TestStartReadsKeptLogBlocks: a second start loads the kept blocks, and
  rows, probes, keys, hosts and hash equal a whole parse; a log edited
  before the offset is parsed whole.
- Broken by hand: accepting the kept blocks without resumeLogHash turns
  TestStartReadsKeptLogBlocks red (hash differs); restored, green.
- Bench on a copy of the real history (~/r2278, 109 MB log), cold start
  polled 120s, second start of each binary (caches hit):
  v0.1.2184: peaklive 217MB fp_peak 307MB, 156 GCs
  this:      peaklive 210MB fp_peak 315MB,  98 GCs
  readLogSnapshot alloc_space 622MB -> 8.6MB; total 1765MB -> 1095MB.
  The first start writes log-v1.bin (7.4 MB).

Not tried: the GUI suite (no GUI file changed); Windows and Linux runs of
the package (vet only).
2026-10-30 17:10:26 +08:00