> ## Documentation Index
> Fetch the complete documentation index at: https://docs.bijection.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Register, promote and serve models

> Keep model versions as Datasets artifacts, gate alias promotion on checks and serve predictions through a receiver.

The ML library, `@bijectionhq/library-ml`, is a model registry and model
serving for the app that places it: models, versions, aliases with their
promotion history, predictions and their observed values. Everything is a
fact of your app. `ml({...})` returns a subtree whose functions take their
paths where you place it (`models.aliases.promote`).

Four facts carry the design:

* **A version is a Datasets artifact.** A model's files are one published
  version of an artifact dataset held by your Datasets app
  (`{ dataset, number, snapshot }`). Training and capture run on your
  compute; the registry stores no file and runs no training.
* **An assessment is a check.** Evaluations are verdicts of your
  `checks({ label, writers })` subtree on `(subject, digest, kind)`: subject
  `<subject>/<model>`, digest the version's exact artifact. A pass on one
  version is never read as a pass on another.
* **Promotion is a gated pointer move.** Each alias names the check kinds
  it requires. `aliases.promote` moves the alias only if every kind's
  newest verdict on exactly that digest passed, in the same commit.
* **Serving is a receiver.** `serving.predict` records a pending prediction
  and submits one task to the serving endpoint; the continuation records
  the answer or the refusal.

```ts bijection/models.ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { label, role, secret, t } from "@bijectionhq/bijection";
import { checks } from "@bijectionhq/library-checks";
import { ml } from "@bijectionhq/library-ml";
import { datasets } from "./datasets";   // the installed Datasets app

export const lab = label();
export const operator = role({ clear: [lab] });
export const evaluator = role({ clear: [lab] });
export const servingToken = secret();    // bijection secret set servingToken

export const assessments = checks({ writers: [evaluator], label: lab });
export const models = ml({
  subject: "models",
  label: lab,
  operators: [operator],
  checks: assessments,
  aliases: { staging: { requires: [] }, production: { requires: ["accuracy", "bias"] } },
  datasets: { holds: datasets.holds, holdDays: 30 },
  serving: {
    endpoint: "https://serving.example.com",
    bearer: servingToken,
    input: { features: t.array(t.number) },
    output: { score: t.number },
  },
});

// app({ lab, operator, evaluator, servingToken, assessments, models, datasets, ... })
```

## Checks

`checks({ writers, label })` from `@bijectionhq/library-checks` is a
subtree of verdicts. A writer records one with
`assessments.record(ctx, { subject, digest, kind, verdict, detail })`, and
any function asks `assessments.passed(ctx, subject, digest, kinds)`. A
verdict is pinned to its digest. Scores, thresholds and report views go in
`detail`. Your evaluation workers, or the [evaluation
library](https://docs.bijection.com/models/evaluation.md), record the verdicts an alias requires.

## Register and promote

| Function | Who | What it does |
| - | - | - |
| `models.create`, `get`, `list` | operators, viewers | A model, with its subject and alias pointers |
| `versions.register({ model, artifact, metadata })` | operators | A version whose files are an artifact; the same artifact answers the same version |
| `versions.retire`, `get`, `list` | operators, viewers | Retire a version (refused while an alias points at it); read versions (a model's list pages in fact-id order, each version with its number) |
| `aliases.promote({ model, alias, number, expected, reason })` | operators | Move an alias to a version, or to `null`, gated on its required checks |
| `aliases.resolve`, `history` | viewers | The live version an alias points at; its moves, newest first |

`expected` makes a promotion compare-and-set against the alias's revision,
and every move is a new promotion fact. They refuse with the codes in
`ML_REFUSES`, such as `ChecksFailing` and `AliasMoved`. For an independent
review, ask the Approvals app and promote from its continuation. The
subtree also carries in-transaction helpers: `register`, `promote`,
`resolve`, `version`, `alias`, `prediction`, `predict` and
`subjectOf(model)`.

With `datasets: { holds }`, the first alias that points at a version holds
its artifact in the Datasets app, so retention never retires a served
model's files. When no alias points at it, the hold ends. A hold
that ends `redacted`, because a legal erasure won over it, marks the
version `redacted` and clears its aliases.

## Serve

With `serving`, `serving.predict({ model, key, alias, number, input })`
requests a prediction by alias or version number (the other `null`) under
your `key`; the same key answers the existing prediction. The task's body
names the version and its artifact, and the endpoint loads it.
`serving.observe({ model, key, value })` records what was later observed,
and `serving.prediction` reads both. Serving infrastructure stays yours:
the endpoint is a receiver with `bearer`, `input` and `output` types and a
`timeout`.

## Managed serving endpoints

Install [External model serving](https://github.com/bijectionhq/bijection/tree/main/npm-packages/libraries/external-model-serving)
with the [ML registry](https://github.com/bijectionhq/bijection/tree/main/npm-packages/libraries/ml)
and a [Compute worker](https://github.com/bijectionhq/bijection/tree/main/npm-packages/libraries/pack-compute).
A target names a model and a promoted alias. Its installation-owned binding maps
each deployable candidate digest to an exact SageMaker, Vertex AI, or Databricks
model artifact and a synthetic probe. The worker uses provider credentials
in its own environment.

Call `deploy({ target, expectedRevision, request })` to create a dedicated
endpoint. The request records the original alias revision and candidate
digest. Read `status({ id })` or `current({ target })` for the latest command,
observation, and retirement evidence. The controller starts a separate
readiness observation after creation. `isReady` requires the bound provider
version to receive all traffic, a live probe that reports the candidate digest
it loaded, and a still-current promotion. The result expires within five
minutes of observation. Call `refresh({ id })` for fresh evidence.

`resume({ id })` reconciles the same endpoint after a lost create reply while
the pinned promotion is current. `retire({ id })` disables readiness and
deletes the owned endpoint. The target becomes available for a new deployment
when the provider confirms that the owned resources are gone. Retrying
`retire` reconciles a lost delete reply. An absent endpoint after an
unacknowledged create leaves the request active until its outcome can be
established.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.