: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): sample busy triggers and wait for events in ideas (#121124)
Daniel Marchuk1684e309c578
diff --git a/products/workflows/backend/services/data_suggestions/README.md b/products/workflows/backend/services/data_suggestions/README.mdindex 4f9680568669..3a0a977adb55 100644--- a/products/workflows/backend/services/data_suggestions/README.md+++ b/products/workflows/backend/services/data_suggestions/README.md@@ -30,6 +30,7 @@ Building a draft (`_plan_and_build` in `service.py`):1. Claude Opus plans a graph of up to 20 steps from the card, the step catalog, saved email templates and connected channels.2. The builder turns the plan into a workflow and rejects plans that do not hold up.+ A trigger with more than 50,000 events a week starts on a random sample that brings it down to about that level.3. The draft is checked the way the caller's own create request checks it.## Premade ideasdiff --git a/products/workflows/backend/services/data_suggestions/builder.py b/products/workflows/backend/services/data_suggestions/builder.pyindex 5b42d25a0156..40ca8a0b8f33 100644--- a/products/workflows/backend/services/data_suggestions/builder.py+++ b/products/workflows/backend/services/data_suggestions/builder.py@@ -24,6 +24,10 @@ _TEMPLATE_TAGS = re.compile(r"\{\{.*?\}\}|\{%.*?%\}|\{[^{}]*\}", re.DOTALL)_EXIT_ID = "exit_node"_DEFAULT_MAX_WAIT = "3d"_REPEAT_ENTRY_SECONDS = 7 * 24 * 60 * 60+# Above this many trigger events a week, a first send would hit the team's email limits and its sender reputation,+# so the draft starts on a random sample that brings the volume down to about this level.+_SAMPLE_ABOVE_WEEKLY_EVENTS = 50_000+_SAMPLE_ID = "action_sample"@frozen@@ -43,6 +47,7 @@ class BuildContext:slack_integration_id: int | Noneonce_per_person: bool = Falseevent_properties: frozenset[str] = frozenset()+ weekly_count: int = 0@frozen@@ -99,6 +104,14 @@ def build_workflow(idea: WorkflowIdea, plan: PlannedWorkflow, context: BuildCont{"id": "trigger_node", "name": "Trigger", "description": "", "type": "trigger", "config": trigger_config}]edges: list[dict[str, Any]] = [{"from": "trigger_node", "to": action_ids[plan.first_step], "type": "continue"}]+ if context.weekly_count > _SAMPLE_ABOVE_WEEKLY_EVENTS:+ actions.append(_sample_action(context.weekly_count))+ edges = [+ {"from": "trigger_node", "to": _SAMPLE_ID, "type": "continue"},+ {"from": _SAMPLE_ID, "to": action_ids[plan.first_step], "type": "branch", "index": 0},+ {"from": _SAMPLE_ID, "to": _EXIT_ID, "type": "branch", "index": 1},+ {"from": _SAMPLE_ID, "to": _EXIT_ID, "type": "continue"},+ ]uses_saved_template = Falsefor step in steps:action = _build_step(step, action_ids[step.id], context)@@ -142,6 +155,22 @@ def build_workflow(idea: WorkflowIdea, plan: PlannedWorkflow, context: BuildCont)+def _sample_action(weekly_count: int) -> dict[str, Any]:+ share = max(1, _SAMPLE_ABOVE_WEEKLY_EVENTS * 100 // weekly_count)+ return {+ "id": _SAMPLE_ID,+ "name": "Start with a sample",+ "description": f"Sends to {share}% of people first. Raise it once the first emails land well.",+ "type": "random_cohort_branch",+ "config": {+ "cohorts": [+ {"percentage": share, "name": "Gets the workflow"},+ {"percentage": 100 - share, "name": "Skipped for now"},+ ]+ },+ }++def _log_rejection(reason: str, **details: Any) -> None:# A rejected plan turns into a 422 for the user, so the reason is only visible here.logger.info("workflows.data_suggestions.plan_rejected", reason=reason, **details)diff --git a/products/workflows/backend/services/data_suggestions/planner.py b/products/workflows/backend/services/data_suggestions/planner.pyindex 00d7b5f6687a..96c3cef1a962 100644--- a/products/workflows/backend/services/data_suggestions/planner.py+++ b/products/workflows/backend/services/data_suggestions/planner.py@@ -169,7 +169,7 @@ Step types:- delay: wait a fixed time.- wait_until_time_window: wait for a time of day or day of the week the team picks.- wait_until_event: wait until the person does one of the project's events, at most duration.-- conditional_branch: split on conditions the team defines, such as a person property.+- conditional_branch: split on person properties the team defines. It cannot check what the person did.- random_split: split people at random into paths, such as for an A/B test.- exit: end the workflow early with a reason.@@ -181,6 +181,8 @@ recipients. The team fills them in. Say what is left in setup_note.successful payment after a failed one, or an order after an abandoned cart. Use it only when an event in the list \clearly means that. Another event of the same kind is not enough: a paid invoice from the person's own customer is not \their subscription payment. When no event means it, use a delay instead.+- To check whether the person did something, like started a chat or placed an order, wait for that event with a \+wait_until_event step. Never use a delay followed by a conditional_branch for it, which the team would have to set up.- {{personalization_rule}}- Never promise a discount, offer or gift. The team adds one if it has one.- {{email_rule}}diff --git a/products/workflows/backend/services/data_suggestions/service.py b/products/workflows/backend/services/data_suggestions/service.pyindex 55b1ec9f4a2b..943672891178 100644--- a/products/workflows/backend/services/data_suggestions/service.py+++ b/products/workflows/backend/services/data_suggestions/service.py@@ -416,6 +416,7 @@ def _plan_and_build(slack_integration_id=slack_integration_id,once_per_person=suggestion.stage in _ONCE_PER_PERSON_STAGES,event_properties=frozenset(event_properties),+ weekly_count=suggestion.weekly_count,)built = None# The model now and then links a step to an id it never wrote. A second plan almost always holds up.diff --git a/products/workflows/backend/test/test_data_suggestions.py b/products/workflows/backend/test/test_data_suggestions.pyindex 000569fa2a00..cd5190e2c340 100644--- a/products/workflows/backend/test/test_data_suggestions.py+++ b/products/workflows/backend/test/test_data_suggestions.py@@ -270,6 +270,24 @@ class TestDataSuggestions(SimpleTestCase):("action_1_delay", "action_2_email", "continue", None),]+ @parameterized.expand([("quiet trigger", 50_000, None), ("busy trigger", 1_000_000, 5), ("flood", 9_000_000, 1)])+ def test_build_workflow_starts_a_busy_trigger_on_a_sample(+ self, _name: str, weekly_count: int, share: int | None+ ) -> None:+ built = build_workflow(IDEA, plan(step("welcome")), context(weekly_count=weekly_count))+ assert built is not None+ if share is None:+ assert edges(built)[0] == ("trigger_node", "action_0_email", "continue", None)+ return+ assert edges(built)[:4] == [+ ("trigger_node", "action_sample", "continue", None),+ ("action_sample", "action_0_email", "branch", 0),+ ("action_sample", "exit_node", "branch", 1),+ ("action_sample", "exit_node", "continue", None),+ ]+ sample = next(action for action in built.workflow["actions"] if action["id"] == "action_sample")+ assert [cohort["percentage"] for cohort in sample["config"]["cohorts"]] == [share, 100 - share]+@parameterized.expand([(