An unpacked block's strings are cut from one allocation (unpackInto). The
gateway-sessions projection (gatewayCache) and the vias cache kept a
session id, model, agent, provider or effort from nearly every block, and
each such substring held the block's whole decoded text: request and
response ids and errors included. keptStrings clones each kept value once
and interns it, so a cache holds only what it uses.
TestSessionCachesHoldNoDecodedBlocks writes 16 blocks x 512 rows with a
1 KB error each, reads GatewaySessions and Vias, drops the snapshot and
compares the live heap with and without the caches. Without the fix
(both call sites reverted) it is red: "the caches hold 16651 KB for 16
sessions: the decoded text of their blocks (8192 KB)". With it, green
-count=3.
Measured (macOS, sandbox HOME seeded with 600k gateway calls, the fixed
build vs ccb2eea15, the same Usage/Sessions/Routing views opened by
Playwright, after GC via /v1/magpie/heap; footprint from `footprint`):
- after the views: live heap 618 MB -> 466 MB, footprint 801 -> 670 MB
- after warm-up only: live heap 467 -> 390 MB
- CPU over the same 9m40s: ~22.4 s both
Go is the process that holds memory: on a live v0.1.1208 app the Go
process's footprint was 1045 MB, the Bun plugin host 51 MB, the two
WebKit WebContent processes 70 and 83 MB.
No single commit in v0.1.1225..HEAD is the cause: the shared-slab decode
(99fcddeeb) and both caches (c9b826337, 60f896031) predate v0.1.1225.
Idle with no page open, HEAD used 1.04 s CPU per 5 min against
v0.1.1225's 0.56 s.
Not fixed here: the gateway projection is kept once per window (up to
4), the startup heap peaks ~1.5 GB from GOGC doubling the warm-up's live
heap, and a synthetic 11k-session history did not reproduce the 1 GB
reported.
Run at this change on ccb2eea15 (load average ~275-500 from other
sessions):
- go test ./... -timeout 45m, sandbox HOME: 61 packages ok;
internal/agent timed out at 45m; 4 reds in gateway
(TestClaudeAccountsSpentFallBackToCodex,
TestCodexServedNamesTheAccountThatSigned) and gui
(TestPluginPageReadsWhatItDeclared,
TestSetAnswersWhileDevinAndProvidersHang).
- Those 4 alone: gateway ok 40.8s, gui ok 13.7s.
- internal/agent alone (-timeout 45m): timed out again at 2732s under
load ~500; the stacks left include a netproxy.Bridge accept loop.
internal/agent never calls GatewaySessions or Vias, the only code
changed. Not filed from here: the run kept only its last 25 lines,
so the test that hung is not named.
- internal/usage: ok 123s.
- gofmt -l: clean. go vet ./internal/usage/ for darwin, linux
(nogui) and windows: clean.