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.

17.7 KB
Raw Blame
import { PutObjectCommandInput, S3Client } from '@aws-sdk/client-s3'
import { ParquetReader } from '@dsnp/parquetjs'
import { register } from 'prom-client'

import { BlockMetadataParquetStore } from './block-metadata-parquet-store'
import { MlBlockMetadataRow } from './block-metadata-row'
import { TrainingEncryptionVector } from './keys/envelope-testing'
import { replayIndexPartitions } from './replay-index'

const row = (sessionId: string, teamId: string): MlBlockMetadataRow => ({
    session_id: sessionId,
    team_id: teamId,
    block_url: `s3://ml-bucket/key-${sessionId}?range=bytes=0-9`,
    block_s3_key: `s3://ml-bucket/key-${sessionId}`,
    block_byte_start: 0,
    block_byte_end: 9,
    block_length: 10,
    first_ts_ms: 1_000,
    last_ts_ms: 2_000,
    event_count: 4,
    message_count: 2,
    click_count: 1,
    keypress_count: 0,
    mouse_activity_count: 1,
    active_milliseconds: 500,
    console_log_count: 0,
    console_warn_count: 0,
    console_error_count: 0,
    size: 100,
    first_url: 'https://x/[redacted]',
    urls: ['https://x/[redacted]'],
    snapshot_source: 'web',
    snapshot_library: 'web',
    retention_period_days: 30,
})

const sessionStartingAt = (date: string): string => {
    const hex = Date.parse(date).toString(16).padStart(12, '0')
    return `${hex.slice(0, 8)}-${hex.slice(8)}-7000-8000-000000000007`
}

async function readRows(body: PutObjectCommandInput['Body']): Promise<Record<string, any>[]> {
    const reader = await ParquetReader.openBuffer(body as Buffer)
    const cursor = reader.getCursor()
    const rows: Record<string, any>[] = []
    let r: unknown
    while ((r = await cursor.next())) {
        rows.push(r as Record<string, any>)
    }
    await reader.close()
    return rows
}

