Skip to content

Commit ee36283

Browse files
RachelWangggbaywet
andauthored
.NET: Deduplicate fan-out targets so each target runs once (#9432)
FanOutEdgeRunner mapped every index returned by a fan-out target selector to a sink ID and delivered once per entry, so a target selected twice ran twice on the same message. The same happened without a selector when the target list named an executor twice. Drop repeated IDs once they are resolved, keeping the order in which each target is first named. Co-authored-by: Vincent Biret <[email protected]>
1 parent c2d2930 commit ee36283

2 files changed

Lines changed: 83 additions & 0 deletions

File tree

‎dotnet/src/Microsoft.Agents.AI.Workflows/Execution/FanOutEdgeRunner.cs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ this.EdgeData.EdgeAssigner is null
2929
: this.EdgeData.EdgeAssigner(message, this.EdgeData.SinkIds.Count)
3030
.Select(i => this.EdgeData.SinkIds[i]);
3131

32+
// The assigner, or the target list itself, can name a target more than once, for example when two
33+
// tags on a message route to the same executor. Deliver to each target once, in the order first named.
34+
targetIds = targetIds.Distinct(StringComparer.Ordinal);
35+
3236
Executor[] result = await Task.WhenAll(targetIds.Where(IsValidTarget)
3337
.Select(tid => this.RunContext.EnsureExecutorAsync(tid, stepTracer)
3438
.AsTask()))

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

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,85 @@ public async Task Test_FanOutEdgeRunnerAsync()
151151
await CreateAndRunFanOutEdgeTestAsync(assignerSelectsEmpty: true, targetMatch: false); // Assigner(does not include output), Targeted(not matching)
152152
}
153153

154+
[Fact]
155+
public async Task Test_FanOutEdgeRunner_DeduplicatesAssignedTargetsAsync()
156+
{
157+
// Arrange
158+
TestRunContext runContext = new();
159+
runContext.ConfigureExecutors([
160+
new ForwardMessageExecutor<string>("executor1"),
161+
new ForwardMessageExecutor<string>("executor2"),
162+
new ForwardMessageExecutor<string>("executor3")
163+
]);
164+
165+
// The assigner names executor3 twice, and names it before executor2.
166+
FanOutEdgeData edgeData = new("executor1", ["executor2", "executor3"], new EdgeId(0), (message, count) => [1, 0, 1]);
167+
FanOutEdgeRunner runner = new(runContext, edgeData);
168+
169+
// Act
170+
DeliveryMapping? mapping = await runner.ChaseEdgeAsync(new("test", "executor1"), stepTracer: null, CancellationToken.None);
171+
172+
// Assert
173+
Assert.NotNull(mapping);
174+
Assert.Equal(["executor3", "executor2"], mapping.Deliveries.Select(delivery => delivery.TargetId));
175+
}
176+
177+
[Fact]
178+
public async Task Test_FanOutEdgeRunner_DeduplicatesRepeatedSinksAsync()
179+
{
180+
// Arrange
181+
TestRunContext runContext = new();
182+
runContext.ConfigureExecutors([
183+
new ForwardMessageExecutor<string>("executor1"),
184+
new ForwardMessageExecutor<string>("executor2")
185+
]);
186+
187+
// No assigner, and the target list names executor2 twice.
188+
FanOutEdgeData edgeData = new("executor1", ["executor2", "executor2"], new EdgeId(0));
189+
FanOutEdgeRunner runner = new(runContext, edgeData);
190+
191+
// Act
192+
DeliveryMapping? mapping = await runner.ChaseEdgeAsync(new("test", "executor1"), stepTracer: null, CancellationToken.None);
193+
194+
// Assert
195+
Assert.NotNull(mapping);
196+
Assert.Equal(["executor2"], mapping.Deliveries.Select(delivery => delivery.TargetId));
197+
}
198+
199+
[Fact]
200+
public async Task Test_FanOutEdge_TargetAssignedTwiceRunsOnceAsync()
201+
{
202+
// Arrange
203+
int billingCalls = 0;
204+
int techCalls = 0;
205+
206+
ForwardMessageExecutor<string> triage = new("triage");
207+
Func<string, ValueTask> billingHandler = _ =>
208+
{
209+
Interlocked.Increment(ref billingCalls);
210+
return default;
211+
};
212+
Func<string, ValueTask> techHandler = _ =>
213+
{
214+
Interlocked.Increment(ref techCalls);
215+
return default;
216+
};
217+
ExecutorBinding billing = billingHandler.BindAsExecutor("billing");
218+
ExecutorBinding tech = techHandler.BindAsExecutor("tech");
219+
220+
// The selector names the billing executor twice, as when two tags on a message route to it.
221+
Workflow workflow = new WorkflowBuilder(triage)
222+
.AddFanOutEdge<string>(triage, [billing, tech], (message, count) => [0, 0])
223+
.Build();
224+
225+
// Act
226+
await using Run run = await InProcessExecution.RunAsync(workflow, "charged twice, want my money back");
227+
228+
// Assert
229+
Assert.Equal(1, billingCalls);
230+
Assert.Equal(0, techCalls);
231+
}
232+
154233
[Fact]
155234
public async Task Test_FanInEdgeRunnerAsync()
156235
{

0 commit comments

Comments
 (0)