notifkit 0.1.10 → 0.1.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (81) hide show
  1. package/README.md +37 -1
  2. package/dashboard/dist/assets/{index-7mvXb4bS.js → index-Bsrmo9LE.js} +1 -1
  3. package/dashboard/dist/assets/index-s-SXGIwv.css +1 -0
  4. package/dashboard/dist/index.html +2 -2
  5. package/dist/bootstrap-omGPIBny.mjs +89 -0
  6. package/dist/bulk-C4hTcnLu.mjs +77 -0
  7. package/dist/index.d.mts +1160 -8886
  8. package/dist/index.mjs +2 -38
  9. package/dist/{main-C9s-2ArH.mjs → main-B-AZ-xoN.mjs} +49 -75
  10. package/dist/main-B5BwjIMu.mjs +811 -0
  11. package/dist/{main-B3iQrl3t.mjs → main-BU27VjDC.mjs} +149 -134
  12. package/dist/main-Bes11Ad3.mjs +312 -0
  13. package/dist/{main-BiFiOX9D.mjs → main-BtnGmyaT.mjs} +119 -112
  14. package/dist/{main-39DtKrX4.mjs → main-BxAeC_k6.mjs} +395 -184
  15. package/dist/{main-Dz2sUh3c.mjs → main-DAXO-MiH.mjs} +57 -54
  16. package/dist/{main-BgmT1MPe.mjs → main-DYGCl7Ja.mjs} +135 -79
  17. package/dist/{src-QiTu5e_a.mjs → src-CIPTzq9i.mjs} +1351 -661
  18. package/drizzle/0006_message_logs_redundant_indexes.sql +7 -0
  19. package/drizzle/0007_workflow_status_canceled.sql +3 -0
  20. package/drizzle/meta/_journal.json +14 -0
  21. package/package.json +21 -18
  22. package/scripts/create-admin.mjs +8 -1
  23. package/scripts/migrate.mjs +43 -0
  24. package/src/client.ts +15 -5
  25. package/src/config/index.ts +37 -3
  26. package/src/contracts/events/notification-ai-pending.ts +12 -0
  27. package/src/contracts/events/notification-delivered.ts +7 -0
  28. package/src/contracts/events/notification-dispatched.ts +21 -2
  29. package/src/contracts/events/notification-enriched.ts +16 -0
  30. package/src/contracts/events/notification-failed.ts +2 -0
  31. package/src/contracts/events/notification-requested.ts +2 -0
  32. package/src/contracts/registry.ts +2 -2
  33. package/src/contracts/sdk.ts +77 -29
  34. package/src/db/bulk.ts +132 -0
  35. package/src/db/index.ts +174 -7
  36. package/src/db/schema.ts +3 -50
  37. package/src/idempotency/index.ts +60 -6
  38. package/src/logger/index.ts +0 -12
  39. package/src/queue/index.ts +255 -76
  40. package/src/rate-limiter/index.ts +217 -16
  41. package/src/redis/index.ts +265 -0
  42. package/src/repositories/index.ts +171 -82
  43. package/src/server.ts +64 -41
  44. package/src/services/ai/main.ts +83 -70
  45. package/src/services/api/admin-static.ts +41 -12
  46. package/src/services/api/handlers.ts +357 -159
  47. package/src/services/api/http.ts +34 -7
  48. package/src/services/api/main.ts +104 -67
  49. package/src/services/api/router.ts +18 -5
  50. package/src/services/delivery/main.ts +495 -230
  51. package/src/services/delivery/throttle.ts +42 -59
  52. package/src/services/engine/main.ts +206 -152
  53. package/src/services/enricher/main.ts +155 -107
  54. package/src/services/events/main.ts +100 -55
  55. package/src/services/scheduler/main.ts +151 -79
  56. package/src/services/scheduler/send-gate.ts +117 -0
  57. package/src/services/workflow/main.ts +220 -100
  58. package/src/shared/batch-processor.ts +65 -34
  59. package/src/shared/dataloader.ts +58 -22
  60. package/src/shared/events.ts +3 -0
  61. package/src/shared/index.ts +0 -1
  62. package/src/shared/utils.ts +2 -47
  63. package/src/templates/index.ts +0 -67
  64. package/src/transport/index.ts +6 -0
  65. package/src/workers/bootstrap.ts +124 -0
  66. package/src/workers/index.ts +99 -24
  67. package/src/workflows/sdk.ts +2 -0
  68. package/dashboard/dist/assets/index-o7L-f3-c.css +0 -1
  69. package/dist/index.d.mts.map +0 -1
  70. package/dist/index.mjs.map +0 -1
  71. package/dist/main-39DtKrX4.mjs.map +0 -1
  72. package/dist/main-B3iQrl3t.mjs.map +0 -1
  73. package/dist/main-BM8DH3v3.mjs +0 -632
  74. package/dist/main-BM8DH3v3.mjs.map +0 -1
  75. package/dist/main-BgmT1MPe.mjs.map +0 -1
  76. package/dist/main-BiFiOX9D.mjs.map +0 -1
  77. package/dist/main-C9s-2ArH.mjs.map +0 -1
  78. package/dist/main-Cqo3rSb5.mjs +0 -224
  79. package/dist/main-Cqo3rSb5.mjs.map +0 -1
  80. package/dist/main-Dz2sUh3c.mjs.map +0 -1
  81. package/dist/src-QiTu5e_a.mjs.map +0 -1
@@ -1,34 +1,315 @@
1
1
  import { t as __exportAll } from "./rolldown-runtime-D7D4PA-g.mjs";
2
- import { registerTransport } from "./index.mjs";
3
2
  import { config } from "dotenv";
4
3
  import { resolve } from "node:path";
5
4
  import { z } from "zod";
6
5
  import { createHmac, randomUUID, timingSafeEqual } from "node:crypto";
6
+ import { EventEmitter } from "node:events";
7
7
  import postgres from "postgres";
8
8
  import { drizzle } from "drizzle-orm/postgres-js";
9
- import { boolean, check, index, integer, jsonb, pgEnum, pgTable, primaryKey, text, time, timestamp, unique, uuid, varchar } from "drizzle-orm/pg-core";
10
- import { and, desc, eq, inArray, or, sql } from "drizzle-orm";
11
- import { createInsertSchema, createSelectSchema } from "drizzle-zod";
9
+ import { PgTable, boolean, check, index, integer, jsonb, pgEnum, pgTable, primaryKey, text, time, timestamp, unique, uuid, varchar } from "drizzle-orm/pg-core";
10
+ import { and, desc, eq, getTableName, inArray, is, or, sql } from "drizzle-orm";
12
11
  import { migrate } from "drizzle-orm/postgres-js/migrator";
13
12
  import { fileURLToPath } from "url";
13
+ import os from "os";
14
14
  import path from "path";
15
15
  import fs from "fs";
16
16
  import pino from "pino";
17
17
  import promClient from "prom-client";
18
- import { EventEmitter } from "node:events";
19
18
  import { randomUUID as randomUUID$1 } from "crypto";
20
19
  import { Redis } from "ioredis";
20
+ //#region src/shared/events.ts
21
+ const globalEmitter = new EventEmitter();
22
+ globalEmitter.setMaxListeners(0);
23
+ //#endregion
24
+ //#region src/shared/cache.ts
25
+ var LRUCache = class {
26
+ cache = /* @__PURE__ */ new Map();
27
+ maxSize;
28
+ defaultTtlMs;
29
+ constructor(maxSize = 1e3, defaultTtlMs = 3e5) {
30
+ this.maxSize = maxSize;
31
+ this.defaultTtlMs = defaultTtlMs;
32
+ }
33
+ get(key) {
34
+ const item = this.cache.get(key);
35
+ if (!item) return void 0;
36
+ if (Date.now() > item.expiresAt) {
37
+ this.cache.delete(key);
38
+ return;
39
+ }
40
+ this.cache.delete(key);
41
+ this.cache.set(key, item);
42
+ return item.value;
43
+ }
44
+ set(key, value, ttlMs = this.defaultTtlMs) {
45
+ if (this.cache.has(key)) this.cache.delete(key);
46
+ else if (this.cache.size >= this.maxSize) {
47
+ const oldestKey = this.cache.keys().next().value;
48
+ if (oldestKey !== void 0) this.cache.delete(oldestKey);
49
+ }
50
+ this.cache.set(key, {
51
+ value,
52
+ expiresAt: Date.now() + ttlMs
53
+ });
54
+ }
55
+ delete(key) {
56
+ this.cache.delete(key);
57
+ }
58
+ clear() {
59
+ this.cache.clear();
60
+ }
61
+ };
62
+ //#endregion
63
+ //#region src/shared/utils.ts
64
+ function getPriorityBucket(priority) {
65
+ const p = priority || "normal";
66
+ return p === "critical" ? "critical" : p === "low" ? "low" : "normal";
67
+ }
68
+ /**
69
+ * Canonical form of a destination, for suppression lookups.
70
+ *
71
+ * Both the writer (the provider webhook) and the reader (the engine's
72
+ * pre-dispatch gate) must agree on this, or an unsubscribe recorded as
73
+ * `Bob@Example.com` will not match a send addressed to `bob@example.com` and
74
+ * the person keeps receiving mail. Case folding is safe for email domains and
75
+ * for the local part in every mailbox provider in practice; phone numbers and
76
+ * push tokens are case-sensitive and are only trimmed.
77
+ */
78
+ function normaliseTarget(target) {
79
+ const trimmed = target.trim();
80
+ return trimmed.includes("@") ? trimmed.toLowerCase() : trimmed;
81
+ }
82
+ //#endregion
83
+ //#region src/shared/semaphore.ts
84
+ var AsyncSemaphore = class {
85
+ max;
86
+ count = 0;
87
+ queue = [];
88
+ constructor(max) {
89
+ this.max = max;
90
+ }
91
+ async acquire() {
92
+ if (this.count < this.max) {
93
+ this.count++;
94
+ return Promise.resolve();
95
+ }
96
+ return new Promise((resolve) => {
97
+ this.queue.push(resolve);
98
+ });
99
+ }
100
+ release() {
101
+ if (this.queue.length > 0) this.queue.shift()();
102
+ else if (this.count > 0) this.count--;
103
+ }
104
+ get activeCount() {
105
+ return this.count;
106
+ }
107
+ };
108
+ //#endregion
109
+ //#region src/shared/batch-processor.ts
110
+ /**
111
+ * Coalesces individual `add()` calls into batched `flushFn` calls.
112
+ *
113
+ * A batch goes out when `maxSize` items are waiting or `maxWaitMs` has passed
114
+ * since the first one arrived. Up to `maxConcurrent` flushes may be in flight
115
+ * at once: with a single flight, every caller queued behind a slow flush holds
116
+ * its worker slot for the whole round trip, which caps a worker's throughput at
117
+ * `maxSize / flushLatency` no matter how much concurrency it was given.
118
+ */
119
+ var BatchProcessor = class {
120
+ maxSize;
121
+ maxWaitMs;
122
+ flushFn;
123
+ maxConcurrent;
124
+ buffer = [];
125
+ timer = null;
126
+ inFlight = /* @__PURE__ */ new Set();
127
+ constructor(maxSize, maxWaitMs, flushFn, maxConcurrent = 1) {
128
+ this.maxSize = maxSize;
129
+ this.maxWaitMs = maxWaitMs;
130
+ this.flushFn = flushFn;
131
+ this.maxConcurrent = maxConcurrent;
132
+ }
133
+ add(item) {
134
+ return new Promise((resolve, reject) => {
135
+ this.buffer.push({
136
+ item,
137
+ resolve,
138
+ reject
139
+ });
140
+ if (this.buffer.length >= this.maxSize && this.inFlight.size < this.maxConcurrent) {
141
+ this.clearTimer();
142
+ this.flushOnce();
143
+ } else if (!this.timer && this.inFlight.size < this.maxConcurrent) this.armTimer();
144
+ });
145
+ }
146
+ /** Flushes everything buffered, waiting for a free flush slot if needed. */
147
+ async flush() {
148
+ while (this.buffer.length > 0) {
149
+ while (this.inFlight.size >= this.maxConcurrent) await Promise.race(this.inFlight);
150
+ this.clearTimer();
151
+ await this.flushOnce();
152
+ }
153
+ }
154
+ flushOnce() {
155
+ if (this.buffer.length === 0 || this.inFlight.size >= this.maxConcurrent) return Promise.resolve();
156
+ const batch = this.buffer;
157
+ this.buffer = [];
158
+ const run = (async () => {
159
+ try {
160
+ const results = await this.flushFn(batch.map((b) => b.item));
161
+ for (let i = 0; i < batch.length; i++) batch[i].resolve(results[i]);
162
+ } catch (err) {
163
+ for (const b of batch) b.reject(err);
164
+ }
165
+ })();
166
+ this.inFlight.add(run);
167
+ return run.finally(() => {
168
+ this.inFlight.delete(run);
169
+ if (this.buffer.length === 0 || this.timer) return;
170
+ if (this.buffer.length >= this.maxSize) this.flushOnce();
171
+ else this.armTimer();
172
+ });
173
+ }
174
+ armTimer() {
175
+ this.timer = setTimeout(() => {
176
+ this.timer = null;
177
+ this.flushOnce();
178
+ }, this.maxWaitMs);
179
+ }
180
+ clearTimer() {
181
+ if (this.timer) {
182
+ clearTimeout(this.timer);
183
+ this.timer = null;
184
+ }
185
+ }
186
+ };
187
+ //#endregion
188
+ //#region src/shared/circuit-breaker.ts
189
+ var CircuitBreaker = class {
190
+ state = "CLOSED";
191
+ failures = 0;
192
+ nextAttemptAt = 0;
193
+ /** True while one caller is testing whether the dependency has recovered. */
194
+ probeInFlight = false;
195
+ threshold;
196
+ timeout;
197
+ constructor(options) {
198
+ this.threshold = options.failureThreshold;
199
+ this.timeout = options.resetTimeoutMs;
200
+ }
201
+ async execute(action) {
202
+ let isProbe = false;
203
+ if (this.state === "OPEN") if (Date.now() > this.nextAttemptAt && !this.probeInFlight) {
204
+ this.state = "HALF_OPEN";
205
+ this.probeInFlight = true;
206
+ isProbe = true;
207
+ } else throw new Error("Circuit breaker is OPEN");
208
+ else if (this.state === "HALF_OPEN") {
209
+ if (this.probeInFlight) throw new Error("Circuit breaker is OPEN");
210
+ this.probeInFlight = true;
211
+ isProbe = true;
212
+ }
213
+ try {
214
+ const result = await action();
215
+ this.onSuccess();
216
+ return result;
217
+ } catch (err) {
218
+ this.onFailure();
219
+ throw err;
220
+ } finally {
221
+ if (isProbe) this.probeInFlight = false;
222
+ }
223
+ }
224
+ onSuccess() {
225
+ this.failures = 0;
226
+ this.state = "CLOSED";
227
+ }
228
+ onFailure() {
229
+ this.failures++;
230
+ if (this.failures >= this.threshold) {
231
+ this.state = "OPEN";
232
+ this.nextAttemptAt = Date.now() + this.timeout;
233
+ }
234
+ }
235
+ getState() {
236
+ return this.state;
237
+ }
238
+ };
239
+ //#endregion
240
+ //#region src/shared/dataloader.ts
241
+ var DataLoader = class {
242
+ batchLoadFn;
243
+ keys = [];
244
+ promises = [];
245
+ scheduled = false;
246
+ timer = null;
247
+ batchDelayMs;
248
+ maxBatchSize;
249
+ constructor(batchLoadFn, { batchDelayMs = 0, maxBatchSize = Infinity } = {}) {
250
+ this.batchLoadFn = batchLoadFn;
251
+ this.batchDelayMs = batchDelayMs;
252
+ this.maxBatchSize = maxBatchSize;
253
+ }
254
+ load(key) {
255
+ return new Promise((resolve, reject) => {
256
+ this.keys.push(key);
257
+ this.promises.push({ resolve: (value) => {
258
+ if (value instanceof Error) reject(value);
259
+ else resolve(value);
260
+ } });
261
+ if (this.keys.length >= this.maxBatchSize) this.dispatch();
262
+ else if (!this.scheduled) {
263
+ this.scheduled = true;
264
+ if (this.batchDelayMs > 0) this.timer = setTimeout(() => this.dispatch(), this.batchDelayMs);
265
+ else Promise.resolve().then(() => this.dispatch());
266
+ }
267
+ });
268
+ }
269
+ dispatch() {
270
+ if (this.timer) {
271
+ clearTimeout(this.timer);
272
+ this.timer = null;
273
+ }
274
+ this.scheduled = false;
275
+ if (this.keys.length === 0) return;
276
+ const keysToLoad = this.keys;
277
+ const currentPromises = this.promises;
278
+ this.keys = [];
279
+ this.promises = [];
280
+ this.batchLoadFn(keysToLoad).then((results) => {
281
+ for (let i = 0; i < currentPromises.length; i++) currentPromises[i].resolve(results[i]);
282
+ }).catch((err) => {
283
+ for (const p of currentPromises) p.resolve(err);
284
+ });
285
+ }
286
+ };
287
+ //#endregion
288
+ //#region src/shared/index.ts
289
+ function generateId() {
290
+ return randomUUID();
291
+ }
292
+ function sleep(ms) {
293
+ return new Promise((resolve) => setTimeout(resolve, ms));
294
+ }
295
+ var AppError = class extends Error {
296
+ code;
297
+ constructor(message, code, options) {
298
+ super(message, options);
299
+ this.name = "AppError";
300
+ this.code = code;
301
+ }
302
+ };
303
+ var ValidationError = class extends AppError {
304
+ fields;
305
+ constructor(message, fields, options) {
306
+ super(message, "VALIDATION_ERROR", options);
307
+ this.name = "ValidationError";
308
+ this.fields = fields;
309
+ }
310
+ };
311
+ //#endregion
21
312
  //#region src/config/index.ts
