Workflow execution engine
The workflow execution engine turns alert, incident, interval, and manual events into controlled workflow runs. It selects matching workflows, builds an execution context, evaluates conditions, calls provider steps and actions, applies retry and concurrency rules, and records the result for later review. See Workflow automation for the wider product model and Workflow definition and management for how workflow definitions are created and changed.
Runtime workflow model
The engine separates trigger selection from workflow execution. A trigger evaluator decides whether an event should start a workflow. The scheduler then owns the run, so event handling can return while the workflow continues in the background.
The main runtime path is:
flowchart LR
E[Trigger event] --> M[Trigger matching]
M --> Q[Scheduler queue]
Q --> R[Execution record]
R --> C[Execution context]
C --> S[Steps]
S --> A[Actions]
A --> F[Final status and results]
S -. failure .-> O[Optional on_failure action]
O --> F
An event can be an alert or incident. An interval workflow has no event payload and is found by the scheduler when its interval has elapsed. A manual run supplies an event-shaped payload and may also supply workflow inputs.
Trigger sources
Alert events enter through alert processing. The manager checks enabled workflows and considers only alert triggers. Incident events enter when incident state changes produce a workflow event. Interval runs are found by the scheduler from stored workflow timing. Manual runs enter through the workflow run API, which creates an execution request before it adds the run to the queue.
These paths converge in the scheduler. This gives each run the same execution lifecycle even when the source and event data differ.
References: keep/…/workflowmanager.py• L124-176 keep/…/workflowmanager.py• L287-314 keep/…/workflowscheduler.py• L115-182 keep/…/workflows.py• L357-451 keep/…/incidents_bl.py• L214-222
Scheduler queue
The scheduler drains queued event runs on a regular loop and submits them to a bounded worker pool. It also checks interval workflows and periodically marks long-running records as timed out. A queued item carries the tenant, workflow, event, execution identifier when one already exists, and any manual inputs.
The queue is an in-process handoff. The scheduler removes a batch from the queue before it starts worker tasks, so service shutdown and worker failure can affect work that has not reached execution.
References: keep/…/workflowscheduler.py• L75-113 keep/…/workflowscheduler.py• L408-475 keep/…/workflowscheduler.py• L649-672
CEL trigger evaluation
CEL is the expression language used to decide whether an alert trigger matches an alert. The engine evaluates the expression against the alert payload before it enriches the alert and places a workflow run on the scheduler queue.
Alert trigger evaluation
For each incoming alert, the manager checks each enabled workflow and each alert trigger. A trigger without filters or CEL is treated as matching. A trigger with the older filter form is converted to CEL first. The current CEL form takes precedence as the runtime contract for new definitions.
The evaluator prepares the payload for common alert fields. It supports list-aware source checks and normalizes alert severity so comparisons use the defined severity order. A false result skips the workflow. A missing field or evaluation error also prevents that event from entering the queue.
After a match, the engine loads alert enrichment and can apply change-only rules. These rules compare the current alert with the prior alert for the same fingerprint. They prevent a matching trigger from running when the fields that matter have not changed.
References: keep/…/workflowmanager.py• L287-527 keep/…/cel_utils.py• L1-200
Incident rule evaluation
Incident correlation is a related CEL path that produces the incident events consumed by incident-triggered workflows. The rules engine evaluates stored rules against alerts, groups matching alerts, and creates or updates an incident when the rule threshold and creation policy allow it.
The alert processing flow sends events to workflow trigger matching and then runs the correlation rules engine. When the rules engine changes an incident, it sends a created or updated workflow event. The workflow manager checks the incident trigger events and queues the matching workflows.
References: keep/…/process_event_task.py• L538-577 keep/…/rulesengine.py• L58-71 keep/…/rulesengine.py• L111-225 keep/…/rulesengine.py• L756-766
Execution context
The execution context is the shared state for one workflow run. It lets later steps use the trigger payload, inputs, constants, secrets, condition results, and outputs from earlier steps without passing each value through the scheduler.
The context exposes both event and incident views. It also exposes aliases for alert data, step data, the current item in a foreach operation, and the last successful workflow result when the workflow asks for it. Each queued run gets its own parsed workflow instance because provider and step state changes during execution.
Template rendering uses this context to build provider parameters and condition expressions. Missing values can fail a required render. Workflow helper functions can provide defaults for optional values. Workflow secrets are loaded into the context for the run and are not part of the normal execution result.
References: keep/…/contextmanager.py• L20-85 keep/…/contextmanager.py• L110-188 keep/…/workflowmanager.py• L576-595 keep/…/iohandler.py• L456-504
Step and action processing
A workflow runs its steps first and its actions second. A step obtains or prepares data through a provider query. An action sends a change or notification through a provider. Both use the same condition, rendering, retry, and context rules.
Condition processing
Before a step or action runs, the engine creates its configured conditions and evaluates each one. It stores each condition result in the execution context. An explicit if expression then combines those results. When no if expression exists, all configured conditions must pass. A step or action with no condition is eligible to run.
A false condition skips the provider call. A throttle can also skip an otherwise eligible action. A skipped item does not stop the workflow unless the workflow reaches a later stop rule based on a successful run.
References: keep/…/step.py• L162-303 keep/…/condition_factory.py• L7-21 keep/…/base_condition.py• L46-71
Provider dispatch
For an eligible item, the engine renders provider parameters from the current context and dispatches the call by item type. Steps call the provider query operation. Actions call the provider notify operation. A successful call writes its output to the context, where later items can read it.
The engine processes items in definition order. The continue setting can stop the step or action sequence after a successful item. The continue_on_error setting controls whether a failed item is ignored for control flow or causes the workflow to report an error.
foreach changes one item into several provider calls. The engine sets the current value for each iteration and collects step results for later use. The step is considered successful when at least one iteration runs successfully.
References: keep/…/workflow.py• L71-171 keep/…/step.py• L73-160 keep/…/step.py• L331-386 keep/…/contextmanager.py• L190-275
Retry and concurrency policy
The engine has separate controls for provider failure and overlapping workflow runs. A step or action can retry its provider call after a failure. A workflow strategy controls what happens when another run with the same fingerprint is already active.
| Concern | Policy | Result |
|---|---|---|
| Provider call failure | Per-item retry count and delay | The same provider call is tried again before the item fails. |
| Same-fingerprint overlap | parallel | The new run receives its own execution number and can run. |
| Same-fingerprint overlap | nonparallel | The new run is finished with an error and is not retried. |
| Same-fingerprint overlap | nonparallel_with_retry | The new run returns to the queue for a later scheduler cycle. |
The default workflow strategy is nonparallel_with_retry. On a retry, the scheduler refreshes alert or incident enrichment before dispatch. This allows a later run to see state written by the earlier run, such as a ticket identifier or a changed alert status.
References: keep/…/step.py• L45-49 keep/…/step.py• L342-376 keep/…/workflow.py• L13-20 keep/…/workflowscheduler.py• L497-629
Execution records and results
Each run has a durable execution record. The record links the workflow revision, tenant, trigger description, event identity, status, duration, error text, and result data. Event links allow the UI to relate an execution to an alert fingerprint or incident.
Execution lifecycle
Event and interval runs create an in_progress record before provider work begins. Manual runs create the record when the API accepts the run, which gives the caller an execution identifier for status tracking. The worker sets the event or incident context and calls the workflow manager.
The worker marks the record success when no action errors remain. It marks the record error when a workflow or action fails. A missing provider configuration has the separate providers_not_configured status. A run that stays in progress beyond the timeout threshold is also finished as an error.
References: keep/…/db.py• L199-259 keep/…/workflowscheduler.py• L184-282 keep/…/workflowscheduler.py• L374-406 keep/…/workflow.py• L77-170
Failure handling
The optional on_failure action runs when the workflow raises an error or when actions return errors that are not allowed to continue. The failure message is made available to that action. The final record keeps the error and the elapsed execution time so callers can distinguish a completed run from a failed run.
References: keep/…/workflowmanager.py• L637-715 keep/…/workflow.py• L97-107 keep/…/workflow.py• L173-199 keep/…/db.py• L964-1000
Result retrieval
After a normal run, the manager stores provider results under the item names used by the workflow. Results are converted to a JSON-safe form when a provider returns a value that cannot be stored directly. The read API returns the status, trigger, event identity, logs, error, duration, and results for a single execution. It also provides paged run summaries for a workflow.
References: keep/…/workflowmanager.py• L717-766 keep/…/db.py• L1207-1221 keep/…/db.py• L2171-2193 keep/…/workflows.py• L977-1073 keep/…/workflows.py• L1089-1145
Change checklist
When these files or sections are changed, remember to consider these:
- Changes to trigger fields or legacy filters must stay aligned with workflow parsing, CEL preparation, alert matching, and incident trigger matching.
- Changes to condition names or condition result shape must preserve the condition factory contract and the context paths used by
ifexpressions. - Changes to provider call inputs or outputs must preserve both template rendering and the step-versus-action dispatch split.
- Changes to workflow strategy or execution uniqueness must preserve collision behavior and the retry queue payload.
- Changes to execution status, event links, or result shape must stay aligned across database records, API DTOs, and the execution detail client model.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Workflow is disabled | The run is skipped before steps start. | The event and interval paths exclude disabled workflows. |
| CEL cannot compile or evaluate | The alert does not queue that workflow. | Validate field names and expression types against the event payload. |
| Required provider configuration is missing | The execution ends with providers_not_configured. | Configure every provider used by the workflow for the tenant. |
| A non-parallel run collides on the same fingerprint | nonparallel ends the new run with an error; the retry strategy requeues it. | Select the strategy that matches whether duplicate work is safe. |
| A provider call exhausts its item retries | The item raises an error and the workflow can run on_failure. | Set a retry count and interval that fit the provider's failure pattern. |
An execution remains in_progress past the timeout limit | The scheduler marks it as an error even if the worker stopped without a final update. | Check worker health and backend logs for long runs. |
| Provider results cannot be written as JSON | The normal result write fails unless the safe serialization path can represent the value. | Return JSON-safe provider results where possible. |
Key files
| File | Why to read it |
|---|---|
| keep/…/workflowmanager.py | Main bridge from incoming events to trigger matching, workflow execution, failure handling, and result saving. |
| keep/…/workflowscheduler.py | Queue, worker dispatch, interval runs, concurrency collisions, timeouts, and execution completion. |
| keep/…/workflow.py | Ordered step and action flow plus stop and continue-on-error behavior. |
| keep/…/step.py | Shared condition, throttle, rendering, provider dispatch, foreach, and item retry behavior. |
| keep/…/contextmanager.py | Runtime data shared by triggers, templates, conditions, steps, and actions. |
| keep/…/rulesengine.py | CEL-based alert correlation that creates incident events for incident-triggered workflows. |
| keep/…/db.py | Durable execution creation, status updates, timeout queries, result storage, and execution reads. |
| keep/…/workflow.py | Workflow execution records and links to workflow revisions, alerts, incidents, and logs. |
| keep/…/workflows.py | Manual run entry points and APIs that expose execution status, logs, results, and run summaries. |
| keep/…/process_event_task.py | Alert processing handoff into workflow trigger matching and incident rule evaluation. |