Each /api/state poll json.Marshal'd the state without its agents twice
(to send it and for keepState's sum) into fresh slices, and made a new
32KB writer; ownCodex cloned Codex's whole model list for a caller that
only reads it. One set of buffers and the writer is now kept between
polls (an atomic slot, not a sync.Pool, which a GC between polls
empties; a poll that finds it taken makes its own, and buffers over
4MB are dropped), and ownCodex reads catalog.CodexShared.
Run on mote at this tree: go vet ./internal/gui ./internal/agent;
go test -run "State|Settings|Allow" ./internal/gui, the same -race for
StateWritten, -run "Codex|OwnCodex|Pick" ./internal/agent: ok.
TestStateWrittenAsEncoded now writes big after small and small after
big; with encodeTo's Reset taken out by hand it is red (a stale tail).
Real accounts, 40 polls of /api/state, alloc_space: 27.8 and 26.1 MB
before, 18.4 and 22.7 MB after.
Not tried: Windows and Linux runs (no OS-specific code).
Each poll of the Agents page encoded the whole state (1.3 MB with real
accounts) into json's buffer, and the GC between polls empties the pool
that buffer comes from, so it grew to the state's size again each poll:
48 MB of what twenty rounds allocated (v0.1.2477 build, real-account
sandbox), and 68 MB at v0.1.2463.
writeStateJSON encodes the state without its agents twice (as sent, and
as keepState keeps it, without the settings), and each agent once,
straight to the response and to keepState's sha256, through one encoder.
keepState is split so the handler hands it that sum (keepStateSummed).
When the splice can't be made (no agents, or a field put before
"agents"), it writes and sums the state whole, as before.
Measured on this Mac with the real-account sandbox (abr.sh, 20 rounds of
drift + state + providers):
- v0.1.2477: writeJSON under /api/state 48 MB, 511 MB in all.
- this change: writeStateJSON 15 MB, 477 MB in all. The /api/state
bytes are identical to v0.1.2477's (cmp).
Run on mote (mote-run, Go 1.26.8):
- gofmt clean; go vet ok, also GOOS=windows.
- TestStateWrittenAsEncoded (same bytes as Encode, same sum as keepState,
for a large state, an empty agent list and none) and
TestStateWriteGrowsNoBufferItsSize, with TestKeptState* and TestLast*:
ok 3 of 3.
- Broken by hand (the whole-state path always taken): red, "writing a
1614586-byte state allocated 4207139 bytes, want under half its size".
- Not run before this push: the full internal/gui package; it runs next.