@azx-pbc/axiom-builder 0.0.0-stage → 0.0.6

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 (231) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +648 -2
  3. package/dist/cards/selectors.d.ts +70 -0
  4. package/dist/cards/selectors.d.ts.map +1 -0
  5. package/dist/cards/selectors.js +194 -0
  6. package/dist/cards/selectors.js.map +1 -0
  7. package/dist/definition.d.ts +36 -0
  8. package/dist/definition.d.ts.map +1 -0
  9. package/dist/definition.js +48 -0
  10. package/dist/definition.js.map +1 -0
  11. package/dist/evidence/cases.d.ts +44 -0
  12. package/dist/evidence/cases.d.ts.map +1 -0
  13. package/dist/evidence/cases.js +578 -0
  14. package/dist/evidence/cases.js.map +1 -0
  15. package/dist/evidence/repairs.d.ts +99 -0
  16. package/dist/evidence/repairs.d.ts.map +1 -0
  17. package/dist/evidence/repairs.js +181 -0
  18. package/dist/evidence/repairs.js.map +1 -0
  19. package/dist/evidence/summary.d.ts +35 -0
  20. package/dist/evidence/summary.d.ts.map +1 -0
  21. package/dist/evidence/summary.js +38 -0
  22. package/dist/evidence/summary.js.map +1 -0
  23. package/dist/i18n/gaps.d.ts +3 -0
  24. package/dist/i18n/gaps.d.ts.map +1 -0
  25. package/dist/i18n/gaps.js +2 -0
  26. package/dist/i18n/gaps.js.map +1 -0
  27. package/dist/i18n/message-slots.d.ts +19 -0
  28. package/dist/i18n/message-slots.d.ts.map +1 -0
  29. package/dist/i18n/message-slots.js +101 -0
  30. package/dist/i18n/message-slots.js.map +1 -0
  31. package/dist/i18n/text-locale.d.ts +13 -0
  32. package/dist/i18n/text-locale.d.ts.map +1 -0
  33. package/dist/i18n/text-locale.js +14 -0
  34. package/dist/i18n/text-locale.js.map +1 -0
  35. package/dist/i18n/translations.d.ts +56 -0
  36. package/dist/i18n/translations.d.ts.map +1 -0
  37. package/dist/i18n/translations.js +103 -0
  38. package/dist/i18n/translations.js.map +1 -0
  39. package/dist/impact/references.d.ts +143 -0
  40. package/dist/impact/references.d.ts.map +1 -0
  41. package/dist/impact/references.js +429 -0
  42. package/dist/impact/references.js.map +1 -0
  43. package/dist/index.d.ts +86 -0
  44. package/dist/index.d.ts.map +1 -0
  45. package/dist/index.js +58 -0
  46. package/dist/index.js.map +1 -0
  47. package/dist/preview/compile-preview.d.ts +67 -0
  48. package/dist/preview/compile-preview.d.ts.map +1 -0
  49. package/dist/preview/compile-preview.js +286 -0
  50. package/dist/preview/compile-preview.js.map +1 -0
  51. package/dist/preview/handle.d.ts +119 -0
  52. package/dist/preview/handle.d.ts.map +1 -0
  53. package/dist/preview/handle.js +2 -0
  54. package/dist/preview/handle.js.map +1 -0
  55. package/dist/preview/held.d.ts +39 -0
  56. package/dist/preview/held.d.ts.map +1 -0
  57. package/dist/preview/held.js +15 -0
  58. package/dist/preview/held.js.map +1 -0
  59. package/dist/preview/measurement.d.ts +5 -0
  60. package/dist/preview/measurement.d.ts.map +1 -0
  61. package/dist/preview/measurement.js +18 -0
  62. package/dist/preview/measurement.js.map +1 -0
  63. package/dist/publish/build.d.ts +33 -0
  64. package/dist/publish/build.d.ts.map +1 -0
  65. package/dist/publish/build.js +15 -0
  66. package/dist/publish/build.js.map +1 -0
  67. package/dist/publish/diff.d.ts +208 -0
  68. package/dist/publish/diff.d.ts.map +1 -0
  69. package/dist/publish/diff.js +227 -0
  70. package/dist/publish/diff.js.map +1 -0
  71. package/dist/publish/review.d.ts +41 -0
  72. package/dist/publish/review.d.ts.map +1 -0
  73. package/dist/publish/review.js +22 -0
  74. package/dist/publish/review.js.map +1 -0
  75. package/dist/schema/dataset-input.d.ts +30 -0
  76. package/dist/schema/dataset-input.d.ts.map +1 -0
  77. package/dist/schema/dataset-input.js +58 -0
  78. package/dist/schema/dataset-input.js.map +1 -0
  79. package/dist/schema/dataset.d.ts +166 -0
  80. package/dist/schema/dataset.d.ts.map +1 -0
  81. package/dist/schema/dataset.js +281 -0
  82. package/dist/schema/dataset.js.map +1 -0
  83. package/dist/schema/declarations.d.ts +15 -0
  84. package/dist/schema/declarations.d.ts.map +1 -0
  85. package/dist/schema/declarations.js +47 -0
  86. package/dist/schema/declarations.js.map +1 -0
  87. package/dist/schema/document.d.ts +504 -0
  88. package/dist/schema/document.d.ts.map +1 -0
  89. package/dist/schema/document.js +337 -0
  90. package/dist/schema/document.js.map +1 -0
  91. package/dist/schema/error.d.ts +15 -0
  92. package/dist/schema/error.d.ts.map +1 -0
  93. package/dist/schema/error.js +18 -0
  94. package/dist/schema/error.js.map +1 -0
  95. package/dist/schema/field-id.d.ts +8 -0
  96. package/dist/schema/field-id.d.ts.map +1 -0
  97. package/dist/schema/field-id.js +47 -0
  98. package/dist/schema/field-id.js.map +1 -0
  99. package/dist/schema/layout.d.ts +118 -0
  100. package/dist/schema/layout.d.ts.map +1 -0
  101. package/dist/schema/layout.js +101 -0
  102. package/dist/schema/layout.js.map +1 -0
  103. package/dist/schema/palette.d.ts +103 -0
  104. package/dist/schema/palette.d.ts.map +1 -0
  105. package/dist/schema/palette.js +140 -0
  106. package/dist/schema/palette.js.map +1 -0
  107. package/dist/schema/read.d.ts +49 -0
  108. package/dist/schema/read.d.ts.map +1 -0
  109. package/dist/schema/read.js +942 -0
  110. package/dist/schema/read.js.map +1 -0
  111. package/dist/store/apply-dataset.d.ts +14 -0
  112. package/dist/store/apply-dataset.d.ts.map +1 -0
  113. package/dist/store/apply-dataset.js +513 -0
  114. package/dist/store/apply-dataset.js.map +1 -0
  115. package/dist/store/apply.d.ts +37 -0
  116. package/dist/store/apply.d.ts.map +1 -0
  117. package/dist/store/apply.js +2269 -0
  118. package/dist/store/apply.js.map +1 -0
  119. package/dist/store/candidate.d.ts +37 -0
  120. package/dist/store/candidate.d.ts.map +1 -0
  121. package/dist/store/candidate.js +178 -0
  122. package/dist/store/candidate.js.map +1 -0
  123. package/dist/store/commands/coverage.d.ts +58 -0
  124. package/dist/store/commands/coverage.d.ts.map +1 -0
  125. package/dist/store/commands/coverage.js +354 -0
  126. package/dist/store/commands/coverage.js.map +1 -0
  127. package/dist/store/commands/dataset.d.ts +26 -0
  128. package/dist/store/commands/dataset.d.ts.map +1 -0
  129. package/dist/store/commands/dataset.js +368 -0
  130. package/dist/store/commands/dataset.js.map +1 -0
  131. package/dist/store/commands/document.d.ts +21 -0
  132. package/dist/store/commands/document.d.ts.map +1 -0
  133. package/dist/store/commands/document.js +160 -0
  134. package/dist/store/commands/document.js.map +1 -0
  135. package/dist/store/commands/field.d.ts +32 -0
  136. package/dist/store/commands/field.d.ts.map +1 -0
  137. package/dist/store/commands/field.js +572 -0
  138. package/dist/store/commands/field.js.map +1 -0
  139. package/dist/store/commands/layout.d.ts +16 -0
  140. package/dist/store/commands/layout.d.ts.map +1 -0
  141. package/dist/store/commands/layout.js +70 -0
  142. package/dist/store/commands/layout.js.map +1 -0
  143. package/dist/store/commands/parse.d.ts +19 -0
  144. package/dist/store/commands/parse.d.ts.map +1 -0
  145. package/dist/store/commands/parse.js +328 -0
  146. package/dist/store/commands/parse.js.map +1 -0
  147. package/dist/store/commands/readers.d.ts +198 -0
  148. package/dist/store/commands/readers.d.ts.map +1 -0
  149. package/dist/store/commands/readers.js +711 -0
  150. package/dist/store/commands/readers.js.map +1 -0
  151. package/dist/store/commands/rule.d.ts +24 -0
  152. package/dist/store/commands/rule.d.ts.map +1 -0
  153. package/dist/store/commands/rule.js +379 -0
  154. package/dist/store/commands/rule.js.map +1 -0
  155. package/dist/store/commands/shapes.d.ts +900 -0
  156. package/dist/store/commands/shapes.d.ts.map +1 -0
  157. package/dist/store/commands/shapes.js +166 -0
  158. package/dist/store/commands/shapes.js.map +1 -0
  159. package/dist/store/commands/suggestions.d.ts +37 -0
  160. package/dist/store/commands/suggestions.d.ts.map +1 -0
  161. package/dist/store/commands/suggestions.js +177 -0
  162. package/dist/store/commands/suggestions.js.map +1 -0
  163. package/dist/store/commands.d.ts +16 -0
  164. package/dist/store/commands.d.ts.map +1 -0
  165. package/dist/store/commands.js +13 -0
  166. package/dist/store/commands.js.map +1 -0
  167. package/dist/store/dataset-candidate.d.ts +33 -0
  168. package/dist/store/dataset-candidate.d.ts.map +1 -0
  169. package/dist/store/dataset-candidate.js +31 -0
  170. package/dist/store/dataset-candidate.js.map +1 -0
  171. package/dist/store/dataset-pin.d.ts +93 -0
  172. package/dist/store/dataset-pin.d.ts.map +1 -0
  173. package/dist/store/dataset-pin.js +86 -0
  174. package/dist/store/dataset-pin.js.map +1 -0
  175. package/dist/store/dataset-store.d.ts +96 -0
  176. package/dist/store/dataset-store.d.ts.map +1 -0
  177. package/dist/store/dataset-store.js +112 -0
  178. package/dist/store/dataset-store.js.map +1 -0
  179. package/dist/store/history.d.ts +149 -0
  180. package/dist/store/history.d.ts.map +1 -0
  181. package/dist/store/history.js +173 -0
  182. package/dist/store/history.js.map +1 -0
  183. package/dist/store/measurement.d.ts +5 -0
  184. package/dist/store/measurement.d.ts.map +1 -0
  185. package/dist/store/measurement.js +20 -0
  186. package/dist/store/measurement.js.map +1 -0
  187. package/dist/store/notify.d.ts +11 -0
  188. package/dist/store/notify.d.ts.map +1 -0
  189. package/dist/store/notify.js +26 -0
  190. package/dist/store/notify.js.map +1 -0
  191. package/dist/store/published.d.ts +50 -0
  192. package/dist/store/published.d.ts.map +1 -0
  193. package/dist/store/published.js +27 -0
  194. package/dist/store/published.js.map +1 -0
  195. package/dist/store/refusals.d.ts +175 -0
  196. package/dist/store/refusals.d.ts.map +1 -0
  197. package/dist/store/refusals.js +160 -0
  198. package/dist/store/refusals.js.map +1 -0
  199. package/dist/store/steps.d.ts +14 -0
  200. package/dist/store/steps.d.ts.map +1 -0
  201. package/dist/store/steps.js +124 -0
  202. package/dist/store/steps.js.map +1 -0
  203. package/dist/store/store.d.ts +142 -0
  204. package/dist/store/store.d.ts.map +1 -0
  205. package/dist/store/store.js +136 -0
  206. package/dist/store/store.js.map +1 -0
  207. package/dist/store/unchanged.d.ts +12 -0
  208. package/dist/store/unchanged.d.ts.map +1 -0
  209. package/dist/store/unchanged.js +35 -0
  210. package/dist/store/unchanged.js.map +1 -0
  211. package/dist/visual/emit.d.ts +21 -0
  212. package/dist/visual/emit.d.ts.map +1 -0
  213. package/dist/visual/emit.js +133 -0
  214. package/dist/visual/emit.js.map +1 -0
  215. package/dist/visual/parse.d.ts +32 -0
  216. package/dist/visual/parse.d.ts.map +1 -0
  217. package/dist/visual/parse.js +330 -0
  218. package/dist/visual/parse.js.map +1 -0
  219. package/dist/visual/project.d.ts +40 -0
  220. package/dist/visual/project.d.ts.map +1 -0
  221. package/dist/visual/project.js +349 -0
  222. package/dist/visual/project.js.map +1 -0
  223. package/dist/visual/represent.d.ts +50 -0
  224. package/dist/visual/represent.d.ts.map +1 -0
  225. package/dist/visual/represent.js +66 -0
  226. package/dist/visual/represent.js.map +1 -0
  227. package/dist/visual/vocabulary.d.ts +121 -0
  228. package/dist/visual/vocabulary.d.ts.map +1 -0
  229. package/dist/visual/vocabulary.js +32 -0
  230. package/dist/visual/vocabulary.js.map +1 -0
  231. package/package.json +52 -3
