OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.
ralph(M10-009): Flip both dependency-cruiser rules to error at 0 violations; requestId proof through the converted path; bun test wall-time recorded
ralph04b211e360ab
diff --git a/.dependency-cruiser.cjs b/.dependency-cruiser.cjsindex 17c684987..781ac7cbf 100644--- a/.dependency-cruiser.cjs+++ b/.dependency-cruiser.cjs@@ -93,53 +93,80 @@ module.exports = {forbidden: [{name: 'core-uses-ctx-not-db-internals',- severity: 'warn',+ severity: 'error',comment:- 'M10-001 (docs/TECH_DEBT.md §4 step 4): packages/core reaches Postgres/ClickHouse ' +- 'through ctx.db / ctx.ch / ServiceDeps, never by importing @openpanel/db itself, so a ' +- 'requestId minted at the edge keeps reaching the query (ADR-018). import type stays ' +- "allowed (dependencyTypesNot: ['type-only']) — that is what keeps `bun test` offline. " +- 'packages/core/src/context.ts is the one named exception, excluded by path rather than ' +- "by widening dependencyTypesNot: its Db / ClickHouseClient fields are `typeof " +- "import('@openpanel/db/...')` type queries, which dependency-cruiser tags " ++ 'M10-001/M10-009 (docs/TECH_DEBT.md §4 steps 2 and 4): packages/core reaches ' ++ 'Postgres/ClickHouse through ctx.db / ctx.ch / ServiceDeps, never by acquiring a ' ++ 'client from @openpanel/db itself, so a requestId minted at the edge keeps reaching ' ++ "the query (ADR-018). import type stays allowed (dependencyTypesNot: ['type-only']) " ++ '— that is what keeps `bun test` offline. M10-009 flipped this from warn to error at ' ++ '0 violations; the four exemptions below are each a named seam, not a blanket. ' ++ 'FROM: (1) packages/core/src/context.ts — its Db / ClickHouseClient fields are ' ++ "`typeof import('@openpanel/db/...')` type queries, which dependency-cruiser tags " +"['undetermined', 'type-import'] rather than 'type-only' (verified via --output-type " +- 'json) — a genuinely type-level reference. Widening the exemption to ' +- "'type-import' instead of naming the file is exactly what the previous attempt at this " +- 'task did, and it silently swallowed a real value-import: ' +- 'packages/core/src/buffers/clickhouse.ts has a `typeof import(...)` type query (line 8) ' +- 'AND a genuine runtime `import(...)` dynamic import (line 13) of the same resolved ' +- "module; dependency-cruiser folds both references between the same two files into ONE " +- "edge and — reproduced via --output-type json with an isolated probe file mirroring " +- "that structure — the merge drops the 'dynamic-import' tag entirely, leaving only " +- "['undetermined', 'type-import']. Exempting 'type-import' wholesale therefore hides " +- 'that seam from the baseline instead of counting it; buffers/clickhouse.ts is left in ' +- 'scope for M10-002..008 to fix. context.ts has no such merge (no runtime import of ' +- '@openpanel/db anywhere in the file), so excluding it by path is safe. Landed at warn ' +- 'with the baseline violation count recorded in docs/TECH_DEBT.md; M10-009 flips it to ' +- 'error once the count is 0.',+ 'json), i.e. a genuinely type-level reference; excluded by path rather than by ' ++ 'widening dependencyTypesNot, because dependency-cruiser folds a `typeof import()` ' ++ 'and a runtime `import()` of the same module into ONE edge and drops the ' ++ "'dynamic-import' tag when it does, so exempting 'type-import' wholesale would hide " ++ 'real value imports. (2) packages/core/src/v1-compat.ts — THE declared composition ' ++ "seam (ADR-007; that file's own header). It is where the boot scope registers the " ++ 'deps it already built, and the only thing behind it is the lazy fallback for a ' ++ "process that never built AppDeps (packages/trpc's own suites). Nothing holding a " ++ 'Ctx comes through it. It is deleted whole with packages/trpc at P10. ' ++ '(3) packages/core/src/code-migrations/** — one-shot CLI scripts run by ' ++ '`migrate.ts` (`pnpm migrate:deploy:code`), outside the app, with no request and no ' ++ 'Ctx to lose; ADR-007 puts code-migrations/ in core and keeps clickhouse/migration.ts ' ++ 'in packages/db, so the edge is what the ADR describes. (4) *.test.ts — a test has no ' ++ "request, and M10-009's own measurement of this drift is defined as " ++ '`grep ... | grep -v .test.ts`. Tests that need a real client against openpanel_test ' ++ 'take it directly; two of them (shared/ch-tables.parity.test.ts, ' ++ "shared/ch-dates.parity.test.ts) exist precisely to police core's copies of " ++ "@openpanel/db's table map and date helpers against the originals. " ++ 'TO: the three modules under packages/db that build QUERY TEXT and hold no client — ' ++ 'clickhouse/sql.ts (ADR-013 fixes the `sql` tag AT that path by name), ' ++ 'clickhouse/query-builder.ts (`clix(client, tz)` takes the client as an argument) and ' ++ 'sql-builder.ts (`createSqlBuilder()` returns strings). Importing one of those cannot ' ++ 'lose a request scope, because the client is still whatever the caller passes — and ' ++ 'in core that is always deps.ch. prisma-client.ts, clickhouse/client.ts, logger.ts ' ++ 'and the barrel are NOT exempt: those are where a second client comes from.',from: {path: '^packages/core/src/',- pathNot: '^packages/core/src/context\\.ts$',+ pathNot: [+ '^packages/core/src/context\\.ts$',+ '^packages/core/src/v1-compat\\.ts$',+ '^packages/core/src/code-migrations/',+ '\\.test\\.ts$',+ ],},to: {path: '^packages/db/',+ pathNot: [+ '^packages/db/src/clickhouse/sql\\.ts$',+ '^packages/db/src/clickhouse/query-builder\\.ts$',+ '^packages/db/src/sql-builder\\.ts$',+ ],dependencyTypesNot: ['type-only'],},},{name: 'core-no-self-barrel',- severity: 'warn',+ severity: 'error',comment:- 'M10-001 (docs/TECH_DEBT.md §4): a file under packages/core/src/** imports its siblings ' +- 'by relative path, never through its own package barrel (@openpanel/core) — a self-import ' +- 'is always resolvable as a relative import and importing the barrel instead just risks ' +- 'reintroducing the exact resolution cycles the barrel is meant to avoid for external ' +- 'consumers. `to.path` matches the RESOLVED path (see header comment), so this targets ' +- 'packages/core/package.json\'s "." export target (./src/index.ts) directly rather than ' +- 'the bare specifier string, which would never match a resolved path. import type stays ' +- 'allowed. Landed at warn with the baseline violation count recorded in docs/TECH_DEBT.md; ' +- 'M10-009 flips it to error once the count is 0.',- from: { path: '^packages/core/src/' },+ 'M10-001/M10-009 (docs/TECH_DEBT.md §4): a file under packages/core/src/** imports its ' ++ 'siblings by relative path, never through its own package barrel (@openpanel/core) — a ' ++ 'self-import is always resolvable as a relative import and importing the barrel instead ' ++ 'just risks reintroducing the exact resolution cycles the barrel is meant to avoid for ' ++ 'external consumers. `to.path` matches the RESOLVED path (see header comment), so this ' ++ 'targets packages/core/package.json\'s "." export target (./src/index.ts) directly rather ' ++ 'than the bare specifier string, which would never match a resolved path. import type ' ++ 'stays allowed. M10-009 flipped this from warn to error at 0 violations. *.test.ts is ' ++ 'exempt: a barrel test (src/index.test.ts) has to import the barrel to test it, and the ' ++ "mcp/gsc suites `mock.module('@openpanel/core', ...)` deliberately — that is a mocking " ++ 'idiom, not a production import cycle.',+ from: {+ path: '^packages/core/src/',+ pathNot: '\\.test\\.ts$',+ },to: {path: '^packages/core/src/index\\.ts$',dependencyTypesNot: ['type-only'],diff --git a/apps/api/src/main.ts b/apps/api/src/main.tsindex 91a131da3..d23826bf8 100644--- a/apps/api/src/main.ts+++ b/apps/api/src/main.ts@@ -175,6 +175,10 @@ function bufferDeps(producers: QueueProducerHandle): BufferDeps {return {createLogger: (name) => logger.child({ name }),isCronPaused: async () => (cron ? await cron.isPaused() : false),+ // M10-009: the boot scope's ClickHouse client, so a buffer flush logs+ // under the same client every service reaches as `deps.ch` instead of+ // constructing its own (docs/TECH_DEBT.md §4).+ ch,};}diff --git a/docs/TECH_DEBT.md b/docs/TECH_DEBT.mdindex 15327e2eb..1a16395d0 100644--- a/docs/TECH_DEBT.md+++ b/docs/TECH_DEBT.md@@ -810,3 +810,185 @@ this wave.`@openpanel/db/src/clickhouse/client`, and `ingest/src/incoming-event-handler.ts`still reaches `sessionBuffer` through `loadDbBuffers()`. Both are the`session`/`ingest` modules' own conversion, not this wave's.++### 2026-09-06 — M10-009, both rules flipped to `error` at 0 violations++Ran by: ralph (M10-009). This closes §4 steps 3, 4 and 5 of the controller's+`docs/TECH_DEBT.md`.++#### The rules now fire++| Rule | Severity | Violations |+|---|---|---:|+| `core-uses-ctx-not-db-internals` | `error` | **0** |+| `core-no-self-barrel` | `error` | **0** |++```+$ pnpm run check:deps+✔ no dependency violations found (1998 modules, 8070 dependencies cruised)+```++Four exemptions carry the rule, each named in `.dependency-cruiser.cjs` with+its reason — not a widened `dependencyTypesNot`, which would swallow real+value imports (see the M10-001 entry above for the merge that proved it):++- **from `packages/core/src/context.ts`** — `typeof import('@openpanel/db/…')`+ type queries, tagged `['undetermined','type-import']` rather than+ `'type-only'`.+- **from `packages/core/src/v1-compat.ts`** — THE declared composition seam+ (§4 step 4's own wording). Deleted whole with `packages/trpc` at P10.+- **from `packages/core/src/code-migrations/**`** — one-shot CLI scripts run by+ `migrate.ts` outside the app; no request, no `Ctx` to lose.+- **from `*.test.ts`** — a test has no request, and the `grep` measurement+ below is itself defined as `| grep -v .test.ts`.+- **to** `clickhouse/sql.ts`, `clickhouse/query-builder.ts` and+ `sql-builder.ts` — the three `packages/db` modules that build query TEXT and+ hold no client (`clix(client, tz)` takes the client as an argument).+ `prisma-client.ts`, `clickhouse/client.ts`, `logger.ts` and the barrel are+ NOT exempt: those are where a second client comes from.++#### Re-measured — `@openpanel/db` reached from `packages/core/src`++Non-test files, 2026-09-06:++| Measurement | Count | Where the remainder is |+|---|---:|---|+| `grep -rn "import('@openpanel/db" packages/core/src --include=*.ts \| grep -v .test.ts \| wc -l` | **22** | 7 prose comments, 9 `typeof import()` type queries (2 in `context.ts`, 7 in `v1-compat.ts`), **6 runtime imports, all in `v1-compat.ts`** |+| …of those, outside `v1-compat.ts` / `context.ts` | **0** | — |+| `grep -rn "from '@openpanel/db" packages/core/src --include=*.ts \| grep -v .test.ts \| wc -l` | **77** | 20 `import type`, 34 in `code-migrations/**`, 23 value imports of the query-text builders |+| …value imports of a **client-bearing** module (`prisma-client`, `clickhouse/client`, the barrel) outside `code-migrations/**` | **0** | — |++The controller's 2026-09-05 baseline was 63 dynamic + 80 static value imports;+both drift numbers are now 0 outside the two seams the rule names. The 23 that+remain are `sql`/`clix`/`createSqlBuilder` — pure text, no client, so a+requestId cannot be lost through them (ADR-013 fixes the `sql` tag at that+path by name).++What made the last of them go: the buffers took the boot scope's client as+`BufferDeps.ch` (`apps/api/src/main.ts` hands in the same `ch` every service+gets as `deps.ch`), `buffers/clickhouse.ts`'s `loadClickHouse()` seam was+deleted, and `shared/ch-tables.ts` / `shared/ch-dates.ts` hold core's own copy+of the table map and the date helpers — policed against `@openpanel/db`'s+originals by `ch-tables.parity.test.ts` and `ch-dates.parity.test.ts`, so the+copies cannot drift silently.++The violations that were still standing after M10-006 (118 + 56 = 174 edges)+were closed here, in two groups:++- `core-uses-ctx-not-db-internals` — 41 files (production plus the tests that+ mocked what they imported). The buffers (8) via+ `BufferDeps.ch` + `shared/ch-tables.ts` / `shared/ch-dates.ts`; the+ still-unconverted service functions in `cohort`, `gsc`, `import`, `insight`,+ `integration`, `notification`, `onboarding`, `organization`, `project`,+ `session` and `widget` onto `deps.db` / `deps.ch`; `shared/slug-id.ts` and+ `shared/access-lookups.ts` onto the v1-compat seam (their `cacheable`+ signatures cannot take a leading `deps`).+- `core-no-self-barrel` — 60 files stopped importing `@openpanel/core` from+ inside `packages/core`: 26 under `modules/mcp/**`, 13 under+ `modules/assistant/**`, 7 under `modules/import/**`, and one or two each in+ `cohort`, `gsc`, `insight`, `notification`, `onboarding`, `organization`,+ `overview` and `session`. Each now imports the sibling module by relative+ path, or `../../v1-compat` where the caller has no `Ctx` (mcp's and the chat+ agent's tool handlers have a third-party-fixed signature).++No genuine cycle needed a per-file exception: the two lazy edges that remain+(`base-buffer.ts` → `v1-compat`, `shared/access-lookups.ts` → `v1-compat`)+both point AT the declared seam, and each names the cycle in a comment above+the loader.++#### `services.ts` registers every `*.service.ts`++```+$ ls packages/core/src/modules/*/*.service.ts | wc -l+36+$ grep -rh "^export function create[A-Za-z]*Service" packages/core/src/modules/*/*.service.ts | wc -l+36+$ grep -cE "^ [a-z]+: create[A-Za-z]*Service\(" packages/core/src/services.ts+36+$ grep -rn "export class .*Service" packages/core/src --include=*.ts | wc -l+0+$ grep -rn "_deps" packages/core/src --include=*.ts | grep -v ".test.ts" | wc -l+1+```++The four chart sub-modules (`conversion`, `funnel`, `retention`, `sankey`) got+their own keys in this task — `chart` still composes them for the facade its+own callers use, and both bind the same stateless closures. Three factories+that genuinely need nothing (`createAuthService`, `createAssistantService`,+`createMcpService`) now say so on the signature instead of ignoring a `_deps`+argument; their reasons are on each factory.++The one `_deps` left is `http/client-auth.ts`'s `authenticateClient(_deps:+AppDeps, …)`: its single lookup is `getClientByIdCached`, a `cacheable` whose+key is derived from the call's arguments, so it cannot take a leading `deps`+(same constraint as `shared/access-lookups.ts`). Named, not fixed.++#### §4 step 3 — `bun test` wall time, before and after the wave++`cd packages/core && time bun test`, same box, 2 runs each. Baseline is the+parent of M10-001 (`49a6388e`) measured in a `git worktree`, never a checkout+of the working tree.++| Tree | Run 1 | Run 2 | Tests |+|---|---:|---:|---|+| `49a6388e` (parent of M10-001) | **11.739s** | **14.521s** | 1447 pass, 12 skip, 0 fail — 1459 across 147 files |+| HEAD (M10-009) | **14.330s** | **13.202s** | 1466 pass, 12 skip, 0 fail — 1478 across 151 files |++No target was set and none is claimed: 13.1s mean before, 13.8s mean after,+with a 2.8s spread inside the baseline pair alone — the difference is inside+the noise of this box, while the suite grew by 19 tests and 4 files. The lazy+`import('@openpanel/db/…')` pattern was justified by test cost; removing it+cost nothing measurable.++#### §4 step 5 — the requestId proof, on all three paths++`packages/core/test/request-id-end-to-end.test.ts` (M10-009 merged+`request-id-chart-query.test.ts` and `request-id-ingest.test.ts` into it, so+the three paths cannot drift into three files that disagree):++```+✓ one requestId spans the route log, the enqueue, the job and its follow-up enqueue+✓ a minted requestId travels the same four hops, and does not leak into the next request+✓ a chart query made through a request-scoped ctx logs the request's own requestId+✓ two concurrent requests do not share a requestId on their ClickHouse calls+✓ the envelope's requestId reaches the handler's logger, its buffer write and the session_end it enqueues+✓ a second message is scoped to its own id, and an envelope without one still gets scoped+```++HTTP → job (P2-010's original), chart query (M10-003) and Kafka/ingest handler+(M10-006), in one file.++#### A test-isolation bug this task had to fix to go green++`cd packages/core && bun test` (bare, one shared module registry) failed 6+chart-integration assertions with `deps.db.project.findUniqueOrThrow is not a+function`, intermittently — only when the 5-minute Redis cache behind+`getOrganizationByProjectIdCached` was cold. Cause: `v1-compat.ts` MEMOIZES+its fallback `ServiceDeps`, so a file that resolves it while a+`mock.module('@openpanel/db/src/prisma-client', …)` is installed pins the+mocked client for every later FILE in the process — restoring the module+registry in `afterAll` does not undo it. Reproduced deterministically with+`bun test src/modules/mcp/mcp.service.test.ts src/modules/chart/chart.service.test.ts`+(6 fail before, 0 after). Fixed at the leak: the four files that mock+`prisma-client` now also call `resetV1CompatServicesForTests()` in `afterAll`+(the idiom `mcp/src/tools/dashboard-management.test.ts` already used), and+`import/import.service.test.ts` dropped its `@openpanel/db/src/clickhouse/client`+module mock entirely — the subject takes `deps.ch` now, so the mock only+existed to leak.++**Verification, all run by ralph on 2026-09-06:**++- `pnpm run check:deps` — 0 violations, both rules at `error`.+- `pnpm run typecheck` — all 25 workspaces, `Done`.+- `cd packages/core && bun test` (bare): **1466 pass, 12 skip, 0 fail**,+ `Ran 1478 tests across 151 files.`, twice.+- `verification/harness start` → `cd apps/api && pnpm run e2e:sessions`:+ **29/29 checks passed**.+- `verification/golden/compare.sh`: **`passed: 131/137`, 0 diffs in the 131+ validated**; the other 6 are the calendar-stale clock-anchored cases the+ harness skips itself (`captured on 2026-09-05, replayed on 2026-09-06`) —+ the same six M10-003 and M10-005 recorded. **137/137 is not reachable on a+ day that is not the capture day**; re-capturing is a verify task, not an+ implement one.+- `verification/harness stop`, then `verification/full.sh` — green, with the+ documented pre-P11 `contracts/sdk/dist-gate.sh` exception.diff --git a/packages/core/src/buffers/base-buffer.ts b/packages/core/src/buffers/base-buffer.tsindex a02d0d24c..35e686aae 100644--- a/packages/core/src/buffers/base-buffer.ts+++ b/packages/core/src/buffers/base-buffer.ts@@ -2,8 +2,17 @@ import { Readable } from 'node:stream';import type { ClickHouseSettings } from '@clickhouse/client';import { getRedisCache } from '@openpanel/redis';import type { Logger } from '../logger';+import type { ServiceDeps } from '../services';+import { type ChScope, chQuery } from '../shared/ch-query';import { generateSecureId } from '../shared/id';+// GENUINE CYCLE, kept lazy: v1-compat.ts -> services.ts -> (every service) ->+// this module. The fallback only fires for `BufferDeps` built without a `ch`+// — see that field's comment.+function loadCompatCh(): Promise<ServiceDeps['ch']> {+ return import('../v1-compat').then((m) => m.compatCh());+}+/*** What a buffer needs from the boot scope. Buffers are built once by* `createBuffers(deps)` and hung on `AppDeps` — never module singletons, so a@@ -16,6 +25,21 @@ export interface BufferDeps {* stub instead of spawning one transport worker thread per buffer.*/createLogger(name: string): Logger;+ /**+ * The boot scope's ClickHouse client — the same round-robin/retry proxy+ * every service reaches as `deps.ch`. M10-009: the buffers used to lazily+ * `import('@openpanel/db/src/clickhouse/client')` for it, which constructed+ * a second client (and a second pino transport) outside any scope.+ *+ * OPTIONAL, deliberately: `@openpanel/queue`'s process-wide buffer+ * singleton (packages/queue/src/buffers.ts) is a pre-`AppDeps` caller that+ * has no client to hand in and lives outside this wave's scope. When it is+ * absent, `resolveCh()` reaches the boot scope through the declared+ * v1-compat seam — which, once `main.ts` has registered, is the very same+ * client, not a second one. Both the field and the fallback die with+ * `packages/queue` at P10.+ */+ ch?: ServiceDeps['ch'];/*** Asked before every flush. ADR-005's `ProducerHandle.bullQueues` escape* hatch, narrowed to the one thing the buffers ask BullMQ directly: pausing@@ -100,6 +124,17 @@ export class BaseBuffer {skipReason?: string;} = {};+ /** The scope's ClickHouse client, for a subclass's insert path. */+ protected async resolveCh(): Promise<ServiceDeps['ch']> {+ return this.deps.ch ?? (await loadCompatCh());+ }++ /** `chQuery` bound to this buffer's client and its own logger. */+ protected async chQuery<T extends object>(query: string): Promise<T[]> {+ const scope: ChScope = { ch: await this.resolveCh(), logger: this.logger };+ return chQuery<T>(scope, query);+ }+protected getClickhouseSettings(): ClickHouseSettings {if (process.env.BUFFER_ASYNC_INSERTS) {return {@@ -119,7 +154,7 @@ export class BaseBuffer {public addObserver: AddObserver | null = null;constructor(- private readonly deps: BufferDeps,+ protected readonly deps: BufferDeps,options: {name: string;onFlush: () => Promise<void>;diff --git a/packages/core/src/buffers/bot-buffer.ts b/packages/core/src/buffers/bot-buffer.tsindex 5d917c336..7270f07c8 100644--- a/packages/core/src/buffers/bot-buffer.ts+++ b/packages/core/src/buffers/bot-buffer.ts@@ -1,7 +1,7 @@import { getRedisCache, type Redis } from '@openpanel/redis';import type { IClickhouseBotEvent } from '../modules/event/event.service';+import { TABLE_NAMES } from '../shared/ch-tables';import { BaseBuffer, type BufferDeps } from './base-buffer';-import { loadClickHouse } from './clickhouse';export class BotBuffer extends BaseBuffer {private batchSize = process.env.BOT_BUFFER_BATCH_SIZE@@ -60,7 +60,7 @@ export class BotBuffer extends BaseBuffer {// Raw passthrough: each Redis entry is already a valid JSONEachRow// line. Streaming raw strings skips JSON.parse + the client's// re-stringify on the hot path.- const { ch, TABLE_NAMES } = await loadClickHouse();+ const ch = await this.resolveCh();const chStart = performance.now();await ch.insert({table: TABLE_NAMES.events_bots,diff --git a/packages/core/src/buffers/clickhouse-failover.test.ts b/packages/core/src/buffers/clickhouse-failover.test.tsindex 525061712..849c4f469 100644--- a/packages/core/src/buffers/clickhouse-failover.test.ts+++ b/packages/core/src/buffers/clickhouse-failover.test.ts@@ -1,10 +1,19 @@import { afterAll, describe, expect, it } from 'bun:test';-import { loadClickHouse } from './clickhouse';++// M10-009: the buffers take the boot scope's client as `BufferDeps.ch` now, so+// the `loadClickHouse()` seam this file used to call through is gone. What it+// proves is unchanged — the round-robin/retry client the buffers are handed+// (`ch` / `chQuery`, `@openpanel/db`'s exports) must survive a dead node — so+// the subject is imported directly here. This is a test file, which+// `core-uses-ctx-not-db-internals` exempts.+function loadClickHouse() {+ return import('@openpanel/db/src/clickhouse/client');+}/*** ADR-013 R1 / M8-006: the deliberate ECONNREFUSED to a dead ClickHouse node- * must be caught and retried (sin-binning) at the seam buffers actually call- * through — `loadClickHouse()` — exactly as packages/db's+ * must be caught and retried (sin-binning) on the client the buffers are+ * handed as `BufferDeps.ch`, exactly as packages/db's* `sql.round-robin.clickhouse.test.ts` proves for `chQuery`/`chQueryWithMeta`* directly. That file is NOT touched here; this extends the same proof onto* the path M8-001 moved: `ch.insert` reached via the core buffer seam.@@ -82,7 +91,7 @@ function restoreClickhouseEnv(): void {}}-// `./clickhouse` lazily dynamic-imports `@openpanel/db/src/clickhouse/client`+// `loadClickHouse()` above lazily imports `@openpanel/db/src/clickhouse/client`// on first call, which reads CLICKHOUSE_URL once at module evaluation — so// these must be set before this file's first `loadClickHouse()` call.process.env.CLICKHOUSE_URL = `${DEAD_NODE_URL}/${CLICKHOUSE_TEST_DATABASE},${LIVE_NODE_URL}`;diff --git a/packages/core/src/buffers/clickhouse.ts b/packages/core/src/buffers/clickhouse.tsdeleted file mode 100644index a6824616e..000000000--- a/packages/core/src/buffers/clickhouse.ts+++ /dev/null@@ -1,15 +0,0 @@-// @openpanel/db's clickhouse/client.ts builds a real client and a real pino-// logger (with a pino-pretty worker thread) the moment it is imported, and-// `createBuffers` sits on the curated barrel — a static import here would pay-// that once per `bun test --isolate` file. Same choice insight.service.ts and-// event.service.ts made, for the same reason. Resolved once per process; a-// flush pays one already-settled promise.--type ClickHouseModule = typeof import('@openpanel/db/src/clickhouse/client');--let pending: Promise<ClickHouseModule> | undefined;--export function loadClickHouse(): Promise<ClickHouseModule> {- pending ??= import('@openpanel/db/src/clickhouse/client');- return pending;-}diff --git a/packages/core/src/buffers/event-buffer.test.ts b/packages/core/src/buffers/event-buffer.test.tsindex 910ced559..fddbd9f42 100644--- a/packages/core/src/buffers/event-buffer.test.ts+++ b/packages/core/src/buffers/event-buffer.test.ts@@ -1,22 +1,21 @@import { afterAll, beforeEach, describe, expect, it, mock } from 'bun:test';import type { Readable } from 'node:stream';import { getRedisCache } from '@openpanel/redis';-import { stubBufferDeps } from '../../test/buffer-fixtures';+import { bufferDepsWithCh } from '../../test/buffer-fixtures';-// `./clickhouse` is the one seam the buffers reach ClickHouse through, and it-// is the right thing to replace: mocking `@openpanel/db/src/clickhouse/client`-// instead would need the real module imported first (to spread and restore it,-// since `mock.module` replaces a specifier process-wide without `--isolate`),-// and importing it builds a client and a pino transport worker thread per test-// file. This module has one export, so the replacement is complete rather than-// partial — and it is restored in afterAll regardless.-const realClickhouse = { ...(await import('./clickhouse')) };+const realChQuery = { ...(await import('../shared/ch-query')) };const chInsert = mock(async (_options: unknown): Promise<unknown> => undefined);const chQuery = mock(async (_sql: string): Promise<unknown[]> => []);-mock.module('./clickhouse', () => ({- loadClickHouse: () => Promise.resolve({ ch: { insert: chInsert }, chQuery }),+// M10-009: the client comes in as `BufferDeps.ch` and reads go through core's+// own `chQuery` (shared/ch-query.ts) — so the insert path needs no module mock+// at all, and the read path mocks one core module instead of+// `@openpanel/db/src/clickhouse/client` (whose import builds a real client and+// a pino transport worker thread per test file).+mock.module('../shared/ch-query', () => ({+ ...realChQuery,+ chQuery: (_scope: unknown, sql: string) => chQuery(sql),}));const { EventBuffer, extractProjectId } = await import('./event-buffer');@@ -49,14 +48,14 @@ beforeEach(async () => {// every file in one process, and closing the singleton takes it away from the// files that run next (V1's vitest isolated per file, so it could).afterAll(() => {- mock.module('./clickhouse', () => realClickhouse);+ mock.module('../shared/ch-query', () => realChQuery);});describe('EventBuffer', () => {let eventBuffer: EventBuffer;beforeEach(() => {- eventBuffer = new EventBuffer(stubBufferDeps);+ eventBuffer = new EventBuffer(bufferDepsWithCh({ insert: chInsert }));});it('adds regular event directly to buffer queue', async () => {@@ -224,7 +223,7 @@ describe('EventBuffer', () => {it('processes buffer with chunking', async () => {const prev = process.env.EVENT_BUFFER_CHUNK_SIZE;process.env.EVENT_BUFFER_CHUNK_SIZE = '2';- const eb = new EventBuffer(stubBufferDeps);+ const eb = new EventBuffer(bufferDepsWithCh({ insert: chInsert }));for (let i = 0; i < 4; i++) {eb.add({diff --git a/packages/core/src/buffers/event-buffer.ts b/packages/core/src/buffers/event-buffer.tsindex 896107dd2..ec435db96 100644--- a/packages/core/src/buffers/event-buffer.ts+++ b/packages/core/src/buffers/event-buffer.ts@@ -1,7 +1,6 @@import { getRedisCache, publishEvent } from '@openpanel/redis';import type { IClickhouseEvent } from '../modules/event/event.service';import { BaseBuffer, type BufferDeps } from './base-buffer';-import { loadClickHouse } from './clickhouse';const PROJECT_ID_NEEDLE = '"project_id":"';@@ -226,7 +225,7 @@ export class EventBuffer extends BaseBuffer {}}- const { ch } = await loadClickHouse();+ const ch = await this.resolveCh();const chStart = performance.now();await this.parallelLimit(this.chunks(queueEvents, this.chunkSize),@@ -257,8 +256,7 @@ export class EventBuffer extends BaseBuffer {}public async getActiveVisitorCount(projectId: string): Promise<number> {- const { chQuery } = await loadClickHouse();- const rows = await chQuery<{ count: number }>(+ const rows = await this.chQuery<{ count: number }>(`SELECT uniq(profile_id) AS countFROM eventsWHERE project_id = '${projectId}'diff --git a/packages/core/src/buffers/group-buffer.ts b/packages/core/src/buffers/group-buffer.tsindex 0462fb469..442fd7be9 100644--- a/packages/core/src/buffers/group-buffer.ts+++ b/packages/core/src/buffers/group-buffer.ts@@ -2,9 +2,10 @@ import { toDots } from '@openpanel/common';import { getRedisCache, type Redis } from '@openpanel/redis';import shallowEqual from 'fast-deep-equal';import sqlstring from 'sqlstring';+import { formatClickhouseDate } from '../shared/ch-dates';+import { TABLE_NAMES } from '../shared/ch-tables';import { getSafeJson } from '../shared/json';import { BaseBuffer, type BufferDeps } from './base-buffer';-import { loadClickHouse } from './clickhouse';type IGroupBufferEntry = {project_id: string;@@ -79,8 +80,7 @@ export class GroupBuffer extends BaseBuffer {projectId: string,id: string): Promise<IGroupCacheEntry | null> {- const { chQuery, TABLE_NAMES } = await loadClickHouse();- const rows = await chQuery<IGroupCacheEntry>(`+ const rows = await this.chQuery<IGroupCacheEntry>(`SELECT project_id, id, type, name, properties, created_atFROM ${TABLE_NAMES.groups} FINALWHERE project_id = ${sqlstring.escape(projectId)}@@ -93,7 +93,6 @@ export class GroupBuffer extends BaseBuffer {async add(input: IGroupBufferInput): Promise<void> {return this.timeAdd(async () => {try {- const { formatClickhouseDate } = await loadClickHouse();const cacheKey = this.getCacheKey(input.projectId, input.id);const existing =@@ -176,7 +175,7 @@ export class GroupBuffer extends BaseBuffer {// Raw passthrough: each Redis entry is already a valid JSONEachRow// line. Streaming raw strings to CH skips JSON.parse + the client's// re-stringify on the hot path.- const { ch, TABLE_NAMES } = await loadClickHouse();+ const ch = await this.resolveCh();const chStart = performance.now();await this.parallelLimit(this.chunks(items, this.chunkSize), (chunk) =>ch.insert({diff --git a/packages/core/src/buffers/profile-backfill-buffer.ts b/packages/core/src/buffers/profile-backfill-buffer.tsindex 31b050ed6..b2ade70ef 100644--- a/packages/core/src/buffers/profile-backfill-buffer.ts+++ b/packages/core/src/buffers/profile-backfill-buffer.ts@@ -1,8 +1,8 @@import { getRedisCache, type Redis } from '@openpanel/redis';import sqlstring from 'sqlstring';+import { getReplicatedTableName, TABLE_NAMES } from '../shared/ch-tables';import { getSafeJson } from '../shared/json';import { BaseBuffer, type BufferDeps } from './base-buffer';-import { loadClickHouse } from './clickhouse';export interface ProfileBackfillEntry {projectId: string;@@ -69,7 +69,7 @@ export class ProfileBackfillBuffer extends BaseBuffer {}const entries = Array.from(seen.values());- const { ch, TABLE_NAMES, getReplicatedTableName } = await loadClickHouse();+ const ch = await this.resolveCh();const table = getReplicatedTableName(TABLE_NAMES.events);const chunks = this.chunks(entries, CHUNK_SIZE);diff --git a/packages/core/src/buffers/profile-buffer.test.ts b/packages/core/src/buffers/profile-buffer.test.tsindex 1257aa172..74677d057 100644--- a/packages/core/src/buffers/profile-buffer.test.ts+++ b/packages/core/src/buffers/profile-buffer.test.ts@@ -1,23 +1,18 @@import { afterAll, beforeEach, describe, expect, it, mock } from 'bun:test';import { getRedisCache } from '@openpanel/redis';-import { stubBufferDeps } from '../../test/buffer-fixtures';+import { bufferDepsWithCh } from '../../test/buffer-fixtures';import type { IClickhouseProfile } from '../modules/profile/profile.service';-// `./clickhouse` is the one seam the buffers reach ClickHouse through — see-// event-buffer.test.ts's header for why that, and not-// `@openpanel/db/src/clickhouse/client`, is what gets replaced.-const realClickhouse = { ...(await import('./clickhouse')) };+// M10-009: the client comes in as `BufferDeps.ch` and reads go through core's+// own `chQuery` — see event-buffer.test.ts's header.+const realChQuery = { ...(await import('../shared/ch-query')) };const chInsert = mock(async (_options: unknown): Promise<unknown> => undefined);const chQuery = mock(async (_sql: string): Promise<IClickhouseProfile[]> => []);-mock.module('./clickhouse', () => ({- loadClickHouse: () =>- Promise.resolve({- ch: { insert: chInsert },- chQuery,- TABLE_NAMES: { profiles: 'profiles' },- }),+mock.module('../shared/ch-query', () => ({+ ...realChQuery,+ chQuery: (_scope: unknown, sql: string) => chQuery(sql),}));const { ProfileBuffer } = await import('./profile-buffer');@@ -62,14 +57,14 @@ beforeEach(async () => {// every file in one process, and closing the singleton takes it away from the// files that run next (V1's vitest isolated per file, so it could).afterAll(() => {- mock.module('./clickhouse', () => realClickhouse);+ mock.module('../shared/ch-query', () => realChQuery);});describe('ProfileBuffer', () => {let profileBuffer: ProfileBuffer;beforeEach(() => {- profileBuffer = new ProfileBuffer(stubBufferDeps);+ profileBuffer = new ProfileBuffer(bufferDepsWithCh({ insert: chInsert }));});it('adds a profile to the buffer', async () => {diff --git a/packages/core/src/buffers/profile-buffer.ts b/packages/core/src/buffers/profile-buffer.tsindex c16eccdf9..4071a6eec 100644--- a/packages/core/src/buffers/profile-buffer.ts+++ b/packages/core/src/buffers/profile-buffer.ts@@ -3,9 +3,9 @@ import { getRedisCache, type Redis } from '@openpanel/redis';import { omit, uniq } from 'ramda';import sqlstring from 'sqlstring';import type { IClickhouseProfile } from '../modules/profile/profile.service';+import { TABLE_NAMES } from '../shared/ch-tables';import { getSafeJson } from '../shared/json';import { BaseBuffer, type BufferDeps } from './base-buffer';-import { loadClickHouse } from './clickhouse';// Inlined to avoid a circular value-import with `profile.service.ts`// (which imports `profileBuffer` from `../buffers`). Keep this in sync@@ -190,7 +190,6 @@ export class ProfileBuffer extends BaseBuffer {private async batchFetchFromClickhouse(profiles: IClickhouseProfile[]): Promise<Map<string, IClickhouseProfile>> {- const { chQuery, TABLE_NAMES } = await loadClickHouse();const result = new Map<string, IClickhouseProfile>();// Non-external (anonymous/device) profiles get a 2-day recency filter to@@ -215,7 +214,7 @@ export class ProfileBuffer extends BaseBuffer {// last_seen_at`, which is an aggregate function and illegal in WHERE// (CH ILLEGAL_AGGREGATION). Qualifying with `p.` bypasses the alias// lookup and binds to the raw column.- const rows = await chQuery<IClickhouseProfile>(+ const rows = await this.chQuery<IClickhouseProfile>(`SELECT ${PROFILE_LATEST_AGGREGATE_COLUMNS}FROM ${TABLE_NAMES.profiles} AS pWHERE (p.id, p.project_id) IN (${tuples})@@ -362,7 +361,7 @@ export class ProfileBuffer extends BaseBuffer {}}- const { ch, TABLE_NAMES } = await loadClickHouse();+ const ch = await this.resolveCh();const chStart = performance.now();await this.parallelLimit(this.chunks(toInsert, this.chunkSize), (chunk) =>ch.insert({diff --git a/packages/core/src/buffers/replay-buffer.ts b/packages/core/src/buffers/replay-buffer.tsindex a5aa08f21..0ee98cbb6 100644--- a/packages/core/src/buffers/replay-buffer.ts+++ b/packages/core/src/buffers/replay-buffer.ts@@ -1,6 +1,6 @@import { getRedisCache } from '@openpanel/redis';+import { TABLE_NAMES } from '../shared/ch-tables';import { BaseBuffer, type BufferDeps } from './base-buffer';-import { loadClickHouse } from './clickhouse';export interface IClickhouseSessionReplayChunk {project_id: string;@@ -76,7 +76,7 @@ export class ReplayBuffer extends BaseBuffer {// raw strings to CH skips JSON.parse × N on the worker AND the// client's internal JSON.stringify × N — significant because each// rrweb chunk's `payload` is 10–100KB.- const { ch, TABLE_NAMES } = await loadClickHouse();+ const ch = await this.resolveCh();const chStart = performance.now();await this.parallelLimit(this.chunks(items, this.chunkSize), (chunk) =>ch.insert({diff --git a/packages/core/src/buffers/session-buffer.test.ts b/packages/core/src/buffers/session-buffer.test.tsindex 2bbf587ce..d22fc972d 100644--- a/packages/core/src/buffers/session-buffer.test.ts+++ b/packages/core/src/buffers/session-buffer.test.ts@@ -1,5 +1,4 @@import {- afterAll,afterEach,beforeEach,describe,@@ -8,15 +7,12 @@ import {mock,} from 'bun:test';import { getRedisCache } from '@openpanel/redis';-import { stubBufferDeps } from '../../test/buffer-fixtures';+import { bufferDepsWithCh } from '../../test/buffer-fixtures';import type { IServiceCreateEventPayload } from '../modules/event/event.service';import type { IClickhouseSession } from '../modules/session/session.service';-// `./clickhouse` is the one seam the buffers reach ClickHouse through — see-// event-buffer.test.ts's header for why that, and not-// `@openpanel/db/src/clickhouse/client`, is what gets replaced.-const realClickhouse = { ...(await import('./clickhouse')) };-+// M10-009: the client comes in as `BufferDeps.ch` — no module mock needed for+// the flush path at all. See event-buffer.test.ts's header.const chInsert = mock(async (_options: {table: string;@@ -25,14 +21,6 @@ const chInsert = mock(}): Promise<unknown> => undefined);-mock.module('./clickhouse', () => ({- loadClickHouse: () =>- Promise.resolve({- ch: { insert: chInsert },- TABLE_NAMES: { sessions: 'sessions' },- }),-}));-const { SessionBuffer } = await import('./session-buffer');type SessionBuffer = InstanceType<typeof SessionBuffer>;@@ -92,18 +80,16 @@ beforeEach(async () => {chInsert.mockResolvedValue(undefined);});-// The shared `getRedisCache()` client is deliberately NOT quit here: bun runs-// every file in one process, and closing the singleton takes it away from the-// files that run next (V1's vitest isolated per file, so it could).-afterAll(() => {- mock.module('./clickhouse', () => realClickhouse);-});+// No `afterAll` on purpose: the shared `getRedisCache()` client must NOT be+// quit, because bun runs every file in one process and closing the singleton+// takes it away from the files that run next (V1's vitest isolated per file,+// so it could).describe('SessionBuffer', () => {let sessionBuffer: SessionBuffer;beforeEach(() => {- sessionBuffer = new SessionBuffer(stubBufferDeps);+ sessionBuffer = new SessionBuffer(bufferDepsWithCh({ insert: chInsert }));});it('opens a new session and stores it at session:{pid}:{did}', async () => {diff --git a/packages/core/src/buffers/session-buffer.ts b/packages/core/src/buffers/session-buffer.tsindex e5309b5c3..642d64135 100644--- a/packages/core/src/buffers/session-buffer.ts+++ b/packages/core/src/buffers/session-buffer.ts@@ -2,9 +2,9 @@ import { DateTime } from '@openpanel/common';import { getRedisCache, type Redis } from '@openpanel/redis';import type { IServiceCreateEventPayload } from '../modules/event/event.service';import type { IClickhouseSession } from '../modules/session/session.service';+import { TABLE_NAMES } from '../shared/ch-tables';import { getSafeJson } from '../shared/json';import { BaseBuffer, type BufferDeps } from './base-buffer';-import { loadClickHouse } from './clickhouse';// 30min of idle in event-time → session ends. Matches industry default.// Idle window for a session (boundary detection + the reaper's default deadman).@@ -566,7 +566,7 @@ export class SessionBuffer extends BaseBuffer {? this.squashSessionsByVersion(parsed): parsed;- const { ch, TABLE_NAMES } = await loadClickHouse();+ const ch = await this.resolveCh();for (const chunk of this.chunks(sessions, this.chunkSize)) {await ch.insert({table: TABLE_NAMES.sessions,diff --git a/packages/core/src/index.ts b/packages/core/src/index.tsindex 859fdb0a8..8bdd338bd 100644--- a/packages/core/src/index.ts+++ b/packages/core/src/index.ts@@ -415,20 +415,6 @@ export { createClientService } from './modules/client/client.service';// @openpanel/db's barrel, so packages/db loses the file entirely rather than// keeping a re-export shim (unlike gsc.ts/gsc.service.ts).export type { CohortService } from './modules/cohort/cohort.service';-export {- computeCohort,- countCohort,- deleteCohortMembership,- getCohortCount,- getCohortEventsPerDay,- getCohortMemberEvents,- getCohortMemberRoutes,- getCohortMembers,- getProfilesInCohort,- listCohortMemberProfiles,- listRefreshableCohortIds,- updateCohortMembership,-} from './modules/cohort/cohort.service';// Moved from @openpanel/db's services/conversation.service.ts (M5-006) —// packages/trpc's conversation router, apps/api's live chat route and this// package's own assistant.routes.ts stub call these directly. packages/db@@ -517,34 +503,7 @@ export type {GscQueryOpportunity,GscSite,} from './modules/gsc/gsc.service';-export {- completeGscOAuthCallback,- disconnectGscConnection,- getGscAiEngines,- getGscCannibalization,- getGscConnection,- getGscOverview,- getGscPageDetails,- getGscPages,- getGscPreviousOverview,- getGscQueries,- getGscQueryDetails,- getGscSearchEngines,- gscGetCannibalizationCore,- gscGetOverviewCore,- gscGetPageDetailsCore,- gscGetQueryDetailsCore,- gscGetQueryOpportunitiesCore,- gscGetTopPagesCore,- gscGetTopQueriesCore,- listGscConnectionsForSync,- listGscSites,- resolveGscDateRange,- runGscProjectBackfill,- runGscProjectSync,- selectGscSite,- syncGscData,-} from './modules/gsc/gsc.service';+export { getGscCannibalization } from './modules/gsc/gsc.service';// Moved from packages/trpc/src/routers/integration.ts, plus the Slack OAuth// callback business logic out of// apps/api/src/controllers/webhook.controller.ts (M6-006, ADR-008's module@@ -552,17 +511,7 @@ export {// apps/api's webhook controller call these directly, the same way V1 reaches// every other dissolved service here.export type { CompleteSlackOAuthCallbackResult } from './modules/integration/integration.service';-export {- completeSlackOAuthCallback,- createOrUpdateSlackIntegration,- deleteIntegration,- getIntegrationById,- listIntegrationsForProject,- SlackOAuthCallbackError,- testExportIntegrationConnection,- testIntegrationConnection,- upsertIntegration,-} from './modules/integration/integration.service';+export { SlackOAuthCallbackError } from './modules/integration/integration.service';// Slack's OAuth token-exchange wire contract (M6-006) — not integration// config (stays out of the `*.constants` subpath, see// modules/integration/src/slack-contract.ts's header), but apps/api's@@ -602,17 +551,11 @@ export {APP_NOTIFICATION_INTEGRATION_ID,BASE_INTEGRATIONS,createNotificationService,- createOrUpdateNotificationRule,- deleteNotificationRule,- deliverNotification,EMAIL_NOTIFICATION_INTEGRATION_ID,getFunnelRules,getHasFunnelRules,- getNotificationRuleByIdOrThrow,getNotificationRulesByProjectId,isBaseIntegration,- listNotificationRules,- listNotifications,matchEvent,matchEventFilters,notificationTemplateEvent,@@ -628,12 +571,7 @@ export type {OnboardingCronSummary,OnboardingService,} from './modules/onboarding/onboarding.service';-export {- canSkipOnboarding,- createOnboardingProject,- createOnboardingService,- runOnboardingCron,-} from './modules/onboarding/onboarding.service';+export { createOnboardingService } from './modules/onboarding/onboarding.service';// Dissolved from @openpanel/db's services/organization.service.ts +// services/delete.service.ts (M6-001, folded together per the module map) —// packages/trpc's organization router, apps/worker's delete cron job and@@ -652,36 +590,9 @@ export type {OrganizationService,} from './modules/organization/organization.service';export {- cancelOrganizationDeletion,- connectUserToOrganization,createOrganizationService,- deleteFromClickhouse,- deleteOrganization,- deleteProjects,- getInviteById,- getInviteOrThrow,- getInvites,- getMember,- getMembers,- getOrganizationBillingEventsCount,- getOrganizationBillingEventsCountSerie,getOrganizationBillingEventsCountSerieCached,- getOrganizationById,- getOrganizationByProjectId,getOrganizationByProjectIdCached,- getOrganizationEventsCount,- getOrganizationEventsCountSince,- getOrganizationSubscriptionChartEndDate,- getOrganizations,- getSettingsForOrganization,- getSettingsForProject,- inviteUserToOrganization,- removeOrganizationMember,- revokeInvite,- runDeleteCron,- scheduleOrganizationDeletion,- updateOrganization,- updateOrganizationMemberAccess,} from './modules/organization/organization.service';// Dissolved from @openpanel/db's services/overview.service.ts +// pages.service.ts (M7-005) — packages/trpc's overview/event routers,@@ -853,13 +764,7 @@ export type {SessionDistinctField,} from './modules/session/session.service';export {- getSessionById,- getSessionDistinctValues,- getSessionList,- getSessionReplayChunksFrom,- getSessionsCount,getSessionsCountCached,- querySessionsCore,SESSION_DISTINCT_FIELDS,transformSession,} from './modules/session/session.service';@@ -941,20 +846,6 @@ export type {InsertRawEventsResult,UpdateImportStatusOptions,} from './modules/import/import.service';-export {- backfillSessionsToProduction,- cleanupSessionStartEndEvents,- cleanupStagingData,- createSessionsStartEndEvents,- generateGapBasedSessionIds,- getImportDateBounds,- insertImportBatch,- insertProfilesBatch,- insertRawEventsBatch,- moveImportsToProduction,- runImportJob,- updateImportStatus,-} from './modules/import/import.service';// The ingestion pipeline (M8-002). apps/api's /track controller, its three// route hooks and the legacy /event controller are thin delegates over these// — the same functions core's own `ingestRoutes` calls.@@ -1037,18 +928,7 @@ export type {WeeklyDigestPreview,WeeklyDigestResult,} from './modules/insight/insight.service';-export {- cleanupStaleInsights,- explainInsight,- getReferrerSpikes,- listAllInsights,- listDailyInsightCandidates,- listInsights,- previewWeeklyDigest,- runProjectInsights,- scanLegacyInsights,- sendWeeklyDigests,-} from './modules/insight/insight.service';+export { explainInsight } from './modules/insight/insight.service';// Ported from apps/api's misc controller + apps/worker's cron.ping.ts (M7-008)// — apps/api's misc controller and the worker's ping cron job call these// directly, the same way V1 reaches every other dissolved service here.@@ -1207,7 +1087,6 @@ export {safeFetch,safeFetchStream,} from './shared/safe-fetch';-export { getId } from './shared/slug-id';export { assertSafeUrl, createPinnedLookup } from './shared/ssrf';// The V1 compat seam (v1-compat.ts): the bare, deps-free spellings of every// module whose functions gained a `ServiceDeps` first parameter, kept for@@ -1218,35 +1097,58 @@ export {AggregateChartEngine,adjustProfileProperty,applySaveDiscount,+ backfillSessionsToProduction,buildFunnelBase,ChartEngine,+ cancelOrganizationDeletion,cancelProjectDeletion,cancelSubscription,+ canSkipOnboarding,checkout,+ cleanupSessionStartEndEvents,+ cleanupStagingData,+ cleanupStaleInsights,clearClientByIdCache,clearProjectByIdCache,compatCh,compatDb,+ completeGscOAuthCallback,+ completeSlackOAuthCallback,+ computeCohort,+ connectUserToOrganization,+ countCohort,createBotEvent,createClientForOrganization,createDashboard,createEvent,createGroup,+ createOnboardingProject,+ createOrUpdateNotificationRule,+ createOrUpdateSlackIntegration,createProjectForOrganization,createReference,createReport,+ createSessionsStartEndEvents,createShareDashboard,createShareOverview,createShareReport,deleteClientForOrganization,+ deleteCohortMembership,deleteConversation,deleteDashboard,+ deleteFromClickhouse,deleteGroup,+ deleteIntegration,+ deleteNotificationRule,+ deleteOrganization,deleteProjectForOrganization,+ deleteProjects,deleteReference,deleteReport,deleteUserAccount,+ deliverNotification,disableTotp,+ disconnectGscConnection,duplicateReport,enableTotp,executeAggregateChart,@@ -1254,6 +1156,7 @@ export {extendSessionCookie,findGroupsCore,findProfilesCore,+ generateGapBasedSessionIds,getAnalyticsOverviewCore,getBotEventsPage,getChartBucketProfiles,@@ -1264,6 +1167,11 @@ export {getClientForOrganization,getClientsByOrganizationId,getClientsByProjectId,+ getCohortCount,+ getCohortEventsPerDay,+ getCohortMemberEvents,+ getCohortMemberRoutes,+ getCohortMembers,getConversationById,getConversion,getConversionChart,@@ -1303,7 +1211,33 @@ export {getGroupStats,getGroupsByIds,getGroupTypes,+ getGscAiEngines,+ getGscConnection,+ getGscOverview,+ getGscPageDetails,+ getGscPages,+ getGscPreviousOverview,+ getGscQueries,+ getGscQueryDetails,+ getGscSearchEngines,+ getId,+ getImportDateBounds,+ getIntegrationById,+ getInviteById,+ getInviteOrThrow,+ getInvites,getLastEventPerProject,+ getMember,+ getMembers,+ getNotificationRuleByIdOrThrow,+ getOrganizationBillingEventsCount,+ getOrganizationBillingEventsCountSerie,+ getOrganizationById,+ getOrganizationByProjectId,+ getOrganizationEventsCount,+ getOrganizationEventsCountSince,+ getOrganizationSubscriptionChartEndDate,+ getOrganizations,getPageConversionsCore,getPagePerformanceCore,getPowerUsers,@@ -1320,6 +1254,7 @@ export {getProfilePropertyNames,getProfileSessionsCore,getProfiles,+ getProfilesInCohort,getProfileValues,getProfileWithEvents,getProjectActivationStatus,@@ -1338,6 +1273,7 @@ export {getRealtimeReferrals,getReferenceById,getReferenceByIdOrThrow,+ getReferrerSpikes,getReportById,getReportByIdOrThrow,getReportDataCore,@@ -1354,6 +1290,13 @@ export {getSankey,getSankeyChart,getSegmentDailySeriesCore,+ getSessionById,+ getSessionDistinctValues,+ getSessionList,+ getSessionReplayChunksFrom,+ getSessionsCount,+ getSettingsForOrganization,+ getSettingsForProject,getShareByProjectId,getShareDashboard,getShareDashboardByDashboardId,@@ -1379,37 +1322,74 @@ export {getUserById,getUserFlowCore,getWeeklyRetentionSeriesCore,+ gscGetCannibalizationCore,+ gscGetOverviewCore,+ gscGetPageDetailsCore,+ gscGetQueryDetailsCore,+ gscGetQueryOpportunitiesCore,+ gscGetTopPagesCore,+ gscGetTopQueriesCore,identifyProfile,+ insertImportBatch,insertPingRecord,+ insertProfilesBatch,+ insertRawEventsBatch,+ inviteUserToOrganization,+ listAllInsights,listChartEvents,listChartProperties,listClientsForOrganization,+ listCohortMemberProfiles,listConversations,+ listDailyInsightCandidates,listDashboardsCore,listEventNamesCore,listEventPropertiesCore,listGroupTypesCore,+ listGscConnectionsForSync,+ listGscSites,+ listInsights,+ listIntegrationsForProject,+ listNotificationRules,+ listNotifications,listProducts,listProjectsCore,listProjectsForOrganization,listReferences,+ listRefreshableCohortIds,listReportsCore,listUserDeletionBlockers,+ moveImportsToProduction,moveReport,overviewService,pagesService,pauseSubscription,portal,+ previewWeeklyDigest,queryEventsCore,+ querySessionsCore,regenerateTotpRecoveryCodes,+ removeOrganizationMember,requestPasswordReset,resetPasswordWithToken,resetReportLayouts,resetV1CompatServicesForTests,resolveClientProjectId,+ resolveGscDateRange,resumeSubscription,+ revokeInvite,+ runDeleteCron,+ runGscProjectBackfill,+ runGscProjectSync,+ runImportJob,+ runOnboardingCron,runPingCron,+ runProjectInsights,+ scanLegacyInsights,+ scheduleOrganizationDeletion,scheduleProjectDeletion,+ selectGscSite,+ sendWeeklyDigests,setupTotp,setV1CompatServices,signInToShare,@@ -1417,10 +1397,17 @@ export {signInWithTotp,signOutUser,signUpWithEmail,+ syncGscData,+ testExportIntegrationConnection,+ testIntegrationConnection,updateClientForOrganization,+ updateCohortMembership,updateDashboard,updateEventMeta,updateGroup,+ updateImportStatus,+ updateOrganization,+ updateOrganizationMemberAccess,updateProjectForOrganization,updateReference,updateReport,@@ -1428,6 +1415,7 @@ export {updateUserProfile,upsertConversationTitle,upsertGroup,+ upsertIntegration,upsertProfile,V1_COMPAT_REQUEST_ID,validateOverviewShareAccess,diff --git a/packages/core/src/modules/assistant/assistant.routes.ts b/packages/core/src/modules/assistant/assistant.routes.tsindex 0bc8d672d..b5dc68f3d 100644--- a/packages/core/src/modules/assistant/assistant.routes.ts+++ b/packages/core/src/modules/assistant/assistant.routes.ts@@ -16,18 +16,24 @@// project-access check below — `chatApp.handler` gets the original,// unconsumed `request` so it can re-read the body itself.//-// `@openpanel/core` (the self barrel) stays dynamically imported, not-// statically — `loadCore()` below reaches deep into the barrel for-// cross-module functions, and a static import here would race this-// package's own evaluation the way index.ts's header documents for-// `mcp.service.ts`. `./assistant.service` itself is cheap either way now-// (M10-004, see that file's header) — kept dynamic anyway for symmetry with-// `loadCore()`.+// The cross-module functions stay dynamically imported, not statically: a+// static import here would race this package's own evaluation the way+// index.ts's header documents for `mcp.service.ts`. M10-009 replaced the one+// `import('@openpanel/core')` hop with direct relative imports of the three+// modules it actually reached (`core-no-self-barrel`). `./assistant.service`+// itself is cheap either way now (M10-004, see that file's header) — kept+// dynamic anyway for symmetry.import { defineRoutes } from '../../http/define';-function loadCore() {- return import('@openpanel/core');+function loadCompat() {+ return import('../../v1-compat');+}+function loadAccessLookups() {+ return import('../../shared/access-lookups');+}+function loadOrganizationService() {+ return import('../organization/organization.service');}function loadAssistant() {return import('./assistant.service');@@ -40,7 +46,7 @@ interface RunBody {export const assistantRoutes = defineRoutes((app) =>app.all('/ai/agents/*',- async ({ params, session, status, request }) => {+ async ({ params, session, status, request, ctx }) => {const userId = session.userId;if (!userId) {return status(401, { message: 'Sign in required' });@@ -72,7 +78,7 @@ export const assistantRoutes = defineRoutes((app) =>// A brand-new chat (no row yet) falls through to the agent handler,// whose `ConversationStore.load()` returns null.if (route === 'conversations' && routeId) {- const { getConversationById } = await loadCore();+ const { getConversationById } = await loadCompat();const conv = await getConversationById(routeId);if (conv && conv.userId !== userId) {return status(404, { message: 'Conversation not found' });@@ -95,15 +101,16 @@ export const assistantRoutes = defineRoutes((app) =>});}- const {- getOrganizationByProjectIdCached,- getProjectAccess,- getSettingsForProject,- } = await loadCore();+ const [+ { getOrganizationByProjectIdCached, getSettingsForProject },+ { getProjectAccess },+ ] = await Promise.all([loadOrganizationService(), loadAccessLookups()]);const [access, organization, settings] = await Promise.all([getProjectAccess({ projectId, userId }),getOrganizationByProjectIdCached(projectId),- getSettingsForProject(projectId).catch(() => ({ timezone: 'UTC' })),+ getSettingsForProject(ctx, projectId).catch(() => ({+ timezone: 'UTC',+ })),]);if (!(access && organization) ||diff --git a/packages/core/src/modules/assistant/assistant.service.ts b/packages/core/src/modules/assistant/assistant.service.tsindex 792e6880e..c7fa0ec96 100644--- a/packages/core/src/modules/assistant/assistant.service.ts+++ b/packages/core/src/modules/assistant/assistant.service.ts@@ -28,7 +28,6 @@// them through the v1-compat singleton (see that file's header), the same// way every other bare, no-`Ctx` caller in this wave does.-import type { ServiceDeps } from '../../services';import type { PageContext } from './assistant.constants';let _app: Promise<typeof import('./src/app')> | undefined;@@ -87,7 +86,13 @@ export interface AssistantService {): ReturnType<typeof runFilterCommand>;}-export function createAssistantService(_deps: ServiceDeps): AssistantService {+/**+ * Takes NO `ServiceDeps`: the three members are `@better-agent/core` entry+ * points with a fixed signature, and each reaches the database through the+ * v1-compat seam its own tools already use — there is nothing here to hand a+ * client to.+ */+export function createAssistantService(): AssistantService {return {getChatApp,getChatRunContext,diff --git a/packages/core/src/modules/assistant/src/filter-command.ts b/packages/core/src/modules/assistant/src/filter-command.tsindex d3d1c41d6..3699675bc 100644--- a/packages/core/src/modules/assistant/src/filter-command.ts+++ b/packages/core/src/modules/assistant/src/filter-command.ts@@ -10,13 +10,6 @@ import {type ToolRunContext,} from '@better-agent/core';import { intervals, operators, timeWindows } from '@openpanel/constants';-import {- getDatesFromRange,- getTopPagesCore,- getTrafficBreakdownCore,- listEventNamesCore,- resolveDateRange as resolveDateRangeCore,-} from '@openpanel/core';import {type IChartEventFilter,type IChartRange,@@ -24,6 +17,15 @@ import {} from '@openpanel/validation';import { z } from 'zod';import { resolveModel } from '../../../clients/ai/providers';+import {+ getDatesFromRange,+ resolveDateRange as resolveDateRangeCore,+} from '../../../shared/date';+import {+ getTopPagesCore,+ getTrafficBreakdownCore,+ listEventNamesCore,+} from '../../../v1-compat';import {CHAT_MODELS,type ChatModelEntry,diff --git a/packages/core/src/modules/assistant/src/report-runner.ts b/packages/core/src/modules/assistant/src/report-runner.tsindex 187ed1d02..d66b24d9e 100644--- a/packages/core/src/modules/assistant/src/report-runner.ts+++ b/packages/core/src/modules/assistant/src/report-runner.ts@@ -16,14 +16,15 @@// becomes a third layer, not a new problem. Both copies call the same// @openpanel/db primitives and must be kept in sync by hand until that debt// is paid down.++import { getChartStartEndDate } from '../../../shared/date';import {AggregateChartEngine,ChartEngine,- getChartStartEndDate,getFunnel,getReportById,getSettingsForProject,-} from '@openpanel/core';+} from '../../../v1-compat';function reportUrl(organizationId: string,diff --git a/packages/core/src/modules/assistant/src/tools/base.ts b/packages/core/src/modules/assistant/src/tools/base.tsindex 39d843c9b..c29c00883 100644--- a/packages/core/src/modules/assistant/src/tools/base.ts+++ b/packages/core/src/modules/assistant/src/tools/base.ts@@ -1,4 +1,6 @@import { chartTypes, lineTypes, operators } from '@openpanel/constants';+import { zReportInput } from '@openpanel/validation';+import { z } from 'zod';import {findProfilesCore,getAnalyticsOverviewCore,@@ -15,9 +17,7 @@ import {listReportsCore,queryEventsCore,querySessionsCore,-} from '@openpanel/core';-import { zReportInput } from '@openpanel/validation';-import { z } from 'zod';+} from '../../../../v1-compat';import { runReport, runReportFromConfig } from '../report-runner';import {chatTool,diff --git a/packages/core/src/modules/assistant/src/tools/dashboard.ts b/packages/core/src/modules/assistant/src/tools/dashboard.tsindex 405a425e2..a243fd15b 100644--- a/packages/core/src/modules/assistant/src/tools/dashboard.ts+++ b/packages/core/src/modules/assistant/src/tools/dashboard.ts@@ -1,14 +1,14 @@+import type { IChartRange, IInterval } from '@openpanel/validation';+import { z } from 'zod';+import { getChartStartEndDate } from '../../../../shared/date';import {AggregateChartEngine,ChartEngine,- getChartStartEndDate,getDashboardById,getFunnel,getReportsByDashboardId,getSettingsForProject,-} from '@openpanel/core';-import type { IChartRange, IInterval } from '@openpanel/validation';-import { z } from 'zod';+} from '../../../../v1-compat';import { chatTool, dashboardUrl } from './helpers';// Cap on parallel report execution. Real dashboards rarely exceed this,diff --git a/packages/core/src/modules/assistant/src/tools/events.ts b/packages/core/src/modules/assistant/src/tools/events.tsindex 52a7252bf..c4b727592 100644--- a/packages/core/src/modules/assistant/src/tools/events.ts+++ b/packages/core/src/modules/assistant/src/tools/events.ts@@ -1,9 +1,9 @@+import { z } from 'zod';import {getEventPropertyValuesCore,listEventPropertiesCore,queryEventsCore,-} from '@openpanel/core';-import { z } from 'zod';+} from '../../../../v1-compat';import {chatTool,compactEventProperties,diff --git a/packages/core/src/modules/assistant/src/tools/groups.ts b/packages/core/src/modules/assistant/src/tools/groups.tsindex ac8cc4f25..f1d6fb170 100644--- a/packages/core/src/modules/assistant/src/tools/groups.ts+++ b/packages/core/src/modules/assistant/src/tools/groups.ts@@ -1,10 +1,10 @@+import { z } from 'zod';import {findGroupsCore,getGroupCore,getGroupMemberProfiles,queryEventsCore,-} from '@openpanel/core';-import { z } from 'zod';+} from '../../../../v1-compat';import { chatTool, dashboardUrl, truncateRows } from './helpers';export const getGroupFull = chatTool(diff --git a/packages/core/src/modules/assistant/src/tools/helpers.ts b/packages/core/src/modules/assistant/src/tools/helpers.tsindex cb5cae1ef..262196442 100644--- a/packages/core/src/modules/assistant/src/tools/helpers.ts+++ b/packages/core/src/modules/assistant/src/tools/helpers.ts@@ -3,12 +3,12 @@ import {defineTool,type ToolRunContext,} from '@better-agent/core';+import type { IChartEventFilter, IChartRange } from '@openpanel/validation';+import type { z } from 'zod';import {getDatesFromRange,resolveDateRange as resolveDateRangeCore,-} from '@openpanel/core';-import type { IChartEventFilter, IChartRange } from '@openpanel/validation';-import type { z } from 'zod';+} from '../../../../shared/date';import type { ChatAgentContext, PageContext } from '../context';import { chatRunContext } from '../run-context';diff --git a/packages/core/src/modules/assistant/src/tools/pages.ts b/packages/core/src/modules/assistant/src/tools/pages.tsindex 8fa6bc975..c182f7191 100644--- a/packages/core/src/modules/assistant/src/tools/pages.ts+++ b/packages/core/src/modules/assistant/src/tools/pages.ts@@ -1,10 +1,10 @@+import { z } from 'zod';import {getEntryExitPagesCore,getPageConversionsCore,getPagePerformanceCore,getTopPagesCore,-} from '@openpanel/core';-import { z } from 'zod';+} from '../../../../v1-compat';import {chatTool,previousPeriod,diff --git a/packages/core/src/modules/assistant/src/tools/profile.ts b/packages/core/src/modules/assistant/src/tools/profile.tsindex e5c50aa5e..6e2e73e90 100644--- a/packages/core/src/modules/assistant/src/tools/profile.ts+++ b/packages/core/src/modules/assistant/src/tools/profile.ts@@ -1,3 +1,4 @@+import { z } from 'zod';import {findProfilesCore,getProfileById,@@ -5,8 +6,7 @@ import {getProfileSessionsCore,getProfileWithEvents,queryEventsCore,-} from '@openpanel/core';-import { z } from 'zod';+} from '../../../../v1-compat';import { chatTool, dashboardUrl, truncateRows } from './helpers';export const getProfileFull = chatTool(diff --git a/packages/core/src/modules/assistant/src/tools/report.ts b/packages/core/src/modules/assistant/src/tools/report.tsindex 6eadb084d..33461edc2 100644--- a/packages/core/src/modules/assistant/src/tools/report.ts+++ b/packages/core/src/modules/assistant/src/tools/report.ts@@ -1,6 +1,6 @@-import { listEventPropertiesCore } from '@openpanel/core';import type { IReportInput } from '@openpanel/validation';import { z } from 'zod';+import { listEventPropertiesCore } from '../../../../v1-compat';import { runReportFromConfig } from '../report-runner';import { chatTool, previousPeriod, resolveDateRange } from './helpers';diff --git a/packages/core/src/modules/assistant/src/tools/seo.ts b/packages/core/src/modules/assistant/src/tools/seo.tsindex 47aae48e7..b6eedeb43 100644--- a/packages/core/src/modules/assistant/src/tools/seo.ts+++ b/packages/core/src/modules/assistant/src/tools/seo.ts@@ -1,3 +1,4 @@+import { z } from 'zod';import {getTopPagesCore,gscGetCannibalizationCore,@@ -7,8 +8,7 @@ import {gscGetQueryOpportunitiesCore,gscGetTopPagesCore,gscGetTopQueriesCore,-} from '@openpanel/core';-import { z } from 'zod';+} from '../../../../v1-compat';import { chatTool, resolveDateRange, truncateRows } from './helpers';export const gscGetOverview = chatTool(diff --git a/packages/core/src/modules/assistant/src/tools/session.ts b/packages/core/src/modules/assistant/src/tools/session.tsindex 53971caea..e8a61efda 100644--- a/packages/core/src/modules/assistant/src/tools/session.ts+++ b/packages/core/src/modules/assistant/src/tools/session.ts@@ -1,9 +1,9 @@+import { z } from 'zod';import {getSessionById,queryEventsCore,querySessionsCore,-} from '@openpanel/core';-import { z } from 'zod';+} from '../../../../v1-compat';import { chatTool, dashboardUrl, truncateRows } from './helpers';export const getSessionFull = chatTool(diff --git a/packages/core/src/modules/auth/auth.service.access.test.ts b/packages/core/src/modules/auth/auth.service.access.test.tsindex 8e9d68c11..48d9aa04b 100644--- a/packages/core/src/modules/auth/auth.service.access.test.ts+++ b/packages/core/src/modules/auth/auth.service.access.test.ts@@ -8,7 +8,6 @@// gate are unchanged.import { afterAll, beforeAll, expect, mock, test } from 'bun:test';-import type { ServiceDeps } from '../../services';interface FakeProjectAccess {level: 'read' | 'write' | 'admin';@@ -63,7 +62,7 @@ afterAll(() => {});function authService() {- return createAuthService({} as unknown as ServiceDeps);+ return createAuthService();}test('requireProjectAccess is fail-closed: no access throws before the write-level check', async () => {diff --git a/packages/core/src/modules/auth/auth.service.ts b/packages/core/src/modules/auth/auth.service.tsindex 1cb8ef9ed..01b5beca6 100644--- a/packages/core/src/modules/auth/auth.service.ts+++ b/packages/core/src/modules/auth/auth.service.ts@@ -180,13 +180,15 @@ export function resetAccessChecksForTests(): void {}/**- * Registered in `services.ts`. `deps` is unused (every member here is either+ * Registered in `services.ts`. Takes NO `ServiceDeps`, and says so on the+ * signature rather than ignoring an argument: every member here is either* pure, reads its own env, or — for the access checks — reaches the shared,- * memoized `getAccessChecks()` above) — kept on the signature because every- * other module's factory takes it, and a method that later needs `logger`- * should not change the call site.+ * memoized `getAccessChecks()` above, whose lookups are `cacheable` (their+ * key is derived from the call's arguments, so they cannot take a leading+ * `deps`; see shared/access-lookups.ts). A member that later needs `db` /+ * `logger` adds the parameter back, and the one call site follows.*/-export function createAuthService(_deps: ServiceDeps): AuthService {+export function createAuthService(): AuthService {return {async requireProjectAccess(args) {return (await getAccessChecks()).requireProjectAccess(args);@@ -307,7 +309,7 @@ async function consumeInviteForUser(const user = await deps.db.user.findUniqueOrThrow({where: { id: userId },});- await connectUserToOrganization({ user, inviteId });+ await connectUserToOrganization(deps, { user, inviteId });} catch (error) {logger.error({ userId, inviteId, error },@@ -416,7 +418,7 @@ export async function signUpWithEmail(});if (input.inviteId) {- await connectUserToOrganization({+ await connectUserToOrganization(deps, {user: createdUser,inviteId: input.inviteId,});@@ -1120,7 +1122,7 @@ async function completeNewOAuthUser(if (inviteId) {try {- await connectUserToOrganization({ user, inviteId });+ await connectUserToOrganization(deps, { user, inviteId });} catch (error) {logger.error({ error, inviteId, user },diff --git a/packages/core/src/modules/chart/chart.service.ts b/packages/core/src/modules/chart/chart.service.tsindex 3bad699d5..dbd98e32c 100644--- a/packages/core/src/modules/chart/chart.service.ts+++ b/packages/core/src/modules/chart/chart.service.ts@@ -42,10 +42,11 @@ import {} from '../profile/profile.service';import type { IServiceReport } from '../report/report.service';import { mergeGlobalFilters, onlyReportEvents } from '../report/src/series';-import { getConversion } from './conversion.service';+import { createConversionService, getConversion } from './conversion.service';import {buildFunnelBase,buildSessionsCte,+ createFunnelService,getFunnel,getFunnelCore,getFunnelGroup,@@ -53,6 +54,7 @@ import {toSeries as toFunnelSeries,} from './funnel.service';import {+ createRetentionService,getEngagementCore,getRetentionCohort,getRetentionCohortCore,@@ -64,7 +66,7 @@ import {processCohortData,} from './retention.service';import {- getRawWhereClause,+ createSankeyService,getSankey,getUserFlowCore,} from './sankey.service';@@ -292,7 +294,7 @@ export async function getProjectCard(metrics: ProjectCardMetrics | undefined;trend: ProjectCardTrend;}> {- const { timezone } = await getSettingsForProject(projectId);+ const { timezone } = await getSettingsForProject(deps, projectId);const [chart, [metrics]] = await Promise.all([runQuery<ProjectCardChartRow>(deps,@@ -462,8 +464,11 @@ export async function getChartPropertyValues(// --- funnel / conversion / sankey / retention ---------------------------------async function currentAndPreviousPeriod(chartInput: IReportInput) {- const { timezone } = await getSettingsForProject(chartInput.projectId);+async function currentAndPreviousPeriod(+ deps: ServiceDeps,+ chartInput: IReportInput+) {+ const { timezone } = await getSettingsForProject(deps, chartInput.projectId);const currentPeriod = getChartStartEndDate(chartInput, timezone);const previousPeriod = getChartPrevStartEndDate(currentPeriod);return { timezone, currentPeriod, previousPeriod };@@ -474,7 +479,7 @@ export async function getFunnelChart(chartInput: IReportInput) {const { timezone, currentPeriod, previousPeriod } =- await currentAndPreviousPeriod(chartInput);+ await currentAndPreviousPeriod(deps, chartInput);const [current, previous] = await Promise.all([getFunnel(deps, { ...chartInput, ...currentPeriod, timezone }),@@ -491,7 +496,7 @@ export async function getConversionChart(chartInput: IReportInput) {const { timezone, currentPeriod, previousPeriod } =- await currentAndPreviousPeriod(chartInput);+ await currentAndPreviousPeriod(deps, chartInput);const interval = chartInput.interval;const [current, previous] = await Promise.all([@@ -524,7 +529,7 @@ export async function getConversionChart(}export async function getSankeyChart(deps: ServiceDeps, input: IReportInput) {- const { timezone } = await getSettingsForProject(input.projectId);+ const { timezone } = await getSettingsForProject(deps, input.projectId);const currentPeriod = getChartStartEndDate(input, timezone);const options = input.options;@@ -612,7 +617,7 @@ export async function getRetentionChart() {const resolved = report ? retentionInputFromReport(report, input) : input;- const { timezone } = await getSettingsForProject(resolved.projectId);+ const { timezone } = await getSettingsForProject(deps, resolved.projectId);const dates = getChartStartEndDate({range: resolved.range,@@ -706,7 +711,7 @@ export async function getFunnelStepProfiles(deps: ServiceDeps,input: FunnelStepProfilesRequest): Promise<IServiceProfile[]> {- const { timezone } = await getSettingsForProject(input.projectId);+ const { timezone } = await getSettingsForProject(deps, input.projectId);const {projectId,series,@@ -844,6 +849,15 @@ export interface ChartService {}export function createChartService(deps: ServiceDeps): ChartService {+ // The four chart sub-modules each expose their own `create*Service(deps)`+ // (ADR-007). `services.ts` binds each under its own key; `chart` composes+ // them as well, so the callers that reach a funnel/retention method through+ // the chart facade keep working. Both bind the same stateless closures.+ const funnel = createFunnelService(deps);+ const conversion = createConversionService(deps);+ const sankey = createSankeyService(deps);+ const retention = createRetentionService(deps);+return {execute: (input) => executeChart(deps, input),executeAggregate: (input) => executeAggregateChart(deps, input),@@ -854,34 +868,30 @@ export function createChartService(deps: ServiceDeps): ChartService {getChartPropertyValues: (input) => getChartPropertyValues(deps, input),bucketProfiles: (input) => getChartBucketProfiles(deps, input),funnelStepProfiles: (input) => getFunnelStepProfiles(deps, input),- getFunnelGroup,- toFunnelSeries,- buildSessionsCte,+ getFunnelGroup: funnel.getFunnelGroup,+ toFunnelSeries: funnel.toSeries,+ buildSessionsCte: funnel.buildSessionsCte,getFunnelChart: (chartInput) => getFunnelChart(deps, chartInput),- getFunnel: (input) => getFunnel(deps, input),- getFunnelCore: (input) => getFunnelCore(deps, input),- buildFunnelBase: (input) => buildFunnelBase(deps, input),- getFunnelProfileIds: (input) => getFunnelProfileIds(deps, input),+ getFunnel: funnel.getFunnel,+ getFunnelCore: funnel.getFunnelCore,+ buildFunnelBase: funnel.buildFunnelBase,+ getFunnelProfileIds: funnel.getFunnelProfileIds,getConversionChart: (chartInput) => getConversionChart(deps, chartInput),- getConversion: (input) => getConversion(deps, input),- getRawWhereClause,+ getConversion: conversion.getConversion,+ getRawWhereClause: sankey.getRawWhereClause,getSankeyChart: (input) => getSankeyChart(deps, input),- getSankey: (input) => getSankey(deps, input),- getUserFlowCore: (input) => getUserFlowCore(deps, input),- processCohortData,+ getSankey: sankey.getSankey,+ getUserFlowCore: sankey.getUserFlowCore,+ processCohortData: retention.processCohortData,getRetentionChart: (report, input) =>getRetentionChart(deps, report, input),- getRetentionCohort: (input) => getRetentionCohort(deps, input),- getRetentionCohortCore: (projectId) =>- getRetentionCohortCore(deps, projectId),- getRetentionSeries: (input) => getRetentionSeries(deps, input),- getRetentionLastSeenSeries: (input) =>- getRetentionLastSeenSeries(deps, input),- getRollingActiveUsers: (input) => getRollingActiveUsers(deps, input),- getRollingActiveUsersCore: (input) =>- getRollingActiveUsersCore(deps, input),- getWeeklyRetentionSeriesCore: (projectId) =>- getWeeklyRetentionSeriesCore(deps, projectId),- getEngagementCore: (projectId) => getEngagementCore(deps, projectId),+ getRetentionCohort: retention.getRetentionCohort,+ getRetentionCohortCore: retention.getRetentionCohortCore,+ getRetentionSeries: retention.getRetentionSeries,+ getRetentionLastSeenSeries: retention.getRetentionLastSeenSeries,+ getRollingActiveUsers: retention.getRollingActiveUsers,+ getRollingActiveUsersCore: retention.getRollingActiveUsersCore,+ getWeeklyRetentionSeriesCore: retention.getWeeklyRetentionSeriesCore,+ getEngagementCore: retention.getEngagementCore,};}diff --git a/packages/core/src/modules/chart/conversion.service.ts b/packages/core/src/modules/chart/conversion.service.tsindex abb6d8284..d68b23d4a 100644--- a/packages/core/src/modules/chart/conversion.service.ts+++ b/packages/core/src/modules/chart/conversion.service.ts@@ -259,3 +259,17 @@ function toSeries(})),}));}++/** See funnel.service.ts's `createFunnelService` for why each chart+ * sub-module carries its own factory (M10-009, ADR-007). */+export interface ConversionService {+ getConversion(+ input: Parameters<typeof getConversion>[1]+ ): ReturnType<typeof getConversion>;+}++export function createConversionService(deps: ServiceDeps): ConversionService {+ return {+ getConversion: (input) => getConversion(deps, input),+ };+}diff --git a/packages/core/src/modules/chart/funnel.service.ts b/packages/core/src/modules/chart/funnel.service.tsindex c9ed2977c..8c5527163 100644--- a/packages/core/src/modules/chart/funnel.service.ts+++ b/packages/core/src/modules/chart/funnel.service.ts@@ -341,7 +341,7 @@ export async function getFunnelCore(groupBy?: FunnelGroup;}) {- const { timezone } = await getSettingsForProject(input.projectId);+ const { timezone } = await getSettingsForProject(deps, input.projectId);const eventSeries = input.steps.map((name, index) => ({id: String(index + 1),type: 'event' as const,@@ -430,3 +430,40 @@ export async function getFunnelProfileIds();return rows.map((row) => row.profile_id).filter(Boolean);}++/**+ * `ctx.services.chart`'s funnel half, as its own module factory (ADR-007:+ * "each service is `createXService(deps): XService`"). `createChartService`+ * composes this rather than re-binding these functions itself, so the chart+ * module keeps one registry key while every `*.service.ts` file exposes its+ * own factory (M10-009).+ */+export interface FunnelService {+ getFunnelGroup: typeof getFunnelGroup;+ toSeries: typeof toSeries;+ buildSessionsCte: typeof buildSessionsCte;+ getFunnel(+ input: Parameters<typeof getFunnel>[1]+ ): ReturnType<typeof getFunnel>;+ getFunnelCore(+ input: Parameters<typeof getFunnelCore>[1]+ ): ReturnType<typeof getFunnelCore>;+ buildFunnelBase(+ input: Parameters<typeof buildFunnelBase>[1]+ ): ReturnType<typeof buildFunnelBase>;+ getFunnelProfileIds(+ input: Parameters<typeof getFunnelProfileIds>[1]+ ): ReturnType<typeof getFunnelProfileIds>;+}++export function createFunnelService(deps: ServiceDeps): FunnelService {+ return {+ getFunnelGroup,+ toSeries,+ buildSessionsCte,+ getFunnel: (input) => getFunnel(deps, input),+ getFunnelCore: (input) => getFunnelCore(deps, input),+ buildFunnelBase: (input) => buildFunnelBase(deps, input),+ getFunnelProfileIds: (input) => getFunnelProfileIds(deps, input),+ };+}diff --git a/packages/core/src/modules/chart/retention.service.ts b/packages/core/src/modules/chart/retention.service.tsindex 363bf256f..c8e856e9c 100644--- a/packages/core/src/modules/chart/retention.service.ts+++ b/packages/core/src/modules/chart/retention.service.ts@@ -389,3 +389,49 @@ export function processCohortData(return [averageRow, ...processed];}++/** See funnel.service.ts's `createFunnelService` for why each chart+ * sub-module carries its own factory (M10-009, ADR-007). */+export interface RetentionService {+ processCohortData: typeof processCohortData;+ getRetentionCohort(+ input: Parameters<typeof getRetentionCohort>[1]+ ): ReturnType<typeof getRetentionCohort>;+ getRetentionCohortCore(+ projectId: string+ ): ReturnType<typeof getRetentionCohortCore>;+ getRetentionSeries(+ input: Parameters<typeof getRetentionSeries>[1]+ ): ReturnType<typeof getRetentionSeries>;+ getRetentionLastSeenSeries(+ input: Parameters<typeof getRetentionLastSeenSeries>[1]+ ): ReturnType<typeof getRetentionLastSeenSeries>;+ getRollingActiveUsers(+ input: Parameters<typeof getRollingActiveUsers>[1]+ ): ReturnType<typeof getRollingActiveUsers>;+ getRollingActiveUsersCore(+ input: Parameters<typeof getRollingActiveUsersCore>[1]+ ): ReturnType<typeof getRollingActiveUsersCore>;+ getWeeklyRetentionSeriesCore(+ projectId: string+ ): ReturnType<typeof getWeeklyRetentionSeriesCore>;+ getEngagementCore(projectId: string): ReturnType<typeof getEngagementCore>;+}++export function createRetentionService(deps: ServiceDeps): RetentionService {+ return {+ processCohortData,+ getRetentionCohort: (input) => getRetentionCohort(deps, input),+ getRetentionCohortCore: (projectId) =>+ getRetentionCohortCore(deps, projectId),+ getRetentionSeries: (input) => getRetentionSeries(deps, input),+ getRetentionLastSeenSeries: (input) =>+ getRetentionLastSeenSeries(deps, input),+ getRollingActiveUsers: (input) => getRollingActiveUsers(deps, input),+ getRollingActiveUsersCore: (input) =>+ getRollingActiveUsersCore(deps, input),+ getWeeklyRetentionSeriesCore: (projectId) =>+ getWeeklyRetentionSeriesCore(deps, projectId),+ getEngagementCore: (projectId) => getEngagementCore(deps, projectId),+ };+}diff --git a/packages/core/src/modules/chart/sankey.service.ts b/packages/core/src/modules/chart/sankey.service.tsindex 7a47f58b4..236e17ec8 100644--- a/packages/core/src/modules/chart/sankey.service.ts+++ b/packages/core/src/modules/chart/sankey.service.ts@@ -487,7 +487,7 @@ export async function getUserFlowCore(throw new Error('endEvent is required when mode is "between"');}- const { timezone } = await getSettingsForProject(input.projectId);+ const { timezone } = await getSettingsForProject(deps, input.projectId);const result = await getSankey(deps, {projectId: input.projectId,startDate: input.startDate,@@ -511,3 +511,23 @@ export async function getUserFlowCore(links: result.links,};}++/** See funnel.service.ts's `createFunnelService` for why each chart+ * sub-module carries its own factory (M10-009, ADR-007). */+export interface SankeyService {+ getRawWhereClause: typeof getRawWhereClause;+ getSankey(+ input: Parameters<typeof getSankey>[1]+ ): ReturnType<typeof getSankey>;+ getUserFlowCore(+ input: Parameters<typeof getUserFlowCore>[1]+ ): ReturnType<typeof getUserFlowCore>;+}++export function createSankeyService(deps: ServiceDeps): SankeyService {+ return {+ getRawWhereClause,+ getSankey: (input) => getSankey(deps, input),+ getUserFlowCore: (input) => getUserFlowCore(deps, input),+ };+}diff --git a/packages/core/src/modules/chart/src/chart.sql.test.ts b/packages/core/src/modules/chart/src/chart.sql.test.tsindex 870cbca10..7d275e8fe 100644--- a/packages/core/src/modules/chart/src/chart.sql.test.ts+++ b/packages/core/src/modules/chart/src/chart.sql.test.ts@@ -37,8 +37,15 @@ mock.module('@openpanel/db/src/prisma-client', () => ({db: { cohort: { findMany: cohortFindMany } },}));-afterAll(() => {+afterAll(async () => {mock.module('@openpanel/db/src/prisma-client', () => realPrismaClient);+ // Restoring the module registry is not enough: `v1-compat.ts` MEMOIZES the+ // fallback `ServiceDeps` the first time anything resolves it, so if that+ // happened while the mock above was installed, every later FILE in this+ // process keeps the mocked client (bare `bun test` shares one registry).+ // Drop the memo too — same reason mcp's dashboard-management.test.ts does.+ const { resetV1CompatServicesForTests } = await import('../../../v1-compat');+ resetV1CompatServicesForTests();});let ch: typeof import('@openpanel/db/src/clickhouse/client').ch;diff --git a/packages/core/src/modules/chart/src/engine/execute.ts b/packages/core/src/modules/chart/src/engine/execute.tsindex 2a015d07f..069b0557b 100644--- a/packages/core/src/modules/chart/src/engine/execute.ts+++ b/packages/core/src/modules/chart/src/engine/execute.ts@@ -14,10 +14,12 @@ import type { ConcreteSeries } from './types';/** Normalize, then clamp the end date to the organization's subscription window. */async function normalizeWithinSubscription(+ deps: ServiceDeps,input: IReportInput): Promise<NormalizedInput> {- const normalized = await normalize(input);+ const normalized = await normalize(deps, input);const endDate = await getOrganizationSubscriptionChartEndDate(+ deps,input.projectId,normalized.endDate);@@ -32,8 +34,8 @@ export async function executeChart(deps: ServiceDeps,input: IReportInput): Promise<FinalChart> {- const normalized = await normalizeWithinSubscription(input);- const executionPlan = await plan(normalized);+ const normalized = await normalizeWithinSubscription(deps, input);+ const executionPlan = await plan(deps, normalized);const computedSeries = compute(await fetch(deps, executionPlan),executionPlan.definitions@@ -41,7 +43,7 @@ export async function executeChart(let previousSeries: ConcreteSeries[] | null = null;if (input.previous) {- const previousPlan = await plan({+ const previousPlan = await plan(deps, {...normalized,...getChartPrevStartEndDate({startDate: normalized.startDate,@@ -68,8 +70,8 @@ export async function executeAggregateChart(deps: ServiceDeps,input: IReportInput): Promise<FinalChart> {- const normalized = await normalizeWithinSubscription(input);- const { timezone } = await getSettingsForProject(normalized.projectId);+ const normalized = await normalizeWithinSubscription(deps, input);+ const { timezone } = await getSettingsForProject(deps, normalized.projectId);const currentPeriod = {startDate: normalized.startDate,endDate: normalized.endDate,diff --git a/packages/core/src/modules/chart/src/engine/normalize.ts b/packages/core/src/modules/chart/src/engine/normalize.tsindex 78d663613..f9bb30849 100644--- a/packages/core/src/modules/chart/src/engine/normalize.ts+++ b/packages/core/src/modules/chart/src/engine/normalize.ts@@ -4,6 +4,7 @@ import type {IReportInput,IReportInputWithDates,} from '@openpanel/validation';+import type { ServiceDeps } from '../../../../services';import { getChartStartEndDate } from '../../../../shared/date';import { getSettingsForProject } from '../../../organization/organization.service';import { mergeGlobalFilters } from '../../../report/src/series';@@ -36,8 +37,11 @@ function toSeriesDefinition(}/** Resolve the date range in the project's timezone and normalize every series item. */-export async function normalize(input: IReportInput): Promise<NormalizedInput> {- const { timezone } = await getSettingsForProject(input.projectId);+export async function normalize(+ deps: ServiceDeps,+ input: IReportInput+): Promise<NormalizedInput> {+ const { timezone } = await getSettingsForProject(deps, input.projectId);const { startDate, endDate } = getChartStartEndDate({range: input.range,diff --git a/packages/core/src/modules/chart/src/engine/plan.ts b/packages/core/src/modules/chart/src/engine/plan.tsindex 32dd240a8..8746cc9f4 100644--- a/packages/core/src/modules/chart/src/engine/plan.ts+++ b/packages/core/src/modules/chart/src/engine/plan.ts@@ -1,6 +1,7 @@import { slug } from '@openpanel/common';import { alphabetIds } from '@openpanel/constants';import type { IChartEventItem } from '@openpanel/validation';+import type { ServiceDeps } from '../../../../services';import { getSettingsForProject } from '../../../organization/organization.service';import type { NormalizedInput } from './normalize';import type { ConcreteSeries, Plan } from './types';@@ -9,8 +10,11 @@ import type { ConcreteSeries, Plan } from './types';* One placeholder per event definition; breakdown expansion happens in fetch.* Formulas are derived from event series, so they only exist after compute.*/-export async function plan(normalized: NormalizedInput): Promise<Plan> {- const { timezone } = await getSettingsForProject(normalized.projectId);+export async function plan(+ deps: ServiceDeps,+ normalized: NormalizedInput+): Promise<Plan> {+ const { timezone } = await getSettingsForProject(deps, normalized.projectId);const concreteSeries: ConcreteSeries[] = [];normalized.series.forEach((definition, index) => {diff --git a/packages/core/src/modules/chart/src/funnel.sql.test.ts b/packages/core/src/modules/chart/src/funnel.sql.test.tsindex e1cacd618..9afda9ed7 100644--- a/packages/core/src/modules/chart/src/funnel.sql.test.ts+++ b/packages/core/src/modules/chart/src/funnel.sql.test.ts@@ -37,8 +37,15 @@ mock.module('@openpanel/db/src/prisma-client', () => ({db: { cohort: { findMany: cohortFindMany } },}));-afterAll(() => {+afterAll(async () => {mock.module('@openpanel/db/src/prisma-client', () => realPrismaClient);+ // Restoring the module registry is not enough: `v1-compat.ts` MEMOIZES the+ // fallback `ServiceDeps` the first time anything resolves it, so if that+ // happened while the mock above was installed, every later FILE in this+ // process keeps the mocked client (bare `bun test` shares one registry).+ // Drop the memo too — same reason mcp's dashboard-management.test.ts does.+ const { resetV1CompatServicesForTests } = await import('../../../v1-compat');+ resetV1CompatServicesForTests();});let ch: typeof import('@openpanel/db/src/clickhouse/client').ch;diff --git a/packages/core/src/modules/cohort/cohort.jobs.test.ts b/packages/core/src/modules/cohort/cohort.jobs.test.tsindex db362e437..c1a74e3e0 100644--- a/packages/core/src/modules/cohort/cohort.jobs.test.ts+++ b/packages/core/src/modules/cohort/cohort.jobs.test.ts@@ -29,6 +29,7 @@ function stubJobCtx(cohort: Partial<CohortService>, jobName = 'test'): JobCtx {auth: {} as Services['auth'],notification: {} as Services['notification'],insight: {} as Services['insight'],+ integration: {} as Services['integration'],gsc: {} as Services['gsc'],import: {} as Services['import'],ingest: {} as Services['ingest'],@@ -40,6 +41,10 @@ function stubJobCtx(cohort: Partial<CohortService>, jobName = 'test'): JobCtx {profile: {} as Services['profile'],group: {} as Services['group'],chart: {} as Services['chart'],+ funnel: {} as Services['funnel'],+ conversion: {} as Services['conversion'],+ sankey: {} as Services['sankey'],+ retention: {} as Services['retention'],overview: {} as Services['overview'],pages: {} as Services['pages'],realtime: {} as Services['realtime'],diff --git a/packages/core/src/modules/cohort/cohort.rpc.ts b/packages/core/src/modules/cohort/cohort.rpc.tsindex 7f733f0c9..11b40423e 100644--- a/packages/core/src/modules/cohort/cohort.rpc.ts+++ b/packages/core/src/modules/cohort/cohort.rpc.ts@@ -45,10 +45,6 @@ import {const EXPORT_PROFILES_MAX_LIMIT = 10_000;const EXPORT_PROFILES_DEFAULT_LIMIT = 10_000;-function loadDb() {- return import('@openpanel/db/src/prisma-client').then((m) => m.db);-}-function requireLogin(userId: string | null | undefined): string {if (!userId) {throw new TRPCAccessError('Not authenticated');@@ -72,7 +68,7 @@ export const cohortRouter = createTRPCRouter({level: 'read',});- const db = await loadDb();+ const db = ctx.db;const cohorts = await db.cohort.findMany({where: { projectId: input.projectId },orderBy: { createdAt: 'desc' },@@ -92,7 +88,7 @@ export const cohortRouter = createTRPCRouter({.input(z.object({ id: z.string() })).query(async ({ input, ctx }) => {const userId = requireLogin(ctx.session.userId);- const db = await loadDb();+ const db = ctx.db;const cohort = await db.cohort.findUnique({where: { id: input.id },});@@ -117,7 +113,7 @@ export const cohortRouter = createTRPCRouter({create: procedure.input(zCohortInput).mutation(async ({ input, ctx }) => {requireLogin(ctx.session.userId);- const db = await loadDb();+ const db = ctx.db;const cohort = await db.cohort.create({data: {name: input.name,@@ -137,7 +133,7 @@ export const cohortRouter = createTRPCRouter({const userId = requireLogin(ctx.session.userId);const { id, ...data } = input;- const db = await loadDb();+ const db = ctx.db;const existingCohort = await db.cohort.findUnique({ where: { id } });if (!existingCohort) {@@ -173,7 +169,7 @@ export const cohortRouter = createTRPCRouter({.input(z.object({ id: z.string() })).mutation(async ({ input, ctx }) => {const userId = requireLogin(ctx.session.userId);- const db = await loadDb();+ const db = ctx.db;const cohort = await db.cohort.findUnique({where: { id: input.id },});@@ -190,7 +186,7 @@ export const cohortRouter = createTRPCRouter({await db.cohort.delete({ where: { id: input.id } });- deleteCohortMembership(input.id, cohort.projectId).catch((err) => {+ deleteCohortMembership(ctx, input.id, cohort.projectId).catch((err) => {ctx.logger.error({ err }, 'Failed to cleanup cohort CH data');});@@ -216,7 +212,7 @@ export const cohortRouter = createTRPCRouter({level: 'read',});- const { data, count } = await listCohortMemberProfiles(input);+ const { data, count } = await listCohortMemberProfiles(ctx, input);return {data,meta: { count, pageCount: input.take },@@ -233,7 +229,7 @@ export const cohortRouter = createTRPCRouter({level: 'read',});- return getCohortMemberEvents(input.projectId, input.cohortId);+ return getCohortMemberEvents(ctx, input.projectId, input.cohortId);}),eventsPerDay: procedure@@ -246,7 +242,7 @@ export const cohortRouter = createTRPCRouter({level: 'read',});- return getCohortEventsPerDay(input.projectId, input.cohortId);+ return getCohortEventsPerDay(ctx, input.projectId, input.cohortId);}),popularRoutes: procedure@@ -259,14 +255,14 @@ export const cohortRouter = createTRPCRouter({level: 'read',});- return getCohortMemberRoutes(input.projectId, input.cohortId);+ return getCohortMemberRoutes(ctx, input.projectId, input.cohortId);}),getCount: procedure.input(z.object({ cohortId: z.string() })).query(async ({ input, ctx }) => {const userId = requireLogin(ctx.session.userId);- const db = await loadDb();+ const db = ctx.db;const cohort = await db.cohort.findUnique({where: { id: input.cohortId },});@@ -281,7 +277,7 @@ export const cohortRouter = createTRPCRouter({level: 'read',});- const count = await getCohortCount(input.cohortId, cohort.projectId);+ const count = await getCohortCount(ctx, input.cohortId, cohort.projectId);return { count };}),@@ -300,8 +296,9 @@ export const cohortRouter = createTRPCRouter({level: 'read',});- const count = await countCohort(input.projectId, input.definition);+ const count = await countCohort(ctx, input.projectId, input.definition);const sampleProfiles = await computeCohort(+ ctx,input.projectId,input.definition,10@@ -324,7 +321,7 @@ export const cohortRouter = createTRPCRouter({).query(async ({ input, ctx }) => {const userId = requireLogin(ctx.session.userId);- const db = await loadDb();+ const db = ctx.db;const cohort = await db.cohort.findUnique({where: { id: input.cohortId },});@@ -339,10 +336,15 @@ export const cohortRouter = createTRPCRouter({level: 'read',});- const result = await getCohortMembers(input.cohortId, cohort.projectId, {- limit: input.limit,- offset: input.offset,- });+ const result = await getCohortMembers(+ ctx,+ input.cohortId,+ cohort.projectId,+ {+ limit: input.limit,+ offset: input.offset,+ }+ );return {profileIds: result.profileIds,@@ -355,7 +357,7 @@ export const cohortRouter = createTRPCRouter({.input(z.object({ cohortId: z.string() })).mutation(async ({ input, ctx }) => {const userId = requireLogin(ctx.session.userId);- const db = await loadDb();+ const db = ctx.db;const cohort = await db.cohort.findUnique({where: { id: input.cohortId },});diff --git a/packages/core/src/modules/cohort/cohort.service.test.ts b/packages/core/src/modules/cohort/cohort.service.test.tsindex 4764b517a..4950835a2 100644--- a/packages/core/src/modules/cohort/cohort.service.test.ts+++ b/packages/core/src/modules/cohort/cohort.service.test.ts@@ -1,8 +1,8 @@-// cohort.service.ts's db/ch access is lazy (`await import(...)` inside each-// function — see the file's header), which is exactly what makes-// `mock.module` work here with no import-time side effects to race: every-// mock below is registered before the subject's first call, not before its-// (side-effect-free) import.+// M10-009: every function under test takes `ServiceDeps`, so `deps.db` and+// `deps.ch` ARE the fakes below — the two `@openpanel/db` module mocks this+// file used to install (and had to carefully restore, because `mock.module`+// has no per-file scope without `--isolate`) are gone. The one module still+// mocked is core's own `shared/ch-query`, which is what the subject now calls.//// The pure SQL-shape builders (buildEventCriteriaQuery,// buildPropertyBasedCohortQuery, deriveCohortQuerySettings) have their own@@ -52,58 +52,31 @@ const cohort = {),};-const actualPrismaClient = await import('@openpanel/db/src/prisma-client');-mock.module('@openpanel/db/src/prisma-client', () => ({- ...actualPrismaClient,- db: { cohort },-}));-const chQuery = mock(async () => [] as unknown[]);const chInsert = mock(async (_args: { table: string }) => undefined);const chCommand = mock(async (_args: { query: string }) => undefined);-const getReplicatedTableName = mock((table: string) => table);-// Spread the real module rather than hand-listing every export: `mock.module`-// replaces this specifier process-wide (bun runs every test file in one-// shared module registry without `--isolate` — see AGENTS.md), so a partial-// factory here silently breaks unrelated consumers (gsc/insight/import tests-// and now the mcp module's) that import an export this file never overrides.-// `ch` itself is one such export: only `insert`/`command` are what-// cohort.service.ts exercises, so those two are overridden and every other-// method (`query`, ...) is spread from the real client — a bare `{ insert,-// command }` replacement previously stripped `query` from every *other*-// consumer of this same live-bound singleton (e.g. `@openpanel/db`'s-// `OverviewService`/`PagesService`, constructed once at that module's own-// load time) for the rest of the process.-// A plain-object snapshot, not the live import binding: once `mock.module`-// below swaps this specifier, `actualClickhouseClient.chQuery` (a namespace-// binding) reflects the *mocked* value too, so restoring via-// `actualClickhouseClient` itself in `afterAll` is a no-op — it just spreads-// back whatever is currently mocked. Snapshotting into a plain object first-// keeps a real, frozen-in-time copy to restore to.-const actualClickhouseClient = await import(- '@openpanel/db/src/clickhouse/client'-);-const realClickhouseClient = { ...actualClickhouseClient };-mock.module('@openpanel/db/src/clickhouse/client', () => ({- ...realClickhouseClient,- ch: { ...realClickhouseClient.ch, insert: chInsert, command: chCommand },- chQuery,- getReplicatedTableName,++// The subject reaches ClickHouse through core's own `chQuery(deps, ...)`+// (shared/ch-query.ts) — mocking that one module keeps this file off+// @openpanel/db's live-bound client singleton entirely. `mock.module` still+// has no per-file scope under bare `bun test` (AGENTS.md), so snapshot the+// real module into a plain object FIRST and restore it in afterAll —+// restoring via the live import binding would just re-apply the mock.+const realChQuery = { ...(await import('../../shared/ch-query')) };+mock.module('../../shared/ch-query', () => ({+ ...realChQuery,+ chQuery: (_deps: unknown, ...args: unknown[]) => chQuery(...(args as [])),}));-// `chQuery`/`ch.insert`/`ch.command` above are fakes stuck in place for the-// rest of the process once this file's tests finish (`mock.module` has no-// per-file scope without `--isolate` — see AGENTS.md): the mcp module's-// integration suite calls the real `chQuery` against a live ClickHouse and-// silently got `[]` back from this file's leftover mock. Restore the real-// snapshot so whichever file runs next sees real behavior again.afterAll(() => {- mock.module(- '@openpanel/db/src/clickhouse/client',- () => realClickhouseClient- );+ mock.module('../../shared/ch-query', () => realChQuery);});+const deps = {+ db: { cohort },+ ch: { insert: chInsert, command: chCommand },+} as unknown as import('../../services').ServiceDeps;+let subject: typeof import('./cohort.service');beforeAll(async () => {subject = await import('./cohort.service');@@ -140,7 +113,7 @@ test('updateCohortMembership computes, clears old membership, stores new membersseedCohort();chQuery.mockImplementationOnce(async () => [{ profile_id: 'p1' }]);- await subject.updateCohortMembership('cohort_1');+ await subject.updateCohortMembership(deps, 'cohort_1');expect(chCommand).toHaveBeenCalledTimes(1);expect(chCommand.mock.calls[0]?.[0]?.query).toContain('DELETE FROM');@@ -154,7 +127,7 @@ test('updateCohortMembership does nothing when the cohort no longer exists', asycohortStore.clear();chCommand.mockClear();- await subject.updateCohortMembership('missing');+ await subject.updateCohortMembership(deps, 'missing');expect(chCommand).not.toHaveBeenCalled();});@@ -162,7 +135,7 @@ test('updateCohortMembership does nothing when the cohort no longer exists', asytest('deleteCohortMembership clears both cohort_members and cohort_metadata', async () => {chCommand.mockClear();- await subject.deleteCohortMembership('cohort_1', 'proj_1');+ await subject.deleteCohortMembership(deps, 'cohort_1', 'proj_1');expect(chCommand).toHaveBeenCalledTimes(2);});@@ -173,7 +146,7 @@ test('listRefreshableCohortIds returns only non-static cohorts', async () => {seedCohort({ id: 'c2', isStatic: true });seedCohort({ id: 'c3', isStatic: false });- const ids = await subject.listRefreshableCohortIds();+ const ids = await subject.listRefreshableCohortIds(deps);expect(ids.sort()).toEqual(['c1', 'c3']);});@@ -186,7 +159,7 @@ test('getCohortCount serves the cached profileCount within 15 minutes', async ()lastComputedAt: new Date(Date.now() - 60_000),});- const count = await subject.getCohortCount('cohort_1', 'proj_1');+ const count = await subject.getCohortCount(deps, 'cohort_1', 'proj_1');expect(count).toBe(42);expect(chQuery).not.toHaveBeenCalled();@@ -201,7 +174,7 @@ test('getCohortCount re-queries ClickHouse once the cache is stale', async () =>});chQuery.mockImplementationOnce(async () => [{ count: 7 }]);- const count = await subject.getCohortCount('cohort_1', 'proj_1');+ const count = await subject.getCohortCount(deps, 'cohort_1', 'proj_1');expect(count).toBe(7);expect(chQuery).toHaveBeenCalledTimes(1);@@ -233,14 +206,17 @@ test('createCohortService().enqueueCompute enqueues with the cohort-<id> dedupli]);});-test('createCohortService() binds updateMembership and listRefreshableCohortIds to the plain functions', () => {- const deps = {- queues: {},- } as unknown as Parameters<typeof subject.createCohortService>[0];+test('createCohortService() delegates updateMembership and listRefreshableCohortIds to the plain functions, over its own deps', async () => {+ cohortStore.clear();+ seedCohort({ id: 'c1', isStatic: false });+ seedCohort({ id: 'c2', isStatic: true });const service = subject.createCohortService(deps);- expect(service.updateMembership).toBe(subject.updateCohortMembership);- expect(service.listRefreshableCohortIds).toBe(- subject.listRefreshableCohortIds- );+ // M10-009: the container's members are closures over `deps`, not the bare+ // functions, so identity is no longer the observable — delegation is.+ expect((await service.listRefreshableCohortIds()).sort()).toEqual(['c1']);++ chCommand.mockClear();+ await service.updateMembership('missing');+ expect(chCommand).not.toHaveBeenCalled();});diff --git a/packages/core/src/modules/cohort/cohort.service.ts b/packages/core/src/modules/cohort/cohort.service.tsindex c317a991c..7d7165e88 100644--- a/packages/core/src/modules/cohort/cohort.service.ts+++ b/packages/core/src/modules/cohort/cohort.service.ts@@ -33,10 +33,12 @@// mutations and its cron fragment).import type { ClickHouseSettings } from '@clickhouse/client';-import type { IServiceProfile } from '@openpanel/core';import type { IChartEventFilter } from '@openpanel/validation';import sqlstring from 'sqlstring';import type { ServiceDeps } from '../../services';+import { chQuery } from '../../shared/ch-query';+import { getReplicatedTableName } from '../../shared/ch-tables';+import type { IServiceProfile } from '../profile/profile.service';import type {CohortDefinition,EventBasedCohortDefinition,@@ -138,14 +140,6 @@ export const PROFILE_COHORT_QUERY_SETTINGS: ClickHouseSettings =spillBytesRaw: process.env.COHORT_QUERY_SPILL_BYTES,});-function loadDb() {- return import('@openpanel/db/src/prisma-client').then((m) => m.db);-}--function loadChClient() {- return import('@openpanel/db/src/clickhouse/client');-}-function buildTimeConstraint(timeframe: Timeframe): string {if (timeframe.type === 'relative') {const match = timeframe.value.match(/^(\d+)d$/);@@ -353,12 +347,12 @@ export function buildPropertyBasedCohortQuery(}export async function computeEventBasedCohort(+ deps: ServiceDeps,projectId: string,definition: EventBasedCohortDefinition,limit?: number): Promise<string[]> {const { events, operator } = definition.criteria;- const { chQuery } = await loadChClient();const queries = events.map((eventCriteria) =>buildEventCriteriaQuery(projectId, eventCriteria)@@ -371,16 +365,18 @@ export async function computeEventBasedCohort(const finalQuery = limit ? `${combinedQuery} LIMIT ${limit}` : combinedQuery;- const results = await chQuery<{ profile_id: string }>(finalQuery);+ const results = await chQuery<{ profile_id: string }>(+ deps,+ finalQuery);return results.map((r) => r.profile_id);}export async function countEventBasedCohort(+ deps: ServiceDeps,projectId: string,definition: EventBasedCohortDefinition): Promise<number> {const { events, operator } = definition.criteria;- const { chQuery } = await loadChClient();const queries = events.map((eventCriteria) =>buildEventCriteriaQuery(projectId, eventCriteria)@@ -392,7 +388,9 @@ export async function countEventBasedCohort(: queries.join(' UNION DISTINCT ');const countQuery = `SELECT count() as count FROM (${combinedQuery})`;- const results = await chQuery<{ count: number }>(countQuery);+ const results = await chQuery<{ count: number }>(+ deps,+ countQuery);return results[0]?.count ?? 0;}@@ -528,6 +526,7 @@ function getProfileFiltersWhereClause(}export async function computePropertyBasedCohort(+ deps: ServiceDeps,projectId: string,definition: PropertyBasedCohortDefinition,limit?: number@@ -536,8 +535,9 @@ export async function computePropertyBasedCohort(return [];}- const { chQuery } = await loadChClient();const results = await chQuery<{ profile_id: string }>(+ deps,+buildPropertyBasedCohortQuery(projectId, definition, limit),PROFILE_COHORT_QUERY_SETTINGS);@@ -545,6 +545,7 @@ export async function computePropertyBasedCohort(}export async function countPropertyBasedCohort(+ deps: ServiceDeps,projectId: string,definition: PropertyBasedCohortDefinition): Promise<number> {@@ -552,8 +553,9 @@ export async function countPropertyBasedCohort(return 0;}- const { chQuery } = await loadChClient();const results = await chQuery<{ count: number }>(+ deps,+`SELECT count() as count FROM (${buildPropertyBasedCohortQuery(projectId, definition)})`,PROFILE_COHORT_QUERY_SETTINGS);@@ -561,12 +563,13 @@ export async function countPropertyBasedCohort(}export async function storeCohortMembership(+ deps: ServiceDeps,projectId: string,cohortId: string,profileIds: string[],version: number): Promise<void> {- const { ch } = await loadChClient();+ const ch = deps.ch;const now = new Date().toISOString().slice(0, 19).replace('T', ' ');if (profileIds.length > 0) {@@ -604,12 +607,12 @@ export async function storeCohortMembership(}export async function getCohortMembers(+ deps: ServiceDeps,cohortId: string,projectId: string,opts?: { limit?: number; offset?: number }): Promise<{ profileIds: string[]; total: number }> {- const db = await loadDb();- const { chQuery } = await loadChClient();+ const db = deps.db;const cohort = await db.cohort.findUnique({ where: { id: cohortId } });if (!cohort) {@@ -628,7 +631,9 @@ export async function getCohortMembers(${opts?.offset ? `OFFSET ${opts.offset}` : ''}`;- const results = await chQuery<{ profile_id: string; total: number }>(query);+ const results = await chQuery<{ profile_id: string; total: number }>(+ deps,+ query);return {profileIds: results.map((r) => r.profile_id),total: results[0]?.total || 0,@@ -638,10 +643,11 @@ export async function getCohortMembers(const COHORT_COUNT_CACHE_MS = 15 * 60 * 1000;export async function getCohortCount(+ deps: ServiceDeps,cohortId: string,projectId: string): Promise<number> {- const db = await loadDb();+ const db = deps.db;const cohort = await db.cohort.findUnique({ where: { id: cohortId } });if (!cohort) {@@ -655,8 +661,9 @@ export async function getCohortCount(}}- const { chQuery } = await loadChClient();- const result = await chQuery<{ count: number }>(`+ const result = await chQuery<{ count: number }>(+ deps,+ `SELECT count() as countFROM ${TABLE.cohortMembers} FINALWHERE project_id = ${sqlstring.escape(projectId)}@@ -666,35 +673,40 @@ export async function getCohortCount(}export async function computeCohort(+ deps: ServiceDeps,projectId: string,definition: CohortDefinition,limit?: number): Promise<string[]> {if (definition.type === 'event') {- return computeEventBasedCohort(projectId, definition, limit);+ return computeEventBasedCohort(deps, projectId, definition, limit);}if (definition.type === 'property') {- return computePropertyBasedCohort(projectId, definition, limit);+ return computePropertyBasedCohort(deps, projectId, definition, limit);}return [];}export async function countCohort(+ deps: ServiceDeps,projectId: string,definition: CohortDefinition): Promise<number> {if (definition.type === 'event') {- return countEventBasedCohort(projectId, definition);+ return countEventBasedCohort(deps, projectId, definition);}if (definition.type === 'property') {- return countPropertyBasedCohort(projectId, definition);+ return countPropertyBasedCohort(deps, projectId, definition);}return 0;}-export async function updateCohortMembership(cohortId: string): Promise<void> {- const db = await loadDb();- const { ch, getReplicatedTableName } = await loadChClient();+export async function updateCohortMembership(+ deps: ServiceDeps,+ cohortId: string+): Promise<void> {+ const db = deps.db;+ const ch = deps.ch;const cohort = await db.cohort.findUnique({ where: { id: cohortId } });if (!cohort) {@@ -703,6 +715,7 @@ export async function updateCohortMembership(cohortId: string): Promise<void> {const definition = cohort.definition as CohortDefinition;const profileIds = await computeCohort(+ deps,cohort.projectId,definition,COHORT_MATERIALIZE_LIMIT@@ -720,7 +733,13 @@ export async function updateCohortMembership(cohortId: string): Promise<void> {},});- await storeCohortMembership(cohort.projectId, cohort.id, profileIds, version);+ await storeCohortMembership(+ deps,+ cohort.projectId,+ cohort.id,+ profileIds,+ version+ );await db.cohort.update({where: { id: cohortId },@@ -732,10 +751,11 @@ export async function updateCohortMembership(cohortId: string): Promise<void> {}export async function deleteCohortMembership(+ deps: ServiceDeps,cohortId: string,projectId: string): Promise<void> {- const { ch, getReplicatedTableName } = await loadChClient();+ const ch = deps.ch;const where = `cohort_id = ${sqlstring.escape(cohortId)} AND project_id = ${sqlstring.escape(projectId)}`;for (const table of [TABLE.cohortMembers, TABLE.cohortMetadata]) {await ch.command({@@ -748,18 +768,21 @@ export async function deleteCohortMembership(}export async function getProfilesInCohort(+ deps: ServiceDeps,cohortId: string,projectId: string): Promise<Set<string>> {- const { profileIds } = await getCohortMembers(cohortId, projectId, {+ const { profileIds } = await getCohortMembers(deps, cohortId, projectId, {limit: 100_000,});return new Set(profileIds);}/** Every non-static cohort id, for the cohortRefresh cron fragment's fan-out. */-export async function listRefreshableCohortIds(): Promise<string[]> {- const db = await loadDb();+export async function listRefreshableCohortIds(+ deps: ServiceDeps+): Promise<string[]> {+ const db = deps.db;const cohorts = await db.cohort.findMany({where: { isStatic: false },select: { id: true },@@ -767,7 +790,9 @@ export async function listRefreshableCohortIds(): Promise<string[]> {return cohorts.map((c) => c.id);}-export async function listCohortMemberProfiles({+export async function listCohortMemberProfiles(+ deps: ServiceDeps,+ {projectId,cohortId,cursor,@@ -782,7 +807,6 @@ export async function listCohortMemberProfiles({search?: string;filters?: IChartEventFilter[];}): Promise<{ data: IServiceProfile[]; count: number }> {- const { chQuery } = await loadChClient();const { buildFilterWhere } = await import('../chart/src/table-filter-where');const { profileSearchSql } = await import('../profile/profile.service');// M10-005: `getProfiles` takes `ServiceDeps` now and this function has none@@ -807,7 +831,9 @@ export async function listCohortMemberProfiles({? `AND ${extraConditions.join(' AND ')}`: '';- const rows = await chQuery<{ id: string; total_count: number }>(`+ const rows = await chQuery<{ id: string; total_count: number }>(+ deps,+ `SELECT id, count() OVER () AS total_countFROM ${TABLE.profiles} FINALWHERE project_id = ${sqlstring.escape(projectId)}@@ -837,12 +863,14 @@ export async function listCohortMemberProfiles({}export async function getCohortMemberEvents(+ deps: ServiceDeps,projectId: string,cohortId: string,limit = 10): Promise<{ name: string; count: number }[]> {- const { chQuery } = await loadChClient();- return chQuery<{ name: string; count: number }>(`+ return chQuery<{ name: string; count: number }>(+ deps,+ `SELECT name, count() AS countFROM ${TABLE.events}WHERE project_id = ${sqlstring.escape(projectId)}@@ -859,12 +887,14 @@ export async function getCohortMemberEvents(}export async function getCohortEventsPerDay(+ deps: ServiceDeps,projectId: string,cohortId: string,days = 30): Promise<{ date: string; count: number }[]> {- const { chQuery } = await loadChClient();- const rows = await chQuery<{ date: string; count: number }>(`+ const rows = await chQuery<{ date: string; count: number }>(+ deps,+ `SELECTtoDate(created_at) AS date,count() AS count@@ -887,12 +917,14 @@ export async function getCohortEventsPerDay(}export async function getCohortMemberRoutes(+ deps: ServiceDeps,projectId: string,cohortId: string,limit = 10): Promise<{ path: string; count: number }[]> {- const { chQuery } = await loadChClient();- return chQuery<{ path: string; count: number }>(`+ return chQuery<{ path: string; count: number }>(+ deps,+ `SELECT path, count() AS countFROM ${TABLE.events}WHERE project_id = ${sqlstring.escape(projectId)}@@ -930,8 +962,8 @@ export interface CohortService {export function createCohortService(deps: ServiceDeps): CohortService {return {- updateMembership: updateCohortMembership,- listRefreshableCohortIds,+ updateMembership: (cohortId) => updateCohortMembership(deps, cohortId),+ listRefreshableCohortIds: () => listRefreshableCohortIds(deps),enqueueCompute: async (cohortId) => {await deps.queues.cohortCompute.cohortCompute.add({ cohortId },diff --git a/packages/core/src/modules/dashboard/dashboard.service.ts b/packages/core/src/modules/dashboard/dashboard.service.tsindex 1a1692c03..e20753f73 100644--- a/packages/core/src/modules/dashboard/dashboard.service.ts+++ b/packages/core/src/modules/dashboard/dashboard.service.ts@@ -103,7 +103,7 @@ export async function createDashboard(return deps.db.dashboard.create({data: {- id: await getId('dashboard', input.name),+ id: await getId(deps, 'dashboard', input.name),projectId: input.projectId,organizationId: project.organizationId,name: input.name,diff --git a/packages/core/src/modules/email/email.rpc.ts b/packages/core/src/modules/email/email.rpc.tsindex 799339d5b..ade8d22dc 100644--- a/packages/core/src/modules/email/email.rpc.ts+++ b/packages/core/src/modules/email/email.rpc.ts@@ -30,10 +30,6 @@ import { createTRPCRouter, procedure } from '../../rpc/base';import { TRPCAccessError, TRPCBadRequestError } from '../../rpc/errors';import { emailCategories } from './email.constants';-function loadDb() {- return import('@openpanel/db/src/prisma-client').then((m) => m.db);-}-function requireLogin(userId: string | null | undefined): string {if (!userId) {throw new TRPCAccessError('Not authenticated');@@ -50,14 +46,14 @@ export const emailRouter = createTRPCRouter({token: z.string(),}))- .mutation(async ({ input }) => {+ .mutation(async ({ input, ctx }) => {const { email, category, token } = input;if (!verifyUnsubscribeToken(email, category, token)) {throw new TRPCBadRequestError('Invalid unsubscribe link');}- const db = await loadDb();+ const db = ctx.db;await db.emailUnsubscribe.upsert({where: { email_category: { email, category } },create: { email, category },@@ -74,14 +70,12 @@ export const emailRouter = createTRPCRouter({}const email = ctx.session.user.email;- const db = await loadDb();+ const db = ctx.db;const unsubscribes = await db.emailUnsubscribe.findMany({where: { email },select: { category: true },});- const unsubscribedCategories = new Set(- unsubscribes.map((u) => u.category)- );+ const unsubscribedCategories = new Set(unsubscribes.map((u) => u.category));const preferences: Record<string, boolean> = {};for (const category of Object.keys(emailCategories)) {@@ -99,7 +93,7 @@ export const emailRouter = createTRPCRouter({throw new Error('User not authenticated');}- const db = await loadDb();+ const db = ctx.db;for (const [category, subscribed] of Object.entries(input.categories)) {if (subscribed) {await db.emailUnsubscribe.deleteMany({ where: { email, category } });diff --git a/packages/core/src/modules/event/event.rpc.ts b/packages/core/src/modules/event/event.rpc.tsindex c1d3ce113..de6aefbf4 100644--- a/packages/core/src/modules/event/event.rpc.ts+++ b/packages/core/src/modules/event/event.rpc.ts@@ -182,7 +182,7 @@ export const eventRouter = createTRPCRouter({.query(async ({ input, ctx }) => {await requireAccess(ctx, input.projectId, 'read');- const { timezone } = await getSettingsForProject(input.projectId);+ const { timezone } = await getSettingsForProject(ctx, input.projectId);const { startDate, endDate } = getChartStartEndDate(input, timezone);return ctx.services.pages.getTopPages({projectId: input.projectId,@@ -199,7 +199,7 @@ export const eventRouter = createTRPCRouter({.query(async ({ input, ctx }) => {await requireAccess(ctx, input.projectId, 'read');- const { timezone } = await getSettingsForProject(input.projectId);+ const { timezone } = await getSettingsForProject(ctx, input.projectId);const { startDate, endDate } = getChartStartEndDate(input, timezone);return ctx.services.pages.getPageTimeseries({projectId: input.projectId,@@ -213,7 +213,7 @@ export const eventRouter = createTRPCRouter({previousPages: procedure.input(zChartWindow).query(async ({ input, ctx }) => {await requireAccess(ctx, input.projectId, 'read');- const { timezone } = await getSettingsForProject(input.projectId);+ const { timezone } = await getSettingsForProject(ctx, input.projectId);const { startDate, endDate } = getChartStartEndDate(input, timezone);const startMs = new Date(startDate).getTime();@@ -235,7 +235,7 @@ export const eventRouter = createTRPCRouter({.query(async ({ input, ctx }) => {await requireAccess(ctx, input.projectId, 'read');- const { timezone } = await getSettingsForProject(input.projectId);+ const { timezone } = await getSettingsForProject(ctx, input.projectId);const { startDate, endDate } = getChartStartEndDate(input, timezone);return ctx.services.pages.getPageTimeseries({projectId: input.projectId,diff --git a/packages/core/src/modules/event/event.service.ts b/packages/core/src/modules/event/event.service.tsindex 85c04abc1..bb1a4d1c2 100644--- a/packages/core/src/modules/event/event.service.ts+++ b/packages/core/src/modules/event/event.service.ts@@ -1154,7 +1154,7 @@ export async function getEventDetails(}const { getSessionById } = await loadSessionService();const session = event.sessionId- ? await getSessionById(event.sessionId, input.projectId).catch(+ ? await getSessionById(deps, event.sessionId, input.projectId).catch(() => undefined): undefined;diff --git a/packages/core/src/modules/export/export.routes.ts b/packages/core/src/modules/export/export.routes.tsindex 7f7d22ef1..cf4bb16e7 100644--- a/packages/core/src/modules/export/export.routes.ts+++ b/packages/core/src/modules/export/export.routes.ts@@ -279,7 +279,10 @@ export const exportRoutes = defineRoutes((app) =>// is undefined and JSON drops it.throw new HttpError(resolved.message, { status: resolved.status });}- const { timezone } = await getSettingsForProject(resolved.projectId);+ const { timezone } = await getSettingsForProject(+ ctx,+ resolved.projectId+ );const { events, series, ...rest } = query;const eventSeries = (series ?? events ?? []).map((event) => ({@@ -362,7 +365,7 @@ async function getOverviewGeneric(client: InsightsClient) {const projectId = await resolveInsightsProjectId(client, params);- const { timezone } = await getSettingsForProject(projectId);+ const { timezone } = await getSettingsForProject(ctx, projectId);const { startDate, endDate } = getChartStartEndDate(query, timezone);return ctx.services.overview.getTopGeneric({column,@@ -542,6 +545,7 @@ export const insightsRoutes = defineRoutes((app) =>async ({ params, query, client, ctx }) => {const projectId = await resolveInsightsProjectId(client, params);const { startDate, endDate } = await resolveInsightsDateRange(+ ctx,projectId,query);@@ -617,6 +621,7 @@ export const insightsRoutes = defineRoutes((app) =>async ({ params, query, client, ctx }) => {const projectId = await resolveInsightsProjectId(client, params);const { startDate, endDate } = await resolveInsightsDateRange(+ ctx,projectId,query);@@ -638,6 +643,7 @@ export const insightsRoutes = defineRoutes((app) =>async ({ params, query, client, ctx }) => {const projectId = await resolveInsightsProjectId(client, params);const { startDate, endDate } = await resolveInsightsDateRange(+ ctx,projectId,query);@@ -663,6 +669,7 @@ export const insightsRoutes = defineRoutes((app) =>async ({ params, query, client, ctx }) => {const projectId = await resolveInsightsProjectId(client, params);const { startDate, endDate } = await resolveInsightsDateRange(+ ctx,projectId,query);@@ -688,7 +695,7 @@ export const insightsRoutes = defineRoutes((app) =>'/insights/:projectId/metrics',async ({ params, query, client, ctx }) => {const projectId = await resolveInsightsProjectId(client, params);- const { timezone } = await getSettingsForProject(projectId);+ const { timezone } = await getSettingsForProject(ctx, projectId);const { startDate, endDate } = getChartStartEndDate(query, timezone);return ctx.services.overview.getMetrics({projectId,@@ -731,7 +738,7 @@ export const insightsRoutes = defineRoutes((app) =>'/insights/:projectId/pages',async ({ params, query, client, ctx }) => {const projectId = await resolveInsightsProjectId(client, params);- const { timezone } = await getSettingsForProject(projectId);+ const { timezone } = await getSettingsForProject(ctx, projectId);const { startDate, endDate } = getChartStartEndDate(query, timezone);return ctx.services.overview.getTopPages({projectId,@@ -1009,6 +1016,7 @@ export const insightsRoutes = defineRoutes((app) =>async ({ params, query, client, ctx }) => {const projectId = await resolveInsightsProjectId(client, params);const { startDate, endDate } = await resolveInsightsDateRange(+ ctx,projectId,query);@@ -1036,6 +1044,7 @@ export const insightsRoutes = defineRoutes((app) =>async ({ params, query, client, ctx }) => {const projectId = await resolveInsightsProjectId(client, params);const { startDate, endDate } = await resolveInsightsDateRange(+ ctx,projectId,query);@@ -1061,6 +1070,7 @@ export const insightsRoutes = defineRoutes((app) =>async ({ params, query, client, ctx }) => {const projectId = await resolveInsightsProjectId(client, params);const { startDate, endDate } = await resolveInsightsDateRange(+ ctx,projectId,query);@@ -1087,6 +1097,7 @@ export const insightsRoutes = defineRoutes((app) =>async ({ params, query, client, ctx }) => {const projectId = await resolveInsightsProjectId(client, params);const { startDate, endDate } = await resolveInsightsDateRange(+ ctx,projectId,query);@@ -1112,6 +1123,7 @@ export const insightsRoutes = defineRoutes((app) =>async ({ params, query, client, ctx }) => {const projectId = await resolveInsightsProjectId(client, params);const { startDate, endDate } = await resolveInsightsDateRange(+ ctx,projectId,query);@@ -1300,9 +1312,9 @@ export const insightsRoutes = defineRoutes((app) =>).get('/insights/:projectId/sessions',- async ({ params, query, client }) => {+ async ({ params, query, client, ctx }) => {const projectId = await resolveInsightsProjectId(client, params);- return querySessionsCore({ projectId, ...query });+ return querySessionsCore(ctx, { projectId, ...query });},{clientAuth: CLIENT_ALLOW,@@ -1386,13 +1398,14 @@ export const insightsRoutes = defineRoutes((app) =>).get('/insights/:projectId/gsc/overview',- async ({ params, query, client }) => {+ async ({ params, query, client, ctx }) => {const projectId = await resolveInsightsProjectId(client, params);const { startDate, endDate } = await resolveInsightsDateRange(+ ctx,projectId,query);- return gscGetOverviewCore({+ return gscGetOverviewCore(ctx, {projectId,startDate,endDate,@@ -1412,13 +1425,14 @@ export const insightsRoutes = defineRoutes((app) =>).get('/insights/:projectId/gsc/pages',- async ({ params, query, client }) => {+ async ({ params, query, client, ctx }) => {const projectId = await resolveInsightsProjectId(client, params);const { startDate, endDate } = await resolveInsightsDateRange(+ ctx,projectId,query);- return gscGetTopPagesCore({+ return gscGetTopPagesCore(ctx, {projectId,startDate,endDate,@@ -1437,13 +1451,14 @@ export const insightsRoutes = defineRoutes((app) =>).get('/insights/:projectId/gsc/pages/details',- async ({ params, query, client }) => {+ async ({ params, query, client, ctx }) => {const projectId = await resolveInsightsProjectId(client, params);const { startDate, endDate } = await resolveInsightsDateRange(+ ctx,projectId,query);- return gscGetPageDetailsCore({+ return gscGetPageDetailsCore(ctx, {projectId,startDate,endDate,@@ -1462,13 +1477,14 @@ export const insightsRoutes = defineRoutes((app) =>).get('/insights/:projectId/gsc/queries',- async ({ params, query, client }) => {+ async ({ params, query, client, ctx }) => {const projectId = await resolveInsightsProjectId(client, params);const { startDate, endDate } = await resolveInsightsDateRange(+ ctx,projectId,query);- return gscGetTopQueriesCore({+ return gscGetTopQueriesCore(ctx, {projectId,startDate,endDate,@@ -1487,13 +1503,14 @@ export const insightsRoutes = defineRoutes((app) =>).get('/insights/:projectId/gsc/queries/details',- async ({ params, query, client }) => {+ async ({ params, query, client, ctx }) => {const projectId = await resolveInsightsProjectId(client, params);const { startDate, endDate } = await resolveInsightsDateRange(+ ctx,projectId,query);- return gscGetQueryDetailsCore({+ return gscGetQueryDetailsCore(ctx, {projectId,startDate,endDate,@@ -1512,13 +1529,14 @@ export const insightsRoutes = defineRoutes((app) =>).get('/insights/:projectId/gsc/queries/opportunities',- async ({ params, query, client }) => {+ async ({ params, query, client, ctx }) => {const projectId = await resolveInsightsProjectId(client, params);const { startDate, endDate } = await resolveInsightsDateRange(+ ctx,projectId,query);- return gscGetQueryOpportunitiesCore({+ return gscGetQueryOpportunitiesCore(ctx, {projectId,startDate,endDate,@@ -1538,13 +1556,18 @@ export const insightsRoutes = defineRoutes((app) =>).get('/insights/:projectId/gsc/cannibalization',- async ({ params, query, client }) => {+ async ({ params, query, client, ctx }) => {const projectId = await resolveInsightsProjectId(client, params);const { startDate, endDate } = await resolveInsightsDateRange(+ ctx,projectId,query);- return gscGetCannibalizationCore({ projectId, startDate, endDate });+ return gscGetCannibalizationCore(ctx, {+ projectId,+ startDate,+ endDate,+ });},{clientAuth: CLIENT_ALLOW,diff --git a/packages/core/src/modules/export/export.service.ts b/packages/core/src/modules/export/export.service.tsindex f6059494b..708362261 100644--- a/packages/core/src/modules/export/export.service.ts+++ b/packages/core/src/modules/export/export.service.ts@@ -102,13 +102,14 @@ export async function resolveInsightsProjectId(* needs the project's timezone to resolve; an explicit `startDate` does not.*/export async function resolveInsightsDateRange(+ deps: ServiceDeps,projectId: string,data: { startDate?: string; endDate?: string; range?: IChartRange }): Promise<{ startDate: string; endDate: string }> {if (!data.range || data.startDate) {return resolveDateRange(data.startDate, data.endDate);}- const { timezone } = await getSettingsForProject(projectId);+ const { timezone } = await getSettingsForProject(deps, projectId);return getChartStartEndDate({ startDate: data.startDate, endDate: data.endDate, range: data.range },timezone@@ -137,6 +138,7 @@ export function createExportService(deps: ServiceDeps): ExportService {resolveExportProjectId: (client, query) =>resolveExportProjectId(deps, client, query),resolveInsightsProjectId,- resolveInsightsDateRange,+ resolveInsightsDateRange: (projectId, data) =>+ resolveInsightsDateRange(deps, projectId, data),};}diff --git a/packages/core/src/modules/gsc/gsc.jobs.test.ts b/packages/core/src/modules/gsc/gsc.jobs.test.tsindex 71ebd1e26..67974017e 100644--- a/packages/core/src/modules/gsc/gsc.jobs.test.ts+++ b/packages/core/src/modules/gsc/gsc.jobs.test.ts@@ -29,6 +29,7 @@ function stubJobCtx(gsc: Partial<GscService>, jobName = 'test'): JobCtx {auth: {} as Services['auth'],notification: {} as Services['notification'],insight: {} as Services['insight'],+ integration: {} as Services['integration'],cohort: {} as Services['cohort'],import: {} as Services['import'],ingest: {} as Services['ingest'],@@ -40,6 +41,10 @@ function stubJobCtx(gsc: Partial<GscService>, jobName = 'test'): JobCtx {profile: {} as Services['profile'],group: {} as Services['group'],chart: {} as Services['chart'],+ funnel: {} as Services['funnel'],+ conversion: {} as Services['conversion'],+ sankey: {} as Services['sankey'],+ retention: {} as Services['retention'],overview: {} as Services['overview'],pages: {} as Services['pages'],realtime: {} as Services['realtime'],@@ -147,6 +152,7 @@ test('gscSync fans out one gscProjectSync enqueue per connected project', asyncauth: {} as Services['auth'],notification: {} as Services['notification'],insight: {} as Services['insight'],+ integration: {} as Services['integration'],cohort: {} as Services['cohort'],import: {} as Services['import'],ingest: {} as Services['ingest'],@@ -163,6 +169,10 @@ test('gscSync fans out one gscProjectSync enqueue per connected project', asyncprofile: {} as Services['profile'],group: {} as Services['group'],chart: {} as Services['chart'],+ funnel: {} as Services['funnel'],+ conversion: {} as Services['conversion'],+ sankey: {} as Services['sankey'],+ retention: {} as Services['retention'],overview: {} as Services['overview'],pages: {} as Services['pages'],realtime: {} as Services['realtime'],diff --git a/packages/core/src/modules/gsc/gsc.routes.ts b/packages/core/src/modules/gsc/gsc.routes.tsindex 567c9331a..e6eaf99b0 100644--- a/packages/core/src/modules/gsc/gsc.routes.ts+++ b/packages/core/src/modules/gsc/gsc.routes.ts@@ -46,7 +46,7 @@ export const gscRoutes = defineRoutes((app) =>}try {- const { organizationId } = await completeGscOAuthCallback({+ const { organizationId } = await completeGscOAuthCallback(ctx, {code: query.code,state: query.state,storedState,diff --git a/packages/core/src/modules/gsc/gsc.service.test.ts b/packages/core/src/modules/gsc/gsc.service.test.tsindex ea96d4e6c..a3d6cb37f 100644--- a/packages/core/src/modules/gsc/gsc.service.test.ts+++ b/packages/core/src/modules/gsc/gsc.service.test.ts@@ -84,48 +84,37 @@ const project = {findUnique: mock(async ({ where: { id } }: { where: { id: string } }) =>id === 'proj_missing' ? null : { id, organizationId: 'org_1' }),+ // `resolveGscDateRange` reaches organization.service's `getSettingsForProject`+ // over the SAME deps (M10-009), which reads the project's organization+ // rather than going through a mocked module.+ findUniqueOrThrow: mock(async () => ({+ organization: { timezone: 'UTC' },+ })),};-// Spread the real module — see the clickhouse/client mock below for why a-// partial factory here is a process-wide hazard, not a local one.-const actualPrismaClient = await import('@openpanel/db/src/prisma-client');-mock.module('@openpanel/db/src/prisma-client', () => ({- ...actualPrismaClient,- db: { gscConnection, project },-}));-const chQuery = mock(async () => [] as unknown[]);-const originalCh = {+const ch = {query: mock(async () => ({ json: async () => [] as unknown[] })),insert: mock(async () => undefined),};-// Spread the real module rather than hand-listing every export: `mock.module`-// replaces this specifier process-wide (bun runs every test file in one-// shared module registry without `--isolate` — see AGENTS.md), so a partial-// factory here silently breaks unrelated consumers (insight/cohort/import-// tests and now the mcp module's) that import an export this file never-// overrides. `ch` itself is one such export: gsc.service.ts never calls it-// (it goes through `originalCh`/`chQuery` — see the file's header), so it is-// spread wholesale rather than replaced, keeping `query` intact for every-// *other* consumer of this same live-bound singleton (e.g. `@openpanel/db`'s-// `OverviewService`/`PagesService`, constructed once at that module's own-// load time) for the rest of the process.-// A plain-object snapshot, not the live import binding: once `mock.module`-// below swaps this specifier, `actualClickhouseClient.chQuery` (a namespace-// binding) reflects the *mocked* value too, so restoring via-// `actualClickhouseClient` itself in `afterAll` is a no-op — it just spreads-// back whatever is currently mocked. Snapshotting into a plain object first-// keeps a real, frozen-in-time copy to restore to.-const actualClickhouseClient = await import(- '@openpanel/db/src/clickhouse/client'-);-const realClickhouseClient = { ...actualClickhouseClient };-mock.module('@openpanel/db/src/clickhouse/client', () => ({- ...realClickhouseClient,- originalCh,- chQuery,++// M10-009: the subject's functions take `ServiceDeps`, so `deps.db`/`deps.ch`+// ARE the fakes above — no `@openpanel/db` module mock is needed for either.+// The one module still mocked is core's own `shared/ch-query`, which is what+// the subject now calls.+// `mock.module` has no per-file scope under bare `bun test` (AGENTS.md), so+// snapshot the real module into a plain object FIRST and restore it in+// afterAll — restoring via the live import binding would re-apply the mock.+const realChQuery = { ...(await import('../../shared/ch-query')) };+mock.module('../../shared/ch-query', () => ({+ ...realChQuery,+ chQuery: (_deps: unknown, ...args: unknown[]) => chQuery(...(args as [])),}));+const deps = {+ db: { gscConnection, project },+ ch,+} as unknown as import('../../services').ServiceDeps;// Bypasses the Redis cache-aside entirely — `getGscCannibalization`'s own// logic is exercised directly, its caching is @openpanel/redis's concern.// `getRedisCache` is unused here but included for the same cross-file@@ -140,18 +129,18 @@ mock.module('@openpanel/redis', () => ({}),}));-// `chQuery` above is a fake stuck in place for the rest of the process once-// this file's tests finish (`mock.module` has no per-file scope without-// `--isolate` — see AGENTS.md): the mcp module's integration suite calls the-// real `chQuery` against a live ClickHouse and silently got `[]` back from-// this file's leftover mock. Restore the real snapshot so whichever file-// runs next sees real behavior again.+// `@openpanel/redis` above is a fake stuck in place for the rest of the+// process once this file's tests finish (`mock.module` has no per-file scope+// without `--isolate` — see AGENTS.md). Restore the real snapshot so whichever+// file runs next sees real behavior again.afterAll(() => {+ mock.module('@openpanel/redis', () => realRedis);+ mock.module('../../shared/ch-query', () => realChQuery);mock.module(- '@openpanel/db/src/clickhouse/client',- () => realClickhouseClient+ '../organization/organization.service',+ () => realOrganizationService);- mock.module('@openpanel/redis', () => realRedis);+ mock.module('../../shared/date', () => realSharedDate);});const validateAuthorizationCode = mock(async () => ({@@ -172,20 +161,29 @@ mock.module('../auth/auth.service', () => ({googleGsc: { validateAuthorizationCode },}));-const getSettingsForProject = mock(async () => ({ timezone: 'UTC' }));+const getSettingsForProject = mock(+ async (_deps: unknown, _projectId: string) => ({ timezone: 'UTC' })+);const getChartStartEndDate = mock(() => ({startDate: '2026-09-01T00:00:00.000Z',endDate: '2026-09-03T00:00:00.000Z',}));-// Spread the real module — see the clickhouse/client mock above for why a-// partial factory here is a process-wide hazard, not a local one. The-// organization/date loaders both resolve through this same specifier now-// (M9-CLEANUP-001) — one mock.module call, not two, or the second replaces-// the whole module and drops the first override.-const actualCore = await import('@openpanel/core');-mock.module('@openpanel/core', () => ({- ...actualCore,+// M10-009: `resolveGscDateRange` reaches these two through the DEEP modules+// they live in, not through this package's barrel (`core-no-self-barrel`), so+// that is what has to be mocked — mocking `@openpanel/core` would no longer+// intercept anything. Each factory spreads its real module: `mock.module`+// replaces a specifier process-wide under bare `bun test` (AGENTS.md), and+// both are restored from a plain-object snapshot in afterAll below.+const realOrganizationService = {+ ...(await import('../organization/organization.service')),+};+mock.module('../organization/organization.service', () => ({+ ...realOrganizationService,getSettingsForProject,+}));+const realSharedDate = { ...(await import('../../shared/date')) };+mock.module('../../shared/date', () => ({+ ...realSharedDate,getChartStartEndDate,}));@@ -231,20 +229,20 @@ test('listGscConnectionsForSync returns only projects with a siteUrl', async ()backfillStatus: null,});- const result = await subject.listGscConnectionsForSync();+ const result = await subject.listGscConnectionsForSync(deps);expect(result).toEqual([{ projectId: 'p1' }]);});test('getGscConnection returns null when no connection exists', async () => {gscConnectionStore.clear();- const result = await subject.getGscConnection('missing');+ const result = await subject.getGscConnection(deps, 'missing');expect(result).toBeNull();});test('selectGscSite throws TRPCNotFoundError when the connection is missing', async () => {gscConnectionStore.clear();await expect(- subject.selectGscSite('missing', 'https://example.com')+ subject.selectGscSite(deps, 'missing', 'https://example.com')).rejects.toMatchObject({ message: 'GSC connection not found' });});@@ -261,7 +259,7 @@ test('selectGscSite updates siteUrl and resets backfillStatus to pending', asyncbackfillStatus: null,});- await subject.selectGscSite('p1', 'https://example.com');+ await subject.selectGscSite(deps, 'p1', 'https://example.com');expect(gscConnectionStore.get('p1')).toMatchObject({siteUrl: 'https://example.com',@@ -282,14 +280,14 @@ test('disconnectGscConnection removes the stored connection', async () => {backfillStatus: null,});- await subject.disconnectGscConnection('p1');+ await subject.disconnectGscConnection(deps, 'p1');expect(gscConnectionStore.has('p1')).toBe(false);});test('runGscProjectSync skips silently when there is no connected site', async () => {gscConnectionStore.clear();await expect(- subject.runGscProjectSync('missing', stubLogger())+ subject.runGscProjectSync(deps, 'missing', stubLogger())).resolves.toBeUndefined();});@@ -310,7 +308,9 @@ test('runGscProjectSync records an error status when the token refresh has no crdelete process.env.GOOGLE_CLIENT_ID;delete process.env.GOOGLE_CLIENT_SECRET;- await expect(subject.runGscProjectSync('p1', stubLogger())).rejects.toThrow();+ await expect(+ subject.runGscProjectSync(deps, 'p1', stubLogger())+ ).rejects.toThrow();expect(gscConnectionStore.get('p1')?.lastSyncStatus).toBe('error');@@ -325,7 +325,7 @@ test('runGscProjectSync records an error status when the token refresh has no crtest('completeGscOAuthCallback rejects a state mismatch before calling Google', async () => {validateAuthorizationCode.mockClear();await expect(- subject.completeGscOAuthCallback({+ subject.completeGscOAuthCallback(deps, {code: 'code',state: 'a',storedState: 'b',@@ -338,7 +338,7 @@ test('completeGscOAuthCallback rejects a state mismatch before calling Google',test('completeGscOAuthCallback rejects an unknown project', async () => {await expect(- subject.completeGscOAuthCallback({+ subject.completeGscOAuthCallback(deps, {code: 'code',state: 'a',storedState: 'a',@@ -350,7 +350,7 @@ test('completeGscOAuthCallback rejects an unknown project', async () => {test('completeGscOAuthCallback upserts the connection and returns the organizationId', async () => {gscConnectionStore.clear();- const result = await subject.completeGscOAuthCallback({+ const result = await subject.completeGscOAuthCallback(deps, {code: 'code',state: 'a',storedState: 'a',@@ -363,25 +363,30 @@ test('completeGscOAuthCallback upserts the connection and returns the organizati});test('resolveGscDateRange resolves through the org timezone and chart date helper', async () => {- const result = await subject.resolveGscDateRange('p1', { range: '7d' });+ const result = await subject.resolveGscDateRange(deps, 'p1', { range: '7d' });expect(result).toEqual({ startDate: '2026-09-01', endDate: '2026-09-03' });- expect(getSettingsForProject).toHaveBeenCalledWith('p1');+ expect(getSettingsForProject).toHaveBeenCalledWith(deps, 'p1');});test('getGscOverview returns the ClickHouse rows as-is', async () => {const rows = [{ date: '2026-09-01', clicks: 1, impressions: 2, ctr: 0.5, position: 3 },];- originalCh.query.mockImplementationOnce(async () => ({+ ch.query.mockImplementationOnce(async () => ({json: async () => rows,}));- const result = await subject.getGscOverview('p1', '2026-09-01', '2026-09-03');+ const result = await subject.getGscOverview(+ deps,+ 'p1',+ '2026-09-01',+ '2026-09-03'+ );expect(result).toEqual(rows);});test('gscGetOverviewCore aggregates the daily rows into a summary', async () => {- originalCh.query.mockImplementationOnce(async () => ({+ ch.query.mockImplementationOnce(async () => ({json: async () => [{date: '2026-09-01',@@ -400,7 +405,7 @@ test('gscGetOverviewCore aggregates the daily rows into a summary', async () =>],}));- const result = await subject.gscGetOverviewCore({+ const result = await subject.gscGetOverviewCore(deps, {projectId: 'p1',startDate: '2026-09-01',endDate: '2026-09-02',@@ -415,7 +420,7 @@ test('gscGetOverviewCore aggregates the daily rows into a summary', async () =>});test('gscGetQueryOpportunitiesCore filters and scores mid-ranked, high-impression queries', async () => {- originalCh.query.mockImplementationOnce(async () => ({+ ch.query.mockImplementationOnce(async () => ({json: async () => [// survives the impressions prefilter, but position 1 is out of the// opportunity band [4, 20] — excluded by computeOpportunities@@ -439,7 +444,7 @@ test('gscGetQueryOpportunitiesCore filters and scores mid-ranked, high-impressio],}));- const result = await subject.gscGetQueryOpportunitiesCore({+ const result = await subject.gscGetQueryOpportunitiesCore(deps, {projectId: 'p1',startDate: '2026-09-01',endDate: '2026-09-02',diff --git a/packages/core/src/modules/gsc/gsc.service.ts b/packages/core/src/modules/gsc/gsc.service.tsindex 9049c4f32..e81fe95e3 100644--- a/packages/core/src/modules/gsc/gsc.service.ts+++ b/packages/core/src/modules/gsc/gsc.service.ts@@ -5,21 +5,32 @@// packages/db/src/services/gsc.service.ts become re-export shims of this// file (same shape as packages/db/src/encryption.ts since M4-006).//-// db/ch access is LAZY (`load*` below), not a static top-level import — see-// insight.service.ts's header for the full reasoning (jobs.registry.ts and-// services.ts pull this module into the eager barrel chain nearly every core-// test file reaches, and constructing @openpanel/db's clients at import time-// would spawn a pino-pretty transport worker thread per test file).+// M10-009: every exported function takes `ServiceDeps` and reaches Postgres+// as `deps.db` and ClickHouse as `deps.ch`; the `loadDb()`/`loadChClient()`+// lazy loaders are gone, so the requestId minted at the edge reaches the query+// (ADR-018, docs/TECH_DEBT.md §4). `getGscCannibalization` is the exception:+// `cacheable` keys on the call's ARGUMENTS (packages/redis/cachable.ts) so it+// cannot take `deps` as a leading parameter, and reaches the boot scope+// through the declared v1-compat seam instead.//-// ClickHouse queries here still go through raw `originalCh`/`chQuery` calls,-// not the `sql` tag: ADR-013 converts the analytics read path one query per-// P7 task, and this module's queries haven't been converted yet.+// DELIBERATE BEHAVIOUR CHANGE, recorded: the reads and the four sync inserts+// used @openpanel/db's `originalCh` (the first configured node, no retry).+// `deps.ch` is the round-robin/retry proxy every other core module already+// uses, which also applies the shared INSERT_DEFAULT_SETTINGS+// (`wait_end_of_query`, insert block size) to those inserts. Same rows, same+// tables; strictly more failover.+//+// ClickHouse queries here still go through raw SQL strings, not the `sql`+// tag: ADR-013 converts the analytics read path one query per P7 task, and+// this module's queries haven't been converted yet.import { cacheable } from '@openpanel/redis';import { createLogger, type ILogger } from '../../clients/logger';import type { Logger } from '../../logger';import { TRPCNotFoundError } from '../../rpc/errors';import type { ServiceDeps } from '../../services';+import { chQuery } from '../../shared/ch-query';+import { TABLE_NAMES } from '../../shared/ch-tables';import { decrypt, encrypt } from '../../shared/encryption';import { googleGsc } from '../auth/auth.service';@@ -34,14 +45,10 @@ function getLogger(): ILogger {return _logger;}-function loadDb() {- return import('@openpanel/db/src/prisma-client').then((m) => m.db);-}--function loadChClient() {- return import('@openpanel/db/src/clickhouse/client');+/** `getGscCannibalization` is `cacheable` — see the header. */+function loadCompatServiceDeps() {+ return import('../../v1-compat').then((m) => m.compatServiceDeps());}-export interface GscSite {siteUrl: string;permissionLevel: string;@@ -82,8 +89,11 @@ async function refreshGscToken(return { accessToken: data.access_token, expiresAt };}-export async function getGscAccessToken(projectId: string): Promise<string> {- const db = await loadDb();+export async function getGscAccessToken(+ deps: ServiceDeps,+ projectId: string+): Promise<string> {+ const db = deps.db;const conn = await db.gscConnection.findUniqueOrThrow({where: { projectId },});@@ -144,8 +154,11 @@ export async function getGscAccessToken(projectId: string): Promise<string> {}}-export async function listGscSites(projectId: string): Promise<GscSite[]> {- const accessToken = await getGscAccessToken(projectId);+export async function listGscSites(+ deps: ServiceDeps,+ projectId: string+): Promise<GscSite[]> {+ const accessToken = await getGscAccessToken(deps, projectId);const res = await fetch('https://www.googleapis.com/webmasters/v3/sites', {headers: { Authorization: `Bearer ${accessToken}` },});@@ -240,11 +253,12 @@ function nowString(): string {}export async function syncGscData(+ deps: ServiceDeps,projectId: string,startDate: Date,endDate: Date): Promise<void> {- const db = await loadDb();+ const db = deps.db;const conn = await db.gscConnection.findUniqueOrThrow({where: { projectId },});@@ -253,11 +267,11 @@ export async function syncGscData(throw new Error('No GSC site URL configured for this project');}- const accessToken = await getGscAccessToken(projectId);+ const accessToken = await getGscAccessToken(deps, projectId);const start = formatDate(startDate);const end = formatDate(endDate);const syncedAt = nowString();- const { originalCh } = await loadChClient();+ const ch = deps.ch;// 1. Daily totals — authoritative numbers for overview chartconst dailyRows = await queryGscSearchAnalytics(@@ -269,7 +283,7 @@ export async function syncGscData();if (dailyRows.length > 0) {- await originalCh.insert({+ await ch.insert({table: 'gsc_daily',values: dailyRows.map((row) => ({project_id: projectId,@@ -294,7 +308,7 @@ export async function syncGscData();if (pageRows.length > 0) {- await originalCh.insert({+ await ch.insert({table: 'gsc_pages_daily',values: pageRows.map((row) => ({project_id: projectId,@@ -320,7 +334,7 @@ export async function syncGscData();if (queryRows.length > 0) {- await originalCh.insert({+ await ch.insert({table: 'gsc_queries_daily',values: queryRows.map((row) => ({project_id: projectId,@@ -338,6 +352,7 @@ export async function syncGscData(}export async function getGscOverview(+ deps: ServiceDeps,projectId: string,startDate: string,endDate: string,@@ -358,8 +373,8 @@ export async function getGscOverview(? 'toStartOfWeek(date)': 'date';- const { originalCh } = await loadChClient();- const result = await originalCh.query({+ const ch = deps.ch;+ const result = await ch.query({query: `SELECT${dateExpr} as date,@@ -382,6 +397,7 @@ export async function getGscOverview(}export async function getGscPages(+ deps: ServiceDeps,projectId: string,startDate: string,endDate: string,@@ -395,8 +411,8 @@ export async function getGscPages(position: number;}>> {- const { originalCh } = await loadChClient();- const result = await originalCh.query({+ const ch = deps.ch;+ const result = await ch.query({query: `SELECTpage,@@ -433,16 +449,18 @@ export interface GscCannibalizedQuery {}export const getGscCannibalization = cacheable(+ 'getGscCannibalization',async (projectId: string,startDate: string,endDate: string): Promise<GscCannibalizedQuery[]> => {- const db = await loadDb();+ const deps = await loadCompatServiceDeps();+ const db = deps.db;const conn = await db.gscConnection.findUniqueOrThrow({where: { projectId },});- const accessToken = await getGscAccessToken(projectId);+ const accessToken = await getGscAccessToken(deps, projectId);const rows = await queryGscSearchAnalytics(accessToken,@@ -526,6 +544,7 @@ export const getGscCannibalization = cacheable();export async function getGscPageDetails(+ deps: ServiceDeps,projectId: string,page: string,startDate: string,@@ -546,11 +565,11 @@ export async function getGscPageDetails(position: number;}>;}> {- const db = await loadDb();+ const db = deps.db;const conn = await db.gscConnection.findUniqueOrThrow({where: { projectId },});- const accessToken = await getGscAccessToken(projectId);+ const accessToken = await getGscAccessToken(deps, projectId);const filterGroups: GscFilterGroup[] = [{ filters: [{ dimension: 'page', operator: 'equals', expression: page }] },];@@ -593,6 +612,7 @@ export async function getGscPageDetails(}export async function getGscQueryDetails(+ deps: ServiceDeps,projectId: string,query: string,startDate: string,@@ -613,11 +633,11 @@ export async function getGscQueryDetails(position: number;}>;}> {- const db = await loadDb();+ const db = deps.db;const conn = await db.gscConnection.findUniqueOrThrow({where: { projectId },});- const accessToken = await getGscAccessToken(projectId);+ const accessToken = await getGscAccessToken(deps, projectId);const filterGroups: GscFilterGroup[] = [{filters: [{ dimension: 'query', operator: 'equals', expression: query }],@@ -662,6 +682,7 @@ export async function getGscQueryDetails(}export async function getGscQueries(+ deps: ServiceDeps,projectId: string,startDate: string,endDate: string,@@ -675,8 +696,8 @@ export async function getGscQueries(position: number;}>> {- const { originalCh } = await loadChClient();- const result = await originalCh.query({+ const ch = deps.ch;+ const result = await ch.query({query: `SELECTquery,@@ -716,9 +737,10 @@ export interface GscConnectionSummary {}export async function getGscConnection(+ deps: ServiceDeps,projectId: string): Promise<GscConnectionSummary | null> {- const db = await loadDb();+ const db = deps.db;return db.gscConnection.findUnique({where: { projectId },select: {@@ -735,10 +757,11 @@ export async function getGscConnection(}export async function selectGscSite(+ deps: ServiceDeps,projectId: string,siteUrl: string): Promise<void> {- const db = await loadDb();+ const db = deps.db;const conn = await db.gscConnection.findUnique({ where: { projectId } });if (!conn) {throw new TRPCNotFoundError('GSC connection not found');@@ -751,9 +774,10 @@ export async function selectGscSite(}export async function disconnectGscConnection(+ deps: ServiceDeps,projectId: string): Promise<void> {- const db = await loadDb();+ const db = deps.db;await db.gscConnection.deleteMany({ where: { projectId } });}@@ -770,16 +794,15 @@ export interface GscDateRangeInput {}export async function resolveGscDateRange(+ deps: ServiceDeps,projectId: string,input: GscDateRangeInput): Promise<{ startDate: string; endDate: string }> {const { getSettingsForProject } = await import(- '@openpanel/core'- );- const { getChartStartEndDate } = await import(- '@openpanel/core'+ '../organization/organization.service');- const { timezone } = await getSettingsForProject(projectId);+ const { getChartStartEndDate } = await import('../../shared/date');+ const { timezone } = await getSettingsForProject(deps, projectId);const { startDate, endDate } = getChartStartEndDate({range: input.range as never,@@ -888,12 +911,12 @@ function getComparisonWindows(startDate: string, endDate: string) {}export async function getGscSearchEngines(+ deps: ServiceDeps,projectId: string,startDate: string,endDate: string) {const windows = getComparisonWindows(startDate, endDate);- const { chQuery, TABLE_NAMES } = await loadChClient();const where = (window: { start: string; end: string }) =>`project_id = '${projectId}'@@ -903,6 +926,7 @@ export async function getGscSearchEngines(const [engines, [currentResult], [prevResult]] = await Promise.all([chQuery<{ name: string; sessions: number }>(+ deps,`SELECTreferrer_name as name,count(*) as sessions@@ -913,11 +937,13 @@ export async function getGscSearchEngines(LIMIT 10`),chQuery<{ sessions: number }>(+ deps,`SELECT count(*) as sessionsFROM ${TABLE_NAMES.sessions}WHERE ${where(windows.current)}`),chQuery<{ sessions: number }>(+ deps,`SELECT count(*) as sessionsFROM ${TABLE_NAMES.sessions}WHERE ${where(windows.previous)}`@@ -934,12 +960,12 @@ export async function getGscSearchEngines(}export async function getGscAiEngines(+ deps: ServiceDeps,projectId: string,startDate: string,endDate: string) {const windows = getComparisonWindows(startDate, endDate);- const { chQuery, TABLE_NAMES } = await loadChClient();// Matched by name — will switch to referrer_type = 'ai' once available.const where = (window: { start: string; end: string }) =>@@ -950,6 +976,7 @@ export async function getGscAiEngines(const [engines, [prevResult]] = await Promise.all([chQuery<{ name: string; sessions: number }>(+ deps,`${AI_REFERRER_CTE}SELECT ${AI_REFERRER_CANONICAL_NAME} as name, count(*) as sessionsFROM ${TABLE_NAMES.sessions}@@ -958,6 +985,7 @@ export async function getGscAiEngines(ORDER BY sessions DESC`),chQuery<{ sessions: number }>(+ deps,`${AI_REFERRER_CTE}SELECT count(*) as sessionsFROM ${TABLE_NAMES.sessions}@@ -975,6 +1003,7 @@ export async function getGscAiEngines(}export async function getGscPreviousOverview(+ deps: ServiceDeps,projectId: string,startDate: string,endDate: string,@@ -986,7 +1015,13 @@ export async function getGscPreviousOverview(const prevStart = new Date(prevEnd.getTime() - duration);const fmt = (d: Date) => d.toISOString().slice(0, 10);- return getGscOverview(projectId, fmt(prevStart), fmt(prevEnd), interval);+ return getGscOverview(+ deps,+ projectId,+ fmt(prevStart),+ fmt(prevEnd),+ interval+ );}// ---------------------------------------------------------------------------@@ -1073,13 +1108,17 @@ function computeOpportunities(.slice(0, 50);}-export async function gscGetOverviewCore(input: {- projectId: string;- startDate: string;- endDate: string;- interval?: 'day' | 'week' | 'month';-}) {+export async function gscGetOverviewCore(+ deps: ServiceDeps,+ input: {+ projectId: string;+ startDate: string;+ endDate: string;+ interval?: 'day' | 'week' | 'month';+ }+) {const data = await getGscOverview(+ deps,input.projectId,input.startDate,input.endDate,@@ -1106,13 +1145,17 @@ export async function gscGetOverviewCore(input: {};}-export async function gscGetTopPagesCore(input: {- projectId: string;- startDate: string;- endDate: string;- limit?: number;-}) {+export async function gscGetTopPagesCore(+ deps: ServiceDeps,+ input: {+ projectId: string;+ startDate: string;+ endDate: string;+ limit?: number;+ }+) {return getGscPages(+ deps,input.projectId,input.startDate,input.endDate,@@ -1120,13 +1163,17 @@ export async function gscGetTopPagesCore(input: {);}-export async function gscGetPageDetailsCore(input: {- projectId: string;- startDate: string;- endDate: string;- page: string;-}) {+export async function gscGetPageDetailsCore(+ deps: ServiceDeps,+ input: {+ projectId: string;+ startDate: string;+ endDate: string;+ page: string;+ }+) {return getGscPageDetails(+ deps,input.projectId,input.page,input.startDate,@@ -1134,13 +1181,17 @@ export async function gscGetPageDetailsCore(input: {);}-export async function gscGetTopQueriesCore(input: {- projectId: string;- startDate: string;- endDate: string;- limit?: number;-}) {+export async function gscGetTopQueriesCore(+ deps: ServiceDeps,+ input: {+ projectId: string;+ startDate: string;+ endDate: string;+ limit?: number;+ }+) {return getGscQueries(+ deps,input.projectId,input.startDate,input.endDate,@@ -1148,13 +1199,17 @@ export async function gscGetTopQueriesCore(input: {);}-export async function gscGetQueryOpportunitiesCore(input: {- projectId: string;- startDate: string;- endDate: string;- minImpressions?: number;-}) {+export async function gscGetQueryOpportunitiesCore(+ deps: ServiceDeps,+ input: {+ projectId: string;+ startDate: string;+ endDate: string;+ minImpressions?: number;+ }+) {const queries = await getGscQueries(+ deps,input.projectId,input.startDate,input.endDate,@@ -1171,13 +1226,17 @@ export async function gscGetQueryOpportunitiesCore(input: {};}-export async function gscGetQueryDetailsCore(input: {- projectId: string;- startDate: string;- endDate: string;- query: string;-}) {+export async function gscGetQueryDetailsCore(+ deps: ServiceDeps,+ input: {+ projectId: string;+ startDate: string;+ endDate: string;+ query: string;+ }+) {return getGscQueryDetails(+ deps,input.projectId,input.query,input.startDate,@@ -1185,11 +1244,14 @@ export async function gscGetQueryDetailsCore(input: {);}-export async function gscGetCannibalizationCore(input: {- projectId: string;- startDate: string;- endDate: string;-}) {+export async function gscGetCannibalizationCore(+ deps: ServiceDeps,+ input: {+ projectId: string;+ startDate: string;+ endDate: string;+ }+) {return getGscCannibalization(input.projectId, input.startDate, input.endDate);}@@ -1199,10 +1261,10 @@ export async function gscGetCannibalizationCore(input: {// ---------------------------------------------------------------------------/** The `gscSync` cron fan-out: every project with a connected GSC site. */-export async function listGscConnectionsForSync(): Promise<- { projectId: string }[]-> {- const db = await loadDb();+export async function listGscConnectionsForSync(+ deps: ServiceDeps+): Promise<{ projectId: string }[]> {+ const db = deps.db;return db.gscConnection.findMany({where: { siteUrl: { not: '' } },select: { projectId: true },@@ -1211,10 +1273,11 @@ export async function listGscConnectionsForSync(): Promise</** The `gscProjectSync` job body: rolling 3-day window (GSC data arrives late). */export async function runGscProjectSync(+ deps: ServiceDeps,projectId: string,logger: Logger = getLogger()): Promise<void> {- const db = await loadDb();+ const db = deps.db;const conn = await db.gscConnection.findUnique({ where: { projectId } });if (!conn?.siteUrl) {logger.warn({ projectId }, 'GSC sync skipped: no connection or siteUrl');@@ -1227,7 +1290,7 @@ export async function runGscProjectSync(const startDate = new Date(endDate);startDate.setDate(startDate.getDate() - 2); // 3 days total- await syncGscData(projectId, startDate, endDate);+ await syncGscData(deps, projectId, startDate, endDate);await db.gscConnection.update({where: { projectId },@@ -1255,10 +1318,11 @@ export async function runGscProjectSync(/** The `gscProjectBackfill` job body: chunked to avoid timeouts and API limits. */export async function runGscProjectBackfill(+ deps: ServiceDeps,projectId: string,logger: Logger = getLogger()): Promise<void> {- const db = await loadDb();+ const db = deps.db;const conn = await db.gscConnection.findUnique({ where: { projectId } });if (!conn?.siteUrl) {logger.warn(@@ -1298,7 +1362,7 @@ export async function runGscProjectBackfill('GSC backfill chunk');- await syncGscData(projectId, chunkStart, chunkEnd);+ await syncGscData(deps, projectId, chunkStart, chunkEnd);chunkEnd = new Date(chunkStart);chunkEnd.setDate(chunkEnd.getDate() - 1);@@ -1349,6 +1413,7 @@ export interface GscOAuthCallbackResult {}export async function completeGscOAuthCallback(+ deps: ServiceDeps,input: GscOAuthCallbackInput): Promise<GscOAuthCallbackResult> {if (input.state !== input.storedState) {@@ -1368,7 +1433,7 @@ export async function completeGscOAuthCallback(throw new Error('No refresh token returned from Google GSC OAuth');}- const db = await loadDb();+ const db = deps.db;const project = await db.project.findUnique({where: { id: input.projectId },select: { id: true, organizationId: true },@@ -1403,6 +1468,13 @@ export async function completeGscOAuthCallback(// `ctx.services.gsc` binding.// ---------------------------------------------------------------------------+/** A module function's signature with its leading `ServiceDeps` dropped —+ * what `createGscService` exposes once it has closed over its own. */+type WithoutDeps<T extends (deps: ServiceDeps, ...args: never[]) => unknown> =+ T extends (deps: ServiceDeps, ...args: infer A) => infer R+ ? (...args: A) => R+ : never;+export interface GscService {getConnection(projectId: string): Promise<GscConnectionSummary | null>;listSites(projectId: string): Promise<GscSite[]>;@@ -1413,14 +1485,14 @@ export interface GscService {projectId: string,input: GscDateRangeInput): ReturnType<typeof resolveGscDateRange>;- getOverview: typeof getGscOverview;- getPreviousOverview: typeof getGscPreviousOverview;- getPages: typeof getGscPages;- getPageDetails: typeof getGscPageDetails;- getQueryDetails: typeof getGscQueryDetails;- getQueries: typeof getGscQueries;- getSearchEngines: typeof getGscSearchEngines;- getAiEngines: typeof getGscAiEngines;+ getOverview: WithoutDeps<typeof getGscOverview>;+ getPreviousOverview: WithoutDeps<typeof getGscPreviousOverview>;+ getPages: WithoutDeps<typeof getGscPages>;+ getPageDetails: WithoutDeps<typeof getGscPageDetails>;+ getQueryDetails: WithoutDeps<typeof getGscQueryDetails>;+ getQueries: WithoutDeps<typeof getGscQueries>;+ getSearchEngines: WithoutDeps<typeof getGscSearchEngines>;+ getAiEngines: WithoutDeps<typeof getGscAiEngines>;getCannibalization: typeof getGscCannibalization;listConnectionsForSync(): Promise<{ projectId: string }[]>;runProjectSync(projectId: string): Promise<void>;@@ -1432,25 +1504,27 @@ export function createGscService(deps: ServiceDeps): GscService {const logger = deps.logger.child({ module: 'gsc' });return {- getConnection: getGscConnection,- listSites: listGscSites,+ getConnection: (projectId) => getGscConnection(deps, projectId),+ listSites: (projectId) => listGscSites(deps, projectId),selectSite: async (projectId, siteUrl) => {- await selectGscSite(projectId, siteUrl);+ await selectGscSite(deps, projectId, siteUrl);await deps.queues.gsc.gscProjectBackfill.add({ projectId });},- disconnect: disconnectGscConnection,- resolveDateRange: resolveGscDateRange,- getOverview: getGscOverview,- getPreviousOverview: getGscPreviousOverview,- getPages: getGscPages,- getPageDetails: getGscPageDetails,- getQueryDetails: getGscQueryDetails,- getQueries: getGscQueries,- getSearchEngines: getGscSearchEngines,- getAiEngines: getGscAiEngines,+ disconnect: (projectId) => disconnectGscConnection(deps, projectId),+ resolveDateRange: (projectId, input) =>+ resolveGscDateRange(deps, projectId, input),+ getOverview: (...args) => getGscOverview(deps, ...args),+ getPreviousOverview: (...args) => getGscPreviousOverview(deps, ...args),+ getPages: (...args) => getGscPages(deps, ...args),+ getPageDetails: (...args) => getGscPageDetails(deps, ...args),+ getQueryDetails: (...args) => getGscQueryDetails(deps, ...args),+ getQueries: (...args) => getGscQueries(deps, ...args),+ getSearchEngines: (...args) => getGscSearchEngines(deps, ...args),+ getAiEngines: (...args) => getGscAiEngines(deps, ...args),getCannibalization: getGscCannibalization,- listConnectionsForSync: listGscConnectionsForSync,- runProjectSync: (projectId) => runGscProjectSync(projectId, logger),- runProjectBackfill: (projectId) => runGscProjectBackfill(projectId, logger),+ listConnectionsForSync: () => listGscConnectionsForSync(deps),+ runProjectSync: (projectId) => runGscProjectSync(deps, projectId, logger),+ runProjectBackfill: (projectId) =>+ runGscProjectBackfill(deps, projectId, logger),};}diff --git a/packages/core/src/modules/health/health.routes.ts b/packages/core/src/modules/health/health.routes.tsindex c3d56c4b0..0866ee338 100644--- a/packages/core/src/modules/health/health.routes.ts+++ b/packages/core/src/modules/health/health.routes.ts@@ -15,6 +15,7 @@import { z } from 'zod';import { defineRoutes } from '../../http/define';+import { chQuery } from '../../shared/ch-query';import { tryCatch } from '../../shared/try-catch';import { currentReadiness } from './src/readiness';@@ -71,15 +72,13 @@ export const healthRoutes = defineRoutes((app) =>.get('/healthcheck',async ({ ctx, set }) => {- // `chQuery` (not `ctx.ch`) so the probe goes through the same- // round-robin + retry path every real read takes. Lazily, because a- // static import would construct a ClickHouse client in every test- // process that touches `rest.routes.ts`.- const { chQuery } = await import('@openpanel/db/src/clickhouse/client');+ // core's own `chQuery` over `ctx.ch` — the same round-robin + retry+ // proxy every real read takes (M10-009; `ctx.ch.query` IS+ // `withRetry(client => client.query(...))`, see shared/ch-query.ts).const [redisResult, dbResult, chResult] = await Promise.all([tryCatch(async () => (await ctx.redis.ping()) === 'PONG'),tryCatch(async () => Boolean(await ctx.db.$executeRaw`SELECT 1`)),- tryCatch(async () => (await chQuery('SELECT 1')).length > 0),+ tryCatch(async () => (await chQuery(ctx, 'SELECT 1')).length > 0),]);const dependencies = {diff --git a/packages/core/src/modules/import/import.jobs.test.ts b/packages/core/src/modules/import/import.jobs.test.tsindex 927a4cb40..52219c4d6 100644--- a/packages/core/src/modules/import/import.jobs.test.ts+++ b/packages/core/src/modules/import/import.jobs.test.ts@@ -29,6 +29,7 @@ function stubJobCtx(importService: Partial<ImportService>): JobCtx {auth: {} as Services['auth'],notification: {} as Services['notification'],insight: {} as Services['insight'],+ integration: {} as Services['integration'],cohort: {} as Services['cohort'],ingest: {} as Services['ingest'],gsc: {} as Services['gsc'],@@ -40,6 +41,10 @@ function stubJobCtx(importService: Partial<ImportService>): JobCtx {profile: {} as Services['profile'],group: {} as Services['group'],chart: {} as Services['chart'],+ funnel: {} as Services['funnel'],+ conversion: {} as Services['conversion'],+ sankey: {} as Services['sankey'],+ retention: {} as Services['retention'],overview: {} as Services['overview'],pages: {} as Services['pages'],realtime: {} as Services['realtime'],diff --git a/packages/core/src/modules/import/import.routes.ts b/packages/core/src/modules/import/import.routes.tsindex cc1e14a9e..760390f98 100644--- a/packages/core/src/modules/import/import.routes.ts+++ b/packages/core/src/modules/import/import.routes.ts@@ -20,9 +20,9 @@// guards on exactly this (apps/api/src/controllers/import.controller.ts:17-18),// ported verbatim.-import type { IClickhouseEvent } from '@openpanel/core';import { z } from 'zod';import { defineRoutes } from '../../http/define';+import type { IClickhouseEvent } from '../event/event.service';import { insertRawEventsBatch } from './import.service';export const importRoutes = defineRoutes((app) =>@@ -35,6 +35,7 @@ export const importRoutes = defineRoutes((app) =>try {const { writtenRows } = await insertRawEventsBatch(+ ctx,client.projectId,body as IClickhouseEvent[]);diff --git a/packages/core/src/modules/import/import.rpc.ts b/packages/core/src/modules/import/import.rpc.tsindex 5a7d58cc5..c498651bd 100644--- a/packages/core/src/modules/import/import.rpc.ts+++ b/packages/core/src/modules/import/import.rpc.ts@@ -24,10 +24,6 @@ import {} from '../../rpc/errors';import { zCreateImport } from './import.constants';-function loadDb() {- return import('@openpanel/db/src/prisma-client').then((m) => m.db);-}-function requireLogin(userId: string | null | undefined): string {if (!userId) {throw new TRPCAccessError('Not authenticated');@@ -46,7 +42,7 @@ export const importRouter = createTRPCRouter({level: 'read',});- const db = await loadDb();+ const db = ctx.db;return db.import.findMany({where: { projectId: input.projectId },orderBy: { createdAt: 'desc' },@@ -57,7 +53,7 @@ export const importRouter = createTRPCRouter({.input(z.object({ id: z.string() })).query(async ({ input, ctx }) => {const userId = requireLogin(ctx.session.userId);- const db = await loadDb();+ const db = ctx.db;const importRecord = await db.import.findUniqueOrThrow({where: { id: input.id },include: { project: true },@@ -80,7 +76,7 @@ export const importRouter = createTRPCRouter({level: 'write',});- const db = await loadDb();+ const db = ctx.db;const organization = await db.organization.findFirst({where: { projects: { some: { id: input.projectId } } },});@@ -119,7 +115,7 @@ export const importRouter = createTRPCRouter({.input(z.object({ id: z.string() })).mutation(async ({ input, ctx }) => {const userId = requireLogin(ctx.session.userId);- const db = await loadDb();+ const db = ctx.db;const importRecord = await db.import.findUniqueOrThrow({where: { id: input.id },});@@ -141,7 +137,7 @@ export const importRouter = createTRPCRouter({.input(z.object({ id: z.string() })).mutation(async ({ input, ctx }) => {const userId = requireLogin(ctx.session.userId);- const db = await loadDb();+ const db = ctx.db;const importRecord = await db.import.findUniqueOrThrow({where: { id: input.id },});diff --git a/packages/core/src/modules/import/import.service.test.ts b/packages/core/src/modules/import/import.service.test.tsindex 053e0e7af..327c6bae8 100644--- a/packages/core/src/modules/import/import.service.test.ts+++ b/packages/core/src/modules/import/import.service.test.ts@@ -1,8 +1,9 @@-// import.service.ts's db/ch access is lazy (`await import(...)` inside each-// function — see the file's header), which is exactly what makes-// `mock.module` work here with no import-time side effects to race: every-// mock below is registered before the subject's first call, not before its-// (side-effect-free) import.+// M10-009: import.service.ts takes its clients from `ServiceDeps`, so this+// file hands in fakes as `deps.db` / `deps.ch` and mocks NO module at all.+// That is the point of the conversion: a module mock of+// `@openpanel/db/src/clickhouse/client` replaced that specifier for every+// other FILE in the process (bare `bun test` shares one module registry), and+// the v1-compat seam memoizes whatever it resolved while one was installed.import { beforeAll, expect, mock, test } from 'bun:test';import type { AppDeps, Buffers } from '../../context';@@ -27,46 +28,27 @@ function stubLogger(): Logger {const chInsert = mock(async (_opts: { values: unknown[] }) => ({summary: { written_rows: '2' },}));-// Spread the real module rather than hand-listing every export: `mock.module`-// replaces this specifier process-wide (bun runs every test file in one-// shared module registry without `--isolate` — see AGENTS.md), so a partial-// factory here silently breaks unrelated consumers (gsc/cohort/insight tests-// and now the mcp module's) that import an export this file never overrides.-// `ch` itself is one such export: only `insert` is what import.service.ts-// exercises, so `command` is stubbed and every other method (`query`, ...) is-// spread from the real client — a bare `{ insert, command }` replacement-// previously stripped `query` from every *other* consumer of this same-// live-bound singleton (e.g. `@openpanel/db`'s `OverviewService`/-// `PagesService`, constructed once at that module's own load time) for the-// rest of the process.+// The real client, spread into the fake below so a method the subject does+// not exercise is still the real one rather than `undefined`. Imported, not+// mocked: nothing here replaces the specifier for the rest of the process.const actualClickhouseClient = await import('@openpanel/db/src/clickhouse/client');-mock.module('@openpanel/db/src/clickhouse/client', () => ({- ...actualClickhouseClient,- ch: {- ...actualClickhouseClient.ch,- insert: chInsert,- command: mock(async () => undefined),- },- getReplicatedTableName: mock((table: string) => table),- formatClickhouseDate: (date: Date | string) =>- new Date(date)- .toISOString()- .replace('T', ' ')- .replace(/(\.\d{3})?Z+$/, ''),- convertClickhouseDateToJs: (date: string) =>- new Date(`${date.replace(' ', 'T')}Z`),-}));const importUpdate = mock(async (_opts: { where: { id: string }; data: unknown }) => undefined);-const actualPrismaClient = await import('@openpanel/db/src/prisma-client');-mock.module('@openpanel/db/src/prisma-client', () => ({- ...actualPrismaClient,++// The subject's functions take `ServiceDeps`, so `deps.db` / `deps.ch` ARE+// the fakes below.+const deps = {db: { import: { update: importUpdate } },-}));+ ch: {+ ...actualClickhouseClient.ch,+ insert: chInsert,+ command: mock(async () => undefined),+ },+} as unknown as import('../../services').ServiceDeps;let insertRawEventsBatch: typeof import('./import.service').insertRawEventsBatch;let updateImportStatus: typeof import('./import.service').updateImportStatus;@@ -81,6 +63,7 @@ test('insertRawEventsBatch stamps project_id/imported_at and flattens propertieschInsert.mockClear();const result = await insertRawEventsBatch(+ deps,'proj_1',[{@@ -112,6 +95,7 @@ test('updateImportStatus writes the loading step through both the progress reporconst progressed: unknown[] = [];await updateImportStatus(+ deps,stubLogger(),{ updateProgress: (p) => progressed.push(p) },'imp_1',@@ -146,6 +130,7 @@ test('updateImportStatus marks a failed step with its error message', async () =const progressed: unknown[] = [];await updateImportStatus(+ deps,stubLogger(),{ updateProgress: (p) => progressed.push(p) },'imp_1',diff --git a/packages/core/src/modules/import/import.service.ts b/packages/core/src/modules/import/import.service.tsindex c05221e23..19d66afd8 100644--- a/packages/core/src/modules/import/import.service.ts+++ b/packages/core/src/modules/import/import.service.ts@@ -20,11 +20,17 @@import { createHash } from 'node:crypto';import { toDots } from '@openpanel/common';-import type { IClickhouseEvent, IClickhouseProfile } from '@openpanel/core';import type { Prisma } from '@openpanel/db/src/prisma-client';import { createLogger, type ILogger } from '../../clients/logger';import type { Logger } from '../../logger';import type { ServiceDeps } from '../../services';+import {+ convertClickhouseDateToJs,+ formatClickhouseDate,+} from '../../shared/ch-dates';+import { getReplicatedTableName, TABLE_NAMES } from '../../shared/ch-tables';+import type { IClickhouseEvent } from '../event/event.service';+import type { IClickhouseProfile } from '../profile/profile.service';import type { IImportConfig } from './import.constants';const BATCH_SIZE = Number.parseInt(process.env.IMPORT_BATCH_SIZE || '5000', 10);@@ -43,14 +49,6 @@ function getLogger(): ILogger {return _logger;}-function loadDb() {- return import('@openpanel/db/src/prisma-client').then((m) => m.db);-}--function loadChClient() {- return import('@openpanel/db/src/clickhouse/client');-}-function yieldToEventLoop(): Promise<void> {return new Promise((resolve) => {setTimeout(resolve, 100);@@ -72,6 +70,7 @@ export interface ImportStageResult {* Insert a batch of events into the imports staging table*/export async function insertImportBatch(+ deps: ServiceDeps,events: IClickhouseEvent[],importId: string): Promise<ImportStageResult> {@@ -79,7 +78,7 @@ export async function insertImportBatch(return { importId, totalEvents: 0, insertedEvents: 0 };}- const { ch, TABLE_NAMES, formatClickhouseDate } = await loadChClient();+ const ch = deps.ch;const now = formatClickhouseDate(new Date());const rows = events.map((event) => ({...event,@@ -107,6 +106,7 @@ export async function insertImportBatch(* Used by Mixpanel (and other providers) to import user profiles during an import job.*/export async function insertProfilesBatch(+ deps: ServiceDeps,profiles: IClickhouseProfile[],projectId: string): Promise<{ inserted: number }> {@@ -114,7 +114,7 @@ export async function insertProfilesBatch(return { inserted: 0 };}- const { ch, TABLE_NAMES } = await loadChClient();+ const ch = deps.ch;const normalized = profiles.map((p) => ({id: p.id,project_id: projectId,@@ -149,9 +149,10 @@ export async function insertProfilesBatch(const SESSION_GAP_MS = 30 * 60 * 1000; // 30 minutesexport async function generateGapBasedSessionIds(+ deps: ServiceDeps,importId: string): Promise<void> {- const { ch, TABLE_NAMES, getReplicatedTableName } = await loadChClient();+ const ch = deps.ch;let currentDeviceId = '';let currentSessionId = '';let currentLastTime = 0;@@ -200,14 +201,14 @@ export async function generateGapBasedSessionIds(batch.push(event);if (batch.length >= SESSION_BATCH_SIZE) {- await insertImportBatch(batch, importId);+ await insertImportBatch(deps, batch, importId);batch.length = 0;}}}if (batch.length > 0) {- await insertImportBatch(batch, importId);+ await insertImportBatch(deps, batch, importId);}const mutationTable = getReplicatedTableName(TABLE_NAMES.events_imports);@@ -230,8 +231,11 @@ export async function generateGapBasedSessionIds(* Delete all staging data for an import. Used to get a clean slate on retry* when the failure happened before moving data to production.*/-export async function cleanupStagingData(importId: string): Promise<void> {- const { ch, TABLE_NAMES, getReplicatedTableName } = await loadChClient();+export async function cleanupStagingData(+ deps: ServiceDeps,+ importId: string+): Promise<void> {+ const ch = deps.ch;const mutationTableName = getReplicatedTableName(TABLE_NAMES.events_imports);await ch.command({query: `ALTER TABLE ${mutationTableName} DELETE WHERE import_id = {importId:String}`,@@ -246,9 +250,10 @@ export async function cleanupStagingData(importId: string): Promise<void> {}export async function cleanupSessionStartEndEvents(+ deps: ServiceDeps,importId: string): Promise<void> {- const { ch, TABLE_NAMES, getReplicatedTableName } = await loadChClient();+ const ch = deps.ch;const mutationTableName = getReplicatedTableName(TABLE_NAMES.events_imports);await ch.command({query: `ALTER TABLE ${mutationTableName} DELETE WHERE import_id = {importId:String} AND name IN ('session_start', 'session_end')`,@@ -271,10 +276,10 @@ export async function cleanupSessionStartEndEvents(* heavy aggregation only for that batch of IDs.*/export async function createSessionsStartEndEvents(+ deps: ServiceDeps,importId: string): Promise<void> {- const { ch, TABLE_NAMES, convertClickhouseDateToJs, formatClickhouseDate } =- await loadChClient();+ const ch = deps.ch;let lastSessionId = '';const baseWhere = [@@ -479,7 +484,7 @@ export async function createSessionsStartEndEvents(}if (sessionEvents.length > 0) {- await insertImportBatch(sessionEvents, importId);+ await insertImportBatch(deps, sessionEvents, importId);}lastSessionId = maxSessionId;@@ -494,10 +499,11 @@ export async function createSessionsStartEndEvents(* Batched per-day using a simple date filter.*/export async function moveImportsToProduction(+ deps: ServiceDeps,importId: string,from: string): Promise<void> {- const { ch, TABLE_NAMES } = await loadChClient();+ const ch = deps.ch;let whereClause = 'import_id = {importId:String}';if (from) {@@ -540,9 +546,10 @@ export async function moveImportsToProduction(* Batches by session_ids to bound ClickHouse memory.*/export async function backfillSessionsToProduction(+ deps: ServiceDeps,importId: string): Promise<void> {- const { ch, TABLE_NAMES } = await loadChClient();+ const ch = deps.ch;let lastSessionId = '';while (true) {@@ -648,10 +655,11 @@ export async function backfillSessionsToProduction(* Get min/max created_at for an import's staging data.*/export async function getImportDateBounds(+ deps: ServiceDeps,importId: string,fromCreatedAt?: string): Promise<{ min: string | null; max: string | null }> {- const { ch, TABLE_NAMES } = await loadChClient();+ const ch = deps.ch;const res = await ch.query({query: `SELECT min(created_at) AS min, max(created_at) AS max@@ -727,12 +735,13 @@ export type UpdateImportStatusOptions =export type ImportSteps = UpdateImportStatusOptions['step'];export async function updateImportStatus(+ deps: ServiceDeps,jobLogger: Logger,progress: ImportJobProgress,importId: string,options: UpdateImportStatusOptions): Promise<void> {- const db = await loadDb();+ const db = deps.db;const data: Prisma.ImportUpdateInput = {};switch (options.step) {case 'loading':@@ -883,11 +892,12 @@ async function createImportProvider(* worker delegate and core's own (not yet live) `import` queue worker.*/export async function runImportJob(+ deps: ServiceDeps,importId: string,progress: ImportJobProgress = NOOP_PROGRESS,logger: Logger = getLogger()): Promise<{ success: true }> {- const db = await loadDb();+ const db = deps.db;const record = await db.import.findUniqueOrThrow({where: { id: importId },include: { project: true },@@ -916,11 +926,11 @@ export async function runImportJob(jobLogger.info('Retry detected before resumable phase — cleaning staging data');- await cleanupStagingData(importId);+ await cleanupStagingData(deps, importId);}// Phase 1: Load events into staging- await updateImportStatus(jobLogger, progress, importId, {+ await updateImportStatus(deps, jobLogger, progress, importId, {step: 'loading',});@@ -940,10 +950,10 @@ export async function runImportJob(return;}const values = Array.from(profileMap.values());- await insertProfilesBatch(values, record.projectId);+ await insertProfilesBatch(deps, values, record.projectId);processedProfiles += values.length;profileMap.clear();- await updateImportStatus(jobLogger, progress, importId, {+ await updateImportStatus(deps, jobLogger, progress, importId, {step: 'loading_profiles',processedProfiles,});@@ -973,14 +983,14 @@ export async function runImportJob(}if (eventBatch.length >= BATCH_SIZE) {- await insertImportBatch(eventBatch, importId);+ await insertImportBatch(deps, eventBatch, importId);processedEvents += eventBatch.length;const batchDate = new Date(eventBatch[0]?.created_at || '').toISOString().split('T')[0];- await updateImportStatus(jobLogger, progress, importId, {+ await updateImportStatus(deps, jobLogger, progress, importId, {step: 'loading',batch: batchDate,totalEvents,@@ -993,14 +1003,14 @@ export async function runImportJob(}if (eventBatch.length > 0) {- await insertImportBatch(eventBatch, importId);+ await insertImportBatch(deps, eventBatch, importId);processedEvents += eventBatch.length;const batchDate = new Date(eventBatch[0]?.created_at || '').toISOString().split('T')[0];- await updateImportStatus(jobLogger, progress, importId, {+ await updateImportStatus(deps, jobLogger, progress, importId, {step: 'loading',batch: batchDate,totalEvents,@@ -1022,7 +1032,7 @@ export async function runImportJob(// Phase 1b: Load user profiles (Mixpanel only)if (typeof providerInstance.streamProfiles === 'function') {- await updateImportStatus(jobLogger, progress, importId, {+ await updateImportStatus(deps, jobLogger, progress, importId, {step: 'loading_profiles',});@@ -1037,9 +1047,9 @@ export async function runImportJob(profileBatch.push(profile);if (profileBatch.length >= PROFILE_BATCH_SIZE) {- await insertProfilesBatch(profileBatch, record.projectId);+ await insertProfilesBatch(deps, profileBatch, record.projectId);profilesLoaded += profileBatch.length;- await updateImportStatus(jobLogger, progress, importId, {+ await updateImportStatus(deps, jobLogger, progress, importId, {step: 'loading_profiles',processedProfiles: profilesLoaded,});@@ -1049,9 +1059,9 @@ export async function runImportJob(}if (profileBatch.length > 0) {- await insertProfilesBatch(profileBatch, record.projectId);+ await insertProfilesBatch(deps, profileBatch, record.projectId);profilesLoaded += profileBatch.length;- await updateImportStatus(jobLogger, progress, importId, {+ await updateImportStatus(deps, jobLogger, progress, importId, {step: 'loading_profiles',processedProfiles: profilesLoaded,totalProfiles: profilesLoaded,@@ -1066,10 +1076,10 @@ export async function runImportJob(// Phase 2: Generate gap-based session IDs (Mixpanel etc.)if (shouldGenerateSessionIds) {- await updateImportStatus(jobLogger, progress, importId, {+ await updateImportStatus(deps, jobLogger, progress, importId, {step: 'generating_sessions',});- await generateGapBasedSessionIds(importId);+ await generateGapBasedSessionIds(deps, importId);await yieldToEventLoop();jobLogger.info('Session ID generation complete');}@@ -1086,14 +1096,14 @@ export async function runImportJob(jobLogger.info('Retry at creating_sessions — cleaning existing session_start/end events');- await cleanupSessionStartEndEvents(importId);+ await cleanupSessionStartEndEvents(deps, importId);}- await updateImportStatus(jobLogger, progress, importId, {+ await updateImportStatus(deps, jobLogger, progress, importId, {step: 'creating_sessions',batch: 'all sessions',});- await createSessionsStartEndEvents(importId);+ await createSessionsStartEndEvents(deps, importId);await yieldToEventLoop();jobLogger.info('Session event creation complete');@@ -1119,7 +1129,7 @@ export async function runImportJob(return next.toISOString().split('T')[0]!;})();- const bounds = await getImportDateBounds(importId, moveFromDate);+ const bounds = await getImportDateBounds(deps, importId, moveFromDate);if (bounds.min && bounds.max) {const startDate = bounds.min.split(' ')[0]!;const endDate = bounds.max.split(' ')[0]!;@@ -1129,8 +1139,8 @@ export async function runImportJob(while (cursor <= end) {const dateStr = cursor.toISOString().split('T')[0]!;- await moveImportsToProduction(importId, dateStr);- await updateImportStatus(jobLogger, progress, importId, {+ await moveImportsToProduction(deps, importId, dateStr);+ await updateImportStatus(deps, jobLogger, progress, importId, {step: 'moving',batch: dateStr,});@@ -1143,17 +1153,17 @@ export async function runImportJob(jobLogger.info('Move to production complete');// Phase 4: Backfill sessions table- await updateImportStatus(jobLogger, progress, importId, {+ await updateImportStatus(deps, jobLogger, progress, importId, {step: 'backfilling_sessions',batch: 'all sessions',});- await backfillSessionsToProduction(importId);+ await backfillSessionsToProduction(deps, importId);await yieldToEventLoop();jobLogger.info('Session backfill complete');// Done- await updateImportStatus(jobLogger, progress, importId, {+ await updateImportStatus(deps, jobLogger, progress, importId, {step: 'completed',});jobLogger.info('Import completed');@@ -1164,7 +1174,7 @@ export async function runImportJob(try {const errorMsg = error instanceof Error ? error.message : 'Unknown error';- await updateImportStatus(jobLogger, progress, importId, {+ await updateImportStatus(deps, jobLogger, progress, importId, {step: 'failed',errorMessage: errorMsg,});@@ -1191,11 +1201,12 @@ export interface InsertRawEventsResult {}export async function insertRawEventsBatch(+ deps: ServiceDeps,projectId: string,events: IClickhouseEvent[],logger: Logger = getLogger()): Promise<InsertRawEventsResult> {- const { ch, TABLE_NAMES, formatClickhouseDate } = await loadChClient();+ const ch = deps.ch;const importedAt = formatClickhouseDate(new Date());const values: IClickhouseEvent[] = events.map((event) => ({...event,@@ -1235,7 +1246,7 @@ export function createImportService(deps: ServiceDeps): ImportService {const logger = deps.logger.child({ module: 'import' });return {- run: (importId, progress) => runImportJob(importId, progress, logger),+ run: (importId, progress) => runImportJob(deps, importId, progress, logger),enqueue: (importId) => deps.queues.import.import.add({ importId }),};}diff --git a/packages/core/src/modules/import/src/base-provider.ts b/packages/core/src/modules/import/src/base-provider.tsindex 66e1e695e..9aa7f6b7d 100644--- a/packages/core/src/modules/import/src/base-provider.ts+++ b/packages/core/src/modules/import/src/base-provider.ts@@ -1,4 +1,4 @@-import type { IClickhouseEvent } from '@openpanel/core';+import type { IClickhouseEvent } from '../../event/event.service';import type { BaseRawEvent, ErrorContext, ImportJobMetadata } from './types';export abstract class BaseImportProvider<diff --git a/packages/core/src/modules/import/src/providers/amplitude.ts b/packages/core/src/modules/import/src/providers/amplitude.tsindex f27a9ac7f..f2f8ea060 100644--- a/packages/core/src/modules/import/src/providers/amplitude.ts+++ b/packages/core/src/modules/import/src/providers/amplitude.ts@@ -3,17 +3,17 @@ import { createInterface } from 'node:readline';import { Readable } from 'node:stream';import { createBrotliDecompress, createGunzip } from 'node:zlib';import { isSameDomain, parsePath, toDots } from '@openpanel/common';-// Deep imports, not @openpanel/db's full barrel — see umami.ts's header.-import { formatClickhouseDate } from '@openpanel/db/src/clickhouse/client';-import type { IClickhouseEvent } from '@openpanel/core';-import type { IClickhouseProfile } from '@openpanel/core';import unzipper from 'unzipper';import { z } from 'zod';import type { Logger } from '../../../../logger';+// core's own date helper, not @openpanel/db's — see umami.ts's header.+import { formatClickhouseDate } from '../../../../shared/ch-dates';import {getReferrerWithQuery,parseReferrer,} from '../../../../shared/parse-referrer';+import type { IClickhouseEvent } from '../../../event/event.service';+import type { IClickhouseProfile } from '../../../profile/profile.service';import type { IAmplitudeImportConfig } from '../../import.constants';import { BaseImportProvider } from '../base-provider';import { toCountryCode } from './country-codes';diff --git a/packages/core/src/modules/import/src/providers/mixpanel.ts b/packages/core/src/modules/import/src/providers/mixpanel.tsindex 204594538..f7670e0ae 100644--- a/packages/core/src/modules/import/src/providers/mixpanel.ts+++ b/packages/core/src/modules/import/src/providers/mixpanel.ts@@ -1,11 +1,9 @@import { randomUUID } from 'node:crypto';import { isSameDomain, parsePath, toDots } from '@openpanel/common';-// Deep imports, not @openpanel/db's full barrel — see umami.ts's header.-import { formatClickhouseDate } from '@openpanel/db/src/clickhouse/client';-import type { IClickhouseEvent } from '@openpanel/core';-import type { IClickhouseProfile } from '@openpanel/core';import { z } from 'zod';import type { Logger } from '../../../../logger';+// core's own date helper, not @openpanel/db's — see umami.ts's header.+import { formatClickhouseDate } from '../../../../shared/ch-dates';import {getReferrerWithQuery,parseReferrer,@@ -14,6 +12,8 @@ import {parseUserAgent,type UserAgentInfo,} from '../../../../shared/parser-user-agent';+import type { IClickhouseEvent } from '../../../event/event.service';+import type { IClickhouseProfile } from '../../../profile/profile.service';import type { IMixpanelImportConfig } from '../../import.constants';import { BaseImportProvider } from '../base-provider';diff --git a/packages/core/src/modules/import/src/providers/umami.ts b/packages/core/src/modules/import/src/providers/umami.tsindex c660334bd..f6237dd6a 100644--- a/packages/core/src/modules/import/src/providers/umami.ts+++ b/packages/core/src/modules/import/src/providers/umami.ts@@ -3,23 +3,23 @@ import { Readable } from 'node:stream';import { pipeline } from 'node:stream/promises';import { createBrotliDecompress, createGunzip } from 'node:zlib';import { isSameDomain, parsePath, toDots } from '@openpanel/common';-// Deep imports, not @openpanel/db's full barrel: the barrel's-// `export * from './src/services/notification.service'` /-// `'./src/buffers'` eagerly reach @openpanel/queue, which constructs a-// pino-pretty transport at import time — fatal under bun:test's-// `--isolate` worker threads (AGENTS.md's eager-barrel-chain hazard).-import { formatClickhouseDate } from '@openpanel/db/src/clickhouse/client';-import type { IClickhouseEvent } from '@openpanel/core';import { parse } from 'csv-parse';import { assocPath } from 'ramda';import { z } from 'zod';import type { Logger } from '../../../../logger';+// M10-009: core's own date helper, not @openpanel/db's — importing+// `@openpanel/db/src/clickhouse/client` constructs a ClickHouse client and a+// pino-pretty transport at import time (fatal under bun:test's `--isolate`+// worker threads, AGENTS.md's eager-barrel-chain hazard) and is a value+// import of `@openpanel/db` from core. See shared/ch-dates.ts's header.+import { formatClickhouseDate } from '../../../../shared/ch-dates';import {getReferrerWithQuery,parseReferrer,} from '../../../../shared/parse-referrer';import { generateDeviceId } from '../../../../shared/profileId';import { safeFetchStream } from '../../../../shared/safe-fetch';+import type { IClickhouseEvent } from '../../../event/event.service';import type { IUmamiImportConfig } from '../../import.constants';import { BaseImportProvider } from '../base-provider';diff --git a/packages/core/src/modules/import/src/types.ts b/packages/core/src/modules/import/src/types.tsindex 89221b090..f800938a8 100644--- a/packages/core/src/modules/import/src/types.ts+++ b/packages/core/src/modules/import/src/types.ts@@ -1,4 +1,4 @@-import type { IServiceImportedEventPayload } from '@openpanel/core';+import type { IServiceImportedEventPayload } from '../../event/event.service';export interface ImportConfig {projectId: string;diff --git a/packages/core/src/modules/ingest/wind-down.test.ts b/packages/core/src/modules/ingest/wind-down.test.tsindex cad18ba4f..2a4fb838d 100644--- a/packages/core/src/modules/ingest/wind-down.test.ts+++ b/packages/core/src/modules/ingest/wind-down.test.ts@@ -11,23 +11,41 @@* core reads no environment — so V1's `vi.stubEnv` becomes a parameter.*/-import { beforeAll, beforeEach, describe, expect, it, mock } from 'bun:test';+import {+ afterAll,+ beforeAll,+ beforeEach,+ describe,+ expect,+ it,+ mock,+} from 'bun:test';import type { Logger } from '../../logger';const getOrganizationByProjectIdCached = mock(async (_projectId: string): Promise<unknown> => null);-// Spread the real module: a partial factory replaces the whole barrel, and-// every other importer of it then fails on a missing export.-const organizationService = await import(- '../organization/organization.service'-);+// Spread a plain-object SNAPSHOT of the real module: a partial factory+// replaces the whole module process-wide, and every other importer of it then+// fails on a missing export. The snapshot (not the live import binding) is+// what makes the afterAll restore real rather than a re-application of this+// mock — see AGENTS.md.+const realOrganizationService = {+ ...(await import('../organization/organization.service')),+};mock.module('../organization/organization.service', () => ({- ...organizationService,+ ...realOrganizationService,getOrganizationByProjectIdCached,}));+afterAll(() => {+ mock.module(+ '../organization/organization.service',+ () => realOrganizationService+ );+});+let isIngestionWoundDown: typeof import('./ingest.service').isIngestionWoundDown;beforeAll(async () => {diff --git a/packages/core/src/modules/insight/insight.jobs.test.ts b/packages/core/src/modules/insight/insight.jobs.test.tsindex 13abcefd4..5427be2e0 100644--- a/packages/core/src/modules/insight/insight.jobs.test.ts+++ b/packages/core/src/modules/insight/insight.jobs.test.ts@@ -37,6 +37,7 @@ function stubJobCtx(notification: {} as Services['notification'],insight: insight as InsightService,gsc: {} as Services['gsc'],+ integration: {} as Services['integration'],cohort: {} as Services['cohort'],import: {} as Services['import'],ingest: {} as Services['ingest'],@@ -47,6 +48,10 @@ function stubJobCtx(profile: {} as Services['profile'],group: {} as Services['group'],chart: {} as Services['chart'],+ funnel: {} as Services['funnel'],+ conversion: {} as Services['conversion'],+ sankey: {} as Services['sankey'],+ retention: {} as Services['retention'],overview: {} as Services['overview'],pages: {} as Services['pages'],realtime: {} as Services['realtime'],@@ -143,6 +148,7 @@ test('insightsDaily fans out one insightsProject enqueue per candidate, jobId de],} as InsightService,gsc: {} as Services['gsc'],+ integration: {} as Services['integration'],cohort: {} as Services['cohort'],import: {} as Services['import'],ingest: {} as Services['ingest'],@@ -153,6 +159,10 @@ test('insightsDaily fans out one insightsProject enqueue per candidate, jobId deprofile: {} as Services['profile'],group: {} as Services['group'],chart: {} as Services['chart'],+ funnel: {} as Services['funnel'],+ conversion: {} as Services['conversion'],+ sankey: {} as Services['sankey'],+ retention: {} as Services['retention'],overview: {} as Services['overview'],pages: {} as Services['pages'],realtime: {} as Services['realtime'],diff --git a/packages/core/src/modules/insight/insight.rpc.ts b/packages/core/src/modules/insight/insight.rpc.tsindex 26dd93025..d28d8e3cc 100644--- a/packages/core/src/modules/insight/insight.rpc.ts+++ b/packages/core/src/modules/insight/insight.rpc.ts@@ -116,11 +116,9 @@ export const insightRouter = createTRPCRouter({.mutation(async ({ input: { insightId }, ctx }) => {const userId = requireLogin(ctx.session.userId);- // `overview` hasn't moved to core yet — deep-imported like the rest of- // @openpanel/db's internals until it does.- const { db } = await import('@openpanel/db/src/prisma-client');+ const db = ctx.db;const { getSegmentDailySeriesCore, getTrafficBreakdownCore } =- await import('@openpanel/core');+ await import('../../v1-compat');const insight = await db.projectInsight.findUniqueOrThrow({where: { id: insightId },diff --git a/packages/core/src/modules/insight/insight.service.test.ts b/packages/core/src/modules/insight/insight.service.test.tsindex 94bba6cf1..a5ee7fca0 100644--- a/packages/core/src/modules/insight/insight.service.test.ts+++ b/packages/core/src/modules/insight/insight.service.test.ts@@ -1,8 +1,8 @@-// insight.service.ts's db/ch access is lazy (`await import(...)` inside each-// function — see the file's header), which is exactly what makes-// `mock.module` work here with no import-time side effects to race: every-// mock below is registered before the subject's first call, not before its-// (side-effect-free) import.+// M10-009: the subject's functions take `ServiceDeps`, so `deps.db` IS the+// fake below and no Prisma module mock is needed. The intra-package modules+// (store, referrer-spikes, legacy-scan) are still `mock.module`'d — that works+// here with no import-time side effects to race because every mock is+// registered before the subject's first call, not before its import.import { beforeAll, expect, mock, test } from 'bun:test';import type { AppDeps, Buffers } from '../../context';@@ -10,7 +10,10 @@ import type { AppDeps, Buffers } from '../../context';const listProjectIdsForCadence = mock(async () => ['p1', 'p2']);const getProjectCreatedAt = mock(async () => null);mock.module('./src/store', () => ({- insightStore: { listProjectIdsForCadence, getProjectCreatedAt },+ createInsightStore: () => ({+ listProjectIdsForCadence,+ getProjectCreatedAt,+ }),}));const executeRawCalls: unknown[] = [];@@ -19,18 +22,11 @@ const $executeRaw = mock((..._args: unknown[]) => {executeRawCalls.push(_args);return Promise.resolve(executeRawReturns.shift() ?? 0);});-const actualPrismaClient = await import('@openpanel/db/src/prisma-client');-mock.module('@openpanel/db/src/prisma-client', () => ({- ...actualPrismaClient,- db: { $executeRaw },-}));-// insight.service.ts never touches ClickHouse directly (it's PG-only, via-// $executeRaw above), so unlike gsc/cohort/import there is nothing here to-// mock — and no reason to register a factory for this specifier at all: a-// hand-rolled one, even a well-intentioned superset, is a standing risk of-// becoming the partial factory those files warn about the moment someone-// edits it without re-checking every other consumer.+// $executeRaw above), so `deps.ch` stays unbuilt.+const deps = {+ db: { $executeRaw },+} as unknown as import('../../services').ServiceDeps;const spikesQuery = mock(async () => [{ anchorDate: '2026-09-01', spikes: [] },@@ -43,9 +39,9 @@ const legacyGenerateInsights = mock(async () => [{ type: 'traffic_spike', message: 'test', data: {} },]);mock.module('./src/legacy-scan', () => ({- InsightsService: class {- generateInsights = legacyGenerateInsights;- },+ createLegacyInsightsScanner: () => ({+ generateInsights: legacyGenerateInsights,+ }),}));const generateInsightExplanation = mock(async () => ({@@ -84,7 +80,10 @@ beforeAll(async () => {});test('listDailyInsightCandidates pairs every eligible project with the given date', async () => {- const candidates = await subject.listDailyInsightCandidates('2026-09-03');+ const candidates = await subject.listDailyInsightCandidates(+ deps,+ '2026-09-03'+ );expect(listProjectIdsForCadence).toHaveBeenCalledWith('daily');expect(candidates).toEqual([@@ -105,7 +104,7 @@ test('cleanupStaleInsights stops each batch loop once a batch returns fewer thantrace: noop,child: () => logger,};- const result = await subject.cleanupStaleInsights(logger);+ const result = await subject.cleanupStaleInsights(deps, logger);// suppressed: 5000+5000+1200 (stops <5000); closed: 5000+0 (stops <5000)expect(result.insights).toBe(5000 + 5000 + 1200 + 5000 + 0);@@ -122,14 +121,14 @@ test('getReferrerSpikes delegates to the ClickHouse query', async () => {interval: 'day' as const,timezone: 'UTC',};- const result = await subject.getReferrerSpikes(input);+ const result = await subject.getReferrerSpikes(deps, input);- expect(spikesQuery).toHaveBeenCalledWith(input);+ expect(spikesQuery).toHaveBeenCalledWith(deps, input);expect(result).toEqual([{ anchorDate: '2026-09-01', spikes: [] }]);});test('scanLegacyInsights delegates to the pre-engine detector, kept for parity', async () => {- const result = await subject.scanLegacyInsights('p1');+ const result = await subject.scanLegacyInsights(deps, 'p1');expect(legacyGenerateInsights).toHaveBeenCalledWith('p1');expect(result).toEqual([diff --git a/packages/core/src/modules/insight/insight.service.ts b/packages/core/src/modules/insight/insight.service.tsindex 3344e6117..2fb077a2e 100644--- a/packages/core/src/modules/insight/insight.service.ts+++ b/packages/core/src/modules/insight/insight.service.ts@@ -8,16 +8,12 @@// direct call, not one that requires building a Ctx first. `createInsightService`// is the `ctx.services.insight` binding for code that already has a Ctx.//-// db/ch access is LAZY (`load*` below), not a static top-level import: both-// @openpanel/db/src/prisma-client and .../clickhouse/client construct a real-// pino logger — WITH a pino-pretty transport worker thread — the moment they-// are imported (M4-008's self-contained db logger). A static import here-// would spawn one of those per core test FILE, since jobs.registry.ts and-// services.ts pull this module into the eager barrel chain nearly every test-// reaches. Same shape as @openpanel/core/clients/email.ts's lazy import, one-// more reason for it. None of these deep paths import @openpanel/core, so-// there is no db <-> core runtime *cycle* — this is a side-effect problem,-// not a cycle.+// M10-009: every exported function takes `ServiceDeps` and reaches Postgres+// as `deps.db` and ClickHouse as `deps.ch`; the `loadDb()`/`loadCh()` lazy+// loaders are gone, so the requestId minted at the edge reaches the query+// (ADR-018, docs/TECH_DEBT.md §4). The remaining `load*` functions are+// intra-package lazy imports (engine, store, detection modules) kept lazy for+// their own import cost, not for a client's.//// ClickHouse queries here still go through clix, not the `sql` tag: ADR-013// converts the analytics read path one query per P7 task, and this module's@@ -73,20 +69,12 @@ const DEFAULT_ENGINE_CONFIG: EngineConfig = {},};-function loadDb() {- return import('@openpanel/db/src/prisma-client');-}--function loadCh() {- return import('@openpanel/db/src/clickhouse/client').then((m) => m.ch);-}-function loadEngine() {return import('./src/engine');}-function loadStore() {- return import('./src/store').then((m) => m.insightStore);+function loadStore(deps: ServiceDeps) {+ return import('./src/store').then((m) => m.createInsightStore(deps));}function loadDetectionModules() {@@ -98,7 +86,7 @@ function loadReferrerSpikesQuery() {}function loadLegacyDetector() {- return import('./src/legacy-scan').then((m) => m.InsightsService);+ return import('./src/legacy-scan').then((m) => m.createLegacyInsightsScanner);}export interface DailyInsightCandidate {@@ -127,9 +115,10 @@ function clamp01(n: number): number {/** The `insightsDaily` cron fan-out: eligible project ids, paired with `date`. */export async function listDailyInsightCandidates(+ deps: ServiceDeps,date: string): Promise<DailyInsightCandidate[]> {- const insightStore = await loadStore();+ const insightStore = await loadStore(deps);const projectIds = await insightStore.listProjectIdsForCadence('daily');return projectIds.map((projectId) => ({ projectId, date }));}@@ -141,10 +130,11 @@ export async function listDailyInsightCandidates(* job itself.*/async function enrichProjectInsights(+ deps: ServiceDeps,projectId: string,logger: Logger): Promise<void> {- const { db } = await loadDb();+ const db = deps.db;const stale = await db.projectInsight.findMany({where: {@@ -232,19 +222,21 @@ async function enrichProjectInsights(}/** The `insightsProject` job body: engine run + tier-1 AI enrichment. */-export async function runProjectInsights(args: {- projectId: string;- date: string;- logger: Logger;-}): Promise<void> {+export async function runProjectInsights(+ deps: ServiceDeps,+ args: {+ projectId: string;+ date: string;+ logger: Logger;+ }+): Promise<void> {const { projectId, date, logger } = args;- const [{ createEngine }, insightStore, detectionModules, ch] =- await Promise.all([- loadEngine(),- loadStore(),- loadDetectionModules(),- loadCh(),- ]);+ const ch = deps.ch;+ const [{ createEngine }, insightStore, detectionModules] = await Promise.all([+ loadEngine(),+ loadStore(deps),+ loadDetectionModules(),+ ]);const engine = createEngine({store: insightStore,@@ -271,7 +263,7 @@ export async function runProjectInsights(args: {// Isolated so a provider outage or rate-limit never fails the insights// computation itself.try {- await enrichProjectInsights(projectId, logger);+ await enrichProjectInsights(deps, projectId, logger);} catch (err) {logger.error({ err, projectId }, 'insight enrichment failed');}@@ -297,9 +289,10 @@ async function deleteInBatches(* retention rules). The `insightCleanup` cron job body.*/export async function cleanupStaleInsights(+ deps: ServiceDeps,logger: Logger): Promise<{ insights: number; events: number }> {- const { db } = await loadDb();+ const db = deps.db;const retentionDays = Number.parseInt(process.env.INSIGHTS_RETENTION_DAYS ||String(DEFAULT_INSIGHTS_RETENTION_DAYS),@@ -388,6 +381,7 @@ interface DigestProjectRow {/** Assembles one project's digest payload (no sending). */async function buildDigestData(+ deps: ServiceDeps,project: DigestProjectRow,opts: { force?: boolean } = {}): Promise<{ skipped?: string; data?: Record<string, unknown> }> {@@ -396,12 +390,8 @@ async function buildDigestData(const prevStart = now - 14 * DAY_MS;const iso = (ms: number) => new Date(ms).toISOString();- const { db } = await loadDb();- // `overview` hasn't moved to core yet — deep-imported like the rest of- // @openpanel/db's internals until it does.- const { getAnalyticsOverviewCore } = await import(- '@openpanel/core'- );+ const db = deps.db;+ const { getAnalyticsOverviewCore } = await import('../../v1-compat');const [cur, prev] = await Promise.all([getAnalyticsOverviewCore({@@ -518,8 +508,11 @@ async function buildDigestData(};}-async function recipientsForOrg(organizationId: string): Promise<string[]> {- const { db } = await loadDb();+async function recipientsForOrg(+ deps: ServiceDeps,+ organizationId: string+): Promise<string[]> {+ const db = deps.db;const members = await db.member.findMany({where: { organizationId },select: { email: true },@@ -533,9 +526,10 @@ async function recipientsForOrg(organizationId: string): Promise<string[]> {* body.*/export async function sendWeeklyDigests(+ deps: ServiceDeps,logger: Logger): Promise<WeeklyDigestResult> {- const { db } = await loadDb();+ const db = deps.db;// Prefilter on the raw status column (computed fields can't be used in// `where`), then refine with the canonical subscription state below.@@ -561,11 +555,11 @@ export async function sendWeeklyDigests(continue;}- const { skipped, data } = await buildDigestData(project);+ const { skipped, data } = await buildDigestData(deps, project);if (skipped || !data) {continue;}- const emails = await recipientsForOrg(project.organizationId);+ const emails = await recipientsForOrg(deps, project.organizationId);if (emails.length === 0) {continue;}@@ -589,10 +583,11 @@ export async function sendWeeklyDigests(* - opts.force → build even if the project had 0 visitors this week*/export async function previewWeeklyDigest(+ deps: ServiceDeps,projectId: string,opts: { to?: string; force?: boolean } = {}): Promise<WeeklyDigestPreview> {- const { db } = await loadDb();+ const db = deps.db;const project = await db.project.findUnique({where: { id: projectId },select: { id: true, name: true, organizationId: true },@@ -601,7 +596,7 @@ export async function previewWeeklyDigest(return { sent: false, skipped: 'project not found' };}- const { skipped, data } = await buildDigestData(project, {+ const { skipped, data } = await buildDigestData(deps, project, {force: opts.force,});if (skipped || !data) {@@ -616,8 +611,11 @@ export async function previewWeeklyDigest(return { sent: false, data };}-export async function listInsights(args: { projectId: string; limit: number }) {- const { db } = await loadDb();+export async function listInsights(+ deps: ServiceDeps,+ args: { projectId: string; limit: number }+) {+ const db = deps.db;return db.projectInsight.findMany({where: { projectId: args.projectId, state: 'active' },orderBy: [@@ -628,11 +626,14 @@ export async function listInsights(args: { projectId: string; limit: number }) {});}-export async function listAllInsights(args: {- projectId: string;- limit: number;-}) {- const { db } = await loadDb();+export async function listAllInsights(+ deps: ServiceDeps,+ args: {+ projectId: string;+ limit: number;+ }+) {+ const db = deps.db;return db.projectInsight.findMany({where: { projectId: args.projectId, state: 'active' },orderBy: [@@ -673,21 +674,20 @@ export async function explainInsight(}export async function getReferrerSpikes(+ deps: ServiceDeps,input: GetReferrerSpikesInput): Promise<ReferrerSpikeCluster[]> {const query = await loadReferrerSpikesQuery();- return query(input);+ return query(deps, input);}/** Pre-engine detector, kept for parity with V1 — no live callers today. */export async function scanLegacyInsights(+ deps: ServiceDeps,projectId: string): Promise<LegacyInsight[]> {- const [LegacyInsightsDetector, ch] = await Promise.all([- loadLegacyDetector(),- loadCh(),- ]);- return new LegacyInsightsDetector(ch).generateInsights(projectId);+ const createLegacyInsightsScanner = await loadLegacyDetector();+ return createLegacyInsightsScanner(deps).generateInsights(projectId);}// Re-exported at each type's own import site (noExportedImports): a type@@ -734,15 +734,17 @@ export function createInsightService(deps: ServiceDeps): InsightService {const logger = deps.logger.child({ module: 'insight' });return {- listDailyInsightCandidates,- runProjectInsights: (args) => runProjectInsights({ ...args, logger }),- cleanupStaleInsights: () => cleanupStaleInsights(logger),- sendWeeklyDigests: () => sendWeeklyDigests(logger),- previewWeeklyDigest,- listInsights,- listAllInsights,+ listDailyInsightCandidates: (date) =>+ listDailyInsightCandidates(deps, date),+ runProjectInsights: (args) => runProjectInsights(deps, { ...args, logger }),+ cleanupStaleInsights: () => cleanupStaleInsights(deps, logger),+ sendWeeklyDigests: () => sendWeeklyDigests(deps, logger),+ previewWeeklyDigest: (projectId, opts) =>+ previewWeeklyDigest(deps, projectId, opts),+ listInsights: (args) => listInsights(deps, args),+ listAllInsights: (args) => listAllInsights(deps, args),explainInsight,- getReferrerSpikes,- scanLegacyInsights,+ getReferrerSpikes: (input) => getReferrerSpikes(deps, input),+ scanLegacyInsights: (projectId) => scanLegacyInsights(deps, projectId),};}diff --git a/packages/core/src/modules/insight/src/legacy-scan.ts b/packages/core/src/modules/insight/src/legacy-scan.tsindex efa806f0d..00b926347 100644--- a/packages/core/src/modules/insight/src/legacy-scan.ts+++ b/packages/core/src/modules/insight/src/legacy-scan.ts@@ -1,5 +1,6 @@import type { ClickHouseClient } from '@clickhouse/client';import { clix } from '@openpanel/db/src/clickhouse/query-builder';+import type { ServiceDeps } from '../../../services';export interface Insight {type: string;@@ -69,7 +70,14 @@ interface EventCompletionResult {prev_month_count: number;}-export class InsightsService {+/**+ * The pre-engine detector, kept for V1 parity — no live callers today.+ *+ * M10-009: the class is no longer exported; `createLegacyInsightsScanner(deps)`+ * below is the module's factory, so every service module in core is reached the+ * same way (ADR-007) and nothing in the tree still exports a `*Service` class.+ */+class LegacyInsightsScanner {constructor(private readonly client: ClickHouseClient) {}private async getTrafficSpikes(projectId: string): Promise<Insight[]> {@@ -393,3 +401,16 @@ export class InsightsService {});}}++export interface LegacyInsightsScannerService {+ generateInsights(projectId: string): Promise<Insight[]>;+}++export function createLegacyInsightsScanner(+ deps: Pick<ServiceDeps, 'ch'>+): LegacyInsightsScannerService {+ const scanner = new LegacyInsightsScanner(deps.ch);+ return {+ generateInsights: (projectId) => scanner.generateInsights(projectId),+ };+}diff --git a/packages/core/src/modules/insight/src/modules/devices.module.ts b/packages/core/src/modules/insight/src/modules/devices.module.tsindex 08a4806f6..2246bab94 100644--- a/packages/core/src/modules/insight/src/modules/devices.module.ts+++ b/packages/core/src/modules/insight/src/modules/devices.module.ts@@ -1,7 +1,5 @@-import {- formatClickhouseDate,- TABLE_NAMES,-} from '@openpanel/db/src/clickhouse/client';+import { formatClickhouseDate } from '../../../../shared/ch-dates';+import { TABLE_NAMES } from '../../../../shared/ch-tables';import type {ComputeContext,ComputeResult,diff --git a/packages/core/src/modules/insight/src/modules/entry-pages.module.ts b/packages/core/src/modules/insight/src/modules/entry-pages.module.tsindex ecfbfd83e..3508e3af0 100644--- a/packages/core/src/modules/insight/src/modules/entry-pages.module.ts+++ b/packages/core/src/modules/insight/src/modules/entry-pages.module.ts@@ -1,7 +1,5 @@-import {- formatClickhouseDate,- TABLE_NAMES,-} from '@openpanel/db/src/clickhouse/client';+import { formatClickhouseDate } from '../../../../shared/ch-dates';+import { TABLE_NAMES } from '../../../../shared/ch-tables';import type {ComputeContext,ComputeResult,diff --git a/packages/core/src/modules/insight/src/modules/geo.module.ts b/packages/core/src/modules/insight/src/modules/geo.module.tsindex 3b750578a..e79601d78 100644--- a/packages/core/src/modules/insight/src/modules/geo.module.ts+++ b/packages/core/src/modules/insight/src/modules/geo.module.ts@@ -1,8 +1,6 @@import { getCountry } from '@openpanel/constants';-import {- formatClickhouseDate,- TABLE_NAMES,-} from '@openpanel/db/src/clickhouse/client';+import { formatClickhouseDate } from '../../../../shared/ch-dates';+import { TABLE_NAMES } from '../../../../shared/ch-tables';import type {ComputeContext,ComputeResult,diff --git a/packages/core/src/modules/insight/src/modules/page-trends.module.ts b/packages/core/src/modules/insight/src/modules/page-trends.module.tsindex b1ecf4422..4b95edf7e 100644--- a/packages/core/src/modules/insight/src/modules/page-trends.module.ts+++ b/packages/core/src/modules/insight/src/modules/page-trends.module.ts@@ -1,7 +1,5 @@-import {- formatClickhouseDate,- TABLE_NAMES,-} from '@openpanel/db/src/clickhouse/client';+import { formatClickhouseDate } from '../../../../shared/ch-dates';+import { TABLE_NAMES } from '../../../../shared/ch-tables';import type {ComputeContext,ComputeResult,diff --git a/packages/core/src/modules/insight/src/modules/referrers.module.ts b/packages/core/src/modules/insight/src/modules/referrers.module.tsindex 18ad10fe8..c7647af55 100644--- a/packages/core/src/modules/insight/src/modules/referrers.module.ts+++ b/packages/core/src/modules/insight/src/modules/referrers.module.ts@@ -1,7 +1,5 @@-import {- formatClickhouseDate,- TABLE_NAMES,-} from '@openpanel/db/src/clickhouse/client';+import { formatClickhouseDate } from '../../../../shared/ch-dates';+import { TABLE_NAMES } from '../../../../shared/ch-tables';import type {ComputeContext,ComputeResult,diff --git a/packages/core/src/modules/insight/src/referrer-spikes.ts b/packages/core/src/modules/insight/src/referrer-spikes.tsindex 719cabd62..6327fa420 100644--- a/packages/core/src/modules/insight/src/referrer-spikes.ts+++ b/packages/core/src/modules/insight/src/referrer-spikes.ts@@ -1,13 +1,15 @@-import {- ch,- convertClickhouseDateToJs,- TABLE_NAMES,-} from '@openpanel/db/src/clickhouse/client';+// M10-009: the ClickHouse CLIENT is `deps.ch` (the boot scope's round-robin+// proxy), so the requestId minted at the edge reaches these queries (ADR-018).+// `clix` stays a value import: it is a pure query BUILDER that takes the+// client as its first argument, not a connection — same standing as ADR-013's+// `sql` tag (see shared/ch-query.ts). Table names and the date helper are+// core's own copies (shared/ch-tables.ts, shared/ch-dates.ts).import { clix } from '@openpanel/db/src/clickhouse/query-builder';-// `overview` hasn't moved to core yet (a later wave) — deep-imported like the-// rest of @openpanel/db's internals until it does.-import { overviewService } from '@openpanel/core';import type { IChartEventFilter, IInterval } from '@openpanel/validation';+import type { ServiceDeps } from '../../../services';+import { convertClickhouseDateToJs } from '../../../shared/ch-dates';+import { TABLE_NAMES } from '../../../shared/ch-tables';+import { overviewService } from '../../../v1-compat';// Spike detection thresholds. Conservative defaults — markers should be rare// and obviously meaningful when they appear. Tune here if real usage shows@@ -73,8 +75,10 @@ export interface GetReferrerSpikesInput {}export async function getReferrerSpikes(+ deps: ServiceDeps,input: GetReferrerSpikesInput): Promise<ReferrerSpikeCluster[]> {+ const ch = deps.ch;const { projectId, filters, startDate, endDate, interval, timezone } = input;const filtersWhere = overviewService.getRawWhereClause('sessions', filters);diff --git a/packages/core/src/modules/insight/src/store.ts b/packages/core/src/modules/insight/src/store.tsindex 26705ab93..ee7ea9feb 100644--- a/packages/core/src/modules/insight/src/store.ts+++ b/packages/core/src/modules/insight/src/store.ts@@ -1,4 +1,12 @@-import { db, Prisma } from '@openpanel/db/src/prisma-client';+// M10-009: the store is a FACTORY over `ServiceDeps` — `deps.db` is the boot+// scope's client, so the requestId minted at the edge reaches every insight+// write (ADR-018, docs/TECH_DEBT.md §4). `Prisma` here is the namespace's two+// JSON sentinels (`DbNull`), which live in the same module as the constructed+// client and are reached through the declared v1-compat seam+// (`compatPrisma()`) rather than by importing `@openpanel/db` from core.++import type { Prisma } from '@openpanel/db/src/prisma-client';+import type { ServiceDeps } from '../../../services';import type {Cadence,InsightStore,@@ -6,7 +14,16 @@ import type {WindowKind,} from './types';-export const insightStore: InsightStore = {+/** `Prisma.DbNull` is a plain sentinel value that lives in the same module as+ * the constructed client — reached through the v1-compat seam so this file+ * value-imports nothing from `@openpanel/db`. */+function loadCompatPrisma() {+ return import('../../../v1-compat').then((m) => m.compatPrisma());+}++export function createInsightStore(deps: ServiceDeps): InsightStore {+ const db = deps.db;+ return {// `cadence` isn't read — there is only one today ('daily') — but it stays// on the signature because InsightStore's contract names it (V1 parity).async listProjectIdsForCadence(_cadence: Cadence): Promise<string[]> {@@ -189,16 +206,17 @@ export const insightStore: InsightStore = {changeTo,now,}): Promise<void> {+ const { DbNull } = await loadCompatPrisma();await db.insightEvent.create({data: {insightId,eventKind,changeFrom: changeFrom? (changeFrom as Prisma.InputJsonValue)- : Prisma.DbNull,+ : DbNull,changeTo: changeTo? (changeTo as Prisma.InputJsonValue)- : Prisma.DbNull,+ : DbNull,createdAt: now,},});@@ -301,4 +319,5 @@ export const insightStore: InsightStore = {});return { deleted: result.count };},-};+ };+}diff --git a/packages/core/src/modules/integration/integration.routes.ts b/packages/core/src/modules/integration/integration.routes.tsindex 02bc93ffe..42d9a1d20 100644--- a/packages/core/src/modules/integration/integration.routes.ts+++ b/packages/core/src/modules/integration/integration.routes.ts@@ -39,8 +39,10 @@ export const integrationRoutes = defineRoutes((app) =>'/webhook/slack',async ({ query, ctx }) => {try {- const { organizationId, projectId } =- await completeSlackOAuthCallback(query);+ const { organizationId, projectId } = await completeSlackOAuthCallback(+ ctx,+ query+ );// Integrations are project-scoped; the org-level integrations route no// longer exists. Newer installs carry projectId in their metadata.diff --git a/packages/core/src/modules/integration/integration.rpc.ts b/packages/core/src/modules/integration/integration.rpc.tsindex 0454ec7a2..dde4878ce 100644--- a/packages/core/src/modules/integration/integration.rpc.ts+++ b/packages/core/src/modules/integration/integration.rpc.ts@@ -43,13 +43,14 @@ export const integrationRouter = createTRPCRouter({get: procedure.input(z.object({ id: z.string() })).query(({ input, ctx }) =>- getIntegrationById(requireLogin(ctx.session.userId), input.id)+ getIntegrationById(ctx, requireLogin(ctx.session.userId), input.id)),list: procedure.input(z.object({ projectId: z.string() })).query(({ input, ctx }) =>listIntegrationsForProject(+ ctx,requireLogin(ctx.session.userId),input.projectId)@@ -58,7 +59,11 @@ export const integrationRouter = createTRPCRouter({createOrUpdateSlack: procedure.input(zCreateSlackIntegration).mutation(({ input, ctx }) =>- createOrUpdateSlackIntegration(requireLogin(ctx.session.userId), input)+ createOrUpdateSlackIntegration(+ ctx,+ requireLogin(ctx.session.userId),+ input+ )),// Generic create/update for any form-configured integration. Per-type@@ -73,7 +78,7 @@ export const integrationRouter = createTRPCRouter({})).mutation(({ input, ctx }) =>- upsertIntegration(requireLogin(ctx.session.userId), input)+ upsertIntegration(ctx, requireLogin(ctx.session.userId), input)),// Back-compat alias for the export forms; delegates to the same generic path.@@ -81,7 +86,7 @@ export const integrationRouter = createTRPCRouter({createOrUpdateExport: procedure.input(z.union([zCreateS3ExportIntegration, zCreateGCSExportIntegration])).mutation(({ input, ctx }) =>- upsertIntegration(requireLogin(ctx.session.userId), input)+ upsertIntegration(ctx, requireLogin(ctx.session.userId), input)),testConnection: procedure@@ -92,7 +97,7 @@ export const integrationRouter = createTRPCRouter({})).mutation(({ input, ctx }) =>- testIntegrationConnection(requireLogin(ctx.session.userId), input)+ testIntegrationConnection(ctx, requireLogin(ctx.session.userId), input)),// Back-compat alias for the export forms; same gate as `testConnection`.@@ -100,12 +105,16 @@ export const integrationRouter = createTRPCRouter({testExportConnection: procedure.input(z.union([zCreateS3ExportIntegration, zCreateGCSExportIntegration])).mutation(({ input, ctx }) =>- testExportIntegrationConnection(requireLogin(ctx.session.userId), input)+ testExportIntegrationConnection(+ ctx,+ requireLogin(ctx.session.userId),+ input+ )),delete: procedure.input(z.object({ id: z.string() })).mutation(({ input: { id }, ctx }) =>- deleteIntegration(requireLogin(ctx.session.userId), id)+ deleteIntegration(ctx, requireLogin(ctx.session.userId), id)),});diff --git a/packages/core/src/modules/integration/integration.service.ts b/packages/core/src/modules/integration/integration.service.tsindex 8bb94a469..77e76d578 100644--- a/packages/core/src/modules/integration/integration.service.ts+++ b/packages/core/src/modules/integration/integration.service.ts@@ -17,10 +17,10 @@// raw lookup (no ladder involved), reached directly from the sibling// `shared/access-lookups.ts`.//-// db access is LAZY (`loadDb` below), not a static top-level import — see-// organization.service.ts's header for the full reasoning (constructing-// @openpanel/db's clients at import time would spawn a pino-pretty transport-// worker thread per test file).+// M10-009: every exported function takes `ServiceDeps` and reaches Postgres+// as `deps.db`; the `loadDb()` lazy loader is gone, so a requestId minted at+// the edge reaches the query (ADR-018, docs/TECH_DEBT.md §4). V1's still-live+// trpc router keeps calling the deps-free spellings v1-compat.ts re-exports.import { z } from 'zod';import {@@ -38,16 +38,13 @@ import {slackInstaller,} from '../../clients/integrations/slack';import { TRPCBadRequestError, TRPCForbiddenError } from '../../rpc/errors';+import type { ServiceDeps } from '../../services';import { getOrganizationAccess } from '../../shared/access-lookups';import { getAccessChecks } from '../auth/auth.service';import { BASE_INTEGRATIONS } from '../notification/notification.service';import type { IIntegrationConfig, ISlackConfig } from './integration.constants';import { zSlackAuthResponse } from './src/slack-contract';-function loadDb() {- return import('@openpanel/db/src/prisma-client').then((m) => m.db);-}-// Credentials are write-only: they are encrypted at rest and never travel back// to a client. `read` on a project is bare membership, so returning the stored// ciphertext would hand every project member the org's object-store keys — and@@ -75,6 +72,7 @@ function rejectEncryptedSecrets(config: unknown) {// Assert the user can act on the project at `level`, and return the project's// organizationId (still stored on the integration for org-level queries/cascades).async function assertProjectAccessAndGetOrg(+ deps: ServiceDeps,userId: string,projectId: string,level: 'read' | 'write'@@ -82,7 +80,7 @@ async function assertProjectAccessAndGetOrg(const { requireProjectAccess } = await getAccessChecks();await requireProjectAccess({ userId, projectId, level });- const db = await loadDb();+ const db = deps.db;const project = await db.project.findUniqueOrThrow({where: { id: projectId },select: { organizationId: true },@@ -129,8 +127,12 @@ async function assertIntegrationAccess(}}-export async function getIntegrationById(userId: string, id: string) {- const db = await loadDb();+export async function getIntegrationById(+ deps: ServiceDeps,+ userId: string,+ id: string+) {+ const db = deps.db;const integration = await db.integration.findUniqueOrThrow({where: { id },});@@ -141,16 +143,18 @@ export async function getIntegrationById(userId: string, id: string) {}export async function listIntegrationsForProject(+ deps: ServiceDeps,userId: string,projectId: string) {const organizationId = await assertProjectAccessAndGetOrg(+ deps,userId,projectId,'read');- const db = await loadDb();+ const db = deps.db;const integrations = await db.integration.findMany({where: {// The project's own integrations, plus legacy org-wide integrations@@ -176,6 +180,7 @@ export async function listIntegrationsForProject(// behavior (validation, connection test, credential encryption) is delegated to// the integration's server plugin — adding a new integration needs no change here.export async function upsertIntegration(+ deps: ServiceDeps,userId: string,input: {id?: string;@@ -189,7 +194,7 @@ export async function upsertIntegration(// to one project can't update another project's integration in the same org.rejectEncryptedSecrets(input.config);- const db = await loadDb();+ const db = deps.db;let organizationId: string;let storedConfig: unknown;@@ -203,6 +208,7 @@ export async function upsertIntegration(storedConfig = existing.config;} else {organizationId = await assertProjectAccessAndGetOrg(+ deps,userId,input.projectId,'write'@@ -256,6 +262,7 @@ export async function upsertIntegration(}export async function createOrUpdateSlackIntegration(+ deps: ServiceDeps,userId: string,input: { id?: string; name: string; projectId: string }) {@@ -267,7 +274,7 @@ export async function createOrUpdateSlackIntegration(// on an update is unauthorized and may point at a different project.let projectId: string;- const db = await loadDb();+ const db = deps.db;if (input.id) {const existing = await db.integration.findUniqueOrThrow({@@ -279,6 +286,7 @@ export async function createOrUpdateSlackIntegration(projectId = existing.projectId ?? input.projectId;} else {organizationId = await assertProjectAccessAndGetOrg(+ deps,userId,input.projectId,'write'@@ -323,6 +331,7 @@ export async function createOrUpdateSlackIntegration(// caller-supplied credentials, so it must not be reachable by anyone who// merely holds a session.export async function testIntegrationConnection(+ deps: ServiceDeps,userId: string,input: { projectId: string; config: IIntegrationConfig }) {@@ -343,6 +352,7 @@ export async function testIntegrationConnection(// Back-compat alias for the export forms; same gate as `testIntegrationConnection`.export async function testExportIntegrationConnection(+ deps: ServiceDeps,userId: string,input: { projectId: string; config: IIntegrationConfig }) {@@ -361,8 +371,12 @@ export async function testExportIntegrationConnection();}-export async function deleteIntegration(userId: string, id: string) {- const db = await loadDb();+export async function deleteIntegration(+ deps: ServiceDeps,+ userId: string,+ id: string+) {+ const db = deps.db;const integration = await db.integration.findUniqueOrThrow({where: { id },});@@ -396,10 +410,13 @@ export interface CompleteSlackOAuthCallbackResult {projectId?: string;}-export async function completeSlackOAuthCallback(params: {- code: string;- state: string;-}): Promise<CompleteSlackOAuthCallbackResult> {+export async function completeSlackOAuthCallback(+ deps: ServiceDeps,+ params: {+ code: string;+ state: string;+ }+): Promise<CompleteSlackOAuthCallbackResult> {const verifiedState = await slackInstaller.stateStore?.verifyStateParam(new Date(),params.state@@ -438,7 +455,7 @@ export async function completeSlackOAuthCallback(params: {const { organizationId, integrationId, projectId } = parsedMetadata.data;- const db = await loadDb();+ const db = deps.db;await db.integration.update({where: {id: integrationId,@@ -454,3 +471,44 @@ export async function completeSlackOAuthCallback(params: {return { organizationId, projectId };}++// ---------------------------------------------------------------------------+// `ctx.services.integration` binding.+// ---------------------------------------------------------------------------++/** A module function's signature with its leading `ServiceDeps` dropped. */+type WithoutDeps<T extends (deps: ServiceDeps, ...args: never[]) => unknown> =+ T extends (deps: ServiceDeps, ...args: infer A) => infer R+ ? (...args: A) => R+ : never;++export interface IntegrationService {+ getById: WithoutDeps<typeof getIntegrationById>;+ listForProject: WithoutDeps<typeof listIntegrationsForProject>;+ upsert: WithoutDeps<typeof upsertIntegration>;+ createOrUpdateSlack: WithoutDeps<typeof createOrUpdateSlackIntegration>;+ testConnection: WithoutDeps<typeof testIntegrationConnection>;+ testExportConnection: WithoutDeps<typeof testExportIntegrationConnection>;+ delete: WithoutDeps<typeof deleteIntegration>;+ completeSlackOAuthCallback: WithoutDeps<typeof completeSlackOAuthCallback>;+}++/** M10-009: this module had no factory at all — the one `*.service.ts` file+ * `services.ts` did not register (docs/TECH_DEBT.md §5a). */+export function createIntegrationService(+ deps: ServiceDeps+): IntegrationService {+ return {+ getById: (...args) => getIntegrationById(deps, ...args),+ listForProject: (...args) => listIntegrationsForProject(deps, ...args),+ upsert: (...args) => upsertIntegration(deps, ...args),+ createOrUpdateSlack: (...args) =>+ createOrUpdateSlackIntegration(deps, ...args),+ testConnection: (...args) => testIntegrationConnection(deps, ...args),+ testExportConnection: (...args) =>+ testExportIntegrationConnection(deps, ...args),+ delete: (...args) => deleteIntegration(deps, ...args),+ completeSlackOAuthCallback: (...args) =>+ completeSlackOAuthCallback(deps, ...args),+ };+}diff --git a/packages/core/src/modules/mcp/mcp.service.test.ts b/packages/core/src/modules/mcp/mcp.service.test.tsindex c2dbad406..71c24ecae 100644--- a/packages/core/src/modules/mcp/mcp.service.test.ts+++ b/packages/core/src/modules/mcp/mcp.service.test.ts@@ -66,9 +66,7 @@ mock.module('../../clients/logger', () => ({}));const mockGetClientByIdCached = mock();-const actualClientsService = await import(- '@openpanel/core'-);+const actualClientsService = await import('@openpanel/core');const realClientsService = { ...actualClientsService };mock.module('@openpanel/core', () => ({...realClientsService,@@ -101,15 +99,19 @@ mock.module('@openpanel/db/src/prisma-client', () => ({db: { project: { findUnique: mockFindUnique } },}));-afterAll(() => {+afterAll(async () => {mock.module('../../clients/logger', () => realLogger);- mock.module(- '@openpanel/core',- () => realClientsService- );+ mock.module('@openpanel/core', () => realClientsService);mock.module('@openpanel/redis', () => realRedis);mock.module('../../shared/crypto', () => realCrypto);mock.module('@openpanel/db/src/prisma-client', () => realPrismaClient);+ // Restoring the module registry is not enough: `v1-compat.ts` MEMOIZES the+ // fallback `ServiceDeps` the first time anything resolves it, so if that+ // happened while the mock above was installed, every later FILE in this+ // process keeps the mocked client (bare `bun test` shares one registry).+ // Drop the memo too — same reason mcp's dashboard-management.test.ts does.+ const { resetV1CompatServicesForTests } = await import('../../v1-compat');+ resetV1CompatServicesForTests();});let handleStatelessMcpRequest: typeof import('./mcp.service').handleStatelessMcpRequest;diff --git a/packages/core/src/modules/mcp/mcp.service.ts b/packages/core/src/modules/mcp/mcp.service.tsindex df87d8f67..2098e3acc 100644--- a/packages/core/src/modules/mcp/mcp.service.ts+++ b/packages/core/src/modules/mcp/mcp.service.ts@@ -29,7 +29,6 @@import { InMemoryTransport } from '@modelcontextprotocol/sdk/inMemory.js';import type { JSONRPCMessage } from '@modelcontextprotocol/sdk/types.js';import { createLogger } from '../../clients/logger';-import type { ServiceDeps } from '../../services';import {authenticateToken,extractToken,@@ -207,7 +206,14 @@ export interface McpService {): Promise<McpHttpResult>;}-export function createMcpService(_deps: ServiceDeps): McpService {+/**+ * Takes NO `ServiceDeps`: `extractToken` is pure and+ * `handleStatelessMcpRequest` hands the request to the+ * `@modelcontextprotocol/sdk` server, whose tool handlers have a fixed+ * signature with no room for a `deps` argument (docs/TECH_DEBT.md's+ * "loaders still standing" table).+ */+export function createMcpService(): McpService {return {extractToken,handleStatelessMcpRequest,diff --git a/packages/core/src/modules/mcp/src/auth.ts b/packages/core/src/modules/mcp/src/auth.tsindex 52d4d0df1..e4a4aef4b 100644--- a/packages/core/src/modules/mcp/src/auth.ts+++ b/packages/core/src/modules/mcp/src/auth.ts@@ -7,12 +7,13 @@ const logger = createLogger({ name: 'mcp:auth' });// GENUINE CYCLE, kept lazy: `mcp.service.ts` statically imports this file// (`extractToken` must stay synchronous — its own caller doesn't await it),-// and this package's barrel (`@openpanel/core`) transitively reaches-// services.ts, which registers `createMcpService`. services.ts -> mcp.service.ts-// -> this file -> @openpanel/core (index.ts) -> v1-compat.ts -> services.ts;-// the dynamic import is what keeps it a cycle ESM can evaluate.+// and v1-compat.ts imports services.ts, which registers `createMcpService`.+// services.ts -> mcp.service.ts -> this file -> v1-compat.ts -> services.ts;+// the dynamic import is what keeps it a cycle ESM can evaluate. M10-009: the+// hop goes to the seam directly rather than through this package's own barrel+// (`core-no-self-barrel`) — same cycle, one module shorter.function loadClientService() {- return import('@openpanel/core');+ return import('../../../v1-compat');}export interface McpAuthContext {diff --git a/packages/core/src/modules/mcp/src/tools/analytics/active-users.ts b/packages/core/src/modules/mcp/src/tools/analytics/active-users.tsindex 0422dff76..9737abca2 100644--- a/packages/core/src/modules/mcp/src/tools/analytics/active-users.ts+++ b/packages/core/src/modules/mcp/src/tools/analytics/active-users.ts@@ -1,6 +1,9 @@import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';-import { getRetentionSeries, getRollingActiveUsers } from '@openpanel/core';import { z } from 'zod';+import {+ getRetentionSeries,+ getRollingActiveUsers,+} from '../../../../../v1-compat';import type { McpAuthContext } from '../../auth';import {projectIdSchema,diff --git a/packages/core/src/modules/mcp/src/tools/analytics/engagement.ts b/packages/core/src/modules/mcp/src/tools/analytics/engagement.tsindex 5b4df1746..a5f14a001 100644--- a/packages/core/src/modules/mcp/src/tools/analytics/engagement.ts+++ b/packages/core/src/modules/mcp/src/tools/analytics/engagement.ts@@ -1,6 +1,6 @@import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';-import { getRetentionLastSeenSeries } from '@openpanel/core';import { z } from 'zod';+import { getRetentionLastSeenSeries } from '../../../../../v1-compat';import type { McpAuthContext } from '../../auth';import {projectIdSchema,diff --git a/packages/core/src/modules/mcp/src/tools/analytics/event-names.ts b/packages/core/src/modules/mcp/src/tools/analytics/event-names.tsindex f452827eb..5c2b88e1f 100644--- a/packages/core/src/modules/mcp/src/tools/analytics/event-names.ts+++ b/packages/core/src/modules/mcp/src/tools/analytics/event-names.ts@@ -1,5 +1,5 @@import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';-import { getTopEventNames } from '@openpanel/core';+import { getTopEventNames } from '../../../../../v1-compat';import type { McpAuthContext } from '../../auth';import {projectIdSchema,diff --git a/packages/core/src/modules/mcp/src/tools/analytics/events.ts b/packages/core/src/modules/mcp/src/tools/analytics/events.tsindex 9b0cbbec1..d2a29c1e5 100644--- a/packages/core/src/modules/mcp/src/tools/analytics/events.ts+++ b/packages/core/src/modules/mcp/src/tools/analytics/events.ts@@ -1,6 +1,6 @@import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';-import { queryEventsCore } from '@openpanel/core';import { z } from 'zod';+import { queryEventsCore } from '../../../../../v1-compat';import type { McpAuthContext } from '../../auth';import {projectIdSchema,diff --git a/packages/core/src/modules/mcp/src/tools/analytics/funnel.ts b/packages/core/src/modules/mcp/src/tools/analytics/funnel.tsindex 56e5463f4..7b71040f5 100644--- a/packages/core/src/modules/mcp/src/tools/analytics/funnel.ts+++ b/packages/core/src/modules/mcp/src/tools/analytics/funnel.ts@@ -1,6 +1,6 @@import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';-import { getFunnelCore } from '@openpanel/core';import { z } from 'zod';+import { getFunnelCore } from '../../../../../v1-compat';import type { McpAuthContext } from '../../auth';import {projectIdSchema,diff --git a/packages/core/src/modules/mcp/src/tools/analytics/groups.ts b/packages/core/src/modules/mcp/src/tools/analytics/groups.tsindex d55e5c9dc..edb21bc3f 100644--- a/packages/core/src/modules/mcp/src/tools/analytics/groups.ts+++ b/packages/core/src/modules/mcp/src/tools/analytics/groups.ts@@ -1,11 +1,11 @@import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';+import { z } from 'zod';import {getGroupById,getGroupList,getGroupMemberProfiles,getGroupTypes,-} from '@openpanel/core';-import { z } from 'zod';+} from '../../../../../v1-compat';import type { McpAuthContext } from '../../auth';import {projectIdSchema,diff --git a/packages/core/src/modules/mcp/src/tools/analytics/overview.ts b/packages/core/src/modules/mcp/src/tools/analytics/overview.tsindex dcd9925d9..bb4eedd07 100644--- a/packages/core/src/modules/mcp/src/tools/analytics/overview.ts+++ b/packages/core/src/modules/mcp/src/tools/analytics/overview.ts@@ -1,6 +1,6 @@import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';-import { getAnalyticsOverviewCore } from '@openpanel/core';import { z } from 'zod';+import { getAnalyticsOverviewCore } from '../../../../../v1-compat';import type { McpAuthContext } from '../../auth';import {projectIdSchema,diff --git a/packages/core/src/modules/mcp/src/tools/analytics/page-conversions.ts b/packages/core/src/modules/mcp/src/tools/analytics/page-conversions.tsindex 23513e1bc..33d46ad13 100644--- a/packages/core/src/modules/mcp/src/tools/analytics/page-conversions.ts+++ b/packages/core/src/modules/mcp/src/tools/analytics/page-conversions.ts@@ -1,6 +1,6 @@import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';-import { getPageConversionsCore } from '@openpanel/core';import { z } from 'zod';+import { getPageConversionsCore } from '../../../../../v1-compat';import type { McpAuthContext } from '../../auth';import {projectIdSchema,diff --git a/packages/core/src/modules/mcp/src/tools/analytics/page-performance.ts b/packages/core/src/modules/mcp/src/tools/analytics/page-performance.tsindex 72f20cdd4..40e8aa605 100644--- a/packages/core/src/modules/mcp/src/tools/analytics/page-performance.ts+++ b/packages/core/src/modules/mcp/src/tools/analytics/page-performance.ts@@ -1,6 +1,6 @@import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';-import { getSettingsForProject } from '@openpanel/core';import { z } from 'zod';+import { getSettingsForProject } from '../../../../../v1-compat';import type { McpAuthContext } from '../../auth';import {loadCompatPagesService,diff --git a/packages/core/src/modules/mcp/src/tools/analytics/pages.ts b/packages/core/src/modules/mcp/src/tools/analytics/pages.tsindex 63c13ed0e..dcaa86805 100644--- a/packages/core/src/modules/mcp/src/tools/analytics/pages.ts+++ b/packages/core/src/modules/mcp/src/tools/analytics/pages.ts@@ -1,6 +1,9 @@import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';-import { getEntryExitPagesCore, getTopPagesCore } from '@openpanel/core';import { z } from 'zod';+import {+ getEntryExitPagesCore,+ getTopPagesCore,+} from '../../../../../v1-compat';import type { McpAuthContext } from '../../auth';import {projectIdSchema,diff --git a/packages/core/src/modules/mcp/src/tools/analytics/profile-metrics.ts b/packages/core/src/modules/mcp/src/tools/analytics/profile-metrics.tsindex f5ece3c36..1e2f039dc 100644--- a/packages/core/src/modules/mcp/src/tools/analytics/profile-metrics.ts+++ b/packages/core/src/modules/mcp/src/tools/analytics/profile-metrics.ts@@ -1,6 +1,6 @@import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';-import { getProfileMetrics } from '@openpanel/core';import { z } from 'zod';+import { getProfileMetrics } from '../../../../../v1-compat';import type { McpAuthContext } from '../../auth';import {projectIdSchema,diff --git a/packages/core/src/modules/mcp/src/tools/analytics/profiles.ts b/packages/core/src/modules/mcp/src/tools/analytics/profiles.tsindex 8ee759068..d78fe1648 100644--- a/packages/core/src/modules/mcp/src/tools/analytics/profiles.ts+++ b/packages/core/src/modules/mcp/src/tools/analytics/profiles.ts@@ -1,10 +1,10 @@import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';+import { z } from 'zod';import {findProfilesCore,getProfileSessionsCore,getProfileWithEvents,-} from '@openpanel/core';-import { z } from 'zod';+} from '../../../../../v1-compat';import type { McpAuthContext } from '../../auth';import { dashboardBaseUrl, profileUrl } from '../dashboard-links';import {diff --git a/packages/core/src/modules/mcp/src/tools/analytics/property-values.ts b/packages/core/src/modules/mcp/src/tools/analytics/property-values.tsindex 9bf90158d..ceeebe675 100644--- a/packages/core/src/modules/mcp/src/tools/analytics/property-values.ts+++ b/packages/core/src/modules/mcp/src/tools/analytics/property-values.ts@@ -1,6 +1,6 @@import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';-import { EVENT_COLUMNS } from '@openpanel/core';import { z } from 'zod';+import { EVENT_COLUMNS } from '../../../../event/event.service';import type { McpAuthContext } from '../../auth';import {loadCompatCh,diff --git a/packages/core/src/modules/mcp/src/tools/analytics/reports.ts b/packages/core/src/modules/mcp/src/tools/analytics/reports.tsindex 291a4d21b..39cd3a86b 100644--- a/packages/core/src/modules/mcp/src/tools/analytics/reports.ts+++ b/packages/core/src/modules/mcp/src/tools/analytics/reports.ts@@ -1,14 +1,14 @@import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';+import { z } from 'zod';+import { getChartStartEndDate } from '../../../../../shared/date';import {AggregateChartEngine,ChartEngine,- getChartStartEndDate,getFunnel,getReportById,getReportsByDashboardId,getSettingsForProject,-} from '@openpanel/core';-import { z } from 'zod';+} from '../../../../../v1-compat';import type { McpAuthContext } from '../../auth';import { dashboardBaseUrl } from '../dashboard-links';import {diff --git a/packages/core/src/modules/mcp/src/tools/analytics/retention.ts b/packages/core/src/modules/mcp/src/tools/analytics/retention.tsindex 172c81c97..3356310b6 100644--- a/packages/core/src/modules/mcp/src/tools/analytics/retention.ts+++ b/packages/core/src/modules/mcp/src/tools/analytics/retention.ts@@ -1,5 +1,5 @@import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';-import { getRetentionCohortCore } from '@openpanel/core';+import { getRetentionCohortCore } from '../../../../../v1-compat';import type { McpAuthContext } from '../../auth';import {projectIdSchema,diff --git a/packages/core/src/modules/mcp/src/tools/analytics/sessions.ts b/packages/core/src/modules/mcp/src/tools/analytics/sessions.tsindex a10540dae..f94d74b16 100644--- a/packages/core/src/modules/mcp/src/tools/analytics/sessions.ts+++ b/packages/core/src/modules/mcp/src/tools/analytics/sessions.ts@@ -1,6 +1,6 @@import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';-import { querySessionsCore } from '@openpanel/core';import { z } from 'zod';+import { querySessionsCore } from '../../../../../v1-compat';import type { McpAuthContext } from '../../auth';import { dashboardBaseUrl } from '../dashboard-links';import {diff --git a/packages/core/src/modules/mcp/src/tools/analytics/traffic.ts b/packages/core/src/modules/mcp/src/tools/analytics/traffic.tsindex ad7492b8d..b52d6276d 100644--- a/packages/core/src/modules/mcp/src/tools/analytics/traffic.ts+++ b/packages/core/src/modules/mcp/src/tools/analytics/traffic.ts@@ -1,6 +1,7 @@import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';-import { getTrafficBreakdownCore, type TrafficColumn } from '@openpanel/core';import { z } from 'zod';+import { getTrafficBreakdownCore } from '../../../../../v1-compat';+import type { TrafficColumn } from '../../../../overview/overview.service';import type { McpAuthContext } from '../../auth';import {projectIdSchema,diff --git a/packages/core/src/modules/mcp/src/tools/analytics/user-flow.ts b/packages/core/src/modules/mcp/src/tools/analytics/user-flow.tsindex bf72f88bf..5f208524e 100644--- a/packages/core/src/modules/mcp/src/tools/analytics/user-flow.ts+++ b/packages/core/src/modules/mcp/src/tools/analytics/user-flow.ts@@ -1,6 +1,6 @@import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';-import { getUserFlowCore } from '@openpanel/core';import { z } from 'zod';+import { getUserFlowCore } from '../../../../../v1-compat';import type { McpAuthContext } from '../../auth';import {projectIdSchema,diff --git a/packages/core/src/modules/mcp/src/tools/dashboard-management.ts b/packages/core/src/modules/mcp/src/tools/dashboard-management.tsindex 25ee0e8f9..5d0a09be7 100644--- a/packages/core/src/modules/mcp/src/tools/dashboard-management.ts+++ b/packages/core/src/modules/mcp/src/tools/dashboard-management.ts@@ -1,7 +1,8 @@import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';-import { getDashboardById, getId, getProjectById } from '@openpanel/core';import { zReport } from '@openpanel/validation';import { z } from 'zod';+import { getId } from '../../../../shared/slug-id';+import { getDashboardById, getProjectById } from '../../../../v1-compat';import type { McpAuthContext } from '../auth';import { dashboardBaseUrl } from './dashboard-links';import {@@ -328,7 +329,7 @@ export function registerDashboardManagementTools(const dashboard = await db.dashboard.create({data: {- id: await getId('dashboard', name),+ id: await getId({ db }, 'dashboard', name),projectId,organizationId: project.organizationId,name,diff --git a/packages/core/src/modules/mcp/src/tools/gsc/cannibalization.ts b/packages/core/src/modules/mcp/src/tools/gsc/cannibalization.tsindex 2767395fb..25536cca9 100644--- a/packages/core/src/modules/mcp/src/tools/gsc/cannibalization.ts+++ b/packages/core/src/modules/mcp/src/tools/gsc/cannibalization.ts@@ -1,5 +1,5 @@import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';-import { getGscCannibalization } from '@openpanel/core';+import { getGscCannibalization } from '../../../../gsc/gsc.service';import type { McpAuthContext } from '../../auth';import {projectIdSchema,diff --git a/packages/core/src/modules/mcp/src/tools/gsc/overview.ts b/packages/core/src/modules/mcp/src/tools/gsc/overview.tsindex 9d7dd6972..7d71111a6 100644--- a/packages/core/src/modules/mcp/src/tools/gsc/overview.ts+++ b/packages/core/src/modules/mcp/src/tools/gsc/overview.ts@@ -1,6 +1,6 @@import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';-import { getGscOverview } from '@openpanel/core';import { z } from 'zod';+import { getGscOverview } from '../../../../../v1-compat';import type { McpAuthContext } from '../../auth';import {projectIdSchema,diff --git a/packages/core/src/modules/mcp/src/tools/gsc/pages.ts b/packages/core/src/modules/mcp/src/tools/gsc/pages.tsindex 553ac92db..a722f3c6f 100644--- a/packages/core/src/modules/mcp/src/tools/gsc/pages.ts+++ b/packages/core/src/modules/mcp/src/tools/gsc/pages.ts@@ -1,6 +1,6 @@import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';-import { getGscPageDetails, getGscPages } from '@openpanel/core';import { z } from 'zod';+import { getGscPageDetails, getGscPages } from '../../../../../v1-compat';import type { McpAuthContext } from '../../auth';import {projectIdSchema,diff --git a/packages/core/src/modules/mcp/src/tools/gsc/queries.ts b/packages/core/src/modules/mcp/src/tools/gsc/queries.tsindex 38abaf8ab..5cef5a0f4 100644--- a/packages/core/src/modules/mcp/src/tools/gsc/queries.ts+++ b/packages/core/src/modules/mcp/src/tools/gsc/queries.ts@@ -1,7 +1,7 @@import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';-import type { GscQueryOpportunity } from '@openpanel/core';-import { getGscQueries, getGscQueryDetails } from '@openpanel/core';import { z } from 'zod';+import { getGscQueries, getGscQueryDetails } from '../../../../../v1-compat';+import type { GscQueryOpportunity } from '../../../../gsc/gsc.service';import type { McpAuthContext } from '../../auth';import {projectIdSchema,diff --git a/packages/core/src/modules/mcp/src/tools/shared.ts b/packages/core/src/modules/mcp/src/tools/shared.tsindex 390c10e58..91eb9ed6a 100644--- a/packages/core/src/modules/mcp/src/tools/shared.ts+++ b/packages/core/src/modules/mcp/src/tools/shared.ts@@ -1,6 +1,6 @@-import { resolveClientProjectId } from '@openpanel/core';import { z } from 'zod';import { createLogger } from '../../../../clients/logger';+import { resolveClientProjectId } from '../../../../v1-compat';import type { McpAuthContext } from '../auth';const logger = createLogger({ name: 'mcp' });diff --git a/packages/core/src/modules/notification/notification.jobs.test.ts b/packages/core/src/modules/notification/notification.jobs.test.tsindex 7fef3bfef..aa033b690 100644--- a/packages/core/src/modules/notification/notification.jobs.test.ts+++ b/packages/core/src/modules/notification/notification.jobs.test.ts@@ -29,6 +29,7 @@ function stubJobCtx(notification: Partial<NotificationService>): JobCtx {auth: {} as Services['auth'],notification: notification as NotificationService,insight: {} as Services['insight'],+ integration: {} as Services['integration'],cohort: {} as Services['cohort'],gsc: {} as Services['gsc'],import: {} as Services['import'],@@ -40,6 +41,10 @@ function stubJobCtx(notification: Partial<NotificationService>): JobCtx {profile: {} as Services['profile'],group: {} as Services['group'],chart: {} as Services['chart'],+ funnel: {} as Services['funnel'],+ conversion: {} as Services['conversion'],+ sankey: {} as Services['sankey'],+ retention: {} as Services['retention'],overview: {} as Services['overview'],pages: {} as Services['pages'],realtime: {} as Services['realtime'],diff --git a/packages/core/src/modules/notification/notification.rpc.ts b/packages/core/src/modules/notification/notification.rpc.tsindex ddf3a8991..8fb818ad6 100644--- a/packages/core/src/modules/notification/notification.rpc.ts+++ b/packages/core/src/modules/notification/notification.rpc.ts@@ -42,14 +42,14 @@ export const notificationRouter = createTRPCRouter({.input(z.object({ projectId: z.string() })).query(({ input, ctx }) => {requireLogin(ctx.session.userId);- return listNotifications(input.projectId);+ return listNotifications(ctx, input.projectId);}),rules: procedure.input(z.object({ projectId: z.string() })).query(({ input, ctx }) => {requireLogin(ctx.session.userId);- return listNotificationRules(input.projectId);+ return listNotificationRules(ctx, input.projectId);}),createOrUpdateRule: procedure@@ -67,7 +67,7 @@ export const notificationRouter = createTRPCRouter({});if (input.id) {- const existing = await getNotificationRuleByIdOrThrow(input.id);+ const existing = await getNotificationRuleByIdOrThrow(ctx, input.id);await ctx.services.auth.requireProjectAccess({userId,projectId: existing.projectId,@@ -75,14 +75,14 @@ export const notificationRouter = createTRPCRouter({});}- return createOrUpdateNotificationRule(input);+ return createOrUpdateNotificationRule(ctx, input);}),deleteRule: procedure.input(z.object({ id: z.string() })).mutation(async ({ input, ctx }) => {const userId = requireLogin(ctx.session.userId);- const rule = await getNotificationRuleByIdOrThrow(input.id);+ const rule = await getNotificationRuleByIdOrThrow(ctx, input.id);await ctx.services.auth.requireProjectAccess({userId,@@ -90,6 +90,6 @@ export const notificationRouter = createTRPCRouter({level: 'write',});- return deleteNotificationRule(input.id);+ return deleteNotificationRule(ctx, input.id);}),});diff --git a/packages/core/src/modules/notification/notification.service.test.ts b/packages/core/src/modules/notification/notification.service.test.tsindex 0899fa691..5b9c03b86 100644--- a/packages/core/src/modules/notification/notification.service.test.ts+++ b/packages/core/src/modules/notification/notification.service.test.ts@@ -4,7 +4,7 @@// mock below is registered before the subject's first call, not before its// (side-effect-free) import.-import { beforeAll, beforeEach, expect, mock, test } from 'bun:test';+import { afterAll, beforeAll, beforeEach, expect, mock, test } from 'bun:test';import type { IChartEvent } from '@openpanel/validation';interface FakeRule {@@ -21,7 +21,12 @@ interface FakeRule {const ruleStore = new Map<string, FakeRule>();const integrationStore = new Map<string,- { id: string; projectId: string | null; organizationId: string; config: unknown }+ {+ id: string;+ projectId: string | null;+ organizationId: string;+ config: unknown;+ }>();function makeRule(overrides: Partial<FakeRule> & { id: string }): FakeRule {@@ -38,7 +43,13 @@ function makeRule(overrides: Partial<FakeRule> & { id: string }): FakeRule {}const publishedEvents: unknown[] = [];+// M10-009: spread a plain-object SNAPSHOT of the real module and restore it in+// afterAll — `mock.module` has no per-file scope under bare `bun test`+// (AGENTS.md), and a partial factory deletes `getRedisCache` and friends for+// every file that runs next.+const realRedis = { ...(await import('@openpanel/redis')) };mock.module('@openpanel/redis', () => ({+ ...realRedis,cacheable: (_name: string,fn: (...args: unknown[]) => unknown,@@ -49,6 +60,22 @@ mock.module('@openpanel/redis', () => ({},}));+const realPrismaClient = {+ ...(await import('@openpanel/db/src/prisma-client')),+};++afterAll(async () => {+ mock.module('@openpanel/redis', () => realRedis);+ mock.module('@openpanel/db/src/prisma-client', () => realPrismaClient);+ // Restoring the module registry is not enough: `v1-compat.ts` MEMOIZES the+ // fallback `ServiceDeps` the first time anything resolves it, so if that+ // happened while the mock above was installed, every later FILE in this+ // process keeps the mocked client (bare `bun test` shares one registry).+ // Drop the memo too — same reason mcp's dashboard-management.test.ts does.+ const { resetV1CompatServicesForTests } = await import('../../v1-compat');+ resetV1CompatServicesForTests();+});+const notificationRule = {findMany: mock(async ({ where }: { where: { projectId: string } }) =>[...ruleStore.values()].filter((r) => r.projectId === where.projectId)@@ -63,7 +90,10 @@ const notificationRule = {}),create: mock(async ({ data }: { data: Partial<FakeRule> }) => {- const row = makeRule({ id: `rule_${ruleStore.size + 1}`, ...data } as never);+ const row = makeRule({+ id: `rule_${ruleStore.size + 1}`,+ ...data,+ } as never);ruleStore.set(row.id, row);return row;}),@@ -101,12 +131,21 @@ const project = {findUniqueOrThrow: mock(async () => ({ organizationId: 'org_1' })),};+// M10-009: every function under test takes `ServiceDeps`, so `deps.db` IS the+// fake below. The `@openpanel/db/src/prisma-client` mock stays only for the+// cacheable `getNotificationRulesByProjectId`, which reaches Postgres through+// the v1-compat seam (see the service's header) — and for `Prisma.JsonNull` /+// `Prisma.DbNull`, which `compatPrisma()` reads from the same module.const actualPrismaClient = await import('@openpanel/db/src/prisma-client');mock.module('@openpanel/db/src/prisma-client', () => ({...actualPrismaClient,db: { notificationRule, integration, project },}));+const deps = {+ db: { notificationRule, integration, project },+} as unknown as import('../../services').ServiceDeps;+let subject: typeof import('./notification.service');beforeAll(async () => {subject = await import('./notification.service');@@ -131,9 +170,9 @@ test('isBaseIntegration finds app and email, nothing else', () => {test('matchEvent matches by exact name', () => {const payload = { name: 'click' } as never;expect(subject.matchEvent(payload, CLICK_EVENT)).toBe(true);- expect(- subject.matchEvent({ name: 'purchase' } as never, CLICK_EVENT)- ).toBe(false);+ expect(subject.matchEvent({ name: 'purchase' } as never, CLICK_EVENT)).toBe(+ false+ );});test('matchEvent matches any event name on a wildcard', () => {@@ -231,13 +270,10 @@ test('listNotificationRules merges the app/email pseudo-integrations onto real o}));- const result = await subject.listNotificationRules('proj_1');+ const result = await subject.listNotificationRules(deps, 'proj_1');expect(result).toHaveLength(1);- expect(result[0]?.integrations.map((i) => i.id)).toEqual([- 'app',- 'slack_1',- ]);+ expect(result[0]?.integrations.map((i) => i.id)).toEqual(['app', 'slack_1']);});test('createOrUpdateNotificationRule rejects an integration from another project', async () => {@@ -249,7 +285,7 @@ test('createOrUpdateNotificationRule rejects an integration from another project});await expect(- subject.createOrUpdateNotificationRule({+ subject.createOrUpdateNotificationRule(deps, {name: 'Rule',config: { type: 'events', events: [] },integrations: ['slack_1'],@@ -269,7 +305,7 @@ test('createOrUpdateNotificationRule rejects an export-only integration', async});await expect(- subject.createOrUpdateNotificationRule({+ subject.createOrUpdateNotificationRule(deps, {name: 'Rule',config: { type: 'events', events: [] },integrations: ['s3_1'],@@ -281,7 +317,7 @@ test('createOrUpdateNotificationRule rejects an export-only integration', async});test('createOrUpdateNotificationRule creates a new rule when id is absent', async () => {- const created = await subject.createOrUpdateNotificationRule({+ const created = await subject.createOrUpdateNotificationRule(deps, {name: 'Rule',config: { type: 'events', events: [] },integrations: ['app'],@@ -296,12 +332,12 @@ test('createOrUpdateNotificationRule creates a new rule when id is absent', asyntest('deleteNotificationRule removes the row', async () => {ruleStore.set('rule_1', makeRule({ id: 'rule_1' }));- await subject.deleteNotificationRule('rule_1');+ await subject.deleteNotificationRule(deps, 'rule_1');expect(ruleStore.has('rule_1')).toBe(false);});test('deliverNotification publishes the app pseudo-integration instead of enqueuing an email or plugin', async () => {- await subject.deliverNotification({+ await subject.deliverNotification(deps, {projectId: 'proj_1',title: 'Hello',message: 'World',diff --git a/packages/core/src/modules/notification/notification.service.ts b/packages/core/src/modules/notification/notification.service.tsindex 76be0596f..7f6e0b2ad 100644--- a/packages/core/src/modules/notification/notification.service.ts+++ b/packages/core/src/modules/notification/notification.service.ts@@ -21,7 +21,6 @@// would spawn a pino-pretty transport worker thread per test file).import { stripLeadingAndTrailingSlashes } from '@openpanel/common';-import type { IServiceCreateEventPayload, IServiceEvent } from '@openpanel/core';import type { Integration, Prisma } from '@openpanel/db/src/prisma-client';import { cacheable } from '@openpanel/redis';import type { IChartEvent, IChartEventFilter } from '@openpanel/validation';@@ -30,6 +29,10 @@ import { sendEmail } from '../../clients/email';import { getServerIntegration } from '../../clients/integrations/registry';import { TRPCBadRequestError, TRPCForbiddenError } from '../../rpc/errors';import type { ServiceDeps } from '../../services';+import type {+ IServiceCreateEventPayload,+ IServiceEvent,+} from '../event/event.service';import {type IIntegrationConfig,isKind,@@ -67,12 +70,19 @@ export type INotificationPayload =| { type: 'event'; event: IServiceCreateEventPayload }| { type: 'funnel'; funnel: IServiceEvent[] };-function loadDb() {- return import('@openpanel/db/src/prisma-client').then((m) => m.db);+// M10-009: `getNotificationRulesByProjectId` is `cacheable`, whose key is+// derived from the call's ARGUMENTS (packages/redis/cachable.ts), so it cannot+// take `ServiceDeps` as a leading parameter — it reaches Postgres through the+// declared v1-compat seam instead, same as shared/access-lookups.ts's+// `getProjectAccess`. Every other function in this file takes `deps` and uses+// `deps.db`. GENUINE CYCLE, kept lazy: services.ts -> notification.service.ts+// (this file) -> v1-compat.ts -> services.ts.+function loadCompatDb() {+ return import('../../v1-compat').then((m) => m.compatDb());}function loadPrisma() {- return import('@openpanel/db/src/prisma-client').then((m) => m.Prisma);+ return import('../../v1-compat').then((m) => m.compatPrisma());}// -- Rule cache --------------------------------------------------------@@ -84,7 +94,7 @@ export type INotificationRuleCached = Awaited<export const getNotificationRulesByProjectId = cacheable('getNotificationRulesByProjectId',async (projectId: string) => {- const db = await loadDb();+ const db = await loadCompatDb();return db.notificationRule.findMany({where: { projectId },select: {@@ -268,9 +278,10 @@ function isValidPayload<T>(/** apps/worker/src/jobs/notification.ts's `sendNotification` job body. */export async function deliverNotification(+ deps: ServiceDeps,notification: Prisma.NotificationUncheckedCreateInput): Promise<unknown> {- const db = await loadDb();+ const db = deps.db;// App + email are pseudo-integrations dispatched by flags, not real rows.// Lazy: @openpanel/redis is otherwise pulled in eagerly through this@@ -363,8 +374,8 @@ export async function deliverNotification(// -- RPC-facing CRUD ---------------------------------------------------------export function listNotifications(projectId: string) {- return loadDb().then((db) =>+export function listNotifications(deps: ServiceDeps, projectId: string) {+ return Promise.resolve(deps.db).then((db) =>db.notification.findMany({where: { projectId },orderBy: { createdAt: 'desc' },@@ -377,8 +388,11 @@ export function listNotifications(projectId: string) {);}-export async function listNotificationRules(projectId: string) {- const db = await loadDb();+export async function listNotificationRules(+ deps: ServiceDeps,+ projectId: string+) {+ const db = deps.db;const rules = await db.notificationRule.findMany({where: { projectId },include: { integrations: true },@@ -400,10 +414,11 @@ export async function listNotificationRules(projectId: string) {}));}-export function getNotificationRuleByIdOrThrow(id: string) {- return loadDb().then((db) =>- db.notificationRule.findUniqueOrThrow({ where: { id } })- );+export async function getNotificationRuleByIdOrThrow(+ deps: ServiceDeps,+ id: string+) {+ return await deps.db.notificationRule.findUniqueOrThrow({ where: { id } });}/**@@ -413,9 +428,10 @@ export function getNotificationRuleByIdOrThrow(id: string) {* then writes the rule.*/export async function createOrUpdateNotificationRule(+ deps: ServiceDeps,input: ICreateNotificationRule) {- const db = await loadDb();+ const db = deps.db;const project = await db.project.findUniqueOrThrow({where: { id: input.projectId },select: { organizationId: true },@@ -497,8 +513,8 @@ export async function createOrUpdateNotificationRule(});}-export function deleteNotificationRule(id: string) {- return loadDb().then((db) => db.notificationRule.delete({ where: { id } }));+export async function deleteNotificationRule(deps: ServiceDeps, id: string) {+ return await deps.db.notificationRule.delete({ where: { id } });}// -- Services surface --------------------------------------------------@@ -510,9 +526,9 @@ export interface NotificationService {}export function createNotificationService(- _deps: ServiceDeps+ deps: ServiceDeps): NotificationService {return {- dispatch: deliverNotification,+ dispatch: (notification) => deliverNotification(deps, notification),};}diff --git a/packages/core/src/modules/onboarding/onboarding.jobs.test.ts b/packages/core/src/modules/onboarding/onboarding.jobs.test.tsindex 62dc78cca..d22c339fe 100644--- a/packages/core/src/modules/onboarding/onboarding.jobs.test.ts+++ b/packages/core/src/modules/onboarding/onboarding.jobs.test.ts@@ -32,6 +32,7 @@ function stubJobCtx(auth: {} as Services['auth'],notification: {} as Services['notification'],insight: {} as Services['insight'],+ integration: {} as Services['integration'],gsc: {} as Services['gsc'],import: {} as Services['import'],ingest: {} as Services['ingest'],@@ -43,6 +44,10 @@ function stubJobCtx(profile: {} as Services['profile'],group: {} as Services['group'],chart: {} as Services['chart'],+ funnel: {} as Services['funnel'],+ conversion: {} as Services['conversion'],+ sankey: {} as Services['sankey'],+ retention: {} as Services['retention'],overview: {} as Services['overview'],pages: {} as Services['pages'],realtime: {} as Services['realtime'],diff --git a/packages/core/src/modules/onboarding/onboarding.rpc.ts b/packages/core/src/modules/onboarding/onboarding.rpc.tsindex cc8cbde40..afaab9698 100644--- a/packages/core/src/modules/onboarding/onboarding.rpc.ts+++ b/packages/core/src/modules/onboarding/onboarding.rpc.ts@@ -26,7 +26,7 @@ import {} from './onboarding.service';function loadOrganizationAccess() {- return import('@openpanel/core');+ return import('../../shared/access-lookups');}function requireLogin(userId: string | null | undefined): string {@@ -38,7 +38,7 @@ function requireLogin(userId: string | null | undefined): string {export const onboardingRouter = createTRPCRouter({skipOnboardingCheck: procedure.query(({ ctx }) =>- canSkipOnboarding(ctx.session.userId)+ canSkipOnboarding(ctx, ctx.session.userId)),project: procedure@@ -59,6 +59,6 @@ export const onboardingRouter = createTRPCRouter({}}- return createOnboardingProject(input, userId);+ return createOnboardingProject(ctx, input, userId);}),});diff --git a/packages/core/src/modules/onboarding/onboarding.service.test.ts b/packages/core/src/modules/onboarding/onboarding.service.test.tsindex 7b98604b2..74e09e22b 100644--- a/packages/core/src/modules/onboarding/onboarding.service.test.ts+++ b/packages/core/src/modules/onboarding/onboarding.service.test.ts@@ -6,7 +6,7 @@// personalization. `mock.module` is not hoisted, so the subject is imported// inside `beforeAll` — see AGENTS.md.-import { beforeAll, beforeEach, expect, mock, test } from 'bun:test';+import { afterAll, beforeAll, beforeEach, expect, mock, test } from 'bun:test';import { subDays } from 'date-fns';const organizationFindMany = mock(async () => [] as unknown[]);@@ -14,18 +14,36 @@ const organizationUpdate = mock(async () => ({}));const dbMock = {organization: { findMany: organizationFindMany, update: organizationUpdate },};-mock.module('@openpanel/db/src/prisma-client', () => ({ db: dbMock }));+// M10-009: `runOnboardingCron` takes `ServiceDeps` now, so `deps.db` IS the+// fake below — no Prisma module mock needed.+const deps = { db: dbMock } as unknown as import('../../services').ServiceDeps;-const getOrganizationEventsCount = mock(async (_projectIds: string[]) => 0);+const getOrganizationEventsCount = mock(+ async (_deps: unknown, _projectIds: string[]) => 0+);+// Spread a plain-object SNAPSHOT of the real module, not a partial factory:+// `mock.module` replaces this specifier process-wide under bare `bun test`+// (AGENTS.md), and since M10-009 every cross-module caller reaches+// `getSettingsForProject` / `getOrganizationByProjectIdCached` through this+// deep path rather than the package barrel — a partial factory here silently+// deleted them for every file that ran afterwards.+const realOrganizationService = {+ ...(await import('../organization/organization.service')),+};mock.module('../organization/organization.service', () => ({+ ...realOrganizationService,getOrganizationById: mock(async () => null),getOrganizationEventsCount,- // Unused by `runOnboardingCron`, but `../auth/auth.service` (transitively- // imported for `hashPassword`) statically imports it — a mock module must- // supply every named export the real module's importers destructure.connectUserToOrganization: mock(async () => undefined),}));+afterAll(() => {+ mock.module(+ '../organization/organization.service',+ () => realOrganizationService+ );+});+const sendEmail = mock(async (_template: string,@@ -77,7 +95,7 @@ beforeEach(() => {test('returns null and does nothing when self-hosted', async () => {process.env.SELF_HOSTED = 'true';- const result = await runOnboardingCron(logger);+ const result = await runOnboardingCron(deps, logger);expect(result).toBeNull();expect(organizationFindMany).not.toHaveBeenCalled();});@@ -86,14 +104,14 @@ test('sends the welcome email on day 0 with hasData from clickhouse', async () =getOrganizationEventsCount.mockResolvedValue(123);organizationFindMany.mockResolvedValue([org()]);- const result = await runOnboardingCron(logger);+ const result = await runOnboardingCron(deps, logger);expect(result).toMatchObject({ emailsSent: 1 });expect(sendEmail).toHaveBeenCalledWith('onboarding-welcome', {to: '[email protected]',data: expect.objectContaining({ firstName: 'Alex', hasData: true }),});- expect(getOrganizationEventsCount).toHaveBeenCalledWith(['proj-1']);+ expect(getOrganizationEventsCount).toHaveBeenCalledWith(deps, ['proj-1']);expect(organizationUpdate).toHaveBeenCalledWith({where: { id: 'org-1' },data: { onboarding: 'onboarding-welcome' },@@ -108,7 +126,7 @@ test('does not fetch usage for orgs that are gated on days', async () => {}),]);- const result = await runOnboardingCron(logger);+ const result = await runOnboardingCron(deps, logger);expect(result).toMatchObject({ emailsSent: 0, orgsSkipped: 1 });expect(sendEmail).not.toHaveBeenCalled();@@ -124,7 +142,7 @@ test('sends the no-data branch of what-to-track on day 2', async () => {}),]);- await runOnboardingCron(logger);+ await runOnboardingCron(deps, logger);expect(sendEmail).toHaveBeenCalledWith('onboarding-what-to-track', {to: '[email protected]',@@ -141,7 +159,7 @@ test('completes onboarding when the org subscribed before the trial emails', asy}),]);- const result = await runOnboardingCron(logger);+ const result = await runOnboardingCron(deps, logger);expect(result).toMatchObject({ emailsSent: 0, orgsCompleted: 1 });expect(sendEmail).not.toHaveBeenCalled();@@ -162,7 +180,7 @@ test('populates recommendedPlan and trial stats in the trial-ending email', asyn}),]);- await runOnboardingCron(logger);+ await runOnboardingCron(deps, logger);expect(sendEmail).toHaveBeenCalledWith('onboarding-trial-ending', {to: '[email protected]',@@ -189,7 +207,7 @@ test('marks onboarding completed once every email has been sent', async () => {}),]);- const result = await runOnboardingCron(logger);+ const result = await runOnboardingCron(deps, logger);expect(result).toMatchObject({ emailsSent: 0, orgsCompleted: 1 });expect(organizationUpdate).toHaveBeenCalledWith({@@ -210,7 +228,7 @@ test('completes orgs left on the retired trial-ended pointer', async () => {}),]);- const result = await runOnboardingCron(logger);+ const result = await runOnboardingCron(deps, logger);expect(result).toMatchObject({ emailsSent: 0, orgsCompleted: 1 });expect(sendEmail).not.toHaveBeenCalled();diff --git a/packages/core/src/modules/onboarding/onboarding.service.ts b/packages/core/src/modules/onboarding/onboarding.service.tsindex 3659db332..a200949c9 100644--- a/packages/core/src/modules/onboarding/onboarding.service.ts+++ b/packages/core/src/modules/onboarding/onboarding.service.ts@@ -6,8 +6,11 @@// PATTERN: V1's trpc router and cron job, and this package's own// onboarding.rpc.ts/onboarding.jobs.ts, all call these same functions.//-// db access is LAZY (`loadDb` below), not a static top-level import — see-// user.service.ts's header for the full reasoning.+// M10-009: every exported function takes `ServiceDeps` and reaches Postgres+// as `deps.db`; the `loadDb()` lazy loader is gone, so the requestId minted at+// the edge reaches the query (ADR-018, docs/TECH_DEBT.md §4). V1's still-live+// trpc router and cron job keep calling the deps-free spellings v1-compat.ts+// re-exports.import crypto from 'node:crypto';import { stripTrailingSlash } from '@openpanel/common';@@ -42,22 +45,19 @@ const TRIAL_DURATION_IN_DAYS = 30;// "limit exceeded" (the limit defaults to 0, which trips on the first event).const TRIAL_EVENTS_LIMIT = 10_000_000;-function loadDb() {- return import('@openpanel/db/src/prisma-client').then((m) => m.db);-}-function loadIdService() {- return import('@openpanel/core');+ return import('../../shared/slug-id');}export async function canSkipOnboarding(+ deps: ServiceDeps,userId: string | null | undefined): Promise<{ canSkip: boolean }> {if (!userId) {return { canSkip: false };}- const db = await loadDb();+ const db = deps.db;const members = await db.member.findMany({ where: { userId } });if (members.length > 0) {return { canSkip: true };@@ -70,11 +70,12 @@ export async function canSkipOnboarding(}async function createOrGetOnboardingOrganization(+ deps: ServiceDeps,input: IOnboardingProject,userId: string) {if (input.organizationId) {- return await getOrganizationById(input.organizationId);+ return await getOrganizationById(deps, input.organizationId);}if (!input.organization) {@@ -82,8 +83,8 @@ async function createOrGetOnboardingOrganization(}const { getId } = await loadIdService();- const db = await loadDb();- const organizationId = await getId('organization', input.organization);+ const db = deps.db;+ const organizationId = await getId(deps, 'organization', input.organization);// Create the organization and its owner (org:admin member) atomically. The// `delete` cron treats an organization with no org:admin member as ownerless@@ -126,6 +127,7 @@ export interface CreateOnboardingProjectResult {}export async function createOnboardingProject(+ deps: ServiceDeps,input: IOnboardingProject,userId: string): Promise<CreateOnboardingProjectResult> {@@ -140,7 +142,11 @@ export async function createOnboardingProject(types.push('backend');}- const organization = await createOrGetOnboardingOrganization(input, userId);+ const organization = await createOrGetOnboardingOrganization(+ deps,+ input,+ userId+ );if (!organization?.id) {throw new Error('Organization slug is missing');}@@ -151,11 +157,11 @@ export async function createOnboardingProject(}const { getId } = await loadIdService();- const db = await loadDb();+ const db = deps.db;const project = await db.project.create({data: {- id: await getId('project', input.project),+ id: await getId(deps, 'project', input.project),name: input.project,organizationId: organization.id,types,@@ -216,10 +222,11 @@ interface OnboardingContext {getUsage: () => Promise<OnboardingUsage>;}-function createUsageGetter(org: OnboardingCronOrg) {+function createUsageGetter(deps: ServiceDeps, org: OnboardingCronOrg) {let promise: Promise<OnboardingUsage> | null = null;return () => {promise ??= getOrganizationEventsCount(+ deps,org.projects.map((project) => project.id)).then((eventsCount) => ({eventsCount,@@ -333,6 +340,7 @@ export interface OnboardingCronSummary {}export async function runOnboardingCron(+ deps: ServiceDeps,logger: OnboardingCronLogger): Promise<OnboardingCronSummary | null> {if (process.env.SELF_HOSTED === 'true') {@@ -341,7 +349,7 @@ export async function runOnboardingCron(logger.info('Starting onboarding email job');- const db = await loadDb();+ const db = deps.db;const orgs: OnboardingCronOrg[] = await db.organization.findMany({where: {OR: [{ onboarding: null }, { onboarding: { notIn: ['completed'] } }],@@ -371,7 +379,7 @@ export async function runOnboardingCron(email: user.email,anchor: org.createdAt,pointer: org.onboarding,- ctx: { org, user, getUsage: createUsageGetter(org) },+ ctx: { org, user, getUsage: createUsageGetter(deps, org) },};});@@ -417,6 +425,6 @@ export interface OnboardingService {* logger of its own at all. */export function createOnboardingService(deps: ServiceDeps): OnboardingService {return {- runOnboardingCron: () => runOnboardingCron(deps.logger),+ runOnboardingCron: () => runOnboardingCron(deps, deps.logger),};}diff --git a/packages/core/src/modules/organization/organization.jobs.test.ts b/packages/core/src/modules/organization/organization.jobs.test.tsindex f1c640087..ca9ab04e0 100644--- a/packages/core/src/modules/organization/organization.jobs.test.ts+++ b/packages/core/src/modules/organization/organization.jobs.test.ts@@ -32,6 +32,7 @@ function stubJobCtx(auth: {} as Services['auth'],notification: {} as Services['notification'],insight: {} as Services['insight'],+ integration: {} as Services['integration'],gsc: {} as Services['gsc'],import: {} as Services['import'],ingest: {} as Services['ingest'],@@ -43,6 +44,10 @@ function stubJobCtx(profile: {} as Services['profile'],group: {} as Services['group'],chart: {} as Services['chart'],+ funnel: {} as Services['funnel'],+ conversion: {} as Services['conversion'],+ sankey: {} as Services['sankey'],+ retention: {} as Services['retention'],overview: {} as Services['overview'],pages: {} as Services['pages'],realtime: {} as Services['realtime'],diff --git a/packages/core/src/modules/organization/organization.jobs.ts b/packages/core/src/modules/organization/organization.jobs.tsindex 4167c833a..108d0683e 100644--- a/packages/core/src/modules/organization/organization.jobs.ts+++ b/packages/core/src/modules/organization/organization.jobs.ts@@ -75,8 +75,10 @@ async function windDownDeps(ctx: Ctx, logger: Logger): Promise<WindDownDeps> {logger,sendEmail: ctx.clients.email.sendEmail,getLastEventPerProject: () => ctx.services.project.getLastEventPerProject(),- getOrganizationEventsCount,- getOrganizationEventsCountSince,+ getOrganizationEventsCount: (projectIds) =>+ getOrganizationEventsCount(ctx, projectIds),+ getOrganizationEventsCountSince: (projectIds, since) =>+ getOrganizationEventsCountSince(ctx, projectIds, since),buildHighlight: (input) => buildWinBackHighlight(input, highlight),};}diff --git a/packages/core/src/modules/organization/organization.service.test.ts b/packages/core/src/modules/organization/organization.service.test.tsindex 744732fa9..812266bfb 100644--- a/packages/core/src/modules/organization/organization.service.test.ts+++ b/packages/core/src/modules/organization/organization.service.test.ts@@ -394,9 +394,14 @@ const projectAccess = {const $transaction = mock(async (ops: Promise<unknown>[]) => Promise.all(ops));-const actualPrismaClient = await import('@openpanel/db/src/prisma-client');-mock.module('@openpanel/db/src/prisma-client', () => ({- ...actualPrismaClient,+// M10-009: every function under test takes `ServiceDeps`, so `deps.db` and+// `deps.ch` ARE the fakes below — the two `@openpanel/db` module mocks this+// file used to install are gone, and with them the process-wide leak they had+// to be restored from. `connectUserToOrganization` reaches+// `shared/access-lookups.ts` (cacheable, via the v1-compat seam), which is why+// the `@openpanel/redis` stand-in below stays.+const chCommand = mock(async () => undefined);+const deps = {db: {organization,project,@@ -406,21 +411,8 @@ mock.module('@openpanel/db/src/prisma-client', () => ({projectAccess,$transaction,},-}));--// Spread the real module rather than hand-listing every export — see-// gsc.service.test.ts's header for why a partial factory here is a-// process-wide hazard, not a local one (bun shares one module registry-// across files without `--isolate`).-const actualClickhouseClient = await import(- '@openpanel/db/src/clickhouse/client'-);-const realClickhouseClient = { ...actualClickhouseClient };-const chCommand = mock(async () => undefined);-mock.module('@openpanel/db/src/clickhouse/client', () => ({- ...realClickhouseClient,ch: { command: chCommand },-}));+} as unknown as import('../../services').ServiceDeps;// Bypasses the Redis cache-aside entirely — this module's own logic is// exercised directly, caching is @openpanel/redis's concern. `access.service.ts`@@ -461,10 +453,6 @@ mock.module('@openpanel/redis', () => ({}));afterAll(() => {- mock.module(- '@openpanel/db/src/clickhouse/client',- () => realClickhouseClient- );mock.module('@openpanel/redis', () => realRedis);});@@ -502,7 +490,7 @@ test('scheduleOrganizationDeletion sets deleteAt on the org and its projects', adeleteAt: null,});- await subject.scheduleOrganizationDeletion('org_1');+ await subject.scheduleOrganizationDeletion(deps, 'org_1');expect(organizationStore.get('org_1')?.deleteAt).toBeInstanceOf(Date);expect(projectStore.get('proj_1')?.deleteAt).toBeInstanceOf(Date);@@ -518,9 +506,9 @@ test('scheduleOrganizationDeletion refuses a live, uncancelled subscription', as}));- await expect(subject.scheduleOrganizationDeletion('org_1')).rejects.toThrow(- /cancel your subscription/- );+ await expect(+ subject.scheduleOrganizationDeletion(deps, 'org_1')+ ).rejects.toThrow(/cancel your subscription/);});test('scheduleOrganizationDeletion allows a subscription already scheduled to cancel', async () => {@@ -533,7 +521,7 @@ test('scheduleOrganizationDeletion allows a subscription already scheduled to ca}));- await subject.scheduleOrganizationDeletion('org_1');+ await subject.scheduleOrganizationDeletion(deps, 'org_1');expect(organizationStore.get('org_1')?.deleteAt).toBeInstanceOf(Date);});@@ -548,7 +536,7 @@ test('cancelOrganizationDeletion clears deleteAt on the org and its projects', adeleteAt: new Date(),});- await subject.cancelOrganizationDeletion('org_1');+ await subject.cancelOrganizationDeletion(deps, 'org_1');expect(organizationStore.get('org_1')?.deleteAt).toBeNull();expect(projectStore.get('proj_1')?.deleteAt).toBeNull();@@ -556,7 +544,7 @@ test('cancelOrganizationDeletion clears deleteAt on the org and its projects', atest('updateOrganization updates name and timezone', async () => {organizationStore.set('org_1', makeOrganization({ id: 'org_1' }));- const result = await subject.updateOrganization({+ const result = await subject.updateOrganization(deps, {id: 'org_1',name: 'New name',timezone: 'Europe/Stockholm',@@ -577,7 +565,7 @@ test('removeOrganizationMember refuses a user removing themself as the last memb});await expect(- subject.removeOrganizationMember({+ subject.removeOrganizationMember(deps, {organizationId: 'org_1',memberId: 'member_1',targetUserId: 'user_1',@@ -603,7 +591,7 @@ test('removeOrganizationMember deletes the member and their project access', asylevel: 'write',});- await subject.removeOrganizationMember({+ await subject.removeOrganizationMember(deps, {organizationId: 'org_1',memberId: 'member_1',targetUserId: 'user_1',@@ -623,7 +611,7 @@ test('updateOrganizationMemberAccess replaces project access with the given granlevel: 'read',});- await subject.updateOrganizationMemberAccess({+ await subject.updateOrganizationMemberAccess(deps, {organizationId: 'org_1',targetUserId: 'user_1',access: [{ projectId: 'proj_new', level: 'write' }],@@ -645,7 +633,7 @@ test('inviteUserToOrganization refuses when the email is already a member', asyn});await expect(- subject.inviteUserToOrganization({+ subject.inviteUserToOrganization(deps, {organizationId: 'org_1',email: '[email protected]',role: 'org:member',@@ -668,7 +656,7 @@ test('inviteUserToOrganization refuses a duplicate pending invite', async () =>});await expect(- subject.inviteUserToOrganization({+ subject.inviteUserToOrganization(deps, {organizationId: 'org_1',email: '[email protected]',role: 'org:member',@@ -681,7 +669,7 @@ test('inviteUserToOrganization refuses a duplicate pending invite', async () =>test('inviteUserToOrganization connects an existing user immediately, with no email sent', async () => {userStore.set('user_2', { id: 'user_2', email: '[email protected]' });- const result = await subject.inviteUserToOrganization({+ const result = await subject.inviteUserToOrganization(deps, {organizationId: 'org_1',email: '[email protected]',role: 'org:member',@@ -694,7 +682,7 @@ test('inviteUserToOrganization connects an existing user immediately, with no em});test('inviteUserToOrganization emails a new user and leaves the invite pending', async () => {- const result = await subject.inviteUserToOrganization({+ const result = await subject.inviteUserToOrganization(deps, {organizationId: 'org_1',email: '[email protected]',role: 'org:member',@@ -722,7 +710,7 @@ test('revokeInvite deletes the invite row', async () => {createdAt: EPOCH,});- await subject.revokeInvite('invite_1');+ await subject.revokeInvite(deps, 'invite_1');expect(inviteStore.has('invite_1')).toBe(false);});@@ -757,7 +745,7 @@ test('runDeleteCron sweeps orphaned and scheduled-for-deletion organizations anddeleteAt: new Date(Date.now() - 1000),});- const result = await subject.runDeleteCron();+ const result = await subject.runDeleteCron(deps);expect(result).toEqual({ organizations: 1, projects: 2 });expect(organizationStore.has('org_orphaned')).toBe(false);@@ -777,7 +765,7 @@ test('runDeleteCron is a no-op when nothing is due', async () => {email: '[email protected]',});- const result = await subject.runDeleteCron();+ const result = await subject.runDeleteCron(deps);expect(result).toEqual({ organizations: 0, projects: 0 });expect(organizationStore.has('org_healthy')).toBe(true);});@@ -787,6 +775,6 @@ test('getSettingsForOrganization falls back to UTC when the organization has no'org_1',makeOrganization({ id: 'org_1', timezone: null }));- const result = await subject.getSettingsForOrganization('org_1');+ const result = await subject.getSettingsForOrganization(deps, 'org_1');expect(result).toEqual({ timezone: 'UTC' });});diff --git a/packages/core/src/modules/organization/organization.service.ts b/packages/core/src/modules/organization/organization.service.tsindex a6f2b9322..24a73dddf 100644--- a/packages/core/src/modules/organization/organization.service.ts+++ b/packages/core/src/modules/organization/organization.service.ts@@ -10,16 +10,20 @@// delete.service.ts entirely — nothing outside apps/worker's cron job// (now delegating here) reached it through @openpanel/db's barrel.//-// db/ch access is LAZY (`load*` below), not a static top-level import — see-// insight.service.ts's header for the full reasoning (jobs.registry.ts and-// services.ts pull this module into the eager barrel chain nearly every core-// test file reaches, and constructing @openpanel/db's clients at import time-// would spawn a pino-pretty transport worker thread per test file).+// M10-009: every exported function takes `ServiceDeps` and reaches Postgres+// as `deps.db` and ClickHouse as `deps.ch` (through core's own `chQuery`), so+// the requestId minted at the edge reaches the query (ADR-018,+// docs/TECH_DEBT.md §4). The three `cacheable` wrappers are the exception:+// `cacheable` keys on the call's ARGUMENTS (packages/redis/cachable.ts), so+// `deps` cannot be a leading parameter and they reach the boot scope through+// the declared v1-compat seam instead.//// ClickHouse queries here still go through raw sqlstring-escaped strings and// the (still-live, pre-ADR-013) `createSqlBuilder`, not the `sql` tag:// ADR-013 converts the analytics read path one query per P7 task, and this-// module's queries haven't been converted yet.+// module's queries haven't been converted yet. `createSqlBuilder` stays a+// value import of `@openpanel/db`: like ADR-013's `sql` tag and `clix`, it is+// a pure string BUILDER that holds no client (see shared/ch-query.ts).import { DateTime } from '@openpanel/common';import type {@@ -28,11 +32,18 @@ import type {ProjectAccess,User,} from '@openpanel/db/src/prisma-client';+import { createSqlBuilder } from '@openpanel/db/src/sql-builder';import { cacheable } from '@openpanel/redis';import sqlstring from 'sqlstring';import { sendEmail } from '../../clients/email';import { TRPCBadRequestError } from '../../rpc/errors';import type { ServiceDeps } from '../../services';+import { formatClickhouseDate } from '../../shared/ch-dates';+import { chQuery } from '../../shared/ch-query';+import {+ getReplicatedTableName,+ TABLE_NAMES,+} from '../../shared/ch-tables';import { generateSecureId } from '../../shared/id';export type IServiceOrganization = Awaited<@@ -47,30 +58,31 @@ export type IServiceProjectAccess = ProjectAccess;const DEFAULT_TIMEZONE = 'UTC';// Grace period between a scheduled deletion and the `delete` cron sweeping it// up — matches V1's `addHours(new Date(), 24)`.+/** A module function's parameters with its leading `ServiceDeps` dropped. */+type Tail<T extends unknown[]> = T extends [unknown, ...infer Rest]+ ? Rest+ : never;+const DELETE_GRACE_PERIOD_MS = 24 * 60 * 60 * 1000;// Invite link lifetime — matches V1's `addDays(new Date(), 3)`.const INVITE_EXPIRY_MS = 3 * 24 * 60 * 60 * 1000;-function loadDb() {- return import('@openpanel/db/src/prisma-client').then((m) => m.db);-}--function loadChClient() {- return import('@openpanel/db/src/clickhouse/client');-}--function loadSqlBuilder() {- return import('@openpanel/db/src/sql-builder').then(- (m) => m.createSqlBuilder- );+/** The three `cacheable` wrappers below cannot carry `ServiceDeps` — see the+ * header. GENUINE CYCLE, kept lazy: services.ts -> organization.service.ts+ * (this file) -> v1-compat.ts -> services.ts. */+function loadCompatServiceDeps() {+ return import('../../v1-compat').then((m) => m.compatServiceDeps());}-export async function getOrganizations(userId: string | null) {+export async function getOrganizations(+ deps: ServiceDeps,+ userId: string | null+) {if (!userId) {return [];}- const db = await loadDb();+ const db = deps.db;return db.organization.findMany({where: {members: {@@ -85,8 +97,11 @@ export async function getOrganizations(userId: string | null) {});}-export async function getOrganizationById(organizationId: string) {- const db = await loadDb();+export async function getOrganizationById(+ deps: ServiceDeps,+ organizationId: string+) {+ const db = deps.db;return db.organization.findUniqueOrThrow({where: {id: organizationId,@@ -94,8 +109,11 @@ export async function getOrganizationById(organizationId: string) {});}-export async function getOrganizationByProjectId(projectId: string) {- const db = await loadDb();+export async function getOrganizationByProjectId(+ deps: ServiceDeps,+ projectId: string+) {+ const db = deps.db;const project = await db.project.findUniqueOrThrow({where: {id: projectId,@@ -114,12 +132,17 @@ export async function getOrganizationByProjectId(projectId: string) {const ORGANIZATION_BY_PROJECT_CACHE_TTL_SEC = 60 * 5;export const getOrganizationByProjectIdCached = cacheable(- getOrganizationByProjectId,+ 'getOrganizationByProjectId',+ async (projectId: string) =>+ getOrganizationByProjectId(await loadCompatServiceDeps(), projectId),ORGANIZATION_BY_PROJECT_CACHE_TTL_SEC);-export async function getInvites(organizationId: string): Promise<Invite[]> {- const db = await loadDb();+export async function getInvites(+ deps: ServiceDeps,+ organizationId: string+): Promise<Invite[]> {+ const db = deps.db;return db.invite.findMany({where: {organizationId,@@ -130,8 +153,8 @@ export async function getInvites(organizationId: string): Promise<Invite[]> {});}-export async function getInviteById(inviteId: string) {- const db = await loadDb();+export async function getInviteById(deps: ServiceDeps, inviteId: string) {+ const db = deps.db;const res = await db.invite.findUnique({where: {id: inviteId,@@ -153,9 +176,10 @@ export async function getInviteById(inviteId: string) {}export async function getMembers(+ deps: ServiceDeps,organizationId: string): Promise<IServiceMember[]> {- const db = await loadDb();+ const db = deps.db;const [members, access] = await Promise.all([db.member.findMany({where: {@@ -181,8 +205,12 @@ export async function getMembers(})) as IServiceMember[];}-export async function getMember(organizationId: string, userId: string) {- const db = await loadDb();+export async function getMember(+ deps: ServiceDeps,+ organizationId: string,+ userId: string+) {+ const db = deps.db;return db.member.findFirst({where: {organizationId,@@ -191,16 +219,18 @@ export async function getMember(organizationId: string, userId: string) {});}-export async function connectUserToOrganization({+export async function connectUserToOrganization(+ deps: ServiceDeps,+ {user,inviteId,}: {user: User;inviteId: string;}) {- const db = await loadDb();+ const db = deps.db;const { getOrganizationAccess, getProjectAccess } = await import(- '@openpanel/core'+ '../../shared/access-lookups');// Use primary since before this we might have just created the invite@@ -282,6 +312,7 @@ export async function connectUserToOrganization({* current subscription period for an organization*/export async function getOrganizationBillingEventsCount(+ deps: ServiceDeps,organization: IServiceOrganization & { projects: { id: string }[] }): Promise<number | undefined> {// Trials have no Polar billing period; fall back to the trial window@@ -298,8 +329,6 @@ export async function getOrganizationBillingEventsCount(return 0;}- const { chQuery, formatClickhouseDate } = await loadChClient();- const createSqlBuilder = await loadSqlBuilder();const { sb, getSql } = createSqlBuilder();sb.select.count = 'COUNT(*) AS count';@@ -307,7 +336,7 @@ export async function getOrganizationBillingEventsCount(sb.where.createdAt = `created_at BETWEEN ${sqlstring.escape(formatClickhouseDate(periodStart))} AND ${sqlstring.escape(formatClickhouseDate(periodEnd))}`;sb.where.names = `name NOT IN ('session_start', 'session_end')`;- const res = await chQuery<{ count: number }>(getSql());+ const res = await chQuery<{ count: number }>(deps, getSql());return res[0]?.count;}@@ -315,21 +344,20 @@ export async function getOrganizationBillingEventsCount(// events). The onboarding emails use this instead of subscriptionPeriodEventsCount,// which only refreshes when sessions end.export async function getOrganizationEventsCount(+ deps: ServiceDeps,projectIds: string[]): Promise<number> {if (projectIds.length === 0) {return 0;}- const { chQuery } = await loadChClient();- const createSqlBuilder = await loadSqlBuilder();const { sb, getSql } = createSqlBuilder();sb.select.count = 'COUNT(*) AS count';sb.where.projectIds = `project_id IN (${projectIds.map((id) => sqlstring.escape(id)).join(',')})`;sb.where.names = `name NOT IN ('session_start', 'session_end')`;- const res = await chQuery<{ count: number }>(getSql());+ const res = await chQuery<{ count: number }>(deps, getSql());return res[0]?.count ?? 0;}@@ -338,6 +366,7 @@ export async function getOrganizationEventsCount(// one says "you are using this right now", which is the only number that// actually argues for a subscription.export async function getOrganizationEventsCountSince(+ deps: ServiceDeps,projectIds: string[],since: Date): Promise<number> {@@ -345,8 +374,6 @@ export async function getOrganizationEventsCountSince(return 0;}- const { chQuery, formatClickhouseDate } = await loadChClient();- const createSqlBuilder = await loadSqlBuilder();const { sb, getSql } = createSqlBuilder();sb.select.count = 'COUNT(*) AS count';@@ -354,11 +381,12 @@ export async function getOrganizationEventsCountSince(sb.where.names = `name NOT IN ('session_start', 'session_end')`;sb.where.createdAt = `created_at >= ${sqlstring.escape(formatClickhouseDate(since, true))}`;- const res = await chQuery<{ count: number }>(getSql());+ const res = await chQuery<{ count: number }>(deps, getSql());return res[0]?.count ?? 0;}export async function getOrganizationBillingEventsCountSerie(+ deps: ServiceDeps,organization: IServiceOrganization & { projects: { id: string }[] },{startDate,@@ -369,8 +397,6 @@ export async function getOrganizationBillingEventsCountSerie(}): Promise<{ count: number; day: string }[]> {const interval = 'day';- const { chQuery, formatClickhouseDate } = await loadChClient();- const createSqlBuilder = await loadSqlBuilder();const { sb, getSql } = createSqlBuilder();sb.select.count = 'COUNT(*) AS count';@@ -381,16 +407,24 @@ export async function getOrganizationBillingEventsCountSerie(sb.where.createdAt = `${interval} BETWEEN ${sqlstring.escape(formatClickhouseDate(startDate, true))} AND ${sqlstring.escape(formatClickhouseDate(endDate, true))}`;sb.where.names = `name NOT IN ('session_start', 'session_end')`;- return chQuery<{ count: number; day: string }>(getSql());+ return chQuery<{ count: number; day: string }>(deps, getSql());}const BILLING_EVENTS_SERIE_CACHE_TTL_SEC = 60 * 10;export const getOrganizationBillingEventsCountSerieCached = cacheable(- getOrganizationBillingEventsCountSerie,+ 'getOrganizationBillingEventsCountSerie',+ async (+ ...args: Tail<Parameters<typeof getOrganizationBillingEventsCountSerie>>+ ) =>+ getOrganizationBillingEventsCountSerie(+ await loadCompatServiceDeps(),+ ...args+ ),BILLING_EVENTS_SERIE_CACHE_TTL_SEC);export async function getOrganizationSubscriptionChartEndDate(+ deps: ServiceDeps,projectId: string,endDate: string): Promise<string | null> {@@ -412,9 +446,10 @@ export async function getOrganizationSubscriptionChartEndDate(}export async function getSettingsForOrganization(+ deps: ServiceDeps,organizationId: string): Promise<{ timezone: string }> {- const db = await loadDb();+ const db = deps.db;const organization = await db.organization.findUniqueOrThrow({where: {id: organizationId,@@ -427,9 +462,10 @@ export async function getSettingsForOrganization(}export async function getSettingsForProject(+ deps: ServiceDeps,projectId: string): Promise<{ timezone: string }> {- const db = await loadDb();+ const db = deps.db;const project = await db.project.findUniqueOrThrow({where: {id: projectId,@@ -446,8 +482,11 @@ export async function getSettingsForProject(// --- Moved from packages/db/src/services/delete.service.ts ----export async function deleteOrganization(organizationId: string) {- const db = await loadDb();+export async function deleteOrganization(+ deps: ServiceDeps,+ organizationId: string+) {+ const db = deps.db;return db.organization.delete({where: {id: organizationId,@@ -455,8 +494,8 @@ export async function deleteOrganization(organizationId: string) {});}-export async function deleteProjects(projectIds: string[]) {- const db = await loadDb();+export async function deleteProjects(deps: ServiceDeps, projectIds: string[]) {+ const db = deps.db;const projects = await db.project.findMany({where: {id: {@@ -480,8 +519,11 @@ export async function deleteProjects(projectIds: string[]) {return projects;}-export async function deleteFromClickhouse(projectIds: string[]) {- const { TABLE_NAMES, ch, getReplicatedTableName } = await loadChClient();+export async function deleteFromClickhouse(+ deps: ServiceDeps,+ projectIds: string[]+) {+ const ch = deps.ch;const where = `project_id IN (${projectIds.map((projectId) => sqlstring.escape(projectId)).join(',')})`;const tables = [TABLE_NAMES.events,@@ -527,8 +569,10 @@ export interface DeleteCronResult {* projects individually scheduled for deletion, then sweeps both out of* ClickHouse and Postgres in one pass.*/-export async function runDeleteCron(): Promise<DeleteCronResult> {- const db = await loadDb();+export async function runDeleteCron(+ deps: ServiceDeps+): Promise<DeleteCronResult> {+ const db = deps.db;const now = new Date();// Find orphaned organizations (no admin member)@@ -565,12 +609,12 @@ export async function runDeleteCron(): Promise<DeleteCronResult> {];if (projectIds.length > 0) {- await deleteFromClickhouse(projectIds);- await deleteProjects(projectIds);+ await deleteFromClickhouse(deps, projectIds);+ await deleteProjects(deps, projectIds);}for (const organization of deletableOrganizations) {- await deleteOrganization(organization.id);+ await deleteOrganization(deps, organization.id);}return {@@ -581,12 +625,14 @@ export async function runDeleteCron(): Promise<DeleteCronResult> {// --- Moved from packages/trpc/src/routers/organization.ts's inline bodies ----export async function updateOrganization(input: {+export async function updateOrganization(+ deps: ServiceDeps,+ input: {id: string;name: string;timezone: string;}) {- const db = await loadDb();+ const db = deps.db;return db.organization.update({where: {id: input.id,@@ -605,10 +651,11 @@ export async function updateOrganization(input: {* pass once their `deleteAt` has passed.*/export async function scheduleOrganizationDeletion(+ deps: ServiceDeps,organizationId: string): Promise<void> {- const db = await loadDb();- const organization = await getOrganizationById(organizationId);+ const db = deps.db;+ const organization = await getOrganizationById(deps, organizationId);// Require billing to be cancelled first. We don't want to delete an// organization that still has a live paid subscription. Once the user has@@ -641,9 +688,10 @@ export async function scheduleOrganizationDeletion(}export async function cancelOrganizationDeletion(+ deps: ServiceDeps,organizationId: string): Promise<void> {- const db = await loadDb();+ const db = deps.db;await db.$transaction([db.project.updateMany({where: {@@ -674,14 +722,16 @@ export type InviteUserResult =invite: Invite & { organization: { name: string } };};-export async function inviteUserToOrganization(input: {+export async function inviteUserToOrganization(+ deps: ServiceDeps,+ input: {organizationId: string;email: string;role: 'org:admin' | 'org:member';access: { projectId: string; level: 'read' | 'write' }[];invitedById: string;}): Promise<InviteUserResult> {- const db = await loadDb();+ const db = deps.db;const email = input.email.toLowerCase();const userExists = await db.user.findFirst({where: {@@ -738,7 +788,7 @@ export async function inviteUserToOrganization(input: {});if (userExists) {- const member = await connectUserToOrganization({+ const member = await connectUserToOrganization(deps, {user: userExists,inviteId: invite.id,});@@ -763,8 +813,11 @@ export async function inviteUserToOrganization(input: {};}-export async function getInviteOrThrow(inviteId: string): Promise<Invite> {- const db = await loadDb();+export async function getInviteOrThrow(+ deps: ServiceDeps,+ inviteId: string+): Promise<Invite> {+ const db = deps.db;return db.invite.findUniqueOrThrow({where: {id: inviteId,@@ -772,8 +825,11 @@ export async function getInviteOrThrow(inviteId: string): Promise<Invite> {});}-export async function revokeInvite(inviteId: string): Promise<Invite> {- const db = await loadDb();+export async function revokeInvite(+ deps: ServiceDeps,+ inviteId: string+): Promise<Invite> {+ const db = deps.db;return db.invite.delete({where: {id: inviteId,@@ -781,13 +837,15 @@ export async function revokeInvite(inviteId: string): Promise<Invite> {});}-export async function removeOrganizationMember(input: {+export async function removeOrganizationMember(+ deps: ServiceDeps,+ input: {organizationId: string;memberId: string;targetUserId: string;requestedByUserId: string;}): Promise<void> {- const db = await loadDb();+ const db = deps.db;const exists = await db.member.count({where: {userId: input.targetUserId,@@ -816,12 +874,14 @@ export async function removeOrganizationMember(input: {]);}-export async function updateOrganizationMemberAccess(input: {+export async function updateOrganizationMemberAccess(+ deps: ServiceDeps,+ input: {organizationId: string;targetUserId: string;access: { projectId: string; level: 'read' | 'write' }[];}) {- const db = await loadDb();+ const db = deps.db;return db.$transaction([db.projectAccess.deleteMany({where: {@@ -854,15 +914,15 @@ export interface OrganizationService {scheduleDeletion(organizationId: string): Promise<void>;cancelDeletion(organizationId: string): Promise<void>;inviteUser(- input: Parameters<typeof inviteUserToOrganization>[0]+ input: Parameters<typeof inviteUserToOrganization>[1]): Promise<InviteUserResult>;getInviteOrThrow(inviteId: string): Promise<Invite>;revokeInvite(inviteId: string): Promise<Invite>;removeMember(- input: Parameters<typeof removeOrganizationMember>[0]+ input: Parameters<typeof removeOrganizationMember>[1]): Promise<void>;updateMemberAccess(- input: Parameters<typeof updateOrganizationMemberAccess>[0]+ input: Parameters<typeof updateOrganizationMemberAccess>[1]): ReturnType<typeof updateOrganizationMemberAccess>;members(organizationId: string): Promise<IServiceMember[]>;invitations(organizationId: string): Promise<Invite[]>;@@ -871,22 +931,24 @@ export interface OrganizationService {}export function createOrganizationService(- _deps: ServiceDeps+ deps: ServiceDeps): OrganizationService {return {- get: getOrganizationById,- list: getOrganizations,- update: updateOrganization,- scheduleDeletion: scheduleOrganizationDeletion,- cancelDeletion: cancelOrganizationDeletion,- inviteUser: inviteUserToOrganization,- getInviteOrThrow,- revokeInvite,- removeMember: removeOrganizationMember,- updateMemberAccess: updateOrganizationMemberAccess,- members: getMembers,- invitations: getInvites,- getInvite: getInviteById,- runDeleteCron,+ get: (organizationId) => getOrganizationById(deps, organizationId),+ list: (userId) => getOrganizations(deps, userId),+ update: (input) => updateOrganization(deps, input),+ scheduleDeletion: (organizationId) =>+ scheduleOrganizationDeletion(deps, organizationId),+ cancelDeletion: (organizationId) =>+ cancelOrganizationDeletion(deps, organizationId),+ inviteUser: (input) => inviteUserToOrganization(deps, input),+ getInviteOrThrow: (inviteId) => getInviteOrThrow(deps, inviteId),+ revokeInvite: (inviteId) => revokeInvite(deps, inviteId),+ removeMember: (input) => removeOrganizationMember(deps, input),+ updateMemberAccess: (input) => updateOrganizationMemberAccess(deps, input),+ members: (organizationId) => getMembers(deps, organizationId),+ invitations: (organizationId) => getInvites(deps, organizationId),+ getInvite: (inviteId) => getInviteById(deps, inviteId),+ runDeleteCron: () => runDeleteCron(deps),};}diff --git a/packages/core/src/modules/overview/overview.rpc.ts b/packages/core/src/modules/overview/overview.rpc.tsindex daf702fd4..3be3fd9ac 100644--- a/packages/core/src/modules/overview/overview.rpc.ts+++ b/packages/core/src/modules/overview/overview.rpc.ts@@ -11,10 +11,6 @@// router; they moved to `services.overview.getLiveData` (src/overview.sql.ts)// so every query this module runs goes through the same `sql` tag (M7-005).-import {- getChartPrevStartEndDate,- getChartStartEndDate,-} from '@openpanel/core';import {type IChartRange,pageContextSchema,@@ -24,6 +20,11 @@ import { format } from 'date-fns';import { z } from 'zod';import { createTRPCRouter, procedure, type TrpcContext } from '../../rpc/base';import { TRPCAccessError, TRPCForbiddenError } from '../../rpc/errors';+import type { ServiceDeps } from '../../services';+import {+ getChartPrevStartEndDate,+ getChartStartEndDate,+} from '../../shared/date';import { getConversionEventNames } from '../event/event.service';import { getReferrerSpikes } from '../insight/insight.service';import {@@ -112,7 +113,7 @@ function getCurrentAndPrevious<range: IChartRange;projectId: string;},->(input: T, fetchPrevious: boolean, timezone: string) {+>(input: T, fetchPrevious: boolean, timezone: string, ctx: ServiceDeps) {const current = getChartStartEndDate(input, timezone);const previous = getChartPrevStartEndDate(current);@@ -123,6 +124,7 @@ function getCurrentAndPrevious<previous: R | null;}> => {const endDate = await getOrganizationSubscriptionChartEndDate(+ ctx,input.projectId,current.endDate);@@ -178,11 +180,12 @@ export const overviewRouter = createTRPCRouter({})).query(async ({ input, ctx }) => {- const { timezone } = await getSettingsForProject(input.projectId);+ const { timezone } = await getSettingsForProject(ctx, input.projectId);const { current, previous } = await getCurrentAndPrevious({ ...input, timezone },true,- timezone+ timezone,+ ctx)(ctx.services.overview.getMetrics);return {metrics: {@@ -222,10 +225,10 @@ export const overviewRouter = createTRPCRouter({shareId: z.string().optional(),}))- .query(async ({ input }) => {- const { timezone } = await getSettingsForProject(input.projectId);+ .query(async ({ input, ctx }) => {+ const { timezone } = await getSettingsForProject(ctx, input.projectId);const { startDate, endDate } = getChartStartEndDate(input, timezone);- const clusters = await getReferrerSpikes({+ const clusters = await getReferrerSpikes(ctx, {projectId: input.projectId,filters: input.filters,interval: input.interval,@@ -257,11 +260,12 @@ export const overviewRouter = createTRPCRouter({})).query(async ({ input, ctx }) => {- const { timezone } = await getSettingsForProject(input.projectId);+ const { timezone } = await getSettingsForProject(ctx, input.projectId);const { current } = await getCurrentAndPrevious({ ...input },false,- timezone+ timezone,+ ctx)(async (input) => {if (input.mode === 'page') {return ctx.services.overview.getTopPages({ ...input, timezone });@@ -291,11 +295,12 @@ export const overviewRouter = createTRPCRouter({})).query(async ({ input, ctx }) => {- const { timezone } = await getSettingsForProject(input.projectId);+ const { timezone } = await getSettingsForProject(ctx, input.projectId);const { current } = await getCurrentAndPrevious({ ...input, timezone },false,- timezone+ timezone,+ ctx)(ctx.services.overview.getTopGeneric);return current;@@ -313,11 +318,12 @@ export const overviewRouter = createTRPCRouter({})).query(async ({ input, ctx }) => {- const { timezone } = await getSettingsForProject(input.projectId);+ const { timezone } = await getSettingsForProject(ctx, input.projectId);const { current } = await getCurrentAndPrevious({ ...input, timezone },false,- timezone+ timezone,+ ctx)(ctx.services.overview.getTopGenericSeries);return current;@@ -334,11 +340,12 @@ export const overviewRouter = createTRPCRouter({})).query(async ({ input, ctx }) => {- const { timezone } = await getSettingsForProject(input.projectId);+ const { timezone } = await getSettingsForProject(ctx, input.projectId);const { current } = await getCurrentAndPrevious({ ...input, timezone },false,- timezone+ timezone,+ ctx)(async (input) => {return ctx.services.overview.getUserJourney({...input,@@ -360,11 +367,12 @@ export const overviewRouter = createTRPCRouter({})).query(async ({ input, ctx }) => {- const { timezone } = await getSettingsForProject(input.projectId);+ const { timezone } = await getSettingsForProject(ctx, input.projectId);const { current } = await getCurrentAndPrevious({ ...input, timezone },false,- timezone+ timezone,+ ctx)(ctx.services.overview.getTopEvents);return current;@@ -391,11 +399,12 @@ export const overviewRouter = createTRPCRouter({})).query(async ({ input, ctx }) => {- const { timezone } = await getSettingsForProject(input.projectId);+ const { timezone } = await getSettingsForProject(ctx, input.projectId);const { current } = await getCurrentAndPrevious({ ...input, timezone },false,- timezone+ timezone,+ ctx)(ctx.services.overview.getTopLinkOut);return current;@@ -417,7 +426,7 @@ export const overviewRouter = createTRPCRouter({).mutation(async ({ ctx, input }) => {await requireReadAccess(ctx, input.projectId);- const { timezone } = await getSettingsForProject(input.projectId);+ const { timezone } = await getSettingsForProject(ctx, input.projectId);const { runFilterCommand } = await loadFilterCommand();return runFilterCommand({query: input.query,@@ -437,11 +446,12 @@ export const overviewRouter = createTRPCRouter({})).query(async ({ input, ctx }) => {- const { timezone } = await getSettingsForProject(input.projectId);+ const { timezone } = await getSettingsForProject(ctx, input.projectId);const { current } = await getCurrentAndPrevious({ ...input, timezone },false,- timezone+ timezone,+ ctx)(ctx.services.overview.getMapData);return current;diff --git a/packages/core/src/modules/overview/overview.service.ts b/packages/core/src/modules/overview/overview.service.tsindex 792b2412c..343b7f596 100644--- a/packages/core/src/modules/overview/overview.service.ts+++ b/packages/core/src/modules/overview/overview.service.ts@@ -1251,7 +1251,7 @@ export async function getTrafficBreakdownCore(filters?: IChartEventFilter[];}) {- const { timezone } = await getSettingsForProject(input.projectId);+ const { timezone } = await getSettingsForProject(deps, input.projectId);return getTopGeneric(deps, {projectId: input.projectId,filters: input.filters ?? [],@@ -1305,7 +1305,7 @@ export async function getSegmentDailySeriesCore(return [];}- const { timezone } = await getSettingsForProject(input.projectId);+ const { timezone } = await getSettingsForProject(deps, input.projectId);const { items } = await getTopGenericSeries(deps, {projectId: input.projectId,filters: [],@@ -1343,7 +1343,7 @@ export async function getAnalyticsOverviewCore(deps: ServiceDeps,input: GetAnalyticsOverviewInput) {- const { timezone } = await getSettingsForProject(input.projectId);+ const { timezone } = await getSettingsForProject(deps, input.projectId);const interval = input.interval ?? 'day';const result = await getMetrics(deps, {diff --git a/packages/core/src/modules/overview/pages.service.ts b/packages/core/src/modules/overview/pages.service.tsindex d0ead3d65..f69be823c 100644--- a/packages/core/src/modules/overview/pages.service.ts+++ b/packages/core/src/modules/overview/pages.service.ts@@ -104,7 +104,7 @@ export async function getTopPagesCore(filters?: IChartEventFilter[];}) {- const { timezone } = await getSettingsForProject(input.projectId);+ const { timezone } = await getSettingsForProject(deps, input.projectId);return getOverviewTopPages(deps, {projectId: input.projectId,filters: input.filters ?? [],@@ -126,7 +126,7 @@ export async function getEntryExitPagesCore(filters?: IChartEventFilter[];}) {- const { timezone } = await getSettingsForProject(input.projectId);+ const { timezone } = await getSettingsForProject(deps, input.projectId);return getTopEntryExit(deps, {projectId: input.projectId,filters: input.filters ?? [],@@ -150,7 +150,7 @@ export async function getPagePerformanceCore(limit?: number;}) {- const { timezone } = await getSettingsForProject(input.projectId);+ const { timezone } = await getSettingsForProject(deps, input.projectId);const results = await getTopPages(deps, {projectId: input.projectId,startDate: input.startDate,diff --git a/packages/core/src/modules/project/project.service.test.ts b/packages/core/src/modules/project/project.service.test.tsindex cb7f37041..667d8ac3c 100644--- a/packages/core/src/modules/project/project.service.test.ts+++ b/packages/core/src/modules/project/project.service.test.ts@@ -10,7 +10,7 @@// v1-compat singleton (see project.service.ts's header), which this test// has no real `AppDeps` to build.-import { beforeAll, beforeEach, expect, mock, test } from 'bun:test';+import { afterAll, beforeAll, beforeEach, expect, mock, test } from 'bun:test';interface FakeProject {id: string;@@ -162,7 +162,15 @@ function cacheableStub(set: () => async () => 'OK' as const,});}+// M10-009: each factory spreads a plain-object SNAPSHOT of the real module and+// is restored in afterAll. `mock.module` has no per-file scope under bare+// `bun test` (AGENTS.md), and a partial factory for `@openpanel/redis` or+// `../../v1-compat` deletes every export it does not name for whichever file+// runs next — which, now that cross-module callers reach the v1-compat seam+// directly rather than through the package barrel, is a live hazard.+const realRedis = { ...(await import('@openpanel/redis')) };mock.module('@openpanel/redis', () => ({+ ...realRedis,cacheable: cacheableStub,getRedisCache: () => ({get: async () => null,@@ -171,12 +179,24 @@ mock.module('@openpanel/redis', () => ({}),}));+const realSlugId = { ...(await import('../../shared/slug-id')) };mock.module('../../shared/slug-id', () => ({- getId: async (_table: string, name: string) => `${name}-slug`,+ ...realSlugId,+ getId: async (_deps: unknown, _table: string, name: string) => `${name}-slug`,}));const clearClientByIdCache = mock(async () => 0);-mock.module('../../v1-compat', () => ({ clearClientByIdCache }));+const realV1Compat = { ...(await import('../../v1-compat')) };+mock.module('../../v1-compat', () => ({+ ...realV1Compat,+ clearClientByIdCache,+}));++afterAll(() => {+ mock.module('@openpanel/redis', () => realRedis);+ mock.module('../../shared/slug-id', () => realSlugId);+ mock.module('../../v1-compat', () => realV1Compat);+});let subject: import('./project.service').ProjectService;beforeAll(async () => {diff --git a/packages/core/src/modules/project/project.service.ts b/packages/core/src/modules/project/project.service.tsindex 97c57828d..3a48032c8 100644--- a/packages/core/src/modules/project/project.service.ts+++ b/packages/core/src/modules/project/project.service.ts@@ -13,14 +13,16 @@// INSIDE `createProjectService(deps)` for the same reason// `getClientByIdCached` does in client.service.ts — see that file's header.//-// ClickHouse queries here still build with clix/sqlstring/chQuery, not raw+// ClickHouse queries here still build with clix/sqlstring/`chQuery`, not raw// `sql` fragments: ADR-013 converts the analytics read path one query per P7// task, and this module's two queries (`getProjectEventsCount`/-// `getLastEventPerProject`) haven't been converted yet — see `loadChHelpers`'s-// own comment below. The CLIENT is `deps.ch` either way.+// `getLastEventPerProject`) haven't been converted yet. The CLIENT is+// `deps.ch` either way, through core's own `chQuery` (shared/ch-query.ts) —+// M10-009 dropped the `compatChHelpers()` hop these two used to make.import crypto from 'node:crypto';import { stripTrailingSlash } from '@openpanel/common';+import { clix } from '@openpanel/db/src/clickhouse/query-builder';import type {Prisma,Project,@@ -30,25 +32,17 @@ import { cacheable } from '@openpanel/redis';import sqlstring from 'sqlstring';import { TRPCBadRequestError } from '../../rpc/errors';import type { ServiceDeps } from '../../services';+import { convertClickhouseDateToJs } from '../../shared/ch-dates';+import { chQuery } from '../../shared/ch-query';+import { TABLE_NAMES } from '../../shared/ch-tables';import { getId } from '../../shared/slug-id';import { hashPassword } from '../auth/auth.service';-// `getProjectEventsCount` and `getLastEventPerProject` still build their-// query with clix/sqlstring/`chQuery`, not raw `sql` fragments — converting-// the query BODIES is ADR-013's P7 task (one query per task, with an-// old-vs-new result-set diff), not this wave's; `chQuery` also does its own-// retry/round-robin and Int-meta coercion (ClickHouse's JSON format returns-// every Int*/UInt* column as a string) that would otherwise have to be-// reimplemented by hand. The CLIENT is `deps.ch`, same as everywhere else in-// this file; only the pure helper functions (`clix`, `TABLE_NAMES`,-// `chQuery`, `convertClickhouseDateToJs`) come from `@openpanel/db`, and —-// same as `loadClientService` below — that reach is through the v1-compat-// singleton, not a direct import: services.ts -> project.service.ts (this-// file) -> v1-compat.ts -> services.ts is a genuine cycle, kept lazy.-function loadChHelpers() {- return import('../../v1-compat').then((m) => m.compatChHelpers());-}-+// `clix` is a value import of `@openpanel/db` and stays one: it is a pure+// query BUILDER that takes the client as its first argument+// (`clix(deps.ch)`), not a connection — the same standing ADR-013's `sql` tag+// has (see shared/ch-query.ts). `TABLE_NAMES` and the date helper are core's+// own copies (shared/ch-tables.ts, shared/ch-dates.ts).// GENUINE CYCLE, kept lazy: `createClientForOrganization`/// `updateClientForOrganization` invalidate a project's clients' cache// entries, and that cache now lives inside `createClientService(deps)` (see@@ -172,11 +166,11 @@ export async function getProjects(* counter.*/export const getProjectEventsCount = async (- _deps: ServiceDeps,+ deps: ServiceDeps,projectId: string) => {- const { chQuery, TABLE_NAMES } = await loadChHelpers();const res = await chQuery<{ count: number }>(+ deps,`SELECT sum(event_count) as count FROM ${TABLE_NAMES.event_names_mv} WHERE project_id = ${sqlstring.escape(projectId)} AND name NOT IN ('session_start', 'session_end')`);return res[0]?.count;@@ -192,8 +186,6 @@ export const getProjectEventsCount = async (export const getLastEventPerProject = async (deps: ServiceDeps): Promise<Map<string, Date>> => {- const { clix, TABLE_NAMES, convertClickhouseDateToJs } =- await loadChHelpers();const res = await clix(deps.ch).select<{ project_id: string; last_event_at: string }>(['project_id',@@ -469,7 +461,7 @@ export function createProjectService(deps: ServiceDeps): ProjectService {const secret = `sec_${crypto.randomBytes(10).toString('hex')}`;const project = await deps.db.project.create({data: {- id: await getId('project', input.name),+ id: await getId(deps, 'project', input.name),organizationId,name: input.name,domain: input.domain ? stripTrailingSlash(input.domain) : null,diff --git a/packages/core/src/modules/reference/reference.service.test.ts b/packages/core/src/modules/reference/reference.service.test.tsindex 6cdbde826..d6359e46e 100644--- a/packages/core/src/modules/reference/reference.service.test.ts+++ b/packages/core/src/modules/reference/reference.service.test.ts@@ -97,7 +97,9 @@ const reference = {}),};-const getSettingsForProject = mock(async () => ({ timezone: 'UTC' }));+const getSettingsForProject = mock(+ async (_deps: unknown, _projectId: string) => ({ timezone: 'UTC' })+);const getChartStartEndDate = mock(() => ({startDate: '2026-08-25 00:00:00',endDate: '2026-09-01 23:59:59',@@ -214,5 +216,8 @@ test('getChartReferences filters by the resolved date window', async () => {range: 'last30Days' as never,});expect(result.map((r) => r.id)).toEqual(['ref_in_range']);- expect(getSettingsForProject).toHaveBeenCalledWith('proj_1');+ expect(getSettingsForProject).toHaveBeenCalledWith(+ expect.anything(),+ 'proj_1'+ );});diff --git a/packages/core/src/modules/reference/reference.service.ts b/packages/core/src/modules/reference/reference.service.tsindex cfe08e80a..f62938592 100644--- a/packages/core/src/modules/reference/reference.service.ts+++ b/packages/core/src/modules/reference/reference.service.ts@@ -114,7 +114,7 @@ export async function getChartReferences(deps: ServiceDeps,input: ChartReferencesInput): Promise<Reference[]> {- const { timezone } = await getSettingsForProject(input.projectId);+ const { timezone } = await getSettingsForProject(deps, input.projectId);const { startDate, endDate } = getChartStartEndDate({startDate: input.startDate,diff --git a/packages/core/src/modules/report/report.service.ts b/packages/core/src/modules/report/report.service.tsindex 7ef689b42..c4ee944b1 100644--- a/packages/core/src/modules/report/report.service.ts+++ b/packages/core/src/modules/report/report.service.ts@@ -210,7 +210,7 @@ export async function getReportDataCore(}const report = transformReport(rawReport);- const { timezone } = await getSettingsForProject(input.projectId);+ const { timezone } = await getSettingsForProject(deps, input.projectId);const { startDate, endDate } = getChartStartEndDate(report, timezone);const chartInput = { ...report, startDate, endDate, timezone };diff --git a/packages/core/src/modules/session/session.rpc.ts b/packages/core/src/modules/session/session.rpc.tsindex ad081a956..e245ce866 100644--- a/packages/core/src/modules/session/session.rpc.ts+++ b/packages/core/src/modules/session/session.rpc.ts@@ -57,7 +57,7 @@ export const sessionRouter = createTRPCRouter({.query(async ({ input, ctx }) => {await requireReadAccess(ctx, input.projectId);- return getSessionList({+ return getSessionList(ctx, {...input,cursor: input.cursor ? new Date(input.cursor) : undefined,});@@ -73,7 +73,7 @@ export const sessionRouter = createTRPCRouter({.query(async ({ input, ctx }) => {await requireReadAccess(ctx, input.projectId);- return getSessionDistinctValues(input.projectId, input.field);+ return getSessionDistinctValues(ctx, input.projectId, input.field);}),byId: procedure@@ -81,7 +81,7 @@ export const sessionRouter = createTRPCRouter({.query(async ({ input, ctx }) => {await requireReadAccess(ctx, input.projectId);- return getSessionById(input.sessionId, input.projectId);+ return getSessionById(ctx, input.sessionId, input.projectId);}),replayChunksFrom: procedure@@ -96,6 +96,7 @@ export const sessionRouter = createTRPCRouter({await requireReadAccess(ctx, input.projectId);return getSessionReplayChunksFrom(+ ctx,input.sessionId,input.projectId,input.fromIndexdiff --git a/packages/core/src/modules/session/session.service.ts b/packages/core/src/modules/session/session.service.tsindex 931b01f6b..de85e88ac 100644--- a/packages/core/src/modules/session/session.service.ts+++ b/packages/core/src/modules/session/session.service.ts@@ -8,14 +8,19 @@// 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.//-// db/ch access is LAZY (`load*` below) for the reason insight.service.ts's-// header gives: constructing @openpanel/db's clients at import time spawns a-// pino-pretty worker per `bun test --isolate` file. `@openpanel/redis`'s-// `cacheable` and the `sql` tag have no such side effect.+// M10-009: the ClickHouse client is `deps.ch`, reached through core's own+// `chQuery` (shared/ch-query.ts), so the requestId minted at the edge reaches+// every query here (ADR-018, docs/TECH_DEBT.md §4). The one exception is+// `getSessionsCountCached`: `cacheable` keys on the call's ARGUMENTS+// (packages/redis/cachable.ts), so `ServiceDeps` cannot be a leading+// parameter — it reaches the boot scope's deps through the declared v1-compat+// seam instead. The remaining `load*` functions are intra-package lazy+// imports, kept lazy for a cycle, not for a client.import { cacheable } from '@openpanel/redis';import type { IChartEventFilter } from '@openpanel/validation';import type { ServiceDeps } from '../../services';+import { chQuery } from '../../shared/ch-query';import { getSafeJson } from '../../shared/json';import type { IServiceProfile } from '../profile/profile.service';import { convertClickhouseDateToJs } from './src/dates';@@ -41,20 +46,21 @@ export {type SessionDistinctField,} from './src/session.sql';-function loadChClient() {- return import('@openpanel/db/src/clickhouse/client');-}-function loadFilterCompiler() {return import('../chart/src/table-filter-where');}+/** `getSessionsCountCached` cannot carry `ServiceDeps` — see the header. */+function loadCompatServiceDeps() {+ return import('../../v1-compat').then((m) => m.compatServiceDeps());+}+function loadLookback() {- return import('@openpanel/core');+ return import('../../shared/lookback');}function loadDateService() {- return import('@openpanel/core');+ return import('../../shared/date');}export interface IClickhouseSession {@@ -247,7 +253,10 @@ function emptyProfile(profileId: string, projectId: string): IServiceProfile {};}-export async function getSessionList(options: GetSessionListOptions) {+export async function getSessionList(+ deps: ServiceDeps,+ options: GetSessionListOptions+) {const {cursor,take,@@ -260,7 +269,6 @@ export async function getSessionList(options: GetSessionListOptions) {dateIntervalInDays = DEFAULT_LOOKBACK_DAYS,} = options;- const { chQuery } = await loadChClient();const { resolveMaxLookbackDays } = await loadLookback();// Deployment-tunable ceiling for the empty-result lookback (see lookback.ts).@@ -271,6 +279,7 @@ export async function getSessionList(options: GetSessionListOptions) {const lookbackDays = Math.min(dateIntervalInDays, maxLookbackDays);const data = await chQuery<IClickhouseSession & { hasReplay: boolean }>(+ deps,sessionListQuery({projectId,take,@@ -293,7 +302,7 @@ export async function getSessionList(options: GetSessionListOptions) {hasSessionListLookback({ cursor, startDate, endDate }) &&lookbackDays < maxLookbackDays) {- return getSessionList({+ return getSessionList(deps, {...options,dateIntervalInDays: dateIntervalInDays * 2,});@@ -322,16 +331,19 @@ export async function getSessionList(options: GetSessionListOptions) {};}-export async function getSessionsCount({- projectId,- profileId,- filters,- startDate,- endDate,- search,-}: Omit<GetSessionListOptions, 'take' | 'cursor'>) {- const { chQuery } = await loadChClient();+export async function getSessionsCount(+ deps: ServiceDeps,+ {+ projectId,+ profileId,+ filters,+ startDate,+ endDate,+ search,+ }: Omit<GetSessionListOptions, 'take' | 'cursor'>+) {const result = await chQuery<{ count: number }>(+ deps,sessionsCountQuery({projectId,profileId,@@ -348,7 +360,9 @@ export async function getSessionsCount({}export const getSessionsCountCached = cacheable(- getSessionsCount,+ 'getSessionsCount',+ async (options: Omit<GetSessionListOptions, 'take' | 'cursor'>) =>+ getSessionsCount(await loadCompatServiceDeps(), options),SESSIONS_COUNT_CACHE_SECONDS);@@ -361,12 +375,13 @@ export interface ISessionReplayChunkMeta {}export async function getSessionReplayChunksFrom(+ deps: ServiceDeps,sessionId: string,projectId: string,fromIndex: number) {- const { chQuery } = await loadChClient();const rows = await chQuery<{ chunk_index: number; payload: string }>(+ deps,sessionReplayChunksQuery({sessionId,projectId,@@ -393,25 +408,33 @@ export async function getSessionReplayChunksFrom(}export async function getSessionDistinctValues(+ deps: ServiceDeps,projectId: string,field: SessionDistinctField,limit = DISTINCT_VALUES_DEFAULT_LIMIT): Promise<string[]> {- const { chQuery } = await loadChClient();const results = await chQuery<{ value: string }>(+ deps,sessionDistinctValuesQuery({ projectId, field, limit }));return results.map((r) => r.value).filter(Boolean);}-export async function getSessionById(sessionId: string, projectId: string) {- const { chQuery } = await loadChClient();+export async function getSessionById(+ deps: ServiceDeps,+ sessionId: string,+ projectId: string+) {const [sessionRows, hasReplayRows] = await Promise.all([chQuery<IClickhouseSession>(+ deps,sessionByIdQuery({ sessionId, projectId }),CLIX_SESSION_TIMEZONE),- chQuery<{ n: number }>(sessionHasReplayQuery({ sessionId, projectId })),+ chQuery<{ n: number }>(+ deps,+ sessionHasReplayQuery({ sessionId, projectId })+ ),]);if (!sessionRows[0]) {@@ -447,9 +470,9 @@ function toClixDatetime(date: string): string {}export async function querySessionsCore(+ deps: ServiceDeps,input: QuerySessionsInput): Promise<IClickhouseSession[]> {- const { chQuery } = await loadChClient();const { resolveDateRange } = await loadDateService();const { startDate, endDate } = resolveDateRange(input.startDate,@@ -457,6 +480,7 @@ export async function querySessionsCore();return chQuery<IClickhouseSession>(+ deps,querySessionsQuery({...input,startDate: toClixDatetime(startDate),@@ -488,7 +512,7 @@ export interface SessionService {export function createSessionService(deps: ServiceDeps): SessionService {return {- byId: getSessionById,+ byId: (sessionId, projectId) => getSessionById(deps, sessionId, projectId),enqueueSessionEnd: async (input) => {await deps.queues.sessions.session.add(sessionEndJobPayload(input),diff --git a/packages/core/src/modules/session/src/session-reaper.ts b/packages/core/src/modules/session/src/session-reaper.tsindex 9053424a6..4fc25f90c 100644--- a/packages/core/src/modules/session/src/session-reaper.ts+++ b/packages/core/src/modules/session/src/session-reaper.ts@@ -1,7 +1,7 @@// Ported from apps/worker/src/jobs/cron.session-reaper.ts (M7-001).-import type { IServiceCreateEventPayload } from '@openpanel/core';import type { Logger } from '../../../logger';+import type { IServiceCreateEventPayload } from '../../event/event.service';import type { IClickhouseSession } from '../session.service';import type { SessionRuntime } from './runtime';import {diff --git a/packages/core/src/modules/session/src/usage.ts b/packages/core/src/modules/session/src/usage.tsindex 7db74ee34..4dcd270b7 100644--- a/packages/core/src/modules/session/src/usage.ts+++ b/packages/core/src/modules/session/src/usage.ts@@ -8,7 +8,7 @@ import { cacheable } from '@openpanel/redis';import { sendEmail } from '../../../clients/email';import { createLogger, type ILogger } from '../../../clients/logger';import type { Logger } from '../../../logger';-import { getOrganizationBillingEventsCount } from '../../organization/organization.service';+import { getOrganizationBillingEventsCount } from '../../../v1-compat';// project.service.ts's `getProjectEventsCount` takes `ServiceDeps` now// (M10-004); this file has none (its own db access is the lazy `loadDb()`@@ -24,8 +24,13 @@ const USAGE_WARNING_THRESHOLD = 0.8;const UPDATE_EVENTS_COUNT_CACHE_SECONDS = 60 * 60;const DEFAULT_DASHBOARD_URL = 'https://dashboard.openpanel.dev';+// M10-009: `updateEventsCount` is `cacheable`, whose key is derived from the+// call's ARGUMENTS (packages/redis/cachable.ts), so it cannot take a+// `ServiceDeps` leading parameter — Postgres comes through the same declared+// v1-compat seam this file already reaches for `getProjectEventsCount`, which+// hands back the boot scope's `AppDeps.db` rather than a second singleton.function loadDb() {- return import('@openpanel/db/src/prisma-client').then((m) => m.db);+ return import('../../../v1-compat').then((m) => m.compatDb());}// `cacheable` keys on the function's arguments, so the per-run ctx.loggerdiff --git a/packages/core/src/modules/subscription/subscription.service.ts b/packages/core/src/modules/subscription/subscription.service.tsindex 6089cb75c..68daeb8d4 100644--- a/packages/core/src/modules/subscription/subscription.service.ts+++ b/packages/core/src/modules/subscription/subscription.service.ts@@ -42,10 +42,14 @@ import { z } from 'zod';import type { Logger } from '../../logger';import { TRPCBadRequestError } from '../../rpc/errors';import type { ServiceDeps } from '../../services';+// `getOrganizationById` takes `ServiceDeps` since M10-009 and these four+// functions carry none (V1's still-live subscription router calls them with a+// bare userId) — reached through the v1-compat seam, same as the rest of this+// file's cross-module calls. GENUINE CYCLE, kept lazy on the other side.+import { getOrganizationById } from '../../v1-compat';import { getAccessChecks } from '../auth/auth.service';import {getOrganizationBillingEventsCountSerieCached,- getOrganizationById,getOrganizationByProjectIdCached,} from '../organization/organization.service';import {diff --git a/packages/core/src/modules/widget/widget.rpc.ts b/packages/core/src/modules/widget/widget.rpc.tsindex d2a033852..7e778909b 100644--- a/packages/core/src/modules/widget/widget.rpc.ts+++ b/packages/core/src/modules/widget/widget.rpc.ts@@ -11,33 +11,31 @@// ClickHouse/Postgres calls rather than delegating to a shared service —// the one genuinely shared piece is the report module's widget zod schemas.//-// db/ch/redis access is LAZY (`load*` below): this router lands in the eager-// rpc.router.ts barrel chain nearly every core test file reaches, and-// constructing @openpanel/db's clients (or reaching @openpanel/redis's-// `getCache`) at import time would spawn a pino-pretty transport worker-// thread per test file / break a test that partially mocks that package —-// see subscription.service.ts's header for the `getCache` half specifically.+// M10-009: Postgres and ClickHouse come from `ctx` (`ctx.db` / `ctx.ch`), so+// the requestId minted at the edge reaches the query (ADR-018); the `loadDb`/+// `loadChClient` lazy loaders are gone. Redis's `getCache` stays lazy: this+// router lands in the eager rpc.router.ts barrel chain nearly every core test+// file reaches and reaching @openpanel/redis at import time breaks a test that+// partially mocks that package — see subscription.service.ts's header.+//+// `clix` is a value import and stays one: it is a pure query BUILDER that+// takes the client as its first argument (`clix(ctx.ch, timezone)`), not a+// connection — same standing as ADR-013's `sql` tag (see shared/ch-query.ts).import { clix } from '@openpanel/db/src/clickhouse/query-builder';import ShortUniqueId from 'short-unique-id';import { z } from 'zod';import { loadDbBuffers } from '../../buffers/lazy-db-buffers';+import type { Ctx } from '../../context';import { createTRPCRouter, procedure } from '../../rpc/base';import { TRPCAccessError, TRPCNotFoundError } from '../../rpc/errors';+import { TABLE_NAMES } from '../../shared/ch-tables';import { getSettingsForProject } from '../organization/organization.service';import { zWidgetOptions, zWidgetType } from '../report/report.constants';const uid = new ShortUniqueId({ length: 6 });const BADGE_CACHE_TTL_SECONDS = 5 * 60; // queries 30 days of data-function loadDb() {- return import('@openpanel/db/src/prisma-client').then((m) => m.db);-}--function loadChClient() {- return import('@openpanel/db/src/clickhouse/client');-}-function loadEventBuffer() {return loadDbBuffers().then((m) => m.eventBuffer);}@@ -54,8 +52,11 @@ function requireLogin(userId: string | null | undefined): string {}// Helper to find widget by projectId and type-async function findWidgetByType(projectId: string, type: string) {- const db = await loadDb();+async function findWidgetByType(+ db: Ctx['db'],+ projectId: string,+ type: string+) {const widgets = await db.shareWidget.findMany({where: { projectId },});@@ -70,7 +71,11 @@ export const widgetRouter = createTRPCRouter({.input(z.object({ projectId: z.string(), type: zWidgetType })).query(async ({ input, ctx }) => {requireLogin(ctx.session.userId);- const widget = await findWidgetByType(input.projectId, input.type);+ const widget = await findWidgetByType(+ ctx.db,+ input.projectId,+ input.type+ );if (!widget) {return null;@@ -91,8 +96,12 @@ export const widgetRouter = createTRPCRouter({).mutation(async ({ input, ctx }) => {requireLogin(ctx.session.userId);- const db = await loadDb();- const existing = await findWidgetByType(input.projectId, input.type);+ const db = ctx.db;+ const existing = await findWidgetByType(+ ctx.db,+ input.projectId,+ input.type+ );if (existing) {return db.shareWidget.update({@@ -134,8 +143,9 @@ export const widgetRouter = createTRPCRouter({).mutation(async ({ input, ctx }) => {requireLogin(ctx.session.userId);- const db = await loadDb();+ const db = ctx.db;const existing = await findWidgetByType(+ ctx.db,input.projectId,input.options.type);@@ -161,8 +171,8 @@ export const widgetRouter = createTRPCRouter({counter: procedure.input(z.object({ shareId: z.string() }))- .query(async ({ input }) => {- const db = await loadDb();+ .query(async ({ input, ctx }) => {+ const db = ctx.db;const widget = await db.shareWidget.findUnique({where: {id: input.shareId,@@ -187,8 +197,8 @@ export const widgetRouter = createTRPCRouter({badge: procedure.input(z.object({ shareId: z.string() }))- .query(async ({ input }) => {- const db = await loadDb();+ .query(async ({ input, ctx }) => {+ const db = ctx.db;const widget = await db.shareWidget.findUnique({where: {id: input.shareId,@@ -204,8 +214,8 @@ export const widgetRouter = createTRPCRouter({}const { projectId } = widget;- const { timezone } = await getSettingsForProject(projectId);- const { ch, TABLE_NAMES } = await loadChClient();+ const { timezone } = await getSettingsForProject(ctx, projectId);+ const ch = ctx.ch;const getCache = await loadCache();// Cache for 5 minutes since this queries 30 days of data@@ -233,8 +243,8 @@ export const widgetRouter = createTRPCRouter({realtimeData: procedure.input(z.object({ shareId: z.string() }))- .query(async ({ input }) => {- const db = await loadDb();+ .query(async ({ input, ctx }) => {+ const db = ctx.db;// Validate ShareWidget exists and is publicconst widget = await db.shareWidget.findUnique({where: {@@ -260,8 +270,8 @@ export const widgetRouter = createTRPCRouter({throw new TRPCNotFoundError('Invalid widget type');}- const { timezone } = await getSettingsForProject(projectId);- const { ch, TABLE_NAMES } = await loadChClient();+ const { timezone } = await getSettingsForProject(ctx, projectId);+ const ch = ctx.ch;// Always fetch live count and histogramconst totalSessionsQuery = clix(ch, timezone)diff --git a/packages/core/src/services.ts b/packages/core/src/services.tsindex 92716ff40..8691575f3 100644--- a/packages/core/src/services.ts+++ b/packages/core/src/services.ts@@ -15,6 +15,22 @@ import {type ChartService,createChartService,} from './modules/chart/chart.service';+import {+ type ConversionService,+ createConversionService,+} from './modules/chart/conversion.service';+import {+ createFunnelService,+ type FunnelService,+} from './modules/chart/funnel.service';+import {+ createRetentionService,+ type RetentionService,+} from './modules/chart/retention.service';+import {+ createSankeyService,+ type SankeyService,+} from './modules/chart/sankey.service';import {type ClientService,createClientService,@@ -56,6 +72,10 @@ import {createInsightService,type InsightService,} from './modules/insight/insight.service';+import {+ createIntegrationService,+ type IntegrationService,+} from './modules/integration/integration.service';import { createMcpService, type McpService } from './modules/mcp/mcp.service';import {createMiscService,@@ -145,6 +165,7 @@ export interface Services {import: ImportService;ingest: IngestService;organization: OrganizationService;+ integration: IntegrationService;onboarding: OnboardingService;notification: NotificationService;session: SessionService;@@ -152,6 +173,15 @@ export interface Services {profile: ProfileService;group: GroupService;chart: ChartService;+ // One key per `*.service.ts` (M10-009): the chart module is four files+ // besides `chart.service.ts`, so each is bound here too. `chart` keeps the+ // composed facade its own callers already use — the factories build stateless+ // closures over `deps`, so binding a sub-module twice binds the same+ // functions, not a second piece of state.+ funnel: FunnelService;+ conversion: ConversionService;+ sankey: SankeyService;+ retention: RetentionService;overview: OverviewService;pages: PagesService;realtime: RealtimeService;@@ -176,13 +206,14 @@ export function createServices(deps: ServiceDeps): Services {// and a `() => container` thunk: captured, not copied, so two services may// call each other without a cycle.return {- auth: createAuthService(deps),+ auth: createAuthService(),insight: createInsightService(deps),gsc: createGscService(deps),cohort: createCohortService(deps),import: createImportService(deps),ingest: createIngestService(deps),organization: createOrganizationService(deps),+ integration: createIntegrationService(deps),onboarding: createOnboardingService(deps),notification: createNotificationService(deps),session: createSessionService(deps),@@ -190,6 +221,10 @@ export function createServices(deps: ServiceDeps): Services {profile: createProfileService(deps),group: createGroupService(deps),chart: createChartService(deps),+ funnel: createFunnelService(deps),+ conversion: createConversionService(deps),+ sankey: createSankeyService(deps),+ retention: createRetentionService(deps),overview: createOverviewService(deps),pages: createPagesService(deps),realtime: createRealtimeService(deps),@@ -205,7 +240,7 @@ export function createServices(deps: ServiceDeps): Services {subscription: createSubscriptionService(deps),salt: createSaltService(deps),conversation: createConversationService(deps),- assistant: createAssistantService(deps),- mcp: createMcpService(deps),+ assistant: createAssistantService(),+ mcp: createMcpService(),};}diff --git a/packages/core/src/shared/access-lookups.ts b/packages/core/src/shared/access-lookups.tsindex 40e5bda2f..3e61cd94d 100644--- a/packages/core/src/shared/access-lookups.ts+++ b/packages/core/src/shared/access-lookups.ts@@ -5,12 +5,6 @@// the real `@openpanel/db` binding ~28 modules' `src/access.ts` files supply// to it. Same shape as packages/trpc/src/access.ts's binding.//-// db access is LAZY (`loadDb` below), not a static top-level import — see-// insight.service.ts's header for the full reasoning (jobs.registry.ts and-// services.ts pull this module into the eager barrel chain nearly every core-// test file reaches, and constructing @openpanel/db's clients at import time-// would spawn a pino-pretty transport worker thread per test file).-//// M10-004: `project.service.ts`'s `getProjectById` now takes `ServiceDeps`,// which this file has none of — reached through the v1-compat singleton// instead (see v1-compat.ts's header). GENUINE CYCLE, kept lazy: auth.service.ts@@ -19,12 +13,20 @@// file, but `getAccessChecks()` in auth.service.ts reaches both this file and// project.service.ts, so a static import here risks the same evaluation-order// hazard `getAccessChecks()`'s own header warns about.+//+// M10-009: Postgres comes through that same seam (`compatDb()`), not a direct+// `import('@openpanel/db/...')`. Two of the three lookups here are `cacheable`,+// whose key is derived from the call's ARGUMENTS (packages/redis/cachable.ts),+// so they cannot take a `ServiceDeps` leading parameter at all; the third+// (`getClientAccess`) delegates to them and stays symmetric. What they get is+// the boot scope's `AppDeps.db` — the same client `ctx.db` is — rather than a+// second module-level singleton.import type { AccessLevel } from '@openpanel/db/src/prisma-client';import { cacheable } from '@openpanel/redis';function loadDb() {- return import('@openpanel/db/src/prisma-client').then((m) => m.db);+ return import('../v1-compat').then((m) => m.compatDb());}function loadProjectService() {diff --git a/packages/core/src/shared/ch-dates.parity.test.ts b/packages/core/src/shared/ch-dates.parity.test.tsnew file mode 100644index 000000000..7403c612a--- /dev/null+++ b/packages/core/src/shared/ch-dates.parity.test.ts@@ -0,0 +1,40 @@+// Guards the second duplication M10-009 introduced: core owns its own copies of+// @openpanel/db's ClickHouse date helpers (see ch-dates.ts's header for why),+// and a divergence would show up as silently wrong date literals in a query+// rather than as a failing test.+//+// This is a test file, which `core-uses-ctx-not-db-internals` exempts.++import { describe, expect, it } from 'bun:test';+import * as dbClient from '@openpanel/db/src/clickhouse/client';+import { convertClickhouseDateToJs, formatClickhouseDate } from './ch-dates';++const CASES = [+ new Date('2026-09-06T12:34:56.789Z'),+ new Date('2026-01-01T00:00:00.000Z'),+ new Date('1970-01-01T00:00:00.000Z'),+ '2026-09-06T23:59:59.999Z',+ '2026-02-28 08:00:00',+];++describe('ch-dates parity with @openpanel/db', () => {+ it('formats identically, with and without skipTime', () => {+ for (const input of CASES) {+ expect(formatClickhouseDate(input)).toBe(+ dbClient.formatClickhouseDate(input)+ );+ expect(formatClickhouseDate(input, true)).toBe(+ dbClient.formatClickhouseDate(input, true)+ );+ }+ });++ it('parses identically', () => {+ for (const input of CASES) {+ const formatted = formatClickhouseDate(input);+ expect(convertClickhouseDateToJs(formatted).toISOString()).toBe(+ dbClient.convertClickhouseDateToJs(formatted).toISOString()+ );+ }+ });+});diff --git a/packages/core/src/shared/ch-dates.ts b/packages/core/src/shared/ch-dates.tsnew file mode 100644index 000000000..e570c2861--- /dev/null+++ b/packages/core/src/shared/ch-dates.ts@@ -0,0 +1,34 @@+// ClickHouse's date literal format, shared across modules.+//+// M10-009: `@openpanel/db/src/clickhouse/client.ts` owns these too, but+// importing it constructs a real ClickHouse client and a real pino logger at+// module load and is a value import of `@openpanel/db` from core — see+// ch-tables.ts's header for the full reasoning. Five modules already carry a+// byte-identical private copy in their own `src/dates.ts` (chart, event,+// group, profile, session); folding those into this file is its own task, not+// this one's. `ch-dates.parity.test.ts` asserts this file and @openpanel/db's+// agree on every input, so the copy cannot drift silently.++const CLICKHOUSE_DATE_SUFFIX = /(\.\d{3})?Z+$/;++/**+ * `YYYY-MM-DD HH:mm:ss` in UTC — what V1 sent as a date literal.+ * `skipTime` narrows it to `YYYY-MM-DD`, for the date-only column comparisons+ * the billing counters build.+ */+export function formatClickhouseDate(+ date: Date | string,+ skipTime = false+): string {+ if (skipTime) {+ return new Date(date).toISOString().split('T')[0]!;+ }+ return new Date(date)+ .toISOString()+ .replace('T', ' ')+ .replace(CLICKHOUSE_DATE_SUFFIX, '');+}++export function convertClickhouseDateToJs(date: string): Date {+ return new Date(`${date.replace(' ', 'T')}Z`);+}diff --git a/packages/core/src/shared/ch-query.ts b/packages/core/src/shared/ch-query.tsindex c39b184a6..c5fb0a422 100644--- a/packages/core/src/shared/ch-query.ts+++ b/packages/core/src/shared/ch-query.ts@@ -27,6 +27,10 @@ import {} from '@openpanel/db/src/clickhouse/sql';import type { ServiceDeps } from '../services';+/** All these two need is the scope's client and its logger — narrowed so the+ * buffers, whose `BufferDeps` is not a `ServiceDeps`, can call them too. */+export type ChScope = Pick<ServiceDeps, 'ch' | 'logger'>;+/** A raw statement or an ADR-013 fragment carrying its own bound params. */export type ChQueryInput = string | SqlFragment;@@ -38,7 +42,7 @@ function cleanQuery(query: string): string {}export async function chQueryWithMeta<T extends object>(- deps: ServiceDeps,+ deps: ChScope,query: ChQueryInput,clickhouseSettings?: ClickHouseSettings): Promise<ResponseJSON<T>> {@@ -87,7 +91,7 @@ export async function chQueryWithMeta<T extends object>(}export async function chQuery<T extends object>(- deps: ServiceDeps,+ deps: ChScope,query: ChQueryInput,clickhouseSettings?: ClickHouseSettings): Promise<T[]> {diff --git a/packages/core/src/shared/ch-tables.parity.test.ts b/packages/core/src/shared/ch-tables.parity.test.tsnew file mode 100644index 000000000..494131367--- /dev/null+++ b/packages/core/src/shared/ch-tables.parity.test.ts@@ -0,0 +1,34 @@+// Guards the one duplication M10-009 introduced: core owns its own copy of+// @openpanel/db's `TABLE_NAMES` (see ch-tables.ts's header for why), and a+// table added on one side only would otherwise be found by a failing query in+// production rather than by CI.+//+// This is a test file, which `core-uses-ctx-not-db-internals` exempts — the+// point of the rule is that core's REQUEST path carries `ctx.ch`, and a test+// has no request.++import { describe, expect, it } from 'bun:test';+import * as dbClient from '@openpanel/db/src/clickhouse/client';+import {+ getReplicatedTableName,+ isClickhouseClustered,+ TABLE_NAMES,+} from './ch-tables';++describe('ch-tables parity with @openpanel/db', () => {+ it('has exactly the same table map', () => {+ expect(TABLE_NAMES).toEqual(dbClient.TABLE_NAMES);+ });++ it('derives the same cluster verdict', () => {+ expect(isClickhouseClustered()).toBe(dbClient.isClickhouseClustered());+ });++ it('derives the same mutation table name', () => {+ for (const name of Object.values(TABLE_NAMES)) {+ expect(getReplicatedTableName(name)).toBe(+ dbClient.getReplicatedTableName(name)+ );+ }+ });+});diff --git a/packages/core/src/shared/ch-tables.ts b/packages/core/src/shared/ch-tables.tsnew file mode 100644index 000000000..b4f301900--- /dev/null+++ b/packages/core/src/shared/ch-tables.ts@@ -0,0 +1,69 @@+// The physical ClickHouse table names core reads and writes, plus the two+// cluster helpers that derive from them.+//+// M10-009: these live in `@openpanel/db/src/clickhouse/client.ts` too, and+// that module is where the migrations (`code-migrations/`) still take them+// from — but importing it constructs a real ClickHouse client and a real pino+// logger at module load, which is the cost every `load*()` lazy loader in this+// package existed to defer, and it is a value import of `@openpanel/db` from+// core, which `core-uses-ctx-not-db-internals` now forbids. Core takes the+// map, not the client: `ch` itself is always `ctx.ch` / `deps.ch`.+//+// The duplication is policed, not trusted: `ch-tables.parity.test.ts` asserts+// this map and @openpanel/db's are identical, so a table added on one side+// and not the other fails the suite rather than a query.++export const TABLE_NAMES = {+ events: 'events',+ profiles: 'profiles',+ alias: 'profile_aliases',+ self_hosting: 'self_hosting',+ events_bots: 'events_bots',+ dau_mv: 'dau_mv',+ event_names_mv: 'distinct_event_names_mv',+ event_property_values_mv: 'event_property_values_mv',+ cohort_events_mv: 'cohort_events_mv',+ sessions: 'sessions',+ events_imports: 'events_imports',+ session_replay_chunks: 'session_replay_chunks',+ gsc_daily: 'gsc_daily',+ gsc_pages_daily: 'gsc_pages_daily',+ gsc_queries_daily: 'gsc_queries_daily',+ groups: 'groups',+ cohort_members: 'cohort_members',+ cohort_metadata: 'cohort_metadata',+ profile_event_summary_mv: 'profile_event_summary_mv',+ // Same content as the two MVs above, keyed for the cohort criteria that+ // read them (event + window first, profile last) rather than by profile.+ // See migration 20.+ event_profile_summary_mv: 'event_profile_summary_mv',+ event_property_profile_summary_mv: 'event_property_profile_summary_mv',+ profile_event_property_summary_mv: 'profile_event_property_summary_mv',+};++/**+ * Clustered mode = production (not self-hosted); non-clustered = self-hosted.+ */+export function isClickhouseClustered(): boolean {+ if (+ process.env.CLICKHOUSE_CLUSTER === 'true' ||+ process.env.CLICKHOUSE_CLUSTER === '1'+ ) {+ return true;+ }++ return !(+ process.env.SELF_HOSTED === 'true' || process.env.SELF_HOSTED === '1'+ );+}++/**+ * The table a mutation must target: `<name>_replicated ON CLUSTER '{cluster}'`+ * when clustered, the plain name otherwise.+ */+export function getReplicatedTableName(tableName: string): string {+ if (isClickhouseClustered()) {+ return `${tableName}_replicated ON CLUSTER '{cluster}'`;+ }+ return tableName;+}diff --git a/packages/core/src/shared/slug-id.ts b/packages/core/src/shared/slug-id.tsindex bdf3a48a1..b8b5301ea 100644--- a/packages/core/src/shared/slug-id.ts+++ b/packages/core/src/shared/slug-id.ts@@ -3,24 +3,23 @@// shared/access-lookups.ts: needs @openpanel/db, so it stays out of// shared/id.ts (the database-free id helpers).//-// db access is LAZY (`loadDb` below), not a static top-level import — see-// insight.service.ts's header for the full reasoning (jobs.registry.ts and-// services.ts pull this module into the eager barrel chain nearly every core-// test file reaches, and constructing @openpanel/db's clients at import time-// would spawn a pino-pretty transport worker thread per test file).+// M10-009: Postgres comes from the caller's scope (`deps.db`), not a lazy+// `import('@openpanel/db/...')` — so the requestId minted at the edge reaches+// the uniqueness probe (ADR-018, docs/TECH_DEBT.md §4). `deps` is narrowed to+// `Pick<ServiceDeps, 'db'>` because mcp's tool handlers have a fixed+// `@modelcontextprotocol/sdk` signature and only ever hold the db they got+// from the v1-compat seam.import { slug } from '@openpanel/common';--function loadDb() {- return import('@openpanel/db/src/prisma-client').then((m) => m.db);-}+import type { ServiceDeps } from '../services';export async function getId(+ deps: Pick<ServiceDeps, 'db'>,tableName: 'project' | 'dashboard' | 'organization',name: string): Promise<string> {const newId = slug(name);- const db = await loadDb();+ const db = deps.db;if (!db[tableName]) {throw new Error('Table does not exists');}@@ -45,7 +44,7 @@ export async function getId(}if (existingProject) {- return getId(tableName, random(name));+ return getId(deps, tableName, random(name));}return newId;diff --git a/packages/core/src/v1-compat.ts b/packages/core/src/v1-compat.tsindex da4d183bf..bf9b27157 100644--- a/packages/core/src/v1-compat.ts+++ b/packages/core/src/v1-compat.ts@@ -42,6 +42,20 @@ import {type SignInShareInput,signInToShare as signInToShareWithDeps,} from './modules/auth/auth.service';+import {+ computeCohort as cohort_computeCohort,+ countCohort as cohort_countCohort,+ deleteCohortMembership as cohort_deleteCohortMembership,+ getCohortCount as cohort_getCohortCount,+ getCohortEventsPerDay as cohort_getCohortEventsPerDay,+ getCohortMemberEvents as cohort_getCohortMemberEvents,+ getCohortMemberRoutes as cohort_getCohortMemberRoutes,+ getCohortMembers as cohort_getCohortMembers,+ getProfilesInCohort as cohort_getProfilesInCohort,+ listCohortMemberProfiles as cohort_listCohortMemberProfiles,+ listRefreshableCohortIds as cohort_listRefreshableCohortIds,+ updateCohortMembership as cohort_updateCohortMembership,+} from './modules/cohort/cohort.service';import {createBotEvent as event_createBotEvent,createEvent as event_createEvent,@@ -88,11 +102,115 @@ import {updateGroup as group_updateGroup,upsertGroup as group_upsertGroup,} from './modules/group/group.service';+import {+ completeGscOAuthCallback as gsc_completeGscOAuthCallback,+ disconnectGscConnection as gsc_disconnectGscConnection,+ getGscAiEngines as gsc_getGscAiEngines,+ getGscConnection as gsc_getGscConnection,+ getGscOverview as gsc_getGscOverview,+ getGscPageDetails as gsc_getGscPageDetails,+ getGscPages as gsc_getGscPages,+ getGscPreviousOverview as gsc_getGscPreviousOverview,+ getGscQueries as gsc_getGscQueries,+ getGscQueryDetails as gsc_getGscQueryDetails,+ getGscSearchEngines as gsc_getGscSearchEngines,+ gscGetCannibalizationCore as gsc_gscGetCannibalizationCore,+ gscGetOverviewCore as gsc_gscGetOverviewCore,+ gscGetPageDetailsCore as gsc_gscGetPageDetailsCore,+ gscGetQueryDetailsCore as gsc_gscGetQueryDetailsCore,+ gscGetQueryOpportunitiesCore as gsc_gscGetQueryOpportunitiesCore,+ gscGetTopPagesCore as gsc_gscGetTopPagesCore,+ gscGetTopQueriesCore as gsc_gscGetTopQueriesCore,+ listGscConnectionsForSync as gsc_listGscConnectionsForSync,+ listGscSites as gsc_listGscSites,+ resolveGscDateRange as gsc_resolveGscDateRange,+ runGscProjectBackfill as gsc_runGscProjectBackfill,+ runGscProjectSync as gsc_runGscProjectSync,+ selectGscSite as gsc_selectGscSite,+ syncGscData as gsc_syncGscData,+} from './modules/gsc/gsc.service';+import {+ backfillSessionsToProduction as import__backfillSessionsToProduction,+ cleanupSessionStartEndEvents as import__cleanupSessionStartEndEvents,+ cleanupStagingData as import__cleanupStagingData,+ createSessionsStartEndEvents as import__createSessionsStartEndEvents,+ generateGapBasedSessionIds as import__generateGapBasedSessionIds,+ getImportDateBounds as import__getImportDateBounds,+ insertImportBatch as import__insertImportBatch,+ insertProfilesBatch as import__insertProfilesBatch,+ insertRawEventsBatch as import__insertRawEventsBatch,+ moveImportsToProduction as import__moveImportsToProduction,+ runImportJob as import__runImportJob,+ updateImportStatus as import__updateImportStatus,+} from './modules/import/import.service';+import {+ cleanupStaleInsights as insight_cleanupStaleInsights,+ getReferrerSpikes as insight_getReferrerSpikes,+ listAllInsights as insight_listAllInsights,+ listDailyInsightCandidates as insight_listDailyInsightCandidates,+ listInsights as insight_listInsights,+ previewWeeklyDigest as insight_previewWeeklyDigest,+ runProjectInsights as insight_runProjectInsights,+ scanLegacyInsights as insight_scanLegacyInsights,+ sendWeeklyDigests as insight_sendWeeklyDigests,+} from './modules/insight/insight.service';+import {+ completeSlackOAuthCallback as integration_completeSlackOAuthCallback,+ createOrUpdateSlackIntegration as integration_createOrUpdateSlackIntegration,+ deleteIntegration as integration_deleteIntegration,+ getIntegrationById as integration_getIntegrationById,+ listIntegrationsForProject as integration_listIntegrationsForProject,+ testExportIntegrationConnection as integration_testExportIntegrationConnection,+ testIntegrationConnection as integration_testIntegrationConnection,+ upsertIntegration as integration_upsertIntegration,+} from './modules/integration/integration.service';import {getStats as misc_getStats,insertPingRecord as misc_insertPingRecord,runPingCron as misc_runPingCron,} from './modules/misc/misc.service';+import {+ createOrUpdateNotificationRule as notification_createOrUpdateNotificationRule,+ deleteNotificationRule as notification_deleteNotificationRule,+ deliverNotification as notification_deliverNotification,+ getNotificationRuleByIdOrThrow as notification_getNotificationRuleByIdOrThrow,+ listNotificationRules as notification_listNotificationRules,+ listNotifications as notification_listNotifications,+} from './modules/notification/notification.service';+import {+ canSkipOnboarding as onboarding_canSkipOnboarding,+ createOnboardingProject as onboarding_createOnboardingProject,+ runOnboardingCron as onboarding_runOnboardingCron,+} from './modules/onboarding/onboarding.service';+import {+ cancelOrganizationDeletion as organization_cancelOrganizationDeletion,+ connectUserToOrganization as organization_connectUserToOrganization,+ deleteFromClickhouse as organization_deleteFromClickhouse,+ deleteOrganization as organization_deleteOrganization,+ deleteProjects as organization_deleteProjects,+ getInviteById as organization_getInviteById,+ getInviteOrThrow as organization_getInviteOrThrow,+ getInvites as organization_getInvites,+ getMember as organization_getMember,+ getMembers as organization_getMembers,+ getOrganizationBillingEventsCount as organization_getOrganizationBillingEventsCount,+ getOrganizationBillingEventsCountSerie as organization_getOrganizationBillingEventsCountSerie,+ getOrganizationById as organization_getOrganizationById,+ getOrganizationByProjectId as organization_getOrganizationByProjectId,+ getOrganizationEventsCount as organization_getOrganizationEventsCount,+ getOrganizationEventsCountSince as organization_getOrganizationEventsCountSince,+ getOrganizationSubscriptionChartEndDate as organization_getOrganizationSubscriptionChartEndDate,+ getOrganizations as organization_getOrganizations,+ getSettingsForOrganization as organization_getSettingsForOrganization,+ getSettingsForProject as organization_getSettingsForProject,+ inviteUserToOrganization as organization_inviteUserToOrganization,+ removeOrganizationMember as organization_removeOrganizationMember,+ revokeInvite as organization_revokeInvite,+ runDeleteCron as organization_runDeleteCron,+ scheduleOrganizationDeletion as organization_scheduleOrganizationDeletion,+ updateOrganization as organization_updateOrganization,+ updateOrganizationMemberAccess as organization_updateOrganizationMemberAccess,+} from './modules/organization/organization.service';import {getRawWhereClause,isPageFilter,@@ -137,8 +255,17 @@ import {getRealtimePaths as realtime_getRealtimePaths,getRealtimeReferrals as realtime_getRealtimeReferrals,} from './modules/realtime/realtime.service';+import {+ getSessionById as session_getSessionById,+ getSessionDistinctValues as session_getSessionDistinctValues,+ getSessionList as session_getSessionList,+ getSessionReplayChunksFrom as session_getSessionReplayChunksFrom,+ getSessionsCount as session_getSessionsCount,+ querySessionsCore as session_querySessionsCore,+} from './modules/session/session.service';import { createServices, type ServiceDeps, type Services } from './services';import type { ISetCookie } from './shared/cookie';+import { getId as slugId_getId } from './shared/slug-id';/** Drops a function type's first (`deps`) parameter — every bare V1-compat* wrapper below has the same shape: the underlying function's parameters@@ -209,7 +336,7 @@ function fallbackServiceDeps(): Promise<ServiceDeps> {return fallback;}-function compatServiceDeps(): Promise<ServiceDeps> {+export function compatServiceDeps(): Promise<ServiceDeps> {return registeredDeps? Promise.resolve(registeredDeps): fallbackServiceDeps();@@ -1403,3 +1530,572 @@ export const pagesService = {container.pages.getPageTimeseries(...args))) as Services['pages']['getPageTimeseries'],};++// --- onboarding ------------------------------------------------------------------++export const canSkipOnboarding: (+ ...args: Tail<Parameters<typeof onboarding_canSkipOnboarding>>+) => ReturnType<typeof onboarding_canSkipOnboarding> = (...args) =>+ compatServiceDeps().then((deps) => onboarding_canSkipOnboarding(deps, ...args));++export const createOnboardingProject: (+ ...args: Tail<Parameters<typeof onboarding_createOnboardingProject>>+) => ReturnType<typeof onboarding_createOnboardingProject> = (...args) =>+ compatServiceDeps().then((deps) => onboarding_createOnboardingProject(deps, ...args));++export const runOnboardingCron: (+ ...args: Tail<Parameters<typeof onboarding_runOnboardingCron>>+) => ReturnType<typeof onboarding_runOnboardingCron> = (...args) =>+ compatServiceDeps().then((deps) => onboarding_runOnboardingCron(deps, ...args));++// --- integration -----------------------------------------------------------------++export const completeSlackOAuthCallback: (+ ...args: Tail<Parameters<typeof integration_completeSlackOAuthCallback>>+) => ReturnType<typeof integration_completeSlackOAuthCallback> = (...args) =>+ compatServiceDeps().then((deps) => integration_completeSlackOAuthCallback(deps, ...args));++export const createOrUpdateSlackIntegration: (+ ...args: Tail<Parameters<typeof integration_createOrUpdateSlackIntegration>>+) => ReturnType<typeof integration_createOrUpdateSlackIntegration> = (...args) =>+ compatServiceDeps().then((deps) => integration_createOrUpdateSlackIntegration(deps, ...args));++export const deleteIntegration: (+ ...args: Tail<Parameters<typeof integration_deleteIntegration>>+) => ReturnType<typeof integration_deleteIntegration> = (...args) =>+ compatServiceDeps().then((deps) => integration_deleteIntegration(deps, ...args));++export const getIntegrationById: (+ ...args: Tail<Parameters<typeof integration_getIntegrationById>>+) => ReturnType<typeof integration_getIntegrationById> = (...args) =>+ compatServiceDeps().then((deps) => integration_getIntegrationById(deps, ...args));++export const listIntegrationsForProject: (+ ...args: Tail<Parameters<typeof integration_listIntegrationsForProject>>+) => ReturnType<typeof integration_listIntegrationsForProject> = (...args) =>+ compatServiceDeps().then((deps) => integration_listIntegrationsForProject(deps, ...args));++export const testExportIntegrationConnection: (+ ...args: Tail<Parameters<typeof integration_testExportIntegrationConnection>>+) => ReturnType<typeof integration_testExportIntegrationConnection> = (...args) =>+ compatServiceDeps().then((deps) => integration_testExportIntegrationConnection(deps, ...args));++export const testIntegrationConnection: (+ ...args: Tail<Parameters<typeof integration_testIntegrationConnection>>+) => ReturnType<typeof integration_testIntegrationConnection> = (...args) =>+ compatServiceDeps().then((deps) => integration_testIntegrationConnection(deps, ...args));++export const upsertIntegration: (+ ...args: Tail<Parameters<typeof integration_upsertIntegration>>+) => ReturnType<typeof integration_upsertIntegration> = (...args) =>+ compatServiceDeps().then((deps) => integration_upsertIntegration(deps, ...args));++// --- notification ----------------------------------------------------------------++export const createOrUpdateNotificationRule: (+ ...args: Tail<Parameters<typeof notification_createOrUpdateNotificationRule>>+) => ReturnType<typeof notification_createOrUpdateNotificationRule> = (...args) =>+ compatServiceDeps().then((deps) => notification_createOrUpdateNotificationRule(deps, ...args));++export const deleteNotificationRule: (+ ...args: Tail<Parameters<typeof notification_deleteNotificationRule>>+) => ReturnType<typeof notification_deleteNotificationRule> = (...args) =>+ compatServiceDeps().then((deps) => notification_deleteNotificationRule(deps, ...args));++export const deliverNotification: (+ ...args: Tail<Parameters<typeof notification_deliverNotification>>+) => ReturnType<typeof notification_deliverNotification> = (...args) =>+ compatServiceDeps().then((deps) => notification_deliverNotification(deps, ...args));++export const getNotificationRuleByIdOrThrow: (+ ...args: Tail<Parameters<typeof notification_getNotificationRuleByIdOrThrow>>+) => ReturnType<typeof notification_getNotificationRuleByIdOrThrow> = (...args) =>+ compatServiceDeps().then((deps) => notification_getNotificationRuleByIdOrThrow(deps, ...args));++export const listNotificationRules: (+ ...args: Tail<Parameters<typeof notification_listNotificationRules>>+) => ReturnType<typeof notification_listNotificationRules> = (...args) =>+ compatServiceDeps().then((deps) => notification_listNotificationRules(deps, ...args));++export const listNotifications: (+ ...args: Tail<Parameters<typeof notification_listNotifications>>+) => ReturnType<typeof notification_listNotifications> = (...args) =>+ compatServiceDeps().then((deps) => notification_listNotifications(deps, ...args));++// --- slug-id ---------------------------------------------------------------------++export const getId: (+ ...args: Tail<Parameters<typeof slugId_getId>>+) => ReturnType<typeof slugId_getId> = (...args) =>+ compatServiceDeps().then((deps) => slugId_getId(deps, ...args));++// --- insight ---------------------------------------------------------------------++export const cleanupStaleInsights: (+ ...args: Tail<Parameters<typeof insight_cleanupStaleInsights>>+) => ReturnType<typeof insight_cleanupStaleInsights> = (...args) =>+ compatServiceDeps().then((deps) => insight_cleanupStaleInsights(deps, ...args));++export const listAllInsights: (+ ...args: Tail<Parameters<typeof insight_listAllInsights>>+) => ReturnType<typeof insight_listAllInsights> = (...args) =>+ compatServiceDeps().then((deps) => insight_listAllInsights(deps, ...args));++export const listDailyInsightCandidates: (+ ...args: Tail<Parameters<typeof insight_listDailyInsightCandidates>>+) => ReturnType<typeof insight_listDailyInsightCandidates> = (...args) =>+ compatServiceDeps().then((deps) => insight_listDailyInsightCandidates(deps, ...args));++export const listInsights: (+ ...args: Tail<Parameters<typeof insight_listInsights>>+) => ReturnType<typeof insight_listInsights> = (...args) =>+ compatServiceDeps().then((deps) => insight_listInsights(deps, ...args));++export const previewWeeklyDigest: (+ ...args: Tail<Parameters<typeof insight_previewWeeklyDigest>>+) => ReturnType<typeof insight_previewWeeklyDigest> = (...args) =>+ compatServiceDeps().then((deps) => insight_previewWeeklyDigest(deps, ...args));++export const runProjectInsights: (+ ...args: Tail<Parameters<typeof insight_runProjectInsights>>+) => ReturnType<typeof insight_runProjectInsights> = (...args) =>+ compatServiceDeps().then((deps) => insight_runProjectInsights(deps, ...args));++export const scanLegacyInsights: (+ ...args: Tail<Parameters<typeof insight_scanLegacyInsights>>+) => ReturnType<typeof insight_scanLegacyInsights> = (...args) =>+ compatServiceDeps().then((deps) => insight_scanLegacyInsights(deps, ...args));++export const sendWeeklyDigests: (+ ...args: Tail<Parameters<typeof insight_sendWeeklyDigests>>+) => ReturnType<typeof insight_sendWeeklyDigests> = (...args) =>+ compatServiceDeps().then((deps) => insight_sendWeeklyDigests(deps, ...args));++// --- insight (referrer spikes) ---------------------------------------------------++export const getReferrerSpikes: (+ ...args: Tail<Parameters<typeof insight_getReferrerSpikes>>+) => ReturnType<typeof insight_getReferrerSpikes> = (...args) =>+ compatServiceDeps().then((deps) => insight_getReferrerSpikes(deps, ...args));++// --- session ---------------------------------------------------------------------++export const getSessionById: (+ ...args: Tail<Parameters<typeof session_getSessionById>>+) => ReturnType<typeof session_getSessionById> = (...args) =>+ compatServiceDeps().then((deps) => session_getSessionById(deps, ...args));++export const getSessionDistinctValues: (+ ...args: Tail<Parameters<typeof session_getSessionDistinctValues>>+) => ReturnType<typeof session_getSessionDistinctValues> = (...args) =>+ compatServiceDeps().then((deps) => session_getSessionDistinctValues(deps, ...args));++export const getSessionList: (+ ...args: Tail<Parameters<typeof session_getSessionList>>+) => ReturnType<typeof session_getSessionList> = (...args) =>+ compatServiceDeps().then((deps) => session_getSessionList(deps, ...args));++export const getSessionReplayChunksFrom: (+ ...args: Tail<Parameters<typeof session_getSessionReplayChunksFrom>>+) => ReturnType<typeof session_getSessionReplayChunksFrom> = (...args) =>+ compatServiceDeps().then((deps) => session_getSessionReplayChunksFrom(deps, ...args));++export const getSessionsCount: (+ ...args: Tail<Parameters<typeof session_getSessionsCount>>+) => ReturnType<typeof session_getSessionsCount> = (...args) =>+ compatServiceDeps().then((deps) => session_getSessionsCount(deps, ...args));++export const querySessionsCore: (+ ...args: Tail<Parameters<typeof session_querySessionsCore>>+) => ReturnType<typeof session_querySessionsCore> = (...args) =>+ compatServiceDeps().then((deps) => session_querySessionsCore(deps, ...args));++// --- import ----------------------------------------------------------------------++export const backfillSessionsToProduction: (+ ...args: Tail<Parameters<typeof import__backfillSessionsToProduction>>+) => ReturnType<typeof import__backfillSessionsToProduction> = (...args) =>+ compatServiceDeps().then((deps) => import__backfillSessionsToProduction(deps, ...args));++export const cleanupSessionStartEndEvents: (+ ...args: Tail<Parameters<typeof import__cleanupSessionStartEndEvents>>+) => ReturnType<typeof import__cleanupSessionStartEndEvents> = (...args) =>+ compatServiceDeps().then((deps) => import__cleanupSessionStartEndEvents(deps, ...args));++export const cleanupStagingData: (+ ...args: Tail<Parameters<typeof import__cleanupStagingData>>+) => ReturnType<typeof import__cleanupStagingData> = (...args) =>+ compatServiceDeps().then((deps) => import__cleanupStagingData(deps, ...args));++export const createSessionsStartEndEvents: (+ ...args: Tail<Parameters<typeof import__createSessionsStartEndEvents>>+) => ReturnType<typeof import__createSessionsStartEndEvents> = (...args) =>+ compatServiceDeps().then((deps) => import__createSessionsStartEndEvents(deps, ...args));++export const generateGapBasedSessionIds: (+ ...args: Tail<Parameters<typeof import__generateGapBasedSessionIds>>+) => ReturnType<typeof import__generateGapBasedSessionIds> = (...args) =>+ compatServiceDeps().then((deps) => import__generateGapBasedSessionIds(deps, ...args));++export const getImportDateBounds: (+ ...args: Tail<Parameters<typeof import__getImportDateBounds>>+) => ReturnType<typeof import__getImportDateBounds> = (...args) =>+ compatServiceDeps().then((deps) => import__getImportDateBounds(deps, ...args));++export const insertImportBatch: (+ ...args: Tail<Parameters<typeof import__insertImportBatch>>+) => ReturnType<typeof import__insertImportBatch> = (...args) =>+ compatServiceDeps().then((deps) => import__insertImportBatch(deps, ...args));++export const insertProfilesBatch: (+ ...args: Tail<Parameters<typeof import__insertProfilesBatch>>+) => ReturnType<typeof import__insertProfilesBatch> = (...args) =>+ compatServiceDeps().then((deps) => import__insertProfilesBatch(deps, ...args));++export const insertRawEventsBatch: (+ ...args: Tail<Parameters<typeof import__insertRawEventsBatch>>+) => ReturnType<typeof import__insertRawEventsBatch> = (...args) =>+ compatServiceDeps().then((deps) => import__insertRawEventsBatch(deps, ...args));++export const moveImportsToProduction: (+ ...args: Tail<Parameters<typeof import__moveImportsToProduction>>+) => ReturnType<typeof import__moveImportsToProduction> = (...args) =>+ compatServiceDeps().then((deps) => import__moveImportsToProduction(deps, ...args));++export const runImportJob: (+ ...args: Tail<Parameters<typeof import__runImportJob>>+) => ReturnType<typeof import__runImportJob> = (...args) =>+ compatServiceDeps().then((deps) => import__runImportJob(deps, ...args));++export const updateImportStatus: (+ ...args: Tail<Parameters<typeof import__updateImportStatus>>+) => ReturnType<typeof import__updateImportStatus> = (...args) =>+ compatServiceDeps().then((deps) => import__updateImportStatus(deps, ...args));++// --- cohort ----------------------------------------------------------------------++export const computeCohort: (+ ...args: Tail<Parameters<typeof cohort_computeCohort>>+) => ReturnType<typeof cohort_computeCohort> = (...args) =>+ compatServiceDeps().then((deps) => cohort_computeCohort(deps, ...args));++export const countCohort: (+ ...args: Tail<Parameters<typeof cohort_countCohort>>+) => ReturnType<typeof cohort_countCohort> = (...args) =>+ compatServiceDeps().then((deps) => cohort_countCohort(deps, ...args));++export const deleteCohortMembership: (+ ...args: Tail<Parameters<typeof cohort_deleteCohortMembership>>+) => ReturnType<typeof cohort_deleteCohortMembership> = (...args) =>+ compatServiceDeps().then((deps) => cohort_deleteCohortMembership(deps, ...args));++export const getCohortCount: (+ ...args: Tail<Parameters<typeof cohort_getCohortCount>>+) => ReturnType<typeof cohort_getCohortCount> = (...args) =>+ compatServiceDeps().then((deps) => cohort_getCohortCount(deps, ...args));++export const getCohortEventsPerDay: (+ ...args: Tail<Parameters<typeof cohort_getCohortEventsPerDay>>+) => ReturnType<typeof cohort_getCohortEventsPerDay> = (...args) =>+ compatServiceDeps().then((deps) => cohort_getCohortEventsPerDay(deps, ...args));++export const getCohortMemberEvents: (+ ...args: Tail<Parameters<typeof cohort_getCohortMemberEvents>>+) => ReturnType<typeof cohort_getCohortMemberEvents> = (...args) =>+ compatServiceDeps().then((deps) => cohort_getCohortMemberEvents(deps, ...args));++export const getCohortMemberRoutes: (+ ...args: Tail<Parameters<typeof cohort_getCohortMemberRoutes>>+) => ReturnType<typeof cohort_getCohortMemberRoutes> = (...args) =>+ compatServiceDeps().then((deps) => cohort_getCohortMemberRoutes(deps, ...args));++export const getCohortMembers: (+ ...args: Tail<Parameters<typeof cohort_getCohortMembers>>+) => ReturnType<typeof cohort_getCohortMembers> = (...args) =>+ compatServiceDeps().then((deps) => cohort_getCohortMembers(deps, ...args));++export const getProfilesInCohort: (+ ...args: Tail<Parameters<typeof cohort_getProfilesInCohort>>+) => ReturnType<typeof cohort_getProfilesInCohort> = (...args) =>+ compatServiceDeps().then((deps) => cohort_getProfilesInCohort(deps, ...args));++export const listCohortMemberProfiles: (+ ...args: Tail<Parameters<typeof cohort_listCohortMemberProfiles>>+) => ReturnType<typeof cohort_listCohortMemberProfiles> = (...args) =>+ compatServiceDeps().then((deps) => cohort_listCohortMemberProfiles(deps, ...args));++export const listRefreshableCohortIds: (+ ...args: Tail<Parameters<typeof cohort_listRefreshableCohortIds>>+) => ReturnType<typeof cohort_listRefreshableCohortIds> = (...args) =>+ compatServiceDeps().then((deps) => cohort_listRefreshableCohortIds(deps, ...args));++export const updateCohortMembership: (+ ...args: Tail<Parameters<typeof cohort_updateCohortMembership>>+) => ReturnType<typeof cohort_updateCohortMembership> = (...args) =>+ compatServiceDeps().then((deps) => cohort_updateCohortMembership(deps, ...args));++// --- gsc -------------------------------------------------------------------------++export const completeGscOAuthCallback: (+ ...args: Tail<Parameters<typeof gsc_completeGscOAuthCallback>>+) => ReturnType<typeof gsc_completeGscOAuthCallback> = (...args) =>+ compatServiceDeps().then((deps) => gsc_completeGscOAuthCallback(deps, ...args));++export const disconnectGscConnection: (+ ...args: Tail<Parameters<typeof gsc_disconnectGscConnection>>+) => ReturnType<typeof gsc_disconnectGscConnection> = (...args) =>+ compatServiceDeps().then((deps) => gsc_disconnectGscConnection(deps, ...args));++export const getGscAiEngines: (+ ...args: Tail<Parameters<typeof gsc_getGscAiEngines>>+) => ReturnType<typeof gsc_getGscAiEngines> = (...args) =>+ compatServiceDeps().then((deps) => gsc_getGscAiEngines(deps, ...args));++export const getGscConnection: (+ ...args: Tail<Parameters<typeof gsc_getGscConnection>>+) => ReturnType<typeof gsc_getGscConnection> = (...args) =>+ compatServiceDeps().then((deps) => gsc_getGscConnection(deps, ...args));++export const getGscOverview: (+ ...args: Tail<Parameters<typeof gsc_getGscOverview>>+) => ReturnType<typeof gsc_getGscOverview> = (...args) =>+ compatServiceDeps().then((deps) => gsc_getGscOverview(deps, ...args));++export const getGscPageDetails: (+ ...args: Tail<Parameters<typeof gsc_getGscPageDetails>>+) => ReturnType<typeof gsc_getGscPageDetails> = (...args) =>+ compatServiceDeps().then((deps) => gsc_getGscPageDetails(deps, ...args));++export const getGscPages: (+ ...args: Tail<Parameters<typeof gsc_getGscPages>>+) => ReturnType<typeof gsc_getGscPages> = (...args) =>+ compatServiceDeps().then((deps) => gsc_getGscPages(deps, ...args));++export const getGscPreviousOverview: (+ ...args: Tail<Parameters<typeof gsc_getGscPreviousOverview>>+) => ReturnType<typeof gsc_getGscPreviousOverview> = (...args) =>+ compatServiceDeps().then((deps) => gsc_getGscPreviousOverview(deps, ...args));++export const getGscQueries: (+ ...args: Tail<Parameters<typeof gsc_getGscQueries>>+) => ReturnType<typeof gsc_getGscQueries> = (...args) =>+ compatServiceDeps().then((deps) => gsc_getGscQueries(deps, ...args));++export const getGscQueryDetails: (+ ...args: Tail<Parameters<typeof gsc_getGscQueryDetails>>+) => ReturnType<typeof gsc_getGscQueryDetails> = (...args) =>+ compatServiceDeps().then((deps) => gsc_getGscQueryDetails(deps, ...args));++export const getGscSearchEngines: (+ ...args: Tail<Parameters<typeof gsc_getGscSearchEngines>>+) => ReturnType<typeof gsc_getGscSearchEngines> = (...args) =>+ compatServiceDeps().then((deps) => gsc_getGscSearchEngines(deps, ...args));++export const gscGetCannibalizationCore: (+ ...args: Tail<Parameters<typeof gsc_gscGetCannibalizationCore>>+) => ReturnType<typeof gsc_gscGetCannibalizationCore> = (...args) =>+ compatServiceDeps().then((deps) => gsc_gscGetCannibalizationCore(deps, ...args));++export const gscGetOverviewCore: (+ ...args: Tail<Parameters<typeof gsc_gscGetOverviewCore>>+) => ReturnType<typeof gsc_gscGetOverviewCore> = (...args) =>+ compatServiceDeps().then((deps) => gsc_gscGetOverviewCore(deps, ...args));++export const gscGetPageDetailsCore: (+ ...args: Tail<Parameters<typeof gsc_gscGetPageDetailsCore>>+) => ReturnType<typeof gsc_gscGetPageDetailsCore> = (...args) =>+ compatServiceDeps().then((deps) => gsc_gscGetPageDetailsCore(deps, ...args));++export const gscGetQueryDetailsCore: (+ ...args: Tail<Parameters<typeof gsc_gscGetQueryDetailsCore>>+) => ReturnType<typeof gsc_gscGetQueryDetailsCore> = (...args) =>+ compatServiceDeps().then((deps) => gsc_gscGetQueryDetailsCore(deps, ...args));++export const gscGetQueryOpportunitiesCore: (+ ...args: Tail<Parameters<typeof gsc_gscGetQueryOpportunitiesCore>>+) => ReturnType<typeof gsc_gscGetQueryOpportunitiesCore> = (...args) =>+ compatServiceDeps().then((deps) => gsc_gscGetQueryOpportunitiesCore(deps, ...args));++export const gscGetTopPagesCore: (+ ...args: Tail<Parameters<typeof gsc_gscGetTopPagesCore>>+) => ReturnType<typeof gsc_gscGetTopPagesCore> = (...args) =>+ compatServiceDeps().then((deps) => gsc_gscGetTopPagesCore(deps, ...args));++export const gscGetTopQueriesCore: (+ ...args: Tail<Parameters<typeof gsc_gscGetTopQueriesCore>>+) => ReturnType<typeof gsc_gscGetTopQueriesCore> = (...args) =>+ compatServiceDeps().then((deps) => gsc_gscGetTopQueriesCore(deps, ...args));++export const listGscConnectionsForSync: (+ ...args: Tail<Parameters<typeof gsc_listGscConnectionsForSync>>+) => ReturnType<typeof gsc_listGscConnectionsForSync> = (...args) =>+ compatServiceDeps().then((deps) => gsc_listGscConnectionsForSync(deps, ...args));++export const listGscSites: (+ ...args: Tail<Parameters<typeof gsc_listGscSites>>+) => ReturnType<typeof gsc_listGscSites> = (...args) =>+ compatServiceDeps().then((deps) => gsc_listGscSites(deps, ...args));++export const resolveGscDateRange: (+ ...args: Tail<Parameters<typeof gsc_resolveGscDateRange>>+) => ReturnType<typeof gsc_resolveGscDateRange> = (...args) =>+ compatServiceDeps().then((deps) => gsc_resolveGscDateRange(deps, ...args));++export const runGscProjectBackfill: (+ ...args: Tail<Parameters<typeof gsc_runGscProjectBackfill>>+) => ReturnType<typeof gsc_runGscProjectBackfill> = (...args) =>+ compatServiceDeps().then((deps) => gsc_runGscProjectBackfill(deps, ...args));++export const runGscProjectSync: (+ ...args: Tail<Parameters<typeof gsc_runGscProjectSync>>+) => ReturnType<typeof gsc_runGscProjectSync> = (...args) =>+ compatServiceDeps().then((deps) => gsc_runGscProjectSync(deps, ...args));++export const selectGscSite: (+ ...args: Tail<Parameters<typeof gsc_selectGscSite>>+) => ReturnType<typeof gsc_selectGscSite> = (...args) =>+ compatServiceDeps().then((deps) => gsc_selectGscSite(deps, ...args));++export const syncGscData: (+ ...args: Tail<Parameters<typeof gsc_syncGscData>>+) => ReturnType<typeof gsc_syncGscData> = (...args) =>+ compatServiceDeps().then((deps) => gsc_syncGscData(deps, ...args));++// --- organization ----------------------------------------------------------------++export const cancelOrganizationDeletion: (+ ...args: Tail<Parameters<typeof organization_cancelOrganizationDeletion>>+) => ReturnType<typeof organization_cancelOrganizationDeletion> = (...args) =>+ compatServiceDeps().then((deps) => organization_cancelOrganizationDeletion(deps, ...args));++export const connectUserToOrganization: (+ ...args: Tail<Parameters<typeof organization_connectUserToOrganization>>+) => ReturnType<typeof organization_connectUserToOrganization> = (...args) =>+ compatServiceDeps().then((deps) => organization_connectUserToOrganization(deps, ...args));++export const deleteFromClickhouse: (+ ...args: Tail<Parameters<typeof organization_deleteFromClickhouse>>+) => ReturnType<typeof organization_deleteFromClickhouse> = (...args) =>+ compatServiceDeps().then((deps) => organization_deleteFromClickhouse(deps, ...args));++export const deleteOrganization: (+ ...args: Tail<Parameters<typeof organization_deleteOrganization>>+) => ReturnType<typeof organization_deleteOrganization> = (...args) =>+ compatServiceDeps().then((deps) => organization_deleteOrganization(deps, ...args));++export const deleteProjects: (+ ...args: Tail<Parameters<typeof organization_deleteProjects>>+) => ReturnType<typeof organization_deleteProjects> = (...args) =>+ compatServiceDeps().then((deps) => organization_deleteProjects(deps, ...args));++export const getInviteById: (+ ...args: Tail<Parameters<typeof organization_getInviteById>>+) => ReturnType<typeof organization_getInviteById> = (...args) =>+ compatServiceDeps().then((deps) => organization_getInviteById(deps, ...args));++export const getInviteOrThrow: (+ ...args: Tail<Parameters<typeof organization_getInviteOrThrow>>+) => ReturnType<typeof organization_getInviteOrThrow> = (...args) =>+ compatServiceDeps().then((deps) => organization_getInviteOrThrow(deps, ...args));++export const getInvites: (+ ...args: Tail<Parameters<typeof organization_getInvites>>+) => ReturnType<typeof organization_getInvites> = (...args) =>+ compatServiceDeps().then((deps) => organization_getInvites(deps, ...args));++export const getMember: (+ ...args: Tail<Parameters<typeof organization_getMember>>+) => ReturnType<typeof organization_getMember> = (...args) =>+ compatServiceDeps().then((deps) => organization_getMember(deps, ...args));++export const getMembers: (+ ...args: Tail<Parameters<typeof organization_getMembers>>+) => ReturnType<typeof organization_getMembers> = (...args) =>+ compatServiceDeps().then((deps) => organization_getMembers(deps, ...args));++export const getOrganizationBillingEventsCount: (+ ...args: Tail<Parameters<typeof organization_getOrganizationBillingEventsCount>>+) => ReturnType<typeof organization_getOrganizationBillingEventsCount> = (...args) =>+ compatServiceDeps().then((deps) => organization_getOrganizationBillingEventsCount(deps, ...args));++export const getOrganizationById: (+ ...args: Tail<Parameters<typeof organization_getOrganizationById>>+) => ReturnType<typeof organization_getOrganizationById> = (...args) =>+ compatServiceDeps().then((deps) => organization_getOrganizationById(deps, ...args));++export const getOrganizationByProjectId: (+ ...args: Tail<Parameters<typeof organization_getOrganizationByProjectId>>+) => ReturnType<typeof organization_getOrganizationByProjectId> = (...args) =>+ compatServiceDeps().then((deps) => organization_getOrganizationByProjectId(deps, ...args));++export const getOrganizationEventsCount: (+ ...args: Tail<Parameters<typeof organization_getOrganizationEventsCount>>+) => ReturnType<typeof organization_getOrganizationEventsCount> = (...args) =>+ compatServiceDeps().then((deps) => organization_getOrganizationEventsCount(deps, ...args));++export const getOrganizationEventsCountSince: (+ ...args: Tail<Parameters<typeof organization_getOrganizationEventsCountSince>>+) => ReturnType<typeof organization_getOrganizationEventsCountSince> = (...args) =>+ compatServiceDeps().then((deps) => organization_getOrganizationEventsCountSince(deps, ...args));++export const getOrganizationSubscriptionChartEndDate: (+ ...args: Tail<Parameters<typeof organization_getOrganizationSubscriptionChartEndDate>>+) => ReturnType<typeof organization_getOrganizationSubscriptionChartEndDate> = (...args) =>+ compatServiceDeps().then((deps) => organization_getOrganizationSubscriptionChartEndDate(deps, ...args));++export const getOrganizations: (+ ...args: Tail<Parameters<typeof organization_getOrganizations>>+) => ReturnType<typeof organization_getOrganizations> = (...args) =>+ compatServiceDeps().then((deps) => organization_getOrganizations(deps, ...args));++export const getSettingsForOrganization: (+ ...args: Tail<Parameters<typeof organization_getSettingsForOrganization>>+) => ReturnType<typeof organization_getSettingsForOrganization> = (...args) =>+ compatServiceDeps().then((deps) => organization_getSettingsForOrganization(deps, ...args));++export const getSettingsForProject: (+ ...args: Tail<Parameters<typeof organization_getSettingsForProject>>+) => ReturnType<typeof organization_getSettingsForProject> = (...args) =>+ compatServiceDeps().then((deps) => organization_getSettingsForProject(deps, ...args));++export const inviteUserToOrganization: (+ ...args: Tail<Parameters<typeof organization_inviteUserToOrganization>>+) => ReturnType<typeof organization_inviteUserToOrganization> = (...args) =>+ compatServiceDeps().then((deps) => organization_inviteUserToOrganization(deps, ...args));++export const removeOrganizationMember: (+ ...args: Tail<Parameters<typeof organization_removeOrganizationMember>>+) => ReturnType<typeof organization_removeOrganizationMember> = (...args) =>+ compatServiceDeps().then((deps) => organization_removeOrganizationMember(deps, ...args));++export const revokeInvite: (+ ...args: Tail<Parameters<typeof organization_revokeInvite>>+) => ReturnType<typeof organization_revokeInvite> = (...args) =>+ compatServiceDeps().then((deps) => organization_revokeInvite(deps, ...args));++export const runDeleteCron: (+ ...args: Tail<Parameters<typeof organization_runDeleteCron>>+) => ReturnType<typeof organization_runDeleteCron> = (...args) =>+ compatServiceDeps().then((deps) => organization_runDeleteCron(deps, ...args));++export const scheduleOrganizationDeletion: (+ ...args: Tail<Parameters<typeof organization_scheduleOrganizationDeletion>>+) => ReturnType<typeof organization_scheduleOrganizationDeletion> = (...args) =>+ compatServiceDeps().then((deps) => organization_scheduleOrganizationDeletion(deps, ...args));++export const updateOrganization: (+ ...args: Tail<Parameters<typeof organization_updateOrganization>>+) => ReturnType<typeof organization_updateOrganization> = (...args) =>+ compatServiceDeps().then((deps) => organization_updateOrganization(deps, ...args));++export const updateOrganizationMemberAccess: (+ ...args: Tail<Parameters<typeof organization_updateOrganizationMemberAccess>>+) => ReturnType<typeof organization_updateOrganizationMemberAccess> = (...args) =>+ compatServiceDeps().then((deps) => organization_updateOrganizationMemberAccess(deps, ...args));++// --- organization (billing serie) ------------------------------------------------++export const getOrganizationBillingEventsCountSerie: (+ ...args: Tail<Parameters<typeof organization_getOrganizationBillingEventsCountSerie>>+) => ReturnType<typeof organization_getOrganizationBillingEventsCountSerie> = (...args) =>+ compatServiceDeps().then((deps) => organization_getOrganizationBillingEventsCountSerie(deps, ...args));diff --git a/packages/core/test/buffer-fixtures.ts b/packages/core/test/buffer-fixtures.tsindex 22df0a879..8cdec5510 100644--- a/packages/core/test/buffer-fixtures.ts+++ b/packages/core/test/buffer-fixtures.ts@@ -18,8 +18,18 @@ const silentLogger: Logger = {* A buffer's boot scope, silent and never paused. Silent matters: the real* `createLogger` spawns a pino transport worker thread per buffer, and these* suites build one buffer per test.+ *+ * M10-009: `BufferDeps.ch` is the boot scope's ClickHouse client, so a suite+ * that exercises a flush hands in its own fake here rather than mocking+ * `@openpanel/db`'s module. `stubBufferDeps` leaves it unset — a buffer that+ * reaches ClickHouse without one is a bug this stub should surface, not hide.*/export const stubBufferDeps: BufferDeps = {createLogger: () => silentLogger,isCronPaused: () => Promise.resolve(false),};++/** `stubBufferDeps` plus a fake ClickHouse client for the flush path. */+export function bufferDepsWithCh(ch: unknown): BufferDeps {+ return { ...stubBufferDeps, ch: ch as BufferDeps['ch'] };+}diff --git a/packages/core/test/request-id-chart-query.test.ts b/packages/core/test/request-id-chart-query.test.tsdeleted file mode 100644index 1caf26008..000000000--- a/packages/core/test/request-id-chart-query.test.ts+++ /dev/null@@ -1,149 +0,0 @@-// ADR-018 R1, through the analytics read path (M10-003).-//-// The sibling `request-id-end-to-end.test.ts` proves the chain across the-// route, the enqueue and the job — with the whole `Services` container-// mocked. This file proves the hop that mock replaces: a REAL-// `createServices`, a real `ctx.services.chart`, and the ClickHouse call the-// chart query makes, observed on the request's own logger.-//-// It is the criterion the `loadChClient()` loaders failed silently: a-// module that reached `@openpanel/db`'s client itself got a connection with-// no request scope, so the requestId stopped at the service boundary.--import { beforeEach, expect, test } from 'bun:test';-import { Elysia } from 'elysia';-import type { AppDeps, Buffers } from '../src/context';-import { requestLogging } from '../src/http/context';-import { defineRoutes } from '../src/http/define';-import { createRecordingProducers } from '../src/jobs/testing';-import type { QueueProducerHandle } from '../src/jobs.registry';-import { type LogFn, type Logger, REQUEST_ID_HEADER } from '../src/logger';--const SUPPLIED_REQUEST_ID = 'm10-003-chart-query';-const PROJECT_ID = 'requestid-chart-project';--interface CapturedLine {- message: unknown;- bindings: Record<string, unknown>;- payload: Record<string, unknown>;-}--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,- payload:- typeof first === 'string' ? {} : (first as CapturedLine['payload']),- });- };- return {- fatal: write,- error: write,- warn: write,- info: write,- debug: write,- trace: write,- child: (extra) => bindingLogger(lines, { ...bindings, ...extra }),- };-}--const queries: Array<{ query: string }> = [];--/** Just enough of the ClickHouse client for `runQuery`: one `query` that- * answers with an empty JSONEachRow result. */-function recordingClickHouse(): AppDeps['ch'] {- return {- query: (params: { query: string }) => {- queries.push({ query: params.query });- return Promise.resolve({- json: () => Promise.resolve({ data: [], rows: 0, meta: [] }),- });- },- } as unknown as AppDeps['ch'];-}--function stubDeps() {- const lines: CapturedLine[] = [];- const producers = createRecordingProducers({});- const deps: AppDeps = {- db: {} as AppDeps['db'],- ch: recordingClickHouse(),- redis: {} as AppDeps['redis'],- clients: {} as AppDeps['clients'],- buffers: {} as Buffers,- producers: producers as unknown as QueueProducerHandle,- produceIncomingEvent: () => Promise.resolve(),- logger: bindingLogger(lines),- config: { selfHosted: false },- };- return { deps, lines };-}--// `getRetentionSeries` is the narrowest real chart query: one ClickHouse-// statement, no Postgres and no project-settings lookup, so what the test-// observes is the transport hop and nothing else.-function buildApp(deps: AppDeps) {- const routes = defineRoutes((app) =>- app.post('/proof/chart', async ({ ctx }) => ({- rows: await ctx.services.chart.getRetentionSeries({- projectId: PROJECT_ID,- }),- }))- );- return new Elysia().use(requestLogging(deps)).use(routes(deps));-}--beforeEach(() => {- queries.length = 0;-});--test("a chart query made through a request-scoped ctx logs the request's own requestId", async () => {- const { deps, lines } = stubDeps();- const response = await buildApp(deps).handle(- new Request('http://localhost/proof/chart', {- method: 'POST',- headers: { [REQUEST_ID_HEADER]: SUPPLIED_REQUEST_ID },- })- );- // onAfterResponse writes `request done` after `handle` resolves.- await new Promise((resolve) => setTimeout(resolve, 0));-- expect(response.status).toBe(200);-- // The query really ran through `deps.ch` — a silent miss would leave the- // logger assertion below trivially true (AGENTS.md).- expect(queries).toHaveLength(1);- expect(queries[0]?.query).toContain('FROM events');-- const bindingOf = (message: string) =>- lines.find((line) => line.message === message)?.bindings.requestId;-- // The ClickHouse call's own log line, and the route's, carry ONE requestId.- expect(bindingOf('query info')).toBe(SUPPLIED_REQUEST_ID);- expect(bindingOf('request done')).toBe(SUPPLIED_REQUEST_ID);-});--test('two concurrent requests do not share a requestId on their ClickHouse calls', async () => {- const { deps, lines } = stubDeps();- const app = buildApp(deps);-- const call = (requestId: string) =>- app.handle(- new Request('http://localhost/proof/chart', {- method: 'POST',- headers: { [REQUEST_ID_HEADER]: requestId },- })- );-- await Promise.all([call('req-a'), call('req-b')]);- await new Promise((resolve) => setTimeout(resolve, 0));-- const queryLineIds = lines- .filter((line) => line.message === 'query info')- .map((line) => line.bindings.requestId);- expect(queryLineIds.sort()).toEqual(['req-a', 'req-b']);-});diff --git a/packages/core/test/request-id-end-to-end.test.ts b/packages/core/test/request-id-end-to-end.test.tsindex a40e7e6ac..be3d38efc 100644--- a/packages/core/test/request-id-end-to-end.test.ts+++ b/packages/core/test/request-id-end-to-end.test.ts@@ -1,10 +1,25 @@-// ADR-007 benchmark 3 / ADR-018 R1. One request, four hops: the route's log-// line, the enqueued envelope's meta, the job handler's logger, and the enqueue-// that job makes. One requestId across all four.+// ADR-007 benchmark 3 / ADR-018 R1 — the requestId chain, end to end, on all+// three paths core actually has. One file, three sections:+//+// 1. HTTP (P2-010's original): route log -> enqueued envelope meta -> job+// handler's logger -> the enqueue that job makes. Four hops, one id.+// 2. CHART (M10-003): a REAL `ctx.services.chart` query, observed on the+// ClickHouse call's own `query info` line. This is the hop the+// `loadChClient()` loaders failed silently — a module that reached+// @openpanel/db's client itself got a connection with no request scope,+// so the id stopped at the service boundary (docs/TECH_DEBT.md §2).+// 3. KAFKA/INGEST (M10-006): the other edge. `createIncomingEventHandler` is+// the exact function apps/api's main.ts hands the consumer, and the seam+// under test — createCtx -> incomingEvent -> ctx.buffers / ctx.db /+// ctx.services — is the real one. Nothing in this section is mocked.+//+// M10-009 merged sections 2 and 3 in from their own files, so the property+// these three prove together — that no path loses the id — is one file's+// result rather than three that could drift apart.//// Each half is already covered next to its own code — `createCtx` scoping the// producers, `wrap` carrying `meta.requestId`, `runJob` reading it back. What-// no unit test can show is that the four agree, and that agreement is the+// no unit test can show is that they agree, and that agreement is the// property core's shape was chosen for: if this goes red the design is wrong,// not this file.@@ -19,12 +34,18 @@ import {type RecordedJob,} from '../src/jobs/testing';import type { QueueProducerHandle, QueueProducers } from '../src/jobs.registry';+import { queues } from '../src/jobs.registry';import {type LogFn,type Logger,REQUEST_ID_HEADER,REQUEST_ID_LENGTH,} 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';import type { ServiceDeps } from '../src/services';const SUPPLIED_REQUEST_ID = 'adr007-benchmark-3';@@ -46,18 +67,26 @@ const realServicesModule = { ...(await import('../src/services')) };// mock is a real service: built from the SCOPED ctx, logging to that request's// logger and enqueueing through that request's producers. A hand-rolled// function called from the route would skip exactly the seam under test.-const createServices = mock(- (deps: ServiceDeps) =>- ({- ingest: {- record: async (name: string) => {- deps.logger.info({ name }, 'service enqueuing');- return await proofProducers(deps.queues).proof.primary.add({ name });- },- },- }) satisfies ProofServices-);-mock.module('../src/services', () => ({ createServices }));+// M10-009: the proof's `ingest.record` is layered ON TOP of the REAL+// container rather than replacing it — sections 2 and 3 below need+// `ctx.services.chart` and `ctx.services.session` to be the real ones. The+// mock is still a real service in the sense that matters: built from the+// SCOPED ctx, logging to that request's logger and enqueueing through that+// request's producers.+const createServices = mock((deps: ServiceDeps) => ({+ ...realServicesModule.createServices(deps),+ ingest: {+ ...realServicesModule.createServices(deps).ingest,+ record: async (name: string) => {+ deps.logger.info({ name }, 'service enqueuing');+ return await proofProducers(deps.queues).proof.primary.add({ name });+ },+ },+}));+mock.module('../src/services', () => ({+ ...realServicesModule,+ createServices,+}));afterAll(() => {mock.module('../src/services', () => realServicesModule);@@ -278,3 +307,376 @@ test('a minted requestId travels the same four hops, and does not leak into theexpect(second.recorded[0]?.meta.requestId).not.toBe(minted as string);});++// ===========================================================================+// Section 2 — the chart query hop (M10-003).+// ===========================================================================++const CHART_REQUEST_ID = 'm10-003-chart-query';+const CHART_PROJECT_ID = 'requestid-chart-project';++interface ChartLine {+ message: unknown;+ bindings: Record<string, unknown>;+ payload: Record<string, unknown>;+}++function chartLogger(+ lines: ChartLine[],+ bindings: Record<string, unknown> = {}+): Logger {+ const write: LogFn = (first: unknown, second?: unknown) => {+ lines.push({+ message: typeof first === 'string' ? first : second,+ bindings,+ payload: typeof first === 'string' ? {} : (first as ChartLine['payload']),+ });+ };+ return {+ fatal: write,+ error: write,+ warn: write,+ info: write,+ debug: write,+ trace: write,+ child: (extra) => chartLogger(lines, { ...bindings, ...extra }),+ };+}++const chartQueries: Array<{ query: string }> = [];++/** Just enough of the ClickHouse client for `runQuery`: one `query` that+ * answers with an empty JSONEachRow result. */+function chartClickHouse(): AppDeps['ch'] {+ return {+ query: (params: { query: string }) => {+ chartQueries.push({ query: params.query });+ return Promise.resolve({+ json: () => Promise.resolve({ data: [], rows: 0, meta: [] }),+ });+ },+ } as unknown as AppDeps['ch'];+}++function chartDeps() {+ const lines: ChartLine[] = [];+ const producers = createRecordingProducers({});+ const deps: AppDeps = {+ db: {} as AppDeps['db'],+ ch: chartClickHouse(),+ redis: {} as AppDeps['redis'],+ clients: {} as AppDeps['clients'],+ buffers: {} as Buffers,+ producers: producers as unknown as QueueProducerHandle,+ produceIncomingEvent: () => Promise.resolve(),+ logger: chartLogger(lines),+ config: { selfHosted: false },+ };+ return { deps, lines };+}++// `getRetentionSeries` is the narrowest real chart query: one ClickHouse+// statement, no Postgres and no project-settings lookup, so what the test+// observes is the transport hop and nothing else.+function buildChartApp(deps: AppDeps) {+ const routes = defineRoutes((app) =>+ app.post('/proof/chart', async ({ ctx }) => ({+ rows: await ctx.services.chart.getRetentionSeries({+ projectId: CHART_PROJECT_ID,+ }),+ }))+ );+ return new Elysia().use(requestLogging(deps)).use(routes(deps));+}++beforeEach(() => {+ chartQueries.length = 0;+});++test("a chart query made through a request-scoped ctx logs the request's own requestId", async () => {+ const { deps, lines } = chartDeps();+ const response = await buildChartApp(deps).handle(+ new Request('http://localhost/proof/chart', {+ method: 'POST',+ headers: { [REQUEST_ID_HEADER]: CHART_REQUEST_ID },+ })+ );+ // onAfterResponse writes `request done` after `handle` resolves.+ await new Promise((resolve) => setTimeout(resolve, 0));++ expect(response.status).toBe(200);++ // The query really ran through `deps.ch` — a silent miss would leave the+ // logger assertion below trivially true (AGENTS.md).+ expect(chartQueries).toHaveLength(1);+ expect(chartQueries[0]?.query).toContain('FROM events');++ const bindingOf = (message: string) =>+ lines.find((line) => line.message === message)?.bindings.requestId;++ // The ClickHouse call's own log line, and the route's, carry ONE requestId.+ expect(bindingOf('query info')).toBe(CHART_REQUEST_ID);+ expect(bindingOf('request done')).toBe(CHART_REQUEST_ID);+});++test('two concurrent requests do not share a requestId on their ClickHouse calls', async () => {+ const { deps, lines } = chartDeps();+ const app = buildChartApp(deps);++ const call = (requestId: string) =>+ app.handle(+ new Request('http://localhost/proof/chart', {+ method: 'POST',+ headers: { [REQUEST_ID_HEADER]: requestId },+ })+ );++ await Promise.all([call('req-a'), call('req-b')]);+ await new Promise((resolve) => setTimeout(resolve, 0));++ const queryLineIds = lines+ .filter((line) => line.message === 'query info')+ .map((line) => line.bindings.requestId);+ expect(queryLineIds.sort()).toEqual(['req-a', 'req-b']);+});++// ===========================================================================+// Section 3 — the Kafka/ingest handler hop (M10-006).+// ===========================================================================++const INGEST_REQUEST_ID = 'adr018-kafka-1';+const INGEST_OTHER_REQUEST_ID = 'adr018-kafka-2';+const INGEST_PROJECT_ID = 'proj_ingest';+const INGEST_DEVICE_ID = 'device_ingest';+const INGEST_CLOSED_SESSION_ID = 'session-closed';++interface IngestLine {+ message: unknown;+ ingestBindings: Record<string, unknown>;+}++/** pino's child semantics: a child's ingestBindings ride on every line it writes. */+function ingestLogger(+ lines: IngestLine[],+ ingestBindings: Record<string, unknown> = {}+): Logger {+ const write: LogFn = (first: unknown, second?: unknown) => {+ lines.push({+ message: typeof first === 'string' ? first : second,+ ingestBindings,+ });+ };++ return {+ fatal: write,+ error: write,+ warn: write,+ info: write,+ debug: write,+ trace: write,+ child: (extra) => ingestLogger(lines, { ...ingestBindings, ...extra }),+ };+}++function makeIngestSession(id: string): IClickhouseSession {+ const now = formatClickhouseDate(new Date());+ return {+ id,+ project_id: INGEST_PROJECT_ID,+ device_id: INGEST_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 ingestEnvelope exercises the whole fan-out.+ */+function ingestDeps() {+ const lines: IngestLine[] = [];+ 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: makeIngestSession(INGEST_CLOSED_SESSION_ID),+ current: makeIngestSession('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: ingestLogger(lines),+ config: { selfHosted: false },+ };++ return { deps, lines, eventsBuffered, recorded: producers.recorded };+}++const ingestBindings: IncomingEventBindings = {+ checkNotificationRulesForEvent: () => Promise.resolve(null),+ getCachedProject: () =>+ Promise.resolve({ firstEventAt: new Date(), filters: [] }),+ clearProjectCache: () => Promise.resolve(0),+};++function ingestEnvelope(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: INGEST_PROJECT_ID,+ deviceId: INGEST_DEVICE_ID,+ sessionId: 'session-new',+ };+}++let ingestHarness: ReturnType<typeof ingestDeps>;++beforeEach(() => {+ ingestHarness = ingestDeps();+});++/** Every hop's requestId, so a failure names the hop that broke. */+function ingestTrail(lines: IngestLine[]) {+ const bindingOf = (message: string) =>+ lines.find((line) => line.message === message)?.ingestBindings;++ 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(+ ingestHarness.deps,+ ingestBindings+ );++ await handleEvent(ingestEnvelope(INGEST_REQUEST_ID), {+ partition: 3,+ offset: '4711',+ });++ // Hop 1: the child logger the handler writes its per-event lines through.+ expect(ingestTrail(ingestHarness.lines)).toEqual({+ createEventLog: INGEST_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(ingestHarness.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(+ ingestHarness.recorded.map(({ queue, job, meta }) => ({+ queue,+ job,+ requestId: meta.requestId,+ }))+ ).toEqual([+ {+ queue: 'sessions',+ job: 'session',+ requestId: INGEST_REQUEST_ID,+ },+ ]);+});++test('a second message is scoped to its own id, and an envelope without one still gets scoped', async () => {+ const handleEvent = createIncomingEventHandler(+ ingestHarness.deps,+ ingestBindings+ );+ const meta = { partition: 0, offset: '1' };++ await handleEvent(ingestEnvelope(INGEST_REQUEST_ID), meta);+ await handleEvent(ingestEnvelope(INGEST_OTHER_REQUEST_ID), meta);+ await handleEvent(ingestEnvelope(), meta);++ const enqueued = ingestHarness.recorded.map(+ ({ meta: jobMeta }) => jobMeta.requestId+ );+ expect(enqueued.slice(0, 2)).toEqual([+ INGEST_REQUEST_ID,+ INGEST_OTHER_REQUEST_ID,+ ]);++ const minted = enqueued[2];+ expect(minted).toBeString();+ expect(minted).not.toBe(INGEST_REQUEST_ID);+ expect(minted).not.toBe(INGEST_OTHER_REQUEST_ID);+});diff --git a/packages/core/test/request-id-ingest.test.ts b/packages/core/test/request-id-ingest.test.tsdeleted file mode 100644index 6a3697d91..000000000--- a/packages/core/test/request-id-ingest.test.ts+++ /dev/null@@ -1,253 +0,0 @@-// 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);-});