# How use cases run

> What Run reads from your map, how the cases of an area become one service, and when a case gets a service of its own.

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

For agents: Run reads the map (cases/*.case.yaml, or use_cases = "sypnorm" in the [run] table of sypnia.toml) at build time and matches each case with the Sypnorm useCase that names its id (place-order ↔ useCase('place-order', …)); a case on the map without code, or code without a case, fails the build like cases check. The cases of one area run together in one service <app>-<area> (Lambda by default, Fargate with runtime = "fargate"), deployed with the app as part of the same immutable deploy. Code in other languages: cases.handlers maps a case to a function/container route returning {outcome|failure, data}; the handler writes its own outbox row. Split a case into its own service with cases.split in the app's table of sypnia.toml (memory, timeout, runtime, concurrency) when it needs other resources. Each run: input checked against the case's schema, actor from the Sypnia Data session (in_app) or the start's credential, one transaction, span with sypnia.case and sypnia.case.outcome, outcome delivered. Deploys show which cases were added, removed or changed.

## What Run reads

At build time Run reads two things and puts them together:

| From         | What                                                                                                | Where                                                                                               |
| ------------ | --------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------- |
| **The map**  | each case's id, area, how it starts (`starts`), where it lives (`where`), its outcomes and failures | the `cases/` folder of your repository, in the [open format](https://cases.docs.sypnia.com/format/) |
| **The code** | each case's rules, written with [Sypnorm](https://cases.docs.sypnia.com/sypnorm/)                   | your source, wherever `useCase(…)` is exported                                                      |

Each case on the map is matched with the code that names its id: `place-order` on the map, `useCase('place-order', …)` in the code. A case on the map with no code, or code that no case describes, stops the build with the same message [`cases check`](https://cases.docs.sypnia.com/contract/the-check/) gives, so nothing reaches production off the map.

Turn it on in the app's table of `sypnia.toml`:

`sypnia.toml`

```toml
[run.apps.laespiga]
kind = "ssr"
use_cases = "cases"
```

`use_cases` is the folder of the map (`"cases"`), or `"sypnorm"` to build the map from the code alone.

### Code in another language

Sypnorm is TypeScript. A case written in another language, such as the Rust of the Telemetry engine, is named in `cases.handlers` with the function or container route that runs it:

`sypnia.toml`

```toml
[run.apps.telemetry.cases.handlers]
roll-up-hour = "engine:/internal/cases/roll-up-hour"
```

Run calls that route with the checked input and the actor, and the handler answers `{ "outcome": "HourRolled", "data": { … } }` or `{ "failure": "…", "data": { … } }`. A handler whose outcome has listeners inserts the event into `sypnia_cases.outbox` in its own transaction, with the columns on [Outcomes and events](https://run.docs.sypnia.com/cases/outcomes-and-events#what-each-event-carries); everything else, from schedules to dead letters, works the same.

## One service per area

A case is a unit of meaning, not of deployment. The cases of one **area** run together in **one service**, named `<app>-<area>`:

| Area of La Espiga | Its cases                                           | Its service           |
| ----------------- | --------------------------------------------------- | --------------------- |
| `orders`          | `order-for-tomorrow`, `rappi-order`, `cancel-order` | `laespiga-orders`     |
| `weather`         | `check-weather`                                     | `laespiga-weather`    |
| `deliveries`      | `pause-deliveries`, `tell-the-baker`                | `laespiga-deliveries` |

A service is a Lambda function by default, and a container on Fargate with `runtime = "fargate"`, like any [function or container](https://run.docs.sypnia.com/apps/functions) of Run. It is part of the app's deploy: built from the same commit, kept unchanged, promoted and rolled back with everything else.

Fewer services mean fewer cold starts and one place to look. When one case needs something different, give it a service of its own:

`sypnia.toml`

```toml
[run.apps.laespiga]
use_cases = "cases"

[run.apps.laespiga.cases.split.print-kitchen-ticket]
memory = 1024
timeout = 120
```

Good reasons to split: a case needs much more memory or time than its area, it carries a heavy dependency the others do not, or it must scale apart. A split case keeps its area: it can still listen only to what its area may hear.

## What happens on each run

1. **The start arrives**: a request, a call from another app, the clock or an event ([How each case starts](https://run.docs.sypnia.com/cases/starts)).
2. **The input is checked** against the case's schema. Invalid input never reaches your code: the caller gets `422` with the field that failed.
3. **The actor is known**: the person's [Sypnia Data](https://data.docs.sypnia.com/) session for a case that starts in the app, the caller's credential for an external one, and the machine actor of the area for schedules and events.
4. **The case runs** in one transaction, through the runner of [Sypnorm](https://cases.docs.sypnia.com/sypnorm/runners/).
5. **The outcome travels**: back to the caller, to every case that listens to it, and to [Sypnia Telemetry](https://run.docs.sypnia.com/observe/telemetry-and-use-cases) as the span of the run, with `sypnia.case` and `sypnia.case.outcome` ([Outcomes and events](https://run.docs.sypnia.com/cases/outcomes-and-events)).

## In each deploy

Every deploy records the map it was built with. Its page, and the comment on the pull request of a [preview](https://run.docs.sypnia.com/deploys/previews), list the cases that were **added**, **removed** or **changed** compared with production, with what Run creates or removes for each one when it is released: a new route, a schedule that stops, a subscription that starts. Nothing changes in production until the deploy is released.
