# Use cases

> Every part of a use case, what it is for, and how to write it well.

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

For agents: Use case fields: id (kebab-case, stable), title (plain words), actor (one primary actor id), goal (one sentence), starts (how it starts: in_app | { external: webhook|api, idempotency_key, respond, input, signature } | { scheduled: 'every 2h' | cron, timezone } | { event: <area.case.Outcome.vN> } | { event: { on: [...] | on_all: [...], key, within, filter, order_by } }), where (optional place id in the navigation map, only for in_app, e.g. shop.order-for-tomorrow), preconditions, steps, input (typed fields), outcomes (named, PascalCase, typed payload: { OrderPlaced: { order_id: uuid, ready_at: datetime } }; long form { payload, says }), failures (outcomes too: { SoldOut: «tell» } or long form { when, tell, payload }; the list form - name/when/tell is accepted), data (reads/writes), allowed (actor ids), ai ({ tasks: [...] } Sypnia AI tasks it calls), files ({ reads, writes }: Sypnia Files buckets), mail ({ sends: [...] }: Sypnia Mail templates), loop ({ bounded: true, max, until } only to close a reviewed cycle), retries ({ max, backoff }), status (idea|approved|building|live|retired), owner, notes. Required: id, title, actor, goal, starts, and at least one outcome. Every case ends in a declared outcome.

A use case is the unit of the map. It is written in plain words first; the technical parts come later, from a programmer, the coding agent or the code itself.

`cases/orders/order-for-tomorrow.case.yaml`

```yaml
id: order-for-tomorrow
title: Order for tomorrow
actor: customer
goal: Get bread reserved for tomorrow morning
starts: in_app
where: shop.order-for-tomorrow          # Shop › Order for tomorrow
preconditions:
  - The bakery bakes tomorrow
steps:
  - The customer picks products and quantities
  - The system checks what is left for tomorrow and reserves it
  - The customer gets a pick-up code
input:
  items: [{ product_id: uuid, qty: int }]
  pickup_at: datetime
outcomes:
  OrderPlaced: { order_id: uuid, ready_at: datetime }
failures:
  SoldOut: «That bread is sold out for tomorrow. Pick another?»
  Closed:
    when: the bakery does not bake that day
    tell: «We don't bake that day. Try the next one.»
data:
  reads: [product, bake_day]
  writes: [order, order_item]
allowed: [customer]
status: approved
owner: ana@laespiga.co
```

## The parts

