Repository navigation
Conversation
BenTaylorDev
left a comment
There was a problem hiding this comment.
Thanks for this — the bug is real and I reproduced it. I'd like to resolve one thing before it lands, because I think the fix is aimed at a mechanism that isn't the actual cause.
The premise
The comment in CopilotChat.tsx describes "retained message objects with in-place activity mutations." I scripted ACTIVITY_SNAPSHOT -> ACTIVITY_DELTA against the pinned @ag-ui/client 0.0.57, reading agent.messages at each onMessagesChanged:
#1 sameObject=true sameContentRef=true content={"step":1,"label":"starting"}
#2 sameObject=false sameContentRef=false content={"step":2,"label":"working"}
The client replaces both the message and its content with new objects. There's no in-place mutation to defend against.
What the actual cause is
messagesMemoKey computes:
const contentKey = typeof m.content === "string" ? m.content.length
: Array.isArray(m.content) ? m.content.length : 0;ActivityMessage.content is z.ZodRecord -> Record<string, any>, so it always takes the : 0 branch. The key is act-1:activity:0: before and after the delta — and activityType isn't in the key either. Two independent blind spots, which is why useMemo returns the stale array and the memoized child never sees new props.
So the two changes that actually fix this are the ones inside messagesMemoKey: hash object content, and add activityType. Once new message objects reach the child, the existing JSON.stringify(content) comparator already detects the change on its own.
That would let the messagesRevisionRef subscription, the WeakMap cache, refreshContentKeys, the CONTENT_MEMO_KEY symbol, the defineProperty stamping, and the per-render shallow copies all come out. I'd much rather carry the smaller version.
Two issues worth fixing either way
-
getContentMemoKeyreturns a constant0on its catch path. A nestedDate,Map,Set, or class instance anywhere in the payload throwsUnsupported content object, yields0on every render, and leaves the memo permanently unable to invalidate — the same symptom this PR fixes, for that class of payload. (Currentmainhas the same blind spot, so this isn't a regression — but the fallback asserts equality, which is the unsafe default. Returning a monotonic counter would fail safe.) -
Render-phase ref mutation.
previousMessagesRef.currentandpreviousMessagesRevisionRef.currentare assigned during render rather than in an effect. A render React discards (concurrent interrupt, StrictMode double-invoke) still advances them, so a later render can see "unchanged" and reuse a stale cached key.
One thing I checked that's fine
Stamping new message identities doesn't regress the other memoized rows — the comparators at CopilotChatMessageView.tsx:94, :172, :246, and :300 compare message.content by reference, which a shallow {...message} copy preserves.
Also: this shares CopilotChat.tsx with #6291, so whichever lands second will need a rebase.
ea26358 to
7faafb4
Compare
|
Thanks, Ben. That diagnosis is right and I've confirmed it against the pinned client: I've shrunk the fix to where you pointed:
The retained-ID Also noting we both touch |
|
Closing this as superseded, and I want to be clear that's about timing rather than the work. Your second push was exactly the shrink I asked for, and the diagnosis behind it was right. What happened is that #6325 landed after your last push here and fixed the same blind spot on : m.content && typeof m.content === "object"
? JSON.stringify(m.content)
: 0;I ran your reproduction test — On the rest of the diff: I'd rather not carry
There is one part of this PR that is still a real fix. Adding The old renderer stays on screen. Your one-liner fixes it. Thanks for the digging on this one — the |
Summary
This fixes stale A2UI activity content in
CopilotChatwhenACTIVITY_DELTAupdates an existing activity object without changing its message ID.The
CopilotChatmessage memo key now hashes object content and includes the activity type, so a retained-ID delta produces a new key and the memoized boundary re-renders. Existing string, array, role, message-ID, and tool-call grouping behavior stays intact. A2UI event handling and backend protocol behavior remain unchanged.Fixes #6001
Changes
activityTypein theCopilotChatmessage memo key so a retained-IDACTIVITY_DELTAinvalidates the memoized messages array.getContentMemoKeyreturns a deterministic hash for serializable object content, a stable value for immutable primitives, and a fail-safe fresh value for unsupported or cyclic content, so the memo can always invalidate.ACTIVITY_DELTAsequence, the retained activity-type switch, and unchanged-serializable-content memoization.release.config.jsonand Nx release convention; no Changeset is included.Test Plan
pnpm -C packages/react-core exec vitest run src/v2/components/chat/__tests__/CopilotChatToolRerenders.e2e.test.tsxpnpm exec oxfmt --check packages/react-core/src/v2/components/chat/CopilotChat.tsx packages/react-core/src/v2/components/chat/content-memo-key.ts packages/react-core/src/v2/components/chat/__tests__/CopilotChatToolRerenders.e2e.test.tsxpnpm exec oxlint packages/react-core/src/v2/components/chat/CopilotChat.tsx packages/react-core/src/v2/components/chat/content-memo-key.ts packages/react-core/src/v2/components/chat/__tests__/CopilotChatToolRerenders.e2e.test.tsxpnpm -C packages/react-core exec tsc --noEmitpnpm exec commitlint --from origin/main --to HEADACTIVITY_DELTA.release.config.jsonand Nx release; no Changeset is included.