@composable-os/cli 0.1.0-beta.11 → 0.1.0-beta.13

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 (166) hide show
  1. package/dist/{accounts-UHYLCLEY.js → accounts-GOCNY6WI.js} +17 -13
  2. package/dist/app-RPRZMLU5.js +43 -0
  3. package/dist/app-upload-YJVEKZC6.js +198 -0
  4. package/dist/{blocks-5IFU25J6.js → blocks-DE4RRWPJ.js} +207 -25
  5. package/dist/{chunk-XIDLW3EP.js → chunk-2B6YUYZV.js} +23499 -11766
  6. package/dist/{run-7P4RQALH.js → chunk-2CCHB37H.js} +9 -146
  7. package/dist/{chunk-DU6OIFOU.js → chunk-3OGDWKJ3.js} +530 -434
  8. package/dist/{chunk-DHS2CV2M.js → chunk-5D7BDF4Y.js} +15 -13
  9. package/dist/{chunk-ZAWES2UD.js → chunk-6CM3WZG5.js} +13 -2483
  10. package/dist/{chunk-7D4GKZOX.js → chunk-CIBJGB5H.js} +422 -75
  11. package/dist/chunk-EPDTPGZJ.js +330 -0
  12. package/dist/{chunk-LAAL4YGR.js → chunk-HAYF4G5V.js} +66 -23
  13. package/dist/chunk-HCG6T53D.js +61904 -0
  14. package/dist/{chunk-K34UKWEI.js → chunk-JX4IFWNT.js} +64 -32
  15. package/dist/{chunk-6XTU2MTP.js → chunk-KFHKNXGH.js} +632 -418
  16. package/dist/chunk-KMFMD52Z.js +13276 -0
  17. package/dist/{chunk-E4WCYTCT.js → chunk-LX2TTLVY.js} +174 -55
  18. package/dist/{chunk-GYTEW66E.js → chunk-O3R4FS2J.js} +1 -1
  19. package/dist/{chunk-E64SG24J.js → chunk-Q6Y45DY6.js} +25 -2
  20. package/dist/{chunk-MBIKVRAC.js → chunk-QJZGYJVV.js} +823 -44
  21. package/dist/{chunk-PDD2ETVF.js → chunk-QQ3YSL5F.js} +172 -16
  22. package/dist/chunk-RGKJKC5T.js +876 -0
  23. package/dist/{chunk-YFTLIEWU.js → chunk-RSEWEFAV.js} +12 -2
  24. package/dist/{chunk-2ABSVMGB.js → chunk-SMJZIK2O.js} +1 -1
  25. package/dist/{chunk-LO6WGU5Y.js → chunk-U72NEGCT.js} +8089 -3464
  26. package/dist/{chunk-P6UWZ6QQ.js → chunk-Z4IKF23A.js} +11 -7
  27. package/dist/{chunk-2IVEVPMY.js → chunk-Z7OVQ6BC.js} +105 -61
  28. package/dist/{chunk-6UWRT4VM.js → chunk-ZJSFX4RD.js} +175 -3
  29. package/dist/{cli-5UNY5G5L.js → cli-OKQHHSOE.js} +17 -25
  30. package/dist/{cli-UYZMVQMR.js → cli-VYSD2S4Z.js} +227 -81
  31. package/dist/cli.js +5 -5
  32. package/dist/{client-repo-SE4EKG6W.js → client-repo-NNUCHQ2Y.js} +7 -5
  33. package/dist/create.js +5 -5
  34. package/dist/{fields-FTWWMD7N.js → fields-GTHE6TTS.js} +27 -3
  35. package/dist/gateway-DPPEQZT4.js +25 -0
  36. package/dist/grants-6JY4XZO7.js +27 -0
  37. package/dist/invoke-MRONRZ5Q.js +33 -0
  38. package/dist/moves-UAUCMWL5.js +36 -0
  39. package/dist/{order-U4KZLLSJ.js → order-RKLGT74L.js} +2 -2
  40. package/dist/{person-UHLVBVLR.js → person-YOZTFV4Q.js} +11 -10
  41. package/dist/plan-on-sale-H4F26YIN.js +26 -0
  42. package/dist/platform/migrations/0730_practitioners.sql +79 -0
  43. package/dist/platform/migrations/0750_routine_mail.sql +98 -0
  44. package/dist/platform/migrations/0770_clear_fixed_role_grants.sql +64 -0
  45. package/dist/platform/migrations/0810_recurrence_exceptions.sql +66 -0
  46. package/dist/platform/migrations/0811_staff_time.sql +109 -0
  47. package/dist/platform/migrations/0812_staff_offers.sql +112 -0
  48. package/dist/platform/migrations/0813_pay_periods.sql +241 -0
  49. package/dist/platform/migrations/0814_staff_feed.sql +47 -0
  50. package/dist/platform/migrations/0815_checklists.sql +237 -0
  51. package/dist/platform/migrations/0816_checklist_schedules.sql +121 -0
  52. package/dist/platform/migrations/0817_checklist_day_window.sql +35 -0
  53. package/dist/platform/migrations/0850_agreements.sql +271 -0
  54. package/dist/platform/migrations/0851_agreement_acceptances.sql +387 -0
  55. package/dist/platform/migrations/0853_agreement_enforcement.sql +58 -0
  56. package/dist/platform/migrations/0854_agreement_copy.sql +149 -0
  57. package/dist/platform/migrations/0855_agreement_retention.sql +279 -0
  58. package/dist/platform/migrations/0856_subscription_cooling_off.sql +159 -0
  59. package/dist/platform/migrations/0857_agreement_copy_guardian.sql +337 -0
  60. package/dist/platform/migrations/0858_agreement_desk.sql +175 -0
  61. package/dist/platform/migrations/0910_routine_messages.sql +99 -0
  62. package/dist/platform/migrations/0911_texts.sql +60 -0
  63. package/dist/platform/migrations/0913_contact_consent.sql +93 -0
  64. package/dist/platform/migrations/0916_contact_mailbox_index.sql +29 -0
  65. package/dist/platform/migrations/0930_stock_movements.sql +95 -0
  66. package/dist/platform/migrations/0931_stock_low.sql +25 -0
  67. package/dist/platform/migrations/0932_order_delivery.sql +78 -0
  68. package/dist/platform/migrations/0950_order_adjustments.sql +111 -0
  69. package/dist/platform/migrations/0951_order_price_guards.sql +33 -0
  70. package/dist/platform/migrations/0952_order_paid_another_way.sql +194 -0
  71. package/dist/platform/migrations/0953_discount_uses.sql +183 -0
  72. package/dist/platform/migrations/0955_folded_addresses.sql +92 -0
  73. package/dist/platform/migrations/0970_published_text.sql +59 -0
  74. package/dist/platform/migrations/0971_publish_acks.sql +68 -0
  75. package/dist/platform/migrations/0990_customer_email_change.sql +65 -0
  76. package/dist/platform/migrations/1010_intake_links.sql +148 -0
  77. package/dist/platform/migrations/1030_invoices.sql +747 -0
  78. package/dist/platform/migrations/1031_invoice_orders.sql +327 -0
  79. package/dist/platform/migrations/1050_inbox_conversations.sql +83 -0
  80. package/dist/platform/migrations/1090_staff_hours.sql +43 -0
  81. package/dist/platform/migrations/1091_staff_time_margins.sql +12 -0
  82. package/dist/platform/migrations/1092_client_guard.sql +26 -0
  83. package/dist/platform/migrations/1095_thing_time.sql +70 -0
  84. package/dist/platform/migrations/1130_devices.sql +162 -0
  85. package/dist/platform/migrations/1135_person_codes.sql +88 -0
  86. package/dist/platform/migrations/1150_finance_periods.sql +316 -0
  87. package/dist/platform/migrations/1151_payouts.sql +202 -0
  88. package/dist/platform/migrations/1153_finance_money_dates.sql +18 -0
  89. package/dist/platform/migrations/1170_journeys.sql +318 -0
  90. package/dist/platform/migrations/1171_person_birthday.sql +52 -0
  91. package/dist/platform/migrations/1175_person_signals.sql +29 -0
  92. package/dist/platform/migrations/1190_restricted_values.sql +25 -0
  93. package/dist/platform/migrations/1230_note_levels.sql +113 -0
  94. package/dist/platform/migrations/1231_person_carers.sql +33 -0
  95. package/dist/platform/migrations/1232_note_kinds.sql +122 -0
  96. package/dist/platform/migrations/1270_time_entries.sql +145 -0
  97. package/dist/platform/migrations/1271_leave.sql +249 -0
  98. package/dist/platform/migrations/1273_time_entry_reopened.sql +21 -0
  99. package/dist/platform/migrations/1330_accounting.sql +260 -0
  100. package/dist/platform/migrations/1350_customer_feed.sql +24 -0
  101. package/dist/platform/migrations/1390_locations.sql +77 -0
  102. package/dist/platform/migrations/1410_outside_copy.sql +213 -0
  103. package/dist/platform/migrations/1430_inbox_activities.sql +231 -0
  104. package/dist/platform/migrations/1450_wrong_guesses.sql +23 -0
  105. package/dist/platform/sdk/acknowledge.ts +100 -0
  106. package/dist/platform/sdk/agreements.ts +185 -0
  107. package/dist/platform/sdk/allowance.ts +98 -8
  108. package/dist/platform/sdk/automation-check.ts +120 -9
  109. package/dist/platform/sdk/automation.ts +78 -10
  110. package/dist/platform/sdk/behaviour.ts +26 -6
  111. package/dist/platform/sdk/calendar-entry.ts +258 -0
  112. package/dist/platform/sdk/capacity.ts +1 -2
  113. package/dist/platform/sdk/checklists.ts +118 -0
  114. package/dist/platform/sdk/contract.ts +489 -27
  115. package/dist/platform/sdk/discount.ts +408 -0
  116. package/dist/platform/sdk/document.ts +4 -2
  117. package/dist/platform/sdk/field-types.ts +5 -0
  118. package/dist/platform/sdk/formula/compile.ts +10 -0
  119. package/dist/platform/sdk/index.ts +17 -0
  120. package/dist/platform/sdk/intake.ts +380 -0
  121. package/dist/platform/sdk/journey-signals.ts +215 -0
  122. package/dist/platform/sdk/manifest.ts +1236 -65
  123. package/dist/platform/sdk/mine.ts +161 -0
  124. package/dist/platform/sdk/occupies.ts +192 -0
  125. package/dist/platform/sdk/questions.ts +681 -0
  126. package/dist/platform/sdk/recurring.ts +90 -9
  127. package/dist/platform/sdk/restricted-fields.ts +129 -0
  128. package/dist/platform/sdk/rich-text.ts +112 -0
  129. package/dist/platform/sdk/routine-mail.ts +1272 -0
  130. package/dist/platform/sdk/runtime.ts +25 -12
  131. package/dist/platform/sdk/screens.ts +91 -0
  132. package/dist/platform/sdk/sellable.ts +344 -0
  133. package/dist/platform/sdk/slug.ts +7 -2
  134. package/dist/platform/sdk/staffed.ts +474 -0
  135. package/dist/platform/sdk/states.ts +68 -26
  136. package/dist/platform/sdk/stock.ts +332 -0
  137. package/dist/platform/sdk/team.ts +83 -0
  138. package/dist/platform/sdk/testing.ts +70 -6
  139. package/dist/platform/sdk/worked-on.ts +134 -0
  140. package/dist/{products-2H5DR232.js → products-GCQLSZ3U.js} +2 -2
  141. package/dist/run-7ID64RA6.js +65 -0
  142. package/dist/run-FH7OGGTV.js +131 -0
  143. package/dist/run-OYZIAP6M.js +3363 -0
  144. package/dist/{run-ZCGFMEZF.js → run-QOFRRSZV.js} +41 -60
  145. package/dist/{run-PO3W5BFA.js → run-TJAEWR5U.js} +21 -51
  146. package/dist/{src-I3IJ4NKC.js → src-52VERUPK.js} +306 -4
  147. package/dist/src-PHRELWN5.js +40 -0
  148. package/dist/{visibility-TVXZ67FD.js → visibility-SZCINPP4.js} +21 -4
  149. package/package.json +2 -2
  150. package/dist/chunk-2TWV3SVB.js +0 -349
  151. package/dist/chunk-2XC5JLBO.js +0 -1931
  152. package/dist/chunk-324WJXNP.js +0 -320
  153. package/dist/chunk-6ELZRNUU.js +0 -116
  154. package/dist/chunk-6WGFA2IV.js +0 -643
  155. package/dist/chunk-7CQGMVDO.js +0 -63
  156. package/dist/chunk-7OGCMQXG.js +0 -284
  157. package/dist/chunk-FJIZGS5Q.js +0 -698
  158. package/dist/chunk-UB4I2QKR.js +0 -31575
  159. package/dist/chunk-WHKJGF36.js +0 -2418
  160. package/dist/chunk-YNUZRTPH.js +0 -182
  161. package/dist/chunk-ZZFWMVJO.js +0 -704
  162. package/dist/grants-LOW6TSWD.js +0 -32
  163. package/dist/invoke-6KVT6WJI.js +0 -41
  164. package/dist/moves-E2HKYCTK.js +0 -41
  165. package/dist/plan-on-sale-TJPFCNL7.js +0 -26
  166. package/dist/src-CRO35DYY.js +0 -46