@@ -0,0 +1,513 @@
1
+ import { AxiomError, assertNever, canonicalizeRestricted, daysFromCivil, parsePlainDate, toWireValue, wireDeclarationOfColumn, } from '@azx-pbc/axiom-runtime';
2
+ import { isDatasetScalar } from '../schema/dataset.js';
3
+ import { datasetRefusal } from './refusals.js';
4
+ import { unchanged } from './unchanged.js';
5
+ function refuse(code, path, detail, subject = {}) {
6
+ return { ok: false, refusal: datasetRefusal(code, path, detail, subject) };
7
+ }
8
+ function applied(document) {
9
+ return { ok: true, document };
10
+ }
11
+ /** The identity every rewrite carries through, so no command invents a new one. */
12
+ function identityOf(document) {
13
+ return { tableId: document.tableId, tableVersion: document.tableVersion };
14
+ }
15
+ /** The clock binding, restated as the union arm the document's own members state. */
16
+ function bindingOf(document) {
17
+ return document.binding === 'data'
18
+ ? { binding: 'data', effectiveFrom: document.effectiveFrom, effectiveTo: document.effectiveTo }
19
+ : { binding: 'behavior' };
20
+ }
21
+ /** The hit policy, restated as the union arm the document's own members state. */
22
+ function policyOf(document) {
23
+ return document.hitPolicy === 'COLLECT'
24
+ ? { hitPolicy: 'COLLECT', collectLimit: document.collectLimit }
25
+ : { hitPolicy: document.hitPolicy };
26
+ }
27
+ /**
28
+ * A keyed table's rows as the one row type a rewrite reads.
29
+ *
30
+ * A lookup row and a decision row are different values, and a union of two array types has no
31
+ * callable `map`; reading them as the common `{ rowId, key }` shape the rewrites share keeps one
32
+ * walk. The authored union is restored by `checkedRow`, which is the only place the per-kind
33
+ * contract is decided.
34
+ */
35
+ function rowsOf(document) {
36
+ return document.rows;
37
+ }
38
+ /** The command names the table this store holds, or the refusal that says which one it holds. */
39
+ function wrongTable(document, tableId) {
40
+ if (document.tableId === tableId)
41
+ return undefined;
42
+ return refuse('UNKNOWN_TABLE', '$.tableId', `this store holds table \`${document.tableId}\`; the command names \`${tableId}\`, and a command never rewrites a table it was not sent to`);
43
+ }
44
+ function notKeyed(document, command) {
45
+ return refuse('INVALID_TABLE', '$.kind', `table \`${document.tableId}\` is a \`set\`; \`${command}\` writes a \`lookup\` or \`decision_table\`, and a set's domain is written with \`setDatasetValues\``);
46
+ }
47
+ function notSet(document, command) {
48
+ return refuse('INVALID_TABLE', '$.kind', `table \`${document.tableId}\` is a \`${document.kind}\`; \`${command}\` writes a \`set\`, whose membership domain has no columns or rows`);
49
+ }
50
+ /** The row's composite key as the canonical JSON text a refusal and a grid address it by. */
51
+ function keyText(key) {
52
+ return canonicalizeRestricted(key);
53
+ }
54
+ function keyColumnsOf(document) {
55
+ return document.columns.filter((column) => column.role === 'key');
56
+ }
57
+ function outputColumnsOf(document) {
58
+ return document.columns.filter((column) => column.role === 'output');
59
+ }
60
+ /**
61
+ * One typed cell against the column it is written into.
62
+ *
63
+ * The command states the primitive it means, so a value entered in a text column is refused rather
64
+ * than coerced; the runtime's wire reader then decides whether the value is the canonical spelling
65
+ * of that primitive, which is the same reader the dataset compiler uses. One authority answers both
66
+ * doors, so a cell the store accepted is a cell the compiler accepts.
67
+ */
68
+ function readCell(input, column, path, subject) {
69
+ if (input.kind !== column.primitive) {
70
+ return {
71
+ ok: false,
72
+ refusal: datasetRefusal('INVALID_CELL', path, `column \`${column.columnId}\` holds \`${column.primitive}\` values and the cell is stated as \`${input.kind}\``, subject),
73
+ };
74
+ }
75
+ try {
76
+ // The column's own declaration — including a quantity's unit domain and scale — is the
77
+ // reader's authority, so the store refuses a cell the dataset compiler would refuse and the
78
+ // two doors cannot disagree about what a column holds.
79
+ const value = toWireValue(input.value, path, wireDeclarationOfColumn(column));
80
+ if (!isDatasetScalar(value)) {
81
+ return {
82
+ ok: false,
83
+ refusal: datasetRefusal('INVALID_CELL', path, `column \`${column.columnId}\` holds a \`${column.primitive}\` scalar; the cell is empty`, subject),
84
+ };
85
+ }
86
+ return { ok: true, value };
87
+ }
88
+ catch (cause) {
89
+ if (!(cause instanceof AxiomError))
90
+ throw cause;
91
+ return { ok: false, refusal: datasetRefusal('INVALID_CELL', path, cause.message, subject) };
92
+ }
93
+ }
94
+ /** The row an edit starts from, as the draft the cell helpers rewrite. */
95
+ function draftOfRow(row) {
96
+ return {
97
+ rowId: row.rowId,
98
+ key: row.key,
99
+ outcome: 'outcome' in row ? row.outcome : undefined,
100
+ output: row.output,
101
+ };
102
+ }
103
+ /** One key vector read against the key columns, in declaration order (`DATA-1.2.3`). */
104
+ function readKey(document, spec, path) {
105
+ const columns = keyColumnsOf(document);
106
+ if (spec.key.length !== columns.length) {
107
+ return {
108
+ ok: false,
109
+ refusal: datasetRefusal('INVALID_ROW', `${path}.key`, `row \`${spec.rowId}\` states ${String(spec.key.length)} key values for ${String(columns.length)} key columns; state one value per key column, in declaration order`, { rowId: spec.rowId }),
110
+ };
111
+ }
112
+ const key = [];
113
+ for (const [index, input] of spec.key.entries()) {
114
+ const column = columns[index];
115
+ if (column === undefined) {
116
+ return {
117
+ ok: false,
118
+ refusal: datasetRefusal('INVALID_ROW', `${path}.key[${String(index)}]`, `the table declares no key column at position ${String(index)}`, { rowId: spec.rowId }),
119
+ };
120
+ }
121
+ const read = readCell(input, column, `${path}.key[${String(index)}]`, {
122
+ rowId: spec.rowId,
123
+ column: column.columnId,
124
+ });
125
+ if (!read.ok)
126
+ return read;
127
+ key.push(read.value);
128
+ }
129
+ return { ok: true, row: key };
130
+ }
131
+ /** One output record read against the output columns, in declaration order (`DATA-1.2.4`). */
132
+ function readOutput(document, spec, path, rowKey) {
133
+ const columns = outputColumnsOf(document);
134
+ if (spec.output === undefined)
135
+ return { ok: true, row: undefined };
136
+ for (const name of Object.keys(spec.output)) {
137
+ if (!columns.some((column) => column.columnId === name)) {
138
+ return {
139
+ ok: false,
140
+ refusal: datasetRefusal('UNKNOWN_COLUMN', `${path}.output.${name}`, `row \`${spec.rowId}\` states output member \`${name}\`, which is not an output column of table \`${document.tableId}\``, { rowId: spec.rowId, rowKey: keyText(rowKey), column: name }),
141
+ };
142
+ }
143
+ }
144
+ const members = [];
145
+ for (const column of columns) {
146
+ const input = spec.output[column.columnId];
147
+ if (input === undefined) {
148
+ return {
149
+ ok: false,
150
+ refusal: datasetRefusal('INVALID_ROW', `${path}.output`, `row \`${spec.rowId}\` states no value for output column \`${column.columnId}\`; a row states one value per output column, and the member set is closed`, { rowId: spec.rowId, rowKey: keyText(rowKey), column: column.columnId }),
151
+ };
152
+ }
153
+ const read = readCell(input, column, `${path}.output.${column.columnId}`, {
154
+ rowId: spec.rowId,
155
+ rowKey: keyText(rowKey),
156
+ column: column.columnId,
157
+ });
158
+ if (!read.ok)
159
+ return read;
160
+ members.push([column.columnId, read.value]);
161
+ }
162
+ // Define own properties rather than assigning them, so a `__proto__` column id stays data.
163
+ return { ok: true, row: Object.fromEntries(members) };
164
+ }
165
+ /** The draft a row spec states, with its cells read against the table's declared columns. */
166
+ function draftFromSpec(document, spec, path) {
167
+ const key = readKey(document, spec, path);
168
+ if (!key.ok)
169
+ return key;
170
+ const output = readOutput(document, spec, path, key.row);
171
+ if (!output.ok)
172
+ return output;
173
+ return {
174
+ ok: true,
175
+ row: { rowId: spec.rowId, key: key.row, outcome: spec.outcome, output: output.row },
176
+ };
177
+ }
178
+ function checkedRow(document, draft, path) {
179
+ if (document.kind === 'lookup') {
180
+ if (draft.outcome !== undefined) {
181
+ return {
182
+ ok: false,
183
+ refusal: datasetRefusal('INVALID_ROW', path, `row \`${draft.rowId}\` states outcome \`${draft.outcome}\`; a lookup row declares no outcome, because a keyed lookup either resolves or does not`, { rowId: draft.rowId, rowKey: keyText(draft.key) }),
184
+ };
185
+ }
186
+ if (draft.output === undefined) {
187
+ return {
188
+ ok: false,
189
+ refusal: datasetRefusal('INVALID_ROW', `${path}.output`, `row \`${draft.rowId}\` states no output; every lookup row carries one, because a hit returns it`, { rowId: draft.rowId, rowKey: keyText(draft.key) }),
190
+ };
191
+ }
192
+ return { ok: true, row: { rowId: draft.rowId, key: draft.key, output: draft.output } };
193
+ }
194
+ if (draft.outcome === undefined) {
195
+ return {
196
+ ok: false,
197
+ refusal: datasetRefusal('INVALID_ROW', path, `row \`${draft.rowId}\` states no outcome; a decision-table row declares its verdict`, { rowId: draft.rowId, rowKey: keyText(draft.key) }),
198
+ };
199
+ }
200
+ if (draft.outcome === 'PASS') {
201
+ if (draft.output === undefined) {
202
+ return {
203
+ ok: false,
204
+ refusal: datasetRefusal('INVALID_ROW', `${path}.output`, `row \`${draft.rowId}\` decides PASS and states no output; a PASS row produces the result a hit returns`, { rowId: draft.rowId, rowKey: keyText(draft.key) }),
205
+ };
206
+ }
207
+ return {
208
+ ok: true,
209
+ row: { rowId: draft.rowId, key: draft.key, outcome: 'PASS', output: draft.output },
210
+ };
211
+ }
212
+ return {
213
+ ok: true,
214
+ row: {
215
+ rowId: draft.rowId,
216
+ key: draft.key,
217
+ outcome: draft.outcome,
218
+ ...(draft.output === undefined ? {} : { output: draft.output }),
219
+ },
220
+ };
221
+ }
222
+ /**
223
+ * The row another row already states this key with, under `UNIQUE`.
224
+ *
225
+ * A repeated key is legal under `FIRST` and `COLLECT`, which promise the first match and the
226
+ * collected list respectively; `UNIQUE` promises at most one row, so the store refuses the edit
227
+ * rather than picking the row a store happens to order first (`DATA-3.1.1`).
228
+ */
229
+ function duplicateKey(document, key, exceptRowId) {
230
+ if (document.hitPolicy !== 'UNIQUE')
231
+ return undefined;
232
+ const text = keyText(key);
233
+ return rowsOf(document).find((row) => row.rowId !== exceptRowId && keyText(row.key) === text);
234
+ }
235
+ function duplicateRefusal(document, row, other, path) {
236
+ return refuse('DUPLICATE_KEY', `${path}.key`, `row \`${row.rowId}\` states key ${keyText(row.key)}, which row \`${other.rowId}\` already states; table \`${document.tableId}\` resolves \`UNIQUE\`, so at most one matching row is legal`, { rowId: row.rowId, rowKey: keyText(row.key) });
237
+ }
238
+ /* ------------------------------------------------------------------------------------ *
239
+ * The commands.
240
+ * ------------------------------------------------------------------------------------ */
241
+ function addRow(document, command) {
242
+ if (document.kind === 'set')
243
+ return notKeyed(document, command.kind);
244
+ const path = `$.rows[${String(document.rows.length)}]`;
245
+ if (rowsOf(document).some((row) => row.rowId === command.row.rowId)) {
246
+ return refuse('DUPLICATE_ID', `${path}.rowId`, `row \`${command.row.rowId}\` is already declared; a row id identifies one row of its table, because provenance names a hit by it`, { rowId: command.row.rowId });
247
+ }
248
+ const draft = draftFromSpec(document, command.row, path);
249
+ if (!draft.ok)
250
+ return draft;
251
+ if (document.kind === 'lookup') {
252
+ const checked = checkedRow(document, draft.row, path);
253
+ if (!checked.ok)
254
+ return checked;
255
+ const other = duplicateKey(document, checked.row.key, checked.row.rowId);
256
+ if (other !== undefined)
257
+ return duplicateRefusal(document, checked.row, other, path);
258
+ return applied({ ...document, rows: [...document.rows, checked.row] });
259
+ }
260
+ const checked = checkedRow(document, draft.row, path);
261
+ if (!checked.ok)
262
+ return checked;
263
+ const other = duplicateKey(document, checked.row.key, checked.row.rowId);
264
+ if (other !== undefined)
265
+ return duplicateRefusal(document, checked.row, other, path);
266
+ return applied({ ...document, rows: [...document.rows, checked.row] });
267
+ }
268
+ function replaceRow(document, command) {
269
+ if (document.kind === 'set')
270
+ return notKeyed(document, command.kind);
271
+ if (command.row.rowId !== command.rowId) {
272
+ return refuse('MALFORMED_COMMAND', '#.row.rowId', `the command names row \`${command.rowId}\` and the replacement states \`${command.row.rowId}\`; a replacement changes one row and never its identity`, { rowId: command.rowId });
273
+ }
274
+ const index = rowsOf(document).findIndex((row) => row.rowId === command.rowId);
275
+ const prior = rowsOf(document)[index];
276
+ if (prior === undefined) {
277
+ return refuse('UNKNOWN_ROW', '$.rows', `table \`${document.tableId}\` holds no row \`${command.rowId}\`; name a row the table holds, or add it with \`addDatasetRow\``, { rowId: command.rowId });
278
+ }
279
+ const path = `$.rows[${String(index)}]`;
280
+ const draft = draftFromSpec(document, command.row, path);
281
+ if (!draft.ok)
282
+ return draft;
283
+ if (document.kind === 'lookup') {
284
+ const checked = checkedRow(document, draft.row, path);
285
+ if (!checked.ok)
286
+ return checked;
287
+ const other = duplicateKey(document, checked.row.key, checked.row.rowId);
288
+ if (other !== undefined)
289
+ return duplicateRefusal(document, checked.row, other, path);
290
+ return applied({
291
+ ...document,
292
+ rows: document.rows.map((row, at) => (at === index ? checked.row : row)),
293
+ });
294
+ }
295
+ const checked = checkedRow(document, draft.row, path);
296
+ if (!checked.ok)
297
+ return checked;
298
+ const other = duplicateKey(document, checked.row.key, checked.row.rowId);
299
+ if (other !== undefined)
300
+ return duplicateRefusal(document, checked.row, other, path);
301
+ return applied({
302
+ ...document,
303
+ rows: document.rows.map((row, at) => (at === index ? checked.row : row)),
304
+ });
305
+ }
306
+ function removeRow(document, command) {
307
+ if (document.kind === 'set')
308
+ return notKeyed(document, command.kind);
309
+ const index = rowsOf(document).findIndex((row) => row.rowId === command.rowId);
310
+ if (index < 0) {
311
+ return refuse('UNKNOWN_ROW', '$.rows', `table \`${document.tableId}\` holds no row \`${command.rowId}\``, { rowId: command.rowId });
312
+ }
313
+ if (document.kind === 'lookup') {
314
+ return applied({
315
+ ...document,
316
+ rows: document.rows.filter((_row, at) => at !== index),
317
+ });
318
+ }
319
+ return applied({
320
+ ...document,
321
+ rows: document.rows.filter((_row, at) => at !== index),
322
+ });
323
+ }
324
+ function setCell(document, command) {
325
+ if (document.kind === 'set')
326
+ return notKeyed(document, command.kind);
327
+ const index = rowsOf(document).findIndex((row) => row.rowId === command.rowId);
328
+ const prior = rowsOf(document)[index];
329
+ if (prior === undefined) {
330
+ return refuse('UNKNOWN_ROW', '$.rows', `table \`${document.tableId}\` holds no row \`${command.rowId}\``, { rowId: command.rowId });
331
+ }
332
+ const column = document.columns.find((declared) => declared.columnId === command.columnId);
333
+ if (column === undefined) {
334
+ return refuse('UNKNOWN_COLUMN', '$.columns', `table \`${document.tableId}\` declares no column \`${command.columnId}\`; declare it with \`setDatasetColumns\` first`, { rowId: command.rowId, column: command.columnId });
335
+ }
336
+ let draft = draftOfRow(prior);
337
+ const path = `$.rows[${String(index)}]`;
338
+ const read = readCell(command.input, column, `${path}.${column.columnId}`, {
339
+ rowId: command.rowId,
340
+ rowKey: keyText(draft.key),
341
+ column: column.columnId,
342
+ });
343
+ if (!read.ok)
344
+ return read;
345
+ if (column.role === 'key') {
346
+ const keyIndex = keyColumnsOf(document).findIndex((declared) => declared.columnId === column.columnId);
347
+ if (keyIndex < 0) {
348
+ return refuse('INVALID_COLUMNS', '$.columns', `column \`${column.columnId}\` states the \`key\` role and no key position; the table's columns are not the ones its rows are addressed by`, { rowId: command.rowId, column: column.columnId });
349
+ }
350
+ const key = [...draft.key];
351
+ key[keyIndex] = read.value;
352
+ draft = { ...draft, key };
353
+ }
354
+ else {
355
+ if (draft.output === undefined) {
356
+ return refuse('INVALID_ROW', `${path}.output`, `row \`${command.rowId}\` declares no output; give it one with \`replaceDatasetRow\` before writing a cell of it`, { rowId: command.rowId, rowKey: keyText(draft.key), column: column.columnId });
357
+ }
358
+ draft = { ...draft, output: { ...draft.output, [column.columnId]: read.value } };
359
+ }
360
+ if (document.kind === 'lookup') {
361
+ const checked = checkedRow(document, draft, path);
362
+ if (!checked.ok)
363
+ return checked;
364
+ const other = duplicateKey(document, checked.row.key, checked.row.rowId);
365
+ if (other !== undefined)
366
+ return duplicateRefusal(document, checked.row, other, path);
367
+ return applied({
368
+ ...document,
369
+ rows: document.rows.map((row, at) => (at === index ? checked.row : row)),
370
+ });
371
+ }
372
+ const checked = checkedRow(document, draft, path);
373
+ if (!checked.ok)
374
+ return checked;
375
+ const other = duplicateKey(document, checked.row.key, checked.row.rowId);
376
+ if (other !== undefined)
377
+ return duplicateRefusal(document, checked.row, other, path);
378
+ return applied({
379
+ ...document,
380
+ rows: document.rows.map((row, at) => (at === index ? checked.row : row)),
381
+ });
382
+ }
383
+ /** Whether two column declarations address rows the same way, member for member and in order. */
384
+ function sameColumns(left, right) {
385
+ return (left.length === right.length &&
386
+ left.every((column, index) => {
387
+ const other = right[index];
388
+ return (other !== undefined &&
389
+ column.columnId === other.columnId &&
390
+ column.primitive === other.primitive &&
391
+ column.role === other.role);
392
+ }));
393
+ }
394
+ function setColumns(document, command) {
395
+ if (document.kind === 'set')
396
+ return notKeyed(document, command.kind);
397
+ if (!command.columns.some((column) => column.role === 'key')) {
398
+ return refuse('INVALID_COLUMNS', '$.columns', `table \`${document.tableId}\` is a \`${document.kind}\` and declares no \`key\` column; a keyed table declares at least one, because its rows are addressed by a composite key`);
399
+ }
400
+ if (document.rows.length > 0 && !sameColumns(document.columns, command.columns)) {
401
+ return refuse('INVALID_COLUMNS', '$.columns', `table \`${document.tableId}\` holds ${String(document.rows.length)} rows addressed by its declared columns; remove them with \`removeDatasetRow\` before changing a column id, primitive, role or order`);
402
+ }
403
+ if (document.kind === 'lookup')
404
+ return applied({ ...document, columns: command.columns });
405
+ return applied({ ...document, columns: command.columns });
406
+ }
407
+ function setValues(document, command) {
408
+ if (document.kind !== 'set')
409
+ return notSet(document, command.kind);
410
+ return applied({ ...document, values: command.values });
411
+ }
412
+ function setPolicy(document, command) {
413
+ if (document.kind === 'set')
414
+ return notKeyed(document, command.kind);
415
+ const identity = identityOf(document);
416
+ const binding = bindingOf(document);
417
+ if (document.kind === 'lookup') {
418
+ return applied({
419
+ ...identity,
420
+ ...binding,
421
+ ...command.policy,
422
+ kind: 'lookup',
423
+ columns: document.columns,
424
+ rows: document.rows,
425
+ });
426
+ }
427
+ return applied({
428
+ ...identity,
429
+ ...binding,
430
+ ...command.policy,
431
+ kind: 'decision_table',
432
+ columns: document.columns,
433
+ rows: document.rows,
434
+ });
435
+ }
436
+ function setRange(document, command) {
437
+ if (document.binding !== 'data') {
438
+ return refuse('INVALID_TABLE', '$.binding', `table \`${document.tableId}\` binds to \`behavior\` and carries no effective range; a behavior table pins to the case, and only a \`data\` table versions by date`);
439
+ }
440
+ const from = civilDay('effectiveFrom', command.effectiveFrom);
441
+ const to = civilDay('effectiveTo', command.effectiveTo);
442
+ if (from >= to) {
443
+ return refuse('INVALID_TABLE', '$.effectiveTo', `table \`${document.tableId}\` would state \`[${command.effectiveFrom}, ${command.effectiveTo})\`, which is empty; the range is half-open and its end is after its start`);
444
+ }
445
+ const identity = identityOf(document);
446
+ const binding = {
447
+ binding: 'data',
448
+ effectiveFrom: command.effectiveFrom,
449
+ effectiveTo: command.effectiveTo,
450
+ };
451
+ if (document.kind === 'set') {
452
+ return applied({ ...identity, ...binding, kind: 'set', values: document.values });
453
+ }
454
+ if (document.kind === 'lookup') {
455
+ return applied({
456
+ ...identity,
457
+ ...binding,
458
+ ...policyOf(document),
459
+ kind: 'lookup',
460
+ columns: document.columns,
461
+ rows: document.rows,
462
+ });
463
+ }
464
+ return applied({
465
+ ...identity,
466
+ ...binding,
467
+ ...policyOf(document),
468
+ kind: 'decision_table',
469
+ columns: document.columns,
470
+ rows: document.rows,
471
+ });
472
+ }
473
+ /** One civil date as the day number the half-open comparison reads. */
474
+ function civilDay(member, text) {
475
+ const date = parsePlainDate(text, member);
476
+ return daysFromCivil(date.year, date.month, date.day);
477
+ }
478
+ /** Applies one validated dataset command to one table. */
479
+ export function applyDatasetCommand(document, command) {
480
+ const result = applyDatasetCommandToTable(document, command);
481
+ if (!result.ok || result.document === document)
482
+ return result;
483
+ // A command that restates the table builds a new tree with identical content. Return the table
484
+ // it was handed so the store's no-edit guard sees one snapshot instead of committing an undo
485
+ // step between two byte-identical tables; the walk stops at every shared subtree.
486
+ return unchanged(result.document, document) ? applied(document) : result;
487
+ }
488
+ function applyDatasetCommandToTable(document, command) {
489
+ const mismatch = wrongTable(document, command.tableId);
490
+ if (mismatch !== undefined)
491
+ return mismatch;
492
+ switch (command.kind) {
493
+ case 'addDatasetRow':
494
+ return addRow(document, command);
495
+ case 'replaceDatasetRow':
496
+ return replaceRow(document, command);
497
+ case 'removeDatasetRow':
498
+ return removeRow(document, command);
499
+ case 'setDatasetCell':
500
+ return setCell(document, command);
501
+ case 'setDatasetColumns':
502
+ return setColumns(document, command);
503
+ case 'setDatasetValues':
504
+ return setValues(document, command);
505
+ case 'setDatasetHitPolicy':
506
+ return setPolicy(document, command);
507
+ case 'setDatasetEffectiveRange':
508
+ return setRange(document, command);
509
+ default:
510
+ return assertNever(command);
511
+ }
512
+ }
513
+ //# sourceMappingURL=apply-dataset.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"apply-dataset.js","sourceRoot":"","sources":["../../src/store/apply-dataset.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,EACV,WAAW,EACX,sBAAsB,EACtB,aAAa,EACb,cAAc,EACd,WAAW,EACX,uBAAuB,GACxB,MAAM,wBAAwB,CAAC;AAUhC,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAcvD,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAE/C,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAsB3C,SAAS,MAAM,CACb,IAA8B,EAC9B,IAAY,EACZ,MAAc,EACd,OAAO,GAA0B,EAAE;IAEnC,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,OAAO,EAAE,cAAc,CAAC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OAAO,CAAC,CAAC;QACtC,KAAK,gBAAgB;YACnB,OAAO,OAAO,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;QACpC,KAAK,mBAAmB;YACtB,OAAO,UAAU,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;QACvC,KAAK,kBAAkB;YACrB,OAAO,SAAS,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;QACtC,KAAK,qBAAqB;YACxB,OAAO,SAAS,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;QACtC,KAAK,0BAA0B;YAC7B,OAAO,QAAQ,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;QACrC;YACE,OAAO,WAAW,CAAC,OAAO,CAAC,CAAC;IAChC,CAAC;AACH,CAAC","sourcesContent":["import {\n AxiomError,\n assertNever,\n canonicalizeRestricted,\n daysFromCivil,\n parsePlainDate,\n toWireValue,\n wireDeclarationOfColumn,\n} from '@azx-pbc/axiom-runtime';\nimport type { DatasetColumn, Outcome, ScalarWireValue, WireValue } from '@azx-pbc/axiom-runtime';\nimport type {\n AuthoredDatasetBinding,\n AuthoredDatasetDocument,\n AuthoredDecisionRow,\n AuthoredHitPolicy,\n AuthoredLookupRow,\n AuthoredSetTable,\n} from '../schema/dataset.js';\nimport { isDatasetScalar } from '../schema/dataset.js';\nimport type {\n AddDatasetRowCommand,\n DatasetCellInput,\n DatasetCommand,\n DatasetRowSpec,\n RemoveDatasetRowCommand,\n ReplaceDatasetRowCommand,\n SetDatasetCellCommand,\n SetDatasetColumnsCommand,\n SetDatasetEffectiveRangeCommand,\n SetDatasetHitPolicyCommand,\n SetDatasetValuesCommand,\n} from './commands.js';\nimport { datasetRefusal } from './refusals.js';\nimport type { AuthoringRefusal, DatasetRefusalSubject } from './refusals.js';\nimport { unchanged } from './unchanged.js';\n\n/**\n * Applying one dataset command to one table.\n *\n * Each command is a pure rewrite, so the undo chain shares every untouched subtree exactly as the\n * schema store's does. The checks here decide the command's own intent — a table id that is not the\n * one the store holds, a row or column the table does not declare, a cell that is not its column's\n * primitive, a duplicate key under `UNIQUE` — while the candidate gate beside this module decides\n * whether the *result* is a document the dataset compiler admits. Neither re-implements the other:\n * the compiler remains the authority on the closed contract, and these refusals name the row, the\n * column and the repair an author works with.\n */\n\ntype KeyedTable = Extract<AuthoredDatasetDocument, { kind: 'lookup' | 'decision_table' }>;\ntype AuthoredRow = AuthoredLookupRow | AuthoredDecisionRow;\n\n/** The document a command produced, or the refusal that names why it produced none. */\nexport type DatasetApplyResult =\n | { readonly ok: true; readonly document: AuthoredDatasetDocument }\n | { readonly ok: false; readonly refusal: AuthoringRefusal };\n\nfunction refuse(\n code: AuthoringRefusal['code'],\n path: string,\n detail: string,\n subject: DatasetRefusalSubject = {},\n): DatasetApplyResult {\n return { ok: false, refusal: datasetRefusal(code, path, detail, subject) };\n}\n\nfunction applied(document: AuthoredDatasetDocument): DatasetApplyResult {\n return { ok: true, document };\n}\n\n/** The identity every rewrite carries through, so no command invents a new one. */\nfunction identityOf(document: AuthoredDatasetDocument): {\n readonly tableId: string;\n readonly tableVersion: number;\n} {\n return { tableId: document.tableId, tableVersion: document.tableVersion };\n}\n\n/** The clock binding, restated as the union arm the document's own members state. */\nfunction bindingOf(document: AuthoredDatasetDocument): AuthoredDatasetBinding {\n return document.binding === 'data'\n ? { binding: 'data', effectiveFrom: document.effectiveFrom, effectiveTo: document.effectiveTo }\n : { binding: 'behavior' };\n}\n\n/** The hit policy, restated as the union arm the document's own members state. */\nfunction policyOf(document: KeyedTable): AuthoredHitPolicy {\n return document.hitPolicy === 'COLLECT'\n ? { hitPolicy: 'COLLECT', collectLimit: document.collectLimit }\n : { hitPolicy: document.hitPolicy };\n}\n\n/**\n * A keyed table's rows as the one row type a rewrite reads.\n *\n * A lookup row and a decision row are different values, and a union of two array types has no\n * callable `map`; reading them as the common `{ rowId, key }` shape the rewrites share keeps one\n * walk. The authored union is restored by `checkedRow`, which is the only place the per-kind\n * contract is decided.\n */\nfunction rowsOf(document: KeyedTable): readonly AuthoredRow[] {\n return document.rows;\n}\n\n/** The command names the table this store holds, or the refusal that says which one it holds. */\nfunction wrongTable(\n document: AuthoredDatasetDocument,\n tableId: string,\n): DatasetApplyResult | undefined {\n if (document.tableId === tableId) return undefined;\n return refuse(\n 'UNKNOWN_TABLE',\n '$.tableId',\n `this store holds table \\`${document.tableId}\\`; the command names \\`${tableId}\\`, and a command never rewrites a table it was not sent to`,\n );\n}\n\nfunction notKeyed(document: AuthoredSetTable, command: string): DatasetApplyResult {\n return refuse(\n 'INVALID_TABLE',\n '$.kind',\n `table \\`${document.tableId}\\` is a \\`set\\`; \\`${command}\\` writes a \\`lookup\\` or \\`decision_table\\`, and a set's domain is written with \\`setDatasetValues\\``,\n );\n}\n\nfunction notSet(document: KeyedTable, command: string): DatasetApplyResult {\n return refuse(\n 'INVALID_TABLE',\n '$.kind',\n `table \\`${document.tableId}\\` is a \\`${document.kind}\\`; \\`${command}\\` writes a \\`set\\`, whose membership domain has no columns or rows`,\n );\n}\n\n/** The row's composite key as the canonical JSON text a refusal and a grid address it by. */\nfunction keyText(key: readonly ScalarWireValue[]): string {\n return canonicalizeRestricted(key);\n}\n\nfunction keyColumnsOf(document: KeyedTable): readonly DatasetColumn[] {\n return document.columns.filter((column) => column.role === 'key');\n}\n\nfunction outputColumnsOf(document: KeyedTable): readonly DatasetColumn[] {\n return document.columns.filter((column) => column.role === 'output');\n}\n\n/* ------------------------------------------------------------------------------------ *\n * Cell reading.\n * ------------------------------------------------------------------------------------ */\n\n/** One cell's canonical value, or the refusal that names the row, the column and the type. */\ntype CellRead =\n | { readonly ok: true; readonly value: ScalarWireValue }\n | { readonly ok: false; readonly refusal: AuthoringRefusal };\n\n/**\n * One typed cell against the column it is written into.\n *\n * The command states the primitive it means, so a value entered in a text column is refused rather\n * than coerced; the runtime's wire reader then decides whether the value is the canonical spelling\n * of that primitive, which is the same reader the dataset compiler uses. One authority answers both\n * doors, so a cell the store accepted is a cell the compiler accepts.\n */\nfunction readCell(\n input: DatasetCellInput,\n column: DatasetColumn,\n path: string,\n subject: DatasetRefusalSubject,\n): CellRead {\n if (input.kind !== column.primitive) {\n return {\n ok: false,\n refusal: datasetRefusal(\n 'INVALID_CELL',\n path,\n `column \\`${column.columnId}\\` holds \\`${column.primitive}\\` values and the cell is stated as \\`${input.kind}\\``,\n subject,\n ),\n };\n }\n try {\n // The column's own declaration — including a quantity's unit domain and scale — is the\n // reader's authority, so the store refuses a cell the dataset compiler would refuse and the\n // two doors cannot disagree about what a column holds.\n const value: WireValue = toWireValue(input.value, path, wireDeclarationOfColumn(column));\n if (!isDatasetScalar(value)) {\n return {\n ok: false,\n refusal: datasetRefusal(\n 'INVALID_CELL',\n path,\n `column \\`${column.columnId}\\` holds a \\`${column.primitive}\\` scalar; the cell is empty`,\n subject,\n ),\n };\n }\n return { ok: true, value };\n } catch (cause) {\n if (!(cause instanceof AxiomError)) throw cause;\n return { ok: false, refusal: datasetRefusal('INVALID_CELL', path, cause.message, subject) };\n }\n}\n\n/* ------------------------------------------------------------------------------------ *\n * Row building.\n * ------------------------------------------------------------------------------------ */\n\n/**\n * One row as a rewrite holds it while its cells are read.\n *\n * The members are required (a member that is not stated is `undefined` rather than absent) because a\n * cell edit mutates one of them and the checked row states the rest; the authored union is built\n * once, at the end, by `checkedRow`.\n */\ninterface RowDraft {\n readonly rowId: string;\n readonly key: readonly ScalarWireValue[];\n readonly outcome: Outcome | undefined;\n readonly output: Readonly<Record<string, ScalarWireValue>> | undefined;\n}\n\ntype RowCheck<Row> =\n | { readonly ok: true; readonly row: Row }\n | { readonly ok: false; readonly refusal: AuthoringRefusal };\n\n/** The row an edit starts from, as the draft the cell helpers rewrite. */\nfunction draftOfRow(row: AuthoredRow): RowDraft {\n return {\n rowId: row.rowId,\n key: row.key,\n outcome: 'outcome' in row ? row.outcome : undefined,\n output: row.output,\n };\n}\n\n/** One key vector read against the key columns, in declaration order (`DATA-1.2.3`). */\nfunction readKey(\n document: KeyedTable,\n spec: DatasetRowSpec,\n path: string,\n): RowCheck<readonly ScalarWireValue[]> {\n const columns = keyColumnsOf(document);\n if (spec.key.length !== columns.length) {\n return {\n ok: false,\n refusal: datasetRefusal(\n 'INVALID_ROW',\n `${path}.key`,\n `row \\`${spec.rowId}\\` states ${String(spec.key.length)} key values for ${String(columns.length)} key columns; state one value per key column, in declaration order`,\n { rowId: spec.rowId },\n ),\n };\n }\n const key: ScalarWireValue[] = [];\n for (const [index, input] of spec.key.entries()) {\n const column = columns[index];\n if (column === undefined) {\n return {\n ok: false,\n refusal: datasetRefusal(\n 'INVALID_ROW',\n `${path}.key[${String(index)}]`,\n `the table declares no key column at position ${String(index)}`,\n { rowId: spec.rowId },\n ),\n };\n }\n const read = readCell(input, column, `${path}.key[${String(index)}]`, {\n rowId: spec.rowId,\n column: column.columnId,\n });\n if (!read.ok) return read;\n key.push(read.value);\n }\n return { ok: true, row: key };\n}\n\n/** One output record read against the output columns, in declaration order (`DATA-1.2.4`). */\nfunction readOutput(\n document: KeyedTable,\n spec: DatasetRowSpec,\n path: string,\n rowKey: readonly ScalarWireValue[],\n): RowCheck<Readonly<Record<string, ScalarWireValue>> | undefined> {\n const columns = outputColumnsOf(document);\n if (spec.output === undefined) return { ok: true, row: undefined };\n for (const name of Object.keys(spec.output)) {\n if (!columns.some((column) => column.columnId === name)) {\n return {\n ok: false,\n refusal: datasetRefusal(\n 'UNKNOWN_COLUMN',\n `${path}.output.${name}`,\n `row \\`${spec.rowId}\\` states output member \\`${name}\\`, which is not an output column of table \\`${document.tableId}\\``,\n { rowId: spec.rowId, rowKey: keyText(rowKey), column: name },\n ),\n };\n }\n }\n const members: [string, ScalarWireValue][] = [];\n for (const column of columns) {\n const input = spec.output[column.columnId];\n if (input === undefined) {\n return {\n ok: false,\n refusal: datasetRefusal(\n 'INVALID_ROW',\n `${path}.output`,\n `row \\`${spec.rowId}\\` states no value for output column \\`${column.columnId}\\`; a row states one value per output column, and the member set is closed`,\n { rowId: spec.rowId, rowKey: keyText(rowKey), column: column.columnId },\n ),\n };\n }\n const read = readCell(input, column, `${path}.output.${column.columnId}`, {\n rowId: spec.rowId,\n rowKey: keyText(rowKey),\n column: column.columnId,\n });\n if (!read.ok) return read;\n members.push([column.columnId, read.value]);\n }\n // Define own properties rather than assigning them, so a `__proto__` column id stays data.\n return { ok: true, row: Object.fromEntries(members) };\n}\n\n/** The draft a row spec states, with its cells read against the table's declared columns. */\nfunction draftFromSpec(\n document: KeyedTable,\n spec: DatasetRowSpec,\n path: string,\n): RowCheck<RowDraft> {\n const key = readKey(document, spec, path);\n if (!key.ok) return key;\n const output = readOutput(document, spec, path, key.row);\n if (!output.ok) return output;\n return {\n ok: true,\n row: { rowId: spec.rowId, key: key.row, outcome: spec.outcome, output: output.row },\n };\n}\n\n/** One lookup row: an output on every row and no verdict at all (`DATA-1.2.4`). */\nfunction checkedRow(\n document: Extract<KeyedTable, { kind: 'lookup' }>,\n draft: RowDraft,\n path: string,\n): RowCheck<AuthoredLookupRow>;\n/** One decision row: a verdict on every row, and an output on a `PASS` row. */\nfunction checkedRow(\n document: Extract<KeyedTable, { kind: 'decision_table' }>,\n draft: RowDraft,\n path: string,\n): RowCheck<AuthoredDecisionRow>;\nfunction checkedRow(document: KeyedTable, draft: RowDraft, path: string): RowCheck<AuthoredRow> {\n if (document.kind === 'lookup') {\n if (draft.outcome !== undefined) {\n return {\n ok: false,\n refusal: datasetRefusal(\n 'INVALID_ROW',\n path,\n `row \\`${draft.rowId}\\` states outcome \\`${draft.outcome}\\`; a lookup row declares no outcome, because a keyed lookup either resolves or does not`,\n { rowId: draft.rowId, rowKey: keyText(draft.key) },\n ),\n };\n }\n if (draft.output === undefined) {\n return {\n ok: false,\n refusal: datasetRefusal(\n 'INVALID_ROW',\n `${path}.output`,\n `row \\`${draft.rowId}\\` states no output; every lookup row carries one, because a hit returns it`,\n { rowId: draft.rowId, rowKey: keyText(draft.key) },\n ),\n };\n }\n return { ok: true, row: { rowId: draft.rowId, key: draft.key, output: draft.output } };\n }\n if (draft.outcome === undefined) {\n return {\n ok: false,\n refusal: datasetRefusal(\n 'INVALID_ROW',\n path,\n `row \\`${draft.rowId}\\` states no outcome; a decision-table row declares its verdict`,\n { rowId: draft.rowId, rowKey: keyText(draft.key) },\n ),\n };\n }\n if (draft.outcome === 'PASS') {\n if (draft.output === undefined) {\n return {\n ok: false,\n refusal: datasetRefusal(\n 'INVALID_ROW',\n `${path}.output`,\n `row \\`${draft.rowId}\\` decides PASS and states no output; a PASS row produces the result a hit returns`,\n { rowId: draft.rowId, rowKey: keyText(draft.key) },\n ),\n };\n }\n return {\n ok: true,\n row: { rowId: draft.rowId, key: draft.key, outcome: 'PASS', output: draft.output },\n };\n }\n return {\n ok: true,\n row: {\n rowId: draft.rowId,\n key: draft.key,\n outcome: draft.outcome,\n ...(draft.output === undefined ? {} : { output: draft.output }),\n },\n };\n}\n\n/**\n * The row another row already states this key with, under `UNIQUE`.\n *\n * A repeated key is legal under `FIRST` and `COLLECT`, which promise the first match and the\n * collected list respectively; `UNIQUE` promises at most one row, so the store refuses the edit\n * rather than picking the row a store happens to order first (`DATA-3.1.1`).\n */\nfunction duplicateKey(\n document: KeyedTable,\n key: readonly ScalarWireValue[],\n exceptRowId: string,\n): AuthoredRow | undefined {\n if (document.hitPolicy !== 'UNIQUE') return undefined;\n const text = keyText(key);\n return rowsOf(document).find((row) => row.rowId !== exceptRowId && keyText(row.key) === text);\n}\n\nfunction duplicateRefusal(\n document: KeyedTable,\n row: { readonly rowId: string; readonly key: readonly ScalarWireValue[] },\n other: AuthoredRow,\n path: string,\n): DatasetApplyResult {\n return refuse(\n 'DUPLICATE_KEY',\n `${path}.key`,\n `row \\`${row.rowId}\\` states key ${keyText(row.key)}, which row \\`${other.rowId}\\` already states; table \\`${document.tableId}\\` resolves \\`UNIQUE\\`, so at most one matching row is legal`,\n { rowId: row.rowId, rowKey: keyText(row.key) },\n );\n}\n\n/* ------------------------------------------------------------------------------------ *\n * The commands.\n * ------------------------------------------------------------------------------------ */\n\nfunction addRow(\n document: AuthoredDatasetDocument,\n command: AddDatasetRowCommand,\n): DatasetApplyResult {\n if (document.kind === 'set') return notKeyed(document, command.kind);\n const path = `$.rows[${String(document.rows.length)}]`;\n if (rowsOf(document).some((row) => row.rowId === command.row.rowId)) {\n return refuse(\n 'DUPLICATE_ID',\n `${path}.rowId`,\n `row \\`${command.row.rowId}\\` is already declared; a row id identifies one row of its table, because provenance names a hit by it`,\n { rowId: command.row.rowId },\n );\n }\n const draft = draftFromSpec(document, command.row, path);\n if (!draft.ok) return draft;\n if (document.kind === 'lookup') {\n const checked = checkedRow(document, draft.row, path);\n if (!checked.ok) return checked;\n const other = duplicateKey(document, checked.row.key, checked.row.rowId);\n if (other !== undefined) return duplicateRefusal(document, checked.row, other, path);\n return applied({ ...document, rows: [...document.rows, checked.row] });\n }\n const checked = checkedRow(document, draft.row, path);\n if (!checked.ok) return checked;\n const other = duplicateKey(document, checked.row.key, checked.row.rowId);\n if (other !== undefined) return duplicateRefusal(document, checked.row, other, path);\n return applied({ ...document, rows: [...document.rows, checked.row] });\n}\n\nfunction replaceRow(\n document: AuthoredDatasetDocument,\n command: ReplaceDatasetRowCommand,\n): DatasetApplyResult {\n if (document.kind === 'set') return notKeyed(document, command.kind);\n if (command.row.rowId !== command.rowId) {\n return refuse(\n 'MALFORMED_COMMAND',\n '#.row.rowId',\n `the command names row \\`${command.rowId}\\` and the replacement states \\`${command.row.rowId}\\`; a replacement changes one row and never its identity`,\n { rowId: command.rowId },\n );\n }\n const index = rowsOf(document).findIndex((row) => row.rowId === command.rowId);\n const prior = rowsOf(document)[index];\n if (prior === undefined) {\n return refuse(\n 'UNKNOWN_ROW',\n '$.rows',\n `table \\`${document.tableId}\\` holds no row \\`${command.rowId}\\`; name a row the table holds, or add it with \\`addDatasetRow\\``,\n { rowId: command.rowId },\n );\n }\n const path = `$.rows[${String(index)}]`;\n const draft = draftFromSpec(document, command.row, path);\n if (!draft.ok) return draft;\n if (document.kind === 'lookup') {\n const checked = checkedRow(document, draft.row, path);\n if (!checked.ok) return checked;\n const other = duplicateKey(document, checked.row.key, checked.row.rowId);\n if (other !== undefined) return duplicateRefusal(document, checked.row, other, path);\n return applied({\n ...document,\n rows: document.rows.map((row, at) => (at === index ? checked.row : row)),\n });\n }\n const checked = checkedRow(document, draft.row, path);\n if (!checked.ok) return checked;\n const other = duplicateKey(document, checked.row.key, checked.row.rowId);\n if (other !== undefined) return duplicateRefusal(document, checked.row, other, path);\n return applied({\n ...document,\n rows: document.rows.map((row, at) => (at === index ? checked.row : row)),\n });\n}\n\nfunction removeRow(\n document: AuthoredDatasetDocument,\n command: RemoveDatasetRowCommand,\n): DatasetApplyResult {\n if (document.kind === 'set') return notKeyed(document, command.kind);\n const index = rowsOf(document).findIndex((row) => row.rowId === command.rowId);\n if (index < 0) {\n return refuse(\n 'UNKNOWN_ROW',\n '$.rows',\n `table \\`${document.tableId}\\` holds no row \\`${command.rowId}\\``,\n { rowId: command.rowId },\n );\n }\n if (document.kind === 'lookup') {\n return applied({\n ...document,\n rows: document.rows.filter((_row, at) => at !== index),\n });\n }\n return applied({\n ...document,\n rows: document.rows.filter((_row, at) => at !== index),\n });\n}\n\nfunction setCell(\n document: AuthoredDatasetDocument,\n command: SetDatasetCellCommand,\n): DatasetApplyResult {\n if (document.kind === 'set') return notKeyed(document, command.kind);\n const index = rowsOf(document).findIndex((row) => row.rowId === command.rowId);\n const prior = rowsOf(document)[index];\n if (prior === undefined) {\n return refuse(\n 'UNKNOWN_ROW',\n '$.rows',\n `table \\`${document.tableId}\\` holds no row \\`${command.rowId}\\``,\n { rowId: command.rowId },\n );\n }\n const column = document.columns.find((declared) => declared.columnId === command.columnId);\n if (column === undefined) {\n return refuse(\n 'UNKNOWN_COLUMN',\n '$.columns',\n `table \\`${document.tableId}\\` declares no column \\`${command.columnId}\\`; declare it with \\`setDatasetColumns\\` first`,\n { rowId: command.rowId, column: command.columnId },\n );\n }\n let draft = draftOfRow(prior);\n const path = `$.rows[${String(index)}]`;\n const read = readCell(command.input, column, `${path}.${column.columnId}`, {\n rowId: command.rowId,\n rowKey: keyText(draft.key),\n column: column.columnId,\n });\n if (!read.ok) return read;\n if (column.role === 'key') {\n const keyIndex = keyColumnsOf(document).findIndex(\n (declared) => declared.columnId === column.columnId,\n );\n if (keyIndex < 0) {\n return refuse(\n 'INVALID_COLUMNS',\n '$.columns',\n `column \\`${column.columnId}\\` states the \\`key\\` role and no key position; the table's columns are not the ones its rows are addressed by`,\n { rowId: command.rowId, column: column.columnId },\n );\n }\n const key = [...draft.key];\n key[keyIndex] = read.value;\n draft = { ...draft, key };\n } else {\n if (draft.output === undefined) {\n return refuse(\n 'INVALID_ROW',\n `${path}.output`,\n `row \\`${command.rowId}\\` declares no output; give it one with \\`replaceDatasetRow\\` before writing a cell of it`,\n { rowId: command.rowId, rowKey: keyText(draft.key), column: column.columnId },\n );\n }\n draft = { ...draft, output: { ...draft.output, [column.columnId]: read.value } };\n }\n if (document.kind === 'lookup') {\n const checked = checkedRow(document, draft, path);\n if (!checked.ok) return checked;\n const other = duplicateKey(document, checked.row.key, checked.row.rowId);\n if (other !== undefined) return duplicateRefusal(document, checked.row, other, path);\n return applied({\n ...document,\n rows: document.rows.map((row, at) => (at === index ? checked.row : row)),\n });\n }\n const checked = checkedRow(document, draft, path);\n if (!checked.ok) return checked;\n const other = duplicateKey(document, checked.row.key, checked.row.rowId);\n if (other !== undefined) return duplicateRefusal(document, checked.row, other, path);\n return applied({\n ...document,\n rows: document.rows.map((row, at) => (at === index ? checked.row : row)),\n });\n}\n\n/** Whether two column declarations address rows the same way, member for member and in order. */\nfunction sameColumns(left: readonly DatasetColumn[], right: readonly DatasetColumn[]): boolean {\n return (\n left.length === right.length &&\n left.every((column, index) => {\n const other = right[index];\n return (\n other !== undefined &&\n column.columnId === other.columnId &&\n column.primitive === other.primitive &&\n column.role === other.role\n );\n })\n );\n}\n\nfunction setColumns(\n document: AuthoredDatasetDocument,\n command: SetDatasetColumnsCommand,\n): DatasetApplyResult {\n if (document.kind === 'set') return notKeyed(document, command.kind);\n if (!command.columns.some((column) => column.role === 'key')) {\n return refuse(\n 'INVALID_COLUMNS',\n '$.columns',\n `table \\`${document.tableId}\\` is a \\`${document.kind}\\` and declares no \\`key\\` column; a keyed table declares at least one, because its rows are addressed by a composite key`,\n );\n }\n if (document.rows.length > 0 && !sameColumns(document.columns, command.columns)) {\n return refuse(\n 'INVALID_COLUMNS',\n '$.columns',\n `table \\`${document.tableId}\\` holds ${String(document.rows.length)} rows addressed by its declared columns; remove them with \\`removeDatasetRow\\` before changing a column id, primitive, role or order`,\n );\n }\n if (document.kind === 'lookup') return applied({ ...document, columns: command.columns });\n return applied({ ...document, columns: command.columns });\n}\n\nfunction setValues(\n document: AuthoredDatasetDocument,\n command: SetDatasetValuesCommand,\n): DatasetApplyResult {\n if (document.kind !== 'set') return notSet(document, command.kind);\n return applied({ ...document, values: command.values });\n}\n\nfunction setPolicy(\n document: AuthoredDatasetDocument,\n command: SetDatasetHitPolicyCommand,\n): DatasetApplyResult {\n if (document.kind === 'set') return notKeyed(document, command.kind);\n const identity = identityOf(document);\n const binding = bindingOf(document);\n if (document.kind === 'lookup') {\n return applied({\n ...identity,\n ...binding,\n ...command.policy,\n kind: 'lookup',\n columns: document.columns,\n rows: document.rows,\n });\n }\n return applied({\n ...identity,\n ...binding,\n ...command.policy,\n kind: 'decision_table',\n columns: document.columns,\n rows: document.rows,\n });\n}\n\nfunction setRange(\n document: AuthoredDatasetDocument,\n command: SetDatasetEffectiveRangeCommand,\n): DatasetApplyResult {\n if (document.binding !== 'data') {\n return refuse(\n 'INVALID_TABLE',\n '$.binding',\n `table \\`${document.tableId}\\` binds to \\`behavior\\` and carries no effective range; a behavior table pins to the case, and only a \\`data\\` table versions by date`,\n );\n }\n const from = civilDay('effectiveFrom', command.effectiveFrom);\n const to = civilDay('effectiveTo', command.effectiveTo);\n if (from >= to) {\n return refuse(\n 'INVALID_TABLE',\n '$.effectiveTo',\n `table \\`${document.tableId}\\` would state \\`[${command.effectiveFrom}, ${command.effectiveTo})\\`, which is empty; the range is half-open and its end is after its start`,\n );\n }\n const identity = identityOf(document);\n const binding: AuthoredDatasetBinding = {\n binding: 'data',\n effectiveFrom: command.effectiveFrom,\n effectiveTo: command.effectiveTo,\n };\n if (document.kind === 'set') {\n return applied({ ...identity, ...binding, kind: 'set', values: document.values });\n }\n if (document.kind === 'lookup') {\n return applied({\n ...identity,\n ...binding,\n ...policyOf(document),\n kind: 'lookup',\n columns: document.columns,\n rows: document.rows,\n });\n }\n return applied({\n ...identity,\n ...binding,\n ...policyOf(document),\n kind: 'decision_table',\n columns: document.columns,\n rows: document.rows,\n });\n}\n\n/** One civil date as the day number the half-open comparison reads. */\nfunction civilDay(member: string, text: string): number {\n const date = parsePlainDate(text, member);\n return daysFromCivil(date.year, date.month, date.day);\n}\n\n/** Applies one validated dataset command to one table. */\nexport function applyDatasetCommand(\n document: AuthoredDatasetDocument,\n command: DatasetCommand,\n): DatasetApplyResult {\n const result = applyDatasetCommandToTable(document, command);\n if (!result.ok || result.document === document) return result;\n // A command that restates the table builds a new tree with identical content. Return the table\n // it was handed so the store's no-edit guard sees one snapshot instead of committing an undo\n // step between two byte-identical tables; the walk stops at every shared subtree.\n return unchanged(result.document, document) ? applied(document) : result;\n}\n\nfunction applyDatasetCommandToTable(\n document: AuthoredDatasetDocument,\n command: DatasetCommand,\n): DatasetApplyResult {\n const mismatch = wrongTable(document, command.tableId);\n if (mismatch !== undefined) return mismatch;\n switch (command.kind) {\n case 'addDatasetRow':\n return addRow(document, command);\n case 'replaceDatasetRow':\n return replaceRow(document, command);\n case 'removeDatasetRow':\n return removeRow(document, command);\n case 'setDatasetCell':\n return setCell(document, command);\n case 'setDatasetColumns':\n return setColumns(document, command);\n case 'setDatasetValues':\n return setValues(document, command);\n case 'setDatasetHitPolicy':\n return setPolicy(document, command);\n case 'setDatasetEffectiveRange':\n return setRange(document, command);\n default:\n return assertNever(command);\n }\n}\n"]}