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.

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

import api from 'lib/api'
import { FEATURE_FLAGS } from 'lib/constants'
import { dayjs } from 'lib/dayjs'
import { lemonToast } from 'lib/lemon-ui/LemonToast'
import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
import { hogql } from 'lib/utils/hogql'
import { projectLogic } from 'scenes/projectLogic'
import { Scene } from 'scenes/sceneTypes'
import { teamLogic } from 'scenes/teamLogic'
import { urls } from 'scenes/urls'

import { Breadcrumb } from '~/types'

import { cohortsCreate } from 'products/cohorts/frontend/generated/api'

import type { FeatureFlagsSet } from '../../../frontend/src/lib/logic/featureFlagLogic'
import {
    AgentSearch,
    ExperimentLogFilter,
    ExperimentLogGroup,
    type RunRole,
    SearchPoint,
    type StatusBrief,
    buildAgentSearch,
    experimentLogGroups,
    runRoles,
    scoredChallenger,
    statusBrief,
} from './agentSearch'
import {
    autoresearchModelsList,
    autoresearchOnlinePerformanceRetrieve,
    autoresearchPartialUpdate,
    autoresearchPauseCreate,
    autoresearchResumeCreate,
    autoresearchRetrieve,
    autoresearchRunsList,
    autoresearchRunsRetrieve,
    autoresearchScoreCreate,
    autoresearchSuggestionsCreate,
    autoresearchSuggestionsList,
    autoresearchTrainingRunsArtifactsGetCreate,
    autoresearchTrainingRunsArtifactsRetrieve,
    autoresearchTrainingRunsList,
    autoresearchTrainCreate,
} from './generated/api'
import {
    type AutoresearchModelApi,
    AutoresearchModelRoleEnumApi,
    type AutoresearchPipelineApi,
    type AutoresearchRunApi,
    type AutoresearchSuggestionApi,
    type AutoresearchTrainingRunApi,
    CreateSuggestionPriorityEnumApi,
    type ModelExplanationFieldApi,
    type OnlinePerformanceRowApi,
    type PredictionSegmentThresholdsApi,
} from './generated/api.schemas'
import type { ConfusionByCutoffApi } from './generated/api.schemas'
import {
    type AccuracyCutoff,
    type AccuracyTrend,
    type AccuracyTrendMetric,
    type PooledConfusion,
    SegmentCalibration,
    accuracyTrend,
    calibrationBySegment,
    firstCheckDate,
    latestChampionRow,
    pooledConfusion,
    validatedPredictionDates,
} from './onlinePerformance'
import { LifecycleStep, pipelineLifecycle } from './pipelineLifecycle'
import { type CoveragePoint, type CoverageSummary, coverageHistory, coverageSummary } from './predictionCoverage'
import {
    PREDICTION_SEGMENT_KEYS,
    type PredictionLinkDestination,
    type PredictionSegmentCounts,
    type PredictionSegmentDefinition,
    type PredictionSegmentKey,
    predictionSegmentCohortFilters,
    predictionSegmentDefinitions,
} from './predictionSegments'
import type { PredictionsPeopleView } from './predictionsPeopleQuery'

/** Where a person opened an experiment from. */
export type ExperimentOpenSource = 'chart' | 'log'

/** The card whose cutoff picker changed the shared cutoff. */
export type AccuracyCutoffSource = 'flagged' | 'trend'

export interface AutoresearchPipelineLogicProps {
    id: string
}

export type AutoresearchPipelineTab = 'predictions' | 'accuracy' | 'agent_research' | 'setup'

const AUTORESEARCH_PIPELINE_TABS: AutoresearchPipelineTab[] = ['predictions', 'accuracy', 'agent_research', 'setup']

/** Tab keys from the old five-tab layout, so links saved before the change still open the right tab. */
const LEGACY_PIPELINE_TABS: Record<string, AutoresearchPipelineTab | null> = {
    overview: null,
    training: 'agent_research',
    online_performance: 'accuracy',
    suggestions: 'agent_research',
}

/** The tab a `?tab=` value opens. Null means the default tab. */
export function pipelineTabFromUrl(value: unknown): AutoresearchPipelineTab | null {
    if (typeof value !== 'string') {
        return null
    }
    if ((AUTORESEARCH_PIPELINE_TABS as string[]).includes(value)) {
        return value as AutoresearchPipelineTab
    }
    return LEGACY_PIPELINE_TABS[value] ?? null
}

/** Where a person changed whether scores are written to person profiles. */
export type PersonPropertyToggleSource = 'actions_panel' | 'setup_tab'

/** How often the Score now button checks a running scoring run. */
export const SCORE_RUN_POLL_INTERVAL_MS = 5000

/** How often the Agent research tab reloads training runs while one is pending or running. */
export const TRAINING_RUN_POLL_INTERVAL_MS = 10000

/** How often the runs reload while a person property cleanup is pending or running. A cleanup takes over an hour. */
export const PROPERTY_CLEANUP_POLL_INTERVAL_MS = 30000

function isTrainingRunLive(run: AutoresearchTrainingRunApi): boolean {
    return run.status === 'pending' || run.status === 'running'
}

/** Matches the backend cutoff: a run still running after the workflow timeout lost its worker. */
export const SCORE_RUN_STALE_AFTER_MS = 5 * 60 * 60 * 1000

function isScoreRunInProgress(run: AutoresearchRunApi): boolean {
    return run.status === 'running' || run.status === 'pending'
}

/** The inference run that is scoring now, if any. Scoring runs in the background, so a reload resumes from it. */
function findRunningScoreRun(runs: AutoresearchRunApi[]): AutoresearchRunApi | null {
    const cutoff = Date.now() - SCORE_RUN_STALE_AFTER_MS
    return (
        runs.find(
            (r) =>
                r.run_type === 'inference' &&
                isScoreRunInProgress(r) &&
                new Date(r.started_at ?? r.created_at).getTime() >= cutoff
        ) ?? null
    )
}

/** How far back the Predictions tab looks for the latest scoring batch, so it never scans the full history. */
export const LATEST_BATCH_LOOKBACK_DAYS = 90

/** One decile of the latest scoring run's predicted probabilities: `lower` ≤ p < `lower` + 0.1. */
export interface ProbabilityBucket {
    lower: number
    users: number
}

/** The list endpoints page at 100 rows. Follow every page so a long-lived model shows its whole history. */
async function fetchAllPages<T>(
    fetchPage: (offset: number) => Promise<{ results: T[]; next?: string | null }>
): Promise<T[]> {
    const rows: T[] = []
    for (;;) {
        const response = await fetchPage(rows.length)
        rows.push(...response.results)
        if (!response.next || response.results.length === 0) {
            return rows
        }
    }
}

/** A decoded artifact bundle file, ready for display. */
interface ViewedArtifact {
    runId: string
    path: string
    sizeBytes: number
    /** UTF-8 text for text files; null when the file is binary (e.g. model.pkl, parquet). */
    text: string | null
}

/** Bundle paths we render inline as text; anything else is treated as binary. */
const TEXT_ARTIFACT_EXTENSIONS = ['.py', '.sql', '.yml', '.yaml', '.json', '.md', '.txt', '.ipynb', '.csv']

function isTextArtifact(path: string): boolean {
    return TEXT_ARTIFACT_EXTENSIONS.some((ext) => path.toLowerCase().endsWith(ext))
}

function base64ToUtf8(base64: string): string {
    const binary = atob(base64)
    const bytes = new Uint8Array(binary.length)
    for (let i = 0; i < binary.length; i++) {
        bytes[i] = binary.charCodeAt(i)
    }
    return new TextDecoder('utf-8').decode(bytes)
}

/** Displayed progress for a training run. */
interface TrainingRunProgress {
    iterationCount: number
    bestHoldoutScore: number | null
}

/**
 * The persisted iteration_count and best_holdout_score are only written at completion,
 * so while a run is in flight we derive progress from the iteration rows, which land live.
 */
export function trainingRunProgress(run: AutoresearchTrainingRunApi): TrainingRunProgress {
    // Only completion writes the counters; a failed run keeps them at zero, so read its recorded iterations.
    if (run.status === 'completed') {
        return { iterationCount: run.iteration_count, bestHoldoutScore: run.best_holdout_score }
    }
    const scores = run.iterations.map((it) => it.holdout_score).filter((score): score is number => score != null)
    return {
        iterationCount: run.iterations.length,
        bestHoldoutScore: scores.length > 0 ? Math.max(...scores) : null,
    }
}

/**
 * Features in the run model's reported top drivers but not the champion's, and the reverse.
 * Each list is capped and can be partial, so a feature missing from a list can still be a model input.
 */
export interface FeatureChanges {
    added: string[]
    dropped: string[]
}

