Repository navigation
fix(react-core): invalidate messages memo for activity object content - #6325
BenTaylorDev merged 4 commits into
Conversation
ACTIVITY_SNAPSHOT replace updates keep the same message id while mutating object content. The messages memo fingerprint used contentKey=0 for any non-string/non-array content, so generative-UI and progress activity renderers stayed on the first frame until some other message list change forced a refresh. Serialize object content in the fingerprint (multimodal attachments remain on the array length branch) and add a regression test for same-id replaces.
@copilotkit/a2ui-renderer
@copilotkit/agentcore-runner
@copilotkit/angular
@copilotkit/channels
@copilotkit/channels-core
@copilotkit/channels-discord
@copilotkit/channels-intelligence
@copilotkit/channels-slack
@copilotkit/channels-teams
@copilotkit/channels-telegram
@copilotkit/channels-ui
@copilotkit/channels-whatsapp
@copilotkit/core
@copilotkit/react-core
@copilotkit/react-native
@copilotkit/react-textarea
@copilotkit/react-ui
@copilotkit/runtime
@copilotkit/runtime-client-gql
@copilotkit/sdk-js
@copilotkit/shared
@copilotkit/sqlite-runner
@copilotkit/voice
@copilotkit/vue
@copilotkit/web-components
@copilotkit/web-inspector
commit: |
BenTaylorDev
left a comment
There was a problem hiding this comment.
Verified against current main. The 0 fallback at CopilotChat.tsx:985-990 is real, and worth noting it was only ever reachable for activity messages — in @ag-ui/[email protected] that's the one role whose content is a Record<string, any>; every other role is a string or an array. So this targets exactly the affected case, and the claim that multimodal attachments stay on the array-length branch holds.
I confirmed the regression test earns its keep: reverting only CopilotChat.tsx to main and keeping the test fails with expected 'Working on your request' to be 'Understanding your request' — the frozen first frame from the issue. With the fix, green.
Two things I checked before worrying about them out loud:
JSON.stringifythrowing on a cycle in render. Not reachable — activity content is zod-parsed from wire JSON, so it's acyclic.- Serialization cost per render. Measured: 0.0006 ms at 500 B, 0.005 ms at 4.7 KB, 0.039 ms at 47 KB. Negligible against a memo that otherwise freezes the UI.
contentKey appears at this one site only, so nothing else needs the same treatment. tsc --noEmit clean, and CI is green across the matrix.
I bless the code with a glad heart.
`messagesFingerprint`'s content key collapsed every object to 0, so an in-place content replacement that kept the same message id was invisible to every memo derived from it. Its comment claimed to mirror react-core's `messagesMemoKey`, which stopped being true when react-core #6325 (de0a659) taught that key to serialize object content. Serialize object content here too, keeping the length-not-value treatment for string and array content so large text and base64 attachment payloads are still never re-serialized per render. Serialization is guarded: the fingerprint runs on every render and `JSON.stringify` throws on a circular structure, which this component is already required to tolerate (see the existing "does not throw on tool content that cannot be JSON-serialised" assertion) — react-core stringifies unguarded, so the guard is a deliberate and documented divergence. Not a live stale-render bug via the activity path: same-id object content comes from an ACTIVITY_SNAPSHOT replace, and `role: "activity"` never reaches `listItems`, which builds rows for `user` and `assistant` only. Object content DOES reach a renderer through the `role: "tool"` correlation, though, which is what the added test drives: an object tool result replaced in place used to leave the renderer showing the first object's serialization. The comment no longer claims to mirror a moving target — it records what the key captures, why the object branch exists, and that the two implementations are independent. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Summary
CopilotChatfingerprints messages foruseMemowithcontentKey = 0for any non-string / non-array content.ACTIVITY_SNAPSHOTupdates often keep the samemessageIdand only replace objectcontent(progress / generative UI). That left activity renderers stuck on the first frame until some other list change forced a refresh.JSON.stringify(m.content)for object content in the fingerprint. Multimodal attachments remain on the array-length branch, so this does not reintroduce base64 serialization for user uploads.messageIdand asserts the UI updates.Test plan
nx run @copilotkit/react-core:test(or the package's activity e2e suite)ACTIVITY_SNAPSHOTwith the samemessageId+replace: trueand confirm custom activity renderers update each frameMade with Cursor