@tokenoftrust/storefront-runner 7.3.2 → 7.5.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 (415) hide show
  1. package/agency-kit/tools/lib/catalog-model/files.mjs +11 -4
  2. package/agency-kit/tools/lib/catalog-model/model.mjs +5 -1
  3. package/agency-kit/tools/lib/redirect-shadows.mjs +261 -0
  4. package/apps/storefront/astro.config.mjs +8 -2
  5. package/apps/storefront/package.json +1 -2
  6. package/apps/storefront/src/components/catalog/storefront/ProductCard.astro +20 -6
  7. package/apps/storefront/src/components/catalog/storefront/islands/ImageGallery.tsx +17 -6
  8. package/apps/storefront/src/components/compliance/storefront/islands/CheckoutComplianceGate.tsx +1 -32
  9. package/apps/storefront/src/config/compliance/rulesets.ts +1 -1
  10. package/apps/storefront/src/layouts/Layout.astro +17 -0
  11. package/apps/storefront/src/lib/access/auth/agentRoles.ts +20 -0
  12. package/apps/storefront/src/lib/access/auth/agentSessionGate.ts +32 -8
  13. package/apps/storefront/src/lib/access/auth/brokerAssertion.ts +16 -2
  14. package/apps/storefront/src/lib/access/auth/contactReaderAuthority.ts +39 -0
  15. package/apps/storefront/src/lib/access/auth/loginGate.ts +15 -17
  16. package/apps/storefront/src/lib/access/dashboard/staffAdmission.ts +5 -0
  17. package/apps/storefront/src/lib/access/dashboard/tenantSelection.ts +2 -0
  18. package/apps/storefront/src/lib/access/shared/capability.ts +14 -6
  19. package/apps/storefront/src/lib/access/shared/generated/capabilityCatalog.generated.ts +161 -0
  20. package/apps/storefront/src/lib/activity/activityScope.ts +36 -0
  21. package/apps/storefront/src/lib/activity/recordActivity.ts +23 -1
  22. package/apps/storefront/src/lib/activity/serverFanOut.ts +31 -0
  23. package/apps/storefront/src/lib/activity/store.ts +9 -3
  24. package/apps/storefront/src/lib/checkout/commerce/cardCartForm.ts +2 -0
  25. package/apps/storefront/src/lib/checkout/commerce/cartSession.ts +109 -12
  26. package/apps/storefront/src/lib/checkout/commerce/checkoutHandoffRoute.ts +53 -25
  27. package/apps/storefront/src/lib/checkout/commerce/finalCartAuthorization.ts +48 -6
  28. package/apps/storefront/src/lib/checkout/proposal/cartProposal.ts +388 -0
  29. package/apps/storefront/src/lib/checkout/proposal/listMatcher.ts +149 -0
  30. package/apps/storefront/src/lib/checkout/proposal/proposalRoutes.ts +447 -0
  31. package/apps/storefront/src/lib/checkout/proposal/reviewHtml.ts +100 -0
  32. package/apps/storefront/src/lib/compliance/browser/checkoutVerificationFlow.ts +26 -2
  33. package/apps/storefront/src/lib/content/shared/redirects.ts +21 -4
  34. package/apps/storefront/src/lib/publication/shared/edgeCache.ts +5 -2
  35. package/apps/storefront/src/lib/publication/shared/previewAnnotations/renderDecorator.ts +15 -8
  36. package/apps/storefront/src/lib/publication/shared/runtimeEnv.ts +1 -1
  37. package/apps/storefront/src/middleware/controlPlane.ts +15 -1
  38. package/apps/storefront/src/middleware/index.ts +7 -3
  39. package/apps/storefront/src/middleware/renderChain.ts +46 -33
  40. package/apps/storefront/src/pages/api/cart-proposal/confirm.ts +12 -0
  41. package/apps/storefront/src/pages/api/cart-proposal/review.ts +26 -0
  42. package/apps/storefront/src/pages/api/commerce/final-cart-authorization.ts +29 -6
  43. package/apps/storefront/src/pages/api/compliance/preflight.ts +25 -1
  44. package/apps/storefront/src/pages/products/[handle].astro +2 -15
  45. package/apps/storefront/tsconfig.astro.json +11 -0
  46. package/package.json +1 -1
  47. package/packages/public-runtime/src/activity/README.md +12 -0
  48. package/packages/public-runtime/src/activity/catalog.ts +7 -0
  49. package/packages/public-runtime/src/activity/event.ts +19 -0
  50. package/packages/public-runtime/src/activity/redaction.ts +31 -2
  51. package/packages/public-runtime/src/catalog-files.ts +3 -2
  52. package/packages/public-runtime/src/checkout-orders.ts +18 -19
  53. package/packages/public-runtime/src/checkout.ts +70 -0
  54. package/packages/public-runtime/src/compliance/evaluate.ts +60 -1
  55. package/packages/public-runtime/src/compliance/index.ts +1 -1
  56. package/packages/public-runtime/src/compliance/pact-report.ts +184 -3
  57. package/packages/public-runtime/src/compliance/profile.ts +268 -20
  58. package/packages/public-runtime/src/compliance/ruleset.ts +42 -0
  59. package/packages/public-runtime/src/customization-runtime.ts +1 -1
  60. package/packages/public-runtime/src/product.ts +9 -1
  61. package/packages/public-runtime/src/publication-content-redirects.ts +16 -3
  62. package/packages/public-runtime/tests/fixtures/compliance-decisions.golden.json +252 -52
  63. package/pnpm-lock.runner.yaml +0 -15
  64. package/scripts/tenant/materialize-tenant-content.mjs +3 -0
  65. package/scripts/tenant/validate.mjs +128 -0
  66. package/apps/storefront/dev-plugins/dev-publish.mjs +0 -55
  67. package/apps/storefront/drizzle.config.apps.ts +0 -13
  68. package/apps/storefront/integrations/preview-annotations-client-bundle.mjs +0 -80
  69. package/apps/storefront/migrations-apps/0000_fast_millenium_guard.sql +0 -129
  70. package/apps/storefront/migrations-apps/0001_woozy_lyja.sql +0 -22
  71. package/apps/storefront/migrations-apps/0002_good_switch.sql +0 -26
  72. package/apps/storefront/migrations-apps/0003_mute_marauders.sql +0 -12
  73. package/apps/storefront/migrations-apps/0004_deep_morph.sql +0 -36
  74. package/apps/storefront/migrations-apps/0005_common_mystique.sql +0 -23
  75. package/apps/storefront/migrations-apps/0006_broad_microchip.sql +0 -16
  76. package/apps/storefront/migrations-apps/0007_brave_outlaw_kid.sql +0 -15
  77. package/apps/storefront/migrations-apps/0008_blue_dazzler.sql +0 -11
  78. package/apps/storefront/migrations-apps/0009_bored_shinobi_shaw.sql +0 -15
  79. package/apps/storefront/migrations-apps/0010_ws4_verified_provider_measurement.sql +0 -35
  80. package/apps/storefront/migrations-apps/0011_dt_tracking_tenant_databases.sql +0 -23
  81. package/apps/storefront/migrations-apps/0012_test_destinations.sql +0 -29
  82. package/apps/storefront/migrations-apps/0013_publication_space_editorial.sql +0 -64
  83. package/apps/storefront/migrations-apps/0014_retire_candidate_ledger.sql +0 -12
  84. package/apps/storefront/migrations-apps/0015_merchant_calculation_settings.sql +0 -31
  85. package/apps/storefront/migrations-apps/0016_scrappy_commerce_contract.sql +0 -166
  86. package/apps/storefront/migrations-apps/0017_commerce_durable_projection.sql +0 -28
  87. package/apps/storefront/migrations-apps/0018_commerce_provider_cart_identity.sql +0 -13
  88. package/apps/storefront/migrations-apps/0019_commerce_source_authority.sql +0 -23
  89. package/apps/storefront/migrations-apps/0020_contact_messages.sql +0 -18
  90. package/apps/storefront/migrations-apps/0021_contact_vault.sql +0 -166
  91. package/apps/storefront/migrations-apps/0022_contact_vault_fields.sql +0 -342
  92. package/apps/storefront/migrations-apps/0023_principal_grant_contacts.sql +0 -15
  93. package/apps/storefront/migrations-apps/0024_order_contact_erasure_state.sql +0 -22
  94. package/apps/storefront/migrations-apps/0025_shipment_audit_order_lookup.sql +0 -2
  95. package/apps/storefront/migrations-apps/0026_fulfillment_assignment.sql +0 -44
  96. package/apps/storefront/migrations-apps/0027_app_contact_authority.sql +0 -70
  97. package/apps/storefront/migrations-apps/0028_app_jwk_and_candidates.sql +0 -133
  98. package/apps/storefront/migrations-apps/0029_inventory_locations_wms.sql +0 -38
  99. package/apps/storefront/migrations-apps/0030_assignment_split_shipments.sql +0 -149
  100. package/apps/storefront/migrations-apps/meta/0000_snapshot.json +0 -843
  101. package/apps/storefront/migrations-apps/meta/0001_snapshot.json +0 -990
  102. package/apps/storefront/migrations-apps/meta/0002_snapshot.json +0 -1160
  103. package/apps/storefront/migrations-apps/meta/0003_snapshot.json +0 -1235
  104. package/apps/storefront/migrations-apps/meta/0004_snapshot.json +0 -1474
  105. package/apps/storefront/migrations-apps/meta/0005_snapshot.json +0 -1617
  106. package/apps/storefront/migrations-apps/meta/0006_snapshot.json +0 -1716
  107. package/apps/storefront/migrations-apps/meta/0007_snapshot.json +0 -1812
  108. package/apps/storefront/migrations-apps/meta/0008_snapshot.json +0 -1881
  109. package/apps/storefront/migrations-apps/meta/0009_snapshot.json +0 -1979
  110. package/apps/storefront/migrations-apps/meta/_journal.json +0 -76
  111. package/apps/storefront/perf/README.md +0 -64
  112. package/apps/storefront/perf/assert-budgets.ts +0 -159
  113. package/apps/storefront/platform-reconcile-tenants.json +0 -3
  114. package/apps/storefront/playwright.config.ts +0 -23
  115. package/apps/storefront/public/js/dashboard-access-banner.js +0 -53
  116. package/apps/storefront/public/js/dashboard-apps.js +0 -183
  117. package/apps/storefront/public/js/dashboard-contact-capture.js +0 -52
  118. package/apps/storefront/public/js/dashboard-delegate.js +0 -167
  119. package/apps/storefront/public/js/dashboard-team.js +0 -356
  120. package/apps/storefront/public/js/dashboard-vendor-search.js +0 -115
  121. package/apps/storefront/src/components/content/storefront/AsyncStylesheet.astro +0 -26
  122. package/apps/storefront/src/components/integrations/storefront/WidgetFrame.astro +0 -34
  123. package/apps/storefront/src/config/adminTenantLookup.ts +0 -35
  124. package/apps/storefront/src/config/evidenceTenantRoster.ts +0 -25
  125. package/apps/storefront/src/config/staffProvisionProjection.ts +0 -24
  126. package/apps/storefront/src/config/storeName.ts +0 -34
  127. package/apps/storefront/src/lib/access/admin/README.md +0 -122
  128. package/apps/storefront/src/lib/access/admin/adminShell.ts +0 -281
  129. package/apps/storefront/src/lib/access/admin/envelope.ts +0 -9
  130. package/apps/storefront/src/lib/access/admin/viewmodels/settings.ts +0 -64
  131. package/apps/storefront/src/lib/access/auth/adminApiGuard.ts +0 -66
  132. package/apps/storefront/src/lib/access/auth/adminEntry.ts +0 -126
  133. package/apps/storefront/src/lib/access/auth/mcpClientAssertion.ts +0 -217
  134. package/apps/storefront/src/lib/access/auth/operatorSession.ts +0 -120
  135. package/apps/storefront/src/lib/access/dashboard/agentStoreEntry.ts +0 -253
  136. package/apps/storefront/src/lib/access/dashboard/delegateGate.ts +0 -130
  137. package/apps/storefront/src/lib/access/dashboard/staffStoreEntry.ts +0 -153
  138. package/apps/storefront/src/lib/access/dashboard/staffVendorAccess.ts +0 -474
  139. package/apps/storefront/src/lib/access/dashboard/vendorQuery.ts +0 -61
  140. package/apps/storefront/src/lib/access/fixtures/settings.ts +0 -67
  141. package/apps/storefront/src/lib/access/fixtures/tenants.ts +0 -29
  142. package/apps/storefront/src/lib/access/notifications/magicLinkInviteEmail.ts +0 -62
  143. package/apps/storefront/src/lib/access/shared/grantContactLookup.ts +0 -97
  144. package/apps/storefront/src/lib/access/shared/principalGrants.ts +0 -775
  145. package/apps/storefront/src/lib/activity/alerts.ts +0 -426
  146. package/apps/storefront/src/lib/activity/candidateSubmitLease.ts +0 -87
  147. package/apps/storefront/src/lib/activity/changeActorAttribution.ts +0 -181
  148. package/apps/storefront/src/lib/activity/evidence/evidenceReference.ts +0 -32
  149. package/apps/storefront/src/lib/activity/fixtures/evidence.ts +0 -99
  150. package/apps/storefront/src/lib/activity/query.ts +0 -403
  151. package/apps/storefront/src/lib/activity/uiActor.ts +0 -152
  152. package/apps/storefront/src/lib/admin/services/mock/index.ts +0 -12
  153. package/apps/storefront/src/lib/assets/addons/catalog.ts +0 -60
  154. package/apps/storefront/src/lib/assets/addons/storeEntitlements.ts +0 -260
  155. package/apps/storefront/src/lib/assets/durableAssetsRoute.ts +0 -65
  156. package/apps/storefront/src/lib/assets/durableR2.ts +0 -473
  157. package/apps/storefront/src/lib/assets/durableR2Bindings.ts +0 -33
  158. package/apps/storefront/src/lib/automation/admin/viewmodels/aiWorkflows.ts +0 -65
  159. package/apps/storefront/src/lib/automation/ai/approvalMode.ts +0 -20
  160. package/apps/storefront/src/lib/automation/ai/askEnvelope.ts +0 -47
  161. package/apps/storefront/src/lib/automation/ai/audit.ts +0 -12
  162. package/apps/storefront/src/lib/automation/ai/operationalRuns.ts +0 -155
  163. package/apps/storefront/src/lib/automation/ai/recommendation.ts +0 -20
  164. package/apps/storefront/src/lib/automation/fixtures/aiWorkflows.ts +0 -59
  165. package/apps/storefront/src/lib/catalog/admin/catalogSettingsClient.ts +0 -250
  166. package/apps/storefront/src/lib/catalog/admin/catalogSettingsFields.ts +0 -134
  167. package/apps/storefront/src/lib/catalog/admin/catalogSettingsPreview.ts +0 -37
  168. package/apps/storefront/src/lib/catalog/admin/catalogSettingsPricingView.ts +0 -39
  169. package/apps/storefront/src/lib/catalog/admin/catalogSettingsRuleView.ts +0 -44
  170. package/apps/storefront/src/lib/catalog/admin/catalogSettingsSkuView.ts +0 -21
  171. package/apps/storefront/src/lib/catalog/admin/catalogSettingsState.ts +0 -132
  172. package/apps/storefront/src/lib/catalog/admin/productSetupPhoto.ts +0 -23
  173. package/apps/storefront/src/lib/catalog/admin/productsTabClient.ts +0 -193
  174. package/apps/storefront/src/lib/catalog/admin/services/catalogProducts.ts +0 -32
  175. package/apps/storefront/src/lib/catalog/admin/viewmodels/catalogProducts.ts +0 -164
  176. package/apps/storefront/src/lib/catalog/admin/viewmodels/products.ts +0 -267
  177. package/apps/storefront/src/lib/catalog/fixtures/products.ts +0 -105
  178. package/apps/storefront/src/lib/catalog/tot/syncState.ts +0 -111
  179. package/apps/storefront/src/lib/checkout/admin/checkoutStyleInput.ts +0 -172
  180. package/apps/storefront/src/lib/checkout/admin/checkoutStyleTarget.ts +0 -65
  181. package/apps/storefront/src/lib/checkout/browser/checkoutExciseForm.ts +0 -152
  182. package/apps/storefront/src/lib/checkout/browser/checkoutRequirementsForm.ts +0 -72
  183. package/apps/storefront/src/lib/checkout/browser/checkoutStyleForm.ts +0 -110
  184. package/apps/storefront/src/lib/checkout/browser/settingDraftActions.ts +0 -367
  185. package/apps/storefront/src/lib/checkout/commerce/checkoutSettings.ts +0 -246
  186. package/apps/storefront/src/lib/checkout/fixtures/checkoutSettings.ts +0 -77
  187. package/apps/storefront/src/lib/content/blog/markdown.ts +0 -174
  188. package/apps/storefront/src/lib/content/chrome/examples/tokenoftrust.chrome-assignments.example.json +0 -6
  189. package/apps/storefront/src/lib/content/chrome/examples/tokenoftrust.chrome.example.json +0 -79
  190. package/apps/storefront/src/lib/content/index.ts +0 -27
  191. package/apps/storefront/src/lib/content/the-build/readingTime.ts +0 -35
  192. package/apps/storefront/src/lib/content/the-build/toc.ts +0 -95
  193. package/apps/storefront/src/lib/customers/admin/viewmodels/customers.ts +0 -56
  194. package/apps/storefront/src/lib/customers/contactVault/blindIndex.ts +0 -28
  195. package/apps/storefront/src/lib/customers/contactVault/contactAudit.ts +0 -149
  196. package/apps/storefront/src/lib/customers/contactVault/dataKeys.ts +0 -283
  197. package/apps/storefront/src/lib/customers/contactVault/fieldEnvelope.ts +0 -88
  198. package/apps/storefront/src/lib/customers/contactVault/isolateMetrics.ts +0 -37
  199. package/apps/storefront/src/lib/customers/contactVault/kms.ts +0 -292
  200. package/apps/storefront/src/lib/customers/contactVault/retention.ts +0 -302
  201. package/apps/storefront/src/lib/customers/contactVault/retentionPolicy.ts +0 -290
  202. package/apps/storefront/src/lib/customers/contactVault/zip3.ts +0 -24
  203. package/apps/storefront/src/lib/customers/fixtures/customers.ts +0 -86
  204. package/apps/storefront/src/lib/developer-experience/admin/memoizeByEnvelope.ts +0 -27
  205. package/apps/storefront/src/lib/developer-experience/admin/seed.ts +0 -54
  206. package/apps/storefront/src/lib/developer-experience/dev/activityIngestToken.ts +0 -85
  207. package/apps/storefront/src/lib/developer-experience/dev/activityStore.ts +0 -82
  208. package/apps/storefront/src/lib/developer-experience/dev/agentGrantAttestation.ts +0 -74
  209. package/apps/storefront/src/lib/developer-experience/dev/ai-widget-client.js +0 -308
  210. package/apps/storefront/src/lib/developer-experience/dev/apiBase.ts +0 -17
  211. package/apps/storefront/src/lib/developer-experience/dev/cliSignInCode.ts +0 -124
  212. package/apps/storefront/src/lib/developer-experience/dev/cockpit/ask.ts +0 -49
  213. package/apps/storefront/src/lib/developer-experience/dev/cockpit/clickToSource.ts +0 -71
  214. package/apps/storefront/src/lib/developer-experience/dev/cockpit/copyCommand.ts +0 -227
  215. package/apps/storefront/src/lib/developer-experience/dev/cockpit/files.ts +0 -249
  216. package/apps/storefront/src/lib/developer-experience/dev/cockpit/hostedActivity.ts +0 -500
  217. package/apps/storefront/src/lib/developer-experience/dev/cockpit/infoPopover.ts +0 -12
  218. package/apps/storefront/src/lib/developer-experience/dev/cockpit/mcpCopy.ts +0 -34
  219. package/apps/storefront/src/lib/developer-experience/dev/cockpit/monitor.ts +0 -107
  220. package/apps/storefront/src/lib/developer-experience/dev/cockpit/stepper.ts +0 -116
  221. package/apps/storefront/src/lib/developer-experience/dev/cockpit/themeToggle.ts +0 -23
  222. package/apps/storefront/src/lib/developer-experience/dev/livePreview.ts +0 -77
  223. package/apps/storefront/src/lib/developer-experience/dev/obstacleStore.ts +0 -214
  224. package/apps/storefront/src/lib/developer-experience/dev/previewStatus.ts +0 -87
  225. package/apps/storefront/src/lib/developer-experience/dev/rendezvousBroker.ts +0 -226
  226. package/apps/storefront/src/lib/developer-experience/dev/runtimeStore.ts +0 -291
  227. package/apps/storefront/src/lib/developer-experience/dev/storeStatus.ts +0 -142
  228. package/apps/storefront/src/lib/developer-experience/dev/subjectReissue.ts +0 -67
  229. package/apps/storefront/src/lib/developer-experience/fixtures/regulated-commerce-truth/index.ts +0 -540
  230. package/apps/storefront/src/lib/developer-experience/fixtures/ws3-readonly-clone-preflight/index.ts +0 -495
  231. package/apps/storefront/src/lib/engagement/contact/contactMessages.ts +0 -671
  232. package/apps/storefront/src/lib/engagement/contact/messagesTabClient.ts +0 -175
  233. package/apps/storefront/src/lib/engagement/contact/submission.ts +0 -112
  234. package/apps/storefront/src/lib/engagement/messaging/messagesClient.ts +0 -149
  235. package/apps/storefront/src/lib/engagement/newsletter/digest.ts +0 -117
  236. package/apps/storefront/src/lib/engagement/newsletter/resendClient.ts +0 -79
  237. package/apps/storefront/src/lib/engagement/newsletter/send.ts +0 -265
  238. package/apps/storefront/src/lib/engagement/social/golive.ts +0 -212
  239. package/apps/storefront/src/lib/engagement/social/notify.ts +0 -32
  240. package/apps/storefront/src/lib/engagement/social/tokenStore.ts +0 -171
  241. package/apps/storefront/src/lib/fulfillment/admin/commerceDesk.ts +0 -180
  242. package/apps/storefront/src/lib/fulfillment/admin/commerceDeskRouting.ts +0 -45
  243. package/apps/storefront/src/lib/fulfillment/admin/viewmodels/fulfillment.ts +0 -1071
  244. package/apps/storefront/src/lib/fulfillment/appShipmentEvidence.ts +0 -271
  245. package/apps/storefront/src/lib/fulfillment/carrierEvidence.ts +0 -105
  246. package/apps/storefront/src/lib/fulfillment/commerceProjection.ts +0 -157
  247. package/apps/storefront/src/lib/fulfillment/commerceReadback.ts +0 -60
  248. package/apps/storefront/src/lib/fulfillment/commerceRecovery.ts +0 -84
  249. package/apps/storefront/src/lib/fulfillment/commerceRuntime.ts +0 -41
  250. package/apps/storefront/src/lib/fulfillment/fulfillmentService.ts +0 -2842
  251. package/apps/storefront/src/lib/fulfillment/readiness.ts +0 -217
  252. package/apps/storefront/src/lib/fulfillment/splitShipments.ts +0 -388
  253. package/apps/storefront/src/lib/fulfillment/syntheticPreviewOrders.ts +0 -292
  254. package/apps/storefront/src/lib/integrations/apps/appContactAuthority.ts +0 -27
  255. package/apps/storefront/src/lib/integrations/apps/appContactConsents.ts +0 -118
  256. package/apps/storefront/src/lib/integrations/apps/appInstallEnv.ts +0 -28
  257. package/apps/storefront/src/lib/integrations/apps/gtmMigrationDraft.ts +0 -103
  258. package/apps/storefront/src/lib/integrations/apps/inventoryWrite.ts +0 -906
  259. package/apps/storefront/src/lib/integrations/apps/orders/customerHash.ts +0 -28
  260. package/apps/storefront/src/lib/integrations/apps/orders/idempotency.ts +0 -100
  261. package/apps/storefront/src/lib/integrations/apps/reports/salesVelocityReport.ts +0 -96
  262. package/apps/storefront/src/lib/integrations/apps/routeParams.ts +0 -45
  263. package/apps/storefront/src/lib/integrations/webhooks/cloudflareQueueDispatcher.ts +0 -82
  264. package/apps/storefront/src/lib/integrations/webhooks/commercePayloads.ts +0 -159
  265. package/apps/storefront/src/lib/integrations/webhooks/deliveryEngine.ts +0 -379
  266. package/apps/storefront/src/lib/integrations/webhooks/deliveryMapper.ts +0 -34
  267. package/apps/storefront/src/lib/integrations/webhooks/deliveryStore.ts +0 -903
  268. package/apps/storefront/src/lib/integrations/webhooks/dispatcher.ts +0 -168
  269. package/apps/storefront/src/lib/integrations/webhooks/emit.ts +0 -245
  270. package/apps/storefront/src/lib/integrations/webhooks/events.ts +0 -105
  271. package/apps/storefront/src/lib/integrations/webhooks/getDispatcher.ts +0 -77
  272. package/apps/storefront/src/lib/integrations/webhooks/orderFulfilledClaim.ts +0 -75
  273. package/apps/storefront/src/lib/measurement/admin/analyticsRegionPolicy.ts +0 -151
  274. package/apps/storefront/src/lib/measurement/admin/analyticsRegionsClient.ts +0 -84
  275. package/apps/storefront/src/lib/measurement/admin/consentSettingsClient.ts +0 -113
  276. package/apps/storefront/src/lib/measurement/analytics/index.ts +0 -44
  277. package/apps/storefront/src/lib/measurement/attribution/attributionService.ts +0 -206
  278. package/apps/storefront/src/lib/measurement/attribution/orderConversionD1.ts +0 -150
  279. package/apps/storefront/src/lib/measurement/attribution/verifiedProviderMeasurement.ts +0 -118
  280. package/apps/storefront/src/lib/measurement/tracking/interactionCollector.ts +0 -205
  281. package/apps/storefront/src/lib/measurement/tracking/pruneRollup.ts +0 -260
  282. package/apps/storefront/src/lib/measurement/tracking/tenantReadAccess.ts +0 -138
  283. package/apps/storefront/src/lib/measurement/tracking-writeback/rewriteTrackingIds.ts +0 -175
  284. package/apps/storefront/src/lib/measurement/tracking-writeback/writebackCandidate.ts +0 -110
  285. package/apps/storefront/src/lib/observability/diag/capture.ts +0 -162
  286. package/apps/storefront/src/lib/observability/diag/consoleLine.ts +0 -76
  287. package/apps/storefront/src/lib/observability/diag/debugConsole.ts +0 -118
  288. package/apps/storefront/src/lib/observability/diag/debugMode.ts +0 -163
  289. package/apps/storefront/src/lib/observability/diag/debugWindow.ts +0 -111
  290. package/apps/storefront/src/lib/observability/diag/redact.ts +0 -17
  291. package/apps/storefront/src/lib/observability/diag/reference.ts +0 -148
  292. package/apps/storefront/src/lib/observability/diag/referenceStore.ts +0 -113
  293. package/apps/storefront/src/lib/observability/diag/report.ts +0 -110
  294. package/apps/storefront/src/lib/observability/diag/serverResponseTime.ts +0 -91
  295. package/apps/storefront/src/lib/observability/diag/session.ts +0 -93
  296. package/apps/storefront/src/lib/observability/monitoring/manifest.ts +0 -301
  297. package/apps/storefront/src/lib/observability/performance/lighthouseReport.ts +0 -109
  298. package/apps/storefront/src/lib/observability/performance/rumAlert.ts +0 -179
  299. package/apps/storefront/src/lib/orders/admin/orderActions.ts +0 -556
  300. package/apps/storefront/src/lib/orders/admin/orderClient.ts +0 -248
  301. package/apps/storefront/src/lib/orders/admin/orderSource.ts +0 -410
  302. package/apps/storefront/src/lib/orders/admin/services/orders.ts +0 -922
  303. package/apps/storefront/src/lib/orders/admin/viewmodels/orders.ts +0 -135
  304. package/apps/storefront/src/lib/orders/data/commerceCallerConfig.ts +0 -37
  305. package/apps/storefront/src/lib/orders/data/ledgerContractOrders.ts +0 -148
  306. package/apps/storefront/src/lib/orders/data/orderForwardReceiver.ts +0 -532
  307. package/apps/storefront/src/lib/orders/data/orderMapper.ts +0 -454
  308. package/apps/storefront/src/lib/orders/data/ordersStore.ts +0 -623
  309. package/apps/storefront/src/lib/orders/data/sourceAuthorityContract.ts +0 -380
  310. package/apps/storefront/src/lib/orders/data/sourceAuthorityStore.ts +0 -830
  311. package/apps/storefront/src/lib/orders/fixtures/orders.ts +0 -186
  312. package/apps/storefront/src/lib/orders/providers/foxyOrderClient.ts +0 -207
  313. package/apps/storefront/src/lib/orders/vault/appRecipientRead.ts +0 -222
  314. package/apps/storefront/src/lib/orders/vault/contactIntake.ts +0 -63
  315. package/apps/storefront/src/lib/orders/vault/contactRoutes.ts +0 -110
  316. package/apps/storefront/src/lib/orders/vault/kmsEnv.ts +0 -11
  317. package/apps/storefront/src/lib/orders/vault/orderContact.ts +0 -96
  318. package/apps/storefront/src/lib/orders/vault/orderContactVault.ts +0 -1427
  319. package/apps/storefront/src/lib/orders/vault/vault-test-support.ts +0 -258
  320. package/apps/storefront/src/lib/publication/d1/schema-apps.ts +0 -808
  321. package/apps/storefront/src/lib/publication/notifications/candidateConflictEmail.ts +0 -80
  322. package/apps/storefront/src/lib/publication/servingMode.ts +0 -52
  323. package/apps/storefront/src/lib/publication/shared/previewAnnotations/moduleAsset.ts +0 -65
  324. package/apps/storefront/src/lib/publication/shared/routedApiParam.ts +0 -38
  325. package/apps/storefront/src/lib/reporting/admin/viewmodels/reporting.ts +0 -34
  326. package/apps/storefront/src/lib/reporting/fixtures/reporting.ts +0 -51
  327. package/apps/storefront/src/lib/subscriptions/admin/viewmodels/subscriptions.ts +0 -106
  328. package/apps/storefront/src/lib/subscriptions/fixtures/subscriptions.ts +0 -398
  329. package/apps/storefront/src/pages/api/admin/activity-alerts.ts +0 -58
  330. package/apps/storefront/src/pages/api/admin/commerce-readiness.ts +0 -172
  331. package/apps/storefront/src/pages/api/admin/fulfillment/batches.ts +0 -59
  332. package/apps/storefront/src/pages/api/admin/fulfillment/orders/[id]/address.ts +0 -66
  333. package/apps/storefront/src/pages/api/admin/fulfillment/orders/[id].ts +0 -118
  334. package/apps/storefront/src/pages/api/admin/fulfillment/orders/contact-search.ts +0 -26
  335. package/apps/storefront/src/pages/api/admin/fulfillment/orders/contacts.ts +0 -22
  336. package/apps/storefront/src/pages/api/admin/interaction-insights.ts +0 -101
  337. package/apps/storefront/src/pages/api/admin/orders/[id].ts +0 -63
  338. package/apps/storefront/src/pages/api/admin/orders.ts +0 -65
  339. package/apps/storefront/src/pages/api/contact.ts +0 -144
  340. package/apps/storefront/src/scheduled/contactVaultSweep.ts +0 -113
  341. package/apps/storefront/src/styles/admin.css +0 -1161
  342. package/apps/storefront/test/pipeline/fake-preview-services.ts +0 -288
  343. package/apps/storefront/test/pipeline/harness.ts +0 -209
  344. package/apps/storefront/test/pipeline/intake-worker.mjs +0 -14
  345. package/apps/storefront/test/pipeline/matrix-faults.ts +0 -366
  346. package/apps/storefront/test/pipeline/pipeline-host.ts +0 -418
  347. package/apps/storefront/test/pipeline/pool-config.cjs +0 -85
  348. package/apps/storefront/test/pipeline/publish-kinds.ts +0 -279
  349. package/apps/storefront/test/pipeline/synthetic-job.ts +0 -303
  350. package/apps/storefront/test/pipeline-faults/matrix-lib.ts +0 -696
  351. package/apps/storefront/vitest.config.cts +0 -44
  352. package/apps/storefront/vitest.faults.config.cts +0 -13
  353. package/apps/storefront/vitest.workers.config.cts +0 -7
  354. package/packages/feedback/fixtures/v1/expected-findings.json +0 -16
  355. package/packages/feedback/fixtures/v1/feedback-action/invalid/carries-transport-credential.json +0 -14
  356. package/packages/feedback/fixtures/v1/feedback-action/invalid/control-char-in-description.json +0 -13
  357. package/packages/feedback/fixtures/v1/feedback-action/invalid/legacy-schema-version.json +0 -13
  358. package/packages/feedback/fixtures/v1/feedback-action/invalid/page-path-with-query.json +0 -14
  359. package/packages/feedback/fixtures/v1/feedback-action/invalid/title-too-short.json +0 -13
  360. package/packages/feedback/fixtures/v1/feedback-action/valid/element-problem.json +0 -33
  361. package/packages/feedback/fixtures/v1/feedback-action/valid/page-comment.json +0 -20
  362. package/packages/feedback/fixtures/v1/feedback-action/valid/region-improvement.json +0 -24
  363. package/packages/feedback/fixtures/v1/feedback-action/valid/text-selection-feature.json +0 -30
  364. package/packages/feedback/fixtures/v1/submission-receipt/invalid/accepted-at-not-utc.json +0 -9
  365. package/packages/feedback/fixtures/v1/submission-receipt/invalid/bad-feedback-id.json +0 -9
  366. package/packages/feedback/fixtures/v1/submission-receipt/invalid/legacy-contract-version.json +0 -9
  367. package/packages/feedback/fixtures/v1/submission-receipt/invalid/missing-replayed.json +0 -8
  368. package/packages/feedback/fixtures/v1/submission-receipt/valid/accepted.json +0 -9
  369. package/packages/feedback/fixtures/v1/submission-receipt/valid/fractional-seconds.json +0 -9
  370. package/packages/feedback/fixtures/v1/submission-receipt/valid/replayed.json +0 -9
  371. package/packages/feedback/fixtures/v1/target-evidence/invalid/page-kind-claims-structure.json +0 -6
  372. package/packages/feedback/fixtures/v1/target-evidence/invalid/path-too-deep.json +0 -17
  373. package/packages/feedback/fixtures/v1/target-evidence/invalid/relative-region-out-of-range.json +0 -11
  374. package/packages/feedback/fixtures/v1/target-evidence/invalid/selector-uses-class.json +0 -7
  375. package/packages/feedback/fixtures/v1/target-evidence/invalid/source-precision-without-anchor.json +0 -6
  376. package/packages/feedback/fixtures/v1/target-evidence/valid/element-structure.json +0 -21
  377. package/packages/feedback/fixtures/v1/target-evidence/valid/host-iframe.json +0 -14
  378. package/packages/feedback/fixtures/v1/target-evidence/valid/source-anchor.json +0 -12
  379. package/packages/feedback/fixtures/v1/target-evidence/valid/text-selection.json +0 -26
  380. package/packages/feedback/package.json +0 -35
  381. package/packages/feedback/scripts/build-player.mjs +0 -28
  382. package/packages/feedback/src/index.ts +0 -1
  383. package/packages/feedback/src/primitives/composer.ts +0 -639
  384. package/packages/feedback/src/primitives/dialog.ts +0 -263
  385. package/packages/feedback/src/primitives/hostAdapter.ts +0 -116
  386. package/packages/feedback/src/primitives/index.ts +0 -10
  387. package/packages/feedback/src/primitives/launcher.ts +0 -200
  388. package/packages/feedback/src/primitives/picker.ts +0 -663
  389. package/packages/feedback/src/primitives/redaction.ts +0 -204
  390. package/packages/feedback/src/primitives/selection.ts +0 -49
  391. package/packages/feedback/src/primitives/shiftAccelerator.ts +0 -160
  392. package/packages/feedback/src/primitives/styles.ts +0 -77
  393. package/packages/feedback/src/primitives/surface.ts +0 -124
  394. package/packages/feedback/src/primitives/target.ts +0 -293
  395. package/packages/feedback/src/tour/canonical.ts +0 -27
  396. package/packages/feedback/src/tour/index.ts +0 -3
  397. package/packages/feedback/src/tour/player/browser.ts +0 -5
  398. package/packages/feedback/src/tour/player/host.ts +0 -12
  399. package/packages/feedback/src/tour/player/index.ts +0 -205
  400. package/packages/feedback/src/tour/player/manifest.ts +0 -76
  401. package/packages/feedback/src/tour/player/player.css +0 -130
  402. package/packages/feedback/src/tour/player/resolver.ts +0 -12
  403. package/packages/feedback/src/tour/player/surface.ts +0 -295
  404. package/packages/feedback/src/tour/player/types.ts +0 -8
  405. package/packages/feedback/src/tour/resolve/core.ts +0 -267
  406. package/packages/feedback/src/tour/resolve/dom.ts +0 -115
  407. package/packages/feedback/src/tour/resolve/index.ts +0 -29
  408. package/packages/feedback/src/tour/sanitize.ts +0 -76
  409. package/packages/feedback/src/tour/schema.ts +0 -362
  410. package/packages/feedback/src/v1/guards.ts +0 -335
  411. package/packages/feedback/src/v1/index.ts +0 -3
  412. package/packages/feedback/src/v1/protectedSurfaces.ts +0 -32
  413. package/packages/feedback/src/v1/types.ts +0 -177
  414. package/packages/feedback/tsconfig.json +0 -9
  415. package/packages/feedback/vitest.config.cts +0 -21
