Skip to content
VerificationWould you notice if your agent drifted in production?Explore the 5-stage harness›
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.

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;
}

  1. Initial state: Every new instance defaults to the state specified in initial.
  2. Transition guards: The guard field accepts a CEL expression. In the example above, only callers with auth.role == 'admin' can transition a ticket from InProgress to Escalated.
  3. Strict transition enforcement: Any attempt to jump between states without an explicit transition (for example, attempting to move directly from Open to Closed) is rejected with a structured transition error.
  4. 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.

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 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.