# Custom pipelines

> Keep your own pipeline templates as files, apply them to a workspace, and launch them with a cost ceiling

A custom pipeline is a template your workspace owns. You write it as a TOML
file, keep it in your own repository, and apply it. An apply that changes its
nodes, edges or outputs makes the template's next **immutable version**; one
that changes only the `description` updates it in place, under the same
version. A run keeps the version it started from, so an edit never changes a
run that already started or finished.

In this first version a template composes the platform's public templates,
each pinned at a version, with your own fields fixed or left to whoever
launches it. Its billing is the run's: you launch with a `max_cost`, and the
run reserves no more than that.

## Who can do what

| Action                                           | Role in the workspace | API key scope     |
| ------------------------------------------------ | --------------------- | ----------------- |
| See the catalogue, validate a file, read history | any role              | `jobs:read`       |
| Apply or retire a template                       | Contributor and up    | `pipelines:write` |
| Launch a run                                     | Contributor and up    | `jobs:write`      |

Only your workspace sees your templates: another workspace can neither list
nor pin them.

## What you can pin

```sh
qualia pipeline catalog
```

| Template            | Inputs                                                                                       | Output  |
| ------------------- | -------------------------------------------------------------------------------------------- | ------- |
| `vla-finetune-act`  | `dataset`, `training_hours`, `batch_size`, `model_name`, `camera_mappings`, `vla_hyper_spec` | `model` |
| `vla-finetune-pi05` | the same, plus `base_model`                                                                  | `model` |

These ports are stable: they may gain inputs but never rename or drop one.
The catalogue also lists your workspace's own templates, each with its latest
version. A template nests one level, so one that pins others itself (every
template you apply here) is listed as not pinnable.

## The file

```toml
name = "our-act"                      # lowercase, digits and dashes
description = "ACT on our pick-and-place rig"

[[nodes]]
id = "train"
template = "vla-finetune-act@2"       # <name>@<version>, from the catalogue
exposed = ["dataset", "camera_mappings", "batch_size"]   # left to whoever launches it

[nodes.consts]                        # fixed: a launch cannot change them
training_hours = 2
model_name = "our-act"

[nodes.defaults]                      # suggested: a launch may change exposed ones
batch_size = 16

[[outputs]]                           # what a run of it answers with
name = "model"
from = "train.model"
```

- Each node pins a template with `template = "<name>@<version>"`. A later
  version of that template never reaches yours until you change the pin.
- `edges` wire one node's output into another node's input:
  `[[edges]]` with `from = "train.model"` and `to = "<node>.<input>"`.
- `name` may not be one of the public templates above: a pin names its
  template, so each name means one template.
- Unknown keys are refused rather than ignored. A file holds at most 16 nodes
  and 64 KiB.

## Validate, apply, launch

```sh
qualia pipeline template validate -f our-act.toml
qualia pipeline template apply -f our-act.toml
qualia pipeline estimate our-act --workspace <WORKSPACE_ID> --set dataset=<VERSION_ID>
qualia pipeline launch our-act --workspace <WORKSPACE_ID> \
  --set dataset=<VERSION_ID> --set camera_mappings='{"cam_1": "observation.images.top"}' \
  --max-cost 400 --quote-digest <FROM_ESTIMATE>
```

`validate` lists every problem and writes nothing. `apply` answers
`our-act@3 applied` when it wrote something (a new version, or a new
description under the same one) and `our-act@3 unchanged` when it did not, so
you can run it on every push. `history` lists the versions; each shows the
template's current description. A launch over its `max_cost` is
refused with the bound it needs; `estimate` gives that bound first.
Your templates are billed per run. On a deployment that does not bill per
run, `apply` still succeeds but says `Not launchable here` with the reason
(`launchable: false` and a `launch_refusal` under `--json`), and a launch is
refused as `run_billing_required` rather than run unbilled. Where your
organisation is billed by contract (no Billing in Settings), credits are
unlimited: the same `estimate` and `launch --max-cost` still hold the run to
its ceiling, and a launch without `--max-cost` runs with none.

The SDK has the same calls:

```python
from pathlib import Path

from qualia import Qualia

client = Qualia()
applied = client.pipelines.apply(Path("our-act.toml").read_text(encoding="utf-8"))
bound = client.pipelines.estimate(
    applied.pipeline_template_id,
    workspace_id="<WORKSPACE_ID>",
    values={"dataset": "<VERSION_ID>"},
)
run = client.pipelines.launch(
    applied.pipeline_template_id,
    workspace_id="<WORKSPACE_ID>",
    values={"dataset": "<VERSION_ID>"},
    max_cost=bound.credits,
    quote_digest=bound.quote_digest,
)
```

## History and retiring

```sh
qualia pipeline template history our-act
qualia pipeline template history our-act --version 1
qualia pipeline template retire our-act
```

A retired template starts no new run and cannot be pinned again; its runs and
history stay. Its name cannot be applied again, so apply the next file under a
new name.
