@turingcode/cli 0.2.13 → 0.3.5

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 (185) 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 +17 -4
  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/exact/fraction.js +2 -0
  11. package/dist/core/files.d.ts +44 -0
  12. package/dist/core/files.js +48 -3
  13. package/dist/core/json-shape.d.ts +45 -0
  14. package/dist/core/json-shape.js +1 -0
  15. package/dist/core/mcp.d.ts +92 -0
  16. package/dist/core/mcp.js +56 -15
  17. package/dist/core/query.d.ts +69 -0
  18. package/dist/core/query.js +2 -2
  19. package/dist/core/report.d.ts +11 -0
  20. package/dist/core/tables/csv.d.ts +29 -0
  21. package/dist/core/tables/csv.js +54 -25
  22. package/dist/core/tables/markdown.d.ts +22 -0
  23. package/dist/core/tables/markdown.js +8 -2
  24. package/dist/core/tables/types.d.ts +25 -0
  25. package/dist/features/contract/check.d.ts +30 -0
  26. package/dist/features/contract/command.d.ts +12 -0
  27. package/dist/features/contract/command.js +64 -64
  28. package/dist/features/contract/describe.d.ts +22 -0
  29. package/dist/features/contract/diagnostics.d.ts +111 -0
  30. package/dist/features/contract/diagnostics.js +7 -4
  31. package/dist/features/contract/load.d.ts +58 -0
  32. package/dist/features/contract/load.js +4 -4
  33. package/dist/features/contract/model.d.ts +67 -0
  34. package/dist/features/contract/rules.d.ts +149 -0
  35. package/dist/features/contract/rules.js +31 -4
  36. package/dist/features/debug/clear_breakpoint.d.ts +3 -0
  37. package/dist/features/debug/clear_breakpoint.js +24 -0
  38. package/dist/features/debug/command.d.ts +5 -0
  39. package/dist/features/debug/command.js +66 -0
  40. package/dist/features/debug/continue.d.ts +3 -0
  41. package/dist/features/debug/continue.js +22 -0
  42. package/dist/features/debug/debug_start.d.ts +3 -0
  43. package/dist/features/debug/debug_start.js +22 -0
  44. package/dist/features/debug/debug_stop.d.ts +3 -0
  45. package/dist/features/debug/debug_stop.js +11 -0
  46. package/dist/features/debug/evaluate.d.ts +3 -0
  47. package/dist/features/debug/evaluate.js +48 -0
  48. package/dist/features/debug/get_stack.d.ts +3 -0
  49. package/dist/features/debug/get_stack.js +14 -0
  50. package/dist/features/debug/get_variables.d.ts +3 -0
  51. package/dist/features/debug/get_variables.js +26 -0
  52. package/dist/features/debug/halt.d.ts +3 -0
  53. package/dist/features/debug/halt.js +16 -0
  54. package/dist/features/debug/set_breakpoint.d.ts +3 -0
  55. package/dist/features/debug/set_breakpoint.js +30 -0
  56. package/dist/features/debug/step.d.ts +3 -0
  57. package/dist/features/debug/step.js +23 -0
  58. package/dist/features/install-mods/command.d.ts +6 -0
  59. package/dist/features/install-mods/command.js +18 -0
  60. package/dist/features/install-mods/install.d.ts +23 -0
  61. package/dist/features/install-mods/install.js +49 -0
  62. package/dist/features/install-skills/command.d.ts +7 -0
  63. package/dist/features/install-skills/command.js +22 -0
  64. package/dist/features/install-skills/install.d.ts +14 -0
  65. package/dist/features/install-skills/install.js +29 -0
  66. package/dist/features/probe/command.d.ts +5 -0
  67. package/dist/features/probe/command.js +112 -0
  68. package/dist/features/probe/kill_all_debuggers.d.ts +3 -0
  69. package/dist/features/probe/kill_all_debuggers.js +50 -0
  70. package/dist/features/probe/list_probes.d.ts +3 -0
  71. package/dist/features/probe/list_probes.js +8 -0
  72. package/dist/features/probe/program_project.d.ts +3 -0
  73. package/dist/features/probe/program_project.js +29 -0
  74. package/dist/features/probe/read_fault_status.d.ts +3 -0
  75. package/dist/features/probe/read_fault_status.js +19 -0
  76. package/dist/features/probe/read_memory.d.ts +3 -0
  77. package/dist/features/probe/read_memory.js +36 -0
  78. package/dist/features/probe/read_peripheral.d.ts +3 -0
  79. package/dist/features/probe/read_peripheral.js +60 -0
  80. package/dist/features/probe/reset_probe_usb.d.ts +3 -0
  81. package/dist/features/probe/reset_probe_usb.js +17 -0
  82. package/dist/features/probe/reset_target.d.ts +3 -0
  83. package/dist/features/probe/reset_target.js +16 -0
  84. package/dist/features/probe/target_info.d.ts +3 -0
  85. package/dist/features/probe/target_info.js +34 -0
  86. package/dist/features/project/analysis_static.d.ts +3 -0
  87. package/dist/features/project/analysis_static.js +19 -0
  88. package/dist/features/project/analysis_syntax_check.d.ts +3 -0
  89. package/dist/features/project/analysis_syntax_check.js +21 -0
  90. package/dist/features/project/command.d.ts +5 -0
  91. package/dist/features/project/command.js +64 -0
  92. package/dist/features/project/project_build.d.ts +3 -0
  93. package/dist/features/project/project_build.js +22 -0
  94. package/dist/features/project/project_download_template.d.ts +3 -0
  95. package/dist/features/project/project_download_template.js +34 -0
  96. package/dist/features/project/project_regenerate.d.ts +2 -0
  97. package/dist/features/project/project_regenerate.js +20 -0
  98. package/dist/features/project/project_sync_from_server.d.ts +2 -0
  99. package/dist/features/project/project_sync_from_server.js +13 -0
  100. package/dist/features/project/project_sync_to_server.d.ts +2 -0
  101. package/dist/features/project/project_sync_to_server.js +51 -0
  102. package/dist/features/project/stm32_cubetools_download_drivers.d.ts +3 -0
  103. package/dist/features/project/stm32_cubetools_download_drivers.js +19 -0
  104. package/dist/features/status/command.d.ts +2 -0
  105. package/dist/features/status/command.js +6 -0
  106. package/dist/features/status/get_status.d.ts +3 -0
  107. package/dist/features/status/get_status.js +44 -0
  108. package/dist/features/table/check.d.ts +38 -0
  109. package/dist/features/table/check.js +5 -5
  110. package/dist/features/table/command.d.ts +3 -0
  111. package/dist/features/table/read.d.ts +28 -0
  112. package/dist/features/table/read.js +9 -9
  113. package/dist/features/tool/command.d.ts +35 -0
  114. package/dist/features/tool/command.js +95 -66
  115. package/dist/lib.d.ts +15 -0
  116. package/dist/lib.js +11 -0
  117. package/dist/stm32/actions/DebugAction.d.ts +65 -0
  118. package/dist/stm32/actions/DebugAction.js +106 -0
  119. package/dist/stm32/actions/DebugActionInfo.d.ts +11 -0
  120. package/dist/stm32/actions/DebugActionInfo.js +11 -0
  121. package/dist/stm32/actions/DebugActionListProbes.d.ts +10 -0
  122. package/dist/stm32/actions/DebugActionListProbes.js +10 -0
  123. package/dist/stm32/actions/DebugActionProgram.d.ts +34 -0
  124. package/dist/stm32/actions/DebugActionProgram.js +81 -0
  125. package/dist/stm32/actions/DebugActionRead.d.ts +39 -0
  126. package/dist/stm32/actions/DebugActionRead.js +61 -0
  127. package/dist/stm32/actions/DebugActionReset.d.ts +12 -0
  128. package/dist/stm32/actions/DebugActionReset.js +9 -0
  129. package/dist/stm32/actions/DebugActionServer.d.ts +11 -0
  130. package/dist/stm32/actions/DebugActionServer.js +11 -0
  131. package/dist/stm32/analysis.d.ts +20 -0
  132. package/dist/stm32/analysis.js +21 -0
  133. package/dist/stm32/build.d.ts +34 -0
  134. package/dist/stm32/build.js +79 -0
  135. package/dist/stm32/context.d.ts +23 -0
  136. package/dist/stm32/context.js +42 -0
  137. package/dist/stm32/daemon.d.ts +65 -0
  138. package/dist/stm32/daemon.js +209 -0
  139. package/dist/stm32/regen.d.ts +68 -0
  140. package/dist/stm32/regen.js +231 -0
  141. package/dist/stm32/session.d.ts +137 -0
  142. package/dist/stm32/session.js +375 -0
  143. package/dist/stm32/sync.d.ts +25 -0
  144. package/dist/stm32/sync.js +74 -0
  145. package/dist/stm32/tool.d.ts +45 -0
  146. package/dist/stm32/tool.js +20 -0
  147. package/dist/stm32/toolCommand.d.ts +50 -0
  148. package/dist/stm32/toolCommand.js +100 -0
  149. package/dist/stm32/utils/discover_tools.d.ts +25 -0
  150. package/dist/stm32/utils/discover_tools.js +121 -0
  151. package/dist/stm32/utils/downloadzip_utils.d.ts +57 -0
  152. package/dist/stm32/utils/downloadzip_utils.js +180 -0
  153. package/dist/stm32/utils/faultUtils.d.ts +15 -0
  154. package/dist/stm32/utils/faultUtils.js +69 -0
  155. package/dist/stm32/utils/findElf.d.ts +9 -0
  156. package/dist/stm32/utils/findElf.js +19 -0
  157. package/dist/stm32/utils/hash_utils.d.ts +19 -0
  158. package/dist/stm32/utils/hash_utils.js +28 -0
  159. package/dist/stm32/utils/listSyncFiles.d.ts +9 -0
  160. package/dist/stm32/utils/listSyncFiles.js +100 -0
  161. package/dist/stm32/utils/mcu_utils.d.ts +58 -0
  162. package/dist/stm32/utils/mcu_utils.js +100 -0
  163. package/dist/stm32/utils/project_utils.d.ts +54 -0
  164. package/dist/stm32/utils/project_utils.js +104 -0
  165. package/dist/stm32/utils/svdUtils.d.ts +31 -0
  166. package/dist/stm32/utils/svdUtils.js +30 -0
  167. package/dist/stm32/utils/truncate.d.ts +7 -0
  168. package/dist/stm32/utils/truncate.js +9 -0
  169. package/dist/stm32/utils/usb_utils.d.ts +5 -0
  170. package/dist/stm32/utils/usb_utils.js +23 -0
  171. package/dist/stm32/utils/user_code.d.ts +17 -0
  172. package/dist/stm32/utils/user_code.js +45 -0
  173. package/dist/stm32/utils/utils_mutex.d.ts +15 -0
  174. package/dist/stm32/utils/utils_mutex.js +113 -0
  175. package/dist/stm32/validate.d.ts +17 -0
  176. package/dist/stm32/validate.js +46 -0
  177. package/mods/claude/.claude-plugin/marketplace.json +8 -0
  178. package/mods/claude/turingcode-skills/.claude-plugin/plugin.json +5 -0
  179. package/mods/claude/turingcode-skills/skills/turingcode-cli/SKILL.md +44 -0
  180. package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/SKILL.md +47 -0
  181. package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/concurrency.md +42 -0
  182. package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/init-and-timing.md +35 -0
  183. package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/input-and-bounds.md +21 -0
  184. package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/registers-and-units.md +35 -0
  185. package/package.json +11 -3
