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.

30.0 KB
Raw Blame
import { AppMetricsOutput, HogInvocationResultsOutput, LogEntriesOutput } from '~/common/outputs'
import { IngestionOutputs } from '~/common/outputs/ingestion-outputs'
import { KafkaProducerRegistry } from '~/common/outputs/kafka-producer-registry'
import { PersonReadRepository } from '~/common/persons/repositories/person-repository'
import { RedisV2, createRedisV2PoolFromConfig } from '~/common/redis/redis-v2'
import { UsageIngestionConfig, createUsageIngestionClient, usageReportTeamMatcher } from '~/common/usage-ingestion'
import { PostgresRouter } from '~/common/utils/db/postgres'
import { getRedisHost } from '~/common/utils/db/redis'
import { logger } from '~/common/utils/logger'
import { PubSub } from '~/common/utils/pubsub'
import { TeamManager } from '~/common/utils/team-manager'
import { InternalLogsProducer, InternalLogsProducerConfig } from '~/logs/internal-logs-producer'

import type { CommonConfig } from '../common/config'
import { InternalCaptureService } from '../common/services/internal-capture'
import type { CdpConfig } from './config'
import { CdpEventsDlqOutput, WarehouseSourceWebhooksOutput } from './outputs/outputs'
import { CdpProducerName } from './outputs/producers'
import { createCdpOutputsRegistry } from './outputs/registry'
import { CapturedEventsService } from './services/captured-events/captured-events.service'
import { CohortMembershipRepository } from './services/cohorts/cohort-membership-repository'
import { PostgresCohortMembershipRepository } from './services/cohorts/postgres-cohort-membership-repository'
import { ConversionWatchersService } from './services/conversion-watchers/conversion-watchers.service'
import { HogExecutorAsyncService } from './services/hog-executor-async.service'
import { HogExecutorService } from './services/hog-executor.service'
import { HogInputsService } from './services/hog-inputs.service'
import { HogFlowDuplicateObserverService } from './services/hogflows/hogflow-duplicate-observer.service'
import { HogFlowExecutorService } from './services/hogflows/hogflow-executor.service'
import { HogFlowFunctionsService } from './services/hogflows/hogflow-functions.service'
import { HogFlowManagerService } from './services/hogflows/hogflow-manager.service'
import { InvocationResultsService } from './services/invocation-results.service'
import { HogFunctionManagerService } from './services/managers/hog-function-manager.service'
import { HogFunctionTemplateManagerService } from './services/managers/hog-function-template-manager.service'
import { IntegrationManagerService } from './services/managers/integration-manager.service'
import { PersonsManagerService } from './services/managers/persons-manager.service'
import { RecipientsManagerService } from './services/managers/recipients-manager.service'
import { TeamWorkflowsConfigService } from './services/managers/team-workflows-config.service'
import {
    EMAIL_ATTACHMENTS_MAX_CONNECTIONS,
    EmailAttachmentsService,
} from './services/messaging/email-attachments.service'
import { EmailStatusService } from './services/messaging/email-status.service'
import { EmailSuppressionService } from './services/messaging/email-suppression.service'
import { EmailValidationService } from './services/messaging/email-validation.service'
import { EmailService, parseTeamEmailCapMode, parseTierCaps } from './services/messaging/email.service'
import { EmailTrackingCodeSigner } from './services/messaging/helpers/tracking-code'
import { MessageAssetsService } from './services/messaging/message-assets.service'
import { PushNotificationService } from './services/messaging/push-notification.service'
import { RecipientPreferencesService } from './services/messaging/recipient-preferences.service'
import { RecipientTokensService } from './services/messaging/recipient-tokens.service'
import {
    HogFunctionMonitoringService,
    parseInternalLogsSources,
} from './services/monitoring/hog-function-monitoring.service'
import { HogInvocationResultsService } from './services/monitoring/hog-invocation-results.service'
import { HogWatcherService } from './services/monitoring/hog-watcher.service'
import { WorkflowsActivationReporter } from './services/monitoring/workflows-activation-reporter'
import { NativeDestinationExecutorService } from './services/native-destination-executor.service'
import { RateLimiterService } from './services/rate-limiter/rate-limiter.service'
import { SegmentDestinationExecutorService } from './services/segment-destination-executor.service'
import { CdpUsageReporterService } from './services/usage/cdp-usage-reporter.service'
import { WarehouseWebhooksService } from './services/warehouse/warehouse-webhooks.service'
import { MAX_FETCH_TIMEOUT_MS, cdpTrackedFetch } from './utils/cdp-fetch'
import { configureValkeyReads } from './utils/dual-store'
import { EncryptedFields } from './utils/encryption-utils'
import { PosthogJwtAudience } from './utils/jwt-utils'
import { ScopedServiceJwt } from './utils/scoped-service-jwt'

