# Loops

> How a cycle between cases is found before it ships, how to allow one on purpose, and how one is stopped if it happens anyway.

Applies to: the Sypnia engine (open source, self-hosted) and Sypnia Cloud (paid service).  
Source: https://cases.docs.sypnia.com/runtime/loops/

For agents: Design time: the check builds the event graph from every starts.event and finds cycles (strongly connected components). A cycle is an error (`loop`) unless one case in it declares loop: { bounded: true, max: <1-10>, until: <plain-words condition> } (one case of the cycle is enough), reviewed like any change; a case listening to its own outcome is always an error (`self-subscription`). Runtime: each event carries cause (the chain of case ids) and depth; the dispatcher stops a delivery when the case already appears in the chain (beyond a bounded loop's max) or depth would exceed 10: the run is not started, the delivery ends as LoopStopped (a runtime outcome, never retried), Telemetry opens a problem with the chain and sends an alert. Limit is fixed at 10.

Cases that start on each other's outcomes can form a circle: A starts B, B starts C, C starts A again. Sometimes that is what you want (send a reminder until the bill is paid, at most three times). Most of the time it is a mistake that would run forever. Sypnia Cases finds circles **when you design**, and stops them **when they run**.

## At design time: the check refuses cycles

The check builds the event graph from every `starts: { event: … }` of the map and looks for cycles (it finds the strongly connected parts of the graph). A cycle is an error:

```text
✗ loop   deliveries.pause-deliveries → deliveries.resume-deliveries → deliveries.pause-deliveries
         say how it stops: loop: { bounded: true, max, until } on one case of the cycle
```

In La Espiga, if «Resume deliveries» ended in an outcome that «Pause deliveries» listened to, the check would ask how the loop stops before it ships.

Two rules have no exception:

- **A case never listens to its own outcome** (`self-subscription`).
- **A cycle must say how it stops.** It is allowed only when one case in it declares a bounded loop.

### A loop on purpose

When the baker does not get the WhatsApp message, La Espiga tries another channel, and then the message again. That is a cycle of two cases:

```yaml
# cases/deliveries/tell-the-baker.case.yaml
starts:
  event:
    on: [deliveries.pause-deliveries.DeliveriesPaused.v1, deliveries.choose-another-channel.ChannelChosen.v1]
loop: { bounded: true, max: 3, until: "the baker got the message, or three channels were tried" }
outcomes:
  BakerTold: { channel: one_of(whatsapp, sms, call) }
failures:
  NotDelivered: { when: the channel did not deliver, payload: { channel: string } }

# cases/deliveries/choose-another-channel.case.yaml
starts: { event: deliveries.tell-the-baker.NotDelivered.v1 }
outcomes:
  ChannelChosen: { channel: one_of(sms, call) }
```

| Field     | What it means                                                                                                                                                                           |
| --------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `bounded` | `true`: this cycle is on purpose                                                                                                                                                        |
| `max`     | how many times the case may run in one chain, from 1 to 10                                                                                                                              |
| `until`   | the condition that ends it, in plain words, for the people who approve the map. The cases make it true: here, `BakerTold` ends the chain because nothing listens to it inside the cycle |

One case of the cycle declares the bounded loop, and that is enough for the whole cycle. A bounded loop is a change to the map like any other: someone proposes it and a person approves it.

## At run time: the chain is cut

Every event carries its **cause**, the chain of cases that led to it, and its **depth**. Before starting a case, the dispatcher looks at the chain:

- if the case is already in it (more times than its bounded loop allows), or
- if the new event would be more than **10** events deep,

the case is **not started**. The delivery ends as `LoopStopped`, a runtime outcome that is never retried, and Telemetry opens a problem with the whole chain and sends an alert to the people who watch the project.

```text
LoopStopped   deliveries.pause-deliveries   depth 11
chain         weather.check-weather → deliveries.pause-deliveries → … → deliveries.pause-deliveries
```

This guard exists for what the check could not see: a cycle through another app (your case calls Rappi, Rappi calls your webhook), or a map changed by hand without the check. The limit of 10 is the same for every project.
