--- id: SG-4 family: RT-SG level: extended profiles: [runtime] posture: normative-target added: "1.0" changed: "1.0" source: https://flowdrop.io/spec/rules/rt-sg/sg-4 specification: FlowDrop Workflow Specification 1.0-draft licence: CC BY 4.0 --- # SG-4 — A ForEach node initializes its iterator once and completes on an empty list *RT-SG (Part II) · level: extended · profiles: runtime · added in 1.0* ## The rule > **Normative.** This is the rule. > > 1. A ForEach node initializes its iterator on the round where the state carries none, and requires items to iterate. > > 2. A value that is not a list is wrapped as a single item, and the items are re-indexed so that iteration is positional. > > 3. An empty list completes the node immediately rather than entering the loop. > > 4. On completion the node emits its defined output set, including the results collected across the rounds it ran. ## What it means An empty list is not a loop that runs zero times — it is completion on the first round, with the node's whole defined output set produced immediately, current item included as absent rather than left unset. A value that is not a list at all is not refused either: it is wrapped as the one item a single round iterates, re-indexed the same way any list would be, so a round that receives a bare scalar behaves exactly like a round over a one-item list. ## Example ```json title="A ForEach launched with no items" {"items": []} ``` ```json title="What it emits on that round" verdict="completed" {"loop_active": false, "current_item": null, "total_count": 0, "collected_results": []} ``` ```json title="A ForEach launched with one scalar, not a list" {"items": "scalar_value"} ``` ```json title="What it emits on that round" verdict="wrapped" {"current_item": "scalar_value", "current_index": 0, "total_count": 1, "is_first": true, "is_last": true} ``` ## Related rules - Names: SG-5, SG-7 - Referenced by: SG-5 --- Rule identifiers are permanent and are never renumbered. This specification carries no implementation status: each implementation publishes its own standing against these rules. Licensed CC BY 4.0.