@hyperscale0/hsx 2.4.1 → 3.0.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 (249) hide show
  1. package/CHANGELOG.md +2 -408
  2. package/README.md +3 -92
  3. package/dist/src/ast.d.ts +99 -310
  4. package/dist/src/ast.d.ts.map +1 -1
  5. package/dist/src/ast.js +16 -69
  6. package/dist/src/ast.js.map +1 -1
  7. package/dist/src/cli.d.ts +6 -19
  8. package/dist/src/cli.d.ts.map +1 -1
  9. package/dist/src/cli.js +76 -315
  10. package/dist/src/cli.js.map +1 -1
  11. package/dist/src/compile.d.ts +23 -73
  12. package/dist/src/compile.d.ts.map +1 -1
  13. package/dist/src/compile.js +1113 -148
  14. package/dist/src/compile.js.map +1 -1
  15. package/dist/src/cost.d.ts +8 -77
  16. package/dist/src/cost.d.ts.map +1 -1
  17. package/dist/src/cost.js +13 -422
  18. package/dist/src/cost.js.map +1 -1
  19. package/dist/src/format.d.ts +8 -7
  20. package/dist/src/format.d.ts.map +1 -1
  21. package/dist/src/format.js +9 -175
  22. package/dist/src/format.js.map +1 -1
  23. package/dist/src/headers.d.ts +35 -0
  24. package/dist/src/headers.d.ts.map +1 -0
  25. package/dist/src/headers.js +108 -0
  26. package/dist/src/headers.js.map +1 -0
  27. package/dist/src/index.d.ts +6 -12
  28. package/dist/src/index.d.ts.map +1 -1
  29. package/dist/src/index.js +4 -9
  30. package/dist/src/index.js.map +1 -1
  31. package/dist/src/lex.d.ts +8 -28
  32. package/dist/src/lex.d.ts.map +1 -1
  33. package/dist/src/lex.js +88 -146
  34. package/dist/src/lex.js.map +1 -1
  35. package/dist/src/parse.d.ts +4 -17
  36. package/dist/src/parse.d.ts.map +1 -1
  37. package/dist/src/parse.js +402 -1040
  38. package/dist/src/parse.js.map +1 -1
  39. package/dist/src/std-bundle.d.ts.map +1 -1
  40. package/dist/src/std-bundle.js +12 -20
  41. package/dist/src/std-bundle.js.map +1 -1
  42. package/dist/src/std-library.d.ts +1 -1
  43. package/dist/src/std-library.d.ts.map +1 -1
  44. package/dist/src/std-library.js +1 -5
  45. package/dist/src/std-library.js.map +1 -1
  46. package/dist/src/tunables.d.ts +7 -0
  47. package/dist/src/tunables.d.ts.map +1 -0
  48. package/dist/src/tunables.js +26 -0
  49. package/dist/src/tunables.js.map +1 -0
  50. package/dist/src/version.d.ts +2 -4
  51. package/dist/src/version.d.ts.map +1 -1
  52. package/dist/src/version.js +2 -4
  53. package/dist/src/version.js.map +1 -1
  54. package/docs/README.md +179 -41
  55. package/docs/headers.md +44 -0
  56. package/examples/cost-table.json +99 -751
  57. package/examples/library.hsx +59 -0
  58. package/package.json +9 -17
  59. package/src/ast.ts +82 -441
  60. package/src/cli.ts +77 -362
  61. package/src/compile.ts +1535 -247
  62. package/src/cost.ts +28 -671
  63. package/src/format.ts +12 -256
  64. package/src/headers.ts +126 -0
  65. package/src/index.ts +7 -39
  66. package/src/lex.ts +99 -195
  67. package/src/parse.ts +403 -1209
  68. package/src/std-bundle.ts +12 -20
  69. package/src/std-library.ts +2 -7
  70. package/src/tunables.ts +31 -0
  71. package/src/version.ts +2 -5
  72. package/std/approvals.hsx +17 -0
  73. package/std/cards.hsx +63 -0
  74. package/std/collections.hsx +31 -0
  75. package/std/escrow.hsx +43 -0
  76. package/std/financing.hsx +372 -0
  77. package/std/insurance.hsx +59 -0
  78. package/std/lending.hsx +115 -0
  79. package/std/marketplace.hsx +24 -0
  80. package/std/money.hsx +195 -0
  81. package/std/savings.hsx +44 -0
  82. package/std/travel.hsx +98 -0
  83. package/std/wallet.hsx +41 -0
  84. package/dist/src/diagnostics.d.ts +0 -13
  85. package/dist/src/diagnostics.d.ts.map +0 -1
  86. package/dist/src/diagnostics.js +0 -587
  87. package/dist/src/diagnostics.js.map +0 -1
  88. package/dist/src/emit.d.ts +0 -51
  89. package/dist/src/emit.d.ts.map +0 -1
  90. package/dist/src/emit.js +0 -192
  91. package/dist/src/emit.js.map +0 -1
  92. package/dist/src/entry-overrides.d.ts +0 -58
  93. package/dist/src/entry-overrides.d.ts.map +0 -1
  94. package/dist/src/entry-overrides.js +0 -284
  95. package/dist/src/entry-overrides.js.map +0 -1
  96. package/dist/src/ir.d.ts +0 -73
  97. package/dist/src/ir.d.ts.map +0 -1
  98. package/dist/src/ir.js +0 -15
  99. package/dist/src/ir.js.map +0 -1
  100. package/dist/src/limits.d.ts +0 -23
  101. package/dist/src/limits.d.ts.map +0 -1
  102. package/dist/src/limits.js +0 -23
  103. package/dist/src/limits.js.map +0 -1
  104. package/dist/src/lsp/server.d.ts +0 -32
  105. package/dist/src/lsp/server.d.ts.map +0 -1
  106. package/dist/src/lsp/server.js +0 -391
  107. package/dist/src/lsp/server.js.map +0 -1
  108. package/dist/src/modules.d.ts +0 -38
  109. package/dist/src/modules.d.ts.map +0 -1
  110. package/dist/src/modules.js +0 -372
  111. package/dist/src/modules.js.map +0 -1
  112. package/dist/src/typecheck.d.ts +0 -8
  113. package/dist/src/typecheck.d.ts.map +0 -1
  114. package/dist/src/typecheck.js +0 -4160
  115. package/dist/src/typecheck.js.map +0 -1
  116. package/docs/guide/01-first-program.md +0 -24
  117. package/docs/guide/02-money.md +0 -25
  118. package/docs/guide/03-instruments.md +0 -93
  119. package/docs/guide/04-lifecycles.md +0 -40
  120. package/docs/guide/05-fees-and-splits.md +0 -45
  121. package/docs/guide/06-schedules.md +0 -26
  122. package/docs/guide/07-composition.md +0 -52
  123. package/docs/guide/08-writing-a-module.md +0 -85
  124. package/docs/guide/09-cost.md +0 -24
  125. package/docs/guide/10-diagnostics.md +0 -27
  126. package/docs/llms-full.txt +0 -3684
  127. package/docs/llms.txt +0 -44
  128. package/docs/piece-plans.md +0 -120
  129. package/docs/playground.md +0 -55
  130. package/docs/reference/cli.md +0 -36
  131. package/docs/reference/diagnostics.md +0 -901
  132. package/docs/reference/grammar.md +0 -38
  133. package/docs/reference/std/advance.md +0 -112
  134. package/docs/reference/std/cancellable_booking.md +0 -159
  135. package/docs/reference/std/captured_payment.md +0 -115
  136. package/docs/reference/std/conditional_disbursement.md +0 -101
  137. package/docs/reference/std/credit_facility.md +0 -108
  138. package/docs/reference/std/held_payment.md +0 -173
  139. package/docs/reference/std/instant_transfer.md +0 -97
  140. package/docs/reference/std/metered.md +0 -89
  141. package/docs/reference/std/pooled_split.md +0 -95
  142. package/docs/reference/std/premium_forward.md +0 -119
  143. package/docs/reference/std/reconciled_payout.md +0 -103
  144. package/docs/reference/std/rotating_pool.md +0 -130
  145. package/docs/reference/std/scheduled.md +0 -136
  146. package/docs/reference/std/security_deposit.md +0 -138
  147. package/docs/reference/std/settlement_batch.md +0 -123
  148. package/docs/reference/std/swap.md +0 -133
  149. package/docs/reference/std/threshold_pool.md +0 -138
  150. package/docs/reference/std/weighted_distribution.md +0 -135
  151. package/docs/reference/types.md +0 -61
  152. package/docs/reference/udl-output.md +0 -13
  153. package/examples/01-first-program/README.md +0 -45
  154. package/examples/01-first-program/tip-jar.hsx +0 -17
  155. package/examples/02-imports-and-modules/README.md +0 -96
  156. package/examples/02-imports-and-modules/photo-booth.hsx +0 -37
  157. package/examples/03-diagnostics/README.md +0 -68
  158. package/examples/03-diagnostics/corner-shop-fixed.hsx +0 -22
  159. package/examples/03-diagnostics/corner-shop.hsx +0 -19
  160. package/examples/04-complete-product/README.md +0 -84
  161. package/examples/04-complete-product/study-hall.hsx +0 -67
  162. package/examples/05-authored-instrument/README.md +0 -5
  163. package/examples/05-authored-instrument/payment.hsx +0 -37
  164. package/examples/05-watch-club/README.md +0 -6
  165. package/examples/05-watch-club/watch-club.hsx +0 -123
  166. package/examples/README.md +0 -48
  167. package/examples/advance/README.md +0 -3
  168. package/examples/advance/advance.hsx +0 -13
  169. package/examples/advance/advance.udl +0 -367
  170. package/examples/cancellable_booking/README.md +0 -3
  171. package/examples/cancellable_booking/cancellable_booking.hsx +0 -14
  172. package/examples/cancellable_booking/cancellable_booking.udl +0 -413
  173. package/examples/captured_payment/README.md +0 -3
  174. package/examples/captured_payment/captured_payment.hsx +0 -17
  175. package/examples/captured_payment/captured_payment.udl +0 -686
  176. package/examples/conditional_disbursement/README.md +0 -3
  177. package/examples/conditional_disbursement/conditional_disbursement.hsx +0 -15
  178. package/examples/conditional_disbursement/conditional_disbursement.udl +0 -418
  179. package/examples/credit_facility/README.md +0 -3
  180. package/examples/credit_facility/credit_facility.hsx +0 -24
  181. package/examples/credit_facility/credit_facility.udl +0 -1396
  182. package/examples/held_payment/README.md +0 -3
  183. package/examples/held_payment/held_payment.hsx +0 -13
  184. package/examples/held_payment/held_payment.udl +0 -569
  185. package/examples/instant_transfer/README.md +0 -3
  186. package/examples/instant_transfer/instant_transfer.hsx +0 -16
  187. package/examples/instant_transfer/instant_transfer.udl +0 -376
  188. package/examples/metered/README.md +0 -3
  189. package/examples/metered/metered.hsx +0 -13
  190. package/examples/metered/metered.udl +0 -253
  191. package/examples/pooled_split/README.md +0 -3
  192. package/examples/pooled_split/pooled_split.hsx +0 -15
  193. package/examples/pooled_split/pooled_split.udl +0 -432
  194. package/examples/premium_forward/README.md +0 -3
  195. package/examples/premium_forward/premium_forward.hsx +0 -19
  196. package/examples/premium_forward/premium_forward.udl +0 -604
  197. package/examples/reconciled_payout/README.md +0 -3
  198. package/examples/reconciled_payout/reconciled_payout.hsx +0 -13
  199. package/examples/reconciled_payout/reconciled_payout.udl +0 -312
  200. package/examples/rotating_pool/README.md +0 -3
  201. package/examples/rotating_pool/rotating_pool.hsx +0 -18
  202. package/examples/rotating_pool/rotating_pool.udl +0 -4370
  203. package/examples/scheduled/README.md +0 -3
  204. package/examples/scheduled/scheduled.hsx +0 -12
  205. package/examples/scheduled/scheduled.udl +0 -355
  206. package/examples/security_deposit/README.md +0 -3
  207. package/examples/security_deposit/security_deposit.hsx +0 -21
  208. package/examples/security_deposit/security_deposit.udl +0 -320
  209. package/examples/settlement_batch/README.md +0 -3
  210. package/examples/settlement_batch/settlement_batch.hsx +0 -19
  211. package/examples/settlement_batch/settlement_batch.udl +0 -757
  212. package/examples/swap/README.md +0 -3
  213. package/examples/swap/swap.hsx +0 -19
  214. package/examples/swap/swap.udl +0 -892
  215. package/examples/threshold_pool/README.md +0 -3
  216. package/examples/threshold_pool/threshold_pool.hsx +0 -16
  217. package/examples/threshold_pool/threshold_pool.udl +0 -680
  218. package/examples/weighted_distribution/README.md +0 -3
  219. package/examples/weighted_distribution/weighted_distribution.hsx +0 -20
  220. package/examples/weighted_distribution/weighted_distribution.udl +0 -348
  221. package/skills/hsx/SKILL.md +0 -449
  222. package/src/diagnostics.ts +0 -620
  223. package/src/emit.ts +0 -282
  224. package/src/entry-overrides.ts +0 -428
  225. package/src/ir.ts +0 -113
  226. package/src/limits.ts +0 -22
  227. package/src/lsp/server.ts +0 -460
  228. package/src/modules.ts +0 -452
  229. package/src/typecheck.ts +0 -5515
  230. package/std/SEMANTICS.md +0 -44
  231. package/std/money_flows/advance.hsx +0 -235
  232. package/std/money_flows/cancellable_booking.hsx +0 -402
  233. package/std/money_flows/captured_payment.hsx +0 -220
  234. package/std/money_flows/conditional_disbursement.hsx +0 -226
  235. package/std/money_flows/credit_facility.hsx +0 -214
  236. package/std/money_flows/held_payment.hsx +0 -812
  237. package/std/money_flows/index.hsx +0 -4
  238. package/std/money_flows/instant_transfer.hsx +0 -231
  239. package/std/money_flows/metered.hsx +0 -104
  240. package/std/money_flows/pooled_split.hsx +0 -109
  241. package/std/money_flows/premium_forward.hsx +0 -307
  242. package/std/money_flows/reconciled_payout.hsx +0 -119
  243. package/std/money_flows/rotating_pool.hsx +0 -305
  244. package/std/money_flows/scheduled.hsx +0 -599
  245. package/std/money_flows/security_deposit.hsx +0 -248
  246. package/std/money_flows/settlement_batch.hsx +0 -173
  247. package/std/money_flows/swap.hsx +0 -1192
  248. package/std/money_flows/threshold_pool.hsx +0 -307
  249. package/std/money_flows/weighted_distribution.hsx +0 -237
