> ## 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.

# Run and cancel

> Separate admission, observation, terminal settlement and cancellation.

## Admit, observe and settle

```ts theme={"system"}
import { Nika, isNikaRunSucceeded } from '@supernovae-st/nika'

const nika = new Nika({ cwd: process.cwd() })
const run = await nika.run('workflows/release.nika', {
  maxCostUsd: 0.25,
})

const watching = (async () => {
  for await (const event of run.events()) {
    timeline.accept(event)
  }
})()

const result = await run.result()
await watching

if (!isNikaRunSucceeded(result)) {
  // 'failed' and 'paused' both land here — a pause carries no success exit
  process.exit(result.exitCode || 1)
}
```

`run()` admits the workflow first: a file that fails admission throws
`NikaOperationError` and no handle is created. The returned `NikaRun` carries
`id` plus its lifecycle methods; the run itself is not an async iterator.
Subscribe through `run.events()`; the run's ending lives in `run.result()`.
The result's [settlement](https://docs.nika.sh/sdk/runtime/results.md) carries the engine's cause,
task tally, spend, elapsed time and error.

A `paused` result means a human gate (`nika:prompt` with no default) holds the
run: it is neither a failure nor a success, and the result's `exitCode` is 4.
Resume a paused local run from the command line, with the trace its receipt
names and the answer:

```sh theme={"system"}
./node_modules/.bin/nika run workflows/release.nika \
  --resume .nika/traces/<run>.ndjson --answer approve=true
```

The trace path is `result.receipt.trace_path`. Give a gate a `default:` (used
when no human can answer) for a run that must never wait.

## Stop observation without cancelling

```ts theme={"system"}
const observer = new AbortController()

for await (const event of run.events({
  signal: observer.signal,
  bufferSize: 64,
})) {
  timeline.accept(event)
}
```

Aborting the observer ends only that view: the loop exits without an error,
and the run keeps running. It never cancels engine work.

## Cancel explicitly

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

console.log(cancellation.accepted, cancellation.status, result.status)
// true cancellation_requested cancelled
```

`run.cancel()` is idempotent: every call returns the same request. Locally the
SDK sends the engine `SIGTERM`. The task already running (a model call, a
`nika:wait`) finishes and is counted, tasks not yet started are cancelled, and
`run.result()` then resolves `cancelled` with `exitCode` 130 and
`settlement.cause` `operator`. That can take as long as the task in flight:
cancelling a run inside a 20-second `nika:wait` settled after the wait ended.

> [accordion] Try it: cancel a run that waits
>
> Save `slow.nika`, then start it and cancel it after a second and a half:
>
> ```yaml slow.nika theme={"system"}
> nika: slow
> model: mock/echo
> permits:
>   tools: ["nika:wait"]
> tasks:
>   hold:
>     invoke:
>       tool: nika:wait
>       args:
>         duration: "20s"
>   after_hold:
>     after: { hold: success }
>     infer:
>       prompt: "done waiting"
>       max_tokens: 16
> ```
>
> ```ts cancel.mts theme={"system"}
> import { Nika } from '@supernovae-st/nika'
>
> const nika = new Nika({ cwd: process.cwd() })
> const run = await nika.run('slow.nika')
> await new Promise((resolve) => setTimeout(resolve, 1500))
>
> const cancellation = await run.cancel()
> const result = await run.result()
> console.log(cancellation.status, result.status, result.exitCode, result.settlement?.cause)
> // cancellation_requested cancelled 130 operator
> ```

`cancellation_requested` acknowledges the request, not its outcome: keep
awaiting `run.result()`, which may still read `succeeded` or `failed` if the
run ended first. On [`nika serve`](https://docs.nika.sh/sdk/remote/jobs#read-status-or-cancel) an
execution that has not settled within the server's 5-second grace ends
`interrupted` instead. Only the deprecated `nika.cancel(run)` wrapper can
throw `NikaRunOwnershipError`, for a run another client created; the handle's
own methods are bound to their run.

> [accordion] Which published source this page describes
>
> > [!TIP]
> >
> > **One client, local by default.** Import `Nika` from
> > the package root and omit `url`. The client takes the engine
> > from `bin`, then `NIKA_BIN` (both absolute paths), then
> > the platform package npm installed with it; it never uses a
> > `nika` found on your `PATH`. These examples target
> > the published [0.124.4 SDK release](https://github.com/supernovae-st/nika-client/tree/v0.124.4).

> [card] [Events](https://docs.nika.sh/sdk/runtime/events.md)
>
> Design an additive event reducer.

> [card] [Receipts](https://docs.nika.sh/sdk/runtime/receipts.md)
>
> Preserve and verify engine-issued proof.


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