@hogsend/engine 0.67.0 → 0.68.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 (738) hide show
  1. package/README.md +3 -2
  2. package/dist/account-links/__fixtures__/fake-fetch.d.ts +38 -0
  3. package/dist/account-links/__fixtures__/fake-fetch.d.ts.map +1 -0
  4. package/dist/account-links/battlenet.d.ts +36 -0
  5. package/dist/account-links/battlenet.d.ts.map +1 -0
  6. package/dist/account-links/epic.d.ts +36 -0
  7. package/dist/account-links/epic.d.ts.map +1 -0
  8. package/dist/account-links/index.d.ts +7 -0
  9. package/dist/account-links/index.d.ts.map +1 -0
  10. package/dist/account-links/riot.d.ts +35 -0
  11. package/dist/account-links/riot.d.ts.map +1 -0
  12. package/dist/account-links/steam.d.ts +35 -0
  13. package/dist/account-links/steam.d.ts.map +1 -0
  14. package/dist/account-links/twitch.d.ts +38 -0
  15. package/dist/account-links/twitch.d.ts.map +1 -0
  16. package/dist/account-links/xbox.d.ts +39 -0
  17. package/dist/account-links/xbox.d.ts.map +1 -0
  18. package/dist/app.d.ts +50 -0
  19. package/dist/app.d.ts.map +1 -0
  20. package/dist/buckets/bucket-access.d.ts +60 -0
  21. package/dist/buckets/bucket-access.d.ts.map +1 -0
  22. package/dist/buckets/bucket-reactions.d.ts +100 -0
  23. package/dist/buckets/bucket-reactions.d.ts.map +1 -0
  24. package/dist/buckets/check-membership.d.ts +105 -0
  25. package/dist/buckets/check-membership.d.ts.map +1 -0
  26. package/dist/buckets/define-bucket.d.ts +53 -0
  27. package/dist/buckets/define-bucket.d.ts.map +1 -0
  28. package/dist/buckets/membership-epoch.d.ts +74 -0
  29. package/dist/buckets/membership-epoch.d.ts.map +1 -0
  30. package/dist/buckets/registry-singleton.d.ts +6 -0
  31. package/dist/buckets/registry-singleton.d.ts.map +1 -0
  32. package/dist/buckets/registry.d.ts +59 -0
  33. package/dist/buckets/registry.d.ts.map +1 -0
  34. package/dist/campaigns/cohort-sql.d.ts +54 -0
  35. package/dist/campaigns/cohort-sql.d.ts.map +1 -0
  36. package/dist/campaigns/define-campaign.d.ts +105 -0
  37. package/dist/campaigns/define-campaign.d.ts.map +1 -0
  38. package/dist/campaigns/reconcile.d.ts +13 -0
  39. package/dist/campaigns/reconcile.d.ts.map +1 -0
  40. package/dist/campaigns/steps.d.ts +29 -0
  41. package/dist/campaigns/steps.d.ts.map +1 -0
  42. package/dist/cold-connect/index.d.ts +98 -0
  43. package/dist/cold-connect/index.d.ts.map +1 -0
  44. package/dist/cold-connect/page.d.ts +69 -0
  45. package/dist/cold-connect/page.d.ts.map +1 -0
  46. package/dist/cold-connect/throttle.d.ts +39 -0
  47. package/dist/cold-connect/throttle.d.ts.map +1 -0
  48. package/dist/cold-connect/token.d.ts +57 -0
  49. package/dist/cold-connect/token.d.ts.map +1 -0
  50. package/dist/connectors/action-registry-singleton.d.ts +21 -0
  51. package/dist/connectors/action-registry-singleton.d.ts.map +1 -0
  52. package/dist/connectors/define-action.d.ts +117 -0
  53. package/dist/connectors/define-action.d.ts.map +1 -0
  54. package/dist/connectors/define-connector.d.ts +172 -0
  55. package/dist/connectors/define-connector.d.ts.map +1 -0
  56. package/dist/connectors/presets/index.d.ts +17 -0
  57. package/dist/connectors/presets/index.d.ts.map +1 -0
  58. package/dist/connectors/registry-singleton.d.ts +34 -0
  59. package/dist/connectors/registry-singleton.d.ts.map +1 -0
  60. package/dist/connectors/runtime.d.ts +68 -0
  61. package/dist/connectors/runtime.d.ts.map +1 -0
  62. package/dist/container.d.ts +678 -0
  63. package/dist/container.d.ts.map +1 -0
  64. package/dist/destinations/define-destination.d.ts +96 -0
  65. package/dist/destinations/define-destination.d.ts.map +1 -0
  66. package/dist/destinations/presets/index.d.ts +42 -0
  67. package/dist/destinations/presets/index.d.ts.map +1 -0
  68. package/dist/destinations/presets/posthog.d.ts +12 -0
  69. package/dist/destinations/presets/posthog.d.ts.map +1 -0
  70. package/dist/destinations/presets/segment.d.ts +15 -0
  71. package/dist/destinations/presets/segment.d.ts.map +1 -0
  72. package/dist/destinations/presets/slack.d.ts +10 -0
  73. package/dist/destinations/presets/slack.d.ts.map +1 -0
  74. package/dist/destinations/presets/webhook.d.ts +14 -0
  75. package/dist/destinations/presets/webhook.d.ts.map +1 -0
  76. package/dist/destinations/registry-singleton.d.ts +44 -0
  77. package/dist/destinations/registry-singleton.d.ts.map +1 -0
  78. package/dist/env.d.ts +119 -0
  79. package/dist/env.d.ts.map +1 -0
  80. package/dist/flags/reconcile.d.ts +36 -0
  81. package/dist/flags/reconcile.d.ts.map +1 -0
  82. package/dist/index.d.ts +210 -0
  83. package/dist/index.d.ts.map +1 -0
  84. package/dist/journeys/authoring.d.ts +15 -0
  85. package/dist/journeys/authoring.d.ts.map +1 -0
  86. package/dist/journeys/client-defaults-singleton.d.ts +16 -0
  87. package/dist/journeys/client-defaults-singleton.d.ts.map +1 -0
  88. package/dist/journeys/constants.d.ts +32 -0
  89. package/dist/journeys/constants.d.ts.map +1 -0
  90. package/dist/journeys/define-journey.d.ts +33 -0
  91. package/dist/journeys/define-journey.d.ts.map +1 -0
  92. package/dist/journeys/durable-law-harness.d.ts +74 -0
  93. package/dist/journeys/durable-law-harness.d.ts.map +1 -0
  94. package/dist/journeys/errors.d.ts +13 -0
  95. package/dist/journeys/errors.d.ts.map +1 -0
  96. package/dist/journeys/execute-journey-run.d.ts +160 -0
  97. package/dist/journeys/execute-journey-run.d.ts.map +1 -0
  98. package/dist/journeys/graph/build-graph.d.ts +24 -0
  99. package/dist/journeys/graph/build-graph.d.ts.map +1 -0
  100. package/dist/journeys/group-scope.d.ts +56 -0
  101. package/dist/journeys/group-scope.d.ts.map +1 -0
  102. package/dist/journeys/journey-boundary.d.ts +264 -0
  103. package/dist/journeys/journey-boundary.d.ts.map +1 -0
  104. package/dist/journeys/journey-context.d.ts +60 -0
  105. package/dist/journeys/journey-context.d.ts.map +1 -0
  106. package/dist/journeys/journey-log.d.ts +42 -0
  107. package/dist/journeys/journey-log.d.ts.map +1 -0
  108. package/dist/journeys/journey-source-locations-singleton.d.ts +6 -0
  109. package/dist/journeys/journey-source-locations-singleton.d.ts.map +1 -0
  110. package/dist/journeys/journey-sources-singleton.d.ts +5 -0
  111. package/dist/journeys/journey-sources-singleton.d.ts.map +1 -0
  112. package/dist/journeys/journey-task-factory.d.ts +11 -0
  113. package/dist/journeys/journey-task-factory.d.ts.map +1 -0
  114. package/dist/journeys/journey-task-runtime.d.ts +2 -0
  115. package/dist/journeys/journey-task-runtime.d.ts.map +1 -0
  116. package/dist/journeys/journey-version.d.ts +71 -0
  117. package/dist/journeys/journey-version.d.ts.map +1 -0
  118. package/dist/journeys/record-once.d.ts +56 -0
  119. package/dist/journeys/record-once.d.ts.map +1 -0
  120. package/dist/journeys/registry-singleton.d.ts +6 -0
  121. package/dist/journeys/registry-singleton.d.ts.map +1 -0
  122. package/dist/journeys/registry.d.ts +59 -0
  123. package/dist/journeys/registry.d.ts.map +1 -0
  124. package/dist/journeys/with-durable-gate.d.ts +97 -0
  125. package/dist/journeys/with-durable-gate.d.ts.map +1 -0
  126. package/dist/lib/account-link-emit.d.ts +69 -0
  127. package/dist/lib/account-link-emit.d.ts.map +1 -0
  128. package/dist/lib/account-link-events.d.ts +128 -0
  129. package/dist/lib/account-link-events.d.ts.map +1 -0
  130. package/dist/lib/account-link-hooks.d.ts +32 -0
  131. package/dist/lib/account-link-hooks.d.ts.map +1 -0
  132. package/dist/lib/account-link-ingest.d.ts +78 -0
  133. package/dist/lib/account-link-ingest.d.ts.map +1 -0
  134. package/dist/lib/account-link-nonce.d.ts +29 -0
  135. package/dist/lib/account-link-nonce.d.ts.map +1 -0
  136. package/dist/lib/account-link-origins.d.ts +37 -0
  137. package/dist/lib/account-link-origins.d.ts.map +1 -0
  138. package/dist/lib/account-link-pkce.d.ts +48 -0
  139. package/dist/lib/account-link-pkce.d.ts.map +1 -0
  140. package/dist/lib/account-link-provider-registry.d.ts +35 -0
  141. package/dist/lib/account-link-provider-registry.d.ts.map +1 -0
  142. package/dist/lib/account-link-throttle.d.ts +76 -0
  143. package/dist/lib/account-link-throttle.d.ts.map +1 -0
  144. package/dist/lib/account-link-url.d.ts +41 -0
  145. package/dist/lib/account-link-url.d.ts.map +1 -0
  146. package/dist/lib/account-links-from-env.d.ts +62 -0
  147. package/dist/lib/account-links-from-env.d.ts.map +1 -0
  148. package/dist/lib/account-links.d.ts +353 -0
  149. package/dist/lib/account-links.d.ts.map +1 -0
  150. package/dist/lib/agent/config.d.ts +14 -0
  151. package/dist/lib/agent/config.d.ts.map +1 -0
  152. package/dist/lib/agent/proposals.d.ts +52 -0
  153. package/dist/lib/agent/proposals.d.ts.map +1 -0
  154. package/dist/lib/agent/provider.d.ts +9 -0
  155. package/dist/lib/agent/provider.d.ts.map +1 -0
  156. package/dist/lib/agent/risk.d.ts +24 -0
  157. package/dist/lib/agent/risk.d.ts.map +1 -0
  158. package/dist/lib/agent/system-prompt.d.ts +8 -0
  159. package/dist/lib/agent/system-prompt.d.ts.map +1 -0
  160. package/dist/lib/agent/tools.d.ts +258 -0
  161. package/dist/lib/agent/tools.d.ts.map +1 -0
  162. package/dist/lib/alerting.d.ts +7 -0
  163. package/dist/lib/alerting.d.ts.map +1 -0
  164. package/dist/lib/analytics-adapter.d.ts +24 -0
  165. package/dist/lib/analytics-adapter.d.ts.map +1 -0
  166. package/dist/lib/analytics-identity.d.ts +60 -0
  167. package/dist/lib/analytics-identity.d.ts.map +1 -0
  168. package/dist/lib/analytics-provider-registry.d.ts +20 -0
  169. package/dist/lib/analytics-provider-registry.d.ts.map +1 -0
  170. package/dist/lib/analytics-providers-from-env.d.ts +41 -0
  171. package/dist/lib/analytics-providers-from-env.d.ts.map +1 -0
  172. package/dist/lib/analytics-singleton.d.ts +10 -0
  173. package/dist/lib/analytics-singleton.d.ts.map +1 -0
  174. package/dist/lib/api-key-hash.d.ts +9 -0
  175. package/dist/lib/api-key-hash.d.ts.map +1 -0
  176. package/dist/lib/app-secret.d.ts +29 -0
  177. package/dist/lib/app-secret.d.ts.map +1 -0
  178. package/dist/lib/attachments.d.ts +53 -0
  179. package/dist/lib/attachments.d.ts.map +1 -0
  180. package/dist/lib/attribution-backfill.d.ts +48 -0
  181. package/dist/lib/attribution-backfill.d.ts.map +1 -0
  182. package/dist/lib/attribution.d.ts +44 -0
  183. package/dist/lib/attribution.d.ts.map +1 -0
  184. package/dist/lib/auth.d.ts +136 -0
  185. package/dist/lib/auth.d.ts.map +1 -0
  186. package/dist/lib/backfill.d.ts +41 -0
  187. package/dist/lib/backfill.d.ts.map +1 -0
  188. package/dist/lib/banner.d.ts +52 -0
  189. package/dist/lib/banner.d.ts.map +1 -0
  190. package/dist/lib/blueprint-lock.d.ts +19 -0
  191. package/dist/lib/blueprint-lock.d.ts.map +1 -0
  192. package/dist/lib/blueprints.d.ts +573 -0
  193. package/dist/lib/blueprints.d.ts.map +1 -0
  194. package/dist/lib/boot-api-key.d.ts +38 -0
  195. package/dist/lib/boot-api-key.d.ts.map +1 -0
  196. package/dist/lib/boot.d.ts +23 -0
  197. package/dist/lib/boot.d.ts.map +1 -0
  198. package/dist/lib/bootstrap-admin.d.ts +33 -0
  199. package/dist/lib/bootstrap-admin.d.ts.map +1 -0
  200. package/dist/lib/bot-prefetch.d.ts +21 -0
  201. package/dist/lib/bot-prefetch.d.ts.map +1 -0
  202. package/dist/lib/bucket-emit.d.ts +75 -0
  203. package/dist/lib/bucket-emit.d.ts.map +1 -0
  204. package/dist/lib/bucket-posthog-sync.d.ts +29 -0
  205. package/dist/lib/bucket-posthog-sync.d.ts.map +1 -0
  206. package/dist/lib/campaign-send-key.d.ts +31 -0
  207. package/dist/lib/campaign-send-key.d.ts.map +1 -0
  208. package/dist/lib/connector-actions.d.ts +83 -0
  209. package/dist/lib/connector-actions.d.ts.map +1 -0
  210. package/dist/lib/connector-heartbeat.d.ts +35 -0
  211. package/dist/lib/connector-heartbeat.d.ts.map +1 -0
  212. package/dist/lib/connector-link-codes.d.ts +105 -0
  213. package/dist/lib/connector-link-codes.d.ts.map +1 -0
  214. package/dist/lib/connector-state.d.ts +97 -0
  215. package/dist/lib/connector-state.d.ts.map +1 -0
  216. package/dist/lib/contacts.d.ts +622 -0
  217. package/dist/lib/contacts.d.ts.map +1 -0
  218. package/dist/lib/conversion-dispatch.d.ts +98 -0
  219. package/dist/lib/conversion-dispatch.d.ts.map +1 -0
  220. package/dist/lib/conversions.d.ts +75 -0
  221. package/dist/lib/conversions.d.ts.map +1 -0
  222. package/dist/lib/create-admin.d.ts +54 -0
  223. package/dist/lib/create-admin.d.ts.map +1 -0
  224. package/dist/lib/crm-deals.d.ts +66 -0
  225. package/dist/lib/crm-deals.d.ts.map +1 -0
  226. package/dist/lib/crm-ingest.d.ts +38 -0
  227. package/dist/lib/crm-ingest.d.ts.map +1 -0
  228. package/dist/lib/crm-provider-registry.d.ts +20 -0
  229. package/dist/lib/crm-provider-registry.d.ts.map +1 -0
  230. package/dist/lib/crm-registry-singleton.d.ts +18 -0
  231. package/dist/lib/crm-registry-singleton.d.ts.map +1 -0
  232. package/dist/lib/db.d.ts +3 -0
  233. package/dist/lib/db.d.ts.map +1 -0
  234. package/dist/lib/deal-money-events.d.ts +35 -0
  235. package/dist/lib/deal-money-events.d.ts.map +1 -0
  236. package/dist/lib/discord-gateway-heartbeat.d.ts +47 -0
  237. package/dist/lib/discord-gateway-heartbeat.d.ts.map +1 -0
  238. package/dist/lib/domain-status.d.ts +87 -0
  239. package/dist/lib/domain-status.d.ts.map +1 -0
  240. package/dist/lib/email-provider-registry.d.ts +30 -0
  241. package/dist/lib/email-provider-registry.d.ts.map +1 -0
  242. package/dist/lib/email-providers-from-env.d.ts +12 -0
  243. package/dist/lib/email-providers-from-env.d.ts.map +1 -0
  244. package/dist/lib/email-service-types.d.ts +189 -0
  245. package/dist/lib/email-service-types.d.ts.map +1 -0
  246. package/dist/lib/email-stats.d.ts +12 -0
  247. package/dist/lib/email-stats.d.ts.map +1 -0
  248. package/dist/lib/email.d.ts +73 -0
  249. package/dist/lib/email.d.ts.map +1 -0
  250. package/dist/lib/enrichment-ledger.d.ts +101 -0
  251. package/dist/lib/enrichment-ledger.d.ts.map +1 -0
  252. package/dist/lib/enrichment-provider-registry.d.ts +20 -0
  253. package/dist/lib/enrichment-provider-registry.d.ts.map +1 -0
  254. package/dist/lib/enrichment-providers-from-env.d.ts +13 -0
  255. package/dist/lib/enrichment-providers-from-env.d.ts.map +1 -0
  256. package/dist/lib/enrichment-registry-singleton.d.ts +9 -0
  257. package/dist/lib/enrichment-registry-singleton.d.ts.map +1 -0
  258. package/dist/lib/enrollment-guards.d.ts +28 -0
  259. package/dist/lib/enrollment-guards.d.ts.map +1 -0
  260. package/dist/lib/enrollment-policy.d.ts +25 -0
  261. package/dist/lib/enrollment-policy.d.ts.map +1 -0
  262. package/dist/lib/event-names.d.ts +27 -0
  263. package/dist/lib/event-names.d.ts.map +1 -0
  264. package/dist/lib/feed.d.ts +64 -0
  265. package/dist/lib/feed.d.ts.map +1 -0
  266. package/dist/lib/flags-registry.d.ts +23 -0
  267. package/dist/lib/flags-registry.d.ts.map +1 -0
  268. package/dist/lib/flags.d.ts +143 -0
  269. package/dist/lib/flags.d.ts.map +1 -0
  270. package/dist/lib/frequency-cap.d.ts +38 -0
  271. package/dist/lib/frequency-cap.d.ts.map +1 -0
  272. package/dist/lib/from-address.d.ts +15 -0
  273. package/dist/lib/from-address.d.ts.map +1 -0
  274. package/dist/lib/funnel-progress.d.ts +37 -0
  275. package/dist/lib/funnel-progress.d.ts.map +1 -0
  276. package/dist/lib/funnel-registry.d.ts +44 -0
  277. package/dist/lib/funnel-registry.d.ts.map +1 -0
  278. package/dist/lib/funnel-transitions.d.ts +63 -0
  279. package/dist/lib/funnel-transitions.d.ts.map +1 -0
  280. package/dist/lib/fx.d.ts +118 -0
  281. package/dist/lib/fx.d.ts.map +1 -0
  282. package/dist/lib/global-control-readout.d.ts +72 -0
  283. package/dist/lib/global-control-readout.d.ts.map +1 -0
  284. package/dist/lib/groups.d.ts +138 -0
  285. package/dist/lib/groups.d.ts.map +1 -0
  286. package/dist/lib/hatchet-duration.d.ts +14 -0
  287. package/dist/lib/hatchet-duration.d.ts.map +1 -0
  288. package/dist/lib/hatchet.d.ts +3 -0
  289. package/dist/lib/hatchet.d.ts.map +1 -0
  290. package/dist/lib/headers.d.ts +8 -0
  291. package/dist/lib/headers.d.ts.map +1 -0
  292. package/dist/lib/holdout.d.ts +36 -0
  293. package/dist/lib/holdout.d.ts.map +1 -0
  294. package/dist/lib/html.d.ts +5 -0
  295. package/dist/lib/html.d.ts.map +1 -0
  296. package/dist/lib/identity-service.d.ts +60 -0
  297. package/dist/lib/identity-service.d.ts.map +1 -0
  298. package/dist/lib/identity-token.d.ts +81 -0
  299. package/dist/lib/identity-token.d.ts.map +1 -0
  300. package/dist/lib/ingestion.d.ts +218 -0
  301. package/dist/lib/ingestion.d.ts.map +1 -0
  302. package/dist/lib/journey-lift.d.ts +56 -0
  303. package/dist/lib/journey-lift.d.ts.map +1 -0
  304. package/dist/lib/journey-suppress.d.ts +59 -0
  305. package/dist/lib/journey-suppress.d.ts.map +1 -0
  306. package/dist/lib/leader-lease.d.ts +36 -0
  307. package/dist/lib/leader-lease.d.ts.map +1 -0
  308. package/dist/lib/lift-stats.d.ts +46 -0
  309. package/dist/lib/lift-stats.d.ts.map +1 -0
  310. package/dist/lib/links.d.ts +186 -0
  311. package/dist/lib/links.d.ts.map +1 -0
  312. package/dist/lib/load-optional-plugin.d.ts +47 -0
  313. package/dist/lib/load-optional-plugin.d.ts.map +1 -0
  314. package/dist/lib/logger.d.ts +4 -0
  315. package/dist/lib/logger.d.ts.map +1 -0
  316. package/dist/lib/mailer.d.ts +43 -0
  317. package/dist/lib/mailer.d.ts.map +1 -0
  318. package/dist/lib/metrics-sql.d.ts +11 -0
  319. package/dist/lib/metrics-sql.d.ts.map +1 -0
  320. package/dist/lib/notifications.d.ts +17 -0
  321. package/dist/lib/notifications.d.ts.map +1 -0
  322. package/dist/lib/oauth-token-manager.d.ts +74 -0
  323. package/dist/lib/oauth-token-manager.d.ts.map +1 -0
  324. package/dist/lib/operator-settings.d.ts +33 -0
  325. package/dist/lib/operator-settings.d.ts.map +1 -0
  326. package/dist/lib/operator.d.ts +20 -0
  327. package/dist/lib/operator.d.ts.map +1 -0
  328. package/dist/lib/outbound-endpoint-cache.d.ts +25 -0
  329. package/dist/lib/outbound-endpoint-cache.d.ts.map +1 -0
  330. package/dist/lib/outbound.d.ts +687 -0
  331. package/dist/lib/outbound.d.ts.map +1 -0
  332. package/dist/lib/phone.d.ts +11 -0
  333. package/dist/lib/phone.d.ts.map +1 -0
  334. package/dist/lib/posthog-scopes.d.ts +30 -0
  335. package/dist/lib/posthog-scopes.d.ts.map +1 -0
  336. package/dist/lib/posthog.d.ts +16 -0
  337. package/dist/lib/posthog.d.ts.map +1 -0
  338. package/dist/lib/preferences.d.ts +72 -0
  339. package/dist/lib/preferences.d.ts.map +1 -0
  340. package/dist/lib/provider-credentials.d.ts +149 -0
  341. package/dist/lib/provider-credentials.d.ts.map +1 -0
  342. package/dist/lib/provision-posthog-loop.d.ts +100 -0
  343. package/dist/lib/provision-posthog-loop.d.ts.map +1 -0
  344. package/dist/lib/recipient-preferences.d.ts +29 -0
  345. package/dist/lib/recipient-preferences.d.ts.map +1 -0
  346. package/dist/lib/redis.d.ts +33 -0
  347. package/dist/lib/redis.d.ts.map +1 -0
  348. package/dist/lib/referral-intent.d.ts +99 -0
  349. package/dist/lib/referral-intent.d.ts.map +1 -0
  350. package/dist/lib/referral-link.d.ts +59 -0
  351. package/dist/lib/referral-link.d.ts.map +1 -0
  352. package/dist/lib/referral-registry.d.ts +37 -0
  353. package/dist/lib/referral-registry.d.ts.map +1 -0
  354. package/dist/lib/referral-report.d.ts +178 -0
  355. package/dist/lib/referral-report.d.ts.map +1 -0
  356. package/dist/lib/referral-runtime.d.ts +43 -0
  357. package/dist/lib/referral-runtime.d.ts.map +1 -0
  358. package/dist/lib/referrals.d.ts +184 -0
  359. package/dist/lib/referrals.d.ts.map +1 -0
  360. package/dist/lib/refine-chain.d.ts +236 -0
  361. package/dist/lib/refine-chain.d.ts.map +1 -0
  362. package/dist/lib/refine-traits.d.ts +78 -0
  363. package/dist/lib/refine-traits.d.ts.map +1 -0
  364. package/dist/lib/refine.d.ts +35 -0
  365. package/dist/lib/refine.d.ts.map +1 -0
  366. package/dist/lib/reset-email.d.ts +38 -0
  367. package/dist/lib/reset-email.d.ts.map +1 -0
  368. package/dist/lib/revenue.d.ts +61 -0
  369. package/dist/lib/revenue.d.ts.map +1 -0
  370. package/dist/lib/schemas.d.ts +9 -0
  371. package/dist/lib/schemas.d.ts.map +1 -0
  372. package/dist/lib/seed-posthog-destination.d.ts +22 -0
  373. package/dist/lib/seed-posthog-destination.d.ts.map +1 -0
  374. package/dist/lib/semantic-click.d.ts +58 -0
  375. package/dist/lib/semantic-click.d.ts.map +1 -0
  376. package/dist/lib/sending-domain-guidance.d.ts +40 -0
  377. package/dist/lib/sending-domain-guidance.d.ts.map +1 -0
  378. package/dist/lib/singleton.d.ts +24 -0
  379. package/dist/lib/singleton.d.ts.map +1 -0
  380. package/dist/lib/sms-frequency-cap.d.ts +16 -0
  381. package/dist/lib/sms-frequency-cap.d.ts.map +1 -0
  382. package/dist/lib/sms-inbound.d.ts +37 -0
  383. package/dist/lib/sms-inbound.d.ts.map +1 -0
  384. package/dist/lib/sms-link-tracking.d.ts +53 -0
  385. package/dist/lib/sms-link-tracking.d.ts.map +1 -0
  386. package/dist/lib/sms-mailer.d.ts +12 -0
  387. package/dist/lib/sms-mailer.d.ts.map +1 -0
  388. package/dist/lib/sms-provider-registry.d.ts +20 -0
  389. package/dist/lib/sms-provider-registry.d.ts.map +1 -0
  390. package/dist/lib/sms-providers-from-env.d.ts +4 -0
  391. package/dist/lib/sms-providers-from-env.d.ts.map +1 -0
  392. package/dist/lib/sms-service-types.d.ts +131 -0
  393. package/dist/lib/sms-service-types.d.ts.map +1 -0
  394. package/dist/lib/sms-tracked.d.ts +36 -0
  395. package/dist/lib/sms-tracked.d.ts.map +1 -0
  396. package/dist/lib/sms.d.ts +58 -0
  397. package/dist/lib/sms.d.ts.map +1 -0
  398. package/dist/lib/snippet.d.ts +32 -0
  399. package/dist/lib/snippet.d.ts.map +1 -0
  400. package/dist/lib/stable-stringify.d.ts +12 -0
  401. package/dist/lib/stable-stringify.d.ts.map +1 -0
  402. package/dist/lib/studio.d.ts +21 -0
  403. package/dist/lib/studio.d.ts.map +1 -0
  404. package/dist/lib/survey.d.ts +35 -0
  405. package/dist/lib/survey.d.ts.map +1 -0
  406. package/dist/lib/test-mode.d.ts +77 -0
  407. package/dist/lib/test-mode.d.ts.map +1 -0
  408. package/dist/lib/timezone.d.ts +55 -0
  409. package/dist/lib/timezone.d.ts.map +1 -0
  410. package/dist/lib/tracked.d.ts +95 -0
  411. package/dist/lib/tracked.d.ts.map +1 -0
  412. package/dist/lib/tracking-event-names.d.ts +7 -0
  413. package/dist/lib/tracking-event-names.d.ts.map +1 -0
  414. package/dist/lib/tracking-events.d.ts +232 -0
  415. package/dist/lib/tracking-events.d.ts.map +1 -0
  416. package/dist/lib/tracking.d.ts +47 -0
  417. package/dist/lib/tracking.d.ts.map +1 -0
  418. package/dist/lib/unconfigured-email-provider.d.ts +20 -0
  419. package/dist/lib/unconfigured-email-provider.d.ts.map +1 -0
  420. package/dist/lib/unique-violation.d.ts +22 -0
  421. package/dist/lib/unique-violation.d.ts.map +1 -0
  422. package/dist/lib/user-token.d.ts +44 -0
  423. package/dist/lib/user-token.d.ts.map +1 -0
  424. package/dist/lib/variant.d.ts +44 -0
  425. package/dist/lib/variant.d.ts.map +1 -0
  426. package/dist/lib/webhook-signing.d.ts +120 -0
  427. package/dist/lib/webhook-signing.d.ts.map +1 -0
  428. package/dist/lib/worker-heartbeat.d.ts +18 -0
  429. package/dist/lib/worker-heartbeat.d.ts.map +1 -0
  430. package/dist/lists/channels.d.ts +43 -0
  431. package/dist/lists/channels.d.ts.map +1 -0
  432. package/dist/lists/define-list.d.ts +76 -0
  433. package/dist/lists/define-list.d.ts.map +1 -0
  434. package/dist/lists/registry-singleton.d.ts +8 -0
  435. package/dist/lists/registry-singleton.d.ts.map +1 -0
  436. package/dist/lists/registry.d.ts +66 -0
  437. package/dist/lists/registry.d.ts.map +1 -0
  438. package/dist/lists/subscription.d.ts +10 -0
  439. package/dist/lists/subscription.d.ts.map +1 -0
  440. package/dist/mcp/authoring-guide.d.ts +33 -0
  441. package/dist/mcp/authoring-guide.d.ts.map +1 -0
  442. package/dist/mcp/blueprint-tools.d.ts +266 -0
  443. package/dist/mcp/blueprint-tools.d.ts.map +1 -0
  444. package/dist/middleware/api-key.d.ts +45 -0
  445. package/dist/middleware/api-key.d.ts.map +1 -0
  446. package/dist/middleware/audit.d.ts +3 -0
  447. package/dist/middleware/audit.d.ts.map +1 -0
  448. package/dist/middleware/error-handler.d.ts +4 -0
  449. package/dist/middleware/error-handler.d.ts.map +1 -0
  450. package/dist/middleware/publishable-key.d.ts +27 -0
  451. package/dist/middleware/publishable-key.d.ts.map +1 -0
  452. package/dist/middleware/rate-limit.d.ts +105 -0
  453. package/dist/middleware/rate-limit.d.ts.map +1 -0
  454. package/dist/middleware/request-logger.d.ts +4 -0
  455. package/dist/middleware/request-logger.d.ts.map +1 -0
  456. package/dist/middleware/require-admin.d.ts +3 -0
  457. package/dist/middleware/require-admin.d.ts.map +1 -0
  458. package/dist/routes/_shared.d.ts +52 -0
  459. package/dist/routes/_shared.d.ts.map +1 -0
  460. package/dist/routes/accounts/callback.d.ts +18 -0
  461. package/dist/routes/accounts/callback.d.ts.map +1 -0
  462. package/dist/routes/accounts/emit.d.ts +96 -0
  463. package/dist/routes/accounts/emit.d.ts.map +1 -0
  464. package/dist/routes/accounts/import-links.d.ts +4 -0
  465. package/dist/routes/accounts/import-links.d.ts.map +1 -0
  466. package/dist/routes/accounts/index.d.ts +39 -0
  467. package/dist/routes/accounts/index.d.ts.map +1 -0
  468. package/dist/routes/accounts/lookup.d.ts +6 -0
  469. package/dist/routes/accounts/lookup.d.ts.map +1 -0
  470. package/dist/routes/accounts/me.d.ts +5 -0
  471. package/dist/routes/accounts/me.d.ts.map +1 -0
  472. package/dist/routes/accounts/mint.d.ts +5 -0
  473. package/dist/routes/accounts/mint.d.ts.map +1 -0
  474. package/dist/routes/accounts/pages.d.ts +23 -0
  475. package/dist/routes/accounts/pages.d.ts.map +1 -0
  476. package/dist/routes/accounts/resolve.d.ts +33 -0
  477. package/dist/routes/accounts/resolve.d.ts.map +1 -0
  478. package/dist/routes/accounts/serialize.d.ts +57 -0
  479. package/dist/routes/accounts/serialize.d.ts.map +1 -0
  480. package/dist/routes/accounts/shared.d.ts +68 -0
  481. package/dist/routes/accounts/shared.d.ts.map +1 -0
  482. package/dist/routes/accounts/start.d.ts +16 -0
  483. package/dist/routes/accounts/start.d.ts.map +1 -0
  484. package/dist/routes/accounts/unlink.d.ts +36 -0
  485. package/dist/routes/accounts/unlink.d.ts.map +1 -0
  486. package/dist/routes/admin/agent.d.ts +4 -0
  487. package/dist/routes/admin/agent.d.ts.map +1 -0
  488. package/dist/routes/admin/alerts.d.ts +190 -0
  489. package/dist/routes/admin/alerts.d.ts.map +1 -0
  490. package/dist/routes/admin/analytics.d.ts +137 -0
  491. package/dist/routes/admin/analytics.d.ts.map +1 -0
  492. package/dist/routes/admin/api-keys.d.ts +158 -0
  493. package/dist/routes/admin/api-keys.d.ts.map +1 -0
  494. package/dist/routes/admin/attribution.d.ts +93 -0
  495. package/dist/routes/admin/attribution.d.ts.map +1 -0
  496. package/dist/routes/admin/audit-logs.d.ts +40 -0
  497. package/dist/routes/admin/audit-logs.d.ts.map +1 -0
  498. package/dist/routes/admin/blueprints.d.ts +4 -0
  499. package/dist/routes/admin/blueprints.d.ts.map +1 -0
  500. package/dist/routes/admin/buckets.d.ts +215 -0
  501. package/dist/routes/admin/buckets.d.ts.map +1 -0
  502. package/dist/routes/admin/bulk.d.ts +203 -0
  503. package/dist/routes/admin/bulk.d.ts.map +1 -0
  504. package/dist/routes/admin/campaigns.d.ts +198 -0
  505. package/dist/routes/admin/campaigns.d.ts.map +1 -0
  506. package/dist/routes/admin/connectors.d.ts +116 -0
  507. package/dist/routes/admin/connectors.d.ts.map +1 -0
  508. package/dist/routes/admin/contacts.d.ts +246 -0
  509. package/dist/routes/admin/contacts.d.ts.map +1 -0
  510. package/dist/routes/admin/conversions.d.ts +103 -0
  511. package/dist/routes/admin/conversions.d.ts.map +1 -0
  512. package/dist/routes/admin/deals.d.ts +136 -0
  513. package/dist/routes/admin/deals.d.ts.map +1 -0
  514. package/dist/routes/admin/dlq.d.ts +100 -0
  515. package/dist/routes/admin/dlq.d.ts.map +1 -0
  516. package/dist/routes/admin/domain.d.ts +314 -0
  517. package/dist/routes/admin/domain.d.ts.map +1 -0
  518. package/dist/routes/admin/emails.d.ts +127 -0
  519. package/dist/routes/admin/emails.d.ts.map +1 -0
  520. package/dist/routes/admin/events.d.ts +140 -0
  521. package/dist/routes/admin/events.d.ts.map +1 -0
  522. package/dist/routes/admin/flags.d.ts +719 -0
  523. package/dist/routes/admin/flags.d.ts.map +1 -0
  524. package/dist/routes/admin/funnels.d.ts +71 -0
  525. package/dist/routes/admin/funnels.d.ts.map +1 -0
  526. package/dist/routes/admin/groups.d.ts +169 -0
  527. package/dist/routes/admin/groups.d.ts.map +1 -0
  528. package/dist/routes/admin/identity.d.ts +72 -0
  529. package/dist/routes/admin/identity.d.ts.map +1 -0
  530. package/dist/routes/admin/impact-schemas.d.ts +48 -0
  531. package/dist/routes/admin/impact-schemas.d.ts.map +1 -0
  532. package/dist/routes/admin/impact.d.ts +316 -0
  533. package/dist/routes/admin/impact.d.ts.map +1 -0
  534. package/dist/routes/admin/index.d.ts +4 -0
  535. package/dist/routes/admin/index.d.ts.map +1 -0
  536. package/dist/routes/admin/journey-impact.d.ts +320 -0
  537. package/dist/routes/admin/journey-impact.d.ts.map +1 -0
  538. package/dist/routes/admin/journey-logs.d.ts +57 -0
  539. package/dist/routes/admin/journey-logs.d.ts.map +1 -0
  540. package/dist/routes/admin/journeys.d.ts +736 -0
  541. package/dist/routes/admin/journeys.d.ts.map +1 -0
  542. package/dist/routes/admin/links.d.ts +382 -0
  543. package/dist/routes/admin/links.d.ts.map +1 -0
  544. package/dist/routes/admin/lists.d.ts +22 -0
  545. package/dist/routes/admin/lists.d.ts.map +1 -0
  546. package/dist/routes/admin/maintenance.d.ts +72 -0
  547. package/dist/routes/admin/maintenance.d.ts.map +1 -0
  548. package/dist/routes/admin/metrics.d.ts +212 -0
  549. package/dist/routes/admin/metrics.d.ts.map +1 -0
  550. package/dist/routes/admin/open-editor.d.ts +36 -0
  551. package/dist/routes/admin/open-editor.d.ts.map +1 -0
  552. package/dist/routes/admin/preferences.d.ts +109 -0
  553. package/dist/routes/admin/preferences.d.ts.map +1 -0
  554. package/dist/routes/admin/provider-credentials.d.ts +108 -0
  555. package/dist/routes/admin/provider-credentials.d.ts.map +1 -0
  556. package/dist/routes/admin/readiness.d.ts +24 -0
  557. package/dist/routes/admin/readiness.d.ts.map +1 -0
  558. package/dist/routes/admin/referrals.d.ts +230 -0
  559. package/dist/routes/admin/referrals.d.ts.map +1 -0
  560. package/dist/routes/admin/reporting.d.ts +149 -0
  561. package/dist/routes/admin/reporting.d.ts.map +1 -0
  562. package/dist/routes/admin/settings.d.ts +80 -0
  563. package/dist/routes/admin/settings.d.ts.map +1 -0
  564. package/dist/routes/admin/suppressions.d.ts +90 -0
  565. package/dist/routes/admin/suppressions.d.ts.map +1 -0
  566. package/dist/routes/admin/targeting.d.ts +58 -0
  567. package/dist/routes/admin/targeting.d.ts.map +1 -0
  568. package/dist/routes/admin/templates.d.ts +197 -0
  569. package/dist/routes/admin/templates.d.ts.map +1 -0
  570. package/dist/routes/admin/timeline.d.ts +51 -0
  571. package/dist/routes/admin/timeline.d.ts.map +1 -0
  572. package/dist/routes/admin/webhooks.d.ts +250 -0
  573. package/dist/routes/admin/webhooks.d.ts.map +1 -0
  574. package/dist/routes/campaigns/index.d.ts +239 -0
  575. package/dist/routes/campaigns/index.d.ts.map +1 -0
  576. package/dist/routes/connectors/index.d.ts +16 -0
  577. package/dist/routes/connectors/index.d.ts.map +1 -0
  578. package/dist/routes/contacts/index.d.ts +187 -0
  579. package/dist/routes/contacts/index.d.ts.map +1 -0
  580. package/dist/routes/email/index.d.ts +4 -0
  581. package/dist/routes/email/index.d.ts.map +1 -0
  582. package/dist/routes/email/preferences.d.ts +17 -0
  583. package/dist/routes/email/preferences.d.ts.map +1 -0
  584. package/dist/routes/email/unsubscribe.d.ts +17 -0
  585. package/dist/routes/email/unsubscribe.d.ts.map +1 -0
  586. package/dist/routes/emails/index.d.ts +90 -0
  587. package/dist/routes/emails/index.d.ts.map +1 -0
  588. package/dist/routes/events/index.d.ts +116 -0
  589. package/dist/routes/events/index.d.ts.map +1 -0
  590. package/dist/routes/feed/index.d.ts +248 -0
  591. package/dist/routes/feed/index.d.ts.map +1 -0
  592. package/dist/routes/feed/recipient.d.ts +65 -0
  593. package/dist/routes/feed/recipient.d.ts.map +1 -0
  594. package/dist/routes/flags/index.d.ts +94 -0
  595. package/dist/routes/flags/index.d.ts.map +1 -0
  596. package/dist/routes/groups/index.d.ts +192 -0
  597. package/dist/routes/groups/index.d.ts.map +1 -0
  598. package/dist/routes/health.d.ts +57 -0
  599. package/dist/routes/health.d.ts.map +1 -0
  600. package/dist/routes/index.d.ts +8 -0
  601. package/dist/routes/index.d.ts.map +1 -0
  602. package/dist/routes/lists/index.d.ts +266 -0
  603. package/dist/routes/lists/index.d.ts.map +1 -0
  604. package/dist/routes/referrals/index.d.ts +22 -0
  605. package/dist/routes/referrals/index.d.ts.map +1 -0
  606. package/dist/routes/referrals/me.d.ts +4 -0
  607. package/dist/routes/referrals/me.d.ts.map +1 -0
  608. package/dist/routes/referrals/report.d.ts +5 -0
  609. package/dist/routes/referrals/report.d.ts.map +1 -0
  610. package/dist/routes/referrals/shared.d.ts +16 -0
  611. package/dist/routes/referrals/shared.d.ts.map +1 -0
  612. package/dist/routes/referrals/touch.d.ts +5 -0
  613. package/dist/routes/referrals/touch.d.ts.map +1 -0
  614. package/dist/routes/snippet.d.ts +12 -0
  615. package/dist/routes/snippet.d.ts.map +1 -0
  616. package/dist/routes/tracking/answer.d.ts +56 -0
  617. package/dist/routes/tracking/answer.d.ts.map +1 -0
  618. package/dist/routes/tracking/arrive.d.ts +32 -0
  619. package/dist/routes/tracking/arrive.d.ts.map +1 -0
  620. package/dist/routes/tracking/click-pipeline.d.ts +213 -0
  621. package/dist/routes/tracking/click-pipeline.d.ts.map +1 -0
  622. package/dist/routes/tracking/click.d.ts +26 -0
  623. package/dist/routes/tracking/click.d.ts.map +1 -0
  624. package/dist/routes/tracking/identify.d.ts +32 -0
  625. package/dist/routes/tracking/identify.d.ts.map +1 -0
  626. package/dist/routes/tracking/index.d.ts +4 -0
  627. package/dist/routes/tracking/index.d.ts.map +1 -0
  628. package/dist/routes/tracking/open.d.ts +17 -0
  629. package/dist/routes/tracking/open.d.ts.map +1 -0
  630. package/dist/routes/tracking/short.d.ts +17 -0
  631. package/dist/routes/tracking/short.d.ts.map +1 -0
  632. package/dist/routes/tracking/vanity.d.ts +17 -0
  633. package/dist/routes/tracking/vanity.d.ts.map +1 -0
  634. package/dist/routes/webhooks/crm-provider.d.ts +24 -0
  635. package/dist/routes/webhooks/crm-provider.d.ts.map +1 -0
  636. package/dist/routes/webhooks/email-provider.d.ts +21 -0
  637. package/dist/routes/webhooks/email-provider.d.ts.map +1 -0
  638. package/dist/routes/webhooks/index.d.ts +8 -0
  639. package/dist/routes/webhooks/index.d.ts.map +1 -0
  640. package/dist/routes/webhooks/resend.d.ts +35 -0
  641. package/dist/routes/webhooks/resend.d.ts.map +1 -0
  642. package/dist/routes/webhooks/sms-provider.d.ts +23 -0
  643. package/dist/routes/webhooks/sms-provider.d.ts.map +1 -0
  644. package/dist/routes/webhooks/sources.d.ts +12 -0
  645. package/dist/routes/webhooks/sources.d.ts.map +1 -0
  646. package/dist/sources/define-contact-source.d.ts +114 -0
  647. package/dist/sources/define-contact-source.d.ts.map +1 -0
  648. package/dist/sources/registry.d.ts +31 -0
  649. package/dist/sources/registry.d.ts.map +1 -0
  650. package/dist/sources/webhook-source.d.ts +53 -0
  651. package/dist/sources/webhook-source.d.ts.map +1 -0
  652. package/dist/testing.d.ts +10 -0
  653. package/dist/testing.d.ts.map +1 -0
  654. package/dist/webhook-sources/define-webhook-source.d.ts +60 -0
  655. package/dist/webhook-sources/define-webhook-source.d.ts.map +1 -0
  656. package/dist/webhook-sources/presets/clerk.d.ts +9 -0
  657. package/dist/webhook-sources/presets/clerk.d.ts.map +1 -0
  658. package/dist/webhook-sources/presets/index.d.ts +140 -0
  659. package/dist/webhook-sources/presets/index.d.ts.map +1 -0
  660. package/dist/webhook-sources/presets/intercom.d.ts +44 -0
  661. package/dist/webhook-sources/presets/intercom.d.ts.map +1 -0
  662. package/dist/webhook-sources/presets/segment.d.ts +16 -0
  663. package/dist/webhook-sources/presets/segment.d.ts.map +1 -0
  664. package/dist/webhook-sources/presets/stripe.d.ts +20 -0
  665. package/dist/webhook-sources/presets/stripe.d.ts.map +1 -0
  666. package/dist/webhook-sources/presets/supabase.d.ts +23 -0
  667. package/dist/webhook-sources/presets/supabase.d.ts.map +1 -0
  668. package/dist/webhook-sources/verify.d.ts +48 -0
  669. package/dist/webhook-sources/verify.d.ts.map +1 -0
  670. package/dist/worker.d.ts +37 -0
  671. package/dist/worker.d.ts.map +1 -0
  672. package/dist/workflows/backfill-contact-id.d.ts +202 -0
  673. package/dist/workflows/backfill-contact-id.d.ts.map +1 -0
  674. package/dist/workflows/bucket-backfill.d.ts +64 -0
  675. package/dist/workflows/bucket-backfill.d.ts.map +1 -0
  676. package/dist/workflows/bucket-reconcile.d.ts +73 -0
  677. package/dist/workflows/bucket-reconcile.d.ts.map +1 -0
  678. package/dist/workflows/check-alerts.d.ts +10 -0
  679. package/dist/workflows/check-alerts.d.ts.map +1 -0
  680. package/dist/workflows/confirm-semantic-click.d.ts +15 -0
  681. package/dist/workflows/confirm-semantic-click.d.ts.map +1 -0
  682. package/dist/workflows/crm-reconcile.d.ts +27 -0
  683. package/dist/workflows/crm-reconcile.d.ts.map +1 -0
  684. package/dist/workflows/deliver-webhook.d.ts +88 -0
  685. package/dist/workflows/deliver-webhook.d.ts.map +1 -0
  686. package/dist/workflows/dispatch-conversion.d.ts +14 -0
  687. package/dist/workflows/dispatch-conversion.d.ts.map +1 -0
  688. package/dist/workflows/identity-alias-backfill.d.ts +96 -0
  689. package/dist/workflows/identity-alias-backfill.d.ts.map +1 -0
  690. package/dist/workflows/impact-digest.d.ts +142 -0
  691. package/dist/workflows/impact-digest.d.ts.map +1 -0
  692. package/dist/workflows/import-contacts.d.ts +14 -0
  693. package/dist/workflows/import-contacts.d.ts.map +1 -0
  694. package/dist/workflows/import-suppressions.d.ts +55 -0
  695. package/dist/workflows/import-suppressions.d.ts.map +1 -0
  696. package/dist/workflows/journey-blueprint-interpreter.d.ts +73 -0
  697. package/dist/workflows/journey-blueprint-interpreter.d.ts.map +1 -0
  698. package/dist/workflows/send-campaign.d.ts +128 -0
  699. package/dist/workflows/send-campaign.d.ts.map +1 -0
  700. package/dist/workflows/send-email.d.ts +15 -0
  701. package/dist/workflows/send-email.d.ts.map +1 -0
  702. package/dist/workflows/send-feed.d.ts +37 -0
  703. package/dist/workflows/send-feed.d.ts.map +1 -0
  704. package/package.json +60 -22
  705. package/src/app.ts +9 -1
  706. package/src/buckets/check-membership.ts +54 -20
  707. package/src/env.ts +30 -0
  708. package/src/journeys/journey-log.ts +25 -8
  709. package/src/lib/contacts.ts +47 -1
  710. package/src/lib/email-service-types.ts +10 -0
  711. package/src/lib/ingestion.ts +30 -19
  712. package/src/lib/mailer-bounce-idempotency.test.ts +7 -2
  713. package/src/lib/mailer-replied.test.ts +7 -2
  714. package/src/lib/mailer.ts +1 -0
  715. package/src/lib/outbound-endpoint-cache.ts +83 -0
  716. package/src/lib/outbound.ts +56 -37
  717. package/src/lib/seed-posthog-destination.ts +5 -1
  718. package/src/lib/tracked.ts +11 -3
  719. package/src/lib/tracking-events.ts +20 -3
  720. package/src/middleware/api-key.ts +69 -60
  721. package/src/middleware/publishable-key.ts +11 -23
  722. package/src/middleware/rate-limit.ts +165 -3
  723. package/src/routes/admin/api-keys.ts +148 -0
  724. package/src/routes/admin/targeting.ts +6 -1
  725. package/src/routes/admin/webhooks.ts +4 -0
  726. package/src/routes/index.ts +67 -24
  727. package/src/workflows/backfill-contact-id.ts +3 -7
  728. package/src/workflows/bucket-backfill.ts +2 -2
  729. package/src/workflows/bucket-reconcile.ts +28 -12
  730. package/src/workflows/check-alerts.ts +3 -9
  731. package/src/workflows/crm-reconcile.ts +3 -2
  732. package/src/workflows/deliver-webhook.ts +21 -8
  733. package/src/workflows/dispatch-conversion.ts +2 -2
  734. package/src/workflows/identity-alias-backfill.ts +3 -2
  735. package/src/workflows/impact-digest.ts +2 -2
  736. package/src/workflows/import-contacts.ts +3 -4
  737. package/src/workflows/import-suppressions.ts +4 -5
  738. package/src/workflows/send-campaign.ts +10 -1
