: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
fix(cdp): drop the internal user filter link from cohort filter errors (#123306)
Harley Alexanderfa97ffe57621
diff --git a/products/cdp/backend/filters.py b/products/cdp/backend/filters.pyindex a0bd8959adab..b89931b239ed 100644--- a/products/cdp/backend/filters.py+++ b/products/cdp/backend/filters.py@@ -471,6 +471,33 @@ class SelectFinder(TraversingVisitor):return visitor.found+class _CohortMembershipFinder(TraversingVisitor):+ found = False++ def visit_compare_operation(self, node: ast.CompareOperation):+ if node.op in (ast.CompareOperationOp.InCohort, ast.CompareOperationOp.NotInCohort):+ self.found = True+ return+ super().visit_compare_operation(node)++ @classmethod+ def has_cohort_membership(cls, node: ast.Expr) -> bool:+ visitor = cls()+ visitor.visit(node)+ return visitor.found+++def _test_account_filters_read_cohorts(filters: dict, team: Team) -> bool:+ # A HogQL test account filter such as `person_id IN COHORT 5` is not a cohort-type filter, so it+ # skips the inlining in _build_test_account_filters and fails later in the bytecode compiler.+ # This runs while an error is being reported, so a failure here must not replace that error.+ try:+ team_exprs = _build_test_account_filters(filters, team)+ except Exception:+ return False+ return any(_CohortMembershipFinder.has_cohort_membership(expr) for expr in team_exprs)++def _internal_user_settings_url(team_id: int) -> str:site_url = settings.SITE_URL.rstrip("/")return f"{site_url}/project/{team_id}/settings/project-customization#internal-user-filtering"@@ -692,15 +719,20 @@ def compile_filters_bytecode(except Exception as e:error_msg = str(e)- # Cohort errors from sources other than test account filters (e.g. global- # property filters referencing a cohort) still hit the bytecode compiler's- # generic "Can't use cohorts in real-time filters" error.- if "Can't use cohorts in real-time filters" in error_msg:- settings_url = _internal_user_settings_url(team.id)+ # Cohort errors that CohortInlineError does not catch (e.g. global property filters+ # referencing a cohort, or a HogQL test account filter) still hit the bytecode+ # compiler's generic "Can't use cohorts in real-time filters" error. Only link the+ # internal user settings when the team's test account filters cause it.+ if "Can't use cohorts in real-time filters" in error_msg and _test_account_filters_read_cohorts(filters, team):+ error_msg = (+ "Your internal/test user filters use a cohort, and cohorts can't be used in real-time filters. "+ "Replace the cohort with person property filters that match the same people. "+ f"Update your filters at: {_internal_user_settings_url(team.id)}"+ )+ elif "Can't use cohorts in real-time filters" in error_msg:error_msg = (- f"Cohort membership can't be evaluated in real-time filters. "- f"Replace cohorts with equivalent inline person property filters. "- f"Update your filters at: {settings_url}"+ "Cohorts can't be used in real-time filters. "+ "Replace the cohort with person property filters that match the same people.")filters["bytecode"] = Nonediff --git a/products/cdp/backend/test/test_filters.py b/products/cdp/backend/test/test_filters.pyindex 797b399dddad..216ca721306f 100644--- a/products/cdp/backend/test/test_filters.py+++ b/products/cdp/backend/test/test_filters.py@@ -1030,7 +1030,31 @@ class TestCohortInlining(ClickhouseTestMixin, APIBaseTest, QueryMatchingTest):res = execute_bytecode(result["bytecode"], hog_globals)assert res.result is False- def test_behavioral_cohort_still_errors(self):+ @parameterized.expand(+ [+ (+ "test_account_filters",+ "Your internal/test user filters include cohorts that can't be used in real-time filters: "+ "cohort 'Behavioral cohort' (id=N) contains behavioral filters — "+ "only cohorts with exclusively person property filters can be used in real-time filters. "+ "Either switch to a cohort that only uses person properties, "+ "or replace the cohort with inline person property filters. "+ "Update your filters at: SETTINGS_URL#internal-user-filtering",+ ),+ (+ "hogql_test_account_filter",+ "Your internal/test user filters use a cohort, and cohorts can't be used in real-time filters. "+ "Replace the cohort with person property filters that match the same people. "+ "Update your filters at: SETTINGS_URL#internal-user-filtering",+ ),+ (+ "own_filters",+ "Cohorts can't be used in real-time filters. "+ "Replace the cohort with person property filters that match the same people.",+ ),+ ]+ )+ def test_behavioral_cohort_still_errors(self, cohort_source: str, expected_error: str) -> None:cohort = Cohort.objects.create(team=self.team,name="Behavioral cohort",@@ -1049,19 +1073,19 @@ class TestCohortInlining(ClickhouseTestMixin, APIBaseTest, QueryMatchingTest):},is_static=False,)- self.team.test_account_filters = [{"type": "cohort", "key": "id", "value": cohort.pk}]- self.team.save()-- result = compile_filters_bytecode({"filter_test_accounts": True}, self.team)+ cohort_filter = {"type": "cohort", "key": "id", "value": cohort.pk, "operator": "not_in"}+ if cohort_source == "own_filters":+ filters: dict = {"properties": [cohort_filter]}+ else:+ if cohort_source == "hogql_test_account_filter":+ cohort_filter = {"type": "hogql", "key": f"person_id NOT IN COHORT {cohort.pk}"}+ self.team.test_account_filters = [cohort_filter]+ self.team.save()+ filters = {"filter_test_accounts": True}++ result = compile_filters_bytecode(filters, self.team)assert result["bytecode"] is None- assert _normalize_error(result["bytecode_error"]) == (- "Your internal/test user filters include cohorts that can't be used in real-time filters: "- "cohort 'Behavioral cohort' (id=N) contains behavioral filters — "- "only cohorts with exclusively person property filters can be used in real-time filters. "- "Either switch to a cohort that only uses person properties, "- "or replace the cohort with inline person property filters. "- "Update your filters at: SETTINGS_URL#internal-user-filtering"- )+ assert _normalize_error(result["bytecode_error"]) == expected_errordef test_nested_cohort_reference_still_errors(self):inner_cohort = Cohort.objects.create(diff --git a/products/workflows/backend/tests/api/test_hog_flow.py b/products/workflows/backend/tests/api/test_hog_flow.pyindex ac2a6461fc3e..9050fd453f71 100644--- a/products/workflows/backend/tests/api/test_hog_flow.py+++ b/products/workflows/backend/tests/api/test_hog_flow.py@@ -2095,7 +2095,7 @@ class TestHogFlowAPI(APIBaseTest):response = self.client.post(f"/api/projects/{self.team.id}/hog_flows", hog_flow)assert response.status_code == 400, response.json()- assert "Cohort membership can't be evaluated in real-time filters" in response.json()["detail"]+ assert "Cohorts can't be used in real-time filters" in response.json()["detail"]def test_hog_flow_grandfathers_stored_cohort_conditions_after_flag_dial_down(self):cohort = self._create_behavioral_cohort(CohortType.REALTIME, backfilled=True)@@ -2129,7 +2129,7 @@ class TestHogFlowAPI(APIBaseTest):{"actions": edited_actions},)assert new_adoption.status_code == 400, new_adoption.json()- assert "Cohort membership can't be evaluated" in new_adoption.json()["detail"]+ assert "Cohorts can't be used in real-time filters" in new_adoption.json()["detail"]def _cohort_trigger_flow(self, config: dict) -> dict:return {@@ -2368,7 +2368,7 @@ class TestHogFlowAPI(APIBaseTest):response = self.client.post(f"/api/projects/{self.team.id}/hog_flows", hog_flow)assert response.status_code == 400, response.json()- assert "Cohort membership can't be evaluated in real-time filters" in response.json()["detail"]+ assert "Cohorts can't be used in real-time filters" in response.json()["detail"]def test_hog_flow_wait_until_condition_defaults_missing_condition(self):# An events-only wait omits 'condition'; the FE always seeds it and StepWaitUntilCondition