Skip to content

.NET: fix: avoid mutating handoff message roles#5808

Merged
lokitoth merged 2 commits into
microsoft:mainfrom
he-yufeng:fix/handoff-telemetry-roles
May 13, 2026
Merged

.NET: fix: avoid mutating handoff message roles#5808
lokitoth merged 2 commits into
microsoft:mainfrom
he-yufeng:fix/handoff-telemetry-roles

Conversation

@he-yufeng
Copy link
Copy Markdown
Contributor

Summary

  • stop handoff role reassignment from mutating shared conversation messages in place
  • keep the existing target-agent input behavior: other agents' text context is still presented as user context
  • add unit and handoff regression coverage for the non-mutating copy path

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

  • dotnet test --project .\tests\Microsoft.Agents.AI.Workflows.UnitTests\Microsoft.Agents.AI.Workflows.UnitTests.csproj -f net10.0 --no-build -- --filter-class Microsoft.Agents.AI.Workflows.UnitTests.AIAgentsAbstractionsExtensionsTests
  • dotnet test --project .\tests\Microsoft.Agents.AI.Workflows.UnitTests\Microsoft.Agents.AI.Workflows.UnitTests.csproj -f net10.0 --no-build -- --filter-class Microsoft.Agents.AI.Workflows.UnitTests.HandoffOrchestrationTests
  • dotnet format .\tests\Microsoft.Agents.AI.Workflows.UnitTests\Microsoft.Agents.AI.Workflows.UnitTests.csproj --include .\src\Microsoft.Agents.AI.Workflows\AIAgentsAbstractionsExtensions.cs .\src\Microsoft.Agents.AI.Workflows\Specialized\HandoffAgentExecutor.cs .\tests\Microsoft.Agents.AI.Workflows.UnitTests\AIAgentsAbstractionsExtensionsTests.cs .\tests\Microsoft.Agents.AI.Workflows.UnitTests\HandoffOrchestrationTests.cs --verify-no-changes --no-restore

Copilot AI review requested due to automatic review settings May 13, 2026 11:38
@moonbox3 moonbox3 added .NET workflows Related to Workflows in agent-framework labels May 13, 2026
@github-actions github-actions Bot changed the title fix: avoid mutating handoff message roles .NET: fix: avoid mutating handoff message roles May 13, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 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.

@he-yufeng he-yufeng force-pushed the fix/handoff-telemetry-roles branch from ddd35b6 to 4b137c3 Compare May 13, 2026 15:51
@lokitoth lokitoth moved this to Community PR in Agent Framework May 13, 2026
@lokitoth lokitoth enabled auto-merge May 13, 2026 17:47
@lokitoth lokitoth added this pull request to the merge queue May 13, 2026
Merged via the queue into microsoft:main with commit 9b9604c May 13, 2026
26 checks passed
@github-project-automation github-project-automation Bot moved this from Community PR to Done in Agent Framework May 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

.NET workflows Related to Workflows in agent-framework

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

5 participants