@hogsend/engine 0.59.0 → 0.61.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 (55) hide show
  1. package/package.json +11 -11
  2. package/src/buckets/bucket-access.ts +34 -11
  3. package/src/buckets/check-membership.ts +74 -10
  4. package/src/buckets/membership-epoch.ts +11 -1
  5. package/src/campaigns/cohort-sql.ts +10 -0
  6. package/src/index.ts +10 -6
  7. package/src/journeys/execute-journey-run.ts +139 -70
  8. package/src/journeys/journey-context.ts +43 -7
  9. package/src/lib/agent/tools.ts +29 -4
  10. package/src/lib/attribution-backfill.ts +35 -4
  11. package/src/lib/attribution.ts +22 -3
  12. package/src/lib/connector-actions.ts +17 -17
  13. package/src/lib/contacts.ts +541 -175
  14. package/src/lib/conversion-dispatch.ts +16 -2
  15. package/src/lib/enrollment-guards.ts +19 -12
  16. package/src/lib/feed.ts +5 -5
  17. package/src/lib/flags.ts +26 -9
  18. package/src/lib/ingestion.ts +14 -2
  19. package/src/lib/journey-lift.ts +17 -5
  20. package/src/lib/preferences.ts +78 -29
  21. package/src/lib/recipient-preferences.ts +16 -2
  22. package/src/lib/refine.ts +16 -10
  23. package/src/lib/revenue.ts +20 -5
  24. package/src/lib/sms-inbound.ts +6 -1
  25. package/src/lib/sms-tracked.ts +18 -1
  26. package/src/lib/timezone.ts +11 -1
  27. package/src/lib/tracked.ts +19 -8
  28. package/src/lib/tracking-events.ts +28 -0
  29. package/src/lib/unique-violation.ts +44 -0
  30. package/src/routes/admin/buckets.ts +21 -1
  31. package/src/routes/admin/bulk.ts +14 -2
  32. package/src/routes/admin/contacts.ts +20 -7
  33. package/src/routes/admin/conversions.ts +20 -4
  34. package/src/routes/admin/emails.ts +17 -2
  35. package/src/routes/admin/events.ts +32 -41
  36. package/src/routes/admin/funnels.ts +6 -1
  37. package/src/routes/admin/groups.ts +4 -0
  38. package/src/routes/admin/impact.ts +30 -10
  39. package/src/routes/admin/journey-impact.ts +18 -9
  40. package/src/routes/admin/journeys.ts +8 -1
  41. package/src/routes/admin/maintenance.ts +7 -1
  42. package/src/routes/admin/preferences.ts +85 -46
  43. package/src/routes/admin/reporting.ts +25 -5
  44. package/src/routes/admin/targeting.ts +1 -0
  45. package/src/routes/admin/timeline.ts +16 -10
  46. package/src/routes/email/preferences.ts +18 -1
  47. package/src/routes/feed/recipient.ts +5 -0
  48. package/src/routes/lists/index.ts +6 -6
  49. package/src/workflows/backfill-contact-id.ts +210 -30
  50. package/src/workflows/bucket-backfill.ts +145 -67
  51. package/src/workflows/bucket-reconcile.ts +164 -51
  52. package/src/workflows/check-alerts.ts +4 -0
  53. package/src/workflows/impact-digest.ts +10 -3
  54. package/src/workflows/journey-blueprint-interpreter.ts +8 -1
  55. package/src/workflows/send-campaign.ts +18 -11
@@ -17,6 +17,7 @@ import {
17
17
  isColdChannelAllowed,
18
18
  } from "../sources/define-contact-source.js";
19
19
  import { getContactSourceRegistry } from "../sources/registry.js";
20
+ import { lookupContactIdByKey } from "./contacts.js";
20
21
  import { getDb } from "./db.js";
21
22
  import { createLogger } from "./logger.js";
22
23
  import { readRecipientPreferences } from "./recipient-preferences.js";
@@ -27,15 +28,6 @@ import { readRecipientPreferences } from "./recipient-preferences.js";
27
28
  // validates env at boot.
