cyber-sdd 0.3.1 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude-plugin/plugin.json +1 -1
- package/.codex-plugin/plugin.json +1 -1
- package/.plugin/plugin.json +1 -1
- package/agents/sdd-automaton.md +5 -1
- package/dist/align-spec/scripts/align-spec.js +102 -0
- package/dist/check-project-specs/scripts/check-project-specs.js +311 -0
- package/dist/check-scenario-overlap/scripts/check-scenario-overlap.js +175 -0
- package/dist/check-spec-references/scripts/check-spec-references.js +285 -0
- package/dist/check-spec-structure/scripts/check-spec-structure.js +241 -0
- package/dist/concept-index/scripts/concept-index.js +181 -0
- package/dist/discover-specs/scripts/discover-specs.js +318 -0
- package/dist/spec-gate/scripts/check-spec-state.js +420 -0
- package/dist/spec-gate/scripts/check-suite.js +317 -0
- package/dist/spec-gate/scripts/classify-edit-class.js +263 -0
- package/license +21 -0
- package/package.json +11 -3
- package/skills/check-project-specs/scripts/check-project-specs.mts +13 -2
- package/skills/concept-index/scripts/concept-index.mts +2 -2
- package/skills/spec-gate/scripts/check-suite.mts +2 -2
- package/skills/spec-gate/scripts/classify-edit-class.mts +7 -7
- package/skills/start-mission/SKILL.md +44 -3
- package/skills/touch-set-correction/scripts/touch-set-correction.mts +7 -7
- package/skills/verify-scenarios/scripts/verify-scenarios.mts +2 -2
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { existsSync, readFileSync, readdirSync, realpathSync } from "node:fs";
|
|
3
|
+
import { dirname, join, relative, resolve } from "node:path";
|
|
4
|
+
import { pathToFileURL } from "node:url";
|
|
5
|
+
//#region skills/check-spec-references/scripts/check-spec-references.mts
|
|
6
|
+
/** Suppresses every reference on the line it appears on. Matched as a COMPLETE html comment, not
|
|
7
|
+
* as a substring: a marker whose name merely starts with this one's (a typo, a future sibling)
|
|
8
|
+
* must not silently inherit its power to hide a reference. The optional `: reason` is convention,
|
|
9
|
+
* not syntax — the marker is recognized with or without one. */
|
|
10
|
+
const IGNORE_MARKER_RE = /<!--\s*spec-ref-ignore\s*(?::[^>]*)?-->/;
|
|
11
|
+
/** A markdown inline link whose target is explicitly relative — covering the plain form, an
|
|
12
|
+
* angle-bracket-wrapped target, and an optional title in any of markdown's three quotings. Run only
|
|
13
|
+
* over the parts of a line that are NOT inside a code span: inside one, markup is literal text on
|
|
14
|
+
* display, not a link. */
|
|
15
|
+
const LINK_RE = /\]\(\s*(?:<([^>\n]*)>|(\.{1,2}\/[^)\s]*))(?:\s+(?:"[^"]*"|'[^']*'|\([^)]*\)))?\s*\)/g;
|
|
16
|
+
/** A reference-style link definition (`[label]: ../path`) whose target is explicitly relative. It
|
|
17
|
+
* is a link target like any other — the label form changes where the path is written, not what it
|
|
18
|
+
* points at. */
|
|
19
|
+
const LINK_DEF_RE = /^\s{0,3}\[.+\]:\s*(?:<([^>\n]*)>|(\S+))/;
|
|
20
|
+
/** An explicitly-relative path, and nothing else. The trailing class excludes whitespace and
|
|
21
|
+
* backticks so that "the span's WHOLE content is the path" means what it says: a span holding
|
|
22
|
+
* `../a` plus commentary is prose that starts with a path, not a citation. */
|
|
23
|
+
const RELATIVE_RE = /^\.{1,2}\/[^\s`]*$/;
|
|
24
|
+
/** The same, minus the no-whitespace rule — for an ANGLE-BRACKET link target, whose whole reason
|
|
25
|
+
* for existing is to carry a path with a space in it. */
|
|
26
|
+
const RELATIVE_IN_ANGLES_RE = /^\.{1,2}\/[^`]*$/;
|
|
27
|
+
/**
|
|
28
|
+
* The fence a line opens, or `undefined`. CommonMark: three or more backticks or tildes, indented
|
|
29
|
+
* fewer than four spaces.
|
|
30
|
+
*/
|
|
31
|
+
function fenceOpenedBy(line) {
|
|
32
|
+
const m = /^ {0,3}(`{3,}|~{3,})/.exec(line);
|
|
33
|
+
if (m === null) return void 0;
|
|
34
|
+
const run = m[1];
|
|
35
|
+
return {
|
|
36
|
+
char: run[0],
|
|
37
|
+
length: run.length
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Whether `line` CLOSES `fence`: the same character, at least as long, and nothing but whitespace
|
|
42
|
+
* after it.
|
|
43
|
+
*
|
|
44
|
+
* Matching the delimiter rather than toggling a boolean on any fence-shaped line is what keeps the
|
|
45
|
+
* exclusion honest. A bare toggle flips on a `~~~` line inside a backtick fence — so a reference
|
|
46
|
+
* genuinely inside a fence gets extracted, and, worse, a fence-shaped line inside another fence
|
|
47
|
+
* leaves the parity inverted and every genuinely broken reference AFTER the block is silently
|
|
48
|
+
* swallowed. That is this engine's own failure class, reached through the one rule meant to
|
|
49
|
+
* suppress noise.
|
|
50
|
+
*/
|
|
51
|
+
function fenceClosedBy(line, fence) {
|
|
52
|
+
const m = /^ {0,3}(`{3,}|~{3,})\s*$/.exec(line);
|
|
53
|
+
if (m === null) return false;
|
|
54
|
+
const run = m[1];
|
|
55
|
+
return run[0] === fence.char && run.length >= fence.length;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* A line that starts a new block: blank, an ATX heading, a blockquote, a list item, or a thematic
|
|
59
|
+
* break. Inline parsing happens WITHIN a block, so a code span cannot reach across one of these —
|
|
60
|
+
* and the boundary matters in the under-reporting direction. An unclosed backtick left by a typo
|
|
61
|
+
* would otherwise pair with the first backtick of the next block, swallowing the span that was
|
|
62
|
+
* about to open and every reference after it in the flow.
|
|
63
|
+
*/
|
|
64
|
+
function startsNewBlock(line) {
|
|
65
|
+
if (/^[ \t]*$/.test(line)) return true;
|
|
66
|
+
if (/^ {0,3}#{1,6}(\s|$)/.test(line)) return true;
|
|
67
|
+
if (/^ {0,3}>/.test(line)) return true;
|
|
68
|
+
if (/^ {0,3}([-+*]|\d{1,9}[.)])(\s|$)/.test(line)) return true;
|
|
69
|
+
if (/^ {0,3}((\*[ \t]*){3,}|(-[ \t]*){3,}|(_[ \t]*){3,})$/.test(line)) return true;
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
/** Same length, same line breaks, no content — so blanking a region shifts no offset and moves no
|
|
73
|
+
* finding to another line. */
|
|
74
|
+
function blankOut(region) {
|
|
75
|
+
return region.replace(/[^\n]/g, " ");
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* The inline-code spans on one line, scanned the way CommonMark delimits them: a run of N backticks
|
|
79
|
+
* opens, the next run of EXACTLY N closes, and one leading + trailing space is stripped when both
|
|
80
|
+
* are present.
|
|
81
|
+
*
|
|
82
|
+
* Scanning properly rather than matching a single-backtick pair is what makes the reference rule —
|
|
83
|
+
* "a code span is a reference when its WHOLE content is the path" — mean one thing everywhere. A
|
|
84
|
+
* doubled span written around another span (`` `x` ``) has content that still carries backticks, so
|
|
85
|
+
* it is not a path and not a reference; a doubled span written around a bare path has the path as
|
|
86
|
+
* its content, so it IS one. Neither is a special case, and neither leaves a broken reference
|
|
87
|
+
* anywhere to hide.
|
|
88
|
+
*/
|
|
89
|
+
function scanCodeSpans(text, blockStarts = []) {
|
|
90
|
+
const out = [];
|
|
91
|
+
let i = 0;
|
|
92
|
+
while (i < text.length) {
|
|
93
|
+
if (text[i] !== "`") {
|
|
94
|
+
i++;
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
const open = i;
|
|
98
|
+
while (text[i] === "`") i++;
|
|
99
|
+
const runLength = i - open;
|
|
100
|
+
const limit = blockStarts.find((b) => b > open) ?? text.length;
|
|
101
|
+
let j = i;
|
|
102
|
+
let closeStart = -1;
|
|
103
|
+
while (j < limit) {
|
|
104
|
+
if (text[j] !== "`") {
|
|
105
|
+
j++;
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
const runStart = j;
|
|
109
|
+
while (text[j] === "`") j++;
|
|
110
|
+
if (j - runStart === runLength) {
|
|
111
|
+
closeStart = runStart;
|
|
112
|
+
break;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
if (closeStart === -1) {
|
|
116
|
+
i = open + runLength;
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
let content = text.slice(open + runLength, closeStart).replace(/\r?\n/g, " ");
|
|
120
|
+
if (content.length > 1 && content.startsWith(" ") && content.endsWith(" ") && content.trim() !== "") content = content.slice(1, -1);
|
|
121
|
+
out.push({
|
|
122
|
+
content,
|
|
123
|
+
start: open,
|
|
124
|
+
end: j
|
|
125
|
+
});
|
|
126
|
+
i = j;
|
|
127
|
+
}
|
|
128
|
+
return out;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Every explicitly-relative reference in `text`, in document order.
|
|
132
|
+
*
|
|
133
|
+
* Skipped: lines inside a fenced code block, and lines carrying the ignore marker. Extraction is
|
|
134
|
+
* line-scoped on purpose — it is what gives each finding a line number, and what lets the marker's
|
|
135
|
+
* scope be exactly the line it appears on rather than the whole file.
|
|
136
|
+
*/
|
|
137
|
+
function extractReferences(text) {
|
|
138
|
+
const lines = text.split("\n");
|
|
139
|
+
let fence;
|
|
140
|
+
const unfenced = lines.map((line) => {
|
|
141
|
+
if (fence !== void 0) {
|
|
142
|
+
if (fenceClosedBy(line, fence)) fence = void 0;
|
|
143
|
+
return blankOut(line);
|
|
144
|
+
}
|
|
145
|
+
const opened = fenceOpenedBy(line);
|
|
146
|
+
if (opened !== void 0) {
|
|
147
|
+
fence = opened;
|
|
148
|
+
return blankOut(line);
|
|
149
|
+
}
|
|
150
|
+
return line;
|
|
151
|
+
});
|
|
152
|
+
const scannable = unfenced.join("\n");
|
|
153
|
+
const lineStarts = [];
|
|
154
|
+
let acc = 0;
|
|
155
|
+
for (const line of lines) {
|
|
156
|
+
lineStarts.push(acc);
|
|
157
|
+
acc += line.length + 1;
|
|
158
|
+
}
|
|
159
|
+
const blockStarts = lineStarts.filter((_start, i) => i > 0 && startsNewBlock(unfenced[i]));
|
|
160
|
+
const lineOf = (offset) => {
|
|
161
|
+
let lo = 0;
|
|
162
|
+
let hi = lineStarts.length - 1;
|
|
163
|
+
while (lo < hi) {
|
|
164
|
+
const mid = lo + hi + 1 >> 1;
|
|
165
|
+
if (lineStarts[mid] <= offset) lo = mid;
|
|
166
|
+
else hi = mid - 1;
|
|
167
|
+
}
|
|
168
|
+
return lo;
|
|
169
|
+
};
|
|
170
|
+
const spans = scanCodeSpans(scannable, blockStarts);
|
|
171
|
+
let outsideSpans = scannable;
|
|
172
|
+
for (const span of spans) outsideSpans = outsideSpans.slice(0, span.start) + blankOut(outsideSpans.slice(span.start, span.end)) + outsideSpans.slice(span.end);
|
|
173
|
+
const outsideLines = outsideSpans.split("\n");
|
|
174
|
+
const marked = outsideLines.map((line) => IGNORE_MARKER_RE.test(line));
|
|
175
|
+
const perLine = /* @__PURE__ */ new Map();
|
|
176
|
+
const add = (line, ref) => {
|
|
177
|
+
if (marked[line] === true) return;
|
|
178
|
+
const set = perLine.get(line) ?? /* @__PURE__ */ new Set();
|
|
179
|
+
set.add(ref);
|
|
180
|
+
perLine.set(line, set);
|
|
181
|
+
};
|
|
182
|
+
for (const span of spans) {
|
|
183
|
+
const content = span.content.trim();
|
|
184
|
+
if (RELATIVE_RE.test(content)) add(lineOf(span.start), content);
|
|
185
|
+
}
|
|
186
|
+
outsideLines.forEach((line, i) => {
|
|
187
|
+
const addTarget = (angled, bare) => {
|
|
188
|
+
if (angled !== void 0) {
|
|
189
|
+
if (RELATIVE_IN_ANGLES_RE.test(angled)) add(i, angled);
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
if (bare !== void 0 && RELATIVE_RE.test(bare)) add(i, bare);
|
|
193
|
+
};
|
|
194
|
+
for (const m of line.matchAll(LINK_RE)) addTarget(m[1], m[2]);
|
|
195
|
+
const def = LINK_DEF_RE.exec(line);
|
|
196
|
+
if (def) addTarget(def[1], def[2]);
|
|
197
|
+
});
|
|
198
|
+
const out = [];
|
|
199
|
+
for (const line of [...perLine.keys()].sort((a, b) => a - b)) for (const ref of perLine.get(line)) out.push({
|
|
200
|
+
line: line + 1,
|
|
201
|
+
ref
|
|
202
|
+
});
|
|
203
|
+
return out;
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Where `ref` points, resolved against `fileDir` — the directory of the file that CARRIES it,
|
|
207
|
+
* never the spec root and never the repo root. A trailing `#fragment` is stripped first; a
|
|
208
|
+
* trailing slash is immaterial (`resolve` drops it), so a directory reference resolves either way.
|
|
209
|
+
*/
|
|
210
|
+
function resolveReference(fileDir, ref) {
|
|
211
|
+
const withoutFragment = ref.replace(/#.*$/, "");
|
|
212
|
+
return resolve(fileDir, withoutFragment);
|
|
213
|
+
}
|
|
214
|
+
/** Every `.md` file under `dir`, at any depth, absolute and sorted so the report is stable. */
|
|
215
|
+
function listMarkdownFiles(dir) {
|
|
216
|
+
const out = [];
|
|
217
|
+
const walk = (d) => {
|
|
218
|
+
for (const e of readdirSync(d, { withFileTypes: true }).sort((a, b) => a.name < b.name ? -1 : 1)) {
|
|
219
|
+
const p = join(d, e.name);
|
|
220
|
+
if (e.isDirectory()) walk(p);
|
|
221
|
+
else if (e.name.endsWith(".md")) out.push(p);
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
walk(dir);
|
|
225
|
+
return out;
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Every unresolved reference under `specDir`, ordered by file, then line, then reference — so two
|
|
229
|
+
* runs over an unchanged tree render byte-identically.
|
|
230
|
+
*
|
|
231
|
+
* EVERY finding, never the first: a single off-by-one lands as a whole family of broken
|
|
232
|
+
* references, and reporting one at a time would take as many runs to clear as there are levels
|
|
233
|
+
* wrong.
|
|
234
|
+
*/
|
|
235
|
+
function audit(specDir, options = {}) {
|
|
236
|
+
const list = options.listMarkdownFiles ?? listMarkdownFiles;
|
|
237
|
+
const read = options.readFile ?? ((p) => readFileSync(p, "utf8"));
|
|
238
|
+
const has = options.exists ?? existsSync;
|
|
239
|
+
const findings = [];
|
|
240
|
+
for (const file of list(specDir)) {
|
|
241
|
+
const fileDir = dirname(file);
|
|
242
|
+
for (const { line, ref } of extractReferences(read(file))) {
|
|
243
|
+
const resolved = resolveReference(fileDir, ref);
|
|
244
|
+
if (!has(resolved)) findings.push({
|
|
245
|
+
file,
|
|
246
|
+
line,
|
|
247
|
+
ref,
|
|
248
|
+
resolved
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
return findings.sort((a, b) => a.file !== b.file ? a.file < b.file ? -1 : 1 : a.line !== b.line ? a.line - b.line : a.ref < b.ref ? -1 : 1);
|
|
253
|
+
}
|
|
254
|
+
/** Renders each finding as `file:line: <ref> -> <resolved>`, both paths relative to `base` so the
|
|
255
|
+
* report reads the same from any checkout. */
|
|
256
|
+
function formatFindings(findings, base) {
|
|
257
|
+
if (findings.length === 0) return "check-spec-references: every relative reference resolves\n";
|
|
258
|
+
const lines = findings.map((f) => ` ${relative(base, f.file)}:${f.line}: \`${f.ref}\` -> ${relative(base, f.resolved)} — no file or directory there\n`);
|
|
259
|
+
lines.push(`check-spec-references: ${findings.length} unresolved reference(s)\n`);
|
|
260
|
+
return lines.join("");
|
|
261
|
+
}
|
|
262
|
+
function main(argv, cwd = process.cwd()) {
|
|
263
|
+
const i = argv.indexOf("--spec-dir");
|
|
264
|
+
const specDir = i === -1 ? "" : argv[i + 1] ?? "";
|
|
265
|
+
if (specDir === "") {
|
|
266
|
+
process.stderr.write("check-spec-references: --spec-dir <dir> is required\n");
|
|
267
|
+
return 1;
|
|
268
|
+
}
|
|
269
|
+
const dir = resolve(cwd, specDir);
|
|
270
|
+
if (!existsSync(dir)) {
|
|
271
|
+
process.stderr.write(`check-spec-references: no directory at ${relative(cwd, dir)}\n`);
|
|
272
|
+
return 1;
|
|
273
|
+
}
|
|
274
|
+
const findings = audit(dir);
|
|
275
|
+
const report = formatFindings(findings, cwd);
|
|
276
|
+
if (findings.length === 0) {
|
|
277
|
+
process.stdout.write(report);
|
|
278
|
+
return 0;
|
|
279
|
+
}
|
|
280
|
+
process.stderr.write(report);
|
|
281
|
+
return 1;
|
|
282
|
+
}
|
|
283
|
+
if (process.argv[1] && import.meta.url === pathToFileURL(realpathSync(process.argv[1])).href) process.exit(main(process.argv.slice(2)));
|
|
284
|
+
//#endregion
|
|
285
|
+
export { audit, extractReferences, formatFindings, listMarkdownFiles, main, resolveReference, scanCodeSpans, startsNewBlock };
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { existsSync, readFileSync, readdirSync, realpathSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { pathToFileURL } from "node:url";
|
|
5
|
+
//#region skills/check-spec-structure/scripts/check-spec-structure.mts
|
|
6
|
+
const SKIP_DIRS = /* @__PURE__ */ new Set([
|
|
7
|
+
"node_modules",
|
|
8
|
+
".git",
|
|
9
|
+
"dist",
|
|
10
|
+
".turbo",
|
|
11
|
+
".next",
|
|
12
|
+
"coverage"
|
|
13
|
+
]);
|
|
14
|
+
const DEFAULT_MAX_SCENARIOS = 40;
|
|
15
|
+
const REQUIRED_BEHAVIORAL_SECTIONS = [
|
|
16
|
+
"What",
|
|
17
|
+
"Use Cases",
|
|
18
|
+
"Control Flow",
|
|
19
|
+
"Scenario map"
|
|
20
|
+
];
|
|
21
|
+
function parseNodeFrontmatter(text) {
|
|
22
|
+
const m = /^---\r?\n([\s\S]*?)\r?\n---\s*(?:\r?\n|$)/.exec(text);
|
|
23
|
+
if (!m) return { concepts: [] };
|
|
24
|
+
const fm = { concepts: [] };
|
|
25
|
+
const lines = m[1].split("\n").map((l) => l.replace(/\r$/, ""));
|
|
26
|
+
for (let i = 0; i < lines.length; i++) {
|
|
27
|
+
const line = lines[i];
|
|
28
|
+
if (line.trim() === "" || line.trim().startsWith("#")) continue;
|
|
29
|
+
if (line.length - line.trimStart().length !== 0) continue;
|
|
30
|
+
const [key, ...rest] = line.trim().split(":");
|
|
31
|
+
const value = rest.join(":").trim();
|
|
32
|
+
if (key === "spec-type") fm.specType = unquote(value);
|
|
33
|
+
else if (key === "concept") {
|
|
34
|
+
if (value === "" || value === "|" || value === ">") for (let j = i + 1; j < lines.length; j++) {
|
|
35
|
+
const item = lines[j];
|
|
36
|
+
if (item.trim() === "") continue;
|
|
37
|
+
if (item.length - item.trimStart().length === 0) break;
|
|
38
|
+
const dash = /^\s*-\s+(.*)$/.exec(item);
|
|
39
|
+
if (dash) fm.concepts.push(unquote(dash[1].trim()));
|
|
40
|
+
}
|
|
41
|
+
else fm.concepts.push(...parseScalarOrFlow(value));
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return fm;
|
|
45
|
+
}
|
|
46
|
+
function parseScalarOrFlow(value) {
|
|
47
|
+
const v = value.trim();
|
|
48
|
+
if (v.startsWith("[") && v.endsWith("]")) return v.slice(1, -1).split(",").map((s) => unquote(s.trim())).filter((s) => s.length > 0);
|
|
49
|
+
const single = unquote(v);
|
|
50
|
+
return single.length > 0 ? [single] : [];
|
|
51
|
+
}
|
|
52
|
+
function unquote(v) {
|
|
53
|
+
return v.replace(/^["']|["']$/g, "");
|
|
54
|
+
}
|
|
55
|
+
function parseSectionHeadings(text) {
|
|
56
|
+
const headings = [];
|
|
57
|
+
let inFence = false;
|
|
58
|
+
for (const raw of text.split("\n")) {
|
|
59
|
+
const line = raw.replace(/\r$/, "");
|
|
60
|
+
if (/^\s*(```|~~~)/.test(line)) {
|
|
61
|
+
inFence = !inFence;
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
if (inFence) continue;
|
|
65
|
+
const m = /^##\s+(.+?)\s*$/.exec(line);
|
|
66
|
+
if (m) headings.push(m[1].replace(/`/g, "").trim());
|
|
67
|
+
}
|
|
68
|
+
return headings;
|
|
69
|
+
}
|
|
70
|
+
function countScenarios(featureText) {
|
|
71
|
+
let n = 0;
|
|
72
|
+
for (const line of featureText.split("\n")) if (/^\s*Scenario:/.test(line)) n++;
|
|
73
|
+
return n;
|
|
74
|
+
}
|
|
75
|
+
const SECTION_HEADER = /^\s*#\s*(?:──|-{3,})/;
|
|
76
|
+
function profileFeature(featureText) {
|
|
77
|
+
const lines = featureText.split("\n");
|
|
78
|
+
let scenarioCount = 0;
|
|
79
|
+
let taggedCount = 0;
|
|
80
|
+
let clusterCount = 0;
|
|
81
|
+
let pendingRubric = false;
|
|
82
|
+
for (const line of lines) {
|
|
83
|
+
if (/^\s*Scenario:/.test(line)) {
|
|
84
|
+
scenarioCount++;
|
|
85
|
+
if (pendingRubric) taggedCount++;
|
|
86
|
+
pendingRubric = false;
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
const trimmed = line.trim();
|
|
90
|
+
if (trimmed === "") continue;
|
|
91
|
+
if (trimmed.startsWith("@")) {
|
|
92
|
+
if (/(^|\s)@rubric(\s|$)/.test(trimmed)) pendingRubric = true;
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
if (SECTION_HEADER.test(line)) {
|
|
96
|
+
clusterCount++;
|
|
97
|
+
pendingRubric = false;
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
pendingRubric = false;
|
|
101
|
+
}
|
|
102
|
+
return {
|
|
103
|
+
scenarioCount,
|
|
104
|
+
plainCount: scenarioCount - taggedCount,
|
|
105
|
+
taggedCount,
|
|
106
|
+
clusterCount
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
function displayPath(relPath) {
|
|
110
|
+
const p = relPath.replace(/\\/g, "/");
|
|
111
|
+
return p.endsWith("/README.md") ? p.slice(0, -9) : p;
|
|
112
|
+
}
|
|
113
|
+
function scanProjectSpec(specDir) {
|
|
114
|
+
const records = [];
|
|
115
|
+
walk(specDir, specDir, records);
|
|
116
|
+
return records.sort((a, b) => a.display.localeCompare(b.display));
|
|
117
|
+
}
|
|
118
|
+
function walk(dir, specDir, out) {
|
|
119
|
+
const entries = readdirSync(dir, { withFileTypes: true });
|
|
120
|
+
for (const entry of entries) {
|
|
121
|
+
if (entry.name.startsWith(".") || SKIP_DIRS.has(entry.name)) continue;
|
|
122
|
+
const full = join(dir, entry.name);
|
|
123
|
+
if (entry.isDirectory()) walk(full, specDir, out);
|
|
124
|
+
else if (entry.name === "README.md") {
|
|
125
|
+
const readmeText = readFileSync(full, "utf8");
|
|
126
|
+
const fm = parseNodeFrontmatter(readmeText);
|
|
127
|
+
if (fm.specType === void 0 && fm.concepts.length === 0) continue;
|
|
128
|
+
const sectionHeadings = parseSectionHeadings(readmeText);
|
|
129
|
+
const features = entries.filter((e) => e.isFile() && e.name.endsWith(".feature"));
|
|
130
|
+
const hasFeature = features.length > 0;
|
|
131
|
+
const profile = hasFeature ? profileFeature(readFileSync(join(dir, features[0].name), "utf8")) : {
|
|
132
|
+
scenarioCount: 0,
|
|
133
|
+
plainCount: 0,
|
|
134
|
+
taggedCount: 0,
|
|
135
|
+
clusterCount: 0
|
|
136
|
+
};
|
|
137
|
+
const relPath = full.slice(specDir.length + 1).replace(/\\/g, "/");
|
|
138
|
+
out.push({
|
|
139
|
+
relPath,
|
|
140
|
+
capability: relPath.split("/")[0],
|
|
141
|
+
display: displayPath(relPath),
|
|
142
|
+
concepts: fm.concepts,
|
|
143
|
+
specType: fm.specType,
|
|
144
|
+
sectionHeadings,
|
|
145
|
+
hasFeature,
|
|
146
|
+
scenarioCount: profile.scenarioCount,
|
|
147
|
+
plainCount: profile.plainCount,
|
|
148
|
+
taggedCount: profile.taggedCount,
|
|
149
|
+
clusterCount: profile.clusterCount
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
function checkUntagged(records) {
|
|
155
|
+
return records.filter((r) => r.specType !== void 0 && r.concepts.length === 0).map((r) => ({
|
|
156
|
+
kind: "untagged-node",
|
|
157
|
+
severity: "blocking",
|
|
158
|
+
node: r.display,
|
|
159
|
+
detail: `spec-type: ${r.specType} but no concept tag — orphaned from the by-concept index`
|
|
160
|
+
}));
|
|
161
|
+
}
|
|
162
|
+
function checkOversized(records, maxScenarios) {
|
|
163
|
+
return records.filter((r) => r.scenarioCount > maxScenarios).map((r) => ({
|
|
164
|
+
kind: "oversized-node",
|
|
165
|
+
severity: "advisory",
|
|
166
|
+
node: r.display,
|
|
167
|
+
detail: `${r.scenarioCount} scenarios > ${maxScenarios} — shape profile: plain ${r.plainCount}, tagged ${r.taggedCount}, clusters ${r.clusterCount} (soft breadth hint); the Warden routes breadth-vs-depth`
|
|
168
|
+
}));
|
|
169
|
+
}
|
|
170
|
+
function checkGlossary(specDir) {
|
|
171
|
+
if (existsSync(join(specDir, "glossary.md"))) return [];
|
|
172
|
+
return [{
|
|
173
|
+
kind: "missing-glossary",
|
|
174
|
+
severity: "advisory",
|
|
175
|
+
node: "glossary.md",
|
|
176
|
+
detail: "no root glossary.md — the project has no home for its ubiquitous language, so a term can be used without ever being defined; a root file, never a folder"
|
|
177
|
+
}];
|
|
178
|
+
}
|
|
179
|
+
function checkIncomplete(records) {
|
|
180
|
+
return records.filter((r) => r.specType === "behavioral" && r.hasFeature).map((r) => {
|
|
181
|
+
return {
|
|
182
|
+
record: r,
|
|
183
|
+
missing: REQUIRED_BEHAVIORAL_SECTIONS.filter((s) => !r.sectionHeadings.includes(s))
|
|
184
|
+
};
|
|
185
|
+
}).filter(({ missing }) => missing.length > 0).map(({ record, missing }) => ({
|
|
186
|
+
kind: "incomplete-node",
|
|
187
|
+
severity: "advisory",
|
|
188
|
+
node: record.display,
|
|
189
|
+
detail: `behavioral leaf spec missing required section(s): ${missing.map((s) => `## ${s}`).join(", ")} — a spec that stops at ## Use Cases never draws its CFG or scenario map (sdd:spec-format-governance)`
|
|
190
|
+
}));
|
|
191
|
+
}
|
|
192
|
+
function audit(records, maxScenarios, specDir) {
|
|
193
|
+
return [
|
|
194
|
+
...checkUntagged(records),
|
|
195
|
+
...checkOversized(records, maxScenarios),
|
|
196
|
+
...checkIncomplete(records),
|
|
197
|
+
...specDir === void 0 ? [] : checkGlossary(specDir)
|
|
198
|
+
];
|
|
199
|
+
}
|
|
200
|
+
function hasBlocking(findings) {
|
|
201
|
+
return findings.some((f) => f.severity === "blocking");
|
|
202
|
+
}
|
|
203
|
+
function renderFindings(findings, specDir) {
|
|
204
|
+
const blocking = findings.filter((f) => f.severity === "blocking");
|
|
205
|
+
const advisory = findings.filter((f) => f.severity === "advisory");
|
|
206
|
+
const lines = [`check-spec-structure: spec-dir=${specDir}`];
|
|
207
|
+
lines.push(`blocking[${blocking.length}]:`);
|
|
208
|
+
for (const f of blocking) lines.push(` ${f.node} — ${f.kind}: ${f.detail}`);
|
|
209
|
+
lines.push(`advisory[${advisory.length}]:`);
|
|
210
|
+
for (const f of advisory) lines.push(` ${f.node} — ${f.kind}: ${f.detail}`);
|
|
211
|
+
lines.push("note: advisory — findings feed the Warden formation pass; the engine writes nothing");
|
|
212
|
+
return lines.join("\n");
|
|
213
|
+
}
|
|
214
|
+
function main(argv) {
|
|
215
|
+
let specDir = ".";
|
|
216
|
+
let mode = "audit";
|
|
217
|
+
let format = "toon";
|
|
218
|
+
let maxScenarios = 40;
|
|
219
|
+
for (let i = 0; i < argv.length; i++) {
|
|
220
|
+
const a = argv[i];
|
|
221
|
+
if (a === "--spec-dir") specDir = argv[++i] ?? ".";
|
|
222
|
+
else if (a === "--check") mode = "check";
|
|
223
|
+
else if (a === "--max-scenarios") maxScenarios = Number(argv[++i] ?? 40);
|
|
224
|
+
else if (a === "--format") format = argv[++i] ?? "toon";
|
|
225
|
+
}
|
|
226
|
+
const findings = audit(scanProjectSpec(specDir), maxScenarios, specDir);
|
|
227
|
+
if (mode === "check") {
|
|
228
|
+
if (hasBlocking(findings)) {
|
|
229
|
+
process.stderr.write(`check-spec-structure: ${findings.filter((f) => f.severity === "blocking").length} blocking finding(s)\n`);
|
|
230
|
+
return 1;
|
|
231
|
+
}
|
|
232
|
+
process.stdout.write("check-spec-structure: no blocking findings\n");
|
|
233
|
+
return 0;
|
|
234
|
+
}
|
|
235
|
+
if (format === "json") process.stdout.write(`${JSON.stringify(findings)}\n`);
|
|
236
|
+
else process.stdout.write(`${renderFindings(findings, specDir)}\n`);
|
|
237
|
+
return 0;
|
|
238
|
+
}
|
|
239
|
+
if (process.argv[1] && import.meta.url === pathToFileURL(realpathSync(process.argv[1])).href) process.exit(main(process.argv.slice(2)));
|
|
240
|
+
//#endregion
|
|
241
|
+
export { DEFAULT_MAX_SCENARIOS, REQUIRED_BEHAVIORAL_SECTIONS, audit, checkGlossary, checkIncomplete, checkOversized, checkUntagged, countScenarios, hasBlocking, main, parseNodeFrontmatter, parseScalarOrFlow, parseSectionHeadings, profileFeature, renderFindings, scanProjectSpec };
|