@delmaredigital/payload-experiments 0.4.1 → 0.5.1

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 (310) hide show
  1. package/README.md +9 -5
  2. package/dist/admin/blocks/model.js +2 -0
  3. package/dist/admin/copy/blocks.d.ts +1 -0
  4. package/dist/admin/copy/blocks.js +1 -0
  5. package/dist/admin/copy/status.d.ts +3 -0
  6. package/dist/admin/copy/status.js +3 -0
  7. package/dist/admin/placementStatus.model.js +3 -0
  8. package/dist/admin/status.model.d.ts +3 -2
  9. package/dist/admin/status.model.js +9 -3
  10. package/dist/admin/surface/ResultsRoute.js +2 -1
  11. package/dist/admin/surface/TestsHome.d.ts +2 -1
  12. package/dist/admin/surface/TestsHome.js +31 -11
  13. package/dist/admin/surface/TestsSurface.js +4 -0
  14. package/dist/admin/surface/home.model.d.ts +11 -6
  15. package/dist/admin/surface/home.model.js +19 -8
  16. package/dist/admin/surface/newTest/PageWizard.js +13 -3
  17. package/dist/admin/surface/results/ResultsHeader.js +43 -9
  18. package/dist/admin/surface/test/TestPage.js +11 -1
  19. package/dist/admin/useLayerUsage.d.ts +1 -1
  20. package/dist/admin/views/buildSurfaceProps.js +3 -0
  21. package/dist/blocks/placementLocale.d.ts +14 -0
  22. package/dist/blocks/placementLocale.js +80 -13
  23. package/dist/blocks/placementPublication.d.ts +14 -0
  24. package/dist/blocks/placementPublication.js +24 -0
  25. package/dist/cohorts/build.d.ts +35 -0
  26. package/dist/cohorts/build.js +134 -0
  27. package/dist/cohorts/key.d.ts +12 -0
  28. package/dist/cohorts/key.js +15 -0
  29. package/dist/cohorts/maintenance.d.ts +10 -0
  30. package/dist/cohorts/maintenance.js +70 -0
  31. package/dist/cohorts/readiness.d.ts +30 -0
  32. package/dist/cohorts/readiness.js +54 -0
  33. package/dist/cohorts/readinessData.d.ts +29 -0
  34. package/dist/cohorts/readinessData.js +123 -0
  35. package/dist/cohorts/rule.d.ts +33 -0
  36. package/dist/cohorts/rule.js +50 -0
  37. package/dist/collections/cohortMembers.d.ts +4 -0
  38. package/dist/collections/cohortMembers.js +95 -0
  39. package/dist/collections/experiments/config.js +157 -7
  40. package/dist/collections/experiments/editability.js +10 -6
  41. package/dist/collections/experiments/lockHook.js +39 -20
  42. package/dist/collections/index.js +4 -1
  43. package/dist/collections/phases.js +4 -1
  44. package/dist/constants.d.ts +2 -0
  45. package/dist/constants.js +2 -0
  46. package/dist/db/adapter.d.ts +2 -1
  47. package/dist/db/adapter.js +17 -4
  48. package/dist/db/appends.d.ts +2 -1
  49. package/dist/db/appends.js +3 -2
  50. package/dist/db/bulkPlacementRemoval.d.ts +5 -0
  51. package/dist/db/bulkPlacementRemoval.js +7 -2
  52. package/dist/db/cohortMembership.d.ts +11 -0
  53. package/dist/db/cohortMembership.js +31 -0
  54. package/dist/db/cohortPool.d.ts +9 -0
  55. package/dist/db/cohortPool.js +53 -0
  56. package/dist/db/cohortTransactions.d.ts +3 -0
  57. package/dist/db/cohortTransactions.js +52 -0
  58. package/dist/db/cohorts.d.ts +72 -0
  59. package/dist/db/cohorts.js +280 -0
  60. package/dist/db/erase.d.ts +2 -0
  61. package/dist/db/erase.js +21 -2
  62. package/dist/db/events.d.ts +2 -0
  63. package/dist/db/events.js +8 -0
  64. package/dist/db/executor.d.ts +2 -0
  65. package/dist/db/executor.js +8 -0
  66. package/dist/db/locks.d.ts +7 -0
  67. package/dist/db/locks.js +13 -1
  68. package/dist/db/names.d.ts +95 -1
  69. package/dist/db/names.js +95 -1
  70. package/dist/db/occupancy.d.ts +21 -0
  71. package/dist/db/occupancy.js +46 -0
  72. package/dist/db/pageBindings.d.ts +2 -2
  73. package/dist/db/pageBindings.js +3 -3
  74. package/dist/db/pageOccupancy.d.ts +4 -4
  75. package/dist/db/pageOccupancy.js +13 -10
  76. package/dist/db/pgExecutor.d.ts +4 -0
  77. package/dist/db/pgExecutor.js +1 -1
  78. package/dist/db/placements.d.ts +6 -1
  79. package/dist/db/placements.js +27 -1
  80. package/dist/db/results.d.ts +5 -2
  81. package/dist/db/results.js +32 -5
  82. package/dist/db/slices.d.ts +9 -2
  83. package/dist/db/slices.js +15 -8
  84. package/dist/db/snapshots.d.ts +16 -1
  85. package/dist/db/snapshots.js +27 -2
  86. package/dist/db/sql.d.ts +2 -2
  87. package/dist/db/sql.js +1 -2
  88. package/dist/endpoints/cohortSources.d.ts +16 -0
  89. package/dist/endpoints/cohortSources.js +335 -0
  90. package/dist/endpoints/contentLocale.d.ts +1 -1
  91. package/dist/endpoints/duplicateOriginal.d.ts +1 -0
  92. package/dist/endpoints/events.js +82 -18
  93. package/dist/endpoints/index.js +45 -1
  94. package/dist/endpoints/pausedBy.d.ts +14 -0
  95. package/dist/endpoints/pausedBy.js +94 -0
  96. package/dist/endpoints/preflight.js +4 -1
  97. package/dist/endpoints/previewToken.js +2 -0
  98. package/dist/endpoints/results.d.ts +2 -2
  99. package/dist/endpoints/results.js +111 -3
  100. package/dist/endpoints/routeId.d.ts +2 -1
  101. package/dist/endpoints/routeId.js +6 -2
  102. package/dist/endpoints/summary.js +24 -1
  103. package/dist/endpoints/transitions.d.ts +7 -0
  104. package/dist/endpoints/transitions.js +50 -1
  105. package/dist/events/rows.d.ts +1 -1
  106. package/dist/events/rows.js +5 -3
  107. package/dist/exports/index.d.ts +2 -0
  108. package/dist/exports/index.js +1 -0
  109. package/dist/exports/runtimeProxy.d.ts +1 -0
  110. package/dist/exports/runtimeProxy.js +1 -0
  111. package/dist/hooks/blockRemovalRule.d.ts +1 -1
  112. package/dist/hooks/blockRemovalRule.js +12 -8
  113. package/dist/hooks/pageGuard.d.ts +7 -1
  114. package/dist/hooks/pageGuard.js +177 -4
  115. package/dist/hooks/puckNestedTests.d.ts +1 -1
  116. package/dist/hooks/puckNestedTests.js +3 -1
  117. package/dist/labels/status.d.ts +15 -0
  118. package/dist/labels/status.js +12 -0
  119. package/dist/manifest/compile.d.ts +15 -4
  120. package/dist/manifest/compile.js +91 -3
  121. package/dist/onInit.js +4 -6
  122. package/dist/options.d.ts +1 -0
  123. package/dist/options.js +10 -0
  124. package/dist/proxy/consentFallback.d.ts +1 -1
  125. package/dist/proxy/consentFallback.js +4 -4
  126. package/dist/proxy/control.d.ts +1 -1
  127. package/dist/proxy/control.js +2 -2
  128. package/dist/proxy/pageRewrite.d.ts +1 -1
  129. package/dist/proxy/pageRewrite.js +3 -2
  130. package/dist/proxy/resolve.js +76 -17
  131. package/dist/puck/ExperimentPicker.js +2 -1
  132. package/dist/puck/copy.d.ts +6 -5
  133. package/dist/puck/copy.js +3 -7
  134. package/dist/puck/editor/ExperimentSlotEditor.js +16 -5
  135. package/dist/puck/editor/QuickTestActions.js +7 -1
  136. package/dist/puck/editor/armWrites.js +6 -1
  137. package/dist/puck/editor/copy.d.ts +3 -0
  138. package/dist/puck/editor/copy.js +3 -0
  139. package/dist/puck/editor/editor.css +2 -1
  140. package/dist/puck/editor/editor.model.d.ts +2 -1
  141. package/dist/puck/editor/editor.model.js +2 -0
  142. package/dist/puck/editor/placementsCache.d.ts +1 -1
  143. package/dist/puck/editor/placementsCache.js +6 -3
  144. package/dist/results/analyse.d.ts +3 -1
  145. package/dist/results/analyse.js +20 -3
  146. package/dist/results/health.d.ts +11 -0
  147. package/dist/results/health.js +38 -0
  148. package/dist/results/power.d.ts +1 -1
  149. package/dist/results/power.js +1 -1
  150. package/dist/results/snapshot.d.ts +8 -2
  151. package/dist/results/snapshot.js +23 -1
  152. package/dist/results/unitValues.d.ts +48 -0
  153. package/dist/results/unitValues.js +166 -0
  154. package/dist/runtime/assign.d.ts +7 -3
  155. package/dist/runtime/assign.js +12 -4
  156. package/dist/runtime/cohortCookie.d.ts +15 -0
  157. package/dist/runtime/cohortCookie.js +72 -0
  158. package/dist/runtime/cohortLookup.d.ts +9 -0
  159. package/dist/runtime/cohortLookup.js +25 -0
  160. package/dist/runtime/eligibility.d.ts +11 -5
  161. package/dist/runtime/eligibility.js +33 -7
  162. package/dist/runtime/header.d.ts +1 -0
  163. package/dist/runtime/header.js +1 -1
  164. package/dist/runtime/proxyRuntime.js +5 -1
  165. package/dist/runtime/targeting.d.ts +3 -2
  166. package/dist/runtime/targeting.js +2 -1
  167. package/dist/sanitizeReadinessDetail.d.ts +2 -2
  168. package/dist/sanitizeReadinessDetail.js +41 -4
  169. package/dist/schedule/activeTime.d.ts +28 -0
  170. package/dist/schedule/activeTime.js +322 -0
  171. package/dist/schedule/ranges.d.ts +13 -0
  172. package/dist/schedule/ranges.js +11 -0
  173. package/dist/schedule/roundTime.d.ts +10 -0
  174. package/dist/schedule/roundTime.js +45 -0
  175. package/dist/schedule/types.d.ts +29 -0
  176. package/dist/schedule/types.js +1 -0
  177. package/dist/server/cohortLookup.d.ts +3 -0
  178. package/dist/server/cohortLookup.js +7 -0
  179. package/dist/server/eraseUser.d.ts +1 -0
  180. package/dist/server/getAssignments.js +63 -40
  181. package/dist/server/previewDraft.d.ts +1 -1
  182. package/dist/server/previewDraft.js +3 -2
  183. package/dist/server/recordContentChange.d.ts +1 -1
  184. package/dist/server/recordContentChange.js +42 -20
  185. package/dist/server/recordExposures.js +4 -2
  186. package/dist/server/runResults.js +97 -150
  187. package/dist/testSurface/actions/LifecycleAction.d.ts +2 -1
  188. package/dist/testSurface/actions/LifecycleAction.js +15 -5
  189. package/dist/testSurface/actions/actions.model.d.ts +13 -2
  190. package/dist/testSurface/actions/actions.model.js +38 -5
  191. package/dist/testSurface/api.d.ts +13 -1
  192. package/dist/testSurface/api.js +19 -0
  193. package/dist/testSurface/copy/actions.d.ts +13 -0
  194. package/dist/testSurface/copy/actions.js +13 -0
  195. package/dist/testSurface/copy/cohorts.d.ts +68 -0
  196. package/dist/testSurface/copy/cohorts.js +81 -0
  197. package/dist/testSurface/copy/common.d.ts +2 -3
  198. package/dist/testSurface/copy/common.js +4 -4
  199. package/dist/testSurface/copy/inputs.d.ts +3 -0
  200. package/dist/testSurface/copy/inputs.js +3 -0
  201. package/dist/testSurface/copy/readiness.d.ts +20 -6
  202. package/dist/testSurface/copy/readiness.js +42 -9
  203. package/dist/testSurface/copy/results.d.ts +1 -0
  204. package/dist/testSurface/copy/results.js +1 -0
  205. package/dist/testSurface/copy/schedule.d.ts +58 -0
  206. package/dist/testSurface/copy/schedule.js +150 -0
  207. package/dist/testSurface/copy/surface.d.ts +2 -0
  208. package/dist/testSurface/copy/surface.js +2 -0
  209. package/dist/testSurface/copy/testPage.d.ts +12 -2
  210. package/dist/testSurface/copy/testPage.js +17 -2
  211. package/dist/testSurface/format/roundProgress.d.ts +13 -1
  212. package/dist/testSurface/format/roundProgress.js +34 -5
  213. package/dist/testSurface/format/scheduleStatus.d.ts +34 -0
  214. package/dist/testSurface/format/scheduleStatus.js +93 -0
  215. package/dist/testSurface/format/useScheduleClock.d.ts +3 -0
  216. package/dist/testSurface/format/useScheduleClock.js +20 -0
  217. package/dist/testSurface/labels.d.ts +4 -9
  218. package/dist/testSurface/labels.js +2 -7
  219. package/dist/testSurface/lockedNotice.model.d.ts +2 -2
  220. package/dist/testSurface/lockedNotice.model.js +16 -3
  221. package/dist/testSurface/readPreflight.d.ts +3 -0
  222. package/dist/testSurface/readPreflight.js +14 -0
  223. package/dist/testSurface/readinessRead.d.ts +2 -2
  224. package/dist/testSurface/results/DataQualityPanel.js +7 -7
  225. package/dist/testSurface/results/results.model.d.ts +1 -1
  226. package/dist/testSurface/results/results.model.js +34 -6
  227. package/dist/testSurface/review/cohortEstimate.d.ts +11 -0
  228. package/dist/testSurface/review/cohortEstimate.js +32 -0
  229. package/dist/testSurface/surface/contracts.d.ts +2 -1
  230. package/dist/testSurface/surface/contracts.js +1 -0
  231. package/dist/testSurface/surface/editorPlacement.d.ts +1 -0
  232. package/dist/testSurface/surface/readiness/ActionDialogs.d.ts +3 -2
  233. package/dist/testSurface/surface/readiness/ActionDialogs.js +121 -32
  234. package/dist/testSurface/surface/readiness/Banner.js +29 -11
  235. package/dist/testSurface/surface/readiness/readiness.model.d.ts +20 -2
  236. package/dist/testSurface/surface/readiness/readiness.model.js +35 -5
  237. package/dist/testSurface/surface/readiness/useTransition.js +17 -5
  238. package/dist/testSurface/surface/results/ResultsBody.js +44 -8
  239. package/dist/testSurface/surface/results/useResults.d.ts +16 -1
  240. package/dist/testSurface/surface/results/useResults.js +17 -6
  241. package/dist/testSurface/surface/test/CohortRuleEditor.d.ts +16 -0
  242. package/dist/testSurface/surface/test/CohortRuleEditor.js +492 -0
  243. package/dist/testSurface/surface/test/LengthSection.js +26 -6
  244. package/dist/testSurface/surface/test/ScheduleDateInput.d.ts +4 -0
  245. package/dist/testSurface/surface/test/ScheduleDateInput.js +15 -0
  246. package/dist/testSurface/surface/test/ScheduleSection.d.ts +2 -0
  247. package/dist/testSurface/surface/test/ScheduleSection.js +322 -0
  248. package/dist/testSurface/surface/test/Section.js +1 -1
  249. package/dist/testSurface/surface/test/TargetingSection.js +306 -182
  250. package/dist/testSurface/surface/test/TestSurface.js +9 -0
  251. package/dist/testSurface/surface/test/fields.d.ts +2 -1
  252. package/dist/testSurface/surface/test/fields.js +3 -3
  253. package/dist/testSurface/surface/test/jumpToSection.d.ts +1 -1
  254. package/dist/testSurface/surface/test/jumpToSection.js +4 -4
  255. package/dist/testSurface/surface/test/testPage.css +26 -0
  256. package/dist/testSurface/surface/testDocument.model.d.ts +12 -1
  257. package/dist/testSurface/surface/testDocument.model.js +2 -1
  258. package/dist/testSurface/surface/useTestDocument.js +11 -4
  259. package/dist/testSurface/targeting/targeting.model.d.ts +2 -1
  260. package/dist/testSurface/targeting/targeting.model.js +1 -1
  261. package/dist/testSurface/useApi.d.ts +12 -0
  262. package/dist/transitions/addMetric.js +5 -2
  263. package/dist/transitions/allocate.js +6 -1
  264. package/dist/transitions/assertNoOverlap.d.ts +9 -0
  265. package/dist/transitions/assertNoOverlap.js +24 -0
  266. package/dist/transitions/buildCohorts.d.ts +10 -0
  267. package/dist/transitions/buildCohorts.js +68 -0
  268. package/dist/transitions/cancelSchedule.d.ts +4 -0
  269. package/dist/transitions/cancelSchedule.js +42 -0
  270. package/dist/transitions/checkStart.d.ts +17 -3
  271. package/dist/transitions/checkStart.js +20 -7
  272. package/dist/transitions/decide.js +40 -13
  273. package/dist/transitions/kernel.d.ts +3 -1
  274. package/dist/transitions/kernel.js +5 -2
  275. package/dist/transitions/pause.d.ts +4 -0
  276. package/dist/transitions/pause.js +35 -0
  277. package/dist/transitions/phaseRecord.d.ts +19 -2
  278. package/dist/transitions/phaseRecord.js +50 -11
  279. package/dist/transitions/placementInteractions.d.ts +7 -2
  280. package/dist/transitions/placementInteractions.js +9 -7
  281. package/dist/transitions/placements.d.ts +4 -3
  282. package/dist/transitions/placements.js +28 -14
  283. package/dist/transitions/preflightData.d.ts +11 -3
  284. package/dist/transitions/preflightData.js +134 -81
  285. package/dist/transitions/reschedule.d.ts +4 -0
  286. package/dist/transitions/reschedule.js +58 -0
  287. package/dist/transitions/resume.d.ts +4 -0
  288. package/dist/transitions/resume.js +32 -0
  289. package/dist/transitions/scheduledRound.d.ts +18 -0
  290. package/dist/transitions/scheduledRound.js +42 -0
  291. package/dist/transitions/start.d.ts +4 -0
  292. package/dist/transitions/start.js +139 -56
  293. package/dist/transitions/startReads.d.ts +14 -0
  294. package/dist/transitions/startReads.js +27 -0
  295. package/dist/transitions/stop.d.ts +10 -0
  296. package/dist/transitions/stop.js +35 -22
  297. package/dist/types/admin.d.ts +1 -1
  298. package/dist/types/contracts.d.ts +7 -1
  299. package/dist/types/endpoints.d.ts +72 -3
  300. package/dist/types/manifest.d.ts +9 -0
  301. package/dist/types/runtime.d.ts +5 -0
  302. package/dist/types/snapshot.d.ts +10 -0
  303. package/dist/types/status.d.ts +1 -0
  304. package/dist/types/status.js +1 -0
  305. package/dist/types/surface.d.ts +1 -0
  306. package/dist/ui/Feedback.d.ts +9 -6
  307. package/dist/ui/Feedback.js +10 -9
  308. package/dist/ui/SelectControl.js +1 -1
  309. package/dist/ui/ui.css +2 -1
  310. package/package.json +2 -2
