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(aio): skip clustering compute when the team is deleted (#115988)

posthog[bot]0e080fbfafe2
diff --git a/posthog/temporal/ai_observability/evaluation_clustering/activities.py b/posthog/temporal/ai_observability/evaluation_clustering/activities.py
index 418d280d9129..83ae4d305b2e 100644
--- a/posthog/temporal/ai_observability/evaluation_clustering/activities.py
+++ b/posthog/temporal/ai_observability/evaluation_clustering/activities.py
@@ -217,8 +217,6 @@ def _parse_iso(ts: str | None) -> datetime | None:
def _compute_sync(inputs: EvaluationClusteringComputeInputs) -> EvaluationClusteringComputeResult:
- team = Team.objects.get(id=inputs.team_id)
-
# Prefer the workflow-provided window so compute and metadata operate on the
# identical time slice. Fall back to activity-local time for replays / manual
# invocations that don't set it.
@@ -232,6 +230,22 @@ def _compute_sync(inputs: EvaluationClusteringComputeInputs) -> EvaluationCluste
base_run_id = f"{inputs.team_id}_evaluation_{window_end_dt.strftime('%Y%m%d_%H%M%S')}_{inputs.job_id}"
clustering_run_id = f"{base_run_id}_{inputs.run_label}" if inputs.run_label else base_run_id
+ # The coordinator can run a team after it is deleted, so skip the run instead of failing it.
+ try:
+ team = Team.objects.get(id=inputs.team_id)
+ except Team.DoesNotExist:
+ logger.info("Team not found, skipping eval clustering run", team_id=inputs.team_id, job_id=inputs.job_id)
+ return EvaluationClusteringComputeResult(
+ clustering_run_id=clustering_run_id,
+ eval_ids=[],
+ labels=[],
+ centroids=[],
+ distances=[],
+ coords_2d=[],
+ centroid_coords_2d=[],
+ skip_reason="team_not_found",
+ )
+
# Bound the random embedding sample to the same window Stage B's metadata
# query uses — otherwise the sample can include older eval ids whose linked
# generations have rolled out of METADATA_LOOKBACK, producing clusters with
diff --git a/posthog/temporal/ai_observability/evaluation_clustering/tests/test_workflow.py b/posthog/temporal/ai_observability/evaluation_clustering/tests/test_workflow.py
index c82889265c86..f430ffc979a0 100644
--- a/posthog/temporal/ai_observability/evaluation_clustering/tests/test_workflow.py
+++ b/posthog/temporal/ai_observability/evaluation_clustering/tests/test_workflow.py
@@ -11,10 +11,12 @@ from temporalio.testing import WorkflowEnvironment
from temporalio.worker import UnsandboxedWorkflowRunner, Worker
from posthog.temporal.ai_observability.evaluation_clustering.activities import (
+ EvaluationClusteringComputeInputs,
EvaluationClusteringComputeResult,
FetchEvaluationMetadataResult,
GenerateEvaluationLabelsInputs,
GenerateEvaluationLabelsOutputs,
+ _compute_sync,
)
from posthog.temporal.ai_observability.evaluation_clustering.constants import (
SAMPLER_MAX_SAMPLES_PER_JOB,
@@ -259,3 +261,11 @@ class TestClusteringWorkflowWiring:
"clustering_run_id": compute_result.clustering_run_id,
"clustering_job_id": "job-1",
}
+
+
+def test_compute_skips_deleted_team() -> None:
+ result = _compute_sync(EvaluationClusteringComputeInputs(team_id=999_999_999, job_id="job-1", job_name="job"))
+
+ assert result.skip_reason == "team_not_found"
+ assert result.eval_ids == []
diff --git a/posthog/temporal/ai_observability/trace_clustering/activities.py b/posthog/temporal/ai_observability/trace_clustering/activities.py
index fbf4c5e2b7a9..97e2fc2d8df0 100644
--- a/posthog/temporal/ai_observability/trace_clustering/activities.py
+++ b/posthog/temporal/ai_observability/trace_clustering/activities.py
@@ -74,7 +74,22 @@ def _perform_clustering_compute(inputs: ClusteringActivityInputs) -> ClusteringC
base_run_id = f"{base_run_id}_{inputs.job_id}"
clustering_run_id = f"{base_run_id}_{inputs.run_label}" if inputs.run_label else base_run_id
- team = Team.objects.get(id=inputs.team_id)
+ # The coordinator can run a team after it is deleted, so skip the run instead of failing it.
+ try:
+ team = Team.objects.get(id=inputs.team_id)
+ except Team.DoesNotExist:
+ logger.info("Team not found, skipping clustering run", team_id=inputs.team_id)
+ return ClusteringComputeResult(
+ clustering_run_id=clustering_run_id,
+ items=[],
+ labels=[],
+ centroids=[],
+ distances=[],
+ coords_2d=[],
+ centroid_coords_2d=[],
+ probabilities=[],
+ analysis_level=inputs.analysis_level,
+ )
item_ids, embeddings_map, batch_run_ids_map = fetch_item_embeddings_for_clustering(
team=team,
diff --git a/posthog/temporal/ai_observability/trace_clustering/tests/test_workflow.py b/posthog/temporal/ai_observability/trace_clustering/tests/test_workflow.py
index e61dce45c406..4cf9d5ae3cfd 100644
--- a/posthog/temporal/ai_observability/trace_clustering/tests/test_workflow.py
+++ b/posthog/temporal/ai_observability/trace_clustering/tests/test_workflow.py
@@ -461,3 +461,18 @@ class TestComputeActivityCapacityBackoff:
<= err.next_retry_delay
<= constants.CLICKHOUSE_AT_CAPACITY_BACKOFF_MAX
)
+
+
+def test_compute_skips_deleted_team() -> None:
+ from posthog.temporal.ai_observability.trace_clustering.activities import _perform_clustering_compute
+
+ result = _perform_clustering_compute(
+ ClusteringActivityInputs(
+ team_id=999_999_999,
+ window_start="2025-01-01T00:00:00Z",
+ window_end="2025-01-08T00:00:00Z",
+ )
+ )
+
+ assert result.items == []