@cosmicdrift/kumiko-samples 0.413.0 → 0.414.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 (459) hide show
  1. package/package.json +1 -1
  2. package/packages/bundled-features/package.json +16 -1
  3. package/packages/bundled-features/src/admin-shell/changes.json +6 -0
  4. package/packages/bundled-features/src/admin-shell/feature.ts +8 -7
  5. package/packages/bundled-features/src/agent-tools/agent-doc-lint.ts +5 -1
  6. package/packages/bundled-features/src/agent-tools/agent-manifest.ts +36 -27
  7. package/packages/bundled-features/src/agent-tools/api-docs/collect-events.ts +8 -1
  8. package/packages/bundled-features/src/agent-tools/api-docs/collect.ts +3 -2
  9. package/packages/bundled-features/src/agent-tools/api-docs/generate.ts +6 -0
  10. package/packages/bundled-features/src/agent-tools/api-docs/index.ts +1 -0
  11. package/packages/bundled-features/src/agent-tools/api-docs/problems.ts +38 -1
  12. package/packages/bundled-features/src/agent-tools/api-docs/render-openapi.ts +1 -0
  13. package/packages/bundled-features/src/agent-tools/api-docs/resolve-config.ts +17 -0
  14. package/packages/bundled-features/src/agent-tools/api-docs/scope-slug-collisions.ts +17 -0
  15. package/packages/bundled-features/src/agent-tools/api-docs/translation-gaps.ts +42 -0
  16. package/packages/bundled-features/src/agent-tools/api-docs/webhook-example-problems.ts +30 -0
  17. package/packages/bundled-features/src/agent-tools/changes.json +12 -0
  18. package/packages/bundled-features/src/agent-tools/index.ts +1 -0
  19. package/packages/bundled-features/src/agent-tools/labels.ts +19 -6
  20. package/packages/bundled-features/src/agent-tools/tool-catalog.ts +18 -7
  21. package/packages/bundled-features/src/agent-tools/tool-dispatch.ts +14 -1
  22. package/packages/bundled-features/src/agent-tools/types.ts +4 -0
  23. package/packages/bundled-features/src/audit/event-classification.ts +6 -0
  24. package/packages/bundled-features/src/audit/web/i18n.ts +1 -0
  25. package/packages/bundled-features/src/auth-email-password/changes.json +18 -0
  26. package/packages/bundled-features/src/auth-email-password/errors.ts +0 -13
  27. package/packages/bundled-features/src/auth-email-password/handlers/invite-signup-complete.write.ts +147 -121
  28. package/packages/bundled-features/src/auth-email-password/handlers/login-options.query.ts +1 -1
  29. package/packages/bundled-features/src/auth-email-password/handlers/request-account-unlock.write.ts +1 -1
  30. package/packages/bundled-features/src/auth-email-password/handlers/request-email-verification.write.ts +0 -1
  31. package/packages/bundled-features/src/auth-email-password/handlers/request-password-reset.write.ts +0 -1
  32. package/packages/bundled-features/src/auth-email-password/handlers/self-registration-status.query.ts +1 -1
  33. package/packages/bundled-features/src/auth-email-password/reset-token.ts +0 -2
  34. package/packages/bundled-features/src/auth-email-password/seeding.ts +39 -7
  35. package/packages/bundled-features/src/auth-email-password/unlock-token.ts +0 -2
  36. package/packages/bundled-features/src/auth-email-password/verification-token.ts +0 -2
  37. package/packages/bundled-features/src/auth-email-password/web/auth-form-primitives.tsx +2 -2
  38. package/packages/bundled-features/src/auth-email-password/web/auth-gate.tsx +2 -2
  39. package/packages/bundled-features/src/auth-email-password/web/auth-redirect.ts +5 -0
  40. package/packages/bundled-features/src/auth-email-password/web/forgot-password-screen.tsx +1 -6
  41. package/packages/bundled-features/src/auth-email-password/web/invite-accept-screen.tsx +1 -1
  42. package/packages/bundled-features/src/auth-email-password/web/return-path.ts +2 -1
  43. package/packages/bundled-features/src/auth-email-password/web/session.tsx +10 -12
  44. package/packages/bundled-features/src/auth-email-password/web/signup-screen.tsx +1 -6
  45. package/packages/bundled-features/src/auth-email-password/web/static-account.ts +6 -1
  46. package/packages/bundled-features/src/auth-email-password/web/tenant-switcher.tsx +1 -1
  47. package/packages/bundled-features/src/auth-email-password/web/token-confirm-screen.tsx +4 -5
  48. package/packages/bundled-features/src/auth-email-password/web/user-menu.tsx +1 -1
  49. package/packages/bundled-features/src/auth-mfa/web/mfa-code-or-recovery-field.tsx +17 -1
  50. package/packages/bundled-features/src/auth-mfa/web/mfa-disable-dialog.tsx +1 -1
  51. package/packages/bundled-features/src/auth-mfa/web/mfa-regenerate-recovery-dialog.tsx +1 -1
  52. package/packages/bundled-features/src/auth-mfa/web/mfa-setup-preauth-screen.tsx +1 -1
  53. package/packages/bundled-features/src/auth-mfa/web/mfa-verify-screen.tsx +16 -47
  54. package/packages/bundled-features/src/auth-mfa-user-data/index.ts +5 -6
  55. package/packages/bundled-features/src/billing-foundation/aggregate-id.ts +0 -4
  56. package/packages/bundled-features/src/billing-foundation/changes.json +37 -0
  57. package/packages/bundled-features/src/billing-foundation/checkout-core.ts +8 -1
  58. package/packages/bundled-features/src/billing-foundation/checkout-ledger.ts +59 -0
  59. package/packages/bundled-features/src/billing-foundation/consumer-protection/consent-gate.ts +2 -1
  60. package/packages/bundled-features/src/billing-foundation/consumer-protection/consent-text.ts +49 -2
  61. package/packages/bundled-features/src/billing-foundation/consumer-protection/termination-open-cancels.ts +5 -0
  62. package/packages/bundled-features/src/billing-foundation/consumer-protection/termination-provider-timeout.ts +23 -0
  63. package/packages/bundled-features/src/billing-foundation/consumer-protection/termination-record.ts +12 -7
  64. package/packages/bundled-features/src/billing-foundation/consumer-protection/termination-revoke.ts +1 -1
  65. package/packages/bundled-features/src/billing-foundation/events.ts +49 -10
  66. package/packages/bundled-features/src/billing-foundation/feature.ts +15 -1
  67. package/packages/bundled-features/src/billing-foundation/handlers/billing-plans.query.ts +47 -14
  68. package/packages/bundled-features/src/billing-foundation/handlers/create-checkout-session.write.ts +7 -1
  69. package/packages/bundled-features/src/billing-foundation/handlers/process-event.write.ts +124 -10
  70. package/packages/bundled-features/src/billing-foundation/handlers/process-payment-event.write.ts +33 -9
  71. package/packages/bundled-features/src/billing-foundation/handlers/record-checkout-opened.write.ts +25 -0
  72. package/packages/bundled-features/src/billing-foundation/handlers/start-plan-checkout.write.ts +7 -1
  73. package/packages/bundled-features/src/billing-foundation/handlers/switch-plan.write.ts +16 -4
  74. package/packages/bundled-features/src/billing-foundation/i18n.ts +3 -0
  75. package/packages/bundled-features/src/billing-foundation/pending-plan-switch.ts +80 -0
  76. package/packages/bundled-features/src/billing-foundation/scrub-raw-payload-headers.ts +50 -0
  77. package/packages/bundled-features/src/billing-foundation/types.ts +47 -30
  78. package/packages/bundled-features/src/billing-foundation/validate-options.ts +18 -0
  79. package/packages/bundled-features/src/billing-foundation/web/billing-plans-panel.tsx +10 -5
  80. package/packages/bundled-features/src/billing-foundation/web/cancel-contract-dialog.tsx +1 -1
  81. package/packages/bundled-features/src/billing-foundation/web/checkout-consent-dialog.tsx +1 -1
  82. package/packages/bundled-features/src/billing-foundation/web/constants.ts +5 -2
  83. package/packages/bundled-features/src/billing-foundation/web/plan-switch-confirm-dialog.tsx +1 -1
  84. package/packages/bundled-features/src/billing-foundation/webhook-handler.ts +65 -17
  85. package/packages/bundled-features/src/cap-counter/aggregate-id.ts +0 -2
  86. package/packages/bundled-features/src/cap-counter/changes.json +6 -0
  87. package/packages/bundled-features/src/cap-counter/stock-cap-guard.ts +19 -4
  88. package/packages/bundled-features/src/cap-overview/changes.json +6 -0
  89. package/packages/bundled-features/src/cap-overview/handlers/tenant-caps-list.query.ts +22 -6
  90. package/packages/bundled-features/src/cap-overview/i18n.ts +1 -0
  91. package/packages/bundled-features/src/cap-overview/index.ts +1 -0
  92. package/packages/bundled-features/src/cap-overview/types.ts +11 -1
  93. package/packages/bundled-features/src/cap-overview/web/cap-cards-panel.tsx +6 -2
  94. package/packages/bundled-features/src/cap-overview/web/cap-usage-bar.tsx +10 -1
  95. package/packages/bundled-features/src/cap-overview/web/usage-math.ts +5 -0
  96. package/packages/bundled-features/src/channel-in-app/changes.json +6 -0
  97. package/packages/bundled-features/src/channel-in-app/handlers/read-state-by-keys.query.ts +2 -2
  98. package/packages/bundled-features/src/channel-whatsapp/aggregate-id.ts +29 -0
  99. package/packages/bundled-features/src/channel-whatsapp/changes.json +8 -0
  100. package/packages/bundled-features/src/channel-whatsapp/constants.ts +46 -0
  101. package/packages/bundled-features/src/channel-whatsapp/events.ts +93 -0
  102. package/packages/bundled-features/src/channel-whatsapp/feature.ts +72 -0
  103. package/packages/bundled-features/src/channel-whatsapp/graph-client.ts +398 -0
  104. package/packages/bundled-features/src/channel-whatsapp/in-memory-meta.ts +300 -0
  105. package/packages/bundled-features/src/channel-whatsapp/inbound-handlers.ts +189 -0
  106. package/packages/bundled-features/src/channel-whatsapp/index.ts +66 -0
  107. package/packages/bundled-features/src/channel-whatsapp/meta-signature.ts +26 -0
  108. package/packages/bundled-features/src/channel-whatsapp/options.ts +149 -0
  109. package/packages/bundled-features/src/channel-whatsapp/pii.ts +109 -0
  110. package/packages/bundled-features/src/channel-whatsapp/webhook-normalize.ts +226 -0
  111. package/packages/bundled-features/src/channel-whatsapp/webhook-routes.ts +185 -0
  112. package/packages/bundled-features/src/compliance-profiles/handlers/sub-processors.query.ts +1 -1
  113. package/packages/bundled-features/src/config/changes.json +12 -0
  114. package/packages/bundled-features/src/config/handlers/set.write.ts +7 -0
  115. package/packages/bundled-features/src/config/handlers/values.query.ts +8 -1
  116. package/packages/bundled-features/src/config/index.ts +1 -0
  117. package/packages/bundled-features/src/config/read-redaction.ts +6 -3
  118. package/packages/bundled-features/src/config/table.ts +1 -0
  119. package/packages/bundled-features/src/config/write-helpers.ts +3 -2
  120. package/packages/bundled-features/src/crypto-shredding/changes.json +18 -0
  121. package/packages/bundled-features/src/crypto-shredding/constants.ts +2 -0
  122. package/packages/bundled-features/src/crypto-shredding/handlers/forget-subject.write.ts +38 -18
  123. package/packages/bundled-features/src/crypto-shredding/index.ts +4 -0
  124. package/packages/bundled-features/src/crypto-shredding/subject-forget-after-delete-hook.ts +49 -0
  125. package/packages/bundled-features/src/custom-fields/aggregate-id.ts +0 -1
  126. package/packages/bundled-features/src/custom-fields/entity.ts +1 -0
  127. package/packages/bundled-features/src/custom-fields/wire-for-entity.ts +0 -1
  128. package/packages/bundled-features/src/data-retention/changes.json +18 -0
  129. package/packages/bundled-features/src/data-retention/erase-notes-of-deleted-row.ts +23 -0
  130. package/packages/bundled-features/src/data-retention/feature.ts +4 -7
  131. package/packages/bundled-features/src/data-retention/run-retention-cleanup.ts +38 -18
  132. package/packages/bundled-features/src/data-retention/tenant-destruction-hold.ts +9 -0
  133. package/packages/bundled-features/src/delivery/attempt-log-factory.ts +11 -0
  134. package/packages/bundled-features/src/delivery/attempt-log.ts +3 -3
  135. package/packages/bundled-features/src/delivery/changes.json +6 -0
  136. package/packages/bundled-features/src/delivery/channel-context.ts +6 -3
  137. package/packages/bundled-features/src/delivery/db/queries/preferences.ts +0 -1
  138. package/packages/bundled-features/src/delivery/delivery-service.ts +3 -0
  139. package/packages/bundled-features/src/delivery/feature.ts +4 -13
  140. package/packages/bundled-features/src/delivery/handlers/opted-in-user-ids.query.ts +45 -0
  141. package/packages/bundled-features/src/delivery/handlers/preferences.query.ts +2 -20
  142. package/packages/bundled-features/src/delivery/handlers/resubscribe-user.write.ts +2 -25
  143. package/packages/bundled-features/src/delivery/handlers/unsubscribe-page-chrome.query.ts +2 -0
  144. package/packages/bundled-features/src/delivery/handlers/unsubscribe-user.write.ts +2 -27
  145. package/packages/bundled-features/src/delivery/index.ts +3 -1
  146. package/packages/bundled-features/src/delivery/options.ts +11 -0
  147. package/packages/bundled-features/src/delivery/preference-rows.ts +33 -0
  148. package/packages/bundled-features/src/delivery/set-preference-enabled.ts +40 -0
  149. package/packages/bundled-features/src/delivery/testing.ts +3 -1
  150. package/packages/bundled-features/src/delivery/types.ts +1 -0
  151. package/packages/bundled-features/src/delivery/unsubscribe-page.ts +7 -1
  152. package/packages/bundled-features/src/delivery/upsert-preference.ts +0 -1
  153. package/packages/bundled-features/src/delivery/web/public-names.ts +1 -0
  154. package/packages/bundled-features/src/derivatives-sharp/render.ts +9 -9
  155. package/packages/bundled-features/src/document-ingest-foundation/aggregate-id.ts +0 -1
  156. package/packages/bundled-features/src/document-ingest-foundation/append-missing-extract-event.ts +37 -25
  157. package/packages/bundled-features/src/document-ingest-foundation/report-ingest-progress.ts +32 -14
  158. package/packages/bundled-features/src/documents/aggregate-id.ts +0 -2
  159. package/packages/bundled-features/src/documents/changes.json +18 -0
  160. package/packages/bundled-features/src/documents/constants.ts +10 -2
  161. package/packages/bundled-features/src/documents/ensure-link.ts +3 -4
  162. package/packages/bundled-features/src/documents/feature.ts +28 -6
  163. package/packages/bundled-features/src/documents/handlers/import.write.ts +2 -2
  164. package/packages/bundled-features/src/documents/queries/expiry-overview.query.ts +2 -1
  165. package/packages/bundled-features/src/documents/queries/for-record.query.ts +3 -4
  166. package/packages/bundled-features/src/documents/queries/list-page.ts +11 -3
  167. package/packages/bundled-features/src/documents/queries/overview.query.ts +2 -2
  168. package/packages/bundled-features/src/documents/screens.ts +8 -3
  169. package/packages/bundled-features/src/documents/tab.ts +1 -1
  170. package/packages/bundled-features/src/feature-toggles/toggle-sync-signal.ts +6 -3
  171. package/packages/bundled-features/src/file-derivatives/handlers/public-variant-by-file-ref.query.ts +1 -1
  172. package/packages/bundled-features/src/file-derivatives/handlers/public-variant.query.ts +1 -1
  173. package/packages/bundled-features/src/file-foundation/probe.ts +9 -3
  174. package/packages/bundled-features/src/file-foundation/read-file-ref-content.ts +24 -11
  175. package/packages/bundled-features/src/file-provider-inmemory/feature.ts +1 -1
  176. package/packages/bundled-features/src/file-provider-s3/changes.json +14 -0
  177. package/packages/bundled-features/src/file-provider-s3/env-feature.ts +1 -1
  178. package/packages/bundled-features/src/file-provider-s3/env-named-feature.ts +1 -1
  179. package/packages/bundled-features/src/file-provider-s3/env-schema.ts +24 -7
  180. package/packages/bundled-features/src/file-provider-s3/feature.ts +1 -1
  181. package/packages/bundled-features/src/file-provider-s3/index.ts +7 -1
  182. package/packages/bundled-features/src/file-provider-s3/lib/env-helper.ts +21 -13
  183. package/packages/bundled-features/src/file-provider-s3/lib/s3-provider.ts +8 -1
  184. package/packages/bundled-features/src/files/changes.json +7 -0
  185. package/packages/bundled-features/src/folders/aggregate-id.ts +0 -1
  186. package/packages/bundled-features/src/folders/web/folder-manager.tsx +6 -6
  187. package/packages/bundled-features/src/folders-user-data/index.ts +12 -14
  188. package/packages/bundled-features/src/helpdesk/changes.json +62 -0
  189. package/packages/bundled-features/src/helpdesk/entity.ts +4 -1
  190. package/packages/bundled-features/src/helpdesk/feature.ts +41 -0
  191. package/packages/bundled-features/src/helpdesk/handlers/access.ts +0 -10
  192. package/packages/bundled-features/src/helpdesk/handlers/attempts.ts +7 -14
  193. package/packages/bundled-features/src/helpdesk/handlers/challenge.query.ts +1 -2
  194. package/packages/bundled-features/src/helpdesk/handlers/reply.write.ts +2 -9
  195. package/packages/bundled-features/src/helpdesk/handlers/report.write.ts +2 -9
  196. package/packages/bundled-features/src/helpdesk/handlers/submit.write.ts +3 -11
  197. package/packages/bundled-features/src/helpdesk/index.ts +1 -0
  198. package/packages/bundled-features/src/helpdesk/legacy-contact-config.ts +61 -0
  199. package/packages/bundled-features/src/helpdesk/legacy-contact-erasure.ts +45 -0
  200. package/packages/bundled-features/src/helpdesk/legacy-contact-retention.ts +65 -0
  201. package/packages/bundled-features/src/helpdesk/legacy-contact-streams.ts +185 -0
  202. package/packages/bundled-features/src/helpdesk/legacy-contact-user-data.ts +84 -0
  203. package/packages/bundled-features/src/helpdesk/lib.ts +47 -34
  204. package/packages/bundled-features/src/helpdesk/user-data-hooks.ts +89 -48
  205. package/packages/bundled-features/src/helpdesk/web/constants.ts +0 -1
  206. package/packages/bundled-features/src/inbound-mail-foundation/account-health.ts +1 -1
  207. package/packages/bundled-features/src/inbound-mail-foundation/account-poll.ts +5 -1
  208. package/packages/bundled-features/src/inbound-mail-foundation/aggregate-id.ts +0 -2
  209. package/packages/bundled-features/src/inbound-mail-foundation/changes.json +48 -0
  210. package/packages/bundled-features/src/inbound-mail-foundation/constants.ts +6 -0
  211. package/packages/bundled-features/src/inbound-mail-foundation/content-refetch.ts +15 -1
  212. package/packages/bundled-features/src/inbound-mail-foundation/entities.ts +64 -15
  213. package/packages/bundled-features/src/inbound-mail-foundation/events.ts +15 -16
  214. package/packages/bundled-features/src/inbound-mail-foundation/feature.ts +3 -1
  215. package/packages/bundled-features/src/inbound-mail-foundation/handlers/account-address.ts +16 -2
  216. package/packages/bundled-features/src/inbound-mail-foundation/handlers/attach-message-content.write.ts +10 -3
  217. package/packages/bundled-features/src/inbound-mail-foundation/handlers/connect-account.write.ts +21 -8
  218. package/packages/bundled-features/src/inbound-mail-foundation/handlers/forget-message.write.ts +73 -0
  219. package/packages/bundled-features/src/inbound-mail-foundation/i18n.ts +3 -0
  220. package/packages/bundled-features/src/inbound-mail-foundation/index.ts +1 -0
  221. package/packages/bundled-features/src/inbound-mail-foundation/oauth-access-token.ts +1 -1
  222. package/packages/bundled-features/src/inbound-mail-foundation/outbound-mail.ts +2 -0
  223. package/packages/bundled-features/src/inbound-mail-foundation/personal-address.ts +20 -0
  224. package/packages/bundled-features/src/inbound-mail-foundation/purge-messages.ts +54 -0
  225. package/packages/bundled-features/src/inbound-mail-foundation/quarantine.ts +15 -5
  226. package/packages/bundled-features/src/inbound-mail-foundation/retention-job.ts +1 -5
  227. package/packages/bundled-features/src/inbound-mail-foundation/retention-sweep.ts +21 -34
  228. package/packages/bundled-features/src/inbound-mail-foundation/send-from-account.ts +9 -1
  229. package/packages/bundled-features/src/inbound-mail-foundation/supervisor-ingest.ts +7 -1
  230. package/packages/bundled-features/src/inbound-mail-foundation/sync-account-now.ts +1 -1
  231. package/packages/bundled-features/src/inbound-mail-foundation/tenant-destroy-hook.ts +14 -17
  232. package/packages/bundled-features/src/inbound-mail-foundation/watch-supervisor.ts +1 -1
  233. package/packages/bundled-features/src/inbound-provider-imap/byte-budget.ts +80 -0
  234. package/packages/bundled-features/src/inbound-provider-imap/changes.json +13 -0
  235. package/packages/bundled-features/src/inbound-provider-imap/connect-mailbox.ts +8 -4
  236. package/packages/bundled-features/src/inbound-provider-imap/connect-my-mailbox.ts +23 -6
  237. package/packages/bundled-features/src/inbound-provider-imap/connect-target-rate-limit.ts +46 -0
  238. package/packages/bundled-features/src/inbound-provider-imap/download-batch.ts +47 -10
  239. package/packages/bundled-features/src/inbound-provider-imap/feature.ts +7 -4
  240. package/packages/bundled-features/src/inbound-provider-imap/fetch-content.ts +43 -29
  241. package/packages/bundled-features/src/inbound-provider-imap/i18n.ts +2 -2
  242. package/packages/bundled-features/src/inbound-provider-imap/parse-message.ts +14 -0
  243. package/packages/bundled-features/src/inbound-provider-imap/reconnect-mailbox.ts +5 -4
  244. package/packages/bundled-features/src/inbound-provider-imap/send-mail.ts +39 -7
  245. package/packages/bundled-features/src/inbound-provider-imap/smtp-client.ts +19 -4
  246. package/packages/bundled-features/src/inbound-provider-imap/watch-mailbox.ts +8 -23
  247. package/packages/bundled-features/src/inbound-provider-inmemory/feature.ts +1 -1
  248. package/packages/bundled-features/src/jobs/events.ts +10 -0
  249. package/packages/bundled-features/src/jobs/feature.ts +2 -0
  250. package/packages/bundled-features/src/jobs/handlers/consumer.write.ts +41 -16
  251. package/packages/bundled-features/src/jobs/web/constants.ts +4 -0
  252. package/packages/bundled-features/src/mail-foundation/changes.json +6 -0
  253. package/packages/bundled-features/src/mail-transport-inmemory/feature.ts +1 -1
  254. package/packages/bundled-features/src/mail-transport-smtp/feature.ts +3 -2
  255. package/packages/bundled-features/src/managed-pages/changes.json +12 -0
  256. package/packages/bundled-features/src/managed-pages/handlers/branding.query.ts +1 -1
  257. package/packages/bundled-features/src/managed-pages/handlers/by-slug.query.ts +1 -1
  258. package/packages/bundled-features/src/managed-pages/handlers/by-tenant-published.query.ts +1 -1
  259. package/packages/bundled-features/src/managed-pages/render-page-body.ts +18 -10
  260. package/packages/bundled-features/src/managed-pages/screens/page-screens.ts +18 -38
  261. package/packages/bundled-features/src/managed-pages/table.ts +1 -0
  262. package/packages/bundled-features/src/managed-pages/web/client-plugin.tsx +8 -6
  263. package/packages/bundled-features/src/managed-pages/web/constants.ts +8 -1
  264. package/packages/bundled-features/src/managed-pages/web/i18n.ts +11 -3
  265. package/packages/bundled-features/src/managed-pages/web/markdown-to-document.ts +1 -1
  266. package/packages/bundled-features/src/managed-pages/web/page-edit-draft.ts +108 -0
  267. package/packages/bundled-features/src/managed-pages/web/page-edit-screen.tsx +198 -0
  268. package/packages/bundled-features/src/managed-pages/web/page-settings-rail.tsx +117 -0
  269. package/packages/bundled-features/src/notes-history-user-data/index.ts +8 -14
  270. package/packages/bundled-features/src/personal-access-tokens/boot-rules.ts +5 -0
  271. package/packages/bundled-features/src/personal-access-tokens/handlers/create.write.ts +0 -1
  272. package/packages/bundled-features/src/personal-access-tokens/scope-events-check.ts +31 -5
  273. package/packages/bundled-features/src/presence/changes.json +6 -0
  274. package/packages/bundled-features/src/presence/handlers/list.query.ts +28 -11
  275. package/packages/bundled-features/src/presence/room.ts +21 -7
  276. package/packages/bundled-features/src/presence/store.ts +79 -21
  277. package/packages/bundled-features/src/readiness/changes.json +25 -0
  278. package/packages/bundled-features/src/readiness/handlers/latest-summary.query.ts +11 -6
  279. package/packages/bundled-features/src/readiness/handlers/run-probes.write.ts +35 -5
  280. package/packages/bundled-features/src/readiness/handlers/run-tone.ts +3 -2
  281. package/packages/bundled-features/src/readiness/handlers/status.query.ts +15 -1
  282. package/packages/bundled-features/src/readiness/probe-context.ts +11 -10
  283. package/packages/bundled-features/src/readiness/screens.ts +4 -4
  284. package/packages/bundled-features/src/readiness/visible-results.ts +5 -3
  285. package/packages/bundled-features/src/renderer-foundation/api.ts +6 -13
  286. package/packages/bundled-features/src/renderer-foundation/types.ts +2 -2
  287. package/packages/bundled-features/src/secrets/changes.json +7 -0
  288. package/packages/bundled-features/src/secrets/db/queries/read.ts +18 -4
  289. package/packages/bundled-features/src/secrets/handlers/list.query.ts +8 -2
  290. package/packages/bundled-features/src/secrets/secret-scope.ts +2 -3
  291. package/packages/bundled-features/src/secrets/secrets-context.ts +6 -4
  292. package/packages/bundled-features/src/secrets/write-gate.ts +4 -2
  293. package/packages/bundled-features/src/seo/handlers/seo-config.query.ts +1 -1
  294. package/packages/bundled-features/src/shared/acknowledged-system-db.ts +20 -0
  295. package/packages/bundled-features/src/shared/assert-billing-live.ts +9 -2
  296. package/packages/bundled-features/src/shared/changes.json +6 -0
  297. package/packages/bundled-features/src/shared/index.ts +6 -1
  298. package/packages/bundled-features/src/shared/parent-visibility.ts +25 -1
  299. package/packages/bundled-features/src/shared/password-hashing.ts +0 -1
  300. package/packages/bundled-features/src/shared/price-env-shape.ts +1 -4
  301. package/packages/bundled-features/src/shared/register-user-data-hooks.ts +29 -0
  302. package/packages/bundled-features/src/shared/require-bootstrap-extension.ts +26 -0
  303. package/packages/bundled-features/src/shared/send-best-effort.ts +19 -0
  304. package/packages/bundled-features/src/shared/stream-tenant-backfill.ts +22 -1
  305. package/packages/bundled-features/src/shared/stream-tenant-derived-state.ts +31 -1
  306. package/packages/bundled-features/src/step-dispatcher/changes.json +12 -0
  307. package/packages/bundled-features/src/step-dispatcher/constants.ts +13 -0
  308. package/packages/bundled-features/src/step-dispatcher/deliver-dispatch-request.ts +47 -2
  309. package/packages/bundled-features/src/step-dispatcher/feature.ts +28 -1
  310. package/packages/bundled-features/src/step-dispatcher/mail-runner.ts +0 -1
  311. package/packages/bundled-features/src/step-dispatcher/sweep-abandoned.ts +57 -0
  312. package/packages/bundled-features/src/subscription-mollie/changes.json +7 -0
  313. package/packages/bundled-features/src/subscription-mollie/plugin-methods.ts +5 -3
  314. package/packages/bundled-features/src/subscription-mollie/verify-webhook.ts +5 -5
  315. package/packages/bundled-features/src/subscription-paddle/backfill-checkout-ledger.ts +159 -0
  316. package/packages/bundled-features/src/subscription-paddle/changes.json +13 -0
  317. package/packages/bundled-features/src/subscription-paddle/checkout-config.query.ts +1 -1
  318. package/packages/bundled-features/src/subscription-paddle/checkout-methods.ts +2 -8
  319. package/packages/bundled-features/src/subscription-paddle/constants.ts +1 -2
  320. package/packages/bundled-features/src/subscription-paddle/feature.ts +26 -24
  321. package/packages/bundled-features/src/subscription-paddle/ledger-backfill-marker.ts +70 -0
  322. package/packages/bundled-features/src/subscription-paddle/plan-switch-methods.ts +2 -8
  323. package/packages/bundled-features/src/subscription-paddle/redeliver-legacy-checkout.ts +36 -0
  324. package/packages/bundled-features/src/subscription-paddle/runtime.ts +13 -24
  325. package/packages/bundled-features/src/subscription-paddle/subscription-state.ts +1 -0
  326. package/packages/bundled-features/src/subscription-paddle/webhook-events.ts +68 -21
  327. package/packages/bundled-features/src/subscription-stripe/changes.json +7 -0
  328. package/packages/bundled-features/src/subscription-stripe/feature.ts +5 -0
  329. package/packages/bundled-features/src/subscription-stripe/plugin-methods.ts +6 -6
  330. package/packages/bundled-features/src/subscription-stripe/runtime.ts +8 -7
  331. package/packages/bundled-features/src/subscription-stripe/verify-webhook.ts +24 -6
  332. package/packages/bundled-features/src/tags/aggregate-id.ts +0 -1
  333. package/packages/bundled-features/src/tags/web/tag-chip.tsx +4 -5
  334. package/packages/bundled-features/src/tags/web/tag-manager.tsx +2 -1
  335. package/packages/bundled-features/src/template-resolver/api.ts +6 -13
  336. package/packages/bundled-features/src/template-resolver/changes.json +36 -0
  337. package/packages/bundled-features/src/template-resolver/document-sanitizer.ts +25 -0
  338. package/packages/bundled-features/src/template-resolver/feature.ts +2 -0
  339. package/packages/bundled-features/src/template-resolver/handlers/body-preview.query.ts +41 -0
  340. package/packages/bundled-features/src/template-resolver/handlers/by-slug.query.ts +1 -1
  341. package/packages/bundled-features/src/template-resolver/handlers/by-tenant.query.ts +1 -1
  342. package/packages/bundled-features/src/template-resolver/handlers/collection-shared.ts +5 -11
  343. package/packages/bundled-features/src/template-resolver/handlers/overview.query.ts +3 -2
  344. package/packages/bundled-features/src/template-resolver/handlers/send-test.write.ts +37 -8
  345. package/packages/bundled-features/src/template-resolver/linked-resources.ts +2 -5
  346. package/packages/bundled-features/src/template-resolver/seeding.ts +77 -0
  347. package/packages/bundled-features/src/template-resolver/table.ts +1 -0
  348. package/packages/bundled-features/src/template-resolver/template-draft.ts +2 -1
  349. package/packages/bundled-features/src/template-resolver/template-preview.ts +14 -3
  350. package/packages/bundled-features/src/template-resolver/testing.ts +0 -2
  351. package/packages/bundled-features/src/template-resolver/web/client-plugin.tsx +3 -0
  352. package/packages/bundled-features/src/template-resolver/web/collection-entry-create-form.tsx +33 -33
  353. package/packages/bundled-features/src/template-resolver/web/constants.ts +7 -0
  354. package/packages/bundled-features/src/template-resolver/web/content-tree-provider.ts +0 -1
  355. package/packages/bundled-features/src/template-resolver/web/content-workbench.tsx +142 -0
  356. package/packages/bundled-features/src/template-resolver/web/document-insert-model.ts +3 -8
  357. package/packages/bundled-features/src/template-resolver/web/document-link-policy.ts +58 -0
  358. package/packages/bundled-features/src/template-resolver/web/document-schema.ts +11 -57
  359. package/packages/bundled-features/src/template-resolver/web/document-tiptap-bubble.tsx +2 -4
  360. package/packages/bundled-features/src/template-resolver/web/document-tiptap-context.ts +4 -5
  361. package/packages/bundled-features/src/template-resolver/web/document-tiptap-editor.tsx +2 -2
  362. package/packages/bundled-features/src/template-resolver/web/document-tiptap-helpers.ts +6 -3
  363. package/packages/bundled-features/src/template-resolver/web/document-tiptap-toolbar-groups.tsx +2 -3
  364. package/packages/bundled-features/src/template-resolver/web/editor-handle.ts +1 -0
  365. package/packages/bundled-features/src/template-resolver/web/fill-document-variables.ts +24 -0
  366. package/packages/bundled-features/src/template-resolver/web/i18n.ts +14 -0
  367. package/packages/bundled-features/src/template-resolver/web/index.ts +3 -0
  368. package/packages/bundled-features/src/template-resolver/web/preview-targets.ts +25 -0
  369. package/packages/bundled-features/src/template-resolver/web/qualified-names.ts +1 -0
  370. package/packages/bundled-features/src/template-resolver/web/snippet-preview-renderer.tsx +24 -0
  371. package/packages/bundled-features/src/template-resolver/web/string-values.ts +7 -0
  372. package/packages/bundled-features/src/template-resolver/web/text-block-editor.tsx +38 -50
  373. package/packages/bundled-features/src/template-resolver/web/use-collection-entry-create.ts +7 -0
  374. package/packages/bundled-features/src/template-resolver/web/use-content-write.ts +1 -3
  375. package/packages/bundled-features/src/template-resolver/web/use-document-tiptap-model.ts +1 -2
  376. package/packages/bundled-features/src/template-resolver/web/use-text-block-editor.ts +28 -7
  377. package/packages/bundled-features/src/template-resolver/web/web-preview-renderer.tsx +126 -0
  378. package/packages/bundled-features/src/template-resolver-user-data/index.ts +11 -10
  379. package/packages/bundled-features/src/tenant/changes.json +6 -0
  380. package/packages/bundled-features/src/tenant/handlers/create.write.ts +7 -8
  381. package/packages/bundled-features/src/tenant/handlers/list.query.ts +7 -9
  382. package/packages/bundled-features/src/tenant/handlers/member-directory.query.ts +10 -4
  383. package/packages/bundled-features/src/tenant/handlers/toggle-enabled.write.ts +1 -1
  384. package/packages/bundled-features/src/tenant/invited-membership.ts +10 -1
  385. package/packages/bundled-features/src/tenant/seeding.ts +4 -2
  386. package/packages/bundled-features/src/tenant/stream-tenant-backfill.ts +5 -1
  387. package/packages/bundled-features/src/tenant/testing.ts +1 -1
  388. package/packages/bundled-features/src/tenant-inactivity/check-inactive-tenants.ts +4 -3
  389. package/packages/bundled-features/src/tenant-inactivity/constants.ts +1 -2
  390. package/packages/bundled-features/src/tenant-inactivity/handlers/keep-account.write.ts +1 -2
  391. package/packages/bundled-features/src/tenant-inactivity/state-store.ts +10 -1
  392. package/packages/bundled-features/src/tenant-lifecycle/changes.json +18 -0
  393. package/packages/bundled-features/src/tenant-lifecycle/constants.ts +4 -0
  394. package/packages/bundled-features/src/tenant-lifecycle/run-tenant-destroy.ts +43 -6
  395. package/packages/bundled-features/src/tenant-lifecycle/stages.ts +15 -2
  396. package/packages/bundled-features/src/tier-engine/aggregate-id.ts +0 -1
  397. package/packages/bundled-features/src/tier-engine/changes.json +6 -0
  398. package/packages/bundled-features/src/tier-engine/feature.ts +8 -2
  399. package/packages/bundled-features/src/user/changes.json +6 -0
  400. package/packages/bundled-features/src/user/db/queries/stream-tenant-backfill.ts +2 -1
  401. package/packages/bundled-features/src/user/handlers/me.query.ts +3 -1
  402. package/packages/bundled-features/src/user/index.ts +1 -0
  403. package/packages/bundled-features/src/user/resolve-verified-user-email.ts +17 -0
  404. package/packages/bundled-features/src/user-data-rights/db/queries/export-jobs.ts +0 -1
  405. package/packages/bundled-features/src/user-data-rights/handlers/confirm-deletion-by-token.write.ts +1 -1
  406. package/packages/bundled-features/src/user-data-rights/handlers/download-by-token.query.ts +1 -1
  407. package/packages/bundled-features/src/user-data-rights/handlers/request-deletion-by-email.write.ts +1 -1
  408. package/packages/bundled-features/src/user-data-rights/run-export-jobs.ts +0 -2
  409. package/packages/bundled-features/src/user-data-rights/run-forget-cleanup.ts +3 -3
  410. package/packages/bundled-features/src/user-data-rights/schema/download-attempt.ts +3 -2
  411. package/packages/bundled-features/src/user-data-rights/web/confirm-deletion-screen.tsx +1 -1
  412. package/packages/bundled-features/src/user-data-rights/web/privacy-center-screen.tsx +1 -1
  413. package/packages/bundled-features/src/user-data-rights/web/request-deletion-screen.tsx +1 -1
  414. package/packages/bundled-features/src/user-data-rights-defaults/hooks/api-token.userdata-hook.ts +5 -5
  415. package/packages/bundled-features/src/user-data-rights-defaults/hooks/hard-delete-own-rows-hook.ts +17 -0
  416. package/packages/bundled-features/src/user-data-rights-defaults/hooks/in-app-message.userdata-hook.ts +5 -5
  417. package/packages/bundled-features/src/user-data-rights-defaults/hooks/job-run.userdata-hook.ts +2 -1
  418. package/packages/bundled-features/src/user-data-rights-defaults/hooks/user-session.userdata-hook.ts +5 -5
  419. package/packages/bundled-features/src/user-profile/handlers/change-email.write.ts +0 -1
  420. package/packages/bundled-features/src/waitlist/changes.json +6 -0
  421. package/packages/bundled-features/src/waitlist/handlers/submit.write.ts +4 -28
  422. package/packages/bundled-features/src/waitlist/lib.ts +2 -7
  423. package/packages/bundled-features/src/waitlist/user-data-hooks.ts +6 -19
  424. package/packages/bundled-features/src/workflow-runner/aggregate-id.ts +0 -1
  425. package/packages/bundled-features/src/workflow-runner/event-trigger.ts +6 -2
  426. package/packages/bundled-features/src/workflow-runner/feature.ts +1 -5
  427. package/packages/bundled-features/src/workflow-runner/handlers/resume-run.write.ts +5 -4
  428. package/packages/bundled-features/src/workflow-runner/runner.ts +2 -0
  429. package/samples/apps/showcase/src/features/demos/web/pages/dialog.tsx +0 -1
  430. package/samples/apps/showcase/src/features/demos/web/pages/layout.tsx +1 -1
  431. package/samples/apps/showcase/src/features/demos/web/pages/sidebar-panel.tsx +0 -1
  432. package/samples/apps/styleguide/src/features/gallery/web/Gallery.tsx +1 -4
  433. package/samples/apps/ui-walkthrough/src/features/subscription-plans/feature.ts +6 -2
  434. package/samples/apps/use-all-bundled/e2e-billing-gallery/gallery-features.ts +12 -14
  435. package/samples/apps/use-all-bundled/feature-manifest.json +60 -29
  436. package/samples/apps/use-all-bundled/playwright.billing-docs.config.ts +1 -1
  437. package/samples/apps/use-all-bundled/playwright.config.ts +1 -0
  438. package/samples/apps/use-all-bundled/src/app/seed.ts +1 -1
  439. package/samples/apps/use-all-bundled/src/run-config.ts +2 -1
  440. package/samples/recipes/auth-mfa/src/web/confirmation-code-step.tsx +32 -10
  441. package/samples/recipes/chat-channels/README.md +2 -0
  442. package/samples/recipes/currencies-global/src/entities/currency.ts +1 -0
  443. package/samples/recipes/helpdesk/README.md +9 -5
  444. package/samples/recipes/helpdesk/src/feature.ts +6 -3
  445. package/samples/recipes/managed-pages/README.md +5 -1
  446. package/samples/recipes/managed-pages/package.json +3 -2
  447. package/samples/recipes/managed-pages/src/web/branded-preview.tsx +21 -0
  448. package/samples/recipes/managed-pages/tsconfig.json +8 -0
  449. package/samples/recipes/pwa-shell/README.md +52 -0
  450. package/samples/recipes/pwa-shell/bin/server.ts +12 -0
  451. package/samples/recipes/pwa-shell/package.json +13 -0
  452. package/samples/recipes/pwa-shell/src/app.ts +34 -0
  453. package/samples/recipes/rate-limiting/README.md +3 -3
  454. package/samples/recipes/record-detail-layout/README.md +33 -0
  455. package/samples/recipes/record-detail-layout/src/feature.ts +525 -44
  456. package/samples/recipes/record-detail-layout/src/order-board.ts +521 -0
  457. package/packages/bundled-features/src/managed-pages/web/page-body-section.tsx +0 -72
  458. package/packages/bundled-features/src/subscription-paddle/tenant-binding.ts +0 -34
  459. package/packages/bundled-features/src/template-resolver/web/content-field.tsx +0 -61
