# How a job runs

A job's status follows directly from what has happened to it: `queued`, `running`, `completed` or `canceled`.

## Job lifecycle

- `queued`: the API accepted the request and held its maximum credits, and the lab has not sent a record for it yet.
- `running`: the lab has started sending records for the job, or reopened a completed job to correct or add data.
- `completed`: the job has a package ready at `GET /v1/jobs/{id}/package`.
- `canceled`: you canceled the job before the lab started it.

You can cancel a job only while it is `queued`. Canceling an already canceled job changes nothing, and canceling a job that has started returns `409`.

```bash
$ curl -X POST https://api.sequencylabs.com/v1/jobs/job_01h455vb4pex5vsknk084sn02q/cancel \
  -H "Authorization: Bearer $SEQUENCY_API_KEY"
```

```python
import os
import httpx

response = httpx.post(
    "https://api.sequencylabs.com/v1/jobs/job_01h455vb4pex5vsknk084sn02q/cancel",
    headers={"Authorization": f"Bearer {os.environ['SEQUENCY_API_KEY']}"},
)
response.raise_for_status()
data = response.json()
```

```javascript
const response = await fetch("https://api.sequencylabs.com/v1/jobs/job_01h455vb4pex5vsknk084sn02q/cancel", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.SEQUENCY_API_KEY}`,
  },
});
const data = await response.json();
```

## Four kinds of work

`POST /v1/jobs` takes one `JobRequest` whose `work.kind` picks what the lab does:

- `route_review`: chemists review a route to `target_smiles` and advise what they would change. The package carries `review` and no rewards or files.
- `reaction_batch`: the lab runs each reaction and returns its measured yield.
- `reaction_screen`: the lab runs each reaction as one well of a plate and returns conversion or yield.
- `synthesis`: the lab makes `amount_mg` of `target_smiles` at `purity_pct`, following `route` if given or choosing a route if omitted, up to `max_credits`.

Set `exclusive` to `true` when only your organization may ever see the job's results. Exclusivity is fixed when the job is created and covers everything the lab runs for the job.

## Reuse

Set `reuse` to `true` to accept results the lab already measured for an exact matching reaction from an earlier job your organization may see. Reused reactions cost the lower `reused_reaction` rate, and the lab runs only the rest.

The `reused` array on a `Job` lists the IDs of reactions served from earlier work, or `target` when a synthesis is served whole. If every reaction in a job is served by reuse, the job completes at once without waiting for the lab.

## Packages and reopening

When a job completes, `GET /v1/jobs/{id}/package` returns version 1 with three parts:

- `files`: release-format files you download from `GET /v1/jobs/{id}/package/files/{path}` (a folder per product holding `<product ID>.json`, experiment records under `experiments/`, raw instrument files under `raw/` and `provenance.jsonl` at the top).
- `rewards`: measured values (`yield_pct`, `conversion_pct` or `purity_pct`), the method used and the source experiment, file, chemist pseudonym, site and UTC time.
- `review`: the chemist's confidence and notes for a `route_review` job, or `null` for other kinds.

If the lab finishes a job too early or finds a mistake, it can reopen the job. The job returns to `running` while its previous package stays readable. When the lab finishes again, `package_version` increases and the new package states the lab's `reason`. Pass `?version=1` to read an earlier version. Reopening does not change what the job costs.
