Skip to content

[Bug]: AG-UI TOOL_CALL_RESULT reuses replyId as messageId, causing duplicate tool-message IDs when multiple tools are called in one reply #2907

Closed

Description

@resomi

Describe the bug

When an agent issues multiple tool calls within a single assistant reply, the
AG-UI adapter emits one TOOL_CALL_RESULT event per tool result, but all of
them share the same messageId (the reply's replyId).

The frontend (@ag-ui/client) creates one role:"tool" message per
TOOL_CALL_RESULT keyed by messageId, so multiple tool results end up with
the same message id. CopilotKit's CopilotChatMessageView deduplicates by
message.id before rendering and keeps only the last one — so any earlier tool
result that has a frontend renderer is silently dropped.

Environment

  • agentscope-java: 2.0.3-SNAPSHOT (also present in 2.0.2)
  • frontend: CopilotKit React v2 / @ag-ui/client

Observed event stream

For a single assistant reply that called two tools (memory_save +
show_note_panel), the SSE stream contains two TOOL_CALL_RESULT events with
the same messageId but different toolCallId:

TOOL_CALL_RESULT messageId=3b69460e5de441c2923b22a7deec4ec4 toolCallId=call_737273cf24ec4878a6417b07
TOOL_CALL_RESULT messageId=3b69460e5de441c2923b22a7deec4ec4 toolCallId=call_53a7ca73c039475a9e1b14a9

Root cause

AguiStreamContext.endToolResult passes replyId as the messageId:

emit(new AguiEvent.ToolCallResult(threadId, runId, toolCallId, content, "tool", replyId));

toolCallId is unique per tool call, but replyId is shared across all tool
calls in a single reply. With one tool call per reply this is harmless; with
2+ tool calls in the same reply the ids collide.

Reproduce

  1. Run an agent that calls two tools within one reply.
  2. Inspect the SSE stream: two TOOL_CALL_RESULT events with the same
    messageId but different toolCallId.
  3. CopilotKit dev console logs:
    CopilotChatMessageView: Merged 1 message(s) with duplicate IDs.

Expected behavior

Each TOOL_CALL_RESULT should carry a unique messageId so every tool result
renders independently.

Related

Activity

  1. steven-ji commented on Aug 30, 2026

    @steven-ji
    Contributor

    Hi, I'd like to work on this.

    I confirmed on current main that AguiStreamContext.endToolResult() uses the shared replyId as ToolCallResult.messageId, while the existing parallel-tool coverage does not assert result message IDs.

    I plan to keep the PR focused on the v2 AguiStreamContext path: emit a deterministic per-tool result message ID and add regression coverage proving that two tool results from the same reply have distinct message IDs. I will leave the legacy adapter path unchanged unless maintainers prefer parity in this PR.

  2. dragondyt commented on Aug 30, 2026

    @dragondyt
    Contributor

    This PR can solve the messageId duplication issue mentioned above, but I think we should fix the replyId at its source instead. Since AGUI is only at the application layer, if the core remains unchanged, other protocols might have problems.

  3. steven-ji commented on Aug 30, 2026

    @steven-ji
    Contributor

    Thanks, that's a fair concern. I checked the core flow before choosing the adapter-level fix.

    replyId is generated once per acting batch and is intentionally shared by all tool-result events in that batch; the code also describes it as an event-correlation identifier. Each AgentEvent already has its own unique id, while toolCallId identifies the individual tool call.

    AG-UI materializes every tool result as a separate ToolMessage, whose message id is distinct from its toolCallId. The collision happens when the adapter uses the batch-level replyId directly as that message ID. This PR derives the ID from both values, so it preserves reply correlation while remaining unique per tool result.

    I also checked the current non-AG-UI consumers I could find. They correlate tool results using toolCallId and/or the event ID or sequence, so I haven't found evidence of the same collision outside AG-UI.

    For that reason, I'd prefer to keep this PR scoped to the adapter. A canonical core-level message ID or helper could still be a useful follow-up, but changing replyId to be unique per tool would change its current semantics.

    Were you suggesting adding a separate message identifier in core, rather than changing the meaning of replyId itself?

  4. added a commit that references this issue on Aug 31, 2026
    4350720
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions