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.

66.4 KB
Raw Blame
"""Temporal activities for logs alerting."""

import gc
import time
import asyncio
import contextlib
import dataclasses
from collections import defaultdict
from collections.abc import Iterator, Sequence
from datetime import UTC, datetime, timedelta
from itertools import batched
from uuid import UUID

from django.db import transaction
from django.db.utils import IntegrityError

import structlog
import temporalio.activity
from pydantic import ValidationError as PydanticValidationError

from posthog.schema import PropertyGroupFilter

from posthog.exceptions_capture import capture_exception
from posthog.kafka_client.client import ProduceResult
from posthog.models import Team
from posthog.slo.context import SloHandle, SloSpec, slo_operation
from posthog.slo.types import SloArea, SloOperation
from posthog.sync import database_sync_to_async_pool

from products.alerts.backend.facade.destinations import (
    alert_internal_event_delivered,
    flush_alert_internal_events,
    produce_alert_internal_event,
)
from products.alerts_platform.backend.facade.delivery_slo import alert_delivery_slo
from products.logs.backend.alert_check_query import (
    AlertCheckQuery,
    BatchedAlertCheckQuery,
    BatchedBucketedResult,
    BucketedCount,
    fetch_live_logs_checkpoint,
    is_projection_eligible,
    resolve_alert_date_to,
)
from products.logs.backend.alert_destinations import LOGS_ALERT_INCIDENT_CLOSED_EVENT, LOGS_ALERT_INCIDENT_OPENED_EVENT
from products.logs.backend.alert_error_classifier import (
    AlertErrorCode,
    classify as classify_alert_error,
)
from products.logs.backend.alert_event_properties import failure_event_properties, threshold_event_properties
from products.logs.backend.alert_incidents import close_incident, has_incident_destination, incident_closed_properties
from products.logs.backend.alert_lookback import rolling_check_lookback_minutes
from products.logs.backend.alert_signal_emitter import (
    NotifiedAlert,
    emit_alert_state_change_signal,
    signal_action_and_weight,
)
from products.logs.backend.alert_state_machine import (
    AlertCheckOutcome,
    AlertState,
    CheckResult,
    ControlPlaneOutcome,
    IncidentCloseReason,
    IncidentEdge,
    NotificationAction,
    apply_outcome,
    evaluate_alert_check,
    incident_edge,
)
from products.logs.backend.alert_utils import (
    advance_next_check_at,
    compute_shard_offset_seconds,
    due_alerts_q,
    next_allowed_check_at,
)
from products.logs.backend.models import LogsAlertConfiguration, LogsAlertEvent
from products.logs.backend.temporal.constants import (
    EMIT_SIGNAL_CONCURRENCY,
    MAX_ALERT_COHORT_SIZE,
    MAX_COHORTS_PER_BATCH,
    MAX_CONCURRENT_BATCHES,
    MAX_CONCURRENT_COHORTS_PER_BATCH,
    NOTIFICATION_FLUSH_TIMEOUT_SECONDS,
)
from products.logs.backend.temporal.metrics import (
    increment_check_errors,
    increment_checkpoint_unavailable,
    increment_checks_total,
    increment_cohort_query_fallback,
    increment_cohort_save_fallback,
    increment_notification_failures,
    increment_state_transition,
    record_alerts_active,
    record_check_duration,
    record_checkpoint_lag,
    record_clickhouse_duration,
    record_cohort_event_insert_duration,
    record_cohort_save_duration,
    record_cohort_size,
    record_cohort_update_duration,
    record_scheduler_lag,
    record_worker_memory_snapshot,
)

logger = structlog.get_logger(__name__)


def _log_metric_failure(label: str, e: BaseException, **context: object) -> None:
    """Warn about a best-effort metric failure WITHOUT a traceback.

    `logger.exception` sets `exc_info=True`, making the underlying `LogRecord`
    retain the exception's `__traceback__` — and through it, every frame's
    `f_locals`. Handlers that buffer records (Sentry breadcrumbs, pytest log
    capture) then pin the entire cohort state alive across activity invocations.
    Metric failures are non-actionable; label + error type + message is enough.
    """
    logger.warning(
        "Failed to record metric",
        label=label,
        error=str(e),
        error_type=type(e).__name__,
        **context,
    )


def _safe_record(label: str, fn, *args, **kwargs) -> None:
    """Best-effort metric recording — failures must never break alerting."""
    try:
        fn(*args, **kwargs)
    except Exception as e:
        _log_metric_failure(label, e)


@contextlib.contextmanager
def _safe_record_block(label: str, **context: object) -> Iterator[None]:
    """Context-manager form of `_safe_record` for try-blocks wrapping multiple
    metric calls (where extracting them into a single callable would obscure
    branching logic). Same warning-without-traceback semantics."""
    try:
        yield
    except Exception as e:
        _log_metric_failure(label, e, **context)


def _post_cohort_memory_cleanup() -> None:
    """gc.collect() then snapshot the post-GC live-object count. Called after each cohort."""
    try:
        gc.collect()
    except Exception:
        logger.exception("gc.collect failed in post-cohort cleanup")
    _safe_record("worker_gc_objects gauge", record_worker_memory_snapshot)


def _derive_breaches(
    buckets: list[BucketedCount],
    threshold_count: int,
    threshold_operator: str,
    evaluation_periods: int,
) -> tuple[bool, ...]:
    """Map ASC-ordered bucketed CH counts to a newest-first breach tuple of length M.

    CH's `GROUP BY` only emits buckets that have data. The state machine needs M
    data points regardless of how sparse the underlying log volume is — so we
    pad the result to `evaluation_periods` with the implicit count=0 outcome:
    `False` for `above` (0 < threshold), `True` for `below` (0 < threshold given
    the model's min_value=1 validator).

    Without this pad, a `below` alert on a truly silent service would never
    fire — CH returns no buckets, the breach tuple is empty, and the N-of-M
    evaluator never sees the implicit "count is below threshold" signal.
    """
    if threshold_operator == "above":
        actual = tuple(b.count > threshold_count for b in reversed(buckets))
        missing_breach = False
    else:
        actual = tuple(b.count < threshold_count for b in reversed(buckets))
        missing_breach = True
    pad = (missing_breach,) * max(0, evaluation_periods - len(actual))
    return actual + pad


@dataclasses.dataclass(frozen=True)
class CheckAlertsInput:
    pass


@dataclasses.dataclass(frozen=True)
class _AlertCohort:
    """Group of alerts that share team + bucket grid and can be batched into one CH query.

    Cohort key matches `_cohort_manifests_from_alerts`: (team_id, window_minutes,
    evaluation_periods, check_interval_minutes, projection_eligible, date_to).
    """

    alerts: tuple[LogsAlertConfiguration, ...]
    date_to: datetime
    projection_eligible: bool

    @property
    def team(self) -> "Team":
        return self.alerts[0].team

    @property
    def team_id(self) -> int:
        return self.alerts[0].team_id

    @property
    def window_minutes(self) -> int:
        return self.alerts[0].window_minutes

    @property
    def evaluation_periods(self) -> int:
        return self.alerts[0].evaluation_periods

    @property
    def check_interval_minutes(self) -> int:
        return self.alerts[0].check_interval_minutes

    @property
    def date_from(self) -> datetime:
        return self.date_to - timedelta(
            minutes=rolling_check_lookback_minutes(
                self.window_minutes, self.check_interval_minutes, self.evaluation_periods
            )
        )


@dataclasses.dataclass(frozen=True)
class CohortManifest:
    """Lightweight cohort identifier passed between workflow and activities.

    Plain JSON-friendly types only — `datetime` becomes ISO string, UUIDs become
    str, alert_ids is `list[str]` (NOT tuple — Temporal's default converter
    deserialises JSON arrays to lists, not tuples).
    """

    team_id: int
    projection_eligible: bool
    date_to_iso: str
    alert_ids: list[str]


