Introducing a new way to review your PRs.

Read the announcement

keephq / keep

The open-source AIOps and alert management platform

View on GitHub

22 documents · Updated Nov 23, 2026

docs / rule-evaluation-and-expression-functions

Rule evaluation and expression functions

Rule evaluation turns alert and workflow data into decisions about grouping, incident creation, provider calls, and alert creation. The system uses CEL for event and rule matching, plus rendered workflow expressions for conditions and values. A shared workflow context connects inputs, event data, earlier results, condition results, and expression outputs. See Automation primitives, Action execution and throttling, and Workflow execution engine for the surrounding automation model.

Evaluation languages

The system has two main expression paths. CEL evaluates structured event data for alert triggers, searches, and incident rules. Workflow expressions first render values from the execution context and then evaluate conditions or compute provider inputs.

CEL event matching

CEL means Common Expression Language. It describes a boolean test over an alert-like payload, so the system can decide whether an event matches a workflow trigger, search filter, or grouping rule. Alert fields are converted to CEL values before evaluation, and common differences such as alert severity names and list-valued sources are normalized for matching.

The API can validate the syntax of a CEL expression before it is used. The validation path checks whether the expression can be parsed and returns a marker for a parse error. It does not decide whether the expression matches a specific event.

References: keep/…/workflowmanager.py• L329-474 keep/…/rulesengine.py• L460-512 keep/…/cel.py• L14-38

Workflow expressions

Workflow conditions use values from the current execution context. The engine renders those values into an expression and evaluates the result as a decision for a step or action. A condition can therefore use an event field, a workflow input, a prior step result, or a value produced by another condition.

The same expression path supports values inside provider parameters. This lets a workflow use one result to form the next request or notification instead of copying data outside the workflow context.

References: keep/…/step.py• L162-271 keep/…/iohandler.py• L189-291 keep/…/iohandler.py• L525-552

Rule and condition decisions

Rules and conditions answer different questions in the automation flow. A rule decides how an incoming event belongs to an incident or whether an event starts a workflow. A condition decides whether an already selected workflow item can continue.

Incident rule evaluation

The rules engine tests accepted alert events against tenant rules. A matching rule derives a grouping identity from the event, then creates or reuses an incident group. Threshold and creation settings control when a group becomes visible. A rule that requires several sub-rules can wait until matching events cover all required parts.

After an incident changes, the rules path can emit a workflow event. This connects incident state to incident-triggered automation without making the condition evaluator responsible for incident storage.

References: keep/…/rulesengine.py• L58-225 keep/…/rulesengine.py• L361-396 keep/…/rule.py• L33-60

Workflow trigger evaluation

An alert trigger uses CEL to decide whether an alert starts a workflow. A missing filter means the trigger can run without a match test. A present filter is converted to CEL for older workflow definitions, while new definitions use the CEL expression directly.

The trigger path normalizes source and severity before it evaluates the expression. A false result skips the workflow. An evaluation error also prevents that workflow from starting, rather than allowing a partial match.

References: keep/…/workflowmanager.py• L329-474 keep/…/cel_utils.py• L6-43

Workflow condition evaluation

Before a step or action calls its provider, the engine evaluates its configured conditions. It records each result in the workflow context. An explicit if expression combines those results when present; otherwise, all configured conditions must pass.

The condition result is a boolean decision with related comparison data. A condition alias exposes that result to later expression evaluation. A false decision skips the provider call, while a condition error stops evaluation of that workflow item.

References: keep/…/step.py• L175-283 keep/…/contextmanager.py• L202-249

Condition types and inputs

Condition types give workflow authors focused ways to compare rendered data. Each type receives a comparison target and a value from the workflow context, then returns a boolean and may add useful result data.

Threshold conditions

A threshold condition compares a rendered value with a numeric or percentage limit. It supports greater-than and less-than checks. Multiple thresholds can map one input to a named level, so a workflow can use the resulting level in later output or decisions.

Threshold inputs must use compatible types. A numeric threshold needs a numeric value, and a percentage threshold needs a percentage value within the supported range.

References: keep/…/base_condition.py• L46-71 keep/…/threshold_condition.py• L15-37 keep/…/threshold_condition.py• L94-134

Assert conditions

An assert condition evaluates a rendered expression and reports whether the assertion should allow the workflow item to run. It is useful when a decision needs a direct expression instead of a numeric threshold. Invalid expression syntax is an error and does not produce a safe false result.

References: keep/…/assert_condition.py• L6-55

Condition factory

The condition factory maps a condition type to its condition implementation. This keeps the step runner independent from the set of supported condition types and gives each type the same context and comparison contract.

References: keep/…/condition_factory.py• L7-21 keep/…/base_condition.py• L11-44

Expression context and functions

Expressions operate on a shared context for one workflow run. The context is the link between incoming data, workflow inputs, computed values, and provider results. Function helpers extend this context with common transformations and domain checks.

Expression inputs

The workflow context exposes the main data groups below:

InputPurpose
alert and eventThe event that caused the workflow run.
incidentThe related incident when the run has incident context.
inputsValues supplied to the workflow.
steps and actionsResults, variables, aliases, and condition records from earlier workflow items.
this and foreachThe current item or value during repeated work.
consts and secretsWorkflow constants and protected values.
last_workflow_results and last_workflow_run_timeData from the last successful run when the workflow requests it.

