# CLI

> Reference for the cairn command-line client, plus a summary of the cairn-ctl recovery tool.

Source: https://cairnflow.dev/docs/reference/cli

`cairn` is a thin client of the [HTTP API](/docs/reference/http-api): each command is one request
(or two, when it follows a `503` to the leader). It prints a readable table by default and the raw
JSON with `--json`.

## Global options

```sh
cairn [--server URL] [--token KEY] [--json] <command> ...
```

| Option | Environment | Default | Meaning |
|---|---|---|---|
| `--server URL` | `CAIRNFLOW_SERVER` | `http://127.0.0.1:8080` | Node or endpoint to talk to. |
| `--token KEY` | `CAIRNFLOW_TOKEN` | none | API key, sent as `Authorization: Bearer`. |
| `--json` | | off | Print the API's JSON instead of a table. |
| `--version` | | | Print the version line. |

```sh
export CAIRNFLOW_SERVER=https://cairn.example.com
export CAIRNFLOW_TOKEN=<your API key>
```

When a node answers `503` with a `leader`, `cairn` resends the request there once.

Exit codes: `0` success; `1` the request failed (the CLI prints the HTTP status and the error
message); `2` wrong usage (the CLI prints the usage text); `3` `replay --bundle` found a mismatch.

## Deployments and services

| Command | Does |
|---|---|
| `cairn register <uri>` | Register the service at `<uri>` (`POST /deployments`). Prints the deployment id and its services. |
| `cairn deployments` | List deployments and which services they are the current revision for. |
| `cairn deployments rm <id>` | Remove a deployment that is no longer a revision and has no bound invocations. |
| `cairn revision <service> <deployment-id> [--expect ID]` | Point a service at another deployment (rollback, pinning). With `--expect`, only if the current revision is `ID`. |
| `cairn services` | Services and handlers you can call. |

The URL you register must be reachable from the cairnflow nodes.

## Invoking

```sh
cairn invoke <service> <handler> [--key K] [--input DATA] [--scope S] [--limit-key L]
```

| Option | Meaning |
|---|---|
| `--key K` | Object key (virtual object) or workflow id. |
| `--input DATA` | Request body. `-` reads it from stdin. |
| `--scope S` | Run in a flow-control scope (`/scope/S/invoke/...`). |
| `--limit-key L` | Limit key inside the scope. Requires `--scope`. |

```sh
cairn invoke Greeter greet --input '"world"'
cairn invoke Cart add --key user-1 --input '{"sku":"tea"}'
echo '{"amount": 100}' | cairn invoke Orders submit --key order-42 --input -
```

The command waits like `POST /invoke`: it prints the result, or the invocation id when the result
is not ready within the node's wait time.

## Workflows

```sh
cairn workflow <service> <key> [--attach] [--scope S]
```

Prints the result of a workflow's run, found by its workflow id. Without `--attach` it does not wait
(`GET /workflow/.../output`); with it, it waits (`POST /workflow/.../attach`).

## Inspecting invocations

| Command | Does |
|---|---|
| `cairn status <id>` | State of one invocation, with its output or error. |
| `cairn journal <id>` | The journal, entry by entry. |
| `cairn invocations [--status S] [--limit N] [--all-nodes]` | Invocations of the ranges led by the node you ask; `--all-nodes` asks the whole cluster. |
| `cairn invocations --cursor '' [--tag k=v] [--class C] [--service S] [--root] [--limit N]` | Paged listing across the cluster, newest first. Any of these options selects it; pass the printed cursor to get the next page. |
| `cairn sql "<query>"` | Run a read-only SQL query (`POST /query`). |
| `cairn stats [--window 5m\|15m\|1h] [--tenant T] [--all-nodes]` | Per-handler summary: turns, outcomes, failed attempts, p50, p99, max. |

```sh
cairn invocations --status suspended
cairn sql "SELECT class, sum(count) FROM sys_invocation_counts GROUP BY class"
```

## Acting on invocations

| Command | Does |
|---|---|
| `cairn cancel <id>` | Hard cancel (`failed`, code 572). |
| `cairn purge <id>` | Delete a finished invocation now. |
| `cairn resume <id>` | Take an invocation out of `paused` and run it. |
| `cairn signal <id> <name\|number> [--value JSON]` | Send a signal to a waiting invocation. |

## Promises

| Command | Does |
|---|---|
| `cairn resolve <service> <key> <name> [--value JSON]` | Resolve a durable promise. |
| `cairn reject <service> <key> <name> <message>` | Reject it; waiters get a terminal error. |
| `cairn promise <service> <key> <name>` | Show whether the promise has a result. |

```sh
cairn signal <id> approve --value '{"by":"alice"}'
cairn resolve Orders order-42 approved --value '"ok"'
```

## API keys

| Command | Does |
|---|---|
| `cairn keys` | List stored keys (never secrets). |
| `cairn keys create <tenant> <role>` | Create a key. The secret is printed **once**. |
| `cairn keys rm <id>` | Revoke a key. Reports nodes that have not confirmed yet. |
| `cairn keys check <id>` | Whether the key is still accepted by any live node. |

