> ## Documentation Index
> Fetch the complete documentation index at: https://docs.oasm.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Workflow engine

> How workflows are stored, validated and triggered: the DAG model, default templates, the trigger paths (target events, target schedules, project schedules, project events, manual runs), batching windows and loop protection

A workflow is a small DAG of scanning steps. The workflow engine is the part of the core API that turns that definition into runs: it validates the graph when it is saved, decides *when* a run starts, and — together with the [job registry](https://docs.oasm.io/architecture/job-registry.md#advancing-a-run) — decides *which* step runs next.

The workflow file format and its options are documented for users in [Workflows](https://docs.oasm.io/workflows.md). This page explains the machinery behind them.

```mermaid theme={null}
flowchart LR
    subgraph Sources["Trigger sources"]
        TE["Target events<br/>create, re-scan, git_org.synced"]
        TS["Target scan schedule<br/>BullMQ repeatable job"]
        PS["Project schedule<br/>BullMQ job scheduler"]
        PE["Project events<br/>via event bridge"]
        M["Manual run<br/>console, API, agent"]
    end

    PE --> Win["Batch window<br/>Redis, per trigger"]
    TS --> TE
    TE --> Start
    PS --> Start
    Win --> Start
    M --> Start

    Start["Start run<br/>pin scope"] --> Engine["Engine<br/>dispatch ready steps"]
    Engine --> Jobs["Job registry<br/>jobs → workers"]
    Jobs -->|"step drained"| Engine
    Engine -->|"run completed"| Ev["workflow.run.completed<br/>project.run.completed"]
    Ev -.-> PE
```

## The definition

A workflow is stored as JSON in the database, authored as YAML. Its `jobs` are a map keyed by job id; each job names a tool (`run`), optional inline `config`, and the ids it `needs`. Jobs without `needs` are roots and start together.

Every save goes through the same checks, and all errors are returned at once:

* **Graph** — every `needs` id exists and there is no cycle.
* **Tools** — each step's tool category can consume the step's declared inputs.
* **Reachability** — for target-triggered workflows, each step can scan at least one target type that the trigger fires for.

Because JSON objects have no key order, the engine never relies on the order of the `jobs` map: it orders steps from the graph itself.

### Default workflows

Every workspace gets a set of default workflows shipped with the core API as YAML templates — domain discovery, IP address discovery, a basic vulnerability scan, repository scanning and on-demand URL discovery. They are created with the workspace and **re-synced on every boot**: a template row whose content differs from the shipped file is overwritten. Default workflows are therefore read-only, and an upgrade can improve them without a migration. To customize one, copy it into a [project](https://docs.oasm.io/project.md).

## Trigger paths

A run can start in five ways. Each has its own plumbing, chosen for the guarantees it needs.

| Path | Mechanism | Why |
| - | - | - |
| **Target events** | In-process events emitted when a target is created or re-scanned, or a Git organization finishes syncing. Every workflow whose `on.target` lists the event starts a run for that target. | Synchronous with the change that caused it; no extra hop. |
| **Target scan schedule** | One repeatable BullMQ job per scheduled target. When it fires, the target is re-scanned, which emits the target event above with the run type **scheduled**. | Schedules live in Redis next to the worker queues and fire once across all replicas. |
| **Project schedule** | One BullMQ job scheduler per cron entry of a project workflow. On boot, the API reconciles schedulers with the database, adding missing ones and removing orphans. | Losing Redis data never silently stops a schedule. |
| **Project events** | The `projects` lane of the [event bridge](https://docs.oasm.io/architecture/event-bridge.md) receives `project.assets.added`, `project.run.completed` and `vulnerability.detected`, and hands them to the trigger service. | Durable, at-least-once delivery, decoupled from the producer. |
| **Manual** | The console, the REST API or the [Security Agent](https://docs.oasm.io/architecture/ai-agent.md). | — |

### Batch windows

Event triggers do not start one run per event. A discovery that adds 3,000 assets to a project would otherwise start 3,000 runs. Instead, each trigger has a **window** (`windowMinutes`, default 10):

1. The first event opens the window and schedules one delayed job. Later events only add their assets to the pending set in Redis.
2. When the window closes, the pending assets that are *still in the project* are split into runs of at most `maxAssets` (default 200).
3. The causing runs are recorded on the new runs, so the console can show why a run started.

Adding an asset twice — for example on a redelivered event — adds it once, because the pending set and the project membership are both idempotent.

### Loop protection

Project workflows can trigger each other, so the engine guards against loops:

* A workflow's own run finishing, or its own findings, never start that workflow again. Only assets it brings into the project can.
* Every run records its **chain depth**. An event more than three hops down a chain starts nothing.
* Archived projects ignore every trigger and their schedulers are removed until the project is reopened.

## A run's scope

A run records its **scope** when it starts — the assets of a target, or the project assets it was started on — and every step reuses that stored scope. Inputs are looked up when a step starts, not when the run starts, so a later step sees what earlier steps of the same run discovered. A project run never leaves the project's assets, and it cannot start unless every root tool is installed in the workspace.

From there, execution is the [job registry](https://docs.oasm.io/architecture/job-registry.md)'s business: each step fans out into one job per input, workers claim them, and the engine advances the run when a step drains.

## Run outcomes

When a run ends, the engine emits an in-process event that several parts of the platform listen to:

* **Statistics** diff the target's inventory against a snapshot taken at the start of a discovery run and publish `workflow.run.completed` with what the run changed.
* **Projects** turn it into `project.run.completed`, which can start the next workflow of a pipeline.
* **URL discovery** publishes `asset.url.discovered` for on-demand crawls.

## Related

> [card] [Workflows](https://docs.oasm.io/workflows.md)
>
> The workflow file, triggers and data kinds, from the user's side

> [card] [Job registry](https://docs.oasm.io/architecture/job-registry.md)
>
> Claiming, results and advancing a run

> [card] [Projects](https://docs.oasm.io/project.md)
>
> Project pipelines, scope rules and triggers

> [card] [Event bridge](https://docs.oasm.io/architecture/event-bridge.md)
>
> The bus that carries project events


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