OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.
ralph(M15-109): Per-module fixes: packages/core/src/modules/event - R1, R2, R4, R8 and the findings no rule covers
ralph99d7c623a5ca
diff --git a/packages/core/src/modules/email/email.constants.ts b/packages/core/src/modules/email/email.constants.tsindex 3752853e5..483225016 100644--- a/packages/core/src/modules/email/email.constants.ts+++ b/packages/core/src/modules/email/email.constants.ts@@ -1,7 +1,5 @@// Moved from packages/constants/index.ts (M6-004, ADR-008's module map:-// email owns "C"). packages/constants re-exports these for existing-// packages/constants importers — same shape as packages/constants/index.ts's-// `ProjectTypeNames` re-export since M6-002.+// email owns "C"). packages/constants was deleted whole at M11-009.//// Keys must match the template `category` in @openpanel/email. Each entry's// label + description drive the account email-preferences toggles.diff --git a/packages/core/src/modules/email/email.rpc.ts b/packages/core/src/modules/email/email.rpc.tsindex 819729da1..14c660a85 100644--- a/packages/core/src/modules/email/email.rpc.ts+++ b/packages/core/src/modules/email/email.rpc.ts@@ -31,7 +31,7 @@ import {protectedProcedure,publicProcedure,} from '../../rpc/base';-import { TRPCBadRequestError } from '../../rpc/errors';+import { TRPCAccessError, TRPCBadRequestError } from '../../rpc/errors';import { emailCategories } from './email.constants';export const emailRouter = createTRPCRouter({@@ -62,7 +62,7 @@ export const emailRouter = createTRPCRouter({getPreferences: protectedProcedure.query(async ({ ctx }) => {if (!ctx.session.user?.email) {- throw new Error('User not authenticated');+ throw new TRPCAccessError('Not authenticated');}const email = ctx.session.user.email;@@ -85,7 +85,7 @@ export const emailRouter = createTRPCRouter({.mutation(async ({ input, ctx }) => {const email = ctx.session.user?.email;if (!email) {- throw new Error('User not authenticated');+ throw new TRPCAccessError('Not authenticated');}const db = ctx.db;diff --git a/packages/core/src/modules/event/event.constants.ts b/packages/core/src/modules/event/event.constants.tsnew file mode 100644index 000000000..b6ae4fe78--- /dev/null+++ b/packages/core/src/modules/event/event.constants.ts@@ -0,0 +1,93 @@+// Isomorphic vocabulary for the event module (ADR-022 R8). These are column+// whitelists, not query logic — moved here from event.service.ts / src/sql.ts+// so a value-import (mcp's property-values tool today; a future dashboard+// form tomorrow) has a `*.constants.ts` to reach instead of a service or+// query-builder file.++/**+ * Top-level filterable columns on the `events` table. These apply to+ * every event regardless of name and can be passed straight to+ * `getEventFiltersWhereClause` as filter / breakdown `name` values.+ *+ * Kept as a whitelist because the filter builder splices `name` into+ * SQL verbatim — only these are safe to expose through the AI /+ * MCP discovery surface.+ */+export const EVENT_COLUMNS = [+ 'path',+ 'origin',+ 'referrer',+ 'referrer_name',+ 'referrer_type',+ 'duration',+ 'country',+ 'city',+ 'region',+ 'os',+ 'os_version',+ 'browser',+ 'browser_version',+ 'device',+ 'brand',+ 'model',+ 'sdk_name',+ 'sdk_version',+ 'profile_id',+ 'session_id',+ 'device_id',+ 'revenue',+] as const;++export type IEventColumn = (typeof EVENT_COLUMNS)[number];++/** Every column `getEventList` can project, in V1's select order. */+export const EVENT_LIST_COLUMNS = [+ 'created_at',+ 'project_id',+ 'id',+ 'name',+ 'device_id',+ 'profile_id',+ 'session_id',+ 'properties',+ 'country',+ 'city',+ 'region',+ 'longitude',+ 'latitude',+ 'os',+ 'os_version',+ 'browser',+ 'browser_version',+ 'device',+ 'brand',+ 'model',+ 'path',+ 'origin',+ 'referrer',+ 'referrer_name',+ 'referrer_type',+ 'imported_at',+ 'sdk_name',+ 'sdk_version',+ 'revenue',+ 'groups',+] as const;++export type EventListColumn = (typeof EVENT_LIST_COLUMNS)[number];++/** The `= value` columns `queryEventsCore` accepts, in V1's clause order. */+export const QUERY_EVENTS_EQUALITY_COLUMNS = [+ 'path',+ 'referrer',+ 'referrer_name',+ 'referrer_type',+ 'device',+ 'country',+ 'city',+ 'os',+ 'browser',+] as const;++export type QueryEventsEqualityColumn =+ (typeof QUERY_EVENTS_EQUALITY_COLUMNS)[number];diff --git a/packages/core/src/modules/event/event.service.ts b/packages/core/src/modules/event/event.service.tsindex c65824c68..1c0f2c826 100644--- a/packages/core/src/modules/event/event.service.ts+++ b/packages/core/src/modules/event/event.service.ts@@ -5,7 +5,7 @@// notification.service, the mcp/assistant tools) keep working while V1 runs// (DELEGATE PATTERN).//-// Every query is a `sql` fragment (src/event.sql.ts), converted one at a time+// Every query is a `sql` fragment (src/sql.ts), converted one at a time// with a result-set proof each, per ADR-013. The two filter compilers// (`getEventFiltersWhereClause`, `buildFilterWhere`) are NOT converted here —// they are shared and out of this task's scope; src/filter-clauses.ts is the@@ -43,12 +43,17 @@ import {} from '../profile/profile.service';import type { IChartEventFilter } from '../report/report.constants';import type { IClickhouseSession } from '../session/session.service';+import {+ EVENT_COLUMNS,+ type EventListColumn,+ type QueryEventsEqualityColumn,+} from './event.constants';import { convertClickhouseDateToJs, formatClickhouseDate } from './src/dates';+import type { CompiledFilterClauses } from './src/filter-clauses';import {botEventsCountQuery,botEventsQuery,type EventFilterJoins,- type EventListColumn,type EventListQuery,type EventsCountQuery,eventByIdQuery,@@ -57,18 +62,18 @@ import {eventPropertyValuesQuery,eventsCountQuery,NO_FILTER_JOINS,- type QueryEventsEqualityColumn,queryEventsQuery,topEventNamesQuery,topOriginsQuery,topPagesQuery,-} from './src/event.sql';-import type { CompiledFilterClauses } from './src/filter-clauses';+} from './src/sql';+export type { IEventColumn } from './event.constants';export {+ EVENT_COLUMNS,EVENT_LIST_COLUMNS,QUERY_EVENTS_EQUALITY_COLUMNS,-} from './src/event.sql';+} from './event.constants';const EVENT_METAS_CACHE_SECONDS = 60 * 5;// V1's `cacheable(getEventMetas, ...)` derived this from the function's own@@ -933,42 +938,6 @@ export const listEventNamesCore = (projectId: string): Promise<string[]> => getTopEventNames(deps, projectId);-/**- * Top-level filterable columns on the `events` table. These apply to- * every event regardless of name and can be passed straight to- * `getEventFiltersWhereClause` as filter / breakdown `name` values.- *- * Kept as a whitelist because the filter builder splices `name` into- * SQL verbatim — only these are safe to expose through the AI /- * MCP discovery surface.- */-export const EVENT_COLUMNS = [- 'path',- 'origin',- 'referrer',- 'referrer_name',- 'referrer_type',- 'duration',- 'country',- 'city',- 'region',- 'os',- 'os_version',- 'browser',- 'browser_version',- 'device',- 'brand',- 'model',- 'sdk_name',- 'sdk_version',- 'profile_id',- 'session_id',- 'device_id',- 'revenue',-] as const;--export type IEventColumn = (typeof EVENT_COLUMNS)[number];-export async function listEventPropertiesCore(deps: ServiceDeps,input: {diff --git a/packages/core/src/modules/event/src/event.sql.proof.md b/packages/core/src/modules/event/src/sql.proof.mdsimilarity index 98%rename from packages/core/src/modules/event/src/event.sql.proof.mdrename to packages/core/src/modules/event/src/sql.proof.mdindex 21b014d96..33b00ad06 100644--- a/packages/core/src/modules/event/src/event.sql.proof.md+++ b/packages/core/src/modules/event/src/sql.proof.md@@ -1,6 +1,6 @@-# event.sql.ts — V1 → V2 result-set proof (M7-002)+# sql.ts (formerly event.sql.ts) — V1 → V2 result-set proof (M7-002)-Every query builder in `event.sql.ts` was executed twice against the same data — once as V1 (the `git HEAD` service/router code, `3300f2a0`) and once as V2 (core's service, which renders the builder) — through one patched ClickHouse client that captured each statement, its `query_params`, `clickhouse_settings`, wall time and the raw JSON response. The two responses were compared on `data` (every row, every column, as JSON) and `meta` (column names and types). Statements without an `ORDER BY` were compared as row sets, because ClickHouse's parallel aggregation returns them in a different order run to run — V1 against itself too.+Every query builder in `sql.ts` was executed twice against the same data — once as V1 (the `git HEAD` service/router code, `3300f2a0`) and once as V2 (core's service, which renders the builder) — through one patched ClickHouse client that captured each statement, its `query_params`, `clickhouse_settings`, wall time and the raw JSON response. The two responses were compared on `data` (every row, every column, as JSON) and `meta` (column names and types). Statements without an `ORDER BY` were compared as row sets, because ClickHouse's parallel aggregation returns them in a different order run to run — V1 against itself too.- **Date**: 2026-09-04. **Data**: local prod-copy `openpanel` (319,499,000 events, static), project `redcollege` unless noted; cases needing rows the prod-copy lacks (`cohort_members`, `events_bots` are empty there) also ran on the isolated `openpanel_test` database with 3,380 events copied from `redcollege` under project `m7-002-proof`, plus seeded cohort/bot rows (deleted afterwards).- **Machine**: single-node ClickHouse 26.1.3.52 on a 4-vCPU box — timings are directional only; production is 2 shards × 2 replicas (`docs/ENVIRONMENT.md`). `rows_read` differences on JOIN statements are ClickHouse's per-run accounting of the right-hand side, not a plan difference: the SQL text is identical up to `{pN:Type}` binding.diff --git a/packages/core/src/modules/event/src/event.sql.test.ts b/packages/core/src/modules/event/src/sql.test.tssimilarity index 98%rename from packages/core/src/modules/event/src/event.sql.test.tsrename to packages/core/src/modules/event/src/sql.test.tsindex e9ad8fa24..01c84fa72 100644--- a/packages/core/src/modules/event/src/event.sql.test.ts+++ b/packages/core/src/modules/event/src/sql.test.ts@@ -2,15 +2,14 @@// `{pN:Type}` parameter (nothing is interpolated), the optional clauses and// joins toggle exactly on V1's conditions, and `sql.id` refuses anything off// the column whitelists. Result-set equivalence against V1 is not tested here-// (these run offline); it is recorded per query in event.sql.proof.md.+// (these run offline); it is recorded per query in sql.proof.md.import { describe, expect, test } from 'bun:test';import { sql } from '@openpanel/db/src/clickhouse/sql';+import { EVENT_LIST_COLUMNS, type EventListColumn } from '../event.constants';import {botEventsCountQuery,botEventsQuery,- EVENT_LIST_COLUMNS,- type EventListColumn,eventByIdQuery,eventListQuery,eventPropertiesQuery,@@ -21,7 +20,7 @@ import {topEventNamesQuery,topOriginsQuery,topPagesQuery,-} from './event.sql';+} from './sql';const PROJECT_ID = 'proj-1';const HOSTILE = "x' OR 1=1 --";diff --git a/packages/core/src/modules/event/src/event.sql.ts b/packages/core/src/modules/event/src/sql.tssimilarity index 93%rename from packages/core/src/modules/event/src/event.sql.tsrename to packages/core/src/modules/event/src/sql.tsindex 7f681986f..86ab02472 100644--- a/packages/core/src/modules/event/src/event.sql.ts+++ b/packages/core/src/modules/event/src/sql.ts@@ -3,7 +3,7 @@// packages/trpc/src/routers/event.ts (M7-002): the SQL text is V1's, with// every value bound as a `{pN:Type}` parameter instead of an escaped literal.// Each builder's result set was diffed against V1's on the local prod-copy;-// the statements, params, row counts and timings are in event.sql.proof.md.+// the statements, params, row counts and timings are in sql.proof.md.//// Dates bind as V1's own `YYYY-MM-DD HH:mm:ss` strings on purpose: a String// param in a DateTime64 position is parsed exactly like the literal it@@ -18,6 +18,12 @@// `Array(String)`, a literal list to the planner.import { type SqlFragment, sql } from '@openpanel/db/src/clickhouse/sql';+import {+ EVENT_LIST_COLUMNS,+ type EventListColumn,+ QUERY_EVENTS_EQUALITY_COLUMNS,+ type QueryEventsEqualityColumn,+} from '../event.constants';import { formatClickhouseDate } from './dates';import {type CompiledFilterClauses,@@ -34,42 +40,6 @@ const TABLE = {eventPropertyValuesMv: 'event_property_values_mv',} as const;-/** Every column `getEventList` can project, in V1's select order. */-export const EVENT_LIST_COLUMNS = [- 'created_at',- 'project_id',- 'id',- 'name',- 'device_id',- 'profile_id',- 'session_id',- 'properties',- 'country',- 'city',- 'region',- 'longitude',- 'latitude',- 'os',- 'os_version',- 'browser',- 'browser_version',- 'device',- 'brand',- 'model',- 'path',- 'origin',- 'referrer',- 'referrer_name',- 'referrer_type',- 'imported_at',- 'sdk_name',- 'sdk_version',- 'revenue',- 'groups',-] as const;--export type EventListColumn = (typeof EVENT_LIST_COLUMNS)[number];-/** What the profile-filter join may project besides `id`. */const PROFILE_JOIN_COLUMNS = ['id',@@ -337,22 +307,6 @@ export function eventPropertyValuesQuery(query: {return sql`SELECT property_value as value FROM ${sql.id(TABLE.eventPropertyValuesMv)} WHERE project_id = ${sql.string(query.projectId)} AND name = ${sql.string(query.eventName)} AND property_key = ${sql.string(query.propertyKey)} ORDER BY created_at DESC LIMIT ${sql.uint64(EVENT_PROPERTY_VALUES_LIMIT)}`;}-/** The `= value` columns `queryEventsCore` accepts, in V1's clause order. */-export const QUERY_EVENTS_EQUALITY_COLUMNS = [- 'path',- 'referrer',- 'referrer_name',- 'referrer_type',- 'device',- 'country',- 'city',- 'os',- 'browser',-] as const;--export type QueryEventsEqualityColumn =- (typeof QUERY_EVENTS_EQUALITY_COLUMNS)[number];-export interface QueryEventsQuery {projectId: string;sessionId?: string;