[react-ag-ui] Reasoning streamed after a tool result ends up on the tool-call message, and is lost after RUN_FINISHED
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):
REASONING_*forr1, thenTOOL_CALL_STARTwithparentMessageId: "a1", args,TOOL_CALL_END,TOOL_CALL_RESULTREASONING_*forr2TEXT_MESSAGE_START/CONTENT/ENDfora2RUN_FINISHED
Test against AgUiThreadRuntimeCore, trimmed from the one I ran on main (helpers only feed the subscriber callbacks in order):
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):
TOOL_CALL_START.parentMessageIdgoes throughreportServerMessageId, which setslastTextMessageId = "a1".TEXT_MESSAGE_STARTfora2is a distinct id, sobeginDistinctTextMessagecallsresetMessageParts(), which clearsreasoningParts, includingr2, which streamed for the new message.onTextMessageStart->adoptServerMessageId(a2, true)marksa1complete with the content it last emitted, which already hadr2appended.- On
RUN_FINISHEDthe aggregator emitsa2withoutr2, andupdateAssistantMessagereplaces 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.
Source: assistant-ui/assistant-ui