Extract thread grouping logic and add stable wrapper identity - #3937
Extract thread grouping logic and add stable wrapper identity#3937adamleithp wants to merge 2 commits into
Conversation
…easure The experimental ChatX thread lagged on scroll where the legacy ConversationView didn't. Three structural gaps, all app-side: - Grouping rebuilt every tool_group/agent_turn wrapper per streamed chunk, so the row memos missed and the whole transcript reconciled on every token. createStableTurnGrouper now reuses a wrapper whenever its member items are reference-equal (the conversation builder freezes completed turns and clones active-turn rows), confining identity churn to the live turn — the same bound the legacy thread gets from createIncrementalThreadGrouper. ThreadItemBody is memoized on item identity for the same reason. - ToolGroup ignored the conversationCollapseMode setting and used uncontrolled defaultOpen, so every group that streamed open stayed expanded (and mounted) for the life of the session. Open state is now controlled with legacy buildThreadGroups semantics — "all" collapses everything, "partial" collapses on turn completion, "none" never — with per-group manual overrides in sessionViewStore, wiped when the mode changes. A closed marker unmounts its body, keeping long transcripts' DOM (and the scroller engine's per-scroll child scans over it) bounded. - The windowed body measured rows via the virtualizer's async ResizeObserver path, painting one frame at the 80px estimate when scrolling through history; it now resizes synchronously on mount like the legacy VirtualizedList, and the diff worker pool is capped at 2 workers to match (the library default of 8 shiki isolates costs hundreds of MB). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EAy2CqYiHQeDTgCEAvAZtG
|
Merging to
After your PR is submitted to the merge queue, this comment will be automatically updated with its status. If the PR fails, failure details will also be posted here |
Both sides added an import at the same spot in ChatThread.tsx: keep the branch's useSessionViewActions (collapse-mode overrides) alongside main's useThreadScrollRequest (activity-pane scroll bridge, #3909).
|
React Doctor found 1 issue in 1 file · 1 warning. 1 warning
Reviewed by React Doctor for commit |
|
Hey @adamleithp, we've migrated this repo into the PostHog/posthog monorepo, so this PR needs to be recreated there. Easiest path: check out the monorepo and run the Shout if you get stuck. |
Problem
The chat thread's tool-run and agent-turn grouping logic was embedded in
ChatThread.tsx, making it hard to test and reuse. Additionally, during streaming, the grouping wrappers (ToolGroupItem,AgentTurn) were recreated on every chunk even when their member items hadn't changed, causing unnecessary re-renders of memoized rows and defeating the performance benefit of memoization.Changes
Extract grouping logic to new module:
threadGrouping.tswithgroupToolRuns(),groupIntoTurns(), and the newcreateStableTurnGrouper()functionisToolCallItem,isInvisibleItem,INVISIBLE_UPDATES) to the new modulethreadGrouping.test.tscovering all grouping scenariosImplement stable wrapper identity:
createStableTurnGrouper()preserves wrapper object identity across calls when member items are reference-equalToolGroupItemandAgentTurnwrappers if their member arrays haven't changedUpdate ChatThread to use stable grouper:
createStableTurnGrouper()once per mounted thread viauseRefturnGrouper.update(items)in the rows memo instead ofgroupIntoTurns(groupToolRuns(items))Update ToolGroup component:
groupIdprop (the run's first tool id) to key manual expand/collapse overridesopenstate instead ofdefaultOpenso groups actually collapse on turn completionUpdate VirtualThreadScrollBody:
measureElementImmediatelycallback to resize rows synchronously on mountUpdate sessionViewStore:
useGroupOverride(id)hook for fine-grained subscription to a single group's overrideHow did you test this?
threadGrouping.test.tscovering:ToolGroup.test.tsxto pass newgroupIdpropAutomatic notifications
https://claude.ai/code/session_01EAy2CqYiHQeDTgCEAvAZtG