# Tasks and asking first

### Buttons that follow a task

Some services answer a command with a task to follow: Proxmox starts a backup or a shutdown and returns the task's id. Give that button `completion`, and Legato follows the task until it ends:

```json
"completion": {
  "path": "/api2/json/nodes/pve/tasks/{data}/status",
  "done": { "path": "data.status", "equals": "stopped" },
  "success": { "path": "data.exitstatus", "equals": "OK" },
  "interval": 2,
  "timeout": 300
}
```

| Field | Required? | Meaning |
| --- | --- | --- |
| `path` | yes | The task's address on the source's host, filled from the command's answer. Filled values are escaped, so an id with `:` or `/` in it stays one part of the path. |
| `done` | yes | A condition on the task's answer that holds once it has ended. |
| `success` | yes | A condition that must also hold then, or the press didn't work. |
| `interval` | no | Seconds between reads of the task, 1 to 60. Default 2. |
| `timeout` | no | Seconds to follow it, 1 to 3600. Default 300; a backup can need 3600. |

- **While it runs:** Legato reads the task with GET, signed in as the source is, and the button shows as busy. A press from Shortcuts or Spotlight returns once the service accepts the command; Shortcuts says it started, and the panel shows how it ends.
- **When it doesn't end well:** the card says so if the task fails, its status can't be read, or the timeout passes first. The card is read again either way, since the command went through. Check the service before pressing again: Legato losing track of a task doesn't stop it.
- **Quitting Legato** stops following the task, and it isn't picked up again on the next launch.
- **A button with `when`** reads its source once more just before sending, so a Start chosen from an old answer isn't sent to a guest that's already running. The service still decides; this isn't a lock.

`completion` doesn't change who asks first: the panel and Shortcuts ask when the button has `confirm`, and Spotlight presses it directly. A checklist row's action can't have it.

In an action body, `{action.id}` becomes a new UUID for each press. Transport retries keep that UUID. This is suitable for command deduplication; a timestamp is not. When a service wants the time of the press instead, `{action.at}` gives it in ISO 8601 UTC. A `success` condition still needs to check the service’s command result when HTTP 200 can contain a refusal.

### Buttons that ask first

Some presses are hard to undo: restarting a server, shutting down a machine other things run on. Give that button `confirm`, and a click in the panel shows a question in its card, where the buttons were, with Cancel and a button that runs it. Nothing is sent until that button is pressed.

```json
{ "label": "Restart host", "icon": "power", "path": "/api2/json/nodes/pve/status", "body": { "command": "reboot" },
  "confirm": { "message": "Restart pve? Every guest stops first.", "button": "Restart", "type": "pve" } }
```

