Skip to content

MESSAGES_SNAPSHOT + addToolOutput #859

Closed

Description

@danciudev

TanStack AI version

Lastest

Framework/Library version

React 19

Describe the bug and the steps to reproduce it

addToolOutput() doesn't work properly when the chat contains a MESSAGES_SNAPSHOT-type message. If I delete it, I can add output without any problems.

Your Minimal, Reproducible Example - (Sandbox Highly Recommended)

Screenshots or Videos (Optional)

No response

Do you intend to try to help solve this bug with your own PR?

None

Terms & Code of Conduct

  • I agree to follow this project's Code of Conduct
  • I understand that if my bug cannot be reliable reproduced in a debuggable environment, it will probably not be fixed and this issue may even be closed.

Activity

  1. NovolineGast commented on Jun 30, 2026

    @NovolineGast
    Contributor

    Hi, I've been digging into this and reproduced two related breakage modes. Want to confirm the intended fix direction before opening a PR.

    Root cause

    MESSAGES_SNAPSHOT (processor.ts handleMessagesSnapshotEvent) replaces this.messages with chunk.messages.map(aguiSnapshotMessageToUIMessage). The normalisation in messages.ts maps an AG-UI role: 'tool' snapshot message to a standalone UIMessage whose only part is a tool-result — it is NOT anchored into the preceding assistant message. So after a snapshot, the assistant message that originally carried the streaming tool-call part is gone, and the tool-result lives in a detached assistant message with no tool-call part.

    Repro A — addToolResult silently no-ops (the reported bug)

    Stream a client-side tool call (TOOL_CALL_START/ARGS/END -> assistant msg with tool-call part tc-1), then receive a snapshot that re-describes the assistant turn with only content (no toolCalls) plus a separate role: 'tool' message for tc-1. After the snapshot, no message contains a tool-call part with id tc-1, so:

    [StreamProcessor] Could not find message with tool call tc-1
    

    and addToolResult('tc-1', ...) no-ops. Removing the snapshot from the stream (the reporter's "if I delete it") leaves the streamed assistant message intact, so addToolResult works — matches the report exactly.

    Repro B — duplicate tool-result when both shapes are present

    If the snapshot's assistant message does carry toolCalls AND a separate role: 'tool' message exists, addToolResult succeeds but produces two tool-result parts for the same toolCallId (one anchored in the assistant msg, one in the detached assistant msg). convertMessagesToModelMessages dedups the LLM-bound model messages, but the UI array carries the duplicate.

    Proposed fix (two options, need your call)

    A) Anchor role: 'tool' snapshot messages into the preceding assistant message during aguiSnapshotMessageToUIMessage / handleMessagesSnapshotEvent — mirror what modelMessagesToUIMessages already does for the streaming fan-out. This fixes B and keeps the tool-result attached, but in Repro A it still can't reconstruct the missing tool-call part (the tool wire message only carries toolCallId + content, not name/args), so addToolResult still can't find tc-1.

    B) Preserve in-flight tool-call parts across the snapshot. In handleMessagesSnapshotEvent, after normalising the snapshot, re-attach any tool-call parts from the pre-snapshot messages whose id is referenced by a snapshot tool-result (or simply not yet complete and not represented in the snapshot). This fully fixes A but is a bigger change and changes snapshot semantics (snapshot is no longer a pure replacement while client tools are pending).

    I can implement either. A is smaller and consistent with the existing fan-out dedup; B is the only thing that actually fixes the reported addToolResult no-op. Which direction do you want? (Or is there a third — e.g. declaring that servers must include toolCalls on snapshot assistant messages and documenting that?)

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