[react-ag-ui] Regenerating the answer after a tool call deletes the earlier answer when the run starts with a MESSAGES_SNAPSHOT

Author: sjmattaCreated Sep 26, 2026Updated Sep 26, 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; the files involved are unchanged on c978a2289.

Affected area(s)

Runtime / Core

Runtime adapter in use

useAgUiRuntime

Steps to reproduce

  1. Run 1: the agent calls a tool (TOOL_CALL_START with parentMessageId: "a1", then the result), then answers with text message b1.
  2. Press Reload on b1. The new run starts from a1, so its input ends with the tool result.
  3. Run 2 starts with a MESSAGES_SNAPSHOT that echoes that input (u1, a1, t1), then streams b2. ag-ui-strands 0.4.1 sends a snapshot like this at the start of every run.
typescript
// Trimmed from a vitest test against AgUiThreadRuntimeCore; userMessage() builds a plain AppendMessage.
const user = { id: "u1", role: "user", content: "hi" };
const call = { id: "a1", role: "assistant", toolCalls: [{ id: "c1", type: "function", function: { name: "lookup", arguments: "{}" } }] };
const result = { id: "t1", role: "tool", toolCallId: "c1", content: "ok" };
let run = 0;
const runAgent = vi.fn(async (_input, s) => {
  run += 1;
  if (run === 1) {
    s.onToolCallStartEvent?.({ event: { type: "TOOL_CALL_START", toolCallId: "c1", toolCallName: "lookup", parentMessageId: "a1" } });
    s.onToolCallEndEvent?.({ event: { type: "TOOL_CALL_END", toolCallId: "c1" } });
    s.onToolCallResultEvent?.({ event: { type: "TOOL_CALL_RESULT", toolCallId: "c1", messageId: "t1", content: "ok" } });
  } else {
    s.onMessagesSnapshotEvent?.({ event: { type: "MESSAGES_SNAPSHOT", messages: [user, call, result] } });
  }
  s.onTextMessageStartEvent?.({ event: { type: "TEXT_MESSAGE_START", messageId: `b${run}` } });
  s.onTextMessageContentEvent?.({ event: { type: "TEXT_MESSAGE_CONTENT", messageId: `b${run}`, delta: `answer ${run}` } });
  s.onTextMessageEndEvent?.({ event: { type: "TEXT_MESSAGE_END", messageId: `b${run}` } });
  s.onRunFinishedEvent?.({ event: { type: "RUN_FINISHED", threadId: "t", runId: `r${run}` } });
  s.onRunFinalized?.({});
});
const core = new AgUiThreadRuntimeCore({ agent: { runAgent } as unknown as HttpAgent, logger: makeLogger(), showThinking: true, notifyUpdate: () => {} });

await core.append(userMessage("hi"));
await core.reload("a1");
console.log(core.getMessageRepository().messages.map((e) => [e.message.id, e.parentId]));

Expected vs actual behavior

Expected: b1 and b2 are siblings under a1, and the branch picker on the answer shows 2/2.

Actual:

[["u1", null], ["a1", "u1"], ["b2", "a1"]]

b1 is deleted from the repository, so the earlier answer can't be reached. Without the snapshot in run 2, the same test keeps b1 next to b2. Reloading from a user message is not affected.

Code snippet or error log

  1. reload("a1") starts a run with an optimistic assistant placeholder under a1.
  2. importMessagesSnapshot does not keep that placeholder, because it is optimistic and the snapshot already ends with an assistant message (converted.at(-1)?.role === "assistant").
  3. applyExternalMessages merges u1 and a1, then calls this.session.resetHead(lastAppliedId) with a1.
  4. MessageRepository.resetHead deletes every descendant of the new head, which removes b1.

When the reload starts from a user message, the snapshot ends with that user message, so the placeholder is kept and becomes the head, and nothing is deleted.

Additional context

#8347, #8180 and #8238 are about resetHead deleting descendants on other paths (repository import, mid-run export, the local runtime). This one goes through applyExternalMessages in react-ag-ui, which calls resetHead directly, so a fix for #8347 in import() may not cover it.

Track in Rupic

Source: assistant-ui/assistant-ui