Skip to content

fix(react-core): key virtualized messages by id for stable measurements - #6564

Closed
deepshekhardas wants to merge 1 commit into
CopilotKit:mainfrom
deepshekhardas:fix/virtual-measurement-keys
Closed

deepshekhardas wants to merge 1 commit into
CopilotKit:mainfrom
deepshekhardas:fix/virtual-measurement-keys

Conversation

@deepshekhardas

Copy link
Copy Markdown

Fixes #6089 (partially)

When deduplicatedMessages merges or drops a message mid-list (duplicate ids from HITL run syncs, double-applied tool-call events), every subsequent message shifts one index. The virtualizer's measurement cache is keyed by index by default, so the cached height of the previous occupant is misattributed to the new item until the ResizeObserver re-fires — the list jumps while scrolling through history.

Change

Pass getItemKey to useVirtualizer so the virtualizer keys items (measurement cache, React keys, edge-change detection for auto-scroll anchoring) by the stable message.id instead of the positional index:

getItemKey: (index) => deduplicatedMessages[index]?.id ?? index,

This is the documented TanStack recommendation for dynamic-height lists whose indices can shift:

"If you have a list of items that can change size or be removed, you should provide a getItemKey function that returns a stable identifier for each item, so the virtualizer can correctly attribute measurements."

No behavioral change on the happy path (append-only streaming lists keep stable indices).

@deepshekhardas
deepshekhardas force-pushed the fix/virtual-measurement-keys branch from d93e879 to 1a6a7bc Compare August 21, 2026 06:23
@BenTaylorDev

Copy link
Copy Markdown
Contributor

Closing the Closes #6089 framing rather than the change.

getItemKey is a genuine improvement to the virtualizer's measurement cache, but #6089 reports height-variance hopping — long code blocks against estimateSize: () => 100 — not mid-list mutation, so this wouldn't resolve it. The comment also says it stabilises React keys; those are already message.id at CopilotChatMessageView.tsx:765, so only the virtualizer's internal cache changes here.

Happy to see it re-filed on its own merits with a test and without the issue link. Leaving #6089 open.

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.

🐛 Bug: Virtual scrolling jitters badly with messages of varying heights

2 participants