# Running cases

> What runs your cases, why a case is a unit of meaning and not of deployment, and where every outcome goes.

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

For agents: The Cases runtime: @sypnia/cases in your app (runs each case: checks input, one Data transaction, ctx.outcome, writes the outbox row in the same transaction when someone listens, span to Telemetry) + the dispatcher (Sypnia Cloud and Run on AWS: EventBridge + one SQS queue with DLQ per subscriber; without AWS: a background task in each area service, a tokio task in Rust services, woken by Postgres LISTEN/NOTIFY). One case = a logical unit, not a deploy unit: the cases of an area are bundled into one service (src/areas/<area>.ts, serveArea(area, cases), written by cases generate; one Run app or function per area with use_cases = "cases" in the [run] table of sypnia.toml); Run splits a case into its own service only when needed (cases.split in the [run] table of sypnia.toml: memory, timeout, runtime, concurrency). From the map: in_app → POST /_cases/<case-id> (a route of the area's service, called with the person's Sypnia Data session, run under RLS; a case someone listens to never runs from the browser), external → POST /hooks/<case-id>, scheduled → jobs, event → subscriptions. Delivery decided by the runtime: caller if one waits, outbox if any listener, always Telemetry. Ends: declared outcome or failure; undeclared errors → Telemetry problem, and for event runs retries then dead letters.

The map says what each case does. The **runtime** makes it happen: it starts each case the way the case says, runs it, and sends its outcome wherever it is needed. You do not wire routes, schedules, queues or subscriptions by hand; they come from the case files.

**Figure**: How La Espiga's cases run. Each area is one service; outcomes travel through the outbox to the areas that listen.

1. A person, another app or the clock starts a case in an area's service.
2. The case runs in one Data transaction; its outcome is saved in the outbox in that same transaction when some case listens.
3. The dispatcher picks the outcome up and gives each listener its own copy, with its own retries.
4. The listening area's service runs its case, and the chain goes on.
5. Every run, outcome and failure goes to Telemetry, in one trace.

## Two parts

| Part                            | What it does                                                                                                                                                                                                | Where it runs                                                                                                                                                                                                                 |
| ------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **`@sypnia/cases`** in your app | checks the input, runs the case in one transaction with [Sypnorm](https://cases.docs.sypnia.com/sypnorm), records its outcome, writes the outbox row when someone listens, and reports the run to Telemetry | inside each area's service, in TypeScript                                                                                                                                                                                     |
| **The dispatcher**              | reads the outbox, fans each event out to its listeners, retries, keeps dead letters, stops loops                                                                                                            | Sypnia Cloud and Sypnia Run on AWS: Amazon EventBridge and one Amazon SQS queue per listener. Without AWS: a background task in each area's service, on Postgres ([Delivery](https://cases.docs.sypnia.com/runtime/delivery)) |

## What each start becomes

| `starts`    | What the runtime sets up                                                                                                                                                                                                                                                                                                                                        |
| ----------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `in_app`    | a route of the area's service, `POST /_cases/<case-id>`, that the case's screen calls with the person's Sypnia Data session; the case runs under the project's row-level security. A case some case listens to always runs here, never from the browser ([Runners](https://cases.docs.sypnia.com/sypnorm/runners#a-case-someone-listens-to-runs-on-the-server)) |
| `external`  | a public endpoint, `POST /hooks/<case-id>`, with its input schema, its check of who is calling and its idempotency key                                                                                                                                                                                                                                          |
| `scheduled` | a job on its schedule and time zone; a run still going makes the next one skip                                                                                                                                                                                                                                                                                  |
| `event`     | a subscription: a queue of its own, retries and dead letters, and the correlation window for `on_all`                                                                                                                                                                                                                                                           |

## A case is a unit of meaning, not of deployment

A case is how people think about the app. It is not how the app is deployed. **The cases of an area run together in one service**: one app or one function in [Sypnia Run](https://run.docs.sypnia.com/), with its routes, its jobs and its subscriptions. La Espiga has three areas that run code, so it has three services, not eleven.

Run splits a case into a service of its own only when there is a reason:

- the case needs something the others do not: more memory, a longer time limit, a container instead of a function;
- it carries a heavy dependency the others do not, or it must scale apart;
- or Telemetry shows it is crowding the rest of its area.

Splitting is a deploy setting, not part of the case, so it lives in Run's table of `sypnia.toml`, the project's one configuration file:

`sypnia.toml`

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

[run.apps.laespiga.cases.split.render-weekly-menu]
memory = 2048
timeout = 300
```

A split changes nothing in the map: the case keeps its area, its outcomes and its listeners.

### An area's service

`npx @sypnia/cases generate` writes one entry file per area, next to the cases' skeletons:

`src/areas/deliveries.ts`

```ts
import { serveArea } from '@sypnia/cases/sypnorm';
import { pauseDeliveries } from '../cases/deliveries/pause-deliveries';
import { tellTheBaker } from '../cases/deliveries/tell-the-baker';
import { chooseAnotherChannel } from '../cases/deliveries/choose-another-channel';

export default serveArea('deliveries', [pauseDeliveries, tellTheBaker, chooseAnotherChannel]);
```

On Sypnia Run, `use_cases = "cases"` in the app's table of `sypnia.toml` deploys every area this way, with its endpoints, jobs and subscriptions. Anywhere else, the file is an ordinary Node server: run it on the port in `PORT`. Its background task delivers events between areas through Postgres ([Delivery](https://cases.docs.sypnia.com/runtime/delivery#in-sypnia-cloud-and-without-aws)).

## Where an outcome goes

The runtime decides, from the map ([Outcomes and events](https://cases.docs.sypnia.com/map/outcomes-and-events#where-an-outcome-goes)):

- **to the caller, if one waits**: the person on the screen, or an `api` start with `respond: outcome`;
- **to the outbox, if some case listens**, saved by the area's service in the same transaction as the data it describes (the browser never writes it);
- **always to Telemetry**, as the outcome of the run.

## How a run ends

| The run ends with        | The person or caller gets                                       | Telemetry records             | For a run started by an event                                                                         |
| ------------------------ | --------------------------------------------------------------- | ----------------------------- | ----------------------------------------------------------------------------------------------------- |
| a declared outcome       | its payload                                                     | the outcome, counted per case | the event is done                                                                                     |
| a declared failure       | its words (`tell`) and payload; the run's own writes are undone | the failure, by name          | the event is done: a failure is an answer, not something to retry                                     |
| an error nobody declared | a short generic message with a reference                        | a problem, with the trace     | retried, then kept in the [dead letters](https://cases.docs.sypnia.com/runtime/delivery#dead-letters) |