| Part             | Required | What it is for                                                                                                                                                                                                             |
| ---------------- | -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `id`             | yes      | A stable name in `kebab-case`. Code, commits, events and traces use it; renaming the title never changes it.                                                                                                               |
| `title`          | yes      | What people call it, in their words.                                                                                                                                                                                       |
| `actor`          | yes      | Who starts it: one [actor](https://cases.docs.sypnia.com/map/actors-areas-journeys) of the map. A case started by another system or by the clock has a system actor.                                                       |
| `goal`           | yes      | One sentence: what the actor wants. If it needs two sentences, it is probably two cases.                                                                                                                                   |
| `starts`         | yes      | How it starts: `in_app`, `external`, `scheduled` or `event`, each with its own fields ([How a case starts](https://cases.docs.sypnia.com/map/starts)).                                                                     |
| `where`          | no       | Only for `in_app`: the [place](https://cases.docs.sypnia.com/map/navigation) in the app where it happens, such as `shop.order-for-tomorrow`. Its path, «Shop › Order for tomorrow», is worked out from the navigation map. |
| `preconditions`  | no       | What must be true before it can start.                                                                                                                                                                                     |
| `steps`          | no       | What happens, in order, in plain words. A step can start with another actor: `staff: confirms the order`.                                                                                                                  |
| `input`          | no       | What it receives, as typed fields ([Types](#types)). For `external` starts this is the body the other app sends.                                                                                                           |
| `outcomes`       | yes      | How it ends well: each outcome has a name in `PascalCase` and a typed payload ([Outcomes and events](https://cases.docs.sypnia.com/map/outcomes-and-events)). At least one.                                                |
| `failures`       | no       | How it ends when something expected goes wrong. **Failures are outcomes too**: a name, when it happens, and what to `tell` the person.                                                                                     |
| `data`           | no       | The tables it `reads` and `writes`. With Sypnorm this comes from the code.                                                                                                                                                 |
| `allowed`        | no       | The actors allowed to run it.                                                                                                                                                                                              |
| `ai`             | no       | The [Sypnia AI](https://ai.docs.sypnia.com/using/cases-and-traces/) tasks it calls: `ai: { tasks: [allergen-answer] }`.                                                                                                    |
| `files`          | no       | The [Sypnia Files](https://files.docs.sypnia.com/buckets/use-cases/) buckets it `reads` and `writes`: `files: { writes: [receipts] }`.                                                                                     |
| `mail`           | no       | The [Sypnia Mail](https://mail.docs.sypnia.com/events/use-cases/) templates it `sends`: `mail: { sends: [order-ready] }`.                                                                                                  |
| `loop`           | no       | Only to close a cycle of events on purpose: `loop: { bounded: true, max: 3, until: "…" }` ([Loops](https://cases.docs.sypnia.com/runtime/loops)).                                                                          |
| `retries`        | no       | For a case that starts on an event: how many attempts before dead letters: 5 by default, 1 to 20 ([Delivery](https://cases.docs.sypnia.com/runtime/delivery#retries)).                                                     |
| `status`         | no       | `idea`, `approved`, `building`, `live` or `retired`.                                                                                                                                                                       |
| `owner`, `notes` | no       | Who answers for it, and anything else worth saying.                                                                                                                                                                        |

`data`, `ai`, `files` and `mail` say what the case uses from the other products. The check compares each list with what really happens, in the code and in production: something used and not listed, or listed and never used, is reported.

## Every case ends in a declared outcome

A run of a case ends in exactly one of its outcomes or failures, never in silence. The words a person reads, the types in the code and what Telemetry counts all come from that list. An error nobody declared (a bug, a database that does not answer) is not an outcome: it is reported to Telemetry as an error, and a run started by an event goes to its [dead letters](https://cases.docs.sypnia.com/runtime/delivery#dead-letters) after its retries.

## Outcomes and failures, short and long

The short form is the one most cases need:

```yaml
outcomes:
  OrderPlaced: { order_id: uuid, ready_at: datetime }
failures:
  SoldOut: «That bread is sold out for tomorrow. Pick another?»
```

The long form adds words for people and a payload to a failure:

```yaml
outcomes:
  OrderPlaced:
    says: The bread is reserved until 10 am tomorrow
    payload: { order_id: uuid, ready_at: datetime }
failures:
  SoldOut:
    when: a product sold out while ordering
    tell: «That bread is sold out for tomorrow. Pick another?»
    payload: { product_id: uuid }
```

An outcome with a `payload` key is in the long form; otherwise every key is a field of the payload. Failures may also be written as a list (`- name: SoldOut`, `when`, `tell`); both forms mean the same.

## Types

Payloads and inputs use a small set of types, the same in every language Sypnorm generates code for:

| Type                           | Means                                                                      |
| ------------------------------ | -------------------------------------------------------------------------- |
| `string`, `text`               | a short text, a long text                                                  |
| `int`, `number`, `money`       | a whole number, any number, an amount in the smallest unit of the currency |
| `bool`                         | true or false                                                              |
| `uuid`, `email`, `url`         | an id, an e-mail address, an address                                       |
| `date`, `datetime`, `duration` | a day, a moment with its time zone, a span of time (`2h`)                  |
| `[type]`, `{ field: type }`    | a list, an object                                                          |
| `type?`                        | optional                                                                   |
| `one_of(a, b)`                 | one of these words                                                         |
| any other word                 | that exact value, always (`channel: whatsapp`)                             |

Inside a YAML flow mapping, `{ channel: one_of(sms, call) }`, YAML itself splits the value at the comma; the reader joins the pieces back, so it means what it says. Quoting it, `channel: "one_of(sms, call)"`, avoids the question.

## Writing a good use case

- **One actor, one goal.** «Customer orders and staff confirms» is two cases joined by an event or a journey.
- **Steps say what, not how.** «The system reserves the products», not «INSERT into reservation».
- **Name every ending.** An outcome named in the map is something you can count, and a failure named in the map is a message a real person will read. One found in production is a bug.
- **Name outcomes in the past tense.** `OrderPlaced`, `RainExpected`, `BakerTold`: something that already happened.
- **Keep the id when the meaning stays.** Change the title freely; change the id only when it becomes a different case.
