@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
@@ -21,7 +21,11 @@ import { dlqRouter } from "./dlq.js";
21
21
  import { domainRouter } from "./domain.js";
22
22
  import { emailsRouter } from "./emails.js";
23
23
  import { eventsRouter } from "./events.js";
24
+ import { adminFlagsRouter } from "./flags.js";
24
25
  import { adminFunnelsRouter } from "./funnels.js";
26
+ import { groupsRouter } from "./groups.js";
27
+ import { impactOverviewRouter } from "./impact.js";
28
+ import { journeyImpactRouter } from "./journey-impact.js";
25
29
  import { journeyLogsRouter } from "./journey-logs.js";
26
30
  import { journeysRouter } from "./journeys.js";
27
31
  import { linksRouter } from "./links.js";
@@ -32,7 +36,9 @@ import { preferencesRouter } from "./preferences.js";
32
36
  import { providerCredentialsRouter } from "./provider-credentials.js";
33
37
  import { readinessRouter } from "./readiness.js";
34
38
  import { reportingRouter } from "./reporting.js";
39
+ import { settingsRouter } from "./settings.js";
35
40
  import { suppressionsRouter } from "./suppressions.js";
41
+ import { targetingRouter } from "./targeting.js";
36
42
  import { templatesRouter } from "./templates.js";
37
43
  import { timelineRouter } from "./timeline.js";
38
44
  import { webhooksRouter } from "./webhooks.js";
@@ -46,6 +52,8 @@ adminRouter.route("/contacts", contactsRouter);
46
52
  adminRouter.route("/contacts", preferencesRouter);
47
53
  adminRouter.route("/contacts", timelineRouter);
48
54
  adminRouter.route("/journeys", journeysRouter);
55
+ adminRouter.route("/journeys", journeyImpactRouter);
56
+ adminRouter.route("/impact", impactOverviewRouter);
49
57
  adminRouter.route("/blueprints", blueprintsRouter);
50
58
  adminRouter.route("/open-in-editor", openEditorRouter);
51
59
  adminRouter.route("/buckets", bucketsRouter);
@@ -55,6 +63,9 @@ adminRouter.route("/deals", adminDealsRouter);
55
63
  adminRouter.route("/conversions", adminConversionsRouter);
56
64
  adminRouter.route("/attribution", adminAttributionRouter);
57
65
  adminRouter.route("/funnels", adminFunnelsRouter);
66
+ adminRouter.route("/groups", groupsRouter);
67
+ adminRouter.route("/flags", adminFlagsRouter);
68
+ adminRouter.route("/targeting", targetingRouter);
58
69
  adminRouter.route("/events", eventsRouter);
59
70
  adminRouter.route("/emails", emailsRouter);
60
71
  adminRouter.route("/links", linksRouter);
@@ -62,6 +73,7 @@ adminRouter.route("/journey-logs", journeyLogsRouter);
62
73
  adminRouter.route("/metrics", metricsRouter);
63
74
  adminRouter.route("/reporting", reportingRouter);
64
75
  adminRouter.route("/templates", templatesRouter);
76
+ adminRouter.route("/settings", settingsRouter);
65
77
  adminRouter.route("/suppressions", suppressionsRouter);
66
78
  adminRouter.route("/api-keys", apiKeysRouter);
67
79
  adminRouter.route("/webhooks", webhooksRouter);
