@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.
- package/LICENSE +21 -0
- package/README.md +648 -2
- package/dist/cards/selectors.d.ts +70 -0
- package/dist/cards/selectors.d.ts.map +1 -0
- package/dist/cards/selectors.js +194 -0
- package/dist/cards/selectors.js.map +1 -0
- package/dist/definition.d.ts +36 -0
- package/dist/definition.d.ts.map +1 -0
- package/dist/definition.js +48 -0
- package/dist/definition.js.map +1 -0
- package/dist/evidence/cases.d.ts +44 -0
- package/dist/evidence/cases.d.ts.map +1 -0
- package/dist/evidence/cases.js +578 -0
- package/dist/evidence/cases.js.map +1 -0
- package/dist/evidence/repairs.d.ts +99 -0
- package/dist/evidence/repairs.d.ts.map +1 -0
- package/dist/evidence/repairs.js +181 -0
- package/dist/evidence/repairs.js.map +1 -0
- package/dist/evidence/summary.d.ts +35 -0
- package/dist/evidence/summary.d.ts.map +1 -0
- package/dist/evidence/summary.js +38 -0
- package/dist/evidence/summary.js.map +1 -0
- package/dist/i18n/gaps.d.ts +3 -0
- package/dist/i18n/gaps.d.ts.map +1 -0
- package/dist/i18n/gaps.js +2 -0
- package/dist/i18n/gaps.js.map +1 -0
- package/dist/i18n/message-slots.d.ts +19 -0
- package/dist/i18n/message-slots.d.ts.map +1 -0
- package/dist/i18n/message-slots.js +101 -0
- package/dist/i18n/message-slots.js.map +1 -0
- package/dist/i18n/text-locale.d.ts +13 -0
- package/dist/i18n/text-locale.d.ts.map +1 -0
- package/dist/i18n/text-locale.js +14 -0
- package/dist/i18n/text-locale.js.map +1 -0
- package/dist/i18n/translations.d.ts +56 -0
- package/dist/i18n/translations.d.ts.map +1 -0
- package/dist/i18n/translations.js +103 -0
- package/dist/i18n/translations.js.map +1 -0
- package/dist/impact/references.d.ts +143 -0
- package/dist/impact/references.d.ts.map +1 -0
- package/dist/impact/references.js +429 -0
- package/dist/impact/references.js.map +1 -0
- package/dist/index.d.ts +86 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +58 -0
- package/dist/index.js.map +1 -0
- package/dist/preview/compile-preview.d.ts +67 -0
- package/dist/preview/compile-preview.d.ts.map +1 -0
- package/dist/preview/compile-preview.js +286 -0
- package/dist/preview/compile-preview.js.map +1 -0
- package/dist/preview/handle.d.ts +119 -0
- package/dist/preview/handle.d.ts.map +1 -0
- package/dist/preview/handle.js +2 -0
- package/dist/preview/handle.js.map +1 -0
- package/dist/preview/held.d.ts +39 -0
- package/dist/preview/held.d.ts.map +1 -0
- package/dist/preview/held.js +15 -0
- package/dist/preview/held.js.map +1 -0
- package/dist/preview/measurement.d.ts +5 -0
- package/dist/preview/measurement.d.ts.map +1 -0
- package/dist/preview/measurement.js +18 -0
- package/dist/preview/measurement.js.map +1 -0
- package/dist/publish/build.d.ts +33 -0
- package/dist/publish/build.d.ts.map +1 -0
- package/dist/publish/build.js +15 -0
- package/dist/publish/build.js.map +1 -0
- package/dist/publish/diff.d.ts +208 -0
- package/dist/publish/diff.d.ts.map +1 -0
- package/dist/publish/diff.js +227 -0
- package/dist/publish/diff.js.map +1 -0
- package/dist/publish/review.d.ts +41 -0
- package/dist/publish/review.d.ts.map +1 -0
- package/dist/publish/review.js +22 -0
- package/dist/publish/review.js.map +1 -0
- package/dist/schema/dataset-input.d.ts +30 -0
- package/dist/schema/dataset-input.d.ts.map +1 -0
- package/dist/schema/dataset-input.js +58 -0
- package/dist/schema/dataset-input.js.map +1 -0
- package/dist/schema/dataset.d.ts +166 -0
- package/dist/schema/dataset.d.ts.map +1 -0
- package/dist/schema/dataset.js +281 -0
- package/dist/schema/dataset.js.map +1 -0
- package/dist/schema/declarations.d.ts +15 -0
- package/dist/schema/declarations.d.ts.map +1 -0
- package/dist/schema/declarations.js +47 -0
- package/dist/schema/declarations.js.map +1 -0
- package/dist/schema/document.d.ts +504 -0
- package/dist/schema/document.d.ts.map +1 -0
- package/dist/schema/document.js +337 -0
- package/dist/schema/document.js.map +1 -0
- package/dist/schema/error.d.ts +15 -0
- package/dist/schema/error.d.ts.map +1 -0
- package/dist/schema/error.js +18 -0
- package/dist/schema/error.js.map +1 -0
- package/dist/schema/field-id.d.ts +8 -0
- package/dist/schema/field-id.d.ts.map +1 -0
- package/dist/schema/field-id.js +47 -0
- package/dist/schema/field-id.js.map +1 -0
- package/dist/schema/layout.d.ts +118 -0
- package/dist/schema/layout.d.ts.map +1 -0
- package/dist/schema/layout.js +101 -0
- package/dist/schema/layout.js.map +1 -0
- package/dist/schema/palette.d.ts +103 -0
- package/dist/schema/palette.d.ts.map +1 -0
- package/dist/schema/palette.js +140 -0
- package/dist/schema/palette.js.map +1 -0
- package/dist/schema/read.d.ts +49 -0
- package/dist/schema/read.d.ts.map +1 -0
- package/dist/schema/read.js +942 -0
- package/dist/schema/read.js.map +1 -0
- package/dist/store/apply-dataset.d.ts +14 -0
- package/dist/store/apply-dataset.d.ts.map +1 -0
- package/dist/store/apply-dataset.js +513 -0
- package/dist/store/apply-dataset.js.map +1 -0
- package/dist/store/apply.d.ts +37 -0
- package/dist/store/apply.d.ts.map +1 -0
- package/dist/store/apply.js +2269 -0
- package/dist/store/apply.js.map +1 -0
- package/dist/store/candidate.d.ts +37 -0
- package/dist/store/candidate.d.ts.map +1 -0
- package/dist/store/candidate.js +178 -0
- package/dist/store/candidate.js.map +1 -0
- package/dist/store/commands/coverage.d.ts +58 -0
- package/dist/store/commands/coverage.d.ts.map +1 -0
- package/dist/store/commands/coverage.js +354 -0
- package/dist/store/commands/coverage.js.map +1 -0
- package/dist/store/commands/dataset.d.ts +26 -0
- package/dist/store/commands/dataset.d.ts.map +1 -0
- package/dist/store/commands/dataset.js +368 -0
- package/dist/store/commands/dataset.js.map +1 -0
- package/dist/store/commands/document.d.ts +21 -0
- package/dist/store/commands/document.d.ts.map +1 -0
- package/dist/store/commands/document.js +160 -0
- package/dist/store/commands/document.js.map +1 -0
- package/dist/store/commands/field.d.ts +32 -0
- package/dist/store/commands/field.d.ts.map +1 -0
- package/dist/store/commands/field.js +572 -0
- package/dist/store/commands/field.js.map +1 -0
- package/dist/store/commands/layout.d.ts +16 -0
- package/dist/store/commands/layout.d.ts.map +1 -0
- package/dist/store/commands/layout.js +70 -0
- package/dist/store/commands/layout.js.map +1 -0
- package/dist/store/commands/parse.d.ts +19 -0
- package/dist/store/commands/parse.d.ts.map +1 -0
- package/dist/store/commands/parse.js +328 -0
- package/dist/store/commands/parse.js.map +1 -0
- package/dist/store/commands/readers.d.ts +198 -0
- package/dist/store/commands/readers.d.ts.map +1 -0
- package/dist/store/commands/readers.js +711 -0
- package/dist/store/commands/readers.js.map +1 -0
- package/dist/store/commands/rule.d.ts +24 -0
- package/dist/store/commands/rule.d.ts.map +1 -0
- package/dist/store/commands/rule.js +379 -0
- package/dist/store/commands/rule.js.map +1 -0
- package/dist/store/commands/shapes.d.ts +900 -0
- package/dist/store/commands/shapes.d.ts.map +1 -0
- package/dist/store/commands/shapes.js +166 -0
- package/dist/store/commands/shapes.js.map +1 -0
- package/dist/store/commands/suggestions.d.ts +37 -0
- package/dist/store/commands/suggestions.d.ts.map +1 -0
- package/dist/store/commands/suggestions.js +177 -0
- package/dist/store/commands/suggestions.js.map +1 -0
- package/dist/store/commands.d.ts +16 -0
- package/dist/store/commands.d.ts.map +1 -0
- package/dist/store/commands.js +13 -0
- package/dist/store/commands.js.map +1 -0
- package/dist/store/dataset-candidate.d.ts +33 -0
- package/dist/store/dataset-candidate.d.ts.map +1 -0
- package/dist/store/dataset-candidate.js +31 -0
- package/dist/store/dataset-candidate.js.map +1 -0
- package/dist/store/dataset-pin.d.ts +93 -0
- package/dist/store/dataset-pin.d.ts.map +1 -0
- package/dist/store/dataset-pin.js +86 -0
- package/dist/store/dataset-pin.js.map +1 -0
- package/dist/store/dataset-store.d.ts +96 -0
- package/dist/store/dataset-store.d.ts.map +1 -0
- package/dist/store/dataset-store.js +112 -0
- package/dist/store/dataset-store.js.map +1 -0
- package/dist/store/history.d.ts +149 -0
- package/dist/store/history.d.ts.map +1 -0
- package/dist/store/history.js +173 -0
- package/dist/store/history.js.map +1 -0
- package/dist/store/measurement.d.ts +5 -0
- package/dist/store/measurement.d.ts.map +1 -0
- package/dist/store/measurement.js +20 -0
- package/dist/store/measurement.js.map +1 -0
- package/dist/store/notify.d.ts +11 -0
- package/dist/store/notify.d.ts.map +1 -0
- package/dist/store/notify.js +26 -0
- package/dist/store/notify.js.map +1 -0
- package/dist/store/published.d.ts +50 -0
- package/dist/store/published.d.ts.map +1 -0
- package/dist/store/published.js +27 -0
- package/dist/store/published.js.map +1 -0
- package/dist/store/refusals.d.ts +175 -0
- package/dist/store/refusals.d.ts.map +1 -0
- package/dist/store/refusals.js +160 -0
- package/dist/store/refusals.js.map +1 -0
- package/dist/store/steps.d.ts +14 -0
- package/dist/store/steps.d.ts.map +1 -0
- package/dist/store/steps.js +124 -0
- package/dist/store/steps.js.map +1 -0
- package/dist/store/store.d.ts +142 -0
- package/dist/store/store.d.ts.map +1 -0
- package/dist/store/store.js +136 -0
- package/dist/store/store.js.map +1 -0
- package/dist/store/unchanged.d.ts +12 -0
- package/dist/store/unchanged.d.ts.map +1 -0
- package/dist/store/unchanged.js +35 -0
- package/dist/store/unchanged.js.map +1 -0
- package/dist/visual/emit.d.ts +21 -0
- package/dist/visual/emit.d.ts.map +1 -0
- package/dist/visual/emit.js +133 -0
- package/dist/visual/emit.js.map +1 -0
- package/dist/visual/parse.d.ts +32 -0
- package/dist/visual/parse.d.ts.map +1 -0
- package/dist/visual/parse.js +330 -0
- package/dist/visual/parse.js.map +1 -0
- package/dist/visual/project.d.ts +40 -0
- package/dist/visual/project.d.ts.map +1 -0
- package/dist/visual/project.js +349 -0
- package/dist/visual/project.js.map +1 -0
- package/dist/visual/represent.d.ts +50 -0
- package/dist/visual/represent.d.ts.map +1 -0
- package/dist/visual/represent.js +66 -0
- package/dist/visual/represent.js.map +1 -0
- package/dist/visual/vocabulary.d.ts +121 -0
- package/dist/visual/vocabulary.d.ts.map +1 -0
- package/dist/visual/vocabulary.js +32 -0
- package/dist/visual/vocabulary.js.map +1 -0
- package/package.json +52 -3
|
@@ -0,0 +1,578 @@
|
|
|
1
|
+
import { AxiomError, DATASET_BUILTINS, DEFAULT_EVIDENCE_TIER, andAll, asDecimalString, assertNever, evaluateForm, firingBranch, isRecord, isWireValue, subtractDecimalTexts, sumDecimalTexts, } from '@azx-pbc/axiom-runtime';
|
|
2
|
+
/** The number of evaluations one rule's search may spend before it states its budget. */
|
|
3
|
+
const MAX_EVALUATIONS = 512;
|
|
4
|
+
/** The number of leaves whose values the search enumerates in full; beyond it, singly and pairwise. */
|
|
5
|
+
const FULL_PRODUCT_LEAVES = 3;
|
|
6
|
+
/**
|
|
7
|
+
* The declarations a writer owns, which a case must not submit as an input.
|
|
8
|
+
*
|
|
9
|
+
* A `compute` rule produces its target and a `defaultCompute` rule keeps its override's stored
|
|
10
|
+
* value instead of producing one — a case that supplied either would be claiming the record
|
|
11
|
+
* already held what the artifact decides. Leaving them out is also what keeps a writer's case
|
|
12
|
+
* creditable: the vector path refuses a retained stored value as evidence, so a case that
|
|
13
|
+
* supplied the override field would pass without proving the branch.
|
|
14
|
+
*/
|
|
15
|
+
function writerOwnedFieldIds(schema) {
|
|
16
|
+
const ids = new Set();
|
|
17
|
+
for (const rule of schema.rules ?? []) {
|
|
18
|
+
if (rule.kind !== 'compute' && rule.kind !== 'defaultCompute')
|
|
19
|
+
continue;
|
|
20
|
+
if (rule.fieldId !== undefined)
|
|
21
|
+
ids.add(rule.fieldId);
|
|
22
|
+
if (rule.overrideFieldId !== undefined)
|
|
23
|
+
ids.add(rule.overrideFieldId);
|
|
24
|
+
}
|
|
25
|
+
return ids;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Every scalar declaration a case can supply, flattened to the path it travels under.
|
|
29
|
+
*
|
|
30
|
+
* A record's members are the fields a case supplies inside the record; a scalar array is one
|
|
31
|
+
* value. A record array is left out: a case that varies its rows needs row identity, which this
|
|
32
|
+
* search does not invent. A field a writer owns is left out too (`writerOwnedFieldIds`).
|
|
33
|
+
*/
|
|
34
|
+
function leafFields(fields, excluded, prefix = []) {
|
|
35
|
+
const leaves = [];
|
|
36
|
+
for (const field of fields) {
|
|
37
|
+
const path = [...prefix, field.fieldId];
|
|
38
|
+
// A record array is rows whose identity a case would have to invent, so it is not a leaf the
|
|
39
|
+
// search varies. A record's members are: they travel inside the record the case supplies.
|
|
40
|
+
if (field.primitive === 'array' && field.fields !== undefined)
|
|
41
|
+
continue;
|
|
42
|
+
if (field.fields !== undefined) {
|
|
43
|
+
leaves.push(...leafFields(field.fields, excluded, path));
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
if (excluded.has(field.fieldId))
|
|
47
|
+
continue;
|
|
48
|
+
leaves.push({ field, path });
|
|
49
|
+
}
|
|
50
|
+
return leaves;
|
|
51
|
+
}
|
|
52
|
+
function expressionFacts(expressions) {
|
|
53
|
+
const paths = [];
|
|
54
|
+
const calls = [];
|
|
55
|
+
const visit = (node) => {
|
|
56
|
+
switch (node.kind) {
|
|
57
|
+
case 'literal':
|
|
58
|
+
return;
|
|
59
|
+
case 'path':
|
|
60
|
+
paths.push(node.path);
|
|
61
|
+
return;
|
|
62
|
+
case 'list':
|
|
63
|
+
for (const item of node.items)
|
|
64
|
+
visit(item);
|
|
65
|
+
return;
|
|
66
|
+
case 'and':
|
|
67
|
+
case 'or':
|
|
68
|
+
case 'coalesce':
|
|
69
|
+
for (const arg of node.args)
|
|
70
|
+
visit(arg);
|
|
71
|
+
return;
|
|
72
|
+
case 'not':
|
|
73
|
+
visit(node.args[0]);
|
|
74
|
+
return;
|
|
75
|
+
case 'compare':
|
|
76
|
+
visit(node.args[0]);
|
|
77
|
+
visit(node.args[1]);
|
|
78
|
+
return;
|
|
79
|
+
case 'if':
|
|
80
|
+
for (const arg of node.args)
|
|
81
|
+
visit(arg);
|
|
82
|
+
return;
|
|
83
|
+
case 'call':
|
|
84
|
+
calls.push({ op: node.op, args: node.args });
|
|
85
|
+
for (const arg of node.args)
|
|
86
|
+
visit(arg);
|
|
87
|
+
return;
|
|
88
|
+
case 'comprehension':
|
|
89
|
+
visit(node.source);
|
|
90
|
+
if (node.valueExpr !== undefined)
|
|
91
|
+
visit(node.valueExpr);
|
|
92
|
+
if (node.predicate !== undefined)
|
|
93
|
+
visit(node.predicate);
|
|
94
|
+
return;
|
|
95
|
+
default:
|
|
96
|
+
return assertNever(node);
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
for (const expression of expressions)
|
|
100
|
+
visit(expression);
|
|
101
|
+
return { paths, calls };
|
|
102
|
+
}
|
|
103
|
+
function literalValues(expressions) {
|
|
104
|
+
const literals = [];
|
|
105
|
+
const visit = (node) => {
|
|
106
|
+
switch (node.kind) {
|
|
107
|
+
case 'literal':
|
|
108
|
+
literals.push({ domain: node.domain, value: node.value });
|
|
109
|
+
return;
|
|
110
|
+
case 'path':
|
|
111
|
+
return;
|
|
112
|
+
case 'list':
|
|
113
|
+
for (const item of node.items)
|
|
114
|
+
visit(item);
|
|
115
|
+
return;
|
|
116
|
+
case 'and':
|
|
117
|
+
case 'or':
|
|
118
|
+
case 'coalesce':
|
|
119
|
+
for (const arg of node.args)
|
|
120
|
+
visit(arg);
|
|
121
|
+
return;
|
|
122
|
+
case 'not':
|
|
123
|
+
visit(node.args[0]);
|
|
124
|
+
return;
|
|
125
|
+
case 'compare':
|
|
126
|
+
visit(node.args[0]);
|
|
127
|
+
visit(node.args[1]);
|
|
128
|
+
return;
|
|
129
|
+
case 'if':
|
|
130
|
+
for (const arg of node.args)
|
|
131
|
+
visit(arg);
|
|
132
|
+
return;
|
|
133
|
+
case 'call':
|
|
134
|
+
for (const arg of node.args)
|
|
135
|
+
visit(arg);
|
|
136
|
+
return;
|
|
137
|
+
case 'comprehension':
|
|
138
|
+
visit(node.source);
|
|
139
|
+
if (node.valueExpr !== undefined)
|
|
140
|
+
visit(node.valueExpr);
|
|
141
|
+
if (node.predicate !== undefined)
|
|
142
|
+
visit(node.predicate);
|
|
143
|
+
return;
|
|
144
|
+
default:
|
|
145
|
+
return assertNever(node);
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
for (const expression of expressions)
|
|
149
|
+
visit(expression);
|
|
150
|
+
return literals;
|
|
151
|
+
}
|
|
152
|
+
/** Every expression slot of one rule, including the items and reasons a composing kind carries. */
|
|
153
|
+
function ruleExpressionsOf(rule) {
|
|
154
|
+
return [
|
|
155
|
+
rule.expr,
|
|
156
|
+
...(rule.referWhen === undefined ? [] : [rule.referWhen]),
|
|
157
|
+
...Object.values(rule.params ?? {}),
|
|
158
|
+
...(rule.kind === 'screen' ? (rule.reasons ?? []).map((reason) => reason.when) : []),
|
|
159
|
+
...(rule.gate?.activeWhen === undefined ? [] : [rule.gate.activeWhen]),
|
|
160
|
+
...(rule.gate?.items ?? []).flatMap((item) => [item.expr, ...Object.values(item.params ?? {})]),
|
|
161
|
+
];
|
|
162
|
+
}
|
|
163
|
+
/** The rule IDs an expression reads through `rule_ok`, so their own inputs stay in the search. */
|
|
164
|
+
function referencedRuleIds(facts) {
|
|
165
|
+
const ids = [];
|
|
166
|
+
for (const call of facts.calls) {
|
|
167
|
+
if (call.op !== 'rule_ok')
|
|
168
|
+
continue;
|
|
169
|
+
const [first] = call.args;
|
|
170
|
+
if (first?.kind === 'literal' && typeof first.value === 'string')
|
|
171
|
+
ids.push(first.value);
|
|
172
|
+
}
|
|
173
|
+
return ids;
|
|
174
|
+
}
|
|
175
|
+
/** Whether an expression folds a whole step or a sibling rule, so every declaration matters. */
|
|
176
|
+
function foldsOtherRules(facts) {
|
|
177
|
+
return facts.calls.some((call) => call.op === 'rule_ok_in' || call.op === 'no_blocking_in' || call.op === 'required_in');
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* The leaves a rule's outcome can depend on.
|
|
181
|
+
*
|
|
182
|
+
* The walk follows the two ways one rule reads another: `rule_ok` reads a sibling rule's verdict,
|
|
183
|
+
* and a path naming a writer's target reads the value that writer produces. Both are followed
|
|
184
|
+
* transitively, so a validate over a computed total searches the inputs the writer reads rather
|
|
185
|
+
* than declaring the total impossible to vary.
|
|
186
|
+
*/
|
|
187
|
+
function dependencyLeaves(schema, rule, leaves) {
|
|
188
|
+
const rules = schema.rules ?? [];
|
|
189
|
+
const writers = new Map();
|
|
190
|
+
for (const candidate of rules) {
|
|
191
|
+
if (candidate.kind !== 'compute' && candidate.kind !== 'defaultCompute')
|
|
192
|
+
continue;
|
|
193
|
+
if (candidate.fieldId !== undefined)
|
|
194
|
+
writers.set(candidate.fieldId, candidate);
|
|
195
|
+
}
|
|
196
|
+
const facts = expressionFacts(ruleExpressionsOf(rule));
|
|
197
|
+
if (rule.kind === 'gate' || rule.kind === 'screen' || foldsOtherRules(facts))
|
|
198
|
+
return leaves;
|
|
199
|
+
const wanted = new Set();
|
|
200
|
+
const visitRule = (current, seen) => {
|
|
201
|
+
if (seen.has(current.ruleId))
|
|
202
|
+
return;
|
|
203
|
+
const next = new Set(seen);
|
|
204
|
+
next.add(current.ruleId);
|
|
205
|
+
const currentFacts = expressionFacts(ruleExpressionsOf(current));
|
|
206
|
+
if (currentFacts.calls.some((call) => call.op === 'rule_ok_in'))
|
|
207
|
+
for (const leaf of leaves)
|
|
208
|
+
wanted.add(leaf.field.fieldId);
|
|
209
|
+
for (const path of currentFacts.paths) {
|
|
210
|
+
const match = leaves.find((leaf) => leaf.path.join('.') === path);
|
|
211
|
+
if (match !== undefined) {
|
|
212
|
+
wanted.add(match.field.fieldId);
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
const writer = writers.get(path);
|
|
216
|
+
if (writer !== undefined)
|
|
217
|
+
visitRule(writer, next);
|
|
218
|
+
}
|
|
219
|
+
for (const id of referencedRuleIds(currentFacts)) {
|
|
220
|
+
const referenced = rules.find((candidate) => candidate.ruleId === id);
|
|
221
|
+
if (referenced !== undefined)
|
|
222
|
+
visitRule(referenced, next);
|
|
223
|
+
}
|
|
224
|
+
};
|
|
225
|
+
visitRule(rule, new Set());
|
|
226
|
+
const target = rule.scope === 'item' ? rule.targetFieldId : rule.fieldId;
|
|
227
|
+
if (target !== undefined) {
|
|
228
|
+
const match = leaves.find((leaf) => leaf.field.fieldId === target);
|
|
229
|
+
if (match !== undefined)
|
|
230
|
+
wanted.add(match.field.fieldId);
|
|
231
|
+
}
|
|
232
|
+
return leaves.filter((leaf) => wanted.has(leaf.field.fieldId));
|
|
233
|
+
}
|
|
234
|
+
/** The field IDs an `is_email` call reads, so their preferred value is a valid address. */
|
|
235
|
+
function emailFieldsOf(expressions, leaves) {
|
|
236
|
+
const fields = new Set();
|
|
237
|
+
const facts = expressionFacts(expressions);
|
|
238
|
+
for (const call of facts.calls) {
|
|
239
|
+
if (call.op !== 'is_email')
|
|
240
|
+
continue;
|
|
241
|
+
for (const path of expressionFacts(call.args).paths) {
|
|
242
|
+
const match = leaves.find((leaf) => leaf.path.join('.') === path);
|
|
243
|
+
if (match !== undefined)
|
|
244
|
+
fields.add(match.field.fieldId);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
return fields;
|
|
248
|
+
}
|
|
249
|
+
/** The expression's context roots, for the sentence that says a record cannot supply them. */
|
|
250
|
+
function contextPathsOf(schema, expressions) {
|
|
251
|
+
const declared = new Set((schema.context?.roots ?? []).map((root) => `context.${root.path}`));
|
|
252
|
+
return expressionFacts(expressions).paths.filter((path) => path.startsWith('context.') || declared.has(path));
|
|
253
|
+
}
|
|
254
|
+
/** The tables an expression reads, for the same sentence. */
|
|
255
|
+
function tableIdsOf(expressions) {
|
|
256
|
+
const tables = new Set();
|
|
257
|
+
for (const call of expressionFacts(expressions).calls) {
|
|
258
|
+
if (!DATASET_BUILTINS.has(call.op))
|
|
259
|
+
continue;
|
|
260
|
+
const [first] = call.args;
|
|
261
|
+
if (first?.kind === 'path')
|
|
262
|
+
tables.add(first.path);
|
|
263
|
+
}
|
|
264
|
+
return [...tables];
|
|
265
|
+
}
|
|
266
|
+
/** A stable key for a candidate, so a dedupe cannot lose a value two spellings share. */
|
|
267
|
+
function valueKey(value) {
|
|
268
|
+
return value === undefined ? 'absent' : JSON.stringify(value);
|
|
269
|
+
}
|
|
270
|
+
/** The preferred value for one field: the one a plain "all answers supplied" case uses. */
|
|
271
|
+
function preferredValue(field, emailFields) {
|
|
272
|
+
switch (field.primitive) {
|
|
273
|
+
case 'boolean':
|
|
274
|
+
return true;
|
|
275
|
+
case 'integer':
|
|
276
|
+
return 1;
|
|
277
|
+
case 'string':
|
|
278
|
+
if (field.options !== undefined)
|
|
279
|
+
return field.options[0]?.code ?? 'x';
|
|
280
|
+
return emailFields.has(field.fieldId) ? 'a@example.co' : 'x';
|
|
281
|
+
case 'array':
|
|
282
|
+
return [];
|
|
283
|
+
case 'timeslot':
|
|
284
|
+
// A slot is a closed record, not a scalar the search can adapt. One well-formed slot is
|
|
285
|
+
// enough to prove a presence check's passing branch; a rule that needs a particular
|
|
286
|
+
// window states that dependency in its own literals.
|
|
287
|
+
return {
|
|
288
|
+
slotId: 'slot_case',
|
|
289
|
+
localDate: '2026-09-02',
|
|
290
|
+
start: '2026-09-02T09:00:00.000Z',
|
|
291
|
+
end: '2026-09-02T11:00:00.000Z',
|
|
292
|
+
};
|
|
293
|
+
case 'decimal':
|
|
294
|
+
case 'money':
|
|
295
|
+
case 'date':
|
|
296
|
+
case 'instant':
|
|
297
|
+
case 'time':
|
|
298
|
+
case 'media_ref':
|
|
299
|
+
case 'signature_ref':
|
|
300
|
+
case 'quantity':
|
|
301
|
+
case 'duration':
|
|
302
|
+
case 'address':
|
|
303
|
+
case 'coordinate':
|
|
304
|
+
case 'record':
|
|
305
|
+
return undefined;
|
|
306
|
+
default:
|
|
307
|
+
return assertNever(field.primitive);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
/** The values one field may take in the search, preferred first, `undefined` meaning absent. */
|
|
311
|
+
function candidateValues(field, literals, emailFields) {
|
|
312
|
+
const preferred = preferredValue(field, emailFields);
|
|
313
|
+
const sameDomain = literals
|
|
314
|
+
.filter((literal) => literal.domain === field.primitive)
|
|
315
|
+
.map((literal) => literal.value);
|
|
316
|
+
const options = (field.options ?? []).map((option) => option.code);
|
|
317
|
+
const strings = [
|
|
318
|
+
...sameDomain,
|
|
319
|
+
...options,
|
|
320
|
+
...(emailFields.has(field.fieldId) ? ['a@example.co'] : []),
|
|
321
|
+
'a@example.co',
|
|
322
|
+
'x',
|
|
323
|
+
'',
|
|
324
|
+
];
|
|
325
|
+
const numbers = sameDomain.filter((value) => typeof value === 'number' && Number.isSafeInteger(value));
|
|
326
|
+
const candidates = [preferred, undefined];
|
|
327
|
+
switch (field.primitive) {
|
|
328
|
+
case 'boolean':
|
|
329
|
+
for (const value of [true, false])
|
|
330
|
+
candidates.push(value);
|
|
331
|
+
break;
|
|
332
|
+
case 'integer':
|
|
333
|
+
for (const value of [0, 1, -1, ...numbers, ...numbers.flatMap((n) => [n + 1, n - 1])]) {
|
|
334
|
+
candidates.push(value);
|
|
335
|
+
}
|
|
336
|
+
break;
|
|
337
|
+
case 'string':
|
|
338
|
+
for (const value of strings)
|
|
339
|
+
candidates.push(value);
|
|
340
|
+
break;
|
|
341
|
+
case 'array':
|
|
342
|
+
candidates.push([]);
|
|
343
|
+
break;
|
|
344
|
+
case 'decimal':
|
|
345
|
+
for (const value of sameDomain) {
|
|
346
|
+
candidates.push(value);
|
|
347
|
+
// A threshold is the operand a comparison reads, so the value just past it is the one
|
|
348
|
+
// that decides the other branch. `sumDecimalTexts` keeps the literal's scale and sign.
|
|
349
|
+
if (typeof value === 'string') {
|
|
350
|
+
try {
|
|
351
|
+
candidates.push(sumDecimalTexts([asDecimalString(value), asDecimalString('1')]));
|
|
352
|
+
candidates.push(subtractDecimalTexts(asDecimalString(value), asDecimalString('1')));
|
|
353
|
+
}
|
|
354
|
+
catch {
|
|
355
|
+
// A literal this constructor refuses is not one the search can step past.
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
break;
|
|
360
|
+
case 'money':
|
|
361
|
+
case 'date':
|
|
362
|
+
case 'instant':
|
|
363
|
+
case 'time':
|
|
364
|
+
case 'media_ref':
|
|
365
|
+
case 'signature_ref':
|
|
366
|
+
case 'timeslot':
|
|
367
|
+
case 'quantity':
|
|
368
|
+
case 'duration':
|
|
369
|
+
case 'address':
|
|
370
|
+
case 'coordinate':
|
|
371
|
+
for (const value of sameDomain)
|
|
372
|
+
candidates.push(value);
|
|
373
|
+
break;
|
|
374
|
+
case 'record':
|
|
375
|
+
break;
|
|
376
|
+
default:
|
|
377
|
+
return assertNever(field.primitive);
|
|
378
|
+
}
|
|
379
|
+
const seen = new Set();
|
|
380
|
+
return candidates.filter((value) => {
|
|
381
|
+
if (value !== undefined && !isWireValue(value))
|
|
382
|
+
return false;
|
|
383
|
+
const key = valueKey(value);
|
|
384
|
+
if (seen.has(key))
|
|
385
|
+
return false;
|
|
386
|
+
seen.add(key);
|
|
387
|
+
return true;
|
|
388
|
+
});
|
|
389
|
+
}
|
|
390
|
+
/** A values tree with one leaf set, or removed when the candidate is absent. */
|
|
391
|
+
function withLeaf(values, path, value) {
|
|
392
|
+
const [head, ...rest] = path;
|
|
393
|
+
if (head === undefined)
|
|
394
|
+
return { ...values };
|
|
395
|
+
const next = { ...values };
|
|
396
|
+
if (rest.length === 0) {
|
|
397
|
+
if (value === undefined)
|
|
398
|
+
delete next[head];
|
|
399
|
+
else
|
|
400
|
+
next[head] = value;
|
|
401
|
+
return next;
|
|
402
|
+
}
|
|
403
|
+
const existing = next[head];
|
|
404
|
+
const nested = isRecord(existing) ? existing : {};
|
|
405
|
+
const child = withLeaf(nested, rest, value);
|
|
406
|
+
if (Object.keys(child).length === 0)
|
|
407
|
+
delete next[head];
|
|
408
|
+
else
|
|
409
|
+
next[head] = child;
|
|
410
|
+
return next;
|
|
411
|
+
}
|
|
412
|
+
/** One assignment's identity, so two routes to the same values spend one evaluation. */
|
|
413
|
+
function valuesKey(values) {
|
|
414
|
+
return JSON.stringify(Object.keys(values)
|
|
415
|
+
.sort()
|
|
416
|
+
.map((key) => [key, values[key]]));
|
|
417
|
+
}
|
|
418
|
+
/**
|
|
419
|
+
* The values assignments the search tries, simplest first.
|
|
420
|
+
*
|
|
421
|
+
* The empty record and the all-preferred record come first because they are the two cases an
|
|
422
|
+
* author would write by hand. Every single-field variation follows: one field is almost always
|
|
423
|
+
* what moves a rule from one branch to the other. A pair and, for a small rule, the whole
|
|
424
|
+
* cross-product follow where a conjunction needs two operands set together. The order is the
|
|
425
|
+
* search's budget: the first assignment that reaches the branch is the case it writes, so the
|
|
426
|
+
* simplest values that prove the branch win.
|
|
427
|
+
*/
|
|
428
|
+
function assignmentsOf(leaves, candidates) {
|
|
429
|
+
const assignments = [];
|
|
430
|
+
const seen = new Set();
|
|
431
|
+
const push = (values) => {
|
|
432
|
+
if (assignments.length >= MAX_EVALUATIONS)
|
|
433
|
+
return;
|
|
434
|
+
const key = valuesKey(values);
|
|
435
|
+
if (seen.has(key))
|
|
436
|
+
return;
|
|
437
|
+
seen.add(key);
|
|
438
|
+
assignments.push(values);
|
|
439
|
+
};
|
|
440
|
+
push({});
|
|
441
|
+
let base = {};
|
|
442
|
+
for (const leaf of leaves) {
|
|
443
|
+
const preferred = candidates.get(leaf.field.fieldId)?.[0];
|
|
444
|
+
if (preferred !== undefined)
|
|
445
|
+
base = withLeaf(base, leaf.path, preferred);
|
|
446
|
+
}
|
|
447
|
+
push(base);
|
|
448
|
+
for (const leaf of leaves) {
|
|
449
|
+
for (const value of candidates.get(leaf.field.fieldId) ?? []) {
|
|
450
|
+
push(withLeaf(base, leaf.path, value));
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
for (let first = 0; first < leaves.length; first += 1) {
|
|
454
|
+
for (let second = first + 1; second < leaves.length; second += 1) {
|
|
455
|
+
const left = leaves[first];
|
|
456
|
+
const right = leaves[second];
|
|
457
|
+
if (left === undefined || right === undefined)
|
|
458
|
+
continue;
|
|
459
|
+
const leftValues = (candidates.get(left.field.fieldId) ?? []).slice(0, 4);
|
|
460
|
+
const rightValues = (candidates.get(right.field.fieldId) ?? []).slice(0, 4);
|
|
461
|
+
for (const leftValue of leftValues) {
|
|
462
|
+
for (const rightValue of rightValues) {
|
|
463
|
+
push(withLeaf(withLeaf(base, left.path, leftValue), right.path, rightValue));
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
if (leaves.length > 0 && leaves.length <= FULL_PRODUCT_LEAVES) {
|
|
469
|
+
let product = [{}];
|
|
470
|
+
for (const leaf of leaves) {
|
|
471
|
+
const next = [];
|
|
472
|
+
for (const values of product) {
|
|
473
|
+
for (const value of (candidates.get(leaf.field.fieldId) ?? []).slice(0, 4)) {
|
|
474
|
+
next.push(withLeaf(values, leaf.path, value));
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
product = next.slice(0, MAX_EVALUATIONS);
|
|
478
|
+
}
|
|
479
|
+
for (const values of product)
|
|
480
|
+
push(values);
|
|
481
|
+
}
|
|
482
|
+
return assignments;
|
|
483
|
+
}
|
|
484
|
+
/**
|
|
485
|
+
* The rule's own result and the `expected` a case states for it.
|
|
486
|
+
*
|
|
487
|
+
* This is the whole reason a generated case can hold: the expectation is read from the
|
|
488
|
+
* evaluation rather than chosen for the branch. A screen states its own five-state outcome, a
|
|
489
|
+
* writer states the value it produced (`null` included), and every other kind states the folded
|
|
490
|
+
* four-state outcome the report carries.
|
|
491
|
+
*/
|
|
492
|
+
function observeRule(schema, rule, values) {
|
|
493
|
+
let snapshot;
|
|
494
|
+
try {
|
|
495
|
+
snapshot = evaluateForm(schema, { values }, { tier: DEFAULT_EVIDENCE_TIER });
|
|
496
|
+
}
|
|
497
|
+
catch (error) {
|
|
498
|
+
if (!(error instanceof AxiomError))
|
|
499
|
+
throw error;
|
|
500
|
+
return undefined;
|
|
501
|
+
}
|
|
502
|
+
if (snapshot.report.diagnostics.some((diagnostic) => diagnostic.code === 'ENGINE_ERROR' && diagnostic.ruleId === rule.ruleId)) {
|
|
503
|
+
return undefined;
|
|
504
|
+
}
|
|
505
|
+
if (rule.kind === 'screen') {
|
|
506
|
+
const outcome = snapshot.report.screens?.[rule.ruleId]?.outcome;
|
|
507
|
+
if (outcome === undefined)
|
|
508
|
+
return undefined;
|
|
509
|
+
return { observation: { kind: 'screen', outcome }, expected: outcome };
|
|
510
|
+
}
|
|
511
|
+
const results = snapshot.report.rules.filter((result) => result.ruleId === rule.ruleId);
|
|
512
|
+
if (results.length === 0)
|
|
513
|
+
return undefined;
|
|
514
|
+
const status = andAll(results.map((result) => result.status));
|
|
515
|
+
if (rule.kind === 'compute' || rule.kind === 'defaultCompute') {
|
|
516
|
+
const [only] = results;
|
|
517
|
+
if (status === 'PASS' && results.length === 1 && only !== undefined) {
|
|
518
|
+
const value = only.value ?? null;
|
|
519
|
+
return { observation: { kind: 'value', value }, expected: value };
|
|
520
|
+
}
|
|
521
|
+
return { observation: { kind: 'outcome', outcome: status }, expected: status };
|
|
522
|
+
}
|
|
523
|
+
return { observation: { kind: 'outcome', outcome: status }, expected: status };
|
|
524
|
+
}
|
|
525
|
+
/** The sentence that says why this rule's branch cannot be written as an embedded case. */
|
|
526
|
+
function impossibleReason(schema, rule, leaves, branch, expressions) {
|
|
527
|
+
const contexts = contextPathsOf(schema, expressions);
|
|
528
|
+
if (contexts.length > 0) {
|
|
529
|
+
return `it depends on host-supplied context \`${contexts[0] ?? ''}\`, which an embedded case's \`values\` cannot carry`;
|
|
530
|
+
}
|
|
531
|
+
const tables = tableIdsOf(expressions);
|
|
532
|
+
if (tables.length > 0) {
|
|
533
|
+
return `it depends on table \`${tables[0] ?? ''}\`, which a case's values do not resolve`;
|
|
534
|
+
}
|
|
535
|
+
if (expressionFacts(expressions).calls.some((call) => call.op === 'has_cap')) {
|
|
536
|
+
return 'it depends on a host capability, which a case states as an environment member rather than a value';
|
|
537
|
+
}
|
|
538
|
+
if (rule.scope === 'item') {
|
|
539
|
+
return `it decides one row of \`${rule.fieldId ?? 'an array'}\`, and a case that varies rows needs row identity this repair does not invent`;
|
|
540
|
+
}
|
|
541
|
+
if (leaves.length === 0) {
|
|
542
|
+
return 'it reads no declared field, so no value a case can supply changes its answer';
|
|
543
|
+
}
|
|
544
|
+
return `no values among the ${String(leaves.length)} ${leaves.length === 1 ? 'field' : 'fields'} it reads make a \`${rule.kind}\` rule ${branch === 'firing' ? 'fire' : 'not fire'}`;
|
|
545
|
+
}
|
|
546
|
+
/**
|
|
547
|
+
* The case that proves one branch of one rule, or the reason no case does.
|
|
548
|
+
*
|
|
549
|
+
* `caseId` is the caller's: an authoring document identifies one vector by it, and the repair
|
|
550
|
+
* walk owns the naming so a second click cannot mint a second id for the same branch.
|
|
551
|
+
*/
|
|
552
|
+
export function synthesizeRuleCase(schema, rule, branch, caseId) {
|
|
553
|
+
const leaves = leafFields(schema.fields, writerOwnedFieldIds(schema));
|
|
554
|
+
const dependencies = dependencyLeaves(schema, rule, leaves);
|
|
555
|
+
// A gate or a screen decides through the rules its items and conditions name, so its literals
|
|
556
|
+
// and the fields an `is_email` call reads belong to those rules too. Reading only the composing
|
|
557
|
+
// rule's own expression would miss the valid address its item delegates to another rule.
|
|
558
|
+
const ownExpressions = ruleExpressionsOf(rule);
|
|
559
|
+
const expressions = rule.kind === 'gate' || rule.kind === 'screen'
|
|
560
|
+
? [...ownExpressions, ...(schema.rules ?? []).flatMap(ruleExpressionsOf)]
|
|
561
|
+
: ownExpressions;
|
|
562
|
+
const literals = literalValues(expressions);
|
|
563
|
+
const emailFields = emailFieldsOf(expressions, leaves);
|
|
564
|
+
const candidates = new Map(dependencies.map((leaf) => [
|
|
565
|
+
leaf.field.fieldId,
|
|
566
|
+
candidateValues(leaf.field, literals, emailFields),
|
|
567
|
+
]));
|
|
568
|
+
for (const values of assignmentsOf(dependencies, candidates)) {
|
|
569
|
+
const observed = observeRule(schema, rule, values);
|
|
570
|
+
if (observed === undefined)
|
|
571
|
+
continue;
|
|
572
|
+
if (firingBranch(rule.kind, observed.observation) !== (branch === 'firing'))
|
|
573
|
+
continue;
|
|
574
|
+
return { ok: true, testCase: { caseId, expected: observed.expected, values } };
|
|
575
|
+
}
|
|
576
|
+
return { ok: false, reason: impossibleReason(schema, rule, dependencies, branch, expressions) };
|
|
577
|
+
}
|
|
578
|
+
//# sourceMappingURL=cases.js.map
|