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.

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

import { dayjs } from 'lib/dayjs'
import { lemonToast } from 'lib/lemon-ui/LemonToast'
import { teamLogic } from 'scenes/teamLogic'

import { AutoresearchPipelineLogicProps, autoresearchPipelineLogic } from './autoresearchPipelineLogic'
import { autoresearchAgentModelsRetrieve, autoresearchLimitsRetrieve, autoresearchPartialUpdate } from './generated/api'
import type {
    AgentModelOptionsApi,
    AgentReasoningEffortOptionApi,
    AutoresearchPipelineApi,
    AutoresearchTrainingRunApi,
    ProjectLimitsApi,
    ResearchIntervalDaysEnumApi,
} from './generated/api.schemas'

export const MIN_ITERATIONS_PER_RUN = 1
export const MAX_ITERATIONS_PER_RUN = 50

export interface TrainingScheduleDraft {
    research_interval_days: ResearchIntervalDaysEnumApi | null
    iterations_per_run: number | undefined
    // Null means the autoresearch default.
    agent_model: string | null
    agent_reasoning_effort: string | null
}

/** Why no training run can start now because of the project's daily limit, or undefined while one can. */
function dailyTrainingLimitReason(limits: ProjectLimitsApi | null): string | undefined {
    if (!limits) {
        return undefined
    }
    if (limits.max_training_runs_per_day === 0) {
        return 'Training is turned off for this project. Contact support to turn it on.'
    }
    if (!limits.next_slot_at) {
        return undefined
    }
    return `This project is at its daily limit of ${limits.max_training_runs_per_day} training runs. The next run can start at ${dayjs(limits.next_slot_at).format('MMM D, h:mm A')}.`
}

/** Why a draft cannot start its first training run because of the project's active model limit. */
function liveModelLimitReason(
    pipeline: AutoresearchPipelineApi | null,
    limits: ProjectLimitsApi | null
): string | undefined {
    if (!pipeline || !limits || pipeline.status !== 'draft' || limits.live_pipelines < limits.max_live_pipelines) {
        return undefined
    }
    return `This project has reached its limit of ${limits.max_live_pipelines} active models. Pause or delete one to train this model.`
}

/** Why the project's limits stop this model from training now, or undefined when they do not. */
export function trainingDisabledByLimitReason(
    pipeline: AutoresearchPipelineApi | null,
    limits: ProjectLimitsApi | null
): string | undefined {
    return liveModelLimitReason(pipeline, limits) ?? dailyTrainingLimitReason(limits)
}

/** One line on what training does next, from the saved schedule and the project's daily limit. */
export function trainingScheduleStatus(
    pipeline: AutoresearchPipelineApi,
    limits: ProjectLimitsApi | null = null,
    now: dayjs.Dayjs = dayjs()
): string {
    const limitReason = dailyTrainingLimitReason(limits)
    if (limitReason) {
        return limitReason
    }
    if (pipeline.research_interval_days === null || !pipeline.next_research_at) {
        return 'Training starts only when you ask for it.'
    }
    if (pipeline.training_phase === 'explore') {
        return 'Exploring: training on each scoring day until a run stops improving the model.'
    }
    const next = dayjs(pipeline.next_research_at)
    if (!next.isAfter(now)) {
        return 'Next research run on the next scoring day.'
    }
    return `Next research run on ${next.format('MMM D')}.`
}

/** Why the model has no live version after its best model failed the final check, or null. */
export function notLiveExplanation(pipeline: AutoresearchPipelineApi): string | null {
    if (!pipeline.not_promoted_reason || pipeline.live_training_run) {
        return null
    }
    const next =
        pipeline.research_interval_days === null
            ? 'Start a training run to try again.'
            : 'The next research run tries again.'
    return `The best model from the last training run couldn't go live, so nothing is scored yet. ${next}`
}

export interface trainingScheduleLogicValues {
    currentTeamId: number | null
    pipeline: AutoresearchPipelineApi | null
    draft: Partial<TrainingScheduleDraft>
    savingSchedule: boolean
    schedule: TrainingScheduleDraft | null
    saveDisabledReason: string | undefined
    agentModelOptions: AgentModelOptionsApi | null
    agentModelOptionsLoading: boolean
    effortOptions: AgentReasoningEffortOptionApi[]
    projectLimits: ProjectLimitsApi | null
    projectLimitsLoading: boolean
    trainDisabledByLimitReason: string | undefined
}

