@llman-sdd/core 0.11.0 → 0.12.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.
Files changed (112) hide show
  1. package/package.json +6 -3
  2. package/src/archive/freeze.ts +0 -13
  3. package/src/archive/frozenCard.ts +1 -10
  4. package/src/archive/ignoreChain.ts +2 -6
  5. package/src/archive/sevenzip.ts +0 -1
  6. package/src/change/closeOutHarness.ts +17 -0
  7. package/src/change/collect.ts +0 -5
  8. package/src/change/frontmatter.ts +15 -7
  9. package/src/change/id.ts +0 -2
  10. package/src/change/lifecycle.ts +1 -38
  11. package/src/config/changeId.ts +0 -2
  12. package/src/config/load.ts +0 -4
  13. package/src/config/schema.ts +24 -0
  14. package/src/git/spawnGit.ts +0 -3
  15. package/src/hints/jitHints.ts +82 -0
  16. package/src/index.ts +455 -3
  17. package/src/init/defaultConfig.ts +2 -0
  18. package/src/init/init.ts +61 -20
  19. package/src/init/uncommentConfig.ts +909 -0
  20. package/src/locale.ts +0 -2
  21. package/src/project/migrateNotes.ts +0 -9
  22. package/src/project/migrations.ts +70 -0
  23. package/src/report/graph/analysis.ts +0 -4
  24. package/src/report/graph/nodes.ts +0 -6
  25. package/src/report/show.ts +7 -23
  26. package/src/report/specHelpers.ts +10 -14
  27. package/src/review/review.ts +0 -12
  28. package/src/spec/assignSpecIds.ts +0 -3
  29. package/src/spec/authoring.ts +14 -26
  30. package/src/spec/ir.ts +14 -5
  31. package/src/spec/keywords.ts +0 -2
  32. package/src/spec/machineKey.ts +49 -0
  33. package/src/spec/migrateNative.ts +4 -18
  34. package/src/spec/parser.ts +10 -19
  35. package/src/spec/reqRef.ts +135 -0
  36. package/src/studio/changeScope.ts +50 -0
  37. package/src/studio/changeScopeDiff.ts +195 -0
  38. package/src/studio/contracts.ts +493 -0
  39. package/src/studio/decisionTree.ts +206 -0
  40. package/src/studio/deeplink.ts +116 -0
  41. package/src/studio/demos.ts +167 -0
  42. package/src/studio/diff.ts +494 -0
  43. package/src/studio/drift.ts +738 -0
  44. package/src/studio/extract.ts +360 -0
  45. package/src/studio/featureSync.ts +592 -0
  46. package/src/studio/forest.ts +570 -0
  47. package/src/studio/index.ts +32 -0
  48. package/src/studio/model.ts +254 -0
  49. package/src/studio/revisions.ts +476 -0
  50. package/src/studio/search.ts +337 -0
  51. package/src/studio/store.ts +811 -0
  52. package/src/studio/sync.ts +912 -0
  53. package/src/studio/textfold.ts +72 -0
  54. package/src/studio/time.ts +107 -0
  55. package/src/studio/validate.ts +363 -0
  56. package/src/templates/embedded.ts +4 -6
  57. package/src/templates/engine.ts +1 -1
  58. package/src/templates/skills.ts +71 -49
  59. package/src/theorems/guard.ts +52 -0
  60. package/src/theorems/index.ts +56 -0
  61. package/src/theorems/migrate.ts +133 -0
  62. package/src/theorems/schema.ts +67 -0
  63. package/src/theorems/scope.ts +68 -0
  64. package/src/theorems/serialize.ts +60 -0
  65. package/src/theorems/transactionMachine.ts +247 -0
  66. package/src/theorems/validate.ts +141 -0
  67. package/src/util/nextNumber.ts +0 -2
  68. package/src/validation/changeCheck.ts +0 -24
  69. package/src/validation/flowAndNesting.ts +434 -0
  70. package/src/validation/harness.ts +108 -65
  71. package/src/validation/validate.ts +26 -12
  72. package/src/validation/xstateTransitions.ts +483 -184
  73. package/templates/zh-Hans/llmanspec-agents-stub.md +17 -2
  74. package/templates/zh-Hans/skills/llman-sdd-autopilot/SKILL.md +34 -0
  75. package/templates/zh-Hans/skills/llman-sdd-change/SKILL.md +44 -0
  76. package/templates/zh-Hans/skills/llman-sdd-change/playbooks/archive.md +15 -0
  77. package/templates/zh-Hans/skills/llman-sdd-change/playbooks/draft.md +9 -0
  78. package/templates/zh-Hans/skills/llman-sdd-review/SKILL.md +71 -0
  79. package/templates/zh-Hans/skills/llman-sdd-specs/SKILL.md +70 -0
  80. package/templates/zh-Hans/skills/llman-sdd-specs/playbooks/compact.md +12 -0
  81. package/templates/zh-Hans/skills/llman-sdd-specs/playbooks/doctor.md +26 -0
  82. package/templates/zh-Hans/skills/llman-sdd-specs/references/graph-dsl.md +33 -0
  83. package/templates/zh-Hans/skills/llman-sdd-studio/SKILL.md +33 -0
  84. package/templates/zh-Hans/skills/llman-sdd-studio/playbooks/collab.md +20 -0
  85. package/templates/zh-Hans/skills/llman-sdd-studio/playbooks/drift.md +52 -0
  86. package/templates/zh-Hans/skills/llman-sdd-studio/playbooks/onboard.md +33 -0
  87. package/templates/zh-Hans/units/skills/cli-footer.md +2 -2
  88. package/templates/zh-Hans/agents-root-stub.md +0 -9
  89. package/templates/zh-Hans/skills/llman-sdd-apply-cycle.md +0 -69
  90. package/templates/zh-Hans/skills/llman-sdd-apply.md +0 -116
  91. package/templates/zh-Hans/skills/llman-sdd-arch-review.md +0 -58
  92. package/templates/zh-Hans/skills/llman-sdd-archive.md +0 -78
  93. package/templates/zh-Hans/skills/llman-sdd-continue.md +0 -34
  94. package/templates/zh-Hans/skills/llman-sdd-draft.md +0 -53
  95. package/templates/zh-Hans/skills/llman-sdd-explore.md +0 -63
  96. package/templates/zh-Hans/skills/llman-sdd-ff.md +0 -34
  97. package/templates/zh-Hans/skills/llman-sdd-graph.md +0 -56
  98. package/templates/zh-Hans/skills/llman-sdd-propose.md +0 -106
  99. package/templates/zh-Hans/skills/llman-sdd-quick.md +0 -40
  100. package/templates/zh-Hans/skills/llman-sdd-research.md +0 -35
  101. package/templates/zh-Hans/skills/llman-sdd-specs-compact.md +0 -42
  102. package/templates/zh-Hans/skills/llman-sdd-validate.md +0 -28
  103. package/templates/zh-Hans/skills/llman-sdd-verify.md +0 -77
  104. package/templates/zh-Hans/skills/llman-sdd-wayfinder.md +0 -83
  105. package/templates/zh-Hans/units/skills/ethics-governance.md +0 -6
  106. package/templates/zh-Hans/units/skills/git-native-flow.md +0 -56
  107. package/templates/zh-Hans/units/skills/human-readable-summary.md +0 -9
  108. package/templates/zh-Hans/units/skills/stage-guard.md +0 -17
  109. package/templates/zh-Hans/units/skills/structured-protocol.md +0 -19
  110. package/templates/zh-Hans/units/skills/validation-hints.md +0 -20
  111. package/templates/zh-Hans/units/skills/version-drift.md +0 -3
  112. package/templates/zh-Hans/units/spec/feature-contract.md +0 -8