@@ -0,0 +1,446 @@
1
+ import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";
2
+ import { sql } from "drizzle-orm";
3
+ import type { AppEnv } from "../../app.js";
4
+ import { getConversionRegistry } from "../../lib/conversions.js";
5
+ import {
6
+ computeJourneyLift,
7
+ computeLiftValues,
8
+ } from "../../lib/journey-lift.js";
9
+ import { computeLift } from "../../lib/lift-stats.js";
10
+ import {
11
+ cohortSchema,
12
+ countsSchema,
13
+ type DefinitionSource,
14
+ definitionSourceSchema,
15
+ liftVerdictSchema,
16
+ } from "./impact-schemas.js";
17
+
18
+ /**
19
+ * GET /v1/admin/journeys/{id}/impact — the per-journey impact readout
20
+ * (impact-experiments spec D4.4).
21
+ *
22
+ * FROZEN CONTRACT (phase 2b): these wire shapes ARE the Studio contract —
23
+ * Studio mirror types (packages/studio admin-api.ts) are written
24
+ * mechanically from them. Additive-only from here; any rename/retype needs
25
+ * a coordinated Studio + docs change.
26
+ *
27
+ * Causal-language law (routes/admin/funnels.ts:16): `causal: true` appears
28
+ * ONLY inside holdout-backed blocks; observational blocks carry
29
+ * `causal: false`. No row-level ambiguous causal flag exists anywhere.
30
+ */
31
+
32
+ export const goalSchema = z.object({
33
+ /** Effective conversion definition scoping every outcome below.
34
+ * query.definitionId beats meta.goal beats null (= any definition). */
35
+ definitionId: z.string().nullable(),
36
+ source: definitionSourceSchema,
37
+ /** Registered definition's display name; null when unscoped/unknown. */
38
+ name: z.string().nullable(),
39
+ });
40
+
41
+ export const overallSchema = z.object({
42
+ /** Causal-language law (routes/admin/funnels.ts:16): true ONLY when a
43
+ * held-out cohort exists to compare against. When false, `treatment` IS
44
+ * the observational read (there is no separate observational block —
45
+ * consumers render treatment with the observational label). */
46
+ causal: z.boolean(),
47
+ treatment: cohortSchema,
48
+ control: cohortSchema,
49
+ /** Null when control.contacts === 0 — with zero control contacts,
50
+ * computeLift would integrate against the uniform Beta(1,1) prior and
51
+ * print a confident-looking winProbability: an uninformed prior
52
+ * masquerading as evidence. The impact surface refuses that.
53
+ * (Deliberately stricter than /lift, which emits a verdict regardless.) */
54
+ verdict: liftVerdictSchema.nullable(),
55
+ });
56
+
57
+ export const versionSchema = z.object({
58
+ /** journey_version_hash; null = the pre-versioning bucket. Treat a new
59
+ * hash as "possible new version" — toolchain bumps can fork it. */
60
+ hash: z.string().nullable(),
61
+ /** Latest-by-created_at label seen on this hash's rows. Hash is truth. */
62
+ label: z.string().nullable(),
63
+ firstEnrolledAt: z.string().nullable(), // ISO; null if only held_out rows
64
+ lastEnrolledAt: z.string().nullable(),
65
+ enrollments: z.number(), // distinct treated users, this hash, in window
66
+ converters: z.number(),
67
+ rate: z.number(),
68
+ /** Contemporaneous holdout lift: control = held_out rows carrying the
69
+ * SAME hash (stamped at diversion by the same deployed code). Null when
70
+ * this version diverted nobody. causal: true by construction when
71
+ * present. NOTE: version-INTERNAL lift is causal; comparing raw rates
72
+ * ACROSS versions is observational — older versions had longer
73
+ * post-entry conversion exposure. A salt/percent change between versions
74
+ * can place one user in treatment under one hash and control under
75
+ * another in the UNVERSIONED overall lift (pre-existing in /lift; hash
76
+ * matching makes it visible, and per-version blocks are immune).
77
+ * COMPOSITION CAVEAT: held_out rows are inserted once-ever per (user,
78
+ * journey), so on re-entrant journeys a later version's treatment mixes
79
+ * novices with returning veterans while its same-hash control holds
80
+ * post-fork novices only — read multi-version re-entrant lift with that
81
+ * skew in mind. */
82
+ liftVsControl: z
83
+ .object({ causal: z.literal(true), control: countsSchema })
84
+ .extend(liftVerdictSchema.shape)
85
+ .nullable(),
86
+ });
87
+
88
+ export const variantArmSchema = z.object({
89
+ arm: z.string(),
90
+ enrollments: z.number(),
91
+ converters: z.number(),
92
+ rate: z.number(),
93
+ /** OBSERVATIONAL engagement funnel for this arm (email_sends joined via
94
+ * journey_state_id) — the first readout an operator asks for on a
95
+ * subject-line test. */
96
+ engagement: z.object({
97
+ causal: z.literal(false),
98
+ sends: z.number(),
99
+ opened: z.number(),
100
+ clicked: z.number(),
101
+ }),
102
+ /** Arm cohort vs the WHOLE held-out cohort (Decision B). Null when the
103
+ * journey has no held-out contacts in the window. CONDITIONING CAVEAT:
104
+ * an arm cohort is conditioned on the enrollment SURVIVING to the
105
+ * ctx.variant call site (branches, exits, errors during earlier waits)
106
+ * while the held-out cohort is unconditioned — arm-vs-holdout is cleanly
107
+ * causal only when the variant call is unconditional near journey start;
108
+ * arm-vs-arm is the always-clean randomized comparison. */
109
+ liftVsControl: z
110
+ .object({ causal: z.literal(true) })
111
+ .extend(liftVerdictSchema.shape)
112
+ .nullable(),
113
+ });
114
+
115
+ export const variantSchema = z.object({
116
+ key: z.string(),
117
+ arms: z.array(variantArmSchema),
118
+ });
119
+
120
+ export const impactResponseSchema = z.object({
121
+ journeyId: z.string(),
122
+ days: z.number(),
123
+ goal: goalSchema,
124
+ /** Authored holdout config from the registry meta; null when none or
125
+ * when the journey is unregistered. Requires the D0 schema fix — holdout
126
+ * MUST be read from the fixed schema/registry, never assumed. */
127
+ holdout: z.object({ percent: z.number() }).nullable(),
128
+ /** The CURRENT deployed definition's identity (what a fresh enrollment
129
+ * would stamp); null when unregistered. */
130
+ currentVersionHash: z.string().nullable(),
131
+ currentVersionLabel: z.string().nullable(),
132
+ overall: overallSchema,
133
+ /** Newest version first (by first activity). */
134
+ versions: z.array(versionSchema),
135
+ variants: z.array(variantSchema),
136
+ });
137
+
138
+ /** timestamptz → ISO string; tolerant of driver Date/string variance. */
139
+ function toIso(value: unknown): string | null {
140
+ if (value == null) return null;
141
+ return new Date(value as string | Date).toISOString();
142
+ }
143
+
144
+ const errorSchema = z.object({ error: z.string() });
145
+
146
+ const impactRoute = createRoute({
147
+ method: "get",
148
+ path: "/{id}/impact",
149
+ tags: ["Admin — Journeys"],
150
+ summary: "Impact readout: holdout lift, version cohorts, variant arms",
151
+ description:
152
+ "Only holdout-backed blocks carry causal language; cross-version and " +
153
+ "no-control numbers are observational. Blueprint journeys are " +
154
+ "DB-authored and can never declare meta.goal (code-first law), so " +
155
+ 'their readout is permanently source "none" unless a definitionId ' +
156
+ "query param is passed. A DEFINED journey excluded by ENABLED_JOURNEYS " +
157
+ 'is never registered, so it also reads as source "none".',
158
+ request: {
159
+ params: z.object({ id: z.string() }),
160
+ query: z.object({
161
+ days: z.coerce.number().min(1).max(365).default(90),
162
+ /** Scope the outcome to one conversion point.
163
+ * Default: the journey's meta.goal; else any conversion. */
164
+ definitionId: z.string().optional(),
165
+ }),
166
+ },
167
+ responses: {
168
+ 200: {
169
+ content: { "application/json": { schema: impactResponseSchema } },
170
+ description: "Per-journey impact readout with per-block causal labeling",
171
+ },
172
+ 404: {
173
+ content: { "application/json": { schema: errorSchema } },
174
+ description: "Journey is not registered and has no journey_states rows",
175
+ },
176
+ },
177
+ });
178
+
179
+ export const journeyImpactRouter = new OpenAPIHono<AppEnv>().openapi(
180
+ impactRoute,
181
+ async (c) => {
182
+ const { db, registry } = c.get("container");
183
+ const { id } = c.req.valid("param");
184
+ const { days, definitionId: queryDefinitionId } = c.req.valid("query");
185
+ const since = new Date(Date.now() - days * 24 * 60 * 60 * 1000);
186
+
187
+ // 1. Goal resolution — explicit query param > meta.goal > any.
188
+ // registry.get(id) is undefined for (a) blueprint/removed journeys with
189
+ // historical states and (b) DEFINED journeys excluded by the
190
+ // ENABLED_JOURNEYS csv — both keep the any-definition behavior and
191
+ // report source "none".
192
+ const meta = registry.get(id);
193
+ const goal = meta?.goal;
194
+ const definitionId = queryDefinitionId ?? goal;
195
+ const source: DefinitionSource = queryDefinitionId
196
+ ? "query"
197
+ : goal
198
+ ? "goal"
199
+ : "none";
200
+ const name = definitionId
201
+ ? (getConversionRegistry()
202
+ ?.getAll()
203
+ .find((d) => d.meta.id === definitionId)?.meta.name ?? null)
204
+ : null;
205
+
206
+ // 2. 404 guard — unregistered AND no states ever (covers blueprint
207
+ // enrollments and removed journeys; the route never 404s a journey
208
+ // that has data).
209
+ if (!meta) {
210
+ const guardRows = await db.execute<{ count: number }>(
211
+ sql`select count(*)::int as count
212
+ from journey_states
213
+ where journey_id = ${id}`,
214
+ );
215
+ if (([...guardRows][0]?.count ?? 0) === 0) {
216
+ return c.json({ error: "Journey not found" }, 404);
217
+ }
218
+ }
219
+
220
+ // 3. Overall — the ONE implementation of the causal math (2a helper).
221
+ const [liftResult, values] = await Promise.all([
222
+ computeJourneyLift({ db, journeyId: id, since, definitionId }),
223
+ computeLiftValues({ db, journeyId: id, since, definitionId }),
224
+ ]);
225
+ const causal = liftResult.control.contacts > 0;
226
+ const overall = {
227
+ causal,
228
+ treatment: { ...liftResult.treatment, value: values.treatment },
229
+ control: { ...liftResult.control, value: values.control },
230
+ verdict: causal ? liftResult.verdict : null,
231
+ };
232
+
233
+ // 4. Versions — ONE grouped query; control matched by SAME hash, not a
234
+ // date-range slice: (a) exact even when two code versions run
235
+ // concurrently (blue-green — date windows would cross-contaminate);
236
+ // (b) survives gaps and low-traffic versions; (c) treatment and control
237
+ // share the identical exposure period by construction. The per-row ITT
238
+ // clock (occurred_at >= created_at) equalizes post-assignment exposure
239
+ // within the version. Label pick is latest-by-created_at (array_agg
240
+ // form) — max() is lexicographic and shows stale labels after a
241
+ // label-only rename (the label is excluded from the hash).
242
+ const definitionSql = definitionId
243
+ ? sql` and c.definition_id = ${definitionId}`
244
+ : sql``;
245
+ const sinceTs = sql`${since.toISOString()}::timestamptz`;
246
+ const versionRows = await db.execute<{
247
+ hash: string | null;
248
+ label: string | null;
249
+ first_enrolled_at: string | Date | null;
250
+ last_enrolled_at: string | Date | null;
251
+ enrollments: number;
252
+ converters: number;
253
+ control_contacts: number;
254
+ control_converters: number;
255
+ }>(sql`
256
+ select
257
+ js.journey_version_hash as hash,
258
+ (array_agg(js.journey_version_label order by js.created_at desc)
259
+ filter (where js.journey_version_label is not null))[1] as label,
260
+ min(js.created_at) filter (where js.status != 'held_out')
261
+ as first_enrolled_at,
262
+ max(js.created_at) filter (where js.status != 'held_out')
263
+ as last_enrolled_at,
264
+ count(distinct js.user_id)
265
+ filter (where js.status != 'held_out')::int as enrollments,
266
+ (count(distinct js.user_id) filter (where js.status != 'held_out'
267
+ and exists (
268
+ select 1 from conversions c
269
+ where c.user_key = js.user_id
270
+ and c.occurred_at >= js.created_at${definitionSql}
271
+ )))::int as converters,
272
+ count(distinct js.user_id)
273
+ filter (where js.status = 'held_out')::int as control_contacts,
274
+ (count(distinct js.user_id) filter (where js.status = 'held_out'
275
+ and exists (
276
+ select 1 from conversions c
277
+ where c.user_key = js.user_id
278
+ and c.occurred_at >= js.created_at${definitionSql}
279
+ )))::int as control_converters
280
+ from journey_states js
281
+ where js.journey_id = ${id}
282
+ and js.created_at >= ${sinceTs}
283
+ group by js.journey_version_hash
284
+ order by min(js.created_at) desc
285
+ `);
286
+
287
+ const versions = [...versionRows].map((r) => {
288
+ const enrollments = Number(r.enrollments);
289
+ const converters = Number(r.converters);
290
+ const controlContacts = Number(r.control_contacts);
291
+ const controlConverters = Number(r.control_converters);
292
+ return {
293
+ hash: r.hash,
294
+ label: r.label,
295
+ firstEnrolledAt: toIso(r.first_enrolled_at),
296
+ lastEnrolledAt: toIso(r.last_enrolled_at),
297
+ enrollments,
298
+ converters,
299
+ rate: enrollments > 0 ? converters / enrollments : 0,
300
+ liftVsControl:
301
+ controlContacts > 0
302
+ ? {
303
+ causal: true as const,
304
+ control: {
305
+ contacts: controlContacts,
306
+ converters: controlConverters,
307
+ rate:
308
+ controlContacts > 0
309
+ ? controlConverters / controlContacts
310
+ : 0,
311
+ },
312
+ ...computeLift({
313
+ treatment: { contacts: enrollments, converters },
314
+ control: {
315
+ contacts: controlContacts,
316
+ converters: controlConverters,
317
+ },
318
+ }),
319
+ }
320
+ : null,
321
+ };
322
+ });
323
+
324
+ // 5. Variants — arms enumerated FROM DATA (a removed arm still reports
325
+ // its historical cohort; injection is closed at the seeding strip, 1d).
326
+ // jsonb_each_text unwraps the recordOnce-stored JSON string values to
327
+ // bare text (record-once.ts:93). held_out rows never run run() so never
328
+ // carry variants (the status filter is belt-and-suspenders). Rows
329
+ // enrolled before an experiment shipped have no arm — excluded by the
330
+ // `?` operator filter, so arm cohorts may sum < treatment total.
331
+ // Control for EVERY arm = the overall held-out cohort (Decision B).
332
+ const [variantCountRows, engagementRows] = await Promise.all([
333
+ db.execute<{
334
+ variant_key: string;
335
+ arm: string;
336
+ enrollments: number;
337
+ converters: number;
338
+ }>(sql`
339
+ select
340
+ v.key as variant_key,
341
+ v.arm as arm,
342
+ count(distinct js.user_id)::int as enrollments,
343
+ (count(distinct js.user_id) filter (where exists (
344
+ select 1 from conversions c
345
+ where c.user_key = js.user_id
346
+ and c.occurred_at >= js.created_at${definitionSql}
347
+ )))::int as converters
348
+ from journey_states js
349
+ cross join lateral
350
+ jsonb_each_text(js.context -> '__variants__') as v(key, arm)
351
+ where js.journey_id = ${id}
352
+ and js.created_at >= ${sinceTs}
353
+ and js.status != 'held_out'
354
+ and js.context ? '__variants__'
355
+ group by v.key, v.arm
356
+ order by v.key, v.arm
357
+ `),
358
+ db.execute<{
359
+ variant_key: string;
360
+ arm: string;
361
+ sends: number;
362
+ opened: number;
363
+ clicked: number;
364
+ }>(sql`
365
+ select v.key as variant_key, v.arm as arm,
366
+ count(es.id)::int as sends,
367
+ count(es.opened_at)::int as opened,
368
+ count(es.clicked_at)::int as clicked
369
+ from journey_states js
370
+ cross join lateral
371
+ jsonb_each_text(js.context -> '__variants__') as v(key, arm)
372
+ join email_sends es on es.journey_state_id = js.id
373
+ where js.journey_id = ${id}
374
+ and js.created_at >= ${sinceTs}
375
+ and js.status != 'held_out'
376
+ and js.context ? '__variants__'
377
+ group by v.key, v.arm
378
+ `),
379
+ ]);
380
+
381
+ const engagementByArm = new Map(
382
+ [...engagementRows].map((r) => [
383
+ `${r.variant_key}:${r.arm}`,
384
+ {
385
+ sends: Number(r.sends),
386
+ opened: Number(r.opened),
387
+ clicked: Number(r.clicked),
388
+ },
389
+ ]),
390
+ );
391
+ const controlCounts = {
392
+ contacts: liftResult.control.contacts,
393
+ converters: liftResult.control.converters,
394
+ };
395
+ const armsByKey = new Map<string, z.infer<typeof variantArmSchema>[]>();
396
+ for (const r of [...variantCountRows]) {
397
+ const enrollments = Number(r.enrollments);
398
+ const converters = Number(r.converters);
399
+ const engagement = engagementByArm.get(`${r.variant_key}:${r.arm}`) ?? {
400
+ sends: 0,
401
+ opened: 0,
402
+ clicked: 0,
403
+ };
404
+ const arm = {
405
+ arm: r.arm,
406
+ enrollments,
407
+ converters,
408
+ rate: enrollments > 0 ? converters / enrollments : 0,
409
+ engagement: { causal: false as const, ...engagement },
410
+ liftVsControl:
411
+ controlCounts.contacts > 0
412
+ ? {
413
+ causal: true as const,
414
+ ...computeLift({
415
+ treatment: { contacts: enrollments, converters },
416
+ control: controlCounts,
417
+ }),
418
+ }
419
+ : null,
420
+ };
421
+ const list = armsByKey.get(r.variant_key) ?? [];
422
+ list.push(arm);
423
+ armsByKey.set(r.variant_key, list);
424
+ }
425
+ // SQL already ordered by (key, arm); Map preserves insertion order.
426
+ const variants = [...armsByKey.entries()].map(([key, arms]) => ({
427
+ key,
428
+ arms,
429
+ }));
430
+
431
+ return c.json(
432
+ {
433
+ journeyId: id,
434
+ days,
435
+ goal: { definitionId: definitionId ?? null, source, name },
436
+ holdout: meta?.holdout ? { percent: meta.holdout.percent } : null,
437
+ currentVersionHash: meta?.versionHash ?? null,
438
+ currentVersionLabel: meta?.version ?? null,
439
+ overall,
440
+ versions,
441
+ variants,
442
+ },
443
+ 200,
444
+ );
445
+ },
446
+ );