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.

11.4 KB
Raw Blame
import { ClickHouseClient } from '@clickhouse/client'
import { DateTime } from 'luxon'

import { HogFlowManagerService } from '../services/hogflows/hogflow-manager.service'
import { CyclotronJobQueuePostgresV2 } from '../services/job-queue/job-queue-postgres-v2'
import { JobQueue } from '../services/job-queue/job-queue.interface'
import { HogFunctionManagerService } from '../services/managers/hog-function-manager.service'
import { HogFunctionMonitoringService } from '../services/monitoring/hog-function-monitoring.service'
import { HogInvocationResultsService } from '../services/monitoring/hog-invocation-results.service'
import { CyclotronJobInvocationHogFlow } from '../types'
import { RerunJobState } from './rerun-job.types'
import { RerunPaginatorService } from './rerun-paginator.service'

// A poisoned wait_until_condition the janitor gave up on is recorded with the
// FINAL flow state in `invocation_globals` — including `currentAction`, which is
// where the executor resumes from. If rerun rehydration dropped it, the flow
// would restart from the trigger and re-send emails that already went out. These
// tests pin the fix: the rehydrated invocation carries `currentAction` forward,
// so replay resumes after the already-completed actions — but WITHOUT its parked
// `hogFunctionState`, whose `globals.inputs` was stripped on persist; restoring
// it verbatim skips the input re-render and message actions then fail with
// "No recipient identifier found".
describe('RerunPaginatorService replay fidelity (hog_flow)', () => {
    const teamId = 42
    const functionId = 'flow-1'

    function fakeClickhouse(rows: Array<Record<string, unknown>>): ClickHouseClient {
        return {
            // The paginator runs two queries per page: the windowed page fetch and a
            // cross-partition stale-status check on the page's ids. The stale check
            // returns the ids that should be SKIPPED, so answer it with an empty set
            // and these tests keep exercising rehydration rather than the stale skip.
            query: jest.fn().mockImplementation(({ query }: { query: string }) => {
                const payload = query.includes('hog_invocation_rerun_stale_status') ? [] : rows
                return Promise.resolve({ json: () => Promise.resolve(payload) })
            }),
        } as unknown as ClickHouseClient
    }

    function buildPaginator(
        clickhouse: ClickHouseClient,
        hogFlowQueue: jest.Mocked<CyclotronJobQueuePostgresV2>,
        monitoring = {
            queueLogs: jest.fn(),
            flush: jest.fn().mockResolvedValue(undefined),
        } as unknown as jest.Mocked<HogFunctionMonitoringService>
    ): RerunPaginatorService {
        const hogFlowManager = {
            getHogFlow: jest.fn().mockResolvedValue({ id: functionId, team_id: teamId, variables: [] }),
        } as unknown as jest.Mocked<HogFlowManagerService>
        const hogFunctionManager = {
            getHogFunction: jest.fn().mockResolvedValue(null),
        } as unknown as jest.Mocked<HogFunctionManagerService>
        const lifecycle = {
            queueLifecycleRow: jest.fn(),
            queueRerunWrapperRow: jest.fn(),
            flush: jest.fn().mockResolvedValue(undefined),
            dropQueuedRowsFor: jest.fn(),
        } as unknown as jest.Mocked<HogInvocationResultsService>
        return new RerunPaginatorService(
            clickhouse,
            hogFunctionManager,
            hogFlowManager,
            lifecycle,
            { hog_function: {} as unknown as JobQueue, hog_flow: hogFlowQueue },
            monitoring,
            10000
        )
    }

    const state: RerunJobState = {
        function_kind: 'hog_flow',
        function_id: functionId,
        request: { filter: { window_start: '2026-01-01T00:00:00Z', window_end: '2027-01-01T00:00:00Z' } },
        progress: { queued: 0, skipped: 0, done: false },
    }

    it.each([undefined, 1] as const)('restores progress and customer task key version %j', async (version) => {
        const persistedState = {
            event: { uuid: 'evt-1', distinct_id: 'd-1', properties: {}, timestamp: '2026-06-01T09:00:00Z' },
            actionStepCount: 3,
            customerTaskIdempotencyVersion: version,
            variables: { ticket_id: '123' },
            // The flow had already advanced to (and parked at) this action. The parked
            // hogFunctionState is stored as `stripInputs` wrote it: `globals.inputs` gone.
            currentAction: {
                id: 'send_email',
                startedAtTimestamp: 999,
                awaitingResume: { key: 'old-key', deadlineAt: '2020-01-01T00:00:00Z', dispatch: { id: 'old-task' } },
                resumeResult: { key: 'old-key', status: 'failed' },
                cohortLookupAttempts: 2,
                cohortLookupFailed: true,
                hogFunctionState: {
                    globals: { event: { uuid: 'evt-1' }, source: { name: 'Email', url: '' } },
                    timings: [],
                    attempts: 0,
                },
            },
            personId: 'person-1',
            messageOptOuts: [{ identifier: '[email protected]', categoryId: '$all' }],
            send: {
                content: { subject: 'Welcome', html: '<p>Hi</p>', text: 'Hi' },
                from: { integrationId: 12, email: '[email protected]', name: 'Example' },
                replyTo: ['[email protected]'],
                tracking: { captureEngagement: false },
            },
        }
        const rows = [
            {
                invocation_id: 'inv-1',
                parent_run_id: 'run-1',
                attempts: 0,
                last_scheduled_at: '2026-06-01 09:00:00.000000',
                run_first_scheduled_at: '2026-06-01 09:00:00.000000',
                // decodeInvocationGlobals treats a leading '{' as raw JSON.
                invocation_globals: JSON.stringify(persistedState),
            },
        ]

        const hogFlowQueue = {
            queueInvocations: jest.fn().mockResolvedValue(undefined),
        } as unknown as jest.Mocked<CyclotronJobQueuePostgresV2>

        const paginator = buildPaginator(fakeClickhouse(rows), hogFlowQueue)
        await paginator.processPage(
            teamId,
            { ...state, request: { ...state.request, replay_code_sends: true } },
            { jobId: 'rerun-1', createdAt: DateTime.now() }
        )

        expect(hogFlowQueue.queueInvocations).toHaveBeenCalledTimes(1)
        const [enqueued, opts] = hogFlowQueue.queueInvocations.mock.calls[0]
        expect(opts).toEqual({ overwriteExisting: true })
        const invocation = enqueued[0] as CyclotronJobInvocationHogFlow
        expect(invocation.id).toBe('inv-1')
        // The resume point and per-actor context survive the round-trip, but the
        // input-stripped hogFunctionState does not — the action re-enters fresh so
        // inputs re-render against the current config instead of the stale snapshot.
        expect(invocation.state?.currentAction).toEqual({ id: 'send_email', startedAtTimestamp: 999 })
        expect(invocation.state?.personId).toBe('person-1')
        expect(invocation.state?.messageOptOuts).toEqual([{ identifier: '[email protected]', categoryId: '$all' }])
        expect(invocation.state?.actionStepCount).toBe(3)
        expect(invocation.state?.customerTaskIdempotencyVersion).toBe(version)
        expect(invocation.state?.variables).toEqual({ ticket_id: '123' })
        expect(invocation.state?.send).toEqual(persistedState.send)
        // Sticky rerun counter still increments so max_attempts guards apply.
        expect(invocation.state?.rerunAttempts).toBe(1)
    })

    const codeSend = {
        content: { subject: 'Welcome', html: '<p>Hi</p>', text: 'Hi' },
        from: { integrationId: 12, email: '[email protected]', name: 'Example' },
        tracking: { captureEngagement: false },
    }

    it.each([
        ['an email sent from code while replay_code_sends is undefined', { send: codeSend }, undefined, []],
        ['an email sent from code while replay_code_sends is false', { send: codeSend }, false, []],
        [
            'a sensitive message whose content was not stored',
            { sensitive_variables_omitted: true },
            true,
            [
                expect.objectContaining({
                    instance_id: 'inv-3',
                    level: 'warn',
                    message:
                        "Not re-run: this run's message is sensitive, so its content wasn't stored and can't be sent again.",
                }),
            ],
        ],
    ] as const)('skips a run with %s', async (_name, stored, allowed, expectedLogs) => {
        const persistedState = {
            event: { uuid: 'evt-3', distinct_id: 'd-3', properties: {}, timestamp: '2026-06-01T09:00:00Z' },
            actionStepCount: 1,
            variables: {},
            ...stored,
        }
        const rows = [
            {
                invocation_id: 'inv-3',
                parent_run_id: '',
                attempts: 0,
                last_scheduled_at: '2026-06-01 09:00:00.000000',
                run_first_scheduled_at: '2026-06-01 09:00:00.000000',
                invocation_globals: JSON.stringify(persistedState),
            },
        ]

        const hogFlowQueue = {
            queueInvocations: jest.fn().mockResolvedValue(undefined),
        } as unknown as jest.Mocked<CyclotronJobQueuePostgresV2>

        const monitoring = {
            queueLogs: jest.fn(),
            flush: jest.fn().mockResolvedValue(undefined),
        } as unknown as jest.Mocked<HogFunctionMonitoringService>
        const paginator = buildPaginator(fakeClickhouse(rows), hogFlowQueue, monitoring)
        await paginator.processPage(
            teamId,
            { ...state, request: { ...state.request, replay_code_sends: allowed } },
            { jobId: 'rerun-3', createdAt: DateTime.now() }
        )

        expect(hogFlowQueue.queueInvocations.mock.calls.flatMap(([invocations]) => invocations)).toEqual([])
        const runLogs = monitoring.queueLogs.mock.calls
            .flatMap(([entries]) => entries)
            .filter((entry) => entry.instance_id === 'inv-3')
        expect(runLogs).toEqual(expectedLogs)
    })

    it('leaves currentAction undefined for a flow that never progressed (fresh start is correct there)', async () => {
        const persistedState = {
            event: { uuid: 'evt-2', distinct_id: 'd-2', properties: {}, timestamp: '2026-06-01T09:00:00Z' },
            actionStepCount: 0,
            variables: {},
        }
        const rows = [
            {
                invocation_id: 'inv-2',
                parent_run_id: '',
                attempts: 0,
                last_scheduled_at: '2026-06-01 09:00:00.000000',
                run_first_scheduled_at: '2026-06-01 09:00:00.000000',
                invocation_globals: JSON.stringify(persistedState),
            },
        ]

        const hogFlowQueue = {
            queueInvocations: jest.fn().mockResolvedValue(undefined),
        } as unknown as jest.Mocked<CyclotronJobQueuePostgresV2>

        const paginator = buildPaginator(fakeClickhouse(rows), hogFlowQueue)
        await paginator.processPage(teamId, state, { jobId: 'rerun-2', createdAt: DateTime.now() })

        const invocation = hogFlowQueue.queueInvocations.mock.calls[0][0][0] as CyclotronJobInvocationHogFlow
        expect(invocation.state?.currentAction).toBeUndefined()
        expect(invocation.state?.customerTaskIdempotencyVersion).toBeUndefined()
    })
})