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).
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).
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).