@hogsend/engine 0.40.0 → 0.42.0

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 (69) hide show
  1. package/package.json +11 -8
  2. package/src/buckets/check-membership.ts +6 -1
  3. package/src/campaigns/cohort-sql.ts +198 -0
  4. package/src/campaigns/define-campaign.ts +207 -28
  5. package/src/campaigns/reconcile.ts +105 -11
  6. package/src/campaigns/steps.ts +48 -0
  7. package/src/connectors/define-action.ts +70 -0
  8. package/src/container.ts +124 -42
  9. package/src/destinations/presets/posthog.ts +7 -1
  10. package/src/index.ts +96 -2
  11. package/src/journeys/constants.ts +10 -0
  12. package/src/journeys/define-journey.ts +14 -405
  13. package/src/journeys/execute-journey-run.ts +641 -0
  14. package/src/journeys/graph/build-graph.ts +212 -56
  15. package/src/journeys/journey-boundary.ts +54 -0
  16. package/src/journeys/journey-context.ts +406 -59
  17. package/src/journeys/record-once.ts +110 -0
  18. package/src/lib/blueprint-lock.ts +21 -0
  19. package/src/lib/blueprints.ts +803 -0
  20. package/src/lib/campaign-send-key.ts +58 -0
  21. package/src/lib/connector-actions.ts +203 -19
  22. package/src/lib/contacts.ts +54 -0
  23. package/src/lib/email-service-types.ts +4 -1
  24. package/src/lib/email.ts +13 -2
  25. package/src/lib/event-names.ts +143 -0
  26. package/src/lib/feed.ts +77 -57
  27. package/src/lib/frequency-cap.ts +45 -15
  28. package/src/lib/hatchet-duration.ts +22 -0
  29. package/src/lib/html.ts +1 -0
  30. package/src/lib/ingestion.ts +145 -4
  31. package/src/lib/journey-suppress.ts +87 -0
  32. package/src/lib/links.ts +204 -18
  33. package/src/lib/outbound.ts +20 -0
  34. package/src/lib/preferences.ts +80 -1
  35. package/src/lib/tracked.ts +44 -29
  36. package/src/lib/tracking-event-names.ts +8 -0
  37. package/src/lib/tracking.ts +1 -5
  38. package/src/lib/webhook-signing.ts +6 -1
  39. package/src/lists/channels.ts +75 -0
  40. package/src/lists/define-list.ts +34 -0
  41. package/src/lists/registry.ts +29 -2
  42. package/src/mcp/authoring-guide.ts +160 -0
  43. package/src/mcp/blueprint-tools.ts +390 -0
  44. package/src/middleware/require-admin.ts +13 -0
  45. package/src/routes/admin/api-keys.ts +72 -0
  46. package/src/routes/admin/blueprints.ts +795 -0
  47. package/src/routes/admin/campaigns.ts +179 -3
  48. package/src/routes/admin/emails.ts +13 -0
  49. package/src/routes/admin/events.ts +55 -0
  50. package/src/routes/admin/index.ts +4 -0
  51. package/src/routes/admin/journeys.ts +4 -2
  52. package/src/routes/admin/links.ts +303 -36
  53. package/src/routes/admin/lists.ts +51 -0
  54. package/src/routes/campaigns/index.ts +44 -6
  55. package/src/routes/email/preferences.ts +50 -13
  56. package/src/routes/feed/recipient.ts +2 -43
  57. package/src/routes/index.ts +6 -0
  58. package/src/routes/lists/index.ts +138 -18
  59. package/src/routes/tracking/arrive.ts +260 -0
  60. package/src/routes/tracking/click-pipeline.ts +329 -0
  61. package/src/routes/tracking/click.ts +5 -270
  62. package/src/routes/tracking/index.ts +2 -0
  63. package/src/routes/tracking/vanity.ts +64 -0
  64. package/src/worker.ts +5 -0
  65. package/src/workflows/bucket-backfill.ts +4 -2
  66. package/src/workflows/bucket-reconcile.ts +11 -4
  67. package/src/workflows/check-alerts.ts +130 -1
  68. package/src/workflows/journey-blueprint-interpreter.ts +656 -0
  69. package/src/workflows/send-campaign.ts +574 -180
