: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): allow disabling hog functions with uncompilable filters (#70041)
Harley Alexanderf9aed8776bce
diff --git a/posthog/cdp/validation.py b/posthog/cdp/validation.pyindex 9436e63ff4f4..86163436b5d7 100644--- a/posthog/cdp/validation.py+++ b/posthog/cdp/validation.py@@ -539,8 +539,10 @@ class HogFunctionFiltersSerializer(serializers.Serializer):del data["bytecode"]else:data = compile_filters_bytecode(data, team)- # Check if bytecode compilation resulted in an error- if data.get("bytecode_error"):+ # Uncompilable filters are only fatal when the function will run (stay enabled).+ # Callers that allow saving anyway (e.g. disabling/deleting a hog function) opt out+ # via context; the error stays persisted on the filters for the UI to surface.+ if data.get("bytecode_error") and self.context.get("function_will_be_enabled", True):raise serializers.ValidationError(f"Invalid filter configuration: {data['bytecode_error']}")return datadiff --git a/products/cdp/backend/api/hog_function.py b/products/cdp/backend/api/hog_function.pyindex 1a963a51de69..6eb86db398ee 100644--- a/products/cdp/backend/api/hog_function.py+++ b/products/cdp/backend/api/hog_function.py@@ -258,6 +258,16 @@ class HogFunctionSerializer(HogFunctionMinimalSerializer):# Set some context variables that are used in the sub validatorsself.context["function_type"] = data["type"]+ # Uncompilable filters only block saves that leave the function enabled - disabling or+ # deleting must stay possible even when e.g. the team's test account filters have since+ # gained a cohort that real-time filters can't evaluate. Coerce via BooleanField so+ # form-encoded string values ("true"/"false") are read correctly, not by Python truthiness.+ to_bool = serializers.BooleanField().to_internal_value+ deleted = to_bool(data["deleted"]) if data.get("deleted") is not None else False+ enabled = (+ to_bool(data["enabled"]) if data.get("enabled") is not None else (instance.enabled if instance else False)+ )+ self.context["function_will_be_enabled"] = False if deleted else enabled# Warehouse-table sources deliver the synced row under event.properties, so input templates# may use the `{record.x}` alias — flag it so the inputs serializer rewrites it on compile.self.context["is_dwh_source"] = data["filters"].get("source") == "data-warehouse-table"diff --git a/products/cdp/backend/api/test/test_hog_function.py b/products/cdp/backend/api/test/test_hog_function.pyindex 7b1638481e56..9522f5ebf0a7 100644--- a/products/cdp/backend/api/test/test_hog_function.py+++ b/products/cdp/backend/api/test/test_hog_function.py@@ -19,6 +19,7 @@ from products.cdp.backend.api.hog_function import MAX_HOG_CODE_SIZE_BYTES, MAX_Tfrom products.cdp.backend.api.test.test_hog_function_templates import MOCK_NODE_TEMPLATESfrom products.cdp.backend.models.hog_function_template import HogFunctionTemplatefrom products.cdp.backend.models.hog_functions.hog_function import DEFAULT_STATE, HogFunction, HogFunctionState+from products.cohorts.backend.models.cohort import Cohortfrom common.hogvm.python.operation import HOGQL_BYTECODE_VERSION, Operation@@ -283,6 +284,67 @@ class TestHogFunctionAPI(ClickhouseTestMixin, APIBaseTest, QueryMatchingTest):)assert response.status_code == expected, response.json()+ @parameterized.expand(+ [+ # Filters that no longer compile (e.g. the team's test account filters gained a static+ # cohort after this function was saved) must not trap the function: disabling or deleting+ # it stays possible, while any save that leaves it enabled is still rejected.+ ("disable_allowed", True, {"enabled": False}, status.HTTP_200_OK),+ ("delete_allowed", True, {"deleted": True}, status.HTTP_200_OK),+ ("edit_while_disabled_allowed", False, {"name": "renamed"}, status.HTTP_200_OK),+ ("enable_blocked", False, {"enabled": True}, status.HTTP_400_BAD_REQUEST),+ ("edit_while_enabled_blocked", True, {"name": "renamed"}, status.HTTP_400_BAD_REQUEST),+ ]+ )+ def test_uncompilable_filters_only_block_saves_that_leave_function_enabled(+ self, _name, initial_enabled, patch, expected+ ):+ cohort = Cohort.objects.create(team=self.team, name="Test users", is_static=True)+ self.team.test_account_filters = [{"key": "id", "type": "cohort", "value": cohort.pk}]+ self.team.save()+ fn = HogFunction.objects.create(+ team=self.team,+ name="Destination with stale filters",+ type="destination",+ enabled=initial_enabled,+ inputs_schema=[],+ inputs={},+ hog="return event",+ filters={"filter_test_accounts": True},+ )+ response = self.client.patch(+ f"/api/projects/{self.team.id}/hog_functions/{fn.id}/",+ data=patch,+ )+ assert response.status_code == expected, response.json()+ if expected == status.HTTP_400_BAD_REQUEST:+ assert "static cohort" in response.json()["detail"]++ def test_uncompilable_filters_disable_with_string_boolean_value(self):+ # A client may send the boolean as a JSON string ("false"). The enable-guard reads the raw+ # value before field coercion, so it must coerce rather than rely on truthiness - otherwise+ # "false" is truthy and the disable is wrongly rejected with the filter error.+ cohort = Cohort.objects.create(team=self.team, name="Test users", is_static=True)+ self.team.test_account_filters = [{"key": "id", "type": "cohort", "value": cohort.pk}]+ self.team.save()+ fn = HogFunction.objects.create(+ team=self.team,+ name="Destination with stale filters",+ type="destination",+ enabled=True,+ inputs_schema=[],+ inputs={},+ hog="return event",+ filters={"filter_test_accounts": True},+ )+ response = self.client.patch(+ f"/api/projects/{self.team.id}/hog_functions/{fn.id}/",+ data={"enabled": "false"},+ )+ assert response.status_code == status.HTTP_200_OK, response.json()+ fn.refresh_from_db()+ assert fn.enabled is False+def test_create_hog_function(self, *args):response = self.client.post(f"/api/projects/{self.team.id}/hog_functions/",