when Guards
REAL
A when is a condition, written as a template. It appears in two
places, and they are evaluated at different moments.
| Where | Evaluated | If false |
|---|---|---|
on an arc (next target or router arc) | when the step it leaves has completed, in the next orchestration pass | that arc is not taken |
on a step (step.when) | when the step is about to be dispatched by an arc | the step is skipped (see below) |
Arc guards: after the step completes
An arc's when is evaluated only in an orchestration pass, and a pass
is triggered by a completion event such as command.completed
(orchestrator.rs, v3.161.2). So the guard can use the result of the
step it leaves:
- step: check_status
tool:
kind: http
method: GET
url: https://api.example.com/v1/jobs/{{ workload.job_id }}
next:
spec: { mode: exclusive }
arcs:
- step: done
when: "{{ output.state == 'finished' }}"
- step: failed
when: "{{ output.state == 'error' }}"
- step: wait_and_retry # default arc: no `when`
In an arc guard, output is the completed step's result data, and
{{ check_status.state }} means the same thing. Everything else a
template can see (4.2) is
available too.
Step guards: before the step runs
A step's own when is checked when an arc is about to dispatch it,
against the context that arc would give it, its set included. When
it is false, the server (v3.161.2, orchestrator.rs):
- records a
step.skippedevent for the step; - follows the skipped step's own
next, taking only the first matching arc, and checks the guard of that step in turn; - stops if there is no matching arc; that path of the workflow ends there.
So a step guard skips one step, not the rest of the path.
The start step's when is never evaluated: the server dispatches
start directly when the execution begins.
What counts as true
The server renders the guard with minijinja. If you omit {{ }}, it
adds them. Then it compares the rendered text. These values are
false, compared case-insensitively after trimming: an empty string,
false, 0, no, none, null, off, {} and []. Anything
else is true (evaluate_condition, orchestrate-core/src/template.rs).
Two consequences:
{{ a and b }}renders the value ofbwhenais true, as Python'sanddoes, so a non-empty value counts as true.- An undefined name renders as an empty string, so a guard on a misspelt variable is false.
On the local runtime
The local runtime (CLI v6.4.1) supports a smaller condition language:
==, !=, 'x' in y, and a bare value tested for truthiness with
the same false-like words. It has no < or > and no and / or
(evaluate_condition, executor/src/condition.rs). For anything more,
compute the decision in a rhai step and test its result. The local
runtime also differs from the server in two
ways that change which path a run takes. CI checks both:
# Chapter 6.2 (local, CLI 6.4.1): an undefined name in a condition is left in place, so the guard is TRUE locally. On a server it renders empty: FALSE.
apiVersion: noetl.io/v2
kind: Playbook
metadata:
name: when_undefined_local
workflow:
- step: start
tool:
kind: rhai
code: |
"start"
next:
- step: a
when: "{{ workload.not_set }}"
- step: b
- step: a
tool:
kind: rhai
code: |
"a"
- step: b
tool:
kind: rhai
code: |
"b"
{
"executed_steps": [
"start",
"a"
],
"final_result": "a",
"playbook_name": "when_undefined_local",
"status": "ok",
"step_results": {
"a": "a",
"start": "start"
}
}
Locally, an unknown name is left in the template as text, and
non-empty text is true. So when: "{{ workload.not_set }}" takes
the arc locally (above: a ran). On a server, the same guard renders
empty and is false.
# Chapter 6.2 (local, CLI 6.4.1): a false step `when` ends the branch locally. On a server, `a` is skipped and its `next` (b) is still followed.
apiVersion: noetl.io/v2
kind: Playbook
metadata:
name: when_skip_local
workload:
go: "false"
workflow:
- step: start
tool:
kind: rhai
code: |
"start"
next:
- step: a
- step: a
tool:
kind: rhai
code: |
"a"
when: "{{ workload.go }}"
next:
- step: b
- step: b
tool:
kind: rhai
code: |
"b"
{
"executed_steps": [
"start"
],
"final_result": "start",
"playbook_name": "when_skip_local",
"status": "ok",
"step_results": {
"start": "start"
}
}
Locally, a step whose when is false is skipped and its branch
ends: b never runs. On a server, a is skipped and its next is
followed, so b runs.