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.

44.8 KB
Raw Blame
import { MakeLogicType, actions, afterMount, connect, kea, listeners, path, props, reducers, selectors } from 'kea'
import { loaders } from 'kea-loaders'
import { router, urlToAction } from 'kea-router'
import posthog from 'posthog-js'

import api from 'lib/api'
import { getSeriesColor } from 'lib/colors'
import { dayjs } from 'lib/dayjs'
import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'
import { trackedActionToUrl } from 'lib/logic/scenes/trackedActionToUrl'
import { hogql } from 'lib/utils/hogql'
import { urls } from 'scenes/urls'

import { AnyPropertyFilter, Breadcrumb } from '~/types'

import { aiObservabilitySharedLogic } from '../aiObservabilitySharedLogic'
import type { ApplyUrlStatePayload } from '../aiObservabilitySharedLogic'
import { llmEvaluationsLogic, waitForEvaluationsSettled } from '../evaluations/llmEvaluationsLogic'
import { loadClusterMetrics } from './clusterMetricsLoader'
import type { ClusterScatterSeries } from './clusterScatter'
import {
    AI_OBSERVABILITY_CLUSTERS_SCENE_TAG,
    CLUSTERING_RUNS_LOOKBACK_DAYS,
    MAX_CLUSTERING_RUNS,
    NOISE_CLUSTER_ID,
    OUTLIER_COLOR,
} from './constants'
import { loadFilterMatchedItemIds } from './filteredItemIdsLoader'
import {
    EvaluationItemAttributes,
    EvaluationVerdict,
    loadEvaluationItemAttributes,
    loadTraceSummaries,
} from './traceSummaryLoader'
import {
    Cluster,
    ClusterMetrics,
    ClusteringLevel,
    ClusteringParams,
    ClusteringRun,
    ClusteringRunOption,
    TraceSummary,
    getLevelFromRunId,
    getTimestampBoundsFromRunId,
    parseClusterMetrics,
} from './types'

/** Map a clustering level to the ClickHouse event name its runs are emitted under. */
function eventNameForLevel(
    level: ClusteringLevel
): '$ai_trace_clusters' | '$ai_generation_clusters' | '$ai_evaluation_clusters' {
    if (level === 'generation') {
        return '$ai_generation_clusters'
    }
    if (level === 'evaluation') {
        return '$ai_evaluation_clusters'
    }
    return '$ai_trace_clusters'
}

