Skip to content

perf(sidebar): stop re-rendering on every streamed token#2710

Open
KarloAldrete wants to merge 1 commit into
PostHog:mainfrom
KarloAldrete:perf/reduce-streaming-rerenders
Open

perf(sidebar): stop re-rendering on every streamed token#2710
KarloAldrete wants to merge 1 commit into
PostHog:mainfrom
KarloAldrete:perf/reduce-streaming-rerenders

Conversation

@KarloAldrete

Copy link
Copy Markdown

Problem

The sidebar re-renders on every streamed agent token. It consumes the whole sessions record via useSessions(), which immer replaces on each appended event (one per token). Because the sidebar is mounted at the root, this re-renders the whole tree on every token during streaming.

Part of #2162.

Changes

deriveTaskData only reads four session fields (isPromptPending, pendingPermissions.size, cloudStatus, cloudOutput.pr_url) — never events.

  • computeSidebarSessionSignature (core, pure): a primitive signature of just those fields.
  • useSidebarSessionMap (ui): subscribes to that signature and rebuilds the taskId → session map only when a sidebar-relevant field changes — not on every token.
  • useSidebarData uses it instead of useSessions().

No behavior change — deriveTaskData receives the same data; only the subscription granularity changed.

How did you test this?

New render-count test (useSidebarSessionMap.test.tsx):

20 streamed tokens sidebar re-renders
before (useSessions()) 20
after (useSidebarSessionMap()) 0
after, when a relevant field changes 1
  • New pure-function test (computeSidebarSessionSignature.test.ts): the signature ignores events, changes on the 4 fields.
  • pnpm --filter @posthog/core --filter @posthog/ui typecheck — clean.
  • biome lint packages/core — clean, no noRestrictedImports; host-boundaries unchanged.
  • Existing suites green: core/sidebar (48 tests), ui/features/sessions (230 tests).

Automatic notifications

  • Publish to changelog?
  • Alert Sales and Marketing teams?

Created with PostHog Code

The sidebar consumed the whole `sessions` record via `useSessions()`, which
immer replaces on every appended event (one per streamed token). Since the
sidebar is mounted at the root, that re-rendered the whole tree on every token.

`deriveTaskData` only reads four session fields (isPromptPending,
pendingPermissions size, cloudStatus, cloudOutput.pr_url) -- never `events`:

- Add `computeSidebarSessionSignature` (core, pure): a primitive signature of
  just those fields.
- Add `useSidebarSessionMap` (ui): subscribes to that signature and rebuilds the
  taskId -> session map only when a sidebar-relevant field changes.
- `useSidebarData` uses it instead of `useSessions()`.

Render-count test: 20 streamed tokens caused 20 sidebar re-renders before, 0
after (and 1 when a relevant field actually changes).

Part of PostHog#2162

Generated-By: PostHog Code
Task-Id: 8e9f327d-84b1-4608-9f48-c3038dbf87ca
@greptile-apps

greptile-apps Bot commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

Reviews (1): Last reviewed commit: "perf(sidebar): stop re-rendering on ever..." | Re-trigger Greptile

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