Skip to content

fix(react-core): keep all tool calls when merging duplicate messages - #8408

Merged
tylerslaton merged 2 commits into
CopilotKit:mainfrom
miah7:fix/3644-merge-duplicate-tool-calls
Nov 16, 2026
Merged

tylerslaton merged 2 commits into
CopilotKit:mainfrom
miah7:fix/3644-merge-duplicate-tool-calls

Conversation

@miah7

@miah7 miah7 commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes the remaining half of #3644: when two copies of an assistant message share an id, the chat view dropped the tool calls held only by the earlier copy. The original cause was in @ag-ui/client: up to 0.0.51, TOOL_CALL_START attached a tool call to its parent only if the parent was the last message. A TOOL_CALL_RESULT between two tool calls for the same parent therefore created a second assistant message with the same id. Upstream fixed this in 0.0.52 (2026-04-08), and CopilotKit has used a fixed version since a5c1137465. I reproduced the reporter's event sequence against an old and a current client to confirm this.

deduplicateMessages in CopilotChatMessageView.tsx already merges duplicate assistant messages (it recovers wiped content and undefined toolCalls), but when both copies had tool calls it kept only the later copy's list. For the reporter's sequence — [tool_a, tool_b] on the first copy and [tool_c] on the second — only tool_c rendered. Duplicates can still reach the view from older clients, custom agents, or stored history.

  • When both copies carry tool calls, deduplicateMessages now keeps the calls from both. The existing rules are unchanged: undefined recovers the earlier list, and an explicit [] is kept.
  • A new mergeToolCalls helper combines the lists: for a tool call in both copies, a later copy with arguments wins, and a blank copy never replaces a populated one. dedupeToolCalls is unchanged, so within a single message the first populated copy still wins (TOOL_CALL_ARGS deltas go to the first copy).
  • Moved the deduplicateMessages doc comment back above its function; an earlier change had placed dedupeToolCalls between them.
  • Four new deduplicateMessages tests: the reporter's scenario (all three tool calls survive and the content is kept), a blank later copy does not replace a populated one, a later populated copy wins when both are populated, and within one message a complete copy is kept over a stale later copy.

This builds on #4059, which proposed combining the lists but merged shared ids with "later wins". As noted when #4058 was closed, the blank copy of a duplicated tool call usually arrives second, so this PR keeps the existing "prefer the populated copy" protection.

Related PRs and Issues

Checklist

  • I have read the Contribution Guide
  • If the PR changes or adds functionality, I have updated the relevant documentation
  • If the PR contains a breaking change, the title has ! (for example feat(react-core)!: ...) and the description has a BREAKING CHANGE: line with migration steps. See VERSIONING.md
  • "Allow edits by maintainers" is checked (lets us help iterate on your PR directly — faster turnaround for everyone)

deduplicateMessages merged two copies of an assistant message by taking
the later copy's toolCalls, so tool calls held only by the earlier copy
were dropped from the chat. When both copies carry tool calls, keep the
calls from both. dedupeToolCalls now lets a populated copy replace an
earlier one, while a blank copy still never replaces a populated one,
so merged streaming updates keep their freshest arguments.

Also moves the deduplicateMessages doc comment back above its function.

Fixes CopilotKit#3644

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@coderabbitai

coderabbitai Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Duplicate assistant messages now merge tool calls by ID when both messages contain calls. Populated arguments are preserved over blank copies. Tests cover duplicate IDs within a message and across duplicate messages.

Changes: Assistant Message Merge

Layer / File(s) Summary
Merge and deduplicate tool calls
packages/react-core/src/v2/components/chat/CopilotChatMessageView.tsx, packages/react-core/src/v2/components/chat/__tests__/CopilotChatMessageView.test.tsx
Duplicate assistant messages merge tool calls by ID when both contain calls. A later call replaces an earlier one only when it has arguments or the earlier call has none. Tests cover within-message duplicates and merges across assistant messages.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Merge Risk: Minimal · up to 739cd

This change keeps tool calls from both copies of a duplicate assistant message, so none are lost in the chat view. The change is localized and has test coverage, and no merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: tool calls from duplicate messages are now kept instead of being discarded. The changeset modifies deduplicateMessages in CopilotChatMessageView.tsx to merge tool call…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Full details: Title check

Explanation

The title names the main change: tool calls from duplicate messages are now kept instead of being discarded. The changeset modifies deduplicateMessages in CopilotChatMessageView.tsx to merge tool calls by ID across duplicate assistant-message occurrences, and adds tests for this behavior. The title is specific, follows the conventional commit format, and reflects the primary fix.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 44b265b and 739cd07.

Files selected for processing: 2

  • packages/react-core/src/v2/components/chat/CopilotChatMessageView.tsx
  • packages/react-core/src/v2/components/chat/__tests__/CopilotChatMessageView.test.tsx

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additional comments: 1

packages/react-core/src/v2/components/chat/CopilotChatMessageView.tsx-370-370: 1

370-370: Major · Functional Correctness · Quick win

Unverified finding
Verification ran but could not confirm this finding. It is shown for review, not as a verified issue.

Keep first-match precedence within a single message.

For restored tool calls [tc-1, tc-1] with arguments '{"symptom":', TOOL_CALL_ARGS in @ag-ui/client 1.0.2 updates only the first entry. After delta '"headache"}', the first entry is complete, but the second remains partial. (raw.githubusercontent.com)

Line 370 selects that stale second entry because its arguments are nonempty. The single-message normalization path therefore discards completed arguments.

Keep first-populated selection within each message occurrence. Apply later-occurrence precedence only when merging distinct occurrences. Add a regression case where streaming advances the first duplicate.

Review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 7b88608 and 44b265b.

Files selected for processing: 2

  • packages/react-core/src/v2/components/chat/CopilotChatMessageView.tsx
  • packages/react-core/src/v2/components/chat/__tests__/CopilotChatMessageView.test.tsx

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

TOOL_CALL_ARGS deltas go to the first copy of a tool call id, so within
one message a later copy can hold stale partial arguments. Restore the
original dedupeToolCalls rule (first populated copy wins) and apply
later-copy precedence only when merging separate occurrences, through a
new mergeToolCalls helper.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@tylerslaton
tylerslaton disabled auto-merge November 16, 2026 20:45
@tylerslaton
tylerslaton merged commit abe996f into CopilotKit:main Nov 16, 2026
2 checks passed
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: Duplicate assistant message IDs when multiple tool calls are interleaved with tool results

2 participants