Skip to content

Commit 5d355ac

Browse files
Python: Fix HandoffBuilder silently dropping context_provider during agent cloning (microsoft#3721)
* Initial plan * Fix context_provider parameter name bug and add test Co-authored-by: markwallace-microsoft <127216156+markwallace-microsoft@users.noreply.github.com> * Improve context_provider test to directly check cloned agent Co-authored-by: markwallace-microsoft <127216156+markwallace-microsoft@users.noreply.github.com> * Improve test based on code review feedback Co-authored-by: markwallace-microsoft <127216156+markwallace-microsoft@users.noreply.github.com> * Remove unused events variable in test_context_provider_preserved_during_handoff Co-authored-by: markwallace-microsoft <127216156+markwallace-microsoft@users.noreply.github.com> * Fix formatting: remove trailing whitespace from test_handoff.py Co-authored-by: markwallace-microsoft <127216156+markwallace-microsoft@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: markwallace-microsoft <127216156+markwallace-microsoft@users.noreply.github.com>
1 parent a17f135 commit 5d355ac

2 files changed

Lines changed: 45 additions & 1 deletion

File tree

python/packages/orchestrations/agent_framework_orchestrations/_handoff.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ def _clone_chat_agent(self, agent: ChatAgent) -> ChatAgent:
309309
name=agent.name,
310310
description=agent.description,
311311
chat_message_store_factory=agent.chat_message_store_factory,
312-
context_providers=agent.context_provider,
312+
context_provider=agent.context_provider,
313313
middleware=middleware,
314314
default_options=cloned_options, # type: ignore[arg-type]
315315
)

python/packages/orchestrations/tests/test_handoff.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
ChatResponse,
1212
ChatResponseUpdate,
1313
Content,
14+
Context,
15+
ContextProvider,
1416
ResponseStream,
1517
WorkflowEvent,
1618
resolve_agent_id,
@@ -303,6 +305,48 @@ async def mock_get_response(messages: Any, options: dict[str, Any] | None = None
303305
assert last_tool_choice == {"mode": "required"}, f"Expected 'required', got {last_tool_choice}"
304306

305307

308+
async def test_context_provider_preserved_during_handoff():
309+
"""Verify that context_provider is preserved when cloning agents in handoff workflows."""
310+
# Track whether context provider methods were called
311+
provider_calls: list[str] = []
312+
313+
class TestContextProvider(ContextProvider):
314+
"""A test context provider that tracks its invocations."""
315+
316+
async def invoking(self, messages: Sequence[ChatMessage], **kwargs: Any) -> Context:
317+
provider_calls.append("invoking")
318+
return Context(instructions="Test context from provider.")
319+
320+
# Create context provider
321+
context_provider = TestContextProvider()
322+
323+
# Create a mock chat client
324+
mock_client = MockChatClient(name="test_agent")
325+
326+
# Create agent with context provider using proper constructor
327+
agent = ChatAgent(
328+
chat_client=mock_client,
329+
name="test_agent",
330+
id="test_agent",
331+
context_provider=context_provider,
332+
)
333+
334+
# Verify the original agent has the context provider
335+
assert agent.context_provider is context_provider, "Original agent should have context provider"
336+
337+
# Build handoff workflow - this should clone the agent and preserve context_provider
338+
workflow = HandoffBuilder(participants=[agent]).with_start_agent(agent).build()
339+
340+
# Run workflow with a simple message to trigger context provider
341+
await _drain(workflow.run("Test message", stream=True))
342+
343+
# Verify context provider was invoked during the workflow execution
344+
assert len(provider_calls) > 0, (
345+
"Context provider should be called during workflow execution, "
346+
"indicating it was properly preserved during agent cloning"
347+
)
348+
349+
306350
# region Participant Factory Tests
307351

308352

0 commit comments

Comments
 (0)