perf(hub): strip large payload bodies in digest fold loaders (#118 §3)#297
Merged
Conversation
backfillAgentDigest / the fold worker load the agent's full event log via
loadFoldEvents{,Before,After}, pulling each row's complete payload_json and
json.Unmarshal-ing it — including the large display bodies (a single `text`
event can carry the run's whole accumulated transcript). For a 10k-event
session that text I/O + unmarshal dominates the backfill (#118 bottleneck #1).
The fold only ever reads small structured keys (input/output_tokens, cost_usd,
by_model, tool ids/names, status, is_error, type, turn_id) — never a body. So
shrink the payload server-side: a shared `foldEventCols` projection json_remove's
text/content/message/delta/output/thinking/thought/reasoning before the row
crosses into Go. SQLite ignores absent paths (listing a body an event lacks is a
no-op) and json_valid guards a NULL/malformed payload through untouched.
Removing a field the fold doesn't read cannot change the digest — and the
brute==incremental tests CAN'T catch a wrongly-dropped field (both paths share
this projection, so they'd break identically). So
TestFoldStripsBodiesWithoutChangingDigest folds the canonical vector with 50KB
bodies injected and asserts the stripped-from-DB digest is byte-identical to the
full-payload reference (verified to fail if a read field like $.name is added to
the strip list).
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Third Tier-A perf PR for #118. Cuts the dominant cost of the digest backfill — loading + unmarshaling the large text bodies of a 10k-event session.
Problem (#118 bottleneck #1)
backfillAgentDigestand the fold worker load the agent's full event log vialoadFoldEvents{,Before,After}, pulling each row's completepayload_jsonandjson.Unmarshal-ing it into amap[string]any. That includes the large display bodies — a singletextevent can carry the run's whole accumulated transcript. For a 10k-event session that text I/O + unmarshal dominates the backfill that still runs on a schema bump or live read-repair (after siblings #295/#296).Fix
The fold only ever reads small structured keys —
input_tokens,output_tokens,cost_usd,by_model, tool ids/names,status,is_error,type,turn_id— never a body. So shrink the payload server-side: a sharedfoldEventColsprojectionjson_removestext/content/message/delta/output/thinking/thought/reasoningbefore the row crosses into Go.json_removepaths that don't exist → listing a body an event lacks is a no-op.json_validguards a NULL/malformed payload through untouched (scanFoldEventsalready tolerates a non-JSON blob).Why a dedicated test (not the brute==incremental ones)
The brute and incremental folds share this projection, so a wrongly-dropped field would break both identically and the equivalence tests would stay green.
TestFoldStripsBodiesWithoutChangingDigestinstead folds the canonical vector with 50 KB bodies injected and asserts the stripped-from-DB digest is byte-identical to the full-payload reference. Verified it fails if a read field (e.g.$.name) is added to the strip list — thetoolsmap collapses tounknownand the assert trips.Verification
go build,go vet, gofmt clean. Digest/fold/insights/turns/canonical-vector suites pass, including-race(97s, clean).Completes #118 Tier A (the read-cache §2 was dropped — #1 + #4 absorbed its benefit; this directly attacks the residual backfill cost instead). Siblings: #295, #296.
🤖 Generated with Claude Code