Automation graphs
cardinal_flow checks an Automation's graph and compiles it to the plan the engine runs.
An Automation's graph field stores nodes and edges as JSON. cardinal_flow checks that graph against the installed apps' step catalog and the trigger RecType's fields, then compiles it to an ordered plan. The desk runs the same check through WASM, so the builder shows the errors a save would.
A graph
{
"nodes": [
{"id": "start", "type": "trigger"},
{"id": "low", "type": "condition", "inputs": {"if": {"expr": "doc.qty < 10"}}},
{"id": "mail", "type": "send_email", "inputs": {"to": {"expr": "doc.owner"}, "subject": "Reorder"}},
{"id": "reply", "type": "wait_reply", "inputs": {"hours": 72}},
{"id": "alert", "type": "notify", "inputs": {"text": {"expr": "steps.reply.body"}}}
],
"edges": [
{"from": "start", "to": "low"},
{"from": "low", "to": "mail", "branch": "true"},
{"from": "mail", "to": "reply"},
{"from": "reply", "to": "alert", "branch": "reply"}
]
}| Part | Rule |
|---|---|
Node id | Letters, digits and _, not starting with a digit, since expressions name it in steps.<id>.<output> |
trigger node | Exactly one. It takes no inputs. The Automation's trigger field says when a run starts |
Edge without branch | On the branch next |
| A branch to several nodes | They start in parallel |
| A branch with no edge | Ends that path |
| An input value | A literal in wire form, or {"expr": "..."}. A JSON float is refused |
What the check does
Checks the shapes of the graph, catalog and fields. At most 50 nodes.
Checks node ids, the single trigger, known step types, edges to known nodes and declared branches.
Finds nodes the trigger cannot reach, and loops that pass through no wait step.
Types each input: required inputs present, literals and expressions of the input's type, fields the RecType has. A step may read only outputs of steps on every path from the trigger to it.
Orders the nodes, trigger first, so outside a loop each node comes before the nodes it leads to.
A refused graph returns every problem as {code, message, node, input, pos}, such as unreachable, cycle_without_wait, missing_input, type_mismatch or unknown_field.
The plan
The plan lists each step once with its inputs and its next nodes per branch. Each expression input carries its type. Two more lists follow:
| List | Holds | Used for |
|---|---|---|
literals | Each string a step compares with a doc field or writes into one | The save check against Select options and Link targets |
refs | The doc.<field> and steps.<node>.<output> names each input reads | The wires the builder draws |
At run time, resolve_inputs evaluates a step's expressions against the Record and the earlier steps' outputs. Elixir calls it through the flow_inputs NIF. A step that has not run reads as null.
Step catalog entries
Each step module's schema/0 gives one catalog entry:
{
"wait_reply": {
"inputs": {"hours": {"type": "number", "required": true}},
"outputs": {"body": "string"},
"branches": ["reply", "timeout"],
"wait": true
}
}Input types are cardinal_expr's (boolean, number, string, date, datetime, time, table), plus field, a field of the trigger RecType, and any. type_of makes an input take another input's field type. literal: true refuses expressions. branches defaults to ["next"].