# How it works

> From an idea to a use case, to code, to production, and back to the map.

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

For agents: Cases lifecycle: (1) write or generate use cases (visual editor, YAML files, or Sypnorm code via `map`) with their start, place and outcomes; (2) the map is versioned and approved (proposals); (3) `cases generate` writes each case's typed skeleton and people or the agent write the rules inside (useCase, ctx.db, ctx.outcome); commits carry `Case: <id>`; (4) `cases check` compares code and map (scope errors, drift, orphans, loops, places); (5) the runtime runs each area as one service: starts become routes, endpoints, jobs and subscriptions, outcomes go to the caller, to listeners through the outbox, and always to Telemetry; (6) Telemetry measures each case and real clicks confirm the navigation map; (7) changes go back to the map as proposals.

**Figure**: Sypnia Cases in the middle of a project. The map is plain files; the server, the editor and the links read and write the same files.

1. You, your team or your client write use cases, in the editor or as files.
2. The map is stored as files in your repository and, in Sypnia Cloud, in the Cases server.
3. Code is written per case from its generated skeleton; commits say which case they touch.
4. The check compares the code with the map on every change.
5. The runtime runs each area and sends outcomes on; Telemetry reports each case, so the map shows how each flow behaves.

## One flow, from the idea to production

### 1. Someone writes the use case

The bakery owner writes, in the editor: _«A customer orders bread for tomorrow and picks it up in the morning.»_ Cases turns it into a case: an actor (customer), a goal, how it starts (in the app, at «Shop › Order for tomorrow»), how it ends (`OrderPlaced`) and the first failure she thinks of: _that bread is sold out_. A programmer or the coding agent fills in the details: the payload types, the data it reads and writes, who is allowed.

### 2. The map is approved

The new case is a **proposal**. The owner and the programmer approve it; the map gets a new **version**. That version is now the scope: building it is in scope, building anything else is not.

### 3. The code is written per case

`npx @sypnia/cases generate` writes the case's skeleton from the file: its typed input, its outcomes and failures, the tables it may touch. A person or the agent writes the rules inside, with [Sypnorm](https://cases.docs.sypnia.com/sypnorm): `ctx.db` to read and write, `ctx.outcome('OrderPlaced', …)` to end. Without Sypnorm, commits carry a trailer `Case: order-for-tomorrow`, and the check reads them.

### 4. The check keeps code and map together

`cases check` runs in the terminal and in CI. It fails when the code does something no case says (a **scope error**), when a promised case is not built, when code and case disagree (**drift**), when a case has no way to start, and when events form a loop nobody said how to stop.

### 5. The runtime runs it

Each area runs as one service. Its starts become real: routes for `in_app`, endpoints for `external`, jobs for `scheduled`, subscriptions for `event`. Each outcome goes back to whoever waits, to every case that listens (through an outbox saved with the data), and always to Telemetry ([Running cases](https://cases.docs.sypnia.com/runtime)).

### 6. Production reports per case

In Sypnia Cloud, every case shows how often it runs, how it ends, its errors and its latency, from Sypnia Telemetry. Real clicks confirm the navigation map. An ending or a path the map does not describe shows up as a gap.

### 7. Changes go back to the map

New ideas, bugs that reveal a missing failure, a client asking for more: each one starts as a proposal on the map, not as code.

## What lives where

| Thing                                   | Where it lives                                                                      | Who can read it                                          |
| --------------------------------------- | ----------------------------------------------------------------------------------- | -------------------------------------------------------- |
| The map                                 | `cases/` in your repository (open format); in Sypnia Cloud also in the Cases server | anyone with the files; in Cloud, the workspace's members |
| Proposals, comments, approvals          | the Cases server                                                                    | the workspace's members and invited reviewers            |
| Links to Data, Telemetry, Git, Agent    | read live from those products, never copied                                         | the same people who can read those products              |
| The outbox, deliveries and dead letters | the `sypnia_cases` schema of your app's own database                                | your app's team, in the map and in Telemetry             |
