Skip to content

Commit 2024d4d

Browse files
authored
.NET: Fix workflow topology edge multiplicity comparison (#8656)
* fix(dotnet): match workflow edges as a multiset Co-authored-by: Copilot <[email protected]> Copilot-Session: 7ed91283-cb21-49a7-98f4-b1c088d0b6b6 * fix(dotnet): clarify edge comparison validation Use a non-null checkpoint local after the assertion and document the bidirectional multiset semantics of edge matching. Co-authored-by: Copilot <[email protected]> Copilot-Session: 560f0ccd-0091-485e-87fc-d55bf3f2bb45 --------- Copilot-Session: 7ed91283-cb21-49a7-98f4-b1c088d0b6b6 Copilot-Session: 560f0ccd-0091-485e-87fc-d55bf3f2bb45
1 parent 37ce872 commit 2024d4d

3 files changed

Lines changed: 134 additions & 3 deletions

File tree

‎dotnet/src/Microsoft.Agents.AI.Workflows/Checkpointing/WorkflowInfo.cs‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ public bool IsMatch(Workflow workflow)
7272
!workflow.Edges.TryGetValue(sourceId, out var edgeList) ||
7373
// If the edge list count does not match, or
7474
edgeList.Count != this.Edges[sourceId].Count ||
75-
// If any edge in the workflow edge list does not match the corresponding edge in this.Edges[sourceId]
76-
!edgeList.All(edge => this.Edges[sourceId].Any(e => e.IsMatch(edge)))
75+
// If the edge lists do not match one-to-one.
76+
!EdgesMatch(this.Edges[sourceId], edgeList)
7777
))
7878
{
7979
return false;
@@ -101,4 +101,26 @@ public bool IsMatch(Workflow workflow)
101101

102102
return true;
103103
}
104+
105+
/// <summary>
106+
/// Compares saved and current edges in both directions, consuming each match so duplicate edge multiplicity is preserved;
107+
/// a LINQ membership comparison would not enforce this one-to-one matching.
108+
/// </summary>
109+
private static bool EdgesMatch(IReadOnlyList<EdgeInfo> savedEdges, IReadOnlyCollection<Edge> currentEdges)
110+
{
111+
List<EdgeInfo> unmatchedEdges = [.. savedEdges];
112+
113+
foreach (Edge edge in currentEdges)
114+
{
115+
int matchIndex = unmatchedEdges.FindIndex(savedEdge => savedEdge.IsMatch(edge));
116+
if (matchIndex < 0)
117+
{
118+
return false;
119+
}
120+
121+
unmatchedEdges.RemoveAt(matchIndex);
122+
}
123+
124+
return unmatchedEdges.Count == 0;
125+
}
104126
}

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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
using System;
44
using System.Collections.Generic;
5+
using System.IO;
56
using System.Linq;
67
using System.Threading;
78
using System.Threading.Tasks;
9+
using Microsoft.Agents.AI.Workflows.Checkpointing;
810
using Microsoft.Agents.AI.Workflows.InProc;
911
using Microsoft.Agents.AI.Workflows.Sample;
1012

@@ -619,6 +621,50 @@ internal async Task Checkpoint_Resume_WithRepublishDisabled_DoesNotEmitRequestIn
619621
Assert.Equal(0, requestEventCount);
620622
}
621623

624+
[Theory]
625+
[InlineData(ExecutionEnvironment.InProcess_OffThread)]
626+
[InlineData(ExecutionEnvironment.InProcess_Lockstep)]
627+
internal async Task Checkpoint_Resume_RejectsDifferentEdgeMultiplicityBeforeImportingQueuedMessagesAsync(
628+
ExecutionEnvironment environment)
629+
{
630+
// Arrange
631+
CheckpointManager checkpointManager = CheckpointManager.CreateInMemory();
632+
InProcessExecutionEnvironment env = environment.ToWorkflowExecutionEnvironment();
633+
CheckpointInfo? checkpoint = null;
634+
635+
await using (StreamingRun firstRun = await env.WithCheckpointing(checkpointManager)
636+
.RunStreamingAsync(
637+
CreateConditionalRoutingWorkflow(duplicateFirstTarget: false),
638+
"queued"))
639+
{
640+
await foreach (WorkflowEvent evt in firstRun.WatchStreamAsync(blockOnPendingRequest: false))
641+
{
642+
if (evt is SuperStepCompletedEvent step && step.CompletionInfo?.Checkpoint is { } cp)
643+
{
644+
checkpoint = cp;
645+
break;
646+
}
647+
}
648+
}
649+
650+
Assert.NotNull(checkpoint);
651+
CheckpointInfo nonNullCheckpoint = checkpoint;
652+
Checkpoint storedCheckpoint = await ((ICheckpointManager)checkpointManager)
653+
.LookupCheckpointAsync(nonNullCheckpoint.SessionId, nonNullCheckpoint);
654+
Assert.Equal(2, storedCheckpoint.RunnerData.QueuedMessages.Values.Sum(messages => messages.Count));
655+
656+
// Act
657+
ValueTask<StreamingRun> resumeTask = env.WithCheckpointing(checkpointManager)
658+
.ResumeStreamingAsync(
659+
CreateConditionalRoutingWorkflow(duplicateFirstTarget: true),
660+
nonNullCheckpoint);
661+
662+
// Assert
663+
InvalidDataException exception = await Assert.ThrowsAsync<InvalidDataException>(
664+
async () => await resumeTask);
665+
Assert.Contains("not compatible", exception.Message, StringComparison.Ordinal);
666+
}
667+
622668
private static Workflow CreateSimpleRequestWorkflow(
623669
string requestPortId = "TestPort",
624670
string processorId = "Processor")
@@ -631,6 +677,20 @@ private static Workflow CreateSimpleRequestWorkflow(
631677
.Build();
632678
}
633679