@@ -0,0 +1,54 @@
1
+ import { cohortClosed } from './build.js';
2
+ import { timestampMs } from '../results/aggregate.js';
3
+ import { isCohortRule, readCohortRule } from './rule.js';
4
+ export const isCohortReadiness = (code)=>code.startsWith('cohort-') && code !== 'cohort-build-timeout';
5
+ function invalidSource(rule, fact, cutoff) {
6
+ const phase = fact?.source?.phase;
7
+ if (!phase) return 'round';
8
+ if (phase.phaseKind !== 'test') return 'round';
9
+ if (phase.endedAt != null && timestampMs(phase.endedAt) < +cutoff) return 'retention';
10
+ if (rule.arms.some((arm)=>!phase.arms.some((version)=>version.key === arm))) return 'arms';
11
+ if (rule.goal && !phase.metricDefinitions?.some((metric)=>String(metric.metricId) === rule.goal.metricId)) return 'goal';
12
+ if (!fact?.hasExposure) return 'no-exposure';
13
+ }
14
+ export function checkCohorts(experiment, facts) {
15
+ const checks = [];
16
+ if (!facts?.proxyLookup && experiment.targeting?.some(isCohortRule)) {
17
+ checks.push({
18
+ code: 'cohort-lookup-missing',
19
+ ok: false
20
+ });
21
+ return checks;
22
+ }
23
+ for (const [index, row] of (experiment.targeting ?? []).entries()){
24
+ if (!isCohortRule(row)) continue;
25
+ const rule = readCohortRule(row);
26
+ const fact = facts?.rules.get(index);
27
+ const reason = !rule ? 'invalid-rule' : rule.experiment === String(experiment.id) ? 'self' : !fact ? 'source' : invalidSource(rule, fact, facts.cutoff);
28
+ if (reason) checks.push({
29
+ code: 'cohort-source-invalid',
30
+ ok: false,
31
+ detail: {
32
+ rule: index,
33
+ reason
34
+ }
35
+ });
36
+ if (fact?.cycle) checks.push({
37
+ code: 'cohort-cycle',
38
+ ok: false,
39
+ detail: {
40
+ rule: index
41
+ }
42
+ });
43
+ if (rule?.goal?.completed === false && fact?.source && !cohortClosed(fact.source.phase, facts.now)) {
44
+ checks.push({
45
+ code: 'cohort-source-open',
46
+ ok: false,
47
+ detail: {
48
+ rule: index
49
+ }
50
+ });
51
+ }
52
+ }
53
+ return checks;
54
+ }
@@ -0,0 +1,29 @@
1
+ import { type PayloadRequest } from 'payload';
2
+ import type { ResolvedOptions } from '../types/contracts.js';
3
+ import type { StartExperiment } from '../transitions/checkStart.js';
4
+ import { type CohortSource } from '../db/cohorts.js';
5
+ import { type Executor } from '../db/executor.js';
6
+ import { type FrozenCohortRule } from './rule.js';
7
+ import type { CohortFacts } from './readiness.js';
8
+ /** A detached Local API request must retain Fetch prototype properties for
9
+ * host access callbacks and get its own Payload data loader. */
10
+ export declare function cohortReadRequest(req: PayloadRequest): PayloadRequest;
11
+ export declare function reachesTarget(exec: Executor, source: string, target: string, dependencies?: Map<string, string[]>): Promise<boolean>;
12
+ export declare function loadCohortFacts(req: PayloadRequest, exec: Executor, experiment: StartExperiment, options: Pick<ResolvedOptions, 'cohorts' | 'events'>, system: boolean, now: Date, deadline?: number): Promise<CohortFacts>;
13
+ /** Called only after the readiness checks have passed on these facts. */
14
+ export declare function freezeCohortTargeting(experiment: StartExperiment, facts: CohortFacts): {
15
+ targeting: (import("../types/contracts.js").TargetingRule | {
16
+ key: string;
17
+ experiment: string;
18
+ phase: number;
19
+ arms: import("../types/armKey.js").ArmKey[];
20
+ kind: "cohort";
21
+ goal?: {
22
+ metricId: string;
23
+ completed: boolean;
24
+ };
25
+ include: boolean;
26
+ })[];
27
+ rules: FrozenCohortRule[];
28
+ sources: Map<string, CohortSource>;
29
+ };
@@ -0,0 +1,123 @@
1
+ import { Forbidden, NotFound } from 'payload';
2
+ import { findStartDocuments } from '../transitions/startReads.js';
3
+ import { cohortDependencies, hasExposure, readCohortSource } from '../db/cohorts.js';
4
+ import { raceDeadline } from '../db/executor.js';
5
+ import { isCohortRule, readCohortRule } from './rule.js';
6
+ import { cohortExposureQuery } from './build.js';
7
+ import { DAY_MS } from '../schedule/roundTime.js';
8
+ import { cohortKey } from './key.js';
9
+ /** A detached Local API request must retain Fetch prototype properties for
10
+ * host access callbacks and get its own Payload data loader. */ export function cohortReadRequest(req) {
11
+ const detached = {
12
+ ...req,
13
+ headers: req.headers,
14
+ url: req.url,
15
+ method: req.method,
16
+ signal: req.signal,
17
+ context: {
18
+ ...req.context
19
+ },
20
+ transactionID: undefined
21
+ };
22
+ const { payloadDataLoader: _loader, ...read } = detached;
23
+ return read;
24
+ }
25
+ export async function reachesTarget(exec, source, target, dependencies = new Map()) {
26
+ const pending = [
27
+ source
28
+ ];
29
+ const visited = new Set();
30
+ while(pending.length){
31
+ const id = pending.pop();
32
+ if (id === target) return true;
33
+ if (visited.has(id)) continue;
34
+ visited.add(id);
35
+ let edges = dependencies.get(id);
36
+ if (!edges) {
37
+ edges = await cohortDependencies(exec, id);
38
+ dependencies.set(id, edges);
39
+ }
40
+ for (const edge of edges)pending.push(edge);
41
+ }
42
+ return false;
43
+ }
44
+ export async function loadCohortFacts(req, exec, experiment, options, system, now, deadline) {
45
+ const proxyLookup = options.cohorts?.proxyLookup === true;
46
+ const facts = {
47
+ proxyLookup,
48
+ now,
49
+ cutoff: new Date(+now - (options.events?.retentionDays ?? 180) * DAY_MS),
50
+ rules: new Map()
51
+ };
52
+ if (!proxyLookup) return facts;
53
+ const dependencies = new Map();
54
+ const target = String(experiment.id);
55
+ for (const [index, row] of (experiment.targeting ?? []).entries()){
56
+ const rule = readCohortRule(row);
57
+ if (!rule || rule.experiment === target) continue;
58
+ // An access refusal must not roll back an enclosing Start transaction.
59
+ const detachedReq = cohortReadRequest(req);
60
+ const read = async ()=>{
61
+ try {
62
+ const result = await findStartDocuments(detachedReq, {
63
+ collection: 'xp-experiments',
64
+ depth: 0,
65
+ limit: 1,
66
+ where: {
67
+ id: {
68
+ equals: rule.experiment
69
+ }
70
+ }
71
+ }, system);
72
+ return result.docs[0];
73
+ } catch (error) {
74
+ if (error instanceof Forbidden || error instanceof NotFound || [
75
+ 'Forbidden',
76
+ 'NotFound'
77
+ ].includes(error?.name ?? '')) return undefined;
78
+ throw error;
79
+ }
80
+ };
81
+ const visible = deadline === undefined ? await read() : await raceDeadline(deadline, read);
82
+ if (!visible) continue;
83
+ const source = await readCohortSource(exec, visible.id, rule.phase);
84
+ const cycle = await reachesTarget(exec, String(visible.id), target, dependencies);
85
+ const exposed = source ? await hasExposure(exec, cohortExposureQuery(source.phase, now)) : false;
86
+ facts.rules.set(index, {
87
+ source,
88
+ sourceName: typeof visible.name === 'string' ? visible.name : undefined,
89
+ cycle,
90
+ hasExposure: exposed
91
+ });
92
+ }
93
+ return facts;
94
+ }
95
+ /** Called only after the readiness checks have passed on these facts. */ export function freezeCohortTargeting(experiment, facts) {
96
+ const rules = [];
97
+ const sources = new Map();
98
+ const targeting = (experiment.targeting ?? []).map((row, index)=>{
99
+ if (!isCohortRule(row)) return row;
100
+ const rule = readCohortRule(row);
101
+ const source = facts.rules.get(index).source;
102
+ const resolved = {
103
+ ...rule,
104
+ experiment: String(source.phase.experiment),
105
+ phase: source.phase.phase,
106
+ arms: [
107
+ ...rule.arms
108
+ ].sort()
109
+ };
110
+ const frozen = {
111
+ ...resolved,
112
+ key: cohortKey(resolved)
113
+ };
114
+ rules.push(frozen);
115
+ sources.set(frozen.key, source);
116
+ return frozen;
117
+ });
118
+ return {
119
+ targeting,
120
+ rules,
121
+ sources
122
+ };
123
+ }
@@ -0,0 +1,33 @@
1
+ import type { ArmKey, DocId } from '../types/contracts.js';
2
+ export type CohortRule = {
3
+ kind: 'cohort';
4
+ experiment: string;
5
+ phase: number | 'latest';
6
+ arms: ArmKey[];
7
+ goal?: {
8
+ metricId: string;
9
+ completed: boolean;
10
+ };
11
+ include: boolean;
12
+ };
13
+ export type FrozenCohortRule = Omit<CohortRule, 'phase'> & {
14
+ phase: number;
15
+ key: string;
16
+ };
17
+ export declare const COHORT_ARMS: readonly ["control", "variantA", "variantB", "variantC", "holdout"];
18
+ export declare function isCohortRule(rule: unknown): rule is CohortRule | FrozenCohortRule;
19
+ export declare function cohortRelationshipId(value: unknown): DocId | undefined;
20
+ /** Read either Payload's flattened array row or a rule already in domain form. */
21
+ export declare function readCohortRule(row: unknown): CohortRule | undefined;
22
+ export type StoredCohortRule = ReturnType<typeof writeCohortRule> & {
23
+ id?: string;
24
+ };
25
+ export declare function writeCohortRule(rule: CohortRule): {
26
+ kind: "cohort";
27
+ sourceExperiment: string;
28
+ sourcePhase: number | null;
29
+ arms: ArmKey[];
30
+ goalMetric: string | null;
31
+ goalCompleted: string | null;
32
+ include: boolean;
33
+ };
@@ -0,0 +1,50 @@
1
+ export const COHORT_ARMS = [
2
+ 'control',
3
+ 'variantA',
4
+ 'variantB',
5
+ 'variantC',
6
+ 'holdout'
7
+ ];
8
+ export function isCohortRule(rule) {
9
+ return typeof rule === 'object' && rule !== null && 'kind' in rule && rule.kind === 'cohort';
10
+ }
11
+ export function cohortRelationshipId(value) {
12
+ const id = value && typeof value === 'object' && 'id' in value ? value.id : value;
13
+ if (typeof id === 'string' && id.trim() || typeof id === 'number' && Number.isSafeInteger(id)) return id;
14
+ }
15
+ /** Read either Payload's flattened array row or a rule already in domain form. */ export function readCohortRule(row) {
16
+ if (!isCohortRule(row)) return;
17
+ const fields = row;
18
+ const experiment = cohortRelationshipId(fields.sourceExperiment ?? fields.experiment);
19
+ const phase = fields.sourcePhase ?? fields.phase ?? 'latest';
20
+ const metric = cohortRelationshipId(fields.goalMetric ?? fields.goal?.metricId);
21
+ if (metric === undefined && (fields.goalCompleted != null || fields.goal != null)) return;
22
+ const completed = fields.goalMetric != null ? fields.goalCompleted === 'completed' ? true : fields.goalCompleted === 'notCompleted' ? false : undefined : fields.goal?.completed;
23
+ if (experiment === undefined || phase !== 'latest' && !(typeof phase === 'number' && Number.isSafeInteger(phase) && phase > 0) || !Array.isArray(fields.arms) || !fields.arms.length || !fields.arms.every((arm)=>COHORT_ARMS.includes(arm)) || metric !== undefined && typeof completed !== 'boolean') return;
24
+ return {
25
+ kind: 'cohort',
26
+ experiment: String(experiment),
27
+ phase: phase,
28
+ arms: [
29
+ ...fields.arms
30
+ ],
31
+ ...metric === undefined ? {} : {
32
+ goal: {
33
+ metricId: String(metric),
34
+ completed: completed
35
+ }
36
+ },
37
+ include: fields.include !== false
38
+ };
39
+ }
40
+ export function writeCohortRule(rule) {
41
+ return {
42
+ kind: rule.kind,
43
+ sourceExperiment: rule.experiment,
44
+ sourcePhase: rule.phase === 'latest' ? null : rule.phase,
45
+ arms: rule.arms,
46
+ goalMetric: rule.goal?.metricId ?? null,
47
+ goalCompleted: rule.goal ? rule.goal.completed ? 'completed' : 'notCompleted' : null,
48
+ include: rule.include
49
+ };
50
+ }
@@ -0,0 +1,4 @@
1
+ import type { CollectionConfig } from 'payload';
2
+ import type { ResolvedOptions } from '../types/contracts.js';
3
+ export declare function cohortMembersCollection(options: ResolvedOptions): CollectionConfig;
4
+ export declare function cohortsCollection(options: ResolvedOptions): CollectionConfig;
@@ -0,0 +1,95 @@
1
+ import { readOnlyAccess } from './access.js';
2
+ function hiddenCollection(options) {
3
+ return {
4
+ graphQL: false,
5
+ versions: false,
6
+ disableDuplicate: true,
7
+ admin: {
8
+ hidden: true
9
+ },
10
+ access: readOnlyAccess(options.access.readIdentities ?? options.access.admin ?? options.access.manage)
11
+ };
12
+ }
13
+ export function cohortMembersCollection(options) {
14
+ return {
15
+ ...hiddenCollection(options),
16
+ slug: 'xp-cohort-members',
17
+ indexes: [
18
+ {
19
+ fields: [
20
+ 'cohortKey',
21
+ 'unitId'
22
+ ],
23
+ unique: true
24
+ }
25
+ ],
26
+ fields: [
27
+ {
28
+ name: 'cohortKey',
29
+ type: 'text',
30
+ required: true
31
+ },
32
+ {
33
+ name: 'unitId',
34
+ type: 'text',
35
+ required: true
36
+ },
37
+ {
38
+ name: 'builtAt',
39
+ type: 'date',
40
+ required: true
41
+ },
42
+ {
43
+ name: 'enteredAt',
44
+ type: 'date',
45
+ required: true
46
+ },
47
+ {
48
+ name: 'exitedAt',
49
+ type: 'date'
50
+ }
51
+ ]
52
+ };
53
+ }
54
+ export function cohortsCollection(options) {
55
+ return {
56
+ ...hiddenCollection(options),
57
+ slug: 'xp-cohorts',
58
+ fields: [
59
+ {
60
+ name: 'cohortKey',
61
+ type: 'text',
62
+ required: true,
63
+ unique: true
64
+ },
65
+ {
66
+ name: 'rule',
67
+ type: 'json',
68
+ required: true
69
+ },
70
+ {
71
+ name: 'state',
72
+ type: 'select',
73
+ options: [
74
+ 'ready'
75
+ ],
76
+ required: true
77
+ },
78
+ {
79
+ name: 'closed',
80
+ type: 'checkbox',
81
+ defaultValue: false
82
+ },
83
+ {
84
+ name: 'members',
85
+ type: 'number',
86
+ required: true
87
+ },
88
+ {
89
+ name: 'builtAt',
90
+ type: 'date',
91
+ required: true
92
+ }
93
+ ]
94
+ };
95
+ }
@@ -1,4 +1,9 @@
1
+ import { COHORT_ARMS, cohortRelationshipId } from '../../cohorts/rule.js';
2
+ import { COHORT_COPY } from '../../testSurface/copy/cohorts.js';
3
+ import { date, relationship } from 'payload/shared';
1
4
  import { SURFACE_COPY } from '../../testSurface/copy/surface.js';