/** Union of every output name resolved by `createCdpOutputsRegistry()`. */
export type CdpOutput =
    | AppMetricsOutput
    | LogEntriesOutput
    | HogInvocationResultsOutput
    | WarehouseSourceWebhooksOutput
    | CdpEventsDlqOutput

export type CdpOutputs = IngestionOutputs<CdpOutput>

export interface CdpValkeyShadowPools {
    writer: RedisV2
    reader: RedisV2
}

export interface CdpCoreServices {
    redis: RedisV2
    /**
     * Valkey pools used for dual-write/read. Consumers that build their own
     * redis-backed services (e.g. CdpEventsConsumer's HogRateLimiterService) read this
     * to construct the Valkey-bound twin.
     */
    valkeyShadow: CdpValkeyShadowPools
    hogFunctionManager: HogFunctionManagerService
    hogFlowManager: HogFlowManagerService
    hogWatcher: HogWatcherService
    /**
     * Valkey-bound twin of `hogWatcher`. Use at call sites alongside `hogWatcher` via
     * `dualRead` / `dualWrite`, which decide which of the two results the caller sees.
     * Constructed with `sendEvents: false` so it never emits duplicate billable team events.
     */
    hogWatcherMirror: HogWatcherService
    /** Hog execution with async functions (fetch, email, push). Its `hogExecutor` is the synchronous core. */
    hogExecutorAsync: HogExecutorAsyncService
    /** Rebuilds the templated/resolved input bundle for a hog function — used by the rerun path to re-derive `inputs` after they're stripped from the persisted payload. */
    hogInputsService: HogInputsService
    hogFunctionTemplateManager: HogFunctionTemplateManagerService
    hogFlowFunctionsService: HogFlowFunctionsService
    personsManager: PersonsManagerService
    recipientsManager: RecipientsManagerService
    recipientPreferencesService: RecipientPreferencesService
    emailSuppressionService: EmailSuppressionService
    emailValidationService: EmailValidationService
    teamWorkflowsConfigService: TeamWorkflowsConfigService
    /** Point lookups for realtime/behavioral cohort membership (behavioral cohorts DB). */
    cohortMembershipRepository: CohortMembershipRepository
    hogFlowExecutor: HogFlowExecutorService
    hogFunctionMonitoringService: HogFunctionMonitoringService
    workflowsActivationReporter: WorkflowsActivationReporter
    cdpUsageReporter: CdpUsageReporterService
    capturedEventsService: CapturedEventsService
    /** Per-invocation lifecycle row producer for the new runs/invocations UI + rerun path. */
    hogInvocationResultsService: HogInvocationResultsService
    /** Fans `CyclotronJobInvocationResult` batches across monitoring / warehouse / captured-events. */
    invocationResultsService: InvocationResultsService
    nativeDestinationExecutorService: NativeDestinationExecutorService
    segmentDestinationExecutorService: SegmentDestinationExecutorService
    recipientTokensService: RecipientTokensService
    /** Resolved outputs shared across every CDP service/consumer. */
    outputs: CdpOutputs
    emailService: EmailService
    /** Buffers rendered-email asset rows and bulk-flushes them at the batch boundary. */
    messageAssetsService: MessageAssetsService
    /** Buffers transactional email status rows. `invocationResultsService.flush()` produces them. */
    emailStatusService: EmailStatusService
    emailAttachmentsService: EmailAttachmentsService
}

export type CdpCoreServicesConfig = Pick<
    CommonConfig,
    | 'REDIS_URL'
    | 'REDIS_POOL_MIN_SIZE'
    | 'REDIS_POOL_MAX_SIZE'
    | 'ENCRYPTION_SALT_KEYS'
    | 'SITE_URL'
    | 'INTERNAL_API_BASE_URL'
> &
    UsageIngestionConfig &
    Pick<
        CdpConfig,
        | 'CDP_REDIS_HOST'
        | 'CDP_REDIS_PORT'
        | 'CDP_REDIS_PASSWORD'
        | 'CDP_REDIS_READER_HOST'
        | 'CDP_REDIS_READER_PORT'
        | 'CDP_VALKEY_HOST'
        | 'CDP_VALKEY_PORT'
        | 'CDP_VALKEY_PASSWORD'
        | 'CDP_VALKEY_READER_HOST'
        | 'CDP_VALKEY_READER_PORT'
        | 'CDP_VALKEY_TLS'
        | 'CDP_VALKEY_READ_FEATURES'
        | 'CYCLOTRON_NODE_DATABASE_URL'
        | 'CYCLOTRON_NODE_MAX_CONNECTIONS'
        | 'CDP_WATCHER_HOG_COST_TIMING_LOWER_MS'
        | 'CDP_WATCHER_HOG_COST_TIMING_UPPER_MS'
        | 'CDP_WATCHER_HOG_COST_TIMING'
        | 'CDP_WATCHER_ASYNC_COST_TIMING_LOWER_MS'
        | 'CDP_WATCHER_ASYNC_COST_TIMING_UPPER_MS'
        | 'CDP_WATCHER_ASYNC_COST_TIMING'
        | 'CDP_WATCHER_SEND_EVENTS'
        | 'CDP_WATCHER_BUCKET_SIZE'
        | 'CDP_WATCHER_REFILL_RATE'
        | 'CDP_WATCHER_TTL'
        | 'CDP_WATCHER_AUTOMATICALLY_DISABLE_FUNCTIONS'
        | 'CDP_WATCHER_THRESHOLD_DEGRADED'
        | 'CDP_WATCHER_STATE_LOCK_TTL'
        | 'CDP_WATCHER_OBSERVE_RESULTS_BUFFER_TIME_MS'
        | 'CDP_WATCHER_OBSERVE_RESULTS_BUFFER_MAX_RESULTS'
        | 'CDP_HOGFLOW_AWAITED_STEPS_ENABLED'
        | 'CDP_INTERNAL_LOGS_SOURCES'
        | 'SES_ACCESS_KEY_ID'
        | 'SES_SECRET_ACCESS_KEY'
        | 'SES_REGION'
        | 'SES_ENDPOINT'
        | 'SES_TRACKED_CONFIGURATION_SET'
        | 'SES_UNTRACKED_CONFIGURATION_SET'
        | 'EMAIL_SUPPRESSION_TRANSIENT_BOUNCE_THRESHOLD'
        | 'EMAIL_SUPPRESSION_GLOBAL_HARD_BOUNCE_ENFORCE'
        | 'EMAIL_TEAM_SENDING_CAP_MODE'
        | 'EMAIL_TEAM_SENDING_CAP_DAILY_BY_TIER'
        | 'CDP_EMAIL_UTM_LINK_SCANNER_TEAMS'
        | 'CDP_GOOGLE_ADWORDS_DEVELOPER_TOKEN'
        | 'CONVERSATIONS_TICKETS_JWT_SECRET'
        | 'CUSTOMER_ANALYTICS_ACCOUNTS_JWT_SECRET'
        | 'CDP_FETCH_RETRIES'
        | 'CDP_FETCH_BACKOFF_BASE_MS'
        | 'CDP_FETCH_BACKOFF_MAX_MS'
        | 'CDP_EMAIL_TRACKING_URL'
        | 'HOG_FUNCTION_MONITORING_APP_METRICS_TOPIC'
        | 'HOG_FUNCTION_MONITORING_APP_METRICS_PRODUCER'
        | 'HOG_FUNCTION_MONITORING_LOG_ENTRIES_TOPIC'
        | 'HOG_FUNCTION_MONITORING_LOG_ENTRIES_PRODUCER'
        | 'HOG_INVOCATION_RESULTS_TOPIC'
        | 'HOG_INVOCATION_RESULTS_PRODUCER'
        | 'HOG_INVOCATION_RESULTS_ENABLED'
        | 'MESSAGE_ASSETS_TOPIC'
        | 'MESSAGE_ASSETS_PRODUCER'
        | 'EMAIL_STATUS_TOPIC'
        | 'EMAIL_STATUS_PRODUCER'
        | 'EMAIL_STATUS_ENABLED'
        | 'CDP_WAREHOUSE_SOURCE_WEBHOOKS_TOPIC'
        | 'CDP_WAREHOUSE_SOURCE_WEBHOOKS_PRODUCER'
        | 'CDP_DLQ_ENABLED'
        | 'CDP_DLQ_BATCH_FAIL_RATIO'
        | 'CDP_EVENTS_DLQ_TOPIC'
        | 'CDP_EVENTS_DLQ_PRODUCER'
    > &
    InternalLogsProducerConfig