@@ -0,0 +1,279 @@
1
+ -- Migration 0855: erase, merge and retention for agreements (stage 44 step 8, ADR 0646). Nothing
2
+ -- here touches core_write_guard.
3
+ --
4
+ -- An erased Person's acceptances are kept, detached, as the business's evidence for the retention
5
+ -- period (decision 15: `agreement_retention` in core_settings, `{"years": n}`, default 7), counted
6
+ -- from the erase, then the host's purge deletes them. Migration 0851's triggers allowed no delete of
7
+ -- a kept acceptance; this migration extends them by the same pattern (each delete tied to what
8
+ -- makes it true, for every writer) and weakens nothing:
9
+ -- - a kept acceptance, its signature, answers and copy row may be deleted once it was erased
10
+ -- longer ago than the retention period, unless a person put it on hold (core_agreement_hold),
11
+ -- and the acceptance itself only after the rows beside it are gone;
12
+ -- - a buyer's acceptance on a paid order nobody settled may be marked erased once erase cleared
13
+ -- the order's address (it can never settle to anyone after that);
14
+ -- - a copy that ever reached the email gatekeeper owes a scrub of the gatekeeper's storage
15
+ -- (`scrub_due_at`), which the host clears only by recording that it was done.
16
+ --
17
+ -- The cutoff is worked out in SQL from the setting: missing, 7 years; a value that is not a whole
18
+ -- number of years from 1 to 50 deletes nothing (a broken setting never deletes evidence early).
19
+
20
+ -- A person on the staff with agreements:manage holds a kept acceptance for a dispute or a claim:
21
+ -- the purge passes it by until the hold is released. Who and when only: no free text.
22
+ CREATE TABLE core_agreement_hold (
23
+ acceptance_id TEXT PRIMARY KEY,
24
+ held_by TEXT NOT NULL,
25
+ held_at TEXT NOT NULL
26
+ );
27
+
28
+ CREATE TRIGGER core_agreement_hold_new
29
+ BEFORE INSERT ON core_agreement_hold
30
+ BEGIN
31
+ SELECT CASE
32
+ WHEN EXISTS (SELECT 1 FROM core_agreement_hold h WHERE h.acceptance_id = NEW.acceptance_id)
33
+ THEN RAISE(ABORT, 'core_agreement_hold_fixed')
34
+ WHEN NOT EXISTS (SELECT 1 FROM core_agreement_acceptance a
35
+ WHERE a.id = NEW.acceptance_id AND a.erased_at IS NOT NULL)
36
+ THEN RAISE(ABORT, 'core_agreement_hold_not_kept')
37
+ END;
38
+ END;
39
+
40
+ CREATE TRIGGER core_agreement_hold_no_update
41
+ BEFORE UPDATE ON core_agreement_hold
42
+ BEGIN
43
+ SELECT RAISE(ABORT, 'core_agreement_hold_fixed');
44
+ END;
45
+
46
+ -- The purge reads erased acceptances oldest first.
47
+ CREATE INDEX core_agreement_acceptance_erased_idx
48
+ ON core_agreement_acceptance (erased_at) WHERE erased_at IS NOT NULL;
49
+
50
+ -- Erase of a buyer's acceptance nobody settled: once erase cleared its paid order's address and
51
+ -- the order has no Person, the order can never be settled (order.setPerson takes only a Person
52
+ -- with the order's address), so the acceptance is kept detached like any other (`erased_at`),
53
+ -- and purged after the retention period. Every other rule is migration 0851's, unchanged.
54
+ DROP TRIGGER core_agreement_acceptance_no_update;
55
+ CREATE TRIGGER core_agreement_acceptance_no_update
56
+ BEFORE UPDATE ON core_agreement_acceptance
57
+ BEGIN
58
+ SELECT CASE
59
+ WHEN NEW.id IS NOT OLD.id OR NEW.document_id IS NOT OLD.document_id
60
+ OR NEW.version_id IS NOT OLD.version_id OR NEW.shown_hash IS NOT OLD.shown_hash
61
+ OR NEW.context IS NOT OLD.context OR NEW.by IS NOT OLD.by
62
+ OR NEW.order_id IS NOT OLD.order_id
63
+ OR NEW.subscription_checkout IS NOT OLD.subscription_checkout
64
+ OR NEW.how IS NOT OLD.how OR NEW.email IS NOT OLD.email
65
+ OR NEW.channel IS NOT OLD.channel OR NEW.desk_session_id IS NOT OLD.desk_session_id
66
+ OR NEW.accepted_at IS NOT OLD.accepted_at OR NEW.signed_on IS NOT OLD.signed_on
67
+ OR NEW.recorded_by IS NOT OLD.recorded_by
68
+ THEN RAISE(ABORT, 'core_agreement_acceptance_immutable')
69
+ -- erased_at: set once, and only as part of one of the three erasures.
70
+ WHEN NOT (NEW.erased_at IS OLD.erased_at
71
+ OR (OLD.erased_at IS NULL AND NEW.erased_at IS NOT NULL AND (
72
+ (OLD.person_id IS NOT NULL AND NEW.person_id IS NULL
73
+ AND NOT EXISTS (SELECT 1 FROM core_person p WHERE p.id = OLD.person_id))
74
+ OR (OLD.by = 'guardian' AND OLD.agent_person_id IS NOT NULL
75
+ AND NEW.agent_person_id IS NULL
76
+ AND NOT EXISTS (SELECT 1 FROM core_person p WHERE p.id = OLD.agent_person_id))
77
+ OR (OLD.by = 'guest' AND OLD.person_id IS NULL AND NEW.person_id IS NULL
78
+ AND EXISTS (SELECT 1 FROM core_order o WHERE o.id = OLD.order_id
79
+ AND o.person_id IS NULL AND o.email IS NULL AND o.paid > 0)))))
80
+ THEN RAISE(ABORT, 'core_agreement_acceptance_immutable')
81
+ WHEN NOT (NEW.person_id IS OLD.person_id
82
+ OR (OLD.person_id IS NULL AND NEW.person_id IS NOT NULL AND OLD.by = 'guest'
83
+ AND OLD.erased_at IS NULL AND NEW.erased_at IS NULL AND (
84
+ NEW.person_id IS (SELECT o.person_id FROM core_order o WHERE o.id = OLD.order_id)
85
+ OR EXISTS (SELECT 1 FROM core_subscription_checkout c
86
+ WHERE c.id = OLD.subscription_checkout
87
+ AND NEW.person_id IN (c.payer_person_id, c.holder_person_id))))
88
+ OR (OLD.person_id IS NOT NULL AND NEW.person_id IS NULL AND NEW.erased_at IS NOT NULL
89
+ AND NOT EXISTS (SELECT 1 FROM core_person p WHERE p.id = OLD.person_id))
90
+ OR (OLD.person_id IS NOT NULL AND NEW.person_id IS NOT NULL AND NEW.person_id IS (
91
+ SELECT p.merged_into_id FROM core_person p WHERE p.id = OLD.person_id)))
92
+ THEN RAISE(ABORT, 'core_agreement_acceptance_immutable')
93
+ WHEN NOT (NEW.agent_person_id IS OLD.agent_person_id
94
+ OR (OLD.agent_person_id IS NOT NULL AND NEW.agent_person_id IS NULL
95
+ AND NOT EXISTS (SELECT 1 FROM core_person p WHERE p.id = OLD.agent_person_id))
96
+ OR (OLD.agent_person_id IS NOT NULL AND NEW.agent_person_id IS NOT NULL
97
+ AND NEW.agent_person_id IS (
98
+ SELECT p.merged_into_id FROM core_person p WHERE p.id = OLD.agent_person_id)))
99
+ THEN RAISE(ABORT, 'core_agreement_acceptance_immutable')
100
+ WHEN NOT (NEW.account_id IS OLD.account_id
101
+ OR (NEW.account_id IS NULL AND NEW.erased_at IS NOT NULL))
102
+ THEN RAISE(ABORT, 'core_agreement_acceptance_immutable')
103
+ WHEN NOT (NEW.typed_name IS OLD.typed_name
104
+ OR (NEW.typed_name IS NULL AND OLD.by = 'guardian' AND NEW.agent_person_id IS NULL
105
+ AND NEW.erased_at IS NOT NULL))
106
+ THEN RAISE(ABORT, 'core_agreement_acceptance_immutable')
107
+ -- A paper form's free text (who signed it, the note staff typed) may name a guardian: erase
108
+ -- of a guardian clears both on their dependants' paper forms (coordinator decision of
109
+ -- 2026-11-23, owner to confirm). Only ever to nothing, both together, and only on paper.
110
+ WHEN (NEW.signed_by IS NOT OLD.signed_by OR NEW.note IS NOT OLD.note) AND NOT (
111
+ OLD.by = 'paper' AND NEW.signed_by IS NULL AND NEW.note IS NULL)
112
+ THEN RAISE(ABORT, 'core_agreement_acceptance_immutable')
113
+ END;
114
+ END;
115
+
116
+ -- Deleted only with a buyer's unpaid order (step 3), or by the purge: kept longer than the
117
+ -- retention period since its erase, not on hold, and nothing left beside it (its signature,
118
+ -- answers and copy row go first, so nothing personal is ever left without its acceptance).
119
+ DROP TRIGGER core_agreement_acceptance_no_delete;
120
+ CREATE TRIGGER core_agreement_acceptance_no_delete
121
+ BEFORE DELETE ON core_agreement_acceptance
122
+ WHEN NOT (
123
+ (OLD.by = 'guest' AND OLD.person_id IS NULL AND OLD.erased_at IS NULL)
124
+ OR (OLD.erased_at IS NOT NULL
125
+ AND coalesce(OLD.erased_at < strftime('%Y-%m-%dT%H:%M:%fZ', 'now', '-' || coalesce((
126
+ SELECT CASE WHEN json_type(s.value, '$.years') = 'integer'
127
+ AND json_extract(s.value, '$.years') BETWEEN 1 AND 50
128
+ THEN json_extract(s.value, '$.years') ELSE 1000 END
129
+ FROM core_settings s WHERE s.key = 'agreement_retention'), 7) || ' years'), 0)
130
+ AND NOT EXISTS (SELECT 1 FROM core_agreement_hold h WHERE h.acceptance_id = OLD.id)
131
+ AND NOT EXISTS (SELECT 1 FROM core_agreement_signature s WHERE s.acceptance_id = OLD.id)
132
+ AND NOT EXISTS (SELECT 1 FROM core_agreement_answer n WHERE n.acceptance_id = OLD.id)
133
+ AND NOT EXISTS (SELECT 1 FROM core_agreement_copy c WHERE c.acceptance_id = OLD.id))
134
+ )
135
+ BEGIN
136
+ SELECT RAISE(ABORT, 'core_agreement_acceptance_immutable');
137
+ END;
138
+
139
+ -- A signature goes with a guardian who is erased, a buyer's unpaid acceptance, or the purge.
140
+ DROP TRIGGER core_agreement_signature_no_delete;
141
+ CREATE TRIGGER core_agreement_signature_no_delete
142
+ BEFORE DELETE ON core_agreement_signature
143
+ WHEN EXISTS (SELECT 1 FROM core_agreement_acceptance a WHERE a.id = OLD.acceptance_id
144
+ AND NOT (a.by = 'guardian' AND a.erased_at IS NOT NULL AND a.agent_person_id IS NULL)
145
+ AND NOT (a.by = 'guest' AND a.person_id IS NULL AND a.erased_at IS NULL)
146
+ AND NOT (a.erased_at IS NOT NULL
147
+ AND coalesce(a.erased_at < strftime('%Y-%m-%dT%H:%M:%fZ', 'now', '-' || coalesce((
148
+ SELECT CASE WHEN json_type(s.value, '$.years') = 'integer'
149
+ AND json_extract(s.value, '$.years') BETWEEN 1 AND 50
150
+ THEN json_extract(s.value, '$.years') ELSE 1000 END
151
+ FROM core_settings s WHERE s.key = 'agreement_retention'), 7) || ' years'), 0)
152
+ AND NOT EXISTS (SELECT 1 FROM core_agreement_hold h WHERE h.acceptance_id = a.id)))
153
+ BEGIN
154
+ SELECT RAISE(ABORT, 'core_agreement_acceptance_immutable');
155
+ END;
156
+
157
+ -- Health answers go when the Person they are about is erased, with a buyer's unpaid acceptance,
158
+ -- or with the purge (a child's answers on an acceptance whose guardian was erased, kept until
159
+ -- then with it).
160
+ DROP TRIGGER core_agreement_answer_no_delete;
161
+ CREATE TRIGGER core_agreement_answer_no_delete
162
+ BEFORE DELETE ON core_agreement_answer
163
+ WHEN EXISTS (SELECT 1 FROM core_agreement_acceptance a WHERE a.id = OLD.acceptance_id
164
+ AND NOT (a.erased_at IS NOT NULL AND a.person_id IS NULL)
165
+ AND NOT (a.by = 'guest' AND a.person_id IS NULL AND a.erased_at IS NULL)
166
+ AND NOT (a.erased_at IS NOT NULL
167
+ AND coalesce(a.erased_at < strftime('%Y-%m-%dT%H:%M:%fZ', 'now', '-' || coalesce((
168
+ SELECT CASE WHEN json_type(s.value, '$.years') = 'integer'
169
+ AND json_extract(s.value, '$.years') BETWEEN 1 AND 50
170
+ THEN json_extract(s.value, '$.years') ELSE 1000 END
171
+ FROM core_settings s WHERE s.key = 'agreement_retention'), 7) || ' years'), 0)
172
+ AND NOT EXISTS (SELECT 1 FROM core_agreement_hold h WHERE h.acceptance_id = a.id)))
173
+ BEGIN
174
+ SELECT RAISE(ABORT, 'core_agreement_acceptance_immutable');
175
+ END;
176
+
177
+ -- The copy: what the email gatekeeper still holds of an erased acceptance's copy. Each send is
178
+ -- recorded with the gatekeeper it reached (`via`: a JSON list of `<connection> <resource>`), so
179
+ -- the scrub goes to the gatekeeper that holds it, not whichever channel is current. A copy that
180
+ -- reached one owes a scrub (`scrub_due_at`, owed since `scrub_owed_since`): the host asks each
181
+ -- gatekeeper, as its own agreements mail, to drop the copy's sends and wipe their recipients and
182
+ -- subject from its history; every gatekeeper is asked each time, whatever another answered, and
183
+ -- those done are recorded (`scrub_done`). The row is scrubbed (`scrubbed_at`) only when all are.
184
+ -- What is left is recorded (`scrub_error`): `unreachable` when every gatekeeper left is gone (its
185
+ -- connection removed, or its sender no longer bound), `inactive` when one still exists but cannot
186
+ -- be used now (it needs re-authorising), `failed` otherwise; each is asked again. Only once every
187
+ -- gatekeeper left has been gone for 30 days (`scrub_gone_since`) may the purge delete the row at
188
+ -- the end of the retention period anyway (and its audit names the gatekeepers left). A sender whose send was in
189
+ -- flight during the erase marks it owed again once its call returns, so a scrub follows the last
190
+ -- send.
191
+ ALTER TABLE core_agreement_copy ADD COLUMN via TEXT CHECK (via IS NULL OR json_valid(via));
192
+ ALTER TABLE core_agreement_copy ADD COLUMN scrub_due_at TEXT;
193
+ ALTER TABLE core_agreement_copy ADD COLUMN scrub_owed_since TEXT;
194
+ ALTER TABLE core_agreement_copy ADD COLUMN scrub_error TEXT
195
+ CHECK (scrub_error IS NULL OR scrub_error IN ('unreachable', 'inactive', 'failed'));
196
+ ALTER TABLE core_agreement_copy ADD COLUMN scrub_done TEXT
197
+ CHECK (scrub_done IS NULL OR json_valid(scrub_done));
198
+ ALTER TABLE core_agreement_copy ADD COLUMN scrub_gone_since TEXT;
199
+ ALTER TABLE core_agreement_copy ADD COLUMN scrubbed_at TEXT;
200
+ CREATE INDEX core_agreement_copy_scrub_idx
201
+ ON core_agreement_copy (scrub_due_at) WHERE scrub_due_at IS NOT NULL;
202
+
203
+ DROP TRIGGER core_agreement_copy_on_erase;
204
+ CREATE TRIGGER core_agreement_copy_on_erase
205
+ AFTER UPDATE OF erased_at ON core_agreement_acceptance
206
+ WHEN OLD.erased_at IS NULL AND NEW.erased_at IS NOT NULL
207
+ BEGIN
208
+ UPDATE core_agreement_copy
209
+ SET to_address = NULL, next_at = NULL, error = NULL, pending_action_id = NULL,
210
+ state = CASE WHEN state IN ('sent', 'failed') THEN state ELSE 'cancelled' END,
211
+ scrub_due_at = CASE WHEN attempts > 0 THEN NEW.erased_at ELSE scrub_due_at END,
212
+ scrub_owed_since = CASE WHEN attempts > 0 THEN coalesce(scrub_owed_since, NEW.erased_at)
213
+ ELSE scrub_owed_since END
214
+ WHERE acceptance_id = NEW.id;
215
+ END;
216
+
217
+ -- Migration 0854's rules, plus: a scrub is owed only for an erased acceptance, is cleared only by
218
+ -- recording that it was done, and when it was first owed never changes; where a send went is only
219
+ -- ever added to.
220
+ DROP TRIGGER core_agreement_copy_no_update;
221
+ CREATE TRIGGER core_agreement_copy_no_update
222
+ BEFORE UPDATE ON core_agreement_copy
223
+ BEGIN
224
+ SELECT CASE
225
+ WHEN NEW.acceptance_id IS NOT OLD.acceptance_id
226
+ OR NEW.queued_at IS NOT OLD.queued_at OR NEW.requested_by IS NOT OLD.requested_by
227
+ THEN RAISE(ABORT, 'core_agreement_copy_fixed')
228
+ WHEN NEW.to_address IS NOT OLD.to_address AND NOT (NEW.to_address IS NULL
229
+ AND EXISTS (SELECT 1 FROM core_agreement_acceptance a
230
+ WHERE a.id = OLD.acceptance_id AND a.erased_at IS NOT NULL))
231
+ THEN RAISE(ABORT, 'core_agreement_copy_fixed')
232
+ WHEN OLD.state = 'sent' AND NEW.state != 'sent'
233
+ THEN RAISE(ABORT, 'core_agreement_copy_fixed')
234
+ WHEN NEW.scrub_due_at IS NOT NULL AND NOT EXISTS (SELECT 1 FROM core_agreement_acceptance a
235
+ WHERE a.id = OLD.acceptance_id AND a.erased_at IS NOT NULL)
236
+ THEN RAISE(ABORT, 'core_agreement_copy_fixed')
237
+ WHEN OLD.scrub_due_at IS NOT NULL AND NEW.scrub_due_at IS NULL
238
+ AND NEW.scrubbed_at IS OLD.scrubbed_at
239
+ THEN RAISE(ABORT, 'core_agreement_copy_fixed')
240
+ WHEN OLD.scrub_owed_since IS NOT NULL AND NEW.scrub_owed_since IS NOT OLD.scrub_owed_since
241
+ THEN RAISE(ABORT, 'core_agreement_copy_fixed')
242
+ WHEN NEW.scrub_due_at IS NOT NULL AND NEW.scrub_owed_since IS NULL
243
+ THEN RAISE(ABORT, 'core_agreement_copy_fixed')
244
+ WHEN OLD.via IS NOT NULL AND (NEW.via IS NULL OR EXISTS (
245
+ SELECT 1 FROM json_each(OLD.via) o
246
+ WHERE NOT EXISTS (SELECT 1 FROM json_each(NEW.via) n WHERE n.value = o.value)))
247
+ THEN RAISE(ABORT, 'core_agreement_copy_fixed')
248
+ END;
249
+ END;
250
+
251
+ -- A copy row is deleted only by the purge, with its acceptance, and never while a scrub is owed,
252
+ -- unless every gatekeeper left has been gone for 30 days (a gatekeeper that no longer exists must
253
+ -- not keep a record past its retention period; the purge audits it). One that exists but needs
254
+ -- re-authorising never starts that clock.
255
+ CREATE TRIGGER core_agreement_copy_no_delete
256
+ BEFORE DELETE ON core_agreement_copy
257
+ WHEN (OLD.scrub_due_at IS NOT NULL AND NOT (OLD.scrub_error IS 'unreachable' AND coalesce(
258
+ OLD.scrub_gone_since < strftime('%Y-%m-%dT%H:%M:%fZ', 'now', '-30 days'), 0)))
259
+ OR NOT EXISTS (
260
+ SELECT 1 FROM core_agreement_acceptance a WHERE a.id = OLD.acceptance_id
261
+ AND a.erased_at IS NOT NULL
262
+ AND coalesce(a.erased_at < strftime('%Y-%m-%dT%H:%M:%fZ', 'now', '-' || coalesce((
263
+ SELECT CASE WHEN json_type(s.value, '$.years') = 'integer'
264
+ AND json_extract(s.value, '$.years') BETWEEN 1 AND 50
265
+ THEN json_extract(s.value, '$.years') ELSE 1000 END
266
+ FROM core_settings s WHERE s.key = 'agreement_retention'), 7) || ' years'), 0)
267
+ AND NOT EXISTS (SELECT 1 FROM core_agreement_hold h WHERE h.acceptance_id = a.id))
268
+ BEGIN
269
+ SELECT RAISE(ABORT, 'core_agreement_copy_fixed');
270
+ END;
271
+
272
+ -- A subscription checkout whose Person (payer or holder) or address was erased before the
273
+ -- provider confirmed it: closed (`erased_at`), its address cleared. A confirmation that arrives
274
+ -- later makes no subscription; staff are told to cancel it with the provider.
275
+ ALTER TABLE core_subscription_checkout ADD COLUMN erased_at TEXT;
276
+
277
+ -- Guarded: migrations land out of order (docs/parallel-work.md).
278
+ UPDATE core_meta SET value = '855'
279
+ WHERE key = 'schema_version' AND CAST(value AS INTEGER) < 855;
@@ -0,0 +1,159 @@
1
+ -- Migration 0856: cooling off (stage 44 step 7, decision 11, ADR 0647).
2
+ --
3
+ -- A business may set a cooling-off period (whole days, 0 off): a new member may end their
4
+ -- membership within it for a full refund of what they paid, which still waits for a person with
5
+ -- `orders:refund` like every refund (ADR 0429). The days are the business's setting, declared by
6
+ -- the pack that sells the plan (`sellable.recurring.coolingOff`); the platform says nothing of
7
+ -- what any law requires.
8
+ --
9
+ -- 1. The days in force when the subscription was started (`subscription.start`), kept on its
10
+ -- checkout: they are the subscription's days for good. A setting changed later, shorter or
11
+ -- longer, applies only to subscriptions started after it. Null: started before this migration
12
+ -- (0 days), or a plan that has no cooling off.
13
+ ALTER TABLE core_subscription_checkout ADD COLUMN cooling_off_days INTEGER
14
+ CHECK (cooling_off_days IS NULL OR cooling_off_days >= 0);
15
+
16
+ -- 1b. When the provider took a subscription period's payment (its verified event's own time), as
17
+ -- against `paid_at`, when the host processed it. What a cooling off owes is judged by this
18
+ -- one, so a payment whose event the host applied late is still owed (ADR 0647, rule C). Null:
19
+ -- an order that is not a period's, or one recorded before this migration.
20
+ ALTER TABLE core_order ADD COLUMN provider_paid_at TEXT;
21
+
22
+ -- 2. A cooling off asked for (`subscription.coolOff`), one per subscription (ADR 0647's rule):
23
+ --
24
+ -- asked open: a valid ask (inside the window) was made at `asked_at`; it stays open until
25
+ -- `deadline_at` (asked_at + the approval expiry, 7 days). Asking again retries the
26
+ -- provider (`call_outcome`, `ask_count`) and never moves either. While it is open and
27
+ -- before its deadline, any end of the subscription is its end
28
+ -- ended the subscription ended while it was open (or staff finished a lapsed one,
29
+ -- `finished`): what is owed was written once, by the bounds kept here (`owed_from`,
30
+ -- `owed_to`); later events add nothing
31
+ -- lapsed its deadline passed with no end (or the provider showed it ended and its event did
32
+ -- not come within a day), its waiting cancel was rejected or expired, the end came
33
+ -- before the ask or after the deadline, or the member had used theirs: nothing owed as
34
+ -- cooled off, admins told; staff may finish it (`finish_state`), and it becomes
35
+ -- `ended` only once the provider's event shows the membership ended
36
+ --
37
+ -- No address, name or free text: the holder is held only so a person cools off once (below), and
38
+ -- goes with them on erase (the foreign key) or to the survivor on a merge.
39
+ CREATE TABLE core_subscription_cool_off (
40
+ subscription_id TEXT PRIMARY KEY NOT NULL
41
+ REFERENCES core_subscription (id) ON DELETE CASCADE,
42
+ holder_person_id TEXT REFERENCES core_person (id) ON DELETE SET NULL,
43
+ -- Who asked: the payer themselves (a member, a guardian paying for a child) or staff.
44
+ asked_by TEXT NOT NULL CHECK (asked_by IN ('payer', 'staff')),
45
+ -- The staff account that asked (never a customer's id).
46
+ asked_by_user TEXT,
47
+ -- The days that applied and the moment the window closed (the end of the last local day).
48
+ days INTEGER NOT NULL CHECK (days > 0),
49
+ window_ends_at TEXT NOT NULL,
50
+ state TEXT NOT NULL CHECK (state IN ('asked', 'ended', 'lapsed')),
51
+ -- What came of the latest call: sent, pending (at the gatekeeper), unknown, refused, or null
52
+ -- (the Worker stopped between the commit and the call).
53
+ call_outcome TEXT
54
+ CHECK (call_outcome IS NULL OR call_outcome IN ('sent', 'pending', 'unknown', 'refused')),
55
+ -- The valid ask (inside the window) and its fixed deadline; the latest ask (a retry).
56
+ asked_at TEXT NOT NULL,
57
+ deadline_at TEXT NOT NULL,
58
+ last_asked_at TEXT NOT NULL,
59
+ -- Each ask by a person is a new request to the provider (its idempotency key carries this
60
+ -- count); the sweep's retry of one keeps it.
61
+ ask_count INTEGER NOT NULL DEFAULT 1 CHECK (ask_count >= 1),
62
+ -- When the provider ended it (its read), once `ended` or `lapsed`.
63
+ ended_at TEXT,
64
+ -- Ended by staff finishing a lapsed one; the bounds what is owed was written by.
65
+ finished INTEGER NOT NULL DEFAULT 0 CHECK (finished IN (0, 1)),
66
+ -- Staff asked to finish a lapsed one (rule E) at `finish_asked_at`, and what came of ending it
67
+ -- at the provider: asked (not called yet), sent, pending (waiting at the gatekeeper), unknown,
68
+ -- or stuck (it could not be done: admins told, the sweep leaves it). It is `ended` only once
69
+ -- the provider's event shows the membership ended.
70
+ finish_state TEXT
71
+ CHECK (finish_state IS NULL OR finish_state IN ('asked', 'sent', 'pending', 'unknown', 'stuck')),
72
+ finish_asked_at TEXT,
73
+ owed_from TEXT,
74
+ owed_to TEXT,
75
+ -- Why it lapsed: its deadline, the action turned down, the end's time, or used before.
76
+ lapsed_because TEXT
77
+ CHECK (lapsed_because IS NULL OR lapsed_because IN ('deadline', 'action', 'end_time', 'used')),
78
+ -- When admins were told it is stuck or lapsed: the member is told so only then.
79
+ told_at TEXT,
80
+ -- Bookings ahead still to release (1), how many were, and how often the follow-up failed.
81
+ release_left INTEGER NOT NULL DEFAULT 0 CHECK (release_left IN (0, 1)),
82
+ released_count INTEGER NOT NULL DEFAULT 0 CHECK (released_count >= 0),
83
+ release_stopped INTEGER NOT NULL DEFAULT 0 CHECK (release_stopped IN (0, 1)),
84
+ release_failures INTEGER NOT NULL DEFAULT 0 CHECK (release_failures >= 0),
85
+ -- The member's one summary email of what was cancelled with the membership.
86
+ mail_state TEXT CHECK (mail_state IS NULL
87
+ OR mail_state IN ('due', 'sending', 'sent', 'pending', 'failed')),
88
+ mail_claimed_at TEXT,
89
+ mail_tries INTEGER NOT NULL DEFAULT 0 CHECK (mail_tries >= 0),
90
+ updated_at TEXT NOT NULL
91
+ );
92
+
93
+ CREATE INDEX core_subscription_cool_off_holder_idx
94
+ ON core_subscription_cool_off (holder_person_id, state);
95
+
96
+ -- The minute cron: open ones (their answer, their deadline), oldest first.
97
+ CREATE INDEX core_subscription_cool_off_open_idx
98
+ ON core_subscription_cool_off (state, updated_at) WHERE state = 'asked';
99
+
100
+ -- Staff finishing a lapsed one, not yet confirmed: the sweep's.
101
+ CREATE INDEX core_subscription_cool_off_finish_idx
102
+ ON core_subscription_cool_off (finish_state, updated_at)
103
+ WHERE state = 'lapsed' AND finish_state IN ('asked', 'sent', 'pending', 'unknown');
104
+
105
+ -- The follow-ups: bookings still to release, and the summary email due or stuck.
106
+ CREATE INDEX core_subscription_cool_off_release_idx
107
+ ON core_subscription_cool_off (release_left) WHERE release_left = 1;
108
+ CREATE INDEX core_subscription_cool_off_mail_idx
109
+ ON core_subscription_cool_off (mail_state) WHERE mail_state IN ('due', 'sending');
110
+
111
+ -- 2b. Every cancel of a cooling off that waited for a person at the gatekeeper, kept for good
112
+ -- (never overwritten by a later ask): while one still waits nobody may ask again, the row
113
+ -- lapses for a turned-down one only when none waits, and at the deadline every one still
114
+ -- waiting is rejected. `ask` is the ask (`ask_count`) it was made by.
115
+ CREATE TABLE core_subscription_cool_off_action (
116
+ subscription_id TEXT NOT NULL
117
+ REFERENCES core_subscription_cool_off (subscription_id) ON DELETE CASCADE,
118
+ action_id TEXT NOT NULL,
119
+ ask INTEGER NOT NULL CHECK (ask >= 1),
120
+ created_at TEXT NOT NULL,
121
+ PRIMARY KEY (subscription_id, action_id)
122
+ );
123
+
124
+ -- 3. One row per subscription for good: an INSERT OR REPLACE over an existing row (which would
125
+ -- pass the update triggers by, recursive triggers being off) is refused like a second insert.
126
+ CREATE TRIGGER core_subscription_cool_off_one
127
+ BEFORE INSERT ON core_subscription_cool_off
128
+ WHEN EXISTS (SELECT 1 FROM core_subscription_cool_off WHERE subscription_id = NEW.subscription_id)
129
+ BEGIN
130
+ SELECT RAISE(ABORT, 'core_cool_off_exists');
131
+ END;
132
+
133
+ -- 4. Once per member, ever (the owner's decision of 2026-11-24): one ended (or finished) counts;
134
+ -- an open, refused or lapsed one does not. The host checks it before it writes an end, so this
135
+ -- only catches two ends racing (the loser's batch fails and is applied again, and then lapses
136
+ -- as used). A move into `ended` only: a merge moving rows to the survivor (an update of the
137
+ -- holder) and an erase (the holder set null) are never refused.
138
+ CREATE TRIGGER core_subscription_cool_off_once
139
+ BEFORE UPDATE OF state ON core_subscription_cool_off
140
+ WHEN NEW.holder_person_id IS NOT NULL AND NEW.state = 'ended' AND OLD.state != 'ended'
141
+ BEGIN
142
+ SELECT CASE WHEN EXISTS (SELECT 1 FROM core_subscription_cool_off
143
+ WHERE holder_person_id = NEW.holder_person_id AND state = 'ended'
144
+ AND subscription_id != NEW.subscription_id)
145
+ THEN RAISE(ABORT, 'core_cool_off_used') END;
146
+ END;
147
+
148
+ -- An ended cooling off is final; a lapsed one only becomes ended (staff finishing it).
149
+ CREATE TRIGGER core_subscription_cool_off_final
150
+ BEFORE UPDATE OF state ON core_subscription_cool_off
151
+ WHEN (OLD.state = 'ended' AND NEW.state != 'ended')
152
+ OR (OLD.state = 'lapsed' AND NEW.state NOT IN ('lapsed', 'ended'))
153
+ BEGIN
154
+ SELECT RAISE(ABORT, 'core_cool_off_final');
155
+ END;
156
+
157
+ -- Guarded: migrations land out of order (docs/parallel-work.md).
158
+ UPDATE core_meta SET value = '856'
159
+ WHERE key = 'schema_version' AND CAST(value AS INTEGER) < 856;