@dataclasses.dataclass(frozen=True)
class _PrefetchedQuery:
    """CH query result for one alert: either `buckets` or `error` is set."""

    buckets: list[BucketedCount] | None = None
    query_duration_ms: int | None = None
    error: Exception | None = None


@dataclasses.dataclass(frozen=True)
class _CohortQueryResult:
    per_alert: dict[str, _PrefetchedQuery]

    def for_alert(self, alert: LogsAlertConfiguration) -> _PrefetchedQuery:
        return self.per_alert.get(str(alert.id), _PrefetchedQuery(buckets=[]))


@dataclasses.dataclass(frozen=True)
class _AlertEvaluation:
    """Phase 1 output: per-alert state-machine result. No Kafka, no PG yet.

    Carries the raw `outcome` (pre-dispatch) — Kafka failure is decided in
    Phase 2 and may roll the state back to `state_before`.
    """

    alert: LogsAlertConfiguration
    outcome: AlertCheckOutcome
    check_result: CheckResult
    date_from: datetime
    date_to: datetime
    state_before: str


@dataclasses.dataclass(frozen=True)
class _DispatchedAlert:
    """Phase 2 output: notification dispatched, ready for the cohort bulk save.

    `notification_failed` is the source of truth for state rollback: if True,
    the state machine's `new_state` is reset to the alert's pre-check value so
    the next cycle re-evaluates and re-tries the notification, and
    `consecutive_failures` may heal downward but never advance. The counter
    matters as much as the state: the error notification fires on the 0 -> 1
    failure-counter edge, so persisting an advanced counter after a failed
    enqueue would silence the retry forever. A successful evaluation's counter
    reset is kept, though — only delivery failed, and voiding the evidence that
    evaluation recovered would let a later error skip its notify edge and drag
    a stale streak toward BROKEN.

    `produce_result` is the pending Kafka delivery for this alert's
    notification (None when no notification was attempted or the enqueue itself
    failed). `incident_produce_result` is the same for the incident edge event,
    and a failed delivery of either rolls the state back. `produce()` only
    buffers locally, so `notification_failed` isn't final until
    `_resolve_notification_deliveries` flushes the producer and folds delivery
    failures in — persisting before that point would record "notified" for a
    message that may never reach the broker.

    Delivery is therefore at least once. For an alert with both an incident
    destination and a message destination, a lost incident event also resends a
    notification the broker already took. Retrying only the incident event would
    need the pending edge stored on the alert row.
    """

    evaluation: _AlertEvaluation
    notification_failed: bool
    produce_result: ProduceResult | None = None
    incident_produce_result: ProduceResult | None = None
    suppressed_by_quiet_hours: bool = False
    shadowed: bool = False

    @property
    def produced(self) -> bool:
        return self.produce_result is not None or self.incident_produce_result is not None

    @property
    def committed_outcome(self) -> AlertCheckOutcome:
        if self.notification_failed:
            return dataclasses.replace(
                self.evaluation.outcome,
                new_state=AlertState(self.evaluation.alert.state),
                consecutive_failures=min(
                    self.evaluation.alert.consecutive_failures,
                    self.evaluation.outcome.consecutive_failures,
                ),
            )
        return self.evaluation.outcome


@dataclasses.dataclass(frozen=True)
class CheckAlertsOutput:
    alerts_checked: int
    alerts_fired: int
    alerts_resolved: int
    alerts_errored: int


@dataclasses.dataclass(frozen=True)
class DiscoverCohortsInput:
    pass


@dataclasses.dataclass(frozen=True)
class DiscoverCohortsOutput:
    manifests: list[CohortManifest]
    # Recorded in workflow history so replays chunk identically even if the env
    # var changes between runs.
    batch_size: int
    # Recorded for the same reason. The default keeps histories written before this
    # field existed replaying with no limit, which is what they ran with.
    max_concurrent_batches: int = 0


@dataclasses.dataclass(frozen=True)
class EvaluateCohortBatchInput:
    manifests: list[CohortManifest]


@dataclasses.dataclass(frozen=True)
class EvaluateCohortBatchOutput:
    alerts_checked: int
    alerts_fired: int
    alerts_resolved: int
    alerts_errored: int
    notified: list[NotifiedAlert] = dataclasses.field(default_factory=list)


@dataclasses.dataclass(frozen=True)
class EmitAlertSignalsInput:
    notified: list[NotifiedAlert]


def _due_alerts_qs(now: datetime):
    return LogsAlertConfiguration.objects.filter(
        due_alerts_q(
            now,
            broken_state=LogsAlertConfiguration.State.BROKEN,
            snoozed_state=LogsAlertConfiguration.State.SNOOZED,
        )
    )


@temporalio.activity.defn
async def discover_cohorts_activity(input: DiscoverCohortsInput) -> DiscoverCohortsOutput:
    """Phase 1: lightweight discovery. Returns serialisable manifests; no full ORM hydration.

    Uses `.values()` to pull only the columns needed for grouping — at scale the
    full `select_related('team')` would OOM. Team objects are loaded later, only
    inside `evaluate_cohort_batch_activity`, scoped to a small batch.
    """
    return await database_sync_to_async_pool(_discover_cohorts_sync)()


def _discover_cohorts_sync() -> DiscoverCohortsOutput:
    now = datetime.now(UTC)
    rows = list(
        _due_alerts_qs(now).values(
            "id",
            "team_id",
            "window_minutes",
            "evaluation_periods",
            "check_interval_minutes",
            "filters",
            "next_check_at",
            "schedule_restriction",
        )
    )
    rescheduled_alert_ids = _reschedule_due_alerts_in_quiet_hours(rows, now)
    rows = [row for row in rows if row["id"] not in rescheduled_alert_ids]

    _safe_record("alerts_active gauge", record_alerts_active, len(rows))

    checkpoint: datetime | None = None
    if rows:
        try:
            # Inline import: Django model imports trip Temporal's workflow
            # sandbox (gettext.GNUTranslations.__mro_entries__). Activities
            # don't run inside the sandbox, but workflow.py imports this
            # module to reference the activity callable, which forces
            # module-level evaluation. Keep this deferred.
            from posthog.models import Team

            first_team = Team.objects.get(pk=rows[0]["team_id"])
            checkpoint = fetch_live_logs_checkpoint(first_team)
        except Exception as e:
            logger.exception(
                "Failed to fetch logs ingestion checkpoint; falling back to wall-clock",
                error=str(e),
            )

    with _safe_record_block("checkpoint metric"):
        if checkpoint is None:
            increment_checkpoint_unavailable()
        else:
            record_checkpoint_lag(now, checkpoint)

    manifests = _cohort_manifests_from_alerts(rows, now=now, checkpoint=checkpoint)
    # Read MAX_COHORTS_PER_BATCH inside the activity, not in workflow code:
    # module-level env reads are non-deterministic on replay because Temporal's
    # sandbox re-imports the workflow module each time.
    return DiscoverCohortsOutput(
        manifests=manifests,
        batch_size=MAX_COHORTS_PER_BATCH,
        max_concurrent_batches=MAX_CONCURRENT_BATCHES,
    )