@@ -0,0 +1,54 @@
1
+ import type { TenantId } from "@cosmicdrift/kumiko-framework/engine";
2
+ import { deleteRowsWithoutReplayableStream } from "@cosmicdrift/kumiko-framework/event-store";
3
+ import { inboundMessagesProjectionTable } from "./projection.ts";
4
+
5
+ export type PurgeableMessageRow = { readonly id: string; readonly bodyRef: string | null };
6
+
7
+ export interface PurgeInboundMessagesArgs {
8
+ readonly db: Parameters<typeof deleteRowsWithoutReplayableStream>[0];
9
+ readonly tenantId: TenantId;
10
+ readonly rows: readonly PurgeableMessageRow[];
11
+ readonly archive: (messageId: string) => Promise<void>;
12
+ readonly deleteBodyObject?: ((bodyRef: string) => Promise<void>) | undefined;
13
+ readonly deleteAttachments?: ((messageId: string) => Promise<void>) | undefined;
14
+ }
15
+
16
+ export interface PurgeInboundMessagesResult {
17
+ readonly purgedIds: readonly string[];
18
+ readonly failedIds: readonly string[];
19
+ readonly bodyObjectsDeleted: number;
20
+ }
21
+
22
+ // Archive first, delete the row last and strictly by the archived ids: a crash in between leaves an
23
+ // archived stream the next run finishes, and a row is never deleted while its stream could replay.
24
+ // A row whose body or attachment delete fails stays, otherwise the only reference to the object is lost.
25
+ export async function purgeInboundMessages(
26
+ args: PurgeInboundMessagesArgs,
27
+ ): Promise<PurgeInboundMessagesResult> {
28
+ const purgedCandidates: string[] = [];
29
+ const failedIds: string[] = [];
30
+ let bodyObjectsDeleted = 0;
31
+ for (const row of args.rows) {
32
+ await args.archive(row.id);
33
+ try {
34
+ if (row.bodyRef && args.deleteBodyObject) {
35
+ await args.deleteBodyObject(row.bodyRef);
36
+ bodyObjectsDeleted++;
37
+ }
38
+ await args.deleteAttachments?.(row.id);
39
+ } catch {
40
+ failedIds.push(row.id);
41
+ continue;
42
+ }
43
+ purgedCandidates.push(row.id);
44
+ }
45
+ const purgedIds =
46
+ purgedCandidates.length === 0
47
+ ? []
48
+ : await deleteRowsWithoutReplayableStream(args.db, {
49
+ table: inboundMessagesProjectionTable,
50
+ ids: purgedCandidates,
51
+ tenantId: args.tenantId,
52
+ });
53
+ return { purgedIds, failedIds, bodyObjectsDeleted };
54
+ }
@@ -22,12 +22,15 @@ export async function quarantineMessage(
22
22
  target: QuarantineTarget,
23
23
  reasonCode: QuarantineReasonCode,
24
24
  ): Promise<boolean> {
25
- const key = { accountId: target.accountId, providerMessageId: target.providerMessageId };
25
+ const key = {
26
+ tenantId: target.tenantId,
27
+ accountId: target.accountId,
28
+ providerMessageId: target.providerMessageId,
29
+ };
26
30
  if (await fetchOne<{ id: string }>(db, quarantineTable, key)) return false;
27
31
  try {
28
32
  await insertOne(db, quarantineTable, {
29
33
  id: crypto.randomUUID(),
30
- tenantId: target.tenantId,
31
34
  ...key,
32
35
  reasonCode,
33
36
  quarantinedAt: Temporal.Now.instant().toString(),
@@ -40,9 +43,12 @@ export async function quarantineMessage(
40
43
  }
41
44
  }
42
45
 
46
+ // tenantId leads the lookups: the unique index and the tenant index are the only ones on the table.
47
+ type QuarantineAccount = Pick<QuarantineTarget, "tenantId" | "accountId">;
48
+
43
49
  export async function quarantinedMessageIds(
44
50
  db: DbConnection,
45
- accountId: string,
51
+ { tenantId, accountId }: QuarantineAccount,
46
52
  providerMessageIds: readonly string[],
47
53
  ): Promise<ReadonlySet<string>> {
48
54
  const ids = new Set<string>();
@@ -51,6 +57,7 @@ export async function quarantinedMessageIds(
51
57
  db,
52
58
  quarantineTable,
53
59
  {
60
+ tenantId,
54
61
  accountId,
55
62
  providerMessageId: { in: providerMessageIds.slice(start, start + LOOKUP_CHUNK_SIZE) },
56
63
  },
@@ -61,11 +68,14 @@ export async function quarantinedMessageIds(
61
68
  return ids;
62
69
  }
63
70
 
64
- export async function hasOpenQuarantine(db: DbConnection, accountId: string): Promise<boolean> {
71
+ export async function hasOpenQuarantine(
72
+ db: DbConnection,
73
+ { tenantId, accountId }: QuarantineAccount,
74
+ ): Promise<boolean> {
65
75
  const rows = await selectMany<{ id: string }>(
66
76
  db,
67
77
  quarantineTable,
68
- { accountId, acknowledgedAt: null },
78
+ { tenantId, accountId, acknowledgedAt: null },
69
79
  { fields: ["id"], limit: 1 },
70
80
  );
71
81
  return rows.length > 0;
@@ -26,11 +26,7 @@ export function registerInboundMailRetentionJob(r: FeatureRegistrar<"inbound-mai
26
26
  if (!ctx.registry) {
27
27
  throw new Error("inbound-mail-retention: ctx.registry required (JobContext incomplete)");
28
28
  }
29
- const tenantId = ctx.systemUser?.tenantId ?? ctx._tenantId;
30
- if (tenantId === undefined) {
31
- // skip: cron fired without a perTenant fan-out tenant — nothing scoped
32
- return;
33
- }
29
+ const tenantId = ctx.systemUser.tenantId;
34
30
  const T = (await import("@cosmicdrift/kumiko-framework/time")).getTemporal();
35
31
  const retentionDb = ctx.db.unsafeRaw() as DbConnection; // @cast-boundary db-operator — jobs never run inside a DbTx
36
32
 
@@ -40,15 +40,13 @@
40
40
 
41
41
  import { type DbRunner, deleteMany, selectMany } from "@cosmicdrift/kumiko-framework/db";
42
42
  import type { TenantId } from "@cosmicdrift/kumiko-framework/engine";
43
- import {
44
- archiveStream,
45
- deleteRowsWithoutReplayableStream,
46
- } from "@cosmicdrift/kumiko-framework/event-store";
43
+ import { archiveStream } from "@cosmicdrift/kumiko-framework/event-store";
47
44
  import type { getTemporal } from "@cosmicdrift/kumiko-framework/time";
48
45
  import { seenMessageTable } from "./entities.ts";
49
46
  import { INBOUND_MESSAGE_AGGREGATE_TYPE } from "./events.ts";
50
47
  import { ingestFailureTable } from "./ingest-failures.ts";
51
48
  import { inboundMessagesProjectionTable } from "./projection.ts";
49
+ import { purgeInboundMessages } from "./purge-messages.ts";
52
50
  import { QUARANTINE_ACKNOWLEDGED_RETENTION_DAYS } from "./quarantine.ts";
53
51
  import { quarantineTable } from "./quarantine-entities.ts";
54
52
 
@@ -150,36 +148,25 @@ async function purgeExpiredMessages(
150
148
  ).filter((row) => !failedIds.has(row.id));
151
149
  if (rows.length === 0) break;
152
150
 
153
- const ids: string[] = [];
154
- for (const row of rows) {
155
- await archiveStream(args.db, {
156
- tenantId: args.tenantId,
157
- aggregateId: row.id,
158
- aggregateType: INBOUND_MESSAGE_AGGREGATE_TYPE,
159
- archivedBy: ARCHIVED_BY,
160
- reason: REASON,
161
- });
162
- try {
163
- if (row.bodyRef && args.deleteBodyObject) {
164
- await args.deleteBodyObject(row.bodyRef);
165
- bodyObjectsDeleted++;
166
- }
167
- await args.deleteAttachments?.(row.id);
168
- } catch {
169
- bodyObjectErrors++;
170
- failedIds.add(row.id);
171
- continue;
172
- }
173
- ids.push(row.id);
174
- }
175
- if (ids.length > 0) {
176
- const deleted = await deleteRowsWithoutReplayableStream(args.db, {
177
- table: inboundMessagesProjectionTable,
178
- ids,
179
- tenantId: args.tenantId,
180
- });
181
- messagesPurged += deleted.length;
182
- }
151
+ const purged = await purgeInboundMessages({
152
+ db: args.db,
153
+ tenantId: args.tenantId,
154
+ rows,
155
+ archive: (messageId) =>
156
+ archiveStream(args.db, {
157
+ tenantId: args.tenantId,
158
+ aggregateId: messageId,
159
+ aggregateType: INBOUND_MESSAGE_AGGREGATE_TYPE,
160
+ archivedBy: ARCHIVED_BY,
161
+ reason: REASON,
162
+ }),
163
+ deleteBodyObject: args.deleteBodyObject,
164
+ deleteAttachments: args.deleteAttachments,
165
+ });
166
+ messagesPurged += purged.purgedIds.length;
167
+ bodyObjectsDeleted += purged.bodyObjectsDeleted;
168
+ bodyObjectErrors += purged.failedIds.length;
169
+ for (const id of purged.failedIds) failedIds.add(id);
183
170
 
184
171
  if (rows.length < BATCH_LIMIT) break;
185
172
  }
@@ -93,6 +93,10 @@ function isSendableBy(row: MailAccountRow, user: SendMailFromAccountContext["use
93
93
  return row.status !== InboundMailAccountStatuses.disconnected && isVisibleToCaller(row, user);
94
94
  }
95
95
 
96
+ // Sends immediately and cannot be rolled back. Call it from a handler's `beforeTransaction`
97
+ // (its ctx fits here): in the handler body the request transaction would stay open for the whole
98
+ // SMTP/IMAP round trip, and a later rollback would not recall the mail. Pass the result on as the
99
+ // hook's data; a client retry after a failed handler still sends a second copy.
96
100
  export async function sendMailFromAccount(
97
101
  ctx: SendMailFromAccountContext,
98
102
  input: SendMailFromAccountInput,
@@ -123,7 +127,11 @@ export async function sendMailFromAccount(
123
127
  const sent = await plugin.send(providerCtx, account, message.data, resolved.attachments);
124
128
  return {
125
129
  isSuccess: true,
126
- data: { messageIdHeader: sent.messageIdHeader, attachmentFileIds: resolved.fileIds },
130
+ data: {
131
+ messageIdHeader: sent.messageIdHeader,
132
+ attachmentFileIds: resolved.fileIds,
133
+ ...(sent.rejectedRecipients && { rejectedRecipients: sent.rejectedRecipients }),
134
+ },
127
135
  };
128
136
  } catch (err) {
129
137
  const failure = sendFailureOf(err);
@@ -181,7 +181,11 @@ export function createMessageIngester(deps: IngesterDeps): MessageIngester {
181
181
  ): Promise<Attempt> {
182
182
  try {
183
183
  const msg = normalizeRawInboundMessage(raw);
184
- const bodyRef = await storeBodyOf(account, raw);
184
+ // Normalization drops an oversized original; the hook must not store what ingest then discards.
185
+ const bodyRef = await storeBodyOf(
186
+ account,
187
+ msg.rawMime === null ? { ...raw, rawMime: null } : raw,
188
+ );
185
189
  const result = await deps.dispatchWrite({
186
190
  handlerQn: InboundMailFoundationHandlers.ingestMessage,
187
191
  tenantId: account.tenantId,
@@ -273,6 +277,8 @@ export function createMessageIngester(deps: IngesterDeps): MessageIngester {
273
277
  let storageHealthy = options.proof?.storageHealthy ?? false;
274
278
  const failures: Failure[] = [];
275
279
  for (const msg of msgs) {
280
+ // An abandoned sync (deadline) must stop storing mail while the next sync of the account runs.
281
+ options.signal?.throwIfAborted();
276
282
  const result = await attempt(account, msg, cursorSnapshot);
277
283
  if (result.kind === "permanent") {
278
284
  const headerOnly = headerOnlyOf(msg);
@@ -24,7 +24,7 @@ export function runExclusivePerAccount(
24
24
  run: () => Promise<void>,
25
25
  ): Promise<void> {
26
26
  const running = inFlight.get(accountId);
27
- if (running) return running;
27
+ if (running !== undefined) return running;
28
28
  const started = run().finally(() => {
29
29
  inFlight.delete(accountId);
30
30
  });
@@ -15,7 +15,6 @@
15
15
  // tenant subject key, making event-log payloads AND any ciphertext copies
16
16
  // unreadable. These hooks dispose of the rows.
17
17
 
18
- import type { EntityTableMeta } from "@cosmicdrift/kumiko-framework/db";
19
18
  import type { TenantDataHookCtx } from "@cosmicdrift/kumiko-framework/engine";
20
19
  import { archiveAndDeleteTenantRows } from "../tenant-lifecycle/index.ts";
21
20
  import { seenMessageTable, syncCursorTable } from "./entities.ts";
@@ -36,24 +35,12 @@ import { quarantineTable } from "./quarantine-entities.ts";
36
35
  export const INBOUND_MAIL_TENANT_DESTROY_ARCHIVE_REASON =
37
36
  "tenant-destroy archives each inbound-mail entity's event-streams before the projection rows are deleted";
38
37
 
39
- function archiveAndDeleteRows(
40
- ctx: TenantDataHookCtx,
41
- table: EntityTableMeta,
42
- aggregateType: string,
43
- ): Promise<readonly string[]> {
44
- return archiveAndDeleteTenantRows(
45
- ctx,
46
- table,
47
- aggregateType,
48
- INBOUND_MAIL_TENANT_DESTROY_ARCHIVE_REASON,
49
- );
50
- }
51
-
52
38
  export async function mailAccountTenantDestroyHook(ctx: TenantDataHookCtx): Promise<void> {
53
- const accountIds = await archiveAndDeleteRows(
39
+ const accountIds = await archiveAndDeleteTenantRows(
54
40
  ctx,
55
41
  mailAccountsProjectionTable,
56
42
  MAIL_ACCOUNT_AGGREGATE_TYPE,
43
+ INBOUND_MAIL_TENANT_DESTROY_ARCHIVE_REASON,
57
44
  );
58
45
  for (const accountId of accountIds) {
59
46
  await ctx.db.deleteMany(syncCursorTable, { accountId });
@@ -64,9 +51,19 @@ export async function mailAccountTenantDestroyHook(ctx: TenantDataHookCtx): Prom
64
51
  }
65
52
 
66
53
  export async function inboundMessageTenantDestroyHook(ctx: TenantDataHookCtx): Promise<void> {
67
- await archiveAndDeleteRows(ctx, inboundMessagesProjectionTable, INBOUND_MESSAGE_AGGREGATE_TYPE);
54
+ await archiveAndDeleteTenantRows(
55
+ ctx,
56
+ inboundMessagesProjectionTable,
57
+ INBOUND_MESSAGE_AGGREGATE_TYPE,
58
+ INBOUND_MAIL_TENANT_DESTROY_ARCHIVE_REASON,
59
+ );
68
60
  }
69
61
 
70
62
  export async function mailThreadTenantDestroyHook(ctx: TenantDataHookCtx): Promise<void> {
71
- await archiveAndDeleteRows(ctx, mailThreadsProjectionTable, MAIL_THREAD_AGGREGATE_TYPE);
63
+ await archiveAndDeleteTenantRows(
64
+ ctx,
65
+ mailThreadsProjectionTable,
66
+ MAIL_THREAD_AGGREGATE_TYPE,
67
+ INBOUND_MAIL_TENANT_DESTROY_ARCHIVE_REASON,
68
+ );
72
69
  }
@@ -484,7 +484,7 @@ export function createInboundMailSupervisor(
484
484
  clearTimeout(pollTimer);
485
485
  pollTimer = null;
486
486
  }
487
- if (pollInFlight) await pollInFlight;
487
+ if (pollInFlight !== null) await pollInFlight;
488
488
  await Promise.allSettled([...inFlightSyncs.values(), watchCatchUp.settled()]);
489
489
  for (const accountId of [...watchers.keys()]) {
490
490
  await stopWatcher(accountId);
@@ -0,0 +1,80 @@
1
+ import {
2
+ INBOUND_MAIL_ATTACHMENTS_MAX_BYTES,
3
+ type RejectedInboundMessage,
4
+ } from "@cosmicdrift/kumiko-bundled-features/inbound-mail-foundation";
5
+ import type { ImapFlow } from "imapflow";
6
+ import { coerceDate, rejectedWithoutSource } from "./parse-message.ts";
7
+
8
+ /** Raw sources held in memory at once per download: a mailbox full of large mails must not grow the heap with the batch count. */
9
+ export const IMAP_BATCH_MAX_BYTES = 100 * 1024 * 1024;
10
+
11
+ export type ByteBudgetPlan = {
12
+ /** Fits the budget: download the source. */
13
+ readonly download: readonly number[];
14
+ /** Above the per-mail limit: never downloaded, reported with its envelope only. */
15
+ readonly tooLarge: readonly RejectedInboundMessage[];
16
+ /** Left for a later round once the budget is used up. */
17
+ readonly deferred: readonly number[];
18
+ };
19
+
20
+ /** Reads sizes and envelopes (no sources) and cuts the uids at the byte budget, in uid order. */
21
+ export async function planWithinByteBudget(
22
+ client: ImapFlow,
23
+ uids: readonly number[],
24
+ uidValidity: string,
25
+ ): Promise<ByteBudgetPlan> {
26
+ const sizeAndEnvelope = new Map<
27
+ number,
28
+ {
29
+ readonly size: number | undefined;
30
+ readonly internalDate: Date | undefined;
31
+ readonly envelope: Parameters<typeof rejectedWithoutSource>[0]["envelope"];
32
+ }
33
+ >();
34
+ if (uids.length > 0) {
35
+ for await (const msg of client.fetch(
36
+ uids.join(","),
37
+ { uid: true, size: true, internalDate: true, envelope: true },
38
+ { uid: true },
39
+ )) {
40
+ sizeAndEnvelope.set(msg.uid, {
41
+ size: msg.size,
42
+ internalDate: coerceDate(msg.internalDate ?? undefined),
43
+ envelope: msg.envelope,
44
+ });
45
+ }
46
+ }
47
+ const download: number[] = [];
48
+ const tooLarge: RejectedInboundMessage[] = [];
49
+ const deferred: number[] = [];
50
+ let downloadBytes = 0;
51
+ for (const uid of uids) {
52
+ const info = sizeAndEnvelope.get(uid);
53
+ // A mail the server did not list is gone; the later fetch simply does not return it.
54
+ if (info === undefined) {
55
+ if (deferred.length === 0) download.push(uid);
56
+ else deferred.push(uid);
57
+ continue;
58
+ }
59
+ // Unknown size counts as the largest accepted mail, so the budget cannot be skipped.
60
+ const size = info.size ?? INBOUND_MAIL_ATTACHMENTS_MAX_BYTES;
61
+ if (deferred.length > 0) {
62
+ deferred.push(uid);
63
+ } else if (size > INBOUND_MAIL_ATTACHMENTS_MAX_BYTES) {
64
+ tooLarge.push(
65
+ rejectedWithoutSource({
66
+ uid,
67
+ uidValidity,
68
+ internalDate: info.internalDate,
69
+ envelope: info.envelope,
70
+ }),
71
+ );
72
+ } else if (download.length > 0 && downloadBytes + size > IMAP_BATCH_MAX_BYTES) {
73
+ deferred.push(uid);
74
+ } else {
75
+ download.push(uid);
76
+ downloadBytes += size;
77
+ }
78
+ }
79
+ return { download, tooLarge, deferred };
80
+ }
@@ -1,4 +1,17 @@
1
1
  [
2
+ {
3
+ "version": "0.414.0",
4
+ "type": "fix",
5
+ "title": "IMAP connect limited per target host, send failures classified correctly, watch and poll share one fetch path",
6
+ "detail": "The connect, connect-my and reconnect checks now also count per target host and per host plus user across all tenants (200 and 20 per 10 minutes), so many tenants cannot probe one foreign mail server. Sending reports \"not set up\" when the credential document is missing or unreadable, and for a failed certificate check or a blocked host, instead of a rejected password or a retryable outage. Poll, watch and content refetch fetch and parse mails through one function. The TLS failure text no longer mentions a TLS setting the form does not have, and the SMTP port hint states that 465 means SSL/TLS and every other port STARTTLS. attach-message-content fails instead of treating a stream without a readable received event as a shared mailbox."
7
+ },
8
+ {
9
+ "version": "0.414.0",
10
+ "type": "breaking",
11
+ "title": "A personal password mailbox only connects the caller's own account address",
12
+ "detail": "`connect-my-mailbox` reads the email of the session user server-side and compares it case-insensitively with the submitted address before any login attempt. A different address is refused with `personal_address_not_own` (i18n `inboundMail.accounts.errors.personalAddressNotOwn`); nothing is created and the mail server is not contacted. The OAuth path needs no extra rule, its callback already binds the mailbox to the provider-authenticated account. The IMAP feature now reads the user through the user feature, which a stack must mount.",
13
+ "migration": "Members who connected a personal mailbox with an address other than their account email must have an admin connect it as a shared mailbox instead. Stacks that mount `inbound-provider-imap` need the user feature."
14
+ },
2
15
  {
3
16
  "version": "0.411.0",
4
17
  "type": "fix",
@@ -16,6 +16,7 @@ import {
16
16
  import { requireSecretsContext } from "@cosmicdrift/kumiko-bundled-features/secrets";
17
17
  import { ROLES } from "@cosmicdrift/kumiko-framework/auth";
18
18
  import {
19
+ type BeforeTransactionContext,
19
20
  type BeforeTransactionResult,
20
21
  createSystemUser,
21
22
  defineWriteHandler,
@@ -24,6 +25,7 @@ import {
24
25
  import { UnprocessableError, writeFailure } from "@cosmicdrift/kumiko-framework/errors";
25
26
  import type { WriteResult } from "@cosmicdrift/kumiko-types/handlers";
26
27
  import * as z from "zod";
28
+ import { enforceConnectTargetLimit } from "./connect-target-rate-limit.ts";
27
29
  import { IMAP_CONNECT_ERRORS, IMAP_PROVIDER_KEY, SMTP_CONNECT_ERRORS } from "./constants.ts";
28
30
  import {
29
31
  type ImapCredentialDocument,
@@ -117,8 +119,11 @@ async function checkSmtpLogin(doc: ImapCredentialDocument): Promise<ConnectFailu
117
119
  // Checks the IMAP login, then the SMTP login of the outgoing server; shared by connect and reconnect.
118
120
  export async function checkMailboxLogin(
119
121
  doc: ImapCredentialDocument,
120
- log: { warn(message: string, data?: Record<string, unknown>): void },
122
+ ctx: Pick<BeforeTransactionContext, "services" | "rateLimitDigest" | "obs">,
121
123
  ): Promise<BeforeTransactionResult<null>> {
124
+ const { log } = ctx.obs;
125
+ const limited = await enforceConnectTargetLimit(ctx, doc);
126
+ if (!limited.isSuccess) return limited;
122
127
  try {
123
128
  await connectOnce(doc, true);
124
129
  } catch (err) {
@@ -158,7 +163,7 @@ export async function connectPasswordMailbox(
158
163
  ): Promise<WriteResult<{ accountId: string }>> {
159
164
  const { displayName, address, credentialDocument } = mailbox;
160
165
  const ownerUserId = scope === "user" ? event.user.id : null;
161
- const existing = await findMailboxByAddress(ctx, event.user.tenantId, address);
166
+ const existing = await findMailboxByAddress(ctx, event.user.tenantId, address, ownerUserId);
162
167
  const reopened = existing !== undefined && isReopenableByThisForm(existing, ownerUserId);
163
168
  let accountId: string;
164
169
  if (reopened) {
@@ -222,8 +227,7 @@ export const createConnectMailboxHandler = ({ adminRoles }: InboundMailRoles) =>
222
227
  // used to scan ports or to guess passwords at someone else's mail server.
223
228
  rateLimit: { per: "tenant+handler", limit: 10, windowSeconds: 600 },
224
229
  escapeHatch: { grants: ["unsafeRaw"], reason: SECRET_TRANSACTION_REASON },
225
- beforeTransaction: (event, ctx) =>
226
- checkMailboxLogin(credentialDocumentOf(event.payload), ctx.obs.log),
230
+ beforeTransaction: (event, ctx) => checkMailboxLogin(credentialDocumentOf(event.payload), ctx),
227
231
  handler: async (event, ctx) => {
228
232
  const { displayName, address, trustedAuthservId } = event.payload;
229
233
  const connected = await connectPasswordMailbox(
@@ -2,8 +2,14 @@
2
2
  // Same login check and secret write as connect-mailbox; the sender-check policy is tenant-wide and
3
3
  // stays with the admin form.
4
4
 
5
- import type { InboundMailRoles } from "@cosmicdrift/kumiko-bundled-features/inbound-mail-foundation";
6
- import { defineWriteHandler } from "@cosmicdrift/kumiko-framework/engine";
5
+ import {
6
+ InboundMailConnectErrors,
7
+ type InboundMailRoles,
8
+ isOwnLoginAddress,
9
+ } from "@cosmicdrift/kumiko-bundled-features/inbound-mail-foundation";
10
+ import { UserQueries } from "@cosmicdrift/kumiko-bundled-features/user";
11
+ import { createSystemUser, defineWriteHandler } from "@cosmicdrift/kumiko-framework/engine";
12
+ import { UnprocessableError, writeFailure } from "@cosmicdrift/kumiko-framework/errors";
7
13
  import {
8
14
  checkMailboxLogin,
9
15
  connectMailboxSchema,
@@ -14,13 +20,13 @@ import {
14
20
 
15
21
  const connectMyMailboxSchema = connectMailboxSchema.omit({ trustedAuthservId: true });
16
22
 
17
- const MY_MAILBOX_ESCAPE_REASON = `${SECRET_TRANSACTION_REASON}; the account is created with writeAs(system) on connect-account, which is admin-only, and the owner comes from the caller's session`;
23
+ const MY_MAILBOX_ESCAPE_REASON = `${SECRET_TRANSACTION_REASON}; the account is created with writeAs(system) on connect-account, which is admin-only, and the owner comes from the caller's session; beforeTransaction reads the caller's own login address with a system queryAs(user findForAuth)`;
18
24
 
19
25
  export const createConnectMyMailboxHandler = ({ memberRoles }: InboundMailRoles) =>
20
26
  defineWriteHandler({
21
27
  name: "connect-my-mailbox",
22
28
  description:
23
- "Connects a personal IMAP mailbox of the caller with a password over implicit TLS: checks the IMAP login and the SMTP login of the outgoing server first, then creates the mail account owned by the caller and stores both servers, user and password as its credential secret in one step. A disconnected personal mailbox of the caller with the same address is reactivated under its old account id.",
29
+ "Connects a personal IMAP mailbox of the caller with a password over implicit TLS; the address must be the email address of the caller's own account (compared case-insensitively), other addresses are connected by an admin as a shared mailbox. Checks the IMAP login and the SMTP login of the outgoing server first, then creates the mail account owned by the caller and stores both servers, user and password as its credential secret in one step. A disconnected personal mailbox of the caller with the same address is reactivated under its old account id.",
24
30
  // The payload carries the plaintext password: as a tool call it would reach the model provider.
25
31
  agent: { expose: false },
26
32
  schema: connectMyMailboxSchema,
@@ -30,8 +36,19 @@ export const createConnectMyMailboxHandler = ({ memberRoles }: InboundMailRoles)
30
36
  // connect-account is admin-only; the personal mailbox is created through the system identity. The owner
31
37
  // is always event.user.id from the session, never a payload field.
32
38
  escapeHatch: { grants: ["unsafeRaw", "systemIdentity"], reason: MY_MAILBOX_ESCAPE_REASON },
33
- beforeTransaction: (event, ctx) =>
34
- checkMailboxLogin(credentialDocumentOf(event.payload), ctx.obs.log),
39
+ // Before the login check, so a foreign address never makes us contact the payload's host.
40
+ beforeTransaction: async (event, ctx) => {
41
+ const ownUser = await ctx.dispatch.queryAs(
42
+ createSystemUser(event.user.tenantId),
43
+ UserQueries.findForAuth,
44
+ { id: event.user.id },
45
+ );
46
+ if (!(await isOwnLoginAddress(ownUser, event.payload.address))) {
47
+ const { reason, i18nKey } = InboundMailConnectErrors.personalAddressNotOwn;
48
+ return writeFailure(new UnprocessableError(reason, { i18nKey }));
49
+ }
50
+ return checkMailboxLogin(credentialDocumentOf(event.payload), ctx);
51
+ },
35
52
  handler: async (event, ctx) => {
36
53
  const { displayName, address } = event.payload;
37
54
  return connectPasswordMailbox(
@@ -0,0 +1,46 @@
1
+ // The handlers' own `rateLimit` counts per tenant, so many tenants could probe one foreign mail
2
+ // host (login oracle, port scan). These buckets are keyed by the target and count across tenants.
3
+ // Trade-off: tokens are booked before the login attempt and the limiter has no refund, so a
4
+ // tenant admin can exhaust a foreign host+user bucket; the oracle protection is the weightier risk.
5
+
6
+ import { InternalError, isKumikoError, writeFailure } from "@cosmicdrift/kumiko-framework/errors";
7
+ import type {
8
+ BeforeTransactionContext,
9
+ BeforeTransactionResult,
10
+ } from "@cosmicdrift/kumiko-types/handlers";
11
+ import type { ImapCredentialDocument } from "./credential-document.ts";
12
+
13
+ const TARGET_WINDOW_SECONDS = 600;
14
+ const PER_HOST_LIMIT = 200;
15
+ const PER_HOST_AND_USER_LIMIT = 20;
16
+
17
+ export async function enforceConnectTargetLimit(
18
+ ctx: Pick<BeforeTransactionContext, "services" | "rateLimitDigest">,
19
+ doc: ImapCredentialDocument,
20
+ ): Promise<BeforeTransactionResult<null>> {
21
+ const { rateLimit } = ctx.services;
22
+ const digest = ctx.rateLimitDigest;
23
+ if (!rateLimit || !digest) {
24
+ throw new InternalError({
25
+ message: "inbound-provider-imap connect checks need a rate limiter and a digest key",
26
+ });
27
+ }
28
+ // Trailing dot is the same DNS name; without stripping it `imap.x.com.` would get its own buckets.
29
+ const host = doc.host.toLowerCase().replace(/\.+$/, "");
30
+ try {
31
+ await rateLimit.enforceAll([
32
+ {
33
+ bucket: `imap-connect-host:${digest(host)}`,
34
+ config: { limit: PER_HOST_LIMIT, windowSeconds: TARGET_WINDOW_SECONDS },
35
+ },
36
+ {
37
+ bucket: `imap-connect-host-user:${digest(`${host}\n${doc.user.toLowerCase()}`)}`,
38
+ config: { limit: PER_HOST_AND_USER_LIMIT, windowSeconds: TARGET_WINDOW_SECONDS },
39
+ },
40
+ ]);
41
+ } catch (error) {
42
+ if (isKumikoError(error)) return writeFailure(error);
43
+ throw error;
44
+ }
45
+ return { isSuccess: true, data: null };
46
+ }
@@ -3,26 +3,34 @@ import type {
3
3
  RejectedInboundMessage,
4
4
  } from "@cosmicdrift/kumiko-bundled-features/inbound-mail-foundation";
5
5
  import type { ImapFlow } from "imapflow";
6
+ import { planWithinByteBudget } from "./byte-budget.ts";
6
7
  import { coerceDate, parseOrReject } from "./parse-message.ts";
7
8
 
8
- export async function downloadBatch(
9
+ export type FetchedAndParsed = {
10
+ readonly messages: RawInboundMessage[];
11
+ readonly rejected: RejectedInboundMessage[];
12
+ /** uids the server returned with a source, parsed or rejected. */
13
+ readonly seenUids: readonly number[];
14
+ };
15
+
16
+ /** The one place that fetches sources from a uid range and parses them, shared by poll, watch and content refetch. */
17
+ export async function fetchAndParse(
9
18
  client: ImapFlow,
10
- batch: readonly number[],
19
+ uidRange: string,
11
20
  uidValidity: string,
12
21
  trustedAuthservId: string | null,
13
- ): Promise<{
14
- readonly messages: RawInboundMessage[];
15
- readonly rejected: RejectedInboundMessage[];
16
- }> {
22
+ minUid = 0,
23
+ ): Promise<FetchedAndParsed> {
17
24
  const messages: RawInboundMessage[] = [];
18
25
  const rejected: RejectedInboundMessage[] = [];
19
- if (batch.length === 0) return { messages, rejected };
26
+ const seenUids: number[] = [];
20
27
  for await (const msg of client.fetch(
21
- batch.join(","),
28
+ uidRange,
22
29
  { uid: true, internalDate: true, source: true, envelope: true },
23
30
  { uid: true },
24
31
  )) {
25
- if (!msg.source) continue;
32
+ // `N:*` always returns the last message even when it is below N.
33
+ if (msg.uid < minUid || !msg.source) continue;
26
34
  const parsed = await parseOrReject({
27
35
  source: msg.source,
28
36
  uid: msg.uid,
@@ -33,6 +41,35 @@ export async function downloadBatch(
33
41
  });
34
42
  if ("message" in parsed) messages.push(parsed.message);
35
43
  else rejected.push(parsed.rejected);
44
+ seenUids.push(msg.uid);
45
+ }
46
+ return { messages, rejected, seenUids };
47
+ }
48
+
49
+ export async function downloadBatch(
50
+ client: ImapFlow,
51
+ batch: readonly number[],
52
+ uidValidity: string,
53
+ trustedAuthservId: string | null,
54
+ ): Promise<{
55
+ readonly messages: RawInboundMessage[];
56
+ readonly rejected: RejectedInboundMessage[];
57
+ /** uids cut by the byte budget; they are fetched in a later round. */
58
+ readonly deferred: readonly number[];
59
+ }> {
60
+ const plan = await planWithinByteBudget(client, batch, uidValidity);
61
+ if (plan.download.length === 0) {
62
+ return { messages: [], rejected: [...plan.tooLarge], deferred: plan.deferred };
36
63
  }
37
- return { messages, rejected };
64
+ const fetched = await fetchAndParse(
65
+ client,
66
+ plan.download.join(","),
67
+ uidValidity,
68
+ trustedAuthservId,
69
+ );
70
+ return {
71
+ messages: fetched.messages,
72
+ rejected: [...plan.tooLarge, ...fetched.rejected],
73
+ deferred: plan.deferred,
74
+ };
38
75
  }