cyber-sdd 0.4.0 → 0.4.2

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 (29) hide show
  1. package/.claude-plugin/plugin.json +1 -1
  2. package/.codex-plugin/plugin.json +1 -1
  3. package/.plugin/plugin.json +1 -1
  4. package/agents/sdd-automaton.md +5 -1
  5. package/dist/align-spec/scripts/align-spec.js +102 -0
  6. package/dist/check-project-specs/scripts/check-project-specs.js +311 -0
  7. package/dist/check-scenario-overlap/scripts/check-scenario-overlap.js +175 -0
  8. package/dist/check-spec-references/scripts/check-spec-references.js +285 -0
  9. package/dist/check-spec-structure/scripts/check-spec-structure.js +241 -0
  10. package/dist/concept-index/scripts/concept-index.js +181 -0
  11. package/dist/discover-specs/scripts/discover-specs.js +318 -0
  12. package/dist/spec-gate/scripts/check-spec-state.js +420 -0
  13. package/dist/spec-gate/scripts/check-suite.js +317 -0
  14. package/dist/spec-gate/scripts/classify-edit-class.js +263 -0
  15. package/dist/spec-gate/vendor/gherkin-cli.js +6369 -0
  16. package/license +21 -0
  17. package/package.json +12 -3
  18. package/skills/check-project-specs/scripts/check-project-specs.mts +13 -2
  19. package/skills/concept-index/scripts/concept-index.mts +2 -2
  20. package/skills/spec-gate/README.md +9 -0
  21. package/skills/spec-gate/scripts/check-suite.mts +3 -2
  22. package/skills/spec-gate/scripts/classify-edit-class.mts +7 -7
  23. package/skills/spec-gate/vendor/gherkin-cli.d.mts +2 -0
  24. package/skills/spec-gate/vendor/gherkin-cli.mjs +6545 -0
  25. package/skills/start-mission/SKILL.md +1 -0
  26. package/skills/touch-set-correction/README.md +5 -0
  27. package/skills/touch-set-correction/scripts/touch-set-correction.mts +7 -7
  28. package/skills/verify-scenarios/README.md +6 -1
  29. package/skills/verify-scenarios/scripts/verify-scenarios.mts +2 -2
