: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(mcp-analytics): route intent clustering recompute to a live task queue (#74007)
Daniel Visca707ed68dada6
diff --git a/posthog/management/commands/start_temporal_worker.py b/posthog/management/commands/start_temporal_worker.pyindex 47be3e988c31..d0850888aaa6 100644--- a/posthog/management/commands/start_temporal_worker.py+++ b/posthog/management/commands/start_temporal_worker.py@@ -466,11 +466,10 @@ _task_queue_specs = [LLM_ANALYTICS_ACTIVITIES,),(- # Dedicated queue for MCP analytics clustering — isolates the CPU- # burst (cluster compute) and external embedding worker calls from- # the general-purpose queue that hosts the rest of mcp_analytics.- # Workflow + activity lists are populated as the stack lands; an- # empty queue is harmless — the worker registers and idles.+ # MCP analytics clustering. MCPA_TASK_QUEUE defaults to the+ # general-purpose queue (no dedicated worker deployed yet), so the+ # defaultdict merge below folds these into the general-purpose+ # registration; the env override routes them to a dedicated worker.settings.MCPA_TASK_QUEUE,MCP_ANALYTICS_INTENT_CLUSTERING_WORKFLOWS,MCP_ANALYTICS_INTENT_CLUSTERING_ACTIVITIES,diff --git a/posthog/settings/temporal.py b/posthog/settings/temporal.pyindex 619decc95f76..20b11717fb28 100644--- a/posthog/settings/temporal.py+++ b/posthog/settings/temporal.py@@ -199,7 +199,10 @@ SURFACING_SCORING_SWEEP_TASK_QUEUE = SESSION_REPLAY_TASK_QUEUEWEEKLY_DIGEST_TASK_QUEUE = _set_temporal_task_queue("weekly-digest-task-queue")LLMA_EVALS_TASK_QUEUE = _set_temporal_task_queue("llm-analytics-evals-task-queue")LLMA_TASK_QUEUE = _set_temporal_task_queue("llm-analytics-task-queue")-MCPA_TASK_QUEUE = _set_temporal_task_queue("mcp-analytics-task-queue")+# Defaults to the general-purpose fleet so dispatch always has a live worker; set the env to+# "mcp-analytics-task-queue" to route MCP analytics clustering to a dedicated, separately-scalable+# worker once one is deployed.+MCPA_TASK_QUEUE = _set_temporal_task_queue(os.getenv("MCPA_TASK_QUEUE", "general-purpose-task-queue"))ERROR_TRACKING_TASK_QUEUE = _set_temporal_task_queue("error-tracking-task-queue")ERROR_TRACKING_LIFECYCLE_TASK_QUEUE = _set_temporal_task_queue("error-tracking-lifecycle-task-queue")EVENT_SCREENSHOTS_TASK_QUEUE = _set_temporal_task_queue("event-screenshots-task-queue")diff --git a/posthog/temporal/mcp_analytics/intent_clustering/schedule.py b/posthog/temporal/mcp_analytics/intent_clustering/schedule.pyindex c9fc2d989f59..15095ef664c1 100644--- a/posthog/temporal/mcp_analytics/intent_clustering/schedule.py+++ b/posthog/temporal/mcp_analytics/intent_clustering/schedule.py@@ -2,10 +2,11 @@Gated behind the ``mcp-analytics-clustering-schedule`` PostHog feature flag.This is a kill switch: the schedule isn't registered when the flag is off,-which keeps the daily fan-out from kicking before the dedicated-``mcp-analytics-task-queue`` worker exists in production. Flipping the+so the daily fan-out only runs once explicitly enabled. Flipping theflag off after a deploy also removes the schedule on next init, so we can-disable the daily path without rolling back code.+disable the daily path without rolling back code. Dispatches to+``settings.MCPA_TASK_QUEUE`` (the general-purpose fleet unless the env+routes it to a dedicated worker)."""import asynciodiff --git a/products/mcp_analytics/backend/logic.py b/products/mcp_analytics/backend/logic.pyindex de69fe773502..1f4c66758ff3 100644--- a/products/mcp_analytics/backend/logic.py+++ b/products/mcp_analytics/backend/logic.py@@ -27,10 +27,12 @@ from products.mcp_analytics.backend.constants import MCP_MISSING_CAPABILITY_EVENfrom products.mcp_analytics.backend.facade import contracts, enumsfrom products.mcp_analytics.backend.models import MCPAnalyticsSubmission, MCPIntentClusterSnapshot, MCPSession-# How long a snapshot may sit in COMPUTING before we assume the task died and-# auto-recover. Generous because a real recompute completes in well under a-# minute even at the top_n=500 cap; anything past 10 minutes is a dead task.-STALE_COMPUTING_THRESHOLD = timedelta(minutes=10)+# How long a snapshot may sit in COMPUTING before we assume the run died and+# auto-recover. Must exceed the compute activity's schedule_to_close budget+# (400s, covering queue wait + both attempts — see intent_clustering+# constants); past that, nothing can still legitimately write a final status,+# so the row is a dead run whose worker never reached _mark_error.+STALE_COMPUTING_THRESHOLD = timedelta(minutes=8)_MCP_TOOL_CALLS_SQL = """SELECT@@ -648,9 +650,9 @@ def get_intent_cluster_snapshot(team: Team) -> contracts.IntentClusterSnapshot:Defensive side effect: any row stuck in COMPUTING pastSTALE_COMPUTING_THRESHOLD is auto-flipped to ERROR so the UI can offer- a retry. The Celery task may have died between writing COMPUTING and- writing its final status (worker restart, OOM, etc.) and otherwise has- no path back to a usable state.+ a retry. The Temporal activity may have died between writing COMPUTING+ and writing its final status (no worker on the queue, worker OOM, etc.)+ and otherwise has no path back to a usable state."""MCPIntentClusterSnapshot.objects.filter(team=team,@@ -658,7 +660,7 @@ def get_intent_cluster_snapshot(team: Team) -> contracts.IntentClusterSnapshot:updated_at__lt=timezone.now() - STALE_COMPUTING_THRESHOLD,).update(status=MCPIntentClusterSnapshot.Status.ERROR,- error_message="Recompute task did not complete within the expected window. Retry to try again.",+ error_message="Clustering didn't finish in time. Retry to start a new run.",)snapshot = MCPIntentClusterSnapshot.objects.filter(team=team).select_related("last_computed_by").first()diff --git a/products/mcp_analytics/backend/tests/test_logic.py b/products/mcp_analytics/backend/tests/test_logic.pyindex 8fd83fd7fa19..7e2b06559490 100644--- a/products/mcp_analytics/backend/tests/test_logic.py+++ b/products/mcp_analytics/backend/tests/test_logic.py@@ -138,11 +138,21 @@ class TestGetIntentClusterSnapshot(_MCPAnalyticsTeamScopedTestMixin, APIBaseTestsnapshot = logic.get_intent_cluster_snapshot(self.team)assert snapshot.status == MCPIntentClusterSnapshot.Status.ERROR- assert "did not complete" in snapshot.error_message+ assert "didn't finish" in snapshot.error_messagerow.refresh_from_db()assert row.status == MCPIntentClusterSnapshot.Status.ERROR+ def test_stale_threshold_exceeds_activity_timeout_budget(self) -> None:+ from posthog.temporal.mcp_analytics.intent_clustering.constants import COMPUTE_SCHEDULE_TO_CLOSE_TIMEOUT++ from products.mcp_analytics.backend.logic import STALE_COMPUTING_THRESHOLD++ # If the sweep threshold drops below the activity's total timeout budget+ # (queue wait + retries), the sweep flips runs to ERROR while they are+ # still legitimately queued or retrying.+ assert STALE_COMPUTING_THRESHOLD > COMPUTE_SCHEDULE_TO_CLOSE_TIMEOUT+def test_fresh_computing_snapshot_is_left_alone(self) -> None:MCPIntentClusterSnapshot.objects.create(team=self.team,