Roles accepted by the API are `invoke`, `view`, `admin`, `provision` and `cluster-admin`. See
[Security](/docs/self-hosting/security#roles).

```sh
cairn keys create default invoke
```

## Flow control

| Command | Does |
|---|---|
| `cairn rules` | The rule book. |
| `cairn rules set <pattern> [--concurrency N\|unlimited] [--description TXT] [--disabled\|--enabled]` | Add or update a concurrency rule (merge). |
| `cairn rules rm <pattern>` | Remove a rule. |
| `cairn limits` | Usage, limit and availability of each active counter. |
| `cairn vqueues` | Queues that have work, and what blocks them. |
| `cairn scheduler` | Queue depth, slots in use, locks held, rule book version in effect. |

Rate limits (`rate`, `burst`) and per-tenant rows are set through `POST /rules`; see
[Flow control](/docs/reference/flow-control).

## Cluster

| Command | Does |
|---|---|
| `cairn cluster` | Who leads each range. |
| `cairn ranges` | The range table: version, key bounds, generation, pending split or merge. |
| `cairn ranges split <id> [--at HEX16]` | Split a range (default: the middle of its keys). |
| `cairn ranges merge <left> <right>` | Merge two adjacent ranges. |

## Capturing runs for debugging

| Command | Does |
|---|---|
| `cairn capture <service> <handler> [--tenant T] [--deployment D] [--count N] [--ttl SECONDS]` | Capture the next matching invocations (journal and final state) when they finish or pause. |
| `cairn capture ls [--tenant T]` | Pending capture requests and captured bundles. |
| `cairn capture get <tenant> <id> [--out FILE]` | Download a bundle (default `<id>.bundle`). |
| `cairn replay --bundle FILE [--endpoint URL]` | Replay a bundle against a service, read-only, without a cluster. `--endpoint` points at a local build you are fixing. Exits `3` on a mismatch. |

Capture commands need a `cluster-admin` key.

## cairn-ctl

`cairn-ctl` is a separate tool that works on the bucket directly, without a running node. It takes
the store like a node does (`--main-store URI` or `--data-dir DIR`, optional `--wal-store` and
`--prefix`) and the bucket credentials from the environment.

| Command | Does |
|---|---|
| `verify [--deep] [--wal-logs] [--no-restore-checks]` | Check the bucket's invariants; exit code `1` on findings. `--deep` also checks that every data file a range's manifest names is present (one listing per range). By default it also looks for cross-range calls that lost a message; `--no-restore-checks` turns that off. |
| `table show`, `owners`, `ranges retired`, `wal floors`, `range dump <id>` | Read-only views of the range table, ownership records and data. |
| `cluster status` | Cluster version, gates and live sessions. |
| `cluster enable-<gate> --apply` | Turn on a one-way feature gate after every node is upgraded. |
| `table restore [--force] --apply` | Rebuild a lost range table from its newest saved version, only when the rest of the bucket proves it is the last one. Every node must be stopped. |
| `keys create <tenant> <role> --apply` | Create an API key directly in the bucket. |
| `identity publish\|activate\|retire <tenant> --apply` | Rotate a tenant's request-signing key. |
| `pricing`, `pricing set <model> <input> <output> --apply` | The token price table, in micro-USD per million input and output tokens (`0 0` removes a model). |
| `backup --to URI`, `backup list`, `backup prune`, `restore --from URI`, `backup check-restore` | Backups at another store. |
| `trash list`, `trash restore` | Inspect and restore cairnflow's own deletions. |
| `fork --to <prefix> [--artifacts live\|all\|none]` | Copy the cluster under another prefix for investigation, with live artifacts by default. |
| `intent clear <range>`, `range force-retire <id>` | Repair a stuck split or merge. |

| Option | Meaning |
|---|---|
| `--backup-env-prefix P` | The backup store (`--to` of `backup`, `backup list` and `backup prune`, `--from` of `restore`) takes its credentials, endpoint and region only from `P`-prefixed `AWS_*` variables, for example `CAIRN_BACKUP_AWS_ACCESS_KEY_ID`. Without it, the backup store uses the same `AWS_*` variables as `--main-store`. |
| `--report-to-bucket ID` | Also write the result (the JSON of `--json`, the exit code, the error; no secrets) to the cluster's bucket, readable with a `provision` key at `GET /admin/ops-reports/{ID}`. Only for `backup`, `backup list`, `backup prune`, `restore`, `cluster status`, `cluster enable-*` and `table restore`. `ID` is 1 to 128 characters of `A-Za-z0-9-_.` and must be new for each run. |
| `--who NAME` | Who is running the command, recorded in the bucket's audit log (default `$USER@$HOST`). |
| `--json` | Print JSON. |

Commands that write run dry unless you add `--apply`. Exit codes: `0` done, `1` `verify` found
problems, `2` bad arguments, `3` refused, `4` store error. See
[Backup and restore](/docs/self-hosting/backup-restore).
