@orcalang/orca-lang 0.1.18 → 0.1.21

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 (62) hide show
  1. package/dist/compiler/dt-compiler.d.ts +26 -0
  2. package/dist/compiler/dt-compiler.d.ts.map +1 -0
  3. package/dist/compiler/dt-compiler.js +387 -0
  4. package/dist/compiler/dt-compiler.js.map +1 -0
  5. package/dist/health-check.d.ts +3 -0
  6. package/dist/health-check.d.ts.map +1 -0
  7. package/dist/health-check.js +235 -0
  8. package/dist/health-check.js.map +1 -0
  9. package/dist/index.d.ts.map +1 -1
  10. package/dist/index.js +5 -1
  11. package/dist/index.js.map +1 -1
  12. package/dist/parser/ast-to-markdown.d.ts.map +1 -1
  13. package/dist/parser/ast-to-markdown.js +3 -1
  14. package/dist/parser/ast-to-markdown.js.map +1 -1
  15. package/dist/parser/ast.d.ts +3 -0
  16. package/dist/parser/ast.d.ts.map +1 -1
  17. package/dist/parser/dt-ast.d.ts +43 -0
  18. package/dist/parser/dt-ast.d.ts.map +1 -0
  19. package/dist/parser/dt-ast.js +3 -0
  20. package/dist/parser/dt-ast.js.map +1 -0
  21. package/dist/parser/dt-parser.d.ts +40 -0
  22. package/dist/parser/dt-parser.d.ts.map +1 -0
  23. package/dist/parser/dt-parser.js +240 -0
  24. package/dist/parser/dt-parser.js.map +1 -0
  25. package/dist/parser/markdown-parser.d.ts.map +1 -1
  26. package/dist/parser/markdown-parser.js +43 -8
  27. package/dist/parser/markdown-parser.js.map +1 -1
  28. package/dist/skills.d.ts +50 -1
  29. package/dist/skills.d.ts.map +1 -1
  30. package/dist/skills.js +508 -21
  31. package/dist/skills.js.map +1 -1
  32. package/dist/tools.d.ts.map +1 -1
  33. package/dist/tools.js +49 -0
  34. package/dist/tools.js.map +1 -1
  35. package/dist/verifier/dt-verifier.d.ts +32 -0
  36. package/dist/verifier/dt-verifier.d.ts.map +1 -0
  37. package/dist/verifier/dt-verifier.js +830 -0
  38. package/dist/verifier/dt-verifier.js.map +1 -0
  39. package/dist/verifier/properties.d.ts +4 -0
  40. package/dist/verifier/properties.d.ts.map +1 -1
  41. package/dist/verifier/properties.js +56 -20
  42. package/dist/verifier/properties.js.map +1 -1
  43. package/dist/verifier/structural.d.ts.map +1 -1
  44. package/dist/verifier/structural.js +6 -1
  45. package/dist/verifier/structural.js.map +1 -1
  46. package/dist/verifier/types.d.ts +4 -0
  47. package/dist/verifier/types.d.ts.map +1 -1
  48. package/package.json +3 -2
  49. package/src/compiler/dt-compiler.ts +454 -0
  50. package/src/health-check.ts +273 -0
  51. package/src/index.ts +5 -1
  52. package/src/parser/ast-to-markdown.ts +2 -1
  53. package/src/parser/ast.ts +4 -0
  54. package/src/parser/dt-ast.ts +40 -0
  55. package/src/parser/dt-parser.ts +289 -0
  56. package/src/parser/markdown-parser.ts +43 -8
  57. package/src/skills.ts +591 -22
  58. package/src/tools.ts +53 -0
  59. package/src/verifier/dt-verifier.ts +928 -0
  60. package/src/verifier/properties.ts +78 -23
  61. package/src/verifier/structural.ts +5 -1
  62. package/src/verifier/types.ts +4 -0