export function featureChanges(
    runExplanation: ModelExplanationFieldApi,
    championExplanation: ModelExplanationFieldApi
): FeatureChanges {
    const runNames = (runExplanation.top_features ?? []).map((f) => f.name)
    const championNames = (championExplanation.top_features ?? []).map((f) => f.name)
    // An empty list means the model recorded no importances, which says nothing about its features.
    if (runNames.length === 0 || championNames.length === 0) {
        return { added: [], dropped: [] }
    }
    return {
        added: runNames.filter((name) => !championNames.includes(name)),
        dropped: championNames.filter((name) => !runNames.includes(name)),
    }
}

/** How much of the inference population the latest scoring run covered, when it scored only part of it. */
export interface ScoringCoverage {
    scored: number
    eligible: number
    /** Days it takes to score everyone once: the runs a rotation needs times the days between runs. */
    rescoreDays: number
}

/**
 * A population at or above the scoring cap is scored on a rolling basis: each run scores the
 * people whose last score is oldest. Returns null when the latest completed run scored everyone.
 */
export function scoringCoverage(runs: AutoresearchRunApi[], cadenceDays: number): ScoringCoverage | null {
    const latest = runs
        .filter((run) => run.run_type === 'inference' && run.status === 'completed')
        .reduce<AutoresearchRunApi | null>(
            (newest, run) => (newest === null || run.created_at > newest.created_at ? run : newest),
            null
        )
    const scored = latest?.rows_scored ?? 0
    const eligible = latest?.metrics?.rows_eligible
    if (scored <= 0 || typeof eligible !== 'number' || eligible <= scored) {
        return null
    }
    return { scored, eligible, rescoreDays: Math.ceil(eligible / scored) * Math.max(cadenceDays, 1) }
}

