⭐️⭐️⭐️ Open-source, self-hosted alternative to Linear and Plane. Project management and issue tracking where teams and AI agents work side by side to plan and ship products.
TypeScript agenticagileai-agentsbug-reportingbug-trackerganttissue-trackerjirajira-alternativekanbanlinearlinear-alternativeopen-sourceproduct-managementproject-managementroadmapself-hostedsprint-planningteam-collaborationtrello-alternative
refactor: replace the internal api routes with direct database access (#373)
Croff6c4761964580
diff --git a/.env.example b/.env.exampleindex bd4e5cdf..bfceb2b1 100644--- a/.env.example+++ b/.env.example@@ -68,10 +68,6 @@ S3_REGION=us-east-1 # required by the SDK, ignored by MinIO# credentials undecryptable. openssl rand -base64 32APP_ENCRYPTION_KEY=-# ── Internal token shared by api, worker and bot ──-# Same value for all three. openssl rand -base64 32-WORKER_INTERNAL_TOKEN=change-me-please-generate-a-real-secret-# ── Outbound request policy (apps/api + apps/worker) ──# Server-side fetches of a URL that came from a request — a webhook target, an# attachment to import, a repository host — are refused when the URL resolves to a@@ -95,11 +91,13 @@ WORKER_INTERNAL_TOKEN=change-me-please-generate-a-real-secret# SSRF_ALLOWED_HOSTS=git.internal.example.com# ── Telegram bot (apps/bot) — the bot token is stored in the DB, not here ──-# BOT_CONFIG_POLL_INTERVAL_MS=30000 # how often to re-read bot settings from the API-# BOT_CONFIG_RETRY_INTERVAL_MS=5000 # retry delay after the API could not be reached-# BOT_API_TIMEOUT_MS=15000 # per-request timeout for calls to the API+# The service reads it with DATABASE_URL and APP_ENCRYPTION_KEY, set above.+# BOT_CONFIG_POLL_INTERVAL_MS=30000 # how often to re-read the bot settings+# BOT_CONFIG_RETRY_INTERVAL_MS=5000 # retry delay after the database was unreachable-# ── Background worker (apps/worker): webhooks, schedules, agent runs ──+# ── Background worker (apps/worker): webhooks, agent schedules, notifications ──+# Notification delivery reads the stored provider credentials, so the worker needs+# DATABASE_URL and APP_ENCRYPTION_KEY, set above.# WEBHOOK_POLL_INTERVAL_MS=2000 # how often to poll for due deliveries# WEBHOOK_BATCH_SIZE=20 # deliveries claimed/sent per tick# WEBHOOK_TIMEOUT_MS=10000 # per-delivery HTTP timeout@@ -108,15 +106,17 @@ WORKER_INTERNAL_TOKEN=change-me-please-generate-a-real-secret# WEBHOOK_LEASE_SECONDS=120 # claim lease; a crashed delivery is retried after this# WEBHOOK_CLEANUP_DAYS=30 # succeeded deliveries older than this are deleted# WEBHOOK_CLEANUP_EVERY_TICKS=300 # run the cleanup once every N ticks+# NOTIFICATION_BATCH_SIZE=20 # notification deliveries claimed per tick+# NOTIFICATION_MAX_ATTEMPTS=5 # attempts before a delivery is marked failed+# NOTIFICATION_LEASE_SECONDS=120 # claim lease for crash recovery++# ── Background jobs (apps/api): the agent run queue and the auto-archive sweep ──# AGENT_RUN_POLL_INTERVAL_MS=2000 # how often to poll for due agent runs# AGENT_RUN_BATCH_SIZE=5 # agent runs claimed per tick-# AGENT_RUN_TIMEOUT_MS=240000 # internal API request timeout+# AGENT_RUN_TIMEOUT_MS=240000 # wall-clock cap on one run# AGENT_RUN_MAX_ATTEMPTS=3 # attempts before a run is marked failed# AGENT_RUN_LEASE_SECONDS=300 # claim lease for crash recovery-# NOTIFICATION_BATCH_SIZE=20 # notification deliveries claimed per tick-# NOTIFICATION_TIMEOUT_MS=20000 # internal send-API request timeout-# NOTIFICATION_MAX_ATTEMPTS=5 # attempts before a delivery is marked failed-# NOTIFICATION_LEASE_SECONDS=120 # claim lease for crash recovery+# AUTO_ARCHIVE_INTERVAL_MS=3600000 # how often the auto-archive sweep runs (own loop)# ── Chat with an external agent (apps/api): the feed its runner drains ──# AGENT_CHAT_LEASE_SECONDS=300 # claim lease; an abandoned answer is served again after thisdiff --git a/AGENTS.md b/AGENTS.mdindex 14460d3f..18c78cd5 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -69,11 +69,11 @@ thinking.## Layout```-apps/api Elysia (Bun) — mounts better-auth at /api/auth/* :3000+apps/api Elysia (Bun) — better-auth at /api/auth/*, agent runs :3000apps/web Next.js App Router, SSR (not SPA) + shadcn + TanStack Q :3001-apps/worker webhook and notification delivery, agent runs, schedules+apps/worker webhook and notification delivery, agent schedulesapps/bot Telegram bot, long polling-packages/db @repo/db — Drizzle client, schema, migrations, permission matrix+packages/db @repo/db — Drizzle client, schema, migrations, permission matrix, shared config readerspackages/auth @repo/auth — better-auth server instance + instance auth settingspackages/crypto @repo/crypto — AES-256-GCM encryption for secrets at restpackages/mailer @repo/mailer — SMTP/Resend transport for outbound email@@ -83,8 +83,8 @@ packages/runner @itsaplan/runner — CLI that runs an external agent's queued tapackages/eslint-config @repo/eslint-config — shared ESLint config```-Dependency graph: `api → @repo/auth → @repo/db`. **The web app never imports packages-directly** — it talks to the API over HTTP (better-auth client + fetch).+Dependency graph: `api → @repo/auth → @repo/db → @repo/crypto`. **The web app never+imports packages directly** — it talks to the API over HTTP (better-auth client + fetch).## Commands (from root)@@ -298,7 +298,9 @@ tidy moves the code. This is separate from the CI gate (`format:check` + `lint`- Tests run on `bun test`. `apps/api` uses Eden Treaty; its setup and the rules forwriting tests are in `apps/api/AGENTS.md`. They are integration tests against a realtest Postgres (`.env.test`), not mocks. `packages/runner` has plain unit tests that- need nothing running.+ need nothing running. `apps/api`, `apps/bot` and `apps/worker` share one test database and+ the api suite truncates every table before each test, so `bun run test` passes+ `--concurrency=1` to turbo: the suites run one package at a time.- `bun --filter` needs the `=` form: `bun --filter='@repo/db' run <script>`. The space form`bun --filter <name> run <script>` matches no packages in Bun 1.3.9.diff --git a/SECURITY.md b/SECURITY.mdindex 219dc1e0..ff95bb03 100644--- a/SECURITY.md+++ b/SECURITY.md@@ -39,8 +39,8 @@ service through raw request volume.An instance is only as safe as its configuration:-- Generate `BETTER_AUTH_SECRET`, `APP_ENCRYPTION_KEY`, and `WORKER_INTERNAL_TOKEN` with- `openssl rand -base64 32`. Never reuse the example values.+- Generate `BETTER_AUTH_SECRET` and `APP_ENCRYPTION_KEY` with `openssl rand -base64 32`.+ Never reuse the example values.- `APP_ENCRYPTION_KEY` encrypts stored provider credentials at rest. Losing it makesthose credentials undecryptable, changing it has the same effect.- Serve the app over HTTPS. Cookies are marked `secure` in production.diff --git a/apps/api/AGENTS.md b/apps/api/AGENTS.mdindex 81d17e51..fc04282c 100644--- a/apps/api/AGENTS.md+++ b/apps/api/AGENTS.md@@ -14,8 +14,15 @@ Rules and invariants for this package below; read the code for the walkthrough.stays flat. A schema several of the nested features share sits in the parent's`model.ts` and is re-exported from each child's (`agentParams`).- `src/app.ts` assembles and exports the app (`export const app`, no `.listen()`);- `src/index.ts` only binds the port. `export type App = typeof app` types the Eden- Treaty client (web + tests).+ `src/index.ts` binds the port and starts `src/background.ts`.+ `export type App = typeof app` types the Eden Treaty client (web + tests).+- **Background jobs are started from `index.ts`, never assembled into the app**, so+ importing the app in a test starts nothing. `background.ts` drains the `agent_run`+ queue in one loop and runs the auto-archive sweep in a loop of its own, so neither+ waits on the other. Several api replicas run them without overlapping: the queue is+ claimed with `FOR UPDATE SKIP LOCKED`, and the sweep only touches rows it has not+ archived yet. An agent run is built from the queue row alone — the project it works+ in and the bot user it acts as are read there, never handed in.- `index.ts`: `new Elysia({ name: "<feature>", detail: { tags: ["<Tag>"] } })` —routes chained directly on it, each route sets `detail.summary`. Handlers only;the schemas they reference come from `model.ts`.@@ -23,7 +30,7 @@ Rules and invariants for this package below; read the code for the walkthrough.is `t.Partial(<create body>)` where it accepts the same fields.- `service.ts`: plain async functions, no Elysia/HTTP types, returns DTOs never rows.- Every feature lives under `src/modules/`. A feature that needs more than the three- files adds one per concern next to them (`emit.ts`, `internal-routes.ts`), it does+ files adds one per concern next to them (`emit.ts`, `run-queue.ts`), it doesnot grow a `routes.ts` + `store.ts` pair.- Imports inside a module are relative (`./model`); everything it reaches outsideitself goes through the subpath aliases in `apps/api/package.json` — `#shared/*`,diff --git a/apps/api/Dockerfile b/apps/api/Dockerfileindex 1406dd8c..3c5d7386 100644--- a/apps/api/Dockerfile+++ b/apps/api/Dockerfile@@ -20,11 +20,13 @@ COPY packages/mailer/package.json ./packages/mailer/COPY packages/net/package.json ./packages/net/COPY packages/runner/package.json ./packages/runner/COPY packages/eslint-config/package.json ./packages/eslint-config/-# --filter api installs only the api workspace and its dependency closure, so the-# image excludes every other app's deps. INSTALL_FLAGS defaults to --production;-# the test gate overrides it to "" so the api suite gets its devDeps (@elysiajs/eden).+# INSTALL_FILTER installs only the api workspace and its dependency closure, so the+# image excludes every other app's deps. INSTALL_FLAGS defaults to --production; the+# test gate overrides both — it runs the bot and worker suites out of this image, and+# those need their own workspace links and the devDeps the suites import.+ARG INSTALL_FILTER="--filter api"ARG INSTALL_FLAGS="--production"-RUN bun install --frozen-lockfile --filter api $INSTALL_FLAGS+RUN bun install --frozen-lockfile $INSTALL_FILTER $INSTALL_FLAGS# 2) Final image. bun keeps some workspace deps in per-package node_modules# (packages/db/node_modules, apps/api/node_modules) rather than hoisting them todiff --git a/apps/api/src/__tests__/integration/openapi.test.ts b/apps/api/src/__tests__/integration/openapi.test.tsindex 01dd1293..94f5ef89 100644--- a/apps/api/src/__tests__/integration/openapi.test.ts+++ b/apps/api/src/__tests__/integration/openapi.test.ts@@ -39,9 +39,6 @@ describe('OpenAPI document', () => {expect(doc.paths['/']!.get!.security).toEqual([]);expect(doc.paths['/me']!.get!.security).toEqual([{}, { apiKey: [] }]);expect(doc.paths['/share/issue/{token}']!.get!.security).toEqual([]);- expect(doc.paths['/internal/agent-runs/execute']!.post!.security).toEqual([- { workerToken: [] },- ]);expect(doc.paths['/scim/v2/Users']!.post!.security).toEqual([{ scimBearer: [] }]);expect(doc.paths['/webhooks/git/{webhookId}']!.post!.security).toContainEqual({gitLabToken: [],diff --git a/apps/api/src/app.ts b/apps/api/src/app.tsindex 3a16a1f0..0ffa3423 100644--- a/apps/api/src/app.ts+++ b/apps/api/src/app.ts@@ -4,20 +4,17 @@ import {oAuthProtectedResourceMetadata,trustedOrigins,getAuthSettings,- hasConfiguredEmailProvider,hasConfiguredGoogle,hasConfiguredOidc,getOidcLabel,} from '@repo/auth';+import { hasConfiguredEmailProvider } from '@repo/db';import { cors } from '@elysiajs/cors';import { swagger } from '@elysiajs/swagger';import { Elysia } from 'elysia';import { planner } from './planner';import { mountMcp } from './mcp/mount';import { setMcpApp } from './mcp/app-ref';-import { internalAgentRunRoutes } from './modules/agents/core/internal-routes';-import { internalNotificationRoutes } from './modules/notifications/internal-routes';-import { internalTelegramRoutes } from './modules/telegram/internal-routes';import { gitWebhookRoutes } from './modules/git/webhook';import { scimRoutes } from './modules/scim';import { syncOidcGroupsAfterCallback } from './modules/scim/oidc-sync';@@ -166,11 +163,6 @@ export const app = new Elysia()name: 'System',description: 'Liveness, the current session user, and the instance sign-in policy',},- {- name: 'Internal',- description:- 'Endpoints the worker and the bot call with the shared WORKER_INTERNAL_TOKEN',- },],// Planner routes are session-gated. Besides the session cookie (sent by the// browser, not modelled here), a request may carry an `x-api-key` header:@@ -186,12 +178,6 @@ export const app = new Elysia()bearerFormat: 'opaque',description: 'Instance SCIM token generated in God mode.',},- workerToken: {- type: 'apiKey',- in: 'header',- name: 'x-worker-token',- description: 'Shared token used only by the worker and bot services.',- },gitHubSignature: {type: 'apiKey',in: 'header',@@ -313,9 +299,6 @@ export const app = new Elysia()description: 'Liveness probe: returns the api name and `status: "ok"`.',},})- .use(internalAgentRunRoutes)- .use(internalNotificationRoutes)- .use(internalTelegramRoutes)// Inbound repository webhook receiver (authenticated by its per-project secret)..use(gitWebhookRoutes)// SCIM 2.0 provisioning (authenticated by the instance SCIM bearer token). Mounteddiff --git a/apps/api/src/background.ts b/apps/api/src/background.tsnew file mode 100644index 00000000..f8661194--- /dev/null+++ b/apps/api/src/background.ts@@ -0,0 +1,36 @@+import { intEnv } from '#shared/lib';+import { agentRunConfig } from '#modules/agents/core/run-queue';+import { processAgentRuns } from '#modules/agents/core/run-poller';+import { sweepStaleIssues } from '#modules/issues/auto-archive';++// The api's background jobs, started by index.ts rather than assembled into the app,+// so importing the app in a test starts nothing. Several api replicas run them without+// overlapping: the queue is claimed with FOR UPDATE SKIP LOCKED, and the sweep only+// touches rows it has not archived yet.++// Each job gets a loop of its own. A run is an LLM call of minutes and a first sweep+// can carry thousands of issues, so sharing one loop would let either hold the other+// back for that long.+export function startBackgroundJobs(): void {+ startLoop('agent-runs', processAgentRuns, agentRunConfig.pollIntervalMs);+ // Archiving is not time-sensitive, so the sweep runs far less often than the queue+ // is drained.+ startLoop('auto-archive', autoArchive, () => intEnv('AUTO_ARCHIVE_INTERVAL_MS', 3_600_000));+}++async function autoArchive(): Promise<void> {+ const archived = await sweepStaleIssues();+ if (archived > 0) console.log(`[background] auto-archived ${archived} stale issues`);+}++function startLoop(name: string, job: () => Promise<void>, intervalMs: () => number): void {+ const tick = async () => {+ try {+ await job();+ } catch (error) {+ console.error(`[background] ${name} failed:`, error);+ }+ setTimeout(tick, intervalMs()).unref();+ };+ void tick();+}diff --git a/apps/api/src/index.ts b/apps/api/src/index.tsindex 9c0aecba..64e63057 100644--- a/apps/api/src/index.ts+++ b/apps/api/src/index.ts@@ -1,9 +1,12 @@import { app } from './app';+import { startBackgroundJobs } from './background';// Bind the port. The app itself is assembled in ./app.ts (without `.listen()`)// so tests can import it and drive routes in memory.app.listen(Number(process.env.API_PORT ?? 3000));+startBackgroundJobs();+console.log(`🦊 API running at http://${app.server?.hostname}:${app.server?.port}`);export type { App } from './app';diff --git a/apps/api/src/modules/agents/chat/service.ts b/apps/api/src/modules/agents/chat/service.tsindex db0952af..36d504be 100644--- a/apps/api/src/modules/agents/chat/service.ts+++ b/apps/api/src/modules/agents/chat/service.ts@@ -1,7 +1,7 @@import { db, agentChatEvent, agentChatFavorite, agentChatMessage, agentChatThread } from '@repo/db';import { and, asc, desc, eq, gt, inArray, notExists, sql } from 'drizzle-orm';import { setTimeout as sleep } from 'node:timers/promises';-import { iso } from '#shared/lib';+import { intEnv, iso } from '#shared/lib';import { deleteContextUsage, recordContextUsage, type ContextUsage } from '../chat-usage';import { deleteFavorite, FAVORITES_LIMIT } from '../chat-favorites';import {@@ -13,7 +13,6 @@ import {type ThreadRow,} from '../chat-history';import { appendTextPart } from '../chat-parts';-import { intEnv } from '../core/helpers/env';import { attachmentPreamble, chartPreamble, projectsPreamble } from '../core/prompt/framing';import { peoplePreamble, type Person } from '../core/prompt/run-context';import type { ChatMessagePage, ChatPart, ChatThreadPage } from '../model';diff --git a/apps/api/src/modules/agents/core/__tests__/integration/run-limits.test.ts b/apps/api/src/modules/agents/core/__tests__/integration/run-limits.test.tsindex bac0fcf1..edf060fc 100644--- a/apps/api/src/modules/agents/core/__tests__/integration/run-limits.test.ts+++ b/apps/api/src/modules/agents/core/__tests__/integration/run-limits.test.ts@@ -1,20 +1,22 @@import { describe, it, expect, afterEach, beforeEach } from 'bun:test';import { db, agentRun } from '@repo/db';import { eq } from 'drizzle-orm';-import { api, authedApi } from '#tests/helpers/app';+import { authedApi } from '#tests/helpers/app';import { signUpTestUser } from '#tests/helpers/auth';import { resetDb } from '#tests/helpers/db';import { clearLimits, setLimits } from '#tests/helpers/limits';import { createAgent } from '#tests/helpers/agents';import { claimDueRuns, enqueueAgentRun } from '../../run-queue';+import { processAgentRuns } from '../../run-poller';-// The ceiling on the runs a team has in flight, enforced where the worker hands a+// The ceiling on the runs a team has in flight, enforced where the poller hands a// claimed run to the runtime. The agent is left without a model credential, so a run// that passes the ceiling fails on the model rather than calling one — which is what// tells "turned away" and "let through" apart.-const WORKER_TOKEN = 'run-limits-test-worker-token';-+// Leaves one run of the team in flight and a second one due, which is the pair the+// ceiling is read against. Claiming the first stamps it without running it, so it holds+// a slot for as long as its lease.async function setup() {const owner = await signUpTestUser({ name: 'Owner' });const asOwner = authedApi(owner.cookie);@@ -27,90 +29,61 @@ async function setup() {const issue = (await asOwner.projects({ projectKey: 'MKT' }).issues.post({ columnId, title: 'Task' })).data!;-const projectId = agent.projects[0].id;- for (const prompt of ['first', 'second'])- await enqueueAgentRun({+ const enqueue = (prompt: string) =>+ enqueueAgentRun({agentId: agent.id,projectId,issueId: issue.id,sourceActivityId: null,prompt,});- // Claiming stamps both runs, which is what makes them count as in flight.++ await enqueue('first');await claimDueRuns();- const [first, second] = await db+ await enqueue('second');+ const [, second] = await db.select().from(agentRun).where(eq(agentRun.issueId, issue.id)).orderBy(agentRun.id);-- return { agent, issue, projectId, first, second };+ return { second };}-type Setup = Awaited<ReturnType<typeof setup>>;--function execute({ agent, issue, projectId }: Setup, run: { id: number; attempts: number }) {- return api.internal['agent-runs'].execute.post(- {- id: run.id,- agentId: agent.id,- issueId: issue.id,- scheduleId: null,- trigger: 'delegation',- prompt: 'do it',- attempts: run.attempts,- projectId,- agentUserId: agent.userId,- issueIdentifier: null,- issueTitle: issue.title,- assigneeName: null,- requesterName: null,- },- { headers: { 'x-worker-token': WORKER_TOKEN } },- );+// Reads the second run back after one poll. A run turned away for a full queue keeps+// the attempts it had; one let through spends an attempt on the model it does not have.+async function pollAndRead(second: { id: number }) {+ await processAgentRuns();+ const [row] = await db.select().from(agentRun).where(eq(agentRun.id, second.id));+ return row;}describe('agent run limits', () => {- let previousToken: string | undefined;-- beforeEach(async () => {- previousToken = process.env.WORKER_INTERNAL_TOKEN;- process.env.WORKER_INTERNAL_TOKEN = WORKER_TOKEN;- await resetDb();- });-- afterEach(() => {- if (previousToken == null) delete process.env.WORKER_INTERNAL_TOKEN;- else process.env.WORKER_INTERNAL_TOKEN = previousToken;- clearLimits();- });+ beforeEach(resetDb);+ afterEach(clearLimits);it('turns away a run the team has no free slot for, leaving it queued', async () => {- const state = await setup();+ const { second } = await setup();setLimits({ maxConcurrentRuns: 1 });- const res = await execute(state, state.second);- expect(res.status).toBe(503);-- const [row] = await db.select().from(agentRun).where(eq(agentRun.id, state.second.id));- expect(row).toMatchObject({ status: 'pending', attempts: state.second.attempts });+ const row = await pollAndRead(second);+ expect(row).toMatchObject({ status: 'pending', attempts: second.attempts, lastError: null });});it('lets a run through while the team is under the ceiling', async () => {- const state = await setup();+ const { second } = await setup();setLimits({ maxConcurrentRuns: 2 });- const res = await execute(state, state.second);- expect(res.status).toBe(400);- // Treaty types the error body as its validation shape, which the message is not.- expect(String(res.error!.value)).toBe('Agent has no model credential set');+ const row = await pollAndRead(second);+ expect(row.attempts).toBe(second.attempts + 1);+ expect(row.lastError).toBe('Agent has no model credential set');});it('runs without a ceiling when the instance sets no limits', async () => {- const state = await setup();+ const { second } = await setup();- const res = await execute(state, state.second);- expect(res.status).toBe(400);+ const row = await pollAndRead(second);+ expect(row.attempts).toBe(second.attempts + 1);+ expect(row.lastError).toBe('Agent has no model credential set');});});diff --git a/apps/api/src/modules/agents/core/__tests__/integration/run-queue.test.ts b/apps/api/src/modules/agents/core/__tests__/integration/run-queue.test.tsindex 7e462fc3..c9bc338a 100644--- a/apps/api/src/modules/agents/core/__tests__/integration/run-queue.test.ts+++ b/apps/api/src/modules/agents/core/__tests__/integration/run-queue.test.ts@@ -104,7 +104,7 @@ describe('agent_run queue store', () => {expect(await countRunsAhead(teamId, second.id)).toBe(1);// A finished run stops counting, so the next one is no longer held behind it.- await markRunSuccess(runId);+ await markRunSuccess(runId, 'done', null);expect(await countRunsAhead(teamId, second.id)).toBe(0);});@@ -112,8 +112,14 @@ describe('agent_run queue store', () => {const { asOwner, columnId } = await setup();const { runId } = await enqueueRun(asOwner, columnId);await claimDueRuns();- await markRunSuccess(runId);- expect(await readRun(runId)).toMatchObject({ status: 'success', lastError: null });+ await markRunSuccess(runId, 'done', { inputTokens: 12, outputTokens: 3 });+ expect(await readRun(runId)).toMatchObject({+ status: 'success',+ output: 'done',+ lastError: null,+ inputTokens: 12,+ outputTokens: 3,+ });});it('marks a run failed and records the error', async () => {diff --git a/apps/api/src/modules/agents/core/__tests__/integration/thread-cleanup.test.ts b/apps/api/src/modules/agents/core/__tests__/integration/thread-cleanup.test.tsindex 15fa8cc5..5ce3051e 100644--- a/apps/api/src/modules/agents/core/__tests__/integration/thread-cleanup.test.ts+++ b/apps/api/src/modules/agents/core/__tests__/integration/thread-cleanup.test.ts@@ -9,7 +9,8 @@ import { createAgent } from '#tests/helpers/agents';// Agent conversation threads live in Mastra's tables, which carry no foreign key of// ours, so they are deleted explicitly when what they are bound to goes away: the-// agent, its project, or (for an issue run's thread) the issue being archived.+// agent, its project, or the schedule. Archiving an issue is reversible and drops+// none of them.//// The runtime is not exercised — a live model call would be needed to produce a thread// — so threads are seeded through the memory module the same way a run creates them.@@ -166,24 +167,20 @@ describe('agent thread cleanup', () => {expect(await messageCount(thread)).toBe(0);});- it("deletes an issue's run threads when it is archived", async () => {+ it("keeps an issue's run threads when it is archived", async () => {const { asOwner, columnId } = await setup();const a = await createInternalAgent(asOwner, 'Bot A', 'bota');const b = await createInternalAgent(asOwner, 'Bot B', 'botb');const issue = await createIssue(asOwner, columnId, 'Ship it');- const other = await createIssue(asOwner, columnId, 'Keep it');const threadA = await seedIssueThread(a, issue.id);const threadB = await seedIssueThread(b, issue.id);await seedMessage(threadA, 'agent-bot');- expect(await messageCount(threadA)).toBe(1);- const untouched = await seedIssueThread(a, other.id);const res = await asOwner.issues({ issueId: issue.id }).archive.post();expect(res.status).toBe(200);- expect(await threadExists(threadA)).toBe(false);- expect(await threadExists(threadB)).toBe(false);- expect(await messageCount(threadA)).toBe(0);- expect(await threadExists(untouched)).toBe(true);+ expect(await threadExists(threadA)).toBe(true);+ expect(await threadExists(threadB)).toBe(true);+ expect(await messageCount(threadA)).toBe(1);});});diff --git a/apps/api/src/modules/agents/core/helpers/backoff.ts b/apps/api/src/modules/agents/core/helpers/backoff.tsnew file mode 100644index 00000000..fc15e38b--- /dev/null+++ b/apps/api/src/modules/agents/core/helpers/backoff.ts@@ -0,0 +1,8 @@+// Equal-jitter exponential backoff. Half of the exponential window is fixed and half+// is random, so retries spread out instead of all coming back at the same moment.+// `attempts` is 1-based: the number of the attempt that just failed.+export function equalJitterBackoffMs(attempts: number, baseMs: number, capMs: number): number {+ const window = Math.min(capMs, baseMs * 2 ** Math.max(0, attempts - 1));+ const half = window / 2;+ return Math.floor(half + Math.random() * half);+}diff --git a/apps/api/src/modules/agents/core/helpers/env.ts b/apps/api/src/modules/agents/core/helpers/env.tsdeleted file mode 100644index 8ce1378a..00000000--- a/apps/api/src/modules/agents/core/helpers/env.ts+++ /dev/null@@ -1,8 +0,0 @@-// Reads a positive integer from an environment variable, falling back to the default-// when unset, empty, or not a positive finite number.-export function intEnv(name: string, fallback: number): number {- const raw = process.env[name];- if (raw == null || raw === '') return fallback;- const n = Number(raw);- return Number.isFinite(n) && n > 0 ? n : fallback;-}diff --git a/apps/api/src/modules/agents/core/internal-routes.ts b/apps/api/src/modules/agents/core/internal-routes.tsdeleted file mode 100644index 1f1802d8..00000000--- a/apps/api/src/modules/agents/core/internal-routes.ts+++ /dev/null@@ -1,115 +0,0 @@-import { Elysia, t } from 'elysia';-import { runAgent } from './runtime';-import { deleteThreadsWhere } from './runtime/memory';-import { runThreadId } from './runtime/thread-ids';-import { framePrompt, runModePreamble, peopleContext } from './prompt/framing';-import { recordAgentRunFinished, recordAgentRunStarted } from './run-activity';-import { agentRunConfig, countRunsAhead } from './run-queue';-import { getProjectTeamId } from '#modules/projects/service';-import { getLimits } from '#shared/limits';--const runBody = t.Object({- id: t.Number(),- agentId: t.Number(),- issueId: t.Nullable(t.Number()),- scheduleId: t.Nullable(t.Number()),- trigger: t.UnionEnum(['mention', 'delegation', 'field', 'schedule', 'manual']),- prompt: t.String(),- attempts: t.Number(),- projectId: t.Number(),- agentUserId: t.String(),- issueIdentifier: t.Nullable(t.String()),- issueTitle: t.Nullable(t.String()),- assigneeName: t.Nullable(t.String()),- requesterName: t.Nullable(t.String()),- // Optional so a worker still running the previous build can hand a run over.- agentUsername: t.Optional(t.Nullable(t.String())),- assigneeUsername: t.Optional(t.Nullable(t.String())),- requesterUsername: t.Optional(t.Nullable(t.String())),- sourceActivityId: t.Optional(t.Nullable(t.Number())),- threadContext: t.Optional(t.Nullable(t.String())),-});--function workerTokenValid(headers: Record<string, string | undefined>): boolean {- const expected = process.env.WORKER_INTERNAL_TOKEN;- return !!expected && headers['x-worker-token'] === expected;-}--export const internalAgentRunRoutes = new Elysia({- name: 'internal-agent-runs',- detail: { tags: ['Internal'] },-})- .post(- '/internal/agent-runs/execute',- async ({ body, headers, set }) => {- if (!workerTokenValid(headers)) {- set.status = 401;- return { error: 'Unauthorized' };- }- const teamId = await getProjectTeamId(body.projectId);- const { maxConcurrentRuns, maxRunSeconds } = await getLimits({ teamId });- // Turned back before anything is recorded, so a run that waits for a free slot- // leaves no trace on the issue and keeps the attempts it has.- if (maxConcurrentRuns > 0 && (await countRunsAhead(teamId, body.id)) >= maxConcurrentRuns) {- set.status = 503;- return { error: 'The team is already running as many agents as it may at once' };- }- // The worker writes the run's own status; the issue's timeline entries are- // written here, where the agent's work actually starts and ends. A failure the- // worker will retry is not the end of the run, so only the last attempt logs one.- await recordAgentRunStarted(body);- try {- const result = await runAgent(body.agentId, body.projectId, framePrompt(body), {- callerUserId: body.agentUserId,- threadId: runThreadId(body),- issueId: body.issueId,- scheduleId: body.scheduleId,- contextPreamble: runModePreamble(body.trigger) + peopleContext(body),- ...(maxRunSeconds > 0 ? { abortSignal: AbortSignal.timeout(maxRunSeconds * 1000) } : {}),- });- await recordAgentRunFinished(body, 'success');- // The worker owns the run row, so the counts go back with the answer for it to- // store. Null where the model reports none, which the run history shows as a dash.- return { output: result.text, usage: result.usage };- } catch (error) {- if (body.attempts >= agentRunConfig.maxAttempts())- await recordAgentRunFinished(body, 'failed');- throw error;- }- },- {- body: runBody,- detail: {- summary: 'Execute a queued agent run',- description:- 'Run one claimed agent run in the api, where the model credentials and the agent ' +- 'runtime live, and return the text the agent produced. Called by the worker with ' +- 'the x-worker-token header.',- },- },- )-- // Deletes the agent memory of archived issues. The worker's auto-archive sweep- // writes archived_at directly in the database and does not import Mastra, so it- // asks the api to drop the threads the same way it asks it to run an agent.- .post(- '/internal/agent-threads/delete-for-issues',- async ({ body, headers, set }) => {- if (!workerTokenValid(headers)) {- set.status = 401;- return { error: 'Unauthorized' };- }- let deleted = 0;- for (const issueId of body.issueIds) deleted += await deleteThreadsWhere({ issueId });- return { deleted };- },- {- body: t.Object({ issueIds: t.Array(t.Number()) }),- detail: {- summary: 'Delete the agent threads of issues',- description:- 'Drop the agent memory threads of the given issues and return how many were ' +- 'deleted. Called by the worker with the x-worker-token header.',- },- },- );diff --git a/apps/api/src/modules/agents/core/prompt/framing.ts b/apps/api/src/modules/agents/core/prompt/framing.tsindex 4fe82d10..8d10ea40 100644--- a/apps/api/src/modules/agents/core/prompt/framing.ts+++ b/apps/api/src/modules/agents/core/prompt/framing.ts@@ -8,8 +8,8 @@ import type { AgentRunTrigger } from '../../model';// (runModePreamble) and the people involved (peopleContext). The interactive test// chat does not use this path — it frames its own prompt in the controller.-// The fields of a claimed run this module reads. The HTTP body in internal-routes.ts-// is structurally compatible.+// The fields of a claimed run this module reads, as both the queue poller and the+// external runner build them.export interface RunForPrompt {id: number;trigger: AgentRunTrigger;@@ -18,19 +18,18 @@ export interface RunForPrompt {issueIdentifier: string | null;issueTitle: string | null;// The issue's assignee and, on a mention run, the author of the comment behind it:- // the name they are called by and the handle they are tagged by. The handles are- // optional so a worker still running the previous build can hand a run over.+ // the name they are called by and the handle they are tagged by.assigneeName: string | null;- assigneeUsername?: string | null;+ assigneeUsername: string | null;requesterName: string | null;- requesterUsername?: string | null;+ requesterUsername: string | null;agentUserId: string;- agentUsername?: string | null;- // The comment the mention replies to and the ones above it, oldest first. Absent- // for a top-level mention.- threadContext?: string | null;+ agentUsername: string | null;+ // The comment the mention replies to and the ones above it, oldest first. Null for+ // a top-level mention.+ threadContext: string | null;// The comment that mentioned the agent, so it can answer in the same thread.- sourceActivityId?: number | null;+ sourceActivityId: number | null;}// System-instruction block describing how this run was started, so the agent knowsdiff --git a/apps/api/src/modules/agents/core/run-poller.ts b/apps/api/src/modules/agents/core/run-poller.tsnew file mode 100644index 00000000..41ca1fd8--- /dev/null+++ b/apps/api/src/modules/agents/core/run-poller.ts@@ -0,0 +1,78 @@+import { getProjectTeamId } from '#modules/projects/service';+import { intEnv } from '#shared/lib';+import { getLimits } from '#shared/limits';+import { equalJitterBackoffMs } from './helpers/backoff';+import { framePrompt, peopleContext, runModePreamble } from './prompt/framing';+import { recordAgentRunFinished, recordAgentRunStarted } from './run-activity';+import {+ agentRunConfig,+ claimDueRuns,+ countRunsAhead,+ deferRun,+ markRunFailed,+ markRunSuccess,+ scheduleRunRetry,+ type ClaimedRun,+} from './run-queue';+import { runAgent } from './runtime';+import { runThreadId } from './runtime/thread-ids';++// Drains the agent_run queue. The runtime, the model credentials and the limits are+// all here, so the run is claimed and executed in the same process — the queue row is+// the only thing a run is built from, so nothing about which project or which bot user+// it acts as can be handed in from outside.++// How long a run waits when its team has no free slot.+const DEFERRED_RETRY_SECONDS = 30;+const RETRY_BASE_MS = 30_000;+const RETRY_CAP_MS = 30 * 60_000;++export async function processAgentRuns(): Promise<void> {+ const runs = await claimDueRuns();+ await Promise.all(runs.map(processRun));+}++async function processRun(run: ClaimedRun): Promise<void> {+ const teamId = await getProjectTeamId(run.projectId);+ const { maxConcurrentRuns, maxRunSeconds } = await getLimits({ teamId });+ if (maxConcurrentRuns > 0 && (await countRunsAhead(teamId, run.id)) >= maxConcurrentRuns) {+ await deferRun(run.id, DEFERRED_RETRY_SECONDS);+ return;+ }+ // The issue's timeline entries are written here, where the agent's work actually+ // starts and ends. A failure that will be retried is not the end of the run, so only+ // the last attempt logs one.+ await recordAgentRunStarted(run);+ try {+ const result = await runAgent(run.agentId, run.projectId, framePrompt(run), {+ callerUserId: run.agentUserId,+ threadId: runThreadId(run),+ issueId: run.issueId,+ scheduleId: run.scheduleId,+ contextPreamble: runModePreamble(run.trigger) + peopleContext(run),+ abortSignal: AbortSignal.timeout(runTimeoutMs(maxRunSeconds)),+ });+ await recordAgentRunFinished(run, 'success');+ await markRunSuccess(run.id, result.text, result.usage);+ } catch (error) {+ const message = error instanceof Error ? error.message : String(error);+ if (run.attempts < agentRunConfig.maxAttempts()) {+ await scheduleRunRetry(+ run.id,+ equalJitterBackoffMs(run.attempts, RETRY_BASE_MS, RETRY_CAP_MS),+ message,+ );+ return;+ }+ await recordAgentRunFinished(run, 'failed');+ await markRunFailed(run.id, message);+ }+}++// The wall time a run gets: the team's ceiling where it has one, and a cap of its own+// either way. Without it a run that never returns holds its slot until the claim lease+// expires and is then started again, having recorded nothing.+function runTimeoutMs(maxRunSeconds: number): number {+ const cap = intEnv('AGENT_RUN_TIMEOUT_MS', 240_000);+ return maxRunSeconds > 0 ? Math.min(maxRunSeconds * 1000, cap) : cap;+}diff --git a/apps/api/src/modules/agents/core/run-queue.ts b/apps/api/src/modules/agents/core/run-queue.tsindex f27e7bb0..88605906 100644--- a/apps/api/src/modules/agents/core/run-queue.ts+++ b/apps/api/src/modules/agents/core/run-queue.ts@@ -1,12 +1,10 @@import { db, agentRun, issue, project } from '@repo/db';import { and, desc, eq, gt, inArray, isNotNull, lt, sql } from 'drizzle-orm';-import { iso } from '#shared/lib';+import { intEnv, iso } from '#shared/lib';import type { AgentRunTrigger } from '../model';-import { intEnv } from './helpers/env';-// The agent_run outbox: data access for issue-triggered runs and run history. The-// background worker claims pending rows, calls the internal runtime route, and-// records the outcome.+// The agent_run outbox: data access for triggered runs and run history. The api's+// run poller claims pending rows, runs them, and records the outcome.// Tuning, env-overridable with defaults. An agent run is an LLM call that can take// tens of seconds, so the lease is generous — it must exceed a run's wall time so a@@ -69,7 +67,10 @@ export async function enqueueAgentRun(input: {export interface ClaimedRun {id: number;agentId: number;- issueId: number;+ // Null for a scheduled or manual run, which works on no single issue.+ issueId: number | null;+ scheduleId: number | null;+ trigger: AgentRunTrigger;prompt: string;attempts: number;// The source comment id when the run was triggered by a mention, null for a@@ -117,7 +118,7 @@ export async function claimDueRuns(): Promise<ClaimedRun[]> {SELECT id FROM agent_run qWHERE q.status = 'pending' AND q.next_attempt_at <= now()AND (SELECT kind FROM ai_agent a WHERE a.id = q.agent_id) = 'internal'- ORDER BY q.next_attempt_at+ ORDER BY q.next_attempt_at, q.idFOR UPDATE SKIP LOCKEDLIMIT ${batchSize})@@ -125,6 +126,8 @@ export async function claimDueRuns(): Promise<ClaimedRun[]> {r.id,r.agent_id AS "agentId",r.issue_id AS "issueId",+ r.schedule_id AS "scheduleId",+ r.trigger,r.prompt,r.attempts,r.source_activity_id AS "sourceActivityId",@@ -185,10 +188,24 @@ export async function loadThreadContext(sourceActivityId: number | null): Promis// A run canceled while it was in flight keeps that outcome: each of these writes only// where the row is still 'pending'.-export async function markRunSuccess(id: number): Promise<void> {++// `usage` is what the last model call of the run read and wrote. Null where the model+// reports none, which the run history shows as a dash.+export async function markRunSuccess(+ id: number,+ output: string,+ usage: { inputTokens: number; outputTokens: number } | null,+): Promise<void> {await db.update(agentRun)- .set({ status: 'success', lastError: null })+ .set({+ status: 'success',+ output,+ lastError: null,+ inputTokens: usage?.inputTokens ?? null,+ outputTokens: usage?.outputTokens ?? null,+ finishedAt: new Date(),+ }).where(and(eq(agentRun.id, id), eq(agentRun.status, 'pending')));}@@ -207,7 +224,21 @@ export async function scheduleRunRetry(id: number, delayMs: number, error: strinexport async function markRunFailed(id: number, error: string): Promise<void> {await db.update(agentRun)- .set({ status: 'failed', lastError: error.slice(0, 500) })+ .set({ status: 'failed', lastError: error.slice(0, 500), finishedAt: new Date() })+ .where(and(eq(agentRun.id, id), eq(agentRun.status, 'pending')));+}++// Puts a claimed run back in the queue without spending the attempt, for a run the+// team has no free slot for. Waiting for a slot is not a failed attempt, and nothing+// has been recorded on the issue yet, so the run leaves no trace of having been picked+// up at all.+export async function deferRun(id: number, delaySeconds: number): Promise<void> {+ await db+ .update(agentRun)+ .set({+ attempts: sql`${agentRun.attempts} - 1`,+ nextAttemptAt: sql`now() + make_interval(secs => ${delaySeconds})`,+ }).where(and(eq(agentRun.id, id), eq(agentRun.status, 'pending')));}diff --git a/apps/api/src/modules/agents/core/runtime/memory.ts b/apps/api/src/modules/agents/core/runtime/memory.tsindex c29c8d15..e74bc6d5 100644--- a/apps/api/src/modules/agents/core/runtime/memory.ts+++ b/apps/api/src/modules/agents/core/runtime/memory.ts@@ -128,11 +128,10 @@ export async function deleteChatThread(threadId: string, resourceId: string): Prreturn true;}-// Deletes every thread bound to the given agent, project, issue or schedule, with its+// Deletes every thread bound to the given agent, project or schedule, with its// messages, and returns how many were deleted. Called when that binding goes away.export async function deleteThreadsWhere(- binding:- { agentId: number } | { projectId: number } | { issueId: number } | { scheduleId: number },+ binding: { agentId: number } | { projectId: number } | { scheduleId: number },): Promise<number> {const memory = getReadMemory();const { threads } = await memory.listThreads({ filter: { metadata: binding }, perPage: false });diff --git a/apps/api/src/modules/god/__tests__/integration/god-email.test.ts b/apps/api/src/modules/god/__tests__/integration/god-email.test.tsindex 73960faf..02aefdaf 100644--- a/apps/api/src/modules/god/__tests__/integration/god-email.test.ts+++ b/apps/api/src/modules/god/__tests__/integration/god-email.test.ts@@ -1,5 +1,6 @@import { beforeEach, describe, expect, it } from 'bun:test';-import { getEmailConfig, resolveEmailConfig, setEmailSettings } from '@repo/auth';+import { resolveEmailConfig, setEmailSettings } from '@repo/auth';+import { getInstanceEmailConfig } from '@repo/db';import { resetDb } from '#tests/helpers/db';import { addUser, setup } from '../helpers';@@ -43,7 +44,7 @@ describe('god email settings', () => {// Port 1 is deliberately unreachable. Reaching the transport proves the draft// was used instead of being rejected as an unconfigured saved provider.expect(res.status).toBe(502);- expect(await getEmailConfig()).toBeNull();+ expect(await getInstanceEmailConfig()).toBeNull();});it('reuses a saved secret while resolving a draft without changing storage', async () => {@@ -79,7 +80,7 @@ describe('god email settings', () => {username: '[email protected]',password: 'stored-secret',});- expect(await getEmailConfig()).toMatchObject({+ expect(await getInstanceEmailConfig()).toMatchObject({smtp: { host: 'smtp.saved.example', password: 'stored-secret' },from: '[email protected]',});diff --git a/apps/api/src/modules/god/index.ts b/apps/api/src/modules/god/index.tsindex 59891a7e..ad0e7995 100644--- a/apps/api/src/modules/god/index.ts+++ b/apps/api/src/modules/god/index.ts@@ -7,7 +7,6 @@ import {getEmailSettings,resolveEmailConfig,setEmailSettings,- hasConfiguredEmailProvider,getGoogleSettings,setGoogleSettings,hasConfiguredGoogle,@@ -18,6 +17,7 @@ import {setScimSettings,rotateScimToken,} from '@repo/auth';+import { hasConfiguredEmailProvider } from '@repo/db';import { emailBody, hasEmailProvider, sendEmail } from '@repo/mailer';import { authContext } from '#shared/auth-context';import { requireGod } from '#shared/access';diff --git a/apps/api/src/modules/invites/__tests__/integration/invites.test.ts b/apps/api/src/modules/invites/__tests__/integration/invites.test.tsindex 3fd2ff32..9cea6567 100644--- a/apps/api/src/modules/invites/__tests__/integration/invites.test.ts+++ b/apps/api/src/modules/invites/__tests__/integration/invites.test.ts@@ -1,5 +1,5 @@import { describe, it, expect, afterEach, beforeEach } from 'bun:test';-import { app, authedApi } from '#tests/helpers/app';+import { authedApi } from '#tests/helpers/app';import { signUpTestUser, type TestUser } from '#tests/helpers/auth';import { resetDb } from '#tests/helpers/db';import { createRole } from '#tests/helpers/roles';@@ -33,31 +33,6 @@ async function configureEmail(owner: ReturnType<typeof authedApi>) {expect(result.status).toBe(200);}-async function deliverInvite(projectId: number, projectInviteId: number) {- const token = 'invite-email-test-worker-token';- const previousToken = process.env.WORKER_INTERNAL_TOKEN;- process.env.WORKER_INTERNAL_TOKEN = token;- let response: Response;- try {- response = await app.handle(- new Request('http://localhost/internal/notification-deliveries/send', {- method: 'POST',- headers: { 'content-type': 'application/json', 'x-worker-token': token },- body: JSON.stringify({- projectId,- channel: 'email',- recipient: '[email protected]',- payload: { text: 'Invitation', projectInviteId },- }),- }),- );- } finally {- if (previousToken == null) delete process.env.WORKER_INTERNAL_TOKEN;- else process.env.WORKER_INTERNAL_TOKEN = previousToken;- }- return { status: response.status, body: await response.json() };-}-// The id of the team the caller owns — every account is given one at registration.async function ownTeamId(api: ReturnType<typeof authedApi>): Promise<number> {const teams = await api.teams.get();@@ -266,20 +241,6 @@ describe('invites', () => {expect(res.status).toBe(409);});- it('drops a queued delivery after the invite was accepted', async () => {- const owner = await setupOwner();- const invitee = await signUpTestUser();- const invite = await owner.api- .projects({ projectKey: 'MKT' })- .invites.post({ email: invitee.email, role: 'member' });- await authedApi(invitee.cookie).invites({ token: invite.data!.token }).accept.post();-- const result = await deliverInvite(owner.projectId, invite.data!.id);-- expect(result.status).toBe(200);- expect(result.body).toEqual({ ok: true });- });-it('denies a non-member', async () => {const owner = await setupOwner();const invite = await owner.apidiff --git a/apps/api/src/modules/invites/email.ts b/apps/api/src/modules/invites/email.tsindex f91a2095..5d8648c0 100644--- a/apps/api/src/modules/invites/email.ts+++ b/apps/api/src/modules/invites/email.ts@@ -1,6 +1,6 @@import { randomUUID } from 'node:crypto';-import { db, notificationDelivery } from '@repo/db';-import { getEmailConfig, trustedOrigins } from '@repo/auth';+import { db, notificationDelivery, getInstanceEmailConfig } from '@repo/db';+import { trustedOrigins } from '@repo/auth';import { hasEmailProvider } from '@repo/mailer';import { and, eq, sql } from 'drizzle-orm';import type { InviteRow } from './service';@@ -19,7 +19,7 @@ export async function enqueueInviteEmail(project: InviteProject,invite: InviteRow,): Promise<boolean> {- const config = await getEmailConfig();+ const config = await getInstanceEmailConfig();if (!config || !hasEmailProvider(config)) return false;const dedupeKey = `project-invite:${invite.id}`;diff --git a/apps/api/src/modules/invites/service.ts b/apps/api/src/modules/invites/service.tsindex 5b3a7e33..7a95e654 100644--- a/apps/api/src/modules/invites/service.ts+++ b/apps/api/src/modules/invites/service.ts@@ -240,22 +240,6 @@ export async function createInvite(input: NewInvite): Promise<InviteRow> {return (await getInviteById(row.id))!;}-// Whether a queued invite email still has a live invite behind it.-export async function isInvitePending(projectId: number, id: number): Promise<boolean> {- const rows = await db- .select({ id: teamInvite.id })- .from(teamInvite)- .where(- and(- eq(teamInvite.projectId, projectId),- eq(teamInvite.id, id),- eq(teamInvite.status, 'pending'),- ),- )- .limit(1);- return rows.length > 0;-}-export async function getInviteById(id: number): Promise<InviteRow | null> {const rows = await selectInvites().where(eq(teamInvite.id, id));return rows[0] ? toRow(rows[0]) : null;diff --git a/apps/api/src/modules/issues/__tests__/integration/auto-archive.test.ts b/apps/api/src/modules/issues/__tests__/integration/auto-archive.test.tsnew file mode 100644index 00000000..5b0cc2c2--- /dev/null+++ b/apps/api/src/modules/issues/__tests__/integration/auto-archive.test.ts@@ -0,0 +1,75 @@+import { describe, it, expect, beforeEach } from 'bun:test';+import { db, issue } from '@repo/db';+import { eq, sql } from 'drizzle-orm';+import { authedApi } from '#tests/helpers/app';+import { signUpTestUser } from '#tests/helpers/auth';+import { resetDb } from '#tests/helpers/db';+import { sweepStaleIssues } from '../../auto-archive';++// The sweep the api runs in the background. Inactivity is the whole point of it, and+// no route can move an issue's updated_at into the past, so that one field is set+// directly; everything else goes through the API.++async function setup() {+ const owner = await signUpTestUser({ name: 'Owner' });+ const api = authedApi(owner.cookie);+ await api.projects.post({ key: 'MKT', name: 'Marketing' });+ const columns = (await api.projects({ projectKey: 'MKT' }).get()).data!.columns;+ const done = columns.find((c) => c.stateType === 'completed')!;+ const todo = columns.find((c) => c.stateType === 'unstarted')!;+ await api.projects({ projectKey: 'MKT' }).settings['auto-archive'].patch({+ completedDays: 14,+ canceledDays: null,+ });+ return { api, doneId: done.id, todoId: todo.id };+}++async function addIssue(api: ReturnType<typeof authedApi>, columnId: number, title: string) {+ const created = (await api.projects({ projectKey: 'MKT' }).issues.post({ columnId, title }))+ .data!;+ return created.id;+}++function backdate(issueId: number, days: number) {+ return db+ .update(issue)+ .set({ updatedAt: sql`now() - make_interval(days => ${days})` })+ .where(eq(issue.id, issueId));+}++describe('auto-archive sweep', () => {+ beforeEach(resetDb);++ it('archives an issue idle past the threshold and logs it', async () => {+ const { api, doneId } = await setup();+ const issueId = await addIssue(api, doneId, 'Shipped');+ await backdate(issueId, 20);++ expect(await sweepStaleIssues()).toBe(1);++ expect((await api.issues({ issueId }).get()).data!.archivedAt).not.toBeNull();+ const feed = (await api.issues({ issueId }).feed.get()).data!;+ expect(feed.items.map((entry) => entry.action)).toContain('archived');+ });++ it('leaves an issue that is still active, recently touched, or in a live column', async () => {+ const { api, doneId, todoId } = await setup();+ const recent = await addIssue(api, doneId, 'Just done');+ const open = await addIssue(api, todoId, 'In progress');+ await backdate(open, 20);++ expect(await sweepStaleIssues()).toBe(0);++ expect((await api.issues({ issueId: recent }).get()).data!.archivedAt).toBeNull();+ expect((await api.issues({ issueId: open }).get()).data!.archivedAt).toBeNull();+ });++ it('does nothing on a second pass over the issues it already archived', async () => {+ const { api, doneId } = await setup();+ const issueId = await addIssue(api, doneId, 'Shipped');+ await backdate(issueId, 20);+ await sweepStaleIssues();++ expect(await sweepStaleIssues()).toBe(0);+ });+});diff --git a/apps/api/src/modules/issues/auto-archive.ts b/apps/api/src/modules/issues/auto-archive.tsnew file mode 100644index 00000000..8f5138a8--- /dev/null+++ b/apps/api/src/modules/issues/auto-archive.ts@@ -0,0 +1,37 @@+import { db } from '@repo/db';+import { sql } from 'drizzle-orm';+import { recordActivityForIssues } from './activity';++// Archives active issues that have sat inactive in a completed/canceled column past+// their project's threshold. Inactivity is measured by issue.updated_at: moving to a+// terminal column bumps it, and any later edit resets the clock, so an issue is+// archived only after the full period with no activity.+export async function sweepStaleIssues(): Promise<number> {+ // The threshold lives in project_setting under key 'auto_archive' as+ // { completedDays, canceledDays } (kept in sync with getAutoArchiveSettings in+ // modules/projects/service.ts); a positive day count enables archiving for that state+ // group, null/absent disables it. The numeric-string regex guards the cast, so a+ // malformed value is treated as disabled rather than raising.+ const rows = await db.execute(sql`+ UPDATE issue i+ SET archived_at = now()+ FROM project_column c, project_setting s+ WHERE i.column_id = c.id+ AND i.project_id = s.project_id+ AND s.key = 'auto_archive'+ AND i.archived_at IS NULL+ AND (+ (c.state_type = 'completed'+ AND (s.value->>'completedDays') ~ '^[0-9]{1,4}$'+ AND i.updated_at < now() - make_interval(days => (s.value->>'completedDays')::int))+ OR+ (c.state_type = 'canceled'+ AND (s.value->>'canceledDays') ~ '^[0-9]{1,4}$'+ AND i.updated_at < now() - make_interval(days => (s.value->>'canceledDays')::int))+ )+ RETURNING i.id+ `);+ const archived = (rows as unknown as Array<{ id: number }>).map((row) => row.id);+ await recordActivityForIssues(archived, { action: 'archived' }, { system: 'Auto-archive' });+ return archived.length;+}diff --git a/apps/api/src/modules/issues/service.ts b/apps/api/src/modules/issues/service.tsindex 1bb342e3..7d536f9b 100644--- a/apps/api/src/modules/issues/service.ts+++ b/apps/api/src/modules/issues/service.ts@@ -65,7 +65,6 @@ import {getFieldTriggerAgent,isProjectAgent,} from '#modules/agents/core/service';-import { deleteThreadsWhere } from '#modules/agents/core/runtime/memory';import { getInitiativeProjectId } from '#modules/initiatives/service';import { cycleStatus, getCycleRef, type CycleStatus } from '#modules/cycles/service';import { getMembership } from '#modules/members/service';@@ -437,10 +436,8 @@ export async function searchIssues(// Archives an issue: sets archived_at so it drops off the board and lists, keeping// the row for restore. Idempotent (archiving an archived issue is a no-op re-stamp// avoided by the archived_at guard). Records a feed entry. Returns the updated-// issue, or null if it does not exist.-//-// The agents' conversation threads for the issue are deleted with it. Archiving is-// reversible and this is not: a restored issue starts with empty agent memory.+// issue, or null if it does not exist. Archiving is reversible, so nothing the issue+// carries is dropped — the agents' conversation threads included.export async function archiveIssue(id: number,actorUserId?: string | null,@@ -455,7 +452,6 @@ export async function archiveIssue(return getIssue(id);}await recordActivity(id, [{ action: 'archived' }], actorUserId);- await deleteThreadsWhere({ issueId: id });return getIssue(id);}diff --git a/apps/api/src/modules/notification-settings/index.ts b/apps/api/src/modules/notification-settings/index.tsindex 0a4f1ad9..e91a61d6 100644--- a/apps/api/src/modules/notification-settings/index.ts+++ b/apps/api/src/modules/notification-settings/index.ts@@ -2,7 +2,7 @@ import { Elysia } from 'elysia';import { guards } from '#shared/guards';import { authContext } from '#shared/auth-context';import { errors } from '#shared/responses';-import { getProjectEmailConfig } from '@repo/auth';+import { getProjectEmailConfig } from '@repo/db';import { teamParams } from '#modules/teams/model';import { NotificationSettingsBody, NotificationSettingsResponse } from './model';import {diff --git a/apps/api/src/modules/notification-settings/service.ts b/apps/api/src/modules/notification-settings/service.tsindex 6a707207..53a60f6e 100644--- a/apps/api/src/modules/notification-settings/service.ts+++ b/apps/api/src/modules/notification-settings/service.ts@@ -1,53 +1,28 @@-import { db, teamNotificationSetting } from '@repo/db';+import {+ db,+ teamNotificationSetting,+ defaultNotificationConfig,+ readNotificationConfig,+ type NotificationConfig,+} from '@repo/db';import { eq, sql } from 'drizzle-orm';-import { encryptSecret, decryptSecret } from '@repo/crypto';-import type { SmtpConfig, ResendConfig } from '@repo/mailer';+import { encryptSecret } from '@repo/crypto';import { HttpError } from '#shared/lib';-// Data access for a team's notification provider credentials: the outbound channels-// every project of the team delivers through (SMTP or Resend for email, a Telegram-// bot). One row per team, managed by its owner. The full config carries secrets, so-// it is stored encrypted as one JSON blob; the `redacted` column holds the same-// config with secret values dropped and replaced by `hasX` flags, for the settings-// UI. The plaintext config is only read by the delivery sender; it is never returned-// over HTTP. Which events reach a given member, and their Telegram chat id, are a-// per-user choice held in notification-preferences, not here.+// The settings UI over a team's notification provider credentials: the outbound+// channels every project of the team delivers through (SMTP or Resend for email, a+// Telegram bot). One row per team, managed by its owner. The stored shape and the+// decrypting reader live in @repo/db (the worker sends with them); what is here is+// the redacted view the UI reads, the partial write, and the validation. The+// plaintext config is never returned over HTTP. Which events reach a given member,+// and their Telegram chat id, are a per-user choice held in+// notification-preferences, not here.// SMTP transport encryption. 'none' is plain (STARTTLS is negotiated by the// sender when offered); 'ssl' is implicit TLS; 'tls' forces STARTTLS.export const ENCRYPTION_MODES = ['none', 'ssl', 'tls'] as const;export type EncryptionMode = (typeof ENCRYPTION_MODES)[number];-interface TelegramConfig {- enabled: boolean;- botToken: string; // secret-}--// The stored, decrypted config. Secret fields carry the plaintext value. Read by-// the delivery sender through getDeliveryConfig; never returned over HTTP.-export interface NotificationConfig {- // Send email through the instance provider instead of the team's own. Carries- // no credentials: they belong to the instance and are read at send time. A team- // that enables SMTP or Resend of its own takes precedence over this.- system: { enabled: boolean };- smtp: SmtpConfig;- resend: ResendConfig;- telegram: TelegramConfig;-}--// Which provider sends this team's email: its own SMTP/Resend when one is-// enabled, otherwise the instance provider when the team asked for it. 'none'-// means email delivery is off for the team.-export function emailSource(config: {- system: { enabled: boolean };- smtp: { enabled: boolean };- resend: { enabled: boolean };-}): 'smtp' | 'resend' | 'system' | 'none' {- if (config.smtp.enabled) return 'smtp';- if (config.resend.enabled) return 'resend';- return config.system.enabled ? 'system' : 'none';-}-// The config as returned to the client: every secret replaced by a boolean// telling whether a value is stored. Non-secret fields are verbatim.export interface NotificationSettingsDto {@@ -84,25 +59,6 @@ export interface NotificationSettingsPatch {telegram?: { enabled: boolean; botToken?: string };}-function defaultConfig(): NotificationConfig {- return {- // A team sends through the instance provider until it configures its own, so- // notifications work out of the box wherever the instance shares one.- system: { enabled: true },- smtp: {- enabled: false,- host: '',- port: 587,- encryption: 'none',- username: '',- password: '',- timeout: null,- },- resend: { enabled: false, apiKey: '' },- telegram: { enabled: false, botToken: '' },- };-}-function toDto(config: NotificationConfig): NotificationSettingsDto {return {system: { enabled: config.system.enabled },@@ -185,26 +141,10 @@ function assertSendable(config: NotificationConfig): void {}}-// Reads and decrypts the stored config, or null when the team has none yet.-async function readConfig(teamId: number): Promise<NotificationConfig | null> {- const rows = await db- .select({- ciphertext: teamNotificationSetting.ciphertext,- iv: teamNotificationSetting.iv,- authTag: teamNotificationSetting.authTag,- })- .from(teamNotificationSetting)- .where(eq(teamNotificationSetting.teamId, teamId));- const row = rows[0];- if (!row) return null;- // Merge over the default so a config written before a field was added stays valid.- return { ...defaultConfig(), ...(JSON.parse(decryptSecret(row)) as NotificationConfig) };-}-// The redacted settings for a team. Defaults (no secrets, no provider of its own,// delivery through the instance provider) when nothing has been saved.export async function getNotificationSettings(teamId: number): Promise<NotificationSettingsDto> {- const config = (await readConfig(teamId)) ?? defaultConfig();+ const config = (await readNotificationConfig(teamId)) ?? defaultNotificationConfig();return toDto(config);}@@ -213,7 +153,7 @@ export async function setNotificationSettings(teamId: number,patch: NotificationSettingsPatch,): Promise<NotificationSettingsDto> {- const current = (await readConfig(teamId)) ?? defaultConfig();+ const current = (await readNotificationConfig(teamId)) ?? defaultNotificationConfig();const next = applyPatch(current, patch);assertSendable(next);const redacted = toDto(next);@@ -250,13 +190,7 @@ export async function readRedactedSettings(teamId: number): Promise<Notification.from(teamNotificationSetting).where(eq(teamNotificationSetting.teamId, teamId));const redacted = rows[0]?.redacted as NotificationSettingsDto | undefined;- // Merge over the default, as readConfig does, so a row written before a field was- // added stays valid.- return { ...toDto(defaultConfig()), ...(redacted ?? {}) };-}--// The full decrypted config, for the sender that actually delivers a notification.-// Carries secrets, so it is only called server-side by the delivery sender.-export async function getDeliveryConfig(teamId: number): Promise<NotificationConfig> {- return (await readConfig(teamId)) ?? defaultConfig();+ // Merge over the default, as the stored reader does, so a row written before a+ // field was added stays valid.+ return { ...toDto(defaultNotificationConfig()), ...(redacted ?? {}) };}diff --git a/apps/api/src/modules/notifications/internal-routes.ts b/apps/api/src/modules/notifications/internal-routes.tsdeleted file mode 100644index 24ea37dd..00000000--- a/apps/api/src/modules/notifications/internal-routes.ts+++ /dev/null@@ -1,54 +0,0 @@-import { Elysia } from 'elysia';-import { isInvitePending } from '#modules/invites/service';-import { getDeliveryConfig } from '#modules/notification-settings/service';-import { getProjectById } from '#modules/projects/service';-import { sendDeliveryBody } from './model';-import { sendDelivery } from './send';--// Internal endpoint the worker calls to deliver one claimed notification_delivery-// row. The channel credentials are encrypted at rest, so the send runs here (in the-// API, which owns the encryption key and the config store) rather than in the worker, mirroring /internal/agent-runs/execute. Authenticated with the-// shared WORKER_INTERNAL_TOKEN. Returns the SendResult so the worker records the-// outcome and decides whether to retry.-export const internalNotificationRoutes = new Elysia({- name: 'internal-notification-deliveries',- detail: { tags: ['Internal'] },-}).post(- '/internal/notification-deliveries/send',- async ({ body, headers, set }) => {- const expected = process.env.WORKER_INTERNAL_TOKEN;- if (!expected || headers['x-worker-token'] !== expected) {- set.status = 401;- return { ok: false, retryable: false, error: 'Unauthorized' };- }- if (- body.payload.projectInviteId != null &&- !(await isInvitePending(body.projectId, body.payload.projectInviteId))- ) {- // The invite was accepted, rejected, or revoked while its email waited in- // the outbox. Treat it as delivered so the worker removes the stale row.- return { ok: true };- }- // The row names the project it came from; the credentials belong to the team- // that owns it.- const project = await getProjectById(body.projectId);- if (!project) return { ok: false, retryable: false, error: 'Project not found' };- const config = await getDeliveryConfig(project.teamId);- return sendDelivery({- channel: body.channel,- recipient: body.recipient,- payload: body.payload,- config,- });- },- {- body: sendDeliveryBody,- detail: {- summary: 'Send one notification delivery',- description:- 'Send a claimed delivery over its channel with the stored credentials of the team that ' +- 'owns its project, and return the result the worker records. Called by the worker with ' +- 'the x-worker-token header.',- },- },-);diff --git a/apps/api/src/modules/notifications/model.ts b/apps/api/src/modules/notifications/model.tsindex 5e981ac1..ec3b6f5f 100644--- a/apps/api/src/modules/notifications/model.ts+++ b/apps/api/src/modules/notifications/model.ts@@ -59,21 +59,3 @@ export const deleteNotificationsQuery = t.Object({export const setNotificationReadBody = t.Optional(t.Object({ read: t.Optional(t.Boolean()) }));export const snoozeNotificationBody = t.Object({ until: t.Nullable(t.String()) });--export const sendDeliveryBody = t.Object({- projectId: t.Number(),- channel: t.UnionEnum(['email', 'telegram']),- recipient: t.Nullable(t.String()),- payload: t.Object({- subject: t.Optional(t.String()),- text: t.String(),- // The Telegram body. Elysia strips fields the schema does not declare, so- // leaving it out here would silently drop the formatted message and send the- // plain-text fallback instead.- html: t.Optional(t.String()),- url: t.Optional(t.String()),- emailSource: t.Optional(t.UnionEnum(['project', 'instance'])),- idempotencyKey: t.Optional(t.String()),- projectInviteId: t.Optional(t.Integer({ minimum: 1 })),- }),-});diff --git a/apps/api/src/modules/notifications/outbound.ts b/apps/api/src/modules/notifications/outbound.tsindex a364abfd..be0665c6 100644--- a/apps/api/src/modules/notifications/outbound.ts+++ b/apps/api/src/modules/notifications/outbound.ts@@ -1,7 +1,16 @@-import { db, notificationDelivery, issue, issueActivity, project, user } from '@repo/db';+import {+ db,+ notificationDelivery,+ issue,+ issueActivity,+ project,+ user,+ emailSource,+ getProjectEmailConfig,+ type DeliveryPayload,+} from '@repo/db';import { eq, inArray } from 'drizzle-orm';-import { getProjectEmailConfig } from '@repo/auth';-import { emailSource, readRedactedSettings } from '#modules/notification-settings/service';+import { readRedactedSettings } from '#modules/notification-settings/service';import { getPreferencesForUsers } from '#modules/notification-preferences/service';import { getTelegramChatIds, hasUsableInstanceBot } from '#modules/telegram/service';import { escapeHtml } from '#shared/lib';@@ -20,20 +29,6 @@ import type { NotificationType, NewNotificationRow } from './service';// This is best-effort: enqueue never throws into the caller (a failure here must not// break creating a comment or updating an issue), so callers wrap it in try/catch.-// The stored message. `subject`/`html` are channel-specific: email uses `subject`-// and builds its own HTML from `text`; Telegram sends `html` (parse_mode HTML) and-// falls back to `text`. The sender appends `url` to plain-text bodies.-export interface DeliveryPayload {- subject?: string;- text: string;- html?: string;- url?: string;- emailSource?: 'project' | 'instance';- idempotencyKey?: string;- dedupeKey?: string;- projectInviteId?: number;-}-interface OutboxRow {projectId: number;channel: 'email' | 'telegram';diff --git a/apps/api/src/modules/projects/service.ts b/apps/api/src/modules/projects/service.tsindex ca91e223..b1bcddde 100644--- a/apps/api/src/modules/projects/service.ts+++ b/apps/api/src/modules/projects/service.ts@@ -455,12 +455,12 @@ export async function setEstimateSettings(// Auto-archive thresholds for a project. Stored in project_setting under// AUTO_ARCHIVE_KEY as { completedDays, canceledDays }. Each value is the number of-// days an issue may sit inactive in a completed/canceled column before the worker+// days an issue may sit inactive in a completed/canceled column before the sweep// archives it; null disables archiving for that state group. A new project is// created with DEFAULT_AUTO_ARCHIVE; a project with no stored row (created before// this) keeps both null, so nothing is archived until an owner turns it on. The-// worker reads the same key and jsonb fields directly (apps/worker/src/store.ts) —-// keep them in sync.+// sweep reads the same key and jsonb fields directly (modules/issues/auto-archive.ts)+// — keep them in sync.const AUTO_ARCHIVE_KEY = 'auto_archive';const DEFAULT_AUTO_ARCHIVE = { completedDays: 28, canceledDays: 7 };diff --git a/apps/api/src/modules/telegram/__tests__/integration/telegram.test.ts b/apps/api/src/modules/telegram/__tests__/integration/telegram.test.tsnew file mode 100644index 00000000..94b0c0bc--- /dev/null+++ b/apps/api/src/modules/telegram/__tests__/integration/telegram.test.ts@@ -0,0 +1,43 @@+import { describe, it, expect, beforeEach } from 'bun:test';+import { writeSecret, getInstanceBotConfig, isInstanceBotUsable } from '@repo/db';+import { app } from '#tests/helpers/app';+import { resetDb } from '#tests/helpers/db';++// The bot token is stored by the api and read back by the bot service itself: no route+// hands it out, and the bot holds no api credential. These cover that seam — the row+// the api writes under the key the bot reads, and the absence of a route that would+// return it.++async function storeBot(botToken: string, enabled = true): Promise<void> {+ await writeSecret(+ 'telegram.bot',+ { enabled, botToken, botUsername: 'itsaplan_bot' },+ { enabled, botUsername: 'itsaplan_bot', hasBotToken: botToken.length > 0 },+ );+}++describe('telegram', () => {+ beforeEach(async () => {+ await resetDb();+ });++ it('has no route that returns the bot token', async () => {+ await storeBot('123:secret');+ const res = await app.handle(new Request('http://localhost/internal/telegram/config'));+ expect(res.status).toBe(404);+ expect(await res.text()).not.toContain('123:secret');+ });++ it('reads back the stored bot config', async () => {+ expect(isInstanceBotUsable(await getInstanceBotConfig())).toBe(false);+ await storeBot('123:secret');+ const config = await getInstanceBotConfig();+ expect(config).toEqual({ enabled: true, botToken: '123:secret', botUsername: 'itsaplan_bot' });+ expect(isInstanceBotUsable(config)).toBe(true);+ });++ it('reports a disabled bot as unusable', async () => {+ await storeBot('123:secret', false);+ expect(isInstanceBotUsable(await getInstanceBotConfig())).toBe(false);+ });+});diff --git a/apps/api/src/modules/telegram/index.ts b/apps/api/src/modules/telegram/index.tsindex fa2f38e1..9d057d90 100644--- a/apps/api/src/modules/telegram/index.ts+++ b/apps/api/src/modules/telegram/index.ts@@ -5,13 +5,8 @@ import { HttpError } from '#shared/lib';import { errors } from '#shared/responses';import { noContent } from '#shared/http';import { TelegramAccountResponse, TelegramLinkStartResponse } from './model';-import {- getInstanceBotConfig,- getTelegramLink,- isInstanceBotUsable,- startTelegramLink,- unlinkTelegram,-} from './service';+import { getInstanceBotConfig, isInstanceBotUsable } from '@repo/db';+import { getTelegramLink, startTelegramLink, unlinkTelegram } from './service';// The session user's own Telegram account link. Linking runs through the instance// bot: this mints a one-time code and returns the deep link that opens the bot withdiff --git a/apps/api/src/modules/telegram/internal-routes.ts b/apps/api/src/modules/telegram/internal-routes.tsdeleted file mode 100644index e5a57b66..00000000--- a/apps/api/src/modules/telegram/internal-routes.ts+++ /dev/null@@ -1,64 +0,0 @@-import { Elysia } from 'elysia';-import { confirmLinkBody } from './model';-import { confirmTelegramLink, getInstanceBotConfig, isInstanceBotUsable } from './service';--// Internal endpoints the bot service calls. The bot holds no database connection and-// no encryption key: it asks for its token here and posts back the `/start` codes it-// receives, mirroring how the worker delivers notifications through-// /internal/notification-deliveries/send. Authenticated with the shared-// WORKER_INTERNAL_TOKEN.-//-// The config endpoint returns the bot token in plaintext. It is reachable only with-// that token, on the internal network, and the bot cannot work without it.--function authorized(headers: Record<string, string | undefined>): boolean {- const expected = process.env.WORKER_INTERNAL_TOKEN;- return Boolean(expected) && headers['x-worker-token'] === expected;-}--export const internalTelegramRoutes = new Elysia({- name: 'internal-telegram',- detail: { tags: ['Internal'] },-})- .get(- '/internal/telegram/config',- async ({ headers, set }) => {- if (!authorized(headers)) {- set.status = 401;- return { enabled: false, botToken: '', botUsername: '' };- }- const config = await getInstanceBotConfig();- // A disabled or tokenless bot is reported as not enabled, so the service simply- // stops polling instead of having to interpret the config itself.- if (!isInstanceBotUsable(config)) return { enabled: false, botToken: '', botUsername: '' };- return { enabled: true, botToken: config.botToken, botUsername: config.botUsername };- },- {- detail: {- summary: "Get the instance's Telegram bot config",- description:- 'Return the bot token and username the bot service polls with, or `enabled: false` ' +- 'when no usable bot is configured. Called by the bot with the x-worker-token header.',- },- },- )-- .post(- '/internal/telegram/link',- async ({ body, headers, set }) => {- if (!authorized(headers)) {- set.status = 401;- return { ok: false as const, reason: 'invalid' as const };- }- return confirmTelegramLink(body);- },- {- body: confirmLinkBody,- detail: {- summary: 'Confirm a Telegram link code',- description:- "Redeem the code a user sent the bot with /start and attach the chat to that user's " +- 'account. Called by the bot with the x-worker-token header.',- },- },- );diff --git a/apps/api/src/modules/telegram/model.ts b/apps/api/src/modules/telegram/model.tsindex ac1f7f0c..9171ff50 100644--- a/apps/api/src/modules/telegram/model.ts+++ b/apps/api/src/modules/telegram/model.ts@@ -15,10 +15,3 @@ export const TelegramLinkStartResponse = t.Object({url: t.String(),expiresAt: t.String(),});--export const confirmLinkBody = t.Object({- code: t.String({ minLength: 1, maxLength: 64 }),- chatId: t.String({ minLength: 1, maxLength: 128 }),- username: t.Nullable(t.String()),- firstName: t.Nullable(t.String()),-});diff --git a/apps/api/src/modules/telegram/service.ts b/apps/api/src/modules/telegram/service.tsindex 52436668..50167dcc 100644--- a/apps/api/src/modules/telegram/service.ts+++ b/apps/api/src/modules/telegram/service.ts@@ -1,6 +1,13 @@-import { db, appSecret, userTelegramAccount } from '@repo/db';-import { and, eq, gt, inArray, ne, sql } from 'drizzle-orm';-import { encryptSecret, decryptSecret } from '@repo/crypto';+import {+ db,+ userTelegramAccount,+ writeSecret,+ getInstanceBotConfig,+ isInstanceBotUsable,+ TELEGRAM_BOT_SECRET_KEY,+ type InstanceBotConfig,+} from '@repo/db';+import { eq, inArray } from 'drizzle-orm';import { randomBytes } from 'node:crypto';// The instance Telegram bot and the account links it creates.@@ -9,27 +16,19 @@ import { randomBytes } from 'node:crypto';// Telegram account, and the default sender for Telegram notifications (a project may// still set its own bot token, which wins for that project's deliveries). The token// is a secret, so it lives encrypted in app_secret under 'telegram.bot' with a-// `redacted` mirror for the settings UI, the same shape as the instance mail and-// Google credentials in @repo/auth.+// `redacted` mirror for the settings UI. The stored shape and its reader are in+// @repo/db, shared with the bot and the worker; what is here is the write side.//// A link is one row in user_telegram_account per user: created with a one-time-// link_code when the user asks to link, completed by the bot when that code arrives-// as `/start <code>`. chat_id null means the link is still pending.--const BOT_SECRET_KEY = 'telegram.bot';+// link_code here, redeemed by the bot when that code arrives as `/start <code>`.+// chat_id null means the link is still pending.// How long a `/start` code stays valid. Long enough to switch to Telegram and press// the button, short enough that an intercepted link is not useful later.const LINK_CODE_TTL_MINUTES = 15;-// The stored, decrypted bot config. Read by the delivery sender and handed to the-// bot service over the internal API; never returned to a browser.-export interface InstanceBotConfig {- enabled: boolean;- botToken: string; // secret- // Resolved from getMe when the token is saved, so the deep link can be built- // without asking the administrator to type the name a second time.- botUsername: string;+export async function hasUsableInstanceBot(): Promise<boolean> {+ return isInstanceBotUsable(await getInstanceBotConfig());}// The config as returned to the client: the token replaced by a boolean telling@@ -47,10 +46,6 @@ export interface InstanceBotPatch {botToken?: string;}-function defaultBotConfig(): InstanceBotConfig {- return { enabled: false, botToken: '', botUsername: '' };-}-function toBotDto(config: InstanceBotConfig): InstanceBotDto {return {enabled: config.enabled,@@ -59,31 +54,10 @@ function toBotDto(config: InstanceBotConfig): InstanceBotDto {};}-export async function getInstanceBotConfig(): Promise<InstanceBotConfig> {- const rows = await db- .select({ ciphertext: appSecret.ciphertext, iv: appSecret.iv, authTag: appSecret.authTag })- .from(appSecret)- .where(eq(appSecret.key, BOT_SECRET_KEY));- const row = rows[0];- if (!row) return defaultBotConfig();- // Merge over the default so a config written before a field was added stays valid.- return { ...defaultBotConfig(), ...(JSON.parse(decryptSecret(row)) as InstanceBotConfig) };-}-export async function getInstanceBotSettings(): Promise<InstanceBotDto> {return toBotDto(await getInstanceBotConfig());}-// Whether the instance bot can be used right now. Account linking is offered only-// when this is true, and Telegram delivery falls back to this bot only when it is.-export function isInstanceBotUsable(config: InstanceBotConfig): boolean {- return config.enabled && config.botToken.length > 0;-}--export async function hasUsableInstanceBot(): Promise<boolean> {- return isInstanceBotUsable(await getInstanceBotConfig());-}-// Asks Telegram who the token belongs to. Doubles as validation: a bad token is// rejected before it is stored, so the administrator finds out at save time rather// than from silently undelivered notifications.@@ -120,27 +94,8 @@ export async function setInstanceBotSettings(patch: InstanceBotPatch): Promise<IbotToken,botUsername: needsLookup ? await fetchBotUsername(botToken) : current.botUsername,};- const enc = encryptSecret(JSON.stringify(next));const redacted = toBotDto(next);- await db- .insert(appSecret)- .values({- key: BOT_SECRET_KEY,- ciphertext: enc.ciphertext,- iv: enc.iv,- authTag: enc.authTag,- redacted,- })- .onConflictDoUpdate({- target: appSecret.key,- set: {- ciphertext: enc.ciphertext,- iv: enc.iv,- authTag: enc.authTag,- redacted,- updatedAt: sql`now()`,- },- });+ await writeSecret(TELEGRAM_BOT_SECRET_KEY, next, redacted);return redacted;}@@ -213,55 +168,3 @@ export async function startTelegramLink(export async function unlinkTelegram(userId: string): Promise<void> {await db.delete(userTelegramAccount).where(eq(userTelegramAccount.userId, userId));}--export interface ConfirmLinkInput {- code: string;- chatId: string;- username: string | null;- firstName: string | null;-}--export type ConfirmLinkResult =- { ok: true; userId: string } | { ok: false; reason: 'invalid' | 'taken' };--// Completes a link from the bot: matches the code, then writes the chat id onto that-// user's row and clears the code so it cannot be replayed. 'invalid' covers an-// unknown, already-used, or expired code — the bot tells the user to start again-// either way. 'taken' means this Telegram account is already linked to someone else.-export async function confirmTelegramLink(input: ConfirmLinkInput): Promise<ConfirmLinkResult> {- const rows = await db- .select({ userId: userTelegramAccount.userId })- .from(userTelegramAccount)- .where(- and(- eq(userTelegramAccount.linkCode, input.code),- gt(userTelegramAccount.linkCodeExpiresAt, new Date()),- ),- );- const pending = rows[0];- if (!pending) return { ok: false, reason: 'invalid' };-- const conflict = await db- .select({ userId: userTelegramAccount.userId })- .from(userTelegramAccount)- .where(- and(- eq(userTelegramAccount.chatId, input.chatId),- ne(userTelegramAccount.userId, pending.userId),- ),- );- if (conflict.length > 0) return { ok: false, reason: 'taken' };-- await db- .update(userTelegramAccount)- .set({- chatId: input.chatId,- username: input.username,- firstName: input.firstName,- linkedAt: new Date(),- linkCode: null,- linkCodeExpiresAt: null,- })- .where(eq(userTelegramAccount.userId, pending.userId));- return { ok: true, userId: pending.userId };-}diff --git a/apps/api/src/openapi.ts b/apps/api/src/openapi.tsindex 215f312c..f6cb7a94 100644--- a/apps/api/src/openapi.ts+++ b/apps/api/src/openapi.ts@@ -101,10 +101,6 @@ function normalizeOperationSecurity(operation: Operation, path: string, method:operation.security = [{ scimBearer: [] }];return;}- if (path.startsWith('/internal/')) {- operation.security = [{ workerToken: [] }];- return;- }if (path === '/webhooks/git/{webhookId}' || path === '/webhooks/github/{webhookId}') {operation.security = GIT_WEBHOOK_SECURITY;return;diff --git a/apps/api/src/shared/lib.ts b/apps/api/src/shared/lib.tsindex 4bceea35..909dc69f 100644--- a/apps/api/src/shared/lib.ts+++ b/apps/api/src/shared/lib.ts@@ -65,3 +65,12 @@ export function rethrowDuplicate(err: unknown, what: string): never {}throw err;}++// Reads a positive integer from an environment variable, falling back to the default+// when unset, empty, or not a positive finite number.+export function intEnv(name: string, fallback: number): number {+ const raw = process.env[name];+ if (raw == null || raw === '') return fallback;+ const n = Number(raw);+ return Number.isFinite(n) && n > 0 ? n : fallback;+}diff --git a/apps/bot/AGENTS.md b/apps/bot/AGENTS.mdindex 0dbc0bb5..479bf13f 100644--- a/apps/bot/AGENTS.md+++ b/apps/bot/AGENTS.md@@ -6,32 +6,39 @@ root `AGENTS.md`.## Invariants-- **Never import `@repo/db`.** Unlike the worker, this service has no database- connection and no encryption key. Everything goes through the api's `/internal/telegram/*`- routes with `WORKER_INTERNAL_TOKEN`. Keep it that way — the bot token and the user- rows stay behind the api.+- **The bot talks to Postgres and Telegram, and to nothing else.** Its queries are in+ `src/db.ts`, over the schema of `@repo/db`, so it needs `DATABASE_URL` and+ `APP_ENCRYPTION_KEY`. It holds no api credential and makes no call to the api — a+ secret that never crosses the network cannot be read off it.+- **The bot settings row is the api's.** The api writes `telegram.bot` in `app_secret`;+ the bot reads it through `@repo/db` (`domains/telegram-bot.ts`), which is also where+ a new field on that config goes.- **One replica only.** Telegram gives each `getUpdates` call to a single caller, soa second instance would steal updates from the first. Do not add replicas or run italongside a webhook registration for the same bot.- **The token is not env configuration.** It is stored in the database and edited in- god mode, so `supervisor.ts` polls the api and starts/stops/replaces the bot when it+ god mode, so `supervisor.ts` polls it and starts/stops/replaces the bot when itchanges. A new bot must work without a redeploy.- **An error must not stop polling.** `bot.catch` swallows update failures and the- supervisor loop tolerates the api being unreachable.+ supervisor loop tolerates the database being unreachable.## Config-`WORKER_INTERNAL_TOKEN` is required. The api origin is not a variable of its own —-it resolves the same way the worker resolves it: `SERVICE_URL_API` in the compose-stack, `API_URL` locally. Do not add a third URL variable. Optional tuning:-`BOT_CONFIG_POLL_INTERVAL_MS`, `BOT_API_TIMEOUT_MS` (see `src/config.ts`).+`DATABASE_URL` and `APP_ENCRYPTION_KEY` are required — the same values the api uses.+Optional tuning: `BOT_CONFIG_POLL_INTERVAL_MS`, `BOT_CONFIG_RETRY_INTERVAL_MS` (see+`src/config.ts`).## Growing itgrammY ships `webhookCallback(bot, 'elysia')`, so moving from polling to a webhookmounted on the api is a swap of the transport, not of the framework. Commands beyond-`/start` go in `bot.ts`; anything needing project data gets a new `/internal/telegram/*`-route rather than a database import.+`/start` go in `bot.ts`, the queries they need in `db.ts`.++## Tests++`src/db.test.ts` covers redeeming a `/start` code against the test database+(`bun run test` loads `.env.test`; the Docker gate runs it after the api suite). It+inserts the pending rows the api mints in production — there is no api to call.## Rundiff --git a/apps/bot/Dockerfile b/apps/bot/Dockerfileindex 3899c15f..fdf67e50 100644--- a/apps/bot/Dockerfile+++ b/apps/bot/Dockerfile@@ -33,6 +33,6 @@ ENV NODE_ENV=productionCOPY --from=install /app ./COPY . .-# No database access and no migrations: the bot reads its token and writes account-# links through the api's internal routes, so it only needs the api to be reachable.+# No migrations: the api owns them. The bot reads the instance bot token and writes+# account links through @repo/db, so it needs the database to be reachable.CMD ["bun", "run", "apps/bot/src/index.ts"]diff --git a/apps/bot/package.json b/apps/bot/package.jsonindex 40d0f066..7c3585af 100644--- a/apps/bot/package.json+++ b/apps/bot/package.json@@ -8,9 +8,11 @@"start": "bun run src/index.ts","lint": "eslint .","typecheck": "tsc --noEmit",- "test": "if find src -name '*.test.ts' -print -quit | grep -q .; then bun test; fi"+ "test": "bun test --env-file=../../.env.test"},"dependencies": {+ "@repo/db": "workspace:*",+ "drizzle-orm": "^0.45.2","grammy": "^1.45.1"},"devDependencies": {diff --git a/apps/bot/src/api.ts b/apps/bot/src/api.tsdeleted file mode 100644index cc840ccc..00000000--- a/apps/bot/src/api.ts+++ /dev/null@@ -1,48 +0,0 @@-import { botConfig } from './config';--// The bot's only data access. It holds no database connection and no encryption key:-// the api owns both and exposes exactly what the bot needs behind the shared-// WORKER_INTERNAL_TOKEN, the same arrangement the worker uses for notification-// delivery.--export interface RemoteBotConfig {- enabled: boolean;- botToken: string;- botUsername: string;-}--export type LinkResult = { ok: true } | { ok: false; reason: 'invalid' | 'taken' };--async function call<T>(path: string, init?: RequestInit): Promise<T> {- const cfg = botConfig();- const res = await fetch(`${cfg.apiBaseUrl}${path}`, {- ...init,- headers: {- 'content-type': 'application/json',- 'x-worker-token': cfg.internalToken,- ...init?.headers,- },- signal: AbortSignal.timeout(cfg.timeoutMs),- });- if (!res.ok) throw new Error(`${path} returned ${res.status}`);- return (await res.json()) as T;-}--// The instance bot settings. `enabled: false` covers both "no bot configured" and-// "turned off", so the caller only has to check that one flag.-export function fetchBotConfig(): Promise<RemoteBotConfig> {- return call<RemoteBotConfig>('/internal/telegram/config');-}--// Completes an account link for a `/start <code>` the bot received.-export function confirmLink(input: {- code: string;- chatId: string;- username: string | null;- firstName: string | null;-}): Promise<LinkResult> {- return call<LinkResult>('/internal/telegram/link', {- method: 'POST',- body: JSON.stringify(input),- });-}diff --git a/apps/bot/src/bot.ts b/apps/bot/src/bot.tsindex 4986d2f8..a490f78a 100644--- a/apps/bot/src/bot.ts+++ b/apps/bot/src/bot.ts@@ -1,5 +1,5 @@import { Bot } from 'grammy';-import { confirmLink } from './api';+import { confirmTelegramLink } from './db';// The Telegram bot itself. Today it handles one thing: the `/start <code>` deep link// that completes an account link. The code is minted by the api when the user presses@@ -30,7 +30,7 @@ export function createBot(token: string): Bot {return;}- const result = await confirmLink({+ const result = await confirmTelegramLink({code,chatId: String(ctx.chat.id),username: from.username ?? null,diff --git a/apps/bot/src/config.ts b/apps/bot/src/config.tsindex 05e60353..779b3989 100644--- a/apps/bot/src/config.ts+++ b/apps/bot/src/config.ts@@ -2,7 +2,7 @@// env is loaded (via --env-file / the container env) before it is read.//// The bot token is not here: it is instance configuration an administrator changes at-// runtime, so it is fetched from the api instead (see api.ts).+// runtime, so it is read from the database (see db.ts).function intEnv(name: string, fallback: number): number {const value = Number(process.env[name]);@@ -10,35 +10,22 @@ function intEnv(name: string, fallback: number): number {}export interface BotConfig {- // The api origin. Same resolution as the worker uses: SERVICE_URL_API in the- // compose stack (Coolify sets it), API_URL locally.- apiBaseUrl: string;- // Shared secret for the /internal/* routes, the same value the worker uses.- internalToken: string;- // How often to re-read the bot settings from the api, so a token added or changed- // in god mode takes effect without a restart.+ // How often to re-read the bot settings, so a token added or changed in god mode+ // takes effect without a restart.configPollIntervalMs: number;- // How soon to retry after the api could not be reached. Shorter than the steady- // interval: at startup the api is often still binding its port, and waiting a full- // cycle would leave the bot idle for no reason.+ // How soon to retry after the database could not be reached. Shorter than the+ // steady interval: at startup Postgres is often still accepting its first+ // connections, and waiting a full cycle would leave the bot idle for no reason.configRetryIntervalMs: number;- timeoutMs: number;}let cached: BotConfig | null = null;export function botConfig(): BotConfig {if (cached) return cached;- const internalToken = process.env.WORKER_INTERNAL_TOKEN;- if (!internalToken) throw new Error('WORKER_INTERNAL_TOKEN is required');- const apiBaseUrl = process.env.SERVICE_URL_API ?? process.env.API_URL;- if (!apiBaseUrl) throw new Error('SERVICE_URL_API or API_URL is required');cached = {- apiBaseUrl,- internalToken,configPollIntervalMs: intEnv('BOT_CONFIG_POLL_INTERVAL_MS', 30_000),configRetryIntervalMs: intEnv('BOT_CONFIG_RETRY_INTERVAL_MS', 5_000),- timeoutMs: intEnv('BOT_API_TIMEOUT_MS', 15_000),};return cached;}diff --git a/apps/bot/src/db.test.ts b/apps/bot/src/db.test.tsnew file mode 100644index 00000000..91dc1e4b--- /dev/null+++ b/apps/bot/src/db.test.ts@@ -0,0 +1,69 @@+import { describe, it, expect } from 'bun:test';+import { randomUUID } from 'node:crypto';+import { db, user, userTelegramAccount } from '@repo/db';+import { confirmTelegramLink } from './db';++// Redeeming a `/start` code is the only write this service makes, and it is where the+// link is decided: a code must work once, and a chat must not be taken over from+// another user. The pending rows the api mints are inserted directly here.++async function pendingLink(ttlMinutes = 15): Promise<{ userId: string; code: string }> {+ const userId = randomUUID();+ await db.insert(user).values({ id: userId, name: 'Linker', email: `${userId}@example.test` });+ const code = randomUUID();+ await db.insert(userTelegramAccount).values({+ userId,+ linkCode: code,+ linkCodeExpiresAt: new Date(Date.now() + ttlMinutes * 60_000),+ });+ return { userId, code };+}++describe('confirmTelegramLink', () => {+ it('links the chat and refuses to replay the code', async () => {+ const { userId, code } = await pendingLink();+ const chatId = randomUUID();++ const first = await confirmTelegramLink({ code, chatId, username: 'lin', firstName: 'Lin' });+ expect(first).toEqual({ ok: true, userId });++ const replay = await confirmTelegramLink({ code, chatId, username: 'lin', firstName: 'Lin' });+ expect(replay).toEqual({ ok: false, reason: 'invalid' });+ });++ it('refuses a chat already linked to another user', async () => {+ const chatId = randomUUID();+ const first = await pendingLink();+ await confirmTelegramLink({ code: first.code, chatId, username: null, firstName: null });++ const second = await pendingLink();+ const result = await confirmTelegramLink({+ code: second.code,+ chatId,+ username: null,+ firstName: null,+ });+ expect(result).toEqual({ ok: false, reason: 'taken' });+ });++ it('rejects an expired code', async () => {+ const { code } = await pendingLink(-1);+ const result = await confirmTelegramLink({+ code,+ chatId: randomUUID(),+ username: null,+ firstName: null,+ });+ expect(result).toEqual({ ok: false, reason: 'invalid' });+ });++ it('rejects an unknown code', async () => {+ const result = await confirmTelegramLink({+ code: 'nope',+ chatId: randomUUID(),+ username: null,+ firstName: null,+ });+ expect(result).toEqual({ ok: false, reason: 'invalid' });+ });+});diff --git a/apps/bot/src/db.ts b/apps/bot/src/db.tsnew file mode 100644index 00000000..7f2eeca4--- /dev/null+++ b/apps/bot/src/db.ts@@ -0,0 +1,57 @@+import { and, eq, gt, ne } from 'drizzle-orm';+import { db, userTelegramAccount } from '@repo/db';++// The account links the bot redeems. The instance bot settings it polls are read+// through @repo/db, which the api writes them with.++export interface ConfirmLinkInput {+ code: string;+ chatId: string;+ username: string | null;+ firstName: string | null;+}++export type ConfirmLinkResult =+ { ok: true; userId: string } | { ok: false; reason: 'invalid' | 'taken' };++// Completes a link: matches the code the api minted, then writes the chat id onto that+// user's row and clears the code so it cannot be replayed. 'invalid' covers an+// unknown, already-used, or expired code — the user is told to start again either+// way. 'taken' means this Telegram account is already linked to someone else.+export async function confirmTelegramLink(input: ConfirmLinkInput): Promise<ConfirmLinkResult> {+ const rows = await db+ .select({ userId: userTelegramAccount.userId })+ .from(userTelegramAccount)+ .where(+ and(+ eq(userTelegramAccount.linkCode, input.code),+ gt(userTelegramAccount.linkCodeExpiresAt, new Date()),+ ),+ );+ const pending = rows[0];+ if (!pending) return { ok: false, reason: 'invalid' };++ const conflict = await db+ .select({ userId: userTelegramAccount.userId })+ .from(userTelegramAccount)+ .where(+ and(+ eq(userTelegramAccount.chatId, input.chatId),+ ne(userTelegramAccount.userId, pending.userId),+ ),+ );+ if (conflict.length > 0) return { ok: false, reason: 'taken' };++ await db+ .update(userTelegramAccount)+ .set({+ chatId: input.chatId,+ username: input.username,+ firstName: input.firstName,+ linkedAt: new Date(),+ linkCode: null,+ linkCodeExpiresAt: null,+ })+ .where(eq(userTelegramAccount.userId, pending.userId));+ return { ok: true, userId: pending.userId };+}diff --git a/apps/bot/src/supervisor.ts b/apps/bot/src/supervisor.tsindex dfe8bae1..7c14372f 100644--- a/apps/bot/src/supervisor.ts+++ b/apps/bot/src/supervisor.ts@@ -1,10 +1,10 @@import type { Bot } from 'grammy';+import { getInstanceBotConfig, isInstanceBotUsable } from '@repo/db';import { botConfig } from './config';-import { fetchBotConfig } from './api';import { createBot } from './bot';// Keeps the running bot in step with the instance settings. The token is not env-// configuration: an administrator sets it in god mode, so this polls the api and+// configuration: an administrator sets it in god mode, so this polls the database and// starts, stops, or replaces the bot when it changes. Without that, adding the token// would need a redeploy.//@@ -49,22 +49,22 @@ async function loop(): Promise<void> {try {await reconcile();} catch (err) {- // The api being briefly unreachable must not kill the service, and a bot already- // running keeps running meanwhile. This is the normal case at startup, where the- // api is still binding its port — so retry sooner than the steady-state interval- // instead of leaving the bot idle for a full poll cycle.+ // The database being briefly unreachable must not kill the service, and a bot+ // already running keeps running meanwhile. This is the normal case at startup, so+ // retry sooner than the steady-state interval instead of leaving the bot idle for+ // a full poll cycle.delay = cfg.configRetryIntervalMs;const reason = err instanceof Error ? err.message : String(err);- console.error(`[bot] could not read bot settings from ${cfg.apiBaseUrl}: ${reason}`);+ console.error(`[bot] could not read bot settings: ${reason}`);}if (stopped) return;timer = setTimeout(loop, delay);}async function reconcile(): Promise<void> {- const settings = await fetchBotConfig();+ const settings = await getInstanceBotConfig();- if (!settings.enabled || !settings.botToken) {+ if (!isInstanceBotUsable(settings)) {if (current) console.log('[bot] bot turned off, stopping');await stopCurrent();return;diff --git a/apps/worker/AGENTS.md b/apps/worker/AGENTS.mdindex f66a95c1..4d1e1ce2 100644--- a/apps/worker/AGENTS.md+++ b/apps/worker/AGENTS.md@@ -8,25 +8,34 @@ own service (own Dockerfile), separate from `apps/api`. See root `AGENTS.md`.- Polls `webhook_delivery` for due `pending` rows, claims a batch with`FOR UPDATE SKIP LOCKED`, posts each to its webhook URL, records the outcome.+- Queues an `agent_run` row for every due `agent_schedule`.- Signs every request: `X-Itsaplan-Signature: t=<ts>,v1=<hmac-sha256>` over`${ts}.${body}` with the webhook's `secret`. Plus `X-Itsaplan-Event`,`X-Itsaplan-Delivery`, `X-Itsaplan-Event-Id` (stable across retries).- Retries transient failures (timeout, 429, 5xx) with equal-jitter exponentialbackoff up to `WEBHOOK_MAX_ATTEMPTS`; permanent 4xx fail immediately. After`WEBHOOK_DISABLE_THRESHOLD` consecutive failures the webhook is auto-disabled.+- Drains `notification_delivery` and sends each row itself: email through+ `@repo/mailer`, Telegram through the Bot API. The provider credentials are read+ from the database and decrypted here (`notification-send.ts`), so the process+ needs `APP_ENCRYPTION_KEY`.## Invariants- **Reads/writes `@repo/db` directly, never the API over HTTP.** It is a DB- consumer and an HTTP producer. It does not import `apps/api`.+ consumer and an HTTP producer. It does not import `apps/api` and does not call it.+- **Notification credentials are read, never taken from the caller.** A delivery+ row names its project; the credentials come from the team that owns it and from+ the instance config. Nothing about the recipient or the provider is passed in+ from outside.- **No migrations here.** The api applies them on startup; the worker only usesexisting tables and tolerates their brief absence (a tick logs and retries).- **At-least-once delivery.** Duplicates are possible (a 2xx whose ACK is lost);the `event_id` is stable across retries so receivers deduplicate. Never mint anew id per attempt.-- **Agent runs: internal agents only.** An external agent's runs are claimed over- HTTP by the operator's runner (`POST /agent-runs/claim`), so the worker's claim- filters on `ai_agent.kind = 'internal'` and leaves the rest queued.+- **Agent schedules are queued here, run in the api.** A due schedule gets an+ `agent_run` row; the api drains that queue, where the agent runtime and the model+ credentials live.- **Claim leases, not a status flag.** Claiming pushes `next_attempt_at` forwardby `WEBHOOK_LEASE_SECONDS`; a crashed delivery is reclaimed after the lease. Keepthe lease comfortably larger than `WEBHOOK_TIMEOUT_MS`.@@ -40,10 +49,16 @@ All via env with defaults (see `src/config.ts`): `WEBHOOK_POLL_INTERVAL_MS`,`WEBHOOK_BATCH_SIZE`, `WEBHOOK_TIMEOUT_MS`, `WEBHOOK_MAX_ATTEMPTS`,`WEBHOOK_DISABLE_THRESHOLD`, `WEBHOOK_LEASE_SECONDS`, `WEBHOOK_CLEANUP_DAYS`,`WEBHOOK_CLEANUP_EVERY_TICKS`. Only `DATABASE_URL` is required for webhook-delivery. Agent runs and notification delivery additionally need-`WORKER_INTERNAL_TOKEN` and an api origin (`SERVICE_URL_API`, else-`API_URL`); `internal-api.ts` throws when either is missing, no fallback-origin.+delivery. Notification delivery also needs `APP_ENCRYPTION_KEY` (the same value the+api uses) to read the stored provider credentials.++## Tests++`src/__tests__/unit/` covers the pure logic with no database. `src/__tests__/integration/`+covers what needs one — the notification send reads its config from the database — and+runs against the test DB (`bun run test` loads `.env.test`; the Docker gate runs it+after the api and bot suites). It inserts the rows the api writes in production, the+way `apps/bot` does; there is no api to call.## Rundiff --git a/apps/worker/package.json b/apps/worker/package.jsonindex 817eb520..f3b9c73c 100644--- a/apps/worker/package.json+++ b/apps/worker/package.json@@ -8,10 +8,11 @@"start": "bun run src/index.ts","lint": "eslint .","typecheck": "tsc --noEmit",- "test": "bun test"+ "test": "bun test --env-file=../../.env.test"},"dependencies": {"@repo/db": "workspace:*",+ "@repo/mailer": "workspace:*","@repo/net": "workspace:*","croner": "^10.0.1","drizzle-orm": "^0.45.2"diff --git a/apps/worker/src/__tests__/integration/notification-send.test.ts b/apps/worker/src/__tests__/integration/notification-send.test.tsnew file mode 100644index 00000000..393a4bcf--- /dev/null+++ b/apps/worker/src/__tests__/integration/notification-send.test.ts@@ -0,0 +1,73 @@+import { beforeEach, describe, it, expect } from 'bun:test';+import { randomUUID } from 'node:crypto';+import { appSecret, db, project, team, teamInvite } from '@repo/db';+import { deliverNotification } from '../../notification-send';++// The send reads the credentials itself, so these cover the two decisions it makes+// before a message can leave: a queued invite email whose invite is gone is dropped+// rather than sent, and a team with no provider configured fails permanently instead+// of being retried forever. The rows the api writes in production are inserted+// directly — there is no api to call.++async function makeProject(): Promise<{ teamId: number; projectId: number }> {+ const [row] = await db.insert(team).values({ name: 'Senders' }).returning({ id: team.id });+ const [created] = await db+ .insert(project)+ .values({ teamId: row!.id, key: randomUUID().slice(0, 8), name: 'Marketing' })+ .returning({ id: project.id });+ return { teamId: row!.id, projectId: created!.id };+}++describe('deliverNotification', () => {+ // A team with no provider of its own falls back to the instance one, so the suite+ // that ran before this in the shared test database must not leave one behind.+ beforeEach(async () => {+ await db.delete(appSecret);+ });++ it('drops an invite email whose invite is no longer pending', async () => {+ const { teamId, projectId } = await makeProject();+ const [invite] = await db+ .insert(teamInvite)+ .values({+ teamId,+ projectId,+ email: '[email protected]',+ status: 'accepted',+ })+ .returning({ id: teamInvite.id });++ const result = await deliverNotification({+ projectId,+ channel: 'email',+ recipient: '[email protected]',+ payload: { text: 'Invitation', projectInviteId: invite!.id },+ });++ expect(result).toEqual({ ok: true });+ });++ it('fails permanently when the team has no email provider', async () => {+ const { projectId } = await makeProject();++ const result = await deliverNotification({+ projectId,+ channel: 'email',+ recipient: '[email protected]',+ payload: { text: 'Assigned to you' },+ });++ expect(result).toEqual({ ok: false, retryable: false, error: 'email not configured' });+ });++ it('fails permanently when the project is gone', async () => {+ const result = await deliverNotification({+ projectId: 0,+ channel: 'email',+ recipient: '[email protected]',+ payload: { text: 'Assigned to you' },+ });++ expect(result).toEqual({ ok: false, retryable: false, error: 'Project not found' });+ });+});diff --git a/apps/worker/src/agent-runs.ts b/apps/worker/src/agent-runs.tsdeleted file mode 100644index 8174a973..00000000--- a/apps/worker/src/agent-runs.ts+++ /dev/null@@ -1,159 +0,0 @@-import { db, agentRun } from '@repo/db';-import { and, eq, sql } from 'drizzle-orm';-import { equalJitterBackoffMs } from './backoff';-import { intEnv } from './env';-import { postInternal } from './internal-api';--type Trigger = 'mention' | 'delegation' | 'schedule' | 'manual';--// How long a run waits after the api turned it back for a full queue.-const DEFERRED_RETRY_SECONDS = 30;--interface ClaimedRun {- id: number;- agentId: number;- issueId: number | null;- scheduleId: number | null;- trigger: Trigger;- prompt: string;- attempts: number;- projectId: number;- agentUserId: string;- agentUsername: string;- issueIdentifier: string | null;- issueTitle: string | null;- assigneeName: string | null;- assigneeUsername: string | null;- requesterName: string | null;- requesterUsername: string | null;-}--export async function processAgentRuns(): Promise<void> {- const runs = await claimDueRuns();- await Promise.all(runs.map(processRun));-}--// Only internal agents run here. An external agent's runs are claimed over HTTP by-// the operator's runner, so the worker must leave them in the queue however long-// they sit there.-async function claimDueRuns(): Promise<ClaimedRun[]> {- const batchSize = intEnv('AGENT_RUN_BATCH_SIZE', 5);- const leaseSeconds = intEnv('AGENT_RUN_LEASE_SECONDS', 300);- const rows = await db.execute(sql`- UPDATE agent_run r- SET attempts = r.attempts + 1,- started_at = coalesce(r.started_at, now()),- next_attempt_at = now() + make_interval(secs => ${leaseSeconds})- WHERE r.id IN (- SELECT id FROM agent_run q- WHERE q.status = 'pending' AND q.next_attempt_at <= now()- AND (SELECT kind FROM ai_agent a WHERE a.id = q.agent_id) = 'internal'- ORDER BY q.next_attempt_at, q.id- FOR UPDATE SKIP LOCKED- LIMIT ${batchSize}- )- RETURNING- r.id, r.agent_id AS "agentId", r.issue_id AS "issueId",- r.schedule_id AS "scheduleId", r.trigger, r.prompt, r.attempts,- r.project_id AS "projectId",- (SELECT user_id FROM ai_agent a WHERE a.id = r.agent_id) AS "agentUserId",- (SELECT username FROM ai_agent a WHERE a.id = r.agent_id) AS "agentUsername",- (SELECT p.key || '-' || i.sequence_number FROM issue i JOIN project p ON p.id = i.project_id WHERE i.id = r.issue_id) AS "issueIdentifier",- (SELECT title FROM issue i WHERE i.id = r.issue_id) AS "issueTitle",- (SELECT u.name FROM issue i JOIN "user" u ON u.id = i.assignee_user_id WHERE i.id = r.issue_id) AS "assigneeName",- (SELECT COALESCE(u.username, ag.username) FROM issue i JOIN "user" u ON u.id = i.assignee_user_id- LEFT JOIN ai_agent ag ON ag.user_id = u.id WHERE i.id = r.issue_id) AS "assigneeUsername",- (SELECT actor_name FROM issue_activity a WHERE a.id = r.source_activity_id) AS "requesterName",- (SELECT COALESCE(u.username, ag.username) FROM issue_activity a JOIN "user" u ON u.id = a.actor_user_id- LEFT JOIN ai_agent ag ON ag.user_id = u.id WHERE a.id = r.source_activity_id) AS "requesterUsername"- `);- return rows as unknown as ClaimedRun[];-}--// The api turned the run back because its team already has as many agents running as it-// may. Waiting for a slot is not a failed attempt.-class RunDeferred extends Error {}--// Every write is conditional on the run still being 'pending': a run canceled while it-// was in flight keeps that outcome instead of being finished or retried.-async function processRun(run: ClaimedRun): Promise<void> {- try {- const { output, usage } = await executeRun(run);- await db- .update(agentRun)- .set({- status: 'success',- output,- lastError: null,- inputTokens: usage?.inputTokens ?? null,- outputTokens: usage?.outputTokens ?? null,- finishedAt: new Date(),- })- .where(and(eq(agentRun.id, run.id), eq(agentRun.status, 'pending')));- } catch (error) {- if (error instanceof RunDeferred) {- await db- .update(agentRun)- .set({- attempts: sql`${agentRun.attempts} - 1`,- nextAttemptAt: sql`now() + make_interval(secs => ${DEFERRED_RETRY_SECONDS})`,- })- .where(and(eq(agentRun.id, run.id), eq(agentRun.status, 'pending')));- return;- }- const message = error instanceof Error ? error.message : String(error);- if (run.attempts < intEnv('AGENT_RUN_MAX_ATTEMPTS', 3)) {- const delaySeconds = Math.ceil(- equalJitterBackoffMs(run.attempts, 30_000, 30 * 60_000) / 1000,- );- await db- .update(agentRun)- .set({- nextAttemptAt: sql`now() + make_interval(secs => ${delaySeconds})`,- lastError: message.slice(0, 500),- })- .where(and(eq(agentRun.id, run.id), eq(agentRun.status, 'pending')));- return;- }- await db- .update(agentRun)- .set({ status: 'failed', lastError: message.slice(0, 500), finishedAt: new Date() })- .where(and(eq(agentRun.id, run.id), eq(agentRun.status, 'pending')));- }-}--// Drops the agent conversation threads of archived issues. The threads live in-// Mastra's tables, which only the api talks to, so the worker asks it over the same-// internal route it uses to run an agent. The api looks the threads up per issue, so-// the ids go in batches: the first sweep of an instance that just enabled auto-archive-// can carry thousands of them, and one request for all would run past the timeout.-export async function deleteIssueAgentThreads(issueIds: number[]): Promise<void> {- for (let from = 0; from < issueIds.length; from += 200) {- const response = await postInternal(- '/internal/agent-threads/delete-for-issues',- { issueIds: issueIds.slice(from, from + 200) },- 30_000,- );- if (!response.ok) throw new Error(`Agent API returned ${response.status}`);- }-}--// `usage` is what the last model call of the run read and wrote. An api still running-// the previous build reports none, and the run is stored without counts.-async function executeRun(- run: ClaimedRun,-): Promise<{ output: string; usage: { inputTokens: number; outputTokens: number } | null }> {- const response = await postInternal(- '/internal/agent-runs/execute',- run,- intEnv('AGENT_RUN_TIMEOUT_MS', 240_000),- );- const body = (await response.json().catch(() => null)) as {- output?: string;- error?: string;- usage?: { inputTokens: number; outputTokens: number } | null;- } | null;- if (response.status === 503) throw new RunDeferred(body?.error ?? 'No free slot');- if (!response.ok) throw new Error(body?.error ?? `Agent API returned ${response.status}`);- return { output: body?.output ?? '', usage: body?.usage ?? null };-}diff --git a/apps/worker/src/agent-worker.ts b/apps/worker/src/agent-worker.tsindex 5cf32452..387a9610 100644--- a/apps/worker/src/agent-worker.ts+++ b/apps/worker/src/agent-worker.ts@@ -1,15 +1,11 @@import { enqueueDueSchedules } from './schedules';-import { processAgentRuns } from './agent-runs';import { intEnv } from './env';import { startPollLoop, type WorkerHandle } from './poll-loop';+// Queues the runs of due agent schedules. Running them is the api's: the agent runtime+// and the model credentials live there, and it drains the queue itself.export function startAgentWorker(): WorkerHandle {- return startPollLoop(- 'agent-worker',- async () => {- await enqueueDueSchedules();- await processAgentRuns();- },- () => intEnv('AGENT_RUN_POLL_INTERVAL_MS', 2000),+ return startPollLoop('agent-worker', enqueueDueSchedules, () =>+ intEnv('AGENT_RUN_POLL_INTERVAL_MS', 2000),);}diff --git a/apps/worker/src/config.ts b/apps/worker/src/config.tsindex 0536e26f..8614a3b2 100644--- a/apps/worker/src/config.ts+++ b/apps/worker/src/config.ts@@ -23,9 +23,6 @@ export interface WorkerConfig {cleanupDays: number;// Run the cleanup once every this many ticks.cleanupEveryTicks: number;- // Run the auto-archive sweep once every this many ticks. Archiving is not time-- // sensitive (an issue past its threshold can wait a tick), so this is coarse.- autoArchiveEveryTicks: number;}let cached: WorkerConfig | null = null;@@ -41,8 +38,6 @@ export function workerConfig(): WorkerConfig {leaseSeconds: intEnv('WEBHOOK_LEASE_SECONDS', 120),cleanupDays: intEnv('WEBHOOK_CLEANUP_DAYS', 30),cleanupEveryTicks: intEnv('WEBHOOK_CLEANUP_EVERY_TICKS', 300),- // Default ~1h at the 2s poll interval (1800 ticks).- autoArchiveEveryTicks: intEnv('AUTO_ARCHIVE_EVERY_TICKS', 1800),};return cached;}diff --git a/apps/worker/src/internal-api.ts b/apps/worker/src/internal-api.tsdeleted file mode 100644index e84af145..00000000--- a/apps/worker/src/internal-api.ts+++ /dev/null@@ -1,20 +0,0 @@-// Posts to the API's /internal/* routes. The worker owns the queues, the API owns-// the credentials and the actual send, so every outbound job goes through here.-// The api origin is SERVICE_URL_API in the compose stack (Coolify sets it) and-// API_URL locally.-export async function postInternal(- path: string,- body: unknown,- timeoutMs: number,-): Promise<Response> {- const token = process.env.WORKER_INTERNAL_TOKEN;- if (!token) throw new Error('WORKER_INTERNAL_TOKEN is required');- const baseUrl = process.env.SERVICE_URL_API ?? process.env.API_URL;- if (!baseUrl) throw new Error('SERVICE_URL_API or API_URL is required');- return fetch(`${baseUrl}${path}`, {- method: 'POST',- headers: { 'content-type': 'application/json', 'x-worker-token': token },- body: JSON.stringify(body),- signal: AbortSignal.timeout(timeoutMs),- });-}diff --git a/apps/worker/src/notification-delivery.ts b/apps/worker/src/notification-delivery.tsindex 6462ffaf..d81b8830 100644--- a/apps/worker/src/notification-delivery.ts+++ b/apps/worker/src/notification-delivery.ts@@ -1,31 +1,23 @@-import { db, notificationDelivery } from '@repo/db';+import { db, notificationDelivery, type DeliveryPayload } from '@repo/db';import { eq, sql } from 'drizzle-orm';import { equalJitterBackoffMs } from './backoff';import { intEnv } from './env';-import { postInternal } from './internal-api';+import { deliverNotification, type SendResult } from './notification-send';-// Drains the notification_delivery outbox: claims due rows and asks the API to send-// each one. Follows the same claim/retry pattern as webhook delivery, but the send-// itself runs in the API (POST /internal/notification-deliveries/send) because the-// channel credentials are encrypted with the API's key — the worker never decrypts.-// A succeeded row is deleted (no delivery history is kept); a permanently failed row-// is left as 'failed' with its last error for debugging.+// Drains the notification_delivery outbox: claims due rows and sends each one.+// Follows the same claim/retry pattern as webhook delivery. A succeeded row is+// deleted (no delivery history is kept); a permanently failed row is left as+// 'failed' with its last error for debugging.interface ClaimedNotification {id: number;projectId: number;channel: string;recipient: string | null;- payload: unknown;+ payload: DeliveryPayload;attempts: number;}-interface SendResult {- ok: boolean;- retryable?: boolean;- error?: string;-}-export async function processNotificationDeliveries(): Promise<void> {const claimed = await claimDueDeliveries();await Promise.all(claimed.map(processDelivery));@@ -63,7 +55,7 @@ async function claimDueDeliveries(): Promise<ClaimedNotification[]> {async function processDelivery(d: ClaimedNotification): Promise<void> {let result: SendResult;try {- result = await send(d);+ result = await deliverNotification(d);} catch (err) {result = {ok: false,@@ -92,22 +84,3 @@ async function processDelivery(d: ClaimedNotification): Promise<void> {.set({ status: 'failed', lastError: error }).where(eq(notificationDelivery.id, d.id));}--// Calls the API to actually deliver the message.-async function send(d: ClaimedNotification): Promise<SendResult> {- const res = await postInternal(- '/internal/notification-deliveries/send',- {- projectId: d.projectId,- channel: d.channel,- recipient: d.recipient,- payload: d.payload,- },- intEnv('NOTIFICATION_TIMEOUT_MS', 20_000),- );- const body = (await res.json().catch(() => null)) as SendResult | null;- if (!res.ok) {- return { ok: false, retryable: res.status >= 500, error: `send API returned ${res.status}` };- }- return body ?? { ok: false, retryable: true, error: 'empty send response' };-}diff --git a/apps/api/src/modules/notifications/send.ts b/apps/worker/src/notification-send.tssimilarity index 57%rename from apps/api/src/modules/notifications/send.tsrename to apps/worker/src/notification-send.tsindex a3fc4c51..7898f961 100644--- a/apps/api/src/modules/notifications/send.ts+++ b/apps/worker/src/notification-send.ts@@ -1,23 +1,32 @@+import {+ db,+ project,+ teamInvite,+ emailSource,+ getDeliveryConfig,+ getInstanceBotConfig,+ getInstanceEmailConfig,+ getProjectEmailConfig,+ isInstanceBotUsable,+ type DeliveryPayload,+ type NotificationConfig,+} from '@repo/db';+import { and, eq } from 'drizzle-orm';import { sendEmail, emailBody, type EmailConfig, type SendResult } from '@repo/mailer';-import { getEmailConfig, getProjectEmailConfig } from '@repo/auth';-import { emailSource, type NotificationConfig } from '#modules/notification-settings/service';-import type { DeliveryPayload } from './outbound';-import { getInstanceBotConfig, isInstanceBotUsable } from '#modules/telegram/service';-// Sends one composed notification over the requested channel using the team's-// decrypted config. Email transport lives in @repo/mailer (shared with the-// authentication mail sent from @repo/auth); Telegram is only used here, so it stays-// in this file. A team that set no bot token of its own sends through the instance-// bot, the same one members link their Telegram accounts through. Adding a channel is-// a new branch here plus a compose function in outbound.ts; nothing else changes. The-// result tells the worker whether a failure is worth retrying (transient: network-// error, timeout, rate limit, server error) or permanent (bad credentials, rejected-// recipient, misconfiguration).+// Sends one claimed notification_delivery row over its channel. Email transport lives+// in @repo/mailer (shared with the authentication mail sent from @repo/auth); Telegram+// is only used here, so it stays in this file. A team that set no bot token of its own+// sends through the instance bot, the same one members link their Telegram accounts+// through. Adding a channel is a new branch here plus a compose function in the api's+// outbound.ts; nothing else changes. The result tells the caller whether a failure is+// worth retrying (transient: network error, timeout, rate limit, server error) or+// permanent (bad credentials, rejected recipient, misconfiguration).export type { SendResult };export interface SendInput {- channel: 'email' | 'telegram';+ channel: string;recipient: string | null;payload: DeliveryPayload;config: NotificationConfig;@@ -31,7 +40,7 @@ async function sendNotificationEmail(input: SendInput): Promise<SendResult> {const source = emailSource(input.config);const config: EmailConfig | null =input.payload.emailSource === 'instance'- ? await getEmailConfig()+ ? await getInstanceEmailConfig(): source === 'system'? await getProjectEmailConfig(): source === 'none'@@ -99,8 +108,59 @@ async function sendTelegram(input: SendInput): Promise<SendResult> {}}-export async function sendDelivery(input: SendInput): Promise<SendResult> {+async function sendDelivery(input: SendInput): Promise<SendResult> {if (input.channel === 'email') return sendNotificationEmail(input);if (input.channel === 'telegram') return sendTelegram(input);- return { ok: false, retryable: false, error: `unknown channel: ${input.channel as string}` };+ return { ok: false, retryable: false, error: `unknown channel: ${input.channel}` };+}++// Whether a queued invite email still has a live invite behind it.+async function isInvitePending(projectId: number, inviteId: number): Promise<boolean> {+ const rows = await db+ .select({ id: teamInvite.id })+ .from(teamInvite)+ .where(+ and(+ eq(teamInvite.projectId, projectId),+ eq(teamInvite.id, inviteId),+ eq(teamInvite.status, 'pending'),+ ),+ )+ .limit(1);+ return rows.length > 0;+}++// The credentials belong to the team that owns the project the row came from.+async function teamOf(projectId: number): Promise<number | null> {+ const rows = await db+ .select({ teamId: project.teamId })+ .from(project)+ .where(eq(project.id, projectId));+ return rows[0]?.teamId ?? null;+}++export interface DeliveryRow {+ projectId: number;+ channel: string;+ recipient: string | null;+ payload: DeliveryPayload;+}++export async function deliverNotification(row: DeliveryRow): Promise<SendResult> {+ if (+ row.payload.projectInviteId != null &&+ !(await isInvitePending(row.projectId, row.payload.projectInviteId))+ ) {+ // The invite was accepted, rejected, or revoked while its email waited in the+ // outbox. Treat it as delivered so the row is removed.+ return { ok: true };+ }+ const teamId = await teamOf(row.projectId);+ if (teamId == null) return { ok: false, retryable: false, error: 'Project not found' };+ return sendDelivery({+ channel: row.channel,+ recipient: row.recipient,+ payload: row.payload,+ config: await getDeliveryConfig(teamId),+ });}diff --git a/apps/worker/src/store.ts b/apps/worker/src/store.tsindex e083e1d7..36018ea6 100644--- a/apps/worker/src/store.ts+++ b/apps/worker/src/store.ts@@ -142,38 +142,3 @@ export async function cleanupOldDeliveries(): Promise<number> {`);return (res as unknown as { count?: number }).count ?? 0;}--// Auto-archive sweep: archives active issues that have sat inactive in a-// completed/canceled column past their project's configured threshold. The-// threshold lives in project_setting under key 'auto_archive' as-// { completedDays, canceledDays }; a positive day count enables archiving for that-// state group, null/absent disables it (kept in sync with getAutoArchiveSettings in-// apps/api/src/modules/projects/service.ts). Inactivity is measured by-// issue.updated_at: moving to a terminal column bumps it, and any later edit resets-// the clock, so an issue is archived only after the full period with no activity.-// The ->> is guarded by a numeric-string regex so a malformed or missing value is-// treated as disabled, never cast. Returns the ids archived, which the caller uses-// to drop the agent conversation threads of those issues. Idempotent-// (archived_at IS NULL filter).-export async function archiveStaleIssues(): Promise<number[]> {- const rows = await db.execute(sql`- UPDATE issue i- SET archived_at = now()- FROM project_column c, project_setting s- WHERE i.column_id = c.id- AND i.project_id = s.project_id- AND s.key = 'auto_archive'- AND i.archived_at IS NULL- AND (- (c.state_type = 'completed'- AND (s.value->>'completedDays') ~ '^[0-9]{1,4}$'- AND i.updated_at < now() - make_interval(days => (s.value->>'completedDays')::int))- OR- (c.state_type = 'canceled'- AND (s.value->>'canceledDays') ~ '^[0-9]{1,4}$'- AND i.updated_at < now() - make_interval(days => (s.value->>'canceledDays')::int))- )- RETURNING i.id- `);- return (rows as unknown as Array<{ id: number }>).map((row) => row.id);-}diff --git a/apps/worker/src/worker.ts b/apps/worker/src/worker.tsindex 2490f8d8..0d8ed4d8 100644--- a/apps/worker/src/worker.ts+++ b/apps/worker/src/worker.ts@@ -2,7 +2,6 @@ import { workerConfig } from './config';import { deliver } from './delivery';import { processNotificationDeliveries } from './notification-delivery';import { equalJitterBackoffMs } from './backoff';-import { deleteIssueAgentThreads } from './agent-runs';import { startPollLoop, type WorkerHandle } from './poll-loop';import { TELEMETRY_CHECK_EVERY_TICKS, processTelemetry } from './telemetry';import {@@ -13,11 +12,9 @@ import {markFailed,markSkippedInactive,cleanupOldDeliveries,- archiveStaleIssues,} from './store';let ticksSinceCleanup = 0;-let ticksSinceAutoArchive = 0;// Starts due, so an install is visible even if the instance is removed minutes later.let ticksSinceTelemetry = TELEMETRY_CHECK_EVERY_TICKS;@@ -26,8 +23,8 @@ export function startWorker(): WorkerHandle {}// One poll: claim a batch of due deliveries, send them concurrently, record each-// outcome, then run the delivery cleanup, the auto-archive sweep and the telemetry-// check on their own tick intervals.+// outcome, then run the delivery cleanup and the telemetry check on their own tick+// intervals.async function tick(): Promise<void> {const cfg = workerConfig();const claimed = await claimDueDeliveries();@@ -40,20 +37,6 @@ async function tick(): Promise<void> {const removed = await cleanupOldDeliveries();if (removed > 0) console.log(`[worker] cleaned up ${removed} old deliveries`);}- if (++ticksSinceAutoArchive >= cfg.autoArchiveEveryTicks) {- ticksSinceAutoArchive = 0;- const archived = await archiveStaleIssues();- if (archived.length > 0) {- console.log(`[worker] auto-archived ${archived.length} stale issues`);- // An unreachable api must not read as a failed tick: the issues are archived- // either way, and their threads then stay until the agent or project goes.- try {- await deleteIssueAgentThreads(archived);- } catch (error) {- console.error('[worker] deleting agent threads of archived issues failed:', error);- }- }- }if (++ticksSinceTelemetry >= TELEMETRY_CHECK_EVERY_TICKS) {ticksSinceTelemetry = 0;// An unreachable collector must not read as a failed tick.diff --git a/bun.lock b/bun.lockindex 5564b5d4..3c2aba97 100644--- a/bun.lock+++ b/bun.lock@@ -54,6 +54,8 @@"name": "bot","version": "1.0.0","dependencies": {+ "@repo/db": "workspace:*",+ "drizzle-orm": "^0.45.2","grammy": "^1.45.1",},"devDependencies": {@@ -153,6 +155,7 @@"version": "1.0.0","dependencies": {"@repo/db": "workspace:*",+ "@repo/mailer": "workspace:*","@repo/net": "workspace:*","croner": "^10.0.1","drizzle-orm": "^0.45.2",@@ -211,6 +214,8 @@"name": "@repo/db","version": "0.0.0","dependencies": {+ "@repo/crypto": "workspace:*",+ "@repo/mailer": "workspace:*","drizzle-orm": "^0.45.2","postgres": "^3.4.9",},@@ -2968,8 +2973,6 @@"@repo/agent-tools/@types/bun": ["@types/[email protected]", "", { "dependencies": { "bun-types": "1.4.1" } }, "sha512-0AVGiTXGajf1rgKom3N+c5L7CBxuoyyv1i44M0nX4UDK0G/fnRAMiri93nHuVPIb429KKtAgj7HatVmmOjeQLA=="],- "@repo/net/@types/bun": ["@types/[email protected]", "", { "dependencies": { "bun-types": "1.4.1" } }, "sha512-0AVGiTXGajf1rgKom3N+c5L7CBxuoyyv1i44M0nX4UDK0G/fnRAMiri93nHuVPIb429KKtAgj7HatVmmOjeQLA=="],-"@scalar/agent-chat/@scalar/helpers": ["@scalar/[email protected]", "", {}, "sha512-IAfnpIZnXY6ni+zyFMwWHBa5b/7yr4mCSvoTGR84oS9q4Quy2wjgafnr7CyfL65ney3iilBZHigZYLYqdqCu3Q=="],"@scalar/api-client/@scalar/helpers": ["@scalar/[email protected]", "", {}, "sha512-IAfnpIZnXY6ni+zyFMwWHBa5b/7yr4mCSvoTGR84oS9q4Quy2wjgafnr7CyfL65ney3iilBZHigZYLYqdqCu3Q=="],@@ -3064,8 +3067,6 @@"body-parser/content-type": ["[email protected]", "", {}, "sha512-j/O/d7GcZCyNl7/hwZAb606rzqkyvaDctLmckbxLzHvFBzTJHuGEdodATcP3yIRoDrLHkIATJuvzbFlp/ki2cQ=="],- "bot/@types/bun": ["@types/[email protected]", "", { "dependencies": { "bun-types": "1.3.14" } }, "sha512-h1hFqFVcvAvD9j9K7ZW7vd82aSA+rTdznZa+5bwvCwqSB1jmmfLcbIWhOLx1/+boy/xmjgCs/OMUL8hRJSmnPw=="],-"chat/@workflow/serde": ["@workflow/[email protected]", "", {}, "sha512-8kkeoQKLDaKXefjV5dbhBj2aErfKp1Mc4pb6tj8144cF+Em5SPbyMbyLCHp+BVrFfFVCBluCtMx+jjvaFVZGww=="],"cmdk/@radix-ui/react-compose-refs": ["@radix-ui/[email protected]", "", { "peerDependencies": { "@types/react": "*", "react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc" }, "optionalPeers": ["@types/react"] }, "sha512-rYOP8OMnuuPMQF1uhPVlGNcCDlkokKqGFE3JcxFViIkAXP7EvFWUliJAstrapypaBLJNHbZL6jGhbVDGTwmVhA=="],@@ -3166,8 +3167,6 @@"which-builtin-type/isarray": ["[email protected]", "", {}, "sha512-xHjhDr3cNBK0BzdUJSPXZntQUx/mwMS5Rw4A7lPJ90XGAO6ISP/ePDNuo0vhqOZU+UD5JoodwCAAoZQd3FeAKw=="],- "worker/@types/bun": ["@types/[email protected]", "", { "dependencies": { "bun-types": "1.4.1" } }, "sha512-0AVGiTXGajf1rgKom3N+c5L7CBxuoyyv1i44M0nX4UDK0G/fnRAMiri93nHuVPIb429KKtAgj7HatVmmOjeQLA=="],-"zip-stream/archiver-utils": ["[email protected]", "", { "dependencies": { "glob": "^7.2.3", "graceful-fs": "^4.2.0", "lazystream": "^1.0.0", "lodash.defaults": "^4.2.0", "lodash.difference": "^4.5.0", "lodash.flatten": "^4.4.0", "lodash.isplainobject": "^4.0.6", "lodash.union": "^4.6.0", "normalize-path": "^3.0.0", "readable-stream": "^3.6.0" } }, "sha512-KVgf4XQVrTjhyWmx6cte4RxonPLR9onExufI1jhvw/MQ4BB6IsZD5gT8Lq+u/+pRkWna/6JoHpiQioaqFP5Rzw=="],"@ai-sdk/vue/@ai-sdk/provider-utils/@ai-sdk/provider": ["@ai-sdk/[email protected]", "", { "dependencies": { "json-schema": "^0.4.0" } }, "sha512-HrEmNt/BH/hkQ7zpi2o6N3k1ZR1QTb7z85WYhYygiTxOQuaml4CMtHCWRbric5WPU+RNsYI7r1EpyVQMKO1pYw=="],@@ -3224,8 +3223,6 @@"@repo/agent-tools/@types/bun/bun-types": ["[email protected]", "", { "dependencies": { "@types/node": "*" } }, "sha512-loKuVrAFZKfEv+JvWkHRS9GW5IqLuLRjVXN9p+vZvBN86O5hf/pBZQ5hSoyipsrMmWObZBDvWnlmKvjKTM0PdA=="],- "@repo/net/@types/bun/bun-types": ["[email protected]", "", { "dependencies": { "@types/node": "*" } }, "sha512-loKuVrAFZKfEv+JvWkHRS9GW5IqLuLRjVXN9p+vZvBN86O5hf/pBZQ5hSoyipsrMmWObZBDvWnlmKvjKTM0PdA=="],-"@scalar/icons/@types/node/undici-types": ["[email protected]", "", {}, "sha512-AsuCzffGHJybSaRrmr5eHr81mwJU3kjw6M+uprWvCXiNeN9SOGwQ3Jn8jb8m3Z6izVgknn1R0FTCEAP2QrLY/w=="],"@types/nodemailer/@types/node/undici-types": ["[email protected]", "", {}, "sha512-AsuCzffGHJybSaRrmr5eHr81mwJU3kjw6M+uprWvCXiNeN9SOGwQ3Jn8jb8m3Z6izVgknn1R0FTCEAP2QrLY/w=="],@@ -3242,8 +3239,6 @@"archiver-utils/readable-stream/string_decoder": ["[email protected]", "", { "dependencies": { "safe-buffer": "~5.1.0" } }, "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg=="],- "bot/@types/bun/bun-types": ["[email protected]", "", { "dependencies": { "@types/node": "*" } }, "sha512-4N0ig0fEomHt5R0KCFWjovxow98rIoRwKolrYdCcknNwMekCXRnWEUvgu5soYV8QXtVsrUD8B95MBOZGPvr6KQ=="],-"cmdk/@radix-ui/react-dialog/@radix-ui/primitive": ["@radix-ui/[email protected]", "", {}, "sha512-d86WIWFYNtGA0H/d8exstrTRTp7eWJYlYJbtNofxr/3ljupZYn6EFDG/Qgu/0Kc8v7yMUxySagqJsL1+PdYjWg=="],"cmdk/@radix-ui/react-dialog/@radix-ui/react-context": ["@radix-ui/[email protected]", "", { "peerDependencies": { "@types/react": "*", "react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc" }, "optionalPeers": ["@types/react"] }, "sha512-fOE+JtN9rygNZkCnHRBEP0TAvLldlhyOxMsbwFvTP4nAs+nBmfnna+o/Zski2wkmY1YMrFC0aSzsHoLY47iLrg=="],@@ -3372,14 +3367,10 @@"unzipper/readable-stream/string_decoder": ["[email protected]", "", { "dependencies": { "safe-buffer": "~5.1.0" } }, "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg=="],- "worker/@types/bun/bun-types": ["[email protected]", "", { "dependencies": { "@types/node": "*" } }, "sha512-loKuVrAFZKfEv+JvWkHRS9GW5IqLuLRjVXN9p+vZvBN86O5hf/pBZQ5hSoyipsrMmWObZBDvWnlmKvjKTM0PdA=="],-"@itsaplan/runner/@types/bun/bun-types/@types/node": ["@types/[email protected]", "", { "dependencies": { "undici-types": "~6.21.0" } }, "sha512-EANqOCF9QFyra+4pfxUcX9STKJpCLjMbObVzljIJomAWSnuSIEAvyzEU53GaajbXJEgdh0iEcPL+DGvpUd4k1Q=="],"@typescript-eslint/typescript-estree/minimatch/brace-expansion/balanced-match": ["[email protected]", "", {}, "sha512-BLrgEcRTwX2o6gGxGOCNyMvGSp35YofuYzw9h1IMTRmKqttAZZVU67bdb9Pr2vUHA8+j3i2tJfjO6C6+4myGTA=="],- "bot/@types/bun/bun-types/@types/node": ["@types/[email protected]", "", { "dependencies": { "undici-types": "~6.21.0" } }, "sha512-EANqOCF9QFyra+4pfxUcX9STKJpCLjMbObVzljIJomAWSnuSIEAvyzEU53GaajbXJEgdh0iEcPL+DGvpUd4k1Q=="],-"cmdk/@radix-ui/react-dialog/@radix-ui/react-dismissable-layer/@radix-ui/react-use-callback-ref": ["@radix-ui/[email protected]", "", { "peerDependencies": { "@types/react": "*", "react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc" }, "optionalPeers": ["@types/react"] }, "sha512-xCso9j1/u8sEgP1RNHjFrXJLApL8LiqOkI1R4ywuN00rxWdYg4oQXuwKLS3i0j5NWLromUD27/4nlxj2UFVvIw=="],"cmdk/@radix-ui/react-dialog/@radix-ui/react-dismissable-layer/@radix-ui/react-use-effect-event": ["@radix-ui/[email protected]", "", { "dependencies": { "@radix-ui/react-use-layout-effect": "1.1.2" }, "peerDependencies": { "@types/react": "*", "react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc" }, "optionalPeers": ["@types/react"] }, "sha512-6c8ZqvPTWILEKnyVkP53EGRCcpnJiKTC21sS/6R1GF5xKyHJJWQEPfkqlcgUkdRQivd6tb23abUwe4ngWmY0JA=="],@@ -3410,8 +3401,6 @@"@itsaplan/runner/@types/bun/bun-types/@types/node/undici-types": ["[email protected]", "", {}, "sha512-iwDZqg0QAGrg9Rav5H4n0M64c3mkR59cJ6wQp+7C4nI0gsmExaedaYLNO44eT4AtBBwjbTiGPMlt2Md0T9H9JQ=="],- "bot/@types/bun/bun-types/@types/node/undici-types": ["[email protected]", "", {}, "sha512-iwDZqg0QAGrg9Rav5H4n0M64c3mkR59cJ6wQp+7C4nI0gsmExaedaYLNO44eT4AtBBwjbTiGPMlt2Md0T9H9JQ=="],-"cmdk/@radix-ui/react-dialog/@radix-ui/react-dismissable-layer/@radix-ui/react-use-effect-event/@radix-ui/react-use-layout-effect": ["@radix-ui/[email protected]", "", { "peerDependencies": { "@types/react": "*", "react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc" }, "optionalPeers": ["@types/react"] }, "sha512-jrBWOxZITuGcnjRCM2t2U5ZPkCLxD+Ym6DjfssS5haTj2iiak/DOb64JeN6OdLfLgptb6/e2kKR+ZuTrGoZTPA=="],"eslint-plugin-i18n-json/chalk/ansi-styles/color-convert/color-name": ["[email protected]", "", {}, "sha512-72fSenhMw2HZMTVHeCA9KCmpEIbzWiQsjN+BHcBbS9vr1mtt+vJjPdksIBNUmKAW8TFUDPJK5SUU3QhE9NEXDw=="],diff --git a/charts/itsaplan/README.md b/charts/itsaplan/README.mdindex 28e00e9d..a899a641 100644--- a/charts/itsaplan/README.md+++ b/charts/itsaplan/README.md@@ -18,7 +18,6 @@ helm install itsaplan charts/itsaplan \--set secrets.postgresPassword=changeme \--set secrets.betterAuthSecret=$(openssl rand -base64 32) \--set secrets.appEncryptionKey=$(openssl rand -base64 32) \- --set secrets.workerInternalToken=$(openssl rand -base64 32) \--set secrets.s3AccessKeyId=minioadmin \--set secrets.s3SecretAccessKey=minioadmin```@@ -282,7 +281,6 @@ bot:| `secrets.postgresPassword` | PostgreSQL password | `""` || `secrets.betterAuthSecret` | better-auth secret | `""` || `secrets.appEncryptionKey` | AES encryption key for secrets at rest | `""` |-| `secrets.workerInternalToken` | Shared token between api, worker, and bot | `""` || `secrets.s3AccessKeyId` | S3 access key (also MinIO root user) | `""` || `secrets.s3SecretAccessKey` | S3 secret key (also MinIO root password) | `""` |diff --git a/charts/itsaplan/templates/secret.yaml b/charts/itsaplan/templates/secret.yamlindex 0cb4c841..67d185b1 100644--- a/charts/itsaplan/templates/secret.yaml+++ b/charts/itsaplan/templates/secret.yaml@@ -9,7 +9,6 @@ stringData:POSTGRES_PASSWORD: {{ .Values.secrets.postgresPassword | quote }}BETTER_AUTH_SECRET: {{ .Values.secrets.betterAuthSecret | quote }}APP_ENCRYPTION_KEY: {{ .Values.secrets.appEncryptionKey | quote }}- WORKER_INTERNAL_TOKEN: {{ .Values.secrets.workerInternalToken | quote }}S3_ACCESS_KEY_ID: {{ .Values.secrets.s3AccessKeyId | quote }}S3_SECRET_ACCESS_KEY: {{ .Values.secrets.s3SecretAccessKey | quote }}DATABASE_URL: {{ include "itsaplan.databaseUrl" . | quote }}diff --git a/charts/itsaplan/values.yaml b/charts/itsaplan/values.yamlindex c473bf55..e9e7e4b6 100644--- a/charts/itsaplan/values.yaml+++ b/charts/itsaplan/values.yaml@@ -154,7 +154,6 @@ secrets:postgresPassword: ""betterAuthSecret: ""appEncryptionKey: ""- workerInternalToken: ""s3AccessKeyId: ""s3SecretAccessKey: ""diff --git a/docker-compose.coolify-images.yml b/docker-compose.coolify-images.ymlindex 1fba56fe..8a374024 100644--- a/docker-compose.coolify-images.yml+++ b/docker-compose.coolify-images.yml@@ -98,8 +98,6 @@ services:# Encrypts stored AI provider API keys at rest (AES-256-GCM). Changing it makes# existing stored credentials undecryptable.- APP_ENCRYPTION_KEY=${SERVICE_PASSWORD_64_ENCRYPTION}- # The worker and the bot authenticate to /internal/* with this same token.- - WORKER_INTERNAL_TOKEN=${SERVICE_PASSWORD_64_WORKER}# API_URL is the api origin; APP_URL is the web origin.- API_URL=${SERVICE_URL_API}- APP_URL=${SERVICE_URL_WEB}@@ -145,8 +143,10 @@ services:environment:NODE_ENV: productionDATABASE_URL: postgres://${POSTGRES_USER:-itsaplan}:${SERVICE_PASSWORD_POSTGRES}@postgres:5432/${POSTGRES_DB:-itsaplan}- API_URL: http://api:3000- WORKER_INTERNAL_TOKEN: ${SERVICE_PASSWORD_64_WORKER}+ # Notification delivery sends email and Telegram messages itself, with the+ # provider credentials stored in the database — encrypted with this key, the+ # same value the api uses.+ APP_ENCRYPTION_KEY: ${SERVICE_PASSWORD_64_ENCRYPTION}# Anonymous telemetry (TELEMETRY.md). Either variable, set to anything but 0,# turns it off.TELEMETRY_DISABLED: ${TELEMETRY_DISABLED:-}@@ -158,12 +158,12 @@ services:# WEBHOOK_POLL_INTERVAL_MS, WEBHOOK_BATCH_SIZE, WEBHOOK_TIMEOUT_MS,# WEBHOOK_MAX_ATTEMPTS, WEBHOOK_DISABLE_THRESHOLD, WEBHOOK_LEASE_SECONDS,# WEBHOOK_CLEANUP_DAYS, WEBHOOK_CLEANUP_EVERY_TICKS.- # Agent runs: AGENT_RUN_BATCH_SIZE, AGENT_RUN_TIMEOUT_MS,- # AGENT_RUN_MAX_ATTEMPTS, AGENT_RUN_LEASE_SECONDS.+ # Agent schedules: AGENT_RUN_POLL_INTERVAL_MS. The api runs the queued runs+ # and reads the other AGENT_RUN_* values.# Telegram bot: long polling for the instance bot, completing the account links- # users start from their profile. No database and no public ports — it reaches the- # api's internal routes with the same shared token the worker uses.+ # users start from their profile. It reads the bot token and writes the links in the+ # database, and holds no api credential — no public ports either.## Must stay at one replica: Telegram hands each getUpdates call to a single caller,# so a second instance would take updates away from the first.@@ -172,16 +172,16 @@ services:restart: unless-stoppedexclude_from_hc: truedepends_on:- api:- condition: service_started+ postgres:+ condition: service_healthyenvironment:NODE_ENV: production- API_URL: http://api:3000- WORKER_INTERNAL_TOKEN: ${SERVICE_PASSWORD_64_WORKER}+ DATABASE_URL: postgres://${POSTGRES_USER:-itsaplan}:${SERVICE_PASSWORD_POSTGRES}@postgres:5432/${POSTGRES_DB:-itsaplan}+ APP_ENCRYPTION_KEY: ${SERVICE_PASSWORD_64_ENCRYPTION}# The bot token itself is not set here: it is stored in the database and edited# in god mode, so the service picks up a change without a redeploy.# Optional tuning — defaults live in apps/bot/src/config.ts:- # BOT_CONFIG_POLL_INTERVAL_MS, BOT_API_TIMEOUT_MS.+ # BOT_CONFIG_POLL_INTERVAL_MS, BOT_CONFIG_RETRY_INTERVAL_MS.web:image: ghcr.io/croffasia/itsaplan-web:${VERSION:-latest}diff --git a/docker-compose.coolify.yml b/docker-compose.coolify.ymlindex 1d8673b7..0c453abd 100644--- a/docker-compose.coolify.yml+++ b/docker-compose.coolify.yml@@ -93,8 +93,6 @@ services:# Encrypts stored AI provider API keys at rest (AES-256-GCM). Changing it makes# existing stored credentials undecryptable.APP_ENCRYPTION_KEY: ${SERVICE_PASSWORD_64_ENCRYPTION}- # The worker and the bot authenticate to /internal/* with this same token.- WORKER_INTERNAL_TOKEN: ${SERVICE_PASSWORD_64_WORKER}# API_URL is the api origin; APP_URL is the web origin.API_URL: ${SERVICE_URL_API:-http://localhost:3000}APP_URL: ${SERVICE_URL_WEB:-http://localhost:3001}@@ -145,8 +143,10 @@ services:environment:NODE_ENV: productionDATABASE_URL: postgres://${POSTGRES_USER:-itsaplan}:${SERVICE_PASSWORD_POSTGRES}@postgres:5432/${POSTGRES_DB:-itsaplan}- SERVICE_URL_API: ${SERVICE_URL_API:-http://api:3000}- WORKER_INTERNAL_TOKEN: ${SERVICE_PASSWORD_64_WORKER}+ # Notification delivery sends email and Telegram messages itself, with the+ # provider credentials stored in the database — encrypted with this key, the+ # same value the api uses.+ APP_ENCRYPTION_KEY: ${SERVICE_PASSWORD_64_ENCRYPTION}# Anonymous telemetry (TELEMETRY.md). Either variable, set to anything but 0,# turns it off.TELEMETRY_DISABLED: ${TELEMETRY_DISABLED:-}@@ -158,12 +158,12 @@ services:# WEBHOOK_POLL_INTERVAL_MS, WEBHOOK_BATCH_SIZE, WEBHOOK_TIMEOUT_MS,# WEBHOOK_MAX_ATTEMPTS, WEBHOOK_DISABLE_THRESHOLD, WEBHOOK_LEASE_SECONDS,# WEBHOOK_CLEANUP_DAYS, WEBHOOK_CLEANUP_EVERY_TICKS.- # Agent runs: AGENT_RUN_BATCH_SIZE, AGENT_RUN_TIMEOUT_MS,- # AGENT_RUN_MAX_ATTEMPTS, AGENT_RUN_LEASE_SECONDS.+ # Agent schedules: AGENT_RUN_POLL_INTERVAL_MS. The api runs the queued runs+ # and reads the other AGENT_RUN_* values.# Telegram bot: long polling for the instance bot, completing the account links- # users start from their profile. No database and no public ports — it reaches the- # api's internal routes with the same shared token the worker uses.+ # users start from their profile. It reads the bot token and writes the links in the+ # database, and holds no api credential — no public ports either.## Must stay at one replica: Telegram hands each getUpdates call to a single caller,# so a second instance would take updates away from the first.@@ -174,16 +174,16 @@ services:restart: unless-stoppedexclude_from_hc: truedepends_on:- api:- condition: service_started+ postgres:+ condition: service_healthyenvironment:NODE_ENV: production- SERVICE_URL_API: ${SERVICE_URL_API:-http://api:3000}- WORKER_INTERNAL_TOKEN: ${SERVICE_PASSWORD_64_WORKER}+ DATABASE_URL: postgres://${POSTGRES_USER:-itsaplan}:${SERVICE_PASSWORD_POSTGRES}@postgres:5432/${POSTGRES_DB:-itsaplan}+ APP_ENCRYPTION_KEY: ${SERVICE_PASSWORD_64_ENCRYPTION}# The bot token itself is not set here: it is stored in the database and edited# in god mode, so the service picks up a change without a redeploy.# Optional tuning — defaults live in apps/bot/src/config.ts:- # BOT_CONFIG_POLL_INTERVAL_MS, BOT_API_TIMEOUT_MS.+ # BOT_CONFIG_POLL_INTERVAL_MS, BOT_CONFIG_RETRY_INTERVAL_MS.web:build:diff --git a/docker-compose.test.yml b/docker-compose.test.ymlindex f0f9d941..c38df61a 100644--- a/docker-compose.test.yml+++ b/docker-compose.test.yml@@ -62,8 +62,11 @@ services:context: .dockerfile: apps/api/Dockerfile# Empty install flags so the image keeps devDeps the test suite needs- # (@elysiajs/eden); production builds default to --production.+ # (@elysiajs/eden); production builds default to --production. The bot and+ # worker suites run out of this image too, so their workspaces are installed+ # as well — the production image keeps only api's closure.args:+ INSTALL_FILTER: '--filter api --filter bot --filter worker'INSTALL_FLAGS: ''depends_on:postgres-test:@@ -85,9 +88,9 @@ services:S3_BUCKET: planner-attachmentsS3_ACCESS_KEY_ID: minioadminS3_SECRET_ACCESS_KEY: minioadmin- # Migrate the throwaway DB, then run the api suite. bun test discovers- # src/**/*.test.ts; env above satisfies the resetDb() guard.- command: sh -c "bun run packages/db/src/migrate.ts && cd apps/api && bun test"+ # Migrate the throwaway DB, then run the api, bot and worker suites. bun test+ # discovers src/**/*.test.ts; env above satisfies the resetDb() guard.+ command: sh -c "bun run packages/db/src/migrate.ts && cd apps/api && bun test && cd ../bot && bun test && cd ../worker && bun test"# Webhook delivery worker, built from the same image as production, so the test# gate also verifies the worker image builds and boots. It idles here (no eventsdiff --git a/docker-compose.yml b/docker-compose.ymlindex 67dc1e50..b457828e 100644--- a/docker-compose.yml+++ b/docker-compose.yml@@ -7,7 +7,7 @@# docker compose up -d## Required in .env (the stack refuses to start without them):-# POSTGRES_PASSWORD, BETTER_AUTH_SECRET, APP_ENCRYPTION_KEY, WORKER_INTERNAL_TOKEN,+# POSTGRES_PASSWORD, BETTER_AUTH_SECRET, APP_ENCRYPTION_KEY,# S3_ACCESS_KEY_ID, S3_SECRET_ACCESS_KEY, API_URL, APP_URL# Generate each secret with: openssl rand -base64 32#@@ -92,8 +92,6 @@ services:# Encrypts stored provider credentials at rest (AES-256-GCM). Changing it# makes existing stored credentials undecryptable.APP_ENCRYPTION_KEY: ${APP_ENCRYPTION_KEY:?set APP_ENCRYPTION_KEY in .env}- # Authenticates the worker and the bot against the /internal/* routes.- WORKER_INTERNAL_TOKEN: ${WORKER_INTERNAL_TOKEN:?set WORKER_INTERNAL_TOKEN in .env}# Public origins, each entered once. API_URL is the api origin (auth handler,# email links); APP_URL is the web origin (CORS, passkey, email links).API_URL: ${API_URL:?public origin of the api, e.g. https://api.example.com}@@ -145,8 +143,10 @@ services:environment:NODE_ENV: productionDATABASE_URL: postgres://${POSTGRES_USER:-itsaplan}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB:-itsaplan}- SERVICE_URL_API: http://api:3000- WORKER_INTERNAL_TOKEN: ${WORKER_INTERNAL_TOKEN}+ # Notification delivery sends email and Telegram messages itself, with the+ # provider credentials stored in the database — encrypted with this key, the+ # same value the api uses.+ APP_ENCRYPTION_KEY: ${APP_ENCRYPTION_KEY:?set APP_ENCRYPTION_KEY in .env}# Anonymous telemetry (TELEMETRY.md). Either variable, set to anything but 0,# turns it off.TELEMETRY_DISABLED: ${TELEMETRY_DISABLED:-}@@ -169,12 +169,12 @@ services:dockerfile: apps/bot/Dockerfilerestart: unless-stoppeddepends_on:- api:- condition: service_started+ postgres:+ condition: service_healthyenvironment:NODE_ENV: production- SERVICE_URL_API: http://api:3000- WORKER_INTERNAL_TOKEN: ${WORKER_INTERNAL_TOKEN}+ DATABASE_URL: postgres://${POSTGRES_USER:-itsaplan}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB:-itsaplan}+ APP_ENCRYPTION_KEY: ${APP_ENCRYPTION_KEY:?set APP_ENCRYPTION_KEY in .env}# The origins are read at startup and handed to the browser by the server render,# so the image carries none of them and a changed API_URL only needs a restart.diff --git a/docs/helm.md b/docs/helm.mdindex 01133c71..e1ca8bef 100644--- a/docs/helm.md+++ b/docs/helm.md@@ -23,7 +23,6 @@ secrets:postgresPassword: "" # openssl rand -base64 32betterAuthSecret: "" # openssl rand -base64 32appEncryptionKey: "" # openssl rand -base64 32- workerInternalToken: "" # openssl rand -base64 32s3AccessKeyId: "minioadmin"s3SecretAccessKey: "minioadmin"```diff --git a/docs/self-hosting.md b/docs/self-hosting.mdindex ccfa1cd8..c6f2e5a1 100644--- a/docs/self-hosting.md+++ b/docs/self-hosting.md@@ -19,7 +19,6 @@ The stack refuses to start while one of these is missing:| `POSTGRES_PASSWORD` | `openssl rand -base64 32` || `BETTER_AUTH_SECRET` | `openssl rand -base64 32` || `APP_ENCRYPTION_KEY` | `openssl rand -base64 32` |-| `WORKER_INTERNAL_TOKEN` | `openssl rand -base64 32` || `S3_ACCESS_KEY_ID` | the MinIO root user, any name over 3 chars || `S3_SECRET_ACCESS_KEY` | `openssl rand -base64 32` |diff --git a/package.json b/package.jsonindex b0354d9e..5e8c54b0 100644--- a/package.json+++ b/package.json@@ -24,7 +24,7 @@"format": "prettier . --write","format:check": "prettier . --check","typecheck": "turbo run typecheck && tsc --noEmit",- "test": "bun test scripts/ && turbo run test",+ "test": "bun test scripts/ && turbo run test --concurrency=1","test:coverage": "cd apps/api && bun run test:coverage","db:generate": "bun --filter='@repo/db' run db:generate","db:migrate": "bun --filter='@repo/db' run db:migrate",diff --git a/packages/auth/AGENTS.md b/packages/auth/AGENTS.mdindex ba4c7430..8eac3805 100644--- a/packages/auth/AGENTS.md+++ b/packages/auth/AGENTS.md@@ -28,6 +28,10 @@ through this module — never inline a query on `app_setting` / `app_secret` elsprovider, the two OAuth providers and the SCIM token, encrypted with `@repo/crypto`,each with a `redacted` mirror for the settings UI. Secrets never leave the server.+The mail provider is read by the api and the worker as well, so its shape and reader+live in `@repo/db` (`domains/instance-email.ts`); what is here is the write side god+mode drives.+"Invite only" means the address has a pending `team_invite` (`hasPendingInvite`).Invites are created and revoked inside a team, so there is no instance-level invitetable and god mode has no invite section — do not add one.diff --git a/packages/auth/src/index.ts b/packages/auth/src/index.tsindex c759c92d..68d2189d 100644--- a/packages/auth/src/index.ts+++ b/packages/auth/src/index.ts@@ -1,5 +1,5 @@import { randomInt } from 'node:crypto';-import { db, defaultMemberPermissions } from '@repo/db';+import { db, defaultMemberPermissions, hasConfiguredEmailProvider } from '@repo/db';import { eq, sql, type SQL } from 'drizzle-orm';import { betterAuth } from 'better-auth';import { createAuthMiddleware, APIError } from 'better-auth/api';@@ -16,7 +16,6 @@ import {isGoogleUsable,getOidcConfig,isOidcUsable,- hasConfiguredEmailProvider,} from './instance';import { sendAuthEmail } from './mail';@@ -648,10 +647,7 @@ export {setAuthSettings,getEmailSettings,setEmailSettings,- getEmailConfig,resolveEmailConfig,- getProjectEmailConfig,- hasConfiguredEmailProvider,getGoogleSettings,setGoogleSettings,getGoogleConfig,@@ -672,7 +668,6 @@ export type {AuthSettings,InstanceEmailDto,InstanceEmailPatch,- InstanceEmailConfig,InstanceGoogleDto,InstanceGooglePatch,InstanceGoogleConfig,diff --git a/packages/auth/src/instance.ts b/packages/auth/src/instance.tsindex c1b6426e..9f3e8317 100644--- a/packages/auth/src/instance.ts+++ b/packages/auth/src/instance.ts@@ -1,13 +1,18 @@import { randomBytes, timingSafeEqual } from 'node:crypto';-import { db, appSecret, teamInvite, getSetting, setSetting } from '@repo/db';-import { and, eq, sql } from 'drizzle-orm';-import { encryptSecret, decryptSecret } from '@repo/crypto';import {- hasEmailProvider,- type SmtpConfig,- type ResendConfig,- type EmailConfig,-} from '@repo/mailer';+ db,+ teamInvite,+ getSetting,+ setSetting,+ readSecret,+ writeSecret,+ defaultInstanceEmailConfig,+ getInstanceEmailConfig,+ INSTANCE_EMAIL_SECRET_KEY,+ type InstanceEmailConfig,+} from '@repo/db';+import { and, eq } from 'drizzle-orm';+import type { SmtpConfig } from '@repo/mailer';// Instance-wide authentication settings: who may register, whether email has to be// confirmed, which sign-in methods are offered, the mail provider used for@@ -19,10 +24,10 @@ import {// Non-secret settings are one jsonb blob in app_setting under the 'auth' key; the// credentials are encrypted in app_secret under 'auth.email', 'auth.google',// 'auth.oidc' and 'auth.scim', each with a `redacted` mirror the settings UI can read-// without decrypting.+// without decrypting. The mail config is also read by the api and the worker, so its+// shape and reader live in @repo/db; what stays here is the write side.const AUTH_SETTING_KEY = 'auth';-const EMAIL_SECRET_KEY = 'auth.email';const GOOGLE_SECRET_KEY = 'auth.google';const OIDC_SECRET_KEY = 'auth.oidc';const SCIM_SECRET_KEY = 'auth.scim';@@ -77,60 +82,12 @@ export async function setAuthSettings(patch: Partial<AuthSettings>): Promise<Aut// ── Encrypted config storage ──────────────────────────────────────────────────-// Every credential is stored the same way: one JSON blob per key in app_secret,-// encrypted as a whole, with a `redacted` mirror the settings UI reads without-// decrypting.--async function readSecret<T>(key: string): Promise<T | null> {- const rows = await db- .select({ ciphertext: appSecret.ciphertext, iv: appSecret.iv, authTag: appSecret.authTag })- .from(appSecret)- .where(eq(appSecret.key, key));- const row = rows[0];- return row ? (JSON.parse(decryptSecret(row)) as T) : null;-}--async function writeSecret(key: string, value: unknown, redacted: object): Promise<void> {- const enc = encryptSecret(JSON.stringify(value));- await db- .insert(appSecret)- .values({- key,- ciphertext: enc.ciphertext,- iv: enc.iv,- authTag: enc.authTag,- redacted,- })- .onConflictDoUpdate({- target: appSecret.key,- set: {- ciphertext: enc.ciphertext,- iv: enc.iv,- authTag: enc.authTag,- redacted,- updatedAt: sql`now()`,- },- });-}-function mergeSecret(current: string, next: string | undefined): string {return next && next.length > 0 ? next : current;}// ── Mail provider ─────────────────────────────────────────────────────────────-// The stored, decrypted config. Secret fields carry the plaintext value; read only-// by the sender, never returned over HTTP.-export interface InstanceEmailConfig extends EmailConfig {- smtp: SmtpConfig;- resend: ResendConfig;- from: string;- // Let projects send their notifications through this provider instead of- // configuring one of their own. Off by default: the instance owner pays for the- // provider, so sharing it is an explicit decision.- allowProjects: boolean;-}-// The config as returned to the client: every secret replaced by a boolean telling// whether a value is stored.export interface InstanceEmailDto {@@ -166,23 +123,6 @@ export interface InstanceEmailPatch {allowProjects?: boolean;}-function defaultEmailConfig(): InstanceEmailConfig {- return {- smtp: {- enabled: false,- host: '',- port: null,- encryption: 'none',- username: '',- password: '',- timeout: null,- },- resend: { enabled: false, apiKey: '' },- from: '',- allowProjects: false,- };-}-function toEmailDto(config: InstanceEmailConfig): InstanceEmailDto {return {smtp: {@@ -200,33 +140,8 @@ function toEmailDto(config: InstanceEmailConfig): InstanceEmailDto {};}-// Reads and decrypts the stored mail config, or null when nothing has been saved.-export async function getEmailConfig(): Promise<InstanceEmailConfig | null> {- const stored = await readSecret<InstanceEmailConfig>(EMAIL_SECRET_KEY);- if (!stored) return null;- // Merge over the default so a config written before a field was added stays valid.- return { ...defaultEmailConfig(), ...stored };-}-export async function getEmailSettings(): Promise<InstanceEmailDto> {- return toEmailDto((await getEmailConfig()) ?? defaultEmailConfig());-}--// Whether outbound mail can be sent right now. Everything that mails the user-// (password reset, address confirmation, sign-in links) is unavailable without it,-// so both the god settings and the public sign-in screens ask this first.-export async function hasConfiguredEmailProvider(): Promise<boolean> {- const config = await getEmailConfig();- return config ? hasEmailProvider(config) : false;-}--// The instance provider a project may send its notifications through, or null when-// projects are not allowed to use it or it is not configured. Read by the api on the-// project notification paths (which channels are enabled, and the actual send).-export async function getProjectEmailConfig(): Promise<InstanceEmailConfig | null> {- const config = await getEmailConfig();- if (!config || !config.allowProjects) return null;- return hasEmailProvider(config) ? config : null;+ return toEmailDto((await getInstanceEmailConfig()) ?? defaultInstanceEmailConfig());}// Resolve a prospective configuration without persisting it. The email test route uses@@ -235,7 +150,7 @@ export async function getProjectEmailConfig(): Promise<InstanceEmailConfig | nulexport async function resolveEmailConfig(patch: InstanceEmailPatch = {},): Promise<InstanceEmailConfig> {- const current = (await getEmailConfig()) ?? defaultEmailConfig();+ const current = (await getInstanceEmailConfig()) ?? defaultInstanceEmailConfig();return {smtp: patch.smtp? { ...patch.smtp, password: mergeSecret(current.smtp.password, patch.smtp.password) }@@ -254,7 +169,7 @@ export async function resolveEmailConfig(export async function setEmailSettings(patch: InstanceEmailPatch): Promise<InstanceEmailDto> {const next = await resolveEmailConfig(patch);const redacted = toEmailDto(next);- await writeSecret(EMAIL_SECRET_KEY, next, redacted);+ await writeSecret(INSTANCE_EMAIL_SECRET_KEY, next, redacted);return redacted;}diff --git a/packages/auth/src/mail.ts b/packages/auth/src/mail.tsindex c2e9a49c..7f290f8f 100644--- a/packages/auth/src/mail.ts+++ b/packages/auth/src/mail.ts@@ -1,5 +1,5 @@import { sendEmail, emailBody } from '@repo/mailer';-import { getEmailConfig } from './instance';+import { getInstanceEmailConfig } from '@repo/db';// Authentication email: password reset, address verification, magic link. It uses// the instance mail provider configured in god mode, which is separate from the@@ -15,7 +15,7 @@ export async function sendAuthEmail(input: {text: string;url?: string;}): Promise<boolean> {- const config = await getEmailConfig();+ const config = await getInstanceEmailConfig();if (!config) {console.warn('[auth] no email provider configured, dropping mail:', input.subject);return false;diff --git a/packages/db/AGENTS.md b/packages/db/AGENTS.mdindex bc71ba7a..f113ac7d 100644--- a/packages/db/AGENTS.md+++ b/packages/db/AGENTS.md@@ -13,6 +13,16 @@ See root `AGENTS.md` for monorepo-wide rules.- `src/permissions.ts` — the permission matrix stored in `team_role.permissions`: theresource/action catalog, the default member role, and the normalizer. It lives herebecause the API and the sign-up hook in `@repo/auth` both write it.+- `src/settings.ts` / `src/secrets.ts` — the two instance config stores: `app_setting`+ in plaintext jsonb, `app_secret` encrypted with `APP_ENCRYPTION_KEY`. Both are read+ by more than one app, which is why they are here.+- `src/domains/` — one module per stored config that more than one process reads: its+ shape, its defaults and the reader that decrypts it. Only that. The write side, the+ redacted view an HTTP client gets, the validation and anything else specific to one+ caller stay with that caller — the api's settings routes, or god mode in+ `@repo/auth`. A config only one process reads does not belong here at all.+ Currently: the instance Telegram bot (api, bot, worker), the instance mail provider+ (`@repo/auth`, api, worker), and a team's notification providers (api, worker).- `src/migrate.ts` — programmatic migrator run on api container startup (no drizzle-kit in prod).- `drizzle/` — generated SQL migrations (committed).diff --git a/packages/db/package.json b/packages/db/package.jsonindex 2bca2fc1..974b5599 100644--- a/packages/db/package.json+++ b/packages/db/package.json@@ -15,6 +15,8 @@"typecheck": "tsc --noEmit"},"dependencies": {+ "@repo/crypto": "workspace:*",+ "@repo/mailer": "workspace:*","drizzle-orm": "^0.45.2","postgres": "^3.4.9"},diff --git a/packages/db/src/domains/instance-email.ts b/packages/db/src/domains/instance-email.tsnew file mode 100644index 00000000..ac06fee6--- /dev/null+++ b/packages/db/src/domains/instance-email.ts@@ -0,0 +1,68 @@+import {+ hasEmailProvider,+ type EmailConfig,+ type ResendConfig,+ type SmtpConfig,+} from '@repo/mailer';+import { readSecret } from '../secrets';++// The instance mail provider, stored encrypted in app_secret under 'auth.email'.+// Three processes read it: @repo/auth (authentication mail), the api (the invite+// email, and whether email features are offered at all), and the worker (a team that+// sends its notifications through the instance provider). God mode in the api owns+// the write, so a field added here has to be set there too.++export const INSTANCE_EMAIL_SECRET_KEY = 'auth.email';++// The stored, decrypted config. Secret fields carry the plaintext value; read only+// by the senders, never returned over HTTP.+export interface InstanceEmailConfig extends EmailConfig {+ smtp: SmtpConfig;+ resend: ResendConfig;+ from: string;+ // Let projects send their notifications through this provider instead of+ // configuring one of their own. Off by default: the instance owner pays for the+ // provider, so sharing it is an explicit decision.+ allowProjects: boolean;+}++export function defaultInstanceEmailConfig(): InstanceEmailConfig {+ return {+ smtp: {+ enabled: false,+ host: '',+ port: null,+ encryption: 'none',+ username: '',+ password: '',+ timeout: null,+ },+ resend: { enabled: false, apiKey: '' },+ from: '',+ allowProjects: false,+ };+}++// The stored config, or null when nothing has been saved.+export async function getInstanceEmailConfig(): Promise<InstanceEmailConfig | null> {+ const stored = await readSecret<InstanceEmailConfig>(INSTANCE_EMAIL_SECRET_KEY);+ if (!stored) return null;+ // Merge over the default so a config written before a field was added stays valid.+ return { ...defaultInstanceEmailConfig(), ...stored };+}++// Whether outbound mail can be sent right now. Everything that mails the user+// (password reset, address confirmation, sign-in links) is unavailable without it,+// so both the god settings and the public sign-in screens ask this first.+export async function hasConfiguredEmailProvider(): Promise<boolean> {+ const config = await getInstanceEmailConfig();+ return config ? hasEmailProvider(config) : false;+}++// The instance provider a project may send its notifications through, or null when+// projects are not allowed to use it or it is not configured.+export async function getProjectEmailConfig(): Promise<InstanceEmailConfig | null> {+ const config = await getInstanceEmailConfig();+ if (!config || !config.allowProjects) return null;+ return hasEmailProvider(config) ? config : null;+}diff --git a/packages/db/src/domains/notification-settings.ts b/packages/db/src/domains/notification-settings.tsnew file mode 100644index 00000000..b17464eb--- /dev/null+++ b/packages/db/src/domains/notification-settings.ts@@ -0,0 +1,84 @@+import { eq } from 'drizzle-orm';+import { decryptSecret } from '@repo/crypto';+import type { ResendConfig, SmtpConfig } from '@repo/mailer';+import { db } from '../client';+import { teamNotificationSetting } from '../schema/app';++// A team's notification provider credentials, stored encrypted as one JSON blob in+// team_notification_setting: the channels every project of the team delivers through.+// Read by the api (the settings UI reads the plaintext `redacted` mirror instead, and+// only the enabled flags) and by the worker, which sends with them. The api owns the+// write, so a field added here has to be set there too.++interface TelegramConfig {+ enabled: boolean;+ botToken: string; // secret+}++// The stored, decrypted config. Secret fields carry the plaintext value; read only+// by the sender, never returned over HTTP.+export interface NotificationConfig {+ // Send email through the instance provider instead of the team's own. Carries+ // no credentials: they belong to the instance and are read at send time. A team+ // that enables SMTP or Resend of its own takes precedence over this.+ system: { enabled: boolean };+ smtp: SmtpConfig;+ resend: ResendConfig;+ telegram: TelegramConfig;+}++export function defaultNotificationConfig(): NotificationConfig {+ return {+ // A team sends through the instance provider until it configures its own, so+ // notifications work out of the box wherever the instance shares one.+ system: { enabled: true },+ smtp: {+ enabled: false,+ host: '',+ port: 587,+ encryption: 'none',+ username: '',+ password: '',+ timeout: null,+ },+ resend: { enabled: false, apiKey: '' },+ telegram: { enabled: false, botToken: '' },+ };+}++// Which provider sends this team's email: its own SMTP/Resend when one is+// enabled, otherwise the instance provider when the team asked for it. 'none'+// means email delivery is off for the team.+export function emailSource(config: {+ system: { enabled: boolean };+ smtp: { enabled: boolean };+ resend: { enabled: boolean };+}): 'smtp' | 'resend' | 'system' | 'none' {+ if (config.smtp.enabled) return 'smtp';+ if (config.resend.enabled) return 'resend';+ return config.system.enabled ? 'system' : 'none';+}++// The stored config, or null when the team has none yet.+export async function readNotificationConfig(teamId: number): Promise<NotificationConfig | null> {+ const rows = await db+ .select({+ ciphertext: teamNotificationSetting.ciphertext,+ iv: teamNotificationSetting.iv,+ authTag: teamNotificationSetting.authTag,+ })+ .from(teamNotificationSetting)+ .where(eq(teamNotificationSetting.teamId, teamId));+ const row = rows[0];+ if (!row) return null;+ // Merge over the default so a config written before a field was added stays valid.+ return {+ ...defaultNotificationConfig(),+ ...(JSON.parse(decryptSecret(row)) as NotificationConfig),+ };+}++// The full decrypted config, for the sender that actually delivers a notification.+export async function getDeliveryConfig(teamId: number): Promise<NotificationConfig> {+ return (await readNotificationConfig(teamId)) ?? defaultNotificationConfig();+}diff --git a/packages/db/src/domains/telegram-bot.ts b/packages/db/src/domains/telegram-bot.tsnew file mode 100644index 00000000..f81a44cd--- /dev/null+++ b/packages/db/src/domains/telegram-bot.ts@@ -0,0 +1,29 @@+import { readSecret } from '../secrets';++// The instance Telegram bot, stored encrypted in app_secret under 'telegram.bot'.+// Three processes read it: the api (settings UI, account linking), the bot (long+// polling), and the worker (Telegram delivery falls back to this bot when a team set+// no token of its own). The api owns the write, so a field added here has to be set+// there too.++export const TELEGRAM_BOT_SECRET_KEY = 'telegram.bot';++export interface InstanceBotConfig {+ enabled: boolean;+ botToken: string; // secret+ // Resolved from getMe when the token is saved, so the deep link can be built+ // without asking the administrator to type the name a second time.+ botUsername: string;+}++export async function getInstanceBotConfig(): Promise<InstanceBotConfig> {+ const stored = await readSecret<InstanceBotConfig>(TELEGRAM_BOT_SECRET_KEY);+ // Merge over the default so a config written before a field was added stays valid.+ return { enabled: false, botToken: '', botUsername: '', ...stored };+}++// Whether the instance bot can be used right now. Account linking is offered only+// when this is true, and Telegram delivery falls back to this bot only when it is.+export function isInstanceBotUsable(config: InstanceBotConfig): boolean {+ return config.enabled && config.botToken.length > 0;+}diff --git a/packages/db/src/index.ts b/packages/db/src/index.tsindex c605f8c2..74621f61 100644--- a/packages/db/src/index.ts+++ b/packages/db/src/index.ts@@ -2,3 +2,25 @@ export { db } from './client';export * from './schema';export * from './permissions';export { getSetting, getOrCreateSetting, setSetting } from './settings';+export { readSecret, writeSecret } from './secrets';+export {+ TELEGRAM_BOT_SECRET_KEY,+ getInstanceBotConfig,+ isInstanceBotUsable,+ type InstanceBotConfig,+} from './domains/telegram-bot';+export {+ INSTANCE_EMAIL_SECRET_KEY,+ defaultInstanceEmailConfig,+ getInstanceEmailConfig,+ getProjectEmailConfig,+ hasConfiguredEmailProvider,+ type InstanceEmailConfig,+} from './domains/instance-email';+export {+ defaultNotificationConfig,+ emailSource,+ getDeliveryConfig,+ readNotificationConfig,+ type NotificationConfig,+} from './domains/notification-settings';diff --git a/packages/db/src/schema/app.ts b/packages/db/src/schema/app.tsindex 47fa03c4..cc501b9e 100644--- a/packages/db/src/schema/app.ts+++ b/packages/db/src/schema/app.ts@@ -905,6 +905,24 @@ export const userTelegramAccount = pgTable(// from team_notification_setting at send time. channel is 'email' | 'telegram'// ('email' picks SMTP or Resend from the team config). recipient is the member's// email address for email rows, or their Telegram chat id for telegram rows.+// The stored message on a notification_delivery row, composed at enqueue time by the+// api and read by the worker that sends it. `subject`/`html` are channel-specific:+// email uses `subject` and builds its own HTML from `text`; Telegram sends `html`+// (parse_mode HTML) and falls back to `text`. The sender appends `url` to plain-text+// bodies. `dedupeKey` is what the enqueue side matches to avoid queuing the same+// message twice; `projectInviteId` ties an invite email to its invite, so a delivery+// whose invite is no longer pending is dropped instead of sent.+export interface DeliveryPayload {+ subject?: string;+ text: string;+ html?: string;+ url?: string;+ emailSource?: 'project' | 'instance';+ idempotencyKey?: string;+ dedupeKey?: string;+ projectInviteId?: number;+}+export const notificationDelivery = pgTable('notification_delivery',{@@ -914,8 +932,7 @@ export const notificationDelivery = pgTable(.references(() => project.id, { onDelete: 'cascade' }),channel: text('channel').notNull(),recipient: text('recipient'),- // Composed message: { subject?, text, html?, url? }. Owned by the sender.- payload: jsonb('payload').notNull(),+ payload: jsonb('payload').$type<DeliveryPayload>().notNull(),status: text('status').notNull().default('pending'),attempts: integer('attempts').notNull().default(0),nextAttemptAt: timestamp('next_attempt_at', { withTimezone: true }).notNull().defaultNow(),diff --git a/packages/db/src/secrets.ts b/packages/db/src/secrets.tsnew file mode 100644index 00000000..5af9a2ce--- /dev/null+++ b/packages/db/src/secrets.ts@@ -0,0 +1,43 @@+import { eq, sql } from 'drizzle-orm';+import { decryptSecret, encryptSecret } from '@repo/crypto';+import { db } from './client';+import { appSecret } from './schema/app';++// Encrypted instance configuration (app_secret): one JSON blob per key, encrypted as+// a whole, with a `redacted` mirror the settings UI reads without decrypting. The+// plaintext counterpart is settings.ts (app_setting).+//+// It lives here because the api, @repo/auth and the bot all go through it, and only a+// process holding APP_ENCRYPTION_KEY can read a value back.++export async function readSecret<T>(key: string): Promise<T | null> {+ const rows = await db+ .select({ ciphertext: appSecret.ciphertext, iv: appSecret.iv, authTag: appSecret.authTag })+ .from(appSecret)+ .where(eq(appSecret.key, key));+ const row = rows[0];+ return row ? (JSON.parse(decryptSecret(row)) as T) : null;+}++export async function writeSecret(key: string, value: unknown, redacted: object): Promise<void> {+ const enc = encryptSecret(JSON.stringify(value));+ await db+ .insert(appSecret)+ .values({+ key,+ ciphertext: enc.ciphertext,+ iv: enc.iv,+ authTag: enc.authTag,+ redacted,+ })+ .onConflictDoUpdate({+ target: appSecret.key,+ set: {+ ciphertext: enc.ciphertext,+ iv: enc.iv,+ authTag: enc.authTag,+ redacted,+ updatedAt: sql`now()`,+ },+ });+}diff --git a/scripts/env-file.test.ts b/scripts/env-file.test.tsindex 8a169fbf..5de6118b 100644--- a/scripts/env-file.test.ts+++ b/scripts/env-file.test.ts@@ -52,15 +52,15 @@ describe('EnvFile', () => {test('generates over an example value and keeps a real one', () => {const env = write(- 'BETTER_AUTH_SECRET=change-me\nAPP_ENCRYPTION_KEY=\nWORKER_INTERNAL_TOKEN=kept\n',+ 'BETTER_AUTH_SECRET=change-me\nAPP_ENCRYPTION_KEY=\nS3_SECRET_ACCESS_KEY=kept\n',);- ['BETTER_AUTH_SECRET', 'APP_ENCRYPTION_KEY', 'WORKER_INTERNAL_TOKEN'].forEach((key) =>+ ['BETTER_AUTH_SECRET', 'APP_ENCRYPTION_KEY', 'S3_SECRET_ACCESS_KEY'].forEach((key) =>env.generate(key),);expect(env.get('BETTER_AUTH_SECRET')).not.toBe('change-me');expect(env.get('APP_ENCRYPTION_KEY')).toHaveLength(44);- expect(env.get('WORKER_INTERNAL_TOKEN')).toBe('kept');+ expect(env.get('S3_SECRET_ACCESS_KEY')).toBe('kept');});test('fresh reads the example, ignoring the file next to it', () => {diff --git a/scripts/setup.ts b/scripts/setup.tsindex e3c50d17..0148bccf 100644--- a/scripts/setup.ts+++ b/scripts/setup.ts@@ -105,7 +105,7 @@ const requireDocker = async () => {const openBrowser = (url: string) => exec(process.platform === 'darwin' ? 'open' : 'xdg-open', url);-const secrets = ['BETTER_AUTH_SECRET', 'APP_ENCRYPTION_KEY', 'WORKER_INTERNAL_TOKEN'];+const secrets = ['BETTER_AUTH_SECRET', 'APP_ENCRYPTION_KEY'];/** Every value the setup chooses, with the default to prefill when the file carries none. */const generated: Record<string, string> = {