@hogsend/engine 0.46.1 → 0.51.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 (88) hide show
  1. package/README.md +6 -0
  2. package/package.json +13 -10
  3. package/src/campaigns/cohort-sql.ts +8 -9
  4. package/src/connectors/presets/index.ts +8 -2
  5. package/src/container.ts +150 -8
  6. package/src/destinations/define-destination.ts +2 -2
  7. package/src/destinations/presets/posthog.ts +7 -0
  8. package/src/destinations/presets/segment.ts +5 -0
  9. package/src/env.ts +38 -0
  10. package/src/flags/reconcile.ts +200 -0
  11. package/src/index.ts +79 -0
  12. package/src/journeys/authoring.ts +32 -0
  13. package/src/journeys/define-journey.ts +45 -40
  14. package/src/journeys/execute-journey-run.ts +73 -32
  15. package/src/journeys/journey-boundary.ts +76 -0
  16. package/src/journeys/journey-context.ts +128 -70
  17. package/src/journeys/journey-task-factory.ts +31 -0
  18. package/src/journeys/journey-task-runtime.ts +31 -0
  19. package/src/journeys/journey-version.test.ts +236 -0
  20. package/src/journeys/journey-version.ts +158 -0
  21. package/src/journeys/record-once.ts +28 -2
  22. package/src/lib/campaign-send-key.ts +9 -18
  23. package/src/lib/connector-actions.test.ts +36 -0
  24. package/src/lib/connector-actions.ts +42 -4
  25. package/src/lib/contacts.ts +49 -0
  26. package/src/lib/conversions.test.ts +33 -0
  27. package/src/lib/conversions.ts +9 -1
  28. package/src/lib/crm-deals.ts +1 -1
  29. package/src/lib/crm-ingest.ts +1 -1
  30. package/src/lib/email.ts +23 -2
  31. package/src/lib/enrollment-policy.ts +51 -0
  32. package/src/lib/event-names.ts +7 -1
  33. package/src/lib/feed.ts +55 -3
  34. package/src/lib/flags-registry.ts +48 -0
  35. package/src/lib/flags.ts +498 -0
  36. package/src/lib/frequency-cap.ts +4 -2
  37. package/src/lib/funnel-progress.ts +1 -1
  38. package/src/lib/fx.ts +374 -0
  39. package/src/lib/global-control-readout.ts +166 -0
  40. package/src/lib/groups.ts +362 -0
  41. package/src/lib/holdout.ts +1 -1
  42. package/src/lib/ingestion.ts +38 -0
  43. package/src/lib/journey-lift.ts +142 -0
  44. package/src/lib/journey-suppress.ts +4 -2
  45. package/src/lib/lift-stats.ts +2 -2
  46. package/src/lib/operator-settings.ts +71 -0
  47. package/src/lib/outbound.ts +147 -0
  48. package/src/lib/preferences.ts +6 -80
  49. package/src/lib/recipient-preferences.ts +53 -0
  50. package/src/lib/sms.ts +21 -2
  51. package/src/lib/stable-stringify.test.ts +42 -0
  52. package/src/lib/stable-stringify.ts +23 -0
  53. package/src/lib/tracked.ts +9 -2
  54. package/src/lib/variant.ts +99 -0
  55. package/src/lib/webhook-signing.ts +16 -1
  56. package/src/lists/registry.ts +6 -2
  57. package/src/lists/subscription.ts +13 -0
  58. package/src/routes/admin/attribution.ts +1 -2
  59. package/src/routes/admin/campaigns.ts +84 -45
  60. package/src/routes/admin/contacts.ts +53 -4
  61. package/src/routes/admin/conversions.ts +138 -1
  62. package/src/routes/admin/deals.ts +43 -34
  63. package/src/routes/admin/emails.ts +4 -6
  64. package/src/routes/admin/events.ts +128 -1
  65. package/src/routes/admin/flags.ts +369 -0
  66. package/src/routes/admin/groups.ts +900 -0
  67. package/src/routes/admin/impact-schemas.ts +44 -0
  68. package/src/routes/admin/impact-wire.test.ts +181 -0
  69. package/src/routes/admin/impact.ts +486 -0
  70. package/src/routes/admin/index.ts +12 -0
  71. package/src/routes/admin/journey-impact.ts +446 -0
  72. package/src/routes/admin/journeys.ts +70 -100
  73. package/src/routes/admin/settings.ts +145 -0
  74. package/src/routes/admin/targeting.ts +259 -0
  75. package/src/routes/events/index.ts +11 -0
  76. package/src/routes/flags/index.ts +120 -0
  77. package/src/routes/groups/index.ts +417 -0
  78. package/src/routes/index.ts +23 -1
  79. package/src/testing.ts +25 -0
  80. package/src/webhook-sources/presets/index.ts +5 -1
  81. package/src/webhook-sources/presets/intercom.ts +230 -0
  82. package/src/webhook-sources/presets/segment.ts +56 -4
  83. package/src/worker.ts +3 -0
  84. package/src/workflows/bucket-backfill.ts +1 -14
  85. package/src/workflows/crm-reconcile.ts +1 -1
  86. package/src/workflows/impact-digest.ts +761 -0
  87. package/src/workflows/journey-blueprint-interpreter.ts +18 -1
  88. package/src/middleware/auth.ts +0 -24
