Skip to content

Python: Fix HandoffBuilder dropping function-level middleware when cloning agents#5220

Merged
moonbox3 merged 4 commits intomicrosoft:mainfrom
moonbox3:agent/fix-5173-2
Apr 14, 2026
Merged

Python: Fix HandoffBuilder dropping function-level middleware when cloning agents#5220
moonbox3 merged 4 commits intomicrosoft:mainfrom
moonbox3:agent/fix-5173-2

Conversation

@moonbox3
Copy link
Copy Markdown
Contributor

Motivation and Context

HandoffAgentExecutor._clone_chat_agent() used agent.agent_middleware (agent-level only) instead of agent.middleware (all types), silently discarding any @function_middleware registered on the original agent so it never executed during handoff workflows.

Fixes #5173

Description

The root cause was in _handoff.py line 290: the clone constructor was passed agent.agent_middleware, which only contains agent-level middleware after categorize_middleware() splits the list during Agent.__init__. The fix changes this to agent.middleware, which holds the full original middleware list including both agent and function middleware. A regression test verifies that function middleware survives the cloning step performed by HandoffBuilder.build().

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.

Note: PR autogenerated by moonbox3's agent

Copilot and others added 2 commits April 13, 2026 03:46
…ents (microsoft#5173)

_clone_chat_agent() was using agent.agent_middleware (agent-level only)
instead of agent.middleware (all types), which silently dropped any
function middleware registered on the original agent.

Changed to use agent.middleware to preserve all middleware types
(agent, function, and chat) during cloning.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 13, 2026 03:54
@moonbox3 moonbox3 self-assigned this Apr 13, 2026
Copy link
Copy Markdown
Contributor Author

@moonbox3 moonbox3 left a comment

Choose a reason for hiding this comment

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

Automated Code Review

Reviewers: 4 | Confidence: 95% | Result: All clear

Reviewed: Correctness, Security Reliability, Test Coverage, Design Approach


Automated review by moonbox3's agents

@moonbox3
Copy link
Copy Markdown
Contributor Author

moonbox3 commented Apr 13, 2026

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/orchestrations/agent_framework_orchestrations
   _handoff.py3395384%104–105, 107, 160–170, 172, 174, 176, 181, 312, 337, 364, 390, 453–454, 497, 505, 509–510, 548–550, 555–557, 675, 678, 691, 753, 758, 765, 775, 777, 796, 798, 880–881, 913–914, 1015, 1022, 1094–1095, 1097
TOTAL27200318488% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
5440 20 💤 0 ❌ 0 🔥 1m 27s ⏱️

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

Fixes a handoff workflow cloning bug where HandoffAgentExecutor._clone_chat_agent() only copied agent-level middleware (agent.agent_middleware) and silently dropped function-level middleware, preventing user @function_middleware from running during handoffs.

Changes:

  • Update handoff agent cloning to pass agent.middleware (all middleware types) instead of agent.agent_middleware.
  • Add a regression test intended to ensure middleware survives the HandoffBuilder.build() cloning path.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
python/packages/orchestrations/agent_framework_orchestrations/_handoff.py Fix clone construction to preserve the original agent’s full middleware list.
python/packages/orchestrations/tests/test_handoff.py Adds a regression test for middleware preservation in handoff agent cloning.

Comment thread python/packages/orchestrations/tests/test_handoff.py Outdated
Copilot and others added 2 commits April 13, 2026 03:58
The test used isinstance(m, FunctionMiddleware) which matched
_AutoHandoffMiddleware (always appended during build) instead of the
user's @function_middleware decorator. Assert directly that
tracking_middleware is present in the cloned agent's middleware list.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…lder drops function-level middleware when cloning agents
Copy link
Copy Markdown
Contributor Author

@moonbox3 moonbox3 left a comment

Choose a reason for hiding this comment

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

Automated Code Review

Reviewers: 4 | Confidence: 95% | Result: All clear

Reviewed: Correctness, Security Reliability, Test Coverage, Design Approach


Automated review by moonbox3's agents

@moonbox3 moonbox3 added agent orchestration Issues related to agent orchestration workflows Related to Workflows in agent-framework labels Apr 13, 2026
@moonbox3 moonbox3 added this pull request to the merge queue Apr 14, 2026
Merged via the queue into microsoft:main with commit 3c31ac2 Apr 14, 2026
35 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent orchestration Issues related to agent orchestration python workflows Related to Workflows in agent-framework

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Python: [Bug]: HandoffBuilder drops function-level middleware when cloning agents

4 participants