[react-ag-ui] Reasoning streamed after a tool result ends up on the tool-call message, and is lost after RUN_FINISHED

Author: sjmattaCreated Sep 25, 2026Updated Sep 25, 2026
Labelsbug

Checklist

  • I have searched existing issues and this is not a duplicate
  • This is a reproducible bug, not a usage question

Environment info

Packages:
  @assistant-ui/react          0.15.22
  @assistant-ui/react-ag-ui    0.0.62
  @ag-ui/client                0.0.59
Also reproduced on main at 877d3da94.

Affected area(s)

Runtime / Core

Runtime adapter in use

useAgUiRuntime

Steps to reproduce

A run with two model steps, where the first step calls a tool and the second step reasons and then answers. This is the normal shape of a tool turn from agents that stream reasoning (we hit it with ag-ui-strands 0.4.1):

  1. REASONING_* for r1, then TOOL_CALL_START with parentMessageId: "a1", args, TOOL_CALL_END, TOOL_CALL_RESULT
  2. REASONING_* for r2
  3. TEXT_MESSAGE_START / CONTENT / END for a2
  4. RUN_FINISHED

Test against AgUiThreadRuntimeCore, trimmed from the one I ran on main (helpers only feed the subscriber callbacks in order):

typescript
const core = new AgUiThreadRuntimeCore({
  agent: {
    runAgent: vi.fn(async (_input, s) => {
      reason(s, "r1", "plan the call");
      s.onToolCallStartEvent?.({ event: { type: "TOOL_CALL_START", toolCallId: "c1", toolCallName: "lookup", parentMessageId: "a1" } });
      s.onToolCallArgsEvent?.({ event: { type: "TOOL_CALL_ARGS", toolCallId: "c1", delta: "{}" } });
      s.onToolCallEndEvent?.({ event: { type: "TOOL_CALL_END", toolCallId: "c1" } });
      s.onToolCallResultEvent?.({ event: { type: "TOOL_CALL_RESULT", toolCallId: "c1", messageId: "t1", content: "ok", role: "tool" } });
      reason(s, "r2", "write the answer");
      say(s, "a2", "Done.");
      s.onRunFinishedEvent?.({ event: { type: "RUN_FINISHED", threadId: "t", runId: "r" } });
      s.onRunFinalized?.({});
    }),
  } as unknown as HttpAgent,
  logger: makeLogger(),
  showThinking: true,
  notifyUpdate: () => {},
});
await core.append(userMessage("hi"));

Expected vs actual behavior

Expected: a1 holds the r1 reasoning and the tool call, and a2 holds the r2 reasoning and the text.

Actual, reasoning parts per assistant message:

[["a1", ["plan the call", "write the answer"]], ["a2", []]]

r2 is shown on the tool-call message instead of the answer.

If the agent also sends MESSAGES_SNAPSHOTs that place r2 before a2 (as ag-ui-strands can), the snapshot fixes the attribution, but RUN_FINISHED then wipes it:

before RUN_FINISHED  [["a1", ["plan the call"]], ["a2", ["write the answer"]]]
after RUN_FINISHED   [["a1", ["plan the call"]], ["a2", []]]

In that case the step's reasoning is gone from the thread, and it isn't sent back in the next run's input either.

Code snippet or error log

The cause looks like RunAggregator.beginDistinctTextMessage (src/runtime/adapter/run-aggregator.ts):

  1. TOOL_CALL_START.parentMessageId goes through reportServerMessageId, which sets lastTextMessageId = "a1".
  2. TEXT_MESSAGE_START for a2 is a distinct id, so beginDistinctTextMessage calls resetMessageParts(), which clears reasoningParts, including r2, which streamed for the new message.
  3. onTextMessageStart -> adoptServerMessageId(a2, true) marks a1 complete with the content it last emitted, which already had r2 appended.
  4. On RUN_FINISHED the aggregator emits a2 without r2, and updateAssistantMessage replaces the content that the snapshot had set.

Additional context

The reasoning that trails the previous message's last text or tool part at the moment a new text message starts belongs to the new message. I have a fix along those lines with a regression test and will open a PR.

Track in Rupic

Source: assistant-ui/assistant-ui