al-sem 0.0.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 (231) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +361 -0
  3. package/package.json +64 -0
  4. package/scripts/d40-diff.ts +44 -0
  5. package/scripts/fetch-native-parser.ts +179 -0
  6. package/scripts/precision-sample.ts +99 -0
  7. package/scripts/precision-study.ts +42 -0
  8. package/scripts/precision-tabulate.ts +52 -0
  9. package/src/cli/baseline.ts +31 -0
  10. package/src/cli/diff.ts +199 -0
  11. package/src/cli/events-chains.ts +56 -0
  12. package/src/cli/events-fanout.ts +87 -0
  13. package/src/cli/exit-code.ts +30 -0
  14. package/src/cli/fingerprint-indexes.ts +130 -0
  15. package/src/cli/fingerprint-query.ts +543 -0
  16. package/src/cli/fingerprint-witness.ts +493 -0
  17. package/src/cli/fingerprint.ts +292 -0
  18. package/src/cli/format-compact-json.ts +45 -0
  19. package/src/cli/format-events.ts +77 -0
  20. package/src/cli/format-fingerprint.ts +295 -0
  21. package/src/cli/format-html.ts +503 -0
  22. package/src/cli/format-json.ts +13 -0
  23. package/src/cli/format-policy.ts +95 -0
  24. package/src/cli/format-sarif.ts +186 -0
  25. package/src/cli/format-terminal.ts +153 -0
  26. package/src/cli/index.ts +566 -0
  27. package/src/cli/policy.ts +204 -0
  28. package/src/config/roots-config.ts +302 -0
  29. package/src/deps/cache-versions.ts +74 -0
  30. package/src/deps/canonical-json.ts +27 -0
  31. package/src/deps/dependency-artifact.ts +144 -0
  32. package/src/deps/dependency-cache.ts +262 -0
  33. package/src/deps/dependency-dag.ts +128 -0
  34. package/src/deps/dependency-package-discovery.ts +85 -0
  35. package/src/deps/dependency-pipeline.ts +483 -0
  36. package/src/deps/dependency-projection.ts +211 -0
  37. package/src/deps/dependency-resolver.ts +154 -0
  38. package/src/deps/workspace-dependencies.ts +114 -0
  39. package/src/detectors/capability-query.ts +145 -0
  40. package/src/detectors/confidence.ts +52 -0
  41. package/src/detectors/d1-db-op-in-loop.ts +457 -0
  42. package/src/detectors/d10-self-modifying-loop.ts +114 -0
  43. package/src/detectors/d11-modify-without-get.ts +129 -0
  44. package/src/detectors/d12-dead-integration-event.ts +81 -0
  45. package/src/detectors/d13-cross-app-internal-call.ts +105 -0
  46. package/src/detectors/d14-dead-routine.ts +151 -0
  47. package/src/detectors/d16-obsolete-routine-call.ts +94 -0
  48. package/src/detectors/d17-min-version-drift.ts +157 -0
  49. package/src/detectors/d18-constant-filter-in-loop.ts +151 -0
  50. package/src/detectors/d19-unused-parameter.ts +116 -0
  51. package/src/detectors/d2-event-fanout-in-loop.ts +240 -0
  52. package/src/detectors/d20-unreachable-after-exit.ts +92 -0
  53. package/src/detectors/d21-read-without-load.ts +128 -0
  54. package/src/detectors/d22-flowfield-without-calcfields.ts +168 -0
  55. package/src/detectors/d29-subscriber-modify-on-event-record.ts +163 -0
  56. package/src/detectors/d3-load-state.ts +72 -0
  57. package/src/detectors/d3-missing-setloadfields.ts +234 -0
  58. package/src/detectors/d32-constant-boolean-parameter.ts +185 -0
  59. package/src/detectors/d33-unfiltered-bulk-write.ts +173 -0
  60. package/src/detectors/d34-commit-in-loop.ts +206 -0
  61. package/src/detectors/d35-commit-in-event-subscriber.ts +138 -0
  62. package/src/detectors/d36-late-setloadfields.ts +162 -0
  63. package/src/detectors/d37-validate-without-persist.ts +271 -0
  64. package/src/detectors/d38-subscriber-to-obsolete-event.ts +140 -0
  65. package/src/detectors/d39-record-left-dirty-across-chain.ts +165 -0
  66. package/src/detectors/d4-repeated-lookup-in-loop.ts +128 -0
  67. package/src/detectors/d40-transitive-load-missing.ts +217 -0
  68. package/src/detectors/d41-transitive-filter-loss.ts +200 -0
  69. package/src/detectors/d42-cross-call-wrong-setloadfields.ts +243 -0
  70. package/src/detectors/d43-event-ishandled-skip.ts +257 -0
  71. package/src/detectors/d44-event-multi-subscriber-overlap.ts +223 -0
  72. package/src/detectors/d45-event-transitive-table-exposure.ts +159 -0
  73. package/src/detectors/d5-set-based-opportunity.ts +162 -0
  74. package/src/detectors/d7-recursive-event-expansion.ts +151 -0
  75. package/src/detectors/d8-commit-in-transaction.ts +132 -0
  76. package/src/detectors/d9-transaction-span-summary.ts +107 -0
  77. package/src/detectors/detector-context.ts +121 -0
  78. package/src/detectors/finding-grouping.ts +61 -0
  79. package/src/detectors/path-merge.ts +174 -0
  80. package/src/detectors/registry.ts +176 -0
  81. package/src/detectors/table-display.ts +42 -0
  82. package/src/diff/diff-abi.ts +195 -0
  83. package/src/diff/diff-capabilities.ts +179 -0
  84. package/src/diff/diff-engine.ts +146 -0
  85. package/src/diff/diff-events.ts +323 -0
  86. package/src/diff/diff-identity.ts +73 -0
  87. package/src/diff/diff-indexes.ts +199 -0
  88. package/src/diff/diff-permissions.ts +260 -0
  89. package/src/diff/diff-policy.ts +101 -0
  90. package/src/diff/diff-preflight.ts +66 -0
  91. package/src/diff/diff-renames.ts +104 -0
  92. package/src/diff/diff-schema.ts +232 -0
  93. package/src/diff/format-diff.ts +148 -0
  94. package/src/engine/attribute-parser.ts +50 -0
  95. package/src/engine/capability-cone.ts +531 -0
  96. package/src/engine/combined-graph.ts +357 -0
  97. package/src/engine/control-flow-walker.ts +1317 -0
  98. package/src/engine/dispatch-sites.ts +199 -0
  99. package/src/engine/effect-lattice.ts +81 -0
  100. package/src/engine/entry-points.ts +57 -0
  101. package/src/engine/event-flow.ts +524 -0
  102. package/src/engine/event-relay.ts +92 -0
  103. package/src/engine/op-classification.ts +92 -0
  104. package/src/engine/path-walker.ts +189 -0
  105. package/src/engine/reverse-call-graph.ts +23 -0
  106. package/src/engine/root-classifier-overlay.ts +194 -0
  107. package/src/engine/root-classifier.ts +135 -0
  108. package/src/engine/scc.ts +110 -0
  109. package/src/engine/source-anchor.ts +25 -0
  110. package/src/engine/summary-context.ts +104 -0
  111. package/src/engine/summary-engine.ts +296 -0
  112. package/src/engine/summary-runner.ts +560 -0
  113. package/src/engine/transaction-spans.ts +112 -0
  114. package/src/engine/uncertainty-util.ts +54 -0
  115. package/src/hash.ts +31 -0
  116. package/src/index/attribute-from-node.ts +141 -0
  117. package/src/index/callee-from-node.ts +181 -0
  118. package/src/index/capability/background.ts +90 -0
  119. package/src/index/capability/commit.ts +44 -0
  120. package/src/index/capability/dispatch.ts +164 -0
  121. package/src/index/capability/events.ts +65 -0
  122. package/src/index/capability/extractor.ts +124 -0
  123. package/src/index/capability/file-blob.ts +137 -0
  124. package/src/index/capability/http.ts +159 -0
  125. package/src/index/capability/hyperlink.ts +60 -0
  126. package/src/index/capability/isolated-storage.ts +179 -0
  127. package/src/index/capability/table.ts +113 -0
  128. package/src/index/capability/telemetry.ts +84 -0
  129. package/src/index/capability/ui.ts +55 -0
  130. package/src/index/capability/value-source.ts +202 -0
  131. package/src/index/expression-from-node.ts +117 -0
  132. package/src/index/indexer.ts +102 -0
  133. package/src/index/intraprocedural-body.ts +1467 -0
  134. package/src/index/intraprocedural-ops.ts +253 -0
  135. package/src/index/intraprocedural-refs.ts +188 -0
  136. package/src/index/object-indexer.ts +279 -0
  137. package/src/index/routine-indexer.ts +282 -0
  138. package/src/index/routine-signature.ts +46 -0
  139. package/src/index/variable-indexer.ts +134 -0
  140. package/src/index/variable-initializer-extractor.ts +155 -0
  141. package/src/index/variable-type-normalizer.ts +83 -0
  142. package/src/index.ts +267 -0
  143. package/src/mcp/server.ts +72 -0
  144. package/src/mcp/session.ts +49 -0
  145. package/src/mcp/tools/explain-path.ts +75 -0
  146. package/src/mcp/tools/get-analysis-health.ts +62 -0
  147. package/src/mcp/tools/get-finding.ts +47 -0
  148. package/src/mcp/tools/get-routine-summary.ts +126 -0
  149. package/src/mcp/tools/list-findings.ts +85 -0
  150. package/src/mcp/tools/list-hotspots.ts +78 -0
  151. package/src/mcp/tools/list-rollups.ts +103 -0
  152. package/src/mcp/tools/validators.ts +25 -0
  153. package/src/model/attributes.ts +120 -0
  154. package/src/model/callee.ts +45 -0
  155. package/src/model/capability.ts +187 -0
  156. package/src/model/coverage.ts +85 -0
  157. package/src/model/entities.ts +628 -0
  158. package/src/model/expression.ts +98 -0
  159. package/src/model/finding.ts +110 -0
  160. package/src/model/graph-edge.ts +93 -0
  161. package/src/model/graph.ts +62 -0
  162. package/src/model/identity.ts +81 -0
  163. package/src/model/ids.ts +90 -0
  164. package/src/model/index.ts +13 -0
  165. package/src/model/model.ts +51 -0
  166. package/src/model/permission.ts +76 -0
  167. package/src/model/root-classification.ts +116 -0
  168. package/src/model/stable-identity.ts +102 -0
  169. package/src/model/summary.ts +96 -0
  170. package/src/parser/ast.ts +82 -0
  171. package/src/parser/native/ffi.ts +145 -0
  172. package/src/parser/native/parse-index-pool.ts +148 -0
  173. package/src/parser/native/parse-index-worker.ts +94 -0
  174. package/src/parser/native/wrapper.ts +353 -0
  175. package/src/parser/parser-init.ts +43 -0
  176. package/src/perf/profiler.ts +66 -0
  177. package/src/policy/policy-default.yaml +83 -0
  178. package/src/policy/policy-engine.ts +339 -0
  179. package/src/policy/policy-loader.ts +257 -0
  180. package/src/policy/policy-schema.json +379 -0
  181. package/src/policy/policy-types.ts +81 -0
  182. package/src/policy/predicate-compiler.ts +151 -0
  183. package/src/policy/predicate-evaluator.ts +267 -0
  184. package/src/policy/predicate-fields.ts +439 -0
  185. package/src/projection/actionable-anchor.ts +48 -0
  186. package/src/projection/finding-filters.ts +44 -0
  187. package/src/projection/finding-fingerprint.ts +54 -0
  188. package/src/projection/finding-groups.ts +41 -0
  189. package/src/projection/finding-summary.ts +110 -0
  190. package/src/projection/rollup-findings.ts +105 -0
  191. package/src/providers/discover.ts +88 -0
  192. package/src/providers/external.ts +46 -0
  193. package/src/providers/types.ts +36 -0
  194. package/src/providers/workspace.ts +117 -0
  195. package/src/resolve/call-resolver.ts +117 -0
  196. package/src/resolve/coverage.ts +61 -0
  197. package/src/resolve/event-graph.ts +166 -0
  198. package/src/resolve/implicit-edges.ts +53 -0
  199. package/src/resolve/record-types.ts +36 -0
  200. package/src/resolve/resolver.ts +23 -0
  201. package/src/resolve/semantic-graph.ts +29 -0
  202. package/src/resolve/symbol-table.ts +69 -0
  203. package/src/snapshot/app-snapshot.ts +74 -0
  204. package/src/snapshot/compose.ts +100 -0
  205. package/src/snapshot/derive/callsite-evidence.ts +76 -0
  206. package/src/snapshot/derive/capability-facts.ts +70 -0
  207. package/src/snapshot/derive/contracts.ts +131 -0
  208. package/src/snapshot/derive/coverage.ts +35 -0
  209. package/src/snapshot/derive/event-declarations.ts +140 -0
  210. package/src/snapshot/derive/identity-table.ts +58 -0
  211. package/src/snapshot/derive/inputs.ts +91 -0
  212. package/src/snapshot/derive/operation-evidence.ts +70 -0
  213. package/src/snapshot/derive/permissions.ts +186 -0
  214. package/src/snapshot/derive/root-classifications.ts +56 -0
  215. package/src/snapshot/derive/schema.ts +130 -0
  216. package/src/snapshot/derive/typed-edges.ts +60 -0
  217. package/src/snapshot/derive/workspace-fingerprint.ts +19 -0
  218. package/src/snapshot/deserialize.ts +40 -0
  219. package/src/snapshot/serialize-cbor-gz.ts +12 -0
  220. package/src/snapshot/serialize-cbor.ts +19 -0
  221. package/src/snapshot/serialize-json.ts +22 -0
  222. package/src/snapshot/shard.ts +134 -0
  223. package/src/snapshot/types.ts +181 -0
  224. package/src/symbols/app-manifest.ts +96 -0
  225. package/src/symbols/app-package-zip.ts +50 -0
  226. package/src/symbols/embedded-source-reader.ts +41 -0
  227. package/src/symbols/package-hash.ts +81 -0
  228. package/src/symbols/symbol-reader.ts +101 -0
  229. package/src/symbols/symbol-reference-parser.ts +378 -0
  230. package/src/symbols/symbol-reference-reader.ts +27 -0
  231. package/tsconfig.json +18 -0