- With `type`, the button stays dimmed until that text is typed exactly, as when a service asks for a name before deleting it. Keep it for the press that would cost the most.
- `"confirm": true` asks "Restart host?" with the label as the button.
- Escape cancels. Choosing another page or closing the panel puts the question away.
- **Spotlight presses it directly.** Choosing a button by name in Spotlight is the decision, so it doesn't ask again.
- **Shortcuts asks first,** with `message` when there is one (it doesn't ask for the typed text).
- **It never goes on a notification,** which can't ask, even with a `success` check.
- A checklist's `itemAction` can't ask first: a tick is meant to be quick. Put an action that needs a question on a button.

A button with `"kind": "open"` opens an https address from the source's answer in the browser, and sends nothing to the service. It takes `label`, `icon`, `url`, `when` and `prominent`. `url` is a template, such as `"{up.join}"` for a meeting's Join button or `"{html_url}"` for a GitHub page. An address that isn't https with a host doesn't open, and the card says why. As an `itemAction`, it fills `url` from the ticked row. With `inputs`, it's a form that opens one of your pages with what was typed, such as a search: see [Forms that open a page](https://legatobar.com/docs/button-input.md#forms-that-open-a-page).

For Home Assistant, this single action object goes inside a card's `actions` array:

```json
{ "label": "Turn off", "icon": "power", "method": "POST", "path": "/api/services/light/turn_off",
  "body": { "entity_id": "light.kitchen_main_lights" }, "when": { "path": "kitchen", "equals": "on" } }
```

### Buttons that act on what the card shows

Some requests name the thing the card is showing: the run that failed, the newest request waiting for approval, the stream playing now. Put `{answer.<path>}` in the button, and the press fills it from the card's source answer (after `derive`). This is a repository page's Re-run failed jobs, whose source derives `failed_run_id` and `failed_run_name` from the newest failed run:

```json
{ "label": "Re-run failed jobs", "icon": "arrow.clockwise", "method": "POST",
  "path": "/repos/example-org/example-app/actions/runs/{answer.failed_run_id}/rerun-failed-jobs",
  "confirm": { "message": "Re-run the failed jobs of {answer.failed_run_name}?", "button": "Re-run" },
  "when": { "path": "failed_run_id", "exists": true }, "error": "message" }
```

| Where | How the value goes in |
| --- | --- |
| `path` | As sent: a number as its digits (`18765432109`, never grouped), percent-encoded so `/`, `?`, `&` or `#` can't change the address. |
| `body` strings | A string that is `"{answer.x}"` and nothing else takes the value as the answer has it, so a number stays a number and a list stays a list. In other text, a number goes as its digits. |
| `label` | As the card writes text, cut to 24 characters. Keep words of its own, such as "Stop {answer.name}": Spotlight and Shortcuts list the button by them. |
| `confirm` `message` and `type` | As the card writes text; the question is cut to 160 characters. With `type`, the name to type can come from the answer, for a delete. |
| `completion` `path` | Filled at the press, as the path is; the task's own id still comes from the command's answer. |
| `headers` | Never: headers carry credentials. A page with one there doesn't load. |

- **The press uses the answer the card is showing.** Nothing is read again first, and nothing checks the service: if the run was re-run elsewhere, the service's refusal shows under the card, with its reason when `error` names where the answer gives one. Each value can be a filter's output, as in `{answer.name|lower}`.
- **A value the answer lacks** leaves the button dimmed, saying why when you point at it ("the answer has no failed_run_id"), and nothing is sent. So does one a path can't take: an empty text, `.` or `..`, a list, or a whole number too big to send exactly (give a big id as text in `derive`). `when` is how a page hides the button instead, as above. The Source Preview window lists such buttons under Issues.
- **If the card changes under you,** the press doesn't go out. A click, a menu choice or a form's Add sends only while the card's values are the ones it was chosen from; a question that's open goes away when they change, and the card says to press again.
- **Spotlight presses it with the latest answer,** without asking, as for any button. **Shortcuts** asks with the filled question and stops if the values change while it asks. A button whose values are missing is listed, but isn't pressed: both say it has nothing to act on.
- **It never goes on a notification,** which can stay for hours: a press from it would act on whatever the answer says then.
- A checklist's `itemAction` fills from its own row (`{id}`), not from `{answer.…}`. An `open` button's `url` and an Add Event `title` already read the answer with plain paths, such as `{html_url}`.

The same pattern fits other services. A series page's Search on Sonarr sends the series' number as a number:

```json
{ "label": "Search", "icon": "magnifyingglass", "path": "/api/v3/command",
  "body": { "name": "SeriesSearch", "seriesId": "{answer.series.id}" } }
```

Approving the oldest waiting request on Seerr names it first. Seerr's request list has no titles, so the page looks each one up with a [request for each row](https://legatobar.com/docs/source-requests.md#a-request-for-each-row) and derives `rows`, each with its `id` and `title`:

```json
{ "label": "Approve", "icon": "checkmark", "path": "/api/v1/request/{answer.rows[0].id}/approve",
  "confirm": { "message": "Approve {answer.rows[0].title}?" }, "when": { "path": "rows[0].title", "exists": true } }
```

And Tautulli ends the stream the card shows, with its session's key in the query: `"path": "/api/v2?cmd=terminate_session&session_key={answer.stream.session_key}"`.