@@ -1,4 +1,4 @@
1
- import { apiKeys } from "@hogsend/db";
1
+ import { apiKeys, type Database } from "@hogsend/db";
2
2
  import { and, eq, isNull } from "drizzle-orm";
3
3
  import { createMiddleware } from "hono/factory";
4
4
  import type { AppEnv } from "../app.js";
@@ -52,16 +52,75 @@ export function hasScope(keyScopes: string[], required: string): boolean {
52
52
  return maxRank >= requiredRank;
53
53
  }
54
54
 
55
- const KEY_CACHE = new Map<
56
- string,
57
- {
58
- data: ApiKeyContext & { expiresAt: Date | null; lastUsedAt: Date | null };
59
- cachedAt: number;
60
- }
61
- >();
55
+ /** An `api_keys` row as both auth guards need it, cached per process. */
56
+ export interface CachedApiKey extends ApiKeyContext {
57
+ expiresAt: Date | null;
58
+ lastUsedAt: Date | null;
59
+ allowedOrigins: string[] | null;
60
+ }
61
+
62
+ const KEY_CACHE = new Map<string, { data: CachedApiKey; cachedAt: number }>();
62
63
  const CACHE_TTL = 60_000;
63
64
  const LAST_USED_DEBOUNCE = 5 * 60_000;