680+
private static Workflow CreateConditionalRoutingWorkflow(bool duplicateFirstTarget)
681+
{
682+
ForwardMessageExecutor<string> source = new("Source");
683+
ForwardMessageExecutor<string> first = new("First");
684+
ForwardMessageExecutor<string> second = new("Second");
685+
686+
return new WorkflowBuilder(source)
687+
.BindExecutor(first)
688+
.BindExecutor(second)
689+
.AddEdge<string>(source, first, static _ => true)
690+
.AddEdge<string>(source, duplicateFirstTarget ? first : second, static _ => true)
691+
.Build(validateOrphans: false);
692+
}
693+
634694
private static Workflow CreateCheckpointedSubworkflowRequestWorkflow()
635695
{
636696
ExecutorBinding subworkflow = CreateSimpleRequestWorkflow(

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

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace Microsoft.Agents.AI.Workflows.UnitTests;
1616

1717
public class RepresentationTests
1818
{
19-
private sealed class TestExecutor() : Executor("TestExecutor")
19+
private sealed class TestExecutor(string id = "TestExecutor") : Executor(id)
2020
{
2121
protected override ProtocolBuilder ConfigureProtocol(ProtocolBuilder protocolBuilder) => protocolBuilder;
2222
}
@@ -185,4 +185,53 @@ static void RunWorkflowInfoMatchTest(Workflow workflow, Workflow? comparator = n
185185
Assert.Equal(expect, info.IsMatch(comparator));
186186
}
187187
}
188+
189+
[Fact]
190+
public void Test_WorkflowInfo_EdgeMultiplicityMustMatch()
191+
{
192+
// Arrange
193+
Workflow savedWorkflow = CreateConditionalEdgeWorkflow("First", "Second");
194+
Workflow currentWorkflow = CreateConditionalEdgeWorkflow("First", "First");
195+
WorkflowInfo info = savedWorkflow.ToWorkflowInfo();
196+
197+
// Act
198+
bool isMatch = info.IsMatch(currentWorkflow);
199+
200+
// Assert
201+
Assert.False(isMatch);
202+
}
203+
204+
[Fact]
205+
public void Test_WorkflowInfo_EquivalentDuplicateEdgesMatch()
206+
{
207+
// Arrange
208+
Workflow savedWorkflow = CreateConditionalEdgeWorkflow("First", "First");
209+
Workflow currentWorkflow = CreateConditionalEdgeWorkflow("First", "First");
210+
WorkflowInfo info = savedWorkflow.ToWorkflowInfo();
211+
212+
// Act
213+
bool isMatch = info.IsMatch(currentWorkflow);
214+
215+
// Assert
216+
Assert.True(isMatch);
217+
}
218+
219+
private static Workflow CreateConditionalEdgeWorkflow(string firstTargetId, string secondTargetId)
220+
{
221+
TestExecutor source = new("Source");
222+
TestExecutor first = new("First");
223+
TestExecutor second = new("Second");
224+
Dictionary<string, TestExecutor> targets = new()
225+
{
226+
[first.Id] = first,
227+
[second.Id] = second,
228+
};
229+
230+
return new WorkflowBuilder(source)
231+
.BindExecutor(first)
232+
.BindExecutor(second)
233+
.AddEdge(source, targets[firstTargetId], Condition())
234+
.AddEdge(source, targets[secondTargetId], Condition())
235+
.Build(validateOrphans: false);
236+
}
188237
}

0 commit comments

Comments
 (0)