perf: Add cancellation support and short-circuit to ModuleConditionHandler#1720
Conversation
…ndler - Add CancellationToken parameter to ShouldIgnore method - Change from parallel evaluation to sequential with short-circuit: - Mandatory conditions: stop on first failure - Non-mandatory conditions: stop on first success - Remove unused EnumerableAsyncProcessor dependency - Add cancellation checks between condition evaluations This reduces wasted work when conditions fail early and allows proper cancellation during condition evaluation. Fixes #1571 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
SummaryThis PR adds cancellation token support and switches from parallel to sequential short-circuit evaluation in ModuleConditionHandler for better performance and proper cancellation handling. Critical IssuesNone found ✅ Suggestions1. Consider passing CancellationToken at call site (ModuleRetriever.cs:55)While the new parameter has a default value, for proper cancellation propagation you might want to update the call site in var (shouldIgnore, skipDecision) = await _moduleConditionHandler.ShouldIgnore(module, cancellationToken).ConfigureAwait(false);This would require adding a 2. Condition evaluation remains non-cancellableThe Previous Review StatusNo previous comments found. Verdict✅ APPROVE - No critical issues. The changes are well-reasoned: sequential evaluation with short-circuiting is both simpler and more efficient than parallel evaluation for this use case. The cancellation token support is a good addition, even if propagation could be improved in future work. |
There was a problem hiding this comment.
Pull request overview
This PR enhances the ModuleConditionHandler with cancellation support and optimizes condition evaluation by replacing parallel processing with sequential short-circuit logic. The change simplifies the code by removing the dependency on EnumerableAsyncProcessor and improves efficiency by stopping evaluation as soon as the outcome is determined.
Key changes:
- Added
CancellationTokenparameter toIModuleConditionHandler.ShouldIgnoremethod - Replaced parallel condition evaluation with sequential evaluation that short-circuits on first failure (mandatory) or first success (non-mandatory)
- Added
ConfigureAwait(false)to async calls and removed theRunnableConditionMetrecord class
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/ModularPipelines/Engine/IModuleConditionHandler.cs | Added CancellationToken parameter to ShouldIgnore method signature with default value |
| src/ModularPipelines/Engine/ModuleConditionHandler.cs | Implemented cancellation support, replaced parallel evaluation with sequential short-circuit logic, removed dependency on EnumerableAsyncProcessor, and cleaned up unused code |
| foreach (var attr in mandatoryRunConditionAttributes) | ||
| { | ||
| return (false, SkipDecision.Skip($"A condition to run this module has not been met - {mandatoryCondition.RunConditionAttribute.GetType().Name}")); | ||
| cancellationToken.ThrowIfCancellationRequested(); |
There was a problem hiding this comment.
The cancellation token is being checked between evaluating conditions, but it's not being passed to the attr.Condition(pipelineContext) call on line 86. This means the actual condition evaluation cannot be cancelled, which limits the effectiveness of the cancellation support added in this PR. Consider updating the RunConditionAttribute.Condition method signature to accept a CancellationToken parameter.
| foreach (var attr in runConditionAttributes) | ||
| { | ||
| return (true, null); | ||
| cancellationToken.ThrowIfCancellationRequested(); |
There was a problem hiding this comment.
The cancellation token is being checked between evaluating conditions, but it's not being passed to the attr.Condition(pipelineContext) call on line 103. This means the actual condition evaluation cannot be cancelled, which limits the effectiveness of the cancellation support added in this PR. Consider updating the RunConditionAttribute.Condition method signature to accept a CancellationToken parameter.
Summary
Fixes #1571
CancellationTokenparameter toIModuleConditionHandler.ShouldIgnorefor proper cancellation supportEnumerableAsyncProcessorfor simpler, more efficient codeConfigureAwait(false)to async callsRationale
The previous implementation evaluated all conditions in parallel, which was wasteful when:
Sequential evaluation with short-circuiting is both simpler and more efficient for this use case.
Test plan
🤖 Generated with Claude Code