@@ -0,0 +1,33 @@
1
+ import assert from "node:assert/strict";
2
+ import test from "node:test";
3
+ import { defineConversion } from "@hogsend/core";
4
+ import { ConversionRegistry } from "./conversions.js";
5
+
6
+ const signup = defineConversion({
7
+ id: "signup-completed",
8
+ name: "Signup completed",
9
+ trigger: { event: "signup.completed" },
10
+ });
11
+ // A wildcard definition (trigger.event "*") lands in the registry's separate
12
+ // `wildcard` list — `has()` must see it via `all`, not `byEvent`.
13
+ const wildcard = defineConversion({
14
+ id: "any-valued",
15
+ name: "Any valued event",
16
+ trigger: { event: "*" },
17
+ });
18
+
19
+ test("has() finds a definition registered under a named trigger", () => {
20
+ const registry = new ConversionRegistry([signup]);
21
+ assert.equal(registry.has("signup-completed"), true);
22
+ });
23
+
24
+ test("has() finds a WILDCARD definition (trigger.event '*')", () => {
25
+ const registry = new ConversionRegistry([wildcard]);
26
+ assert.equal(registry.has("any-valued"), true);
27
+ });
28
+
29
+ test("has() is false for an unknown id and on an empty registry", () => {
30
+ const registry = new ConversionRegistry([signup, wildcard]);
31
+ assert.equal(registry.has("signup-complete"), false); // typo'd id
32
+ assert.equal(new ConversionRegistry().has("signup-completed"), false);
33
+ });
@@ -12,7 +12,7 @@ import { REVENUE_EXCLUDED_EVENTS } from "./revenue.js";
12
12
  import { createOptionalSingleton } from "./singleton.js";
13
13
 
14
14
  /**
15
- * Conversion evaluation at ingest (docs/revenue-attribution-plan.md §5.1) —
15
+ * Conversion evaluation at ingest —
16
16
  * the hook `ingestEvent` calls after a FRESH event insert. Definitions are
17
17
  * code-first (`createHogsendClient({ conversions })`); a fired instance is
18
18
  * recorded once per (definition, event row) — the unique index makes a
@@ -64,6 +64,14 @@ export class ConversionRegistry {
64
64
  count(): number {
65
65
  return this.all.length;
66
66
  }
67
+
68
+ /**
69
+ * Membership check over ALL definitions (named-trigger + wildcard) by
70
+ * `meta.id` — the container's `meta.goal` boot validation reads this.
71
+ */
72
+ has(id: string): boolean {
73
+ return this.all.some((def) => def.meta.id === id);
74
+ }
67
75
  }
68
76
 
69
77
  const singleton = createOptionalSingleton<ConversionRegistry>();
@@ -10,7 +10,7 @@ import type { Logger } from "./logger.js";
10
10
 
