Skip to content

fix(cloudflare-agents): snapshot every step's assistant message in order - #3192

Closed
rudycelekli wants to merge 1 commit into
ag-ui-protocol:mainfrom
rudycelekli:fix/cloudflare-agents-multistep-snapshot
Closed

rudycelekli wants to merge 1 commit into
ag-ui-protocol:mainfrom
rudycelekli:fix/cloudflare-agents-multistep-snapshot

Conversation

@rudycelekli

Copy link
Copy Markdown
Contributor

What's wrong

AgentsToAGUIAdapter.adaptStreamToAGUI builds its closing MESSAGES_SNAPSHOT from three run-wide accumulators: one textContent string, one toolCalls list and one toolMessages list. It emits them as a single assistant message, with the id of whichever text message was streamed last, followed by all the tool results.

That only works for single-step runs. In a multi-step AI SDK run, for example a server tool with stopWhen/maxSteps, the stream is:

text-delta "Let me check."   -> TEXT_MESSAGE_START M1
tool-call tc1                -> TOOL_CALL_START tc1 (parentMessageId M1)
tool-result tc1              -> TOOL_CALL_RESULT
text-delta "It is sunny."    -> TEXT_MESSAGE_START M2

The snapshot on main is:

[ user, { id: M2, assistant, content: "Let me check.It is sunny.", toolCalls: [tc1] }, { tool, tc1 } ]

Because the client replaces its message list with the snapshot, this causes three problems:

  • Message M1, which the client already rendered, disappears.
  • M2 shows both steps' text run together.
  • The tool result follows the final answer. On the next turn the model gets a history in which the answer comes before the tool call's result.

The fix

The snapshot is now built in stream order:

  • one assistant message per text message, holding the calls whose TOOL_CALL_START named it as parent;
  • the tool results placed where they occurred.

For calls streamed through tool-input-*, the parent is recorded at tool-input-start, so it matches the emitted event. A run with no text still gets one assistant message, filled from stream.text as before. Single-step snapshots are unchanged, and every existing snapshot test passes as is.

Test

A new case in src/adapter.test.ts, "MESSAGES_SNAPSHOT keeps each step's assistant message in stream order", runs the sequence above. It expects [user, assistant M1 ("Let me check.", toolCalls [tc1]), tool tc1, assistant M2 ("It is sunny.")], with ids matching the emitted TEXT_MESSAGE_START and TOOL_CALL_RESULT events.

  • On main it fails.
  • With the fix it passes.
  • Mutation check: with only src/adapter.ts reverted and the test kept, the named test fails again (23 others pass).

Checks run (integrations/community/cloudflare-agents/typescript)

  • vitest run: 3 files, 46 tests passed
  • tsc --noEmit: clean

🤖 Generated with Claude Code

MESSAGES_SNAPSHOT was built from one running text buffer, one tool-call
list and one tool-result list, with the assistant message given the id of
the LAST text message. In a multi-step run (text, tool call, tool result,
more text) the snapshot dropped the first assistant message the client
had already rendered. It put all text from every step into the last
message with no separator, attached the earlier step's tool calls to it,
and placed the tool results after the final answer. The client replaced
its history with that, so the next turn sent the model a reordered,
merged conversation.

Build the snapshot in stream order: one assistant message per text
message (holding the calls whose TOOL_CALL_START named it as parent),
with the tool results where they occurred.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@rudycelekli
rudycelekli requested a review from a team as a code owner October 23, 2026 15:12
@NathanTarbert

Copy link
Copy Markdown
Contributor

Hey @rudycelekli,

Apologies for the delayed response. Nothing blocking from me on this one.

Text, then a tool call, its result, then more text now comes out of the snapshot in that order, with each step's text in its own assistant message. A tool call in a later step with no new text stays attached to the earlier message, matching its parentMessageId, and single-step output is unchanged.

@contextablemark, this one looks ready for a maintainer review.

@mme

mme commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

Thanks @rudycelekli, this fix is right. It's included in #3365 as your own commit (authorship kept), which reworks @ag-ui/cloudflare-agents for the current Agents SDK and AI SDK v6/v7. Your multi-step snapshot test is kept there and passes against the rewritten stream translator, which builds the snapshot in stream order the same way. Once #3365 merges this PR can be closed as superseded.

@mme

mme commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Thanks for this fix! It landed as part of #3365, which reworks the adapter for the current Cloudflare Agents SDK: your commit is included there with your authorship, and every step's assistant message is now snapshotted in order. Closing this one in favour of #3365; the change ships in the next @ag-ui/cloudflare-agents release.

@mme mme closed this Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants