@seanhogg/builderforce-memory 2026.10.71 → 2026.10.73

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 (204) hide show
  1. package/dist/knowledge/coChange.d.ts +3 -1
  2. package/dist/knowledge/coChange.d.ts.map +1 -1
  3. package/dist/knowledge/coChange.js +24 -12
  4. package/dist/knowledge/coChange.js.map +1 -1
  5. package/dist/knowledge/extractFile.d.ts +10 -0
  6. package/dist/knowledge/extractFile.d.ts.map +1 -1
  7. package/dist/knowledge/extractFile.js +16 -5
  8. package/dist/knowledge/extractFile.js.map +1 -1
  9. package/dist/knowledge/extractJob.d.ts +18 -1
  10. package/dist/knowledge/extractJob.d.ts.map +1 -1
  11. package/dist/knowledge/extractJob.js +10 -4
  12. package/dist/knowledge/extractJob.js.map +1 -1
  13. package/dist/knowledge/facts/extractorVersion.d.ts +45 -0
  14. package/dist/knowledge/facts/extractorVersion.d.ts.map +1 -0
  15. package/dist/knowledge/facts/extractorVersion.js +56 -0
  16. package/dist/knowledge/facts/extractorVersion.js.map +1 -0
  17. package/dist/knowledge/facts/fileFacts.d.ts +166 -0
  18. package/dist/knowledge/facts/fileFacts.d.ts.map +1 -0
  19. package/dist/knowledge/facts/fileFacts.js +156 -0
  20. package/dist/knowledge/facts/fileFacts.js.map +1 -0
  21. package/dist/knowledge/findings.d.ts +17 -14
  22. package/dist/knowledge/findings.d.ts.map +1 -1
  23. package/dist/knowledge/findings.js +29 -129
  24. package/dist/knowledge/findings.js.map +1 -1
  25. package/dist/knowledge/index.d.ts +14 -2
  26. package/dist/knowledge/index.d.ts.map +1 -1
  27. package/dist/knowledge/index.js +8 -1
  28. package/dist/knowledge/index.js.map +1 -1
  29. package/dist/knowledge/model.d.ts +23 -0
  30. package/dist/knowledge/model.d.ts.map +1 -1
  31. package/dist/knowledge/owedFiles.d.ts +14 -4
  32. package/dist/knowledge/owedFiles.d.ts.map +1 -1
  33. package/dist/knowledge/owedFiles.js +30 -8
  34. package/dist/knowledge/owedFiles.js.map +1 -1
  35. package/dist/knowledge/pack/sections/index.d.ts +4 -1
  36. package/dist/knowledge/pack/sections/index.d.ts.map +1 -1
  37. package/dist/knowledge/pack/sections/index.js +7 -3
  38. package/dist/knowledge/pack/sections/index.js.map +1 -1
  39. package/dist/knowledge/pack/sections/rows.d.ts +9 -0
  40. package/dist/knowledge/pack/sections/rows.d.ts.map +1 -1
  41. package/dist/knowledge/pack/sections/rows.js +46 -1
  42. package/dist/knowledge/pack/sections/rows.js.map +1 -1
  43. package/dist/knowledge/plan.d.ts +10 -1
  44. package/dist/knowledge/plan.d.ts.map +1 -1
  45. package/dist/knowledge/plan.js +9 -4
  46. package/dist/knowledge/plan.js.map +1 -1
  47. package/dist/knowledge/review.js +1 -0
  48. package/dist/knowledge/review.js.map +1 -1
  49. package/dist/knowledge/reviewContext.d.ts.map +1 -1
  50. package/dist/knowledge/reviewContext.js +5 -2
  51. package/dist/knowledge/reviewContext.js.map +1 -1
  52. package/dist/knowledge/reviewFinish.d.ts.map +1 -1
  53. package/dist/knowledge/reviewFinish.js +7 -2
  54. package/dist/knowledge/reviewFinish.js.map +1 -1
  55. package/dist/knowledge/reviewModel.d.ts +18 -1
  56. package/dist/knowledge/reviewModel.d.ts.map +1 -1
  57. package/dist/knowledge/reviewPrefetch.d.ts +2 -0
  58. package/dist/knowledge/reviewPrefetch.d.ts.map +1 -1
  59. package/dist/knowledge/reviewPrefetch.js +1 -0
  60. package/dist/knowledge/reviewPrefetch.js.map +1 -1
  61. package/dist/knowledge/rules/cloneGroup.d.ts +10 -0
  62. package/dist/knowledge/rules/cloneGroup.d.ts.map +1 -0
  63. package/dist/knowledge/rules/cloneGroup.js +46 -0
  64. package/dist/knowledge/rules/cloneGroup.js.map +1 -0
  65. package/dist/knowledge/rules/components.d.ts +8 -0
  66. package/dist/knowledge/rules/components.d.ts.map +1 -0
  67. package/dist/knowledge/rules/components.js +34 -0
  68. package/dist/knowledge/rules/components.js.map +1 -0
  69. package/dist/knowledge/rules/cycle.d.ts +7 -0
  70. package/dist/knowledge/rules/cycle.d.ts.map +1 -0
  71. package/dist/knowledge/rules/cycle.js +18 -0
  72. package/dist/knowledge/rules/cycle.js.map +1 -0
  73. package/dist/knowledge/rules/duplicateCopy.d.ts +7 -0
  74. package/dist/knowledge/rules/duplicateCopy.d.ts.map +1 -0
  75. package/dist/knowledge/rules/duplicateCopy.js +18 -0
  76. package/dist/knowledge/rules/duplicateCopy.js.map +1 -0
  77. package/dist/knowledge/rules/evaluateRules.d.ts +60 -0
  78. package/dist/knowledge/rules/evaluateRules.d.ts.map +1 -0
  79. package/dist/knowledge/rules/evaluateRules.js +164 -0
  80. package/dist/knowledge/rules/evaluateRules.js.map +1 -0
  81. package/dist/knowledge/rules/factGraph.d.ts +51 -0
  82. package/dist/knowledge/rules/factGraph.d.ts.map +1 -0
  83. package/dist/knowledge/rules/factGraph.js +77 -0
  84. package/dist/knowledge/rules/factGraph.js.map +1 -0
  85. package/dist/knowledge/rules/factRule.d.ts +56 -0
  86. package/dist/knowledge/rules/factRule.d.ts.map +1 -0
  87. package/dist/knowledge/rules/factRule.js +46 -0
  88. package/dist/knowledge/rules/factRule.js.map +1 -0
  89. package/dist/knowledge/rules/godModule.d.ts +8 -0
  90. package/dist/knowledge/rules/godModule.d.ts.map +1 -0
  91. package/dist/knowledge/rules/godModule.js +16 -0
  92. package/dist/knowledge/rules/godModule.js.map +1 -0
  93. package/dist/knowledge/rules/knowledgeInput.d.ts +35 -0
  94. package/dist/knowledge/rules/knowledgeInput.d.ts.map +1 -0
  95. package/dist/knowledge/rules/knowledgeInput.js +33 -0
  96. package/dist/knowledge/rules/knowledgeInput.js.map +1 -0
  97. package/dist/knowledge/rules/nearDuplicate.d.ts +7 -0
  98. package/dist/knowledge/rules/nearDuplicate.d.ts.map +1 -0
  99. package/dist/knowledge/rules/nearDuplicate.js +20 -0
  100. package/dist/knowledge/rules/nearDuplicate.js.map +1 -0
  101. package/dist/knowledge/rules/partitionViolation.d.ts +10 -0
  102. package/dist/knowledge/rules/partitionViolation.d.ts.map +1 -0
  103. package/dist/knowledge/rules/partitionViolation.js +27 -0
  104. package/dist/knowledge/rules/partitionViolation.js.map +1 -0
  105. package/dist/knowledge/rules/possibleSame.d.ts +7 -0
  106. package/dist/knowledge/rules/possibleSame.d.ts.map +1 -0
  107. package/dist/knowledge/rules/possibleSame.js +15 -0
  108. package/dist/knowledge/rules/possibleSame.js.map +1 -0
  109. package/dist/knowledge/rules/stale.d.ts +7 -0
  110. package/dist/knowledge/rules/stale.d.ts.map +1 -0
  111. package/dist/knowledge/rules/stale.js +28 -0
  112. package/dist/knowledge/rules/stale.js.map +1 -0
  113. package/dist/knowledge/rules/unpartitioned.d.ts +7 -0
  114. package/dist/knowledge/rules/unpartitioned.d.ts.map +1 -0
  115. package/dist/knowledge/rules/unpartitioned.js +26 -0
  116. package/dist/knowledge/rules/unpartitioned.js.map +1 -0
  117. package/dist/knowledge/sources.d.ts +13 -0
  118. package/dist/knowledge/sources.d.ts.map +1 -1
  119. package/dist/knowledge/sources.js +14 -0
  120. package/dist/knowledge/sources.js.map +1 -1
  121. package/dist/knowledge/stores/localSnapshotStore.d.ts.map +1 -1
  122. package/dist/knowledge/stores/localSnapshotStore.js +5 -0
  123. package/dist/knowledge/stores/localSnapshotStore.js.map +1 -1
  124. package/dist/retrieval/knowledge-index/sha256.d.ts.map +1 -1
  125. package/dist/retrieval/knowledge-index/sha256.js +2 -2
  126. package/dist/retrieval/knowledge-index/sha256.js.map +1 -1
  127. package/dist/stats/distributions.d.ts +17 -1
  128. package/dist/stats/distributions.d.ts.map +1 -1
  129. package/dist/stats/distributions.js +26 -1
  130. package/dist/stats/distributions.js.map +1 -1
  131. package/dist/stats/fitting.d.ts.map +1 -1
  132. package/dist/stats/fitting.js +1 -6
  133. package/dist/stats/fitting.js.map +1 -1
  134. package/dist/stats/index.d.ts +10 -3
  135. package/dist/stats/index.d.ts.map +1 -1
  136. package/dist/stats/index.js +10 -3
  137. package/dist/stats/index.js.map +1 -1
  138. package/dist/stats/logistic.d.ts +30 -0
  139. package/dist/stats/logistic.d.ts.map +1 -0
  140. package/dist/stats/logistic.js +43 -0
  141. package/dist/stats/logistic.js.map +1 -0
  142. package/dist/stats/multivariate.d.ts +8 -13
  143. package/dist/stats/multivariate.d.ts.map +1 -1
  144. package/dist/stats/multivariate.js +7 -53
  145. package/dist/stats/multivariate.js.map +1 -1
  146. package/dist/stats/policies.d.ts.map +1 -1
  147. package/dist/stats/policies.js +4 -22
  148. package/dist/stats/policies.js.map +1 -1
  149. package/package.json +4 -3
  150. package/src/contract/exports.snapshot.json +37 -1
  151. package/src/contract/regression.extractorKey.test.ts +202 -0
  152. package/src/contract/regression.scopeMeasured.test.ts +121 -0
  153. package/src/contract/regression.statsEngineFree.test.ts +70 -0
  154. package/src/contract/regression.thompsonDeterminism.test.ts +23 -0
  155. package/src/contract/types.contract.test.ts +66 -2
  156. package/src/knowledge/coChange.test.ts +15 -2
  157. package/src/knowledge/coChange.ts +22 -10
  158. package/src/knowledge/extractFile.ts +27 -5
  159. package/src/knowledge/extractJob.ts +24 -5
  160. package/src/knowledge/facts/extractorVersion.ts +73 -0
  161. package/src/knowledge/facts/fileFacts.test.ts +157 -0
  162. package/src/knowledge/facts/fileFacts.ts +305 -0
  163. package/src/knowledge/findings.ts +30 -127
  164. package/src/knowledge/index.ts +20 -1
  165. package/src/knowledge/model.ts +24 -0
  166. package/src/knowledge/owedFiles.ts +36 -8
  167. package/src/knowledge/pack/pack.testkit.test-util.ts +4 -1
  168. package/src/knowledge/pack/sections/index.ts +7 -3
  169. package/src/knowledge/pack/sections/rows.ts +39 -1
  170. package/src/knowledge/plan.ts +16 -4
  171. package/src/knowledge/review.ts +1 -0
  172. package/src/knowledge/reviewContext.ts +5 -2
  173. package/src/knowledge/reviewFinish.ts +7 -2
  174. package/src/knowledge/reviewModel.ts +18 -1
  175. package/src/knowledge/reviewPrefetch.ts +3 -0
  176. package/src/knowledge/rules/cloneGroup.ts +45 -0
  177. package/src/knowledge/rules/components.ts +31 -0
  178. package/src/knowledge/rules/cycle.ts +20 -0
  179. package/src/knowledge/rules/duplicateCopy.ts +22 -0
  180. package/src/knowledge/rules/evaluateRules.test.ts +172 -0
  181. package/src/knowledge/rules/evaluateRules.ts +186 -0
  182. package/src/knowledge/rules/factGraph.oracle.test.ts +174 -0
  183. package/src/knowledge/rules/factGraph.ts +107 -0
  184. package/src/knowledge/rules/factRule.ts +74 -0
  185. package/src/knowledge/rules/godModule.ts +18 -0
  186. package/src/knowledge/rules/knowledgeInput.ts +60 -0
  187. package/src/knowledge/rules/knowledgeRules.test.ts +78 -0
  188. package/src/knowledge/rules/nearDuplicate.ts +22 -0
  189. package/src/knowledge/rules/partitionViolation.ts +31 -0
  190. package/src/knowledge/rules/possibleSame.ts +17 -0
  191. package/src/knowledge/rules/rules.testkit.test-util.ts +78 -0
  192. package/src/knowledge/rules/stale.ts +31 -0
  193. package/src/knowledge/rules/unpartitioned.ts +27 -0
  194. package/src/knowledge/sources.ts +16 -0
  195. package/src/knowledge/stores/localSnapshotStore.ts +4 -0
  196. package/src/retrieval/knowledge-index/sha256.test.ts +25 -0
  197. package/src/retrieval/knowledge-index/sha256.ts +2 -1
  198. package/src/stats/distributions.ts +29 -2
  199. package/src/stats/fitting.ts +1 -6
  200. package/src/stats/index.ts +12 -2
  201. package/src/stats/logistic.test.ts +89 -0
  202. package/src/stats/logistic.ts +49 -0
  203. package/src/stats/multivariate.ts +8 -59
  204. package/src/stats/policies.ts +4 -24