11
11
  /**
12
12
  * The deals projection + external-id alias map
13
- * (docs/revenue-attribution-plan.md §4.2). Events stay the append-only truth;
13
+ *. Events stay the append-only truth;
14
14
  * this module materializes current deal state with the MONOTONIC stage rule:
15
15
  * a change applies only when it advances the canonical rank (heals
16
16
  * webhook+poll double-detection and out-of-order delivery), `lost` applies
@@ -20,7 +20,7 @@ import { emitOutbound } from "./outbound.js";
20
20
  /**
21
21
  * Land normalized {@link CrmStageEvent}s on the spine — the shared sink of
22
22
  * BOTH the webhook route and the reconciliation poll
23
- * (docs/revenue-attribution-plan.md §4).
23
+ *.
24
24
  *
25
25
  * Per event: (1) resolve the contact — `crm_links` alias first (works with
26
26
  * zero PII in the payload), else the payload email (minting the links so the
package/src/lib/email.ts CHANGED
@@ -75,8 +75,6 @@ export interface SendEmailResult {
75
75
  export async function sendEmail(
76
76
  opts: SendEmailOptions,
77
77
  ): Promise<SendEmailResult> {
78
- const service = getEmailService();
79
-
80
78
  // Exactly-once across a durable replay. When inside a journey run, derive a
81
79
  // deterministic, branch-stable key (anchored on the replay-stable
82
80
  // `boundary.runAnchor` = Hatchet run id, NOT the freshly-minted stateId) so a
@@ -109,6 +107,7 @@ export async function sendEmail(
109
107
  secret: process.env.BETTER_AUTH_SECRET,
110
108
  externalId: opts.userId,
111
109
  email: opts.to,
110
+ ...(boundary?.now ? { now: boundary.now() } : {}),
112
111
  });
113
112
  }
114
113
 
@@ -157,6 +156,24 @@ export async function sendEmail(
157
156
  headers,
158
157
  } as unknown as EmailServiceSendOptions;
159
158
 
159
+ const effect = {
160
+ to: opts.to,
161
+ userId: opts.userId,
162
+ template: String(opts.template),
163
+ ...(opts.subject !== undefined ? { subject: opts.subject } : {}),
164
+ props: sendOptions.props as Record<string, unknown>,
165
+ category: boundary?.category ?? "journey",
166
+ ...(opts.journeyName !== undefined
167
+ ? { journeyName: opts.journeyName }
168
+ : {}),
169
+ ...(opts.journeyStateId !== undefined
170
+ ? { journeyStateId: opts.journeyStateId }
171
+ : {}),
172
+ ...(resolvedIdempotencyKey
173
+ ? { idempotencyKey: resolvedIdempotencyKey }
174
+ : {}),
175
+ };
176
+
160
177
  // Layer 1 (fast path): when inside a journey on an eviction-capable engine,
161
178
  // run the provider send through Hatchet's durable `memo` so a replay returns
162
179
  // the recorded `{ emailSendId, sentAt }` WITHOUT re-hitting the provider or
@@ -164,6 +181,10 @@ export async function sendEmail(
164
181
  // eviction is unavailable — Layer 2 (the DB key above) still guarantees
165
182
  // exactly-once. Outside a journey, send directly (no boundary).
166
183
  const doSend = async (): Promise<SendEmailResult> => {
184
+ if (boundary?.services?.email) {
185
+ return boundary.services.email(effect);
186
+ }
187
+ const service = getEmailService();
167
188
  const result = await service.send(sendOptions);
168
189
  return {
169
190
  emailSendId: result.emailSendId,
@@ -0,0 +1,51 @@
1
+ import { evaluatePropertyConditions } from "@hogsend/core";
2
+ import type { JourneyMeta } from "@hogsend/core/types";
3
+
4
+ export interface EnrollmentPolicyFacts {
5
+ /** Omit when there is no persisted admin override. */
6
+ adminEnabled?: boolean;
7
+ entry?: { allowed: boolean; reason?: string };
8
+ unsubscribed?: boolean;
9
+ heldOut?: boolean;
10
+ alreadyActive?: boolean;
11
+ }
12
+
13
+ export type EnrollmentPolicyResult =
14
+ | { allowed: true }
15
+ | { allowed: false; reason: string };
16
+
17
+ /** Pure enrollment guard ordering shared by production and test fixtures. */
18
+ export function evaluateEnrollmentPolicy(opts: {
19
+ journey: JourneyMeta;
20
+ properties: Record<string, unknown>;
21
+ facts?: EnrollmentPolicyFacts;
22
+ }): EnrollmentPolicyResult {
23
+ const { journey, properties, facts = {} } = opts;
24
+ if (!journey.enabled) return { allowed: false, reason: "journey_disabled" };
25
+ if (facts.adminEnabled === false) {
26
+ return { allowed: false, reason: "journey_disabled_by_admin" };
27
+ }
28
+ if (
29
+ journey.trigger.where?.length &&
30
+ !evaluatePropertyConditions({
31
+ conditions: journey.trigger.where,
32
+ properties,
33
+ })
34
+ ) {
35
+ return { allowed: false, reason: "trigger_conditions_not_met" };
36
+ }
37
+ if (facts.entry && !facts.entry.allowed) {
38
+ return {
39
+ allowed: false,
40
+ reason: facts.entry.reason ?? "entry_limit",
41
+ };
42
+ }
43
+ if (facts.unsubscribed) {
44
+ return { allowed: false, reason: "user_unsubscribed" };
45
+ }
46
+ if (facts.heldOut) return { allowed: false, reason: "held_out" };
47
+ if (facts.alreadyActive) {
48
+ return { allowed: false, reason: "already_active" };
49
+ }
50
+ return { allowed: true };
51
+ }
@@ -14,7 +14,7 @@
14
14
  */
