Skip to content

.NET: Add Workflow Builder Specialized Edge tests#5826

Merged
lokitoth merged 23 commits into
mainfrom
copilot/add-unit-tests-edge-types
May 14, 2026
Merged

.NET: Add Workflow Builder Specialized Edge tests#5826
lokitoth merged 23 commits into
mainfrom
copilot/add-unit-tests-edge-types

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 13, 2026

Motivation and Context

The WorkflowBuilderExtensions.ForwardMessage and ForwardExcept methods were materializing an intermediate List<ExecutorBinding> on every call, even when only a single target was passed. This is unnecessary allocation that can be avoided when the enumerable's count can be determined without enumeration. Additionally, the single-target optimization path was missing for ForwardMessage. The SwitchBuilder.AddCase and WithDefault methods also lacked null-validation for individual executor elements within the collection parameters. Finally, there was no unit test coverage for the edge types produced by the workflow builder extension methods.

Description

Performance Optimization (WorkflowBuilderExtensions):

  • Refactored ForwardMessage<TMessage> and ForwardExcept<TMessage> to avoid unnecessary list materialization
  • Added TFM-specialized count detection:
    • .NET (core): Uses TryGetNonEnumeratedCount for O(1) count detection
    • netstandard2.0/net472: Uses ICollection<ExecutorBinding> { Count: 1 } pattern matching
  • Single target fast path creates a direct edge via AddEdge
  • Multiple targets lazily validate via targets.Select(ValidateTarget) passed to AddSwitch

Null-Validation (SwitchBuilder and WorkflowBuilderExtensions):

  • Added Throw.IfNull for builder and source in ForwardMessage and ForwardExcept IEnumerable overloads
  • Added Throw.IfNull for executors parameter in AddChain
  • Added per-element Throw.IfNull(executor, $"{nameof(executors)}[{executorIndex++}]") in SwitchBuilder.AddCase and WithDefault foreach loops with inline index tracking
  • Added per-element Throw.IfNull(target, nameof(targets)) validation in ForwardMessage/ForwardExcept via ValidateTarget local function
  • Added Throw.IfNull(target) in single-target ForwardMessage and ForwardExcept overloads

New Unit Tests (WorkflowBuilderSmokeTests):
Added comprehensive smoke tests for workflow edge type construction and argument validation:

  • ForwardMessage_WithSingleTarget_CreatesDirectEdge — verifies direct edge and type predicate behavior
  • ForwardMessage_WithMultipleTargets_CreatesFanOutEdge — verifies fan-out edge with condition
  • ForwardExcept_WithSingleTarget_CreatesDirectEdge — verifies exclusion predicate
  • ForwardExcept_WithMultipleTargets_CreatesFanOutEdge — verifies fan-out exclusion
  • AddChain_CreatesSequentialDirectEdges — verifies sequential direct edge chain
  • AddChain_WhenExecutorRepeats_Throws — verifies cycle detection
  • AddExternalCall_CreatesRequestPortAndRoundTripEdges — verifies port and round-trip edges
  • AddSwitch_CreatesFanOutEdgeWithCasesAndDefault — verifies switch with cases and default
  • ForwardMessage_InvalidArguments_Throw — null/empty argument validation
  • ForwardExcept_InvalidArguments_Throw — null/empty argument validation
  • AddChain_InvalidArguments_Throw — null/duplicate argument validation
  • AddExternalCall_InvalidArguments_Throw — null argument validation
  • AddSwitch_InvalidArguments_Throw — null/empty argument validation
  • SwitchBuilder_InvalidArguments_Throw — null element-in-collection validation with indexed parameter names

Contribution Checklist

  • The code builds clean without any errors or warnings
  • The PR follows the Contribution Guidelines
  • All unit tests pass, and I have added new tests where possible
  • Is this a breaking change? If yes, add "[BREAKING]" prefix to the title of the PR.

Copilot AI and others added 15 commits May 13, 2026 18:58
Agent-Logs-Url: https://github.com/microsoft/agent-framework/sessions/3c3d5324-cdcd-4a38-8c67-94e4e78e29c5