@@ -0,0 +1,60 @@
1
+ /**
2
+ * knowledge/rules/evaluateRules.ts — the incremental FactRule evaluator (PRD 41 §B1, §B5).
3
+ *
4
+ * Verifying traces with early cutoff (Mokhov, Mitchell, Peyton Jones, *Build Systems à la Carte*): every
5
+ * derived row stores the hash of the inputs it read — the rule's id and version, its focus, and for each
6
+ * file in its scope the hash of each fact kind it `reads`, plus the hash of each declared extra input. On
7
+ * the next evaluation a row whose input hash is unchanged is REUSED without calling `derive`; otherwise
8
+ * the rule reruns. No dirty flags: validity is recomputed from the current facts, so a thrown-away row
9
+ * store rebuilds to the same rows.
10
+ *
11
+ * The trace is ENFORCED, not trusted: `derive` receives each file projected to the kinds the rule reads
12
+ * (`projectFileFacts`, every other field empty) and `extra` cut to its declared `extraInputs`, so an undeclared
13
+ * read sees nothing instead of silently breaking early cutoff.
14
+ *
15
+ * Consequences the trace tests pin: a body-only edit reruns `body`-reading rules for that file and skips
16
+ * `structure`-only rules; a global rule is skipped when no file's read kinds changed anywhere.
17
+ */
18
+ import type { Finding } from '../findingModel.js';
19
+ import { type FileFacts } from '../facts/fileFacts.js';
20
+ import { type FactRule } from './factRule.js';
21
+ /** What the evaluator reads: the current facts by path, the import neighbourhood, and the hashed extra inputs. */
22
+ export interface RuleWorld<X = Record<never, never>> {
23
+ files: ReadonlyMap<string, FileFacts>;
24
+ /** Undirected one-hop neighbours of a path. Default: from the facts' resolved imports ({@link importNeighbours}). */
25
+ neighbours?: (path: string) => readonly string[];
26
+ extra?: X;
27
+ /** The hash of each extra input. A rule that declares an extra input with no hash here throws `rule_extra_input_unhashed`. */
28
+ extraHashes?: Readonly<Record<string, string>>;
29
+ }
30
+ /** One derived result and the hash of the inputs it read. `focus` is '' for a global rule. */
31
+ export interface DerivedRow {
32
+ rule: string;
33
+ version: number;
34
+ focus: string;
35
+ inputs: string;
36
+ findings: Finding[];
37
+ }
38
+ export type DerivedRows = ReadonlyMap<string, DerivedRow>;
39
+ /** What happened to one row in an evaluation. Sorted by rule, then focus. */
40
+ export interface RuleTraceEntry {
41
+ rule: string;
42
+ focus: string;
43
+ outcome: 'ran' | 'reused';
44
+ }
45
+ export interface RuleEvaluation {
46
+ rows: Map<string, DerivedRow>;
47
+ trace: RuleTraceEntry[];
48
+ /** Every row's findings, deduped by id, sorted by id. */
49
+ findings: Finding[];
50
+ }
51
+ /** The key of a derived row. */
52
+ export declare const derivedRowKey: (rule: string, focus: string) => string;
53
+ /** Undirected one-hop import neighbours from each file's resolved imports (only files present), each list sorted. */
54
+ export declare function importNeighbours(files: ReadonlyMap<string, FileFacts>): (path: string) => readonly string[];
55
+ /**
56
+ * Evaluates `rules` against `world`, reusing every row of `previous` whose input hash still holds. Rows for files
57
+ * that no longer exist (and for rules no longer registered) are dropped. Pure: `previous` is not mutated.
58
+ */
59
+ export declare function evaluateRules<X>(rules: readonly FactRule<X>[], world: RuleWorld<X>, previous?: DerivedRows): RuleEvaluation;
60
+ //# sourceMappingURL=evaluateRules.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"evaluateRules.d.ts","sourceRoot":"","sources":["../../../src/knowledge/rules/evaluateRules.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAIH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAuE,KAAK,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAC5H,OAAO,EAAqB,KAAK,QAAQ,EAAE,MAAM,eAAe,CAAC;AAEjE,kHAAkH;AAClH,MAAM,WAAW,SAAS,CAAC,CAAC,GAAG,MAAM,CAAC,KAAK,EAAE,KAAK,CAAC;IAC/C,KAAK,EAAE,WAAW,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;IACtC,qHAAqH;IACrH,UAAU,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,SAAS,MAAM,EAAE,CAAC;IACjD,KAAK,CAAC,EAAE,CAAC,CAAC;IACV,8HAA8H;IAC9H,WAAW,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;CAClD;AAED,8FAA8F;AAC9F,MAAM,WAAW,UAAU;IACvB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,OAAO,EAAE,CAAC;CACvB;AAED,MAAM,MAAM,WAAW,GAAG,WAAW,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;AAE1D,6EAA6E;AAC7E,MAAM,WAAW,cAAc;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,KAAK,GAAG,QAAQ,CAAC;CAC7B;AAED,MAAM,WAAW,cAAc;IAC3B,IAAI,EAAE,GAAG,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;IAC9B,KAAK,EAAE,cAAc,EAAE,CAAC;IACxB,yDAAyD;IACzD,QAAQ,EAAE,OAAO,EAAE,CAAC;CACvB;AAED,gCAAgC;AAChC,eAAO,MAAM,aAAa,SAAU,MAAM,SAAS,MAAM,KAAG,MAAiC,CAAC;AAE9F,qHAAqH;AACrH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,WAAW,CAAC,MAAM,EAAE,SAAS,CAAC,GAAG,CAAC,IAAI,EAAE,MAAM,KAAK,SAAS,MAAM,EAAE,CAgB3G;AAwDD;;;GAGG;AACH,wBAAgB,aAAa,CAAC,CAAC,EAAE,KAAK,EAAE,SAAS,QAAQ,CAAC,CAAC,CAAC,EAAE,EAAE,KAAK,EAAE,SAAS,CAAC,CAAC,CAAC,EAAE,QAAQ,GAAE,WAAuB,GAAG,cAAc,CA8CtI"}
@@ -0,0 +1,164 @@
1
+ /**
2
+ * knowledge/rules/evaluateRules.ts — the incremental FactRule evaluator (PRD 41 §B1, §B5).
3
+ *
4
+ * Verifying traces with early cutoff (Mokhov, Mitchell, Peyton Jones, *Build Systems à la Carte*): every
5
+ * derived row stores the hash of the inputs it read — the rule's id and version, its focus, and for each
6
+ * file in its scope the hash of each fact kind it `reads`, plus the hash of each declared extra input. On
7
+ * the next evaluation a row whose input hash is unchanged is REUSED without calling `derive`; otherwise
8
+ * the rule reruns. No dirty flags: validity is recomputed from the current facts, so a thrown-away row
9
+ * store rebuilds to the same rows.
10
+ *
11
+ * The trace is ENFORCED, not trusted: `derive` receives each file projected to the kinds the rule reads
12
+ * (`projectFileFacts`, every other field empty) and `extra` cut to its declared `extraInputs`, so an undeclared
13
+ * read sees nothing instead of silently breaking early cutoff.
14
+ *
15
+ * Consequences the trace tests pin: a body-only edit reruns `body`-reading rules for that file and skips
16
+ * `structure`-only rules; a global rule is skipped when no file's read kinds changed anywhere.
17
+ */
18
+ import { compareStr } from '../refs.js';
19
+ import { sha256Hex } from '../../retrieval/knowledge-index/sha256.js';
20
+ import { canonicalFileFacts, factKindHashes, projectFileFacts } from '../facts/fileFacts.js';
21
+ import { DEFAULT_RULE_HOPS } from './factRule.js';
22
+ /** The key of a derived row. */
23
+ export const derivedRowKey = (rule, focus) => `${rule}\u0000${focus}`;
24
+ /** Undirected one-hop import neighbours from each file's resolved imports (only files present), each list sorted. */
25
+ export function importNeighbours(files) {
26
+ const adj = new Map();
27
+ const link = (a, b) => {
28
+ let s = adj.get(a);
29
+ if (!s)
30
+ adj.set(a, (s = new Set()));
31
+ s.add(b);
32
+ };
33
+ for (const [path, f] of files) {
34
+ for (const i of f.imports) {
35
+ if (i.resolved === undefined || i.resolved === path || !files.has(i.resolved))
36
+ continue;
37
+ link(path, i.resolved);
38
+ link(i.resolved, path);
39
+ }
40
+ }
41
+ const sorted = new Map([...adj].map(([k, v]) => [k, [...v].sort(compareStr)]));
42
+ return (path) => sorted.get(path) ?? [];
43
+ }
44
+ /** The paths a neighbourhood evaluation reads: BFS to `hops`, plus the guard-chain files when declared. Sorted. */
45
+ function neighbourhoodOf(rule, focus, world, neighbours) {
46
+ const seen = new Set([focus]);
47
+ let frontier = [focus];
48
+ for (let hop = 0; hop < (rule.hops ?? DEFAULT_RULE_HOPS); hop++) {
49
+ const next = [];
50
+ for (const p of frontier)
51
+ for (const n of neighbours(p))
52
+ if (!seen.has(n)) {
53
+ seen.add(n);
54
+ next.push(n);
55
+ }
56
+ frontier = next;
57
+ }
58
+ if (rule.readsGuardChain) {
59
+ for (const e of world.files.get(focus)?.entryPoints ?? [])
60
+ for (const c of e.chain)
61
+ if (world.files.has(c))
62
+ seen.add(c);
63
+ }
64
+ return [...seen].sort(compareStr);
65
+ }
66
+ /** `path\0hash\n` of one kind over every file, hashed once per evaluation and shared by every global rule reading that kind. */
67
+ function kindRoots(world, paths) {
68
+ const roots = new Map();
69
+ return (kind) => {
70
+ let root = roots.get(kind);
71
+ if (root === undefined) {
72
+ root = sha256Hex(paths.map((p) => `${p}\u0000${factKindHashes(world.files.get(p))[kind]}\n`).join(''));
73
+ roots.set(kind, root);
74
+ }
75
+ return root;
76
+ };
77
+ }
78
+ /**
79
+ * The trace hash: rule identity, focus, the read kind hashes of each in-scope file (a global rule: the kind roots,
80
+ * which cover every file), each declared extra input's hash.
81
+ */
82
+ function inputsHash(rule, focus, paths, world, roots) {
83
+ const parts = [`${rule.id}\u0000${rule.version}\u0000${rule.scope}\u0000${focus}\n`];
84
+ if (rule.scope === 'global') {
85
+ for (const k of rule.reads)
86
+ parts.push(`#${k}=${roots(k)}\n`);
87
+ }
88
+ else {
89
+ for (const p of paths) {
90
+ const kinds = factKindHashes(world.files.get(p));
91
+ parts.push(p);
92
+ for (const k of rule.reads)
93
+ parts.push(`\u0000${k}=${kinds[k]}`);
94
+ parts.push('\n');
95
+ }
96
+ }
97
+ for (const name of [...(rule.extraInputs ?? [])].sort(compareStr)) {
98
+ const hash = world.extraHashes?.[name];
99
+ if (hash === undefined)
100
+ throw new Error(`rule_extra_input_unhashed:${rule.id}:${name}`);
101
+ parts.push(`@${name}=${hash}\n`);
102
+ }
103
+ return sha256Hex(parts.join(''));
104
+ }
105
+ const byId = (a, b) => compareStr(a.id, b.id);
106
+ /**
107
+ * Evaluates `rules` against `world`, reusing every row of `previous` whose input hash still holds. Rows for files
108
+ * that no longer exist (and for rules no longer registered) are dropped. Pure: `previous` is not mutated.
109
+ */
110
+ export function evaluateRules(rules, world, previous = new Map()) {
111
+ const ids = new Set();
112
+ for (const r of rules) {
113
+ if (ids.has(r.id))
114
+ throw new Error(`fact_rule_duplicate:${r.id}`);
115
+ ids.add(r.id);
116
+ }
117
+ const paths = [...world.files.keys()].sort(compareStr);
118
+ // Canonical (deep-frozen) facts: the hash and projection caches key on them, and a caller's later mutation never reaches a row.
119
+ const w = { ...world, files: new Map(paths.map((p) => [p, canonicalFileFacts(world.files.get(p))])) };
120
+ let neighbours = w.neighbours;
121
+ const rows = new Map();
122
+ const trace = [];
123
+ const roots = kindRoots(w, paths);
124
+ const evaluate = (rule, focus, scopePaths) => {
125
+ const key = derivedRowKey(rule.id, focus);
126
+ // The version is part of the hash, so a bumped rule never matches its old row.
127
+ const inputs = inputsHash(rule, focus, scopePaths, w, roots);
128
+ const prior = previous.get(key);
129
+ if (prior && prior.inputs === inputs) {
130
+ rows.set(key, prior);
131
+ trace.push({ rule: rule.id, focus, outcome: 'reused' });
132
+ return;
133
+ }
134
+ // `derive` sees ONLY what its trace covers: each file projected to the kinds it reads, `extra` cut to its declared inputs.
135
+ // A rule reading no fact kind (extra inputs only) sees no files: not even which paths exist is in its trace.
136
+ const files = new Map(rule.reads.length === 0 ? [] : scopePaths.map((p) => [p, projectFileFacts(w.files.get(p), rule.reads)]));
137
+ const all = (w.extra ?? {});
138
+ const extra = Object.fromEntries((rule.extraInputs ?? []).filter((n) => n in all).map((n) => [n, all[n]]));
139
+ const findings = rule.derive({ scope: rule.scope, ...(focus ? { focus } : {}), files, extra });
140
+ rows.set(key, { rule: rule.id, version: rule.version, focus, inputs, findings: [...findings].sort(byId) });
141
+ trace.push({ rule: rule.id, focus, outcome: 'ran' });
142
+ };
143
+ for (const rule of [...rules].sort((a, b) => compareStr(a.id, b.id))) {
144
+ if (rule.scope === 'global') {
145
+ evaluate(rule, '', paths);
146
+ continue;
147
+ }
148
+ for (const p of paths) {
149
+ if (rule.scope === 'file') {
150
+ evaluate(rule, p, [p]);
151
+ continue;
152
+ }
153
+ neighbours ??= importNeighbours(w.files);
154
+ evaluate(rule, p, neighbourhoodOf(rule, p, w, neighbours));
155
+ }
156
+ }
157
+ const found = new Map();
158
+ for (const row of rows.values())
159
+ for (const f of row.findings)
160
+ if (!found.has(f.id))
161
+ found.set(f.id, f);
162
+ return { rows, trace, findings: [...found.values()].sort(byId) };
163
+ }
164
+ //# sourceMappingURL=evaluateRules.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"evaluateRules.js","sourceRoot":"","sources":["../../../src/knowledge/rules/evaluateRules.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AACxC,OAAO,EAAE,SAAS,EAAE,MAAM,2CAA2C,CAAC;AAEtE,OAAO,EAAE,kBAAkB,EAAE,cAAc,EAAE,gBAAgB,EAAiC,MAAM,uBAAuB,CAAC;AAC5H,OAAO,EAAE,iBAAiB,EAAiB,MAAM,eAAe,CAAC;AAqCjE,gCAAgC;AAChC,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,IAAY,EAAE,KAAa,EAAU,EAAE,CAAC,GAAG,IAAI,SAAS,KAAK,EAAE,CAAC;AAE9F,qHAAqH;AACrH,MAAM,UAAU,gBAAgB,CAAC,KAAqC;IAClE,MAAM,GAAG,GAAG,IAAI,GAAG,EAAuB,CAAC;IAC3C,MAAM,IAAI,GAAG,CAAC,CAAS,EAAE,CAAS,EAAE,EAAE;QAClC,IAAI,CAAC,GAAG,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;QACnB,IAAI,CAAC,CAAC;YAAE,GAAG,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,IAAI,GAAG,EAAE,CAAC,CAAC,CAAC;QACpC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;IACb,CAAC,CAAC;IACF,KAAK,MAAM,CAAC,IAAI,EAAE,CAAC,CAAC,IAAI,KAAK,EAAE,CAAC;QAC5B,KAAK,MAAM,CAAC,IAAI,CAAC,CAAC,OAAO,EAAE,CAAC;YACxB,IAAI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AAC,CAAC,CAAC;QAC/D,CAAC;IACL,CAAC;IAED,MAAM,KAAK,GAAG,IAAI,GAAG,EAAmB,CAAC;IACzC,KAAK,MAAM,GAAG,IAAI,IAAI,CAAC,MAAM,EAAE;QAAE,KAAK,MAAM,CAAC,IAAI,GAAG,CAAC,QAAQ;YAAE,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC;gBAAE,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC;IACxG,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,QAAQ,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;AACrE,CAAC"}
@@ -0,0 +1,51 @@
1
+ /**
2
+ * knowledge/rules/factGraph.ts — the content-addressed fact graph in memory (PRD 41 §B1, §B8.6): the
3
+ * manifest (path → content hash, facts hash, kind hashes), the facts objects by facts hash, and the
4
+ * derived rule rows with their traces. Pure and immutable: an update returns a new graph.
5
+ *
6
+ * {@link factGraphDump} is the canonical dump the determinism oracle compares: sorted canonical JSON of
7
+ * the manifest, the referenced objects and the derived rows — never a trace outcome, a time or an order of
8
+ * arrival. An incremental graph and a from-scratch build of the same files dump to the same bytes.
9
+ * A durable host persists the same three tables behind its store; this module does no I/O.
10
+ */
11
+ import type { Finding } from '../findingModel.js';
12
+ import { type FactKind, type FileFacts } from '../facts/fileFacts.js';
13
+ import type { FactRule } from './factRule.js';
14
+ import { type DerivedRows, type RuleTraceEntry, type RuleWorld } from './evaluateRules.js';
15
+ export interface FactManifestEntry {
16
+ contentHash: string;
17
+ factsHash: string;
18
+ kinds: Readonly<Record<FactKind, string>>;
19
+ }
20
+ export interface FactGraph {
21
+ manifest: ReadonlyMap<string, FactManifestEntry>;
22
+ /** Facts by facts hash — only those the manifest references. */
23
+ objects: ReadonlyMap<string, FileFacts>;
24
+ derived: DerivedRows;
25
+ }
26
+ /** One file's new facts, or `null` when it was deleted. A rename is a delete plus an add. */
27
+ export interface FactChange {
28
+ path: string;
29
+ facts: FileFacts | null;
30
+ }
31
+ export type FactGraphOptions<X> = Pick<RuleWorld<X>, 'neighbours' | 'extra' | 'extraHashes'>;
32
+ export interface FactGraphUpdate {
33
+ graph: FactGraph;
34
+ trace: RuleTraceEntry[];
35
+ findings: Finding[];
36
+ /** Paths whose facts hash changed (added, edited or deleted), sorted. Empty ⇒ every rule row was reused (early cutoff). */
37
+ changed: string[];
38
+ }
39
+ /** The current dump format. */
40
+ export declare const FACT_GRAPH_DUMP_VERSION = 1;
41
+ export declare const emptyFactGraph: () => FactGraph;
42
+ /**
43
+ * Applies `changes` (the last change of a path wins) and re-evaluates `rules`, reusing every derived row whose
44
+ * trace still holds. Unreferenced objects are dropped, so the graph never depends on its history.
45
+ */
46
+ export declare function updateFactGraph<X>(graph: FactGraph, changes: readonly FactChange[], rules: readonly FactRule<X>[], opts?: FactGraphOptions<X>): FactGraphUpdate;
47
+ /** A from-scratch build: every file added to an empty graph. */
48
+ export declare function buildFactGraph<X>(files: Iterable<readonly [string, FileFacts]>, rules: readonly FactRule<X>[], opts?: FactGraphOptions<X>): FactGraphUpdate;
49
+ /** The canonical dump: sorted canonical JSON of the manifest, the referenced objects and the derived rows. */
50
+ export declare function factGraphDump(graph: FactGraph): string;
51
+ //# sourceMappingURL=factGraph.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"factGraph.d.ts","sourceRoot":"","sources":["../../../src/knowledge/rules/factGraph.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAGH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAiD,KAAK,QAAQ,EAAE,KAAK,SAAS,EAAE,MAAM,uBAAuB,CAAC;AACrH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAC9C,OAAO,EAAiB,KAAK,WAAW,EAAE,KAAK,c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AAK,EAAE,QAAQ,CAAC,SAAS,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC,EAAE,KAAK,EAAE,SAAS,QAAQ,CAAC,CAAC,CAAC,EAAE,EAAE,IAAI,GAAE,gBAAgB,CAAC,CAAC,CAAM,GAAG,eAAe,CAE/J;AAED,8GAA8G;AAC9G,wBAAgB,aAAa,CAAC,KAAK,EAAE,SAAS,GAAG,MAAM,CAQtD"}
@@ -0,0 +1,77 @@
1
+ /**
2
+ * knowledge/rules/factGraph.ts — the content-addressed fact graph in memory (PRD 41 §B1, §B8.6): the
3
+ * manifest (path → content hash, facts hash, kind hashes), the facts objects by facts hash, and the
4
+ * derived rule rows with their traces. Pure and immutable: an update returns a new graph.
5
+ *
6
+ * {@link factGraphDump} is the canonical dump the determinism oracle compares: sorted canonical JSON of
7
+ * the manifest, the referenced objects and the derived rows — never a trace outcome, a time or an order of
8
+ * arrival. An incremental graph and a from-scratch build of the same files dump to the same bytes.
9
+ * A durable host persists the same three tables behind its store; this module does no I/O.
10
+ */
11
+ import { canonicalJson, compareStr, isRootFilePath, normalizePath } from '../refs.js';
12
+ import { canonicalFileFacts, factKindHashes, factsHash } from '../facts/fileFacts.js';
13
+ import { evaluateRules } from './evaluateRules.js';
14
+ /** The current dump format. */
15
+ export const FACT_GRAPH_DUMP_VERSION = 1;
16
+ export const emptyFactGraph = () => ({ manifest: new Map(), objects: new Map(), derived: new Map() });
17
+ /** The ONE path form of the graph (§B8.1); throws `fact_graph_bad_path` for a path outside the root. */
18
+ function pathOf(raw) {
19
+ const p = normalizePath(raw);
20
+ if (!isRootFilePath(p))
21
+ throw new Error(`fact_graph_bad_path:${raw}`);
22
+ return p;
23
+ }
24
+ /**
25
+ * Applies `changes` (the last change of a path wins) and re-evaluates `rules`, reusing every derived row whose
26
+ * trace still holds. Unreferenced objects are dropped, so the graph never depends on its history.
27
+ */
28
+ export function updateFactGraph(graph, changes, rules, opts = {}) {
29
+ const manifest = new Map(graph.manifest);
30
+ const objects = new Map(graph.objects);
31
+ const touched = new Set();
32
+ for (const c of changes) {
33
+ const path = pathOf(c.path);
34
+ touched.add(path);
35
+ if (c.facts === null) {
36
+ manifest.delete(path);
37
+ continue;
38
+ }
39
+ // Canonical facts are deep-frozen: the stored object can never be mutated under its hash.
40
+ const facts = canonicalFileFacts(c.facts);
41
+ const hash = factsHash(facts);
42
+ if (manifest.get(path)?.factsHash === hash)
43
+ continue;
44
+ if (!objects.has(hash))
45
+ objects.set(hash, facts);
46
+ manifest.set(path, { contentHash: facts.contentHash, factsHash: hash, kinds: factKindHashes(objects.get(hash)) });
47
+ }
48
+ // Against the PRE-batch manifest, so a path edited and reverted (or added and deleted) in one batch is no change.
49
+ const changed = [...touched].filter((p) => graph.manifest.get(p)?.factsHash !== manifest.get(p)?.factsHash);
50
+ const live = new Set([...manifest.values()].map((e) => e.factsHash));
51
+ for (const h of [...objects.keys()])
52
+ if (!live.has(h))
53
+ objects.delete(h);
54
+ const files = new Map([...manifest].map(([p, e]) => [p, objects.get(e.factsHash)]));
55
+ const result = evaluateRules(rules, { files, ...opts }, graph.derived);
56
+ return {
57
+ graph: { manifest, objects, derived: result.rows },
58
+ trace: result.trace,
59
+ findings: result.findings,
60
+ changed: changed.sort(compareStr),
61
+ };
62
+ }
63
+ /** A from-scratch build: every file added to an empty graph. */
64
+ export function buildFactGraph(files, rules, opts = {}) {
65
+ return updateFactGraph(emptyFactGraph(), [...files].map(([path, facts]) => ({ path, facts })), rules, opts);
66
+ }
67
+ /** The canonical dump: sorted canonical JSON of the manifest, the referenced objects and the derived rows. */
68
+ export function factGraphDump(graph) {
69
+ const sortedEntries = (m) => [...m].sort((a, b) => compareStr(a[0], b[0]));
70
+ return canonicalJson({
71
+ version: FACT_GRAPH_DUMP_VERSION,
72
+ manifest: sortedEntries(graph.manifest).map(([path, e]) => ({ path, ...e })),
73
+ objects: sortedEntries(graph.objects).map(([hash, facts]) => ({ hash, facts })),
74
+ derived: sortedEntries(graph.derived).map(([, row]) => row),
75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E,EAAE,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IACtG,OAAO,aAAa,CAAC;QACjB,OAAO,EAAE,uBAAuB;QAChC,QAAQ,EAAE,aAAa,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,GAAG,CAAC,EAAE,CAAC,CAAC;QAC5E,OAAO,EAAE,aAAa,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC;QAC/E,OAAO,EAAE,aAAa,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,GAAG,CAAC,EAAE,EAAE,CAAC,GAAG,CAAC;KAC9D,CAAC,CAAC;AACP,CAAC"}
@@ -0,0 +1,56 @@
1
+ /**
2
+ * knowledge/rules/factRule.ts — the FactRule contract (PRD 41 §B5): derived knowledge as rules over facts.
3
+ *
4
+ * A rule declares what it READS (fact kinds, and optionally named non-fact inputs such as the knowledge
5
+ * graph), its SCOPE, and for a neighbourhood rule its HOPS. The evaluator (evaluateRules.ts) uses exactly
6
+ * those declarations as the rule's dependency trace: a rule reruns only when one of its read inputs changed
7
+ * (Build Systems à la Carte: verifying traces + early cutoff). So `derive` must read nothing else — the
8
+ * determinism oracle (factGraph.oracle.test.ts) catches a rule that does.
9
+ *
10
+ * - `file`: one evaluation per file, reading that file's facts.
11
+ * - `neighbourhood`: one per file, reading the file plus its import neighbours up to `hops` (default 1,
12
+ * max 2), plus — with `readsGuardChain` — the guard-chain files of its entry points.
13
+ * - `global`: one evaluation, reading every file.
14
+ *
15
+ * One rule per file under knowledge/rules/, registered as data (findings.ts is the generic registry).
16
+ */
17
+ import type { Finding } from '../findingModel.js';
18
+ import type { FactKind, FileFacts } from '../facts/fileFacts.js';
19
+ export type RuleScope = 'file' | 'neighbourhood' | 'global';
20
+ export declare const DEFAULT_RULE_HOPS = 1;
21
+ /** The C2 impact-set bound: a neighbourhood never reaches further than this. */
22
+ export declare const MAX_RULE_HOPS = 2;
23
+ /** What one evaluation of a rule sees. */
24
+ export interface RuleInput<X = Record<never, never>> {
25
+ scope: RuleScope;
26
+ /** `file` / `neighbourhood`: the file the evaluation is for. Absent for `global`. */
27
+ focus?: string;
28
+ /** The facts in scope, by root-relative path (iterate in sorted order): the focus; the focus plus its neighbourhood; every file. */
29
+ files: ReadonlyMap<string, FileFacts>;
30
+ /** The named non-fact inputs the host supplies (only those the rule declares in `extraInputs` are part of its trace). */
31
+ extra: X;
32
+ }
33
+ export interface FactRule<X = Record<never, never>> {
34
+ /** 'cycle', 'partition-violation', 'unused-export', 'io-in-loop', … */
35
+ id: string;
36
+ /** Bump whenever `derive` can return different findings for the same inputs: it is part of the cache key. */
37
+ version: number;
38
+ /** The fact kinds it consumes — its dependency trace. Empty only for a global rule reading extra inputs alone. */
39
+ reads: readonly FactKind[];
40
+ scope: RuleScope;
41
+ /** Neighbourhood rules: import hops to read around the focus (1 or 2). */
42
+ hops?: number;
43
+ /** Neighbourhood rules: also read the facts of the guard-chain files of the focus's entry points. */
44
+ readsGuardChain?: boolean;
45
+ /** Names of `extra` inputs `derive` reads; each needs a hash from the host (`RuleWorld.extraHashes`). */
46
+ extraInputs?: readonly (keyof X & string)[];
47
+ /** Pure. Findings sorted by id (the evaluator sorts again). */
48
+ derive(input: RuleInput<X>): Finding[];
49
+ }
50
+ /**
51
+ * Validates and freezes a rule: a non-empty id, an integer version ≥ 1, known `reads` — empty only for a global rule
52
+ * that reads extra inputs alone (it then sees no files) — and `hops` only on a neighbourhood rule and within
53
+ * 1..{@link MAX_RULE_HOPS}. Throws `fact_rule_invalid:<id>:<why>`.
54
+ */
55
+ export declare function defineRule<X = Record<never, never>>(rule: FactRule<X>): FactRule<X>;
56
+ //# sourceMappingURL=factRule.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"factRule.d.ts","sourceRoot":"","sources":["../../../src/knowledge/rules/factRule.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,KAAK,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAGjE,MAAM,MAAM,SAAS,GAAG,MAAM,GAAG,eAAe,GAAG,QAAQ,CAAC;AAE5D,eAAO,MAAM,iBAAiB,IAAI,CAAC;AACnC,gFAAgF;AAChF,eAAO,MAAM,aAAa,IAAI,CAAC;AAE/B,0CAA0C;AAC1C,MAAM,WAAW,SAAS,CAAC,CAAC,GAAG,MAAM,CAAC,KAAK,EAAE,KAAK,CAAC;IAC/C,KAAK,EAAE,SAAS,CAAC;IACjB,qFAAqF;IACrF,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,oIAAoI;IACpI,KAAK,EAAE,WAAW,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;IACtC,yHAAyH;IACzH,KAAK,EAAE,CAAC,CAAC;CACZ;AAED,MAAM,WAAW,QAAQ,CAAC,CAAC,GAAG,MAAM,CAAC,KAAK,EAAE,KAAK,CAAC;IAC9C,uEAAuE;IACvE,EAAE,EAAE,MAAM,CAAC;IACX,6GAA6G;IAC7G,OAAO,EAAE,MAAM,CAAC;IAChB,kHAAkH;IAClH,KAAK,EAAE,SAAS,QAAQ,EAAE,CAAC;IAC3B,KAAK,EAAE,SAAS,CAAC;IACjB,0EAA0E;IAC1E,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,qGAAqG;IACrG,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,yGAAyG;IACzG,WAAW,CAAC,EAAE,SAAS,CAAC,MAAM,CAAC,GAAG,MAAM,CAAC,EAAE,CAAC;IAC5C,+DAA+D;IAC/D,MAAM,CAAC,KAAK,EAAE,SAAS,CAAC,CAAC,CAAC,GAAG,OAAO,EAAE,CAAC;CAC1C;AAED;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,CAAC,GAAG,MAAM,CAAC,KAAK,EAAE,KAAK,CAAC,EAAE,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,CAYnF"}
@@ -0,0 +1,46 @@
1
+ /**
2
+ * knowledge/rules/factRule.ts — the FactRule contract (PRD 41 §B5): derived knowledge as rules over facts.
3
+ *
4
+ * A rule declares what it READS (fact kinds, and optionally named non-fact inputs such as the knowledge
5
+ * graph), its SCOPE, and for a neighbourhood rule its HOPS. The evaluator (evaluateRules.ts) uses exactly
6
+ * those declarations as the rule's dependency trace: a rule reruns only when one of its read inputs changed
7
+ * (Build Systems à la Carte: verifying traces + early cutoff). So `derive` must read nothing else — the
8
+ * determinism oracle (factGraph.oracle.test.ts) catches a rule that does.
9
+ *
10
+ * - `file`: one evaluation per file, reading that file's facts.
11
+ * - `neighbourhood`: one per file, reading the file plus its import neighbours up to `hops` (default 1,
12
+ * max 2), plus — with `readsGuardChain` — the guard-chain files of its entry points.
13
+ * - `global`: one evaluation, reading every file.
14
+ *
15
+ * One rule per file under knowledge/rules/, registered as data (findings.ts is the generic registry).
16
+ */
17
+ import { FACT_KINDS } from '../facts/fileFacts.js';
18
+ export const DEFAULT_RULE_HOPS = 1;
19
+ /** The C2 impact-set bound: a neighbourhood never reaches further than this. */
20
+ export const MAX_RULE_HOPS = 2;
21
+ /**
22
+ * Validates and freezes a rule: a non-empty id, an integer version ≥ 1, known `reads` — empty only for a global rule
23
+ * that reads extra inputs alone (it then sees no files) — and `hops` only on a neighbourhood rule and within
24
+ * 1..{@link MAX_RULE_HOPS}. Throws `fact_rule_invalid:<id>:<why>`.
25
+ */
26
+ export function defineRule(rule) {
27
+ const fail = (why) => { throw new Error(`fact_rule_invalid:${rule.id}:${why}`); };
28
+ if (!rule.id)
29
+ fail('id');
30
+ if (!Number.isInteger(rule.version) || rule.version < 1)
31
+ fail('version');
32
+ if (rule.reads.some((k) => !FACT_KINDS.includes(k)))
33
+ fail('reads');
34
+ if (rule.reads.length === 0 && (rule.scope !== 'global' || (rule.extraInputs ?? []).length === 0))
35
+ fail('reads');
36
+ if (rule.hops !== undefined) {
37
+ if (rule.scope !== 'neighbourhood')
38
+ fail('hops-outside-neighbourhood');
39
+ if (!Number.isInteger(rule.hops) || rule.hops < 1 || rule.hops > MAX_RULE_HOPS)
40
+ fail('hops');
41
+ }
42
+ if (rule.readsGuardChain && rule.scope !== 'neighbourhood')
43
+ fail('guard-chain-outside-neighbourhood');
44
+ return Object.freeze({ ...rule, reads: Object.freeze([...new Set(rule.reads)].sort()) });
45
+ }
46
+ //# sourceMappingURL=factRule.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"factRule.js","sourceRoot":"","sources":["../../../src/knowledge/rules/factRule.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAIH,OAAO,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AAInD,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,CAAC;AACnC,gFAAgF;AAChF,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,CAAC;AA+B/B;;;;GAIG;AACH,MAAM,UAAU,UAAU,CAA2B,IAAiB;IAClE,MAAM,IAAI,GAAG,CAAC,GAAW,EAAS,EAAE,GAAG,MAAM,IAAI,KAAK,CAAC,qBAAqB,IAAI,CAAC,EAAE,IAAI,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;IACjG,IAAI,CAAC,IAAI,CAAC,EAAE;QAAE,IAAI,CAAC,IAAI,CAAC,CAAC;IACzB,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,IAAI,CAAC,OAAO,GAAG,CAAC;QAAE,IAAI,CAAC,SAAS,CAAC,CAAC;IACzE,IAAI,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;QAAE,IAAI,CAAC,OAAO,CAAC,CAAC;IACnE,IAAI,IAAI,CAAC,KAAK,CAAC,MAAM,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,KAAK,QAAQ,IAAI,CAAC,IAAI,CAAC,WAAW,IAAI,EAAE,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC;QAAE,IAAI,CAAC,OAAO,CAAC,CAAC;IACjH,IAAI,IAAI,CAAC,IAAI,KAAK,SAAS,EAAE,CAAC;QAC1B,IAAI,IAAI,CAAC,KAAK,KAAK,eAAe;YAAE,IAAI,CAAC,4BAA4B,CAAC,CAAC;QACvE,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,IAAI,GAAG,CAAC,IAAI,IAAI,CAAC,IAAI,GAAG,aAAa;YAAE,IAAI,CAAC,MAAM,CAAC,CAAC;IACjG,CAAC;IACD,IAAI,IAAI,CAAC,eAAe,IAAI,IAAI,CAAC,KAAK,KAAK,eAAe;QAAE,IAAI,CAAC,mCAAmC,CAAC,CAAC;IACtG,OAAO,MAAM,CAAC,MAAM,CAAC,EAAE,GAAG,IAAI,EAAE,KAAK,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC,GAAG,IAAI,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,EAAE,CAAC,CAAC;AAC7F,CAAC"}
@@ -0,0 +1,8 @@
1
+ /**
2
+ * knowledge/rules/godModule.ts — `god-module` as a rule: the detector and its limits stay in
3
+ * knowledge/godModule.ts (the code pack imports the limits from there); this registers it.
4
+ * Reads the lines, exports and worst-function complexity badges of the file nodes of the knowledge input.
5
+ */
6
+ import type { KnowledgeRuleExtra } from './knowledgeInput.js';
7
+ export declare const godModuleRule: import("./factRule.js").FactRule<KnowledgeRuleExtra>;
8
+ //# sourceMappingURL=godModule.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"godModule.d.ts","sourceRoot":"","sources":["../../../src/knowledge/rules/godModule.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AAE9D,eAAO,MAAM,aAAa,sDAOxB,CAAC"}
@@ -0,0 +1,16 @@
1
+ /**
2
+ * knowledge/rules/godModule.ts — `god-module` as a rule: the detector and its limits stay in
3
+ * knowledge/godModule.ts (the code pack imports the limits from there); this registers it.
4
+ * Reads the lines, exports and worst-function complexity badges of the file nodes of the knowledge input.
5
+ */
6
+ import { godModuleFindings, GOD_MODULE_KIND } from '../godModule.js';
7
+ import { defineRule } from './factRule.js';
8
+ export const godModuleRule = defineRule({
9
+ id: GOD_MODULE_KIND,
10
+ version: 1,
11
+ reads: [], // the knowledge graph only (the measures are file-node badges)
12
+ scope: 'global',
13
+ extraInputs: ['knowledge'],
14
+ derive: ({ extra: { knowledge: k } }) => godModuleFindings(k.graph),
15
+ });
16
+ //# sourceMappingURL=godModule.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"godModule.js","sourceRoot":"","sources":["../../../src/knowledge/rules/godModule.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AACrE,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAG3C,MAAM,CAAC,MAAM,aAAa,GAAG,UAAU,CAAqB;IACxD,EAAE,EAAE,eAAe;IACnB,OAAO,EAAE,CAAC;IACV,KAAK,EAAE,EAAE,EAAE,+DAA+D;IAC1E,KAAK,EAAE,QAAQ;IACf,WAAW,EAAE,CAAC,WAAW,CAAC;IAC1B,MAAM,EAAE,CAAC,EAAE,KAAK,EAAE,EAAE,SAAS,EAAE,CAAC,EAAE,EAAE,EAAE,EAAE,CAAC,iBAAiB,CAAC,CAAC,CAAC,KAAK,CAAC;CACtE,CAAC,CAAC"}
@@ -0,0 +1,35 @@
1
+ /**
2
+ * knowledge/rules/knowledgeInput.ts — the non-fact input of the generic finding rules ported from
3
+ * findings.ts (PRD 41 §B5): the built knowledge graph plus the partition registries and near-duplicate
4
+ * pairs. These rules are `global` with `reads: []` and `extraInputs: ['knowledge']`, so in an incremental
5
+ * evaluation they rerun exactly when {@link knowledgeInputHash} of this input moves.
6
+ */
7
+ import type { SimilarPair } from '../../retrieval/knowledge-index/types.js';
8
+ import type { Finding } from '../findingModel.js';
9
+ import type { KNode, KnowledgeGraph, NodeRef, PartitionRegistry } from '../model.js';
10
+ import type { FactRule } from './factRule.js';
11
+ export interface KnowledgeFacts {
12
+ graph: KnowledgeGraph;
13
+ byRef: ReadonlyMap<NodeRef, KNode>;
14
+ registries: ReadonlyMap<string, PartitionRegistry>;
15
+ nearDuplicates: readonly SimilarPair[];
16
+ }
17
+ export interface KnowledgeRuleExtra {
18
+ knowledge: KnowledgeFacts;
19
+ }
20
+ /** A rule over the knowledge graph. */
21
+ export type KnowledgeRule = FactRule<KnowledgeRuleExtra>;
22
+ /** The extra input name the knowledge rules declare. */
23
+ export declare const KNOWLEDGE_INPUT = "knowledge";
24
+ export declare function knowledgeFacts(graph: KnowledgeGraph, registries: ReadonlyMap<string, PartitionRegistry>, nearDuplicates?: readonly SimilarPair[]): KnowledgeFacts;
25
+ /**
26
+ * THE hash a host passes as `extraHashes.knowledge`: sha256 over the canonical JSON of the graph digest, the partition
27
+ * registries (sorted by pack) and the near-duplicate pairs (sorted). The graph digest alone misses a registry edit
28
+ * (`unpartitioned`) or pairs passed beside the graph (`possible-same`).
29
+ */
30
+ export declare function knowledgeInputHash(k: KnowledgeFacts): string;
31
+ /** The pack of a ref's node, else the fallback pack. */
32
+ export declare const packOfRef: (k: KnowledgeFacts, ref: NodeRef) => string;
33
+ /** Runs knowledge rules once, in registry order, non-incrementally (the `computeFindings` path). */
34
+ export declare function runKnowledgeRules(rules: readonly KnowledgeRule[], knowledge: KnowledgeFacts): Finding[];
35
+ //# sourceMappingURL=knowledgeInput.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"knowledgeInput.d.ts","sourceRoot":"","sources":["../../../src/knowledge/rules/knowledgeInput.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,0CAA0C,CAAC;AAC5E,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,KAAK,EAAE,KAAK,EAAE,cAAc,EAAE,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAGrF,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAE9C,MAAM,WAAW,cAAc;IAC3B,KAAK,EAAE,cAAc,CAAC;IACtB,KAAK,EAAE,WAAW,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;IACnC,UAAU,EAAE,WAAW,CAAC,MAAM,EAAE,iBAAiB,CAAC,CAAC;IACnD,cAAc,EAAE,SAAS,WAAW,EAAE,CAAC;CAC1C;AAED,MAAM,WAAW,kBAAkB;IAC/B,SAAS,EAAE,cAAc,CAAC;CAC7B;AAED,uCAAuC;AACvC,MAAM,MAAM,aAAa,GAAG,QAAQ,CAAC,kBAAkB,CAAC,CAAC;AAEzD,wDAAwD;AACxD,eAAO,MAAM,eAAe,cAAc,CAAC;AAE3C,wBAAgB,cAAc,CAC1B,KAAK,EAAE,cAAc,EACrB,UAAU,EAAE,WAAW,CAAC,MAAM,EAAE,iBAAiB,CAAC,EAClD,cAAc,GAAE,SAAS,WAAW,EAAO,GAC5C,cAAc,CAEhB;AAED;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAC,CAAC,EAAE,cAAc,GAAG,MAAM,CAM5D;AAED,wDAAwD;AACxD,eAAO,MAAM,SAAS,MAAO,cAAc,OAAO,OAAO,KAAG,MAAoD,CAAC;AAEjH,oGAAoG;AACpG,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,SAAS,aAAa,EAAE,EAAE,SAAS,EAAE,cAAc,GAAG,OAAO,EAAE,CAGvG"}
@@ -0,0 +1,33 @@
1
+ /**
2
+ * knowledge/rules/knowledgeInput.ts — the non-fact input of the generic finding rules ported from
3
+ * findings.ts (PRD 41 §B5): the built knowledge graph plus the partition registries and near-duplicate
4
+ * pairs. These rules are `global` with `reads: []` and `extraInputs: ['knowledge']`, so in an incremental
5
+ * evaluation they rerun exactly when {@link knowledgeInputHash} of this input moves.
6
+ */
7
+ import { canonicalJson, compareStr, FALLBACK_PACK_ID } from '../refs.js';
8
+ import { sha256Hex } from '../../retrieval/knowledge-index/sha256.js';
9
+ /** The extra input name the knowledge rules declare. */
10
+ export const KNOWLEDGE_INPUT = 'knowledge';
11
+ export function knowledgeFacts(graph, registries, nearDuplicates = []) {
12
+ return { graph, byRef: new Map(graph.nodes.map((n) => [n.ref, n])), registries, nearDuplicates };
13
+ }
14
+ /**
15
+ * THE hash a host passes as `extraHashes.knowledge`: sha256 over the canonical JSON of the graph digest, the partition
16
+ * registries (sorted by pack) and the near-duplicate pairs (sorted). The graph digest alone misses a registry edit
17
+ * (`unpartitioned`) or pairs passed beside the graph (`possible-same`).
18
+ */
19
+ export function knowledgeInputHash(k) {
20
+ return sha256Hex(canonicalJson({
21
+ digest: k.graph.scope.digest,
22
+ registries: [...k.registries].sort((a, b) => compareStr(a[0], b[0])),
23
+ nearDuplicates: k.nearDuplicates.map((p) => canonicalJson(p)).sort(compareStr),
24
+ }));
25
+ }
26
+ /** The pack of a ref's node, else the fallback pack. */
27
+ export const packOfRef = (k, ref) => k.byRef.get(ref)?.pack ?? FALLBACK_PACK_ID;
28
+ /** Runs knowledge rules once, in registry order, non-incrementally (the `computeFindings` path). */
29
+ export function runKnowledgeRules(rules, knowledge) {
30
+ const files = new Map();
31
+ return rules.flatMap((r) => r.derive({ scope: r.scope, files, extra: { knowledge } }));
32
+ }
33
+ //# sourceMappingURL=knowledgeInput.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"knowledgeInput.js","sourceRoot":"","sources":["../../../src/knowledge/rules/knowledgeInput.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAKH,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AACzE,OAAO,EAAE,SAAS,EAAE,MAAM,2CAA2C,CAAC;AAiBtE,wDAAwD;AACxD,MAAM,CAAC,MAAM,eAAe,GAAG,WAAW,CAAC;AAE3C,MAAM,UAAU,cAAc,CAC1B,KAAqB,EACrB,UAAkD,EAClD,cAAc,GAA2B,EAAE;IAE3C,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,GAAG,CAAC,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC,CAAU,CAAC,CAAC,EAAE,UAAU,EAAE,cAAc,EAAE,CAAC;AAC9G,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,kBAAkB,CAAC,CAAiB;IAChD,OAAO,SAAS,CAAC,aAAa,CAAC;QAC3B,MAAM,EAAE,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,MAAM;QAC5B,UAAU,EAAE,CAAC,GAAG,CAAC,CAAC,UAAU,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;QACpE,cAAc,EAAE,CAAC,CAAC,cAAc,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,UAAU,CAAC;KACjF,CAAC,CAAC,CAAC;AACR,CAAC;AAED,wDAAwD;AACxD,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,CAAiB,EAAE,GAAY,EAAU,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,IAAI,IAAI,gBAAgB,CAAC;AAEjH,oGAAoG;AACpG,MAAM,UAAU,iBAAiB,CAAC,KAA+B,EAAE,SAAyB;IACxF,MAAM,KAAK,GAAG,IAAI,GAAG,EAAE,CAAC;IACxB,OAAO,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE,SAAS,EAAE,EAAE,CAAC,CAAC,CAAC;AAC3F,CAAC"}
@@ -0,0 +1,7 @@
1
+ /**
2
+ * knowledge/rules/nearDuplicate.ts — `near-duplicate`: one finding per near-duplicate edge.
3
+ * Anchor (PRD 41 §C3): the sorted-first ref of the pair.
4
+ */
5
+ import { type KnowledgeRuleExtra } from './knowledgeInput.js';
6
+ export declare const nearDuplicateRule: import("./factRule.js").FactRule<KnowledgeRuleExtra>;
7
+ //# sourceMappingURL=nearDuplicate.d.ts.map