Skip to content

Self-improving AI Employees for everyone

fix(temporal): apply the trigger node's filter before spawning a run

trohitg53e1a0fd0bea
diff --git a/server/services/temporal/activities.py b/server/services/temporal/activities.py
index 825e9851..66c99101 100644
--- a/server/services/temporal/activities.py
+++ b/server/services/temporal/activities.py
@@ -528,6 +528,51 @@ async def broadcast_trigger_status_activity(payload: Dict[str, Any]) -> None:
activity.logger.warning(f"broadcast_trigger_status_activity failed for " f"node={payload.get('node_id')!r}: {exc}")
+async def evaluate_trigger_filter_activity(payload: Dict[str, Any]) -> bool:
+ """Apply a trigger node's own filter to one event.
+
+ Payload shape:
+ {
+ "node_type": str, # e.g. "webhookTrigger"
+ "filter_params": dict, # the node's saved parameters
+ "event_data": dict, # the CloudEvents ``data`` member
+ }
+
+ Why this is an activity and not inline workflow code: filter builders
+ live in plugin folders (``nodes/whatsapp/_filters.py`` and friends) and
+ reach imports that are not workflow-sandbox safe. Activities run outside
+ the sandbox, and the boolean is recorded in history so replay stays
+ deterministic.
+
+ **The predicate takes the CloudEvents ``data`` member, not the envelope.**
+ ``event_waiter.dispatch`` unpacks to ``(event_type, data)`` and hands
+ filters the inner payload, so a webhook filter reads ``data["path"]``.
+ Passing the envelope would make every filter silently reject.
+
+ Fails OPEN. An unknown node type, a malformed payload or a raising
+ builder all return True, which preserves the pre-fix behaviour of
+ spawning the run: over-firing is visible and recoverable, whereas a
+ silently dropped trigger event looks like the product is broken.
+ """
+ node_type = payload.get("node_type") or ""
+ data = payload.get("event_data")
+ if not isinstance(data, dict):
+ return True
+
+ try:
+ from services import event_waiter
+
+ predicate = event_waiter.build_filter(node_type, payload.get("filter_params") or {})
+ return bool(predicate(data))
+ except Exception as exc: # noqa: BLE001 — fail open, see docstring
+ activity.logger.warning(
+ f"evaluate_trigger_filter_activity failed for node_type={node_type!r}; "
+ f"admitting the event rather than dropping it: {exc}"
+ )
+ return True
+
+
@activity.defn(name="workflow_control.pause_on_failure")
async def pause_workflow_on_failure_activity(payload: Dict[str, Any]) -> Dict[str, Any]:
"""Circuit breaker: pause a controlled deployment after a failed run.
diff --git a/server/services/temporal/trigger_listener_workflow.py b/server/services/temporal/trigger_listener_workflow.py
index 3411b979..8035318e 100644
--- a/server/services/temporal/trigger_listener_workflow.py
+++ b/server/services/temporal/trigger_listener_workflow.py
@@ -74,6 +74,24 @@ _HISTORY_SOFT_CAP = 10_000
# many still-queued events across a rollover (oldest dropped, logged).
_MAX_CARRIED_EVENTS = 256
+# Applying the node's own filter before spawning changes the recorded
+# command sequence, so it is patch-gated: histories written before this
+# landed replay with the old "spawn on every event" behaviour. Marker
+# naming follows the one existing precedent, CONDITIONAL_EDGES_PATCH.
+NODE_FILTER_PATCH = "machina-trigger-listener-node-filter"
+
+_FILTER_ACTIVITY_NAME = "evaluate_trigger_filter_activity"
+_FILTER_ACTIVITY_TIMEOUT = timedelta(seconds=10)
+# Bounded, unlike Temporal's unlimited default: the spawn loop is
+# serialized, so a wedged filter activity would stall every later event on
+# this listener. Exhausting the attempts raises, and the caller admits the
+# event rather than dropping it.
+_FILTER_ACTIVITY_RETRY = RetryPolicy(
+ initial_interval=timedelta(seconds=1),
+ maximum_interval=timedelta(seconds=10),
+ maximum_attempts=3,
+)
+
def _history_pressure(soft_cap: int) -> bool:
"""True when the server suggests continue-as-new or the current
@@ -134,6 +152,57 @@ class TriggerListenerWorkflow:
if self._control_paused:
await workflow.wait_condition(lambda: not self._control_paused)
+ async def _event_passes_node_filter(
+ self, event: Dict[str, Any], listener_data: Dict[str, Any]
+ ) -> bool:
+ """True when the trigger node's configured filter admits this event.
+
+ Before this existed, ``filter_params`` was carried in
+ ``listener_data`` and never read, so the ``EventType`` Search
+ Attribute was the only narrowing on the deployed path. Every event
+ of the right type spawned a run: a ``webhookTrigger`` bound to
+ ``/a`` fired on a POST to ``/b`` (all webhooks share one CloudEvents
+ type and are unscoped), a ``taskTrigger`` watching one agent fired
+ on every task, and a ``whatsappReceive`` scoped to one group fired
+ on every message. The canvas-Run path applied these filters, so the
+ two paths disagreed about what the same node does.
+
+ ``listener_data["filter_params"]`` is the only available source, not
+ a shortcut: the graph snapshot carries no parameters at all
+ (``node.data`` holds just the label, and
+ ``load_persisted_workflow_graph_activity`` returns nodes/edges
+ only), so the hot graph re-read below cannot supply them. Editing a
+ deployed trigger's filter therefore takes effect on re-deploy, which
+ matches the trigger node itself: it is pre-executed with the event
+ as its output and never re-reads its parameters at run time.
+
+ An empty ``filter_params`` short-circuits without an activity: an
+ unconfigured trigger admits everything, and this is read from
+ immutable ``listener_data``, so the branch is replay-stable.
+ """
+ filter_params = listener_data.get("filter_params") or {}
+ if not filter_params:
+ return True
+
+ data = event.get("data")
+ if not isinstance(data, dict):
+ # Not the envelope shape the filters expect. Admit rather than
+ # guess -- see the fail-open contract on the activity.
+ return True
+
+ return bool(
+ await workflow.execute_activity(
+ _FILTER_ACTIVITY_NAME,
+ {
+ "node_type": listener_data.get("node_type") or "",
+ "filter_params": filter_params,
+ "event_data": data,
+ },
+ start_to_close_timeout=_FILTER_ACTIVITY_TIMEOUT,
+ retry_policy=_FILTER_ACTIVITY_RETRY,
+ )
+ )
+
@workflow.signal
async def on_event(self, event_payload: Dict[str, Any]) -> None:
"""Receive an event from ``services.events.dispatch.emit``.
@@ -252,6 +321,23 @@ class TriggerListenerWorkflow:
- After spawn returns → trigger node ``"waiting"`` (child runs
independently per ``parent_close_policy=ABANDON``).
"""
+ # The trigger node's own filter gates the spawn, and it is enforced
+ # here rather than in the caller because this method is the single
+ # choke point: WorkflowControlWorkflow._spawn_push_run constructs a
+ # TriggerListenerWorkflow purely to call it. Gating in either run
+ # loop would leave the other path unfiltered.
+ #
+ # Patch-gated: skipping a spawn changes the recorded command
+ # sequence, so pre-patch histories must keep replaying the old
+ # "spawn on every event" behaviour.
+ if workflow.patched(NODE_FILTER_PATCH):
+ if not await self._event_passes_node_filter(event, listener_data):
+ workflow.logger.debug(
+ f"Trigger filter rejected event.id={event.get('id')} for "
+ f"node={listener_data.get('trigger_node_id')}; no run spawned"
+ )
+ return
+
trigger_node_id = listener_data["trigger_node_id"]
nodes = listener_data["nodes"]
edges = listener_data["edges"]
diff --git a/server/services/temporal/worker.py b/server/services/temporal/worker.py
index 83dc9512..c8969394 100644
--- a/server/services/temporal/worker.py
+++ b/server/services/temporal/worker.py
@@ -222,6 +222,7 @@ class TemporalWorkerManager:
from services.temporal.agent_activities import collect_agent_activities
from services.temporal.activities import (
broadcast_trigger_status_activity,
+ evaluate_trigger_filter_activity,
load_persisted_workflow_graph_activity,
pause_workflow_on_failure_activity,
store_node_output_activity,
@@ -269,6 +270,7 @@ class TemporalWorkerManager:
activities=[
self._activities.execute_node_activity,
broadcast_trigger_status_activity,
+ evaluate_trigger_filter_activity,
load_persisted_workflow_graph_activity,
pause_workflow_on_failure_activity,
store_node_output_activity,
@@ -722,6 +724,7 @@ async def run_standalone_worker(
from services.temporal.activities import (
broadcast_trigger_status_activity,
+ evaluate_trigger_filter_activity,
load_persisted_workflow_graph_activity,
pause_workflow_on_failure_activity,
store_node_output_activity,
@@ -748,6 +751,7 @@ async def run_standalone_worker(
activities=[
activities.execute_node_activity,
broadcast_trigger_status_activity,
+ evaluate_trigger_filter_activity,
load_persisted_workflow_graph_activity,
pause_workflow_on_failure_activity,
store_node_output_activity,
@@ -799,6 +803,7 @@ async def create_worker(
activities = NodeExecutionActivities(session)
from services.temporal.activities import (
broadcast_trigger_status_activity,
+ evaluate_trigger_filter_activity,
load_persisted_workflow_graph_activity,
pause_workflow_on_failure_activity,
store_node_output_activity,
@@ -824,6 +829,7 @@ async def create_worker(
activities=[
activities.execute_node_activity,
broadcast_trigger_status_activity,
+ evaluate_trigger_filter_activity,
load_persisted_workflow_graph_activity,
pause_workflow_on_failure_activity,
store_node_output_activity,
diff --git a/server/tests/temporal/test_durability_hardening.py b/server/tests/temporal/test_durability_hardening.py
index 5d09a49e..2c327768 100644
--- a/server/tests/temporal/test_durability_hardening.py
+++ b/server/tests/temporal/test_durability_hardening.py
@@ -78,6 +78,10 @@ async def test_listener_child_runs_carry_no_lifetime_caps(monkeypatch):
temporal_workflow = trigger_module.workflow
monkeypatch.setattr(temporal_workflow, "logger", MagicMock())
+ # ``workflow.patched`` needs a real workflow event loop; the spawn path
+ # consults it for the node-filter gate. This listener_data carries no
+ # filter_params, so the gate short-circuits and admits.
+ monkeypatch.setattr(temporal_workflow, "patched", lambda _pid: True)
# The spawn path refreshes the graph for uncontrolled deployments;
# ``found: False`` keeps the listener's own nodes/edges in play.
monkeypatch.setattr(
diff --git a/server/tests/temporal/test_trigger_filters.py b/server/tests/temporal/test_trigger_filters.py
new file mode 100644
index 00000000..407fe583
--- /dev/null
+++ b/server/tests/temporal/test_trigger_filters.py
@@ -0,0 +1,265 @@
+"""Deployed triggers must honour the filter configured on the node.
+
+Before this landed, ``filter_params`` was carried in ``listener_data`` and
+never read. The ``EventType`` Search Attribute was the only narrowing on the
+deployed path, so every event of the right type spawned a run:
+
+ * ``webhookTrigger`` bound to ``/a`` also fired on a POST to ``/b`` --
+ all webhooks share one CloudEvents type and are unscoped.
+ * ``taskTrigger`` watching one agent fired on every task completion.
+ * ``whatsappReceive`` scoped to one group fired on every message.
+
+The canvas-Run path applied these filters, so Run and deploy disagreed
+about what the same node does.
+
+Harness note: ``temporal_workflow.patched`` and ``execute_activity`` are
+monkeypatched and the workflow methods called directly, same approach as
+test_conditional_edges.py.
+"""
+
+from __future__ import annotations
+
+import inspect
+from unittest.mock import MagicMock
+
+import pytest
+
+import nodes # noqa: F401 -- populate the plugin registry
+
+from services.temporal.activities import evaluate_trigger_filter_activity
+from services.temporal.trigger_listener_workflow import (
+ NODE_FILTER_PATCH,
+ TriggerListenerWorkflow,
+)
+
+
+pytestmark = pytest.mark.unit
+
+
+class _SpawnReached(BaseException):
+ """Raised by the stubbed activity so a test can prove the gate opened.
+
+ Deliberately a ``BaseException``: the spawn body wraps its graph lookup
+ and status broadcasts in ``except Exception`` fallbacks, which would
+ swallow an ordinary sentinel and let the test pass for the wrong reason.
+ """
+
+
+def _listener_data(**overrides):
+ data = {
+ "workflow_id": "wf-1",
+ "workflow_slug": "Demo_1",
+ "trigger_node_id": "trigger-1",
+ "trigger_label": "webhookTrigger",
+ "node_type": "webhookTrigger",
+ "event_type": "com.opencompany.webhook.received",
+ "filter_params": {"path": "hooks/a"},
+ "nodes": [{"id": "trigger-1", "type": "webhookTrigger"}],
+ "edges": [],
+ "session_id": "default",
+ }
+ data.update(overrides)
+ return data
+
+
+def _event(path="hooks/a"):
+ return {
+ "id": "evt-1",
+ "type": "com.opencompany.webhook.received",
+ "data": {"path": path, "body": {}},
+ }
+
+
+class TestFilterActivity:
+ """The activity is where the predicate actually runs.
+
+ It lives outside the workflow sandbox because filter builders are in
+ plugin folders and reach imports the sandbox forbids.
+ """
+
+ async def test_matching_event_is_admitted(self):
+ assert await evaluate_trigger_filter_activity(
+ {
+ "node_type": "webhookTrigger",
+ "filter_params": {"path": "hooks/a"},
+ "event_data": {"path": "hooks/a"},
+ }
+ )
+
+ async def test_non_matching_event_is_rejected(self):
+ """The whole point: a webhook on another path must not spawn a run."""
+ assert not await evaluate_trigger_filter_activity(
+ {
+ "node_type": "webhookTrigger",
+ "filter_params": {"path": "hooks/a"},
+ "event_data": {"path": "hooks/b"},
+ }
+ )
+
+ async def test_predicate_receives_the_data_member_not_the_envelope(self):
+ """``event_waiter.dispatch`` unpacks to ``(event_type, data)`` and
+ hands filters the inner payload. Passing the whole CloudEvents
+ envelope would make every filter silently reject, which is worse
+ than the bug being fixed."""
+ envelope = _event(path="hooks/a")
+ # The envelope has no top-level "path"; only envelope["data"] does.
+ assert "path" not in envelope
+ assert not await evaluate_trigger_filter_activity(
+ {
+ "node_type": "webhookTrigger",
+ "filter_params": {"path": "hooks/a"},
+ "event_data": envelope,
+ }
+ )
+ assert await evaluate_trigger_filter_activity(
+ {
+ "node_type": "webhookTrigger",
+ "filter_params": {"path": "hooks/a"},
+ "event_data": envelope["data"],
+ }
+ )
+
+ async def test_task_trigger_filter_is_applied(self):
+ """Not webhook-specific -- any registered builder is honoured."""
+ payload = {
+ "node_type": "taskTrigger",
+ "filter_params": {"agent_name": "alpha"},
+ }
+ assert await evaluate_trigger_filter_activity(
+ {**payload, "event_data": {"agent_name": "alpha", "status": "completed"}}
+ )
+ assert not await evaluate_trigger_filter_activity(
+ {**payload, "event_data": {"agent_name": "beta", "status": "completed"}}
+ )
+
+ async def test_unknown_node_type_admits(self):
+ """``build_filter`` defaults to accept-all for unregistered types."""
+ assert await evaluate_trigger_filter_activity(
+ {"node_type": "somethingElse", "filter_params": {"x": 1}, "event_data": {}}
+ )
+
+ async def test_non_dict_event_data_admits(self):
+ assert await evaluate_trigger_filter_activity(
+ {"node_type": "webhookTrigger", "filter_params": {"path": "a"}, "event_data": None}
+ )
+
+ async def test_a_raising_builder_fails_open(self, monkeypatch):
+ """Over-firing is visible and recoverable; a silently dropped
+ trigger event looks like the product is broken."""
+ from temporalio import activity as temporal_activity
+
+ monkeypatch.setattr(temporal_activity, "logger", MagicMock())
+
+ def _boom(_node_type, _params):
+ raise RuntimeError("builder exploded")
+
+ monkeypatch.setattr("services.event_waiter.build_filter", _boom)
+ assert await evaluate_trigger_filter_activity(
+ {
+ "node_type": "webhookTrigger",
+ "filter_params": {"path": "hooks/a"},
+ "event_data": {"path": "hooks/zzz"},
+ }
+ )
+
+
+class TestSpawnIsGatedAtTheChokePoint:
+ """``_spawn_child_run`` is where the gate belongs.
+
+ ``WorkflowControlWorkflow._spawn_push_run`` constructs a
+ ``TriggerListenerWorkflow`` purely to call this method, so gating in
+ either workflow's own run loop would leave the other path unfiltered.
+ """
+
+ def _patch_workflow(self, monkeypatch, *, patched: bool, admits: bool):
+ from temporalio import workflow as temporal_workflow
+
+ monkeypatch.setattr(temporal_workflow, "patched", lambda _id: patched)
+ monkeypatch.setattr(temporal_workflow, "logger", MagicMock())
+
+ async def _execute_activity(name, *_args, **_kwargs):
+ if name == "evaluate_trigger_filter_activity":
+ return admits
+ # Any other activity means the gate let the event through and
+ # the spawn body started.
+ raise _SpawnReached(name)
+
+ monkeypatch.setattr(temporal_workflow, "execute_activity", _execute_activity)
+
+ async def test_rejected_event_spawns_nothing(self, monkeypatch):
+ self._patch_workflow(monkeypatch, patched=True, admits=False)
+ # Returns cleanly: no graph lookup, no status broadcast, no child.
+ await TriggerListenerWorkflow()._spawn_child_run(
+ _event(path="hooks/b"), _listener_data()
+ )
+
+ async def test_admitted_event_proceeds_to_the_spawn_body(self, monkeypatch):
+ self._patch_workflow(monkeypatch, patched=True, admits=True)
+ with pytest.raises(_SpawnReached):
+ await TriggerListenerWorkflow()._spawn_child_run(
+ _event(path="hooks/a"), _listener_data()
+ )
+
+ async def test_unpatched_history_still_spawns_everything(self, monkeypatch):
+ """Replay safety: skipping a spawn changes the recorded command
+ sequence, so pre-patch histories must keep the old behaviour even
+ for an event the filter would now reject."""
+ self._patch_workflow(monkeypatch, patched=False, admits=False)
+ with pytest.raises(_SpawnReached):
+ await TriggerListenerWorkflow()._spawn_child_run(
+ _event(path="hooks/b"), _listener_data()
+ )
+
+ async def test_empty_filter_params_skips_the_activity(self, monkeypatch):
+ """An unconfigured trigger admits everything without paying for an
+ activity round trip per event."""
+ from temporalio import workflow as temporal_workflow
+
+ monkeypatch.setattr(temporal_workflow, "patched", lambda _id: True)
+ monkeypatch.setattr(temporal_workflow, "logger", MagicMock())
+
+ async def _execute_activity(name, *_args, **_kwargs):
+ if name == "evaluate_trigger_filter_activity":
+ pytest.fail("filter activity ran for an unconfigured trigger")
+ raise _SpawnReached(name)
+
+ monkeypatch.setattr(temporal_workflow, "execute_activity", _execute_activity)
+ with pytest.raises(_SpawnReached):
+ await TriggerListenerWorkflow()._spawn_child_run(
+ _event(), _listener_data(filter_params={})
+ )
+
+
+class TestBothDeployedPathsShareTheGate:
+ def test_controller_push_spawn_delegates_to_the_listener(self):
+ """Structural: if the controller ever grows its own spawn body, the
+ filter stops applying to the modern path and this fails."""
+ from services.temporal.workflow_control_workflow import WorkflowControlWorkflow
+
+ source = inspect.getsource(WorkflowControlWorkflow._spawn_push_run)
+ assert "TriggerListenerWorkflow" in source
+ assert "_spawn_child_run" in source
+
+ def test_the_gate_is_inside_spawn_child_run(self):
+ source = inspect.getsource(TriggerListenerWorkflow._spawn_child_run)
+ assert NODE_FILTER_PATCH in source or "NODE_FILTER_PATCH" in source
+ assert "_event_passes_node_filter" in source
+
+ def test_patch_marker_follows_the_established_naming(self):
+ """One standard, matching CONDITIONAL_EDGES_PATCH."""
+ from services.temporal.workflow import CONDITIONAL_EDGES_PATCH
+
+ assert CONDITIONAL_EDGES_PATCH.startswith("machina-")
+ assert NODE_FILTER_PATCH.startswith("machina-")
+
+
+class TestFilterActivityIsRegisteredOnWorkers:
+ def test_registered_everywhere_the_status_activity_is(self):
+ """A workflow calling an unregistered activity fails at run time,
+ not import time, so this is asserted structurally."""
+ import services.temporal.worker as worker_module
+
+ source = inspect.getsource(worker_module)
+ assert source.count("evaluate_trigger_filter_activity") == source.count(
+ "broadcast_trigger_status_activity"
+ )