22
- var config_exports = /* @__PURE__ */ __exportAll({
23
- AI_DEFAULTS: () => AI_DEFAULTS,
24
- baseConfigSchema: () => baseConfigSchema,
25
- getAiConfig: () => getAiConfig,
26
- loadEnv: () => loadEnv,
27
- parseConfig: () => parseConfig,
28
- readBaseConfig: () => readBaseConfig,
29
- setAiConfig: () => setAiConfig,
30
- setGlobalConfig: () => setGlobalConfig
31
- });
32
313
  function loadEnv(path) {
33
314
  const envPath = path ?? resolve(process.cwd(), ".env");
34
315
  config({
@@ -73,8 +354,32 @@ const baseConfigSchema = z.object({
73
354
  ADMIN_PASSWORD: z.string().trim().min(6).optional(),
74
355
  ADMIN_USERNAME: z.string().trim().optional(),
75
356
  WORKER_CONCURRENCY: z.coerce.number().int().min(1).default(10),
76
- QUEUE_MAX_LEN: z.coerce.number().int().min(1).default(1e7),
357
+ /** In-flight sends per delivery worker. Defaults to WORKER_CONCURRENCY. */
358
+ DELIVERY_CONCURRENCY: z.coerce.number().int().min(1).optional(),
359
+ /**
360
+ * When enricher, engine and delivery run in the same process, hand
361
+ * single-recipient notifications from stage to stage in memory instead of
362
+ * through the enriched/outbound streams. See `NotifkitOptions.fusedPipeline`.
363
+ */
364
+ PIPELINE_FUSED: z.string().optional().transform((v) => v === "true" || v === "1"),
365
+ QUEUE_MAX_LEN: z.coerce.number().int().min(1).default(1e5),
366
+ /**
367
+ * How long the pipeline remembers it handled a message, so a redelivery
368
+ * after a crash is not sent twice. It only has to outlast redelivery (about
369
+ * ten minutes) plus however far behind the streams run; a caller's own
370
+ * X-Idempotency-Key is always kept for 24 hours regardless. These markers
371
+ * are most of Redis memory under load, so the cost of a longer window is
372
+ * real: a few hundred bytes per message for as long as it lasts.
373
+ */
374
+ IDEMPOTENCY_TTL_SECONDS: z.coerce.number().int().min(60).default(3600),
77
375
  DB_MAX_CONNECTIONS: z.coerce.number().int().min(1).default(2),
376
+ /**
377
+ * PostgreSQL schema that holds notifkit's tables and enum types. Defaults to
378
+ * `public`. Any other value makes migrations create every object in that
379
+ * schema and sets `search_path` on every connection, so notifkit can share a
380
+ * database with an application that owns `public`.
381
+ */
382
+ DB_SCHEMA: z.string().trim().regex(/^[a-z_][a-z0-9_]{0,62}$/, "must be a lowercase PostgreSQL identifier").default("public"),
78
383
  LOG_FLUSH_INTERVAL_MS: z.coerce.number().int().min(50).default(500),
79
384
  LOG_BUFFER_MAX_SIZE: z.coerce.number().int().min(100).default(5e3),
80
385
  SEGMENT_MAX_USERS: z.coerce.number().int().min(1).default(1e4),
@@ -95,7 +400,7 @@ const baseConfigSchema = z.object({
95
400
  * When true, trusts X-Forwarded-For headers from reverse proxies for client IP resolution.
96
401
  * Defaults to false to prevent client-spoofed headers from bypassing rate limits.
97
402
  */
98
- TRUST_PROXY: z.coerce.boolean().default(false),
403
+ TRUST_PROXY: z.string().optional().transform((v) => v === "true" || v === "1"),
99
404
  /**
100
405
  * Comma-separated list of allowed origins for CORS on session-authenticated admin routes.
101
406
  */
@@ -464,7 +769,15 @@ const WorkflowStepSchema = z.discriminatedUnion("action", [
464
769
  z.object({
465
770
  action: z.literal("waitForEvent"),
466
771
  event: z.string(),
467
- options: z.object({ timeout: z.string().optional() }).optional()
772
+ options: z.object({
773
+ timeout: z.string().optional(),
774
+ /**
775
+ * Fields the event must carry to wake this instance, e.g.
776
+ * `{ userId: "u1" }`. Without it any event of that name in the project
777
+ * wakes every instance waiting on it.
778
+ */
779
+ match: z.record(z.string(), z.unknown()).optional()
780
+ }).optional()
468
781
  })
469
782
  ]);
470
783
  const CreateWorkflowSchema = z.object({
@@ -515,7 +828,9 @@ const NotificationRequestedPayloadSchema = z.object({
515
828
  * Groups every message this request fans out into, so the send can be
516
829
  * reported on afterwards. Carried unchanged to the delivery log.
517
830
  */
518
- campaignId: z.string().min(1).max(128).optional()
831
+ campaignId: z.string().min(1).max(128).optional(),
832
+ /** Set by the workflow worker on a step's notify, carried to the delivery log. */
833
+ workflowInstanceId: z.string().uuid().optional()
519
834
  });
520
835
  //#endregion
521
836
  //#region src/contracts/events/notification-created.ts
@@ -564,8 +879,24 @@ const NotificationEnrichedPayloadSchema = z.object({
564
879
  aiPrompts: z.record(z.string(), z.string()).optional(),
565
880
  scheduledAt: z.string().datetime().optional(),
566
881
  fallbackChain: z.array(NotificationChannelSchema).optional(),
882
+ /**
883
+ * The contact the enricher resolved this message to, and its address. When
884
+ * present the engine sends to exactly this contact instead of re-reading the
885
+ * user's contacts. Absent on events from delivery's channel fallback and the
886
+ * AI worker, which carry a recipient but no resolved contact.
887
+ */
888
+ contactId: z.string().min(1).optional(),
889
+ destination: z.string().min(1).optional(),
567
890
  /** Campaign this message belongs to, carried from the originating request. */
568
- campaignId: z.string().min(1).max(128).optional()
891
+ campaignId: z.string().min(1).max(128).optional(),
892
+ /** Workflow instance whose step sent this, carried to the delivery log. */
893
+ workflowInstanceId: z.string().uuid().optional(),
894
+ /**
895
+ * Set on delivery's channel fallback. The notification was counted against
896
+ * the user's throttle when it was first sent; counting it again per channel
897
+ * it falls back through would drop the very send meant to reach them.
898
+ */
899
+ throttleCounted: z.boolean().optional()
569
900
  });
570
901
  //#endregion
571
902
  //#region src/contracts/events/notification-scheduled.ts
@@ -601,13 +932,26 @@ const NotificationDispatchedPayloadSchema = z.object({
601
932
  templateVariables: z.record(z.string(), z.unknown()).default({}),
602
933
  aiPrompts: z.record(z.string(), z.string()).optional(),
603
934
  recipient: RecipientProfileSchema.optional(),
604
- renderedContent: RenderedContentSchema,
935
+ /**
936
+ * Absent on tasks the engine dispatches: delivery renders `templateId` with
937
+ * `templateVariables` just before sending, so the rendered body never rides
938
+ * through the outbound stream or sits in a scheduled payload. The AI worker
939
+ * still sends it pre-rendered, since its variables are generated.
940
+ */
941
+ renderedContent: RenderedContentSchema.optional(),
605
942
  destination: z.string().min(1).optional(),
606
943
  deliveryOptions: DeliveryOptionsSchema,
607
944
  fallbackChain: z.array(NotificationChannelSchema).optional(),
608
945
  throttleAttemptCount: z.number().int().nonnegative().optional(),
946
+ /**
947
+ * Provider attempts that already failed. Delivery re-queues a failed send
948
+ * through the scheduler until this reaches `deliveryOptions.maxAttempts`.
949
+ */
950
+ deliveryAttemptCount: z.number().int().nonnegative().optional(),
609
951
  /** Campaign this message belongs to, carried from the originating request. */
610
- campaignId: z.string().min(1).max(128).optional()
952
+ campaignId: z.string().min(1).max(128).optional(),
953
+ /** Workflow instance whose step sent this, recorded on the delivery log. */
954
+ workflowInstanceId: z.string().uuid().optional()
611
955
  });
612
956
  //#endregion
613
957
  //#region src/contracts/events/notification-delivered.ts
@@ -622,7 +966,14 @@ const NotificationDeliveredPayloadSchema = z.object({
622
966
  templateId: z.string().uuid().optional(),
623
967
  workflowInstanceId: z.string().uuid().optional(),
624
968
  /** Campaign this message belongs to, carried from the originating request. */
625
- campaignId: z.string().min(1).max(128).optional()
969
+ campaignId: z.string().min(1).max(128).optional(),
970
+ attempt: z.number().int().positive().optional(),
971
+ /**
972
+ * Set when this event also stands in for the attempt's
973
+ * `notification.dispatched` log entry (see DeliveryWorker): the send finished
974
+ * quickly enough that both were published as one stream entry.
975
+ */
976
+ dispatchedAt: z.string().datetime().optional()
626
977
  });
627
978
  //#endregion
628
979
  //#region src/contracts/events/notification-failed.ts
@@ -639,7 +990,9 @@ const NotificationFailedPayloadSchema = z.object({
639
990
  templateId: z.string().uuid().optional(),
640
991
  workflowInstanceId: z.string().uuid().optional(),
641
992
  /** Campaign this message belongs to, carried from the originating request. */
642
- campaignId: z.string().min(1).max(128).optional()
993
+ campaignId: z.string().min(1).max(128).optional(),
994
+ /** See NotificationDeliveredPayload.dispatchedAt. */
995
+ dispatchedAt: z.string().datetime().optional()
643
996
  });
644
997
  //#endregion
645
998
  //#region src/contracts/events/notification-skipped.ts
@@ -668,7 +1021,19 @@ const NotificationAiPendingPayloadSchema = z.object({
668
1021
  recipient: RecipientProfileSchema,
669
1022
  aiPrompts: z.record(z.string(), z.string()),
670
1023
  scheduledAt: z.string().datetime().optional(),
671
- fallbackChain: z.array(NotificationChannelSchema).optional()
1024
+ fallbackChain: z.array(NotificationChannelSchema).optional(),
1025
+ /**
1026
+ * The task id the engine assigned. Stable across replays, so a re-run of
1027
+ * this event dispatches the same task — which delivery's lease then sends
1028
+ * once — rather than a fresh one.
1029
+ */
1030
+ taskId: z.string().min(1).optional(),
1031
+ /** The contact address the engine resolved and cleared against suppressions. */
1032
+ destination: z.string().min(1).optional(),
1033
+ /** Headers for the provider, e.g. one-click unsubscribe. */
1034
+ deliveryHeaders: z.record(z.string(), z.string()).optional(),
1035
+ campaignId: z.string().min(1).max(128).optional(),
1036
+ workflowInstanceId: z.string().uuid().optional()
672
1037
  });
673
1038
  //#endregion
674
1039
  //#region src/contracts/index.ts
@@ -690,41 +1055,11 @@ var schema_exports = /* @__PURE__ */ __exportAll({
690
1055
  channelEnum: () => channelEnum,
691
1056
  contactTopicPreferences: () => contactTopicPreferences,
692
1057
  deliveryOutbox: () => deliveryOutbox,
693
- insertAdminUserSchema: () => insertAdminUserSchema,
694
- insertDeliveryOutboxSchema: () => insertDeliveryOutboxSchema,
695
- insertMessageLogSchema: () => insertMessageLogSchema,
696
- insertProjectApiKeySchema: () => insertProjectApiKeySchema,
697
- insertProjectSchema: () => insertProjectSchema,
698
- insertScheduledPayloadSchema: () => insertScheduledPayloadSchema,
699
- insertSuppressionSchema: () => insertSuppressionSchema,
700
- insertUserChannelPreferenceSchema: () => insertUserChannelPreferenceSchema,
701
- insertUserContactSchema: () => insertUserContactSchema,
702
- insertUserSchema: () => insertUserSchema,
703
- insertUserSegmentSchema: () => insertUserSegmentSchema,
704
- insertUserTopicPreferenceSchema: () => insertUserTopicPreferenceSchema,
705
- insertWorkflowInstanceSchema: () => insertWorkflowInstanceSchema,
706
- insertWorkflowStepSchema: () => insertWorkflowStepSchema,
707
- insertWorkflowWaiterSchema: () => insertWorkflowWaiterSchema,
708
1058
  messageLogs: () => messageLogs,
709
1059
  projectApiKeys: () => projectApiKeys,
710
1060
  projects: () => projects,
711
1061
  quietHours: () => quietHours,
712
1062
  scheduledPayloads: () => scheduledPayloads,
713
- selectAdminUserSchema: () => selectAdminUserSchema,
714
- selectDeliveryOutboxSchema: () => selectDeliveryOutboxSchema,
715
- selectMessageLogSchema: () => selectMessageLogSchema,
716
- selectProjectApiKeySchema: () => selectProjectApiKeySchema,
717
- selectProjectSchema: () => selectProjectSchema,
718
- selectScheduledPayloadSchema: () => selectScheduledPayloadSchema,
719
- selectSuppressionSchema: () => selectSuppressionSchema,
720
- selectUserChannelPreferenceSchema: () => selectUserChannelPreferenceSchema,
721
- selectUserContactSchema: () => selectUserContactSchema,
722
- selectUserSchema: () => selectUserSchema,
723
- selectUserSegmentSchema: () => selectUserSegmentSchema,
724
- selectUserTopicPreferenceSchema: () => selectUserTopicPreferenceSchema,
725
- selectWorkflowInstanceSchema: () => selectWorkflowInstanceSchema,
726
- selectWorkflowStepSchema: () => selectWorkflowStepSchema,
727
- selectWorkflowWaiterSchema: () => selectWorkflowWaiterSchema,
728
1063
  suppressions: () => suppressions,
729
1064
  templates: () => templates,
730
1065
  userChannelPreferences: () => userChannelPreferences,
@@ -857,8 +1192,6 @@ const messageLogs = pgTable("message_logs", {
857
1192
  metadata: jsonb("metadata"),
858
1193
  timestamp: timestamp("timestamp", { withTimezone: true }).defaultNow().notNull()
859
1194
  }, (table) => ({
860
- projectIdx: index("message_logs_project_idx").on(table.projectId),
861
- taskIdx: index("task_idx").on(table.taskId),
862
1195
  projectIdTaskIdIdx: index("message_logs_project_task_idx").on(table.projectId, table.taskId),
863
1196
  providerMsgIdx: index("provider_msg_idx").on(table.providerMessageId),
864
1197
  projectTimeIdx: index("msg_log_proj_time_idx").on(table.projectId, table.timestamp),
@@ -898,7 +1231,8 @@ const workflowStatusEnum = pgEnum("workflow_status", [
898
1231
  "pending",
899
1232
  "running",
900
1233
  "completed",
901
- "failed"
1234
+ "failed",
1235
+ "canceled"
902
1236
  ]);
903
1237
  const workflowDefinitions = pgTable("workflow_definitions", {
904
1238
  id: uuid("id").primaryKey().defaultRandom(),
@@ -965,55 +1299,23 @@ const adminUsers = pgTable("admin_users", {
965
1299
  createdAt: timestamp("created_at", { withTimezone: true }).defaultNow().notNull(),
966
1300
  updatedAt: timestamp("updated_at", { withTimezone: true }).defaultNow().notNull()
967
1301
  });
968
- const insertAdminUserSchema = createInsertSchema(adminUsers);
969
- const selectAdminUserSchema = createSelectSchema(adminUsers);
970
- const insertProjectSchema = createInsertSchema(projects);
971
- const selectProjectSchema = createSelectSchema(projects);
972
- const insertProjectApiKeySchema = createInsertSchema(projectApiKeys);
973
- const selectProjectApiKeySchema = createSelectSchema(projectApiKeys);
974
- const insertUserSchema = createInsertSchema(users);
975
- const selectUserSchema = createSelectSchema(users);
976
- const insertUserSegmentSchema = createInsertSchema(userSegments);
977
- const selectUserSegmentSchema = createSelectSchema(userSegments);
978
- const insertUserContactSchema = createInsertSchema(userContacts);
979
- const selectUserContactSchema = createSelectSchema(userContacts);
980
- const insertUserChannelPreferenceSchema = createInsertSchema(userChannelPreferences);
981
- const selectUserChannelPreferenceSchema = createSelectSchema(userChannelPreferences);
982
- const insertUserTopicPreferenceSchema = createInsertSchema(userTopicPreferences);
983
- const selectUserTopicPreferenceSchema = createSelectSchema(userTopicPreferences);
984
- const insertMessageLogSchema = createInsertSchema(messageLogs);
985
- const selectMessageLogSchema = createSelectSchema(messageLogs);
986
- const insertSuppressionSchema = createInsertSchema(suppressions);
987
- const selectSuppressionSchema = createSelectSchema(suppressions);
988
- const insertWorkflowInstanceSchema = createInsertSchema(workflowInstances);
989
- const selectWorkflowInstanceSchema = createSelectSchema(workflowInstances);
990
- const insertWorkflowStepSchema = createInsertSchema(workflowSteps);
991
- const selectWorkflowStepSchema = createSelectSchema(workflowSteps);
992
- const insertWorkflowWaiterSchema = createInsertSchema(workflowWaiters);
993
- const selectWorkflowWaiterSchema = createSelectSchema(workflowWaiters);
994
- const insertDeliveryOutboxSchema = createInsertSchema(deliveryOutbox);
995
- const selectDeliveryOutboxSchema = createSelectSchema(deliveryOutbox);
996
- const insertScheduledPayloadSchema = createInsertSchema(scheduledPayloads);
997
- const selectScheduledPayloadSchema = createSelectSchema(scheduledPayloads);
998
1302
  //#endregion
999
1303
  //#region src/db/index.ts
1000
- var db_exports = /* @__PURE__ */ __exportAll({
1001
- createDatabase: () => createDatabase,
1002
- runMigrations: () => runMigrations
1003
- });
1004
1304
  /**
1005
1305
  * Create a postgres.js connection pool and initialize Drizzle ORM.
1006
1306
  * Call once at application startup; pass db into repositories.
1007
1307
  * Call sql.end() during graceful shutdown.
1008
1308
  */
1009
- function createDatabase({ url, applicationName = "notifkit", maxConnections, idleTimeoutSeconds = 30, logger }) {
1309
+ function createDatabase({ url, schema: dbSchema, applicationName = "notifkit", maxConnections, idleTimeoutSeconds = 30, logger }) {
1010
1310
  const finalMaxConnections = maxConnections ?? readBaseConfig().DB_MAX_CONNECTIONS;
1311
+ const finalSchema = resolveSchema(dbSchema);
1011
1312
  const sql = postgres(url, {
1012
1313
  max: finalMaxConnections,
1013
1314
  idle_timeout: idleTimeoutSeconds,
1014
1315
  connection: {
1015
1316
  application_name: applicationName,
1016
- statement_timeout: 1e4
1317
+ statement_timeout: 1e4,
1318
+ ...finalSchema === DEFAULT_SCHEMA ? {} : { search_path: quoteIdentifier(finalSchema) }
1017
1319
  },
1018
1320
  onnotice: (notice) => {
1019
1321
  logger?.debug({ notice }, "postgres notice");
@@ -1024,19 +1326,130 @@ function createDatabase({ url, applicationName = "notifkit", maxConnections, idl
1024
1326
  db: drizzle(sql, { schema: schema_exports })
1025
1327
  };
1026
1328
  }
1027
- async function runMigrations(db) {
1329
+ const DEFAULT_SCHEMA = "public";
1330
+ const SCHEMA_NAME = /^[a-z_][a-z0-9_]{0,62}$/;
1331
+ const STATEMENT_BREAKPOINT = "--> statement-breakpoint";
1332
+ function resolveSchema(schema) {
1333
+ const name = schema ?? readBaseConfig().DB_SCHEMA;
1334
+ if (!SCHEMA_NAME.test(name)) throw new Error(`Invalid database schema "${name}": must be a lowercase PostgreSQL identifier`);
1335
+ return name;
1336
+ }
1337
+ function quoteIdentifier(name) {
1338
+ return `"${name}"`;
1339
+ }
1340
+ function packagedMigrationsFolder() {
1028
1341
  const __filename = fileURLToPath(import.meta.url);
1029
1342
  const __dirname = path.dirname(__filename);
1030
- let migrationsFolder = path.resolve(__dirname, "../../drizzle");
1031
- if (!fs.existsSync(migrationsFolder)) migrationsFolder = path.resolve(__dirname, "../drizzle");
1032
- await migrate(db, { migrationsFolder });
1343
+ const folder = path.resolve(__dirname, "../../drizzle");
1344
+ return fs.existsSync(folder) ? folder : path.resolve(__dirname, "../drizzle");
1033
1345
  }
1034
- //#endregion
1035
- //#region src/idempotency/index.ts
1036
1346
  /**
1037
- * SETNX-based idempotency guard.
1038
- * Returns true from checkAndMark() only the first time a given ID is seen
1039
- * within the TTL window; subsequent calls return false (duplicate / retry).
1347
+ * Retarget one generated migration at `schema`. drizzle-kit qualifies enum
1348
+ * types and foreign-key targets with `"public".` but leaves tables and indexes
1349
+ * unqualified, so both halves have to move: the qualifier is rewritten, and
1350
+ * `SET LOCAL search_path` places the unqualified objects. `SET LOCAL` ends with
1351
+ * the migration transaction and never leaks into a pooled connection.
1352
+ *
1353
+ * Throws if `public` is still mentioned after the rewrite: a migration that
1354
+ * spells the schema some other way would otherwise put objects into `public`
1355
+ * without a word.
1356
+ */
1357
+ function qualifyMigrationSql(source, schema) {
1358
+ const rewritten = source.replaceAll(`"${DEFAULT_SCHEMA}".`, `${quoteIdentifier(schema)}.`);
1359
+ if (/\bpublic\b/i.test(rewritten)) throw new Error(`Migration still references the public schema after retargeting it to "${schema}"`);
1360
+ return `SET LOCAL search_path TO ${quoteIdentifier(schema)};\n${STATEMENT_BREAKPOINT}\n${rewritten}`;
1361
+ }
1362
+ /** Copy the journal and every migration it lists into `target`, retargeted at `schema`. */
1363
+ function writeSchemaMigrations(source, target, schema) {
1364
+ const journalPath = path.join(source, "meta", "_journal.json");
1365
+ const journal = JSON.parse(fs.readFileSync(journalPath, "utf8"));
1366
+ fs.mkdirSync(path.join(target, "meta"));
1367
+ fs.copyFileSync(journalPath, path.join(target, "meta", "_journal.json"));
1368
+ for (const { tag } of journal.entries) {
1369
+ const file = `${tag}.sql`;
1370
+ const migration = fs.readFileSync(path.join(source, file), "utf8");
1371
+ fs.writeFileSync(path.join(target, file), qualifyMigrationSql(migration, schema));
1372
+ }
1373
+ }
1374
+ async function runMigrations(db, options = {}) {
1375
+ const migrationsFolder = packagedMigrationsFolder();
1376
+ const schema = resolveSchema(options.schema);
1377
+ const { migrationsTable } = options;
1378
+ if (schema === DEFAULT_SCHEMA) {
1379
+ await migrate(db, {
1380
+ migrationsFolder,
1381
+ migrationsTable,
1382
+ migrationsSchema: options.migrationsSchema
1383
+ });
1384
+ return;
1385
+ }
1386
+ await db.execute(sql`CREATE SCHEMA IF NOT EXISTS ${sql.identifier(schema)}`);
1387
+ const retargeted = fs.mkdtempSync(path.join(os.tmpdir(), "notifkit-migrations-"));
1388
+ try {
1389
+ writeSchemaMigrations(migrationsFolder, retargeted, schema);
1390
+ await migrate(db, {
1391
+ migrationsFolder: retargeted,
1392
+ migrationsTable,
1393
+ migrationsSchema: options.migrationsSchema ?? schema
1394
+ });
1395
+ } finally {
1396
+ fs.rmSync(retargeted, {
1397
+ recursive: true,
1398
+ force: true
1399
+ });
1400
+ }
1401
+ await assertTablesInSchema(db, schema);
1402
+ }
1403
+ /**
1404
+ * Names of every table notifkit's Drizzle schema defines, sorted. This is the
1405
+ * same object the ORM queries at run time, so the list cannot drift from the
1406
+ * tables notifkit actually uses.
1407
+ */
1408
+ function notifkitTableNames() {
1409
+ return Object.values(schema_exports).filter((value) => is(value, PgTable)).map((table) => getTableName(table)).sort();
1410
+ }
1411
+ /**
1412
+ * Fail if any notifkit table is missing from `targetSchema`. Run after
1413
+ * migrating into a custom schema: if the retargeting ever stops working (the
1414
+ * `search_path` is overridden, a migration is skipped because of a shared
1415
+ * journal, ...), the tables land elsewhere or nowhere, and every runtime query
1416
+ * would fail with "relation does not exist". Only the target schema is checked:
1417
+ * the application sharing the database may well own `users` or `projects` in
1418
+ * `public`, and that is not an error.
1419
+ */
1420
+ async function assertTablesInSchema(db, targetSchema) {
1421
+ const rows = await db.execute(sql`SELECT table_name FROM information_schema.tables WHERE table_schema = ${targetSchema}`);
1422
+ const present = new Set(rows.map((row) => row.table_name));
1423
+ const missing = notifkitTableNames().filter((name) => !present.has(name));
1424
+ if (missing.length > 0) throw new Error(`Migrations finished, but schema "${targetSchema}" is missing notifkit tables: ${missing.join(", ")}`);
1425
+ }
1426
+ /**
1427
+ * Apply the bundled migrations to the database at `url` and close the
1428
+ * connection, whether or not they succeed. `schema` defaults to `DB_SCHEMA`.
1429
+ * This is what `notifkit-migrate` runs.
1430
+ */
1431
+ async function migrateDatabase({ url, schema: dbSchema }) {
1432
+ const targetSchema = resolveSchema(dbSchema);
1433
+ const { db, sql } = createDatabase({
1434
+ url,
1435
+ schema: targetSchema,
1436
+ applicationName: "notifkit-migrate",
1437
+ maxConnections: 1
1438
+ });
1439
+ try {
1440
+ await runMigrations(db, { schema: targetSchema });
1441
+ } finally {
1442
+ await sql.end();
1443
+ }
1444
+ return targetSchema;
1445
+ }
1446
+ //#endregion
1447
+ //#region src/idempotency/index.ts
1448
+ /**
1449
+ * Two-phase SETNX-based idempotency guard.
1450
+ * Supports:
1451
+ * - Phase 1 (:lock): Short lease acquired at the start of message processing.
1452
+ * - Phase 2 (:sent): Long-lived completion marker recorded when processing succeeds.
1040
1453
  */
1041
1454
  var IdempotencyGuard = class {
1042
1455
  redis;
@@ -1050,22 +1463,53 @@ var IdempotencyGuard = class {
1050
1463
  key(id) {
1051
1464
  return `${this.keyPrefix}:${id}`;
1052
1465
  }
1466
+ lockKey(id) {
1467
+ return `${this.keyPrefix}:${id}:lock`;
1468
+ }
1469
+ completedKey(id) {
1470
+ return `${this.keyPrefix}:${id}:sent`;
1471
+ }
1472
+ /**
1473
+ * Two-phase lease acquisition.
1474
+ * Atomically checks existing state and acquires lock if available.
1475
+ * - Happy path (new message): single native SET NX command storing "L" (lock lease).
1476
+ * - If already present: checks if "S" (sent/completed) or "L" (locked by another worker).
1477
+ */
1478
+ async acquireLease(id, lockTtlSeconds = 30) {
1479
+ const mainKey = this.key(id);
1480
+ const lockKey = this.lockKey(id);
1481
+ if (await this.redis.set(mainKey, "L", "EX", lockTtlSeconds, "NX") === "OK") return "acquired";
1482
+ const [val, sent, lockVal] = await Promise.all([
1483
+ this.redis.get(mainKey),
1484
+ this.redis.get(this.completedKey(id)),
1485
+ this.redis.get(lockKey)
1486
+ ]);
1487
+ if (val === "S" || val === "1" || sent !== null) return "completed";
1488
+ if (val === "L" || lockVal !== null) return "locked";
1489
+ if (await this.redis.set(mainKey, "L", "EX", lockTtlSeconds, "NX") === "OK") return "acquired";
1490
+ return "locked";
1491
+ }
1053
1492
  /** Atomically mark id as processed. Returns true on first call; false if already seen. */
1054
1493
  async checkAndMark(id, customTtlSeconds) {
1055
1494
  const ttl = customTtlSeconds ?? this.ttlSeconds;
1056
1495
  return await this.redis.set(this.key(id), "1", "EX", ttl, "NX") === "OK";
1057
1496
  }
1058
- /** Unconditionally mark id as processed (e.g. to upgrade a short-lived lock). */
1497
+ /** Unconditionally mark id as processed (upgrades lock to completed "1" with full TTL). */
1059
1498
  async markProcessed(id, customTtlSeconds) {
1060
1499
  const ttl = customTtlSeconds ?? this.ttlSeconds;
1061
1500
  await this.redis.set(this.key(id), "1", "EX", ttl);
1062
1501
  }
1063
1502
  async isProcessed(id) {
1064
- return await this.redis.get(this.key(id)) !== null;
1503
+ const val = await this.redis.get(this.key(id));
1504
+ return val !== null && val !== void 0;
1065
1505
  }
1066
1506
  /** Remove the idempotency marker (useful in tests or manual rollbacks). */
1067
1507
  async unmark(id) {
1068
- await this.redis.del(this.key(id));
1508
+ await Promise.all([
1509
+ this.redis.del(this.key(id)),
1510
+ this.redis.del(this.lockKey(id)),
1511
+ this.redis.del(this.completedKey(id))
1512
+ ]);
1069
1513
  }
1070
1514
  };
1071
1515
  //#endregion
@@ -1101,15 +1545,6 @@ function createLogger({ name, level = "info", pretty, context }) {
1101
1545
  };
1102
1546
  return pino(options);
1103
1547
  }
1104
- function withRequestId(logger, requestId) {
1105
- return logger.child({ requestId });
1106
- }
1107
- function withContext(logger, context) {
1108
- return logger.child(context);
1109
- }
1110
- function childLogger(logger, bindings) {
1111
- return logger.child(bindings);
1112
- }
1113
1548
  //#endregion
1114
1549
  //#region src/metrics/index.ts
1115
1550
  const register = new promClient.Registry();
@@ -1197,6 +1632,9 @@ var StreamProducer = class {
1197
1632
  stream;
1198
1633
  logger;
1199
1634
  maxLen;
1635
+ pendingQueue = [];
1636
+ flushScheduled = false;
1637
+ lastMaxLenCheck = 0;
1200
1638
  constructor({ redis, stream, logger, maxLen }) {
1201
1639
  this.redis = redis;
1202
1640
  this.stream = stream;
@@ -1204,23 +1642,68 @@ var StreamProducer = class {
1204
1642
  this.maxLen = maxLen ?? readBaseConfig().QUEUE_MAX_LEN;
1205
1643
  }
1206
1644
  async publish(partial) {
1207
- const event = {
1645
+ return this.enqueue({
1208
1646
  ...partial,
1209
1647
  id: crypto.randomUUID(),
1210
1648
  timestamp: (/* @__PURE__ */ new Date()).toISOString()
1211
- };
1212
- const messageId = await this.redis.xadd(this.stream, "MAXLEN", "~", String(this.maxLen), "*", "data", JSON.stringify(event));
1213
- if (!messageId) throw new Error(`XADD to ${this.stream} returned null`);
1214
- this.logger?.debug({
1215
- stream: this.stream,
1216
- messageId,
1217
- eventType: event.type,
1218
- eventId: event.id
1219
- }, "event published");
1220
- return messageId;
1649
+ });
1650
+ }
1651
+ enqueue(event) {
1652
+ return new Promise((resolve, reject) => {
1653
+ this.pendingQueue.push({
1654
+ event,
1655
+ resolve,
1656
+ reject
1657
+ });
1658
+ if (this.pendingQueue.length >= 100) this.flush();
1659
+ else if (!this.flushScheduled) {
1660
+ this.flushScheduled = true;
1661
+ setImmediate(() => {
1662
+ this.flush();
1663
+ });
1664
+ }
1665
+ });
1666
+ }
1667
+ async flush() {
1668
+ this.flushScheduled = false;
1669
+ const batch = this.pendingQueue;
1670
+ this.pendingQueue = [];
1671
+ if (batch.length === 0) return;
1672
+ try {
1673
+ if (typeof this.redis.pipeline === "function") {
1674
+ const pipeline = this.redis.pipeline();
1675
+ for (let i = 0; i < batch.length; i++) {
1676
+ const item = batch[i];
1677
+ if (i === batch.length - 1) pipeline.xadd(this.stream, "MAXLEN", "~", String(this.maxLen), "*", "data", JSON.stringify(item.event));
1678
+ else pipeline.xadd(this.stream, "*", "data", JSON.stringify(item.event));
1679
+ }
1680
+ const results = await pipeline.exec();
1681
+ if (!results) throw new Error(`Pipeline execution failed for ${this.stream}`);
1682
+ for (let i = 0; i < results.length; i++) {
1683
+ const [err, msgId] = results[i] ?? [];
1684
+ if (err) batch[i].reject(err);
1685
+ else batch[i].resolve(msgId);
1686
+ }
1687
+ } else for (let i = 0; i < batch.length; i++) {
1688
+ const item = batch[i];
1689
+ try {
1690
+ const msgId = await this.redis.xadd(this.stream, "MAXLEN", "~", String(this.maxLen), "*", "data", JSON.stringify(item.event));
1691
+ if (!msgId) throw new Error(`XADD to ${this.stream} returned null`);
1692
+ item.resolve(msgId);
1693
+ } catch (err) {
1694
+ item.reject(err);
1695
+ }
1696
+ }
1697
+ this.monitorMaxLen(this.stream).catch(() => {});
1698
+ } catch (err) {
1699
+ for (const item of batch) item.reject(err);
1700
+ }
1221
1701
  }
1222
1702
  async monitorMaxLen(stream) {
1223
- if (Math.random() < .05) try {
1703
+ const now = Date.now();
1704
+ if (now - this.lastMaxLenCheck < 5e3) return;
1705
+ this.lastMaxLenCheck = now;
1706
+ try {
1224
1707
  const len = await this.redis.xlen(stream);
1225
1708
  metrics.queueSize.set({ stream }, len);
1226
1709
  if (len > this.maxLen * .8) this.logger?.warn({
@@ -1239,36 +1722,19 @@ var StreamProducer = class {
1239
1722
  messageIds: [],
1240
1723
  eventIds: []
1241
1724
  };
1242
- const pipeline = this.redis.pipeline();
1243
- const timestamp = (/* @__PURE__ */ new Date()).toISOString();
1244
1725
  const eventIds = [];
1726
+ const promises = [];
1245
1727
  for (const partial of partials) {
1246
1728
  const id = crypto.randomUUID();
1247
1729
  eventIds.push(id);
1248
- const event = {
1730
+ promises.push(this.enqueue({
1249
1731
  ...partial,
1250
1732
  id,
1251
- timestamp
1252
- };
1253
- pipeline.xadd(this.stream, "MAXLEN", "~", String(this.maxLen), "*", "data", JSON.stringify(event));
1254
- }
1255
- const results = await pipeline.exec();
1256
- if (!results) throw new Error(`Pipeline execution failed for ${this.stream}`);
1257
- const messageIds = [];
1258
- for (let i = 0; i < results.length; i++) {
1259
- const result = results[i];
1260
- if (!result) throw new Error("Pipeline result is undefined");
1261
- const [err, msgId] = result;
1262
- if (err) throw err;
1263
- messageIds.push(msgId);
1264
- }
1265
- this.logger?.debug({
1266
- stream: this.stream,
1267
- count: partials.length
1268
- }, "batch events published");
1269
- this.monitorMaxLen(this.stream).catch(() => {});
1733
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
1734
+ }));
1735
+ }
1270
1736
  return {
1271
- messageIds,
1737
+ messageIds: await Promise.all(promises),
1272
1738
  eventIds
1273
1739
  };
1274
1740
  }
@@ -1283,10 +1749,15 @@ var StreamConsumer = class {
1283
1749
  logger;
1284
1750
  batchSize;
1285
1751
  blockMs;
1752
+ bufferAcks;
1753
+ ackFlushMs;
1754
+ coalesceMs;
1755
+ ackBuffers = /* @__PURE__ */ new Map();
1756
+ ackTimer = null;
1286
1757
  running = false;
1287
- constructor({ redis, stream, group, consumer, dlqStream, logger, batchSize = 10, blockMs = 5e3 }) {
1758
+ constructor({ redis, stream, group, consumer, dlqStream, logger, batchSize = 10, blockMs = 5e3, bufferAcks = false, ackFlushMs = 5, coalesceMs = 0 }) {
1288
1759
  this.redis = redis;
1289
- this.blockingRedis = redis.duplicate();
1760
+ this.blockingRedis = redis.duplicate({ enableAutoPipelining: false });
1290
1761
  this.streams = Array.isArray(stream) ? stream : [stream];
1291
1762
  this.group = group;
1292
1763
  this.consumer = consumer;
@@ -1294,6 +1765,9 @@ var StreamConsumer = class {
1294
1765
  this.logger = logger;
1295
1766
  this.batchSize = batchSize;
1296
1767
  this.blockMs = blockMs;
1768
+ this.bufferAcks = bufferAcks;
1769
+ this.ackFlushMs = ackFlushMs;
1770
+ this.coalesceMs = coalesceMs;
1297
1771
  }
1298
1772
  async ensureGroup() {
1299
1773
  for (const s of this.streams) try {
@@ -1344,15 +1818,27 @@ var StreamConsumer = class {
1344
1818
  for (const [streamName, messages] of results) for (const [id, fields] of messages) {
1345
1819
  const msg = parseMessage(id, fields, this.logger);
1346
1820
  if (!msg) {
1347
- await this.redis.xack(streamName, this.group, id);
1821
+ await this.ackAndDelete(streamName, [id]);
1348
1822
  continue;
1349
1823
  }
1350
1824
  msg.stream = streamName;
1825
+ msg.deliveryCount = 1;
1351
1826
  batch.push(msg);
1352
1827
  }
1353
1828
  if (batch.length > 0) yield batch;
1829
+ if (this.coalesceMs > 0 && batch.length < this.batchSize && this.running) await new Promise((resolve) => setTimeout(resolve, this.coalesceMs));
1354
1830
  } catch (err) {
1355
1831
  if (!this.running && err instanceof Error && err?.message?.toLowerCase?.().includes("connection is closed")) break;
1832
+ if (err instanceof Error && err.message.startsWith("NOGROUP")) try {
1833
+ await this.ensureGroup();
1834
+ this.logger?.warn({
1835
+ group: this.group,
1836
+ streams: this.streams
1837
+ }, "consumer group was missing — recreated it");
1838
+ continue;
1839
+ } catch (recreateErr) {
1840
+ this.logger?.error({ err: recreateErr }, "failed to recreate consumer group");
1841
+ }
1356
1842
  this.logger?.error({ err }, "error reading from stream");
1357
1843
  await new Promise((resolve) => setTimeout(resolve, retryDelay));
1358
1844
  retryDelay = Math.min(retryDelay * 2, 3e4);
@@ -1362,24 +1848,96 @@ var StreamConsumer = class {
1362
1848
  const s = stream ?? this.streams[0];
1363
1849
  const ids = Array.isArray(messageId) ? messageId : [messageId];
1364
1850
  if (ids.length === 0) return;
1365
- await this.redis.xack(s, this.group, ...ids);
1366
- this.logger?.debug({
1367
- stream: s,
1368
- count: ids.length
1369
- }, "messages acknowledged");
1851
+ if (!this.bufferAcks) {
1852
+ await this.ackAndDelete(s, ids);
1853
+ this.logger?.debug({
1854
+ stream: s,
1855
+ count: ids.length
1856
+ }, "messages acknowledged");
1857
+ return;
1858
+ }
1859
+ let buf = this.ackBuffers.get(s);
1860
+ if (!buf) {
1861
+ buf = [];
1862
+ this.ackBuffers.set(s, buf);
1863
+ }
1864
+ buf.push(...ids);
1865
+ if (buf.length >= this.batchSize) await this.flushAcksForStream(s);
1866
+ else if (!this.ackTimer) this.ackTimer = setTimeout(() => {
1867
+ this.ackTimer = null;
1868
+ this.flushAcks();
1869
+ }, this.ackFlushMs);
1870
+ }
1871
+ /**
1872
+ * Acks and then deletes the entries. Every stream is read by exactly one
1873
+ * consumer group, so an acked entry is never read again; leaving it in place
1874
+ * holds Redis memory until MAXLEN trims it, and a group recreated from "0"
1875
+ * (ensureGroup) would process it a second time. Both commands are issued
1876
+ * before either is awaited, so auto-pipelining sends them in one round trip,
1877
+ * and Redis runs them in order.
1878
+ */
1879
+ async ackAndDelete(stream, ids) {
1880
+ await Promise.all([this.redis.xack(stream, this.group, ...ids), this.redis.xdel(stream, ...ids)]);
1370
1881
  }
1371
- async nack(messageId, event, stream) {
1882
+ async flushAcksForStream(stream) {
1883
+ const ids = this.ackBuffers.get(stream);
1884
+ if (!ids || ids.length === 0) return;
1885
+ this.ackBuffers.set(stream, []);
1886
+ try {
1887
+ await this.ackAndDelete(stream, ids);
1888
+ this.logger?.debug({
1889
+ stream,
1890
+ count: ids.length
1891
+ }, "buffered messages acknowledged");
1892
+ } catch (err) {
1893
+ this.logger?.error({
1894
+ err,
1895
+ stream,
1896
+ count: ids.length
1897
+ }, "failed to flush buffered acks");
1898
+ }
1899
+ }
1900
+ async flushAcks() {
1901
+ if (this.ackTimer) {
1902
+ clearTimeout(this.ackTimer);
1903
+ this.ackTimer = null;
1904
+ }
1905
+ const streams = Array.from(this.ackBuffers.keys());
1906
+ await Promise.all(streams.map((s) => this.flushAcksForStream(s)));
1907
+ }
1908
+ /**
1909
+ * Writes an event that never had a stream entry of its own (one handed
1910
+ * straight to a worker in-process) to the dead-letter stream, recorded
1911
+ * against this consumer's stream so a replay lands where it would have been
1912
+ * read from.
1913
+ */
1914
+ async deadLetter(event, reason) {
1915
+ if (!this.dlqStream) {
1916
+ this.logger?.error({ eventId: event.id }, "no dead-letter stream configured — dropping event");
1917
+ return;
1918
+ }
1919
+ await this.redis.xadd(this.dlqStream, "*", "data", JSON.stringify({
1920
+ ...event,
1921
+ dlq: {
1922
+ originalStream: this.streams[0],
1923
+ ackedAt: (/* @__PURE__ */ new Date()).toISOString(),
1924
+ reason
1925
+ }
1926
+ }));
1927
+ }
1928
+ async nack(messageId, event, stream, reason) {
1372
1929
  const s = stream ?? this.streams[0];
1373
1930
  if (this.dlqStream) {
1374
1931
  const dlqId = await this.redis.xadd(this.dlqStream, "*", "data", JSON.stringify({
1375
1932
  ...event,
1376
1933
  dlq: {
1377
1934
  originalStream: s,
1378
- ackedAt: (/* @__PURE__ */ new Date()).toISOString()
1935
+ ackedAt: (/* @__PURE__ */ new Date()).toISOString(),
1936
+ reason
1379
1937
  }
1380
1938
  }));
1381
1939
  if (!dlqId) throw new Error(`XADD to dead-letter stream ${this.dlqStream} returned null`);
1382
- await this.redis.xack(s, this.group, messageId);
1940
+ await this.ackAndDelete(s, [messageId]);
1383
1941
  this.logger?.warn({
1384
1942
  stream: s,
1385
1943
  dlqStream: this.dlqStream,
@@ -1391,6 +1949,7 @@ var StreamConsumer = class {
1391
1949
  }
1392
1950
  async stop() {
1393
1951
  this.running = false;
1952
+ await this.flushAcks().catch(() => {});
1394
1953
  try {
1395
1954
  await this.blockingRedis.quit();
1396
1955
  } catch (err) {
@@ -1432,328 +1991,40 @@ var PendingMessageScanner = class {
1432
1991
  for (const item of result) if (Array.isArray(item)) allEntries.push({
1433
1992
  id: item[0],
1434
1993
  consumer: item[1],
1435
- idleMs: item[2],
1436
- deliveryCount: item[3],
1437
- stream: s
1438
- });
1439
- }
1440
- if (allEntries.length >= limit) break;
1441
- }
1442
- return allEntries.slice(0, limit);
1443
- }
1444
- async autoclaim(minIdleMs, limit = 10) {
1445
- const recovered = [];
1446
- for (const s of this.streams) {
1447
- if (recovered.length >= limit) break;
1448
- const toClaim = (await this.getPendingEntries(limit * 2, s)).filter((p) => p.idleMs > minIdleMs * Math.pow(2, p.deliveryCount - 1)).slice(0, limit - recovered.length);
1449
- if (toClaim.length === 0) continue;
1450
- const ids = toClaim.map((p) => p.id);
1451
- const result = await this.redis.xclaim(s, this.group, this.consumer, minIdleMs, ...ids);
1452
- for (const raw of result) {
1453
- if (!raw) continue;
1454
- const [id, fields] = raw;
1455
- const msg = parseMessage(id, fields, this.logger);
1456
- if (msg) {
1457
- msg.stream = s;
1458
- recovered.push(msg);
1459
- }
1460
- }
1461
- }
1462
- if (recovered.length > 0) this.logger?.info({
1463
- group: this.group,
1464
- count: recovered.length
1465
- }, "autoclaimed pending messages");
1466
- return recovered;
1467
- }
1468
- };
1469
- //#endregion
1470
- //#region src/shared/events.ts
1471
- const globalEmitter = new EventEmitter();
1472
- //#endregion
1473
- //#region src/shared/cache.ts
1474
- var LRUCache = class {
1475
- cache = /* @__PURE__ */ new Map();
1476
- maxSize;
1477
- defaultTtlMs;
1478
- constructor(maxSize = 1e3, defaultTtlMs = 3e5) {
1479
- this.maxSize = maxSize;
1480
- this.defaultTtlMs = defaultTtlMs;
1481
- }
1482
- get(key) {
1483
- const item = this.cache.get(key);
1484
- if (!item) return void 0;
1485
- if (Date.now() > item.expiresAt) {
1486
- this.cache.delete(key);
1487
- return;
1488
- }
1489
- this.cache.delete(key);
1490
- this.cache.set(key, item);
1491
- return item.value;
1492
- }
1493
- set(key, value, ttlMs = this.defaultTtlMs) {
1494
- if (this.cache.has(key)) this.cache.delete(key);
1495
- else if (this.cache.size >= this.maxSize) {
1496
- const oldestKey = this.cache.keys().next().value;
1497
- if (oldestKey !== void 0) this.cache.delete(oldestKey);
1498
- }
1499
- this.cache.set(key, {
1500
- value,
1501
- expiresAt: Date.now() + ttlMs
1502
- });
1503
- }
1504
- delete(key) {
1505
- this.cache.delete(key);
1506
- }
1507
- clear() {
1508
- this.cache.clear();
1509
- }
1510
- };
1511
- //#endregion
1512
- //#region src/shared/utils.ts
1513
- function getPriorityBucket(priority) {
1514
- const p = priority || "normal";
1515
- return p === "critical" || p === "high" ? "critical" : p === "low" ? "low" : "normal";
1516
- }
1517
- /**
1518
- * Canonical form of a destination, for suppression lookups.
1519
- *
1520
- * Both the writer (the provider webhook) and the reader (the engine's
1521
- * pre-dispatch gate) must agree on this, or an unsubscribe recorded as
1522
- * `Bob@Example.com` will not match a send addressed to `bob@example.com` and
1523
- * the person keeps receiving mail. Case folding is safe for email domains and
1524
- * for the local part in every mailbox provider in practice; phone numbers and
1525
- * push tokens are case-sensitive and are only trimmed.
1526
- */
1527
- function normaliseTarget(target) {
1528
- const trimmed = target.trim();
1529
- return trimmed.includes("@") ? trimmed.toLowerCase() : trimmed;
1530
- }
1531
- const LUA_SCHEDULER_POLL = `
1532
- local key = KEYS[1]
1533
- local maxScore = tonumber(ARGV[1])
1534
- local limit = tonumber(ARGV[2])
1535
- local visibilityTimeout = tonumber(ARGV[3]) or 0
1536
- local tasks = redis.call('ZRANGE', key, 0, maxScore, 'BYSCORE', 'LIMIT', 0, limit)
1537
- if #tasks > 0 then
1538
- for i, task in ipairs(tasks) do
1539
- redis.call('ZADD', key, maxScore + visibilityTimeout, task)
1540
- end
1541
- end
1542
- return tasks
1543
- `;
1544
- const LUA_SCHEDULER_CLAIM = `
1545
- local payloadKey = KEYS[1]
1546
- local claimedKey = KEYS[2]
1547
-
1548
- if redis.call('EXISTS', payloadKey) == 1 then
1549
- redis.call('RENAME', payloadKey, claimedKey)
1550
- return redis.call('GET', claimedKey)
1551
- elseif redis.call('EXISTS', claimedKey) == 1 then
1552
- return redis.call('GET', claimedKey)
1553
- else
1554
- return nil
1555
- end
1556
- `;
1557
- /** Release a lock only if we still hold it (value matches our token). */
1558
- const LUA_RELEASE_LOCK = `
1559
- if redis.call('GET', KEYS[1]) == ARGV[1] then
1560
- return redis.call('DEL', KEYS[1])
1561
- end
1562
- return 0
1563
- `;
1564
- /** Extend a lock's TTL only if we still hold it. */
1565
- const LUA_RENEW_LOCK = `
1566
- if redis.call('GET', KEYS[1]) == ARGV[1] then
1567
- return redis.call('EXPIRE', KEYS[1], ARGV[2])
1568
- end
1569
- return 0
1570
- `;
1571
- //#endregion
1572
- //#region src/shared/semaphore.ts
1573
- var AsyncSemaphore = class {
1574
- max;
1575
- count = 0;
1576
- queue = [];
1577
- constructor(max) {
1578
- this.max = max;
1579
- }
1580
- async acquire() {
1581
- if (this.count < this.max) {
1582
- this.count++;
1583
- return Promise.resolve();
1584
- }
1585
- return new Promise((resolve) => {
1586
- this.queue.push(resolve);
1587
- });
1588
- }
1589
- release() {
1590
- if (this.queue.length > 0) this.queue.shift()();
1591
- else if (this.count > 0) this.count--;
1592
- }
1593
- get activeCount() {
1594
- return this.count;
1595
- }
1596
- };
1597
- //#endregion
1598
- //#region src/shared/batch-processor.ts
1599
- var BatchProcessor = class {
1600
- maxSize;
1601
- maxWaitMs;
1602
- flushFn;
1603
- buffer = [];
1604
- timer = null;
1605
- isFlushing = false;
1606
- constructor(maxSize, maxWaitMs, flushFn) {
1607
- this.maxSize = maxSize;
1608
- this.maxWaitMs = maxWaitMs;
1609
- this.flushFn = flushFn;
1610
- }
1611
- add(item) {
1612
- return new Promise((resolve, reject) => {
1613
- this.buffer.push({
1614
- item,
1615
- resolve,
1616
- reject
1617
- });
1618
- if (this.buffer.length >= this.maxSize && !this.isFlushing) {
1619
- if (this.timer) {
1620
- clearTimeout(this.timer);
1621
- this.timer = null;
1622
- }
1623
- this.flush();
1624
- } else if (!this.timer && !this.isFlushing) this.timer = setTimeout(() => {
1625
- this.timer = null;
1626
- this.flush();
1627
- }, this.maxWaitMs);
1628
- });
1629
- }
1630
- async flush() {
1631
- if (this.isFlushing || this.buffer.length === 0) return;
1632
- this.isFlushing = true;
1633
- const batch = this.buffer;
1634
- this.buffer = [];
1635
- try {
1636
- const items = batch.map((b) => b.item);
1637
- const results = await this.flushFn(items);
1638
- for (let i = 0; i < batch.length; i++) batch[i].resolve(results[i]);
1639
- } catch (err) {
1640
- for (const b of batch) b.reject(err);
1641
- } finally {
1642
- this.isFlushing = false;
1643
- if (this.buffer.length > 0 && !this.timer) if (this.buffer.length >= this.maxSize) this.flush();
1644
- else this.timer = setTimeout(() => {
1645
- this.timer = null;
1646
- this.flush();
1647
- }, this.maxWaitMs);
1648
- }
1649
- }
1650
- };
1651
- //#endregion
1652
- //#region src/shared/circuit-breaker.ts
1653
- var CircuitBreaker = class {
1654
- state = "CLOSED";
1655
- failures = 0;
1656
- nextAttemptAt = 0;
1657
- /** True while one caller is testing whether the dependency has recovered. */
1658
- probeInFlight = false;
1659
- threshold;
1660
- timeout;
1661
- constructor(options) {
1662
- this.threshold = options.failureThreshold;
1663
- this.timeout = options.resetTimeoutMs;
1664
- }
1665
- async execute(action) {
1666
- let isProbe = false;
1667
- if (this.state === "OPEN") if (Date.now() > this.nextAttemptAt && !this.probeInFlight) {
1668
- this.state = "HALF_OPEN";
1669
- this.probeInFlight = true;
1670
- isProbe = true;
1671
- } else throw new Error("Circuit breaker is OPEN");
1672
- else if (this.state === "HALF_OPEN") {
1673
- if (this.probeInFlight) throw new Error("Circuit breaker is OPEN");
1674
- this.probeInFlight = true;
1675
- isProbe = true;
1676
- }
1677
- try {
1678
- const result = await action();
1679
- this.onSuccess();
1680
- return result;
1681
- } catch (err) {
1682
- this.onFailure();
1683
- throw err;
1684
- } finally {
1685
- if (isProbe) this.probeInFlight = false;
1686
- }
1687
- }
1688
- onSuccess() {
1689
- this.failures = 0;
1690
- this.state = "CLOSED";
1691
- }
1692
- onFailure() {
1693
- this.failures++;
1694
- if (this.failures >= this.threshold) {
1695
- this.state = "OPEN";
1696
- this.nextAttemptAt = Date.now() + this.timeout;
1697
- }
1698
- }
1699
- getState() {
1700
- return this.state;
1701
- }
1702
- };
1703
- //#endregion
1704
- //#region src/shared/dataloader.ts
1705
- var DataLoader = class {
1706
- batchLoadFn;
1707
- keys = [];
1708
- promises = [];
1709
- currentTick = null;
1710
- constructor(batchLoadFn) {
1711
- this.batchLoadFn = batchLoadFn;
1712
- }
1713
- load(key) {
1714
- return new Promise((resolve, reject) => {
1715
- this.keys.push(key);
1716
- this.promises.push({ resolve: (value) => {
1717
- if (value instanceof Error) reject(value);
1718
- else resolve(value);
1719
- } });
1720
- if (!this.currentTick) this.currentTick = Promise.resolve().then(() => {
1721
- const keysToLoad = this.keys;
1722
- const currentPromises = this.promises;
1723
- this.keys = [];
1724
- this.promises = [];
1725
- this.currentTick = null;
1726
- this.batchLoadFn(keysToLoad).then((results) => {
1727
- for (let i = 0; i < currentPromises.length; i++) currentPromises[i].resolve(results[i]);
1728
- }).catch((err) => {
1729
- for (const p of currentPromises) p.resolve(err);
1994
+ idleMs: item[2],
1995
+ deliveryCount: item[3],
1996
+ stream: s
1730
1997
  });
1731
- });
1732
- });
1733
- }
1734
- };
1735
- //#endregion
1736
- //#region src/shared/index.ts
1737
- function generateId() {
1738
- return randomUUID();
1739
- }
1740
- function sleep(ms) {
1741
- return new Promise((resolve) => setTimeout(resolve, ms));
1742
- }
1743
- var AppError = class extends Error {
1744
- code;
1745
- constructor(message, code, options) {
1746
- super(message, options);
1747
- this.name = "AppError";
1748
- this.code = code;
1998
+ }
1999
+ if (allEntries.length >= limit) break;
2000
+ }
2001
+ return allEntries.slice(0, limit);
1749
2002
  }
1750
- };
1751
- var ValidationError = class extends AppError {
1752
- fields;
1753
- constructor(message, fields, options) {
1754
- super(message, "VALIDATION_ERROR", options);
1755
- this.name = "ValidationError";
1756
- this.fields = fields;
2003
+ async autoclaim(minIdleMs, limit = 10) {
2004
+ const recovered = [];
2005
+ for (const s of this.streams) {
2006
+ if (recovered.length >= limit) break;
2007
+ const toClaim = (await this.getPendingEntries(limit * 2, s)).filter((p) => p.idleMs > minIdleMs * Math.pow(2, p.deliveryCount - 1)).slice(0, limit - recovered.length);
2008
+ if (toClaim.length === 0) continue;
2009
+ const ids = toClaim.map((p) => p.id);
2010
+ const countsById = new Map(toClaim.map((p) => [p.id, p.deliveryCount]));
2011
+ const result = await this.redis.xclaim(s, this.group, this.consumer, minIdleMs, ...ids);
2012
+ for (const raw of result) {
2013
+ if (!raw) continue;
2014
+ const [id, fields] = raw;
2015
+ const msg = parseMessage(id, fields, this.logger);
2016
+ if (msg) {
2017
+ msg.stream = s;
2018
+ msg.deliveryCount = (countsById.get(id) ?? 1) + 1;
2019
+ recovered.push(msg);
2020
+ }
2021
+ }
2022
+ }
2023
+ if (recovered.length > 0) this.logger?.info({
2024
+ group: this.group,
2025
+ count: recovered.length
2026
+ }, "autoclaimed pending messages");
2027
+ return recovered;
1757
2028
  }
1758
2029
  };
1759
2030
  //#endregion
@@ -1853,20 +2124,10 @@ var UserThrottle = class {
1853
2124
  const key = `throttle:${projectId}:user:${userId}`;
1854
2125
  const targetTime = options.scheduledAt ? new Date(options.scheduledAt).getTime() : Date.now();
1855
2126
  const windowStart = targetTime - windowMs;
1856
- const memberId = randomUUID$1();
1857
- const LUA_THROTTLE = `
1858
- redis.call("ZREMRANGEBYSCORE", KEYS[1], "-inf", ARGV[1])
1859
- local count = redis.call("ZCARD", KEYS[1])
1860
- if tonumber(count) < tonumber(ARGV[2]) then
1861
- redis.call("ZADD", KEYS[1], tonumber(ARGV[3]), ARGV[4])
1862
- redis.call("EXPIRE", KEYS[1], tonumber(ARGV[5]))
1863
- return tonumber(count) + 1
1864
- end
1865
- return tonumber(count) + 1
1866
- `;
2127
+ const memberId = options.messageId ?? randomUUID$1();
1867
2128
  const windowSeconds = Math.ceil(windowMs / 1e3);
1868
2129
  const ttlSeconds = Math.max(windowSeconds, Math.ceil((targetTime - Date.now()) / 1e3) + windowSeconds);
1869
- const count = await this.redis.eval(LUA_THROTTLE, 1, key, windowStart, limit, targetTime, memberId, ttlSeconds);
2130
+ const count = await this.redis.throttleUser(key, windowStart, limit, targetTime, memberId, ttlSeconds, Date.now() - windowMs);
1870
2131
  return {
1871
2132
  allowed: count <= limit,
1872
2133
  count,
@@ -1874,10 +2135,330 @@ var UserThrottle = class {
1874
2135
  };
1875
2136
  }
1876
2137
  };
2138
+ /**
2139
+ * Computes dynamic token lease batch size based on RPM.
2140
+ * Targets ~5s of limit capacity so Redis is contacted at most ~12 times/min
2141
+ * regardless of instantaneous request rate. Bounded between 1 and maxBatchSize.
2142
+ */
2143
+ function calculateTokenBatchSize(limitRpm, maxBatchSize = 500) {
2144
+ if (limitRpm <= 0) return 0;
2145
+ if (limitRpm <= 10) return 1;
2146
+ if (limitRpm <= 60) return 5;
2147
+ const perSecond = limitRpm / 60;
2148
+ return Math.min(Math.max(Math.ceil(perSecond * 5), 10), maxBatchSize);
2149
+ }
2150
+ var ApiRateLimiter = class {
2151
+ redis;
2152
+ windowMs;
2153
+ maxBatchSize;
2154
+ logger;
2155
+ keyPrefix;
2156
+ leases = /* @__PURE__ */ new Map();
2157
+ constructor({ redis, windowMs = 6e4, maxBatchSize = 500, logger, keyPrefix = "rate-limit:api:req" }) {
2158
+ this.redis = redis;
2159
+ this.windowMs = windowMs;
2160
+ this.maxBatchSize = maxBatchSize;
2161
+ this.logger = logger;
2162
+ this.keyPrefix = keyPrefix;
2163
+ }
2164
+ /**
2165
+ * Evaluates if a request for `projectId` is allowed within its `limitRpm`.
2166
+ * Fast-path uses local in-memory token lease (0ms Redis network latency).
2167
+ * Refills in the background before tokens are depleted.
2168
+ */
2169
+ async check(projectId, limitRpm) {
2170
+ if (limitRpm <= 0) return false;
2171
+ const nowMs = Date.now();
2172
+ const currentBucket = Math.floor(nowMs / this.windowMs);
2173
+ let state = this.leases.get(projectId);
2174
+ if (!state) {
2175
+ state = {
2176
+ bucketIndex: currentBucket,
2177
+ tokens: 0,
2178
+ batchSize: calculateTokenBatchSize(limitRpm, this.maxBatchSize),
2179
+ inFlightLease: null
2180
+ };
2181
+ this.leases.set(projectId, state);
2182
+ this.maybePrune(currentBucket);
2183
+ }
2184
+ if (state.bucketIndex !== currentBucket) {
2185
+ state.bucketIndex = currentBucket;
2186
+ state.tokens = 0;
2187
+ state.batchSize = calculateTokenBatchSize(limitRpm, this.maxBatchSize);
2188
+ state.inFlightLease = null;
2189
+ this.refill(projectId, state, limitRpm, nowMs, currentBucket).catch(() => {});
2190
+ }
2191
+ if (state.tokens > 0) {
2192
+ state.tokens--;
2193
+ const refillThreshold = Math.max(1, Math.floor(state.batchSize * .25));
2194
+ if (state.tokens <= refillThreshold && !state.inFlightLease) this.refill(projectId, state, limitRpm, nowMs, currentBucket).catch(() => {});
2195
+ return true;
2196
+ }
2197
+ for (;;) {
2198
+ if (!state.inFlightLease) this.refill(projectId, state, limitRpm, nowMs, currentBucket).catch(() => {});
2199
+ const granted = state.inFlightLease ? await state.inFlightLease : 0;
2200
+ if (state.bucketIndex !== currentBucket) return false;
2201
+ if (state.tokens > 0) {
2202
+ state.tokens--;
2203
+ return true;
2204
+ }
2205
+ if (granted <= 0) return false;
2206
+ }
2207
+ }
2208
+ async refill(projectId, state, limitRpm, nowMs, bucketIndex) {
2209
+ if (state.inFlightLease) return state.inFlightLease;
2210
+ const prevBucket = bucketIndex - 1;
2211
+ const rlTag = `{${this.keyPrefix}:${projectId || "global"}}`;
2212
+ const currentKey = `${rlTag}:${bucketIndex}`;
2213
+ const prevKey = `${rlTag}:${prevBucket}`;
2214
+ const requested = state.batchSize;
2215
+ let inFlightRef = null;
2216
+ const executeLease = async () => {
2217
+ try {
2218
+ let granted = await this.redis.leaseApiRateLimit(currentKey, prevKey, nowMs, this.windowMs, limitRpm, requested);
2219
+ if (typeof granted !== "number" || granted < 0) granted = requested;
2220
+ if (state.bucketIndex === bucketIndex) state.tokens += granted;
2221
+ return granted;
2222
+ } catch (err) {
2223
+ this.logger?.warn({
2224
+ err,
2225
+ projectId
2226
+ }, "project rate limit lease failed — allowing request (fail-open)");
2227
+ if (state.bucketIndex === bucketIndex) state.tokens += requested;
2228
+ return requested;
2229
+ } finally {
2230
+ if (state.inFlightLease === inFlightRef) state.inFlightLease = null;
2231
+ }
2232
+ };
2233
+ const promise = executeLease();
2234
+ inFlightRef = promise;
2235
+ state.inFlightLease = promise;
2236
+ return promise;
2237
+ }
2238
+ maybePrune(currentBucket) {
2239
+ if (this.leases.size > 2e3) {
2240
+ for (const [id, s] of this.leases) if (currentBucket - s.bucketIndex > 1) this.leases.delete(id);
2241
+ }
2242
+ }
2243
+ /** Reset all local cached leases */
2244
+ clear() {
2245
+ this.leases.clear();
2246
+ }
2247
+ /** Invalidate local lease for a specific project */
2248
+ invalidate(projectId) {
2249
+ this.leases.delete(projectId);
2250
+ }
2251
+ };
1877
2252
  //#endregion
1878
2253
  //#region src/redis/index.ts
1879
- var RedisClient = class {
2254
+ /**
2255
+ * Sliding-window token leasing for high-throughput API rate limiting.
2256
+ * Atomically checks available capacity and reserves up to N tokens in one round trip.
2257
+ */
2258
+ const LUA_LEASE_API_RATE_LIMIT = `
2259
+ local currentKey = KEYS[1]
2260
+ local prevKey = KEYS[2]
2261
+ local now = tonumber(ARGV[1])
2262
+ local window = tonumber(ARGV[2])
2263
+ local maxReqs = tonumber(ARGV[3])
2264
+ local requested = tonumber(ARGV[4]) or 1
2265
+ local ttl = tonumber(ARGV[5]) or (math.ceil((window * 2) / 1000) + 60)
2266
+
2267
+ if not now or not window or window <= 0 then
2268
+ return -1
2269
+ end
2270
+ if not maxReqs or maxReqs <= 0 then
2271
+ return 0
2272
+ end
2273
+
2274
+ if not prevKey then
2275
+ local currentBucket = math.floor(now / window)
2276
+ local prevBucket = currentBucket - 1
2277
+ currentKey = KEYS[1] .. ":" .. currentBucket
2278
+ prevKey = KEYS[1] .. ":" .. prevBucket
2279
+ end
2280
+
2281
+ local currentCount = tonumber(redis.call("GET", currentKey) or "0")
2282
+ local prevCount = tonumber(redis.call("GET", prevKey) or "0")
2283
+
2284
+ local timeIntoCurrent = now % window
2285
+ local weight = (window - timeIntoCurrent) / window
2286
+ local estimated = math.floor(prevCount * weight + currentCount)
2287
+ local available = maxReqs - estimated
2288
+
2289
+ if available <= 0 then
2290
+ return 0
2291
+ end
2292
+
2293
+ local toGrant = math.min(requested, available)
2294
+ local newCount = redis.call("INCRBY", currentKey, toGrant)
2295
+ if newCount == toGrant then
2296
+ redis.call("EXPIRE", currentKey, ttl)
2297
+ end
2298
+
2299
+ return toGrant
2300
+ `;
2301
+ /** Release a lock only if we still hold it (value matches our token). */
2302
+ const LUA_RELEASE_LOCK = `
2303
+ if redis.call('GET', KEYS[1]) == ARGV[1] then
2304
+ return redis.call('DEL', KEYS[1])
2305
+ end
2306
+ return 0
2307
+ `;
2308
+ /** Extend a lock's TTL only if we still hold it. */
2309
+ const LUA_RENEW_LOCK = `
2310
+ if redis.call('GET', KEYS[1]) == ARGV[1] then
2311
+ return redis.call('EXPIRE', KEYS[1], ARGV[2])
2312
+ end
2313
+ return 0
2314
+ `;
2315
+ /**
2316
+ * User-level sliding window notification throttle.
2317
+ *
2318
+ * Members are sends scored by when they go out, which for a scheduled send is
2319
+ * in the future. A send at targetTime is checked against the window ending at
2320
+ * it, (windowStart, targetTime], so sends scheduled later do not count against
2321
+ * it. Pruning uses pruneBefore (now minus one window), never windowStart: a
2322
+ * check for a far-future send must not delete sends that went out today.
2323
+ *
2324
+ * A member already present was counted by an earlier attempt at the same
2325
+ * message (a replay after a crash), so it is allowed again without counting
2326
+ * twice. Returns 0 in that case, otherwise the would-be count.
2327
+ *
2328
+ * ARGV: windowStart, limit, targetTime, member, ttlSeconds, pruneBefore
2329
+ */
2330
+ const LUA_USER_THROTTLE = `
2331
+ local limit = tonumber(ARGV[2])
2332
+ local targetTime = tonumber(ARGV[3])
2333
+ local ttl = tonumber(ARGV[5])
2334
+ redis.call("ZREMRANGEBYSCORE", KEYS[1], "-inf", ARGV[6] or ARGV[1])
2335
+ if redis.call("ZSCORE", KEYS[1], ARGV[4]) then
2336
+ return 0
2337
+ end
2338
+ local count = redis.call("ZCOUNT", KEYS[1], "(" .. ARGV[1], targetTime)
2339
+ if count < limit then
2340
+ redis.call("ZADD", KEYS[1], targetTime, ARGV[4])
2341
+ -- Only ever extend: a send today must not cut short the life of a key
2342
+ -- that is still holding a send scheduled for next week.
2343
+ if redis.call("TTL", KEYS[1]) < ttl then
2344
+ redis.call("EXPIRE", KEYS[1], ttl)
2345
+ end
2346
+ end
2347
+ return count + 1
2348
+ `;
2349
+ /** Polls scheduler zsets with visibility timeouts. */
2350
+ const LUA_SCHEDULER_POLL = `
2351
+ local key = KEYS[1]
2352
+ local maxScore = tonumber(ARGV[1])
2353
+ local limit = tonumber(ARGV[2])
2354
+ local visibilityTimeout = tonumber(ARGV[3]) or 0
2355
+ local tasks = redis.call('ZRANGE', key, 0, maxScore, 'BYSCORE', 'LIMIT', 0, limit)
2356
+ if #tasks > 0 then
2357
+ for i, task in ipairs(tasks) do
2358
+ redis.call('ZADD', key, maxScore + visibilityTimeout, task)
2359
+ end
2360
+ end
2361
+ return tasks
2362
+ `;
2363
+ /** Two-phase idempotency lease acquisition. Checks sent/legacy, then attempts SET NX lock. */
2364
+ const LUA_ACQUIRE_LEASE = `
2365
+ if redis.call('EXISTS', KEYS[1]) == 1 or redis.call('EXISTS', KEYS[3]) == 1 then
2366
+ return 'completed'
2367
+ end
2368
+ local lockAcquired = redis.call('SET', KEYS[2], '1', 'EX', ARGV[1], 'NX')
2369
+ if lockAcquired then
2370
+ return 'acquired'
2371
+ end
2372
+ if redis.call('EXISTS', KEYS[1]) == 1 or redis.call('EXISTS', KEYS[3]) == 1 then
2373
+ return 'completed'
2374
+ end
2375
+ return 'locked'
2376
+ `;
2377
+ /** Two-phase idempotency completion marker. Atomically sets completed + legacy and deletes lock. */
2378
+ const LUA_MARK_PROCESSED = `
2379
+ redis.call('SET', KEYS[1], '1', 'EX', ARGV[1])
2380
+ redis.call('SET', KEYS[2], '1', 'EX', ARGV[1])
2381
+ redis.call('DEL', KEYS[3])
2382
+ return 1
2383
+ `;
2384
+ /**
2385
+ * Registers pre-compiled custom commands on an ioredis instance.
2386
+ * Calls defineCommand so ioredis uses EVALSHA rather than re-transmitting
2387
+ * full script strings over the wire.
2388
+ */
2389
+ function registerCustomCommands(redis) {
2390
+ redis.defineCommand("leaseApiRateLimit", {
2391
+ numberOfKeys: 2,
2392
+ lua: LUA_LEASE_API_RATE_LIMIT
2393
+ });
2394
+ redis.defineCommand("releaseLock", {
2395
+ numberOfKeys: 1,
2396
+ lua: LUA_RELEASE_LOCK
2397
+ });
2398
+ redis.defineCommand("renewLock", {
2399
+ numberOfKeys: 1,
2400
+ lua: LUA_RENEW_LOCK
2401
+ });
2402
+ redis.defineCommand("throttleUser", {
2403
+ numberOfKeys: 1,
2404
+ lua: LUA_USER_THROTTLE
2405
+ });
2406
+ redis.defineCommand("schedulerPoll", {
2407
+ numberOfKeys: 1,
2408
+ lua: LUA_SCHEDULER_POLL
2409
+ });
2410
+ redis.defineCommand("acquireLease", {
2411
+ numberOfKeys: 3,
2412
+ lua: LUA_ACQUIRE_LEASE
2413
+ });
2414
+ redis.defineCommand("markProcessed", {
2415
+ numberOfKeys: 3,
2416
+ lua: LUA_MARK_PROCESSED
2417
+ });
2418
+ }
2419
+ const sharedClients = /* @__PURE__ */ new Map();
2420
+ var RedisClient = class RedisClient {
1880
2421
  native;
2422
+ /**
2423
+ * One command connection per Redis URL for every service in this process.
2424
+ *
2425
+ * Each service used to open its own, so a single notification's commands
2426
+ * reached Redis over three or four sockets, one round trip each. Sharing a
2427
+ * connection lets auto-pipelining merge whatever the services issue in the
2428
+ * same tick into one write, and Redis spends most of its time per round trip
2429
+ * rather than per command. Blocking reads and subscribers still duplicate
2430
+ * their own connections.
2431
+ *
2432
+ * The handle's `disconnect()` releases this caller's reference; the
2433
+ * connection closes when the last service lets go.
2434
+ */
2435
+ static shared(options) {
2436
+ let entry = sharedClients.get(options.url);
2437
+ if (!entry) {
2438
+ entry = {
2439
+ client: new RedisClient({
2440
+ ...options,
2441
+ name: "notifkit"
2442
+ }),
2443
+ refs: 0
2444
+ };
2445
+ sharedClients.set(options.url, entry);
2446
+ }
2447
+ entry.refs++;
2448
+ const owner = entry;
2449
+ let released = false;
2450
+ const handle = Object.create(owner.client);
2451
+ handle.disconnect = async () => {
2452
+ if (released) return;
2453
+ released = true;
2454
+ owner.refs--;
2455
+ if (owner.refs === 0) {
2456
+ if (sharedClients.get(options.url) === owner) sharedClients.delete(options.url);
2457
+ await owner.client.disconnect();
2458
+ }
2459
+ };
2460
+ return handle;
2461
+ }
1881
2462
  logger;
1882
2463
  isClosing = false;
1883
2464
  constructor({ url, name = "notifkit", logger, redisOptions }) {
@@ -1887,8 +2468,10 @@ var RedisClient = class {
1887
2468
  enableReadyCheck: true,
1888
2469
  lazyConnect: false,
1889
2470
  connectionName: name,
2471
+ enableAutoPipelining: true,
1890
2472
  ...redisOptions
1891
2473
  });
2474
+ registerCustomCommands(this.native);
1892
2475
  this.native.on("connect", () => {
1893
2476
  this.logger?.info({ url: redactUrl(url) }, "redis connected");
1894
2477
  });
@@ -1937,8 +2520,16 @@ function redactUrl(url) {
1937
2520
  }
1938
2521
  //#endregion
1939
2522
  //#region src/repositories/index.ts
2523
+ const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
2524
+ /**
2525
+ * Applied when a profile is read, not when it is written: an upsert that
2526
+ * leaves a field out must keep the stored value rather than reset it.
2527
+ */
2528
+ const DEFAULT_LANGUAGE = "en";
2529
+ const DEFAULT_TIMEZONE = "UTC";
1940
2530
  var UserRepository = class {
1941
2531
  db;
2532
+ recordCache = new LRUCache(5e3, 3e4);
1942
2533
  constructor(db) {
1943
2534
  this.db = db;
1944
2535
  }
@@ -1948,49 +2539,30 @@ var UserRepository = class {
1948
2539
  const attrs = rows[0].attributes;
1949
2540
  return {
1950
2541
  userId: rows[0].externalId,
1951
- language: attrs.language,
1952
- timezone: attrs.timezone,
2542
+ language: attrs.language ?? DEFAULT_LANGUAGE,
2543
+ timezone: attrs.timezone ?? DEFAULT_TIMEZONE,
1953
2544
  email: attrs.email
1954
2545
  };
1955
2546
  }
1956
2547
  async findRecordById(projectId, userId) {
1957
- const userRows = await this.db.select().from(users).where(and(eq(users.externalId, userId), eq(users.projectId, projectId)));
1958
- if (!userRows[0]) return null;
1959
- const userRow = userRows[0];
1960
- const internalId = userRow.id;
1961
- const attrs = userRow.attributes;
1962
- const [segmentRows, topicRows, channelRows, qhRows] = await Promise.all([
1963
- this.db.select().from(userSegments).where(eq(userSegments.userId, internalId)),
1964
- this.db.select().from(userTopicPreferences).where(eq(userTopicPreferences.userId, internalId)),
1965
- this.db.select().from(userChannelPreferences).where(eq(userChannelPreferences.userId, internalId)),
1966
- this.db.select().from(quietHours).where(eq(quietHours.userId, internalId))
1967
- ]);
1968
- const segments = segmentRows.map((r) => r.segment);
1969
- const topics = {};
1970
- for (const r of topicRows) topics[r.topic] = r.enabled;
1971
- const channels = {};
1972
- for (const r of channelRows) channels[r.channel] = r.enabled;
1973
- const quietHoursList = qhRows.map((r) => ({
1974
- start: r.startTime.slice(0, 5),
1975
- end: r.endTime.slice(0, 5)
1976
- }));
1977
- return {
1978
- userId: userRow.externalId,
1979
- language: attrs.language,
1980
- timezone: attrs.timezone,
1981
- email: attrs.email,
1982
- segments,
1983
- preferences: {
1984
- channels,
1985
- topics,
1986
- quietHours: quietHoursList.length > 0 ? quietHoursList : void 0
1987
- }
1988
- };
2548
+ const cacheKey = `${projectId}:${userId}`;
2549
+ const cached = this.recordCache.get(cacheKey);
2550
+ if (cached !== void 0) return cached;
2551
+ return (await this.findRecordsByIds(projectId, [userId]))[0] ?? null;
1989
2552
  }
1990
2553
  async findRecordsByIds(projectId, userIds) {
1991
2554
  if (userIds.length === 0) return [];
1992
- const usersRows = await this.db.select().from(users).where(and(inArray(users.externalId, userIds), eq(users.projectId, projectId)));
1993
- if (usersRows.length === 0) return [];
2555
+ const results = [];
2556
+ const missingIds = [];
2557
+ for (const id of userIds) {
2558
+ const cacheKey = `${projectId}:${id}`;
2559
+ const cached = this.recordCache.get(cacheKey);
2560
+ if (cached !== void 0) results.push(cached);
2561
+ else missingIds.push(id);
2562
+ }
2563
+ if (missingIds.length === 0) return results;
2564
+ const usersRows = await this.db.select().from(users).where(and(inArray(users.externalId, missingIds), eq(users.projectId, projectId)));
2565
+ if (usersRows.length === 0) return results;
1994
2566
  const internalIds = usersRows.map((r) => r.id);
1995
2567
  const [segmentRows, topicRows, channelRows, qhRows] = await Promise.all([
1996
2568
  this.db.select().from(userSegments).where(inArray(userSegments.userId, internalIds)),
@@ -2022,14 +2594,13 @@ var UserRepository = class {
2022
2594
  end: r.endTime.slice(0, 5)
2023
2595
  });
2024
2596
  }
2025
- const userRecords = [];
2026
2597
  for (const userRow of usersRows) {
2027
2598
  const attrs = userRow.attributes;
2028
2599
  const internalId = userRow.id;
2029
- userRecords.push({
2600
+ const record = {
2030
2601
  userId: userRow.externalId,
2031
- language: attrs.language,
2032
- timezone: attrs.timezone,
2602
+ language: attrs.language ?? DEFAULT_LANGUAGE,
2603
+ timezone: attrs.timezone ?? DEFAULT_TIMEZONE,
2033
2604
  email: attrs.email,
2034
2605
  segments: segmentsByUserId.get(internalId) || [],
2035
2606
  preferences: {
@@ -2037,9 +2608,11 @@ var UserRepository = class {
2037
2608
  topics: topicsByUserId.get(internalId) || {},
2038
2609
  quietHours: qhByUserId.get(internalId)
2039
2610
  }
2040
- });
2611
+ };
2612
+ this.recordCache.set(`${projectId}:${record.userId}`, record);
2613
+ results.push(record);
2041
2614
  }
2042
- return userRecords;
2615
+ return results;
2043
2616
  }
2044
2617
  async upsertFull(projectId, user) {
2045
2618
  await this.db.transaction(async (tx) => {
@@ -2099,13 +2672,16 @@ var UserRepository = class {
2099
2672
  })));
2100
2673
  }
2101
2674
  });
2675
+ this.recordCache.delete(`${projectId}:${user.userId}`);
2102
2676
  }
2103
2677
  async upsertManyFull(projectId, usersList) {
2104
2678
  if (usersList.length === 0) return;
2679
+ const uniqueUsers = [...new Map(usersList.map((u) => [u.userId, u])).values()];
2680
+ const winners = new Set(uniqueUsers);
2105
2681
  let attempts = 0;
2106
2682
  while (attempts < 3) try {
2107
2683
  await this.db.transaction(async (tx) => {
2108
- await tx.insert(users).values(usersList.map((u) => ({
2684
+ await tx.insert(users).values(uniqueUsers.map((u) => ({
2109
2685
  projectId,
2110
2686
  externalId: u.userId,
2111
2687
  attributes: {
@@ -2116,7 +2692,7 @@ var UserRepository = class {
2116
2692
  }))).onConflictDoUpdate({
2117
2693
  target: [users.projectId, users.externalId],
2118
2694
  set: {
2119
- attributes: sql`excluded.attributes`,
2695
+ attributes: sql`${users.attributes} || excluded.attributes`,
2120
2696
  updatedAt: /* @__PURE__ */ new Date()
2121
2697
  }
2122
2698
  });
@@ -2146,7 +2722,7 @@ var UserRepository = class {
2146
2722
  channel,
2147
2723
  enabled
2148
2724
  });
2149
- if (u.preferences?.quietHours && u.preferences.quietHours.length > 0) for (const window of u.preferences.quietHours) quietHoursInserts.push({
2725
+ if (winners.has(u) && u.preferences?.quietHours && u.preferences.quietHours.length > 0) for (const window of u.preferences.quietHours) quietHoursInserts.push({
2150
2726
  userId: internalId,
2151
2727
  startTime: window.start,
2152
2728
  endTime: window.end
@@ -2176,10 +2752,11 @@ var UserRepository = class {
2176
2752
  target: [userChannelPreferences.userId, userChannelPreferences.channel],
2177
2753
  set: { enabled: sql`excluded.enabled` }
2178
2754
  });
2179
- const internalIdsToClearQuietHours = usersList.filter((u) => u.preferences?.quietHours !== void 0).map((u) => idMap.get(u.userId)).filter(Boolean);
2755
+ const internalIdsToClearQuietHours = uniqueUsers.filter((u) => u.preferences?.quietHours !== void 0).map((u) => idMap.get(u.userId)).filter(Boolean);
2180
2756
  if (internalIdsToClearQuietHours.length > 0) await tx.delete(quietHours).where(inArray(quietHours.userId, internalIdsToClearQuietHours));
2181
2757
  if (quietHoursInserts.length > 0) await tx.insert(quietHours).values(quietHoursInserts);
2182
2758
  });
2759
+ for (const u of usersList) this.recordCache.delete(`${projectId}:${u.userId}`);
2183
2760
  return;
2184
2761
  } catch (err) {
2185
2762
  attempts++;
@@ -2193,7 +2770,7 @@ var UserRepository = class {
2193
2770
  const attrs = {
2194
2771
  language: patch.language ?? existing.language,
2195
2772
  timezone: patch.timezone ?? existing.timezone,
2196
- email: patch.email ?? existing.email
2773
+ email: patch.email !== void 0 ? patch.email : existing.email
2197
2774
  };
2198
2775
  await this.db.transaction(async (tx) => {
2199
2776
  await tx.update(users).set({
@@ -2240,38 +2817,37 @@ var UserRepository = class {
2240
2817
  })));
2241
2818
  }
2242
2819
  });
2820
+ this.recordCache.delete(`${projectId}:${userId}`);
2243
2821
  return true;
2244
2822
  }
2245
2823
  async delete(projectId, userId) {
2824
+ this.recordCache.delete(`${projectId}:${userId}`);
2246
2825
  return (await this.db.delete(users).where(and(eq(users.externalId, userId), eq(users.projectId, projectId))).returning()).length > 0;
2247
2826
  }
2248
2827
  async list(projectId, limit, cursor, filters) {
2249
2828
  const conditions = [eq(users.projectId, projectId)];
2250
- if (cursor) {
2251
- const cursorDate = new Date(parseInt(cursor, 10));
2252
- if (!isNaN(cursorDate.getTime())) conditions.push(sql`${users.createdAt} < ${cursorDate.toISOString()}`);
2253
- }
2829
+ if (cursor && UUID_RE.test(cursor)) conditions.push(sql`(${users.createdAt}, ${users.id}) < (SELECT c.created_at, c.id FROM ${users} c WHERE c.id = ${cursor})`);
2254
2830
  if (filters?.language) conditions.push(sql`(${users.attributes}->>'language') = ${filters.language}`);
2255
2831
  if (filters?.timezone) conditions.push(sql`(${users.attributes}->>'timezone') = ${filters.timezone}`);
2256
2832
  if (filters?.search) {
2257
- const term = `%${filters.search.trim()}%`;
2833
+ const term = `%${filters.search.trim().replace(/[\\%_]/g, "\\$&")}%`;
2258
2834
  conditions.push(sql`(${users.externalId} ILIKE ${term} OR (${users.attributes}->>'email') ILIKE ${term})`);
2259
2835
  }
2260
2836
  if (filters?.segment) conditions.push(sql`EXISTS (SELECT 1 FROM ${userSegments} WHERE ${userSegments.userId} = ${users.id} AND ${userSegments.segment} = ${filters.segment})`);
2261
2837
  if (filters?.channel) conditions.push(sql`EXISTS (SELECT 1 FROM ${userContacts} WHERE ${userContacts.userId} = ${users.id} AND ${userContacts.channel} = ${filters.channel})`);
2262
- const items = (await this.db.select().from(users).where(and(...conditions)).orderBy(desc(users.createdAt)).limit(limit)).map((r) => {
2263
- const attrs = r.attributes;
2264
- return {
2265
- userId: r.externalId,
2266
- language: attrs.language,
2267
- timezone: attrs.timezone,
2268
- email: attrs.email,
2269
- createdAt: r.createdAt.getTime()
2270
- };
2271
- });
2838
+ const rows = await this.db.select().from(users).where(and(...conditions)).orderBy(desc(users.createdAt), desc(users.id)).limit(limit);
2272
2839
  return {
2273
- users: items,
2274
- nextCursor: items.length === limit ? items[items.length - 1].createdAt.toString() : null
2840
+ users: rows.map((r) => {
2841
+ const attrs = r.attributes;
2842
+ return {
2843
+ userId: r.externalId,
2844
+ language: attrs.language ?? DEFAULT_LANGUAGE,
2845
+ timezone: attrs.timezone ?? DEFAULT_TIMEZONE,
2846
+ email: attrs.email,
2847
+ createdAt: r.createdAt.getTime()
2848
+ };
2849
+ }),
2850
+ nextCursor: rows.length === limit ? rows[rows.length - 1].id : null
2275
2851
  };
2276
2852
  }
2277
2853
  async findUsersBySegment(projectId, segmentName) {
@@ -2302,6 +2878,7 @@ var PreferenceRepository = class {
2302
2878
  };
2303
2879
  var ContactRepository = class {
2304
2880
  db;
2881
+ activeContactsCache = new LRUCache(5e3, 3e4);
2305
2882
  constructor(db) {
2306
2883
  this.db = db;
2307
2884
  }
@@ -2329,24 +2906,49 @@ var ContactRepository = class {
2329
2906
  async findActiveByUserIds(projectId, userIds) {
2330
2907
  const byUser = /* @__PURE__ */ new Map();
2331
2908
  if (userIds.length === 0) return byUser;
2909
+ const missingIds = [];
2910
+ for (const userId of userIds) {
2911
+ const cacheKey = `${projectId}:${userId}`;
2912
+ const cached = this.activeContactsCache.get(cacheKey);
2913
+ if (cached !== void 0) {
2914
+ if (cached.length > 0) byUser.set(userId, cached);
2915
+ } else missingIds.push(userId);
2916
+ }
2917
+ if (missingIds.length === 0) return byUser;
2332
2918
  const rows = await this.db.select({
2333
2919
  userId: users.externalId,
2334
2920
  id: userContacts.id,
2335
2921
  channel: userContacts.channel,
2336
2922
  target: userContacts.target,
2337
2923
  enabled: userContacts.enabled
2338
- }).from(users).innerJoin(userContacts, eq(users.id, userContacts.userId)).where(and(eq(users.projectId, projectId), inArray(users.externalId, userIds), eq(userContacts.enabled, true)));
2924
+ }).from(users).innerJoin(userContacts, eq(users.id, userContacts.userId)).where(and(eq(users.projectId, projectId), inArray(users.externalId, missingIds), eq(userContacts.enabled, true)));
2925
+ const topicsByContact = /* @__PURE__ */ new Map();
2926
+ if (rows.length > 0) {
2927
+ const topicRows = await this.db.select().from(contactTopicPreferences).where(inArray(contactTopicPreferences.contactId, rows.map((r) => r.id)));
2928
+ for (const t of topicRows) {
2929
+ if (!topicsByContact.has(t.contactId)) topicsByContact.set(t.contactId, {});
2930
+ topicsByContact.get(t.contactId)[t.topic] = t.enabled;
2931
+ }
2932
+ }
2933
+ const fetchedByUser = /* @__PURE__ */ new Map();
2934
+ for (const id of missingIds) fetchedByUser.set(id, []);
2339
2935
  for (const row of rows) {
2340
- const contacts = byUser.get(row.userId) ?? [];
2936
+ const contacts = fetchedByUser.get(row.userId) ?? [];
2937
+ const topics = topicsByContact.get(row.id);
2341
2938
  contacts.push({
2342
2939
  id: row.id,
2343
2940
  userId: row.userId,
2344
2941
  channel: row.channel,
2345
2942
  target: row.target,
2346
- preferences: {},
2943
+ preferences: topics ? { topics } : {},
2347
2944
  active: row.enabled
2348
2945
  });
2349
- byUser.set(row.userId, contacts);
2946
+ fetchedByUser.set(row.userId, contacts);
2947
+ }
2948
+ for (const id of missingIds) {
2949
+ const contacts = fetchedByUser.get(id) ?? [];
2950
+ this.activeContactsCache.set(`${projectId}:${id}`, contacts);
2951
+ if (contacts.length > 0) byUser.set(id, contacts);
2350
2952
  }
2351
2953
  return byUser;
2352
2954
  }
@@ -2376,6 +2978,7 @@ var ContactRepository = class {
2376
2978
  target: [contactTopicPreferences.contactId, contactTopicPreferences.topic],
2377
2979
  set: { enabled: sql`excluded.enabled` }
2378
2980
  });
2981
+ this.activeContactsCache.delete(`${projectId}:${userId}`);
2379
2982
  }
2380
2983
  async upsertMany(projectId, contactsList) {
2381
2984
  if (contactsList.length === 0) return;
@@ -2384,7 +2987,24 @@ var ContactRepository = class {
2384
2987
  externalId: users.externalId
2385
2988
  }).from(users).where(and(inArray(users.externalId, contactsList.map((c) => c.userId)), eq(users.projectId, projectId)));
2386
2989
  const idMap = new Map(internalUserIdRows.map((r) => [r.externalId, r.id]));
2387
- const validContacts = contactsList.filter((c) => idMap.has(c.userId));
2990
+ const byAddress = /* @__PURE__ */ new Map();
2991
+ for (const c of contactsList) {
2992
+ if (!idMap.has(c.userId)) continue;
2993
+ const key = `${c.userId}\u0000${c.channel}\u0000${c.target}`;
2994
+ const seen = byAddress.get(key);
2995
+ byAddress.set(key, seen ? {
2996
+ ...c,
2997
+ preferences: {
2998
+ ...seen.preferences,
2999
+ ...c.preferences,
3000
+ topics: {
3001
+ ...seen.preferences?.topics,
3002
+ ...c.preferences?.topics
3003
+ }
3004
+ }
3005
+ } : c);
3006
+ }
3007
+ const validContacts = [...byAddress.values()];
2388
3008
  if (validContacts.length === 0) return;
2389
3009
  const inserted = await this.db.insert(userContacts).values(validContacts.map((c) => ({
2390
3010
  userId: idMap.get(c.userId),
@@ -2421,6 +3041,7 @@ var ContactRepository = class {
2421
3041
  target: [contactTopicPreferences.contactId, contactTopicPreferences.topic],
2422
3042
  set: { enabled: sql`excluded.enabled` }
2423
3043
  });
3044
+ for (const c of validContacts) this.activeContactsCache.delete(`${projectId}:${c.userId}`);
2424
3045
  }
2425
3046
  /**
2426
3047
  * Mark a contact unusable without destroying it — used when a provider
@@ -2428,11 +3049,13 @@ var ContactRepository = class {
2428
3049
  * device permanently on what is often a transient provider response.
2429
3050
  */
2430
3051
  async deactivate(projectId, userId, channel, target) {
3052
+ this.activeContactsCache.delete(`${projectId}:${userId}`);
2431
3053
  const internalId = (await this.db.select({ id: users.id }).from(users).where(and(eq(users.externalId, userId), eq(users.projectId, projectId))))[0]?.id;
2432
3054
  if (!internalId) return false;
2433
3055
  return (await this.db.update(userContacts).set({ enabled: false }).where(and(eq(userContacts.userId, internalId), eq(userContacts.channel, channel), eq(userContacts.target, target))).returning()).length > 0;
2434
3056
  }
2435
3057
  async delete(projectId, userId, channel, target) {
3058
+ this.activeContactsCache.delete(`${projectId}:${userId}`);
2436
3059
  const internalId = (await this.db.select({ id: users.id }).from(users).where(and(eq(users.externalId, userId), eq(users.projectId, projectId))))[0]?.id;
2437
3060
  if (!internalId) return false;
2438
3061
  return (await this.db.delete(userContacts).where(and(eq(userContacts.userId, internalId), eq(userContacts.channel, channel), eq(userContacts.target, target))).returning()).length > 0;
@@ -2518,6 +3141,7 @@ var ProjectRepository = class {
2518
3141
  await tx.delete(suppressions).where(eq(suppressions.projectId, id));
2519
3142
  await tx.delete(messageLogs).where(eq(messageLogs.projectId, id));
2520
3143
  await tx.delete(workflowInstances).where(eq(workflowInstances.projectId, id));
3144
+ await tx.delete(workflowDefinitions).where(eq(workflowDefinitions.projectId, id));
2521
3145
  return (await tx.delete(projects).where(eq(projects.id, id)).returning()).length > 0;
2522
3146
  });
2523
3147
  }
@@ -2747,43 +3371,33 @@ var TemplateCache = class {
2747
3371
  }
2748
3372
  };
2749
3373
  //#endregion
2750
- //#region src/templates/index.ts
2751
- /** Coerce a template variable to a non-empty string, or undefined. */
2752
- function asText(value) {
2753
- return typeof value === "string" && value.length > 0 ? value : void 0;
2754
- }
2755
- var TemplateRegistry = class {
2756
- renderers = /* @__PURE__ */ new Map();
2757
- /**
2758
- * Register a custom renderer for an event type.
2759
- * Overwrites any previous registration for the same type.
2760
- */
2761
- register(eventType, renderer) {
2762
- this.renderers.set(eventType, renderer);
3374
+ //#region src/transport/index.ts
3375
+ var TransportRegistry = class {
3376
+ transports = /* @__PURE__ */ new Map();
3377
+ register(transport, priority = 0) {
3378
+ const list = this.transports.get(transport.channel) || [];
3379
+ list.push({
3380
+ transport,
3381
+ priority
3382
+ });
3383
+ list.sort((a, b) => b.priority - a.priority);
3384
+ this.transports.set(transport.channel, list);
2763
3385
  }
2764
- /** Render content for the given context, falling back to the i18n table. */
2765
- render(ctx) {
2766
- const renderer = this.renderers.get(ctx.eventType);
2767
- if (renderer) return renderer(ctx);
2768
- return defaultRenderer(ctx);
3386
+ get(channel) {
3387
+ const list = this.transports.get(channel);
3388
+ return list && list.length > 0 ? list[0]?.transport : void 0;
2769
3389
  }
2770
- has(eventType) {
2771
- return this.renderers.has(eventType);
3390
+ getAll(channel) {
3391
+ const list = this.transports.get(channel);
3392
+ return list ? list.map((item) => item.transport) : [];
2772
3393
  }
2773
- registeredTypes() {
2774
- return [...this.renderers.keys()];
3394
+ registeredChannels() {
3395
+ return [...this.transports.keys()];
2775
3396
  }
2776
3397
  };
2777
- function defaultRenderer(ctx) {
2778
- const vars = ctx.templateVariables;
2779
- return { content: {
2780
- subject: asText(vars.subject ?? vars.title),
2781
- body: asText(vars.body ?? vars.message) ?? `Notification: ${ctx.eventType}`
2782
- } };
2783
- }
2784
- const templateRegistry = new TemplateRegistry();
2785
- function renderTemplate(ctx) {
2786
- return templateRegistry.render(ctx);
3398
+ const transportRegistry = new TransportRegistry();
3399
+ function registerTransport(transport, priority = 0) {
3400
+ transportRegistry.register(transport, priority);
2787
3401
  }
2788
3402
  //#endregion
2789
3403
  //#region src/unsubscribe/index.ts
@@ -2878,6 +3492,13 @@ var NonRetryableError = class extends Error {
2878
3492
  this.name = "NonRetryableError";
2879
3493
  }
2880
3494
  };
3495
+ var LockHeldError = class extends Error {
3496
+ lockHeld = true;
3497
+ constructor(message = "Idempotency lock held — leaving message pending") {
3498
+ super(message);
3499
+ this.name = "LockHeldError";
3500
+ }
3501
+ };
2881
3502
  var BaseWorker = class {
2882
3503
  logger;
2883
3504
  consumer;
@@ -2892,6 +3513,7 @@ var BaseWorker = class {
2892
3513
  lastProcessedAt = null;
2893
3514
  lastErrorAt = null;
2894
3515
  recoveryTimer = null;
3516
+ recovering = false;
2895
3517
  lastPendingCount = null;
2896
3518
  active = /* @__PURE__ */ new Set();
2897
3519
  semaphore;
@@ -2971,25 +3593,74 @@ var BaseWorker = class {
2971
3593
  pendingCount: this.lastPendingCount
2972
3594
  };
2973
3595
  }
3596
+ /**
3597
+ * Processes an event produced by an upstream stage in this same process,
3598
+ * skipping the stream hop in between.
3599
+ *
3600
+ * The caller is still holding its own stream entry, and does not ack it until
3601
+ * this returns. A thrown error therefore leaves that entry pending and the
3602
+ * whole chain is replayed from the upstream stage; the idempotency keys each
3603
+ * stage already checks make the replay safe. Non-retryable failures go to the
3604
+ * dead-letter stream here, as they would have from this worker's own stream,
3605
+ * so they do not send the upstream message there instead.
3606
+ */
3607
+ async processInline(event) {
3608
+ try {
3609
+ await this.process({
3610
+ id: event.id,
3611
+ event,
3612
+ deliveryCount: 1,
3613
+ inline: true
3614
+ }, 1);
3615
+ this.processedCount += 1;
3616
+ this.lastProcessedAt = (/* @__PURE__ */ new Date()).toISOString();
3617
+ metrics.messagesProcessed.inc({
3618
+ worker: this.constructor.name,
3619
+ status: "success"
3620
+ });
3621
+ } catch (err) {
3622
+ if (err instanceof LockHeldError || err?.lockHeld) return;
3623
+ this.errorCount += 1;
3624
+ this.lastErrorAt = (/* @__PURE__ */ new Date()).toISOString();
3625
+ metrics.messagesProcessed.inc({
3626
+ worker: this.constructor.name,
3627
+ status: "error"
3628
+ });
3629
+ if (err instanceof NonRetryableError || err?.nonRetryable) {
3630
+ this.logger.warn({
3631
+ err,
3632
+ eventId: event.id
3633
+ }, "non-retryable error on inline message — moving to dead-letter queue");
3634
+ await this.consumer.deadLetter(event, err.message);
3635
+ globalEmitter.emit("notification:failed", event.id, err.message, event.type);
3636
+ return;
3637
+ }
3638
+ throw err;
3639
+ }
3640
+ }
2974
3641
  async processWithTracking(message) {
2975
3642
  const start = Date.now();
2976
3643
  const stream = message.stream;
2977
- const retryKey = `notif:worker:retries:${this.constructor.name}:${stream ?? "default"}:${message.id}`;
3644
+ const retryCount = message.deliveryCount ?? 1;
2978
3645
  try {
2979
- const retryCount = (await this.consumer.redis.multi().incr(retryKey).expire(retryKey, 7200).exec())?.[0]?.[1] ?? 1;
2980
3646
  if (retryCount > this.maxRetriesBeforeDlq) {
2981
3647
  this.logger.warn({
2982
3648
  messageId: message.id,
2983
3649
  retryCount
2984
3650
  }, "max retries exceeded, moving to dead-letter queue");
2985
- await this.consumer.nack(message.id, message.event, stream);
2986
- await this.consumer.redis.del(retryKey);
2987
- globalEmitter.emit("notification:failed", message.id, "Poison pill: max retries exceeded", message.event.type);
3651
+ await this.consumer.nack(message.id, message.event, stream, `max retries exceeded (${retryCount - 1} attempts)`);
3652
+ globalEmitter.emit("notification:failed", message.event.id, "Poison pill: max retries exceeded", message.event.type);
3653
+ return;
3654
+ }
3655
+ const result = await this.process(message, retryCount);
3656
+ if (result && result.ack === false) {
3657
+ this.logger.debug({
3658
+ messageId: message.id,
3659
+ eventType: message.event.type
3660
+ }, "message skipped without ack — leaving pending in stream");
2988
3661
  return;
2989
3662
  }
2990
- await this.process(message, retryCount);
2991
3663
  await this.consumer.ack(message.id, stream);
2992
- await this.consumer.redis.del(retryKey);
2993
3664
  this.processedCount += 1;
2994
3665
  this.lastProcessedAt = (/* @__PURE__ */ new Date()).toISOString();
2995
3666
  metrics.messagesProcessed.inc({
@@ -3002,6 +3673,14 @@ var BaseWorker = class {
3002
3673
  durationMs: Date.now() - start
3003
3674
  }, "message processed");
3004
3675
  } catch (err) {
3676
+ if (err instanceof LockHeldError || err?.lockHeld) {
3677
+ this.logger.debug({
3678
+ err,
3679
+ messageId: message.id,
3680
+ eventType: message.event.type
3681
+ }, "lock held by another worker — leaving message pending in stream");
3682
+ return;
3683
+ }
3005
3684
  this.errorCount += 1;
3006
3685
  this.lastErrorAt = (/* @__PURE__ */ new Date()).toISOString();
3007
3686
  metrics.messagesProcessed.inc({
@@ -3013,9 +3692,8 @@ var BaseWorker = class {
3013
3692
  err,
3014
3693
  messageId: message.id
3015
3694
  }, "non-retryable error encountered, immediately moving to dead-letter queue without retry loop");
3016
- await this.consumer.nack(message.id, message.event, stream);
3017
- await this.consumer.redis.del(retryKey);
3018
- globalEmitter.emit("notification:failed", message.id, err.message, message.event.type);
3695
+ await this.consumer.nack(message.id, message.event, stream, err.message);
3696
+ globalEmitter.emit("notification:failed", message.event.id, err.message, message.event.type);
3019
3697
  return;
3020
3698
  }
3021
3699
  this.logger.error({
@@ -3025,12 +3703,26 @@ var BaseWorker = class {
3025
3703
  }, "failed to process message");
3026
3704
  }
3027
3705
  }
3706
+ /**
3707
+ * `recoveryIntervalMs` is how long an entry must sit idle before it is
3708
+ * claimed, not how often to look. Scanning only once per interval, timed from
3709
+ * this worker's start, meant a worker restarted more often than that (a crash
3710
+ * loop, an eager autoscaler) never scanned at all, and whatever its
3711
+ * predecessors held stayed pending for good. So scan on start and then
3712
+ * several times per interval; the idle threshold alone decides what is taken.
3713
+ */
3028
3714
  startRecoveryLoop() {
3029
- this.recoveryTimer = setInterval(() => {
3030
- if (this.state === "running") this.recover().catch((err) => {
3715
+ const scan = () => {
3716
+ if (this.state !== "running" || this.recovering) return;
3717
+ this.recovering = true;
3718
+ this.recover().catch((err) => {
3031
3719
  this.logger.error({ err }, "recovery loop error");
3720
+ }).finally(() => {
3721
+ this.recovering = false;
3032
3722
  });
3033
- }, this.recoveryIntervalMs);
3723
+ };
3724
+ scan();
3725
+ this.recoveryTimer = setInterval(scan, Math.max(Math.floor(this.recoveryIntervalMs / 4), 10));
3034
3726
  }
3035
3727
  stopRecoveryLoop() {
3036
3728
  if (this.recoveryTimer) {
@@ -3088,7 +3780,7 @@ function resolveStepTarget(args, instanceInput) {
3088
3780
  * `templateId`, `sendAt` becomes `scheduledAt` — so this translation is
3089
3781
  * deliberate rather than a spread, and the return type keeps it honest.
3090
3782
  */
3091
- function buildStepNotifyPayload(args, instanceInput, projectId, idempotencyKey) {
3783
+ function buildStepNotifyPayload(args, instanceInput, projectId, idempotencyKey, workflowInstanceId) {
3092
3784
  return {
3093
3785
  projectId,
3094
3786
  target: resolveStepTarget(args, instanceInput),
@@ -3099,7 +3791,8 @@ function buildStepNotifyPayload(args, instanceInput, projectId, idempotencyKey)
3099
3791
  aiPrompts: args.aiPrompts,
3100
3792
  fallback: args.fallback ?? false,
3101
3793
  scheduledAt: args.sendAt,
3102
- idempotencyKey
3794
+ idempotencyKey,
3795
+ ...workflowInstanceId ? { workflowInstanceId } : {}
3103
3796
  };
3104
3797
  }
3105
3798
  //#endregion
@@ -3140,9 +3833,16 @@ var NotifkitClient = class {
3140
3833
  body: body ? JSON.stringify(body) : void 0
3141
3834
  });
3142
3835
  if (res.status === 204) return;
3143
- const data = await res.json();
3836
+ const text = await res.text();
3837
+ let data;
3838
+ try {
3839
+ data = text ? JSON.parse(text) : void 0;
3840
+ } catch {
3841
+ data = void 0;
3842
+ }
3144
3843
  if (!res.ok) {
3145
- const errorMsg = data.message || data.error || `Request failed with status ${res.status}`;
3844
+ const body = data;
3845
+ const errorMsg = body?.message || body?.error || `Request failed with status ${res.status}`;
3146
3846
  throw new Error(errorMsg);
3147
3847
  }
3148
3848
  return data;
@@ -3415,7 +4115,6 @@ var NotifkitServer = class extends EventEmitter {
3415
4115
  }
3416
4116
  }
3417
4117
  async start() {
3418
- const { setGlobalConfig, readBaseConfig } = await Promise.resolve().then(() => config_exports);
3419
4118
  if (this.options.port) process.env.PORT = String(this.options.port);
3420
4119
  if (this.options.logLevel) process.env.LOG_LEVEL = this.options.logLevel;
3421
4120
  if (this.options.nodeEnv) process.env.NODE_ENV = this.options.nodeEnv;
@@ -3423,8 +4122,11 @@ var NotifkitServer = class extends EventEmitter {
3423
4122
  if (this.options.adminUser?.password) process.env.ADMIN_PASSWORD = this.options.adminUser.password;
3424
4123
  if (this.options.adminUser?.username) process.env.ADMIN_USERNAME = this.options.adminUser.username;
3425
4124
  if (this.options.workerConcurrency) process.env.WORKER_CONCURRENCY = String(this.options.workerConcurrency);
4125
+ if (this.options.fusedPipeline !== void 0) process.env.PIPELINE_FUSED = String(this.options.fusedPipeline);
4126
+ if (this.options.deliveryConcurrency) process.env.DELIVERY_CONCURRENCY = String(this.options.deliveryConcurrency);
3426
4127
  if (this.options.redisOptions?.maxQueueLength) process.env.QUEUE_MAX_LEN = String(this.options.redisOptions.maxQueueLength);
3427
4128
  if (this.options.dbOptions?.maxConnections) process.env.DB_MAX_CONNECTIONS = String(this.options.dbOptions.maxConnections);
4129
+ if (this.options.dbOptions?.schema) process.env.DB_SCHEMA = this.options.dbOptions.schema;
3428
4130
  const isProduction = process.env.NODE_ENV === "production";
3429
4131
  if (!this.options.redisUrl) if (process.env.REDIS_URL) this.options.redisUrl = process.env.REDIS_URL;
3430
4132
  else if (!isProduction) {
@@ -3443,13 +4145,9 @@ var NotifkitServer = class extends EventEmitter {
3443
4145
  if (this.options.redisUrl) process.env.REDIS_URL = this.options.redisUrl;
3444
4146
  if (this.options.databaseUrl) process.env.DATABASE_URL = this.options.databaseUrl;
3445
4147
  setGlobalConfig(readBaseConfig());
3446
- if (this.options.aiModel) {
3447
- const { setAiConfig } = await Promise.resolve().then(() => config_exports);
3448
- setAiConfig({ aiModel: this.options.aiModel });
3449
- }
4148
+ if (this.options.aiModel) setAiConfig({ aiModel: this.options.aiModel });
3450
4149
  if (this.options.autoMigrate !== false) {
3451
4150
  this.logger.info("Running database migrations...");
3452
- const { createDatabase, runMigrations } = await Promise.resolve().then(() => db_exports);
3453
4151
  const { db, sql } = createDatabase({ url: this.options.databaseUrl });
3454
4152
  await runMigrations(db);
3455
4153
  await sql.end();
@@ -3471,35 +4169,35 @@ var NotifkitServer = class extends EventEmitter {
3471
4169
  }
3472
4170
  const startupPromises = [];
3473
4171
  if (services.includes("api")) {
3474
- const { startApiServer } = await import("./main-39DtKrX4.mjs");
4172
+ const { startApiServer } = await import("./main-BxAeC_k6.mjs");
3475
4173
  startupPromises.push(startApiServer());
3476
4174
  }
3477
4175
  if (services.includes("delivery")) {
3478
- const { startDeliveryWorker } = await import("./main-BM8DH3v3.mjs");
4176
+ const { startDeliveryWorker } = await import("./main-B5BwjIMu.mjs");
3479
4177
  startupPromises.push(startDeliveryWorker());
3480
4178
  }
3481
4179
  if (services.includes("engine")) {
3482
- const { startEngineWorker } = await import("./main-BiFiOX9D.mjs");
4180
+ const { startEngineWorker } = await import("./main-BtnGmyaT.mjs");
3483
4181
  startupPromises.push(startEngineWorker());
3484
4182
  }
3485
4183
  if (services.includes("enricher")) {
3486
- const { startEnricherWorker } = await import("./main-B3iQrl3t.mjs");
4184
+ const { startEnricherWorker } = await import("./main-BU27VjDC.mjs");
3487
4185
  startupPromises.push(startEnricherWorker());
3488
4186
  }
3489
4187
  if (services.includes("scheduler")) {
3490
- const { startSchedulerWorker } = await import("./main-Cqo3rSb5.mjs");
4188
+ const { startSchedulerWorker } = await import("./main-Bes11Ad3.mjs");
3491
4189
  startupPromises.push(startSchedulerWorker());
3492
4190
  }
3493
4191
  if (services.includes("ai")) {
3494
- const { startAiWorker } = await import("./main-C9s-2ArH.mjs");
4192
+ const { startAiWorker } = await import("./main-B-AZ-xoN.mjs");
3495
4193
  startupPromises.push(startAiWorker());
3496
4194
  }
3497
4195
  if (services.includes("workflow")) {
3498
- const { startWorkflowWorker } = await import("./main-BgmT1MPe.mjs");
4196
+ const { startWorkflowWorker } = await import("./main-DYGCl7Ja.mjs");
3499
4197
  startupPromises.push(startWorkflowWorker());
3500
4198
  }
3501
4199
  if (services.includes("events")) {
3502
- const { startEventWorker } = await import("./main-Dz2sUh3c.mjs");
4200
+ const { startEventWorker } = await import("./main-DAXO-MiH.mjs");
3503
4201
  startupPromises.push(startEventWorker());
3504
4202
  }
3505
4203
  const handleSignal = async (signal) => {
@@ -3519,6 +4217,21 @@ var NotifkitServer = class extends EventEmitter {
3519
4217
  this.signalHandlersAttached = true;
3520
4218
  }
3521
4219
  await Promise.all(startupPromises);
4220
+ if (readBaseConfig().PIPELINE_FUSED && services.includes("enricher") && services.includes("engine") && services.includes("delivery")) {
4221
+ const [{ getEnricherWorker }, { getEngineWorker }, { getDeliveryWorker }] = await Promise.all([
4222
+ import("./main-BU27VjDC.mjs"),
4223
+ import("./main-BtnGmyaT.mjs"),
4224
+ import("./main-B5BwjIMu.mjs")
4225
+ ]);
4226
+ const enricher = getEnricherWorker();
4227
+ const engine = getEngineWorker();
4228
+ const delivery = getDeliveryWorker();
4229
+ if (enricher && engine && delivery) {
4230
+ engine.setInlineNext((event) => delivery.processInline(event));
4231
+ enricher.setInlineNext((event) => engine.processInline(event));
4232
+ this.logger.info("pipeline fused: enricher → engine → delivery hand off in-process");
4233
+ }
4234
+ }
3522
4235
  }
3523
4236
  async stop() {
3524
4237
  if (this.signalHandlersAttached) {
@@ -3538,38 +4251,17 @@ var NotifkitServer = class extends EventEmitter {
3538
4251
  "workflow",
3539
4252
  "events"
3540
4253
  ] : this.options.services;
3541
- if (services.includes("api")) {
3542
- const { stopApiServer } = await import("./main-39DtKrX4.mjs");
3543
- await stopApiServer();
3544
- }
3545
- if (services.includes("delivery")) {
3546
- const { stopDeliveryWorker } = await import("./main-BM8DH3v3.mjs");
3547
- await stopDeliveryWorker();
3548
- }
3549
- if (services.includes("engine")) {
3550
- const { stopEngineWorker } = await import("./main-BiFiOX9D.mjs");
3551
- await stopEngineWorker();
3552
- }
3553
- if (services.includes("enricher")) {
3554
- const { stopEnricherWorker } = await import("./main-B3iQrl3t.mjs");
3555
- await stopEnricherWorker();
3556
- }
3557
- if (services.includes("scheduler")) {
3558
- const { stopSchedulerWorker } = await import("./main-Cqo3rSb5.mjs");
3559
- await stopSchedulerWorker();
3560
- }
3561
- if (services.includes("ai")) {
3562
- const { stopAiWorker } = await import("./main-C9s-2ArH.mjs");
3563
- await stopAiWorker();
3564
- }
3565
- if (services.includes("workflow")) {
3566
- const { stopWorkflowWorker } = await import("./main-BgmT1MPe.mjs");
3567
- await stopWorkflowWorker();
3568
- }
3569
- if (services.includes("events")) {
3570
- const { stopEventWorker } = await import("./main-Dz2sUh3c.mjs");
3571
- await stopEventWorker();
3572
- }
4254
+ const shutdownOrder = [
4255
+ ["api", async () => (await import("./main-BxAeC_k6.mjs")).stopApiServer()],
4256
+ ["workflow", async () => (await import("./main-DYGCl7Ja.mjs")).stopWorkflowWorker()],
4257
+ ["enricher", async () => (await import("./main-BU27VjDC.mjs")).stopEnricherWorker()],
4258
+ ["ai", async () => (await import("./main-B-AZ-xoN.mjs")).stopAiWorker()],
4259
+ ["engine", async () => (await import("./main-BtnGmyaT.mjs")).stopEngineWorker()],
4260
+ ["scheduler", async () => (await import("./main-Bes11Ad3.mjs")).stopSchedulerWorker()],
4261
+ ["delivery", async () => (await import("./main-B5BwjIMu.mjs")).stopDeliveryWorker()],
4262
+ ["events", async () => (await import("./main-DAXO-MiH.mjs")).stopEventWorker()]
4263
+ ];
4264
+ for (const [name, stop] of shutdownOrder) if (services.includes(name)) await stop();
3573
4265
  if (this.pgContainer) {
3574
4266
  this.logger.info("Stopping PostgreSQL container...");
3575
4267
  await this.pgContainer.stop();
@@ -3581,6 +4273,4 @@ var NotifkitServer = class extends EventEmitter {
3581
4273
  }
3582
4274
  };
3583
4275
  //#endregion
3584
- export { metrics as $, ENRICHED_STREAMS as $t, RedisClient as A, NotificationCreatedPayloadSchema as At, AsyncSemaphore as B, NotifyRequestSchema as Bt, PreferenceRepository as C, NotificationDeliveredPayloadSchema as Ct, UserRepository as D, NotificationScheduledPayloadSchema as Dt, TemplateRepository as E, RenderedContentSchema as Et, generateId as F, BatchAddContactsSchema as Ft, getPriorityBucket as G, TriggerWorkflowSchema as Gt, LUA_RENEW_LOCK as H, QuietHoursSchema as Ht, sleep as I, ContactChannelSchema as It, globalEmitter as J, UserContactInputSchema as Jt, normaliseTarget as K, UpdateProjectSchema as Kt, DataLoader as L, CreateWorkflowSchema as Lt, UserThrottle as M, NotificationTargetSchema as Mt, AppError as N, AddContactSchema as Nt, WorkflowRepository as O, NotificationEnrichedPayloadSchema as Ot, ValidationError as P, AddUserSchema as Pt, getMetricsRegistry as Q, CONSUMER_GROUPS as Qt, CircuitBreaker as R, IngestEventSchema as Rt, ContactRepository as S, NotificationFailedPayloadSchema as St, SegmentRepository as T, NotificationDispatchedPayloadSchema as Tt, LUA_SCHEDULER_CLAIM as U, SyncTemplatesSchema as Ut, LUA_RELEASE_LOCK as V, PreferencesSchema as Vt, LUA_SCHEDULER_POLL as W, TemplateSchema as Wt, StreamConsumer as X, WorkflowStepSchema as Xt, PendingMessageScanner as Y, WorkflowNotifyPayloadSchema as Yt, StreamProducer as Z, buildStreamEvent as Zt, escapeHeader as _, parseConfig as _n, workflowSteps as _t, SuspendExecutionError as a, StreamEventMetadataSchema as an, createDatabase as at, renderWithTemplate as b, setGlobalConfig as bn, NotificationCanceledPayloadSchema as bt, BaseWorker as c, registry as cn, messageLogs as ct, buildUnsubscribeHeaders as d, NotificationPrioritySchema as dn, scheduledPayloads as dt, INBOUND_STREAMS as en, childLogger as et, signUnsubscribeToken as f, NotificationStatusSchema as fn, suppressions as ft, TemplateCache as g, loadEnv as gn, workflowInstances as gt, templateRegistry as h, getAiConfig as hn, workflowDefinitions as ht, workflowRegistry as i, EventEnvelopeSchema as in, IdempotencyGuard as it, ProjectSettingsCache as j, NotificationRequestedPayloadSchema as jt, Redis as k, RecipientProfileSchema as kt, NonRetryableError as l, EventMetadataSchema as ln, projectApiKeys as lt, renderTemplate as m, baseConfigSchema as mn, users as mt, NotifkitClient as n, PUBSUB_CHANNELS as nn, withContext as nt, buildStepNotifyPayload as o, StreamEventSchema as on, runMigrations as ot, verifyUnsubscribeToken as p, AI_DEFAULTS as pn, userTopicPreferences as pt, LRUCache as q, UpdateUserSchema as qt, workflow as r, STREAMS as rn, withRequestId as rt, resolveStepTarget as s, EventRegistry as sn, deliveryOutbox as st, NotifkitServer as t, OUTBOUND_STREAMS as tn, createLogger as tt, startHealthReporter as u, NotificationChannelSchema as un, projects as ut, escapeHtml as v, readBaseConfig as vn, workflowWaiters as vt, ProjectRepository as w, DeliveryOptionsSchema as wt, AdminUserRepository as x, NotificationSkippedPayloadSchema as xt, interpolate as y, setAiConfig as yn, NotificationAiPendingPayloadSchema as yt, BatchProcessor as z, InlineUserSchema as zt };
3585
-
3586
- //# sourceMappingURL=src-QiTu5e_a.mjs.map
4276
+ export { migrateDatabase as $, STREAMS as $t, LUA_ACQUIRE_LEASE as A, AddUserSchema as At, ApiRateLimiter as B, TemplateSchema as Bt, ContactRepository as C, BatchProcessor as Cn, NotificationScheduledPayloadSchema as Ct, TemplateRepository as D, LRUCache as Dn, NotificationRequestedPayloadSchema as Dt, SegmentRepository as E, normaliseTarget as En, NotificationCreatedPayloadSchema as Et, LUA_SCHEDULER_POLL as F, InlineUserSchema as Ft, StreamConsumer as G, WorkflowNotifyPayloadSchema as Gt, UserThrottle as H, UpdateProjectSchema as Ht, LUA_USER_THROTTLE as I, NotifyRequestSchema as It, metrics as J, CONSUMER_GROUPS as Jt, StreamProducer as K, WorkflowStepSchema as Kt, Redis as L, PreferencesSchema as Lt, LUA_MARK_PROCESSED as M, ContactChannelSchema as Mt, LUA_RELEASE_LOCK as N, CreateWorkflowSchema as Nt, UserRepository as O, globalEmitter as On, NotificationTargetSchema as Ot, LUA_RENEW_LOCK as P, IngestEventSchema as Pt, createDatabase as Q, PUBSUB_CHANNELS as Qt, RedisClient as R, QuietHoursSchema as Rt, AdminUserRepository as S, CircuitBreaker as Sn, RenderedContentSchema as St, ProjectRepository as T, getPriorityBucket as Tn, RecipientProfileSchema as Tt, calculateTokenBatchSize as U, UpdateUserSchema as Ut, ProjectSettingsCache as V, TriggerWorkflowSchema as Vt, PendingMessageScanner as W, UserContactInputSchema as Wt, IdempotencyGuard as X, INBOUND_STREAMS as Xt, createLogger as Y, ENRICHED_STREAMS as Yt, assertTablesInSchema as Z, OUTBOUND_STREAMS as Zt, TemplateCache as _, AppError as _n, NotificationSkippedPayloadSchema as _t, SuspendExecutionError as a, EventMetadataSchema as an, projectApiKeys as at, interpolate as b, sleep as bn, DeliveryOptionsSchema as bt, BaseWorker as c, NotificationStatusSchema as cn, suppressions as ct, startHealthReporter as d, getAiConfig as dn, workflowDefinitions as dt, EventEnvelopeSchema as en, notifkitTableNames as et, buildUnsubscribeHeaders as f, loadEnv as fn, workflowInstances as ft, transportRegistry as g, setGlobalConfig as gn, NotificationCanceledPayloadSchema as gt, registerTransport as h, setAiConfig as hn, NotificationAiPendingPayloadSchema as ht, workflowRegistry as i, registry as in, messageLogs as it, LUA_LEASE_API_RATE_LIMIT as j, BatchAddContactsSchema as jt, WorkflowRepository as k, AddContactSchema as kt, LockHeldError as l, AI_DEFAULTS as ln, userTopicPreferences as lt, verifyUnsubscribeToken as m, readBaseConfig as mn, workflowWaiters as mt, NotifkitClient as n, StreamEventSchema as nn, runMigrations as nt, buildStepNotifyPayload as o, NotificationChannelSchema as on, projects as ot, signUnsubscribeToken as p, parseConfig as pn, workflowSteps as pt, getMetricsRegistry as q, buildStreamEvent as qt, workflow as r, EventRegistry as rn, deliveryOutbox as rt, resolveStepTarget as s, NotificationPrioritySchema as sn, scheduledPayloads as st, NotifkitServer as t, StreamEventMetadataSchema as tn, qualifyMigrationSql as tt, NonRetryableError as u, baseConfigSchema as un, users as ut, escapeHeader as v, ValidationError as vn, NotificationFailedPayloadSchema as vt, PreferenceRepository as w, AsyncSemaphore as wn, NotificationEnrichedPayloadSchema as wt, renderWithTemplate as x, DataLoader as xn, NotificationDispatchedPayloadSchema as xt, escapeHtml as y, generateId as yn, NotificationDeliveredPayloadSchema as yt, registerCustomCommands as z, SyncTemplatesSchema as zt };