yetone/magpie-releases#24: ~19% of the profile was gateway
recordConversation -> conversationTurn -> redact.ScrubJSON -> mask. Codex
sends its whole history on every turn, and what is kept of each request
is scrubbed whole, so each turn ran every rule over the whole session
again: O(history) per turn.
Scrub now remembers, for strings of 256 bytes or more, what it made of
them, keyed by SHA-256 of the string and by knownGen. What Scrub returns
depends only on the string and on the values known (rules are static);
known.go bumps knownGen whenever that set changes (a value added or
replaced, a reindex after values are let go), and a result made at
another generation is never used. Nothing is kept but the hash and, when
the string changed, the scrubbed text. Two generations of at most 32,768
entries / 32 MB each. ScrubJSON's fallback for a body that isn't JSON is
unchanged, so a failed parse still scrubs line by line; nothing is
recorded unscrubbed.
Measured (Apple M4 Pro, BenchmarkScrubCodexTurn: turn 301 of a Codex
Responses history, 1.3 MB, each turn's tool output ~4 KB of code naming
tokens, keys and URLs, scrubbed after turn 300 was):
before (memo off): 2.81 s/op
after: 0.10 s/op, 5.4 MB and 21.6k allocs/op
TestScrubRunsTheRulesOnceAString: turn 41 after turn 40 runs the rules
over one long string (its new output); on the old behaviour (memo off)
it fails with "the rules ran over 41 strings for one new turn". It also
checks the output is the same as a fresh scrub.
TestScrubMemoForgetsWhenAValueBecomesKnown: a string remembered as clean
is redacted once a value in it is masked elsewhere (Scrub and ScrubJSON).
TestScrubMemoKeepsTheSecretOut, TestScrubMemoBounded.
Run: go test -tags nogui -count=3 ./internal/redact/ ok; the full suite
as in the sessions commit before this one (same tree). agent and gateway rerun at a lower load: ok; gui rerun: ok. (The first
rerun had TestPluginStreamLargeReplies red on time again, #2551, which
passes alone in 22s, and TestDiffLines at 2.04s against its 2s bound,
filed as #2766.)
gofmt -l clean; GOOS=linux and GOOS=windows go vet -tags nogui ./... ok.
Rebased onto b14784c03: go test -tags nogui ./internal/redact/ ./internal/sessions/ ./internal/usage/ ok; gofmt -l clean; GOOS=linux/windows vet of both ok.