# How it works

A comp file passes through four stages to become a picture: script execution, IR, evaluation and output. The figure is at the end of the chapter.

## From script to IR

A `*.comp.ts` file runs in an embedded JavaScript engine (QuickJS). TypeScript types are only stripped before execution; type checking is the job of the editor and `lamino.d.ts`.

What the script does is not make a picture but assemble a graph of nodes. A call such as `read()` or `.blur()` only adds one node and does not touch any pixels. The result of the run is written to `.lamino/ir/` as the **IR** (intermediate representation, JSON). The IR lists each node's `id`, type, parameters and input connections.

This runtime is restricted. There are no general APIs for files, the network or processes, and `Date.now()` and `Math.random()` are not available. Only asset functions such as `json()`, `table()`, `readText()`, `values()` and `camera()` can read files, and every file read leaves its hash in the lock. As a result, the same comp file and the same lock give a byte-identical IR.

Animation is also settled at this stage. `keys()` goes into the IR as a keyframe table, and `t((f) => ...)` goes into the IR as a list of values obtained by calling the function over the comp's frame range. No JavaScript runs during evaluation.

## The node contract

Every node has the same shape. It is a pair of a **declaration** (input and output ports, parameter types and ranges, the padding of the region it reads, references to neighbouring frames, optimisation hints, the implementation version) and **kernels** (the CPU reference implementation and WGSL for the GPU). The scheduler reads only the declaration and never looks inside the kernels.

`merge`, `glow`, `read` and a GLSL node written by the project are all treated the same by the engine. A GLSL node writes the same declaration with `#pragma lamino ...` lines in its source.

## Evaluation

The engine walks the graph backwards from the requested node and frame and computes only what is needed.

- **Only the needed region**: when only part of the image is requested, as with `preview --region`, each node requests from its inputs the region widened by its declared padding (the ROI).
- **Proxy**: evaluation runs at one of 1/1, 1/2, 1/4 or 1/8 resolution. Parameters in width units scale automatically.
- **Fusion**: when per-pixel independent nodes (those declared `pointwise`, such as grades and colour conversions) follow one another, the GPU runs them together in one pass.
- **Cache**: node outputs are stored in two tiers, RAM and disk (`.lamino/cache/`). The key is built from the node type, the implementation version, the resolved parameters, the input keys, the frame, the proxy factor and the asset hashes. Changing one parameter recomputes only that node and what is downstream of it.
- **Time-invariant nodes**: a node that does not change with the frame, such as reading a still image, shares one cache entry across all frames.

Inside the engine, an image is always scene-linear, premultiplied, 32-bit float RGBA. `read` converts from the input colour space to the working space (ACEScg by default), and the display transform (display / view) is applied only at the exits: the preview, the review window and the stream. It is not applied to EXR output.

## CPU and GPU

Where the kernels run (the backend) is chosen with `--backend`.

| Backend | What it is |
| --- | --- |
| `auto` (default) | Tries the hardware GPU first, and uses the CPU reference implementation if there is none |
| `gpu` | The GPU through wgpu (DX12 on Windows) |
| `gpu-software` | A software GPU adapter (WARP). For when you do not want to use a shared GPU, or for verification |
| `cpu` | The CPU reference implementation. Only shader nodes run on the software adapter |

The CPU implementation is the reference, and tests check that the GPU results fall within a tolerance of it (relative 1e-5 or absolute 1e-6 for per-pixel independent nodes, absolute 1e-4 for the rest). Setting the environment variable `LAMINO_GPU=software` makes `auto` avoid the hardware GPU.

## Watching and the lock

`lamino dev` watches the comp file and the assets. When it detects a save, it runs `check` (IR generation) and `lint` and streams the results as JSON lines. The review window runs the same watcher inside itself, so the picture updates on every save.

When a new asset is used, the per-frame content hashes (xxh3) are computed in the background and appended to `lamino.lock`. Until that finishes, lint returns `L_LOCK_PENDING`, but the work loop does not stop. After that, the lock is checked against the files by size and modification time, and a swapped file is reported with `L_LOCK_STALE`.

## How the processes divide

`lamino` is a single executable, and depending on how it is used it becomes one of three kinds of process.

- **CLI**: one command returns one JSON result and exits.
- **MCP server** (`lamino mcp`): stays resident on standard input and output and offers the same operations as the CLI as tools. Long renders run in a child process, so the server survives a render that crashes.
- **Review window** (`lamino review`): a separate process with its own engine. Its state is held only in memory, and `lamino viewer` reads it over 127.0.0.1.

Every process uses the same engine code. The CLI subcommands are the source of truth for the operations, and the MCP tools are a thin wrapper around them.

![The flow from comp file to picture. The four stages, and the two paths back to the agent](./figures/pipeline.svg)

There are two paths back to the agent. One is reading the numbers and images the engine returns. The other is receiving instructions in words from the human who watched the review window. On either path, only the agent changes the comp file.
