@dataparade/cli 0.0.31 → 0.0.33

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 (141) hide show
  1. package/README.md +56 -1
  2. package/dist/package.json +4 -3
  3. package/dist/src/ai-enrichment/fallbacks.js +12 -1
  4. package/dist/src/cli.js +50 -4
  5. package/dist/src/core/pipeline/omit-internal-implementation.js +13 -6
  6. package/dist/src/diff/register-diff-command.d.ts +4 -0
  7. package/dist/src/diff/register-diff-command.js +79 -0
  8. package/dist/src/diff/run-diff.d.ts +80 -0
  9. package/dist/src/diff/run-diff.js +206 -0
  10. package/dist/src/diff/upload-diff.d.ts +26 -0
  11. package/dist/src/diff/upload-diff.js +69 -0
  12. package/dist/src/facts/apply-answers.js +1 -1
  13. package/dist/src/facts/build-facts.d.ts +3 -1
  14. package/dist/src/facts/build-facts.js +10 -1
  15. package/dist/src/facts/identity.d.ts +27 -0
  16. package/dist/src/facts/identity.js +203 -0
  17. package/dist/src/facts/io.d.ts +2 -0
  18. package/dist/src/facts/io.js +7 -0
  19. package/dist/src/facts/scan-stamps.d.ts +55 -0
  20. package/dist/src/facts/scan-stamps.js +165 -0
  21. package/dist/src/facts/schema.d.ts +243 -7
  22. package/dist/src/facts/schema.js +71 -1
  23. package/dist/src/kb/answer-fingerprint.d.ts +44 -0
  24. package/dist/src/kb/answer-fingerprint.js +173 -0
  25. package/dist/src/kb/brief.d.ts +8 -1
  26. package/dist/src/kb/brief.js +18 -2
  27. package/dist/src/kb/land.js +3 -0
  28. package/dist/src/kb/register-kb-commands.js +7 -1
  29. package/dist/src/output/git-context.d.ts +25 -0
  30. package/dist/src/output/git-context.js +122 -0
  31. package/dist/src/output/scan-output-excludes.js +5 -1
  32. package/dist/src/output/write-scan-knowledge.d.ts +2 -0
  33. package/dist/src/output/write-scan-knowledge.js +1 -0
  34. package/dist/src/platform-api/dataparade-app-base-url.d.ts +8 -4
  35. package/dist/src/platform-api/dataparade-app-base-url.js +10 -5
  36. package/dist/src/platform-api/upload-client.d.ts +1 -1
  37. package/dist/src/platform-api/upload-client.js +32 -1
  38. package/dist/src/platform-api/upload.types.d.ts +55 -0
  39. package/dist/src/sensitive-scan/process.d.ts +5 -0
  40. package/dist/src/sensitive-scan/process.js +48 -0
  41. package/dist/src/sensitive-scan/project.d.ts +12 -0
  42. package/dist/src/sensitive-scan/project.js +177 -0
  43. package/dist/src/sensitive-scan/report.d.ts +50 -0
  44. package/dist/src/sensitive-scan/report.js +32 -0
  45. package/dist/src/sensitive-scan/sessions.d.ts +2 -0
  46. package/dist/src/sensitive-scan/sessions.js +120 -0
  47. package/dist/src/ui/scan-progress-renderer.d.ts +1 -0
  48. package/dist/src/ui/scan-progress-renderer.js +8 -0
  49. package/dist/src/upload/build-preview-url.d.ts +2 -2
  50. package/dist/src/upload/build-preview-url.js +4 -4
  51. package/dist/src/upload/run-upload.js +6 -2
  52. package/dist/src/upload/types.d.ts +2 -0
  53. package/dist/tests/unit/ai-enrichment/fallbacks.spec.js +30 -0
  54. package/dist/tests/unit/core/omit-internal-implementation.spec.js +21 -0
  55. package/dist/tests/unit/diff/run-diff.spec.d.ts +1 -0
  56. package/dist/tests/unit/diff/run-diff.spec.js +189 -0
  57. package/dist/tests/unit/diff/upload-diff.spec.d.ts +1 -0
  58. package/dist/tests/unit/diff/upload-diff.spec.js +81 -0
  59. package/dist/tests/unit/facts/apply-answers.spec.js +1 -1
  60. package/dist/tests/unit/facts/build-facts.spec.js +67 -1
  61. package/dist/tests/unit/facts/identity.spec.d.ts +1 -0
  62. package/dist/tests/unit/facts/identity.spec.js +148 -0
  63. package/dist/tests/unit/facts/scan-stamps.spec.d.ts +1 -0
  64. package/dist/tests/unit/facts/scan-stamps.spec.js +95 -0
  65. package/dist/tests/unit/kb/answer-fingerprint.spec.d.ts +1 -0
  66. package/dist/tests/unit/kb/answer-fingerprint.spec.js +157 -0
  67. package/dist/tests/unit/kb/kb-interview-loop.spec.js +16 -0
  68. package/dist/tests/unit/output/git-context.spec.d.ts +1 -0
  69. package/dist/tests/unit/output/git-context.spec.js +98 -0
  70. package/dist/tests/unit/platform-api/dataparade-app-base-url.spec.js +9 -0
  71. package/dist/tests/unit/platform-api/upload-client.spec.js +101 -1
  72. package/dist/tests/unit/sensitive-scan/project.spec.d.ts +1 -0
  73. package/dist/tests/unit/sensitive-scan/project.spec.js +98 -0
  74. package/dist/tests/unit/upload/build-preview-url.spec.js +4 -0
  75. package/dist/tests/unit/upload/collect-scan-sidecars.spec.js +1 -0
  76. package/dist/tests/unit/upload/run-upload.spec.js +32 -0
  77. package/dist/vendor/kb-compare/dist/src/canonical.d.ts +80 -0
  78. package/dist/vendor/kb-compare/dist/src/canonical.js +119 -0
  79. package/dist/vendor/kb-compare/dist/src/change-set.d.ts +144 -0
  80. package/dist/vendor/kb-compare/dist/src/change-set.js +80 -0
  81. package/dist/vendor/kb-compare/dist/src/comparable.d.ts +23 -0
  82. package/dist/vendor/kb-compare/dist/src/comparable.js +48 -0
  83. package/dist/vendor/kb-compare/dist/src/compare.d.ts +52 -0
  84. package/dist/vendor/kb-compare/dist/src/compare.js +370 -0
  85. package/dist/vendor/kb-compare/dist/src/fingerprint.d.ts +40 -0
  86. package/dist/vendor/kb-compare/dist/src/fingerprint.js +88 -0
  87. package/dist/vendor/kb-compare/dist/src/index.d.ts +15 -0
  88. package/dist/vendor/kb-compare/dist/src/index.js +34 -0
  89. package/dist/vendor/kb-compare/dist/src/input.d.ts +128 -0
  90. package/dist/vendor/kb-compare/dist/src/input.js +75 -0
  91. package/dist/vendor/kb-compare/dist/src/slot-rules.d.ts +69 -0
  92. package/dist/vendor/kb-compare/dist/src/slot-rules.js +203 -0
  93. package/dist/vendor/kb-compare/dist/src/summary.d.ts +3 -0
  94. package/dist/vendor/kb-compare/dist/src/summary.js +47 -0
  95. package/dist/vendor/kb-compare/package.json +6 -0
  96. package/dist/vendor/scanner/dist/src/analyzers/python/python-detection-config.d.ts +11 -0
  97. package/dist/vendor/scanner/dist/src/analyzers/python/python-detection-config.js +2 -0
  98. package/dist/vendor/scanner/dist/src/analyzers/registry.d.ts +2 -0
  99. package/dist/vendor/scanner/dist/src/analyzers/registry.js +3 -2
  100. package/dist/vendor/scanner/dist/src/analyzers/typescript/typescript-detection-config.d.ts +17 -0
  101. package/dist/vendor/scanner/dist/src/analyzers/typescript/typescript-detection-config.js +11 -0
  102. package/dist/vendor/scanner/dist/src/classifier/application-injection.js +5 -0
  103. package/dist/vendor/scanner/dist/src/classifier/component-factory.js +33 -3
  104. package/dist/vendor/scanner/dist/src/classifier/enhance.js +14 -0
  105. package/dist/vendor/scanner/dist/src/classifier/external-url-third-party.d.ts +8 -0
  106. package/dist/vendor/scanner/dist/src/classifier/external-url-third-party.js +43 -2
  107. package/dist/vendor/scanner/dist/src/classifier/postprocessing.js +4 -1
  108. package/dist/vendor/scanner/dist/src/config/third-party-detection-config.d.ts +41 -0
  109. package/dist/vendor/scanner/dist/src/config/third-party-detection-config.js +65 -1
  110. package/dist/vendor/scanner/dist/src/core/pipeline/stable-component-ids.d.ts +8 -0
  111. package/dist/vendor/scanner/dist/src/core/pipeline/stable-component-ids.js +11 -1
  112. package/dist/vendor/scanner/dist/src/core/pipeline/structural-scan.js +2 -0
  113. package/dist/vendor/scanner/dist/src/eval/canonical/scanner/components.d.ts +5 -0
  114. package/dist/vendor/scanner/dist/src/eval/canonical/scanner/components.js +10 -2
  115. package/dist/vendor/scanner/dist/src/eval/canonical/scanner/data-flows.js +4 -4
  116. package/dist/vendor/scanner/dist/src/index.d.ts +1 -1
  117. package/dist/vendor/scanner/dist/src/index.js +4 -2
  118. package/dist/vendor/scanner/dist/src/patterns/config.js +7 -1
  119. package/dist/vendor/scanner/dist/src/patterns/detectors/git-remote-owner.d.ts +14 -0
  120. package/dist/vendor/scanner/dist/src/patterns/detectors/git-remote-owner.js +108 -0
  121. package/dist/vendor/scanner/dist/src/patterns/detectors/python-outbound-http.d.ts +93 -0
  122. package/dist/vendor/scanner/dist/src/patterns/detectors/python-outbound-http.js +1206 -0
  123. package/dist/vendor/scanner/dist/src/patterns/detectors/python-source.d.ts +180 -0
  124. package/dist/vendor/scanner/dist/src/patterns/detectors/python-source.js +1022 -0
  125. package/dist/vendor/scanner/dist/src/patterns/detectors/python.d.ts +6 -0
  126. package/dist/vendor/scanner/dist/src/patterns/detectors/python.js +49 -1
  127. package/dist/vendor/scanner/dist/src/patterns/detectors/repo-declared-domains.d.ts +2 -0
  128. package/dist/vendor/scanner/dist/src/patterns/detectors/repo-declared-domains.js +80 -0
  129. package/dist/vendor/scanner/dist/src/patterns/detectors/source-module-index.d.ts +23 -0
  130. package/dist/vendor/scanner/dist/src/patterns/detectors/source-module-index.js +145 -0
  131. package/dist/vendor/scanner/dist/src/patterns/detectors/typescript-outbound-http.d.ts +30 -0
  132. package/dist/vendor/scanner/dist/src/patterns/detectors/typescript-outbound-http.js +575 -0
  133. package/dist/vendor/scanner/dist/src/patterns/detectors/typescript.js +12 -0
  134. package/dist/vendor/scanner/dist/src/patterns/engine.js +6 -1
  135. package/dist/vendor/scanner/package.json +3 -1
  136. package/dist/vendor/scanner/patterns/classifier/third-party.classifier.yaml +198 -0
  137. package/dist/vendor/scanner/patterns/component-taxonomy.yaml +1 -1
  138. package/dist/vendor/scanner/patterns/python.patterns.yaml +10 -0
  139. package/dist/vendor/scanner/patterns/third-party.patterns.yaml +271 -0
  140. package/dist/vendor/scanner/patterns/typescript.patterns.yaml +21 -0
  141. package/package.json +4 -3
@@ -0,0 +1,370 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.baseKey = baseKey;
4
+ exports.matchByKey = matchByKey;
5
+ exports.rewritePaths = rewritePaths;
6
+ exports.compareScans = compareScans;
7
+ const canonical_1 = require("./canonical");
8
+ const change_set_1 = require("./change-set");
9
+ const comparable_1 = require("./comparable");
10
+ const input_1 = require("./input");
11
+ /** Properties compared on matched elements (DATAP-851). */
12
+ const COMPONENT_PROPERTIES = ["name", "type", "subType", "confidence"];
13
+ const FLOW_PROPERTIES = ["source", "target", "type", "targetScope", "confidence"];
14
+ const DATA_ITEM_PROPERTIES = ["labels"];
15
+ const GROUP_PROPERTIES = ["label", "basis", "handledBy"];
16
+ function compareStrings(a, b) {
17
+ return a < b ? -1 : a > b ? 1 : 0;
18
+ }
19
+ /** The key without its `#n` duplicate marker. */
20
+ function baseKey(element) {
21
+ return element.duplicateIndex ? element.key.slice(0, -`#${element.duplicateIndex}`.length) : element.key;
22
+ }
23
+ /** What an element says, without its key or duplicate number. */
24
+ function content(element) {
25
+ const { key: _key, duplicateIndex: _n, previousKey: _p, ...rest } = element;
26
+ return (0, canonical_1.canonicalJson)(rest);
27
+ }
28
+ /**
29
+ * Matches elements by base key (DATAP-853). Elements that share a base key are compared as one
30
+ * group, never by their duplicate number, because that number follows source position: within a
31
+ * group, identical elements match first, then the rest pair up in duplicate order, and any extra
32
+ * element is added or removed.
33
+ */
34
+ function matchByKey(base, head) {
35
+ const groups = new Map();
36
+ const groupFor = (key) => {
37
+ let group = groups.get(key);
38
+ if (!group)
39
+ groups.set(key, (group = { base: [], head: [] }));
40
+ return group;
41
+ };
42
+ for (const element of base)
43
+ groupFor(baseKey(element)).base.push(element);
44
+ for (const element of head)
45
+ groupFor(baseKey(element)).head.push(element);
46
+ const result = { pairs: [], removed: [], added: [] };
47
+ const byDuplicate = (a, b) => (a.duplicateIndex ?? 1) - (b.duplicateIndex ?? 1);
48
+ for (const key of [...groups.keys()].sort(compareStrings)) {
49
+ const group = groups.get(key);
50
+ const restBase = [...group.base].sort(byDuplicate);
51
+ const restHead = [...group.head].sort(byDuplicate);
52
+ if (restBase.length > 1 || restHead.length > 1) {
53
+ for (let i = 0; i < restBase.length;) {
54
+ const j = restHead.findIndex((h) => content(h) === content(restBase[i]));
55
+ if (j === -1) {
56
+ i += 1;
57
+ continue;
58
+ }
59
+ result.pairs.push([restBase[i], restHead[j]]);
60
+ restBase.splice(i, 1);
61
+ restHead.splice(j, 1);
62
+ }
63
+ }
64
+ const paired = Math.min(restBase.length, restHead.length);
65
+ for (let i = 0; i < paired; i += 1)
66
+ result.pairs.push([restBase[i], restHead[i]]);
67
+ result.removed.push(...restBase.slice(paired));
68
+ result.added.push(...restHead.slice(paired));
69
+ }
70
+ return result;
71
+ }
72
+ function propertyChanges(before, after, properties) {
73
+ return properties.flatMap((property) => {
74
+ const a = before[property] ?? null;
75
+ const b = after[property] ?? null;
76
+ return (0, canonical_1.canonicalJson)(a) === (0, canonical_1.canonicalJson)(b) ? [] : [{ property, before: a, after: b }];
77
+ });
78
+ }
79
+ function changeId(change, subject, key, parent, previousKey) {
80
+ return (0, canonical_1.sha256)((0, canonical_1.canonicalJson)({ change, subject, key, parent, previousKey })).slice(0, 16);
81
+ }
82
+ function makeChange(change, subject, key, name, extra = {}, properties) {
83
+ return {
84
+ changeId: changeId(change, subject, key, extra.parent, extra.previousKey),
85
+ change,
86
+ subject,
87
+ key,
88
+ name,
89
+ ...extra,
90
+ ...(properties && properties.length > 0 ? { properties } : {}),
91
+ };
92
+ }
93
+ const escapeRegExp = (text) => text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
94
+ /**
95
+ * Rewrites renamed file paths in a key or reference. A path matches only as a whole segment of the
96
+ * key: between the separators keys use (`|`, `"`, `{`, `}`, `@`, `#`, `,`, `[`, `]`) or the ends.
97
+ */
98
+ function rewritePaths(text, renames) {
99
+ let out = text;
100
+ for (const { from, to } of renames) {
101
+ if (!out.includes(from))
102
+ continue;
103
+ out = out.replace(new RegExp(`(^|[|"{@,\\[])${escapeRegExp(from)}(?=$|[|"}#,\\]])`, "g"), `$1${to}`);
104
+ }
105
+ return out;
106
+ }
107
+ const withSuffix = (key, duplicateIndex) => (duplicateIndex ? `${key}#${duplicateIndex}` : key);
108
+ const stripSuffix = (key, duplicateIndex) => (duplicateIndex ? key.slice(0, -`#${duplicateIndex}`.length) : key);
109
+ /**
110
+ * The base scan as it would key itself after the renames (DATAP-866). Each element's files are
111
+ * rewritten through the map, its first file is taken again (keys use the first sorted file), and
112
+ * its key is rebuilt from its identity parts the way the scanner builds it. Managed components and
113
+ * flows are rebuilt from the new keys of their components. Elements whose key changed keep the old
114
+ * one as `previousKey`. Without file lists (older scans), the key's own file is rewritten.
115
+ */
116
+ function applyRenames(scan, renames, overrides = new Map()) {
117
+ if (renames.length === 0 && overrides.size === 0)
118
+ return scan;
119
+ const renameFile = (file) => rewritePaths(file, renames);
120
+ const firstFile = (source, fallback) => {
121
+ const files = (source?.files ?? []).map(renameFile).sort(compareStrings);
122
+ return files[0] ?? renameFile(fallback);
123
+ };
124
+ // Components: app keys first, then managed keys, which embed their provider's base key.
125
+ const unique = new Map();
126
+ const base = new Map();
127
+ const componentKey = (c) => {
128
+ const source = scan.keySources.components[c.key];
129
+ const p = source?.parts;
130
+ if (!p)
131
+ return rewritePaths(c.key, renames);
132
+ if (p.scheme === "app")
133
+ return withSuffix(`app:${p.section}|${p.type}|${p.subType}|${firstFile(source, p.file ?? "")}|${p.name}`, c.duplicateIndex);
134
+ if (p.scheme === "managed") {
135
+ const provider = base.get(p.provider) ?? rewritePaths(p.provider, renames);
136
+ return withSuffix(`managed:{${provider}}|${p.serviceKey}|${p.section}`, c.duplicateIndex);
137
+ }
138
+ return c.key;
139
+ };
140
+ const scheme = (c) => scan.keySources.components[c.key]?.parts.scheme;
141
+ for (const c of [...scan.components].sort((a, b) => Number(scheme(a) === "managed") - Number(scheme(b) === "managed"))) {
142
+ // A component matched by kind and name (DATAP-867) takes its head key.
143
+ const key = overrides.get(c.key) ?? componentKey(c);
144
+ unique.set(c.key, key);
145
+ base.set(stripSuffix(c.key, c.duplicateIndex), stripSuffix(key, c.duplicateIndex));
146
+ }
147
+ const ref = (key) => unique.get(key) ?? rewritePaths(key, renames);
148
+ const rekeyed = (element, key, extra = {}) => key === element.key ? { ...element, ...extra } : { ...element, ...extra, key, previousKey: element.key };
149
+ return {
150
+ ...scan,
151
+ components: scan.components.map((c) => rekeyed(c, unique.get(c.key) ?? c.key)),
152
+ dataFlows: scan.dataFlows.map((f) => {
153
+ const source = scan.keySources.dataFlows[f.key];
154
+ const p = source?.parts;
155
+ const key = p
156
+ ? withSuffix(JSON.stringify(["flow/v1", ref(p.source), ref(p.target), p.type, p.method, p.endpoint, firstFile(source, p.file ?? "")]), f.duplicateIndex)
157
+ : rewritePaths(f.key, renames);
158
+ return rekeyed(f, key, { source: ref(f.source), target: ref(f.target) });
159
+ }),
160
+ dataItems: scan.dataItems.map((item) => ({
161
+ ...item,
162
+ groups: item.groups.map((g) => rekeyed(g, rewritePaths(g.key, renames), { handledBy: g.handledBy.map(ref).sort(compareStrings) })),
163
+ })),
164
+ };
165
+ }
166
+ function diffSubject(subject, base, head, properties, describe) {
167
+ const { pairs, removed, added } = matchByKey(base, head);
168
+ const changes = [];
169
+ for (const element of removed) {
170
+ const { name, ...extra } = describe(element);
171
+ changes.push(makeChange("removed", subject, element.previousKey ?? element.key, name, extra));
172
+ }
173
+ for (const element of added) {
174
+ const { name, ...extra } = describe(element);
175
+ changes.push(makeChange("added", subject, element.key, name, extra));
176
+ }
177
+ for (const [before, after] of pairs) {
178
+ const changed = propertyChanges(before, after, properties);
179
+ const { name, ...extra } = describe(after);
180
+ if (before.previousKey !== undefined) {
181
+ changes.push(makeChange("renamed", subject, after.key, name, { ...extra, previousKey: before.previousKey }, changed));
182
+ continue;
183
+ }
184
+ if (changed.length === 0)
185
+ continue;
186
+ changes.push(makeChange("modified", subject, after.key, name, extra, changed));
187
+ }
188
+ return changes;
189
+ }
190
+ /**
191
+ * Matches components that are still unmatched after key and rename matching, by kind and name
192
+ * (DATAP-867): the same type, subtype and normalized name, in the same section, or in a section
193
+ * that is new in the head (a renamed package or service folder). No similarity scores. Only
194
+ * one-to-one candidates match; anything else stays removed or added. A component with the same type
195
+ * and subtype and exactly the same files (after renames) also matches when only its name differs
196
+ * (DATAP-870): the scanner names some services after their folder. Returns base key → head key, by
197
+ * the base's original key.
198
+ */
199
+ function matchByKindAndName(original, rekeyed, head, renames) {
200
+ const { removed, added } = matchByKey(rekeyed.components, head.components);
201
+ const describe = (scan, sourceKey, key, c, rename) => {
202
+ const source = scan.keySources.components[sourceKey];
203
+ const parts = source?.parts;
204
+ const files = (source?.files ?? []).map((f) => (rename ? rewritePaths(f, renames) : f)).sort(compareStrings);
205
+ const kind = `${c.type}|${c.subType}`;
206
+ return { key, kind, kindName: `${kind}|${parts?.name ?? c.name.trim().toLowerCase()}`, files: files.length > 0 ? JSON.stringify(files) : "", section: parts?.section ?? "" };
207
+ };
208
+ const baseSections = new Set(original.components.map((c) => original.keySources.components[c.key]?.parts.section ?? ""));
209
+ // Base side keyed by its original key; head side by its key.
210
+ const bases = removed.map((r) => describe(original, r.previousKey ?? r.key, r.previousKey ?? r.key, r, true));
211
+ const heads = added.map((a) => describe(head, a.key, a.key, a, false));
212
+ const sameFiles = (b, h) => b.kind === h.kind && b.files !== "" && b.files === h.files;
213
+ const fits = (b, h) => (b.kindName === h.kindName || sameFiles(b, h)) && (b.section === h.section || !baseSections.has(h.section));
214
+ /** Candidates for one element; a candidate in the same section wins over one in a new section. */
215
+ const pick = (all, section) => {
216
+ const same = all.filter((x) => x.section === section);
217
+ return same.length > 0 ? same : all;
218
+ };
219
+ const overrides = new Map();
220
+ for (const b of bases) {
221
+ const forward = pick(heads.filter((h) => fits(b, h)), b.section);
222
+ if (forward.length !== 1)
223
+ continue;
224
+ const back = pick(bases.filter((x) => fits(x, forward[0])), forward[0].section);
225
+ if (back.length === 1 && back[0] === b)
226
+ overrides.set(b.key, forward[0].key);
227
+ }
228
+ return overrides;
229
+ }
230
+ /**
231
+ * Finds splits and merges of components (DATAP-870). A base component and a head component are
232
+ * linked when they have the same type, subtype and normalized name, share at least one file (after
233
+ * renames), and are in the same section or in a section that the other scan does not have. Pairs
234
+ * that already match by key are linked too. A group of linked components with one base component and
235
+ * several head components is a split; several base components and one head component is a merge.
236
+ * Only groups that contain an added or removed component count. Other shapes are not recorded.
237
+ */
238
+ function findSplits(original, rekeyed, head, renames) {
239
+ const side = (prefix, scan, sourceKey, key, c, rename, open) => {
240
+ const source = scan.keySources.components[sourceKey];
241
+ const name = source?.parts.name ?? c.name.trim().toLowerCase();
242
+ const files = (source?.files ?? []).map((f) => (rename ? rewritePaths(f, renames) : f));
243
+ return { id: `${prefix}${key}`, key, name, kindName: `${c.type}|${c.subType}|${name}`, section: source?.parts.section ?? "", files: new Set(files), open };
244
+ };
245
+ const { pairs, removed, added } = matchByKey(rekeyed.components, head.components);
246
+ const removedSet = new Set(removed);
247
+ const addedSet = new Set(added);
248
+ const bases = rekeyed.components.map((c) => side("b:", original, c.previousKey ?? c.key, c.previousKey ?? c.key, c, true, removedSet.has(c)));
249
+ const heads = head.components.map((c) => side("h:", head, c.key, c.key, c, false, addedSet.has(c)));
250
+ const baseOf = new Map(rekeyed.components.map((c, i) => [c, bases[i]]));
251
+ const headOf = new Map(head.components.map((c, i) => [c, heads[i]]));
252
+ const baseSections = new Set(bases.map((b) => b.section));
253
+ const headSections = new Set(heads.map((h) => h.section));
254
+ const linked = (b, h) => b.kindName === h.kindName &&
255
+ (b.section === h.section || !baseSections.has(h.section) || !headSections.has(b.section)) &&
256
+ [...b.files].some((f) => h.files.has(f));
257
+ const parent = new Map();
258
+ const find = (id) => {
259
+ const p = parent.get(id) ?? id;
260
+ if (p === id)
261
+ return id;
262
+ const root = find(p);
263
+ parent.set(id, root);
264
+ return root;
265
+ };
266
+ const union = (a, b) => parent.set(find(a), find(b));
267
+ for (const [b, h] of pairs)
268
+ union(baseOf.get(b).id, headOf.get(h).id);
269
+ for (const b of bases)
270
+ for (const h of heads)
271
+ if ((b.open || h.open) && linked(b, h))
272
+ union(b.id, h.id);
273
+ const groups = new Map();
274
+ const groupOf = (s) => {
275
+ const root = find(s.id);
276
+ let g = groups.get(root);
277
+ if (!g)
278
+ groups.set(root, (g = { bases: [], heads: [] }));
279
+ return g;
280
+ };
281
+ for (const b of bases)
282
+ groupOf(b).bases.push(b);
283
+ for (const h of heads)
284
+ groupOf(h).heads.push(h);
285
+ const splits = [];
286
+ for (const g of groups.values()) {
287
+ if (![...g.bases, ...g.heads].some((s) => s.open))
288
+ continue;
289
+ const kind = g.bases.length === 1 && g.heads.length > 1 ? "split" : g.bases.length > 1 && g.heads.length === 1 ? "merge" : null;
290
+ if (!kind)
291
+ continue;
292
+ const keys = (all) => all.map((s) => s.key).sort(compareStrings);
293
+ splits.push({ kind, subject: "component", name: g.bases[0].name, base: keys(g.bases), head: keys(g.heads) });
294
+ }
295
+ return splits.sort((a, b) => compareStrings(a.name, b.name) || compareStrings(a.base[0], b.base[0]));
296
+ }
297
+ function diffScans(base, head) {
298
+ const changes = [
299
+ ...diffSubject("component", base.components, head.components, COMPONENT_PROPERTIES, (c) => ({ name: c.name })),
300
+ ...diffSubject("dataFlow", base.dataFlows, head.dataFlows, FLOW_PROPERTIES, (f) => ({
301
+ name: `${f.type}`,
302
+ source: f.source,
303
+ target: f.target,
304
+ })),
305
+ ...diffSubject("dataItem", base.dataItems, head.dataItems, DATA_ITEM_PROPERTIES, (i) => ({ name: i.key })),
306
+ ];
307
+ // Groups of data items that are in both scans. An added or removed data item is one change;
308
+ // its groups are not listed again.
309
+ const headItems = new Map(head.dataItems.map((i) => [i.key, i]));
310
+ for (const item of base.dataItems) {
311
+ const after = headItems.get(item.key);
312
+ if (!after)
313
+ continue;
314
+ changes.push(...diffSubject("group", item.groups, after.groups, GROUP_PROPERTIES, (g) => ({ name: g.label, parent: item.key })));
315
+ }
316
+ const order = (subject) => change_set_1.SUBJECTS.indexOf(subject);
317
+ const changeOrder = ["removed", "added", "renamed", "modified"];
318
+ return changes.sort((a, b) => order(a.subject) - order(b.subject) ||
319
+ compareStrings(a.parent ?? "", b.parent ?? "") ||
320
+ compareStrings(a.key, b.key) ||
321
+ changeOrder.indexOf(a.change) - changeOrder.indexOf(b.change));
322
+ }
323
+ function unmatched(base, head) {
324
+ return [
325
+ ...base.withoutIdentity.map((w) => ({ side: "base", subject: w.subject, id: w.id, reason: "no_identity" })),
326
+ ...head.withoutIdentity.map((w) => ({ side: "head", subject: w.subject, id: w.id, reason: "no_identity" })),
327
+ ];
328
+ }
329
+ /** The base keyed as the head would key it: renamed paths first, then matches by kind and name. */
330
+ function rekeyBase(base, head, renames) {
331
+ const renamed = applyRenames(base, renames);
332
+ const overrides = matchByKindAndName(base, renamed, head, renames);
333
+ return overrides.size > 0 ? applyRenames(base, renames, overrides) : renamed;
334
+ }
335
+ /**
336
+ * Compares two scans of one repository (DATAP-847). `base` and `head` are parsed `facts.json`
337
+ * values. The result follows `changeSetSchema`. Throws `ComparisonRefused` when the pair cannot be
338
+ * compared (DATAP-877).
339
+ */
340
+ function compareScans(base, head, options = {}) {
341
+ const baseFacts = (0, input_1.parseFacts)(base);
342
+ const headFacts = (0, input_1.parseFacts)(head);
343
+ if (options.checkStamps !== false) {
344
+ const comparable = (0, comparable_1.checkComparable)(baseFacts, headFacts);
345
+ if (!comparable.comparable)
346
+ throw new comparable_1.ComparisonRefused(comparable.reason, comparable.message);
347
+ }
348
+ const baseScan = (0, canonical_1.canonicalize)(baseFacts);
349
+ const headScan = (0, canonical_1.canonicalize)(headFacts);
350
+ const baseHash = (0, canonical_1.modelHash)(baseScan);
351
+ const headHash = (0, canonical_1.modelHash)(headScan);
352
+ const unmatchedFacts = unmatched(baseScan, headScan);
353
+ // Facts without identity are not in the model hash, so equal hashes do not prove that nothing
354
+ // changed when either scan has them (scans from CLI 0.0.32 and earlier have no identity at all).
355
+ const unchanged = baseHash === headHash && unmatchedFacts.length === 0;
356
+ const renames = options.renames ?? [];
357
+ const rekeyed = unchanged ? baseScan : rekeyBase(baseScan, headScan, renames);
358
+ return {
359
+ kind: "dataparade_change_set",
360
+ version: 1,
361
+ base: { modelHash: baseHash },
362
+ head: { modelHash: headHash },
363
+ unchanged,
364
+ configChanged: options.configChanged ?? null,
365
+ renames: options.renames ? "applied" : "no_map",
366
+ changes: unchanged ? [] : diffScans(rekeyed, headScan),
367
+ splits: unchanged ? [] : findSplits(baseScan, rekeyed, headScan, renames),
368
+ unmatched: unmatchedFacts,
369
+ };
370
+ }
@@ -0,0 +1,40 @@
1
+ import { z } from "zod";
2
+ import { type CanonicalScan } from "./canonical";
3
+ import { type Subject } from "./change-set";
4
+ /**
5
+ * The evidence fingerprint of an element (DATAP-872): what an answer about the element was based
6
+ * on. It is stored with each answer, and the slot rules (DATAP-874) compare it with the element in
7
+ * a later scan to carry, reopen or retire the answer.
8
+ *
9
+ * Version 1 holds the identity key, the shape (type, subtype, target scope), the neighbours of a
10
+ * flow (source and target keys) and the groups a component handles. Method, endpoint, observed
11
+ * data categories, data actions and a code hash come later, when `facts.json` has them.
12
+ */
13
+ export declare const fingerprintSchema: z.ZodObject<{
14
+ version: z.ZodLiteral<1>;
15
+ subject: z.ZodEnum<{
16
+ component: "component";
17
+ dataFlow: "dataFlow";
18
+ dataItem: "dataItem";
19
+ group: "group";
20
+ }>;
21
+ key: z.ZodString;
22
+ shape: z.ZodObject<{
23
+ type: z.ZodNullable<z.ZodString>;
24
+ subType: z.ZodNullable<z.ZodString>;
25
+ targetScope: z.ZodNullable<z.ZodString>;
26
+ }, z.core.$strip>;
27
+ neighbours: z.ZodNullable<z.ZodObject<{
28
+ source: z.ZodString;
29
+ target: z.ZodString;
30
+ }, z.core.$strip>>;
31
+ handledGroups: z.ZodArray<z.ZodString>;
32
+ }, z.core.$strip>;
33
+ export type Fingerprint = z.infer<typeof fingerprintSchema>;
34
+ /** Fingerprints of every element with an identity in a canonical scan, by subject and key. */
35
+ export declare function fingerprintsOfScan(scan: CanonicalScan): Map<string, Fingerprint>;
36
+ /**
37
+ * The fingerprint of one element of a parsed `facts.json`, found by subject and identity key.
38
+ * Returns null when the scan has no such element.
39
+ */
40
+ export declare function fingerprintOf(facts: unknown, subject: Subject, key: string): Fingerprint | null;
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.fingerprintSchema = void 0;
4
+ exports.fingerprintsOfScan = fingerprintsOfScan;
5
+ exports.fingerprintOf = fingerprintOf;
6
+ const zod_1 = require("zod");
7
+ const canonical_1 = require("./canonical");
8
+ const change_set_1 = require("./change-set");
9
+ const input_1 = require("./input");
10
+ /**
11
+ * The evidence fingerprint of an element (DATAP-872): what an answer about the element was based
12
+ * on. It is stored with each answer, and the slot rules (DATAP-874) compare it with the element in
13
+ * a later scan to carry, reopen or retire the answer.
14
+ *
15
+ * Version 1 holds the identity key, the shape (type, subtype, target scope), the neighbours of a
16
+ * flow (source and target keys) and the groups a component handles. Method, endpoint, observed
17
+ * data categories, data actions and a code hash come later, when `facts.json` has them.
18
+ */
19
+ exports.fingerprintSchema = zod_1.z.object({
20
+ version: zod_1.z.literal(1),
21
+ subject: zod_1.z.enum(change_set_1.SUBJECTS),
22
+ /** `identity.key` of the element, with its `#n` when it has one. */
23
+ key: zod_1.z.string(),
24
+ /** Components: type and subtype. Flows: type and target scope. Null where it does not apply. */
25
+ shape: zod_1.z.object({
26
+ type: zod_1.z.string().nullable(),
27
+ subType: zod_1.z.string().nullable(),
28
+ targetScope: zod_1.z.string().nullable(),
29
+ }),
30
+ /** Flows only: the keys of the endpoints. Null for other subjects. */
31
+ neighbours: zod_1.z.object({ source: zod_1.z.string(), target: zod_1.z.string() }).nullable(),
32
+ /** Components: keys of the groups whose `handledBy` names the component, sorted. Empty for other subjects, or when there are none. */
33
+ handledGroups: zod_1.z.array(zod_1.z.string()),
34
+ });
35
+ const NO_SHAPE = { type: null, subType: null, targetScope: null };
36
+ function compareStrings(a, b) {
37
+ return a < b ? -1 : a > b ? 1 : 0;
38
+ }
39
+ /** Fingerprints of every element with an identity in a canonical scan, by subject and key. */
40
+ function fingerprintsOfScan(scan) {
41
+ const out = new Map();
42
+ const add = (fp) => out.set(`${fp.subject}\u0000${fp.key}`, fp);
43
+ const handled = new Map();
44
+ for (const item of scan.dataItems) {
45
+ for (const group of item.groups) {
46
+ for (const component of group.handledBy) {
47
+ const list = handled.get(component) ?? [];
48
+ list.push(group.key);
49
+ handled.set(component, list);
50
+ }
51
+ }
52
+ }
53
+ for (const c of scan.components) {
54
+ const key = c.key;
55
+ add({
56
+ version: 1,
57
+ subject: "component",
58
+ key,
59
+ shape: { type: c.type, subType: c.subType, targetScope: null },
60
+ neighbours: null,
61
+ handledGroups: [...new Set(handled.get(key) ?? [])].sort(compareStrings),
62
+ });
63
+ }
64
+ for (const f of scan.dataFlows) {
65
+ add({
66
+ version: 1,
67
+ subject: "dataFlow",
68
+ key: f.key,
69
+ shape: { type: f.type, subType: null, targetScope: f.targetScope },
70
+ neighbours: { source: f.source, target: f.target },
71
+ handledGroups: [],
72
+ });
73
+ }
74
+ for (const item of scan.dataItems) {
75
+ add({ version: 1, subject: "dataItem", key: item.key, shape: NO_SHAPE, neighbours: null, handledGroups: [] });
76
+ for (const g of item.groups) {
77
+ add({ version: 1, subject: "group", key: g.key, shape: NO_SHAPE, neighbours: null, handledGroups: [] });
78
+ }
79
+ }
80
+ return out;
81
+ }
82
+ /**
83
+ * The fingerprint of one element of a parsed `facts.json`, found by subject and identity key.
84
+ * Returns null when the scan has no such element.
85
+ */
86
+ function fingerprintOf(facts, subject, key) {
87
+ return fingerprintsOfScan((0, canonical_1.canonicalize)((0, input_1.parseFacts)(facts))).get(`${subject}\u0000${key}`) ?? null;
88
+ }
@@ -0,0 +1,15 @@
1
+ export { canonicalize, canonicalJson, confidenceBand, modelHash } from "./canonical";
2
+ export type { CanonicalScan, ConfidenceBand } from "./canonical";
3
+ export { changeSetSchema, splitSchema, SUBJECTS } from "./change-set";
4
+ export type { Change, ChangeSet, PropertyChange, Split, Subject, Unmatched } from "./change-set";
5
+ export { checkComparable, ComparisonRefused, REFUSAL_REASONS } from "./comparable";
6
+ export type { Comparability, RefusalReason } from "./comparable";
7
+ export { baseKey, compareScans, matchByKey, rewritePaths } from "./compare";
8
+ export type { CompareOptions, Rename } from "./compare";
9
+ export { factsInputSchema, parseFacts } from "./input";
10
+ export type { FactsInput } from "./input";
11
+ export { summarizeChangeSet } from "./summary";
12
+ export { fingerprintOf, fingerprintSchema, fingerprintsOfScan } from "./fingerprint";
13
+ export type { Fingerprint } from "./fingerprint";
14
+ export { applySlotRules, carryInputSchema, SYSTEM_KEY } from "./slot-rules";
15
+ export type { CarryDecision, CarryInput, CarryReason } from "./slot-rules";
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SYSTEM_KEY = exports.carryInputSchema = exports.applySlotRules = exports.fingerprintsOfScan = exports.fingerprintSchema = exports.fingerprintOf = exports.summarizeChangeSet = exports.parseFacts = exports.factsInputSchema = exports.rewritePaths = exports.matchByKey = exports.compareScans = exports.baseKey = exports.REFUSAL_REASONS = exports.ComparisonRefused = exports.checkComparable = exports.SUBJECTS = exports.splitSchema = exports.changeSetSchema = exports.modelHash = exports.confidenceBand = exports.canonicalJson = exports.canonicalize = void 0;
4
+ var canonical_1 = require("./canonical");
5
+ Object.defineProperty(exports, "canonicalize", { enumerable: true, get: function () { return canonical_1.canonicalize; } });
6
+ Object.defineProperty(exports, "canonicalJson", { enumerable: true, get: function () { return canonical_1.canonicalJson; } });
7
+ Object.defineProperty(exports, "confidenceBand", { enumerable: true, get: function () { return canonical_1.confidenceBand; } });
8
+ Object.defineProperty(exports, "modelHash", { enumerable: true, get: function () { return canonical_1.modelHash; } });
9
+ var change_set_1 = require("./change-set");
10
+ Object.defineProperty(exports, "changeSetSchema", { enumerable: true, get: function () { return change_set_1.changeSetSchema; } });
11
+ Object.defineProperty(exports, "splitSchema", { enumerable: true, get: function () { return change_set_1.splitSchema; } });
12
+ Object.defineProperty(exports, "SUBJECTS", { enumerable: true, get: function () { return change_set_1.SUBJECTS; } });
13
+ var comparable_1 = require("./comparable");
14
+ Object.defineProperty(exports, "checkComparable", { enumerable: true, get: function () { return comparable_1.checkComparable; } });
15
+ Object.defineProperty(exports, "ComparisonRefused", { enumerable: true, get: function () { return comparable_1.ComparisonRefused; } });
16
+ Object.defineProperty(exports, "REFUSAL_REASONS", { enumerable: true, get: function () { return comparable_1.REFUSAL_REASONS; } });
17
+ var compare_1 = require("./compare");
18
+ Object.defineProperty(exports, "baseKey", { enumerable: true, get: function () { return compare_1.baseKey; } });
19
+ Object.defineProperty(exports, "compareScans", { enumerable: true, get: function () { return compare_1.compareScans; } });
20
+ Object.defineProperty(exports, "matchByKey", { enumerable: true, get: function () { return compare_1.matchByKey; } });
21
+ Object.defineProperty(exports, "rewritePaths", { enumerable: true, get: function () { return compare_1.rewritePaths; } });
22
+ var input_1 = require("./input");
23
+ Object.defineProperty(exports, "factsInputSchema", { enumerable: true, get: function () { return input_1.factsInputSchema; } });
24
+ Object.defineProperty(exports, "parseFacts", { enumerable: true, get: function () { return input_1.parseFacts; } });
25
+ var summary_1 = require("./summary");
26
+ Object.defineProperty(exports, "summarizeChangeSet", { enumerable: true, get: function () { return summary_1.summarizeChangeSet; } });
27
+ var fingerprint_1 = require("./fingerprint");
28
+ Object.defineProperty(exports, "fingerprintOf", { enumerable: true, get: function () { return fingerprint_1.fingerprintOf; } });
29
+ Object.defineProperty(exports, "fingerprintSchema", { enumerable: true, get: function () { return fingerprint_1.fingerprintSchema; } });
30
+ Object.defineProperty(exports, "fingerprintsOfScan", { enumerable: true, get: function () { return fingerprint_1.fingerprintsOfScan; } });
31
+ var slot_rules_1 = require("./slot-rules");
32
+ Object.defineProperty(exports, "applySlotRules", { enumerable: true, get: function () { return slot_rules_1.applySlotRules; } });
33
+ Object.defineProperty(exports, "carryInputSchema", { enumerable: true, get: function () { return slot_rules_1.carryInputSchema; } });
34
+ Object.defineProperty(exports, "SYSTEM_KEY", { enumerable: true, get: function () { return slot_rules_1.SYSTEM_KEY; } });