@turingcode/cli 0.2.13 → 0.3.4

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 (177) hide show
  1. package/README.md +2 -0
  2. package/dist/cli/create-cli.d.ts +10 -0
  3. package/dist/cli/create-cli.js +12 -1
  4. package/dist/cli/index.d.ts +9 -0
  5. package/dist/cli/index.js +5 -1
  6. package/dist/core/bound-error.d.ts +10 -0
  7. package/dist/core/date.d.ts +12 -0
  8. package/dist/core/exact/expression.d.ts +49 -0
  9. package/dist/core/exact/fraction.d.ts +85 -0
  10. package/dist/core/files.d.ts +44 -0
  11. package/dist/core/files.js +46 -1
  12. package/dist/core/json-shape.d.ts +45 -0
  13. package/dist/core/mcp.d.ts +92 -0
  14. package/dist/core/mcp.js +56 -15
  15. package/dist/core/query.d.ts +69 -0
  16. package/dist/core/report.d.ts +11 -0
  17. package/dist/core/tables/csv.d.ts +18 -0
  18. package/dist/core/tables/markdown.d.ts +22 -0
  19. package/dist/core/tables/types.d.ts +25 -0
  20. package/dist/features/contract/check.d.ts +30 -0
  21. package/dist/features/contract/command.d.ts +12 -0
  22. package/dist/features/contract/command.js +64 -64
  23. package/dist/features/contract/describe.d.ts +22 -0
  24. package/dist/features/contract/diagnostics.d.ts +108 -0
  25. package/dist/features/contract/load.d.ts +58 -0
  26. package/dist/features/contract/model.d.ts +67 -0
  27. package/dist/features/contract/rules.d.ts +137 -0
  28. package/dist/features/debug/clear_breakpoint.d.ts +2 -0
  29. package/dist/features/debug/clear_breakpoint.js +24 -0
  30. package/dist/features/debug/command.d.ts +5 -0
  31. package/dist/features/debug/command.js +66 -0
  32. package/dist/features/debug/continue.d.ts +2 -0
  33. package/dist/features/debug/continue.js +22 -0
  34. package/dist/features/debug/debug_start.d.ts +2 -0
  35. package/dist/features/debug/debug_start.js +22 -0
  36. package/dist/features/debug/debug_stop.d.ts +2 -0
  37. package/dist/features/debug/debug_stop.js +11 -0
  38. package/dist/features/debug/evaluate.d.ts +2 -0
  39. package/dist/features/debug/evaluate.js +48 -0
  40. package/dist/features/debug/get_stack.d.ts +2 -0
  41. package/dist/features/debug/get_stack.js +14 -0
  42. package/dist/features/debug/get_variables.d.ts +2 -0
  43. package/dist/features/debug/get_variables.js +26 -0
  44. package/dist/features/debug/halt.d.ts +2 -0
  45. package/dist/features/debug/halt.js +16 -0
  46. package/dist/features/debug/set_breakpoint.d.ts +2 -0
  47. package/dist/features/debug/set_breakpoint.js +30 -0
  48. package/dist/features/debug/step.d.ts +2 -0
  49. package/dist/features/debug/step.js +23 -0
  50. package/dist/features/install-mods/command.d.ts +6 -0
  51. package/dist/features/install-mods/command.js +18 -0
  52. package/dist/features/install-mods/install.d.ts +23 -0
  53. package/dist/features/install-mods/install.js +48 -0
  54. package/dist/features/install-skills/command.d.ts +7 -0
  55. package/dist/features/install-skills/command.js +22 -0
  56. package/dist/features/install-skills/install.d.ts +13 -0
  57. package/dist/features/install-skills/install.js +29 -0
  58. package/dist/features/probe/command.d.ts +5 -0
  59. package/dist/features/probe/command.js +44 -0
  60. package/dist/features/probe/kill_all_debuggers.d.ts +2 -0
  61. package/dist/features/probe/kill_all_debuggers.js +50 -0
  62. package/dist/features/probe/list_probes.d.ts +2 -0
  63. package/dist/features/probe/list_probes.js +8 -0
  64. package/dist/features/probe/program_project.d.ts +2 -0
  65. package/dist/features/probe/program_project.js +29 -0
  66. package/dist/features/probe/read_fault_status.d.ts +2 -0
  67. package/dist/features/probe/read_fault_status.js +19 -0
  68. package/dist/features/probe/read_memory.d.ts +2 -0
  69. package/dist/features/probe/read_memory.js +36 -0
  70. package/dist/features/probe/read_peripheral.d.ts +2 -0
  71. package/dist/features/probe/read_peripheral.js +58 -0
  72. package/dist/features/probe/reset_probe_usb.d.ts +2 -0
  73. package/dist/features/probe/reset_probe_usb.js +17 -0
  74. package/dist/features/probe/reset_target.d.ts +2 -0
  75. package/dist/features/probe/reset_target.js +16 -0
  76. package/dist/features/probe/target_info.d.ts +2 -0
  77. package/dist/features/probe/target_info.js +34 -0
  78. package/dist/features/project/analysis_static.d.ts +2 -0
  79. package/dist/features/project/analysis_static.js +19 -0
  80. package/dist/features/project/analysis_syntax_check.d.ts +2 -0
  81. package/dist/features/project/analysis_syntax_check.js +21 -0
  82. package/dist/features/project/command.d.ts +5 -0
  83. package/dist/features/project/command.js +64 -0
  84. package/dist/features/project/project_build.d.ts +2 -0
  85. package/dist/features/project/project_build.js +22 -0
  86. package/dist/features/project/project_download_template.d.ts +2 -0
  87. package/dist/features/project/project_download_template.js +34 -0
  88. package/dist/features/project/project_regenerate.d.ts +2 -0
  89. package/dist/features/project/project_regenerate.js +20 -0
  90. package/dist/features/project/project_sync_from_server.d.ts +2 -0
  91. package/dist/features/project/project_sync_from_server.js +13 -0
  92. package/dist/features/project/project_sync_to_server.d.ts +2 -0
  93. package/dist/features/project/project_sync_to_server.js +51 -0
  94. package/dist/features/project/stm32_cubetools_download_drivers.d.ts +2 -0
  95. package/dist/features/project/stm32_cubetools_download_drivers.js +19 -0
  96. package/dist/features/status/command.d.ts +2 -0
  97. package/dist/features/status/command.js +6 -0
  98. package/dist/features/status/get_status.d.ts +2 -0
  99. package/dist/features/status/get_status.js +44 -0
  100. package/dist/features/table/check.d.ts +38 -0
  101. package/dist/features/table/command.d.ts +3 -0
  102. package/dist/features/table/read.d.ts +28 -0
  103. package/dist/features/tool/command.d.ts +35 -0
  104. package/dist/lib.d.ts +11 -0
  105. package/dist/lib.js +9 -0
  106. package/dist/stm32/actions/DebugAction.d.ts +63 -0
  107. package/dist/stm32/actions/DebugAction.js +105 -0
  108. package/dist/stm32/actions/DebugActionInfo.d.ts +7 -0
  109. package/dist/stm32/actions/DebugActionInfo.js +9 -0
  110. package/dist/stm32/actions/DebugActionListProbes.d.ts +9 -0
  111. package/dist/stm32/actions/DebugActionListProbes.js +10 -0
  112. package/dist/stm32/actions/DebugActionProgram.d.ts +28 -0
  113. package/dist/stm32/actions/DebugActionProgram.js +79 -0
  114. package/dist/stm32/actions/DebugActionRead.d.ts +38 -0
  115. package/dist/stm32/actions/DebugActionRead.js +59 -0
  116. package/dist/stm32/actions/DebugActionReset.d.ts +11 -0
  117. package/dist/stm32/actions/DebugActionReset.js +9 -0
  118. package/dist/stm32/actions/DebugActionServer.d.ts +7 -0
  119. package/dist/stm32/actions/DebugActionServer.js +9 -0
  120. package/dist/stm32/analysis.d.ts +19 -0
  121. package/dist/stm32/analysis.js +21 -0
  122. package/dist/stm32/build.d.ts +34 -0
  123. package/dist/stm32/build.js +79 -0
  124. package/dist/stm32/context.d.ts +23 -0
  125. package/dist/stm32/context.js +42 -0
  126. package/dist/stm32/daemon.d.ts +63 -0
  127. package/dist/stm32/daemon.js +202 -0
  128. package/dist/stm32/regen.d.ts +68 -0
  129. package/dist/stm32/regen.js +225 -0
  130. package/dist/stm32/session.d.ts +134 -0
  131. package/dist/stm32/session.js +374 -0
  132. package/dist/stm32/sync.d.ts +24 -0
  133. package/dist/stm32/sync.js +72 -0
  134. package/dist/stm32/tool.d.ts +43 -0
  135. package/dist/stm32/tool.js +20 -0
  136. package/dist/stm32/toolCommand.d.ts +50 -0
  137. package/dist/stm32/toolCommand.js +102 -0
  138. package/dist/stm32/utils/discover_tools.d.ts +24 -0
  139. package/dist/stm32/utils/discover_tools.js +121 -0
  140. package/dist/stm32/utils/downloadzip_utils.d.ts +57 -0
  141. package/dist/stm32/utils/downloadzip_utils.js +180 -0
  142. package/dist/stm32/utils/faultUtils.d.ts +14 -0
  143. package/dist/stm32/utils/faultUtils.js +69 -0
  144. package/dist/stm32/utils/findElf.d.ts +9 -0
  145. package/dist/stm32/utils/findElf.js +19 -0
  146. package/dist/stm32/utils/hash_utils.d.ts +19 -0
  147. package/dist/stm32/utils/hash_utils.js +28 -0
  148. package/dist/stm32/utils/listSyncFiles.d.ts +9 -0
  149. package/dist/stm32/utils/listSyncFiles.js +98 -0
  150. package/dist/stm32/utils/mcu_utils.d.ts +59 -0
  151. package/dist/stm32/utils/mcu_utils.js +99 -0
  152. package/dist/stm32/utils/project_utils.d.ts +54 -0
  153. package/dist/stm32/utils/project_utils.js +102 -0
  154. package/dist/stm32/utils/svdUtils.d.ts +31 -0
  155. package/dist/stm32/utils/svdUtils.js +30 -0
  156. package/dist/stm32/utils/truncate.d.ts +7 -0
  157. package/dist/stm32/utils/truncate.js +9 -0
  158. package/dist/stm32/utils/usb_utils.d.ts +5 -0
  159. package/dist/stm32/utils/usb_utils.js +23 -0
  160. package/dist/stm32/utils/user_code.d.ts +14 -0
  161. package/dist/stm32/utils/user_code.js +43 -0
  162. package/dist/stm32/utils/utils_mutex.d.ts +15 -0
  163. package/dist/stm32/utils/utils_mutex.js +93 -0
  164. package/dist/stm32/validate.d.ts +16 -0
  165. package/dist/stm32/validate.js +46 -0
  166. package/mods/claude/.claude-plugin/marketplace.json +8 -0
  167. package/mods/claude/turingcode-skills/.claude-plugin/plugin.json +5 -0
  168. package/mods/claude/turingcode-skills/skills/turingcode-cli/SKILL.md +50 -0
  169. package/mods/claude/turingcode-skills/skills/turingcode-cli/references/board-and-debug.md +22 -0
  170. package/mods/claude/turingcode-skills/skills/turingcode-cli/references/build-and-check.md +19 -0
  171. package/mods/claude/turingcode-skills/skills/turingcode-cli/references/new-project.md +9 -0
  172. package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/SKILL.md +43 -0
  173. package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/concurrency.md +42 -0
  174. package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/init-and-timing.md +35 -0
  175. package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/input-and-bounds.md +34 -0
  176. package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/registers-and-units.md +35 -0
  177. package/package.json +11 -3
