Skip to content

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

ralph(M15-125): Per-module fixes: packages/core/src/modules/session - R1, R2, R4, R11, R13, R18 and the findings no rule covers

ralph13c7ab2bcec1
diff --git a/packages/core/src/modules/session/session.jobs.ts b/packages/core/src/modules/session/session.jobs.ts
index 7005dc00e..78aef8979 100644
--- a/packages/core/src/modules/session/session.jobs.ts
+++ b/packages/core/src/modules/session/session.jobs.ts
@@ -31,6 +31,7 @@ import {
createSessionEnd,
type SessionEndDeps,
type SessionEndJobData,
+ type SessionEndJobWire,
} from './src/session-end';
import { reapIdleSessions } from './src/session-reaper';
import { vacuumStaleSessions } from './src/session-vacuum';
@@ -44,10 +45,15 @@ const FLUSH_SESSIONS_INTERVAL_MS = 10_000;
const FLUSH_REPLAY_INTERVAL_MS = 10_000;
// Wire-shape check only — V1 never validated it either (see
-// jobs/compat.test.ts). `event` is an IServiceCreateEventPayload after a JSON
-// round-trip (its `createdAt` is a string; the handler replaces it anyway),
-// `snapshot` an IClickhouseSession. Typed via `z.custom` so the producer side
-// (`queues.sessions.session.add`) takes the real `SessionEndJobData`.
+// jobs/compat.test.ts), and tightening it now would reject payloads the
+// running system accepts.
+//
+// The schema's INPUT is what the producer holds (`SessionEndJobData`, a real
+// `Date` on `event.createdAt`) and its OUTPUT is what a handler is actually
+// handed after BullMQ's JSON round-trip (`SessionEndJobWire`, a string there).
+// `PayloadOf` reads the input and the handler reads the output, so the two
+// stay honest without either side casting (ADR-022 R18). The transform is a
+// type seam only — it returns the value it was given.
const sessionEndWireShape = z.object({
event: z.object({ projectId: z.string(), deviceId: z.string() }),
snapshot: z.object({
@@ -58,9 +64,11 @@ const sessionEndWireShape = z.object({
}),
});
-const sessionEndPayload = z.custom<SessionEndJobData>(
- (value) => sessionEndWireShape.safeParse(value).success
-);
+const sessionEndPayload = z
+ .custom<SessionEndJobData>(
+ (value) => sessionEndWireShape.safeParse(value).success
+ )
+ .transform((value) => value as unknown as SessionEndJobWire);
/**
* The session-end job's dependencies, bound to the run's own ctx.
diff --git a/packages/core/src/modules/session/session.rpc.test.ts b/packages/core/src/modules/session/session.rpc.test.ts
index c5db6171d..978802fa9 100644
--- a/packages/core/src/modules/session/session.rpc.test.ts
+++ b/packages/core/src/modules/session/session.rpc.test.ts
@@ -52,23 +52,6 @@ test('list rejects an unauthenticated caller before querying ClickHouse', async
});
});
-test('distinctValues rejects an unauthenticated caller before querying ClickHouse', async () => {
- const caller = await anonCaller();
- await expect(
- caller.distinctValues({ projectId: 'proj_1', field: 'country' })
- ).rejects.toMatchObject({ code: 'UNAUTHORIZED' });
-});
-
-test('distinctValues rejects a field outside SESSION_DISTINCT_FIELDS at the input boundary', async () => {
- const caller = await memberCaller();
- await expect(
- caller.distinctValues({
- projectId: 'proj_1',
- field: 'profile_id' as never,
- })
- ).rejects.toMatchObject({ code: 'BAD_REQUEST' });
-});
-
test('byId rejects an unauthenticated caller before querying ClickHouse', async () => {
const caller = await anonCaller();
await expect(
@@ -82,3 +65,14 @@ test('replayChunksFrom rejects an unauthenticated caller before querying ClickHo
caller.replayChunksFrom({ projectId: 'proj_1', sessionId: 'sess_1' })
).rejects.toMatchObject({ code: 'UNAUTHORIZED' });
});
+
+test('replayChunksFrom rejects a negative fromIndex at the input boundary', async () => {
+ const caller = await memberCaller();
+ await expect(
+ caller.replayChunksFrom({
+ projectId: 'proj_1',
+ sessionId: 'sess_1',
+ fromIndex: -1,
+ })
+ ).rejects.toMatchObject({ code: 'BAD_REQUEST' });
+});
diff --git a/packages/core/src/modules/session/session.rpc.ts b/packages/core/src/modules/session/session.rpc.ts
index a1fe464a5..11a379eeb 100644
--- a/packages/core/src/modules/session/session.rpc.ts
+++ b/packages/core/src/modules/session/session.rpc.ts
@@ -16,10 +16,8 @@ import { createTRPCRouter, protectedProcedure } from '../../rpc/base';
import { zChartEventFilter } from '../report/report.constants';
import {
getSessionById,
- getSessionDistinctValues,
getSessionList,
getSessionReplayChunksFrom,
- SESSION_DISTINCT_FIELDS,
} from './session.service';
const DEFAULT_LIST_TAKE = 50;
@@ -51,23 +49,6 @@ export const sessionRouter = createTRPCRouter({
});
}),
- distinctValues: protectedProcedure
- .input(
- z.object({
- projectId: z.string(),
- field: z.enum(SESSION_DISTINCT_FIELDS),
- })
- )
- .query(async ({ input, ctx }) => {
- await ctx.services.auth.requireProjectAccess({
- userId: ctx.session.userId,
- projectId: input.projectId,
- level: 'read',
- });
-
- return getSessionDistinctValues(ctx, input.projectId, input.field);
- }),
-
byId: protectedProcedure
.input(z.object({ sessionId: z.string(), projectId: z.string() }))
.query(async ({ input, ctx }) => {
diff --git a/packages/core/src/modules/session/session.service.ts b/packages/core/src/modules/session/session.service.ts
index f61e0edf7..2f46991fb 100644
--- a/packages/core/src/modules/session/session.service.ts
+++ b/packages/core/src/modules/session/session.service.ts
@@ -1,9 +1,8 @@
-// Ported from packages/db/src/services/session.service.ts (M7-001). That file
-// is now a re-export shim over this one, so @openpanel/db importers
-// (packages/trpc's session + event routers, apps/api's insights controller,
-// apps/start's types) keep working while V1 runs (DELEGATE PATTERN).
+// Ported from packages/db/src/services/session.service.ts (M7-001); that file
+// and its re-export shim are gone (M9-CLEANUP-001), so this is the only
+// session read path.
//
-// Every query here is a `sql` fragment (src/session.sql.ts) — the module's
+// Every query here is a `sql` fragment (src/sql.ts) — the module's
// queries are converted, one at a time with a result-set proof each, per
// ADR-013. `buildFilterWhere` is NOT converted here: it is the shared filter
// compiler, out of this task's scope; src/filter-clauses.ts is the one bridge.
@@ -13,9 +12,9 @@
// every query here (ADR-018, docs/TECH_DEBT.md §4). `getSessionsCountCached`
// is `cacheablePerDeps` (M15-004): `cacheable` keys on the call's ARGUMENTS
// (packages/redis/cachable.ts), so the caller's deps travel beside the key
-// rather than inside it, and the Redis key stays byte-identical. The remaining
-// `load*` functions are intra-package lazy imports, kept lazy for a cycle, not
-// for a client — the date helpers left that set in M15-010, because
+// rather than inside it, and the Redis key stays byte-identical. The one
+// remaining `load*` function is an intra-package lazy import, kept lazy for a
+// cycle, not for a client — the date helpers left that set in M15-010, because
// @openpanel/shared sits below this package and cannot cycle back into it.
import { getSafeJson, resolveDateRange } from '@openpanel/shared';
@@ -26,6 +25,11 @@ import type { IServiceProfile } from '../profile/profile.service';
import type { IChartEventFilter } from '../report/report.constants';
import { convertClickhouseDateToJs } from './src/dates';
import type { CompiledFilterClauses } from './src/filter-clauses';
+import {
+ type EnqueueSessionEndInput,
+ sessionEndEnqueueOptions,
+ sessionEndJobPayload,
+} from './src/session-end';
import {
hasSessionListLookback,
querySessionsQuery,
@@ -36,17 +40,12 @@ import {
sessionListQuery,
sessionReplayChunksQuery,
sessionsCountQuery,
-} from './src/session.sql';
-import {
- type EnqueueSessionEndInput,
- sessionEndEnqueueOptions,
- sessionEndJobPayload,
-} from './src/session-end';
+} from './src/sql';
export {
SESSION_DISTINCT_FIELDS,
type SessionDistinctField,
-} from './src/session.sql';
+} from './src/sql';
function loadFilterCompiler() {
return import('../chart/src/table-filter-where');
@@ -375,6 +374,10 @@ export async function getSessionReplayChunksFrom(
})
);
+ // `chunkIndex` is the row's position, not `row.chunk_index`, and `hasMore`
+ // counts rows the JSON filter may drop — so an unparseable chunk rewinds the
+ // client (docs/review/session.md, "not covered by any rule" #5). Correcting
+ // either changes what the endpoint returns, so it is reported, not fixed.
return {
data: rows
.slice(0, REPLAY_CHUNKS_PAGE_SIZE)
@@ -484,23 +487,20 @@ export function createSessionService(
deps: ServiceDeps,
_services: () => Services
) {
- return {
- byId: (
- sessionId: string,
- projectId: string
- ): Promise<IServiceSession & { hasReplay: boolean }> =>
- getSessionById(deps, sessionId, projectId),
- /**
- * Enqueue one `session_end` job, idempotent on the closed session's id.
- * The ctx.queues-based producer; V1's now-deleted apps/worker kept its own
- * @openpanel/queue producer (utils/session-handler.ts) because core could
- * not import @openpanel/queue back (see cohort.service.ts's header).
- */
- enqueueSessionEnd: async (input: EnqueueSessionEndInput): Promise<void> => {
- await deps.queues.sessions.session.add(
- sessionEndJobPayload(input),
- sessionEndEnqueueOptions(input.closedSession.id)
- );
- },
- };
+ /**
+ * Enqueue one `session_end` job, idempotent on the closed session's id.
+ * The ctx.queues-based producer; V1's now-deleted apps/worker kept its own
+ * @openpanel/queue producer (utils/session-handler.ts) because core could
+ * not import @openpanel/queue back (see cohort.service.ts's header).
+ */
+ async function enqueueSessionEnd(
+ input: EnqueueSessionEndInput
+ ): Promise<void> {
+ await deps.queues.sessions.session.add(
+ sessionEndJobPayload(input),
+ sessionEndEnqueueOptions(input.closedSession.id)
+ );
+ }
+
+ return { enqueueSessionEnd };
}
diff --git a/packages/core/src/modules/session/src/session-keys.ts b/packages/core/src/modules/session/src/keys.ts
similarity index 100%
rename from packages/core/src/modules/session/src/session-keys.ts
rename to packages/core/src/modules/session/src/keys.ts
diff --git a/packages/core/src/modules/session/src/session-lifecycle.fixtures.ts b/packages/core/src/modules/session/src/lifecycle.fixtures.ts
similarity index 100%
rename from packages/core/src/modules/session/src/session-lifecycle.fixtures.ts
rename to packages/core/src/modules/session/src/lifecycle.fixtures.ts
diff --git a/packages/core/src/modules/session/src/session-end.test.ts b/packages/core/src/modules/session/src/session-end.test.ts
index 2d2693cc7..b10edc7fb 100644
--- a/packages/core/src/modules/session/src/session-end.test.ts
+++ b/packages/core/src/modules/session/src/session-end.test.ts
@@ -6,29 +6,34 @@
import { describe, expect, mock, test } from 'bun:test';
import { testCoreConfig } from '../../../../test/config-fixture';
import type { IServiceCreateEventPayload } from '../../event/event.service';
+import {
+ fixtureSession,
+ stubLogger,
+ stubRedis,
+ stubStore,
+} from './lifecycle.fixtures';
import {
createSessionEnd,
getSessionEndJobId,
type SessionEndDeps,
+ type SessionEndJobWire,
sessionEndEnqueueOptions,
sessionEndJobPayload,
} from './session-end';
-import {
- fixtureSession,
- stubLogger,
- stubRedis,
- stubStore,
-} from './session-lifecycle.fixtures';
const snapshot = fixtureSession();
+/** The event as a handler receives it, after BullMQ's JSON round-trip. */
const payload = {
projectId: 'proj-1',
deviceId: 'dev-1',
profileId: 'dev-1',
name: 'session_end',
properties: {},
-} as unknown as IServiceCreateEventPayload;
+} as unknown as SessionEndJobWire['event'];
+
+/** The same fields on the producer side, where `createdAt` is still a Date. */
+const producerPayload = payload as unknown as IServiceCreateEventPayload;
function makeDeps(live = snapshot as ReturnType<typeof fixtureSession> | null) {
const redis = stubRedis();
@@ -157,9 +162,11 @@ describe('enqueue shape', () => {
profile_id: 'user-9',
});
- expect(sessionEndJobPayload({ payload, closedSession })).toEqual({
+ expect(
+ sessionEndJobPayload({ payload: producerPayload, closedSession })
+ ).toEqual({
event: {
- ...payload,
+ ...producerPayload,
projectId: 'proj-1',
deviceId: 'dev-9',
sessionId: 'sess-9',
diff --git a/packages/core/src/modules/session/src/session-end.ts b/packages/core/src/modules/session/src/session-end.ts
index f55df78be..2f28d22b5 100644
--- a/packages/core/src/modules/session/src/session-end.ts
+++ b/packages/core/src/modules/session/src/session-end.ts
@@ -24,7 +24,7 @@ import {
sessionEndsSkipped,
sessionEventsOnClose,
} from './session.metrics';
-import { sessionEventsQuery } from './session.sql';
+import { sessionEventsQuery } from './sql';
const SESSION_END_JOB_ID_PREFIX = 'sessionEnd:v2';
const SESSION_END_ATTEMPTS = 3;
@@ -67,6 +67,18 @@ export interface SessionEndJobData {
snapshot: IClickhouseSession;
}
+/**
+ * `SessionEndJobData` as it comes back OFF the queue (ADR-022 R18). BullMQ
+ * stores job data as JSON, so `event.createdAt` is a string by the time a
+ * handler sees it however the producer typed it — `createSessionEnd`
+ * overwrites the field before use, and this type is what stops the next
+ * reader from trusting the `Date` the producer side declares.
+ */
+export interface SessionEndJobWire {
+ event: Omit<IServiceCreateEventPayload, 'createdAt'> & { createdAt: string };
+ snapshot: IClickhouseSession;
+}
+
export function sessionEndJobPayload({
payload,
closedSession,
@@ -136,7 +148,7 @@ function isProfileBackfillEnabled(
* event, or null when nothing was emitted (extended, gone, or already done).
*/
export async function createSessionEnd(
- { event: payload, snapshot }: SessionEndJobData,
+ { event: payload, snapshot }: SessionEndJobWire,
deps: SessionEndDeps
): Promise<IClickhouseEvent | null> {
const { logger } = deps;
@@ -153,6 +165,9 @@ export async function createSessionEnd(
// Same session, unchanged → live. A different session in the slot (boundary)
// or no blob at all → the snapshot; cleanup is then id-gated to a no-op.
const sameSession = live && live.id === snapshot.id;
+ // Lexicographic, and only chronological because both sides are ClickHouse's
+ // fixed-width zero-padded `YYYY-MM-DD HH:mm:ss` — a fractional part on
+ // either would silently change what this compares.
if (sameSession && live.ended_at > snapshot.ended_at) {
sessionEndsSkipped.inc({ reason: 'extended_after_enqueue' });
logger.info(
diff --git a/packages/core/src/modules/session/src/session-reaper.test.ts b/packages/core/src/modules/session/src/session-reaper.test.ts
index c998ec414..fdb6ccf8d 100644
--- a/packages/core/src/modules/session/src/session-reaper.test.ts
+++ b/packages/core/src/modules/session/src/session-reaper.test.ts
@@ -9,7 +9,7 @@ import {
stubLogger,
stubRedis,
stubStore,
-} from './session-lifecycle.fixtures';
+} from './lifecycle.fixtures';
import { reapIdleSessions, type SessionReaperDeps } from './session-reaper';
function makeDeps(
diff --git a/packages/core/src/modules/session/src/session-reaper.ts b/packages/core/src/modules/session/src/session-reaper.ts
index 14c699dea..9d350330c 100644
--- a/packages/core/src/modules/session/src/session-reaper.ts
+++ b/packages/core/src/modules/session/src/session-reaper.ts
@@ -3,6 +3,11 @@
import type { Logger } from '../../../logger';
import type { IServiceCreateEventPayload } from '../../event/event.service';
import type { IClickhouseSession } from '../session.service';
+import {
+ DEFAULT_SESSION_TIMEOUT_MS,
+ PROJECTS_SET_KEY,
+ wallclockSetKey,
+} from './keys';
import type { SessionRuntime } from './runtime';
import {
sessionEndsEnqueued,
@@ -10,11 +15,6 @@ import {
sessionsReaperOrphans,
} from './session.metrics';
import type { EnqueueSessionEndInput } from './session-end';
-import {
- DEFAULT_SESSION_TIMEOUT_MS,
- PROJECTS_SET_KEY,
- wallclockSetKey,
-} from './session-keys';
const DEFAULT_REAPER_BATCH_SIZE = 5000;
const LOCK_TTL_SECONDS = 60;
diff --git a/packages/core/src/modules/session/src/session-vacuum.test.ts b/packages/core/src/modules/session/src/session-vacuum.test.ts
index 10f33ace5..03661ccfa 100644
--- a/packages/core/src/modules/session/src/session-vacuum.test.ts
+++ b/packages/core/src/modules/session/src/session-vacuum.test.ts
@@ -9,7 +9,7 @@ import {
stubLogger,
stubRedis,
stubStore,
-} from './session-lifecycle.fixtures';
+} from './lifecycle.fixtures';
import { vacuumStaleSessions } from './session-vacuum';
function makeDeps(
diff --git a/packages/core/src/modules/session/src/session-vacuum.ts b/packages/core/src/modules/session/src/session-vacuum.ts
index d2f22f157..a7aa275ab 100644
--- a/packages/core/src/modules/session/src/session-vacuum.ts
+++ b/packages/core/src/modules/session/src/session-vacuum.ts
@@ -1,9 +1,9 @@
// Ported from apps/worker/src/jobs/cron.session-vacuum.ts (M7-001).
import type { Logger } from '../../../logger';
+import { PROJECTS_SET_KEY, wallclockSetKey } from './keys';
import type { SessionRuntime } from './runtime';
import { sessionsVacuumed } from './session.metrics';
-import { PROJECTS_SET_KEY, wallclockSetKey } from './session-keys';
const DEFAULT_VACUUM_BATCH_SIZE = 1000;
// Much larger than the reaper deadman, so it never races with normal reaping.
diff --git a/packages/core/src/modules/session/src/session.sql.test.ts b/packages/core/src/modules/session/src/sql.test.ts
similarity index 99%
rename from packages/core/src/modules/session/src/session.sql.test.ts
rename to packages/core/src/modules/session/src/sql.test.ts
index ca261668f..5dac08717 100644
--- a/packages/core/src/modules/session/src/session.sql.test.ts
+++ b/packages/core/src/modules/session/src/sql.test.ts
@@ -17,7 +17,7 @@ import {
sessionListQuery,
sessionReplayChunksQuery,
sessionsCountQuery,
-} from './session.sql';
+} from './sql';
const PROJECT_ID = 'proj-1';
diff --git a/packages/core/src/modules/session/src/session.sql.ts b/packages/core/src/modules/session/src/sql.ts
similarity index 100%
rename from packages/core/src/modules/session/src/session.sql.ts
rename to packages/core/src/modules/session/src/sql.ts