# Run providers

> Add entries to the New tab launcher's Run column with a command that prints JSON, and learn the environment, output format, timeouts and failures.

The New tab launcher lists commands you can start in one keystroke. Static entries come from the `run` key of your [.flowspace.json](https://flow.worldbreakstudios.com/docs/configuration/flowspace.md) files. A **run provider** adds entries dynamically: a command you register that prints JSON, run each time the launcher opens. Use one to offer every `just` recipe, `mise` task or `package.json` script without listing them by hand.

## Where run entries appear

The launcher opens on a tile strip's "+" or `⌘T` while Settings › General › Tabs › New tab (`tabs.new_tab`) is **Ask**, the default. Under its tab-type tiles it shows:

- **Run**: commands that open in a new terminal tab, titled by their label. `⌥1`–`⌥9` launch the first nine visible rows. The static `run` entries come first, then each provider's rows. The column's caption names the file or provider each row came from.
- **Web**: `docs` pages, then pages you visited recently (this repository's first). This column needs the Web tabs experiment (`features.web_tabs`).

Type in the launcher's field to filter rows by label, command or URL. `↑` / `↓` select a row and `↩` launches it.

## Register a provider

Add a `run_providers` entry to any flowspace file: a checked-in `.flowspace.json`, your `~/.flow/repo_config/REPO/flowspace.json`, or `~/.flow/flowspace.json` for every repository.

```json
{ "run_providers": [{ "name": "just", "command": "~/bin/just-flow" }] }
```

| Field | Meaning |
|---|---|
| `name` | Shown in the Run column's caption and in error rows. When two files register the same name (compared without surrounding spaces), the earlier file wins. |
| `command` | A shell command line. |

Providers from several files run side by side; see [Settings scopes](https://flow.worldbreakstudios.com/docs/configuration/settings-scopes.md) for the file order.

## When providers run

Flow runs every provider each time a launcher opens, and again when its session's directory changes while it shows. Each run:

- happens in the background, so the launcher never waits on it;
- is `$SHELL -lc COMMAND`, with the session's directory as the working directory;
- gets its own process group, killed after **5 seconds**. Once the shell exits, anything still running in that group is killed too: don't leave work in the background. Whatever reached stdout by then is the output.

Results are cached per directory. Sessions on remote hosts never run providers.

## Environment

A provider gets your usual environment plus:

| Variable | Value |
|---|---|
| `FLOW_RUN_CONTRACT` | `1`, this contract's version |
| `FLOW_CWD` | the session's directory |
| `FLOW_REPO_ROOT` | the git work-tree top of that directory, when there is one |
| `FLOW_BRANCH` | the current branch, when known |
| `FLOW_PR` | the branch's pull request number, when known |
| `FLOW_SESSION` | the session's title |
| `FLOW_HOST` | the ssh destination of a remote session (never set in practice, since remote sessions run no providers) |

## Output format

Print **one JSON object** on stdout, at most **1 MiB**:

```json
{
  "version": 1,
  "run": [
    { "label": "Build", "command": "just build", "detail": "release" }
  ],
  "docs": [
    { "label": "Runbook", "url": "https://wiki.example.com/runbook" }
  ]
}
```

- `version` is required and must be `1`.
- `run` and `docs` are optional and take the same entry shapes as the flowspace keys: `{label, command, cwd?, detail?}` and `{label, url}`.
- A relative `cwd` is relative to the session's directory; a leading `~/` is your home directory.
- Unknown keys are ignored, and a malformed entry is skipped.
- stderr is ignored unless the provider fails.

## Example provider

A provider that offers every `just` recipe in the session's directory:

```sh
#!/bin/sh
# ~/bin/just-flow: list just recipes for the Flow launcher.
[ "$FLOW_RUN_CONTRACT" = 1 ] || exit 2
cd "$FLOW_CWD" || exit 1
command -v just >/dev/null || { echo '{"version":1}'; exit 0; }
just --summary 2>/dev/null | tr ' ' '\n' | awk '
  BEGIN { printf "{\"version\":1,\"run\":[" }
  NF { printf "%s{\"label\":\"%s\",\"command\":\"just %s\"}", sep, $0, $0; sep = "," }
  END { print "]}" }'
```

Make it executable (`chmod +x ~/bin/just-flow`), register it as shown above, then press `⌘T` in a repository with a `justfile`.

## Failures

A provider that fails shows as **one dim row** in the Run column, reading `NAME: REASON`. That row can't be selected or launched, and the other providers and static entries are unaffected.

| Failure | Reason shown |
|---|---|
| The shell could not be started | `could not start: …` |
| It ran past 5 seconds (it is killed) | `timed out after 5s` |
| A non-zero exit | `exit status N`, plus its first stderr line |
| Nothing on stdout | `printed nothing` |
| More than 1 MiB on stdout | `output over 1 MiB` |
| Not JSON, or not an object | `bad JSON` / `not a JSON object` |
| `version` missing or not 1 | `missing version` / `unsupported version N` |

## Limits

- Remote sessions run no providers and read only `~/.flow/flowspace.json`; their run entries run in the session's own terminal on the host.
- A session on a paired device lists only the static `run` entries of that device's checkout, never its providers.
- `docs` entries, from a file or a provider, only show while the Web tabs experiment is on.
