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.

9.9 KB
Raw Blame
import { SkillCatalog } from '@/skills/skill-catalog'

import type { OmnibusSkillsCache } from './cache/OmnibusSkillsCache'
import type { RedisLike } from './cache/RedisCache'
import { hashBytes } from './cache/SharedBlobCache'
import { SkillArchiveCache, type SkillArchiveCacheOptions } from './cache/SkillArchiveCache'
import {
    skillArchiveEventsTotal,
    skillArchiveLastValidatedTimestampSeconds,
    skillCatalogAgeSeconds,
    skillCatalogLoadDurationSeconds,
    skillCatalogSkills,
} from './metrics'

const DEFAULT_POLL_INTERVAL_MS = 60_000
const DEFAULT_POLL_JITTER_MS = 15_000
const DEFAULT_WARMUP_TIMEOUT_MS = 60_000
const DEFAULT_WARMUP_RETRY_MS = 2_000

export interface SkillCatalogServiceOptions extends SkillArchiveCacheOptions {
    /** Base interval between polls of the shared version key. */
    pollIntervalMs?: number
    /** Random extra per pod, so a fleet does not poll in lockstep. */
    pollJitterMs?: number
    /** How long startup waits for a catalog before the pod serves without one. */
    warmupTimeoutMs?: number
    warmupRetryMs?: number
    omnibusSkills?: OmnibusSkillsCache
}

type LoadPhase = 'warmup' | 'poll'

class SkillArchiveIntegrityError extends Error {}

/**
 * Holds the parsed product skill catalog for this pod.
 *
 * Requests only ever read `getCatalog()` from memory. Startup blocks on the first
 * load, then `start()` runs a background poller that adopts a newer shared
 * archive and refreshes the shared copy when it is stale. A failed refresh or a
 * bad archive keeps the last catalog that parsed.
 */
export class SkillCatalogService {
    private readonly expectedArchiveSha: string | undefined
    private readonly archiveCache: SkillArchiveCache
    private readonly omnibusSkills: OmnibusSkillsCache | undefined
    private readonly pollIntervalMs: number
    private readonly pollJitterMs: number
    private readonly warmupTimeoutMs: number
    private readonly warmupRetryMs: number

    private catalog: SkillCatalog | undefined
    private archiveBytes: Uint8Array | undefined
    private archiveSha: string | undefined
    private omnibusBytes: Uint8Array | undefined
    private omnibusSha: string | undefined
    private loadedAt: number | undefined
    private timer: ReturnType<typeof setInterval> | undefined
    private polling = false

    constructor(redis: RedisLike, opts: SkillCatalogServiceOptions = {}) {
        const {
            expectedArchiveSha,
            pollIntervalMs,
            pollJitterMs,
            warmupTimeoutMs,
            warmupRetryMs,
            omnibusSkills,
            ...cacheOptions
        } = opts
        this.expectedArchiveSha = expectedArchiveSha
        this.omnibusSkills = omnibusSkills
        const validateArchive =
            cacheOptions.validateArchive ?? ((bytes: Uint8Array) => void SkillCatalog.fromZip(bytes))
        this.archiveCache = new SkillArchiveCache(redis, {
            ...cacheOptions,
            expectedArchiveSha,
            // Parse before the bytes can reach Redis: a release that does not parse
            // never becomes the fleet's shared copy.
            validateArchive: (bytes) => {
                this.verifyArchive(bytes)
                validateArchive(bytes)
            },
        })
        this.pollIntervalMs = pollIntervalMs ?? DEFAULT_POLL_INTERVAL_MS
        this.pollJitterMs = pollJitterMs ?? DEFAULT_POLL_JITTER_MS
        this.warmupTimeoutMs = warmupTimeoutMs ?? DEFAULT_WARMUP_TIMEOUT_MS
        this.warmupRetryMs = warmupRetryMs ?? DEFAULT_WARMUP_RETRY_MS
    }

    getCatalog(): SkillCatalog | undefined {
        return this.catalog
    }

    /**
     * Blocks boot until a catalog is in memory, retrying inside a fixed budget.
     * Past the budget the pod starts without skills rather than holding the whole
     * fleet unready during a source and Redis outage; the poller keeps trying.
     */
    async warmup(): Promise<void> {
        const deadline = Date.now() + this.warmupTimeoutMs
        for (;;) {
            try {
                const loaded = await this.archiveCache.loadOrRefresh()
                skillArchiveEventsTotal.inc({ phase: 'warmup', result: loaded.result })
                this.install(loaded.bytes, loaded.sha, 'warmup')
                await this.adoptOmnibusSkills('warmup')
                return
            } catch (error) {
                skillArchiveEventsTotal.inc({ phase: 'warmup', result: 'error' })
                if (error instanceof SkillArchiveIntegrityError) {
                    throw error
                }
                if (Date.now() + this.warmupRetryMs >= deadline) {
                    if (this.expectedArchiveSha) {
                        throw error
                    }
                    console.error('[SkillCatalogService] warmup failed; starting without product skills:', error)
                    return
                }
                console.error('[SkillCatalogService] warmup attempt failed; retrying:', error)
                await sleep(this.warmupRetryMs)
            } finally {
                skillArchiveLastValidatedTimestampSeconds.set((this.archiveCache.getLastValidatedAt() ?? 0) / 1000)
            }
        }
    }

