@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.
- package/README.md +6 -0
- package/package.json +13 -10
- package/src/campaigns/cohort-sql.ts +8 -9
- package/src/connectors/presets/index.ts +8 -2
- package/src/container.ts +150 -8
- package/src/destinations/define-destination.ts +2 -2
- package/src/destinations/presets/posthog.ts +7 -0
- package/src/destinations/presets/segment.ts +5 -0
- package/src/env.ts +38 -0
- package/src/flags/reconcile.ts +200 -0
- package/src/index.ts +79 -0
- package/src/journeys/authoring.ts +32 -0
- package/src/journeys/define-journey.ts +45 -40
- package/src/journeys/execute-journey-run.ts +73 -32
- package/src/journeys/journey-boundary.ts +76 -0
- package/src/journeys/journey-context.ts +128 -70
- package/src/journeys/journey-task-factory.ts +31 -0
- package/src/journeys/journey-task-runtime.ts +31 -0
- package/src/journeys/journey-version.test.ts +236 -0
- package/src/journeys/journey-version.ts +158 -0
- package/src/journeys/record-once.ts +28 -2
- package/src/lib/campaign-send-key.ts +9 -18
- package/src/lib/connector-actions.test.ts +36 -0
- package/src/lib/connector-actions.ts +42 -4
- package/src/lib/contacts.ts +49 -0
- package/src/lib/conversions.test.ts +33 -0
- package/src/lib/conversions.ts +9 -1
- package/src/lib/crm-deals.ts +1 -1
- package/src/lib/crm-ingest.ts +1 -1
- package/src/lib/email.ts +23 -2
- package/src/lib/enrollment-policy.ts +51 -0
- package/src/lib/event-names.ts +7 -1
- package/src/lib/feed.ts +55 -3
- package/src/lib/flags-registry.ts +48 -0
- package/src/lib/flags.ts +498 -0
- package/src/lib/frequency-cap.ts +4 -2
- package/src/lib/funnel-progress.ts +1 -1
- package/src/lib/fx.ts +374 -0
- package/src/lib/global-control-readout.ts +166 -0
- package/src/lib/groups.ts +362 -0
- package/src/lib/holdout.ts +1 -1
- package/src/lib/ingestion.ts +38 -0
- package/src/lib/journey-lift.ts +142 -0
- package/src/lib/journey-suppress.ts +4 -2
- package/src/lib/lift-stats.ts +2 -2
- package/src/lib/operator-settings.ts +71 -0
- package/src/lib/outbound.ts +147 -0
- package/src/lib/preferences.ts +6 -80
- package/src/lib/recipient-preferences.ts +53 -0
- package/src/lib/sms.ts +21 -2
- package/src/lib/stable-stringify.test.ts +42 -0
- package/src/lib/stable-stringify.ts +23 -0
- package/src/lib/tracked.ts +9 -2
- package/src/lib/variant.ts +99 -0
- package/src/lib/webhook-signing.ts +16 -1
- package/src/lists/registry.ts +6 -2
- package/src/lists/subscription.ts +13 -0
- package/src/routes/admin/attribution.ts +1 -2
- package/src/routes/admin/campaigns.ts +84 -45
- package/src/routes/admin/contacts.ts +53 -4
- package/src/routes/admin/conversions.ts +138 -1
- package/src/routes/admin/deals.ts +43 -34
- package/src/routes/admin/emails.ts +4 -6
- package/src/routes/admin/events.ts +128 -1
- package/src/routes/admin/flags.ts +369 -0
- package/src/routes/admin/groups.ts +900 -0
- package/src/routes/admin/impact-schemas.ts +44 -0
- package/src/routes/admin/impact-wire.test.ts +181 -0
- package/src/routes/admin/impact.ts +486 -0
- package/src/routes/admin/index.ts +12 -0
- package/src/routes/admin/journey-impact.ts +446 -0
- package/src/routes/admin/journeys.ts +70 -100
- package/src/routes/admin/settings.ts +145 -0
- package/src/routes/admin/targeting.ts +259 -0
- package/src/routes/events/index.ts +11 -0
- package/src/routes/flags/index.ts +120 -0
- package/src/routes/groups/index.ts +417 -0
- package/src/routes/index.ts +23 -1
- package/src/testing.ts +25 -0
- package/src/webhook-sources/presets/index.ts +5 -1
- package/src/webhook-sources/presets/intercom.ts +230 -0
- package/src/webhook-sources/presets/segment.ts +56 -4
- package/src/worker.ts +3 -0
- package/src/workflows/bucket-backfill.ts +1 -14
- package/src/workflows/crm-reconcile.ts +1 -1
- package/src/workflows/impact-digest.ts +761 -0
- package/src/workflows/journey-blueprint-interpreter.ts +18 -1
- package/src/middleware/auth.ts +0 -24
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Boot-time reconciler for code-defined feature flags ({@link defineFlag}).
|
|
3
|
+
*
|
|
4
|
+
* Runs once at container build (in BOTH the API and worker processes),
|
|
5
|
+
* fire-and-forget and best-effort — a single malformed definition warns and is
|
|
6
|
+
* skipped; the reconciler NEVER throws and never fails boot. State stays
|
|
7
|
+
* DB-owned: this only writes the CONTRACT (identity + served shape). Per
|
|
8
|
+
* definition (deduped on key):
|
|
9
|
+
*
|
|
10
|
+
* - No live row → INSERT the contract fields with `origin:"code"`, born OFF
|
|
11
|
+
* (`enabled:false` — the DB column defaults to `true`, so this MUST be set
|
|
12
|
+
* explicitly) and inert (`rollout:0`, `targeting:[]`, `conditionSets:[]`).
|
|
13
|
+
* The insert is `onConflictDoNothing` against the partial-unique live-key
|
|
14
|
+
* index, so two replicas reconciling concurrently is safe.
|
|
15
|
+
* - Live row with `origin:"code"` → sync CONTRACT drift ONLY (name /
|
|
16
|
+
* description / type / variants / defaultValue) via a canonical-JSON diff.
|
|
17
|
+
* Operator STATE (enabled / rollout / targeting / conditionSets) is NEVER
|
|
18
|
+
* touched — an operator who turned the flag on and set a rollout keeps it.
|
|
19
|
+
* - Live row with a non-`code` origin (e.g. a Studio-authored "native" flag
|
|
20
|
+
* that happens to share the key) → LEFT untouched + a warn. Code never
|
|
21
|
+
* steals an operator's flag.
|
|
22
|
+
* - A flag removed from code → not iterated → left exactly as-is (no
|
|
23
|
+
* auto-archive).
|
|
24
|
+
*/
|
|
25
|
+
import type { DefinedFlag } from "@hogsend/core";
|
|
26
|
+
import { flags } from "@hogsend/db";
|
|
27
|
+
import { and, eq, isNull, sql } from "drizzle-orm";
|
|
28
|
+
import type { HogsendClient } from "../container.js";
|
|
29
|
+
|
|
30
|
+
export interface FlagReconcileResult {
|
|
31
|
+
created: number;
|
|
32
|
+
updated: number;
|
|
33
|
+
skipped: number;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export async function reconcileDefinedFlags(opts: {
|
|
37
|
+
client: HogsendClient;
|
|
38
|
+
flags: DefinedFlag[];
|
|
39
|
+
}): Promise<FlagReconcileResult> {
|
|
40
|
+
const { client } = opts;
|
|
41
|
+
const { db, logger } = client;
|
|
42
|
+
const result: FlagReconcileResult = { created: 0, updated: 0, skipped: 0 };
|
|
43
|
+
const seen = new Set<string>();
|
|
44
|
+
|
|
45
|
+
for (const def of opts.flags) {
|
|
46
|
+
const meta = def.meta;
|
|
47
|
+
const key = meta.key;
|
|
48
|
+
|
|
49
|
+
if (seen.has(key)) {
|
|
50
|
+
logger.warn("flags: duplicate defineFlag key — skipping", { key });
|
|
51
|
+
result.skipped++;
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
seen.add(key);
|
|
55
|
+
|
|
56
|
+
// The contract projection, resolved once. `defaultValue` defaults to the
|
|
57
|
+
// type's natural empty (false for boolean, null for multivariate) — never
|
|
58
|
+
// relying on the DB column default so the row matches the definition
|
|
59
|
+
// byte-for-byte.
|
|
60
|
+
const variants = [...(meta.variants ?? [])];
|
|
61
|
+
const defaultValue =
|
|
62
|
+
meta.defaultValue ?? (meta.type === "boolean" ? false : null);
|
|
63
|
+
const description = meta.description ?? null;
|
|
64
|
+
|
|
65
|
+
try {
|
|
66
|
+
const existingRows = await db
|
|
67
|
+
.select({
|
|
68
|
+
id: flags.id,
|
|
69
|
+
origin: flags.origin,
|
|
70
|
+
name: flags.name,
|
|
71
|
+
description: flags.description,
|
|
72
|
+
type: flags.type,
|
|
73
|
+
variants: flags.variants,
|
|
74
|
+
defaultValue: flags.defaultValue,
|
|
75
|
+
})
|
|
76
|
+
.from(flags)
|
|
77
|
+
.where(and(eq(flags.key, key), isNull(flags.archivedAt)))
|
|
78
|
+
.limit(1);
|
|
79
|
+
const existing = existingRows[0];
|
|
80
|
+
|
|
81
|
+
if (!existing) {
|
|
82
|
+
const inserted = await db
|
|
83
|
+
.insert(flags)
|
|
84
|
+
.values({
|
|
85
|
+
key,
|
|
86
|
+
name: meta.name,
|
|
87
|
+
description,
|
|
88
|
+
// The DB column defaults to `true`; a defined flag is born OFF
|
|
89
|
+
// (unless the definition seeds `enabled:true`) so a deploy never
|
|
90
|
+
// silently flips live traffic. Explicit by contract.
|
|
91
|
+
enabled: meta.enabled ?? false,
|
|
92
|
+
type: meta.type,
|
|
93
|
+
variants,
|
|
94
|
+
defaultValue,
|
|
95
|
+
targeting: [],
|
|
96
|
+
rollout: 0,
|
|
97
|
+
conditionSets: [],
|
|
98
|
+
origin: "code",
|
|
99
|
+
})
|
|
100
|
+
// The loser of a two-replica insert race defers to the winner. The
|
|
101
|
+
// `where` predicate MUST match the partial-unique index
|
|
102
|
+
// (`flags_key_unique_idx WHERE archived_at IS NULL`) or Postgres
|
|
103
|
+
// cannot infer the conflict arbiter (42P10).
|
|
104
|
+
.onConflictDoNothing({
|
|
105
|
+
target: flags.key,
|
|
106
|
+
where: sql`archived_at is null`,
|
|
107
|
+
})
|
|
108
|
+
.returning({ id: flags.id });
|
|
109
|
+
|
|
110
|
+
if (inserted[0]) {
|
|
111
|
+
logger.info("flags: defined flag created (disabled)", { key });
|
|
112
|
+
result.created++;
|
|
113
|
+
} else {
|
|
114
|
+
result.skipped++;
|
|
115
|
+
}
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// A same-key flag owned by an operator/Studio (origin != "code") is never
|
|
120
|
+
// clobbered by a code definition — leave it and warn. The warn names the
|
|
121
|
+
// resolution because this is easy to trip innocently: `origin` defaults
|
|
122
|
+
// to "native" on insert, so any row seeded out-of-band (SQL, a seed
|
|
123
|
+
// script) for a code-defined key lands here on every boot.
|
|
124
|
+
if (existing.origin !== "code") {
|
|
125
|
+
logger.warn(
|
|
126
|
+
`flags: key "${key}" already owned by a non-code flag (origin "${existing.origin}") — leaving it untouched. ` +
|
|
127
|
+
"If this row should be code-owned (contract-synced from defineFlag), set origin = 'code' on it; " +
|
|
128
|
+
"otherwise remove the defineFlag that shares this key.",
|
|
129
|
+
{ key, origin: existing.origin },
|
|
130
|
+
);
|
|
131
|
+
result.skipped++;
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const changed =
|
|
136
|
+
existing.name !== meta.name ||
|
|
137
|
+
(existing.description ?? null) !== description ||
|
|
138
|
+
existing.type !== meta.type ||
|
|
139
|
+
canonicalJson(existing.variants ?? []) !== canonicalJson(variants) ||
|
|
140
|
+
canonicalJson(existing.defaultValue ?? null) !==
|
|
141
|
+
canonicalJson(defaultValue);
|
|
142
|
+
|
|
143
|
+
if (!changed) {
|
|
144
|
+
result.skipped++;
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// Contract drift only. STATE columns (enabled / rollout / targeting /
|
|
149
|
+
// conditionSets) are deliberately absent from this SET. The CAS on
|
|
150
|
+
// `origin = "code"` keeps an operator who flipped provenance mid-flight
|
|
151
|
+
// from being overwritten.
|
|
152
|
+
await db
|
|
153
|
+
.update(flags)
|
|
154
|
+
.set({
|
|
155
|
+
name: meta.name,
|
|
156
|
+
description,
|
|
157
|
+
type: meta.type,
|
|
158
|
+
variants,
|
|
159
|
+
defaultValue,
|
|
160
|
+
updatedAt: new Date(),
|
|
161
|
+
})
|
|
162
|
+
.where(and(eq(flags.id, existing.id), eq(flags.origin, "code")));
|
|
163
|
+
|
|
164
|
+
logger.info("flags: defined flag contract synced", { key });
|
|
165
|
+
result.updated++;
|
|
166
|
+
} catch (err) {
|
|
167
|
+
// One bad definition must never take down boot for every other flag.
|
|
168
|
+
logger.warn("flags: reconcile failed for definition — skipping", {
|
|
169
|
+
key,
|
|
170
|
+
error: err instanceof Error ? err.message : String(err),
|
|
171
|
+
});
|
|
172
|
+
result.skipped++;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return result;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Key-order-canonical JSON for the changed-comparison. `existing.variants` /
|
|
181
|
+
* `existing.defaultValue` round-trip through Postgres jsonb, which does NOT
|
|
182
|
+
* preserve object key order, so a plain `JSON.stringify` would report a phantom
|
|
183
|
+
* "changed" for any multi-key object — rewriting (and logging) an unchanged
|
|
184
|
+
* definition on every boot. Arrays keep order; only object keys are sorted.
|
|
185
|
+
*/
|
|
186
|
+
function canonicalJson(value: unknown): string {
|
|
187
|
+
return JSON.stringify(sortKeysDeep(value));
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function sortKeysDeep(value: unknown): unknown {
|
|
191
|
+
if (Array.isArray(value)) return value.map(sortKeysDeep);
|
|
192
|
+
if (value !== null && typeof value === "object") {
|
|
193
|
+
return Object.fromEntries(
|
|
194
|
+
Object.entries(value as Record<string, unknown>)
|
|
195
|
+
.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))
|
|
196
|
+
.map(([k, entry]) => [k, sortKeysDeep(entry)]),
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
return value;
|
|
200
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
// Install the production Hatchet binding. The `./journeys` authoring subpath
|
|
2
|
+
// intentionally omits this side effect so journey modules stay test-importable
|
|
3
|
+
// without database or Hatchet credentials.
|
|
4
|
+
import "./journeys/journey-task-runtime.js";
|
|
5
|
+
|
|
1
6
|
// @hogsend/engine — public API surface (the committed semver boundary).
|
|
2
7
|
//
|
|
3
8
|
// Content (journeys, webhook sources, workflows) is injected into these
|
|
@@ -196,6 +201,11 @@ export {
|
|
|
196
201
|
} from "./destinations/registry-singleton.js";
|
|
197
202
|
// --- Env ---
|
|
198
203
|
export { API_VERSION, env } from "./env.js";
|
|
204
|
+
// --- Native feature flags (DB-backed, sticky evaluation) ---
|
|
205
|
+
export {
|
|
206
|
+
type FlagReconcileResult,
|
|
207
|
+
reconcileDefinedFlags,
|
|
208
|
+
} from "./flags/reconcile.js";
|
|
199
209
|
// --- Journeys ---
|
|
200
210
|
export { BLUEPRINT_RUN_EVENT } from "./journeys/constants.js";
|
|
201
211
|
export {
|
|
@@ -222,6 +232,11 @@ export {
|
|
|
222
232
|
deriveJourneyKey,
|
|
223
233
|
getJourneyBoundary,
|
|
224
234
|
type JourneyBoundary,
|
|
235
|
+
type JourneyConnectorEffect,
|
|
236
|
+
type JourneyEmailEffect,
|
|
237
|
+
type JourneyFeedEffect,
|
|
238
|
+
type JourneyServiceOverrides,
|
|
239
|
+
type JourneySmsEffect,
|
|
225
240
|
parseJourneySendSite,
|
|
226
241
|
registerKey,
|
|
227
242
|
registerRecordLabel,
|
|
@@ -244,9 +259,14 @@ export {
|
|
|
244
259
|
resetJourneySources,
|
|
245
260
|
setJourneySources,
|
|
246
261
|
} from "./journeys/journey-sources-singleton.js";
|
|
262
|
+
export {
|
|
263
|
+
computeJourneyVersionHash,
|
|
264
|
+
normalizeRunSource,
|
|
265
|
+
} from "./journeys/journey-version.js";
|
|
247
266
|
export {
|
|
248
267
|
type RecordNamespace,
|
|
249
268
|
recordOnce,
|
|
269
|
+
stripRecordNamespaces,
|
|
250
270
|
} from "./journeys/record-once.js";
|
|
251
271
|
export {
|
|
252
272
|
buildJourneyRegistry,
|
|
@@ -451,6 +471,11 @@ export type {
|
|
|
451
471
|
} from "./lib/email-service-types.js";
|
|
452
472
|
// --- Enrollment guards ---
|
|
453
473
|
export { checkEmailPreferences } from "./lib/enrollment-guards.js";
|
|
474
|
+
export {
|
|
475
|
+
type EnrollmentPolicyFacts,
|
|
476
|
+
type EnrollmentPolicyResult,
|
|
477
|
+
evaluateEnrollmentPolicy,
|
|
478
|
+
} from "./lib/enrollment-policy.js";
|
|
454
479
|
// --- In-app feed (sendFeedItem — sibling of sendEmail/sendConnectorAction) ---
|
|
455
480
|
export {
|
|
456
481
|
IN_APP_LIST_ID,
|
|
@@ -458,9 +483,42 @@ export {
|
|
|
458
483
|
type SendFeedItemResult,
|
|
459
484
|
sendFeedItem,
|
|
460
485
|
} from "./lib/feed.js";
|
|
486
|
+
export {
|
|
487
|
+
type EvaluableFlag,
|
|
488
|
+
emptySnapshot,
|
|
489
|
+
evaluateFlag,
|
|
490
|
+
evaluateFlagsForContact,
|
|
491
|
+
evaluateTargeting,
|
|
492
|
+
flagBucket,
|
|
493
|
+
flagUnit,
|
|
494
|
+
loadTargetingSnapshot,
|
|
495
|
+
type TargetingEvalContext,
|
|
496
|
+
type TargetingSnapshot,
|
|
497
|
+
} from "./lib/flags.js";
|
|
498
|
+
export {
|
|
499
|
+
FlagRegistry,
|
|
500
|
+
getFlagRegistry,
|
|
501
|
+
resetFlagRegistry,
|
|
502
|
+
setFlagRegistry,
|
|
503
|
+
} from "./lib/flags-registry.js";
|
|
461
504
|
export { countRecentSends, isFrequencyCapped } from "./lib/frequency-cap.js";
|
|
462
505
|
export { addrSpecOf, hostOfFromAddress } from "./lib/from-address.js";
|
|
463
506
|
export { FunnelRegistry } from "./lib/funnel-registry.js";
|
|
507
|
+
// --- Base-currency FX lens (optional; docs/groups.md §Base-currency lens) ---
|
|
508
|
+
export {
|
|
509
|
+
createFrankfurterFxProvider,
|
|
510
|
+
createFxLens,
|
|
511
|
+
createStaticFxProvider,
|
|
512
|
+
type FxLens,
|
|
513
|
+
type FxRatesToBase,
|
|
514
|
+
fxProviderFromEnv,
|
|
515
|
+
parseFxRatesEnv,
|
|
516
|
+
} from "./lib/fx.js";
|
|
517
|
+
export {
|
|
518
|
+
computeGlobalControlReadout,
|
|
519
|
+
GLOBAL_CONTROL_SCAN_CEILING,
|
|
520
|
+
type GlobalControlReadout,
|
|
521
|
+
} from "./lib/global-control-readout.js";
|
|
464
522
|
export { hatchet } from "./lib/hatchet.js";
|
|
465
523
|
export {
|
|
466
524
|
globalControlPercent,
|
|
@@ -488,6 +546,15 @@ export {
|
|
|
488
546
|
type IngestResult,
|
|
489
547
|
ingestEvent,
|
|
490
548
|
} from "./lib/ingestion.js";
|
|
549
|
+
// --- Reconciled journey-lift helper (impact experiments D4.1) — the ONE
|
|
550
|
+
// implementation of the causal cohort math, shared by /lift (2a), /impact
|
|
551
|
+
// (2b), and the impact digest (3b) ---
|
|
552
|
+
export {
|
|
553
|
+
computeJourneyLift,
|
|
554
|
+
computeLiftValues,
|
|
555
|
+
type JourneyLiftResult,
|
|
556
|
+
type LiftCohort,
|
|
557
|
+
} from "./lib/journey-lift.js";
|
|
491
558
|
// --- Leader lease (connector-runtime singleton election) ---
|
|
492
559
|
export {
|
|
493
560
|
acquireLeaderLease,
|
|
@@ -534,6 +601,10 @@ export {
|
|
|
534
601
|
// --- Outbound webhooks: emit spine (Section 1.4) ---
|
|
535
602
|
export {
|
|
536
603
|
emitOutbound,
|
|
604
|
+
type ImpactDigestEntry,
|
|
605
|
+
type ImpactDigestLiftEntry,
|
|
606
|
+
type ImpactDigestShippedEntry,
|
|
607
|
+
type ImpactVersionCohort,
|
|
537
608
|
OUTBOUND_EVENTS,
|
|
538
609
|
type OutboundEventName,
|
|
539
610
|
type OutboundPayloads,
|
|
@@ -785,6 +856,7 @@ export {
|
|
|
785
856
|
// --- Integration presets (Section 2.3/2.4) ---
|
|
786
857
|
export {
|
|
787
858
|
clerkSource,
|
|
859
|
+
intercomSource,
|
|
788
860
|
PRESET_SOURCES,
|
|
789
861
|
type PresetId,
|
|
790
862
|
presetsFromEnv,
|
|
@@ -821,6 +893,13 @@ export {
|
|
|
821
893
|
deliverWebhookTask,
|
|
822
894
|
reapDueWebhookDeliveriesTask,
|
|
823
895
|
} from "./workflows/deliver-webhook.js";
|
|
896
|
+
export {
|
|
897
|
+
buildImpactDigest,
|
|
898
|
+
detectLiftCrossings,
|
|
899
|
+
detectShippedVersions,
|
|
900
|
+
type ImpactDigestInput,
|
|
901
|
+
impactDigestTask,
|
|
902
|
+
} from "./workflows/impact-digest.js";
|
|
824
903
|
export { importContactsTask } from "./workflows/import-contacts.js";
|
|
825
904
|
export {
|
|
826
905
|
importSuppressionsTask,
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Environment-free journey authoring surface.
|
|
3
|
+
*
|
|
4
|
+
* Journey files should import from this entry point so deterministic tests can
|
|
5
|
+
* load the original module without validating production database or Hatchet
|
|
6
|
+
* credentials. Importing the main `@hogsend/engine` entry in an API/worker
|
|
7
|
+
* process installs the production Hatchet task binding before tasks are read.
|
|
8
|
+
*/
|
|
9
|
+
export * from "@hogsend/core";
|
|
10
|
+
export {
|
|
11
|
+
type SendConnectorActionArgs,
|
|
12
|
+
sendConnectorAction,
|
|
13
|
+
} from "../lib/connector-actions.js";
|
|
14
|
+
export {
|
|
15
|
+
type SendEmailOptions,
|
|
16
|
+
type SendEmailResult,
|
|
17
|
+
sendEmail,
|
|
18
|
+
} from "../lib/email.js";
|
|
19
|
+
export {
|
|
20
|
+
type SendFeedItemOptions,
|
|
21
|
+
type SendFeedItemResult,
|
|
22
|
+
sendFeedItem,
|
|
23
|
+
} from "../lib/feed.js";
|
|
24
|
+
export {
|
|
25
|
+
type SendSmsOptions,
|
|
26
|
+
type SendSmsResult,
|
|
27
|
+
sendSms,
|
|
28
|
+
} from "../lib/sms.js";
|
|
29
|
+
export {
|
|
30
|
+
type DefinedJourney,
|
|
31
|
+
defineJourney,
|
|
32
|
+
} from "./define-journey.js";
|
|
@@ -6,19 +6,19 @@ import type {
|
|
|
6
6
|
JourneyMetaInput,
|
|
7
7
|
JourneyRunFn,
|
|
8
8
|
} from "@hogsend/core/types";
|
|
9
|
-
import { hatchet } from "../lib/hatchet.js";
|
|
10
9
|
import {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
executeJourneyRun,
|
|
17
|
-
} from "./execute-journey-run.js";
|
|
10
|
+
createJourneyTask,
|
|
11
|
+
hasJourneyTaskFactory,
|
|
12
|
+
type JourneyTask,
|
|
13
|
+
} from "./journey-task-factory.js";
|
|
14
|
+
import { computeJourneyVersionHash } from "./journey-version.js";
|
|
18
15
|
|
|
19
16
|
export interface DefinedJourney {
|
|
20
17
|
meta: JourneyMeta;
|
|
21
|
-
|
|
18
|
+
/** Original author function. Testing harnesses execute this directly. */
|
|
19
|
+
run: JourneyRunFn;
|
|
20
|
+
/** Production Hatchet task, materialized eagerly by the runtime or lazily on access. */
|
|
21
|
+
task: JourneyTask;
|
|
22
22
|
/**
|
|
23
23
|
* The journey's `run` function serialized via `Function.prototype.toString()`,
|
|
24
24
|
* captured at definition time. This is the substrate the Studio journey-graph
|
|
@@ -126,7 +126,7 @@ export function defineJourney(options: {
|
|
|
126
126
|
const source = captureCallSite();
|
|
127
127
|
const { trigger, exitOn, ...rest } = options.meta;
|
|
128
128
|
const triggerWhere = normalizeWhere(trigger.where);
|
|
129
|
-
const
|
|
129
|
+
const normalized: JourneyMeta = {
|
|
130
130
|
...rest,
|
|
131
131
|
trigger: {
|
|
132
132
|
event: trigger.event,
|
|
@@ -144,36 +144,41 @@ export function defineJourney(options: {
|
|
|
144
144
|
}
|
|
145
145
|
: {}),
|
|
146
146
|
};
|
|
147
|
+
// Impact experiments (Decision A): attach the engine-computed content
|
|
148
|
+
// fingerprint AFTER normalization, so `where` builder fns are already
|
|
149
|
+
// resolved POJOs (the hash input is canonical data, never a function).
|
|
150
|
+
// NEVER authored: the spread overwrites any input value (JourneyMetaInput
|
|
151
|
+
// omits versionHash; JS callers are overridden here). Both the eager task
|
|
152
|
+
// path and the lazy authoring-subpath getter below close over this same
|
|
153
|
+
// meta — executeJourneyRun sees the hash with zero further plumbing.
|
|
154
|
+
const meta: JourneyMeta = {
|
|
155
|
+
...normalized,
|
|
156
|
+
versionHash: computeJourneyVersionHash({
|
|
157
|
+
meta: normalized,
|
|
158
|
+
body: runSource,
|
|
159
|
+
}),
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
const definition = { meta, run: options.run, runSource, source };
|
|
147
163
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
// first (Resend/Postmark `Idempotency-Key`); tracked as a follow-up.
|
|
160
|
-
retries: 0,
|
|
161
|
-
// `scheduleTimeout` widens the queue-wait ceiling (SDK default ~5m) so a
|
|
162
|
-
// durable-wait RESUME re-queued during a redeploy's slot saturation reclaims
|
|
163
|
-
// a slot instead of being cancelled (which strands the enrollment). Unlike
|
|
164
|
-
// retries this adds NO replay — it is pure head-room, so it is safe on its own.
|
|
165
|
-
scheduleTimeout: JOURNEY_SCHEDULE_TIMEOUT,
|
|
166
|
-
// The full enrollment + run lifecycle lives in `executeJourneyRun`, shared
|
|
167
|
-
// verbatim with the blueprint interpreter task (spec §6) — code journeys
|
|
168
|
-
// and blueprints execute through the IDENTICAL machinery.
|
|
169
|
-
fn: async (input: EventPayloadInput, hatchetCtx) =>
|
|
170
|
-
executeJourneyRun({
|
|
171
|
-
meta,
|
|
172
|
-
run: options.run,
|
|
173
|
-
input,
|
|
174
|
-
hatchetCtx,
|
|
175
|
-
}),
|
|
176
|
-
});
|
|
164
|
+
// Main-engine imports install the production task factory before callers can
|
|
165
|
+
// invoke defineJourney, preserving the existing eager task behavior. The
|
|
166
|
+
// environment-free authoring subpath intentionally leaves it uninstalled;
|
|
167
|
+
// its task getter remains dormant in unit tests and materializes after the
|
|
168
|
+
// production runtime is loaded by a worker.
|
|
169
|
+
if (hasJourneyTaskFactory()) {
|
|
170
|
+
return {
|
|
171
|
+
...definition,
|
|
172
|
+
task: createJourneyTask(meta, options.run),
|
|
173
|
+
};
|
|
174
|
+
}
|
|
177
175
|
|
|
178
|
-
|
|
176
|
+
let task: JourneyTask | undefined;
|
|
177
|
+
return Object.defineProperty(definition, "task", {
|
|
178
|
+
enumerable: true,
|
|
179
|
+
get: () => {
|
|
180
|
+
task ??= createJourneyTask(meta, options.run);
|
|
181
|
+
return task;
|
|
182
|
+
},
|
|
183
|
+
}) as DefinedJourney;
|
|
179
184
|
}
|