Skip to content

Commit 91980c6

Browse files
author
Jacob Alber
committed
Fix handoff orchestration not passing user message to handoff target agent (#3161)
Filter out internal handoff function call and tool result messages before passing conversation history to the target agent's LLM. These messages confused the model into ignoring the original user question.
1 parent cd4e36e commit 91980c6

2 files changed

Lines changed: 152 additions & 1 deletion

File tree

‎dotnet/src/Microsoft.Agents.AI.Workflows/Specialized/HandoffAgentExecutor.cs‎

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Generic;
55
using System.ComponentModel;
66
using System.Diagnostics;
7+
using System.Linq;
78
using System.Text.Json;
89
using System.Threading;
910
using System.Threading.Tasks;
@@ -69,7 +70,15 @@ protected override RouteBuilder ConfigureRoutes(RouteBuilder routeBuilder) =>
6970

7071
List<ChatMessage>? roleChanges = allMessages.ChangeAssistantToUserForOtherParticipants(this._agent.Name ?? this._agent.Id);
7172

72-
await foreach (var update in this._agent.RunStreamingAsync(allMessages,
73+
// If a handoff was invoked by a previous agent, filter out the handoff function
74+
// call and tool result messages before sending to the underlying agent. These
75+
// are internal workflow mechanics that confuse the target model into ignoring the
76+
// original user question.
77+
List<ChatMessage> messagesForAgent = handoffState.InvokedHandoff is not null
78+
? FilterHandoffMessages(allMessages)
79+
: allMessages;
80+
81+
await foreach (var update in this._agent.RunStreamingAsync(messagesForAgent,
7382
options: this._agentOptions,
7483
cancellationToken: cancellationToken)
7584
.ConfigureAwait(false))
@@ -114,5 +123,39 @@ async Task AddUpdateAsync(AgentResponseUpdate update, CancellationToken cancella
114123
}
115124
});
116125

126+
/// <summary>
127+
/// Creates a filtered copy of the message list with handoff function call contents and
128+
/// tool result messages removed, preserving any non-handoff content in mixed messages.
129+
/// </summary>
130+
private static List<ChatMessage> FilterHandoffMessages(List<ChatMessage> messages)
131+
{
132+
List<ChatMessage> filtered = [];
133+
foreach (ChatMessage m in messages)
134+
{
135+
if (m.Role == ChatRole.Tool && m.Contents.Any(c => c is FunctionResultContent))
136+
{
137+
continue;
138+
}
139+
140+
if (m.Role == ChatRole.Assistant && m.Contents.Any(c => c is FunctionCallContent))
141+
{
142+
List<AIContent> filteredContents = m.Contents
143+
.Where(c => c is not FunctionCallContent)
144+
.ToList();
145+
146+
if (filteredContents.Count > 0)
147+
{
148+
filtered.Add(new ChatMessage(m.Role, filteredContents) { AuthorName = m.AuthorName });
149+
}
150+
151+
continue;
152+
}
153+
154+
filtered.Add(m);
155+
}
156+
157+
return filtered;
158+
}
159+
117160
public ValueTask ResetAsync() => default;
118161
}

‎dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/AgentWorkflowBuilderTests.cs‎

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,114 @@ public async Task Handoffs_OneTransfer_ResponseServedBySecondAgentAsync()
273273
Assert.Contains("nextAgent", result[3].AuthorName);
274274
}
275275

