End the open message part in UI event streams when a restarted part's start arrives

Merged
DO
7d ago
+310-12

Based on #11736 by @rahul05ranjan, whose commits are kept.

A retried durable activity republishes the model events it already published, so UIEventStream.transform_stream can see the first attempt's PartStartEvent and deltas followed by the retry's PartStartEvent with no PartEndEvent in between. The stream then overwrote its open-part tracking and never ended the first part: AG-UI's verifyEvents fails the run with Cannot send 'RUN_FINISHED' while text messages are still active, and the AI SDK leaves the part streaming.

A PartStartEvent now ends the part still tracked as open before the new part starts. _end_open_part() is the one implementation of that close, shared with the error path, which already did the same cleanup for a stream that failed mid-part. A well-formed stream ends each part before the next one starts, so nothing changes for it.

What the client sees

The first attempt's partial part ends as a part of its own, followed by the retry's full part. Deduplicating a retried attempt's events is a separate change (the stream-attempts work in #14378). tests/test_ui.py snapshots the exact events per protocol and part kind:

  • text and thinking: two parts on both protocols, the first holding the partial content.
  • tool call, AG-UI: TOOL_CALL_START / args / TOOL_CALL_END for the first attempt, then the same toolCallId started again with the full args.
  • tool call, Vercel AI: two tool-input-start chunks for the same toolCallId, since a tool call's input closes at tool-input-available and the synthetic end emits no chunk.

I fed all six streams through the real clients from tests/ui_roundtrip (@ag-ui/client 1.0.4 runAgent(), AI SDK 6 readUIMessageStream). All six pass, while the unfixed AG-UI text stream fails verifyEvents with the error above. Both clients collapse the restarted tool call into a single call with input {"tz": "UTC"}, so the duplicate Vercel tool-input-start does no harm there.

Checklist

  • No breaking changes in accordance with the version policy.
  • Any permitted compatibility impact has a label, warning, migration and release note, and an exact API-check waiver if the gate reports it.
  • PR title is fit for the release changelog.

šŸ¤– Generated with Claude Code


Summary by cubic

Ends the UI event stream's open message part when a retried durable activity republishes a part start, so clients no longer see a part stuck streaming and AG-UI's verifyEvents no longer fails the run.

  • A PartStartEvent that arrives while another part is open now emits the open part's end before the new start.
  • _end_open_part() is the single implementation of that cleanup, shared with the error path.
  • Tests cover text, thinking, and tool-call parts under both AG-UI and Vercel AI encoders, including the expected duplicate Vercel tool-input-start for a restarted tool call.
  • No breaking changes; the PR title and changelog entry are already set.