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.

fix(signals): gate report canvas workflow creation (#85108)

Dylan Martin013921af1943
diff --git a/posthog/temporal/tests/ai/test_module_integrity.py b/posthog/temporal/tests/ai/test_module_integrity.py
index 036b4c16e4c9..3afb9551ad3e 100644
--- a/posthog/temporal/tests/ai/test_module_integrity.py
+++ b/posthog/temporal/tests/ai/test_module_integrity.py
@@ -144,6 +144,7 @@ class TestSignalsProductModuleIntegrity:
"mark_report_pending_input_activity",
"mark_report_ready_activity",
"fail_report_canvas_generation_activity",
+ "report_canvases_enabled_activity",
"start_report_canvas_generation_activity",
"poll_report_canvas_generation_activity",
"publish_report_completed_activity",
diff --git a/products/signals/backend/temporal/__init__.py b/products/signals/backend/temporal/__init__.py
index c51a69bb3fbe..abe6c0582a15 100644
--- a/products/signals/backend/temporal/__init__.py
+++ b/products/signals/backend/temporal/__init__.py
@@ -57,6 +57,7 @@ from products.signals.backend.temporal.report_canvas import (
SignalReportCanvasWorkflow,
fail_report_canvas_generation_activity,
poll_report_canvas_generation_activity,
+ report_canvases_enabled_activity,
start_report_canvas_generation_activity,
)
from products.signals.backend.temporal.report_safety_judge import report_safety_judge_activity
@@ -128,6 +129,7 @@ ACTIVITIES = [
mark_report_pending_input_activity,
mark_report_ready_activity,
fail_report_canvas_generation_activity,
+ report_canvases_enabled_activity,
start_report_canvas_generation_activity,
poll_report_canvas_generation_activity,
publish_report_completed_activity,
diff --git a/products/signals/backend/temporal/report_canvas.py b/products/signals/backend/temporal/report_canvas.py
index b9a65d462eef..a89a5a858a50 100644
--- a/products/signals/backend/temporal/report_canvas.py
+++ b/products/signals/backend/temporal/report_canvas.py
@@ -9,6 +9,7 @@ from temporalio import workflow
from temporalio.common import RetryPolicy, WorkflowIDReusePolicy
from temporalio.exceptions import WorkflowAlreadyStartedError
+from posthog.models import Team
from posthog.sync import database_sync_to_async
from posthog.temporal.common.client import async_connect
from posthog.temporal.common.scoped import scoped_temporal
@@ -19,6 +20,7 @@ from products.signals.backend.report_canvas import (
ensure_and_start_report_canvas_generation,
fail_report_canvas_generation,
finalize_report_canvas_generation,
+ report_canvases_enabled,
)
@@ -29,6 +31,17 @@ class ReportCanvasWorkflowInput:
notify_reviewers: bool = True
+@scoped_temporal()
+@close_db_connections
+async def report_canvases_enabled_activity(team_id: int) -> bool:
+ try:
+ team = await database_sync_to_async(Team.objects.get, thread_sensitive=False)(id=team_id)
+ except Team.DoesNotExist:
+ return False
+ return await database_sync_to_async(report_canvases_enabled, thread_sensitive=False)(team)
+
+
@temporalio.activity.defn
@scoped_temporal()
@close_db_connections
diff --git a/products/signals/backend/temporal/summary.py b/products/signals/backend/temporal/summary.py
index af7f2f573896..e2c9a4180393 100644
--- a/products/signals/backend/temporal/summary.py
+++ b/products/signals/backend/temporal/summary.py
@@ -48,7 +48,11 @@ from products.signals.backend.temporal.inbox_notification import (
InboxNotificationInput,
SignalReportInboxNotificationWorkflow,
)
-from products.signals.backend.temporal.report_canvas import ReportCanvasWorkflowInput, SignalReportCanvasWorkflow
+from products.signals.backend.temporal.report_canvas import (
+ ReportCanvasWorkflowInput,
+ SignalReportCanvasWorkflow,
+ report_canvases_enabled_activity,
+)
from products.signals.backend.temporal.report_safety_judge import SafetyJudgeInput, report_safety_judge_activity
from products.signals.backend.temporal.signal_queries import (
FetchSignalsForReportInput,
@@ -234,6 +238,15 @@ class SignalReportSummaryWorkflow:
if not workflow.patched("signals-report-canvases"):
return
try:
+ if workflow.patched("signals-report-canvases-parent-gate"):
+ enabled = await workflow.execute_activity(
+ report_canvases_enabled_activity,
+ inputs.team_id,
+ start_to_close_timeout=timedelta(minutes=1),
+ retry_policy=RetryPolicy(maximum_attempts=3),
+ )
+ if not enabled:
+ return
await workflow.start_child_workflow(
SignalReportCanvasWorkflow.run,
ReportCanvasWorkflowInput(team_id=inputs.team_id, report_id=inputs.report_id),
diff --git a/products/signals/backend/test/test_report_canvas_workflow.py b/products/signals/backend/test/test_report_canvas_workflow.py
index e6145b8edb64..f89400da9b5e 100644
--- a/products/signals/backend/test/test_report_canvas_workflow.py
+++ b/products/signals/backend/test/test_report_canvas_workflow.py
@@ -1,17 +1,30 @@
import uuid
import pytest
+from unittest.mock import patch
from temporalio import activity
from temporalio.testing import WorkflowEnvironment
from temporalio.worker import UnsandboxedWorkflowRunner, Worker
+from posthog.models import Team
+
from products.signals.backend.report_canvas import ReportCanvasGeneration
-from products.signals.backend.temporal.report_canvas import ReportCanvasWorkflowInput, SignalReportCanvasWorkflow
+from products.signals.backend.temporal.report_canvas import (
+ ReportCanvasWorkflowInput,
+ SignalReportCanvasWorkflow,
+ report_canvases_enabled_activity,
+)
TASK_QUEUE = "test-report-canvas"
+async def test_report_canvas_gate_treats_deleted_team_as_disabled() -> None:
+ with patch("products.signals.backend.temporal.report_canvas.Team.objects.get", side_effect=Team.DoesNotExist):
+ assert await report_canvases_enabled_activity(123) is False
+
+
@pytest.mark.asyncio
async def test_workflow_rechecks_the_fingerprint_after_generation() -> None:
generation = ReportCanvasGeneration(
diff --git a/products/signals/backend/test/test_summary_workflow.py b/products/signals/backend/test/test_summary_workflow.py
index a447d5208f02..4318cc278ea7 100644
--- a/products/signals/backend/test/test_summary_workflow.py
+++ b/products/signals/backend/test/test_summary_workflow.py
@@ -4,7 +4,7 @@ import asyncio
from datetime import UTC, datetime
import pytest
-from unittest.mock import patch
+from unittest.mock import AsyncMock, patch
import pytest_asyncio
from asgiref.sync import sync_to_async
@@ -43,6 +43,40 @@ SUMMARY_MODULE_PATH = "products.signals.backend.temporal.summary"
TASK_QUEUE = "test-summary-workflow-queue"
+async def test_disabled_report_canvases_do_not_start_child_workflow() -> None:
+ inputs = SignalReportSummaryWorkflowInputs(team_id=1, report_id=str(uuid.uuid4()))
+ execute_activity = AsyncMock(return_value=False)
+ start_child_workflow = AsyncMock()
+
+ with (
+ patch(f"{SUMMARY_MODULE_PATH}.workflow.patched", return_value=True),
+ patch(f"{SUMMARY_MODULE_PATH}.workflow.execute_activity", execute_activity),
+ patch(f"{SUMMARY_MODULE_PATH}.workflow.start_child_workflow", start_child_workflow),
+ ):
+ await SignalReportSummaryWorkflow()._start_report_canvas(inputs)
+
+ execute_activity.assert_awaited_once()
+ start_child_workflow.assert_not_awaited()
+
+
+async def test_report_canvas_gate_failure_does_not_fail_completed_report() -> None:
+ inputs = SignalReportSummaryWorkflowInputs(team_id=1, report_id=str(uuid.uuid4()))
+ execute_activity = AsyncMock(side_effect=RuntimeError("database unavailable"))
+ start_child_workflow = AsyncMock()
+
+ with (
+ patch(f"{SUMMARY_MODULE_PATH}.workflow.patched", return_value=True),
+ patch(f"{SUMMARY_MODULE_PATH}.workflow.execute_activity", execute_activity),
+ patch(f"{SUMMARY_MODULE_PATH}.workflow.start_child_workflow", start_child_workflow),
+ patch(f"{SUMMARY_MODULE_PATH}.workflow.logger"),
+ ):
+ await SignalReportSummaryWorkflow()._start_report_canvas(inputs)
+
+ start_child_workflow.assert_not_awaited()
+
+
@pytest_asyncio.fixture
async def aorganization():
organization = await sync_to_async(Organization.objects.create)(