Skip to content

What a loop is (its body, and that it is keyed by its head)

A loopback edge on a canvas draws a cycle, but the loop is the set of nodes that cycle actually turns, loops nested inside it included. Nearly everything about rounds, budgets and staleness rests on this definition.

The rule

Normative: this is the rule
  1. The forward graph is the workflow's edges without its loopback edges and without every edge type excluded from execution.
  2. A loopback edge running from tail S back to head H closes a cycle when S is H or S is reachable from H over the forward graph.
  3. A loopback edge that closes no cycle MUST yield a body containing only its head, rather than being discarded.
  4. It MUST take no part in computing any other loop's body.
  5. For a loopback edge that closes a cycle, the loop's body MUST be {H} ∪ (descendants(H) ∩ back(S, H)).
  6. descendants(H) is the set of nodes reachable from H over the forward graph.
  7. back(S, H) is the set of nodes reached by walking backwards from S, starting at S, over the forward graph and over the loopback edges that close a cycle, entering only nodes in descendants(H), so the walk never passes through H.
  8. The bodies of loopback edges sharing a head MUST be unioned.
  9. A loop MUST be keyed by its head, so two tails looping back to one node are two continuations of one loop with one round counter, and not two loops.
  10. Membership is topological, not observed: a node on a conditional arm inside the body is in the loop even on rounds it does not run, while a node that cannot reach the tail over those edges, such as a node downstream of the tail only or a dead end, is outside it, since it cannot influence a later round.

What it means

A loopback edge is drawn from one node back to an earlier one, but the loop it closes is not "everything the loopback edge touches". It is the head, plus every node the head can reach that can also reach the tail. The second half is the part that needs care: "can reach the tail" is decided by walking backwards from the tail, and that walk crosses other loops' loopback edges. This is the natural loop of the back edge, as compilers define it.

Crossing other loopback edges is what lets loops nest. In a ForEach inside a ForEach, the inner loop's tail often has one outgoing edge: its own loopback edge. Over forward edges alone it reaches nothing, so it would not count as upstream of the outer tail, and the outer body would lose it. The walk back from the outer tail reaches the inner head, crosses the inner loopback edge to the inner tail, and so takes the whole inner loop into the outer body.

Two limits keep the walk from taking too much. It stops at the head: it never walks back past it, so an inner loop never pulls in the outer loop around it. And it enters only nodes the head can reach. Without that second limit, a node that feeds the inner loop from the side (from the outer body, not from the inner head) would let the walk escape into the outer loop and pull the outer tail into the inner body.

A loopback edge whose tail the head cannot reach closes no cycle. It still yields a loop, with the head as its only member, but it is left out of every other loop's walk, so it cannot drag nodes into a body through the back door. Tool wires and other edge types excluded from execution are left out of both computations, so a tool consumer downstream of the loop never widens it.

Two clauses are easy to get backwards. A loop is keyed by its head, not by the loopback edge that closes it: two tails looping back to the same node are two continuations of one loop with one round counter. And membership is topological: a node on a conditional arm inside the body is in the loop on every round, including the rounds it does not run.

Example

A ForEach inside a ForEach, as an author draws it. log_inner feeds only its own loopback edge, and the inner ForEach's completed port leads to log_outer:

Nested ForEach nodes
{
  "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"}
  ]
}
The loops it resolves tonested
{"inner": ["inner", "log_inner"], "outer": ["inner", "log_inner", "log_outer", "outer"]}

The walk back from log_outer reaches inner, crosses the loopback edge into log_inner, and stops at outer. Over forward edges alone the outer body would lack log_inner, the two bodies would overlap without either containing the other, and SG-18 would merge them, so the inner loop would never start over for each outer item. The same holds one level deeper: three ForEach nodes inside each other give three nested loops.

A node that feeds a nested loop from the side:

An outer loop whose body feeds the inner loop from the side
{
  "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"}
  ]
}
The inner loop's bodyrestricted
["check", "inner", "inner_tail"]