276+
[Fact]
277+
public async Task Handoffs_OneTransfer_HandoffTargetDoesNotReceiveHandoffFunctionMessagesAsync()
278+
{
279+
// Regression test for https://github.com/microsoft/agent-framework/issues/3161
280+
// When a handoff occurs, the target agent should receive the original user message
281+
// but should NOT receive the handoff function call or tool result messages from the
282+
// source agent, as these confuse the target LLM into ignoring the user's question.
283+
284+
List<ChatMessage>? capturedNextAgentMessages = null;
285+
286+
var initialAgent = new ChatClientAgent(new MockChatClient((messages, options) =>
287+
{
288+
string? transferFuncName = options?.Tools?.FirstOrDefault(t => t.Name.StartsWith("handoff_to_", StringComparison.Ordinal))?.Name;
289+
Assert.NotNull(transferFuncName);
290+
291+
return new(new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("call1", transferFuncName)]));
292+
}), name: "initialAgent");
293+
294+
var nextAgent = new ChatClientAgent(new MockChatClient((messages, options) =>
295+
{
296+
capturedNextAgentMessages = messages.ToList();
297+
return new(new ChatMessage(ChatRole.Assistant, "The derivative of x^2 is 2x."));
298+
}),
299+
name: "nextAgent",
300+
description: "The second agent");
301+
302+
var workflow =
303+
AgentWorkflowBuilder.CreateHandoffBuilderWith(initialAgent)
304+
.WithHandoff(initialAgent, nextAgent)
305+
.Build();
306+
307+
_ = await RunWorkflowAsync(workflow, [new ChatMessage(ChatRole.User, "What is the derivative of x^2?")]);
308+
309+
Assert.NotNull(capturedNextAgentMessages);
310+
311+
// The target agent should see the original user message
312+
Assert.Contains(capturedNextAgentMessages, m => m.Role == ChatRole.User && m.Text == "What is the derivative of x^2?");
313+
314+
// The target agent should NOT see the handoff function call or tool result from the source agent
315+
Assert.DoesNotContain(capturedNextAgentMessages, m => m.Contents.Any(c => c is FunctionCallContent fcc && fcc.Name.StartsWith("handoff_to_", StringComparison.Ordinal)));
316+
Assert.DoesNotContain(capturedNextAgentMessages, m => m.Role == ChatRole.Tool && m.Contents.Any(c => c is FunctionResultContent frc && frc.Result?.ToString() == "Transferred."));
317+
}
318+
319+
[Fact]
320+
public async Task Handoffs_TwoTransfers_HandoffTargetsDoNotReceiveHandoffFunctionMessagesAsync()
321+
{
322+
// Regression test for https://github.com/microsoft/agent-framework/issues/3161
323+
// With two hops (initial -> second -> third), each target agent should receive the
324+
// original user message and text responses from prior agents (as User role), but
325+
// NOT any handoff function call or tool result messages.
326+
327+
List<ChatMessage>? capturedSecondAgentMessages = null;
328+
List<ChatMessage>? capturedThirdAgentMessages = null;
329+
330+
var initialAgent = new ChatClientAgent(new MockChatClient((messages, options) =>
331+
{
332+
string? transferFuncName = options?.Tools?.FirstOrDefault(t => t.Name.StartsWith("handoff_to_", StringComparison.Ordinal))?.Name;
333+
Assert.NotNull(transferFuncName);
334+
335+
// Return both a text message and a handoff function call
336+
return new(new ChatMessage(ChatRole.Assistant, [new TextContent("Routing to second agent"), new FunctionCallContent("call1", transferFuncName)]));
337+
}), name: "initialAgent");
338+
339+
var secondAgent = new ChatClientAgent(new MockChatClient((messages, options) =>
340+
{
341+
capturedSecondAgentMessages = messages.ToList();
342+
343+
string? transferFuncName = options?.Tools?.FirstOrDefault(t => t.Name.StartsWith("handoff_to_", StringComparison.Ordinal))?.Name;
344+
Assert.NotNull(transferFuncName);
345+
346+
// Return both a text message and a handoff function call
347+
return new(new ChatMessage(ChatRole.Assistant, [new TextContent("Routing to third agent"), new FunctionCallContent("call2", transferFuncName)]));
348+
}), name: "secondAgent", description: "The second agent");
349+
350+
var thirdAgent = new ChatClientAgent(new MockChatClient((messages, options) =>
351+
{
352+
capturedThirdAgentMessages = messages.ToList();
353+
return new(new ChatMessage(ChatRole.Assistant, "Hello from agent3"));
354+
}),
355+
name: "thirdAgent",
356+
description: "The third / final agent");
357+
358+
var workflow =
359+
AgentWorkflowBuilder.CreateHandoffBuilderWith(initialAgent)
360+
.WithHandoff(initialAgent, secondAgent)
361+
.WithHandoff(secondAgent, thirdAgent)
362+
.Build();
363+
364+
(string updateText, _) = await RunWorkflowAsync(workflow, [new ChatMessage(ChatRole.User, "abc")]);
365+
366+
Assert.Contains("Hello from agent3", updateText);
367+
368+
// Second agent should see the original user message and initialAgent's text as context
369+
Assert.NotNull(capturedSecondAgentMessages);
370+
Assert.Contains(capturedSecondAgentMessages, m => m.Text == "abc");
371+
Assert.Contains(capturedSecondAgentMessages, m => m.Text!.Contains("Routing to second agent"));
372+
Assert.DoesNotContain(capturedSecondAgentMessages, m => m.Contents.Any(c => c is FunctionCallContent fcc && fcc.Name.StartsWith("handoff_to_", StringComparison.Ordinal)));
373+
Assert.DoesNotContain(capturedSecondAgentMessages, m => m.Role == ChatRole.Tool && m.Contents.Any(c => c is FunctionResultContent));
374+
375+
// Third agent should see the original user message and both prior agents' text as context
376+
Assert.NotNull(capturedThirdAgentMessages);
377+
Assert.Contains(capturedThirdAgentMessages, m => m.Text == "abc");
378+
Assert.Contains(capturedThirdAgentMessages, m => m.Text!.Contains("Routing to second agent"));
379+
Assert.Contains(capturedThirdAgentMessages, m => m.Text!.Contains("Routing to third agent"));
380+
Assert.DoesNotContain(capturedThirdAgentMessages, m => m.Contents.Any(c => c is FunctionCallContent fcc && fcc.Name.StartsWith("handoff_to_", StringComparison.Ordinal)));
381+
Assert.DoesNotContain(capturedThirdAgentMessages, m => m.Role == ChatRole.Tool && m.Contents.Any(c => c is FunctionResultContent));
382+
}
383+
276384
[Fact]
277385
public async Task Handoffs_TwoTransfers_ResponseServedByThirdAgentAsync()
278386
{

0 commit comments

Comments
 (0)