Skip to content

Fix message ordering in workflow-hosted agents#7123

Open
peibekwe wants to merge 1 commit into
microsoft:mainfrom
peibekwe:peibekwe/workflows-bugfix
Open

Fix message ordering in workflow-hosted agents#7123
peibekwe wants to merge 1 commit into
microsoft:mainfrom
peibekwe:peibekwe/workflows-bugfix

Conversation

@peibekwe

Copy link
Copy Markdown
Contributor

Motivation & Context

When a handoff workflow was used as an agent, non-streaming responses and saved history could move a tool result away from its matching tool call. This produced invalid conversation history, potentially causing provider errors on later turns, while streaming output appeared correct and hid the problem.

Description & Review Guide

Fixes handoff tool results being separated from their matching tool calls. Messages now preserve their produced order within each agent response while keeping concurrent responses grouped correctly.

Fixes #5720

Contribution Checklist

  • The code builds clean without any errors or warnings
  • All unit tests pass, and I have added new tests where possible
  • The PR follows the Contribution Guidelines
  • This PR is linked to an issue and there is no other open PR for this issue (see Related Issue above).
  • This is not a breaking change. If it is a breaking change, add the breaking change label (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.

@peibekwe peibekwe self-assigned this Jul 14, 2026
Copilot AI review requested due to automatic review settings July 14, 2026 23:32

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes message ordering for workflow-hosted agents (especially handoff workflows) so that tool results stay adjacent to their corresponding tool calls in both non-streaming responses and persisted chat history, matching the produced order seen during streaming.

Changes:

  • Update MessageMerger to preserve first-seen ordering of messages within a response and keep multi-response output grouped contiguously in first-seen response order.
  • Ensure injected handoff tool results in HandoffAgentExecutor carry the originating ResponseId, so merging/history logic can keep them with the correct response.
  • Add unit tests covering ordering guarantees for message IDs, identifierless segments, and handoff call/result ordering across streaming vs non-streaming and history.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
dotnet/src/Microsoft.Agents.AI.Workflows/MessageMerger.cs Reworks merge state to keep message and response ordering stable by first observation, preventing tool call/result separation.
dotnet/src/Microsoft.Agents.AI.Workflows/Specialized/HandoffAgentExecutor.cs Tags synthesized handoff tool-result updates with the originating response ID to preserve correct grouping/order.
dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/MessageMergerTests.cs Adds targeted unit tests for ordering invariants (first-seen order, contiguity, call/result adjacency, identifierless segments).
dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/HandoffOrchestrationTests.cs Adds end-to-end workflow-host-as-agent tests verifying call/result order for both non-streaming and streaming plus persisted history.

@peibekwe peibekwe requested a review from alliscode July 14, 2026 23:47
@peibekwe peibekwe marked this pull request as ready for review July 14, 2026 23:54
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.

.NET: Workflow Host-as-Agent can improperly reorder messages in history and when running non-streaming

4 participants