def _reschedule_due_alerts_in_quiet_hours(rows: Sequence[dict], now: datetime) -> set[UUID]:
    alert_ids = [row["id"] for row in rows if row["schedule_restriction"]]
    if not alert_ids:
        return set()

    rescheduled_alert_ids: set[UUID] = set()
    with transaction.atomic():
        alerts = _due_alerts_qs(now).select_for_update(of=("self",)).select_related("team").filter(id__in=alert_ids)
        for alert in alerts:
            try:
                next_check_at = next_allowed_check_at(
                    now,
                    team_timezone=alert.team.timezone,
                    schedule_restriction=alert.schedule_restriction,
                )
            except Exception as e:
                logger.exception(
                    "Skipping alert with invalid quiet-hours configuration",
                    alert_id=str(alert.id),
                    team_id=alert.team_id,
                    error=str(e),
                )
                rescheduled_alert_ids.add(alert.id)
                continue
            if next_check_at <= now:
                continue
            alert.next_check_at = next_check_at
            alert.save(update_fields=["next_check_at", "updated_at"])
            rescheduled_alert_ids.add(alert.id)

    return rescheduled_alert_ids


def _detect_broken_filter_config(filters: object) -> str | None:
    """Returns a human-readable reason if `filters` is structurally broken in a way
    that will permanently prevent the alert from running. Returns None if the shape
    is valid enough to attempt evaluation.
    """
    if not isinstance(filters, dict):
        return f"filters must be a JSON object, got {type(filters).__name__}"
    filter_group = filters.get("filterGroup")
    if not filter_group:
        return None
    try:
        PropertyGroupFilter.model_validate(filter_group)
    except PydanticValidationError as e:
        return f"Invalid filterGroup shape: {e.errors()[0]['msg']}"
    return None


def _mark_alert_broken_for_bad_config(alert_id: str, reason: str) -> None:
    """Transition an alert to BROKEN because its configuration is structurally
    invalid and can never succeed. Runs in the same sync DB pool as discovery —
    each call is its own short transaction so a failure on one row doesn't
    roll back the others.

    A firing alert with an incident destination closes its incident first. Discovery skips BROKEN
    alerts, so the close must be delivered before the transition commits. When it is not, the alert
    keeps its state, and the next discovery cycle finds the same bad config and tries again.
    """
    try:
        state_seen = LogsAlertConfiguration.objects.values_list("state", flat=True).get(pk=alert_id)
        if state_seen == LogsAlertConfiguration.State.BROKEN:
            return
        if incident_edge(state_seen, AlertState.BROKEN) == IncidentEdge.CLOSED:
            alert_for_close = LogsAlertConfiguration.objects.get(pk=alert_id)
            if has_incident_destination(alert_for_close) and not close_incident(
                alert_for_close, IncidentCloseReason.BROKEN, flush_timeout_seconds=NOTIFICATION_FLUSH_TIMEOUT_SECONDS
            ):
                logger.warning("Deferring BROKEN until the incident close is delivered", alert_id=alert_id)
                return
        with transaction.atomic():
            alert = LogsAlertConfiguration.objects.select_for_update().get(pk=alert_id)
            if alert.state != state_seen:
                # The state moved after the close decision, so decide again on the next cycle.
                return
            state_before = alert.state
            outcome = ControlPlaneOutcome(
                new_state=AlertState.BROKEN,
                consecutive_failures=alert.consecutive_failures,
            )
            update_fields = apply_outcome(alert, outcome)
            LogsAlertEvent.objects.create(
                alert=alert,
                kind=LogsAlertEvent.Kind.BROKEN_CONFIG,
                threshold_breached=False,
                state_before=state_before,
                state_after=outcome.new_state.value,
                error_message=reason,
            )
            alert.save(update_fields=update_fields)
    except Exception as e:
        logger.exception(
            "Failed to mark alert BROKEN for invalid config",
            alert_id=alert_id,
            reason=reason,
            error=str(e),
        )
        capture_exception(e)


def _cohort_manifests_from_alerts(
    rows: Sequence[dict],
    *,
    now: datetime,
    checkpoint: datetime | None,
) -> list[CohortManifest]:
    """Group alert rows (from a `.values()` query) into `CohortManifest` objects.

    Cohort key + size-cap logic on plain dicts (no Django model hydration)
    so it stays cheap at scale and produces serialisable manifests for the
    workflow. Alerts with structurally broken filter configs are transitioned
    to BROKEN and excluded from the manifests — they would never succeed
    anyway, and one bad row must not brick discovery for the rest of the
    project.
    """
    grouped: defaultdict[tuple[int, int, int, int, bool, datetime], list[str]] = defaultdict(list)
    for row in rows:
        try:
            broken_reason = _detect_broken_filter_config(row["filters"])
            if broken_reason is not None:
                logger.warning(
                    "Marking alert BROKEN due to invalid filter config",
                    alert_id=str(row["id"]),
                    team_id=row.get("team_id"),
                    reason=broken_reason,
                )
                _mark_alert_broken_for_bad_config(str(row["id"]), broken_reason)
                continue
            nca = row["next_check_at"] if row["next_check_at"] is not None else now
            date_to = resolve_alert_date_to(nca, checkpoint)
            projection_eligible = is_projection_eligible(row["filters"])
            key = (
                row["team_id"],
                row["window_minutes"],
                row["evaluation_periods"],
                row["check_interval_minutes"],
                projection_eligible,
                date_to,
            )
            grouped[key].append(str(row["id"]))
        except Exception as e:
            # Any unexpected per-row failure must not brick discovery for the
            # rest of the project.
            logger.exception(
                "Skipping alert in cohort discovery due to malformed row",
                alert_id=str(row.get("id")),
                team_id=row.get("team_id"),
                error=str(e),
            )
            capture_exception(e)

    manifests: list[CohortManifest] = []
    for (team_id, _wm, _ep, _cim, projection_eligible, date_to), alert_ids in grouped.items():
        for chunk in batched(alert_ids, MAX_ALERT_COHORT_SIZE, strict=False):
            manifests.append(
                CohortManifest(
                    team_id=team_id,
                    projection_eligible=projection_eligible,
                    date_to_iso=date_to.isoformat(),
                    alert_ids=list(chunk),
                )
            )
    return manifests


def _cohort_from_manifest(
    manifest: CohortManifest,
    alerts_by_id: dict[str, LogsAlertConfiguration],
) -> _AlertCohort:
    """Reconstruct an `_AlertCohort` from a manifest and a pre-loaded alerts dict."""
    alerts = tuple(alerts_by_id[alert_id] for alert_id in manifest.alert_ids)
    return _AlertCohort(
        alerts=alerts,
        date_to=datetime.fromisoformat(manifest.date_to_iso),
        projection_eligible=manifest.projection_eligible,
    )


