Building Blocks
Cohesive. Machines
Declare a lifecycle in C#, bind its states to entity observations, and coordinate reactions to committed movements.
Core Idea
Cohesive.Machines describes which movements an entity's lifecycle allows, separately from how its fields change or where it is stored. A machine is a small declared graph of states and named edges, with optional guards over the current state and the command. Entity Transitions request a movement instead of rewriting status fields themselves, so the lifecycle rules live in one place that tools can evaluate, draw, and check.
Declare legal movements
A tender can be offered, pending acceptance, or confirmed. A state machine gives each movement an identity, so callers can ask which actions are enabled and which edge a command selects. The Cohesive.Machines namespace lives in the core Cohesive assembly.
var source = Machine.Define("order.lifecycle", revision: "1");
var draft = source.State("draft").Initial();
var submitted = source.State("submitted").Final();
var submit = source.Edge("submit").From(draft).To(submitted);
var lifecycle = source.Build();
var decision = lifecycle.Decide(lifecycle.Initial, "submit");Build rejects malformed graphs, including unresolved states, unreachable states, and nonterminal dead ends. A built definition persists as a canonical, fingerprinted document, and ToMermaid() draws it as a diagram.
Guard a movement
An edge can carry a guard over the current facts and the command input. Here an order can be submitted only when it has items and the customer accepted the terms:
var submit = source.Edge("submit").From(draft).To(submitted)
.When((Order order, SubmitOrder command) =>
order.ItemCount > 0 && command.AcceptTerms);The lambda lowers to the same portable expression IR that Relations and Assurance use, so the guard can be evaluated, inspected, and checked without the original C# code. Evaluate(state, action, facts, input) answers with one of six outcomes: Accepted with the selected edge, UnknownAction, Illegal, Disabled, Ambiguous, or Indeterminate when the guard cannot be evaluated. Evaluation commits nothing. AdmissionMatrix projects the same decisions for every action, which suits UI tooling, and the service runtime can list the operations an actor may perform on an entity after applying authorization and the whole Transition.
Declaration order never sets a priority between guards. If two sibling edges are both enabled for the same observation and command, admission fails as ambiguous. The linker proves disjointness for simple cases, such as equality against distinct values of one field, and warns about sibling guards it cannot prove apart.
Bind an entity
A discriminator binding can assign a status field as part of a movement. A predicate binding can classify a state from several fields, such as a linked load ID and an acceptance flag. The Transition applies its ordinary field updates and checks the resulting state against the bound lifecycle.
The machine owns the precondition, so the Transition requests the movement with .Move(submit, rejection) and adds only its own updates, such as recording who submitted the order. It needs no duplicate guard and no manual status assignment. Finishing a binding with .Bind(entity) gives the entity one authoritative lifecycle: every Transition over that entity must either realize a movement or explicitly preserve its state, and compilation, execution, registration, and state preparation all enforce that rule.
React to a committed movement
When a tender enters pending acceptance, a reaction can request an acknowledgment from its load. The reaction selects the committed movement and projects the target entity ID and command. The target Transition owns its authorization and admission rules. A Process coordinates delivery, waits for prerequisites, and recovers the attempt. An explicit episode identity distinguishes a second round of acceptance on the same tender from redelivery of the first.
Relate two machines
A projection maps every state of a detailed machine onto a coarser one and maps each edge to a path in the target, where an empty path declares that the coarser machine does not move. Validation checks coverage, endpoints, initial states, and terminal states. A coherence constraint lists the state pairs two machines may occupy at the same time. Its analysis explores their interleavings on demand and returns a proof, a counterexample trace, or an unknown result when guards or the exploration bound prevent a conclusion. Both relationships are immutable documents pinned to each participant's revision, and keeping two live machines coherent over time remains the job of an explicit Process.