OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.
ralph(M9-003): DELETE apps/worker: V1 worker gone, ROLE=worker replaces it end to end
ralphbf51d5a4cfd2
diff --git a/.github/smoke/docker-compose.yml b/.github/smoke/docker-compose.ymlindex b3e51b5a2..29591d87f 100644--- a/.github/smoke/docker-compose.yml+++ b/.github/smoke/docker-compose.yml@@ -141,13 +141,20 @@ services:op-api:condition: service_healthy+ # M9-003 deleted apps/worker: the worker role is the SAME image as the api,+ # run as ROLE=worker (apps/api/Dockerfile's CMD picks the half). There is no+ # separate worker image to resolve any more.op-worker:- image: ${OP_WORKER_IMAGE:-openpanel-smoke-image-unset}+ image: ${OP_API_IMAGE:-openpanel-smoke-image-unset}ports:- "9999:3000"- environment: *app_env+ environment:+ <<: *app_env+ ROLE: worker+ # V2 serves /healthz/ready, not V1's deep /healthcheck (that one lands with+ # the real clients at M9-004). Same probe the controller's boot harness uses.healthcheck:- test: ["CMD-SHELL", "curl -f http://localhost:3000/healthcheck || exit 1"]+ test: ["CMD-SHELL", "curl -f http://localhost:3000/healthz/ready || exit 1"]interval: 5stimeout: 5sretries: 36diff --git a/.github/smoke/smoke.sh b/.github/smoke/smoke.shindex 7b747329a..23b38d580 100755--- a/.github/smoke/smoke.sh+++ b/.github/smoke/smoke.sh@@ -1,7 +1,8 @@#!/usr/bin/env bash#-# Boots the three app images against real Postgres/Redis/ClickHouse and asserts-# they actually serve traffic.+# Boots the app images against real Postgres/Redis/ClickHouse and asserts they+# actually serve traffic. Two images, three services: since M9-003 the worker+# is the api image run as ROLE=worker.## Why this exists: main-8e60 built green, pushed green, and every dashboard# route returned 500 (`r.createEffectfulFunction is not a function`) because a@@ -13,7 +14,7 @@ set -euo pipefailcd "$(dirname "$0")"-for var in OP_API_IMAGE OP_DASHBOARD_IMAGE OP_WORKER_IMAGE; do+for var in OP_API_IMAGE OP_DASHBOARD_IMAGE; doif [ -z "${!var:-}" ]; thenecho "::error::$var is not set"exit 1@@ -99,7 +100,7 @@ docker compose up -d --wait --wait-timeout 420 || fail "stack never became healtecho "==> service reachability"wait_for "api" "$API/healthcheck"-wait_for "worker" "$WORKER/healthcheck"+wait_for "worker" "$WORKER/healthz/ready"wait_for "dashboard" "$DASHBOARD/api/healthcheck"echo "==> SSR rendering (the check that would have caught main-8e60)"diff --git a/.github/workflows/docker-build.yml b/.github/workflows/docker-build.ymlindex ec00134c3..ebe16132f 100644--- a/.github/workflows/docker-build.yml+++ b/.github/workflows/docker-build.yml@@ -193,7 +193,7 @@ jobs:permissions:packages: readcontents: read- needs: [build-api, build-worker, build-dashboard]+ needs: [build-api, build-dashboard]runs-on: ubuntu-lateststeps:- name: Checkout repository@@ -217,7 +217,7 @@ jobs:- name: Resolve imagesrun: |set -euo pipefail- for app in api worker dashboard; do+ for app in api dashboard; dodir="/tmp/smoke-digests/digests-${app}-linux-amd64"digest="$(ls "$dir" | head -1)"if [ -z "$digest" ]; then@@ -295,125 +295,6 @@ jobs:git push origin "api" --forceecho "Pushed tag: api"- build-worker:- permissions:- packages: write- contents: read- needs: lint-and-test- strategy:- fail-fast: false- matrix:- include:- - platform: linux/amd64- runner: ubuntu-latest- - platform: linux/arm64- runner: ubuntu-24.04-arm- runs-on: ${{ matrix.runner }}- steps:- - name: Prepare platform pair- run: |- platform=${{ matrix.platform }}- echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV-- - name: Checkout repository- uses: actions/checkout@v4-- - name: Set up Docker Buildx- uses: docker/setup-buildx-action@v3-- - name: Log in to GitHub Container Registry- uses: docker/login-action@v3- with:- registry: ghcr.io- username: ${{ github.actor }}- password: ${{ secrets.GITHUB_TOKEN }}-- - name: Build and push by digest- id: build- uses: docker/build-push-action@v6- with:- context: .- file: apps/worker/Dockerfile- platforms: ${{ matrix.platform }}- cache-from: type=gha,scope=worker-${{ env.PLATFORM_PAIR }}- cache-to: type=gha,mode=max,scope=worker-${{ env.PLATFORM_PAIR }}- outputs: type=image,name=ghcr.io/${{ env.repo_owner }}/worker,push-by-digest=true,name-canonical=true,push=true- build-args: |- DATABASE_URL=postgresql://dummy:dummy@localhost:5432/dummy-- - name: Export digest- run: |- mkdir -p /tmp/digests- digest="${{ steps.build.outputs.digest }}"- touch "/tmp/digests/${digest#sha256:}"-- - name: Upload digest- uses: actions/upload-artifact@v4- with:- name: digests-worker-${{ env.PLATFORM_PAIR }}- path: /tmp/digests/*- if-no-files-found: error- retention-days: 1-- merge-worker:- permissions:- packages: write- contents: write- needs: [build-worker, smoke]- runs-on: ubuntu-latest- steps:- - name: Checkout repository- uses: actions/checkout@v4-- - name: Download digests- uses: actions/download-artifact@v4- with:- path: /tmp/digests- pattern: digests-worker-*- merge-multiple: true-- - name: Generate tags- id: tags- run: |- BRANCH_NAME=$(echo "${{ github.ref_name }}" | sed 's/\//-/g')- SHORT_SHA=$(echo "${{ github.sha }}" | cut -c1-4)- echo "branch_name=$BRANCH_NAME" >> $GITHUB_OUTPUT- echo "short_sha=$SHORT_SHA" >> $GITHUB_OUTPUT-- - name: Set up Docker Buildx- uses: docker/setup-buildx-action@v3-- - name: Log in to GitHub Container Registry- uses: docker/login-action@v3- with:- registry: ghcr.io- username: ${{ github.actor }}- password: ${{ secrets.GITHUB_TOKEN }}-- - name: Create manifest list and push- working-directory: /tmp/digests- run: |- docker buildx imagetools create \- -t ghcr.io/${{ env.repo_owner }}/worker:${{ steps.tags.outputs.branch_name }}-${{ steps.tags.outputs.short_sha }} \- $(printf 'ghcr.io/${{ env.repo_owner }}/worker@sha256:%s ' *)-- - name: Create/Update Worker tag- if: github.ref == 'refs/heads/main'- run: |- # Delete existing tag if it exists- if git tag -l "worker" | grep -q "worker"; then- git tag -d "worker"- echo "Deleted local tag: worker"- fi-- # Create new tag- git tag "worker" "${{ github.sha }}"- echo "Created tag: worker"-- # Push tag to remote- git push origin "worker" --force- echo "Pushed tag: worker"-build-dashboard:permissions:packages: writediff --git a/apps/api/Dockerfile b/apps/api/Dockerfileindex c3b4c8cd4..cf6ddd9da 100644--- a/apps/api/Dockerfile+++ b/apps/api/Dockerfile@@ -1,4 +1,10 @@ARG NODE_VERSION=24.19.0+# ADR-016 pins Bun exactly at 1.4.0 (floor >=1.2.23). Its declared home is+# `.bun-version` + this ARG; the file does not exist in the tree yet, so this+# ARG is the single home until it does. Keep the two in sync when it lands.+ARG BUN_VERSION=1.4.0++FROM oven/bun:${BUN_VERSION}-slim AS bunFROM node:${NODE_VERSION}-slim AS base@@ -15,6 +21,12 @@ RUN corepack enable && apt-get update && \&& apt-get clean && \rm -rf /var/lib/apt/lists/*+# M9-003 deleted apps/worker: this image is now BOTH halves of the stack. The+# V1 Fastify api still runs on node from `dist/`; `ROLE=worker` runs V2's+# `src/main.ts` from source on Bun (ADR-001: "ROLE=worker moves when+# apps/worker is deleted at P9"). Collapsing the two boots into one is P9.5.+COPY --from=bun /usr/local/bin/bun /usr/local/bin/bun+ARG DATABASE_URLENV DATABASE_URL=$DATABASE_URLENV PNPM_HOME="/pnpm"@@ -26,15 +38,14 @@ WORKDIR /appCOPY package.json pnpm-lock.yaml pnpm-workspace.yaml ./# AppsCOPY apps/api/package.json ./apps/api/-# Packages+# Packages — @openpanel/api's workspace closure, exactly.+COPY packages/core/package.json packages/core/COPY packages/db/package.json packages/db/COPY packages/trpc/package.json packages/trpc/-COPY packages/auth/package.json packages/auth/COPY packages/json/package.json packages/json/COPY packages/email/package.json packages/email/COPY packages/queue/package.json packages/queue/COPY packages/redis/package.json packages/redis/-COPY packages/logger/package.json packages/logger/COPY packages/common/package.json packages/common/COPY packages/payments/package.json packages/payments/COPY packages/constants/package.json packages/constants/@@ -88,18 +99,17 @@ WORKDIR /appCOPY --from=build /app/package.json ./COPY --from=prod /app/node_modules ./node_modules-# Apps+# Apps — src/ travels too, not just dist/: ROLE=worker runs src/main.ts.COPY --from=build /app/apps/api ./apps/api# Packages+COPY --from=build /app/packages/core ./packages/coreCOPY --from=build /app/packages/db ./packages/db-COPY --from=build /app/packages/auth ./packages/authCOPY --from=build /app/packages/trpc ./packages/trpcCOPY --from=build /app/packages/json ./packages/jsonCOPY --from=build /app/packages/email ./packages/emailCOPY --from=build /app/packages/queue ./packages/queueCOPY --from=build /app/packages/redis ./packages/redis-COPY --from=build /app/packages/logger ./packages/loggerCOPY --from=build /app/packages/common ./packages/commonCOPY --from=build /app/packages/payments ./packages/paymentsCOPY --from=build /app/packages/constants ./packages/constants@@ -111,9 +121,18 @@ WORKDIR /app/apps/api# Drop a diagnostic report on fatal errors / SIGUSR2 so the next native# segfault (exit 139) leaves a stack we can read. Override per-environment-# by setting NODE_OPTIONS in the swarm stack file.+# by setting NODE_OPTIONS in the swarm stack file. Bun ignores these flags.ENV NODE_OPTIONS="--report-on-fatalerror --report-on-signal --report-directory=/tmp"EXPOSE 3000-CMD ["node", "dist/index.js"]\ No newline at end of file+# ROLE picks the half. `api` (or unset) keeps V1's Fastify boot byte-for-byte;+# `worker` and `all` run V2's merged process, which validates the rest of its+# configuration itself (apps/api/src/config/env.ts). An unknown ROLE fails+# here rather than silently starting an api: under V1's entrypoint ROLE is not+# read at all, so a typo would otherwise look like a healthy container that+# consumes nothing.+#+# `all` serves the ops surface only until M9-004 mounts publicApiRoutes /+# dashboardRoutes on V2 — do not point production traffic at it before then.+CMD ["sh", "-c", "case \"${ROLE:-api}\" in api) exec node dist/index.js ;; worker|all) exec bun run src/main.ts ;; *) echo \"ROLE=$ROLE is not one of: api, worker, all\" >&2; exit 1 ;; esac"]diff --git a/apps/api/e2e/boot-proof.sh b/apps/api/e2e/boot-proof.shindex b999ae51c..bd870d72a 100755--- a/apps/api/e2e/boot-proof.sh+++ b/apps/api/e2e/boot-proof.sh@@ -2,7 +2,9 @@# Proves `bun run apps/api/src/main.ts` actually boots — every ROLE the ROLE# table declares — rather than merely typechecking (playbook rule 1).#-# M3-CLOSE-002 wrote parts 1-3; M9-002 added parts 4-5.+# M3-CLOSE-002 wrote parts 1-3; M9-002 added parts 4-6; M9-003 (which deleted+# apps/worker) flipped the unhandled-scheduler check in part 4 from "names the+# three missing handlers" to "there are none".## 1. An unknown ROLE fails boot loudly (non-zero exit, named value in the# message) — and so does an unknown ENABLED_QUEUES token.@@ -356,12 +358,15 @@ assert_consuming_role() {fail "ROLE=$role did not start the Kafka ingest consumer"fi- # M9-003 lands dataHealth/windDown/flushExports. Until then main.ts must SAY- # they are missing at boot rather than let each tick fail silently.+ # M9-003 landed dataHealth/windDown/flushExports, so every one of the 20+ # scheduler ids now has a handler on the `cron` queue and this line must NOT+ # appear. It is the runtime half of "every job the old worker ran is served+ # by the merged app under ROLE=worker" — the static half is+ # jobs.registry.test.ts's both-ways scheduler/handler match.if grep -q 'cron schedulers have no handler' "$log"; then- pass "ROLE=$role names the three scheduler ids with no handler yet (M9-003)"+ fail "ROLE=$role reported cron schedulers with no handler: $(grep -o '"schedulers":\[[^]]*\]' "$log" | head -1)"else- fail "ROLE=$role did not report the unhandled schedulers"+ pass "ROLE=$role: every declared cron scheduler has a handler"filocal ready_codediff --git a/apps/api/package.json b/apps/api/package.jsonindex b73aeb388..705822656 100644--- a/apps/api/package.json+++ b/apps/api/package.json@@ -5,6 +5,7 @@"scripts": {"dev": "dotenv -e ../../.env -c -v WATCH=1 tsdown","testing": "API_PORT=3333 pnpm dev",+ "dev:worker": "ROLE=worker dotenv -e ../../.env -- bun --watch src/main.ts","start": "dotenv -e ../../.env node dist/index.js","build": "rm -rf dist && tsdown","test:manage": "jiti scripts/test-manage-api.ts",diff --git a/apps/api/src/hooks/subscription.hook.ts b/apps/api/src/hooks/subscription.hook.tsindex e63e536fb..9e1a2f39b 100644--- a/apps/api/src/hooks/subscription.hook.ts+++ b/apps/api/src/hooks/subscription.hook.ts@@ -7,7 +7,7 @@ import type { FastifyReply, FastifyRequest } from 'fastify';/*** Rejects ingestion for organizations that have been blocked by the wind-down- * sequence (see apps/worker/src/jobs/cron.wind-down.ts).+ * sequence (see packages/core/src/modules/organization/src/wind-down.ts).** Two things worth knowing about the shape of this:*diff --git a/apps/worker/Dockerfile b/apps/worker/Dockerfiledeleted file mode 100644index dd496926b..000000000--- a/apps/worker/Dockerfile+++ /dev/null@@ -1,94 +0,0 @@-ARG NODE_VERSION=24.19.0--FROM node:${NODE_VERSION}-slim AS base--# FIX: Bad workaround (https://github.com/nodejs/corepack/issues/612)-ENV COREPACK_INTEGRITY_KEYS=0--ARG DATABASE_URL-ENV DATABASE_URL=$DATABASE_URL-ENV PNPM_HOME="/pnpm"-ENV PATH="$PNPM_HOME:$PATH"--RUN corepack enable && \- apt-get update && \- apt-get install -y --no-install-recommends \- ca-certificates \- openssl \- curl \- libssl3 && \- apt-get clean && \- rm -rf /var/lib/apt/lists/*--WORKDIR /app--# Workspace-COPY package.json pnpm-lock.yaml pnpm-workspace.yaml ./-# Apps-COPY apps/worker/package.json ./apps/worker/-# Packages-COPY packages/db/package.json ./packages/db/-COPY packages/json/package.json ./packages/json/-COPY packages/email/package.json ./packages/email/-COPY packages/redis/package.json ./packages/redis/-COPY packages/queue/package.json ./packages/queue/-COPY packages/logger/package.json ./packages/logger/-COPY packages/common/package.json ./packages/common/-COPY packages/payments/package.json ./packages/payments/-COPY packages/constants/package.json ./packages/constants/-COPY packages/validation/package.json ./packages/validation/-COPY patches ./patches--# BUILD-FROM base AS build--WORKDIR /app-RUN pnpm install --frozen-lockfile && \- pnpm store prune--COPY apps/worker ./apps/worker-COPY packages ./packages-COPY tooling ./tooling--RUN pnpm codegen && \- pnpm --filter worker run build--# PROD-FROM base AS prod--WORKDIR /app-COPY --from=build /app/package.json ./-COPY --from=build /app/pnpm-lock.yaml ./-RUN pnpm install --frozen-lockfile --prod && \- pnpm store prune--# FINAL-FROM base AS runner--WORKDIR /app--COPY --from=build /app/package.json ./-COPY --from=prod /app/node_modules ./node_modules--# Apps-COPY --from=build /app/apps/worker ./apps/worker--# Packages-COPY --from=build /app/packages/db ./packages/db-COPY --from=build /app/packages/json ./packages/json-COPY --from=build /app/packages/email ./packages/email-COPY --from=build /app/packages/redis ./packages/redis-COPY --from=build /app/packages/queue ./packages/queue-COPY --from=build /app/packages/logger ./packages/logger-COPY --from=build /app/packages/common ./packages/common-COPY --from=build /app/packages/payments ./packages/payments-COPY --from=build /app/packages/constants ./packages/constants-COPY --from=build /app/packages/validation ./packages/validation-COPY --from=build /app/tooling/typescript ./tooling/typescript-RUN pnpm db:codegen--WORKDIR /app/apps/worker--EXPOSE 3000--CMD ["node", "dist/index.js"]\ No newline at end of filediff --git a/apps/worker/package.json b/apps/worker/package.jsondeleted file mode 100644index 0b8f59a2d..000000000--- a/apps/worker/package.json+++ /dev/null@@ -1,47 +0,0 @@-{- "name": "@openpanel/worker",- "version": "0.0.3",- "type": "module",- "scripts": {- "test": "vitest",- "dev": "dotenv -e ../../.env -c -v WATCH=1 tsdown",- "testing": "WORKER_PORT=9999 pnpm dev",- "start": "node dist/index.js",- "build": "rm -rf dist && tsdown",- "typecheck": "tsc --noEmit"- },- "dependencies": {- "@bull-board/api": "6.14.0",- "@bull-board/express": "6.14.0",- "@hyperdx/node-opentelemetry": "catalog:",- "@openpanel/common": "workspace:*",- "@openpanel/core": "workspace:*",- "@openpanel/db": "workspace:*",- "@openpanel/email": "workspace:*",- "@openpanel/payments": "workspace:*",- "@openpanel/queue": "workspace:*",- "@openpanel/redis": "workspace:*",- "@openpanel/validation": "workspace:*",- "bullmq": "^5.63.0",- "date-fns": "^3.3.1",- "express": "^4.18.2",- "pino": "catalog:",- "pino-pretty": "catalog:",- "prom-client": "^15.1.3",- "ramda": "^0.29.1",- "source-map-support": "^0.5.21"- },- "devDependencies": {- "@openpanel/tsconfig": "workspace:*",- "@types/express": "^4.17.21",- "@types/ramda": "^0.29.6",- "@types/source-map-support": "^0.5.10",- "tsdown": "0.14.2",- "typescript": "catalog:"- },- "peerDependencies": {- "@aws-sdk/client-s3": "^3.974.0",- "@aws-sdk/client-sts": "^3.974.0",- "@google-cloud/storage": "^7.18.0"- }-}diff --git a/apps/worker/scripts/get-referrers.ts b/apps/worker/scripts/get-referrers.tsdeleted file mode 100644index 27302eb79..000000000--- a/apps/worker/scripts/get-referrers.ts+++ /dev/null@@ -1,96 +0,0 @@-import fs from 'node:fs';-import path from 'node:path';-import { dirname } from 'node:path';-import { fileURLToPath } from 'node:url';--const __filename = fileURLToPath(import.meta.url);-const __dirname = dirname(__filename);--// extras-const extraReferrers = {- 'zoom.us': { type: 'social', name: 'Zoom' },- 'apple.com': { type: 'tech', name: 'Apple' },- 'adobe.com': { type: 'tech', name: 'Adobe' },- 'figma.com': { type: 'tech', name: 'Figma' },- 'wix.com': { type: 'commerce', name: 'Wix' },- 'gmail.com': { type: 'email', name: 'Gmail' },- 'notion.so': { type: 'tech', name: 'Notion' },- 'ebay.com': { type: 'commerce', name: 'eBay' },- 'github.com': { type: 'tech', name: 'GitHub' },- 'gitlab.com': { type: 'tech', name: 'GitLab' },- 'slack.com': { type: 'social', name: 'Slack' },- 'etsy.com': { type: 'commerce', name: 'Etsy' },- 'bsky.app': { type: 'social', name: 'Bluesky' },- 'twitch.tv': { type: 'content', name: 'Twitch' },- 'dropbox.com': { type: 'tech', name: 'Dropbox' },- 'outlook.com': { type: 'email', name: 'Outlook' },- 'medium.com': { type: 'content', name: 'Medium' },- 'paypal.com': { type: 'commerce', name: 'PayPal' },- 'discord.com': { type: 'social', name: 'Discord' },- 'stripe.com': { type: 'commerce', name: 'Stripe' },- 'spotify.com': { type: 'content', name: 'Spotify' },- 'netflix.com': { type: 'content', name: 'Netflix' },- 'whatsapp.com': { type: 'social', name: 'WhatsApp' },- 'shopify.com': { type: 'commerce', name: 'Shopify' },- 'microsoft.com': { type: 'tech', name: 'Microsoft' },- 'alibaba.com': { type: 'commerce', name: 'Alibaba' },- 'telegram.org': { type: 'social', name: 'Telegram' },- 'substack.com': { type: 'content', name: 'Substack' },- 'salesforce.com': { type: 'tech', name: 'Salesforce' },- 'instagram.com': { type: 'social', name: 'Instagram' },- 'wikipedia.org': { type: 'content', name: 'Wikipedia' },- 'mastodon.social': { type: 'social', name: 'Mastodon' },- 'office.com': { type: 'tech', name: 'Microsoft Office' },- 'squarespace.com': { type: 'commerce', name: 'Squarespace' },- 'stackoverflow.com': { type: 'tech', name: 'Stack Overflow' },- 'teams.microsoft.com': { type: 'social', name: 'Microsoft Teams' },-};--function transform(data: any) {- const obj: Record<string, unknown> = {};- for (const type in data) {- for (const name in data[type]) {- const domains = data[type][name].domains ?? [];- for (const domain of domains) {- obj[domain] = {- type,- name,- };- }- }- }-- return obj;-}--async function main() {- // Get document, or throw exception on error- try {- const data = await fetch(- 'https://s3-eu-west-1.amazonaws.com/snowplow-hosted-assets/third-party/referer-parser/referers-latest.json',- ).then((res) => res.json());-- fs.writeFileSync(- path.resolve(__dirname, '../../worker/src/referrers/index.ts'),- [- '// This file is generated by the script get-referrers.ts',- '',- '// The data is fetch from snowplow-referer-parser https://github.com/snowplow-referer-parser/referer-parser',- `// The orginal referers.yml is based on Piwik's SearchEngines.php and Socials.php, copyright 2012 Matthieu Aubry and available under the GNU General Public License v3.`,- '',- `const referrers: Record<string, { type: string, name: string }> = ${JSON.stringify(- {- ...transform(data),- ...extraReferrers,- },- )} as const;`,- 'export default referrers;',- ].join('\n'),- 'utf-8',- );- } catch (e) {- console.log(e);- }-}--main();diff --git a/apps/worker/src/boot-cron.ts b/apps/worker/src/boot-cron.tsdeleted file mode 100644index 15c21db15..000000000--- a/apps/worker/src/boot-cron.ts+++ /dev/null@@ -1,237 +0,0 @@-import type { CronQueueType } from '@openpanel/queue';-import { cronQueue } from '@openpanel/queue';--import { logger } from './utils/logger';--async function removeConflictingJobs(schedulerKey: string) {- // Remove any existing jobs that might conflict with the scheduler- // BullMQ scheduler jobs have IDs like "repeat:<key>:<timestamp>"- const jobStates = ['delayed', 'waiting', 'completed', 'failed'] as const;-- for (const state of jobStates) {- try {- const jobs = await cronQueue.getJobs([state]);- for (const job of jobs) {- // Check if this job was created by the scheduler we're about to upsert- if (job.id?.startsWith(`repeat:${schedulerKey}:`)) {- await job.remove();- logger.info(- { jobId: job.id, schedulerKey },- 'Removed conflicting scheduler job',- );- }- }- } catch (error) {- // Ignore errors during cleanup- }- }-}--export async function bootCron() {- const jobs: {- name: string;- type: CronQueueType;- pattern: string | number;- }[] = [- {- name: 'salt',- type: 'salt',- pattern: '0 0 * * *',- },- {- name: 'delete',- type: 'delete',- pattern: '0 * * * *',- },- {- name: 'flush',- type: 'flushEvents',- pattern: 1000 * 10,- },- {- name: 'flush',- type: 'flushProfiles',- pattern: 1000 * 10,- },- {- name: 'flush',- type: 'flushSessions',- pattern: 1000 * 10,- },- {- name: 'flush',- type: 'flushProfileBackfill',- pattern: 1000 * 30,- },- {- name: 'flush',- type: 'flushReplay',- pattern: 1000 * 10,- },- {- name: 'flush',- type: 'flushGroups',- pattern: 1000 * 10,- },- {- name: 'insightsDaily',- type: 'insightsDaily',- pattern: '0 2 * * *',- },- {- name: 'onboarding',- type: 'onboarding',- pattern: '0 * * * *',- },- {- name: 'gscSync',- type: 'gscSync',- pattern: '0 3 * * *',- },- {- name: 'cohortRefresh',- type: 'cohortRefresh',- pattern: '*/30 * * * *',- },- {- name: 'sessionReaper',- type: 'sessionReaper',- pattern: 1000 * 60 * 5, // every 5 minutes- },- {- name: 'sessionVacuum',- type: 'sessionVacuum',- pattern: '0 4 * * *', // daily at 04:00 UTC — backstop for cleanup leaks- },- {- name: 'insightCleanup',- type: 'insightCleanup',- pattern: '30 4 * * *', // daily at 04:30 UTC — prune stale insights/events- },- {- name: 'weeklyDigest',- type: 'weeklyDigest',- pattern: '0 8 * * 1', // Mondays 08:00 UTC — weekly analytics digest email- },- {- name: 'dataHealth',- type: 'dataHealth',- pattern: '30 7 * * *', // Daily 07:30 UTC — no-data / data-stopped rescue emails- },- {- name: 'windDown',- type: 'windDown',- pattern: '0 * * * *', // Hourly — expired-trial wind-down emails, block, delete- },- {- name: 'flushExports',- type: 'flushExports',- pattern: 1000 * 60, // Every 1 minute — drains export buffers to S3/GCS- },- ];-- if (process.env.SELF_HOSTED && process.env.NODE_ENV === 'production') {- jobs.push({- name: 'ping',- type: 'ping',- pattern: '0 0 * * *',- });- }-- logger.info('Updating cron jobs');-- const jobsToKeep = new Set(jobs.map((job) => job.type));-- const currentJobSchedulers = await cronQueue- .getJobSchedulers()- .catch((error) => {- logger.error({ err: error }, 'Error getting job schedulers');- return [];- });- for (const jobScheduler of currentJobSchedulers) {- if (!jobsToKeep.has(jobScheduler.key as CronQueueType)) {- await cronQueue.removeJobScheduler(jobScheduler.key).catch((error) => {- logger.error(- { err: error, jobScheduler: jobScheduler.key },- 'Error removing job scheduler',- );- });- }- }-- for (const job of jobs) {- try {- await cronQueue.upsertJobScheduler(- job.type,- typeof job.pattern === 'number'- ? {- every: job.pattern,- }- : {- pattern: job.pattern,- },- {- data: {- type: job.type,- payload: undefined,- },- },- );- } catch (error) {- // If upsert fails due to conflicting job, try to clean up and retry- const isConflictError =- error instanceof Error &&- error.message.includes('job ID already exists');-- if (isConflictError) {- logger.warn(- { job: job.type },- 'Job scheduler conflict detected, attempting cleanup',- );-- await removeConflictingJobs(job.type);-- // Also try removing the scheduler itself to start fresh- try {- await cronQueue.removeJobScheduler(job.type);- } catch {- // Ignore - scheduler might not exist- }-- // Retry the upsert- try {- await cronQueue.upsertJobScheduler(- job.type,- typeof job.pattern === 'number'- ? {- every: job.pattern,- }- : {- pattern: job.pattern,- },- {- data: {- type: job.type,- payload: undefined,- },- },- );- logger.info(- { job: job.type },- 'Job scheduler created after cleanup',- );- } catch (retryError) {- logger.error(- { err: retryError, job: job.type },- 'Error upserting job scheduler after cleanup',- );- }- } else {- logger.error(- { err: error, job: job.type },- 'Error upserting job scheduler',- );- }- }- }-}diff --git a/apps/worker/src/boot-debug.ts b/apps/worker/src/boot-debug.tsdeleted file mode 100644index 49f5e15ed..000000000--- a/apps/worker/src/boot-debug.ts+++ /dev/null@@ -1,260 +0,0 @@-import { db } from '@openpanel/db';-import {- type CronQueuePayload,- type CronQueueType,- type InsightsQueuePayloadProject,- insightsQueue,-} from '@openpanel/queue';-import type { Job } from 'bullmq';-import type { Express, Request } from 'express';-import { cronJob } from './jobs/cron';-import { insightsProjectJob } from './jobs/insights';-import { previewWeeklyDigestForProject } from './jobs/cron.weekly-digest';-import { logger } from './utils/logger';--const CRON_TYPES = [- 'salt',- 'flushEvents',- 'flushProfiles',- 'flushSessions',- 'flushProfileBackfill',- 'flushReplay',- 'flushGroups',- 'ping',- 'delete',- 'insightsDaily',- 'onboarding',- 'gscSync',- 'cohortRefresh',- 'sessionReaper',- 'sessionVacuum',- 'insightCleanup',- 'weeklyDigest',- 'dataHealth',- 'windDown',- 'flushExports',-] as const satisfies readonly CronQueueType[];--function escapeHtml(value: string) {- return value- .replace(/&/g, '&')- .replace(/</g, '<')- .replace(/>/g, '>')- .replace(/"/g, '"');-}--function buildJobs(req: Request) {- const baseUrl = `${req.protocol}://${req.get('host')}`;- return CRON_TYPES.map((type) => ({- type,- url: `${baseUrl}/debug/cron/${type}`,- }));-}--// Browsers send an explicit `text/html` accept, curl sends `*/*`. Listing json-// first means the wildcard falls back to json while a browser still gets html.-function wantsHtml(req: Request) {- return req.accepts(['json', 'html']) === 'html';-}--function renderHtml(jobs: { type: string; url: string }[], message?: string) {- const links = jobs- .map((job) => `<li><a href="${job.url}">${escapeHtml(job.type)}</a></li>`)- .join('');- const note = message- ? `<pre>${escapeHtml(message)}</pre>`- : '<p>Click a job to run it now.</p>';- return `<!doctype html>-<html lang="en">-<head><meta charset="utf-8"><title>Debug cron</title></head>-<body style="font-family: ui-monospace, monospace; max-width: 40rem; margin: 2rem auto;">-<h1>Trigger a cron job</h1>-${note}-<ul>${links}</ul>-</body>-</html>`;-}--// Local-only debug routes to run a cron job on demand instead of waiting for-// its schedule. Runs the job in this process through the same dispatcher the-// scheduler uses, so it reuses the worker's db, redis and clickhouse clients.-// Returns clickable links: an HTML page in a browser, full URLs in JSON for-// curl. Only mounted when NODE_ENV !== 'production'.-export function bootDebugRoutes(app: Express) {- app.get('/debug/cron', (req, res) => {- const jobs = buildJobs(req);- if (wantsHtml(req)) {- res.type('html').send(renderHtml(jobs));- return;- }- res.json({- message: 'Run a cron job now with GET or POST /debug/cron/:type',- jobs,- });- });-- app.all('/debug/cron/:type', async (req, res) => {- const type = req.params.type;- const jobs = buildJobs(req);- const html = wantsHtml(req);-- if (!CRON_TYPES.includes(type as CronQueueType)) {- const message = `Unknown cron type "${type}"`;- if (html) {- res.status(400).type('html').send(renderHtml(jobs, message));- return;- }- res.status(400).json({ ok: false, error: message, jobs });- return;- }-- logger.info({ type }, 'Manually triggering cron job');-- try {- const result = await cronJob({- data: { type: type as CronQueueType, payload: undefined },- } as Job<CronQueuePayload>);- if (html) {- res- .type('html')- .send(- renderHtml(- jobs,- `${type} ran. Result: ${JSON.stringify(result ?? null)}. Check the worker logs for details.`,- ),- );- return;- }- res.json({ ok: true, type, result: result ?? null });- } catch (error) {- const message = error instanceof Error ? error.message : String(error);- logger.error({ err: error, type }, 'Manual cron trigger failed');- if (html) {- res- .status(500)- .type('html')- .send(renderHtml(jobs, `Error running ${type}: ${message}`));- return;- }- res.status(500).json({ ok: false, type, error: message });- }- });-- // Run the full insights pipeline (engine + AI enrichment) for ONE project,- // bypassing the eligibility filter the daily cron applies. For local testing.- //- // Default: enqueues a real `insightsProject` job on the `insights` queue, so- // it shows up in BullBoard and runs through the normal worker path.- // `?inline=1`: runs synchronously instead and returns a result summary.- app.get('/debug/insights/:projectId', async (req, res) => {- const projectId = req.params.projectId;- const inline = req.query.inline === '1' || req.query.inline === 'true';- const date = new Date().toISOString().slice(0, 10);-- if (!inline) {- try {- const job = await insightsQueue.add(- 'insightsProject',- { type: 'insightsProject', payload: { projectId, date } },- // Unique id so repeated test runs aren't deduped by BullMQ.- { jobId: `debug:${projectId}:${Date.now()}` },- );- logger.info({ projectId, jobId: job.id }, 'Enqueued insights job');- res.json({- ok: true,- projectId,- jobId: job.id,- message:- 'Enqueued on the "insights" queue — watch it in BullBoard. Add ?inline=1 to run synchronously and get a result summary.',- });- } catch (error) {- const message = error instanceof Error ? error.message : String(error);- logger.error({ err: error, projectId }, 'Failed to enqueue insights');- res.status(500).json({ ok: false, projectId, error: message });- }- return;- }-- logger.info({ projectId }, 'Manually running insights for project (inline)');-- try {- await insightsProjectJob({- data: { type: 'insightsProject', payload: { projectId, date } },- } as Job<InsightsQueuePayloadProject>);-- const countsByState = await db.projectInsight.groupBy({- by: ['state'],- where: { projectId },- _count: true,- });-- const topActive = await db.projectInsight.findMany({- where: { projectId, state: 'active' },- orderBy: [- { relevanceScore: { sort: 'desc', nulls: 'last' } },- { impactScore: 'desc' },- ],- take: 15,- select: {- title: true,- aiSummary: true,- aiCategory: true,- relevanceScore: true,- emailWorthy: true,- referenceWorthy: true,- enrichedAt: true,- },- });-- const enrichedCount = topActive.filter((i) => i.enrichedAt).length;-- res.json({- ok: true,- projectId,- countsByState,- enrichedInTopActive: `${enrichedCount}/${topActive.length}`,- topActive,- });- } catch (error) {- const message = error instanceof Error ? error.message : String(error);- logger.error({ err: error, projectId }, 'Manual insights run failed');- res.status(500).json({ ok: false, projectId, error: message });- }- });-- // Weekly digest for ONE project, bypassing eligibility. For local testing.- // [email protected] → send only to that address- // (no ?to) → return the assembled payload without sending- // ?force=1 → build even with 0 visitors this week- app.get('/debug/weekly-digest/:projectId', async (req, res) => {- const projectId = req.params.projectId;- const to = typeof req.query.to === 'string' ? req.query.to : undefined;- const force = req.query.force === '1' || req.query.force === 'true';- logger.info({ projectId, to, force }, 'Manual weekly digest');-- try {- const result = await previewWeeklyDigestForProject(projectId, {- to,- force,- });- res.json({- ok: true,- ...result,- hint: to- ? `Sent to ${to} (needs RESEND_API_KEY/SMTP_HOST, else logged to worker console).`- : 'Preview only — add [email protected] to send. ?force=1 builds even with no traffic this week.',- });- } catch (error) {- const message = error instanceof Error ? error.message : String(error);- logger.error({ err: error, projectId }, 'Manual weekly digest failed');- res.status(500).json({ ok: false, projectId, error: message });- }- });-- logger.info('Debug cron endpoint enabled: /debug/cron/:type');- logger.info('Debug insights endpoint enabled: /debug/insights/:projectId');- logger.info(- 'Debug weekly digest enabled: /debug/weekly-digest/:projectId?to=&force=1',- );-}-diff --git a/apps/worker/src/boot-workers.test.ts b/apps/worker/src/boot-workers.test.tsdeleted file mode 100644index f7fb50d64..000000000--- a/apps/worker/src/boot-workers.test.ts+++ /dev/null@@ -1,134 +0,0 @@-/**- * ENABLED_QUEUES validation only (ADR-004 rec 5/6 + docs/ANSWERS.md §1.3- * ruling). `bootWorkers()` itself isn't exercised here — it starts real- * BullMQ workers and the Kafka consumer, which is integration territory.- */--import { beforeEach, describe, expect, it, vi } from 'vitest';--vi.mock('./utils/logger', () => {- const makeLogger = (): Record<string, ReturnType<typeof vi.fn>> => {- const log: Record<string, ReturnType<typeof vi.fn>> = {- info: vi.fn(),- warn: vi.fn(),- error: vi.fn(),- fatal: vi.fn(),- };- log.child = vi.fn(() => makeLogger());- return log;- };- return { logger: makeLogger() };-});--import { assertKnownQueue, getEnabledQueues } from './boot-workers';-import { logger } from './utils/logger';--const fatal = vi.mocked(logger.fatal);--// `process.exit`'s two overloads (string | number | null | undefined) don't-// unify with vi.spyOn's generic inference — pin the mock to one signature.-function mockProcessExit() {- return vi- .spyOn(process, 'exit')- .mockImplementation((_code?: string | number | null): never => {- return undefined as never;- });-}--describe('getEnabledQueues', () => {- const originalEnabledQueues = process.env.ENABLED_QUEUES;- let exitSpy: ReturnType<typeof mockProcessExit>;-- beforeEach(() => {- vi.clearAllMocks();- if (originalEnabledQueues === undefined) {- delete process.env.ENABLED_QUEUES;- } else {- process.env.ENABLED_QUEUES = originalEnabledQueues;- }- exitSpy = mockProcessExit();- });-- it('defaults to events plus the seven registry queues when unset', () => {- delete process.env.ENABLED_QUEUES;- expect(getEnabledQueues()).toEqual([- 'events',- 'sessions',- 'cron',- 'notification',- 'import',- 'insights',- 'gsc',- 'cohortCompute',- ]);- expect(exitSpy).not.toHaveBeenCalled();- });-- it('accepts a comma-separated subset of known queues', () => {- process.env.ENABLED_QUEUES = 'events, sessions ,cron';- expect(getEnabledQueues()).toEqual(['events', 'sessions', 'cron']);- expect(exitSpy).not.toHaveBeenCalled();- });-- it('rejects the events_kafka fixture with a message naming the rename', () => {- process.env.ENABLED_QUEUES = 'events_kafka';- getEnabledQueues();-- expect(exitSpy).toHaveBeenCalledWith(1);- expect(fatal).toHaveBeenCalledTimes(1);- const [meta, message] = fatal.mock.calls[0] ?? [];- expect(message).toContain('events_kafka');- expect(message).toContain('renamed');- expect(message).toContain('events');- expect(meta).toMatchObject({ value: 'events_kafka', renamedTo: 'events' });- });-- it('rejects an unknown token, non-zero exit naming value and accepted set', () => {- process.env.ENABLED_QUEUES = 'not_a_real_queue';- getEnabledQueues();-- expect(exitSpy).toHaveBeenCalledWith(1);- expect(fatal).toHaveBeenCalledTimes(1);- const [meta, message] = fatal.mock.calls[0] ?? [];- expect(message).toContain('not_a_real_queue');- expect(meta).toMatchObject({- value: 'not_a_real_queue',- accepted: [- 'events',- 'sessions',- 'cron',- 'notification',- 'import',- 'insights',- 'gsc',- 'cohortCompute',- ],- });- });-});--describe('assertKnownQueue', () => {- let exitSpy: ReturnType<typeof mockProcessExit>;-- beforeEach(() => {- vi.clearAllMocks();- exitSpy = mockProcessExit();- });-- it('accepts every registry queue and events', () => {- for (const name of [- 'events',- 'sessions',- 'cron',- 'notification',- 'import',- 'insights',- 'gsc',- 'cohortCompute',- ]) {- assertKnownQueue(name);- }- expect(fatal).not.toHaveBeenCalled();- expect(exitSpy).not.toHaveBeenCalled();- });-});diff --git a/apps/worker/src/boot-workers.ts b/apps/worker/src/boot-workers.tsdeleted file mode 100644index d04aad402..000000000--- a/apps/worker/src/boot-workers.ts+++ /dev/null@@ -1,404 +0,0 @@-import { performance } from 'node:perf_hooks';-import { setTimeout as sleep } from 'node:timers/promises';-import { rawStderrWrite } from '@openpanel/core';-import {- cohortComputeQueue,- cronQueue,- gscQueue,- importQueue,- insightsQueue,- notificationQueue,- sessionsQueue,-} from '@openpanel/queue';-import { getRedisQueue } from '@openpanel/redis';-import type { Queue, WorkerOptions } from 'bullmq';-import { Worker } from 'bullmq';-import { cohortComputeJob } from './jobs/cohort.compute';-import { cronJob } from './jobs/cron';-import {- type KafkaConsumerHandle,- startKafkaEventsConsumer,-} from './jobs/events.kafka-consumer';-import { gscJob } from './jobs/gsc';-import { importJob } from './jobs/import';-import { insightsProjectJob } from './jobs/insights';-import { notificationJob } from './jobs/notification';-import { sessionsJob } from './jobs/sessions';-import { eventsGroupJobDuration } from './metrics';-import { setShuttingDown } from './utils/graceful-shutdown';-import { logger } from './utils/logger';-import { enableEventsHeartbeat } from './utils/worker-heartbeat';--const workerOptions: WorkerOptions = {- connection: getRedisQueue(),-};--// The Kafka events consumer's token. Renamed from `events_kafka` (ADR-004-// rec 5/6 + docs/ANSWERS.md §1.3): there is one events transport now, so-// there is one token for it.-const KAFKA_QUEUE_NAME = 'events';--// The seven BullMQ registry queues (ADR-005). Kept in sync with-// packages/core/src/jobs.registry.ts by hand until the worker merges into-// core at P9.-const REGISTRY_QUEUE_NAMES = [- 'sessions',- 'cron',- 'notification',- 'import',- 'insights',- 'gsc',- 'cohortCompute',-] as const;--const KNOWN_QUEUE_NAMES = [KAFKA_QUEUE_NAME, ...REGISTRY_QUEUE_NAMES] as const;--type QueueName = (typeof KNOWN_QUEUE_NAMES)[number];--const RENAMED_QUEUE_TOKENS: Record<string, string> = {- events_kafka: KAFKA_QUEUE_NAME,-};--/**- * Fails boot loudly on an unknown ENABLED_QUEUES token, instead of V1's- * silent ignore (docs/ANSWERS.md §1.3 ruling). `events_kafka` gets a message- * naming the rename; anything else names the offending value and the- * accepted set.- */-export function assertKnownQueue(value: string): void {- if ((KNOWN_QUEUE_NAMES as readonly string[]).includes(value)) {- return;- }-- const renamedTo = RENAMED_QUEUE_TOKENS[value];- if (renamedTo) {- logger.fatal(- { value, renamedTo, accepted: KNOWN_QUEUE_NAMES },- `ENABLED_QUEUES: "${value}" was renamed to "${renamedTo}" — update ENABLED_QUEUES to use the new name.`- );- process.exit(1);- return;- }-- logger.fatal(- { value, accepted: KNOWN_QUEUE_NAMES },- `ENABLED_QUEUES: unknown queue "${value}". Accepted values: ${KNOWN_QUEUE_NAMES.join(', ')}.`- );- process.exit(1);-}--/**- * Parses the ENABLED_QUEUES environment variable and returns an array of queue names to start.- * If no env var is provided, returns all queues.- */-export function getEnabledQueues(): QueueName[] {- const enabledQueuesEnv = process.env.ENABLED_QUEUES?.trim();-- if (!enabledQueuesEnv) {- logger.info('No ENABLED_QUEUES specified, starting all queues');- return [...KNOWN_QUEUE_NAMES];- }-- const queues = enabledQueuesEnv- .split(',')- .map((q) => q.trim())- .filter(Boolean);-- for (const queue of queues) {- assertKnownQueue(queue);- }-- logger.info({ queues }, 'Starting queues from ENABLED_QUEUES');- return queues as QueueName[];-}--/**- * Gets the concurrency setting for a queue from environment variables.- * Env var format: {QUEUE_NAME}_CONCURRENCY (e.g., SESSIONS_CONCURRENCY=32)- */-function getConcurrencyFor(queueName: string, defaultValue = 1): number {- const envKey = `${queueName.toUpperCase().replace(/[^A-Z0-9]/g, '_')}_CONCURRENCY`;- const value = process.env[envKey];-- if (value) {- const parsed = Number.parseInt(value, 10);- if (!Number.isNaN(parsed) && parsed > 0) {- return parsed;- }- }-- return defaultValue;-}--export function bootWorkers() {- const enabledQueues = getEnabledQueues();-- const workers: Worker[] = [];- const extraStops: Array<() => Promise<unknown>> = [];-- // Start Kafka events consumer — the sole events transport (ADR-004).- if (enabledQueues.includes(KAFKA_QUEUE_NAME)) {- enableEventsHeartbeat();- let handle: KafkaConsumerHandle | null = null;- const startPromise = startKafkaEventsConsumer()- .then((h) => {- handle = h;- logger.info('Started Kafka events consumer');- })- .catch((err) => {- logger.error({ err }, 'Failed to start Kafka events consumer');- });- extraStops.push(async () => {- await startPromise.catch(() => undefined);- if (handle) {- await handle.stop();- }- });- }-- // Start sessions worker- if (enabledQueues.includes('sessions')) {- const concurrency = getConcurrencyFor('sessions');- const sessionsWorker = new Worker(sessionsQueue.name, sessionsJob, {- ...workerOptions,- concurrency,- });- workers.push(sessionsWorker);- logger.info({ concurrency }, 'Started worker for sessions');- }-- // Start cron worker- if (enabledQueues.includes('cron')) {- const concurrency = getConcurrencyFor('cron');- const cronWorker = new Worker(cronQueue.name, cronJob, {- ...workerOptions,- concurrency,- });- workers.push(cronWorker);- logger.info({ concurrency }, 'Started worker for cron');- }-- // Start notification worker- if (enabledQueues.includes('notification')) {- const concurrency = getConcurrencyFor('notification');- const notificationWorker = new Worker(- notificationQueue.name,- notificationJob,- { ...workerOptions, concurrency }- );- workers.push(notificationWorker);- logger.info({ concurrency }, 'Started worker for notification');- }-- // Start import worker- if (enabledQueues.includes('import')) {- const concurrency = getConcurrencyFor('import');- const importWorker = new Worker(importQueue.name, importJob, {- ...workerOptions,- concurrency,- });- workers.push(importWorker);- logger.info({ concurrency }, 'Started worker for import');- }-- // Start insights worker- if (enabledQueues.includes('insights')) {- const concurrency = getConcurrencyFor('insights', 5);- const insightsWorker = new Worker(insightsQueue.name, insightsProjectJob, {- ...workerOptions,- concurrency,- });- workers.push(insightsWorker);- logger.info({ concurrency }, 'Started worker for insights');- }-- // Start gsc worker- if (enabledQueues.includes('gsc')) {- const concurrency = getConcurrencyFor('gsc', 5);- const gscWorker = new Worker(gscQueue.name, gscJob, {- ...workerOptions,- concurrency,- });- workers.push(gscWorker);- logger.info({ concurrency }, 'Started worker for gsc');- }-- // Start cohortCompute worker- if (enabledQueues.includes('cohortCompute')) {- const concurrency = getConcurrencyFor('cohortCompute', 2);- const cohortComputeWorker = new Worker(- cohortComputeQueue.name,- cohortComputeJob,- {- ...workerOptions,- concurrency,- }- );- workers.push(cohortComputeWorker);- logger.info({ concurrency }, 'Started worker for cohortCompute');- }-- if (workers.length === 0) {- logger.warn(- 'No workers started. Check ENABLED_QUEUES environment variable.'- );- }-- workers.forEach((worker) => {- worker.on('error', (error) => {- logger.error({ err: error, worker: worker.name }, 'worker error');- });-- worker.on('closed', () => {- logger.info({ worker: worker.name }, 'worker closed');- });-- worker.on('ready', () => {- logger.info({ worker: worker.name }, 'worker ready');- });-- worker.on('failed', (job) => {- if (job) {- if (job.processedOn && job.finishedOn) {- const elapsed = job.finishedOn - job.processedOn;- eventsGroupJobDuration.observe(- { name: worker.name, status: 'failed' },- elapsed- );- }- logger.error(- {- jobId: job.id,- worker: worker.name,- data: job.data,- failedReason: job.failedReason,- options: job.opts,- },- 'job failed'- );- }- });-- worker.on('ioredis:close', () => {- logger.error(- { worker: worker.name },- 'worker closed due to ioredis:close'- );- });- });-- async function exitHandler(- eventName: string,- evtOrExitCodeOrError: number | string | Error- ) {- logger.info(- { code: evtOrExitCodeOrError, eventName },- 'Starting graceful shutdown'- );-- // Hard deadline: if cron drain or worker.close() hangs, force-exit- // before Docker's stop_grace_period elapses. Without this the- // container sits in "Stopping" until SIGKILL (exit 137) and looks- // like a real crash to the swarm.- const forceExitMs = Number(process.env.SHUTDOWN_FORCE_EXIT_MS || '20000');- const exitCode = Number.isNaN(+evtOrExitCodeOrError)- ? 1- : +evtOrExitCodeOrError;- const forceExit = setTimeout(() => {- logger.error(- { eventName, forceExitMs },- 'Graceful shutdown timed out — forcing exit'- );- process.exit(exitCode);- }, forceExitMs);- forceExit.unref();-- try {- const time = performance.now();-- // Wait for cron queue to empty if it's running- if (enabledQueues.includes('cron')) {- await waitForQueueToEmpty(cronQueue);- }-- await Promise.all([- ...workers.map((worker) => worker.close()),- ...extraStops.map((stop) =>- stop().catch((err) => {- logger.error({ err }, 'extra stop handler error');- })- ),- ]);-- logger.info(- { elapsed: performance.now() - time },- 'workers closed successfully'- );- } catch (e) {- logger.error(- { err: e, code: evtOrExitCodeOrError },- 'exit handler error'- );- }- clearTimeout(forceExit);- process.exit(exitCode);- }-- // SIGTERM / SIGINT: drain in-flight jobs, then exit.- ['SIGTERM', 'SIGINT'].forEach((evt) => {- process.on(evt, (code) => {- setShuttingDown(true);- exitHandler(evt, code);- });- });-- // uncaughtException / unhandledRejection: process state is corrupt.- // Don't try to drain — log and exit fast so Docker respawns us.- // Mirror fatals to the real stderr (bypassing the output interceptor, so- // nothing ships twice) — the OTLP flush window can be lost on the way- // down, and `docker logs` must always show why we died.- process.on('uncaughtException', (error) => {- logger.fatal({ err: error }, 'Uncaught exception — exiting');- rawStderrWrite(`Uncaught exception — exiting: ${error?.stack ?? error}\n`);- setShuttingDown(true);- setTimeout(() => process.exit(1), 1000).unref();- });- process.on('unhandledRejection', (reason, promise) => {- logger.fatal({ reason, promise }, 'Unhandled rejection — exiting');- rawStderrWrite(- `Unhandled rejection — exiting: ${- reason instanceof Error ? reason.stack : String(reason)- }\n`- );- setShuttingDown(true);- setTimeout(() => process.exit(1), 1000).unref();- });-- return workers;-}--export async function waitForQueueToEmpty(queue: Queue, timeout = 60_000) {- const startTime = performance.now();-- while (true) {- const activeCount = await queue.getActiveCount();-- if (activeCount === 0) {- break;- }-- if (performance.now() - startTime > timeout) {- logger.warn(- { queue: queue.name, remainingCount: activeCount },- 'Timeout reached while waiting for queue to empty'- );- break;- }-- logger.info(- { queue: queue.name, count: activeCount },- 'Waiting for queue to finish'- );- await sleep(500);- }-}diff --git a/apps/worker/src/index.ts b/apps/worker/src/index.tsdeleted file mode 100644index e4c879f02..000000000--- a/apps/worker/src/index.ts+++ /dev/null@@ -1,183 +0,0 @@-import './utils/observability';--import { createBullBoard } from '@bull-board/api';-import { BullMQAdapter } from '@bull-board/api/bullMQAdapter';-import { ExpressAdapter } from '@bull-board/express';-import { tryCatch } from '@openpanel/common';-import { chQuery, createInitialSalts, db } from '@openpanel/db';-import {- assertKafkaConfigured,- cohortComputeQueue,- cronQueue,- gscQueue,- importQueue,- insightsQueue,- notificationQueue,- sessionsQueue,-} from '@openpanel/queue';-import { getRedisCache } from '@openpanel/redis';-import express from 'express';-import client from 'prom-client';-import sourceMapSupport from 'source-map-support';-import { bootCron } from './boot-cron';-import { bootDebugRoutes } from './boot-debug';-import { bootWorkers } from './boot-workers';-import { register } from './metrics';-import { isShuttingDown } from './utils/graceful-shutdown';-import { logger } from './utils/logger';-import { getEventsHeartbeat } from './utils/worker-heartbeat';--const EVENTS_HEARTBEAT_STALE_MS = 60_000;--sourceMapSupport.install();--// Kafka/Redpanda is the sole events transport (ADR-004) — a missing broker list-// must kill boot, not surface as a failing consumer nobody notices.-try {- assertKafkaConfigured();-} catch (error) {- logger.fatal({ err: error }, 'Refusing to start');- process.exit(1);-}--async function start() {- const collectDefaultMetrics = client.collectDefaultMetrics;- collectDefaultMetrics({ register });-- const PORT = Number.parseInt(process.env.WORKER_PORT || '3000', 10);- const app = express();-- // Local-only: trigger cron jobs on demand. Disabled in production. Mounted- // before bull-board so its routes take precedence.- if (process.env.NODE_ENV !== 'production') {- bootDebugRoutes(app);- }-- if (- process.env.DISABLE_BULLBOARD !== '1' &&- process.env.DISABLE_BULLBOARD !== 'true'- ) {- const serverAdapter = new ExpressAdapter();- serverAdapter.setBasePath('/');- createBullBoard({- queues: [- new BullMQAdapter(sessionsQueue),- new BullMQAdapter(cronQueue),- new BullMQAdapter(notificationQueue),- new BullMQAdapter(importQueue),- new BullMQAdapter(insightsQueue),- new BullMQAdapter(gscQueue),- new BullMQAdapter(cohortComputeQueue),- ],- serverAdapter,- });-- app.use('/', serverAdapter.getRouter());- }-- app.get('/metrics', (req, res) => {- res.set('Content-Type', register.contentType);- register- .metrics()- .then((metrics) => {- res.end(metrics);- })- .catch((error) => {- res.status(500).end(error);- });- });-- app.get('/healthcheck', async (req, res) => {- const [redisResult, dbResult, chResult] = await Promise.all([- tryCatch(async () => (await getRedisCache().ping()) === 'PONG'),- tryCatch(async () => !!(await db.$executeRaw`SELECT 1`)),- tryCatch(async () => (await chQuery('SELECT 1')).length > 0),- ]);-- const dependencies = {- redis: redisResult.ok && redisResult.data,- db: dbResult.ok && dbResult.data,- ch: chResult.ok && chResult.data,- };- const dependencyErrors = {- redis: redisResult.error?.message,- db: dbResult.error?.message,- ch: chResult.error?.message,- };-- const failedDependencies = Object.entries(dependencies)- .filter(([, ok]) => !ok)- .map(([name]) => name);- const workingDependencies = Object.entries(dependencies)- .filter(([, ok]) => ok)- .map(([name]) => name);-- const status = failedDependencies.length === 0 ? 200 : 503;-- if (status !== 200) {- logger.warn(- {- workingDependencies,- failedDependencies,- dependencies,- dependencyErrors,- },- 'healthcheck failed',- );- }-- res.status(status).json({- ready: status === 200,- ...dependencies,- failedDependencies,- workingDependencies,- });- });-- // Kubernetes liveness — shallow, event loop only.- app.get('/healthz/live', (req, res) => {- res.status(200).json({ live: true });- });-- // Kubernetes readiness — shallow + shutdown-aware. When events workers run- // on this instance, also require the events consumer-loop heartbeat to be- // fresh (refreshed on each `completed`/`drained` event). If events are not- // enabled here, the heartbeat check is skipped.- app.get('/healthz/ready', (req, res) => {- if (isShuttingDown()) {- res.status(503).json({ ready: false, reason: 'shutting down' });- return;- }-- const { enabled, lastActivityAt } = getEventsHeartbeat();- if (enabled) {- const idleMs = Date.now() - lastActivityAt;- if (idleMs > EVENTS_HEARTBEAT_STALE_MS) {- res.status(503).json({- ready: false,- reason: 'events consumer heartbeat stale',- idleMs,- thresholdMs: EVENTS_HEARTBEAT_STALE_MS,- });- return;- }- }-- res.status(200).json({ ready: true });- });-- app.listen(PORT, () => {- logger.info(`For the UI, open http://localhost:${PORT}/`);- });-- if (process.env.DISABLE_WORKERS === undefined) {- await bootWorkers();- await bootCron();- } else {- logger.warn('Workers are disabled');- }-- await createInitialSalts();-}--start();diff --git a/apps/worker/src/jobs/cohort.compute.ts b/apps/worker/src/jobs/cohort.compute.tsdeleted file mode 100644index 8bea88292..000000000--- a/apps/worker/src/jobs/cohort.compute.ts+++ /dev/null@@ -1,11 +0,0 @@-// Dissolved into @openpanel/core's cohort module (M5-003): the compute body-// moved to packages/core/src/modules/cohort/cohort.service.ts#updateCohortMembership.-// This file stays (DELEGATE PATTERN) — it is the BullMQ job body V1's worker-// registers (boot-workers.ts), a thin wrapper around the core function.-import { updateCohortMembership } from '@openpanel/core';-import type { CohortComputePayload } from '@openpanel/queue';-import type { Job } from 'bullmq';--export async function cohortComputeJob(job: Job<CohortComputePayload>) {- await updateCohortMembership(job.data.cohortId);-}diff --git a/apps/worker/src/jobs/cron.cohort-refresh.ts b/apps/worker/src/jobs/cron.cohort-refresh.tsdeleted file mode 100644index f9c037e64..000000000--- a/apps/worker/src/jobs/cron.cohort-refresh.ts+++ /dev/null@@ -1,25 +0,0 @@-// Dissolved into @openpanel/core's cohort module (M5-003): the non-static-// cohort lookup moved to-// packages/core/src/modules/cohort/cohort.service.ts#listRefreshableCohortIds.-// This file stays (DELEGATE PATTERN) — it is the `cron.ts` dispatcher's-// `cohortRefresh` case, a thin wrapper around the core function. The enqueue-// itself stays inline against @openpanel/queue's cohortComputeQueue directly-// — same as gsc's worker file does for `gscProjectSync` (M5-002); see-// packages/core/src/modules/cohort/cohort.service.ts's header for why core-// cannot import @openpanel/queue back.-import { listRefreshableCohortIds } from '@openpanel/core';-import { cohortComputeQueue } from '@openpanel/queue';--export async function cohortRefreshCronJob() {- const cohortIds = await listRefreshableCohortIds();-- await Promise.all(- cohortIds.map((cohortId) =>- cohortComputeQueue.add(- 'cohortCompute',- { cohortId },- { deduplication: { id: `cohort-${cohortId}` } }- )- )- );-}diff --git a/apps/worker/src/jobs/cron.delete.ts b/apps/worker/src/jobs/cron.delete.tsdeleted file mode 100644index 18e04797f..000000000--- a/apps/worker/src/jobs/cron.delete.ts+++ /dev/null@@ -1,14 +0,0 @@-// Dissolved into @openpanel/core's organization module (M6-001): the-// deletion sweep itself, plus packages/db/src/services/delete.service.ts,-// moved to-// packages/core/src/modules/organization/organization.service.ts#runDeleteCron.-// This file stays (DELEGATE PATTERN) — it is the `cron.ts` dispatcher's-// `delete` case, a thin wrapper around the core function, same shape as-// gsc.ts's `gscSyncAllJob` (M5-002).-import { runDeleteCron } from '@openpanel/core';-import { logger } from '@/utils/logger';--export async function jobDelete() {- const result = await runDeleteCron();- logger.info(result, 'Delete cron complete');-}diff --git a/apps/worker/src/jobs/cron.flush-exports.test.ts b/apps/worker/src/jobs/cron.flush-exports.test.tsdeleted file mode 100644index ad7318017..000000000--- a/apps/worker/src/jobs/cron.flush-exports.test.ts+++ /dev/null@@ -1,149 +0,0 @@-import { gunzipSync } from 'node:zlib';-import { createGCSAdapter } from '@openpanel/core';-import {- clickhouseEventToExportEvent,- createBatch,- createManifest,- generateBatchPath,- MANIFEST_CONTENT_TYPE,- MANIFEST_FILENAME,- parseManifest,- serializeManifest,-} from '@openpanel/db/src/exports';-import { describe, expect, it } from 'vitest';--/**- * Exercises the object-store export path end to end against a local- * fake-gcs-server (see gcs-adapter.test.ts for how to start it). The job's own- * ClickHouse/Postgres I/O is out of scope here; what this pins down is the part- * a consumer depends on — batch files land, then a manifest that points at- * them, under the partitioned path.- *- * Skips when the emulator isn't reachable.- */-const EMULATOR = process.env.GCS_API_ENDPOINT ?? 'http://localhost:4443';-const BUCKET = 'op-flush-exports-test';--async function emulatorReachable(): Promise<boolean> {- try {- const res = await fetch(`${EMULATOR}/storage/v1/b`, {- signal: AbortSignal.timeout(2000),- });- return res.ok;- } catch {- return false;- }-}--const available = await emulatorReachable();--// A structurally complete service account document. fake-gcs-server does no-// auth, but the adapter pins the credential type and requires the fields a real-// key has before it will build a client.-const KEY = JSON.stringify({- type: 'service_account',- project_id: 'openpanel-test',- client_email: '[email protected]',- private_key: 'test-private-key',-});--const chEvent = (i: number) =>- ({- id: `00000000-0000-0000-0000-00000000000${i}`,- project_id: 'proj_1',- name: 'screen_view',- created_at: `2026-08-26 10:0${i}:00.000`,- inserted_at: `2026-08-26 11:0${i}:00.000`,- profile_id: `user_${i}`,- device_id: `dev_${i}`,- session_id: `sess_${i}`,- properties: { __path: `/p/${i}`, n: i },- country: 'SE',- city: 'Stockholm',- region: 'AB',- os: 'macOS',- browser: 'Chrome',- device: 'desktop',- path: `/p/${i}`,- origin: 'https://openpanel.dev',- referrer: '',- }) as never;--describe.skipIf(!available)('flush-exports -> GCS end to end', () => {- it('writes batch files then a manifest that points at them', async () => {- process.env.GCS_API_ENDPOINT = EMULATOR;- await fetch(`${EMULATOR}/storage/v1/b`, {- method: 'POST',- headers: { 'Content-Type': 'application/json' },- body: JSON.stringify({ name: BUCKET }),- });-- const config = {- type: 'gcs_export' as const,- bucket: BUCKET,- prefix: 'openpanel-exports',- format: 'jsonl_gzip' as const,- serviceAccountKey: KEY,- };- const adapter = createGCSAdapter(config);-- // --- 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 basePath = generateBatchPath(- config.prefix,- 'proj_1',- 'int_1',- batch.info.batchId,- new Date(batch.info.minEventTime)- );-- for (const file of batch.files) {- await adapter.upload({- bucket: config.bucket,- key: `${basePath}/${file.filename}`,- content: file.content,- contentType: file.contentType,- });- }- const manifest = createManifest(- batch.info,- batch.files.map((f) => f.filename)- );- await adapter.upload({- bucket: config.bucket,- key: `${basePath}/${MANIFEST_FILENAME}`,- content: serializeManifest(manifest),- contentType: MANIFEST_CONTENT_TYPE,- });- // --- end ----- const read = async (key: string) => {- const res = await fetch(- `${EMULATOR}/storage/v1/b/${BUCKET}/o/${encodeURIComponent(key)}?alt=media`- );- expect(res.ok).toBe(true);- return Buffer.from(await res.arrayBuffer());- };-- const storedManifest = parseManifest(- (await read(`${basePath}/${MANIFEST_FILENAME}`)).toString()- );- expect(storedManifest.record_count).toBe(3);- expect(storedManifest.files).toEqual(['part-0000.jsonl.gz']);- expect(storedManifest.partition_date).toBe('2026-08-26');-- const lines = gunzipSync(- await read(`${basePath}/${storedManifest.files[0]}`)- )- .toString()- .trim()- .split('\n')- .map((l) => JSON.parse(l));-- expect(lines).toHaveLength(3);- expect(lines[0].event_name).toBe('screen_view');- expect(lines[0].project_id).toBe('proj_1');- expect(lines.map((l) => l.path)).toEqual(['/p/1', '/p/2', '/p/3']);- }, 60_000);-});diff --git a/apps/worker/src/jobs/cron.insight-cleanup.ts b/apps/worker/src/jobs/cron.insight-cleanup.tsdeleted file mode 100644index 6e45c983a..000000000--- a/apps/worker/src/jobs/cron.insight-cleanup.ts+++ /dev/null@@ -1,13 +0,0 @@-// Dissolved into @openpanel/core's insight module (M5-001): the retention-// rules moved to-// packages/core/src/modules/insight/insight.service.ts#cleanupStaleInsights.-// This file stays (DELEGATE PATTERN) — it is the `insightCleanup` case of-// `cron.ts`'s dispatcher.-import { cleanupStaleInsights } from '@openpanel/core';-import { logger as baseLogger } from '@/utils/logger';--const logger = baseLogger.child({ job: 'insight-cleanup' });--export async function insightCleanupCronJob() {- await cleanupStaleInsights(logger);-}diff --git a/apps/worker/src/jobs/cron.onboarding.ts b/apps/worker/src/jobs/cron.onboarding.tsdeleted file mode 100644index ceadbd48d..000000000--- a/apps/worker/src/jobs/cron.onboarding.ts+++ /dev/null@@ -1,12 +0,0 @@-// Dissolved into @openpanel/core's onboarding module (M6-003): the drip-// sequence (ONBOARDING_EMAILS) and the sweep itself moved to-// packages/core/src/modules/onboarding/onboarding.service.ts#runOnboardingCron.-// This file stays (DELEGATE PATTERN) — it is the `cron.ts` dispatcher's-// `onboarding` case, a thin wrapper around the core function, same shape as-// cron.delete.ts (M6-001).-import { runOnboardingCron } from '@openpanel/core';-import { logger } from '@/utils/logger';--export async function onboardingJob() {- return await runOnboardingCron(logger);-}diff --git a/apps/worker/src/jobs/cron.ping.ts b/apps/worker/src/jobs/cron.ping.tsdeleted file mode 100644index b9e7440d1..000000000--- a/apps/worker/src/jobs/cron.ping.ts+++ /dev/null@@ -1,10 +0,0 @@-// Dissolved into @openpanel/core's misc module (M7-008): the outbound-// telemetry sweep moved to-// packages/core/src/modules/misc/misc.service.ts#runPingCron. This file-// stays (DELEGATE PATTERN) — it is `cron.ts`'s dispatcher `ping` case, a thin-// wrapper around the core function, same shape as `cron.delete.ts` (M6-001).-import { runPingCron } from '@openpanel/core';--export async function ping() {- return await runPingCron();-}diff --git a/apps/worker/src/jobs/cron.salt.ts b/apps/worker/src/jobs/cron.salt.tsdeleted file mode 100644index 5e10b4d71..000000000--- a/apps/worker/src/jobs/cron.salt.ts+++ /dev/null@@ -1,10 +0,0 @@-// Dissolved into @openpanel/core's salt module (M8-004): rotation logic-// moved to packages/core/src/modules/salt/salt.service.ts#rotateSalt. This-// file stays (DELEGATE PATTERN) — it is `cron.ts`'s dispatcher `salt` case,-// a thin wrapper around the core function, same shape as `cron.delete.ts`-// (M6-001).-import { rotateSalt } from '@openpanel/core';--export async function salt() {- return await rotateSalt();-}diff --git a/apps/worker/src/jobs/cron.session-reaper.ts b/apps/worker/src/jobs/cron.session-reaper.tsdeleted file mode 100644index 66c5171d6..000000000--- a/apps/worker/src/jobs/cron.session-reaper.ts+++ /dev/null@@ -1,18 +0,0 @@-// Dissolved into @openpanel/core's session module (M7-001): the wall-clock-// deadman scan, per-project lock and orphan handling moved to-// packages/core/src/modules/session/src/session-reaper.ts. This file stays-// (DELEGATE PATTERN) — it is the `sessionReaper` case of `cron.ts`'s-// dispatcher, and enqueues through V1's BullMQ `sessionsQueue`.-import { loadSessionRuntime, reapIdleSessions } from '@openpanel/core';-import { logger as baseLogger } from '@/utils/logger';-import { enqueueSessionEndV2 } from '@/utils/session-handler';--const logger = baseLogger.child({ job: 'session-reaper' });--export async function sessionReaperCronJob() {- await reapIdleSessions({- ...(await loadSessionRuntime()),- logger,- enqueueSessionEnd: enqueueSessionEndV2,- });-}diff --git a/apps/worker/src/jobs/cron.session-vacuum.ts b/apps/worker/src/jobs/cron.session-vacuum.tsdeleted file mode 100644index 4c5d72c3f..000000000--- a/apps/worker/src/jobs/cron.session-vacuum.ts+++ /dev/null@@ -1,12 +0,0 @@-// Dissolved into @openpanel/core's session module (M7-001): the stale-blob /-// orphan sweep moved to packages/core/src/modules/session/src/session-vacuum.ts.-// This file stays (DELEGATE PATTERN) — it is the `sessionVacuum` case of-// `cron.ts`'s dispatcher.-import { loadSessionRuntime, vacuumStaleSessions } from '@openpanel/core';-import { logger as baseLogger } from '@/utils/logger';--const logger = baseLogger.child({ job: 'session-vacuum' });--export async function sessionVacuumCronJob() {- await vacuumStaleSessions({ ...(await loadSessionRuntime()), logger });-}diff --git a/apps/worker/src/jobs/cron.ts b/apps/worker/src/jobs/cron.tsdeleted file mode 100644index 0ec4ac29d..000000000--- a/apps/worker/src/jobs/cron.ts+++ /dev/null@@ -1,91 +0,0 @@-import {- eventBuffer,- groupBuffer,- profileBackfillBuffer,- profileBuffer,- replayBuffer,- sessionBuffer,-} from '@openpanel/db';-import type { CronQueuePayload } from '@openpanel/queue';-import type { Job } from 'bullmq';-import { cohortRefreshCronJob } from './cron.cohort-refresh';-import { dataHealthCronJob } from './cron.data-health';-import { jobDelete } from './cron.delete';-import { flushExportsJob } from './cron.flush-exports';-import { insightCleanupCronJob } from './cron.insight-cleanup';-import { onboardingJob } from './cron.onboarding';-import { ping } from './cron.ping';-import { salt } from './cron.salt';-import { sessionReaperCronJob } from './cron.session-reaper';-import { sessionVacuumCronJob } from './cron.session-vacuum';-import { weeklyDigestCronJob } from './cron.weekly-digest';-import { windDownCronJob } from './cron.wind-down';-import { gscSyncAllJob } from './gsc';-import { insightsDailyJob } from './insights';-import { logger } from '@/utils/logger';--export async function cronJob(job: Job<CronQueuePayload>) {- logger.debug(`Cron job started - ${job.data.type}`);- switch (job.data.type) {- case 'salt': {- return await salt();- }- case 'flushEvents': {- return await eventBuffer.tryFlush({ trigger: 'cron' });- }- case 'flushProfiles': {- return await profileBuffer.tryFlush({ trigger: 'cron' });- }- case 'flushSessions': {- return await sessionBuffer.tryFlush({ trigger: 'cron' });- }- case 'flushProfileBackfill': {- return await profileBackfillBuffer.tryFlush({ trigger: 'cron' });- }- case 'flushReplay': {- return await replayBuffer.tryFlush({ trigger: 'cron' });- }- case 'flushGroups': {- return await groupBuffer.tryFlush({ trigger: 'cron' });- }- case 'ping': {- return await ping();- }- case 'delete': {- return await jobDelete();- }- case 'insightsDaily': {- return await insightsDailyJob(job);- }- case 'onboarding': {- return await onboardingJob();- }- case 'gscSync': {- return await gscSyncAllJob();- }- case 'cohortRefresh': {- return await cohortRefreshCronJob();- }- case 'sessionReaper': {- return await sessionReaperCronJob();- }- case 'sessionVacuum': {- return await sessionVacuumCronJob();- }- case 'insightCleanup': {- return await insightCleanupCronJob();- }- case 'weeklyDigest': {- return await weeklyDigestCronJob();- }- case 'dataHealth': {- return await dataHealthCronJob();- }- case 'windDown': {- return await windDownCronJob();- }- case 'flushExports': {- return await flushExportsJob(job);- }- }-}diff --git a/apps/worker/src/jobs/cron.weekly-digest.ts b/apps/worker/src/jobs/cron.weekly-digest.tsdeleted file mode 100644index 6c5965562..000000000--- a/apps/worker/src/jobs/cron.weekly-digest.ts+++ /dev/null@@ -1,27 +0,0 @@-// Dissolved into @openpanel/core's insight module (M5-001): the digest-// assembly, narrative call and send loop moved to-// packages/core/src/modules/insight/insight.service.ts. This file stays-// (DELEGATE PATTERN) — it is the `weeklyDigest` case of `cron.ts`'s-// dispatcher, and `previewWeeklyDigestForProject` backs boot-debug.ts's-// single-project preview route.-import { previewWeeklyDigest, sendWeeklyDigests } from '@openpanel/core';-import { logger as baseLogger } from '@/utils/logger';--const logger = baseLogger.child({ job: 'weekly-digest' });--export async function weeklyDigestCronJob() {- await sendWeeklyDigests(logger);-}--/**- * Debug/testing helper for a SINGLE project, bypassing eligibility.- * - opts.to → send only to that address (safe for testing)- * - no opts.to → assemble and return the payload without sending (preview)- * - opts.force → build even if the project had 0 visitors this week- */-export async function previewWeeklyDigestForProject(- projectId: string,- opts: { to?: string; force?: boolean } = {}-) {- return previewWeeklyDigest(projectId, opts);-}diff --git a/apps/worker/src/jobs/cron.wind-down.test.ts b/apps/worker/src/jobs/cron.wind-down.test.tsdeleted file mode 100644index e51aeb33b..000000000--- a/apps/worker/src/jobs/cron.wind-down.test.ts+++ /dev/null@@ -1,332 +0,0 @@-/**- * The db, email and pricing modules are replaced wholesale — these assert the- * job's decisions (who enters, which step fires, what gets written), not any- * real persistence.- */-import { subDays } from 'date-fns';-import { beforeEach, describe, expect, it, vi } from 'vitest';--const {- dbMock,- getOrganizationEventsCountMock,- getOrganizationEventsCountSinceMock,- getLastEventPerProjectMock,- sendEmailMock,- getRecommendedPlanMock,-} = vi.hoisted(() => ({- dbMock: {- organization: {- findMany: vi.fn(),- update: vi.fn().mockResolvedValue({}),- updateMany: vi.fn().mockResolvedValue({ count: 0 }),- },- },- getOrganizationEventsCountMock: vi.fn(),- getOrganizationEventsCountSinceMock: vi.fn(),- getLastEventPerProjectMock: vi.fn(),- sendEmailMock: vi.fn().mockResolvedValue({}),- getRecommendedPlanMock: vi.fn(),-}));--vi.mock('@openpanel/db', () => ({- db: dbMock,- getOrganizationEventsCount: getOrganizationEventsCountMock,- getOrganizationEventsCountSince: getOrganizationEventsCountSinceMock,- getLastEventPerProject: getLastEventPerProjectMock,-}));-vi.mock('@openpanel/email', () => ({ sendEmail: sendEmailMock }));-vi.mock('@openpanel/payments', () => ({- getRecommendedPlan: getRecommendedPlanMock,-}));-const { buildWinBackHighlightMock } = vi.hoisted(() => ({- buildWinBackHighlightMock: vi.fn(),-}));-vi.mock('./lib/win-back-highlight', () => ({- buildWinBackHighlight: buildWinBackHighlightMock,- HIGHLIGHT_MIN_RECENT_EVENTS: 1000,-}));--import { windDownCronJob } from './cron.wind-down';--type OrgOverrides = Record<string, unknown>;--function makeOrg(overrides: OrgOverrides = {}) {- return {- id: 'org-1',- subscriptionState: 'trial_expired',- subscriptionStatus: 'trialing',- subscriptionEndsAt: subDays(new Date(), 800),- subscriptionId: null,- windDownStartedAt: null,- windDownStep: null,- deleteAt: null,- createdBy: {- id: 'user-1',- email: '[email protected]',- firstName: 'Alex',- deletedAt: null,- },- projects: [{ id: 'project-1', name: 'acme-web' }],- ...overrides,- };-}--beforeEach(() => {- vi.clearAllMocks();- vi.unstubAllEnvs();- vi.stubEnv('SELF_HOSTED', 'false');- vi.stubEnv('DASHBOARD_URL', 'https://dashboard.openpanel.dev');- dbMock.organization.update.mockResolvedValue({});- dbMock.organization.updateMany.mockResolvedValue({ count: 0 });- sendEmailMock.mockResolvedValue({});- getOrganizationEventsCountMock.mockResolvedValue(1000);- getOrganizationEventsCountSinceMock.mockResolvedValue(250);- // Default: nothing has sent recently.- getLastEventPerProjectMock.mockResolvedValue(new Map());- buildWinBackHighlightMock.mockResolvedValue(undefined);- getRecommendedPlanMock.mockReturnValue('100K events per month for $20.00');-});--describe('windDownCronJob', () => {- it('does nothing when self hosted', async () => {- vi.stubEnv('SELF_HOSTED', 'true');-- expect(await windDownCronJob()).toBeNull();- expect(dbMock.organization.findMany).not.toHaveBeenCalled();- });-- it('starts a long-expired org at step 0 rather than at deletion', async () => {- // The anchor decision, asserted directly: this org's trial ended 800 days- // ago. Measuring the schedule from subscriptionEndsAt would put it past- // every step and delete it without warning.- dbMock.organization.findMany.mockResolvedValue([makeOrg()]);-- const result = await windDownCronJob();-- expect(result).toMatchObject({ entering: 1, emailsSent: 1 });- expect(sendEmailMock).toHaveBeenCalledWith('wind-down-expired', {- to: '[email protected]',- data: expect.objectContaining({ firstName: 'Alex' }),- });- expect(dbMock.organization.updateMany).toHaveBeenCalledWith({- where: { id: { in: ['org-1'] } },- data: { windDownStartedAt: expect.any(Date) },- });- expect(dbMock.organization.update).toHaveBeenCalledWith({- where: { id: 'org-1' },- data: { windDownStep: 'expired_notice' },- });- // Nothing scheduled for deletion on the way in.- expect(dbMock.organization.update).not.toHaveBeenCalledWith(- expect.objectContaining({- data: expect.objectContaining({ deleteAt: expect.anything() }),- }),- );- });-- it('caps how many organizations enter per run', async () => {- vi.stubEnv('WIND_DOWN_MAX_PER_RUN', '2');- dbMock.organization.findMany.mockResolvedValue([- makeOrg({ id: 'org-1' }),- makeOrg({ id: 'org-2' }),- makeOrg({ id: 'org-3' }),- makeOrg({ id: 'org-4' }),- ]);-- const result = await windDownCronJob();-- expect(result).toMatchObject({ entering: 2 });- expect(dbMock.organization.updateMany).toHaveBeenCalledWith({- where: { id: { in: ['org-1', 'org-2'] } },- data: { windDownStartedAt: expect.any(Date) },- });- });-- it('blocks ingestion by advancing the pointer on day 21', async () => {- dbMock.organization.findMany.mockResolvedValue([- makeOrg({- windDownStartedAt: subDays(new Date(), 21),- windDownStep: 'stopping_soon',- }),- ]);-- await windDownCronJob();-- expect(sendEmailMock).toHaveBeenCalledWith(- 'wind-down-blocked',- expect.anything(),- );- expect(dbMock.organization.update).toHaveBeenCalledWith({- where: { id: 'org-1' },- data: { windDownStep: 'blocked' },- });- });-- it('arms deletion once the final warning is delivered', async () => {- dbMock.organization.findMany.mockResolvedValue([- makeOrg({- windDownStartedAt: subDays(new Date(), 44),- windDownStep: 'blocked',- }),- ]);-- await windDownCronJob();-- expect(sendEmailMock).toHaveBeenCalledWith(- 'wind-down-final-warning',- expect.anything(),- );- expect(dbMock.organization.update).toHaveBeenCalledWith({- where: { id: 'org-1' },- data: { deleteAt: expect.any(Date) },- });- expect(dbMock.organization.update).toHaveBeenCalledWith({- where: { id: 'org-1' },- data: { windDownStep: 'final_warning' },- });- });-- it('does not arm deletion when the final warning was not delivered', async () => {- sendEmailMock.mockResolvedValue(null);- dbMock.organization.findMany.mockResolvedValue([- makeOrg({- windDownStartedAt: subDays(new Date(), 44),- windDownStep: 'blocked',- }),- ]);-- const result = await windDownCronJob();-- expect(result).toMatchObject({ emailsSent: 0, failed: 1 });- expect(dbMock.organization.update).not.toHaveBeenCalled();- });-- it('releases an organization that subscribed mid-sequence', async () => {- dbMock.organization.findMany.mockResolvedValue([- makeOrg({- subscriptionState: 'active',- subscriptionStatus: 'active',- subscriptionId: 'sub-123',- windDownStartedAt: subDays(new Date(), 30),- windDownStep: 'blocked',- deleteAt: new Date(),- }),- ]);-- const result = await windDownCronJob();-- expect(result).toMatchObject({ recovered: 1, emailsSent: 0 });- expect(dbMock.organization.updateMany).toHaveBeenCalledWith({- where: { id: { in: ['org-1'] } },- data: { windDownStartedAt: null, windDownStep: null, deleteAt: null },- });- expect(sendEmailMock).not.toHaveBeenCalled();- });-- it('skips organizations with no one to warn', async () => {- dbMock.organization.findMany.mockResolvedValue([- makeOrg({ createdBy: null }),- makeOrg({- id: 'org-2',- createdBy: {- id: 'user-2',- email: '[email protected]',- firstName: null,- deletedAt: new Date(),- },- }),- ]);-- const result = await windDownCronJob();-- expect(result).toMatchObject({ entering: 0, emailsSent: 0 });- expect(sendEmailMock).not.toHaveBeenCalled();- });-- describe('still-tracking organizations', () => {- // 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 () => {- vi.stubEnv('WIND_DOWN_MAX_PER_RUN', '1');- getLastEventPerProjectMock.mockResolvedValue(- new Map([['project-active', subDays(new Date(), 1)]]),- );- // The dormant org is returned first, so only ordering can save the- // active one from being crowded out by the long tail.- dbMock.organization.findMany.mockResolvedValue([- makeOrg({- id: 'org-dormant',- projects: [{ id: 'project-dormant', name: 'dormant-web' }],- }),- makeOrg({- id: 'org-active',- projects: [{ id: 'project-active', name: 'active-web' }],- }),- ]);-- const result = await windDownCronJob();-- expect(result).toMatchObject({ entering: 1, stillTracking: 1 });- expect(dbMock.organization.updateMany).toHaveBeenCalledWith({- where: { id: { in: ['org-active'] } },- data: { windDownStartedAt: expect.any(Date) },- });- });-- it('sends current volume, project names and the data highlight', async () => {- getLastEventPerProjectMock.mockResolvedValue(- new Map([['project-1', subDays(new Date(), 1)]]),- );- getOrganizationEventsCountMock.mockResolvedValue(842_110);- getOrganizationEventsCountSinceMock.mockResolvedValue(128_400);- buildWinBackHighlightMock.mockResolvedValue(- 'acme-web had a strong month.',- );- dbMock.organization.findMany.mockResolvedValue([makeOrg()]);-- await windDownCronJob();-- expect(sendEmailMock).toHaveBeenCalledWith('wind-down-expired', {- to: '[email protected]',- data: expect.objectContaining({- stillTracking: true,- eventsCount: 842_110,- recentEventsCount: 128_400,- projectNames: ['acme-web'],- highlight: 'acme-web had a strong month.',- }),- });- expect(buildWinBackHighlightMock).toHaveBeenCalledWith({- project: { id: 'project-1', name: 'acme-web' },- recentEventsCount: 128_400,- });- });-- it('asks for no highlight when the org is not still tracking', async () => {- dbMock.organization.findMany.mockResolvedValue([makeOrg()]);-- await windDownCronJob();-- expect(buildWinBackHighlightMock).not.toHaveBeenCalled();- expect(sendEmailMock).toHaveBeenCalledWith('wind-down-expired', {- to: '[email protected]',- data: expect.objectContaining({ highlight: undefined }),- });- });-- it('does not call an org still tracking when its last event is old', async () => {- getLastEventPerProjectMock.mockResolvedValue(- new Map([['project-1', subDays(new Date(), 200)]]),- );- dbMock.organization.findMany.mockResolvedValue([makeOrg()]);-- const result = await windDownCronJob();-- expect(result).toMatchObject({ stillTracking: 0 });- expect(sendEmailMock).toHaveBeenCalledWith('wind-down-expired', {- to: '[email protected]',- data: expect.objectContaining({ stillTracking: false }),- });- });- });-});diff --git a/apps/worker/src/jobs/events.create-session-end.ts b/apps/worker/src/jobs/events.create-session-end.tsdeleted file mode 100644index d0539c15d..000000000--- a/apps/worker/src/jobs/events.create-session-end.ts+++ /dev/null@@ -1,23 +0,0 @@-// Dissolved into @openpanel/core's session module (M7-001): the three-case-// live-vs-snapshot logic, the idempotency claim and the funnel-rule check-// moved to packages/core/src/modules/session/src/session-end.ts (and its-// ClickHouse query onto the `sql` tag). This file stays (DELEGATE PATTERN) —-// it adapts V1's BullMQ `{ type, payload, snapshot }` job to core's-// `{ event, snapshot }` and is what `sessions.ts` still calls.-import {- createSessionEnd as createSessionEndCore,- loadSessionEndDeps,- loadSessionRuntime,-} from '@openpanel/core';-import type { EventsQueuePayloadCreateSessionEnd } from '@openpanel/queue';-import type { Job } from 'bullmq';-import { logger as baseLogger } from '@/utils/logger';--export async function createSessionEnd(- job: Job<EventsQueuePayloadCreateSessionEnd>-) {- const { payload, snapshot } = job.data;- const logger = baseLogger.child({ payload, jobId: job.id });- const deps = await loadSessionEndDeps(await loadSessionRuntime(), logger);- return createSessionEndCore({ event: payload, snapshot }, deps);-}diff --git a/apps/worker/src/jobs/events.incoming-event.ts b/apps/worker/src/jobs/events.incoming-event.tsdeleted file mode 100644index ed4adeacb..000000000--- a/apps/worker/src/jobs/events.incoming-event.ts+++ /dev/null@@ -1,40 +0,0 @@-// Dissolved into @openpanel/core's ingest module (M8-003): the whole handler-// — session lifecycle decisions, session_start emission, referrer inheritance-// and the producer-minted event id — moved to-// packages/core/src/modules/ingest/src/incoming-event-handler.ts. This file-// stays (DELEGATE PATTERN) because it is what binds core's injected-// dependencies to V1's world: this worker's registry counters, this worker's-// pino, and `enqueueSessionEndV2`, which speaks @openpanel/queue's BullMQ-// wire shape and therefore cannot live in core.-import {- type IncomingEventDelivery,- type IncomingEventDeps,- type IncomingEventPayload,- incomingEvent as incomingEventCore,- loadIncomingEventDeps,-} from '@openpanel/core';-import { sessionEndsEnqueued, sessionsStarted } from '@/metrics';-import { logger as baseLogger } from '@/utils/logger';-import { enqueueSessionEndV2 } from '@/utils/session-handler';--let deps: Promise<IncomingEventDeps> | null = null;--// Resolved once per process: the loader dynamically imports @openpanel/db's-// buffers, prisma client and notification service, and this is the hot path.-function getDeps(): Promise<IncomingEventDeps> {- deps ??= loadIncomingEventDeps(baseLogger, enqueueSessionEndV2, {- // V1's own registry, so this worker's /metrics body is unchanged.- metrics: {- sessionStarted: (kind) => sessionsStarted.inc({ kind }),- sessionEndEnqueued: (source) => sessionEndsEnqueued.inc({ source }),- },- });- return deps;-}--export async function incomingEvent(- jobPayload: IncomingEventPayload,- meta?: IncomingEventDelivery-) {- return incomingEventCore(jobPayload, await getDeps(), meta);-}diff --git a/apps/worker/src/jobs/events.kafka-consumer.ts b/apps/worker/src/jobs/events.kafka-consumer.tsdeleted file mode 100644index bde98df53..000000000--- a/apps/worker/src/jobs/events.kafka-consumer.ts+++ /dev/null@@ -1,57 +0,0 @@-// Dissolved into @openpanel/core's ingest module (M8-003): the batch handler-// (per-key serial groups, the ascending contiguous-prefix offset walk,-// at-least-once, bounded retry, DLQ) is-// packages/core/src/modules/ingest/src/consumer.ts. This file stays (DELEGATE-// PATTERN) and supplies the halves core cannot reach: @openpanel/queue's-// kafkajs client and its topic / consumer-group / retry constants, this-// worker's registry counters, and the events heartbeat.-import { startKafkaEventsConsumer as startKafkaEventsConsumerCore } from '@openpanel/core';-import {- createKafkaEventsConsumer,- KAFKA_EVENTS_TOPIC,- KAFKA_HANDLER_MAX_ATTEMPTS,- KAFKA_HANDLER_RETRY_INITIAL_MS,- KAFKA_HANDLER_RETRY_MAX_MS,- KAFKA_PARTITIONS_CONCURRENT,- kafkaLogger,- produceDeadLetterEvent,-} from '@openpanel/queue';-import {- kafkaDeadLetteredTotal,- kafkaDeadLetterFailedTotal,- kafkaHandlerFailuresTotal,- kafkaReprocessedTotal,-} from '../metrics';-import { logger } from '../utils/logger';-import { markEventsActivity } from '../utils/worker-heartbeat';-import { incomingEvent } from './events.incoming-event';--export type { KafkaConsumerHandle } from '@openpanel/core';--export function startKafkaEventsConsumer() {- return startKafkaEventsConsumerCore({- createConsumer: createKafkaEventsConsumer,- logger,- kafkaLogger,- topic: KAFKA_EVENTS_TOPIC,- partitionsConsumedConcurrently: KAFKA_PARTITIONS_CONCURRENT,- batch: {- handleEvent: incomingEvent,- sendToDeadLetter: produceDeadLetterEvent,- logger,- metrics: {- reprocessed: (partition) => kafkaReprocessedTotal.inc({ partition }),- handlerFailed: (partition) =>- kafkaHandlerFailuresTotal.inc({ partition }),- deadLettered: (partition, reason) =>- kafkaDeadLetteredTotal.inc({ partition, reason }),- deadLetterFailed: (partition) =>- kafkaDeadLetterFailedTotal.inc({ partition }),- },- onActivity: markEventsActivity,- maxAttempts: KAFKA_HANDLER_MAX_ATTEMPTS,- initialRetryMs: KAFKA_HANDLER_RETRY_INITIAL_MS,- maxRetryMs: KAFKA_HANDLER_RETRY_MAX_MS,- },- });-}diff --git a/apps/worker/src/jobs/gsc.ts b/apps/worker/src/jobs/gsc.tsdeleted file mode 100644index 9480be5db..000000000--- a/apps/worker/src/jobs/gsc.ts+++ /dev/null@@ -1,42 +0,0 @@-// Dissolved into @openpanel/core's gsc module (M5-002): the sync/backfill job-// bodies and their status-update bookkeeping moved to-// packages/core/src/modules/gsc/gsc.service.ts. This file stays (DELEGATE-// PATTERN) — it is the BullMQ job body V1's worker registers-// (boot-workers.ts) and the `cron.ts` dispatcher's `gscSync` case, both thin-// wrappers around the core functions.-import {- listGscConnectionsForSync,- runGscProjectBackfill,- runGscProjectSync,-} from '@openpanel/core';-import type { GscQueuePayload } from '@openpanel/queue';-import { gscQueue } from '@openpanel/queue';-import type { Job } from 'bullmq';-import { logger } from '../utils/logger';--export function gscJob(job: Job<GscQueuePayload>) {- switch (job.data.type) {- case 'gscProjectSync':- return runGscProjectSync(job.data.payload.projectId);- case 'gscProjectBackfill':- return runGscProjectBackfill(job.data.payload.projectId);- default:- throw new Error('Unknown GSC job type');- }-}--export async function gscSyncAllJob() {- const connections = await listGscConnectionsForSync();-- logger.info(- { count: connections.length },- 'GSC nightly sync: enqueuing projects'- );-- for (const conn of connections) {- await gscQueue.add('gscProjectSync', {- type: 'gscProjectSync',- payload: { projectId: conn.projectId },- });- }-}diff --git a/apps/worker/src/jobs/import.ts b/apps/worker/src/jobs/import.tsdeleted file mode 100644index 6941ea53b..000000000--- a/apps/worker/src/jobs/import.ts+++ /dev/null@@ -1,15 +0,0 @@-// Dissolved into @openpanel/core's import module (M5-004): the ClickHouse-// staging/session pipeline, the provider dispatch and the job body itself-// moved to packages/core/src/modules/import/import.service.ts. This file-// stays (DELEGATE PATTERN) — it is the BullMQ job body V1's worker registers-// (boot-workers.ts), a thin wrapper handing the real BullMQ `Job` to-// `runImportJob` as its progress reporter (`ImportJobProgress` — a BullMQ-// `Job` satisfies it structurally, see import.service.ts's header).-import { runImportJob } from '@openpanel/core';-import type { ImportQueuePayload } from '@openpanel/queue';-import type { Job } from 'bullmq';--export async function importJob(job: Job<ImportQueuePayload>) {- const { importId } = job.data.payload;- return runImportJob(importId, job);-}diff --git a/apps/worker/src/jobs/insights.ts b/apps/worker/src/jobs/insights.tsdeleted file mode 100644index 23d0739c7..000000000--- a/apps/worker/src/jobs/insights.ts+++ /dev/null@@ -1,44 +0,0 @@-// Dissolved into @openpanel/core's insight module (M5-001): the engine run +-// tier-1 AI enrichment moved to-// packages/core/src/modules/insight/insight.service.ts. This file stays-// (DELEGATE PATTERN) — it is the BullMQ job body V1's worker registers-// (boot-workers.ts, boot-debug.ts) and the `cron.ts` dispatcher's-// `insightsDaily` case, both thin wrappers around the core functions.-import {- listDailyInsightCandidates,- runProjectInsights,-} from '@openpanel/core';-import type {- CronQueuePayload,- InsightsQueuePayloadProject,-} from '@openpanel/queue';-import { insightsQueue } from '@openpanel/queue';-import type { Job } from 'bullmq';-import { logger as baseLogger } from '@/utils/logger';--const logger = baseLogger.child({ job: 'insights' });--export async function insightsDailyJob(_job: Job<CronQueuePayload>) {- const date = new Date().toISOString().slice(0, 10);- const candidates = await listDailyInsightCandidates(date);-- for (const { projectId, date: candidateDate } of candidates) {- await insightsQueue.add(- 'insightsProject',- {- type: 'insightsProject',- payload: { projectId, date: candidateDate },- },- {- jobId: `daily:${candidateDate}:${projectId}`, // idempotent- }- );- }-}--export async function insightsProjectJob(- job: Job<InsightsQueuePayloadProject>-) {- const { projectId, date } = job.data.payload;- await runProjectInsights({ projectId, date, logger });-}diff --git a/apps/worker/src/jobs/lib/email-sequence.ts b/apps/worker/src/jobs/lib/email-sequence.tsdeleted file mode 100644index e0dcf21d2..000000000--- a/apps/worker/src/jobs/lib/email-sequence.ts+++ /dev/null@@ -1,12 +0,0 @@-// Moved into @openpanel/core's shared/ (M6-003): the day-gated sequence-// runner shared by the onboarding drip (now in core) and the wind-down track-// (cron.wind-down.ts, still here). Re-exported for that one remaining-// importer — same shape as packages/db/src/gsc.ts since M5-002.-export type {- RunSequenceOptions,- SequenceResult,- SequenceStep,- SequenceSubject,- StepResult,-} from '@openpanel/core';-export { runSequence, step } from '@openpanel/core';diff --git a/apps/worker/src/jobs/lib/win-back-highlight.test.ts b/apps/worker/src/jobs/lib/win-back-highlight.test.tsdeleted file mode 100644index 585730b9b..000000000--- a/apps/worker/src/jobs/lib/win-back-highlight.test.ts+++ /dev/null@@ -1,138 +0,0 @@-/**- * The stats services and the AI call are mocked — these assert the gating and- * fallback decisions, which are the point of the module: a highlight is a- * bonus, never a reason an email fails or embarrasses us with tiny numbers.- */-import { beforeEach, describe, expect, it, vi } from 'vitest';--const {- getAnalyticsOverviewCoreMock,- getTopPagesCoreMock,- generateWinBackPitchMock,-} = vi.hoisted(() => ({- getAnalyticsOverviewCoreMock: vi.fn(),- getTopPagesCoreMock: vi.fn(),- generateWinBackPitchMock: vi.fn(),-}));--vi.mock('@openpanel/db', () => ({- getAnalyticsOverviewCore: getAnalyticsOverviewCoreMock,- getTopPagesCore: getTopPagesCoreMock,-}));-// Partial mock: `../../utils/logger` also imports `createLogger` from this-// same module, so a full replacement breaks it too.-vi.mock('@openpanel/core', async (importOriginal) => ({- ...(await importOriginal<typeof import('@openpanel/core')>()),- generateWinBackPitch: generateWinBackPitchMock,-}));--import { buildWinBackHighlight } from './win-back-highlight';--const project = { id: 'project-1', name: 'acme-web' };--beforeEach(() => {- vi.clearAllMocks();- getAnalyticsOverviewCoreMock.mockResolvedValue({- summary: { unique_visitors: 12_400 },- series: [- { date: '2026-08-01', unique_visitors: 300 },- { date: '2026-08-12', unique_visitors: 840 },- { date: '2026-08-20', unique_visitors: 500 },- ],- });- getTopPagesCoreMock.mockResolvedValue([- { path: '/pricing', sessions: 2100, pageviews: 3400 },- ]);- generateWinBackPitchMock.mockResolvedValue(- 'acme-web had a strong month with 12,400 visitors.'- );-});--describe('buildWinBackHighlight', () => {- it('returns the AI pitch built from the collected facts', async () => {- const highlight = await buildWinBackHighlight({- project,- recentEventsCount: 50_000,- });-- expect(highlight).toBe('acme-web had a strong month with 12,400 visitors.');- expect(generateWinBackPitchMock).toHaveBeenCalledWith(- expect.objectContaining({- projectName: 'acme-web',- uniqueVisitors: 12_400,- busiestDay: { date: 'August 12', visitors: 840 },- topPage: { path: '/pricing', sessions: 2100 },- })- );- });-- it('skips low-volume orgs entirely — tiny facts undercut the email', async () => {- const highlight = await buildWinBackHighlight({- project,- recentEventsCount: 999,- });-- expect(highlight).toBeUndefined();- expect(getAnalyticsOverviewCoreMock).not.toHaveBeenCalled();- expect(generateWinBackPitchMock).not.toHaveBeenCalled();- });-- it('skips when there is no project to pull facts from', async () => {- const highlight = await buildWinBackHighlight({- project: null,- recentEventsCount: 50_000,- });-- expect(highlight).toBeUndefined();- });-- it('falls back to a deterministic sentence when the AI call fails', async () => {- generateWinBackPitchMock.mockRejectedValue(new Error('rate limited'));-- const highlight = await buildWinBackHighlight({- project,- recentEventsCount: 50_000,- });-- expect(highlight).toContain('acme-web had 12,400 visitors');- expect(highlight).toContain('August 12');- expect(highlight).toContain('/pricing');- });-- it('falls back when the AI returns an empty pitch', async () => {- generateWinBackPitchMock.mockResolvedValue(' ');-- const highlight = await buildWinBackHighlight({- project,- recentEventsCount: 50_000,- });-- expect(highlight).toContain('acme-web had 12,400 visitors');- });-- it('returns nothing when the stats queries fail — the email still goes out', async () => {- getAnalyticsOverviewCoreMock.mockRejectedValue(new Error('ch down'));-- const highlight = await buildWinBackHighlight({- project,- recentEventsCount: 50_000,- });-- expect(highlight).toBeUndefined();- expect(generateWinBackPitchMock).not.toHaveBeenCalled();- });-- it('returns nothing for a window with zero visitors', async () => {- getAnalyticsOverviewCoreMock.mockResolvedValue({- summary: { unique_visitors: 0 },- series: [],- });-- const highlight = await buildWinBackHighlight({- project,- recentEventsCount: 50_000,- });-- expect(highlight).toBeUndefined();- });-});diff --git a/apps/worker/src/jobs/notification.ts b/apps/worker/src/jobs/notification.tsdeleted file mode 100644index ad3161de8..000000000--- a/apps/worker/src/jobs/notification.ts+++ /dev/null@@ -1,15 +0,0 @@-// Dissolved into @openpanel/core's notification module (M6-005): the-// dispatch/delivery body moved to-// packages/core/src/modules/notification/notification.service.ts. This file-// stays (DELEGATE PATTERN) — it is the BullMQ job body V1's worker registers-// (boot-workers.ts), a thin wrapper around the core function.-import { deliverNotification } from '@openpanel/core';-import type { NotificationQueuePayload } from '@openpanel/queue';-import type { Job } from 'bullmq';--export function notificationJob(job: Job<NotificationQueuePayload>) {- switch (job.data.type) {- case 'sendNotification':- return deliverNotification(job.data.payload.notification);- }-}diff --git a/apps/worker/src/jobs/sessions.ts b/apps/worker/src/jobs/sessions.tsdeleted file mode 100644index 2618094e1..000000000--- a/apps/worker/src/jobs/sessions.ts+++ /dev/null@@ -1,19 +0,0 @@-// Dissolved into @openpanel/core's session module (M7-001): the events-count-// refresh and the 80%/100% usage alerts moved to-// packages/core/src/modules/session/src/usage.ts. This file stays (DELEGATE-// PATTERN) — it is what `boot-workers.ts` hands V1's BullMQ sessions Worker.-import { updateEventsCount } from '@openpanel/core';-import type { SessionsQueuePayload } from '@openpanel/queue';-import type { Job } from 'bullmq';-import { createSessionEnd } from './events.create-session-end';-import { logger } from '@/utils/logger';--export async function sessionsJob(job: Job<SessionsQueuePayload>) {- const res = await createSessionEnd(job);- try {- await updateEventsCount(job.data.payload.projectId);- } catch (e) {- logger.error({ err: e }, 'Failed to update events count');- }- return res;-}diff --git a/apps/worker/src/metrics.ts b/apps/worker/src/metrics.tsdeleted file mode 100644index 59563c955..000000000--- a/apps/worker/src/metrics.ts+++ /dev/null@@ -1,300 +0,0 @@-import { registerBufferMetrics } from '@openpanel/core';-import {- botBuffer,- eventBuffer,- groupBuffer,- profileBackfillBuffer,- profileBuffer,- replayBuffer,- sessionBuffer,-} from '@openpanel/db';-import { cronQueue, sessionsQueue } from '@openpanel/queue';-import { getRedisCache } from '@openpanel/redis';-import client from 'prom-client';--const Registry = client.Registry;--export const register = new Registry();--const queues = [sessionsQueue, cronQueue];--export const eventsGroupJobDuration = new client.Histogram({- name: 'job_duration_ms',- help: 'Duration of job processing (in ms)',- labelNames: ['name', 'status'],- buckets: [10, 25, 50, 100, 250, 500, 750, 1000, 2000, 5000, 10_000, 30_000],-});--register.registerMetric(eventsGroupJobDuration);--// Kafka event messages reprocessed (same offset redelivered outside a-// rebalance). Should stay ~0. A sustained non-zero rate means the consumer is-// re-delivering messages it already handled — an offset-handling/duplicate bug.-export const kafkaReprocessedTotal = new client.Counter({- name: 'kafka_events_reprocessed_total',- help: 'Kafka event messages reprocessed (offset redelivered outside a rebalance)',- labelNames: ['partition'],-});--register.registerMetric(kafkaReprocessedTotal);--// Handler exceptions on the Kafka ingest path, counted per attempt — retries-// included, so the series shows real failure pressure and not just the-// messages that ran out of attempts.-export const kafkaHandlerFailuresTotal = new client.Counter({- name: 'kafka_events_handler_failures_total',- help: 'Kafka event handler exceptions (each attempt, retries included)',- labelNames: ['partition'],-});--register.registerMetric(kafkaHandlerFailuresTotal);--// Messages written to the dead-letter topic after their attempts were-// exhausted (or after they failed to parse). One increment per message.-export const kafkaDeadLetteredTotal = new client.Counter({- name: 'kafka_events_dead_lettered_total',- help: 'Kafka event messages produced to the dead-letter topic',- labelNames: ['partition', 'reason'],-});--register.registerMetric(kafkaDeadLetteredTotal);--// The dead-letter produce itself failed. The offset is then left unresolved-// and the message is redelivered, so this is a stuck partition, not a loss.-export const kafkaDeadLetterFailedTotal = new client.Counter({- name: 'kafka_events_dead_letter_failed_total',- help: 'Failed attempts to produce a Kafka event message to the dead-letter topic',- labelNames: ['partition'],-});--register.registerMetric(kafkaDeadLetterFailedTotal);--queues.forEach((queue) => {- register.registerMetric(- new client.Gauge({- name: `${queue.name.replace(/[{}]/g, '')}_active_count`,- help: 'Active count',- async collect() {- const metric = await queue.getActiveCount();- this.set(metric);- },- })- );-- register.registerMetric(- new client.Gauge({- name: `${queue.name.replace(/[{}]/g, '')}_delayed_count`,- help: 'Delayed count',- async collect() {- const metric = await queue.getDelayedCount();- this.set(metric);- },- })- );-- register.registerMetric(- new client.Gauge({- name: `${queue.name.replace(/[{}]/g, '')}_failed_count`,- help: 'Failed count',- async collect() {- const metric = await queue.getFailedCount();- this.set(metric);- },- })- );-- register.registerMetric(- new client.Gauge({- name: `${queue.name.replace(/[{}]/g, '')}_completed_count`,- help: 'Completed count',- async collect() {- const metric = await queue.getCompletedCount();- this.set(metric);- },- })- );-- register.registerMetric(- new client.Gauge({- name: `${queue.name.replace(/[{}]/g, '')}_waiting_count`,- help: 'Waiting count',- async collect() {- const metric = await queue.getWaitingCount();- this.set(metric);- },- })- );-});--// ------------------------------------------------------------------------------// Buffer metrics-// -------------------------------------------------------------------------------// The twelve `buffer_*` series moved to @openpanel/core with the buffers-// themselves (M8-001). V1 keeps its own registry, so it hands one in: the-// names, labels, buckets and registration order are core's, and this body is-// byte-identical to what this file registered before the move.-registerBufferMetrics(- {- event: eventBuffer,- profile: profileBuffer,- bot: botBuffer,- session: sessionBuffer,- replay: replayBuffer,- group: groupBuffer,- profileBackfill: profileBackfillBuffer,- },- register-);--// ------------------------------------------------------------------------// Session lifecycle metrics (new session-buffer + reaper world)-// -------------------------------------------------------------------------// Counters incremented at runtime by the session lifecycle code paths.--export const sessionsStarted = new client.Counter({- name: 'sessions_started_total',- help: 'session_start events emitted, by lifecycle kind',- labelNames: ['kind'], // 'new' | 'boundary'-});-register.registerMetric(sessionsStarted);--export const sessionEndsEnqueued = new client.Counter({- name: 'session_ends_enqueued_total',- help: 'session_end jobs pushed onto the sessions queue, by trigger source',- labelNames: ['source'], // 'boundary' | 'reaper'-});-register.registerMetric(sessionEndsEnqueued);--export const sessionEndsEmitted = new client.Counter({- name: 'session_ends_emitted_total',- help: 'session_end events actually written (post-idempotency claim)',-});-register.registerMetric(sessionEndsEmitted);--export const sessionEndsSkipped = new client.Counter({- name: 'session_ends_skipped_total',- help: 'session_end jobs that ran but did not emit, by reason',- labelNames: ['reason'], // 'not_found' | 'already_emitted'-});-register.registerMetric(sessionEndsSkipped);--export const sessionsReaped = new client.Counter({- name: 'sessions_reaped_total',- help: 'Sessions closed by the reaper, by trigger condition',- labelNames: ['reason'], // 'event-time' | 'deadman'-});-register.registerMetric(sessionsReaped);--export const sessionsReaperOrphans = new client.Counter({- name: 'sessions_reaper_orphans_total',- help: 'Reaper found a sorted-set entry whose session blob is missing. Non-zero usually means TTL mismatch.',- labelNames: ['reason'], // 'event-time' | 'deadman'-});-register.registerMetric(sessionsReaperOrphans);--export const sessionDurationOnClose = new client.Histogram({- name: 'session_duration_ms_on_close',- help: 'Duration of closed sessions (ms)',- buckets: [- 1000, // 1s- 10_000, // 10s- 60_000, // 1m- 5 * 60_000, // 5m- 15 * 60_000, // 15m- 30 * 60_000, // 30m- 60 * 60_000, // 1h- 24 * 60 * 60_000, // 24h- ],-});-register.registerMetric(sessionDurationOnClose);--export const sessionEventsOnClose = new client.Histogram({- name: 'session_events_on_close',- help: 'Total events (event_count + screen_view_count) on session close',- buckets: [1, 2, 5, 10, 25, 50, 100, 250, 500, 1000],-});-register.registerMetric(sessionEventsOnClose);--// Gauges polled at scrape time. The active-sessions gauge does one ZCARD-// per project — cheap individually, fine for hundreds of projects.--register.registerMetric(- new client.Gauge({- name: 'sessions_active_total',- help: 'Active sessions in Redis across all projects',- async collect() {- const redis = getRedisCache();- const projectIds = await redis.smembers('session:projects');- if (projectIds.length === 0) {- this.set(0);- return;- }- const multi = redis.multi();- for (const pid of projectIds) {- multi.zcard(`session:wallclock:${pid}`);- }- const results = await multi.exec();- let total = 0;- for (const entry of results ?? []) {- const count = Number(entry?.[1] ?? 0);- if (Number.isFinite(count)) {- total += count;- }- }- this.set(total);- },- })-);--register.registerMetric(- new client.Gauge({- name: 'sessions_projects_active',- help: 'Projects with at least one active session',- async collect() {- const redis = getRedisCache();- this.set(await redis.scard('session:projects'));- },- })-);--register.registerMetric(- new client.Gauge({- name: 'sessions_hwm_lag_ms',- help: 'Max lag (ms) between wall-clock now and project event-time HWM, across all projects. Big number → queue lag or imports.',- async collect() {- const redis = getRedisCache();- const projectIds = await redis.smembers('session:projects');- if (projectIds.length === 0) {- this.set(0);- return;- }- const multi = redis.multi();- for (const pid of projectIds) {- multi.get(`session:hwm:${pid}`);- }- const results = await multi.exec();- const now = Date.now();- let maxLag = 0;- for (const entry of results ?? []) {- const hwm = Number(entry?.[1] ?? 0);- if (!Number.isFinite(hwm) || hwm <= 0) {- continue;- }- const lag = now - hwm;- if (lag > maxLag) {- maxLag = lag;- }- }- this.set(maxLag);- },- })-);--export const sessionsVacuumed = new client.Counter({- name: 'sessions_vacuumed_total',- help: 'Sessions removed by the daily vacuum cron (catches blobs that cleanup() missed)',- labelNames: ['reason'], // 'stale_blob' | 'missing_blob'-});-register.registerMetric(sessionsVacuumed);diff --git a/apps/worker/src/utils/graceful-shutdown.ts b/apps/worker/src/utils/graceful-shutdown.tsdeleted file mode 100644index 535568c53..000000000--- a/apps/worker/src/utils/graceful-shutdown.ts+++ /dev/null@@ -1,9 +0,0 @@-let shuttingDown = false;--export function setShuttingDown(value: boolean) {- shuttingDown = value;-}--export function isShuttingDown() {- return shuttingDown;-}diff --git a/apps/worker/src/utils/logger.ts b/apps/worker/src/utils/logger.tsdeleted file mode 100644index df9b72691..000000000--- a/apps/worker/src/utils/logger.ts+++ /dev/null@@ -1,3 +0,0 @@-import { createLogger } from '@openpanel/core';--export const logger = createLogger({ name: 'worker' });diff --git a/apps/worker/src/utils/observability.ts b/apps/worker/src/utils/observability.tsdeleted file mode 100644index bd06d6a56..000000000--- a/apps/worker/src/utils/observability.ts+++ /dev/null@@ -1,27 +0,0 @@-import { init } from '@hyperdx/node-opentelemetry';-import { getServiceName, interceptProcessOutput } from '@openpanel/core';-import { logger } from './logger';--// Side-effect module — must stay the first import in index.ts.-//-// Traces only: logs ship via the pino transport and the output interceptor,-// so the SDK's log pipeline and console capture stay off — one shipping path-// per signal, nothing lands twice (see logging-capture-plan.md). Metrics stay-// on prom-client/Grafana.-//-// tsdown bundles most deps (and hoists external imports above this code), so-// require-hook auto-instrumentation only reaches diagnostics_channel-based-// instrumentation (undici/fetch). To get spans from ioredis/bullmq/pg, move-// them to `external` in tsdown.config.ts.-if (process.env.HYPERDX_API_KEY) {- init({- apiKey: process.env.HYPERDX_API_KEY,- service: getServiceName('worker'),- consoleCapture: false,- disableLogs: true,- disableMetrics: true,- disableStartupLogs: true,- });-}--interceptProcessOutput(logger);diff --git a/apps/worker/src/utils/session-handler.ts b/apps/worker/src/utils/session-handler.tsdeleted file mode 100644index 73f5708a5..000000000--- a/apps/worker/src/utils/session-handler.ts+++ /dev/null@@ -1,35 +0,0 @@-// The jobId/payload/retry rules live in @openpanel/core now (M7-001):-// packages/core/src/modules/session/src/session-end.ts. This file stays-// (DELEGATE PATTERN) because V1's BullMQ `sessionsQueue` wire shape is-// `{ type: 'createSessionEnd', payload, snapshot }` — core's-// `legacyCompat.sessions` reads exactly that — and core cannot import-// `@openpanel/queue` (cycle), so the `.add()` itself stays here.-import type { EnqueueSessionEndInput } from '@openpanel/core';-import {- getSessionEndJobId,- sessionEndEnqueueOptions,- sessionEndJobPayload,-} from '@openpanel/core';-import { SESSION_TIMEOUT_MS } from '@openpanel/db';-import { sessionsQueue } from '@openpanel/queue';--export { SESSION_TIMEOUT_MS } from '@openpanel/db';-export const SESSION_TIMEOUT = SESSION_TIMEOUT_MS;--export const getSessionEndJobIdV2 = getSessionEndJobId;--/**- * Enqueue a session_end job. Idempotent via jobId.- *- * Called when a boundary is detected during ingest (old session must close)- * or by the reaper when a session has been idle past the timeout.- */-export async function enqueueSessionEndV2(input: EnqueueSessionEndInput) {- const { event, snapshot } = sessionEndJobPayload(input);-- return sessionsQueue.add(- 'session',- { type: 'createSessionEnd', payload: event, snapshot },- sessionEndEnqueueOptions(input.closedSession.id)- );-}diff --git a/apps/worker/src/utils/worker-heartbeat.ts b/apps/worker/src/utils/worker-heartbeat.tsdeleted file mode 100644index 4b6834353..000000000--- a/apps/worker/src/utils/worker-heartbeat.ts+++ /dev/null@@ -1,23 +0,0 @@-// Consumer-loop heartbeat, scoped to the events queue.-//-// Enabled only on instances that run events workers. Refreshed on each events-// worker's `completed` (job processed) or `drained` (poll returned empty), so-// a healthy consumer loop refreshes the timestamp every ~blockingTimeoutSec-// regardless of traffic. If enabled and the timestamp goes stale past the-// readiness threshold, the events consumer is wedged.--let enabled = false;-let lastActivityAt = Date.now();--export function enableEventsHeartbeat() {- enabled = true;- lastActivityAt = Date.now();-}--export function markEventsActivity() {- lastActivityAt = Date.now();-}--export function getEventsHeartbeat() {- return { enabled, lastActivityAt };-}diff --git a/apps/worker/tsconfig.json b/apps/worker/tsconfig.jsondeleted file mode 100644index 6f9c5b170..000000000--- a/apps/worker/tsconfig.json+++ /dev/null@@ -1,18 +0,0 @@-{- "extends": "@openpanel/tsconfig/base.json",- "compilerOptions": {- "baseUrl": ".",- "paths": {- "@/*": ["./src/*"]- },- "plugins": [- {- "name": "next"- }- ],- "tsBuildInfoFile": "node_modules/.cache/tsbuildinfo.json",- "strictNullChecks": true- },- "include": ["."],- "exclude": ["node_modules", "dist"]-}diff --git a/apps/worker/tsdown.config.ts b/apps/worker/tsdown.config.tsdeleted file mode 100644index 86b00dd5f..000000000--- a/apps/worker/tsdown.config.ts+++ /dev/null@@ -1,32 +0,0 @@-import type { Options } from 'tsdown';-import { defineConfig } from 'tsdown';--const options: Options = {- clean: true,- entry: ['src/index.ts'],- noExternal: [/^@openpanel\/.*$/u, /^@\/.*$/u],- external: [- '@hyperdx/node-opentelemetry',- 'pino',- 'pino-pretty',- '@node-rs/argon2',- // object-store adapter SDKs, now in @openpanel/core- '@aws-sdk/client-s3',- '@aws-sdk/client-sts',- '@google-cloud/storage',- ],- sourcemap: true,- platform: 'node',- shims: true,- inputOptions: {- jsx: 'react',- },-};--if (process.env.WATCH) {- options.watch = ['src', '../../packages'];- options.onSuccess = 'node --enable-source-maps dist/index.js';- options.minify = false;-}--export default defineConfig(options);diff --git a/apps/worker/vitest.config.ts b/apps/worker/vitest.config.tsdeleted file mode 100644index f87a2039f..000000000--- a/apps/worker/vitest.config.ts+++ /dev/null@@ -1,3 +0,0 @@-import { getSharedVitestConfig } from '../../vitest.shared';--export default getSharedVitestConfig({ __dirname });diff --git a/packages/core/src/clients/geo.ts b/packages/core/src/clients/geo.tsindex 81768d839..57ca12e3a 100644--- a/packages/core/src/clients/geo.ts+++ b/packages/core/src/clients/geo.ts@@ -11,12 +11,12 @@ import datacenterAsns from './datacenter-asns';const __filename = fileURLToPath(import.meta.url);const __dirname = dirname(__filename);-// Resolve a bundled `.mmdb` file, trying the api/worker bundled-dist layout-// first and the local package layout second (mirrors how the file ships via-// `pnpm codegen`). apps/api and apps/worker still bundle every `@openpanel/*`-// package into one file (tsdown `noExternal`), so `import.meta.url` there-// resolves to `apps/{api,worker}/dist/index.js` regardless of this file's-// nesting under `src/clients/` — three levels up from dist is the repo root.+// Resolve a bundled `.mmdb` file, trying the bundled-dist layout first and the+// local package layout second (mirrors how the file ships via `pnpm codegen`).+// apps/api's V1 build still bundles every `@openpanel/*` package into one file+// (tsdown `noExternal`), so `import.meta.url` there resolves to+// `apps/api/dist/index.js` regardless of this file's nesting under+// `src/clients/` — three levels up from dist is the repo root.async function loadDatabase(filename: string): Promise<ReaderModel | null> {// From api or worker dist bundleconst dbPath = path.join(__dirname, `../../../packages/core/${filename}`);diff --git a/packages/core/src/clients/integrations/export/export-event.ts b/packages/core/src/clients/integrations/export/export-event.tsindex cda15dd24..822539573 100644--- a/packages/core/src/clients/integrations/export/export-event.ts+++ b/packages/core/src/clients/integrations/export/export-event.ts@@ -1,6 +1,12 @@-import { convertClickhouseDateToJs } from '@openpanel/db/src/clickhouse/client';import type { IClickhouseEvent } from '../../../modules/event/event.service';+// Local copy for the reason every modules/*/src/dates.ts gives: importing+// @openpanel/db's clickhouse/client constructs a ClickHouse client at import+// time, and M9-003 put this module on jobs.registry.ts's eager import chain.+function convertClickhouseDateToJs(date: string): Date {+ return new Date(`${date.replace(' ', 'T')}Z`);+}+/*** Stable, versioned export schema written to the object store (JSONL/Parquet).* Decoupled from the ClickHouse row shape so the export format can evolvediff --git a/packages/core/src/clients/logger.ts b/packages/core/src/clients/logger.tsindex aa484d891..e5dcbea4b 100644--- a/packages/core/src/clients/logger.ts+++ b/packages/core/src/clients/logger.ts@@ -1,9 +1,9 @@// Concrete pino implementation. `../logger.ts` is the structural interface// every module and service codes against; this file is what actually// instantiates pino, and it is deliberately the only place in core that-// imports it (ADR-007 layout: "pino instantiated in apps/api" — apps/api,-// and the still-live V1 apps/worker, both build their named logger by-// calling `createLogger` from here rather than each owning a copy).+// imports it (ADR-007 layout: "pino instantiated in apps/api" — apps/api+// builds its named logger by calling `createLogger` from here rather than+// owning a copy).//// Ported from @openpanel/logger, unchanged (dissolved into core — M4-003).diff --git a/packages/core/src/index.ts b/packages/core/src/index.tsindex 8c963ae3f..7626e7d90 100644--- a/packages/core/src/index.ts+++ b/packages/core/src/index.ts@@ -8,8 +8,7 @@// The concrete pino implementation (dissolved from @openpanel/logger,// M4-003). `./logger` above is the structural interface every module codes-// against; this is what apps/api, and the still-live apps/worker, call to-// build one.+// against; this is what apps/api calls to build one.// The seven buffers (moved from packages/db/src/buffers, M8-001). Only the// FACTORY is on the barrel: they are boot singletons on `AppDeps`, built once// by `main.ts`, never module singletons (ADR-007). V1's@@ -1395,9 +1394,8 @@ export {resolveDateRange,} from './shared/date';// Moved from apps/worker/src/jobs/lib/email-sequence.ts (M6-003) — shared by-// the onboarding module and, until it moves too, apps/worker's own-// cron.wind-down.ts, which reaches it through this barrel (same shape as-// every other still-live V1 consumer here).+// the onboarding module and, since M9-003, the organization module's+// wind-down track.export type {RunSequenceOptions,SequenceResult,diff --git a/packages/core/src/jobs.registry.test.ts b/packages/core/src/jobs.registry.test.tsindex e102d5a77..68b765ec0 100644--- a/packages/core/src/jobs.registry.test.ts+++ b/packages/core/src/jobs.registry.test.ts@@ -86,12 +86,17 @@ test('the insights queue carries the insight module job', () => {// production (jobs/schedulers.ts's `PING_SCHEDULE`), but the job HANDLER is// always registered here so a scheduled run always has somewhere to land.// `salt` (salt.jobs.ts) and the six `flush*` jobs (event/profile/session/-// group.jobs.ts) join the list at M8-004.+// group.jobs.ts) join the list at M8-004; `dataHealth` (misc.jobs.ts),+// `windDown` (organization.jobs.ts) and `flushExports` (integration.jobs.ts)+// at M9-003, which completes the set — all 20 V1 scheduler ids now have a+// handler here.test('the cron queue carries the insight module cron fragment', () => {expect(Object.keys(queues.cron.jobs).sort()).toEqual(['cohortRefresh',+ 'dataHealth','delete',+ 'flushExports','flushEvents','flushGroups','flushProfileBackfill',@@ -107,6 +112,7 @@ test('the cron queue carries the insight module cron fragment', () => {'sessionReaper','sessionVacuum','weeklyDigest',+ 'windDown',].sort());for (const name of ['insightsDaily', 'insightCleanup', 'weeklyDigest']) {@@ -219,24 +225,18 @@ test('the notification queue carries the notification module job', () => {// scheduled run resolves (legacyCompat.cron maps `type` straight onto a job// name) and then throws `has no job named ...`.//-// Three ids do not yet: `dataHealth`, `windDown` and `flushExports` are still-// only implemented in apps/worker/src/jobs (cron.data-health.ts,-// cron.wind-down.ts, cron.flush-exports.ts) and are ported with the rest of-// the worker at M9-003 ("every job the old worker ran is served by the merged-// app under ROLE=worker"). Pinned as a precise, exhaustive exception set so-// the gap is loud here rather than silent in production — and so M9-003 has-// to delete this list to go green.-const CRON_JOBS_NOT_YET_PORTED = ['dataHealth', 'flushExports', 'windDown'];--test('every cron scheduler id has a handler, bar the three M9-003 ports', () => {+// M9-003 closed the last gap: `dataHealth`, `windDown` and `flushExports`+// moved off apps/worker into misc/organization/integration, so this is now an+// exact both-ways match and the exception list is gone. That is the+// machine-checkable half of "every job the old worker ran is served by the+// merged app under ROLE=worker".+test('every cron scheduler id has a handler, and vice versa', () => {const declared = new Set(Object.keys(queues.cron.jobs));const scheduled = [...CRON_SCHEDULES, PING_SCHEDULE].map((scheduler) => scheduler.id);- expect(scheduled.filter((id) => !declared.has(id)).sort()).toEqual(- CRON_JOBS_NOT_YET_PORTED- );+ expect(scheduled.filter((id) => !declared.has(id)).sort()).toEqual([]);// The reverse direction: nothing is registered that nothing ever schedules.expect([...declared].filter((name) => !scheduled.includes(name)).sort()diff --git a/packages/core/src/jobs.registry.ts b/packages/core/src/jobs.registry.tsindex 18944bd60..ee5fa43e2 100644--- a/packages/core/src/jobs.registry.ts+++ b/packages/core/src/jobs.registry.ts@@ -23,6 +23,7 @@ import {insightCronJobs,insightQueueJobs,} from './modules/insight/insight.jobs';+import { integrationCronJobs } from './modules/integration/integration.jobs';import { miscCronJobs } from './modules/misc/misc.jobs';import { notificationQueueJobs } from './modules/notification/notification.jobs';import { onboardingCronJobs } from './modules/onboarding/onboarding.jobs';@@ -80,6 +81,7 @@ const cron = defineQueue('cron', {...onboardingCronJobs,...sessionCronJobs,...miscCronJobs,+ ...integrationCronJobs,},});diff --git a/packages/core/src/jobs/jobs.metrics.ts b/packages/core/src/jobs/jobs.metrics.tsindex ad56c1317..33906191e 100644--- a/packages/core/src/jobs/jobs.metrics.ts+++ b/packages/core/src/jobs/jobs.metrics.ts@@ -1,6 +1,6 @@// The queue-side collectors, moved from apps/worker/src/metrics.ts onto the// one core registry (TARGET_ARCHITECTURE §18). Names, labels and buckets are-// V1's; the worker-side copies die with apps/worker (P9).+// V1's; the worker-side copies died with apps/worker (M9-003).//// Three deliberate differences from V1, all listed in// `packages/core/docs/OPS_GRAFANA_MIGRATION.md`:diff --git a/packages/core/src/jobs/schedulers.ts b/packages/core/src/jobs/schedulers.tsindex 0ef918778..ea2462cc8 100644--- a/packages/core/src/jobs/schedulers.ts+++ b/packages/core/src/jobs/schedulers.ts@@ -10,6 +10,8 @@ import { eventCronSchedules } from '../modules/event/event.jobs';import { groupCronSchedules } from '../modules/group/group.jobs';import { gscCronSchedules } from '../modules/gsc/gsc.jobs';import { insightCronSchedules } from '../modules/insight/insight.jobs';+import { integrationCronSchedules } from '../modules/integration/integration.jobs';+import { miscCronSchedules } from '../modules/misc/misc.jobs';import { onboardingCronSchedules } from '../modules/onboarding/onboarding.jobs';import { organizationCronSchedules } from '../modules/organization/organization.jobs';import { profileCronSchedules } from '../modules/profile/profile.jobs';@@ -59,8 +61,6 @@ const CONFLICT_JOB_STATES = [const CONFLICT_ERROR_SUBSTRING = 'job ID already exists';-const MINUTE_MS = 60_000;-// V1's exact 19 always-on scheduler ids and cadences// (apps/worker/src/boot-cron.ts). `ping` is the 20th and is conditional —// see `PING_SCHEDULE` and `startSchedulers`.@@ -68,8 +68,8 @@ export const CRON_SCHEDULES: readonly SchedulerDefinition[] = [// salt — owned by the salt module, declared next to its jobs// (modules/salt/salt.jobs.ts)....saltCronSchedules,- // delete — owned by the organization module, declared next to its jobs- // (modules/organization/organization.jobs.ts).+ // delete / windDown — owned by the organization module, declared next to its+ // jobs (modules/organization/organization.jobs.ts)....organizationCronSchedules,// flushEvents — owned by the event module, declared next to its jobs// (modules/event/event.jobs.ts).@@ -98,13 +98,14 @@ export const CRON_SCHEDULES: readonly SchedulerDefinition[] = [// insightsDaily / insightCleanup / weeklyDigest — owned by the insight// module, declared next to its jobs (modules/insight/insight.jobs.ts)....insightCronSchedules,- // Daily 07:30 UTC — no-data / data-stopped rescue emails. Owner: misc- // (ADR-005 acceptance note completes the ownership map).- { id: 'dataHealth', schedule: { pattern: '30 7 * * *' } },- // Hourly — expired-trial wind-down emails, block, delete. Owner: organization.- { id: 'windDown', schedule: { pattern: '0 * * * *' } },- // Every 1 minute — drains export buffers to S3/GCS. Owner: integration.- { id: 'flushExports', schedule: { every: MINUTE_MS } },+ // dataHealth — owned by the misc module, declared next to its jobs+ // (modules/misc/misc.jobs.ts). ADR-005's acceptance note completed the+ // ownership map for this and the two below; M9-003 moved the handlers off+ // apps/worker, so each now sits with its owner rather than inline here.+ ...miscCronSchedules,+ // flushExports — owned by the integration module, declared next to its jobs+ // (modules/integration/integration.jobs.ts).+ ...integrationCronSchedules,];// V1 gated this on `SELF_HOSTED && NODE_ENV === 'production'`diff --git a/packages/core/src/modules/integration/integration.jobs.ts b/packages/core/src/modules/integration/integration.jobs.tsnew file mode 100644index 000000000..3f833adb6--- /dev/null+++ b/packages/core/src/modules/integration/integration.jobs.ts@@ -0,0 +1,31 @@+// Ported from apps/worker/src/jobs/cron.flush-exports.ts + boot-cron.ts's+// `flushExports` schedule (M9-003, the wave that deletes apps/worker).+// ADR-005's acceptance note gives `flushExports` to this module.+//+// `flushExports` is this module's fragment of the ONE `cron` queue's jobs:+// declared here, spread into jobs.registry.ts and into `CRON_SCHEDULES` by+// jobs/schedulers.ts. Scheduler id and cadence are V1's, unchanged.++import { z } from 'zod';+import { defineJob } from '../../jobs/define';+import type { SchedulerDefinition } from '../../jobs/schedulers';+import { loadFlushExportsDeps, runFlushExportsCron } from './src/flush-exports';++/** Every 1 minute — drains export buffers to S3/GCS. */+const FLUSH_EXPORTS_INTERVAL_MS = 60_000;++/** This module's fragment of the `cron` queue's jobs. */+export const integrationCronJobs = {+ flushExports: defineJob({+ payload: z.null(),+ handler: async ({ ctx }) => {+ const logger = ctx.logger.child({ job: 'flush-exports' });+ await runFlushExportsCron(await loadFlushExportsDeps(logger));+ },+ }),+};++/** This module's fragment of `CRON_SCHEDULES` — id and cadence unchanged. */+export const integrationCronSchedules: readonly SchedulerDefinition[] = [+ { id: 'flushExports', schedule: { every: FLUSH_EXPORTS_INTERVAL_MS } },+];diff --git a/packages/core/src/modules/integration/src/flush-exports.test.ts b/packages/core/src/modules/integration/src/flush-exports.test.tsnew file mode 100644index 000000000..d184855e8--- /dev/null+++ b/packages/core/src/modules/integration/src/flush-exports.test.ts@@ -0,0 +1,284 @@+// Moved from apps/worker/src/jobs/cron.flush-exports.test.ts (M9-003), plus+// the orchestration half V1 never covered: `runFlushExportsCron` now takes+// injected deps, so the fan-out, the watermark advance and the manifest-last+// upload order can be asserted with no infrastructure at all.++import { describe, expect, it, mock } from 'bun:test';+import { gunzipSync } from 'node:zlib';+import {+ clickhouseEventToExportEvent,+ createBatch,+ createManifest,+ generateBatchPath,+ MANIFEST_CONTENT_TYPE,+ MANIFEST_FILENAME,+ parseManifest,+ serializeManifest,+} from '../../../clients/integrations/export';+import { createGCSAdapter } from '../../../clients/integrations/object-store/gcs-adapter';+import type { Logger } from '../../../logger';+import {+ type ExportIntegrationRow,+ type ExportWatermarkRow,+ type FlushExportsDeps,+ runFlushExportsCron,+} from './flush-exports';++function stubLogger(): Logger {+ const noop = () => undefined;+ const logger: Logger = {+ fatal: noop,+ error: noop,+ warn: noop,+ info: noop,+ debug: noop,+ trace: noop,+ child: () => logger,+ };+ return logger;+}++const chEvent = (i: number) =>+ ({+ id: `00000000-0000-0000-0000-00000000000${i}`,+ project_id: 'proj_1',+ name: 'screen_view',+ created_at: `2026-08-26 10:0${i}:00.000`,+ inserted_at: `2026-08-26 11:0${i}:00.000`,+ profile_id: `user_${i}`,+ device_id: `dev_${i}`,+ session_id: `sess_${i}`,+ properties: { __path: `/p/${i}`, n: i },+ country: 'SE',+ city: 'Stockholm',+ region: 'AB',+ os: 'macOS',+ browser: 'Chrome',+ device: 'desktop',+ path: `/p/${i}`,+ origin: 'https://openpanel.dev',+ referrer: '',+ }) as never;++const gcsIntegration: ExportIntegrationRow = {+ id: 'int_1',+ organizationId: 'org_1',+ projectId: 'proj_1',+ config: {+ type: 'gcs_export',+ bucket: 'op-bucket',+ prefix: 'openpanel-exports',+ format: 'jsonl_gzip',+ serviceAccountKey: 'unused — the adapter is stubbed',+ } as never,+};++const watermark: ExportWatermarkRow = {+ lastInsertedAt: new Date('2026-08-26T09:00:00.000Z'),+ lastEventId: '00000000-0000-0000-0000-000000000000',+};++function fakeDeps(rows: unknown[][]) {+ const uploads: { key: string; contentType: string }[] = [];+ const watermarkUpdates: unknown[] = [];+ const windows = [...rows];+ const query = mock(async () => ({+ json: async () => windows.shift() ?? [],+ }));++ const deps: FlushExportsDeps = {+ db: {+ integration: { findMany: async () => [gcsIntegration] },+ project: { findMany: async () => [] },+ exportWatermark: {+ findUnique: async () => watermark,+ create: async () => ({}),+ update: async (args) => {+ watermarkUpdates.push(args);+ return {};+ },+ },+ },+ ch: { query } as unknown as FlushExportsDeps['ch'],+ logger: stubLogger(),+ createAdapter: () =>+ ({+ upload: async ({+ key,+ contentType,+ }: {+ key: string;+ contentType: string;+ }) => {+ uploads.push({ key, contentType });+ return { key };+ },+ }) as never,+ };++ return { deps, uploads, watermarkUpdates, query };+}++describe('runFlushExportsCron', () => {+ it('uploads the data files before the manifest and advances the watermark', async () => {+ const { deps, uploads, watermarkUpdates } = fakeDeps([+ [1, 2, 3].map(chEvent),+ ]);++ await runFlushExportsCron(deps);++ expect(uploads).toHaveLength(2);+ expect(uploads[0]?.key).toContain('part-0000.jsonl.gz');+ // The manifest is the commit marker: it must land last.+ expect(uploads[1]?.key.endsWith(`/${MANIFEST_FILENAME}`)).toBe(true);+ expect(uploads[1]?.contentType).toBe(MANIFEST_CONTENT_TYPE);++ expect(watermarkUpdates).toHaveLength(1);+ expect(watermarkUpdates[0]).toMatchObject({+ data: {+ lastEventId: '00000000-0000-0000-0000-000000000003',+ lastInsertedAt: new Date('2026-08-26T11:03:00.000Z'),+ },+ });+ });++ it('does nothing when no integration declares an export capability', async () => {+ const { deps, uploads } = fakeDeps([]);+ deps.db.integration.findMany = async () => [+ { ...gcsIntegration, config: { type: 'slack' } as never },+ ];++ await runFlushExportsCron(deps);++ expect(uploads).toHaveLength(0);+ });++ it('stops draining once a short window comes back', async () => {+ const { deps, query } = fakeDeps([[1, 2].map(chEvent)]);++ await runFlushExportsCron(deps);++ // Two rows is far under BATCH_SIZE, so one window is the whole backlog.+ expect(query).toHaveBeenCalledTimes(1);+ });+});++/**+ * Exercises the object-store export path end to end against a local+ * fake-gcs-server (see clients/integrations/object-store/gcs-adapter.test.ts+ * for how to start it). The job's own ClickHouse/Postgres I/O is out of scope+ * here; what this pins down is the part a consumer depends on — batch files+ * land, then a manifest that points at them, under the partitioned path.+ *+ * Skips when the emulator isn't reachable.+ */+const EMULATOR = process.env.GCS_API_ENDPOINT ?? 'http://localhost:4443';+const BUCKET = 'op-flush-exports-test';+const EMULATOR_TIMEOUT_MS = 2000;+const EMULATOR_TEST_TIMEOUT_MS = 60_000;++async function emulatorReachable(): Promise<boolean> {+ try {+ const res = await fetch(`${EMULATOR}/storage/v1/b`, {+ signal: AbortSignal.timeout(EMULATOR_TIMEOUT_MS),+ });+ return res.ok;+ } catch {+ return false;+ }+}++const available = await emulatorReachable();++// A structurally complete service account document. fake-gcs-server does no+// auth, but the adapter pins the credential type and requires the fields a real+// key has before it will build a client.+const KEY = JSON.stringify({+ type: 'service_account',+ project_id: 'openpanel-test',+ client_email: '[email protected]',+ private_key: 'test-private-key',+});++describe.skipIf(!available)('flush-exports -> GCS end to end', () => {+ it(+ 'writes batch files then a manifest that points at them',+ async () => {+ process.env.GCS_API_ENDPOINT = EMULATOR;+ await fetch(`${EMULATOR}/storage/v1/b`, {+ method: 'POST',+ headers: { 'Content-Type': 'application/json' },+ body: JSON.stringify({ name: BUCKET }),+ });++ const config = {+ type: 'gcs_export' as const,+ bucket: BUCKET,+ prefix: 'openpanel-exports',+ format: 'jsonl_gzip' as const,+ serviceAccountKey: KEY,+ };+ const adapter = createGCSAdapter(config);++ // --- 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 basePath = generateBatchPath(+ config.prefix,+ 'proj_1',+ 'int_1',+ batch.info.batchId,+ new Date(batch.info.minEventTime)+ );++ for (const file of batch.files) {+ await adapter.upload({+ bucket: config.bucket,+ key: `${basePath}/${file.filename}`,+ content: file.content,+ contentType: file.contentType,+ });+ }+ const manifest = createManifest(+ batch.info,+ batch.files.map((f) => f.filename)+ );+ await adapter.upload({+ bucket: config.bucket,+ key: `${basePath}/${MANIFEST_FILENAME}`,+ content: serializeManifest(manifest),+ contentType: MANIFEST_CONTENT_TYPE,+ });+ // --- end ---++ const read = async (key: string) => {+ const res = await fetch(+ `${EMULATOR}/storage/v1/b/${BUCKET}/o/${encodeURIComponent(key)}?alt=media`+ );+ expect(res.ok).toBe(true);+ return Buffer.from(await res.arrayBuffer());+ };++ const storedManifest = parseManifest(+ (await read(`${basePath}/${MANIFEST_FILENAME}`)).toString()+ );+ expect(storedManifest.record_count).toBe(3);+ expect(storedManifest.files).toEqual(['part-0000.jsonl.gz']);+ expect(storedManifest.partition_date).toBe('2026-08-26');++ const lines = gunzipSync(+ await read(`${basePath}/${storedManifest.files[0]}`)+ )+ .toString()+ .trim()+ .split('\n')+ .map((l) => JSON.parse(l));++ expect(lines).toHaveLength(3);+ expect(lines[0].event_name).toBe('screen_view');+ expect(lines[0].project_id).toBe('proj_1');+ expect(lines.map((l) => l.path)).toEqual(['/p/1', '/p/2', '/p/3']);+ },+ EMULATOR_TEST_TIMEOUT_MS+ );+});diff --git a/apps/worker/src/jobs/cron.flush-exports.ts b/packages/core/src/modules/integration/src/flush-exports.tssimilarity index 66%rename from apps/worker/src/jobs/cron.flush-exports.tsrename to packages/core/src/modules/integration/src/flush-exports.tsindex d40e9dcc3..0e3d97f45 100644--- a/apps/worker/src/jobs/cron.flush-exports.ts+++ b/packages/core/src/modules/integration/src/flush-exports.ts@@ -1,33 +1,33 @@+// Ported from apps/worker/src/jobs/cron.flush-exports.ts (M9-003, the wave that+// deletes apps/worker). ADR-005's acceptance note gives `flushExports` to the+// integration module. Behaviour is V1's, verbatim; Prisma, the ClickHouse+// client and the object-store adapter factory arrive as injected deps so the+// orchestration is testable without `mock.module` (same idiom as+// modules/session/src/runtime.ts).+//+// The window query is a MOVE, not an ADR-013 conversion: it already binds every+// value through ClickHouse's own `{name:Type}` params, and converting it to the+// `sql` tag would change the emitted statement. Kept byte-identical.+import { DateTime } from '@openpanel/common';import {- createLogger,- getServerIntegration,- type IObjectStoreAdapter,-} from '@openpanel/core';-import {- ch,clickhouseEventToExportEvent,- convertClickhouseDateToJs,createBatch,createManifest,- db,generateBatchPath,- type IClickhouseEvent,MANIFEST_CONTENT_TYPE,MANIFEST_FILENAME,serializeManifest,- TABLE_NAMES,-} from '@openpanel/db';-import type { CronQueuePayload } from '@openpanel/queue';+} from '../../../clients/integrations/export';+import type { IObjectStoreAdapter } from '../../../clients/integrations/object-store';+import type { Logger } from '../../../logger';+import type { IClickhouseEvent } from '../../event/event.service';import {type IGCSExportConfig,type IIntegrationConfig,type IS3ExportConfig,isKind,-} from '@openpanel/validation';-import type { Job } from 'bullmq';--const logger = createLogger({ name: 'flush-exports' });+} from '../integration.constants';// 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@@ -52,6 +52,11 @@ const CONCURRENCY = Number.parseInt(process.env.EXPORT_CONCURRENCY || '4', 10);// would skip all but one row.const NIL_UUID = '00000000-0000-0000-0000-000000000000';+// Local literal, not @openpanel/db's TABLE_NAMES — same choice cohort.service.ts+// made, and for the same reason: importing that module constructs a ClickHouse+// client at import time.+const EVENTS_TABLE = 'events';+const EXPORT_COLUMNS = `id, name, sdk_name, sdk_version, device_id, profile_id, project_id,session_id, path, origin, referrer, referrer_name, referrer_type,@@ -69,6 +74,64 @@ interface Cursor {eventId: string;}+/** The ClickHouse surface this job touches — nothing wider. */+export interface ExportClickhouse {+ query(params: {+ query: string;+ query_params: Record<string, unknown>;+ format: 'JSONEachRow';+ }): Promise<{ json(): Promise<unknown> }>;+}++export interface ExportIntegrationRow {+ id: string;+ organizationId: string;+ projectId: string | null;+ config: IIntegrationConfig;+}++export interface ExportWatermarkRow {+ lastInsertedAt: Date;+ lastEventId: string;+}++/** The Prisma surface this job touches — nothing wider. */+export interface ExportDb {+ integration: { findMany(args?: unknown): Promise<ExportIntegrationRow[]> };+ project: { findMany(args: unknown): Promise<{ id: string }[]> };+ exportWatermark: {+ findUnique(args: unknown): Promise<ExportWatermarkRow | null>;+ create(args: unknown): Promise<unknown>;+ update(args: unknown): Promise<unknown>;+ };+}++export interface FlushExportsDeps {+ db: ExportDb;+ ch: ExportClickhouse;+ logger: Logger;+ /** Resolves the object-store adapter for an export config, or undefined. */+ createAdapter(config: ExportConfig): IObjectStoreAdapter | undefined;+}++export async function loadFlushExportsDeps(+ logger: Logger+): Promise<FlushExportsDeps> {+ const [{ db }, { ch }, { getServerIntegration }] = await Promise.all([+ import('@openpanel/db/src/prisma-client'),+ import('@openpanel/db/src/clickhouse/client'),+ import('../../../clients/integrations/registry'),+ ]);++ return {+ db: db as unknown as ExportDb,+ ch: ch as unknown as ExportClickhouse,+ logger,+ createAdapter: (config) =>+ getServerIntegration(config.type).export?.createAdapter(config),+ };+}+function isExportConfig(config: IIntegrationConfig): config is ExportConfig {// Capability comes from the integration registry, not a hardcoded type list.return isKind(config, 'export');@@ -84,8 +147,10 @@ const formatCh = (date: Date): string =>* truth, so this job windows the events table by `inserted_at` and uploads* batched files. Export never touches the ingestion path.*/-export async function flushExportsJob(_job: Job<CronQueuePayload>) {- const integrations = await db.integration.findMany();+export async function runFlushExportsCron(+ deps: FlushExportsDeps+): Promise<void> {+ const integrations = await deps.db.integration.findMany();const exportIntegrations = integrations.filter((i) =>isExportConfig(i.config));@@ -115,7 +180,7 @@ export async function flushExportsJob(_job: Job<CronQueuePayload>) {continue;}- const projects = await db.project.findMany({+ const projects = await deps.db.project.findMany({where: { organizationId: integration.organizationId, deleteAt: null },select: { id: true },});@@ -129,9 +194,9 @@ export async function flushExportsJob(_job: Job<CronQueuePayload>) {}await runWithConcurrency(items, CONCURRENCY, (item) =>- processExport(item.projectId, item.integrationId, item.config).catch(+ processExport(item.projectId, item.integrationId, item.config, deps).catch((error) => {- logger.error(+ deps.logger.error({err: error,projectId: item.projectId,@@ -147,12 +212,11 @@ export async function flushExportsJob(_job: Job<CronQueuePayload>) {async function processExport(projectId: string,integrationId: string,- config: ExportConfig+ config: ExportConfig,+ deps: FlushExportsDeps): Promise<void> {- let cursor = await loadCursor(projectId, integrationId);- const adapter: IObjectStoreAdapter | undefined = getServerIntegration(- config.type- ).export?.createAdapter(config);+ let cursor = await loadCursor(projectId, integrationId, deps);+ const adapter = deps.createAdapter(config);if (!adapter) {throw new Error(`Integration ${config.type} has no export adapter`);}@@ -160,7 +224,7 @@ async function processExport(const format = config.format || 'jsonl_gzip';for (let i = 0; i < MAX_BATCHES_PER_RUN; i++) {- const rows = await queryWindow(projectId, cursor);+ const rows = await queryWindow(projectId, cursor, deps);if (rows.length === 0) {break;}@@ -198,11 +262,11 @@ async function processExport(// Advance the watermark only after a successful upload. A crash mid-run// re-exports the un-acked batch next tick (at-least-once); the manifest is// the consumer's signal that a batch is complete.- const last = rows[rows.length - 1]!;+ const last = rows.at(-1)!;cursor = { insertedAt: last.inserted_at!, eventId: last.id };- await saveCursor(projectId, integrationId, cursor);+ await saveCursor(projectId, integrationId, cursor, deps);- logger.info(+ deps.logger.info({projectId,integrationId,@@ -221,12 +285,13 @@ async function processExport(async function queryWindow(projectId: string,- cursor: Cursor+ cursor: Cursor,+ deps: FlushExportsDeps): Promise<IClickhouseEvent[]> {- const result = await ch.query({+ const result = await deps.ch.query({query: `SELECT ${EXPORT_COLUMNS}- FROM ${TABLE_NAMES.events}+ FROM ${EVENTS_TABLE}WHERE project_id = {projectId:String}AND inserted_at <= now64(3) - INTERVAL {lag:UInt32} SECONDAND (@@ -251,9 +316,10 @@ async function queryWindow(async function loadCursor(projectId: string,- integrationId: string+ integrationId: string,+ deps: FlushExportsDeps): Promise<Cursor> {- const existing = await db.exportWatermark.findUnique({+ const existing = await deps.db.exportWatermark.findUnique({where: { projectId_integrationId: { projectId, integrationId } },});if (existing) {@@ -267,7 +333,7 @@ async function loadCursor(// dump the project's entire history. Historical backfill is a separate,// explicit operation (reset the watermark).const now = new Date();- await db.exportWatermark.create({+ await deps.db.exportWatermark.create({data: {projectId,integrationId,@@ -281,9 +347,10 @@ async function loadCursor(async function saveCursor(projectId: string,integrationId: string,- cursor: Cursor+ cursor: Cursor,+ deps: FlushExportsDeps): Promise<void> {- await db.exportWatermark.update({+ await deps.db.exportWatermark.update({where: { projectId_integrationId: { projectId, integrationId } },data: {lastInsertedAt: convertClickhouseDateToJs(cursor.insertedAt),@@ -292,6 +359,12 @@ async function saveCursor(});}+// Local copy for the reason every modules/*/src/dates.ts gives: importing+// @openpanel/db's clickhouse/client constructs a client at import time.+function convertClickhouseDateToJs(date: string): Date {+ return new Date(`${date.replace(' ', 'T')}Z`);+}+async function runWithConcurrency<T>(items: T[],limit: number,diff --git a/packages/core/src/modules/misc/misc.jobs.ts b/packages/core/src/modules/misc/misc.jobs.tsindex 4893bb092..248288042 100644--- a/packages/core/src/modules/misc/misc.jobs.ts+++ b/packages/core/src/modules/misc/misc.jobs.ts@@ -1,5 +1,6 @@// Ported from apps/worker/src/jobs/cron.ping.ts + boot-cron.ts's `ping`-// schedule (M7-008).+// schedule (M7-008); `dataHealth` joined it at M9-003, the wave that deletes+// apps/worker (ADR-005's acceptance note gives it to this module).//// `ping` is this module's fragment of the ONE `cron` queue's jobs: declared// here and spread into jobs.registry.ts, same shape as@@ -10,6 +11,11 @@import { z } from 'zod';import { defineJob } from '../../jobs/define';+import type { SchedulerDefinition } from '../../jobs/schedulers';+import { loadDataHealthDeps, runDataHealthCron } from './src/data-health';++/** Daily 07:30 UTC — V1's cadence (apps/worker/src/boot-cron.ts). */+const DATA_HEALTH_CRON = '30 7 * * *';/** This module's fragment of the `cron` queue's jobs. */export const miscCronJobs = {@@ -19,4 +25,16 @@ export const miscCronJobs = {await ctx.services.misc.runPingCron();},}),+ dataHealth: defineJob({+ payload: z.null(),+ handler: async ({ ctx }) => {+ const logger = ctx.logger.child({ job: 'data-health' });+ await runDataHealthCron(await loadDataHealthDeps(logger));+ },+ }),};++/** This module's fragment of `CRON_SCHEDULES` — id and cadence unchanged. */+export const miscCronSchedules: readonly SchedulerDefinition[] = [+ { id: 'dataHealth', schedule: { pattern: DATA_HEALTH_CRON } },+];diff --git a/apps/worker/src/jobs/cron.data-health.ts b/packages/core/src/modules/misc/src/data-health.tssimilarity index 64%rename from apps/worker/src/jobs/cron.data-health.tsrename to packages/core/src/modules/misc/src/data-health.tsindex 5dc2e7513..3ddaddefc 100644--- a/apps/worker/src/jobs/cron.data-health.ts+++ b/packages/core/src/modules/misc/src/data-health.ts@@ -1,14 +1,17 @@-import { db, getLastEventPerProject } from '@openpanel/db';-import { sendEmail } from '@openpanel/email';-import { logger as baseLogger } from '@/utils/logger';+// Ported from apps/worker/src/jobs/cron.data-health.ts (M9-003, the wave that+// deletes apps/worker). ADR-005's acceptance note gives `dataHealth` to the+// misc module. Behaviour is V1's, verbatim; Prisma, the last-event lookup and+// `sendEmail` arrive as injected deps so the job is testable without+// `mock.module` (same idiom as modules/session/src/runtime.ts).-const logger = baseLogger.child({ job: 'data-health' });+import type { Logger } from '../../../logger';const DAY_MS = 24 * 60 * 60 * 1000;// A brand-new project gets 48h to send its first event before we reach out.const NO_DATA_AFTER_MS = 2 * DAY_MS;// An active project whose newest event is older than this counts as stalled.const STALLED_AFTER_MS = 7 * DAY_MS;+const DEFAULT_DASHBOARD_URL = 'https://dashboard.openpanel.dev';interface OrgAlert {organizationId: string;@@ -16,8 +19,68 @@ interface OrgAlert {stalled: { id: string; name: string; lastEventAt: Date }[];}-async function recipientsForOrg(organizationId: string) {- const members = await db.member.findMany({+/** The project row shape this job reads. */+export interface DataHealthProject {+ id: string;+ name: string;+ createdAt: Date;+ organizationId: string;+ noDataNotifiedAt: Date | null;+ dataStoppedNotifiedAt: Date | null;+ organization: {+ id: string;+ subscriptionState: string;+ onboarding: string | null;+ };+}++export interface DataHealthMember {+ user: { email: string | null; firstName: string | null } | null;+}++/** The Prisma surface this job touches — nothing wider. */+export interface DataHealthDb {+ member: { findMany(args: unknown): Promise<DataHealthMember[]> };+ project: {+ findMany(args: unknown): Promise<DataHealthProject[]>;+ updateMany(args: unknown): Promise<unknown>;+ };+}++export type DataHealthTemplate = 'tracking-no-data' | 'tracking-data-stopped';++export interface DataHealthDeps {+ db: DataHealthDb;+ logger: Logger;+ getLastEventPerProject(): Promise<Map<string, Date>>;+ sendEmail(+ template: DataHealthTemplate,+ options: { to: string; data: Record<string, unknown> }+ ): Promise<unknown>;+}++export async function loadDataHealthDeps(+ logger: Logger+): Promise<DataHealthDeps> {+ const [{ db }, { getLastEventPerProject }, { sendEmail }] = await Promise.all(+ [+ import('@openpanel/db/src/prisma-client'),+ import('../../project/project.service'),+ import('../../../clients/email'),+ ]+ );++ return {+ db: db as unknown as DataHealthDb,+ logger,+ getLastEventPerProject,+ sendEmail: (template, options) =>+ sendEmail(template, options as { to: string; data: never }),+ };+}++async function recipientsForOrg(organizationId: string, deps: DataHealthDeps) {+ const members = await deps.db.member.findMany({where: {organizationId,user: { deletedAt: null },@@ -33,6 +96,12 @@ async function recipientsForOrg(organizationId: string) {return seen;}+export interface DataHealthResult extends Record<string, number> {+ projects: number;+ organizations: number;+ emailsSent: number;+}+/*** Daily rescue emails for paying/trialing orgs whose tracking is broken:* projects that never received an event (48h grace) and projects whose event@@ -43,17 +112,19 @@ async function recipientsForOrg(organizationId: string) {* is compared against the newest event, so data resuming and stalling again* notifies again without any clearing step.*/-export async function dataHealthCronJob() {+export async function runDataHealthCron(+ deps: DataHealthDeps+): Promise<DataHealthResult | null> {if (process.env.SELF_HOSTED === 'true') {- return;+ return null;}const now = Date.now();- const lastEventByProject = await getLastEventPerProject();+ const lastEventByProject = await deps.getLastEventPerProject();// Prefilter on the raw status column (computed fields can't be used in// `where`), then refine with the canonical subscription state below.- const projects = await db.project.findMany({+ const projects = await deps.db.project.findMany({where: {deleteAt: null,organization: { subscriptionStatus: { in: ['active', 'trialing'] } },@@ -123,19 +194,19 @@ export async function dataHealthCronJob() {for (const alert of byOrg.values()) {try {- const recipients = await recipientsForOrg(alert.organizationId);+ const recipients = await recipientsForOrg(alert.organizationId, deps);if (recipients.size === 0) {continue;}- const dashboardUrl = `${process.env.DASHBOARD_URL ?? 'https://dashboard.openpanel.dev'}/${alert.organizationId}`;+ const dashboardUrl = `${process.env.DASHBOARD_URL ?? DEFAULT_DASHBOARD_URL}/${alert.organizationId}`;if (alert.noData.length > 0) {for (const [to, firstName] of recipients) {// Per-recipient guard: one bad address must not block the marker// update below — that would re-email everyone tomorrow.try {- await sendEmail('tracking-no-data', {+ await deps.sendEmail('tracking-no-data', {to,data: {firstName,@@ -145,13 +216,13 @@ export async function dataHealthCronJob() {});emailsSent++;} catch (err) {- logger.error(+ deps.logger.error({ err, organizationId: alert.organizationId, recipient: to },'Failed to send no-data alert to recipient');}}- await db.project.updateMany({+ await deps.db.project.updateMany({where: { id: { in: alert.noData.map((p) => p.id) } },data: { noDataNotifiedAt: new Date() },});@@ -163,7 +234,7 @@ export async function dataHealthCronJob() {.sort((a, b) => b.getTime() - a.getTime())[0];for (const [to, firstName] of recipients) {try {- await sendEmail('tracking-data-stopped', {+ await deps.sendEmail('tracking-data-stopped', {to,data: {firstName,@@ -177,31 +248,32 @@ export async function dataHealthCronJob() {});emailsSent++;} catch (err) {- logger.error(+ deps.logger.error({ err, organizationId: alert.organizationId, recipient: to },'Failed to send data-stopped alert to recipient');}}- await db.project.updateMany({+ await deps.db.project.updateMany({where: { id: { in: alert.stalled.map((p) => p.id) } },data: { dataStoppedNotifiedAt: new Date() },});}} catch (err) {- logger.error(+ deps.logger.error({ err, organizationId: alert.organizationId },'Data-health alert failed');}}- logger.info(- {- projects: projects.length,- organizations: byOrg.size,- emailsSent,- },- 'Data-health check complete'- );+ const result: DataHealthResult = {+ projects: projects.length,+ organizations: byOrg.size,+ emailsSent,+ };++ deps.logger.info(result, 'Data-health check complete');++ return result;}diff --git a/packages/core/src/modules/organization/organization.jobs.test.ts b/packages/core/src/modules/organization/organization.jobs.test.tsindex c92495b5e..bff8dc3d2 100644--- a/packages/core/src/modules/organization/organization.jobs.test.ts+++ b/packages/core/src/modules/organization/organization.jobs.test.ts@@ -71,6 +71,7 @@ test('the cron queue declares delete on the cron queue', () => {test('the organization cron fragment is spread into the cron queue', () => {expect(queues.cron.jobs.delete).toBeDefined();+ expect(queues.cron.jobs.windDown).toBeDefined();});test('delete delegates to runDeleteCron', async () => {@@ -92,5 +93,6 @@ test('delete delegates to runDeleteCron', async () => {test('the organization cron fragment carries V1 id and cadence unchanged', () => {expect(organizationCronSchedules).toEqual([{ id: 'delete', schedule: { pattern: '0 * * * *' } },+ { id: 'windDown', schedule: { pattern: '0 * * * *' } },]);});diff --git a/packages/core/src/modules/organization/organization.jobs.ts b/packages/core/src/modules/organization/organization.jobs.tsindex 6b84c53cf..ce475afc9 100644--- a/packages/core/src/modules/organization/organization.jobs.ts+++ b/packages/core/src/modules/organization/organization.jobs.ts@@ -1,5 +1,6 @@// Ported from apps/worker/src/jobs/cron.delete.ts + boot-cron.ts's `delete`-// schedule (M6-001).+// schedule (M6-001); `windDown` joined it at M9-003, the wave that deletes+// apps/worker (ADR-005's acceptance note gives it to this module).//// `delete` is this module's fragment of the ONE `cron` queue's jobs:// declared here, spread into jobs.registry.ts and into `CRON_SCHEDULES` by@@ -9,6 +10,10 @@import { z } from 'zod';import { defineJob } from '../../jobs/define';import type { SchedulerDefinition } from '../../jobs/schedulers';+import { loadWindDownDeps, runWindDownCron } from './src/wind-down';++/** Hourly — V1's cadence (apps/worker/src/boot-cron.ts). */+const WIND_DOWN_CRON = '0 * * * *';/** This module's fragment of the `cron` queue's jobs. */export const organizationCronJobs = {@@ -19,9 +24,17 @@ export const organizationCronJobs = {ctx.logger.info({ ...result }, 'Delete cron complete');},}),+ windDown: defineJob({+ payload: z.null(),+ handler: async ({ ctx }) => {+ const logger = ctx.logger.child({ job: 'wind-down' });+ await runWindDownCron(await loadWindDownDeps(logger));+ },+ }),};/** This module's fragment of `CRON_SCHEDULES` — id and cadence unchanged. */export const organizationCronSchedules: readonly SchedulerDefinition[] = [{ id: 'delete', schedule: { pattern: '0 * * * *' } },+ { id: 'windDown', schedule: { pattern: WIND_DOWN_CRON } },];diff --git a/packages/core/src/modules/organization/src/win-back-highlight.test.ts b/packages/core/src/modules/organization/src/win-back-highlight.test.tsnew file mode 100644index 000000000..ed7dd4da1--- /dev/null+++ b/packages/core/src/modules/organization/src/win-back-highlight.test.ts@@ -0,0 +1,151 @@+// Moved from apps/worker/src/jobs/lib/win-back-highlight.test.ts (M9-003).+// Asserts the gating and fallback decisions, which are the point of the+// module: a highlight is a bonus, never a reason an email fails or embarrasses+// us with tiny numbers. The stats lookups and the AI call are injected stubs —+// no `mock.module`, so every assertion is on a call the code under test made.++import { beforeEach, describe, expect, it, mock } from 'bun:test';+import type { Logger } from '../../../logger';+import {+ buildWinBackHighlight,+ type HighlightOverview,+ type HighlightTopPage,+ type WinBackHighlightDeps,+} from './win-back-highlight';++const project = { id: 'project-1', name: 'acme-web' };++function stubLogger(): Logger {+ const noop = () => undefined;+ const logger: Logger = {+ fatal: noop,+ error: noop,+ warn: noop,+ info: noop,+ debug: noop,+ trace: noop,+ child: () => logger,+ };+ return logger;+}++const getAnalyticsOverview = mock(+ async (): Promise<HighlightOverview> => ({ summary: {}, series: [] })+);+const getTopPages = mock(async (): Promise<HighlightTopPage[]> => []);+const generatePitch = mock(async () => '');++function deps(): WinBackHighlightDeps {+ return {+ logger: stubLogger(),+ getAnalyticsOverview,+ getTopPages,+ generatePitch,+ };+}++beforeEach(() => {+ getAnalyticsOverview.mockClear().mockResolvedValue({+ summary: { unique_visitors: 12_400 },+ series: [+ { date: '2026-08-01', unique_visitors: 300 },+ { date: '2026-08-12', unique_visitors: 840 },+ { date: '2026-08-20', unique_visitors: 500 },+ ],+ });+ getTopPages+ .mockClear()+ .mockResolvedValue([{ path: '/pricing', sessions: 2100 }]);+ generatePitch+ .mockClear()+ .mockResolvedValue('acme-web had a strong month with 12,400 visitors.');+});++describe('buildWinBackHighlight', () => {+ it('returns the AI pitch built from the collected facts', async () => {+ const highlight = await buildWinBackHighlight(+ { project, recentEventsCount: 50_000 },+ deps()+ );++ expect(highlight).toBe('acme-web had a strong month with 12,400 visitors.');+ expect(generatePitch).toHaveBeenCalledWith(+ expect.objectContaining({+ projectName: 'acme-web',+ uniqueVisitors: 12_400,+ busiestDay: { date: 'August 12', visitors: 840 },+ topPage: { path: '/pricing', sessions: 2100 },+ })+ );+ });++ it('skips low-volume orgs entirely — tiny facts undercut the email', async () => {+ const highlight = await buildWinBackHighlight(+ { project, recentEventsCount: 999 },+ deps()+ );++ expect(highlight).toBeUndefined();+ expect(getAnalyticsOverview).not.toHaveBeenCalled();+ expect(generatePitch).not.toHaveBeenCalled();+ });++ it('skips when there is no project to pull facts from', async () => {+ const highlight = await buildWinBackHighlight(+ { project: null, recentEventsCount: 50_000 },+ deps()+ );++ expect(highlight).toBeUndefined();+ });++ it('falls back to a deterministic sentence when the AI call fails', async () => {+ generatePitch.mockRejectedValue(new Error('rate limited'));++ const highlight = await buildWinBackHighlight(+ { project, recentEventsCount: 50_000 },+ deps()+ );++ expect(highlight).toContain('acme-web had 12,400 visitors');+ expect(highlight).toContain('August 12');+ expect(highlight).toContain('/pricing');+ });++ it('falls back when the AI returns an empty pitch', async () => {+ generatePitch.mockResolvedValue(' ');++ const highlight = await buildWinBackHighlight(+ { project, recentEventsCount: 50_000 },+ deps()+ );++ expect(highlight).toContain('acme-web had 12,400 visitors');+ });++ it('returns nothing when the stats queries fail — the email still goes out', async () => {+ getAnalyticsOverview.mockRejectedValue(new Error('ch down'));++ const highlight = await buildWinBackHighlight(+ { project, recentEventsCount: 50_000 },+ deps()+ );++ expect(highlight).toBeUndefined();+ expect(generatePitch).not.toHaveBeenCalled();+ });++ it('returns nothing for a window with zero visitors', async () => {+ getAnalyticsOverview.mockResolvedValue({+ summary: { unique_visitors: 0 },+ series: [],+ });++ const highlight = await buildWinBackHighlight(+ { project, recentEventsCount: 50_000 },+ deps()+ );++ expect(highlight).toBeUndefined();+ });+});diff --git a/apps/worker/src/jobs/lib/win-back-highlight.ts b/packages/core/src/modules/organization/src/win-back-highlight.tssimilarity index 56%rename from apps/worker/src/jobs/lib/win-back-highlight.tsrename to packages/core/src/modules/organization/src/win-back-highlight.tsindex 464eed3b2..d62486887 100644--- a/apps/worker/src/jobs/lib/win-back-highlight.ts+++ b/packages/core/src/modules/organization/src/win-back-highlight.ts@@ -1,7 +1,19 @@-import { generateWinBackPitch } from '@openpanel/core';-import { getAnalyticsOverviewCore, getTopPagesCore } from '@openpanel/db';+// Ported from apps/worker/src/jobs/lib/win-back-highlight.ts (M9-003, the+// wave that deletes apps/worker). Behaviour is V1's, verbatim; only the+// wiring changed — the two stats lookups and the AI call arrive as injected+// deps (`WinBackHighlightDeps`) instead of static imports, the same idiom the+// session lifecycle uses (modules/session/src/runtime.ts): the job handler+// binds them to the real core services through `loadWinBackHighlightDeps`,+// and tests hand in stubs, so no `mock.module` is needed anywhere.+//+// Loading is lazy for the reason organization.service.ts's header gives:+// organization.jobs.ts is pulled into the eager `jobs.registry.ts` barrel, and+// a static import of overview.service.ts from here would drag the whole+// analytics read path — and @openpanel/db's clients — into every core test+// file's import graph.+import { format, subDays } from 'date-fns';-import { logger } from '../../utils/logger';+import type { Logger } from '../../../logger';/*** The personalized paragraph in the wind-down emails: a couple of concrete@@ -40,24 +52,89 @@ interface HighlightFacts {topPage?: { path: string; sessions: number };}+/** The `getAnalyticsOverviewCore` slice this reads — nothing more. */+export interface HighlightOverview {+ summary: { unique_visitors?: number | null };+ series: { date: string; unique_visitors: number }[];+}++/** The `getTopPagesCore` slice this reads — nothing more. */+export interface HighlightTopPage {+ path: string;+ sessions: number;+}++export interface WinBackPitchFacts {+ projectName: string;+ window: string;+ eventsCount: number;+ uniqueVisitors: number;+ busiestDay?: { date: string; visitors: number };+ topPage?: { path: string; sessions: number };+}++export interface WinBackHighlightDeps {+ logger: Logger;+ getAnalyticsOverview(input: {+ projectId: string;+ startDate: string;+ endDate: string;+ interval: 'day';+ }): Promise<HighlightOverview>;+ getTopPages(input: {+ projectId: string;+ startDate: string;+ endDate: string;+ limit: number;+ }): Promise<HighlightTopPage[]>;+ generatePitch(facts: WinBackPitchFacts): Promise<string>;+}++export async function loadWinBackHighlightDeps(+ logger: Logger+): Promise<WinBackHighlightDeps> {+ const [+ { getAnalyticsOverviewCore },+ { getTopPagesCore },+ { generateWinBackPitch },+ ] = await Promise.all([+ import('../../overview/overview.service'),+ import('../../overview/pages.service'),+ import('../../../clients/ai/win-back'),+ ]);++ return {+ logger,+ getAnalyticsOverview: (input) => getAnalyticsOverviewCore(input),+ getTopPages: (input) => getTopPagesCore(input),+ generatePitch: (facts) => generateWinBackPitch(facts),+ };+}+const formatCount = (n: number) => new Intl.NumberFormat('en-US').format(n);async function collectFacts(project: HighlightProject,- recentEventsCount: number+ recentEventsCount: number,+ deps: WinBackHighlightDeps): Promise<HighlightFacts | null> {const now = new Date();const startDate = subDays(now, HIGHLIGHT_WINDOW_DAYS).toISOString();const endDate = now.toISOString();const [overview, topPages] = await Promise.all([- getAnalyticsOverviewCore({+ deps.getAnalyticsOverview({projectId: project.id,startDate,endDate,interval: 'day',}),- getTopPagesCore({ projectId: project.id, startDate, endDate, limit: 1 }),+ deps.getTopPages({+ projectId: project.id,+ startDate,+ endDate,+ limit: 1,+ }),]);const uniqueVisitors = overview.summary.unique_visitors ?? 0;@@ -105,22 +182,25 @@ function deterministicPitch(facts: HighlightFacts): string {return `${parts.join(' ')}.`;}-export async function buildWinBackHighlight({- project,- recentEventsCount,-}: {- project: HighlightProject | null;- recentEventsCount: number;-}): Promise<string | undefined> {+export async function buildWinBackHighlight(+ {+ project,+ recentEventsCount,+ }: {+ project: HighlightProject | null;+ recentEventsCount: number;+ },+ deps: WinBackHighlightDeps+): Promise<string | undefined> {if (!project || recentEventsCount < HIGHLIGHT_MIN_RECENT_EVENTS) {return undefined;}let facts: HighlightFacts | null;try {- facts = await collectFacts(project, recentEventsCount);+ facts = await collectFacts(project, recentEventsCount, deps);} catch (error) {- logger.warn(+ deps.logger.warn({ err: error, projectId: project.id },'Win-back highlight stats failed, sending without highlight');@@ -132,7 +212,7 @@ export async function buildWinBackHighlight({}try {- const pitch = await generateWinBackPitch({+ const pitch = await deps.generatePitch({projectName: facts.projectName,window: 'the last 30 days',eventsCount: facts.eventsCount,@@ -144,7 +224,7 @@ export async function buildWinBackHighlight({return pitch.trim();}} catch (error) {- logger.warn(+ deps.logger.warn({ err: error, projectId: project.id },'Win-back pitch AI call failed, using deterministic fallback');diff --git a/packages/core/src/modules/organization/src/wind-down.test.ts b/packages/core/src/modules/organization/src/wind-down.test.tsnew file mode 100644index 000000000..ea187e670--- /dev/null+++ b/packages/core/src/modules/organization/src/wind-down.test.ts@@ -0,0 +1,356 @@+// Moved from apps/worker/src/jobs/cron.wind-down.test.ts (M9-003).+//+// Prisma, the event counts, the last-event lookup, the highlight builder and+// the email transport are injected stubs — these assert the job's decisions+// (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 { subDays } from 'date-fns';+import type { Logger } from '../../../logger';+import {+ runWindDownCron,+ type WindDownDeps,+ type WindDownOrganization,+} from './wind-down';++function stubLogger(): Logger {+ const noop = () => undefined;+ const logger: Logger = {+ fatal: noop,+ error: noop,+ warn: noop,+ info: noop,+ debug: noop,+ trace: noop,+ child: () => logger,+ };+ return logger;+}++const organizationFindMany = mock(+ async (): Promise<WindDownOrganization[]> => []+);+const organizationUpdate = mock(async (_args: unknown) => ({}));+const organizationUpdateMany = mock(async (_args: unknown) => ({ count: 0 }));+const getLastEventPerProject = mock(async () => new Map<string, Date>());+const getOrganizationEventsCount = mock(async () => 1000);+const getOrganizationEventsCountSince = mock(async () => 250);+const buildHighlight = mock(async (): Promise<string | undefined> => undefined);+const sendEmail = mock(+ async (_template: string, _options: unknown): Promise<unknown> => ({})+);++function deps(): WindDownDeps {+ return {+ db: {+ organization: {+ findMany: organizationFindMany,+ update: organizationUpdate,+ updateMany: organizationUpdateMany,+ },+ },+ logger: stubLogger(),+ sendEmail: sendEmail as WindDownDeps['sendEmail'],+ getLastEventPerProject,+ getOrganizationEventsCount,+ getOrganizationEventsCountSince,+ buildHighlight,+ };+}++function makeOrg(+ overrides: Partial<WindDownOrganization> & Record<string, unknown> = {}+): WindDownOrganization {+ return {+ id: 'org-1',+ subscriptionState: 'trial_expired',+ subscriptionStatus: 'trialing',+ subscriptionEndsAt: subDays(new Date(), 800),+ subscriptionId: null,+ windDownStartedAt: null,+ windDownStep: null,+ deleteAt: null,+ createdBy: {+ id: 'user-1',+ email: '[email protected]',+ firstName: 'Alex',+ deletedAt: null,+ },+ projects: [{ id: 'project-1', name: 'acme-web' }],+ ...overrides,+ } 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,+};++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 });+ sendEmail.mockClear().mockResolvedValue({});+ getOrganizationEventsCount.mockClear().mockResolvedValue(1000);+ getOrganizationEventsCountSince.mockClear().mockResolvedValue(250);+ // Default: nothing has sent recently.+ getLastEventPerProject.mockClear().mockResolvedValue(new Map());+ 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(organizationFindMany).not.toHaveBeenCalled();+ });++ it('starts a long-expired org at step 0 rather than at deletion', async () => {+ // The anchor decision, asserted directly: this org's trial ended 800 days+ // ago. Measuring the schedule from subscriptionEndsAt would put it past+ // every step and delete it without warning.+ organizationFindMany.mockResolvedValue([makeOrg()]);++ const result = await runWindDownCron(deps());++ expect(result).toMatchObject({ entering: 1, emailsSent: 1 });+ expect(sendEmail).toHaveBeenCalledWith('wind-down-expired', {+ to: '[email protected]',+ data: expect.objectContaining({ firstName: 'Alex' }),+ });+ expect(organizationUpdateMany).toHaveBeenCalledWith({+ where: { id: { in: ['org-1'] } },+ data: { windDownStartedAt: expect.any(Date) },+ });+ expect(organizationUpdate).toHaveBeenCalledWith({+ where: { id: 'org-1' },+ data: { windDownStep: 'expired_notice' },+ });+ // Nothing scheduled for deletion on the way in.+ expect(organizationUpdate).not.toHaveBeenCalledWith(+ expect.objectContaining({+ data: expect.objectContaining({ deleteAt: expect.anything() }),+ })+ );+ });++ 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' }),+ makeOrg({ id: 'org-3' }),+ makeOrg({ id: 'org-4' }),+ ]);++ const result = await runWindDownCron(deps());++ expect(result).toMatchObject({ entering: 2 });+ expect(organizationUpdateMany).toHaveBeenCalledWith({+ where: { id: { in: ['org-1', 'org-2'] } },+ data: { windDownStartedAt: expect.any(Date) },+ });+ });++ it('blocks ingestion by advancing the pointer on day 21', async () => {+ organizationFindMany.mockResolvedValue([+ makeOrg({+ windDownStartedAt: subDays(new Date(), 21),+ windDownStep: 'stopping_soon',+ }),+ ]);++ await runWindDownCron(deps());++ expect(sendEmail).toHaveBeenCalledWith(+ 'wind-down-blocked',+ expect.anything()+ );+ expect(organizationUpdate).toHaveBeenCalledWith({+ where: { id: 'org-1' },+ data: { windDownStep: 'blocked' },+ });+ });++ it('arms deletion once the final warning is delivered', async () => {+ organizationFindMany.mockResolvedValue([+ makeOrg({+ windDownStartedAt: subDays(new Date(), 44),+ windDownStep: 'blocked',+ }),+ ]);++ await runWindDownCron(deps());++ expect(sendEmail).toHaveBeenCalledWith(+ 'wind-down-final-warning',+ expect.anything()+ );+ expect(organizationUpdate).toHaveBeenCalledWith({+ where: { id: 'org-1' },+ data: { deleteAt: expect.any(Date) },+ });+ expect(organizationUpdate).toHaveBeenCalledWith({+ where: { id: 'org-1' },+ data: { windDownStep: 'final_warning' },+ });+ });++ it('does not arm deletion when the final warning was not delivered', async () => {+ sendEmail.mockResolvedValue(null);+ organizationFindMany.mockResolvedValue([+ makeOrg({+ windDownStartedAt: subDays(new Date(), 44),+ windDownStep: 'blocked',+ }),+ ]);++ const result = await runWindDownCron(deps());++ expect(result).toMatchObject({ emailsSent: 0, failed: 1 });+ expect(organizationUpdate).not.toHaveBeenCalled();+ });++ it('releases an organization that subscribed mid-sequence', async () => {+ organizationFindMany.mockResolvedValue([+ makeOrg({+ subscriptionState: 'active',+ subscriptionStatus: 'active',+ subscriptionId: 'sub-123',+ windDownStartedAt: subDays(new Date(), 30),+ windDownStep: 'blocked',+ deleteAt: new Date(),+ }),+ ]);++ const result = await runWindDownCron(deps());++ expect(result).toMatchObject({ recovered: 1, emailsSent: 0 });+ expect(organizationUpdateMany).toHaveBeenCalledWith({+ where: { id: { in: ['org-1'] } },+ data: { windDownStartedAt: null, windDownStep: null, deleteAt: null },+ });+ expect(sendEmail).not.toHaveBeenCalled();+ });++ it('skips organizations with no one to warn', async () => {+ organizationFindMany.mockResolvedValue([+ makeOrg({ createdBy: null }),+ makeOrg({+ id: 'org-2',+ createdBy: {+ id: 'user-2',+ email: '[email protected]',+ firstName: null,+ deletedAt: new Date(),+ },+ }),+ ]);++ const result = await runWindDownCron(deps());++ expect(result).toMatchObject({ entering: 0, emailsSent: 0 });+ expect(sendEmail).not.toHaveBeenCalled();+ });++ describe('still-tracking organizations', () => {+ // 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)]])+ );+ // The dormant org is returned first, so only ordering can save the+ // active one from being crowded out by the long tail.+ organizationFindMany.mockResolvedValue([+ makeOrg({+ id: 'org-dormant',+ projects: [{ id: 'project-dormant', name: 'dormant-web' }],+ }),+ makeOrg({+ id: 'org-active',+ projects: [{ id: 'project-active', name: 'active-web' }],+ }),+ ]);++ const result = await runWindDownCron(deps());++ expect(result).toMatchObject({ entering: 1, stillTracking: 1 });+ expect(organizationUpdateMany).toHaveBeenCalledWith({+ where: { id: { in: ['org-active'] } },+ data: { windDownStartedAt: expect.any(Date) },+ });+ });++ it('sends current volume, project names and the data highlight', async () => {+ getLastEventPerProject.mockResolvedValue(+ new Map([['project-1', subDays(new Date(), 1)]])+ );+ getOrganizationEventsCount.mockResolvedValue(842_110);+ getOrganizationEventsCountSince.mockResolvedValue(128_400);+ buildHighlight.mockResolvedValue('acme-web had a strong month.');+ organizationFindMany.mockResolvedValue([makeOrg()]);++ await runWindDownCron(deps());++ expect(sendEmail).toHaveBeenCalledWith('wind-down-expired', {+ to: '[email protected]',+ data: expect.objectContaining({+ stillTracking: true,+ eventsCount: 842_110,+ recentEventsCount: 128_400,+ projectNames: ['acme-web'],+ highlight: 'acme-web had a strong month.',+ }),+ });+ expect(buildHighlight).toHaveBeenCalledWith({+ project: { id: 'project-1', name: 'acme-web' },+ recentEventsCount: 128_400,+ });+ });++ it('asks for no highlight when the org is not still tracking', async () => {+ organizationFindMany.mockResolvedValue([makeOrg()]);++ await runWindDownCron(deps());++ expect(buildHighlight).not.toHaveBeenCalled();+ expect(sendEmail).toHaveBeenCalledWith('wind-down-expired', {+ to: '[email protected]',+ data: expect.objectContaining({ highlight: undefined }),+ });+ });++ it('does not call an org still tracking when its last event is old', async () => {+ getLastEventPerProject.mockResolvedValue(+ new Map([['project-1', subDays(new Date(), 200)]])+ );+ organizationFindMany.mockResolvedValue([makeOrg()]);++ const result = await runWindDownCron(deps());++ expect(result).toMatchObject({ stillTracking: 0 });+ expect(sendEmail).toHaveBeenCalledWith('wind-down-expired', {+ to: '[email protected]',+ data: expect.objectContaining({ stillTracking: false }),+ });+ });+ });+});diff --git a/apps/worker/src/jobs/cron.wind-down.ts b/packages/core/src/modules/organization/src/wind-down.tssimilarity index 58%rename from apps/worker/src/jobs/cron.wind-down.tsrename to packages/core/src/modules/organization/src/wind-down.tsindex 3e403b704..b788f9622 100644--- a/apps/worker/src/jobs/cron.wind-down.ts+++ b/packages/core/src/modules/organization/src/wind-down.ts@@ -1,31 +1,32 @@-import {- db,- getLastEventPerProject,- getOrganizationEventsCount,- getOrganizationEventsCountSince,-} from '@openpanel/db';+// Ported from apps/worker/src/jobs/cron.wind-down.ts (M9-003, the wave that+// deletes apps/worker). ADR-005's acceptance note gives `windDown` to the+// organization module. Behaviour is V1's, verbatim; only the wiring changed —+// Prisma, the two ClickHouse counts, the last-event lookup, the plan pricer,+// the highlight builder and `sendEmail` all arrive as injected deps+// (`WindDownDeps`), the same idiom the session lifecycle uses+// (modules/session/src/runtime.ts). The job handler binds them to the real+// implementations through `loadWindDownDeps`; tests hand in stubs, so no+// `mock.module` is needed.+import { getRecommendedPlan } from '@openpanel/payments';-import { addDays, differenceInDays, format, subDays } from 'date-fns';+import { addDays, format, subDays } from 'date-fns';+import type { Logger } from '../../../logger';import {+ runSequence,+ type SendSequenceEmail,type SequenceStep,type SequenceSubject,- runSequence,step,-} from './lib/email-sequence';-import {- type HighlightProject,- buildWinBackHighlight,-} from './lib/win-back-highlight';-import { logger } from '../utils/logger';+} from '../../../shared/email-sequence';+import type { HighlightProject } from './win-back-highlight';/*** Wind-down: what happens to a trial that expired without a purchase.** Four emails over 44 days, then deletion. Ingestion keeps flowing until day* 21 so the first two emails have something to be about; from day 21 the- * ingestion hook rejects events (see apps/api/src/hooks/subscription.hook.ts,- * which reads `windDownStep`). Day 44 arms `deleteAt`, and the existing delete- * cron sweeps it seven days later.+ * ingestion hook rejects events (it reads `windDownStep`). Day 44 arms+ * `deleteAt`, and the existing delete cron sweeps it seven days later.** The schedule is measured from `windDownStartedAt`, stamped when an org* enters. That is the whole reason the column exists: `subscriptionEndsAt` for@@ -56,18 +57,30 @@ function getMaxPerRun(): number {return Number.isNaN(parsed) || parsed <= 0 ? DEFAULT_MAX_PER_RUN : parsed;}-const orgQuery = {- include: {- createdBy: {- select: { id: true, email: true, firstName: true, deletedAt: true },- },- projects: { select: { id: true, name: true } },- },-} as const;+/** The organization row shape this job reads — V1's `orgQuery` include. */+export interface WindDownOrganization {+ id: string;+ subscriptionState: string;+ subscriptionEndsAt: Date | null;+ windDownStartedAt: Date | null;+ windDownStep: string | null;+ createdBy: {+ id: string;+ email: string;+ firstName: string | null;+ deletedAt: Date | null;+ } | null;+ projects: { id: string; name: string }[];+}-type OrgWithCreator = Awaited<- ReturnType<typeof db.organization.findMany<typeof orgQuery>>->[number];+/** The Prisma surface this job touches — nothing wider. */+export interface WindDownDb {+ organization: {+ findMany(args: unknown): Promise<WindDownOrganization[]>;+ update(args: unknown): Promise<unknown>;+ updateMany(args: unknown): Promise<unknown>;+ };+}interface WindDownUsage {/** Everything ever collected — what deletion would destroy. */@@ -77,9 +90,25 @@ interface WindDownUsage {hasData: boolean;}+export interface WindDownDeps {+ db: WindDownDb;+ logger: Logger;+ sendEmail: SendSequenceEmail;+ getLastEventPerProject(): Promise<Map<string, Date>>;+ getOrganizationEventsCount(projectIds: string[]): Promise<number>;+ getOrganizationEventsCountSince(+ projectIds: string[],+ since: Date+ ): Promise<number>;+ buildHighlight(input: {+ project: HighlightProject | null;+ recentEventsCount: number;+ }): Promise<string | undefined>;+}+interface WindDownContext {- org: OrgWithCreator;- user: NonNullable<OrgWithCreator['createdBy']>;+ org: WindDownOrganization;+ user: NonNullable<WindDownOrganization['createdBy']>;/** Day 0 of this org's schedule. */startedAt: Date;lastEventAt: Date | null;@@ -91,40 +120,71 @@ interface WindDownContext {getHighlight: () => Promise<string | undefined>;}+export async function loadWindDownDeps(logger: Logger): Promise<WindDownDeps> {+ const [+ { db },+ { getOrganizationEventsCount, getOrganizationEventsCountSince },+ { getLastEventPerProject },+ { sendEmail },+ { loadWinBackHighlightDeps, buildWinBackHighlight },+ ] = await Promise.all([+ import('@openpanel/db/src/prisma-client'),+ import('../organization.service'),+ import('../../project/project.service'),+ import('../../../clients/email'),+ import('./win-back-highlight'),+ ]);++ const highlightDeps = await loadWinBackHighlightDeps(logger);++ return {+ db: db as unknown as WindDownDb,+ logger,+ sendEmail,+ getLastEventPerProject,+ getOrganizationEventsCount,+ getOrganizationEventsCountSince,+ buildHighlight: (input) => buildWinBackHighlight(input, highlightDeps),+ };+}+// Lazy + memoized like getUsage: only still-tracking orgs that clear a day// gate pay for the stats queries and the AI call, and each org pays once per// tick even though two steps include the highlight.-function createHighlightGetter(ctx: {- stillTracking: boolean;- highlightProject: HighlightProject | null;- getUsage: () => Promise<WindDownUsage>;-}) {+function createHighlightGetter(+ ctx: {+ stillTracking: boolean;+ highlightProject: HighlightProject | null;+ getUsage: () => Promise<WindDownUsage>;+ },+ deps: WindDownDeps+) {let promise: Promise<string | undefined> | null = null;return () => {if (!ctx.stillTracking) {return Promise.resolve(undefined);}promise ??= ctx.getUsage().then(({ recentEventsCount }) =>- buildWinBackHighlight({+ deps.buildHighlight({project: ctx.highlightProject,recentEventsCount,- }),+ }));return promise;};}-function createUsageGetter(org: OrgWithCreator) {+function createUsageGetter(org: WindDownOrganization, deps: WindDownDeps) {let promise: Promise<WindDownUsage> | null = null;return () => {const projectIds = org.projects.map((project) => project.id);// Only orgs that clear a day gate pay for these two ClickHouse counts, and// each org pays at most once per tick.promise ??= Promise.all([- getOrganizationEventsCount(projectIds),- getOrganizationEventsCountSince(+ deps.getOrganizationEventsCount(projectIds),+ deps.getOrganizationEventsCountSince(projectIds,- subDays(new Date(), RECENT_VOLUME_DAYS),+ subDays(new Date(), RECENT_VOLUME_DAYS)),]).then(([eventsCount, recentEventsCount]) => ({eventsCount,@@ -144,7 +204,7 @@ const getters = {deleteDate: (ctx: WindDownContext) =>format(addDays(ctx.startedAt, FINAL_WARNING_DAY + DELETE_GRACE_DAYS),- 'MMMM d',+ 'MMMM d'),trialEndedDate: (ctx: WindDownContext) =>ctx.org.subscriptionEndsAt@@ -157,7 +217,7 @@ const getters = {return getRecommendedPlan(recentEventsCount || eventsCount,(plan) =>- `${plan.formattedEvents} events per month for ${plan.formattedPrice}`,+ `${plan.formattedEvents} events per month for ${plan.formattedPrice}`);},} as const;@@ -175,64 +235,66 @@ async function baseData(ctx: WindDownContext) {};}-export const WIND_DOWN_STEPS: SequenceStep<WindDownContext>[] = [- step<WindDownContext, 'wind-down-expired'>({- day: 0,- step: 'expired_notice',- template: 'wind-down-expired',- data: async (ctx) => ({- ...(await baseData(ctx)),- blockDate: getters.blockDate(ctx),- trialEndedDate: getters.trialEndedDate(ctx),- recommendedPlan: await getters.recommendedPlan(ctx),- highlight: await ctx.getHighlight(),+function windDownSteps(deps: WindDownDeps): SequenceStep<WindDownContext>[] {+ return [+ step<WindDownContext, 'wind-down-expired'>({+ day: 0,+ step: 'expired_notice',+ template: 'wind-down-expired',+ data: async (ctx) => ({+ ...(await baseData(ctx)),+ blockDate: getters.blockDate(ctx),+ trialEndedDate: getters.trialEndedDate(ctx),+ recommendedPlan: await getters.recommendedPlan(ctx),+ highlight: await ctx.getHighlight(),+ }),}),- }),- step<WindDownContext, 'wind-down-stopping-soon'>({- day: 14,- step: 'stopping_soon',- template: 'wind-down-stopping-soon',- data: async (ctx) => ({- ...(await baseData(ctx)),- blockDate: getters.blockDate(ctx),- recommendedPlan: await getters.recommendedPlan(ctx),- highlight: await ctx.getHighlight(),+ step<WindDownContext, 'wind-down-stopping-soon'>({+ day: 14,+ step: 'stopping_soon',+ template: 'wind-down-stopping-soon',+ data: async (ctx) => ({+ ...(await baseData(ctx)),+ blockDate: getters.blockDate(ctx),+ recommendedPlan: await getters.recommendedPlan(ctx),+ highlight: await ctx.getHighlight(),+ }),}),- }),- step<WindDownContext, 'wind-down-blocked'>({- day: BLOCK_DAY,- step: 'blocked',- template: 'wind-down-blocked',- // No onSent: advancing the pointer to 'blocked' *is* the block, because- // the ingestion hook reads the pointer.- data: async (ctx) => ({- ...(await baseData(ctx)),- deleteDate: getters.deleteDate(ctx),+ step<WindDownContext, 'wind-down-blocked'>({+ day: BLOCK_DAY,+ step: 'blocked',+ template: 'wind-down-blocked',+ // No onSent: advancing the pointer to 'blocked' *is* the block, because+ // the ingestion hook reads the pointer.+ data: async (ctx) => ({+ ...(await baseData(ctx)),+ deleteDate: getters.deleteDate(ctx),+ }),}),- }),- step<WindDownContext, 'wind-down-final-warning'>({- day: FINAL_WARNING_DAY,- step: 'final_warning',- template: 'wind-down-final-warning',- // Never arm deletion off an email that didn't go out.- requireDelivery: true,- onSent: async (ctx) => {- await db.organization.update({- where: { id: ctx.org.id },- data: { deleteAt: addDays(new Date(), DELETE_GRACE_DAYS) },- });- },- data: async (ctx) => ({- ...(await baseData(ctx)),- deleteDate: format(addDays(new Date(), DELETE_GRACE_DAYS), 'MMMM d'),+ step<WindDownContext, 'wind-down-final-warning'>({+ day: FINAL_WARNING_DAY,+ step: 'final_warning',+ template: 'wind-down-final-warning',+ // Never arm deletion off an email that didn't go out.+ requireDelivery: true,+ onSent: async (ctx) => {+ await deps.db.organization.update({+ where: { id: ctx.org.id },+ data: { deleteAt: addDays(new Date(), DELETE_GRACE_DAYS) },+ });+ },+ data: async (ctx) => ({+ ...(await baseData(ctx)),+ deleteDate: format(addDays(new Date(), DELETE_GRACE_DAYS), 'MMMM d'),+ }),}),- }),-];+ ];+}/** Newest event across all of an organization's projects. */function lastEventFor(- org: OrgWithCreator,- lastEventPerProject: Map<string, Date>,+ org: WindDownOrganization,+ lastEventPerProject: Map<string, Date>): Date | null {let latest: Date | null = null;for (const project of org.projects) {@@ -244,7 +306,21 @@ function lastEventFor(return latest;}-export async function windDownCronJob() {+export interface WindDownResult {+ expired: number;+ recovered: number;+ entering: number;+ stillTracking: number;+ emailsSent: number;+ completed: number;+ deferred: number;+ stepsSkipped: number;+ failed: number;+}++export async function runWindDownCron(+ deps: WindDownDeps+): Promise<WindDownResult | null> {if (process.env.SELF_HOSTED === 'true') {return null;}@@ -257,7 +333,7 @@ export async function windDownCronJob() {// clearing. Note there is deliberately no filter on event volume: an org that// never sent anything and one still sending millions are both in scope, they// just get different copy and different priority.- const candidates = await db.organization.findMany({+ const candidates = await deps.db.organization.findMany({where: {OR: [{ windDownStartedAt: { not: null } },@@ -275,11 +351,16 @@ export async function windDownCronJob() {},],},- ...orgQuery,+ include: {+ createdBy: {+ select: { id: true, email: true, firstName: true, deletedAt: true },+ },+ projects: { select: { id: true, name: true } },+ },});const expired = candidates.filter(- (org) => org.subscriptionState === 'trial_expired',+ (org) => org.subscriptionState === 'trial_expired');// Recovered: subscribed (or otherwise left trial_expired) while in the@@ -288,11 +369,11 @@ export async function windDownCronJob() {const recovered = candidates.filter((org) =>org.windDownStartedAt !== null &&- org.subscriptionState !== 'trial_expired',+ org.subscriptionState !== 'trial_expired');if (recovered.length > 0) {- await db.organization.updateMany({+ await deps.db.organization.updateMany({where: { id: { in: recovered.map((org) => org.id) } },data: { windDownStartedAt: null, windDownStep: null, deleteAt: null },});@@ -301,16 +382,19 @@ export async function windDownCronJob() {// Without a creator we have nobody to warn, so the sequence can't run. Such// orgs are left alone; the delete cron already sweeps ones with no admin.const contactable = expired.filter(- (org) => org.createdBy && !org.createdBy.deletedAt,+ (org) => org.createdBy && !org.createdBy.deletedAt);// One cheap aggregate for the whole instance, same source the data-health// job uses. It answers "are the SDKs still live", which decides both the// copy and who gets in first.- const lastEventPerProject = await getLastEventPerProject();+ const lastEventPerProject = await deps.getLastEventPerProject();const stillTrackingCutoff = subDays(now, STILL_TRACKING_WITHIN_DAYS);- const activity = new Map<string, { lastEventAt: Date | null; still: boolean }>(+ const activity = new Map<+ string,+ { lastEventAt: Date | null; still: boolean }+ >(contactable.map((org) => {const lastEventAt = lastEventFor(org, lastEventPerProject);return [@@ -320,7 +404,7 @@ export async function windDownCronJob() {still: lastEventAt !== null && lastEventAt > stillTrackingCutoff,},];- }),+ }));// Orgs still sending go first: they are the ones there is anything to@@ -342,7 +426,7 @@ export async function windDownCronJob() {.slice(0, getMaxPerRun());if (entering.length > 0) {- await db.organization.updateMany({+ await deps.db.organization.updateMany({where: { id: { in: entering.map((org) => org.id) } },data: { windDownStartedAt: now },});@@ -350,11 +434,13 @@ export async function windDownCronJob() {const enteringIds = new Set(entering.map((org) => org.id));const active = contactable.filter(- (org) => org.windDownStartedAt !== null || enteringIds.has(org.id),+ (org) => org.windDownStartedAt !== null || enteringIds.has(org.id));const subjects: SequenceSubject<WindDownContext>[] = active.map((org) => {- const user = org.createdBy as NonNullable<OrgWithCreator['createdBy']>;+ const user = org.createdBy as NonNullable<+ WindDownOrganization['createdBy']+ >;const startedAt = org.windDownStartedAt ?? now;const seen = activity.get(org.id);@@ -369,7 +455,7 @@ export async function windDownCronJob() {}}- const getUsage = createUsageGetter(org);+ const getUsage = createUsageGetter(org, deps);const stillTracking = seen?.still ?? false;return {id: org.id,@@ -384,22 +470,22 @@ export async function windDownCronJob() {stillTracking,highlightProject,getUsage,- getHighlight: createHighlightGetter({- stillTracking,- highlightProject,- getUsage,- }),+ getHighlight: createHighlightGetter(+ { stillTracking, highlightProject, getUsage },+ deps+ ),},};});const result = await runSequence({name: 'wind-down',- steps: WIND_DOWN_STEPS,+ steps: windDownSteps(deps),subjects,- logger,+ logger: deps.logger,+ send: deps.sendEmail,onAdvance: async (subject, stepName) => {- await db.organization.update({+ await deps.db.organization.update({where: { id: subject.id },data: { windDownStep: stepName },});@@ -410,35 +496,21 @@ export async function windDownCronJob() {});const stillTrackingCount = [...activity.values()].filter(- (entry) => entry.still,+ (entry) => entry.still).length;- logger.info(- {- candidates: candidates.length,- expired: expired.length,- recovered: recovered.length,- entering: entering.length,- stillTracking: stillTrackingCount,- ...result,- },- 'Wind-down cron complete',- );-- return {+ const summary: WindDownResult = {expired: expired.length,recovered: recovered.length,entering: entering.length,stillTracking: stillTrackingCount,...result,};-}-/** Days into the sequence, for logging and the debug route. */-export function windDownDay(org: {- windDownStartedAt: Date | null;-}): number | null {- return org.windDownStartedAt- ? differenceInDays(new Date(), org.windDownStartedAt)- : null;+ deps.logger.info(+ { candidates: candidates.length, ...summary },+ 'Wind-down cron complete'+ );++ return summary;}diff --git a/packages/core/src/shared/email-sequence.ts b/packages/core/src/shared/email-sequence.tsindex 2f0665e5e..6bc6dceb7 100644--- a/packages/core/src/shared/email-sequence.ts+++ b/packages/core/src/shared/email-sequence.ts@@ -1,9 +1,8 @@// Moved from apps/worker/src/jobs/lib/email-sequence.ts (M6-003). Shared by-// the onboarding drip (modules/onboarding) and, once it moves, the wind-down-// track (apps/worker/src/jobs/cron.wind-down.ts still reaches this through-// the re-export shim apps/worker keeps). `shared/`, not a module, because two-// modules need it (ADR-007 layout: "shared/ when several backend modules-// need it").+// the onboarding drip (modules/onboarding) and the wind-down track+// (modules/organization/src/wind-down.ts, which joined it at M9-003 when+// apps/worker was deleted). `shared/`, not a module, because two modules need+// it (ADR-007 layout: "shared/ when several backend modules need it").//// A day-gated email sequence runner. Each subject carries a pointer (the// last step whose email was sent) and an anchor date. On every tick the@@ -90,6 +89,16 @@ export interface SequenceResult {failed: number;}+/**+ * The `sendEmail` seam. Injectable so a sequence's own tests can assert which+ * template went to whom, and drive the `requireDelivery` branch, without+ * `mock.module`-ing `clients/email` for the whole test process.+ */+export type SendSequenceEmail = (+ template: EmailTemplate,+ options: { to: string; data: never }+) => Promise<unknown>;+export interface RunSequenceOptions<TCtx> {/** Sequence name, for log lines. */name: string;@@ -105,6 +114,8 @@ export interface RunSequenceOptions<TCtx> {*/onComplete?: (subject: SequenceSubject<TCtx>) => Promise<void>;logger: Pick<Logger, 'info' | 'warn' | 'error'>;+ /** Defaults to the real transport; see `SendSequenceEmail`. */+ send?: SendSequenceEmail;}export async function runSequence<TCtx>({@@ -114,6 +125,7 @@ export async function runSequence<TCtx>({onAdvance,onComplete,logger,+ send = sendEmail as SendSequenceEmail,}: RunSequenceOptions<TCtx>): Promise<SequenceResult> {const result: SequenceResult = {emailsSent: 0,@@ -181,6 +193,7 @@ export async function runSequence<TCtx>({subject,step: current,logger,+ send,});if (!sent) {@@ -221,16 +234,18 @@ async function sendStep<TCtx>({subject,step: current,logger,+ send,}: {sequence: string;subject: SequenceSubject<TCtx>;step: SequenceStep<TCtx, EmailTemplate>;logger: Pick<Logger, 'info' | 'warn' | 'error'>;+ send: SendSequenceEmail;}): Promise<boolean> {try {const data = await current.data(subject.ctx);- const delivery = await sendEmail(current.template, {+ const delivery = await send(current.template, {to: subject.email,data: data as never,});diff --git a/packages/db/index.ts b/packages/db/index.tsindex 452d6a408..b065e0251 100644--- a/packages/db/index.ts+++ b/packages/db/index.ts@@ -3,7 +3,6 @@ export * from './src/clickhouse/client';export * from './src/clickhouse/query-builder';export * from './src/encryption';export * from './src/engine';-export * from './src/exports';export * from './src/gsc';export * from './src/prisma-client';export * from './src/services/access.service';diff --git a/packages/db/src/exports/index.ts b/packages/db/src/exports/index.tsdeleted file mode 100644index 5bd8c2f92..000000000--- a/packages/db/src/exports/index.ts+++ /dev/null@@ -1,23 +0,0 @@-// Object-store export batch creation lives in @openpanel/core now (M8-005):-// packages/core/src/clients/integrations/export/. Re-exported here for the-// existing `@openpanel/db` importer (apps/worker's flush-exports cron job) —-// same shape as event.service.ts since M7-002.-export {- clickhouseEventToExportEvent,- createBatch,- createManifest,- EXPORT_SCHEMA_VERSION,- type ExportFormat,- generateBatchPath,- getContentType,- getFileExtension,- type IBatchFile,- type IBatchInfo,- type IBatchResult,- type IExportEvent,- type IManifest,- MANIFEST_CONTENT_TYPE,- MANIFEST_FILENAME,- parseManifest,- serializeManifest,-} from '@openpanel/core';diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 37b478f1d..d1ab33cbe 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -45,8 +45,6 @@ overrides:'@nuxt/vite-builder>seroval': 1.4.0embla-carousel: 8.0.0-rc22-packageExtensionsChecksum: sha256-8pKsevF6VIp4dvYbdnOYuuSpx5qM62WntMQClY5PwWE=-patchedDependencies:nuqs: 4f93812bf7a04685f9477b2935e3acbf2aa24dfbb1b00b9ae0ed8f7a2877a98e@@ -906,94 +904,6 @@ importers:specifier: ^6.0.0version: 6.3.5(@types/[email protected])([email protected])([email protected])([email protected])([email protected])([email protected])- apps/worker:- dependencies:- '@aws-sdk/client-s3':- specifier: ^3.974.0- version: 3.984.0- '@aws-sdk/client-sts':- specifier: ^3.974.0- version: 3.1117.0- '@bull-board/api':- specifier: 6.14.0- version: 6.14.0(@bull-board/[email protected]([email protected]([email protected])))([email protected]([email protected]))- '@bull-board/express':- specifier: 6.14.0- '@google-cloud/storage':- specifier: ^7.18.0- version: 7.22.0([email protected])- '@hyperdx/node-opentelemetry':- specifier: 'catalog:'- version: 0.10.3([email protected])- '@openpanel/common':- specifier: workspace:*- version: link:../../packages/common- '@openpanel/core':- specifier: workspace:*- version: link:../../packages/core- '@openpanel/db':- specifier: workspace:*- version: link:../../packages/db- '@openpanel/email':- specifier: workspace:*- version: link:../../packages/email- '@openpanel/payments':- specifier: workspace:*- version: link:../../packages/payments- '@openpanel/queue':- specifier: workspace:*- version: link:../../packages/queue- '@openpanel/redis':- specifier: workspace:*- version: link:../../packages/redis- '@openpanel/validation':- specifier: workspace:*- version: link:../../packages/validation- bullmq:- specifier: ^5.63.0- version: 5.63.0([email protected])- date-fns:- specifier: ^3.3.1- version: 3.3.1- express:- specifier: ^4.18.2- version: 4.18.2([email protected])- pino:- specifier: 'catalog:'- version: 10.3.1- pino-pretty:- specifier: 'catalog:'- version: 13.1.3- prom-client:- specifier: ^15.1.3- version: 15.1.3- ramda:- specifier: ^0.29.1- version: 0.29.1- source-map-support:- specifier: ^0.5.21- version: 0.5.21- devDependencies:- '@openpanel/tsconfig':- specifier: workspace:*- version: link:../../tooling/typescript- '@types/express':- specifier: ^4.17.21- version: 4.17.21- '@types/ramda':- specifier: ^0.29.6- version: 0.29.10- '@types/source-map-support':- specifier: ^0.5.10- version: 0.5.10- tsdown:- specifier: 0.14.2- version: 0.14.2(@emnapi/[email protected])(@emnapi/[email protected])([email protected](@emnapi/[email protected])(@emnapi/[email protected]))([email protected])([email protected])- typescript:- specifier: 'catalog:'- version: 5.9.3-packages/common:dependencies:'@openpanel/constants':@@ -3273,12 +3183,6 @@ packages:'@borewit/[email protected]':resolution: {integrity: sha512-DDaRehssg1aNrH4+2hnj1B7vnUGEjU6OIlyRdkMd0aUdIUvKXrJfXsy8LVtXAy7DRvYVluWbMspsRhz2lcW0mQ==}- '@bull-board/[email protected]':- resolution: {integrity: sha512-oMDwXwoPn0RsdZ3Y68/bOErZ/qGZE5H97vgE/Pc8Uul/OHajlvajKW4NV+ZGTix82liUfH9CkjYx7PpwvBWhxg==}- peerDependencies:- '@bull-board/ui': 6.14.0- bullmq: '*'-'@bull-board/[email protected]':resolution: {integrity: sha512-RJcXIaFDO2l76t1MNxhIiKNaDIKIm4F8vsQq32grV9Rw4DiclKFlXSht2SN/+VIVyHJ7NzlCskMd8HgMpb/aEg==}peerDependencies:@@ -3296,12 +3200,6 @@ packages:peerDependencies:elysia: ^1.1.0- '@bull-board/[email protected]':- resolution: {integrity: sha512-3H1ame2G1+eVnqqSsw6KfzTGYAWSpVsIx6EPwg9vPSP2eKfNAm12Cm4zvL6ZkwAvTCkAByt5PPDRWbbwWB6HHQ==}-- '@bull-board/[email protected]':- resolution: {integrity: sha512-5yqfS9CwWR8DBxpReIbqv/VSPFM/zT4KZ75keyApMiejasRC2joaHqEzYWlMCjkMycbNNCvlQNlTbl+C3dE/dg==}-'@bull-board/[email protected]':resolution: {integrity: sha512-rJDD+CvxI43Avq2gZpHunPT67HToYXyHSRoXsiVXDh/6MB93YtU+ofkgrrYBY25FswDSvtfnQN/ID3Ypf9OyOg==}@@ -10606,10 +10504,6 @@ packages:resolution: {integrity: sha512-DRQrD6gJyy8FbiE4s+bDoXS9hiW3Vbx5uCdwvcCf3zLHL+Iv7LtGHLpr+GZV8rHG8tK766FGYBwRbu8pELTt+w==}- resolution: {integrity: sha512-jWi7abTbYwajOytWCQc37VulmWiRae5RyTpaCyDcS5/lMdtwSz5lOpDE67srw/HYe35f1z3fDQw+3txg7gNtWw==}- engines: {node: '>= 0.8', npm: 1.2.8000 || >= 1.4.16}-resolution: {integrity: sha512-ml9pReCu3M61kGlqoTm2umSXTlRTuGTx0bfYj+uIUKKYycG5NtSbeetV3faSU6R7ajOPw0g/J1PvK4qNy7s5bA==}engines: {node: '>= 0.8', npm: 1.2.8000 || >= 1.4.16}@@ -11194,10 +11088,6 @@ packages:resolution: {integrity: sha512-D76uU73ulSXrD1UXF4KE2TMxVVwhsnCgfAyTg9k8P6KGZjlXKrOLe4dJQKI3Bxi5wjesZoFXJWElNWBjPZMbhg==}engines: {node: '>=6.6.0'}- resolution: {integrity: sha512-YZ3GUyn/o8gfKJlnlX7g7xq4gyO6OSuhGPKaaGssGB2qgDUS0gPgtTvoyZLTt9Ab6dC4hfc9dV5arkvc/OCmrw==}- engines: {node: '>= 0.6'}-resolution: {integrity: sha512-U71cyTamuh1CRNCfpGY6to28lxvNwPG4Guz/EVjgf3Jmzv0vlDp1atT9eS5dDjMYHucpHbWns6Lwf3BKz6svdw==}engines: {node: '>= 0.6'}@@ -11929,11 +11819,6 @@ packages:resolution: {integrity: sha512-N39iBk0K71F9nb442TLbTkjl24FLUzuvx2i1I2RsEAQsdAdUTuUoW0vlfUXgkMTUOnYqKnWcFfqw4hK4Pw27hg==}- resolution: {integrity: sha512-UeJmFfOrAQS8OJWPZ4qtgHyWExa088/MtK5UEyoJGFH67cDEXkZSviOiKRCZ4Xij0zxI3JECgYs3oKx+AizQBA==}- engines: {node: '>=0.10.0'}- hasBin: true-resolution: {integrity: sha512-UaaM14yby8U3k02ihS1Bmj5Kz2d7CCQM1scxpgs4Mhkq8F1wR2gl3+Ts4h5Ne4Mnt7M9m4Dw7jsuMr3+xO4vZA==}engines: {node: '>=0.12.18'}@@ -12308,10 +12193,6 @@ packages:peerDependencies:express: '>= 4.11'- resolution: {integrity: sha512-5/PsL6iGPdfQ/lKM1UuielYgv3BUoJfz1aUwU9vHZ+J7gyvwdQXFEBIEIaxeGf0GIcreATNyBExtalisDbuMqQ==}- engines: {node: '>= 0.10.0'}-resolution: {integrity: sha512-5T6nhjsT+EOMzuck8JjBHARTHfMht0POzlA60WV2pMD3gyXw2LZnZ+ueGdNxG+0calOJcWKbpFcuzLZ91YWq9Q==}engines: {node: '>= 0.10.0'}@@ -12501,9 +12382,6 @@ packages:resolution: {integrity: sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw==}- resolution: {integrity: sha512-w1cEuf3S+DrLCQL7ET6kz+gmlJdbq9J7yXCSjK/OZCPA+qEN1WyF4ZAf0YYJa4/shHJra2t/d/r8SV4Ji+x+8Q==}-resolution: {integrity: sha512-qOo9F+dMUmC2Lcb4BbVvnKJxTPjCm+RRpe4gDuGrzkL7mEVl/djYSu2OdQ2Pa302N4oqkSg9ir6jaLWJ2USVpQ==}engines: {node: '>=8'}@@ -13777,11 +13655,6 @@ packages:resolution: {integrity: sha512-ykkVRwrYvFm1nb2AJfKKYPr0emF6IiXDYUaFx4Zn9ZuIH7MrzEZ3sD5RlqGXNRpHtvUHJyOnCEFxOlNDtGo7wg==}engines: {node: 20 || >=22}- resolution: {integrity: sha512-ZDi3aP+fG/LchyBzUM804VjddnwfSfsdeYkwt8NcbKRvo4rFkjhs456iLFn3k2ZUWvNe4i48WACDbza8fhq2+w==}- engines: {node: '>=10'}- hasBin: true-resolution: {integrity: sha512-nO6jcEfZWQXDhOiBtG2KvKyEptz7RVbpGP4vTD2hLBdmNQSsCiicO2Ioinv6UI4y9ukqnBpy+XZ9H6uLNgJTlw==}@@ -16229,10 +16102,6 @@ packages:resolution: {integrity: sha512-Hrgsx+orqoygnmhFbKaHE6c296J+HTAQXoxEF6gNupROmmGJRoyzfG3ccAveqCBrwr/2yxQ5BVd/GTl5agOwSg==}engines: {node: '>= 0.6'}- resolution: {integrity: sha512-qqJBtEyVgS0ZmPGdCFPWJ3FreoqvG4MVQln/kCgF7Olq95IbOp0/BWyMwbdtn4VTvkM8Y7khCQ2Xgk/tcrCXig==}- engines: {node: '>= 0.8'}-resolution: {integrity: sha512-8zGqypfENjCIqGhgXToC8aB2r7YrBX+AQAfIPs/Mlk+BtPTztOvTS01NRW/3Eh60J+a48lt8qsCzirQ6loCVfA==}engines: {node: '>= 0.8'}@@ -22501,12 +22370,6 @@ snapshots:'@borewit/[email protected]': {}- '@bull-board/[email protected](@bull-board/[email protected]([email protected]([email protected])))([email protected]([email protected]))':- dependencies:- '@bull-board/ui': 6.14.0([email protected]([email protected]))- bullmq: 5.63.0([email protected])- redis-info: 3.1.0-'@bull-board/[email protected](@bull-board/[email protected]([email protected]([email protected]([email protected]))))([email protected]([email protected]([email protected])))':dependencies:@@ -22525,22 +22388,6 @@ snapshots:- bull- bullmq- dependencies:- '@bull-board/api': 6.14.0(@bull-board/[email protected]([email protected]([email protected])))([email protected]([email protected]))- '@bull-board/ui': 6.14.0([email protected]([email protected]))- ejs: 3.1.10- express: 4.21.2([email protected])- transitivePeerDependencies:- - bullmq- - supports-color-- dependencies:- '@bull-board/api': 6.14.0(@bull-board/[email protected]([email protected]([email protected])))([email protected]([email protected]))- transitivePeerDependencies:- - bullmq-dependencies:'@bull-board/api': 9.6.0(@bull-board/[email protected]([email protected]([email protected]([email protected]))))([email protected]([email protected]([email protected])))@@ -30858,23 +30705,6 @@ snapshots:- dependencies:- bytes: 3.1.2- content-type: 1.0.5- debug: 2.6.9([email protected])- depd: 2.0.0- destroy: 1.2.0- http-errors: 2.0.0- iconv-lite: 0.4.24- on-finished: 2.4.1- qs: 6.11.0- raw-body: 2.5.1- type-is: 1.6.18- unpipe: 1.0.0- transitivePeerDependencies:- - supports-color-dependencies:bytes: 3.1.2@@ -31566,8 +31396,6 @@ snapshots:- [email protected]: {}-@@ -32321,10 +32149,6 @@ snapshots:'@standard-schema/spec': 1.1.0fast-check: 3.23.2- dependencies:- jake: 10.8.7-@@ -32933,42 +32757,6 @@ snapshots:express: 5.2.1([email protected])ip-address: 10.1.0- dependencies:- accepts: 1.3.8- array-flatten: 1.1.1- body-parser: 1.20.1([email protected])- content-disposition: 0.5.4- content-type: 1.0.5- cookie: 0.5.0- cookie-signature: 1.0.6- debug: 2.6.9([email protected])- depd: 2.0.0- encodeurl: 1.0.2- escape-html: 1.0.3- etag: 1.8.1- finalhandler: 1.2.0([email protected])- fresh: 0.5.2- http-errors: 2.0.0- merge-descriptors: 1.0.1- methods: 1.1.2- on-finished: 2.4.1- parseurl: 1.3.3- path-to-regexp: 0.1.7- proxy-addr: 2.0.7- qs: 6.11.0- range-parser: 1.2.1- safe-buffer: 5.2.1- send: 0.18.0([email protected])- serve-static: 1.15.0([email protected])- setprototypeof: 1.2.0- statuses: 2.0.1- type-is: 1.6.18- utils-merge: 1.0.1- vary: 1.1.2- transitivePeerDependencies:- - supports-color-dependencies:accepts: 1.3.8@@ -33283,10 +33071,6 @@ snapshots:- dependencies:- minimatch: 5.1.6-dependencies:to-regex-range: 5.0.1@@ -34759,13 +34543,6 @@ snapshots:dependencies:'@isaacs/cliui': 9.0.0- dependencies:- async: 3.2.5- chalk: 4.1.2- filelist: 1.0.4- minimatch: 3.1.2-@@ -37997,13 +37774,6 @@ snapshots:- dependencies:- bytes: 3.1.2- http-errors: 2.0.0- iconv-lite: 0.4.24- unpipe: 1.0.0-dependencies:bytes: 3.1.2diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yamlindex 0d532cf51..e2cea20ff 100644--- a/pnpm-workspace.yaml+++ b/pnpm-workspace.yaml@@ -7,13 +7,13 @@ packages:patchedDependencies:nuqs: patches/nuqs.patch-# nuqs is an apps/start dependency, but the api and worker images install a-# subset of the workspace (their Dockerfiles copy only their own and their-# backend packages' package.json). The patch then matches nothing, and pnpm 11-# turned that from a warning into ERR_PNPM_UNUSED_PATCH, breaking both builds.-# This only tolerates a patch matching nothing — patch application FAILURES-# still always error in v11 (ignorePatchFailures was removed), so the start-# build still fails loudly if nuqs.patch stops applying cleanly.+# nuqs is an apps/start dependency, but the api image installs a subset of the+# workspace (its Dockerfile copies only its own and its backend packages'+# package.json). The patch then matches nothing, and pnpm 11 turned that from a+# warning into ERR_PNPM_UNUSED_PATCH, breaking the build. This only tolerates a+# patch matching nothing — patch application FAILURES still always error in v11+# (ignorePatchFailures was removed), so the start build still fails loudly if+# nuqs.patch stops applying cleanly.allowUnusedPatches: true# pnpm 11 verifies that node_modules matches the workspace before running a@@ -32,7 +32,7 @@ allowUnusedPatches: true# createEffectfulFunction from the serializer context class that# seroval-plugins <=1.3.3 calls, so every SSR render died with# "r.createEffectfulFunction is not a function" - the whole dashboard 500'd on-# every route, /login included, while api and worker were fine.+# every route, /login included, while the api image was fine.## false = the pnpm 10 behavior these images have always been built with.verifyDepsBeforeRun: false@@ -71,25 +71,6 @@ overrides:# peer to the copy embla-carousel-react actually uses.embla-carousel: 8.0.0-rc22-# @bull-board/api's BullMQAdapter takes a real bullmq Queue (instanceof check-# at runtime, per node_modules/@bull-board/api/dist/queueAdapters/bullMQ.js)-# but the package never declares bullmq as a (peer) dependency - just a-# devDependency used to build itself. Since M4-003 gave apps/worker a-# @openpanel/core dependency, and core pins bullmq exact 6.3.3 (ADR-016) while-# apps/worker/packages/queue stay on the V1 pin ^5.63.0 (ADR-016, same table),-# node's own "walk up and find bullmq" resolution started finding whichever-# copy pnpm happened to hoist nearest - 6.3.3, not the 5.63.0 the queues are-# actually built from. TS caught it as `Queue<...>` missing 6.x-only members-# (queueMetaInitialized, backend, ...); unpatched, the same mismatch trips-# BullMQAdapter's `instanceof Queue` guard at runtime. Declaring the peer-# forces pnpm to resolve it per-consumer like any other correctly-described-# package, matching apps/worker's own ^5.63.0 - no version decision here, just-# making the two pins ADR-016 already accepted resolve correctly.-packageExtensions:- "@bull-board/[email protected]":- peerDependencies:- bullmq: "*"-# Define a catalog of version ranges.catalog:zod: ^4.1.8diff --git a/self-hosting/caddy/Caddyfile.template b/self-hosting/caddy/Caddyfile.templateindex a17034585..5a6d86635 100644--- a/self-hosting/caddy/Caddyfile.template+++ b/self-hosting/caddy/Caddyfile.template@@ -15,5 +15,11 @@ worker.$DOMAIN_NAME {$SSL_CONFIGadmin $BASIC_AUTH_PASSWORD}+ # Bull-board moved from `/` (v2's separate worker server) to `/bullboard`+ # on the merged process's single port, so keep worker.<domain> landing on+ # it. It is additionally behind the dashboard session guard now - this+ # basic_auth is no longer the only thing in front of it.+ redir / /bullboard/+reverse_proxy op-worker:3000}\ No newline at end of filediff --git a/self-hosting/coolify.yml b/self-hosting/coolify.ymlindex 46aa5593f..301a1bdc8 100644--- a/self-hosting/coolify.yml+++ b/self-hosting/coolify.yml@@ -186,13 +186,17 @@ services:timeout: 5sretries: 5+ # ONE image, two roles — see the note on op-worker in+ # docker-compose.template.yml. ROLE=worker turns the api image into the+ # queue/scheduler/Kafka half.opworker:- image: lindesvard/openpanel-worker:latest+ image: lindesvard/openpanel-api:latestrestart: alwaysdepends_on:opapi:condition: service_healthyenvironment:+ - ROLE=worker# FQDN- SERVICE_FQDN_OPBULLBOARD# Common@@ -206,7 +210,7 @@ services:# Set coolify FQDN domain- API_URL=$SERVICE_FQDN_OPAPIhealthcheck:- test: ["CMD-SHELL", "curl -f http://localhost:3000/healthcheck || exit 1"]+ test: ["CMD-SHELL", "curl -f http://localhost:3000/healthz/ready || exit 1"]interval: 10stimeout: 5sretries: 5diff --git a/self-hosting/docker-compose.template.yml b/self-hosting/docker-compose.template.ymlindex d8b18ae8b..6ecdb99cc 100644--- a/self-hosting/docker-compose.template.yml+++ b/self-hosting/docker-compose.template.yml@@ -178,9 +178,16 @@ services:max-size: "20m"max-file: "3"+ # ONE image, two roles. v3.0.0 deleted the separate worker image: the api+ # image serves the queues, the schedulers and the Kafka consumer when it is+ # started with ROLE=worker. Upgrading from v2 means changing this image line+ # and adding ROLE — nothing else; queue names, scheduler ids and the Kafka+ # topic are unchanged.op-worker:- image: lindesvard/openpanel-worker:2+ image: lindesvard/openpanel-api:2restart: always+ environment:+ - ROLE=workerdepends_on:op-api:condition: service_healthy@@ -189,7 +196,7 @@ services:env_file:- .envhealthcheck:- test: ["CMD-SHELL", "curl -f http://localhost:3000/healthcheck || exit 1"]+ test: ["CMD-SHELL", "curl -f http://localhost:3000/healthz/ready || exit 1"]interval: 10stimeout: 5sretries: 5diff --git a/self-hosting/get_latest_images b/self-hosting/get_latest_imagesindex 4744965af..27b7f43ca 100755--- a/self-hosting/get_latest_images+++ b/self-hosting/get_latest_images@@ -3,8 +3,10 @@# GitHub repository (update if needed)REPO="${GITHUB_REPO:-Openpanel-dev/openpanel}"-# Components to find tags for-COMPONENTS=("worker" "api" "dashboard")+# Components to find tags for. No "worker": v3.0.0 deleted the separate worker+# image - op-worker runs the api image with ROLE=worker, so the "api" pass+# below rewrites both service lines.+COMPONENTS=("api" "dashboard")# Docker compose file pathDOCKER_COMPOSE_FILE="${DOCKER_COMPOSE_FILE:-./docker-compose.yml}"@@ -22,7 +24,7 @@ NC='\033[0m' # No Colorshow_usage() {echo "Usage: $0 [COMMAND] [OPTIONS]"echo ""- echo "Fetches the latest Git tags for worker, api, and dashboard components"+ echo "Fetches the latest Git tags for the api and dashboard components"echo ""echo "Commands:"echo " apply Apply the latest tags to docker-compose.yml"@@ -185,7 +187,6 @@ if [ "$TAGS_CLEAN" == "[]" ]; thenecho -e "${YELLOW}Create tags using: git tag <tag-name> && git push origin <tag-name>${NC}"echo ""echo -e "${GRAY}Example tag naming patterns:${NC}"- echo -e " ${GRAY}- worker-v1.0.0${NC}"echo -e " ${GRAY}- api-v1.0.0${NC}"echo -e " ${GRAY}- dashboard-v1.0.0${NC}"exit 1