Repository navigation
perf(agents): reuse activity record parses while an agent streams - #3830
Draft
kalvinnchau wants to merge 1 commit into
Draft
kalvinnchau wants to merge 1 commit into
kalvinnchau wants to merge 1 commit into
Conversation
each observer frame replaces the activity records array. request activity projections were unfrozen copies, so transcripts re-parsed every selected record's json on every render, and request work scanned every record's channels for each mounted row. freeze cached per-scope projections, cache each frozen record's parsed plaintext for transcripts, and index each records array's channel agents once. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Kalvin Chau <kalvin@block.xyz>
This branch has not been deployed
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.
Problem
While an agent streams, each observer frame replaces the activity records array. Three costs grow with the length of the turn:
requestActivitycaches a per-scope projection for each frozen record, but the projections are unfrozen copies.activityTranscriptcaches only frozen inputs.JSON.parse-ing every selected record on every render, so the parse work is quadratic over a long turn.requestWorkscans every record. It filtered every record'schannelIdsfor each mounted message row on every frame.Change
request-activity.ts: freeze a cached projection when its source record is frozen.activity-transcript.ts: cache each frozen record's parsed plaintext by identity, so a rebuilt transcript parses only new records.request-work.ts: index each frozen records array's channel → agents once.Transcript output is still built fresh. Only the parsed JSON is shared, and it is read-only. No code in
buildTranscriptor its consumers writes to it.Evidence
Perf harness
agent-stream-visiblescenario (3 agents streaming into the visible channel), 4× CPU throttle. Runs were interleaved base/fix ×3 with 2 samples each, against base46a09dbfd.In the CPU profile,
buildTranscriptplus its parse self time fell from about 220 ms to about 45 ms per 25 s window, andrequestWorkself time fell from 155 ms to 20 ms.Tests
request-activity.test.ts: a transcript of a later selection parses only the newly selected projection. It fails without the freeze (3 parses, 1 expected).vitest run src/features/agents src/bundled/agent-activitypasses (1746 tests), and so doesbin/pnpm check.Deferred
🤖 Generated with Claude Code