@@ -0,0 +1,18 @@
1
+ import { TablesDoc } from "./types.js";
2
+ /**
3
+ * Parses CSV text (RFC 4180: quoted fields, doubled quotes, CRLF).
4
+ *
5
+ * @param {string} text - The CSV text.
6
+ * @returns {string[][]} Records; fields are not trimmed.
7
+ * @throws {Error} On an unterminated quoted field.
8
+ */
9
+ export declare function parseCsv(text: string): string[][];
10
+ /**
11
+ * Reads a folder of CSV tables: holding one `<Table>.csv` per table.
12
+ *
13
+ * @param {string} dir - The folder.
14
+ * @param {number} maxRows - `bounds.max_table_rows`.
15
+ * @returns {TablesDoc} The tables.
16
+ * @throws {BoundError} When a table has more than maxRows rows.
17
+ */
18
+ export declare function parseCsvFolder(dir: string, maxRows: number): TablesDoc;
@@ -0,0 +1,22 @@
1
+ import { TablesDoc } from "./types.js";
2
+ /**
3
+ * Splits one Markdown table line into trimmed cells; `\|` is a literal pipe.
4
+ *
5
+ * @param {string} line - A line starting with `|`.
6
+ * @returns {string[]} The cells.
7
+ *
8
+ * @example
9
+ * splitRow("| a | b \\| c |"); // ["a", "b | c"]
10
+ */
11
+ export declare function splitRow(line: string): string[];
12
+ /**
13
+ * Reads the tables of a Markdown document. A table is the first pipe
14
+ * table under a `## <Name>` heading; pipe tables inside fenced code blocks
15
+ * are ignored.
16
+ *
17
+ * @param {string} text - The Markdown text.
18
+ * @param {number} maxRows - `bounds.max_table_rows`.
19
+ * @returns {TablesDoc} The tables.
20
+ * @throws {BoundError} When a table has more than maxRows rows.
21
+ */
22
+ export declare function parseMarkdown(text: string, maxRows: number): TablesDoc;
@@ -0,0 +1,25 @@
1
+ /** One table read from a Markdown or CSV document. */
2
+ export interface ParsedTable {
3
+ name: string;
4
+ header: string[];
5
+ rows: string[][];
6
+ /** 1-based source line of each row, for messages. */
7
+ rowLines: number[];
8
+ }
9
+ /** A document of tables, plus the problems found while reading it. */
10
+ export interface TablesDoc {
11
+ tables: Map<string, ParsedTable>;
12
+ /** Table names that appear more than once. */
13
+ duplicates: string[];
14
+ /** `[table, message]` for tables whose layout could not be read. */
15
+ malformed: [string, string][];
16
+ /** Full text, for citation checks. */
17
+ text: string;
18
+ /**
19
+ * The text without the named tables (their header, separator and rows).
20
+ *
21
+ * @param {string[]} sections - Table names to leave out.
22
+ * @returns {string} The remaining text.
23
+ */
24
+ textOutside(sections: string[]): string;
25
+ }
@@ -0,0 +1,30 @@
1
+ import { Diagnostic } from "./diagnostics.js";
2
+ export interface ContractCheckResult {
3
+ status: "VALID" | "INVALID_CONTRACT" | "MISSING_DATA";
4
+ contract: string;
5
+ /** Labels of the contract and every dependency that was checked. */
6
+ checked: string[];
7
+ diagnostics: Diagnostic[];
8
+ }
9
+ /**
10
+ * contract check: validates a contract and the contracts it depends on.
11
+ *
12
+ * @param {string} path - Contract file.
13
+ * @returns {ContractCheckResult} Status and diagnostics.
14
+ *
15
+ * @example
16
+ * contractCheck("orders.contract.json").status; // "VALID"
17
+ */
18
+ export declare function contractCheck(path: string): ContractCheckResult;
19
+ /**
20
+ * Compact text for a contract check (or describe) result.
21
+ *
22
+ * @param {{status: string, contract: string, diagnostics: Diagnostic[], checked?: string[]}} r - The result.
23
+ * @returns {string} Status line, then the diagnostics.
24
+ */
25
+ export declare function formatContractCheck(r: {
26
+ status: string;
27
+ contract: string;
28
+ diagnostics: Diagnostic[];
29
+ checked?: string[];
30
+ }): string;
@@ -0,0 +1,12 @@
1
+ import { CommandModule } from "yargs";
2
+ export declare const CONTRACT: {
3
+ readonly type: "string";
4
+ readonly demandOption: true;
5
+ readonly describe: "Contract JSON (required)";
6
+ };
7
+ export declare const JSON_FLAG: {
8
+ readonly type: "boolean";
9
+ readonly describe: "Full result as one-line JSON";
10
+ };
11
+ declare const contract: CommandModule;
12
+ export default contract;
@@ -4,70 +4,70 @@ import { describeContract } from "./describe.js";
4
4
  export const CONTRACT = { type: "string", demandOption: true, describe: "Contract JSON (required)" };
