# Checks

> Run checks, read their findings, thresholds and reach

`client.checks` runs the checks the dataset page runs and reads what they
found. A check never keeps, rejects or deletes an episode: its findings are
issues, which hold the episode out of kept until a person keeps or rejects it,
and notes, which hold nothing. A check's `blocks` says which its failures
raise.

The default check is the VLM check (`vlm`): a model reads each episode against
the Task it is linked to. It costs credits; `estimate` shows the price and
starts nothing. The catalog's checks (`heuristic.jerk`, `task.graph`, …) run as
one selection of one `kind` with `check_ids`, `categories` or `all`.

Checks speak qualia.v1, in the API key's own account. Run and findings
methods take a dataset id or its full name
(`accounts/{account}/datasets/{dataset}`). Automatic policy methods also
accept an organization or workspace id/name.

## Automatic checks on new uploads

The free (heuristic) checks run on every upload, in every environment. They are
not a setting: no level switches them off. Paid video (graph) checks are **on**
by default and use credits; an organization, workspace or dataset can turn them
off. At each level, the graph setting can inherit or be explicitly enabled or
disabled. It resolves **dataset → workspace → organization → platform**. The
returned `graph_source` names the level supplying its effective value; `graph`
is the override at the requested level (`SETTING_UNSPECIFIED` means inherit).
`heuristic_enabled` always reads true, from `platform`. No policy rows are
created for existing organizations, workspaces or datasets at deployment.

```python
# Inspect the effective value and where it came from.
org = client.checks.automatic_policy("<ORG_UUID>", org=True)
workspace = client.checks.automatic_policy("<WORKSPACE_UUID>", workspace=True)
dataset = client.checks.automatic_policy("<DATASET_UUID>")
print(dataset.graph_enabled, dataset.graph_source)

# An organization's Admin or Owner may turn paid video checks off.
client.checks.set_automatic_policy("<ORG_UUID>", org=True, graph=False)
# A workspace Admin or Owner may override that for the workspace.
client.checks.set_automatic_policy("<WORKSPACE_UUID>", workspace=True, graph=True)
# A dataset writer may inherit that workspace value again.
client.checks.set_automatic_policy("<DATASET_UUID>", reset=("graph",))
```

`set_automatic_policy(parent, *, org=False, workspace=False, graph=None,
reset=())` changes the graph setting: `True` is on, `False` off;
`reset=("graph",)` removes the explicit override. `heuristic` is deprecated:
`heuristic=False` raises `ValueError`, and the server refuses an update that
sets the free checks off ("free checks run on every upload"). Never pass
`org=True` and `workspace=True` together. A switch starts **no** checks and
never backfills old episodes: **Applies to new uploads. Run checks to check
existing episodes.** Switching graph off also stops paid checks not yet started.
A person's `run` remains available when automatic checks
are off. Use `estimate` before a manual paid run to see its credit bound.

While a new take's free checks run, it has a blocking issue, code
`checks_pending` ("Free checks are running"). Only the system writes it: it
cannot be dismissed, converted or reopened, and keep and reset leave it alone.
It clears by itself once the checks have run. A keep made meanwhile is
recorded; the take reads kept once the checks clear and nothing else blocks.
If the checks cannot run after the last retry, a blocking issue
`free_checks_failed` (`Free checks could not run: <reason>. Run them again, or
keep the take.`) replaces it; a keep dismisses that one.

The public `Checks` API provides GET and masked PATCH on the singleton
`checkPolicies/automatic` under each parent:

| Parent       | GET/PATCH resource name                                                 |
| ------------ | ----------------------------------------------------------------------- |
| Organization | `/v1/accounts/{account}/checkPolicies/automatic`                        |
| Workspace    | `/v1/accounts/{account}/workspaces/{workspace}/checkPolicies/automatic` |
| Dataset      | `/v1/accounts/{account}/datasets/{dataset}/checkPolicies/automatic`     |