export interface trainingScheduleLogicActions {
    loadPipelineSuccess: (pipeline: AutoresearchPipelineApi | null) => {
        pipeline: AutoresearchPipelineApi | null
        payload?: any
    }
    setResearchIntervalDays: (days: ResearchIntervalDaysEnumApi | null) => {
        days: ResearchIntervalDaysEnumApi | null
    }
    setIterationsPerRun: (iterations: number | undefined) => { iterations: number | undefined }
    setAgentModel: (model: string | null) => { model: string | null }
    setAgentReasoningEffort: (effort: string | null) => { effort: string | null }
    loadAgentModelOptions: () => { value: true }
    loadAgentModelOptionsSuccess: (agentModelOptions: AgentModelOptionsApi | null) => {
        agentModelOptions: AgentModelOptionsApi | null
        payload?: any
    }
    loadAgentModelOptionsFailure: (error: string) => { error: string }
    loadProjectLimits: () => { value: true }
    loadProjectLimitsSuccess: (projectLimits: ProjectLimitsApi | null) => {
        projectLimits: ProjectLimitsApi | null
        payload?: any
    }
    loadProjectLimitsFailure: (error: string) => { error: string }
    startTrainingSuccess: (
        startTrainingResult: AutoresearchTrainingRunApi | null,
        payload?: any
    ) => { startTrainingResult: AutoresearchTrainingRunApi | null; payload?: any }
    startTrainingFailure: (error: string, errorObject?: any) => { error: string; errorObject?: any }
    resumePipelineSuccess: (
        pipeline: AutoresearchPipelineApi | null,
        payload?: any
    ) => { pipeline: AutoresearchPipelineApi | null; payload?: any }
    resetDraft: () => { value: true }
    saveSchedule: () => { value: true }
    saveScheduleFinished: () => { value: true }
}

export type trainingScheduleLogicType = MakeLogicType<
    trainingScheduleLogicValues,
    trainingScheduleLogicActions,
    AutoresearchPipelineLogicProps
>

