# The map from code

> How Sypnorm use cases become the map, and what stays written by people.

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

For agents: `npx @sypnia/cases generate` writes each case's typed skeleton from its file (input, outcomes, failures, touches), .sypnia/cases/types.d.ts and src/areas/<area>.ts; `npx @sypnia/cases map` reads Sypnorm useCase() definitions and emits each one's case id, actor, input, touched tables, the outcomes its code can end in (every ctx.outcome name) and allowed roles; `map --upload --project <x>` sends it as a draft proposal: with a Data project ref (20 lowercase letters) to the Data project's «Use cases», which forwards it to its linked Cases map; with a Cases map's name, straight to the Cases server. Cases merges it: the code is the source of truth for data and allowed actors; outcome and failure names and payloads must agree on both sides; the case file is the source of truth for title, goal, starts, where, steps and messages (tell). Matching by id: useCase('place-order') ↔ the case place-order. An upload is a draft proposal of what the code owns: data and allowed of mapped cases when they differ (table names by singular and plural); a skeleton (starts: in_app, status: building, outcomes and failures by name, area from the upload, the source path src/cases/<area>/…, or the only area) for a use case with no case; a case without code is never retired; { files } uploads make a change per differing case; the check runs and is kept; find_place uses its code sources; a draft becomes open when someone asks for reviews. `cases sync --from-code` writes code-owned fields into the files; differences in owned fields are drift.

With [Sypnorm](https://cases.docs.sypnia.com/sypnorm), the use-case ORM that comes with Cases, the code _is_ a set of use cases. The traffic goes both ways: the skeleton of each case is generated from its file, and the technical side of the map is read back from the code.

```bash
npx @sypnia/cases generate      # a typed skeleton for every case that has none, and the types of every case
```

`generate` writes `src/cases/<area>/<id>.ts` for each new case (never overwriting one that exists) `.sypnia/cases/types.d.ts` with every input, outcome and failure, so `ctx.outcome` only accepts what the case declares, and `src/areas/<area>.ts`, the service of each area.

`src/cases/place-order.ts`

```ts
import { useCase } from '@sypnia/cases/sypnorm';
import { db } from '../db';

export const placeOrder = useCase('place-order', {
  actor: 'customer',
  touches: { reads: [db.products], writes: [db.orders, db.order_items] },
  async run(ctx) {
    // … every path ends in ctx.outcome('OrderPlaced', …), ctx.outcome('OutOfStock', …) or ctx.outcome('BakeryClosed')
  },
});
```

`npx @sypnia/cases map` lists it, and `npx @sypnia/cases map --upload --project <x>` sends it to the map as a draft proposal: `--project la-espiga` names the Cases map and goes straight to the Cases server; a Data project's ref (20 lowercase letters) sends it to that project's «Use cases», and Sypnia Data forwards it to the Cases map linked with it ([The map from your code](https://cases.docs.sypnia.com/sypnorm/the-map)). Cases matches each use case to its case file by the case id it names: `useCase('place-order', …)` ↔ `cases/orders/place-order.case.yaml`.

## Who owns which field

| Field                                                                         | Comes from                                                                         | When they disagree |
| ----------------------------------------------------------------------------- | ---------------------------------------------------------------------------------- | ------------------ |
| `title`, `goal`, `steps`, the words of outcomes and failures (`says`, `tell`) | the case file (people)                                                             | —                  |
| `starts`, `where`, `loop`, `ai`                                               | the case file                                                                      | —                  |
| outcome and failure names and payloads                                        | the case file declares them, `generate` types them, the code must end only in them | drift              |
| `data.reads`, `data.writes`                                                   | the code                                                                           | drift              |
| `allowed`                                                                     | the code (and the policies in Data)                                                | drift              |

```bash
npx @sypnia/cases sync --from-code   # fills the code-owned fields into the files; review them as a change to the map
```

## What an upload proposes

An upload never changes an approved version: it becomes a **draft proposal** of what the code owns, and the check runs with it.

- **A use case whose case is on the map:** a change to its `data` and `allowed` when they differ from the code. Table names compare by singular and plural, as the check does.
- **A use case with no case:** a skeleton case, with `starts: in_app`, `status: building`, its outcomes and failures by name, its area (from the upload, from its source path `src/cases/<area>/…`, or the map's only area), ready for someone to write its title, goal and steps.
- **A case with no code** is never retired by an upload.
- **The files of the format** (`{ files }`) can be uploaded too: each case that differs from the approved version becomes a change.

The upload's code sources are kept, so «where is it?» answers with the files behind each place. A draft becomes open when someone asks for reviews ([Versions and reviews](https://cases.docs.sypnia.com/contract/versions-and-reviews)).

Without Sypnorm, the code side is thinner: `Case: <id>` trailers in commits and `// case: <id>` notes in the sources say _which_ cases have code, not what they do ([The check](https://cases.docs.sypnia.com/contract/the-check)).