@@ -0,0 +1,641 @@
1
+ import type { Conditions } from "@hatchet-dev/typescript-sdk/v1/index.js";
2
+ import type { JsonValue } from "@hatchet-dev/typescript-sdk/v1/types.js";
3
+ import {
4
+ type DurationObject,
5
+ durationToMs,
6
+ evaluatePropertyConditions,
7
+ } from "@hogsend/core";
8
+ import type {
9
+ JourneyMeta,
10
+ JourneyRunFn,
11
+ JourneyUser,
12
+ } from "@hogsend/core/types";
13
+ import {
14
+ contacts,
15
+ type Database,
16
+ journeyConfigs,
17
+ journeyStates,
18
+ } from "@hogsend/db";
19
+ import { and, eq, inArray, notInArray, sql } from "drizzle-orm";
20
+ import { getAnalytics } from "../lib/analytics-singleton.js";
21
+ import { blueprintGraphLock } from "../lib/blueprint-lock.js";
22
+ import { getDb } from "../lib/db.js";
23
+ import {
24
+ checkEmailPreferences,
25
+ checkEntryLimit,
26
+ } from "../lib/enrollment-guards.js";
27
+ import { hatchet } from "../lib/hatchet.js";
28
+ import { createLogger } from "../lib/logger.js";
29
+ import { emitOutbound } from "../lib/outbound.js";
30
+ import { resolveTimezoneWithSource } from "../lib/timezone.js";
31
+ import { getClientScheduleDefaults } from "./client-defaults-singleton.js";
32
+ import { JourneyExitedError } from "./errors.js";
33
+ import {
34
+ createMemoize,
35
+ type JourneyBoundary,
36
+ runWithJourneyBoundary,
37
+ supportsEviction,
38
+ } from "./journey-boundary.js";
39
+ import { createJourneyContext, TERMINAL_STATUSES } from "./journey-context.js";
40
+ import { logTransition } from "./journey-log.js";
41
+ import { getJourneyRegistrySingleton } from "./registry-singleton.js";
42
+
43
+ const logger = createLogger(process.env.LOG_LEVEL);
44
+
45
+ /**
46
+ * Log whether Hatchet's durable `memo` (Layer-1 fast path) is actually durable
47
+ * on this engine — ONCE per worker process. `supportsEviction === false`
48
+ * (hatchet-lite < v0.80.0) means `memo` silently no-ops, so Layer 2 (the
49
+ * Postgres `email_sends`/`user_events` unique-index dedup) is the sole live
50
+ * guarantee. Surfaced at boot so the team knows which layer is carrying the load.
51
+ */
52
+ let evictionSupportLogged = false;
53
+ function logEvictionSupportOnce(hatchetCtx: unknown): void {
54
+ if (evictionSupportLogged) return;
55
+ evictionSupportLogged = true;
56
+ const live = supportsEviction(hatchetCtx);
57
+ logger.info(
58
+ live
59
+ ? "journey replay-safety: Layer-1 memo fast path is LIVE (engine supports eviction)"
60
+ : "journey replay-safety: Layer-1 memo is INERT (engine < v0.80.0); " +
61
+ "exactly-once relies on the Layer-2 Postgres unique-index dedup",
62
+ );
63
+ }
64
+
65
+ /** The event payload a journey durable task receives from `hatchet.events.push`. */
66
+ export interface EventPayloadInput {
67
+ userId: JsonValue;
68
+ userEmail: JsonValue;
69
+ properties: JsonValue;
70
+ [key: string]: JsonValue;
71
+ }
72
+
73
+ /**
74
+ * The structural slice of Hatchet's `DurableContext` the journey run lifecycle
75
+ * needs. The real SDK context satisfies it; tests pass a stub. Mirrors the
76
+ * shape `createJourneyContext` documents (sleepFor/waitFor normalization,
77
+ * memoized `now`), plus the run-id anchor and the abort signal the
78
+ * shutdown-release path reads.
79
+ */
80
+ export interface JourneyDurableCtx {
81
+ /** Replay-stable id of THIS logical durable run (see runAnchor below). */
82
+ workflowRunId: () => string;
83
+ sleepFor: (
84
+ duration: DurationObject | number | `${number}s`,
85
+ ) => Promise<unknown>;
86
+ waitFor: (
87
+ conditions: Conditions | Conditions[],
88
+ ) => Promise<Record<string, unknown>>;
89
+ /** Replay-memoized clock (SDK >= 1.22.3); optional so stubs fall back live. */
90
+ now?: () => Promise<Date>;
91
+ /** Aborted when the worker releases the run (graceful shutdown). */
92
+ abortController?: { signal?: { aborted?: boolean } };
93
+ }
94
+
95
+ /** The `journey_states` row an enrollment insert returns. */
96
+ export type JourneyStateRow = typeof journeyStates.$inferSelect;
97
+
98
+ /**
99
+ * Insert the enrollment row, tolerating the partial-unique-index race.
100
+ *
101
+ * The active-state read guard in the run lifecycle (a `findFirst` over the live
102
+ * statuses) is NOT atomic with this insert: two near-simultaneous FIRST events
103
+ * for the same (user, journey) — a burst, which is the digest primitive's whole
104
+ * target workload — can BOTH clear that guard and race here. The loser would
105
+ * otherwise hit the `uq_user_journey_active` partial unique index with a raw
106
+ * 23505 that escapes the task fn (durable tasks run with `retries: 0`) and
107
+ * surfaces as a FAILED Hatchet run. `onConflictDoNothing` against that index
108
+ * absorbs the loser, returning `undefined` (0 rows) — the SAME outcome the read
109
+ * guard produces, so the caller maps it to the `already_active` skip.
110
+ *
111
+ * DRIZZLE GOTCHA (from prior prod debugging in this repo — mirrors
112
+ * campaigns/reconcile.ts): for a PARTIAL unique index the arbiter predicate goes
113
+ * in `where`, NOT `targetWhere`. A `targetWhere` is silently ignored and
114
+ * Postgres then throws 42P10 ("no unique or exclusion constraint matching the ON
115
+ * CONFLICT specification") at runtime. The `where` reproduces the index
116
+ * predicate (`status IN ('active','waiting')`) EXACTLY — see
117
+ * journey-states.ts:uq_user_journey_active.
118
+ */
119
+ export async function insertEnrollment(opts: {
120
+ db: Database;
121
+ userId: string;
122
+ userEmail: string;
123
+ journeyId: string;
124
+ context: Record<string, unknown>;
125
+ hatchetRunId?: string;
126
+ /**
127
+ * Blueprint runs serialize this insert against a concurrent GRAPH EDIT via a
128
+ * transaction-scoped advisory lock — `updateBlueprint` takes the SAME lock
129
+ * ({@link blueprintGraphLock}) around its in-flight count + guarded update,
130
+ * so an enrollment cannot become active/waiting in the window between that
131
+ * count and update. Code journeys have no mutable graph, so they insert
132
+ * lock-free (the default).
133
+ */
134
+ serializeWithGraphLock?: boolean;
135
+ }): Promise<JourneyStateRow | undefined> {
136
+ const values = {
137
+ userId: opts.userId,
138
+ userEmail: opts.userEmail,
139
+ journeyId: opts.journeyId,
140
+ currentNodeId: "start",
141
+ status: "active" as const,
142
+ context: opts.context,
143
+ hatchetRunId: opts.hatchetRunId,
144
+ };
145
+ const onConflict = {
146
+ target: [journeyStates.userId, journeyStates.journeyId],
147
+ where: sql`status IN ('active', 'waiting')`,
148
+ };
149
+
150
+ if (!opts.serializeWithGraphLock) {
151
+ const [row] = await opts.db
152
+ .insert(journeyStates)
153
+ .values(values)
154
+ .onConflictDoNothing(onConflict)
155
+ .returning();
156
+ return row;
157
+ }
158
+
159
+ return opts.db.transaction(async (tx) => {
160
+ await tx.execute(blueprintGraphLock(opts.journeyId));
161
+ const [row] = await tx
162
+ .insert(journeyStates)
163
+ .values(values)
164
+ .onConflictDoNothing(onConflict)
165
+ .returning();
166
+ return row;
167
+ });
168
+ }
169
+
170
+ export interface ExecuteJourneyRunOptions {
171
+ /**
172
+ * The journey's resolved meta. For a code journey this is `defineJourney`'s
173
+ * normalized `JourneyMeta`; for a blueprint it is built from the
174
+ * `journey_blueprints` row (same shape — that is the whole point: the
175
+ * enrollment guards below receive it verbatim, spec §6).
176
+ */
177
+ meta: JourneyMeta;
178
+ /** The run body — a code journey's `run` or the blueprint tree-walk. */
179
+ run: JourneyRunFn;
180
+ /** The Hatchet event payload the durable task received. */
181
+ input: EventPayloadInput;
182
+ /** The Hatchet `DurableContext` (or a structural stub in tests). */
183
+ hatchetCtx: JourneyDurableCtx;
184
+ /**
185
+ * Extra keys merged into the enrollment row's `context` jsonb on FIRST entry
186
+ * (a replay recovers the existing row and never re-writes it). The blueprint
187
+ * interpreter pins `__blueprintVersion` here (spec §12).
188
+ */
189
+ extraContext?: Record<string, unknown>;
190
+ /**
191
+ * Serialize the enrollment insert against a concurrent blueprint graph edit
192
+ * with a transaction-scoped advisory lock (spec §12). Set by the blueprint
193
+ * interpreter — the blueprint id IS the journeyId, so the lock key matches
194
+ * `updateBlueprint`'s. Code journeys leave it unset (no mutable graph).
195
+ */
196
+ serializeEnrollment?: boolean;
197
+ /**
198
+ * Serialize a run failure into `journeyStates.errorMessage`. Defaults to the
199
+ * error's message; the blueprint interpreter writes the structured
200
+ * `{ blueprintId, nodeId, message }` JSON instead (spec §6.1).
201
+ */
202
+ serializeError?: (err: unknown) => string;
203
+ }
204
+
205
+ export type ExecuteJourneyRunResult =
206
+ | { status: "skipped"; reason: string }
207
+ | { stateId: string; status: "completed" | "exited" };
208
+
209
+ /**
210
+ * The full journey durable-run lifecycle, shared VERBATIM by `defineJourney`
211
+ * tasks and the blueprint interpreter: replay recovery by run id → enrollment
212
+ * guards (entry only) → enrollment insert → timezone resolve → journey context
213
+ * + replay-safety boundary → `run()` → terminal transitions (completed /
214
+ * exited / released / failed). Extracted from `defineJourney` so the blueprint
215
+ * interpreter executes through the IDENTICAL machinery instead of a parallel
216
+ * reimplementation (spec §6) — behavior for code journeys is unchanged.
217
+ */
218
+ export async function executeJourneyRun(
219
+ options: ExecuteJourneyRunOptions,
220
+ ): Promise<ExecuteJourneyRunResult> {
221
+ const { meta, input, hatchetCtx } = options;
222
+ const db = getDb();
223
+ const userId = input.userId as string;
224
+ const userEmail = input.userEmail as string;
225
+ const properties = (input.properties ?? {}) as Record<
226
+ string,
227
+ string | number | boolean | null
228
+ >;
229
+
230
+ // The replay-stable anchor for this enrollment: the Hatchet run id is
231
+ // preserved across replays of the SAME logical durable run (crash / OOM /
232
+ // eviction / redeploy mid-run), whereas a freshly-minted journeyStates.id
233
+ // is not. Both dedup-key derivation AND the enrollment-recovery below key
234
+ // on it so a replay re-derives the SAME stateId and the SAME keys.
235
+ const workflowRunId = hatchetCtx.workflowRunId();
236
+
237
+ // REPLAY RECOVERY — must run BEFORE the enrollment guards below. An
238
+ // eviction-capable Hatchet engine (hatchet-lite >= v0.80.0) replays the fn
239
+ // from the top on every durable-wait resume, so every statement above the
240
+ // first durable primitive re-runs on each resume. The enrollment guards
241
+ // (entryLimit / preferences / trigger conditions / enabled / active-state)
242
+ // are ENTRY gates: re-running them on a resume is wrong. In particular a
243
+ // `once` journey's own entry-limit guard would find the row it created on
244
+ // first entry and skip EVERY resume, stranding the journey in `waiting` and
245
+ // silently dropping all sends after the first. Recovering by the run-stable
246
+ // id first lets a resume reuse its enrollment and bypass the entry gates (a
247
+ // resume is not an entry). It also keeps the original purpose: a journey
248
+ // whose prior enrollment reached a TERMINAL status (unlimited /
249
+ // once_per_period) recovers the SAME stateId so derived keys still collide.
250
+ const recovered = workflowRunId
251
+ ? await db.query.journeyStates.findFirst({
252
+ where: and(
253
+ eq(journeyStates.hatchetRunId, workflowRunId),
254
+ eq(journeyStates.journeyId, meta.id),
255
+ ),
256
+ })
257
+ : undefined;
258
+
259
+ let state = recovered;
260
+
261
+ // FIRST ENTRY ONLY — the enrollment guards gate ENTRY, not resume. A
262
+ // recovered enrollment already passed them on first entry, so it skips
263
+ // straight to resuming the run. (Sends inside `run` still re-check
264
+ // subscription via `ctx.guard.isSubscribed()` after every wait, so
265
+ // bypassing the entry-time preference gate never emails an unsubscriber.)
266
+ if (!state) {
267
+ if (!meta.enabled) {
268
+ return { status: "skipped", reason: "journey_disabled" };
269
+ }
270
+
271
+ const configOverride = await db.query.journeyConfigs.findFirst({
272
+ where: eq(journeyConfigs.journeyId, meta.id),
273
+ });
274
+ if (configOverride && !configOverride.enabled) {
275
+ return { status: "skipped", reason: "journey_disabled_by_admin" };
276
+ }
277
+
278
+ if (meta.trigger.where?.length) {
279
+ if (
280
+ !evaluatePropertyConditions({
281
+ conditions: meta.trigger.where,
282
+ properties,
283
+ })
284
+ ) {
285
+ return { status: "skipped", reason: "trigger_conditions_not_met" };
286
+ }
287
+ }
288
+
289
+ const entryAllowed = await checkEntryLimit({
290
+ db,
291
+ journey: meta,
292
+ userId,
293
+ });
294
+ if (!entryAllowed.allowed) {
295
+ return {
296
+ status: "skipped",
297
+ reason: entryAllowed.reason ?? "entry_limit",
298
+ };
299
+ }
300
+
301
+ const prefs = await checkEmailPreferences({ db, userId });
302
+ if (prefs.unsubscribed) {
303
+ return { status: "skipped", reason: "user_unsubscribed" };
304
+ }
305
+
306
+ const activeState = await db.query.journeyStates.findFirst({
307
+ where: and(
308
+ eq(journeyStates.userId, userId),
309
+ eq(journeyStates.journeyId, meta.id),
310
+ inArray(journeyStates.status, ["active", "waiting"]),
311
+ ),
312
+ });
313
+ if (activeState) {
314
+ return { status: "skipped", reason: "already_active" };
315
+ }
316
+
317
+ // A successful insert always returns exactly one row, so undefined here
318
+ // can ONLY mean a burst-race conflict (see insertEnrollment's doc for
319
+ // the race + arbiter mechanics) — the same outcome as the read guard,
320
+ // mapped to the identical skip.
321
+ state = await insertEnrollment({
322
+ db,
323
+ userId,
324
+ userEmail,
325
+ journeyId: meta.id,
326
+ context: options.extraContext
327
+ ? { ...properties, ...options.extraContext }
328
+ : properties,
329
+ hatchetRunId: workflowRunId,
330
+ serializeWithGraphLock: options.serializeEnrollment,
331
+ });
332
+ if (!state) {
333
+ return { status: "skipped", reason: "already_active" };
334
+ }
335
+ }
336
+
337
+ const stateId = state.id;
338
+
339
+ // Fire-and-forget enrollment transition (FRESH entry only — a replay
340
+ // recovers `state` by run id above and must NOT re-log an entry). Writes
341
+ // `journey_logs` best-effort; never throws into / alters execution.
342
+ if (!recovered) {
343
+ logTransition({
344
+ db,
345
+ journeyStateId: stateId,
346
+ from: null,
347
+ to: "start",
348
+ action: "entered",
349
+ });
350
+ }
351
+
352
+ // The replay-stable key anchor: prefer the run id (constant across replays
353
+ // of this run), fall back to stateId when the engine has no run id.
354
+ const runAnchor = workflowRunId || stateId;
355
+
356
+ const user: JourneyUser = {
357
+ id: userId,
358
+ email: userEmail,
359
+ properties,
360
+ stateId,
361
+ journeyId: meta.id,
362
+ journeyName: meta.name,
363
+ };
364
+
365
+ // The injected analytics instance (set by createHogsendClient). Same
366
+ // object as container.analytics; undefined when POSTHOG_API_KEY is unset.
367
+ const posthog = getAnalytics();
368
+ const scheduleDefaults = getClientScheduleDefaults();
369
+
370
+ // Resolve the user's timezone via the precedence chain (explicit is N/A
371
+ // at enrollment; PostHog person props → contacts row → client default →
372
+ // UTC). Best-effort: failures fall through to the client default tz.
373
+ // Independent I/O — fetch the contact row and PostHog person props
374
+ // concurrently. PostHog failures fall through to undefined.
375
+ const [contact, posthogProperties] = await Promise.all([
376
+ // Both legs are best-effort tz inputs: a failure here must fall through
377
+ // to the client-default tz, never reject the `Promise.all` and throw out
378
+ // of the fn BEFORE the try/catch below (which would strand the row —
379
+ // already inserted "active" — outside the failure handling). The PostHog
380
+ // leg already `.catch`es; mirror it on the contact read.
381
+ db.query.contacts
382
+ .findFirst({ where: eq(contacts.externalId, userId) })
383
+ .catch(() => undefined),
384
+ posthog?.getPersonProperties(userId).catch(() => undefined),
385
+ ]);
386
+
387
+ const tz = resolveTimezoneWithSource({
388
+ posthogProperties,
389
+ contactTimezone: contact?.timezone ?? null,
390
+ contactProperties: contact?.properties ?? null,
391
+ defaultTimezone: scheduleDefaults.timezone,
392
+ logger,
393
+ });
394
+
395
+ // Opportunistic cache write: when the tz came from a PostHog source and
396
+ // the contacts.timezone column is empty, persist it (fire-and-forget;
397
+ // PostHog/JSONB remain authoritative so nothing blocks on the column).
398
+ if (
399
+ (tz.source === "posthog_timezone" || tz.source === "posthog_geoip") &&
400
+ contact &&
401
+ !contact.timezone
402
+ ) {
403
+ db.update(contacts)
404
+ .set({ timezone: tz.timezone, updatedAt: new Date() })
405
+ .where(eq(contacts.id, contact.id))
406
+ .catch(() => {
407
+ // best-effort cache write; never block the journey
408
+ });
409
+ }
410
+
411
+ const ctx = createJourneyContext({
412
+ db,
413
+ hatchet,
414
+ // The real DurableContext structurally satisfies the (widened) config —
415
+ // it exposes `sleepFor`/`waitFor` AND a replay-memoized `now()` (SDK
416
+ // 1.22.3). Forwarding it whole lets ctx clock reads (ctx.when,
417
+ // sleepUntil delta, lookback, ctx.now) read the memoized clock on an
418
+ // eviction-capable engine, falling back to the live clock pre-eviction.
419
+ hatchetCtx,
420
+ registry: getJourneyRegistrySingleton(),
421
+ logger,
422
+ stateId,
423
+ userId,
424
+ userEmail,
425
+ journeyContext: { ...properties },
426
+ resolvedTimezone: tz.timezone,
427
+ defaultSendWindow: scheduleDefaults.sendWindow,
428
+ // Digest defaults: the trigger event + its already-normalized `where`
429
+ // (so a digest of the journey's own trigger honors the trigger contract
430
+ // without restating it), plus the enrollment shape for the digest's
431
+ // definition-interplay warnings.
432
+ triggerEvent: meta.trigger.event,
433
+ ...(meta.trigger.where ? { triggerWhere: meta.trigger.where } : {}),
434
+ journeyId: meta.id,
435
+ entryLimit: meta.entryLimit,
436
+ ...(meta.entryPeriod ? { entryPeriod: meta.entryPeriod } : {}),
437
+ });
438
+
439
+ // The journey boundary makes journey side effects (sendEmail, ctx.trigger)
440
+ // EXACTLY-ONCE across a durable replay WITHOUT any change to journey
441
+ // authoring. It is established once around `run()`: a replay-from-top
442
+ // re-enters this scope from the top. Its `runAnchor` is the replay-stable
443
+ // Hatchet run id (recovered above so the SAME stateId is reused on replay),
444
+ // so the derived keys collide across a replay of the same run even if the
445
+ // enrollment row had to be recovered rather than freshly inserted.
446
+ // `memoize` is the Layer-1 fast path (durable only when the engine supports
447
+ // eviction); the auto-derived `email_sends`/`user_events` idempotency keys
448
+ // (anchored on `runAnchor`) are the version-independent Layer-2 guarantee.
449
+ logEvictionSupportOnce(hatchetCtx);
450
+ const boundary: JourneyBoundary = {
451
+ stateId,
452
+ runAnchor,
453
+ currentLabel: undefined,
454
+ seenKeys: new Set<string>(),
455
+ seenRecordLabels: new Set<string>(),
456
+ memoize: createMemoize(hatchetCtx),
457
+ journeyId: meta.id,
458
+ // `meta.suppress` is a required DurationObject, but a `{}` / zero
459
+ // duration must yield 0 (disabled); `durationToMs` maps both to 0.
460
+ // Guard against a runtime-absent value so an undefined never reaches
461
+ // durationToMs (which would throw dereferencing `.hours`).
462
+ suppressMs: meta.suppress ? durationToMs(meta.suppress) : 0,
463
+ // The journey-level email-preference category (validated fail-closed at
464
+ // boot). The standalone `sendEmail` reads it off the boundary and stamps
465
+ // it on the send, overriding the template's own category. Undefined for
466
+ // journeys without a `meta.category` (incl. bucket reactions) ⇒ the send
467
+ // keeps the built-in `journey` default.
468
+ category: meta.category,
469
+ };
470
+
471
+ // Seed the context's memoized-clock snapshot ONCE before run() so a
472
+ // `ctx.when` chain used BEFORE the first durable step reads a replay-stable
473
+ // instant (on an eviction engine) instead of the construction-time
474
+ // `new Date()` seed. Best-effort: on a pre-eviction engine this reads the
475
+ // live clock, same as before.
476
+ await ctx.now();
477
+
478
+ try {
479
+ await runWithJourneyBoundary(boundary, () => options.run(user, ctx));
480
+
481
+ const completedAt = new Date();
482
+ await db
483
+ .update(journeyStates)
484
+ .set({
485
+ status: "completed",
486
+ completedAt,
487
+ updatedAt: completedAt,
488
+ })
489
+ .where(eq(journeyStates.id, stateId));
490
+
491
+ // Fire-and-forget completion transition (best-effort; never throws).
492
+ logTransition({
493
+ db,
494
+ journeyStateId: stateId,
495
+ from: boundary.currentLabel ?? null,
496
+ to: "end-completed",
497
+ action: "completed",
498
+ });
499
+
500
+ await hatchet.events.push("journey:completed", {
501
+ journeyId: meta.id,
502
+ stateId,
503
+ userId,
504
+ });
505
+
506
+ // OUTBOUND `journey.completed` — fired alongside the internal
507
+ // `journey:completed` push. Runs in the WORKER (this durable task), so it
508
+ // uses the engine `db`/`hatchet`/`logger` singletons. `dedupeKey` =
509
+ // `journey.completed:<stateId>`: a Hatchet re-execution recomputes the
510
+ // identical key and the unique `(endpointId, dedupeKey)` index absorbs the
511
+ // duplicate (risk 3). `journey:failed` is NOT in the catalog → no emit.
512
+ void emitOutbound({
513
+ db,
514
+ hatchet,
515
+ logger,
516
+ event: "journey.completed",
517
+ dedupeKey: `journey.completed:${stateId}`,
518
+ payload: {
519
+ journeyId: meta.id,
520
+ journeyName: meta.name,
521
+ stateId,
522
+ userId,
523
+ userEmail,
524
+ completedAt: completedAt.toISOString(),
525
+ },
526
+ }).catch(logger.warn);
527
+
528
+ return { stateId, status: "completed" };
529
+ } catch (err) {
530
+ // The journey reached a terminal state (exitOn / cancel) while suspended
531
+ // in a durable wait. The state row is already terminal — stop gracefully
532
+ // without marking it "failed" or re-pushing a journey:failed event.
533
+ if (err instanceof JourneyExitedError) {
534
+ return { stateId, status: "exited" };
535
+ }
536
+
537
+ // Graceful worker shutdown (SIGTERM → worker.stop()) ABORTS in-flight
538
+ // durable runs so Hatchet can REASSIGN them: the suspended sleepFor/
539
+ // waitFor rejects with the SDK's AbortError. That is a RELEASE, not a
540
+ // failure — writing "failed" + pushing journey:failed here permanently
541
+ // POISONS the enrollment (recovery-first later finds a terminal row and
542
+ // never resumes), turning every graceful redeploy mid-wait into enrollment
543
+ // death. Detect the abort by the SDK's error CONTRACT (name/code — the
544
+ // message text varies, so never match on it), with the DurableContext's
545
+ // aborted signal as a belt-and-braces fallback.
546
+ const isAbort =
547
+ (err instanceof Error &&
548
+ (err.name === "AbortError" ||
549
+ (err as { code?: string }).code === "ABORT_ERR")) ||
550
+ hatchetCtx.abortController?.signal?.aborted === true;
551
+
552
+ if (isAbort) {
553
+ // Read the CURRENT status: an exitOn/admin cancel may have flipped the
554
+ // row terminal BEFORE the abort surfaced — keep today's "exited"
555
+ // outcome there. Otherwise the row is still waiting/active (the
556
+ // graceful-shutdown release) — leave it EXACTLY as-is so recovery-first
557
+ // (by hatchetRunId) resumes the recorded window on re-dispatch. The old
558
+ // "failed" write was converting a redeploy into permanent enrollment
559
+ // death; if Hatchet ever fails to re-dispatch, the stranded-waiting
560
+ // alert flags the row — strictly better than a false "failed".
561
+ const current = await db.query.journeyStates.findFirst({
562
+ where: eq(journeyStates.id, stateId),
563
+ columns: { status: true },
564
+ });
565
+ const status = current?.status;
566
+ if (status && (TERMINAL_STATUSES as readonly string[]).includes(status)) {
567
+ return { stateId, status: "exited" };
568
+ }
569
+ logger.info(
570
+ "journey run released mid-wait (worker shutdown/reassignment); " +
571
+ "enrollment left intact for re-dispatch",
572
+ { journeyId: meta.id, stateId, status },
573
+ );
574
+ // Rethrow so the SDK completes its cancellation flow. NO row write, NO
575
+ // journey:failed push, NO "failed" transition.
576
+ throw err;
577
+ }
578
+
579
+ const message = options.serializeError
580
+ ? options.serializeError(err)
581
+ : err instanceof Error
582
+ ? err.message
583
+ : "Unknown error during journey";
584
+
585
+ // Mark "failed" ONLY if the row isn't already terminal. A run cancelled
586
+ // by exitOn (ingestEvent sets "exited" then `runs.cancel`) or by the
587
+ // admin route surfaces here as a Hatchet AbortError thrown from the
588
+ // suspended waitFor/sleepFor — NOT a JourneyExitedError. Guarding on a
589
+ // non-terminal status prevents clobbering that "exited" row to "failed"
590
+ // and emitting a spurious journey:failed event.
591
+ const [failed] = await db
592
+ .update(journeyStates)
593
+ .set({
594
+ status: "failed",
595
+ errorMessage: message,
596
+ updatedAt: new Date(),
597
+ })
598
+ .where(
599
+ and(
600
+ eq(journeyStates.id, stateId),
601
+ notInArray(journeyStates.status, [...TERMINAL_STATUSES]),
602
+ ),
603
+ )
604
+ .returning({ id: journeyStates.id });
605
+
606
+ if (!failed) {
607
+ // Already terminal (cancelled after exit) — swallow the cancellation
608
+ // so the run doesn't double-report as failed.
609
+ return { stateId, status: "exited" };
610
+ }
611
+
612
+ logger.error("Journey run failed", {
613
+ journeyId: meta.id,
614
+ journeyName: meta.name,
615
+ stateId,
616
+ userId,
617
+ error: message,
618
+ });
619
+
620
+ // Fire-and-forget failure transition — only reached when a row was
621
+ // actually flipped to "failed" (the guard above). `to` is the best-effort
622
+ // last durable node (approximate — see plan Guardrails). Never throws.
623
+ logTransition({
624
+ db,
625
+ journeyStateId: stateId,
626
+ from: null,
627
+ to: boundary.currentLabel ?? state.currentNodeId ?? null,
628
+ action: "failed",
629
+ detail: { error: message },
630
+ });
631
+
632
+ await hatchet.events.push("journey:failed", {
633
+ journeyId: meta.id,
634
+ stateId,
635
+ userId,
636
+ error: message,
637
+ });
638
+
639
+ throw err;
640
+ }
641
+ }