Skip to content

.NET: feat: Implement return-to-previous routing in handoff workflow#4356

Merged
lokitoth merged 7 commits into
mainfrom
dev/dotnet_workflow/Enable-HandoffHILReturnToPrevious
Mar 26, 2026
Merged

.NET: feat: Implement return-to-previous routing in handoff workflow#4356
lokitoth merged 7 commits into
mainfrom
dev/dotnet_workflow/Enable-HandoffHILReturnToPrevious

Conversation

@lokitoth

@lokitoth lokitoth commented Feb 27, 2026

Copy link
Copy Markdown
Contributor

Motivation and Context

It is often desirable to return control to the user in a handoff orchestration session without losing the context of which Agent is active (and returning to the original starting agent).

Description

Contribution Checklist

  • The code builds clean without any errors or warnings
  • The PR follows the Contribution Guidelines
  • All unit tests pass, and I have added new tests where possible
  • Is this a breaking change? If yes, add "[BREAKING]" prefix to the title of the PR.

Copilot AI review requested due to automatic review settings February 27, 2026 18:58
@markwallace-microsoft markwallace-microsoft added .NET workflows Related to Workflows in agent-framework labels Feb 27, 2026
@github-actions github-actions Bot changed the title feat: Implement return-to-previous routing in handoff workflow .NET: feat: Implement return-to-previous routing in handoff workflow Feb 27, 2026

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 implements return-to-previous routing in handoff workflows, allowing subsequent user turns to route directly back to the specialist agent that handled the previous turn, rather than always routing through the initial coordinator agent. The PR also deprecates HandoffsWorkflowBuilder in favor of HandoffWorkflowBuilder (without the 's').

Changes:

  • Added EnableReturnToPrevious() method to configure return-to-previous routing behavior
  • Introduced HandoffsCurrentAgentTracker to track the active agent across turns
  • Modified HandoffState to include CurrentAgentId for routing decisions
  • Deprecated HandoffsWorkflowBuilder and introduced HandoffWorkflowBuilder as the preferred name

Reviewed changes

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

Show a summary per file
File Description
HandoffsWorkflowBuilder.cs Adds class hierarchy with obsolete HandoffsWorkflowBuilder and new HandoffWorkflowBuilder; implements EnableReturnToPrevious() method and dynamic routing logic
HandoffsCurrentAgentTracker.cs New simple tracker class to maintain current agent ID across workflow turns
HandoffState.cs Adds optional CurrentAgentId parameter to track active agent
HandoffsStartExecutor.cs Updated to accept tracker and pass current agent ID to HandoffState
HandoffsEndExecutor.cs Updated to accept tracker and update current agent ID at workflow completion
HandoffAgentExecutor.cs Determines current agent ID based on handoff target and stores mapping
AgentWorkflowBuilderTests.cs Comprehensive tests covering default behavior, enabled return-to-previous, and edge cases
Comments suppressed due to low confidence (1)

dotnet/src/Microsoft.Agents.AI.Workflows/HandoffsWorkflowBuilder.cs:39

  • The constructor parameter name in the XML documentation incorrectly refers to HandoffsWorkflowBuilder (with 's'), but this is now in the base class HandoffWorkflowBuilderCore. The documentation should be updated to refer to the correct class name or remain generic.
    /// <summary>
    /// Initializes a new instance of the <see cref="HandoffsWorkflowBuilder"/> class with no handoff relationships.
    /// </summary>
    /// <param name="initialAgent">The first agent to be invoked (prior to any handoff).</param>
    internal HandoffsWorkflowBuilder(AIAgent initialAgent)

Comment thread dotnet/src/Microsoft.Agents.AI.Workflows/HandoffsWorkflowBuilder.cs Outdated
Comment thread dotnet/src/Microsoft.Agents.AI.Workflows/HandoffsWorkflowBuilder.cs Outdated
Comment thread dotnet/src/Microsoft.Agents.AI.Workflows/HandoffsWorkflowBuilder.cs Outdated
Comment thread dotnet/src/Microsoft.Agents.AI.Workflows/HandoffsWorkflowBuilder.cs Outdated
@lokitoth lokitoth force-pushed the dev/dotnet_workflow/Enable-HandoffHILReturnToPrevious branch from 9fab1ea to 155372a Compare March 19, 2026 16:16
@lokitoth lokitoth force-pushed the dev/dotnet_workflow/Enable-HandoffHILReturnToPrevious branch from 155372a to 5fce20d Compare March 23, 2026 17:02
@lokitoth lokitoth force-pushed the dev/dotnet_workflow/Enable-HandoffHILReturnToPrevious branch from 5fce20d to eab0951 Compare March 23, 2026 17:25
@lokitoth lokitoth force-pushed the dev/dotnet_workflow/Enable-HandoffHILReturnToPrevious branch from eab0951 to 5f4f700 Compare March 24, 2026 16:01
@lokitoth lokitoth force-pushed the dev/dotnet_workflow/Enable-HandoffHILReturnToPrevious branch from 5f4f700 to 670f7ea Compare March 25, 2026 22:01
@lokitoth lokitoth moved this to In Review in Agent Framework Mar 25, 2026
@lokitoth lokitoth force-pushed the dev/dotnet_workflow/Enable-HandoffHILReturnToPrevious branch from 3f62312 to 2e9a164 Compare March 25, 2026 22:49
- Also obsoletes HandoffsWorkflowBuilder => HandoffWorkflowBuilder (no "s")
Because the tracker was instance-shared between the start and end executors, it would be shared between all sessions, resulting in incorrect behaviour.

The corect way to do this is to keep the data in a shared executor scope, which is per-session.
@lokitoth lokitoth force-pushed the dev/dotnet_workflow/Enable-HandoffHILReturnToPrevious branch from 2e9a164 to 3d6fa76 Compare March 25, 2026 22:54
Comment thread dotnet/src/Microsoft.Agents.AI.Workflows/HandoffsWorkflowBuilder.cs
@lokitoth lokitoth added this pull request to the merge queue Mar 26, 2026
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Mar 26, 2026
@lokitoth lokitoth added this pull request to the merge queue Mar 26, 2026
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Mar 26, 2026
@lokitoth lokitoth added this pull request to the merge queue Mar 26, 2026
Merged via the queue into main with commit 5530bc5 Mar 26, 2026
21 checks passed
@github-project-automation github-project-automation Bot moved this from In Review to Done in Agent Framework Mar 26, 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.

.NET: Feature Request: Add EnableReturnToPrevious() to HandoffsWorkflowBuilder in .NET SDK

5 participants