These names are part of the expression contract. A step result becomes available after a successful provider call, while condition aliases become available when their conditions are recorded. This ordering lets later work depend on earlier decisions without changing the event payload.

References: keep/…/contextmanager.py• L116-188 keep/…/contextmanager.py• L250-290

Custom functions

Custom functions are named helpers that turn common data work into reusable expression calls. The keep.* function set covers arithmetic, string and list operations, date conversion, JSON handling, dictionary updates, and selected alert-state checks. Functions that need tenant data receive the current tenant through the workflow context.

Workflow templates also provide fn.* helpers for safe text handling. The default, missing-value, upper-case, lower-case, and trim helpers let a template produce a usable value when optional context is empty.

References: keep/…/iohandler.py• L16-40 keep/…/iohandler.py• L293-410 keep/…/__init__.py• L25-159 keep/…/__init__.py• L459-510

Expression outputs

Expression results flow into three downstream forms. A boolean result controls whether a step or action runs. A scalar or structured result can become a provider parameter. A provider result is stored in the context so later conditions, functions, and actions can use it.

The Keep provider uses the same model when it creates alerts from workflow data. It filters candidate results with an if expression, renders alert fields, resolves optional ternary values, and sends the resulting alerts back through normal alert processing.

References: keep/…/step.py• L231-397 keep/…/contextmanager.py• L259-275 keep/…/keep_provider.py• L508-623 keep/…/keep_provider.py• L767-897

Automation decision flow

The evaluation flow moves from event selection to context-based decisions and then to an external effect. This diagram shows where CEL matching ends and workflow expression evaluation begins.

flowchart TD
    A[Alert or incident event] --> B{CEL trigger or rule match}
    B -- no match --> C[No workflow or incident change]
    B -- match --> D[Build workflow context]
    D --> E[Render condition inputs]
    E --> F[Evaluate conditions and if expression]
    F -- false --> G[Skip step or action]
    F -- true --> H[Render provider inputs]
    H --> I[Run provider step or action]
    I --> J[Store output in context]
    J --> K[Use output in later decisions or alert creation]

A rule match does not by itself call an external provider. It first establishes the event or incident context. The workflow engine then applies item-level conditions, throttling, and provider execution in the order described by the workflow.

References: keep/…/workflowmanager.py• L402-474 keep/…/step.py• L162-397 keep/…/contextmanager.py• L151-188

Change checklist

When these files or sections are changed, remember to consider these:

  • Preserve the names and shapes of context entries because workflow templates and if expressions address them directly.
  • Keep CEL preprocessing aligned with both the event values and the trigger and rule evaluators. Severity and source normalization can silently change match results.
  • When adding a condition type, preserve the factory naming contract and the recorded condition-result shape used by aliases and if expressions.
  • When adding a custom function, check its input and output types at template render time and its tenant-data needs at workflow run time.
  • Keep alert-producing expressions compatible with the normal alert processor so generated alerts receive the same identity and rule evaluation as external alerts.
  • Keep expression validation aligned with the CEL features accepted by runtime evaluation and search conversion.

Important failure modes

TriggerConsequenceGuard
CEL uses a field or method that the payload or parser does not supportThe matching workflow or rule is skipped.Validate the expression and use fields present in the event shape.
A CEL comparison uses incompatible typesThe event does not match, or evaluation reports an error.Use the supported severity and source forms and keep compared values compatible.
A rendered workflow value is missing when safe rendering is requiredCondition or provider-parameter evaluation fails before the provider call.Use the optional fn.* helpers or provide the missing context value.
A threshold compares a number with a non-number or mixes percentage and non-percentage valuesThe condition raises an error and the workflow item does not run.Keep the threshold and value in the same supported type.
A custom function receives malformed JSON, dates, or required alert identity dataThe expression returns a fallback or raises an error, so a decision or output can be wrong.Validate function inputs and provide the required fingerprint and tenant context for alert-state helpers.
An expression references a skipped step resultThe later condition or provider input can fail to render.Gate dependent expressions with the same workflow path that produces the result.

Key files

FileWhy to read it
keep/…/rulesengine.pyShows how alert events become incident changes through CEL rules, grouping, thresholds, and workflow events.
keep/…/workflowmanager.pyShows how alert triggers convert and evaluate CEL before a workflow starts.
keep/…/step.pyShows the shared order for condition evaluation, if decisions, provider inputs, and stored outputs.
keep/…/contextmanager.pyDefines the data available to expressions and how condition and provider results enter the context.
keep/…/iohandler.pyShows template rendering, custom function calls, workflow helper functions, and expression parsing.
keep/…/condition_factory.pyDefines the extension point for condition types.
keep/…/threshold_condition.pyIllustrates numeric, percentage, and multi-level condition decisions.
keep/…/keep_provider.pyShows how expressions filter and build alerts that re-enter the alert pipeline.
keep/…/__init__.pyDefines the reusable keep.* expression function set.
keep/…/cel.pyDefines the API contract for CEL syntax validation.