@codyn06/cboard 0.0.1 → 0.2.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.
Files changed (117) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +71 -3
  3. package/THIRD_PARTY_NOTICES +29 -0
  4. package/bin/cboard-context.js +15 -0
  5. package/bin/cboard.js +5 -0
  6. package/dist/adapters/bash.d.ts +2 -0
  7. package/dist/adapters/bash.js +14 -0
  8. package/dist/adapters/c-family.d.ts +2 -0
  9. package/dist/adapters/c-family.js +81 -0
  10. package/dist/adapters/csharp.d.ts +2 -0
  11. package/dist/adapters/csharp.js +47 -0
  12. package/dist/adapters/extra-languages.d.ts +3 -0
  13. package/dist/adapters/extra-languages.js +15 -0
  14. package/dist/adapters/go.d.ts +2 -0
  15. package/dist/adapters/go.js +42 -0
  16. package/dist/adapters/grammar.d.ts +21 -0
  17. package/dist/adapters/grammar.js +54 -0
  18. package/dist/adapters/index.d.ts +20 -0
  19. package/dist/adapters/index.js +62 -0
  20. package/dist/adapters/java.d.ts +2 -0
  21. package/dist/adapters/java.js +36 -0
  22. package/dist/adapters/kotlin.d.ts +2 -0
  23. package/dist/adapters/kotlin.js +65 -0
  24. package/dist/adapters/lua-family.d.ts +2 -0
  25. package/dist/adapters/lua-family.js +186 -0
  26. package/dist/adapters/php.d.ts +2 -0
  27. package/dist/adapters/php.js +28 -0
  28. package/dist/adapters/python.d.ts +2 -0
  29. package/dist/adapters/python.js +68 -0
  30. package/dist/adapters/ruby.d.ts +2 -0
  31. package/dist/adapters/ruby.js +46 -0
  32. package/dist/adapters/rust.d.ts +2 -0
  33. package/dist/adapters/rust.js +85 -0
  34. package/dist/adapters/swift.d.ts +2 -0
  35. package/dist/adapters/swift.js +38 -0
  36. package/dist/adapters/types.d.ts +61 -0
  37. package/dist/adapters/types.js +1 -0
  38. package/dist/adapters/typescript.d.ts +2 -0
  39. package/dist/adapters/typescript.js +52 -0
  40. package/dist/api.d.ts +56 -0
  41. package/dist/api.js +439 -0
  42. package/dist/cboard.d.ts +2 -0
  43. package/dist/cboard.js +151 -0
  44. package/dist/cli.d.ts +27 -0
  45. package/dist/cli.js +180 -0
  46. package/dist/docs-parse.d.ts +64 -0
  47. package/dist/docs-parse.js +762 -0
  48. package/dist/docs.d.ts +39 -0
  49. package/dist/docs.js +243 -0
  50. package/dist/extract.d.ts +36 -0
  51. package/dist/extract.js +480 -0
  52. package/dist/index.d.ts +5 -0
  53. package/dist/index.js +5 -0
  54. package/dist/issues.d.ts +44 -0
  55. package/dist/issues.js +138 -0
  56. package/dist/languages.d.ts +9 -0
  57. package/dist/languages.js +128 -0
  58. package/dist/model.d.ts +85 -0
  59. package/dist/model.js +1 -0
  60. package/dist/wake.d.ts +13 -0
  61. package/dist/wake.js +33 -0
  62. package/grammars/LICENSES/README.md +3 -0
  63. package/grammars/LICENSES/c.LICENSE +21 -0
  64. package/grammars/LICENSES/kotlin.LICENSE +21 -0
  65. package/grammars/LICENSES/lua.LICENSE +21 -0
  66. package/grammars/LICENSES/luau.LICENSE +21 -0
  67. package/grammars/LICENSES/swift.LICENSE +21 -0
  68. package/grammars/manifest.json +39 -0
  69. package/grammars/tree-sitter-c.wasm +0 -0
  70. package/grammars/tree-sitter-kotlin.wasm +0 -0
  71. package/grammars/tree-sitter-lua.wasm +0 -0
  72. package/grammars/tree-sitter-luau.wasm +0 -0
  73. package/grammars/tree-sitter-swift.wasm +0 -0
  74. package/kit/cboard-connect.js +869 -0
  75. package/kit/cboard-kit/brief.js +512 -0
  76. package/kit/cboard-kit/config.js +250 -0
  77. package/kit/cboard-kit/connect-check.js +95 -0
  78. package/kit/cboard-kit/device.js +192 -0
  79. package/kit/cboard-kit/drift.js +55 -0
  80. package/kit/cboard-kit/headers.js +46 -0
  81. package/kit/cboard-kit/mcp.js +97 -0
  82. package/kit/cboard-kit/references.js +70 -0
  83. package/kit/cboard-kit/skill/SKILL.md +170 -0
  84. package/kit/cboard-kit/wait.js +45 -0
  85. package/package.json +47 -5
  86. package/queries/bash/imports.scm +11 -0
  87. package/queries/bash/symbols.scm +1 -0
  88. package/queries/c/imports.scm +2 -0
  89. package/queries/c/symbols.scm +10 -0
  90. package/queries/cpp/imports.scm +2 -0
  91. package/queries/cpp/symbols.scm +11 -0
  92. package/queries/csharp/imports.scm +2 -0
  93. package/queries/csharp/symbols.scm +10 -0
  94. package/queries/go/imports.scm +2 -0
  95. package/queries/go/symbols.scm +8 -0
  96. package/queries/java/imports.scm +2 -0
  97. package/queries/java/symbols.scm +9 -0
  98. package/queries/javascript/imports.scm +3 -0
  99. package/queries/javascript/symbols.scm +16 -0
  100. package/queries/kotlin/imports.scm +2 -0
  101. package/queries/kotlin/symbols.scm +4 -0
  102. package/queries/lua/imports.scm +5 -0
  103. package/queries/lua/symbols.scm +6 -0
  104. package/queries/luau/imports.scm +5 -0
  105. package/queries/luau/symbols.scm +9 -0
  106. package/queries/php/imports.scm +6 -0
  107. package/queries/php/symbols.scm +7 -0
  108. package/queries/python/imports.scm +3 -0
  109. package/queries/python/symbols.scm +8 -0
  110. package/queries/ruby/imports.scm +2 -0
  111. package/queries/ruby/symbols.scm +4 -0
  112. package/queries/rust/imports.scm +5 -0
  113. package/queries/rust/symbols.scm +11 -0
  114. package/queries/swift/imports.scm +2 -0
  115. package/queries/swift/symbols.scm +5 -0
  116. package/queries/typescript/imports.scm +3 -0
  117. package/queries/typescript/symbols.scm +20 -0
