Files
magpie/internal
yetone 938c31424e gateway: a long request body is checked for valid JSON once, not by every decodeJSON
decodeJSON runs jsonValid over its whole input before filling the value,
and a request's body goes through it a dozen times on its way: modelOf,
parseAnthropicRequest, swapImageBlocks, blocksOf, the prompt builder,
conversationID, requestEffort, bodyEffort, streamOf, walkImages... On a
2 MB Anthropic request the repeated checks were ~18% of the CPU of
serving it (pprof).

jsonValid now remembers the last 8 bodies of 32 KB or more that it found
valid, by length and a maphash under a seed of the process's own. Only
valid results are kept, so an invalid body is checked (and refused) every
time. A false hit needs a 64-bit collision under a seed no caller can
see; even then fillJSON still parses the body.

Guard: TestDecodeJSONChecksALongBodyOnce counts the byte-by-byte checks
of one 84 KB body over six decodes. With the lookup turned off by hand:
"a 84087-byte body was checked 6 times over six decodes, want 1".
TestDecodeJSONMemoKeepsRefusing: a body changed by one byte at the same
length, and a truncated one, are still refused after the valid one was
remembered.

Numbers (BenchmarkZZHot, a fake upstream with no network, 2 MB request,
count 3, CPU from rusage since the box sits at load ~350):
  big-nostream     before 113.4 / 140.1 / 117.1 cpu-ms/op
                   after  107.6 /  98.7 /  99.2 cpu-ms/op
  big-stream-pass  before 134.6 / 151.0 / 161.6 cpu-ms/op
                   after  124.1 / 131.0 / 144.5 cpu-ms/op
B/op and allocs/op are unchanged (65.9 MB, 181k; 81.9 MB, 310k), as
expected: the check allocates nothing.

Run (on ccb2eea15 with this commit and the previous one, the collector
fix, both in the tree, sandbox HOME, load ~350):
- go test -tags nogui ./... -timeout 45m -count=1: 62 packages ok,
  internal/gateway ok (2694s). Two reds, both in untouched packages and
  both filed:
  - internal/gui TestSetAnswersWhileDevinAndProvidersHang: set slow/slow-1
    took 10.67s against its 10s bound (#1375). Rerun alone with -count=3,
    it passed at 4.7s, 2.8s and 7.1s.
  - internal/agent ran past 45m, with TestWSLMoreAgentsPick running at the
    deadline (#2544).
- gofmt -l internal/: clean.
- go vet ./internal/gateway passed.
- GOOS=linux and GOOS=windows go vet -tags nogui ./... passed.
- go build passed.
- The memo guard is red with the lookup turned off by hand (above).
2026-10-19 03:11:12 +08:00
..
…
…