export interface CdpCoreServicesDeps {
    postgres: PostgresRouter
    pubSub: PubSub
    encryptedFields: EncryptedFields
    teamManager: TeamManager
    personRepository: PersonReadRepository
    integrationManager: IntegrationManagerService
    /** Registry of producers backing the CDP outputs (DEFAULT / MSK / Warpstream-ingestion / Warehouse). */
    cdpProducerRegistry: KafkaProducerRegistry<CdpProducerName>
    internalCaptureService: InternalCaptureService
    /**
     * SES Valkey pool shared with the SES rate limiter, opened only on pods whose
     * capabilities actually execute email actions (hogflow/email cyclotron workers)
     * and on cdp-api, which checks a transactional email's recipient against the
     * MX-verdict cache. `null` on every other CDP consumer so idle pods don't hold
     * open connections against the SES Valkey instance.
     */
    emailValidationValkey: RedisV2 | null
    /**
     * Dedicated frequency cap Valkey pool, opened only on the same pods as `emailValidationValkey`.
     * `null` elsewhere, and when no host is configured, which turns the cap off.
     */
    frequencyCapValkey: RedisV2 | null
}

/**
 * Creates a Redis reader pool, using a dedicated reader host if configured,
 * otherwise falling back to the writer pool.
 */
export function createCdpReaderRedisPool(
    config: Pick<
        CdpCoreServicesConfig,
        | 'CDP_REDIS_HOST'
        | 'CDP_REDIS_PORT'
        | 'CDP_REDIS_PASSWORD'
        | 'CDP_REDIS_READER_HOST'
        | 'CDP_REDIS_READER_PORT'
        | 'REDIS_URL'
        | 'REDIS_POOL_MIN_SIZE'
        | 'REDIS_POOL_MAX_SIZE'
    >,
    writerPool: RedisV2,
    name: string
): RedisV2 {
    if (config.CDP_REDIS_READER_HOST) {
        logger.info(
            '🔌',
            `[${name}] writer=${config.CDP_REDIS_HOST}:${config.CDP_REDIS_PORT} reader=${config.CDP_REDIS_READER_HOST}:${config.CDP_REDIS_READER_PORT}`
        )
        const readerPool = createRedisV2PoolFromConfig({
            connection: {
                url: config.CDP_REDIS_READER_HOST,
                options: { port: config.CDP_REDIS_READER_PORT, password: config.CDP_REDIS_PASSWORD },
                name: `${name}-reader`,
            },
            poolMinSize: config.REDIS_POOL_MIN_SIZE,
            poolMaxSize: config.REDIS_POOL_MAX_SIZE,
        })

        // Non-blocking startup health check — surfaces misconfig immediately in logs
        void readerPool
            .useClient({ name: 'startup-ping', timeout: 5000 }, (client) => client.ping())
            .catch((err) => {
                logger.error(
                    '🔌',
                    `[${name}] reader at ${config.CDP_REDIS_READER_HOST}:${config.CDP_REDIS_READER_PORT} failed startup health check — reads will fall back to writer`,
                    { err }
                )
            })

        return readerPool
    }

    const sanitizedWriter = config.CDP_REDIS_HOST || getRedisHost(config.REDIS_URL)
    logger.info('🔌', `[${name}] writer=${sanitizedWriter}:${config.CDP_REDIS_PORT} reader=<falling back to writer>`)
    return writerPool
}

/**
 * Creates writer + reader pools for the shadow Valkey instance used in dual-write/read mode.
 * Throws when CDP_VALKEY_HOST is unset: a CDP process that cannot reach Valkey is
 * misconfigured, and failing at startup surfaces that here rather than as mirror warn logs
 * from every consumer.
 *
 * The reader falls back to the writer pool when CDP_VALKEY_READER_HOST is unset.
 */