5
+ import { SCHEDULE_VALIDATION } from '../../testSurface/copy/testPage.js';
6
+ import { isValidTimeZone } from '../../schedule/activeTime.js';
2
7
  import * as paths from '../../admin/paths.js';
3
8
  import { managedAccess } from '../access.js';
4
9
  import { serializeAttributeKeys, serializeFlagPoints } from '../serialize.js';
@@ -13,6 +18,16 @@ import { checkArms } from './armInvariants.js';
13
18
  import { XpNotImplemented, XpRefusal } from '../../errors.js';
14
19
  const targetingValue = (rule)=>rule.op === 'in' || rule.op === 'nin' ? readListValue(rule.value) : rule.value;
15
20
  const positiveInteger = (value)=>value != null && Number.isInteger(value) && value > 0 || 'Enter a positive whole number.';
21
+ const weekdays = [
22
+ 'mon',
23
+ 'tue',
24
+ 'wed',
25
+ 'thu',
26
+ 'fri',
27
+ 'sat',
28
+ 'sun'
29
+ ];
30
+ const validClockTime = (value)=>typeof value === 'string' && value.length === 5 && /^(?:[01]\d|2[0-3]):[0-5]\d$/.test(value) || SCHEDULE_VALIDATION.clockTime;
16
31
  export function experimentsCollection(options) {
17
32
  const flagPoints = serializeFlagPoints(options);
18
33
  const attributeKeys = serializeAttributeKeys(options);
@@ -136,6 +151,7 @@ export function experimentsCollection(options) {
136
151
  options: [
137
152
  'draft',
138
153
  'running',
154
+ 'paused',
139
155
  'stopped',
140
156
  'decided',
141
157
  'archived'
@@ -251,7 +267,7 @@ export function experimentsCollection(options) {
251
267
  const status = data?.status ?? 'draft';
252
268
  if (data?.kind === 'block' && (status === 'draft' || status === 'stopped')) {
253
269
  for (const rule of value ?? []){
254
- if (rule.attribute !== 'path') continue;
270
+ if (rule.kind === 'cohort' || rule.attribute !== 'path') continue;
255
271
  const previous = rule.id == null ? undefined : previousValue?.find((stored)=>stored.id != null && String(stored.id) === String(rule.id));
256
272
  if (!previous || previous.attribute !== rule.attribute || previous.op !== rule.op || JSON.stringify(targetingValue(previous)) !== JSON.stringify(targetingValue(rule))) {
257
273
  return 'Page block tests run on their page; remove the page-path rule.';
@@ -261,26 +277,34 @@ export function experimentsCollection(options) {
261
277
  return true;
262
278
  },
263
279
  fields: [
280
+ {
281
+ name: 'kind',
282
+ type: 'select',
283
+ required: true,
284
+ defaultValue: 'attribute',
285
+ options: [
286
+ 'attribute',
287
+ 'cohort'
288
+ ]
289
+ },
264
290
  {
265
291
  name: 'attribute',
266
292
  type: 'text',
267
- required: true,
268
- validate: (value)=>typeof value === 'string' && Object.hasOwn(attributeKeys, value) || 'Choose a registered attribute.'
293
+ validate: (value, { siblingData })=>siblingData?.kind === 'cohort' || typeof value === 'string' && Object.hasOwn(attributeKeys, value) || 'Choose a registered attribute.'
269
294
  },
270
295
  {
271
296
  name: 'op',
272
297
  type: 'select',
273
- required: true,
274
298
  options: [
275
299
  ...new Set(Object.values(TARGETING_OPS).flat())
276
300
  ],
277
- validate: (value, { siblingData })=>isOpValid(attributeKeys[siblingData?.attribute ?? '']?.type ?? '', value) || 'Choose an operator supported by this attribute.'
301
+ validate: (value, { siblingData })=>siblingData?.kind === 'cohort' || isOpValid(attributeKeys[siblingData?.attribute ?? '']?.type ?? '', value) || 'Choose an operator supported by this attribute.'
278
302
  },
279
303
  {
280
304
  name: 'value',
281
305
  type: 'json',
282
- required: true,
283
306
  validate: (value, { siblingData })=>{
307
+ if (siblingData?.kind === 'cohort') return true;
284
308
  const type = attributeKeys[siblingData?.attribute ?? '']?.type;
285
309
  const op = siblingData?.op ?? '';
286
310
  if (!type || !isOpValid(type, op)) return 'Choose a registered attribute and supported operator.';
@@ -288,6 +312,48 @@ export function experimentsCollection(options) {
288
312
  if (!valid && type === 'string' && (op === 'in' || op === 'nin') && Array.isArray(value) && value.length === 0) return 'Add at least one value; an empty list matches nobody.';
289
313
  return valid || 'Enter a value of the type required by this attribute and operator.';
290
314
  }
315
+ },
316
+ {
317
+ name: 'sourceExperiment',
318
+ type: 'relationship',
319
+ relationTo: 'xp-experiments',
320
+ validate: (value, context)=>{
321
+ const { id, data, siblingData } = context;
322
+ if (siblingData?.kind !== 'cohort') return true;
323
+ const source = cohortRelationshipId(value);
324
+ if (source === undefined) return COHORT_COPY.source;
325
+ if (String(source) === String(id ?? data?.id)) return COHORT_COPY.self;
326
+ return relationship(value, context);
327
+ }
328
+ },
329
+ {
330
+ name: 'sourcePhase',
331
+ type: 'number',
332
+ validate: (value, { siblingData })=>siblingData?.kind !== 'cohort' || value == null || positiveInteger(value) === true || COHORT_COPY.round
333
+ },
334
+ {
335
+ name: 'arms',
336
+ type: 'select',
337
+ hasMany: true,
338
+ options: [
339
+ ...COHORT_ARMS
340
+ ],
341
+ validate: (value, { siblingData })=>siblingData?.kind !== 'cohort' || Array.isArray(value) && value.length > 0 && value.every((arm)=>COHORT_ARMS.includes(arm)) || COHORT_COPY.arms
342
+ },
343
+ metric('goalMetric'),
344
+ {
345
+ name: 'goalCompleted',
346
+ type: 'select',
347
+ options: [
348
+ 'completed',
349
+ 'notCompleted'
350
+ ],
351
+ validate: (value, { siblingData })=>siblingData?.kind !== 'cohort' || siblingData.goalMetric == null || value === 'completed' || value === 'notCompleted' || COHORT_COPY.goal
352
+ },
353
+ {
354
+ name: 'include',
355
+ type: 'checkbox',
356
+ defaultValue: true
291
357
  }
292
358
  ]
293
359
  }
@@ -440,6 +506,72 @@ export function experimentsCollection(options) {
440
506
  }
441
507
  ]
442
508
  },
509
+ {
510
+ name: 'schedule',
511
+ type: 'group',
512
+ hooks: {
513
+ beforeValidate: [
514
+ ({ value })=>{
515
+ // Give Payload a group object to traverse when a PATCH clears the schedule.
516
+ return value === null ? {
517
+ startAt: null,
518
+ endAt: null,
519
+ timeZone: null,
520
+ windows: []
521
+ } : value;
522
+ }
523
+ ]
524
+ },
525
+ fields: [
526
+ {
527
+ name: 'startAt',
528
+ type: 'date'
529
+ },
530
+ {
531
+ name: 'endAt',
532
+ type: 'date',
533
+ validate: async (value, context)=>{
534
+ const valid = await date(value, context);
535
+ const start = context.siblingData?.startAt;
536
+ return valid === true && value && start && new Date(value).getTime() <= new Date(start).getTime() ? SCHEDULE_VALIDATION.endAt : valid;
537
+ }
538
+ },
539
+ {
540
+ name: 'timeZone',
541
+ type: 'text',
542
+ hooks: {
543
+ beforeValidate: [
544
+ ({ operation, value })=>operation === 'create' && value === undefined ? options.timeZone : value
545
+ ]
546
+ },
547
+ validate: (value)=>value == null || value === '' || isValidTimeZone(value) || SCHEDULE_VALIDATION.timeZone
548
+ },
549
+ {
550
+ name: 'windows',
551
+ type: 'array',
552
+ fields: [
553
+ {
554
+ name: 'days',
555
+ type: 'json',
556
+ required: true,
557
+ validate: (value)=>Array.isArray(value) && value.length > 0 && value.every((day)=>weekdays.includes(day)) || SCHEDULE_VALIDATION.days
558
+ },
559
+ {
560
+ name: 'from',
561
+ type: 'text',
562
+ required: true,
563
+ validate: validClockTime
564
+ },
565
+ {
566
+ name: 'to',
567
+ type: 'text',
568
+ required: true,
569
+ validate: validClockTime
570
+ }
571
+ ]
572
+ }
573
+ ]
574
+ },
443
575
  {
444
576
  name: 'layerSlices',
445
577
  type: 'json',
@@ -465,8 +597,11 @@ export function experimentsCollection(options) {
465
597
  ...[
466
598
  'startedAt',
467
599
  'stoppedAt',
600
+ 'pausedAt',
468
601
  'decidedAt',
469
- 'archivedAt'
602
+ 'archivedAt',
603
+ 'claimFrom',
604
+ 'claimTo'
470
605
  ].map((name)=>({
471
606
  name,
472
607
  type: 'date',
@@ -474,6 +609,21 @@ export function experimentsCollection(options) {
474
609
  hidden: true
475
610
  }
476
611
  })),
612
+ {
613
+ name: 'pausedBy',
614
+ type: 'text',
615
+ admin: {
616
+ hidden: true
617
+ }
618
+ },
619
+ {
620
+ name: 'pauseReason',
621
+ type: 'textarea',
622
+ maxLength: 500,
623
+ admin: {
624
+ hidden: true
625
+ }
626
+ },
477
627
  {
478
628
  name: 'decision',
479
629
  type: 'group',
@@ -1,3 +1,7 @@
1
+ const runningFields = [
2
+ 'name',
3
+ 'hypothesis'
4
+ ];
1
5
  export const EDITABLE_BY_STATUS = {
2
6
  draft: [
3
7
  'name',
@@ -9,7 +13,8 @@ export const EDITABLE_BY_STATUS = {
9
13
  'metrics',
10
14
  'analysis',
11
15
  'exposurePolicy',
12
- 'pageCollection'
16
+ 'pageCollection',
17
+ 'schedule'
13
18
  ],
14
19
  stopped: [
15
20
  'name',
@@ -20,12 +25,11 @@ export const EDITABLE_BY_STATUS = {
20
25
  'metrics',
21
26
  'analysis',
22
27
  'exposurePolicy',
23
- 'pageCollection'
24
- ],
25
- running: [
26
- 'name',
27
- 'hypothesis'
28
+ 'pageCollection',
29
+ 'schedule'
28
30
  ],
31
+ running: runningFields,
32
+ paused: runningFields,
29
33
  decided: [
30
34
  'name',
31
35
  'hypothesis'