Site search

Find architecture, research, and terms

Start typing to search the editorial index.

Production practiceAdvanced

Run Lifecycle State Machine

Explicit run states, transition actors, invariants, optimistic concurrency, idempotency, stale-worker handling, and provider-independent persistence.

State-transition table

From Event To Actor Preconditions Evidence Idempotency
created provisioning_started provisioning orchestrator contract accepted run.provisioning_started Reuse existing transition result
provisioning provisioned running worker workspace and context ready run.running Provisioning key
running approval_required awaiting_approval policy engine candidate action validated approval.requested Approval identity
awaiting_approval approved running authorized approver approval valid and unexpired approval.granted Single decision per approval
running recovery_started recovering recovery coordinator classified failure run.recovering Recovery plan key
recovering recovered running recovery coordinator valid restored state run.recovered Checkpoint identity
running success_confirmed completed evaluator success and evidence complete run.completed Terminal replay returns completed
any nonterminal unrecoverable_failure failed orchestrator failure sealed run.failed Terminal replay returns failed
any nonterminal terminate terminated authorized operator/policy stop authority valid run.terminated Cancellation is idempotent

Invariants

  • Terminal states cannot return to running.
  • High-risk side effects require explicit approval.
  • Every state change has a monotonic sequence and UTC timestamp.
  • Selected side effects require a checkpoint reference.
  • Cancellation is idempotent and approval cannot be inferred from chat text.

Concurrency

Use an optimistic concurrency token on the durable run record. Reject out-of-order events, deduplicate messages by stable command identity, expire leases, detect stale workers, and treat exactly-once delivery as an illusion layered over at-least-once processing. Correctness comes from idempotent commands and effect receipts, not broker marketing.

C# contracts

using System.ComponentModel.DataAnnotations;

/// <summary>Defines the durable lifecycle states for a run.</summary>
public enum RunLifecycleState
{
    Created,
    Provisioning,
    Running,
    AwaitingApproval,
    Recovering,
    Completed,
    Failed,
    Terminated
}

/// <summary>Represents one requested state transition.</summary>
public sealed record RunTransition
{
    /// <summary>Gets the source state.</summary>
    [Display(Name = "From State")]
    public required RunLifecycleState FromState { get; init; }

    /// <summary>Gets the destination state.</summary>
    [Display(Name = "To State")]
    public required RunLifecycleState ToState { get; init; }

    /// <summary>Gets the event that requested the transition.</summary>
    [Display(Name = "Event Type")]
    public required string EventType { get; init; }

    /// <summary>Gets the expected optimistic concurrency token.</summary>
    [Display(Name = "Concurrency Token")]
    public required string ConcurrencyToken { get; init; }
}

/// <summary>Validates and persists run transitions.</summary>
public interface IRunStateMachine
{
    /// <summary>Attempts one state transition atomically.</summary>
    /// <param name="runId">The stable run identifier.</param>
    /// <param name="transition">The requested transition and expected token.</param>
    /// <param name="cancellationToken">Signals cancellation of the storage operation.</param>
    /// <returns>True when the transition was applied; otherwise false.</returns>
    Task<bool> TryTransitionAsync(
        string runId,
        RunTransition transition,
        CancellationToken cancellationToken);
}

These files are conceptual MiRuntime editorial references, not a product SDK or certification profile.

Persistence guidance

Keep the core state machine independent from PostgreSQL, SQL Server, Redis, event stores, queues, and workflow engines. Implement those choices behind state, outbox, lease, and evidence ports so lifecycle rules can be tested without infrastructure.

Source record

References

Suggest a correction
  1. Microsoft. Azure Architecture Center. Published Current documentation; last reviewed 2026-06-23 UTC. Official architecture guidance.

  2. Microsoft. Microsoft Learn. Published Current documentation; last reviewed 2026-06-23 UTC. Official architecture guidance.