@temporalio.activity.defn
async def evaluate_cohort_batch_activity(input: EvaluateCohortBatchInput) -> EvaluateCohortBatchOutput:
    """Phase 2: process a batch of cohorts with bounded intra-batch parallelism.

    Loads alerts once per batch (`id__in`), reconstructs `_AlertCohort`s from
    manifests, then drives the existing per-cohort flow concurrently — bounded by
    `MAX_CONCURRENT_COHORTS_PER_BATCH` via `asyncio.Semaphore`. Pure asyncio
    (NOT a thread pool — nested thread pools deadlock under Temporal cancellation).

    Per-cohort failure is contained: a single cohort's exception only counts its
    alerts as errored. Per-batch failure (uncaught) bubbles up to the workflow,
    which treats this batch's alerts as errored via `gather(return_exceptions=True)`.
    """
    now = datetime.now(UTC)

    all_alert_ids = {aid for manifest in input.manifests for aid in manifest.alert_ids}
    alerts_by_id = await database_sync_to_async_pool(_load_alerts_for_batch)(all_alert_ids)

    cohort_query_async = database_sync_to_async_pool(_run_cohort_query)
    save_cohort_async = database_sync_to_async_pool(_save_cohort_outcomes)
    dispatch_async = database_sync_to_async_pool(_dispatch_for_alert)

    semaphore = asyncio.Semaphore(MAX_CONCURRENT_COHORTS_PER_BATCH)

    async def _run_one_cohort(manifest: CohortManifest) -> tuple[dict[str, int], list[NotifiedAlert]]:
        """Process one cohort, returning its stats delta and notified alerts. Always
        returns — per-cohort failure is captured into local_stats, never raised.

        Cohorts run concurrently under the semaphore, so this returns its deltas
        for serial aggregation by the caller rather than mutating shared state.
        """
        local_stats = {"checked": 0, "fired": 0, "resolved": 0, "errored": 0}
        local_notified: list[NotifiedAlert] = []

        async with semaphore:
            try:
                cohort = _cohort_from_manifest(manifest, alerts_by_id)
            except KeyError:
                logger.warning(
                    "Manifest references missing alert(s); skipping cohort",
                    team_id=manifest.team_id,
                    missing_count=len(manifest.alert_ids),
                )
                local_stats["errored"] += len(manifest.alert_ids)
                return local_stats, local_notified

            _safe_record("cohort_size histogram", record_cohort_size, len(cohort.alerts))

            with contextlib.ExitStack() as slo_stack:
                slo_handles: dict[str, SloHandle] = {
                    str(alert.id): slo_stack.enter_context(
                        slo_operation(
                            spec=SloSpec(
                                distinct_id=str(alert.id),
                                area=SloArea.ANALYTIC_PLATFORM,
                                operation=SloOperation.ALERT_CHECK,
                                team_id=alert.team_id,
                                resource_id=str(alert.id),
                            ),
                            properties={
                                "alert_type": "logs",
                                "check_interval_minutes": alert.check_interval_minutes,
                                "window_minutes": alert.window_minutes,
                            },
                        )
                    )
                    for alert in cohort.alerts
                }

                try:
                    query_result = await cohort_query_async(cohort)
                except Exception:
                    logger.exception(
                        "Cohort CH query failed unrecoverably",
                        team_id=cohort.team_id,
                        cohort_size=len(cohort.alerts),
                    )
                    for slo_handle in slo_handles.values():
                        slo_handle.fail(failure_phase="cohort_query")
                    local_stats["errored"] += len(cohort.alerts)
                    return local_stats, local_notified

                evaluations: list[_AlertEvaluation] = []
                eval_starts: list[float] = []
                for alert in cohort.alerts:
                    alert_id = str(alert.id)
                    eval_start = time.perf_counter()
                    try:
                        evaluations.append(
                            _evaluate_single_alert(
                                alert,
                                now,
                                checkpoint=None,
                                prefetched=query_result.for_alert(alert),
                            )
                        )
                        eval_starts.append(eval_start)
                    except Exception:
                        logger.exception(
                            "Unexpected error evaluating alert",
                            alert_id=alert_id,
                            team_id=alert.team_id,
                        )
                        slo_handles[alert_id].fail(failure_phase="evaluation")
                        local_stats["errored"] += 1

                delivery_slo_handles: dict[str, SloHandle] = {}
                for evaluation in evaluations:
                    delivery_action = evaluation.outcome.notification
                    # A shadowed alert sends nothing, so it has no delivery to measure.
                    if delivery_action != NotificationAction.NONE and not evaluation.alert.platform_owned:
                        alert_id = str(evaluation.alert.id)
                        delivery_slo_handles[alert_id] = slo_stack.enter_context(
                            alert_delivery_slo(
                                alert_type="logs",
                                notification_action=delivery_action.value,
                                distinct_id=alert_id,
                                team_id=evaluation.alert.team_id,
                                resource_id=alert_id,
                                properties={
                                    "check_interval_minutes": evaluation.alert.check_interval_minutes,
                                    "window_minutes": evaluation.alert.window_minutes,
                                },
                            )
                        )

                dispatched_or_errors = await asyncio.gather(
                    *(dispatch_async(ev, now) for ev in evaluations),
                    return_exceptions=True,
                )
                phase_2_end = time.perf_counter()
                dispatched: list[_DispatchedAlert] = []
                elapsed_ms_per_alert: list[int] = []
                for ev, result, eval_start in zip(evaluations, dispatched_or_errors, eval_starts):
                    alert_id = str(ev.alert.id)
                    if isinstance(result, BaseException):
                        logger.exception(
                            "Unexpected error dispatching alert",
                            alert_id=alert_id,
                            team_id=ev.alert.team_id,
                            exc_info=result,
                        )
                        slo_handles[alert_id].fail(failure_phase="dispatch")
                        if delivery_slo := delivery_slo_handles.get(alert_id):
                            delivery_slo.fail(failure_phase="dispatch")
                        local_stats["errored"] += 1
                    elif result.suppressed_by_quiet_hours:
                        continue
                    else:
                        dispatched.append(result)
                        elapsed_ms_per_alert.append(int((phase_2_end - eval_start) * 1000))

                # Delivery barrier: block until Kafka acks (or the flush deadline
                # passes) BEFORE the save, so undelivered notifications roll state
                # back and get retried next cycle. flush() blocks, so run it off
                # the event loop, and only when something was actually produced,
                # sparing quiet cohorts the thread hop.
                if any(d.produced for d in dispatched):
                    dispatched = await asyncio.to_thread(_resolve_notification_deliveries, dispatched)

                for dispatched_alert in dispatched:
                    alert_id = str(dispatched_alert.evaluation.alert.id)
                    if delivery_slo := delivery_slo_handles.get(alert_id):
                        if dispatched_alert.notification_failed:
                            delivery_slo.fail(failure_phase="notification_delivery")
                        else:
                            delivery_slo.succeed()

                try:
                    saved, failed = (await save_cohort_async(dispatched, now)) if dispatched else ([], [])
                except Exception as e:
                    logger.exception("Cohort bulk save failed (non-recoverable)", team_id=cohort.team_id)
                    capture_exception(e, {"team_id": cohort.team_id, "phase": "bulk_save"})
                    for dispatched_alert in dispatched:
                        slo_handles[str(dispatched_alert.evaluation.alert.id)].fail(failure_phase="save")
                    local_stats["errored"] += len(dispatched)
                    local_stats["checked"] += len(dispatched)
                    return local_stats, local_notified

                elapsed_by_id = {str(d.evaluation.alert.id): ms for d, ms in zip(dispatched, elapsed_ms_per_alert)}
                for dispatched_alert in saved:
                    alert_id = str(dispatched_alert.evaluation.alert.id)
                    _finalize_alert(dispatched_alert, elapsed_by_id[alert_id], local_stats)
                    committed_state = dispatched_alert.committed_outcome.new_state.value
                    notification_action = dispatched_alert.evaluation.outcome.notification.value
                    if dispatched_alert.notification_failed:
                        slo_handles[alert_id].fail(
                            alert_state=committed_state,
                            notification_action=notification_action,
                            failure_phase="notification_delivery",
                        )
                    else:
                        slo_handles[alert_id].succeed(
                            alert_state=committed_state,
                            notification_action=notification_action,
                        )
                for dispatched_alert in failed:
                    slo_handles[str(dispatched_alert.evaluation.alert.id)].fail(failure_phase="save")
                    local_stats["checked"] += 1
                    local_stats["errored"] += 1

                local_notified.extend(_build_notified_from_saved(saved))
                return local_stats, local_notified

    cohort_results = await asyncio.gather(
        *(_run_one_cohort(m) for m in input.manifests),
        return_exceptions=False,  # _run_one_cohort never raises; it captures failures into local_stats
    )

    # Single GC at batch end rather than per-cohort. With up to 20 cohorts × 50
    # alerts retained inside the closure during gather, a stop-the-world per
    # cohort costs more than the once-per-batch reclaim it buys.
    await asyncio.to_thread(_post_cohort_memory_cleanup)

    stats = {"checked": 0, "fired": 0, "resolved": 0, "errored": 0}
    batch_notified: list[NotifiedAlert] = []
    for local_stats, local_notified in cohort_results:
        for k, v in local_stats.items():
            stats[k] += v
        batch_notified.extend(local_notified)

    if stats["checked"] > 0:
        logger.info("Cohort batch complete", **stats)

    return EvaluateCohortBatchOutput(
        alerts_checked=stats["checked"],
        alerts_fired=stats["fired"],
        alerts_resolved=stats["resolved"],
        alerts_errored=stats["errored"],
        notified=batch_notified,
    )