`GetAutomaticCheckPolicy` returns effective `graphEnabled`, `graphSource` and
the requested level's `graph` override. Its free-check fields
(`heuristicEnabled`, `heuristicSource`, `heuristic`) are deprecated:
`heuristicEnabled` is always true, from `platform`. `UpdateAutomaticCheckPolicy`
PATCH takes the policy body and `updateMask=graph`, set to `ENABLED`,
`DISABLED` or `SETTING_UNSPECIFIED` (inherit). An update that sets `heuristic`
to `DISABLED` is refused: "free checks run on every upload". A dataset read/write takes
`data:read` / `data:write`; changing a workspace or organization default takes
an Admin or Owner at that level and a key with opt-in `members:write` scope.
A key bound to a workspace cannot change the organization's default.

## The VLM check

```python
from qualia import Qualia

client = Qualia()

estimate = client.checks.estimate(dataset_id, max_episodes=50)
print(estimate.credits, estimate.episode_count, estimate.models)

started = client.checks.run(dataset_id, max_episodes=50, max_cost=estimate.credits)
run = client.checks.wait(started.check_runs[0])   # SUCCEEDED or FAILED
for issue in client.checks.findings(dataset_id).items:
    # An episode the run could not read has no reading: code "not_checked".
    verdict = issue.finding.verdict if issue.finding else issue.code
    print(issue.episode, verdict, issue.text)
```

For both VLM and heuristic checks, `checks.run(...)` returns a
`CheckRunResult` with a `check_runs` list of run names. A heuristic selection
starts one run. `check_run` is an alias for `check_runs[0]` (or `""` when
there are no runs).

`client.checks.get_run(started.check_runs[-1]).phase` shows `READING`,
`PUBLISHING` or `DONE`.
Findings already stand during `PUBLISHING`; proposed labels can wait behind
preparation or another background publisher in the account. A run without
proposals skips this phase. `DONE` includes failures: read `state` for the
result. Queued runs and older servers report `PHASE_UNSPECIFIED`.

While another VLM run is on the dataset (the automatic one after preparation,
or anyone's), `run` queues the new run behind it instead of
starting it: `started.queued_behind` names the run ahead and
`started.queued_reason` says so in a sentence. The queued run starts when that
one ends and reserves credits only then; `wait` covers the wait. `estimate`
answers either way.

A run that could not read some episodes (no published video, a refused call,
an invalid answer) still ends `SUCCEEDED`. It says so, and names each episode
with why:

```python
if run.vlm.not_checked_summary:          # "completed, 4 of 30 episodes not checked"
    print(run.vlm.not_checked_summary)
    for take in run.vlm.unread_episodes:  # the first 100
        print(take.episode, take.reason)
```

Each of those episodes also has an open issue, code `not_checked`, so it is no
longer kept. The next run that reads the episode closes it.

`estimate.models` lists the Qualia model tiers the check offers, the default
first. The VLM check reads each episode's task graph on `qualia-vlm` (the
strongest) only, so it lists that one. Another tier passed as `model=` is
refused (400) before anything starts.

`check="reach"` runs the reach solver instead; `check="-"` runs the dataset's
default set (`episode-rules` on an upload, `mixture` on a mixture).

Is the VLM check set up on this deployment?

```python
client.checks.get("vlm").runnable
```

## Checks from the catalog

```python
page = client.checks.list(dataset_id)
for check in page.items:
    print(check.check_id, check.kind, check.runnable, check.unavailable_reason)
print(page.free_checks_pending)  # True until the free checks are done here

bound = client.checks.estimate(dataset_id, kind="heuristic", categories=["heuristic"])
started = client.checks.run(dataset_id, kind="heuristic", all=True)
run = client.checks.wait(started.check_runs[0])

page = client.checks.list_findings(dataset_id, verdict="FAIL")
for finding in page.items:
    print(finding.episode, finding.title, finding.severity)
```

Each catalog check has a kind. Heuristic checks read the episode's data with
no model and cost nothing. VLM checks have a model read it, one call per
episode for every selected VLM check, and cost credits. One run holds one
kind: `kind` (`"heuristic"` or `"vlm"`) pins the kind `categories` and `all`
take. Without it this deployment selects runnable heuristics; VLM catalog
checks are unavailable. It lists skipped checks and why. If an explicitly pinned kind takes nothing, the
`ValidationError` suggests the other runnable kind, if any.

