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.

24.7 KB
Raw Blame
"""
Grouping e2e eval — measures how well the signal grouping pipeline clusters
signals into reports, evaluated against known ground-truth groups.

Feeds synthetic signals through the real pipeline (LLM query generation,
embedding search, LLM matching, specificity verification) with mocked
infrastructure (in-memory embedding store replaces ClickHouse + Kafka).

After grouping, each report is judged for safety (prompt injection detection).

Records four levels of metrics:
- Per-signal matching: correct_match (binary), correct_match_pre_specificity
  (binary, LLM matcher decision before specificity judge),
  failure_mode (categorical: NONE, UNDERGROUP, OVERGROUP),
  query_diversity (numeric, avg pairwise cosine distance between query
  embeddings), candidate_diversity (numeric, avg 1 − Jaccard across
  query pairs' candidate sets)
- Per-report grouping: purity, is_pure, group_recall
- Per-report judges: correct_classification (binary) for report-safety-check
- Aggregate: ARI, homogeneity, completeness, mean_purity,
  group_recall, malicious_leaked_rate

Run:
    pytest products/signals/eval/eval_grouping_e2e.py -xvs
    pytest products/signals/eval/eval_grouping_e2e.py -xvs --limit 10 --no-capture
"""

import sys
import uuid
import asyncio
import logging
from itertools import combinations

import pytest

from sklearn.metrics import adjusted_rand_score, homogeneity_completeness_v_measure
from tqdm import tqdm

from products.signals.backend.emission.pipeline import (
    check_actionability,
    summarize_long_descriptions as _summarize_long_descriptions,
)
from products.signals.backend.temporal.grouping import (
    GenerateSearchQueriesInput,
    MatchSignalToReportInput,
    generate_search_queries,
    match_signal_to_report,
    verify_match_specificity,
)
from products.signals.backend.temporal.report_safety_judge import judge_report_safety
from products.signals.backend.temporal.safety_filter import safety_filter
from products.signals.backend.temporal.types import (
    ExistingReportMatch,
    MatchResult,
    NewReportMatch,
    NoMatchMetadata,
    SignalCandidate,
    SpecificityMetadata,
)
from products.signals.eval.common import (
    MAX_CONCURRENT_RUNS,
    EvalProgress,
    EvalSignalCase,
    MatchFailureMode,
    get_signals_stream,
)
from products.signals.eval.conftest import EVAL_TEAM_ID
from products.signals.eval.fixtures.grouping_data import GROUP_DATA
from products.signals.eval.mock import EmbeddingStore, ReportStore
from products.signals.eval.offline import EvalMetric, SignalsEvalRecorder
from products.signals.eval.offline_config import GROUPING_SUITE

TRANSPORT_RETRIES = 3