/** One scoring day's volume: emitted prediction events and their average probability as a 0-100 percentage. */
export interface DailyVolumePoint {
    day: string
    users: number
    avgProbabilityPct: number
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface autoresearchPipelineLogicValues {
    featureFlags: FeatureFlagsSet // featureFlagLogic
    currentProjectId: number | null // projectLogic
    currentTeamId: number | null // teamLogic
    accuracyCutoff: AccuracyCutoff
    accuracyTrend: AccuracyTrend
    accuracyTrendMetric: AccuracyTrendMetric
    activeScoreRun: AutoresearchRunApi | null
    activeTab: AutoresearchPipelineTab
    agentSearch: AgentSearch
    artifactsByRun: Record<string, string[]>
    artifactsByRunLoading: boolean
    breadcrumbs: Breadcrumb[]
    champion: AutoresearchModelApi | null
    championConfusion: PooledConfusion | null
    coverageHistory: CoveragePoint[]
    coverageSummary: CoverageSummary | null
    dailyVolume: DailyVolumePoint[] | null
    dailyVolumeError: boolean
    dailyVolumeLoading: boolean
    defaultTab: AutoresearchPipelineTab
    detailRequested: boolean
    expandedLogRunIds: string[]
    expandedRunId: string | null
    experimentLogFilter: ExperimentLogFilter
    experimentLogGroups: ExperimentLogGroup[]
    firstCheck: dayjs.Dayjs | null
    hasChallengerPerformance: boolean
    hasLiveTrainingRun: boolean
    hoveredAccuracyDate: string | null
    hoveredSearchPoint: SearchPoint | null
    latestChampionPerformance: OnlinePerformanceRowApi | null
    latestPropertyCleanupRun: AutoresearchRunApi | null
    lifecycleSteps: LifecycleStep[] | null
    modelByTrainingRun: Record<string, AutoresearchModelApi>
    models: AutoresearchModelApi[]
    modelsError: boolean
    modelsLoaded: boolean
    modelsLoading: boolean
    onlinePerformance: OnlinePerformanceRowApi[]
    onlinePerformanceError: boolean
    onlinePerformanceLoading: boolean
    openedExperiment: SearchPoint | null
    personPropertyDisabledReason: string | undefined
    pipeline: AutoresearchPipelineApi | null
    pipelineError: boolean
    pipelineLoading: boolean
    predictionSegments: PredictionSegmentCounts | null
    predictionSegmentsError: boolean
    predictionSegmentsLoading: boolean
    predictionsPeopleView: PredictionsPeopleView
    probabilityDistribution: ProbabilityBucket[] | null
    probabilityDistributionError: boolean
    probabilityDistributionLoading: boolean
    probabilityHistogram: ProbabilityBucket[] | null
    queuedSuggestionCount: number
    reportByRun: Record<string, string | null>
    reportByRunLoading: boolean
    runRoles: Record<string, RunRole>
    runs: AutoresearchRunApi[]
    runsError: boolean
    runsLoaded: boolean
    runsLoading: boolean
    savingCohortSegment: PredictionSegmentKey | null
    savingWritePersonProperty: boolean
    scoreResult: AutoresearchRunApi | null
    scoreResultLoading: boolean
    scoredChallenger: AutoresearchModelApi | null
    scoringCoverage: ScoringCoverage | null
    segmentCalibration: SegmentCalibration[]
    segmentDefinitions: PredictionSegmentDefinition[] | null
    segmentThresholds: PredictionSegmentThresholdsApi | null
    selectedTab: AutoresearchPipelineTab | null
    showChallengersInTrend: boolean
    startTrainingResult: AutoresearchTrainingRunApi | null
    startTrainingResultLoading: boolean
    statusBrief: StatusBrief | null
    suggestionDraft: string
    suggestionPriority: CreateSuggestionPriorityEnumApi
    suggestionSubmitResult: AutoresearchSuggestionApi | null
    suggestionSubmitResultLoading: boolean
    suggestions: AutoresearchSuggestionApi[]
    suggestionsError: boolean
    suggestionsLoading: boolean
    trainingRuns: AutoresearchTrainingRunApi[]
    trainingRunsError: boolean
    trainingRunsLoading: boolean
    viewedArtifact: ViewedArtifact | null
    viewedArtifactLoading: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface autoresearchPipelineLogicActions {
    setFeatureFlags: (
        flags: string[],
        variants: Record<string, boolean | string>
    ) => {
        flags: string[]
        variants: Record<string, boolean | string>
    } // featureFlagLogic
    loadCurrentTeamSuccess: (
        currentTeam: null | import('~/types').TeamPublicType,
        payload?: any
    ) => {
        currentTeam: null | import('~/types').TeamPublicType
        payload?: any
    } // teamLogic
    closeArtifact: () => any
    closeArtifactFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    closeArtifactSuccess: (
        viewedArtifact: null,
        payload?: any
    ) => {
        viewedArtifact: null
        payload?: any
    }
    closeExperiment: () => {
        value: true
    }
    experimentDetailsOpened: (point: SearchPoint) => {
        point: SearchPoint
    }
    fullNotesOpened: () => {
        value: true
    }
    loadDailyVolume: () => any
    loadDailyVolumeFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadDailyVolumeSuccess: (
        dailyVolume: {
            avgProbabilityPct: number
            day: string
            users: number
        }[],
        payload?: any
    ) => {
        dailyVolume: {
            avgProbabilityPct: number
            day: string
            users: number
        }[]
        payload?: any
    }
    loadDetail: () => {
        value: true
    }
    loadModels: () => any
    loadModelsFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadModelsSuccess: (
        models: AutoresearchModelApi[],
        payload?: any
    ) => {
        models: AutoresearchModelApi[]
        payload?: any
    }
    loadOnlinePerformance: () => any
    loadOnlinePerformanceFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadOnlinePerformanceSuccess: (
        onlinePerformance: OnlinePerformanceRowApi[],
        payload?: any
    ) => {
        onlinePerformance: OnlinePerformanceRowApi[]
        payload?: any
    }
    loadPipeline: () => any
    loadPipelineFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadPipelineSuccess: (
        pipeline: AutoresearchPipelineApi | null,
        payload?: any
    ) => {
        pipeline: AutoresearchPipelineApi | null
        payload?: any
    }
    loadPredictionSegments: () => any
    loadPredictionSegmentsFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadPredictionSegmentsSuccess: (
        predictionSegments: PredictionSegmentCounts | null,
        payload?: any
    ) => {
        predictionSegments: PredictionSegmentCounts | null
        payload?: any
    }
    loadProbabilityDistribution: () => any
    loadProbabilityDistributionFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadProbabilityDistributionSuccess: (
        probabilityDistribution: {
            lower: number
            users: number
        }[],
        payload?: any
    ) => {
        probabilityDistribution: {
            lower: number
            users: number
        }[]
        payload?: any
    }
    loadRunArtifacts: ({ runId }: { runId: string }) => {
        runId: string
    }
    loadRunArtifactsFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadRunArtifactsSuccess: (
        artifactsByRun: Record<string, string[]>,
        payload?: {
            runId: string
        }
    ) => {
        artifactsByRun: Record<string, string[]>
        payload?: {
            runId: string
        }
    }
    loadRunReport: ({ runId }: { runId: string }) => {
        runId: string
    }
    loadRunReportFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadRunReportSuccess: (
        reportByRun: Record<string, string | null>,
        payload?: {
            runId: string
        }
    ) => {
        reportByRun: Record<string, string | null>
        payload?: {
            runId: string
        }
    }
    loadRuns: () => any
    loadRunsFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadRunsSuccess: (
        runs: AutoresearchRunApi[],
        payload?: any
    ) => {
        runs: AutoresearchRunApi[]
        payload?: any
    }
    loadSuggestions: () => any
    loadSuggestionsFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadSuggestionsSuccess: (
        suggestions: AutoresearchSuggestionApi[],
        payload?: any
    ) => {
        suggestions: AutoresearchSuggestionApi[]
        payload?: any
    }
    loadTrainingRuns: () => any
    loadTrainingRunsFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadTrainingRunsSuccess: (
        trainingRuns: AutoresearchTrainingRunApi[],
        payload?: any
    ) => {
        trainingRuns: AutoresearchTrainingRunApi[]
        payload?: any
    }
    openExperiment: (
        point: SearchPoint,
        source: ExperimentOpenSource
    ) => {
        point: SearchPoint
        source: ExperimentOpenSource
    }
    pausePipeline: () => any
    pausePipelineFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    pausePipelineSuccess: (
        pipeline: AutoresearchPipelineApi | null,
        payload?: any
    ) => {
        pipeline: AutoresearchPipelineApi | null
        payload?: any
    }
    pollScoreRun: () => {
        value: true
    }
    pollTrainingRuns: () => {
        value: true
    }
    reportNotebookOpened: (runId: string) => {
        runId: string
    }
    reportPredictionLinkClicked: (destination: PredictionLinkDestination) => {
        destination: PredictionLinkDestination
    }
    resumePipeline: () => any
    resumePipelineFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    resumePipelineSuccess: (
        pipeline: AutoresearchPipelineApi | null,
        payload?: any
    ) => {
        pipeline: AutoresearchPipelineApi | null
        payload?: any
    }
    saveSegmentCohort: (segment: PredictionSegmentKey) => {
        segment: PredictionSegmentKey
    }
    saveSegmentCohortFinished: () => {
        value: true
    }
    scoreNow: () => any
    scoreNowFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    scoreNowSuccess: (
        scoreResult: AutoresearchRunApi | null,
        payload?: any
    ) => {
        scoreResult: AutoresearchRunApi | null
        payload?: any
    }
    scoreRunFinished: (run: AutoresearchRunApi) => {
        run: AutoresearchRunApi
    }
    searchPointClicked: (point: SearchPoint) => {
        point: SearchPoint
    }
    setAccuracyCutoff: (
        cutoff: AccuracyCutoff,
        source: AccuracyCutoffSource
    ) => {
        cutoff: keyof ConfusionByCutoffApi
        source: AccuracyCutoffSource
    }
    setAccuracyTrendMetric: (metric: AccuracyTrendMetric) => {
        metric: AccuracyTrendMetric
    }
    setActiveScoreRun: (run: AutoresearchRunApi | null) => {
        run: AutoresearchRunApi | null
    }
    setActiveTab: (tab: AutoresearchPipelineTab) => {
        tab: AutoresearchPipelineTab
    }
    setExperimentLogFilter: (filter: ExperimentLogFilter) => {
        filter: ExperimentLogFilter
    }
    setHoveredAccuracyDate: (date: string | null) => {
        date: string | null
    }
    setHoveredSearchPoint: (point: SearchPoint | null) => {
        point: SearchPoint | null
    }
    setPredictionsPeopleView: (view: PredictionsPeopleView) => {
        view: PredictionsPeopleView
    }
    setSegmentThresholds: (thresholds: PredictionSegmentThresholdsApi) => {
        thresholds: PredictionSegmentThresholdsApi
    }
    setShowChallengersInTrend: (show: boolean) => {
        show: boolean
    }
    setSuggestionDraft: (draft: string) => {
        draft: string
    }
    setSuggestionPriority: (priority: CreateSuggestionPriorityEnumApi) => {
        priority: CreateSuggestionPriorityEnumApi
    }
    setTabFromUrl: (tab: AutoresearchPipelineTab | null) => {
        tab: AutoresearchPipelineTab | null
    }
    setWritePersonProperty: (
        enabled: boolean,
        source: PersonPropertyToggleSource
    ) => {
        enabled: boolean
        source: PersonPropertyToggleSource
    }
    setWritePersonPropertyFinished: () => {
        value: true
    }
    startScorePolling: () => {
        value: true
    }
    startTraining: () => any
    startTrainingFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    startTrainingPolling: () => {
        value: true
    }
    startTrainingSuccess: (
        startTrainingResult: AutoresearchTrainingRunApi | null,
        payload?: any
    ) => {
        startTrainingResult: AutoresearchTrainingRunApi | null
        payload?: any
    }
    steerOpened: () => {
        value: true
    }
    submitSuggestion: () => any
    submitSuggestionFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    submitSuggestionSuccess: (
        suggestionSubmitResult: AutoresearchSuggestionApi | null,
        payload?: any
    ) => {
        suggestionSubmitResult: AutoresearchSuggestionApi | null
        payload?: any
    }
    toggleLogRun: (runId: string) => {
        runId: string
    }
    toggleRunArtifacts: (runId: string) => {
        runId: string
    }
    viewArtifact: ({ runId, path }: { path: string; runId: string }) => {
        runId: string
        path: string
    }
    viewArtifactFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    viewArtifactSuccess: (
        viewedArtifact: {
            path: string
            runId: string
            sizeBytes: number
            text: string | null
        } | null,
        payload?: {
            runId: string
            path: string
        }
    ) => {
        viewedArtifact: {
            path: string
            runId: string
            sizeBytes: number
            text: string | null
        } | null
        payload?: {
            runId: string
            path: string
        }
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface autoresearchPipelineLogicMeta {
    key: string
    __keaTypeGenInternalSelectorTypes: {
        breadcrumbs: (pipeline: AutoresearchPipelineApi | null) => Breadcrumb[]
        champion: (models: AutoresearchModelApi[]) => AutoresearchModelApi | null
        modelByTrainingRun: (models: AutoresearchModelApi[]) => Record<string, AutoresearchModelApi>
        scoringCoverage: (
            runs: AutoresearchRunApi[],
            pipeline: AutoresearchPipelineApi | null
        ) => ScoringCoverage | null
        coverageSummary: (
            runs: AutoresearchRunApi[],
            scoringCoverage: ScoringCoverage | null,
            pipeline: AutoresearchPipelineApi | null
        ) => CoverageSummary | null
        coverageHistory: (runs: AutoresearchRunApi[]) => CoveragePoint[]
        latestChampionPerformance: (onlinePerformance: OnlinePerformanceRowApi[]) => OnlinePerformanceRowApi | null
        accuracyTrend: (
            onlinePerformance: OnlinePerformanceRowApi[],
            accuracyTrendMetric: AccuracyTrendMetric,
            accuracyCutoff: keyof ConfusionByCutoffApi,
            showChallengersInTrend: boolean
        ) => AccuracyTrend
        hasChallengerPerformance: (onlinePerformance: OnlinePerformanceRowApi[]) => boolean
        segmentDefinitions: (
            segmentThresholds: PredictionSegmentThresholdsApi | null
        ) => PredictionSegmentDefinition[] | null
        segmentCalibration: (
            latestChampionPerformance: OnlinePerformanceRowApi | null,
            segmentThresholds: PredictionSegmentThresholdsApi | null
        ) => SegmentCalibration[]
        championConfusion: (
            onlinePerformance: OnlinePerformanceRowApi[],
            accuracyCutoff: keyof ConfusionByCutoffApi
        ) => PooledConfusion | null
        firstCheck: (runs: AutoresearchRunApi[], pipeline: AutoresearchPipelineApi | null) => dayjs.Dayjs | null
        probabilityHistogram: (probabilityDistribution: ProbabilityBucket[] | null) => ProbabilityBucket[] | null
        hasLiveTrainingRun: (trainingRuns: AutoresearchTrainingRunApi[]) => boolean
        scoredChallenger: (models: AutoresearchModelApi[]) => AutoresearchModelApi | null
        runRoles: (
            champion: AutoresearchModelApi | null,
            scoredChallenger: AutoresearchModelApi | null
        ) => Record<string, RunRole>
        queuedSuggestionCount: (suggestions: AutoresearchSuggestionApi[]) => number
        agentSearch: (
            trainingRuns: AutoresearchTrainingRunApi[],
            champion: AutoresearchModelApi | null,
            scoredChallenger: AutoresearchModelApi | null
        ) => AgentSearch
        experimentLogGroups: (
            trainingRuns: AutoresearchTrainingRunApi[],
            agentSearch: AgentSearch,
            experimentLogFilter: ExperimentLogFilter
        ) => ExperimentLogGroup[]
        statusBrief: (
            trainingRuns: AutoresearchTrainingRunApi[],
            champion: AutoresearchModelApi | null,
            scoredChallenger: AutoresearchModelApi | null
        ) => StatusBrief | null
        defaultTab: (pipeline: AutoresearchPipelineApi | null) => AutoresearchPipelineTab
        latestPropertyCleanupRun: (runs: AutoresearchRunApi[]) => AutoresearchRunApi | null
        personPropertyDisabledReason: (
            pipeline: AutoresearchPipelineApi | null,
            savingWritePersonProperty: boolean
        ) => string | undefined
        activeTab: (
            selectedTab: AutoresearchPipelineTab | null,
            defaultTab: AutoresearchPipelineTab
        ) => AutoresearchPipelineTab
        lifecycleSteps: (
            pipeline: AutoresearchPipelineApi | null,
            champion: AutoresearchModelApi | null,
            runs: AutoresearchRunApi[],
            modelsLoaded: boolean,
            runsLoaded: boolean
        ) => LifecycleStep[] | null
    }
}

export type autoresearchPipelineLogicType = MakeLogicType<
    autoresearchPipelineLogicValues,
    autoresearchPipelineLogicActions,
    AutoresearchPipelineLogicProps,
    autoresearchPipelineLogicMeta
>

export const autoresearchPipelineLogic = kea<autoresearchPipelineLogicType>([
    path((key) => ['products', 'autoresearch', 'autoresearchPipelineLogic', key]),
    props({} as AutoresearchPipelineLogicProps),
    key((props) => props.id),
    connect({
        values: [teamLogic, ['currentTeamId'], projectLogic, ['currentProjectId'], featureFlagLogic, ['featureFlags']],
        actions: [teamLogic, ['loadCurrentTeamSuccess'], featureFlagLogic, ['setFeatureFlags']],
    }),
    actions({
        setActiveTab: (tab: AutoresearchPipelineTab) => ({ tab }),
        setPredictionsPeopleView: (view: PredictionsPeopleView) => ({ view }),
        setAccuracyCutoff: (cutoff: AccuracyCutoff, source: AccuracyCutoffSource) => ({ cutoff, source }),
        setAccuracyTrendMetric: (metric: AccuracyTrendMetric) => ({ metric }),
        setShowChallengersInTrend: (show: boolean) => ({ show }),
        setHoveredAccuracyDate: (date: string | null) => ({ date }),
        setTabFromUrl: (tab: AutoresearchPipelineTab | null) => ({ tab }),
        loadDetail: true,
        toggleRunArtifacts: (runId: string) => ({ runId }),
        reportNotebookOpened: (runId: string) => ({ runId }),
        reportPredictionLinkClicked: (destination: PredictionLinkDestination) => ({ destination }),
        setSuggestionDraft: (draft: string) => ({ draft }),
        setSuggestionPriority: (priority: CreateSuggestionPriorityEnumApi) => ({ priority }),
        setActiveScoreRun: (run: AutoresearchRunApi | null) => ({ run }),
        startScorePolling: true,
        pollScoreRun: true,
        scoreRunFinished: (run: AutoresearchRunApi) => ({ run }),
        startTrainingPolling: true,
        pollTrainingRuns: true,
        setExperimentLogFilter: (filter: ExperimentLogFilter) => ({ filter }),
        toggleLogRun: (runId: string) => ({ runId }),
        searchPointClicked: (point: SearchPoint) => ({ point }),
        experimentDetailsOpened: (point: SearchPoint) => ({ point }),
        openExperiment: (point: SearchPoint, source: ExperimentOpenSource) => ({ point, source }),
        closeExperiment: true,
        setHoveredSearchPoint: (point: SearchPoint | null) => ({ point }),
        steerOpened: true,
        fullNotesOpened: true,
        saveSegmentCohort: (segment: PredictionSegmentKey) => ({ segment }),
        saveSegmentCohortFinished: true,
        setWritePersonProperty: (enabled: boolean, source: PersonPropertyToggleSource) => ({ enabled, source }),
        setWritePersonPropertyFinished: true,
        setSegmentThresholds: (thresholds: PredictionSegmentThresholdsApi) => ({ thresholds }),
    }),
    reducers({
        detailRequested: [
            false,
            {
                loadDetail: () => true,
            },
        ],
        selectedTab: [
            null as AutoresearchPipelineTab | null,
            {
                setActiveTab: (_, { tab }) => tab,
                setTabFromUrl: (_, { tab }) => tab,
            },
        ],
        predictionsPeopleView: [
            'most_likely' as PredictionsPeopleView,
            {
                setPredictionsPeopleView: (_, { view }) => view,
            },
        ],
        accuracyCutoff: [
            'top_10' as AccuracyCutoff,
            {
                setAccuracyCutoff: (_, { cutoff }) => cutoff,
            },
        ],
        accuracyTrendMetric: [
            'ranking' as AccuracyTrendMetric,
            {
                setAccuracyTrendMetric: (_, { metric }) => metric,
            },
        ],
        showChallengersInTrend: [
            false,
            {
                setShowChallengersInTrend: (_, { show }) => show,
            },
        ],
        hoveredAccuracyDate: [
            null as string | null,
            {
                setHoveredAccuracyDate: (_, { date }) => date,
            },
        ],
        activeScoreRun: [
            null as AutoresearchRunApi | null,
            {
                setActiveScoreRun: (_, { run }) => run,
                scoreNowSuccess: (_, { scoreResult }) => (scoreResult?.status === 'running' ? scoreResult : null),
                loadRunsSuccess: (current, { runs }) => current ?? findRunningScoreRun(runs),
                scoreRunFinished: () => null,
            },
        ],
        expandedRunId: [
            null as string | null,
            {
                toggleRunArtifacts: (current, { runId }) => (current === runId ? null : runId),
            },
        ],
        experimentLogFilter: [
            'all' as ExperimentLogFilter,
            {
                setExperimentLogFilter: (_, { filter }) => filter,
            },
        ],
        expandedLogRunIds: [
            [] as string[],
            {
                toggleLogRun: (current, { runId }) =>
                    current.includes(runId) ? current.filter((id) => id !== runId) : [...current, runId],
                searchPointClicked: (current, { point }) =>
                    current.includes(point.runId) ? current : [...current, point.runId],
            },
        ],
        openedExperiment: [
            null as SearchPoint | null,
            {
                openExperiment: (_, { point }) => point,
                closeExperiment: () => null,
            },
        ],
        hoveredSearchPoint: [
            null as SearchPoint | null,
            {
                setHoveredSearchPoint: (_, { point }) => point,
            },
        ],
        suggestionDraft: [
            '',
            {
                setSuggestionDraft: (_, { draft }) => draft,
                submitSuggestionSuccess: () => '',
            },
        ],
        suggestionPriority: [
            CreateSuggestionPriorityEnumApi.Consider as CreateSuggestionPriorityEnumApi,
            {
                setSuggestionPriority: (_, { priority }) => priority,
            },
        ],
        savingCohortSegment: [
            null as PredictionSegmentKey | null,
            {
                saveSegmentCohort: (_, { segment }) => segment,
                saveSegmentCohortFinished: () => null,
            },
        ],
        savingWritePersonProperty: [
            false,
            {
                setWritePersonProperty: () => true,
                setWritePersonPropertyFinished: () => false,
            },
        ],
        segmentThresholds: [
            null as PredictionSegmentThresholdsApi | null,
            {
                setSegmentThresholds: (_, { thresholds }) => thresholds,
            },
        ],
        predictionSegmentsError: [
            false,
            {
                loadPredictionSegments: () => false,
                loadPredictionSegmentsFailure: () => true,
                // The segments cannot be counted without the cut points.
                loadOnlinePerformanceFailure: () => true,
            },
        ],
        probabilityDistributionError: [
            false,
            {
                loadProbabilityDistribution: () => false,
                loadProbabilityDistributionFailure: () => true,
            },
        ],
        pipelineError: [
            false,
            {
                loadPipeline: () => false,
                loadPipelineSuccess: () => false,
                loadPipelineFailure: () => true,
            },
        ],
        modelsError: [
            false,
            {
                loadModels: () => false,
                loadModelsFailure: () => true,
            },
        ],
        trainingRunsError: [
            false,
            {
                loadTrainingRuns: () => false,
                loadTrainingRunsFailure: () => true,
            },
        ],
        modelsLoaded: [
            false,
            {
                loadModelsSuccess: () => true,
            },
        ],
        runsLoaded: [
            false,
            {
                loadRunsSuccess: () => true,
            },
        ],
        runsError: [
            false,
            {
                loadRuns: () => false,
                loadRunsFailure: () => true,
            },
        ],
        onlinePerformanceError: [
            false,
            {
                loadOnlinePerformance: () => false,
                loadOnlinePerformanceFailure: () => true,
            },
        ],
        suggestionsError: [
            false,
            {
                loadSuggestions: () => false,
                loadSuggestionsFailure: () => true,
            },
        ],
        dailyVolumeError: [
            false,
            {
                loadDailyVolume: () => false,
                loadDailyVolumeFailure: () => true,
            },
        ],
    }),
    loaders(({ actions, values, props }) => ({
        pipeline: [
            null as AutoresearchPipelineApi | null,
            {
                loadPipeline: async () => {
                    if (!values.currentTeamId) {
                        return null
                    }
                    return autoresearchRetrieve(String(values.currentTeamId), props.id)
                },
                pausePipeline: async () => {
                    if (!values.currentTeamId || !values.pipeline) {
                        return values.pipeline
                    }
                    // The endpoint ignores the body (it only flips status), but the generated
                    // client types a pipeline body — pass the current one to satisfy it.
                    return autoresearchPauseCreate(String(values.currentTeamId), props.id)
                },
                resumePipeline: async () => {
                    if (!values.currentTeamId || !values.pipeline) {
                        return values.pipeline
                    }
                    return autoresearchResumeCreate(String(values.currentTeamId), props.id)
                },
            },
        ],
        models: [
            [] as AutoresearchModelApi[],
            {
                loadModels: async () => {
                    if (!values.currentTeamId) {
                        return []
                    }
                    const teamId = String(values.currentTeamId)
                    return fetchAllPages((offset) => autoresearchModelsList(teamId, props.id, { offset }))
                },
            },
        ],
        trainingRuns: [
            [] as AutoresearchTrainingRunApi[],
            {
                loadTrainingRuns: async () => {
                    if (!values.currentTeamId) {
                        return []
                    }
                    const teamId = String(values.currentTeamId)
                    return fetchAllPages((offset) => autoresearchTrainingRunsList(teamId, props.id, { offset }))
                },
            },
        ],
        runs: [
            [] as AutoresearchRunApi[],
            {
                loadRuns: async () => {
                    if (!values.currentTeamId) {
                        return []
                    }
                    const teamId = String(values.currentTeamId)
                    return fetchAllPages((offset) => autoresearchRunsList(teamId, props.id, { offset }))
                },
            },
        ],
        onlinePerformance: [
            [] as OnlinePerformanceRowApi[],
            {
                loadOnlinePerformance: async () => {
                    if (!values.currentTeamId) {
                        return []
                    }
                    const { rows, segment_thresholds } = await autoresearchOnlinePerformanceRetrieve(
                        String(values.currentTeamId),
                        props.id
                    )
                    actions.setSegmentThresholds(segment_thresholds)
                    return rows
                },
            },
        ],
        suggestions: [
            [] as AutoresearchSuggestionApi[],
            {
                loadSuggestions: async () => {
                    if (!values.currentTeamId) {
                        return []
                    }
                    const teamId = String(values.currentTeamId)
                    return fetchAllPages((offset) => autoresearchSuggestionsList(teamId, props.id, { offset }))
                },
            },
        ],
        startTrainingResult: [
            null as AutoresearchTrainingRunApi | null,
            {
                startTraining: async () => {
                    if (!values.currentTeamId) {
                        return null
                    }
                    const result = await autoresearchTrainCreate(String(values.currentTeamId), props.id)
                    return result
                },
            },
        ],
        artifactsByRun: [
            {} as Record<string, string[]>,
            {
                loadRunArtifacts: async ({ runId }: { runId: string }) => {
                    if (!values.currentTeamId) {
                        return values.artifactsByRun
                    }
                    const response = await autoresearchTrainingRunsArtifactsRetrieve(
                        String(values.currentTeamId),
                        props.id,
                        runId
                    )
                    return { ...values.artifactsByRun, [runId]: response.paths }
                },
            },
        ],
        reportByRun: [
            {} as Record<string, string | null>,
            {
                // A run's report.md, decoded to text. null = loaded but the agent uploaded no report.
                loadRunReport: async ({ runId }: { runId: string }) => {
                    if (!values.currentTeamId) {
                        return values.reportByRun
                    }
                    try {
                        const response = await autoresearchTrainingRunsArtifactsGetCreate(
                            String(values.currentTeamId),
                            props.id,
                            runId,
                            { path: 'report.md' }
                        )
                        return { ...values.reportByRun, [runId]: base64ToUtf8(response.content_base64) }
                    } catch {
                        return { ...values.reportByRun, [runId]: null }
                    }
                },
            },
        ],
        viewedArtifact: [
            null as ViewedArtifact | null,
            {
                viewArtifact: async ({ runId, path }: { runId: string; path: string }) => {
                    if (!values.currentTeamId) {
                        return null
                    }
                    const response = await autoresearchTrainingRunsArtifactsGetCreate(
                        String(values.currentTeamId),
                        props.id,
                        runId,
                        { path }
                    )
                    return {
                        runId,
                        path: response.path,
                        sizeBytes: response.size_bytes,
                        text: isTextArtifact(response.path) ? base64ToUtf8(response.content_base64) : null,
                    }
                },
                closeArtifact: () => null,
            },
        ],
        scoreResult: [
            null as AutoresearchRunApi | null,
            {
                scoreNow: async () => {
                    if (!values.currentTeamId) {
                        return null
                    }
                    return autoresearchScoreCreate(String(values.currentTeamId), props.id)
                },
            },
        ],
        probabilityDistribution: [
            null as ProbabilityBucket[] | null,
            {
                loadProbabilityDistribution: async () => {
                    // Capture can shift the timestamps of one batch apart, so the latest batch is its prediction date.
                    const response = await api.queryHogQL(
                        hogql`
                            SELECT least(floor(p * 10), 9) AS bucket, count() AS users
                            FROM (
                                SELECT
                                    coalesce(nullIf(properties.$autoresearch_person_id, ''), distinct_id) AS person_id,
                                    argMax(toFloat(properties.$autoresearch_p_y), timestamp) AS p
                                FROM events
                                WHERE event = 'autoresearch_prediction'
                                  AND properties.$autoresearch_pipeline_id = ${props.id}
                                  AND properties.$autoresearch_model_role != 'shadow'
                                  AND timestamp >= now() - INTERVAL ${LATEST_BATCH_LOOKBACK_DAYS} DAY
                                  AND properties.$autoresearch_prediction_date = (
                                      SELECT max(properties.$autoresearch_prediction_date)
                                      FROM events
                                      WHERE event = 'autoresearch_prediction'
                                        AND properties.$autoresearch_pipeline_id = ${props.id}
                                        AND timestamp >= now() - INTERVAL ${LATEST_BATCH_LOOKBACK_DAYS} DAY
                                  )
                                GROUP BY person_id
                            )
                            GROUP BY bucket
                            ORDER BY bucket
                        `,
                        { productKey: 'autoresearch', name: 'autoresearch_probability_distribution' }
                    )
                    return (response.results ?? []).map((row: any[]) => ({
                        lower: Number(row[0]) / 10,
                        users: Number(row[1]),
                    }))
                },
            },
        ],
        predictionSegments: [
            null as PredictionSegmentCounts | null,
            {
                loadPredictionSegments: async () => {
                    const thresholds = values.segmentThresholds
                    if (!thresholds) {
                        return null
                    }
                    // The same per-person scores as the probability distribution, so the segments add up to its total.
                    const response = await api.queryHogQL(
                        hogql`
                            SELECT
                                multiIf(p >= ${thresholds.likely_threshold}, 'likely', p >= ${thresholds.possible_threshold}, 'possible', 'unlikely') AS segment,
                                count() AS people,
                                sum(p) AS expected_conversions
                            FROM (
                                SELECT
                                    coalesce(nullIf(properties.$autoresearch_person_id, ''), distinct_id) AS person_id,
                                    argMax(toFloat(properties.$autoresearch_p_y), timestamp) AS p
                                FROM events
                                WHERE event = 'autoresearch_prediction'
                                  AND properties.$autoresearch_pipeline_id = ${props.id}
                                  AND properties.$autoresearch_model_role != 'shadow'
                                  AND timestamp >= now() - INTERVAL ${LATEST_BATCH_LOOKBACK_DAYS} DAY
                                  AND properties.$autoresearch_prediction_date = (
                                      SELECT max(properties.$autoresearch_prediction_date)
                                      FROM events
                                      WHERE event = 'autoresearch_prediction'
                                        AND properties.$autoresearch_pipeline_id = ${props.id}
                                        AND timestamp >= now() - INTERVAL ${LATEST_BATCH_LOOKBACK_DAYS} DAY
                                  )
                                GROUP BY person_id
                            )
                            GROUP BY segment
                        `,
                        { productKey: 'autoresearch', name: 'autoresearch_prediction_segments' }
                    )
                    const rows = new Map<string, any[]>(
                        (response.results ?? []).map((row: any[]) => [String(row[0]), row])
                    )
                    return Object.fromEntries(
                        PREDICTION_SEGMENT_KEYS.map((key) => {
                            const row = rows.get(key)
                            return [key, { people: Number(row?.[1] ?? 0), expectedConversions: Number(row?.[2] ?? 0) }]
                        })
                    ) as PredictionSegmentCounts
                },
            },
        ],
        dailyVolume: [
            null as DailyVolumePoint[] | null,
            {
                loadDailyVolume: async () => {
                    // The last 60 days, returned oldest-first for left-to-right display. The time bound keeps the scan off the full history.
                    const response = await api.queryHogQL(
                        hogql`
                            SELECT toDate(timestamp) AS day,
                                   count() AS users_scored,
                                   round(100 * avg(toFloat(properties.$autoresearch_p_y)), 1) AS avg_probability
                            FROM events
                            WHERE event = 'autoresearch_prediction'
                              AND properties.$autoresearch_pipeline_id = ${props.id}
                              AND timestamp >= now() - INTERVAL 60 DAY
                            GROUP BY day
                            ORDER BY day DESC
                            LIMIT 60
                        `,
                        { productKey: 'autoresearch', name: 'autoresearch_daily_volume' }
                    )
                    return (response.results ?? [])
                        .map((row: any[]) => ({
                            day: String(row[0]),
                            users: Number(row[1]),
                            avgProbabilityPct: Number(row[2]),
                        }))
                        .reverse()
                },
            },
        ],
        suggestionSubmitResult: [
            null as AutoresearchSuggestionApi | null,
            {
                submitSuggestion: async () => {
                    if (!values.currentTeamId || !values.suggestionDraft.trim()) {
                        return null
                    }
                    return autoresearchSuggestionsCreate(String(values.currentTeamId), props.id, {
                        prompt: values.suggestionDraft.trim(),
                        priority: values.suggestionPriority,
                    })
                },
            },
        ],
    })),
    selectors({
        breadcrumbs: [
            (s) => [s.pipeline],
            (pipeline: AutoresearchPipelineApi | null): Breadcrumb[] => [
                {
                    key: Scene.Autoresearch,
                    name: 'Autoresearch',
                    path: urls.autoresearch(),
                },
                {
                    key: [Scene.AutoresearchPipeline, pipeline?.id ?? 'unknown'],
                    name: pipeline?.name ?? 'Model',
                },
            ],
        ],
        champion: [
            (s) => [s.models],
            (models: AutoresearchModelApi[]): AutoresearchModelApi | null =>
                models.find((m) => m.role === AutoresearchModelRoleEnumApi.Champion) ?? null,
        ],
        modelByTrainingRun: [
            (s) => [s.models],
            (models: AutoresearchModelApi[]): Record<string, AutoresearchModelApi> =>
                Object.fromEntries(
                    models.filter((m) => m.source_training_run).map((m) => [m.source_training_run as string, m])
                ),
        ],
        scoringCoverage: [
            (s) => [s.runs, s.pipeline],
            (runs: AutoresearchRunApi[], pipeline: AutoresearchPipelineApi | null): ScoringCoverage | null =>
                scoringCoverage(runs, pipeline?.cadence_days ?? 1),
        ],
        coverageSummary: [
            (s) => [s.runs, s.scoringCoverage, s.pipeline],
            (
                runs: AutoresearchRunApi[],
                scoringCoverage: ScoringCoverage | null,
                pipeline: AutoresearchPipelineApi | null
            ): CoverageSummary | null =>
                coverageSummary(runs, scoringCoverage?.rescoreDays ?? Math.max(pipeline?.cadence_days ?? 1, 1)),
        ],
        coverageHistory: [(s) => [s.runs], (runs: AutoresearchRunApi[]): CoveragePoint[] => coverageHistory(runs)],
        latestChampionPerformance: [
            (s) => [s.onlinePerformance],
            (onlinePerformance: OnlinePerformanceRowApi[]): OnlinePerformanceRowApi | null =>
                latestChampionRow(onlinePerformance),
        ],
        accuracyTrend: [
            (s) => [s.onlinePerformance, s.accuracyTrendMetric, s.accuracyCutoff, s.showChallengersInTrend],
            (
                onlinePerformance: OnlinePerformanceRowApi[],
                accuracyTrendMetric: AccuracyTrendMetric,
                accuracyCutoff: AccuracyCutoff,
                showChallengersInTrend: boolean
            ): AccuracyTrend =>
                accuracyTrend(onlinePerformance, accuracyTrendMetric, accuracyCutoff, showChallengersInTrend),
        ],
        hasChallengerPerformance: [
            (s) => [s.onlinePerformance],
            (onlinePerformance: OnlinePerformanceRowApi[]): boolean =>
                onlinePerformance.some((row) => row.emitted_role !== 'champion'),
        ],
        segmentDefinitions: [
            (s) => [s.segmentThresholds],
            (segmentThresholds: PredictionSegmentThresholdsApi | null): PredictionSegmentDefinition[] | null =>
                segmentThresholds ? predictionSegmentDefinitions(segmentThresholds) : null,
        ],
        segmentCalibration: [
            (s) => [s.latestChampionPerformance, s.segmentThresholds],
            (
                latestChampionPerformance: OnlinePerformanceRowApi | null,
                segmentThresholds: PredictionSegmentThresholdsApi | null
            ): SegmentCalibration[] =>
                segmentThresholds
                    ? calibrationBySegment(latestChampionPerformance?.calibration_bins ?? [], segmentThresholds)
                    : [],
        ],
        championConfusion: [
            (s) => [s.onlinePerformance, s.accuracyCutoff],
            (onlinePerformance: OnlinePerformanceRowApi[], accuracyCutoff: AccuracyCutoff): PooledConfusion | null =>
                pooledConfusion(onlinePerformance, accuracyCutoff),
        ],
        firstCheck: [
            (s) => [s.runs, s.pipeline],
            (runs: AutoresearchRunApi[], pipeline: AutoresearchPipelineApi | null): dayjs.Dayjs | null =>
                firstCheckDate(runs, pipeline?.horizon_days),
        ],
        probabilityHistogram: [
            (s) => [s.probabilityDistribution],
            (probabilityDistribution: ProbabilityBucket[] | null): ProbabilityBucket[] | null => {
                if (!probabilityDistribution) {
                    return null
                }
                const usersByDecile = new Map(probabilityDistribution.map((b) => [Math.round(b.lower * 10), b.users]))
                return Array.from({ length: 10 }, (_, decile) => ({
                    lower: decile / 10,
                    users: usersByDecile.get(decile) ?? 0,
                }))
            },
        ],
        hasLiveTrainingRun: [
            (s) => [s.trainingRuns],
            (trainingRuns: AutoresearchTrainingRunApi[]): boolean => trainingRuns.some(isTrainingRunLive),
        ],
        scoredChallenger: [
            (s) => [s.models],
            (models: AutoresearchModelApi[]): AutoresearchModelApi | null => scoredChallenger(models),
        ],
        runRoles: [
            (s) => [s.champion, s.scoredChallenger],
            (
                champion: AutoresearchModelApi | null,
                scoredChallenger: AutoresearchModelApi | null
            ): Record<string, RunRole> => runRoles(champion, scoredChallenger),
        ],
        queuedSuggestionCount: [
            (s) => [s.suggestions],
            (suggestions: AutoresearchSuggestionApi[]): number =>
                suggestions.filter((suggestion) => suggestion.status === 'queued').length,
        ],
        agentSearch: [
            (s) => [s.trainingRuns, s.champion, s.scoredChallenger],
            (
                trainingRuns: AutoresearchTrainingRunApi[],
                champion: AutoresearchModelApi | null,
                scoredChallenger: AutoresearchModelApi | null
            ): AgentSearch => buildAgentSearch(trainingRuns, champion, scoredChallenger),
        ],
        experimentLogGroups: [
            (s) => [s.trainingRuns, s.agentSearch, s.experimentLogFilter],
            (
                trainingRuns: AutoresearchTrainingRunApi[],
                agentSearch: AgentSearch,
                experimentLogFilter: ExperimentLogFilter
            ): ExperimentLogGroup[] => experimentLogGroups(trainingRuns, agentSearch, experimentLogFilter),
        ],
        statusBrief: [
            (s) => [s.trainingRuns, s.champion, s.scoredChallenger],
            (
                trainingRuns: AutoresearchTrainingRunApi[],
                champion: AutoresearchModelApi | null,
                scoredChallenger: AutoresearchModelApi | null
            ): StatusBrief | null => statusBrief(trainingRuns, champion, scoredChallenger),
        ],
        defaultTab: [
            (s) => [s.pipeline],
            (pipeline: AutoresearchPipelineApi | null): AutoresearchPipelineTab =>
                pipeline?.last_scored_at ? 'predictions' : 'agent_research',
        ],
        latestPropertyCleanupRun: [
            (s) => [s.runs],
            (runs: AutoresearchRunApi[]): AutoresearchRunApi | null =>
                runs.find((run) => run.run_type === 'property_cleanup') ?? null,
        ],
        personPropertyDisabledReason: [
            (s) => [s.pipeline, s.savingWritePersonProperty],
            (pipeline: AutoresearchPipelineApi | null, savingWritePersonProperty: boolean): string | undefined =>
                savingWritePersonProperty
                    ? 'Wait for the person profile setting to save'
                    : pipeline && !pipeline.write_person_property
                      ? 'Turn on "Write scores to person profiles" to target people by score'
                      : undefined,
        ],
        activeTab: [
            (s) => [s.selectedTab, s.defaultTab],
            (
                selectedTab: AutoresearchPipelineTab | null,
                defaultTab: AutoresearchPipelineTab
            ): AutoresearchPipelineTab => selectedTab ?? defaultTab,
        ],
        lifecycleSteps: [
            (s) => [s.pipeline, s.champion, s.runs, s.modelsLoaded, s.runsLoaded],
            (
                pipeline: AutoresearchPipelineApi | null,
                champion: AutoresearchModelApi | null,
                runs: AutoresearchRunApi[],
                modelsLoaded: boolean,
                runsLoaded: boolean
            ): LifecycleStep[] | null =>
                // Before models and runs load, an empty list means "not known yet", not "none".
                pipeline && modelsLoaded && runsLoaded
                    ? pipelineLifecycle({
                          pipeline,
                          champion,
                          runs,
                          validatedDates: validatedPredictionDates(runs),
                          now: dayjs(),
                      })
                    : null,
        ],
    }),
    listeners(({ actions, values, props, cache }) => ({
        loadDetail: () => {
            actions.loadPipeline()
            actions.loadModels()
            actions.loadTrainingRuns()
            actions.loadRuns()
            actions.loadOnlinePerformance()
            actions.loadSuggestions()
        },
        loadCurrentTeamSuccess: () => {
            if (values.featureFlags[FEATURE_FLAGS.AUTORESEARCH]) {
                actions.loadDetail()
            }
        },
        setFeatureFlags: () => {
            // Flags can arrive after mount, when afterMount already skipped the load. Start it once the flag is on.
            if (values.featureFlags[FEATURE_FLAGS.AUTORESEARCH] && !values.detailRequested) {
                posthog.capture('autoresearch model viewed', { pipeline_id: props.id })
                actions.loadDetail()
            }
        },
        loadPipelineFailure: () => {
            posthog.capture('autoresearch model load failed', { pipeline_id: props.id })
        },
        loadPipelineSuccess: ({ pipeline }) => {
            // The prediction-event queries are only worth running once the pipeline has ever scored.
            if (pipeline?.last_scored_at && !values.probabilityDistribution && !values.probabilityDistributionLoading) {
                actions.loadProbabilityDistribution()
            }
            if (
                pipeline?.last_scored_at &&
                values.segmentThresholds &&
                !values.predictionSegments &&
                !values.predictionSegmentsLoading
            ) {
                actions.loadPredictionSegments()
            }
            if (pipeline?.last_scored_at && !values.dailyVolume && !values.dailyVolumeLoading) {
                actions.loadDailyVolume()
            }
        },
        startTrainingSuccess: () => {
            posthog.capture('autoresearch model training started', { pipeline_id: props.id })
            actions.loadTrainingRuns()
            actions.loadPipeline()
            lemonToast.success('Training run started')
        },
        startTrainingFailure: ({ errorObject }) => {
            posthog.capture('autoresearch model action failed', {
                action: 'train',
                pipeline_id: props.id,
                status: errorObject?.status,
            })
            // A project at its daily training limit explains when the next run can start in `detail`.
            lemonToast.error(errorObject?.detail ?? errorObject?.data?.detail ?? 'Could not start training run')
        },
        pausePipelineSuccess: () => {
            posthog.capture('autoresearch model paused', { pipeline_id: props.id })
            lemonToast.success('Model paused. Scheduled scoring is on hold.')
        },
        pausePipelineFailure: () => {
            posthog.capture('autoresearch model action failed', { action: 'pause', pipeline_id: props.id })
            lemonToast.error('Could not pause the model')
        },
        resumePipelineSuccess: () => {
            posthog.capture('autoresearch model resumed', { pipeline_id: props.id })
            lemonToast.success('Model resumed')
        },
        resumePipelineFailure: ({ errorObject }) => {
            posthog.capture('autoresearch model action failed', { action: 'resume', pipeline_id: props.id })
            // A project at its active model limit explains it in `detail`.
            lemonToast.error(errorObject?.detail ?? errorObject?.data?.detail ?? 'Could not resume the model')
        },
        scoreNowSuccess: ({ scoreResult }) => {
            if (!scoreResult) {
                return
            }
            posthog.capture('autoresearch model score started', { pipeline_id: props.id, run_id: scoreResult.id })
            if (scoreResult.status === 'running') {
                lemonToast.info('Scoring started. Predictions update when it finishes.')
                actions.startScorePolling()
            } else {
                actions.scoreRunFinished(scoreResult)
            }
        },
        scoreNowFailure: ({ error }) => {
            posthog.capture('autoresearch model action failed', { action: 'score', pipeline_id: props.id })
            lemonToast.error(error ? `Could not start scoring. ${error}` : 'Could not start scoring. Try again.')
        },
        loadRunsSuccess: () => {
            // A run started before a reload, or by the daily schedule, is still followed to the end.
            if (values.activeScoreRun) {
                actions.startScorePolling()
            }
            // Follow the cleanup to the end, so the setting shows when it finished or failed.
            const cleanup = values.latestPropertyCleanupRun
            if (!cleanup || !isScoreRunInProgress(cleanup)) {
                cache.disposables.dispose('cleanupPoll')
            } else if (!cache.disposables.registry.has('cleanupPoll')) {
                cache.disposables.add(() => {
                    const timer = window.setInterval(() => actions.loadRuns(), PROPERTY_CLEANUP_POLL_INTERVAL_MS)
                    return () => clearInterval(timer)
                }, 'cleanupPoll')
            }
        },
        startScorePolling: () => {
            if (cache.disposables.registry.has('scorePoll')) {
                return
            }
            cache.disposables.add(() => {
                const timer = window.setInterval(() => actions.pollScoreRun(), SCORE_RUN_POLL_INTERVAL_MS)
                return () => clearInterval(timer)
            }, 'scorePoll')
        },
        pollScoreRun: async () => {
            const active = values.activeScoreRun
            if (!values.currentTeamId || !active) {
                cache.disposables.dispose('scorePoll')
                return
            }
            let run: AutoresearchRunApi
            try {
                run = await autoresearchRunsRetrieve(String(values.currentTeamId), props.id, active.id)
            } catch {
                // A failed check is retried on the next interval.
                return
            }
            if (isScoreRunInProgress(run)) {
                return
            }
            actions.scoreRunFinished(run)
        },
        scoreRunFinished: ({ run }) => {
            cache.disposables.dispose('scorePoll')
            actions.loadRuns()
            actions.loadPipeline()
            if (run.status === 'completed') {
                posthog.capture('autoresearch model scored', {
                    pipeline_id: props.id,
                    run_id: run.id,
                    rows_scored: run.rows_scored,
                })
                actions.loadProbabilityDistribution()
                actions.loadPredictionSegments()
                actions.loadDailyVolume()
                const scored = run.rows_scored ?? 0
                lemonToast.success(`Scored ${scored.toLocaleString()} users`)
            } else {
                posthog.capture('autoresearch model action failed', {
                    action: 'score',
                    stage: 'run',
                    pipeline_id: props.id,
                    run_id: run.id,
                })
                lemonToast.error(
                    run.error
                        ? `Scoring failed. ${run.error}`
                        : 'Scoring failed. Try again, and contact support if it keeps happening.'
                )
            }
        },
        loadTrainingRunsSuccess: ({ trainingRuns }) => {
            if (trainingRuns.some(isTrainingRunLive)) {
                actions.startTrainingPolling()
            } else if (cache.disposables.registry.has('trainingPoll')) {
                // The run finished, so the champion and the pipeline counters may have changed.
                cache.disposables.dispose('trainingPoll')
                actions.loadModels()
                actions.loadPipeline()
            }
        },
        startTrainingPolling: () => {
            if (cache.disposables.registry.has('trainingPoll')) {
                return
            }
            cache.disposables.add(() => {
                const timer = window.setInterval(() => actions.pollTrainingRuns(), TRAINING_RUN_POLL_INTERVAL_MS)
                return () => clearInterval(timer)
            }, 'trainingPoll')
        },
        pollTrainingRuns: () => {
            if (!values.trainingRunsLoading) {
                actions.loadTrainingRuns()
            }
        },
        setExperimentLogFilter: ({ filter }) => {
            // pinned: analytics event name and properties. Renaming breaks insights built on them.
            posthog.capture('autoresearch model experiment log filter changed', { pipeline_id: props.id, filter })
        },
        searchPointClicked: ({ point }) => {
            // pinned: analytics event name and properties. Renaming breaks insights built on them.
            posthog.capture('autoresearch model search point clicked', {
                pipeline_id: props.id,
                run_id: point.runId,
                iteration_number: point.iterationNumber,
                status: point.status,
            })
            actions.openExperiment(point, 'chart')
        },
        openExperiment: ({ point, source }) => {
            // pinned: analytics event name and properties. Renaming breaks insights built on them.
            posthog.capture('autoresearch agent research experiment opened', {
                pipeline_id: props.id,
                run_id: point.runId,
                iteration_number: point.iterationNumber,
                status: point.status,
                source,
            })
            actions.experimentDetailsOpened(point)
            if (!values.artifactsByRun[point.runId]) {
                actions.loadRunArtifacts({ runId: point.runId })
            }
        },
        steerOpened: () => {
            // pinned: analytics event name and properties. Renaming breaks insights built on them.
            posthog.capture('autoresearch agent research steer opened', {
                pipeline_id: props.id,
                queued_count: values.queuedSuggestionCount,
            })
        },
        fullNotesOpened: () => {
            // pinned: analytics event name and properties. Renaming breaks insights built on them.
            posthog.capture('autoresearch agent research full notes opened', {
                pipeline_id: props.id,
                run_number: values.statusBrief?.runNumber ?? null,
            })
        },
        experimentDetailsOpened: ({ point }) => {
            // pinned: analytics event name and properties. Renaming breaks insights built on them.
            posthog.capture('autoresearch model experiment details opened', {
                pipeline_id: props.id,
                run_id: point.runId,
                iteration_number: point.iterationNumber,
            })
        },
        submitSuggestionSuccess: ({ suggestionSubmitResult }) => {
            posthog.capture('autoresearch model suggestion sent', {
                pipeline_id: props.id,
                priority: suggestionSubmitResult?.priority ?? values.suggestionPriority,
            })
            actions.loadSuggestions()
            lemonToast.success('Suggestion sent. The agent will pick it up on its next run.')
        },
        submitSuggestionFailure: () => {
            posthog.capture('autoresearch model action failed', { action: 'suggest', pipeline_id: props.id })
            lemonToast.error('Could not submit the suggestion')
        },
        toggleRunArtifacts: ({ runId }) => {
            // Lazy-load a run's bundle and report the first time it's expanded.
            if (values.expandedRunId === runId) {
                if (!values.artifactsByRun[runId]) {
                    actions.loadRunArtifacts({ runId })
                }
                if (values.reportByRun[runId] === undefined) {
                    actions.loadRunReport({ runId })
                }
            }
        },
        setActiveTab: ({ tab }) => {
            // pinned: analytics event name and properties. Renaming breaks insights built on them.
            posthog.capture('autoresearch model tab changed', { pipeline_id: props.id, tab })
        },
        reportNotebookOpened: ({ runId }) => {
            posthog.capture('autoresearch model report notebook opened', { pipeline_id: props.id, run_id: runId })
        },
        reportPredictionLinkClicked: ({ destination }) => {
            posthog.capture('autoresearch model prediction link clicked', { pipeline_id: props.id, destination })
        },
        setPredictionsPeopleView: ({ view }) => {
            posthog.capture('autoresearch model predictions view changed', { pipeline_id: props.id, view })
        },
        setSegmentThresholds: () => {
            if (values.pipeline?.last_scored_at) {
                actions.loadPredictionSegments()
            }
        },
        setAccuracyCutoff: ({ cutoff, source }) => {
            // pinned: analytics event name and properties. Renaming breaks insights built on them.
            posthog.capture('autoresearch model accuracy cutoff changed', { pipeline_id: props.id, cutoff, source })
        },
        setAccuracyTrendMetric: ({ metric }) => {
            // pinned: analytics event name and properties. Renaming breaks insights built on them.
            posthog.capture('autoresearch model accuracy trend metric changed', { pipeline_id: props.id, metric })
        },
        setShowChallengersInTrend: ({ show }) => {
            // pinned: analytics event name and properties. Renaming breaks insights built on them.
            posthog.capture('autoresearch model accuracy trend challengers toggled', {
                pipeline_id: props.id,
                shown: show,
            })
        },
        setWritePersonProperty: async ({ enabled, source }) => {
            if (!values.currentTeamId) {
                actions.setWritePersonPropertyFinished()
                return
            }
            const teamId = String(values.currentTeamId)
            try {
                actions.loadPipelineSuccess(
                    await autoresearchPartialUpdate(teamId, props.id, { write_person_property: enabled })
                )
                posthog.capture('autoresearch model person property toggled', {
                    pipeline_id: props.id,
                    enabled,
                    source,
                })
                if (enabled) {
                    lemonToast.success(
                        'Scores are now written to person profiles. People get the property over the next few scoring runs.'
                    )
                } else {
                    // The cleanup records a run, so reload the runs to show it.
                    actions.loadRuns()
                    lemonToast.success(
                        'Scores are no longer written to person profiles. The property is being removed.'
                    )
                }
            } catch {
                posthog.capture('autoresearch model action failed', {
                    action: 'person_property',
                    pipeline_id: props.id,
                    enabled,
                    source,
                })
                lemonToast.error("Couldn't change the setting. Try again.")
            } finally {
                actions.setWritePersonPropertyFinished()
            }
        },
        saveSegmentCohort: async ({ segment }) => {
            const { pipeline, currentProjectId, segmentThresholds } = values
            const definition = values.segmentDefinitions?.find(({ key }) => key === segment)
            const outputProperty = pipeline?.output_person_property
            if (!pipeline || !outputProperty || !currentProjectId || !definition || !segmentThresholds) {
                actions.saveSegmentCohortFinished()
                return
            }
            try {
                const cohort = await cohortsCreate(String(currentProjectId), {
                    name: `${pipeline.name}: ${definition.label.toLowerCase()} (${definition.probabilityRange.toLowerCase()})`,
                    description: `People whose ${outputProperty} score is ${definition.probabilityRange.toLowerCase()}. The members update each time the model scores, but the cut point stays at the value it had when the cohort was saved.`,
                    is_static: false,
                    filters: predictionSegmentCohortFilters(segment, outputProperty, segmentThresholds),
                })
                posthog.capture('autoresearch model cohort saved', {
                    pipeline_id: props.id,
                    segment,
                    cohort_id: cohort.id,
                    people: values.predictionSegments?.[segment].people,
                    cut_points:
                        segmentThresholds.base_rate_source === 'realized'
                            ? 'base_rate'
                            : segmentThresholds.base_rate_source,
                })
                lemonToast.success('Cohort saved', {
                    button: { label: 'View cohort', action: () => router.actions.push(urls.cohort(cohort.id)) },
                })
            } catch {
                posthog.capture('autoresearch model action failed', {
                    action: 'save_cohort',
                    pipeline_id: props.id,
                    segment,
                })
                lemonToast.error("Couldn't save the cohort. Try again.")
            } finally {
                actions.saveSegmentCohortFinished()
            }
        },
    })),
    actionToUrl(() => ({
        // Reflect the active tab in the URL (?tab=…) so each tab is deep-linkable.
        setActiveTab: ({ tab }) => {
            if (router.values.searchParams.tab === tab) {
                return // no-op when the URL already matches (avoids a redundant history entry)
            }
            return [router.values.location.pathname, { ...router.values.searchParams, tab }, router.values.hashParams]
        },
    })),
    urlToAction(({ actions, values }) => ({
        '/autoresearch/:id': (_, searchParams) => {
            const tab = pipelineTabFromUrl(searchParams.tab)
            if (searchParams.tab !== undefined && searchParams.tab !== tab) {
                // A legacy or unknown key: rewrite the URL in place so it shows the tab that opens.
                const { tab: _legacyTab, ...rest } = searchParams
                router.actions.replace(
                    router.values.location.pathname,
                    tab ? { ...rest, tab } : rest,
                    router.values.hashParams
                )
                return
            }
            if (tab !== values.selectedTab) {
                actions.setTabFromUrl(tab)
            }
        },
    })),
    afterMount(({ actions, values, props }) => {
        // With the flag off the scene shows NotFound, so there is nothing to record or load.
        if (values.featureFlags[FEATURE_FLAGS.AUTORESEARCH]) {
            posthog.capture('autoresearch model viewed', { pipeline_id: props.id })
            actions.loadDetail()
        }
    }),
])