def _load_teams_for_signals(team_ids: set[int]) -> dict[int, Team]:
    """Load teams (with organization) for signal emission, keyed by id.

    `emit_signal` reads `team.organization` for the AI-consent gate, so prefetch
    it here to avoid a per-emit round-trip.
    """
    return {team.id: team for team in Team.objects.filter(id__in=team_ids).select_related("organization")}


@temporalio.activity.defn
async def emit_alert_signals_activity(input: EmitAlertSignalsInput) -> int:
    """Fan out emit_signal for alerts that notified this cycle.

    Dedicated activity (off the eval hot path) with bounded concurrency,
    heartbeats, and per-emit isolation — mirrors the data_imports signal pipeline.
    Returns the number of signals successfully emitted.
    """
    return await emit_notified_alert_signals(input.notified)


async def emit_notified_alert_signals(notified: list[NotifiedAlert]) -> int:
    """Emits one signal per alert, for whichever logs alerting path notified it. Never raises."""
    if not notified:
        return 0

    team_ids = {na.team_id for na in notified}
    teams = await database_sync_to_async_pool(_load_teams_for_signals)(team_ids)

    semaphore = asyncio.Semaphore(EMIT_SIGNAL_CONCURRENCY)
    completed = 0

    async def _bounded(na: NotifiedAlert) -> bool:
        nonlocal completed
        async with semaphore:
            team = teams.get(na.team_id)
            if team is None:
                logger.warning(
                    "Team missing for logs alert signal; skipping",
                    team_id=na.team_id,
                    alert_id=na.alert_id,
                )
                return False
            ok = await emit_alert_state_change_signal(team, na)
            completed += 1
            if completed % EMIT_SIGNAL_CONCURRENCY == 0:
                temporalio.activity.heartbeat()
            return ok

    results = await asyncio.gather(*(_bounded(na) for na in notified))
    return sum(1 for ok in results if ok)


def _load_alerts_for_batch(alert_ids: set[str]) -> dict[str, LogsAlertConfiguration]:
    """Single `id__in` query for all alerts referenced by the batch.

    Alert IDs are UUID PKs (unique, indexed) so cross-team `id__in` is no
    slower than per-team scoping — and saves one round-trip per team.
    """
    return {
        str(alert.id): alert for alert in LogsAlertConfiguration.objects.filter(id__in=alert_ids).select_related("team")
    }


def _run_batched_query(cohort: _AlertCohort) -> BatchedBucketedResult:
    return BatchedAlertCheckQuery(
        team=cohort.team,
        alerts=list(cohort.alerts),
        date_from=cohort.date_from,
        date_to=cohort.date_to,
        projection_eligible=cohort.projection_eligible,
    ).execute_rolling_checks(
        nca=cohort.date_to,
        window_minutes=cohort.window_minutes,
        cadence_minutes=cohort.check_interval_minutes,
        period_count=cohort.evaluation_periods,
    )


def _run_per_alert_queries(cohort: _AlertCohort) -> _CohortQueryResult:
    per_alert: dict[str, _PrefetchedQuery] = {}
    for alert in cohort.alerts:
        start = time.monotonic_ns()
        try:
            buckets = AlertCheckQuery(
                team=alert.team,
                alert=alert,
                date_from=cohort.date_from,
                date_to=cohort.date_to,
            ).execute_rolling_checks(
                nca=cohort.date_to,
                window_minutes=cohort.window_minutes,
                cadence_minutes=cohort.check_interval_minutes,
                period_count=cohort.evaluation_periods,
            )
            duration_ms = (time.monotonic_ns() - start) // 1_000_000
            per_alert[str(alert.id)] = _PrefetchedQuery(buckets=buckets, query_duration_ms=duration_ms)
        except Exception as e:
            duration_ms = (time.monotonic_ns() - start) // 1_000_000
            per_alert[str(alert.id)] = _PrefetchedQuery(error=e, query_duration_ms=duration_ms)
    return _CohortQueryResult(per_alert=per_alert)


def _run_cohort_query(cohort: _AlertCohort) -> _CohortQueryResult:
    """Batched CH query for a cohort; per-alert fallback on non-transient failure.

    Cohorts are pre-sized at `_cohort_manifests_from_alerts` time so this function
    operates on a single bounded group. Skip fallback for single-alert cohorts
    (would hit the same error) and transient cluster errors (would hammer a
    struggling cluster with N more failing queries).
    """
    try:
        batched_result = _run_batched_query(cohort)
    except Exception as e:
        team_id = cohort.team_id
        cohort_size = len(cohort.alerts)

        if cohort_size == 1:
            return _CohortQueryResult(per_alert={str(cohort.alerts[0].id): _PrefetchedQuery(error=e)})

        alert_ids = [str(a.id) for a in cohort.alerts]

        classified = classify_alert_error(e)
        if classified.is_transient:
            logger.warning(
                "Batched cohort query failed (transient); skipping per-alert fallback",
                team_id=team_id,
                cohort_size=cohort_size,
                alert_ids=alert_ids,
                error=str(e),
                classification=classified.code,
            )
            _safe_record("cohort_query_fallback counter", increment_cohort_query_fallback, "transient_no_fallback")
            return _CohortQueryResult(per_alert={str(a.id): _PrefetchedQuery(error=e) for a in cohort.alerts})

        logger.warning(
            "Batched cohort query failed; falling back to per-alert queries",
            team_id=team_id,
            cohort_size=cohort_size,
            alert_ids=alert_ids,
            error=str(e),
            classification=classified.code,
        )
        capture_exception(e, {"team_id": team_id, "cohort_size": cohort_size, "alert_ids": alert_ids})
        _safe_record("cohort_query_fallback counter", increment_cohort_query_fallback, "batched_failure")
        return _run_per_alert_queries(cohort)

    return _CohortQueryResult(
        per_alert={
            str(alert.id): _PrefetchedQuery(
                buckets=batched_result.per_alert.get(str(alert.id), []),
                query_duration_ms=batched_result.query_duration_ms,
            )
            for alert in cohort.alerts
        }
    )


def _dispatch_notification(
    outcome: AlertCheckOutcome,
    alert: LogsAlertConfiguration,
    check_result: CheckResult,
    now: datetime,
    *,
    date_from: datetime,
    date_to: datetime,
) -> ProduceResult | None:
    """Emit the notification for this outcome. Returns the pending Kafka
    delivery, or None when nothing was enqueued (no action, or the enqueue
    failed synchronously — distinguishable via `outcome.notification`).

    Pure-effect: produces a Kafka message and logs. Does NOT mutate `stats` —
    the orchestrator owns stats accounting so it stays single-threaded after
    the parallel dispatch phase.
    """
    action = outcome.notification
    if action == NotificationAction.NONE:
        return None

    log = logger.bind(alert_id=str(alert.id), alert_name=alert.name, team_id=alert.team_id)

    if action == NotificationAction.FIRE:
        result = _emit_alert_event(alert, "$logs_alert_firing", check_result, now, date_from=date_from, date_to=date_to)
        log.info("Alert fired", result_count=check_result.result_count, enqueued=result is not None)
    elif action == NotificationAction.RESOLVE:
        result = _emit_alert_event(
            alert, "$logs_alert_resolved", check_result, now, date_from=date_from, date_to=date_to
        )
        log.info("Alert resolved", enqueued=result is not None)
    elif action == NotificationAction.ERROR:
        result = _emit_alert_errored_event(alert, outcome, now)
        log.info(
            "Alert entered errored state",
            consecutive_failures=outcome.consecutive_failures,
            enqueued=result is not None,
        )
    elif action == NotificationAction.BROKEN:
        result = _emit_auto_disabled_event(alert, outcome, now)
        log.warning(
            "Alert broken after consecutive failures",
            consecutive_failures=outcome.consecutive_failures,
            enqueued=result is not None,
        )
    else:
        raise ValueError(f"Unhandled NotificationAction: {action!r}")

    return result


