Skip to content

armgit / AiSOC

Public Imported from GitHub
0

Open-source AI Security Operations Center: alert fusion, LLM-agent triage, MITRE ATT&CK investigation, and a replayable decision ledger for every agent step. Self-hostable, runs with no API keys, MIT licensed. Ships an MCP server for Claude, Cursor and Continue.

fix(agents/playbook): correctness pass on engine evaluation + flattening (#129)

Beenu Arora987d3bc2ea18
diff --git a/services/agents/app/playbook/engine.py b/services/agents/app/playbook/engine.py
index b195545d0..1abd53f74 100644
--- a/services/agents/app/playbook/engine.py
+++ b/services/agents/app/playbook/engine.py
@@ -98,19 +98,76 @@ class PlaybookRun:
def _resolve_field(context: dict[str, Any], field: str) -> Any:
- """Resolve a dot-path field from context, e.g. 'alert.severity'."""
- parts = field.split(".")
+ """Resolve a dot-path field from context, e.g. ``alert.severity`` or
+ ``entities.0.name``.
+
+ Supports traversal through both ``dict`` keys and ``list``/``tuple``
+ indices. A blank field returns ``None`` (rather than the whole context,
+ which would conflate "missing path" with "no path requested").
+ """
+ if not field:
+ return None
cur: Any = context
- for p in parts:
+ for part in field.split("."):
if isinstance(cur, dict):
- cur = cur.get(p)
+ cur = cur.get(part)
+ elif isinstance(cur, (list, tuple)):
+ # Numeric indices may be specified as bare ints in a dot-path.
+ try:
+ idx = int(part)
+ except (TypeError, ValueError):
+ return None
+ if -len(cur) <= idx < len(cur):
+ cur = cur[idx]
+ else:
+ return None
else:
return None
return cur
+def _to_float(value: Any) -> float | None:
+ """Best-effort numeric coercion. Returns ``None`` for non-numeric input
+ so callers can decide how to handle the failure rather than crashing.
+
+ ``None`` and ``""`` coerce to ``0.0`` to preserve historical "missing
+ means zero" semantics for numeric comparisons.
+ """
+ if value is None or value == "":
+ return 0.0
+ if isinstance(value, bool): # bool is a subclass of int; treat explicitly
+ return 1.0 if value else 0.0
+ if isinstance(value, (int, float)):
+ return float(value)
+ try:
+ return float(value)
+ except (TypeError, ValueError):
+ return None
+
+
def _evaluate_condition(condition: StepCondition, context: dict[str, Any]) -> bool:
- """Return True if the condition passes."""
+ """Return True if the condition passes.
+
+ Two forms are supported:
+
+ 1. Structured: ``StepCondition(field=..., operator=..., value=...)``.
+ 2. Expression string: ``StepCondition(expression="severity in ['high','critical']")``.
+ Evaluated by :func:`_evaluate_expression` with a sandboxed
+ ``eval()`` over the run context. Falsy/unparseable expressions
+ return ``False`` so a malformed playbook never silently "passes".
+ """
+ # Expression-string form takes precedence when set. Previously this was
+ # silently ignored, which made every expression-only condition evaluate
+ # as ``None == None`` (i.e. always true).
+ if condition.expression:
+ return _evaluate_expression(condition.expression, context)
+
+ if not condition.field:
+ # No field and no expression — nothing to evaluate. Treat as "no
+ # condition" (i.e. pass) so an empty StepCondition() doesn't gate
+ # the step. The engine only invokes us when condition is non-None.
+ return True
+
value = _resolve_field(context, condition.field)
op = condition.operator
expected = condition.value
@@ -122,11 +179,140 @@ def _evaluate_condition(condition: StepCondition, context: dict[str, Any]) -> bo
if op == "ne":
return value != expected
if op == "contains":
- return expected in str(value) if value is not None else False
- if op == "gt":
- return float(value or 0) > float(expected or 0)
- if op == "lt":
- return float(value or 0) < float(expected or 0)
+ # Support both substring containment ("error" contains "err") and
+ # set-membership ("severity in ['high','critical']"). The structured
+ # form historically used "expected in str(value)" which crashed when
+ # expected was a list — and silently false-matched the common SOAR
+ # pattern of testing field membership in an allowed set.
+ if value is None:
+ return False
+ if isinstance(expected, (list, tuple, set)):
+ return value in expected
+ return str(expected) in str(value)
+ if op in ("gt", "lt"):
+ lhs = _to_float(value)
+ rhs = _to_float(expected)
+ if lhs is None or rhs is None:
+ return False
+ return lhs > rhs if op == "gt" else lhs < rhs
+ return False
+
+
+# Operators allowed in expression-string conditions, in longest-match-first
+# order so ``>=`` is tried before ``>``.
+_EXPR_OPERATORS: tuple[tuple[str, str], ...] = (
+ ("==", "eq"),
+ ("!=", "ne"),
+ (">=", "gte"),
+ ("<=", "lte"),
+ (">", "gt"),
+ ("<", "lt"),
+ (" not in ", "not_in"),
+ (" in ", "in_"),
+)
+
+
+def _parse_literal(token: str, context: dict[str, Any]) -> Any:
+ """Parse one side of an expression. Strings, numbers, bool/null/None,
+ and bracketed list literals are returned as Python values; everything
+ else is treated as a dot-path into the run context.
+ """
+ token = token.strip()
+ if not token:
+ return None
+ if token in ("null", "None"):
+ return None
+ if token in ("true", "True"):
+ return True
+ if token in ("false", "False"):
+ return False
+ # Quoted string literal
+ if len(token) >= 2 and token[0] == token[-1] and token[0] in ("'", '"'):
+ return token[1:-1]
+ # List literal: ['a', 'b'] or ["high","critical"]
+ if token.startswith("[") and token.endswith("]"):
+ inner = token[1:-1].strip()
+ if not inner:
+ return []
+ return [_parse_literal(piece, context) for piece in inner.split(",")]
+ # Numeric literal
+ try:
+ if "." in token:
+ return float(token)
+ return int(token)
+ except ValueError:
+ pass
+ # Otherwise: dot-path into context
+ return _resolve_field(context, token)
+
+
+def _evaluate_expression(expression: str, context: dict[str, Any]) -> bool:
+ """Evaluate a single ``"<lhs> <op> <rhs>"`` boolean expression.
+
+ Intentionally restricted: no ``and``/``or`` chains, no function calls,
+ no Python ``eval``. Callers that need compound logic should use
+ multiple structured conditions across multiple steps. Returns
+ ``False`` for any malformed input.
+ """
+ if not expression or not isinstance(expression, str):
+ return False
+ expr = expression.strip()
+
+ # "<field> exists" / "<field> is null" sugar.
+ lowered = expr.lower()
+ if lowered.endswith(" is not null") or lowered.endswith(" != null"):
+ base = expr[: -len(" is not null")] if lowered.endswith(" is not null") else expr[: -len(" != null")]
+ return _resolve_field(context, base.strip()) is not None
+ if lowered.endswith(" is null") or lowered.endswith(" == null"):
+ base = expr[: -len(" is null")] if lowered.endswith(" is null") else expr[: -len(" == null")]
+ return _resolve_field(context, base.strip()) is None
+
+ for token, op in _EXPR_OPERATORS:
+ # Use lowercase comparison for the " in "/" not in " word-operators
+ # so capitalization doesn't trip us up, but split on the original
+ # string to preserve quoted-literal casing.
+ haystack = lowered if op in ("in_", "not_in") else expr
+ needle = token if op in ("in_", "not_in") else token
+ idx = haystack.find(needle)
+ if idx == -1:
+ continue
+ lhs_raw = expr[:idx]
+ rhs_raw = expr[idx + len(needle) :]
+ lhs = _parse_literal(lhs_raw, context)
+ rhs = _parse_literal(rhs_raw, context)
+ try:
+ if op == "eq":
+ return lhs == rhs
+ if op == "ne":
+ return lhs != rhs
+ if op == "gt":
+ lf, rf = _to_float(lhs), _to_float(rhs)
+ return lf is not None and rf is not None and lf > rf
+ if op == "lt":
+ lf, rf = _to_float(lhs), _to_float(rhs)
+ return lf is not None and rf is not None and lf < rf
+ if op == "gte":
+ lf, rf = _to_float(lhs), _to_float(rhs)
+ return lf is not None and rf is not None and lf >= rf
+ if op == "lte":
+ lf, rf = _to_float(lhs), _to_float(rhs)
+ return lf is not None and rf is not None and lf <= rf
+ if op == "in_":
+ if rhs is None:
+ return False
+ if isinstance(rhs, (list, tuple, set)):
+ return lhs in rhs
+ return str(lhs) in str(rhs)
+ if op == "not_in":
+ if rhs is None:
+ return True
+ if isinstance(rhs, (list, tuple, set)):
+ return lhs not in rhs
+ return str(lhs) not in str(rhs)
+ except TypeError:
+ return False
+
+ logger.warning("Could not parse playbook condition expression: %r", expression)
return False
@@ -414,12 +600,19 @@ class PlaybookEngine:
break
pr.step_results.append({"step_id": step.id, "name": step.name, "status": step_status, "result": result})
- # Merge result into context for downstream steps
- pr.context.update({f"_step_{step.id}": result})
- # Also flatten top-level keys without underscore prefix for convenience
+ # Merge result into context for downstream steps. The namespaced
+ # key ``_step_<id>`` always reflects this step's full result.
+ pr.context[f"_step_{step.id}"] = result
+ # Flatten top-level keys for convenience so downstream steps can
+ # reference ``alert.severity`` or ``user_id`` directly without
+ # the ``_step_<id>.`` prefix. We *overwrite* prior values so a
+ # later enrichment step can refine context (e.g. replacing a
+ # placeholder ``user_id`` from the trigger with the resolved
+ # canonical id). Keys starting with ``_`` are reserved for
+ # engine bookkeeping and never auto-flattened.
for k, v in result.items():
if not k.startswith("_"):
- pr.context.setdefault(k, v)
+ pr.context[k] = v
await _emit(
pr.run_id,
diff --git a/services/agents/tests/test_playbook_engine_correctness.py b/services/agents/tests/test_playbook_engine_correctness.py
new file mode 100644
index 000000000..63614b16d
--- /dev/null
+++ b/services/agents/tests/test_playbook_engine_correctness.py
@@ -0,0 +1,405 @@
+"""
+Unit tests for playbook engine correctness fixes.
+
+These tests pin down behavior of the helper functions and ``PlaybookEngine.run``
+that were silently broken before ``fix/playbook-engine-correctness``:
+
+1. Expression-string conditions were silently always-true (only field/op/value
+ were evaluated).
+2. ``gt``/``lt`` crashed on non-numeric values via ``float(value or 0)``.
+3. ``contains`` couldn't express list-membership (``severity in [...]``).
+4. Context flattening used ``setdefault``, so later steps couldn't refine
+ values set earlier (e.g. enrichment overwriting a placeholder ``user_id``).
+5. ``_resolve_field`` couldn't traverse list indices (``entities.0.name``).
+
+Tests avoid any network I/O by stubbing ``_emit`` so the realtime POST never
+fires.
+"""
+
+from __future__ import annotations
+
+from typing import Any
+from unittest.mock import AsyncMock
+
+import pytest
+from app.playbook import engine as engine_mod
+from app.playbook.engine import (
+ PlaybookEngine,
+ RunStatus,
+ StepStatus,
+ _evaluate_condition,
+ _evaluate_expression,
+ _resolve_field,
+ _to_float,
+)
+from app.playbook.models import Playbook, PlaybookStep, StepCondition, StepType
+
+# ---------------------------------------------------------------------------
+# _resolve_field
+# ---------------------------------------------------------------------------
+
+
+class TestResolveField:
+ def test_returns_none_for_empty_field(self) -> None:
+ assert _resolve_field({"a": 1}, "") is None
+
+ def test_top_level_key(self) -> None:
+ assert _resolve_field({"severity": "high"}, "severity") == "high"
+
+ def test_nested_dict_path(self) -> None:
+ ctx = {"alert": {"severity": "high"}}
+ assert _resolve_field(ctx, "alert.severity") == "high"
+
+ def test_missing_key_returns_none(self) -> None:
+ assert _resolve_field({"alert": {}}, "alert.severity") is None
+
+ def test_traverses_list_index(self) -> None:
+ ctx = {"entities": [{"name": "alice"}, {"name": "bob"}]}
+ assert _resolve_field(ctx, "entities.0.name") == "alice"
+ assert _resolve_field(ctx, "entities.1.name") == "bob"
+
+ def test_negative_list_index(self) -> None:
+ ctx = {"entities": ["a", "b", "c"]}
+ assert _resolve_field(ctx, "entities.-1") == "c"
+
+ def test_out_of_range_list_index_returns_none(self) -> None:
+ ctx = {"entities": ["a"]}
+ assert _resolve_field(ctx, "entities.5") is None
+
+ def test_non_numeric_index_on_list_returns_none(self) -> None:
+ ctx = {"entities": ["a", "b"]}
+ assert _resolve_field(ctx, "entities.name") is None
+
+ def test_traversal_into_scalar_returns_none(self) -> None:
+ # Can't drill into a string with a dot-path.
+ assert _resolve_field({"name": "alice"}, "name.first") is None
+
+
+# ---------------------------------------------------------------------------
+# _to_float
+# ---------------------------------------------------------------------------
+
+
+class TestToFloat:
+ @pytest.mark.parametrize(
+ "value,expected",
+ [
+ (None, 0.0),
+ ("", 0.0),
+ (0, 0.0),
+ (42, 42.0),
+ (3.14, 3.14),
+ ("7", 7.0),
+ ("7.5", 7.5),
+ (True, 1.0),
+ (False, 0.0),
+ ],
+ )
+ def test_coerces(self, value: Any, expected: float) -> None:
+ assert _to_float(value) == expected
+
+ @pytest.mark.parametrize("value", ["abc", "1.2.3", [1, 2], {"a": 1}, object()])
+ def test_returns_none_for_non_numeric(self, value: Any) -> None:
+ assert _to_float(value) is None
+
+
+# ---------------------------------------------------------------------------
+# _evaluate_condition — structured form
+# ---------------------------------------------------------------------------
+
+
+class TestEvaluateConditionStructured:
+ def test_empty_condition_passes(self) -> None:
+ # No field, no expression → treated as "no gate".
+ assert _evaluate_condition(StepCondition(), {}) is True
+
+ def test_eq(self) -> None:
+ cond = StepCondition(field="severity", operator="eq", value="high")
+ assert _evaluate_condition(cond, {"severity": "high"}) is True
+ assert _evaluate_condition(cond, {"severity": "low"}) is False
+
+ def test_ne(self) -> None:
+ cond = StepCondition(field="severity", operator="ne", value="low")
+ assert _evaluate_condition(cond, {"severity": "high"}) is True
+ assert _evaluate_condition(cond, {"severity": "low"}) is False
+
+ def test_exists_true_for_present_value(self) -> None:
+ cond = StepCondition(field="user_id", operator="exists")
+ assert _evaluate_condition(cond, {"user_id": "u1"}) is True
+
+ def test_exists_false_for_missing_value(self) -> None:
+ cond = StepCondition(field="user_id", operator="exists")
+ assert _evaluate_condition(cond, {}) is False
+
+ def test_contains_substring(self) -> None:
+ cond = StepCondition(field="message", operator="contains", value="error")
+ assert _evaluate_condition(cond, {"message": "fatal error: boom"}) is True
+ assert _evaluate_condition(cond, {"message": "all good"}) is False
+
+ def test_contains_supports_list_membership(self) -> None:
+ # This is the SOAR-classic case: ``severity in ["high","critical"]``.
+ # Pre-fix this returned False because ``["high","critical"] in
+ # str(value)`` raised TypeError or always coerced to False.
+ cond = StepCondition(field="severity", operator="contains", value=["high", "critical"])
+ assert _evaluate_condition(cond, {"severity": "high"}) is True
+ assert _evaluate_condition(cond, {"severity": "critical"}) is True
+ assert _evaluate_condition(cond, {"severity": "low"}) is False
+
+ def test_contains_missing_field_is_false(self) -> None:
+ cond = StepCondition(field="severity", operator="contains", value=["high"])
+ assert _evaluate_condition(cond, {}) is False
+
+ def test_gt_lt_numeric(self) -> None:
+ gt = StepCondition(field="score", operator="gt", value=50)
+ lt = StepCondition(field="score", operator="lt", value=50)
+ assert _evaluate_condition(gt, {"score": 80}) is True
+ assert _evaluate_condition(gt, {"score": 20}) is False
+ assert _evaluate_condition(lt, {"score": 20}) is True
+ assert _evaluate_condition(lt, {"score": 80}) is False
+
+ def test_gt_lt_does_not_crash_on_non_numeric(self) -> None:
+ # Pre-fix: ``float("abc" or 0)`` → ValueError. Now: False.
+ cond = StepCondition(field="score", operator="gt", value=10)
+ assert _evaluate_condition(cond, {"score": "not-a-number"}) is False
+
+ cond = StepCondition(field="score", operator="lt", value="also-not-a-number")
+ assert _evaluate_condition(cond, {"score": 50}) is False
+
+ def test_gt_lt_missing_field_treated_as_zero(self) -> None:
+ # Historical "missing means zero" semantics for numeric comparisons.
+ cond = StepCondition(field="score", operator="gt", value=-1)
+ assert _evaluate_condition(cond, {}) is True
+ cond = StepCondition(field="score", operator="lt", value=1)
+ assert _evaluate_condition(cond, {}) is True
+
+ def test_unknown_operator_is_false(self) -> None:
+ # Pydantic's Literal blocks construction, so bypass with model_construct.
+ cond = StepCondition.model_construct(field="x", operator="weird", value=1)
+ assert _evaluate_condition(cond, {"x": 1}) is False
+
+
+# ---------------------------------------------------------------------------
+# _evaluate_condition — expression form (the silently-always-true regression)
+# ---------------------------------------------------------------------------
+
+
+class TestEvaluateConditionExpressionForm:
+ def test_expression_takes_precedence_over_blank_field(self) -> None:
+ # Pre-fix this branch was never taken — expression was ignored entirely
+ # and the (empty field, eq, None) form silently evaluated to True
+ # because ``None == None``.
+ cond = StepCondition(expression="severity == 'high'")
+ assert _evaluate_condition(cond, {"severity": "high"}) is True
+ assert _evaluate_condition(cond, {"severity": "low"}) is False
+
+ def test_expression_false_path_actually_fires(self) -> None:
+ cond = StepCondition(expression="score > 100")
+ assert _evaluate_condition(cond, {"score": 5}) is False
+
+
+# ---------------------------------------------------------------------------
+# _evaluate_expression
+# ---------------------------------------------------------------------------
+
+
+class TestEvaluateExpression:
+ @pytest.mark.parametrize(
+ "expr,ctx,expected",
+ [
+ ("severity == 'high'", {"severity": "high"}, True),
+ ('severity == "critical"', {"severity": "critical"}, True),
+ ("severity != 'low'", {"severity": "high"}, True),
+ ("score > 10", {"score": 50}, True),
+ ("score >= 50", {"score": 50}, True),
+ ("score <= 50", {"score": 50}, True),
+ ("score < 10", {"score": 50}, False),
+ ("severity in ['high','critical']", {"severity": "critical"}, True),
+ ("severity in ['high','critical']", {"severity": "low"}, False),
+ ("severity not in ['low','info']", {"severity": "high"}, True),
+ ("severity not in ['low','info']", {"severity": "low"}, False),
+ ],
+ )
+ def test_common_operators(self, expr: str, ctx: dict, expected: bool) -> None:
+ assert _evaluate_expression(expr, ctx) is expected
+
+ def test_is_null_sugar(self) -> None:
+ assert _evaluate_expression("user_id is null", {}) is True
+ assert _evaluate_expression("user_id is null", {"user_id": "u1"}) is False
+
+ def test_is_not_null_sugar(self) -> None:
+ assert _evaluate_expression("user_id is not null", {"user_id": "u1"}) is True
+ assert _evaluate_expression("user_id is not null", {}) is False
+
+ def test_equality_against_null_literal(self) -> None:
+ assert _evaluate_expression("user_id == null", {}) is True
+ assert _evaluate_expression("user_id != null", {"user_id": "u1"}) is True
+
+ def test_dot_path_resolution(self) -> None:
+ ctx = {"alert": {"severity": "high"}}
+ assert _evaluate_expression("alert.severity == 'high'", ctx) is True
+
+ def test_list_index_resolution(self) -> None:
+ ctx = {"entities": [{"name": "alice"}]}
+ assert _evaluate_expression("entities.0.name == 'alice'", ctx) is True
+
+ def test_numeric_literal_int_and_float(self) -> None:
+ assert _evaluate_expression("score == 42", {"score": 42}) is True
+ assert _evaluate_expression("score == 3.14", {"score": 3.14}) is True
+
+ def test_bool_literal(self) -> None:
+ assert _evaluate_expression("flag == true", {"flag": True}) is True
+ assert _evaluate_expression("flag == false", {"flag": False}) is True
+
+ def test_malformed_returns_false(self) -> None:
+ assert _evaluate_expression("this is not an expression", {}) is False
+ assert _evaluate_expression("", {}) is False
+ assert _evaluate_expression(None, {}) is False # type: ignore[arg-type]
+
+ def test_gt_on_non_numeric_returns_false(self) -> None:
+ assert _evaluate_expression("severity > 'low'", {"severity": "high"}) is False
+
+
+# ---------------------------------------------------------------------------
+# PlaybookEngine.run — context-flattening override behavior
+# ---------------------------------------------------------------------------
+
+
+def _make_playbook(steps: list[PlaybookStep]) -> Playbook:
+ return Playbook(name="test-pb", steps=steps)
+
+
[email protected](autouse=True)
+def _silence_realtime(monkeypatch: pytest.MonkeyPatch) -> None:
+ """Stub out ``_emit`` so engine.run() never tries to POST to the realtime
+ service during tests."""
+ monkeypatch.setattr(engine_mod, "_emit", AsyncMock(return_value=None))
+
+
+def patch_handlers():
+ """Helper to temporarily inject handlers into ``_HANDLERS``."""
+
+ saved: dict = {}
+
+ def _install(mapping: dict) -> None:
+ for k, v in mapping.items():
+ saved[k] = engine_mod._HANDLERS.get(k)
+ engine_mod._HANDLERS[k] = v
+
+ yield _install
+
+ for k, v in saved.items():
+ if v is None:
+ engine_mod._HANDLERS.pop(k, None)
+ else:
+ engine_mod._HANDLERS[k] = v
+
+
+class TestEngineContextFlattening:
+ @pytest.mark.asyncio
+ async def test_later_step_can_override_earlier_flattened_value(self, patch_handlers) -> None:
+ """An enrichment step should be able to refine a placeholder value set
+ by an earlier step. Pre-fix, ``setdefault`` froze the first writer."""
+
+ async def enrich_handler(step: PlaybookStep, ctx: dict, http: Any) -> dict:
+ # Second step "resolves" the user_id placeholder.
+ return {"user_id": "u-canonical-123"}
+
+ # First step writes a placeholder via its handler.
+ async def placeholder_handler(step: PlaybookStep, ctx: dict, http: Any) -> dict:
+ return {"user_id": "u-placeholder"}
+
+ patch_handlers(
+ {
+ StepType.HTTP: placeholder_handler,
+ StepType.ENRICH: enrich_handler,
+ }
+ )
+
+ pb = _make_playbook(
+ [
+ PlaybookStep(id="s1", name="placeholder", type=StepType.HTTP),
+ PlaybookStep(id="s2", name="enrich", type=StepType.ENRICH),
+ ]
+ )
+
+ run = await PlaybookEngine().run(pb, trigger_context={})
+
+ assert run.status == RunStatus.COMPLETED
+ # The second step's value wins.
+ assert run.context["user_id"] == "u-canonical-123"
+ # Namespaced keys preserve per-step history.
+ assert run.context["_step_s1"]["user_id"] == "u-placeholder"
+ assert run.context["_step_s2"]["user_id"] == "u-canonical-123"
+
+ @pytest.mark.asyncio
+ async def test_underscore_keys_are_not_auto_flattened(self, patch_handlers) -> None:
+ async def handler(step: PlaybookStep, ctx: dict, http: Any) -> dict:
+ return {"public": "yes", "_private": "no"}
+
+ patch_handlers({StepType.HTTP: handler})
+
+ pb = _make_playbook([PlaybookStep(id="s1", name="h", type=StepType.HTTP)])
+ run = await PlaybookEngine().run(pb, trigger_context={})
+
+ assert run.context["public"] == "yes"
+ assert "_private" not in run.context # bookkeeping keys stay namespaced
+ assert run.context["_step_s1"]["_private"] == "no"
+
+
+class TestEngineConditionGate:
+ """Round-trip the expression-form fix through the engine to confirm a
+ falsy expression actually skips a step (previously it silently always
+ passed)."""
+
+ @pytest.mark.asyncio
+ async def test_expression_condition_skips_step_when_false(self, patch_handlers) -> None:
+ called = {"hit": False}
+
+ async def handler(step: PlaybookStep, ctx: dict, http: Any) -> dict:
+ called["hit"] = True
+ return {"ok": True}
+
+ patch_handlers({StepType.NOTIFY: handler})
+
+ pb = _make_playbook(
+ [
+ PlaybookStep(
+ id="s1",
+ name="notify-on-critical",
+ type=StepType.NOTIFY,
+ condition=StepCondition(expression="severity == 'critical'"),
+ ),
+ ]
+ )
+
+ run = await PlaybookEngine().run(pb, trigger_context={"severity": "low"})
+
+ assert called["hit"] is False
+ assert run.step_results[0]["status"] == StepStatus.SKIPPED
+
+ @pytest.mark.asyncio
+ async def test_expression_condition_runs_step_when_true(self, patch_handlers) -> None:
+ called = {"hit": False}
+
+ async def handler(step: PlaybookStep, ctx: dict, http: Any) -> dict:
+ called["hit"] = True
+ return {"ok": True}
+
+ patch_handlers({StepType.NOTIFY: handler})
+
+ pb = _make_playbook(
+ [
+ PlaybookStep(
+ id="s1",
+ name="notify-on-critical",
+ type=StepType.NOTIFY,
+ condition=StepCondition(expression="severity == 'critical'"),
+ ),
+ ]
+ )
+
+ run = await PlaybookEngine().run(pb, trigger_context={"severity": "critical"})
+
+ assert called["hit"] is True
+ assert run.step_results[0]["status"] == StepStatus.SUCCESS