Skip to content

Add Default Workflow and Activity Commit Strategy Support#7148

Merged
KnibbsyMan merged 3 commits intoelsa-workflows:mainfrom
dwoldo:dwoldo/add-commit-strategy-defaults
Dec 23, 2025
Merged

Add Default Workflow and Activity Commit Strategy Support#7148
KnibbsyMan merged 3 commits intoelsa-workflows:mainfrom
dwoldo:dwoldo/add-commit-strategy-defaults

Conversation

@dwoldo
Copy link

@dwoldo dwoldo commented Dec 16, 2025

Add Default Workflow and Activity Commit Strategy Support

Summary

This PR introduces optional global default commit strategies for both workflows and activities, providing a fallback mechanism when no explicit commit strategy is configured. This feature allows developers to set application-wide defaults while still maintaining the ability to override them at the workflow or activity level.

Addresses #7135 #7135

Problem

Currently, if a workflow or activity doesn't specify a commit strategy, no commits occur during execution (except for the final commit). This requires developers to explicitly configure commit strategies for every workflow and activity, even when they want consistent behavior across their application.

Solution

Implemented two complementary features:

1. Default Workflow Commit Strategy

  • Allows setting a global default commit strategy that applies to all workflows without an explicit strategy
  • Configured via WithDefaultWorkflowCommitStrategy() extension method
  • Does not appear in the commit strategy registry (discrete implementation)
  • Can be overridden by workflow-specific strategies

2. Default Activity Commit Strategy

  • Allows setting a global default commit strategy that applies to all activities without an explicit strategy
  • Configured via WithDefaultActivityCommitStrategy() extension method
  • Falls through to workflow-level strategy resolution if not configured
  • Does not appear in the commit strategy registry (discrete implementation)
  • Can be overridden by activity-specific strategies

Resolution Order

For Activities:

  1. Activity-specific commit strategy (if configured)
  2. Default activity commit strategy (if configured)
  3. Workflow-level commit strategy (if configured)
  4. Default workflow commit strategy (if configured)
  5. No commit (default behavior)

For Workflows:

  1. Workflow-specific commit strategy (if configured)
  2. Default workflow commit strategy (if configured)
  3. No commit (default behavior)

Usage Example

// Configure default strategies
services.AddElsa(elsa => elsa
    .UseWorkflows(workflows => workflows
        // Set default workflow-level strategy
        .WithDefaultWorkflowCommitStrategy(new ActivityExecutedWorkflowStrategy())
        
        // Set default activity-level strategy
        .WithDefaultActivityCommitStrategy(new ExecutedActivityStrategy())
        
        // Standard strategies still available for explicit use
        .UseCommitStrategies(strategies => strategies.AddStandardStrategies())
    )
);

// Workflows/activities without explicit strategies will use the defaults
// Explicit strategies still override the defaults

Key Features

  • Type-safe API: Accepts IWorkflowCommitStrategy and IActivityCommitStrategy instances directly
  • Discrete implementation: Default strategies don't appear in the registry, avoiding confusion in UI/tooling
  • Backward compatible: Existing behavior unchanged when defaults are not configured
  • Fluent API: Clean, chainable configuration methods
  • Full test coverage: 11 integration tests validating all scenarios

Changes

Core Implementation

  • CommitStateOptions.cs: Added DefaultWorkflowCommitStrategy and DefaultActivityCommitStrategy properties
  • CommitStrategiesFeature.cs: Added SetDefaultWorkflowCommitStrategy() and SetDefaultActivityCommitStrategy() configuration methods
  • ModuleExtensions.cs: Added WithDefaultWorkflowCommitStrategy() and WithDefaultActivityCommitStrategy() extension methods
  • DefaultActivitySchedulerMiddleware.cs: Modified to fall back to default workflow strategy
  • DefaultActivityInvokerMiddleware.cs: Modified to implement full resolution chain with default strategies

Testing

  • DefaultWorkflowCommitStrategy/Tests.cs: 5 integration tests covering workflow-level defaults
  • DefaultActivityCommitStrategy/Tests.cs: 6 integration tests covering activity-level defaults
  • All tests include exact commit count validation to ensure runtime behavior matches expectations

Test Coverage

Workflow Default Strategy Tests:

  1. Workflow uses default when no explicit strategy is set (6 commits)
  2. Workflow-specific strategy overrides default (1 commit)
  3. No commits when no default and no explicit strategy (1 commit)
  4. Default strategy not visible in registry
  5. Default strategy with standard strategies doesn't duplicate

Activity Default Strategy Tests:

  1. Activity uses default when no explicit strategy is set (6 commits)
  2. Activity-specific strategy overrides default (4 commits)
  3. No commits when no default and no explicit strategy (1 commit)
  4. Default strategy not visible in registry
  5. Default strategy with standard strategies doesn't duplicate
  6. Default workflow strategy used when no activity default (6 commits)