@@ -0,0 +1,434 @@
1
+ /**
2
+ * Spec-level @flow F3/F4 warnings and nested-machine gates N1–N6 (validation r169).
3
+ * Pure — no I/O. Lives in validation so it does not import studio (r72).
4
+ */
5
+ import type { RuleIR, ScenarioIR } from '../spec/ir.ts';
6
+ import { ruleHasRunnableScenario, specIdOf } from '../spec/ir.ts';
7
+ import { makeMachineKey, splitMachineKey } from '../spec/machineKey.ts';
8
+ import { buildReqRegistry } from '../spec/reqRegistry.ts';
9
+ import type { SpecEntry, ValidationItem } from './validate.ts';
10
+ import {
11
+ extractTransitions,
12
+ FLOW_TAG,
13
+ STATE_TAG,
14
+ isJudgmentKind,
15
+ namedExitsInGraph,
16
+ NODES_TAG,
17
+ parseNestRef,
18
+ parseVerifyPointers,
19
+ resolveGraphScope,
20
+ resolveNodesColumns,
21
+ type TransitionEdge,
22
+ } from './xstateTransitions.ts';
23
+
24
+ function nestAnchor(cap: string, reqId: string): string {
25
+ return `${cap}/transitions/${reqId}`;
26
+ }
27
+
28
+ interface CatalogMachine {
29
+ machineKey: string;
30
+ capability: string;
31
+ reqId: string;
32
+ scenarioName: string;
33
+ ruleTitle: string;
34
+ graphKind: 'machine' | 'flow';
35
+ states: Set<string>;
36
+ judgments: Set<string>;
37
+ outs: Map<string, number>;
38
+ outgoingLabels: Map<string, Set<string>>;
39
+ namedExits: Set<string>;
40
+ transitions: TransitionEdge[];
41
+ }
42
+
43
+ function isEdgeScenario(sc: ScenarioIR): boolean {
44
+ const scope = resolveGraphScope(sc);
45
+ return scope === FLOW_TAG || scope === STATE_TAG;
46
+ }
47
+
48
+ function catalogMachines(entries: readonly SpecEntry[]): CatalogMachine[] {
49
+ const catalog: CatalogMachine[] = [];
50
+ for (const entry of entries) {
51
+ const cap = specIdOf(entry);
52
+ for (const rule of entry.doc.rules) {
53
+ for (const sc of rule.scenarios) {
54
+ if (!isEdgeScenario(sc)) continue;
55
+ const transitions = extractTransitions(sc);
56
+ const states = new Set<string>();
57
+ const outs = new Map<string, number>();
58
+ const outgoingLabels = new Map<string, Set<string>>();
59
+ for (const t of transitions) {
60
+ const from = t.from.trim();
61
+ const to = t.to.trim();
62
+ if (from !== '') states.add(from);
63
+ if (to !== '') states.add(to);
64
+ outs.set(from, (outs.get(from) ?? 0) + 1);
65
+ const labels = outgoingLabels.get(from) ?? new Set<string>();
66
+ labels.add(t.event.trim());
67
+ outgoingLabels.set(from, labels);
68
+ }
69
+ catalog.push({
70
+ machineKey: makeMachineKey(cap, rule.reqId, sc.name),
71
+ capability: cap,
72
+ reqId: rule.reqId,
73
+ scenarioName: sc.name,
74
+ ruleTitle: rule.title,
75
+ graphKind: sc.tags.includes(FLOW_TAG) ? 'flow' : 'machine',
76
+ states,
77
+ judgments: new Set(),
78
+ outs,
79
+ outgoingLabels,
80
+ namedExits: namedExitsInGraph(transitions),
81
+ transitions,
82
+ });
83
+ }
84
+ }
85
+ }
86
+ return catalog;
87
+ }
88
+
89
+ function resolveTargetMachine(
90
+ catalog: readonly CatalogMachine[],
91
+ ref: { reqId: string; scenarioName: string | null },
92
+ ): CatalogMachine[] {
93
+ return catalog.filter((m) => {
94
+ if (m.reqId !== ref.reqId) return false;
95
+ if (ref.scenarioName === null) return true;
96
+ return m.scenarioName === ref.scenarioName;
97
+ });
98
+ }
99
+
100
+ function collectCyclePaths(edges: ReadonlyMap<string, string[]>): string[][] {
101
+ const cycles: string[][] = [];
102
+ const visited = new Set<string>();
103
+ const stack: string[] = [];
104
+
105
+ const dfs = (node: string): void => {
106
+ if (visited.has(node)) return;
107
+ stack.push(node);
108
+ for (const next of edges.get(node) ?? []) {
109
+ const idx = stack.indexOf(next);
110
+ if (idx !== -1) {
111
+ cycles.push([...stack.slice(idx), next]);
112
+ continue;
113
+ }
114
+ if (!visited.has(next)) dfs(next);
115
+ }
116
+ stack.pop();
117
+ visited.add(node);
118
+ };
119
+
120
+ for (const node of edges.keys()) dfs(node);
121
+ return cycles;
122
+ }
123
+
124
+ const PLACEHOLDER_NAME_RE = /^(新状态|中间态|终态|新步骤|条件判断|state|step)\d+$/iu;
125
+
126
+ function applyNodeKindFlags(catalog: CatalogMachine[], entries: readonly SpecEntry[]): void {
127
+ const hostsByRule = new Map<string, CatalogMachine[]>();
128
+ for (const m of catalog) {
129
+ const key = `${m.capability}\0${m.reqId}`;
130
+ const list = hostsByRule.get(key) ?? [];
131
+ list.push(m);
132
+ hostsByRule.set(key, list);
133
+ }
134
+ for (const entry of entries) {
135
+ const cap = specIdOf(entry);
136
+ for (const rule of entry.doc.rules) {
137
+ const hosts = hostsByRule.get(`${cap}\0${rule.reqId}`) ?? [];
138
+ for (const sc of rule.scenarios) {
139
+ if (!sc.tags.includes(NODES_TAG)) continue;
140
+ for (const block of sc.examples ?? []) {
141
+ const cols = resolveNodesColumns(block.headers);
142
+ if (!cols || cols.nodeKind === undefined) continue;
143
+ const nodeIdx = block.headers.indexOf(cols.node);
144
+ const kindIdx = block.headers.indexOf(cols.nodeKind);
145
+ for (const row of block.rows) {
146
+ const stateName = (row[nodeIdx] ?? '').trim();
147
+ if (stateName === '' || !isJudgmentKind(row[kindIdx] ?? '')) continue;
148
+ const hit = hosts.filter((h) => h.states.has(stateName));
149
+ if (hit.length === 1) hit[0]?.judgments.add(stateName);
150
+ }
151
+ }
152
+ }
153
+ }
154
+ }
155
+ }
156
+
157
+ function emitFlowWarnings(catalog: readonly CatalogMachine[], items: ValidationItem[]): void {
158
+ for (const m of catalog) {
159
+ if (m.graphKind !== 'flow') continue;
160
+ const id = nestAnchor(m.capability, m.reqId || m.ruleTitle);
161
+ for (const name of m.judgments) {
162
+ const outs = m.outs.get(name) ?? 0;
163
+ if (outs < 2) {
164
+ items.push({
165
+ level: 'WARNING',
166
+ id,
167
+ message: `F3 judgment: step \`${name}\` has ${String(outs)} outgoing branch(es); expected >= 2`,
168
+ });
169
+ }
170
+ }
171
+ for (const name of m.states) {
172
+ if (PLACEHOLDER_NAME_RE.test(name)) {
173
+ items.push({
174
+ level: 'WARNING',
175
+ id,
176
+ message: `F4 placeholder name: step \`${name}\` should be renamed to a concrete action`,
177
+ });
178
+ }
179
+ }
180
+ }
181
+ }
182
+
183
+ /**
184
+ * N1–N6 plus @flow F3/F4 WARNINGs across a full spec set (global req registry).
185
+ */
186
+ export function validateFlowAndNesting(entries: readonly SpecEntry[]): ValidationItem[] {
187
+ const items: ValidationItem[] = [];
188
+ const catalog = catalogMachines(entries);
189
+ applyNodeKindFlags(catalog, entries);
190
+ const registry = buildReqRegistry(entries);
191
+
192
+ const byRuleHosts = new Map<string, CatalogMachine[]>();
193
+ for (const machine of catalog) {
194
+ const key = `${machine.capability}\0${machine.reqId}`;
195
+ const list = byRuleHosts.get(key) ?? [];
196
+ list.push(machine);
197
+ byRuleHosts.set(key, list);
198
+ }
199
+
200
+ const nestEdges = new Map<string, string[]>();
201
+ const addEdge = (from: string, to: string): void => {
202
+ const list = nestEdges.get(from) ?? [];
203
+ list.push(to);
204
+ nestEdges.set(from, list);
205
+ };
206
+
207
+ for (const entry of entries) {
208
+ const cap = specIdOf(entry);
209
+ for (const rule of entry.doc.rules) {
210
+ const hosts = byRuleHosts.get(`${cap}\0${rule.reqId}`) ?? [];
211
+ const mapped = new Map<string, number>();
212
+
213
+ for (const sc of rule.scenarios) {
214
+ if (!sc.tags.includes(NODES_TAG)) continue;
215
+ walkNodesRows(sc, rule, cap, hosts, catalog, registry, mapped, items, addEdge);
216
+ }
217
+ }
218
+ }
219
+
220
+ const seenCycle = new Set<string>();
221
+ const capabilityByKey = new Map(catalog.map((m) => [m.machineKey, m.capability]));
222
+ for (const cycle of collectCyclePaths(nestEdges)) {
223
+ const path = cycle.join(' -> ');
224
+ if (seenCycle.has(path)) continue;
225
+ seenCycle.add(path);
226
+ const startKey = cycle[0] ?? '';
227
+ const cap = capabilityByKey.get(startKey) ?? 'unknown';
228
+ const req = splitMachineKey(startKey)?.reqId ?? 'unknown';
229
+ items.push({
230
+ level: 'ERROR',
231
+ id: nestAnchor(cap, req),
232
+ message: `nested machine cycle: ${path}`,
233
+ });
234
+ }
235
+
236
+ emitN6ExitChecks(catalog, entries, items);
237
+ emitFlowWarnings(catalog, items);
238
+ emitVerifyPointerChecks(entries, items);
239
+ return items;
240
+ }
241
+
242
+ function emitVerifyPointerChecks(entries: readonly SpecEntry[], items: ValidationItem[]): void {
243
+ const rulesByReq = new Map<string, { cap: string; rule: RuleIR }>();
244
+ for (const entry of entries) {
245
+ const cap = specIdOf(entry);
246
+ for (const rule of entry.doc.rules) {
247
+ if (rule.reqId !== '') rulesByReq.set(rule.reqId, { cap, rule });
248
+ }
249
+ }
250
+
251
+ for (const entry of entries) {
252
+ const cap = specIdOf(entry);
253
+ for (const rule of entry.doc.rules) {
254
+ for (const sc of rule.scenarios) {
255
+ if (!sc.tags.includes(FLOW_TAG)) continue;
256
+ for (const t of extractTransitions(sc)) {
257
+ const pointers = parseVerifyPointers(t.verify ?? '');
258
+ for (const ptr of pointers) {
259
+ const id = nestAnchor(cap, rule.reqId || 'unknown');
260
+ if (ptr.reqId === '') {
261
+ items.push({
262
+ level: 'ERROR',
263
+ id,
264
+ message: `verify pointer syntax: \`${ptr.raw}\` is not @req:<id>`,
265
+ });
266
+ continue;
267
+ }
268
+ const hit = rulesByReq.get(ptr.reqId);
269
+ if (hit === undefined) {
270
+ items.push({
271
+ level: 'ERROR',
272
+ id: nestAnchor(cap, ptr.reqId),
273
+ message: `dangling verify pointer: ${ptr.raw}`,
274
+ });
275
+ continue;
276
+ }
277
+ if (ptr.scenarioName !== null) {
278
+ const named = hit.rule.scenarios.find((s) => s.name === ptr.scenarioName);
279
+ if (named === undefined) {
280
+ items.push({
281
+ level: 'ERROR',
282
+ id: nestAnchor(cap, ptr.reqId),
283
+ message: `dangling verify pointer: ${ptr.raw}`,
284
+ });
285
+ continue;
286
+ }
287
+ }
288
+ if (!ruleHasRunnableScenario(hit.rule)) {
289
+ items.push({
290
+ level: 'WARNING',
291
+ id: nestAnchor(cap, ptr.reqId),
292
+ message: `verify pointer unbound: ${ptr.raw}`,
293
+ });
294
+ }
295
+ }
296
+ }
297
+ }
298
+ }
299
+ }
300
+ }
301
+
302
+ function emitN6ExitChecks(
303
+ catalog: readonly CatalogMachine[],
304
+ entries: readonly SpecEntry[],
305
+ items: ValidationItem[],
306
+ ): void {
307
+ for (const entry of entries) {
308
+ const cap = specIdOf(entry);
309
+ for (const rule of entry.doc.rules) {
310
+ const hosts = catalog.filter((m) => m.capability === cap && m.reqId === rule.reqId);
311
+ for (const sc of rule.scenarios) {
312
+ if (!sc.tags.includes(NODES_TAG)) continue;
313
+ for (const block of sc.examples ?? []) {
314
+ const cols = resolveNodesColumns(block.headers);
315
+ if (!cols) continue;
316
+ const nodeIdx = block.headers.indexOf(cols.node);
317
+ const nestIdx = cols.nestKey === undefined ? -1 : block.headers.indexOf(cols.nestKey);
318
+ if (nestIdx === -1) continue;
319
+ for (const row of block.rows) {
320
+ const stateName = (row[nodeIdx] ?? '').trim();
321
+ const ref = parseNestRef(row[nestIdx] ?? '');
322
+ if (ref === null || ref.reqId === '') continue;
323
+ const targets = resolveTargetMachine(catalog, ref);
324
+ if (targets.length !== 1) continue;
325
+ const child = targets[0]!;
326
+ if (child.namedExits.size === 0) continue;
327
+ const hostHits = hosts.filter((h) => h.states.has(stateName));
328
+ if (hostHits.length !== 1) continue;
329
+ const host = hostHits[0]!;
330
+ const labels = host.outgoingLabels.get(stateName) ?? new Set<string>();
331
+ for (const exitName of child.namedExits) {
332
+ if (!labels.has(exitName)) {
333
+ items.push({
334
+ level: 'ERROR',
335
+ id: nestAnchor(cap, rule.reqId || rule.title),
336
+ message: `nested exit \`${exitName}\` from child \`${child.scenarioName}\` is not covered by outgoing triggers/branches on host node \`${stateName}\``,
337
+ });
338
+ }
339
+ }
340
+ }
341
+ }
342
+ }
343
+ }
344
+ }
345
+ }
346
+
347
+ function walkNodesRows(
348
+ sc: ScenarioIR,
349
+ rule: RuleIR,
350
+ cap: string,
351
+ hosts: readonly CatalogMachine[],
352
+ catalog: readonly CatalogMachine[],
353
+ registry: ReturnType<typeof buildReqRegistry>,
354
+ mapped: Map<string, number>,
355
+ items: ValidationItem[],
356
+ addEdge: (from: string, to: string) => void,
357
+ ): void {
358
+ for (const block of sc.examples ?? []) {
359
+ const cols = resolveNodesColumns(block.headers);
360
+ if (!cols) continue;
361
+ const nodeIdx = block.headers.indexOf(cols.node);
362
+ const nestIdx = cols.nestKey === undefined ? -1 : block.headers.indexOf(cols.nestKey);
363
+
364
+ for (const row of block.rows) {
365
+ const stateName = (row[nodeIdx] ?? '').trim();
366
+ if (stateName === '') continue;
367
+ const hostsWithState = hosts.filter((m) => m.states.has(stateName));
368
+ const prev = mapped.get(stateName) ?? 0;
369
+ mapped.set(stateName, prev + 1);
370
+
371
+ if (hostsWithState.length === 0) {
372
+ items.push({
373
+ level: 'ERROR',
374
+ id: nestAnchor(cap, rule.reqId || rule.title),
375
+ message: `host state \`${stateName}\` in \`${sc.name}\` does not exist in the host machine`,
376
+ });
377
+ } else if (hostsWithState.length > 1) {
378
+ items.push({
379
+ level: 'ERROR',
380
+ id: nestAnchor(cap, rule.reqId || rule.title),
381
+ message: `host state \`${stateName}\` is ambiguous across multiple machines in the same rule`,
382
+ });
383
+ }
384
+ if (prev >= 1) {
385
+ items.push({
386
+ level: 'ERROR',
387
+ id: nestAnchor(cap, rule.reqId || rule.title),
388
+ message: `host state \`${stateName}\` is mapped more than once in the same rule`,
389
+ });
390
+ }
391
+ if (hostsWithState.length !== 1 || prev >= 1) continue;
392
+
393
+ const nestCell = nestIdx === -1 ? '' : (row[nestIdx] ?? '');
394
+ const ref = parseNestRef(nestCell);
395
+ if (ref === null) continue;
396
+ if (ref.reqId === '') {
397
+ items.push({
398
+ level: 'ERROR',
399
+ id: nestAnchor(cap, rule.reqId || rule.title),
400
+ message: `dangling nested machine reference in \`${sc.name}\`: \`${ref.raw}\` is not a valid @req handle`,
401
+ });
402
+ continue;
403
+ }
404
+ if (!registry.byId.has(ref.reqId)) {
405
+ items.push({
406
+ level: 'ERROR',
407
+ id: nestAnchor(cap, rule.reqId || rule.title),
408
+ message: `dangling nested machine reference in \`${sc.name}\`: \`@req:${ref.reqId}\` does not exist`,
409
+ });
410
+ continue;
411
+ }
412
+ const targets = resolveTargetMachine(catalog, ref);
413
+ if (targets.length === 0) {
414
+ items.push({
415
+ level: 'ERROR',
416
+ id: nestAnchor(cap, rule.reqId || rule.title),
417
+ message: `dangling nested machine reference in \`${sc.name}\`: rule \`${ref.reqId}\` has no state machine`,
418
+ });
419
+ continue;
420
+ }
421
+ if (targets.length > 1) {
422
+ items.push({
423
+ level: 'ERROR',
424
+ id: nestAnchor(cap, rule.reqId || rule.title),
425
+ message: `dangling nested machine reference in \`${sc.name}\`: rule \`${ref.reqId}\` has no unique state machine`,
426
+ });
427
+ continue;
428
+ }
429
+ const host = hostsWithState[0];
430
+ const target = targets[0];
431
+ if (host && target) addEdge(host.machineKey, target.machineKey);
432
+ }
433
+ }
434
+ }
@@ -1,20 +1,20 @@
1
1
  /**
2
- * Spec verification check execution for validate (validation capability, r13/r48):
3
- * expands specs.check_command per capability, executes each expanded command
4
- * string at most once per validate invocation (batch-once), and maps results
5
- * to per-capability issues. Pure — the subprocess runs through the injected
6
- * HarnessRunner; this module never reads the environment or the wall clock
7
- * (the nested-invocation guard is read by the CLI and passed in as a
8
- * parameter).
2
+ * Spec verification check execution for validate (validation capability, r13/r48/r181):
3
+ * expands named harness commands (check_command / check_dangling_bdd_command) per
4
+ * capability, executes each expanded command string at most once per validate
5
+ * invocation (batch-once), and maps results to per-capability issues. Pure —
6
+ * the subprocess runs through the injected HarnessRunner; this module never
7
+ * reads the environment or the wall clock (the nested-invocation guard is
8
+ * read by the CLI and passed in as a parameter).
9
9
  */