@@ -0,0 +1,113 @@
1
+ /**
2
+ * Probe lock for the debug actions and the debug session: one probe operation at a time, in this process and,
3
+ * once setProbeLockFile was called, across processes through a lock file that holds the owner's pid.
4
+ */
5
+ import { mkdirSync, readFileSync, renameSync, rmdirSync, unlinkSync, writeFileSync } from 'node:fs';
6
+ import { dirname } from 'node:path';
7
+ let owner; /** name of the run in this process holding the probe */
8
+ let lockFile; /** set by the CLI to <project>/.turingcode/probe.lock */
9
+ /**
10
+ * Makes claimProbe also take a lock file, so other processes see the probe as busy.
11
+ * @param file lock file path, e.g. <project>/.turingcode/probe.lock; undefined turns the lock file off
12
+ */
13
+ export function setProbeLockFile(file) {
14
+ lockFile = file;
15
+ }
16
+ /**
17
+ * Tells whether a process is running.
18
+ * @param pid process id
19
+ * @returns true when it exists (also when it belongs to another user)
20
+ */
21
+ function isAlive(pid) {
22
+ try {
23
+ process.kill(pid, 0);
24
+ return true;
25
+ }
26
+ catch (e) {
27
+ return e?.code === 'EPERM';
28
+ }
29
+ }
30
+ /**
31
+ * Takes the lock file for this process.
32
+ * @param name name of the run, written into the file for the busy error of other processes
33
+ * @throws when a running process other than this one holds it
34
+ */
35
+ function claimLockFile(name) {
36
+ const file = lockFile;
37
+ mkdirSync(dirname(file), { recursive: true });
38
+ for (;;) {
39
+ try {
40
+ writeFileSync(file, JSON.stringify({ pid: process.pid, name }), { flag: 'wx' });
41
+ return;
42
+ }
43
+ catch (e) {
44
+ if (e?.code !== 'EEXIST')
45
+ throw e;
46
+ }
47
+ let text = '';
48
+ let held = {};
49
+ try {
50
+ text = readFileSync(file, 'utf8');
51
+ held = JSON.parse(text);
52
+ }
53
+ catch {
54
+ /** unreadable or half written: treated as stale */
55
+ }
56
+ /** shortcut: a reused pid of a dead owner looks alive, so its lock waits for that process; add a start time if that bites */
57
+ if (held.pid && held.pid !== process.pid && isAlive(held.pid))
58
+ throw new Error(`The probe is busy with another turingcode run (pid ${held.pid}): ${held.name ?? 'unknown'}`);
59
+ /** stale: its process is gone. Moved away first (atomic), so two processes never both delete it and one a fresh lock. */
60
+ const moved = `${file}.${process.pid}`;
61
+ try {
62
+ renameSync(file, moved);
63
+ }
64
+ catch (e) {
65
+ if (e?.code === 'ENOENT')
66
+ continue; /** another process took it over first */
67
+ throw e;
68
+ }
69
+ /** shortcut: if a third process created a lock while a fresh one was moved, putting it back replaces that one; add a takeover lock if that bites */
70
+ if (readFileSync(moved, 'utf8') === text)
71
+ unlinkSync(moved);
72
+ else
73
+ renameSync(moved, file); /** a fresh lock had replaced the stale one: put it back, the next loop reports it busy */
74
+ }
75
+ }
76
+ /**
77
+ * Takes the probe for a debug action or the debug session; fails if another run holds it.
78
+ * @param name name of the run, shown in the error when a later claim fails
79
+ */
80
+ export function claimProbe(name) {
81
+ if (owner !== undefined)
82
+ throw new Error(`The probe is busy with another debug tool run: ${owner}`);
83
+ if (lockFile)
84
+ claimLockFile(name);
85
+ owner = name;
86
+ }
87
+ /**
88
+ * Releases the probe taken with claimProbe, and the lock file if this process holds it; removes the lock's
89
+ * folder when that leaves it empty.
90
+ */
91
+ export function releaseProbe() {
92
+ owner = undefined;
93
+ if (!lockFile)
94
+ return;
95
+ try {
96
+ if (JSON.parse(readFileSync(lockFile, 'utf8')).pid === process.pid)
97
+ unlinkSync(lockFile);
98
+ }
99
+ catch {
100
+ /** no lock file, or not ours */
101
+ }
102
+ try {
103
+ /**
104
+ * an empty .turingcode/ left outside a project would mark that folder as one.
105
+ * Keep this non-recursive: rmdir removes only an empty folder in one atomic step, so it can never delete
106
+ * project files or another process's lock; a readdir check first would race with that process.
107
+ */
108
+ rmdirSync(dirname(lockFile));
109
+ }
110
+ catch {
111
+ /** not empty (other project files, or another process's lock) or already gone */
112
+ }
113
+ }
@@ -0,0 +1,17 @@
1
+ /** Argument validation and safety rules of the STM32 tools, used in-process and by the debug daemon. */
2
+ import { ToolDef } from './tool.js';
3
+ export declare const PROBE_BUSY = "probe owned by the debug session: run `turingcode debug stop` first";
4
+ /**
5
+ * Checks tool arguments against the tool's schema.
6
+ * @param def tool definition
7
+ * @param args arguments to check
8
+ * @returns the error message, or undefined when the arguments are valid
9
+ */
10
+ export declare function validateArgs(def: ToolDef, args: unknown): string | undefined;
11
+ /**
12
+ * Tells why a valid call must be refused in the current state.
13
+ * @param def tool definition
14
+ * @param debugActive true while a debug session owns the probe
15
+ * @returns the reason, or undefined when the call may run
16
+ */
17
+ export declare function safetyCheck(def: ToolDef, debugActive: boolean): string | undefined;
@@ -0,0 +1,46 @@
1
+ export const PROBE_BUSY = 'probe owned by the debug session: run `turingcode debug stop` first';
2
+ /**
3
+ * Checks tool arguments against the tool's schema.
4
+ * @param def tool definition
5
+ * @param args arguments to check
6
+ * @returns the error message, or undefined when the arguments are valid
7
+ */
8
+ export function validateArgs(def, args) {
9
+ if (typeof args !== 'object' || args === null || Array.isArray(args))
10
+ return 'Arguments must be an object';
11
+ const { properties, required = [] } = def.inputSchema;
12
+ for (const key of required)
13
+ if (!Object.hasOwn(args, key))
14
+ return `Missing argument: ${key}`;
15
+ for (const [key, v] of Object.entries(args)) {
16
+ /** own keys only: properties.__proto__ or .constructor would otherwise be found on Object.prototype */
17
+ const p = Object.hasOwn(properties, key) ? properties[key] : undefined;
18
+ if (!p)
19
+ return `Unknown argument: ${key}`;
20
+ const okType = p.type === 'integer' ? Number.isInteger(v) : p.type === 'number' ? typeof v === 'number' && Number.isFinite(v) : typeof v === p.type;
21
+ if (!okType)
22
+ return `${key} must be ${p.type}`;
23
+ if (p.enum && !p.enum.includes(v))
24
+ return `${key} must be one of ${p.enum.join(', ')}`;
25
+ if (p.pattern && !new RegExp(p.pattern).test(v))
26
+ return `${key} has an invalid format`;
27
+ if (p.maxLength !== undefined && v.length > p.maxLength)
28
+ return `${key} is longer than ${p.maxLength}`;
29
+ if (p.minimum !== undefined && v < p.minimum)
30
+ return `${key} must be >= ${p.minimum}`;
31
+ if (p.maximum !== undefined && v > p.maximum)
32
+ return `${key} must be <= ${p.maximum}`;
33
+ }
34
+ return undefined;
35
+ }
36
+ /**
37
+ * Tells why a valid call must be refused in the current state.
38
+ * @param def tool definition
39
+ * @param debugActive true while a debug session owns the probe
40
+ * @returns the reason, or undefined when the call may run
41
+ */
42
+ export function safetyCheck(def, debugActive) {
43
+ if (debugActive && def.needsProbe)
44
+ return PROBE_BUSY;
45
+ return undefined;
46
+ }
@@ -0,0 +1,8 @@
1
+ {
2
+ "name": "turingcode-mods",
3
+ "owner": { "name": "turingcode" },
4
+ "description": "Claude Code mods shipped with the turingcode CLI",
5
+ "plugins": [
6
+ { "name": "turingcode-skills", "source": "./turingcode-skills" }
7
+ ]
8
+ }
@@ -0,0 +1,5 @@
1
+ {
2
+ "name": "turingcode-skills",
3
+ "version": "0.1.5",
4
+ "description": "Skills for working with the turingcode CLI and STM32 projects."
5
+ }
@@ -0,0 +1,44 @@
1
+ ---
2
+ name: turingcode-cli
3
+ description: Use when running the turingcode CLI on an STM32 project - creating a project, syncing, building, syntax checks or static analysis, regenerating from the `.ioc`, changing pins, clocks or peripherals through server tools, flashing, reading the board's registers, or debugging with breakpoints. Not for how to write the C/C++ code itself.
4
+ ---
5
+
6
+ # turingcode for STM32
7
+
8
+ - `turingcode` is a set of tools for working with stm32 projects.
9
+ - Start with `turingcode status`: it shows the project, token, last build, debug session and the installed probe tools.
10
+ - Server commands need `TURINGCODE_TOKEN`: set it in the environment, or put `TURINGCODE_TOKEN=<token>` in `.env` in the project folder.
11
+
12
+ ## Commands
13
+
14
+ Read the help before you run commands in an area; every subcommand has its own `--help` with flags and examples.
15
+
16
+ - `turingcode project --help`: sync, build, pull, regen, drivers, syntax check, static analysis.
17
+ - `turingcode probe --help`: flash, reset, read memory or registers, decode faults, recover a stuck probe.
18
+ - `turingcode debug --help`: breakpoints, stepping, stack, variables.
19
+
20
+ ## New project
21
+
22
+ Only when the user asks for one.
23
+
24
+ 1. `turingcode probe info` identifies the connected chip and lists the matching CubeMX names. Don't call probe-rs or STM32_Programmer_CLI yourself.
25
+ 2. Pick one name. The package (pin count) can't be read over SWD: ask the user, or pick one that has the pins they need. Names differing only in package build the same code for pins they share.
26
+ 3. `turingcode project template --mcu <name>` downloads it into `./<name>/`; `cd` into it, every project command runs from the project folder (copy the `.env` there too if you use one).
27
+
28
+
29
+ ## Server tools
30
+
31
+ Pins, clocks, .ioc edits, the knowledge base and register lookup are server tools:
32
+
33
+ - `turingcode tool list` to get a list of available tools.
34
+ - `turingcode tool call <name> --args '<json>' > result.md` to run it.
35
+
36
+ After a tool changes the server copy of the project, run `project pull` (or `project regen`).
37
+
38
+ ## Typical loop
39
+
40
+ 1. Edit the code, then `turingcode project sync -b` and fix the reported errors;
41
+ `turingcode project sync -b -a` before finishing a change.
42
+ 2. `turingcode probe program` to flash.
43
+ 3. Confirm it runs without looking at the board: read the register the program changes a few times with `probe read` and check it changes as expected.
44
+ 4. On a crash, `probe faults`, or `debug start` with breakpoints to find the cause.
@@ -0,0 +1,47 @@
1
+ ---
2
+ name: turingcode-firmware-code
3
+ description: Coding rules for embedded C/C++ firmware (STM32, HAL, FreeRTOS/CMSIS-RTOS2). Use whenever writing, editing or reviewing .c, .cpp, .h or .hpp files for a microcontroller - sensor drivers, ISRs and HAL callbacks, DMA, UART/SPI protocols, RTOS tasks, flash storage, filters, register maps.
4
+ ---
5
+
6
+ # Embedded C/C++ rules
7
+
8
+ When a rule and speed conflict, the rule wins.
9
+
10
+ ## Load the reference that matches the task
11
+
12
+ Read the file before you write code in that area, not after.
13
+
14
+ | Read | When the change touches |
15
+ |---|---|
16
+ | `references/input-and-bounds.md` | parsing UART/SPI/host frames, lengths, indices, register writes from a host, array or page bounds |
17
+ | `references/concurrency.md` | ISRs, HAL callbacks, DMA, semaphores/mutexes, buffers shared between tasks or with an ISR, read-and-clear flags |
18
+ | `references/init-and-timing.md` | boot order, flash-backed config, driver (re)init, timer callbacks, µs time, `extern "C"` interfaces |
19
+ | `references/registers-and-units.md` | sensor drivers, datasheet registers, bit fields, units, signedness, macros |
20
+
21
+ ## General code editing rules
22
+
23
+ - In generated files (main.c, stm32*_it.c, ...) write code only between `/* USER CODE BEGIN <name> */` and `/* USER CODE END <name> */`; CubeMX deletes everything else on project regeneration. Never change, move or delete the markers, and keep code the section already holds.
24
+
25
+ ## House style
26
+
27
+ Match this; don't "modernize" it.
28
+
29
+ - **Classes**: PascalCase joined with underscores, prefixed by layer or role: `Sensor_Driver`, `Util_Mutex`, `Filter_MovingAvg`.
30
+ - **Members**: private fields start with `_`; internal helpers start with `__`. Always access members with `this->`.
31
+ - **Methods**: snake_case for new code. Existing names in another case stay as they are; don't rename them on the side.
32
+ - **Errors**: return a status enum, not `bool` or raw ints. Accumulate with `status |= ...` so the worst result wins.
33
+ - **Comments**: `/* ---- Title ---- */` section banners. `/** */` blocks explain why, physics and units, and cite the datasheet. `////` marks code switched off for a short time.
34
+ - **Compile-time choices**: templates and `if constexpr` for sizes and variants. Project selection goes through typedefs in a config header. Runtime feature switches are register or config bits.
35
+ - **Layering**: reusable code goes in a shared library; project code only adds what's chip- or product-specific. C code (`main.c`) reaches C++ only through a thin `extern "C"` interface.
36
+ - Never edit vendor code (HAL, CMSIS, generated code outside `USER CODE` blocks).
37
+
38
+ ## Rules for all code
39
+
40
+ 1. **Never drop a status.** Check or accumulate every status, `HAL_StatusTypeDef` and RTOS return (`xTaskCreate`, semaphore acquire, queue send). Don't return a hard-coded OK after fallible steps. If ignoring one is truly fine, say why in one comment.
41
+ 2. **Every wait has a timeout and an error path.** No bare `while(1)` on hardware (ID checks, waiting on a bit, ready flags), and no retry count where 0 means "forever". Return an error and make the failure visible; a missing peripheral must not hang boot silently.
42
+ 3. **Memory budget.** No `new`/`delete` in periodic or per-transaction paths; preallocate buffers and matrices as members or statics. Keep KB-sized objects (large filter windows, matrices, arrays) off task stacks, and size each stack from what it holds.
43
+ 4. **Speed tweaks get tested.** Before committing a performance change to a read or parse path, test it on hardware and check that the success branch still runs on success.
44
+
45
+ ## Before you finish
46
+
47
+ Re-read the diff against the rules above and against the checklist at the end of each reference file you loaded.
@@ -0,0 +1,42 @@
1
+ # Concurrency: ISRs, DMA, tasks
2
+
3
+ ## Release on every path
4
+
5
+ If a function takes a semaphore or mutex and then starts a transfer, release it when the start fails too. Otherwise the next wait blocks forever:
6
+ ```cpp
7
+ if (HAL_UART_Transmit_DMA(...) != HAL_OK) {
8
+ this->_tx_sem.release();
9
+ return Status::ERROR;
10
+ }
11
+ ```
12
+ Error callbacks only release what belongs to that error. An RX overrun must not release the TX semaphore while TX DMA is still running.
13
+
14
+ ## DMA buffers outlive the transfer
15
+
16
+ - A buffer handed to DMA is static, a class member, or from a pool, never a local array. The stack frame is gone while DMA is still reading it.
17
+ - Check the length against the DMA buffer size before copying into it.
18
+ - Outgoing bytes (command, address) go in the TX buffer, not the RX one.
19
+
20
+ ## Shared data has one owner and one protection
21
+
22
+ For every buffer touched by an ISR and a task, or by two tasks, write a comment saying who writes it, who reads it and what protects it. Then pick one of these:
23
+
24
+ - **ISR-filled queue:** the task wraps compaction and clearing in a critical section (or disables that IRQ), or the queue is a lock-free single-producer/single-consumer ring.
25
+ - **Sample written by one task while another parses it:** read into a temp buffer, then copy or swap under the lock. Never write straight into the buffer being parsed, or you get torn samples.
26
+ - **Driver used by a running task:** don't re-init or calibrate it while that task is still using it (see `init-and-timing.md`).
27
+
28
+ ## Read-and-clear flags are consumed once
29
+
30
+ A "consume" call returns a flag and clears it. Call it once per flag per cycle and keep the result:
31
+ ```cpp
32
+ const bool acc_changed = this->acc->consume_changed();
33
+ const bool gyro_changed = this->gyro->consume_changed(); // its own flag
34
+ ```
35
+ A second call on the same flag always returns false.
36
+
37
+ ## Checklist
38
+ - [ ] Every acquire has a release on the failure path
39
+ - [ ] Error callbacks only release their own resources
40
+ - [ ] No DMA buffer on the stack; lengths checked against the DMA buffer
41
+ - [ ] Every shared buffer has a documented owner and lock
42
+ - [ ] Each read-and-clear flag is consumed exactly once per cycle
@@ -0,0 +1,35 @@
1
+ # Initialization and timing
2
+
3
+ ## Order is explicit
4
+
5
+ - Nothing reads a store before it's initialized: init the flash driver before loading config from it. If the load fails, use defaults, not whatever `new` left in memory (it isn't zeroed).
6
+ - Erased flash reads 0xFF, and that isn't a valid config: every bit on, every scale at its maximum. Detect it with a magic number, version or CRC, and fall back to safe defaults.
7
+ - Leave the flash locked after init. Unlock only around an erase or program.
8
+
9
+ ## Callbacks may fire before your objects exist
10
+
11
+ HAL and ISR callbacks (EXTI, UART error, timer) can run before the task that creates the global object, for example when the interrupt is enabled in generated init code. Null-check every global pointer they use:
12
+ ```cpp
13
+ if (driver == nullptr) return;
14
+ ```
15
+
16
+ ## Re-init
17
+
18
+ Before re-initializing or calibrating a driver:
19
+ 1. Stop or pause the tasks that use it.
20
+ 2. Clear its ready flag first.
21
+ 3. Set the flag again only when init succeeds.
22
+
23
+ ## Timers
24
+
25
+ - Pass the correct handle to every timer helper. Helpers that compare handles return early on a mismatch, and the event is lost.
26
+ - Declare every C-callable function in its header. Otherwise C compiles an implicit declaration, and the function gets garbage arguments without any error.
27
+ - A 32-bit µs counter wraps after about 71.6 min. Extend it with overflow counting, and test the wrap.
28
+ - Names match types: `delay_us(time_us_t us)`. Don't cast ns into a µs type without converting.
29
+
30
+ ## Checklist
31
+ - [ ] Stores are initialized before they're read; failed loads fall back to defaults
32
+ - [ ] Blank flash is detected
33
+ - [ ] Callbacks null-check globals
34
+ - [ ] Re-init stops users and clears the ready flag first
35
+ - [ ] Timer callbacks pass the right handle; C interfaces are declared in headers
@@ -0,0 +1,21 @@
1
+ # Input and bounds
2
+
3
+ Bytes from UART, SPI, flash or a host are untrusted. One bad length byte can let the host write anywhere in memory.
4
+
5
+ ## Check before you use
6
+
7
+ Before using a received value as a length, index or offset:
8
+
9
+ - Check it against the **real** buffer size, not a "max expected" constant. A `memcpy` or search over the max length reads past a shorter buffer.
10
+ - Guard unsigned subtraction:
11
+ ```cpp
12
+ if (recv_len < HEADER_LEN) return Status::OUT_OF_RANGE;
13
+ size_t data_len = recv_len - HEADER_LEN;
14
+ ```
15
+ - Bound every write into a table (registers, config) against its count, for every element (`start + i < count`), not only the first.
16
+ - Keep these checks compiled in. Don't put them behind a debug-only macro.
17
+
18
+
19
+ ## Checklist
20
+ - [ ] Every external length, index and offset is checked against the real size
21
+ - [ ] No unsigned subtraction without a guard
@@ -0,0 +1,35 @@
1
+ # Registers, units and macros
2
+
3
+ ## Read the datasheet, then copy it exactly
4
+
5
+ - Check every bit position against the datasheet. A reset bit isn't bit 0 just because it's listed first.
6
+ - Read two's-complement registers (trim values, temperature) into signed types (`int8_t`), as the datasheet says.
7
+ - Keep the datasheet formula and section in a `/** */` comment next to the code that uses it.
8
+
9
+ ## Bit edits clear before they set
10
+
11
+ To change a field, clear it and then set it:
12
+ ```cpp
13
+ reg = (reg & ~ODR_MASK) | (odr << ODR_POS);
14
+ ```
15
+ Don't just OR a new value into a register that already holds the old one.
16
+
17
+ ## Units are part of the name
18
+
19
+ - Put the unit in the name or in the comment next to every constant: `ref_uT`, `dt_us`, `heading_rad`.
20
+ - A fallback or reference constant uses the sensor's real output unit (a reference in gauss is wrong for a sensor that outputs µT).
21
+ - Noise and scale conversions keep their units consistent: rad vs deg, magnitude vs magnitude squared.
22
+
23
+ ## Macros are fully parenthesized
24
+
25
+ ```cpp
26
+ #define BUF_SIZE (3 * sizeof(float))
27
+ #define CLIP_U16(x) ((x) > 0xFFFF ? 0xFFFF : (x))
28
+ ```
29
+ Prefer `constexpr` constants and `inline` functions over macros when no token pasting is needed. Derive layout constants (group lengths, offsets) from one source, so nothing is counted twice.
30
+
31
+ ## Checklist
32
+ - [ ] Bit positions and signedness match the datasheet
33
+ - [ ] Field writes clear the old value first
34
+ - [ ] Every constant states its unit, and the unit matches the sensor
35
+ - [ ] Macro bodies and arguments are parenthesized
package/package.json CHANGED
@@ -1,13 +1,20 @@
1
1
  {
2
2
  "name": "@turingcode/cli",
3
- "version": "0.2.13",
3
+ "version": "0.3.5",
4
4
  "description": "Turing Code toolkit for AI agents: check Markdown/CSV tables against table contracts, e.g. `turingcode table check`.",
5
5
  "type": "module",
6
+ "exports": {
7
+ ".": {
8
+ "types": "./dist/lib.d.ts",
9
+ "default": "./dist/lib.js"
10
+ }
11
+ },
6
12
  "bin": {
7
13
  "turingcode": "dist/cli/index.js"
8
14
  },
9
15
  "files": [
10
- "dist"
16
+ "dist",
17
+ "mods"
11
18
  ],
12
19
  "engines": {
13
20
  "node": ">=18"
@@ -23,6 +30,7 @@
23
30
  "access": "public"
24
31
  },
25
32
  "dependencies": {
33
+ "diff": "9.0.0",
26
34
  "sift": "17.1.4",
27
35
  "yargs": "17.7.3"
28
36
  },
@@ -31,4 +39,4 @@
31
39
  "@types/yargs": "17.0.35",
32
40
  "typescript": "7.0.2"
33
41
  }
34
- }
42
+ }