mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
6f31e32df6
Aligns the .NET orchestration builders with Python's output / intermediate-output distinction. Each builder either applies a Python-aligned default designation set or replays the user's explicit `WithOutputFrom` / `WithIntermediateOutputFrom` calls, never both. Static `AgentWorkflowBuilder.BuildSequential` / `BuildConcurrent` apply defaults unconditionally (no user-facing fluent surface to take control through): - Sequential: terminal `end` + every agent designated intermediate. - Concurrent: terminal `end` + every agent and per-agent accumulator designated intermediate. The three fluent instance builders memoize agent-typed designation calls in a `Dictionary<AIAgent, HashSet<OutputTag>>` (empty set = terminal-only, non-empty = intermediate tag(s)) so repeated calls dedupe naturally. They replay the entries at `Build()` time, suppressing defaults when any call has been made: - `HandoffWorkflowBuilder` / `HandoffWorkflowBuilderCore<TBuilder>` (also picked up by the obsolete `HandoffsWorkflowBuilder` via inheritance). Default: terminal `HandoffEnd` + every handoff agent intermediate. (Bug fix: legacy code relied on `WithOutputFrom(end)` to bind `HandoffEnd`. The new explicit-designation path bypasses that, so `Build()` now calls `BindExecutor(end)` unconditionally to keep validation happy.) - `GroupChatWorkflowBuilder` — default: terminal host + every participant intermediate. - `MagenticWorkflowBuilder` — default: terminal orchestrator + every team member intermediate. Designating a non-participant agent throws `InvalidOperationException`. The bare `WorkflowBuilder` default is unchanged — only the orchestration-style builders gain implicit defaults, matching the plan's non-goal. Tests ----- - `AgentWorkflowBuilder.SequentialTests` / `.ConcurrentTests`: one default-spec assertion each. - `GroupChatWorkflowBuilderTests`: defaults-match-spec, explicit-replaces-defaults, non-participant throws. - `HandoffWorkflowBuilderTests` (new file): same three. - `MagenticWorkflowBuilderTests` (new file): same three. 593/593 unit tests pass on net10.0 (582 baseline + 11 new). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
78 lines
3.1 KiB
C#
78 lines
3.1 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Text.RegularExpressions;
|
|
using System.Threading.Tasks;
|
|
using FluentAssertions;
|
|
using Microsoft.Extensions.AI;
|
|
|
|
#pragma warning disable SYSLIB1045 // Use GeneratedRegex
|
|
#pragma warning disable RCS1186 // Use Regex instance instead of static method
|
|
|
|
namespace Microsoft.Agents.AI.Workflows.UnitTests;
|
|
|
|
public static partial class AgentWorkflowBuilderTests
|
|
{
|
|
public class ConcurrentTests
|
|
{
|
|
[Fact]
|
|
public void BuildConcurrent_InvalidArguments_Throws()
|
|
{
|
|
Assert.Throws<ArgumentNullException>("agents", () => AgentWorkflowBuilder.BuildConcurrent(null!));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task BuildConcurrent_AgentsRunInParallelAsync()
|
|
{
|
|
StrongBox<TaskCompletionSource<bool>> barrier = new();
|
|
StrongBox<int> remaining = new();
|
|
|
|
var workflow = AgentWorkflowBuilder.BuildConcurrent(
|
|
[
|
|
new DoubleEchoAgentWithBarrier("agent1", barrier, remaining),
|
|
new DoubleEchoAgentWithBarrier("agent2", barrier, remaining),
|
|
]);
|
|
|
|
for (int iter = 0; iter < 3; iter++)
|
|
{
|
|
barrier.Value = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
remaining.Value = 2;
|
|
|
|
(string updateText, List<ChatMessage>? result, _, _) = await RunWorkflowAsync(workflow, [new ChatMessage(ChatRole.User, "abc")]);
|
|
Assert.NotEmpty(updateText);
|
|
Assert.NotNull(result);
|
|
|
|
// TODO: https://github.com/microsoft/agent-framework/issues/784
|
|
// These asserts are flaky until we guarantee message delivery order.
|
|
Assert.Single(Regex.Matches(updateText, "agent1"));
|
|
Assert.Single(Regex.Matches(updateText, "agent2"));
|
|
Assert.Equal(4, Regex.Matches(updateText, "abc").Count);
|
|
Assert.Equal(2, result.Count);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void Test_BuildConcurrent_DefaultDesignationsMatchSpec()
|
|
{
|
|
Workflow workflow = AgentWorkflowBuilder.BuildConcurrent(
|
|
[new DoubleEchoAgent("agent1"), new DoubleEchoAgent("agent2"), new DoubleEchoAgent("agent3")]);
|
|
|
|
Dictionary<string, HashSet<OutputTag>> designations = workflow.OutputExecutors;
|
|
|
|
List<KeyValuePair<string, HashSet<OutputTag>>> terminals = designations
|
|
.Where(kvp => kvp.Value.Count == 0)
|
|
.ToList();
|
|
terminals.Should().ContainSingle("Concurrent has exactly one terminal output executor (ConcurrentEndExecutor)");
|
|
|
|
List<KeyValuePair<string, HashSet<OutputTag>>> intermediates = designations
|
|
.Where(kvp => kvp.Value.Contains(OutputTag.Intermediate))
|
|
.ToList();
|
|
intermediates.Should().HaveCount(6,
|
|
"every concurrent agent (3) and its per-agent accumulator (3) are designated intermediate");
|
|
}
|
|
}
|
|
}
|