Skip to content

perf(agents): reuse activity record parses while an agent streams - #3830

Draft
kalvinnchau wants to merge 1 commit into
mainfrom
peon/perf-agent-activity-records
Draft

kalvinnchau wants to merge 1 commit into
mainfrom
peon/perf-agent-activity-records

Conversation

@kalvinnchau

Copy link
Copy Markdown
Contributor

Problem

While an agent streams, each observer frame replaces the activity records array. Three costs grow with the length of the turn:

  • Projections were never cached. requestActivity caches a per-scope projection for each frozen record, but the projections are unfrozen copies. activityTranscript caches only frozen inputs.
  • Transcripts re-parse everything. Each activity bubble, preview and indicator rebuilt its transcript by JSON.parse-ing every selected record on every render, so the parse work is quadratic over a long turn.
  • requestWork scans every record. It filtered every record's channelIds for 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 buildTranscript or its consumers writes to it.

Evidence

Perf harness agent-stream-visible scenario (3 agents streaming into the visible channel), 4× CPU throttle. Runs were interleaved base/fix ×3 with 2 samples each, against base 46a09dbfd.

run base script ms fix script ms base task ms fix task ms
1 946 840 2250 2094
2 979 826 2578 2076
3 966 831 2287 1899

In the CPU profile, buildTranscript plus its parse self time fell from about 220 ms to about 45 ms per 25 s window, and requestWork self time fell from 155 ms to 20 ms.

Tests

  • New test in 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-activity passes (1746 tests), and so does bin/pnpm check.

Deferred

  • Human test in the desktop app: watch an agent turn stream in a channel and a thread. Check that the bubble, its preview and the transcript panel update live, and that stopping and retrying still work.

🤖 Generated with Claude Code

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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant