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.

62.4 KB
Raw Blame
import { Message, MessageHeader, TopicPartitionOffset } from 'node-rdkafka'
import { gzipSync } from 'node:zlib'

import { MlKeyReader } from '~/ingestion/pipelines/sessionreplay/ml-mirror/keys/reader'
import { MlKeyManager } from '~/ingestion/pipelines/sessionreplay/ml-mirror/keys/runtime'
import {
    INGESTION_VERSION_HEADER,
    imageKeyId,
    tableKeyString,
} from '~/ingestion/pipelines/sessionreplay/ml-mirror/keys/schema'
import { MlKafkaTransport } from '~/ingestion/pipelines/sessionreplay/ml-mirror/keys/transport'
import { CAPTURE_TIMESTAMP_HEADER } from '~/ingestion/pipelines/sessionreplay/shared/capture-watermark'

import { hashImageBytes, imageRef, urlRef } from './content-ref'
import { ImageBatcher, OffsetStore, WRITE_CONCURRENCY } from './image-batcher'
import { ImageShardStore, ScrubbedImage, ScrubbedUrlImage, UrlImageWriteOutcome } from './image-shard-store'
import { ImageScrubConsumerMetrics } from './metrics'
import { ScrubClient, ScrubPoisoned } from './scrub-client'

const pt = (n: number): string => String(n).padStart(32, '0')
const CONTENT_KEY = 'fedcba9876543210fedcba9876543210'
const CAPTURED_AT = 1_700_000_000_000

function msg(
    partition: number,
    offset: number,
    pseudoTeam: string,
    bytes: Buffer,
    keyOverride?: string,
    headers?: MessageHeader[]
): Message {
    const ref = keyOverride ?? imageRef(pseudoTeam, hashImageBytes(CONTENT_KEY, bytes))
    return {
        topic: 'session_replay_image_scrub',
        partition,
        offset,
        key: Buffer.from(ref),
        value: bytes,
        headers,
    } as unknown as Message
}

class FakeStore {
    public writes: ScrubbedImage[][] = []
    public urlWrites: ScrubbedUrlImage[] = []
    public failNext = false
    public failNextUrl = false
    public urlWriteOutcome: UrlImageWriteOutcome = 'created'
    // eslint-disable-next-line @typescript-eslint/require-await
    async writeShard(images: ScrubbedImage[]): Promise<{ shard: string; bytes: number }> {
        if (this.failNext) {
            throw new Error('s3 down')
        }
        this.writes.push(images)
        return { shard: 'shard', bytes: images.reduce((n, i) => n + i.bytes.length, 0) }
    }

    // eslint-disable-next-line @typescript-eslint/require-await
    async writeUrlImage(image: ScrubbedUrlImage): Promise<UrlImageWriteOutcome> {
        if (this.failNextUrl) {
            throw new Error('url s3 down')
        }
        this.urlWrites.push(image)
        return this.urlWriteOutcome
    }
}

class FakeOffsets implements OffsetStore {
    public stored = 0
    public received: TopicPartitionOffset[][] = []
    offsetsStore(offsets: TopicPartitionOffset[]): void {
        this.stored += 1
        this.received.push(offsets)
    }
}

const scrubClient = {
    scrub: (b: Buffer) => Promise.resolve(Buffer.concat([Buffer.from('x'), b])),
} as unknown as ScrubClient

const options = {
    flushIntervalMs: 0,
    maxImages: 1000,
    maxBytes: 1e9,
    scrubConcurrency: 4,
    dedupMaxRefs: 1000,
}

/** Runs a batch and waits for its writes, for tests that assert on what reached the store. */
async function handleAndWrite(batcher: ImageBatcher, messages: Message[]): Promise<void> {
    await batcher.handleBatch(messages)
    await batcher.drain()
}