@@ -1,599 +0,0 @@
1
- module std.money_flows.scheduled
2
-
3
- // Calendar-anchored payments supporting fixed installment plans, recurring subscriptions, or debt obligations.
4
- //
5
- // ### Purpose
6
- // `scheduled` automates time-anchored payment series between one payer and one payee.
7
- // It supports three modes:
8
- //
9
- // 1. Installment plan: Partitions a fixed total amount into `count` installments, each collected on its own stored date.
10
- // 2. Open recurring subscription: Charges a recurring amount on an interval `every` until an `until` port fires.
11
- // 3. Debt obligation: Manages legally binding multi-installment debt with delinquency tracking, mandate evidence, and child payment records.
12
- //
13
- // ### Selection guidance
14
- // - vs `metered`: `scheduled` executes calendar-based recurring charges or fixed installment plans.
15
- // `metered` bills variable usage per event based on a committed rate card.
16
- // - vs `rotating_pool`: `scheduled` coordinates one payer to one payee on a calendar.
17
- // `rotating_pool` coordinates a multi-party peer circle where members rotate turns receiving the entire pot.
18
- //
19
- // ### Parameters
20
- // - `payer`: The paying party (or repayment source).
21
- // - `payee`: The beneficiary party receiving installment funds.
22
- // - `amount`: Total amount to partition into installments or recurring charge amount in minor units of currency `C`.
23
- // - `count`: Optional number of installments for installment or obligation modes.
24
- // - `every`: Recurrence cadence duration string (e.g. `"P30D"`, `"P1M"`).
25
- // - `first_due`: Stored date anchor for the first installment or charge.
26
- // - `mode`: Optional mode selector (`obligation`). Omit for standard installment plans.
27
- // - `debtor`: Optional debtor party when distinct from the payment source in obligation mode.
28
- // - `advance_to`: Optional third-party receiving upfront advance disbursements in obligation mode.
29
- // - `until`: Optional condition port triggering cancellation of recurring subscription runs.
30
- // - `month_end`: Optional handling policy for calendar month-end adjustments.
31
- // - `period_liability`: Optional liability policy for billing periods.
32
- // - `termination_drain`: Optional policy for draining balances on early termination.
33
- // - `mandate`: Optional condition port supplying formal debit mandate evidence.
34
- // - `derived_amount`: Optional calculated markup or fee rules.
35
- //
36
- // ### Decision ports
37
- // - `until`: Port terminating recurring subscriptions.
38
- // - `mandate`: Port supplying mandate verification evidence for direct debit obligations.
39
- //
40
- // ### Example
41
- // ```hsx
42
- // program scheduled_example "Scheduled example"
43
- // import { scheduled } from "std/money_flows"
44
- // party payer: business
45
- // party payee: business
46
- // settlement installments = scheduled {
47
- // payer: payer
48
- // payee: payee
49
- // amount: totalAmount: money(SAR)
50
- // count: 3
51
- // every: P30D
52
- // first_due: firstDueAt
53
- // }
54
- // ```
55
- export instrument scheduled<C>(
56
- payer: party,
57
- payee: party,
58
- amount: money<C>,
59
- count: optional<integer>,
60
- every: text,
61
- first_due: date,
62
- mode: optional<text>,
63
- debtor: optional<party>,
64
- advance_to: optional<party>,
65
- until: optional<condition>,
66
- month_end: optional<text>,
67
- period_liability: optional<text>,
68
- termination_drain: optional<text>,
69
- mandate: optional<condition>,
70
- derived_amount: optional<unknown>
71
- ) {
72
- agent_description: "Reach for scheduled when one payer owes one payee on a calendar. It covers a plan of fixed installments partitioning one total, an obligation whose installments are paid through child payment records and can go delinquent, and an open period charge that repeats until a cancel port fires. Rotating pool is for a group taking turns.";
73
- when_not(mode) {
74
- let(instrument_words): words(instrument);
75
-
76
- title: sentence(instrument);
77
- summary: "{count}-installment schedule from {payer} to {payee}";
78
- description: "Scheduled payment: the {payer} pays {amount} to the {payee} in {count} installments, one every {every}";
79
- partitions: { totalField: amount; pieceFields: names("installment", count, "Amount"); };
80
-
81
- fields {
82
- amount: {
83
- type: money<C>;
84
- description: "The total scheduled amount in {C} minor units; the installment fields below partition it exactly";
85
- }
86
- first_due: {
87
- type: date;
88
- description: "Due date of the first installment; installment k falls {every} after its predecessor";
89
- }
90
- for i in count {
91
- let(remainder_words): if_eq(i, 1, " (carries the integer-division remainder)", "");
92
- installment[i]Amount: {
93
- type: money<C>;
94
- description: "Installment {i} of {count}{remainder_words}: a stored share of {amount}, collected on its own stored-date anchor";
95
- }
96
- }
97
- }
98
-
99
- parties {
100
- payer: payer;
101
- beneficiary: payee;
102
- }
103
-
104
- lifecycle {
105
- states active;
106
- for i in sub(count, 1) { states collecting_[i]; }
107
- states settled;
108
- initial active;
109
- for i in count {
110
- let(from_state): if_eq(i, 1, "active", concat("collecting_", sub(i, 1)));
111
- let(to_state): if_eq(i, count, "settled", concat("collecting_", i));
112
- on pay_installment_[i]: from_state -> to_state;
113
- }
114
- }
115
-
116
- action create {
117
- agent_description: "Create the installment plan. No money moves. The total and its per-installment amounts are fixed here, and each installment collects on its own stored due date from then on.";
118
- summary: "Create a {instrument_words} plan";
119
- steps: [];
120
- }
121
-
122
- for i in count {
123
- action pay_installment_[i] {
124
- let(first): if_eq(i, 1, true, false);
125
- let(offset): scale_duration(every, sub(i, 1));
126
- let(installment_field): concat("installment", i, "Amount");
127
- summary: "Collect installment {i} of {count}";
128
- when(first) { due: { field: first_due; }; sandbox_failure_point: funding; }
129
- when_not(first) { due: { field: first_due; offset: offset; }; }
130
- moves: [{ amount: installment_field; from: payer; to: beneficiary; key: transfer; }];
131
- steps: [];
132
- }
133
- }
134
- }
135
-
136
- when_eq(mode, obligation) {
137
- when(count) {
138
- let(instrument_words): words(instrument);
139
- let(amount_field): concat(amount);
140
- let(first_due_field): concat(first_due);
141
- let(payer_field): camel(concat(payer, "_account_id"));
142
- let(payer_words): words(payer);
143
- let(payee_field): camel(concat(payee, "_account_id"));
144
- let(payee_words): words(payee);
145
- let(debtor_field): camel(concat(debtor, "_account_id"));
146
- let(debtor_words): words(debtor);
147
- let(advance_field): camel(concat(advance_to, "_account_id"));
148
- let(advance_words): words(advance_to);
149
- let(parent_ref_field): camel(concat(instrument, "_id"));
150
- let(has_advance): advance_to;
151
- let(same_debtor): if_eq(payer, debtor, true, false);
152
- let(parent_statuses): concat_lists(["active"], names("installment_", count, "_delinquent"));
153
- let(delinquent_statuses): names("installment_", count, "_delinquent");
154
- let(last_offset): scale_duration(every, sub(count, 1));
155
- let(reference_filter_key): "x-hyperscale-reference-filter";
156
- let(fee_port_key): "x-hyperscale-fee-collection-port";
157
- let(derived_field): concat(get(derived_amount, "field"));
158
- let(derived_source): concat(get(derived_amount, "source"));
159
- let(derived_rule): get(derived_amount, "rule");
160
- let(derived_bps): add(derived_rule, 0);
161
- let(derived_percent): concat(derived_rule);
162
- let(derived_bearer): get(derived_amount, "bearer");
163
- let(derived_bearer_field): camel(concat(derived_bearer, "_account_id"));
164
- let(metadata_instrument_key): "metadata.instrumentId";
165
- let(metadata_instance_key): "metadata.instrumentInstanceId";
166
- let(metadata_phase_key): "metadata.phase";
167
-
168
- title: sentence(instrument);
169
- summary: "{count}-anchor obligation for {debtor_words}";
170
- description: "Installment obligation for {debtor_words}; {payer_words} pays {payee_words} against exact stored anchors";
171
- partitions: { total_field: amount_field; piece_fields: names("installment", count, "Amount"); };
172
- when(derived_amount) {
173
- computes derived: {
174
- field: derived_field;
175
- source_field: derived_source;
176
- rule: { kind: percentage_of; bps: derived_bps; };
177
- rounding: floor;
178
- };
179
- }
180
- for i in count {
181
- aggregate_invariants: {
182
- child_field: "amount";
183
- child_instrument_id: concat(instrument, "_installment_", i, "_payment");
184
- child_ref_field: parent_ref_field;
185
- child_statuses: [paid];
186
- parent_field: concat("installment", i, "Amount");
187
- };
188
- }
189
- required: concat_lists(
190
- [payee_field, payer_field, "currency", amount_field, first_due_field],
191
- names("installment", count, "Amount"),
192
- if_eq(has_advance, false, [], [advance_field]),
193
- if_eq(same_debtor, true, [], [debtor_field]),
194
- if_eq(derived_amount, false, [], ["platformAccountId"])
195
- );
196
-
197
- fields {
198
- when(has_advance) {
199
- [payee_field]: {
200
- type: account;
201
- description: "The {payee_words} account";
202
- [reference_filter_key]: { column: role; values: [customer_balance]; };
203
- }
204
- }
205
- when(same_debtor) {
206
- [payer_field]: {
207
- type: account;
208
- description: "The {payer_words} account";
209
- [reference_filter_key]: { column: role; values: [customer_balance]; };
210
- }
211
- }
212
- when_not(same_debtor) {
213
- [debtor_field]: {
214
- type: account;
215
- description: "The {debtor_words} account";
216
- [reference_filter_key]: { column: role; values: [customer_balance]; };
217
- }
218
- }
219
- when(derived_amount) {
220
- platform_account_id: {
221
- type: account;
222
- description: "The product fee collection account";
223
- [fee_port_key]: true;
224
- [reference_filter_key]: { column: role; values: [customer_balance, product_revenue]; };
225
- }
226
- }
227
- currency: {
228
- type: text;
229
- description: "ISO 4217 currency code";
230
- max_length: 3;
231
- min_length: 3;
232
- pattern: "^[A-Z]{3}$";
233
- }
234
- amount: {
235
- type: money<C>;
236
- description: "The principal in {C} minor units; the stored installment anchors partition it exactly";
237
- }
238
- first_due: {
239
- type: date;
240
- description: "Due date of the first installment; later anchors use fixed offsets of {every}";
241
- }
242
- when(derived_amount) {
243
- [derived_field]: {
244
- type: money<C>;
245
- optional: true;
246
- pattern: "^[1-9][0-9]{0,17}$";
247
- description: "Machine-computed {derived_percent} of {derived_source}; callers never supply it";
248
- }
249
- }
250
- for i in count {
251
- let(remainder_words): if_eq(i, 1, " (carries the integer-division remainder)", "");
252
- installment[i]Amount: {
253
- type: money<C>;
254
- description: "Stored amount for installment {i} of {count}{remainder_words}";
255
- }
256
- installment[i]DelinquentAfter: {
257
- type: date;
258
- optional: true;
259
- description: "Machine-set marker proving installment {i} reached its stored due date while unpaid";
260
- }
261
- }
262
- }
263
-
264
- parties {
265
- when(has_advance) { beneficiary: advance_to; }
266
- when_not(has_advance) { beneficiary: payee; }
267
- when_not(same_debtor) { payer: payer; }
268
- }
269
-
270
- lifecycle {
271
- states draft;
272
- when(has_advance) { states approved; }
273
- states active;
274
- let(states_key): "states";
275
- [states_key]: delinquent_statuses;
276
- states written_off repaid;
277
- initial draft;
278
- when(has_advance) { on approve: draft -> approved; on advance: approved -> active; }
279
- when_not(has_advance) { on approve: draft -> active; }
280
- for i in count {
281
- let(target): concat("installment_", i, "_delinquent");
282
- let(other_states): without(parent_statuses, target);
283
- on collect_installment_[i]: delinquent_statuses -> active;
284
- let(mark_action): concat("mark_installment_", i, "_delinquent");
285
- on [mark_action]: other_states -> target;
286
- }
287
- on complete: parent_statuses -> repaid;
288
- when(has_advance) { on write_off: draft|approved|parent_statuses -> written_off; }
289
- when_not(has_advance) { on write_off: draft|parent_statuses -> written_off; }
290
- }
291
-
292
- action create {
293
- agent_description: "Create the obligation in draft with its principal split across stored installment anchors. Approve is the next call. Where the product declares a derived fee, creating the obligation also moves that fee from its bearer to the platform account.";
294
- summary: "Create a {instrument_words} obligation";
295
- when(derived_amount) {
296
- moves: [{
297
- key: "derived_amount";
298
- operation: "internal_transfer.create";
299
- bind: {
300
- amount: { from: instance; path: concat("fields.", derived_field); };
301
- currency: { from: instance; path: fields.currency; };
302
- destination_account_id: { from: instance; path: fields.platformAccountId; };
303
- source_account_id: { from: instance; path: concat("fields.", derived_bearer_field); };
304
- [metadata_instrument_key]: { from: const; value: instrument; };
305
- [metadata_instance_key]: { from: instance; path: instrumentInstanceId; };
306
- [metadata_phase_key]: { from: const; value: create; };
307
- product_id: { from: instance; path: productId; };
308
- };
309
- capture: { create_derived_amount_transfer_id: transferId; };
310
- }];
311
- }
312
- steps: [];
313
- }
314
- action approve {
315
- agent_description: "Approve the draft obligation and freeze its principal partition and due anchors. The obligation must be in draft. No money moves. Where the product advances funds, approve is what makes advance available.";
316
- summary: "Approve the immutable principal partition and stored anchors";
317
- steps: [];
318
- }
319
- when(has_advance) {
320
- action advance {
321
- agent_description: "Pay the principal out to the advance recipient and put the obligation into repayment. The obligation must be approved. This moves the whole principal and does not reverse, so approve is the last point to stop.";
322
- summary: "Advance the principal to the {advance_words}; the internal ledger receipt is the confirmation";
323
- moves: [{ amount: amount_field; from: payee; to: beneficiary; key: advance; }];
324
- steps: [];
325
- }
326
- }
327
- for i in count {
328
- let(child_id): concat(instrument, "_installment_", i, "_payment");
329
- let(target_field): concat("installment", i, "Amount");
330
- let(marker_field): concat("installment", i, "DelinquentAfter");
331
- let(delinquent_action): concat("mark_installment_", i, "_delinquent");
332
- let(offset): scale_duration(every, sub(i, 1));
333
- action collect_installment_[i] {
334
- let(first): if_eq(i, 1, true, false);
335
- summary: "Close delinquent installment {i} after linked payments reach its stored amount";
336
- when(first) { due: { field: first_due_field; }; }
337
- when_not(first) { due: { field: first_due_field; offset: offset; }; }
338
- requires aggregate: {
339
- check: { amount_field: "amount"; kind: sum_exactly; target_field: target_field; };
340
- instrument_id: child_id;
341
- over: children;
342
- ref_field: parent_ref_field;
343
- statuses: [paid];
344
- };
345
- steps: [];
346
- }
347
- action [delinquent_action] {
348
- let(first): if_eq(i, 1, true, false);
349
- summary: "Mark installment {i} delinquent only when its due anchor is unmet";
350
- when(first) { due: { field: first_due_field; }; }
351
- when_not(first) { due: { field: first_due_field; offset: offset; }; }
352
- requires aggregate: {
353
- check: { amount_field: "amount"; kind: sum_below; target_field: target_field; };
354
- instrument_id: child_id;
355
- over: children;
356
- ref_field: parent_ref_field;
357
- statuses: [paid];
358
- };
359
- sets_at: { field: marker_field; marker: true; offset: PT1S; };
360
- steps: [];
361
- }
362
- }
363
- action complete {
364
- summary: "Close the obligation only after every stored anchor is paid exactly";
365
- due: { field: first_due_field; offset: last_offset; };
366
- for i in count {
367
- requires aggregate: {
368
- check: { amount_field: "amount"; kind: sum_exactly; target_field: concat("installment", i, "Amount"); };
369
- instrument_id: concat(instrument, "_installment_", i, "_payment");
370
- over: children;
371
- ref_field: parent_ref_field;
372
- statuses: [paid];
373
- };
374
- }
375
- steps: [];
376
- }
377
- action write_off {
378
- agent_description: "Write off the remaining exposure and stop the obligation. No money moves and nothing is refunded. This is the bookkeeping exit for debt that will not be collected. A written-off obligation never returns to repayment.";
379
- summary: "Write off the remaining exposure without moving money";
380
- steps: [];
381
- }
382
-
383
- instruments {
384
- for i in count {
385
- instrument {
386
- let(has_mandate): mandate;
387
- id: concat(instrument, "_installment_", i, "_payment");
388
- generatedPrefix: true;
389
- title: concat(sentence(instrument), " Installment ", i, " Payment");
390
- agent_description: concat("One payment against installment ", i, " of the obligation. Payments can be partial, so create a record, repay it, and the parent installment closes once its paid records sum to the stored anchor amount.");
391
- summary: concat("Anchor-bound payment for installment ", i);
392
- description: concat("One partial or full payment bound to installment ", i, " of ", instrument, "; the operation name fixes the anchor and ", parent_ref_field, " fixes the obligation");
393
- fields {
394
- amount: {
395
- type: money<C>;
396
- description: concat("Positive payment amount capped with its paid siblings at installment", i, "Amount");
397
- }
398
- currency: {
399
- type: text;
400
- description: "ISO 4217 currency code";
401
- max_length: 3;
402
- min_length: 3;
403
- pattern: "^[A-Z]{3}$";
404
- }
405
- [parent_ref_field]: {
406
- type: ref<instrument>;
407
- description: "The exact {instrument_words} this payment belongs to";
408
- pattern: concat("^", prefix(instrument), "_(sandbox|live)_[a-z0-9]{8,64}$");
409
- }
410
- }
411
- parties { beneficiary: payee; payer: payer; }
412
- lifecycle {
413
- states created paid refunded;
414
- initial created;
415
- on repay: created -> paid;
416
- on refund: paid -> refunded;
417
- }
418
- action create {
419
- agent_description: concat("Open a payment record against installment ", i, ". The parent obligation must be live. No money moves yet. Repay is what pays.");
420
- summary: concat("Create a payment record for installment ", i);
421
- requires refs: {
422
- field: parent_ref_field;
423
- statuses: parent_statuses;
424
- bind: {
425
- currency: fields.currency;
426
- [payer_field]: concat("fields.", payer_field);
427
- [payee_field]: concat("fields.", payee_field);
428
- };
429
- };
430
- steps: [];
431
- }
432
- action repay {
433
- agent_description: concat("Pay this record's amount toward installment ", i, ". The record must be created and its obligation live. Refused when this amount plus the records already paid against that installment would pass the stored amount. This moves the payer's money. Where a mandate applies, its declared parties may call it and an evidence reference is required.");
434
- summary: concat("Pay a partial or full amount against installment ", i);
435
- when(has_mandate) {
436
- input: {
437
- additional_properties: false;
438
- properties: {
439
- evidence_reference: {
440
- description: "Required decision reference retained in the operation receipt";
441
- max_length: 180;
442
- min_length: 1;
443
- type: string;
444
- };
445
- };
446
- required: [evidenceReference];
447
- type: object;
448
- };
449
- capture_input: { mandate_evidence_reference: evidenceReference; };
450
- port: { allowed_parties: mandate_allowed; };
451
- }
452
- requires refs: {
453
- field: parent_ref_field;
454
- statuses: parent_statuses;
455
- match: {
456
- let(payer_match): concat("fields.", payer_field);
457
- let(payee_match): concat("fields.", payee_field);
458
- let(currency_match): "fields.currency";
459
- [payer_match]: payer_match;
460
- [payee_match]: payee_match;
461
- [currency_match]: currency_match;
462
- };
463
- };
464
- requires exposure: {
465
- amount_field: "amount";
466
- anchor_field: parent_ref_field;
467
- cap_field: concat("installment", i, "Amount");
468
- cap_on_anchor: true;
469
- child_instrument_id: concat(instrument, "_installment_", i, "_payment");
470
- statuses: [paid];
471
- };
472
- moves: [{ amount: "amount"; from: payer; to: beneficiary; key: repayment; }];
473
- steps: [];
474
- }
475
- action refund {
476
- agent_description: concat("Return one whole installment ", i, " payment to the payer. The record must be paid, and the refund is always its full amount, so split a payment across records when a partial refund could be needed. This moves money back.");
477
- summary: concat("Refund this one stored installment ", i, " payment whole");
478
- requires refs: {
479
- field: parent_ref_field;
480
- statuses: parent_statuses;
481
- match: {
482
- let(payer_match): concat("fields.", payer_field);
483
- let(payee_match): concat("fields.", payee_field);
484
- let(currency_match): "fields.currency";
485
- [payer_match]: payer_match;
486
- [payee_match]: payee_match;
487
- [currency_match]: currency_match;
488
- };
489
- };
490
- moves: [{ amount: "amount"; from: beneficiary; to: payer; key: refund; }];
491
- steps: [];
492
- }
493
- }
494
- }
495
- }
496
- }
497
-
498
- when_not(count) {
499
- let(amount_field): concat(amount);
500
- let(until_name): concat(until);
501
- title: sentence(instrument);
502
- summary: "Open {every} obligation for {payer}";
503
- description: "Open monthly obligation for {payer}; one period liability at a time";
504
-
505
- fields {
506
- amount: {
507
- type: money<C>;
508
- description: "Exact charge for one {every} period in {C} minor units";
509
- }
510
- currency: {
511
- type: text;
512
- const: C;
513
- description: "ISO 4217 currency code";
514
- }
515
- first_due: {
516
- type: date;
517
- description: "First period anchor; missing Gregorian month days clamp to the target month's final day";
518
- }
519
- }
520
-
521
- parties { payer: payer; beneficiary: payee; }
522
-
523
- lifecycle {
524
- states active period_open canceled;
525
- initial active;
526
- on open_period: active -> period_open;
527
- on collect_period: period_open -> active;
528
- on [until]: active|period_open -> canceled;
529
- }
530
-
531
- action create {
532
- agent_description: "Create the open obligation before its first period anchor. No money moves. The charge for one period and the first anchor are fixed here, and periods then open on their own.";
533
- summary: "Create the open obligation before its first anchor";
534
- steps: [];
535
- }
536
-
537
- action open_period {
538
- summary: "Open exactly one period liability without moving money";
539
- due: {
540
- field: first_due;
541
- every: {
542
- period: { calendar: gregorian; month_end: month_end; months: 1; };
543
- until_action: until_name;
544
- liability: period_liability;
545
- delinquency: parent_policy;
546
- drain_action: termination_drain;
547
- };
548
- };
549
- steps: [];
550
- }
551
-
552
- action collect_period {
553
- agent_description: "Collect one open period charge from the payer, with the mandate evidence reference kept in the receipt. A period must be open. The mandate's declared parties may call it, or the payer when no mandate is wired. This moves the period amount and does not reverse.";
554
- summary: "Collect one authored period charge with mandate evidence";
555
- input: {
556
- additional_properties: false;
557
- properties: {
558
- evidence_reference: {
559
- description: "Required decision reference retained in the operation receipt";
560
- max_length: 180;
561
- min_length: 1;
562
- type: string;
563
- };
564
- };
565
- required: [evidenceReference];
566
- type: object;
567
- };
568
- capture_input: { mandate_evidence_reference: evidenceReference; };
569
- when(mandate) { port: { allowed_parties: mandate_allowed; }; }
570
- when_not(mandate) { port: { allowed_parties: [payer]; }; }
571
- moves: [{ amount: amount_field; from: payer; to: beneficiary; key: period_collection; }];
572
- sandbox_failure_point: funding;
573
- steps: [];
574
- }
575
-
576
- action [until] {
577
- agent_description: "Cancel the open schedule for good and drain any period liability still standing. Only the payer may call it, and the reference fields the port declares are required. No further period opens and there is no reactivation.";
578
- summary: "Cancel the open schedule and drain any period liability";
579
- when(until_fields) {
580
- input: {
581
- additional_properties: false;
582
- properties: {
583
- for item in keys(until_fields) {
584
- [item]: get(until_fields, item);
585
- }
586
- };
587
- required: keys(until_fields);
588
- type: object;
589
- };
590
- capture_input: {
591
- for item in keys(until_fields) { [item]: item; }
592
- };
593
- }
594
- port { allowed_parties: until_allowed; };
595
- steps: [];
596
- }
597
- }
598
- }
599
- }