: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(workflows): save drafts while their cohorts are still calculating (#124769)
Harley Alexanderdca520fa8136
diff --git a/products/cdp/backend/presentation/hog_functions.py b/products/cdp/backend/presentation/hog_functions.pyindex 915b9106498d..d4745ab1e49b 100644--- a/products/cdp/backend/presentation/hog_functions.py+++ b/products/cdp/backend/presentation/hog_functions.py@@ -16,6 +16,7 @@ from products.cdp.backend.flag_gated_templates import (gated_template_enabled as gated_template_enabled,)from products.cdp.backend.validation import (+ COHORT_NOT_READY_CODE as COHORT_NOT_READY_CODE,build_html_wrap_design as build_html_wrap_design,generate_template_bytecode as generate_template_bytecode,validate_realtime_cohorts as validate_realtime_cohorts,diff --git a/products/cdp/backend/validation.py b/products/cdp/backend/validation.pyindex c0b8ad0de0ad..8dbabe865a62 100644--- a/products/cdp/backend/validation.py+++ b/products/cdp/backend/validation.py@@ -39,7 +39,8 @@ from products.cdp.backend.models.hog_functions.hog_function import (TYPES_WITH_JAVASCRIPT_SOURCE,TYPES_WITH_TRANSPILED_FILTERS,)-from products.cohorts.backend.models.cohort import Cohort+from products.cohorts.backend.models.cohort import Cohort, CohortType+from products.cohorts.backend.realtime_state import CohortRealtimeState, has_realtime_state, resolve_realtime_readinessfrom common.hogvm.python.stl import STLfrom common.hogvm.python.stl.bytecode import BYTECODE_STL@@ -1313,7 +1314,9 @@ class HogFunctionFiltersSerializer(serializers.Serializer):cohort_membership_supported = bool(self.context.get("cohort_membership_supported"))validated_cohort_ids: Optional[set[int]] = Noneif cohort_membership_supported:- validated_cohort_ids = self._validate_realtime_cohorts(data, team)+ validated_cohort_ids = self._validate_realtime_cohorts(+ data, team, require_ready=not self.context.get("is_draft")+ )data = compile_filters_bytecode(data,team,@@ -1328,7 +1331,7 @@ class HogFunctionFiltersSerializer(serializers.Serializer):return data- def _validate_realtime_cohorts(self, data: dict, team: Team) -> set[int]:+ def _validate_realtime_cohorts(self, data: dict, team: Team, *, require_ready: bool) -> set[int]:"""Validate every cohort these filters reference, including inside referenced Actions.Returns the validated ids so the compiler can reject any IN COHORT operand outside them.@@ -1340,13 +1343,34 @@ class HogFunctionFiltersSerializer(serializers.Serializer):for action in Action.objects.filter(id__in=action_ids, team__project_id=team.project_id, deleted=False):for step in action.steps:collected |= collect_property_cohort_ids(step.properties or [])- validate_realtime_cohorts(collected, team)+ validate_realtime_cohorts(collected, team, require_ready=require_ready)return collected-def validate_realtime_cohorts(cohort_ids: Iterable[int], team: Team) -> None:+COHORT_NOT_READY_CODE = "cohort_not_ready"+++def _cohort_not_ready_error(cohort: Cohort) -> serializers.ValidationError:+ readiness = resolve_realtime_readiness([cohort]).get(cohort.pk)+ if readiness is not None and readiness.state in (CohortRealtimeState.BUILDING, CohortRealtimeState.REBUILDING):+ percent = readiness.build.percent_complete if readiness.build is not None else None+ progress = f" ({percent}% done)" if percent is not None else ""+ return serializers.ValidationError(+ f"Cohort {cohort.pk} is still calculating{progress}. Workflows can use it once it finishes.",+ code=COHORT_NOT_READY_CODE,+ )+ return serializers.ValidationError(+ f"Cohort {cohort.pk} isn't ready for realtime evaluation. "+ f"Workflows can only use realtime cohorts that have finished calculating."+ )+++def validate_realtime_cohorts(cohort_ids: Iterable[int], team: Team, *, require_ready: bool = True) -> None:"""Reject any cohort without maintained cohort_membership rows, which would treat everyone as a non-member.+ With `require_ready=False` a realtime cohort that is still calculating passes, so a draft can reference+ it before its history finishes. A cohort that can never become ready is rejected either way.+Error messages carry the id rather than the name: they surface to callers holding only workflowscopes, and a name would hand them a cohort-metadata oracle."""@@ -1362,11 +1386,11 @@ def validate_realtime_cohorts(cohort_ids: Iterable[int], team: Team) -> None:raise serializers.ValidationError(f"Cohort {cohort_id} is a static cohort. Workflows can only use realtime cohorts.")- if not cohort.is_flag_compatible:- raise serializers.ValidationError(- f"Cohort {cohort_id} isn't ready for realtime evaluation. "- f"Workflows can only use realtime cohorts that have finished calculating."- )+ if cohort.is_flag_compatible:+ continue+ can_become_ready = cohort.cohort_type == CohortType.REALTIME and has_realtime_state(cohort)+ if require_ready or not can_become_ready:+ raise _cohort_not_ready_error(cohort)class FunctionInputsSerializer(serializers.Serializer):diff --git a/products/workflows/backend/presentation/views/hog_flow.py b/products/workflows/backend/presentation/views/hog_flow.pyindex 936369623ff1..7f1f80fa40b3 100644--- a/products/workflows/backend/presentation/views/hog_flow.py+++ b/products/workflows/backend/presentation/views/hog_flow.py@@ -94,6 +94,7 @@ from products.access_control.backend.presentation.access_control import (UserAccessControlSerializerMixin,)from products.cdp.backend.presentation.hog_functions import (+ COHORT_NOT_READY_CODE,DATA_WAREHOUSE_SOURCES,FLAG_GATED_TEMPLATE_IDS,compile_filters_expr,@@ -933,13 +934,18 @@ def _describe_action_errors(errors: list[Any], actions: list[dict]) -> str:# The many=True error list mirrors the actions list, with {} entries for valid actions. Raising it# as-is gets flattened by the exception handler to that first empty dict, so the client sees "{}".# Name each offending step instead so the error points at what to fix.- parts = []+ failures = []for action_data, error in zip(actions, errors):if not error:continuemessage = _first_error_string(error) or "has an invalid configuration"name = action_data.get("name") if isinstance(action_data, dict) else None- parts.append(f"step '{name or 'unnamed'}': {message}")+ failures.append((name or "unnamed", message))+ if failures and all(getattr(message, "code", None) == COHORT_NOT_READY_CODE for _, message in failures):+ # Nothing in the workflow needs a fix, so the message says to wait rather than to fix a step.+ waits = " ".join(f"Step '{name}': {message}" for name, message in failures)+ return f"Can't enable this workflow yet. {waits}"+ parts = [f"step '{name}': {message}" for name, message in failures]return f"Can't enable this workflow. Fix {'; '.join(parts) or 'the invalid steps'} and try again."@@ -1601,7 +1607,9 @@ class HogFlowActionSerializer(serializers.Serializer):if not unchanged and not self._cohort_conditions_enabled():raise serializers.ValidationError({"type": "Cohort triggers aren't available for this organization."})try:- validate_realtime_cohorts([cohort_id], self.context["get_team"]())+ validate_realtime_cohorts(+ [cohort_id], self.context["get_team"](), require_ready=not self.context.get("is_draft")+ )except serializers.ValidationError as error:raise serializers.ValidationError({"cohort_id": error.detail})diff --git a/products/workflows/backend/tests/api/test_hog_flow.py b/products/workflows/backend/tests/api/test_hog_flow.pyindex 94e3439e6cc9..259e3e897eb4 100644--- a/products/workflows/backend/tests/api/test_hog_flow.py+++ b/products/workflows/backend/tests/api/test_hog_flow.py@@ -39,6 +39,16 @@ from products.cdp.backend.flag_gated_templates import gated_template_enabledfrom products.cdp.backend.models.hog_functions.hog_function import HogFunctionfrom products.cdp.backend.templates.fixtures import template_slackfrom products.cdp.backend.templates.hog_function_template import sync_template_to_db+from products.cohorts.backend.models.backfill import (+ CohortBackfillChunk,+ CohortBackfillChunkStatus,+ CohortBackfillKind,+ CohortBackfillRun,+ CohortBackfillRunCohort,+ CohortBackfillRunStatus,+ CohortBackfillScope,+ CohortBackfillTrigger,+)from products.cohorts.backend.models.cohort import Cohort, CohortTypefrom products.feature_flags.backend.user_blast_radius import BlastRadiusResultfrom products.messaging.backend.facade.categories import update_category@@ -2271,6 +2281,91 @@ class TestHogFlowAPI(APIBaseTest):assert response.status_code == 400, response.json()assert expected_error in response.json()["detail"]+ def _start_cohort_history_build(self, cohort: Cohort, *, chunks_total: int, chunks_confirmed: int) -> None:+ run = CohortBackfillRun.objects.for_team(self.team.id).create(+ team_id=self.team.id,+ cohort=cohort,+ backfill_kind=CohortBackfillKind.BEHAVIORAL,+ trigger_kind=CohortBackfillTrigger.COHORT_CREATED,+ scope=CohortBackfillScope.COHORT,+ status=CohortBackfillRunStatus.SEEDING,+ timezone="UTC",+ )+ CohortBackfillRunCohort.objects.for_team(self.team.id).create(+ run=run, team_id=self.team.id, cohort=cohort, filters_shape_hash="shape", pinned_filters=cohort.filters+ )+ for index in range(chunks_total):+ CohortBackfillChunk.objects.for_team(self.team.id).create(+ run=run,+ team_id=self.team.id,+ day=timezone.now().date() - timedelta(days=index),+ status=(+ CohortBackfillChunkStatus.CONFIRMED+ if index < chunks_confirmed+ else CohortBackfillChunkStatus.PENDING+ ),+ )++ def _draft_referencing_cohort(self, reference: str, cohort_id: int) -> dict:+ if reference == "trigger":+ flow = self._cohort_trigger_flow({"cohort_id": cohort_id, "transition": "entered"})+ else:+ flow = self._hog_flow_with_condition_filters(+ "conditional_branch", {"properties": [{"key": "id", "type": "cohort", "value": cohort_id}]}+ )+ return {**flow, "status": "draft"}++ @parameterized.expand(+ [+ ("cohort_trigger", "trigger", "trigger_1"),+ ("conditional_branch", "condition", "cond_1"),+ ]+ )+ @override_settings(REALTIME_COHORT_TEAM_ALLOWLIST="all")+ @patch("products.workflows.backend.presentation.views.hog_flow.feature_enabled_or_false", return_value=True)+ def test_draft_saves_with_calculating_cohort_and_enable_waits_for_it(self, _name, reference, step_name, _mock_flag):+ cohort = self._create_behavioral_cohort(CohortType.REALTIME, backfilled=False)+ self._start_cohort_history_build(cohort, chunks_total=4, chunks_confirmed=1)++ created = self.client.post(+ f"/api/projects/{self.team.id}/hog_flows",+ self._draft_referencing_cohort(reference, cohort.id),+ HTTP_X_POSTHOG_CLIENT="mcp",+ )+ assert created.status_code == 201, created.json()++ enabled = self.client.patch(+ f"/api/projects/{self.team.id}/hog_flows/{created.json()['id']}", {"status": "active"}+ )++ assert enabled.status_code == 400, enabled.json()+ assert enabled.json()["detail"] == (+ f"Can't enable this workflow yet. Step '{step_name}': Cohort {cohort.id} is still calculating "+ "(25% done). Workflows can use it once it finishes."+ )++ @parameterized.expand(+ [+ ("non_realtime_trigger", "trigger", "non_realtime"),+ ("non_realtime_condition", "condition", "non_realtime"),+ ("off_pipeline_trigger", "trigger", "off_pipeline"),+ ]+ )+ @patch("products.workflows.backend.presentation.views.hog_flow.feature_enabled_or_false", return_value=True)+ def test_draft_rejects_cohort_that_can_never_finish(self, _name, reference, cohort_kind, _mock_flag):+ cohort_type = None if cohort_kind == "non_realtime" else CohortType.REALTIME+ cohort = self._create_behavioral_cohort(cohort_type, backfilled=False)++ with override_settings(REALTIME_COHORT_TEAM_ALLOWLIST="none" if cohort_kind == "off_pipeline" else "all"):+ response = self.client.post(+ f"/api/projects/{self.team.id}/hog_flows",+ self._draft_referencing_cohort(reference, cohort.id),+ HTTP_X_POSTHOG_CLIENT="mcp",+ )++ assert response.status_code == 400, response.json()+ assert "isn't ready for realtime evaluation" in response.json()["detail"]+def test_hog_flow_grandfathers_stored_cohort_trigger_after_flag_dial_down(self):cohort = self._create_behavioral_cohort(CohortType.REALTIME, backfilled=True)other_cohort = self._create_behavioral_cohort(CohortType.REALTIME, backfilled=True)