Each check's `blocks` says whether a failed finding blocks the take. Of the
free checks, `heuristic.nan` blocks, and `heuristic.frozen` blocks only when a
joint is stuck: its finding names the joint, and a command that never moves is
a note. The others are notes
([the table](https://docs.qualia.dev/global/guides/data-collection.md#free-checks-on-every-upload)).
`get("heuristic.frozen")` reads one catalog check by its id. A finding's
`severity` is its stored issue's (`ISSUE` where it blocks, `NOTE` where it does
not), and `last_result` counts `issue_count` and `note_count` the same way. A
run of the free checks by hand can move kept takes out of kept.

A VLM run needs `max_cost` covering the bound `estimate` gives for the same
selection; a heuristic run's bound is 0, so it needs none. `run` sends a
`request_id`, so a retry with the same id returns the first run.

In the CLI, [`qualia dataset checks run --json`](https://docs.qualia.dev/cli/commands.md#qualia-dataset-checks)
prints this result: `checkRuns`, the run names, and `checkRun`, the first of
them. With `--wait` it prints `{"checkRuns": [...]}`, each ended run as an
object, where it used to print one run object, so a script reads
`.checkRuns[0].state`, not `.state`.

`list_findings(dataset, check="-", *, check_run=, episode=, verdict=, all_revisions=)`
reads one check's findings (`"task.graph"`) or every check's: `"-"` lists VLM
and recording (heuristic) findings together, newest first. It returns the
findings that stand: per check and episode, the newest run's at the episode's
linked Task revision (its own link, else its dataset's), each with the
`task_revision` it read. Publishing a newer revision leaves them standing;
after `datasets.relink_task()`, an episode has none until a run reads the new
revision.
`all_revisions=True` returns every run's findings, superseded ones included.
A finding's `fallback_model` is true when the model's vendor answered with
another of its own models than the one the check asked for. The finding
stands; a different model read it. The issue it raised says the same in
`finding.fallback_model`.

A heuristic check lists only its failures: where it assessed an episode and
lists no finding, the episode passed. So `verdict="PASS"` returns none of its
passes, only issues a later run resolved. For an episode it could not assess,
a run writes no new finding, and a finding an earlier run wrote there stays
listed as that run left it. Each check's `last_result`, from `list(dataset)`,
has `assessed_count` of `selected_count` episodes assessed, the results that
passed, failed and were not assessed (`pass_count`, `fail_count`,
`unknown_count`) and why (`unassessed_reasons`).
`get_run(run).vlm.unread_episodes` names the episodes a run could not read;
nothing names those a check read and could not assess.

A VLM (graph) failure at 80% confidence or more against a published Task opens
a blocking issue on its episode. One below that bar, an uncertain reading, or
one read against a draft Task opens a note. Filter episodes by how many notes
they carry with `note_count`, as `open_issue_count` counts issues, and by their
codes with `note_codes`:

```python
client.episodes.list(dataset_id, filter="note_count > 0")
```

## Reference ranges

The episode rules judge each episode against its robot's reference ranges:
one per robot interface (or robot) and metric, with the fences an episode is
judged against.

```python
page = client.checks.reference_ranges(interface_hash="b3:…", metric="duration_seconds")
r = page.items[0]
print(r.metric, r.fence_low, r.fence_high, r.sample_count)
```

The heuristic check judges a merged dataset or a mixture against the ranges of
the interface its uploads share, and of their robot when they share one. While
its uploads do not share one interface (a merged dataset's are still settling
to one) it has no ranges, and the check reports jerk, noise and velocity as
unassessed.

`reference_range(name)` reads one.

## Reach

```python
status = client.checks.reach_status(dataset_id)
print(status.target_robot, status.current_key, status.stale)
if status.last_run:
    print(status.last_run.state, status.last_run.done, status.last_run.total)

client.checks.run(dataset_id, "reach")        # the solver; queued, not waited for
client.checks.cancel_reach(dataset_id)        # stop it; solved takes keep results

for result in client.checks.results(dataset_id, state="SUCCEEDED").items:
    print(result.episode, result.reach.longest_seconds, len(result.reach.stretches))
```

`results(dataset, check="reach", *, episode=, state=, robot=, filter=)` lists
each take on the dataset's target robot at its current reach key, `NOT_RUN`
where nothing is solved under it yet. Pass `None` for every stored result in
your workspace.

`status.calibration_mix` is set when the uploads a reach reads hold their poses
under more than one capture calibration (uploads recorded on differently
calibrated handheld rigs), or when the solver is refused for a mix of frames or
calibrations, and `None` otherwise. Its `calibrations` list each one with the
`calibration` itself (file name and date), the rig that recorded it (`robot`,
empty when none was recorded), whether it is that rig's `current` calibration
(`None` when that could not be read) and how many uploads hold it
(`upload_count`). `refused` is `True` when the solver will not run on the mix
(`current_key` is then empty) and `False` when it runs, each take on its own
upload's calibration. Uploads at the gripper root, and uploads that hold no
poses at all (never converted, or an arm recording), are one entry each, with
`frame` ROOT and UNSPECIFIED and no `calibration`.

## Methods

| Method                                                                                                             | Call                                              | Returns                                                                |
| ------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------- | ---------------------------------------------------------------------- |
| `automatic_policy(parent, *, org=False, workspace=False)`                                                          | `GetAutomaticCheckPolicy`                         | `AutomaticCheckPolicy`                                                 |
| `set_automatic_policy(parent, *, org=False, workspace=False, heuristic=None, graph=None, reset=())`                | `UpdateAutomaticCheckPolicy`                      | `AutomaticCheckPolicy`                                                 |
| `get(check="vlm")`                                                                                                 | `GetCheck`                                        | `Check`                                                                |
| `estimate(dataset, *, check_ids=, categories=, all=, kind=, episodes=, filter=, model=, max_episodes=, max_cost=)` | `RunCheck` or `RunDatasetChecks`, `validate_only` | `CheckEstimate`, or `RunDatasetChecksResponse` for a catalog selection |
| `run(dataset, check="vlm", *, …same…, request_id=)`                                                                | `RunCheck` or `RunDatasetChecks`                  | `CheckRunResult`                                                       |
| `get_run(run)`, `wait(run, *, timeout=, interval=)`                                                                | `GetCheckRun`                                     | `CheckRun`                                                             |
| `list_runs(check="-", *, dataset=, state=, filter=)`                                                               | `ListCheckRuns`                                   | page of `CheckRun`                                                     |
| `list(dataset)`                                                                                                    | `ListDatasetChecks`                               | page of `DatasetCheck`                                                 |
| `list_findings(dataset, check="-", *, check_run=, episode=, verdict=, all_revisions=)`                             | `ListCheckFindings`                               | page of `CheckFinding`                                                 |
| `findings(dataset, check="vlm", *, filter=)`                                                                       | `Issues.ListIssues`                               | page of `Issue`                                                        |
| `reference_ranges(check="episode-rules", *, interface_hash=, robot=, metric=, filter=)`                            | `ListReferenceRanges`                             | page of `ReferenceRange`                                               |
| `reference_range(name)`                                                                                            | `GetReferenceRange`                               | `ReferenceRange`                                                       |
| `reach_status(dataset)`                                                                                            | `GetReachStatus`                                  | `ReachStatus`                                                          |
| `cancel_reach(dataset)`                                                                                            | `CancelReachStatus`                               | `ReachStatus`                                                          |
| `results(dataset, check="reach", *, episode=, state=, robot=, filter=)`                                            | `ListCheckResults`                                | page of `CheckResult`                                                  |

Pages take `limit=` and `cursor=` and return `items` with
`page_info.end_cursor`. A key restricted to workspaces reaches none of these;
the CLI is [`qualia check`](https://docs.qualia.dev/cli/commands.md#qualia-check).