def _dispatch_for_alert(evaluation: _AlertEvaluation, now: datetime) -> _DispatchedAlert:
    """Phase 2: defer blocked alerts or dispatch their Kafka notification."""
    with transaction.atomic():
        current_alert = (
            LogsAlertConfiguration.objects.select_for_update(of=("self",))
            .select_related("team")
            .get(id=evaluation.alert.id)
        )
        try:
            next_check_at = next_allowed_check_at(
                now,
                team_timezone=current_alert.team.timezone,
                schedule_restriction=current_alert.schedule_restriction,
            )
        except Exception as e:
            logger.exception(
                "Skipping alert with invalid quiet-hours configuration",
                alert_id=str(current_alert.id),
                team_id=current_alert.team_id,
                error=str(e),
            )
            return _DispatchedAlert(
                evaluation=evaluation,
                notification_failed=False,
                suppressed_by_quiet_hours=True,
            )
        if next_check_at > now:
            current_alert.next_check_at = next_check_at
            current_alert.save(update_fields=["next_check_at", "updated_at"])
            return _DispatchedAlert(
                evaluation=evaluation,
                notification_failed=False,
                suppressed_by_quiet_hours=True,
            )

        # Decided and enqueued under the row lock a cutover also takes, so a cutover cannot land
        # between the two. Counted as delivered, so state and cooldown move as they would have. An
        # owner change since the check read the alert also sends nothing: the row now carries the
        # other stack's state, which this check never saw, and the save skips it too.
        if current_alert.platform_owned or current_alert.owner_changed_at != evaluation.alert.owner_changed_at:
            return _DispatchedAlert(evaluation=evaluation, notification_failed=False, shadowed=True)

        produce_result = _dispatch_notification(
            evaluation.outcome,
            evaluation.alert,
            evaluation.check_result,
            now,
            date_from=evaluation.date_from,
            date_to=evaluation.date_to,
        )
        edge = incident_edge(evaluation.state_before, evaluation.outcome.new_state)
        incident_expected = edge is not None and has_incident_destination(evaluation.alert)
        incident_produce_result = _dispatch_incident_edge(edge, evaluation, now) if edge and incident_expected else None
        enqueue_failed = (evaluation.outcome.notification != NotificationAction.NONE and produce_result is None) or (
            incident_expected and incident_produce_result is None
        )
        return _DispatchedAlert(
            evaluation=evaluation,
            notification_failed=enqueue_failed,
            produce_result=produce_result,
            incident_produce_result=incident_produce_result,
        )


def _dispatch_incident_edge(edge: IncidentEdge, evaluation: _AlertEvaluation, now: datetime) -> ProduceResult | None:
    alert = evaluation.alert
    if edge == IncidentEdge.OPENED:
        return _emit_alert_event(
            alert,
            LOGS_ALERT_INCIDENT_OPENED_EVENT,
            evaluation.check_result,
            now,
            date_from=evaluation.date_from,
            date_to=evaluation.date_to,
        )
    else:
        reason = (
            IncidentCloseReason.BROKEN
            if evaluation.outcome.new_state == AlertState.BROKEN
            else IncidentCloseReason.RESOLVED
        )
        return _emit_incident_closed_event(alert, reason, now)


def _resolve_notification_deliveries(dispatched: list[_DispatchedAlert]) -> list[_DispatchedAlert]:
    """Phase 2.5: barrier between dispatch and save — wait for Kafka to ack the
    cohort's notifications, folding delivery failures into `notification_failed`.

    Without this, `produce()`'s local buffering makes "notified" state durable
    before the message ever reaches the broker: a broker outage after enqueue
    would advance the alert to FIRING with no notification delivered, and the
    transition-triggered FIRE would never be re-attempted.

    Flush failures are swallowed: the per-result checks below classify each
    alert individually (an unresolved result reads as undelivered), and a save
    with rolled-back state beats no save at all — the cohort erroring out here
    would skip the save entirely.
    """
    if not any(d.produced for d in dispatched):
        return dispatched

    flush_alert_internal_events(NOTIFICATION_FLUSH_TIMEOUT_SECONDS)

    resolved: list[_DispatchedAlert] = []
    for d in dispatched:
        alert = d.evaluation.alert
        edge = incident_edge(d.evaluation.state_before, d.evaluation.outcome.new_state)
        incident_event = (
            LOGS_ALERT_INCIDENT_OPENED_EVENT if edge == IncidentEdge.OPENED else LOGS_ALERT_INCIDENT_CLOSED_EVENT
        )
        deliveries = (
            (d.produce_result, d.evaluation.outcome.notification.value),
            (d.incident_produce_result, incident_event),
        )
        delivered = all(
            alert_internal_event_delivered(
                produce_result, team_id=alert.team_id, alert_id=str(alert.id), event_name=event_name
            )
            for produce_result, event_name in deliveries
            if produce_result is not None
        )
        resolved.append(d if delivered else dataclasses.replace(d, notification_failed=True))
    return resolved


def _stage_alert_for_save(dispatched: _DispatchedAlert, now: datetime) -> tuple[list[str], LogsAlertEvent | None]:
    """Mutate the in-memory alert so it's ready for `bulk_update`, return the
    `update_fields` list and the optional `LogsAlertEvent` row to insert.

    All state/consecutive_failures writes go through `apply_outcome` per the
    semgrep rule — same contract as the per-alert path.
    """
    evaluation = dispatched.evaluation
    alert = evaluation.alert
    committed = dispatched.committed_outcome
    is_error = evaluation.outcome.error_message is not None
    state_changed = evaluation.state_before != committed.new_state.value

    update_fields = apply_outcome(alert, committed)
    alert.last_checked_at = now
    # `updated_at` has `auto_now=True`, but Django's `bulk_update` doesn't apply
    # `auto_now` — set it explicitly so the timestamp advances on the bulk path
    # (the per-alert fallback's `alert.save()` would honour `auto_now`, but the
    # happy path is bulk_update).
    alert.updated_at = now
    next_check_at = advance_next_check_at(
        alert.next_check_at,
        alert.check_interval_minutes,
        now,
        shard_offset_seconds=compute_shard_offset_seconds(alert.team_id, alert.check_interval_minutes),
    )
    try:
        alert.next_check_at = next_allowed_check_at(
            next_check_at,
            team_timezone=alert.team.timezone,
            schedule_restriction=alert.schedule_restriction,
        )
    except Exception as e:
        logger.exception(
            "Ignoring invalid quiet-hours configuration while saving alert",
            alert_id=str(alert.id),
            team_id=alert.team_id,
            error=str(e),
        )
        alert.next_check_at = next_check_at
    update_fields.extend(["last_checked_at", "next_check_at", "updated_at"])

    if (
        not dispatched.notification_failed
        and evaluation.outcome.notification != NotificationAction.NONE
        and evaluation.outcome.update_last_notified_at
    ):
        alert.last_notified_at = now
        update_fields.append("last_notified_at")

    event: LogsAlertEvent | None = None
    if state_changed or is_error:
        event = LogsAlertEvent(
            alert=alert,
            result_count=evaluation.check_result.result_count,
            threshold_breached=evaluation.check_result.threshold_breached,
            state_before=evaluation.state_before,
            state_after=committed.new_state.value,
            error_message=evaluation.outcome.error_message,
            query_duration_ms=evaluation.check_result.query_duration_ms,
        )
    return update_fields, event