    /** Starts the background poller. Idempotent; the timer never keeps the process alive. */
    start(): void {
        if (this.timer) {
            return
        }
        const interval = this.pollIntervalMs + Math.floor(Math.random() * this.pollJitterMs)
        this.timer = setInterval(() => void this.poll(), interval)
        this.timer.unref?.()
    }

    stop(): void {
        if (this.timer) {
            clearInterval(this.timer)
            this.timer = undefined
        }
    }

    /**
     * One poll: adopt a newer shared archive, then repair the shared copy if it is
     * stale or missing. Overlapping polls collapse into one.
     */
    async poll(): Promise<void> {
        if (this.polling) {
            return
        }
        this.polling = true
        try {
            await this.adoptSharedArchive()
            await this.adoptOmnibusSkills('poll')
            const refresh = await this.archiveCache.refreshIfStale()
            skillArchiveEventsTotal.inc({ phase: 'poll', result: refresh })
            if (refresh === 'downloaded') {
                await this.adoptSharedArchive()
            } else if (refresh === 'missing') {
                const loaded = await this.archiveCache.loadOrRefresh()
                skillArchiveEventsTotal.inc({ phase: 'poll', result: loaded.result })
                this.install(loaded.bytes, loaded.sha, 'poll')
            }
        } catch (error) {
            skillArchiveEventsTotal.inc({ phase: 'poll', result: 'error' })
            console.error('[SkillCatalogService] poll failed; keeping the current catalog:', error)
        } finally {
            this.polling = false
            skillArchiveLastValidatedTimestampSeconds.set((this.archiveCache.getLastValidatedAt() ?? 0) / 1000)
            if (this.loadedAt !== undefined) {
                skillCatalogAgeSeconds.set((Date.now() - this.loadedAt) / 1000)
            }
        }
    }

    private async adoptSharedArchive(): Promise<void> {
        const changed = await this.archiveCache.readIfChanged(this.archiveSha)
        if (!changed) {
            return
        }
        skillArchiveEventsTotal.inc({ phase: 'poll', result: 'reloaded' })
        this.install(changed.bytes, changed.sha, 'poll')
    }

    private verifyArchive(bytes: Uint8Array): void {
        if (this.expectedArchiveSha && hashBytes(bytes) !== this.expectedArchiveSha) {
            throw new SkillArchiveIntegrityError('Skill archive does not match the expected SHA-256')
        }
    }

    private async adoptOmnibusSkills(phase: LoadPhase): Promise<void> {
        // A pinned archive serves exactly its bytes, so no unpinned overlay joins it.
        if (!this.omnibusSkills || !this.archiveBytes || this.expectedArchiveSha) {
            return
        }
        try {
            const changed = await this.omnibusSkills.readIfChanged(this.omnibusSha)
            if (!changed) {
                return
            }
            this.omnibusBytes = changed.bytes
            this.omnibusSha = changed.sha
            this.installCatalog(this.archiveBytes, phase)
            skillArchiveEventsTotal.inc({ phase, result: 'omnibus_reloaded' })
        } catch (error) {
            skillArchiveEventsTotal.inc({ phase, result: 'omnibus_error' })
            console.error('[SkillCatalogService] omnibus skills failed to load; keeping the current catalog:', error)
        }
    }

    private install(bytes: Uint8Array, sha: string, phase: LoadPhase): void {
        this.verifyArchive(bytes)
        if (sha === this.archiveSha) {
            return
        }
        this.archiveBytes = bytes
        this.archiveSha = sha
        this.installCatalog(bytes, phase)
    }

    private installCatalog(bytes: Uint8Array, phase: LoadPhase): void {
        const start = Date.now()
        const catalog = this.buildCatalog(bytes)
        this.catalog = catalog
        this.loadedAt = Date.now()
        skillCatalogLoadDurationSeconds.observe({ phase }, (this.loadedAt - start) / 1000)
        skillCatalogSkills.set(catalog.size)
        skillCatalogAgeSeconds.set(0)
        console.info(
            `[SkillCatalogService] installed ${catalog.size} skills (${phase}, sha ${this.archiveSha?.slice(0, 12)})`
        )
    }

    /** The product archive must always install, so a failing overlay degrades to product skills only. */
    private buildCatalog(bytes: Uint8Array): SkillCatalog {
        if (this.omnibusBytes) {
            try {
                return SkillCatalog.fromZip(bytes, this.omnibusBytes)
            } catch (error) {
                console.error(
                    '[SkillCatalogService] omnibus skills failed to overlay; serving product skills only:',
                    error
                )
            }
        }
        return SkillCatalog.fromZip(bytes)
    }
}

function sleep(ms: number): Promise<void> {
    return new Promise((resolve) => setTimeout(resolve, ms))
}