export type ClustersLogicProps = Record<string, never>

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface clustersLogicValues {
    propertyFilters: AnyPropertyFilter[] // aiObservabilitySharedLogic
    shouldFilterTestAccounts: boolean // aiObservabilitySharedLogic
    detectorEvaluationIds: string[] // llmEvaluationsLogic
    anyFiltersActive: boolean
    availableEvaluatorNames: {
        count: number
        name: string
    }[]
    availableVerdictCounts: Record<EvaluationVerdict, number>
    breadcrumbs: Breadcrumb[]
    clusterMembershipPredicate: (id: string) => boolean
    clusterMetrics: Record<number, ClusterMetrics>
    clusterMetricsLoading: boolean
    clusteringLevel: ClusteringLevel
    clusteringRuns: ClusteringRunOption[]
    clusteringRunsLoading: boolean
    currentRun: ClusteringRun | null
    currentRunLoading: boolean
    currentRunMatchesLevel: boolean
    effectiveRunId: string | null
    evalFilterEvaluatorNames: string[]
    evalFilterPredicate: (evalId: string) => boolean
    evalFilterVerdicts: EvaluationVerdict[]
    evalFiltersActive: boolean
    evaluationItemAttributes: Record<string, EvaluationItemAttributes>
    expandedClusterIds: Set<number>
    filteredItemCount: number
    filteredSortedClusters: Cluster[]
    isClusterExpanded: (clusterId: number) => boolean
    isScatterPlotExpanded: boolean
    propertyFilteredItemIds: Set<string> | null
    propertyFilteredItemIdsLoading: boolean
    propertyFiltersActive: boolean
    scatterPlotSeries: ClusterScatterSeries[]
    selectedRunId: string | null
    sortedClusters: Cluster[]
    totalItemCount: number
    traceSummaries: Record<string, TraceSummary>
    traceSummariesLoading: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface clustersLogicActions {
    applyUrlState: (state: ApplyUrlStatePayload) => ApplyUrlStatePayload // aiObservabilitySharedLogic
    setPropertyFilters: (propertyFilters: AnyPropertyFilter[]) => {
        propertyFilters: AnyPropertyFilter[]
    } // aiObservabilitySharedLogic
    setShouldFilterTestAccounts: (shouldFilterTestAccounts: boolean) => {
        shouldFilterTestAccounts: boolean
    } // aiObservabilitySharedLogic
    clearEvalFilters: () => {
        value: true
    }
    loadClusterMetricsForRun: (run: ClusteringRun) => {
        run: ClusteringRun
    }
    loadClusteringRun: (runId: string) => string
    loadClusteringRunFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadClusteringRunSuccess: (
        currentRun: ClusteringRun | null,
        payload?: string
    ) => {
        currentRun: ClusteringRun | null
        payload?: string
    }
    loadClusteringRuns: () => any
    loadClusteringRunsFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadClusteringRunsSuccess: (
        clusteringRuns: {
            label: string
            runId: string
            windowEnd: string
        }[],
        payload?: any
    ) => {
        clusteringRuns: {
            label: string
            runId: string
            windowEnd: string
        }[]
        payload?: any
    }
    loadEvaluationAttributesForRun: (run: ClusteringRun) => {
        run: ClusteringRun
    }
    loadPropertyFilteredItemIds: () => {
        value: true
    }
    loadPropertyFilteredItemIdsFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadPropertyFilteredItemIdsSuccess: (
        propertyFilteredItemIds: Set<string> | null,
        payload?: {
            value: true
        }
    ) => {
        propertyFilteredItemIds: Set<string> | null
        payload?: {
            value: true
        }
    }
    loadTraceSummariesForRun: (run: ClusteringRun) => {
        run: ClusteringRun
    }
    setClusterMetrics: (metrics: Record<number, ClusterMetrics>) => {
        metrics: Record<number, ClusterMetrics>
    }
    setClusterMetricsLoading: (loading: boolean) => {
        loading: boolean
    }
    setClusteringLevel: (level: ClusteringLevel) => {
        level: ClusteringLevel
    }
    setEvalEvaluatorNamesFilter: (names: string[]) => {
        names: string[]
    }
    setEvalVerdictsFilter: (verdicts: EvaluationVerdict[]) => {
        verdicts: EvaluationVerdict[]
    }
    setEvaluationItemAttributes: (attributes: Record<string, EvaluationItemAttributes>) => {
        attributes: Record<string, EvaluationItemAttributes>
    }
    setSelectedRunId: (runId: string | null) => {
        runId: string | null
    }
    setTraceSummaries: (summaries: Record<string, TraceSummary>) => {
        summaries: Record<string, TraceSummary>
    }
    setTraceSummariesLoading: (loading: boolean) => {
        loading: boolean
    }
    syncClusteringLevelFromRun: (level: ClusteringLevel) => {
        level: ClusteringLevel
    }
    toggleClusterExpanded: (clusterId: number) => {
        clusterId: number
    }
    toggleScatterPlotExpanded: () => {
        value: true
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface clustersLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        effectiveRunId: (selectedRunId: string | null, clusteringRuns: ClusteringRunOption[]) => string | null
        currentRunMatchesLevel: (currentRun: ClusteringRun | null, clusteringLevel: ClusteringLevel) => boolean
        sortedClusters: (currentRun: ClusteringRun | null, currentRunMatchesLevel: boolean) => Cluster[]
        evalFiltersActive: (
            clusteringLevel: ClusteringLevel,
            evalFilterEvaluatorNames: string[],
            evalFilterVerdicts: EvaluationVerdict[]
        ) => boolean
        availableEvaluatorNames: (evaluationItemAttributes: Record<string, EvaluationItemAttributes>) => {
            count: number
            name: string
        }[]
        availableVerdictCounts: (
            evaluationItemAttributes: Record<string, EvaluationItemAttributes>
        ) => Record<EvaluationVerdict, number>
        evalFilterPredicate: (
            clusteringLevel: ClusteringLevel,
            evaluationItemAttributes: Record<string, EvaluationItemAttributes>,
            evalFilterEvaluatorNames: string[],
            evalFilterVerdicts: EvaluationVerdict[]
        ) => (evalId: string) => boolean
        propertyFiltersActive: (propertyFilters: AnyPropertyFilter[], shouldFilterTestAccounts: boolean) => boolean
        anyFiltersActive: (evalFiltersActive: boolean, propertyFiltersActive: boolean) => boolean
        clusterMembershipPredicate: (
            evalFilterPredicate: (evalId: string) => boolean,
            propertyFilteredItemIds: Set<string> | null
        ) => (id: string) => boolean
        filteredSortedClusters: (
            sortedClusters: Cluster[],
            clusterMembershipPredicate: (id: string) => boolean,
            anyFiltersActive: boolean
        ) => Cluster[]
        filteredItemCount: (filteredSortedClusters: Cluster[]) => number
        totalItemCount: (sortedClusters: Cluster[]) => number
        isClusterExpanded: (expandedClusterIds: Set<number>) => (clusterId: number) => boolean
        scatterPlotSeries: (filteredSortedClusters: Cluster[]) => ClusterScatterSeries[]
    }
}

export type clustersLogicType = MakeLogicType<
    clustersLogicValues,
    clustersLogicActions,
    ClustersLogicProps,
    clustersLogicMeta
>

export const clustersLogic = kea<clustersLogicType>([
    path(['products', 'ai_observability', 'frontend', 'clusters', 'clustersLogic']),
    props({} as ClustersLogicProps),

    connect(() => ({
        values: [
            aiObservabilitySharedLogic,
            ['propertyFilters', 'shouldFilterTestAccounts'],
            llmEvaluationsLogic,
            ['detectorEvaluationIds'],
        ],
        actions: [aiObservabilitySharedLogic, ['setPropertyFilters', 'setShouldFilterTestAccounts', 'applyUrlState']],
    })),

    actions({
        setClusteringLevel: (level: ClusteringLevel) => ({ level }),
        // Declared explicitly so kea-typegen emits a no-arg signature for the loader
        // action below — without this, the `(_, breakpoint)` loader signature forces
        // every call site to pass a placeholder argument.
        loadPropertyFilteredItemIds: true,
        syncClusteringLevelFromRun: (level: ClusteringLevel) => ({ level }),
        setSelectedRunId: (runId: string | null) => ({ runId }),
        toggleClusterExpanded: (clusterId: number) => ({ clusterId }),
        toggleScatterPlotExpanded: true,
        setTraceSummaries: (summaries: Record<string, TraceSummary>) => ({ summaries }),
        setTraceSummariesLoading: (loading: boolean) => ({ loading }),
        loadTraceSummariesForRun: (run: ClusteringRun) => ({ run }),
        setClusterMetrics: (metrics: Record<number, ClusterMetrics>) => ({ metrics }),
        setClusterMetricsLoading: (loading: boolean) => ({ loading }),
        loadClusterMetricsForRun: (run: ClusteringRun) => ({ run }),
        setEvaluationItemAttributes: (attributes: Record<string, EvaluationItemAttributes>) => ({ attributes }),
        loadEvaluationAttributesForRun: (run: ClusteringRun) => ({ run }),
        setEvalEvaluatorNamesFilter: (names: string[]) => ({ names }),
        setEvalVerdictsFilter: (verdicts: EvaluationVerdict[]) => ({ verdicts }),
        clearEvalFilters: true,
    }),

    reducers({
        clusteringLevel: [
            'trace' as ClusteringLevel,
            {
                setClusteringLevel: (_, { level }) => level,
                // Sync from run without triggering reload (used when loading a run from URL)
                syncClusteringLevelFromRun: (_, { level }) => level,
            },
        ],
        selectedRunId: [
            null as string | null,
            {
                setSelectedRunId: (_, { runId }) => runId,
                // Clear selected run when level changes
                setClusteringLevel: () => null,
            },
        ],
        expandedClusterIds: [
            new Set<number>() as Set<number>,
            {
                toggleClusterExpanded: (state, { clusterId }) => {
                    const newSet = new Set(state)
                    if (newSet.has(clusterId)) {
                        newSet.delete(clusterId)
                    } else {
                        newSet.add(clusterId)
                    }
                    return newSet
                },
            },
        ],
        traceSummaries: [
            {} as Record<string, TraceSummary>,
            {
                setTraceSummaries: (state, { summaries }) => ({ ...state, ...summaries }),
            },
        ],
        traceSummariesLoading: [
            false,
            {
                setTraceSummariesLoading: (_, { loading }) => loading,
            },
        ],
        isScatterPlotExpanded: [
            true,
            {
                toggleScatterPlotExpanded: (state) => !state,
            },
        ],
        clusterMetrics: [
            {} as Record<number, ClusterMetrics>,
            {
                setClusterMetrics: (_, { metrics }) => metrics,
                // Clear metrics when level changes (new run will load fresh metrics)
                setClusteringLevel: () => ({}),
            },
        ],
        clusterMetricsLoading: [
            false,
            {
                setClusterMetricsLoading: (_, { loading }) => loading,
            },
        ],
        evaluationItemAttributes: [
            {} as Record<string, EvaluationItemAttributes>,
            {
                setEvaluationItemAttributes: (_, { attributes }) => attributes,
                // Wipe on level switches so a stale lookup doesn't leak across levels.
                // Wipe on loadClusteringRun (rather than setSelectedRunId) so a run switch
                // doesn't briefly show the previous run's counts in the filter bar before
                // the new attributes load. Using setSelectedRunId would also wipe on the
                // nav-back URL handler that fires setSelectedRunId(null) without reloading,
                // which would leave the filter bar empty and un-refillable.
                setClusteringLevel: () => ({}),
                loadClusteringRun: () => ({}),
            },
        ],
        evalFilterEvaluatorNames: [
            [] as string[],
            {
                setEvalEvaluatorNamesFilter: (_, { names }) => names,
                clearEvalFilters: () => [],
                setClusteringLevel: () => [],
                setSelectedRunId: () => [],
            },
        ],
        evalFilterVerdicts: [
            [] as EvaluationVerdict[],
            {
                setEvalVerdictsFilter: (_, { verdicts }) => verdicts,
                clearEvalFilters: () => [],
                setClusteringLevel: () => [],
                setSelectedRunId: () => [],
            },
        ],
    }),

    loaders(({ values }) => ({
        // Subset of the current run's item IDs that match the user's active property
        // filters (cohorts, person properties, group properties, etc.). Null when no
        // filters are active or filtering can't be applied — selectors treat null as
        // "show everything" so unfiltered runs skip the round-trip.
        propertyFilteredItemIds: [
            null as Set<string> | null,
            {
                loadPropertyFilteredItemIds: async (_, breakpoint) => {
                    // Debounce overlapping filter changes (quick toggles or successive
                    // cohort selections) into a single round-trip.
                    await breakpoint(150)

                    const run = values.currentRun
                    if (!run) {
                        return null
                    }

                    const matched = await loadFilterMatchedItemIds({
                        itemIds: run.clusters.flatMap((cluster) => Object.keys(cluster.traces)),
                        level: run.level || values.clusteringLevel,
                        windowStart: run.windowStart,
                        windowEnd: run.windowEnd,
                        propertyFilters: values.propertyFilters || [],
                        filterTestAccounts: values.shouldFilterTestAccounts,
                        scene: AI_OBSERVABILITY_CLUSTERS_SCENE_TAG,
                    })
                    breakpoint()
                    return matched
                },
            },
        ],

        clusteringRuns: [
            [] as ClusteringRunOption[],
            {
                loadClusteringRuns: async () => {
                    const eventName = eventNameForLevel(values.clusteringLevel)

                    // Look back a wide window rather than a hard 7 days: scheduled runs are
                    // emitted ~daily but a team can go several days without a fresh one, and a
                    // narrow window made the page go empty the moment the last run aged out.
                    // MAX_CLUSTERING_RUNS still bounds the result.
                    const response = await api.queryHogQL(
                        hogql`
                            SELECT
                                properties.$ai_clustering_run_id as run_id,
                                properties.$ai_window_end as window_end,
                                timestamp
                            FROM events
                            WHERE event = ${eventName}
                                AND timestamp >= now() - INTERVAL ${hogql.raw(String(CLUSTERING_RUNS_LOOKBACK_DAYS))} DAY
                            ORDER BY timestamp DESC
                            LIMIT ${MAX_CLUSTERING_RUNS}
                        `,
                        { productKey: 'llm_analytics', scene: 'AIObservabilityClusters' },
                        {
                            refresh: 'force_blocking',
                            // Run IDs are generated in UTC, so compare timestamps in UTC
                            queryParams: { modifiers: { convertToProjectTimezone: false } },
                        }
                    )

                    return (response?.results || []).map((row: string[]) => ({
                        runId: row[0],
                        windowEnd: row[1],
                        label: dayjs(row[2]).format('MMM D, YYYY h:mm A'),
                    }))
                },
            },
        ],

        currentRun: [
            null as ClusteringRun | null,
            {
                loadClusteringRun: async (runId: string) => {
                    const { dayStart, dayEnd } = getTimestampBoundsFromRunId(runId)
                    // Derive level from runId to ensure correct event is queried even on direct URL navigation
                    const level = getLevelFromRunId(runId)
                    const eventName = eventNameForLevel(level)

                    const response = await api.queryHogQL(
                        hogql`
                            SELECT
                                properties.$ai_clustering_run_id as run_id,
                                properties.$ai_window_start as window_start,
                                properties.$ai_window_end as window_end,
                                toInt(properties.$ai_total_items_analyzed) as total_items,
                                properties.$ai_clusters as clusters,
                                timestamp,
                                properties.$ai_clustering_params as clustering_params,
                                properties.$ai_clustering_level as clustering_level
                            FROM events
                            WHERE event = ${eventName}
                                AND timestamp >= ${dayStart}
                                AND timestamp <= ${dayEnd}
                                AND properties.$ai_clustering_run_id = ${runId}
                            LIMIT 1
                        `,
                        { productKey: 'llm_analytics', scene: 'AIObservabilityClusters' },
                        // Run IDs and bounds are in UTC, so compare timestamps in UTC
                        { queryParams: { modifiers: { convertToProjectTimezone: false } } }
                    )

                    if (!response?.results?.length) {
                        return null
                    }

                    const row = response.results[0] as (string | number)[]

                    let clustersData: Cluster[] = []
                    try {
                        const rawClusters: Array<Record<string, unknown>> = JSON.parse((row[4] as string) || '[]')
                        // The backend emits per-cluster aggregate metrics with snake_case
                        // field names (dataclasses.asdict on ClusterAggregateMetrics);
                        // normalize to the frontend's camelCase ClusterMetrics shape here
                        // so consumers don't need to know both conventions.
                        clustersData = rawClusters.map((raw) => {
                            const { metrics: rawMetrics, ...rest } = raw as { metrics?: unknown } & Record<
                                string,
                                unknown
                            >
                            const parsed = parseClusterMetrics(rawMetrics)
                            return {
                                ...(rest as unknown as Cluster),
                                ...(parsed ? { metrics: parsed } : {}),
                            }
                        })
                    } catch {
                        console.error('Failed to parse clusters data')
                        return null
                    }

                    let clusteringParams: ClusteringParams | undefined
                    const clusteringParamsRaw = row[6] as string | null
                    if (clusteringParamsRaw) {
                        try {
                            clusteringParams = JSON.parse(clusteringParamsRaw)
                        } catch {
                            // Non-critical, continue without params
                        }
                    }

                    return {
                        runId: row[0] as string,
                        windowStart: row[1] as string,
                        windowEnd: row[2] as string,
                        totalItemsAnalyzed: row[3] as number,
                        clusters: clustersData,
                        timestamp: row[5] as string,
                        clusteringParams,
                        level: (row[7] as ClusteringLevel) || level,
                    } as ClusteringRun
                },
            },
        ],
    })),

    selectors({
        breadcrumbs: [
            () => [],
            (): Breadcrumb[] => [
                {
                    key: 'AIObservabilityClusters',
                    name: 'Clusters',
                    path: urls.aiObservabilityClusters(),
                    iconType: 'llm_clusters',
                },
            ],
        ],

        effectiveRunId: [
            (s) => [s.selectedRunId, s.clusteringRuns],
            (selectedRunId: string | null, runs: ClusteringRunOption[]): string | null => {
                if (selectedRunId) {
                    return selectedRunId
                }
                return runs.length > 0 ? runs[0].runId : null
            },
        ],

        // True while the loaded run still belongs to the level the user is currently
        // looking at. Goes false the moment the user switches levels, until the new
        // level's first run resolves — gates downstream selectors so we don't paint
        // trace cards on the Generations tab during the load. kea-loaders preserves
        // the previous loader value, so without this check `currentRun` would still
        // be the trace-level run for a few hundred milliseconds.
        //
        // Runs emitted before the `clustering_level` event property existed are inferred
        // as 'trace' by `loadClusteringRun` via `getLevelFromRunId`, so the same default
        // applies here for any path that bypasses the loader (e.g. test fixtures).
        currentRunMatchesLevel: [
            (s) => [s.currentRun, s.clusteringLevel],
            (currentRun: ClusteringRun | null, level: ClusteringLevel): boolean =>
                !!currentRun && (currentRun.level || 'trace') === level,
        ],

        sortedClusters: [
            (s) => [s.currentRun, s.currentRunMatchesLevel],
            (currentRun: ClusteringRun | null, matches: boolean): Cluster[] => {
                if (!matches || !currentRun?.clusters) {
                    return []
                }
                return [...currentRun.clusters].sort((a, b) => b.size - a.size)
            },
        ],

        evalFiltersActive: [
            (s) => [s.clusteringLevel, s.evalFilterEvaluatorNames, s.evalFilterVerdicts],
            (level: ClusteringLevel, names: string[], verdicts: EvaluationVerdict[]): boolean =>
                level === 'evaluation' && (names.length > 0 || verdicts.length > 0),
        ],

        availableEvaluatorNames: [
            (s) => [s.evaluationItemAttributes],
            (attrs: Record<string, EvaluationItemAttributes>): { name: string; count: number }[] => {
                const counts = new Map<string, number>()
                for (const a of Object.values(attrs)) {
                    counts.set(a.evaluatorName, (counts.get(a.evaluatorName) || 0) + 1)
                }
                return Array.from(counts.entries())
                    .map(([name, count]) => ({ name, count }))
                    .sort((a, b) => b.count - a.count)
            },
        ],

        availableVerdictCounts: [
            (s) => [s.evaluationItemAttributes],
            (attrs: Record<string, EvaluationItemAttributes>): Record<EvaluationVerdict, number> => {
                const counts: Record<EvaluationVerdict, number> = { pass: 0, fail: 0, 'n/a': 0, unknown: 0 }
                for (const a of Object.values(attrs)) {
                    counts[a.verdict] = (counts[a.verdict] || 0) + 1
                }
                return counts
            },
        ],

        // Single predicate keeps the filtering rule in one place; consumers (scatter, cards,
        // distribution bar) all derive from this so any divergence is impossible.
        evalFilterPredicate: [
            (s) => [s.clusteringLevel, s.evaluationItemAttributes, s.evalFilterEvaluatorNames, s.evalFilterVerdicts],
            (
                level: ClusteringLevel,
                attrs: Record<string, EvaluationItemAttributes>,
                names: string[],
                verdicts: EvaluationVerdict[]
            ): ((evalId: string) => boolean) => {
                if (level !== 'evaluation' || (names.length === 0 && verdicts.length === 0)) {
                    return () => true
                }
                // Attributes haven't loaded yet — show everything to avoid an empty flash.
                if (Object.keys(attrs).length === 0) {
                    return () => true
                }
                const nameSet = new Set(names)
                const verdictSet = new Set(verdicts)
                return (evalId: string) => {
                    const a = attrs[evalId]
                    if (!a) {
                        return false
                    }
                    if (nameSet.size > 0 && !nameSet.has(a.evaluatorName)) {
                        return false
                    }
                    if (verdictSet.size > 0 && !verdictSet.has(a.verdict)) {
                        return false
                    }
                    return true
                }
            },
        ],

        propertyFiltersActive: [
            (s) => [s.propertyFilters, s.shouldFilterTestAccounts],
            (propertyFilters: AnyPropertyFilter[], shouldFilterTestAccounts: boolean): boolean =>
                (propertyFilters?.length ?? 0) > 0 || shouldFilterTestAccounts,
        ],

        anyFiltersActive: [
            (s) => [s.evalFiltersActive, s.propertyFiltersActive],
            (evalActive: boolean, propertyActive: boolean): boolean => evalActive || propertyActive,
        ],

        // Single predicate combines the eval-specific filter (for evaluation-level clusters)
        // with the cohort/property filter (for trace and generation levels). Cluster cards,
        // scatter plot, and distribution bar all derive from this so the views can never
        // diverge on which items count as "in" the filtered set.
        clusterMembershipPredicate: [
            (s) => [s.evalFilterPredicate, s.propertyFilteredItemIds],
            (
                evalPredicate: (id: string) => boolean,
                propertyFilteredItemIds: Set<string> | null
            ): ((id: string) => boolean) => {
                return (id: string) => {
                    if (!evalPredicate(id)) {
                        return false
                    }
                    if (propertyFilteredItemIds && !propertyFilteredItemIds.has(id)) {
                        return false
                    }
                    return true
                }
            },
        ],

        filteredSortedClusters: [
            (s) => [s.sortedClusters, s.clusterMembershipPredicate, s.anyFiltersActive],
            (clusters: Cluster[], predicate: (id: string) => boolean, active: boolean): Cluster[] => {
                if (!active) {
                    return clusters
                }
                return clusters
                    .map((cluster) => {
                        const filteredTraces = Object.fromEntries(
                            Object.entries(cluster.traces).filter(([id]) => predicate(id))
                        )
                        return {
                            ...cluster,
                            traces: filteredTraces,
                            size: Object.keys(filteredTraces).length,
                        }
                    })
                    .filter((c) => c.size > 0)
            },
        ],

        filteredItemCount: [
            (s) => [s.filteredSortedClusters],
            (clusters: Cluster[]): number => clusters.reduce((sum, c) => sum + c.size, 0),
        ],

        totalItemCount: [
            (s) => [s.sortedClusters],
            (clusters: Cluster[]): number => clusters.reduce((sum, c) => sum + c.size, 0),
        ],

        isClusterExpanded: [
            (s) => [s.expandedClusterIds],
            (expandedIds: Set<number>) =>
                (clusterId: number): boolean =>
                    expandedIds.has(clusterId),
        ],

        scatterPlotSeries: [
            (s) => [s.filteredSortedClusters],
            (clusters: Cluster[]): ClusterScatterSeries[] => {
                const itemSeries: ClusterScatterSeries[] = []
                const centroidSeries: ClusterScatterSeries[] = []

                for (const cluster of clusters) {
                    const isOutlier = cluster.cluster_id === NOISE_CLUSTER_ID
                    const color = isOutlier ? OUTLIER_COLOR : getSeriesColor(cluster.cluster_id)
                    const label = cluster.title || `Cluster ${cluster.cluster_id}`

                    // Item points (traces or generations)
                    itemSeries.push({
                        key: `cluster-${cluster.cluster_id}`,
                        label,
                        color,
                        pointRadius: 4,
                        shape: isOutlier ? 'cross' : 'circle', // X shape for outliers
                        points: Object.entries(cluster.traces).map(([itemKey, itemInfo]) => ({
                            x: itemInfo.x,
                            y: itemInfo.y,
                            meta: {
                                traceId: itemInfo.trace_id || itemKey,
                                generationId: itemInfo.generation_id,
                                timestamp: itemInfo.timestamp,
                            },
                        })),
                    })

                    // Centroid marker (skip for outliers - they don't have a real centroid)
                    if (!isOutlier) {
                        centroidSeries.push({
                            key: `centroid-${cluster.cluster_id}`,
                            label: `${label} (centroid)`,
                            color,
                            pointRadius: 8,
                            shape: 'circle',
                            points: [
                                {
                                    x: cluster.centroid_x,
                                    y: cluster.centroid_y,
                                    meta: { clusterId: cluster.cluster_id, isCentroid: true },
                                },
                            ],
                        })
                    }
                }

                return [...itemSeries, ...centroidSeries]
            },
        ],
    }),

    listeners(({ actions, values }) => ({
        setClusteringLevel: ({ level }) => {
            posthog.capture('llma clusters level changed', { level })
            // Reload runs when level changes
            actions.loadClusteringRuns()
        },

        loadClusterMetricsForRun: async ({ run }) => {
            if (!run.clusters || run.clusters.length === 0) {
                return
            }

            const level = run.level || values.clusteringLevel

            // Evaluation clusters ship with metrics baked into the event by the backend
            // (ClusterAggregateMetrics → dataclasses.asdict → $ai_clusters[i].metrics).
            // Use those directly instead of a second HogQL round-trip that tries to
            // recompute from the events table — the backend already joined eval →
            // generation and computed both operational + eval-specific metrics.
            if (level === 'evaluation') {
                const baked: Record<number, ClusterMetrics> = {}
                for (const cluster of run.clusters) {
                    if (cluster.metrics) {
                        baked[cluster.cluster_id] = cluster.metrics
                    }
                }
                actions.setClusterMetrics(baked)
                return
            }

            actions.setClusterMetricsLoading(true)

            try {
                const metrics = await loadClusterMetrics(run.clusters, run.windowStart, run.windowEnd, level)
                actions.setClusterMetrics(metrics)
            } catch (error) {
                console.error('Failed to load cluster metrics:', error)
            } finally {
                actions.setClusterMetricsLoading(false)
            }
        },

        loadEvaluationAttributesForRun: async ({ run }) => {
            if (run.level !== 'evaluation') {
                return
            }
            const allItemIds: string[] = []
            for (const cluster of run.clusters) {
                allItemIds.push(...Object.keys(cluster.traces))
            }
            if (allItemIds.length === 0) {
                return
            }
            try {
                // detectorEvaluationIds reads as [] until llmEvaluationsLogic's own fetch
                // settles. Wait for it here so a clustering response that resolves first
                // can't bake a wrong (non-detector) verdict into a cached summary that never
                // gets refetched — loadTraceSummaries only fetches ids missing from the cache.
                // No separate level check needed here, unlike the other call sites below: the
                // early return above already confines this listener to evaluation-level runs.
                await waitForEvaluationsSettled()
                const attrs = await loadEvaluationItemAttributes(
                    allItemIds,
                    run.windowStart,
                    run.windowEnd,
                    values.detectorEvaluationIds
                )
                // Guard against a slow response for a prior run overwriting attributes
                // after the user has switched to a different run. If currentRun has
                // advanced past the one we were loading for, drop the response on the
                // floor — the new run's load will have already fired.
                if (values.currentRun?.runId && values.currentRun.runId !== run.runId) {
                    return
                }
                actions.setEvaluationItemAttributes(attrs)
            } catch (error) {
                console.error('Failed to load evaluation item attributes:', error)
            }
        },

        loadTraceSummariesForRun: async ({ run }) => {
            // Collect all item IDs from all clusters
            const allItemIds: string[] = []
            for (const cluster of run.clusters) {
                allItemIds.push(...Object.keys(cluster.traces))
            }

            actions.setTraceSummariesLoading(true)

            try {
                // Same rationale as loadEvaluationAttributesForRun: only evaluation-level
                // clustering derives a verdict, so only that level needs to wait.
                if (values.clusteringLevel === 'evaluation') {
                    await waitForEvaluationsSettled()
                }
                const summaries = await loadTraceSummaries(
                    allItemIds,
                    values.traceSummaries,
                    run.windowStart,
                    run.windowEnd,
                    values.clusteringLevel,
                    values.detectorEvaluationIds
                )
                actions.setTraceSummaries(summaries)
            } catch (error) {
                console.error('Failed to load trace summaries:', error)
            } finally {
                actions.setTraceSummariesLoading(false)
            }
        },

        toggleClusterExpanded: async ({ clusterId }) => {
            posthog.capture('llma clusters cluster expanded', {
                cluster_id: clusterId,
                run_id: values.effectiveRunId,
            })
            // Load summaries when expanding a cluster (fallback for lazy loading)
            if (values.expandedClusterIds.has(clusterId)) {
                const run = values.currentRun
                const cluster = run?.clusters.find((c: Cluster) => c.cluster_id === clusterId)
                if (cluster && run) {
                    const itemIds = Object.keys(cluster.traces)

                    actions.setTraceSummariesLoading(true)

                    try {
                        // Same rationale as loadEvaluationAttributesForRun.
                        if (values.clusteringLevel === 'evaluation') {
                            await waitForEvaluationsSettled()
                        }
                        const summaries = await loadTraceSummaries(
                            itemIds,
                            values.traceSummaries,
                            run.windowStart,
                            run.windowEnd,
                            values.clusteringLevel,
                            values.detectorEvaluationIds
                        )
                        actions.setTraceSummaries(summaries)
                    } catch (error) {
                        console.error('Failed to load trace summaries:', error)
                    } finally {
                        actions.setTraceSummariesLoading(false)
                    }
                }
            }
        },

        loadClusteringRunSuccess: ({ currentRun }) => {
            // Sync clusteringLevel with the loaded run's level (without triggering reload)
            // This handles direct URL navigation to a run with a different level
            if (currentRun?.level && currentRun.level !== values.clusteringLevel) {
                actions.syncClusteringLevelFromRun(currentRun.level)
                // Reload runs for the correct level so the dropdown shows proper labels
                actions.loadClusteringRuns()
            }
            // Load all trace summaries when a run is loaded for scatter plot tooltips
            if (currentRun) {
                if (currentRun.clusters.length === 0) {
                    posthog.capture('llma clusters empty state shown', {
                        reason: 'no_clusters_in_run',
                        clustering_level: currentRun.level || values.clusteringLevel,
                        run_id: currentRun.runId,
                    })
                }
                actions.loadTraceSummariesForRun(currentRun)
                // Load cluster metrics for displaying averages in cluster cards
                actions.loadClusterMetricsForRun(currentRun)
                // For evaluation runs, also load the per-item (evaluator name, verdict) lookup
                // so the post-hoc filter bar can drive scatter + cluster card filtering.
                actions.loadEvaluationAttributesForRun(currentRun)
                // Re-run the property filter against the new run's items. The shared filter
                // state is preserved across run switches, so a previously filtered view
                // should immediately narrow the new run's clusters.
                actions.loadPropertyFilteredItemIds()
            }
        },

        // Property filter state lives in the shared logic; mirror its updates into a
        // fresh query scoped to the current run's items. Both listener entries are
        // needed because the dispatch path differs by trigger:
        //   - UI clicks dispatch `setPropertyFilters` / `setShouldFilterTestAccounts`
        //     directly. The shared logic's `actionToUrl` then echoes the new state into
        //     the URL, but `applySearchParams` short-circuits because URL and state
        //     already agree, so `applyUrlState` is *not* fired in this path.
        //   - Deep links and browser back/forward fire the URL handler first; that
        //     path detects the divergence and dispatches `applyUrlState` (not
        //     `setPropertyFilters`) to sync state.
        // The 150ms breakpoint debounce inside the loader collapses any stray double
        // dispatches into a single round-trip.
        setPropertyFilters: () => actions.loadPropertyFilteredItemIds(),
        setShouldFilterTestAccounts: () => actions.loadPropertyFilteredItemIds(),
        applyUrlState: () => actions.loadPropertyFilteredItemIds(),

        loadClusteringRunFailure: () => {
            lemonToast.error('Failed to load clustering run')
        },

        loadClusteringRunsSuccess: ({ clusteringRuns }) => {
            if (clusteringRuns.length === 0) {
                posthog.capture('llma clusters empty state shown', {
                    reason: 'no_clustering_runs',
                    clustering_level: values.clusteringLevel,
                })
            }
            // Auto-load the first run if available and no run is selected
            if (clusteringRuns.length > 0 && !values.selectedRunId) {
                actions.loadClusteringRun(clusteringRuns[0].runId)
            }
        },

        toggleScatterPlotExpanded: () => {
            posthog.capture('llma clusters scatter plot toggled', {
                expanded: values.isScatterPlotExpanded,
            })
        },

        setSelectedRunId: ({ runId }) => {
            if (runId) {
                posthog.capture('llma clusters run selected', { run_id: runId })
                actions.loadClusteringRun(runId)
            }
        },
    })),

    afterMount(({ actions }) => {
        actions.loadClusteringRuns()
    }),

    urlToAction(({ actions, values }) => ({
        [urls.aiObservabilityClusters()]: () => {
            if (values.selectedRunId !== null) {
                actions.setSelectedRunId(null)
            }
        },
        '/ai-observability/clusters/:runId': ({ runId }: { runId?: string }) => {
            // Decode the URL-encoded runId. Only re-dispatch when the runId actually
            // changed — a filter change updates the URL's search params (?filters=…)
            // but keeps the same path, and re-firing `setSelectedRunId` here would
            // chain into `loadClusteringRun` → `loadClusteringRunSuccess` →
            // `loadPropertyFilteredItemIds`, which races with the loader the
            // `setPropertyFilters` listener already kicked off and silently aborts it.
            const newRunId = runId ? decodeURIComponent(runId) : null
            if (newRunId !== values.selectedRunId) {
                actions.setSelectedRunId(newRunId)
            }
        },
    })),

    trackedActionToUrl(({ values }) => ({
        setSelectedRunId: () => {
            // Preserve any search params already on the URL — `setPropertyFilters` (in the
            // shared logic) writes `?filters=...` and `?filter_test_accounts=...`, and the
            // route handler immediately echoes that back through `setSelectedRunId(currentRunId)`.
            // Returning a bare path here would strip those params, the URL handler would re-fire
            // with an empty `filters` set, and the property filter would be reset on every change.
            const pathname = values.selectedRunId
                ? urls.aiObservabilityClusters(values.selectedRunId)
                : urls.aiObservabilityClusters()
            return [pathname, router.values.searchParams]
        },
    })),
])