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

# Remote jobs

> Admit, persist, recover, observe and cancel resident HTTP jobs.

> [!NOTE]
>
> **The same client selects HTTP when `url` and
> `token` are present.** A served name such as
> `workflows/release.nika` (its path from the project root) goes
> directly to the server and needs no local engine or file. An explicit local
> path such as `./release.nika` needs a compatible local engine to
> capture an immutable snapshot. The released OpenAPI of
> `nika serve` 0.124.1 covers jobs, SSE events,
> cancellation, trace verification, the artifacts a settled job's sealed
> journal records (behind `run.artifacts()`), workflow listing,
> schedule read and write, and stateless authoring
> (`POST /v1/compile`, behind `compile()`). It has no
> `POST /v1/run`, job-list, or schedule list, delete, trigger
> or backfill route. The server owns its engine version; check its
> compatibility separately from the bundled local engine.

## Admit and persist identity

```ts theme={"system"}
// Mint the key once per logical job and persist it WITH the request —
// an uncertain response retries with the same key and the same request.
const idempotencyKey = crypto.randomUUID()
await database.runs.insert({ idempotencyKey, workflow: 'workflows/release.nika' })

const run = await nika.run('workflows/release.nika', { idempotencyKey })
await database.runs.update({ idempotencyKey }, { jobId: run.id, lastEventId: 0 })
```

The served name in this example submits
`{"workflow":"workflows/release.nika"}` to `POST /v1/jobs`; the server captures
its served workflow itself. A served name is the workflow's path from the
project root, not from `--workflows`. An explicit local path such as
`./release.nika` instead asks a compatible local engine for a snapshot and
sends those captured bytes; a snapshot freezes inputs, so `inputs` is refused
on that form.

Remote run options accept `idempotencyKey`, `access` (the job's access pin,
sent as `JobByName.access` beside a served name) and, where the server
advertises `jobInputs`, literal `inputs`. `vars`, `model` and `maxCostUsd` are
refused with `NikaCompatibilityError` (`capability: 'runOptions'`) because
their request envelopes do not exist. A snapshot of a local file carries no
pin either: `access` beside one is refused (`capability: 'snapshotAccess'`).
A fresh `randomUUID()` per retry defeats the contract: the key is what lets
the server return the original job instead of starting a second one. Keys
cover the whole `--state-root` and do not expire, so never reuse one for
another operation.

## Spend on the server

The server bounds spend itself: `nika serve --run-cost-ceiling <USD>` caps
every job your code submits, **1 USD by default** (`none` removes the cap).
A job whose cost floor exceeds it is admitted, then settles `failed` with
`error.code` `NIKA-1709`; `run()` does not throw. Bound a job through its
workflow (its model and `max_tokens`), and see [Spend caps](https://docs.nika.sh/sdk/runtime/spend.md).

## Observe and settle

```ts theme={"system"}
for await (const event of run.events()) {
  if (event.sequence !== undefined) {
    await saveCheckpoint(run.id, event.sequence)
  }
}

const result = await run.result()
```

The default `machineBufferBytes`, 16 MiB, is the bound of the server's own job
store, so every result the server settles reaches `run.result()`. A client
configured with a smaller bound is refused with a `NikaProtocolError` that
names the size to set; the job keeps its result, and `attachRun()` with a
larger bound reads it again.

## Recover after a process restart

```ts theme={"system"}
const recovered = await nika.attachRun(saved.jobId, {
  lastEventId: saved.lastEventId,
})

for await (const event of recovered.events()) {
  if (event.sequence !== undefined) {
    await saveCheckpoint(recovered.id, event.sequence)
  }
}

const result = await recovered.result()
```

Persist the job id and the last durably consumed sequence under one name,
`lastEventId`, and reuse it. Reattachment avoids resubmitting a workflow
merely because the Node process restarted. The engine's settlement remains
available on `result.settlement` even when the cursor already consumed its
terminal event. Only a server job id is durable: a local `run.id` is a
throwaway id, and `attachRun()` refuses it.

> [!WARNING]
>
> **A paused job stays paused.** In 0.124.1, `nika serve` has no route to
> answer a human gate: a job that reaches a `nika:prompt` without a
> `default:` settles `paused` (a `run.waiting` event), no request resumes it,
> and cancelling it answers `already_settled`. For served workflows, give
> every `nika:prompt` a `default:` (used when no human can answer), or keep
> human gates on the local engine, where the CLI resumes a paused run.

## Read status or cancel

```ts theme={"system"}
console.log(await run.status())
const cancellation = await run.cancel()
const result = await run.result()
```

A running job answers `202` with `{ accepted: true, status:
'cancellation_requested' }`: the server accepted the request and the run is
still settling. Await `run.result()` for its actual status. A success or
failure that wins the race is preserved; an execution that has not settled
within the server's 5-second grace ends `interrupted`, with no settlement. A
job cancelled before its execution started settles `cancelled` on the reply
itself (`200`), and a job that already ended answers `accepted: false`,
`status: 'already_settled'`. The API has no job-list route, so application
indexes remain application-owned.

## Download what it produced

Once the job has settled, `run.artifacts()` lists the files its sealed journal
records, and `run.readArtifact()` or `run.downloadArtifact()` returns their
bytes, proven against the recorded size and SHA-256. A handle from
`attachRun()` has the same methods, so a recovered job's files stay
reachable by its id:

```ts theme={"system"}
const recovered = await nika.attachRun(saved.jobId)
const { artifacts } = await recovered.artifacts()
```

[Outputs and artifacts](https://docs.nika.sh/sdk/remote/artifacts.md) covers what is listed, what a
download proves and its refusals.

> [card] [SSE streaming](https://docs.nika.sh/sdk/remote/streaming.md)
>
> Preserve sequence continuity across observers.

> [card] [Errors](https://docs.nika.sh/sdk/runtime/errors.md)
>
> Separate run failure from boundary failure.


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