class EvalGroupingPipeline:
    def _eval_team(self):
        """Lightweight Team stand-in for the pipeline helpers — they only read .id."""
        from unittest.mock import MagicMock

        return MagicMock(id=EVAL_TEAM_ID)

    @pytest.fixture(autouse=True)
    def _setup(self, openai_client, gateway_client, mock_temporal, limit, no_capture, eval_metadata):
        self.results = SignalsEvalRecorder(
            GROUPING_SUITE,
            disabled=no_capture,
            metadata=eval_metadata,
        )
        self.gateway_client = gateway_client
        self.openai_client = openai_client
        self.store = EmbeddingStore(openai_client)
        self.report_store = ReportStore()
        self.limit = limit
        self._match_lock = asyncio.Lock()

        # Suppress structlog noise from downstream modules during the eval
        root_logger = logging.getLogger()
        previous_level = root_logger.level
        root_logger.setLevel(logging.ERROR)
        yield
        root_logger.setLevel(previous_level)

    @pytest.mark.django_db(transaction=True)
    async def eval_grouping_pipeline(self):
        stream = get_signals_stream()
        if self.limit:
            stream = stream[: self.limit]
            tqdm.write(f"Limiting to {self.limit} signals.", file=sys.stderr)

        n_groups = len({case.group_index for case in stream})
        self.progress = EvalProgress(n_signals=len(stream), n_groups=n_groups)

        sem = asyncio.Semaphore(MAX_CONCURRENT_RUNS)
        await asyncio.gather(
            *[self.run_signal_pipeline_concurrently(sem, record_id=i, case=case) for i, case in enumerate(stream)]
        )

        reports = self.report_store.all_reports()
        self.progress.start_judging(len(reports))
        await self._judge_reports()
        self.progress.done()

        self._record_grouping_quality()
        self._record_aggregate_metrics(stream)
        await self.results.publish()

    async def run_signal_pipeline_concurrently(self, sem: asyncio.Semaphore, record_id: int, case: EvalSignalCase):
        async with sem:
            await self.run_signal_pipeline(record_id, case)

    async def run_signal_pipeline(self, record_id: int, case: EvalSignalCase, attempt: int = 0):
        """Run a single signal through the pre-emit pipeline."""

        if attempt == 0:
            self.progress.signal_started()
        try:
            description = await self.pre_emit(record_id, case)

            if not description:
                self.progress.signal_dropped()
                return

            safety_result = await safety_filter(
                EVAL_TEAM_ID, description, source_product=case.signal.content.source_product
            )
            await self._record_safety_filter(case, safety_result)

            if not safety_result.safe:
                self.progress.signal_dropped()
                return

            # Prepare queries and embeddings outside the lock — these are
            # store-independent and can run concurrently across signals.
            queries, query_embeddings, signal_embedding = await self._prepare(description, case)

            async with self._match_lock:
                match_result, specificity_result, candidates = await self._match(
                    description, case, queries, query_embeddings
                )
                self._record_match_quality(
                    case, match_result, specificity_result, queries, query_embeddings, candidates
                )
                self._persist_signal(record_id, description, case, specificity_result, signal_embedding)

            self.progress.signal_done()
        except BaseException as e:
            logging.getLogger(__name__).exception("Signal %d (group %d) failed", record_id, case.group_index)
            # A dropped connection would otherwise silently shrink the dataset, which reads as a
            # grouping difference when comparing two runs. Nothing is persisted until the last step,
            # so replaying the signal is safe.
            if attempt < TRANSPORT_RETRIES and not isinstance(e, asyncio.CancelledError):
                await asyncio.sleep(2**attempt)
                await self.run_signal_pipeline(record_id, case, attempt + 1)
                return
            self.progress.signal_dropped()

    async def _prepare(
        self, description: str, case: EvalSignalCase
    ) -> tuple[list[str], list[list[float]], list[float]]:
        """Generate search queries and compute all embeddings. No store mutations."""

        queries = await generate_search_queries(
            GenerateSearchQueriesInput(
                team_id=EVAL_TEAM_ID,
                description=description,
                source_product=case.signal.config.source_product,
                source_type=case.signal.config.source_type,
                signal_type_examples=self.store.get_type_examples(),
            )
        )

        all_embeddings = await asyncio.gather(
            *[self.store.embed(q) for q in queries],
            self.store.embed(description),
        )
        query_embeddings = list(all_embeddings[: len(queries)])
        signal_embedding = all_embeddings[-1]

        return queries, query_embeddings, signal_embedding

    async def _match(
        self,
        description: str,
        case: EvalSignalCase,
        queries: list[str],
        query_embeddings: list[list[float]],
    ) -> tuple[MatchResult, MatchResult, list[list[SignalCandidate]]]:
        """Search, LLM-match, and verify specificity. Must run under _match_lock."""

        candidates = [self.store.search(emb) for emb in query_embeddings]

        match_result = await match_signal_to_report(
            MatchSignalToReportInput(
                team_id=EVAL_TEAM_ID,
                description=description,
                source_product=case.signal.config.source_product,
                source_type=case.signal.config.source_type,
                queries=queries,
                query_results=candidates,
                report_contexts=self.report_store.get_contexts(),
            )
        )
        specificity_match_result = match_result

        if isinstance(specificity_match_result, ExistingReportMatch):
            report_ctx = self.report_store.get(specificity_match_result.report_id)
            report_title = report_ctx.context.title if report_ctx else ""
            group_signals = self.store.get_signals_for_report(specificity_match_result.report_id)

            specificity_result = await verify_match_specificity(
                team_id=EVAL_TEAM_ID,
                new_signal_description=description,
                new_signal_source_product=case.signal.config.source_product,
                new_signal_source_type=case.signal.config.source_type,
                report_title=report_title,
                group_signals=group_signals,
            )

            specificity_meta = SpecificityMetadata(
                pr_title=specificity_result.pr_title,
                specific_enough=specificity_result.specific_enough,
                reason=specificity_result.reason,
            )

            if specificity_result.specific_enough:
                specificity_match_result.match_metadata.specificity = specificity_meta
            else:
                specificity_match_result = NewReportMatch(
                    title=description.split("\n")[0],
                    summary=f"Split from group: {report_title}",
                    match_metadata=NoMatchMetadata(
                        reason=f'PR-specificity rejected: "{specificity_result.pr_title}" — {specificity_result.reason}',
                        specificity_rejection=specificity_meta,
                    ),
                )

        return match_result, specificity_match_result, candidates

    def _persist_signal(
        self,
        record_id: int,
        description: str,
        case: EvalSignalCase,
        match_result: MatchResult,
        signal_embedding: list[float],
    ) -> str:
        """Write match result to both stores. Must run under _match_lock."""

        report_id = match_result.report_id if isinstance(match_result, ExistingReportMatch) else str(uuid.uuid4())

        self.report_store.insert(report_id, match_result, case.group_index)

        self.store.store(
            signal_id=f"sig-{record_id}",
            content=description,
            embedding=signal_embedding,
            report_id=report_id,
            source_product=case.signal.config.source_product,
            source_type=case.signal.config.source_type,
            source_id="",
            weight=1.0,
        )

        return report_id

    async def pre_emit(self, record_id: int, case: EvalSignalCase) -> str | None:
        output = case.signal.content
        config = case.signal.config

        outputs = [output]

        if config.summarization_prompt is not None and config.description_summarization_threshold_chars is not None:
            outputs = await _summarize_long_descriptions(
                team=self._eval_team(),
                outputs=outputs,
                summarization_prompt=config.summarization_prompt,
                threshold=config.description_summarization_threshold_chars,
                extra={},
            )

        output = outputs[0]

        if config.actionability_prompt:
            is_actionable = await check_actionability(
                self.gateway_client,
                EVAL_TEAM_ID,
                output,
                config.actionability_prompt,
                context_fields=config.actionability_context_fields,
            )
            await self._record_pre_emit_actionability(case, None, is_actionable)
            if not is_actionable:
                return None

        return output.description or None

    async def _record_safety_filter(self, case: EvalSignalCase, result):
        passed = result.safe == case.safe
        self.results.record(
            eval_name="signal-safety-filter",
            item_name=f"filter-{case.group_index}-{case.signal_index}",
            input=case.signal.content.description,
            output="SAFE" if result.safe else f"UNSAFE ({result.threat_type})",
            expected="SAFE" if case.safe else "UNSAFE",
            metrics=[
                EvalMetric(
                    name="correct_classification",
                    score=1.0 if passed else 0.0,
                    reasoning=result.explanation,
                ),
            ],
            passed=passed,
        )

    async def _record_pre_emit_actionability(self, case: EvalSignalCase, thoughts: str | None, outcome: bool):
        passed = outcome == case.actionable
        self.results.record(
            eval_name=f"{case.signal.config.source_product.lower()}-actionability-check",
            item_name=f"case-{case.group_index}-{case.signal_index}",
            input=case.signal.content.description,
            output="ACTIONABLE" if outcome else "NOT_ACTIONABLE",
            expected="ACTIONABLE" if case.actionable else "NOT_ACTIONABLE",
            metrics=[
                EvalMetric(
                    name="correct_classification",
                    score=1.0 if passed else 0.0,
                    reasoning=thoughts,
                )
            ],
            passed=passed,
        )

    def _record_match_quality(
        self,
        case: EvalSignalCase,
        match_result: MatchResult,
        specificity_match_result: MatchResult,
        queries: list[str],
        query_embeddings: list[list[float]],
        candidates: list[list[SignalCandidate]],
    ):
        """Records whether the matching decision was correct and classifies the failure mode."""
        expected_report = self.report_store.find_report_by_group_index(case.group_index)
        expected_id = expected_report.context.report_id if expected_report else None
        expected = "NEW_REPORT" if expected_report is None else "EXISTING_REPORT"

        def evaluate_match_failure(mr: MatchResult) -> MatchFailureMode:
            is_existing = isinstance(mr, ExistingReportMatch)
            if expected_report is None:
                return MatchFailureMode.OVERGROUP if is_existing else MatchFailureMode.NONE
            if isinstance(mr, ExistingReportMatch) and mr.report_id == expected_id:
                return MatchFailureMode.NONE
            if is_existing:
                return MatchFailureMode.OVERGROUP
            return MatchFailureMode.UNDERGROUP

        match_failure_mode = evaluate_match_failure(match_result)
        specificity_failure_mode = evaluate_match_failure(specificity_match_result)
        correct = specificity_failure_mode == MatchFailureMode.NONE
        pre_specificity_correct = match_failure_mode == MatchFailureMode.NONE

        specificity_reasoning = (
            specificity_match_result.match_metadata.reason
            if hasattr(specificity_match_result.match_metadata, "reason")
            else ""
        )

        # Query diversity: average pairwise cosine distance between query embeddings
        if len(query_embeddings) < 2:
            query_diversity = 0.0
        else:
            distances = [
                self.store.cosine_distance(query_embeddings[i], query_embeddings[j])
                for i, j in combinations(range(len(query_embeddings)), 2)
            ]
            query_diversity = sum(distances) / len(distances)

        # Candidate diversity
        if len(candidates) < 2:
            candidate_diversity = 0.0
        else:
            candidate_sets = [{c.signal_id for c in cands} for cands in candidates]
            jaccards = []
            for i, j in combinations(range(len(candidate_sets)), 2):
                union = candidate_sets[i] | candidate_sets[j]
                if not union:
                    jaccards.append(0.0)
                else:
                    intersection = candidate_sets[i] & candidate_sets[j]
                    jaccards.append(1.0 - len(intersection) / len(union))
            candidate_diversity = sum(jaccards) / len(jaccards)

        is_existing = isinstance(specificity_match_result, ExistingReportMatch)
        report_id = (
            specificity_match_result.report_id if isinstance(specificity_match_result, ExistingReportMatch) else None
        )

        output = {
            "report": "EXISTING_REPORT" if is_existing else "NEW_REPORT",
            "specificity_reasoning": specificity_reasoning,
            "queries": queries,
            "report_signals": [sig.content for sig in self.store.get_signals_for_report(report_id)]
            if report_id
            else None,
        }

        self.results.record(
            eval_name="match-quality",
            item_name=f"match-{case.group_index}-{case.signal_index}",
            input=case.signal.content.description,
            output=output,
            expected=expected,
            metrics=[
                EvalMetric(
                    name="correct_match",
                    score=1.0 if correct else 0.0,
                    reasoning=None if correct else f"Failure mode: {specificity_failure_mode.value}",
                ),
                EvalMetric(
                    name="correct_match_pre_specificity",
                    score=1.0 if pre_specificity_correct else 0.0,
                    reasoning=None if pre_specificity_correct else f"Failure mode: {match_failure_mode.value}",
                ),
                EvalMetric(
                    name="query_diversity",
                    score=query_diversity,
                ),
                EvalMetric(
                    name="candidate_diversity",
                    score=candidate_diversity,
                ),
            ],
            passed=correct,
        )

    async def _judge_reports(self):
        await asyncio.gather(*[self._judge_single_report(report) for report in self.report_store.all_reports()])

    async def _judge_single_report(self, report):
        report_id = report.context.report_id
        signals = self.store.get_signals_for_report(report_id)
        if not signals:
            return

        try:
            expected_safe = all(GROUP_DATA[g].safe for g in report.true_signal_groups)

            safety_result = await judge_report_safety(team_id=EVAL_TEAM_ID, signals=signals)

            report.safety_choice = safety_result.choice
            passed = safety_result.choice == expected_safe

            self.results.record(
                eval_name="report-safety-check",
                item_name=f"report-{report_id[:12]}",
                input=f"report-{report_id[:12]} ({len(signals)} signals)",
                output="SAFE" if safety_result.choice else "UNSAFE",
                expected="SAFE" if expected_safe else "UNSAFE",
                metrics=[
                    EvalMetric(
                        name="correct_classification",
                        score=1.0 if passed else 0.0,
                        reasoning=safety_result.explanation,
                    ),
                ],
                passed=passed,
            )
            self.progress.report_judged()
        except BaseException:
            logging.getLogger(__name__).exception("Judging report %s failed", report_id[:12])
            self.progress.report_judged()

    def _record_grouping_quality(self):
        """Per-report metrics: purity, is_pure, group_recall."""
        for report in self.report_store.all_reports():
            groups = report.true_signal_groups
            total = len(groups)
            if total == 0:
                continue

            dominant_group = report.true_group_index
            dominant_count = groups.count(dominant_group)

            purity = dominant_count / total
            is_pure = dominant_count == total

            # recall: what fraction of the dominant group's total signals landed here
            total_in_group = len(GROUP_DATA[dominant_group].signals)
            group_recall = dominant_count / total_in_group
            input = [sig.content for sig in self.store.get_signals_for_report(report.context.report_id)]

            self.results.record(
                eval_name="grouping-quality",
                item_name=f"report-{report.context.report_id[:12]}",
                input=input,
                output=report.context.title,
                expected=f"group-{dominant_group}",
                metrics=[
                    EvalMetric(name="purity", score=purity),
                    EvalMetric(name="is_pure", score=1.0 if is_pure else 0.0),
                    EvalMetric(name="group_recall", score=group_recall),
                ],
            )

    def _record_aggregate_metrics(self, stream: list[EvalSignalCase]):
        """Global clustering metrics: ARI, homogeneity, completeness, mean_purity, mean_group_recall, malicious_leaked_rate."""

        true_labels: list[int] = []
        pred_labels: list[str] = []
        reports = self.report_store.all_reports()

        for report in reports:
            for group_index in report.true_signal_groups:
                true_labels.append(group_index)
                pred_labels.append(report.context.report_id)

        purities: list[float] = []
        group_recalls: list[float] = []
        for report in reports:
            groups = report.true_signal_groups
            dominant_count = groups.count(report.true_group_index)
            purities.append(dominant_count / len(groups))
            total_in_group = len(GROUP_DATA[report.true_group_index].signals)
            group_recalls.append(dominant_count / total_in_group)

        ari = adjusted_rand_score(true_labels, pred_labels)
        homogeneity, completeness, v_measure = homogeneity_completeness_v_measure(true_labels, pred_labels)
        mean_purity = sum(purities) / len(purities) if purities else 0.0
        mean_group_recall = sum(group_recalls) / len(group_recalls) if group_recalls else 0.0

        n_groups_expected = len({case.group_index for case in stream})
        n_reports_actual = len(reports)

        # Compute unsafe signal blocking rate: what % of unsafe signals never make it
        # through the pipeline — either dropped at pre-emit (not in any report) or
        # caught by the safety judge on their report.
        unsafe_group_indices = {i for i, g in enumerate(GROUP_DATA) if not g.safe}
        stream_unsafe_groups = unsafe_group_indices & {case.group_index for case in stream}
        total_unsafe = sum(len(GROUP_DATA[gi].signals) for gi in stream_unsafe_groups)

        # Count unsafe signals that leaked through: present in a report the safety judge called safe
        unsafe_leaked = 0
        for report in reports:
            if report.safety_choice is True:
                unsafe_leaked += sum(1 for gi in report.true_signal_groups if gi in unsafe_group_indices)

        unsafe_leaked_rate = unsafe_leaked / total_unsafe if total_unsafe > 0 else 0.0

        tqdm.write(
            f"\nResults ({n_groups_expected} groups → {n_reports_actual} reports):\n"
            f"  ARI              {ari:.2f}\n"
            f"  Homogeneity      {homogeneity:.2f}\n"
            f"  Completeness     {completeness:.2f}\n"
            f"  Mean purity      {mean_purity:.2f}\n"
            f"  Mean recall      {mean_group_recall:.2f}\n"
            f"  Malicious leaked {unsafe_leaked}/{total_unsafe}",
            file=sys.stderr,
        )

        self.results.record(
            eval_name="grouping-aggregate",
            item_name="aggregate statistics",
            input=f"{len(stream)} signals, {n_groups_expected} true groups",
            output=f"{n_reports_actual} reports",
            expected=f"{n_groups_expected} reports",
            metrics=[
                EvalMetric(
                    name="ari",
                    score=ari,
                ),
                EvalMetric(
                    name="homogeneity",
                    score=homogeneity,
                ),
                EvalMetric(
                    name="completeness",
                    score=completeness,
                ),
                EvalMetric(
                    name="mean_purity",
                    score=mean_purity,
                ),
                EvalMetric(
                    name="group_recall",
                    score=mean_group_recall,
                ),
                EvalMetric(
                    name="malicious_leaked_rate",
                    score=unsafe_leaked_rate,
                    reasoning=f"{unsafe_leaked}/{total_unsafe} malicious signals leaked through",
                ),
            ],
        )