Co-authored-by: lokitoth <6936551+lokitoth@users.noreply.github.com>
Agent-Logs-Url: https://github.com/microsoft/agent-framework/sessions/af831ee2-0a99-4427-9ffd-a3b5022c1b3b

Co-authored-by: lokitoth <6936551+lokitoth@users.noreply.github.com>
Agent-Logs-Url: https://github.com/microsoft/agent-framework/sessions/af831ee2-0a99-4427-9ffd-a3b5022c1b3b

Co-authored-by: lokitoth <6936551+lokitoth@users.noreply.github.com>
Agent-Logs-Url: https://github.com/microsoft/agent-framework/sessions/af831ee2-0a99-4427-9ffd-a3b5022c1b3b

Co-authored-by: lokitoth <6936551+lokitoth@users.noreply.github.com>
Agent-Logs-Url: https://github.com/microsoft/agent-framework/sessions/af831ee2-0a99-4427-9ffd-a3b5022c1b3b

Co-authored-by: lokitoth <6936551+lokitoth@users.noreply.github.com>
Agent-Logs-Url: https://github.com/microsoft/agent-framework/sessions/af831ee2-0a99-4427-9ffd-a3b5022c1b3b

Co-authored-by: lokitoth <6936551+lokitoth@users.noreply.github.com>
Agent-Logs-Url: https://github.com/microsoft/agent-framework/sessions/af831ee2-0a99-4427-9ffd-a3b5022c1b3b

Co-authored-by: lokitoth <6936551+lokitoth@users.noreply.github.com>
Agent-Logs-Url: https://github.com/microsoft/agent-framework/sessions/af831ee2-0a99-4427-9ffd-a3b5022c1b3b

Co-authored-by: lokitoth <6936551+lokitoth@users.noreply.github.com>
Agent-Logs-Url: https://github.com/microsoft/agent-framework/sessions/af831ee2-0a99-4427-9ffd-a3b5022c1b3b

Co-authored-by: lokitoth <6936551+lokitoth@users.noreply.github.com>
Agent-Logs-Url: https://github.com/microsoft/agent-framework/sessions/af831ee2-0a99-4427-9ffd-a3b5022c1b3b

Co-authored-by: lokitoth <6936551+lokitoth@users.noreply.github.com>
Agent-Logs-Url: https://github.com/microsoft/agent-framework/sessions/af831ee2-0a99-4427-9ffd-a3b5022c1b3b

Co-authored-by: lokitoth <6936551+lokitoth@users.noreply.github.com>
Agent-Logs-Url: https://github.com/microsoft/agent-framework/sessions/cb9a6a6a-02c7-41a8-a4b4-da16ad62ef86

Co-authored-by: lokitoth <6936551+lokitoth@users.noreply.github.com>
…ICollection pattern for NETFX

Agent-Logs-Url: https://github.com/microsoft/agent-framework/sessions/8ec28a43-e7b7-456e-8d8e-921511b4accc

Co-authored-by: lokitoth <6936551+lokitoth@users.noreply.github.com>
Copilot AI self-assigned this May 13, 2026
Copilot AI review requested due to automatic review settings May 13, 2026 19:54
Copilot AI review requested due to automatic review settings May 13, 2026 19:54
@moonbox3 moonbox3 added .NET workflows Related to Workflows in agent-framework labels May 13, 2026
@github-actions github-actions Bot changed the title Restore TFM-specialized count check in ForwardMessage/ForwardExcept .NET: Restore TFM-specialized count check in ForwardMessage/ForwardExcept May 13, 2026
Copilot AI requested a review from lokitoth May 13, 2026 20:02
@lokitoth lokitoth changed the title .NET: Restore TFM-specialized count check in ForwardMessage/ForwardExcept .NET: Add Workflow Builder Specialized Edge tests May 13, 2026
@lokitoth lokitoth added this pull request to the merge queue May 14, 2026
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks May 14, 2026
@lokitoth lokitoth added this pull request to the merge queue May 14, 2026
Merged via the queue into main with commit 190ca75 May 14, 2026
26 checks passed
@github-project-automation github-project-automation Bot moved this from In Review to Done in Agent Framework May 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

.NET workflows Related to Workflows in agent-framework

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

6 participants