10
10
  import type { ValidationItem } from './validate.ts';
11
11
 
12
12
  /**
13
- * Harness runner port (r13/r48): executes one expanded specs.check_command
14
- * string; the CLI adapter owns the subprocess (and the nested-invocation
15
- * guard env it exports). exitCode is null only when the command could not
16
- * start (spawnError then carries the reason). Lives here rather than in
17
- * ports.ts so the validation module gains no new cross-module edge (r72).
13
+ * Harness runner port (r13/r48): executes one expanded command string; the
14
+ * CLI adapter owns the subprocess (and the nested-invocation guard env it
15
+ * exports). exitCode is null only when the command could not start
16
+ * (spawnError then carries the reason). Lives here rather than in ports.ts
17
+ * so the validation module gains no new cross-module edge (r72).
18
18
  */
19
19
  export interface HarnessRunner {
20
20
  run(
@@ -30,22 +30,30 @@ export interface HarnessRunner {
30
30
  export interface HarnessTarget {
31
31
  /** Capability id ({feature_name}). */
32
32
  capability: string;
33
- /** Repo-root-relative path of the capability's main .feature file. */
33
+ /** Repo-root-relative POSIX path of the capability's main .feature file. */
34
34
  featurePath: string;
35
35
  }
36
36
 
37
+ export type HarnessCommandKind = 'check' | 'dangling';
38
+
39
+ export interface HarnessNamedCommand {
40
+ kind: HarnessCommandKind;
41
+ command: string;
42
+ }
43
+
37
44
  /** Trigger state assembled by the CLI (env guard + flags + config + runner). */
38
45
  export interface HarnessGate {
39
46
  /** LLMAN_SDD_HARNESS_ACTIVE=1 was set by an enclosing validate invocation. */
40
47
  nested: boolean;
41
48
  check: 'default' | 'on' | 'off';
42
- /** Undefined when specs.check_command is not configured (non-empty string). */
49
+ /** Undefined when no harness command is configured (non-empty string). */
43
50
  runner: HarnessRunner | undefined;
44
- runCommand: string | null;
51
+ /** Ordered named commands; empty means nothing configured. */
52
+ commands: readonly HarnessNamedCommand[];
45
53
  /** Project root — the cwd every harness subprocess runs in. */
46
54
  cwd: string;
47
55
  /** CLI emits its one-line stderr banner through this hook before the first execution. */
48
- onBeforeFirstRun?: (expanded: string) => void;
56
+ onBeforeFirstRun?: (expanded: string, kind: HarnessCommandKind) => void;
49
57
  }
50
58
 
51
59
  export interface HarnessRunOutcome {
@@ -57,8 +65,6 @@ export interface HarnessRunOutcome {
57
65
 
58
66
  /** Plain-text placeholder expansion — capability ids are kebab-constrained. */
59
67
  export function expandRunCommand(command: string, target: HarnessTarget): string {
60
- // featurePath is a repo-relative spec path (llmanspec/specs/x.feature), POSIX by
61
- // contract on every host — keep '/'-splitting, do not switch to node:path here.
62
68
  const dirEnd = target.featurePath.lastIndexOf('/');
63
69
  const featureDir = dirEnd === -1 ? target.featurePath : target.featurePath.slice(0, dirEnd);
64
70
  return command
@@ -75,11 +81,40 @@ interface CacheEntry {
75
81
 
76
82
  const OUTPUT_TAIL = 200;
77
83
 
84
+ function kindLabels(kind: HarnessCommandKind): {
85
+ passed: string;
86
+ failed: string;
87
+ spawn: string;
88
+ } {
89
+ if (kind === 'dangling') {
90
+ return {
91
+ passed: 'dangling bdd check passed',
92
+ failed: 'dangling bdd check failed',
93
+ spawn: 'dangling bdd check could not start',
94
+ };
95
+ }
96
+ return {
97
+ passed: 'spec check passed',
98
+ failed: 'spec check failed',
99
+ spawn: 'spec check could not start',
100
+ };
101
+ }
102
+
103
+ function appendIssues(
104
+ map: Map<string, ValidationItem[]>,
105
+ capability: string,
106
+ issues: ValidationItem[],
107
+ ): void {
108
+ const existing = map.get(capability) ?? [];
109
+ map.set(capability, [...existing, ...issues]);
110
+ }
111
+
78
112
  /**
79
- * Trigger matrix (r13): default skips; a nested invocation skips
80
- * with a per-spec INFO; an explicit --check without a configured check_command
81
- * yields a single INFO on the first spec; otherwise every expanded command
82
- * executes at most once (cache keyed by the expanded string).
113
+ * Trigger matrix (r13/r181): default skips at the CLI (sends 'off'); a nested
114
+ * invocation skips with a per-spec INFO; an explicit --check with an empty
115
+ * command table yields a single INFO on the first spec; otherwise every
116
+ * expanded command executes at most once (cache keyed by kind + expanded
117
+ * string). Forward failure does not skip dangling.
83
118
  */
84
119
  export function runHarnessForSpecs(
85
120
  targets: readonly HarnessTarget[],
@@ -99,7 +134,8 @@ export function runHarnessForSpecs(
99
134
  }
100
135
  return { issuesByCapability, executed: false };
101
136
  }
102
- if (gate.runner === undefined || gate.runCommand === null || gate.runCommand === '') {
137
+ const commands = gate.commands.filter((c) => c.command.trim() !== '');
138
+ if (commands.length === 0) {
103
139
  if (gate.check === 'on' && targets.length > 0) {
104
140
  const first = targets[0] as HarnessTarget;
105
141
  issuesByCapability.set(first.capability, [
@@ -112,52 +148,59 @@ export function runHarnessForSpecs(
112
148
  }
113
149
  return { issuesByCapability, executed: false };
114
150
  }
115
- const cache = new Map<string, CacheEntry>();
116
151
  let executed = false;
117
152
  let announced = false;
118
- for (const target of targets) {
119
- const expanded = expandRunCommand(gate.runCommand, target);
120
- const cached = cache.get(expanded);
121
- let issues: ValidationItem[];
122
- if (cached !== undefined) {
123
- issues = cached.success
124
- ? [
125
- {
126
- level: 'INFO',
127
- id: target.featurePath,
128
- message: `spec check passed (cached): ${expanded}`,
129
- },
130
- ]
131
- : [
132
- {
133
- level: 'ERROR',
134
- id: target.featurePath,
135
- message: `spec check failed (cached result of ${expanded}): ${cached.failureSummary}`,
136
- },
137
- ];
138
- } else {
139
- if (!announced) {
140
- gate.onBeforeFirstRun?.(expanded);
141
- announced = true;
153
+ for (const named of commands) {
154
+ const labels = kindLabels(named.kind);
155
+ const cache = new Map<string, CacheEntry>();
156
+ for (const target of targets) {
157
+ const expanded = expandRunCommand(named.command, target);
158
+ const cached = cache.get(expanded);
159
+ let issues: ValidationItem[];
160
+ if (cached !== undefined) {
161
+ issues = cached.success
162
+ ? [
163
+ {
164
+ level: 'INFO',
165
+ id: target.featurePath,
166
+ message: `${labels.passed} (cached): ${expanded}`,
167
+ },
168
+ ]
169
+ : [
170
+ {
171
+ level: 'ERROR',
172
+ id: target.featurePath,
173
+ message: `${labels.failed} (cached result of ${expanded}): ${cached.failureSummary}`,
174
+ },
175
+ ];
176
+ } else if (gate.runner === undefined) {
177
+ const firstError = `${labels.spawn}: ${expanded}: harness runner missing`;
178
+ issues = [{ level: 'ERROR', id: target.featurePath, message: firstError }];
179
+ cache.set(expanded, { success: false, failureSummary: firstError.slice(0, OUTPUT_TAIL) });
180
+ } else {
181
+ if (!announced) {
182
+ gate.onBeforeFirstRun?.(expanded, named.kind);
183
+ announced = true;
184
+ }
185
+ executed = true;
186
+ const result = gate.runner.run(expanded, gate.cwd);
187
+ const firstError =
188
+ result.spawnError !== undefined
189
+ ? `${labels.spawn}: ${expanded}: ${result.spawnError}`
190
+ : result.exitCode === 0
191
+ ? null
192
+ : `${labels.failed} (exit ${result.exitCode}): ${expanded}: ${result.output.slice(-OUTPUT_TAIL)}`;
193
+ issues =
194
+ firstError === null
195
+ ? [{ level: 'INFO', id: target.featurePath, message: `${labels.passed}: ${expanded}` }]
196
+ : [{ level: 'ERROR', id: target.featurePath, message: firstError }];
197
+ cache.set(expanded, {
198
+ success: firstError === null,
199
+ failureSummary: firstError === null ? null : firstError.slice(0, OUTPUT_TAIL),
200
+ });
142
201
  }
143
- executed = true;
144
- const result = gate.runner.run(expanded, gate.cwd);
145
- const firstError =
146
- result.spawnError !== undefined
147
- ? `spec check could not start: ${expanded}: ${result.spawnError}`
148
- : result.exitCode === 0
149
- ? null
150
- : `spec check failed (exit ${result.exitCode}): ${expanded}: ${result.output.slice(-OUTPUT_TAIL)}`;
151
- issues =
152
- firstError === null
153
- ? [{ level: 'INFO', id: target.featurePath, message: `spec check passed: ${expanded}` }]
154
- : [{ level: 'ERROR', id: target.featurePath, message: firstError }];
155
- cache.set(expanded, {
156
- success: firstError === null,
157
- failureSummary: firstError === null ? null : firstError.slice(0, OUTPUT_TAIL),
158
- });
202
+ appendIssues(issuesByCapability, target.capability, issues);
159
203
  }
160
- issuesByCapability.set(target.capability, issues);
161
204
  }
162
205
  return { issuesByCapability, executed };
163
206
  }