Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(cohorts): lock criteria edits on cohorts used by active workflows (#118447)

Harley Alexander5258abfe9f6e
diff --git a/frontend/snapshots.yml b/frontend/snapshots.yml
index 8e4c4f90cee2..58dbfeb0980e 100644
--- a/frontend/snapshots.yml
+++ b/frontend/snapshots.yml
@@ -13523,13 +13523,13 @@ snapshots:
scenes-app-people-cohorts-blocking-workflows-modal--delete-blocked--light:
hash: v1.k794b7964.6f6ceb5effdae404b9f66569db456095c2af833f861b93af32578c499d028f27.mz4x5kPGdheXrB1x9LMwRyL-jWlMZ74IBbKy8vHI3zY
scenes-app-people-cohorts-blocking-workflows-modal--edit-blocked--dark:
- hash: v1.k794b7964.4ab673c6606d909b34c56b8bfb177364d5eb7fa35d02061cd9f833f12c63cdaf.wkc81U4GRFFxj8xah2nNomuMtXvizMXjF5bSZbNDlkk
+ hash: v1.k794b7964.07eb6248f2c51f297eee9e426527f12ae2f45f29a5d47e1b7cafd074921d43ca.ayfi3N0r6LsUm6vGM8Qe7Xo4OHufFYP-rxjyuxQAR9I
scenes-app-people-cohorts-blocking-workflows-modal--edit-blocked--light:
- hash: v1.k794b7964.a20b5934f62fbbd3cefc8844040941854f8c07d7baab13cde28e99c51a77776b.fXVAOpjznHOqNSNNaHMHG0HiD5IGiDrBeEODg2cTtoc
+ hash: v1.k794b7964.0cb284f42f57491bde3b4b510285b39f13c5f467c23d54f1e2a95983156fdc14.XYUD3sIdsEf3kTLaZCo3oLbpg0rQPUZtFG1QCovXbPQ
scenes-app-people-cohorts-blocking-workflows-modal--locked-from-details--dark:
- hash: v1.k794b7964.a8256a9ab17c87980c37f839c64524986fc4faa877c0ed181ff243b881c4b8bf.nYbKjn1dpL-ZVnM4yrFPXO90qg8a2LgFPB5Fg7G9Qcg
+ hash: v1.k794b7964.93a91a7c721456ea858c7daaca50b9e0e139a6cca3d4c5561766af6bdff0d446.iOwqrGTMXmqca1y1fXUHy1yrC6IAU1MG2Bp2QwUcMF0
scenes-app-people-cohorts-blocking-workflows-modal--locked-from-details--light:
- hash: v1.k794b7964.9819971802de11467c344f7a264ee683d097adcf9365dbf0ddf7247a3ff52f72.uf3FzWXYyPG0c_v1sj4Tzz3essREBnh8ZvXL0Hh3WLs
+ hash: v1.k794b7964.a85e4e8e15e396a695ac820e7b5f12e090749678a37337d68f58c8ef2f3c84d1.mdB_yxx_jxgQdKnZ9yx0MqpZLGLVed12nt-qc_rElNs
scenes-app-people-groups--groups--dark:
hash: v1.k794b7964.43aff64914fff6a42e75760dd43d3a6a389992009b996d68255cdf1edfaa4c08.J_2LUzPOjqGdvly-10vw0UsY8tOJAv6Jt76dYvjl7Gs
scenes-app-people-groups--groups--light:
diff --git a/frontend/src/scenes/cohorts/CohortBlockingWorkflowsModal.tsx b/frontend/src/scenes/cohorts/CohortBlockingWorkflowsModal.tsx
index cc4b13c8a9cf..90dd01f02162 100644
--- a/frontend/src/scenes/cohorts/CohortBlockingWorkflowsModal.tsx
+++ b/frontend/src/scenes/cohorts/CohortBlockingWorkflowsModal.tsx
@@ -8,8 +8,8 @@ import { CohortBlockedAction, cohortBlockingWorkflowsModalLogic } from './cohort
const BLOCKING_COPY: Record<CohortBlockedAction, string> = {
delete: 'These workflows trigger on this cohort or branch on it. Remove the cohort from each workflow, or disable the workflow, then delete the cohort.',
- edit: 'This change would stop the cohort updating in real time, and these workflows need real-time updates. Remove the cohort from each workflow, or disable the workflow, then save your change.',
- view: "These workflows trigger on this cohort or branch on it, so the cohort is locked. You can't delete it or make a change that stops real-time updates. Remove the cohort from each workflow, or disable the workflow, to unlock it.",
+ edit: 'These workflows trigger on this cohort or branch on it, so its criteria are locked. Changing them would send current members through these workflows again. Remove the cohort from each workflow, or disable the workflow, then save your change.',
+ view: "These workflows trigger on this cohort or branch on it, so the cohort is locked. You can't delete it or change its criteria. Remove the cohort from each workflow, or disable the workflow, to unlock it.",
}
export function CohortBlockingWorkflowsModal(): JSX.Element {
diff --git a/posthog/api/cohort.py b/posthog/api/cohort.py
index 840c5ba4c8be..0c04c6f9c642 100644
--- a/posthog/api/cohort.py
+++ b/posthog/api/cohort.py
@@ -132,11 +132,15 @@ from products.product_analytics.backend.facade.models import Insight
from products.workflows.backend.facade.api import (
filter_hog_flow_references_by_access_level,
get_active_workflows_using_realtime_cohort,
+ team_has_active_workflows_using_cohorts,
)
from products.workflows.backend.facade.contracts import CohortDependentWorkflow, HogFlowReference
def _workflows_relying_on_cohort(cohort: Cohort) -> list[CohortDependentWorkflow]:
+ # Most teams have no active cohort workflows, so skip loading every cohort in the project for them.
+ if not team_has_active_workflows_using_cohorts(team_id=cohort.team_id):
+ return []
# A cohort that includes this one as a criterion reads its membership, so a workflow on that cohort
# also relies on this one. Load the project's cohort references in one query and walk them in memory.
dependents: defaultdict[int, set[int]] = defaultdict(set)
@@ -420,6 +424,37 @@ def _single_value_operator_value(operator: str | None, value: Any) -> Any:
return value
+_COMPUTED_FILTER_KEYS = frozenset({"bytecode", "bytecode_error", "conditionHash"})
+
+
+def _canonical_criteria(node: Any) -> Any:
+ """The criteria with server-computed keys removed and single-child groups collapsed.
+
+ The editor wraps conditions in an extra group and the server recompiles bytecode on every save,
+ so comparing raw filters would count a save that touched no criteria as a change.
+ """
+ if isinstance(node, list):
+ return [_canonical_criteria(item) for item in node]
+ if not isinstance(node, dict):
+ return node
+ canonical = {key: _canonical_criteria(value) for key, value in node.items() if key not in _COMPUTED_FILTER_KEYS}
+ values = canonical.get("values")
+ if (
+ canonical.get("type") in ("AND", "OR")
+ and isinstance(values, list)
+ and len(values) == 1
+ and isinstance(values[0], dict)
+ and values[0].get("type") in ("AND", "OR")
+ ):
+ return values[0]
+ return canonical
+
+
+def _criteria_key(filters: Any) -> str:
+ # JSON keeps 1 and 1.0 apart, which Python equality does not, and they compile to different criteria.
+ return json.dumps(_canonical_criteria(filters), sort_keys=True, default=str)
+
+
def _coerce_stored_filter_values(filters: Any) -> Any:
"""Apply the single-value unwrap to a stored filters dict, without re-running validation.
@@ -1670,8 +1705,8 @@ class CohortSerializer(SearchMatchTypeSerializerMixin, serializers.ModelSerializ
def update(self, cohort: Cohort, validated_data: dict, *args: Any, **kwargs: Any) -> Cohort: # type: ignore
request = self.context["request"]
existing_has_criteria = cohort_filters_have_values(cohort.filters)
- filters_changed = "filters" in validated_data and validated_data.get("filters") != _coerce_stored_filter_values(
- cohort.filters
+ filters_changed = "filters" in validated_data and _criteria_key(validated_data.get("filters")) != _criteria_key(
+ _coerce_stored_filter_values(cohort.filters)
)
create_in_folder = validated_data.pop("_create_in_folder", None)
@@ -1706,16 +1741,27 @@ class CohortSerializer(SearchMatchTypeSerializerMixin, serializers.ModelSerializ
# A cohort that stops being realtime stops emitting membership changes, so its workflows would
# never trigger and their branches would read frozen membership, with no error anywhere.
- if was_realtime and (cohort.is_static or cohort.cohort_type != CohortType.REALTIME):
- workflows_relying_on_realtime = _workflows_relying_on_cohort(cohort)
- if workflows_relying_on_realtime:
+ stops_realtime = was_realtime and (cohort.is_static or cohort.cohort_type != CohortType.REALTIME)
+ # A criteria edit makes the stream processor re-evaluate every person and send `entered` again for
+ # current members, so cohort-triggered workflows would run again for people who never left.
+ if stops_realtime or filters_changed:
+ workflows_relying_on_cohort = _workflows_relying_on_cohort(cohort)
+ if workflows_relying_on_cohort:
+ count = len(workflows_relying_on_cohort)
names = _workflow_names_suffix(
- request, self._user_access_control(), cohort.team_id, workflows_relying_on_realtime
+ request, self._user_access_control(), cohort.team_id, workflows_relying_on_cohort
)
+ if stops_realtime:
+ raise ValidationError(
+ f"This change stops the cohort updating in real time, and {count} "
+ f"active workflow(s) rely on that{names}. "
+ "Please remove the cohort from these workflows before making this change.",
+ code=COHORT_USED_BY_WORKFLOWS_ERROR_CODE,
+ )
raise ValidationError(
- f"This change stops the cohort updating in real time, and {len(workflows_relying_on_realtime)} "
- f"active workflow(s) rely on that{names}. "
- "Please remove the cohort from these workflows before making this change.",
+ f"This cohort is used in {count} active workflow(s){names}. Changing its criteria would send "
+ "its current members through them again. "
+ "Please remove the cohort from these workflows before changing its criteria.",
code=COHORT_USED_BY_WORKFLOWS_ERROR_CODE,
)
diff --git a/posthog/api/test/test_cohort.py b/posthog/api/test/test_cohort.py
index 968092d0a81f..f5f7a3abe3d0 100644
--- a/posthog/api/test/test_cohort.py
+++ b/posthog/api/test/test_cohort.py
@@ -7366,6 +7366,50 @@ class TestCohortTypeIntegration(APIBaseTest):
assert response.json()["code"] == "cohort_used_by_workflows"
assert Cohort.objects.get(id=cohort_id).cohort_type == CohortType.REALTIME
+ @parameterized.expand(
+ [
+ ("criteria_change", "active", {"value": "@example.com"}, 400),
+ ("criteria_change_to_an_equal_looking_number", "active", {"stored_value": 1, "value": 1.0}, 400),
+ ("criteria_change_with_draft_workflow", "draft", {"value": "@example.com"}, 200),
+ ("rename", "active", {"name": "Renamed"}, 200),
+ ("rename_resending_the_same_criteria", "active", {"name": "Renamed", "value": "@posthog.com"}, 200),
+ (
+ "rename_resending_the_same_criteria_regrouped",
+ "active",
+ {"name": "Renamed", "value": "@posthog.com", "regrouped": True},
+ 200,
+ ),
+ ]
+ )
+ def test_criteria_edit_is_blocked_by_active_workflows_using_it(self, _name, status, change, expected_status):
+ cohort_id = self._create_realtime_cohort()
+ self._create_workflow_using_cohort(cohort_id, "trigger", status)
+ if "stored_value" in change:
+ stored_filters = Cohort.objects.get(id=cohort_id).filters
+ assert stored_filters is not None
+ stored_filters["properties"]["values"][0]["value"] = change["stored_value"]
+ Cohort.objects.filter(id=cohort_id).update(filters=stored_filters)
+ payload: dict[str, Any] = {
+ key: value for key, value in change.items() if key not in ("value", "regrouped", "stored_value")
+ }
+ if "value" in change:
+ condition = {"type": "person", "key": "email", "operator": "icontains", "value": change["value"]}
+ if change.get("regrouped"):
+ condition = {"type": "AND", "values": [condition]}
+ payload["filters"] = {"properties": {"type": "AND", "values": [condition]}}
+
+ response = self.client.patch(f"/api/projects/{self.team.id}/cohorts/{cohort_id}", payload, format="json")
+
+ assert response.status_code == expected_status, response.json()
+ if expected_status == 400:
+ assert (
+ "Changing its criteria would send its current members through them again" in response.json()["detail"]
+ )
+ assert response.json()["code"] == "cohort_used_by_workflows"
+ stored_filters = Cohort.objects.get(id=cohort_id).filters
+ assert stored_filters is not None
+ assert stored_filters["properties"]["values"][0]["value"] == change.get("stored_value", "@posthog.com")
+
def test_edit_that_stops_realtime_is_blocked_by_workflows_on_a_cohort_that_includes_it(self):
included_id = self._create_realtime_cohort()
including_id = included_id
diff --git a/products/workflows/backend/facade/api.py b/products/workflows/backend/facade/api.py
index 1c681ec429c7..4840094292de 100644
--- a/products/workflows/backend/facade/api.py
+++ b/products/workflows/backend/facade/api.py
@@ -255,6 +255,16 @@ def get_active_workflows_using_realtime_cohort(
]
+def team_has_active_workflows_using_cohorts(*, team_id: int) -> bool:
+ """Cheap pre-check: False means no active workflow in the team can rely on any cohort."""
+ # Deferred for the same reason as get_active_workflows_using_realtime_cohort.
+ from products.workflows.backend.services.cohort_usage import ( # noqa: PLC0415
+ team_has_active_hog_flows_mentioning_cohorts,
+ )
+
+ return team_has_active_hog_flows_mentioning_cohorts(team_id)
+
+
def recompute_email_sending_tier(team_id: int) -> TierDecision | None:
# Deferred to keep the ClickHouse metrics client off the facade import path.
from products.workflows.backend.services.email_sending_tier import ( # noqa: PLC0415
diff --git a/products/workflows/backend/services/cohort_usage.py b/products/workflows/backend/services/cohort_usage.py
index c3ef7b73ee77..8a6136bf1949 100644
--- a/products/workflows/backend/services/cohort_usage.py
+++ b/products/workflows/backend/services/cohort_usage.py
@@ -1,7 +1,7 @@
from collections.abc import Collection, Iterator
from typing import Any
-from django.db.models import TextField
+from django.db.models import QuerySet, TextField
from django.db.models.functions import Cast
from products.cdp.backend.presentation.hog_functions import filter_cohort_ids
@@ -23,18 +23,25 @@ def _action_uses_realtime_cohort(action: Any, cohort_ids: set[int]) -> bool:
return False
-def get_active_hog_flows_using_realtime_cohort(team_id: int, cohort_ids: Collection[int]) -> Iterator[HogFlow]:
- """Active workflows in the environment that read the realtime membership of any of these cohorts, as a
- cohort trigger or in a conditional branch."""
- # Only the fields the check reads, streamed: full rows carry encrypted inputs and the whole graph.
- ids = set(cohort_ids)
+def _active_hog_flows_mentioning_cohorts(team_id: int) -> QuerySet[HogFlow]:
# Postgres skips workflows whose actions never mention a cohort, so only those graphs reach Python.
- flows = (
+ return (
HogFlow.objects.filter(team_id=team_id, status=HogFlow.State.ACTIVE)
.alias(actions_text=Cast("actions", output_field=TextField()))
.filter(actions_text__contains="cohort")
- .only("id", "name", "status", "actions")
)
+
+
+def team_has_active_hog_flows_mentioning_cohorts(team_id: int) -> bool:
+ return _active_hog_flows_mentioning_cohorts(team_id).exists()
+
+
+def get_active_hog_flows_using_realtime_cohort(team_id: int, cohort_ids: Collection[int]) -> Iterator[HogFlow]:
+ """Active workflows in the environment that read the realtime membership of any of these cohorts, as a
+ cohort trigger or in a conditional branch."""
+ # Only the fields the check reads, streamed: full rows carry encrypted inputs and the whole graph.
+ ids = set(cohort_ids)
+ flows = _active_hog_flows_mentioning_cohorts(team_id).only("id", "name", "status", "actions")
for flow in flows.iterator():
if any(_action_uses_realtime_cohort(action, ids) for action in (flow.actions or [])):
yield flow