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.

feat(error-tracking): add the auto-assignment pipeline with a history processor (#128304)

James Gillcf451236f9e1
diff --git a/products/error_tracking/backend/logic/auto_assignment/__init__.py b/products/error_tracking/backend/logic/auto_assignment/__init__.py
new file mode 100644
index 000000000000..e69de29bb2d1
diff --git a/products/error_tracking/backend/logic/auto_assignment/base.py b/products/error_tracking/backend/logic/auto_assignment/base.py
new file mode 100644
index 000000000000..fa90e0d45d2e
--- /dev/null
+++ b/products/error_tracking/backend/logic/auto_assignment/base.py
@@ -0,0 +1,50 @@
+from abc import ABC, abstractmethod
+from typing import TYPE_CHECKING
+
+from posthog.dataclasses import frozen
+
+from products.error_tracking.backend.logic.auto_assignment.gate import AutoAssignmentConfig
+from products.error_tracking.backend.logic.issue_changes import AssigneeRef
+
+if TYPE_CHECKING:
+ from products.error_tracking.backend.logic.auto_assignment.evidence import IssueEvidence
+
+
+@frozen
+class HistoryEvidence:
+ similar_issue_ids: tuple[str, ...]
+ nearest_distance: float
+
+
+type CandidateEvidence = HistoryEvidence
+
+
+@frozen
+class Candidate:
+ assignee: AssigneeRef
+ confidence: float
+ source: str
+ reason: str
+ evidence: CandidateEvidence
+
+
+@frozen
+class Decision:
+ candidate: Candidate
+ threshold: float
+
+
+class Processor(ABC):
+ name: str
+ # A processor that needs a model to see the issue runs only for organizations that approved AI data processing.
+ sends_data_to_model: bool = False
+
+ @abstractmethod
+ def threshold(self, config: AutoAssignmentConfig) -> float: ...
+
+ @abstractmethod
+ def propose(self, evidence: "IssueEvidence", config: AutoAssignmentConfig) -> Candidate | None:
+ """The best assignee this signal can name, or None when it has no evidence.
+
+ Return the best guess even below the threshold, so a backtest can measure other thresholds.
+ """
diff --git a/products/error_tracking/backend/logic/auto_assignment/evidence.py b/products/error_tracking/backend/logic/auto_assignment/evidence.py
new file mode 100644
index 000000000000..7735d059a9a1
--- /dev/null
+++ b/products/error_tracking/backend/logic/auto_assignment/evidence.py
@@ -0,0 +1,151 @@
+from datetime import datetime, timedelta
+from functools import cached_property
+
+from django.db.models import Exists, OuterRef, Q
+
+from posthog.hogql import ast
+from posthog.hogql.constants import HogQLGlobalSettings
+from posthog.hogql.database.schema.document_embeddings import HOGQL_EMBEDDING_TABLES
+from posthog.hogql.parser import parse_select
+from posthog.hogql.query import execute_hogql_query
+
+from posthog.clickhouse.client.connection import ClickHouseUser, Workload
+from posthog.dataclasses import frozen
+from posthog.models import Team
+from posthog.models.organization import OrganizationMembership
+
+from products.error_tracking.backend.logic.issue_changes import AssigneeRef
+from products.error_tracking.backend.models import ErrorTrackingIssueFingerprintV2
+
+# The embedding tables keep three months of rows. Older fingerprints have nothing to compare against.
+EMBEDDING_RETENTION = timedelta(days=90)
+MAX_ASSIGNED_FINGERPRINTS = 5000
+# Fingerprints, not issues: one merged issue can own many near-identical fingerprints.
+MAX_NEAREST_FINGERPRINTS = 200
+MAX_SIMILAR_ISSUES = 20
+LOOKUP_MAX_EXECUTION_SECONDS = 10
+
+NEAREST_ASSIGNED_FINGERPRINTS_QUERY = """
+ SELECT document_id, cosineDistance({target_embedding}, embedding) AS distance
+ FROM {table}
+ WHERE document_type = 'fingerprint'
+ AND product = 'error_tracking'
+ AND rendering = {rendering}
+ AND team_id = {team_id}
+ AND timestamp >= {min_timestamp}
+ AND document_id IN {fingerprints}
+ ORDER BY distance ASC
+ LIMIT {limit}
+"""
+
+
+@frozen
+class IssueEmbedding:
+ model_name: str
+ rendering: str
+ vector: tuple[float, ...]
+
+
+@frozen
+class SimilarAssignedIssue:
+ issue_id: str
+ assignee: AssigneeRef
+ distance: float
+
+
+@frozen
+class SimilarIssueLookup:
+ issues: tuple[SimilarAssignedIssue, ...]
+
+
+class IssueEvidence:
+ """Facts about one new issue. Each fact loads on first use, so a processor that does not run costs nothing."""
+
+ def __init__(self, team: Team, issue_id: str, created_at: datetime, embedding: IssueEmbedding | None) -> None:
+ self.team = team
+ self.issue_id = issue_id
+ self.created_at = created_at
+ self.embedding = embedding
+
+ @cached_property
+ def ai_processing_approved(self) -> bool:
+ return self.team.organization.is_ai_data_processing_approved is True
+
+ @cached_property
+ def similar_assigned_issues(self) -> SimilarIssueLookup | None:
+ if self.embedding is None:
+ return None
+ assignees = self._assigned_fingerprints()
+ nearest = self._nearest_fingerprints(self.embedding, list(assignees)) if assignees else []
+ issues: dict[str, SimilarAssignedIssue] = {}
+ for fingerprint, distance in nearest:
+ issue_id, assignee = assignees[fingerprint]
+ if issue_id not in issues:
+ issues[issue_id] = SimilarAssignedIssue(issue_id=issue_id, assignee=assignee, distance=distance)
+ return SimilarIssueLookup(
+ issues=tuple(issues.values())[:MAX_SIMILAR_ISSUES],
+ )
+
+ def _assigned_fingerprints(self) -> dict[str, tuple[str, AssigneeRef]]:
+ # The fingerprint, its issue and the assignment must all predate this issue, so a backtest over past issues
+ # does not see them early. A reassignment keeps the row's original created_at, so a backtest still sees the
+ # latest assignee, not the one at the time.
+ assignee_is_member = Exists(
+ OrganizationMembership.objects.filter(
+ user_id=OuterRef("issue__assignment__user_id"), organization_id=self.team.organization_id
+ )
+ )
+ rows = (
+ ErrorTrackingIssueFingerprintV2.objects.filter(
+ team_id=self.team.id,
+ first_seen__gte=self.created_at - EMBEDDING_RETENTION,
+ first_seen__lt=self.created_at,
+ issue__created_at__lt=self.created_at,
+ issue__assignment__created_at__lt=self.created_at,
+ )
+ .alias(assignee_is_member=assignee_is_member)
+ # Removing a member from the organization keeps their assignments, and legacy assignments have neither a
+ # user nor a role. Neither can be assigned again, so neither votes.
+ .filter(
+ Q(issue__assignment__user_id__isnull=False, assignee_is_member=True)
+ | Q(issue__assignment__user_id__isnull=True, issue__assignment__role_id__isnull=False)
+ )
+ .exclude(issue_id=self.issue_id)
+ .order_by("-first_seen")
+ .values_list("fingerprint", "issue_id", "issue__assignment__user_id", "issue__assignment__role_id")
+ )[:MAX_ASSIGNED_FINGERPRINTS]
+ fingerprints: dict[str, tuple[str, AssigneeRef]] = {}
+ for fingerprint, issue_id, user_id, role_id in rows:
+ assignee = (
+ AssigneeRef(type="user", id=str(user_id))
+ if user_id is not None
+ else AssigneeRef(type="role", id=str(role_id))
+ )
+ fingerprints[fingerprint] = (str(issue_id), assignee)
+ return fingerprints
+
+ def _nearest_fingerprints(self, embedding: IssueEmbedding, fingerprints: list[str]) -> list[tuple[str, float]]:
+ table = HOGQL_EMBEDDING_TABLES.get(embedding.model_name)
+ if table is None:
+ raise ValueError(f"Invalid embedding model: {embedding.model_name}")
+ query = parse_select(
+ NEAREST_ASSIGNED_FINGERPRINTS_QUERY,
+ placeholders={
+ "table": ast.Field(chain=[table.to_printed_hogql()]),
+ "target_embedding": ast.Constant(value=list(embedding.vector)),
+ "rendering": ast.Constant(value=embedding.rendering),
+ "team_id": ast.Constant(value=self.team.id),
+ "min_timestamp": ast.Constant(value=self.created_at - EMBEDDING_RETENTION),
+ "fingerprints": ast.Constant(value=fingerprints),
+ "limit": ast.Constant(value=MAX_NEAREST_FINGERPRINTS),
+ },
+ )
+ response = execute_hogql_query(
+ query=query,
+ team=self.team,
+ query_type="ErrorTrackingAutoAssignmentSimilarIssues",
+ workload=Workload.OFFLINE,
+ ch_user=ClickHouseUser.ERROR_TRACKING,
+ settings=HogQLGlobalSettings(max_execution_time=LOOKUP_MAX_EXECUTION_SECONDS),
+ )
+ return [(str(row[0]), float(row[1])) for row in response.results]
diff --git a/products/error_tracking/backend/logic/auto_assignment/gate.py b/products/error_tracking/backend/logic/auto_assignment/gate.py
new file mode 100644
index 000000000000..5c9d554d96a3
--- /dev/null
+++ b/products/error_tracking/backend/logic/auto_assignment/gate.py
@@ -0,0 +1,47 @@
+from posthog.dataclasses import frozen
+
+from products.error_tracking.backend.logic.team_flags import team_flag_enabled, team_flag_payload
+
+AUTO_ASSIGNMENT_FLAG = "error-tracking-auto-assignment"
+
+
+def auto_assignment_enabled(team_id: int) -> bool:
+ # The billing entitlement check goes here, beside the flag, when auto-assignment becomes a paid feature.
+ return team_flag_enabled(AUTO_ASSIGNMENT_FLAG, team_id)
+
+
+@frozen
+class HistoryConfig:
+ top_k: int = 3
+ # A past issue votes only when its cosine distance to the new issue is at most this.
+ max_distance: float = 0.15
+ min_confidence: float = 0.66
+
+
+@frozen
+class AutoAssignmentConfig:
+ history: HistoryConfig = HistoryConfig()
+
+
+def _bounded(value: object, default: float, low: float, high: float) -> float:
+ if isinstance(value, bool) or not isinstance(value, int | float) or not low <= value <= high:
+ return default
+ return float(value)
+
+
+def parse_auto_assignment_config(payload: object) -> AutoAssignmentConfig:
+ payload = payload if isinstance(payload, dict) else {}
+ raw_history = payload.get("history")
+ history: dict[str, object] = raw_history if isinstance(raw_history, dict) else {}
+ defaults = HistoryConfig()
+ return AutoAssignmentConfig(
+ history=HistoryConfig(
+ top_k=int(_bounded(history.get("top_k"), defaults.top_k, 1, 20)),
+ max_distance=_bounded(history.get("max_distance"), defaults.max_distance, 0, 2),
+ min_confidence=_bounded(history.get("min_confidence"), defaults.min_confidence, 0, 1),
+ )
+ )
+
+
+def load_auto_assignment_config(team_id: int) -> AutoAssignmentConfig:
+ return parse_auto_assignment_config(team_flag_payload(AUTO_ASSIGNMENT_FLAG, team_id))
diff --git a/products/error_tracking/backend/logic/auto_assignment/history.py b/products/error_tracking/backend/logic/auto_assignment/history.py
new file mode 100644
index 000000000000..ce95b6454a54
--- /dev/null
+++ b/products/error_tracking/backend/logic/auto_assignment/history.py
@@ -0,0 +1,38 @@
+from collections import Counter
+
+from products.error_tracking.backend.logic.auto_assignment.base import Candidate, HistoryEvidence, Processor
+from products.error_tracking.backend.logic.auto_assignment.evidence import IssueEvidence
+from products.error_tracking.backend.logic.auto_assignment.gate import AutoAssignmentConfig
+
+
+class HistoryVote(Processor):
+ name = "history"
+
+ def threshold(self, config: AutoAssignmentConfig) -> float:
+ return config.history.min_confidence
+
+ def propose(self, evidence: IssueEvidence, config: AutoAssignmentConfig) -> Candidate | None:
+ lookup = evidence.similar_assigned_issues
+ if lookup is None:
+ return None
+ voters = [
+ issue for issue in lookup.issues[: config.history.top_k] if issue.distance <= config.history.max_distance
+ ]
+ if not voters:
+ return None
+ votes = Counter(issue.assignee for issue in voters)
+ top_votes = max(votes.values())
+ # On a tie the assignee of the nearest issue wins, because voters are nearest first.
+ winner = next(issue.assignee for issue in voters if votes[issue.assignee] == top_votes)
+ supporting = [issue for issue in voters if issue.assignee == winner]
+ # Divide by top_k, not by the voter count, so that one close match out of three is weak evidence.
+ return Candidate(
+ assignee=winner,
+ confidence=top_votes / config.history.top_k,
+ source=self.name,
+ reason=f"Assigned on {top_votes} of the {config.history.top_k} most similar past issues",
+ evidence=HistoryEvidence(
+ similar_issue_ids=tuple(issue.issue_id for issue in supporting),
+ nearest_distance=supporting[0].distance,
+ ),
+ )
diff --git a/products/error_tracking/backend/logic/auto_assignment/pipeline.py b/products/error_tracking/backend/logic/auto_assignment/pipeline.py
new file mode 100644
index 000000000000..026a6a07522b
--- /dev/null
+++ b/products/error_tracking/backend/logic/auto_assignment/pipeline.py
@@ -0,0 +1,101 @@
+import time
+from collections.abc import Sequence
+from enum import StrEnum
+
+import structlog
+
+from posthog.dataclasses import frozen
+from posthog.exceptions_capture import capture_exception
+
+from products.error_tracking.backend.logic.auto_assignment.base import Candidate, Decision, Processor
+from products.error_tracking.backend.logic.auto_assignment.evidence import IssueEvidence
+from products.error_tracking.backend.logic.auto_assignment.gate import AutoAssignmentConfig, load_auto_assignment_config
+from products.error_tracking.backend.logic.auto_assignment.registry import PROCESSORS
+
+logger = structlog.get_logger(__name__)
+
+
+class ProcessorStatus(StrEnum):
+ ACCEPTED = "accepted"
+ BELOW_THRESHOLD = "below_threshold"
+ NO_CANDIDATE = "no_candidate"
+ FAILED = "failed"
+ SKIPPED_AI_NOT_APPROVED = "skipped_ai_not_approved"
+ SKIPPED_ALREADY_DECIDED = "skipped_already_decided"
+
+
+@frozen
+class ProcessorOutcome:
+ processor: str
+ status: ProcessorStatus
+ candidate: Candidate | None = None
+ threshold: float | None = None
+ # Time spent in propose(), which includes loading the evidence it needs. None when it did not run.
+ duration_ms: float | None = None
+
+
+@frozen
+class PipelineRun:
+ decision: Decision | None
+ outcomes: tuple[ProcessorOutcome, ...]
+
+
+def run(
+ evidence: IssueEvidence,
+ *,
+ processors: Sequence[Processor] = PROCESSORS,
+ config: AutoAssignmentConfig | None = None,
+) -> PipelineRun:
+ config = config or load_auto_assignment_config(evidence.team.id)
+
+ decision: Decision | None = None
+ outcomes: list[ProcessorOutcome] = []
+ for processor in processors:
+ if decision is not None:
+ outcome = ProcessorOutcome(processor=processor.name, status=ProcessorStatus.SKIPPED_ALREADY_DECIDED)
+ else:
+ outcome = _evaluate(processor, evidence, config)
+ if (
+ outcome.status == ProcessorStatus.ACCEPTED
+ and outcome.candidate is not None
+ and outcome.threshold is not None
+ ):
+ decision = Decision(candidate=outcome.candidate, threshold=outcome.threshold)
+ outcomes.append(outcome)
+ return PipelineRun(decision=decision, outcomes=tuple(outcomes))
+
+
+def _evaluate(processor: Processor, evidence: IssueEvidence, config: AutoAssignmentConfig) -> ProcessorOutcome:
+ if processor.sends_data_to_model and not evidence.ai_processing_approved:
+ return ProcessorOutcome(processor=processor.name, status=ProcessorStatus.SKIPPED_AI_NOT_APPROVED)
+ threshold = processor.threshold(config)
+ start = time.monotonic()
+ try:
+ candidate = processor.propose(evidence, config)
+ except Exception as error:
+ capture_exception(error, additional_properties={"team_id": evidence.team.id, "processor": processor.name})
+ logger.warning(
+ "error_tracking_auto_assignment_processor_failed",
+ team_id=evidence.team.id,
+ issue_id=evidence.issue_id,
+ processor=processor.name,
+ exc_info=True,
+ )
+ return ProcessorOutcome(processor=processor.name, status=ProcessorStatus.FAILED, duration_ms=_elapsed_ms(start))
+ if candidate is None:
+ status = ProcessorStatus.NO_CANDIDATE
+ elif candidate.confidence >= threshold:
+ status = ProcessorStatus.ACCEPTED
+ else:
+ status = ProcessorStatus.BELOW_THRESHOLD
+ return ProcessorOutcome(
+ processor=processor.name,
+ status=status,
+ candidate=candidate,
+ threshold=threshold,
+ duration_ms=_elapsed_ms(start),
+ )
+
+
+def _elapsed_ms(start: float) -> float:
+ return (time.monotonic() - start) * 1000
diff --git a/products/error_tracking/backend/logic/auto_assignment/registry.py b/products/error_tracking/backend/logic/auto_assignment/registry.py
new file mode 100644
index 000000000000..145ea5d9d8d5
--- /dev/null
+++ b/products/error_tracking/backend/logic/auto_assignment/registry.py
@@ -0,0 +1,7 @@
+from products.error_tracking.backend.logic.auto_assignment.base import Processor
+from products.error_tracking.backend.logic.auto_assignment.history import HistoryVote
+
+# Processors run in this order and the first candidate over its threshold wins.
+PROCESSORS: list[Processor] = [
+ HistoryVote(),
+]
diff --git a/products/error_tracking/backend/logic/auto_assignment/test/test_history.py b/products/error_tracking/backend/logic/auto_assignment/test/test_history.py
new file mode 100644
index 000000000000..b89cd513daea
--- /dev/null
+++ b/products/error_tracking/backend/logic/auto_assignment/test/test_history.py
@@ -0,0 +1,174 @@
+import math
+from datetime import timedelta
+from typing import Literal
+from uuid import UUID
+
+import pytest
+from posthog.test.base import BaseTest, ClickhouseTestMixin
+from unittest.mock import MagicMock
+
+from django.test import SimpleTestCase
+from django.utils import timezone
+
+from parameterized import parameterized
+
+from posthog.clickhouse.client import sync_execute
+from posthog.models import User
+from posthog.models.organization import OrganizationMembership
+
+from products.access_control.backend.facade.testing import create_role
+from products.error_tracking.backend.logic.auto_assignment.evidence import (
+ IssueEmbedding,
+ IssueEvidence,
+ SimilarAssignedIssue,
+ SimilarIssueLookup,
+)
+from products.error_tracking.backend.logic.auto_assignment.gate import AutoAssignmentConfig, HistoryConfig
+from products.error_tracking.backend.logic.auto_assignment.history import HistoryVote
+from products.error_tracking.backend.logic.issue_changes import AssigneeRef
+from products.error_tracking.backend.models import (
+ ErrorTrackingIssue,
+ ErrorTrackingIssueAssignment,
+ ErrorTrackingIssueFingerprintV2,
+)
+
+ALICE = AssigneeRef(type="user", id="1")
+BOB = AssigneeRef(type="user", id="2")
+CONFIG = AutoAssignmentConfig(history=HistoryConfig(top_k=3, max_distance=0.2, min_confidence=0.66))
+MODEL = "text-embedding-3-small-1536"
+
+
+def _evidence(*matches: tuple[AssigneeRef, float]) -> MagicMock:
+ evidence = MagicMock()
+ evidence.similar_assigned_issues = SimilarIssueLookup(
+ issues=tuple(
+ SimilarAssignedIssue(issue_id=f"issue-{i}", assignee=assignee, distance=distance)
+ for i, (assignee, distance) in enumerate(matches)
+ ),
+ )
+ return evidence
+
+
+class TestHistoryVote(SimpleTestCase):
+ @parameterized.expand(
+ [
+ ("agree", [(ALICE, 0.05), (ALICE, 0.1), (BOB, 0.12)], ALICE, 2 / 3, ("issue-0", "issue-1")),
+ (
+ "disagree_nearest_wins_tie",
+ [(BOB, 0.05), (ALICE, 0.1), (AssigneeRef(type="role", id="r"), 0.12)],
+ BOB,
+ 1 / 3,
+ ("issue-0",),
+ ),
+ ("far_matches_do_not_vote", [(ALICE, 0.05), (BOB, 0.3), (BOB, 0.4)], ALICE, 1 / 3, ("issue-0",)),
+ (
+ "only_top_k_vote",
+ [(ALICE, 0.01), (ALICE, 0.02), (BOB, 0.03), (BOB, 0.04), (BOB, 0.05)],
+ ALICE,
+ 2 / 3,
+ ("issue-0", "issue-1"),
+ ),
+ ]
+ )
+ def test_vote(
+ self,
+ _name: str,
+ matches: list[tuple[AssigneeRef, float]],
+ assignee: AssigneeRef,
+ confidence: float,
+ similar_issue_ids: tuple[str, ...],
+ ) -> None:
+ candidate = HistoryVote().propose(_evidence(*matches), CONFIG)
+
+ assert candidate is not None
+ assert candidate.assignee == assignee
+ assert candidate.confidence == confidence
+ assert candidate.evidence.similar_issue_ids == similar_issue_ids
+
+ @parameterized.expand(
+ [
+ ("no_history", ()),
+ ("all_below_similarity_threshold", ((ALICE, 0.3), (ALICE, 0.4))),
+ ]
+ )
+ def test_no_candidate(self, _name: str, matches: tuple[tuple[AssigneeRef, float], ...]) -> None:
+ assert HistoryVote().propose(_evidence(*matches), CONFIG) is None
+
+ def test_no_embedding_means_no_candidate(self) -> None:
+ evidence = MagicMock(similar_assigned_issues=None)
+
+ assert HistoryVote().propose(evidence, CONFIG) is None
+
+
+def _vector(*values: float) -> list[float]:
+ return [*values, *([0.0] * (1536 - len(values)))]
+
+
+class TestSimilarAssignedIssues(ClickhouseTestMixin, BaseTest):
+ def _issue(
+ self,
+ fingerprint: str,
+ vector: list[float],
+ *,
+ assignee: Literal["user", "neither"] | User | UUID | None = "user",
+ ) -> ErrorTrackingIssue:
+ issue = ErrorTrackingIssue.objects.create(team=self.team, name=fingerprint)
+ self._fingerprint(issue, fingerprint, vector)
+ if assignee == "user":
+ ErrorTrackingIssueAssignment.objects.create(issue=issue, team=self.team, user=self.user)
+ elif assignee == "neither":
+ ErrorTrackingIssueAssignment.objects.create(issue=issue, team=self.team)
+ elif isinstance(assignee, User):
+ ErrorTrackingIssueAssignment.objects.create(issue=issue, team=self.team, user=assignee)
+ elif assignee is not None:
+ ErrorTrackingIssueAssignment.objects.create(issue=issue, team=self.team, role_id=assignee)
+ return issue
+
+ def _fingerprint(self, issue: ErrorTrackingIssue, fingerprint: str, vector: list[float]) -> None:
+ ErrorTrackingIssueFingerprintV2.objects.create(team=self.team, issue=issue, fingerprint=fingerprint)
+ now = timezone.now()
+ sync_execute(
+ """
+ INSERT INTO distributed_posthog_document_embeddings_text_embedding_3_small_1536 (
+ team_id, product, document_type, rendering, document_id, timestamp, inserted_at,
+ content, metadata, embedding, _timestamp, _offset, _partition
+ ) VALUES
+ """,
+ [(self.team.id, "error_tracking", "fingerprint", "r", fingerprint, now, now, "", "{}", vector, now, 0, 0)],
+ flush=True,
+ team_id=self.team.pk,
+ )
+
+ def test_returns_assigned_past_issues_nearest_first(self) -> None:
+ near = self._issue("near", _vector(1.0, 0.1))
+ self._fingerprint(near, "near-2", _vector(1.0, 0.05))
+ far = self._issue("far", _vector(1.0, 1.0))
+ role_id = create_role(organization_id=self.organization.id, name="Backend")
+ by_role = self._issue("by-role", _vector(1.0, 2.0), assignee=role_id)
+ self._issue("unassigned", _vector(1.0), assignee=None)
+ self._issue("no-user-or-role", _vector(1.0), assignee="neither")
+ former_member = self._create_user("[email protected]")
+ OrganizationMembership.objects.filter(user=former_member, organization=self.organization).delete()
+ self._issue("former-member", _vector(1.0), assignee=former_member)
+ late = self._issue("assigned-later", _vector(1.0))
+ seen_later = self._issue("seen-later", _vector(1.0))
+ target = self._issue("target", _vector(1.0))
+ created_at = timezone.now() + timedelta(minutes=1)
+ ErrorTrackingIssueAssignment.objects.filter(issue=late).update(created_at=created_at + timedelta(hours=1))
+ ErrorTrackingIssueFingerprintV2.objects.filter(issue=seen_later).update(
+ first_seen=created_at + timedelta(hours=1)
+ )
+
+ evidence = IssueEvidence(
+ team=self.team,
+ issue_id=str(target.id),
+ created_at=created_at,
+ embedding=IssueEmbedding(model_name=MODEL, rendering="r", vector=tuple(_vector(1.0))),
+ )
+ lookup = evidence.similar_assigned_issues
+
+ assert lookup is not None
+ assert [issue.issue_id for issue in lookup.issues] == [str(near.id), str(far.id), str(by_role.id)]
+ assert lookup.issues[0].distance == pytest.approx(1 - 1 / math.sqrt(1 + 0.05**2), abs=1e-5)
+ assert lookup.issues[0].assignee == AssigneeRef(type="user", id=str(self.user.id))
+ assert lookup.issues[2].assignee == AssigneeRef(type="role", id=str(role_id))
diff --git a/products/error_tracking/backend/logic/auto_assignment/test/test_pipeline.py b/products/error_tracking/backend/logic/auto_assignment/test/test_pipeline.py
new file mode 100644
index 000000000000..876e5180f64d
--- /dev/null
+++ b/products/error_tracking/backend/logic/auto_assignment/test/test_pipeline.py
@@ -0,0 +1,122 @@
+from unittest.mock import MagicMock, patch
+
+from django.test import SimpleTestCase
+
+from parameterized import parameterized
+
+from products.error_tracking.backend.logic.auto_assignment.base import Candidate, HistoryEvidence, Processor
+from products.error_tracking.backend.logic.auto_assignment.gate import (
+ AutoAssignmentConfig,
+ parse_auto_assignment_config,
+)
+from products.error_tracking.backend.logic.auto_assignment.pipeline import ProcessorStatus, run
+from products.error_tracking.backend.logic.issue_changes import AssigneeRef
+
+CONFIG = AutoAssignmentConfig()
+
+
+class FakeProcessor(Processor):
+ def __init__(
+ self,
+ name: str,
+ confidence: float | None,
+ threshold: float = 0.5,
+ sends_data_to_model: bool = False,
+ error: Exception | None = None,
+ ) -> None:
+ self.name = name
+ self.sends_data_to_model = sends_data_to_model
+ self._confidence = confidence
+ self._threshold = threshold
+ self._error = error
+ self.calls = 0
+
+ def threshold(self, config: AutoAssignmentConfig) -> float:
+ return self._threshold
+
+ def propose(self, evidence: object, config: AutoAssignmentConfig) -> Candidate | None:
+ self.calls += 1
+ if self._error is not None:
+ raise self._error
+ if self._confidence is None:
+ return None
+ return Candidate(
+ assignee=AssigneeRef(type="user", id=self.name),
+ confidence=self._confidence,
+ source=self.name,
+ reason="fake",
+ evidence=HistoryEvidence(similar_issue_ids=(), nearest_distance=0.0),
+ )
+
+
+def _evidence(ai_approved: bool = True) -> MagicMock:
+ evidence = MagicMock(ai_processing_approved=ai_approved, issue_id="issue")
+ evidence.team.id = 1
+ evidence.similar_issue_lookup_ms = None
+ return evidence
+
+
+class TestPipeline(SimpleTestCase):
+ def test_first_candidate_over_its_threshold_wins_and_later_processors_do_not_run(self) -> None:
+ none = FakeProcessor("none", confidence=None)
+ weak = FakeProcessor("weak", confidence=0.4, threshold=0.5)
+ strong = FakeProcessor("strong", confidence=0.6, threshold=0.5)
+ later = FakeProcessor("later", confidence=1.0)
+
+ result = run(_evidence(), processors=[none, weak, strong, later], config=CONFIG)
+
+ assert result.decision is not None
+ assert result.decision.candidate.source == "strong"
+ assert [(o.processor, o.status) for o in result.outcomes] == [
+ ("none", ProcessorStatus.NO_CANDIDATE),
+ ("weak", ProcessorStatus.BELOW_THRESHOLD),
+ ("strong", ProcessorStatus.ACCEPTED),
+ ("later", ProcessorStatus.SKIPPED_ALREADY_DECIDED),
+ ]
+ assert result.outcomes[1].candidate is not None and result.outcomes[1].candidate.confidence == 0.4
+ assert later.calls == 0
+
+ def test_confidence_equal_to_threshold_is_accepted(self) -> None:
+ result = run(_evidence(), processors=[FakeProcessor("p", confidence=0.5, threshold=0.5)], config=CONFIG)
+
+ assert result.decision is not None
+
+ def test_model_processor_is_skipped_without_ai_approval(self) -> None:
+ model = FakeProcessor("model", confidence=1.0, sends_data_to_model=True)
+ local = FakeProcessor("local", confidence=1.0)
+
+ result = run(_evidence(ai_approved=False), processors=[model, local], config=CONFIG)
+
+ assert [(o.processor, o.status) for o in result.outcomes] == [
+ ("model", ProcessorStatus.SKIPPED_AI_NOT_APPROVED),
+ ("local", ProcessorStatus.ACCEPTED),
+ ]
+ assert model.calls == 0
+
+ @patch("products.error_tracking.backend.logic.auto_assignment.pipeline.capture_exception")
+ def test_failing_processor_does_not_stop_the_next(self, _capture: MagicMock) -> None:
+ broken = FakeProcessor("broken", confidence=None, error=RuntimeError("boom"))
+ working = FakeProcessor("working", confidence=1.0)
+
+ result = run(_evidence(), processors=[broken, working], config=CONFIG)
+
+ assert [o.status for o in result.outcomes] == [ProcessorStatus.FAILED, ProcessorStatus.ACCEPTED]
+ _capture.assert_called_once()
+
+
+class TestConfig(SimpleTestCase):
+ @parameterized.expand(
+ [
+ ("no_payload", None, (3, 0.15, 0.66)),
+ ("valid", {"history": {"top_k": 5, "max_distance": 0.3, "min_confidence": 0.8}}, (5, 0.3, 0.8)),
+ (
+ "invalid_fields_keep_defaults",
+ {"history": {"top_k": 0, "max_distance": "x", "min_confidence": 2}},
+ (3, 0.15, 0.66),
+ ),
+ ]
+ )
+ def test_parse(self, _name: str, payload: object, expected: tuple[int, float, float]) -> None:
+ history = parse_auto_assignment_config(payload).history
+
+ assert (history.top_k, history.max_distance, history.min_confidence) == expected
diff --git a/products/error_tracking/backend/logic/team_flags.py b/products/error_tracking/backend/logic/team_flags.py
index 011b349dbed4..5d7e7e853bf9 100644
--- a/products/error_tracking/backend/logic/team_flags.py
+++ b/products/error_tracking/backend/logic/team_flags.py
@@ -42,3 +42,12 @@ def team_flag_explicitly_off(flag: str, team_id: int) -> bool:
except Exception:
logger.exception("error_tracking_flag_check_failed", flag=flag, team_id=team_id)
return False
+
+
+def team_flag_payload(flag: str, team_id: int) -> object:
+ """Return the project flag payload, or None when the lookup fails or the SDK is disabled in local dev."""
+ try:
+ return posthoganalytics.get_feature_flag_payload(flag, str(team_id), **_project_flag_options(team_id))
+ except Exception:
+ logger.exception("error_tracking_flag_payload_failed", flag=flag, team_id=team_id)
+ return None