@@ -1,830 +0,0 @@
1
- /** Durable authority transitions and immutable control receipts, fenced by D1 CAS. */
2
- import type { Queryable, QueryStatement } from "../../catalog/d1/catalog.js";
3
- import type { CommerceStage } from "@tot/public-runtime";
4
- import {
5
- SOURCE_LEASE_MS,
6
- isHash,
7
- canonicalInstant,
8
- isSourceId,
9
- exactKeys,
10
- SOURCE_LIMIT,
11
- SourceAuthorityError,
12
- digestJson,
13
- observationId,
14
- conditionDigest,
15
- faultProof,
16
- validateManifestSet,
17
- uniqueLineage,
18
- type SourceKey,
19
- type SourceBegin,
20
- type SourceResult,
21
- type SourceHold,
22
- type ManifestCore,
23
- type BusinessPair,
24
- type BeginAck,
25
- type ResultAck,
26
- type Provenance,
27
- } from "./sourceAuthorityContract.js";
28
- interface State {
29
- version: number;
30
- revision: number;
31
- active: string | null;
32
- pending: boolean;
33
- accepted: string | null;
34
- businessDigest: string | null;
35
- pairs: BusinessPair[];
36
- expires: string | null;
37
- sync: boolean;
38
- holds: SourceHold[];
39
- budget: boolean;
40
- orderId: string | null;
41
- }
42
- export interface AuthorityRow {
43
- tenant_id: string;
44
- stage: CommerceStage;
45
- source: string;
46
- provider_store_id: string;
47
- source_order_id: string;
48
- order_id: string | null;
49
- source_state_version: number;
50
- check_revision: number;
51
- active_check_id: string | null;
52
- pending_check: number;
53
- accepted_ledger_generation: string | null;
54
- accepted_business_event_set_digest: string | null;
55
- source_proof_expires_at: string | null;
56
- source_sync_pending: number;
57
- unresolved_count: number;
58
- authority_budget: number;
59
- binding_digest: string;
60
- state_json: string;
61
- mutation_id: string;
62
- }
63
- interface Replay {
64
- issuer: string;
65
- jti: string;
66
- }
67
- const WHERE =
68
- "tenant_id=? AND stage=? AND source=? AND provider_store_id=? AND source_order_id=?";
69
- const keyParams = (k: SourceKey): unknown[] => [
70
- k.tenantId,
71
- k.stage,
72
- k.source,
73
- k.providerStoreId,
74
- k.sourceOrderId,
75
- ];
76
- const initial = (): State => ({
77
- version: 0,
78
- revision: 0,
79
- active: null,
80
- pending: true,
81
- accepted: null,
82
- businessDigest: null,
83
- pairs: [],
84
- expires: null,
85
- sync: true,
86
- holds: [],
87
- budget: false,
88
- orderId: null,
89
- });
90
- export type SourceReadiness =
91
- | "source-check-pending"
92
- | "source-ledger-conflict"
93
- | "source-authority-unreadable"
94
- | "source-authority-unavailable"
95
- | "source-sync-pending";
96
- export function authorityHolds(
97
- row: AuthorityRow | null,
98
- now: string,
99
- ): SourceReadiness[] {
100
- if (!row) return ["source-authority-unavailable"];
101
- try {
102
- const s = JSON.parse(row.state_json) as State;
103
- if (
104
- !Number.isSafeInteger(row.source_state_version) ||
105
- row.source_state_version < 0 ||
106
- !Number.isSafeInteger(row.check_revision) ||
107
- row.check_revision < 0 ||
108
- ![0, 1].includes(row.pending_check) ||
109
- ![0, 1].includes(row.source_sync_pending) ||
110
- ![0, 1].includes(row.authority_budget) ||
111
- !isSourceId(row.provider_store_id) ||
112
- !isHash(row.binding_digest) ||
113
- (row.accepted_ledger_generation !== null &&
114
- !isHash(row.accepted_ledger_generation)) ||
115
- (row.accepted_business_event_set_digest !== null &&
116
- !isHash(row.accepted_business_event_set_digest)) ||
117
- (row.source_proof_expires_at !== null &&
118
- !canonicalInstant(row.source_proof_expires_at))
119
- )
120
- return ["source-authority-unreadable"];
121
- if (
122
- Array.isArray(s.pairs) &&
123
- s.pairs.some(
124
- (p, i) =>
125
- !exactKeys(p, ["sourceEventId", "originalEventDigest"]) ||
126
- typeof p.sourceEventId !== "string" ||
127
- !/^[-A-Za-z0-9._:]{1,128}$/.test(p.sourceEventId) ||
128
- !isHash(p.originalEventDigest) ||
129
- (i > 0 && s.pairs[i - 1]!.sourceEventId >= p.sourceEventId),
130
- )
131
- )
132
- return ["source-authority-unreadable"];
133
- if (
134
- !Number.isSafeInteger(s.version) ||
135
- !Number.isSafeInteger(s.revision) ||
136
- !Array.isArray(s.holds) ||
137
- s.holds.length > SOURCE_LIMIT ||
138
- !Array.isArray(s.pairs) ||
139
- s.pairs.length > SOURCE_LIMIT ||
140
- s.version !== row.source_state_version ||
141
- s.revision !== row.check_revision ||
142
- s.active !== row.active_check_id ||
143
- Number(s.pending) !== row.pending_check ||
144
- s.accepted !== row.accepted_ledger_generation ||
145
- s.businessDigest !== row.accepted_business_event_set_digest ||
146
- s.expires !== row.source_proof_expires_at ||
147
- Number(s.sync) !== row.source_sync_pending ||
148
- s.holds.length !== row.unresolved_count ||
149
- Number(s.budget) !== row.authority_budget ||
150
- s.orderId !== row.order_id
151
- )
152
- return ["source-authority-unreadable"];
153
- } catch {
154
- return ["source-authority-unreadable"];
155
- }
156
- const holds: SourceReadiness[] = [];
157
- if (row.pending_check) holds.push("source-check-pending");
158
- if (row.unresolved_count || row.authority_budget)
159
- holds.push("source-ledger-conflict");
160
- if (
161
- !row.accepted_ledger_generation ||
162
- !row.accepted_business_event_set_digest ||
163
- !row.source_proof_expires_at ||
164
- !Number.isFinite(Date.parse(row.source_proof_expires_at)) ||
165
- Date.parse(row.source_proof_expires_at) <= Date.parse(now)
166
- )
167
- holds.push("source-authority-unavailable");
168
- if (row.source_sync_pending) holds.push("source-sync-pending");
169
- return holds;
170
- }
171
- /** SQL checks both the loaded receiver revision and its live lease at the write boundary. */
172
- export function authorityFence(
173
- row: AuthorityRow | null,
174
- now: string,
175
- requireHealthy: boolean,
176
- ): { sql: string; params: unknown[] } {
177
- if (!row)
178
- return {
179
- sql: requireHealthy
180
- ? "0"
181
- : "NOT EXISTS(SELECT 1 FROM commerce_source_authority sa WHERE sa.tenant_id=scrappy_commerce_orders.tenant_id AND sa.stage=scrappy_commerce_orders.stage AND sa.source=scrappy_commerce_orders.source AND sa.source_order_id=scrappy_commerce_orders.source_order_id)",
182
- params: [],
183
- };
184
- return {
185
- sql: `EXISTS(SELECT 1 FROM commerce_source_authority sa WHERE sa.tenant_id=scrappy_commerce_orders.tenant_id AND sa.stage=scrappy_commerce_orders.stage AND sa.source=scrappy_commerce_orders.source AND sa.source_order_id=scrappy_commerce_orders.source_order_id AND sa.order_id=scrappy_commerce_orders.order_id AND sa.provider_store_id=? AND sa.source_state_version=? AND sa.check_revision=? AND sa.binding_digest=? AND json_valid(sa.state_json)=1 ${requireHealthy ? "AND sa.pending_check=0 AND sa.unresolved_count=0 AND sa.authority_budget=0 AND sa.source_sync_pending=0 AND sa.accepted_ledger_generation IS NOT NULL AND sa.source_proof_expires_at>?" : ""})`,
186
- params: [
187
- row.provider_store_id,
188
- row.source_state_version,
189
- row.check_revision,
190
- row.binding_digest,
191
- ...(requireHealthy ? [now] : []),
192
- ],
193
- };
194
- }
195
- export class SourceAuthorityStore {
196
- constructor(private readonly db: Queryable) {}
197
- async read(key: SourceKey): Promise<AuthorityRow | null> {
198
- return this.db.first<AuthorityRow>(
199
- `SELECT * FROM commerce_source_authority WHERE ${WHERE}`,
200
- ...keyParams(key),
201
- );
202
- }
203
- async forOrder(
204
- tenant: string,
205
- stage: CommerceStage,
206
- source: string,
207
- sourceOrderId: string,
208
- ): Promise<AuthorityRow | null> {
209
- return this.db.first<AuthorityRow>(
210
- `SELECT * FROM commerce_source_authority WHERE tenant_id=? AND stage=? AND source=? AND source_order_id=?`,
211
- tenant,
212
- stage,
213
- source,
214
- sourceOrderId,
215
- );
216
- }
217
- private async consumeReplay(
218
- key: SourceKey,
219
- now: string,
220
- replay?: Replay,
221
- ): Promise<void> {
222
- if (replay)
223
- await this.db.run(
224
- `INSERT INTO scrappy_caller_replay(tenant_id,stage,issuer,jti,seen_at) VALUES(?,?,?,?,?)`,
225
- key.tenantId,
226
- key.stage,
227
- replay.issuer,
228
- replay.jti,
229
- now,
230
- );
231
- }
232
- private async record<T>(
233
- key: SourceKey,
234
- kind: string,
235
- id: string,
236
- ): Promise<{ payload_digest: string; payload: T } | null> {
237
- const r = await this.db.first<{
238
- payload_digest: string;
239
- payload_json: string;
240
- }>(
241
- `SELECT payload_digest,payload_json FROM commerce_source_observations WHERE ${WHERE} AND record_kind=? AND record_id=?`,
242
- ...keyParams(key),
243
- kind,
244
- id,
245
- );
246
- return r
247
- ? {
248
- payload_digest: r.payload_digest,
249
- payload: JSON.parse(r.payload_json) as T,
250
- }
251
- : null;
252
- }
253
- private async transition(
254
- key: SourceKey,
255
- old: AuthorityRow | null,
256
- state: State,
257
- now: string,
258
- records: { kind: string; id: string; digest: string; payload: unknown }[],
259
- replay?: Replay,
260
- resolve: string[] = [],
261
- ): Promise<boolean> {
262
- if (!this.db.batchResults)
263
- throw new SourceAuthorityError("source-authority-unavailable");
264
- const mutation = crypto.randomUUID();
265
- const binding = await digestJson({ ...key, orderId: state.orderId });
266
- const values = [
267
- state.orderId,
268
- state.version,
269
- state.revision,
270
- state.active,
271
- state.pending ? 1 : 0,
272
- state.accepted,
273
- state.businessDigest,
274
- state.expires,
275
- state.sync ? 1 : 0,
276
- state.holds.length,
277
- state.budget ? 1 : 0,
278
- binding,
279
- JSON.stringify(state),
280
- mutation,
281
- ];
282
- const statements: QueryStatement[] = [
283
- old
284
- ? {
285
- sql: `UPDATE commerce_source_authority SET order_id=?,source_state_version=?,check_revision=?,active_check_id=?,pending_check=?,accepted_ledger_generation=?,accepted_business_event_set_digest=?,source_proof_expires_at=?,source_sync_pending=?,unresolved_count=?,authority_budget=?,binding_digest=?,state_json=?,mutation_id=? WHERE ${WHERE} AND source_state_version=? AND mutation_id=? RETURNING source_state_version`,
286
- params: [
287
- ...values,
288
- ...keyParams(key),
289
- old.source_state_version,
290
- old.mutation_id,
291
- ],
292
- }
293
- : {
294
- sql: `INSERT INTO commerce_source_authority(tenant_id,stage,source,provider_store_id,source_order_id,order_id,source_state_version,check_revision,active_check_id,pending_check,accepted_ledger_generation,accepted_business_event_set_digest,source_proof_expires_at,source_sync_pending,unresolved_count,authority_budget,binding_digest,state_json,mutation_id) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ON CONFLICT DO NOTHING RETURNING source_state_version`,
295
- params: [...keyParams(key), ...values],
296
- },
297
- ];
298
- // One bounded insert persists every immutable record inside the authority CAS.
299
- statements.push({
300
- sql: `INSERT INTO commerce_source_observations(tenant_id,stage,source,provider_store_id,source_order_id,record_kind,record_id,payload_digest,payload_json)
301
- SELECT sa.tenant_id,sa.stage,sa.source,sa.provider_store_id,sa.source_order_id,json_extract(r.value,'$.kind'),json_extract(r.value,'$.id'),json_extract(r.value,'$.digest'),json_extract(r.value,'$.payloadJson')
302
- FROM commerce_source_authority sa,json_each(?) r
303
- WHERE sa.tenant_id=? AND sa.stage=? AND sa.source=? AND sa.provider_store_id=? AND sa.source_order_id=? AND sa.mutation_id=?`,
304
- params: [
305
- JSON.stringify(
306
- records.map((r) => ({
307
- kind: r.kind,
308
- id: r.id,
309
- digest: r.digest,
310
- payloadJson: JSON.stringify(r.payload),
311
- })),
312
- ),
313
- ...keyParams(key),
314
- mutation,
315
- ],
316
- });
317
- for (const r of records.filter(
318
- (r) => r.kind === "check" || r.kind === "result",
319
- )) {
320
- const controlId = `source-control:${await digestJson({ key, kind: r.kind, id: r.id })}`;
321
- statements.push({
322
- sql: `INSERT INTO scrappy_commerce_inbox(tenant_id,stage,source,source_event_id,event_version,idempotency_key,payload_digest,received_at) SELECT tenant_id,stage,source,?,1,?,?,? FROM commerce_source_authority WHERE ${WHERE} AND mutation_id=?`,
323
- params: [
324
- controlId,
325
- controlId,
326
- r.digest,
327
- now,
328
- ...keyParams(key),
329
- mutation,
330
- ],
331
- });
332
- }
333
- if (replay)
334
- statements.push({
335
- sql: `INSERT INTO scrappy_caller_replay(tenant_id,stage,issuer,jti,seen_at) SELECT tenant_id,stage,?,?,? FROM commerce_source_authority WHERE ${WHERE} AND mutation_id=?`,
336
- params: [replay.issuer, replay.jti, now, ...keyParams(key), mutation],
337
- });
338
- statements.push({
339
- sql: `UPDATE commerce_source_observations SET resolved_version=?,resolved_generation=? WHERE ${WHERE} AND record_kind='hold' AND record_id IN(SELECT value FROM json_each(?)) AND resolved_version IS NULL AND EXISTS(SELECT 1 FROM commerce_source_authority WHERE ${WHERE} AND mutation_id=?)`,
340
- params: [
341
- state.version,
342
- state.accepted,
343
- ...keyParams(key),
344
- JSON.stringify(resolve),
345
- ...keyParams(key),
346
- mutation,
347
- ],
348
- });
349
- // Every authority change invalidates a previously loaded fulfillment CAS. Terminal progress is retained.
350
- statements.push({
351
- sql: `UPDATE scrappy_commerce_orders SET event_version=event_version+1 WHERE tenant_id=? AND stage=? AND source=? AND source_order_id=? AND EXISTS(SELECT 1 FROM commerce_source_authority WHERE ${WHERE} AND mutation_id=?)`,
352
- params: [
353
- key.tenantId,
354
- key.stage,
355
- key.source,
356
- key.sourceOrderId,
357
- ...keyParams(key),
358
- mutation,
359
- ],
360
- });
361
- statements.push({
362
- sql: `INSERT INTO scrappy_shipment_audit(audit_id,tenant_id,stage,order_id,at,actor_id,kind,from_state,to_state)
363
- SELECT ?,tenant_id,stage,order_id,?,'commerce-source-authority','source-authority','source',?
364
- FROM scrappy_commerce_orders WHERE tenant_id=? AND stage=? AND source=? AND source_order_id=?
365
- AND EXISTS(SELECT 1 FROM commerce_source_authority WHERE ${WHERE} AND mutation_id=?)`,
366
- params: [
367
- `audit_${crypto.randomUUID()}`,
368
- `${now}#source-${state.version}`,
369
- state.pending || state.sync || state.holds.length || state.budget
370
- ? "held"
371
- : "verified",
372
- key.tenantId,
373
- key.stage,
374
- key.source,
375
- key.sourceOrderId,
376
- ...keyParams(key),
377
- mutation,
378
- ],
379
- });
380
- const result = await this.db.batchResults(statements);
381
- return Boolean(result[0]?.results.length);
382
- }
383
- async begin(
384
- key: SourceKey,
385
- event: SourceBegin,
386
- now: string,
387
- replay?: Replay,
388
- ): Promise<BeginAck> {
389
- for (let attempt = 0; attempt < 8; attempt++) {
390
- const old = await this.read(key);
391
- const priorBegin = await this.db.first<{ payload_json: string }>(
392
- `SELECT payload_json FROM commerce_source_observations WHERE ${WHERE} AND record_kind='check' AND json_extract(payload_json,'$.requestId')=?`,
393
- ...keyParams(key),
394
- event.requestId,
395
- );
396
- if (priorBegin) {
397
- await this.consumeReplay(key, now, replay);
398
- return (JSON.parse(priorBegin.payload_json) as { beginAck: BeginAck })
399
- .beginAck;
400
- }
401
- const state: State = old ? JSON.parse(old.state_json) : initial();
402
- if (state.active && state.pending) {
403
- const check = await this.record<{
404
- issuedAt: string;
405
- checkRevision: number;
406
- }>(key, "check", state.active);
407
- if (
408
- !check ||
409
- !canonicalInstant(check.payload.issuedAt) ||
410
- check.payload.checkRevision !== state.revision ||
411
- Date.parse(check.payload.issuedAt) > Date.parse(now)
412
- )
413
- throw new SourceAuthorityError("source-authority-unavailable");
414
- if (
415
- Date.parse(check.payload.issuedAt) + SOURCE_LEASE_MS >
416
- Date.parse(now)
417
- ) {
418
- await this.consumeReplay(key, now, replay);
419
- return {
420
- ok: true,
421
- duplicate: true,
422
- checkId: state.active,
423
- checkRevision: state.revision,
424
- sourceStateVersion: state.version,
425
- acceptedLedgerGeneration: state.accepted,
426
- acceptedBusinessEventSetDigest: state.businessDigest,
427
- unresolvedHolds: state.holds,
428
- };
429
- }
430
- }
431
- state.version++;
432
- state.revision++;
433
- state.active = `check_${crypto.randomUUID()}`;
434
- state.pending = true;
435
- const beginAck: BeginAck = {
436
- ok: true,
437
- duplicate: false,
438
- checkId: state.active,
439
- checkRevision: state.revision,
440
- sourceStateVersion: state.version,
441
- acceptedLedgerGeneration: state.accepted,
442
- acceptedBusinessEventSetDigest: state.businessDigest,
443
- unresolvedHolds: state.holds,
444
- };
445
- const check = {
446
- issuedAt: now,
447
- checkRevision: state.revision,
448
- sourceStateVersion: state.version,
449
- requestId: event.requestId,
450
- beginAck,
451
- };
452
- if (
453
- await this.transition(
454
- key,
455
- old,
456
- state,
457
- now,
458
- [
459
- {
460
- kind: "check",
461
- id: state.active,
462
- digest: await digestJson(check),
463
- payload: check,
464
- },
465
- ],
466
- replay,
467
- )
468
- )
469
- return {
470
- ok: true,
471
- duplicate: false,
472
- checkId: state.active,
473
- checkRevision: state.revision,
474
- sourceStateVersion: state.version,
475
- acceptedLedgerGeneration: state.accepted,
476
- acceptedBusinessEventSetDigest: state.businessDigest,
477
- unresolvedHolds: state.holds,
478
- };
479
- }
480
- throw new SourceAuthorityError("source-state-changed");
481
- }
482
- async result(
483
- key: SourceKey,
484
- event: SourceResult,
485
- now: string,
486
- replay?: Replay,
487
- ): Promise<ResultAck> {
488
- const digest = await digestJson(event);
489
- for (let attempt = 0; attempt < 8; attempt++) {
490
- const old = await this.read(key);
491
- if (!old) throw new SourceAuthorityError("source-authority-unavailable");
492
- const state: State = JSON.parse(old.state_json);
493
- const prior = await this.record<{ ack: ResultAck; event: SourceResult }>(
494
- key,
495
- "result",
496
- event.observationId,
497
- );
498
- if (prior) {
499
- if (prior.payload_digest === digest) {
500
- await this.consumeReplay(key, now, replay);
501
- return prior.payload.ack;
502
- }
503
- await this.conflict(key, old, state, now);
504
- throw new SourceAuthorityError("control-identity-conflict");
505
- }
506
- if ((await observationId(key, event)) !== event.observationId) {
507
- await this.conflict(key, old, state, now);
508
- throw new SourceAuthorityError("control-identity-conflict");
509
- }
510
- const check = await this.record<{
511
- issuedAt: string;
512
- checkRevision: number;
513
- sourceStateVersion: number;
514
- }>(key, "check", event.checkId);
515
- if (
516
- !check ||
517
- check.payload.checkRevision !== event.expectedCheckRevision ||
518
- event.expectedSourceStateVersion < check.payload.sourceStateVersion ||
519
- event.expectedSourceStateVersion > state.version
520
- )
521
- throw new SourceAuthorityError("source-state-changed");
522
- const records: {
523
- kind: string;
524
- id: string;
525
- digest: string;
526
- payload: unknown;
527
- }[] = [];
528
- let resolves: string[] = [];
529
- if (event.outcome === "fault") {
530
- const proof = faultProof(event);
531
- const { conditionDigest: _, ...facts } = proof;
532
- if ((await conditionDigest(key, facts)) !== event.conditionDigest)
533
- throw new SourceAuthorityError("malformed");
534
- const manifests = event.completeValidatedManifestSetOrNull
535
- ? await validateManifestSet(
536
- key,
537
- event.completeValidatedManifestSetOrNull,
538
- )
539
- : new Map<string, ManifestCore>();
540
- if (
541
- event.faultKind === "fork" &&
542
- (!event.completeValidatedManifestSetOrNull ||
543
- !event.conflictingValidatedGenerationsOrNull ||
544
- event.conflictingValidatedGenerationsOrNull.some(
545
- (id) =>
546
- !manifests.has(id) ||
547
- manifests.get(id)!.parent !== event.divergenceParentOrNull,
548
- ))
549
- )
550
- throw new SourceAuthorityError("malformed");
551
- for (const [id, c] of manifests)
552
- if (!(await this.record(key, "manifest", id)))
553
- records.push({ kind: "manifest", id, digest: id, payload: c });
554
- let changed = false;
555
- if (
556
- !state.holds.some((h) => h.conditionDigest === event.conditionDigest)
557
- ) {
558
- changed = true;
559
- if (state.holds.length >= SOURCE_LIMIT) state.budget = true;
560
- else {
561
- const hold: SourceHold = {
562
- holdId: `hold_${crypto.randomUUID()}`,
563
- conditionDigest: event.conditionDigest,
564
- faultKind: event.faultKind,
565
- openingObservationId: event.observationId,
566
- openingProof: proof,
567
- };
568
- state.holds.push(hold);
569
- records.push({
570
- kind: "hold",
571
- id: hold.holdId,
572
- digest: await digestJson(hold),
573
- payload: hold,
574
- });
575
- }
576
- }
577
- if (changed) state.version++;
578
- } else {
579
- if (
580
- state.active !== event.checkId ||
581
- !state.pending ||
582
- state.version !== event.expectedSourceStateVersion ||
583
- state.revision !== event.expectedCheckRevision
584
- )
585
- throw new SourceAuthorityError("source-state-changed");
586
- const issued = Date.parse(check.payload.issuedAt);
587
- if (
588
- Date.parse(event.actualObservedAt) < issued ||
589
- Date.parse(event.actualObservedAt) > Date.parse(now) ||
590
- issued + SOURCE_LEASE_MS <= Date.parse(now)
591
- )
592
- throw new SourceAuthorityError("source-authority-unavailable");
593
- const map = await validateManifestSet(
594
- key,
595
- event.validatedManifestCores,
596
- );
597
- if (
598
- !uniqueLineage(map, event.tipGeneration) ||
599
- (state.accepted && !map.has(state.accepted))
600
- )
601
- throw new SourceAuthorityError("source-state-changed");
602
- const retained = await this.db.all<{ record_id: string }>(
603
- `SELECT record_id FROM commerce_source_observations WHERE ${WHERE} AND record_kind='manifest'`,
604
- ...keyParams(key),
605
- );
606
- if (retained.some((r) => !map.has(r.record_id)))
607
- throw new SourceAuthorityError("source-state-changed");
608
- if (
609
- state.budget ||
610
- state.holds.some(
611
- (h) =>
612
- ![
613
- "source-unavailable",
614
- "census-incomplete",
615
- "census-changing",
616
- "committed-payload-missing",
617
- ].includes(h.faultKind) ||
618
- (h.faultKind === "committed-payload-missing" &&
619
- h.openingProof.completeValidatedManifestSetOrNull === null),
620
- )
621
- )
622
- throw new SourceAuthorityError("source-state-changed");
623
- const expected = state.holds.map((h) => h.holdId).sort();
624
- if (
625
- JSON.stringify(expected) !==
626
- JSON.stringify([...event.resolvesHoldIds].sort())
627
- )
628
- throw new SourceAuthorityError("source-state-changed");
629
- if (
630
- (await digestJson(event.businessEventDigestPairs)) !==
631
- event.businessEventSetDigest
632
- )
633
- throw new SourceAuthorityError("malformed");
634
- // A new accepted source set must preserve all immutable business identities already admitted.
635
- if (
636
- state.pairs.some(
637
- (p) =>
638
- !event.businessEventDigestPairs.some(
639
- (q) =>
640
- q.sourceEventId === p.sourceEventId &&
641
- q.originalEventDigest === p.originalEventDigest,
642
- ),
643
- )
644
- )
645
- throw new SourceAuthorityError("source-state-changed");
646
- const persisted = await this.persistedPairs(key);
647
- if (
648
- persisted.some(
649
- (p) =>
650
- !event.businessEventDigestPairs.some(
651
- (q) =>
652
- q.sourceEventId === p.sourceEventId &&
653
- q.originalEventDigest === p.originalEventDigest,
654
- ),
655
- )
656
- )
657
- throw new SourceAuthorityError("source-state-changed");
658
- for (const [id, c] of map)
659
- if (!(await this.record(key, "manifest", id)))
660
- records.push({ kind: "manifest", id, digest: id, payload: c });
661
- resolves = expected;
662
- state.holds = [];
663
- state.version++;
664
- state.pending = false;
665
- state.accepted = event.tipGeneration;
666
- state.businessDigest = event.businessEventSetDigest;
667
- state.pairs = event.businessEventDigestPairs;
668
- state.expires = new Date(issued + SOURCE_LEASE_MS).toISOString();
669
- state.sync = (await digestJson(persisted)) !== state.businessDigest;
670
- }
671
- const ack: ResultAck = {
672
- ok: true,
673
- duplicate: false,
674
- checkId: event.checkId,
675
- observationId: event.observationId,
676
- sourceStateVersion: state.version,
677
- acceptedLedgerGeneration: state.accepted,
678
- acceptedBusinessEventSetDigest: state.businessDigest,
679
- sourceProofExpiresAt: state.expires,
680
- pendingCheck: state.pending,
681
- unresolvedHoldIdsDigest: await digestJson(
682
- state.holds.map((h) => h.holdId).sort(),
683
- ),
684
- sourceSyncPending: state.sync,
685
- };
686
- records.push({
687
- kind: "result",
688
- id: event.observationId,
689
- digest,
690
- payload: { event, ack },
691
- });
692
- // A result receipt shares the exact authority transaction; ACK loss is safely retryable.
693
- try {
694
- if (
695
- await this.transition(key, old, state, now, records, replay, resolves)
696
- )
697
- return ack;
698
- } catch (error) {
699
- const receipt = await this.record<{
700
- ack: ResultAck;
701
- event: SourceResult;
702
- }>(key, "result", event.observationId);
703
- if (receipt?.payload_digest === digest) return receipt.payload.ack;
704
- throw error;
705
- }
706
- }
707
- throw new SourceAuthorityError("source-state-changed");
708
- }
709
- async budget(key: SourceKey, now: string): Promise<void> {
710
- for (let n = 0; n < 8; n++) {
711
- const old = await this.read(key);
712
- const s: State = old ? JSON.parse(old.state_json) : initial();
713
- s.budget = true;
714
- s.version++;
715
- if (await this.transition(key, old, s, now, [])) return;
716
- }
717
- throw new SourceAuthorityError("source-state-changed");
718
- }
719
- private async conflict(
720
- key: SourceKey,
721
- old: AuthorityRow,
722
- state: State,
723
- now: string,
724
- ): Promise<void> {
725
- state.version++;
726
- state.budget = true;
727
- if (!(await this.transition(key, old, state, now, []))) {
728
- const current = await this.read(key);
729
- if (!current)
730
- throw new SourceAuthorityError("source-authority-unavailable");
731
- return this.conflict(key, current, JSON.parse(current.state_json), now);
732
- }
733
- }
734
- async persistedPairs(key: SourceKey): Promise<BusinessPair[]> {
735
- const rows = await this.db.all<{
736
- source_event_id: string;
737
- payload_digest: string;
738
- }>(
739
- `SELECT i.source_event_id,i.payload_digest FROM scrappy_commerce_inbox i JOIN scrappy_commerce_outbox o ON o.tenant_id=i.tenant_id AND o.stage=i.stage AND o.source=i.source AND o.source_event_id=i.source_event_id WHERE o.tenant_id=? AND o.stage=? AND o.source=? AND json_extract(o.payload,'$.source.sourceOrderId')=? ORDER BY i.source_event_id`,
740
- key.tenantId,
741
- key.stage,
742
- key.source,
743
- key.sourceOrderId,
744
- );
745
- return rows.map((r) => ({
746
- sourceEventId: r.source_event_id,
747
- originalEventDigest: r.payload_digest,
748
- }));
749
- }
750
- async authorizeEvent(
751
- tenantId: string,
752
- stage: CommerceStage,
753
- event: {
754
- source: string;
755
- sourceOrderId: string;
756
- orderId: string;
757
- sourceEventId: string;
758
- },
759
- provenance: Provenance,
760
- ): Promise<AuthorityRow> {
761
- const row = await this.forOrder(
762
- tenantId,
763
- stage,
764
- event.source,
765
- event.sourceOrderId,
766
- );
767
- if (!row) throw new SourceAuthorityError("source-authority-unavailable");
768
- const state: State = JSON.parse(row.state_json);
769
- const expected = state.pairs.find(
770
- (p) => p.sourceEventId === event.sourceEventId,
771
- );
772
- if (
773
- expected &&
774
- expected.originalEventDigest !== provenance.exactOriginalEventDigest
775
- ) {
776
- await this.budget(
777
- {
778
- tenantId,
779
- stage,
780
- source: "foxycart",
781
- providerStoreId: row.provider_store_id,
782
- sourceOrderId: row.source_order_id,
783
- },
784
- new Date().toISOString(),
785
- );
786
- throw new SourceAuthorityError("control-identity-conflict");
787
- }
788
- if (
789
- (row.order_id !== null && row.order_id !== event.orderId) ||
790
- event.orderId !== event.sourceOrderId ||
791
- state.accepted !== provenance.acceptedLedgerGeneration ||
792
- state.businessDigest !== provenance.businessEventSetDigest ||
793
- !state.pairs.some(
794
- (p) =>
795
- p.sourceEventId === event.sourceEventId &&
796
- p.originalEventDigest === provenance.exactOriginalEventDigest,
797
- )
798
- )
799
- throw new SourceAuthorityError("source-state-changed");
800
- return row;
801
- }
802
- /** Part of the business commit transaction: sync reflects the exact committed immutable set. */
803
- async businessStatements(
804
- row: AuthorityRow,
805
- orderId: string,
806
- ): Promise<QueryStatement[]> {
807
- const key: SourceKey = {
808
- tenantId: row.tenant_id,
809
- stage: row.stage,
810
- source: "foxycart",
811
- providerStoreId: row.provider_store_id,
812
- sourceOrderId: row.source_order_id,
813
- };
814
- return [
815
- {
816
- sql: `UPDATE commerce_source_authority SET source_state_version=source_state_version+1,mutation_id=?,order_id=?,binding_digest=?,source_sync_pending=CASE WHEN (SELECT COUNT(*) FROM json_each(state_json,'$.pairs'))=(SELECT COUNT(*) FROM scrappy_commerce_outbox o WHERE o.tenant_id=commerce_source_authority.tenant_id AND o.stage=commerce_source_authority.stage AND o.source=commerce_source_authority.source AND json_extract(o.payload,'$.source.sourceOrderId')=commerce_source_authority.source_order_id) AND NOT EXISTS(SELECT 1 FROM json_each(state_json,'$.pairs') p WHERE NOT EXISTS(SELECT 1 FROM scrappy_commerce_inbox i WHERE i.tenant_id=commerce_source_authority.tenant_id AND i.stage=commerce_source_authority.stage AND i.source=commerce_source_authority.source AND i.source_event_id=json_extract(p.value,'$.sourceEventId') AND i.payload_digest=json_extract(p.value,'$.originalEventDigest'))) THEN 0 ELSE 1 END WHERE ${WHERE}`,
817
- params: [
818
- crypto.randomUUID(),
819
- orderId,
820
- await digestJson({ ...key, orderId }),
821
- ...keyParams(key),
822
- ],
823
- },
824
- {
825
- sql: `UPDATE commerce_source_authority SET state_json=json_set(state_json,'$.version',source_state_version,'$.orderId',order_id,'$.sync',json(CASE WHEN source_sync_pending=0 THEN 'false' ELSE 'true' END)) WHERE ${WHERE}`,
826
- params: keyParams(key),
827
- },
828
- ];
829
- }
830
- }