# Pipelines

> Launch any pipeline template the launcher offers

`client.pipelines` is the dashboard's launcher: the templates you can launch,
each with its launch form, and a launch with flat values for that form. An
environment without the `pipelines` feature refuses these calls with
`AuthenticationError("Access denied: Training isn't available on your account's plan yet. We're working on it. To request access, write to support@qualiastudios.dev.")`.

## List templates

```python
from qualia import Qualia

client = Qualia()

for template in client.pipelines.templates():
    required = [f.name for f in template.launch_fields if f.required]
    print(template.pipeline_template_id, template.name, required)
```

Each `LaunchField` has a `name` (its key in `values`), a `type_name`, its JSON
`schema_` (`schema` on the wire), `required`, a `default`, and `resolve` when
its default is computed from other fields. `client.pipelines.field_types()`
returns the field-type registry: how each `type_name` renders and where its
values come from.

## Compute a field's default

A field with `resolve` names the fields its default is read from
(`field.resolve["inputs"]`). Send their current values:

```python
resolved = client.pipelines.resolve(
    template.pipeline_template_id, "batch_size", values={"model": model_ref}
)
print(resolved.default)
```

A value for a field the resolver does not read is refused (422).

## Launch

```python
run = client.pipelines.launch(
    template.pipeline_template_id,
    project_id="<PROJECT_UUID>",
    values={"dataset": "<DATASET_VERSION_UUID>", "hours": 1},
    name="first try",
)
for event in client.events.stream(job_id=run.job_id, project_id="<PROJECT_UUID>"):
    print(event.type, event.data.get("message", ""))
```

`values` holds one value per launch field, keyed by its name. A dataset field
takes a dataset version ID; a model field takes
`{"hub_id": ..., "revision": ...}`. A key that is not a launch field, or a
required field without a value, is refused (422).

A template billed per run needs a cost ceiling: `max_cost`, the most credits
the run may reserve. `estimate` gives its bound first, with a `quote_digest`
that makes the launch refuse (409) if the price moved. `idempotency_key`
makes a retried launch the same run.

```python
bound = client.pipelines.estimate(
    template.pipeline_template_id, project_id="<PROJECT_UUID>", values=values
)
run = client.pipelines.launch(
    template.pipeline_template_id,
    project_id="<PROJECT_UUID>",
    values=values,
    max_cost=bound.credits,
    quote_digest=bound.quote_digest,
)
```

## Your own templates

A workspace keeps its own templates as TOML files
([Custom pipelines](https://docs.qualia.dev/global/guides/custom-pipelines.md) has the format):

```python
from pathlib import Path

for entry in client.pipelines.catalog():  # what a template may pin
    print(f"{entry.name}@{entry.version}", entry.owner, [o.name for o in entry.outputs])

source = Path("our-act.toml").read_text()
checked = client.pipelines.validate(source)  # writes nothing
if not checked.valid:
    print("\n".join(checked.errors))
applied = client.pipelines.apply(source)  # new graph: next version; new description: same version
history = client.pipelines.versions(applied.pipeline_template_id)
first = client.pipelines.version(applied.pipeline_template_id, 1)
client.pipelines.retire(applied.pipeline_template_id)
```

`apply` makes the next version when the nodes, edges or outputs changed and
updates the description in place otherwise; `changed` is False only when
nothing was written. It raises `ValidationError` listing every problem in the file, one per
line, and a 409 for a retired name. Applying needs a Contributor role and a
key with `pipelines:write`.

## Follow a run

The run is running work like any other, listed under the project it was
launched into. Its status has kind `PIPELINE_RUN`, and its `operation` is the
run's `job_id`:
[`client.statuses.list(parent="<PROJECT_UUID>")`](https://docs.qualia.dev/sdk/resources/statuses.md)
shows its state, and `client.statuses.cancel(run.job_id)` stops it. Without
`parent`, the list is the workspace the client acts in, which may not hold the
run. Each op in the run is listed too, as `PIPELINE_OP`; an op cannot be
cancelled on its own.

`client.events.stream(job_id=...)` yields the run's live lines: log lines,
telemetry and capacity waits. It carries no state, so read that from
`client.statuses`. It has no replay: the server ends the stream now and then,
and lines sent while it was closed are not resent. Open it again to keep
following. A key restricted to one project must pass `project_id`.

The CLI has the same: `qualia pipeline templates`, `show` and `launch`, then
`qualia status list <PROJECT_ID> --running` and `qualia status cancel`.
