crbench//

← all cases

feat(workflow_engine): Add in hook for producing occurrences from the stateful detector

Tier 2 easy python · local · real merged PR from getsentry/sentry #80168

Ground-truth defects

d1 · other · high
MetricAlertDetectorHandler inherits from StatefulDetectorHandler but only contains pass, failing to implement its required abstract methods: counter_names (property), get_dedupe_value(), get_group_key_values(), and build_occurrence_and_event_data(). This will cause a TypeError at runtime when the class is instantiated.

The change under review

diff (truncated — full diff)
diff --git a/src/sentry/incidents/grouptype.py b/src/sentry/incidents/grouptype.py
index 5b01444025b..a17a843e72b 100644
--- a/src/sentry/incidents/grouptype.py
+++ b/src/sentry/incidents/grouptype.py
@@ -4,17 +4,12 @@ from sentry.incidents.utils.types import QuerySubscriptionUpdate
 from sentry.issues.grouptype import GroupCategory, GroupType
 from sentry.ratelimits.sliding_windows import Quota
 from sentry.types.group import PriorityLevel
-from sentry.workflow_engine.models import DataPacket
-from sentry.workflow_engine.processors.detector import DetectorEvaluationResult, DetectorHandler
+from sentry.workflow_engine.processors.detector import StatefulDetectorHandler
 
 
 # TODO: This will be a stateful detector when we build that abstraction
-class MetricAlertDetectorHandler(DetectorHandler[QuerySubscriptionUpdate]):
-    def evaluate(
-        self, data_packet: DataPacket[QuerySubscriptionUpdate]
-    ) -> list[DetectorEvaluationResult]:
-        # TODO: Implement
-        return []
+class MetricAlertDetectorHandler(StatefulDetectorHandler[QuerySubscriptionUpdate]):
+    pass
 
 
 # Example GroupType and detector handler for metric alerts. We don't create these issues yet, but we'll use something
diff --git a/src/sentry/workflow_engine/models/detector.py b/src/sentry/workflow_engine/models/detector.py
index 1b72815cb78..2f293ac3956 100644
--- a/src/sentry/workflow_engine/models/detector.py
+++ b/src/sentry/workflow_engine/models/detector.py
@@ -1,5 +1,6 @@
 from __future__ import annotations
 
+import builtins
 import logging
 from typing import TYPE_CHECKING
 
@@ -9,6 +10,7 @@ from django.db.models import UniqueConstraint
 from sentry.backup.scopes import RelocationScope
 from sentry.db.models import DefaultFieldsModel, FlexibleForeignKey, region_silo_model
 from sentry.issues import grouptype
+from sentry.issues.grouptype import GroupType
 from sentry.models.owner_base import OwnerModel
 
 if TYPE_CHECKING:
@@ -53,9 +55,13 @@ class Detector(DefaultFieldsModel, OwnerModel):
         # XXX: Temporary property until we add `project_id` to the model.
         return 1
 
+    @property
+    def group_type(self) -> builtins.type[GroupType] | None:
+        return grouptype.registry.get_by_slug(self.type)
+
     @property
     def detector_handler(self) -> DetectorHandler | None:
-        group_type = grouptype.registry.get_by_slug(self.type)
+        group_type = self.group_type
         if not group_type:
             logger.error(
                 "No registered grouptype for detector",
diff --git a/src/sentry/workflow_engine/processors/detector.py b/src/sentry/workflow_engine/processors/detector.py
index 4e2e9b06a41..898b59345d0 100644
--- a/src/sentry/workflow_engine/processors/detector.py
+++ b/src/sentry/workflow_engine/processors/detector.py
@@ -14,6 +14,7 @@ from sentry.issues.issue_occurrence import IssueOccurrence
 from sentry.issues.producer import PayloadType, produce_occurrence_to_kafka
 from sentry.issues.status_change_message import StatusChangeMessage
 from sentry.models.group import GroupStatus
+from sentry.types.group import PriorityLevel
 from sentry.utils import metrics, redis
 from sentry.utils.function_cache import cache_func_for_models
 from sentry.utils.iterators import chunked
@@ -45,7 +46,7 @@ class DetectorEvaluationResult:
 
 def process_detectors(
     data_packet: DataPacket, detectors: list[Detector]
-) -> list[tuple[Detector, list[DetectorEvaluationResult]]]:
+) -> list[tuple[Detector, dict[DetectorGroupKey, DetectorEvaluationResult]]]:
     results = []
 
     for detector in detectors:
@@ -55,25 +56,11 @@ def process_detectors(
             continue
 
         detector_results = handler.evaluate(data_packet)
-        detector_group_keys = set()
-
-        for result in detector_results:
-            if result.group_key in detector_group_keys:
-                # This shouldn't happen - log an error and continue on, but we should investigate this.
-                logger.error(
-                    "Duplicate detector state group keys found",
-                    extra={
-                        "detector_id": detector.id,
-                        "group_key": result.group_key,
-                    },
-                )
-                continue
 
+        for result in detector_results.values():
             if result.result is not None:
                 create_issue_occurrence_from_result(result)
 
-            detector_group_keys.add(result.group_key)
-
         if detector_results:
             results.append((detector, detector_results))
 
@@ -136,7 +123,9 @@ class DetectorHandler(abc.ABC, Generic[T]):
             self.conditions = []
 
     @abc.abstractmethod
-    def evaluate(self, data_packet: DataPacket[T]) -> list[DetectorEvaluationResult]:
+    def evaluate(
+        self, data_packet: DataPacket[T]
+    ) -> dict[DetectorGroupKey, DetectorEvaluationResult]:
         pass
 
     def commit_state_updates(self):
@@ -174,6 +163,12 @@ class StatefulDetectorHandler(DetectorHandler[T], abc.ABC):
         """
         pass
 
+    @abc.abstractmethod
+    def build_occurrence_and_event_data(
+        self, group_key: DetectorGroupKey, value: int, new_status: PriorityLevel
+    ) -> tuple[IssueOccurrence, dict[str, Any]]:
+        pass
+
     def build_fingerprint(self, group_key) -> list[str]:
         """
         Builds a fingerprint to uniquely identify a detected issue
@@ -228,7 +223,9 @@ class StatefulDetectorHandler(DetectorHandler[T], abc.ABC):
             )
         return results
 
