Fix message ordering in workflow-hosted agents#7123
Open
peibekwe wants to merge 1 commit into
Open
Conversation
Contributor
There was a problem hiding this comment.
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
MessageMergerto 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
HandoffAgentExecutorcarry the originatingResponseId, 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. |
westey-m
approved these changes
Jul 15, 2026
alliscode
approved these changes
Jul 15, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
breaking changelabel (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.