28
29
  const logger = createLogger(process.env.LOG_LEVEL);
29
30
 
30
- /**
31
- * Matches the canonical UUID form of `contacts.id` (8-4-4-4-12 hex). Gates the
32
- * `eq(contacts.id, ref)` leg so a uuid comparison only runs for a genuinely
33
- * uuid-shaped ref — an email/snowflake ref would otherwise raise a Postgres
34
- * `22P02 invalid input syntax for type uuid`.
35
- */
36
- const CONTACT_ID_UUID =
37
- /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
38
-
39
31
  /**
40
32
  * Matches a NAMESPACED platform ref (`telegram:<chatId>`, `discord:<id>`, …):
41
33
  * a leading `<namespace>:` followed by a non-empty value. Captures the namespace
@@ -49,10 +41,10 @@ const NAMESPACED_REF = /^([a-z][a-z0-9_-]*):(.+)$/i;
49
41
  * when the ref is uuid-shaped — the contact's row `id`. Matching every canonical
50
42
  * key form (the journey subject key is `external_id ?? anonymous_id ?? id`) lets
51
43
  * `member: user.id` resolve a member who has NOT linked (an anonymous Discord
52
- * contact keyed by its uuid) or an email-only member. The uuid `id` leg is
53
- * shape-gated: `contacts.id` is a uuid column and an unguarded `eq(id, ref)`
54
- * against an email/snowflake text ref throws an invalid-uuid cast. First live
55
- * match wins.
44
+ * contact keyed by its uuid) or an email-only member. Those three canonical
45
+ * forms — plus a merged-away key that survives only as an identity row — are
46
+ * resolved by `lookupContactIdByKey` (which owns the uuid shape-gate: an
47
+ * unguarded `id = <text ref>` throws 22P02). First live match wins.
56
48
  *
57
49
  * NAMESPACED refs (`telegram:<chatId>`) ADDITIONALLY match the platform metadata
58
50
  * under `properties.<namespace>` (the `chat_id`/`id` fields). This is load-bearing
@@ -82,6 +74,13 @@ async function resolveContact(
82
74
  sql`${contacts.properties} -> ${nsMatch[1]}::text ->> 'id' = ${nsMatch[2]}`,
83
75
  ]
84
76
  : [];
77
+ // The three CANONICAL-key legs (external_id / anonymous_id / uuid id) are
78
+ // resolved by the alias-aware primitive instead of probed as columns: same
79
+ // row for a live key, same miss for an unknown one, and — new — the SURVIVOR
80
+ // for a merged-away key, which no column probe can see (the loser's row is
81
+ // soft-deleted). The email / discord / namespaced-jsonb legs stay columns:
82
+ // `lookupContactIdByKey` deliberately answers only for canonical keys.
83
+ const canonicalId = await lookupContactIdByKey(db, ref);
85
84
  const rows = await db
86
85
  .select({
87
86
  id: contacts.id,
@@ -96,11 +95,9 @@ async function resolveContact(
96
95
  and(
97
96
  isNull(contacts.deletedAt),
98
97
  or(
98
+ ...(canonicalId ? [eq(contacts.id, canonicalId)] : []),
99
99
  eq(contacts.email, ref),
100
- eq(contacts.externalId, ref),
101
100
  eq(contacts.discordId, ref),
102
- eq(contacts.anonymousId, ref),
103
- ...(CONTACT_ID_UUID.test(ref) ? [eq(contacts.id, ref)] : []),
104
101
  ...namespacedLegs,
105
102
  ),
106
103
  ),
@@ -224,9 +221,12 @@ export async function checkActionAudience(
224
221
  });
225
222
  }
226
223
 
224
+ // PRD 05 T6 — the contact row is in hand, so the read is contact-scoped
225
+ // directly; the old `externalId ?? id` string derivation is the idiom the
226
+ // flip retires.
227
227
  const prefs = await readRecipientPreferences(db, {
228
228
  email: contact.email,
229
- userId: contact.externalId ?? contact.id,
229
+ contactId: contact.id,
230
230
  });
231
231
  if (prefs.unsubscribedAll) {
232
232
  return {