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(workflows): configurable batch trigger audience limit (#65142)

Meikel Ratze4637d1f9cc6
diff --git a/posthog/settings/web.py b/posthog/settings/web.py
index 45f7f5a23b2d..c1a2538ff13f 100644
--- a/posthog/settings/web.py
+++ b/posthog/settings/web.py
@@ -805,6 +805,16 @@ POSTHOG_JS_UUID_VERSION = os.getenv("POSTHOG_JS_UUID_VERSION", "v7")
# Comma-separated list of team IDs that should receive the digest
HOG_FUNCTIONS_DAILY_DIGEST_TEAM_IDS = get_list(get_from_env("HOG_FUNCTIONS_DAILY_DIGEST_TEAM_IDS", ""))
+# Maximum audience size for HogFlow batch triggers. Default that applies to all teams unless they
+# opt in to the elevated value below. Only used to inform the frontend UI; no backend enforcement.
+HOGFLOW_BATCH_TRIGGER_LIMIT = int(get_from_env("HOGFLOW_BATCH_TRIGGER_LIMIT", 5000))
+# Elevated maximum audience size, returned for teams listed in HOGFLOW_BATCH_TRIGGER_ELEVATED_TEAM_IDS.
+HOGFLOW_BATCH_TRIGGER_LIMIT_ELEVATED = int(get_from_env("HOGFLOW_BATCH_TRIGGER_LIMIT_ELEVATED", 50000))
+# Comma-separated list of team IDs that get the elevated batch trigger limit instead of the default.
+HOGFLOW_BATCH_TRIGGER_ELEVATED_TEAM_IDS: set[int] = {
+ int(team_id) for team_id in get_list(get_from_env("HOGFLOW_BATCH_TRIGGER_ELEVATED_TEAM_IDS", "2"))
+}
+
# Comma-separated list of org ids allowed to receive the Error Tracking weekly digest
# "*" for all, empty to disable feature
ERROR_TRACKING_WEEKLY_DIGEST_ORG_IDS = get_list(get_from_env("ERROR_TRACKING_WEEKLY_DIGEST_ORG_IDS", ""))
diff --git a/products/workflows/backend/api/hog_flow.py b/products/workflows/backend/api/hog_flow.py
index ac820fddd1b2..25ce340fbe40 100644
--- a/products/workflows/backend/api/hog_flow.py
+++ b/products/workflows/backend/api/hog_flow.py
@@ -64,6 +64,7 @@ from products.workflows.backend.api.hog_flow_batch_job import HogFlowBatchJobSer
from products.workflows.backend.models.hog_flow.hog_flow import BILLABLE_ACTION_TYPES, HogFlow
from products.workflows.backend.models.hog_flow_batch_job import HogFlowBatchJob
from products.workflows.backend.models.hog_flow_schedule import SCHEDULED_TRIGGER_TYPES, HogFlowSchedule
+from products.workflows.backend.utils.batch_trigger_limit import get_hogflow_batch_trigger_limit
from products.workflows.backend.utils.rrule_utils import compute_next_occurrences, validate_rrule
logger = structlog.get_logger(__name__)
@@ -135,6 +136,7 @@ class BlastRadiusRequestSerializer(serializers.Serializer):
class BlastRadiusSerializer(serializers.Serializer):
affected = serializers.IntegerField(help_text="Number of users matching the filters")
total = serializers.IntegerField(help_text="Total number of users")
+ limit = serializers.IntegerField(help_text="Maximum allowed audience size for batch triggers for this team.")
class WorkflowGlobalStatsRequestSerializer(serializers.Serializer):
@@ -1341,7 +1343,15 @@ class HogFlowViewSet(TeamAndOrgViewSetMixin, LogEntryMixin, AppMetricsMixin, vie
result = get_user_blast_radius(self.team, filters, group_type_index)
- return Response(BlastRadiusSerializer(result).data)
+ return Response(
+ BlastRadiusSerializer(
+ {
+ "affected": result.affected,
+ "total": result.total,
+ "limit": get_hogflow_batch_trigger_limit(self.team_id),
+ }
+ ).data
+ )
@extend_schema(
operation_id="hog_flows_invocation_results_retrieve",
@@ -1578,7 +1588,15 @@ class InternalHogFlowViewSet(TeamAndOrgViewSetMixin, LogEntryMixin, AppMetricsMi
try:
reject_flag_conditions_in_audience(team, filters)
result = get_user_blast_radius(team, filters, group_type_index)
- return Response(BlastRadiusSerializer(result).data)
+ return Response(
+ BlastRadiusSerializer(
+ {
+ "affected": result.affected,
+ "total": result.total,
+ "limit": get_hogflow_batch_trigger_limit(team.id),
+ }
+ ).data
+ )
except exceptions.ValidationError as e:
return Response({"error": _validation_error_message(e)}, status=400)
except Exception as e:
diff --git a/products/workflows/backend/api/test/test_hog_flow.py b/products/workflows/backend/api/test/test_hog_flow.py
index 184966215104..4e2ba7a17cf9 100644
--- a/products/workflows/backend/api/test/test_hog_flow.py
+++ b/products/workflows/backend/api/test/test_hog_flow.py
@@ -1712,7 +1712,49 @@ class TestHogFlowAPI(APIBaseTest):
)
assert response.status_code == 200, response.json()
- assert response.json() == {"affected": 4, "total": 10}
+ body = response.json()
+ assert body["affected"] == 4
+ assert body["total"] == 10
+ assert "limit" in body
+ assert body["limit"] > 0
+
+ @override_settings(
+ HOGFLOW_BATCH_TRIGGER_LIMIT=5000,
+ HOGFLOW_BATCH_TRIGGER_LIMIT_ELEVATED=50000,
+ HOGFLOW_BATCH_TRIGGER_ELEVATED_TEAM_IDS=set(),
+ )
+ def test_hog_flow_user_blast_radius_returns_default_limit_for_unlisted_team(self):
+ with patch("products.workflows.backend.api.hog_flow.get_user_blast_radius") as mock_get_user_blast_radius:
+ from products.feature_flags.backend.user_blast_radius import BlastRadiusResult # noqa: PLC0415
+
+ mock_get_user_blast_radius.return_value = BlastRadiusResult(affected=0, total=0)
+ response = self.client.post(
+ f"/api/projects/{self.team.id}/hog_flows/user_blast_radius",
+ {"filters": {"properties": []}},
+ )
+
+ assert response.status_code == 200, response.json()
+ assert response.json()["limit"] == 5000
+
+ def test_hog_flow_user_blast_radius_returns_elevated_limit_for_listed_team(self):
+ with (
+ override_settings(
+ HOGFLOW_BATCH_TRIGGER_LIMIT=5000,
+ HOGFLOW_BATCH_TRIGGER_LIMIT_ELEVATED=50000,
+ HOGFLOW_BATCH_TRIGGER_ELEVATED_TEAM_IDS={self.team.id},
+ ),
+ patch("products.workflows.backend.api.hog_flow.get_user_blast_radius") as mock_get_user_blast_radius,
+ ):
+ from products.feature_flags.backend.user_blast_radius import BlastRadiusResult # noqa: PLC0415
+
+ mock_get_user_blast_radius.return_value = BlastRadiusResult(affected=0, total=0)
+ response = self.client.post(
+ f"/api/projects/{self.team.id}/hog_flows/user_blast_radius",
+ {"filters": {"properties": []}},
+ )
+
+ assert response.status_code == 200, response.json()
+ assert response.json()["limit"] == 50000
def test_user_blast_radius_personal_api_key_requires_person_read_scope(self):
# Sizing an audience queries person data, so a hog_flow:read-only token must NOT be able to use
@@ -1747,7 +1789,10 @@ class TestHogFlowAPI(APIBaseTest):
headers={"authorization": f"Bearer {key}"},
)
assert response.status_code == 200, response.json()
- assert response.json() == {"affected": 1, "total": 10}
+ body = response.json()
+ assert body["affected"] == 1
+ assert body["total"] == 10
+ assert "limit" in body
@parameterized.expand(
[
diff --git a/products/workflows/backend/utils/batch_trigger_limit.py b/products/workflows/backend/utils/batch_trigger_limit.py
new file mode 100644
index 000000000000..38febf0c2442
--- /dev/null
+++ b/products/workflows/backend/utils/batch_trigger_limit.py
@@ -0,0 +1,7 @@
+from django.conf import settings
+
+
+def get_hogflow_batch_trigger_limit(team_id: int) -> int:
+ if team_id in settings.HOGFLOW_BATCH_TRIGGER_ELEVATED_TEAM_IDS:
+ return settings.HOGFLOW_BATCH_TRIGGER_LIMIT_ELEVATED
+ return settings.HOGFLOW_BATCH_TRIGGER_LIMIT
diff --git a/products/workflows/backend/utils/test_batch_trigger_limit.py b/products/workflows/backend/utils/test_batch_trigger_limit.py
new file mode 100644
index 000000000000..6d60daf7e615
--- /dev/null
+++ b/products/workflows/backend/utils/test_batch_trigger_limit.py
@@ -0,0 +1,42 @@
+from django.test import TestCase, override_settings
+
+from products.workflows.backend.utils.batch_trigger_limit import get_hogflow_batch_trigger_limit
+
+
+class TestGetHogflowBatchTriggerLimit(TestCase):
+ @override_settings(
+ HOGFLOW_BATCH_TRIGGER_LIMIT=5000,
+ HOGFLOW_BATCH_TRIGGER_LIMIT_ELEVATED=50000,
+ HOGFLOW_BATCH_TRIGGER_ELEVATED_TEAM_IDS={2, 99},
+ )
+ def test_returns_default_for_unlisted_team(self):
+ assert get_hogflow_batch_trigger_limit(1) == 5000
+ assert get_hogflow_batch_trigger_limit(7) == 5000
+
+ @override_settings(
+ HOGFLOW_BATCH_TRIGGER_LIMIT=5000,
+ HOGFLOW_BATCH_TRIGGER_LIMIT_ELEVATED=50000,
+ HOGFLOW_BATCH_TRIGGER_ELEVATED_TEAM_IDS={2, 99},
+ )
+ def test_returns_elevated_for_listed_team(self):
+ assert get_hogflow_batch_trigger_limit(2) == 50000
+ assert get_hogflow_batch_trigger_limit(99) == 50000
+
+ @override_settings(
+ HOGFLOW_BATCH_TRIGGER_LIMIT=5000,
+ HOGFLOW_BATCH_TRIGGER_LIMIT_ELEVATED=50000,
+ HOGFLOW_BATCH_TRIGGER_ELEVATED_TEAM_IDS=set(),
+ )
+ def test_returns_default_when_elevated_set_is_empty(self):
+ assert get_hogflow_batch_trigger_limit(2) == 5000
+
+ @override_settings(
+ HOGFLOW_BATCH_TRIGGER_LIMIT=123,
+ HOGFLOW_BATCH_TRIGGER_LIMIT_ELEVATED=456,
+ HOGFLOW_BATCH_TRIGGER_ELEVATED_TEAM_IDS={42},
+ )
+ def test_returns_currently_configured_values(self):
+ # Doesn't snapshot 5000/50000 — picks up whatever the settings are at call time, so a
+ # production tweak via env var is reflected immediately.
+ assert get_hogflow_batch_trigger_limit(42) == 456
+ assert get_hogflow_batch_trigger_limit(1) == 123
diff --git a/products/workflows/frontend/Workflows/hogflows/HogFlowManualTriggerButton.tsx b/products/workflows/frontend/Workflows/hogflows/HogFlowManualTriggerButton.tsx
index 8b8181d67690..ade51b95bdd9 100644
--- a/products/workflows/frontend/Workflows/hogflows/HogFlowManualTriggerButton.tsx
+++ b/products/workflows/frontend/Workflows/hogflows/HogFlowManualTriggerButton.tsx
@@ -11,7 +11,7 @@ import { CyclotronJobInputSchemaType } from '~/types'
import { WorkflowLogicProps, workflowLogic } from '../workflowLogic'
import { hogFlowManualTriggerButtonLogic } from './HogFlowManualTriggerButtonLogic'
-import { batchTriggerLogic, BLAST_RADIUS_LIMIT } from './steps/batchTriggerLogic'
+import { batchTriggerLogic } from './steps/batchTriggerLogic'
const TriggerPopover = ({
setPopoverVisible,
@@ -32,7 +32,10 @@ const TriggerPopover = ({
)
const blastRadiusExceeded =
- workflow?.trigger?.type === 'batch' && blastRadius != null && blastRadius.affected > BLAST_RADIUS_LIMIT
+ workflow?.trigger?.type === 'batch' &&
+ blastRadius != null &&
+ blastRadius.limit != null &&
+ blastRadius.affected > blastRadius.limit
const blastRadiusSuffix = (): string => {
if (workflow?.trigger?.type === 'batch') {
@@ -104,8 +107,8 @@ const TriggerPopover = ({
status="alt"
loading={blastRadiusLoading}
disabledReason={
- blastRadiusExceeded
- ? `Batch size exceeds the limit of ${humanFriendlyNumber(BLAST_RADIUS_LIMIT)} users. Add filters to narrow your audience. This limit will be loosened in the future.`
+ blastRadiusExceeded && blastRadius?.limit != null
+ ? `Batch size exceeds the limit of ${humanFriendlyNumber(blastRadius.limit)} users. Add filters to narrow your audience. This limit will be loosened in the future.`
: undefined
}
onClick={() => {
diff --git a/products/workflows/frontend/Workflows/hogflows/steps/StepTrigger.tsx b/products/workflows/frontend/Workflows/hogflows/steps/StepTrigger.tsx
index c5de01d0c143..842e66b60891 100644
--- a/products/workflows/frontend/Workflows/hogflows/steps/StepTrigger.tsx
+++ b/products/workflows/frontend/Workflows/hogflows/steps/StepTrigger.tsx
@@ -51,7 +51,7 @@ import { workflowLogic } from '../../workflowLogic'
import { HogFlowEventFilters, WORKFLOW_OPERATOR_ALLOWLIST } from '../filters/HogFlowFilters'
import { getRegisteredTriggerTypes } from '../registry/triggers/triggerTypeRegistry'
import { HogFlowAction } from '../types'
-import { batchTriggerLogic, BLAST_RADIUS_LIMIT } from './batchTriggerLogic'
+import { batchTriggerLogic } from './batchTriggerLogic'
import { HogFlowFunctionConfiguration } from './components/HogFlowFunctionConfiguration'
import { RecurringSchedulePicker } from './components/RecurringSchedulePicker'
import { ScheduleStatusBadge } from './components/ScheduleStatusBadge'
@@ -526,19 +526,19 @@ function StepTriggerAffectedUsers({ actionId, filters }: { actionId: string; fil
return null
}
- const { affected, total } = blastRadius
+ const { affected, total, limit } = blastRadius
if (affected != null && total != null) {
- const exceeded = affected > BLAST_RADIUS_LIMIT
+ const exceeded = limit != null && affected > limit
return (
<div className="text-muted">
<div className={exceeded ? 'text-danger font-semibold' : 'text-muted'}>
approximately {humanFriendlyNumber(affected)} of {humanFriendlyNumber(total)} persons.
</div>
- {exceeded && (
+ {exceeded && limit != null && (
<div className="text-danger text-xs">
- Batch size exceeds the limit of {humanFriendlyNumber(BLAST_RADIUS_LIMIT)} users. Add filters to
- narrow your audience. This limit will be loosened in the future.
+ Batch size exceeds the limit of {humanFriendlyNumber(limit)} users. Add filters to narrow your
+ audience. This limit will be loosened in the future.
</div>
)}
</div>
diff --git a/products/workflows/frontend/Workflows/hogflows/steps/batchTriggerLogic.ts b/products/workflows/frontend/Workflows/hogflows/steps/batchTriggerLogic.ts
index 30abdeed7ee0..71d186b83eec 100644
--- a/products/workflows/frontend/Workflows/hogflows/steps/batchTriggerLogic.ts
+++ b/products/workflows/frontend/Workflows/hogflows/steps/batchTriggerLogic.ts
@@ -9,8 +9,6 @@ import { BlastRadiusApi } from 'products/workflows/frontend/generated/api.schema
import { HogFlowAction } from '../types'
import type { batchTriggerLogicType } from './batchTriggerLogicType'
-export const BLAST_RADIUS_LIMIT = 5000
-
export interface BatchTriggerLogicProps {
id?: string | 'new'
filters?: Extract<HogFlowAction['config'], { type: 'batch' }>['filters']
diff --git a/products/workflows/frontend/generated/api.schemas.ts b/products/workflows/frontend/generated/api.schemas.ts
index 9a8d7f9bbef7..7e0af5da6a0a 100644
--- a/products/workflows/frontend/generated/api.schemas.ts
+++ b/products/workflows/frontend/generated/api.schemas.ts
@@ -783,6 +783,8 @@ export interface BlastRadiusApi {
affected: number
/** Total number of users */
total: number
+ /** Maximum allowed audience size for batch triggers for this team. */
+ limit: number
}
export type HogFlowTemplatesListParams = {
diff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.ts
index 18ff9034a0e3..1ff9e117dac6 100644
--- a/services/mcp/src/api/generated.ts
+++ b/services/mcp/src/api/generated.ts
@@ -11353,6 +11353,8 @@ export namespace Schemas {
affected: number;
/** Total number of users */
total: number;
+ /** Maximum allowed audience size for batch triggers for this team. */
+ limit: number;
}
/**