Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

ralph(M10-006): Job handlers take clients from ctx: delete the 7 named load*Deps/Runtime loaders in session, ingest, misc, organization, integration

ralph6a5f1bb8310a
diff --git a/apps/api/src/main.ts b/apps/api/src/main.ts
index 700ba8e08..91a131da3 100644
--- a/apps/api/src/main.ts
+++ b/apps/api/src/main.ts
@@ -37,9 +37,11 @@ import {
type BufferDeps,
bullBoardRoutes,
COOKIE_OPTIONS,
+ clearProjectByIdCache,
corsDelegator,
createBuffers,
createClients,
+ createIncomingEventHandler,
createInitialSalts,
createProducers,
createTrpcFetchHandler,
@@ -47,11 +49,10 @@ import {
debugRoutes,
enableEventsHeartbeat,
errorHandler,
+ getProjectByIdCached,
type HttpCtx,
- incomingEvent,
ingestConsumerMetrics,
type KafkaConsumerHandle,
- loadIncomingEventDeps,
markEventsActivity,
opsRoutes,
publicApiRoutes,
@@ -66,8 +67,6 @@ import {
registerSessionScrapeMetrics,
requestLogging,
type SessionMetricsRedis,
- sessionEndEnqueueOptions,
- sessionEndJobPayload,
setShuttingDown,
setV1CompatServices,
startKafkaEventsConsumer,
@@ -91,6 +90,7 @@ import {
produceDeadLetterEvent,
produceIncomingEvent,
} from '@openpanel/queue';
+import { checkNotificationRulesForEvent } from '@openpanel/queue/src/notification-dispatch';
import { getRedisCache, getRedisPub, getRedisQueue } from '@openpanel/redis';
import { appRouter } from '@openpanel/trpc';
import { Elysia } from 'elysia';
@@ -255,8 +255,7 @@ function warnOnUnhandledSchedulers(schedulerIds: string[]): void {
* The boot scope as `ServiceDeps`. `AppDeps` carries a `QueueProducerHandle`;
* a service wants an already-scoped `QueueProducers`, and outside a request
* there is nothing to correlate with, so the boot scope stamps
- * `V1_COMPAT_REQUEST_ID` (M10-005 — the ingest consumer's event deps need the
- * same shape the v1-compat seam does).
+ * `V1_COMPAT_REQUEST_ID` (M10-005).
*/
function bootServiceDeps(
deps: AppDeps
@@ -284,17 +283,6 @@ async function startIngestConsumer(
assertKafkaConfigured();
enableEventsHeartbeat();
- const eventDeps = await loadIncomingEventDeps(
- bootServiceDeps(deps),
- logger,
- async (input) => {
- await deps.producers.queues.sessions.session.add(
- sessionEndJobPayload(input),
- sessionEndEnqueueOptions(input.closedSession.id)
- );
- }
- );
-
return await startKafkaEventsConsumer({
createConsumer: createKafkaEventsConsumer,
logger,
@@ -302,7 +290,16 @@ async function startIngestConsumer(
topic: KAFKA_EVENTS_TOPIC,
partitionsConsumedConcurrently: KAFKA_PARTITIONS_CONCURRENT,
batch: {
- handleEvent: (payload, meta) => incomingEvent(payload, eventDeps, meta),
+ // One Ctx per message, scoped to the requestId the producer stamped
+ // into the envelope (M10-006). The two bindings a work scope cannot
+ // supply are resolved here, once: the notification dispatch lives in
+ // @openpanel/queue (core cannot import it back) and the project cache
+ // is the boot-registered singleton ingest/http/mcp share.
+ handleEvent: createIncomingEventHandler(deps, {
+ checkNotificationRulesForEvent,
+ getCachedProject: getProjectByIdCached,
+ clearProjectCache: clearProjectByIdCache,
+ }),
sendToDeadLetter: produceDeadLetterEvent,
logger,
metrics: ingestConsumerMetrics,
diff --git a/packages/core/src/http/hooks.test.ts b/packages/core/src/http/hooks.test.ts
index f3649debe..84df53d61 100644
--- a/packages/core/src/http/hooks.test.ts
+++ b/packages/core/src/http/hooks.test.ts
@@ -2,35 +2,10 @@ import { describe, expect, test } from 'bun:test';
import { cors } from '@elysiajs/cors';
import { Elysia } from 'elysia';
import { REQUEST_ID_LENGTH } from '../logger';
-import {
- clientIpHook,
- requestIdHook,
- sanitizeRequestId,
- timestampHook,
-} from './hooks';
+import { clientIpHook, requestIdHook, timestampHook } from './hooks';
const ALLOWED_ORIGIN = 'https://dashboard.test';
-describe('sanitizeRequestId', () => {
- test('keeps an id a caller can correlate on', () => {
- expect(sanitizeRequestId('adr018-1a2b_3c')).toBe('adr018-1a2b_3c');
- });
-
- test('strips everything outside [A-Za-z0-9_-]', () => {
- expect(sanitizeRequestId('a b/c\n<script>')).toBe('abcscript');
- });
-
- test('truncates to 64 characters', () => {
- expect(sanitizeRequestId('x'.repeat(200))).toHaveLength(64);
- });
-
- test('reports nothing survivable rather than an empty id', () => {
- expect(sanitizeRequestId('!!!')).toBeNull();
- expect(sanitizeRequestId('')).toBeNull();
- expect(sanitizeRequestId(null)).toBeNull();
- });
-});
-
describe('requestIdHook', () => {
const app = new Elysia()
.use(requestIdHook())
diff --git a/packages/core/src/http/hooks.ts b/packages/core/src/http/hooks.ts
index 8c320f638..a5d36857a 100644
--- a/packages/core/src/http/hooks.ts
+++ b/packages/core/src/http/hooks.ts
@@ -16,21 +16,7 @@ import { Elysia } from 'elysia';
import { REQUEST_ID_HEADER, REQUEST_ID_LENGTH } from '../logger';
import { getClientIpFromHeaders } from '../shared/get-client-ip';
import { generateId } from '../shared/id';
-
-// ADR-018 R2: an inbound request-id is honoured but never trusted verbatim —
-// it reaches log storage and a log search.
-const DISALLOWED_REQUEST_ID_CHARS = /[^A-Za-z0-9_-]/g;
-const REQUEST_ID_MAX_LENGTH = 64;
-
-export function sanitizeRequestId(candidate: string | null): string | null {
- if (!candidate) {
- return null;
- }
- const cleaned = candidate
- .replace(DISALLOWED_REQUEST_ID_CHARS, '')
- .slice(0, REQUEST_ID_MAX_LENGTH);
- return cleaned.length > 0 ? cleaned : null;
-}
+import { sanitizeRequestId } from '../shared/request-id';
/**
* First of the three. `requestIdFromCaller` is not decoration: ADR-018 R6
diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts
index 1c4b843b5..91bf3e790 100644
--- a/packages/core/src/index.ts
+++ b/packages/core/src/index.ts
@@ -861,7 +861,6 @@ export {
SESSION_DISTINCT_FIELDS,
transformSession,
} from './modules/session/session.service';
-export { loadSessionRuntime } from './modules/session/src/runtime';
export {
als,
getAlsSessionId,
@@ -874,7 +873,6 @@ export type {
export {
createSessionEnd,
getSessionEndJobId,
- loadSessionEndDeps,
sessionEndEnqueueOptions,
sessionEndJobPayload,
} from './modules/session/src/session-end';
@@ -1009,6 +1007,7 @@ export {
createEventsBatchHandler,
startKafkaEventsConsumer,
} from './modules/ingest/src/consumer';
+export { createIncomingEventHandler } from './modules/ingest/src/consumer-handler';
// The readiness probe's two inputs (M9-002). `markEventsActivity` is the
// consumer's `onActivity`; `setShuttingDown` is main.ts's signal handler.
export {
@@ -1017,14 +1016,16 @@ export {
markEventsActivity,
} from './modules/ingest/src/heartbeat';
export type {
+ IncomingEventBindings,
IncomingEventDelivery,
IncomingEventDeps,
IncomingEventMetrics,
+ IncomingEventProject,
IncomingEventSessions,
} from './modules/ingest/src/incoming-event-handler';
export {
+ createIncomingEventDeps,
incomingEvent,
- loadIncomingEventDeps,
} from './modules/ingest/src/incoming-event-handler';
export { ingestConsumerMetrics } from './modules/ingest/src/ingest.metrics';
export type {
@@ -1221,6 +1222,7 @@ export {
cancelSubscription,
checkout,
clearClientByIdCache,
+ clearProjectByIdCache,
compatCh,
compatDb,
createBotEvent,
diff --git a/packages/core/src/modules/ingest/src/consumer-handler.ts b/packages/core/src/modules/ingest/src/consumer-handler.ts
new file mode 100644
index 000000000..37dd152f3
--- /dev/null
+++ b/packages/core/src/modules/ingest/src/consumer-handler.ts
@@ -0,0 +1,40 @@
+// One `Ctx` per Kafka message, so the consumer's `handleEvent` is the same
+// kind of work scope an HTTP request or a job run is (ADR-007).
+//
+// The requestId comes out of the envelope's `headers` map — the copy `/track`
+// puts there, which ADR-018 R3 keeps as the consumer's source — and is
+// sanitised exactly as an inbound HTTP `request-id` is, because it lands in
+// the same log storage. Before M10-006 the handler read it only to bind a
+// `reqId` log field: the ClickHouse writes, the buffer writes and the
+// session-end enqueue the message caused all ran on boot-scoped clients, so
+// the chain ended at the log line (docs/TECH_DEBT.md §2).
+
+import { type AppDeps, createCtx } from '../../../context';
+import { REQUEST_ID_HEADER, REQUEST_ID_LOG_FIELD } from '../../../logger';
+import { resolveRequestId } from '../../../shared/request-id';
+import type { IncomingEventPayload } from './incoming-event';
+import {
+ createIncomingEventDeps,
+ type IncomingEventBindings,
+ type IncomingEventDelivery,
+ incomingEvent,
+} from './incoming-event-handler';
+
+export function incomingEventRequestId(payload: IncomingEventPayload): string {
+ return resolveRequestId(payload.headers?.[REQUEST_ID_HEADER]);
+}
+
+/** The consumer's `handleEvent` (`EventsBatchHandlerDeps.handleEvent`). */
+export function createIncomingEventHandler(
+ deps: AppDeps,
+ bindings: IncomingEventBindings
+) {
+ return (payload: IncomingEventPayload, meta: IncomingEventDelivery) => {
+ const requestId = incomingEventRequestId(payload);
+ const ctx = createCtx(deps, {
+ requestId,
+ logger: deps.logger.child({ [REQUEST_ID_LOG_FIELD]: requestId }),
+ });
+ return incomingEvent(payload, createIncomingEventDeps(ctx, bindings), meta);
+ };
+}
diff --git a/packages/core/src/modules/ingest/src/incoming-event-handler.ts b/packages/core/src/modules/ingest/src/incoming-event-handler.ts
index 3c12208e4..848dd64f6 100644
--- a/packages/core/src/modules/ingest/src/incoming-event-handler.ts
+++ b/packages/core/src/modules/ingest/src/incoming-event-handler.ts
@@ -1,25 +1,22 @@
// Ported from apps/worker/src/jobs/events.incoming-event.ts (M8-003): the
// Kafka consumer's per-message handler — the step that turns one topic
-// payload into session lifecycle decisions and ClickHouse rows. V1's job file
-// stays (DELEGATE PATTERN) and calls this until apps/worker dies (P9).
+// payload into session lifecycle decisions and ClickHouse rows.
//
// Everything that touches Redis, Postgres, ClickHouse or a queue is INJECTED
-// (`IncomingEventDeps`), the way session-end.ts does it: the tests then drive
-// the real code with recording doubles instead of `mock.module`, and V1 keeps
-// handing in its own buffers, counters and `sessionsQueue` producer so its
-// behaviour and its `/metrics` body are unchanged.
-//
-// createEvent is a static import for the reason session.service.ts's header
-// gives: a dynamic edge here closes a dynamic-import cycle that panics
-// rolldown when apps/worker bundles the workspace.
+// (`IncomingEventDeps`), the way session-end.ts does it, so the tests drive
+// the real code with recording doubles instead of `mock.module`.
+// `createIncomingEventDeps` binds it to ONE MESSAGE'S ctx (M10-006): the
+// buffers, the Postgres client and the session-end enqueue all ride the scope
+// carrying that envelope's requestId, so the id the producer stamped reaches
+// the row the consumer writes (ADR-018 R1). The two lookups a work scope
+// cannot supply arrive as `IncomingEventBindings` from the composition root.
import { getTime, isSameDomain, parsePath } from '@openpanel/common';
import type { IProjectFilters } from '@openpanel/validation';
import { anyPass, isEmpty, isNil, mergeDeepRight, omit, reject } from 'ramda';
-import { loadDbBuffers } from '../../../buffers/lazy-db-buffers';
import type { SessionIngestResult } from '../../../buffers/session-buffer';
+import type { Ctx } from '../../../context';
import type { Logger } from '../../../logger';
-import type { ServiceDeps } from '../../../services';
import {
getReferrerWithQuery,
parseReferrer,
@@ -31,20 +28,6 @@ import {
type IServiceEvent,
} from '../../event/event.service';
import { matchEvent } from '../../notification/notification.service';
-
-// project.service.ts's `getProjectByIdCached` takes `ServiceDeps` now
-// (M10-004), and its `.clear()` LRU-invalidation must hit the SAME cache
-// instance ingest/http/mcp all read — see that file's header — so this
-// reaches the v1-compat singleton instead. Previously a STATIC import for a
-// rolldown-chunking reason tied to apps/worker's bundle: apps/worker is
-// gone (M9) and apps/api ships with no bundler at all (ADR-010), so that
-// constraint no longer applies — GENUINE CYCLE, kept lazy, same as every
-// other one in this wave: services.ts -> ingest.service.ts -> this file ->
-// v1-compat.ts -> services.ts.
-function loadProjectService() {
- return import('../../../v1-compat');
-}
-
import type { IClickhouseSession } from '../../session/session.service';
import { sessionEndsEnqueued } from '../../session/src/session.metrics';
import type { EnqueueSessionEndInput } from '../../session/src/session-end';
@@ -67,7 +50,7 @@ export interface IncomingEventDelivery {
}
/** The project fields the filter and first-event checks read. */
-interface IncomingEventProject {
+export interface IncomingEventProject {
firstEventAt?: Date | null;
filters?: IProjectFilters[] | null;
}
@@ -93,7 +76,8 @@ export interface IncomingEventMetrics {
}
export interface IncomingEventDeps {
- /** Base logger; the handler child()s it per message. */
+ /** Already scoped to the message's requestId; the handler only adds the
+ * Kafka delivery coordinates to it. */
logger: Logger;
sessions: IncomingEventSessions;
createEvent(
@@ -112,44 +96,55 @@ export interface IncomingEventDeps {
}
/**
- * Binds `IncomingEventDeps` to the real implementations. db/ch access stays
- * lazy — see event.service.ts's header.
+ * The two lookups a work scope cannot supply. Both are resolved once at the
+ * composition root (`apps/api`'s main.ts) rather than per message.
*/
-export async function loadIncomingEventDeps(
- deps: ServiceDeps,
- logger: Logger,
- enqueueSessionEnd: (input: EnqueueSessionEndInput) => Promise<unknown>,
- overrides: Partial<IncomingEventDeps> = {}
-): Promise<IncomingEventDeps> {
- const [{ sessionBuffer }, { db }, notifications, projectService] =
- await Promise.all([
- loadDbBuffers(),
- import('@openpanel/db/src/prisma-client'),
- import('@openpanel/queue/src/notification-dispatch'),
- loadProjectService(),
- ]);
+export interface IncomingEventBindings {
+ /**
+ * BullMQ-producer orchestration, which lives in `@openpanel/queue` —
+ * a package that imports `@openpanel/core` back, so core cannot reach it
+ * (notification.service.ts's header).
+ */
+ checkNotificationRulesForEvent(
+ payload: IServiceCreateEventPayload
+ ): Promise<unknown>;
+ /**
+ * `getProjectByIdCached`'s L1 LRU lives inside `createProjectService(deps)`,
+ * so `ctx.services.project` would hand every message a fresh, empty cache;
+ * ingest, http and mcp read the one instance registered at boot.
+ */
+ getCachedProject(projectId: string): Promise<IncomingEventProject | null>;
+ clearProjectCache(projectId: string): Promise<unknown>;
+}
+
+/**
+ * Binds `IncomingEventDeps` to one message's scope. Synchronous by design —
+ * the hot path gets no per-message `await` it did not already have.
+ */
+export function createIncomingEventDeps(
+ ctx: Ctx,
+ bindings: IncomingEventBindings
+): IncomingEventDeps {
return {
- logger,
- sessions: sessionBuffer,
- createEvent: (payload) => createEvent(deps, payload),
- checkNotificationRulesForEvent:
- notifications.checkNotificationRulesForEvent,
+ logger: ctx.logger,
+ sessions: ctx.buffers.session,
+ createEvent: (payload) => createEvent(ctx, payload),
+ checkNotificationRulesForEvent: bindings.checkNotificationRulesForEvent,
projects: {
- getCached: projectService.getProjectByIdCached,
+ getCached: bindings.getCachedProject,
markFirstEvent: async (projectId) => {
- await db.project.updateMany({
+ await ctx.db.project.updateMany({
where: { id: projectId, firstEventAt: null },
data: { firstEventAt: new Date() },
});
- await projectService.clearProjectByIdCache(projectId);
+ await bindings.clearProjectCache(projectId);
},
},
- enqueueSessionEnd,
+ enqueueSessionEnd: (input) => ctx.services.session.enqueueSessionEnd(input),
metrics: {
sessionStarted: (kind) => sessionsStarted.inc({ kind }),
sessionEndEnqueued: (source) => sessionEndsEnqueued.inc({ source }),
},
- ...overrides,
};
}
@@ -252,13 +247,15 @@ export async function incomingEvent(
id: eventId,
} = jobPayload;
const properties: Record<string, unknown> = body.properties ?? {};
- const reqId = headers['request-id'] ?? 'unknown';
- const logger = deps.logger.child({
- reqId,
- ...(meta
- ? { kafkaPartition: meta.partition, kafkaOffset: meta.offset }
- : {}),
- });
+ // `requestId` rides in on `deps.logger`, which the consumer scopes to this
+ // envelope's id (ADR-018 R1 renames V1's `reqId`); only the delivery
+ // coordinates are per-message news.
+ const logger = meta
+ ? deps.logger.child({
+ kafkaPartition: meta.partition,
+ kafkaOffset: meta.offset,
+ })
+ : deps.logger;
const getProperty = (name: string): string | undefined => {
// replace thing is just for older sdks when we didn't have `__`
// remove when kiddokitchen app (24.09.02) is not used anymore
diff --git a/packages/core/src/modules/integration/integration.jobs.ts b/packages/core/src/modules/integration/integration.jobs.ts
index 3f833adb6..09d74fc9c 100644
--- a/packages/core/src/modules/integration/integration.jobs.ts
+++ b/packages/core/src/modules/integration/integration.jobs.ts
@@ -9,7 +9,11 @@
import { z } from 'zod';
import { defineJob } from '../../jobs/define';
import type { SchedulerDefinition } from '../../jobs/schedulers';
-import { loadFlushExportsDeps, runFlushExportsCron } from './src/flush-exports';
+import {
+ type ExportClickhouse,
+ type ExportDb,
+ runFlushExportsCron,
+} from './src/flush-exports';
/** Every 1 minute — drains export buffers to S3/GCS. */
const FLUSH_EXPORTS_INTERVAL_MS = 60_000;
@@ -19,8 +23,20 @@ export const integrationCronJobs = {
flushExports: defineJob({
payload: z.null(),
handler: async ({ ctx }) => {
- const logger = ctx.logger.child({ job: 'flush-exports' });
- await runFlushExportsCron(await loadFlushExportsDeps(logger));
+ // Lazy: the registry pulls the S3 and GCS SDKs, ~270ms of module
+ // evaluation (measured, Bun 1.4.0) that a static edge here would add to
+ // jobs.registry.ts's eager graph and so to every core test file.
+ const { getServerIntegration } = await import(
+ '../../clients/integrations/registry'
+ );
+
+ await runFlushExportsCron({
+ db: ctx.db as unknown as ExportDb,
+ ch: ctx.ch as unknown as ExportClickhouse,
+ logger: ctx.logger.child({ job: 'flush-exports' }),
+ createAdapter: (config) =>
+ getServerIntegration(config.type).export?.createAdapter(config),
+ });
},
}),
};
diff --git a/packages/core/src/modules/integration/src/flush-exports.ts b/packages/core/src/modules/integration/src/flush-exports.ts
index 0e3d97f45..363d862f3 100644
--- a/packages/core/src/modules/integration/src/flush-exports.ts
+++ b/packages/core/src/modules/integration/src/flush-exports.ts
@@ -114,24 +114,6 @@ export interface FlushExportsDeps {
createAdapter(config: ExportConfig): IObjectStoreAdapter | undefined;
}
-export async function loadFlushExportsDeps(
- logger: Logger
-): Promise<FlushExportsDeps> {
- const [{ db }, { ch }, { getServerIntegration }] = await Promise.all([
- import('@openpanel/db/src/prisma-client'),
- import('@openpanel/db/src/clickhouse/client'),
- import('../../../clients/integrations/registry'),
- ]);
-
- return {
- db: db as unknown as ExportDb,
- ch: ch as unknown as ExportClickhouse,
- logger,
- createAdapter: (config) =>
- getServerIntegration(config.type).export?.createAdapter(config),
- };
-}
-
function isExportConfig(config: IIntegrationConfig): config is ExportConfig {
// Capability comes from the integration registry, not a hardcoded type list.
return isKind(config, 'export');
diff --git a/packages/core/src/modules/misc/misc.jobs.ts b/packages/core/src/modules/misc/misc.jobs.ts
index 1241991ca..18ebd6809 100644
--- a/packages/core/src/modules/misc/misc.jobs.ts
+++ b/packages/core/src/modules/misc/misc.jobs.ts
@@ -12,7 +12,7 @@
import { z } from 'zod';
import { defineJob } from '../../jobs/define';
import type { SchedulerDefinition } from '../../jobs/schedulers';
-import { loadDataHealthDeps, runDataHealthCron } from './src/data-health';
+import { type DataHealthDb, runDataHealthCron } from './src/data-health';
/** Daily 07:30 UTC — V1's cadence (apps/worker/src/boot-cron.ts). */
const DATA_HEALTH_CRON = '30 7 * * *';
@@ -28,8 +28,17 @@ export const miscCronJobs = {
dataHealth: defineJob({
payload: z.null(),
handler: async ({ ctx }) => {
- const logger = ctx.logger.child({ job: 'data-health' });
- await runDataHealthCron(await loadDataHealthDeps(ctx, logger));
+ await runDataHealthCron({
+ db: ctx.db as unknown as DataHealthDb,
+ logger: ctx.logger.child({ job: 'data-health' }),
+ getLastEventPerProject: () =>
+ ctx.services.project.getLastEventPerProject(),
+ sendEmail: (template, options) =>
+ ctx.clients.email.sendEmail(
+ template,
+ options as { to: string; data: never }
+ ),
+ });
},
}),
};
diff --git a/packages/core/src/modules/misc/src/data-health.ts b/packages/core/src/modules/misc/src/data-health.ts
index b24bbb834..82865e5c1 100644
--- a/packages/core/src/modules/misc/src/data-health.ts
+++ b/packages/core/src/modules/misc/src/data-health.ts
@@ -2,10 +2,9 @@
// deletes apps/worker). ADR-005's acceptance note gives `dataHealth` to the
// misc module. Behaviour is V1's, verbatim; Prisma, the last-event lookup and
// `sendEmail` arrive as injected deps so the job is testable without
-// `mock.module` (same idiom as modules/session/src/runtime.ts).
+// `mock.module`. `misc.jobs.ts` binds them off the job's ctx (M10-006).
import type { Logger } from '../../../logger';
-import type { ServiceDeps } from '../../../services';
const DAY_MS = 24 * 60 * 60 * 1000;
// A brand-new project gets 48h to send its first event before we reach out.
@@ -60,29 +59,6 @@ export interface DataHealthDeps {
): Promise<unknown>;
}
-export async function loadDataHealthDeps(
- deps: ServiceDeps,
- logger: Logger
-): Promise<DataHealthDeps> {
- // M10-005: Prisma is `deps.db` and `getLastEventPerProject` is the sibling
- // module's `ServiceDeps` spelling, so this job no longer reaches
- // @openpanel/db or the v1-compat seam. Both imports stay lazy: this file is
- // reached from jobs.registry.ts, and project.service.ts -> v1-compat.ts ->
- // services.ts is a cycle a static edge here would join.
- const [{ getLastEventPerProject }, { sendEmail }] = await Promise.all([
- import('../../project/project.service'),
- import('../../../clients/email'),
- ]);
-
- return {
- db: deps.db as unknown as DataHealthDb,
- logger,
- getLastEventPerProject: () => getLastEventPerProject(deps),
- sendEmail: (template, options) =>
- sendEmail(template, options as { to: string; data: never }),
- };
-}
-
async function recipientsForOrg(organizationId: string, deps: DataHealthDeps) {
const members = await deps.db.member.findMany({
where: {
diff --git a/packages/core/src/modules/organization/organization.jobs.ts b/packages/core/src/modules/organization/organization.jobs.ts
index ce475afc9..d51b7b0e5 100644
--- a/packages/core/src/modules/organization/organization.jobs.ts
+++ b/packages/core/src/modules/organization/organization.jobs.ts
@@ -8,9 +8,19 @@
// (apps/worker/src/boot-cron.ts).
import { z } from 'zod';
+import type { Ctx } from '../../context';
import { defineJob } from '../../jobs/define';
import type { SchedulerDefinition } from '../../jobs/schedulers';
-import { loadWindDownDeps, runWindDownCron } from './src/wind-down';
+import type { Logger } from '../../logger';
+import {
+ buildWinBackHighlight,
+ type WinBackHighlightDeps,
+} from './src/win-back-highlight';
+import {
+ runWindDownCron,
+ type WindDownDb,
+ type WindDownDeps,
+} from './src/wind-down';
/** Hourly — V1's cadence (apps/worker/src/boot-cron.ts). */
const WIND_DOWN_CRON = '0 * * * *';
@@ -27,12 +37,49 @@ export const organizationCronJobs = {
windDown: defineJob({
payload: z.null(),
handler: async ({ ctx }) => {
- const logger = ctx.logger.child({ job: 'wind-down' });
- await runWindDownCron(await loadWindDownDeps(logger));
+ await runWindDownCron(
+ await windDownDeps(ctx, ctx.logger.child({ job: 'wind-down' }))
+ );
},
}),
};
+/**
+ * The wind-down cron's dependencies, bound to the job's own ctx.
+ *
+ * Two reaches stay dynamic. The sibling service's two ClickHouse counts are
+ * not on `OrganizationService`, and `clients/ai/win-back.ts` pulls the agent
+ * runtime — a static edge from here would drag either into jobs.registry.ts's
+ * eager import graph, which every core test file walks.
+ */
+async function windDownDeps(ctx: Ctx, logger: Logger): Promise<WindDownDeps> {
+ const [
+ { getOrganizationEventsCount, getOrganizationEventsCountSince },
+ { generateWinBackPitch },
+ ] = await Promise.all([
+ import('./organization.service'),
+ import('../../clients/ai/win-back'),
+ ]);
+
+ const highlight: WinBackHighlightDeps = {
+ logger,
+ getAnalyticsOverview: (input) =>
+ ctx.services.overview.getAnalyticsOverviewCore(input),
+ getTopPages: (input) => ctx.services.pages.getTopPagesCore(input),
+ generatePitch: (facts) => generateWinBackPitch(facts),
+ };
+
+ return {
+ db: ctx.db as unknown as WindDownDb,
+ logger,
+ sendEmail: ctx.clients.email.sendEmail,
+ getLastEventPerProject: () => ctx.services.project.getLastEventPerProject(),
+ getOrganizationEventsCount,
+ getOrganizationEventsCountSince,
+ buildHighlight: (input) => buildWinBackHighlight(input, highlight),
+ };
+}
+
/** This module's fragment of `CRON_SCHEDULES` — id and cadence unchanged. */
export const organizationCronSchedules: readonly SchedulerDefinition[] = [
{ id: 'delete', schedule: { pattern: '0 * * * *' } },
diff --git a/packages/core/src/modules/organization/src/win-back-highlight.ts b/packages/core/src/modules/organization/src/win-back-highlight.ts
index 26f41403d..66690b19c 100644
--- a/packages/core/src/modules/organization/src/win-back-highlight.ts
+++ b/packages/core/src/modules/organization/src/win-back-highlight.ts
@@ -2,15 +2,9 @@
// wave that deletes apps/worker). Behaviour is V1's, verbatim; only the
// wiring changed — the two stats lookups and the AI call arrive as injected
// deps (`WinBackHighlightDeps`) instead of static imports, the same idiom the
-// session lifecycle uses (modules/session/src/runtime.ts): the job handler
-// binds them to the real core services through `loadWinBackHighlightDeps`,
-// and tests hand in stubs, so no `mock.module` is needed anywhere.
-//
-// Loading is lazy for the reason organization.service.ts's header gives:
-// organization.jobs.ts is pulled into the eager `jobs.registry.ts` barrel, and
-// a static import of overview.service.ts from here would drag the whole
-// analytics read path — and @openpanel/db's clients — into every core test
-// file's import graph.
+// session lifecycle uses (modules/session/src/runtime.ts): `organization.jobs.ts`
+// binds them off the job's ctx (M10-006), and tests hand in stubs, so no
+// `mock.module` is needed anywhere.
import { format, subDays } from 'date-fns';
import type { Logger } from '../../../logger';
@@ -90,29 +84,6 @@ export interface WinBackHighlightDeps {
generatePitch(facts: WinBackPitchFacts): Promise<string>;
}
-export async function loadWinBackHighlightDeps(
- logger: Logger
-): Promise<WinBackHighlightDeps> {
- // M10-005: both overview entry points take `ServiceDeps` now; the wind-down
- // cron that reaches this has none of its own, so — same as
- // `loadWindDownDeps`'s `getLastEventPerProject` — it uses the bare,
- // v1-compat-wrapped spellings.
- const [
- { getAnalyticsOverviewCore, getTopPagesCore },
- { generateWinBackPitch },
- ] = await Promise.all([
- import('../../../v1-compat'),
- import('../../../clients/ai/win-back'),
- ]);
-
- return {
- logger,
- getAnalyticsOverview: (input) => getAnalyticsOverviewCore(input),
- getTopPages: (input) => getTopPagesCore(input),
- generatePitch: (facts) => generateWinBackPitch(facts),
- };
-}
-
const formatCount = (n: number) => new Intl.NumberFormat('en-US').format(n);
async function collectFacts(
diff --git a/packages/core/src/modules/organization/src/wind-down.ts b/packages/core/src/modules/organization/src/wind-down.ts
index e6e5f59ae..21da55370 100644
--- a/packages/core/src/modules/organization/src/wind-down.ts
+++ b/packages/core/src/modules/organization/src/wind-down.ts
@@ -4,9 +4,8 @@
// Prisma, the two ClickHouse counts, the last-event lookup, the plan pricer,
// the highlight builder and `sendEmail` all arrive as injected deps
// (`WindDownDeps`), the same idiom the session lifecycle uses
-// (modules/session/src/runtime.ts). The job handler binds them to the real
-// implementations through `loadWindDownDeps`; tests hand in stubs, so no
-// `mock.module` is needed.
+// (modules/session/src/runtime.ts). `organization.jobs.ts` binds them off the
+// job's ctx (M10-006); tests hand in stubs, so no `mock.module` is needed.
import { getRecommendedPlan } from '@openpanel/payments';
import { addDays, format, subDays } from 'date-fns';
@@ -120,37 +119,6 @@ interface WindDownContext {
getHighlight: () => Promise<string | undefined>;
}
-export async function loadWindDownDeps(logger: Logger): Promise<WindDownDeps> {
- const [
- { db },
- { getOrganizationEventsCount, getOrganizationEventsCountSince },
- { getLastEventPerProject },
- { sendEmail },
- { loadWinBackHighlightDeps, buildWinBackHighlight },
- ] = await Promise.all([
- import('@openpanel/db/src/prisma-client'),
- import('../organization.service'),
- // project.service.ts's `getLastEventPerProject` takes `ServiceDeps` now
- // (M10-004); this file has none, so it reaches the bare,
- // v1-compat-wrapped, zero-arg spelling instead.
- import('../../../v1-compat'),
- import('../../../clients/email'),
- import('./win-back-highlight'),
- ]);
-
- const highlightDeps = await loadWinBackHighlightDeps(logger);
-
- return {
- db: db as unknown as WindDownDb,
- logger,
- sendEmail,
- getLastEventPerProject,
- getOrganizationEventsCount,
- getOrganizationEventsCountSince,
- buildHighlight: (input) => buildWinBackHighlight(input, highlightDeps),
- };
-}
-
// Lazy + memoized like getUsage: only still-tracking orgs that clear a day
// gate pay for the stats queries and the AI call, and each org pays once per
// tick even though two steps include the highlight.
diff --git a/packages/core/src/modules/session/session.jobs.ts b/packages/core/src/modules/session/session.jobs.ts
index 9fbe58762..5413feb44 100644
--- a/packages/core/src/modules/session/session.jobs.ts
+++ b/packages/core/src/modules/session/session.jobs.ts
@@ -10,17 +10,27 @@
// `sessionVacuum` are cron fragments: declared here, spread into the ONE
// `cron` queue by jobs.registry.ts and into `CRON_SCHEDULES` by
// jobs/schedulers.ts, ids and cadences unchanged (apps/worker/src/boot-cron.ts).
-// `flushSessions`/`flushReplay` read the buffers off `ctx.buffers` — the
-// AppDeps boot singleton (ADR-007) — never a module-level import of
-// @openpanel/db's buffer shim.
+//
+// Every handler reads its clients off `ctx` — the buffers and the Redis
+// connection main.ts built once, and the ClickHouse client the job's own
+// requestId is bound to (M10-006, docs/TECH_DEBT.md §2).
import { z } from 'zod';
+import type { Ctx } from '../../context';
import { defineJob } from '../../jobs/define';
import type { SchedulerDefinition } from '../../jobs/schedulers';
-import { loadSessionRuntime } from './src/runtime';
+import type { Logger } from '../../logger';
+import { chQuery } from '../../shared/ch-query';
+import {
+ createEvent,
+ type IClickhouseEvent,
+ transformEvent,
+ transformSessionToEvent,
+} from '../event/event.service';
+import { sessionRuntimeFrom } from './src/runtime';
import {
createSessionEnd,
- loadSessionEndDeps,
+ type SessionEndDeps,
type SessionEndJobData,
} from './src/session-end';
import { reapIdleSessions } from './src/session-reaper';
@@ -53,19 +63,50 @@ const sessionEndPayload = z.custom<SessionEndJobData>(
(value) => sessionEndWireShape.safeParse(value).success
);
+/**
+ * The session-end job's dependencies, bound to the run's own ctx.
+ *
+ * Two lookups stay dynamic. `checkNotificationRulesForSessionEnd` lives in
+ * `@openpanel/queue`, which imports `@openpanel/core` back — a real package
+ * cycle (notification.service.ts's header). The rule cache is reached the same
+ * way only to keep notification.service.ts out of jobs.registry.ts's eager
+ * import graph, which every core test file walks.
+ */
+async function sessionEndDeps(
+ ctx: Ctx,
+ logger: Logger
+): Promise<SessionEndDeps> {
+ const [notifications, { checkNotificationRulesForSessionEnd }] =
+ await Promise.all([
+ import('../notification/notification.service'),
+ import('@openpanel/queue/src/notification-dispatch'),
+ ]);
+
+ return {
+ ...sessionRuntimeFrom(ctx),
+ logger,
+ createEvent: (payload) => createEvent(ctx, payload),
+ transformEvent,
+ transformSessionToEvent,
+ getEvents: async (query) =>
+ (await chQuery<IClickhouseEvent>(ctx, query)).map(transformEvent),
+ profileBackfill: ctx.buffers.profileBackfill,
+ notifications: {
+ getRules: notifications.getNotificationRulesByProjectId,
+ hasFunnelRules: notifications.getHasFunnelRules,
+ checkFunnelRules: checkNotificationRulesForSessionEnd,
+ },
+ };
+}
+
/** The `sessions` queue's own job. */
export const sessionQueueJobs = {
session: defineJob({
payload: sessionEndPayload,
handler: async ({ payload: data, ctx }) => {
const logger = ctx.logger.child({ payload: data.event });
- const deps = await loadSessionEndDeps(
- ctx,
- await loadSessionRuntime(),
- logger
- );
- await createSessionEnd(data, deps);
+ await createSessionEnd(data, await sessionEndDeps(ctx, logger));
try {
await updateEventsCount(data.event.projectId);
@@ -97,7 +138,7 @@ export const sessionCronJobs = {
payload: z.null(),
handler: async ({ ctx }) => {
await reapIdleSessions({
- ...(await loadSessionRuntime()),
+ ...sessionRuntimeFrom(ctx),
logger: ctx.logger.child({ job: 'session-reaper' }),
enqueueSessionEnd: (input) =>
ctx.services.session.enqueueSessionEnd(input),
@@ -108,7 +149,7 @@ export const sessionCronJobs = {
payload: z.null(),
handler: async ({ ctx }) => {
await vacuumStaleSessions({
- ...(await loadSessionRuntime()),
+ ...sessionRuntimeFrom(ctx),
logger: ctx.logger.child({ job: 'session-vacuum' }),
});
},
diff --git a/packages/core/src/modules/session/src/runtime.ts b/packages/core/src/modules/session/src/runtime.ts
index b13e6dacb..97b889b00 100644
--- a/packages/core/src/modules/session/src/runtime.ts
+++ b/packages/core/src/modules/session/src/runtime.ts
@@ -1,11 +1,10 @@
// What the session lifecycle (session_end emission, reaper, vacuum) needs from
// the outside world, as narrow structural interfaces. The job handlers bind
-// them to the real @openpanel/db buffers and @openpanel/redis client through
-// `loadSessionRuntime` (lazily — see session.service.ts's header); the tests
+// them to the scope's own clients with `sessionRuntimeFrom(ctx)`; the tests
// hand in stubs, so no `mock.module` is needed and every assertion is on a
// call the code under test made.
-import { loadDbBuffers } from '../../../buffers/lazy-db-buffers';
+import type { Ctx } from '../../../context';
import type { IClickhouseSession } from '../session.service';
export interface SessionStore {
@@ -51,10 +50,14 @@ export interface SessionRuntime {
sessions: SessionStore;
}
-export async function loadSessionRuntime(): Promise<SessionRuntime> {
- const [{ getRedisCache }, { sessionBuffer }] = await Promise.all([
- import('@openpanel/redis'),
- loadDbBuffers(),
- ]);
- return { redis: getRedisCache(), sessions: sessionBuffer };
+/**
+ * The two boot singletons the lifecycle needs, read off the work scope rather
+ * than constructed here: `ctx.redis` and `ctx.buffers` are the same objects
+ * main.ts built once, so a job handler no longer opens a connection of its
+ * own and its writes stay inside the request's scope (ADR-007, ADR-018 R1).
+ */
+export function sessionRuntimeFrom(
+ ctx: Pick<Ctx, 'redis' | 'buffers'>
+): SessionRuntime {
+ return { redis: ctx.redis, sessions: ctx.buffers.session };
}
diff --git a/packages/core/src/modules/session/src/session-end.ts b/packages/core/src/modules/session/src/session-end.ts
index 6f9301c09..830c802cf 100644
--- a/packages/core/src/modules/session/src/session-end.ts
+++ b/packages/core/src/modules/session/src/session-end.ts
@@ -1,22 +1,17 @@
// Ported from apps/worker/src/jobs/events.create-session-end.ts and
-// utils/session-handler.ts (M7-001). The worker's copies are thin delegates
-// onto this file until apps/worker dies (P9).
+// utils/session-handler.ts (M7-001).
+//
+// Everything that touches Redis, ClickHouse, a buffer or a queue is INJECTED
+// (`SessionEndDeps`): M10-006 moved the binding into `session.jobs.ts`, where
+// the job's `ctx` supplies every client, so the requestId that opened the
+// session still labels the rows this close writes (ADR-018 R1).
-import { loadDbBuffers } from '../../../buffers/lazy-db-buffers';
import type { EnqueueOptions } from '../../../jobs/define';
import type { Logger } from '../../../logger';
-import type { ServiceDeps } from '../../../services';
-// Static, not lazy like the db imports below: core's event.service touches no
-// client at import time, and a dynamic edge here closes a dynamic-import cycle
-// (event.service ⇢ session.service → this file) that panics rolldown when
-// apps/worker bundles the workspace.
-import {
- createEvent,
- type IClickhouseEvent,
- type IServiceCreateEventPayload,
- type IServiceEvent,
- transformEvent,
- transformSessionToEvent,
+import type {
+ IClickhouseEvent,
+ IServiceCreateEventPayload,
+ IServiceEvent,
} from '../../event/event.service';
import type { INotificationRuleCached } from '../../notification/notification.service';
import type { IClickhouseSession } from '../session.service';
@@ -90,7 +85,10 @@ export function sessionEndJobPayload({
};
}
-/** The bits of @openpanel/db the emission needs beyond the shared runtime. */
+/**
+ * What the emission needs beyond the shared runtime. `session.jobs.ts` binds
+ * it from the job's own ctx; the tests hand in stubs.
+ */
export interface SessionEndDeps extends SessionRuntime {
logger: Logger;
createEvent(
@@ -116,39 +114,6 @@ export interface SessionEndDeps extends SessionRuntime {
};
}
-export async function loadSessionEndDeps(
- deps: ServiceDeps,
- runtime: SessionRuntime,
- logger: Logger
-): Promise<SessionEndDeps> {
- const [
- { chQuery },
- { profileBackfillBuffer },
- notificationService,
- { checkNotificationRulesForSessionEnd },
- ] = await Promise.all([
- import('@openpanel/db/src/clickhouse/client'),
- loadDbBuffers(),
- import('../../notification/notification.service'),
- import('@openpanel/queue/src/notification-dispatch'),
- ]);
- return {
- ...runtime,
- logger,
- createEvent: (payload) => createEvent(deps, payload),
- transformEvent,
- transformSessionToEvent,
- getEvents: async (query) =>
- (await chQuery<IClickhouseEvent>(query)).map(transformEvent),
- profileBackfill: profileBackfillBuffer,
- notifications: {
- getRules: notificationService.getNotificationRulesByProjectId,
- hasFunnelRules: notificationService.getHasFunnelRules,
- checkFunnelRules: checkNotificationRulesForSessionEnd,
- },
- };
-}
-
function claimKey(session: IClickhouseSession): string {
return `session:end:emitted:${session.project_id}:${session.device_id}:${session.id}`;
}
diff --git a/packages/core/src/shared/request-id.test.ts b/packages/core/src/shared/request-id.test.ts
new file mode 100644
index 000000000..137d1c969
--- /dev/null
+++ b/packages/core/src/shared/request-id.test.ts
@@ -0,0 +1,35 @@
+import { describe, expect, test } from 'bun:test';
+import { REQUEST_ID_LENGTH } from '../logger';
+import { resolveRequestId, sanitizeRequestId } from './request-id';
+
+describe('sanitizeRequestId', () => {
+ test('keeps an id a caller can correlate on', () => {
+ expect(sanitizeRequestId('adr018-1a2b_3c')).toBe('adr018-1a2b_3c');
+ });
+
+ test('strips everything outside [A-Za-z0-9_-]', () => {
+ expect(sanitizeRequestId('a b/c\n<script>')).toBe('abcscript');
+ });
+
+ test('truncates to 64 characters', () => {
+ expect(sanitizeRequestId('x'.repeat(200))).toHaveLength(64);
+ });
+
+ test('reports nothing survivable rather than an empty id', () => {
+ expect(sanitizeRequestId('!!!')).toBeNull();
+ expect(sanitizeRequestId('')).toBeNull();
+ expect(sanitizeRequestId(null)).toBeNull();
+ expect(sanitizeRequestId(undefined)).toBeNull();
+ });
+});
+
+describe('resolveRequestId', () => {
+ test('honours a supplied id', () => {
+ expect(resolveRequestId('adr018-kafka')).toBe('adr018-kafka');
+ });
+
+ test('mints one when nothing survivable was supplied', () => {
+ expect(resolveRequestId(undefined)).toHaveLength(REQUEST_ID_LENGTH);
+ expect(resolveRequestId('!!!')).toHaveLength(REQUEST_ID_LENGTH);
+ });
+});
diff --git a/packages/core/src/shared/request-id.ts b/packages/core/src/shared/request-id.ts
new file mode 100644
index 000000000..444dc3215
--- /dev/null
+++ b/packages/core/src/shared/request-id.ts
@@ -0,0 +1,30 @@
+// ADR-018 R2: an inbound request-id is honoured but never trusted verbatim —
+// it reaches log storage and a log search. Two transports are handed one from
+// outside the process — the HTTP edge (`http/hooks.ts`) and the Kafka consumer,
+// which reads the id the producer stamped into the envelope — so the rule that
+// decides what is safe to bind to a logger lives here rather than in either.
+
+import { REQUEST_ID_LENGTH } from '../logger';
+import { generateId } from './id';
+
+const DISALLOWED_REQUEST_ID_CHARS = /[^A-Za-z0-9_-]/g;
+const REQUEST_ID_MAX_LENGTH = 64;
+
+export function sanitizeRequestId(
+ candidate: string | null | undefined
+): string | null {
+ if (!candidate) {
+ return null;
+ }
+ const cleaned = candidate
+ .replace(DISALLOWED_REQUEST_ID_CHARS, '')
+ .slice(0, REQUEST_ID_MAX_LENGTH);
+ return cleaned.length > 0 ? cleaned : null;
+}
+
+/** The supplied id when anything survives sanitising, a fresh one otherwise. */
+export function resolveRequestId(candidate: string | null | undefined): string {
+ return (
+ sanitizeRequestId(candidate) ?? generateId(undefined, REQUEST_ID_LENGTH)
+ );
+}
diff --git a/packages/core/test/request-id-ingest.test.ts b/packages/core/test/request-id-ingest.test.ts
new file mode 100644
index 000000000..6a3697d91
--- /dev/null
+++ b/packages/core/test/request-id-ingest.test.ts
@@ -0,0 +1,253 @@
+// ADR-018 R1/R3, the ingest half of `request-id-end-to-end.test.ts`.
+//
+// That file follows one requestId across the HTTP edge; this one follows it
+// across the OTHER edge, the Kafka consumer, where until M10-006 the chain
+// stopped at a log field: the handler bound the envelope's id to a `reqId`
+// binding, then wrote its rows through boot-scoped clients and enqueued the
+// boundary's session_end through boot-scoped producers, so nothing downstream
+// carried it (docs/TECH_DEBT.md §2).
+//
+// Nothing here is mocked with `mock.module`. `createIncomingEventHandler` is
+// the exact function `apps/api`'s main.ts hands to the consumer as
+// `handleEvent`, and the seam under test — createCtx → createIncomingEventDeps
+// → incomingEvent → ctx.buffers / ctx.db / ctx.services — is the real one.
+
+import { beforeEach, expect, test } from 'bun:test';
+import type { AppDeps, Buffers } from '../src/context';
+import { createRecordingProducers } from '../src/jobs/testing';
+import type { QueueProducerHandle } from '../src/jobs.registry';
+import { queues } from '../src/jobs.registry';
+import { type LogFn, type Logger, REQUEST_ID_HEADER } from '../src/logger';
+import { formatClickhouseDate } from '../src/modules/event/src/dates';
+import { createIncomingEventHandler } from '../src/modules/ingest/src/consumer-handler';
+import type { IncomingEventPayload } from '../src/modules/ingest/src/incoming-event';
+import type { IncomingEventBindings } from '../src/modules/ingest/src/incoming-event-handler';
+import type { IClickhouseSession } from '../src/modules/session/session.service';
+
+const SUPPLIED_REQUEST_ID = 'adr018-kafka-1';
+const OTHER_REQUEST_ID = 'adr018-kafka-2';
+const PROJECT_ID = 'proj_ingest';
+const DEVICE_ID = 'device_ingest';
+const CLOSED_SESSION_ID = 'session-closed';
+
+interface CapturedLine {
+ message: unknown;
+ bindings: Record<string, unknown>;
+}
+
+/** pino's child semantics: a child's bindings ride on every line it writes. */
+function bindingLogger(
+ lines: CapturedLine[],
+ bindings: Record<string, unknown> = {}
+): Logger {
+ const write: LogFn = (first: unknown, second?: unknown) => {
+ lines.push({
+ message: typeof first === 'string' ? first : second,
+ bindings,
+ });
+ };
+
+ return {
+ fatal: write,
+ error: write,
+ warn: write,
+ info: write,
+ debug: write,
+ trace: write,
+ child: (extra) => bindingLogger(lines, { ...bindings, ...extra }),
+ };
+}
+
+function makeSession(id: string): IClickhouseSession {
+ const now = formatClickhouseDate(new Date());
+ return {
+ id,
+ project_id: PROJECT_ID,
+ device_id: DEVICE_ID,
+ profile_id: '',
+ event_count: 1,
+ screen_view_count: 0,
+ entry_path: '/',
+ entry_origin: 'https://example.com',
+ exit_path: '/',
+ exit_origin: 'https://example.com',
+ created_at: now,
+ ended_at: now,
+ os: 'Windows',
+ os_version: '10',
+ browser: 'Chrome',
+ browser_version: '91',
+ device: 'desktop',
+ brand: '',
+ model: '',
+ country: 'US',
+ region: 'NY',
+ city: 'New York',
+ longitude: 0,
+ latitude: 0,
+ duration: 0,
+ referrer: '',
+ referrer_name: '',
+ referrer_type: '',
+ is_bounce: true,
+ utm_term: '',
+ utm_source: '',
+ utm_campaign: '',
+ utm_content: '',
+ utm_medium: '',
+ revenue: 0,
+ sign: 1,
+ version: 1,
+ groups: [],
+ };
+}
+
+/**
+ * A boundary on every message: it is the branch that both writes a row and
+ * enqueues a job, so one envelope exercises the whole fan-out.
+ */
+function stubDeps() {
+ const lines: CapturedLine[] = [];
+ const producers = createRecordingProducers(queues);
+ const eventsBuffered: { id: string }[] = [];
+
+ const buffers = {
+ event: { add: (event: { id: string }) => eventsBuffered.push(event) },
+ profile: { add: () => Promise.resolve() },
+ session: {
+ getExistingSession: () => Promise.resolve(null),
+ ingest: () =>
+ Promise.resolve({
+ kind: 'boundary' as const,
+ closed: makeSession(CLOSED_SESSION_ID),
+ current: makeSession('session-new'),
+ }),
+ },
+ } as unknown as Buffers;
+
+ const deps: AppDeps = {
+ db: {
+ project: { updateMany: () => Promise.resolve({ count: 0 }) },
+ } as unknown as AppDeps['db'],
+ ch: {} as AppDeps['ch'],
+ redis: {} as AppDeps['redis'],
+ clients: {} as AppDeps['clients'],
+ buffers,
+ producers: producers as unknown as QueueProducerHandle,
+ produceIncomingEvent: () => Promise.resolve(),
+ logger: bindingLogger(lines),
+ config: { selfHosted: false },
+ };
+
+ return { deps, lines, eventsBuffered, recorded: producers.recorded };
+}
+
+const bindings: IncomingEventBindings = {
+ checkNotificationRulesForEvent: () => Promise.resolve(null),
+ getCachedProject: () =>
+ Promise.resolve({ firstEventAt: new Date(), filters: [] }),
+ clearProjectCache: () => Promise.resolve(0),
+};
+
+function envelope(requestId?: string): IncomingEventPayload {
+ return {
+ geo: {
+ country: 'US',
+ city: 'New York',
+ region: 'NY',
+ longitude: 0,
+ latitude: 0,
+ },
+ event: {
+ name: 'screen_view',
+ timestamp: new Date().toISOString(),
+ isTimestampFromThePast: false,
+ properties: { __path: 'https://example.com/pricing' },
+ },
+ uaInfo: {
+ isServer: false,
+ device: 'desktop',
+ os: 'Windows',
+ osVersion: '10',
+ browser: 'Chrome',
+ browserVersion: '91',
+ brand: '',
+ model: '',
+ },
+ headers: requestId ? { [REQUEST_ID_HEADER]: requestId } : {},
+ projectId: PROJECT_ID,
+ deviceId: DEVICE_ID,
+ sessionId: 'session-new',
+ };
+}
+
+let harness: ReturnType<typeof stubDeps>;
+
+beforeEach(() => {
+ harness = stubDeps();
+});
+
+/** Every hop's requestId, so a failure names the hop that broke. */
+function trail(lines: CapturedLine[]) {
+ const bindingOf = (message: string) =>
+ lines.find((line) => line.message === message)?.bindings;
+
+ return {
+ createEventLog: bindingOf('Creating event')?.requestId,
+ kafkaOffset: bindingOf('Creating event')?.kafkaOffset,
+ };
+}
+
+test("the envelope's requestId reaches the handler's logger, its buffer write and the session_end it enqueues", async () => {
+ const handleEvent = createIncomingEventHandler(harness.deps, bindings);
+
+ await handleEvent(envelope(SUPPLIED_REQUEST_ID), {
+ partition: 3,
+ offset: '4711',
+ });
+
+ // Hop 1: the child logger the handler writes its per-event lines through.
+ expect(trail(harness.lines)).toEqual({
+ createEventLog: SUPPLIED_REQUEST_ID,
+ kafkaOffset: '4711',
+ });
+
+ // Hop 2: the ClickHouse row went through the ctx's buffer, not a client the
+ // handler opened for itself. Two rows: `session_start` and the event.
+ expect(harness.eventsBuffered).toHaveLength(2);
+
+ // Hop 3: the boundary's session_end job, enqueued through `ctx.services`.
+ // Before M10-006 this carried the boot scope's `v1-compat` id.
+ expect(
+ harness.recorded.map(({ queue, job, meta }) => ({
+ queue,
+ job,
+ requestId: meta.requestId,
+ }))
+ ).toEqual([
+ {
+ queue: 'sessions',
+ job: 'session',
+ requestId: SUPPLIED_REQUEST_ID,
+ },
+ ]);
+});
+
+test('a second message is scoped to its own id, and an envelope without one still gets scoped', async () => {
+ const handleEvent = createIncomingEventHandler(harness.deps, bindings);
+ const meta = { partition: 0, offset: '1' };
+
+ await handleEvent(envelope(SUPPLIED_REQUEST_ID), meta);
+ await handleEvent(envelope(OTHER_REQUEST_ID), meta);
+ await handleEvent(envelope(), meta);
+
+ const enqueued = harness.recorded.map(
+ ({ meta: jobMeta }) => jobMeta.requestId
+ );
+ expect(enqueued.slice(0, 2)).toEqual([SUPPLIED_REQUEST_ID, OTHER_REQUEST_ID]);
+
+ const minted = enqueued[2];
+ expect(minted).toBeString();
+ expect(minted).not.toBe(SUPPLIED_REQUEST_ID);
+ expect(minted).not.toBe(OTHER_REQUEST_ID);
+});