Kaizen
Browse modulesLifecyclelifecycle/serverFunctions

Function: defineMachine()

function defineMachine(def: MachineDef): CompiledMachine;

Defined in: shared/lifecycle/define-machine.ts:43

Compile and validate a flat lifecycle machine definition.

Validation runs fail-fast IN THIS ORDER (the order matters — wildcard expansion happens first so every later check sees concrete source states):

  1. Expand from: "*" to all NON-TERMINAL states.
  2. Every from / to / initial / terminal references a declared state.
  3. No terminal state has an outgoing edge.
  4. No duplicate (from, event) pair (determinism).
  5. No to === from unless the edge sets allowSelf (NoOpTransition policy).
  6. Warn (do not throw) on unreachable states.

Also computes a normalized content hash (machineVersion) over the sorted states / events / transitions / guard names so it is reproducible across deploys and machines compiled in different declaration orders.

From the validated edge list we build an XState v5 machine that is the runtime authority for the GRAPH: resolve/eventsFrom consult it for legality and targets (setup().createMachine() + transition(), the same pattern the bookings composer uses). XState is passed NO guards and NO actions — it never evaluates a guard or runs an effect. The engine-owned metadata XState can't hold (guards / effects / allowSelf) lives in a sidecar Map keyed by ${from} ${event}; resolve merges XState's to with that metadata. Compile-time validation stays here (XState would silently merge duplicate/self edges).

Parameters

ParameterType
defMachineDef

Returns

CompiledMachine

Throws

with a LIFECYCLE_DEFINE_* prefix on any structural error.

On this page