-    def evaluate(self, data_packet: DataPacket[T]) -> list[DetectorEvaluationResult]:
+    def evaluate(
+        self, data_packet: DataPacket[T]
+    ) -> dict[DetectorGroupKey, DetectorEvaluationResult]:
         """
         Evaluates a given data packet and returns a list of `DetectorEvaluationResult`.
         There will be one result for each group key result in the packet, unless the
@@ -237,13 +234,13 @@ class StatefulDetectorHandler(DetectorHandler[T], abc.ABC):
         dedupe_value = self.get_dedupe_value(data_packet)
         group_values = self.get_group_key_values(data_packet)
         all_state_data = self.get_state_data(list(group_values.keys()))
-        results = []
+        results = {}
         for group_key, group_value in group_values.items():
             result = self.evaluate_group_key_value(
                 group_key, group_value, all_state_data[group_key], dedupe_value
             )
             if result:
-                results.append(result)
+                results[result.group_key] = result
         return results
 
     def evaluate_group_key_value(
@@ -289,7 +286,7 @@ class StatefulDetectorHandler(DetectorHandler[T], abc.ABC):
             is_active = new_status != DetectorPriorityLevel.OK
             self.enqueue_state_update(group_key, is_active, new_status)
             event_data = None
-            result = None
+            result: StatusChangeMessage | IssueOccurrence
             if new_status == DetectorPriorityLevel.OK:
                 # If we've determined that we're now ok, we just want to resolve the issue
                 result = StatusChangeMessage(
@@ -298,8 +295,10 @@ class StatefulDetectorHandler(DetectorHandler[T], abc.ABC):
                     new_status=GroupStatus.RESOLVED,
                     new_substatus=None,
                 )
-
-            # TODO: Add hook here for generating occurrence
+            else:
+                result, event_data = self.build_occurrence_and_event_data(
+                    group_key, value, PriorityLevel(new_status)
+                )
             return DetectorEvaluationResult(
                 group_key=group_key,
                 is_active=is_active,
diff --git a/tests/sentry/workflow_engine/processors/test_detector.py b/tests/sentry/workflow_engine/processors/test_detector.py
index 7ac44a7b364..d56b02d0714 100644
--- a/tests/sentry/workflow_engine/processors/test_detector.py
+++ b/tests/sentry/workflow_engine/processors/test_detector.py
@@ -1,10 +1,16 @@
 import unittest
+from datetime import datetime, timezone
+from typing import Any
 from unittest import mock
+from unittest.mock import call
 
 from sentry.issues.grouptype import GroupCategory, GroupType
+from sentry.issues.issue_occurrence import IssueOccurrence
+from sentry.issues.producer import PayloadType
 from sentry.issues.status_change_message import StatusChangeMessage
 from sentry.testutils.abstract import Abstract
-from sentry.testutils.cases import TestCase
+from sentry.testutils.helpers.datetime import freeze_time
+from sentry.types.group import PriorityLevel
 from sentry.workflow_engine.models import DataPacket, Detector, DetectorState
 from sentry.workflow_engine.processors.detector import (
     DetectorEvaluationResult,
@@ -14,13 +20,34 @@ from sentry.workflow_engine.processors.detector import (
     get_redis_client,
     process_detectors,
 )
-from sentry.workflow_engine.types import DetectorPriorityLevel
+from sentry.workflow_engine.types import DetectorGroupKey, DetectorPriorityLevel
 from tests.sentry.issues.test_grouptype import BaseGroupTypeTest
 
 
-class TestProcessDetectors(BaseGroupTypeTest):
+class MockDetectorStateHandler(StatefulDetectorHandler[dict]):
+    counter_names = ["test1", "test2"]
+
+    def get_dedupe_value(self, data_packet: DataPacket[dict]) -> int:
+        return data_packet.packet.get("dedupe", 0)
+
+    def get_group_key_values(self, data_packet: DataPacket[dict]) -> dict[str, int]:
+        return data_packet.packet.get("group_vals", {})
+
+    def build_occurrence_and_event_data(
+        self, group_key: DetectorGroupKey, value: int, new_status: PriorityLevel
+    ) -> tuple[IssueOccurrence, dict[str, Any]]:
+        return build_mock_occurrence_and_event(self, group_key, value, new_status)
+
+
+class BaseDetectorHandlerTest(BaseGroupTypeTest):
+    __test__ = Abstract(__module__, __qualname__)
+
     def setUp(self):
         super().setUp()
+        self.sm_comp_patcher = mock.patch.object(
+            StatusChangeMessage, "__eq__", status_change_comparator
+        )
+        self.sm_comp_patcher.__enter__()
 
         class NoHandlerGroupType(GroupType):
             type_id = 1
@@ -29,8 +56,10 @@ class TestProcessDetectors(BaseGroupTypeTest):
             category = GroupCategory.METRIC_ALERT.value
 
         class MockDetectorHandler(DetectorHandler[dict]):
-            def evaluate(self, data_packet: DataPacket[dict]) -> list[DetectorEvaluationResult]:
-                return [DetectorEvaluationResult(None, True, DetectorPriorityLevel.HIGH)]
+            def evaluate(
+                self, data_packet: DataPacket[dict]
+            ) -> dict[DetectorGroupKey, DetectorEvaluationResult]:
+                return {None: DetectorEvaluationResult(None, True, DetectorPriorityLevel.HIGH)}
 
         class HandlerGroupType(GroupType):
             type_id = 2
@@ -39,18 +68,6 @@ class TestProcessDetectors(BaseGroupTypeTest):
             category = GroupCategory.METRIC_ALERT.value
             detector_handler = MockDetectorHandler
 
-        class MockDetectorStateHandler(DetectorHandler[dict]):
-            def evaluate(self, data_packet: DataPacket[dict]) -> list[DetectorEvaluationResult]:
-                group_keys = data_packet.packet.get("group_keys", [None])
-                return [
-                    DetectorEvaluationResult(
-                        group_key,
-                        True,
-                        DetectorPriorityLevel.HIGH,
-                    )
-                    for group_key in group_keys
-                ]
-
         class HandlerStateGroupType(GroupType):
             type_id = 3
             slug = "handler_with_state"
@@ -62,9 +79,57 @@ class TestProcessDetectors(BaseGroupTypeTest):
         self.handler_type = HandlerGroupType
         self.handler_state_type = HandlerStateGroupType
 
+    def tearDown(self):
+        super().tearDown()
+        self.sm_comp_patcher.__exit__(None, None, None)
+
+    def create_detector_and_conditions(self, type: str | None = None):
+        if type is None:
+            type = "handler_with_state"
+        detector = self.create_detector(
+            workflow_condition_group=self.create_data_condition_group(),
+            type=type,
+        )
+        self.create_data_condition(
+            condition="gt",
+            comparison=5,
+            condition_result=DetectorPriorityLevel.HIGH,
+            condition_group=detector.workflow_condition_group,
+        )
+        return detector
+
+    def build_handler(
+        self, detector: Detector | None = None, detector_type=None
+    ) -> MockDetectorStateHandler:
+        if detector is None:
+            detector = self.create_detector_and_conditions(detector_type)
+        return MockDetectorStateHandler(detector)
+
+    def assert_updates(self, handler, group_key, dedupe_value, counter_updates, active, priority):
+        if dedupe_value is not None:
+            assert handler.dedupe_updates.get(group_key) == dedupe_value
+        else:
+            assert group_key not in handler.dedupe_updates
+        if counter_updates is not None:
+            assert handler.counter_updates.get(group_key) == counter_updates
+        else:
+            assert group_key not in handler.counter_updates
+        if active is not None or priority is not None:
+            assert handler.state_updates.get(group_key) == (active, priority)
+        else:
+            assert group_key not in handler.state_updates
+
+
+@freeze_time()
+class TestProcessDetectors(BaseDetectorHandlerTest):
+    def setUp(self):
+        super().setUp()
+
     def build_data_packet(self, **kwargs):
         query_id = "1234"
-        return DataPacket[dict](query_id, {"query_id": query_id, "some": "data", **kwargs})
+        return DataPacket[dict](
+            query_id, {"query_id": query_id, "group_vals": {"group_1": 6}, **kwargs}
+        )
 
     def test(self):
         detector = self.create_detector(type=self.handler_type.slug)
@@ -73,64 +138,88 @@ class TestProcessDetectors(BaseGroupTypeTest):
         assert results == [
             (
                 detector,
-                [DetectorEvaluationResult(None, True, DetectorPriorityLevel.HIGH)],
+                {None: DetectorEvaluationResult(None, True, DetectorPriorityLevel.HIGH)},
             )
         ]
 
-    def test_state_results(self):
-        detector = self.create_detector(type=self.handler_state_type.slug)
-        data_packet = self.build_data_packet()
+    @mock.patch("sentry.workflow_engine.processors.detector.produce_occurrence_to_kafka")
+    def test_state_results(self, mock_produce_occurrence_to_kafka):
+        detector = self.create_detector_and_conditions(type=self.handler_state_type.slug)
+        data_packet = DataPacket("1", {"dedupe": 2, "group_vals": {None: 6}})
         results = process_detectors(data_packet, [detector])
+        occurrence, event_data = build_mock_occurrence_and_event(
+            detector.detector_handler, None, 6, PriorityLevel.HIGH
+        )
+
         result = DetectorEvaluationResult(
             None,
             True,
             DetectorPriorityLevel.HIGH,
+            occurrence,
+            event_data,
         )
         assert results == [
             (
                 detector,
-                [result],
+                {result.group_key: result},
             )
         ]
+        mock_produce_occurrence_to_kafka.assert_called_once_with(
+            payload_type=PayloadType.OCCURRENCE,
+            occurrence=occurrence,
+            status_change=None,
+            event_data=event_data,
+        )
 
-    def test_state_results_multi_group(self):
-        detector = self.create_detector(type=self.handler_state_type.slug)
-        data_packet = self.build_data_packet(group_keys=["group_1", "group_2"])
+    @mock.patch("sentry.workflow_engine.processors.detector.produce_occurrence_to_kafka")
+    def test_state_results_multi_group(self, mock_produce_occurrence_to_kafka):
+        detector = self.create_detector_and_conditions(type=self.handler_state_type.slug)
+        data_packet = DataPacket("1", {"dedupe": 2, "group_vals": {"group_1": 6, "group_2": 10}})
         results = process_detectors(data_packet, [detector])
+        occurrence, event_data = build_mock_occurrence_and_event(
+            detector.detector_handler, "group_1", 6, PriorityLevel.HIGH
+        )
+
         result_1 = DetectorEvaluationResult(
             "group_1",
             True,
             DetectorPriorityLevel.HIGH,
+            occurrence,
+            event_data,
+        )
+        occurrence_2, event_data_2 = build_mock_occurrence_and_event(
+            detector.detector_handler, "group_2", 6, PriorityLevel.HIGH
         )
         result_2 = DetectorEvaluationResult(
             "group_2",
             True,
             DetectorPriorityLevel.HIGH,
+            occurrence_2,
+            event_data_2,
         )
         assert results == [
             (
                 detector,
-                [result_1, result_2],
+                {result_1.group_key: result_1, result_2.group_key: result_2},
             )
         ]
-
-    def test_state_results_multi_group_dupe(self):
-        detector = self.create_detector(type=self.handler_state_type.slug)
-        data_packet = self.build_data_packet(group_keys=["dupe", "dupe"])
-        with mock.patch("sentry.workflow_engine.processors.detector.logger") as mock_logger:
-            results = process_detectors(data_packet, [detector])
-            assert mock_logger.error.call_args[0][0] == "Duplicate detector state group keys found"
-        result = DetectorEvaluationResult(
-            "dupe",
-            True,
-            DetectorPriorityLevel.HIGH,
+        mock_produce_occurrence_to_kafka.assert_has_calls(
+            [
+                call(
+                    payload_type=PayloadType.OCCURRENCE,
+                    occurrence=occurrence,
+                    status_change=None,
+                    event_data=event_data,
+                ),
+                call(
+                    payload_type=PayloadType.OCCURRENCE,
+                    occurrence=occurrence_2,
+                    status_change=None,
+                    event_data=event_data_2,
+                ),
+            ],
+            any_order=True,
         )
-        assert results == [
-            (
-                detector,
-                [result, result],
-            )
-        ]
 
     def test_no_issue_type(self):
         detector = self.create_detector(type="invalid slug")
@@ -152,14 +241,38 @@ class TestProcessDetectors(BaseGroupTypeTest):
         assert results == []
 
 
-class MockDetectorStateHandler(StatefulDetectorHandler[dict]):
-    counter_names = ["test1", "test2"]
-
-    def get_dedupe_value(self, data_packet: DataPacket[dict]) -> int:
-        return data_packet.packet.get("dedupe", 0)
-
-    def get_group_key_values(self, data_packet: DataPacket[dict]) -> dict[str, int]:
-        return data_packet.packet.get("group_vals", {})
+def build_mock_occurrence_and_event(
+    handler: StatefulDetectorHandler,
+    group_key: DetectorGroupKey,
+    value: int,
+    new_status: PriorityLevel,
+) -> tuple[IssueOccurrence, dict[str, Any]]:
+    assert handler.detector.group_type is not None
+    occurrence = IssueOccurrence(
+        id="eb4b0acffadb4d098d48cb14165ab578",
+        project_id=handler.detector.project_id,
+        event_id="43878ab4419f4ab181f6379ac376d5aa",
+        fingerprint=handler.build_fingerprint(group_key),
+        issue_title="Some Issue",
+        subtitle="Some subtitle",
+        resource_id=None,
+        evidence_data={},
+        evidence_display=[],
+        type=handler.detector.group_type,
+        detection_time=datetime.now(timezone.utc),
+        level="error",
+        culprit="Some culprit",
+        initial_issue_priority=new_status.value,
+    )
+    event_data = {
+        "timestamp": occurrence.detection_time,
+        "project_id": occurrence.project_id,
+        "event_id": occurrence.event_id,
+        "platform": "python",
+        "received": occurrence.detection_time,
+        "tags": {},
+    }
+    return occurrence, event_data
 
 
 class TestKeyBuilders(unittest.TestCase):
@@ -211,49 +324,7 @@ def status_change_comparator(self: StatusChangeMessage, other: StatusChangeMessa
     )
 
 
-class StatefulDetectorHandlerTestMixin(TestCase):
-    __test__ = Abstract(__module__, __qualname__)
-
-    def setUp(self):

Leaderboard for this case

easy · 7 of 9 bots fully caught it (78% solve rate) · 78% avg coverage.

#BotResultCaughtCoverageFindingsFalse alarms
1 Claude Code (Opus 5.5) solved 1/1 100.0% 1 0
2 Cubic solved 1/1 100.0% 3 0
3 Drape (Deep Review) solved 1/1 100.0% 1 0
4 Drape (Review) solved 1/1 100.0% 2 0
5 greptile solved 1/1 100.0% 1 0
6 Mjolnir (Opus 5.5) solved 1/1 100.0% 2 0
7 Mjolnir (Sonnet 5.5) solved 1/1 100.0% 2 0
8 cursor miss 0/1 0.0% 0 0
9 qodo miss 0/1 0.0% 0 0

Bot results

Claude Code (Opus 5.5) · cross-file engine perris 0.11.1+proven.tiered0.25.call.read.subclass.type
DefectSeverityFoundGrade
d1 high ✓ exact
valid · matches d1 · impact high
raw comments (1)
src/sentry/incidents/grouptype.py:11
`MetricAlertDetectorHandler` now subclasses `StatefulDetectorHandler` with only `pass`, so it never implements the abstract members `counter_names`, `get_dedupe_value`, `get_group_key_values` or the new `build_occurrence_and_event_data`. That makes it an abstract class. `MetricAlertFire.detector_handler` still points to it, so `Detector.detector_handler` (`group_type.detector_handler(self)`) raises `TypeError: Can't instantiate abstract class` for any detector of type `metric_alert_fire`. `process_detectors` does not catch exceptions per detector, so one such detector aborts the whole batch, and the other detectors in the list are neither evaluated nor have their state committed. The old class was concrete and returned an empty result. This one is a crash waiting to happen.
Cubic · cross-file engine perris 0.11.1+proven.tiered0.25.call.read.subclass.type
DefectSeverityFoundGrade
d1 high ✓ off by one
valid · matches d1 · impact high
minor · impact low
The TODO comment says the class will become stateful later, but the change already makes it extend StatefulDetectorHandler, so the comment is now out of date. This is a documentation issue and doesn't change how the code behaves. [grounded: tree src/sentry/incidents/grouptype.py] [consensus 5/5: minor=5]
minor · impact low
The helper build_mock_occurrence_and_event ignores its value argument, so the test passes today. The mismatch only misleads readers and would only fail if the helper changed later, so this is a test-clarity note, not a behaviour defect. [grounded: diff] [consensus 5/5: minor=5]
raw comments (4)
src/sentry/incidents/grouptype.py:11
<!-- cubic:v=a3c2c4e8-14c9-4d70-8272-04526682f0f7 --> <!-- cubic:review-run=29f76a05-a8d7-50c9-8a2c-350f4776cb7f --> <!-- metadata:{"confidence":9} --> P2: `MetricAlertDetectorHandler` now subclasses the abstract `StatefulDetectorHandler` but implements none of its abstract members (`counter_names`, `get_dedupe_value`, `get_group_key_values`, `build_occurrence_and_event_data`), so it stays abstract and can never be instantiated. The previous class implemented `evaluate` (the only abstract method of `DetectorHandler`) and was concrete. `MetricAlertFire.detector_handler` still points at this class, and `Detector.detector_handler` (workflow_engine/models/detector.py:86) instantiates `group_type.detector_handler(self)`; the first time a Detector is created for this group type that call raises `TypeError: Can't instantiate abstract class MetricAlertDetectorHandler`. Either drop the ``detector_handler = MetricAlertDetectorHandler`` registration or give the class concrete stub implementations of the abstract methods. <details> <summary>Prompt for AI agents</summary> ```text Check if this issue is valid — if so, understand the root cause and fix it. At src/sentry/incidents/grouptype.py, line 11: <comment>`MetricAlertDetectorHandler` now subclasses the abstract `StatefulDetectorHandler` but implements none of its abstract members (`counter_names`, `get_dedupe_value`, `get_group_key_values`, `build_occurrence_and_event_data`), so it stays abstract and can never be instantiated. The previous class implemented `evaluate` (the only abstract method of `DetectorHandler`) and was concrete. `MetricAlertFire.detector_handler` still points at this class, and `Detector.detector_handler` (workflow_engine/models/detector.py:86) instantiates `group_type.detector_handler(self)`; the first time a Detector is created for this group type that call raises `TypeError: Can't instantiate abstract class MetricAlertDetectorHandler`. Either drop the ``detector_handler = MetricAlertDetectorHandler`` registration or give the class concrete stub implementations of the abstract methods.</comment> <file context> @@ -4,17 +4,12 @@ - ) -> list[DetectorEvaluationResult]: - # TODO: Implement - return [] +class MetricAlertDetectorHandler(StatefulDetectorHandler[QuerySubscriptionUpdate]): + pass </file context> ``` </details>
src/sentry/incidents/grouptype.py:11
<!-- cubic:v=83e1d609-eaa4-4ded-965b-4792cdec7af8 --> <!-- cubic:review-run=29f76a05-a8d7-50c9-8a2c-350f4776cb7f --> <!-- metadata:{"confidence":10} --> P3: The comment above the class says "This will be a stateful detector when we build that abstraction," but this change makes the class extend the already-built `StatefulDetectorHandler`. Delete or update the comment so it matches the code. <details> <summary>Prompt for AI agents</summary> ```text Check if this issue is valid — if so, understand the root cause and fix it. At src/sentry/incidents/grouptype.py, line 11: <comment>The comment above the class says "This will be a stateful detector when we build that abstraction," but this change makes the class extend the already-built `StatefulDetectorHandler`. Delete or update the comment so it matches the code.</comment> <file context> @@ -4,17 +4,12 @@ - ) -> list[DetectorEvaluationResult]: - # TODO: Implement - return [] +class MetricAlertDetectorHandler(StatefulDetectorHandler[QuerySubscriptionUpdate]): + pass </file context> ``` </details>
tests/sentry/workflow_engine/processors/test_detector.py:192
<!-- cubic:v=00405ed7-8e26-48bd-b9db-c733b525da08 --> <!-- cubic:review-run=29f76a05-a8d7-50c9-8a2c-350f4776cb7f --> <!-- metadata:{"confidence":6} --> P3: In `test_state_results_multi_group`, the data packet assigns `group_2` the value 10, but the expected occurrence for `group_2` is built with `6`. `build_mock_occurrence_and_event` currently ignores its `value` argument, so the test still passes, but the mismatch makes the expectation misleading and will silently break if the helper ever uses `value`. Pass 10 to keep the expected result consistent with the input packet. <details> <summary>Prompt for AI agents</summary> ```text Check if this issue is valid — if so, understand the root cause and fix it. At tests/sentry/workflow_engine/processors/test_detector.py, line 190: <comment>In `test_state_results_multi_group`, the data packet assigns `group_2` the value 10, but the expected occurrence for `group_2` is built with `6`. `build_mock_occurrence_and_event` currently ignores its `value` argument, so the test still passes, but the mismatch makes the expectation misleading and will silently break if the helper ever uses `value`. Pass 10 to keep the expected result consistent with the input packet.</comment> <file context> @@ -73,64 +138,88 @@ def test(self): + occurrence, + event_data, + ) + occurrence_2, event_data_2 = build_mock_occurrence_and_event( + detector.detector_handler, "group_2", 6, PriorityLevel.HIGH ) </file context> ``` </details> ```suggestion occurrence_2, event_data_2 = build_mock_occurrence_and_event( detector.detector_handler, "group_2", 10, PriorityLevel.HIGH ) ```
PR-level
<!-- cubic:review-summary:start --> **3 issues found** across 4 files <!-- cubic:review-summary:end --> <details> <summary>Prompt for AI agents (unresolved issues)</summary> ```text Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately. <file name="src/sentry/incidents/grouptype.py"> <violation number="1" location="src/sentry/incidents/grouptype.py:11"> P3: The comment above the class says "This will be a stateful detector when we build that abstraction," but this change makes the class extend the already-built `StatefulDetectorHandler`. Delete or update the comment so it matches the code.</violation> <violation number="2" location="src/sentry/incidents/grouptype.py:11"> P2: `MetricAlertDetectorHandler` now subclasses the abstract `StatefulDetectorHandler` but implements none of its abstract members (`counter_names`, `get_dedupe_value`, `get_group_key_values`, `build_occurrence_and_event_data`), so it stays abstract and can never be instantiated. The previous class implemented `evaluate` (the only abstract method of `DetectorHandler`) and was concrete. `MetricAlertFire.detector_handler` still points at this class, and `Detector.detector_handler` (workflow_engine/models/detector.py:86) instantiates `group_type.detector_handler(self)`; the first time a Detector is created for this group type that call raises `TypeError: Can't instantiate abstract class MetricAlertDetectorHandler`. Either drop the ``detector_handler = MetricAlertDetectorHandler`` registration or give the class concrete stub implementations of the abstract methods.</violation> </file> <file name="tests/sentry/workflow_engine/processors/test_detector.py"> <violation number="1" location="tests/sentry/workflow_engine/processors/test_detector.py:190"> P3: In `test_state_results_multi_group`, the data packet assigns `group_2` the value 10, but the expected occurrence for `group_2` is built with `6`. `build_mock_occurrence_and_event` currently ignores its `value` argument, so the test still passes, but the mismatch makes the expectation misleading and will silently break if the helper ever uses `value`. Pass 10 to keep the expected result consistent with the input packet.</violation> </file> ``` </details> <sub>Reply with feedback, questions, or to request a fix.<br /><br />[Re-trigger cubic](https://www.cubic.dev/action/re-review/pr/drape-io/crbench-cubic-2026-09/94/ai_pr_review_github_delivery_b1c21e57ffe44516c93a4f6e04e55f8585e6b62062b7720f4b87d248e0dd4ef0?returnTo=https%3A%2F%2Fgithub.com%2Fdrape-io%2Fcrbench-cubic-2026-09%2Fpull%2F94)</sub> <!-- cubic:review-post:ai_pr_review_github_delivery_b1c21e57ffe44516c93a4f6e04e55f8585e6b62062b7720f4b87d248e0dd4ef0:338430b7f13e3bbb0a24f572f23879e8fd32c098:29f76a05-a8d7-50c9-8a2c-350f4776cb7f -->
cursor · cross-file engine perris 0.11.1+proven.tiered0.25.call.read.subclass.type
DefectSeverityFoundGrade
d1 high ✗ —
Drape (Deep Review) · cross-file engine perris 0.11.1+proven.tiered0.25.call.read.subclass.type
DefectSeverityFoundGrade
d1 high ✓ off by one
valid · matches d1 · impact high
raw comments (1)
src/sentry/incidents/grouptype.py:11
**Metric alert detector handler can't be instantiated** `MetricAlertDetectorHandler` now subclasses `StatefulDetectorHandler` but has only `pass`, so the abstract `counter_names`, `get_dedupe_value`, `get_group_key_values` and `build_occurrence_and_event_data` are missing. Any `metric_alert_fire` detector raises TypeError in `detector_handler` instead of evaluating. Implement the abstract members, or keep a concrete `evaluate` stub until the stateful version is ready. ``` src/sentry/incidents/grouptype.py:11 class MetricAlertDetectorHandler(StatefulDetectorHandler[QuerySubscriptionUpdate]): src/sentry/incidents/grouptype.py:12 pass src/sentry/incidents/grouptype.py:12 (base) class MetricAlertDetectorHandler(DetectorHandler[QuerySubscriptionUpdate]): src/sentry/incidents/grouptype.py:13 (base) def evaluate( src/sentry/workflow_engine/processors/detector.py:142 @property src/sentry/workflow_engine/processors/detector.py:143 @abc.abstractmethod src/sentry/workflow_engine/processors/detector.py:144 def counter_names(self) -> list[str]: src/sentry/workflow_engine/processors/detector.py:145 """ src/sentry/workflow_engine/processors/detector.py:146 The names of counters that this detector is going to keep track of. src/sentry/workflow_engine/processors/detector.py:147 """ ```
Drape (Review) · cross-file engine perris 0.11.1+proven.tiered0.25.call.read.subclass.type
DefectSeverityFoundGrade
d1 high ✓ off by one
minor · impact low
The change switched the return type to a dict, but the docstring still says it returns a list. The docstring is out of date, but the code works correctly, so this is a documentation note rather than a behaviour defect. [grounded: tree src/sentry/workflow_engine/processors/detector.py] [consensus 3/4: minor=3, valid=1]
valid · matches d1 · impact high
raw comments (2)
src/sentry/workflow_engine/processors/detector.py:230
**Docstring still says evaluate returns a list** `StatefulDetectorHandler.evaluate` now returns a dict keyed by group key, but its docstring still says it returns a list of `DetectorEvaluationResult`. A caller who trusts it will iterate the keys instead of the results. Update the docstring to say it returns a dict keyed by group key. ``` src/sentry/workflow_engine/processors/detector.py:226 def evaluate( src/sentry/workflow_engine/processors/detector.py:227 self, data_packet: DataPacket[T] src/sentry/workflow_engine/processors/detector.py:228 ) -> dict[DetectorGroupKey, DetectorEvaluationResult]: src/sentry/workflow_engine/processors/detector.py:229 """ ```
src/sentry/incidents/grouptype.py:11
**MetricAlertDetectorHandler is abstract and cannot be instantiated** `MetricAlertDetectorHandler` now subclasses `StatefulDetectorHandler` with an empty body. It leaves `counter_names`, `get_dedupe_value`, `get_group_key_values` and `build_occurrence_and_event_data` abstract, so constructing it raises `TypeError`. `MetricAlertFire` is registered with this handler. Implement the abstract methods on MetricAlertDetectorHandler (stub them out if needed), or keep the old stub evaluate until they exist. ``` src/sentry/incidents/grouptype.py:7 from sentry.workflow_engine.processors.detector import StatefulDetectorHandler src/sentry/incidents/grouptype.py:10 # TODO: This will be a stateful detector when we build that abstraction src/sentry/incidents/grouptype.py:11 class MetricAlertDetectorHandler(StatefulDetectorHandler[QuerySubscriptionUpdate]): src/sentry/incidents/grouptype.py:12 pass src/sentry/workflow_engine/processors/detector.py:142 @property src/sentry/workflow_engine/processors/detector.py:143 @abc.abstractmethod src/sentry/workflow_engine/processors/detector.py:144 def counter_names(self) -> list[str]: src/sentry/workflow_engine/processors/detector.py:145 """ src/sentry/workflow_engine/processors/detector.py:146 The names of counters that this detector is going to keep track of. src/sentry/workflow_engine/processors/detector.py:147 """ ```
greptile · cross-file engine perris 0.11.1+proven.tiered0.25.call.read.subclass.type
DefectSeverityFoundGrade
d1 high ✓ exact
valid · matches d1 · impact high
raw comments (2)
src/sentry/incidents/grouptype.py:12
<a href="#"><img alt="P1" src="https://greptile-static-assets.s3.amazonaws.com/badges/p1.svg?v=9" align="top"></a> **Metric handler is abstract** When a `metric_alert_fire` detector is processed, `Detector.detector_handler` tries to instantiate `MetricAlertDetectorHandler`. This class now inherits from `StatefulDetectorHandler` but does not implement its required abstract members, including `counter_names`, `get_dedupe_value`, `get_group_key_values`, and `build_occurrence_and_event_data`. Python therefore raises `TypeError` before detector evaluation; previously, this handler could be instantiated and returned no results.
PR-level
<h3>Greptile Summary</h3> This PR changes stateful detector evaluation results from lists to group-keyed dictionaries and adds occurrence generation for non-OK transitions. - Adds a reusable `Detector.group_type` property. - Introduces an abstract occurrence/event builder for stateful handlers. - Publishes generated occurrences while processing detector results. - Updates detector processor tests for the new result shape and occurrence publishing. - Leaves the registered metric-alert handler abstract and therefore unconstructable. <h3>Confidence Score: 4/5</h3> The PR is not safe to merge until the registered metric-alert handler can be instantiated. Processing a `metric_alert_fire` detector now attempts to instantiate an empty subclass with several unimplemented abstract members, raising `TypeError` before detector evaluation. **Files Needing Attention:** src/sentry/incidents/grouptype.py <details><summary><h3>Important Files Changed</h3></summary> | Filename | Overview | |----------|----------| | src/sentry/incidents/grouptype.py | Converts the metric-alert handler to the stateful abstraction without implementing its required abstract members, preventing construction. | | src/sentry/workflow_engine/models/detector.py | Adds a group-type lookup property and reuses it during handler resolution. | | src/sentry/workflow_engine/processors/detector.py | Returns keyed evaluation results and generates occurrences for non-OK state transitions. | | tests/sentry/workflow_engine/processors/test_detector.py | Updates stateful detector tests for dictionary results and Kafka occurrence publication but does not instantiate the production metric-alert handler. | </details> <details open><summary><h3>Flowchart</h3></summary> ```mermaid %%{init: {'theme': 'neutral'}}%% flowchart TD A[process_detectors] --> B[Detector.detector_handler] B --> C[Resolve GroupType by detector type] C --> D[Instantiate registered handler] D -->|metric_alert_fire| E[MetricAlertDetectorHandler] E --> F{All abstract methods implemented?} F -->|No| G[TypeError before evaluation] F -->|Yes| H[Evaluate grouped values] H --> I[Build occurrence or resolution] I --> J[Publish and commit state] ``` </details> <!-- greptile_other_comments_section --> <sub>Reviews (1): Last reviewed commit: ["Update src/sentry/incidents/grouptype.py..."](https://github.com/drape-io/crbench-greptile-2026-09/commit/a698ec32152967f292644cdeba374c068e3253ea) | [Re-trigger Greptile](https://app.greptile.com/api/retrigger?id=64289021)</sub>
Mjolnir (Opus 5.5) · cross-file engine perris 0.11.1+proven.tiered0.25.call.read.subclass.type
DefectSeverityFoundGrade
d1 high ✓ off by one
valid · matches d1 · impact high
valid · impact medium
Before the change, MetricAlertDetectorHandler was concrete because it implemented `evaluate`. Now it subclasses StatefulDetectorHandler with only `pass` and leaves counter_names, get_dedupe_value, get_group_key_values and the new build_occurrence_and_event_data abstract. Detector.detector_handler instantiates it via `group_type.detector_handler(self)` with no try/except, and process_detectors doesn't catch the error either, so the TypeError ends the whole loop. [grounded: diff] [consensus 3/3: valid=3]
raw comments (2)
PR-level
[P2] MetricAlertDetectorHandler is now an abstract class that cannot be instantiated src/sentry/incidents/grouptype.py:11-12 `MetricAlertDetectorHandler` now subclasses `StatefulDetectorHandler` but its body is only `pass`. That leaves `counter_names`, `get_dedupe_value`, `get_group_key_values` and the new `build_occurrence_and_event_data` abstract. Before this change the class could be instantiated, and its `evaluate` returned `[]`. Now any detector with `type="metric_alert_fire"` breaks when `Detector.detector_handler` runs `group_type.detector_handler(self)`: it raises `TypeError: Can't instantiate abstract class`. You would expect it to return a handler, or `None` with a logged error. So once metric alert detectors go through `process_detectors`, processing fails with an exception rather than being a no-op. Severity depends on whether these detectors are processed yet, since `MetricAlertFire` is registered in the grouptype registry but `process_detectors` has no production caller today. To keep the class instantiable, give it stub implementations of the abstract methods.
src/sentry/incidents/grouptype.py:11
[P2] `MetricAlertDetectorHandler` is now abstract and can't be instantiated Before this change, `MetricAlertDetectorHandler` was a concrete handler whose `evaluate` returned `[]`. It now subclasses `StatefulDetectorHandler` with only `pass`, so it doesn't implement the abstract members `counter_names`, `get_dedupe_value`, `get_group_key_values` or the new `build_occurrence_and_event_data`. `MetricAlertFire.detector_handler` still points at this class, and `Detector.detector_handler` (models/detector.py) instantiates it with `group_type.detector_handler(self)`. As a result, reading `detector_handler` on any `Detector` with `type="metric_alert_fire"` raises `TypeError: Can't instantiate abstract class`. That happens on every call to `process_detectors` with such a detector, and the error is not caught, so the remaining detectors in the batch are not processed either. Unless the subclass is meant to stay unusable until it's implemented, it should provide stub implementations of these methods, as it did before.
Mjolnir (Sonnet 5.5) · cross-file engine perris 0.11.1+proven.tiered0.25.call.read.subclass.type
DefectSeverityFoundGrade
d1 high ✓ off by one
valid · impact medium
StatefulDetectorHandler declares abstract counter_names, get_dedupe_value, get_group_key_values and the new build_occurrence_and_event_data. MetricAlertDetectorHandler implements none of them, so `group_type.detector_handler(self)` raises TypeError for metric_alert_fire detectors. Before this change the handler was concrete and its evaluate returned []. [grounded: diff] [consensus 3/3: valid=3]
valid · matches d1 · impact high
raw comments (2)
src/sentry/incidents/grouptype.py:10
[P2] MetricAlertDetectorHandler is now an uninstantiable abstract class `MetricAlertDetectorHandler` now subclasses `StatefulDetectorHandler` with only `pass`. `StatefulDetectorHandler` has abstract methods (`counter_names`, `get_dedupe_value`, `get_group_key_values`, and the newly added `build_occurrence_and_event_data`), and none are implemented here. `MetricAlertFire` registers this class as `detector_handler`, so `Detector.detector_handler` will raise `TypeError: Can't instantiate abstract class` for any detector of type `metric_alert_fire`. Before this change the handler was a concrete class whose `evaluate` returned `[]`. `process_detectors` will crash for those detectors instead of no-oping. It only shows up if such detectors exist or are processed. Either keep the previous stub, or implement the abstract methods.
src/sentry/incidents/grouptype.py:11
[P2] MetricAlertDetectorHandler is now abstract and cannot be instantiated The class now subclasses `StatefulDetectorHandler` with only `pass`, but that base has unimplemented abstract members: `counter_names`, `get_dedupe_value`, `get_group_key_values`, and the newly added `build_occurrence_and_event_data`. `Detector.detector_handler` calls `group_type.detector_handler(self)`, so any Detector with type `metric_alert_fire` now raises `TypeError: Can't instantiate abstract class`. Before this change, the handler was concrete and returned `[]`. `process_detectors` would crash for such a detector instead of skipping it. The impact depends on whether a `metric_alert_fire` detector exists. The group type is registered, though, and the base class's abstract methods are not covered by the `pass` body
qodo · cross-file engine perris 0.11.1+proven.tiered0.25.call.read.subclass.type
DefectSeverityFoundGrade
d1 high ✗ —
raw comments (2)
PR-level
<h3>Qodo is busy working</h3> Check back in a few minutes. Qodo's code review agents are on it. <img src="https://www.qodo.ai/wp-content/uploads/2025/11/light-grey-line.svg" height="10%" alt="Grey Divider"> <img src="https://www.qodo.ai/wp-content/uploads/2026/01/anteater-looking-at-ants-01-transparent-bg-croped-ezgif.com-optimize-1.gif" width="30%">
PR-level
<h3>PR Summary by Qodo</h3> Emit issue occurrences from stateful detector transitions <code>✨ Enhancement</code> <code>🧪 Tests</code> <code>🕐 20-40 Minutes</code> <img src="https://www.qodo.ai/wp-content/uploads/2025/11/light-grey-line.svg" height="10%" alt="Grey Divider"> <details> <summary>AI Description</summary> <dl> <dd> <br/> ><pre> >• Emit issue occurrences when stateful detectors enter non-OK priority states. >• Key detector evaluations by group to guarantee one result per group. >• Expose detector group types and verify Kafka publication for grouped evaluations. ></pre> </dd> </dl> </details> <details> <summary>Diagram</summary> <dl> <dd> <br/> ```mermaid graph TD A["Data Packet"] --> B["Detector Model"] --> C["Group Type Registry"] --> D["Stateful Handler"] --> E{"Priority changed?"} E -- "Active" --> F["Issue Occurrence"] --> H["Kafka Producer"] E -- "Resolved" --> G["Status Change"] --> H ``` </dd> </dl> </details> <details> <summary>High-Level Assessment</summary> <dl> <dd> <br/> >The following are alternative approaches to this PR: <details> <summary><b>1. Centralized occurrence factory</b></summary> <dl> <dd> - ➕ Keeps detector handlers focused on condition and state evaluation. - ➕ Standardizes common occurrence and event fields across detector types. - ➖ Requires the processor to understand issue-type-specific titles, evidence, and event metadata. - ➖ Reduces extensibility as new detector types require central factory changes. </dd> </dl> </details> <details> <summary><b>2. Retain list-based evaluation results</b></summary> <dl> <dd> - ➕ Preserves the existing handler return contract and result ordering. - ➕ Minimizes changes for any external handler implementations. - ➖ Requires runtime duplicate detection and error handling. - ➖ Provides a weaker guarantee that each detector group produces at most one result. </dd> </dl> </details> >**Recommendation:** Keep the PR’s dictionary-based result contract and handler-specific occurrence hook. Group keys naturally enforce uniqueness, while individual handlers remain responsible for issue-type-specific occurrence and event construction. </dd> </dl> </details> <details> <summary> Files changed (4) <code> +249 / -151 </code> </summary> <dl> <dd> <br/> <details> <summary>Enhancement (2) <code> +25 / -31 </code></summary> <dl> <dd> <details> <summary>grouptype.py<code>Adopt the stateful handler abstraction for metric alerts</code> <code>+3/-8</code></summary> <br/> >Adopt the stateful handler abstraction for metric alerts > ><pre> >• Changes the placeholder metric alert detector handler to inherit from StatefulDetectorHandler, aligning metric alerts with stateful transition processing. ></pre> > ><a href='https://github.com/drape-io/crbench-qodo-2026-09/pull/71/files#diff-81f569e5989a9b26aad32b54a29a451eb9aa1fb290ffa3b753375db4a8887d0e'>src/sentry/incidents/grouptype.py</a> </details> <details> <summary>detector.py<code>Build and publish occurrences for active detector transitions</code> <code>+22/-23</code></summary> <br/> >Build and publish occurrences for active detector transitions > ><pre> >• Changes detector evaluations from lists to dictionaries keyed by detector group. Stateful handlers now construct issue occurrences for non-OK transitions, while OK transitions continue producing resolution messages for Kafka publication. ></pre> > ><a href='https://github.com/drape-io/crbench-qodo-2026-09/pull/71/files#diff-5be652c4f9cfde75ca59b124eefeb643a1147a011cea3cd328b89e27b8e514c1'>src/sentry/workflow_engine/processors/detector.py</a> </details> </dd> </dl> </details> <details> <summary>Refactor (1) <code> +7 / -1 </code></summary> <dl> <dd> <details> <summary>detector.py<code>Expose the detector’s registered group type</code> <code>+7/-1</code></summary> <br/> >Expose the detector’s registered group type > ><pre> >• Adds a reusable group_type property that resolves the detector type slug through the group type registry. Handler lookup now uses this property instead of repeating registry access. ></pre> > ><a href='https://github.com/drape-io/crbench-qodo-2026-09/pull/71/files#diff-28c8dcf5ffaab310fa97eaae882ed40de50aec0792e6c4b72df65ea1f20bbe71'>src/sentry/workflow_engine/models/detector.py</a> </details> </dd> </dl> </details> <details> <summary>Tests (1) <code> +217 / -119 </code></summary> <dl> <dd> <details> <summary>test_detector.py<code>Test keyed evaluations and occurrence publication</code> <code>+217/-119</code></summary> <br/> >Test keyed evaluations and occurrence publication > ><pre> >• Refactors shared stateful-handler fixtures and updates assertions for dictionary results. Adds occurrence construction, deterministic timestamps, and Kafka publication coverage for single-group and multi-group detector transitions. ></pre> > ><a href='https://github.com/drape-io/crbench-qodo-2026-09/pull/71/files#diff-62d03a543b4e91d4c9c65f67127e8008e25a861ba7d82a4878b66442ba814093'>tests/sentry/workflow_engine/processors/test_detector.py</a> </details> </dd> </dl> </details> </dd> </dl> </details>