Commit Graph
1 Commits
Author SHA1 Message Date
yetone 0e643b14e4 sessions: a screenshot's line is read thinned, not gathered whole (#3318)
A cold read of the sessions gathered every long line it read whole
(takeLong), to take a timestamp, a folder and a session id off it. In a
10GB Claude projects folder and a 50GB Codex sessions folder the lines
behind that were Claude Code tool results holding a screenshot twice
(message and toolUseResult), Claude Code's api-request-blob lines (up to
15MB) and 154 Codex prompts with screenshots (5 to 15MB a line). Spread
over the read's workers they held 130 to 154MB live at its peak.

scanThin hands such a line on thinned, a chunk at a time, never whole:
- Claude (summary and calls): a user line whose message is a tool result,
  or a line of Claude Code's own other than a title or summary (both read
  only for keys, timestamp, folder and session id), has every string cut
  to 4KB.
- Codex (summary): a line the head lets through has only its data: URLs
  cut; a prompt's text is kept whole.
Every byte outside a string is kept, an escape is kept whole or not at
all, and a cut never splits a rune, so the line stays JSON and a search
for a quoted key finds what it found before. Lines over maxLine are still
passed over.

Run:
- TestThinnerKeepsJSON: 200 rounds of random chunk splits (1 to 7 and up
  to 9000 bytes) over a line with escapes, \u surrogate pairs and CJK;
  red when escapes are ignored.
- TestThinToolResultReadsTheSame: claudeLine (main and not) and
  claudeCallLine give the same state thinned as whole over two 3MB tool
  results; a thinned read allocates under 3MB (red at 7.9 to 8.4MB with
  the thinning off).
- TestThinCodexKeepsPromptText: codexHead+codexBody give the same state
  with a 3MB image cut, the 21KB prompt kept whole; red when every
  string is cut.
- go test ./internal/sessions ./internal/gui ./internal/usage
  -timeout 45m: sessions and usage ok; gui red in
  TestRestartHandsTheGatewayOver (12 of 13 requests answered, a reset),
  which is #3451; alone -count=3 ok.
- GOOS=linux and windows go vet -tags nogui ./internal/sessions, gofmt.
- Real use: magpie web --gateway under a sandbox HOME whose
  .claude/projects and .codex/sessions are the owner's real ones (10GB
  and 50GB), caches cold, /api/sessions?limit=50 then the 7 and 30 day
  overview (load ~40 on the machine):
    before (e57e78563): takeLong 130 to 154MB live at +6 to +20s, heap
    in use 246 to 291MB, peak footprint 590MB, ps RSS 729MB, heapSys
    736MB
    after: takeLong gone from the heap, heap in use 118MB at +12s, peak
    footprint 483MB, ps RSS 636MB, heapSys 615MB
  A debug build printing each line gathered whole found the 154 Codex
  prompts and four api-request-blob lines; this commit covers both.

Not tried: Windows and Linux real reads; a Claude prompt (not a tool
result) with pasted images is still read whole, as claudeLine reads its
text.
2026-10-25 01:39:40 +08:00