.NET: fix: avoid mutating handoff message roles#5808
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a .NET handoff orchestration bug where role reassignment (assistant→user for “other agents” context) was mutating shared conversation message instances in-place, causing downstream consumers (e.g., telemetry) to observe agent responses incorrectly labeled as User. The change preserves the existing “handoff target sees other agents as user context” behavior while ensuring the shared conversation history remains unchanged.
Changes:
- Replace in-place role reassignment with a non-mutating copy path for messages sent to the invoked agent during handoff.
- Remove the prior “mutate + reset” mechanism in favor of a single copy-and-adjust step.
- Add unit/regression tests to ensure role reassignment does not mutate shared conversation messages and that the handoff target receives the adjusted role context.
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/Specialized/HandoffAgentExecutor.cs | Switches to a non-mutating message copy for role-adjusted agent input and removes the reset step. |
| dotnet/src/Microsoft.Agents.AI.Workflows/AIAgentsAbstractionsExtensions.cs | Introduces CopyWithAssistantToUserForOtherParticipants and removes the previous in-place change/reset helpers. |
| dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/HandoffOrchestrationTests.cs | Adds a regression test verifying shared conversation messages are not mutated during handoff role reassignment. |
| dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/AIAgentsAbstractionsExtensionsTests.cs | Adds a focused unit test ensuring the copy path does not mutate the original message and produces a distinct instance with the adjusted role. |
ddd35b6 to
4b137c3
Compare
lokitoth
approved these changes
May 13, 2026
peibekwe
approved these changes
May 13, 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.
Summary
Notes
This addresses the in-place role mutation root cause called out in #5804. The target agent still receives the same role-adjusted context, but the shared conversation keeps prior agent responses as assistant messages during and after invocation.
To verify