Walking back from inner_tail, the walk reaches check and then prepare, which inner cannot reach, so the walk does not enter it. Allowed in, it would go on through prepare to outer, cross the outer loopback edge to outer_tail, and report outer_tail, which inner does reach, as part of the inner loop.

A gateway's two arms both looping back into the same head make one loop:

Two tails looping back to one head
{
  "edges": [
    {"source": "trigger", "target": "head"},
    {"source": "head", "target": "gateway"},
    {"source": "gateway", "target": "tail_a"},
    {"source": "gateway", "target": "tail_b"}
  ],
  "loopbacks": [
    {"source": "tail_a", "target": "head"},
    {"source": "tail_b", "target": "head"}
  ]
}
The loop the two tails resolve tounioned
{"head": ["gateway", "head", "tail_a", "tail_b"]}

A loopback edge whose tail the head cannot reach yields the head alone, and lends no edge to another loop's walk:

A loopback edge that closes no cycle, beside one that does
{
  "edges": [
    {"source": "trigger", "target": "h1"},
    {"source": "h1", "target": "s"},
    {"source": "trigger", "target": "x"},
    {"source": "x", "target": "h2"},
    {"source": "h2", "target": "y"},
    {"source": "y", "target": "s"}
  ],
  "loopbacks": [
    {"source": "y", "target": "h2"},
    {"source": "x", "target": "h1"}
  ]
}
The loops they resolve tohead-only
{"h1": ["h1"], "h2": ["h2", "y"]}

These shapes, and the ones SG-18 merges, are conformance cases in fixtures/loops/.

Why

Recorded under OPEN-25, OPEN-58.

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"
          ]
        }
      }
    }

    fixtures/loops/loops-inner-head-as-outer-tail-nests.json

  • 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"
          ]
        }
      }
    }

    fixtures/loops/loops-inner-tail-feeding-outer-tail.json

  • 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"
          ]
        }
      }
    }

    fixtures/loops/loops-nested-foreach-nests.json

  • A loopback edge that closes no cycle yields its head alone resolve-loops

    x cannot be reached from h1, so x to h1 closes no cycle, and it takes no part in the walk for h2's loop either.

    {
      "given": {
        "edges": [
          {
            "source": "trigger",
            "target": "h1"
          },
          {
            "source": "h1",
            "target": "s"
          },
          {
            "source": "trigger",
            "target": "x"
          },
          {
            "source": "x",
            "target": "h2"
          },
          {
            "source": "h2",
            "target": "y"
          },
          {
            "source": "y",
            "target": "s"
          }
        ],
        "loopbacks": [
          {
            "source": "y",
            "target": "h2"
          },
          {
            "source": "x",
            "target": "h1"
          }
        ]
      },
      "expect": {
        "loops": {
          "h1": [
            "h1"
          ],
          "h2": [
            "h2",
            "y"
          ]
        }
      }
    }

    fixtures/loops/loops-non-closing-head-only.json

  • 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"
          ]
        }
      }
    }

    fixtures/loops/loops-outer-tail-inside-inner-merges.json

  • 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"
          ]
        }
      }
    }

    fixtures/loops/loops-side-feed-stays-outside.json

  • 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"
          ]
        }
      }
    }

    fixtures/loops/loops-three-level-nests.json

  • Two tails looping back to one head make one loop resolve-loops
    {
      "given": {
        "edges": [
          {
            "source": "trigger",
            "target": "head"
          },
          {
            "source": "head",
            "target": "gateway"
          },
          {
            "source": "gateway",
            "target": "tail_a"
          },
          {
            "source": "gateway",
            "target": "tail_b"
          }
        ],
        "loopbacks": [
          {
            "source": "tail_a",
            "target": "head"
          },
          {
            "source": "tail_b",
            "target": "head"
          }
        ]
      },
      "expect": {
        "loops": {
          "head": [
            "gateway",
            "head",
            "tail_a",
            "tail_b"
          ]
        }
      }
    }

    fixtures/loops/loops-two-tails-one-head.json

Rule identifiers are permanent and are never renumbered. Each implementation publishes its own standing against these rules; this specification does not.spec 1.0 · SG-16 · changed in spec 1.1