Breaking Changes

None. This is a purely additive feature that maintains backward compatibility.

Additional Notes

  • Composite activities (like Sequence) trigger commit strategy evaluation when processing child activity completions, which is why some tests show 6 commits instead of 3 (3 WriteLine activities + 3 Sequence composite completions)
  • The implementation ensures that default strategies are stored separately from the registry to maintain a clear separation between explicitly registered strategies and fallback defaults
  • All 11 integration tests pass with precise commit count validation

Documentation

Usage examples and API documentation have been added via XML comments on all new public methods.


This change is Reviewable

David Garza added 3 commits December 16, 2025 08:01
…sage examples

- Introduced methods to set default workflow and activity commit strategies in CommitStrategiesFeature.
- Updated CommitStateOptions to include properties for default strategies.
- Added extension methods for configuring default strategies in WorkflowsFeature.
- Created usage examples demonstrating how to set and utilize default commit strategies.
- Implemented tests to verify default strategy behavior in various scenarios.
…cy; remove unused example files and add a new CommitTracker helper for testing.
@dwoldo
Copy link
Author

dwoldo commented Dec 16, 2025

@KnibbsyMan I aborted my other PR due to the force push. Prior PR: #7137 for reference

@dwoldo dwoldo changed the title Add Default Workflow and Activity Commit Strategy Support #7137 Add Default Workflow and Activity Commit Strategy Support Dec 16, 2025
@KnibbsyMan KnibbsyMan requested a review from Copilot December 16, 2025 23:48
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces optional global default commit strategies for workflows and activities, providing application-wide defaults while maintaining override capabilities at the workflow/activity level. The implementation adds two new properties to CommitStateOptions, extension methods for configuration, and updates the middleware resolution logic to check for default strategies when no explicit strategy is specified.

Key Changes:

  • Added default workflow and activity commit strategy support with proper resolution fallback chain
  • Updated middleware to resolve default strategies when no explicit strategy is configured
  • Added comprehensive integration tests (11 tests) validating all scenarios including commit counts

Reviewed changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
CommitTracker.cs New test helper to track commit invocations in integration tests
DefaultWorkflowCommitStrategy/Tests.cs 5 integration tests validating workflow-level default strategy behavior
DefaultWorkflowCommitStrategy/*.cs Test workflow classes with and without explicit strategies
DefaultActivityCommitStrategy/Tests.cs 6 integration tests validating activity-level default strategy behavior and fallback to workflow defaults
DefaultActivityCommitStrategy/*.cs Test workflow classes demonstrating activity-level strategy resolution
CommitStateOptions.cs Added DefaultWorkflowCommitStrategy and DefaultActivityCommitStrategy properties with documentation
ModuleExtensions.cs Added WithDefaultWorkflowCommitStrategy and WithDefaultActivityCommitStrategy extension methods
CommitStrategiesFeature.cs Added SetDefaultWorkflowCommitStrategy and SetDefaultActivityCommitStrategy configuration methods
DefaultActivitySchedulerMiddleware.cs Updated to fallback to default workflow strategy when no explicit strategy is set
DefaultActivityInvokerMiddleware.cs Updated with full resolution chain: activity strategy → default activity strategy → workflow strategy → default workflow strategy
BackgroundActivityInvokerMiddleware.cs Updated constructor to accept IOptions<CommitStateOptions> parameter and minor whitespace cleanup

@KnibbsyMan
Copy link
Member

Hi @dwoldo, if you close out the Copilot comments, agree or disagree with them as you see fit, I’ll give this a look over this week and try get it merged🚀

@dwoldo
Copy link
Author

dwoldo commented Dec 21, 2025

Thank you for your patience @KnibbsyMan. I've resolved the comments with no changes.

@KnibbsyMan
Copy link
Member

Hi @dwoldo,

Not a problem, it's that time of year! I'll take a look at it over the next few days but at a glance it seems to make sense!

@KnibbsyMan
Copy link
Member

Hi @dwoldo,

LGTM👍🏼

PS: Great to see test coverage, they are always a welcomed addition to any new feature!

@KnibbsyMan KnibbsyMan merged commit 31eff52 into elsa-workflows:main Dec 23, 2025
9 checks passed
@dwoldo
Copy link
Author

dwoldo commented Dec 27, 2025

@KnibbsyMan The tests are important, thank you! J/W, could this be part of the 3.6 release coming out soon?

@KnibbsyMan
Copy link
Member

@dwoldo

Yes, it should be part of either the 3.6.0-rc2 or the 3.6.0 release, whatever happens first!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants