OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.
ralph(M15-006): R7/R17: every process.env read in core moves to the config loader, and the loader gains the shape ADR-022 R17 specifies
ralphd7005eb0d149
diff --git a/apps/api/e2e/lag-monitor.ts b/apps/api/e2e/lag-monitor.tsindex 904e391eb..bd6a50592 100644--- a/apps/api/e2e/lag-monitor.ts+++ b/apps/api/e2e/lag-monitor.ts@@ -10,10 +10,13 @@ import {type Admin,type ConsumerGroupLag,createKafkaAdmin,- KAFKA_CONSUMER_GROUP,- KAFKA_EVENTS_TOPIC,sampleConsumerGroupLag,} from '@openpanel/core';+import { loadConfig } from '../src/config/env';++// The Kafka topic and group come from the same loader the API boots on, so a+// run that points the stack at a non-default topic samples the same one.+const kafkaConfig = loadConfig().core;export interface LagSummary {sampleCount: number;@@ -31,13 +34,13 @@ export class LagMonitor {private connected = false;constructor(- private readonly topic: string = KAFKA_EVENTS_TOPIC,- private readonly groupId: string = KAFKA_CONSUMER_GROUP+ private readonly topic: string = kafkaConfig.kafka.eventsTopic,+ private readonly groupId: string = kafkaConfig.kafka.consumerGroup) {}async start(intervalMs = 1000): Promise<void> {try {- this.admin = createKafkaAdmin();+ this.admin = createKafkaAdmin(kafkaConfig);await this.admin.connect();this.connected = true;} catch (error) {diff --git a/apps/api/e2e/legacy-job-proof.ts b/apps/api/e2e/legacy-job-proof.tsindex 388202f56..2f689a2b1 100644--- a/apps/api/e2e/legacy-job-proof.ts+++ b/apps/api/e2e/legacy-job-proof.ts@@ -42,6 +42,7 @@ import {} from '@openpanel/core';import { Redis } from '@openpanel/redis';import pino from 'pino';+import { loadConfig } from '../src/config/env';/** Isolates the proof from the shared dev Redis — see the header. */const NAMESPACE = 'm9001proof';@@ -273,9 +274,12 @@ async function main() {logger,});+ const config = loadConfig();+const buffers = createBuffers({createLogger: (name) => logger.child({ name }),isCronPaused: async () => false,+ config: config.core,// Never flushed: this proof asserts BullMQ keys, not ClickHouse rows.ch: undefined as unknown as AppDeps['ch'],});@@ -288,7 +292,7 @@ async function main() {buffers,producers,logger,- config: { selfHosted: false },+ config: config.core,};process.stdout.write(diff --git a/apps/api/src/config/env.test.ts b/apps/api/src/config/env.test.tsindex 86a945e5c..ec00a3ad0 100644--- a/apps/api/src/config/env.test.ts+++ b/apps/api/src/config/env.test.ts@@ -126,3 +126,219 @@ describe('the boot flags main.ts branches on', () => {).toThrow(/unknown value "nope"[\s\S]*unknown queue "alsonope"/);});});++// --- M15-006: the parsing core used to do for itself -------------------------++describe('blank KEY= is absent, not empty', () => {+ it('falls through to the default on every field, not just the preprocessed ones', () => {+ const config = loadConfig({ ...base, API_PORT: '', LOG_LEVEL: ' ' });+ expect(config.API_PORT).toBe(3000);+ expect(config.LOG_LEVEL).toBe('info');+ expect(config.SHUTDOWN_FORCE_EXIT_MS).toBe(20_000);+ });++ it('treats a blank SELF_HOSTED as unset rather than as the string ""', () => {+ expect(loadConfig({ ...base, SELF_HOSTED: '' }).core.selfHosted).toBe(+ false+ );+ expect(loadConfig({ ...base, SELF_HOSTED: 'true' }).core.selfHosted).toBe(+ true+ );+ // One truthiness rule tree-wide: `1` counts, anything else does not.+ expect(loadConfig({ ...base, SELF_HOSTED: '1' }).core.selfHosted).toBe(+ true+ );+ expect(loadConfig({ ...base, SELF_HOSTED: 'yes' }).core.selfHosted).toBe(+ false+ );+ });+});++describe('tuning knobs keep the module default on a malformed value', () => {+ const malformed = ['0', '-1', '7.5', '7days', 'junk', ''];++ it('EVENT_LIST_MAX_LOOKBACK_DAYS / SESSION_LIST_MAX_LOOKBACK_DAYS', () => {+ for (const bad of malformed) {+ const config = loadConfig({+ ...base,+ EVENT_LIST_MAX_LOOKBACK_DAYS: bad,+ SESSION_LIST_MAX_LOOKBACK_DAYS: bad,+ });+ expect(config.core.query.eventListMaxLookbackDays).toBeUndefined();+ expect(config.core.query.sessionListMaxLookbackDays).toBeUndefined();+ }++ const config = loadConfig({+ ...base,+ EVENT_LIST_MAX_LOOKBACK_DAYS: '7',+ SESSION_LIST_MAX_LOOKBACK_DAYS: '3650',+ });+ expect(config.core.query.eventListMaxLookbackDays).toBe(7);+ expect(config.core.query.sessionListMaxLookbackDays).toBe(3650);+ });++ it('reads each list its own variable — no cross-talk', () => {+ const config = loadConfig({ ...base, EVENT_LIST_MAX_LOOKBACK_DAYS: '7' });+ expect(config.core.query.eventListMaxLookbackDays).toBe(7);+ expect(config.core.query.sessionListMaxLookbackDays).toBeUndefined();+ });++ it('COHORT_QUERY_MEMORY_LIMIT_BYTES / COHORT_QUERY_SPILL_BYTES', () => {+ const config = loadConfig({+ ...base,+ COHORT_QUERY_MEMORY_LIMIT_BYTES: '2gb',+ COHORT_QUERY_SPILL_BYTES: '-1',+ });+ expect(config.core.query.cohortQueryMemoryLimitBytes).toBeUndefined();+ expect(config.core.query.cohortQuerySpillBytes).toBeUndefined();+ });+});++describe('derived values, computed once in the transform', () => {+ it('DASHBOARD_URL wins over NEXT_PUBLIC_DASHBOARD_URL; neither means empty', () => {+ expect(loadConfig(base).core.dashboardUrl).toBe('');+ expect(+ loadConfig({ ...base, NEXT_PUBLIC_DASHBOARD_URL: 'https://next' }).core+ .dashboardUrl+ ).toBe('https://next');+ expect(+ loadConfig({+ ...base,+ DASHBOARD_URL: 'https://own',+ NEXT_PUBLIC_DASHBOARD_URL: 'https://next',+ }).core.dashboardUrl+ ).toBe('https://own');+ });++ it('clickhouseClustered is CLICKHOUSE_CLUSTER, else the inverse of SELF_HOSTED', () => {+ expect(loadConfig(base).core.clickhouseClustered).toBe(true);+ expect(+ loadConfig({ ...base, SELF_HOSTED: 'true' }).core.clickhouseClustered+ ).toBe(false);+ expect(+ loadConfig({ ...base, SELF_HOSTED: 'true', CLICKHOUSE_CLUSTER: '1' }).core+ .clickhouseClustered+ ).toBe(true);+ });++ it('the CORS allowlist, the verbose client ids and the listen address', () => {+ const config = loadConfig({+ ...base,+ DASHBOARD_URL: 'https://own',+ API_CORS_ORIGINS: ' https://a , https://b ',+ ENABLE_VERBOSE_LOGGING: 'c1,c2',+ API_HOST: '127.0.0.1',+ API_PORT: '4000',+ });+ expect(config.dashboardOrigins).toEqual([+ 'https://own',+ 'https://a',+ 'https://b',+ ]);+ expect(config.verboseClientIds).toEqual(['c1', 'c2']);+ expect(config.listen).toEqual({ port: 4000, hostname: '127.0.0.1' });+ // No hostname unless API_HOST says one — Bun's `localhost` binds IPv6 only.+ expect(loadConfig(base).listen).toEqual({ port: 3000 });+ });++ it('the DLQ topic follows the events topic unless it is named', () => {+ expect(loadConfig(base).core.kafka.eventsDlqTopic).toBe('events-dlq');+ expect(+ loadConfig({ ...base, KAFKA_EVENTS_TOPIC: 'evt' }).core.kafka+ .eventsDlqTopic+ ).toBe('evt-dlq');+ expect(+ loadConfig({ ...base, KAFKA_EVENTS_DLQ_TOPIC: 'own' }).core.kafka+ .eventsDlqTopic+ ).toBe('own');+ });++ it('the log exporter and the stdout interception verdict', () => {+ expect(loadConfig(base).core.logging).toMatchObject({+ exporter: 'stdout',+ interceptProcessOutput: false,+ });+ expect(+ loadConfig({ ...base, HYPERDX_API_KEY: 'k' }).core.logging+ ).toMatchObject({ exporter: 'otlp', interceptProcessOutput: true });+ expect(+ loadConfig({ ...base, NODE_ENV: 'production' }).core.logging+ ).toMatchObject({ exporter: 'stdout', interceptProcessOutput: true });+ });++ it('ALLOW_REGISTRATION keeps "unset" distinct from "false"', () => {+ expect(loadConfig(base).core.auth.allowRegistration).toBeUndefined();+ expect(+ loadConfig({ ...base, ALLOW_REGISTRATION: 'false' }).core.auth+ .allowRegistration+ ).toBe(false);+ expect(+ loadConfig({ ...base, ALLOW_REGISTRATION: 'true' }).core.auth+ .allowRegistration+ ).toBe(true);+ });+});++describe('cross-field invariants, checked before the transform', () => {+ it('refuses a consuming role with nothing to consume', () => {+ expect(() => loadConfig({ ROLE: 'worker', ENABLED_QUEUES: ' , ' })).toThrow(+ /consumes queues but ENABLED_QUEUES names none/+ );+ // ROLE=api never consumes, so the same env is fine for it.+ expect(() =>+ loadConfig({ ROLE: 'api', ENABLED_QUEUES: ' , ' })+ ).not.toThrow();+ });++ it('refuses LOG_EXPORTER=otlp without a HyperDX key', () => {+ expect(() => loadConfig({ ...base, LOG_EXPORTER: 'otlp' })).toThrow(+ /LOG_EXPORTER=otlp requires HYPERDX_API_KEY/+ );+ expect(() =>+ loadConfig({ ...base, LOG_EXPORTER: 'otlp', HYPERDX_API_KEY: 'k' })+ ).not.toThrow();+ });++ it('refuses a Kafka heartbeat that cannot fit inside the session timeout', () => {+ expect(() =>+ loadConfig({+ ...base,+ KAFKA_SESSION_TIMEOUT_MS: '1000',+ KAFKA_HEARTBEAT_INTERVAL_MS: '1000',+ })+ ).toThrow(/must be below KAFKA_SESSION_TIMEOUT_MS/);+ });++ it('refuses a reaper deadman below the session idle window', () => {+ expect(() =>+ loadConfig({+ ...base,+ SESSION_TIMEOUT_MS: '60000',+ SESSION_REAPER_WALLCLOCK_DEADMAN_MS: '1000',+ })+ ).toThrow(/is below SESSION_TIMEOUT_MS/);+ });++ it('refuses a vacuum threshold that would race the reaper', () => {+ expect(() =>+ loadConfig({+ ...base,+ SESSION_TIMEOUT_MS: '60000',+ SESSION_VACUUM_STALE_THRESHOLD_MS: '60000',+ })+ ).toThrow(/must exceed the reaper deadman/);+ });++ it('refuses a profile-backfill project list with the flag off', () => {+ expect(() =>+ loadConfig({ ...base, EXPERIMENTAL_PROFILE_BACKFILL_PROJECTS: 'p1' })+ ).toThrow(/the allowlist has no effect/);+ expect(() =>+ loadConfig({+ ...base,+ EXPERIMENTAL_PROFILE_BACKFILL: '1',+ EXPERIMENTAL_PROFILE_BACKFILL_PROJECTS: 'p1',+ })+ ).not.toThrow();+ });+});diff --git a/apps/api/src/config/env.ts b/apps/api/src/config/env.tsindex 08786f8a5..069e52048 100644--- a/apps/api/src/config/env.ts+++ b/apps/api/src/config/env.ts@@ -1,15 +1,33 @@-// The sole `process.env` reader on the V2 boot path (main.ts + core wiring),-// per TARGET_ARCHITECTURE §7 "apps/api — three files". `packages/db` and-// `packages/redis` keep reading their own env until they grow factories —-// an accepted pragmatic deviation (ADR-007 §7) — so this catalogue covers-// only what `main.ts` itself reads today: TZ, ROLE, NODE_ENV, LOG_LEVEL,-// API_PORT, API_HOST, SELF_HOSTED, QUEUE_CLUSTER, QUEUE_NAMESPACE,-// ENABLED_QUEUES, the seven `<QUEUE>_CONCURRENCY` overrides, DISABLE_WORKERS,-// DISABLE_BULLBOARD and SHUTDOWN_FORCE_EXIT_MS.+// The sole `process.env` READER on the V2 boot path, and since M15-006 that is+// true rather than aspirational: `packages/core` reads none (ADR-022 R7), so+// every variable core used to read itself is parsed here and travels down as+// `AppDeps.config` (`CoreConfig`). The one other `process.env` touch on the+// path is `main.ts`'s `process.env.TZ = 'UTC'` write, which sets the process's+// timezone rather than reading configuration. `packages/db` and+// `packages/redis` keep reading their own connection env until they grow+// factories — an accepted pragmatic deviation (ADR-007 §7).//-// Invalid config fails boot loudly, with every issue reported at once —-// never a fail-fast on the first bad var.+// The shape is ADR-022 R17's, in this order:+// 1. one zod object over the raw environment;+// 2. every cross-field invariant as a `.superRefine` BEFORE the transform,+// so a contradiction is named at boot rather than discovered at runtime;+// 3. one `.transform` that computes every derived value — URLs, namespaces,+// IS_PRODUCTION, the listen address, the concurrency map — so nothing+// downstream re-derives one;+// 4. `loadConfig(source = process.env)`, so a test hands in a minimal object.+//+// A blank `KEY=` is absent everywhere: `blankToUndefined` runs in front of+// every field, so `.default()` fires on an empty value exactly as it does on+// an unset one.+//+// Invalid config fails boot loudly, with every issue reported at once — never+// a fail-fast on the first bad var. Two classes, deliberately different:+// - boot-critical values (roles, queues, Kafka wiring) FAIL;+// - tuning knobs (batch sizes, limits, TTLs) fall back to the module's own+// documented default on a malformed value, which is what the modules did+// when they parsed these themselves.+import type { CoreConfig } from '@openpanel/core';import { queues } from '@openpanel/core';import { z } from 'zod';@@ -19,10 +37,33 @@ export type Role = (typeof ROLE_VALUES)[number];const DEFAULT_API_PORT = 3000;const DEFAULT_LOG_LEVEL = 'info';const TRUE_STRING = 'true';+const ONE_STRING = '1';+const FALSE_STRING = 'false';const PRODUCTION = 'production';+const DEVELOPMENT = 'development';+/** pino's own service-name suffix when NODE_ENV is unset. */+const DEFAULT_SERVICE_ENVIRONMENT = 'dev';/** V1's worker deadline (apps/worker/src/boot-workers.ts). */const DEFAULT_SHUTDOWN_FORCE_EXIT_MS = 20_000;+const DEFAULT_KAFKA_CLIENT_ID = 'openpanel';+const DEFAULT_KAFKA_EVENTS_TOPIC = 'events';+const DEFAULT_KAFKA_CONSUMER_GROUP = 'openpanel-events';+const DEFAULT_KAFKA_PARTITIONS_CONCURRENT = 8;+const DEFAULT_KAFKA_MIN_MESSAGES = 1;+const DEFAULT_KAFKA_MAX_WAIT_MS = 500;+const DEFAULT_KAFKA_MAX_MESSAGES_PER_PARTITION = 256;+const DEFAULT_KAFKA_SESSION_TIMEOUT_MS = 30_000;+const DEFAULT_KAFKA_HEARTBEAT_INTERVAL_MS = 3000;+const DEFAULT_KAFKA_REQUEST_TIMEOUT_MS = 5000;+const DEFAULT_KAFKA_CONNECTION_TIMEOUT_MS = 2000;+const DEFAULT_KAFKA_PRODUCER_RETRIES = 2;+const DEFAULT_KAFKA_PRODUCER_INITIAL_RETRY_MS = 100;+const DEFAULT_KAFKA_PRODUCER_MAX_RETRY_MS = 1000;+const DEFAULT_KAFKA_HANDLER_MAX_ATTEMPTS = 3;+const DEFAULT_KAFKA_HANDLER_RETRY_INITIAL_MS = 100;+const DEFAULT_KAFKA_HANDLER_RETRY_MAX_MS = 1000;+/*** The Kafka events consumer's token. Renamed from `events_kafka` — there is* one events transport now, so there is one token for it (ADR-004 rec 5/6,@@ -52,7 +93,7 @@ export function concurrencyEnvKey(queueName: string): string {return `${queueName.toUpperCase().replace(NON_ENV_KEY_CHARS, '_')}_CONCURRENCY`;}-// An unset or blank var should fall through to the default, matching the+// An unset or blank var falls through to the default, matching the// `value?.trim() || fallback` idiom the ported reads used before this file// existed — `.default()` alone only fires on `undefined`, not `""`.function blankToUndefined(value: unknown): unknown {@@ -63,6 +104,98 @@ function blankToUndefined(value: unknown): unknown {return trimmed === '' ? undefined : trimmed;}+function splitTokens(value: string | undefined): string[] {+ if (value === undefined) {+ return [];+ }+ return value+ .split(',')+ .map((token) => token.trim())+ .filter(Boolean);+}++// --- field schemas ----------------------------------------------------------++const optionalString = z.preprocess(blankToUndefined, z.string().optional());++const stringWithDefault = (fallback: string) =>+ z.preprocess(blankToUndefined, z.string().default(fallback));++/** `KEY=true` or `KEY=1`. The two spellings V1 accepted interchangeably. */+const trueOrOneSchema = z.preprocess(+ blankToUndefined,+ z+ .string()+ .optional()+ .transform((value) => value === TRUE_STRING || value === ONE_STRING)+);++/** Any defined value means on — V1's spelling for DISABLE_WORKERS. */+const definedIsTrueSchema = z.preprocess(+ blankToUndefined,+ z+ .string()+ .optional()+ .transform((value) => value !== undefined)+);++const ZERO_STRING = '0';++/** On unless explicitly switched off with `false` or `0`. */+const onUnlessDisabledSchema = z.preprocess(+ blankToUndefined,+ z+ .string()+ .optional()+ .transform((value) => value !== FALSE_STRING && value !== ZERO_STRING)+);++/** On unless explicitly `0` — the session reaper/vacuum kill switches. */+const onUnlessZeroSchema = z.preprocess(+ blankToUndefined,+ z+ .string()+ .optional()+ .transform((value) => value !== ZERO_STRING)+);++/**+ * A tuning knob: a positive integer, or `undefined` when unset OR malformed.+ * Malformed keeps the module's own default rather than failing boot — `-1`,+ * `0` and `5000junk` all land on `undefined`, which is what the hand-rolled+ * `parsePositiveInt` guards in `cohort.service.ts` and `lookback.ts` did.+ */+const optionalPositiveInt = z.preprocess(+ blankToUndefined,+ z.coerce.number().int().positive().optional().catch(undefined)+);++/** A boot-critical integer: a bad value fails boot instead of becoming NaN. */+const positiveIntWithDefault = (fallback: number) =>+ z.preprocess(+ blankToUndefined,+ z.coerce.number().int().positive().default(fallback)+ );++/** Comma-separated list; `undefined` when unset so the caller's order stands. */+const optionalTokenList = z.preprocess(+ blankToUndefined,+ z+ .string()+ .optional()+ .transform((value) =>+ value === undefined ? undefined : splitTokens(value)+ )+);++const tokenList = z.preprocess(+ blankToUndefined,+ z+ .string()+ .optional()+ .transform((value) => splitTokens(value))+);+/*** Same doctrine as `ENABLED_QUEUES` (apps/worker/src/boot-workers.ts's* `assertKnownQueue`): a stale or mistyped value fails boot loudly, naming@@ -81,9 +214,8 @@ const roleSchema = z.preprocess(});}})- // Refine before transform (TARGET_ARCHITECTURE §7): the cast is only- // ever observed once the refinement above has already accepted the- // value.+ // Refine before transform (ADR-022 R17): the cast is only ever observed+ // once the refinement above has already accepted the value..transform((value) => value as Role));@@ -121,62 +253,50 @@ const enabledQueuesSchema = z.preprocess());-function splitTokens(value: string | undefined): string[] {- if (value === undefined) {- return [];- }- return value- .split(',')- .map((token) => token.trim())- .filter(Boolean);-}--/** V1 treated any defined value as "off" for workers and only `'1'`/`'true'`- * as "off" for bull-board. Both spellings port as-is. */-const definedIsTrueSchema = z.preprocess(- blankToUndefined,- z- .string()- .optional()- .transform((value) => value !== undefined)-);--const oneOrTrueSchema = z.preprocess(- blankToUndefined,- z- .string()- .optional()- .transform((value) => value === '1' || value === TRUE_STRING)-);-/** One `<QUEUE>_CONCURRENCY` slot per registry queue. V1 ignored a* non-numeric or non-positive value and kept the default; so does this. */-const concurrencySchema = z.preprocess(- blankToUndefined,- z.coerce.number().int().positive().optional().catch(undefined)-);-const concurrencyShape = Object.fromEntries(Object.keys(queues).map((name) => [concurrencyEnvKey(name),- concurrencySchema,+ optionalPositiveInt,])-) as Record<string, typeof concurrencySchema>;+) as Record<string, typeof optionalPositiveInt>;-const envSchema = z.object({+const rawSchema = z.object({...concurrencyShape,++ // --- process / boot ---ROLE: roleSchema,- NODE_ENV: z.preprocess(blankToUndefined, z.string().optional()),- LOG_LEVEL: z.preprocess(+ NODE_ENV: optionalString,+ LOG_LEVEL: stringWithDefault(DEFAULT_LOG_LEVEL),+ API_PORT: z.preprocess(blankToUndefined,- z.string().default(DEFAULT_LOG_LEVEL)+ z.coerce.number().int().min(0).default(DEFAULT_API_PORT)),- API_PORT: z.coerce.number().int().min(0).default(DEFAULT_API_PORT),- SELF_HOSTED: z- .string()- .optional()- .default('false')- .transform((value) => value === TRUE_STRING),+ /**+ * Left UNSET means Bun's own default, `0.0.0.0`.+ *+ * Deliberate deviation from V1, which defaulted to `localhost` outside+ * production (apps/api/src/index.ts): `Bun.serve({hostname:'localhost'})`+ * binds `::1` ONLY, so a dev box's `127.0.0.1` request is refused. Making+ * the loopback-only bind explicit rather than the accidental default is+ * what keeps `bun run src/main.ts` reachable the way `node dist/index.js`+ * was.+ */+ API_HOST: optionalString,+ /**+ * One truthiness rule for the whole tree (M15-006). Core used to read this+ * three different ways — `=== 'true'`, `'true' || '1'`, and any truthy+ * string — so a `SELF_HOSTED=1` deployment got the SSRF guard dropped but+ * kept the cloud-only cron jobs. `true` or `1`, everywhere.+ */+ SELF_HOSTED: trueOrOneSchema,+ SHUTDOWN_FORCE_EXIT_MS: z.preprocess(+ blankToUndefined,+ z.coerce.number().int().positive().default(DEFAULT_SHUTDOWN_FORCE_EXIT_MS)+ ),++ // --- queues ---/*** Redis Cluster hash-tags every queue key (`cron` -> `{cron}`). Read here* because `main.ts` hands it to `createProducers`, and it decides a Redis@@ -185,44 +305,26 @@ const envSchema = z.object({* (`packages/queue/src/queues.ts` `getQueueName`); a blank value is unset,* exactly as before.*/- QUEUE_CLUSTER: z.preprocess(- blankToUndefined,- z- .string()- .optional()- .transform((value) => value !== undefined)- ),+ QUEUE_CLUSTER: definedIsTrueSchema,/*** Isolates two deployments — or a proof run and the dev box — sharing one* Redis (`queueKey`'s `namespace`). UNSET in every real deployment: setting* it renames every queue key, which orphans the jobs already in Redis.*/- QUEUE_NAMESPACE: z.preprocess(blankToUndefined, z.string().optional()),+ QUEUE_NAMESPACE: optionalString,ENABLED_QUEUES: enabledQueuesSchema,DISABLE_WORKERS: definedIsTrueSchema,- DISABLE_BULLBOARD: oneOrTrueSchema,- /**- * Left UNSET means Bun's own default, `0.0.0.0`.- *- * Deliberate deviation from V1, which defaulted to `localhost` outside- * production (apps/api/src/index.ts): `Bun.serve({hostname:'localhost'})`- * binds `::1` ONLY, so a dev box's `127.0.0.1` request is refused. Making- * the loopback-only bind explicit rather than the accidental default is- * what keeps `bun run src/main.ts` reachable the way `node dist/index.js`- * was.- */- API_HOST: z.preprocess(blankToUndefined, z.string().optional()),+ DISABLE_BULLBOARD: trueOrOneSchema,++ // --- HTTP surface ---/*** The CORS delegator's allowlist (apps/api/src/app.ts:107-110): the* dashboard's own origin plus any extra comma-separated ones. Read once at* boot, exactly as V1 did — changing an origin has always needed a restart.*/- DASHBOARD_URL: z.preprocess(blankToUndefined, z.string().optional()),- NEXT_PUBLIC_DASHBOARD_URL: z.preprocess(- blankToUndefined,- z.string().optional()- ),- API_CORS_ORIGINS: z.preprocess(blankToUndefined, z.string().optional()),+ DASHBOARD_URL: optionalString,+ NEXT_PUBLIC_DASHBOARD_URL: optionalString,+ API_CORS_ORIGINS: optionalString,/*** Signs the three GSC OAuth cookies. Always set in a real deployment* (docs/ANSWERS.md §1.5); an unset value degrades to V1's `?? ''`.@@ -233,50 +335,502 @@ const envSchema = z.object({),/** Set on the demo deployment only. `validateSessionToken` short-circuits on* it, and `enforceAccess` bans mutations for it. */- DEMO_USER_ID: z.preprocess(blankToUndefined, z.string().optional()),+ DEMO_USER_ID: optionalString,/** V1's `requestLoggingHook`: the client ids whose requests log ip/UA. */- ENABLE_VERBOSE_LOGGING: z.preprocess(blankToUndefined, z.string().optional()),- SHUTDOWN_FORCE_EXIT_MS: z.coerce- .number()- .int()- .positive()- .default(DEFAULT_SHUTDOWN_FORCE_EXIT_MS),+ ENABLE_VERBOSE_LOGGING: optionalString,++ // --- logging ---+ LOG_SILENT: trueOrOneSchema,+ LOG_EXPORTER: z.preprocess(+ blankToUndefined,+ z.enum(['otlp', 'stdout']).optional()+ ),+ LOG_PREFIX: optionalString,+ HYPERDX_API_KEY: optionalString,++ // --- storage / crypto ---+ CLICKHOUSE_CLUSTER: trueOrOneSchema,+ ENCRYPTION_KEY: optionalString,++ // --- auth ---+ ALLOW_REGISTRATION: optionalString,+ ALLOW_INVITATION: optionalString,+ GITHUB_CLIENT_ID: stringWithDefault(''),+ GITHUB_CLIENT_SECRET: stringWithDefault(''),+ GITHUB_REDIRECT_URI: stringWithDefault(''),+ GOOGLE_CLIENT_ID: stringWithDefault(''),+ GOOGLE_CLIENT_SECRET: stringWithDefault(''),+ GOOGLE_REDIRECT_URI: stringWithDefault(''),+ GSC_GOOGLE_REDIRECT_URI: stringWithDefault(''),+ COOKIE_TLDS: tokenList,+ CUSTOM_COOKIE_DOMAIN: optionalString,++ // --- outbound providers ---+ OPENAI_API_KEY: optionalString,+ OPENAI_BASE_URL: optionalString,+ OPENAI_PROJECT: optionalString,+ OPENAI_ORGANIZATION: optionalString,+ ANTHROPIC_API_KEY: optionalString,+ ANTHROPIC_BASE_URL: optionalString,+ ANTHROPIC_TOKEN: optionalString,+ ANTHROPIC_VERSION: optionalString,+ SLACK_CLIENT_ID: optionalString,+ SLACK_CLIENT_SECRET: optionalString,+ SLACK_OAUTH_REDIRECT_URL: optionalString,+ SLACK_STATE_SECRET: optionalString,+ POLAR_SAVE_DISCOUNT_ID: optionalString,+ POLAR_WEBHOOK_SECRET: optionalString,+ GCS_API_ENDPOINT: optionalString,+ DISABLE_PING: definedIsTrueSchema,++ // --- kafka ---+ KAFKA_CLIENT_ID: stringWithDefault(DEFAULT_KAFKA_CLIENT_ID),+ KAFKA_BROKERS: tokenList,+ KAFKA_EVENTS_TOPIC: stringWithDefault(DEFAULT_KAFKA_EVENTS_TOPIC),+ KAFKA_EVENTS_DLQ_TOPIC: optionalString,+ KAFKA_CONSUMER_GROUP: stringWithDefault(DEFAULT_KAFKA_CONSUMER_GROUP),+ KAFKA_PARTITIONS_CONCURRENT: positiveIntWithDefault(+ DEFAULT_KAFKA_PARTITIONS_CONCURRENT+ ),+ KAFKA_MIN_MESSAGES: positiveIntWithDefault(DEFAULT_KAFKA_MIN_MESSAGES),+ KAFKA_MAX_WAIT_MS: positiveIntWithDefault(DEFAULT_KAFKA_MAX_WAIT_MS),+ KAFKA_MAX_MESSAGES_PER_PARTITION: positiveIntWithDefault(+ DEFAULT_KAFKA_MAX_MESSAGES_PER_PARTITION+ ),+ KAFKA_SESSION_TIMEOUT_MS: positiveIntWithDefault(+ DEFAULT_KAFKA_SESSION_TIMEOUT_MS+ ),+ KAFKA_HEARTBEAT_INTERVAL_MS: positiveIntWithDefault(+ DEFAULT_KAFKA_HEARTBEAT_INTERVAL_MS+ ),+ KAFKA_REQUEST_TIMEOUT_MS: positiveIntWithDefault(+ DEFAULT_KAFKA_REQUEST_TIMEOUT_MS+ ),+ KAFKA_CONNECTION_TIMEOUT_MS: positiveIntWithDefault(+ DEFAULT_KAFKA_CONNECTION_TIMEOUT_MS+ ),+ KAFKA_PRODUCER_RETRIES: positiveIntWithDefault(+ DEFAULT_KAFKA_PRODUCER_RETRIES+ ),+ KAFKA_PRODUCER_INITIAL_RETRY_MS: positiveIntWithDefault(+ DEFAULT_KAFKA_PRODUCER_INITIAL_RETRY_MS+ ),+ KAFKA_PRODUCER_MAX_RETRY_MS: positiveIntWithDefault(+ DEFAULT_KAFKA_PRODUCER_MAX_RETRY_MS+ ),+ KAFKA_HANDLER_MAX_ATTEMPTS: positiveIntWithDefault(+ DEFAULT_KAFKA_HANDLER_MAX_ATTEMPTS+ ),+ KAFKA_HANDLER_RETRY_INITIAL_MS: positiveIntWithDefault(+ DEFAULT_KAFKA_HANDLER_RETRY_INITIAL_MS+ ),+ KAFKA_HANDLER_RETRY_MAX_MS: positiveIntWithDefault(+ DEFAULT_KAFKA_HANDLER_RETRY_MAX_MS+ ),++ // --- buffers ---+ BUFFER_ASYNC_INSERTS: definedIsTrueSchema,+ BUFFER_CH_INSERT_CONCURRENCY: optionalPositiveInt,+ BOT_BUFFER_BATCH_SIZE: optionalPositiveInt,+ EVENT_BUFFER_BATCH_SIZE: optionalPositiveInt,+ EVENT_BUFFER_CHUNK_SIZE: optionalPositiveInt,+ EVENT_BUFFER_MICRO_BATCH_MS: optionalPositiveInt,+ EVENT_BUFFER_MICRO_BATCH_SIZE: optionalPositiveInt,+ GROUP_BUFFER_BATCH_SIZE: optionalPositiveInt,+ GROUP_BUFFER_CHUNK_SIZE: optionalPositiveInt,+ GROUP_BUFFER_TTL_IN_SECONDS: optionalPositiveInt,+ PROFILE_BUFFER_BATCH_SIZE: optionalPositiveInt,+ PROFILE_BUFFER_CHUNK_SIZE: optionalPositiveInt,+ PROFILE_BUFFER_TTL_IN_SECONDS: optionalPositiveInt,+ PROFILE_BUFFER_FETCH_CHUNK_SIZE: optionalPositiveInt,+ PROFILE_BACKFILL_BUFFER_BATCH_SIZE: optionalPositiveInt,+ REPLAY_BUFFER_BATCH_SIZE: optionalPositiveInt,+ REPLAY_BUFFER_CHUNK_SIZE: optionalPositiveInt,+ SESSION_BUFFER_BATCH_SIZE: optionalPositiveInt,+ SESSION_BUFFER_CHUNK_SIZE: optionalPositiveInt,+ SESSION_BUFFER_SQUASH: onUnlessDisabledSchema,++ // --- sessions ---+ SESSION_TIMEOUT_MS: optionalPositiveInt,+ SESSION_REAPER: onUnlessZeroSchema,+ SESSION_REAPER_BATCH_SIZE: optionalPositiveInt,+ SESSION_REAPER_WALLCLOCK_DEADMAN_MS: optionalPositiveInt,+ SESSION_VACUUM: onUnlessZeroSchema,+ SESSION_VACUUM_BATCH_SIZE: optionalPositiveInt,+ SESSION_VACUUM_STALE_THRESHOLD_MS: optionalPositiveInt,+ EXPERIMENTAL_PROFILE_BACKFILL: z.preprocess(+ blankToUndefined,+ z+ .string()+ .optional()+ .transform((value) => value === ONE_STRING)+ ),+ EXPERIMENTAL_PROFILE_BACKFILL_PROJECTS: tokenList,++ // --- query sizing ---+ EVENT_PROPERTY_VALUE_AUTOCOMPLETE_LIMIT: optionalPositiveInt,+ COHORT_MATERIALIZE_LIMIT: optionalPositiveInt,+ COHORT_QUERY_MEMORY_LIMIT_BYTES: optionalPositiveInt,+ COHORT_QUERY_SPILL_BYTES: optionalPositiveInt,+ EVENT_LIST_MAX_LOOKBACK_DAYS: optionalPositiveInt,+ SESSION_LIST_MAX_LOOKBACK_DAYS: optionalPositiveInt,+ IMPORT_BATCH_SIZE: optionalPositiveInt,+ INSIGHTS_RETENTION_DAYS: optionalPositiveInt,+ WIND_DOWN_MAX_PER_RUN: optionalPositiveInt,+ FUNNEL_NON_STRICT_ORDERING: trueOrOneSchema,++ // --- object-store export ---+ EXPORT_LAG_SECONDS: optionalPositiveInt,+ EXPORT_BATCH_SIZE: optionalPositiveInt,+ EXPORT_MAX_BATCHES_PER_RUN: optionalPositiveInt,+ EXPORT_CONCURRENCY: optionalPositiveInt,++ // --- request IP resolution ---+ IP_HEADER_ORDER: optionalTokenList,+ TRUSTED_IP_HEADER_ORDER: optionalTokenList,});-export type Config = z.infer<typeof envSchema> & {- [key: `${string}_CONCURRENCY`]: number | undefined;-};+type RawEnv = z.infer<typeof rawSchema>;++// --- cross-field invariants -------------------------------------------------++/** A role that consumes must have something to consume. */+function checkRoleConsumesSomething(raw: RawEnv, ctx: z.RefinementCtx): void {+ if (raw.ROLE === 'api' || raw.ENABLED_QUEUES.length > 0) {+ return;+ }+ ctx.addIssue({+ code: z.ZodIssueCode.custom,+ message: `ROLE=${raw.ROLE} consumes queues but ENABLED_QUEUES names none — this process would boot and do nothing.`,+ });+}++/** `LOG_EXPORTER=otlp` ships through the HyperDX transport, which needs a key. */+function checkOtlpHasKey(raw: RawEnv, ctx: z.RefinementCtx): void {+ if (raw.LOG_EXPORTER !== 'otlp' || raw.HYPERDX_API_KEY !== undefined) {+ return;+ }+ ctx.addIssue({+ code: z.ZodIssueCode.custom,+ message:+ 'LOG_EXPORTER=otlp requires HYPERDX_API_KEY — without it pino has no transport and every log line is dropped.',+ });+}++/** kafkajs evicts a consumer that cannot heartbeat inside its session. */+function checkKafkaHeartbeatFitsSession(+ raw: RawEnv,+ ctx: z.RefinementCtx+): void {+ if (raw.KAFKA_HEARTBEAT_INTERVAL_MS < raw.KAFKA_SESSION_TIMEOUT_MS) {+ return;+ }+ ctx.addIssue({+ code: z.ZodIssueCode.custom,+ message: `KAFKA_HEARTBEAT_INTERVAL_MS (${raw.KAFKA_HEARTBEAT_INTERVAL_MS}) must be below KAFKA_SESSION_TIMEOUT_MS (${raw.KAFKA_SESSION_TIMEOUT_MS}) — otherwise every consumer is evicted before its first heartbeat.`,+ });+}-/** The CORS delegator's origin allowlist, in V1's own order. */-export function dashboardOrigins(config: Config): string[] {- return [- config.DASHBOARD_URL ?? config.NEXT_PUBLIC_DASHBOARD_URL,- ...splitTokens(config.API_CORS_ORIGINS),- ].filter((origin): origin is string => Boolean(origin));+/** The reaper's deadman closes sessions; below the idle window it closes live ones. */+function checkReaperDeadmanOutlivesSession(+ raw: RawEnv,+ ctx: z.RefinementCtx+): void {+ const deadman = raw.SESSION_REAPER_WALLCLOCK_DEADMAN_MS;+ const timeout = raw.SESSION_TIMEOUT_MS;+ if (deadman === undefined || timeout === undefined || deadman >= timeout) {+ return;+ }+ ctx.addIssue({+ code: z.ZodIssueCode.custom,+ message: `SESSION_REAPER_WALLCLOCK_DEADMAN_MS (${deadman}) is below SESSION_TIMEOUT_MS (${timeout}) — the reaper would close sessions that are still live.`,+ });+}++/** The vacuum is a backstop; it must never race normal reaping. */+function checkVacuumOutlivesReaper(raw: RawEnv, ctx: z.RefinementCtx): void {+ const stale = raw.SESSION_VACUUM_STALE_THRESHOLD_MS;+ const deadman =+ raw.SESSION_REAPER_WALLCLOCK_DEADMAN_MS ?? raw.SESSION_TIMEOUT_MS;+ if (stale === undefined || deadman === undefined || stale > deadman) {+ return;+ }+ ctx.addIssue({+ code: z.ZodIssueCode.custom,+ message: `SESSION_VACUUM_STALE_THRESHOLD_MS (${stale}) must exceed the reaper deadman (${deadman}) — the vacuum would drop sessions the reaper is still closing.`,+ });}-/** V1's `ENABLE_VERBOSE_LOGGING`, a comma-separated client-id list. */-export function verboseClientIds(config: Config): string[] {- return splitTokens(config.ENABLE_VERBOSE_LOGGING);+/** A project allowlist for a switch that is off silently does nothing. */+function checkProfileBackfillProjectsHaveAFlag(+ raw: RawEnv,+ ctx: z.RefinementCtx+): void {+ if (+ raw.EXPERIMENTAL_PROFILE_BACKFILL ||+ raw.EXPERIMENTAL_PROFILE_BACKFILL_PROJECTS.length === 0+ ) {+ return;+ }+ ctx.addIssue({+ code: z.ZodIssueCode.custom,+ message:+ 'EXPERIMENTAL_PROFILE_BACKFILL_PROJECTS is set while EXPERIMENTAL_PROFILE_BACKFILL is not 1 — the allowlist has no effect.',+ });}-/** V1's default host rule (apps/api/src/index.ts). */-export function apiHost(config: Config): string {- return config.API_HOST ?? (isProduction(config) ? '0.0.0.0' : 'localhost');+// --- derivations ------------------------------------------------------------++function deriveDashboardUrl(raw: RawEnv): string {+ return raw.DASHBOARD_URL ?? raw.NEXT_PUBLIC_DASHBOARD_URL ?? '';}-export function isProduction(config: Config): boolean {- return config.NODE_ENV === PRODUCTION;+function deriveCoreConfig(raw: RawEnv): CoreConfig {+ const isProduction = raw.NODE_ENV === PRODUCTION;+ const exporter =+ raw.LOG_EXPORTER ?? (raw.HYPERDX_API_KEY ? 'otlp' : 'stdout');+ const googleClient = {+ clientId: raw.GOOGLE_CLIENT_ID,+ clientSecret: raw.GOOGLE_CLIENT_SECRET,+ };+ return {+ isProduction,+ isDevelopment: raw.NODE_ENV === DEVELOPMENT,+ selfHosted: raw.SELF_HOSTED,+ dashboardUrl: deriveDashboardUrl(raw),+ demoUserId: raw.DEMO_USER_ID,+ // Clustered mode = production (not self-hosted), unless said otherwise.+ clickhouseClustered: raw.CLICKHOUSE_CLUSTER || !raw.SELF_HOSTED,+ encryptionKey: raw.ENCRYPTION_KEY,+ pingDisabled: raw.DISABLE_PING,+ logging: {+ level: raw.LOG_LEVEL,+ silent: raw.LOG_SILENT,+ exporter,+ hyperdxApiKey: raw.HYPERDX_API_KEY,+ serviceNamePrefix: raw.LOG_PREFIX,+ serviceNameEnvironment: raw.NODE_ENV ?? DEFAULT_SERVICE_ENVIRONMENT,+ interceptProcessOutput:+ isProduction ||+ raw.HYPERDX_API_KEY !== undefined ||+ raw.LOG_EXPORTER !== undefined,+ },+ auth: {+ allowRegistration:+ raw.ALLOW_REGISTRATION === undefined+ ? undefined+ : raw.ALLOW_REGISTRATION !== FALSE_STRING,+ allowInvitation:+ raw.ALLOW_INVITATION === undefined+ ? undefined+ : raw.ALLOW_INVITATION !== FALSE_STRING,+ github: {+ clientId: raw.GITHUB_CLIENT_ID,+ clientSecret: raw.GITHUB_CLIENT_SECRET,+ redirectUri: raw.GITHUB_REDIRECT_URI,+ },+ google: { ...googleClient, redirectUri: raw.GOOGLE_REDIRECT_URI },+ googleGsc: { ...googleClient, redirectUri: raw.GSC_GOOGLE_REDIRECT_URI },+ },+ cookies: {+ extraMultiPartTlds: raw.COOKIE_TLDS.map((tld) => tld.toLowerCase()),+ customDomain: raw.CUSTOM_COOKIE_DOMAIN,+ },+ ai: {+ openai: {+ apiKey: raw.OPENAI_API_KEY,+ baseUrl: raw.OPENAI_BASE_URL,+ project: raw.OPENAI_PROJECT,+ organization: raw.OPENAI_ORGANIZATION,+ },+ anthropic: {+ apiKey: raw.ANTHROPIC_API_KEY,+ baseUrl: raw.ANTHROPIC_BASE_URL,+ authToken: raw.ANTHROPIC_TOKEN,+ version: raw.ANTHROPIC_VERSION,+ },+ },+ slack: {+ clientId: raw.SLACK_CLIENT_ID,+ clientSecret: raw.SLACK_CLIENT_SECRET,+ oauthRedirectUrl: raw.SLACK_OAUTH_REDIRECT_URL,+ stateSecret: raw.SLACK_STATE_SECRET,+ },+ polar: {+ saveDiscountId: raw.POLAR_SAVE_DISCOUNT_ID,+ webhookSecret: raw.POLAR_WEBHOOK_SECRET,+ },+ kafka: {+ clientId: raw.KAFKA_CLIENT_ID,+ brokers: raw.KAFKA_BROKERS,+ eventsTopic: raw.KAFKA_EVENTS_TOPIC,+ // Same broker as the events topic, so a poison message is retained and+ // countable instead of dropped (ADR-004 delivery semantics).+ eventsDlqTopic:+ raw.KAFKA_EVENTS_DLQ_TOPIC ?? `${raw.KAFKA_EVENTS_TOPIC}-dlq`,+ consumerGroup: raw.KAFKA_CONSUMER_GROUP,+ partitionsConcurrent: raw.KAFKA_PARTITIONS_CONCURRENT,+ minMessages: raw.KAFKA_MIN_MESSAGES,+ maxWaitMs: raw.KAFKA_MAX_WAIT_MS,+ maxMessagesPerPartition: raw.KAFKA_MAX_MESSAGES_PER_PARTITION,+ sessionTimeoutMs: raw.KAFKA_SESSION_TIMEOUT_MS,+ heartbeatIntervalMs: raw.KAFKA_HEARTBEAT_INTERVAL_MS,+ requestTimeoutMs: raw.KAFKA_REQUEST_TIMEOUT_MS,+ connectionTimeoutMs: raw.KAFKA_CONNECTION_TIMEOUT_MS,+ producerRetries: raw.KAFKA_PRODUCER_RETRIES,+ producerInitialRetryMs: raw.KAFKA_PRODUCER_INITIAL_RETRY_MS,+ producerMaxRetryMs: raw.KAFKA_PRODUCER_MAX_RETRY_MS,+ handlerMaxAttempts: raw.KAFKA_HANDLER_MAX_ATTEMPTS,+ handlerRetryInitialMs: raw.KAFKA_HANDLER_RETRY_INITIAL_MS,+ handlerRetryMaxMs: raw.KAFKA_HANDLER_RETRY_MAX_MS,+ },+ buffers: {+ asyncInserts: raw.BUFFER_ASYNC_INSERTS,+ chInsertConcurrency: raw.BUFFER_CH_INSERT_CONCURRENCY,+ bot: { batchSize: raw.BOT_BUFFER_BATCH_SIZE },+ event: {+ batchSize: raw.EVENT_BUFFER_BATCH_SIZE,+ chunkSize: raw.EVENT_BUFFER_CHUNK_SIZE,+ microBatchMs: raw.EVENT_BUFFER_MICRO_BATCH_MS,+ microBatchSize: raw.EVENT_BUFFER_MICRO_BATCH_SIZE,+ },+ group: {+ batchSize: raw.GROUP_BUFFER_BATCH_SIZE,+ chunkSize: raw.GROUP_BUFFER_CHUNK_SIZE,+ ttlSeconds: raw.GROUP_BUFFER_TTL_IN_SECONDS,+ },+ profile: {+ batchSize: raw.PROFILE_BUFFER_BATCH_SIZE,+ chunkSize: raw.PROFILE_BUFFER_CHUNK_SIZE,+ ttlSeconds: raw.PROFILE_BUFFER_TTL_IN_SECONDS,+ fetchChunkSize: raw.PROFILE_BUFFER_FETCH_CHUNK_SIZE,+ },+ profileBackfill: { batchSize: raw.PROFILE_BACKFILL_BUFFER_BATCH_SIZE },+ replay: {+ batchSize: raw.REPLAY_BUFFER_BATCH_SIZE,+ chunkSize: raw.REPLAY_BUFFER_CHUNK_SIZE,+ },+ session: {+ batchSize: raw.SESSION_BUFFER_BATCH_SIZE,+ chunkSize: raw.SESSION_BUFFER_CHUNK_SIZE,+ squash: raw.SESSION_BUFFER_SQUASH,+ },+ },+ session: {+ timeoutMs: raw.SESSION_TIMEOUT_MS,+ reaperEnabled: raw.SESSION_REAPER,+ reaperBatchSize: raw.SESSION_REAPER_BATCH_SIZE,+ reaperWallclockDeadmanMs: raw.SESSION_REAPER_WALLCLOCK_DEADMAN_MS,+ vacuumEnabled: raw.SESSION_VACUUM,+ vacuumBatchSize: raw.SESSION_VACUUM_BATCH_SIZE,+ vacuumStaleThresholdMs: raw.SESSION_VACUUM_STALE_THRESHOLD_MS,+ profileBackfillEnabled: raw.EXPERIMENTAL_PROFILE_BACKFILL,+ profileBackfillProjectIds: raw.EXPERIMENTAL_PROFILE_BACKFILL_PROJECTS,+ },+ query: {+ eventPropertyValueAutocompleteLimit:+ raw.EVENT_PROPERTY_VALUE_AUTOCOMPLETE_LIMIT,+ cohortMaterializeLimit: raw.COHORT_MATERIALIZE_LIMIT,+ cohortQueryMemoryLimitBytes: raw.COHORT_QUERY_MEMORY_LIMIT_BYTES,+ cohortQuerySpillBytes: raw.COHORT_QUERY_SPILL_BYTES,+ eventListMaxLookbackDays: raw.EVENT_LIST_MAX_LOOKBACK_DAYS,+ sessionListMaxLookbackDays: raw.SESSION_LIST_MAX_LOOKBACK_DAYS,+ importBatchSize: raw.IMPORT_BATCH_SIZE,+ insightsRetentionDays: raw.INSIGHTS_RETENTION_DAYS,+ windDownMaxPerRun: raw.WIND_DOWN_MAX_PER_RUN,+ funnelNonStrictOrdering: raw.FUNNEL_NON_STRICT_ORDERING,+ },+ objectStoreExport: {+ lagSeconds: raw.EXPORT_LAG_SECONDS,+ batchSize: raw.EXPORT_BATCH_SIZE,+ maxBatchesPerRun: raw.EXPORT_MAX_BATCHES_PER_RUN,+ concurrency: raw.EXPORT_CONCURRENCY,+ gcsApiEndpoint: raw.GCS_API_ENDPOINT,+ },+ ipHeaders: {+ attributionOrder: raw.IP_HEADER_ORDER,+ trustedOrder: raw.TRUSTED_IP_HEADER_ORDER,+ },+ };}+/** What `apps/api` itself branches on, plus `core` for everything below it. */+export interface Config {+ ROLE: Role;+ LOG_LEVEL: string;+ API_PORT: number;+ QUEUE_CLUSTER: boolean;+ QUEUE_NAMESPACE: string | undefined;+ ENABLED_QUEUES: EnabledQueue[];+ DISABLE_WORKERS: boolean;+ DISABLE_BULLBOARD: boolean;+ COOKIE_SECRET: string;+ SHUTDOWN_FORCE_EXIT_MS: number;+ /** The CORS delegator's origin allowlist, in V1's own order. */+ dashboardOrigins: string[];+ /** V1's `ENABLE_VERBOSE_LOGGING`, already split. */+ verboseClientIds: string[];+ /**+ * `Bun.serve`'s listen options. No `hostname` unless API_HOST says one:+ * Bun's default is `0.0.0.0`, and its `localhost` binds IPv6-only.+ */+ listen: { port: number; hostname?: string };+ /** `<QUEUE>_CONCURRENCY`, resolved per registry queue name. */+ concurrency: Record<string, number | undefined>;+ /** Everything `packages/core` reads, parsed and derived once. */+ core: CoreConfig;+}++const envSchema = rawSchema+ // Every cross-field invariant runs BEFORE the transform (ADR-022 R17), so+ // the derivations below can assume a coherent environment.+ .superRefine((raw, ctx) => {+ checkRoleConsumesSomething(raw, ctx);+ checkOtlpHasKey(raw, ctx);+ checkKafkaHeartbeatFitsSession(raw, ctx);+ checkReaperDeadmanOutlivesSession(raw, ctx);+ checkVacuumOutlivesReaper(raw, ctx);+ checkProfileBackfillProjectsHaveAFlag(raw, ctx);+ })+ .transform((raw): Config => {+ const dashboardUrl = deriveDashboardUrl(raw);+ return {+ ROLE: raw.ROLE,+ LOG_LEVEL: raw.LOG_LEVEL,+ API_PORT: raw.API_PORT,+ QUEUE_CLUSTER: raw.QUEUE_CLUSTER,+ QUEUE_NAMESPACE: raw.QUEUE_NAMESPACE,+ ENABLED_QUEUES: raw.ENABLED_QUEUES,+ DISABLE_WORKERS: raw.DISABLE_WORKERS,+ DISABLE_BULLBOARD: raw.DISABLE_BULLBOARD,+ COOKIE_SECRET: raw.COOKIE_SECRET,+ SHUTDOWN_FORCE_EXIT_MS: raw.SHUTDOWN_FORCE_EXIT_MS,+ dashboardOrigins: [+ dashboardUrl,+ ...splitTokens(raw.API_CORS_ORIGINS),+ ].filter(Boolean),+ verboseClientIds: splitTokens(raw.ENABLE_VERBOSE_LOGGING),+ listen: raw.API_HOST+ ? { port: raw.API_PORT, hostname: raw.API_HOST }+ : { port: raw.API_PORT },+ concurrency: Object.fromEntries(+ Object.keys(queues).map((name) => [+ name,+ (raw as Record<string, unknown>)[concurrencyEnvKey(name)] as+ | number+ | undefined,+ ])+ ),+ core: deriveCoreConfig(raw),+ };+ });+/** The `<QUEUE>_CONCURRENCY` override for one registry queue, if any. */export function concurrencyOverride(config: Config,queueName: string): number | undefined {- return (config as Record<string, unknown>)[concurrencyEnvKey(queueName)] as- | number- | undefined;+ return config.concurrency[queueName];}function formatIssues(issues: z.ZodIssue[]): string {diff --git a/apps/api/src/main.ts b/apps/api/src/main.tsindex 0f21de482..4207ad84d 100644--- a/apps/api/src/main.ts+++ b/apps/api/src/main.ts@@ -28,6 +28,8 @@// dashboard surface and `/trpc`. A role that only consumes serves none of the// last three — V1's worker never did either.+// The only `process.env` touch outside config/env.ts, and a WRITE: it sets the+// process timezone. Every value the app READS comes from `loadConfig` below.process.env.TZ = 'UTC';import { createHmac } from 'node:crypto';@@ -38,8 +40,8 @@ import {BULL_BOARD_BASE_PATH,type BufferDeps,bullBoardRoutes,- COOKIE_OPTIONS,checkNotificationRulesForEvent,+ cookieOptions,corsDelegator,createBuffers,createClients,@@ -54,11 +56,6 @@ import {errorHandler,type HttpCtx,ingestConsumerMetrics,- KAFKA_EVENTS_TOPIC,- KAFKA_HANDLER_MAX_ATTEMPTS,- KAFKA_HANDLER_RETRY_INITIAL_MS,- KAFKA_HANDLER_RETRY_MAX_MS,- KAFKA_PARTITIONS_CONCURRENT,type KafkaConsumerHandle,kafkaLogger,markEventsActivity,@@ -88,15 +85,7 @@ import { db } from '@openpanel/db/src/prisma-client';import { getRedisCache, getRedisPub, getRedisQueue } from '@openpanel/redis';import { Elysia } from 'elysia';import pino from 'pino';-import {- type Config,- concurrencyOverride,- dashboardOrigins,- isProduction,- KAFKA_QUEUE_TOKEN,- loadConfig,- verboseClientIds,-} from './config/env';+import { type Config, KAFKA_QUEUE_TOKEN, loadConfig } from './config/env';// The registry key and the Redis name are the same string for all seven queues// (jobs.registry.ts); `queueKey` only braces it under `QUEUE_CLUSTER`.@@ -168,6 +157,7 @@ function bufferDeps(producers: QueueProducerHandle): BufferDeps {return {createLogger: (name) => logger.child({ name }),isCronPaused: async () => (cron ? await cron.isPaused() : false),+ config: config.core,// 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).@@ -200,7 +190,7 @@ function buildDeps(): AppDeps {buffers: createBuffers(bufferDeps(producers)),producers,logger,- config: { selfHosted: config.SELF_HOSTED },+ config: config.core,};}@@ -219,7 +209,7 @@ function consumedQueues(): Record<string, QueueDefinition> {if (!enabled.has(name)) {continue;}- const concurrency = concurrencyOverride(config, name);+ const concurrency = config.concurrency[name];selected[name] =concurrency === undefined? definition@@ -254,15 +244,16 @@ function warnOnUnhandledSchedulers(schedulerIds: string[]): void {async function startIngestConsumer(deps: AppDeps): Promise<KafkaConsumerHandle> {- assertKafkaConfigured();+ assertKafkaConfigured(config.core.kafka);enableEventsHeartbeat();return await startKafkaEventsConsumer({- createConsumer: createKafkaEventsConsumer,+ createConsumer: (options?: { groupId?: string }) =>+ createKafkaEventsConsumer(config.core, options),logger,- kafkaLogger,- topic: KAFKA_EVENTS_TOPIC,- partitionsConsumedConcurrently: KAFKA_PARTITIONS_CONCURRENT,+ kafkaLogger: kafkaLogger(config.core),+ topic: config.core.kafka.eventsTopic,+ partitionsConsumedConcurrently: config.core.kafka.partitionsConcurrent,batch: {// One Ctx per message, scoped to the requestId the producer stamped// into the envelope (M10-006). The one binding a work scope cannot@@ -271,13 +262,14 @@ async function startIngestConsumer(handleEvent: createIncomingEventHandler(deps, {checkNotificationRulesForEvent,}),- sendToDeadLetter: produceDeadLetterEvent,+ sendToDeadLetter: (message) =>+ produceDeadLetterEvent(config.core, message),logger,metrics: ingestConsumerMetrics,onActivity: markEventsActivity,- maxAttempts: KAFKA_HANDLER_MAX_ATTEMPTS,- initialRetryMs: KAFKA_HANDLER_RETRY_INITIAL_MS,- maxRetryMs: KAFKA_HANDLER_RETRY_MAX_MS,+ maxAttempts: config.core.kafka.handlerMaxAttempts,+ initialRetryMs: config.core.kafka.handlerRetryInitialMs,+ maxRetryMs: config.core.kafka.handlerRetryMaxMs,},});}@@ -372,19 +364,20 @@ async function buildHttpApp(deps: AppDeps) {? { secrets: config.COOKIE_SECRET, sign: SIGNED_COOKIE_NAMES }: {},})- .use(corsDelegator({ dashboardOrigins: dashboardOrigins(config) }))- .use(errorHandler(deps, { production: isProduction(config) }))- .use(requestLogging(deps, { verboseClientIds: verboseClientIds(config) }))+ .use(corsDelegator({ dashboardOrigins: config.dashboardOrigins }))+ .use(errorHandler(deps, { production: config.core.isProduction }))+ .use(requestLogging(deps, { verboseClientIds: config.verboseClientIds })).use(opsRoutes(deps));if (roleServesHttp) {const trpc = createTrpcFetchHandler({router: appRouter,logger,- cookieOptions: COOKIE_OPTIONS,- simulateLatency: !isProduction(config),+ cookieOptions: cookieOptions(config.core),+ ipHeaders: config.core.ipHeaders,+ simulateLatency: !config.core.isProduction,signCookie,- demoMode: Boolean(config.DEMO_USER_ID),+ demoMode: Boolean(config.core.demoUserId),});app@@ -412,7 +405,7 @@ async function buildHttpApp(deps: AppDeps) {// schedule. Two conditions, both V1's: `NODE_ENV != production`, because it// is an unauthenticated "run anything now" surface, and a consuming role,// because in V1 these routes only ever existed on the worker.- if (roleConsumes && !isProduction(config)) {+ if (roleConsumes && !config.core.isProduction) {app.use(debugRoutes(deps));logger.info('Debug routes enabled at /debug/cron');}@@ -488,8 +481,8 @@ async function main() {await startSchedulers({queue: cron,flags: {- selfHosted: config.SELF_HOSTED,- production: isProduction(config),+ selfHosted: config.core.selfHosted,+ production: config.core.isProduction,},logger,});@@ -553,17 +546,12 @@ async function main() {shutdown('SIGINT');});- // No `hostname` unless API_HOST says one: Bun's default is `0.0.0.0`, and- // its `localhost` binds IPv6-only (see config/env.ts's API_HOST).- const listen = config.API_HOST- ? { port: config.API_PORT, hostname: config.API_HOST }- : { port: config.API_PORT };- app.listen(listen, () => {+ app.listen(config.listen, () => {logger.info({role,port: config.API_PORT,- hostname: config.API_HOST ?? '0.0.0.0',+ hostname: config.listen.hostname ?? '0.0.0.0',// ADR-016 rule 5: the running Bun version, asserted against// .bun-version by scripts/doctor.sh — logged so a wrong-runtime// incident is one log line away rather than an inference.diff --git a/packages/core/package.json b/packages/core/package.jsonindex 1012857e6..65fba95d1 100644--- a/packages/core/package.json+++ b/packages/core/package.json@@ -15,7 +15,7 @@"gen:asns": "bun scripts/get-datacenter-asns.ts","gen:bots": "bun scripts/get-bots.ts && biome format --write ./src/modules/ingest/src/bots/bots.ts","gen:referrers": "bun scripts/get-referrers.ts && biome format --write ./src/shared/referrers/index.ts",- "migrate:deploy:code": "bun run with-env bun ./src/code-migrations/migrate.ts",+ "migrate:deploy:code": "bun run with-env bun ./scripts/migrate-code.ts","with-env": "dotenv -e ../../.env -c --"},"dependencies": {diff --git a/packages/core/scripts/migrate-code.ts b/packages/core/scripts/migrate-code.tsnew file mode 100644index 000000000..d96eeddfd--- /dev/null+++ b/packages/core/scripts/migrate-code.ts@@ -0,0 +1,26 @@+// The shell for `src/code-migrations`. It reads the environment, the runner+// does the work — the same split `apps/api/src/main.ts` and `config/env.ts`+// have, and what keeps `packages/core/src` free of `process.env` (ADR-022 R7).+//+// `code-migrations` is not a conformance target (ADR-022, Carl's rulings): it+// runs as a script with direct database access and no services, and it leaves+// core for `packages/db` in its own task. This file is the seam that makes it+// runnable without a `process.env` read inside `src/`.++import type { CodeMigrationEnv } from '../src/code-migrations/helpers';+import { runCodeMigrations } from '../src/code-migrations/migrate';++const TRUE_VALUES = new Set(['true', '1']);++function isEnabled(value: string | undefined): boolean {+ return value !== undefined && TRUE_VALUES.has(value.trim());+}++const env: CodeMigrationEnv = {+ clickhouseCluster: isEnabled(process.env.CLICKHOUSE_CLUSTER),+ selfHosted: isEnabled(process.env.SELF_HOSTED),+ databaseUrl: process.env.DATABASE_URL,+ clickhouseUrl: process.env.CLICKHOUSE_URL,+};++await runCodeMigrations(env);diff --git a/packages/core/src/buffers/base-buffer.ts b/packages/core/src/buffers/base-buffer.tsindex 81a490165..0f55ece4c 100644--- a/packages/core/src/buffers/base-buffer.ts+++ b/packages/core/src/buffers/base-buffer.ts@@ -1,6 +1,7 @@import { Readable } from 'node:stream';import type { ClickHouseSettings } from '@clickhouse/client';import { getRedisCache } from '@openpanel/redis';+import type { CoreConfig } from '../config';import type { Logger } from '../logger';import type { ServiceDeps } from '../services';import { type ChQueryInput, type ChScope, chQuery } from '../shared/ch-query';@@ -36,8 +37,24 @@ export interface BufferDeps {* (docs/ANSWERS.md §3: "known!").*/isCronPaused(): Promise<boolean>;+ /**+ * The parsed environment. Every buffer's batch/chunk/TTL knob used to be a+ * module-scope `process.env` read; they arrive here instead (ADR-022 R7),+ * which is also what lets a test build a buffer with a different sizing+ * without touching the process environment.+ */+ config: CoreConfig;}+/**+ * Max number of ch.insert sub-chunks that a single flush may run in parallel.+ * Caps connection-pool usage and worker memory peak. The ClickHouse client+ * pool defaults to 30 connections; with 3 workers and up to ~3 buffers+ * flushing concurrently cluster-wide, a per-flush cap of 5 keeps total+ * in-flight inserts well under the pool.+ */+const DEFAULT_CH_INSERT_CONCURRENCY = 5;+export type FlushPhaseTimings = {lrangeMs?: number;/** Time spent in CH SELECT(s) during flush (e.g. profile-buffer's@@ -125,7 +142,7 @@ export class BaseBuffer {}protected getClickhouseSettings(): ClickHouseSettings {- if (process.env.BUFFER_ASYNC_INSERTS) {+ if (this.deps.config.buffers.asyncInserts) {return {async_insert: 1,wait_for_async_insert: 0,@@ -150,6 +167,8 @@ export class BaseBuffer {enableParallelProcessing?: boolean;}) {+ this.chInsertConcurrency =+ deps.config.buffers.chInsertConcurrency ?? DEFAULT_CH_INSERT_CONCURRENCY;this.logger = deps.createLogger(options.name);this.name = options.name;this.lockKey = `lock:${this.name}`;@@ -242,16 +261,8 @@ export class BaseBuffer {return chunks;}- /**- * Max number of ch.insert sub-chunks that a single flush may run in- * parallel. Caps connection-pool usage and worker memory peak. The- * ClickHouse client pool defaults to 30 connections; with 3 workers and- * up to ~3 buffers flushing concurrently cluster-wide, a per-flush cap- * of 5 keeps total in-flight inserts well under the pool.- */- protected chInsertConcurrency = process.env.BUFFER_CH_INSERT_CONCURRENCY- ? Math.max(1, Number.parseInt(process.env.BUFFER_CH_INSERT_CONCURRENCY, 10))- : 5;+ /** BUFFER_CH_INSERT_CONCURRENCY, or DEFAULT_CH_INSERT_CONCURRENCY. */+ protected chInsertConcurrency: number;/*** Run `fn` over `items` with a bounded concurrency. Preserves order ofdiff --git a/packages/core/src/buffers/bot-buffer.ts b/packages/core/src/buffers/bot-buffer.tsindex 2707f83bb..d9aefad0b 100644--- a/packages/core/src/buffers/bot-buffer.ts+++ b/packages/core/src/buffers/bot-buffer.ts@@ -3,10 +3,11 @@ import type { IClickhouseBotEvent } from '../modules/event/event.service';import { TABLE_NAMES } from '../shared/ch-tables';import { BaseBuffer, type BufferDeps } from './base-buffer';+const DEFAULT_BATCH_SIZE = 1000;+export class BotBuffer extends BaseBuffer {- private batchSize = process.env.BOT_BUFFER_BATCH_SIZE- ? Number.parseInt(process.env.BOT_BUFFER_BATCH_SIZE, 10)- : 1000;+ private readonly batchSize =+ this.deps.config.buffers.bot.batchSize ?? DEFAULT_BATCH_SIZE;private readonly redisKey = 'bot-events-buffer';private redis: Redis;diff --git a/packages/core/src/buffers/event-buffer.test.ts b/packages/core/src/buffers/event-buffer.test.tsindex fddbd9f42..6acbb5955 100644--- a/packages/core/src/buffers/event-buffer.test.ts+++ b/packages/core/src/buffers/event-buffer.test.ts@@ -2,6 +2,7 @@ import { afterAll, beforeEach, describe, expect, it, mock } from 'bun:test';import type { Readable } from 'node:stream';import { getRedisCache } from '@openpanel/redis';import { bufferDepsWithCh } from '../../test/buffer-fixtures';+import { testCoreConfig } from '../../test/config-fixture';const realChQuery = { ...(await import('../shared/ch-query')) };@@ -221,9 +222,18 @@ 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(bufferDepsWithCh({ insert: chInsert }));+ const base = testCoreConfig();+ const eb = new EventBuffer(+ bufferDepsWithCh(+ { insert: chInsert },+ testCoreConfig({+ buffers: {+ ...base.buffers,+ event: { ...base.buffers.event, chunkSize: 2 },+ },+ })+ )+ );for (let i = 0; i < 4; i++) {eb.add({@@ -245,12 +255,6 @@ describe('EventBuffer', () => {);expect(call1.length).toBe(2);expect(call2.length).toBe(2);-- if (prev === undefined) {- delete process.env.EVENT_BUFFER_CHUNK_SIZE;- } else {- process.env.EVENT_BUFFER_CHUNK_SIZE = prev;- }});it('tracks active visitors', async () => {diff --git a/packages/core/src/buffers/event-buffer.ts b/packages/core/src/buffers/event-buffer.tsindex 18ae870c9..7ee9bc260 100644--- a/packages/core/src/buffers/event-buffer.ts+++ b/packages/core/src/buffers/event-buffer.ts@@ -53,20 +53,21 @@ export function extractProjectId(line: string): string | null {return line.slice(valueStart, valueEnd);}+const DEFAULT_BATCH_SIZE = 4000;+const DEFAULT_CHUNK_SIZE = 1000;+const DEFAULT_MICRO_BATCH_MS = 10;+const DEFAULT_MICRO_BATCH_SIZE = 100;+export class EventBuffer extends BaseBuffer {- private batchSize = process.env.EVENT_BUFFER_BATCH_SIZE- ? Number.parseInt(process.env.EVENT_BUFFER_BATCH_SIZE, 10)- : 4000;- private chunkSize = process.env.EVENT_BUFFER_CHUNK_SIZE- ? Number.parseInt(process.env.EVENT_BUFFER_CHUNK_SIZE, 10)- : 1000;-- private microBatchIntervalMs = process.env.EVENT_BUFFER_MICRO_BATCH_MS- ? Number.parseInt(process.env.EVENT_BUFFER_MICRO_BATCH_MS, 10)- : 10;- private microBatchMaxSize = process.env.EVENT_BUFFER_MICRO_BATCH_SIZE- ? Number.parseInt(process.env.EVENT_BUFFER_MICRO_BATCH_SIZE, 10)- : 100;+ private readonly batchSize =+ this.deps.config.buffers.event.batchSize ?? DEFAULT_BATCH_SIZE;+ private readonly chunkSize =+ this.deps.config.buffers.event.chunkSize ?? DEFAULT_CHUNK_SIZE;++ private readonly microBatchIntervalMs =+ this.deps.config.buffers.event.microBatchMs ?? DEFAULT_MICRO_BATCH_MS;+ private readonly microBatchMaxSize =+ this.deps.config.buffers.event.microBatchSize ?? DEFAULT_MICRO_BATCH_SIZE;private pendingEvents: IClickhouseEvent[] = [];private flushTimer: ReturnType<typeof setTimeout> | null = null;diff --git a/packages/core/src/buffers/group-buffer.ts b/packages/core/src/buffers/group-buffer.tsindex 86dada5d2..1ecc6f163 100644--- a/packages/core/src/buffers/group-buffer.ts+++ b/packages/core/src/buffers/group-buffer.ts@@ -35,16 +35,17 @@ export type IGroupBufferInput = {properties?: Record<string, unknown>;};+const DEFAULT_BATCH_SIZE = 200;+const DEFAULT_CHUNK_SIZE = 1000;+const DEFAULT_TTL_IN_SECONDS = 60 * 60;+export class GroupBuffer extends BaseBuffer {- private batchSize = process.env.GROUP_BUFFER_BATCH_SIZE- ? Number.parseInt(process.env.GROUP_BUFFER_BATCH_SIZE, 10)- : 200;- private chunkSize = process.env.GROUP_BUFFER_CHUNK_SIZE- ? Number.parseInt(process.env.GROUP_BUFFER_CHUNK_SIZE, 10)- : 1000;- private ttlInSeconds = process.env.GROUP_BUFFER_TTL_IN_SECONDS- ? Number.parseInt(process.env.GROUP_BUFFER_TTL_IN_SECONDS, 10)- : 60 * 60;+ private readonly batchSize =+ this.deps.config.buffers.group.batchSize ?? DEFAULT_BATCH_SIZE;+ private readonly chunkSize =+ this.deps.config.buffers.group.chunkSize ?? DEFAULT_CHUNK_SIZE;+ private readonly ttlInSeconds =+ this.deps.config.buffers.group.ttlSeconds ?? DEFAULT_TTL_IN_SECONDS;private readonly redisKey = 'group-buffer';private readonly redisCachePrefix = 'group-cache:';diff --git a/packages/core/src/buffers/profile-backfill-buffer.ts b/packages/core/src/buffers/profile-backfill-buffer.tsindex 8828d2708..5a4e6c533 100644--- a/packages/core/src/buffers/profile-backfill-buffer.ts+++ b/packages/core/src/buffers/profile-backfill-buffer.ts@@ -13,10 +13,11 @@ export interface ProfileBackfillEntry {// Max session IDs per IN clause before we split into another queryconst CHUNK_SIZE = 500;+const DEFAULT_BATCH_SIZE = 1000;+export class ProfileBackfillBuffer extends BaseBuffer {- private batchSize = process.env.PROFILE_BACKFILL_BUFFER_BATCH_SIZE- ? Number.parseInt(process.env.PROFILE_BACKFILL_BUFFER_BATCH_SIZE, 10)- : 1000;+ private readonly batchSize =+ this.deps.config.buffers.profileBackfill.batchSize ?? DEFAULT_BATCH_SIZE;private readonly redisKey = 'profile-backfill-buffer';private redis: Redis;@@ -70,7 +71,10 @@ export class ProfileBackfillBuffer extends BaseBuffer {const entries = Array.from(seen.values());const ch = this.resolveCh();- const table = getReplicatedTableName(TABLE_NAMES.events);+ const table = getReplicatedTableName(+ this.deps.config.clickhouseClustered,+ TABLE_NAMES.events+ );const chunks = this.chunks(entries, CHUNK_SIZE);let processedChunks = 0;diff --git a/packages/core/src/buffers/profile-buffer.ts b/packages/core/src/buffers/profile-buffer.tsindex b57e3400b..812a65f7d 100644--- a/packages/core/src/buffers/profile-buffer.ts+++ b/packages/core/src/buffers/profile-buffer.ts@@ -45,20 +45,21 @@ const PROFILE_LATEST_AGGREGATE_COLUMNS = sql.join(', ');+const DEFAULT_BATCH_SIZE = 200;+const DEFAULT_CHUNK_SIZE = 1000;+const DEFAULT_TTL_IN_SECONDS = 60 * 60;+const DEFAULT_FETCH_CHUNK_SIZE = 50;+export class ProfileBuffer extends BaseBuffer {- private readonly batchSize = process.env.PROFILE_BUFFER_BATCH_SIZE- ? Number.parseInt(process.env.PROFILE_BUFFER_BATCH_SIZE, 10)- : 200;- private readonly chunkSize = process.env.PROFILE_BUFFER_CHUNK_SIZE- ? Number.parseInt(process.env.PROFILE_BUFFER_CHUNK_SIZE, 10)- : 1000;- private readonly ttlInSeconds = process.env.PROFILE_BUFFER_TTL_IN_SECONDS- ? Number.parseInt(process.env.PROFILE_BUFFER_TTL_IN_SECONDS, 10)- : 60 * 60;+ private readonly batchSize =+ this.deps.config.buffers.profile.batchSize ?? DEFAULT_BATCH_SIZE;+ private readonly chunkSize =+ this.deps.config.buffers.profile.chunkSize ?? DEFAULT_CHUNK_SIZE;+ private readonly ttlInSeconds =+ this.deps.config.buffers.profile.ttlSeconds ?? DEFAULT_TTL_IN_SECONDS;/** Max profiles per ClickHouse IN-clause fetch to keep query size bounded */- private readonly fetchChunkSize = process.env.PROFILE_BUFFER_FETCH_CHUNK_SIZE- ? Number.parseInt(process.env.PROFILE_BUFFER_FETCH_CHUNK_SIZE, 10)- : 50;+ private readonly fetchChunkSize =+ this.deps.config.buffers.profile.fetchChunkSize ?? DEFAULT_FETCH_CHUNK_SIZE;private readonly redisKey = 'profile-buffer';private readonly redisProfilePrefix = 'profile-cache:';diff --git a/packages/core/src/buffers/replay-buffer.ts b/packages/core/src/buffers/replay-buffer.tsindex f030eb723..fe184d17e 100644--- a/packages/core/src/buffers/replay-buffer.ts+++ b/packages/core/src/buffers/replay-buffer.ts@@ -13,13 +13,14 @@ export interface IClickhouseSessionReplayChunk {payload: string;}+const DEFAULT_BATCH_SIZE = 500;+const DEFAULT_CHUNK_SIZE = 500;+export class ReplayBuffer extends BaseBuffer {- private batchSize = process.env.REPLAY_BUFFER_BATCH_SIZE- ? Number.parseInt(process.env.REPLAY_BUFFER_BATCH_SIZE, 10)- : 500;- private chunkSize = process.env.REPLAY_BUFFER_CHUNK_SIZE- ? Number.parseInt(process.env.REPLAY_BUFFER_CHUNK_SIZE, 10)- : 500;+ private readonly batchSize =+ this.deps.config.buffers.replay.batchSize ?? DEFAULT_BATCH_SIZE;+ private readonly chunkSize =+ this.deps.config.buffers.replay.chunkSize ?? DEFAULT_CHUNK_SIZE;private readonly redisKey = 'replay-buffer';diff --git a/packages/core/src/buffers/session-buffer.ts b/packages/core/src/buffers/session-buffer.tsindex ab0449b7a..6e2fe448b 100644--- a/packages/core/src/buffers/session-buffer.ts+++ b/packages/core/src/buffers/session-buffer.ts@@ -1,4 +1,5 @@import { getRedisCache, type Redis } from '@openpanel/redis';+import type { CoreConfig } from '../config';import type { IServiceCreateEventPayload } from '../modules/event/event.service';import type { IClickhouseSession } from '../modules/session/session.service';import { TABLE_NAMES } from '../shared/ch-tables';@@ -7,12 +8,17 @@ import { getSafeJson } from '../shared/json';import { BaseBuffer, type BufferDeps } from './base-buffer';// 30min of idle in event-time → session ends. Matches industry default.-// Idle window for a session (boundary detection + the reaper's default deadman).-// Env-overridable so E2E tests can shrink it from 30min to a few seconds.-export const SESSION_TIMEOUT_MS = Number.parseInt(- process.env.SESSION_TIMEOUT_MS || String(1000 * 60 * 30),- 10-);+const DEFAULT_SESSION_TIMEOUT_MS = 1000 * 60 * 30;++/**+ * Idle window for a session (boundary detection + the reaper's default+ * deadman). Overridable through SESSION_TIMEOUT_MS so the E2E harness can+ * shrink it from 30min to a few seconds — the harness and the stack must be+ * given the same value (apps/api/e2e/README.md).+ */+export function resolveSessionTimeoutMs(config: CoreConfig): number {+ return config.session.timeoutMs ?? DEFAULT_SESSION_TIMEOUT_MS;+}const sessionKey = (projectId: string, deviceId: string) =>`session:${projectId}:${deviceId}`;@@ -119,17 +125,17 @@ function pickUtm(return v ? String(v) : '';}+const DEFAULT_BATCH_SIZE = 1000;+const DEFAULT_CHUNK_SIZE = 1000;+export class SessionBuffer extends BaseBuffer {- private batchSize = process.env.SESSION_BUFFER_BATCH_SIZE- ? Number.parseInt(process.env.SESSION_BUFFER_BATCH_SIZE, 10)- : 1000;- private chunkSize = process.env.SESSION_BUFFER_CHUNK_SIZE- ? Number.parseInt(process.env.SESSION_BUFFER_CHUNK_SIZE, 10)- : 1000;+ private readonly batchSize =+ this.deps.config.buffers.session.batchSize ?? DEFAULT_BATCH_SIZE;+ private readonly chunkSize =+ this.deps.config.buffers.session.chunkSize ?? DEFAULT_CHUNK_SIZE;- private readonly squashEnabled =- process.env.SESSION_BUFFER_SQUASH !== 'false' &&- process.env.SESSION_BUFFER_SQUASH !== '0';+ private readonly squashEnabled = this.deps.config.buffers.session.squash;+ private readonly idleTimeoutMs = resolveSessionTimeoutMs(this.deps.config);private readonly redisKey = 'session-buffer';private redis: Redis;@@ -201,7 +207,7 @@ export class SessionBuffer extends BaseBuffer {const isBoundary =existing &&eventTimeMs - fromClickhouseDate(existing.ended_at).getTime() >- SESSION_TIMEOUT_MS;+ this.idleTimeoutMs;if (existing && !isBoundary) {const { current, chRows } = this.extendSession(existing, payload);diff --git a/packages/core/src/clients/ai/enrich.ts b/packages/core/src/clients/ai/enrich.tsindex fed0f1668..401724612 100644--- a/packages/core/src/clients/ai/enrich.ts+++ b/packages/core/src/clients/ai/enrich.ts@@ -7,6 +7,7 @@// writes the results back (this module stays db-free).import { betterAgent, defineAgent } from '@better-agent/core';import { z } from 'zod';+import type { CoreConfig } from '../../config';import { ALLOWED_MODELS, resolveModel } from './providers';// Bump when the prompt or output shape changes — the worker re-enriches any@@ -16,14 +17,14 @@ export const ENRICH_VERSION = 1;// Tier-1 is cheap, fast, non-reasoning work — gpt-4.1-mini is the right tier.const ENRICH_MODEL_ID = 'gpt-4-1-mini';-function enrichModel() {+function enrichModel(config: CoreConfig) {const entry =ALLOWED_MODELS.find((m) => m.id === ENRICH_MODEL_ID) ??ALLOWED_MODELS.find((m) => m.group === 'OpenAI');if (!entry) {throw new Error('No OpenAI model available for insight enrichment');}- return resolveModel(entry);+ return resolveModel(config, entry);}export interface InsightToEnrich {@@ -84,7 +85,7 @@ For EACH input insight, produce:Return exactly one result per input insight, preserving each "id" verbatim. Be calibrated and consistent across the batch.`;let _app: ReturnType<typeof betterAgent> | null = null;-function getApp() {+function getApp(config: CoreConfig) {if (_app) {return _app;}@@ -92,7 +93,7 @@ function getApp() {name: 'insight-enrich',description:'Scores and summarizes analytics insights (one-shot, no persistence).',- model: enrichModel(),+ model: enrichModel(config),contextSchema: z.object({}),outputSchema: {schema: enrichmentOutputJsonSchema,@@ -114,6 +115,7 @@ function getApp() {* Keep batches modest (~25) so the prompt stays small and the mapping reliable.*/export async function enrichInsights(+ config: CoreConfig,insights: InsightToEnrich[]): Promise<InsightEnrichment[]> {if (insights.length === 0) {@@ -121,7 +123,7 @@ export async function enrichInsights(}const input = JSON.stringify(insights);- const result = (await getApp().run('insight-enrich', {+ const result = (await getApp(config).run('insight-enrich', {input,context: {},// biome-ignore lint/suspicious/noExplicitAny: same dodge as filter-command.tsdiff --git a/packages/core/src/clients/ai/explain.ts b/packages/core/src/clients/ai/explain.tsindex cca70a018..5985fe307 100644--- a/packages/core/src/clients/ai/explain.ts+++ b/packages/core/src/clients/ai/explain.ts@@ -8,6 +8,7 @@// causes. The caller gathers the data (DB/CH); this module just narrates.import { betterAgent, defineAgent } from '@better-agent/core';import { z } from 'zod';+import type { CoreConfig } from '../../config';import { ALLOWED_MODELS, resolveModel } from './providers';// gpt-4.1 (the larger non-reasoning model) synthesizes the supplied breakdown@@ -15,14 +16,14 @@ import { ALLOWED_MODELS, resolveModel } from './providers';// deeper inference is wanted.const EXPLAIN_MODEL_ID = 'gpt-4-1';-function explainModel() {+function explainModel(config: CoreConfig) {const entry =ALLOWED_MODELS.find((m) => m.id === EXPLAIN_MODEL_ID) ??ALLOWED_MODELS.find((m) => m.group === 'OpenAI');if (!entry) {throw new Error('No OpenAI model available for insight explanation');}- return resolveModel(entry);+ return resolveModel(config, entry);}export interface BreakdownComparison {@@ -99,14 +100,14 @@ Produce:Be honest and precise. You can only see what's in the data: explain the shape and the internal decomposition (which segment moved, when), not external causes you can't observe. If the data doesn't clearly explain it, say so and set confidence low. Never invent numbers or dates.`;let _app: ReturnType<typeof betterAgent> | null = null;-function getApp() {+function getApp(config: CoreConfig) {if (_app) {return _app;}const agent = defineAgent({name: 'insight-explain',description: 'Explains why an insight changed (one-shot).',- model: explainModel(),+ model: explainModel(config),contextSchema: z.object({}),outputSchema: {schema: explanationJsonSchema,@@ -123,9 +124,10 @@ function getApp() {}export async function generateInsightExplanation(+ config: CoreConfig,input: ExplainInsightInput): Promise<InsightExplanation | null> {- const result = (await getApp().run('insight-explain', {+ const result = (await getApp(config).run('insight-explain', {input: JSON.stringify(input),context: {},// biome-ignore lint/suspicious/noExplicitAny: same dodge as enrich.tsdiff --git a/packages/core/src/clients/ai/narrative.ts b/packages/core/src/clients/ai/narrative.tsindex bf9e762a6..8bf4f2146 100644--- a/packages/core/src/clients/ai/narrative.ts+++ b/packages/core/src/clients/ai/narrative.ts@@ -6,18 +6,19 @@// pattern as enrich.ts / filter-command.ts.import { betterAgent, defineAgent } from '@better-agent/core';import { z } from 'zod';+import type { CoreConfig } from '../../config';import { ALLOWED_MODELS, resolveModel } from './providers';const NARRATIVE_MODEL_ID = 'gpt-4-1-mini';-function narrativeModel() {+function narrativeModel(config: CoreConfig) {const entry =ALLOWED_MODELS.find((m) => m.id === NARRATIVE_MODEL_ID) ??ALLOWED_MODELS.find((m) => m.group === 'OpenAI');if (!entry) {throw new Error('No OpenAI model available for weekly narrative');}- return resolveModel(entry);+ return resolveModel(config, entry);}export interface WeeklyNarrativeInput {@@ -49,14 +50,14 @@ Rules:- If the week was quiet, say so plainly and briefly. Never invent data.`;let _app: ReturnType<typeof betterAgent> | null = null;-function getApp() {+function getApp(config: CoreConfig) {if (_app) {return _app;}const agent = defineAgent({name: 'weekly-narrative',description: 'Writes a weekly digest intro paragraph (one-shot).',- model: narrativeModel(),+ model: narrativeModel(config),contextSchema: z.object({}),outputSchema: {schema: narrativeOutputJsonSchema,@@ -73,9 +74,10 @@ function getApp() {}export async function generateWeeklyNarrative(+ config: CoreConfig,input: WeeklyNarrativeInput): Promise<string> {- const result = (await getApp().run('weekly-narrative', {+ const result = (await getApp(config).run('weekly-narrative', {input: JSON.stringify(input),context: {},// biome-ignore lint/suspicious/noExplicitAny: same dodge as enrich.tsdiff --git a/packages/core/src/clients/ai/providers.ts b/packages/core/src/clients/ai/providers.tsindex f185f3e41..fc50ca407 100644--- a/packages/core/src/clients/ai/providers.ts+++ b/packages/core/src/clients/ai/providers.ts@@ -1,78 +1,58 @@// Ported from @openpanel/ai (dissolved into core — M4-005).//-// SERVER-ONLY. This module instantiates `@better-agent/providers` clients and-// reads provider API keys from `process.env`. Never value-import it from the-// browser (apps/start) — import the model catalog/types from+// SERVER-ONLY. This module instantiates `@better-agent/providers` clients from+// the credentials the config loader parsed (ADR-022 R9: config in). Never+// value-import it from the browser (apps/start) — import the model+// catalog/types from// `@openpanel/core/modules/assistant/assistant.constants` instead.import { createAnthropic } from '@better-agent/providers/anthropic';import { createOpenAI } from '@better-agent/providers/openai';-import { z } from 'zod';+import type { CoreConfig } from '../../config';import type { ChatModelEntry } from '../../modules/assistant/assistant.constants';export type { ChatModelEntry } from '../../modules/assistant/assistant.constants';export { CHAT_MODELS as ALLOWED_MODELS } from '../../modules/assistant/assistant.constants';-const openAiEnvSchema = z.object({- OPENAI_API_KEY: z.string().optional(),- OPENAI_BASE_URL: z.string().optional(),- OPENAI_PROJECT: z.string().optional(),- OPENAI_ORGANIZATION: z.string().optional(),-});--const anthropicEnvSchema = z.object({- ANTHROPIC_API_KEY: z.string().optional(),- ANTHROPIC_BASE_URL: z.string().optional(),- ANTHROPIC_TOKEN: z.string().optional(),- ANTHROPIC_VERSION: z.string().optional(),-});-+// One provider client per process, built on first use. The credentials are+// the same object for every caller — `loadConfig` runs once at boot — so the+// memo cannot serve one caller another caller's key.let _openai: ReturnType<typeof createOpenAI> | null = null;-function openai() {+function openai(config: CoreConfig) {if (!_openai) {- const {- OPENAI_API_KEY,- OPENAI_BASE_URL,- OPENAI_PROJECT,- OPENAI_ORGANIZATION,- } = openAiEnvSchema.parse(process.env);+ const { apiKey, baseUrl, project, organization } = config.ai.openai;- if (!OPENAI_API_KEY) {+ if (!apiKey) {console.warn(`[chat] OPENAI_API_KEY is not set. Models routed through OpenAI will fail with "x-api-key required" until you add it to the API's env.`);}_openai = createOpenAI({- apiKey: OPENAI_API_KEY,- baseURL: OPENAI_BASE_URL,- project: OPENAI_PROJECT,- organization: OPENAI_ORGANIZATION,+ apiKey,+ baseURL: baseUrl,+ project,+ organization,});}return _openai;}let _anthropic: ReturnType<typeof createAnthropic> | null = null;-function anthropic() {+function anthropic(config: CoreConfig) {if (!_anthropic) {- const {- ANTHROPIC_API_KEY,- ANTHROPIC_BASE_URL,- ANTHROPIC_TOKEN,- ANTHROPIC_VERSION,- } = anthropicEnvSchema.parse(process.env);+ const { apiKey, baseUrl, authToken, version } = config.ai.anthropic;- if (!ANTHROPIC_API_KEY) {+ if (!apiKey) {console.warn(`[chat] ANTHROPIC_API_KEY is not set. Models routed through Anthropic will fail with "x-api-key required" until you add it to the API's env.`);}_anthropic = createAnthropic({- apiKey: ANTHROPIC_API_KEY,- baseURL: ANTHROPIC_BASE_URL,- authToken: ANTHROPIC_TOKEN,- anthropicVersion: ANTHROPIC_VERSION,+ apiKey,+ baseURL: baseUrl,+ authToken,+ anthropicVersion: version,});}return _anthropic;@@ -81,13 +61,13 @@ function anthropic() {export const openaiProvider = openai;export const anthropicProvider = anthropic;-export function resolveModel(entry: ChatModelEntry) {+export function resolveModel(config: CoreConfig, entry: ChatModelEntry) {switch (entry.group) {case 'OpenAI':// biome-ignore lint/suspicious/noExplicitAny: OpenAI model id union is open- return openai().model(entry.modelId as any);+ return openai(config).model(entry.modelId as any);case 'Anthropic':// biome-ignore lint/suspicious/noExplicitAny: Anthropic model id union is open- return anthropic().model(entry.modelId as any);+ return anthropic(config).model(entry.modelId as any);}}diff --git a/packages/core/src/clients/ai/win-back.ts b/packages/core/src/clients/ai/win-back.tsindex 3a9ff826a..1c0cb8621 100644--- a/packages/core/src/clients/ai/win-back.ts+++ b/packages/core/src/clients/ai/win-back.ts@@ -8,18 +8,19 @@// the same facts.import { betterAgent, defineAgent } from '@better-agent/core';import { z } from 'zod';+import type { CoreConfig } from '../../config';import { ALLOWED_MODELS, resolveModel } from './providers';const WIN_BACK_MODEL_ID = 'gpt-4-1-mini';-function winBackModel() {+function winBackModel(config: CoreConfig) {const entry =ALLOWED_MODELS.find((m) => m.id === WIN_BACK_MODEL_ID) ??ALLOWED_MODELS.find((m) => m.group === 'OpenAI');if (!entry) {throw new Error('No OpenAI model available for win-back pitch');}- return resolveModel(entry);+ return resolveModel(config, entry);}export interface WinBackPitchInput {@@ -55,14 +56,14 @@ Rules:- Contractions are fine.`;let _app: ReturnType<typeof betterAgent> | null = null;-function getApp() {+function getApp(config: CoreConfig) {if (_app) {return _app;}const agent = defineAgent({name: 'win-back-pitch',description: 'Writes a wind-down win-back paragraph (one-shot).',- model: winBackModel(),+ model: winBackModel(config),contextSchema: z.object({}),outputSchema: {schema: winBackOutputJsonSchema,@@ -79,9 +80,10 @@ function getApp() {}export async function generateWinBackPitch(+ config: CoreConfig,input: WinBackPitchInput): Promise<string> {- const result = (await getApp().run('win-back-pitch', {+ const result = (await getApp(config).run('win-back-pitch', {input: JSON.stringify(input),context: {},// biome-ignore lint/suspicious/noExplicitAny: same dodge as enrich.tsdiff --git a/packages/core/src/clients/integrations/export/batch-creator.ts b/packages/core/src/clients/integrations/export/batch-creator.tsindex d87f0ffb7..122da8d5e 100644--- a/packages/core/src/clients/integrations/export/batch-creator.ts+++ b/packages/core/src/clients/integrations/export/batch-creator.ts@@ -1,12 +1,10 @@import { Readable } from 'node:stream';import { pipeline } from 'node:stream/promises';import { createGzip } from 'node:zlib';+import type { Logger } from '../../../logger';import { generateSecureId } from '../../../shared/id';-import { createLogger } from '../../logger';import type { IExportEvent } from './export-event';-const logger = createLogger({ name: 'batch-creator' });-/*** Supported export formats*/@@ -145,6 +143,7 @@ async function gzipCompress(content: string): Promise<Buffer> {* Create a batch of events in the specified format*/export async function createBatch(+ logger: Logger,projectId: string,integrationId: string,events: IExportEvent[],diff --git a/packages/core/src/clients/integrations/object-store/gcs-adapter.test.ts b/packages/core/src/clients/integrations/object-store/gcs-adapter.test.tsindex 8de9787c2..614c32eb0 100644--- a/packages/core/src/clients/integrations/object-store/gcs-adapter.test.ts+++ b/packages/core/src/clients/integrations/object-store/gcs-adapter.test.ts@@ -1,5 +1,6 @@import { beforeAll, describe, expect, it } from 'bun:test';import { gunzipSync, gzipSync } from 'node:zlib';+import { testCoreConfig } from '../../../../test/config-fixture';import { encryptCredential } from '../../../shared/encryption';import { createGCSAdapter } from './gcs-adapter';@@ -16,7 +17,7 @@ import { createGCSAdapter } from './gcs-adapter';* The whole suite skips when it isn't reachable, so `pnpm test` stays green* without Docker.*/-const EMULATOR = process.env.GCS_API_ENDPOINT ?? 'http://localhost:4443';+const EMULATOR = 'http://localhost:4443';const BUCKET = 'op-gcs-adapter-test';// A structurally valid service account key. fake-gcs-server does no auth, and@@ -60,18 +61,44 @@ async function readObject(bucket: string, key: string): Promise<Buffer> {return Buffer.from(await res.arrayBuffer());}-function adapter(- overrides: { bucket?: string; serviceAccountKey?: string } = {}+// GCS_API_ENDPOINT and ENCRYPTION_KEY arrive as config, not as env. The+// endpoint is set by the emulator block's `beforeAll` and left unset for the+// validation-only tests above it, exactly as the env write it replaces did.+let emulatorEndpoint: string | undefined;++function coreConfig(+ overrides: { gcsApiEndpoint?: string; encryptionKey?: string } = {}) {- return createGCSAdapter({- type: 'gcs_export',- bucket: overrides.bucket ?? BUCKET,- prefix: 'openpanel-exports',- format: 'jsonl_gzip',- serviceAccountKey: overrides.serviceAccountKey ?? SERVICE_ACCOUNT_KEY,+ const base = testCoreConfig();+ return testCoreConfig({+ encryptionKey: overrides.encryptionKey,+ objectStoreExport: {+ ...base.objectStoreExport,+ gcsApiEndpoint: overrides.gcsApiEndpoint ?? emulatorEndpoint,+ },});}+function adapter(+ overrides: {+ bucket?: string;+ serviceAccountKey?: string;+ gcsApiEndpoint?: string;+ encryptionKey?: string;+ } = {}+) {+ return createGCSAdapter(+ {+ type: 'gcs_export',+ bucket: overrides.bucket ?? BUCKET,+ prefix: 'openpanel-exports',+ format: 'jsonl_gzip',+ serviceAccountKey: overrides.serviceAccountKey ?? SERVICE_ACCOUNT_KEY,+ },+ coreConfig(overrides)+ );+}+// Runs without the emulator: the credential document is rejected before any// client is constructed, so there is nothing to connect to.describe('GCSAdapter credential validation', () => {@@ -118,7 +145,7 @@ const available = await emulatorReachable();describe.skipIf(!available)('GCSAdapter (fake-gcs-server)', () => {beforeAll(async () => {- process.env.GCS_API_ENDPOINT = EMULATOR;+ emulatorEndpoint = EMULATOR;await createBucket(BUCKET);});@@ -211,11 +238,15 @@ describe.skipIf(!available)('GCSAdapter (fake-gcs-server)', () => {});it('decrypts an encrypted service account key', async () => {- process.env.ENCRYPTION_KEY = 'a'.repeat(64);+ const encryptionKey = 'a'.repeat(64);const key = 'upload/encrypted-creds.txt';await adapter({- serviceAccountKey: encryptCredential(SERVICE_ACCOUNT_KEY),+ encryptionKey,+ serviceAccountKey: encryptCredential(+ encryptionKey,+ SERVICE_ACCOUNT_KEY+ ),}).upload({bucket: BUCKET,key,diff --git a/packages/core/src/clients/integrations/object-store/gcs-adapter.ts b/packages/core/src/clients/integrations/object-store/gcs-adapter.tsindex eaf407ccc..8c7ecf869 100644--- a/packages/core/src/clients/integrations/object-store/gcs-adapter.ts+++ b/packages/core/src/clients/integrations/object-store/gcs-adapter.ts@@ -1,5 +1,6 @@// Ported from @openpanel/integrations (dissolved into core — M4-005).import { Storage } from '@google-cloud/storage';+import type { CoreConfig } from '../../../config';import {type IGCSExportConfig,parseServiceAccountKey,@@ -16,8 +17,8 @@ import type {// pino-pretty transport, so a module that is merely imported (every test file// that reaches the object-store barrel) must not pay for one unused.let _logger: ILogger | null = null;-function logger(): ILogger {- _logger ??= createLogger({ name: 'gcs-adapter' });+function logger(coreConfig: CoreConfig): ILogger {+ _logger ??= createLogger({ name: 'gcs-adapter', config: coreConfig });return _logger;}@@ -32,11 +33,17 @@ export class GCSAdapter implements IObjectStoreAdapter {private config: IGCSExportConfig;private storage: Storage | null = null;- constructor(config: IGCSExportConfig) {+ constructor(+ config: IGCSExportConfig,+ private readonly coreConfig: CoreConfig+ ) {// Decrypt the service account key if encryptedthis.config = {...config,- serviceAccountKey: decryptCredential(config.serviceAccountKey),+ serviceAccountKey: decryptCredential(+ coreConfig.encryptionKey,+ config.serviceAccountKey+ ),};}@@ -62,7 +69,7 @@ export class GCSAdapter implements IObjectStoreAdapter {// any future caller that skips it.const parsed = parseServiceAccountKey(this.config.serviceAccountKey);if (!parsed.ok) {- logger().error(+ logger(this.coreConfig).error({ reason: parsed.error },'Rejected GCS credential document');@@ -79,7 +86,7 @@ export class GCSAdapter implements IObjectStoreAdapter {// Deliberately NOT the SDK's own `STORAGE_EMULATOR_HOST`: in v7 that var// is applied to the JSON API base but not the upload base, so metadata// reads and uploads can't both resolve. `apiEndpoint` sets both.- const apiEndpoint = process.env.GCS_API_ENDPOINT;+ const apiEndpoint = this.coreConfig.objectStoreExport.gcsApiEndpoint;this.storage = new Storage({// Allowlisted fields only, and deliberately WITHOUT `type`: with no@@ -99,7 +106,7 @@ export class GCSAdapter implements IObjectStoreAdapter {...(apiEndpoint ? { apiEndpoint } : {}),});- logger().debug(+ logger(this.coreConfig).debug({projectId: credentials.project_id,},@@ -108,7 +115,7 @@ export class GCSAdapter implements IObjectStoreAdapter {return this.storage;} catch (error) {- logger().error({ error }, 'Failed to create GCS client');+ logger(this.coreConfig).error({ error }, 'Failed to create GCS client');throw new Error('Failed to create GCS client');}}@@ -139,7 +146,7 @@ export class GCSAdapter implements IObjectStoreAdapter {// it with getMetadata() would double the request count of every export.const metadata = file.metadata;- logger().debug(+ logger(this.coreConfig).debug({bucket: options.bucket,key: options.key,@@ -155,7 +162,7 @@ export class GCSAdapter implements IObjectStoreAdapter {location: `gs://${options.bucket}/${options.key}`,};} catch (error) {- logger().error(+ logger(this.coreConfig).error({error,bucket: options.bucket,@@ -237,6 +244,9 @@ export class GCSAdapter implements IObjectStoreAdapter {/*** Create a GCS adapter from integration config*/-export function createGCSAdapter(config: IGCSExportConfig): GCSAdapter {- return new GCSAdapter(config);+export function createGCSAdapter(+ config: IGCSExportConfig,+ coreConfig: CoreConfig+): GCSAdapter {+ return new GCSAdapter(config, coreConfig);}diff --git a/packages/core/src/clients/integrations/object-store/s3-adapter.ts b/packages/core/src/clients/integrations/object-store/s3-adapter.tsindex 6c1c042ce..1f2a5abb4 100644--- a/packages/core/src/clients/integrations/object-store/s3-adapter.ts+++ b/packages/core/src/clients/integrations/object-store/s3-adapter.ts@@ -9,6 +9,7 @@ import {S3Client,} from '@aws-sdk/client-s3';import { AssumeRoleCommand, STSClient } from '@aws-sdk/client-sts';+import type { CoreConfig } from '../../../config';import type { IS3ExportConfig } from '../../../modules/integration/integration.constants';import { decryptCredential } from '../../../shared/encryption';import { assertSafeUrl, createPinnedLookup } from '../../../shared/ssrf';@@ -23,8 +24,8 @@ import type {// pino-pretty transport, so a module that is merely imported (every test file// that reaches the object-store barrel) must not pay for one unused.let _logger: ILogger | null = null;-function logger(): ILogger {- _logger ??= createLogger({ name: 's3-adapter' });+function logger(coreConfig: CoreConfig): ILogger {+ _logger ??= createLogger({ name: 's3-adapter', config: coreConfig });return _logger;}@@ -56,12 +57,18 @@ export class S3Adapter implements IObjectStoreAdapter {private clientPromise: Promise<S3Client> | null = null;private clientExpiresAt = 0;- constructor(config: IS3ExportConfig) {+ constructor(+ config: IS3ExportConfig,+ private readonly coreConfig: CoreConfig+ ) {// Decrypt secretAccessKey if present and encryptedif (config.authMode === 'access_key') {this.config = {...config,- secretAccessKey: decryptCredential(config.secretAccessKey),+ secretAccessKey: decryptCredential(+ coreConfig.encryptionKey,+ config.secretAccessKey+ ),};} else {this.config = config;@@ -86,7 +93,7 @@ export class S3Adapter implements IObjectStoreAdapter {// Self-hosted returns null (guard skipped) — a single tenant already owns// the network, and internal MinIO endpoints are a legitimate use.const addresses = this.config.endpoint- ? await assertSafeUrl(this.config.endpoint)+ ? await assertSafeUrl(this.coreConfig.selfHosted, this.config.endpoint): null;return this.getClientWithAccessKeys(addresses?.[0]);@@ -127,7 +134,7 @@ export class S3Adapter implements IObjectStoreAdapter {: {}),});- logger().debug(+ logger(this.coreConfig).debug({region: this.config.region,endpoint: this.config.endpoint || 'default',@@ -206,7 +213,7 @@ export class S3Adapter implements IObjectStoreAdapter {},});- logger().debug(+ logger(this.coreConfig).debug({roleArn: this.config.roleArn,expiresAt: new Date(this.clientExpiresAt).toISOString(),@@ -216,7 +223,7 @@ export class S3Adapter implements IObjectStoreAdapter {return s3Client;} catch (error) {- logger().error(+ logger(this.coreConfig).error({error,roleArn: this.config.roleArn,@@ -270,7 +277,7 @@ export class S3Adapter implements IObjectStoreAdapter {const command = new PutObjectCommand(putParams);const response = await client.send(command);- logger().debug(+ logger(this.coreConfig).debug({bucket: options.bucket,key: options.key,@@ -286,7 +293,7 @@ export class S3Adapter implements IObjectStoreAdapter {location: `s3://${options.bucket}/${options.key}`,};} catch (error) {- logger().error(+ logger(this.coreConfig).error({error,bucket: options.bucket,@@ -349,7 +356,10 @@ export class S3Adapter implements IObjectStoreAdapter {const status = (error as { $metadata?: { httpStatusCode?: number } } | null)?.$metadata?.httpStatusCode;- logger().warn({ err: error, bucket: this.config.bucket }, 'S3 test failed');+ logger(this.coreConfig).warn(+ { err: error, bucket: this.config.bucket },+ 'S3 test failed'+ );if (status === 404 || name === 'NotFound' || name === 'NoSuchBucket') {return `Bucket '${this.config.bucket}' does not exist or is not accessible`;@@ -371,6 +381,9 @@ export class S3Adapter implements IObjectStoreAdapter {/*** Create an S3 adapter from integration config*/-export function createS3Adapter(config: IS3ExportConfig): S3Adapter {- return new S3Adapter(config);+export function createS3Adapter(+ config: IS3ExportConfig,+ coreConfig: CoreConfig+): S3Adapter {+ return new S3Adapter(config, coreConfig);}diff --git a/packages/core/src/clients/integrations/registry.test.ts b/packages/core/src/clients/integrations/registry.test.tsindex ef347b0af..e015d4a70 100644--- a/packages/core/src/clients/integrations/registry.test.ts+++ b/packages/core/src/clients/integrations/registry.test.ts@@ -1,4 +1,4 @@-import { beforeAll, describe, expect, it } from 'bun:test';+import { describe, expect, it } from 'bun:test';import {carryOverConfigSecrets,encryptConfigSecrets,@@ -7,9 +7,10 @@ import {redactConfigSecrets,} from './registry';-beforeAll(() => {- process.env.ENCRYPTION_KEY = 'a'.repeat(64);-});+/** The ENCRYPTION_KEY the config loader would hand down. */+const KEY = 'a'.repeat(64);++const encryptSecrets = <C>(config: C): C => encryptConfigSecrets(KEY, config);const gcs = (serviceAccountKey: string) =>({@@ -64,19 +65,19 @@ describe('config secret handling', () => {});it('encrypts declared secrets before persisting', () => {- const encrypted = encryptConfigSecrets(gcs('super-secret'));+ const encrypted = encryptSecrets(gcs('super-secret'));expect(encrypted.serviceAccountKey.startsWith('enc:')).toBe(true);expect(encrypted.serviceAccountKey).not.toContain('super-secret');});it('carries a blank secret over from the stored row on update', () => {- const stored = encryptConfigSecrets(gcs('super-secret'));+ const stored = encryptSecrets(gcs('super-secret'));const submitted = carryOverConfigSecrets(gcs(''), stored);expect(submitted.serviceAccountKey).toBe(stored.serviceAccountKey);});it('does not let a submitted secret be overwritten by the stored one', () => {- const stored = encryptConfigSecrets(gcs('old'));+ const stored = encryptSecrets(gcs('old'));const submitted = carryOverConfigSecrets(gcs('new-key'), stored);expect(submitted.serviceAccountKey).toBe('new-key');});@@ -175,7 +176,7 @@ describe('config secret handling', () => {it('does not encrypt redact-only secrets', () => {// The notification senders read these raw; encrypting without decrypting at// use would break delivery.- const slack = encryptConfigSecrets({+ const slack = encryptSecrets({type: 'slack',access_token: 'xoxb-super-secret',incoming_webhook: { url: 'https://hooks.slack.com/x' },diff --git a/packages/core/src/clients/integrations/registry.ts b/packages/core/src/clients/integrations/registry.tsindex 623b0ba29..fdaed2042 100644--- a/packages/core/src/clients/integrations/registry.ts+++ b/packages/core/src/clients/integrations/registry.ts@@ -1,9 +1,11 @@// Ported from @openpanel/integrations (dissolved into core — M4-005).++import type { CoreConfig } from '../../config';import {type IIntegrationConfig,looksEncrypted,} from '../../modules/integration/integration.constants';-import { encryptCredential } from '../../shared/encryption';+import { type EncryptionKey, encryptCredential } from '../../shared/encryption';import {execute as executeJavaScriptTemplate,validate as validateJavaScriptTemplate,@@ -62,14 +64,18 @@ export interface IServerIntegration<T extends IIntegrationConfig['type']> {};// Object-store export sink.export?: {- createAdapter(config: ConfigOf<T>): IObjectStoreAdapter;+ createAdapter(+ config: ConfigOf<T>,+ coreConfig: CoreConfig+ ): IObjectStoreAdapter;};// Optional synchronous config validation run before persisting (e.g. webhook// JS template). Returning invalid rejects the create/update.validateConfig?(config: ConfigOf<T>): { valid: boolean; error?: string };// Optional pre-save connection test (used by the generic tRPC procedure).testConnection?(- config: ConfigOf<T>+ config: ConfigOf<T>,+ coreConfig: CoreConfig): Promise<{ success: boolean; error?: string }>;/*** Config values that must never travel back to a client. ONE declaration@@ -208,9 +214,10 @@ const webhookServer: IServerIntegration<'webhook'> = {const s3Server: IServerIntegration<'s3_export'> = {type: 's3_export',export: {- createAdapter: (config) => createS3Adapter(config),+ createAdapter: (config, coreConfig) => createS3Adapter(config, coreConfig),},- testConnection: (config) => createS3Adapter(config).testConnection(),+ testConnection: (config, coreConfig) =>+ createS3Adapter(config, coreConfig).testConnection(),// Absent in iam_role configs; the generic helpers skip keys that aren't there.secretFields: [{ path: 'secretAccessKey', encrypted: true }],};@@ -218,9 +225,10 @@ const s3Server: IServerIntegration<'s3_export'> = {const gcsServer: IServerIntegration<'gcs_export'> = {type: 'gcs_export',export: {- createAdapter: (config) => createGCSAdapter(config),+ createAdapter: (config, coreConfig) => createGCSAdapter(config, coreConfig),},- testConnection: (config) => createGCSAdapter(config).testConnection(),+ testConnection: (config, coreConfig) =>+ createGCSAdapter(config, coreConfig).testConnection(),secretFields: [{ path: 'serviceAccountKey', encrypted: true }],};@@ -359,9 +367,9 @@ function mapSecrets<C>(}/** Encrypt every declared secret that is stored encrypted. */-export function encryptConfigSecrets<C>(config: C): C {+export function encryptConfigSecrets<C>(key: EncryptionKey, config: C): C {return mapSecrets(config, (value, secret) =>- secret.encrypted ? encryptCredential(value) : undefined+ secret.encrypted ? encryptCredential(key, value) : undefined);}diff --git a/packages/core/src/clients/integrations/slack.ts b/packages/core/src/clients/integrations/slack.tsindex e24e1ca51..5ff35a482 100644--- a/packages/core/src/clients/integrations/slack.ts+++ b/packages/core/src/clients/integrations/slack.ts@@ -2,6 +2,7 @@// Cred to (@c_alares) https://github.com/christianalares/seventy-seven/blob/main/packages/integrations/src/slack/index.tsimport * as Slack from '@slack/bolt';+import type { CoreConfig } from '../../config';import {browserFetcher,postWebhook,@@ -13,44 +14,52 @@ const { LogLevel, App: SlackApp } = Slack;import { InstallProvider } from '@slack/oauth';-const SLACK_CLIENT_ID = process.env.SLACK_CLIENT_ID;-const SLACK_CLIENT_SECRET = process.env.SLACK_CLIENT_SECRET;-const SLACK_OAUTH_REDIRECT_URL = process.env.SLACK_OAUTH_REDIRECT_URL;-const SLACK_STATE_SECRET = process.env.SLACK_STATE_SECRET;+const INSTALL_SCOPES = [+ 'incoming-webhook',+ 'chat:write',+ 'chat:write.public',+ 'team:read',+];-export const slackInstaller = SLACK_CLIENT_ID- ? new InstallProvider({- clientId: SLACK_CLIENT_ID!,- clientSecret: SLACK_CLIENT_SECRET!,- stateSecret: SLACK_STATE_SECRET,- logLevel:- process.env.NODE_ENV === 'development' ? LogLevel.DEBUG : undefined,- })- : ({+/**+ * Built from the credentials the config loader parsed (ADR-022 R9). Without a+ * client id there is no install flow, and the stub keeps the two members the+ * integration module touches so an unconfigured deployment still boots.+ */+export function slackInstaller(config: CoreConfig): InstallProvider {+ const { clientId, clientSecret, stateSecret } = config.slack;+ if (!clientId) {+ return {generateInstallUrl: () => {},stateStore: {},- } as unknown as InstallProvider);+ } as unknown as InstallProvider;+ }+ return new InstallProvider({+ clientId,+ // An id without a secret is a misconfiguration Slack itself rejects.+ clientSecret: clientSecret ?? '',+ stateSecret,+ logLevel: config.isDevelopment ? LogLevel.DEBUG : undefined,+ });+}export const getSlackInstallUrl = ({+ config,integrationId,organizationId,projectId,}: {+ config: CoreConfig;integrationId: string;organizationId: string;projectId: string;}) => {- if (!SLACK_CLIENT_ID) {+ if (!config.slack.clientId) {throw new Error('SLACK_CLIENT_ID is not set (slack.ts)');}- return slackInstaller.generateInstallUrl({- scopes: [- 'incoming-webhook',- 'chat:write',- 'chat:write.public',- 'team:read',- ],- redirectUri: SLACK_OAUTH_REDIRECT_URL,+ return slackInstaller(config).generateInstallUrl({+ scopes: INSTALL_SCOPES,+ redirectUri: config.slack.oauthRedirectUrl,metadata: JSON.stringify({ integrationId, organizationId, projectId }),});};diff --git a/packages/core/src/clients/logger.ts b/packages/core/src/clients/logger.tsindex e9d2d1ad4..cc28030b1 100644--- a/packages/core/src/clients/logger.ts+++ b/packages/core/src/clients/logger.ts@@ -9,18 +9,10 @@import * as HyperDX from '@hyperdx/node-opentelemetry';import pino, { type Logger as PinoLogger } from 'pino';+import type { CoreConfig } from '../config';export type ILogger = PinoLogger;-const logLevel = process.env.LOG_LEVEL ?? 'info';-const silent = process.env.LOG_SILENT === 'true';--// Exactly one shipping path at a time (see logging-capture-plan.md):-// - 'otlp': pino ships via the HyperDX transport (requires HYPERDX_API_KEY).-// - 'stdout': pino writes JSON to stdout; an external collector ships it.-const logExporter =- process.env.LOG_EXPORTER ?? (process.env.HYPERDX_API_KEY ? 'otlp' : 'stdout');-// Originals captured before interceptProcessOutput wraps the streams. Code// that must bypass capture (e.g. crash handlers mirroring fatals to stderr)// uses these so the line isn't re-ingested and shipped twice.@@ -91,8 +83,9 @@ function redactSensitive(value: unknown, depth = 0): unknown {// Shared by logs and traces so both signals land under the same service// name in ClickStack (e.g. new-api-production).-export function getServiceName(name: string): string {- return [process.env.LOG_PREFIX, name, process.env.NODE_ENV ?? 'dev']+export function getServiceName(config: CoreConfig, name: string): string {+ const { serviceNamePrefix, serviceNameEnvironment } = config.logging;+ return [serviceNamePrefix, name, serviceNameEnvironment].filter(Boolean).join('-');}@@ -107,16 +100,22 @@ export function getServiceName(name: string): string {// (V1 api/worker) is unaffected.const isBun = !!process.versions.bun;-export function createLogger({ name }: { name: string }): ILogger {- const service = getServiceName(name);+export function createLogger({+ name,+ config,+}: {+ name: string;+ config: CoreConfig;+}): ILogger {+ const service = getServiceName(config, name);+ const { level, silent, exporter, hyperdxApiKey } = config.logging;- const useHyperDX = logExporter === 'otlp' && !!process.env.HYPERDX_API_KEY;- const usePretty =- !(useHyperDX || isBun) && process.env.NODE_ENV !== 'production';+ const useHyperDX = exporter === 'otlp' && !!hyperdxApiKey;+ const usePretty = !(useHyperDX || isBun || config.isProduction);return pino({name: service,- level: logLevel,+ level,enabled: !silent,formatters: {log: (obj) => {@@ -125,11 +124,9 @@ export function createLogger({ name }: { name: string }): ILogger {},// Keep trace_id/span_id on every line even in stdout mode so trace↔log// correlation survives when a collector does the shipping.- mixin: process.env.HYPERDX_API_KEY- ? HyperDX.getPinoMixinFunction- : undefined,+ mixin: hyperdxApiKey ? HyperDX.getPinoMixinFunction : undefined,transport: useHyperDX- ? HyperDX.getPinoTransport(logLevel, {+ ? HyperDX.getPinoTransport(level, {detectResources: true,service,})@@ -165,21 +162,20 @@ let intercepted = false;* `docker logs` stays useful. In stdout mode pino's JSON line on stdout IS* the container output — teeing would print everything twice.*/-export function interceptProcessOutput(logger: ILogger): void {+export function interceptProcessOutput(+ config: CoreConfig,+ logger: ILogger+): void {if (intercepted) {return;}// Keep local dev output untouched.- if (- process.env.NODE_ENV !== 'production' &&- !process.env.HYPERDX_API_KEY &&- process.env.LOG_EXPORTER === undefined- ) {+ if (!config.logging.interceptProcessOutput) {return;}intercepted = true;- const passthrough = logExporter !== 'stdout';+ const passthrough = config.logging.exporter !== 'stdout';let logging = false;const wrap = (diff --git a/packages/core/src/code-migrations/10-add-session-replay.ts b/packages/core/src/code-migrations/10-add-session-replay.tsindex b6c08b329..6b2f2bbd3 100644--- a/packages/core/src/code-migrations/10-add-session-replay.ts+++ b/packages/core/src/code-migrations/10-add-session-replay.ts@@ -6,10 +6,10 @@ import {modifyTTL,runClickhouseMigrationCommands,} from '@openpanel/db/src/clickhouse/migration';-import { getIsCluster } from './helpers';+import { type CodeMigrationEnv, getIsCluster } from './helpers';-export async function up() {- const isClustered = getIsCluster();+export async function up(env: CodeMigrationEnv) {+ const isClustered = getIsCluster(env);const sqls: string[] = [...createTable({diff --git a/packages/core/src/code-migrations/11-add-groups.ts b/packages/core/src/code-migrations/11-add-groups.tsindex ce0a08509..9580fa0bb 100644--- a/packages/core/src/code-migrations/11-add-groups.ts+++ b/packages/core/src/code-migrations/11-add-groups.ts@@ -6,10 +6,10 @@ import {createTable,runClickhouseMigrationCommands,} from '@openpanel/db/src/clickhouse/migration';-import { getIsCluster } from './helpers';+import { type CodeMigrationEnv, getIsCluster } from './helpers';-export async function up() {- const isClustered = getIsCluster();+export async function up(env: CodeMigrationEnv) {+ const isClustered = getIsCluster(env);const sqls: string[] = [...addColumns(diff --git a/packages/core/src/code-migrations/12-add-gsc.ts b/packages/core/src/code-migrations/12-add-gsc.tsindex a0267fe8a..33afa98c2 100644--- a/packages/core/src/code-migrations/12-add-gsc.ts+++ b/packages/core/src/code-migrations/12-add-gsc.ts@@ -4,10 +4,10 @@ import {createTable,runClickhouseMigrationCommands,} from '@openpanel/db/src/clickhouse/migration';-import { getIsCluster } from './helpers';+import { type CodeMigrationEnv, getIsCluster } from './helpers';-export async function up() {- const isClustered = getIsCluster();+export async function up(env: CodeMigrationEnv) {+ const isClustered = getIsCluster(env);const commonMetricColumns = ['`clicks` UInt32 CODEC(Delta(4), LZ4)',diff --git a/packages/core/src/code-migrations/13-cohorts.ts b/packages/core/src/code-migrations/13-cohorts.tsindex fc6df64b4..fc067a3e6 100644--- a/packages/core/src/code-migrations/13-cohorts.ts+++ b/packages/core/src/code-migrations/13-cohorts.ts@@ -8,12 +8,12 @@ import {getExistingTables,runClickhouseMigrationCommands,} from '@openpanel/db/src/clickhouse/migration';-import { getIsCluster } from './helpers';+import { type CodeMigrationEnv, getIsCluster } from './helpers';-export async function up() {+export async function up(env: CodeMigrationEnv) {const replicatedVersion = '1';const existingTables = await getExistingTables();- const isClustered = getIsCluster();+ const isClustered = getIsCluster(env);const sqls: string[] = [];@@ -138,8 +138,8 @@ export async function up() {}}-export async function down() {- const isClustered = getIsCluster();+export async function down(env: CodeMigrationEnv) {+ const isClustered = getIsCluster(env);const sqls = [dropTable(diff --git a/packages/core/src/code-migrations/14-profile-event-property-summary-mv.ts b/packages/core/src/code-migrations/14-profile-event-property-summary-mv.tsindex 564ac4e14..d03901e1a 100644--- a/packages/core/src/code-migrations/14-profile-event-property-summary-mv.ts+++ b/packages/core/src/code-migrations/14-profile-event-property-summary-mv.ts@@ -7,7 +7,7 @@ import {getExistingTables,runClickhouseMigrationCommands,} from '@openpanel/db/src/clickhouse/migration';-import { getIsCluster } from './helpers';+import { type CodeMigrationEnv, getIsCluster } from './helpers';// Per-property breakdown of profile events, used when cohort event criteria// filter on properties.<key> AND as a future fast path for property breakdowns@@ -15,10 +15,10 @@ import { getIsCluster } from './helpers';//// NOTE: fork references this MV but never shipped a migration for it; we own// the authoritative DDL.-export async function up() {+export async function up(env: CodeMigrationEnv) {const replicatedVersion = '1';const existingTables = await getExistingTables();- const isClustered = getIsCluster();+ const isClustered = getIsCluster(env);const sqls: string[] = [];@@ -85,8 +85,8 @@ export async function up() {}}-export async function down() {- const isClustered = getIsCluster();+export async function down(env: CodeMigrationEnv) {+ const isClustered = getIsCluster(env);const sqls = [dropTable(diff --git a/packages/core/src/code-migrations/15-backfill-cohort-mvs.ts b/packages/core/src/code-migrations/15-backfill-cohort-mvs.tsindex bc12f2634..d0d60f39e 100644--- a/packages/core/src/code-migrations/15-backfill-cohort-mvs.ts+++ b/packages/core/src/code-migrations/15-backfill-cohort-mvs.ts@@ -3,7 +3,7 @@ import {chMigrationClient,runClickhouseMigrationCommands,} from '@openpanel/db/src/clickhouse/migration';-import { getIsCluster } from './helpers';+import { type CodeMigrationEnv, getIsCluster } from './helpers';/*** Backfill the two cohort MVs for the last 30 days.@@ -174,8 +174,8 @@ async function executeBatches(label: string, batches: Batch[]): Promise<void> {}}-export async function up() {- const isClustered = getIsCluster();+export async function up(env: CodeMigrationEnv) {+ const isClustered = getIsCluster(env);const isDryRun = process.argv.includes('--dry');const days = Number.parseInt(getArg('days') ?? String(DEFAULT_DAYS), 10);@@ -255,7 +255,7 @@ export async function up() {console.log('✅ Backfill complete.');}-export async function down() {+export async function down(_env: CodeMigrationEnv) {console.log('⚠️ No down migration — backfill writes MV state, not schema.');console.log(' If a clean slate is needed, DROP + CREATE the two MVs and re-run.'diff --git a/packages/core/src/code-migrations/16-restructure-profiles.ts b/packages/core/src/code-migrations/16-restructure-profiles.tsindex d3a6244ce..1043db439 100644--- a/packages/core/src/code-migrations/16-restructure-profiles.ts+++ b/packages/core/src/code-migrations/16-restructure-profiles.ts@@ -6,7 +6,7 @@ import {renameTable,runClickhouseMigrationCommands,} from '@openpanel/db/src/clickhouse/migration';-import { getIsCluster } from './helpers';+import { type CodeMigrationEnv, getIsCluster } from './helpers';// Restructures the `profiles` table so the column names actually mean what// they say:@@ -63,8 +63,8 @@ const OLD_TABLE = `profiles_${SUFFIX}`;const replicated = (table: string) => `${table}_replicated`;-export async function up() {- const isClustered = getIsCluster();+export async function up(env: CodeMigrationEnv) {+ const isClustered = getIsCluster(env);const sqls: string[] = [];// 1. New profiles table. ReplacingMergeTree's version is now `last_seen_at`,diff --git a/packages/core/src/code-migrations/18-events-profile-id-index.ts b/packages/core/src/code-migrations/18-events-profile-id-index.tsindex d87bb2395..7ba1473d9 100644--- a/packages/core/src/code-migrations/18-events-profile-id-index.ts+++ b/packages/core/src/code-migrations/18-events-profile-id-index.ts@@ -2,7 +2,7 @@ import {chMigrationClient,runClickhouseMigrationCommands,} from '@openpanel/db/src/clickhouse/migration';-import { getIsCluster } from './helpers';+import { type CodeMigrationEnv, getIsCluster } from './helpers';/*** Data-skipping index on events.profile_id.@@ -109,8 +109,8 @@ async function hasCompletedMaterialization(}}-export async function up() {- const isClustered = getIsCluster();+export async function up(env: CodeMigrationEnv) {+ const isClustered = getIsCluster(env);const table = isClustered ? 'events_replicated' : 'events';const onCluster = isClustered ? " ON CLUSTER '{cluster}'" : '';@@ -133,8 +133,8 @@ export async function up() {await runClickhouseMigrationCommands(sqls);}-export async function down() {- const isClustered = getIsCluster();+export async function down(env: CodeMigrationEnv) {+ const isClustered = getIsCluster(env);const table = isClustered ? 'events_replicated' : 'events';const onCluster = isClustered ? " ON CLUSTER '{cluster}'" : '';diff --git a/packages/core/src/code-migrations/19-event-property-values-projections.ts b/packages/core/src/code-migrations/19-event-property-values-projections.tsindex 7b2665033..0b70d0247 100644--- a/packages/core/src/code-migrations/19-event-property-values-projections.ts+++ b/packages/core/src/code-migrations/19-event-property-values-projections.ts@@ -3,7 +3,7 @@ import {chMigrationClient,runClickhouseMigrationCommands,} from '@openpanel/db/src/clickhouse/migration';-import { getIsCluster } from './helpers';+import { type CodeMigrationEnv, getIsCluster } from './helpers';/*** Aggregating projection for the property-key autocomplete dropdown.@@ -188,8 +188,8 @@ async function materializedProjections(}}-export async function up() {- const isClustered = getIsCluster();+export async function up(env: CodeMigrationEnv) {+ const isClustered = getIsCluster(env);const storage = await resolveStorageTable(isClustered);const tbl = `\`${storage}\``;const onCluster = isClustered ? " ON CLUSTER '{cluster}'" : '';@@ -214,8 +214,8 @@ export async function up() {]);}-export async function down() {- const isClustered = getIsCluster();+export async function down(env: CodeMigrationEnv) {+ const isClustered = getIsCluster(env);const storage = await resolveStorageTable(isClustered);const tbl = `\`${storage}\``;const onCluster = isClustered ? " ON CLUSTER '{cluster}'" : '';diff --git a/packages/core/src/code-migrations/20-cohort-summary-mv-sort-key.ts b/packages/core/src/code-migrations/20-cohort-summary-mv-sort-key.tsindex 71e45964e..5240f0217 100644--- a/packages/core/src/code-migrations/20-cohort-summary-mv-sort-key.ts+++ b/packages/core/src/code-migrations/20-cohort-summary-mv-sort-key.ts@@ -6,7 +6,7 @@ import {getExistingTables,runClickhouseMigrationCommands,} from '@openpanel/db/src/clickhouse/migration';-import { getIsCluster } from './helpers';+import { type CodeMigrationEnv, getIsCluster } from './helpers';/*** Re-key the cohort summary MVs for the queries that actually read them.@@ -41,10 +41,10 @@ import { getIsCluster } from './helpers';* The old MVs are left in place and keep receiving inserts. Once the new* ones are verified, dropping them is a one-line follow-up migration.*/-export async function up() {+export async function up(env: CodeMigrationEnv) {const replicatedVersion = '1';const existingTables = await getExistingTables();- const isClustered = getIsCluster();+ const isClustered = getIsCluster(env);const sqls: string[] = [];if (diff --git a/packages/core/src/code-migrations/21-backfill-cohort-summary-mvs.ts b/packages/core/src/code-migrations/21-backfill-cohort-summary-mvs.tsindex 2faeaa21c..598c21c52 100644--- a/packages/core/src/code-migrations/21-backfill-cohort-summary-mvs.ts+++ b/packages/core/src/code-migrations/21-backfill-cohort-summary-mvs.ts@@ -3,7 +3,7 @@ import {chMigrationClient,runClickhouseMigrationCommands,} from '@openpanel/db/src/clickhouse/migration';-import { getIsCluster } from './helpers';+import { type CodeMigrationEnv, getIsCluster } from './helpers';/*** Fill the re-keyed cohort summary MVs from migration 20 with history.@@ -237,8 +237,8 @@ async function scalar(query: string): Promise<string | undefined> {return rows[0] ? Object.values(rows[0])[0] : undefined;}-export async function up() {- const isClustered = getIsCluster();+export async function up(env: CodeMigrationEnv) {+ const isClustered = getIsCluster(env);const isDry = process.argv.includes('--dry');const only = getArg('only');const batchDays = getPositiveInt(getArg('batch-days'), DEFAULT_BATCH_DAYS);@@ -369,9 +369,15 @@ export async function up() {console.log('\n✅ Rebuild complete.');}-// Allow direct execution for the supervised path.+// Allow direct execution for the supervised path. `--cluster` is the only+// input `up` reads through `getIsCluster`, and it comes from argv.if (import.meta.url === `file://${process.argv[1]}`) {- up()+ up({+ clickhouseCluster: false,+ selfHosted: false,+ databaseUrl: undefined,+ clickhouseUrl: undefined,+ }).then(() => process.exit(0)).catch((err) => {console.error(err);diff --git a/packages/core/src/code-migrations/22-add-events-inserted-at.ts b/packages/core/src/code-migrations/22-add-events-inserted-at.tsindex d46d1ef01..0cd53b7a6 100644--- a/packages/core/src/code-migrations/22-add-events-inserted-at.ts+++ b/packages/core/src/code-migrations/22-add-events-inserted-at.ts@@ -4,10 +4,10 @@ import {addColumns,runClickhouseMigrationCommands,} from '@openpanel/db/src/clickhouse/migration';-import { getIsCluster } from './helpers';+import { type CodeMigrationEnv, getIsCluster } from './helpers';-export async function up() {- const isClustered = getIsCluster();+export async function up(env: CodeMigrationEnv) {+ const isClustered = getIsCluster(env);// `inserted_at` is the ingestion (CH-insert) time, used as the cursor for the// object-store export job. New rows set it explicitly at insert time (seediff --git a/packages/core/src/code-migrations/3-init-ch.ts b/packages/core/src/code-migrations/3-init-ch.tsindex 1a40f4cd1..6ce128922 100644--- a/packages/core/src/code-migrations/3-init-ch.ts+++ b/packages/core/src/code-migrations/3-init-ch.ts@@ -16,9 +16,14 @@ import {renameTable,runClickhouseMigrationCommands,} from '@openpanel/db/src/clickhouse/migration';-import { getIsCluster, getIsSelfHosting, printBoxMessage } from './helpers';+import {+ type CodeMigrationEnv,+ getIsCluster,+ getIsSelfHosting,+ printBoxMessage,+} from './helpers';-export async function up() {+export async function up(env: CodeMigrationEnv) {const replicatedVersion = '1';const existingTables = await getExistingTables();const hasSelfHosting = existingTables.includes('self_hosting_distributed');@@ -30,8 +35,8 @@ export async function up() {'profile_aliases_distributed');- const isSelfHosting = getIsSelfHosting();- const isClustered = getIsCluster();+ const isSelfHosting = getIsSelfHosting(env);+ const isClustered = getIsCluster(env);const isSelfHostingPostCluster =existingTables.includes('events_replicated') && isSelfHosting;diff --git a/packages/core/src/code-migrations/4-add-sessions.ts b/packages/core/src/code-migrations/4-add-sessions.tsindex b56eef9d3..1f79e66c8 100644--- a/packages/core/src/code-migrations/4-add-sessions.ts+++ b/packages/core/src/code-migrations/4-add-sessions.ts@@ -9,10 +9,10 @@ import {createTable,runClickhouseMigrationCommands,} from '@openpanel/db/src/clickhouse/migration';-import { getIsCluster } from './helpers';+import { type CodeMigrationEnv, getIsCluster } from './helpers';-export async function up() {- const isClustered = getIsCluster();+export async function up(env: CodeMigrationEnv) {+ const isClustered = getIsCluster(env);const sqls: string[] = [...createTable({diff --git a/packages/core/src/code-migrations/5-add-imports-table.ts b/packages/core/src/code-migrations/5-add-imports-table.tsindex 0829efba0..d131c715f 100644--- a/packages/core/src/code-migrations/5-add-imports-table.ts+++ b/packages/core/src/code-migrations/5-add-imports-table.ts@@ -5,10 +5,10 @@ import {modifyTTL,runClickhouseMigrationCommands,} from '@openpanel/db/src/clickhouse/migration';-import { getIsCluster } from './helpers';+import { type CodeMigrationEnv, getIsCluster } from './helpers';-export async function up() {- const isClustered = getIsCluster();+export async function up(env: CodeMigrationEnv) {+ const isClustered = getIsCluster(env);const sqls: string[] = [...createTable({diff --git a/packages/core/src/code-migrations/6-add-revenue-column.ts b/packages/core/src/code-migrations/6-add-revenue-column.tsindex 647bb766a..dbfff35a2 100644--- a/packages/core/src/code-migrations/6-add-revenue-column.ts+++ b/packages/core/src/code-migrations/6-add-revenue-column.ts@@ -4,10 +4,10 @@ import {addColumns,runClickhouseMigrationCommands,} from '@openpanel/db/src/clickhouse/migration';-import { getIsCluster } from './helpers';+import { type CodeMigrationEnv, getIsCluster } from './helpers';-export async function up() {- const isClustered = getIsCluster();+export async function up(env: CodeMigrationEnv) {+ const isClustered = getIsCluster(env);const sqls: string[] = [...addColumns(diff --git a/packages/core/src/code-migrations/8-order-keys.ts b/packages/core/src/code-migrations/8-order-keys.tsindex 1638f2046..1f1c1feb8 100644--- a/packages/core/src/code-migrations/8-order-keys.ts+++ b/packages/core/src/code-migrations/8-order-keys.ts@@ -7,10 +7,10 @@ import {renameTable,runClickhouseMigrationCommands,} from '@openpanel/db/src/clickhouse/migration';-import { getIsCluster } from './helpers';+import { type CodeMigrationEnv, getIsCluster } from './helpers';-export async function up() {- const isClustered = getIsCluster();+export async function up(env: CodeMigrationEnv) {+ const isClustered = getIsCluster(env);const sqls: string[] = [];diff --git a/packages/core/src/code-migrations/helpers.ts b/packages/core/src/code-migrations/helpers.tsindex 38e060d7e..c0b41d304 100644--- a/packages/core/src/code-migrations/helpers.ts+++ b/packages/core/src/code-migrations/helpers.ts@@ -14,17 +14,24 @@ export function printBoxMessage(title: string, lines: (string | unknown)[]) {console.log('└──┘');}-export function getIsCluster() {- const args = process.argv;- return (- args.includes('--cluster') ||- process.env.CLICKHOUSE_CLUSTER === 'true' ||- process.env.CLICKHOUSE_CLUSTER === '1'- );+/**+ * The environment this script needs, read by its shell+ * (`packages/core/scripts/migrate-code.ts`) rather than here: nothing under+ * `packages/core/src` reads `process.env` (ADR-022 R7).+ */+export interface CodeMigrationEnv {+ clickhouseCluster: boolean;+ selfHosted: boolean;+ databaseUrl: string | undefined;+ clickhouseUrl: string | undefined;}-export function getIsSelfHosting() {- return process.env.SELF_HOSTED === 'true' || !!process.env.SELF_HOSTED;+export function getIsCluster(env: CodeMigrationEnv) {+ return process.argv.includes('--cluster') || env.clickhouseCluster;+}++export function getIsSelfHosting(env: CodeMigrationEnv) {+ return env.selfHosted;}export function getIsDry() {diff --git a/packages/core/src/code-migrations/migrate.ts b/packages/core/src/code-migrations/migrate.tsindex 42221c673..df67b1019 100644--- a/packages/core/src/code-migrations/migrate.ts+++ b/packages/core/src/code-migrations/migrate.ts@@ -7,6 +7,7 @@ const __dirname = dirname(__filename);import { db } from '@openpanel/db';import {+ type CodeMigrationEnv,getIsCluster,getIsDry,getIsSelfHosting,@@ -14,7 +15,7 @@ import {printBoxMessage,} from './helpers';-async function migrate() {+export async function runCodeMigrations(env: CodeMigrationEnv) {const args = process.argv.slice(2);const migration = args.filter((arg) => !arg.startsWith('--'))[0];@@ -54,16 +55,16 @@ async function migrate() {]);printBoxMessage('🤝 Config', [- `isClustered: ${getIsCluster()}`,- `isSelfHosting: ${getIsSelfHosting()}`,+ `isClustered: ${getIsCluster(env)}`,+ `isSelfHosting: ${getIsSelfHosting(env)}`,]);printBoxMessage('🌍 Environment', [- `POSTGRES: ${process.env.DATABASE_URL}`,- `CLICKHOUSE: ${process.env.CLICKHOUSE_URL}`,+ `POSTGRES: ${env.databaseUrl}`,+ `CLICKHOUSE: ${env.clickhouseUrl}`,]);- if (!getIsSelfHosting()) {+ if (!getIsSelfHosting(env)) {if (getIsDry()) {printBoxMessage('🕒 Migrations starts now (dry run)', []);} else {@@ -73,7 +74,7 @@ async function migrate() {}if (migration) {- await runMigration(migrationsDir, migration);+ await runMigration(migrationsDir, migration, env);} else {for (const file of migrations) {if (finishedMigrations.some((migration) => migration.name === file)) {@@ -81,7 +82,7 @@ async function migrate() {continue;}- await runMigration(migrationsDir, file);+ await runMigration(migrationsDir, file, env);}}@@ -89,11 +90,15 @@ async function migrate() {process.exit(0);}-async function runMigration(migrationsDir: string, file: string) {+async function runMigration(+ migrationsDir: string,+ file: string,+ env: CodeMigrationEnv+) {printBoxMessage('⚡️ Running Migration ⚡️ ', [`${file}`]);try {const migration = await import(path.join(migrationsDir, file));- await migration.up();+ await migration.up(env);if (!(getIsDry() || getShouldIgnoreRecord())) {await db.codeMigration.upsert({where: {@@ -115,5 +120,3 @@ async function runMigration(migrationsDir: string, file: string) {process.exit(1);}}--migrate();diff --git a/packages/core/src/config.ts b/packages/core/src/config.tsnew file mode 100644index 000000000..0843b4f80--- /dev/null+++ b/packages/core/src/config.ts@@ -0,0 +1,216 @@+// The shape of everything core needs from the environment (ADR-022 R7: only+// the config loader reads `process.env`). `apps/api/src/config/env.ts` is that+// loader; it parses, validates and derives, and hands the result down as+// `AppDeps.config` / `Ctx.config`, so nothing below this file re-derives a+// value or reaches for `process.env` again.+//+// Two conventions hold throughout:+// - `undefined` means "not set", never "blank". A blank `KEY=` is absent.+// - a field that only ever had one spelling of a default keeps that default+// next to the code that documents WHY (e.g. the buffers' batch sizes), so+// the loader parses and the module decides. Anything DERIVED from more+// than one variable is resolved here and arrives finished.++/** Which pino transport ships the logs, and how the service name is built. */+export interface LoggingConfig {+ level: string;+ silent: boolean;+ /** LOG_EXPORTER, or `otlp` when only HYPERDX_API_KEY is set. */+ exporter: 'otlp' | 'stdout';+ hyperdxApiKey: string | undefined;+ /** The two halves of `getServiceName`: `<prefix>-<name>-<environment>`. */+ serviceNamePrefix: string | undefined;+ serviceNameEnvironment: string;+ /** Whether stdout/stderr are routed through pino at all (production, or an+ * exporter explicitly asked for). Local dev output stays untouched. */+ interceptProcessOutput: boolean;+}++/** One OAuth client's three credentials. */+export interface OAuthClientConfig {+ clientId: string;+ clientSecret: string;+ redirectUri: string;+}++export interface AuthConfig {+ /**+ * `undefined` means the variable is unset, which is what cloud runs and what+ * makes registration unconditionally open — distinct from an explicit+ * `ALLOW_REGISTRATION=false`.+ */+ allowRegistration: boolean | undefined;+ allowInvitation: boolean | undefined;+ github: OAuthClientConfig;+ google: OAuthClientConfig;+ /** The same Google client, redirected at the Search Console callback. */+ googleGsc: OAuthClientConfig;+}++export interface CookieConfig {+ /** COOKIE_TLDS: extra multi-part public suffixes, lowercased. */+ extraMultiPartTlds: string[];+ /** CUSTOM_COOKIE_DOMAIN: overrides the domain derived from the dashboard. */+ customDomain: string | undefined;+}++export interface OpenAiConfig {+ apiKey: string | undefined;+ baseUrl: string | undefined;+ project: string | undefined;+ organization: string | undefined;+}++export interface AnthropicConfig {+ apiKey: string | undefined;+ baseUrl: string | undefined;+ authToken: string | undefined;+ version: string | undefined;+}++export interface AiConfig {+ openai: OpenAiConfig;+ anthropic: AnthropicConfig;+}++export interface SlackConfig {+ clientId: string | undefined;+ clientSecret: string | undefined;+ oauthRedirectUrl: string | undefined;+ stateSecret: string | undefined;+}++export interface PolarConfig {+ saveDiscountId: string | undefined;+ webhookSecret: string | undefined;+}++/**+ * The Kafka transport's knobs. Every one is resolved: kafkajs is handed these+ * values verbatim, so a `??` at the call site would be a second default.+ */+export interface KafkaConfig {+ clientId: string;+ brokers: string[];+ eventsTopic: string;+ eventsDlqTopic: string;+ consumerGroup: string;+ partitionsConcurrent: number;+ minMessages: number;+ maxWaitMs: number;+ maxMessagesPerPartition: number;+ sessionTimeoutMs: number;+ heartbeatIntervalMs: number;+ requestTimeoutMs: number;+ connectionTimeoutMs: number;+ producerRetries: number;+ producerInitialRetryMs: number;+ producerMaxRetryMs: number;+ handlerMaxAttempts: number;+ handlerRetryInitialMs: number;+ handlerRetryMaxMs: number;+}++/** Per-buffer sizing. `undefined` keeps the buffer's own documented default. */+export interface BufferSizing {+ batchSize: number | undefined;+ chunkSize?: number | undefined;+ ttlSeconds?: number | undefined;+ fetchChunkSize?: number | undefined;+}++export interface BuffersConfig {+ /** BUFFER_ASYNC_INSERTS: switches every buffer's inserts to async mode. */+ asyncInserts: boolean;+ chInsertConcurrency: number | undefined;+ bot: BufferSizing;+ event: BufferSizing & {+ microBatchMs: number | undefined;+ microBatchSize: number | undefined;+ };+ group: BufferSizing;+ profile: BufferSizing;+ profileBackfill: BufferSizing;+ replay: BufferSizing;+ session: BufferSizing & { squash: boolean };+}++export interface SessionConfig {+ /** SESSION_TIMEOUT_MS: idle window before a session ends. */+ timeoutMs: number | undefined;+ reaperEnabled: boolean;+ reaperBatchSize: number | undefined;+ reaperWallclockDeadmanMs: number | undefined;+ vacuumEnabled: boolean;+ vacuumBatchSize: number | undefined;+ vacuumStaleThresholdMs: number | undefined;+ /** EXPERIMENTAL_PROFILE_BACKFILL, narrowed to a project list when given. */+ profileBackfillEnabled: boolean;+ profileBackfillProjectIds: string[];+}++/**+ * Sizing knobs on read and write paths. Every one is a positive integer or+ * `undefined`: a malformed value is not a boot failure, it keeps the default,+ * which is what the modules did when they parsed these themselves.+ */+export interface QueryConfig {+ eventPropertyValueAutocompleteLimit: number | undefined;+ cohortMaterializeLimit: number | undefined;+ cohortQueryMemoryLimitBytes: number | undefined;+ cohortQuerySpillBytes: number | undefined;+ eventListMaxLookbackDays: number | undefined;+ sessionListMaxLookbackDays: number | undefined;+ importBatchSize: number | undefined;+ insightsRetentionDays: number | undefined;+ windDownMaxPerRun: number | undefined;+ /** FUNNEL_NON_STRICT_ORDERING: drops `strict_increase` from windowFunnel. */+ funnelNonStrictOrdering: boolean;+}++export interface ObjectStoreExportConfig {+ lagSeconds: number | undefined;+ batchSize: number | undefined;+ maxBatchesPerRun: number | undefined;+ concurrency: number | undefined;+ /** GCS_API_ENDPOINT: a non-default endpoint (an emulator, or a test double). */+ gcsApiEndpoint: string | undefined;+}++/** Comma-separated header orders; `undefined` keeps the shipped order. */+export interface IpHeaderConfig {+ attributionOrder: string[] | undefined;+ trustedOrder: string[] | undefined;+}++/** Everything core reads from the environment, parsed and derived once. */+export interface CoreConfig {+ isProduction: boolean;+ isDevelopment: boolean;+ selfHosted: boolean;+ /** DASHBOARD_URL, else the dashboard's public URL. `''` when neither is set. */+ dashboardUrl: string;+ /** Set on the demo deployment only: every session resolves to this user. */+ demoUserId: string | undefined;+ /**+ * CLICKHOUSE_CLUSTER, else the inverse of SELF_HOSTED — the one place that+ * cross-field rule is applied.+ */+ clickhouseClustered: boolean;+ /** The single symmetric key for at-rest encryption; 32 bytes as 64 hex. */+ encryptionKey: string | undefined;+ /** DISABLE_PING: a self-hosted instance opts out of the usage ping. */+ pingDisabled: boolean;+ logging: LoggingConfig;+ auth: AuthConfig;+ cookies: CookieConfig;+ ai: AiConfig;+ slack: SlackConfig;+ polar: PolarConfig;+ kafka: KafkaConfig;+ buffers: BuffersConfig;+ session: SessionConfig;+ query: QueryConfig;+ objectStoreExport: ObjectStoreExportConfig;+ ipHeaders: IpHeaderConfig;+}diff --git a/packages/core/src/context.test.ts b/packages/core/src/context.test.tsindex 2125f9dcb..a999b1078 100644--- a/packages/core/src/context.test.ts+++ b/packages/core/src/context.test.ts@@ -1,4 +1,5 @@import { afterAll, beforeAll, expect, mock, test } from 'bun:test';+import { testCoreConfig } from '../test/config-fixture';import type { AppDeps, Buffers, Ctx } from './context';import type { JobMeta } from './jobs/envelope';import { createRecordingProducers } from './jobs/testing';@@ -82,7 +83,7 @@ function stubDeps(): {},},logger: stubLogger(),- config: { selfHosted: true },+ config: testCoreConfig({ selfHosted: true }),},};}diff --git a/packages/core/src/context.ts b/packages/core/src/context.tsindex e0209440c..6129c3b52 100644--- a/packages/core/src/context.ts+++ b/packages/core/src/context.ts@@ -5,6 +5,7 @@// (ADR-007 decision 18, ADR-018 R1).import type { Buffers } from './buffers/create-buffers';+import type { CoreConfig } from './config';import type { QueueProducerHandle, QueueProducers } from './jobs.registry';import type { Logger } from './logger';import type { SessionValidationResult } from './modules/auth/src/login-session';@@ -74,11 +75,10 @@ export type { Buffers } from './buffers/create-buffers';// `@openpanel/db`'s Prisma client from.export type Session = SessionValidationResult;-/** The env-derived flags core needs. apps/api's config/env.ts is the sole- * reader of process.env; core reads none. */-export interface RuntimeFlags {- selfHosted: boolean;-}+// The parsed environment. `apps/api`'s config/env.ts is the sole reader of+// process.env and core reads none (ADR-022 R7, made true at M15-006), so the+// shape of what core needs is declared in `./config.ts` and arrives here.+export type { CoreConfig } from './config';/** Boot scope. Built once in apps/api's main.ts, closed once in shutdown. */export interface AppDeps {@@ -89,7 +89,7 @@ export interface AppDeps {buffers: Buffers;producers: QueueProducerHandle;logger: Logger;- config: RuntimeFlags;+ config: CoreConfig;}/** Work scope. One per HTTP request, RPC call, job run or Kafka batch. */@@ -101,6 +101,7 @@ export interface Ctx {buffers: Buffers;logger: Logger;queues: QueueProducers;+ config: CoreConfig;services: Services;requestId: string;}@@ -139,6 +140,7 @@ export function createCtx(deps: AppDeps, scope: ScopeMeta): Ctx {buffers: deps.buffers,logger: scope.logger,queues: deps.producers.scope({ requestId: scope.requestId }),+ config: deps.config,requestId: scope.requestId,// Installed as a getter immediately below.services: undefined as unknown as Services,@@ -174,6 +176,7 @@ function installLazyServices(ctx: Ctx): void {buffers: ctx.buffers,logger: ctx.logger,queues: ctx.queues,+ config: ctx.config,});return built;},diff --git a/packages/core/src/http/context.ts b/packages/core/src/http/context.tsindex 53633951c..38c64c311 100644--- a/packages/core/src/http/context.ts+++ b/packages/core/src/http/context.ts@@ -44,7 +44,7 @@ export function requestContext(deps: AppDeps) {return new Elysia({ name: 'core/request-context' }).use(requestIdHook()).use(timestampHook())- .use(clientIpHook())+ .use(clientIpHook(deps.config.ipHeaders)).derive({ as: 'global' },({ request, cookie, requestId, clientIp }): { ctx: HttpCtx } => {diff --git a/packages/core/src/http/hooks.test.ts b/packages/core/src/http/hooks.test.tsindex 84df53d61..b975974f3 100644--- a/packages/core/src/http/hooks.test.ts+++ b/packages/core/src/http/hooks.test.ts@@ -1,6 +1,7 @@import { describe, expect, test } from 'bun:test';import { cors } from '@elysiajs/cors';import { Elysia } from 'elysia';+import { testCoreConfig } from '../../test/config-fixture';import { REQUEST_ID_LENGTH } from '../logger';import { clientIpHook, requestIdHook, timestampHook } from './hooks';@@ -42,7 +43,7 @@ describe('requestIdHook', () => {describe('clientIpHook', () => {const app = new Elysia()- .use(clientIpHook())+ .use(clientIpHook(testCoreConfig().ipHeaders)).get('/', ({ clientIp, clientIpHeader }) => ({ clientIp, clientIpHeader }));test('prefers the client-forwarded header, as V1 does', async () => {@@ -92,7 +93,7 @@ test('root chain runs cors -> requestId -> timestamp -> ip', async () => {ran.push(`timestamp:${typeof timestamp}`);return {};})- .use(clientIpHook())+ .use(clientIpHook(testCoreConfig().ipHeaders)).derive({ as: 'global' }, ({ clientIp }) => {ran.push(`ip:${clientIp}`);return {};diff --git a/packages/core/src/http/hooks.ts b/packages/core/src/http/hooks.tsindex a5d36857a..cfd14167a 100644--- a/packages/core/src/http/hooks.ts+++ b/packages/core/src/http/hooks.ts@@ -13,6 +13,7 @@// consumer.import { Elysia } from 'elysia';+import type { IpHeaderConfig } from '../config';import { REQUEST_ID_HEADER, REQUEST_ID_LENGTH } from '../logger';import { getClientIpFromHeaders } from '../shared/get-client-ip';import { generateId } from '../shared/id';@@ -54,11 +55,11 @@ export function timestampHook() {* `openpanel-client-id` and then `getTrustedIpFromHeaders`, and must never* read this (ADR-002 "behaviour that must be preserved explicitly" 2).*/-export function clientIpHook() {+export function clientIpHook(ipHeaders: IpHeaderConfig) {return new Elysia({ name: 'core/http/client-ip' }).derive({ as: 'global' },({ request }) => {- const { ip, header } = getClientIpFromHeaders(request.headers);+ const { ip, header } = getClientIpFromHeaders(ipHeaders, request.headers);return { clientIp: ip, clientIpHeader: header };});diff --git a/packages/core/src/index.ts b/packages/core/src/index.tsindex 19ae4c5a8..b964bc3fe 100644--- a/packages/core/src/index.ts+++ b/packages/core/src/index.ts@@ -30,7 +30,7 @@ export type { IGroupBufferInput } from './buffers/group-buffer';export type { ProfileBackfillEntry } from './buffers/profile-backfill-buffer';export type { IClickhouseSessionReplayChunk } from './buffers/replay-buffer';export type { SessionIngestResult } from './buffers/session-buffer';-export { SESSION_TIMEOUT_MS } from './buffers/session-buffer';+export { resolveSessionTimeoutMs } from './buffers/session-buffer';// Dissolved from @openpanel/ai (M4-005) — the chat agent app, the filter// command bar, insight explanation/enrichment and the worker's digest and// win-back emails all call these directly.@@ -136,10 +136,10 @@ export {} from './clients/logger';export type {AppDeps,+ CoreConfig,Ctx,HttpCtx,JobCtx,- RuntimeFlags,ScopeMeta,Session,} from './context';@@ -246,8 +246,8 @@ export {assertOAuthState,buildOtpauthUrl,COOKIE_MAX_AGE,- COOKIE_OPTIONS,consumeRecoveryCode,+ cookieOptions,createAuthService,decodeSessionToken,deleteSessionTokenCookie,@@ -257,9 +257,9 @@ export {generateRecoveryCodes,generateSessionToken,generateTotpSecret,- github,- google,- googleGsc,+ githubClient,+ googleClient,+ googleGscClient,hashPassword as hashUserPassword,hashRecoveryCodes,hashSessionToken,@@ -596,24 +596,6 @@ export {createKafkaAdmin,createKafkaEventsConsumer,disconnectKafka,- KAFKA_BROKERS,- KAFKA_CONNECTION_TIMEOUT_MS,- KAFKA_CONSUMER_GROUP,- KAFKA_EVENTS_DLQ_TOPIC,- KAFKA_EVENTS_TOPIC,- KAFKA_HANDLER_MAX_ATTEMPTS,- KAFKA_HANDLER_RETRY_INITIAL_MS,- KAFKA_HANDLER_RETRY_MAX_MS,- KAFKA_HEARTBEAT_INTERVAL_MS,- KAFKA_MAX_MESSAGES_PER_PARTITION,- KAFKA_MAX_WAIT_MS,- KAFKA_MIN_MESSAGES,- KAFKA_PARTITIONS_CONCURRENT,- KAFKA_PRODUCER_INITIAL_RETRY_MS,- KAFKA_PRODUCER_MAX_RETRY_MS,- KAFKA_PRODUCER_RETRIES,- KAFKA_REQUEST_TIMEOUT_MS,- KAFKA_SESSION_TIMEOUT_MS,kafkaLogger,produceDeadLetterEvent,produceIncomingEvent,@@ -1068,7 +1050,6 @@ export { generateId, generateSecureId, shortId } from './shared/id';// only definition already lived here; the barrel is how a package outside// core reaches it, since the exports map has no ./shared/* entry).export { getSafeJson } from './shared/json';-export { resolveMaxLookbackDays } from './shared/lookback';export { getReferrerWithQuery, parseReferrer } from './shared/parse-referrer';export type {UserAgentInfo,diff --git a/packages/core/src/jobs/workers.test.ts b/packages/core/src/jobs/workers.test.tsindex 438615cf9..00609b08a 100644--- a/packages/core/src/jobs/workers.test.ts+++ b/packages/core/src/jobs/workers.test.ts@@ -1,5 +1,6 @@import { expect, test } from 'bun:test';import { z } from 'zod';+import { testCoreConfig } from '../../test/config-fixture';import type { AppDeps, Buffers, JobCtx } from '../context';import { queues } from '../jobs.registry';import type { Logger } from '../logger';@@ -31,7 +32,7 @@ function stubDeps(): AppDeps {buffers: {} as Buffers,producers: createRecordingProducers(queues),logger: stubLogger(),- config: { selfHosted: true },+ config: testCoreConfig({ selfHosted: true }),};}diff --git a/packages/core/src/modules/assistant/assistant.rpc.test.ts b/packages/core/src/modules/assistant/assistant.rpc.test.tsindex 419b06ea6..9fbefab14 100644--- a/packages/core/src/modules/assistant/assistant.rpc.test.ts+++ b/packages/core/src/modules/assistant/assistant.rpc.test.ts@@ -1,10 +1,11 @@// Only the "is anyone logged in" boundary and the deterministic model-list// shape are exercised here — no LLM call, no database. `chat.models` reads-// only `process.env` + the static whitelist (assistant.constants.ts);+// only `ctx.config.ai` + the static whitelist (assistant.constants.ts);// wiring the live-model whitelist against real provider keys is out of// scope for a unit test — see assistant.rpc.ts's header.-import { afterEach, beforeEach, expect, test } from 'bun:test';+import { expect, test } from 'bun:test';+import { testCoreConfig } from '../../../test/config-fixture';import { stubHttpCtx, TEST_SESSION } from '../../../test/rpc-fixtures';import { makeTrpcContext } from '../../rpc/base';import type { CookieOptions } from '../../shared/cookie';@@ -22,37 +23,28 @@ const COOKIE_OPTIONS: CookieOptions = {// `TrpcContext.session` is never literally `null`, only its `userId` is.const EMPTY_SESSION = { session: null, user: null, userId: null };-async function callerWith(session: unknown) {- const { ctx } = stubHttpCtx({}, session);+/** OPENAI_API_KEY / ANTHROPIC_API_KEY arrive as `ctx.config.ai`. */+function configWithKeys(keys: { openai?: string; anthropic?: string }) {+ const base = testCoreConfig();+ return testCoreConfig({+ ai: {+ openai: { ...base.ai.openai, apiKey: keys.openai },+ anthropic: { ...base.ai.anthropic, apiKey: keys.anthropic },+ },+ });+}++async function callerWith(+ session: unknown,+ keys: { openai?: string; anthropic?: string } = {}+) {+ const { ctx } = stubHttpCtx({ config: configWithKeys(keys) }, session);const trpcCtx = await makeTrpcContext(ctx, new Headers(), {cookieOptions: COOKIE_OPTIONS,});return chatRouter.createCaller(trpcCtx);}-const ORIGINAL_ENV = {- OPENAI_API_KEY: process.env.OPENAI_API_KEY,- ANTHROPIC_API_KEY: process.env.ANTHROPIC_API_KEY,-};--beforeEach(() => {- delete process.env.OPENAI_API_KEY;- delete process.env.ANTHROPIC_API_KEY;-});--afterEach(() => {- if (ORIGINAL_ENV.OPENAI_API_KEY === undefined) {- delete process.env.OPENAI_API_KEY;- } else {- process.env.OPENAI_API_KEY = ORIGINAL_ENV.OPENAI_API_KEY;- }- if (ORIGINAL_ENV.ANTHROPIC_API_KEY === undefined) {- delete process.env.ANTHROPIC_API_KEY;- } else {- process.env.ANTHROPIC_API_KEY = ORIGINAL_ENV.ANTHROPIC_API_KEY;- }-});-test('models rejects an unauthenticated caller before reading provider keys', async () => {const caller = await callerWith(EMPTY_SESSION);await expect(caller.models()).rejects.toMatchObject({@@ -70,8 +62,7 @@ test('models returns no models and a null default with no provider keys set', as});test('models filters to the configured provider and prefers the recorded default', async () => {- process.env.OPENAI_API_KEY = 'sk-test';- const caller = await callerWith(TEST_SESSION);+ const caller = await callerWith(TEST_SESSION, { openai: 'sk-test' });const result = await caller.models();expect(result.providers).toEqual({ openai: true, anthropic: false });expect(result.models.every((m) => m.group === 'OpenAI')).toBe(true);diff --git a/packages/core/src/modules/assistant/assistant.rpc.ts b/packages/core/src/modules/assistant/assistant.rpc.tsindex 5e82b43ee..c4b429cde 100644--- a/packages/core/src/modules/assistant/assistant.rpc.ts+++ b/packages/core/src/modules/assistant/assistant.rpc.ts@@ -26,8 +26,8 @@ export const chatRouter = createTRPCRouter({requireLogin(ctx.session.userId);const providers = {- openai: Boolean(process.env.OPENAI_API_KEY),- anthropic: Boolean(process.env.ANTHROPIC_API_KEY),+ openai: Boolean(ctx.config.ai.openai.apiKey),+ anthropic: Boolean(ctx.config.ai.anthropic.apiKey),};const models = getAvailableChatModels(providers);const preferred = models.find((m) => m.id === PREFERRED_DEFAULT_MODEL_ID);diff --git a/packages/core/src/modules/assistant/src/app.ts b/packages/core/src/modules/assistant/src/app.tsindex 0e3ddf9a1..9f305672a 100644--- a/packages/core/src/modules/assistant/src/app.ts+++ b/packages/core/src/modules/assistant/src/app.ts@@ -42,7 +42,7 @@ function createChatAgent(deps: ServiceDeps, entry: ChatModelEntry) {return defineAgent({name: entry.id,description: `OpenPanel chat assistant (${entry.label})`,- model: resolveModel(entry),+ model: resolveModel(deps.config, entry),contextSchema: chatContextSchema,instruction: (context: ChatAgentContext) => buildSystemPrompt(context),tools: (context: ChatAgentContext) => composeChatTools(deps, context),@@ -70,12 +70,12 @@ function createChatAgent(deps: ServiceDeps, entry: ChatModelEntry) {* Called fire-and-forget by the route after the first turn of a new* conversation completes.*/-function createTitlerAgent() {+function createTitlerAgent(deps: ServiceDeps) {return defineAgent({name: '__titler',description: 'Generates concise 3-5 word titles for chat conversations.',// biome-ignore lint/suspicious/noExplicitAny: OpenAI model id union is open- model: openaiProvider().model('gpt-4.1-mini' as any),+ model: openaiProvider(deps.config).model('gpt-4.1-mini' as any),instruction:'You generate concise 3-5 word titles for chat conversations. Respond with ONLY the title. No quotes, no punctuation, no trailing period.',maxSteps: 1,@@ -97,7 +97,7 @@ export function createChatApp(deps: ServiceDeps) {return betterAgent({agents: [...ALLOWED_MODELS.map((entry) => createChatAgent(deps, entry)),- createTitlerAgent(),+ createTitlerAgent(deps),],persistence: {conversations: createConversationStore(deps),diff --git a/packages/core/src/modules/assistant/src/filter-command.ts b/packages/core/src/modules/assistant/src/filter-command.tsindex d3ba0c7da..44c6ba8c4 100644--- a/packages/core/src/modules/assistant/src/filter-command.ts+++ b/packages/core/src/modules/assistant/src/filter-command.ts@@ -15,6 +15,7 @@ import {} from '@better-agent/core';import { z } from 'zod';import { resolveModel } from '../../../clients/ai/providers';+import type { CoreConfig } from '../../../config';import type { ServiceDeps } from '../../../services';import {getDatesFromRange,@@ -120,9 +121,9 @@ type FilterCommandContext = z.infer<typeof filterCommandContextSchema>;const PREFERRED_MODEL_IDS = ['claude-haiku-4-5', 'gpt-4-1-mini'] as const;-function pickModel(): ChatModelEntry {- const haveOpenai = Boolean(process.env.OPENAI_API_KEY);- const haveAnthropic = Boolean(process.env.ANTHROPIC_API_KEY);+function pickModel(config: CoreConfig): ChatModelEntry {+ const haveOpenai = Boolean(config.ai.openai.apiKey);+ const haveAnthropic = Boolean(config.ai.anthropic.apiKey);const available = (entry: ChatModelEntry) =>(entry.group === 'OpenAI' && haveOpenai) ||(entry.group === 'Anthropic' && haveAnthropic);@@ -376,7 +377,7 @@ function createFilterCommandApp(deps: ServiceDeps) {const agent = defineAgent({name: 'filter-command',description: 'OpenPanel filter command bar (one-shot, no persistence).',- model: resolveModel(pickModel()),+ model: resolveModel(deps.config, pickModel(deps.config)),contextSchema: filterCommandContextSchema,outputSchema: {schema: filterCommandOutputJsonSchema,diff --git a/packages/core/src/modules/assistant/src/report-runner.ts b/packages/core/src/modules/assistant/src/report-runner.tsindex c3c7bc909..3b97a2554 100644--- a/packages/core/src/modules/assistant/src/report-runner.ts+++ b/packages/core/src/modules/assistant/src/report-runner.ts@@ -12,6 +12,7 @@// @openpanel/db primitives and must be kept in sync by hand until that debt// is paid down.+import type { CoreConfig } from '../../../config';import type { ServiceDeps } from '../../../services';import { getChartStartEndDate } from '../../../shared/date';import { executeAggregateChart, executeChart } from '../../chart/chart.service';@@ -19,16 +20,19 @@ import { getFunnel } from '../../chart/funnel.service';import { getSettingsForProject } from '../../organization/organization.service';import { getReportById } from '../../report/report.service';+const DEFAULT_DASHBOARD_URL = 'https://dashboard.openpanel.dev';+const TRAILING_SLASH = /\/$/;+function reportUrl(+ config: CoreConfig,organizationId: string,projectId: string,reportId: string): string {- const base = (- process.env.DASHBOARD_URL ||- process.env.NEXT_PUBLIC_DASHBOARD_URL ||- 'https://dashboard.openpanel.dev'- ).replace(/\/$/, '');+ const base = (config.dashboardUrl || DEFAULT_DASHBOARD_URL).replace(+ TRAILING_SLASH,+ ''+ );return `${base}/${organizationId}/${projectId}/reports/${reportId}`;}@@ -88,6 +92,7 @@ export async function runReport(startDate,endDate,dashboard_url: reportUrl(+ deps.config,input.organizationId,input.projectId,input.reportIddiff --git a/packages/core/src/modules/assistant/src/tools/base.ts b/packages/core/src/modules/assistant/src/tools/base.tsindex 7f704446d..eaeb8038d 100644--- a/packages/core/src/modules/assistant/src/tools/base.ts+++ b/packages/core/src/modules/assistant/src/tools/base.ts@@ -129,6 +129,7 @@ export const listDashboards = (deps: ServiceDeps) =>return dashboards.map((d) => ({...d,dashboard_url: dashboardUrl(+ deps.config,context.organizationId,context.projectId,`/dashboards/${d.id}`@@ -979,6 +980,7 @@ export const findProfiles = (deps: ServiceDeps) =>profiles.map((p) => ({...p,dashboard_url: dashboardUrl(+ deps.config,context.organizationId,context.projectId,`/profiles/${p.id}`diff --git a/packages/core/src/modules/assistant/src/tools/dashboard.ts b/packages/core/src/modules/assistant/src/tools/dashboard.tsindex 2aba6330b..1a01688ea 100644--- a/packages/core/src/modules/assistant/src/tools/dashboard.ts+++ b/packages/core/src/modules/assistant/src/tools/dashboard.ts@@ -53,6 +53,7 @@ export const summarizeDashboard = (deps: ServiceDeps) =>id: dashboard.id,name: dashboard.name,dashboard_url: dashboardUrl(+ deps.config,context.organizationId,context.projectId,`/dashboards/${dashboard.id}`diff --git a/packages/core/src/modules/assistant/src/tools/groups.ts b/packages/core/src/modules/assistant/src/tools/groups.tsindex 2d42a4b71..02faaa68b 100644--- a/packages/core/src/modules/assistant/src/tools/groups.ts+++ b/packages/core/src/modules/assistant/src/tools/groups.ts@@ -22,7 +22,12 @@ export const getGroupFull = (deps: ServiceDeps) =>const defaultGroupId = context.pageContext?.ids?.groupId ?? '';const id = groupId || defaultGroupId;const orgPath = (path = '') =>- dashboardUrl(context.organizationId, context.projectId, path);+ dashboardUrl(+ deps.config,+ context.organizationId,+ context.projectId,+ path+ );const group = await getGroupCore(deps, {projectId: context.projectId,@@ -65,7 +70,12 @@ export const getGroupMembers = (deps: ServiceDeps) =>const defaultGroupId = context.pageContext?.ids?.groupId ?? '';const id = groupId || defaultGroupId;const orgPath = (path = '') =>- dashboardUrl(context.organizationId, context.projectId, path);+ dashboardUrl(+ deps.config,+ context.organizationId,+ context.projectId,+ path+ );const result = await getGroupMemberProfiles(deps, {projectId: context.projectId,diff --git a/packages/core/src/modules/assistant/src/tools/helpers.ts b/packages/core/src/modules/assistant/src/tools/helpers.tsindex 25a8519c6..8b901794b 100644--- a/packages/core/src/modules/assistant/src/tools/helpers.ts+++ b/packages/core/src/modules/assistant/src/tools/helpers.ts@@ -4,6 +4,7 @@ import {type ToolRunContext,} from '@better-agent/core';import type { z } from 'zod';+import type { CoreConfig } from '../../../../config';import {getDatesFromRange,resolveDateRange as resolveDateRangeCore,@@ -244,17 +245,18 @@ export function previousPeriod(startDate: string, endDate: string) {};}+/** Where a dev box's dashboard runs when DASHBOARD_URL is not set. */+const DEFAULT_DASHBOARD_URL = 'http://localhost:3000';+/*** Build a clickable dashboard URL for a tool result.*/export function dashboardUrl(+ config: CoreConfig,organizationId: string,projectId: string,path = ''): string {- const base =- process.env.DASHBOARD_URL ||- process.env.NEXT_PUBLIC_DASHBOARD_URL ||- 'http://localhost:3000';+ const base = config.dashboardUrl || DEFAULT_DASHBOARD_URL;return `${base}/${organizationId}/${projectId}${path}`;}diff --git a/packages/core/src/modules/assistant/src/tools/profile.ts b/packages/core/src/modules/assistant/src/tools/profile.tsindex 747e3f32e..0539206ff 100644--- a/packages/core/src/modules/assistant/src/tools/profile.ts+++ b/packages/core/src/modules/assistant/src/tools/profile.ts@@ -43,6 +43,7 @@ export const getProfileFull = (deps: ServiceDeps) =>recent_sessions: sessions,recent_events: withEvents.recent_events,dashboard_url: dashboardUrl(+ deps.config,context.organizationId,context.projectId,`/profiles/${id}`@@ -100,6 +101,7 @@ export const getProfileSessions = (deps: ServiceDeps) =>sessions.map((s) => ({...s,dashboard_url: dashboardUrl(+ deps.config,context.organizationId,context.projectId,`/sessions/${s.id}`@@ -192,6 +194,7 @@ export const getProfileJourney = (deps: ServiceDeps) =>created_at: e.created_at,})),dashboard_url: dashboardUrl(+ deps.config,context.organizationId,context.projectId,`/profiles/${id}`diff --git a/packages/core/src/modules/assistant/src/tools/session.ts b/packages/core/src/modules/assistant/src/tools/session.tsindex 966ae5ad3..ecdbb94ce 100644--- a/packages/core/src/modules/assistant/src/tools/session.ts+++ b/packages/core/src/modules/assistant/src/tools/session.ts@@ -39,6 +39,7 @@ export const getSessionFull = (deps: ServiceDeps) =>path: screenViews,events: events.slice(0, 50),dashboard_url: dashboardUrl(+ deps.config,context.organizationId,context.projectId,`/sessions/${id}`@@ -132,6 +133,7 @@ export const getSimilarSessions = (deps: ServiceDeps) =>similar_sessions: filtered.map((s) => ({...s,dashboard_url: dashboardUrl(+ deps.config,context.organizationId,context.projectId,`/sessions/${s.id}`@@ -274,6 +276,7 @@ export const getSessionReplaySummary = (deps: ServiceDeps) =>available: session.hasReplay ?? false,replay_url: session.hasReplay? dashboardUrl(+ deps.config,context.organizationId,context.projectId,`/sessions/${id}`diff --git a/packages/core/src/modules/auth/auth.routes.ts b/packages/core/src/modules/auth/auth.routes.tsindex d6d7f5d80..7ab1a0681 100644--- a/packages/core/src/modules/auth/auth.routes.ts+++ b/packages/core/src/modules/auth/auth.routes.ts@@ -27,14 +27,15 @@ const callbackQuery = z.object({state: z.string(),});-function dashboardUrl(): string {- return (- process.env.DASHBOARD_URL || process.env.NEXT_PUBLIC_DASHBOARD_URL || ''- );-}+/** Only used to make the URL parseable when DASHBOARD_URL is not set. */+const FALLBACK_ORIGIN = 'http://localhost';-function loginErrorRedirect(message: string, correlationId: string) {- const url = new URL('/login', dashboardUrl() || 'http://localhost');+function loginErrorRedirect(+ dashboardUrl: string,+ message: string,+ correlationId: string+) {+ const url = new URL('/login', dashboardUrl || FALLBACK_ORIGIN);url.searchParams.set('error', message);url.searchParams.set('correlationId', correlationId);return redirect(url.toString());@@ -50,7 +51,7 @@ export const authRoutes = defineRoutes((app) =>try {assertOAuthState('github', query.state, storedState);- const oauthUser = await fetchGithubOAuthUser(query.code);+ const oauthUser = await fetchGithubOAuthUser(ctx.config, query.code);await completeOAuthCallback(ctx, {provider: 'github',oauthUser,@@ -59,7 +60,7 @@ export const authRoutes = defineRoutes((app) =>logger: ctx.logger,});ctx.setCookie('github_oauth_state', '', { maxAge: 0 });- return redirect(dashboardUrl());+ return redirect(ctx.config.dashboardUrl);} catch (error) {ctx.logger.error({ err: error }, 'GitHub OAuth callback error');ctx.setCookie('github_oauth_state', '', { maxAge: 0 });@@ -67,7 +68,11 @@ export const authRoutes = defineRoutes((app) =>error instanceof OAuthCallbackError? error.message: 'An error occurred';- return loginErrorRedirect(message, ctx.requestId);+ return loginErrorRedirect(+ ctx.config.dashboardUrl,+ message,+ ctx.requestId+ );}},{ query: callbackQuery, detail: { hide: true } }@@ -87,6 +92,7 @@ export const authRoutes = defineRoutes((app) =>});}const oauthUser = await fetchGoogleOAuthUser(+ ctx.config,query.code,codeVerifier);@@ -99,7 +105,7 @@ export const authRoutes = defineRoutes((app) =>});ctx.setCookie('google_oauth_state', '', { maxAge: 0 });ctx.setCookie('google_code_verifier', '', { maxAge: 0 });- return redirect(dashboardUrl());+ return redirect(ctx.config.dashboardUrl);} catch (error) {ctx.logger.error({ err: error }, 'Google OAuth callback error');ctx.setCookie('google_oauth_state', '', { maxAge: 0 });@@ -108,7 +114,11 @@ export const authRoutes = defineRoutes((app) =>error instanceof OAuthCallbackError? error.message: 'An error occurred';- return loginErrorRedirect(message, ctx.requestId);+ return loginErrorRedirect(+ ctx.config.dashboardUrl,+ message,+ ctx.requestId+ );}},{ query: callbackQuery, detail: { hide: true } }diff --git a/packages/core/src/modules/auth/auth.rpc.ts b/packages/core/src/modules/auth/auth.rpc.tsindex 119fad1e9..04a1a3b1d 100644--- a/packages/core/src/modules/auth/auth.rpc.ts+++ b/packages/core/src/modules/auth/auth.rpc.ts@@ -62,7 +62,7 @@ export const authRouter = createTRPCRouter({signInOAuth: publicProcedure.input(z.object({ provider: zProvider, inviteId: z.string().nullish() }))- .mutation(({ input, ctx }) => startOAuthSignIn(input, ctx.setCookie)),+ .mutation(({ input, ctx }) => startOAuthSignIn(ctx, input, ctx.setCookie)),signUpEmail: publicProcedure.input(zSignUpEmail)diff --git a/packages/core/src/modules/auth/auth.service.ts b/packages/core/src/modules/auth/auth.service.tsindex b13898379..3d69258e1 100644--- a/packages/core/src/modules/auth/auth.service.ts+++ b/packages/core/src/modules/auth/auth.service.ts@@ -16,6 +16,7 @@// `auth.rpc.ts` already carries a `Ctx` and passes it straight through.import { z } from 'zod';+import type { CoreConfig } from '../../config';import type { ServiceDeps, Services } from '../../services';import { type AccessChecks, createAccessChecks } from '../../shared/access';import type {@@ -30,7 +31,7 @@ import {setSessionTokenCookie,} from './src/cookie';import { parseCookieDomain } from './src/cookie-domain';-import { Arctic, github, google } from './src/oauth';+import { Arctic, githubClient, googleClient } from './src/oauth';import { hashPassword, verifyPasswordHash } from './src/password';import {decodeSessionToken,@@ -52,7 +53,7 @@ import {// exist for `createAuthService` below) — `noExportedImports` would otherwise// flag every one of those imports as "only re-exported", which is false;// they are also the members `createAuthService` returns.-export { COOKIE_MAX_AGE, COOKIE_OPTIONS } from './src/constants';+export { COOKIE_MAX_AGE, cookieOptions } from './src/constants';export {deleteSessionTokenCookie,setLastAuthProviderCookie,@@ -60,7 +61,12 @@ export {} from './src/cookie';export { parseCookieDomain } from './src/cookie-domain';export type { OAuth2Tokens } from './src/oauth';-export { Arctic, github, google, googleGsc } from './src/oauth';+export {+ Arctic,+ githubClient,+ googleClient,+ googleGscClient,+} from './src/oauth';export { hashPassword, verifyPasswordHash } from './src/password';export {decodeSessionToken,@@ -143,7 +149,7 @@ export function resetAccessChecksForTests(): void {* needs `db` / `logger` drops the underscore and reads the parameter.*/export function createAuthService(- _deps: ServiceDeps,+ deps: ServiceDeps,_services: () => Services) {// Annotated as a whole rather than member by member: one annotation binds@@ -192,10 +198,19 @@ export function createAuthService(hashRecoveryCodes,normalizeRecoveryCode,consumeRecoveryCode,- setSessionTokenCookie,- setLastAuthProviderCookie,- deleteSessionTokenCookie,- parseCookieDomain,+ setSessionTokenCookie: (+ setCookie: ISetCookie,+ token: string,+ expiresAt: Date+ ): void => setSessionTokenCookie(deps.config, setCookie, token, expiresAt),+ setLastAuthProviderCookie: (+ setCookie: ISetCookie,+ provider: string+ ): void => setLastAuthProviderCookie(deps.config, setCookie, provider),+ deleteSessionTokenCookie: (setCookie: ISetCookie): void =>+ deleteSessionTokenCookie(deps.config, setCookie),+ parseCookieDomain: (url: string): ReturnType<typeof parseCookieDomain> =>+ parseCookieDomain(deps.config, url),};}@@ -259,12 +274,6 @@ function loadShare() {return import('../share/share.service');}-function dashboardUrl(): string {- return (- process.env.DASHBOARD_URL || process.env.NEXT_PUBLIC_DASHBOARD_URL || ''- );-}-/*** Best-effort consumption of an invite for a user that just authenticated.* Failures (expired/invalid invite) must not block the sign-in itself, so we@@ -294,7 +303,7 @@ export async function signOutUser(setCookie: ISetCookie,sessionId: string | null | undefined): Promise<void> {- deleteSessionTokenCookie(setCookie);+ deleteSessionTokenCookie(deps.config, setCookie);if (sessionId) {await invalidateSession(deps, sessionId);}@@ -317,6 +326,7 @@ export type StartOAuthSignInResult =* only place we know the user is new.*/export function startOAuthSignIn(+ deps: ServiceDeps,input: StartOAuthSignInInput,setCookie: ISetCookie): StartOAuthSignInResult {@@ -326,7 +336,7 @@ export function startOAuthSignIn(if (input.provider === 'github') {const state = Arctic.generateState();- const url = github.createAuthorizationURL(state, [+ const url = githubClient(deps.config).createAuthorizationURL(state, ['user:email','user:read',]);@@ -336,11 +346,11 @@ export function startOAuthSignIn(const state = Arctic.generateState();const codeVerifier = Arctic.generateCodeVerifier();- const url = google.createAuthorizationURL(state, codeVerifier, [- 'openid',- 'profile',- 'email',- ]);+ const url = googleClient(deps.config).createAuthorizationURL(+ state,+ codeVerifier,+ ['openid', 'profile', 'email']+ );setCookie('google_oauth_state', state, { maxAge: 60 * 10 });setCookie('google_code_verifier', codeVerifier, { maxAge: 60 * 10 });return { type: 'google', url: url.toString() };@@ -397,7 +407,7 @@ export async function signUpWithEmail(const token = generateSessionToken();const session = await createSession(deps, token, createdUser.id);- setSessionTokenCookie(setCookie, token, session.expiresAt);+ setSessionTokenCookie(deps.config, setCookie, token, session.expiresAt);return session;}@@ -469,8 +479,8 @@ export async function signInWithEmail(const token = generateSessionToken();const session = await createSession(deps, token, user.id);- setSessionTokenCookie(setCookie, token, session.expiresAt);- setLastAuthProviderCookie(setCookie, 'email');+ setSessionTokenCookie(deps.config, setCookie, token, session.expiresAt);+ setLastAuthProviderCookie(deps.config, setCookie, 'email');if (input.inviteId) {await consumeInviteForUser(deps, user.id, input.inviteId, logger);@@ -516,7 +526,7 @@ export async function signInWithTotp(throw new TRPCAccessError('Two-factor is not enabled');}- const secret = decrypt(totp.secret);+ const secret = decrypt(deps.config.encryptionKey, totp.secret);const isTotpCode = /^\d{6}$/.test(input.code.replace(/\s+/g, ''));let valid = false;@@ -545,8 +555,8 @@ export async function signInWithTotp(const token = generateSessionToken();const session = await createSession(deps, token, challenge.userId);- setSessionTokenCookie(setCookie, token, session.expiresAt);- setLastAuthProviderCookie(setCookie, 'email');+ setSessionTokenCookie(deps.config, setCookie, token, session.expiresAt);+ setLastAuthProviderCookie(deps.config, setCookie, 'email');const inviteId = cookies.get(INVITE_COOKIE);if (inviteId) {@@ -601,8 +611,16 @@ export async function setupTotp(deps: ServiceDeps, userId: string) {await deps.db.userTotp.upsert({where: { userId },- create: { userId, secret: encrypt(secret), recoveryCodes: [] },- update: { secret: encrypt(secret), recoveryCodes: [], enabledAt: null },+ create: {+ userId,+ secret: encrypt(deps.config.encryptionKey, secret),+ recoveryCodes: [],+ },+ update: {+ secret: encrypt(deps.config.encryptionKey, secret),+ recoveryCodes: [],+ enabledAt: null,+ },});return { otpauthUrl, qrDataUrl, secret };@@ -621,7 +639,7 @@ export async function enableTotp(throw new TRPCAccessError('Two-factor is already enabled');}- const secret = decrypt(totp.secret);+ const secret = decrypt(deps.config.encryptionKey, totp.secret);if (!verifyTotpCode(secret, code)) {throw new TRPCAccessError('Invalid code');}@@ -647,7 +665,7 @@ export async function disableTotp(throw new TRPCAccessError('Two-factor is not enabled');}- const secret = decrypt(totp.secret);+ const secret = decrypt(deps.config.encryptionKey, totp.secret);const isTotpCode = /^\d{6}$/.test(code.replace(/\s+/g, ''));const valid = isTotpCode? verifyTotpCode(secret, code)@@ -672,7 +690,7 @@ export async function regenerateTotpRecoveryCodes(if (!totp?.enabledAt) {throw new TRPCAccessError('Two-factor is not enabled');}- const secret = decrypt(totp.secret);+ const secret = decrypt(deps.config.encryptionKey, totp.secret);if (!verifyTotpCode(secret, code)) {throw new TRPCAccessError('Invalid code');}@@ -742,7 +760,9 @@ export async function requestPasswordReset(await sendEmail('reset-password', {to: input.email,- data: { url: `${dashboardUrl()}/reset-password?token=${token}` },+ data: {+ url: `${deps.config.dashboardUrl}/reset-password?token=${token}`,+ },});return true;@@ -762,7 +782,12 @@ export async function extendSessionCookie(const session = await validateSessionToken(deps, token);if (session.session) {- setSessionTokenCookie(setCookie, token, session.session.expiresAt);+ setSessionTokenCookie(+ deps.config,+ setCookie,+ token,+ session.session.expiresAt+ );return { extended: true, expiresAt: session.session.expiresAt };}@@ -870,8 +895,11 @@ async function fetchGithubEmail(accessToken: string): Promise<string | null> {return email;}-export async function fetchGithubOAuthUser(code: string): Promise<OAuthUser> {- const tokens = await github.validateAuthorizationCode(code);+export async function fetchGithubOAuthUser(+ config: CoreConfig,+ code: string+): Promise<OAuthUser> {+ const tokens = await githubClient(config).validateAuthorizationCode(code);const accessToken = tokens.accessToken();const email = await fetchGithubEmail(accessToken);if (!email) {@@ -907,10 +935,14 @@ export async function fetchGithubOAuthUser(code: string): Promise<OAuthUser> {}export async function fetchGoogleOAuthUser(+ config: CoreConfig,code: string,codeVerifier: string): Promise<OAuthUser> {- const tokens = await google.validateAuthorizationCode(code, codeVerifier);+ const tokens = await googleClient(config).validateAuthorizationCode(+ code,+ codeVerifier+ );const claims = Arctic.decodeIdToken(tokens.idToken());const claimsSchema = z.object({@@ -1038,8 +1070,13 @@ async function completeExistingOAuthUser(await consumeInviteForUser(deps, account.userId, inviteId, logger);}- setSessionTokenCookie(setCookie, sessionToken, session.expiresAt);- setLastAuthProviderCookie(setCookie, provider);+ setSessionTokenCookie(+ deps.config,+ setCookie,+ sessionToken,+ session.expiresAt+ );+ setLastAuthProviderCookie(deps.config, setCookie, provider);}async function completeNewOAuthUser(@@ -1104,6 +1141,11 @@ async function completeNewOAuthUser(const sessionToken = generateSessionToken();const session = await createSession(deps, sessionToken, user.id);- setSessionTokenCookie(setCookie, sessionToken, session.expiresAt);- setLastAuthProviderCookie(setCookie, provider);+ setSessionTokenCookie(+ deps.config,+ setCookie,+ sessionToken,+ session.expiresAt+ );+ setLastAuthProviderCookie(deps.config, setCookie, provider);}diff --git a/packages/core/src/modules/auth/src/constants.ts b/packages/core/src/modules/auth/src/constants.tsindex 6fe5f7ea6..c7510b399 100644--- a/packages/core/src/modules/auth/src/constants.ts+++ b/packages/core/src/modules/auth/src/constants.ts@@ -1,19 +1,19 @@-// Read at import time, not through config/env.ts: this is the same pragmatic-// deviation @openpanel/db and @openpanel/redis take (TARGET_ARCHITECTURE §7)-// — ported from @openpanel/auth unchanged rather than threaded through-// AppDeps, which would make every module reachable from a session cookie-// carry a dependency on this one.+// Deployment-derived: the session cookie's domain and `secure` flag come from+// the dashboard's own URL, so they are computed from the config the boot path+// already holds rather than read at import (ADR-022 R7).+import type { CoreConfig } from '../../../config';+import type { CookieOptions } from '../../../shared/cookie';import { parseCookieDomain } from './cookie-domain';-const parsed = parseCookieDomain(- (process.env.DASHBOARD_URL || process.env.NEXT_PUBLIC_DASHBOARD_URL) ?? ''-);-export const COOKIE_MAX_AGE = 60 * 60 * 24 * 30;-export const COOKIE_OPTIONS = {- domain: parsed.domain,- secure: parsed.secure,- sameSite: 'lax',- httpOnly: true,- path: '/',-} as const;++export function cookieOptions(config: CoreConfig): CookieOptions {+ const parsed = parseCookieDomain(config, config.dashboardUrl);+ return {+ domain: parsed.domain,+ secure: parsed.secure,+ sameSite: 'lax',+ httpOnly: true,+ path: '/',+ };+}diff --git a/packages/core/src/modules/auth/src/cookie-domain.test.ts b/packages/core/src/modules/auth/src/cookie-domain.test.tsindex 7ffcbc70e..2e95a2e3d 100644--- a/packages/core/src/modules/auth/src/cookie-domain.test.ts+++ b/packages/core/src/modules/auth/src/cookie-domain.test.ts@@ -1,9 +1,22 @@-import { afterEach, beforeEach, describe, expect, it } from 'bun:test';+import { describe, expect, it } from 'bun:test';+import { testCoreConfig } from '../../../../test/config-fixture';import { parseCookieDomain } from './cookie-domain';+/** The config the loader builds for an environment with no COOKIE_TLDS set. */+const parse = (url: string) => parseCookieDomain(testCoreConfig(), url);++/** COOKIE_TLDS, already split and lowercased by the loader. */+const parseWithTlds = (extraMultiPartTlds: string[], url: string) =>+ parseCookieDomain(+ testCoreConfig({+ cookies: { extraMultiPartTlds, customDomain: undefined },+ }),+ url+ );+describe('parseCookieDomain', () => {it('should return undefined domain for empty string', () => {- expect(parseCookieDomain('')).toEqual({+ expect(parse('')).toEqual({domain: undefined,secure: false,});@@ -11,35 +24,35 @@ describe('parseCookieDomain', () => {describe('localhost and IP addresses', () => {it('should return undefined domain for localhost', () => {- expect(parseCookieDomain('http://localhost:3000')).toEqual({+ expect(parse('http://localhost:3000')).toEqual({domain: undefined,secure: false,});});it('should return undefined domain for localhost with https', () => {- expect(parseCookieDomain('https://localhost:3000')).toEqual({+ expect(parse('https://localhost:3000')).toEqual({domain: undefined,secure: true,});});it('should return undefined domain for IPv4 addresses', () => {- expect(parseCookieDomain('http://192.168.1.1')).toEqual({+ expect(parse('http://192.168.1.1')).toEqual({domain: undefined,secure: false,});});it('should return undefined domain for IPv4 addresses with https', () => {- expect(parseCookieDomain('https://192.168.1.1')).toEqual({+ expect(parse('https://192.168.1.1')).toEqual({domain: undefined,secure: true,});});it('should return undefined domain for IPv4 addresses with port', () => {- expect(parseCookieDomain('http://192.168.1.1:8080')).toEqual({+ expect(parse('http://192.168.1.1:8080')).toEqual({domain: undefined,secure: false,});@@ -48,70 +61,70 @@ describe('parseCookieDomain', () => {describe('multi-part TLDs (co.uk, com.au, etc.)', () => {it('should handle co.uk domains correctly', () => {- expect(parseCookieDomain('https://example.co.uk')).toEqual({+ expect(parse('https://example.co.uk')).toEqual({domain: '.example.co.uk',secure: true,});});it('should handle subdomains of co.uk domains', () => {- expect(parseCookieDomain('https://subdomain.example.co.uk')).toEqual({+ expect(parse('https://subdomain.example.co.uk')).toEqual({domain: '.example.co.uk',secure: true,});});it('should handle deep subdomains of co.uk domains', () => {- expect(parseCookieDomain('https://api.subdomain.example.co.uk')).toEqual({+ expect(parse('https://api.subdomain.example.co.uk')).toEqual({domain: '.example.co.uk',secure: true,});});it('should handle com.au domains correctly', () => {- expect(parseCookieDomain('https://example.com.au')).toEqual({+ expect(parse('https://example.com.au')).toEqual({domain: '.example.com.au',secure: true,});});it('should handle subdomains of com.au domains', () => {- expect(parseCookieDomain('https://api.example.com.au')).toEqual({+ expect(parse('https://api.example.com.au')).toEqual({domain: '.example.com.au',secure: true,});});it('should handle co.za domains correctly', () => {- expect(parseCookieDomain('https://example.co.za')).toEqual({+ expect(parse('https://example.co.za')).toEqual({domain: '.example.co.za',secure: true,});});it('should handle org.uk domains correctly', () => {- expect(parseCookieDomain('https://example.org.uk')).toEqual({+ expect(parse('https://example.org.uk')).toEqual({domain: '.example.org.uk',secure: true,});});it('should handle gov.uk domains correctly', () => {- expect(parseCookieDomain('https://example.gov.uk')).toEqual({+ expect(parse('https://example.gov.uk')).toEqual({domain: '.example.gov.uk',secure: true,});});it('should handle ac.uk domains correctly', () => {- expect(parseCookieDomain('https://example.ac.uk')).toEqual({+ expect(parse('https://example.ac.uk')).toEqual({domain: '.example.ac.uk',secure: true,});});it('should handle nhs.uk domains correctly', () => {- expect(parseCookieDomain('https://example.nhs.uk')).toEqual({+ expect(parse('https://example.nhs.uk')).toEqual({domain: '.example.nhs.uk',secure: true,});@@ -120,35 +133,35 @@ describe('parseCookieDomain', () => {describe('regular domains', () => {it('should handle root domains correctly', () => {- expect(parseCookieDomain('https://example.com')).toEqual({+ expect(parse('https://example.com')).toEqual({domain: '.example.com',secure: true,});});it('should handle root domains with http', () => {- expect(parseCookieDomain('http://example.com')).toEqual({+ expect(parse('http://example.com')).toEqual({domain: '.example.com',secure: false,});});it('should handle subdomains correctly', () => {- expect(parseCookieDomain('https://api.example.com')).toEqual({+ expect(parse('https://api.example.com')).toEqual({domain: '.example.com',secure: true,});});it('should handle deep subdomains correctly', () => {- expect(parseCookieDomain('https://v1.api.example.com')).toEqual({+ expect(parse('https://v1.api.example.com')).toEqual({domain: '.example.com',secure: true,});});it('should handle very deep subdomains correctly', () => {- expect(parseCookieDomain('https://staging.v1.api.example.com')).toEqual({+ expect(parse('https://staging.v1.api.example.com')).toEqual({domain: '.example.com',secure: true,});@@ -157,35 +170,35 @@ describe('parseCookieDomain', () => {describe('PaaS platform subdomains', () => {it('should handle zeabur.app subdomains correctly', () => {- expect(parseCookieDomain('https://xxx.zeabur.app')).toEqual({+ expect(parse('https://xxx.zeabur.app')).toEqual({domain: '.zeabur.app',secure: true,});});it('should handle railway.app subdomains correctly', () => {- expect(parseCookieDomain('https://xxx.railway.app')).toEqual({+ expect(parse('https://xxx.railway.app')).toEqual({domain: '.railway.app',secure: true,});});it('should handle vercel.app subdomains correctly', () => {- expect(parseCookieDomain('https://xxx.vercel.app')).toEqual({+ expect(parse('https://xxx.vercel.app')).toEqual({domain: '.vercel.app',secure: true,});});it('should handle netlify.app subdomains correctly', () => {- expect(parseCookieDomain('https://xxx.netlify.app')).toEqual({+ expect(parse('https://xxx.netlify.app')).toEqual({domain: '.netlify.app',secure: true,});});it('should handle render.com subdomains correctly', () => {- expect(parseCookieDomain('https://xxx.onrender.com')).toEqual({+ expect(parse('https://xxx.onrender.com')).toEqual({domain: '.onrender.com',secure: true,});@@ -194,28 +207,28 @@ describe('parseCookieDomain', () => {describe('edge cases and potential breaking scenarios', () => {it('should handle domains with ports', () => {- expect(parseCookieDomain('https://example.com:8080')).toEqual({+ expect(parse('https://example.com:8080')).toEqual({domain: '.example.com',secure: true,});});it('should handle domains with paths', () => {- expect(parseCookieDomain('https://example.com/path')).toEqual({+ expect(parse('https://example.com/path')).toEqual({domain: '.example.com',secure: true,});});it('should handle domains with query parameters', () => {- expect(parseCookieDomain('https://example.com?param=value')).toEqual({+ expect(parse('https://example.com?param=value')).toEqual({domain: '.example.com',secure: true,});});it('should handle domains with fragments', () => {- expect(parseCookieDomain('https://example.com#fragment')).toEqual({+ expect(parse('https://example.com#fragment')).toEqual({domain: '.example.com',secure: true,});@@ -223,7 +236,7 @@ describe('parseCookieDomain', () => {it('should handle domains with all URL components', () => {expect(- parseCookieDomain('https://example.com:8080/path?param=value#fragment')+ parse('https://example.com:8080/path?param=value#fragment')).toEqual({domain: '.example.com',secure: true,@@ -231,28 +244,28 @@ describe('parseCookieDomain', () => {});it('should handle single-level domains', () => {- expect(parseCookieDomain('https://example')).toEqual({+ expect(parse('https://example')).toEqual({domain: '.example',secure: true,});});it('should handle domains with underscores (invalid but should not crash)', () => {- expect(parseCookieDomain('https://example_test.com')).toEqual({+ expect(parse('https://example_test.com')).toEqual({domain: '.example_test.com',secure: true,});});it('should handle domains with hyphens', () => {- expect(parseCookieDomain('https://example-test.com')).toEqual({+ expect(parse('https://example-test.com')).toEqual({domain: '.example-test.com',secure: true,});});it('should handle domains with numbers', () => {- expect(parseCookieDomain('https://example123.com')).toEqual({+ expect(parse('https://example123.com')).toEqual({domain: '.example123.com',secure: true,});@@ -261,57 +274,53 @@ describe('parseCookieDomain', () => {describe('error cases that should break', () => {it('should throw error for invalid URLs', () => {- expect(() => parseCookieDomain('not-a-url')).toThrow();+ expect(() => parse('not-a-url')).toThrow();});it('should throw error for URLs without protocol', () => {- expect(() => parseCookieDomain('example.com')).toThrow();+ expect(() => parse('example.com')).toThrow();});it('should throw error for malformed URLs', () => {- expect(() => parseCookieDomain('http://')).toThrow();+ expect(() => parse('http://')).toThrow();});it('should throw error for URLs with invalid characters', () => {- expect(() =>- parseCookieDomain('http://example.com:invalid-port')- ).toThrow();+ expect(() => parse('http://example.com:invalid-port')).toThrow();});});describe('specific real-world scenarios', () => {it('should handle openpanel.dev domains correctly', () => {- expect(parseCookieDomain('https://api.openpanel.dev')).toEqual({+ expect(parse('https://api.openpanel.dev')).toEqual({domain: '.openpanel.dev',secure: true,});});it('should handle dashboard.openpanel.dev domains correctly', () => {- expect(parseCookieDomain('https://dashboard.openpanel.dev')).toEqual({+ expect(parse('https://dashboard.openpanel.dev')).toEqual({domain: '.openpanel.dev',secure: true,});});it('should handle subdomains of openpanel.dev correctly', () => {- expect(- parseCookieDomain('https://staging.dashboard.openpanel.dev')- ).toEqual({+ expect(parse('https://staging.dashboard.openpanel.dev')).toEqual({domain: '.openpanel.dev',secure: true,});});it('should handle custom domains correctly', () => {- expect(parseCookieDomain('https://myapp.com')).toEqual({+ expect(parse('https://myapp.com')).toEqual({domain: '.myapp.com',secure: true,});});it('should handle subdomains of custom domains correctly', () => {- expect(parseCookieDomain('https://api.myapp.com')).toEqual({+ expect(parse('https://api.myapp.com')).toEqual({domain: '.myapp.com',secure: true,});@@ -385,14 +394,14 @@ describe('parseCookieDomain', () => {for (const tld of multiPartTLDs) {it(`should handle ${tld} domains correctly`, () => {- expect(parseCookieDomain(`https://example.${tld}`)).toEqual({+ expect(parse(`https://example.${tld}`)).toEqual({domain: `.example.${tld}`,secure: true,});});it(`should handle subdomains of ${tld} domains correctly`, () => {- expect(parseCookieDomain(`https://api.example.${tld}`)).toEqual({+ expect(parse(`https://api.example.${tld}`)).toEqual({domain: `.example.${tld}`,secure: true,});@@ -401,98 +410,80 @@ describe('parseCookieDomain', () => {});describe('custom multi-part TLDs via COOKIE_TLDS', () => {- const originalEnv = process.env.COOKIE_TLDS;-- beforeEach(() => {- // Reset the environment variable before each test- delete process.env.COOKIE_TLDS;- });-- afterEach(() => {- // Restore original value- if (originalEnv !== undefined) {- process.env.COOKIE_TLDS = originalEnv;- } else {- delete process.env.COOKIE_TLDS;- }- });-it('should handle my.id domains when COOKIE_TLDS includes my.id', () => {- process.env.COOKIE_TLDS = 'my.id';- expect(parseCookieDomain('https://abc.my.id')).toEqual({+ expect(parseWithTlds(['my.id'], 'https://abc.my.id')).toEqual({domain: '.abc.my.id',secure: true,});});it('should handle subdomains of my.id domains correctly', () => {- process.env.COOKIE_TLDS = 'my.id';- expect(parseCookieDomain('https://api.abc.my.id')).toEqual({+ expect(parseWithTlds(['my.id'], 'https://api.abc.my.id')).toEqual({domain: '.abc.my.id',secure: true,});});it('should handle multiple custom TLDs', () => {- process.env.COOKIE_TLDS = 'my.id,web.id,co.id';+ const tlds = ['my.id', 'web.id', 'co.id'];- expect(parseCookieDomain('https://abc.my.id')).toEqual({+ expect(parseWithTlds(tlds, 'https://abc.my.id')).toEqual({domain: '.abc.my.id',secure: true,});- expect(parseCookieDomain('https://abc.web.id')).toEqual({+ expect(parseWithTlds(tlds, 'https://abc.web.id')).toEqual({domain: '.abc.web.id',secure: true,});- expect(parseCookieDomain('https://abc.co.id')).toEqual({+ expect(parseWithTlds(tlds, 'https://abc.co.id')).toEqual({domain: '.abc.co.id',secure: true,});});- it('should handle custom TLDs with extra whitespace', () => {- process.env.COOKIE_TLDS = ' my.id , web.id ';- expect(parseCookieDomain('https://abc.my.id')).toEqual({- domain: '.abc.my.id',- secure: true,- });- });-+ // Trimming and lowercasing happen in the config loader (`tokenList` plus+ // the transform), so what reaches here is already normalised.it('should handle case-insensitive custom TLDs', () => {- process.env.COOKIE_TLDS = 'MY.ID';- expect(parseCookieDomain('https://abc.my.id')).toEqual({+ expect(parseWithTlds(['my.id'], 'https://abc.my.id')).toEqual({domain: '.abc.my.id',secure: true,});});- it('should not affect domains when env variable is empty', () => {- process.env.COOKIE_TLDS = '';+ it('should not affect domains when the list is empty', () => {// Without the custom TLD, my.id is treated as a regular TLD- expect(parseCookieDomain('https://abc.my.id')).toEqual({+ expect(parseWithTlds([], 'https://abc.my.id')).toEqual({domain: '.my.id',secure: true,});- });-- it('should not affect domains when env variable is not set', () => {- delete process.env.COOKIE_TLDS;- // Without the custom TLD, my.id is treated as a regular TLD- expect(parseCookieDomain('https://abc.my.id')).toEqual({+ expect(parse('https://abc.my.id')).toEqual({domain: '.my.id',secure: true,});});it('should still work with built-in multi-part TLDs when custom TLDs are set', () => {- process.env.COOKIE_TLDS = 'my.id';// Built-in TLDs should still work- expect(parseCookieDomain('https://example.co.uk')).toEqual({+ expect(parseWithTlds(['my.id'], 'https://example.co.uk')).toEqual({domain: '.example.co.uk',secure: true,});});++ it('uses CUSTOM_COOKIE_DOMAIN verbatim when it is set', () => {+ expect(+ parseCookieDomain(+ testCoreConfig({+ cookies: {+ extraMultiPartTlds: [],+ customDomain: '.openpanel.dev',+ },+ }),+ 'https://abc.my.id'+ )+ ).toEqual({ domain: '.openpanel.dev', secure: true });+ });});});diff --git a/packages/core/src/modules/auth/src/cookie-domain.ts b/packages/core/src/modules/auth/src/cookie-domain.tsindex e405a5765..8ef93801d 100644--- a/packages/core/src/modules/auth/src/cookie-domain.ts+++ b/packages/core/src/modules/auth/src/cookie-domain.ts@@ -1,3 +1,5 @@+import type { CoreConfig } from '../../../config';+// List of known multi-part TLDs that should be treated as single domainsconst MULTI_PART_TLDS = [/com\.\w{2}$/,@@ -12,30 +14,18 @@ const MULTI_PART_TLDS = [/go\.\w{2}$/,];-function getCustomMultiPartTLDs(): string[] {- const envValue = process.env.COOKIE_TLDS || '';- if (!envValue.trim()) {- return [];- }- return envValue- .split(',')- .map((tld) => tld.trim().toLowerCase())- .filter((tld) => tld.length > 0);-}--function isMultiPartTLD(potentialTLD: string): boolean {+function isMultiPartTLD(config: CoreConfig, potentialTLD: string): boolean {if (MULTI_PART_TLDS.some((pattern) => pattern.test(potentialTLD))) {return true;}- const customTLDs = getCustomMultiPartTLDs();- return customTLDs.includes(potentialTLD.toLowerCase());+ return config.cookies.extraMultiPartTlds.includes(potentialTLD.toLowerCase());}-export const parseCookieDomain = (url: string) => {- if (process.env.CUSTOM_COOKIE_DOMAIN) {+export const parseCookieDomain = (config: CoreConfig, url: string) => {+ if (config.cookies.customDomain) {return {- domain: process.env.CUSTOM_COOKIE_DOMAIN,+ domain: config.cookies.customDomain,secure: true,};}@@ -63,7 +53,7 @@ export const parseCookieDomain = (url: string) => {// Handle multi-part TLDs like co.uk, com.au, etc.if (parts.length >= 3) {const potentialTLD = parts.slice(-2).join('.');- if (isMultiPartTLD(potentialTLD)) {+ if (isMultiPartTLD(config, potentialTLD)) {// For domains like example.co.uk or subdomain.example.co.uk// Use the last 3 parts: .example.co.ukreturn {diff --git a/packages/core/src/modules/auth/src/cookie.ts b/packages/core/src/modules/auth/src/cookie.tsindex 320b48179..989a96b67 100644--- a/packages/core/src/modules/auth/src/cookie.ts+++ b/packages/core/src/modules/auth/src/cookie.ts@@ -1,30 +1,38 @@+import type { CoreConfig } from '../../../config';import type { ISetCookie } from '../../../shared/cookie';-import { COOKIE_OPTIONS } from './constants';+import { cookieOptions } from './constants';++const LAST_AUTH_PROVIDER_MAX_AGE_SECONDS = 60 * 60 * 24 * 365;export function setSessionTokenCookie(+ config: CoreConfig,setCookie: ISetCookie,token: string,expiresAt: Date): void {setCookie('session', token, {maxAge: Math.floor((expiresAt.getTime() - Date.now()) / 1000),- ...COOKIE_OPTIONS,+ ...cookieOptions(config),});}export function setLastAuthProviderCookie(+ config: CoreConfig,setCookie: ISetCookie,provider: string): void {setCookie('last-auth-provider', provider, {- maxAge: 60 * 60 * 24 * 365,- ...COOKIE_OPTIONS,+ maxAge: LAST_AUTH_PROVIDER_MAX_AGE_SECONDS,+ ...cookieOptions(config),});}-export function deleteSessionTokenCookie(setCookie: ISetCookie): void {+export function deleteSessionTokenCookie(+ config: CoreConfig,+ setCookie: ISetCookie+): void {setCookie('session', '', {maxAge: 0,- ...COOKIE_OPTIONS,+ ...cookieOptions(config),});}diff --git a/packages/core/src/modules/auth/src/login-session.ts b/packages/core/src/modules/auth/src/login-session.tsindex 29df7c93a..a3db4b684 100644--- a/packages/core/src/modules/auth/src/login-session.ts+++ b/packages/core/src/modules/auth/src/login-session.ts@@ -14,7 +14,7 @@ import { decodeSessionToken, hashSessionToken } from './token';/** Only Postgres — narrowed so `AppDeps` (http/session.ts, before* `ServiceDeps.queues` exists) satisfies it with no cast. */-type LoginSessionDeps = Pick<ServiceDeps, 'db'>;+type LoginSessionDeps = Pick<ServiceDeps, 'db' | 'config'>;const SESSION_TTL_MS = 1000 * 60 * 60 * 24 * 30;const SESSION_RENEWAL_THRESHOLD_MS = 1000 * 60 * 60 * 24 * 15;@@ -76,8 +76,9 @@ export async function validateSessionToken(deps: LoginSessionDeps,token: string | null | undefined): Promise<SessionValidationResult> {- if (process.env.DEMO_USER_ID) {- return createDemoSession(deps, process.env.DEMO_USER_ID);+ const demoUserId = deps.config.demoUserId;+ if (demoUserId) {+ return createDemoSession(deps, demoUserId);}if (!token) {diff --git a/packages/core/src/modules/auth/src/oauth.ts b/packages/core/src/modules/auth/src/oauth.tsindex b14083df3..e9e205035 100644--- a/packages/core/src/modules/auth/src/oauth.ts+++ b/packages/core/src/modules/auth/src/oauth.ts@@ -1,5 +1,6 @@import * as Arctic from 'arctic';import { GitHub } from 'arctic';+import type { CoreConfig } from '../../../config';export type { OAuth2Tokens } from 'arctic';// Re-exported straight from source, not the `Arctic` import above — that one@@ -7,20 +8,21 @@ export type { OAuth2Tokens } from 'arctic';// `noExportedImports` over a binding that genuinely has a second use.export * as Arctic from 'arctic';-export const github = new GitHub(- process.env.GITHUB_CLIENT_ID ?? '',- process.env.GITHUB_CLIENT_SECRET ?? '',- process.env.GITHUB_REDIRECT_URI ?? ''-);+// Built per call, not at import: an arctic client is a credential holder with+// no socket, so there is nothing to keep alive and nothing to construct at+// module scope (ADR-022 R15).+export function githubClient(config: CoreConfig): GitHub {+ const { clientId, clientSecret, redirectUri } = config.auth.github;+ return new GitHub(clientId, clientSecret, redirectUri);+}-export const google = new Arctic.Google(- process.env.GOOGLE_CLIENT_ID ?? '',- process.env.GOOGLE_CLIENT_SECRET ?? '',- process.env.GOOGLE_REDIRECT_URI ?? ''-);+export function googleClient(config: CoreConfig): Arctic.Google {+ const { clientId, clientSecret, redirectUri } = config.auth.google;+ return new Arctic.Google(clientId, clientSecret, redirectUri);+}-export const googleGsc = new Arctic.Google(- process.env.GOOGLE_CLIENT_ID ?? '',- process.env.GOOGLE_CLIENT_SECRET ?? '',- process.env.GSC_GOOGLE_REDIRECT_URI ?? ''-);+/** The same Google credentials, redirected at the Search Console callback. */+export function googleGscClient(config: CoreConfig): Arctic.Google {+ const { clientId, clientSecret, redirectUri } = config.auth.googleGsc;+ return new Arctic.Google(clientId, clientSecret, redirectUri);+}diff --git a/packages/core/src/modules/auth/src/registration.test.ts b/packages/core/src/modules/auth/src/registration.test.tsindex 6089451f2..e69682ee4 100644--- a/packages/core/src/modules/auth/src/registration.test.ts+++ b/packages/core/src/modules/auth/src/registration.test.ts@@ -1,4 +1,5 @@-import { afterEach, beforeEach, describe, expect, it, mock } from 'bun:test';+import { beforeEach, describe, expect, it, mock } from 'bun:test';+import { testCoreConfig } from '../../../../test/config-fixture';import type { ServiceDeps } from '../../../services';import { getIsRegistrationAllowed } from './registration';@@ -7,14 +8,24 @@ const mockInviteFindUnique = mock(async (): Promise<{ id: string } | null> => null);-const deps = {- db: {- user: { count: mockUserCount },- invite: { findUnique: mockInviteFindUnique },- },-} as unknown as ServiceDeps;--const ORIGINAL_ENV = { ...process.env };+/**+ * ALLOW_REGISTRATION / ALLOW_INVITATION arrive as `config.auth`, where+ * `undefined` is "unset" (cloud) and the loader has already resolved the+ * `!== 'false'` reading.+ */+function depsWith(auth: {+ allowRegistration?: boolean;+ allowInvitation?: boolean;+}): ServiceDeps {+ const base = testCoreConfig();+ return {+ db: {+ user: { count: mockUserCount },+ invite: { findUnique: mockInviteFindUnique },+ },+ config: testCoreConfig({ auth: { ...base.auth, ...auth } }),+ } as unknown as ServiceDeps;+}beforeEach(() => {mockUserCount.mockClear();@@ -24,60 +35,63 @@ beforeEach(() => {mockInviteFindUnique.mockResolvedValue(null);});-afterEach(() => {- process.env = { ...ORIGINAL_ENV };-});-describe('getIsRegistrationAllowed', () => {it('allows everything in cloud (ALLOW_REGISTRATION unset)', async () => {- process.env.ALLOW_REGISTRATION = undefined;- delete process.env.ALLOW_REGISTRATION;-- expect(await getIsRegistrationAllowed(deps)).toBe(true);+ expect(await getIsRegistrationAllowed(depsWith({}))).toBe(true);expect(mockUserCount).not.toHaveBeenCalled();});it('allows the very first user even when registration is disabled', async () => {- process.env.ALLOW_REGISTRATION = 'false';mockUserCount.mockResolvedValue(0);- expect(await getIsRegistrationAllowed(deps)).toBe(true);+ expect(+ await getIsRegistrationAllowed(depsWith({ allowRegistration: false }))+ ).toBe(true);});it('blocks a new user with no invite when registration is disabled', async () => {- process.env.ALLOW_REGISTRATION = 'false';-- expect(await getIsRegistrationAllowed(deps)).toBe(false);+ expect(+ await getIsRegistrationAllowed(depsWith({ allowRegistration: false }))+ ).toBe(false);});it('allows a new user holding a valid invite when registration is disabled', async () => {- process.env.ALLOW_REGISTRATION = 'false';- process.env.ALLOW_INVITATION = 'true';mockInviteFindUnique.mockResolvedValue({ id: 'invite-1' });- expect(await getIsRegistrationAllowed(deps, 'invite-1')).toBe(true);+ expect(+ await getIsRegistrationAllowed(+ depsWith({ allowRegistration: false, allowInvitation: true }),+ 'invite-1'+ )+ ).toBe(true);});it('blocks an unknown invite id', async () => {- process.env.ALLOW_REGISTRATION = 'false';- process.env.ALLOW_INVITATION = 'true';mockInviteFindUnique.mockResolvedValue(null);- expect(await getIsRegistrationAllowed(deps, 'nope')).toBe(false);+ expect(+ await getIsRegistrationAllowed(+ depsWith({ allowRegistration: false, allowInvitation: true }),+ 'nope'+ )+ ).toBe(false);});it('blocks a valid invite when invitations are disabled', async () => {- process.env.ALLOW_REGISTRATION = 'false';- process.env.ALLOW_INVITATION = 'false';mockInviteFindUnique.mockResolvedValue({ id: 'invite-1' });- expect(await getIsRegistrationAllowed(deps, 'invite-1')).toBe(false);+ expect(+ await getIsRegistrationAllowed(+ depsWith({ allowRegistration: false, allowInvitation: false }),+ 'invite-1'+ )+ ).toBe(false);expect(mockInviteFindUnique).not.toHaveBeenCalled();});it('allows open self-hosted registration', async () => {- process.env.ALLOW_REGISTRATION = 'true';-- expect(await getIsRegistrationAllowed(deps)).toBe(true);+ expect(+ await getIsRegistrationAllowed(depsWith({ allowRegistration: true }))+ ).toBe(true);});});diff --git a/packages/core/src/modules/auth/src/registration.ts b/packages/core/src/modules/auth/src/registration.tsindex bf342b82c..765fe1e75 100644--- a/packages/core/src/modules/auth/src/registration.ts+++ b/packages/core/src/modules/auth/src/registration.ts@@ -4,7 +4,7 @@ import type { ServiceDeps } from '../../../services';/** Only Postgres — narrowed so any of this module's several callers, none of* which carry a full `ServiceDeps`, satisfy it with no cast. */-type RegistrationDeps = Pick<ServiceDeps, 'db'>;+type RegistrationDeps = Pick<ServiceDeps, 'db' | 'config'>;/*** Whether a *new* user may be created right now.@@ -19,7 +19,8 @@ export async function getIsRegistrationAllowed(inviteId?: string | null) {// ALLOW_REGISTRATION is always undefined in cloud- if (process.env.ALLOW_REGISTRATION === undefined) {+ const { allowRegistration, allowInvitation } = deps.config.auth;+ if (allowRegistration === undefined) {return true;}@@ -32,7 +33,7 @@ export async function getIsRegistrationAllowed(// 2. If there is an invite, check if it is validif (inviteId) {- if (process.env.ALLOW_INVITATION === 'false') {+ if (allowInvitation === false) {return false;}@@ -46,5 +47,5 @@ export async function getIsRegistrationAllowed(}// 3. Otherwise, check if general registration is allowed- return process.env.ALLOW_REGISTRATION !== 'false';+ return allowRegistration;}diff --git a/packages/core/src/modules/chart/chart.service.ts b/packages/core/src/modules/chart/chart.service.tsindex 565c1230f..12818dfa8 100644--- a/packages/core/src/modules/chart/chart.service.ts+++ b/packages/core/src/modules/chart/chart.service.ts@@ -143,13 +143,6 @@ const EVENT_PROPERTY_KEY_LIMIT = 50_000;* keep the default).*/const DEFAULT_EVENT_PROPERTY_VALUE_AUTOCOMPLETE_LIMIT = 500;-const EVENT_PROPERTY_VALUE_AUTOCOMPLETE_LIMIT = (() => {- const raw = process.env.EVENT_PROPERTY_VALUE_AUTOCOMPLETE_LIMIT;- const parsed = raw && /^\d+$/.test(raw) ? Number(raw) : Number.NaN;- return Number.isSafeInteger(parsed) && parsed > 0- ? parsed- : DEFAULT_EVENT_PROPERTY_VALUE_AUTOCOMPLETE_LIMIT;-})();/** Profile lookups are batched so the `IN (...)` never exceeds max_query_size. */const BUCKET_PROFILES_BATCH_SIZE = 200;@@ -393,7 +386,8 @@ export async function getChartPropertyValues(projectId,property.replace(/^properties\./, ''),event,- EVENT_PROPERTY_VALUE_AUTOCOMPLETE_LIMIT+ deps.config.query.eventPropertyValueAutocompleteLimit ??+ DEFAULT_EVENT_PROPERTY_VALUE_AUTOCOMPLETE_LIMIT));return { values: rows.map((row) => row.property_value) };diff --git a/packages/core/src/modules/chart/funnel.service.ts b/packages/core/src/modules/chart/funnel.service.tsindex cebba69d3..5718ebe41 100644--- a/packages/core/src/modules/chart/funnel.service.ts+++ b/packages/core/src/modules/chart/funnel.service.ts@@ -134,6 +134,7 @@ export async function buildFunnelBase(funnelGroup,cohortMetadata,timezone,+ config: deps.config,});}diff --git a/packages/core/src/modules/chart/src/funnel.sql.test.ts b/packages/core/src/modules/chart/src/funnel.sql.test.tsindex 99b211b40..e5887f75b 100644--- a/packages/core/src/modules/chart/src/funnel.sql.test.ts+++ b/packages/core/src/modules/chart/src/funnel.sql.test.ts@@ -22,6 +22,7 @@import { afterAll, beforeAll, describe, expect, it, mock } from 'bun:test';import type { SqlFragment } from '@openpanel/db/src/clickhouse/sql';+import { testCoreConfig } from '../../../../test/config-fixture';import type {IChartBreakdown,IReportInput,@@ -162,14 +163,17 @@ const chartSql = async (breakdowns: IChartBreakdown[]) =>const chartText = async (breakdowns: IChartBreakdown[]) =>render(await chartStatement({ breakdowns })).text;-const originalNonStrict = process.env.FUNNEL_NON_STRICT_ORDERING;-function setNonStrictOrdering(value: string | undefined) {- if (value === undefined) {- process.env.FUNNEL_NON_STRICT_ORDERING = undefined;- delete process.env.FUNNEL_NON_STRICT_ORDERING;- return;- }- process.env.FUNNEL_NON_STRICT_ORDERING = value;+/** `deps` with FUNNEL_NON_STRICT_ORDERING flipped, as the loader resolves it. */+function withNonStrictOrdering(+ nonStrict: boolean+): import('../../../services').ServiceDeps {+ // Object.create, not a spread: `testServiceDeps` exposes `clients`/`buffers`+ // as throwing getters and a spread would read them.+ return Object.assign(Object.create(deps), {+ config: testCoreConfig({+ query: { ...deps.config.query, funnelNonStrictOrdering: nonStrict },+ }),+ });}beforeAll(async () => {@@ -384,27 +388,26 @@ describe('funnel.sql / buildFunnelBase — breakdown attribution', () => {});describe('funnel.sql / funnel CTE — windowFunnel ordering', () => {- afterAll(() => {- setNonStrictOrdering(originalNonStrict);- });-it('defaults to strict_increase (unchanged behavior)', async () => {- // Set to '' rather than deleting: a dev shell with the variable exported- // would otherwise flip this test.- setNonStrictOrdering('');expect(await chartSql([])).toContain("'strict_increase'");});it('drops strict_increase when FUNNEL_NON_STRICT_ORDERING is set', async () => {- setNonStrictOrdering('1');- const sql = await chartSql([]);+ const base = await buildFunnelBase(+ withNonStrictOrdering(true),+ baseInput()+ );+ const sql = render(funnelChartQuery(base)).sql;expect(sql).not.toContain('strict_increase');expect(sql).toMatch(/windowFunnel\(\{p:UInt64\}\)\(/);});it('non-strict funnel SQL parses and resolves', async () => {- setNonStrictOrdering('true');- const statement = await chartStatement();+ const base = await buildFunnelBase(+ withNonStrictOrdering(true),+ baseInput()+ );+ const statement = funnelChartQuery(base);expect(render(statement).sql).not.toContain('strict_increase');await explain(statement);});diff --git a/packages/core/src/modules/chart/src/funnel.sql.ts b/packages/core/src/modules/chart/src/funnel.sql.tsindex b1306ec34..b664f7210 100644--- a/packages/core/src/modules/chart/src/funnel.sql.ts+++ b/packages/core/src/modules/chart/src/funnel.sql.ts@@ -23,6 +23,7 @@// no `IN (subquery)` is introduced or removed.import { type SqlFragment, sql } from '@openpanel/db/src/clickhouse/sql';+import type { CoreConfig } from '../../../config';import type {IChartBreakdown,IChartEvent,@@ -71,11 +72,10 @@ const JOINABLE_PROFILE_COLUMNS = [*/const STRICT_INCREASE_MODE = ", 'strict_increase'";-function windowFunnelMode(): SqlFragment {- const nonStrictOrdering =- process.env.FUNNEL_NON_STRICT_ORDERING === '1' ||- process.env.FUNNEL_NON_STRICT_ORDERING === 'true';- return nonStrictOrdering ? sql.empty : compiledText(STRICT_INCREASE_MODE);+function windowFunnelMode(config: CoreConfig): SqlFragment {+ return config.query.funnelNonStrictOrdering+ ? sql.empty+ : compiledText(STRICT_INCREASE_MODE);}export interface FunnelBaseInput {@@ -93,6 +93,8 @@ export interface FunnelBaseInput {cohortMetadata: Map<string, CohortMetadata>;/** Travels to `session_timezone`; `toDateTime('…')` parses in it. */timezone: string;+ /** FUNNEL_NON_STRICT_ORDERING decides `windowFunnel`'s ordering mode. */+ config: CoreConfig;}/** Everything the funnel chart and the funnel profile list share. */@@ -293,7 +295,7 @@ export function funnelBase(input: FunnelBaseInput): FunnelBase {const primaryKey = sql.id(group, FUNNEL_GROUPS);const cteSelects: SqlFragment[] = [primaryKey,- sql`windowFunnel(${sql.uint64(input.funnelWindowMilliseconds)}${windowFunnelMode()})(toUInt64(toUnixTimestamp64Milli(created_at)), ${sql.join(conditions)}) AS level`,+ sql`windowFunnel(${sql.uint64(input.funnelWindowMilliseconds)}${windowFunnelMode(input.config)})(toUInt64(toUnixTimestamp64Milli(created_at)), ${sql.join(conditions)}) AS level`,];if (group === 'session_id') {// Resolves identity changes mid-session.diff --git a/packages/core/src/modules/cohort/cohort.jobs.test.ts b/packages/core/src/modules/cohort/cohort.jobs.test.tsindex 8d72967d6..a73095892 100644--- a/packages/core/src/modules/cohort/cohort.jobs.test.ts+++ b/packages/core/src/modules/cohort/cohort.jobs.test.ts@@ -2,6 +2,7 @@// methods themselves are exercised in cohort.service.test.ts.import { expect, test } from 'bun:test';+import { testCoreConfig } from '../../../test/config-fixture';import type { AppDeps, Buffers, JobCtx } from '../../context';import { createRecordingProducers } from '../../jobs/testing';import { queues } from '../../jobs.registry';@@ -74,6 +75,7 @@ function stubJobCtx(buffers: {} as Buffers,logger: stubLogger(),queues: createRecordingProducers(queues).queues,+ config: testCoreConfig(),services,requestId: 'req_1',job: { id: 'job_1', attempt: 0, queue: 'cohortCompute', name: jobName },diff --git a/packages/core/src/modules/cohort/cohort.service.test.ts b/packages/core/src/modules/cohort/cohort.service.test.tsindex 3340d6e67..61dcdd59c 100644--- a/packages/core/src/modules/cohort/cohort.service.test.ts+++ b/packages/core/src/modules/cohort/cohort.service.test.ts@@ -10,6 +10,7 @@// ClickHouse connection.import { afterAll, beforeAll, expect, mock, test } from 'bun:test';+import { testCoreConfig } from '../../../test/config-fixture';import { testServices } from '../../../test/service-deps';interface FakeCohort {@@ -76,6 +77,7 @@ afterAll(() => {const deps = {db: { cohort },ch: { insert: chInsert, command: chCommand },+ config: testCoreConfig(),} as unknown as import('../../services').ServiceDeps;let subject: typeof import('./cohort.service');diff --git a/packages/core/src/modules/cohort/cohort.service.ts b/packages/core/src/modules/cohort/cohort.service.tsindex 05a0512c4..a92c87af7 100644--- a/packages/core/src/modules/cohort/cohort.service.ts+++ b/packages/core/src/modules/cohort/cohort.service.ts@@ -38,9 +38,9 @@import type { ClickHouseSettings } from '@clickhouse/client';import { type SqlFragment, sql } from '@openpanel/db/src/clickhouse/sql';+import type { CoreConfig } from '../../config';import type { ServiceDeps, Services } from '../../services';import { chQuery } from '../../shared/ch-query';-import { isClickhouseClustered } from '../../shared/ch-tables';import type { IServiceProfile } from '../profile/profile.service';import type { IChartEventFilter } from '../report/report.constants';import type {@@ -73,8 +73,11 @@ const TABLE = {* `'{cluster}'` is a ClickHouse *macro*, not a `{name:Type}` placeholder — it* carries no type suffix, so parameter substitution leaves it alone.*/-export function replicatedTarget(tableName: string): SqlFragment {- if (isClickhouseClustered()) {+export function replicatedTarget(+ clustered: boolean,+ tableName: string+): SqlFragment {+ if (clustered) {return sql`${sql.id(`${tableName}_replicated`)} ON CLUSTER '{cluster}'`;}return sql.id(tableName);@@ -85,24 +88,18 @@ export function replicatedTarget(tableName: string): SqlFragment {// with bigger cohorts need to raise it — env-tunable to avoid an image// rebuild for what is really a sizing knob.//-// Strictly a positive safe integer: anything else falls back to the-// default. Number.parseInt would accept '5000junk' or '-1' (LIMIT -1 is a-// query error), and 0 is falsy at the `limit ? LIMIT ... : ''` call sites,-// which would silently remove the cap entirely.-function parsePositiveInt(raw: string | undefined): number | undefined {- if (!(raw && /^\d+$/.test(raw))) {- return undefined;- }- const parsed = Number(raw);- return Number.isSafeInteger(parsed) && parsed > 0 ? parsed : undefined;+// Strictly a positive safe integer, which is what the config loader's+// `optionalPositiveInt` guarantees: '5000junk', '-1' and '0' all arrive as+// undefined, and 0 is falsy at the `limit ? LIMIT ... : ''` call sites, which+// would silently remove the cap entirely.+const DEFAULT_COHORT_MATERIALIZE_LIMIT = 10_000;++export function cohortMaterializeLimit(config: CoreConfig): number {+ return (+ config.query.cohortMaterializeLimit ?? DEFAULT_COHORT_MATERIALIZE_LIMIT+ );}-const COHORT_MATERIALIZE_LIMIT_PARSED = parsePositiveInt(- process.env.COHORT_MATERIALIZE_LIMIT-);-export const COHORT_MATERIALIZE_LIMIT =- COHORT_MATERIALIZE_LIMIT_PARSED ?? 10_000;-// Property cohorts aggregate every profile row for the project, so they are// the one cohort query that can outgrow the server's memory headroom. Two// opt-in knobs bound them; with NEITHER set, no per-query settings are@@ -122,20 +119,18 @@ export const COHORT_MATERIALIZE_LIMIT =// ~281MB spilled whether the threshold was 300, 512 or 768MB, at// 6.9s/6.7s/6.0s, while peak memory climbed 410/695/893MiB).//-// A standalone function of its raw env inputs (not a module-level read), so-// a test can exercise every branch by calling it directly — bun:test shares-// one module registry per file even under --isolate, so vitest's+// A standalone function of its two parsed inputs (not a module-level read),+// so a test can exercise every branch by calling it directly — bun:test+// shares one module registry per file even under --isolate, so vitest's// vi.resetModules()-per-case porting has no equivalent (see AGENTS.md; this// was the one vi.resetModules site in the suite, ADR-010's tail table).export function deriveCohortQuerySettings({- memoryLimitBytesRaw,- spillBytesRaw,+ memoryLimitBytes,+ spillBytes: spillBytesParsed,}: {- memoryLimitBytesRaw: string | undefined;- spillBytesRaw: string | undefined;+ memoryLimitBytes: number | undefined;+ spillBytes: number | undefined;}): ClickHouseSettings {- const memoryLimitBytes = parsePositiveInt(memoryLimitBytesRaw);- const spillBytesParsed = parsePositiveInt(spillBytesRaw);const spillBytes =memoryLimitBytes !== undefined &&(spillBytesParsed === undefined || spillBytesParsed >= memoryLimitBytes)@@ -155,11 +150,14 @@ export function deriveCohortQuerySettings({};}-export const PROFILE_COHORT_QUERY_SETTINGS: ClickHouseSettings =- deriveCohortQuerySettings({- memoryLimitBytesRaw: process.env.COHORT_QUERY_MEMORY_LIMIT_BYTES,- spillBytesRaw: process.env.COHORT_QUERY_SPILL_BYTES,+export function profileCohortQuerySettings(+ config: CoreConfig+): ClickHouseSettings {+ return deriveCohortQuerySettings({+ memoryLimitBytes: config.query.cohortQueryMemoryLimitBytes,+ spillBytes: config.query.cohortQuerySpillBytes,});+}// The column is a parameter rather than a post-hoc `.replace('created_at',// 'event_date')` on finished text: V1 rewrote the clause that way at all four@@ -381,7 +379,7 @@ export function buildPropertyBasedCohortQuery(// Resolve each profile's newest row with GROUP BY + argMax instead of// FINAL: FINAL cannot spill to disk, so on wide projects the dedup itself// is what runs out of memory. The aggregate shape spills normally under- // PROFILE_COHORT_QUERY_SETTINGS, and filters on aggregates move to HAVING.+ // profileCohortQuerySettings, and filters on aggregates move to HAVING.return sql`SELECT id as profile_idFROM ${sql.id(TABLE.profiles)}@@ -601,7 +599,7 @@ export async function computePropertyBasedCohort(deps,buildPropertyBasedCohortQuery(projectId, definition, limit),- PROFILE_COHORT_QUERY_SETTINGS+ profileCohortQuerySettings(deps.config));return results.map((r) => r.profile_id);}@@ -619,7 +617,7 @@ export async function countPropertyBasedCohort(deps,sql`SELECT count() as count FROM (${buildPropertyBasedCohortQuery(projectId, definition)})`,- PROFILE_COHORT_QUERY_SETTINGS+ profileCohortQuerySettings(deps.config));return results[0]?.count ?? 0;}@@ -782,7 +780,7 @@ export async function updateCohortMembership(deps,cohort.projectId,definition,- COHORT_MATERIALIZE_LIMIT+ cohortMaterializeLimit(deps.config));const version = Date.now();@@ -791,7 +789,7 @@ export async function updateCohortMembership(// (project_id, cohort_id, profile_id), so profiles that fell out of the// cohort definition would otherwise linger forever. Clear them first.await ch.command({- ...sql`DELETE FROM ${replicatedTarget(TABLE.cohortMembers)} WHERE cohort_id = ${sql.string(cohort.id)} AND project_id = ${sql.string(cohort.projectId)}`.toStatement(),+ ...sql`DELETE FROM ${replicatedTarget(deps.config.clickhouseClustered, TABLE.cohortMembers)} WHERE cohort_id = ${sql.string(cohort.id)} AND project_id = ${sql.string(cohort.projectId)}`.toStatement(),clickhouse_settings: {lightweight_deletes_sync: '1',},@@ -823,7 +821,7 @@ export async function deleteCohortMembership(const where = sql`cohort_id = ${sql.string(cohortId)} AND project_id = ${sql.string(projectId)}`;for (const table of [TABLE.cohortMembers, TABLE.cohortMetadata]) {await ch.command({- ...sql`DELETE FROM ${replicatedTarget(table)} WHERE ${where}`.toStatement(),+ ...sql`DELETE FROM ${replicatedTarget(deps.config.clickhouseClustered, table)} WHERE ${where}`.toStatement(),clickhouse_settings: {lightweight_deletes_sync: '0',},diff --git a/packages/core/src/modules/cohort/src/cohort-sql.test.ts b/packages/core/src/modules/cohort/src/cohort-sql.test.tsindex 29aa96da9..467a0c19d 100644--- a/packages/core/src/modules/cohort/src/cohort-sql.test.ts+++ b/packages/core/src/modules/cohort/src/cohort-sql.test.ts@@ -166,11 +166,13 @@ test('binds user-controlled property keys instead of escaping them', () => {});test('replicatedTarget renders exactly what getReplicatedTableName produces', () => {- for (const table of ['cohort_members', 'cohort_metadata']) {- expect(replicatedTarget(table).toStatement()).toEqual({- query: getReplicatedTableName(table),- query_params: {},- });+ for (const clustered of [true, false]) {+ for (const table of ['cohort_members', 'cohort_metadata']) {+ expect(replicatedTarget(clustered, table).toStatement()).toEqual({+ query: getReplicatedTableName(clustered, table),+ query_params: {},+ });+ }}});@@ -203,8 +205,8 @@ test('matches nothing when every filter was dropped as empty', () => {test('deriveCohortQuerySettings applies NO settings when neither variable is set (upstream defaults govern)', () => {expect(deriveCohortQuerySettings({- memoryLimitBytesRaw: undefined,- spillBytesRaw: undefined,+ memoryLimitBytes: undefined,+ spillBytes: undefined,})).toEqual({});});@@ -212,8 +214,8 @@ test('deriveCohortQuerySettings applies NO settings when neither variable is settest('deriveCohortQuerySettings derives the spill threshold as limit/3 when only the limit is set', () => {expect(deriveCohortQuerySettings({- memoryLimitBytesRaw: '3000000000',- spillBytesRaw: undefined,+ memoryLimitBytes: 3_000_000_000,+ spillBytes: undefined,})).toEqual({max_bytes_before_external_group_by: '1000000000',@@ -224,8 +226,8 @@ test('deriveCohortQuerySettings derives the spill threshold as limit/3 when onlytest('deriveCohortQuerySettings respects both values when both are set', () => {expect(deriveCohortQuerySettings({- memoryLimitBytesRaw: '2000000000',- spillBytesRaw: '500000000',+ memoryLimitBytes: 2_000_000_000,+ spillBytes: 500_000_000,})).toEqual({max_bytes_before_external_group_by: '500000000',@@ -236,8 +238,8 @@ test('deriveCohortQuerySettings respects both values when both are set', () => {test('deriveCohortQuerySettings applies only the spill threshold when only it is set', () => {expect(deriveCohortQuerySettings({- memoryLimitBytesRaw: undefined,- spillBytesRaw: '500000000',+ memoryLimitBytes: undefined,+ spillBytes: 500_000_000,})).toEqual({ max_bytes_before_external_group_by: '500000000' });});@@ -248,8 +250,8 @@ test('deriveCohortQuerySettings re-derives an inverted pair (spill >= limit woul// writes to disk — the exact inversion ClickHouse Cloud ships.expect(deriveCohortQuerySettings({- memoryLimitBytesRaw: '900000000',- spillBytesRaw: '900000000',+ memoryLimitBytes: 900_000_000,+ spillBytes: 900_000_000,})).toEqual({max_bytes_before_external_group_by: '300000000',@@ -258,10 +260,10 @@ test('deriveCohortQuerySettings re-derives an inverted pair (spill >= limit woul});test('deriveCohortQuerySettings never derives a zero spill threshold (0 would DISABLE spilling)', () => {- for (const memoryLimitBytesRaw of ['1', '2', '3']) {+ for (const memoryLimitBytes of [1, 2, 3]) {const settings = deriveCohortQuerySettings({- memoryLimitBytesRaw,- spillBytesRaw: undefined,+ memoryLimitBytes,+ spillBytes: undefined,});expect(Number(settings.max_bytes_before_external_group_by)@@ -269,11 +271,5 @@ test('deriveCohortQuerySettings never derives a zero spill threshold (0 would DI}});-test('deriveCohortQuerySettings ignores malformed values', () => {- expect(- deriveCohortQuerySettings({- memoryLimitBytesRaw: '2gb',- spillBytesRaw: '-1',- })- ).toEqual({});-});+// A malformed COHORT_QUERY_* value is rejected by the config loader now, not+// here — `apps/api/src/config/env.test.ts` owns that case since M15-006.diff --git a/packages/core/src/modules/event/event.service.ts b/packages/core/src/modules/event/event.service.tsindex b8b9b6660..be468d697 100644--- a/packages/core/src/modules/event/event.service.ts+++ b/packages/core/src/modules/event/event.service.ts@@ -22,8 +22,8 @@// shared/ch-query.ts) and the event/bot buffers as `deps.buffers.*`. The// `loadDb` / `loadChClient` / `loadDbBuffers` lazy loaders are gone, and so// are the two `import('@openpanel/core')` self-barrel hops this file made for-// `resolveMaxLookbackDays` / `resolveDateRange` — both are imported straight-// from `shared/` (docs/TECH_DEBT.md §2, §4). What stays lazy is named and+// `resolveDateRange` — imported straight from `shared/`+// (docs/TECH_DEBT.md §2, §4). What stays lazy is named and// argued at each remaining `load*` below; none of them reach @openpanel/db.import type { SqlFragment } from '@openpanel/db/src/clickhouse/sql';@@ -33,7 +33,6 @@ import type { ServiceDeps, Services } from '../../services';import { cacheablePerDeps } from '../../shared/cacheable-per-deps';import { chQuery } from '../../shared/ch-query';import { DateTime, resolveDateRange } from '../../shared/date';-import { resolveMaxLookbackDays } from '../../shared/lookback';import { toDots } from '../../shared/object';import { getEventFiltersWhereClause } from '../chart/src/filter-where';import {@@ -79,7 +78,6 @@ const EVENT_METAS_CACHE_NAME = 'getEventMetas';const TOP_EVENT_NAMES_CACHE_SECONDS = 60 * 10;const EVENT_LIST_DEFAULT_LOOKBACK_DAYS = 0.5;const EVENT_LIST_MAX_LOOKBACK_DAYS_DEFAULT = 365 * 5;-const EVENT_LIST_MAX_LOOKBACK_DAYS_ENV = 'EVENT_LIST_MAX_LOOKBACK_DAYS';const QUERY_EVENTS_DEFAULT_LIMIT = 20;const CLICKHOUSE_DATETIME_FORMAT = 'yyyy-MM-dd HH:mm:ss.SSS';const MASK_CHARACTER = '*';@@ -737,11 +735,11 @@ export async function getEventList(dateIntervalInDays = EVENT_LIST_DEFAULT_LOOKBACK_DAYS,} = options;- // Deployment-tunable ceiling for the empty-result lookback (see lookback.ts).- const maxLookbackDays = resolveMaxLookbackDays(- EVENT_LIST_MAX_LOOKBACK_DAYS_ENV,- EVENT_LIST_MAX_LOOKBACK_DAYS_DEFAULT- );+ // Deployment-tunable ceiling for the empty-result lookback+ // (EVENT_LIST_MAX_LOOKBACK_DAYS).+ const maxLookbackDays =+ deps.config.query.eventListMaxLookbackDays ??+ EVENT_LIST_MAX_LOOKBACK_DAYS_DEFAULT;const lookbackDays = Math.min(dateIntervalInDays, maxLookbackDays);const hasLookback = hasEventListLookback(options);diff --git a/packages/core/src/modules/gsc/gsc.jobs.test.ts b/packages/core/src/modules/gsc/gsc.jobs.test.tsindex bc58476de..c914fa4a3 100644--- a/packages/core/src/modules/gsc/gsc.jobs.test.ts+++ b/packages/core/src/modules/gsc/gsc.jobs.test.ts@@ -2,6 +2,7 @@// methods themselves are exercised in gsc.service.test.ts.import { expect, test } from 'bun:test';+import { testCoreConfig } from '../../../test/config-fixture';import type { AppDeps, Buffers, JobCtx } from '../../context';import { createRecordingProducers } from '../../jobs/testing';import { queues } from '../../jobs.registry';@@ -74,6 +75,7 @@ function stubJobCtx(buffers: {} as Buffers,logger: stubLogger(),queues: createRecordingProducers(queues).queues,+ config: testCoreConfig(),services,requestId: 'req_1',job: { id: 'job_1', attempt: 0, queue: 'gsc', name: jobName },@@ -151,6 +153,7 @@ test('gscSync fans out one gscProjectSync enqueue per connected project', asyncbuffers: {} as Buffers,logger: stubLogger(),queues: producers.queues,+ config: testCoreConfig(),services: {auth: {} as Services['auth'],notification: {} as Services['notification'],diff --git a/packages/core/src/modules/gsc/gsc.routes.ts b/packages/core/src/modules/gsc/gsc.routes.tsindex e6eaf99b0..b4044ad25 100644--- a/packages/core/src/modules/gsc/gsc.routes.ts+++ b/packages/core/src/modules/gsc/gsc.routes.ts@@ -21,14 +21,11 @@ const gscCallbackQuery = z.object({state: z.string(),});-function dashboardUrl(): string {- return (- process.env.DASHBOARD_URL || process.env.NEXT_PUBLIC_DASHBOARD_URL || ''- );-}+/** Only used to make the URL parseable when DASHBOARD_URL is not set. */+const FALLBACK_ORIGIN = 'http://localhost';-function loginErrorRedirect(message: string) {- const url = new URL('/login', dashboardUrl() || 'http://localhost');+function loginErrorRedirect(dashboardUrl: string, message: string) {+ const url = new URL('/login', dashboardUrl || FALLBACK_ORIGIN);url.searchParams.set('error', message);return redirect(url.toString());}@@ -42,7 +39,10 @@ export const gscRoutes = defineRoutes((app) =>const projectId = ctx.cookies.get('gsc_project_id');if (!(storedState && codeVerifier && projectId)) {- return loginErrorRedirect('Missing GSC OAuth cookies');+ return loginErrorRedirect(+ ctx.config.dashboardUrl,+ 'Missing GSC OAuth cookies'+ );}try {@@ -54,7 +54,7 @@ export const gscRoutes = defineRoutes((app) =>projectId,});return redirect(- `${dashboardUrl()}/${organizationId}/${projectId}/settings/gsc`+ `${ctx.config.dashboardUrl}/${organizationId}/${projectId}/settings/gsc`);} catch (error) {ctx.logger.error({ err: error }, 'GSC OAuth callback error');@@ -62,7 +62,7 @@ export const gscRoutes = defineRoutes((app) =>error instanceof Error? error.message: 'Failed to connect Google Search Console';- return loginErrorRedirect(message);+ return loginErrorRedirect(ctx.config.dashboardUrl, message);}},{ query: gscCallbackQuery, detail: { hide: true } }diff --git a/packages/core/src/modules/gsc/gsc.rpc.ts b/packages/core/src/modules/gsc/gsc.rpc.tsindex 58757f128..671f74c85 100644--- a/packages/core/src/modules/gsc/gsc.rpc.ts+++ b/packages/core/src/modules/gsc/gsc.rpc.ts@@ -17,7 +17,7 @@ import {type TrpcContext,} from '../../rpc/base';import { TRPCAccessError } from '../../rpc/errors';-import { Arctic, googleGsc } from '../auth/auth.service';+import { Arctic, googleGscClient } from '../auth/auth.service';import { zRange, zTimeInterval } from '../report/report.constants';const OAUTH_COOKIE_MAX_AGE_SECONDS = 60 * 10;@@ -78,9 +78,11 @@ export const gscRouter = createTRPCRouter({const state = Arctic.generateState();const codeVerifier = Arctic.generateCodeVerifier();- const url = googleGsc.createAuthorizationURL(state, codeVerifier, [- 'https://www.googleapis.com/auth/webmasters.readonly',- ]);+ const url = googleGscClient(ctx.config).createAuthorizationURL(+ state,+ codeVerifier,+ ['https://www.googleapis.com/auth/webmasters.readonly']+ );url.searchParams.set('access_type', 'offline');url.searchParams.set('prompt', 'consent');diff --git a/packages/core/src/modules/gsc/gsc.service.test.ts b/packages/core/src/modules/gsc/gsc.service.test.tsindex a3d6cb37f..13b95dc88 100644--- a/packages/core/src/modules/gsc/gsc.service.test.ts+++ b/packages/core/src/modules/gsc/gsc.service.test.ts@@ -5,6 +5,7 @@// (side-effect-free) import.import { afterAll, beforeAll, expect, mock, test } from 'bun:test';+import { testCoreConfig } from '../../../test/config-fixture';process.env.ENCRYPTION_KEY = 'a'.repeat(64);@@ -114,6 +115,8 @@ mock.module('../../shared/ch-query', () => ({const deps = {db: { gscConnection, project },ch,+ // The GSC tokens are stored encrypted; the key arrives as config now.+ config: testCoreConfig({ encryptionKey: 'a'.repeat(64) }),} 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.@@ -158,7 +161,7 @@ const validateAuthorizationCode = mock(async () => ({const actualAuthService = await import('../auth/auth.service');mock.module('../auth/auth.service', () => ({...actualAuthService,- googleGsc: { validateAuthorizationCode },+ googleGscClient: () => ({ validateAuthorizationCode }),}));const getSettingsForProject = mock(diff --git a/packages/core/src/modules/gsc/gsc.service.ts b/packages/core/src/modules/gsc/gsc.service.tsindex 50ac9ddf8..149eba746 100644--- a/packages/core/src/modules/gsc/gsc.service.ts+++ b/packages/core/src/modules/gsc/gsc.service.ts@@ -24,7 +24,7 @@// tag: ADR-013 converts the analytics read path one query per P7 task, and// this module's queries haven't been converted yet.-import { createLogger, type ILogger } from '../../clients/logger';+import type { CoreConfig } from '../../config';import type { Logger } from '../../logger';import { TRPCNotFoundError } from '../../rpc/errors';import type { ServiceDeps, Services } from '../../services';@@ -32,36 +32,32 @@ import { cacheablePerDeps } from '../../shared/cacheable-per-deps';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';+import { googleGscClient } from '../auth/auth.service';const BACKFILL_MONTHS = 6;const CHUNK_DAYS = 14;const CANNIBALIZATION_CACHE_TTL_SEC = 60 * 60 * 4;const GSC_ROW_LIMIT = 25_000;-let _logger: ILogger | undefined;-function getLogger(): ILogger {- _logger ??= createLogger({ name: 'core:gsc' });- return _logger;-}-export interface GscSite {siteUrl: string;permissionLevel: string;}async function refreshGscToken(+ config: CoreConfig,refreshToken: string): Promise<{ accessToken: string; expiresAt: Date }> {- if (!(process.env.GOOGLE_CLIENT_ID && process.env.GOOGLE_CLIENT_SECRET)) {+ const { clientId, clientSecret } = config.auth.google;+ if (!(clientId && clientSecret)) {throw new Error('GOOGLE_CLIENT_ID or GOOGLE_CLIENT_SECRET is not set in this environment');}const params = new URLSearchParams({- client_id: process.env.GOOGLE_CLIENT_ID,- client_secret: process.env.GOOGLE_CLIENT_SECRET,+ client_id: clientId,+ client_secret: clientSecret,refresh_token: refreshToken,grant_type: 'refresh_token',});@@ -98,14 +94,14 @@ export async function getGscAccessToken(conn.accessTokenExpiresAt &&conn.accessTokenExpiresAt.getTime() > Date.now() + 60_000) {- getLogger().info(+ deps.logger.info({ projectId, expiresAt: conn.accessTokenExpiresAt },'GSC using cached access token');- return decrypt(conn.accessToken);+ return decrypt(deps.config.encryptionKey, conn.accessToken);}- getLogger().info(+ deps.logger.info({projectId,expiresAt: conn.accessTokenExpiresAt,@@ -116,16 +112,17 @@ export async function getGscAccessToken(try {const { accessToken, expiresAt } = await refreshGscToken(- decrypt(conn.refreshToken)+ deps.config,+ decrypt(deps.config.encryptionKey, conn.refreshToken));await db.gscConnection.update({where: { projectId },data: {- accessToken: encrypt(accessToken),+ accessToken: encrypt(deps.config.encryptionKey, accessToken),accessTokenExpiresAt: expiresAt,},});- getLogger().info(+ deps.logger.info({ projectId, expiresAt },'GSC token refreshed successfully');@@ -133,7 +130,7 @@ export async function getGscAccessToken(} catch (error) {const errorMessage =error instanceof Error ? error.message : 'Failed to refresh token';- getLogger().error(+ deps.logger.error({ err: error, projectId, errorMessage },'GSC token refresh failed');@@ -1276,7 +1273,7 @@ export async function listGscConnectionsForSync(export async function runGscProjectSync(deps: ServiceDeps,projectId: string,- logger: Logger = getLogger()+ logger: Logger = deps.logger): Promise<void> {const db = deps.db;const conn = await db.gscConnection.findUnique({ where: { projectId } });@@ -1321,7 +1318,7 @@ export async function runGscProjectSync(export async function runGscProjectBackfill(deps: ServiceDeps,projectId: string,- logger: Logger = getLogger()+ logger: Logger = deps.logger): Promise<void> {const db = deps.db;const conn = await db.gscConnection.findUnique({ where: { projectId } });@@ -1421,7 +1418,7 @@ export async function completeGscOAuthCallback(throw new Error('GSC OAuth state mismatch');}- const tokens = await googleGsc.validateAuthorizationCode(+ const tokens = await googleGscClient(deps.config).validateAuthorizationCode(input.code,input.codeVerifier);@@ -1448,14 +1445,14 @@ export async function completeGscOAuthCallback(where: { projectId: input.projectId },create: {projectId: input.projectId,- accessToken: encrypt(accessToken),- refreshToken: encrypt(refreshToken),+ accessToken: encrypt(deps.config.encryptionKey, accessToken),+ refreshToken: encrypt(deps.config.encryptionKey, refreshToken),accessTokenExpiresAt,siteUrl: '',},update: {- accessToken: encrypt(accessToken),- refreshToken: encrypt(refreshToken),+ accessToken: encrypt(deps.config.encryptionKey, accessToken),+ refreshToken: encrypt(deps.config.encryptionKey, refreshToken),accessTokenExpiresAt,lastSyncStatus: null,lastSyncError: null,diff --git a/packages/core/src/modules/import/import.jobs.test.ts b/packages/core/src/modules/import/import.jobs.test.tsindex 0ecbd9236..e47a1eb73 100644--- a/packages/core/src/modules/import/import.jobs.test.ts+++ b/packages/core/src/modules/import/import.jobs.test.ts@@ -2,6 +2,7 @@// themselves are exercised in import.service.test.ts.import { expect, test } from 'bun:test';+import { testCoreConfig } from '../../../test/config-fixture';import type { AppDeps, Buffers, JobCtx } from '../../context';import { createRecordingProducers } from '../../jobs/testing';import { queues } from '../../jobs.registry';@@ -73,6 +74,7 @@ function stubJobCtx(buffers: {} as Buffers,logger: stubLogger(),queues: createRecordingProducers(queues).queues,+ config: testCoreConfig(),services,requestId: 'req_1',job: { id: 'job_1', attempt: 0, queue: 'import', name: 'import' },diff --git a/packages/core/src/modules/import/import.service.test.ts b/packages/core/src/modules/import/import.service.test.tsindex e11f67e86..87415c835 100644--- a/packages/core/src/modules/import/import.service.test.ts+++ b/packages/core/src/modules/import/import.service.test.ts@@ -5,6 +5,7 @@// other FILE in the process (bare `bun test` shares one module registry).import { beforeAll, expect, mock, test } from 'bun:test';+import { testCoreConfig } from '../../../test/config-fixture';import { testServices } from '../../../test/service-deps';import type { AppDeps, Buffers } from '../../context';import { createRecordingProducers } from '../../jobs/testing';@@ -153,6 +154,7 @@ test('ImportService.enqueue adds the import job onto the import queue', async ()buffers: {} as Buffers,logger: stubLogger(),queues: producers.queues,+ config: testCoreConfig(),},testServices());diff --git a/packages/core/src/modules/import/import.service.ts b/packages/core/src/modules/import/import.service.tsindex f31eba621..0c982aacc 100644--- a/packages/core/src/modules/import/import.service.ts+++ b/packages/core/src/modules/import/import.service.ts@@ -20,7 +20,6 @@import { createHash } from 'node:crypto';import type { Prisma } from '@openpanel/db/src/prisma-client';-import { createLogger, type ILogger } from '../../clients/logger';import type { Logger } from '../../logger';import type { ServiceDeps, Services } from '../../services';import {@@ -33,7 +32,7 @@ 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);+const DEFAULT_BATCH_SIZE = 5000;const SESSION_BATCH_SIZE = 5000;const PROFILE_BATCH_SIZE = 5000;// Profiles derived inline from events (Amplitude, which has no profile export@@ -43,12 +42,6 @@ const PROFILE_BATCH_SIZE = 5000;const PROFILE_MAP_CAP = 50_000;const RESUMABLE_STEPS = ['creating_sessions', 'moving', 'backfilling_sessions'];-let _logger: ILogger | undefined;-function getLogger(): ILogger {- _logger ??= createLogger({ name: 'core:import' });- return _logger;-}-function yieldToEventLoop(): Promise<void> {return new Promise((resolve) => {setTimeout(resolve, 100);@@ -211,7 +204,10 @@ export async function generateGapBasedSessionIds(await insertImportBatch(deps, batch, importId);}- const mutationTable = getReplicatedTableName(TABLE_NAMES.events_imports);+ const mutationTable = getReplicatedTableName(+ deps.config.clickhouseClustered,+ TABLE_NAMES.events_imports+ );await ch.command({query: `ALTER TABLE ${mutationTable} DELETEWHERE import_id = {importId:String}@@ -236,7 +232,10 @@ export async function cleanupStagingData(importId: string): Promise<void> {const ch = deps.ch;- const mutationTableName = getReplicatedTableName(TABLE_NAMES.events_imports);+ const mutationTableName = getReplicatedTableName(+ deps.config.clickhouseClustered,+ TABLE_NAMES.events_imports+ );await ch.command({query: `ALTER TABLE ${mutationTableName} DELETE WHERE import_id = {importId:String}`,query_params: { importId },@@ -254,7 +253,10 @@ export async function cleanupSessionStartEndEvents(importId: string): Promise<void> {const ch = deps.ch;- const mutationTableName = getReplicatedTableName(TABLE_NAMES.events_imports);+ const mutationTableName = getReplicatedTableName(+ deps.config.clickhouseClustered,+ TABLE_NAMES.events_imports+ );await ch.command({query: `ALTER TABLE ${mutationTableName} DELETE WHERE import_id = {importId:String} AND name IN ('session_start', 'session_end')`,query_params: { importId },@@ -895,9 +897,10 @@ export async function runImportJob(deps: ServiceDeps,importId: string,progress: ImportJobProgress = NOOP_PROGRESS,- logger: Logger = getLogger()+ logger: Logger = deps.logger): Promise<{ success: true }> {const db = deps.db;+ const batchSize = deps.config.query.importBatchSize ?? DEFAULT_BATCH_SIZE;const record = await db.import.findUniqueOrThrow({where: { id: importId },include: { project: true },@@ -982,7 +985,7 @@ export async function runImportJob(}}- if (eventBatch.length >= BATCH_SIZE) {+ if (eventBatch.length >= batchSize) {await insertImportBatch(deps, eventBatch, importId);processedEvents += eventBatch.length;@@ -1204,7 +1207,7 @@ export async function insertRawEventsBatch(deps: ServiceDeps,projectId: string,events: IClickhouseEvent[],- logger: Logger = getLogger()+ logger: Logger = deps.logger): Promise<InsertRawEventsResult> {const ch = deps.ch;const importedAt = formatClickhouseDate(new Date());diff --git a/packages/core/src/modules/ingest/ingest.routes.ts b/packages/core/src/modules/ingest/ingest.routes.tsindex 3b108c804..2feba5e54 100644--- a/packages/core/src/modules/ingest/ingest.routes.ts+++ b/packages/core/src/modules/ingest/ingest.routes.ts@@ -178,7 +178,8 @@ export const ingestRoutes = defineRoutes((app, deps: AppDeps) =>},{buffers: ctx.buffers,- produceIncomingEvent,+ produceIncomingEvent: (payload, partitionKey) =>+ produceIncomingEvent(ctx.config, payload, partitionKey),deps: ctx,});@@ -249,7 +250,8 @@ export const ingestRoutes = defineRoutes((app, deps: AppDeps) =>},{buffers: ctx.buffers,- produceIncomingEvent,+ produceIncomingEvent: (payload, partitionKey) =>+ produceIncomingEvent(ctx.config, payload, partitionKey),deps: ctx,});diff --git a/packages/core/src/modules/ingest/ingest.service.test.ts b/packages/core/src/modules/ingest/ingest.service.test.tsindex 62c97d709..2ff3f1d29 100644--- a/packages/core/src/modules/ingest/ingest.service.test.ts+++ b/packages/core/src/modules/ingest/ingest.service.test.ts@@ -24,6 +24,7 @@ import {it,mock,} from 'bun:test';+import { testCoreConfig } from '../../../test/config-fixture';import type { Buffers } from '../../buffers/create-buffers';import type { GeoLocation } from '../../clients/geo';import type { IReplayPayload, ITrackHandlerPayload } from './ingest.constants';@@ -193,7 +194,7 @@ const transport = (group: { add: mock(async () => undefined) },},produceIncomingEvent,- deps: { buffers: {} },+ deps: { buffers: {}, config: testCoreConfig() },}) as unknown as Parameters<typeof ingestTrack>[1];const request = (body: ITrackHandlerPayload) => ({diff --git a/packages/core/src/modules/ingest/ingest.service.ts b/packages/core/src/modules/ingest/ingest.service.tsindex 2f2ab2cb5..325eab43d 100644--- a/packages/core/src/modules/ingest/ingest.service.ts+++ b/packages/core/src/modules/ingest/ingest.service.ts@@ -16,7 +16,7 @@import { assocPath, pathOr, pick } from 'ramda';import { v4 as uuid } from 'uuid';import type { Buffers } from '../../buffers/create-buffers';-import { SESSION_TIMEOUT_MS } from '../../buffers/session-buffer';+import { resolveSessionTimeoutMs } from '../../buffers/session-buffer';import {type AsnInfo,type GeoLocation,@@ -92,6 +92,10 @@ export interface IngestTransport {deps: ServiceDeps;}+/** What the two context builders below need: the Postgres client the salt+ * cache keys on, and the idle window every session decision reads. */+type IngestScope = DbScope & Pick<ServiceDeps, 'config'>;+const QUEUE_PAYLOAD_HEADERS = ['user-agent','openpanel-sdk-name',@@ -228,10 +232,11 @@ export type TrackOutcome =| { status: 'profile-property-not-a-number' };async function buildContext(- deps: DbScope,+ deps: IngestScope,request: TrackRequest,buffers: IngestBuffers): Promise<TrackContext> {+ const sessionTimeoutMs = resolveSessionTimeoutMs(deps.config);const projectId = request.projectId as string;const timestamp = getTimestamp(request.timestamp, request.body.payload);const ip =@@ -267,6 +272,7 @@ async function buildContext(overrideDeviceId,eventTimeMs: timestamp.timestamp,sessionBuffer: buffers.session,+ sessionTimeoutMs,});return {@@ -658,6 +664,7 @@ export async function ingestLegacyEvent(ua,salts,sessionBuffer: transport.buffers.session,+ sessionTimeoutMs: resolveSessionTimeoutMs(transport.deps.config),});const uaInfo = parseUserAgent(ua, request.body?.properties);@@ -706,7 +713,7 @@ export type DeviceIdentity =/** `GET /track/device-id`. */export async function fetchDeviceIdentity(- deps: DbScope,+ deps: IngestScope,request: {projectId: string | null | undefined;clientIp: string | undefined;@@ -716,6 +723,7 @@ export async function fetchDeviceIdentity(logger: Logger): Promise<DeviceIdentity> {const salts = await getSalts(deps);+ const sessionTimeoutMs = resolveSessionTimeoutMs(deps.config);const projectId = request.projectId;if (!projectId) {return { status: 'missing-project-id' };@@ -761,7 +769,7 @@ export async function fetchDeviceIdentity(// server to start a fresh session id on the next event.const now = Date.now();- if (current && isLiveSession(current, now)) {+ if (current && isLiveSession(current, now, sessionTimeoutMs)) {return {status: 'ok',deviceId: currentDeviceId,@@ -770,7 +778,7 @@ export async function fetchDeviceIdentity(};}- if (previous && isLiveSession(previous, now)) {+ if (previous && isLiveSession(previous, now, sessionTimeoutMs)) {return {status: 'ok',deviceId: previousDeviceId,@@ -895,10 +903,14 @@ function getBotEventPath(body: unknown): string | undefined {return undefined;}-function isLiveSession(session: { ended_at: string }, now: number): boolean {+function isLiveSession(+ session: { ended_at: string },+ now: number,+ sessionTimeoutMs: number+): boolean {return (now - convertClickhouseDateToJs(session.ended_at).getTime() <- SESSION_TIMEOUT_MS+ sessionTimeoutMs);}diff --git a/packages/core/src/modules/ingest/legacy-event.test.ts b/packages/core/src/modules/ingest/legacy-event.test.tsindex 054f3ee3d..cc9220a1d 100644--- a/packages/core/src/modules/ingest/legacy-event.test.ts+++ b/packages/core/src/modules/ingest/legacy-event.test.ts@@ -22,6 +22,7 @@ import {it,mock,} from 'bun:test';+import { testCoreConfig } from '../../../test/config-fixture';import type { Buffers } from '../../buffers/create-buffers';import type { GeoLocation } from '../../clients/geo';import type { ServiceDeps } from '../../services';@@ -96,7 +97,10 @@ const buffers = {// The legacy route produces to Kafka and never touches Postgres/ClickHouse;// `deps` is on `IngestTransport` for the profile writes /track makes (M10-005).-const transportDeps = { buffers } as unknown as ServiceDeps;+const transportDeps = {+ buffers,+ config: testCoreConfig(),+} as unknown as ServiceDeps;const legacyBody = (overrides: Partial<DeprecatedPostEventPayload> = {}diff --git a/packages/core/src/modules/ingest/src/consumer.ts b/packages/core/src/modules/ingest/src/consumer.tsindex a2fa53277..cf7225abc 100644--- a/packages/core/src/modules/ingest/src/consumer.ts+++ b/packages/core/src/modules/ingest/src/consumer.ts@@ -321,7 +321,7 @@ export function createEventsBatchHandler(/** The broker-facing half, supplied by whoever owns the kafkajs client. */export interface EventsConsumerDeps {/** Already configured with the group id, timeouts and fetch bounds. */- createConsumer: () => Consumer;+ createConsumer: (options?: { groupId?: string }) => Consumer;/** Lifecycle / rebalance visibility. */logger: ConsumerLogger;/** The "consumer running" line, on the kafka-scoped logger. */diff --git a/packages/core/src/modules/ingest/src/device-id.test.ts b/packages/core/src/modules/ingest/src/device-id.test.tsindex 26d57db95..33151f3eb 100644--- a/packages/core/src/modules/ingest/src/device-id.test.ts+++ b/packages/core/src/modules/ingest/src/device-id.test.ts@@ -18,12 +18,16 @@ import { getDeviceId, type SessionBufferReader } from './device-id';const NOW = new Date('2026-06-08T12:00:00.000Z').getTime();const MINUTE = 60 * 1000;const SALTS = { current: 'salt-current', previous: 'salt-previous' };+/** SESSION_TIMEOUT_MS's default: the caller resolves it from config now. */+const SESSION_TIMEOUT_MS = 30 * 60 * 1000;+const BASE = {projectId: 'proj-1',ip: '1.2.3.4',ua: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/148.0.0.0',salts: SALTS,eventTimeMs: NOW,+ sessionTimeoutMs: SESSION_TIMEOUT_MS,};// withinIdleWindow only reads `id` + `ended_at`; the rest is irrelevant here.diff --git a/packages/core/src/modules/ingest/src/device-id.ts b/packages/core/src/modules/ingest/src/device-id.tsindex 2ce76937f..04b8a84e8 100644--- a/packages/core/src/modules/ingest/src/device-id.ts+++ b/packages/core/src/modules/ingest/src/device-id.ts@@ -4,7 +4,6 @@import crypto from 'node:crypto';import type { SessionBuffer } from '../../../buffers/session-buffer';-import { SESSION_TIMEOUT_MS } from '../../../buffers/session-buffer';import { generateDeviceId } from '../../../shared/profileId';import type { IClickhouseSession } from '../../session/session.service';import { convertClickhouseDateToJs } from '../../session/src/dates';@@ -36,6 +35,7 @@ export async function getDeviceId({overrideDeviceId,eventTimeMs,sessionBuffer,+ sessionTimeoutMs,}: {projectId: string;ip: string;@@ -46,6 +46,8 @@ export async function getDeviceId({* still within its idle window. Defaults to `Date.now()`. */eventTimeMs?: number;sessionBuffer: SessionBufferReader;+ /** The idle window, resolved from config by the caller. */+ sessionTimeoutMs: number;}): Promise<DeviceIdResult> {if (overrideDeviceId) {// A caller-supplied device id is stable (no salt rotation), so it's the only@@ -55,6 +57,7 @@ export async function getDeviceId({deviceIds: [overrideDeviceId],eventTimeMs: eventTimeMs ?? Date.now(),sessionBuffer,+ sessionTimeoutMs,});}@@ -80,6 +83,7 @@ export async function getDeviceId({deviceIds: [currentDeviceId, previousDeviceId],eventTimeMs: eventTimeMs ?? Date.now(),sessionBuffer,+ sessionTimeoutMs,});}@@ -95,10 +99,11 @@ export async function getDeviceId({*/function withinIdleWindow(session: IClickhouseSession,- eventTimeMs: number+ eventTimeMs: number,+ sessionTimeoutMs: number): boolean {const lastEventMs = convertClickhouseDateToJs(session.ended_at).getTime();- return eventTimeMs - lastEventMs < SESSION_TIMEOUT_MS;+ return eventTimeMs - lastEventMs < sessionTimeoutMs;}async function getInfoFromSession({@@ -106,6 +111,7 @@ async function getInfoFromSession({deviceIds,eventTimeMs,sessionBuffer,+ sessionTimeoutMs,}: {projectId: string;/** Candidate device ids in priority order (e.g. [current, previous] salt@@ -113,6 +119,7 @@ async function getInfoFromSession({deviceIds: string[];eventTimeMs: number;sessionBuffer: SessionBufferReader;+ sessionTimeoutMs: number;}): Promise<DeviceIdResult> {const candidates = [...new Set(deviceIds.filter(Boolean))];const primary = candidates[0] ?? '';@@ -128,7 +135,7 @@ async function getInfoFromSession({);for (const [index, session] of sessions.entries()) {- if (session && withinIdleWindow(session, eventTimeMs)) {+ if (session && withinIdleWindow(session, eventTimeMs, sessionTimeoutMs)) {return { deviceId: candidates[index]!, sessionId: session.id };}}@@ -144,7 +151,7 @@ async function getInfoFromSession({//// The bucket window MUST track the idle timeout: a gap > the window has to// land in a new bucket so a boundary mints a *fresh* id. If it didn't (e.g.- // a hardcoded 30min while SESSION_TIMEOUT_MS is shorter), the worker would+ // a hardcoded 30min while the idle timeout is shorter), the worker would// reopen the just-closed id and its session_end would be skipped. Grace must// stay < window or getSessionId throws.sessionId: getSessionId({@@ -153,9 +160,9 @@ async function getInfoFromSession({eventMs: eventTimeMs,graceMs: Math.min(SESSION_GRACE_CEILING_MS,- Math.floor(SESSION_TIMEOUT_MS / SESSION_GRACE_WINDOW_DIVISOR)+ Math.floor(sessionTimeoutMs / SESSION_GRACE_WINDOW_DIVISOR)),- windowMs: SESSION_TIMEOUT_MS,+ windowMs: sessionTimeoutMs,}),};}diff --git a/packages/core/src/modules/ingest/src/kafka.ts b/packages/core/src/modules/ingest/src/kafka.tsindex dae90966d..4fd0a51a5 100644--- a/packages/core/src/modules/ingest/src/kafka.ts+++ b/packages/core/src/modules/ingest/src/kafka.ts@@ -19,7 +19,8 @@ import {logLevel,type Producer,} from 'kafkajs';-import { createLogger } from '../../../clients/logger';+import { createLogger, type ILogger } from '../../../clients/logger';+import type { CoreConfig, KafkaConfig } from '../../../config';// One definition, not two: the consumer already declares the dead-letter// message shape it hands to this producer, and both files are now siblings.import type { DeadLetterMessage } from './consumer';@@ -27,106 +28,24 @@ import type { IncomingEventPayload } from './incoming-event';export type { Admin, EachBatchPayload, KafkaMessage } from 'kafkajs';-export const kafkaLogger = createLogger({ name: 'kafka' });--const parseBrokers = (raw: string | undefined): string[] => {- if (!raw) {- return [];- }- return raw- .split(',')- .map((b) => b.trim())- .filter(Boolean);-};--export const KAFKA_BROKERS = parseBrokers(process.env.KAFKA_BROKERS);-export const KAFKA_EVENTS_TOPIC = process.env.KAFKA_EVENTS_TOPIC || 'events';-// Dead-letter topic for messages the consumer could not handle (ADR-004-// delivery semantics). Kept on the same broker so a poison message is retained-// and countable instead of dropped.-export const KAFKA_EVENTS_DLQ_TOPIC =- process.env.KAFKA_EVENTS_DLQ_TOPIC || `${KAFKA_EVENTS_TOPIC}-dlq`;-export const KAFKA_CONSUMER_GROUP =- process.env.KAFKA_CONSUMER_GROUP || 'openpanel-events';-export const KAFKA_PARTITIONS_CONCURRENT = Number.parseInt(- process.env.KAFKA_PARTITIONS_CONCURRENT || '8',- 10-);-// Approx size of one event payload (observed range ~0.9–1.3 KiB).// We size fetch knobs in messages and convert to bytes via this constant.const KAFKA_BYTES_PER_MESSAGE = 1024;-export const KAFKA_MIN_MESSAGES = Number.parseInt(- process.env.KAFKA_MIN_MESSAGES || '1',- 10-);-export const KAFKA_MAX_WAIT_MS = Number.parseInt(- process.env.KAFKA_MAX_WAIT_MS || '500',- 10-);-export const KAFKA_MAX_MESSAGES_PER_PARTITION = Number.parseInt(- process.env.KAFKA_MAX_MESSAGES_PER_PARTITION || '256',- 10-);-export const KAFKA_SESSION_TIMEOUT_MS = Number.parseInt(- process.env.KAFKA_SESSION_TIMEOUT_MS || '30000',- 10-);-export const KAFKA_HEARTBEAT_INTERVAL_MS = Number.parseInt(- process.env.KAFKA_HEARTBEAT_INTERVAL_MS || '3000',- 10-);--// Producer fail-fast knobs. Defaults give a worst-case total of a few seconds-// instead of kafkajs's stock ~150s, so a broker outage doesn't park HTTP-// requests on the track path long enough to saturate the LB.-export const KAFKA_REQUEST_TIMEOUT_MS = Number.parseInt(- process.env.KAFKA_REQUEST_TIMEOUT_MS || '5000',- 10-);-export const KAFKA_CONNECTION_TIMEOUT_MS = Number.parseInt(- process.env.KAFKA_CONNECTION_TIMEOUT_MS || '2000',- 10-);-export const KAFKA_PRODUCER_RETRIES = Number.parseInt(- process.env.KAFKA_PRODUCER_RETRIES || '2',- 10-);-export const KAFKA_PRODUCER_INITIAL_RETRY_MS = Number.parseInt(- process.env.KAFKA_PRODUCER_INITIAL_RETRY_MS || '100',- 10-);-export const KAFKA_PRODUCER_MAX_RETRY_MS = Number.parseInt(- process.env.KAFKA_PRODUCER_MAX_RETRY_MS || '1000',- 10-);--// In-consumer retry for handler exceptions (ADR-004: at-least-once). Bounded-// so the worst case stays far inside KAFKA_SESSION_TIMEOUT_MS — a batch that-// out-waits the session timeout is a rebalance, which is worse than a DLQ.-export const KAFKA_HANDLER_MAX_ATTEMPTS = Number.parseInt(- process.env.KAFKA_HANDLER_MAX_ATTEMPTS || '3',- 10-);-export const KAFKA_HANDLER_RETRY_INITIAL_MS = Number.parseInt(- process.env.KAFKA_HANDLER_RETRY_INITIAL_MS || '100',- 10-);-export const KAFKA_HANDLER_RETRY_MAX_MS = Number.parseInt(- process.env.KAFKA_HANDLER_RETRY_MAX_MS || '1000',- 10-);--const KAFKA_MIN_BYTES = KAFKA_MIN_MESSAGES * KAFKA_BYTES_PER_MESSAGE;-const KAFKA_MAX_BYTES_PER_PARTITION =- KAFKA_MAX_MESSAGES_PER_PARTITION * KAFKA_BYTES_PER_MESSAGE;+// One logger and one client per process, built on first use from the config+// the caller was handed. `loadConfig` runs once at boot, so the memo cannot+// serve one caller another caller's brokers.+let kafkaLoggerInstance: ILogger | null = null;+export const kafkaLogger = (config: CoreConfig): ILogger => {+ kafkaLoggerInstance ??= createLogger({ name: 'kafka', config });+ return kafkaLoggerInstance;+};// Kafka/Redpanda is the sole events transport (ADR-004): there is no fallback,// so an unset KAFKA_BROKERS must fail loudly at boot rather than quietly at the// first event.-export const assertKafkaConfigured = (): void => {- if (KAFKA_BROKERS.length === 0) {+export const assertKafkaConfigured = (config: KafkaConfig): void => {+ if (config.brokers.length === 0) {throw new Error('KAFKA_BROKERS is not set. Kafka/Redpanda is the only events transport — set KAFKA_BROKERS to a comma-separated broker list.');@@ -134,15 +53,15 @@ export const assertKafkaConfigured = (): void => {};let kafka: Kafka | null = null;-const getKafka = (): Kafka => {- assertKafkaConfigured();+const getKafka = (config: CoreConfig): Kafka => {+ assertKafkaConfigured(config.kafka);if (!kafka) {kafka = new Kafka({- clientId: process.env.KAFKA_CLIENT_ID || 'openpanel',- brokers: KAFKA_BROKERS,+ clientId: config.kafka.clientId,+ brokers: config.kafka.brokers,logLevel: logLevel.WARN,- requestTimeout: KAFKA_REQUEST_TIMEOUT_MS,- connectionTimeout: KAFKA_CONNECTION_TIMEOUT_MS,+ requestTimeout: config.kafka.requestTimeoutMs,+ connectionTimeout: config.kafka.connectionTimeoutMs,});}return kafka;@@ -151,12 +70,12 @@ const getKafka = (): Kafka => {let producer: Producer | null = null;let producerConnectPromise: Promise<Producer> | null = null;-const getProducer = async (): Promise<Producer> => {+const getProducer = async (config: CoreConfig): Promise<Producer> => {if (producer) {return producer;}if (!producerConnectPromise) {- const client = getKafka();+ const client = getKafka(config);const p = client.producer({idempotent: true,// 1 (not 5) to avoid in-flight reordering after a transient broker hiccup:@@ -165,9 +84,9 @@ const getProducer = async (): Promise<Producer> => {maxInFlightRequests: 1,allowAutoTopicCreation: true,retry: {- retries: KAFKA_PRODUCER_RETRIES,- initialRetryTime: KAFKA_PRODUCER_INITIAL_RETRY_MS,- maxRetryTime: KAFKA_PRODUCER_MAX_RETRY_MS,+ retries: config.kafka.producerRetries,+ initialRetryTime: config.kafka.producerInitialRetryMs,+ maxRetryTime: config.kafka.producerMaxRetryMs,factor: 2,},});@@ -175,8 +94,11 @@ const getProducer = async (): Promise<Producer> => {.connect().then(() => {producer = p;- kafkaLogger.info(- { brokers: KAFKA_BROKERS, topic: KAFKA_EVENTS_TOPIC },+ kafkaLogger(config).info(+ {+ brokers: config.kafka.brokers,+ topic: config.kafka.eventsTopic,+ },'kafka producer connected');return p;@@ -216,14 +138,14 @@ const isFatalProducerError = (err: unknown): boolean => {return false;};-const resetProducer = (broken: Producer): void => {+const resetProducer = (config: CoreConfig, broken: Producer): void => {if (producer !== broken) {return;}producer = null;producerConnectPromise = null;broken.disconnect().catch((err) => {- kafkaLogger.warn(+ kafkaLogger(config).warn({ err },'kafka producer disconnect after fatal error failed');@@ -236,31 +158,36 @@ interface OutgoingMessage {headers?: IHeaders;}-const send = async (topic: string, message: OutgoingMessage): Promise<void> => {- const p = await getProducer();+const send = async (+ config: CoreConfig,+ topic: string,+ message: OutgoingMessage+): Promise<void> => {+ const p = await getProducer(config);try {await p.send({topic,- timeout: KAFKA_REQUEST_TIMEOUT_MS,+ timeout: config.kafka.requestTimeoutMs,messages: [message],});} catch (err) {if (isFatalProducerError(err)) {- kafkaLogger.warn(+ kafkaLogger(config).warn({ err },'kafka producer in fatal state; resetting for next call');- resetProducer(p);+ resetProducer(config, p);}throw err;}};export const produceIncomingEvent = async (+ config: CoreConfig,payload: IncomingEventPayload,partitionKey: string): Promise<void> =>- send(KAFKA_EVENTS_TOPIC, {+ send(config, config.kafka.eventsTopic, {key: Buffer.from(partitionKey),value: Buffer.from(JSON.stringify(payload)),});@@ -269,9 +196,10 @@ export const produceIncomingEvent = async (// value stays the producer's original bytes, so a DLQ message can be replayed// onto the events topic unchanged.export const produceDeadLetterEvent = async (+ config: CoreConfig,message: DeadLetterMessage): Promise<void> =>- send(KAFKA_EVENTS_DLQ_TOPIC, {+ send(config, config.kafka.eventsDlqTopic, {key: message.key,value: message.value,headers: {@@ -287,17 +215,21 @@ export const produceDeadLetterEvent = async (const consumers = new Set<Consumer>();-export const createKafkaEventsConsumer = (options?: {- groupId?: string;-}): Consumer => {- const client = getKafka();+export const createKafkaEventsConsumer = (+ config: CoreConfig,+ options?: {+ groupId?: string;+ }+): Consumer => {+ const client = getKafka(config);const consumer = client.consumer({- groupId: options?.groupId || KAFKA_CONSUMER_GROUP,- sessionTimeout: KAFKA_SESSION_TIMEOUT_MS,- heartbeatInterval: KAFKA_HEARTBEAT_INTERVAL_MS,- minBytes: KAFKA_MIN_BYTES,- maxWaitTimeInMs: KAFKA_MAX_WAIT_MS,- maxBytesPerPartition: KAFKA_MAX_BYTES_PER_PARTITION,+ groupId: options?.groupId || config.kafka.consumerGroup,+ sessionTimeout: config.kafka.sessionTimeoutMs,+ heartbeatInterval: config.kafka.heartbeatIntervalMs,+ minBytes: config.kafka.minMessages * KAFKA_BYTES_PER_MESSAGE,+ maxWaitTimeInMs: config.kafka.maxWaitMs,+ maxBytesPerPartition:+ config.kafka.maxMessagesPerPartition * KAFKA_BYTES_PER_MESSAGE,});consumers.add(consumer);return consumer;@@ -322,12 +254,13 @@ export interface ConsumerGroupLag {partitions: PartitionLag[];}-export const createKafkaAdmin = (): Admin => getKafka().admin();+export const createKafkaAdmin = (config: CoreConfig): Admin =>+ getKafka(config).admin();export const sampleConsumerGroupLag = async (admin: Admin,- topic: string = KAFKA_EVENTS_TOPIC,- groupId: string = KAFKA_CONSUMER_GROUP+ topic: string,+ groupId: string): Promise<ConsumerGroupLag> => {const [endOffsets, committedByTopic] = await Promise.all([admin.fetchTopicOffsets(topic),@@ -362,12 +295,12 @@ export const sampleConsumerGroupLag = async (};};-export const disconnectKafka = async (): Promise<void> => {+export const disconnectKafka = async (config: CoreConfig): Promise<void> => {const tasks: Promise<unknown>[] = [];for (const c of consumers) {tasks.push(c.disconnect().catch((err) => {- kafkaLogger.error({ err }, 'kafka consumer disconnect error');+ kafkaLogger(config).error({ err }, 'kafka consumer disconnect error');}));}@@ -378,7 +311,7 @@ export const disconnectKafka = async (): Promise<void> => {producerConnectPromise = null;tasks.push(p.disconnect().catch((err) => {- kafkaLogger.error({ err }, 'kafka producer disconnect error');+ kafkaLogger(config).error({ err }, 'kafka producer disconnect error');}));}diff --git a/packages/core/src/modules/insight/insight.jobs.test.ts b/packages/core/src/modules/insight/insight.jobs.test.tsindex abb224a23..06a341f8d 100644--- a/packages/core/src/modules/insight/insight.jobs.test.ts+++ b/packages/core/src/modules/insight/insight.jobs.test.ts@@ -2,6 +2,7 @@// methods themselves are exercised in insight.service.test.ts.import { expect, test } from 'bun:test';+import { testCoreConfig } from '../../../test/config-fixture';import type { AppDeps, Buffers, JobCtx } from '../../context';import { createRecordingProducers } from '../../jobs/testing';import { queues } from '../../jobs.registry';@@ -78,6 +79,7 @@ function stubJobCtx(buffers: {} as Buffers,logger: stubLogger(),queues: createRecordingProducers(queues).queues,+ config: testCoreConfig(),services,requestId: 'req_1',job: { id: 'job_1', attempt: 0, queue: 'cron', name: jobName },@@ -138,6 +140,7 @@ test('insightsDaily fans out one insightsProject enqueue per candidate, jobId debuffers: {} as Buffers,logger: stubLogger(),queues: producers.queues,+ config: testCoreConfig(),services: {auth: {} as Services['auth'],notification: {} as Services['notification'],diff --git a/packages/core/src/modules/insight/insight.service.test.ts b/packages/core/src/modules/insight/insight.service.test.tsindex 0b45f6c52..aeea7f377 100644--- a/packages/core/src/modules/insight/insight.service.test.ts+++ b/packages/core/src/modules/insight/insight.service.test.ts@@ -5,6 +5,7 @@// registered before the subject's first call, not before its import.import { beforeAll, expect, mock, test } from 'bun:test';+import { testCoreConfig } from '../../../test/config-fixture';import { testServices } from '../../../test/service-deps';import type { AppDeps, Buffers } from '../../context';@@ -27,6 +28,7 @@ const $executeRaw = mock((..._args: unknown[]) => {// $executeRaw above), so `deps.ch` stays unbuilt.const deps = {db: { $executeRaw },+ config: testCoreConfig(),} as unknown as import('../../services').ServiceDeps;const spikesQuery = mock(async () => [@@ -144,12 +146,20 @@ test('explainInsight is a cache-aside over the AI call, keyed by the caller-suppreferences: [],};- const first = await subject.explainInsight(input, 'insight-explain:i1:100');+ const first = await subject.explainInsight(+ deps,+ input,+ 'insight-explain:i1:100'+ );expect(generateInsightExplanation).toHaveBeenCalledTimes(1);expect(first?.summary).toBe('x');// Second call with the same key is served from the cache, not the LLM.- const second = await subject.explainInsight(input, 'insight-explain:i1:100');+ const second = await subject.explainInsight(+ deps,+ input,+ 'insight-explain:i1:100'+ );expect(generateInsightExplanation).toHaveBeenCalledTimes(1);expect(second).toEqual(first);});@@ -175,6 +185,7 @@ test('createInsightService binds every InsightService method', () => {logger,queues:undefined as unknown as import('../../jobs.registry').QueueProducers,+ config: testCoreConfig(),},testServices());diff --git a/packages/core/src/modules/insight/insight.service.ts b/packages/core/src/modules/insight/insight.service.tsindex 8f33114a8..f382aa0a7 100644--- a/packages/core/src/modules/insight/insight.service.ts+++ b/packages/core/src/modules/insight/insight.service.ts@@ -54,6 +54,8 @@ const ENRICH_BATCH_SIZE = 25;const CLEANUP_BATCH_SIZE = 5000;const CLEANUP_MAX_BATCHES = 2000; // runaway backstop: 5000 * 2000 = 10M rowsconst DEFAULT_INSIGHTS_RETENTION_DAYS = 90;+/** Where the weekly-digest link points when DASHBOARD_URL is not set. */+const DEFAULT_DASHBOARD_URL = 'https://dashboard.openpanel.dev';// Weekly digest: quiet threshold + how many email-worthy insights to surface.const WEEKLY_DIGEST_MIN_EVENTS = 100;@@ -185,7 +187,7 @@ async function enrichProjectInsights(let results: Awaited<ReturnType<typeof enrichInsights>>;try {- results = await enrichInsights(input);+ results = await enrichInsights(deps.config, input);} catch (err) {logger.error({ err, projectId, batchSize: batch.length },@@ -293,11 +295,8 @@ export async function cleanupStaleInsights(logger: Logger): Promise<{ insights: number; events: number }> {const db = deps.db;- const retentionDays = Number.parseInt(- process.env.INSIGHTS_RETENTION_DAYS ||- String(DEFAULT_INSIGHTS_RETENTION_DAYS),- 10- );+ const retentionDays =+ deps.config.query.insightsRetentionDays ?? DEFAULT_INSIGHTS_RETENTION_DAYS;const cutoff = new Date(Date.now() - retentionDays * 24 * 60 * 60 * 1000);const suppressed = await deleteInBatches(@@ -464,7 +463,7 @@ async function buildDigestData(let narrative = '';try {- narrative = await generateWeeklyNarrative({+ narrative = await generateWeeklyNarrative(deps.config, {projectName: project.name,dateRange,stats: [@@ -496,7 +495,7 @@ async function buildDigestData(// Narrative is best-effort — the digest still sends without it.}- const dashboardUrl = `${process.env.DASHBOARD_URL ?? 'https://dashboard.openpanel.dev'}/${project.organizationId}/${project.id}`;+ const dashboardUrl = `${deps.config.dashboardUrl || DEFAULT_DASHBOARD_URL}/${project.organizationId}/${project.id}`;return {data: {@@ -652,6 +651,7 @@ export async function listAllInsights(* the LLM.*/export async function explainInsight(+ deps: ServiceDeps,input: ExplainInsightInput,cacheKey: string): Promise<InsightExplanation | null> {@@ -660,7 +660,7 @@ export async function explainInsight(return JSON.parse(cached) as InsightExplanation;}- const explanation = await generateInsightExplanation(input);+ const explanation = await generateInsightExplanation(deps.config, input);// Only cache a successful explanation — a null is a transient LLM failure// and should be retried on the next click.@@ -737,7 +737,11 @@ export function createInsightService(projectId: string;limit: number;}): ReturnType<typeof listAllInsights> => listAllInsights(deps, args),- explainInsight,+ explainInsight: (+ input: ExplainInsightInput,+ cacheKey: string+ ): Promise<InsightExplanation | null> =>+ explainInsight(deps, input, cacheKey),getReferrerSpikes: (input: GetReferrerSpikesInput): Promise<ReferrerSpikeCluster[]> => getReferrerSpikes(deps, input),diff --git a/packages/core/src/modules/integration/integration.jobs.ts b/packages/core/src/modules/integration/integration.jobs.tsindex b3207e38c..fd6a56043 100644--- a/packages/core/src/modules/integration/integration.jobs.ts+++ b/packages/core/src/modules/integration/integration.jobs.ts@@ -34,8 +34,12 @@ export const integrationCronJobs = {db: ctx.db as unknown as ExportDb,ch: ctx.ch as unknown as ExportClickhouse,logger: ctx.logger.child({ job: 'flush-exports' }),+ config: ctx.config,createAdapter: (config) =>- getServerIntegration(config.type).export?.createAdapter(config),+ getServerIntegration(config.type).export?.createAdapter(+ config,+ ctx.config+ ),});},}),diff --git a/packages/core/src/modules/integration/integration.routes.ts b/packages/core/src/modules/integration/integration.routes.tsindex 42d9a1d20..15a6338ee 100644--- a/packages/core/src/modules/integration/integration.routes.ts+++ b/packages/core/src/modules/integration/integration.routes.ts@@ -22,14 +22,11 @@ const slackCallbackQuery = z.object({state: z.string(),});-function dashboardUrl(): string {- return (- process.env.DASHBOARD_URL || process.env.NEXT_PUBLIC_DASHBOARD_URL || ''- );-}+/** Only used to make the URL parseable when DASHBOARD_URL is not set. */+const FALLBACK_ORIGIN = 'http://localhost';-function loginErrorRedirect(message: string) {- const url = new URL('/login', dashboardUrl() || 'http://localhost');+function loginErrorRedirect(dashboardUrl: string, message: string) {+ const url = new URL('/login', dashboardUrl || FALLBACK_ORIGIN);url.searchParams.set('error', message);return redirect(url.toString());}@@ -51,14 +48,14 @@ export const integrationRoutes = defineRoutes((app) =>// than a now-404 integrations URL.return redirect(projectId- ? `${dashboardUrl()}/${organizationId}/${projectId}/integrations/installed`- : `${dashboardUrl()}/${organizationId}`+ ? `${ctx.config.dashboardUrl}/${organizationId}/${projectId}/integrations/installed`+ : `${ctx.config.dashboardUrl}/${organizationId}`);} catch (error) {ctx.logger.error({ err: error }, 'Slack OAuth callback error');const message =error instanceof Error ? error.message : 'An error occurred';- return loginErrorRedirect(message);+ return loginErrorRedirect(ctx.config.dashboardUrl, message);}},{ query: slackCallbackQuery, detail: { hide: true } }diff --git a/packages/core/src/modules/integration/integration.service.ts b/packages/core/src/modules/integration/integration.service.tsindex f6ea35ac2..be604e8f4 100644--- a/packages/core/src/modules/integration/integration.service.ts+++ b/packages/core/src/modules/integration/integration.service.ts@@ -237,12 +237,12 @@ export async function upsertIntegration(// Test the connection with the real credentials before saving. `submitted`// may carry a still-encrypted value forward from the stored row; the adapters// decrypt on construction, so this works for both new and carried-over keys.- const testResult = await plugin.testConnection?.(submitted);+ const testResult = await plugin.testConnection?.(submitted, deps.config);if (testResult && !testResult.success) {throw new TRPCBadRequestError(`Failed to connect: ${testResult.error}`);}- const config = encryptConfigSecrets(submitted);+ const config = encryptConfigSecrets(deps.config.encryptionKey, submitted);if (input.id) {return db.integration.update({@@ -318,6 +318,7 @@ export async function createOrUpdateSlackIntegration(return {...res,slackInstallUrl: await getSlackInstallUrl({+ config: deps.config,integrationId: res.id,organizationId,projectId,@@ -344,7 +345,8 @@ export async function testIntegrationConnection(return ((await getServerIntegration(input.config.type).testConnection?.(- input.config+ input.config,+ deps.config)) ?? { success: true });}@@ -365,7 +367,8 @@ export async function testExportIntegrationConnection(return ((await getServerIntegration(input.config.type).testConnection?.(- input.config+ input.config,+ deps.config)) ?? { success: false, error: 'Unknown export type' });}@@ -416,10 +419,9 @@ export async function completeSlackOAuthCallback(state: string;}): Promise<CompleteSlackOAuthCallbackResult> {- const verifiedState = await slackInstaller.stateStore?.verifyStateParam(- new Date(),- params.state- );+ const verifiedState = await slackInstaller(+ deps.config+ ).stateStore?.verifyStateParam(new Date(), params.state);const parsedMetadata = slackOAuthMetadataSchema.safeParse(JSON.parse(verifiedState?.metadata ?? '{}'));@@ -428,12 +430,13 @@ export async function completeSlackOAuthCallback(throw new SlackOAuthCallbackError('Invalid metadata');}+ const slack = deps.config.slack;const slackOauthAccessUrl = ['https://slack.com/api/oauth.v2.access',- `?client_id=${process.env.SLACK_CLIENT_ID}`,- `&client_secret=${process.env.SLACK_CLIENT_SECRET}`,+ `?client_id=${slack.clientId}`,+ `&client_secret=${slack.clientSecret}`,`&code=${params.code}`,- `&redirect_uri=${process.env.SLACK_OAUTH_REDIRECT_URL}`,+ `&redirect_uri=${slack.oauthRedirectUrl}`,].join('');const response = await fetch(slackOauthAccessUrl);diff --git a/packages/core/src/modules/integration/src/flush-exports.test.ts b/packages/core/src/modules/integration/src/flush-exports.test.tsindex d184855e8..5ad70b937 100644--- a/packages/core/src/modules/integration/src/flush-exports.test.ts+++ b/packages/core/src/modules/integration/src/flush-exports.test.ts@@ -5,6 +5,7 @@import { describe, expect, it, mock } from 'bun:test';import { gunzipSync } from 'node:zlib';+import { testCoreConfig } from '../../../../test/config-fixture';import {clickhouseEventToExportEvent,createBatch,@@ -101,6 +102,7 @@ function fakeDeps(rows: unknown[][]) {},ch: { query } as unknown as FlushExportsDeps['ch'],logger: stubLogger(),+ config: testCoreConfig(),createAdapter: () =>({upload: async ({@@ -218,11 +220,17 @@ describe.skipIf(!available)('flush-exports -> GCS end to end', () => {format: 'jsonl_gzip' as const,serviceAccountKey: KEY,};- const adapter = createGCSAdapter(config);+ const adapter = createGCSAdapter(config, testCoreConfig());// --- exactly what processExport() does ---const events = [1, 2, 3].map(chEvent).map(clickhouseEventToExportEvent);- const batch = await createBatch('proj_1', 'int_1', events, 'jsonl_gzip');+ const batch = await createBatch(+ stubLogger(),+ 'proj_1',+ 'int_1',+ events,+ 'jsonl_gzip'+ );const basePath = generateBatchPath(config.prefix,'proj_1',diff --git a/packages/core/src/modules/integration/src/flush-exports.ts b/packages/core/src/modules/integration/src/flush-exports.tsindex 1b893bc7a..cf1cb0d38 100644--- a/packages/core/src/modules/integration/src/flush-exports.ts+++ b/packages/core/src/modules/integration/src/flush-exports.ts@@ -19,6 +19,7 @@ import {serializeManifest,} from '../../../clients/integrations/export';import type { IObjectStoreAdapter } from '../../../clients/integrations/object-store';+import type { CoreConfig } from '../../../config';import type { Logger } from '../../../logger';import { DateTime } from '../../../shared/date';import type { IClickhouseEvent } from '../../event/event.service';@@ -32,18 +33,12 @@ import {// Safety lag: never export events whose inserted_at is within this window of// now(), so an in-flight CH insert batch (or replica lag) can't be half-read at// the boundary. Evaluated server-side via CH now64() to avoid worker/CH clock skew.-const LAG_SECONDS = Number.parseInt(process.env.EXPORT_LAG_SECONDS || '60', 10);+const DEFAULT_LAG_SECONDS = 60;// Max rows per object/batch and max batches drained per (project, integration)// per run. A backlog drains over subsequent ticks rather than in one giant pass.-const BATCH_SIZE = Number.parseInt(- process.env.EXPORT_BATCH_SIZE || '50000',- 10-);-const MAX_BATCHES_PER_RUN = Number.parseInt(- process.env.EXPORT_MAX_BATCHES_PER_RUN || '20',- 10-);-const CONCURRENCY = Number.parseInt(process.env.EXPORT_CONCURRENCY || '4', 10);+const DEFAULT_BATCH_SIZE = 50_000;+const DEFAULT_MAX_BATCHES_PER_RUN = 20;+const DEFAULT_CONCURRENCY = 4;// Sentinel cursor id for the first window of a (project, integration). The// events `id` column is a UUID, so the tie-breaker must compare as UUID — an@@ -110,6 +105,8 @@ export interface FlushExportsDeps {db: ExportDb;ch: ExportClickhouse;logger: Logger;+ /** EXPORT_LAG_SECONDS / _BATCH_SIZE / _MAX_BATCHES_PER_RUN / _CONCURRENCY. */+ config: CoreConfig;/** Resolves the object-store adapter for an export config, or undefined. */createAdapter(config: ExportConfig): IObjectStoreAdapter | undefined;}@@ -175,9 +172,16 @@ export async function runFlushExportsCron(}}- await runWithConcurrency(items, CONCURRENCY, (item) =>- processExport(item.projectId, item.integrationId, item.config, deps).catch(- (error) => {+ await runWithConcurrency(+ items,+ deps.config.objectStoreExport.concurrency ?? DEFAULT_CONCURRENCY,+ (item) =>+ processExport(+ item.projectId,+ item.integrationId,+ item.config,+ deps+ ).catch((error) => {deps.logger.error({err: error,@@ -186,8 +190,7 @@ export async function runFlushExportsCron(},'Export failed for project');- }- )+ }));}@@ -205,14 +208,25 @@ async function processExport(const prefix = config.prefix || 'openpanel-exports';const format = config.format || 'jsonl_gzip';- for (let i = 0; i < MAX_BATCHES_PER_RUN; i++) {+ const maxBatches =+ deps.config.objectStoreExport.maxBatchesPerRun ??+ DEFAULT_MAX_BATCHES_PER_RUN;+ const batchSize =+ deps.config.objectStoreExport.batchSize ?? DEFAULT_BATCH_SIZE;+ for (let i = 0; i < maxBatches; i++) {const rows = await queryWindow(projectId, cursor, deps);if (rows.length === 0) {break;}const events = rows.map(clickhouseEventToExportEvent);- const batch = await createBatch(projectId, integrationId, events, format);+ const batch = await createBatch(+ deps.logger,+ projectId,+ integrationId,+ events,+ format+ );const basePath = generateBatchPath(prefix,projectId,@@ -259,7 +273,7 @@ async function processExport('Export batch uploaded');- if (rows.length < BATCH_SIZE) {+ if (rows.length < batchSize) {break;}}@@ -285,10 +299,10 @@ async function queryWindow(`,query_params: {projectId,- lag: LAG_SECONDS,+ lag: deps.config.objectStoreExport.lagSeconds ?? DEFAULT_LAG_SECONDS,wTs: cursor.insertedAt,wId: cursor.eventId || NIL_UUID,- limit: BATCH_SIZE,+ limit: deps.config.objectStoreExport.batchSize ?? DEFAULT_BATCH_SIZE,},format: 'JSONEachRow',});diff --git a/packages/core/src/modules/mcp/src/tools/analytics/profiles.ts b/packages/core/src/modules/mcp/src/tools/analytics/profiles.tsindex a8d6399ab..dc4470005 100644--- a/packages/core/src/modules/mcp/src/tools/analytics/profiles.ts+++ b/packages/core/src/modules/mcp/src/tools/analytics/profiles.ts@@ -99,7 +99,7 @@ export function registerProfileTools(});return {// One template instead of a ~90-character absolute URL on every row.- profile_url_template: `${dashboardBaseUrl()}/${context.organizationId}/${projectId}/profiles/{id}`,+ profile_url_template: `${dashboardBaseUrl(deps.config)}/${context.organizationId}/${projectId}/profiles/{id}`,...table(profiles.slice(0, take), {limit: take,columns: PROFILE_COLUMNS,@@ -135,6 +135,7 @@ export function registerProfileTools(return {profile: result.profile,dashboard_url: profileUrl(+ deps.config,context.organizationId,projectId,profileId@@ -170,11 +171,12 @@ export function registerProfileTools(return {profileId,dashboard_url: profileUrl(+ deps.config,context.organizationId,projectId,profileId),- session_url_template: `${dashboardBaseUrl()}/${context.organizationId}/${projectId}/sessions/{id}`,+ session_url_template: `${dashboardBaseUrl(deps.config)}/${context.organizationId}/${projectId}/sessions/{id}`,...table(sessions.slice(0, take), {limit: take,columns: SESSION_COLUMNS_DEFAULT,diff --git a/packages/core/src/modules/mcp/src/tools/analytics/reports.ts b/packages/core/src/modules/mcp/src/tools/analytics/reports.tsindex 01e3a3aee..35e93cb79 100644--- a/packages/core/src/modules/mcp/src/tools/analytics/reports.ts+++ b/packages/core/src/modules/mcp/src/tools/analytics/reports.ts@@ -1,5 +1,6 @@import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';import { z } from 'zod';+import type { CoreConfig } from '../../../../../config';import type { ServiceDeps } from '../../../../../services';import { getChartStartEndDate } from '../../../../../shared/date';import {@@ -30,19 +31,21 @@ const MAX_PLOTTED_SERIES = 25;const MAX_DATA_POINTS = 180;function reportUrl(+ config: CoreConfig,organizationId: string,projectId: string,reportId: string) {- return `${dashboardBaseUrl()}/${organizationId}/${projectId}/reports/${reportId}`;+ return `${dashboardBaseUrl(config)}/${organizationId}/${projectId}/reports/${reportId}`;}function dashboardUrl(+ config: CoreConfig,organizationId: string,projectId: string,dashboardId: string) {- return `${dashboardBaseUrl()}/${organizationId}/${projectId}/dashboards/${dashboardId}`;+ return `${dashboardBaseUrl(config)}/${organizationId}/${projectId}/dashboards/${dashboardId}`;}type ChartSeries = {@@ -167,7 +170,12 @@ export function registerReportTools(});return dashboards.map((d) => ({...d,- dashboard_url: dashboardUrl(context.organizationId, projectId, d.id),+ dashboard_url: dashboardUrl(+ deps.config,+ context.organizationId,+ projectId,+ d.id+ ),}));}));@@ -207,7 +215,7 @@ export function registerReportTools(breakdowns: r.breakdowns,}));return {- report_url_template: `${dashboardBaseUrl()}/${context.organizationId}/${projectId}/reports/{id}`,+ report_url_template: `${dashboardBaseUrl(deps.config)}/${context.organizationId}/${projectId}/reports/{id}`,...table(rows, {limit: MAX_SERIES_LIMIT,columns: [@@ -342,6 +350,7 @@ export async function runReport(startDate,endDate,dashboard_url: reportUrl(+ deps.config,input.organizationId,input.projectId,input.reportIddiff --git a/packages/core/src/modules/mcp/src/tools/analytics/sessions.ts b/packages/core/src/modules/mcp/src/tools/analytics/sessions.tsindex 062a98288..8b1421779 100644--- a/packages/core/src/modules/mcp/src/tools/analytics/sessions.ts+++ b/packages/core/src/modules/mcp/src/tools/analytics/sessions.ts@@ -67,7 +67,7 @@ export function registerSessionTools(return {// One template beats stamping a ~90-character absolute URL onto every// row — the model can substitute `id` itself.- session_url_template: `${dashboardBaseUrl()}/${context.organizationId}/${projectId}/sessions/{id}`,+ session_url_template: `${dashboardBaseUrl(deps.config)}/${context.organizationId}/${projectId}/sessions/{id}`,...table(sessions.slice(0, take), {limit: take,columns: [...SESSION_COLUMNS_DEFAULT, ...(fields ?? [])],diff --git a/packages/core/src/modules/mcp/src/tools/dashboard-links.ts b/packages/core/src/modules/mcp/src/tools/dashboard-links.tsindex 4064c0949..12fca986e 100644--- a/packages/core/src/modules/mcp/src/tools/dashboard-links.ts+++ b/packages/core/src/modules/mcp/src/tools/dashboard-links.ts@@ -1,5 +1,6 @@import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';import { z } from 'zod';+import type { CoreConfig } from '../../../../config';import {type McpToolDeps,projectIdSchema,@@ -7,28 +8,32 @@ import {withErrorHandling,} from './shared';-export function dashboardBaseUrl() {- return (- process.env.DASHBOARD_URL ||- process.env.NEXT_PUBLIC_DASHBOARD_URL ||- 'https://dashboard.openpanel.dev'- ).replace(/\/$/, '');+const DEFAULT_DASHBOARD_URL = 'https://dashboard.openpanel.dev';+const TRAILING_SLASH = /\/$/;++export function dashboardBaseUrl(config: CoreConfig) {+ return (config.dashboardUrl || DEFAULT_DASHBOARD_URL).replace(+ TRAILING_SLASH,+ ''+ );}export function profileUrl(+ config: CoreConfig,organizationId: string,projectId: string,profileId: string) {- return `${dashboardBaseUrl()}/${organizationId}/${projectId}/profiles/${profileId}`;+ return `${dashboardBaseUrl(config)}/${organizationId}/${projectId}/profiles/${profileId}`;}export function sessionUrl(+ config: CoreConfig,organizationId: string,projectId: string,sessionId: string) {- return `${dashboardBaseUrl()}/${organizationId}/${projectId}/sessions/${sessionId}`;+ return `${dashboardBaseUrl(config)}/${organizationId}/${projectId}/sessions/${sessionId}`;}export function registerDashboardLinkTools(@@ -66,7 +71,7 @@ export function registerDashboardLinkTools(}) =>withErrorHandling(deps, async () => {const projectId = await resolveProjectId(deps, context, inputProjectId);- const base = `${dashboardBaseUrl()}/${context.organizationId}/${projectId}`;+ const base = `${dashboardBaseUrl(deps.config)}/${context.organizationId}/${projectId}`;const urls: Record<string, string> = {overview: base,diff --git a/packages/core/src/modules/mcp/src/tools/dashboard-management.test.ts b/packages/core/src/modules/mcp/src/tools/dashboard-management.test.tsindex 2b82a8f96..7c0fb7c8f 100644--- a/packages/core/src/modules/mcp/src/tools/dashboard-management.test.ts+++ b/packages/core/src/modules/mcp/src/tools/dashboard-management.test.ts@@ -118,6 +118,7 @@ afterAll(() => {mock.module('../../../../shared/slug-id', () => realSlugId);});+import { testCoreConfig } from '../../../../../test/config-fixture';import type { ServiceDeps } from '../../../../services';import type { McpAuthContext } from '../auth';import type { McpToolDeps } from './shared';@@ -261,7 +262,11 @@ function register(context: McpAuthContext = ROOT_CONTEXT) {const tools: McpToolDeps = {context,dbJsonNull: { kind: 'DbNull' },- deps: { db: mockDb, logger: noopLogger } as unknown as ServiceDeps,+ deps: {+ db: mockDb,+ logger: noopLogger,+ config: testCoreConfig(),+ } as unknown as ServiceDeps,services: {} as McpToolDeps['services'],};registerDashboardManagementTools(server as any, tools);diff --git a/packages/core/src/modules/mcp/src/tools/dashboard-management.ts b/packages/core/src/modules/mcp/src/tools/dashboard-management.tsindex 31b84c3b6..5a7efc63e 100644--- a/packages/core/src/modules/mcp/src/tools/dashboard-management.ts+++ b/packages/core/src/modules/mcp/src/tools/dashboard-management.ts@@ -1,5 +1,6 @@import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';import { z } from 'zod';+import type { CoreConfig } from '../../../../config';import type { ServiceDeps } from '../../../../services';import { getId } from '../../../../shared/slug-id';import { getDashboardById } from '../../../dashboard/dashboard.service';@@ -125,19 +126,21 @@ function isValidDateOnly(value: unknown): value is string {}function dashboardUrl(+ config: CoreConfig,organizationId: string,projectId: string,dashboardId: string) {- return `${dashboardBaseUrl()}/${organizationId}/${projectId}/dashboards/${dashboardId}`;+ return `${dashboardBaseUrl(config)}/${organizationId}/${projectId}/dashboards/${dashboardId}`;}function reportUrl(+ config: CoreConfig,organizationId: string,projectId: string,reportId: string) {- return `${dashboardBaseUrl()}/${organizationId}/${projectId}/reports/${reportId}`;+ return `${dashboardBaseUrl(config)}/${organizationId}/${projectId}/reports/${reportId}`;}// `dbNull` is `Prisma.DbNull` (an explicit SQL NULL on a nullable Json@@ -193,6 +196,7 @@ async function requireReport(}function withDashboardUrl(+ config: CoreConfig,organizationId: string,projectId: string,dashboard: {@@ -213,11 +217,17 @@ function withDashboardUrl(organizationId: dashboard.organizationId,createdAt: dashboard.createdAt,updatedAt: dashboard.updatedAt,- dashboard_url: dashboardUrl(organizationId, projectId, dashboard.id),+ dashboard_url: dashboardUrl(+ config,+ organizationId,+ projectId,+ dashboard.id+ ),};}function withReportUrl(+ config: CoreConfig,organizationId: string,projectId: string,report: { id: string }@@ -225,7 +235,7 @@ function withReportUrl(return {...report,// Keep the existing MCP report-tool field name for this report URL.- dashboard_url: reportUrl(organizationId, projectId, report.id),+ dashboard_url: reportUrl(config, organizationId, projectId, report.id),};}@@ -292,6 +302,7 @@ export function registerDashboardManagementTools(report: canonicalReportConfig(report),layout: report.layout,dashboard_url: reportUrl(+ deps.config,context.organizationId,projectId,report.id@@ -300,6 +311,7 @@ export function registerDashboardManagementTools(return {dashboard: withDashboardUrl(+ deps.config,context.organizationId,projectId,dashboard@@ -343,6 +355,7 @@ export function registerDashboardManagementTools(return {dashboard: withDashboardUrl(+ deps.config,context.organizationId,projectId,dashboard@@ -373,6 +386,7 @@ export function registerDashboardManagementTools(return {dashboard: withDashboardUrl(+ deps.config,context.organizationId,projectId,dashboard@@ -449,6 +463,7 @@ export function registerDashboardManagementTools(return {deleted: true,dashboard: withDashboardUrl(+ deps.config,context.organizationId,projectId,dashboard@@ -479,7 +494,12 @@ export function registerDashboardManagementTools(});return {- report: withReportUrl(context.organizationId, projectId, created),+ report: withReportUrl(+ deps.config,+ context.organizationId,+ projectId,+ created+ ),};}));@@ -503,7 +523,12 @@ export function registerDashboardManagementTools(});return {- report: withReportUrl(context.organizationId, projectId, updated),+ report: withReportUrl(+ deps.config,+ context.organizationId,+ projectId,+ updated+ ),};}));@@ -524,7 +549,12 @@ export function registerDashboardManagementTools(return {deleted: true,- report: withReportUrl(context.organizationId, projectId, deleted),+ report: withReportUrl(+ deps.config,+ context.organizationId,+ projectId,+ deleted+ ),};}));@@ -565,7 +595,12 @@ export function registerDashboardManagementTools(});return {- report: withReportUrl(context.organizationId, projectId, duplicate),+ report: withReportUrl(+ deps.config,+ context.organizationId,+ projectId,+ duplicate+ ),};}));@@ -621,6 +656,7 @@ export function registerDashboardManagementTools(dashboardId,count: result.count,dashboard_url: dashboardUrl(+ deps.config,context.organizationId,projectId,dashboardIddiff --git a/packages/core/src/modules/misc/misc.jobs.ts b/packages/core/src/modules/misc/misc.jobs.tsindex a2dd61d49..963e86468 100644--- a/packages/core/src/modules/misc/misc.jobs.ts+++ b/packages/core/src/modules/misc/misc.jobs.ts@@ -33,6 +33,7 @@ export const miscCronJobs = {await runDataHealthCron({db: ctx.db as unknown as DataHealthDb,logger: ctx.logger.child({ job: 'data-health' }),+ config: ctx.config,getLastEventPerProject: () =>ctx.services.project.getLastEventPerProject(),sendEmail: (template, options) =>diff --git a/packages/core/src/modules/misc/misc.routes.ts b/packages/core/src/modules/misc/misc.routes.tsindex 770729835..80ee7dd93 100644--- a/packages/core/src/modules/misc/misc.routes.ts+++ b/packages/core/src/modules/misc/misc.routes.ts@@ -48,7 +48,7 @@ export const miscRoutes = defineRoutes((app) =>.get('/misc/favicon',async ({ query, ctx, set }) => {- const result = await getFavicon(query.url, ctx.logger);+ const result = await getFavicon(ctx.config, query.url, ctx.logger);set.status = result.status;for (const [name, value] of Object.entries(result.headers)) {set.headers[name] = value;@@ -60,7 +60,7 @@ export const miscRoutes = defineRoutes((app) =>.get('/misc/og',async ({ query, ctx, set }) => {- const result = await getOgImage(query.url, ctx.logger);+ const result = await getOgImage(ctx.config, query.url, ctx.logger);set.status = result.status;for (const [name, value] of Object.entries(result.headers)) {set.headers[name] = value;@@ -72,7 +72,7 @@ export const miscRoutes = defineRoutes((app) =>.get('/misc/geo',async ({ ctx, status }) => {- const report = await getGeoReport(ctx.headers);+ const report = await getGeoReport(ctx.config, ctx.headers);if (!report.ok) {return status(400, 'Bad Request');}diff --git a/packages/core/src/modules/misc/misc.service.ts b/packages/core/src/modules/misc/misc.service.tsindex 100050cea..d30007bf7 100644--- a/packages/core/src/modules/misc/misc.service.ts+++ b/packages/core/src/modules/misc/misc.service.ts@@ -25,6 +25,7 @@import crypto from 'node:crypto';import { getRedisCache } from '@openpanel/redis';import { type GeoLocation, getGeoLocation } from '../../clients/geo';+import type { CoreConfig } from '../../config';import type { Logger } from '../../logger';import type { ServiceDeps, Services } from '../../services';import { formatClickhouseDate } from '../../shared/ch-dates';@@ -203,17 +204,19 @@ function textFailure(status: 400 | 404, body: string): ImageAssetFailure {return { status, body, headers: { 'content-type': 'text/plain' } };}-function blockedOrErrorResult(error: unknown): ImageAssetFailure {+function blockedOrErrorResult(+ isProduction: boolean,+ error: unknown+): ImageAssetFailure {if (error instanceof BlockedUrlError) {return {...textFailure(400, 'Bad request'),headers: { 'content-type': 'text/plain', 'cache-control': 'no-store' },};}- const message =- process.env.NODE_ENV === 'production'- ? 'Bad request'- : ((error as Error)?.message ?? 'Error');+ const message = isProduction+ ? 'Bad request'+ : ((error as Error)?.message ?? 'Error');return {...textFailure(400, message),headers: { 'content-type': 'text/plain', 'cache-control': 'no-store' },@@ -221,6 +224,7 @@ function blockedOrErrorResult(error: unknown): ImageAssetFailure {}export async function getFavicon(+ config: CoreConfig,rawUrl: string | undefined,logger?: Pick<Logger, 'debug' | 'warn' | 'error'>): Promise<ImageAssetResult> {@@ -295,17 +299,18 @@ export async function getFavicon(};} catch (error) {logger?.error?.({ err: error, url: rawUrl }, 'Favicon fetch error');- return blockedOrErrorResult(error);+ return blockedOrErrorResult(config.isProduction, error);}}export async function getOgImage(+ config: CoreConfig,rawUrl: string | undefined,logger?: Pick<Logger, 'debug' | 'warn' | 'error'>): Promise<ImageAssetResult> {const url = validateUrl(rawUrl);if (!url) {- return getFavicon(rawUrl, logger);+ return getFavicon(config, rawUrl, logger);}try {@@ -327,14 +332,14 @@ export async function getOgImage(} else {const meta = await parseUrlMeta(url.toString());if (!meta?.ogImage) {- return getFavicon(rawUrl, logger);+ return getFavicon(config, rawUrl, logger);}imageUrl = new URL(meta.ogImage);}const { buffer, status } = await fetchImage(imageUrl, logger);if (status !== 200 || buffer.length === 0) {- return getFavicon(rawUrl, logger);+ return getFavicon(config, rawUrl, logger);}// Rasterize to a 300px-wide PNG@@ -352,7 +357,7 @@ export async function getOgImage(};} catch (error) {logger?.error?.({ err: error, url: rawUrl }, 'OG image fetch error');- return blockedOrErrorResult(error);+ return blockedOrErrorResult(config.isProduction, error);}}@@ -424,12 +429,17 @@ export type GeoReport =};export async function getGeoReport(+ config: CoreConfig,headers: Record<string, string | string[] | undefined> | Headers): Promise<GeoReport> {- const { ip, header } = getClientIpFromHeaders(headers);+ const { ip, header } = getClientIpFromHeaders(config.ipHeaders, headers);const others = await Promise.all(DEFAULT_IP_HEADER_ORDER.map(async (headerName) => {- const { ip: otherIp } = getClientIpFromHeaders(headers, headerName);+ const { ip: otherIp } = getClientIpFromHeaders(+ config.ipHeaders,+ headers,+ headerName+ );return {header: headerName,ip: otherIp,@@ -459,7 +469,7 @@ export async function getGeoReport(* apps/worker/src/jobs/cron.ping.ts.*/export async function runPingCron(deps: ServiceDeps): Promise<unknown> {- if (process.env.DISABLE_PING) {+ if (deps.config.pingDisabled) {return;}@@ -476,8 +486,7 @@ export async function runPingCron(deps: ServiceDeps): Promise<unknown> {method: 'POST',headers: { 'Content-Type': 'application/json' },body: JSON.stringify({- domain:- process.env.DASHBOARD_URL || process.env.NEXT_PUBLIC_DASHBOARD_URL,+ domain: deps.config.dashboardUrl || undefined,count: res.count,}),});diff --git a/packages/core/src/modules/misc/src/data-health.ts b/packages/core/src/modules/misc/src/data-health.tsindex 82865e5c1..57d654e1a 100644--- a/packages/core/src/modules/misc/src/data-health.ts+++ b/packages/core/src/modules/misc/src/data-health.ts@@ -4,6 +4,7 @@// `sendEmail` arrive as injected deps so the job is testable without// `mock.module`. `misc.jobs.ts` binds them off the job's ctx (M10-006).+import type { CoreConfig } from '../../../config';import type { Logger } from '../../../logger';const DAY_MS = 24 * 60 * 60 * 1000;@@ -52,6 +53,7 @@ export type DataHealthTemplate = 'tracking-no-data' | 'tracking-data-stopped';export interface DataHealthDeps {db: DataHealthDb;logger: Logger;+ config: CoreConfig;getLastEventPerProject(): Promise<Map<string, Date>>;sendEmail(template: DataHealthTemplate,@@ -95,7 +97,7 @@ export interface DataHealthResult extends Record<string, number> {export async function runDataHealthCron(deps: DataHealthDeps): Promise<DataHealthResult | null> {- if (process.env.SELF_HOSTED === 'true') {+ if (deps.config.selfHosted) {return null;}@@ -179,7 +181,7 @@ export async function runDataHealthCron(continue;}- const dashboardUrl = `${process.env.DASHBOARD_URL ?? DEFAULT_DASHBOARD_URL}/${alert.organizationId}`;+ const dashboardUrl = `${deps.config.dashboardUrl || DEFAULT_DASHBOARD_URL}/${alert.organizationId}`;if (alert.noData.length > 0) {for (const [to, firstName] of recipients) {diff --git a/packages/core/src/modules/notification/notification.jobs.test.ts b/packages/core/src/modules/notification/notification.jobs.test.tsindex ad4f130ff..5552157ba 100644--- a/packages/core/src/modules/notification/notification.jobs.test.ts+++ b/packages/core/src/modules/notification/notification.jobs.test.ts@@ -2,6 +2,7 @@// dispatch logic themselves are exercised in notification.service.test.ts.import { expect, test } from 'bun:test';+import { testCoreConfig } from '../../../test/config-fixture';import type { AppDeps, Buffers, JobCtx } from '../../context';import { createRecordingProducers } from '../../jobs/testing';import { queues } from '../../jobs.registry';@@ -73,6 +74,7 @@ function stubJobCtx(buffers: {} as Buffers,logger: stubLogger(),queues: createRecordingProducers(queues).queues,+ config: testCoreConfig(),services,requestId: 'req_1',job: {diff --git a/packages/core/src/modules/notification/notification.service.ts b/packages/core/src/modules/notification/notification.service.tsindex 81332de39..d3f35dd29 100644--- a/packages/core/src/modules/notification/notification.service.ts+++ b/packages/core/src/modules/notification/notification.service.ts@@ -272,6 +272,9 @@ function isValidPayload<T>(}/** apps/worker/src/jobs/notification.ts's `sendNotification` job body. */+/** Where a notification email links when DASHBOARD_URL is not set. */+const DEFAULT_DASHBOARD_URL = 'https://dashboard.openpanel.dev';+export async function deliverNotification(deps: ServiceDeps,notification: Prisma.NotificationUncheckedCreateInput@@ -315,7 +318,7 @@ export async function deliverNotification(title: notification.title,message: notification.message,projectName: project.name,- dashboardUrl: `${process.env.DASHBOARD_URL ?? 'https://dashboard.openpanel.dev'}/${project.organizationId}/${notification.projectId}`,+ dashboardUrl: `${deps.config.dashboardUrl || DEFAULT_DASHBOARD_URL}/${project.organizationId}/${notification.projectId}`,},});}diff --git a/packages/core/src/modules/onboarding/onboarding.jobs.test.ts b/packages/core/src/modules/onboarding/onboarding.jobs.test.tsindex 5fed26160..371934c7d 100644--- a/packages/core/src/modules/onboarding/onboarding.jobs.test.ts+++ b/packages/core/src/modules/onboarding/onboarding.jobs.test.ts@@ -2,6 +2,7 @@// method itself is exercised in onboarding.service.test.ts.import { expect, test } from 'bun:test';+import { testCoreConfig } from '../../../test/config-fixture';import type { AppDeps, Buffers, JobCtx } from '../../context';import { createRecordingProducers } from '../../jobs/testing';import { queues } from '../../jobs.registry';@@ -74,6 +75,7 @@ function stubJobCtx(buffers: {} as Buffers,logger: stubLogger(),queues: createRecordingProducers(queues).queues,+ config: testCoreConfig(),services,requestId: 'req_1',job: { id: 'job_1', attempt: 0, queue: 'cron', name: jobName },diff --git a/packages/core/src/modules/onboarding/onboarding.service.test.ts b/packages/core/src/modules/onboarding/onboarding.service.test.tsindex 74e09e22b..58f4f6911 100644--- a/packages/core/src/modules/onboarding/onboarding.service.test.ts+++ b/packages/core/src/modules/onboarding/onboarding.service.test.ts@@ -8,6 +8,7 @@import { afterAll, beforeAll, beforeEach, expect, mock, test } from 'bun:test';import { subDays } from 'date-fns';+import { testCoreConfig } from '../../../test/config-fixture';const organizationFindMany = mock(async () => [] as unknown[]);const organizationUpdate = mock(async () => ({}));@@ -16,7 +17,10 @@ const 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 deps = {+ db: dbMock,+ config: testCoreConfig(),+} as unknown as import('../../services').ServiceDeps;const getOrganizationEventsCount = mock(async (_deps: unknown, _projectIds: string[]) => 0@@ -90,12 +94,13 @@ beforeEach(() => {logger.info.mockClear();logger.warn.mockClear();logger.error.mockClear();- process.env.SELF_HOSTED = 'false';});test('returns null and does nothing when self-hosted', async () => {- process.env.SELF_HOSTED = 'true';- const result = await runOnboardingCron(deps, logger);+ const result = await runOnboardingCron(+ { ...deps, config: testCoreConfig({ selfHosted: true }) },+ logger+ );expect(result).toBeNull();expect(organizationFindMany).not.toHaveBeenCalled();});diff --git a/packages/core/src/modules/onboarding/onboarding.service.ts b/packages/core/src/modules/onboarding/onboarding.service.tsindex 3c5678351..e41977ad0 100644--- a/packages/core/src/modules/onboarding/onboarding.service.ts+++ b/packages/core/src/modules/onboarding/onboarding.service.ts@@ -208,6 +208,7 @@ interface OnboardingCronOrg {interface OnboardingContext {org: OnboardingCronOrg;user: NonNullable<OnboardingCronOrg['createdBy']>;+ dashboardUrl: string;// Lazy + memoized: only emails past the day gate pay for the ClickHouse count.getUsage: () => Promise<OnboardingUsage>;}@@ -229,10 +230,10 @@ function createUsageGetter(deps: ServiceDeps, org: OnboardingCronOrg) {const getters = {firstName: (ctx: OnboardingContext) => ctx.user.firstName || undefined,dashboardUrl: (ctx: OnboardingContext) => {- return `${process.env.DASHBOARD_URL}/${ctx.org.id}`;+ return `${ctx.dashboardUrl}/${ctx.org.id}`;},billingUrl: (ctx: OnboardingContext) => {- return `${process.env.DASHBOARD_URL}/${ctx.org.id}/billing`;+ return `${ctx.dashboardUrl}/${ctx.org.id}/billing`;},trialEndDate: (ctx: OnboardingContext) => {return ctx.org.subscriptionEndsAt@@ -333,7 +334,7 @@ export async function runOnboardingCron(deps: ServiceDeps,logger: OnboardingCronLogger): Promise<OnboardingCronSummary | null> {- if (process.env.SELF_HOSTED === 'true') {+ if (deps.config.selfHosted) {return null;}@@ -369,7 +370,12 @@ export async function runOnboardingCron(email: user.email,anchor: org.createdAt,pointer: org.onboarding,- ctx: { org, user, getUsage: createUsageGetter(deps, org) },+ ctx: {+ org,+ user,+ dashboardUrl: deps.config.dashboardUrl,+ getUsage: createUsageGetter(deps, org),+ },};});diff --git a/packages/core/src/modules/organization/organization.jobs.test.ts b/packages/core/src/modules/organization/organization.jobs.test.tsindex 7a6167a89..6289f7473 100644--- a/packages/core/src/modules/organization/organization.jobs.test.ts+++ b/packages/core/src/modules/organization/organization.jobs.test.ts@@ -2,6 +2,7 @@// method itself is exercised in organization.service.test.ts.import { expect, test } from 'bun:test';+import { testCoreConfig } from '../../../test/config-fixture';import type { AppDeps, Buffers, JobCtx } from '../../context';import { createRecordingProducers } from '../../jobs/testing';import { queues } from '../../jobs.registry';@@ -74,6 +75,7 @@ function stubJobCtx(buffers: {} as Buffers,logger: stubLogger(),queues: createRecordingProducers(queues).queues,+ config: testCoreConfig(),services,requestId: 'req_1',job: { id: 'job_1', attempt: 0, queue: 'cron', name: jobName },diff --git a/packages/core/src/modules/organization/organization.jobs.ts b/packages/core/src/modules/organization/organization.jobs.tsindex 108d0683e..1cb813e0e 100644--- a/packages/core/src/modules/organization/organization.jobs.ts+++ b/packages/core/src/modules/organization/organization.jobs.ts@@ -67,12 +67,13 @@ async function windDownDeps(ctx: Ctx, logger: Logger): Promise<WindDownDeps> {getAnalyticsOverview: (input) =>ctx.services.overview.getAnalyticsOverviewCore(input),getTopPages: (input) => ctx.services.pages.getTopPagesCore(input),- generatePitch: (facts) => generateWinBackPitch(facts),+ generatePitch: (facts) => generateWinBackPitch(ctx.config, facts),};return {db: ctx.db as unknown as WindDownDb,logger,+ config: ctx.config,sendEmail: ctx.clients.email.sendEmail,getLastEventPerProject: () => ctx.services.project.getLastEventPerProject(),getOrganizationEventsCount: (projectIds) =>diff --git a/packages/core/src/modules/organization/organization.service.test.ts b/packages/core/src/modules/organization/organization.service.test.tsindex 5c767b18d..28d81f780 100644--- a/packages/core/src/modules/organization/organization.service.test.ts+++ b/packages/core/src/modules/organization/organization.service.test.ts@@ -4,6 +4,7 @@// registered before the subject's first call, not before its import.import { afterAll, beforeAll, beforeEach, expect, mock, test } from 'bun:test';+import { testCoreConfig } from '../../../test/config-fixture';interface FakeOrganization {id: string;@@ -411,6 +412,7 @@ const deps = {$transaction,},ch: { command: chCommand },+ config: testCoreConfig(),} as unknown as import('../../services').ServiceDeps;// Bypasses the Redis cache-aside entirely — this module's own logic isdiff --git a/packages/core/src/modules/organization/organization.service.ts b/packages/core/src/modules/organization/organization.service.tsindex 747eb6c58..6b6c6aa93 100644--- a/packages/core/src/modules/organization/organization.service.ts+++ b/packages/core/src/modules/organization/organization.service.ts@@ -49,7 +49,7 @@ import {import { cacheablePerDeps } from '../../shared/cacheable-per-deps';import { formatClickhouseDate } from '../../shared/ch-dates';import { chQuery } from '../../shared/ch-query';-import { isClickhouseClustered, TABLE_NAMES } from '../../shared/ch-tables';+import { TABLE_NAMES } from '../../shared/ch-tables';import { DateTime } from '../../shared/date';import { generateSecureId } from '../../shared/id';@@ -247,7 +247,7 @@ export async function connectUserToOrganization(throw new Error('Invite not found');}- if (process.env.ALLOW_INVITATION === 'false') {+ if (deps.config.auth.allowInvitation === false) {throw new Error('Invitations are not allowed');}@@ -559,7 +559,7 @@ export async function deleteFromClickhouse(// `getReplicatedTableName` appends `ON CLUSTER '{cluster}'` in clustered// mode, which is a clause rather than an identifier — so the table name// goes through `sql.id` and the clause stays literal template text.- const target = isClickhouseClustered()+ const target = deps.config.clickhouseClustered? sql`${sql.id(`${table}_replicated`)} ON CLUSTER '{cluster}'`: sql.id(table);// If materialized view, use ALTER TABLE since DELETE is not supported@@ -825,7 +825,7 @@ export async function inviteUserToOrganization(await sendEmail('invite', {to: email,data: {- url: `${process.env.DASHBOARD_URL || process.env.NEXT_PUBLIC_DASHBOARD_URL}/onboarding?inviteId=${invite.id}`,+ url: `${deps.config.dashboardUrl}/onboarding?inviteId=${invite.id}`,organizationName: invite.organization.name,},});diff --git a/packages/core/src/modules/organization/src/wind-down.test.ts b/packages/core/src/modules/organization/src/wind-down.test.tsindex ea187e670..438863cc2 100644--- a/packages/core/src/modules/organization/src/wind-down.test.ts+++ b/packages/core/src/modules/organization/src/wind-down.test.ts@@ -5,8 +5,9 @@// (who enters, which step fires, what gets written), not any real persistence.// No `mock.module`, so nothing this file does leaks into another test file.-import { afterEach, beforeEach, describe, expect, it, mock } from 'bun:test';+import { beforeEach, describe, expect, it, mock } from 'bun:test';import { subDays } from 'date-fns';+import { testCoreConfig } from '../../../../test/config-fixture';import type { Logger } from '../../../logger';import {runWindDownCron,@@ -51,6 +52,7 @@ function deps(): WindDownDeps {},},logger: stubLogger(),+ config: windDownConfig(),sendEmail: sendEmail as WindDownDeps['sendEmail'],getLastEventPerProject,getOrganizationEventsCount,@@ -82,16 +84,19 @@ function makeOrg(} as WindDownOrganization;}-const savedEnv = {- SELF_HOSTED: process.env.SELF_HOSTED,- DASHBOARD_URL: process.env.DASHBOARD_URL,- WIND_DOWN_MAX_PER_RUN: process.env.WIND_DOWN_MAX_PER_RUN,-};+/** SELF_HOSTED / DASHBOARD_URL / WIND_DOWN_MAX_PER_RUN arrive as config. */+function windDownConfig(+ overrides: { selfHosted?: boolean; maxPerRun?: number } = {}+) {+ const base = testCoreConfig();+ return testCoreConfig({+ selfHosted: overrides.selfHosted ?? false,+ dashboardUrl: 'https://dashboard.openpanel.dev',+ query: { ...base.query, windDownMaxPerRun: overrides.maxPerRun },+ });+}beforeEach(() => {- process.env.SELF_HOSTED = 'false';- process.env.DASHBOARD_URL = 'https://dashboard.openpanel.dev';- delete process.env.WIND_DOWN_MAX_PER_RUN;organizationFindMany.mockClear().mockResolvedValue([]);organizationUpdate.mockClear().mockResolvedValue({});organizationUpdateMany.mockClear().mockResolvedValue({ count: 0 });@@ -103,21 +108,14 @@ beforeEach(() => {buildHighlight.mockClear().mockResolvedValue(undefined);});-afterEach(() => {- for (const [key, value] of Object.entries(savedEnv)) {- if (value === undefined) {- delete process.env[key];- } else {- process.env[key] = value;- }- }-});-describe('runWindDownCron', () => {it('does nothing when self hosted', async () => {- process.env.SELF_HOSTED = 'true';-- expect(await runWindDownCron(deps())).toBeNull();+ expect(+ await runWindDownCron({+ ...deps(),+ config: windDownConfig({ selfHosted: true }),+ })+ ).toBeNull();expect(organizationFindMany).not.toHaveBeenCalled();});@@ -151,7 +149,6 @@ describe('runWindDownCron', () => {});it('caps how many organizations enter per run', async () => {- process.env.WIND_DOWN_MAX_PER_RUN = '2';organizationFindMany.mockResolvedValue([makeOrg({ id: 'org-1' }),makeOrg({ id: 'org-2' }),@@ -159,7 +156,10 @@ describe('runWindDownCron', () => {makeOrg({ id: 'org-4' }),]);- const result = await runWindDownCron(deps());+ const result = await runWindDownCron({+ ...deps(),+ config: windDownConfig({ maxPerRun: 2 }),+ });expect(result).toMatchObject({ entering: 2 });expect(organizationUpdateMany).toHaveBeenCalledWith({@@ -273,7 +273,6 @@ describe('runWindDownCron', () => {// The population this whole sequence is aimed at: trial lapsed months ago,// SDKs never stopped, never paid a cent.it('lets organizations that are still tracking into the sequence first', async () => {- process.env.WIND_DOWN_MAX_PER_RUN = '1';getLastEventPerProject.mockResolvedValue(new Map([['project-active', subDays(new Date(), 1)]]));@@ -290,7 +289,10 @@ describe('runWindDownCron', () => {}),]);- const result = await runWindDownCron(deps());+ const result = await runWindDownCron({+ ...deps(),+ config: windDownConfig({ maxPerRun: 1 }),+ });expect(result).toMatchObject({ entering: 1, stillTracking: 1 });expect(organizationUpdateMany).toHaveBeenCalledWith({diff --git a/packages/core/src/modules/organization/src/wind-down.ts b/packages/core/src/modules/organization/src/wind-down.tsindex 21da55370..2f0a813ab 100644--- a/packages/core/src/modules/organization/src/wind-down.ts+++ b/packages/core/src/modules/organization/src/wind-down.ts@@ -9,6 +9,7 @@import { getRecommendedPlan } from '@openpanel/payments';import { addDays, format, subDays } from 'date-fns';+import type { CoreConfig } from '../../../config';import type { Logger } from '../../../logger';import {runSequence,@@ -51,9 +52,8 @@ const RECENT_VOLUME_DAYS = 30;/** Orgs allowed to *enter* the sequence per tick. Entry staggers the cohort. */const DEFAULT_MAX_PER_RUN = 100;-function getMaxPerRun(): number {- const parsed = Number.parseInt(process.env.WIND_DOWN_MAX_PER_RUN ?? '', 10);- return Number.isNaN(parsed) || parsed <= 0 ? DEFAULT_MAX_PER_RUN : parsed;+function getMaxPerRun(config: CoreConfig): number {+ return config.query.windDownMaxPerRun ?? DEFAULT_MAX_PER_RUN;}/** The organization row shape this job reads — V1's `orgQuery` include. */@@ -92,6 +92,7 @@ interface WindDownUsage {export interface WindDownDeps {db: WindDownDb;logger: Logger;+ config: CoreConfig;sendEmail: SendSequenceEmail;getLastEventPerProject(): Promise<Map<string, Date>>;getOrganizationEventsCount(projectIds: string[]): Promise<number>;@@ -108,6 +109,7 @@ export interface WindDownDeps {interface WindDownContext {org: WindDownOrganization;user: NonNullable<WindDownOrganization['createdBy']>;+ dashboardUrl: string;/** Day 0 of this org's schedule. */startedAt: Date;lastEventAt: Date | null;@@ -169,7 +171,7 @@ function createUsageGetter(org: WindDownOrganization, deps: WindDownDeps) {const getters = {firstName: (ctx: WindDownContext) => ctx.user.firstName || undefined,billingUrl: (ctx: WindDownContext) =>- `${process.env.DASHBOARD_URL}/${ctx.org.id}/billing`,+ `${ctx.dashboardUrl}/${ctx.org.id}/billing`,blockDate: (ctx: WindDownContext) =>format(addDays(ctx.startedAt, BLOCK_DAY), 'MMMM d'),deleteDate: (ctx: WindDownContext) =>@@ -292,7 +294,7 @@ export interface WindDownResult {export async function runWindDownCron(deps: WindDownDeps): Promise<WindDownResult | null> {- if (process.env.SELF_HOSTED === 'true') {+ if (deps.config.selfHosted) {return null;}@@ -394,7 +396,7 @@ export async function runWindDownCron((left?.lastEventAt?.getTime() ?? 0));})- .slice(0, getMaxPerRun());+ .slice(0, getMaxPerRun(deps.config));if (entering.length > 0) {await deps.db.organization.updateMany({@@ -436,6 +438,7 @@ export async function runWindDownCron(ctx: {org,user,+ dashboardUrl: deps.config.dashboardUrl,startedAt,lastEventAt: seen?.lastEventAt ?? null,stillTracking,diff --git a/packages/core/src/modules/session/session.service.ts b/packages/core/src/modules/session/session.service.tsindex 5db1b03c6..537758a21 100644--- a/packages/core/src/modules/session/session.service.ts+++ b/packages/core/src/modules/session/session.service.ts@@ -51,10 +51,6 @@ function loadFilterCompiler() {return import('../chart/src/table-filter-where');}-function loadLookback() {- return import('../../shared/lookback');-}-function loadDateService() {return import('../../shared/date');}@@ -164,7 +160,6 @@ const SESSION_FILTER_TABLE = {const DEFAULT_LOOKBACK_DAYS = 0.5;const SESSION_LIST_MAX_LOOKBACK_DAYS_DEFAULT = 365;-const SESSION_LIST_MAX_LOOKBACK_DAYS_ENV = 'SESSION_LIST_MAX_LOOKBACK_DAYS';const SESSIONS_COUNT_CACHE_SECONDS = 60 * 10;const REPLAY_CHUNKS_PAGE_SIZE = 40;@@ -265,13 +260,11 @@ export async function getSessionList(dateIntervalInDays = DEFAULT_LOOKBACK_DAYS,} = options;- const { resolveMaxLookbackDays } = await loadLookback();-- // Deployment-tunable ceiling for the empty-result lookback (see lookback.ts).- const maxLookbackDays = resolveMaxLookbackDays(- SESSION_LIST_MAX_LOOKBACK_DAYS_ENV,- SESSION_LIST_MAX_LOOKBACK_DAYS_DEFAULT- );+ // Deployment-tunable ceiling for the empty-result lookback+ // (SESSION_LIST_MAX_LOOKBACK_DAYS).+ const maxLookbackDays =+ deps.config.query.sessionListMaxLookbackDays ??+ SESSION_LIST_MAX_LOOKBACK_DAYS_DEFAULT;const lookbackDays = Math.min(dateIntervalInDays, maxLookbackDays);const data = await chQuery<IClickhouseSession & { hasReplay: boolean }>(diff --git a/packages/core/src/modules/session/src/runtime.ts b/packages/core/src/modules/session/src/runtime.tsindex 97b889b00..0b55a50f6 100644--- a/packages/core/src/modules/session/src/runtime.ts+++ b/packages/core/src/modules/session/src/runtime.ts@@ -4,6 +4,7 @@// hand in stubs, so no `mock.module` is needed and every assertion is on a// call the code under test made.+import type { CoreConfig } from '../../../config';import type { Ctx } from '../../../context';import type { IClickhouseSession } from '../session.service';@@ -48,6 +49,8 @@ export interface SessionRedis {export interface SessionRuntime {redis: SessionRedis;sessions: SessionStore;+ /** The reaper's and vacuum's kill switches, batch sizes and thresholds. */+ config: CoreConfig;}/**@@ -57,7 +60,11 @@ export interface SessionRuntime {* own and its writes stay inside the request's scope (ADR-007, ADR-018 R1).*/export function sessionRuntimeFrom(- ctx: Pick<Ctx, 'redis' | 'buffers'>+ ctx: Pick<Ctx, 'redis' | 'buffers' | 'config'>): SessionRuntime {- return { redis: ctx.redis, sessions: ctx.buffers.session };+ return {+ redis: ctx.redis,+ sessions: ctx.buffers.session,+ config: ctx.config,+ };}diff --git a/packages/core/src/modules/session/src/session-end.test.ts b/packages/core/src/modules/session/src/session-end.test.tsindex 442ddb4c6..2d2693cc7 100644--- a/packages/core/src/modules/session/src/session-end.test.ts+++ b/packages/core/src/modules/session/src/session-end.test.ts@@ -4,6 +4,7 @@// id-gated to the closed session.import { describe, expect, mock, test } from 'bun:test';+import { testCoreConfig } from '../../../../test/config-fixture';import type { IServiceCreateEventPayload } from '../../event/event.service';import {createSessionEnd,@@ -39,6 +40,7 @@ function makeDeps(live = snapshot as ReturnType<typeof fixtureSession> | null) {redis,sessions,logger: stubLogger(),+ config: testCoreConfig(),createEvent,transformEvent: (event) => event as never,transformSessionToEvent: () => ({}) as never,diff --git a/packages/core/src/modules/session/src/session-end.ts b/packages/core/src/modules/session/src/session-end.tsindex 84fe8f063..f55df78be 100644--- a/packages/core/src/modules/session/src/session-end.ts+++ b/packages/core/src/modules/session/src/session-end.ts@@ -6,6 +6,7 @@// the job's `ctx` supplies every client, so the requestId that opened the// session still labels the rows this close writes (ADR-018 R1).+import type { CoreConfig } from '../../../config';import type { EnqueueOptions } from '../../../jobs/define';import type { Logger } from '../../../logger';import type {@@ -38,9 +39,6 @@ const SESSION_EVENTS_WINDOW_PADDING_MS = 1000;/** The session_end event lands one second after the last event. */const SESSION_END_OFFSET_MS = 1000;-const PROFILE_BACKFILL_FLAG_ENV = 'EXPERIMENTAL_PROFILE_BACKFILL';-const PROFILE_BACKFILL_PROJECTS_ENV = 'EXPERIMENTAL_PROFILE_BACKFILL_PROJECTS';-/*** Deterministic jobId for a closed session, so concurrent / retried closes* dedupe in BullMQ. BullMQ only accepts ':' in custom jobIds when splitting@@ -119,14 +117,18 @@ function claimKey(session: IClickhouseSession): string {return `session:end:emitted:${session.project_id}:${session.device_id}:${session.id}`;}-function isProfileBackfillEnabled(projectId: string): boolean {- if (process.env[PROFILE_BACKFILL_FLAG_ENV] !== '1') {+function isProfileBackfillEnabled(+ config: CoreConfig,+ projectId: string+): boolean {+ const { profileBackfillEnabled, profileBackfillProjectIds } = config.session;+ if (!profileBackfillEnabled) {return false;}- const runOnProjects =- process.env[PROFILE_BACKFILL_PROJECTS_ENV]?.split(',').filter(Boolean) ??- [];- return runOnProjects.length === 0 || runOnProjects.includes(projectId);+ return (+ profileBackfillProjectIds.length === 0 ||+ profileBackfillProjectIds.includes(projectId)+ );}/**@@ -202,7 +204,7 @@ export async function createSessionEnd(if (profileId !== session.device_id &&- isProfileBackfillEnabled(payload.projectId)+ isProfileBackfillEnabled(deps.config, payload.projectId)) {await deps.profileBackfill.add({projectId: payload.projectId,diff --git a/packages/core/src/modules/session/src/session-keys.ts b/packages/core/src/modules/session/src/session-keys.tsindex 9ae6394b3..3d82c8a2b 100644--- a/packages/core/src/modules/session/src/session-keys.ts+++ b/packages/core/src/modules/session/src/session-keys.ts@@ -3,13 +3,9 @@// V1's reaper and vacuum redefine them — the buffer keeps its own copy and// importing it would construct the ClickHouse client at import time.-const DEFAULT_SESSION_TIMEOUT_MS = 1000 * 60 * 30;--/** Idle time after which a session is over; also the reaper's deadman default. */-export const SESSION_TIMEOUT_MS = Number.parseInt(- process.env.SESSION_TIMEOUT_MS || String(DEFAULT_SESSION_TIMEOUT_MS),- 10-);+/** Idle time after which a session is over; also the reaper's deadman default.+ * SESSION_TIMEOUT_MS overrides it (`config.session.timeoutMs`). */+export const DEFAULT_SESSION_TIMEOUT_MS = 1000 * 60 * 30;/** Set of project ids with at least one live session. */export const PROJECTS_SET_KEY = 'session:projects';diff --git a/packages/core/src/modules/session/src/session-reaper.test.ts b/packages/core/src/modules/session/src/session-reaper.test.tsindex ba7245cb5..c998ec414 100644--- a/packages/core/src/modules/session/src/session-reaper.test.ts+++ b/packages/core/src/modules/session/src/session-reaper.test.ts@@ -2,7 +2,8 @@// wall-clock deadman that closes idle sessions: stub Redis drives the// wallclock ZSET + lock, so the assertions are on decisions, not side effects.-import { afterEach, describe, expect, mock, test } from 'bun:test';+import { describe, expect, mock, test } from 'bun:test';+import { testCoreConfig } from '../../../../test/config-fixture';import {fixtureSession,stubLogger,@@ -11,7 +12,10 @@ import {} from './session-lifecycle.fixtures';import { reapIdleSessions, type SessionReaperDeps } from './session-reaper';-function makeDeps(live: ReturnType<typeof fixtureSession> | null) {+function makeDeps(+ live: ReturnType<typeof fixtureSession> | null,+ config = testCoreConfig()+) {const redis = stubRedis();const sessions = stubStore(live);const enqueueSessionEnd = mock<SessionReaperDeps['enqueueSessionEnd']>(@@ -21,14 +25,16 @@ function makeDeps(live: ReturnType<typeof fixtureSession> | null) {redis,sessions,enqueueSessionEnd,- deps: { redis, sessions, enqueueSessionEnd, logger: stubLogger() },+ deps: {+ redis,+ sessions,+ enqueueSessionEnd,+ logger: stubLogger(),+ config,+ },};}-afterEach(() => {- delete process.env.SESSION_REAPER;-});-describe('reapIdleSessions', () => {test('enqueues a session_end for an idle session blob', async () => {const { deps, redis, enqueueSessionEnd } = makeDeps(@@ -73,8 +79,12 @@ describe('reapIdleSessions', () => {});test('is a no-op when disabled via SESSION_REAPER=0', async () => {- process.env.SESSION_REAPER = '0';- const { deps, redis } = makeDeps(null);+ const { deps, redis } = makeDeps(+ null,+ testCoreConfig({+ session: { ...testCoreConfig().session, reaperEnabled: false },+ })+ );await reapIdleSessions(deps);diff --git a/packages/core/src/modules/session/src/session-reaper.ts b/packages/core/src/modules/session/src/session-reaper.tsindex 4fc25f90c..14c699dea 100644--- a/packages/core/src/modules/session/src/session-reaper.ts+++ b/packages/core/src/modules/session/src/session-reaper.ts@@ -11,23 +11,16 @@ import {} from './session.metrics';import type { EnqueueSessionEndInput } from './session-end';import {+ DEFAULT_SESSION_TIMEOUT_MS,PROJECTS_SET_KEY,- SESSION_TIMEOUT_MS,wallclockSetKey,} from './session-keys';-const REAPER_DISABLED_ENV = 'SESSION_REAPER';-const REAPER_BATCH_SIZE_ENV = 'SESSION_REAPER_BATCH_SIZE';-const REAPER_DEADMAN_ENV = 'SESSION_REAPER_WALLCLOCK_DEADMAN_MS';const DEFAULT_REAPER_BATCH_SIZE = 5000;const LOCK_TTL_SECONDS = 60;const lockKey = (projectId: string) => `session:reaper:lock:${projectId}`;-function readIntEnv(name: string, fallback: number): number {- return Number.parseInt(process.env[name] || String(fallback), 10);-}-export interface SessionReaperDeps extends SessionRuntime {logger: Logger;enqueueSessionEnd(input: EnqueueSessionEndInput): Promise<unknown>;@@ -42,7 +35,7 @@ export interface SessionReaperDeps extends SessionRuntime {* the session_end jobId.*/export async function reapIdleSessions(deps: SessionReaperDeps): Promise<void> {- if (process.env[REAPER_DISABLED_ENV] === '0') {+ if (!deps.config.session.reaperEnabled) {return;}@@ -94,7 +87,11 @@ async function reapProject(}try {- const deadmanMs = readIntEnv(REAPER_DEADMAN_ENV, SESSION_TIMEOUT_MS);+ const session = deps.config.session;+ const deadmanMs =+ session.reaperWallclockDeadmanMs ??+ session.timeoutMs ??+ DEFAULT_SESSION_TIMEOUT_MS;const cutoff = Date.now() - deadmanMs;const candidates = await redis.zrangebyscore(wallclockSetKey(projectId),@@ -102,7 +99,7 @@ async function reapProject(cutoff,'LIMIT',0,- readIntEnv(REAPER_BATCH_SIZE_ENV, DEFAULT_REAPER_BATCH_SIZE)+ session.reaperBatchSize ?? DEFAULT_REAPER_BATCH_SIZE);let reaped = 0;diff --git a/packages/core/src/modules/session/src/session-vacuum.test.ts b/packages/core/src/modules/session/src/session-vacuum.test.tsindex 75626cd12..10f33ace5 100644--- a/packages/core/src/modules/session/src/session-vacuum.test.ts+++ b/packages/core/src/modules/session/src/session-vacuum.test.ts@@ -2,7 +2,8 @@// daily backstop for sessions whose cleanup() leaked: a lingering blob →// id-gated cleanup(); a missing blob → ZREM the orphan.-import { afterEach, describe, expect, test } from 'bun:test';+import { describe, expect, test } from 'bun:test';+import { testCoreConfig } from '../../../../test/config-fixture';import {fixtureSession,stubLogger,@@ -11,16 +12,19 @@ import {} from './session-lifecycle.fixtures';import { vacuumStaleSessions } from './session-vacuum';-function makeDeps(live: ReturnType<typeof fixtureSession> | null) {+function makeDeps(+ live: ReturnType<typeof fixtureSession> | null,+ config = testCoreConfig()+) {const redis = stubRedis();const sessions = stubStore(live);- return { redis, sessions, deps: { redis, sessions, logger: stubLogger() } };+ return {+ redis,+ sessions,+ deps: { redis, sessions, logger: stubLogger(), config },+ };}-afterEach(() => {- delete process.env.SESSION_VACUUM;-});-describe('vacuumStaleSessions', () => {test('id-gated cleanup() for a stale blob that lingered', async () => {const { deps, redis, sessions } = makeDeps(@@ -57,8 +61,12 @@ describe('vacuumStaleSessions', () => {});test('is a no-op when disabled via SESSION_VACUUM=0', async () => {- process.env.SESSION_VACUUM = '0';- const { deps, redis } = makeDeps(null);+ const { deps, redis } = makeDeps(+ null,+ testCoreConfig({+ session: { ...testCoreConfig().session, vacuumEnabled: false },+ })+ );await vacuumStaleSessions(deps);diff --git a/packages/core/src/modules/session/src/session-vacuum.ts b/packages/core/src/modules/session/src/session-vacuum.tsindex d62f0adbe..d2f22f157 100644--- a/packages/core/src/modules/session/src/session-vacuum.ts+++ b/packages/core/src/modules/session/src/session-vacuum.ts@@ -5,17 +5,10 @@ import type { SessionRuntime } from './runtime';import { sessionsVacuumed } from './session.metrics';import { PROJECTS_SET_KEY, wallclockSetKey } from './session-keys';-const VACUUM_DISABLED_ENV = 'SESSION_VACUUM';-const VACUUM_BATCH_SIZE_ENV = 'SESSION_VACUUM_BATCH_SIZE';-const VACUUM_STALE_THRESHOLD_ENV = 'SESSION_VACUUM_STALE_THRESHOLD_MS';const DEFAULT_VACUUM_BATCH_SIZE = 1000;// Much larger than the reaper deadman, so it never races with normal reaping.const DEFAULT_VACUUM_STALE_THRESHOLD_MS = 1000 * 60 * 60 * 24 * 7;-function readIntEnv(name: string, fallback: number): number {- return Number.parseInt(process.env[name] || String(fallback), 10);-}-export interface SessionVacuumDeps extends SessionRuntime {logger: Logger;}@@ -30,11 +23,11 @@ export interface SessionVacuumDeps extends SessionRuntime {export async function vacuumStaleSessions(deps: SessionVacuumDeps): Promise<void> {- if (process.env[VACUUM_DISABLED_ENV] === '0') {+ if (!deps.config.session.vacuumEnabled) {return;}- const { redis, sessions, logger } = deps;+ const { redis, sessions, logger, config } = deps;const projectIds = await redis.smembers(PROJECTS_SET_KEY);if (projectIds.length === 0) {return;@@ -44,11 +37,9 @@ export async function vacuumStaleSessions(const cutoff =Date.now() -- readIntEnv(VACUUM_STALE_THRESHOLD_ENV, DEFAULT_VACUUM_STALE_THRESHOLD_MS);- const batchSize = readIntEnv(- VACUUM_BATCH_SIZE_ENV,- DEFAULT_VACUUM_BATCH_SIZE- );+ (config.session.vacuumStaleThresholdMs ??+ DEFAULT_VACUUM_STALE_THRESHOLD_MS);+ const batchSize = config.session.vacuumBatchSize ?? DEFAULT_VACUUM_BATCH_SIZE;let total = 0;let staleBlobs = 0;let missingBlobs = 0;diff --git a/packages/core/src/modules/session/src/usage.ts b/packages/core/src/modules/session/src/usage.tsindex 37a010caf..c4f2f01f2 100644--- a/packages/core/src/modules/session/src/usage.ts+++ b/packages/core/src/modules/session/src/usage.ts@@ -25,10 +25,6 @@ const USAGE_WARNING_THRESHOLD = 0.8;const UPDATE_EVENTS_COUNT_CACHE_SECONDS = 60 * 60;const DEFAULT_DASHBOARD_URL = 'https://dashboard.openpanel.dev';-function isSelfHosted(): boolean {- return process.env.SELF_HOSTED === 'true';-}-/** Saturating: the columns are INT4 and lifetime counts can exceed them. */function clampToInt4(count: number): number {return Math.min(count, INT4_MAX);@@ -36,11 +32,12 @@ function clampToInt4(count: number): number {function nextExceededAt(organization: Organization,- count: number+ count: number,+ selfHosted: boolean): Date | null {// Self-hosting has no billing limits: never flag, and clear any stale flag// set before this guard existed (default limit 0 trips on the first event).- if (isSelfHosted()) {+ if (selfHosted) {return null;}const limit = organization.subscriptionPeriodEventsLimit;@@ -90,12 +87,13 @@ export const updateEventsCount = cacheablePerDeps(subscriptionPeriodEventsCount: clampToInt4(organizationEventsCount),subscriptionPeriodEventsCountExceededAt: nextExceededAt(organization,- organizationEventsCount+ organizationEventsCount,+ deps.config.selfHosted),},});- if (!isSelfHosted()) {+ if (!deps.config.selfHosted) {try {await sendUsageAlerts(deps, organization, organizationEventsCount);} catch (error) {@@ -169,7 +167,7 @@ async function sendUsageAlerts(include: { user: { select: { email: true, firstName: true } } },});- const billingUrl = `${process.env.DASHBOARD_URL ?? DEFAULT_DASHBOARD_URL}/${organization.id}/billing`;+ const billingUrl = `${deps.config.dashboardUrl || DEFAULT_DASHBOARD_URL}/${organization.id}/billing`;const recipients = new Map<string, string | undefined>();for (const member of admins) {if (member.user?.email) {diff --git a/packages/core/src/modules/subscription/subscription.service.ts b/packages/core/src/modules/subscription/subscription.service.tsindex 160150948..ded5228a8 100644--- a/packages/core/src/modules/subscription/subscription.service.ts+++ b/packages/core/src/modules/subscription/subscription.service.ts@@ -314,7 +314,7 @@ export async function applySaveDiscount(const { requireOrganizationAdmin } = await getAccessChecks();await requireOrganizationAdmin({ userId, organizationId });- const discountId = process.env.POLAR_SAVE_DISCOUNT_ID;+ const discountId = deps.config.polar.saveDiscountId;if (!discountId) {throw new TRPCBadRequestError('Save discount is not configured');}@@ -674,7 +674,7 @@ export async function handlePolarWebhookEvent(event = validatePolarEvent(rawBody,headers,- process.env.POLAR_WEBHOOK_SECRET ?? ''+ deps.config.polar.webhookSecret ?? '');} catch (err) {logger.error({ err }, 'polar webhook: failed to parse event');diff --git a/packages/core/src/modules/tools/src/ip-lookup.ts b/packages/core/src/modules/tools/src/ip-lookup.tsindex 1db5740c8..b6b350573 100644--- a/packages/core/src/modules/tools/src/ip-lookup.ts+++ b/packages/core/src/modules/tools/src/ip-lookup.ts@@ -2,6 +2,7 @@// `ipLookup` (M7-008).import { getGeoLocation } from '../../../clients/geo';+import type { CoreConfig } from '../../../config';import type { Logger } from '../../../logger';import { getClientIpFromHeaders } from '../../../shared/get-client-ip';import { checkRateLimit } from './rate-limit';@@ -65,11 +66,12 @@ function isPrivateIP(ip: string): boolean {}export async function runIpLookup(+ config: CoreConfig,ipParam: string | undefined,headers: Record<string, string | string[] | undefined> | Headers,logger?: Pick<Logger, 'error'>): Promise<IpLookupOutcome> {- const { ip: clientIp } = getClientIpFromHeaders(headers);+ const { ip: clientIp } = getClientIpFromHeaders(config.ipHeaders, headers);if (clientIp &&!checkRateLimit(`ip:${clientIp}`, IP_LOOKUP_WINDOW_MS, IP_LOOKUP_MAX)diff --git a/packages/core/src/modules/tools/src/site-checker.ts b/packages/core/src/modules/tools/src/site-checker.tsindex debddb51e..40ae0a7ed 100644--- a/packages/core/src/modules/tools/src/site-checker.ts+++ b/packages/core/src/modules/tools/src/site-checker.ts@@ -6,6 +6,7 @@ import * as net from 'node:net';import * as tls from 'node:tls';import * as cheerio from 'cheerio';import { getGeoLocation } from '../../../clients/geo';+import type { CoreConfig } from '../../../config';import type { Logger } from '../../../logger';import { getClientIpFromHeaders } from '../../../shared/get-client-ip';import {@@ -508,6 +509,7 @@ function calculateSecurityScore(security: SiteCheckResult['security']): number {}export async function runSiteCheck(+ config: CoreConfig,urlParam: string | undefined,headers: Record<string, string | string[] | undefined> | Headers,logger?: Pick<Logger, 'error'>@@ -516,7 +518,7 @@ export async function runSiteCheck(return { status: 400, error: 'URL parameter is required' };}- const { ip } = getClientIpFromHeaders(headers);+ const { ip } = getClientIpFromHeaders(config.ipHeaders, headers);if (ip &&!checkRateLimit(`site:${ip}`, SITE_CHECK_WINDOW_MS, SITE_CHECK_MAX)diff --git a/packages/core/src/modules/tools/tools.routes.ts b/packages/core/src/modules/tools/tools.routes.tsindex b66d3e0d6..cc6c3dcc7 100644--- a/packages/core/src/modules/tools/tools.routes.ts+++ b/packages/core/src/modules/tools/tools.routes.ts@@ -20,7 +20,12 @@ export const toolsRoutes = defineRoutes((app) =>.get('/site-checker',async ({ query, ctx, status }) => {- const outcome = await runSiteCheck(query.url, ctx.headers, ctx.logger);+ const outcome = await runSiteCheck(+ ctx.config,+ query.url,+ ctx.headers,+ ctx.logger+ );if (outcome.status !== 200) {return status(outcome.status, { error: outcome.error });}@@ -34,7 +39,12 @@ export const toolsRoutes = defineRoutes((app) =>.get('/ip-lookup',async ({ query, ctx, status }) => {- const outcome = await runIpLookup(query.ip, ctx.headers, ctx.logger);+ const outcome = await runIpLookup(+ ctx.config,+ query.ip,+ ctx.headers,+ ctx.logger+ );if (outcome.status !== 200) {return status(outcome.status, { error: outcome.error });}diff --git a/packages/core/src/modules/user/user.rpc.ts b/packages/core/src/modules/user/user.rpc.tsindex 0f49c7b3b..a6fa8d596 100644--- a/packages/core/src/modules/user/user.rpc.ts+++ b/packages/core/src/modules/user/user.rpc.ts@@ -31,7 +31,7 @@ export const userRouter = createTRPCRouter({delete: protectedProcedure.mutation(async ({ ctx }) => {const userId = requireLogin(ctx.session.userId);await ctx.services.user.deleteUserAccount(userId);- deleteSessionTokenCookie(ctx.setCookie);+ deleteSessionTokenCookie(ctx.config, ctx.setCookie);return true;}),diff --git a/packages/core/src/rpc.router.test.ts b/packages/core/src/rpc.router.test.tsindex 844fa585c..b308b3ff2 100644--- a/packages/core/src/rpc.router.test.ts+++ b/packages/core/src/rpc.router.test.ts@@ -1,4 +1,5 @@import { expect, test } from 'bun:test';+import { testCoreConfig } from '../test/config-fixture';import { stubHttpCtx } from '../test/rpc-fixtures';import { createTrpcFetchHandler } from './rpc/handler';import { appRouter } from './rpc.router';@@ -18,6 +19,7 @@ test('the composed appRouter serves the health procedure', async () => {router: appRouter,logger,cookieOptions: COOKIE_OPTIONS,+ ipHeaders: testCoreConfig().ipHeaders,});const response = await handler(diff --git a/packages/core/src/rpc/base.test.ts b/packages/core/src/rpc/base.test.tsindex a899dc3da..0cc328434 100644--- a/packages/core/src/rpc/base.test.ts+++ b/packages/core/src/rpc/base.test.ts@@ -1,6 +1,7 @@import { expect, test } from 'bun:test';import { TRPCError } from '@trpc/server';import { z } from 'zod';+import { testCoreConfig } from '../../test/config-fixture';import { stubHttpCtx, TEST_SESSION } from '../../test/rpc-fixtures';import type { CookieOptions } from '../shared/cookie';import * as baseModule from './base';@@ -201,6 +202,7 @@ function mountWith(router,logger,cookieOptions: COOKIE_OPTIONS,+ ipHeaders: testCoreConfig().ipHeaders,});return {logger,@@ -334,6 +336,7 @@ test('the rate limit middleware hands the limiter ctx fields, not a request', asrouter,logger,cookieOptions: COOKIE_OPTIONS,+ ipHeaders: testCoreConfig().ipHeaders,});await handler(postTo('signIn'), ctx);@@ -369,6 +372,7 @@ test('a limiter rejection stops the procedure and surfaces as 429', async () =>router,logger,cookieOptions: COOKIE_OPTIONS,+ ipHeaders: testCoreConfig().ipHeaders,});const response = await handler(postTo('signIn'), ctx);diff --git a/packages/core/src/rpc/handler.test.ts b/packages/core/src/rpc/handler.test.tsindex e3f0fdb2d..81c812e50 100644--- a/packages/core/src/rpc/handler.test.ts+++ b/packages/core/src/rpc/handler.test.ts@@ -1,6 +1,7 @@import { expect, test } from 'bun:test';import { TRPCError } from '@trpc/server';import { z } from 'zod';+import { testCoreConfig } from '../../test/config-fixture';import {capturingLogger,stubHttpCtx,@@ -45,7 +46,10 @@ function report(overrides: Partial<TrpcErrorReport> = {}): TrpcErrorReport {test('UNAUTHORIZED on organization.list is dropped', () => {const logger = capturingLogger();- createTrpcOnError(logger)(+ createTrpcOnError(+ logger,+ testCoreConfig().ipHeaders+ )(report({error: new TRPCError({ code: 'UNAUTHORIZED' }),path: 'organization.list',@@ -58,7 +62,10 @@ test('UNAUTHORIZED on organization.list is dropped', () => {test('the drop is scoped to that one path', () => {const logger = capturingLogger();- createTrpcOnError(logger)(+ createTrpcOnError(+ logger,+ testCoreConfig().ipHeaders+ )(report({error: new TRPCError({ code: 'UNAUTHORIZED' }),path: 'organization.get',@@ -72,9 +79,10 @@ test('the drop is scoped to that one path', () => {test('TOO_MANY_REQUESTS logs at warn as "trpc rate limited"', () => {const logger = capturingLogger();- createTrpcOnError(logger)(- report({ error: new TRPCError({ code: 'TOO_MANY_REQUESTS' }) })- );+ createTrpcOnError(+ logger,+ testCoreConfig().ipHeaders+ )(report({ error: new TRPCError({ code: 'TOO_MANY_REQUESTS' }) }));expect(logger.lines).toHaveLength(1);expect(logger.lines[0]?.level).toBe('warn');@@ -85,7 +93,7 @@ test('everything else logs at error as "trpc error", with the V1 fields', () =>const logger = capturingLogger();const failure = report();- createTrpcOnError(logger)(failure);+ createTrpcOnError(logger, testCoreConfig().ipHeaders)(failure);expect(logger.lines).toHaveLength(1);const [line] = logger.lines;@@ -110,7 +118,10 @@ test('the request logger is preferred and the resolved session is carried', asyncookieOptions: COOKIE_OPTIONS,});- createTrpcOnError(bootLogger)(report({ ctx: trpcCtx }));+ createTrpcOnError(+ bootLogger,+ testCoreConfig().ipHeaders+ )(report({ ctx: trpcCtx }));expect(bootLogger.lines).toHaveLength(0);expect(requestLogger.lines).toHaveLength(1);@@ -123,7 +134,10 @@ test('a ctx-less failure (createContext threw) logs through the boot logger', ()const bootLogger = capturingLogger();expect(() =>- createTrpcOnError(bootLogger)(report({ ctx: undefined }))+ createTrpcOnError(+ bootLogger,+ testCoreConfig().ipHeaders+ )(report({ ctx: undefined }))).not.toThrow();expect(bootLogger.lines).toHaveLength(1);@@ -133,7 +147,10 @@ test('a ctx-less failure (createContext threw) logs through the boot logger', ()test('an untrusted forwarded ip is ignored', () => {const logger = capturingLogger();- createTrpcOnError(logger)(+ createTrpcOnError(+ logger,+ testCoreConfig().ipHeaders+ )(report({req: new Request('https://api.openpanel.dev/trpc/report.get', {headers: { 'x-client-ip': '198.51.100.9' },@@ -170,6 +187,7 @@ function mount() {router,logger,cookieOptions: COOKIE_OPTIONS,+ ipHeaders: testCoreConfig().ipHeaders,});return { logger, call: (request: Request) => handler(request, ctx) };}diff --git a/packages/core/src/rpc/handler.ts b/packages/core/src/rpc/handler.tsindex eeb57e5ef..52d4a69ee 100644--- a/packages/core/src/rpc/handler.ts+++ b/packages/core/src/rpc/handler.ts@@ -9,6 +9,7 @@import type { AnyRouter, TRPCError } from '@trpc/server';import { fetchRequestHandler } from '@trpc/server/adapters/fetch';+import type { IpHeaderConfig } from '../config';import type { HttpCtx } from '../context';import type { Logger } from '../logger';import { getTrustedIpFromHeaders } from '../shared/get-client-ip';@@ -43,7 +44,10 @@ const SILENCED_UNAUTHORIZED_PATH = 'organization.list';* no `ctx` then, and a naive `ctx.logger.error(...)` would turn a* context-construction failure into a second throw inside the error handler.*/-export function createTrpcOnError(bootLogger: Logger) {+export function createTrpcOnError(+ bootLogger: Logger,+ ipHeaders: IpHeaderConfig+) {return function onError(report: TrpcErrorReport): void {if (report.error.code === 'UNAUTHORIZED' &&@@ -55,7 +59,10 @@ export function createTrpcOnError(bootLogger: Logger) {// The IP is resolved from trusted headers only (see// `getTrustedIpFromHeaders`), so it is the address to hand to// Cloudflare when an abuser needs blocking at the edge.- const { ip, header } = getTrustedIpFromHeaders(report.req.headers);+ const { ip, header } = getTrustedIpFromHeaders(+ ipHeaders,+ report.req.headers+ );const payload = {err: report.error,path: report.path,@@ -84,6 +91,8 @@ export interface TrpcFetchHandlerOptions extends TrpcContextOptions {router: AnyRouter;/** Used only when `createContext` threw. Every other line goes to `ctx.logger`. */logger: Logger;+ /** `onError` resolves the abuser's IP from trusted headers only. */+ ipHeaders: IpHeaderConfig;endpoint?: string;}@@ -96,10 +105,11 @@ export function createTrpcFetchHandler(options: TrpcFetchHandlerOptions) {const {router,logger,+ ipHeaders,endpoint = TRPC_ENDPOINT,...contextOptions} = options;- const onError = createTrpcOnError(logger);+ const onError = createTrpcOnError(logger, ipHeaders);return (request: Request, ctx: HttpCtx): Promise<Response> =>fetchRequestHandler({diff --git a/packages/core/src/services.ts b/packages/core/src/services.tsindex f8df6bcfc..55e2f0f2b 100644--- a/packages/core/src/services.ts+++ b/packages/core/src/services.ts@@ -43,7 +43,7 @@ import { createUserService } from './modules/user/user.service';/** What every service factory receives — derived from Ctx, so it cannot drift. */export type ServiceDeps = Pick<Ctx,- 'db' | 'ch' | 'redis' | 'clients' | 'buffers' | 'logger' | 'queues'+ 'db' | 'ch' | 'redis' | 'clients' | 'buffers' | 'logger' | 'queues' | 'config'>;// Two type rules the compiler enforces but cannot explain:diff --git a/packages/core/src/shared/ch-tables.parity.test.ts b/packages/core/src/shared/ch-tables.parity.test.tsindex 494131367..dd2fba1a2 100644--- a/packages/core/src/shared/ch-tables.parity.test.ts+++ b/packages/core/src/shared/ch-tables.parity.test.ts@@ -9,24 +9,21 @@import { describe, expect, it } from 'bun:test';import * as dbClient from '@openpanel/db/src/clickhouse/client';-import {- getReplicatedTableName,- isClickhouseClustered,- TABLE_NAMES,-} from './ch-tables';+import { getReplicatedTableName, 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', () => {+ // The cluster VERDICT is the config loader's since M15-006+ // (`config.clickhouseClustered` = CLICKHOUSE_CLUSTER, else not SELF_HOSTED);+ // @openpanel/db still reads it itself. Feeding db's verdict in is what makes+ // this a parity check of the NAME derivation rather than of two env reads.+ it('derives the same mutation table name for the same verdict', () => {+ const clustered = dbClient.isClickhouseClustered();for (const name of Object.values(TABLE_NAMES)) {- expect(getReplicatedTableName(name)).toBe(+ expect(getReplicatedTableName(clustered, name)).toBe(dbClient.getReplicatedTableName(name));}diff --git a/packages/core/src/shared/ch-tables.ts b/packages/core/src/shared/ch-tables.tsindex b4f301900..66c3f76cb 100644--- a/packages/core/src/shared/ch-tables.ts+++ b/packages/core/src/shared/ch-tables.ts@@ -41,28 +41,19 @@ export const TABLE_NAMES = {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.+ *+ * Clustered mode = production (not self-hosted); non-clustered = self-hosted.+ * The verdict is `config.clickhouseClustered`, resolved once by the config+ * loader from CLICKHOUSE_CLUSTER and SELF_HOSTED.*/-export function getReplicatedTableName(tableName: string): string {- if (isClickhouseClustered()) {+export function getReplicatedTableName(+ clustered: boolean,+ tableName: string+): string {+ if (clustered) {return `${tableName}_replicated ON CLUSTER '{cluster}'`;}return tableName;diff --git a/packages/core/src/shared/encryption.test.ts b/packages/core/src/shared/encryption.test.tsindex 1a7bc833e..c471d6e64 100644--- a/packages/core/src/shared/encryption.test.ts+++ b/packages/core/src/shared/encryption.test.ts@@ -1,4 +1,4 @@-import { beforeAll, describe, expect, it } from 'bun:test';+import { describe, expect, it } from 'bun:test';import {decrypt,decryptCredential,@@ -7,42 +7,41 @@ import {isEncrypted,} from './encryption';-beforeAll(() => {- // Deterministic key for the round-trips (overrides any ambient value).- process.env.ENCRYPTION_KEY = 'a'.repeat(64);-});+// Deterministic key for the round-trips — the value the config loader would+// hand down as `config.encryptionKey`.+const KEY = 'a'.repeat(64);describe('encryption (single ENCRYPTION_KEY)', () => {it('encrypt/decrypt round-trips without a prefix (TOTP/GSC format)', () => {const secret = 'totp-or-gsc-secret';- const enc = encrypt(secret);+ const enc = encrypt(KEY, secret);expect(isEncrypted(enc)).toBe(false);expect(enc).not.toBe(secret);- expect(decrypt(enc)).toBe(secret);+ expect(decrypt(KEY, enc)).toBe(secret);});it('encryptCredential/decryptCredential round-trips with the enc: prefix', () => {const secret = 'aws-secret-access-key';- const enc = encryptCredential(secret);+ const enc = encryptCredential(KEY, secret);expect(isEncrypted(enc)).toBe(true);- expect(decryptCredential(enc)).toBe(secret);+ expect(decryptCredential(KEY, enc)).toBe(secret);});it('encryptCredential is idempotent (never double-encrypts)', () => {- const enc = encryptCredential('x');- expect(encryptCredential(enc)).toBe(enc);+ const enc = encryptCredential(KEY, 'x');+ expect(encryptCredential(KEY, enc)).toBe(enc);});it('decryptCredential passes plaintext through (test-connection flow)', () => {- expect(decryptCredential('plaintext')).toBe('plaintext');+ expect(decryptCredential(KEY, 'plaintext')).toBe('plaintext');});it('decrypt round-trips multi-byte UTF-8 across cipher chunk boundaries', () => {// GCM is a stream cipher: update() can return a chunk ending mid-character,// so decoding per chunk instead of after concatenation corrupts the value.const secret = `${'ä'.repeat(500)}🔐日本語`;- expect(decrypt(encrypt(secret))).toBe(secret);- expect(decryptCredential(encryptCredential(secret))).toBe(secret);+ expect(decrypt(KEY, encrypt(KEY, secret))).toBe(secret);+ expect(decryptCredential(KEY, encryptCredential(KEY, secret))).toBe(secret);});it('decrypt throws a guarded error for a too-short/malformed ciphertext', () => {@@ -51,10 +50,10 @@ describe('encryption (single ENCRYPTION_KEY)', () => {// bare TypeError before this guard existed; the guard normalizes that// to the same descriptive error on both runtimes.const tooShort = Buffer.from('short').toString('base64');- expect(() => decrypt(tooShort)).toThrow(+ expect(() => decrypt(KEY, tooShort)).toThrow('Invalid encrypted value: expected a 16-byte auth tag, got 0');- expect(() => decrypt('')).toThrow(+ expect(() => decrypt(KEY, '')).toThrow('Invalid encrypted value: expected a 16-byte auth tag, got 0');});diff --git a/packages/core/src/shared/encryption.ts b/packages/core/src/shared/encryption.tsindex 3865d1b0c..321220053 100644--- a/packages/core/src/shared/encryption.ts+++ b/packages/core/src/shared/encryption.ts@@ -6,14 +6,19 @@ const IV_LENGTH = 12; // 96 bits for GCMconst AUTH_TAG_LENGTH = 16; // 128 bitsconst ENCODING = 'base64';+/** The hex ENCRYPTION_KEY as it arrives from the config loader. */+export type EncryptionKey = string | undefined;+/*** Single symmetric key for all at-rest encryption (TOTP secrets, GSC tokens,* integration credentials). Must be 32 bytes (64 hex characters).* Generate with: openssl rand -hex 32+ *+ * Validated here rather than at boot: a deployment that never encrypts+ * anything has always been allowed to run without the key, and only the first+ * call that needs it fails.*/-function getEncryptionKey(): Buffer {- const keyHex = process.env.ENCRYPTION_KEY;-+function getEncryptionKey(keyHex: EncryptionKey): Buffer {if (!keyHex) {throw new Error('ENCRYPTION_KEY environment variable is not set');}@@ -33,10 +38,10 @@ function getEncryptionKey(): Buffer {// ciphertext keeps decrypting; this is the implementation db re-exports.)// ----------------------------------------------------------------------------export function encrypt(plaintext: string): string {- const key = getEncryptionKey();+export function encrypt(key: EncryptionKey, plaintext: string): string {+ const encryptionKey = getEncryptionKey(key);const iv = randomBytes(IV_LENGTH);- const cipher = createCipheriv(ALGORITHM, key, iv);+ const cipher = createCipheriv(ALGORITHM, encryptionKey, iv);const encrypted = Buffer.concat([cipher.update(plaintext, 'utf8'),cipher.final(),@@ -45,8 +50,8 @@ export function encrypt(plaintext: string): string {return Buffer.concat([iv, tag, encrypted]).toString(ENCODING);}-export function decrypt(ciphertext: string): string {- const key = getEncryptionKey();+export function decrypt(key: EncryptionKey, ciphertext: string): string {+ const encryptionKey = getEncryptionKey(key);const buf = Buffer.from(ciphertext, ENCODING);const iv = buf.subarray(0, IV_LENGTH);const tag = buf.subarray(IV_LENGTH, IV_LENGTH + AUTH_TAG_LENGTH);@@ -59,7 +64,7 @@ export function decrypt(ciphertext: string): string {`Invalid encrypted value: expected a ${AUTH_TAG_LENGTH}-byte auth tag, got ${tag.length}`);}- const decipher = createDecipheriv(ALGORITHM, key, iv);+ const decipher = createDecipheriv(ALGORITHM, encryptionKey, iv);decipher.setAuthTag(tag);// Concatenate first, decode once: GCM is a stream cipher, so update() can end// mid-way through a multi-byte UTF-8 sequence and a per-chunk toString would@@ -80,7 +85,10 @@ export function isEncrypted(value: string): boolean {return value.startsWith(ENCRYPTION_PREFIX);}-export function encryptCredential(plaintext: string): string {+export function encryptCredential(+ key: EncryptionKey,+ plaintext: string+): string {if (!plaintext) {return plaintext;}@@ -89,10 +97,10 @@ export function encryptCredential(plaintext: string): string {return plaintext;}- const key = getEncryptionKey();+ const encryptionKey = getEncryptionKey(key);const iv = randomBytes(IV_LENGTH);- const cipher = createCipheriv(ALGORITHM, key, iv, {+ const cipher = createCipheriv(ALGORITHM, encryptionKey, iv, {authTagLength: AUTH_TAG_LENGTH,});@@ -109,7 +117,10 @@ export function encryptCredential(plaintext: string): string {return ENCRYPTION_PREFIX + combined.toString('base64');}-export function decryptCredential(ciphertext: string): string {+export function decryptCredential(+ key: EncryptionKey,+ ciphertext: string+): string {if (!ciphertext) {return ciphertext;}@@ -118,7 +129,7 @@ export function decryptCredential(ciphertext: string): string {return ciphertext;}- const key = getEncryptionKey();+ const encryptionKey = getEncryptionKey(key);// Remove prefix and decode base64const combined = Buffer.from(@@ -138,7 +149,7 @@ export function decryptCredential(ciphertext: string): string {combined.length - AUTH_TAG_LENGTH);- const decipher = createDecipheriv(ALGORITHM, key, iv, {+ const decipher = createDecipheriv(ALGORITHM, encryptionKey, iv, {authTagLength: AUTH_TAG_LENGTH,});diff --git a/packages/core/src/shared/get-client-ip.test.ts b/packages/core/src/shared/get-client-ip.test.tsindex ef9972a9f..50773d689 100644--- a/packages/core/src/shared/get-client-ip.test.ts+++ b/packages/core/src/shared/get-client-ip.test.ts@@ -1,9 +1,16 @@import { describe, expect, it } from 'bun:test';+import type { IpHeaderConfig } from '../config';import {getClientIpFromHeaders,getTrustedIpFromHeaders,} from './get-client-ip';+/** IP_HEADER_ORDER / TRUSTED_IP_HEADER_ORDER unset: the shipped orders stand. */+const DEFAULT_ORDERS: IpHeaderConfig = {+ attributionOrder: undefined,+ trustedOrder: undefined,+};+describe('getTrustedIpFromHeaders', () => {it('prefers cf-connecting-ip over anything the caller can set', () => {// Verified against production: Cloudflare overwrites cf-connecting-ip, but@@ -14,19 +21,19 @@ describe('getTrustedIpFromHeaders', () => {'cf-connecting-ip': '98.128.229.56',};- expect(getTrustedIpFromHeaders(headers)).toEqual({+ expect(getTrustedIpFromHeaders(DEFAULT_ORDERS, headers)).toEqual({ip: '98.128.229.56',header: 'cf-connecting-ip',});// The analytics resolver deliberately trusts the caller - that is the// difference the two functions exist for.- expect(getClientIpFromHeaders(headers).ip).toBe('9.9.9.9');+ expect(getClientIpFromHeaders(DEFAULT_ORDERS, headers).ip).toBe('9.9.9.9');});it('never returns a client-forwarded header', () => {expect(- getTrustedIpFromHeaders({+ getTrustedIpFromHeaders(DEFAULT_ORDERS, {'openpanel-client-ip': '9.9.9.9','x-client-ip': '7.7.7.7','true-client-ip': '6.6.6.6',@@ -36,18 +43,23 @@ describe('getTrustedIpFromHeaders', () => {it('takes the last x-forwarded-for entry, the one the proxy appended', () => {expect(- getTrustedIpFromHeaders({ 'x-forwarded-for': '8.8.8.8, 1.2.3.4' })+ getTrustedIpFromHeaders(DEFAULT_ORDERS, {+ 'x-forwarded-for': '8.8.8.8, 1.2.3.4',+ })).toEqual({ ip: '1.2.3.4', header: 'x-forwarded-for' });});it('falls back to the socket address, private ranges included', () => {- expect(getTrustedIpFromHeaders({}, '127.0.0.1')).toEqual({+ expect(getTrustedIpFromHeaders(DEFAULT_ORDERS, {}, '127.0.0.1')).toEqual({ip: '127.0.0.1',header: 'socket',});});it('returns nothing when there is neither a trusted header nor a socket', () => {- expect(getTrustedIpFromHeaders({})).toEqual({ ip: '', header: '' });+ expect(getTrustedIpFromHeaders(DEFAULT_ORDERS, {})).toEqual({+ ip: '',+ header: '',+ });});});diff --git a/packages/core/src/shared/get-client-ip.ts b/packages/core/src/shared/get-client-ip.tsindex 86504a463..85b171e24 100644--- a/packages/core/src/shared/get-client-ip.ts+++ b/packages/core/src/shared/get-client-ip.ts@@ -1,9 +1,10 @@/*** Get client IP from headers*- * Can be configured via IP_HEADER_ORDER env variable- * Example: IP_HEADER_ORDER="cf-connecting-ip,x-real-ip,x-forwarded-for"+ * Both orders are configurable per deployment (IP_HEADER_ORDER,+ * TRUSTED_IP_HEADER_ORDER); the parsed lists arrive as `config.ipHeaders`.*/+import type { IpHeaderConfig } from '../config';// Order matters: client-forwarded headers (set explicitly by SDKs/upstream// servers) must come before infrastructure headers like `cf-connecting-ip`.@@ -79,13 +80,6 @@ function isPublicIp(ip: string): boolean {return true;}-function getHeaderOrder(): string[] {- if (typeof process !== 'undefined' && process.env?.IP_HEADER_ORDER) {- return process.env.IP_HEADER_ORDER.split(',').map((h) => h.trim());- }- return DEFAULT_IP_HEADER_ORDER;-}-function isValidIp(ip: string): boolean {// Basic IP validationconst ipv4 = /^(\d{1,3}\.){3}\d{1,3}$/;@@ -94,13 +88,14 @@ function isValidIp(ip: string): boolean {}export function getClientIpFromHeaders(+ config: IpHeaderConfig,headers: Record<string, string | string[] | undefined> | Headers,overrideHeaderName?: string): {ip: string;header: string;} {- let headerOrder = getHeaderOrder();+ let headerOrder = config.attributionOrder ?? DEFAULT_IP_HEADER_ORDER;if (overrideHeaderName) {headerOrder = [overrideHeaderName];@@ -179,12 +174,11 @@ export const TRUSTED_IP_HEADER_ORDER = [* (comma separated) - set it to the single header your edge guarantees.*/export function getTrustedIpFromHeaders(+ config: IpHeaderConfig,headers: Record<string, string | string[] | undefined> | Headers,socketIp?: string): { ip: string; header: string } {- const headerOrder = process.env.TRUSTED_IP_HEADER_ORDER- ? process.env.TRUSTED_IP_HEADER_ORDER.split(',').map((h) => h.trim())- : TRUSTED_IP_HEADER_ORDER;+ const headerOrder = config.trustedOrder ?? TRUSTED_IP_HEADER_ORDER;for (const headerName of headerOrder) {let value: string | null = null;diff --git a/packages/core/src/shared/lookback.test.ts b/packages/core/src/shared/lookback.test.tsdeleted file mode 100644index 942755743..000000000--- a/packages/core/src/shared/lookback.test.ts+++ /dev/null@@ -1,70 +0,0 @@-import { afterEach, describe, expect, it } from 'bun:test';-import { resolveMaxLookbackDays } from './lookback';--function withEnv(name: string, value: string | undefined, fn: () => void) {- const previous = process.env[name];- if (value === undefined) {- delete process.env[name];- } else {- process.env[name] = value;- }- try {- fn();- } finally {- if (previous === undefined) {- delete process.env[name];- } else {- process.env[name] = previous;- }- }-}--describe('resolveMaxLookbackDays', () => {- afterEach(() => {- delete process.env.EVENT_LIST_MAX_LOOKBACK_DAYS;- delete process.env.SESSION_LIST_MAX_LOOKBACK_DAYS;- });-- it('returns the caller default when unset', () => {- withEnv('EVENT_LIST_MAX_LOOKBACK_DAYS', '', () => {- expect(- resolveMaxLookbackDays('EVENT_LIST_MAX_LOOKBACK_DAYS', 365 * 5)- ).toBe(365 * 5);- expect(- resolveMaxLookbackDays('SESSION_LIST_MAX_LOOKBACK_DAYS', 365)- ).toBe(365);- });- });-- it('replaces the ceiling when set to a positive integer', () => {- withEnv('EVENT_LIST_MAX_LOOKBACK_DAYS', '7', () => {- expect(- resolveMaxLookbackDays('EVENT_LIST_MAX_LOOKBACK_DAYS', 365 * 5)- ).toBe(7);- });- // Raising past the default is allowed too — it's a ceiling, not a min.- withEnv('SESSION_LIST_MAX_LOOKBACK_DAYS', '3650', () => {- expect(- resolveMaxLookbackDays('SESSION_LIST_MAX_LOOKBACK_DAYS', 365)- ).toBe(3650);- });- });-- it('reads each list its own variable — no cross-talk', () => {- withEnv('EVENT_LIST_MAX_LOOKBACK_DAYS', '7', () => {- expect(- resolveMaxLookbackDays('SESSION_LIST_MAX_LOOKBACK_DAYS', 365)- ).toBe(365);- });- });-- it('falls back to the default for malformed values', () => {- for (const bad of ['0', '-1', '7.5', '7days', 'junk']) {- withEnv('EVENT_LIST_MAX_LOOKBACK_DAYS', bad, () => {- expect(- resolveMaxLookbackDays('EVENT_LIST_MAX_LOOKBACK_DAYS', 365)- ).toBe(365);- });- }- });-});diff --git a/packages/core/src/shared/lookback.ts b/packages/core/src/shared/lookback.tsdeleted file mode 100644index bb16da2c3..000000000--- a/packages/core/src/shared/lookback.ts+++ /dev/null@@ -1,30 +0,0 @@-/**- * Ceiling (in days) for the empty-result lookback expansion on the event and- * session lists.- *- * When a cursor window matches nothing, those lists double the window and- * retry until the ceiling is reached. The doubling geometry keeps the total- * cost at roughly 2x the final window, so the ceiling is what actually- * bounds the worst case: with a filter no index can prune (e.g. a- * properties value) that matches nothing at all, every step is a full scan- * of its window, and a multi-year ceiling turns one page view into minutes- * of scanning.- *- * Each list has its own variable (EVENT_LIST_MAX_LOOKBACK_DAYS,- * SESSION_LIST_MAX_LOOKBACK_DAYS) because their built-in ceilings differ —- * the surfaces have different cost profiles and a shared knob would couple- * them. Values are human-sized day counts (1 / 7 / 365 / ...); unset or- * invalid (non-integer, zero, negative) keeps the caller's default, so- * behavior is unchanged unless a variable is set.- */-export function resolveMaxLookbackDays(- envName: string,- defaultDays: number-): number {- const raw = process.env[envName];- if (!(raw && /^\d+$/.test(raw))) {- return defaultDays;- }- const parsed = Number(raw);- return Number.isSafeInteger(parsed) && parsed > 0 ? parsed : defaultDays;-}diff --git a/packages/core/src/shared/ssrf.test.ts b/packages/core/src/shared/ssrf.test.tsindex 8a9682ae8..aaf0161e9 100644--- a/packages/core/src/shared/ssrf.test.ts+++ b/packages/core/src/shared/ssrf.test.ts@@ -1,61 +1,41 @@-import { afterEach, describe, expect, it, mock } from 'bun:test';+import { describe, expect, it, mock } from 'bun:test';import { assertSafeUrl, createPinnedLookup } from './ssrf';-describe('assertSafeUrl', () => {- const original = process.env.SELF_HOSTED;- afterEach(() => {- // Assigning undefined would leave the string "undefined" behind, which is- // truthy — restore by deleting instead.- if (original === undefined) {- delete process.env.SELF_HOSTED;- } else {- process.env.SELF_HOSTED = original;- }- });+// SELF_HOSTED arrives as `config.selfHosted`; the loader is what decides that+// only `true`/`1` mean self-hosted (`apps/api/src/config/env.test.ts`).+const CLOUD = false;+const SELF_HOSTED = true;+describe('assertSafeUrl', () => {it('rejects non-http(s) schemes on the cloud', async () => {- process.env.SELF_HOSTED = '';- await expect(assertSafeUrl('ftp://example.com')).rejects.toThrow();+ await expect(assertSafeUrl(CLOUD, 'ftp://example.com')).rejects.toThrow();});it('rejects malformed URLs', async () => {- process.env.SELF_HOSTED = '';- await expect(assertSafeUrl('not a url')).rejects.toThrow();+ await expect(assertSafeUrl(CLOUD, 'not a url')).rejects.toThrow();});it('rejects literal private / metadata hosts on the cloud', async () => {- process.env.SELF_HOSTED = '';- await expect(assertSafeUrl('http://127.0.0.1/x')).rejects.toThrow();- await expect(assertSafeUrl('http://10.0.0.5/x')).rejects.toThrow();+ await expect(assertSafeUrl(CLOUD, 'http://127.0.0.1/x')).rejects.toThrow();+ await expect(assertSafeUrl(CLOUD, 'http://10.0.0.5/x')).rejects.toThrow();await expect(- assertSafeUrl('http://169.254.169.254/latest/meta-data/')+ assertSafeUrl(CLOUD, 'http://169.254.169.254/latest/meta-data/')).rejects.toThrow();- await expect(assertSafeUrl('http://[::1]/x')).rejects.toThrow();+ await expect(assertSafeUrl(CLOUD, 'http://[::1]/x')).rejects.toThrow();});it('is a no-op on self-hosted (operator controls the network)', async () => {- process.env.SELF_HOSTED = 'true';- await expect(assertSafeUrl('http://127.0.0.1/x')).resolves.toBeNull();- });-- it('only treats "true"/"1" as self-hosted', async () => {- // Bare truthiness would read SELF_HOSTED="false" as self-hosted and drop- // the guard on the cloud.- for (const value of ['false', '0', 'no']) {- process.env.SELF_HOSTED = value;- await expect(assertSafeUrl('http://127.0.0.1/x')).rejects.toThrow();- }- process.env.SELF_HOSTED = '1';- await expect(assertSafeUrl('http://127.0.0.1/x')).resolves.toBeNull();+ await expect(+ assertSafeUrl(SELF_HOSTED, 'http://127.0.0.1/x')+ ).resolves.toBeNull();});it('returns the validated addresses so the caller can pin to them', async () => {// Validating without pinning is check-then-connect: a client that resolves// the hostname again can be steered elsewhere by a changed DNS answer.- process.env.SELF_HOSTED = '';- await expect(assertSafeUrl('http://93.184.216.34/x')).resolves.toEqual([- '93.184.216.34',- ]);+ await expect(+ assertSafeUrl(CLOUD, 'http://93.184.216.34/x')+ ).resolves.toEqual(['93.184.216.34']);});});diff --git a/packages/core/src/shared/ssrf.ts b/packages/core/src/shared/ssrf.tsindex fe974b9e1..4213d2a02 100644--- a/packages/core/src/shared/ssrf.ts+++ b/packages/core/src/shared/ssrf.ts@@ -19,10 +19,11 @@ export { createPinnedLookup } from './safe-fetch';* or webhook receiver) is a legitimate, pre-existing use. The guard exists to* stop cross-tenant SSRF on the managed/multi-tenant cloud.*/-export async function assertSafeUrl(rawUrl: string): Promise<string[] | null> {- // Compare explicitly: bare truthiness would treat SELF_HOSTED="false" as- // self-hosted and silently drop the guard on the cloud.- if (process.env.SELF_HOSTED === 'true' || process.env.SELF_HOSTED === '1') {+export async function assertSafeUrl(+ selfHosted: boolean,+ rawUrl: string+): Promise<string[] | null> {+ if (selfHosted) {return null;}diff --git a/packages/core/test/buffer-fixtures.ts b/packages/core/test/buffer-fixtures.tsindex d3816affa..d04535432 100644--- a/packages/core/test/buffer-fixtures.ts+++ b/packages/core/test/buffer-fixtures.ts@@ -3,6 +3,7 @@import type { BufferDeps } from '../src/buffers/base-buffer';import type { Logger } from '../src/logger';+import { testCoreConfig } from './config-fixture';const silentLogger: Logger = {fatal: () => undefined,@@ -35,11 +36,16 @@ const throwingClickHouse = new Proxy({} as BufferDeps['ch'], {export const stubBufferDeps: BufferDeps = {createLogger: () => silentLogger,+ config: testCoreConfig(),isCronPaused: () => Promise.resolve(false),ch: throwingClickHouse,};-/** `stubBufferDeps` plus a fake ClickHouse client for the flush path. */-export function bufferDepsWithCh(ch: unknown): BufferDeps {- return { ...stubBufferDeps, ch: ch as BufferDeps['ch'] };+/** `stubBufferDeps` plus a fake ClickHouse client for the flush path, and an+ * optional sizing override for the suites that exercise chunking. */+export function bufferDepsWithCh(+ ch: unknown,+ config: BufferDeps['config'] = stubBufferDeps.config+): BufferDeps {+ return { ...stubBufferDeps, ch: ch as BufferDeps['ch'], config };}diff --git a/packages/core/test/config-fixture.ts b/packages/core/test/config-fixture.tsnew file mode 100644index 000000000..1c929225f--- /dev/null+++ b/packages/core/test/config-fixture.ts@@ -0,0 +1,144 @@+// The `CoreConfig` a test hands to a service, a buffer or a route: every+// variable unset, so each module's own documented default applies. This is not+// a second config loader — it holds no defaults of its own; `undefined` and+// the "not set" spelling of every flag is exactly what `apps/api`'s+// `loadConfig({})` produces for an empty environment.+//+// A test that cares about one knob spreads an override over it.++import type { CoreConfig } from '../src/config';++export function testCoreConfig(+ overrides: Partial<CoreConfig> = {}+): CoreConfig {+ return {+ isProduction: false,+ isDevelopment: false,+ selfHosted: false,+ dashboardUrl: '',+ demoUserId: undefined,+ clickhouseClustered: true,+ encryptionKey: undefined,+ pingDisabled: false,+ logging: {+ level: 'info',+ silent: false,+ exporter: 'stdout',+ hyperdxApiKey: undefined,+ serviceNamePrefix: undefined,+ serviceNameEnvironment: 'dev',+ interceptProcessOutput: false,+ },+ auth: {+ allowRegistration: undefined,+ allowInvitation: undefined,+ github: { clientId: '', clientSecret: '', redirectUri: '' },+ google: { clientId: '', clientSecret: '', redirectUri: '' },+ googleGsc: { clientId: '', clientSecret: '', redirectUri: '' },+ },+ cookies: { extraMultiPartTlds: [], customDomain: undefined },+ ai: {+ openai: {+ apiKey: undefined,+ baseUrl: undefined,+ project: undefined,+ organization: undefined,+ },+ anthropic: {+ apiKey: undefined,+ baseUrl: undefined,+ authToken: undefined,+ version: undefined,+ },+ },+ slack: {+ clientId: undefined,+ clientSecret: undefined,+ oauthRedirectUrl: undefined,+ stateSecret: undefined,+ },+ polar: { saveDiscountId: undefined, webhookSecret: undefined },+ kafka: {+ clientId: 'openpanel',+ brokers: [],+ eventsTopic: 'events',+ eventsDlqTopic: 'events-dlq',+ consumerGroup: 'openpanel-events',+ partitionsConcurrent: 8,+ minMessages: 1,+ maxWaitMs: 500,+ maxMessagesPerPartition: 256,+ sessionTimeoutMs: 30_000,+ heartbeatIntervalMs: 3000,+ requestTimeoutMs: 5000,+ connectionTimeoutMs: 2000,+ producerRetries: 2,+ producerInitialRetryMs: 100,+ producerMaxRetryMs: 1000,+ handlerMaxAttempts: 3,+ handlerRetryInitialMs: 100,+ handlerRetryMaxMs: 1000,+ },+ buffers: {+ asyncInserts: false,+ chInsertConcurrency: undefined,+ bot: { batchSize: undefined },+ event: {+ batchSize: undefined,+ chunkSize: undefined,+ microBatchMs: undefined,+ microBatchSize: undefined,+ },+ group: {+ batchSize: undefined,+ chunkSize: undefined,+ ttlSeconds: undefined,+ },+ profile: {+ batchSize: undefined,+ chunkSize: undefined,+ ttlSeconds: undefined,+ fetchChunkSize: undefined,+ },+ profileBackfill: { batchSize: undefined },+ replay: { batchSize: undefined, chunkSize: undefined },+ session: {+ batchSize: undefined,+ chunkSize: undefined,+ squash: true,+ },+ },+ session: {+ timeoutMs: undefined,+ reaperEnabled: true,+ reaperBatchSize: undefined,+ reaperWallclockDeadmanMs: undefined,+ vacuumEnabled: true,+ vacuumBatchSize: undefined,+ vacuumStaleThresholdMs: undefined,+ profileBackfillEnabled: false,+ profileBackfillProjectIds: [],+ },+ query: {+ eventPropertyValueAutocompleteLimit: undefined,+ cohortMaterializeLimit: undefined,+ cohortQueryMemoryLimitBytes: undefined,+ cohortQuerySpillBytes: undefined,+ eventListMaxLookbackDays: undefined,+ sessionListMaxLookbackDays: undefined,+ importBatchSize: undefined,+ insightsRetentionDays: undefined,+ windDownMaxPerRun: undefined,+ funnelNonStrictOrdering: false,+ },+ objectStoreExport: {+ lagSeconds: undefined,+ batchSize: undefined,+ maxBatchesPerRun: undefined,+ concurrency: undefined,+ gcsApiEndpoint: undefined,+ },+ ipHeaders: { attributionOrder: undefined, trustedOrder: undefined },+ ...overrides,+ };+}diff --git a/packages/core/test/http-fixtures.ts b/packages/core/test/http-fixtures.tsindex df38bfcb7..ed93d4aef 100644--- a/packages/core/test/http-fixtures.ts+++ b/packages/core/test/http-fixtures.ts@@ -4,6 +4,7 @@import type { AppDeps, Buffers } from '../src/context';import { createRecordingProducers } from '../src/jobs/testing';import { queues } from '../src/jobs.registry';+import { testCoreConfig } from './config-fixture';import { type CapturedLogger, capturingLogger } from './rpc-fixtures';export interface AppDepsStub {@@ -45,7 +46,7 @@ export function stubAppDeps(): AppDepsStub {buffers: {} as Buffers,producers,logger,- config: { selfHosted: false },+ config: testCoreConfig(),},};}diff --git a/packages/core/test/request-id-end-to-end.test.ts b/packages/core/test/request-id-end-to-end.test.tsindex 7d405ebf1..eb57d12a6 100644--- a/packages/core/test/request-id-end-to-end.test.ts+++ b/packages/core/test/request-id-end-to-end.test.ts@@ -47,6 +47,7 @@ import type { IncomingEventPayload } from '../src/modules/ingest/src/incoming-evimport 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';+import { testCoreConfig } from './config-fixture';const SUPPLIED_REQUEST_ID = 'adr007-benchmark-3';@@ -187,7 +188,7 @@ function stubDeps() {buffers: {} as Buffers,producers: producers as unknown as QueueProducerHandle,logger: bindingLogger(lines),- config: { selfHosted: false },+ config: testCoreConfig(),};return { deps, lines, recorded: producers.recorded };@@ -368,7 +369,7 @@ function chartDeps() {buffers: {} as Buffers,producers: producers as unknown as QueueProducerHandle,logger: chartLogger(lines),- config: { selfHosted: false },+ config: testCoreConfig(),};return { deps, lines };}@@ -560,7 +561,7 @@ function ingestDeps() {buffers,producers: producers as unknown as QueueProducerHandle,logger: ingestLogger(lines),- config: { selfHosted: false },+ config: testCoreConfig(),};return { deps, lines, eventsBuffered, recorded: producers.recorded };diff --git a/packages/core/test/rpc-fixtures.ts b/packages/core/test/rpc-fixtures.tsindex bccf79346..5830dc1a7 100644--- a/packages/core/test/rpc-fixtures.ts+++ b/packages/core/test/rpc-fixtures.ts@@ -13,6 +13,7 @@ import { createRecordingProducers } from '../src/jobs/testing';import { queues } from '../src/jobs.registry';import type { LogFn, Logger } from '../src/logger';import type { Services } from '../src/services';+import { testCoreConfig } from './config-fixture';export interface LoggedLine {level: 'fatal' | 'error' | 'warn' | 'info' | 'debug' | 'trace';@@ -94,7 +95,7 @@ export function stubHttpCtx(buffers: {} as Buffers,producers: createRecordingProducers(queues),logger,- config: { selfHosted: false },+ config: testCoreConfig(),};const { services, ...rest } = overrides;diff --git a/packages/core/test/service-deps.ts b/packages/core/test/service-deps.tsindex f202bc2db..81376674e 100644--- a/packages/core/test/service-deps.ts+++ b/packages/core/test/service-deps.ts@@ -7,6 +7,7 @@import type { Logger } from '../src/logger';import type { ServiceDeps, Services } from '../src/services';+import { testCoreConfig } from './config-fixture';/** Records what the ClickHouse/Postgres call was logged with — the requestId* chain's observation point. */@@ -59,6 +60,7 @@ export async function testServiceDeps(ch,redis,logger: recordingLogger(),+ config: testCoreConfig(),get clients(): never {return unavailable('ServiceDeps.clients');},