Skip to content

Workflow State Machine Specification

Document ID: WF-006
Version: 1.0.0
Status: Draft
Owner: Workflow Engine Team
Last Updated: 2026-06-26


This document defines the finite state machine (FSM) used by the Wovyr Workflow Engine.

The FSM governs:

  • Workflow lifecycle
  • Activity lifecycle
  • State transitions
  • Transition validation
  • Recovery behavior
  • Replay semantics
  • Distributed execution consistency

Every workflow execution must transition only through valid states defined in this specification.


The state machine must provide:

  • Deterministic execution
  • Explicit lifecycle management
  • Compile-time safety
  • Replay compatibility
  • Fault tolerance
  • Idempotent transitions
  • Distributed consistency
  • Auditability

The state machine follows these principles:

  1. Every transition is event-driven.
  2. Every transition is persisted.
  3. Invalid transitions are rejected.
  4. State transitions are deterministic.
  5. Terminal states are immutable.
  6. Workflow state is reconstructed through replay.
  7. Activity state is isolated from workflow state.
  8. All transitions are auditable.

+-----------+
| Created |
+-----+-----+
|
+-----------+
|Validated |
+-----+-----+
|
+-----------+
|Scheduled |
+-----+-----+
|
+-----------+
| Running |
+--+--+--+--+
| | |
+---------------+ | +----------------+
▼ ▼ ▼
Waiting Cancelled Failed
| |
▼ ▼
Resumed Compensating
| |
+----------------+-------------------+
Completed

StateDescriptionTerminal
CreatedWorkflow instance createdNo
ValidatedWorkflow definition validatedNo
ScheduledReady for executionNo
RunningCurrently executingNo
WaitingWaiting for event, timer, or humanNo
ResumedExecution resumed after waitingNo
CompensatingExecuting rollback logicNo
CompletedSuccessfully finishedYes
FailedExecution failedYes
CancelledExecution cancelledYes

Created
Ready
Scheduled
Running
┌──┴──────────────┐
▼ ▼
Completed Failed
Retrying
Scheduled

StateDescription
CreatedActivity instantiated
ReadyDependencies satisfied
ScheduledAssigned to scheduler
RunningExecuting on worker
CompletedSuccessfully finished
FailedExecution failed
RetryingWaiting for retry

FromToAllowed
CreatedValidated
ValidatedScheduled
ScheduledRunning
RunningWaiting
WaitingResumed
ResumedRunning
RunningCompleted
RunningFailed
RunningCancelled
FailedCompensating
CompensatingCompleted
CompletedRunning
FailedRunning
CancelledRunning

FromToAllowed
CreatedReady
ReadyScheduled
ScheduledRunning
RunningCompleted
RunningFailed
FailedRetrying
RetryingScheduled
CompletedRunning

Before a transition is committed, the runtime validates:

  • Workflow version
  • Dependency completion
  • Activity ownership
  • Worker lease validity
  • Security permissions
  • Resource availability
  • Timeout constraints
  • Tenant isolation

If any validation fails, the transition is rejected.


Every successful transition produces an immutable event.

Examples:

WorkflowCreated
WorkflowValidated
WorkflowScheduled
WorkflowStarted
WorkflowPaused
WorkflowResumed
WorkflowCompleted
WorkflowFailed
WorkflowCancelled
ActivityReady
ActivityScheduled
ActivityStarted
ActivityCompleted
ActivityFailed
ActivityRetried

These events are published to the Event Bus and persisted.


Each transition persists:

workflowId:
executionId:
previousState:
currentState:
transitionTime:
workerId:
correlationId:
causationId:
version:
reason:

Persistence occurs before acknowledging completion.


Each workflow instance maintains a monotonically increasing version.

Example:

Version 1 -> Created
Version 2 -> Validated
Version 3 -> Scheduled
Version 4 -> Running
Version 5 -> Waiting
Version 6 -> Running
Version 7 -> Completed

Version numbers enable optimistic concurrency control.


