# Outcomes and events

> How every outcome reaches the caller, every listener and Telemetry: the outbox in the case's own transaction, the fast path and the sweep, and what Run guarantees.

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

For agents: Outcome delivery: to the caller (the HTTP answer of in_app/external), to every listener, always to Telemetry (span with sypnia.case, sypnia.case.outcome). Every run ends with ctx.outcome(name, data), outcomes and failures alike. Outbox: when anything listens to an outcome, the runtime inserts it into sypnia_cases.outbox in the same Data transaction as the case's writes (direct runner and Data API atomic transaction); nobody listening = no row. Fast path: right after commit the service publishes its rows (EventBridge in Sypnia Cloud and the engine in your AWS account) and marks them sent; sweep: every minute a sweeper publishes rows unsent for 30 s. Event envelope: id (uuid), name area.case.Outcome.vN, data, occurred_at, key, traceparent, cause (chain of case ids), depth. Each listener: own SQS queue (FIFO with message group = order_by), 5 attempts with backoff, then its dead letters (run cases dlq <case> [--replay]), shown on the map; at least once; the runtime skips an event id the listener already handled (kept 7 days). on_all waits in sypnia_cases.waits by key and window; a window that closes first runs nothing and ends as WindowExpired, visible in Telemetry. Loops: refused at build unless loop: bounded with max and until; at run time a case already in the cause chain, or depth > 10, is not run: LoopStopped on the trace and a Telemetry alert. Without AWS (Docker, your own server): a background task in each service drains the outbox, woken by LISTEN/NOTIFY right after commit and sweeping every minute; listener queues are Postgres tables with the same attempts and dead letters. Limits: event data up to 256 KB (bigger → Files path); name versions: a new version is published beside the old for 90 days, then the old stops.

## Where an outcome goes

When a case ends, its outcome goes to three places, and Run decides how it travels:

| To                                | How                                                                                                                                                        |
| --------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **Whoever is waiting**            | The answer of the request, for a case that starts `in_app` or `external`                                                                                   |
| **Every case that listens to it** | An event, through the outbox, to each listener's own queue                                                                                                 |
| **Sypnia Telemetry**, always      | The span of the run, with `sypnia.case` and `sypnia.case.outcome` ([Telemetry and use cases](https://run.docs.sypnia.com/observe/telemetry-and-use-cases)) |

In the code, every run ends with `ctx.outcome`: the outcome or the failure that happened, with its data ([Sypnorm](https://cases.docs.sypnia.com/sypnorm/use-cases/)):

`src/cases/check-weather.ts`

```ts
if (forecast.rainFrom) return ctx.outcome('RainExpected', { from: forecast.rainFrom });
return ctx.outcome('ClearSkies', {});
```

## The outbox: saved with the data

An outcome that someone listens to is saved **in the same transaction** as the data it describes. When `pause-deliveries` commits, the paused deliveries and the event `deliveries.pause-deliveries.DeliveriesPaused.v1` are written together, or neither is. There is never an event about data that was rolled back, and never data whose event was lost.

Run knows at build time which outcomes have listeners. For those, the runtime adds one row to the table `sypnia_cases.outbox` inside the case's transaction, with the [direct runner](https://cases.docs.sypnia.com/sypnorm/runners/) and with the Data API's all-or-nothing transaction alike. An outcome nobody listens to writes nothing.

Then the event leaves by one of two roads:

### 1. The fast path

Right after the commit, the service that ran the case publishes the rows it just wrote and marks them sent. Listeners usually hear the event within a second.

### 2. The sweep

Every minute, a sweeper publishes any row still unsent after 30 seconds: the service stopped between the commit and the publish, or the bus did not answer. Nothing written is ever left behind.

| Where Run runs                                                                                          | The bus                                                                               | The sweeper                                                                                                 |
| ------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------- |
| **\[Cloud]** Sypnia Cloud                                                                               | Amazon EventBridge, one bus per app environment, and an Amazon SQS queue per listener | a schedule of the app                                                                                       |
| **\[Engine]** The engine in your AWS account                                                            | the same, in your account                                                             | the same                                                                                                    |
| **\[Engine]** Without AWS: [Docker](https://run.docs.sypnia.com/self-hosting/docker) or your own server | Postgres: each listener's queue is a table                                            | a background task in each service, woken by `LISTEN`/`NOTIFY` right after the commit, sweeping every minute |

## What each event carries

```json
{
  "id": "0193f6c2-7a1e-7c3b-9d2a-5e8f1b4c6a70",
  "name": "weather.check-weather.RainExpected.v1",
  "data": { "from": "2026-11-18T15:00:00-05:00" },
  "occurred_at": "2026-11-18T13:00:04Z",
  "key": null,
  "traceparent": "00-7c2e9a4b1d8f4e6a9b3c5d7e1f2a4b6c-a1b2c3d4e5f60718-01",
  "cause": ["check-weather"],
  "depth": 1
}
```

- **`name`** is `area.case.Outcome.v<n>`. Changing an outcome's data in a way that would break a listener needs a new version; the old one keeps being published next to it for 90 days, then stops, and the build warns every listener still on it with the days left ([Versions](https://cases.docs.sypnia.com/map/outcomes-and-events/#versions)).
- **`traceparent`** carries the trace, so the listener's run is part of the same story in Telemetry: the weather check, the paused deliveries and the message to the baker are one trace.
- **`cause`** and **`depth`** say which cases led here. They are how Run stops a loop.
- **`data`** is at most 256 KB. For something bigger, put it in [Sypnia Files](https://files.sypnia.com) and send its path.

## What Run guarantees

- **At least once.** An event reaches every listener at least once. Getting it twice does what getting it once does: the runtime records the event ids each listener already handled, for 7 days, and skips a repeat before your code runs.
- **Each listener on its own.** Every listener has its own queue and its own retries: 5 attempts with growing waits. After the last one, the event goes to that listener's **dead letters**, shown on its case in the map and in the console. `run cases dlq tell-the-baker` lists them, and `--replay` sends them again once the cause is fixed.
- **Order when you ask for it.** Events are handled in any order unless the start says `order_by: <field>`; then the events with the same value of that field are handled one at a time, in order.
- **Waiting for several.** An `on_all` start keeps what it has seen for each key until all of its outcomes arrived within the window, and runs once. A window that closes first runs nothing: it ends as `WindowExpired`, with what had arrived, on the case in the map and in Sypnia Telemetry, among the case's outcomes and on the trace of the last event that arrived.

## Loops

A chain of cases can come back on itself: «Resume deliveries» leading again to «Pause deliveries». Run checks twice:

1. **At build time**, from the map: a chain of listeners that closes a circle stops the build, unless one case in it declares how it stops: `loop: { bounded: true, max: 3, until: "the forecast is clear" }`. A case can never listen to its own outcome.
2. **At run time**, from `cause` and `depth`: an event whose chain already holds the case it would start, or that is more than 10 steps deep, does not run it. The run is recorded as `LoopStopped` on the trace, and Telemetry raises an alert.
