# Workflows

A workflow chains a few approved steps inside one vault: select
findings, keep the severe ones, mark them for triage, count them and
tell you. It runs when you start it, on a schedule or when something
happens in the vault, and it can never do more than the steps it is
built from.

## Steps

Only these steps exist. A workflow cannot run code, call an address of
its choosing or reach anything else.

| Step | Kind | Takes | Gives | Notes |
|---|---|---|---|---|
| Select findings | ingest | | findings | by severity, status and text, most severe first, up to 500 |
| Keep severe ones | classify | findings | findings | at or above a severity |
| Set status | classify | findings | findings | changes data, recorded in each finding's history |
| Summarize | analyze | findings | count, text | e.g. `3 findings: 1 high, 2 medium` |
| Prepare a report | report | findings | report | a draft from a template, for you to review |
| Notify me | notify | text (optional) | | an in-app notification |
| Refresh program scope | ingest | | text | the linked program; a widening waits for you |
| Start a scan | active | | scan | stops for your approval; only in runs you start |

Each step takes its inputs from outputs of earlier steps, of the same
type. Steps run top to bottom.

## The canvas

Each step is a card with its inputs on the top edge and its outputs on
the bottom edge. Click an output, then an input of the same type on a
later step to connect them; inputs that cannot take it are dimmed.
Click a wire to remove it. Drag a card by its header to move it; moving
a card above the step it reads from drops that wire, since it would run
first. Select a card to configure it on the right: its id, its
parameters, how often it retries (0 to 3). Every card has the same
actions on right click, and Delete removes the selected one.

Mistakes are listed above the canvas and marked on the card they
concern. A workflow with problems can be saved and finished later, but
it cannot run.

## Versions

Changes stay a draft until you save them. Saving new steps or triggers
makes a new version; **Versions** lists them, and restoring one makes it
current again as a new version. A run always executes the version it
was started with.

## Triggers

- **When you run it.** **Run** starts a run now. **Dry run** goes
  through every step without changing anything: steps that change data
  only report what they would hand on, and approvals are skipped.
- **On a schedule**, every 15 minutes to every week.
- **On an event** in the vault: a scan completes, or a finding is
  created.

A schedule slot or an event starts one run, however often it is
delivered, so a restart or a repeat never runs the same thing twice.
Only you create, change, start and delete workflows; agents are
refused. A copied template stays off until you turn it on.

## Runs

Each run keeps the state of every step (succeeded, failed, waiting,
attempts, outputs) and a log. A step that fails is retried as often as
it says; when it still fails, the run stops there and keeps what it
finished. **Resume** continues from the first unfinished step without
repeating the others. **Cancel** stops a run: a step in progress
finishes, nothing after it starts.

Every step is checked when it runs, not when it was saved: the workflow
must still be on, its definition must still check, and each step goes
through the same checks as the interface (the vault scope, the
engagement rules, the emergency stop). **Start a scan** stops the run
until you approve that step, and it is refused in runs started by a
schedule or an event. At most two runs execute at once, and each step
has two minutes.

## Templates

**Templates** lists ready-made workflows with the steps they use, where
they come from and what they need: a daily triage digest, a follow-up
after a scan, a report of validated findings, a scope watch, and a scan
followed by a summary. Using one copies it into the vault, turned off.

## REST

| Route | Body | Returns |
|---|---|---|
| `GET /workflows/nodes` | | the steps and the events |
| `GET /workflows/templates` | | templates |
| `POST /workflows/check` | definition | `{problems}` |
| `GET/POST /vaults/{id}/workflows` | definition | workflows / the new one |
| `POST /vaults/{id}/workflows/from-template/{key}` | | the copy, turned off |
| `GET/PATCH/DELETE /vaults/{id}/workflows/{wid}` | changes | the workflow |
| `GET /vaults/{id}/workflows/{wid}/versions` | | versions |
| `POST /vaults/{id}/workflows/{wid}/versions/{v}/restore` | | the workflow |
| `GET/POST /vaults/{id}/workflows/{wid}/runs` | `{dry_run}` | runs / the new run (`409` when off or with problems) |
| `GET /vaults/{id}/workflow-runs/{rid}` | | the run |
| `POST /vaults/{id}/workflow-runs/{rid}/approve`, `/cancel`, `/resume` | | the run |