# All bulk_update calls touch the same fields per cohort — alerts that aren't
# notifying still get last_notified_at written back unchanged. Cheap, keeps
# the bulk_update field list constant.
_COHORT_UPDATE_FIELDS: list[str] = [
    "state",
    "consecutive_failures",
    "last_checked_at",
    "next_check_at",
    "last_notified_at",
    "updated_at",
]


def _save_cohort_outcomes(
    dispatched: list[_DispatchedAlert], now: datetime
) -> tuple[list[_DispatchedAlert], list[_DispatchedAlert]]:
    """Phase 3: persist the cohort's outcomes via one bulk_create + one bulk_update.

    Returns `(saved, failed)` — saved alerts advanced to their committed state,
    failed alerts didn't (caller treats them as errored).

    On `IntegrityError` (constraint shaped — a row hit a DB constraint we didn't
    anticipate), fall back to per-alert UPDATEs so the rest still advance.
    `OperationalError`/`DataError` are propagated so the caller advances each
    alert's `consecutive_failures` via the standard error path; per-alert
    fallback against the same broken cluster wouldn't help.
    """
    if not dispatched:
        return [], []

    save_start = time.perf_counter()

    event_insert_ms: int | None = None
    update_ms: int | None = None
    saved: list[_DispatchedAlert] = list(dispatched)
    failed: list[_DispatchedAlert] = []
    superseded: list[_DispatchedAlert] = []
    try:
        with transaction.atomic():
            current = {
                alert_id: (restriction, owner_changed_at)
                for alert_id, restriction, owner_changed_at in LogsAlertConfiguration.objects.select_for_update()
                .filter(id__in=[d.evaluation.alert.id for d in dispatched])
                .values_list("id", "schedule_restriction", "owner_changed_at")
            }

            # The worker can evaluate an alert while a user saves quiet hours. Locking
            # before staging makes either write order safe: this worker uses the saved
            # restriction, or the API waits and then reschedules after this check.
            staged: list[tuple[_DispatchedAlert, list[str], LogsAlertEvent | None]] = []
            for d in dispatched:
                restriction, owner_changed_at = current.get(d.evaluation.alert.id, (None, None))
                # A cutover or rollback carried another stack's state onto this row after the check
                # read it, so this check's state and schedule are stale and are not saved.
                if owner_changed_at != d.evaluation.alert.owner_changed_at:
                    superseded.append(d)
                    continue
                d.evaluation.alert.schedule_restriction = restriction
                update_fields, event = _stage_alert_for_save(d, now)
                staged.append((d, update_fields, event))

            events = [event for _, _, event in staged if event is not None]
            alerts = [d.evaluation.alert for d, _, _ in staged]
            if events:
                event_insert_start = time.perf_counter()
                LogsAlertEvent.objects.bulk_create(events)
                event_insert_ms = int((time.perf_counter() - event_insert_start) * 1000)

            update_start = time.perf_counter()
            LogsAlertConfiguration.objects.bulk_update(alerts, fields=_COHORT_UPDATE_FIELDS)
            update_ms = int((time.perf_counter() - update_start) * 1000)
    except IntegrityError as e:
        # Recover the rest of the cohort via per-alert UPDATEs using the
        # already-staged data.
        logger.warning(
            "Cohort bulk save hit IntegrityError; falling back to per-alert",
            error=str(e),
            cohort_size=len(dispatched),
        )
        capture_exception(e, {"cohort_size": len(dispatched), "fallback": "per_alert"})
        increment_cohort_save_fallback("integrity_error")
        saved, failed = _save_staged_per_alert(staged)
        saved += superseded

    save_ms = int((time.perf_counter() - save_start) * 1000)
    with _safe_record_block("cohort save metrics"):
        record_cohort_save_duration(save_ms)
        if event_insert_ms is not None:
            record_cohort_event_insert_duration(event_insert_ms)
        if update_ms is not None:
            record_cohort_update_duration(update_ms)

    return saved, failed


def _save_staged_per_alert(
    staged: list[tuple[_DispatchedAlert, list[str], LogsAlertEvent | None]],
) -> tuple[list[_DispatchedAlert], list[_DispatchedAlert]]:
    """Fallback path used when `bulk_update` raises `IntegrityError`.

    Returns `(saved, failed)`. Each alert saves independently so a single bad
    row doesn't strand the cohort. Takes pre-staged tuples (alert already
    mutated, event already instantiated) so we don't re-run
    `_stage_alert_for_save` and accidentally advance `next_check_at` twice.
    """
    saved: list[_DispatchedAlert] = []
    failed: list[_DispatchedAlert] = []
    for d, update_fields, event in staged:
        try:
            with transaction.atomic():
                # The bulk attempt's locks are gone, so an owner change can have landed since.
                current_owner_changed_at = (
                    LogsAlertConfiguration.objects.select_for_update()
                    .filter(id=d.evaluation.alert.id)
                    .values_list("owner_changed_at", flat=True)
                    .first()
                )
                if current_owner_changed_at == d.evaluation.alert.owner_changed_at:
                    if event is not None:
                        event.save()
                    d.evaluation.alert.save(update_fields=update_fields)
            saved.append(d)
        except Exception as e:
            logger.exception(
                "Per-alert fallback save failed",
                alert_id=str(d.evaluation.alert.id),
                team_id=d.evaluation.alert.team_id,
            )
            capture_exception(e, {"alert_id": str(d.evaluation.alert.id), "phase": "per_alert_fallback"})
            failed.append(d)
    return saved, failed


def _finalize_alert(dispatched: _DispatchedAlert, elapsed_ms: int, stats: dict[str, int]) -> None:
    """Per-alert post-save bookkeeping: cycle stats + metrics that depend on the committed state.

    Runs serially (not gathered) so `stats` mutation stays single-threaded.
    `elapsed_ms` is the precomputed eval + dispatch duration captured by the
    orchestrator at the end of Phase 2 — *before* Phase 3 (the cohort bulk save)
    starts. `record_check_duration` therefore measures only the per-alert work,
    matching the metric description; cohort save time is captured separately by
    `record_cohort_save_duration`.
    """
    evaluation = dispatched.evaluation
    outcome = evaluation.outcome
    committed_state = dispatched.committed_outcome.new_state

    stats["checked"] += 1
    if outcome.error_message:
        stats["errored"] += 1
    elif not dispatched.notification_failed:
        if outcome.notification == NotificationAction.FIRE:
            stats["fired"] += 1
        elif outcome.notification == NotificationAction.RESOLVE:
            stats["resolved"] += 1

    with _safe_record_block("alert finalize metrics", alert_id=str(evaluation.alert.id)):
        record_check_duration(elapsed_ms)

        if outcome.error_message:
            increment_checks_total("errored")
        elif dispatched.notification_failed:
            increment_checks_total("errored")
        elif outcome.notification == NotificationAction.FIRE:
            increment_checks_total("fired")
        elif outcome.notification == NotificationAction.RESOLVE:
            increment_checks_total("resolved")
        else:
            increment_checks_total("ok")

        if dispatched.notification_failed:
            increment_notification_failures(outcome.notification)

        state_before_enum = AlertState(evaluation.state_before)
        if committed_state != state_before_enum:
            increment_state_transition(state_before_enum, committed_state)


