Skip to content
Source
Sign inSign up
pingdotgg/t3code

perf: reduce CPU usage in event replay, projections, and sidebar #3825

Closedt3dotgg(GitHub profile, opens in a new tab)wants to merge 4 commits intomain fromt3code/performance-fixesopened View on GitHub

Fixes the main CPU hot paths from the web performance audit. The theme across all of these: the app was doing history-sized work per event. Reconnects scanned the whole event log, every streamed token re-read entire thread histories on the server, and every keystroke re-serialized every draft. These fixes make that work proportional to what actually changed.

Intentionally out of scope: bundle-size work (desktop-first, big upfront payloads are fine) and streaming-path optimizations (streaming is off by default).

Server

Catch-up replay is no longer a global scan. Thread reconnects read only that thread's events via the existing (aggregate_kind, stream_id, sequence) index instead of scanning every event after the cursor. Shell reconnects are capped at 500 events; past that, the server sends a fresh shell snapshot instead of replaying the gap. Reconnect cost now scales with the selected thread, not total server history.

Shell summaries update incrementally. Previously any message or activity event re-read all of a thread's messages, plans, activities, and approvals to recompute sidebar state. Now message-sent just advances latestUserMessageAt, and only the six approval/user-input activity kinds trigger a full recompute. High-volume activity (command output, file edits, progress) no longer touches the thread row or broadcasts shell updates at all.

Client

Sidebar prewarm: 10 streams → 1. The sidebar opened a full detail subscription for the first ten visible threads. Now it prewarms only the row the pointer rests on for 120ms. At idle: one shell stream plus at most one detail stream.

Per-event costs in the thread reducer. Activities append in O(1) when they arrive in order (the normal case) instead of filter+sort over the whole history. IndexedDB snapshots aren't written while a turn is running — the full-history encode used to fire on a 500ms debounce throughout every turn; now the settled state persists once at turn end.

Timeline derivation. Four derivations each copied and re-sorted the same activities array per render; they now share one cached ordered view. The combined timeline uses a k-way merge of its pre-sorted inputs instead of concat+sort per event.

Composer drafts. Typing used to re-run JSON.stringify over all retained drafts per keystroke (only the storage write was debounced). Serialization itself is now debounced, and unchanged drafts reuse their previous partialized form.

Smaller fixes. One shared bootstrap poller instead of one 2s interval per consumer, notifying only on actual changes. Minimap writes data-in-view only when it flips. Shiki languages without grammars (sh, yaml) are negative-cached instead of retrying and throwing on every render.

Testing

  • Full suite passes (575 files / 4549 tests) including new coverage for the replay, summary, persistence, and prewarm changes; vp check and typecheck clean
  • Manually stress-tested against a source build: reconnects with large replay gaps (including the 500-event snapshot fallback), force-quit mid-turn recovery, approval/input/plan sidebar badges, draft persistence across immediate tab close
  • Review bot findings (SVG hover targets, Shiki cache bounds, residual activity fan-out) verified and fixed in 6b2f3f8ae

One behavior note: threads sorted by "Last user message" no longer drift upward from agent tool activity alone — updatedAt moves on messages, session changes, and plan/approval events, not every tool call.


Note

Medium Risk Touches reconnect/catch-up, shell projection semantics (updatedAt / sidebar sort), and thread cache persistence during active turns—behavior changes are intentional but worth validating on large histories and mid-turn recovery.

Overview Reduces CPU by making hot paths proportional to what actually changed instead of re-scanning full histories on every event.

Server: Adds readAggregateFromSequence / readAggregateEvents so thread reconnect catch-up reads only that thread’s events via the aggregate index; shell catch-up is capped at 500 global events, then falls back to a fresh shell snapshot. Shell projections update incrementally for thread.message-sent (e.g. latestUserMessageAt, no updatedAt bump on streaming assistant chunks) and skip most thread.activity-appended kinds unless they affect approval/user-input summary fields; WS shell fan-out mirrors those skips.

Client: Sidebar detail prewarm moves from the first N visible rows to a single hover-rest target (120ms). Thread reducer fast-paths in-order activity appends; IndexedDB thread snapshots are not written while session.status === "running". Session derivations share a cached ordered-activities view; composer persistence uses debounced JSON serialization; Shiki negative-caches unsupported fence languages; minimap and desktop bootstrap polling avoid redundant updates.

Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit e5d5249680d718c7b22d093adb77f2950c29064d. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).

Note

Reduce CPU usage in event replay, projections, sidebar prewarming, and thread sync

  • Shell event stream now skips assistant streaming message chunks and non-summary activity kinds, reducing redundant broadcasts; catch-up replays are bounded to 500 events with snapshot fallback beyond that limit
  • Projection pipeline handles thread.message-sent incrementally (no full summary rebuild) and ignores thread.activity-appended events for non-summary activity kinds
  • Adds readAggregateEvents to the event store and engine, enabling thread detail catch-up to read from the thread aggregate stream directly instead of scanning the global stream
  • Sidebar prewarming switches from eagerly prewarming the first N visible threads to a hover-intent model with a 120ms delay, prewarming only one thread at a time
  • Thread snapshot persistence is skipped while a session status is running, resuming once the session goes idle
  • Activity list appends in applyThreadDetailEvent use a fast path that skips sort when the new activity is already in order
Macroscope summarized e5d5249.
    1. coderabbitai[bot](GitHub profile, opens in a new tab)commented

      Important

      Review skipped

      Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

      ⚙️ Run configuration

      Configuration used: Repository UI

      Review profile: CHILL

      Plan: Pro

      Run ID: f245e5af-6999-463f-850c-e9751674655d

      You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

      Use the checkbox below for a quick retry:

      • 🔍 Trigger review
      ✨ Finishing Touches
      🧪 Generate unit tests (beta)
      • Create PR with unit tests
      • Commit unit tests in branch `t3code/performance-fixes`

      Comment `@coderabbitai help` to get the list of available commands.
    2. github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted

    3. t3dotgg(GitHub profile, opens in a new tab) changed the title

    4. Cursor Bugbot has reviewed your changes using high effort and found 2 potential issues.

      Fix All in Cursor ❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the [Cursor dashboard](https://www.cursor.com/dashboard/bugbot).

      Want fixes drafted automatically? Bugbot Autofix can create code changes for findings. A team admin can enable Autofix in the Cursor dashboard.

      Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 05e623fee9e0173c0e579136e6bc46f31516caca. Configure [here](https://www.cursor.com/dashboard/bugbot).
    5. macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      Approvability

      Verdict: Approved

      Performance optimization PR with bounded event replay, incremental projection updates, and client-side caching. Changes are well-tested and functionally equivalent to prior behavior, with clear documentation of the optimization tradeoffs.

      You can customize Macroscope's approvability policy. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).
    6. t3dotgg(GitHub profile, opens in a new tab) changed the title

Sign in to join this conversation.