@turingcode/cli 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli/create-cli.js +21 -0
- package/dist/cli/index.js +19 -0
- package/dist/core/mcp.js +80 -0
- package/dist/features/clock/command.js +66 -0
- package/dist/features/table/ast.js +177 -0
- package/dist/features/table/command.js +95 -0
- package/dist/features/table/contract.js +487 -0
- package/dist/features/table/contract_check.js +265 -0
- package/dist/features/table/describe.js +197 -0
- package/dist/features/table/diagnostics.js +129 -0
- package/dist/features/table/format.js +75 -0
- package/dist/features/table/fraction.js +130 -0
- package/dist/features/table/primitives.js +268 -0
- package/dist/features/table/shape.js +137 -0
- package/dist/features/table/source_kinds.js +41 -0
- package/dist/features/table/table_check.js +538 -0
- package/dist/features/table/tables.js +223 -0
- package/package.json +30 -3
- package/README.md +0 -3
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/** Largest integer exponent magnitude allowed anywhere in exact arithmetic (spec §3.4). */
|
|
2
|
+
export const MAX_EXPONENT = 1000;
|
|
3
|
+
/** Raised for any arithmetic the exact engine refuses (bad literal, division by zero, bad exponent). */
|
|
4
|
+
export class ArithmeticError extends Error {
|
|
5
|
+
}
|
|
6
|
+
const abs = (v) => (v < 0n ? -v : v);
|
|
7
|
+
/**
|
|
8
|
+
* Greatest common divisor of two bigints.
|
|
9
|
+
*
|
|
10
|
+
* @param {bigint} a - First value.
|
|
11
|
+
* @param {bigint} b - Second value.
|
|
12
|
+
* @returns {bigint} The non-negative gcd.
|
|
13
|
+
*/
|
|
14
|
+
function gcd(a, b) {
|
|
15
|
+
a = abs(a);
|
|
16
|
+
b = abs(b);
|
|
17
|
+
while (b) {
|
|
18
|
+
[a, b] = [b, a % b];
|
|
19
|
+
}
|
|
20
|
+
return a;
|
|
21
|
+
}
|
|
22
|
+
/** An exact rational number on BigInt; never touches a JS float. */
|
|
23
|
+
export class Fraction {
|
|
24
|
+
n;
|
|
25
|
+
d;
|
|
26
|
+
/**
|
|
27
|
+
* Builds a reduced fraction with a positive denominator.
|
|
28
|
+
*
|
|
29
|
+
* @param {bigint} n - Numerator.
|
|
30
|
+
* @param {bigint} [d=1n] - Denominator.
|
|
31
|
+
* @throws {ArithmeticError} When the denominator is zero.
|
|
32
|
+
*/
|
|
33
|
+
constructor(n, d = 1n) {
|
|
34
|
+
if (d === 0n) {
|
|
35
|
+
throw new ArithmeticError("division by zero");
|
|
36
|
+
}
|
|
37
|
+
if (d < 0n) {
|
|
38
|
+
n = -n;
|
|
39
|
+
d = -d;
|
|
40
|
+
}
|
|
41
|
+
const g = gcd(n, d) || 1n;
|
|
42
|
+
this.n = n / g;
|
|
43
|
+
this.d = d / g;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Parses a number literal: integer, decimal (with optional exponent),
|
|
47
|
+
* `0x..` / `..h` hex, or `0b..` / `..b` binary.
|
|
48
|
+
*
|
|
49
|
+
* @param {string} text - The literal.
|
|
50
|
+
* @returns {Fraction} Its exact value.
|
|
51
|
+
* @throws {ArithmeticError} When the text is not a literal or its exponent exceeds MAX_EXPONENT.
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* Fraction.parse("1e-3").toString(); // "1/1000"
|
|
55
|
+
*/
|
|
56
|
+
static parse(text) {
|
|
57
|
+
let m;
|
|
58
|
+
if ((m = /^0[xX]([0-9a-fA-F]+)$/.exec(text)) || (m = /^([0-9][0-9a-fA-F]*)[hH]$/.exec(text))) {
|
|
59
|
+
return new Fraction(BigInt("0x" + m[1]));
|
|
60
|
+
}
|
|
61
|
+
if ((m = /^0[bB]([01]+)$/.exec(text)) || (m = /^([01]+)[bB]$/.exec(text))) {
|
|
62
|
+
return new Fraction(BigInt("0b" + m[1]));
|
|
63
|
+
}
|
|
64
|
+
m = /^(\d*)(?:\.(\d*))?(?:[eE]([+-]?\d+))?$/.exec(text);
|
|
65
|
+
if (!m || !/\d/.test((m[1] ?? "") + (m[2] ?? ""))) {
|
|
66
|
+
throw new ArithmeticError(`not a number literal: '${text}'`);
|
|
67
|
+
}
|
|
68
|
+
const frac = m[2] ?? "";
|
|
69
|
+
const exp = Number(m[3] ?? "0") - frac.length;
|
|
70
|
+
if (Math.abs(exp) > MAX_EXPONENT) {
|
|
71
|
+
throw new ArithmeticError(`exponent of '${text}' exceeds ${MAX_EXPONENT}`);
|
|
72
|
+
}
|
|
73
|
+
const digits = BigInt((m[1] ?? "") + frac || "0");
|
|
74
|
+
return exp >= 0 ? new Fraction(digits * 10n ** BigInt(exp)) : new Fraction(digits, 10n ** BigInt(-exp));
|
|
75
|
+
}
|
|
76
|
+
/** @returns {boolean} Whether the value is zero. */
|
|
77
|
+
isZero() {
|
|
78
|
+
return this.n === 0n;
|
|
79
|
+
}
|
|
80
|
+
/** @returns {boolean} Whether the value is a whole number. */
|
|
81
|
+
isInteger() {
|
|
82
|
+
return this.d === 1n;
|
|
83
|
+
}
|
|
84
|
+
/** @param {Fraction} o - Addend. @returns {Fraction} this + o. */
|
|
85
|
+
add(o) {
|
|
86
|
+
return new Fraction(this.n * o.d + o.n * this.d, this.d * o.d);
|
|
87
|
+
}
|
|
88
|
+
/** @param {Fraction} o - Subtrahend. @returns {Fraction} this - o. */
|
|
89
|
+
sub(o) {
|
|
90
|
+
return new Fraction(this.n * o.d - o.n * this.d, this.d * o.d);
|
|
91
|
+
}
|
|
92
|
+
/** @param {Fraction} o - Factor. @returns {Fraction} this * o. */
|
|
93
|
+
mul(o) {
|
|
94
|
+
return new Fraction(this.n * o.n, this.d * o.d);
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* @param {Fraction} o - Divisor.
|
|
98
|
+
* @returns {Fraction} this / o.
|
|
99
|
+
* @throws {ArithmeticError} When o is zero.
|
|
100
|
+
*/
|
|
101
|
+
div(o) {
|
|
102
|
+
return new Fraction(this.n * o.d, this.d * o.n);
|
|
103
|
+
}
|
|
104
|
+
/** @returns {Fraction} -this. */
|
|
105
|
+
neg() {
|
|
106
|
+
return new Fraction(-this.n, this.d);
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Raises to a whole exponent.
|
|
110
|
+
*
|
|
111
|
+
* @param {Fraction} e - Exponent; must be an integer with |e| <= MAX_EXPONENT.
|
|
112
|
+
* @returns {Fraction} this ** e.
|
|
113
|
+
* @throws {ArithmeticError} For a non-integer or too-large exponent, or 0 ** negative.
|
|
114
|
+
*/
|
|
115
|
+
pow(e) {
|
|
116
|
+
if (!e.isInteger()) {
|
|
117
|
+
throw new ArithmeticError("exponent must be a whole number");
|
|
118
|
+
}
|
|
119
|
+
if (abs(e.n) > BigInt(MAX_EXPONENT)) {
|
|
120
|
+
throw new ArithmeticError(`exponent exceeds ${MAX_EXPONENT}`);
|
|
121
|
+
}
|
|
122
|
+
const k = abs(e.n);
|
|
123
|
+
const raised = new Fraction(this.n ** k, this.d ** k);
|
|
124
|
+
return e.n < 0n ? new Fraction(1n).div(raised) : raised;
|
|
125
|
+
}
|
|
126
|
+
/** @returns {string} `n` or `n/d`. */
|
|
127
|
+
toString() {
|
|
128
|
+
return this.d === 1n ? `${this.n}` : `${this.n}/${this.d}`;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
const S = "string";
|
|
2
|
+
const SL = { list: "string" };
|
|
3
|
+
const B = "bool";
|
|
4
|
+
const I = "int";
|
|
5
|
+
const WHEN = { map: "string_or_list" };
|
|
6
|
+
/**
|
|
7
|
+
* The closed catalog of rule primitives with their normative params
|
|
8
|
+
* signatures (spec §3.3, "Normative params Signatures"). Names, including
|
|
9
|
+
* their spelling, are copied from the spec.
|
|
10
|
+
*/
|
|
11
|
+
export const PRIMITIVES = {
|
|
12
|
+
required_tables: { group: "C0", params: { "extensions_table?": S } },
|
|
13
|
+
column_order: { group: "C0", params: { "allow_extensions?": B, "extensions_table?": S } },
|
|
14
|
+
id_pattern: { group: "C0", params: { id_col: S, pattern: "regex" } },
|
|
15
|
+
unique_id: { group: "C0", params: { id_col: S } },
|
|
16
|
+
allowed_values: { group: "C0", params: { column: S, values: SL, "allow_dash?": B, "multi_valued_sep?": S } },
|
|
17
|
+
non_empty: { group: "C0", params: { columns: SL } },
|
|
18
|
+
conditional_non_dash: { group: "C0", params: { when_column: S, in: SL, target_column: S, "when?": WHEN } },
|
|
19
|
+
sentinel_row: { group: "C0", params: { id_col: S, id_value: S, min_count: I, max_count: I } },
|
|
20
|
+
extension_schema: { group: "C0", params: { "extensions_table?": S } },
|
|
21
|
+
foreign_key: {
|
|
22
|
+
group: "C1",
|
|
23
|
+
params: { column: S, "target_table?": S, "target_column?": S, "target_contracts?": SL, "allow_dash?": B },
|
|
24
|
+
},
|
|
25
|
+
scoped_id_trace: {
|
|
26
|
+
group: "C1",
|
|
27
|
+
params: {
|
|
28
|
+
mode: { enum: ["stage0_definition_coverage", "stage1_requirements_scope", "table_to_table_trace"] },
|
|
29
|
+
"source_col?": S, "target_table?": S, "target_col?": S,
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
version_locked_anchor: {
|
|
33
|
+
group: "C1",
|
|
34
|
+
params: { upstream_stage: S, check_stage0_anchor: B, check_claimed_values: B, "check_q_numbers?": B, "skip_tables?": SL },
|
|
35
|
+
},
|
|
36
|
+
extension_reader_citation: {
|
|
37
|
+
group: "C1", params: { upstream_stages: SL, "exclude_sections?": SL, "allow_row_id_for_column?": B },
|
|
38
|
+
},
|
|
39
|
+
quote_on_line: {
|
|
40
|
+
group: "C2",
|
|
41
|
+
params: {
|
|
42
|
+
call_col: S, quote_col: S, "doc_col?": S, "page_col?": S, "line_col?": S, "value_col?": S, "unit_col?": S,
|
|
43
|
+
"max_words?": I, "strip_stage_prefix?": B, "when?": WHEN,
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
token_whole_in_quote: {
|
|
47
|
+
group: "C2", params: { target_col: S, quote_col: S, "forbid_quote_equals_value?": B, "when?": WHEN },
|
|
48
|
+
},
|
|
49
|
+
token_provenance: {
|
|
50
|
+
group: "C2",
|
|
51
|
+
params: {
|
|
52
|
+
target_col: S, quote_col: S, value_col: S, call_col: S, "include_table_header?": B,
|
|
53
|
+
"strip_stage_names?": B, "strip_id_tokens?": B, "when?": WHEN,
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
definition_sentence_coverage: {
|
|
57
|
+
group: "C2",
|
|
58
|
+
params: {
|
|
59
|
+
req_table: S, req_line_col: S, req_quote_col: S, qty_table: S, qty_line_col: S, qty_written_col: S,
|
|
60
|
+
qty_tol_col: S, not_qty_table: S, not_qty_line_col: S, not_qty_token_col: S,
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
page_span_coverage: {
|
|
64
|
+
group: "C2",
|
|
65
|
+
params: {
|
|
66
|
+
calls_table: S, "lists_table?": S, "file_col?": S, "total_rows_col?": S, "call_ids_col?": S,
|
|
67
|
+
"parts_table?": S, "facts_table?": S, "fact_call_col?": S, "source_kind_ids?": SL,
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
harvest_reply_rows: {
|
|
71
|
+
group: "C2", params: { target_table: S, target_col: S, lists_table: S, calls_table: S, "ref_set_name?": S },
|
|
72
|
+
},
|
|
73
|
+
concept_method_mapping_coverage: { group: "C2", params: { concepts_table: S, methods_Col: S, stage2_Ref_Col: S } },
|
|
74
|
+
Cross_Column_Predicate: {
|
|
75
|
+
group: "C2",
|
|
76
|
+
params: {
|
|
77
|
+
when: { obj: { col: S, op: { enum: ["eq", "ne", "in", "not_in", "empty", "non_empty"] }, "val?": "any" } },
|
|
78
|
+
then: { list: { obj: { col: S, op: S, "val?": "any" } } },
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
json_arg_schema: {
|
|
82
|
+
group: "C2",
|
|
83
|
+
params: {
|
|
84
|
+
args_col: S, "tool_col?": S, "forbid_helper_path?": B,
|
|
85
|
+
"doc_kind_rules?": { list: { obj: { tool: S, doc_kind: S } } },
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
conditional_source_rule: {
|
|
89
|
+
group: "C2",
|
|
90
|
+
params: {
|
|
91
|
+
kind_column: S, extracted_value: S, premise_value: S, call_column: S, calls_table: S, quote_column: S,
|
|
92
|
+
doc_kind_column: S, basis_column: S, premise_target_table: S, "tool_doc_kind_map?": { map: S },
|
|
93
|
+
"forbidden_substrings?": SL, "calc_column?": S, "documented_value?": S, "factor_value?": S, "author_value?": S,
|
|
94
|
+
},
|
|
95
|
+
},
|
|
96
|
+
source_priority_order: {
|
|
97
|
+
group: "C2", params: { facts_list_col: S, chosen_col: S, "reason_col?": S, facts_table: S, doc_kind_col: S },
|
|
98
|
+
},
|
|
99
|
+
ordered_range: { group: "C2", params: { min_col: S, "typ_col?": S, max_col: S, "allow_na?": B } },
|
|
100
|
+
unit_in_quote: { group: "C3", params: { unit_col: S, quote_col: S, "when?": WHEN } },
|
|
101
|
+
unit_missing_in_quote: { group: "C3", params: { value_cols: SL, unit_col: S, quote_col: S, "when?": WHEN } },
|
|
102
|
+
unit_convert_or_factor: {
|
|
103
|
+
group: "C3",
|
|
104
|
+
params: {
|
|
105
|
+
expr_col: S, "unit_col?": S, calls_table: S, "refs_col?": S, "rel_tol?": S,
|
|
106
|
+
"require_as_factor?": B, "note_if_no_call?": B,
|
|
107
|
+
},
|
|
108
|
+
},
|
|
109
|
+
tolerance_bounds: {
|
|
110
|
+
group: "C3",
|
|
111
|
+
params: { value_col: S, tolerance_col: S, lower_col: S, upper_col: S, bound_type_col: S, unit_col: S },
|
|
112
|
+
},
|
|
113
|
+
register_layout_and_decode: {
|
|
114
|
+
group: "C3",
|
|
115
|
+
params: { registers_table: S, fields_table: S, "ops_table?": S, calls_table: S, "mcu_exclude_table?": S },
|
|
116
|
+
},
|
|
117
|
+
unit_spelling: { group: "C3", params: null },
|
|
118
|
+
value_requires_unit: { group: "C3", params: null },
|
|
119
|
+
calc_fraction_eval: {
|
|
120
|
+
group: "C4",
|
|
121
|
+
params: {
|
|
122
|
+
calc_col: S, value_col: S, "unit_col?": S, ref_tables: SL, "refs_col?": S,
|
|
123
|
+
"mode?": { enum: ["calc_body", "frame_calc", "author_timing"] },
|
|
124
|
+
"forbid_id_in_exponent?": B, "require_unit_convert_call?": B,
|
|
125
|
+
},
|
|
126
|
+
},
|
|
127
|
+
math_calculate_verify: {
|
|
128
|
+
group: "C4", params: { expr_col: S, result_col: S, calls_table: S, "q_id_col?": S, "f_ids_col?": S },
|
|
129
|
+
},
|
|
130
|
+
written_equals: { group: "C4", params: { expr_col: S, written_col: S, "min_sig_digits?": I } },
|
|
131
|
+
margin_sign_agrees: { group: "C4", params: { result_col: S, verdict_col: S } },
|
|
132
|
+
oracle_eval_and_vectors: {
|
|
133
|
+
group: "C4",
|
|
134
|
+
params: {
|
|
135
|
+
variables_table: S, formulas_table: S, methods_table: S, arguments_table: S,
|
|
136
|
+
output_fields_table: S, line_grammar_table: S, vectors_table: S,
|
|
137
|
+
},
|
|
138
|
+
},
|
|
139
|
+
line_grammar_and_width: {
|
|
140
|
+
group: "C4", params: { output_fields_table: S, line_grammar_table: S, variables_table: S, formulas_table: S },
|
|
141
|
+
},
|
|
142
|
+
custom_predicate: { group: "C4", params: { hook_name: S, max_rule_fraction: { literal: "0.10" } } },
|
|
143
|
+
acceptance_and_claim_provenance: {
|
|
144
|
+
group: "C5",
|
|
145
|
+
params: {
|
|
146
|
+
requirements_table: S, "scenarios_table?": S, questions_table: S,
|
|
147
|
+
upstream_quantities_table: S, "upstream_reqs_table?": S,
|
|
148
|
+
},
|
|
149
|
+
},
|
|
150
|
+
checklist_floor: {
|
|
151
|
+
group: "C5",
|
|
152
|
+
params: { checklist_csv: S, due_stage: I, coverage_table: S, item_col: S, status_col: S, evidence_col: S },
|
|
153
|
+
},
|
|
154
|
+
no_device_bound_guard: {
|
|
155
|
+
group: "C5", params: { feasibility_table: S, verdict_col: S, margin_col: S, facts_table: S },
|
|
156
|
+
},
|
|
157
|
+
owner_item_closing: {
|
|
158
|
+
group: "C5", params: { closings_table: S, item_col: S, owner_stage: I, calls_table: S, facts_table: S },
|
|
159
|
+
},
|
|
160
|
+
free_text_token_provenance: {
|
|
161
|
+
group: "C5",
|
|
162
|
+
params: {
|
|
163
|
+
target_cols: SL, refs_col: S, "mode?": { enum: ["all_numbers", "hex_only", "whole_or_number"] },
|
|
164
|
+
"allow_small_ints?": { list: I },
|
|
165
|
+
},
|
|
166
|
+
},
|
|
167
|
+
output_replacement_guard: { group: "C5", params: { named_by_col: S, definition_file: S, upstream_reqs_table: S } },
|
|
168
|
+
state_machine_exits: {
|
|
169
|
+
group: "C5", params: { conditions_table: S, behaviour_table: S, error_table: S, line_grammar_table: S },
|
|
170
|
+
},
|
|
171
|
+
critic_round_present: {
|
|
172
|
+
group: "C5", params: { critic_table: S, reply_file_pattern: S, point_col: S, status_col: S, reason_col: S },
|
|
173
|
+
},
|
|
174
|
+
card_prose_conformance: { group: "C5", params: { card_glob: S, forbidden_Import_Tokens: SL } },
|
|
175
|
+
helper_file_cap: {
|
|
176
|
+
group: "C5", params: { max_files: I, dir: S, problems_table: S, detected_col: S, row_prefix: S },
|
|
177
|
+
},
|
|
178
|
+
};
|
|
179
|
+
/** Primitives that need `contract.unit_table` (C3 unit rules, spec §3.1). */
|
|
180
|
+
export const NEEDS_UNIT_TABLE = new Set([
|
|
181
|
+
"unit_in_quote", "unit_missing_in_quote", "unit_convert_or_factor", "tolerance_bounds", "unit_spelling",
|
|
182
|
+
"value_requires_unit",
|
|
183
|
+
]);
|
|
184
|
+
/** Primitives that need `contract.source_order` (K5 priority, spec §3.1). */
|
|
185
|
+
export const NEEDS_SOURCE_ORDER = new Set(["source_priority_order"]);
|
|
186
|
+
/**
|
|
187
|
+
* What each table/column param of a primitive names, so contract_check can
|
|
188
|
+
* resolve it: `"table"` = a table of this contract; `"self"` = a column of
|
|
189
|
+
* the rule's own table; any other value = a column of the table named by
|
|
190
|
+
* that param. Params not listed (upstream tables, files, CSV aliases, nested
|
|
191
|
+
* objects) are not resolved here; foreign_key has its own check.
|
|
192
|
+
*/
|
|
193
|
+
export const PARAM_REFS = {
|
|
194
|
+
id_pattern: { id_col: "self" },
|
|
195
|
+
unique_id: { id_col: "self" },
|
|
196
|
+
allowed_values: { column: "self" },
|
|
197
|
+
non_empty: { columns: "self" },
|
|
198
|
+
conditional_non_dash: { when_column: "self", target_column: "self" },
|
|
199
|
+
sentinel_row: { id_col: "self" },
|
|
200
|
+
scoped_id_trace: { source_col: "self" },
|
|
201
|
+
quote_on_line: {
|
|
202
|
+
call_col: "self", quote_col: "self", doc_col: "self", page_col: "self", line_col: "self",
|
|
203
|
+
value_col: "self", unit_col: "self",
|
|
204
|
+
},
|
|
205
|
+
token_whole_in_quote: { target_col: "self", quote_col: "self" },
|
|
206
|
+
token_provenance: { target_col: "self", quote_col: "self", value_col: "self", call_col: "self" },
|
|
207
|
+
definition_sentence_coverage: {
|
|
208
|
+
req_table: "table", req_line_col: "req_table", req_quote_col: "req_table",
|
|
209
|
+
qty_table: "table", qty_line_col: "qty_table", qty_written_col: "qty_table", qty_tol_col: "qty_table",
|
|
210
|
+
not_qty_table: "table", not_qty_line_col: "not_qty_table", not_qty_token_col: "not_qty_table",
|
|
211
|
+
},
|
|
212
|
+
page_span_coverage: {
|
|
213
|
+
calls_table: "table", lists_table: "table", file_col: "lists_table", total_rows_col: "lists_table",
|
|
214
|
+
call_ids_col: "lists_table", parts_table: "table", facts_table: "table", fact_call_col: "facts_table",
|
|
215
|
+
},
|
|
216
|
+
harvest_reply_rows: { target_table: "table", target_col: "target_table", lists_table: "table", calls_table: "table" },
|
|
217
|
+
concept_method_mapping_coverage: { concepts_table: "table" },
|
|
218
|
+
json_arg_schema: { args_col: "self", tool_col: "self" },
|
|
219
|
+
conditional_source_rule: {
|
|
220
|
+
kind_column: "self", call_column: "self", quote_column: "self", doc_kind_column: "self", basis_column: "self",
|
|
221
|
+
calc_column: "self", calls_table: "table", premise_target_table: "table",
|
|
222
|
+
},
|
|
223
|
+
source_priority_order: {
|
|
224
|
+
facts_list_col: "self", chosen_col: "self", reason_col: "self", facts_table: "table", doc_kind_col: "facts_table",
|
|
225
|
+
},
|
|
226
|
+
ordered_range: { min_col: "self", typ_col: "self", max_col: "self" },
|
|
227
|
+
unit_in_quote: { unit_col: "self", quote_col: "self" },
|
|
228
|
+
unit_missing_in_quote: { value_cols: "self", unit_col: "self", quote_col: "self" },
|
|
229
|
+
unit_convert_or_factor: { expr_col: "self", unit_col: "self", refs_col: "self", calls_table: "table" },
|
|
230
|
+
tolerance_bounds: {
|
|
231
|
+
value_col: "self", tolerance_col: "self", lower_col: "self", upper_col: "self", bound_type_col: "self",
|
|
232
|
+
unit_col: "self",
|
|
233
|
+
},
|
|
234
|
+
register_layout_and_decode: {
|
|
235
|
+
registers_table: "table", fields_table: "table", ops_table: "table", calls_table: "table",
|
|
236
|
+
mcu_exclude_table: "table",
|
|
237
|
+
},
|
|
238
|
+
calc_fraction_eval: { calc_col: "self", value_col: "self", unit_col: "self", refs_col: "self" },
|
|
239
|
+
math_calculate_verify: {
|
|
240
|
+
expr_col: "self", result_col: "self", q_id_col: "self", f_ids_col: "self", calls_table: "table",
|
|
241
|
+
},
|
|
242
|
+
written_equals: { expr_col: "self", written_col: "self" },
|
|
243
|
+
margin_sign_agrees: { result_col: "self", verdict_col: "self" },
|
|
244
|
+
oracle_eval_and_vectors: {
|
|
245
|
+
variables_table: "table", formulas_table: "table", methods_table: "table", arguments_table: "table",
|
|
246
|
+
output_fields_table: "table", line_grammar_table: "table", vectors_table: "table",
|
|
247
|
+
},
|
|
248
|
+
line_grammar_and_width: {
|
|
249
|
+
output_fields_table: "table", line_grammar_table: "table", variables_table: "table", formulas_table: "table",
|
|
250
|
+
},
|
|
251
|
+
acceptance_and_claim_provenance: { requirements_table: "table", scenarios_table: "table", questions_table: "table" },
|
|
252
|
+
checklist_floor: {
|
|
253
|
+
coverage_table: "table", item_col: "coverage_table", status_col: "coverage_table", evidence_col: "coverage_table",
|
|
254
|
+
},
|
|
255
|
+
no_device_bound_guard: {
|
|
256
|
+
feasibility_table: "table", facts_table: "table", verdict_col: "feasibility_table", margin_col: "feasibility_table",
|
|
257
|
+
},
|
|
258
|
+
owner_item_closing: { closings_table: "table", calls_table: "table", facts_table: "table", item_col: "closings_table" },
|
|
259
|
+
free_text_token_provenance: { target_cols: "self", refs_col: "self" },
|
|
260
|
+
output_replacement_guard: { named_by_col: "self" },
|
|
261
|
+
state_machine_exits: {
|
|
262
|
+
conditions_table: "table", behaviour_table: "table", error_table: "table", line_grammar_table: "table",
|
|
263
|
+
},
|
|
264
|
+
critic_round_present: {
|
|
265
|
+
critic_table: "table", point_col: "critic_table", status_col: "critic_table", reason_col: "critic_table",
|
|
266
|
+
},
|
|
267
|
+
helper_file_cap: { problems_table: "table", detected_col: "problems_table" },
|
|
268
|
+
};
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { contractDiag, show } from "./diagnostics.js";
|
|
2
|
+
const VERSION = /^\d+\.\d+\.\d+$/;
|
|
3
|
+
const PIN = /^[^@\s]+@\d+\.\d+\.\d+$/;
|
|
4
|
+
/** @param {unknown} v - Value. @returns {string} Its JSON type name. */
|
|
5
|
+
const typeOf = (v) => (v === null ? "null" : Array.isArray(v) ? "array" : typeof v);
|
|
6
|
+
/**
|
|
7
|
+
* Compiles a contract regex.
|
|
8
|
+
*
|
|
9
|
+
* @param {string} pattern - The pattern.
|
|
10
|
+
* @returns {RegExp | string} The regex, or the error message.
|
|
11
|
+
*/
|
|
12
|
+
export function compileRegex(pattern) {
|
|
13
|
+
try {
|
|
14
|
+
return new RegExp(pattern);
|
|
15
|
+
}
|
|
16
|
+
catch (e) {
|
|
17
|
+
return e.message;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/** @param {Shape} shape - A shape. @returns {string} A short description for `expected`. */
|
|
21
|
+
function describe(shape) {
|
|
22
|
+
if (typeof shape === "string") {
|
|
23
|
+
return shape === "pin" ? "<contract_id>@<x.y.z>" : shape === "version" ? "x.y.z" : shape;
|
|
24
|
+
}
|
|
25
|
+
if ("enum" in shape)
|
|
26
|
+
return `one of ${shape.enum.join(", ")}`;
|
|
27
|
+
if ("literal" in shape)
|
|
28
|
+
return JSON.stringify(shape.literal);
|
|
29
|
+
if ("list" in shape)
|
|
30
|
+
return `array of ${describe(shape.list)}`;
|
|
31
|
+
if ("map" in shape)
|
|
32
|
+
return `object of ${describe(shape.map)}`;
|
|
33
|
+
if ("nullable" in shape)
|
|
34
|
+
return `${describe(shape.nullable)} or null`;
|
|
35
|
+
return "object";
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Checks a JSON value against a shape and appends a diagnostic for every mismatch.
|
|
39
|
+
*
|
|
40
|
+
* @param {unknown} value - The value.
|
|
41
|
+
* @param {Shape} shape - Its shape.
|
|
42
|
+
* @param {string} path - JSON path for messages, e.g. `tables[0].name`.
|
|
43
|
+
* @param {Where} at - Table / rule the value belongs to.
|
|
44
|
+
* @param {Diagnostic[]} out - Receives the diagnostics.
|
|
45
|
+
* @param {string} [code="CONTRACT.BAD_TYPE"] - Code used for type mismatches.
|
|
46
|
+
* @returns {boolean} Whether the value matched without any diagnostic.
|
|
47
|
+
*/
|
|
48
|
+
export function checkShape(value, shape, path, at, out, code = "CONTRACT.BAD_TYPE") {
|
|
49
|
+
const before = out.length;
|
|
50
|
+
const where = { ...at, column: path };
|
|
51
|
+
const fail = (c, msg, expected = describe(shape)) => out.push(contractDiag(c, `${path}: ${msg}`, where, show(value), expected));
|
|
52
|
+
if (typeof shape === "string") {
|
|
53
|
+
switch (shape) {
|
|
54
|
+
case "any":
|
|
55
|
+
break;
|
|
56
|
+
case "string":
|
|
57
|
+
if (typeof value !== "string" || value === "")
|
|
58
|
+
fail(code, `expected a non-empty string, got ${typeOf(value)}`);
|
|
59
|
+
break;
|
|
60
|
+
case "bool":
|
|
61
|
+
if (typeof value !== "boolean")
|
|
62
|
+
fail(code, `expected a boolean, got ${typeOf(value)}`);
|
|
63
|
+
break;
|
|
64
|
+
case "int":
|
|
65
|
+
case "pos_int":
|
|
66
|
+
if (!Number.isInteger(value) || value < (shape === "int" ? 0 : 1)) {
|
|
67
|
+
fail(code, `expected a ${shape === "int" ? "non-negative" : "positive"} integer`);
|
|
68
|
+
}
|
|
69
|
+
break;
|
|
70
|
+
case "regex": {
|
|
71
|
+
const compiled = typeof value === "string" ? compileRegex(value) : "not a string";
|
|
72
|
+
if (typeof compiled === "string")
|
|
73
|
+
fail("CONTRACT.BAD_REGEX", `invalid regex (${compiled})`);
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
case "version":
|
|
77
|
+
case "pin":
|
|
78
|
+
if (typeof value !== "string" || !(shape === "pin" ? PIN : VERSION).test(value)) {
|
|
79
|
+
fail(typeof value === "string" ? "CONTRACT.BAD_VALUE" : code, "bad version format");
|
|
80
|
+
}
|
|
81
|
+
break;
|
|
82
|
+
case "string_or_list":
|
|
83
|
+
if (typeof value !== "string" && !(Array.isArray(value) && value.every(v => typeof v === "string"))) {
|
|
84
|
+
fail(code, "expected a string or an array of strings");
|
|
85
|
+
}
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
else if ("enum" in shape) {
|
|
90
|
+
if (typeof value !== "string" || !shape.enum.includes(value)) {
|
|
91
|
+
fail(typeof value === "string" ? "CONTRACT.BAD_VALUE" : code, "value not allowed");
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
else if ("literal" in shape) {
|
|
95
|
+
if (value !== shape.literal)
|
|
96
|
+
fail("CONTRACT.BAD_VALUE", "value not allowed");
|
|
97
|
+
}
|
|
98
|
+
else if ("nullable" in shape) {
|
|
99
|
+
if (value !== null)
|
|
100
|
+
checkShape(value, shape.nullable, path, at, out, code);
|
|
101
|
+
}
|
|
102
|
+
else if ("list" in shape) {
|
|
103
|
+
if (!Array.isArray(value)) {
|
|
104
|
+
fail(code, `expected an array, got ${typeOf(value)}`);
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
if (value.length < (shape.min ?? 0))
|
|
108
|
+
fail("CONTRACT.BAD_VALUE", `needs at least ${shape.min} entries`);
|
|
109
|
+
value.forEach((v, i) => checkShape(v, shape.list, `${path}[${i}]`, at, out, code));
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
else if (typeOf(value) !== "object") {
|
|
113
|
+
fail(code, `expected an object, got ${typeOf(value)}`);
|
|
114
|
+
}
|
|
115
|
+
else if ("map" in shape) {
|
|
116
|
+
for (const [k, v] of Object.entries(value))
|
|
117
|
+
checkShape(v, shape.map, `${path}.${k}`, at, out, code);
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
const obj = value;
|
|
121
|
+
const keys = new Map(Object.keys(shape.obj).map(k => [k.replace(/\?$/, ""), k]));
|
|
122
|
+
for (const [name, key] of keys) {
|
|
123
|
+
if (obj[name] === undefined) {
|
|
124
|
+
if (!key.endsWith("?")) {
|
|
125
|
+
out.push(contractDiag(code === "CONTRACT.BAD_TYPE" ? "CONTRACT.MISSING_FIELD" : code, `${path}: missing required '${name}'`, { ...at, column: `${path}.${name}` }, "(absent)", describe(shape.obj[key])));
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
checkShape(obj[name], shape.obj[key], `${path}.${name}`, at, out, code);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
for (const k of Object.keys(obj).filter(k => !keys.has(k))) {
|
|
133
|
+
out.push(contractDiag(code === "CONTRACT.BAD_TYPE" ? "CONTRACT.UNKNOWN_FIELD" : code, `${path}: unknown field '${k}'`, { ...at, column: `${path}.${k}` }, show(obj[k]), `one of ${[...keys.keys()].join(", ")}`));
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return out.length === before;
|
|
137
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { contractDiag } from "./diagnostics.js";
|
|
2
|
+
import { checkShape } from "./shape.js";
|
|
3
|
+
const REGISTRY = {
|
|
4
|
+
obj: {
|
|
5
|
+
source_kinds_version: "version",
|
|
6
|
+
kinds: {
|
|
7
|
+
list: {
|
|
8
|
+
obj: {
|
|
9
|
+
kind_id: "string",
|
|
10
|
+
filename_pattern: "string",
|
|
11
|
+
doc_kind: "string",
|
|
12
|
+
trigger_mode: { enum: ["per_extracted_part", "when_page_cited"] },
|
|
13
|
+
read_mode: { enum: ["all_pages", "by_row_id"] },
|
|
14
|
+
populates_ref_set: { nullable: { obj: { ref_set_name: "string", id_column_regex: "regex" } } },
|
|
15
|
+
},
|
|
16
|
+
},
|
|
17
|
+
min: 1,
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Validates a `source_kinds` registry.
|
|
23
|
+
*
|
|
24
|
+
* @param {unknown} data - Parsed registry JSON.
|
|
25
|
+
* @returns {Diagnostic[]} Empty when the registry is valid.
|
|
26
|
+
*/
|
|
27
|
+
export function validateSourceKinds(data) {
|
|
28
|
+
const out = [];
|
|
29
|
+
const at = { table: "source_kinds" };
|
|
30
|
+
if (!checkShape(data, REGISTRY, "$", at, out)) {
|
|
31
|
+
return out;
|
|
32
|
+
}
|
|
33
|
+
const seen = new Set();
|
|
34
|
+
for (const kind of data.kinds) {
|
|
35
|
+
if (seen.has(kind.kind_id)) {
|
|
36
|
+
out.push(contractDiag("CONTRACT.DUPLICATE", `duplicate kind_id '${kind.kind_id}'`, at, kind.kind_id, "unique kind_id"));
|
|
37
|
+
}
|
|
38
|
+
seen.add(kind.kind_id);
|
|
39
|
+
}
|
|
40
|
+
return out;
|
|
41
|
+
}
|