Skip to content

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

ralph(M10-001): dependency-cruiser: core-uses-ctx-not-db-internals + core-no-self-barrel, landed as warn with the violation counts recorded

ralpha51cbaa31e52
diff --git a/.dependency-cruiser.cjs b/.dependency-cruiser.cjs
index 8c1bb8dd8..17c684987 100644
--- a/.dependency-cruiser.cjs
+++ b/.dependency-cruiser.cjs
@@ -91,6 +91,60 @@
*/
module.exports = {
forbidden: [
+ {
+ name: 'core-uses-ctx-not-db-internals',
+ severity: 'warn',
+ 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 " +
+ "['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.',
+ from: {
+ path: '^packages/core/src/',
+ pathNot: '^packages/core/src/context\\.ts$',
+ },
+ to: {
+ path: '^packages/db/',
+ dependencyTypesNot: ['type-only'],
+ },
+ },
+ {
+ name: 'core-no-self-barrel',
+ severity: 'warn',
+ 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/' },
+ to: {
+ path: '^packages/core/src/index\\.ts$',
+ dependencyTypesNot: ['type-only'],
+ },
+ },
{
name: 'constants-stay-isomorphic',
severity: 'error',
diff --git a/docs/TECH_DEBT.md b/docs/TECH_DEBT.md
new file mode 100644
index 000000000..15c1b3cfa
--- /dev/null
+++ b/docs/TECH_DEBT.md
@@ -0,0 +1,106 @@
+# Tech debt register — dependency-cruiser baseline log
+
+The full remediation narrative (why `packages/core` ended up bypassing
+`ctx.db`/`ctx.ch` and importing its own barrel) lives in the controller repo
+at `docs/TECH_DEBT.md` — that is the source of truth for sections 1-5 (the
+"Deliberate", "Drift", "Planned remediation" and Carl's 2026-09-05 review).
+This file is the in-repo log of the dependency-cruiser rule that enforces
+that remediation (`docs/TECH_DEBT.md` §4 step 4, §5a), so the numbers below
+are re-measurable from this repo without controller access.
+
+## Re-measured
+
+### 2026-09-05 — M10-001, dependency-cruiser rules landed at `warn`
+
+Ran by: ralph (M10-001 implement task).
+Command: `pnpm run check:deps` (`.dependency-cruiser.cjs`), each new rule set
+to `severity: 'error'` in isolation for the count, then both reset to `warn`
+before commit.
+
+Scope of both rules: every file under `packages/core/src/**` (production and
+test files alike — the rule text in `.dependency-cruiser.cjs` does not carve
+tests out; see the note below on why some of these are expected to persist).
+
+| Rule | Violations (error, isolated run) | Breakdown |
+|---|---:|---|
+| `core-uses-ctx-not-db-internals` | **161** | 83 dynamic (`import('@openpanel/db/...')`) + 78 static (`from '@openpanel/db/...'`) edges, via `--output-type json`'s `dependencyTypes`/`dynamic` fields |
+| `core-no-self-barrel` | **96** | value-imports of `packages/core/src/index.ts` (the `"."` export target for the bare `@openpanel/core` specifier) from a sibling file under `packages/core/src/**` |
+
+Both were confirmed to fire with a real count (not vacuous): the same run
+with both rules reset to `warn` exits `0` (`x 257 dependency violations (0
+errors, 257 warnings)`), and the two ADR-008 rules
+(`constants-stay-isomorphic`, `frontend-values-only-constants`) reported 0
+violations throughout, at their unchanged `error` severity.
+
+**Numbers vs. the controller doc's 2026-09-05 measurement (`grep`-based, 63
+dynamic + 80 static, non-test files only):** roughly consistent. The
+difference is methodology, not drift — the grep baseline excluded test
+files and dependency-cruiser here does not, and a handful of test files use
+the ADR-010 `mock.module('@openpanel/db/...')` idiom (dynamic `import()` of
+the *mocked* specifier inside `beforeAll`), which is the sanctioned test
+pattern, not the production-code drift `core-uses-ctx-not-db-internals`
+exists to catch. Restricting the JSON breakdown to non-test files alone
+gives 57 dynamic + 75 static, closer to the controller doc's figures.
+Deciding whether the rule itself should exclude test files (and how
+`M10-009`'s flip to `error` treats the legitimate mock-module imports) is
+left to a later M10 task — this task only proves the rule fires and records
+the baseline, per its acceptance criteria.
+
+`context.ts`'s `typeof import('@openpanel/db/...')` type queries
+(`Db`/`ClickHouseClient`) do **not** count: verified via
+`--output-type json` that dependency-cruiser tags them `['undetermined',
+'type-import']`, a genuinely type-level reference distinct from the
+`'type-only'` tag `import type {...}` statements get. `dependencyTypesNot`
+on `core-uses-ctx-not-db-internals` exempts both tags, so no per-file
+exception entry was needed for `context.ts`.
+
+### 2026-09-05 — M10-001 fix, `core-uses-ctx-not-db-internals` re-measured (161 → 167)
+
+Ran by: ralph (M10-001 rework, after the independent reviewer rejected the
+first attempt above).
+
+The first attempt's blanket `dependencyTypesNot: ['type-only', 'type-import']`
+was wrong: it exempted the `'type-import'` tag package-wide instead of naming
+`context.ts` in the rule's exception list, and that hid a real composition
+seam. `packages/core/src/buffers/clickhouse.ts` has a `typeof import(...)`
+type query (line 8, genuinely type-only on its own) *and* a real runtime
+`import(...)` dynamic import of the same resolved module (line 13, the
+deliberate lazy-load-to-keep-`bun-test`-offline pattern). Confirmed with an
+isolated probe file mirroring that exact structure, via `--output-type json`:
+dependency-cruiser merges the two references between the same pair of files
+into **one** edge and the merge drops the `'dynamic-import'` tag entirely,
+leaving only `['undetermined', 'type-import']` — indistinguishable, by tag
+alone, from `context.ts`'s pure type queries. The same merge silently hid the
+identical pattern in five test files that use the ADR-010
+`mock.module('@openpanel/db/...')` idiom (a `typeof import(...)` alias
+declared once, then `await import(...)`'d for real inside `beforeAll`):
+`chart.service.test.ts`, `chart.sql.test.ts`, `funnel.sql.test.ts`,
+`overview.sql.test.ts`, `pages.sql.test.ts` (all under
+`packages/core/src/modules/chart|overview/`).
+
+Fix: `dependencyTypesNot` on the rule's `to` now reads `['type-only']` only,
+matching the idiom the other three rules in this file already use.
+`context.ts` is excluded by `pathNot` on the rule's `from` instead — named
+explicitly, as the acceptance criteria requires — since it is the one file
+under `packages/core/src/**` that references `@openpanel/db` exclusively
+through type queries with no accompanying value import to merge with.
+
+Command: `pnpm run check:deps` (`.dependency-cruiser.cjs`), `core-uses-ctx-not-db-internals`
+set to `severity: 'error'` in isolation (`core-no-self-barrel` left at `warn`).
+
+| Rule | Violations (error, isolated run) | Breakdown |
+|---|---:|---|
+| `core-uses-ctx-not-db-internals` | **167** (was 161) | 83 dynamic (`dependencyTypes` includes `'dynamic-import'`) + 78 static (plain `import`/`esm`) + **6 newly caught** merged edges tagged `['undetermined', 'type-import']` that are real value imports (`buffers/clickhouse.ts` + the 5 test files above), via `--output-type json`'s `dependencyTypes` field |
+| `core-no-self-barrel` | **96** (unchanged) | not touched by this fix |
+
+Diffed the violation edge list (`(from, to)` pairs) between the old rule
+config and the fixed one: exactly those 6 edges were added, zero were
+removed or changed — the fix is strictly additive detection, not a
+re-classification of the existing 161.
+
+Both rules reset to `warn` after measuring: `pnpm run check:deps` exits `0`
+(`x 263 dependency violations (0 errors, 263 warnings)`). The two ADR-008
+rules still report 0 violations at their unchanged `error` severity.
+`pnpm run typecheck` and `cd packages/core && bun test --isolate` (1447
+pass, 0 fail) both pass unaffected — this task only changes the lint config
+and this doc, not any source file.