@@ -0,0 +1,420 @@
1
+ #!/usr/bin/env node
2
+ import { execFileSync } from "node:child_process";
3
+ import { existsSync, readFileSync, readdirSync, realpathSync } from "node:fs";
4
+ import { basename, dirname, join } from "node:path";
5
+ import { pathToFileURL } from "node:url";
6
+ //#region skills/spec-gate/scripts/check-spec-state.mts
7
+ const LIFECYCLE_STATUSES = [
8
+ "draft",
9
+ "approved",
10
+ "implemented",
11
+ "deprecated"
12
+ ];
13
+ const GATES = ["spec", "impl"];
14
+ const VERDICTS = [
15
+ "approve",
16
+ "pause",
17
+ "reject"
18
+ ];
19
+ const SPEC_TYPES = ["reference", "behavioral"];
20
+ const NODE_FORBIDDEN_FIELDS = [
21
+ "status",
22
+ "project-path",
23
+ "approval",
24
+ "produced-by",
25
+ "aligned",
26
+ "spec-layout",
27
+ "leash"
28
+ ];
29
+ function frontmatter(text) {
30
+ const m = /^---\n([\s\S]*?)\n---/.exec(text);
31
+ return m ? m[1].split("\n") : [];
32
+ }
33
+ function prose(text) {
34
+ return text.replace(/```[\s\S]*?```/g, "").replace(/`[^`\n]*`/g, "");
35
+ }
36
+ function parseSpecState(text) {
37
+ const lines = frontmatter(text);
38
+ let status = "";
39
+ let projectPath = null;
40
+ let approval = null;
41
+ for (let i = 0; i < lines.length; i++) {
42
+ const s = /^status:\s*(.+)$/.exec(lines[i]);
43
+ if (s) {
44
+ status = s[1].trim().replace(/^["']|["']$/g, "");
45
+ continue;
46
+ }
47
+ const p = /^project-path:\s*(.+)$/.exec(lines[i]);
48
+ if (p) {
49
+ projectPath = p[1].trim().replace(/^["']|["']$/g, "");
50
+ continue;
51
+ }
52
+ const ap = /^approval:\s*(.*)$/.exec(lines[i]);
53
+ if (ap) {
54
+ approval = {};
55
+ if (ap[1].trim() && ap[1].trim() !== "{}") continue;
56
+ let gate = null;
57
+ for (let j = i + 1; j < lines.length; j++) {
58
+ if (!/^\s/.test(lines[j])) break;
59
+ const g = /^ {2}(\w+):\s*$/.exec(lines[j]);
60
+ if (g) {
61
+ gate = g[1];
62
+ approval[gate] = { hasWhy: false };
63
+ continue;
64
+ }
65
+ if (!gate) continue;
66
+ const verdict = /^ {4}verdict:\s*(.+)$/.exec(lines[j]);
67
+ if (verdict) approval[gate].verdict = verdict[1].trim().replace(/^["']|["']$/g, "");
68
+ const by = /^ {4}by:\s*(.+)$/.exec(lines[j]);
69
+ if (by) approval[gate].by = by[1].trim().replace(/^["']|["']$/g, "");
70
+ if (/^ {4}why:/.test(lines[j])) approval[gate].hasWhy = true;
71
+ }
72
+ }
73
+ }
74
+ const markerCount = (prose(text).match(/<!--\s*open:/g) ?? []).length;
75
+ return {
76
+ status,
77
+ projectPath,
78
+ markerCount,
79
+ approval
80
+ };
81
+ }
82
+ function checkSpec(slug, state) {
83
+ const { status, markerCount, approval } = state;
84
+ const v = [];
85
+ const tag = (msg) => v.push(`${slug}: ${msg}`);
86
+ if (!LIFECYCLE_STATUSES.includes(status)) tag(status === "" ? `no lifecycle status in frontmatter — a spec.md must declare status (${LIFECYCLE_STATUSES.join(" | ")}), and one that does not is checked by nothing` : `status "${status}" is not in the lifecycle enum (${LIFECYCLE_STATUSES.join(" | ")}) — discovery drops it, so it is checked by nothing`);
87
+ if ((status === "approved" || status === "implemented") && markerCount > 0) tag(`illegal state — ${markerCount} open marker(s) but status is ${status} (markers block the gate)`);
88
+ if (approval) for (const [gate, entry] of Object.entries(approval)) {
89
+ if (!GATES.includes(gate)) tag(`approval has unknown gate "${gate}" (expected spec | impl)`);
90
+ if (entry.verdict && !VERDICTS.includes(entry.verdict)) tag(`approval.${gate} has unknown verdict "${entry.verdict}" (expected approve | pause | reject)`);
91
+ if (entry.verdict === "pause" && entry.by) tag(`approval.${gate} is a pause but carries by — a pause is always the agent's act and omits by`);
92
+ if (entry.verdict === "approve" && !entry.by) tag(`approval.${gate} is an approve with no by — an approve must record its approver`);
93
+ if (entry.by === "agent" && !entry.hasWhy) tag(`approval.${gate} is by:agent but has no why block (a self-assertion must record its derivation)`);
94
+ const passed = gate === "spec" && (status === "approved" || status === "implemented") || gate === "impl" && status === "implemented";
95
+ if (entry.verdict === "pause" && passed) tag(`approval.${gate} is a pause but the ${gate} gate is already passed (status ${status})`);
96
+ }
97
+ if ((status === "approved" || status === "implemented") && !(approval?.spec?.verdict === "approve" && approval?.spec?.by)) tag(`status is ${status} but approval.spec has no approve verdict with an approver — the spec gate has no recorded ratification`);
98
+ if (status === "implemented" && !(approval?.impl?.verdict === "approve" && approval?.impl?.by)) tag("status is implemented but approval.impl has no approve verdict with an approver — the impl gate has no recorded ratification");
99
+ return v;
100
+ }
101
+ const PATH_PREFIXES = [
102
+ ".agents/",
103
+ "plugins/",
104
+ "packages/",
105
+ "apps/",
106
+ "docs/",
107
+ ".claude/"
108
+ ];
109
+ function extractPathRefs(text) {
110
+ const out = [];
111
+ const re = /`([^`\n]+)`/g;
112
+ let m;
113
+ while (m = re.exec(text)) {
114
+ const token = m[1].trim();
115
+ if (/[<*]/.test(token)) continue;
116
+ const isRelative = token.startsWith("./") || token.startsWith("../");
117
+ const isRooted = PATH_PREFIXES.some((p) => token.startsWith(p));
118
+ if (isRelative || isRooted) out.push(token);
119
+ }
120
+ return out;
121
+ }
122
+ function introducedPathRefs(baselineText, currentText) {
123
+ const baseRefs = new Set(extractPathRefs(baselineText));
124
+ return extractPathRefs(currentText).filter((ref) => !baseRefs.has(ref));
125
+ }
126
+ function checkReferencedArtifacts(slug, dir, text, baselineText = "") {
127
+ const v = [];
128
+ const tag = (msg) => v.push(`${slug}: introduces unresolved reference \`${msg}\` — surfaced for judgment`);
129
+ for (const ref of introducedPathRefs(baselineText, text)) {
130
+ const clean = ref.replace(/#.*$/, "");
131
+ const resolved = clean.startsWith("./") || clean.startsWith("../") ? join(dir, clean) : clean;
132
+ if (!existsSync(resolved)) tag(clean);
133
+ }
134
+ return v;
135
+ }
136
+ function parseNode(text) {
137
+ let type = null;
138
+ const lifecycleFields = [];
139
+ for (const l of frontmatter(text)) {
140
+ const key = /^([\w-]+):/.exec(l);
141
+ if (!key) continue;
142
+ if (key[1] === "spec-type") {
143
+ const m = /^spec-type:\s*(.+)$/.exec(l);
144
+ if (m) type = m[1].trim().replace(/^["']|["']$/g, "");
145
+ } else if (NODE_FORBIDDEN_FIELDS.includes(key[1])) lifecycleFields.push(key[1]);
146
+ }
147
+ const body = prose(text);
148
+ return {
149
+ type,
150
+ hasSubject: /^##\s+Subject\b/m.test(body),
151
+ hasUseCases: /^##\s+Use Cases\b/m.test(body),
152
+ lifecycleFields
153
+ };
154
+ }
155
+ function checkNode(slug, node, hasFeature) {
156
+ const { type, hasSubject, hasUseCases, lifecycleFields } = node;
157
+ const v = [];
158
+ const tag = (msg) => v.push(`${slug}: ${msg}`);
159
+ for (const f of lifecycleFields) tag(`carries lifecycle field "${f}" — lifecycle frontmatter is root-spec.md-only (a node README carries only spec-type)`);
160
+ if (type === null) return v;
161
+ if (!SPEC_TYPES.includes(type)) {
162
+ tag(`unknown spec-type "${type}" (expected reference | behavioral)`);
163
+ return v;
164
+ }
165
+ if (type === "reference") {
166
+ if (hasFeature) tag("spec-type: reference but a sibling .feature exists — a reference artifact is suite-less by design");
167
+ if (!hasSubject) tag("spec-type: reference but no ## Subject section");
168
+ }
169
+ if (type === "behavioral" && !hasUseCases) tag("spec-type: behavioral but no ## Use Cases section");
170
+ return v;
171
+ }
172
+ function parseLedgerGates(text) {
173
+ const gates = [];
174
+ for (const line of text.split("\n")) {
175
+ const s = line.trim();
176
+ if (!s) continue;
177
+ let obj;
178
+ try {
179
+ obj = JSON.parse(s);
180
+ } catch {
181
+ continue;
182
+ }
183
+ if (obj.kind === "gate" && typeof obj.gate === "string" && typeof obj.verdict === "string") gates.push({
184
+ gate: obj.gate,
185
+ verdict: obj.verdict
186
+ });
187
+ }
188
+ return gates;
189
+ }
190
+ function readLedgerText(root, slug) {
191
+ const parts = [];
192
+ const legacy = join(root, slug, "ledger.jsonl");
193
+ if (existsSync(legacy)) parts.push(readFileSync(legacy, "utf8"));
194
+ const dir = join(root, slug, "ledger");
195
+ if (existsSync(dir)) {
196
+ for (const e of readdirSync(dir, { withFileTypes: true })) if (e.isFile() && e.name.endsWith(".jsonl")) parts.push(readFileSync(join(dir, e.name), "utf8"));
197
+ }
198
+ return parts.join("\n");
199
+ }
200
+ function checkGateFloor(slug, status, gates) {
201
+ const v = [];
202
+ const tag = (msg) => v.push(`${slug}: ${msg}`);
203
+ const approved = (gate) => gates.some((g) => g.gate === gate && g.verdict === "approve");
204
+ if ((status === "approved" || status === "implemented") && !approved("spec")) tag(`status is ${status} but the ledger has no spec gate approve line — the durable gate floor is missing`);
205
+ if (status === "implemented" && !approved("impl")) tag("status is implemented but the ledger has no impl gate approve line — the durable gate floor is missing");
206
+ return v;
207
+ }
208
+ function hasFeatureFile(dir) {
209
+ try {
210
+ return readdirSync(dir).some((f) => f.endsWith(".feature"));
211
+ } catch {
212
+ return false;
213
+ }
214
+ }
215
+ function discoverDirsWith(root, name) {
216
+ const out = [];
217
+ const walk = (dir, rel) => {
218
+ let entries;
219
+ try {
220
+ entries = readdirSync(dir, { withFileTypes: true });
221
+ } catch {
222
+ return;
223
+ }
224
+ if (entries.some((e) => e.isFile() && e.name === name)) out.push(rel);
225
+ for (const e of entries) {
226
+ if (!e.isDirectory() || e.name === "node_modules" || e.name.startsWith(".")) continue;
227
+ walk(join(dir, e.name), rel ? join(rel, e.name) : e.name);
228
+ }
229
+ };
230
+ walk(root, "");
231
+ return out;
232
+ }
233
+ const discoverSpecDirs = (root) => discoverDirsWith(root, "spec.md");
234
+ const discoverNodeDirs = (root) => discoverDirsWith(root, "README.md");
235
+ function parseFilesArg(argv) {
236
+ const idx = argv.indexOf("--files");
237
+ if (idx === -1) return [];
238
+ const paths = [];
239
+ for (let i = idx + 1; i < argv.length; i++) {
240
+ if (argv[i].startsWith("--")) break;
241
+ paths.push(argv[i]);
242
+ }
243
+ return paths;
244
+ }
245
+ function readBaselineFromGit(base, path) {
246
+ try {
247
+ return execFileSync("git", ["show", `${base}:${path}`], {
248
+ encoding: "utf8",
249
+ stdio: [
250
+ "ignore",
251
+ "pipe",
252
+ "ignore"
253
+ ]
254
+ });
255
+ } catch {
256
+ return "";
257
+ }
258
+ }
259
+ function checkReferencedArtifactsInFiles(paths, baseline = () => "") {
260
+ const findings = [];
261
+ const violations = [];
262
+ for (const p of paths) {
263
+ let text;
264
+ try {
265
+ text = readFileSync(p, "utf8");
266
+ } catch {
267
+ violations.push(`${p}: cannot read file`);
268
+ continue;
269
+ }
270
+ findings.push(...checkReferencedArtifacts(p, dirname(p), text, baseline(p)));
271
+ }
272
+ return {
273
+ findings,
274
+ violations
275
+ };
276
+ }
277
+ function isUnderSpecTree(path) {
278
+ return /(^|\/)\.agents\/specs?(\/|$)/.test(path);
279
+ }
280
+ function filterProseMdInSpecTree(paths) {
281
+ return paths.filter((p) => p.endsWith(".md") && isUnderSpecTree(p));
282
+ }
283
+ function splitTableRow(line) {
284
+ return line.trim().replace(/^\|/, "").replace(/\|$/, "").split("|").map((c) => c.trim());
285
+ }
286
+ function extractSection(body, heading) {
287
+ const start = new RegExp(`(^|\\n)##\\s+${heading}\\b`).exec(body);
288
+ if (!start) return null;
289
+ const rest = body.slice(start.index + start[0].length);
290
+ const end = /\n##\s/.exec(rest);
291
+ return end ? rest.slice(0, end.index) : rest;
292
+ }
293
+ function extractUseCaseScenarioRefs(text) {
294
+ const section = extractSection(text.replace(/```[\s\S]*?```/g, ""), "Use Cases");
295
+ if (section === null) return {
296
+ hasSection: false,
297
+ refs: [],
298
+ unparseable: []
299
+ };
300
+ const lines = section.split("\n");
301
+ const headerIdx = lines.findIndex((l) => l.trim().startsWith("|"));
302
+ if (headerIdx === -1) return {
303
+ hasSection: true,
304
+ refs: [],
305
+ unparseable: []
306
+ };
307
+ const scenarioIdx = splitTableRow(lines[headerIdx]).findIndex((c) => /^scenario$/i.test(c));
308
+ if (scenarioIdx === -1) return {
309
+ hasSection: true,
310
+ refs: [],
311
+ unparseable: []
312
+ };
313
+ const refs = [];
314
+ const unparseable = [];
315
+ for (let i = headerIdx + 2; i < lines.length; i++) {
316
+ if (!lines[i].trim().startsWith("|")) break;
317
+ const cell = splitTableRow(lines[i])[scenarioIdx] ?? "";
318
+ const ref = /`([^`\n]+)`/.exec(cell);
319
+ if (ref) refs.push(ref[1].trim());
320
+ else unparseable.push(lines[i].trim());
321
+ }
322
+ return {
323
+ hasSection: true,
324
+ refs,
325
+ unparseable
326
+ };
327
+ }
328
+ function escapeRegExp(s) {
329
+ return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
330
+ }
331
+ function resolveScenarioRef(ref, featureText) {
332
+ if (ref.startsWith("@")) return new RegExp(`(^|\\s)${escapeRegExp(ref)}(\\s|$)`, "m").test(featureText);
333
+ const m = /^Scenario:\s*(.+)$/.exec(ref);
334
+ if (!m) return false;
335
+ const title = m[1].trim();
336
+ return new RegExp(`^\\s*Scenario:\\s*${escapeRegExp(title)}\\s*$`, "m").test(featureText);
337
+ }
338
+ function findSiblingFeature(dir) {
339
+ let entries;
340
+ try {
341
+ entries = readdirSync(dir).filter((f) => f.endsWith(".feature"));
342
+ } catch {
343
+ return null;
344
+ }
345
+ if (entries.length === 0) return null;
346
+ const named = `${basename(dir)}.feature`;
347
+ if (entries.includes(named)) return join(dir, named);
348
+ return join(dir, entries[0]);
349
+ }
350
+ function checkUseCaseCoverage(slug, dir, text) {
351
+ const v = [];
352
+ const tag = (msg) => v.push(`${slug}: ${msg}`);
353
+ const { hasSection, refs, unparseable } = extractUseCaseScenarioRefs(text);
354
+ if (!hasSection) return v;
355
+ for (const raw of unparseable) tag(`Use Cases data row has no backtick-wrapped Scenario cell — ${raw}`);
356
+ if (refs.length === 0) return v;
357
+ const featurePath = findSiblingFeature(dir);
358
+ const featureText = featurePath ? readFileSync(featurePath, "utf8") : "";
359
+ for (const ref of refs) if (!resolveScenarioRef(ref, featureText)) tag(`Use Cases table names scenario \`${ref}\` that does not resolve in the sibling .feature`);
360
+ return v;
361
+ }
362
+ function checkUseCaseCoverageInFiles(paths) {
363
+ const violations = [];
364
+ for (const p of paths) {
365
+ let text;
366
+ try {
367
+ text = readFileSync(p, "utf8");
368
+ } catch {
369
+ continue;
370
+ }
371
+ violations.push(...checkUseCaseCoverage(p, dirname(p), text));
372
+ }
373
+ return violations;
374
+ }
375
+ function main(argv) {
376
+ if (argv.includes("--files")) {
377
+ const paths = parseFilesArg(argv);
378
+ if (paths.length === 0) {
379
+ console.error("✗ --files requires at least one .md path under the spec tree");
380
+ return 1;
381
+ }
382
+ const proseFiles = filterProseMdInSpecTree(paths);
383
+ const base = argv.includes("--base") ? argv[argv.indexOf("--base") + 1] : void 0;
384
+ const { findings, violations: refReadErrors } = checkReferencedArtifactsInFiles(proseFiles, base ? (p) => readBaselineFromGit(base, p) : () => "");
385
+ const ucViolations = checkUseCaseCoverageInFiles(proseFiles);
386
+ const violations = [...refReadErrors, ...ucViolations];
387
+ for (const f of findings) process.stdout.write(`⚠ ${f}\n`);
388
+ if (violations.length) {
389
+ for (const line of violations) console.error(`✗ ${line}`);
390
+ return 1;
391
+ }
392
+ process.stdout.write(findings.length ? `referenced-artifact findings surfaced for judgment (${findings.length}); use-case-coverage OK\n` : "referenced-artifact and use-case-coverage checks OK\n");
393
+ return 0;
394
+ }
395
+ const root = argv.includes("--root") ? argv[argv.indexOf("--root") + 1] : ".agents/specs";
396
+ let violations = [];
397
+ for (const slug of discoverSpecDirs(root)) {
398
+ const specPath = join(root, slug, "spec.md");
399
+ if (!existsSync(specPath)) continue;
400
+ const state = parseSpecState(readFileSync(specPath, "utf8"));
401
+ violations = violations.concat(checkSpec(slug, state));
402
+ const gates = parseLedgerGates(readLedgerText(root, slug));
403
+ violations = violations.concat(checkGateFloor(slug, state.status, gates));
404
+ }
405
+ for (const slug of discoverNodeDirs(root)) {
406
+ const dir = join(root, slug);
407
+ const readme = join(dir, "README.md");
408
+ if (!existsSync(readme)) continue;
409
+ violations = violations.concat(checkNode(slug, parseNode(readFileSync(readme, "utf8")), hasFeatureFile(dir)));
410
+ }
411
+ if (violations.length) {
412
+ for (const line of violations) console.error(`✗ ${line}`);
413
+ return 1;
414
+ }
415
+ process.stdout.write("spec states OK\n");
416
+ return 0;
417
+ }
418
+ if (process.argv[1] && import.meta.url === pathToFileURL(realpathSync(process.argv[1])).href) process.exit(main(process.argv.slice(2)));
419
+ //#endregion
420
+ export { checkGateFloor, checkNode, checkReferencedArtifacts, checkReferencedArtifactsInFiles, checkSpec, checkUseCaseCoverage, checkUseCaseCoverageInFiles, discoverNodeDirs, discoverSpecDirs, extractPathRefs, extractUseCaseScenarioRefs, filterProseMdInSpecTree, findSiblingFeature, introducedPathRefs, isUnderSpecTree, main, parseFilesArg, parseLedgerGates, parseNode, parseSpecState, readLedgerText, resolveScenarioRef };