Skip to content

[Bug]: assistant text messageId changes across text→tool→text within one reply #4067

Open

Description

@chcodex

Summary

AG-UI clients aggregate assistant text by messageId. Within one assistant turn (reply), a text → tool → text sequence is a continuation of the same assistant message and should use one stable text messageId, derived from the assistant turn identity the session log already records.

Related: ag-ui-protocol/ag-ui#1580.

This is a bug fix, not a feature request: no opt-in/opt-out configuration. The previous per-model-call text id is incorrect for AG-UI message aggregation.

Current behavior

  • ReActAgent.buildAgentStream() computes one turnId per assistant turn and stores it in RuntimeContext (SessionRecorder.TURN_ID_KEY). It builds an ExecutionIdentity(agentId, sessionId, turnId, runId) and attaches it to every non-forwarded AgentEvent, so event.getExecution().turnId() is available on the live stream. The same turnId is recorded on every SessionEvent.
  • ReActAgent.buildIdentifiedAgentStream() generates a separate replyId UUID for AgentStartEvent / AgentEndEvent. That id is not the session fact, is not persisted, and is not available on non-AG-UI call paths.
  • modelCallStream() creates a new callId per model call and passes it to preparedModelCallStream(...); ModelCallBlockLifecycle then emits TextBlockStartEvent / TextBlockDeltaEvent / TextBlockEndEvent with that callId as their replyId.
  • The text converter therefore derives the text messageId from a per-model-call id. Text before and after a tool call gets different ids, and AG-UI clients render two assistant text messages.
  • Downstream applications currently work around this by pinning the first text id for the whole run (for example StableAguiMessageEventConverter in copilot-api).

Expected behavior

  1. Pin one text messageId per assistant turn using the turn identity the session log already records: ExecutionIdentity.turnId on live events, SessionEvent.turnId on replay. Do not use AgentStartEvent.replyId or the per-call TextBlockDeltaEvent.replyId.
  2. Reuse it across text → tool → text; a tool call in the middle must not change the text message id.
  3. Keep per-segment boundaries: each text segment still emits its own TEXT_MESSAGE_START / TEXT_MESSAGE_END with the pinned id.
    • Per the AG-UI spec (1.0 and the current draft), a TEXT_MESSAGE_START for an already-open id is a malformed sequence and fatal to the run.
    • A closed message is closed, not sealed: a producer MAY reopen the same messageId with a new TEXT_MESSAGE_START, and further content appends to what is already there. A reopening START MUST agree with the message it reopens — the same owner, role and name.
    • Do not rely on a repeated START being a client no-op: either re-START after END with the pinned id, or defer END until the assistant turn ends.
  4. Reasoning messages keep an independent namespace: one assistant turn uses <turnId>:reasoning, reopened across reasoning segments, so live, replay and persisted history agree on a single reasoning message id. Reasoning must never reuse the bare text id.
  5. Sub-agent / forwarded text and text from different roles must not share the parent's pinned id.
  6. The replay path (AguiSessionPresentation) must use the same rule, so a client that reconnects sees one stable id across replay and live frames.
  7. No new legacy flag. Applications that need the old behavior can stay on the previous release or provide a custom AgentEventConverter.

Why turnId

  • It already exists: ExecutionIdentity.turnId on live events, SessionEvent.turnId on the log, and chat_message.turnId downstream.
  • It is path-independent: A2A, plain GraphQL and AG-UI all go through the same SessionRecorder, so history export does not depend on an AG-UI-only id.
  • It requires no schema change, no Msg.id change, and no data migration.
  • replyId is generated independently from turnId and never enters the session log; using it would make AG-UI ids and persisted history diverge.

Implementation sketch

  • AguiStreamContext records event.getExecution().turnId() from non-forwarded lifecycle events and pins the text id to it. Keep the existing replyId/blockId fallback for streams without an execution identity (synthetic or forwarded events).
  • TextBlockEventConverter uses the pinned text id for TEXT_MESSAGE_START/CONTENT/END.
  • AguiSessionPresentation uses SessionEvent.turnId() for text and <turnId>:reasoning for reasoning, identical to the live path.
  • Forwarded/sub-agent frames must not inherit the parent id.
  • Do not add public opt-in/opt-out configuration.

Acceptance

  • Given one reply with text -> tool_call -> text, the SSE stream emits the same text messageId for both text segments, and that id equals event.getExecution().turnId().
  • Each text segment has a matched TEXT_MESSAGE_START / TEXT_MESSAGE_END.
  • Reasoning uses <turnId>:reasoning and never the bare text id.
  • Replay and live frames for the same segment carry the same messageId.
  • Sub-agent/forwarded text does not reuse the parent text id.
  • An official @ag-ui/client applied to the run reports one assistant text message, not two.
  • No custom AgentEventConverter is needed downstream.

Context

Found while integrating @ag-ui/client 1.0.3 against an AgentScope Java backend. The downstream StableAguiMessageEventConverter workaround can be removed once the framework does this by default, except for the application-level <messageId>:reasoning mapping, which is a persistence convention.

References:

Activity

  1. changed the title [-]fix(agui): keep one text messageId across text→tool→text in a run[/-] [+]fix(agui): keep one text messageId across text→tool→text in an assistant turn[/+] on Nov 8, 2026
  2. changed the title [-]fix(agui): keep one text messageId across text→tool→text in an assistant turn[/-] [+][Bug]: assistant text messageId changes across text→tool→text within one reply[/+] on Nov 8, 2026
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