export function createCdpValkeyShadowPools(
    config: Pick<
        CdpCoreServicesConfig,
        | 'CDP_VALKEY_HOST'
        | 'CDP_VALKEY_PORT'
        | 'CDP_VALKEY_PASSWORD'
        | 'CDP_VALKEY_READER_HOST'
        | 'CDP_VALKEY_READER_PORT'
        | 'CDP_VALKEY_TLS'
        | 'REDIS_POOL_MIN_SIZE'
        | 'REDIS_POOL_MAX_SIZE'
    >,
    name: string
): CdpValkeyShadowPools {
    if (!config.CDP_VALKEY_HOST) {
        throw new Error(
            `[${name}] CDP_VALKEY_HOST is required but unset. Every CDP process dual-writes to Valkey; set CDP_VALKEY_HOST (and CDP_VALKEY_PORT, CDP_VALKEY_TLS as needed) for this deployment.`
        )
    }

    logger.info(
        '🪞',
        `[${name}] shadow valkey writer=${config.CDP_VALKEY_HOST}:${config.CDP_VALKEY_PORT} reader=${config.CDP_VALKEY_READER_HOST || '<falling back to writer>'}`
    )

    // commandTimeout aborts in-flight commands at the ioredis protocol level, so a slow Valkey
    // doesn't tie up a pool client until the kernel TCP timeout. Pair this with the race-timeout
    // in dual-store's secondary path (which only stops awaiting); together they prevent leaks
    // on bad Valkey health.
    const shadowCommandTimeoutMs = 1000

    const tls = config.CDP_VALKEY_TLS ? {} : undefined

    const writer = createRedisV2PoolFromConfig({
        connection: {
            url: config.CDP_VALKEY_HOST,
            options: {
                port: config.CDP_VALKEY_PORT,
                password: config.CDP_VALKEY_PASSWORD,
                commandTimeout: shadowCommandTimeoutMs,
                tls,
            },
            name: `${name}-shadow`,
        },
        poolMinSize: config.REDIS_POOL_MIN_SIZE,
        poolMaxSize: config.REDIS_POOL_MAX_SIZE,
    })

    // The config is required, but reachability is not fatal: features not named in
    // CDP_VALKEY_READ_FEATURES still read from Redis, so an unreachable Valkey must not take
    // CDP down at boot. Features that have moved over surface it as failing reads instead.
    void writer
        .useClient({ name: 'startup-ping', timeout: 5000 }, (client) => client.ping())
        .catch((err) => {
            logger.error(
                '🪞',
                `[${name}] shadow writer at ${config.CDP_VALKEY_HOST}:${config.CDP_VALKEY_PORT} failed startup health check — valkey ops will surface as "[mirror:*] failed" warn logs from dual-store`,
                { err }
            )
        })

    let reader: RedisV2 = writer
    if (config.CDP_VALKEY_READER_HOST) {
        reader = createRedisV2PoolFromConfig({
            connection: {
                url: config.CDP_VALKEY_READER_HOST,
                options: {
                    port: config.CDP_VALKEY_READER_PORT,
                    password: config.CDP_VALKEY_PASSWORD,
                    commandTimeout: shadowCommandTimeoutMs,
                    tls,
                },
                name: `${name}-shadow-reader`,
            },
            poolMinSize: config.REDIS_POOL_MIN_SIZE,
            poolMaxSize: config.REDIS_POOL_MAX_SIZE,
        })

        void reader
            .useClient({ name: 'startup-ping', timeout: 5000 }, (client) => client.ping())
            .catch((err) => {
                logger.error(
                    '🪞',
                    `[${name}] shadow reader at ${config.CDP_VALKEY_READER_HOST}:${config.CDP_VALKEY_READER_PORT} failed startup health check`,
                    { err }
                )
            })
    }

    return { writer, reader }
}