5
5
  export const JSON_FLAG = { type: "boolean", describe: "Full result as one-line JSON" };
6
6
  /** The contract format in brief, for `contract --help`. Only tables, and each table's and column's name, are required. */
7
- const FORMAT = `Contract JSON. Only "tables" and each table's and column's "name" are required; every other
8
- field is optional. "(default)" marks the value used when a field is left out; other values are examples.
9
- {
10
- "name": "shop.orders", "version": "1.0.0", "description": "...", // shown in output
11
- "format": "markdown", // (default), or "format": "csv": one <table>.csv per table. The tables
12
- // given with --tables / --dep must be in their own contract's format
13
- "missing_values": [""], // (default) cell texts that count as empty (null in queries), e.g. ["", "-"].
14
- // Keep a marker like "-" out of it when "-" is a real value: "-" allowed
15
- // but an empty cell forbidden is "required": true plus
16
- // "match": {"$or": [{"$eq": "-"}, {"$every": {"$ref": "Tests.ID"}}]}
17
- "allow_extra_tables": false, // (default) true: tables not declared here are allowed
18
- "depends_on": { "customers": "customers.contract.json" }, // path absolute or relative to this contract;
19
- // its tables passed with --dep customers=<file>, referenced as "customers:Table.Column"
20
- "tables": [{
21
- "name": "Orders",
22
- "required": true, // (default) the table must be in the document
23
- "primary_key": "Order_ID", // or ["A", "B"]; always filled and unique, no "required" needed
24
- "ordered": false, // (default) true: columns must be in the declared order
25
- "allow_extra_columns": false, // (default) true: columns not declared here are allowed
26
- "columns": [{
27
- "name": "Total",
28
- "type": "number", // string (default) | integer | number | boolean | date (YYYY-MM-DD) | list
29
- "required": false, // (default) true: every cell must be filled (an empty cell fails
30
- // MISSING_VALUE); the column itself must always be in the header
31
- "separator": ",", // (default) list item separator
32
- "match": { "$gte": 0, "$lte": 100 } // condition every filled cell meets (MongoDB query);
33
- // empty cells are skipped: use "required" to forbid them
34
- }],
35
- "rules": [{ // exactly one of require, count
36
- "id": "...", "description": "...", // shown in findings; description replaces the "expected" text
37
- "severity": "error", // (default) or "warning": reported as WARN, does not fail the check
38
- "on": "Table", // or "alias:Table"; default: the table the rule is in
39
- "when": { "Status": "shipped" }, // rows the rule applies to; default: all rows
40
- "require": { "Tracking": { "$ne": null } }, // row query each of them matches
41
- "count": { "$gte": 1 } // or: condition on how many there are
42
- }]
43
- }]
44
- }
45
- Queries are MongoDB syntax: cells are text, an empty cell is null, a list is an array of its items
46
- (split on "separator", trimmed; an operator matches if any item does, so {"$eq": "x"} means "has item x"
47
- and {"$ne": "x"} means "no item is x"). Operators:
48
- $eq $ne $in $nin $not $regex $options $or $and $nor (also inside a column's condition)
49
- $gt $gte $lt $lte exact number comparison, or date comparison with a "YYYY-MM-DD" bound
50
- $ref "Table.Column" value exists there (or "alias:Table.Column" of a dependency)
51
- $every {...} every list item meets the condition
52
- $length {...} length (chars, or list items) meets the condition
53
- $unique true no two rows share the value; in a row query ["A", "B"]: the combination
54
- $mentioned "Table.Column" | [...] | "*" value appears there as a word; "*": anywhere in the
55
- document outside the value's own table (prose and other tables both count)
56
- $calc "Qty * Price == Total" | {"expr": ..., "tolerance": 0.01} row query only; exact arithmetic
57
- Codes: each failing operator reports its own: $eq $in → BAD_VALUE, $regex → PATTERN, $gt.. → OUT_OF_RANGE,
58
- $length → BAD_LENGTH, $ref → UNKNOWN_REF, $unique → DUPLICATE_KEY, $mentioned → NOT_MENTIONED,
59
- {"$ne": null} in a rule → REQUIRED_IF, a wrong type → BAD_TYPE, a rule's other queries → MATCH.
60
- A value failing every branch of an $or reports the branches' code when they agree, else NONE_OF
61
- (each branch's code in the message, and in --json "alternatives").
62
- Examples:
63
- "match": { "$regex": "^O-[0-9]+$" }
64
- "match": { "$in": ["open", "paid", "shipped"] }
65
- "match": { "$length": { "$gte": 1 }, "$every": { "$ref": "Lines.Line_ID" } }
66
- "match": { "$or": [{ "$eq": "always" }, { "$ref": "Scenarios.ID" }] }
67
- "match": { "$ref": "customers:Customers.ID" } // a column of the dependency "customers"
68
- { "when": { "Kind": { "$in": ["resistor", "capacitor"] } }, "require": { "Value": { "$ne": null } } }
69
- { "require": { "$calc": { "expr": "Qty * Price == Total", "tolerance": "0.005" } } }
70
- { "on": "bom:Parts", "require": { "Ref": { "$ref": "Stock.Ref" } } }
7
+ const FORMAT = `Contract JSON. Only "tables" and each table's and column's "name" are required; every other
8
+ field is optional. "(default)" marks the value used when a field is left out; other values are examples.
9
+ {
10
+ "name": "shop.orders", "version": "1.0.0", "description": "...", // shown in output
11
+ "format": "markdown", // (default), or "format": "csv": one <table>.csv per table. The tables
12
+ // given with --tables / --dep must be in their own contract's format
13
+ "missing_values": [""], // (default) cell texts that count as empty (null in queries), e.g. ["", "-"].
14
+ // Keep a marker like "-" out of it when "-" is a real value: "-" allowed
15
+ // but an empty cell forbidden is "required": true plus
16
+ // "match": {"$or": [{"$eq": "-"}, {"$every": {"$ref": "Tests.ID"}}]}
17
+ "allow_extra_tables": false, // (default) true: tables not declared here are allowed
18
+ "depends_on": { "customers": "customers.contract.json" }, // path absolute or relative to this contract;
19
+ // its tables passed with --dep customers=<file>, referenced as "customers:Table.Column"
20
+ "tables": [{
21
+ "name": "Orders",
22
+ "required": true, // (default) the table must be in the document
23
+ "primary_key": "Order_ID", // or ["A", "B"]; always filled and unique, no "required" needed
24
+ "ordered": false, // (default) true: columns must be in the declared order
25
+ "allow_extra_columns": false, // (default) true: columns not declared here are allowed
26
+ "columns": [{
27
+ "name": "Total",
28
+ "type": "number", // string (default) | integer | number | boolean | date (YYYY-MM-DD) | list
29
+ "required": false, // (default) true: every cell must be filled (an empty cell fails
30
+ // MISSING_VALUE); the column itself must always be in the header
31
+ "separator": ",", // (default) list item separator
32
+ "match": { "$gte": 0, "$lte": 100 } // condition every filled cell meets (MongoDB query);
33
+ // empty cells are skipped: use "required" to forbid them
34
+ }],
35
+ "rules": [{ // exactly one of require, count
36
+ "id": "...", "description": "...", // shown in findings; description replaces the "expected" text
37
+ "severity": "error", // (default) or "warning": reported as WARN, does not fail the check
38
+ "on": "Table", // or "alias:Table"; default: the table the rule is in
39
+ "when": { "Status": "shipped" }, // rows the rule applies to; default: all rows
40
+ "require": { "Tracking": { "$ne": null } }, // row query each of them matches
41
+ "count": { "$gte": 1 } // or: condition on how many there are
42
+ }]
43
+ }]
44
+ }
45
+ Queries are MongoDB syntax: cells are text, an empty cell is null, a list is an array of its items
46
+ (split on "separator", trimmed; an operator matches if any item does, so {"$eq": "x"} means "has item x"
47
+ and {"$ne": "x"} means "no item is x"). Operators:
48
+ $eq $ne $in $nin $not $regex $options $or $and $nor (also inside a column's condition)
49
+ $gt $gte $lt $lte exact number comparison, or date comparison with a "YYYY-MM-DD" bound
50
+ $ref "Table.Column" value exists there (or "alias:Table.Column" of a dependency)
51
+ $every {...} every list item meets the condition
52
+ $length {...} length (chars, or list items) meets the condition
53
+ $unique true no two rows share the value; in a row query ["A", "B"]: the combination
54
+ $mentioned "Table.Column" | [...] | "*" value appears there as a word; "*": anywhere in the
55
+ document outside the value's own table (prose and other tables both count)
56
+ $calc "Qty * Price == Total" | {"expr": ..., "tolerance": 0.01} row query only; exact arithmetic
57
+ Codes: each failing operator reports its own: $eq $in → BAD_VALUE, $regex → PATTERN, $gt.. → OUT_OF_RANGE,
58
+ $length → BAD_LENGTH, $ref → UNKNOWN_REF, $unique → DUPLICATE_KEY, $mentioned → NOT_MENTIONED,
59
+ {"$ne": null} in a rule → REQUIRED_IF, a wrong type → BAD_TYPE, a rule's other queries → MATCH.
60
+ A value failing every branch of an $or reports the branches' code when they agree, else NONE_OF
61
+ (each branch's code in the message, and in --json "alternatives").
62
+ Examples:
63
+ "match": { "$regex": "^O-[0-9]+$" }
64
+ "match": { "$in": ["open", "paid", "shipped"] }
65
+ "match": { "$length": { "$gte": 1 }, "$every": { "$ref": "Lines.Line_ID" } }
66
+ "match": { "$or": [{ "$eq": "always" }, { "$ref": "Scenarios.ID" }] }
67
+ "match": { "$ref": "customers:Customers.ID" } // a column of the dependency "customers"
68
+ { "when": { "Kind": { "$in": ["resistor", "capacitor"] } }, "require": { "Value": { "$ne": null } } }
69
+ { "require": { "$calc": { "expr": "Qty * Price == Total", "tolerance": "0.005" } } }
70
+ { "on": "bom:Parts", "require": { "Ref": { "$ref": "Stock.Ref" } } }
71
71
  { "severity": "warning", "require": { "ID": { "$mentioned": "*" } } }`;
72
72
  const check = {
73
73
  command: "check",
@@ -0,0 +1,22 @@
1
+ import { Diagnostic } from "./diagnostics.js";
2
+ import { Contract } from "./model.js";
3
+ export interface DescribeResult {
4
+ status: "OK" | "INVALID_CONTRACT" | "MISSING_DATA";
5
+ contract: string;
6
+ markdown?: string;
7
+ diagnostics: Diagnostic[];
8
+ }
9
+ /**
10
+ * Renders a valid contract as a Markdown guide to the tables it expects.
11
+ *
12
+ * @param {Contract} c - The contract.
13
+ * @returns {string} The guide.
14
+ */
15
+ export declare function renderGuide(c: Contract): string;
16
+ /**
17
+ * contract describe: validates the contract, then renders its guide.
18
+ *
19
+ * @param {string} path - Contract file.
20
+ * @returns {DescribeResult} OK with the Markdown, or the contract's problems.
21
+ */
22
+ export declare function describeContract(path: string): DescribeResult;
@@ -0,0 +1,108 @@
1
+ /** One finding of `contract check` or `table check`. */
2
+ export interface Diagnostic {
3
+ severity: "FAIL" | "WARN";
4
+ code: Code;
5
+ /** Table name, or "-" for document- or contract-level findings. */
6
+ table: string;
7
+ /** Row label (primary key or `#n`), or "-". */
8
+ row: string;
9
+ /** Column name or contract field path, or "-". */
10
+ column: string;
11
+ message: string;
12
+ found: string;
13
+ expected: string;
14
+ fix: string;
15
+ /** ID of the contract rule that failed, if a rule did. */
16
+ rule?: string;
17
+ /** For NONE_OF: why each `$or` branch failed, in order. */
18
+ alternatives?: Alternative[];
19
+ }
20
+ /** Why one branch of a failing `$or` failed. */
21
+ export interface Alternative {
22
+ code: Code;
23
+ message: string;
24
+ }
25
+ /** Every code, with its fix hint. */
26
+ declare const HINTS: {
27
+ readonly "CONTRACT.BAD_JSON": "Fix the JSON syntax of the contract file.";
28
+ readonly "CONTRACT.BAD_FIELD": "Give the field the type or value named in 'expected'; remove unknown fields.";
29
+ readonly "CONTRACT.BAD_REGEX": "Write a valid JavaScript regular expression.";
30
+ readonly "CONTRACT.BAD_EXPRESSION": "Write an arithmetic expression over this table's number columns.";
31
+ readonly "CONTRACT.UNKNOWN_TABLE": "Name a table the contract declares.";
32
+ readonly "CONTRACT.UNKNOWN_COLUMN": "Name a column the table declares.";
33
+ readonly "CONTRACT.UNKNOWN_DEPENDENCY": "Declare the alias in depends_on.";
34
+ readonly "CONTRACT.DUPLICATE": "Give each table, column and rule a unique name.";
35
+ readonly "CONTRACT.CONFLICT": "Make the settings agree (e.g. minimum <= maximum, constraint fits the type).";
36
+ readonly "CONTRACT.CYCLE": "Remove the circular depends_on.";
37
+ readonly "CONTRACT.MISSING_FILE": "Check the path; the file must exist and be readable.";
38
+ readonly MISSING_TABLE: "Add the table under a '## <name>' heading (Markdown) or as <name>.csv.";
39
+ readonly EXTRA_TABLE: "Remove the table, or declare it in the contract.";
40
+ readonly DUPLICATE_TABLE: "Write each table once.";
41
+ readonly BAD_TABLE: "Write the table as a header row, a '|---|' separator row, then data rows.";
42
+ readonly MISSING_COLUMN: "Add the column to the header.";
43
+ readonly EXTRA_COLUMN: "Remove the column, or declare it in the contract.";
44
+ readonly DUPLICATE_COLUMN: "Name each column once.";
45
+ readonly COLUMN_ORDER: "Put the columns in the declared order.";
46
+ readonly ROW_SHAPE: "Give the row one cell per column; in Markdown write '\\|' for a '|' inside a cell.";
47
+ readonly MISSING_VALUE: "Fill the cell.";
48
+ readonly BAD_TYPE: "Write the value in the column's type.";
49
+ readonly BAD_VALUE: "Use one of the allowed values, spelled exactly.";
50
+ readonly PATTERN: "Write the value in the required format.";
51
+ readonly OUT_OF_RANGE: "Use a value within the allowed range.";
52
+ readonly BAD_LENGTH: "Use a value of the allowed length.";
53
+ readonly DUPLICATE_KEY: "Make the value unique in its table.";
54
+ readonly UNKNOWN_REF: "Use only values that exist in the referenced column.";
55
+ readonly REQUIRED_IF: "Fill the cell; the rule requires it for this row.";
56
+ readonly EMPTY_IF: "Leave the cell empty; the rule forbids a value for this row.";
57
+ readonly COMPARE: "Correct the values so the comparison holds.";
58
+ readonly UNIQUE_TOGETHER: "Make the combination of values unique.";
59
+ readonly ROW_COUNT: "Add or remove rows so the count fits.";
60
+ readonly NOT_MENTIONED: "Mention the value where the rule says.";
61
+ readonly MATCH: "Correct the row so it matches the rule's query (shown in 'expected').";
62
+ readonly NONE_OF: "Make the value meet one of the alternatives in 'expected'.";
63
+ readonly MISSING_FILE: "Check the path; the file must exist and be readable.";
64
+ readonly MISSING_DEPENDENCY: "Pass the dependency's tables with --dep <alias>=<file>.";
65
+ readonly TOO_LARGE: "Split the table; it exceeds the row limit.";
66
+ readonly INVALID_INPUT: "Fix the command arguments named in the message.";
67
+ };
68
+ export type Code = keyof typeof HINTS;
69
+ /** Where a finding points. */
70
+ export interface Where {
71
+ table?: string;
72
+ row?: string;
73
+ column?: string;
74
+ rule?: string;
75
+ }
76
+ /**
77
+ * Builds a diagnostic with its fix hint.
78
+ *
79
+ * @param {Code} code - The code.
80
+ * @param {string} message - What is wrong.
81
+ * @param {Where} at - Table, row, column and rule.
82
+ * @param {string} found - Observed value.
83
+ * @param {string} expected - Required value.
84
+ * @param {"FAIL"|"WARN"} [severity="FAIL"] - Severity.
85
+ * @param {Alternative[]} [alternatives] - For NONE_OF: why each `$or` branch failed.
86
+ * @returns {Diagnostic} The diagnostic.
87
+ */
88
+ export declare function diag(code: Code, message: string, at: Where, found: string, expected: string, severity?: "FAIL" | "WARN", alternatives?: Alternative[]): Diagnostic;
89
+ /**
90
+ * Sorts diagnostics deterministically: FAIL before WARN, then by table, row, column, code.
91
+ *
92
+ * @param {Diagnostic[]} list - Diagnostics.
93
+ * @returns {Diagnostic[]} A sorted copy.
94
+ */
95
+ export declare function sortDiagnostics(list: Diagnostic[]): Diagnostic[];
96
+ /**
97
+ * Renders diagnostics as compact text: one line per finding, findings that
98
+ * differ only by row grouped onto one line, then each fix hint once per code.
99
+ *
100
+ * @param {Diagnostic[]} diags - Sorted diagnostics.
101
+ * @returns {string[]} The lines.
102
+ *
103
+ * @example
104
+ * formatDiagnostics(result.diagnostics);
105
+ * // ["FAIL BAD_VALUE Orders Status ×2: O-1=Open, O-4=Shipped; expected open, closed", "fix BAD_VALUE: ..."]
106
+ */
107
+ export declare function formatDiagnostics(diags: Diagnostic[]): string[];
108
+ export {};
@@ -0,0 +1,58 @@
1
+ import { Diagnostic } from "./diagnostics.js";
2
+ import { Contract } from "./model.js";
3
+ /** A contract file that was read, with its dependencies. */
4
+ export interface LoadedContract {
5
+ path: string;
6
+ /** `name@version`, `name`, or the file name: how findings refer to this contract. */
7
+ label: string;
8
+ /** The contract when it and all its dependencies are valid, else null. */
9
+ contract: Contract | null;
10
+ /** Dependencies by alias (each loaded, valid or not). */
11
+ deps: Map<string, LoadedContract>;
12
+ }
13
+ /**
14
+ * Parses a table reference (a rule's `on`).
15
+ *
16
+ * @param {string} ref - `Table` or `alias:Table`.
17
+ * @returns {{alias: string|null, table: string}} Its parts.
18
+ */
19
+ export declare function parseTableRef(ref: string): {
20
+ alias: string | null;
21
+ table: string;
22
+ };
23
+ /**
24
+ * Parses a column reference.
25
+ *
26
+ * @param {string} ref - `Column`, `Table.Column` or `alias:Table.Column`.
27
+ * @returns {{alias: string|null, table: string|null, column: string}} Its parts.
28
+ */
29
+ export declare function parseRef(ref: string): {
30
+ alias: string | null;
31
+ table: string | null;
32
+ column: string;
33
+ };
34
+ /**
35
+ * The status of a check whose contract did not load: MISSING_DATA when a contract file
36
+ * could not be read, else INVALID_CONTRACT.
37
+ *
38
+ * @param {Diagnostic[]} diags - The loading diagnostics.
39
+ * @returns {"MISSING_DATA" | "INVALID_CONTRACT"} The status.
40
+ */
41
+ export declare const failedLoadStatus: (diags: Diagnostic[]) => "MISSING_DATA" | "INVALID_CONTRACT";
42
+ /**
43
+ * Validates one contract whose dependencies are already loaded.
44
+ *
45
+ * @param {unknown} data - Parsed contract JSON.
46
+ * @param {Map<string, LoadedContract>} deps - Its dependencies by alias.
47
+ * @returns {Diagnostic[]} Every problem; empty when valid.
48
+ */
49
+ export declare function validateContract(data: unknown, deps: Map<string, LoadedContract>): Diagnostic[];
50
+ /**
51
+ * Reads and validates a contract and, recursively, the contracts it depends on.
52
+ *
53
+ * @param {string} path - Contract file.
54
+ * @param {Diagnostic[]} out - Receives every problem; a dependency's are prefixed with its label.
55
+ * @param {string[]} [stack=[]] - Files being loaded above this one (cycle detection).
56
+ * @returns {LoadedContract} The contract (null when it or a dependency is invalid).
57
+ */
58
+ export declare function loadContract(path: string, out: Diagnostic[], stack?: string[]): LoadedContract;
@@ -0,0 +1,67 @@
1
+ import type { Shape } from "../../core/json-shape.js";
2
+ import type { Query } from "../../core/query.js";
3
+ export declare const COLUMN_TYPES: readonly ["string", "integer", "number", "boolean", "date", "list"];
4
+ export type ColumnType = (typeof COLUMN_TYPES)[number];
5
+ /** One declared column. Only `name` is required; every value constraint is in `match`. */
6
+ export interface Column {
7
+ name: string;
8
+ /** Default `string`. */
9
+ type?: ColumnType;
10
+ description?: string;
11
+ /** The cell must not be a missing value. Default false. */
12
+ required?: boolean;
13
+ /** Item separator for `list`. Default ",". */
14
+ separator?: string;
15
+ /** A MongoDB-style condition every filled cell must meet, e.g. `{"$in": ["a", "b"]}`. */
16
+ match?: Record<string, unknown>;
17
+ }
18
+ /** A rule: rows (of `on`, default this table) matching `when` must match `require`, or their number must match `count`. */
19
+ export interface Rule {
20
+ id?: string;
21
+ description?: string;
22
+ /** `Table` or `alias:Table` whose rows the rule tests. Default: the table the rule is in. */
23
+ on?: string;
24
+ /** Rows the rule applies to: a row query. */
25
+ when?: Query;
26
+ /** A row query every applicable row must match. */
27
+ require?: Query;
28
+ /** A condition on the number of applicable rows, e.g. `{"$gte": 1}`. */
29
+ count?: Record<string, unknown>;
30
+ /** `error` (default) fails the check, `warning` only reports. */
31
+ severity?: "error" | "warning";
32
+ }
33
+ export interface Table {
34
+ name: string;
35
+ description?: string;
36
+ /** The table must be present. Default true. */
37
+ required?: boolean;
38
+ /** Columns must appear in the declared order. Default false. */
39
+ ordered?: boolean;
40
+ /** Columns not declared are allowed. Default false. */
41
+ allow_extra_columns?: boolean;
42
+ /** Column (or columns) whose values identify a row: required and unique. */
43
+ primary_key?: string | string[];
44
+ columns: Column[];
45
+ rules?: Rule[];
46
+ }
47
+ /** A table contract. Only `tables` is required. */
48
+ export interface Contract {
49
+ name?: string;
50
+ version?: string;
51
+ description?: string;
52
+ /** `markdown` (default): one `## <Table>` section per table; `csv`: one `<Table>.csv` per table. */
53
+ format?: "markdown" | "csv";
54
+ /** Cell texts that count as missing. Default [""]. */
55
+ missing_values?: string[];
56
+ /** Tables not declared are allowed. Default false. */
57
+ allow_extra_tables?: boolean;
58
+ /** Other contracts this one references: alias -> contract file (relative to this one). */
59
+ depends_on?: Record<string, string>;
60
+ tables: Table[];
61
+ }
62
+ export declare const COLUMN_SHAPE: Shape;
63
+ /** A rule. */
64
+ export declare const RULE_SHAPE: Shape;
65
+ export declare const CONTRACT_SHAPE: Shape;
66
+ /** The primary key columns of a table (empty when none). */
67
+ export declare const keyColumns: (t: Table) => string[];
@@ -0,0 +1,137 @@
1
+ import type { Node } from "../../core/exact/expression.js";
2
+ import { Fraction } from "../../core/exact/fraction.js";
3
+ import { Doc, Query, QueryHooks, Value } from "../../core/query.js";
4
+ import type { Alternative, Code } from "./diagnostics.js";
5
+ import type { Column, Contract, Table } from "./model.js";
6
+ declare const CALC_OPS: readonly ["<=", ">=", "==", "!=", "<", ">"];
7
+ type CalcOp = (typeof CALC_OPS)[number];
8
+ /** One table row: its label (primary key or `#n`), its cells by column name, and the cells as a query sees them. */
9
+ export interface Row {
10
+ label: string;
11
+ cells: Record<string, string>;
12
+ doc: Doc;
13
+ /** The row has the wrong number of cells (ROW_SHAPE): it counts and can be referenced, but its own cells are not checked. */
14
+ shapeError: boolean;
15
+ }
16
+ /** A resolved `Table.Column` or `alias:Table.Column` reference. */
17
+ export interface Ref {
18
+ alias: string | null;
19
+ table: Table;
20
+ column: Column;
21
+ }
22
+ /** What `contract check` gives a query to validate it. */
23
+ export interface ValidateContext {
24
+ contract: Contract;
25
+ /** The table whose rows the query tests. */
26
+ table: Table;
27
+ /** Reports a contract problem (`field` names the contract field). */
28
+ problem(code: Code, message: string, field: string, found: string, expected: string): void;
29
+ /** Reports UNKNOWN_COLUMN for names the table does not declare; returns whether all exist. */
30
+ requireColumns(names: string[], field: string): boolean;
31
+ /** Resolves `Table.Column` or `alias:Table.Column`, reporting what is unknown. */
32
+ resolve(ref: string, field: string): Ref | null;
33
+ }
34
+ /** A query's hooks plus `$calc`'s values, which explaining a COMPARE failure needs. */
35
+ export interface CheckHooks extends QueryHooks {
36
+ calcValues(param: unknown, doc: Doc): {
37
+ left: Fraction;
38
+ right: Fraction;
39
+ } | null;
40
+ }
41
+ /** A failing part of a query, ready to report. */
42
+ export interface Failure {
43
+ code: Code;
44
+ message: string;
45
+ column: string;
46
+ found: string;
47
+ expected: string;
48
+ /** For NONE_OF: why each `$or` branch failed. */
49
+ alternatives?: Alternative[];
50
+ }
51
+ /** Splits a list cell into trimmed, non-empty items. */
52
+ export declare const listItems: (cell: string, separator?: string) => string[];
53
+ /** A value as text: list items joined, missing as empty. */
54
+ export declare const valueText: (v: Value | undefined) => string;
55
+ /**
56
+ * Whether `word` occurs in `text` as a whole token (not inside a longer word or ID).
57
+ *
58
+ * @param {string} text - Text to search.
59
+ * @param {string} word - The value.
60
+ * @returns {boolean} True when mentioned.
61
+ */
62
+ export declare function mentions(text: string, word: string): boolean;
63
+ /** Number columns of a table: the names a `$calc` expression may use. */
64
+ export declare const numberColumns: (t: Table) => Set<string>;
65
+ /** A parsed `$calc`: `"Qty * Price == Total"` or `{"expr": "...", "tolerance": 0.01}`. */
66
+ interface Calc {
67
+ text: string;
68
+ left: Node;
69
+ op: CalcOp;
70
+ right: Node;
71
+ tolerance: Fraction;
72
+ }
73
+ /**
74
+ * Parses a `$calc` parameter over the given number columns.
75
+ *
76
+ * @param {unknown} param - The parameter.
77
+ * @param {Set<string>} allowed - Number columns the expression may use.
78
+ * @returns {Calc | string} The parsed comparison, or what is wrong with it.
79
+ */
80
+ export declare function parseCalc(param: unknown, allowed: Set<string>): Calc | string;
81
+ /**
82
+ * Evaluates a `$calc` on a row.
83
+ *
84
+ * @param {Calc} c - The parsed comparison.
85
+ * @param {Doc} doc - The row.
86
+ * @param {Set<string>} numbers - The table's number columns.
87
+ * @returns {{left: Fraction, right: Fraction, holds: boolean} | null} The sides and the result; null when a
88
+ * value is missing or not a number, or the arithmetic fails (the column check reports those).
89
+ */
90
+ export declare function evaluateCalc(c: Calc, doc: Doc, numbers: Set<string>): {
91
+ left: Fraction;
92
+ right: Fraction;
93
+ holds: boolean;
94
+ } | null;
95
+ /**
96
+ * Validates a query of the contract (`match`, `when`, `require`, `count`):
97
+ * operators, values, columns, `$ref`/`$mentioned` targets, `$calc` and `$regex`.
98
+ *
99
+ * @param {unknown} q - The query, as a row query.
100
+ * @param {string} field - Contract field, for the report.
101
+ * @param {ValidateContext} v - Validation context.
102
+ * @returns {void}
103
+ */
104
+ export declare function checkQuery(q: unknown, field: string, v: ValidateContext): void;
105
+ /**
106
+ * Splits a row query into parts that are checked (and reported) one by one.
107
+ *
108
+ * @param {Query} q - The query.
109
+ * @returns {Query[]} One row query per column operator, logical operator, `$calc` or `$unique`.
110
+ */
111
+ export declare function splitQuery(q: Query): Query[];
112
+ /**
113
+ * Explains why a row fails one part of a query.
114
+ *
115
+ * @param {Query} part - A part from `splitQuery`.
116
+ * @param {Doc} doc - The row.
117
+ * @param {CheckHooks} hooks - The hooks the query ran with.
118
+ * @returns {Failure} Code, message, column, found and expected.
119
+ */
120
+ export declare function explain(part: Query, doc: Doc, hooks: CheckHooks): Failure;
121
+ /**
122
+ * One column condition in words, e.g. "one of `a`, `b`".
123
+ *
124
+ * @param {unknown} cond - The condition.
125
+ * @param {(v: unknown) => string} c - Formats a value (backticks for describe, String for diagnostics).
126
+ * @returns {string} The text.
127
+ */
128
+ export declare function renderCondition(cond: unknown, c?: (v: unknown) => string): string;
129
+ /**
130
+ * A row query in words, e.g. "`Status` = `shipped` and `Tracking` filled".
131
+ *
132
+ * @param {unknown} q - The query.
133
+ * @param {(v: unknown) => string} c - Formats a value.
134
+ * @returns {string} The text.
135
+ */
136
+ export declare function renderQuery(q: unknown, c?: (v: unknown) => string): string;
137
+ export {};
@@ -0,0 +1,2 @@
1
+ declare const _default: import("../../stm32/tool.js").ToolDef;
2
+ export default _default;