: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(error-tracking): break circular import in query runners (#62883)
Julian Bez5faf9382faa8
diff --git a/.github/workflows/ci-agent-skills.yml b/.github/workflows/ci-agent-skills.ymlindex f136e3c8eb15..d5ba71ac3e5a 100644--- a/.github/workflows/ci-agent-skills.yml+++ b/.github/workflows/ci-agent-skills.yml@@ -1,14 +1,14 @@name: Agent Skills+# No paths filter on master pushes (matching the other ci-* workflows): the skills build+# imports product code whose import graph any Python change can affect, so a filtered+# master trigger lets unrelated commits break the build invisibly until the next+# skills-path commit takes the blame. PR runs stay filtered via the changes job below.on:pull_request:push:branches:- master- paths:- - 'products/*/skills/**'- - 'products/posthog_ai/scripts/**'- - '.github/workflows/ci-agent-skills.yml'concurrency:group: ${{ github.workflow }}-${{ github.head_ref || github.ref }}diff --git a/products/error_tracking/backend/api/issues.py b/products/error_tracking/backend/api/issues.pyindex d9ff3189f238..140a92186582 100644--- a/products/error_tracking/backend/api/issues.py+++ b/products/error_tracking/backend/api/issues.py@@ -24,6 +24,7 @@ from posthog.tasks.email import send_error_tracking_issue_assignedfrom products.cohorts.backend.models.cohort import Cohortfrom products.error_tracking.backend.facade import api as facade_api+from products.error_tracking.backend.issue_serializers import ErrorTrackingIssueAssignmentSerializerfrom products.error_tracking.backend.models import (ErrorTrackingIssue,ErrorTrackingIssueAssignment,@@ -33,7 +34,6 @@ from products.error_tracking.backend.models import (from products.error_tracking.backend.notifications import dispatch_issue_assigned_realtimefrom .external_references import ErrorTrackingExternalReferenceSerializer-from .utils import ErrorTrackingIssueAssignmentSerializerIssueNotFoundError = facade_api.IssueNotFoundError@@ -67,15 +67,6 @@ class ErrorTrackingIssueReadSerializer(serializers.Serializer):cohort = ErrorTrackingIssueCohortReadSerializer(allow_null=True)-class ErrorTrackingIssuePreviewSerializer(serializers.ModelSerializer):- first_seen = serializers.DateTimeField()- assignee = ErrorTrackingIssueAssignmentSerializer(source="assignment")-- class Meta:- model = ErrorTrackingIssue- fields = ["id", "status", "name", "description", "first_seen", "assignee"]--DEPRECATED_ISSUE_STATUSES = frozenset({ErrorTrackingIssue.Status.ARCHIVED, ErrorTrackingIssue.Status.PENDING_RELEASE})SUPPORTED_WRITE_STATUSES = (ErrorTrackingIssue.Status.ACTIVE,diff --git a/products/error_tracking/backend/api/utils.py b/products/error_tracking/backend/api/utils.pyindex 1fd5845caa59..04d6452517d6 100644--- a/products/error_tracking/backend/api/utils.py+++ b/products/error_tracking/backend/api/utils.py@@ -1,8 +1,7 @@from typing import Any, Protocol, TypeVarimport structlog-from drf_spectacular.utils import extend_schema_field-from rest_framework import serializers, status+from rest_framework import statusfrom rest_framework.exceptions import ValidationErrorfrom rest_framework.response import Response@@ -16,30 +15,12 @@ from posthog.api.utils import actionfrom posthog.models.team.team import Teamfrom products.error_tracking.backend.hogvm_stl import RUST_HOGVM_STL-from products.error_tracking.backend.models import ErrorTrackingIssueAssignmentfrom common.hogvm.python.operation import Operationlogger = structlog.get_logger(__name__)-class ErrorTrackingIssueAssignmentSerializer(serializers.ModelSerializer):- id = serializers.SerializerMethodField()- type = serializers.SerializerMethodField()-- class Meta:- model = ErrorTrackingIssueAssignment- fields = ["id", "type"]-- @extend_schema_field({"oneOf": [{"type": "integer"}, {"type": "string"}], "nullable": True})- def get_id(self, obj):- return obj.user_id if obj.user_id else str(obj.role_id) if obj.role_id else None-- @extend_schema_field(serializers.CharField())- def get_type(self, obj):- return "role" if obj.role else "user"--class HasGetQueryset(Protocol):def get_queryset(self): ...diff --git a/products/error_tracking/backend/hogql_queries/error_tracking_issue_correlation_query_runner.py b/products/error_tracking/backend/hogql_queries/error_tracking_issue_correlation_query_runner.pyindex 2b154e37f204..2331ae0d0d23 100644--- a/products/error_tracking/backend/hogql_queries/error_tracking_issue_correlation_query_runner.py+++ b/products/error_tracking/backend/hogql_queries/error_tracking_issue_correlation_query_runner.py@@ -20,7 +20,7 @@ from posthog.hogql.parser import parse_selectfrom posthog.hogql_queries.insights.paginators import HogQLHasMorePaginatorfrom posthog.hogql_queries.query_runner import AnalyticsQueryRunner-from products.error_tracking.backend.api.issues import ErrorTrackingIssuePreviewSerializer+from products.error_tracking.backend.issue_serializers import ErrorTrackingIssuePreviewSerializerfrom products.error_tracking.backend.models import ErrorTrackingIssuelogger = structlog.get_logger(__name__)diff --git a/products/error_tracking/backend/hogql_queries/error_tracking_query_runner_v1.py b/products/error_tracking/backend/hogql_queries/error_tracking_query_runner_v1.pyindex 64dd00583e2e..b4a9f7605c8e 100644--- a/products/error_tracking/backend/hogql_queries/error_tracking_query_runner_v1.py+++ b/products/error_tracking/backend/hogql_queries/error_tracking_query_runner_v1.py@@ -15,7 +15,6 @@ from posthog.hogql.property import property_to_exprfrom posthog.models.filters.mixins.utils import cached_propertyfrom posthog.models.property.util import property_to_django_filter-from products.error_tracking.backend.api.issues import ErrorTrackingIssuePreviewSerializerfrom products.error_tracking.backend.hogql_queries.error_tracking_query_runner_utils import (build_event_where_exprs,build_select_expressions,@@ -23,6 +22,7 @@ from products.error_tracking.backend.hogql_queries.error_tracking_query_runner_uextract_event,order_direction,)+from products.error_tracking.backend.issue_serializers import ErrorTrackingIssuePreviewSerializerfrom products.error_tracking.backend.models import ErrorTrackingIssuediff --git a/products/error_tracking/backend/issue_serializers.py b/products/error_tracking/backend/issue_serializers.pynew file mode 100644index 000000000000..2bfb56c62024--- /dev/null+++ b/products/error_tracking/backend/issue_serializers.py@@ -0,0 +1,33 @@+# These serializers live outside api/ on purpose: the hogql_queries runners need them,+# and any `api.*` import executes api/__init__.py, whose viewset imports circle back into+# the runners (api/query.py imports ErrorTrackingQueryRunner).+from drf_spectacular.utils import extend_schema_field+from rest_framework import serializers++from products.error_tracking.backend.models import ErrorTrackingIssue, ErrorTrackingIssueAssignment+++class ErrorTrackingIssueAssignmentSerializer(serializers.ModelSerializer):+ id = serializers.SerializerMethodField()+ type = serializers.SerializerMethodField()++ class Meta:+ model = ErrorTrackingIssueAssignment+ fields = ["id", "type"]++ @extend_schema_field({"oneOf": [{"type": "integer"}, {"type": "string"}], "nullable": True})+ def get_id(self, obj):+ return obj.user_id if obj.user_id else str(obj.role_id) if obj.role_id else None++ @extend_schema_field(serializers.CharField())+ def get_type(self, obj):+ return "role" if obj.role else "user"+++class ErrorTrackingIssuePreviewSerializer(serializers.ModelSerializer):+ first_seen = serializers.DateTimeField()+ assignee = ErrorTrackingIssueAssignmentSerializer(source="assignment")++ class Meta:+ model = ErrorTrackingIssue+ fields = ["id", "status", "name", "description", "first_seen", "assignee"]diff --git a/products/error_tracking/backend/test/test_import_cycles.py b/products/error_tracking/backend/test/test_import_cycles.pynew file mode 100644index 000000000000..154daf467ff9--- /dev/null+++ b/products/error_tracking/backend/test/test_import_cycles.py@@ -0,0 +1,35 @@+import sys+import subprocess+from pathlib import Path++# The product-level turbo test task runs pytest from the product directory, and+# `python -c` puts cwd on sys.path — the subprocess needs the repo root there to+# find the posthog package.+_REPO_ROOT = Path(__file__).parents[4]++# The skills build (products/posthog_ai/scripts/build_skills.py) imports the query runners+# in a cold interpreter, before anything has touched the error_tracking api package. If a+# runner imports from api.*, that executes api/__init__.py, whose viewsets import the+# runners right back (api/query.py -> ErrorTrackingQueryRunner) and the import blows up+# with "partially initialized module". Pytest cannot reproduce this in-process — by the+# time tests run, the modules are already cached — so boot a clean interpreter and import+# the runners first, like build_skills does.+_COLD_IMPORT = """+import os+os.environ.setdefault("DJANGO_SETTINGS_MODULE", "posthog.settings")+import django+django.setup()+import products.error_tracking.backend.hogql_queries.error_tracking_query_runner+import products.error_tracking.backend.hogql_queries.error_tracking_issue_correlation_query_runner+"""+++def test_query_runners_import_in_cold_interpreter() -> None:+ result = subprocess.run(+ [sys.executable, "-c", _COLD_IMPORT],+ capture_output=True,+ text=True,+ timeout=180,+ cwd=_REPO_ROOT,+ )+ assert result.returncode == 0, f"cold import of error_tracking query runners failed:\n{result.stderr[-2000:]}"