Specification Language
State Machines & Workflows
State machines formalize entity lifecycles by defining allowable status transitions, initial values, and role-based guards. Aegil compiles @stateMachine definitions into database-level transition checks, server endpoint validations, and reactive Flutter UI state gates.
Defining a finite state machine
Section titled “Defining a finite state machine”namespace Helpdesk;
enum TicketStatus { Open, InProgress, Escalated, Resolved, Closed,}
@stateMachine({ field: "status", initial: "Open", states: ["Open", "InProgress", "Escalated", "Resolved", "Closed"], transitions: [ { from: "Open", to: "InProgress" }, { from: "InProgress", to: "Escalated", guard: "auth.role == 'admin'" }, { from: "InProgress", to: "Resolved" }, { from: "Escalated", to: "Resolved" }, { from: "Resolved", to: "Closed" }, { from: "Closed", to: "Open" } ]})@entity({ table: "tickets" })model Ticket { @pk id: uuid;
@owner reporterId: uuid;
status: TicketStatus = TicketStatus.Open; title: string;}How state machines work in Aegil
Section titled “How state machines work in Aegil”- Initial state: Every new instance defaults to the state specified in
initial. - Transition guards: The
guardfield accepts a CEL expression. In the example above, only callers withauth.role == 'admin'can transition a ticket fromInProgresstoEscalated. - Strict transition enforcement: Any attempt to jump between states without an explicit transition (for example, attempting to move directly from
OpentoClosed) is rejected with a structured transition error. - Optimistic UI: When a mutation declaring a transition succeeds on the client, the generated BLoC immediately updates local state before network completion, rolling back if the server rejects the request.
Exhaustive verification in aegil verify
Section titled “Exhaustive verification in aegil verify”When you run aegil verify, the invariant test harness tests the entire Transition Matrix:
- Tests every defined transition (must succeed).
- Tests every undefined transition combination (must fail).
- Tests guarded transitions with both unauthorized and authorized caller personas.
You never have to manually write state machine unit tests; Aegil mathematically verifies the full state transition graph.
What could go wrong?
Section titled “What could go wrong?”| What you see | Why | What to do |
|---|---|---|
ERR_STATE_MACHINE_SYNTAX |
A transition references a state name not declared in states. |
Ensure every state in from and to is listed in states and in the matching enum. |
ERR_INVALID_TRANSITION |
A client or hook attempted a state transition that is not declared in transitions. |
Check the allowed transitions in specs/<feature>/*.tsp or update the state machine definition. |