Skip to content

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

Merged
Evan Mattson (moonbox3) merged 4 commits into
microsoft:mainfrom
moonbox3:agent/fix-5173-2
Apr 14, 2026
Merged

Python: Fix HandoffBuilder dropping function-level middleware when cloning agents#5220
Evan Mattson (moonbox3) merged 4 commits into
microsoft: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

Copilotand 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>
CopilotAI review requested due to automatic review settings April 13, 2026 03:54
@moonbox3Evan Mattson (moonbox3) added the python Usage: [Issues, PRs], Target: Python label Apr 13, 2026

@moonbox3Evan Mattson (moonbox3) left a comment

Copy link
Copy Markdown
ContributorAuthor

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

Evan Mattson (moonbox3) commented Apr 13, 2026

Copy link
Copy Markdown
ContributorAuthor

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

TestsSkippedFailuresErrorsTime
544020 💤0 ❌0 🔥1m 27s ⏱️

CopilotAI 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

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.

FileDescription
python/packages/orchestrations/agent_framework_orchestrations/_handoff.pyFix clone construction to preserve the original agent’s full middleware list.
python/packages/orchestrations/tests/test_handoff.pyAdds a regression test for middleware preservation in handoff agent cloning.

Comment threadpython/packages/orchestrations/tests/test_handoff.py Outdated
Copilotand 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

@moonbox3Evan Mattson (moonbox3) left a comment

Copy link
Copy Markdown
ContributorAuthor

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
Evan Mattson (moonbox3) added this pull request to the merge queueApr 14, 2026
Merged via the queue into microsoft:main with commit 3c31ac2Apr 14, 2026
35 checks passed
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

orchestrationUsage: [Issues, PRs], Target: multi-agent orchestration (high-level patterns)pythonUsage: [Issues, PRs], Target: PythonworkflowsUsage: [Issues, PRs], Target: Workflows

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

@moonbox3@TaoChenOSU@eavanvalkenburg