@@ -0,0 +1,379 @@
1
+ {
2
+ "$schema": "http://json-schema.org/draft-07/schema#",
3
+ "title": "al-sem policy",
4
+ "type": "object",
5
+ "required": ["version", "rules"],
6
+ "properties": {
7
+ "version": {
8
+ "const": 1
9
+ },
10
+ "description": {
11
+ "type": "string"
12
+ },
13
+ "defaults": {
14
+ "type": "object",
15
+ "properties": {
16
+ "onUnknown": {
17
+ "type": "string",
18
+ "enum": ["fail-open", "fail-closed"]
19
+ },
20
+ "requireCoverage": {
21
+ "type": "string",
22
+ "enum": ["complete", "partial", "any"]
23
+ }
24
+ },
25
+ "additionalProperties": false
26
+ },
27
+ "rules": {
28
+ "type": "array",
29
+ "items": {
30
+ "$ref": "#/definitions/Rule"
31
+ }
32
+ }
33
+ },
34
+ "additionalProperties": false,
35
+ "definitions": {
36
+ "Rule": {
37
+ "type": "object",
38
+ "required": ["id", "severity", "when"],
39
+ "properties": {
40
+ "id": {
41
+ "type": "string",
42
+ "pattern": "^[a-z][a-z0-9-]{2,80}$"
43
+ },
44
+ "title": {
45
+ "type": "string"
46
+ },
47
+ "description": {
48
+ "type": "string"
49
+ },
50
+ "message": {
51
+ "type": "string"
52
+ },
53
+ "severity": {
54
+ "type": "string",
55
+ "enum": ["critical", "high", "medium", "low", "info"]
56
+ },
57
+ "when": {
58
+ "$ref": "#/definitions/Predicate"
59
+ },
60
+ "except": {
61
+ "$ref": "#/definitions/Predicate"
62
+ },
63
+ "requireCoverage": {
64
+ "type": "string",
65
+ "enum": ["complete", "partial", "any"]
66
+ },
67
+ "onUnknown": {
68
+ "type": "string",
69
+ "enum": ["fail-open", "fail-closed"]
70
+ },
71
+ "facts": {
72
+ "type": "string",
73
+ "enum": ["direct", "inherited", "any"]
74
+ }
75
+ },
76
+ "additionalProperties": false
77
+ },
78
+ "Predicate": {
79
+ "type": "object",
80
+ "properties": {
81
+ "root.kinds": {
82
+ "oneOf": [
83
+ {
84
+ "type": "string"
85
+ },
86
+ {
87
+ "type": "array",
88
+ "items": {
89
+ "type": "string"
90
+ }
91
+ }
92
+ ]
93
+ },
94
+ "routine.name": {
95
+ "oneOf": [
96
+ {
97
+ "type": "string"
98
+ },
99
+ {
100
+ "type": "array",
101
+ "items": {
102
+ "type": "string"
103
+ }
104
+ }
105
+ ]
106
+ },
107
+ "routine.kind": {
108
+ "type": "string",
109
+ "enum": ["procedure", "trigger", "event-publisher", "event-subscriber"]
110
+ },
111
+ "routine.accessModifier": {
112
+ "oneOf": [
113
+ {
114
+ "type": "string"
115
+ },
116
+ {
117
+ "type": "array",
118
+ "items": {
119
+ "type": "string"
120
+ }
121
+ }
122
+ ]
123
+ },
124
+ "object.name": {
125
+ "oneOf": [
126
+ {
127
+ "type": "string"
128
+ },
129
+ {
130
+ "type": "array",
131
+ "items": {
132
+ "type": "string"
133
+ }
134
+ }
135
+ ]
136
+ },
137
+ "object.kind": {
138
+ "oneOf": [
139
+ {
140
+ "type": "string"
141
+ },
142
+ {
143
+ "type": "array",
144
+ "items": {
145
+ "type": "string"
146
+ }
147
+ }
148
+ ]
149
+ },
150
+ "object.appGuid": {
151
+ "type": "string"
152
+ },
153
+ "capability.op": {
154
+ "oneOf": [
155
+ {
156
+ "type": "string",
157
+ "enum": [
158
+ "read",
159
+ "insert",
160
+ "modify",
161
+ "delete",
162
+ "execute",
163
+ "publish",
164
+ "subscribe",
165
+ "send",
166
+ "log",
167
+ "store-read",
168
+ "store-write",
169
+ "store-delete",
170
+ "commit",
171
+ "open",
172
+ "write-blob",
173
+ "start",
174
+ "ui-confirm",
175
+ "ui-message",
176
+ "ui-error"
177
+ ]
178
+ },
179
+ {
180
+ "type": "array",
181
+ "items": {
182
+ "type": "string",
183
+ "enum": [
184
+ "read",
185
+ "insert",
186
+ "modify",
187
+ "delete",
188
+ "execute",
189
+ "publish",
190
+ "subscribe",
191
+ "send",
192
+ "log",
193
+ "store-read",
194
+ "store-write",
195
+ "store-delete",
196
+ "commit",
197
+ "open",
198
+ "write-blob",
199
+ "start",
200
+ "ui-confirm",
201
+ "ui-message",
202
+ "ui-error"
203
+ ]
204
+ }
205
+ }
206
+ ]
207
+ },
208
+ "capability.resourceKind": {
209
+ "oneOf": [
210
+ {
211
+ "type": "string",
212
+ "enum": [
213
+ "table",
214
+ "event",
215
+ "codeunit",
216
+ "page",
217
+ "report",
218
+ "http",
219
+ "telemetry",
220
+ "isolated-storage",
221
+ "file",
222
+ "transaction",
223
+ "ui",
224
+ "background"
225
+ ]
226
+ },
227
+ {
228
+ "type": "array",
229
+ "items": {
230
+ "type": "string",
231
+ "enum": [
232
+ "table",
233
+ "event",
234
+ "codeunit",
235
+ "page",
236
+ "report",
237
+ "http",
238
+ "telemetry",
239
+ "isolated-storage",
240
+ "file",
241
+ "transaction",
242
+ "ui",
243
+ "background"
244
+ ]
245
+ }
246
+ }
247
+ ]
248
+ },
249
+ "capability.resource.table.name": {
250
+ "oneOf": [
251
+ {
252
+ "type": "string"
253
+ },
254
+ {
255
+ "type": "array",
256
+ "items": {
257
+ "type": "string"
258
+ }
259
+ }
260
+ ]
261
+ },
262
+ "capability.resource.event.name": {
263
+ "oneOf": [
264
+ {
265
+ "type": "string"
266
+ },
267
+ {
268
+ "type": "array",
269
+ "items": {
270
+ "type": "string"
271
+ }
272
+ }
273
+ ]
274
+ },
275
+ "capability.resource.http.method": {
276
+ "oneOf": [
277
+ {
278
+ "type": "string"
279
+ },
280
+ {
281
+ "type": "array",
282
+ "items": {
283
+ "type": "string"
284
+ }
285
+ }
286
+ ]
287
+ },
288
+ "capability.resource.ui.kind": {
289
+ "oneOf": [
290
+ {
291
+ "type": "string"
292
+ },
293
+ {
294
+ "type": "array",
295
+ "items": {
296
+ "type": "string"
297
+ }
298
+ }
299
+ ]
300
+ },
301
+ "capability.confidence": {
302
+ "oneOf": [
303
+ {
304
+ "type": "string",
305
+ "enum": ["static", "boundedDynamic", "configDynamic", "userDynamic", "unresolved"]
306
+ },
307
+ {
308
+ "type": "array",
309
+ "items": {
310
+ "type": "string",
311
+ "enum": ["static", "boundedDynamic", "configDynamic", "userDynamic", "unresolved"]
312
+ }
313
+ }
314
+ ]
315
+ },
316
+ "capability.origin": {
317
+ "oneOf": [
318
+ {
319
+ "type": "string",
320
+ "enum": ["direct", "inherited"]
321
+ },
322
+ {
323
+ "type": "array",
324
+ "items": {
325
+ "type": "string",
326
+ "enum": ["direct", "inherited"]
327
+ }
328
+ }
329
+ ]
330
+ },
331
+ "capability.via": {
332
+ "oneOf": [
333
+ {
334
+ "type": "string",
335
+ "enum": [
336
+ "self",
337
+ "call",
338
+ "object-run",
339
+ "event-dispatch",
340
+ "implicit-trigger",
341
+ "dependency"
342
+ ]
343
+ },
344
+ {
345
+ "type": "array",
346
+ "items": {
347
+ "type": "string",
348
+ "enum": [
349
+ "self",
350
+ "call",
351
+ "object-run",
352
+ "event-dispatch",
353
+ "implicit-trigger",
354
+ "dependency"
355
+ ]
356
+ }
357
+ }
358
+ ]
359
+ },
360
+ "all": {
361
+ "type": "array",
362
+ "items": {
363
+ "$ref": "#/definitions/Predicate"
364
+ }
365
+ },
366
+ "any": {
367
+ "type": "array",
368
+ "items": {
369
+ "$ref": "#/definitions/Predicate"
370
+ }
371
+ },
372
+ "not": {
373
+ "$ref": "#/definitions/Predicate"
374
+ }
375
+ },
376
+ "additionalProperties": false
377
+ }
378
+ }
379
+ }
@@ -0,0 +1,81 @@
1
+ import type { Diagnostic, Finding } from "../model/finding.ts";
2
+
3
+ export type PolicySeverity = "critical" | "high" | "medium" | "low" | "info";
4
+ export type PolicyVerdict = "pass" | "fail" | "unknown" | "not-applicable";
5
+ export type Tristate = "true" | "false" | "unknown";
6
+ export type CoveragePolicy = "complete" | "partial" | "any";
7
+ export type UnknownPolicy = "fail-open" | "fail-closed";
8
+ export type FactOriginFilter = "direct" | "inherited" | "any";
9
+
10
+ export type PredicateOperator = "==" | "in" | "glob" | "glob-in";
11
+
12
+ export type Predicate =
13
+ | { kind: "field"; field: string; operator: PredicateOperator; value: unknown }
14
+ | { kind: "all"; children: readonly Predicate[] }
15
+ | { kind: "any"; children: readonly Predicate[] }
16
+ | { kind: "not"; child: Predicate };
17
+
18
+ export interface Rule {
19
+ id: string;
20
+ title?: string;
21
+ description?: string;
22
+ message?: string;
23
+ severity: PolicySeverity;
24
+ when: Predicate;
25
+ except?: Predicate;
26
+ requireCoverage?: CoveragePolicy;
27
+ onUnknown?: UnknownPolicy;
28
+ facts?: FactOriginFilter;
29
+ }
30
+
31
+ export interface PolicyDoc {
32
+ version: 1;
33
+ description?: string;
34
+ defaults?: {
35
+ onUnknown?: UnknownPolicy;
36
+ requireCoverage?: CoveragePolicy;
37
+ };
38
+ rules: readonly Rule[];
39
+ }
40
+
41
+ export interface PredicateTrace {
42
+ node: "field" | "all" | "any" | "not";
43
+ result: Tristate;
44
+ field?: string;
45
+ expected?: unknown;
46
+ actual?: unknown;
47
+ unknownReason?:
48
+ | "field-not-applicable"
49
+ | "resource-id-unresolved"
50
+ | "no-root-classification"
51
+ | "coverage-below-bar"
52
+ | "evaluator-error";
53
+ children?: readonly PredicateTrace[];
54
+ }
55
+
56
+ /** Per-rule run counters. NOTE: routines a rule is structurally not applicable to
57
+ * (applicability "false") or structurally exempt from (except "true") are counted in
58
+ * `routinesEvaluated` but in NONE of the sub-buckets — so
59
+ * `routinesEvaluated >= matched + skippedCoverage + skippedUnknown + passed`, not `==`. */
60
+ export interface RuleRunSummary {
61
+ ruleId: string;
62
+ routinesEvaluated: number;
63
+ routinesMatched: number;
64
+ routinesSkippedCoverage: number;
65
+ routinesSkippedUnknown: number;
66
+ routinesPassed: number;
67
+ findingsEmitted: number;
68
+ /** Traces captured for sample findings or via policy explain --routine. */
69
+ traces?: readonly { routineId: string; trace: PredicateTrace }[];
70
+ /** Per-rule errors (compilation or evaluation). */
71
+ errors?: readonly string[];
72
+ }
73
+
74
+ export interface PolicyRunResult {
75
+ /** "default" | "auto:<absolute-path>" | "explicit:<absolute-path>" | "inline" | "disabled" */
76
+ policySource: string;
77
+ policyVersion: number;
78
+ ruleSummaries: readonly RuleRunSummary[];
79
+ findings: readonly Finding[];
80
+ diagnostics: readonly Diagnostic[];
81
+ }
@@ -0,0 +1,151 @@
1
+ import type { Predicate, PredicateOperator } from "./policy-types.ts";
2
+ import { type FieldScope, type PredicateFieldDef, getFieldDef } from "./predicate-fields.ts";
3
+
4
+ export type CompileResult = { ok: true; value: Predicate } | { ok: false; error: string };
5
+
6
+ export function compilePredicate(raw: unknown, path = ""): CompileResult {
7
+ if (raw === null || typeof raw !== "object" || Array.isArray(raw)) {
8
+ return { ok: false, error: `${path || "<root>"}: expected an object` };
9
+ }
10
+ const obj = raw as Record<string, unknown>;
11
+ const keys = Object.keys(obj);
12
+
13
+ // Single-key boolean wrappers
14
+ if (keys.length === 1) {
15
+ const k = keys[0] as string;
16
+ if (k === "all" || k === "any") {
17
+ const items = obj[k];
18
+ if (!Array.isArray(items)) return { ok: false, error: `${path}.${k}: expected array` };
19
+ if (items.length === 0) return { ok: false, error: `${path}.${k}: empty array not allowed` };
20
+ const children: Predicate[] = [];
21
+ for (let i = 0; i < items.length; i++) {
22
+ const child = compilePredicate(items[i], `${path}.${k}[${i}]`);
23
+ if (!child.ok) return child;
24
+ children.push(child.value);
25
+ }
26
+ if (k === "any") {
27
+ const scopes = collectScopes(children);
28
+ if (scopes.size > 1) {
29
+ return {
30
+ ok: false,
31
+ error: `${path}.any: mixed ${[...scopes].join("/")} scope predicates — move routine filters into a parent all, or split into separate rules`,
32
+ };
33
+ }
34
+ }
35
+ return { ok: true, value: { kind: k, children } };
36
+ }
37
+ if (k === "not") {
38
+ const inner = obj.not;
39
+ if (
40
+ inner === null ||
41
+ typeof inner !== "object" ||
42
+ Array.isArray(inner) ||
43
+ Object.keys(inner as object).length === 0
44
+ ) {
45
+ return { ok: false, error: `${path}.not: empty or missing predicate` };
46
+ }
47
+ const child = compilePredicate(inner, `${path}.not`);
48
+ if (!child.ok) return child;
49
+ const scopes = collectScopes([child.value]);
50
+ // Disallow `not` over a routine-only predicate (use `except` for routine carve-outs).
51
+ if (scopes.size === 1 && scopes.has("routine")) {
52
+ return {
53
+ ok: false,
54
+ error: `${path}.not wraps a routine-scope predicate — use except: for routine-scope carve-outs`,
55
+ };
56
+ }
57
+ return { ok: true, value: { kind: "not", child: child.value } };
58
+ }
59
+ }
60
+
61
+ // Multi-key map (or single-key field) → compile each key; wrap in implicit all if >1
62
+ const children: Predicate[] = [];
63
+ for (const k of keys) {
64
+ if (k === "all" || k === "any" || k === "not") {
65
+ const sub = compilePredicate({ [k]: obj[k] }, path);
66
+ if (!sub.ok) return sub;
67
+ children.push(sub.value);
68
+ continue;
69
+ }
70
+ // Field predicate
71
+ const def = getFieldDef(k);
72
+ if (def === undefined) {
73
+ return { ok: false, error: `${path}.${k}: unknown predicate field` };
74
+ }
75
+ const fieldP = compileFieldPredicate(def, obj[k], `${path}.${k}`);
76
+ if (!fieldP.ok) return fieldP;
77
+ children.push(fieldP.value);
78
+ }
79
+ if (children.length === 0) {
80
+ return { ok: false, error: `${path || "<root>"}: empty predicate` };
81
+ }
82
+ if (children.length === 1) {
83
+ const first = children[0];
84
+ // biome-ignore lint/style/noNonNullAssertion: length === 1 guarantees index 0 exists
85
+ return { ok: true, value: first! };
86
+ }
87
+ return { ok: true, value: { kind: "all", children } };
88
+ }
89
+
90
+ function compileFieldPredicate(def: PredicateFieldDef, raw: unknown, path: string): CompileResult {
91
+ const isList = Array.isArray(raw);
92
+ const list = isList ? (raw as unknown[]) : [raw];
93
+
94
+ if (def.valueShape === "enum" || def.valueShape === "enum-list") {
95
+ if (def.enumValues !== undefined) {
96
+ for (const v of list) {
97
+ if (typeof v !== "string" || !def.enumValues.includes(v)) {
98
+ return {
99
+ ok: false,
100
+ error: `${path}: invalid enum value '${String(v)}' (allowed: ${def.enumValues.join(", ")})`,
101
+ };
102
+ }
103
+ }
104
+ }
105
+ if (def.valueShape === "enum" && isList && list.length > 1) {
106
+ return { ok: false, error: `${path}: expected single value, got list` };
107
+ }
108
+ const op: PredicateOperator = "in";
109
+ return { ok: true, value: { kind: "field", field: def.name, operator: op, value: list } };
110
+ }
111
+
112
+ if (def.valueShape === "glob" || def.valueShape === "glob-list") {
113
+ for (const v of list) {
114
+ if (typeof v !== "string")
115
+ return { ok: false, error: `${path}: glob value must be a string` };
116
+ }
117
+ const op: PredicateOperator = isList ? "glob-in" : "glob";
118
+ const value = isList ? list : list[0];
119
+ // biome-ignore lint/style/noNonNullAssertion: list is [raw] when !isList, so index 0 always exists
120
+ return { ok: true, value: { kind: "field", field: def.name, operator: op, value: value! } };
121
+ }
122
+
123
+ if (def.valueShape === "string-exact" || def.valueShape === "string-list") {
124
+ for (const v of list) {
125
+ if (typeof v !== "string") return { ok: false, error: `${path}: value must be a string` };
126
+ }
127
+ const op: PredicateOperator = isList ? "in" : "==";
128
+ const value = isList ? list : list[0];
129
+ // biome-ignore lint/style/noNonNullAssertion: list is [raw] when !isList, so index 0 always exists
130
+ return { ok: true, value: { kind: "field", field: def.name, operator: op, value: value! } };
131
+ }
132
+
133
+ return { ok: false, error: `${path}: unsupported value shape '${def.valueShape}'` };
134
+ }
135
+
136
+ function collectScopes(preds: readonly Predicate[]): Set<FieldScope> {
137
+ const out = new Set<FieldScope>();
138
+ for (const p of preds) collectScopesOne(p, out);
139
+ return out;
140
+ }
141
+
142
+ function collectScopesOne(p: Predicate, out: Set<FieldScope>): void {
143
+ if (p.kind === "field") {
144
+ const def = getFieldDef(p.field);
145
+ if (def !== undefined) out.add(def.scope);
146
+ } else if (p.kind === "not") {
147
+ collectScopesOne(p.child, out);
148
+ } else {
149
+ for (const c of p.children) collectScopesOne(c, out);
150
+ }
151
+ }