15
15
  import { journeyBlueprints, userEvents } from "@hogsend/db";
16
16
  import { z } from "@hono/zod-openapi";
17
- import { count, desc, ilike, inArray, max } from "drizzle-orm";
17
+ import { count, desc, ilike, inArray, max, min } from "drizzle-orm";
18
18
  import type { HogsendClient } from "../container.js";
19
19
 
20
20
  /**
@@ -25,6 +25,7 @@ import type { HogsendClient } from "../container.js";
25
25
  export const eventNameEntrySchema = z.object({
26
26
  name: z.string(),
27
27
  occurrences: z.number(),
28
+ firstSeenAt: z.string().nullable(),
28
29
  lastSeenAt: z.string().nullable(),
29
30
  // Where the name is declared as a trigger: "journey:<id>" for code journeys,
30
31
  // "blueprint:<id> (<status>)" for blueprints.
@@ -58,6 +59,7 @@ export async function listEventNameVocabulary(opts: {
58
59
  .select({
59
60
  name: userEvents.event,
60
61
  occurrences: count(),
62
+ firstSeenAt: min(userEvents.occurredAt),
61
63
  lastSeenAt: lastSeen,
62
64
  })
63
65
  .from(userEvents)
@@ -79,6 +81,7 @@ export async function listEventNameVocabulary(opts: {
79
81
  byName.set(row.name, {
80
82
  name: row.name,
81
83
  occurrences: Number(row.occurrences),
84
+ firstSeenAt: row.firstSeenAt?.toISOString() ?? null,
82
85
  lastSeenAt: row.lastSeenAt?.toISOString() ?? null,
83
86
  usedBy: [],
84
87
  });
@@ -92,6 +95,7 @@ export async function listEventNameVocabulary(opts: {
92
95
  const created: EventNameEntry = {
93
96
  name,
94
97
  occurrences: 0,
98
+ firstSeenAt: null,
95
99
  lastSeenAt: null,
96
100
  usedBy: [],
97
101
  };
@@ -126,6 +130,7 @@ export async function listEventNameVocabulary(opts: {
126
130
  .select({
127
131
  name: userEvents.event,
128
132
  occurrences: count(),
133
+ firstSeenAt: min(userEvents.occurredAt),
129
134
  lastSeenAt: max(userEvents.occurredAt),
130
135
  })
131
136
  .from(userEvents)
@@ -135,6 +140,7 @@ export async function listEventNameVocabulary(opts: {
135
140
  const entry = byName.get(row.name);
136
141
  if (!entry) continue;
137
142
  entry.occurrences = Number(row.occurrences);
143
+ entry.firstSeenAt = row.firstSeenAt?.toISOString() ?? null;
138
144
  entry.lastSeenAt = row.lastSeenAt?.toISOString() ?? null;
139
145
  }
140
146
  }
package/src/lib/feed.ts CHANGED
@@ -9,7 +9,7 @@ import { getListRegistry } from "../lists/registry-singleton.js";
9
9
  import { resolveOrCreateContact, resolveRecipient } from "./contacts.js";
10
10
  import { getDb } from "./db.js";
11
11
  import { createLogger } from "./logger.js";
12
- import { readRecipientPreferences } from "./preferences.js";
12
+ import { readRecipientPreferences } from "./recipient-preferences.js";
13
13
  import { getRedis } from "./redis.js";
14
14
 
15
15
  const logger = createLogger(process.env.LOG_LEVEL);
@@ -76,10 +76,63 @@ export interface SendFeedItemResult {
76
76
  export async function sendFeedItem(
77
77
  opts: SendFeedItemOptions,
78
78
  ): Promise<SendFeedItemResult> {
79
- const db = getDb();
80
79
  const { recipient } = opts;
80
+ // Keep the production recipient contract in front of the scoped test
81
+ // override. The normal path eventually enforces this in
82
+ // `resolveOrCreateContact`, but an override deliberately avoids the database;
83
+ // without this pure check a zero-recipient feed item passed in journey tests
84
+ // and failed only in production.
85
+ if (
86
+ !recipient.userId?.trim() &&
87
+ !recipient.email?.trim() &&
88
+ !recipient.anonymousId?.trim()
89
+ ) {
90
+ throw new Error(
91
+ "resolveOrCreateContact requires at least one of userId, email, " +
92
+ "anonymousId, discordId",
93
+ );
94
+ }
81
95
  const category = opts.category ?? IN_APP_LIST_ID;
82
96
 
97
+ const boundary = getJourneyBoundary();
98
+ const override = boundary?.services?.feed;
99
+ if (boundary && override) {
100
+ let overrideKey: string | undefined = opts.idempotencyKey;
101
+ if (!overrideKey) {
102
+ const site = opts.idempotencyLabel ?? boundary.currentLabel ?? opts.type;
103
+ overrideKey = deriveJourneyKey({
104
+ kind: "send",
105
+ anchor: boundary.runAnchor,
106
+ site,
107
+ discriminant: `feed:${opts.type}`,
108
+ });
109
+ registerKey(boundary, overrideKey);
110
+ }
111
+ const runOverride = () =>
112
+ override({
113
+ recipient,
114
+ type: opts.type,
115
+ ...(opts.title !== undefined ? { title: opts.title } : {}),
116
+ ...(opts.body !== undefined ? { body: opts.body } : {}),
117
+ ...(opts.blocks !== undefined ? { blocks: opts.blocks } : {}),
118
+ ...(opts.actionUrl !== undefined ? { actionUrl: opts.actionUrl } : {}),
119
+ ...(opts.metadata !== undefined ? { metadata: opts.metadata } : {}),
120
+ category,
121
+ ...(opts.templateKey !== undefined
122
+ ? { templateKey: opts.templateKey }
123
+ : {}),
124
+ ...(opts.journeyStateId !== undefined
125
+ ? { journeyStateId: opts.journeyStateId }
126
+ : {}),
127
+ ...(overrideKey ? { idempotencyKey: overrideKey } : {}),
128
+ });
129
+ return overrideKey
130
+ ? boundary.memoize([overrideKey], runOverride)
131
+ : runOverride();
132
+ }
133
+
134
+ const db = getDb();
135
+
83
136
  // (1) Resolve recipient → canonical key. Throws on a zero-key recipient (same
84
137
  // contract as `resolveOrCreateContact`). This is a server/journey-side send —
85
138
  // never a browser write — so no `restrictToAnonymous` clamp.
@@ -108,7 +161,6 @@ export async function sendFeedItem(
108
161
  // preference verdict INSIDE the memo closure (step 3) so the skip/allow verdict
109
162
  // is RECORDED by the durable memo and replays verbatim — the live-flipped
110
163
  // preference is never re-read on a replay.
111
- const boundary = getJourneyBoundary();
112
164
  let key: string | undefined = opts.idempotencyKey;
113
165
  if (!key && boundary) {
114
166
  const site = opts.idempotencyLabel ?? boundary.currentLabel ?? opts.type;
@@ -0,0 +1,48 @@
1
+ import type { DefinedFlag } from "@hogsend/core";
2
+ import type { Logger } from "./logger.js";
3
+ import { createOptionalSingleton } from "./singleton.js";
4
+
5
+ /**
6
+ * Registry of code-defined feature flags ({@link defineFlag}), the flag sibling
7
+ * of `ConversionRegistry`. Indexed by `meta.key`; a duplicate key warns and is
8
+ * skipped (first definition wins) so one fat-fingered copy-paste never silently
9
+ * shadows another flag. Constructed by `createHogsendClient` from `opts.flags`
10
+ * and installed on the process singleton so the boot reconciler + Studio can
11
+ * read it without a client reference.
12
+ */
13
+ export class FlagRegistry {
14
+ private byKey = new Map<string, DefinedFlag>();
15
+
16
+ constructor(definitions: DefinedFlag[] = [], logger?: Logger) {
17
+ for (const def of definitions) {
18
+ const key = def.meta.key;
19
+ if (this.byKey.has(key)) {
20
+ logger?.warn("flags: duplicate defineFlag key — skipping", { key });
21
+ continue;
22
+ }
23
+ this.byKey.set(key, def);
24
+ }
25
+ }
26
+
27
+ get(key: string): DefinedFlag | undefined {
28
+ return this.byKey.get(key);
29
+ }
30
+
31
+ has(key: string): boolean {
32
+ return this.byKey.has(key);
33
+ }
34
+
35
+ getAll(): DefinedFlag[] {
36
+ return [...this.byKey.values()];
37
+ }
38
+
39
+ count(): number {
40
+ return this.byKey.size;
41
+ }
42
+ }
43
+
44
+ const singleton = createOptionalSingleton<FlagRegistry>();
45
+ export const setFlagRegistry = singleton.set;
46
+ export const getFlagRegistry = singleton.get;
47
+ /** Reset the singleton — only for test cleanup. */
48
+ export const resetFlagRegistry = singleton.reset;