export function createCdpCoreServices(
    config: CdpCoreServicesConfig,
    deps: CdpCoreServicesDeps,
    redisName = 'cdp-redis'
): CdpCoreServices {
    const redis = createRedisV2PoolFromConfig({
        connection: config.CDP_REDIS_HOST
            ? {
                  url: config.CDP_REDIS_HOST,
                  options: { port: config.CDP_REDIS_PORT, password: config.CDP_REDIS_PASSWORD },
                  name: redisName,
              }
            : { url: config.REDIS_URL, name: `${redisName}-fallback` },
        poolMinSize: config.REDIS_POOL_MIN_SIZE,
        poolMaxSize: config.REDIS_POOL_MAX_SIZE,
    })

    const redisReader = createCdpReaderRedisPool(config, redis, redisName)
    const valkeyShadow = createCdpValkeyShadowPools(config, redisName)

    // Every CDP process funnels through here, so this is the one place the per-feature read
    // source has to be applied for `dualRead` / `dualWrite` to see it.
    configureValkeyReads(config.CDP_VALKEY_READ_FEATURES)

    const hogFunctionManager = new HogFunctionManagerService(deps.postgres, deps.pubSub, deps.encryptedFields)
    const hogFlowManager = new HogFlowManagerService(deps.postgres, deps.pubSub, deps.encryptedFields)

    const hogWatcherConfig = {
        hogCostTimingLowerMs: config.CDP_WATCHER_HOG_COST_TIMING_LOWER_MS,
        hogCostTimingUpperMs: config.CDP_WATCHER_HOG_COST_TIMING_UPPER_MS,
        hogCostTiming: config.CDP_WATCHER_HOG_COST_TIMING,
        asyncCostTimingLowerMs: config.CDP_WATCHER_ASYNC_COST_TIMING_LOWER_MS,
        asyncCostTimingUpperMs: config.CDP_WATCHER_ASYNC_COST_TIMING_UPPER_MS,
        asyncCostTiming: config.CDP_WATCHER_ASYNC_COST_TIMING,
        sendEvents: config.CDP_WATCHER_SEND_EVENTS,
        bucketSize: config.CDP_WATCHER_BUCKET_SIZE,
        refillRate: config.CDP_WATCHER_REFILL_RATE,
        ttl: config.CDP_WATCHER_TTL,
        automaticallyDisableFunctions: config.CDP_WATCHER_AUTOMATICALLY_DISABLE_FUNCTIONS,
        thresholdDegraded: config.CDP_WATCHER_THRESHOLD_DEGRADED,
        stateLockTtl: config.CDP_WATCHER_STATE_LOCK_TTL,
        observeResultsBufferTimeMs: config.CDP_WATCHER_OBSERVE_RESULTS_BUFFER_TIME_MS,
        observeResultsBufferMaxResults: config.CDP_WATCHER_OBSERVE_RESULTS_BUFFER_MAX_RESULTS,
    }

    const hogWatcher = new HogWatcherService(deps.teamManager, hogWatcherConfig, redis, redisReader)

    // Valkey-bound twin of the watcher. `sendEvents: false` so it never emits duplicate
    // billable team events on state transitions; the Prom counter
    // `cdp_hog_function_state_change` may double-emit when both pools detect the same
    // transition — rare, accepted during dual-write mode.
    const hogWatcherMirror = new HogWatcherService(
        deps.teamManager,
        { ...hogWatcherConfig, sendEvents: false },
        valkeyShadow.writer,
        valkeyShadow.reader
    )

    const trackingCodeSigner = new EmailTrackingCodeSigner(config.ENCRYPTION_SALT_KEYS, config.CDP_EMAIL_TRACKING_URL)
    const teamWorkflowsConfigService = new TeamWorkflowsConfigService(deps.postgres, deps.pubSub)
    const outputs = createCdpOutputsRegistry().build(deps.cdpProducerRegistry, config)
    const messageAssetsService = new MessageAssetsService(outputs)
    const emailStatusService = new EmailStatusService(outputs, config)
    // Constructed here (rather than below with the other messaging services) so it can be threaded
    // into EmailService — the pre-send suppression check lives there so every send path shares one
    // choke point regardless of whether the invocation came from a workflow action or a hog function.
    const emailSuppressionService = new EmailSuppressionService(deps.postgres, {
        transientBounceThreshold: config.EMAIL_SUPPRESSION_TRANSIENT_BOUNCE_THRESHOLD,
        globalHardBounceEnforce: config.EMAIL_SUPPRESSION_GLOBAL_HARD_BOUNCE_ENFORCE,
    })
    const recipientsManager = new RecipientsManagerService(deps.postgres)
    const personsManager = new PersonsManagerService(deps.teamManager, deps.personRepository, config.SITE_URL)
    const workflowsActivationReporter = new WorkflowsActivationReporter(deps.teamManager)
    // Per-workflow send pacing rides the SES Valkey pool, which is only populated on pods that
    // execute email actions — exactly where the limit is enforced. Null elsewhere disables it.
    // cdp-api also gets the pool, but its sends are all test sends, which skip both limits.
    const workflowEmailRateLimiter = deps.emailValidationValkey
        ? new RateLimiterService(deps.emailValidationValkey, { name: 'workflow-email' })
        : null
    // Separate limiter instance from the per-workflow one so the two show up under different
    // `limiter` labels on the shared claim metrics. Same Valkey pool, different bucket keys.
    const teamEmailRateLimiter = deps.emailValidationValkey
        ? new RateLimiterService(deps.emailValidationValkey, { name: 'team-email' })
        : null
    const emailAttachmentsService = new EmailAttachmentsService(
        config.CYCLOTRON_NODE_DATABASE_URL,
        EMAIL_ATTACHMENTS_MAX_CONNECTIONS
    )
    const emailService = new EmailService(
        {
            sesAccessKeyId: config.SES_ACCESS_KEY_ID,
            sesSecretAccessKey: config.SES_SECRET_ACCESS_KEY,
            sesRegion: config.SES_REGION,
            sesEndpoint: config.SES_ENDPOINT,
            sesTrackedConfigurationSet: config.SES_TRACKED_CONFIGURATION_SET,
            sesUntrackedConfigurationSet: config.SES_UNTRACKED_CONFIGURATION_SET,
            teamEmailCapMode: parseTeamEmailCapMode(config.EMAIL_TEAM_SENDING_CAP_MODE),
            teamEmailTierDailyCaps: parseTierCaps(config.EMAIL_TEAM_SENDING_CAP_DAILY_BY_TIER),
            utmLinkScannerTeams: config.CDP_EMAIL_UTM_LINK_SCANNER_TEAMS,
        },
        deps.integrationManager,
        teamWorkflowsConfigService,
        config.ENCRYPTION_SALT_KEYS,
        config.SITE_URL,
        trackingCodeSigner,
        emailSuppressionService,
        recipientsManager,
        emailStatusService,
        workflowsActivationReporter,
        messageAssetsService,
        workflowEmailRateLimiter,
        teamEmailRateLimiter,
        emailAttachmentsService
    )
    const recipientTokensService = new RecipientTokensService(config.ENCRYPTION_SALT_KEYS, config.SITE_URL)
    const hogInputsService = new HogInputsService(deps.integrationManager, recipientTokensService, deps.encryptedFields)
    const pushNotificationService = new PushNotificationService(
        deps.integrationManager,
        deps.encryptedFields,
        {
            trackedFetch: cdpTrackedFetch,
            maxFetchTimeoutMs: MAX_FETCH_TIMEOUT_MS,
            maxRetries: config.CDP_FETCH_RETRIES,
            backoffBaseMs: config.CDP_FETCH_BACKOFF_BASE_MS,
            backoffMaxMs: config.CDP_FETCH_BACKOFF_MAX_MS,
        },
        // Valkey-only: push is pre-release, so it moved over whole rather than dual-writing.
        valkeyShadow.writer,
        personsManager,
        messageAssetsService
    )

    const hogExecutorAsync = new HogExecutorAsyncService(
        new HogExecutorService({ executionTimeoutMs: config.CDP_WATCHER_HOG_COST_TIMING_UPPER_MS }, hogInputsService),
        {
            googleAdwordsDeveloperToken: config.CDP_GOOGLE_ADWORDS_DEVELOPER_TOKEN,
            fetchRetries: config.CDP_FETCH_RETRIES,
            fetchBackoffBaseMs: config.CDP_FETCH_BACKOFF_BASE_MS,
            fetchBackoffMaxMs: config.CDP_FETCH_BACKOFF_MAX_MS,
            siteUrl: config.SITE_URL,
            internalApiBaseUrl: config.INTERNAL_API_BASE_URL,
        },
        {
            teamManager: deps.teamManager,
            conversationsTicketsJwt: new ScopedServiceJwt(
                PosthogJwtAudience.CONVERSATIONS_TICKETS,
                config.CONVERSATIONS_TICKETS_JWT_SECRET
            ),
            customerAnalyticsAccountsJwt: new ScopedServiceJwt(
                PosthogJwtAudience.CUSTOMER_ANALYTICS_ACCOUNTS,
                config.CUSTOMER_ANALYTICS_ACCOUNTS_JWT_SECRET
            ),
            hogInputsService,
            emailService,
            recipientTokensService,
            pushNotificationService,
        }
    )

    const hogFunctionTemplateManager = new HogFunctionTemplateManagerService(deps.postgres)
    const hogFlowFunctionsService = new HogFlowFunctionsService(
        config.SITE_URL,
        hogFunctionTemplateManager,
        hogExecutorAsync
    )

    const recipientPreferencesService = new RecipientPreferencesService(
        recipientsManager,
        emailSuppressionService,
        deps.frequencyCapValkey
            ? {
                  teamWorkflowsConfig: teamWorkflowsConfigService,
                  valkey: deps.frequencyCapValkey,
                  persons: personsManager,
              }
            : undefined
    )
    // MX verdicts live on the dedicated SES Valkey (same instance as the SES rate
    // limiter, separate pool). The pool is created by the server only on pods
    // whose capabilities execute email actions and on cdp-api; everywhere else
    // this is null and EmailValidationService degrades to the local cache + DNS.
    const emailValidationService = new EmailValidationService(deps.emailValidationValkey)
    const cohortMembershipRepository = new PostgresCohortMembershipRepository(deps.postgres)
    // Observer writes to both stores; the read source decides which verdict drives the metric.
    const hogFlowDuplicateObserver = new HogFlowDuplicateObserverService(redis, valkeyShadow.writer)
    const cdpUsageReporter = new CdpUsageReporterService(
        createUsageIngestionClient(config, 'cdp'),
        usageReportTeamMatcher(config)
    )
    const hogFlowExecutor = new HogFlowExecutorService(
        hogFlowFunctionsService,
        recipientPreferencesService,
        emailValidationService,
        cohortMembershipRepository,
        deps.integrationManager,
        emailStatusService,
        hogFlowDuplicateObserver,
        cdpUsageReporter,
        { awaitedStepsEnabled: config.CDP_HOGFLOW_AWAITED_STEPS_ENABLED }
    )

    const internalLogsSources = parseInternalLogsSources(config.CDP_INTERNAL_LOGS_SOURCES)
    const hogFunctionMonitoringService = new HogFunctionMonitoringService(
        outputs,
        internalLogsSources.size > 0 ? new InternalLogsProducer(config, deps.teamManager) : undefined,
        internalLogsSources
    )
    const hogInvocationResultsService = new HogInvocationResultsService(outputs, config)
    const warehouseWebhooksService = new WarehouseWebhooksService(outputs)
    const capturedEventsService = new CapturedEventsService(deps.internalCaptureService, deps.teamManager)
    const conversionWatchersService = new ConversionWatchersService(
        config.CYCLOTRON_NODE_DATABASE_URL,
        config.CYCLOTRON_NODE_MAX_CONNECTIONS
    )
    const invocationResultsService = new InvocationResultsService(
        hogFunctionMonitoringService,
        hogInvocationResultsService,
        warehouseWebhooksService,
        capturedEventsService,
        messageAssetsService,
        conversionWatchersService,
        emailStatusService
    )

    const nativeDestinationExecutorService = new NativeDestinationExecutorService(config)
    const segmentDestinationExecutorService = new SegmentDestinationExecutorService(config)

    return {
        redis,
        valkeyShadow,
        hogFunctionManager,
        hogFlowManager,
        hogWatcher,
        hogWatcherMirror,
        hogExecutorAsync,
        hogInputsService,
        hogFunctionTemplateManager,
        hogFlowFunctionsService,
        personsManager,
        recipientsManager,
        recipientPreferencesService,
        emailSuppressionService,
        emailValidationService,
        teamWorkflowsConfigService,
        cohortMembershipRepository,
        hogFlowExecutor,
        hogFunctionMonitoringService,
        workflowsActivationReporter,
        cdpUsageReporter,
        capturedEventsService,
        hogInvocationResultsService,
        invocationResultsService,
        nativeDestinationExecutorService,
        segmentDestinationExecutorService,
        recipientTokensService,
        outputs,
        emailService,
        emailAttachmentsService,
        messageAssetsService,
        emailStatusService,
    }
}