@@ -0,0 +1,928 @@
1
+ // Decision Table Verifier
2
+ // Checks: completeness, consistency, redundancy, structural integrity, co-location alignment,
3
+ // and machine integration (coverage gap + dead guard detection).
4
+
5
+ import { DecisionTableDef, ConditionDef, CellValue, Rule } from '../parser/dt-ast.js';
6
+ import { MachineDef, OrcaFile, ContextField, GuardExpression, ComparisonOp } from '../parser/ast.js';
7
+ import { VerificationError, VerificationResult, Severity } from './types.js';
8
+
9
+ // Helper to get all values for a condition
10
+ function getConditionValues(condition: ConditionDef): string[] {
11
+ if (condition.type === 'bool') {
12
+ return condition.values.length > 0 ? condition.values : ['true', 'false'];
13
+ }
14
+ return condition.values;
15
+ }
16
+
17
+ // Check if a cell matches a given value
18
+ function cellMatches(cell: CellValue, value: string): boolean {
19
+ switch (cell.kind) {
20
+ case 'any':
21
+ return true;
22
+ case 'exact':
23
+ return cell.value === value;
24
+ case 'negated':
25
+ return cell.value !== value;
26
+ case 'set':
27
+ return cell.values.includes(value);
28
+ }
29
+ }
30
+
31
+ // Check if a rule's conditions cover a specific input combination
32
+ function ruleMatchesInput(rule: Rule, conditionDefs: ConditionDef[], input: Map<string, string>): boolean {
33
+ for (const cond of conditionDefs) {
34
+ const cell = rule.conditions.get(cond.name);
35
+ if (!cell) continue; // No condition for this column means "any"
36
+ const expectedValue = input.get(cond.name);
37
+ if (!cellMatches(cell, expectedValue || '')) {
38
+ return false;
39
+ }
40
+ }
41
+ return true;
42
+ }
43
+
44
+ // Check if two rules overlap (can match the same input)
45
+ function rulesOverlap(rule1: Rule, rule2: Rule, conditionDefs: ConditionDef[]): boolean {
46
+ for (const cond of conditionDefs) {
47
+ const cell1 = rule1.conditions.get(cond.name);
48
+ const cell2 = rule2.conditions.get(cond.name);
49
+
50
+ // If either rule doesn't constrain this condition, they overlap on it
51
+ if (!cell1 || cell1.kind === 'any') continue;
52
+ if (!cell2 || cell2.kind === 'any') continue;
53
+
54
+ // Check if cells intersect
55
+ if (!cellsIntersect(cell1, cell2)) {
56
+ return false; // No overlap on this condition means no overall overlap
57
+ }
58
+ }
59
+ return true; // All constrained conditions intersect
60
+ }
61
+
62
+ // Check if two cells intersect (can match the same value)
63
+ function cellsIntersect(cell1: CellValue, cell2: CellValue): boolean {
64
+ // Any intersects with everything
65
+ if (cell1.kind === 'any' || cell2.kind === 'any') return true;
66
+
67
+ // Exact vs Exact
68
+ if (cell1.kind === 'exact' && cell2.kind === 'exact') {
69
+ return cell1.value === cell2.value;
70
+ }
71
+
72
+ // Exact vs Negated
73
+ if (cell1.kind === 'exact' && cell2.kind === 'negated') {
74
+ return cell2.value !== cell1.value;
75
+ }
76
+ if (cell1.kind === 'negated' && cell2.kind === 'exact') {
77
+ return cell1.value !== cell2.value;
78
+ }
79
+
80
+ // Exact vs Set
81
+ if (cell1.kind === 'exact' && cell2.kind === 'set') {
82
+ return cell2.values.includes(cell1.value);
83
+ }
84
+ if (cell1.kind === 'set' && cell2.kind === 'exact') {
85
+ return cell1.values.includes(cell2.value);
86
+ }
87
+
88
+ // Negated vs Negated
89
+ if (cell1.kind === 'negated' && cell2.kind === 'negated') {
90
+ return cell1.value !== cell2.value;
91
+ }
92
+
93
+ // Negated vs Set
94
+ if (cell1.kind === 'negated' && cell2.kind === 'set') {
95
+ return !cell2.values.includes(cell1.value);
96
+ }
97
+ if (cell1.kind === 'set' && cell2.kind === 'negated') {
98
+ return !cell1.values.includes(cell2.value);
99
+ }
100
+
101
+ // Set vs Set
102
+ if (cell1.kind === 'set' && cell2.kind === 'set') {
103
+ return cell1.values.some(v => cell2.values.includes(v));
104
+ }
105
+
106
+ return true;
107
+ }
108
+
109
+ // Generate all possible combinations of condition values
110
+ function generateCombinations(conditionDefs: ConditionDef[]): Map<string, string>[] {
111
+ const combinations: Map<string, string>[] = [];
112
+ const values = conditionDefs.map(c => getConditionValues(c));
113
+
114
+ function* cartesian(index: number, current: Map<string, string>): Generator<Map<string, string>> {
115
+ if (index === conditionDefs.length) {
116
+ yield new Map(current);
117
+ return;
118
+ }
119
+ const condName = conditionDefs[index].name;
120
+ for (const val of values[index]) {
121
+ current.set(condName, val);
122
+ yield* cartesian(index + 1, current);
123
+ current.delete(condName);
124
+ }
125
+ }
126
+
127
+ return [...cartesian(0, new Map())];
128
+ }
129
+
130
+ // Find which rules match a given input
131
+ function findMatchingRules(dt: DecisionTableDef, input: Map<string, string>): Rule[] {
132
+ return dt.rules.filter(rule => ruleMatchesInput(rule, dt.conditions, input));
133
+ }
134
+
135
+ // Check if two rules produce the same action outputs
136
+ function actionsMatch(rule1: Rule, rule2: Rule, actionNames: string[]): boolean {
137
+ for (const actionName of actionNames) {
138
+ const val1 = rule1.actions.get(actionName);
139
+ const val2 = rule2.actions.get(actionName);
140
+ if (val1 !== val2) return false;
141
+ }
142
+ return true;
143
+ }
144
+
145
+ // ============================================================
146
+ // Structural Checks
147
+ // ============================================================
148
+
149
+ function checkStructural(dt: DecisionTableDef): VerificationError[] {
150
+ const errors: VerificationError[] = [];
151
+
152
+ // DT_NO_CONDITIONS
153
+ if (dt.conditions.length === 0) {
154
+ errors.push({
155
+ code: 'DT_NO_CONDITIONS',
156
+ message: 'Decision table has no conditions declared',
157
+ severity: 'error',
158
+ location: { decisionTable: dt.name },
159
+ suggestion: 'Add a ## conditions section with at least one condition',
160
+ });
161
+ }
162
+
163
+ // DT_NO_ACTIONS
164
+ if (dt.actions.length === 0) {
165
+ errors.push({
166
+ code: 'DT_NO_ACTIONS',
167
+ message: 'Decision table has no actions declared',
168
+ severity: 'error',
169
+ location: { decisionTable: dt.name },
170
+ suggestion: 'Add an ## actions section with at least one action',
171
+ });
172
+ }
173
+
174
+ // DT_EMPTY_RULES
175
+ if (dt.rules.length === 0) {
176
+ errors.push({
177
+ code: 'DT_EMPTY_RULES',
178
+ message: 'Decision table has no rules',
179
+ severity: 'warning',
180
+ location: { decisionTable: dt.name },
181
+ suggestion: 'Add rules to the ## rules section',
182
+ });
183
+ return errors; // No point checking rule content if there are no rules
184
+ }
185
+
186
+ const conditionNames = new Set(dt.conditions.map(c => c.name));
187
+ const actionNames = new Set(dt.actions.map(a => a.name));
188
+ const ruleConditionNames = new Set<string>();
189
+ const ruleActionNames = new Set<string>();
190
+
191
+ // Check each rule
192
+ for (let ruleIdx = 0; ruleIdx < dt.rules.length; ruleIdx++) {
193
+ const rule = dt.rules[ruleIdx];
194
+ const ruleNum = rule.number ?? ruleIdx + 1;
195
+
196
+ // Check condition columns
197
+ for (const [condName] of rule.conditions) {
198
+ ruleConditionNames.add(condName);
199
+
200
+ // DT_UNKNOWN_CONDITION_COLUMN
201
+ if (!conditionNames.has(condName)) {
202
+ errors.push({
203
+ code: 'DT_UNKNOWN_CONDITION_COLUMN',
204
+ message: `Rule ${ruleNum} has unknown condition column "${condName}"`,
205
+ severity: 'warning',
206
+ location: { decisionTable: dt.name, rule: ruleNum, condition: condName },
207
+ suggestion: `Remove or rename the column to match a declared condition`,
208
+ });
209
+ } else {
210
+ // DT_UNKNOWN_CONDITION_VALUE
211
+ const cond = dt.conditions.find(c => c.name === condName)!;
212
+ const cell = rule.conditions.get(condName)!;
213
+
214
+ // Skip value validation for int_range — values are ranges, not enum values
215
+ if (cell.kind !== 'any' && cond.type !== 'string' && cond.type !== 'int_range') {
216
+ const validValues = getConditionValues(cond);
217
+ if (cell.kind === 'exact' && !validValues.includes(cell.value)) {
218
+ errors.push({
219
+ code: 'DT_UNKNOWN_CONDITION_VALUE',
220
+ message: `Rule ${ruleNum} condition "${condName}" has value "${cell.value}" not in declared values`,
221
+ severity: 'error',
222
+ location: { decisionTable: dt.name, rule: ruleNum, condition: condName },
223
+ suggestion: `Change to one of: ${validValues.join(', ')}`,
224
+ });
225
+ } else if (cell.kind === 'negated' && !validValues.includes(cell.value)) {
226
+ errors.push({
227
+ code: 'DT_UNKNOWN_CONDITION_VALUE',
228
+ message: `Rule ${ruleNum} condition "${condName}" negates "${cell.value}" which is not in declared values`,
229
+ severity: 'error',
230
+ location: { decisionTable: dt.name, rule: ruleNum, condition: condName },
231
+ suggestion: `Change to negate one of: ${validValues.join(', ')}`,
232
+ });
233
+ } else if (cell.kind === 'set') {
234
+ for (const v of cell.values) {
235
+ if (!validValues.includes(v)) {
236
+ errors.push({
237
+ code: 'DT_UNKNOWN_CONDITION_VALUE',
238
+ message: `Rule ${ruleNum} condition "${condName}" has value "${v}" not in declared values`,
239
+ severity: 'error',
240
+ location: { decisionTable: dt.name, rule: ruleNum, condition: condName },
241
+ suggestion: `Change to one of: ${validValues.join(', ')}`,
242
+ });
243
+ }
244
+ }
245
+ }
246
+ }
247
+ }
248
+ }
249
+
250
+ // Check action columns
251
+ for (const [actionName] of rule.actions) {
252
+ ruleActionNames.add(actionName);
253
+
254
+ // DT_UNKNOWN_ACTION_COLUMN
255
+ if (!actionNames.has(actionName)) {
256
+ errors.push({
257
+ code: 'DT_UNKNOWN_ACTION_COLUMN',
258
+ message: `Rule ${ruleNum} has unknown action column "${actionName}"`,
259
+ severity: 'warning',
260
+ location: { decisionTable: dt.name, rule: ruleNum, action: actionName },
261
+ suggestion: `Remove or rename the column to match a declared action`,
262
+ });
263
+ } else {
264
+ // DT_UNKNOWN_ACTION_VALUE
265
+ const action = dt.actions.find(a => a.name === actionName)!;
266
+ const value = rule.actions.get(actionName)!;
267
+
268
+ if (action.type === 'enum' && action.values) {
269
+ if (!action.values.includes(value)) {
270
+ errors.push({
271
+ code: 'DT_UNKNOWN_ACTION_VALUE',
272
+ message: `Rule ${ruleNum} action "${actionName}" has value "${value}" not in declared values`,
273
+ severity: 'error',
274
+ location: { decisionTable: dt.name, rule: ruleNum, action: actionName },
275
+ suggestion: `Change to one of: ${action.values.join(', ')}`,
276
+ });
277
+ }
278
+ }
279
+ }
280
+ }
281
+ }
282
+
283
+ // DT_MISSING_CONDITION_COLUMN
284
+ for (const condName of conditionNames) {
285
+ if (!ruleConditionNames.has(condName)) {
286
+ errors.push({
287
+ code: 'DT_MISSING_CONDITION_COLUMN',
288
+ message: `Condition "${condName}" is declared but has no column in the rules table`,
289
+ severity: 'warning',
290
+ location: { decisionTable: dt.name, condition: condName },
291
+ suggestion: `Add a "${condName}" column to the rules table`,
292
+ });
293
+ }
294
+ }
295
+
296
+ // DT_MISSING_ACTION_COLUMN
297
+ for (const actionName of actionNames) {
298
+ if (!ruleActionNames.has(actionName)) {
299
+ errors.push({
300
+ code: 'DT_MISSING_ACTION_COLUMN',
301
+ message: `Action "${actionName}" is declared but has no corresponding column in the rules table`,
302
+ severity: 'warning',
303
+ location: { decisionTable: dt.name, action: actionName },
304
+ suggestion: `Add a "→ ${actionName}" column to the rules table`,
305
+ });
306
+ }
307
+ }
308
+
309
+ // DT_DUPLICATE_RULE
310
+ for (let i = 0; i < dt.rules.length; i++) {
311
+ for (let j = i + 1; j < dt.rules.length; j++) {
312
+ const rule1 = dt.rules[i];
313
+ const rule2 = dt.rules[j];
314
+
315
+ // Check if conditions are identical
316
+ let identical = true;
317
+ if (rule1.conditions.size !== rule2.conditions.size) {
318
+ identical = false;
319
+ } else {
320
+ for (const [name, cell1] of rule1.conditions) {
321
+ const cell2 = rule2.conditions.get(name);
322
+ if (!cell2 || !cellsEqual(cell1, cell2)) {
323
+ identical = false;
324
+ break;
325
+ }
326
+ }
327
+ }
328
+
329
+ if (identical) {
330
+ const rule1Num = rule1.number ?? i + 1;
331
+ const rule2Num = rule2.number ?? j + 1;
332
+ errors.push({
333
+ code: 'DT_DUPLICATE_RULE',
334
+ message: `Rule ${rule1Num} and Rule ${rule2Num} have identical condition patterns`,
335
+ severity: 'warning',
336
+ location: { decisionTable: dt.name, rule: rule2Num },
337
+ suggestion: `Remove or modify one of the duplicate rules`,
338
+ });
339
+ }
340
+ }
341
+ }
342
+
343
+ return errors;
344
+ }
345
+
346
+ function cellsEqual(cell1: CellValue, cell2: CellValue): boolean {
347
+ if (cell1.kind !== cell2.kind) return false;
348
+ // Now cell1 and cell2 have the same kind, narrow both
349
+ const kind = cell1.kind;
350
+ if (kind === 'any') return true;
351
+ if (kind === 'exact') {
352
+ return (cell1 as { kind: 'exact'; value: string }).value === (cell2 as { kind: 'exact'; value: string }).value;
353
+ }
354
+ if (kind === 'negated') {
355
+ return (cell1 as { kind: 'negated'; value: string }).value === (cell2 as { kind: 'negated'; value: string }).value;
356
+ }
357
+ if (kind === 'set') {
358
+ const s1 = cell1 as { kind: 'set'; values: string[] };
359
+ const s2 = cell2 as { kind: 'set'; values: string[] };
360
+ return s1.values.length === s2.values.length && s1.values.every(v => s2.values.includes(v));
361
+ }
362
+ return true;
363
+ }
364
+
365
+ // ============================================================
366
+ // Completeness Check
367
+ // ============================================================
368
+
369
+ function checkCompleteness(dt: DecisionTableDef): VerificationError[] {
370
+ const errors: VerificationError[] = [];
371
+
372
+ if (dt.conditions.length === 0) return errors; // Already reported in structural
373
+
374
+ // int_range conditions cannot be exhaustively enumerated — skip completeness check
375
+ if (dt.conditions.some(c => c.type === 'int_range')) {
376
+ errors.push({
377
+ code: 'DT_COMPLETENESS_SKIPPED',
378
+ message: 'Completeness check skipped: int_range conditions cannot be exhaustively enumerated',
379
+ severity: 'warning',
380
+ location: { decisionTable: dt.name },
381
+ suggestion: 'Manually verify that all numeric ranges are covered without gaps',
382
+ });
383
+ return errors;
384
+ }
385
+
386
+ // Calculate total combinations for enum/bool conditions
387
+ let totalCombinations = 1;
388
+ for (const cond of dt.conditions) {
389
+ const values = getConditionValues(cond);
390
+ if (values.length === 0) {
391
+ totalCombinations = Infinity;
392
+ break;
393
+ }
394
+ totalCombinations *= values.length;
395
+ if (totalCombinations > 4096) break;
396
+ }
397
+
398
+ if (totalCombinations > 4096) {
399
+ errors.push({
400
+ code: 'DT_COMPLETENESS_SKIPPED',
401
+ message: `Completeness check skipped: ${totalCombinations} combinations exceed limit of 4096`,
402
+ severity: 'warning',
403
+ location: { decisionTable: dt.name },
404
+ suggestion: 'Consider simplifying conditions or using wildcards to reduce combination count',
405
+ });
406
+ return errors;
407
+ }
408
+
409
+ // Generate all combinations and check coverage
410
+ const combinations = generateCombinations(dt.conditions);
411
+ const actionNames = dt.actions.map(a => a.name);
412
+
413
+ for (const combo of combinations) {
414
+ const matchingRules = findMatchingRules(dt, combo);
415
+
416
+ if (matchingRules.length === 0) {
417
+ const comboDesc = [...combo.entries()].map(([k, v]) => `${k}=${v}`).join(', ');
418
+ errors.push({
419
+ code: 'DT_INCOMPLETE',
420
+ message: `Missing coverage for: ${comboDesc}`,
421
+ severity: 'error',
422
+ location: { decisionTable: dt.name },
423
+ suggestion: `Add a rule to cover this condition combination`,
424
+ });
425
+ }
426
+ }
427
+
428
+ return errors;
429
+ }
430
+
431
+ // ============================================================
432
+ // Consistency Check
433
+ // ============================================================
434
+
435
+ function checkConsistency(dt: DecisionTableDef): VerificationError[] {
436
+ const errors: VerificationError[] = [];
437
+ const isAllMatch = dt.policy === 'all-match';
438
+
439
+ for (let i = 0; i < dt.rules.length; i++) {
440
+ for (let j = i + 1; j < dt.rules.length; j++) {
441
+ const rule1 = dt.rules[i];
442
+ const rule2 = dt.rules[j];
443
+
444
+ // Check if rules overlap
445
+ if (!rulesOverlap(rule1, rule2, dt.conditions)) continue;
446
+
447
+ // Rules overlap - check if actions agree
448
+ const actionNames = dt.actions.map(a => a.name);
449
+ const actionsAgree = actionsMatch(rule1, rule2, actionNames);
450
+
451
+ if (!actionsAgree) {
452
+ const rule1Num = rule1.number ?? i + 1;
453
+ const rule2Num = rule2.number ?? j + 1;
454
+ const severity: Severity = isAllMatch ? 'error' : 'warning';
455
+ const code = isAllMatch ? 'DT_INCONSISTENT' : 'DT_INCONSISTENT';
456
+
457
+ const overlappingConditions: string[] = [];
458
+ for (const cond of dt.conditions) {
459
+ const cell1 = rule1.conditions.get(cond.name);
460
+ const cell2 = rule2.conditions.get(cond.name);
461
+ if (cell1 && cell2 && cellsIntersect(cell1, cell2)) {
462
+ overlappingConditions.push(cond.name);
463
+ }
464
+ }
465
+
466
+ errors.push({
467
+ code,
468
+ message: `Rules ${rule1Num} and ${rule2Num} can match the same input but produce different results`,
469
+ severity,
470
+ location: { decisionTable: dt.name, rule: rule2Num },
471
+ suggestion: `For ${isAllMatch ? 'all-match' : 'first-match'} policy: review overlapping rules on conditions: ${overlappingConditions.join(', ')}`,
472
+ });
473
+ }
474
+ }
475
+ }
476
+
477
+ return errors;
478
+ }
479
+
480
+ // ============================================================
481
+ // Redundancy Check
482
+ // ============================================================
483
+
484
+ function checkRedundancy(dt: DecisionTableDef): VerificationError[] {
485
+ const errors: VerificationError[] = [];
486
+
487
+ // For each rule, check if it's fully covered by earlier rules with same actions
488
+ for (let ruleIdx = 1; ruleIdx < dt.rules.length; ruleIdx++) {
489
+ const rule = dt.rules[ruleIdx];
490
+ const ruleNum = rule.number ?? ruleIdx + 1;
491
+ const actionNames = dt.actions.map(a => a.name);
492
+
493
+ // A rule is redundant if at least one earlier rule overlaps it AND all overlapping
494
+ // earlier rules produce the same actions (meaning first-match would always hit them first
495
+ // with an identical result, so this rule can never change the outcome).
496
+ let hasOverlappingPredecessor = false;
497
+ let allOverlappingHaveSameActions = true;
498
+
499
+ for (let prevIdx = 0; prevIdx < ruleIdx; prevIdx++) {
500
+ const prevRule = dt.rules[prevIdx];
501
+
502
+ if (!rulesOverlap(prevRule, rule, dt.conditions)) continue;
503
+
504
+ hasOverlappingPredecessor = true;
505
+
506
+ if (!actionsMatch(prevRule, rule, actionNames)) {
507
+ allOverlappingHaveSameActions = false;
508
+ break;
509
+ }
510
+ }
511
+
512
+ if (hasOverlappingPredecessor && allOverlappingHaveSameActions) {
513
+ errors.push({
514
+ code: 'DT_REDUNDANT',
515
+ message: `Rule ${ruleNum} is redundant — earlier rules cover all its cases with the same actions`,
516
+ severity: 'warning',
517
+ location: { decisionTable: dt.name, rule: ruleNum },
518
+ suggestion: `Remove this rule or make it more specific`,
519
+ });
520
+ }
521
+ }
522
+
523
+ return errors;
524
+ }
525
+
526
+ // ============================================================
527
+ // Main Verifier
528
+ // ============================================================
529
+
530
+ export function verifyDecisionTable(dt: DecisionTableDef): VerificationResult {
531
+ const errors: VerificationError[] = [];
532
+
533
+ // Run structural checks first
534
+ errors.push(...checkStructural(dt));
535
+
536
+ // Run semantic checks (only if basic structure is valid)
537
+ const hasNoConditions = dt.conditions.length === 0;
538
+ const hasNoActions = dt.actions.length === 0;
539
+
540
+ if (!hasNoConditions && !hasNoActions) {
541
+ errors.push(...checkCompleteness(dt));
542
+ errors.push(...checkConsistency(dt));
543
+ errors.push(...checkRedundancy(dt));
544
+ }
545
+
546
+ return {
547
+ valid: !errors.some(e => e.severity === 'error'),
548
+ errors,
549
+ };
550
+ }
551
+
552
+ export function verifyDecisionTables(dts: DecisionTableDef[]): VerificationResult {
553
+ const allErrors: VerificationError[] = [];
554
+
555
+ for (const dt of dts) {
556
+ const result = verifyDecisionTable(dt);
557
+ allErrors.push(...result.errors);
558
+ }
559
+
560
+ return {
561
+ valid: !allErrors.some(e => e.severity === 'error'),
562
+ errors: allErrors,
563
+ };
564
+ }
565
+
566
+ // ============================================================
567
+ // Co-location Alignment Check
568
+ // ============================================================
569
+
570
+ /**
571
+ * Check that every condition name and output name in a co-located decision table
572
+ * exists as a context field in the machine. When a DT and machine are in the same
573
+ * file, this contract allows action generation to produce fully-wired code.
574
+ */
575
+ export function checkDTContextAlignment(
576
+ dt: DecisionTableDef,
577
+ machine: MachineDef
578
+ ): VerificationError[] {
579
+ const errors: VerificationError[] = [];
580
+ const contextNames = new Set(machine.context.map(f => f.name));
581
+
582
+ for (const cond of dt.conditions) {
583
+ if (!contextNames.has(cond.name)) {
584
+ errors.push({
585
+ code: 'DT_CONTEXT_MISMATCH',
586
+ message: `Decision table '${dt.name}' condition '${cond.name}' has no matching context field in machine '${machine.name}'`,
587
+ severity: 'error',
588
+ location: { decisionTable: dt.name, condition: cond.name },
589
+ suggestion: `Add '${cond.name}' to the ## context section, or rename the condition to match an existing context field`,
590
+ });
591
+ }
592
+ }
593
+
594
+ for (const action of dt.actions) {
595
+ if (!contextNames.has(action.name)) {
596
+ errors.push({
597
+ code: 'DT_CONTEXT_MISMATCH',
598
+ message: `Decision table '${dt.name}' output '${action.name}' has no matching context field in machine '${machine.name}'`,
599
+ severity: 'error',
600
+ location: { decisionTable: dt.name, action: action.name },
601
+ suggestion: `Add '${action.name}' to the ## context section, or rename the output to match an existing context field`,
602
+ });
603
+ }
604
+ }
605
+
606
+ return errors;
607
+ }
608
+
609
+ /**
610
+ * For a file with exactly one machine and one or more decision tables, verify
611
+ * that every DT condition and output name matches a machine context field.
612
+ * Multi-machine files are skipped (ambiguous ownership).
613
+ */
614
+ export function checkFileContextAlignment(file: OrcaFile): VerificationError[] {
615
+ if (file.machines.length !== 1 || file.decisionTables.length === 0) {
616
+ return [];
617
+ }
618
+ const machine = file.machines[0];
619
+ const errors: VerificationError[] = [];
620
+ for (const dt of file.decisionTables) {
621
+ errors.push(...checkDTContextAlignment(dt, machine));
622
+ }
623
+ return errors;
624
+ }
625
+
626
+ // ============================================================
627
+ // Machine Integration Checks
628
+ // ============================================================
629
+
630
+ /**
631
+ * Get the enumerable values for a machine context field.
632
+ * Returns null for types that cannot be exhaustively enumerated (string, int, decimal).
633
+ * For enum fields, values are stored as a comma-separated defaultValue string.
634
+ */
635
+ function getMachineFieldValues(field: ContextField): string[] | null {
636
+ if (field.type.kind === 'bool') return ['true', 'false'];
637
+ if (field.type.kind === 'custom' && field.type.name === 'enum') {
638
+ if (!field.defaultValue) return null;
639
+ const vals = field.defaultValue.split(',').map(v => v.trim()).filter(Boolean);
640
+ return vals.length > 0 ? vals : null;
641
+ }
642
+ return null;
643
+ }
644
+
645
+ /**
646
+ * Generate all combinations of condition values using machine context values as the
647
+ * input domain. Returns null if any condition cannot be enumerated or if the
648
+ * total combinations exceed the safety limit.
649
+ */
650
+ function generateMachineContextCombinations(
651
+ dt: DecisionTableDef,
652
+ contextMap: Map<string, ContextField>
653
+ ): Map<string, string>[] | null {
654
+ const domainPerCondition: string[][] = [];
655
+
656
+ for (const cond of dt.conditions) {
657
+ const field = contextMap.get(cond.name);
658
+ if (!field) return null; // Alignment not met — caller should have checked
659
+ const vals = getMachineFieldValues(field);
660
+ if (!vals) return null; // Non-enumerable type
661
+ domainPerCondition.push(vals);
662
+ }
663
+
664
+ // Safety limit
665
+ let total = 1;
666
+ for (const vals of domainPerCondition) total *= vals.length;
667
+ if (total > 4096) return null;
668
+
669
+ // Cartesian product
670
+ const combos: Map<string, string>[] = [];
671
+ function cartesian(idx: number, current: Map<string, string>): void {
672
+ if (idx === dt.conditions.length) {
673
+ combos.push(new Map(current));
674
+ return;
675
+ }
676
+ const condName = dt.conditions[idx].name;
677
+ for (const val of domainPerCondition[idx]) {
678
+ current.set(condName, val);
679
+ cartesian(idx + 1, current);
680
+ current.delete(condName);
681
+ }
682
+ }
683
+ cartesian(0, new Map());
684
+ return combos;
685
+ }
686
+
687
+ /**
688
+ * DT_COVERAGE_GAP: Decision table must cover all input combinations the machine
689
+ * context can actually produce. Uses machine enum/bool values as the authoritative
690
+ * domain — stricter than DT_INCOMPLETE which only checks DT-declared values.
691
+ */
692
+ function checkDTCoverageGap(
693
+ dt: DecisionTableDef,
694
+ contextMap: Map<string, ContextField>
695
+ ): VerificationError[] {
696
+ const errors: VerificationError[] = [];
697
+
698
+ const combos = generateMachineContextCombinations(dt, contextMap);
699
+ if (!combos) return errors; // Non-enumerable conditions or too many combinations
700
+
701
+ for (const combo of combos) {
702
+ const matched = dt.rules.some(rule => ruleMatchesInput(rule, dt.conditions, combo));
703
+ if (!matched) {
704
+ const comboDesc = [...combo.entries()].map(([k, v]) => `${k}=${v}`).join(', ');
705
+ errors.push({
706
+ code: 'DT_COVERAGE_GAP',
707
+ message: `Decision table '${dt.name}' has no rule for machine context combination: ${comboDesc}`,
708
+ severity: 'error',
709
+ location: { decisionTable: dt.name },
710
+ suggestion: `Add a rule covering this combination, or add a catch-all row using '-' wildcards`,
711
+ });
712
+ }
713
+ }
714
+
715
+ return errors;
716
+ }
717
+
718
+ /**
719
+ * Recursively collect all equality comparisons from a guard expression.
720
+ * Returns tuples of (fieldName, op, comparedValue) for any `ctx.X op Y` node.
721
+ */
722
+ function collectFieldComparisons(
723
+ expr: GuardExpression
724
+ ): Array<{ field: string; op: ComparisonOp; value: string }> {
725
+ if (expr.kind === 'compare') {
726
+ // Only handle ctx.fieldName comparisons
727
+ if (expr.left.path.length === 2 && expr.left.path[0] === 'ctx') {
728
+ return [{ field: expr.left.path[1], op: expr.op, value: String(expr.right.value) }];
729
+ }
730
+ return [];
731
+ }
732
+ if (expr.kind === 'not') return collectFieldComparisons(expr.expr);
733
+ if (expr.kind === 'and' || expr.kind === 'or') {
734
+ return [...collectFieldComparisons(expr.left), ...collectFieldComparisons(expr.right)];
735
+ }
736
+ return [];
737
+ }
738
+
739
+ /**
740
+ * Compute the set of guard names that test a DT output field against a value
741
+ * the DT never produces. These guards are always false after the DT action fires.
742
+ */
743
+ function computeDeadGuardNames(dt: DecisionTableDef, machine: MachineDef): Set<string> {
744
+ const outputDomain = new Map<string, Set<string>>();
745
+ for (const action of dt.actions) {
746
+ outputDomain.set(action.name, new Set<string>());
747
+ }
748
+ for (const rule of dt.rules) {
749
+ for (const [name, value] of rule.actions) {
750
+ outputDomain.get(name)?.add(value);
751
+ }
752
+ }
753
+
754
+ const outputFields = new Set(dt.actions.map(a => a.name));
755
+ const dead = new Set<string>();
756
+
757
+ for (const guardDef of machine.guards) {
758
+ const comparisons = collectFieldComparisons(guardDef.expression);
759
+ for (const { field, op, value } of comparisons) {
760
+ if (!outputFields.has(field)) continue;
761
+ if (op !== 'eq') continue;
762
+ const possible = outputDomain.get(field)!;
763
+ if (!possible.has(value)) {
764
+ dead.add(guardDef.name);
765
+ }
766
+ }
767
+ }
768
+
769
+ return dead;
770
+ }
771
+
772
+ /**
773
+ * DT_GUARD_DEAD: A guard that compares a DT output field against a value the DT
774
+ * never produces is a dead guard — it can never be true immediately after the
775
+ * DT action fires. Reported as a warning since another action might set the field.
776
+ */
777
+ function checkDTGuardDead(dt: DecisionTableDef, machine: MachineDef): VerificationError[] {
778
+ const errors: VerificationError[] = [];
779
+
780
+ // Build output domain: field → set of values the DT can produce
781
+ const outputDomain = new Map<string, Set<string>>();
782
+ for (const action of dt.actions) {
783
+ outputDomain.set(action.name, new Set<string>());
784
+ }
785
+ for (const rule of dt.rules) {
786
+ for (const [name, value] of rule.actions) {
787
+ outputDomain.get(name)?.add(value);
788
+ }
789
+ }
790
+
791
+ const outputFields = new Set(dt.actions.map(a => a.name));
792
+
793
+ for (const guardDef of machine.guards) {
794
+ const comparisons = collectFieldComparisons(guardDef.expression);
795
+ for (const { field, op, value } of comparisons) {
796
+ if (!outputFields.has(field)) continue; // Not a DT output field
797
+ if (op !== 'eq') continue; // Only equality checks are conclusive
798
+
799
+ const possible = outputDomain.get(field)!;
800
+ if (!possible.has(value)) {
801
+ const possibleList = [...possible].join(', ') || '(none)';
802
+ errors.push({
803
+ code: 'DT_GUARD_DEAD',
804
+ message: `Guard '${guardDef.name}' tests '${field} == ${value}' but '${dt.name}' never outputs '${value}' for '${field}' (possible: ${possibleList})`,
805
+ severity: 'warning',
806
+ location: { decisionTable: dt.name, condition: field },
807
+ suggestion: `Update '${dt.name}' to produce '${value}' for '${field}', or remove this guard`,
808
+ });
809
+ }
810
+ }
811
+ }
812
+
813
+ return errors;
814
+ }
815
+
816
+ /**
817
+ * BFS from initial state, optionally skipping transitions guarded by dead guards.
818
+ * A non-negated transition guarded by a name in `deadGuards` is skipped (never fires).
819
+ * A negated dead guard (!dead) is NOT skipped — negation of a dead guard is always true.
820
+ */
821
+ function bfsReachableWithDeadGuards(machine: MachineDef, deadGuards: Set<string>): Set<string> {
822
+ const initial = machine.states.find(s => s.isInitial);
823
+ if (!initial) return new Set();
824
+
825
+ const visited = new Set<string>();
826
+ const queue = [initial.name];
827
+
828
+ while (queue.length > 0) {
829
+ const state = queue.shift()!;
830
+ if (visited.has(state)) continue;
831
+ visited.add(state);
832
+
833
+ for (const t of machine.transitions) {
834
+ if (t.source !== state) continue;
835
+ // A non-negated dead guard means the transition can never fire
836
+ if (t.guard && !t.guard.negated && deadGuards.has(t.guard.name)) continue;
837
+ if (!visited.has(t.target)) queue.push(t.target);
838
+ }
839
+ }
840
+
841
+ return visited;
842
+ }
843
+
844
+ /**
845
+ * DT_UNREACHABLE_STATE: A state that is graph-reachable but only accessible via
846
+ * transitions guarded by dead guards — it can never be entered given DT outputs.
847
+ * Reported as a warning (structural reachability is preserved; the constraint is semantic).
848
+ */
849
+ function checkDTDeadGuardReachability(dt: DecisionTableDef, machine: MachineDef): VerificationError[] {
850
+ const deadGuards = computeDeadGuardNames(dt, machine);
851
+ if (deadGuards.size === 0) return [];
852
+
853
+ const plainReachable = bfsReachableWithDeadGuards(machine, new Set());
854
+ const dtReachable = bfsReachableWithDeadGuards(machine, deadGuards);
855
+
856
+ const errors: VerificationError[] = [];
857
+ const deadList = [...deadGuards].join(', ');
858
+
859
+ for (const state of machine.states) {
860
+ if (plainReachable.has(state.name) && !dtReachable.has(state.name)) {
861
+ errors.push({
862
+ code: 'DT_UNREACHABLE_STATE',
863
+ message: `State '${state.name}' is unreachable given '${dt.name}' output constraints — all entry paths are gated by dead guards (${deadList})`,
864
+ severity: 'warning',
865
+ location: { state: state.name, decisionTable: dt.name },
866
+ suggestion: `Update '${dt.name}' to produce values that satisfy the guards leading to '${state.name}', or revise the guard expressions`,
867
+ });
868
+ }
869
+ }
870
+
871
+ return errors;
872
+ }
873
+
874
+ /**
875
+ * Check DT integration with the machine: coverage gap, dead guards, and
876
+ * DT-constrained reachability. Only runs when exactly one machine is present
877
+ * and the DT is fully aligned. Multi-machine files are skipped (ambiguous ownership).
878
+ */
879
+ export function checkDTMachineIntegration(file: OrcaFile): VerificationError[] {
880
+ if (file.machines.length !== 1 || file.decisionTables.length === 0) {
881
+ return [];
882
+ }
883
+ const machine = file.machines[0];
884
+ const contextMap = new Map(machine.context.map(f => [f.name, f]));
885
+ const errors: VerificationError[] = [];
886
+
887
+ for (const dt of file.decisionTables) {
888
+ // Only verify DTs that are fully aligned with machine context
889
+ const allAligned = [...dt.conditions, ...dt.actions].every(item => contextMap.has(item.name));
890
+ if (!allAligned) continue;
891
+
892
+ errors.push(...checkDTCoverageGap(dt, contextMap));
893
+ errors.push(...checkDTGuardDead(dt, machine));
894
+ errors.push(...checkDTDeadGuardReachability(dt, machine));
895
+ }
896
+
897
+ return errors;
898
+ }
899
+
900
+ /**
901
+ * Compute the merged output domain across all aligned DTs in a single-machine file.
902
+ * Returns a map from DT output field name → set of values the DT(s) can produce.
903
+ * Used by the properties checker to prune guard-protected transitions that are
904
+ * semantically impossible given DT output constraints.
905
+ * Returns undefined if no aligned DTs are found.
906
+ */
907
+ export function computeAlignedDTOutputDomain(file: OrcaFile): Map<string, Set<string>> | undefined {
908
+ if (file.machines.length !== 1 || file.decisionTables.length === 0) return undefined;
909
+ const machine = file.machines[0];
910
+ const contextMap = new Map(machine.context.map(f => [f.name, f]));
911
+
912
+ const domain = new Map<string, Set<string>>();
913
+
914
+ for (const dt of file.decisionTables) {
915
+ const allAligned = [...dt.conditions, ...dt.actions].every(item => contextMap.has(item.name));
916
+ if (!allAligned) continue;
917
+
918
+ for (const actionDef of dt.actions) {
919
+ if (!domain.has(actionDef.name)) domain.set(actionDef.name, new Set());
920
+ for (const rule of dt.rules) {
921
+ const val = rule.actions.get(actionDef.name);
922
+ if (val !== undefined) domain.get(actionDef.name)!.add(val);
923
+ }
924
+ }
925
+ }
926
+
927
+ return domain.size > 0 ? domain : undefined;
928
+ }