describe('ImageBatcher', () => {
    afterEach(() => jest.restoreAllMocks())

    it.each([false, true])(
        'retries malformed encrypted image parking and supports shutdown (stop: %s)',
        async (stop) => {
            jest.useFakeTimers()
            try {
                const store = new FakeStore()
                const offsets = new FakeOffsets()
                const park = jest.fn().mockRejectedValueOnce(new Error('dlq unavailable')).mockResolvedValue(undefined)
                const keyManager = {
                    kafka: new MlKafkaTransport({
                        read: jest.fn().mockResolvedValue(new Map()),
                    } as unknown as MlKeyReader),
                } as MlKeyManager
                const incrementVersion = jest.spyOn(ImageScrubConsumerMetrics, 'incrementVersion')
                const batcher = new ImageBatcher(
                    store as unknown as ImageShardStore,
                    offsets,
                    scrubClient,
                    options,
                    { park },
                    keyManager
                )
                const invalid = msg(0, 0, pt(1), Buffer.from('invalid envelope'), undefined, [
                    { [INGESTION_VERSION_HEADER]: Buffer.from('2') },
                ])
                const batch = batcher.handleBatch([invalid])
                await jest.advanceTimersByTimeAsync(0)
                expect(park).toHaveBeenCalledTimes(1)
                expect(offsets.received).toEqual([])
                // Neither bucket moves: a decryption outage must not read as a rollback to version 1.
                expect(incrementVersion.mock.calls).toEqual([
                    ['2', 0],
                    ['1', 0],
                ])
                if (stop) {
                    await batcher.stop()
                } else {
                    await jest.advanceTimersByTimeAsync(500)
                }
                await expect(batch).resolves.toBeUndefined()
                expect(park).toHaveBeenCalledTimes(stop ? 1 : 2)
                expect(park.mock.calls[0][0]).toEqual({
                    ref: invalid.key!.toString(),
                    bytes: invalid.value,
                    headers: { [INGESTION_VERSION_HEADER]: '2' },
                    detail: {
                        reason: 'invalid_record',
                        sourceTopic: invalid.topic,
                        sourcePartition: 0,
                        sourceOffset: 0,
                    },
                })
                expect(store.writes).toEqual([])
                if (!stop) {
                    expect(offsets.received.flat().map((offset) => offset.offset)).toEqual([1])
                }
            } finally {
                jest.useRealTimers()
            }
        }
    )

    it.each([false, true])('scrubs mixed teams and separates legacy records (mixed formats: %s)', async (mixed) => {
        const store = new FakeStore()
        const offsets = new FakeOffsets()
        const observeCaptureToS3 = jest.spyOn(ImageScrubConsumerMetrics, 'observeCaptureToS3').mockImplementation()
        const batcher = new ImageBatcher(store as unknown as ImageShardStore, offsets, scrubClient, options)

        const captureHeader = [{ [CAPTURE_TIMESTAMP_HEADER]: Buffer.from(String(CAPTURED_AT)) }]
        await handleAndWrite(batcher, [
            msg(0, 0, pt(1), Buffer.from('a'), undefined, captureHeader),
            msg(0, 1, mixed ? '42' : pt(2), Buffer.from('b'), undefined, captureHeader),
        ])

        expect(store.writes).toHaveLength(mixed ? 2 : 1)
        expect(
            store.writes
                .flat()
                .map((i) => i.teamId ?? i.pseudoTeam)
                .sort()
        ).toEqual([pt(1), mixed ? '42' : pt(2)])
        if (mixed) {
            expect(store.writes.flat().find((image) => image.teamId === '42')).toBeDefined()
            expect(store.writes.flat().find((image) => image.pseudoTeam === pt(1))).toBeDefined()
        }
        expect(offsets.stored).toBe(1)
        expect(observeCaptureToS3).toHaveBeenCalledTimes(2)
        expect(observeCaptureToS3).toHaveBeenNthCalledWith(1, 'inline', CAPTURED_AT, expect.any(Number))
        expect(observeCaptureToS3).toHaveBeenNthCalledWith(2, 'inline', CAPTURED_AT, expect.any(Number))
    })

    it('trusts the producer ref: the bytes are indexed under the key hash without recomputing it', async () => {
        // The hash is a producer-side per-team HMAC; this consumer has no key and must not try to
        // validate — it indexes the scrubbed bytes under whatever hash the ref carries.
        const store = new FakeStore()
        const batcher = new ImageBatcher(store as unknown as ImageShardStore, new FakeOffsets(), scrubClient, options)

        const ref = imageRef(pt(1), hashImageBytes('some-opaque-producer-key', Buffer.from('a')))
        await handleAndWrite(batcher, [msg(0, 0, pt(1), Buffer.from('a'), ref)])

        expect(store.writes).toHaveLength(1)
        expect(store.writes[0][0].hash).toBe(ref.split(':')[2])
    })

    it('counts a cleartext image as version 1', async () => {
        const incrementVersion = jest.spyOn(ImageScrubConsumerMetrics, 'incrementVersion')
        const batcher = new ImageBatcher(
            new FakeStore() as unknown as ImageShardStore,
            new FakeOffsets(),
            scrubClient,
            options
        )

        await handleAndWrite(batcher, [msg(0, 0, pt(1), Buffer.from('a'))])

        expect(incrementVersion.mock.calls).toEqual([
            ['2', 0],
            ['1', 1],
        ])
    })

    it('decodes and validates a URL image from its Kafka transport headers before scrubbing it', async () => {
        const png = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])
        const received: Buffer[] = []
        const capturingClient = {
            scrub: (bytes: Buffer) => {
                received.push(bytes)
                return Promise.resolve(bytes)
            },
        } as unknown as ScrubClient
        const store = new FakeStore()
        const observeCaptureToS3 = jest.spyOn(ImageScrubConsumerMetrics, 'observeCaptureToS3').mockImplementation()
        const batcher = new ImageBatcher(
            store as unknown as ImageShardStore,
            new FakeOffsets(),
            capturingClient,
            options
        )
        const ref = urlRef(hashImageBytes(CONTENT_KEY, Buffer.from('https://example.com/image.png')))

        await handleAndWrite(batcher, [
            msg(0, 0, pt(1), gzipSync(png), ref, [
                { 'content-type': Buffer.from('image/png') },
                { 'content-encoding': Buffer.from('gzip') },
                { [CAPTURE_TIMESTAMP_HEADER]: Buffer.from(String(CAPTURED_AT)) },
            ]),
        ])

        expect(received).toEqual([png])
        expect(store.urlWrites).toEqual([
            {
                hash: ref.slice('imageurl:'.length),
                bytes: png,
                sourcePartition: 0,
                sourceOffset: 0,
            },
        ])
        expect(store.writes).toHaveLength(0)
        expect(observeCaptureToS3).toHaveBeenCalledWith('url', CAPTURED_AT, expect.any(Number))
    })

    it('does not observe URL latency when the object already exists', async () => {
        const png = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])
        const store = new FakeStore()
        store.urlWriteOutcome = 'already_exists'
        const observeCaptureToS3 = jest.spyOn(ImageScrubConsumerMetrics, 'observeCaptureToS3').mockImplementation()
        const batcher = new ImageBatcher(
            store as unknown as ImageShardStore,
            new FakeOffsets(),
            { scrub: (bytes: Buffer) => Promise.resolve(bytes) } as unknown as ScrubClient,
            options
        )
        const ref = urlRef(hashImageBytes(CONTENT_KEY, Buffer.from('https://example.com/image.png')))

        await handleAndWrite(batcher, [
            msg(0, 0, pt(1), png, ref, [
                { 'content-type': Buffer.from('image/png') },
                { [CAPTURE_TIMESTAMP_HEADER]: Buffer.from(String(CAPTURED_AT)) },
            ]),
        ])

        expect(store.urlWrites).toHaveLength(1)
        expect(observeCaptureToS3).not.toHaveBeenCalled()
    })

    it('submits every valid URL image for the conditional store to select the first completed write', async () => {
        const png = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])
        const newer = Buffer.concat([png, Buffer.from('newer')])
        const older = Buffer.concat([png, Buffer.from('older')])
        const store = new FakeStore()
        const batcher = new ImageBatcher(
            store as unknown as ImageShardStore,
            new FakeOffsets(),
            { scrub: (bytes: Buffer) => Promise.resolve(bytes) } as unknown as ScrubClient,
            options
        )
        const ref = urlRef(hashImageBytes(CONTENT_KEY, Buffer.from('https://example.com/image.png')))

        await handleAndWrite(batcher, [
            msg(0, 12, pt(1), newer, ref, [{ 'content-type': Buffer.from('image/png') }]),
            msg(0, 10, pt(1), older, ref, [{ 'content-type': Buffer.from('image/png') }]),
        ])

        expect(store.urlWrites).toEqual([
            {
                hash: ref.slice('imageurl:'.length),
                bytes: newer,
                sourcePartition: 0,
                sourceOffset: 12,
            },
            {
                hash: ref.slice('imageurl:'.length),
                bytes: older,
                sourcePartition: 0,
                sourceOffset: 10,
            },
        ])
    })

    it('uses a later valid URL image when an earlier candidate for the ref is invalid', async () => {
        const png = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])
        const store = new FakeStore()
        const batcher = new ImageBatcher(
            store as unknown as ImageShardStore,
            new FakeOffsets(),
            { scrub: (bytes: Buffer) => Promise.resolve(bytes) } as unknown as ScrubClient,
            options
        )
        const ref = urlRef(hashImageBytes(CONTENT_KEY, Buffer.from('https://example.com/image.png')))

        await handleAndWrite(batcher, [
            msg(0, 10, pt(1), Buffer.from('not a png'), ref, [{ 'content-type': Buffer.from('image/png') }]),
            msg(0, 11, pt(1), png, ref, [{ 'content-type': Buffer.from('image/png') }]),
        ])

        expect(store.urlWrites).toEqual([
            {
                hash: ref.slice('imageurl:'.length),
                bytes: png,
                sourcePartition: 0,
                sourceOffset: 11,
            },
        ])
    })

    it('skips a URL ref in a later batch after this pod persisted it', async () => {
        const png = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])
        const first = Buffer.concat([png, Buffer.from('first')])
        const second = Buffer.concat([png, Buffer.from('second')])
        const store = new FakeStore()
        let scrubs = 0
        const batcher = new ImageBatcher(
            store as unknown as ImageShardStore,
            new FakeOffsets(),
            {
                scrub: (bytes: Buffer) => {
                    scrubs += 1
                    return Promise.resolve(bytes)
                },
            } as unknown as ScrubClient,
            options
        )
        const ref = urlRef(hashImageBytes(CONTENT_KEY, Buffer.from('https://example.com/image.png')))

        await handleAndWrite(batcher, [msg(0, 10, pt(1), first, ref, [{ 'content-type': Buffer.from('image/png') }])])
        await handleAndWrite(batcher, [msg(0, 11, pt(1), second, ref, [{ 'content-type': Buffer.from('image/png') }])])

        expect(scrubs).toBe(1)
        expect(store.urlWrites.map((image) => [image.sourceOffset, image.bytes])).toEqual([[10, first]])
    })

    it('stores a URL image in a later batch when its month key was missing at the first write', async () => {
        const png = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])
        const ref = `imageurl:v3:7:2026-09:${'a'.repeat(22)}`
        const monthKey = tableKeyString(imageKeyId(7, '2026-09'))
        const readKeys = jest
            .fn()
            .mockResolvedValueOnce(new Map())
            .mockResolvedValue(new Map([[monthKey, { identity: { teamId: 7, sessionMonth: '2026-09' } }]]))
        const keyManager = {
            kafka: {
                read: (messages: Message[]) =>
                    Promise.resolve(messages.map((message) => ({ message, original: message, version: 2 }))),
            },
            reader: { read: readKeys },
        } as unknown as MlKeyManager
        const store = new FakeStore()
        const batcher = new ImageBatcher(
            store as unknown as ImageShardStore,
            new FakeOffsets(),
            { scrub: (bytes: Buffer) => Promise.resolve(bytes) } as unknown as ScrubClient,
            options,
            null,
            keyManager
        )
        const copy = (offset: number): Message =>
            msg(0, offset, pt(1), png, ref, [{ 'content-type': Buffer.from('image/png') }])

        await handleAndWrite(batcher, [copy(10)])
        await handleAndWrite(batcher, [copy(11)])

        expect(store.urlWrites.map((image) => image.sourceOffset)).toEqual([11])
    })

    it('keeps a URL ref seen when a later copy of it is dropped after an earlier copy was stored', async () => {
        const png = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])
        const ref = `imageurl:v3:7:2026-09:${'a'.repeat(22)}`
        const monthKey = tableKeyString(imageKeyId(7, '2026-09'))
        const readKeys = jest
            .fn()
            .mockResolvedValueOnce(new Map([[monthKey, { identity: { teamId: 7, sessionMonth: '2026-09' } }]]))
            .mockResolvedValue(new Map())
        const keyManager = {
            kafka: {
                read: (messages: Message[]) =>
                    Promise.resolve(messages.map((message) => ({ message, original: message, version: 2 }))),
            },
            reader: { read: readKeys },
        } as unknown as MlKeyManager
        const store = new FakeStore()
        let scrubs = 0
        const batcher = new ImageBatcher(
            store as unknown as ImageShardStore,
            new FakeOffsets(),
            {
                scrub: (bytes: Buffer) => {
                    scrubs += 1
                    return Promise.resolve(bytes)
                },
            } as unknown as ScrubClient,
            { ...options, maxImages: 1, scrubConcurrency: 1 },
            null,
            keyManager
        )
        const copy = (offset: number): Message =>
            msg(0, offset, pt(1), png, ref, [{ 'content-type': Buffer.from('image/png') }])

        await handleAndWrite(batcher, [copy(10), copy(11)])
        await handleAndWrite(batcher, [copy(12)])

        expect(store.urlWrites.map((image) => image.sourceOffset)).toEqual([10])
        expect(scrubs).toBe(2)
    })

    it('bounds concurrent URL-image writes by the write concurrency, not the scrub slots', async () => {
        // The writes run behind the next batch's scrub, so sharing the scrub slots would let a write
        // burst take the sidecar's work away from it. The bound is the write lane's own.
        const png = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])
        let activeWrites = 0
        let maximumActiveWrites = 0
        const store = {
            writeUrlImage: async () => {
                activeWrites += 1
                maximumActiveWrites = Math.max(maximumActiveWrites, activeWrites)
                await Promise.resolve()
                activeWrites -= 1
            },
            writeShard: () => Promise.resolve({ shard: 'unused', bytes: 0 }),
        } as unknown as ImageShardStore
        const batcher = new ImageBatcher(
            store,
            new FakeOffsets(),
            { scrub: () => Promise.resolve(png) } as unknown as ScrubClient,
            { ...options, scrubConcurrency: 2 }
        )
        const messages = Array.from({ length: WRITE_CONCURRENCY + 2 }, (_, index) => {
            const ref = urlRef(hashImageBytes(CONTENT_KEY, Buffer.from(`https://example.com/${index}.png`)))
            return msg(0, index, pt(1), png, ref, [{ 'content-type': Buffer.from('image/png') }])
        })

        await handleAndWrite(batcher, messages)

        expect(maximumActiveWrites).toBe(WRITE_CONCURRENCY)
    })

    it('drops a URL image whose bytes do not match its Kafka content-type', async () => {
        let scrubCalls = 0
        const incSkipped = jest.spyOn(ImageScrubConsumerMetrics, 'incSkipped')
        const batcher = new ImageBatcher(
            new FakeStore() as unknown as ImageShardStore,
            new FakeOffsets(),
            {
                scrub: () => {
                    scrubCalls += 1
                    return Promise.resolve(Buffer.from('scrubbed'))
                },
            } as unknown as ScrubClient,
            options
        )
        const ref = urlRef(hashImageBytes(CONTENT_KEY, Buffer.from('https://example.com/image.png')))

        await handleAndWrite(batcher, [
            msg(0, 0, pt(1), Buffer.from([0xff, 0xd8, 0xff]), ref, [{ 'content-type': Buffer.from('image/png') }]),
        ])

        expect(scrubCalls).toBe(0)
        expect(incSkipped).toHaveBeenCalledWith('content_type_mismatch')
    })

    it('does not store offsets when the shard write fails, and fails the next batch so the pod replays', async () => {
        // The write runs behind the next batch, so the batch that scrubbed the image has already
        // returned when S3 refuses it. The lane is poisoned instead: nothing behind the failed write
        // stores an offset, and the next batch raises the failure, which is what exits the process.
        const store = new FakeStore()
        store.failNext = true
        const offsets = new FakeOffsets()
        const observeCaptureToS3 = jest.spyOn(ImageScrubConsumerMetrics, 'observeCaptureToS3').mockImplementation()
        const batcher = new ImageBatcher(store as unknown as ImageShardStore, offsets, scrubClient, options)

        await batcher.handleBatch([
            msg(0, 0, pt(1), Buffer.from('a'), undefined, [
                { [CAPTURE_TIMESTAMP_HEADER]: Buffer.from(String(CAPTURED_AT)) },
            ]),
        ])
        // The failed write has settled and left the in-flight list before anyone drains.
        for (let tick = 0; tick < 20; tick++) {
            await new Promise((resolve) => setImmediate(resolve))
        }
        await expect(batcher.drain()).rejects.toThrow('s3 down')

        store.failNext = false
        await expect(batcher.handleBatch([msg(0, 1, pt(1), Buffer.from('b'))])).rejects.toThrow('s3 down')
        expect(store.writes).toHaveLength(0)
        expect(offsets.stored).toBe(0)
        expect(observeCaptureToS3).not.toHaveBeenCalled()
    })

    it('does not create a random inline shard before deterministic URL writes succeed', async () => {
        const store = new FakeStore()
        store.failNextUrl = true
        const offsets = new FakeOffsets()
        const batcher = new ImageBatcher(store as unknown as ImageShardStore, offsets, scrubClient, options)
        const png = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])
        const ref = urlRef(hashImageBytes(CONTENT_KEY, Buffer.from('https://example.com/image.png')))

        await batcher.handleBatch([
            msg(0, 0, pt(1), Buffer.from('inline')),
            msg(0, 1, pt(1), png, ref, [{ 'content-type': Buffer.from('image/png') }]),
        ])
        await expect(batcher.drain()).rejects.toThrow('url s3 down')

        expect(store.writes).toHaveLength(0)
        expect(offsets.stored).toBe(0)
    })

    it('stores offsets with each mid-batch hand-off, so a later failure replays only from there', async () => {
        // A hand-off persists a randomly named shard; if the batch then fails (e.g. the scrub deadline)
        // without having stored those messages' offsets, Kafka replays them and every replay
        // writes another duplicate shard — unbounded write amplification an attacker can induce.
        const store = new FakeStore()
        const offsets = new FakeOffsets()
        let scrubs = 0
        const failsAfterFirstChunk = {
            scrub: () => {
                scrubs += 1
                return scrubs <= 2 ? Promise.resolve(Buffer.alloc(16)) : Promise.reject(new Error('sidecar down'))
            },
        } as unknown as ScrubClient
        const batcher = new ImageBatcher(store as unknown as ImageShardStore, offsets, failsAfterFirstChunk, {
            ...options,
            maxBytes: 32,
            scrubConcurrency: 2,
        })

        const messages = Array.from({ length: 4 }, (_, i) => msg(0, i, pt(1), Buffer.from(`img-${i}`)))
        await expect(batcher.handleBatch(messages)).rejects.toThrow('sidecar down')
        await batcher.drain()

        expect(store.writes).toHaveLength(1)
        // The handed-off chunk's messages (offsets 0-1) are recorded, so only 2-3 replay.
        expect(offsets.received).toEqual([[{ topic: 'session_replay_image_scrub', partition: 0, offset: 2 }]])
    })

    it('hands off mid-batch when scrubbed bytes cross the byte bound instead of holding the whole batch', async () => {
        // Scrubbed outputs can be far larger than their inputs (full-resolution PNG re-encode), so
        // the byte bound must apply while a poll batch is still scrubbing — a reverted batcher that
        // accumulates all outputs first can hold gigabytes before its first hand-off.
        const store = new FakeStore()
        const offsets = new FakeOffsets()
        const bigOutputClient = { scrub: () => Promise.resolve(Buffer.alloc(16)) } as unknown as ScrubClient
        const batcher = new ImageBatcher(store as unknown as ImageShardStore, offsets, bigOutputClient, {
            ...options,
            maxBytes: 32,
            scrubConcurrency: 2,
        })

        const messages = Array.from({ length: 6 }, (_, i) => msg(0, i, pt(1), Buffer.from(`img-${i}`)))
        await handleAndWrite(batcher, messages)

        expect(store.writes.length).toBeGreaterThan(1)
        expect(store.writes.flat()).toHaveLength(6)
        expect(offsets.stored).toBe(store.writes.length)
    })

    it('does not count write time against the scrub deadline', async () => {
        // Slow-but-succeeding S3 writes must not exhaust the scrub budget: a reverted deadline that
        // spans the writes turns degraded storage into an abort/replay loop blamed on the sidecar.
        const slowStore = new FakeStore()
        const writeShard = slowStore.writeShard.bind(slowStore)
        slowStore.writeShard = async (images) => {
            await new Promise((resolve) => setTimeout(resolve, 80))
            return writeShard(images)
        }
        const offsets = new FakeOffsets()
        const batcher = new ImageBatcher(
            slowStore as unknown as ImageShardStore,
            offsets,
            { scrub: () => Promise.resolve(Buffer.alloc(16)) } as unknown as ScrubClient,
            { ...options, maxBytes: 32, scrubConcurrency: 2 }
        )

        const messages = Array.from({ length: 6 }, (_, i) => msg(0, i, pt(1), Buffer.from(`img-${i}`)))
        await handleAndWrite(batcher, messages)

        expect(slowStore.writes.flat()).toHaveLength(6)
        expect(offsets.stored).toBe(slowStore.writes.length)
    })

    it('scrubs each ref once: duplicates in and across batches skip the sidecar but still advance offsets', async () => {
        // The topic is keyed by ref, so duplicate produces (per-mirror-pod producer dedup misses
        // cross-pod repeats) are partition-affine; losing this dedup silently re-burns sidecar CPU
        // per duplicate and writes duplicate shard entries.
        const store = new FakeStore()
        const offsets = new FakeOffsets()
        let scrubs = 0
        const countingClient = {
            scrub: (b: Buffer) => {
                scrubs += 1
                return Promise.resolve(b)
            },
        } as unknown as ScrubClient
        const batcher = new ImageBatcher(store as unknown as ImageShardStore, offsets, countingClient, options)

        const sprite = Buffer.from('sprite')
        await handleAndWrite(batcher, [msg(0, 0, pt(1), sprite), msg(0, 1, pt(1), sprite)])
        await handleAndWrite(batcher, [msg(0, 2, pt(1), sprite)])

        expect(scrubs).toBe(1)
        expect(store.writes.flat()).toHaveLength(1)
        // The all-duplicate second batch still advances offsets, or the duplicates replay forever.
        expect(offsets.received.at(-1)).toEqual([{ topic: 'session_replay_image_scrub', partition: 0, offset: 3 }])
    })

    it.each([0, 1000])(
        'scrubs the distinct images of a duplicate-heavy batch at full concurrency (dedupMaxRefs %p)',
        async (dedupMaxRefs) => {
            // Duplicates have to collapse before the batch is chunked. Left in, they occupy
            // concurrency slots, so a chunk finishes as soon as its one distinct image does and the
            // batch serializes behind the chunk barriers — the lane then runs one image in flight
            // instead of scrubConcurrency, regardless of how many pods it has. Consecutive repeats
            // are the realistic shape: one sprite recurs across a session's snapshots.
            const store = new FakeStore()
            let inFlight = 0
            let maxInFlight = 0
            let scrubs = 0
            const trackingClient = {
                scrub: async (b: Buffer) => {
                    scrubs += 1
                    inFlight += 1
                    maxInFlight = Math.max(maxInFlight, inFlight)
                    await new Promise((resolve) => setImmediate(resolve))
                    inFlight -= 1
                    return b
                },
            } as unknown as ScrubClient
            const batcher = new ImageBatcher(store as unknown as ImageShardStore, new FakeOffsets(), trackingClient, {
                ...options,
                dedupMaxRefs,
            })

            const batch: Message[] = []
            for (let sprite = 0; sprite < 4; sprite++) {
                for (let repeat = 0; repeat < 8; repeat++) {
                    batch.push(msg(0, batch.length, pt(1), Buffer.from(`sprite-${sprite}`)))
                }
            }
            await handleAndWrite(batcher, batch)

            expect(scrubs).toBe(4)
            expect(maxInFlight).toBe(4)
            expect(store.writes.flat()).toHaveLength(4)
        }
    )

    it('refills a slot as soon as it frees rather than waiting on the slowest image in flight', async () => {
        // Awaiting a whole group of scrubConcurrency before starting the next means one slow image
        // holds its group's other slots idle until it finishes, so throughput tracks the slowest image
        // in each group rather than the average one. On a spread-out scrub-time distribution that is
        // most of the sidecar's capacity. A sliding window costs one slot for a slow image, not all of
        // them, so every remaining image is already in flight before the slow one returns.
        const store = new FakeStore()
        let releaseSlow = (): void => {}
        const slow = new Promise<void>((resolve) => (releaseSlow = resolve))
        let started = 0
        const gatedClient = {
            scrub: (b: Buffer) => {
                started += 1
                return b.toString() === 'slow' ? slow.then(() => b) : Promise.resolve(b)
            },
        } as unknown as ScrubClient
        const batcher = new ImageBatcher(store as unknown as ImageShardStore, new FakeOffsets(), gatedClient, options)

        const batch: Message[] = [msg(0, 0, pt(1), Buffer.from('slow'))]
        for (let i = 1; i < 8; i++) {
            batch.push(msg(0, i, pt(1), Buffer.from(`img-${i}`)))
        }
        const running = batcher.handleBatch(batch)
        for (let tick = 0; tick < 20; tick++) {
            await new Promise((resolve) => setImmediate(resolve))
        }

        // All 8 are in flight while the slow one is still blocked; a barrier would stall at 4.
        expect(started).toBe(8)

        releaseSlow()
        await running
        expect(store.writes.flat()).toHaveLength(8)
    })

    it('never writes an image whose offset a slow predecessor is still holding back', async () => {
        // Completions arrive out of order, so a slow first image leaves later ones finished but not
        // retired. Writing those on a capacity flush persists bytes whose offsets cannot be stored
        // yet, and a batch failure then rewrites them under a fresh shard key: the same content in
        // two shards, and two index rows pointing at it. A flush must only ever cover what it can
        // also commit.
        const store = new FakeStore()
        const offsets = new FakeOffsets()
        let releaseSlow = (): void => {}
        const slow = new Promise<void>((resolve) => (releaseSlow = resolve))
        const gatedClient = {
            scrub: (b: Buffer) => (b.toString() === 'slow' ? slow.then(() => b) : Promise.resolve(b)),
        } as unknown as ScrubClient
        const batcher = new ImageBatcher(store as unknown as ImageShardStore, offsets, gatedClient, {
            ...options,
            maxImages: 2,
            scrubConcurrency: 4,
        })

        const batch: Message[] = [msg(0, 0, pt(1), Buffer.from('slow'))]
        for (let i = 1; i < 5; i++) {
            batch.push(msg(0, i, pt(1), Buffer.from(`img-${i}`)))
        }
        const running = batcher.handleBatch(batch)
        for (let tick = 0; tick < 20; tick++) {
            await new Promise((resolve) => setImmediate(resolve))
        }

        // The fast images are done but unretired, so nothing may be written or committed yet.
        expect(store.writes.flat()).toHaveLength(0)
        expect(offsets.received).toHaveLength(0)

        releaseSlow()
        await running
        await batcher.drain()

        expect(store.writes.flat()).toHaveLength(5)
        expect(offsets.received.at(-1)).toEqual([{ topic: 'session_replay_image_scrub', partition: 0, offset: 5 }])
    })

    it.each([
        ['an inline image', () => msg(0, 0, pt(1), Buffer.from('sprite'))],
        [
            'a URL image',
            () =>
                msg(
                    0,
                    0,
                    pt(1),
                    Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]),
                    urlRef(hashImageBytes(CONTENT_KEY, Buffer.from('https://example.com/image.png'))),
                    [{ 'content-type': Buffer.from('image/png') }]
                ),
        ],
    ])('rescrubs %s whose batch failed rather than pod-deduping the replay away', async (_name, message) => {
        // A ref is marked seen only once its image is buffered. Marking it at plan time, or inside
        // scrubOne, reads as a harmless simplification and silently drops the image instead: the
        // replay is pod-deduped, its offset advances, and nothing ever writes it. No error, no
        // counter, and every other test still green.
        let attempts = 0
        const flakyClient = {
            scrub: (b: Buffer) => {
                attempts += 1
                return attempts === 1 ? Promise.reject(new Error('sidecar down')) : Promise.resolve(b)
            },
        } as unknown as ScrubClient
        const store = new FakeStore()
        const batcher = new ImageBatcher(store as unknown as ImageShardStore, new FakeOffsets(), flakyClient, options)

        await expect(batcher.handleBatch([message()])).rejects.toThrow('sidecar down')
        await handleAndWrite(batcher, [message()])

        expect(store.writes.flat().length + store.urlWrites.length).toBe(1)
    })

    it('advances every partition past the duplicates it skipped, not just the one it scrubbed on', async () => {
        // The span walk is the one place that can commit an offset for a message it never processed,
        // and that failure is silent permanent loss. Both partitions here end on a duplicate, so the
        // trailing skips are only covered if the final recordOffsets spans the whole batch.
        const offsets = new FakeOffsets()
        const batcher = new ImageBatcher(new FakeStore() as unknown as ImageShardStore, offsets, scrubClient, options)
        const a = Buffer.from('a')
        const b = Buffer.from('b')

        await handleAndWrite(batcher, [
            msg(0, 100, pt(1), a),
            msg(1, 200, pt(1), b),
            msg(0, 101, pt(1), a),
            msg(1, 201, pt(1), b),
        ])

        expect(offsets.received.at(-1)).toEqual(
            expect.arrayContaining([
                { topic: 'session_replay_image_scrub', partition: 0, offset: 102 },
                { topic: 'session_replay_image_scrub', partition: 1, offset: 202 },
            ])
        )
    })

    it('leaves both partitions replayable when a chunk fails after a mid-batch hand-off', async () => {
        // A mid-batch hand-off commits offsets for the prefix it persisted, and everything after it has
        // to stay replayable. Multi-partition is where that can quietly go wrong, because each
        // partition carries its own maximum and only one of them appears in a given chunk's span.
        const store = new FakeStore()
        const offsets = new FakeOffsets()
        let scrubs = 0
        const failLateClient = {
            scrub: (b: Buffer) => {
                scrubs += 1
                return scrubs > 2 ? Promise.reject(new Error('sidecar down')) : Promise.resolve(b)
            },
        } as unknown as ScrubClient
        const batcher = new ImageBatcher(store as unknown as ImageShardStore, offsets, failLateClient, {
            ...options,
            maxImages: 2,
            scrubConcurrency: 2,
        })

        await expect(
            batcher.handleBatch([
                msg(0, 10, pt(1), Buffer.from('a')),
                msg(1, 20, pt(1), Buffer.from('b')),
                msg(0, 11, pt(1), Buffer.from('c')),
                msg(1, 21, pt(1), Buffer.from('d')),
            ])
        ).rejects.toThrow('sidecar down')
        await batcher.drain()

        expect(offsets.received).toHaveLength(1)
        expect(offsets.received[0]).toHaveLength(2)
        expect(offsets.received[0]).toEqual(
            expect.arrayContaining([
                { topic: 'session_replay_image_scrub', partition: 0, offset: 11 },
                { topic: 'session_replay_image_scrub', partition: 1, offset: 21 },
            ])
        )
    })

    it('stops re-sending an image the sidecar permanently rejected', async () => {
        // A 422/413 is a verdict on the content, so every later copy would earn the same rejection.
        // Without marking it, the most broken images are the ones that keep costing sidecar calls.
        let calls = 0
        const rejectingClient = {
            scrub: () => {
                calls += 1
                return Promise.resolve(null)
            },
        } as unknown as ScrubClient
        const batcher = new ImageBatcher(
            new FakeStore() as unknown as ImageShardStore,
            new FakeOffsets(),
            rejectingClient,
            options
        )
        const broken = Buffer.from('broken')
        const incSkipped = jest.spyOn(ImageScrubConsumerMetrics, 'incSkipped')

        await handleAndWrite(batcher, [msg(0, 0, pt(1), broken)])
        await handleAndWrite(batcher, [msg(0, 1, pt(1), broken)])

        expect(calls).toBe(1)
        expect(incSkipped).toHaveBeenCalledWith('sidecar_rejected')
    })

    it('dedupMaxRefs 0 disables the cross-batch cache but never intra-batch dedup', async () => {
        const store = new FakeStore()
        const batcher = new ImageBatcher(store as unknown as ImageShardStore, new FakeOffsets(), scrubClient, {
            ...options,
            dedupMaxRefs: 0,
        })

        const sprite = Buffer.from('sprite')
        await handleAndWrite(batcher, [msg(0, 0, pt(1), sprite)])
        await handleAndWrite(batcher, [msg(0, 1, pt(1), sprite)])
        expect(store.writes.flat()).toHaveLength(2)

        // Intra-batch dedup keeps no state between batches, so turning the cache off cannot reach it.
        await handleAndWrite(batcher, [msg(0, 2, pt(1), sprite), msg(0, 3, pt(1), sprite)])
        expect(store.writes.flat()).toHaveLength(3)
    })

    test.each([[0], [NaN], [-1]])('rejects scrubConcurrency %p at construction', (scrubConcurrency) => {
        // 0 spins the chunk loop forever; NaN skips it entirely yet still commits offsets,
        // silently dropping the whole batch — both must fail loudly at boot instead.
        expect(
            () =>
                new ImageBatcher(new FakeStore() as unknown as ImageShardStore, new FakeOffsets(), scrubClient, {
                    ...options,
                    scrubConcurrency,
                })
        ).toThrow('scrubConcurrency')
    })

    it('parks a poison image and lets the batch move past it', async () => {
        const store = new FakeStore()
        const offsets = new FakeOffsets()
        const parked: string[] = []
        const poisonClient = {
            scrub: (b: Buffer) =>
                b.toString() === 'poison'
                    ? Promise.reject(
                          new ScrubPoisoned('cannot process', {
                              reason: 'rejected',
                              lastError: 'sidecar responded 500',
                              attempts: 12,
                              waitedMs: 60_000,
                              elapsedMs: 120_000,
                              rejectedMs: 120_000,
                          })
                      )
                    : Promise.resolve(b),
        } as unknown as ScrubClient
        const batcher = new ImageBatcher(store as unknown as ImageShardStore, offsets, poisonClient, options, {
            park: (image) => Promise.resolve(void parked.push(image.ref)),
        })

        await handleAndWrite(batcher, [msg(0, 0, pt(1), Buffer.from('poison')), msg(0, 1, pt(1), Buffer.from('ok'))])

        expect(parked).toHaveLength(1)
        // The healthy image behind it still gets written, and the offset advances over both.
        expect(store.writes.flat()).toHaveLength(1)
        expect(offsets.received.flat().map((o) => o.offset)).toEqual([2])
    })

    it('preserves the replay count when re-parking, so round trips accumulate', async () => {
        // The cap that stops an image cycling between the two topics only binds if the count comes
        // back out with it. Dropped here, every replay run starts the image at zero and it can be
        // pushed at a sidecar that still cannot take it forever.
        const parked: Record<string, unknown>[] = []
        const poisonClient = {
            scrub: () =>
                Promise.reject(
                    new ScrubPoisoned('cannot process', {
                        reason: 'rejected',
                        lastError: 'sidecar responded 500',
                        attempts: 12,
                        waitedMs: 60_000,
                        elapsedMs: 120_000,
                        rejectedMs: 120_000,
                    })
                ),
        } as unknown as ScrubClient
        const batcher = new ImageBatcher(
            new FakeStore() as unknown as ImageShardStore,
            new FakeOffsets(),
            poisonClient,
            options,
            { park: (image) => Promise.resolve(void parked.push(image.detail)) }
        )

        const replayed = msg(0, 0, pt(1), Buffer.from('poison'))
        ;(replayed as unknown as { headers: unknown[] }).headers = [{ replayCount: Buffer.from('1') }]
        await handleAndWrite(batcher, [replayed])

        expect(parked[0].replayCount).toBe(1)
    })

    it('preserves URL transport headers when it parks the original compressed bytes', async () => {
        const parked: { headers: Record<string, string>; bytes: Buffer }[] = []
        const poisonClient = {
            scrub: () =>
                Promise.reject(
                    new ScrubPoisoned('cannot process', {
                        reason: 'rejected',
                        lastError: 'sidecar responded 500',
                        attempts: 12,
                        waitedMs: 60_000,
                        elapsedMs: 120_000,
                        rejectedMs: 120_000,
                    })
                ),
        } as unknown as ScrubClient
        const batcher = new ImageBatcher(
            new FakeStore() as unknown as ImageShardStore,
            new FakeOffsets(),
            poisonClient,
            options,
            {
                park: (image) => Promise.resolve(void parked.push({ headers: image.headers, bytes: image.bytes })),
            }
        )
        const png = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])
        const compressed = gzipSync(png)
        const ref = urlRef(hashImageBytes(CONTENT_KEY, Buffer.from('https://example.com/poison.png')))

        await handleAndWrite(batcher, [
            msg(0, 0, pt(1), compressed, ref, [
                { 'content-type': Buffer.from('image/png') },
                { 'content-encoding': Buffer.from('gzip') },
                { [CAPTURE_TIMESTAMP_HEADER]: Buffer.from(String(CAPTURED_AT)) },
            ]),
        ])

        expect(parked).toEqual([
            {
                bytes: compressed,
                headers: {
                    'content-type': 'image/png',
                    'content-encoding': 'gzip',
                    [CAPTURE_TIMESTAMP_HEADER]: String(CAPTURED_AT),
                },
            },
        ])
    })

    it('refuses to start when concurrency is too low for dead-lettering to be reachable', () => {
        // The poison gate can only ever count successes from slots running alongside the image it is
        // judging: the batch holding that image cannot finish, and the pod cannot poll for more work
        // until it does. At or below the threshold the gate is unreachable, so the first image the
        // sidecar cannot scrub stops the pod consuming for good. Failing at boot beats deadlocking
        // in traffic, where it presents as a pod that is Ready and quietly doing nothing.
        expect(
            () =>
                new ImageBatcher(
                    new FakeStore() as unknown as ImageShardStore,
                    new FakeOffsets(),
                    scrubClient,
                    { ...options, scrubConcurrency: 2 },
                    { park: () => Promise.resolve() }
                )
        ).toThrow('scrubConcurrency must exceed')
    })

    it('retries a failed park rather than failing the batch over it', async () => {
        // Ordering is the whole safety property here. Marking the ref or retiring the slot before the
        // bytes are durably parked would advance the offset over an image held nowhere at all.
        //
        // Letting the failure escape is worse still than the stall it would replace: the Kafka loop
        // exits the process on any batch error, so a dead-letter topic that is missing, on the wrong
        // cluster, or smaller than a source image would crash-loop every pod in the lane on the same
        // message. Retrying leaves the image where it was, which is what this lane did before a
        // dead-letter topic existed.
        const store = new FakeStore()
        const offsets = new FakeOffsets()
        const poisonClient = {
            scrub: () =>
                Promise.reject(
                    new ScrubPoisoned('cannot process', {
                        reason: 'rejected',
                        lastError: 'sidecar responded 500',
                        attempts: 12,
                        waitedMs: 60_000,
                        elapsedMs: 120_000,
                        rejectedMs: 120_000,
                    })
                ),
        } as unknown as ScrubClient
        let parkAttempts = 0
        const batcher = new ImageBatcher(store as unknown as ImageShardStore, offsets, poisonClient, options, {
            park: () => {
                parkAttempts += 1
                return parkAttempts < 3 ? Promise.reject(new Error('dlq produce failed')) : Promise.resolve()
            },
        })

        await expect(batcher.handleBatch([msg(0, 0, pt(1), Buffer.from('poison'))])).resolves.toBeUndefined()

        expect(parkAttempts).toBe(3)
        // The offset only moves once the bytes are somewhere, never while parking is still failing.
        expect(offsets.received.flat().map((o) => o.offset)).toEqual([1])
    })

    it('returns when stopped, so shutdown does not wait on an unresponsive sidecar', async () => {
        // disconnect() awaits the running batch, and the scrub client now waits on a busy sidecar
        // rather than giving up, so a batch against a sidecar that is down never returns on its own.
        // Without the interrupt a graceful stop runs to the termination grace period and is SIGKILLed.
        const store = new FakeStore()
        const offsets = new FakeOffsets()
        const hangingClient = {
            scrub: (_b: Buffer, signal: AbortSignal) =>
                new Promise<Buffer>((_resolve, reject) =>
                    signal.addEventListener('abort', () => reject(new Error('scrub batch aborted')), { once: true })
                ),
        } as unknown as ScrubClient
        const batcher = new ImageBatcher(store as unknown as ImageShardStore, offsets, hangingClient, options)
        const startBatch = jest.spyOn(ImageScrubConsumerMetrics, 'startBatch')
        const finishBatch = jest.spyOn(ImageScrubConsumerMetrics, 'finishBatch')

        const running = batcher.handleBatch([msg(0, 0, pt(1), Buffer.from('a'))])
        expect(startBatch).toHaveBeenCalledTimes(1)
        expect(finishBatch).not.toHaveBeenCalled()
        await batcher.stop()

        await expect(running).resolves.toBeUndefined()
        expect(finishBatch).toHaveBeenCalledTimes(1)
        // Nothing finished, so nothing may be committed over: the image replays under the next owner.
        expect(offsets.received.flat()).toEqual([])
        expect(store.writes).toHaveLength(0)
    })

    it('discards offsets for a partition a rebalance already revoked, rather than exiting', async () => {
        // librdkafka raises ERR__STATE for a partition we no longer hold. The shard is already on S3,
        // so the span simply rescrubs under its new owner. Propagating it exits the process, and a
        // pod exiting is itself what triggers the next rebalance.
        const store = new FakeStore()
        const revoked = new FakeOffsets()
        revoked.offsetsStore = () => {
            throw Object.assign(new Error('Local: Erroneous state'), { code: -172 })
        }
        const batcher = new ImageBatcher(store as unknown as ImageShardStore, revoked, scrubClient, options)

        await expect(batcher.handleBatch([msg(0, 0, pt(1), Buffer.from('a'))])).resolves.toBeUndefined()
        await batcher.drain()
        expect(store.writes).toHaveLength(1)
    })

    it('still fails when storing offsets fails for any other reason', async () => {
        const store = new FakeStore()
        const broken = new FakeOffsets()
        broken.offsetsStore = () => {
            throw Object.assign(new Error('Broker: Not coordinator'), { code: 16 })
        }
        const batcher = new ImageBatcher(store as unknown as ImageShardStore, broken, scrubClient, options)

        await batcher.handleBatch([msg(0, 0, pt(1), Buffer.from('a'))])
        await expect(batcher.drain()).rejects.toThrow('Not coordinator')
        await expect(batcher.handleBatch([msg(0, 1, pt(1), Buffer.from('b'))])).rejects.toThrow('Not coordinator')
    })

    it('writes the shards of one batch while it scrubs the next batch', async () => {
        // A batch's images spread over as many shard groups as team-months, and each group costs S3
        // round trips. A write that blocks its batch leaves the sidecar idle for the sum of them.
        const store = new FakeStore()
        let releaseWrite = (): void => {}
        const writeGate = new Promise<void>((resolve) => (releaseWrite = resolve))
        const writeShard = store.writeShard.bind(store)
        store.writeShard = async (images) => {
            await writeGate
            return writeShard(images)
        }
        const offsets = new FakeOffsets()
        const scrubbed: string[] = []
        const recordingClient = {
            scrub: (b: Buffer) => {
                scrubbed.push(b.toString())
                return Promise.resolve(b)
            },
        } as unknown as ScrubClient
        const batcher = new ImageBatcher(store as unknown as ImageShardStore, offsets, recordingClient, options)

        await batcher.handleBatch([msg(0, 0, pt(1), Buffer.from('first'))])
        let secondSettled = false
        const second = batcher.handleBatch([msg(0, 1, pt(1), Buffer.from('second'))]).then(() => {
            secondSettled = true
        })
        for (let tick = 0; tick < 20; tick++) {
            await new Promise((resolve) => setImmediate(resolve))
        }

        // Both batches scrubbed while the first write is still held open, and nothing is stored yet.
        // The second batch then waits at its hand-off, because one write is running and one is queued.
        expect(scrubbed).toEqual(['first', 'second'])
        expect(store.writes).toHaveLength(0)
        expect(offsets.received).toEqual([])
        expect(secondSettled).toBe(false)

        releaseWrite()
        await second
        await batcher.drain()

        expect(store.writes.flat()).toHaveLength(2)
        expect(offsets.received.flat().map((offset) => offset.offset)).toEqual([1, 2])
    })

    it('writes the shard groups of one hand-off concurrently', async () => {
        const store = new FakeStore()
        let active = 0
        let maximumActive = 0
        const writeShard = store.writeShard.bind(store)
        store.writeShard = async (images) => {
            active += 1
            maximumActive = Math.max(maximumActive, active)
            await new Promise((resolve) => setImmediate(resolve))
            active -= 1
            return writeShard(images)
        }
        const batcher = new ImageBatcher(store as unknown as ImageShardStore, new FakeOffsets(), scrubClient, options)

        await handleAndWrite(batcher, [msg(0, 0, pt(1), Buffer.from('a')), msg(0, 1, '42', Buffer.from('b'))])

        expect(store.writes).toHaveLength(2)
        expect(maximumActive).toBe(2)
    })

    it('waits on the oldest hand-off once one is writing and one is queued, so a stalled S3 bounds what a pod holds', async () => {
        const store = new FakeStore()
        let releaseWrite = (): void => {}
        const writeGate = new Promise<void>((resolve) => (releaseWrite = resolve))
        const writeShard = store.writeShard.bind(store)
        store.writeShard = async (images) => {
            await writeGate
            return writeShard(images)
        }
        const batcher = new ImageBatcher(store as unknown as ImageShardStore, new FakeOffsets(), scrubClient, options)

        await batcher.handleBatch([msg(0, 0, pt(1), Buffer.from('a'))])
        let secondSettled = false
        const second = batcher.handleBatch([msg(0, 1, pt(1), Buffer.from('b'))]).then(() => {
            secondSettled = true
        })
        for (let tick = 0; tick < 20; tick++) {
            await new Promise((resolve) => setImmediate(resolve))
        }
        expect(secondSettled).toBe(false)

        releaseWrite()
        await second
        await batcher.drain()
        expect(store.writes.flat()).toHaveLength(2)
    })

    it('drains the write lane before a failed batch is raised, so its offsets are stored while Kafka is still connected', async () => {
        // The Kafka loop disconnects the client as soon as a batch rejects, before shutdown reaches
        // stop(). A hand-off still writing at that point would find no client for its offsets and its
        // shards would be written again after the restart.
        const store = new FakeStore()
        let releaseWrite = (): void => {}
        const writeGate = new Promise<void>((resolve) => (releaseWrite = resolve))
        const writeShard = store.writeShard.bind(store)
        store.writeShard = async (images) => {
            await writeGate
            return writeShard(images)
        }
        const offsets = new FakeOffsets()
        const failingClient = {
            scrub: (b: Buffer) =>
                b.toString() === 'boom' ? Promise.reject(new Error('sidecar down')) : Promise.resolve(b),
        } as unknown as ScrubClient
        const batcher = new ImageBatcher(store as unknown as ImageShardStore, offsets, failingClient, options)

        await batcher.handleBatch([msg(0, 0, pt(1), Buffer.from('a'))])
        let outcome: unknown = 'pending'
        const failing = batcher.handleBatch([msg(0, 1, pt(1), Buffer.from('boom'))]).then(
            () => (outcome = 'resolved'),
            (error) => (outcome = error)
        )
        for (let tick = 0; tick < 20; tick++) {
            await new Promise((resolve) => setImmediate(resolve))
        }
        expect(outcome).toBe('pending')
        expect(offsets.received).toEqual([])

        releaseWrite()
        await failing
        expect(outcome).toBeInstanceOf(Error)
        expect(store.writes.flat()).toHaveLength(1)
        expect(offsets.received.flat().map((offset) => offset.offset)).toEqual([1])
    })

    it('accumulates images across batches until the flush interval, so shards stay batched', async () => {
        // A hand-off per poll would split one team-month across many small shards and multiply the
        // shard, index and lookup objects per image.
        const store = new FakeStore()
        const offsets = new FakeOffsets()
        const batcher = new ImageBatcher(store as unknown as ImageShardStore, offsets, scrubClient, {
            ...options,
            flushIntervalMs: 30_000,
        })

        await batcher.handleBatch([msg(0, 0, pt(1), Buffer.from('a'))], 0)
        await batcher.drain()
        expect(store.writes).toHaveLength(1)

        await batcher.handleBatch([msg(0, 1, pt(1), Buffer.from('b'))], 1_000)
        await batcher.handleBatch([msg(0, 2, pt(1), Buffer.from('c'))], 20_000)
        await batcher.drain()
        expect(store.writes).toHaveLength(1)
        expect(offsets.received.flat().map((offset) => offset.offset)).toEqual([1])

        await batcher.handleBatch([msg(0, 3, pt(1), Buffer.from('d'))], 30_000)
        await batcher.drain()
        expect(store.writes).toHaveLength(2)
        expect(store.writes[1]).toHaveLength(3)
        expect(offsets.received.flat().map((offset) => offset.offset)).toEqual([1, 4])
    })

    it('hands off the tail of a batch once the interval has passed, even after a capacity hand-off in the same batch', async () => {
        // The batch clock is read once per batch, so a capacity hand-off must not reset the interval
        // or the tail behind it would wait for a later batch and hold its offsets uncommitted.
        const store = new FakeStore()
        const offsets = new FakeOffsets()
        const batcher = new ImageBatcher(store as unknown as ImageShardStore, offsets, scrubClient, {
            ...options,
            flushIntervalMs: 30_000,
            maxImages: 2,
            scrubConcurrency: 1,
        })

        await batcher.handleBatch([msg(0, 0, pt(1), Buffer.from('a'))], 0)
        await batcher.drain()
        expect(store.writes).toHaveLength(1)

        const late = Array.from({ length: 5 }, (_, i) => msg(0, 1 + i, pt(1), Buffer.from(`late-${i}`)))
        await batcher.handleBatch(late, 40_000)
        await batcher.drain()

        expect(store.writes.flat()).toHaveLength(6)
        expect(offsets.received.at(-1)).toEqual([{ topic: 'session_replay_image_scrub', partition: 0, offset: 6 }])
    })

    it('stop() waits for the write lane, so finished images reach S3 and their offsets are stored', async () => {
        // disconnect() commits whatever offsets are stored when it runs, so a stop that returned
        // before the lane finished would leave the last written images to be scrubbed again.
        const store = new FakeStore()
        let releaseWrite = (): void => {}
        const writeGate = new Promise<void>((resolve) => (releaseWrite = resolve))
        const writeShard = store.writeShard.bind(store)
        store.writeShard = async (images) => {
            await writeGate
            return writeShard(images)
        }
        const offsets = new FakeOffsets()
        const batcher = new ImageBatcher(store as unknown as ImageShardStore, offsets, scrubClient, options)
        await batcher.handleBatch([msg(0, 0, pt(1), Buffer.from('a'))])

        let stopped = false
        const stopping = batcher.stop().then(() => {
            stopped = true
        })
        for (let tick = 0; tick < 20; tick++) {
            await new Promise((resolve) => setImmediate(resolve))
        }
        expect(stopped).toBe(false)

        releaseWrite()
        await stopping
        expect(store.writes.flat()).toHaveLength(1)
        expect(offsets.received.flat().map((offset) => offset.offset)).toEqual([1])
    })

    it('discards a hand-off queued behind one that found its partitions revoked', async () => {
        // The new owner rescrubs that span, so writing it here leaves a second shard under a random
        // key. The discarded refs are unmarked, or a replay on this pod would dedup them away unwritten.
        const store = new FakeStore()
        let releaseWrite = (): void => {}
        const writeGate = new Promise<void>((resolve) => (releaseWrite = resolve))
        const writeShard = store.writeShard.bind(store)
        store.writeShard = async (images) => {
            await writeGate
            return writeShard(images)
        }
        const revoked = new FakeOffsets()
        revoked.offsetsStore = () => {
            throw Object.assign(new Error('Local: Erroneous state'), { code: -172 })
        }
        const batcher = new ImageBatcher(store as unknown as ImageShardStore, revoked, scrubClient, options)

        await batcher.handleBatch([msg(0, 0, pt(1), Buffer.from('a'))])
        // The second batch queues its hand-off behind the gated first one and waits for it.
        const second = batcher.handleBatch([msg(0, 1, pt(1), Buffer.from('b'))])
        releaseWrite()
        await second
        await batcher.drain()
        expect(store.writes.flat()).toHaveLength(1)

        await handleAndWrite(batcher, [msg(0, 1, pt(1), Buffer.from('b'))])
        expect(store.writes.flat()).toHaveLength(2)
    })

    it('lets sibling object writes settle before a hand-off reports the first failure', async () => {
        // The lane's failure ends the process, and a write still in flight at that point leaves a
        // partial shard behind that nothing indexes.
        const store = new FakeStore()
        let releaseWrite = (): void => {}
        const writeGate = new Promise<void>((resolve) => (releaseWrite = resolve))
        let settledWrites = 0
        store.writeShard = async (images) => {
            if (images[0].teamId === '42') {
                throw new Error('s3 down')
            }
            await writeGate
            settledWrites += 1
            return { shard: 'shard', bytes: 0 }
        }
        const batcher = new ImageBatcher(store as unknown as ImageShardStore, new FakeOffsets(), scrubClient, options)
        await batcher.handleBatch([msg(0, 0, pt(1), Buffer.from('a')), msg(0, 1, '42', Buffer.from('b'))])

        let failed = false
        const drain = batcher.drain().catch(() => {
            failed = true
        })
        for (let tick = 0; tick < 20; tick++) {
            await new Promise((resolve) => setImmediate(resolve))
        }
        expect(failed).toBe(false)

        releaseWrite()
        await drain
        expect(failed).toBe(true)
        expect(settledWrites).toBe(1)
    })
})