Loops & Workflows
Describe recoverable execution with explicit states.
for iterates a collection. workflow divides a longer task into recoverable states. This example totals scores, then transitions to done to return the result.
Complete example
Save the example as workflows.aal
agent Tutorial is Agent
public
ability run(scores: List<Int>) -> Int effects(workflow)
workflow "total-scores" persist "scores.json"
state sum
total = 0
for score in scores
total = total + score
-> done
state done
return totalSyntax explained
-
The first
stateis the entry; -> done transitions to anotherstate. Every reachable path mustreturnor transition, and every declaredstatemust be reachable. -
wait until condition is valid only inside a
state. A false condition suspends the invocation; resumption replays from thatstate’s entry. -
Transitions commit execution
stateand pending memory. The kernel cannot roll back external effects; this is not a distributed exactly-once guarantee.
Build and run
cargo run --locked --bin aalc -- check workflows.aal
cargo run --locked --bin aalc -- build workflows.aal -o workflows.aalc
cargo run --locked --bin aalc -- run workflows.aalc \
--agent Tutorial --ability run \
--args '{"scores":[4,6,8]}' \
--state-dir .aal-tutorial/workflows --invocation workflows-1Run with a fresh state directory. The returned value is shown below; the CLI also prints execution information.
18Try it yourself
Run scores [10,20] with a new invocation ID to get 30. Try an empty list to get 0.