describe('BlockMetadataParquetStore', () => {
    let puts: PutObjectCommandInput[]
    let s3: S3Client

    beforeEach(() => {
        register.resetMetrics()
        puts = []
        s3 = {
            send: jest.fn((cmd: { input: PutObjectCommandInput }) => {
                puts.push(cmd.input)
                return Promise.resolve({})
            }),
        } as unknown as S3Client
    })

    it('keeps sealed storage for v2 sessions and refuses v3 sessions', async () => {
        const [v2Envelope, v3Envelope] = ['2026-09-21T16:59:59.999Z', '2026-09-21T17:00:00Z'].map((date) => ({
            ...TrainingEncryptionVector.envelope,
            context: { ...TrainingEncryptionVector.envelope.context, sessionId: sessionStartingAt(date) },
        }))
        const store = new BlockMetadataParquetStore(
            s3,
            { v2: 'ml-bucket', v3: 'ml-bucket-v3' },
            'block-metadata',
            'pod'
        )
        await store.writeEncrypted([v2Envelope])
        await store.writeEncryptedReplayIndex([{ kind: 'page', rowCount: 1, envelope: v2Envelope }])
        expect(puts.map((put) => [put.Bucket, put.Key?.split('/').slice(0, 3).join('/')])).toEqual([
            ['ml-bucket', 'block-metadata/v2/2026-09'],
            ['ml-bucket', 'block-metadata-replay-index/v2/2026-09'],
        ])
        await expect(store.writeEncrypted([v3Envelope])).rejects.toThrow('plain storage')
        await expect(
            store.writeEncryptedReplayIndex([{ kind: 'page', rowCount: 1, envelope: v3Envelope }])
        ).rejects.toThrow('plain storage')
        expect(puts).toHaveLength(2)
    })

    it('writes v3 metadata as plain rows split by session month rather than upload time', async () => {
        const rows = ['2026-09-30T23:59:59.999Z', '2026-10-01T00:00:00Z'].map((date) =>
            row(sessionStartingAt(date), '7')
        )
        await new BlockMetadataParquetStore(
            s3,
            { v2: 'ml-bucket', v3: 'ml-bucket-v3' },
            'block-metadata',
            'pod'
        ).writePlainV3(rows)
        expect(puts.map((put) => [put.Bucket, put.Key?.split('/').slice(0, 3).join('/')])).toEqual([
            ['ml-bucket-v3', 'block-metadata/v3/2026-09'],
            ['ml-bucket-v3', 'block-metadata/v3/2026-10'],
        ])
        for (const [index, put] of puts.entries()) {
            expect(await readRows(put.Body)).toEqual([
                expect.objectContaining({
                    team_id: '7',
                    session_id: rows[index].session_id,
                    urls: ['https://x/[redacted]'],
                }),
            ])
        }
    })

    it('stores v3 eval indexes by session month with raw team IDs and scrubbed URLs across late blocks', async () => {
        const starts = ['2026-09-30T23:59:59.999Z', '2026-10-01T00:00:00Z']
        const eventTimestamp = Date.parse('2026-10-01T00:00:01Z')
        const store = new BlockMetadataParquetStore(
            s3,
            { v2: 'ml-bucket', v3: 'ml-bucket-v3' },
            'block-metadata',
            'pod'
        )
        for (const [index, start] of starts.entries()) {
            const sessionId = sessionStartingAt(start)
            await store.writePlainV3([
                {
                    ...row(sessionId, '7'),
                    format_version: 2,
                    first_ts_ms: eventTimestamp,
                    last_ts_ms: eventTimestamp,
                    replay_index_entries: [
                        { kind: 'full_snapshot', windowId: 'w1', eventTimestamp, eventIndex: 0 },
                        { kind: 'json_ld', windowId: 'w1', eventTimestamp, eventIndex: 1, rootTypes: ['Product'] },
                        {
                            kind: 'page',
                            windowId: 'w1',
                            eventTimestamp,
                            eventIndex: 1,
                            url: 'https://example.com/product',
                        },
                    ],
                },
            ])
            const month = start.slice(0, 7)
            const objects = puts.filter((put) => put.Key!.startsWith(`block-metadata-replay-index/v3/${month}/`))
            expect(objects).toHaveLength(2)
            const labels = await readRows(objects.find((put) => put.Key!.includes('/kind=json_ld/'))!.Body)
            expect(labels).toEqual([
                expect.objectContaining({
                    team_id: '7',
                    session_id: sessionId,
                    session_start_ts_ms: Date.parse(starts[index]),
                    event_ts_ms: eventTimestamp,
                    kind: 'json_ld',
                    root_types: ['Product'],
                    url: 'https://example.com/product',
                }),
            ])
        }
    })

    it.each([false, true])('counts encrypted uploads and failures: %s', async (failed) => {
        if (failed) {
            jest.mocked(s3.send).mockImplementationOnce(() => Promise.reject(new Error('upload failed')))
        }
        const store = new BlockMetadataParquetStore(
            s3,
            { v2: 'ml-bucket', v3: 'ml-bucket-v3' },
            'block-metadata',
            'pod-1'
        )
        const write = store.writeEncrypted([TrainingEncryptionVector.envelope])
        if (failed) {
            await expect(write).rejects.toThrow('upload failed')
        } else {
            await write
        }
        const rows = await register.getSingleMetric('ml_mirror_parquet_sink_rows_written_total')!.get()
        const failures = await register.getSingleMetric('ml_mirror_parquet_sink_write_errors_total')!.get()
        expect(rows.values[0].value).toBe(failed ? 0 : 1)
        expect(failures.values[0].value).toBe(failed ? 1 : 0)
        if (!failed) {
            expect((await readRows(puts[0].Body))[0].payload).toEqual(
                Buffer.from(JSON.stringify(TrainingEncryptionVector.envelope))
            )
        }
    })

    it.each([
        { name: 'adjacent URL', pageIndex: 0, pageTime: 1000, window: 'w1', expected: 'https://example.com/page' },
        { name: 'same timestamp', pageIndex: 0, pageTime: 1100, window: 'w1', expected: 'https://example.com/page' },
        { name: 'intervening event', pageIndex: 0, pageTime: 1000, window: 'w1', expected: null },
        { name: 'different window', pageIndex: 0, pageTime: 1000, window: 'w2', expected: null },
        { name: 'reversed timestamps', pageIndex: 0, pageTime: 1200, window: 'w1', expected: null },
        { name: 'later URL', pageIndex: 4, pageTime: 1200, window: 'w1', expected: null },
    ])('enriches snapshot URLs from block metadata: $name', ({ name, pageIndex, pageTime, window, expected }) => {
        const block: MlBlockMetadataRow = {
            ...row('s1', 't1'),
            session_start_ts_ms: 1000,
            event_count: 6,
            replay_index_entries: [
                {
                    kind: 'full_snapshot',
                    windowId: 'w1',
                    eventIndex: name === 'intervening event' ? 2 : 1,
                    eventTimestamp: 1100,
                },
                {
                    kind: 'page',
                    windowId: window,
                    eventIndex: pageIndex,
                    eventTimestamp: pageTime,
                    url: 'https://example.com/page',
                },
            ],
        }
        const entries = [...replayIndexPartitions([block]).values()].flat()
        expect(entries.find((entry) => entry.kind === 'full_snapshot')?.url).toBe(expected)
    })

    it('does not carry URLs between blocks', () => {
        const source = {
            ...row('s1', 't1'),
            session_start_ts_ms: 1000,
        }
        const entries = [
            ...replayIndexPartitions([
                {
                    ...source,
                    replay_index_entries: [
                        {
                            kind: 'page',
                            windowId: 'w1',
                            eventIndex: 0,
                            eventTimestamp: 1000,
                            url: 'https://example.com/page',
                        },
                    ],
                },
                {
                    ...source,
                    replay_index_entries: [
                        { kind: 'full_snapshot', windowId: 'w1', eventIndex: 1, eventTimestamp: 1100 },
                    ],
                },
            ]).values(),
        ].flat()
        expect(entries.find((entry) => entry.kind === 'full_snapshot')?.url).toBeNull()
    })

    it.each([
        {
            storage: 'legacy',
            write: (store: BlockMetadataParquetStore) => store.write([{ ...row('session', '42'), format_version: 2 }]),
            error: 'sealed v2 or plain v3 storage',
        },
        {
            storage: 'plain v3',
            write: (store: BlockMetadataParquetStore) =>
                store.writePlainV3([
                    { ...row(sessionStartingAt('2026-09-21T16:59:59.999Z'), '42'), format_version: 2 },
                ]),
            error: 'only v3 sessions',
        },
    ])('rejects v2 metadata in $storage storage before writing any objects', async ({ write, error }) => {
        const store = new BlockMetadataParquetStore(
            s3,
            { v2: 'ml-bucket', v3: 'ml-bucket-v3' },
            'block-metadata',
            'pod-1'
        )
        await expect(write(store)).rejects.toThrow(error)
        expect(puts).toHaveLength(0)
    })

    it('writes one dt-partitioned Parquet object that round-trips', async () => {
        const store = new BlockMetadataParquetStore(
            s3,
            { v2: 'ml-bucket', v3: 'ml-bucket-v3' },
            'block-metadata',
            'pod-1'
        )
        await store.write([row('s1', 't1'), row('s2', 't1')])

        expect(puts).toHaveLength(1)
        expect(puts[0].Bucket).toBe('ml-bucket')
        expect(puts[0].Key).toMatch(/^block-metadata\/dt=\d{4}-\d{2}-\d{2}\/part-pod-1-\d+-\d+\.parquet$/)

        const rows = await readRows(puts[0].Body)
        expect(rows).toHaveLength(2)
        expect(Number(rows[0].block_byte_end)).toBe(9)
        expect(rows[0].snapshot_source).toBe('web')
    })

    it('indexes cross-block pairs by session start day and preserves window and fractional timestamps', async () => {
        const store = new BlockMetadataParquetStore(
            s3,
            { v2: 'ml-bucket', v3: 'ml-bucket-v3' },
            'block-metadata',
            'pod-1'
        )
        const start = Date.parse('2025-01-01T23:59:00Z')
        const snapshotTimestamp = start + 60_000.5
        const full = {
            ...row('s1', 't1'),
            session_start_ts_ms: start,
            first_ts_ms: snapshotTimestamp,
            last_ts_ms: snapshotTimestamp,
            replay_index_entries: [
                { kind: 'full_snapshot' as const, windowId: 'w1', eventTimestamp: snapshotTimestamp, eventIndex: 0 },
                {
                    kind: 'page' as const,
                    windowId: 'w1',
                    eventTimestamp: snapshotTimestamp,
                    eventIndex: 1,
                    url: 'https://example.com/[redacted]',
                },
            ],
        }
        const label = {
            ...full,
            block_s3_key: 's3://ml-bucket/other-block',
            replay_index_entries: [
                {
                    kind: 'json_ld' as const,
                    windowId: 'w1',
                    eventTimestamp: snapshotTimestamp,
                    eventIndex: 2,
                    fullSnapshotTimestamp: snapshotTimestamp,
                    rootTypes: ['Product'],
                },
            ],
        }
        await store.write([label])
        await store.write([full])
        const indexPuts = puts.filter((put) => put.Key!.includes('-replay-index/'))
        expect(indexPuts).toHaveLength(3)
        for (const put of indexPuts) {
            expect(put.Key).toContain('/session_start_date=2025-01-01/')
        }
        const labels = await readRows(indexPuts.find((put) => put.Key!.includes('kind=json_ld'))!.Body)
        const snapshots = await readRows(indexPuts.find((put) => put.Key!.includes('kind=full_snapshot'))!.Body)
        expect(labels[0]).toMatchObject({
            team_id: 't1',
            session_id: 's1',
            window_id: 'w1',
            event_index: 2,
            full_snapshot_ts_ms: snapshots[0].event_ts_ms,
            root_types: ['Product'],
            block_s3_key: 's3://ml-bucket/other-block',
            block_byte_start: 0,
            block_byte_end: 9,
        })
        expect(snapshots[0].event_ts_ms).toBe(snapshotTimestamp)
        const pages = await readRows(indexPuts.find((put) => put.Key!.includes('kind=page'))!.Body)
        expect(pages[0].url).toBe('https://example.com/[redacted]')
    })

    it('propagates index upload failures so Kafka offsets cannot advance', async () => {
        const store = new BlockMetadataParquetStore(
            s3,
            { v2: 'ml-bucket', v3: 'ml-bucket-v3' },
            'block-metadata',
            'pod-1'
        )
        jest.mocked(s3.send).mockImplementationOnce(() => Promise.reject(new Error('index unavailable')))
        await expect(
            store.write([
                {
                    ...row('s1', 't1'),
                    session_start_ts_ms: 1000,
                    replay_index_entries: [
                        { kind: 'full_snapshot', windowId: 'w1', eventTimestamp: 1000, eventIndex: 0 },
                    ],
                },
            ])
        ).rejects.toThrow('index unavailable')
    })

    it.each([
        { session_start_ts_ms: undefined },
        { session_start_ts_ms: 3000 },
        { session_start_ts_ms: 1, last_ts_ms: 8 * 86_400_000 },
        { replay_index_entries: [{ kind: 'full_snapshot', windowId: 'w1', eventTimestamp: 1000, eventIndex: 99 }] },
        {
            replay_index_entries: [
                {
                    kind: 'json_ld',
                    windowId: 'w1',
                    eventTimestamp: 1000,
                    eventIndex: 0,
                    fullSnapshotTimestamp: 'invalid',
                },
            ],
        },
    ])('keeps block metadata when index fields are unusable: %j', async (override) => {
        const store = new BlockMetadataParquetStore(
            s3,
            { v2: 'ml-bucket', v3: 'ml-bucket-v3' },
            'block-metadata',
            'pod-1'
        )
        await store.write([
            {
                ...row('s1', 't1'),
                session_start_ts_ms: 1000,
                replay_index_entries: [{ kind: 'full_snapshot', windowId: 'w1', eventTimestamp: 1000, eventIndex: 0 }],
                ...override,
            } as MlBlockMetadataRow,
        ])
        expect(puts).toHaveLength(1)
        expect(puts[0].Key).toMatch(/^block-metadata\/dt=/)
    })

    it('sorts rows by (team_id, session_id)', async () => {
        const store = new BlockMetadataParquetStore(
            s3,
            { v2: 'ml-bucket', v3: 'ml-bucket-v3' },
            'block-metadata',
            'pod-1'
        )
        await store.write([row('s9', 't3'), row('s1', 't1'), row('s5', 't2'), row('s2', 't1')])
        const rows = await readRows(puts[0].Body)
        const keys = rows.map((r) => `${r.team_id}|${r.session_id}`)
        expect(keys).toEqual([...keys].sort())
    })

    it('writes nothing for an empty batch', async () => {
        const store = new BlockMetadataParquetStore(
            s3,
            { v2: 'ml-bucket', v3: 'ml-bucket-v3' },
            'block-metadata',
            'pod-1'
        )
        await store.write([])
        expect(puts).toHaveLength(0)
    })

    it('propagates upload failures so the caller can replay from Kafka', async () => {
        s3 = { send: jest.fn(() => Promise.reject(new Error('s3 down'))) } as unknown as S3Client
        const store = new BlockMetadataParquetStore(
            s3,
            { v2: 'ml-bucket', v3: 'ml-bucket-v3' },
            'block-metadata',
            'pod-1'
        )
        await expect(store.write([row('s1', 't1')])).rejects.toThrow('s3 down')
    })
})