64
65
 
66
+ /**
67
+ * Drop a key's cached auth entry so THIS process re-reads the row on the key's
68
+ * next request. Called after a revoke or a rotation changes the row. The cache
69
+ * is per process: other API instances keep their entry until its TTL (60s)
70
+ * runs out, which is the documented revocation lag.
71
+ */
72
+ export function evictCachedApiKey(keyHash: string): void {
73
+ KEY_CACHE.delete(keyHash);
74
+ }
75
+
76
+ /**
77
+ * Read a live (unrevoked) key by hash through the 60s per-process cache, for
78
+ * secret and publishable keys alike. Misses are not cached, so a key minted a
79
+ * moment ago works on its first request. Expiry is the caller's check (it is
80
+ * re-evaluated on every request, cached or not). On a cache fill, `lastUsedAt`
81
+ * is bumped fire-and-forget at most once per five minutes.
82
+ */
83
+ export async function loadApiKey(
84
+ db: Database,
85
+ keyHash: string,
86
+ ): Promise<CachedApiKey | null> {
87
+ const cached = KEY_CACHE.get(keyHash);
88
+ if (cached && Date.now() - cached.cachedAt < CACHE_TTL) {
89
+ return cached.data;
90
+ }
91
+
92
+ const rows = await db
93
+ .select()
94
+ .from(apiKeys)
95
+ .where(and(eq(apiKeys.keyHash, keyHash), isNull(apiKeys.revokedAt)))
96
+ .limit(1);
97
+ const key = rows[0];
98
+ if (!key) return null;
99
+
100
+ const data: CachedApiKey = {
101
+ id: key.id,
102
+ name: key.name,
103
+ scopes: key.scopes as string[],
104
+ expiresAt: key.expiresAt,
105
+ lastUsedAt: key.lastUsedAt,
106
+ allowedOrigins: key.allowedOrigins ?? null,
107
+ };
108
+ KEY_CACHE.set(keyHash, { data, cachedAt: Date.now() });
109
+
110
+ if (
111
+ !key.lastUsedAt ||
112
+ Date.now() - key.lastUsedAt.getTime() > LAST_USED_DEBOUNCE
113
+ ) {
114
+ db.update(apiKeys)
115
+ .set({ lastUsedAt: new Date() })
116
+ .where(eq(apiKeys.id, key.id))
117
+ .then(() => {})
118
+ .catch(() => {});
119
+ }
120
+
121
+ return data;
122
+ }
123
+
65
124
  export const requireApiKey = createMiddleware<AppEnv>(async (c, next) => {
66
125
  const { env, db } = c.get("container");
67
126
 
@@ -81,65 +140,15 @@ export const requireApiKey = createMiddleware<AppEnv>(async (c, next) => {
81
140
  return next();
82
141
  }
83
142
 
84
- const keyHash = hashApiKey(provided);
85
-
86
- const cached = KEY_CACHE.get(keyHash);
87
- if (cached && Date.now() - cached.cachedAt < CACHE_TTL) {
88
- if (cached.data.expiresAt && cached.data.expiresAt < new Date()) {
89
- return c.json({ error: "API key expired" }, 401);
90
- }
91
- c.set("apiKey", {
92
- id: cached.data.id,
93
- name: cached.data.name,
94
- scopes: cached.data.scopes,
95
- });
96
- return next();
97
- }
98
-
99
- const rows = await db
100
- .select()
101
- .from(apiKeys)
102
- .where(and(eq(apiKeys.keyHash, keyHash), isNull(apiKeys.revokedAt)))
103
- .limit(1);
104
-
105
- const key = rows[0];
143
+ const key = await loadApiKey(db, hashApiKey(provided));
106
144
  if (!key) {
107
145
  return c.json({ error: "Unauthorized" }, 401);
108
146
  }
109
-
110
147
  if (key.expiresAt && key.expiresAt < new Date()) {
111
148
  return c.json({ error: "API key expired" }, 401);
112
149
  }
113
150
 
114
- const keyContext: ApiKeyContext = {
115
- id: key.id,
116
- name: key.name,
117
- scopes: key.scopes as string[],
118
- };
119
-
120
- c.set("apiKey", keyContext);
121
-
122
- KEY_CACHE.set(keyHash, {
123
- data: {
124
- ...keyContext,
125
- expiresAt: key.expiresAt,
126
- lastUsedAt: key.lastUsedAt,
127
- },
128
- cachedAt: Date.now(),
129
- });
130
-
131
- const shouldUpdateLastUsed =
132
- !key.lastUsedAt ||
133
- Date.now() - key.lastUsedAt.getTime() > LAST_USED_DEBOUNCE;
134
-
135
- if (shouldUpdateLastUsed) {
136
- db.update(apiKeys)
137
- .set({ lastUsedAt: new Date() })
138
- .where(eq(apiKeys.id, key.id))
139
- .then(() => {})
140
- .catch(() => {});
141
- }
142
-
151
+ c.set("apiKey", { id: key.id, name: key.name, scopes: key.scopes });
143
152
  return next();
144
153
  });
145
154
 
@@ -1,9 +1,7 @@
1
- import { apiKeys } from "@hogsend/db";
2
- import { and, eq, isNull } from "drizzle-orm";
3
1
  import { createMiddleware } from "hono/factory";
4
2
  import type { AppEnv } from "../app.js";
5
3
  import { hashApiKey } from "../lib/api-key-hash.js";
6
- import { hasScope, requireApiKey } from "./api-key.js";
4
+ import { hasScope, loadApiKey, requireApiKey } from "./api-key.js";
7
5
 
8
6
  /**
9
7
  * Data-plane guard for the BROWSER-REACHABLE subset of `/v1` (POST /events, the
@@ -17,8 +15,9 @@ import { hasScope, requireApiKey } from "./api-key.js";
17
15
  * byte-for-byte behaviorally identical to the existing
18
16
  * `requireApiKey` → `requireScope("ingest")` stack.
19
17
  *
20
- * - PUBLISHABLE key (`pk_` bearer): its own UNCACHED DB lookup (the secret
21
- * cache does not carry `allowed_origins`). Requires scope `ingest-public`
18
+ * - PUBLISHABLE key (`pk_` bearer): read through the same 60s key cache
19
+ * (`loadApiKey`, which carries `allowed_origins`) with the same debounced
20
+ * `lastUsedAt` bump as secret keys. Requires scope `ingest-public`
22
21
  * and REJECTS any pk_ token that somehow also carries `ingest`/`full-admin`
23
22
  * (defence in depth — a prefix never implies privilege). Enforces the
24
23
  * per-key Origin allowlist FAIL-CLOSED (no/empty allowlist ⇒ reject; Origin
@@ -61,17 +60,13 @@ export const requirePublishableOrIngest = createMiddleware<AppEnv>(
61
60
  return next();
62
61
  }
63
62
 
64
- // PUBLISHABLE path: pk_ token. Own DB lookup — `requireApiKey`'s cache does
65
- // not carry `allowed_origins`, and pk_ traffic is browser-Origin-gated and
66
- // lower-volume, so correctness over the 60s cache.
63
+ // PUBLISHABLE path: pk_ token. Same cached read as the secret path (a
64
+ // browser key is hit on every page view, so an uncached lookup plus an
65
+ // undebounced `lastUsedAt` write was two statements per request). Revoking
66
+ // or rotating evicts it here at once; other instances pick it up within
67
+ // the 60s TTL, the same lag secret keys have.
67
68
  const { db } = c.get("container");
68
- const keyHash = hashApiKey(provided);
69
- const rows = await db
70
- .select()
71
- .from(apiKeys)
72
- .where(and(eq(apiKeys.keyHash, keyHash), isNull(apiKeys.revokedAt)))
73
- .limit(1);
74
- const key = rows[0];
69
+ const key = await loadApiKey(db, hashApiKey(provided));
75
70
  if (!key) {
76
71
  return c.json({ error: "Unauthorized" }, 401);
77
72
  }
@@ -79,7 +74,7 @@ export const requirePublishableOrIngest = createMiddleware<AppEnv>(
79
74
  return c.json({ error: "API key expired" }, 401);
80
75
  }
81
76
 
82
- const scopes = key.scopes as string[];
77
+ const scopes = key.scopes;
83
78
  // A pk_ key MUST be exactly a publishable key. Reject a pk_-prefixed token
84
79
  // that somehow carries `ingest`/`full-admin` — the mint route forces
85
80
  // `["ingest-public"]`, but never trust the prefix alone to imply privilege.
@@ -107,13 +102,6 @@ export const requirePublishableOrIngest = createMiddleware<AppEnv>(
107
102
  });
108
103
  c.set("publishable", true);
109
104
 
110
- // Best-effort last-used bump (mirror requireApiKey's fire-and-forget).
111
- db.update(apiKeys)
112
- .set({ lastUsedAt: new Date() })
113
- .where(eq(apiKeys.id, key.id))
114
- .then(() => {})
115
- .catch(() => {});
116
-
117
105
  return next();
118
106
  },
119
107
  );
@@ -8,6 +8,22 @@ const DEFAULT_MAX_REQUESTS = 100;
8
8
  const DEFAULT_PREFIX = "ratelimit";
9
9
  const MAX_KEYS = 10_000;
10
10
 
11
+ /**
12
+ * Every header `createRateLimit` can set. `app.ts` passes this list to the CORS
13
+ * `exposeHeaders` option: none of these is a CORS-safelisted response header,
14
+ * so without it a browser (`@hogsend/js` on a pk_ key) could not read them —
15
+ * including `Retry-After`, which the SDK maps to `RateLimitError.retryAfter`.
16
+ */
17
+ export const RATE_LIMIT_HEADERS = [
18
+ "RateLimit-Limit",
19
+ "RateLimit-Remaining",
20
+ "RateLimit-Reset",
21
+ "RateLimit-Policy",
22
+ "X-RateLimit-Limit",
23
+ "X-RateLimit-Remaining",
24
+ "Retry-After",
25
+ ] as const;
26
+
11
27
  export interface RateLimitOptions {
12
28
  windowMs?: number;
13
29
  max?: number;
@@ -46,6 +62,52 @@ export function clientIpKey(c: Context<AppEnv>): string {
46
62
  return `ip:${clientIp(c)}`;
47
63
  }
48
64
 
65
+ /**
66
+ * Bucket key for the browser-reachable ingest surface (the routes behind
67
+ * `requirePublishableOrIngest`). Runs AFTER that guard, so `apiKey` is set.
68
+ *
69
+ * - Secret key: one bucket per key. A secret key is one server integration.
70
+ * - Publishable (pk_) key: one bucket per key PER CLIENT IP. A pk_ key is
71
+ * public and shared by every visitor of the site, so a per-key bucket
72
+ * would 429 the whole site once its aggregate traffic crossed the limit,
73
+ * and would let one client holding the (public) key exhaust everyone's
74
+ * budget.
75
+ * - No resolved key (not reachable behind the guard): client IP, so a
76
+ * misordered mount still never collapses onto one shared bucket.
77
+ */
78
+ export function ingestRateLimitKey(c: Context<AppEnv>): string {
79
+ const apiKey = c.get("apiKey");
80
+ if (!apiKey) return clientIpKey(c);
81
+ if (c.get("publishable")) return `pk:${apiKey.id}:${clientIpKey(c)}`;
82
+ return `key:${apiKey.id}`;
83
+ }
84
+
85
+ /**
86
+ * When the next quota unit frees, in ms epoch, for a sliding log.
87
+ *
88
+ * `oldest` is the oldest entry still in the window and `pivot` the max-th
89
+ * newest entry (counting the current request), present only once the log holds
90
+ * `max` or more entries. With room left, the next unit frees when the oldest
91
+ * entry ages out. With none left, the next request is accepted only once the
92
+ * pivot ages out: every entry after it fits under `max`. Rejected requests are
93
+ * logged too, which is why the pivot and not the oldest entry decides.
94
+ */
95
+ function nextSlotAt(
96
+ windowMs: number,
97
+ now: number,
98
+ oldest: number | undefined,
99
+ pivot: number | undefined,
100
+ ): number {
101
+ return (pivot ?? oldest ?? now) + windowMs;
102
+ }
103
+
104
+ /** Parse the `[member, score]` reply of `ZRANGE … WITHSCORES` for one rank. */
105
+ function scoreOf(reply: unknown): number | undefined {
106
+ if (!Array.isArray(reply) || reply.length < 2) return undefined;
107
+ const score = Number(reply[1]);
108
+ return Number.isFinite(score) ? score : undefined;
109
+ }
110
+
49
111
  /**
50
112
  * Build a sliding-window rate-limit middleware.
51
113
  *
@@ -53,20 +115,42 @@ export function clientIpKey(c: Context<AppEnv>): string {
53
115
  * and a distinct Redis key namespace, so two middlewares with different
54
116
  * prefixes (e.g. "ratelimit" vs "ratelimit:emails") never share a budget —
55
117
  * an email send must not consume the contact-upsert sliding window.
118
+ *
119
+ * Headers, on every limited response (allowed or 429):
120
+ * - `RateLimit-Limit` / `RateLimit-Remaining` / `RateLimit-Reset` /
121
+ * `RateLimit-Policy` (IETF draft-ietf-httpapi-ratelimit-headers, the
122
+ * separate-header form). `RateLimit-Reset` is the seconds until the window
123
+ * frees the next unit; `RateLimit-Policy` is `<max>;w=<window seconds>`.
124
+ * - `X-RateLimit-Limit` / `X-RateLimit-Remaining`, kept for existing readers.
125
+ * - `Retry-After` (seconds) on a 429 only: when the next request is accepted.
126
+ *
127
+ * An instance counts a request at most ONCE, however many matching `use`
128
+ * registrations reach it. Hono runs every matching `use`, and a `/<prefix>/*`
129
+ * pattern also matches the bare `/<prefix>`, so the same instance mounted on
130
+ * overlapping patterns would otherwise log two entries per request and halve
131
+ * the effective budget.
56
132
  */
57
133
  export function createRateLimit(opts: RateLimitOptions = {}) {
58
134
  const windowMs = opts.windowMs ?? DEFAULT_WINDOW_MS;
59
135
  const max = opts.max ?? DEFAULT_MAX_REQUESTS;
60
136
  const prefix = opts.prefix ?? DEFAULT_PREFIX;
61
137
  const disableInTest = opts.disableInTest ?? true;
138
+ const windowSeconds = Math.ceil(windowMs / 1000);
139
+ const policy = `${max};w=${windowSeconds}`;
62
140
 
63
141
  // Per-instance memory store so prefixes stay budget-isolated in the
64
142
  // Redis-less fallback path too.
65
143
  const memoryStore = new Map<string, number[]>();
66
144
  let cleanupCounter = 0;
145
+ // Requests this instance has already counted (see the doc comment above).
146
+ // Keyed on the raw Request, which is the same object for every middleware a
147
+ // request passes through; entries go with the request.
148
+ const counted = new WeakSet<Request>();
67
149
 
68
150
  return createMiddleware<AppEnv>(async (c, next) => {
69
151
  if (disableInTest && process.env.NODE_ENV === "test") return next();
152
+ if (counted.has(c.req.raw)) return next();
153
+ counted.add(c.req.raw);
70
154
 
71
155
  const keyId = opts.keyFn
72
156
  ? opts.keyFn(c)
@@ -74,6 +158,8 @@ export function createRateLimit(opts: RateLimitOptions = {}) {
74
158
  const now = Date.now();
75
159
 
76
160
  let count: number;
161
+ let oldest: number | undefined;
162
+ let pivot: number | undefined;
77
163
 
78
164
  try {
79
165
  const redis = getRedis();
@@ -83,10 +169,16 @@ export function createRateLimit(opts: RateLimitOptions = {}) {
83
169
  pipeline.zremrangebyscore(windowKey, 0, now - windowMs);
84
170
  pipeline.zcard(windowKey);
85
171
  pipeline.zadd(windowKey, now, `${now}:${Math.random()}`);
86
- pipeline.expire(windowKey, Math.ceil(windowMs / 1000));
172
+ pipeline.expire(windowKey, windowSeconds);
173
+ // Two O(log n) rank reads for RateLimit-Reset / Retry-After: the
174
+ // oldest entry, and the max-th newest (empty while under `max`).
175
+ pipeline.zrange(windowKey, 0, 0, "WITHSCORES");
176
+ pipeline.zrange(windowKey, -max, -max, "WITHSCORES");
87
177
 
88
178
  const results = await pipeline.exec();
89
179
  count = (results?.[1]?.[1] as number) ?? 0;
180
+ oldest = scoreOf(results?.[4]?.[1]);
181
+ pivot = scoreOf(results?.[5]?.[1]);
90
182
  } else {
91
183
  throw new Error("No Redis");
92
184
  }
@@ -97,6 +189,8 @@ export function createRateLimit(opts: RateLimitOptions = {}) {
97
189
  valid.push(now);
98
190
  memoryStore.set(keyId, valid);
99
191
  count = valid.length - 1;
192
+ oldest = valid[0];
193
+ pivot = valid.length >= max ? valid[valid.length - max] : undefined;
100
194
 
101
195
  if (++cleanupCounter % 100 === 0 && memoryStore.size > MAX_KEYS) {
102
196
  const sweepCutoff = now - windowMs;
@@ -108,12 +202,22 @@ export function createRateLimit(opts: RateLimitOptions = {}) {
108
202
  }
109
203
  }
110
204
 
205
+ const remaining = Math.max(0, max - count - 1);
206
+ const resetSeconds = Math.max(
207
+ 1,
208
+ Math.ceil((nextSlotAt(windowMs, now, oldest, pivot) - now) / 1000),
209
+ );
210
+
211
+ c.header("RateLimit-Limit", String(max));
212
+ c.header("RateLimit-Remaining", String(remaining));
213
+ c.header("RateLimit-Reset", String(resetSeconds));
214
+ c.header("RateLimit-Policy", policy);
111
215
  c.header("X-RateLimit-Limit", String(max));
112
- c.header("X-RateLimit-Remaining", String(Math.max(0, max - count - 1)));
216
+ c.header("X-RateLimit-Remaining", String(remaining));
113
217
 
114
218
  if (count >= max) {
115
219
  // SDKs map Retry-After to RateLimitError.retryAfter (seconds).
116
- c.header("Retry-After", String(Math.ceil(windowMs / 1000)));
220
+ c.header("Retry-After", String(resetSeconds));
117
221
  return c.json({ error: "Rate limit exceeded" }, 429);
118
222
  }
119
223
 
@@ -122,3 +226,61 @@ export function createRateLimit(opts: RateLimitOptions = {}) {
122
226
  }
123
227
 
124
228
  export const rateLimit = createRateLimit();
229
+
230
+ /**
231
+ * Data-plane budget defaults. `env.ts` declares the matching variables as
232
+ * optional so these stay the one place the numbers live.
233
+ *
234
+ * - Secret key on the ingest routes: 3000/min (50 req/s sustained). A secret
235
+ * key is usually one backend forwarding every user's events, and there is no
236
+ * batch event endpoint, so the budget has to cover a busy production app; it
237
+ * still stops a runaway loop well before it saturates an API instance.
238
+ * - Publishable key on the ingest routes: 1000/min per client IP. A browser on
239
+ * `@hogsend/js` makes a few requests a minute (one per captured event, plus a
240
+ * feed poll every 12s), so this leaves room for many visitors behind one
241
+ * office or carrier NAT while capping a single client that holds the public
242
+ * key.
243
+ * - `/v1/emails`: 30/min per key (unchanged). A loop sending more than that is
244
+ * almost certainly a runaway.
245
+ */
246
+ export const DEFAULT_INGEST_RATE_LIMIT_MAX = 3000;
247
+ export const DEFAULT_INGEST_PUBLIC_RATE_LIMIT_MAX = 1000;
248
+ export const DEFAULT_INGEST_RATE_LIMIT_WINDOW_MS = 60_000;
249
+ export const DEFAULT_EMAIL_RATE_LIMIT_MAX = 30;
250
+
251
+ export interface IngestRateLimitOptions {
252
+ /** Requests per window per secret key. */
253
+ max?: number;
254
+ /** Requests per window per publishable key per client IP. */
255
+ publicMax?: number;
256
+ windowMs?: number;
257
+ disableInTest?: boolean;
258
+ }
259
+
260
+ /**
261
+ * The rate limit for the browser-reachable ingest routes. Two isolated budgets
262
+ * (own prefixes, so neither shares a window with `/v1/emails` or the admin
263
+ * plane), picked by the key class `requirePublishableOrIngest` resolved. Mount
264
+ * it AFTER that guard: the bucket key and the budget both read what the guard
265
+ * set (`apiKey`, `publishable`).
266
+ */
267
+ export function createIngestRateLimit(opts: IngestRateLimitOptions = {}) {
268
+ const windowMs = opts.windowMs ?? DEFAULT_INGEST_RATE_LIMIT_WINDOW_MS;
269
+ const secret = createRateLimit({
270
+ prefix: "ratelimit:ingest",
271
+ max: opts.max ?? DEFAULT_INGEST_RATE_LIMIT_MAX,
272
+ windowMs,
273
+ keyFn: ingestRateLimitKey,
274
+ disableInTest: opts.disableInTest,
275
+ });
276
+ const publishable = createRateLimit({
277
+ prefix: "ratelimit:ingest-public",
278
+ max: opts.publicMax ?? DEFAULT_INGEST_PUBLIC_RATE_LIMIT_MAX,
279
+ windowMs,
280
+ keyFn: ingestRateLimitKey,
281
+ disableInTest: opts.disableInTest,
282
+ });
283
+ return createMiddleware<AppEnv>((c, next) =>
284
+ c.get("publishable") ? publishable(c, next) : secret(c, next),
285
+ );
286
+ }
@@ -4,6 +4,7 @@ import { and, count, desc, eq, isNull } from "drizzle-orm";
4
4
  import type { AppEnv } from "../../app.js";
5
5
  import { generateApiKey } from "../../lib/api-key-hash.js";
6
6
  import { errorSchema } from "../../lib/schemas.js";
7
+ import { evictCachedApiKey } from "../../middleware/api-key.js";
7
8
 
8
9
  const apiKeySchema = z.object({
9
10
  id: z.string(),
@@ -192,6 +193,72 @@ const revokeRoute = createRoute({
192
193
  },
193
194
  });
194
195
 
196
+ /** How long a rotated-out key keeps working by default: 24 hours. */
197
+ const DEFAULT_ROTATION_GRACE_SECONDS = 24 * 60 * 60;
198
+ /** The longest grace a rotation accepts: 30 days. */
199
+ const MAX_ROTATION_GRACE_SECONDS = 30 * 24 * 60 * 60;
200
+
201
+ const rotateRoute = createRoute({
202
+ method: "post",
203
+ path: "/{id}/rotate",
204
+ tags: ["Admin — API Keys"],
205
+ summary: "Rotate an API key",
206
+ description:
207
+ "Mints a replacement key with the same name, scopes, key class (secret " +
208
+ "or publishable) and Origin allowlist, and sets the old key to expire " +
209
+ "after a grace period so running deployments can switch over. An earlier " +
210
+ "existing expiry on the old key is kept. The new key is shown only once.",
211
+ request: {
212
+ params: z.object({ id: z.string().uuid() }),
213
+ body: {
214
+ required: false,
215
+ content: {
216
+ "application/json": {
217
+ schema: z.object({
218
+ // Seconds the OLD key keeps working. 0 expires it now.
219
+ gracePeriodSeconds: z
220
+ .number()
221
+ .int()
222
+ .min(0)
223
+ .max(MAX_ROTATION_GRACE_SECONDS)
224
+ .optional(),
225
+ // Expiry for the NEW key. Omitted: no expiry.
226
+ expiresAt: z.string().datetime().optional(),
227
+ }),
228
+ },
229
+ },
230
+ },
231
+ },
232
+ responses: {
233
+ 201: {
234
+ content: {
235
+ "application/json": {
236
+ schema: z.object({
237
+ id: z.string(),
238
+ name: z.string(),
239
+ key: z.string(),
240
+ keyPrefix: z.string(),
241
+ scopes: z.array(z.string()),
242
+ allowedOrigins: z.array(z.string()).nullable(),
243
+ expiresAt: z.string().nullable(),
244
+ createdAt: z.string(),
245
+ rotatedFrom: z.object({
246
+ id: z.string(),
247
+ // When the old key stops being accepted.
248
+ expiresAt: z.string(),
249
+ }),
250
+ }),
251
+ },
252
+ },
253
+ description: "Replacement key created — key is shown only once",
254
+ },
255
+ 404: {
256
+ content: { "application/json": { schema: errorSchema } },
257
+ description: "API key not found or already revoked",
258
+ },
259
+ },
260
+ });
261
+
195
262
  function serializeKey(row: typeof apiKeys.$inferSelect) {
196
263
  return {
197
264
  id: row.id,
@@ -326,6 +393,87 @@ export const apiKeysRouter = new OpenAPIHono<AppEnv>()
326
393
  .update(apiKeys)
327
394
  .set({ revokedAt: new Date(), updatedAt: new Date() })
328
395
  .where(eq(apiKeys.id, id));
396
+ // Takes effect at once on this instance; other instances drop their
397
+ // cached entry within its 60s TTL.
398
+ evictCachedApiKey(rows[0].keyHash);
329
399
 
330
400
  return c.json({ revoked: true }, 200);
401
+ })
402
+ .openapi(rotateRoute, async (c) => {
403
+ const { db } = c.get("container");
404
+ const { id } = c.req.valid("param");
405
+ // The body is optional: with no JSON body the validator hands back `{}`.
406
+ const body = c.req.valid("json");
407
+ const actor = c.get("apiKey");
408
+ const graceSeconds =
409
+ body.gracePeriodSeconds ?? DEFAULT_ROTATION_GRACE_SECONDS;
410
+
411
+ const result = await db.transaction(async (tx) => {
412
+ const [old] = await tx
413
+ .select()
414
+ .from(apiKeys)
415
+ .where(and(eq(apiKeys.id, id), isNull(apiKeys.revokedAt)))
416
+ .limit(1);
417
+ if (!old) return null;
418
+
419
+ // The class lives in the token prefix (lib/api-key-hash.ts), so the
420
+ // replacement is minted in the same class. A publishable key keeps
421
+ // exactly `["ingest-public"]` and its Origin allowlist, as on create.
422
+ const publishable = old.keyPrefix.startsWith("pk_");
423
+ const { key, prefix, hash } = generateApiKey({ publishable });
424
+
425
+ const now = new Date();
426
+ const graceEnd = new Date(now.getTime() + graceSeconds * 1000);
427
+ // Never EXTEND the old key's life: an earlier expiry already set wins.
428
+ const oldExpiresAt =
429
+ old.expiresAt && old.expiresAt < graceEnd ? old.expiresAt : graceEnd;
430
+
431
+ const [created] = await tx
432
+ .insert(apiKeys)
433
+ .values({
434
+ organizationId: old.organizationId,
435
+ name: old.name,
436
+ keyPrefix: prefix,
437
+ keyHash: hash,
438
+ scopes: publishable ? ["ingest-public"] : (old.scopes as string[]),
439
+ allowedOrigins: publishable ? (old.allowedOrigins ?? null) : null,
440
+ createdBy: actor?.name ?? null,
441
+ expiresAt: body.expiresAt ? new Date(body.expiresAt) : null,
442
+ })
443
+ .returning();
444
+ if (!created) throw new Error("Failed to create API key");
445
+
446
+ await tx
447
+ .update(apiKeys)
448
+ .set({ expiresAt: oldExpiresAt, updatedAt: now })
449
+ .where(eq(apiKeys.id, old.id));
450
+
451
+ return { old, created, key, prefix, oldExpiresAt };
452
+ });
453
+
454
+ if (!result) {
455
+ return c.json({ error: "API key not found or already revoked" }, 404);
456
+ }
457
+
458
+ // The old key's cached entry still carries its previous expiry.
459
+ evictCachedApiKey(result.old.keyHash);
460
+
461
+ const { created } = result;
462
+ return c.json(
463
+ {
464
+ id: created.id,
465
+ name: created.name,
466
+ key: result.key,
467
+ keyPrefix: result.prefix,
468
+ scopes: created.scopes as string[],
469
+ allowedOrigins: created.allowedOrigins ?? null,
470
+ expiresAt: created.expiresAt?.toISOString() ?? null,
471
+ createdAt: created.createdAt.toISOString(),
472
+ rotatedFrom: {
473
+ id: result.old.id,
474
+ expiresAt: result.oldExpiresAt.toISOString(),
475
+ },
476
+ },
477
+ 201,
478
+ );
331
479
  });
@@ -253,8 +253,13 @@ export const targetingRouter = new OpenAPIHono<AppEnv>()
253
253
  }
254
254
 
255
255
  const sampled = sample.length;
256
+ // The sample and the count run on separate connections (separate
257
+ // snapshots), so a concurrent delete can return a count below the sample
258
+ // size. Every sampled row existed, so the population is at least `sampled`
259
+ // and the estimate never drops below the matches actually seen.
260
+ const population = Math.max(liveTotal, sampled);
256
261
  const estimatedTotal =
257
- sampled === 0 ? 0 : Math.round((matched / sampled) * liveTotal);
262
+ sampled === 0 ? 0 : Math.round((matched / sampled) * population);
258
263
 
259
264
  return c.json({ matched, sampled, estimatedTotal }, 200);
260
265
  });
@@ -4,6 +4,7 @@ import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";
4
4
  import { count, desc, eq } from "drizzle-orm";
5
5
  import type { AppEnv } from "../../app.js";
6
6
  import { PRESET_DESTINATIONS } from "../../destinations/presets/index.js";
7
+ import { invalidateOutboundEndpointCache } from "../../lib/outbound-endpoint-cache.js";
7
8
  import { errorSchema } from "../../lib/schemas.js";
8
9
  import {
9
10
  generateWebhookSecret,
@@ -366,6 +367,7 @@ export const webhooksRouter = new OpenAPIHono<AppEnv>()
366
367
  .returning();
367
368
 
368
369
  if (!created) throw new Error("Failed to create webhook endpoint");
370
+ invalidateOutboundEndpointCache();
369
371
 
370
372
  // The ONLY list/get-shaped response that also carries the full secret — and
371
373
  // ONLY for kind="webhook" (keyed destinations have no secret to surface).
@@ -438,6 +440,7 @@ export const webhooksRouter = new OpenAPIHono<AppEnv>()
438
440
  if (!updated) {
439
441
  return c.json({ error: "Webhook endpoint not found" }, 404);
440
442
  }
443
+ invalidateOutboundEndpointCache();
441
444
 
442
445
  return c.json(serializeEndpoint(updated), 200);
443
446
  })
@@ -455,6 +458,7 @@ export const webhooksRouter = new OpenAPIHono<AppEnv>()
455
458
  if (deleted.length === 0) {
456
459
  return c.json({ error: "Webhook endpoint not found" }, 404);
457
460
  }
461
+ invalidateOutboundEndpointCache();
458
462
 
459
463
  return c.json({ deleted: true }, 200);
460
464
  })