export const trainingScheduleLogic = kea<trainingScheduleLogicType>([
    props({} as AutoresearchPipelineLogicProps),
    key((props) => props.id),
    path((key) => ['products', 'autoresearch', 'frontend', 'trainingScheduleLogic', key]),
    connect((props: AutoresearchPipelineLogicProps) => ({
        values: [teamLogic, ['currentTeamId'], autoresearchPipelineLogic(props), ['pipeline']],
        actions: [
            autoresearchPipelineLogic(props),
            ['loadPipelineSuccess', 'startTrainingSuccess', 'startTrainingFailure', 'resumePipelineSuccess'],
        ],
    })),
    actions({
        setResearchIntervalDays: (days: ResearchIntervalDaysEnumApi | null) => ({ days }),
        setIterationsPerRun: (iterations: number | undefined) => ({ iterations }),
        setAgentModel: (model: string | null) => ({ model }),
        setAgentReasoningEffort: (effort: string | null) => ({ effort }),
        resetDraft: true,
        saveSchedule: true,
        saveScheduleFinished: true,
    }),
    reducers({
        // The unsaved edits. A field that is not here shows its saved value.
        draft: [
            {} as Partial<TrainingScheduleDraft>,
            {
                setResearchIntervalDays: (state, { days }) => ({ ...state, research_interval_days: days }),
                setIterationsPerRun: (state, { iterations }) => ({ ...state, iterations_per_run: iterations }),
                // Efforts differ per model, so a new model starts from its default effort.
                setAgentModel: (state, { model }) => ({ ...state, agent_model: model, agent_reasoning_effort: null }),
                setAgentReasoningEffort: (state, { effort }) => ({ ...state, agent_reasoning_effort: effort }),
                resetDraft: () => ({}),
            },
        ],
        savingSchedule: [
            false,
            {
                saveSchedule: () => true,
                saveScheduleFinished: () => false,
            },
        ],
    }),
    loaders(({ values }) => ({
        agentModelOptions: [
            null as AgentModelOptionsApi | null,
            {
                loadAgentModelOptions: async () => {
                    if (!values.currentTeamId) {
                        return null
                    }
                    return autoresearchAgentModelsRetrieve(String(values.currentTeamId))
                },
            },
        ],
        projectLimits: [
            null as ProjectLimitsApi | null,
            {
                loadProjectLimits: async () => {
                    if (!values.currentTeamId) {
                        return null
                    }
                    return autoresearchLimitsRetrieve(String(values.currentTeamId))
                },
            },
        ],
    })),
    selectors({
        schedule: [
            (s) => [s.pipeline, s.draft],
            (pipeline: AutoresearchPipelineApi | null, draft: Partial<TrainingScheduleDraft>) =>
                pipeline ? { ...pickSchedule(pipeline), ...draft } : null,
        ],
        saveDisabledReason: [
            (s) => [s.pipeline, s.schedule, s.savingSchedule],
            (
                pipeline: AutoresearchPipelineApi | null,
                schedule: TrainingScheduleDraft | null,
                savingSchedule: boolean
            ): string | undefined => {
                if (savingSchedule) {
                    return 'The schedule is saving'
                }
                if (!pipeline || !schedule) {
                    return 'The model is loading'
                }
                const iterations = schedule.iterations_per_run
                if (
                    iterations === undefined ||
                    !Number.isInteger(iterations) ||
                    iterations < MIN_ITERATIONS_PER_RUN ||
                    iterations > MAX_ITERATIONS_PER_RUN
                ) {
                    return `Enter ${MIN_ITERATIONS_PER_RUN} to ${MAX_ITERATIONS_PER_RUN} experiments per run`
                }
                const saved = pickSchedule(pipeline)
                if (
                    saved.iterations_per_run === iterations &&
                    saved.research_interval_days === schedule.research_interval_days &&
                    saved.agent_model === schedule.agent_model &&
                    saved.agent_reasoning_effort === schedule.agent_reasoning_effort
                ) {
                    return 'Change a setting to save'
                }
                return undefined
            },
        ],
        trainDisabledByLimitReason: [
            (s) => [s.pipeline, s.projectLimits],
            (pipeline: AutoresearchPipelineApi | null, projectLimits: ProjectLimitsApi | null): string | undefined =>
                trainingDisabledByLimitReason(pipeline, projectLimits),
        ],
        effortOptions: [
            (s) => [s.agentModelOptions, s.schedule],
            (
                agentModelOptions: AgentModelOptionsApi | null,
                schedule: TrainingScheduleDraft | null
            ): AgentReasoningEffortOptionApi[] => {
                if (!agentModelOptions) {
                    return []
                }
                const model = schedule?.agent_model ?? agentModelOptions.default_model
                return agentModelOptions.models.find((option) => option.model === model)?.supported_efforts ?? []
            },
        ],
    }),
    listeners(({ actions, values, props }) => ({
        // Each changes the project's usage, and a refused start means the shown limits were stale.
        startTrainingSuccess: () => actions.loadProjectLimits(),
        startTrainingFailure: () => actions.loadProjectLimits(),
        resumePipelineSuccess: () => actions.loadProjectLimits(),
        saveSchedule: async () => {
            const { schedule, currentTeamId } = values
            if (!schedule || schedule.iterations_per_run === undefined || !currentTeamId) {
                actions.saveScheduleFinished()
                return
            }
            const scheduleChanges = {
                research_interval_days: schedule.research_interval_days,
                iterations_per_run: schedule.iterations_per_run,
            }
            const modelChanges = {
                agent_model: schedule.agent_model,
                agent_reasoning_effort: schedule.agent_reasoning_effort,
            }
            const saved = values.pipeline ? pickSchedule(values.pipeline) : null
            const scheduleChanged =
                saved?.research_interval_days !== scheduleChanges.research_interval_days ||
                saved?.iterations_per_run !== scheduleChanges.iterations_per_run
            const modelChanged =
                saved?.agent_model !== modelChanges.agent_model ||
                saved?.agent_reasoning_effort !== modelChanges.agent_reasoning_effort
            try {
                actions.loadPipelineSuccess(
                    await autoresearchPartialUpdate(String(currentTeamId), props.id, {
                        ...scheduleChanges,
                        ...modelChanges,
                    })
                )
                actions.resetDraft()
                // pinned: analytics event names. Renaming breaks insights built on them.
                if (scheduleChanged) {
                    posthog.capture('autoresearch training schedule changed', {
                        pipeline_id: props.id,
                        ...scheduleChanges,
                    })
                }
                if (modelChanged) {
                    posthog.capture('autoresearch research model changed', { pipeline_id: props.id, ...modelChanges })
                }
                lemonToast.success('Training settings saved.')
            } catch {
                posthog.capture('autoresearch model action failed', {
                    action: 'training_schedule',
                    pipeline_id: props.id,
                })
                lemonToast.error("Couldn't save the training settings. Try again.")
            } finally {
                actions.saveScheduleFinished()
            }
        },
    })),
    afterMount(({ actions }) => {
        actions.loadAgentModelOptions()
        actions.loadProjectLimits()
    }),
])

function pickSchedule(pipeline: AutoresearchPipelineApi): TrainingScheduleDraft {
    return {
        research_interval_days: pipeline.research_interval_days,
        iterations_per_run: pipeline.iterations_per_run,
        agent_model: pipeline.agent_model,
        agent_reasoning_effort: pipeline.agent_reasoning_effort,
    }
}