Skip to content

Trace-native CI/CD for AI agents — production failures become regression tests that block the PR. Auto-detect, cluster, freeze into hermetic cases, replay in CI for $0.

6.6 KB
Raw Blame
"""Failure clustering — group similar auto-detected failures into `FailureCluster`s using the
cheap structural signature (failed-evaluator set + masked failure text).

Ingest-time stage (design 07/91). The semantic embedding pass lives in
`tracely.services.failure_intel_service`. Human-in-the-loop: clusters are promoted to
regression cases, not auto-promoted.

The value object lives in `tracely.domain.failure.signature` — this service just persists.
"""

from __future__ import annotations

import uuid
from datetime import datetime, timezone

import structlog
from sqlalchemy import desc, select
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm import Session

from tracely.config import settings
from tracely.domain.failure.signature import FailureSignature
from tracely.infrastructure.db.models import ClusterMember, FailureCluster

log = structlog.get_logger()

# ponytail: only the N most recently active clusters of an agent are considered for a fuzzy
# merge. Older ones are re-formed by Analyze anyway; raise it if agents run many failure modes.
_MERGE_SCAN_LIMIT = 200


class StructuralClusteringService:
    """Upserts a `FailureCluster` keyed by the masked signature and records the trace as a member."""

    def __init__(self, session: Session) -> None:
        self.session = session

    def cluster_failure(
        self,
        project_id: str,
        agent_id: str,
        trace_id: str,
        eval_failures: list,
        spans: list[dict],
    ) -> str | None:
        """Returns the cluster id (existing or new), or None if there's nothing to cluster."""
        if not agent_id or not eval_failures:
            return None
        sig = FailureSignature.compute(eval_failures, spans)
        # The cluster's own signature is MASKED (that is what makes two traces the same failure);
        # the member keeps the unmasked reason, so the linked-traces list can say what went wrong
        # in THIS trace instead of showing a wall of identical ids.
        summary = "; ".join(f.comment for f in eval_failures if f.comment)[:1000]
        now = datetime.now(timezone.utc)

        cl = self._find_existing(project_id, agent_id, sig.key) or self._find_similar(
            project_id, agent_id, sig
        )
        if cl is None:
            try:
                cl = self._create(project_id, agent_id, sig, now)
                self._record_member(cl, trace_id, now, summary)
                self.session.commit()
                self._notify_new_cluster(cl)
                return cl.id
            except IntegrityError:
                # Two workers raced on the same brand-new signature; the other insert won the
                # (project, agent, cluster_key) unique constraint. Join its row instead.
                self.session.rollback()
                cl = self._find_existing(project_id, agent_id, sig.key)
                if cl is None:
                    raise

        self._record_member(cl, trace_id, now, summary)
        self.session.commit()
        return cl.id

    def _notify_new_cluster(self, cl: FailureCluster) -> None:
        """A signature no trace has produced before is a NEW failure mode in production — the one
        alert that is genuinely trace-native, and the reason a cluster is worth a page while the
        1000th instance of a known one is not. Best-effort: an alerting hiccup must never break the
        eval that found the failure."""
        try:
            from tracely.services.alert_events import cluster_event
            from tracely.services.monitoring_service import notify_event

            notify_event(cl.project_id, cluster_event(self.session, cl))
        except Exception as exc:
            log.warning("cluster_notify_failed", cluster_id=cl.id, error=str(exc))

    # ── internals ─────────────────────────────────────────────────────────────

    def _find_existing(
        self, project_id: str, agent_id: str, cluster_key: str
    ) -> FailureCluster | None:
        return self.session.execute(
            select(FailureCluster).where(
                FailureCluster.project_id == project_id,
                FailureCluster.agent_id == agent_id,
                FailureCluster.cluster_key == cluster_key,
            )
        ).scalar_one_or_none()

    def _find_similar(
        self, project_id: str, agent_id: str, sig: FailureSignature
    ) -> FailureCluster | None:
        """Join an existing cluster whose failure text says the same thing in different words.

        Without this every LLM-judge comment hashes to its own cluster and the list is one row
        per failing trace. IGNOREd clusters are skipped — a merge would resurrect them.
        """
        threshold = settings.cluster_merge_similarity
        if threshold > 1:
            return None  # merging disabled — exact signature only
        candidates = self.session.execute(
            select(FailureCluster)
            .where(
                FailureCluster.project_id == project_id,
                FailureCluster.agent_id == agent_id,
                FailureCluster.status != "IGNORED",
            )
            .order_by(desc(FailureCluster.last_seen_at))
            .limit(_MERGE_SCAN_LIMIT)
        ).scalars()
        return next((c for c in candidates if sig.matches(c.signature or "", threshold)), None)

    def _create(
        self,
        project_id: str,
        agent_id: str,
        sig: FailureSignature,
        now: datetime,
    ) -> FailureCluster:
        cl = FailureCluster(
            id=str(uuid.uuid4()),
            project_id=project_id,
            agent_id=agent_id,
            cluster_key=sig.key,
            label=sig.label,
            taxonomy=sig.taxonomy,
            signature=sig.signature[:2000],
            count=0,
            status="OPEN",
            first_seen_at=now,
            last_seen_at=now,
        )
        self.session.add(cl)
        self.session.flush()
        return cl

    def _record_member(
        self, cl: FailureCluster, trace_id: str, now: datetime, summary: str = ""
    ) -> None:
        member = self.session.get(ClusterMember, (cl.id, trace_id))
        if member is None:
            self.session.add(
                ClusterMember(
                    cluster_id=cl.id,
                    trace_id=trace_id,
                    is_medoid=(cl.count == 0),
                    summary=summary,
                )
            )
            cl.count = (cl.count or 0) + 1
        elif summary and not member.summary:
            member.summary = summary  # heal a row recorded before summaries were kept
        cl.last_seen_at = now