Nested loops keep separate rounds, tangled ones are merged
The rule
- Where one loop's body is a strict subset of another's, the two MUST stay distinct and each MUST carry its own round, unless a tail of the outer loop, other than a head of the inner loop, lies inside the inner loop's body and has a forward edge to another node of that body.
- Advancing an outer loop's round MUST reset every loop nested inside it to round 0 and clear the iterators of the ForEach nodes in it (SG-4).
- Loops that share nodes without one containing the other, including two heads with identical bodies, MUST be merged into a single loop over the union of their bodies.
- A loop with such a tail inside the body of a loop nested within it MUST be merged with that nested loop the same way.
- A head of a loop is any head of the loopback edges the loop was formed from, including the heads of the loops merged into it.
- Any other tail of the outer loop inside the nested body leaves the two loops nested: a head of the nested loop, or a tail with no forward edge into the nested body.
- Where a job's completion takes a loop round again, that loop MUST go round first, resetting every loop nested in it, before any other edge of the job is followed; where it takes several nested loops round, the outermost goes round first.
- The job's round on that loop has then ended, so its loopback edges into a loop nested in it are not followed, and its edges into that loop's body lead into a round that has ended (SG-21).
- Merging MUST proceed in steps.
- Each step MUST merge at once every set of loops linked by these two conditions, and the steps MUST repeat until no two loops meet either condition.
- A merged loop MUST be keyed by the head of the largest body among the loops it merges, ties going to the head whose identifier sorts first by code point.
- A loopback edge of any of the merged loops, when followed, MUST advance the merged loop's round.
- Every merge, and every loopback edge that closes no cycle, MUST be reported to the author as a static diagnostic on the workflow rather than as a run-time warning.
- The report of a merge MUST name each loopback edge of the merged loop whose head has an incoming execution edge from a node of the merged body that cannot be reached over the forward graph from any head of the loopback edges that leave its tail on the same branch into the merged loop, those heads included, since an edge on another branch is not followed with it.
- Following such an edge creates a job on the new round whose source does not run on that round, so SG-21 skips the job and the edge cannot take the merged loop round.
What it means
Two loops can relate to each other in three ways, and only one of them leaves both loops standing as drawn. Where one loop's body is a strict subset of another's, the two stay distinct, each with its own round. Advancing the outer loop's round resets every loop nested inside it back to round 0; without that reset an inner loop would spend its whole budget once for the entire run, and a round number on the inner loop would mean nothing across different passes of the outer one.
Nesting has one condition: no tail of the outer loop may lie inside the inner loop's body and have a forward edge to another node of that body, unless the tail is a head of the inner loop. Such a tail does two things when it completes. It starts the outer loop's next round, which resets the inner loop, and it continues the inner loop's current round partway through, along its forward edges. The reset and the inner round it interrupts would race, so the two loops are merged instead. A ForEach inside a ForEach does not meet this: the outer tail sits after the inner loop, not inside it. "A head" here means any head of the loopback edges the inner loop was formed from, so it includes the heads of loops merged into it.
Two kinds of outer tail inside the inner body leave the loops nested. One is a
head of the inner loop, which stands at the boundary of an inner round, not in
the middle of one. An inner ForEach whose completed port is wired straight
into the outer loopback edge is the common case: the head that finishes the
inner loop also takes the outer loop round. The other is a tail with no forward
edge into the inner body, which has no inner round to continue. In both cases
the order is fixed: when a job's completion takes a loop round again, that loop
goes round first and resets every loop nested in it, and only then are the
job's other edges followed. The job's round on that loop has then ended, so its loopback
edges into a nested loop are not followed, and what its edges into that loop's body start
belongs to a round that has just ended: SG-21 lets it finish without turning
either loop.
Where two loops share nodes without one containing the other, including two heads whose bodies turn out identical, there is no consistent way to assign a round to the shared nodes. With bodies computed as SG-16 computes them, this happens where a cycle can be entered at two different nodes. The engine does not refuse the workflow over it. It merges the loops into one coarser loop over the union of their bodies, keyed by whichever head has the larger body, ties going to the head that sorts first. Every loopback edge of the merged loops then turns that one loop, and where one job follows several of them at once, the loop goes round once and each of their heads starts the new round.
Merging can make new overlaps, so it runs in steps: each step merges every
group of loops that call for it at once, and steps repeat until none do.
Merging a whole group at once, rather than pair by pair, keeps the result
independent of the order the pairs were found in. Merging rather than
refusing is deliberate, and it is sound in one sense: a coarser loop never
claims two nodes are in different rounds where the topology cannot say so, so
no job ever mixes rounds. It is not free. A loopback edge into a head that
needs a value from elsewhere in the merged body cannot turn the merged loop
unless the same tail also takes round again a head that leads to that value:
otherwise the head's job on the new round waits for a source that does not run
on that round, and SG-21 skips it. With a→p→b→d and loopback edges from d
to both a and b, the loops are not merged: d has no forward edge into the
body of the loop headed by b, so the two stay nested and each edge works as
drawn. When d follows both, the outer loop goes round first and the edge into
b is not followed. In the shape below, the merge makes an edge inert. Every merge, and every loopback
edge that closes no cycle, is surfaced to the author as a static diagnostic on
the workflow, not as a run-time warning, and the report of a merge names each
loopback edge that can no longer turn the loop, so the author knows which of
the edges they drew is now inert.
Example
An inner loop nested inside an outer one:
{
"edges": [
{"source": "outer_head", "target": "inner_head"},
{"source": "inner_head", "target": "inner_tail"},
{"source": "inner_tail", "target": "outer_tail"}
],
"loopbacks": [
{"source": "inner_tail", "target": "inner_head"},
{"source": "outer_tail", "target": "outer_head"}
]
}{"inner_head": ["inner_head", "inner_tail"], "outer_head": ["inner_head", "inner_tail", "outer_head", "outer_tail"]}The shared node inner_tail belongs to both loops, and the outer tail lies
outside the inner body. A cycle that can be entered at two nodes resolves
differently:
{
"edges": [
{"source": "trigger", "target": "a"},
{"source": "trigger", "target": "b"},
{"source": "a", "target": "x"},
{"source": "b", "target": "x"}
],
"loopbacks": [
{"source": "x", "target": "a"},
{"source": "x", "target": "b"}
]
}{"a": ["a", "b", "x"]}The bodies {a, x} and {b, x} share x and neither contains the other.
Both hold two nodes, so the tie-break decides: a sorts first and keys the
merged loop. An outer tail inside a nested loop merges as well:
{
"edges": [
{"source": "trigger", "target": "a"},
{"source": "a", "target": "b"},
{"source": "b", "target": "c"},
{"source": "c", "target": "d"}
],
"loopbacks": [
{"source": "c", "target": "a"},
{"source": "d", "target": "b"}
]
}{"a": ["a", "b", "c", "d"]}The bodies are {a, b, c, d} and {b, c, d}, so the second is nested in the
first, but c, a tail of the outer loop, lies inside it: when c completes,
it would start the outer loop's next round and continue to d in the inner
loop's current round. One merged loop, keyed by a, has a single round
instead. The diagnostic names d to b: b needs a, which runs only when
c takes the loop round, so a round started from d skips b and the loop
stops there. Unless c takes the loop round itself, the drawn inner loop never
turns. Wiring the inner head itself back to the outer head keeps the loops
nested:
{
"edges": [
{"source": "trigger", "target": "outer"},
{"source": "outer", "target": "inner"},
{"source": "inner", "target": "log_inner"},
{"source": "outer", "target": "end"}
],
"loopbacks": [
{"source": "log_inner", "target": "inner"},
{"source": "inner", "target": "outer"}
]
}{"inner": ["inner", "log_inner"], "outer": ["inner", "log_inner", "outer"]}Why
Fixtures
Test cases whose outcome this rule decides. A case compares absent, null and empty members exactly; the fixtures README defines each operation. Where a case and the rule disagree, the rule is right.
An inner head that loops back to the outer head stays nested resolve-loops
The inner ForEach's completed port feeds the outer loopback edge, so the inner head is a tail of the outer loop; a nested head is not a tail that interrupts a nested round, so the loops do not merge.
{ "given": { "edges": [ { "source": "trigger", "target": "outer" }, { "source": "outer", "target": "inner" }, { "source": "inner", "target": "log_inner" }, { "source": "outer", "target": "end" } ], "loopbacks": [ { "source": "log_inner", "target": "inner" }, { "source": "inner", "target": "outer" } ] }, "expect": { "loops": { "inner": [ "inner", "log_inner" ], "outer": [ "inner", "log_inner", "outer" ] } } }An inner tail wired forward to the outer tail nests too resolve-loops
{ "given": { "edges": [ { "source": "trigger", "target": "outer" }, { "source": "outer", "target": "inner" }, { "source": "inner", "target": "log_inner" }, { "source": "log_inner", "target": "log_outer" }, { "source": "inner", "target": "log_outer" }, { "source": "outer", "target": "end" } ], "loopbacks": [ { "source": "log_inner", "target": "inner" }, { "source": "log_outer", "target": "outer" } ] }, "expect": { "loops": { "inner": [ "inner", "log_inner" ], "outer": [ "inner", "log_inner", "log_outer", "outer" ] } } }A ForEach inside a ForEach nests rather than merging resolve-loops
The inner tail's only outgoing edge is its loopback edge; the backward walk from the outer tail crosses it, so the inner loop lies inside the outer one.
{ "given": { "edges": [ { "source": "trigger", "target": "outer" }, { "source": "outer", "target": "inner" }, { "source": "inner", "target": "log_inner" }, { "source": "inner", "target": "log_outer" }, { "source": "outer", "target": "end" } ], "loopbacks": [ { "source": "log_inner", "target": "inner" }, { "source": "log_outer", "target": "outer" } ] }, "expect": { "loops": { "inner": [ "inner", "log_inner" ], "outer": [ "inner", "log_inner", "log_outer", "outer" ] } } }An outer tail inside a nested loop merges the two loops resolve-loops
c is the outer loop's tail and lies in the inner loop's body, so its completion would both start the outer loop's next round and continue the inner loop's current one.
{ "given": { "edges": [ { "source": "trigger", "target": "a" }, { "source": "a", "target": "b" }, { "source": "b", "target": "c" }, { "source": "c", "target": "d" } ], "loopbacks": [ { "source": "c", "target": "a" }, { "source": "d", "target": "b" } ] }, "expect": { "loops": { "a": [ "a", "b", "c", "d" ] } } }A side feed into a nested loop does not pull the outer tail in resolve-loops
The walk back from the inner tail reaches the outer loop's nodes only through prepare, which the inner head cannot reach, so it stops there.
{ "given": { "edges": [ { "source": "trigger", "target": "outer" }, { "source": "outer", "target": "prepare" }, { "source": "outer", "target": "inner" }, { "source": "inner", "target": "check" }, { "source": "prepare", "target": "check" }, { "source": "check", "target": "inner_tail" }, { "source": "inner_tail", "target": "outer_tail" }, { "source": "inner", "target": "outer_tail" } ], "loopbacks": [ { "source": "inner_tail", "target": "inner" }, { "source": "outer_tail", "target": "outer" } ] }, "expect": { "loops": { "inner": [ "check", "inner", "inner_tail" ], "outer": [ "check", "inner", "inner_tail", "outer", "outer_tail", "prepare" ] } } }Three ForEach loops inside each other give three nested loops resolve-loops
{ "given": { "edges": [ { "source": "trigger", "target": "a" }, { "source": "a", "target": "b" }, { "source": "b", "target": "c" }, { "source": "c", "target": "log_c" }, { "source": "c", "target": "log_b" }, { "source": "b", "target": "log_a" }, { "source": "a", "target": "end" } ], "loopbacks": [ { "source": "log_c", "target": "c" }, { "source": "log_b", "target": "b" }, { "source": "log_a", "target": "a" } ] }, "expect": { "loops": { "a": [ "a", "b", "c", "log_a", "log_b", "log_c" ], "b": [ "b", "c", "log_b", "log_c" ], "c": [ "c", "log_c" ] } } }A cycle entered at two heads is one merged loop resolve-loops
Both bodies hold two nodes, so the lexicographic tie-break keys the merged loop by a.
{ "given": { "edges": [ { "source": "trigger", "target": "a" }, { "source": "trigger", "target": "b" }, { "source": "a", "target": "x" }, { "source": "b", "target": "x" } ], "loopbacks": [ { "source": "x", "target": "a" }, { "source": "x", "target": "b" } ] }, "expect": { "loops": { "a": [ "a", "b", "x" ] } } }