Replay reconstructs workflow state using:

  1. Latest checkpoint
  2. Transition events
  3. Activity history
  4. Runtime metadata

Replay must produce the same final state regardless of worker assignment.


Workflows may pause indefinitely.

Supported waiting reasons:

  • Human approval
  • External webhook
  • Message queue event
  • Timer
  • Cron trigger
  • File upload
  • Child workflow completion

Waiting workflows consume no execution thread.


Compensation executes only after configured failures.

Example:

Reserve Inventory
Charge Payment
Create Shipment
Failure
Compensating
Refund Payment
Release Inventory
Completed

Compensation itself is managed by the state machine.


Cancellation may originate from:

  • User
  • Administrator
  • Timeout
  • Policy Engine
  • Scheduler
  • Parent workflow

Cancellation modes:

  • Immediate
  • Graceful
  • Compensated

Timeout scopes:

TimeoutDescription
ActivityActivity exceeded execution limit
WorkflowWorkflow exceeded maximum duration
WaitingWaiting period expired
LeaseWorker lease expired

Timeouts generate events before state transitions.


Only one workflow state transition may commit at a time.

Concurrency is enforced using:

  • Version numbers
  • Optimistic locking
  • Lease ownership
  • Atomic persistence

Conflicting updates must be rejected.


When execution moves between workers:

  1. Current worker releases lease.
  2. Scheduler assigns new worker.
  3. State restored from persistence.
  4. Replay validates consistency.
  5. Execution resumes.

No transition may be skipped during migration.


Recovery procedure:

  1. Detect failed worker.
  2. Expire lease.
  3. Restore checkpoint.
  4. Replay transition history.
  5. Resume execution.
  6. Publish recovery event.

Recovery must preserve deterministic behavior.


#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WorkflowState {
Created,
Validated,
Scheduled,
Running,
Waiting,
Resumed,
Compensating,
Completed,
Failed,
Cancelled,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ActivityState {
Created,
Ready,
Scheduled,
Running,
Completed,
Failed,
Retrying,
}

Recommended interface:

pub trait StateMachine {
fn current_state(&self) -> WorkflowState;
fn transition(
&mut self,
event: TransitionEvent,
) -> Result<(), StateTransitionError>;
fn validate(
&self,
event: &TransitionEvent,
) -> bool;
}

Implementations must guarantee deterministic transitions.


The runtime validates:

  • No invalid transitions
  • Terminal state immutability
  • Valid worker ownership
  • Dependency satisfaction
  • Activity completion ordering
  • Replay consistency
  • Version monotonicity
  • Security policies

Required tests:

  • Transition validation
  • Guard validation
  • Invalid transitions
  • Timeout handling
  • Scheduler interaction
  • Replay
  • Worker migration
  • Compensation
  • Random transition sequences
  • Concurrency validation
  • Replay determinism
  • Worker crash
  • Lease expiration
  • Network partition
  • Database restart

Metrics:

  • Active workflows
  • Running workflows
  • Waiting workflows
  • State transition latency
  • Failed transitions
  • Compensation count
  • Cancellation count
  • Replay count

Logs must include:

  • Workflow ID
  • Execution ID
  • Correlation ID
  • Worker ID
  • Previous state
  • Current state

The state machine enforces:

  • Tenant isolation
  • Authorization
  • Immutable audit history
  • Transition validation
  • Secret protection
  • Worker authentication

No unauthorized component may mutate workflow state.


RequirementTarget
Transition latency< 5 ms
Replay accuracy100%
Invalid transition detection100%
Recovery correctness100%
State persistence durabilityNo data loss
Concurrent transition conflictsAutomatically detected

  • Workflow Overview
  • Workflow Execution Model
  • Workflow DSL
  • DAG Engine
  • Scheduler
  • Checkpointing
  • Retry Engine
  • Compensation
  • Distributed Execution
  • Persistence
  • Rust Crate Design

VersionDateDescription
1.0.02026-06-26Initial Workflow State Machine Specification