# Digital Twin

A Digital Twin is a 3D view of a TilliT site: its lines and machines laid out on
a floor, each machine bound to its TilliT asset by name and drawn with a model,
with the flows between them, the products they carry, zones, walkways and
cameras. The tenant's View app renders each twin the tenant has published.

A twin is one JSON definition per twin, stored per tenant by the DigitalTwin API
(simstack). It is not schema-driven config, so `import`/`diff`/`deploy` don't
manage it. Use `tllt twin`.

## Models

A twin draws each asset with a model the tenant can already use: the shared
library (`library/{name}@{version}`) or a committed tenant model
(`tenant/{name}@{version}`). `tllt twin models` lists them. To add one of the
tenant's own, build a GLB to the API's rules and upload it:
[`twin-models.md`](twin-models.md) has both, and needs nothing from simstack.

## The flow

```bash
tllt --json twin models                    # the refs a twin can use
tllt --json twin draft plant-1 --site "Plant 1" --config generate.yaml
#   -> {profile}/twin/plant-1/twin.yaml: edit positions, flows, what to leave out
tllt --json twin validate plant-1          # the publish checks; publishes nothing
tllt --json twin push plant-1 --yes        # in the tenant's View within a minute
```

To change a published twin: `pull`, edit, `validate`, `push`.

Snapshot layout, beside `do/` and `scheduler/`:

```
{profile}/twin/{twin}/twin.yaml     the definition (source of truth)
{profile}/twin/{twin}/_meta.json    {twin, site, etag} of the version last pulled or pushed
```

Twin files are YAML or JSON (JSON is YAML too), so `--file` also takes a twin
written in simstack's repository. `pull` and `draft` write YAML with vectors on
one line. The API keeps no comments: a pulled twin is the definition alone.

## Drafting

`draft` asks the API for a first draft of a TilliT site. The API reads the site's
active assets as you and writes nothing. `--config` takes simstack's
`generate.json` shape:

| Key | Meaning |
| --- | ------- |
| `classModels` | TilliT asset class name → model ref, e.g. `"Fillers": "library/monobloc@1"` |
| `products` | The twin's products block: product key → `{model, …}` |
| `site` | The TilliT site; `--site` overrides it |
| `lineSpacing` | Metres between line rows (default 8) |
| `gap` | Metres between machine footprints (default 5) |

Unknown keys are refused. The result lists `skipped` (assets whose class has no
model, lines with nothing modelled) and `problems` to fix before publishing.
`draft` won't replace an existing twin file without `--force`.

## What the API checks

`validate` and `push` run the same checks, as the signed-in user: the
definition's shape; that every model ref exists and its ports connect; that every
`tillit` name is an active asset of the twin's site; and, for warehouse twins,
the tenant's WMS locations. The API fills in each model's metadata, and the
site's timezone from TilliT: a site without one can't be published. `validate`
and `draft` work for any signed-in user; `push` needs the ADMIN role.

## Publishing safely

- `push` validates first. An invalid twin returns `code: "validation_failed"`
  with the API's `problems`; nothing is written and nothing is asked.
- Without `--yes`, `--json` mode returns `code: "confirmation_required"` with the
  plan: `twin`, `site`, `file`, and `action`, which is `replace` when a twin of
  that name is already published and `create` otherwise.
- `push` publishes over only the version last pulled or pushed from this
  snapshot: it sends `_meta.json`'s ETag as `If-Match`. If someone published in
  between, it returns `code: "changed_since_pull"` and writes nothing. Pull
  again and redo the edit, or pass `--force`. A file that was never pulled (a
  draft, a repository twin) goes without `If-Match`, so read the plan's `action`.
- The gateway serves a twin's ETag weak (`W/"…"`) when it compresses the
  response, and the API hands `If-Match` to S3, which compares strong tags.
  tllt keeps and sends the strong form; a raw `tllt api PUT` with `If-Match`
  has to strip the `W/` too.

## Failures

| Result | Meaning |
| ------ | ------- |
| `code: "unsupported"` | The environment's Digital Twin API has no such route (`404 no route …`). As of November 2026, production's API (simstack `main`) serves twins but has no validate, draft or publish route |
| `status: 403` | Publishing needs the ADMIN role |
| `status: 404`, `no twin …` | Nothing is published under that name |
| `status: 400` with `problems` | The API refused the twin: the same problems `validate` reports |
| `status: 409` | Another publish of this twin is under way; try again |

Twin names are lowercase letters, digits and dashes, starting with a letter, at
most 63 characters; tllt refuses any other before calling the API.

## Gotchas

- `tllt import` without `--entity` replaces the whole `{profile}/` folder,
  `twin/` and `wireflow/` included. Push or commit twin edits before re-importing.
- The API keeps each tenant's twins for a minute per warm function: a publish can
  take a minute to show everywhere, and a pull right after someone else's publish
  can still return the previous version.
- A published twin goes stale when the library retires a model version it names:
  the View keeps serving it, but it fails validation until the ref is updated.
  Validate a pulled twin before editing it, and check `models` for what exists.
- The simulator behind a demo twin is published separately (simstack's
  `npm run sim -- publish`, or the API's `PUT …/simulator/config`).