@@ -0,0 +1,762 @@
1
+ // Deterministic (no AI) parsers that turn cboard's own Markdown docs into graph nodes and edges for the
2
+ // prototype registry. Pure functions: no I/O, no network, no clock. Used by scripts/import-docs.ts.
3
+ //
4
+ // Only node types and relations that exist in the prototype registry (server/graph.ts) are emitted. When
5
+ // the docs need something the registry lacks, the closest existing type is used and the substitution is
6
+ // recorded in `mismatches` (evidence for a later write-up); the registry itself is never changed.
7
+ export const MAX_BODY_BYTES = 8192; // mirrors server/graph.ts MAX_BODY_BYTES (the client must not import server code)
8
+ // ---------------------------------------------------------------------------
9
+ // Small helpers
10
+ // ---------------------------------------------------------------------------
11
+ const TAG_RE = /\[(Req|Choice|Deferred|Impl|Open)\b[^\]]*\]/g;
12
+ export function splitLines(text) {
13
+ return text.split(/\r?\n/);
14
+ }
15
+ export function stripMd(s) {
16
+ return s
17
+ .replace(/\*\*/g, "")
18
+ .replace(/`/g, "")
19
+ .replace(/\[([^\]]+)\]\([^)]*\)/g, "$1")
20
+ .replace(/\s+/g, " ")
21
+ .trim();
22
+ }
23
+ export function slug(s) {
24
+ return stripMd(s)
25
+ .toLowerCase()
26
+ .replace(/[^a-z0-9]+/g, "-")
27
+ .replace(/^-+|-+$/g, "")
28
+ .slice(0, 48)
29
+ .replace(/-+$/, "");
30
+ }
31
+ function truncWords(s, max) {
32
+ if (s.length <= max)
33
+ return s;
34
+ const cut = s.slice(0, max);
35
+ const sp = cut.lastIndexOf(" ");
36
+ return `${(sp > max / 2 ? cut.slice(0, sp) : cut).replace(/[\s,;:(-]+$/, "")}…`;
37
+ }
38
+ function truncBytes(s, max) {
39
+ if (Buffer.byteLength(s, "utf8") <= max)
40
+ return { text: s, cut: false };
41
+ const marker = "\n\n[truncated: body exceeds MAX_BODY_BYTES]";
42
+ let t = s;
43
+ while (Buffer.byteLength(t + marker, "utf8") > max)
44
+ t = t.slice(0, Math.floor(t.length * 0.95));
45
+ return { text: t + marker, cut: true };
46
+ }
47
+ function tagsOf(s) {
48
+ return [...s.matchAll(TAG_RE)].map((m) => (m[1] ?? "").toLowerCase());
49
+ }
50
+ /** First segment of a text, used as a short title when no bold lead exists. */
51
+ function leadOf(text) {
52
+ const bold = /^\*\*(.+?)\*\*/.exec(text);
53
+ if (bold?.[1]) {
54
+ const inner = stripMd(bold[1]).replace(/[.:]+$/, "");
55
+ if (inner && !/^\[?(Req|Choice|Deferred|Impl|Open)/.test(inner))
56
+ return inner;
57
+ }
58
+ const plain = stripMd(text.replace(TAG_RE, ""));
59
+ const seg = /^(.+?)(?::\s|;\s|\.\s|$)/.exec(plain)?.[1] ?? plain;
60
+ return truncWords(seg.trim(), 64);
61
+ }
62
+ class Collector {
63
+ nodes = [];
64
+ edges = [];
65
+ notes = [];
66
+ titles = new Set();
67
+ keys = new Set();
68
+ mm = new Map();
69
+ edgeKeys = new Set();
70
+ byKey = new Map();
71
+ mismatch(wanted, used, reason) {
72
+ const k = `${wanted}>${used}>${reason}`;
73
+ const m = this.mm.get(k);
74
+ if (m)
75
+ m.count++;
76
+ else
77
+ this.mm.set(k, { wanted, used, reason, count: 1 });
78
+ }
79
+ add(n) {
80
+ let title = n.title;
81
+ let i = 2;
82
+ const norm = (t) => `${n.type}|${t.toLowerCase().replace(/[^a-z0-9]+/g, " ")}`;
83
+ while (this.titles.has(norm(title)))
84
+ title = `${n.title} (${String(i++)})`;
85
+ this.titles.add(norm(title));
86
+ let key = n.key;
87
+ let j = 2;
88
+ while (this.keys.has(key))
89
+ key = `${n.key}-${String(j++)}`;
90
+ this.keys.add(key);
91
+ const body = truncBytes(n.body, MAX_BODY_BYTES);
92
+ if (body.cut)
93
+ this.mismatch("body larger than MAX_BODY_BYTES", "truncated body", `no way to attach a long document section; body cut at ${String(MAX_BODY_BYTES)} bytes`);
94
+ const out = { ...n, key, title, body: body.text };
95
+ this.nodes.push(out);
96
+ this.byKey.set(key, out);
97
+ return out;
98
+ }
99
+ get(key) {
100
+ return this.byKey.get(key);
101
+ }
102
+ edge(e) {
103
+ if (e.from === e.to)
104
+ return;
105
+ const k = `${e.type}|${e.from}|${e.to}`;
106
+ if (this.edgeKeys.has(k))
107
+ return;
108
+ this.edgeKeys.add(k);
109
+ this.edges.push(e);
110
+ }
111
+ hasEdgeBetween(a, b) {
112
+ for (const k of this.edgeKeys) {
113
+ const [, f, t] = k.split("|");
114
+ if ((f === a && t === b) || (f === b && t === a))
115
+ return true;
116
+ }
117
+ return false;
118
+ }
119
+ result() {
120
+ return {
121
+ nodes: this.nodes,
122
+ edges: this.edges,
123
+ mismatches: [...this.mm.values()],
124
+ notes: this.notes,
125
+ };
126
+ }
127
+ }
128
+ const isSeparator = (l) => /^\|[\s:|-]+\|\s*$/.test(l);
129
+ export function splitCells(line) {
130
+ return line
131
+ .trim()
132
+ .replace(/^\|/, "")
133
+ .replace(/\|$/, "")
134
+ .split("|")
135
+ .map((c) => c.trim());
136
+ }
137
+ export function splitUnits(lines, from, to) {
138
+ const units = [];
139
+ let cur;
140
+ let blanks = [];
141
+ let headers;
142
+ const close = () => {
143
+ if (cur)
144
+ units.push(cur);
145
+ cur = undefined;
146
+ blanks = [];
147
+ };
148
+ for (let i = from; i < to; i++) {
149
+ const line = lines[i] ?? "";
150
+ if (line.trim() === "") {
151
+ blanks.push(line);
152
+ continue;
153
+ }
154
+ const pending = blanks;
155
+ blanks = [];
156
+ if (/^#{1,6}\s/.test(line)) {
157
+ close();
158
+ continue;
159
+ }
160
+ if (line.startsWith("|")) {
161
+ close();
162
+ if (isSeparator(line))
163
+ continue;
164
+ if (isSeparator(lines[i + 1] ?? "")) {
165
+ headers = splitCells(line);
166
+ continue;
167
+ }
168
+ units.push({ kind: "row", start: i, end: i, lines: [line], ...(headers ? { headers } : {}) });
169
+ continue;
170
+ }
171
+ headers = undefined;
172
+ const bullet = /^- /.test(line);
173
+ const number = /^(\d+)\. /.exec(line);
174
+ if (bullet || number) {
175
+ close();
176
+ cur = {
177
+ kind: bullet ? "bullet" : "number",
178
+ ...(number?.[1] ? { num: number[1] } : {}),
179
+ start: i,
180
+ end: i,
181
+ lines: [line],
182
+ };
183
+ continue;
184
+ }
185
+ const indented = /^\s/.test(line);
186
+ if (cur && (indented || pending.length === 0)) {
187
+ cur.lines.push(...pending, line);
188
+ cur.end = i;
189
+ continue;
190
+ }
191
+ close();
192
+ cur = { kind: "para", start: i, end: i, lines: [line] };
193
+ }
194
+ close();
195
+ return units;
196
+ }
197
+ export function unitText(u) {
198
+ const [first = "", ...rest] = u.lines;
199
+ const head = u.kind === "bullet"
200
+ ? first.replace(/^- /, "")
201
+ : u.kind === "number"
202
+ ? first.replace(/^\d+\. /, "")
203
+ : first;
204
+ return [head, ...rest.map((l) => l.replace(/^ {1,2}/, ""))].join("\n").trim();
205
+ }
206
+ function sectionsOf(lines) {
207
+ const out = [];
208
+ lines.forEach((l, i) => {
209
+ const m = /^(#{2,3}) (\d+(?:\.\d+)?)\.? (.*)$/.exec(l);
210
+ if (m?.[2] && m[3] !== undefined)
211
+ out.push({
212
+ level: (m[1] ?? "").length,
213
+ num: m[2],
214
+ title: m[3],
215
+ tag: tagsOf(m[3])[0],
216
+ start: i,
217
+ end: lines.length,
218
+ });
219
+ });
220
+ out.forEach((s, i) => {
221
+ const next = out[i + 1];
222
+ if (next)
223
+ s.end = next.start;
224
+ // A level-3 heading inherits its level-2 parent's tag.
225
+ if (s.level === 3 && !s.tag) {
226
+ const parent = [...out.slice(0, i)].reverse().find((p) => p.level === 2);
227
+ s.tag = parent?.tag;
228
+ }
229
+ });
230
+ return out;
231
+ }
232
+ function parseProductDefinition(c, file) {
233
+ const lines = splitLines(file.text);
234
+ for (const sec of sectionsOf(lines)) {
235
+ const top = Number(sec.num.split(".")[0]);
236
+ const units = splitUnits(lines, sec.start + 1, sec.end);
237
+ if (top === 11) {
238
+ for (const u of units) {
239
+ if (u.kind !== "number" || !u.num)
240
+ continue;
241
+ const text = unitText(u);
242
+ const own = text.replace(/^\[(?:Open[^\]]*)\]\s*/, "").replace(/^\(D-\d+\)\s*/, "");
243
+ c.add({
244
+ key: `doc:${file.path}#open-${u.num}`,
245
+ type: "open_issue",
246
+ title: `OD-${u.num} ${leadOfOpen(own)}`,
247
+ body: text,
248
+ status: /^\[Resolved\b/i.test(text) ? "resolved" : "open",
249
+ path: file.path,
250
+ startLine: u.start + 1,
251
+ endLine: u.end + 1,
252
+ });
253
+ }
254
+ continue;
255
+ }
256
+ if (top < 2 || top > 9)
257
+ continue;
258
+ for (const u of units) {
259
+ const text = unitText(u);
260
+ const own = tagsOf(text);
261
+ let inheritedTag;
262
+ if (own.length === 0) {
263
+ if (!sec.tag)
264
+ continue;
265
+ // Plain paragraphs only inherit a heading tag when they are substantial prose.
266
+ if (u.kind === "para" && text.length < 120)
267
+ continue;
268
+ inheritedTag = sec.tag;
269
+ }
270
+ const tag = own[0] ?? inheritedTag ?? "req";
271
+ let title;
272
+ let body = text;
273
+ if (u.kind === "row") {
274
+ const cells = splitCells(u.lines[0] ?? "");
275
+ title = `§${sec.num} ${truncWords(stripMd(cells[0] ?? ""), 64)}`;
276
+ body = cells
277
+ .map((cell, i) => `${u.headers?.[i] ?? `col ${String(i + 1)}`}: ${cell}`)
278
+ .join("\n");
279
+ }
280
+ else if (u.kind === "number") {
281
+ title = `§${sec.num} Rule ${u.num ?? ""}: ${leadOf(text)}`;
282
+ }
283
+ else {
284
+ title = `§${sec.num} ${leadOf(text)}`;
285
+ }
286
+ if (inheritedTag)
287
+ body += `\n\n(tag ${inheritedTag} inherited from the section heading)`;
288
+ c.add({
289
+ key: `doc:${file.path}#s${sec.num}-${slug(title.replace(/^§[\d.]+ /, ""))}`,
290
+ type: "requirement",
291
+ title,
292
+ body,
293
+ status: tag,
294
+ path: file.path,
295
+ startLine: u.start + 1,
296
+ endLine: u.end + 1,
297
+ });
298
+ }
299
+ }
300
+ }
301
+ function leadOfOpen(text) {
302
+ return truncWords(stripMd(text.replace(TAG_RE, "").replace(/^\(D-\d+\)\s*/, "")).split(/;|\.\s/)[0] ?? "", 64);
303
+ }
304
+ // ---------------------------------------------------------------------------
305
+ // docs/decisions/product-redefinition-2026-11-02.md
306
+ // ---------------------------------------------------------------------------
307
+ const ORDINALS = "First|Second|Third|Fourth|Fifth|Sixth|Seventh|Eighth";
308
+ function parseDecisions(c, file) {
309
+ const lines = splitLines(file.text);
310
+ let round = "First round (initial answers)";
311
+ let headers = [];
312
+ lines.forEach((line, i) => {
313
+ const r = new RegExp(`^(${ORDINALS}) round\\b[^:.]*`).exec(line);
314
+ if (r)
315
+ round = r[0].trim();
316
+ if (!line.startsWith("|"))
317
+ return;
318
+ const cells = splitCells(line);
319
+ if (!/^\d+$/.test(cells[0] ?? "")) {
320
+ if (!isSeparator(line) && isSeparator(lines[i + 1] ?? ""))
321
+ headers = cells;
322
+ return;
323
+ }
324
+ const n = cells[0] ?? "";
325
+ const rest = [cells[1] ?? "", cells[2] ?? "", cells.slice(3).join(" | ")];
326
+ const body = [
327
+ `Round: ${round}`,
328
+ ...rest.map((cell, k) => `${headers[k + 1] ?? `col ${String(k + 2)}`}: ${cell}`),
329
+ ].join("\n");
330
+ c.add({
331
+ key: `doc:${file.path}#D-${n}`,
332
+ type: "decision",
333
+ title: `D-${n} ${truncWords(stripMd(rest[0] ?? ""), 70)}`,
334
+ body,
335
+ status: "decided",
336
+ path: file.path,
337
+ startLine: i + 1,
338
+ endLine: i + 1,
339
+ });
340
+ });
341
+ c.mismatch("decision options/rationale fields and partial supersession", "decision body text + qualifies/supersedes edge", "Decision nodes have no options/answer/consequence fields; supersession is whole-node, but D-10/D-6 are superseded only for graph/documents");
342
+ }
343
+ const AMEND_RE = /\b(amends|amended by|extends|extended by|refines|refined by|supersedes|superseded by|reverses|reversed by)\b([^.|\n]{0,200})/gi;
344
+ export function dRefs(s) {
345
+ const out = [];
346
+ for (const m of s.matchAll(/\bD-(\d+)(?:\s*\.\.\s*D-(\d+))?/g)) {
347
+ const a = Number(m[1]);
348
+ const b = m[2] ? Number(m[2]) : a;
349
+ for (let k = a; k <= Math.max(a, b); k++)
350
+ out.push(k);
351
+ }
352
+ return out;
353
+ }
354
+ export function tRefs(s) {
355
+ return [...s.matchAll(/\bT(\d+[bc]?)\b/g)].map((m) => `T${m[1] ?? ""}`);
356
+ }
357
+ function decisionRelations(c, file) {
358
+ const dKey = (n) => `doc:${file.path}#D-${String(n)}`;
359
+ for (const node of c.nodes) {
360
+ if (node.type !== "decision" || node.path !== file.path)
361
+ continue;
362
+ const self = /^D-(\d+)/.exec(node.title)?.[1];
363
+ if (!self)
364
+ continue;
365
+ for (const m of node.body.matchAll(AMEND_RE)) {
366
+ const verb = (m[1] ?? "").toLowerCase();
367
+ const type = /supersede|revers/.test(verb) ? "supersedes" : "qualifies";
368
+ const passive = verb.endsWith(" by");
369
+ for (const n of dRefs(m[2] ?? "")) {
370
+ if (n === Number(self) || !c.get(dKey(n)))
371
+ continue;
372
+ c.edge({
373
+ type,
374
+ from: passive ? dKey(n) : dKey(Number(self)),
375
+ to: passive ? dKey(Number(self)) : dKey(n),
376
+ path: node.path,
377
+ line: node.startLine,
378
+ });
379
+ }
380
+ }
381
+ }
382
+ }
383
+ // ---------------------------------------------------------------------------
384
+ // docs/plans/local-milestone.md
385
+ // ---------------------------------------------------------------------------
386
+ function sentenceAfter(text, label) {
387
+ const at = text.indexOf(label);
388
+ if (at < 0)
389
+ return undefined;
390
+ let depth = 0;
391
+ const from = at + label.length;
392
+ for (let i = from; i < text.length; i++) {
393
+ const ch = text[i];
394
+ if (ch === "(")
395
+ depth++;
396
+ else if (ch === ")")
397
+ depth = Math.max(0, depth - 1);
398
+ else if (ch === "." && depth === 0 && /\s|$/.test(text[i + 1] ?? " "))
399
+ return text.slice(from, i);
400
+ }
401
+ return text.slice(from);
402
+ }
403
+ function expandRows(list, rowKeys) {
404
+ const rows = [];
405
+ const ambiguous = [];
406
+ for (const tok of list.split(/,|\band\b/)) {
407
+ const m = /^\s*(\d+[a-d]?)(?:\s*[–-]\s*(\d+[a-d]?))?\s*$/.exec(tok);
408
+ if (!m?.[1])
409
+ continue;
410
+ const a = m[1];
411
+ const b = m[2];
412
+ if (!b) {
413
+ rows.push(a);
414
+ continue;
415
+ }
416
+ const lo = Number.parseInt(a, 10);
417
+ const hi = Number.parseInt(b, 10);
418
+ for (let k = lo; k <= hi; k++)
419
+ rows.push(String(k));
420
+ for (const key of rowKeys)
421
+ if (/^\d+[a-d]$/.test(key) &&
422
+ Number.parseInt(key, 10) >= lo &&
423
+ Number.parseInt(key, 10) <= hi)
424
+ ambiguous.push(key);
425
+ }
426
+ return { rows, ambiguous };
427
+ }
428
+ function parsePlan(c, file) {
429
+ const lines = splitLines(file.text);
430
+ const heads = [];
431
+ lines.forEach((l, i) => {
432
+ const m = /^(#{1,6}) (.*)$/.exec(l);
433
+ if (m)
434
+ heads.push({ i, level: (m[1] ?? "").length, text: m[2] ?? "" });
435
+ });
436
+ const sectionEnd = (i) => heads.find((h) => h.i > i)?.i ?? lines.length;
437
+ // Tasks
438
+ const tasks = [];
439
+ const taskStarts = lines
440
+ .map((l, i) => ({ i, m: /^\*\*(T\d+[bc]?)\.\s+(.+?)\*\*/.exec(l) }))
441
+ .filter((x) => x.m !== null);
442
+ for (const [k, t] of taskStarts.entries()) {
443
+ let end = Math.min(taskStarts[k + 1]?.i ?? lines.length, sectionEnd(t.i));
444
+ while (end > t.i + 1 && (lines[end - 1] ?? "").trim() === "")
445
+ end--;
446
+ const id = t.m[1] ?? "";
447
+ const name = stripMd(t.m[2] ?? "")
448
+ .replace(/\s+—\s+DEFERRED.*$/, "")
449
+ .replace(/\.$/, "");
450
+ const deferred = /DEFERRED/.test(t.m[2] ?? "");
451
+ const wave = [...heads].reverse().find((h) => h.i < t.i && /^Wave\b/.test(h.text))?.text;
452
+ const text = lines.slice(t.i, end).join("\n");
453
+ const node = c.add({
454
+ key: `doc:${file.path}#${id}`,
455
+ type: "task",
456
+ title: `${id} ${name}`,
457
+ body: wave ? `${text}\n\nWave: ${wave}` : text,
458
+ status: deferred ? "deferred" : "planned",
459
+ path: file.path,
460
+ startLine: t.i + 1,
461
+ endLine: end,
462
+ });
463
+ tasks.push({ id, node, text: text.replace(/\s+/g, " ") });
464
+ }
465
+ // Acceptance rows (section 3 table)
466
+ const rowKeys = new Set();
467
+ const accStart = heads.find((h) => /^3\./.test(h.text));
468
+ if (accStart) {
469
+ for (const u of splitUnits(lines, accStart.i + 1, sectionEnd(accStart.i))) {
470
+ if (u.kind !== "row")
471
+ continue;
472
+ const cells = splitCells(u.lines[0] ?? "");
473
+ const id = cells[0] ?? "";
474
+ if (!/^\d+[a-d]?$/.test(id))
475
+ continue;
476
+ const test = cells.slice(1, -1).join(" | ");
477
+ const kind = cells[cells.length - 1] ?? "";
478
+ rowKeys.add(id);
479
+ c.mismatch("acceptance_test", "requirement", "registry has no acceptance-test node type (nor 'verifies' relation); acceptance rows stored as requirements with status planned/deferred");
480
+ c.add({
481
+ key: `doc:${file.path}#AT-${id}`,
482
+ type: "requirement",
483
+ title: `AT-${id} ${truncWords(stripMd(test), 60)}`,
484
+ body: `${test}\nTest type: ${kind}`,
485
+ status: /^DEFERRED/.test(test) ? "deferred" : "planned",
486
+ path: file.path,
487
+ startLine: u.start + 1,
488
+ endLine: u.end + 1,
489
+ });
490
+ }
491
+ }
492
+ // depends_on and section-3 row citations
493
+ for (const t of tasks) {
494
+ const deps = sentenceAfter(t.text, "Deps:");
495
+ if (deps) {
496
+ for (const d of new Set(tRefs(deps))) {
497
+ const to = `doc:${file.path}#${d}`;
498
+ if (c.get(to) && d !== t.id)
499
+ c.edge({
500
+ type: "depends_on",
501
+ from: t.node.key,
502
+ to,
503
+ path: file.path,
504
+ line: t.node.endLine,
505
+ });
506
+ }
507
+ }
508
+ for (const m of t.text.matchAll(/section 3 rows?\s+([0-9][0-9a-d\s,–-]*)/g)) {
509
+ const { rows, ambiguous } = expandRows(m[1] ?? "", rowKeys);
510
+ for (const r of rows) {
511
+ const to = `doc:${file.path}#AT-${r}`;
512
+ if (c.get(to))
513
+ c.edge({
514
+ type: "references",
515
+ from: t.node.key,
516
+ to,
517
+ path: file.path,
518
+ line: t.node.startLine,
519
+ });
520
+ }
521
+ if (ambiguous.length)
522
+ c.notes.push(`${t.id}: "rows ${(m[1] ?? "").trim()}" is a numeric range; lettered rows ${ambiguous.join(", ")} lie inside it but were NOT linked (ambiguous).`);
523
+ }
524
+ }
525
+ // Risks (section 7)
526
+ const riskStart = heads.find((h) => /^7\./.test(h.text));
527
+ if (riskStart) {
528
+ let n = 0;
529
+ for (const u of splitUnits(lines, riskStart.i + 1, sectionEnd(riskStart.i))) {
530
+ if (u.kind !== "bullet")
531
+ continue;
532
+ n++;
533
+ const text = unitText(u);
534
+ c.add({
535
+ key: `doc:${file.path}#risk-${String(n)}`,
536
+ type: "risk",
537
+ title: `Risk: ${truncWords(stripMd(text).split(/[;.]\s/)[0] ?? text, 64)}`,
538
+ body: text,
539
+ status: "open",
540
+ path: file.path,
541
+ startLine: u.start + 1,
542
+ endLine: u.end + 1,
543
+ });
544
+ }
545
+ }
546
+ }
547
+ // ---------------------------------------------------------------------------
548
+ // docs/research/*.md
549
+ // ---------------------------------------------------------------------------
550
+ function firstSentence(text) {
551
+ const plain = stripMd(text);
552
+ const m = /^(.+?[.!?])(\s|$)/.exec(plain);
553
+ return m?.[1] ?? plain;
554
+ }
555
+ function claimTitle(text) {
556
+ const bold = /^\*\*(.+?)\*\*[:.]?\s*(.*)$/s.exec(text);
557
+ const label = bold?.[1] ? stripMd(bold[1]).replace(/[.:]+$/, "") : "";
558
+ const t = bold?.[2] && label.length <= 40 ? `${label}: ${firstSentence(bold[2])}` : firstSentence(text);
559
+ return truncWords(t.replace(/:\s+(?=[,(])/g, " ").replace(/\s+,/g, ","), 110);
560
+ }
561
+ function parseResearch(c, file) {
562
+ const lines = splitLines(file.text);
563
+ const short = (file.path.split("/").pop() ?? file.path).replace(/\.md$/, "");
564
+ const h1 = lines.findIndex((l) => /^# /.test(l));
565
+ const firstH2 = lines.findIndex((l) => /^## /.test(l));
566
+ const title = stripMd((lines[h1] ?? file.path).replace(/^# /, ""));
567
+ const introEnd = firstH2 > 0 ? firstH2 : Math.min(lines.length, 20);
568
+ const intro = lines
569
+ .slice(h1 + 1, introEnd)
570
+ .join("\n")
571
+ .trim();
572
+ const source = c.add({
573
+ key: `doc:${file.path}#source`,
574
+ type: "source",
575
+ title,
576
+ body: intro,
577
+ status: "recorded",
578
+ path: file.path,
579
+ startLine: Math.max(h1, 0) + 1,
580
+ endLine: introEnd,
581
+ });
582
+ c.mismatch("document / section node", "source (one per file)", "registry has no document or section type; each research file is a Source and individual sections cannot be targets");
583
+ const candidates = [];
584
+ const seen = new Set();
585
+ // (a) top-level items of the first (Summary) section, at most 6
586
+ if (firstH2 >= 0) {
587
+ const secEnd = lines.findIndex((l, i) => i > firstH2 && /^## /.test(l));
588
+ let taken = 0;
589
+ for (const u of splitUnits(lines, firstH2 + 1, secEnd < 0 ? lines.length : secEnd)) {
590
+ if ((u.kind !== "bullet" && u.kind !== "number") || taken >= 6)
591
+ continue;
592
+ const text = unitText(u);
593
+ if (text.length < 40)
594
+ continue;
595
+ candidates.push({ u, text });
596
+ seen.add(u.start);
597
+ taken++;
598
+ }
599
+ }
600
+ // (b) explicit Recommendation lines anywhere, at most 4
601
+ let recs = 0;
602
+ for (const u of splitUnits(lines, 0, lines.length)) {
603
+ if (recs >= 4 || seen.has(u.start) || u.kind === "row")
604
+ continue;
605
+ const text = unitText(u);
606
+ if (!/^(\*\*Recommendation\b|Recommendation:)/.test(text) || text.length < 40)
607
+ continue;
608
+ if (/a proposal, not a fact/.test(text))
609
+ continue; // the label legend, not a finding
610
+ candidates.push({ u, text });
611
+ recs++;
612
+ }
613
+ for (const { u, text } of candidates) {
614
+ const claim = c.add({
615
+ key: `doc:${file.path}#claim-${slug(claimTitle(text))}`,
616
+ type: "claim",
617
+ title: `[${short}] ${claimTitle(text)}`,
618
+ body: text,
619
+ status: "recorded",
620
+ path: file.path,
621
+ startLine: u.start + 1,
622
+ endLine: u.end + 1,
623
+ });
624
+ c.edge({
625
+ type: "derived_from",
626
+ from: claim.key,
627
+ to: source.key,
628
+ path: file.path,
629
+ line: u.start + 1,
630
+ });
631
+ }
632
+ }
633
+ // ---------------------------------------------------------------------------
634
+ // docs/invariants.md
635
+ // ---------------------------------------------------------------------------
636
+ const INVARIANTS = "docs/invariants.md";
637
+ const CURRENT_INVARIANT_SECTION = /^## Implications of the 2026-11-02 product definition/;
638
+ export function invRefs(s) {
639
+ return [...s.matchAll(/\bINV-(\d+)\b/g)].map((m) => `INV-${m[1] ?? ""}`);
640
+ }
641
+ /** Definitions table plus the current 2026-11-02 implications; other sections are Phase 0 history. */
642
+ function parseInvariants(c, file) {
643
+ const lines = splitLines(file.text);
644
+ const h2 = lines.flatMap((l, i) => (/^## /.test(l) ? [i] : []));
645
+ const firstH2 = h2[0] ?? lines.length;
646
+ for (const u of splitUnits(lines, 0, firstH2)) {
647
+ if (u.kind !== "row")
648
+ continue;
649
+ const [id = "", definition = ""] = splitCells(u.lines[0] ?? "");
650
+ if (!/^INV-\d+$/.test(id))
651
+ continue;
652
+ c.add({
653
+ key: `doc:${file.path}#${id}`,
654
+ type: "requirement",
655
+ title: `${id} ${truncWords(stripMd(definition), 70)}`,
656
+ body: `Definition: ${definition}`,
657
+ status: "req",
658
+ path: file.path,
659
+ startLine: u.start + 1,
660
+ endLine: u.end + 1,
661
+ });
662
+ }
663
+ h2.forEach((start, k) => {
664
+ const end = h2[k + 1] ?? lines.length;
665
+ const heading = lines[start] ?? "";
666
+ if (!CURRENT_INVARIANT_SECTION.test(heading)) {
667
+ c.notes.push(`${file.path}: section "${heading.replace(/^## /, "")}" is historical and not imported.`);
668
+ return;
669
+ }
670
+ for (const u of splitUnits(lines, start + 1, end)) {
671
+ if (u.kind !== "bullet")
672
+ continue;
673
+ const text = unitText(u);
674
+ const own = /^\*\*(INV-\d+)\.\*\*\s*(.*)$/s.exec(text);
675
+ const id = own?.[1];
676
+ const defines = id !== undefined && !c.get(`doc:${file.path}#${id}`);
677
+ const lead = leadOf(text);
678
+ c.add({
679
+ key: defines ? `doc:${file.path}#${id}` : `doc:${file.path}#implication-${slug(lead)}`,
680
+ type: "requirement",
681
+ title: defines
682
+ ? `${id} ${truncWords(stripMd(own?.[2] ?? ""), 70)}`
683
+ : `INV implication: ${lead}`,
684
+ body: text,
685
+ status: /\bdeferred\b/i.test(lead) ? "deferred" : "req",
686
+ path: file.path,
687
+ startLine: u.start + 1,
688
+ endLine: u.end + 1,
689
+ });
690
+ }
691
+ });
692
+ }
693
+ // ---------------------------------------------------------------------------
694
+ // Entry point
695
+ // ---------------------------------------------------------------------------
696
+ function crossReferences(c) {
697
+ const taskKeys = new Map();
698
+ const decisionKeys = new Map();
699
+ const invariantKeys = new Map();
700
+ for (const n of c.nodes) {
701
+ const inv = n.key.startsWith(`doc:${INVARIANTS}#`) && /#(INV-\d+)$/.exec(n.key)?.[1];
702
+ if (inv)
703
+ invariantKeys.set(inv, n.key);
704
+ if (n.type === "task") {
705
+ const id = /^(T\d+[bc]?)\b/.exec(n.title)?.[1];
706
+ if (id)
707
+ taskKeys.set(id, n.key);
708
+ }
709
+ if (n.type === "decision") {
710
+ const d = /^D-(\d+)/.exec(n.title)?.[1];
711
+ if (d)
712
+ decisionKeys.set(Number(d), n.key);
713
+ }
714
+ }
715
+ const nodes = [...c.nodes];
716
+ for (const n of nodes) {
717
+ if (!["requirement", "decision", "task", "risk", "open_issue"].includes(n.type))
718
+ continue;
719
+ const text = `${n.title}\n${n.body}`;
720
+ const type = n.type === "task" || n.key.includes("#AT-") ? "references" : "mentions";
721
+ const targets = [
722
+ ...dRefs(text).flatMap((d) => decisionKeys.get(d) ?? []),
723
+ ...tRefs(text).flatMap((t) => taskKeys.get(t) ?? []),
724
+ ...invRefs(text).flatMap((i) => invariantKeys.get(i) ?? []),
725
+ ];
726
+ for (const to of new Set(targets)) {
727
+ if (to === n.key || c.hasEdgeBetween(n.key, to))
728
+ continue;
729
+ c.edge({ type, from: n.key, to, path: n.path, line: n.startLine });
730
+ }
731
+ }
732
+ }
733
+ /** Parse the cboard docs. Files not present in `files` are skipped; order of `files` does not matter. */
734
+ export function parseDocs(files) {
735
+ const c = new Collector();
736
+ const sorted = [...files].sort((a, b) => a.path.localeCompare(b.path));
737
+ const find = (p) => sorted.find((f) => f.path === p);
738
+ const def = find("docs/product-definition.md");
739
+ if (def)
740
+ parseProductDefinition(c, def);
741
+ const dec = find("docs/decisions/product-redefinition-2026-11-02.md");
742
+ if (dec) {
743
+ parseDecisions(c, dec);
744
+ decisionRelations(c, dec);
745
+ }
746
+ const plan = find("docs/plans/local-milestone.md");
747
+ if (plan)
748
+ parsePlan(c, plan);
749
+ for (const f of sorted)
750
+ if (/^docs\/research\/[^/]+\.md$/.test(f.path))
751
+ parseResearch(c, f);
752
+ const inv = find(INVARIANTS);
753
+ if (inv)
754
+ parseInvariants(c, inv);
755
+ crossReferences(c);
756
+ if (c.nodes.some((n) => n.type === "requirement"))
757
+ c.notes.push("section citations (for example 'section 4') are not linked: the registry has no section/document node to point at.");
758
+ return c.result();
759
+ }
760
+ export function provenanceDetail(n, commit) {
761
+ return `lines ${String(n.startLine)}-${String(n.endLine)} @ ${commit}`;
762
+ }