: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(subscriptions): only freeze report plans with zero failed steps (#71837)
Vasco de Krijgerf0958d9cf2ce
diff --git a/products/exports/backend/temporal/subscriptions/ai_subscription/report_pipeline.py b/products/exports/backend/temporal/subscriptions/ai_subscription/report_pipeline.pyindex 99843107aab9..32c44c922191 100644--- a/products/exports/backend/temporal/subscriptions/ai_subscription/report_pipeline.py+++ b/products/exports/backend/temporal/subscriptions/ai_subscription/report_pipeline.py@@ -255,11 +255,22 @@ def _plan_to_freeze(trace_correlation_id: Optional[Union[int, str]],) -> Optional[dict]:# Steps already carry their final HogQL by this point — see the write-back in `run_step`.- # Never freeze a plan the next delivery is better off re-planning: an all-failed plan would replay- # broken HogQL forever, and a step without any window placeholder would scan unbounded every run.+ # Never freeze a plan the next delivery is better off re-planning: a plan with any failed step would+ # replay that broken HogQL every run, and a step without any window placeholder would scan unbounded+ # every run.if not freshly_planned:return None- if total_steps and failed_count >= total_steps:+ # Freeze only when every step succeeded. If any step failed, re-plan next run instead — a frozen plan+ # replays verbatim until the plan version bumps, so even a single broken step would re-send broken+ # HogQL every delivery, whereas re-planning gives the planner and fix loop another shot (and lets the+ # subscription pick up any planner/prompt improvements we've since shipped).+ if failed_count:+ logger.warning(+ "ai_report.plan_had_failures_not_frozen",+ trace_correlation_id=trace_correlation_id,+ failed_count=failed_count,+ total_steps=total_steps,+ )return Noneif not all(any(token in step.hogql for token in WINDOW_PLACEHOLDERS) for step in plan.steps):logger.warning(diff --git a/products/exports/backend/temporal/subscriptions/ai_subscription/test/test_report_pipeline.py b/products/exports/backend/temporal/subscriptions/ai_subscription/test/test_report_pipeline.pyindex dd1aa0e924cf..b20aca182a7a 100644--- a/products/exports/backend/temporal/subscriptions/ai_subscription/test/test_report_pipeline.py+++ b/products/exports/backend/temporal/subscriptions/ai_subscription/test/test_report_pipeline.py@@ -13,6 +13,7 @@ from products.exports.backend.temporal.subscriptions.ai_subscription.report_pipeQueryStepDiagnostic,_all_queries_failed_notice,_arequest_hogql_fix,+ _plan_to_freeze,_run_steps,_safe_error_message,generate_ai_report,@@ -461,6 +462,41 @@ async def test_unfrozen_run_returns_plan_to_persist(}+ "total_steps,failed_count,should_freeze",+ [+ (12, 0, True), # all succeeded+ (12, 1, False), # a single step failed — re-plan rather than replay one broken query every run+ (12, 6, False), # half failed+ (12, 12, False), # all failed+ (1, 1, False), # the single step failed+ ],+)+def test_plan_to_freeze_requires_no_failures(total_steps: int, failed_count: int, should_freeze: bool) -> None:+ # A frozen plan replays verbatim until AI_QUERY_PLAN_VERSION bumps, so a plan with ANY failed step must+ # NOT be frozen — otherwise a subscription whose generation was partly broken keeps re-sending the+ # broken queries instead of re-planning. Guards against the freeze bar loosening back to allowing+ # partially-failed plans.+ plan = QueryPlan(+ overall_intent="i",+ steps=[+ QueryPlanStep(description=f"s{n}", hogql="SELECT count() FROM events WHERE {{date_range}}")+ for n in range(total_steps)+ ],+ )+ result = _plan_to_freeze(+ plan,+ freshly_planned=True,+ failed_count=failed_count,+ total_steps=total_steps,+ trace_correlation_id=None,+ )+ if should_freeze:+ assert result == {"version": AI_QUERY_PLAN_VERSION, "plan": plan.model_dump()}+ else:+ assert result is None++@pytest.mark.parametrize("fixed_hogql,expected_frozen_hogql",[