def _build_notified_from_saved(saved: list[_DispatchedAlert]) -> list[NotifiedAlert]:
    """Map saved+notified dispatched alerts to serialisable signal descriptors.

    Only alerts whose committed outcome carried a signalable notification
    (FIRE/BROKEN) and whose Kafka dispatch succeeded are included. RESOLVE/ERROR
    map to None via signal_action_and_weight and are skipped here.
    """
    notified: list[NotifiedAlert] = []
    for d in saved:
        if d.notification_failed or d.shadowed:
            continue
        mapping = signal_action_and_weight(d.evaluation.outcome.notification)
        if mapping is None:
            continue
        action, weight = mapping
        alert = d.evaluation.alert
        notified.append(
            NotifiedAlert(
                alert_id=str(alert.id),
                team_id=alert.team_id,
                alert_name=alert.name,
                action=action,
                weight=weight,
                threshold_count=alert.threshold_count,
                threshold_operator=alert.threshold_operator,
                window_minutes=alert.window_minutes,
                result_count=d.evaluation.check_result.result_count,
                consecutive_failures=d.evaluation.outcome.consecutive_failures,
                filters=alert.filters,
            )
        )
    return notified


def _evaluate_single_alert(
    alert: LogsAlertConfiguration,
    now: datetime,
    *,
    checkpoint: datetime | None = None,
    prefetched: _PrefetchedQuery | None = None,
) -> _AlertEvaluation:
    """Phase 1: run the CH query (or use prefetched buckets), apply the state machine, return the outcome.

    Pure-ish — does not dispatch Kafka, does not write to Postgres. The cohort
    orchestrator handles both side-effect phases.

    Stateless eval: a single bucketed CH query returns the last M counts; the
    N-of-M evaluator decides from those buckets directly. Anchored on
    `next_check_at` so two evals at different actual eval times produce the
    same query.

    `prefetched` is set when the caller already ran a batched query for the
    alert's cohort — either with the alert's bucket slice, or with the cohort's
    error (re-raised inside the try block to flow through the same
    classification as a per-alert failure).
    """
    original_next_check_at = alert.next_check_at

    nca = alert.next_check_at if alert.next_check_at is not None else now
    date_to = resolve_alert_date_to(nca, checkpoint)
    date_from = date_to - timedelta(
        minutes=rolling_check_lookback_minutes(
            alert.window_minutes, alert.check_interval_minutes, alert.evaluation_periods
        )
    )

    check_result: CheckResult
    recent_breaches: tuple[bool, ...] = ()
    error_category: AlertErrorCode | None = None
    try:
        if prefetched is not None and prefetched.error is not None:
            raise prefetched.error
        if prefetched is not None and prefetched.buckets is not None:
            buckets = prefetched.buckets
            query_duration_ms = prefetched.query_duration_ms if prefetched.query_duration_ms is not None else 0
        else:
            query_start = time.perf_counter()
            buckets = AlertCheckQuery(
                team=alert.team,
                alert=alert,
                date_from=date_from,
                date_to=date_to,
            ).execute_rolling_checks(
                nca=date_to,
                window_minutes=alert.window_minutes,
                cadence_minutes=alert.check_interval_minutes,
                period_count=alert.evaluation_periods,
            )
            query_duration_ms = int((time.perf_counter() - query_start) * 1000)

        breaches = _derive_breaches(buckets, alert.threshold_count, alert.threshold_operator, alert.evaluation_periods)
        latest_count = buckets[-1].count if buckets else 0
        check_result = CheckResult(
            result_count=latest_count,
            threshold_breached=breaches[0] if breaches else False,
            query_duration_ms=query_duration_ms,
        )
        recent_breaches = breaches[1:]
    except Exception as e:
        classified = classify_alert_error(e)
        error_category = classified.code
        capture_exception(e, {"alert_id": str(alert.id), "classification": classified.code})
        logger.warning(
            "Alert check query failed",
            alert_id=str(alert.id),
            alert_name=alert.name,
            team_id=alert.team_id,
            error=str(e),
            classification=classified.code,
        )
        check_result = CheckResult(
            result_count=None,
            threshold_breached=False,
            error_message=classified.user_message,
            is_transient_error=classified.is_transient,
        )

    outcome = evaluate_alert_check(alert.to_snapshot(recent_events_breached=recent_breaches), check_result, now)

    # Eval-phase metrics: CH-side and scheduler lag. Save/dispatch metrics fire
    # later in their own phases.
    with _safe_record_block("alert eval metrics", alert_id=str(alert.id)):
        if check_result.query_duration_ms is not None:
            record_clickhouse_duration(check_result.query_duration_ms)
        if original_next_check_at is not None:
            lag_ms = int((now - original_next_check_at).total_seconds() * 1000)
            if lag_ms > 0:
                record_scheduler_lag(lag_ms)
        if error_category is not None:
            increment_check_errors(error_category)

    return _AlertEvaluation(
        alert=alert,
        outcome=outcome,
        check_result=check_result,
        date_from=date_from,
        date_to=date_to,
        state_before=alert.state,
    )


def _produce_alert_internal_event(
    alert: LogsAlertConfiguration,
    event_name: str,
    properties: dict,
    now: datetime,
) -> ProduceResult | None:
    return produce_alert_internal_event(
        team_id=alert.team_id,
        event_name=event_name,
        properties=properties,
        timestamp=now,
    )


def _emit_alert_event(
    alert: LogsAlertConfiguration,
    event_name: str,
    check_result: CheckResult,
    now: datetime,
    *,
    date_from: datetime,
    date_to: datetime,
) -> ProduceResult | None:
    properties = threshold_event_properties(
        alert_id=str(alert.id),
        alert_name=alert.name,
        team_id=alert.team_id,
        threshold_count=alert.threshold_count,
        threshold_operator=alert.threshold_operator,
        window_minutes=alert.window_minutes,
        result_count=check_result.result_count,
        filters=alert.filters,
        date_from=date_from,
        date_to=date_to,
        triggered_at=now,
    )
    return _produce_alert_internal_event(alert, event_name, properties, now)


def _base_failure_properties(
    alert: LogsAlertConfiguration,
    outcome: AlertCheckOutcome,
    now: datetime,
) -> dict:
    return failure_event_properties(
        alert_id=str(alert.id),
        alert_name=alert.name,
        team_id=alert.team_id,
        consecutive_failures=outcome.consecutive_failures,
        filters=alert.filters,
        triggered_at=now,
    )


def _emit_auto_disabled_event(
    alert: LogsAlertConfiguration,
    outcome: AlertCheckOutcome,
    now: datetime,
) -> ProduceResult | None:
    properties = {
        **_base_failure_properties(alert, outcome, now),
        "last_error_message": outcome.error_message or "",
    }
    return _produce_alert_internal_event(alert, "$logs_alert_auto_disabled", properties, now)


def _emit_incident_closed_event(
    alert: LogsAlertConfiguration,
    reason: IncidentCloseReason,
    now: datetime,
) -> ProduceResult | None:
    properties = incident_closed_properties(alert, reason, now)
    return _produce_alert_internal_event(alert, LOGS_ALERT_INCIDENT_CLOSED_EVENT, properties, now)


def _emit_alert_errored_event(
    alert: LogsAlertConfiguration,
    outcome: AlertCheckOutcome,
    now: datetime,
) -> ProduceResult | None:
    properties = {
        **_base_failure_properties(alert, outcome, now),
        "error_message": outcome.error_message or "",
    }
    return _produce_alert_internal_event(alert, "$logs_alert_errored", properties, now)