knodin 0.14.1 → 0.14.2
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.
- package/dist/bin/cli.js +28 -18
- package/dist/src/agent-events.js +34 -1
- package/dist/src/agent-hooks.js +33 -9
- package/dist/src/cli-model.js +1 -1
- package/dist/src/engine/historical-retrieval-eval.js +412 -0
- package/dist/src/engine/index.js +326 -15
- package/dist/src/engine/parse-pool-resources.js +63 -7
- package/dist/src/engine/retrieval-governance-eval.js +278 -0
- package/dist/src/engine/semantic-graph-delta.js +655 -0
- package/dist/src/engine/target-uncertainty.js +1068 -0
- package/dist/src/progressive-evidence.js +39 -16
- package/dist/src/session-telemetry.js +25 -0
- package/dist/src/tools/knodin-tools.js +2 -0
- package/docs/releases/0.14.2.md +72 -0
- package/package.json +2 -1
- package/roadmap/competitive-roadmap.md +21 -9
|
@@ -0,0 +1,655 @@
|
|
|
1
|
+
import { execFileSync } from "node:child_process";
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
|
+
import fs from "node:fs";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
function sha256(value) {
|
|
6
|
+
return createHash("sha256").update(value).digest("hex");
|
|
7
|
+
}
|
|
8
|
+
export function buildStableSymbolIdentity(filePath, kind, symbol, disambiguator = "") {
|
|
9
|
+
const suffix = disambiguator ? `:${disambiguator}` : "";
|
|
10
|
+
return `sym_${sha256(`${filePath}:${kind}:${symbol}${suffix}`).slice(0, 24)}`;
|
|
11
|
+
}
|
|
12
|
+
function normalizeBodyForRenameMatch(source, symbolName) {
|
|
13
|
+
const withoutComments = source.replace(/\/\*[\s\S]*?\*\//g, " ").replace(/\/\/.*$/gm, " ");
|
|
14
|
+
const escapedName = symbolName.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
15
|
+
const selfReplaced = escapedName
|
|
16
|
+
? withoutComments.replace(new RegExp(String.raw `\b${escapedName}\b`, "g"), "__SELF__")
|
|
17
|
+
: withoutComments;
|
|
18
|
+
const openParen = selfReplaced.indexOf("(");
|
|
19
|
+
const openBrace = selfReplaced.indexOf("{");
|
|
20
|
+
const signaturePart = openParen >= 0 && openBrace > openParen
|
|
21
|
+
? selfReplaced.slice(openParen, openBrace).replace(/\s+/g, " ").trim()
|
|
22
|
+
: "";
|
|
23
|
+
const bodyPart = openBrace >= 0
|
|
24
|
+
? selfReplaced.slice(openBrace).replace(/\s+/g, " ").trim()
|
|
25
|
+
: selfReplaced.replace(/\s+/g, " ").trim();
|
|
26
|
+
const tokenMatches = bodyPart.match(/[A-Za-z_$][\w$]*|\d+|==|!=|<=|>=|=>|[+\-*/%<>]=?/g) ?? [];
|
|
27
|
+
const reserved = new Set([
|
|
28
|
+
"function",
|
|
29
|
+
"return",
|
|
30
|
+
"const",
|
|
31
|
+
"let",
|
|
32
|
+
"var",
|
|
33
|
+
"if",
|
|
34
|
+
"else",
|
|
35
|
+
"for",
|
|
36
|
+
"while",
|
|
37
|
+
"switch",
|
|
38
|
+
"case",
|
|
39
|
+
"break",
|
|
40
|
+
"continue",
|
|
41
|
+
"new",
|
|
42
|
+
"typeof",
|
|
43
|
+
"instanceof",
|
|
44
|
+
"async",
|
|
45
|
+
"await",
|
|
46
|
+
"export",
|
|
47
|
+
"import",
|
|
48
|
+
"from",
|
|
49
|
+
"class",
|
|
50
|
+
"interface",
|
|
51
|
+
"type",
|
|
52
|
+
"true",
|
|
53
|
+
"false",
|
|
54
|
+
"null",
|
|
55
|
+
"undefined",
|
|
56
|
+
"__SELF__",
|
|
57
|
+
]);
|
|
58
|
+
const bodyTokens = tokenMatches.filter((tok) => !reserved.has(tok));
|
|
59
|
+
const callees = [];
|
|
60
|
+
const callRegex = /\b([A-Za-z_$][\w$]*)\s*\(/g;
|
|
61
|
+
let match = callRegex.exec(bodyPart);
|
|
62
|
+
while (match !== null) {
|
|
63
|
+
const callee = match[1];
|
|
64
|
+
if (callee && !reserved.has(callee)) {
|
|
65
|
+
callees.push(callee);
|
|
66
|
+
}
|
|
67
|
+
match = callRegex.exec(bodyPart);
|
|
68
|
+
}
|
|
69
|
+
return {
|
|
70
|
+
signatureHash: sha256(signaturePart),
|
|
71
|
+
bodyHash: sha256(bodyPart),
|
|
72
|
+
bodyTokens,
|
|
73
|
+
callees: [...new Set(callees)].sort(),
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Lightweight deterministic TypeScript/JavaScript source-to-snapshot extractor
|
|
78
|
+
* for before/after semantic graph comparison without requiring external dependencies.
|
|
79
|
+
*/
|
|
80
|
+
export function extractSemanticSnapshotFromFiles(revision, files, options) {
|
|
81
|
+
const sortedPaths = Object.keys(files).sort();
|
|
82
|
+
const digestHash = createHash("sha256");
|
|
83
|
+
const nodes = [];
|
|
84
|
+
const edges = [];
|
|
85
|
+
const prefix = options?.nodeIdPrefix ?? revision;
|
|
86
|
+
const declRegex = /^(?:\s*export\s+)?(?:\s*default\s+)?(?:\s*async\s+)?(function|class|interface|type|enum|const|let|var)\s+([A-Za-z_$][\w$]*)/;
|
|
87
|
+
for (const filePath of sortedPaths) {
|
|
88
|
+
const content = files[filePath] ?? "";
|
|
89
|
+
digestHash.update(`${filePath}\0${content}\0`);
|
|
90
|
+
const lines = content.split(/\r?\n/);
|
|
91
|
+
const labelCounts = new Map();
|
|
92
|
+
for (let i = 0; i < lines.length; i++) {
|
|
93
|
+
const line = lines[i] ?? "";
|
|
94
|
+
const m = declRegex.exec(line);
|
|
95
|
+
if (!m)
|
|
96
|
+
continue;
|
|
97
|
+
const rawKind = m[1] ?? "function";
|
|
98
|
+
const symbol = m[2] ?? "";
|
|
99
|
+
if (!symbol)
|
|
100
|
+
continue;
|
|
101
|
+
const kind = rawKind === "const" || rawKind === "let" || rawKind === "var" ? "variable" : rawKind;
|
|
102
|
+
let depth = 0;
|
|
103
|
+
let sawBrace = false;
|
|
104
|
+
let endIdx = i;
|
|
105
|
+
for (let cursor = i; cursor < lines.length; cursor++) {
|
|
106
|
+
const text = lines[cursor] ?? "";
|
|
107
|
+
for (const ch of text) {
|
|
108
|
+
if (ch === "{") {
|
|
109
|
+
depth++;
|
|
110
|
+
sawBrace = true;
|
|
111
|
+
}
|
|
112
|
+
else if (ch === "}") {
|
|
113
|
+
depth--;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
endIdx = cursor;
|
|
117
|
+
if (sawBrace && depth <= 0)
|
|
118
|
+
break;
|
|
119
|
+
if (!sawBrace && text.trimEnd().endsWith(";"))
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
122
|
+
const occurrence = (labelCounts.get(`${kind}:${symbol}`) ?? 0) + 1;
|
|
123
|
+
labelCounts.set(`${kind}:${symbol}`, occurrence);
|
|
124
|
+
const disambiguator = occurrence > 1 ? `occ${occurrence}@L${i + 1}` : "";
|
|
125
|
+
const identity = buildStableSymbolIdentity(filePath, kind, symbol, disambiguator);
|
|
126
|
+
const spanText = lines.slice(i, endIdx + 1).join("\n");
|
|
127
|
+
const normalized = normalizeBodyForRenameMatch(spanText, symbol);
|
|
128
|
+
const node = {
|
|
129
|
+
nodeId: `${prefix}:${filePath}:${i + 1}:${symbol}`,
|
|
130
|
+
identity,
|
|
131
|
+
symbol,
|
|
132
|
+
kind,
|
|
133
|
+
filePath,
|
|
134
|
+
startLine: i + 1,
|
|
135
|
+
endLine: endIdx + 1,
|
|
136
|
+
signatureHash: normalized.signatureHash,
|
|
137
|
+
bodyHash: normalized.bodyHash,
|
|
138
|
+
bodyTokens: normalized.bodyTokens,
|
|
139
|
+
callees: normalized.callees,
|
|
140
|
+
};
|
|
141
|
+
nodes.push(node);
|
|
142
|
+
for (const callee of normalized.callees) {
|
|
143
|
+
edges.push({
|
|
144
|
+
callerIdentity: identity,
|
|
145
|
+
callerSymbol: symbol,
|
|
146
|
+
callerFile: filePath,
|
|
147
|
+
calleeSymbol: callee,
|
|
148
|
+
line: i + 1,
|
|
149
|
+
kind: "call",
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
// Resolve intra-snapshot calleeIdentity where unambiguous
|
|
155
|
+
for (const edge of edges) {
|
|
156
|
+
const candidates = nodes.filter((n) => n.symbol === edge.calleeSymbol);
|
|
157
|
+
if (candidates.length === 1 && candidates[0]) {
|
|
158
|
+
edge.calleeIdentity = candidates[0].identity;
|
|
159
|
+
edge.calleeFile = candidates[0].filePath;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return {
|
|
163
|
+
revision,
|
|
164
|
+
sourceDigest: digestHash.digest("hex"),
|
|
165
|
+
nodes,
|
|
166
|
+
edges,
|
|
167
|
+
...(options?.unknownFiles ? { unknownFiles: options.unknownFiles } : {}),
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
function jaccardOverlap(left, right) {
|
|
171
|
+
if (left.length === 0 || right.length === 0)
|
|
172
|
+
return 0;
|
|
173
|
+
const leftSet = new Set(left);
|
|
174
|
+
const rightSet = new Set(right);
|
|
175
|
+
let intersection = 0;
|
|
176
|
+
for (const item of leftSet) {
|
|
177
|
+
if (rightSet.has(item))
|
|
178
|
+
intersection++;
|
|
179
|
+
}
|
|
180
|
+
const union = new Set([...leftSet, ...rightSet]).size;
|
|
181
|
+
return union > 0 ? intersection / union : 0;
|
|
182
|
+
}
|
|
183
|
+
export function computeSemanticGraphDelta(before, after, options) {
|
|
184
|
+
const startedAt = performance.now();
|
|
185
|
+
const entityDeltas = [];
|
|
186
|
+
const edgeDeltas = [];
|
|
187
|
+
const omissions = [];
|
|
188
|
+
const unknowns = [];
|
|
189
|
+
for (const file of after.unknownFiles ?? []) {
|
|
190
|
+
omissions.push({
|
|
191
|
+
filePath: file,
|
|
192
|
+
reason: "unindexed or binary/unsupported file excluded from semantic graph alignment",
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
const matchedBefore = new Set();
|
|
196
|
+
const matchedAfter = new Set();
|
|
197
|
+
const alignedBeforeToAfter = new Map();
|
|
198
|
+
// 1. Detect ambiguous same-label nodes first so we NEVER silently merge by shared labels!
|
|
199
|
+
const beforeByLabel = new Map();
|
|
200
|
+
const afterByLabel = new Map();
|
|
201
|
+
for (const node of before.nodes) {
|
|
202
|
+
const key = `${node.kind}:${node.symbol}`;
|
|
203
|
+
const list = beforeByLabel.get(key) ?? [];
|
|
204
|
+
list.push(node);
|
|
205
|
+
beforeByLabel.set(key, list);
|
|
206
|
+
}
|
|
207
|
+
for (const node of after.nodes) {
|
|
208
|
+
const key = `${node.kind}:${node.symbol}`;
|
|
209
|
+
const list = afterByLabel.get(key) ?? [];
|
|
210
|
+
list.push(node);
|
|
211
|
+
afterByLabel.set(key, list);
|
|
212
|
+
}
|
|
213
|
+
for (const [labelKey, beforeGroup] of beforeByLabel.entries()) {
|
|
214
|
+
const afterGroup = afterByLabel.get(labelKey) ?? [];
|
|
215
|
+
if (beforeGroup.length > 1 || afterGroup.length > 1) {
|
|
216
|
+
// Check if any nodes in beforeGroup/afterGroup have ambiguous alignment (e.g., different files or duplicate declarations without unique body+file match)
|
|
217
|
+
const canUniquelyPairAll = beforeGroup.length === afterGroup.length &&
|
|
218
|
+
beforeGroup.every((b) => afterGroup.filter((a) => a.filePath === b.filePath && a.bodyHash === b.bodyHash)
|
|
219
|
+
.length === 1);
|
|
220
|
+
if (!canUniquelyPairAll && beforeGroup.length > 0 && afterGroup.length > 0) {
|
|
221
|
+
for (const b of beforeGroup)
|
|
222
|
+
matchedBefore.add(b.identity);
|
|
223
|
+
for (const a of afterGroup)
|
|
224
|
+
matchedAfter.add(a.identity);
|
|
225
|
+
const sample = beforeGroup[0] ?? afterGroup[0];
|
|
226
|
+
if (sample) {
|
|
227
|
+
entityDeltas.push({
|
|
228
|
+
classification: "ambiguous-same-label",
|
|
229
|
+
beforeIdentity: beforeGroup[0]?.identity ?? null,
|
|
230
|
+
afterIdentity: afterGroup[0]?.identity ?? null,
|
|
231
|
+
beforeNodeId: beforeGroup[0]?.nodeId ?? null,
|
|
232
|
+
afterNodeId: afterGroup[0]?.nodeId ?? null,
|
|
233
|
+
beforeSymbol: sample.symbol,
|
|
234
|
+
afterSymbol: sample.symbol,
|
|
235
|
+
kind: sample.kind,
|
|
236
|
+
beforeFile: beforeGroup[0]?.filePath ?? null,
|
|
237
|
+
afterFile: afterGroup[0]?.filePath ?? null,
|
|
238
|
+
semanticallyUnchanged: false,
|
|
239
|
+
isSuggestion: false,
|
|
240
|
+
confidence: "heuristic",
|
|
241
|
+
uncertainty: "ambiguous",
|
|
242
|
+
uncertaintyReason: `multiple (${beforeGroup.length} before, ${afterGroup.length} after) entities share label "${sample.symbol}" without unique structural identity alignment`,
|
|
243
|
+
candidateBeforeIdentities: beforeGroup.map((n) => n.identity),
|
|
244
|
+
candidateAfterIdentities: afterGroup.map((n) => n.identity),
|
|
245
|
+
evidence: `${sample.kind} ${sample.symbol} appears across [${beforeGroup.map((n) => `${n.filePath}:${n.startLine}`).join(", ")}] -> [${afterGroup.map((n) => `${n.filePath}:${n.startLine}`).join(", ")}]`,
|
|
246
|
+
});
|
|
247
|
+
unknowns.push({
|
|
248
|
+
symbol: sample.symbol,
|
|
249
|
+
filePath: afterGroup[0]?.filePath ?? beforeGroup[0]?.filePath ?? "",
|
|
250
|
+
reason: "ambiguous-same-label entities cannot be merged by shared label alone",
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
// 2. Match same-symbol, same-file, same-kind entities (distinguishing harmless id-churn vs modified)
|
|
257
|
+
for (const bNode of before.nodes) {
|
|
258
|
+
if (matchedBefore.has(bNode.identity))
|
|
259
|
+
continue;
|
|
260
|
+
const exactAfter = after.nodes.find((aNode) => !matchedAfter.has(aNode.identity) &&
|
|
261
|
+
aNode.filePath === bNode.filePath &&
|
|
262
|
+
aNode.kind === bNode.kind &&
|
|
263
|
+
aNode.symbol === bNode.symbol);
|
|
264
|
+
if (!exactAfter)
|
|
265
|
+
continue;
|
|
266
|
+
matchedBefore.add(bNode.identity);
|
|
267
|
+
matchedAfter.add(exactAfter.identity);
|
|
268
|
+
alignedBeforeToAfter.set(bNode.identity, exactAfter);
|
|
269
|
+
if (bNode.bodyHash === exactAfter.bodyHash) {
|
|
270
|
+
// Check if nodeId or line span shifted -> harmless ID churn!
|
|
271
|
+
if (bNode.nodeId !== exactAfter.nodeId ||
|
|
272
|
+
bNode.startLine !== exactAfter.startLine ||
|
|
273
|
+
bNode.endLine !== exactAfter.endLine) {
|
|
274
|
+
entityDeltas.push({
|
|
275
|
+
classification: "id-churn",
|
|
276
|
+
beforeIdentity: bNode.identity,
|
|
277
|
+
afterIdentity: exactAfter.identity,
|
|
278
|
+
beforeNodeId: bNode.nodeId,
|
|
279
|
+
afterNodeId: exactAfter.nodeId,
|
|
280
|
+
beforeSymbol: bNode.symbol,
|
|
281
|
+
afterSymbol: exactAfter.symbol,
|
|
282
|
+
kind: bNode.kind,
|
|
283
|
+
beforeFile: bNode.filePath,
|
|
284
|
+
afterFile: exactAfter.filePath,
|
|
285
|
+
semanticallyUnchanged: true,
|
|
286
|
+
isSuggestion: false,
|
|
287
|
+
confidence: "exact",
|
|
288
|
+
uncertainty: "exact",
|
|
289
|
+
evidence: `${bNode.filePath}:${bNode.startLine}->${exactAfter.startLine} (${bNode.symbol} bodyHash=${bNode.bodyHash.slice(0, 12)} unchanged despite node/line churn)`,
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
else {
|
|
294
|
+
entityDeltas.push({
|
|
295
|
+
classification: "modified",
|
|
296
|
+
beforeIdentity: bNode.identity,
|
|
297
|
+
afterIdentity: exactAfter.identity,
|
|
298
|
+
beforeNodeId: bNode.nodeId,
|
|
299
|
+
afterNodeId: exactAfter.nodeId,
|
|
300
|
+
beforeSymbol: bNode.symbol,
|
|
301
|
+
afterSymbol: exactAfter.symbol,
|
|
302
|
+
kind: bNode.kind,
|
|
303
|
+
beforeFile: bNode.filePath,
|
|
304
|
+
afterFile: exactAfter.filePath,
|
|
305
|
+
semanticallyUnchanged: false,
|
|
306
|
+
isSuggestion: false,
|
|
307
|
+
confidence: "exact",
|
|
308
|
+
uncertainty: "exact",
|
|
309
|
+
evidence: `${bNode.filePath}:${exactAfter.startLine} (${bNode.symbol} body modified)`,
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
// 3. Detect true renames among remaining unmatched before/after nodes
|
|
314
|
+
const unmatchedBefore = before.nodes.filter((n) => !matchedBefore.has(n.identity));
|
|
315
|
+
const unmatchedAfter = after.nodes.filter((n) => !matchedAfter.has(n.identity));
|
|
316
|
+
for (const bNode of unmatchedBefore) {
|
|
317
|
+
if (matchedBefore.has(bNode.identity))
|
|
318
|
+
continue;
|
|
319
|
+
const renameCandidates = unmatchedAfter.filter((aNode) => !matchedAfter.has(aNode.identity) &&
|
|
320
|
+
aNode.kind === bNode.kind &&
|
|
321
|
+
(aNode.bodyHash === bNode.bodyHash ||
|
|
322
|
+
(aNode.filePath === bNode.filePath &&
|
|
323
|
+
aNode.signatureHash === bNode.signatureHash &&
|
|
324
|
+
jaccardOverlap(bNode.bodyTokens, aNode.bodyTokens) >= 0.75)));
|
|
325
|
+
if (renameCandidates.length === 1 && renameCandidates[0]) {
|
|
326
|
+
const target = renameCandidates[0];
|
|
327
|
+
// Ensure no other unmatched before node competes for the same target
|
|
328
|
+
const competingBefore = unmatchedBefore.filter((other) => !matchedBefore.has(other.identity) &&
|
|
329
|
+
other.kind === target.kind &&
|
|
330
|
+
other.bodyHash === target.bodyHash);
|
|
331
|
+
if (competingBefore.length === 1) {
|
|
332
|
+
matchedBefore.add(bNode.identity);
|
|
333
|
+
matchedAfter.add(target.identity);
|
|
334
|
+
alignedBeforeToAfter.set(bNode.identity, target);
|
|
335
|
+
entityDeltas.push({
|
|
336
|
+
classification: "renamed",
|
|
337
|
+
beforeIdentity: bNode.identity,
|
|
338
|
+
afterIdentity: target.identity,
|
|
339
|
+
beforeNodeId: bNode.nodeId,
|
|
340
|
+
afterNodeId: target.nodeId,
|
|
341
|
+
beforeSymbol: bNode.symbol,
|
|
342
|
+
afterSymbol: target.symbol,
|
|
343
|
+
kind: bNode.kind,
|
|
344
|
+
beforeFile: bNode.filePath,
|
|
345
|
+
afterFile: target.filePath,
|
|
346
|
+
semanticallyUnchanged: false,
|
|
347
|
+
isSuggestion: false,
|
|
348
|
+
confidence: bNode.bodyHash === target.bodyHash ? "exact" : "heuristic",
|
|
349
|
+
uncertainty: bNode.bodyHash === target.bodyHash ? "exact" : "possible-set",
|
|
350
|
+
evidence: `${bNode.filePath}:${bNode.startLine} ${bNode.symbol} -> ${target.filePath}:${target.startLine} ${target.symbol}`,
|
|
351
|
+
});
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
// 4. Detect split/merge suggestions (never silently merged; marked isSuggestion: true, uncertainty: "possible-set")
|
|
356
|
+
const stillUnmatchedBefore = before.nodes.filter((n) => !matchedBefore.has(n.identity));
|
|
357
|
+
const stillUnmatchedAfter = after.nodes.filter((n) => !matchedAfter.has(n.identity));
|
|
358
|
+
for (const bNode of stillUnmatchedBefore) {
|
|
359
|
+
if (matchedBefore.has(bNode.identity) || bNode.bodyTokens.length < 2)
|
|
360
|
+
continue;
|
|
361
|
+
const sameFileAfter = stillUnmatchedAfter.filter((aNode) => !matchedAfter.has(aNode.identity) &&
|
|
362
|
+
aNode.filePath === bNode.filePath &&
|
|
363
|
+
aNode.kind === bNode.kind);
|
|
364
|
+
if (sameFileAfter.length >= 2) {
|
|
365
|
+
const contributing = sameFileAfter.filter((aNode) => {
|
|
366
|
+
const aTokens = new Set(aNode.bodyTokens);
|
|
367
|
+
const shared = bNode.bodyTokens.filter((t) => aTokens.has(t));
|
|
368
|
+
return shared.length >= 2;
|
|
369
|
+
});
|
|
370
|
+
if (contributing.length >= 2) {
|
|
371
|
+
matchedBefore.add(bNode.identity);
|
|
372
|
+
for (const c of contributing)
|
|
373
|
+
matchedAfter.add(c.identity);
|
|
374
|
+
entityDeltas.push({
|
|
375
|
+
classification: "split-suggestion",
|
|
376
|
+
beforeIdentity: bNode.identity,
|
|
377
|
+
afterIdentity: null,
|
|
378
|
+
beforeNodeId: bNode.nodeId,
|
|
379
|
+
afterNodeId: null,
|
|
380
|
+
beforeSymbol: bNode.symbol,
|
|
381
|
+
afterSymbol: contributing.map((c) => c.symbol).join(", "),
|
|
382
|
+
kind: bNode.kind,
|
|
383
|
+
beforeFile: bNode.filePath,
|
|
384
|
+
afterFile: bNode.filePath,
|
|
385
|
+
semanticallyUnchanged: false,
|
|
386
|
+
isSuggestion: true,
|
|
387
|
+
confidence: "heuristic",
|
|
388
|
+
uncertainty: "possible-set",
|
|
389
|
+
uncertaintyReason: "split candidate inferred from partitioned body tokens/callees; requires human or identity qualification",
|
|
390
|
+
relatedIdentities: contributing.map((c) => c.identity),
|
|
391
|
+
evidence: `${bNode.symbol} possibly split into [${contributing.map((c) => c.symbol).join(", ")}] in ${bNode.filePath}`,
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
for (const aNode of stillUnmatchedAfter) {
|
|
397
|
+
if (matchedAfter.has(aNode.identity) || aNode.bodyTokens.length < 2)
|
|
398
|
+
continue;
|
|
399
|
+
const sameFileBefore = stillUnmatchedBefore.filter((bNode) => !matchedBefore.has(bNode.identity) &&
|
|
400
|
+
bNode.filePath === aNode.filePath &&
|
|
401
|
+
bNode.kind === aNode.kind);
|
|
402
|
+
if (sameFileBefore.length >= 2) {
|
|
403
|
+
const contributing = sameFileBefore.filter((bNode) => {
|
|
404
|
+
const aTokens = new Set(aNode.bodyTokens);
|
|
405
|
+
const shared = bNode.bodyTokens.filter((t) => aTokens.has(t));
|
|
406
|
+
return shared.length >= 2;
|
|
407
|
+
});
|
|
408
|
+
if (contributing.length >= 2) {
|
|
409
|
+
matchedAfter.add(aNode.identity);
|
|
410
|
+
for (const c of contributing)
|
|
411
|
+
matchedBefore.add(c.identity);
|
|
412
|
+
entityDeltas.push({
|
|
413
|
+
classification: "merge-suggestion",
|
|
414
|
+
beforeIdentity: null,
|
|
415
|
+
afterIdentity: aNode.identity,
|
|
416
|
+
beforeNodeId: null,
|
|
417
|
+
afterNodeId: aNode.nodeId,
|
|
418
|
+
beforeSymbol: contributing.map((c) => c.symbol).join(", "),
|
|
419
|
+
afterSymbol: aNode.symbol,
|
|
420
|
+
kind: aNode.kind,
|
|
421
|
+
beforeFile: aNode.filePath,
|
|
422
|
+
afterFile: aNode.filePath,
|
|
423
|
+
semanticallyUnchanged: false,
|
|
424
|
+
isSuggestion: true,
|
|
425
|
+
confidence: "heuristic",
|
|
426
|
+
uncertainty: "possible-set",
|
|
427
|
+
uncertaintyReason: "merge candidate inferred from combined body tokens/callees; never silently merged",
|
|
428
|
+
relatedIdentities: contributing.map((c) => c.identity),
|
|
429
|
+
evidence: `[${contributing.map((c) => c.symbol).join(", ")}] possibly merged into ${aNode.symbol} in ${aNode.filePath}`,
|
|
430
|
+
});
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
// 5. Remaining unmatched nodes are added or removed
|
|
435
|
+
for (const bNode of before.nodes) {
|
|
436
|
+
if (matchedBefore.has(bNode.identity))
|
|
437
|
+
continue;
|
|
438
|
+
entityDeltas.push({
|
|
439
|
+
classification: "removed",
|
|
440
|
+
beforeIdentity: bNode.identity,
|
|
441
|
+
afterIdentity: null,
|
|
442
|
+
beforeNodeId: bNode.nodeId,
|
|
443
|
+
afterNodeId: null,
|
|
444
|
+
beforeSymbol: bNode.symbol,
|
|
445
|
+
afterSymbol: null,
|
|
446
|
+
kind: bNode.kind,
|
|
447
|
+
beforeFile: bNode.filePath,
|
|
448
|
+
afterFile: null,
|
|
449
|
+
semanticallyUnchanged: false,
|
|
450
|
+
isSuggestion: false,
|
|
451
|
+
confidence: "exact",
|
|
452
|
+
uncertainty: "exact",
|
|
453
|
+
evidence: `${bNode.filePath}:${bNode.startLine} ${bNode.symbol} removed`,
|
|
454
|
+
});
|
|
455
|
+
}
|
|
456
|
+
for (const aNode of after.nodes) {
|
|
457
|
+
if (matchedAfter.has(aNode.identity))
|
|
458
|
+
continue;
|
|
459
|
+
entityDeltas.push({
|
|
460
|
+
classification: "added",
|
|
461
|
+
beforeIdentity: null,
|
|
462
|
+
afterIdentity: aNode.identity,
|
|
463
|
+
beforeNodeId: null,
|
|
464
|
+
afterNodeId: aNode.nodeId,
|
|
465
|
+
beforeSymbol: null,
|
|
466
|
+
afterSymbol: aNode.symbol,
|
|
467
|
+
kind: aNode.kind,
|
|
468
|
+
beforeFile: null,
|
|
469
|
+
afterFile: aNode.filePath,
|
|
470
|
+
semanticallyUnchanged: false,
|
|
471
|
+
isSuggestion: false,
|
|
472
|
+
confidence: "exact",
|
|
473
|
+
uncertainty: "exact",
|
|
474
|
+
evidence: `${aNode.filePath}:${aNode.startLine} ${aNode.symbol} added`,
|
|
475
|
+
});
|
|
476
|
+
}
|
|
477
|
+
// 6. Detect edge retargeting across aligned callers
|
|
478
|
+
for (const [beforeCallerId, afterCallerNode] of alignedBeforeToAfter.entries()) {
|
|
479
|
+
const beforeCallerEdges = before.edges.filter((e) => e.callerIdentity === beforeCallerId);
|
|
480
|
+
const afterCallerEdges = after.edges.filter((e) => e.callerIdentity === afterCallerNode.identity);
|
|
481
|
+
const beforeTargets = new Map(beforeCallerEdges.map((e) => [e.calleeSymbol, e]));
|
|
482
|
+
const afterTargets = new Map(afterCallerEdges.map((e) => [e.calleeSymbol, e]));
|
|
483
|
+
const removedEdges = beforeCallerEdges.filter((e) => !afterTargets.has(e.calleeSymbol));
|
|
484
|
+
const addedEdges = afterCallerEdges.filter((e) => !beforeTargets.has(e.calleeSymbol));
|
|
485
|
+
// Also check if an edge kept the same calleeSymbol name or changed calleeSymbol, OR if its resolved calleeIdentity changed!
|
|
486
|
+
for (const bEdge of beforeCallerEdges) {
|
|
487
|
+
const sameNameAfter = afterTargets.get(bEdge.calleeSymbol);
|
|
488
|
+
if (sameNameAfter &&
|
|
489
|
+
bEdge.calleeIdentity &&
|
|
490
|
+
sameNameAfter.calleeIdentity &&
|
|
491
|
+
bEdge.calleeIdentity !== sameNameAfter.calleeIdentity) {
|
|
492
|
+
const alignedTarget = alignedBeforeToAfter.get(bEdge.calleeIdentity);
|
|
493
|
+
if (!alignedTarget || alignedTarget.identity !== sameNameAfter.calleeIdentity) {
|
|
494
|
+
edgeDeltas.push({
|
|
495
|
+
classification: "edge-retargeted",
|
|
496
|
+
callerIdentity: afterCallerNode.identity,
|
|
497
|
+
callerSymbol: afterCallerNode.symbol,
|
|
498
|
+
callerFile: afterCallerNode.filePath,
|
|
499
|
+
beforeTargetIdentity: bEdge.calleeIdentity,
|
|
500
|
+
afterTargetIdentity: sameNameAfter.calleeIdentity,
|
|
501
|
+
beforeTargetSymbol: bEdge.calleeSymbol,
|
|
502
|
+
afterTargetSymbol: sameNameAfter.calleeSymbol,
|
|
503
|
+
beforeTargetFile: bEdge.calleeFile ?? null,
|
|
504
|
+
afterTargetFile: sameNameAfter.calleeFile ?? null,
|
|
505
|
+
line: sameNameAfter.line,
|
|
506
|
+
confidence: "exact",
|
|
507
|
+
uncertainty: "exact",
|
|
508
|
+
evidence: `${afterCallerNode.symbol} call to ${bEdge.calleeSymbol} retargeted from ${bEdge.calleeFile ?? bEdge.calleeIdentity} to ${sameNameAfter.calleeFile ?? sameNameAfter.calleeIdentity}`,
|
|
509
|
+
});
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
if (removedEdges.length === 1 && addedEdges.length === 1 && removedEdges[0] && addedEdges[0]) {
|
|
514
|
+
const rem = removedEdges[0];
|
|
515
|
+
const add = addedEdges[0];
|
|
516
|
+
// Check if this is merely calling a renamed symbol (same underlying target) vs genuine edge retarget to a different target!
|
|
517
|
+
const alignedTarget = rem.calleeIdentity
|
|
518
|
+
? alignedBeforeToAfter.get(rem.calleeIdentity)
|
|
519
|
+
: undefined;
|
|
520
|
+
const isSameRenamedTarget = alignedTarget && add.calleeIdentity && alignedTarget.identity === add.calleeIdentity;
|
|
521
|
+
if (!isSameRenamedTarget) {
|
|
522
|
+
edgeDeltas.push({
|
|
523
|
+
classification: "edge-retargeted",
|
|
524
|
+
callerIdentity: afterCallerNode.identity,
|
|
525
|
+
callerSymbol: afterCallerNode.symbol,
|
|
526
|
+
callerFile: afterCallerNode.filePath,
|
|
527
|
+
beforeTargetIdentity: rem.calleeIdentity ?? null,
|
|
528
|
+
afterTargetIdentity: add.calleeIdentity ?? null,
|
|
529
|
+
beforeTargetSymbol: rem.calleeSymbol,
|
|
530
|
+
afterTargetSymbol: add.calleeSymbol,
|
|
531
|
+
beforeTargetFile: rem.calleeFile ?? null,
|
|
532
|
+
afterTargetFile: add.calleeFile ?? null,
|
|
533
|
+
line: add.line,
|
|
534
|
+
confidence: rem.calleeIdentity && add.calleeIdentity ? "exact" : "heuristic",
|
|
535
|
+
uncertainty: rem.calleeIdentity && add.calleeIdentity ? "exact" : "possible-set",
|
|
536
|
+
evidence: `${afterCallerNode.symbol} in ${afterCallerNode.filePath}:${add.line} retargeted call from ${rem.calleeSymbol} to ${add.calleeSymbol}`,
|
|
537
|
+
});
|
|
538
|
+
entityDeltas.push({
|
|
539
|
+
classification: "edge-retargeted",
|
|
540
|
+
beforeIdentity: beforeCallerId,
|
|
541
|
+
afterIdentity: afterCallerNode.identity,
|
|
542
|
+
beforeSymbol: rem.calleeSymbol,
|
|
543
|
+
afterSymbol: add.calleeSymbol,
|
|
544
|
+
kind: "call-edge",
|
|
545
|
+
beforeFile: rem.calleeFile ?? afterCallerNode.filePath,
|
|
546
|
+
afterFile: add.calleeFile ?? afterCallerNode.filePath,
|
|
547
|
+
semanticallyUnchanged: false,
|
|
548
|
+
isSuggestion: false,
|
|
549
|
+
confidence: rem.calleeIdentity && add.calleeIdentity ? "exact" : "heuristic",
|
|
550
|
+
uncertainty: rem.calleeIdentity && add.calleeIdentity ? "exact" : "possible-set",
|
|
551
|
+
evidence: `${afterCallerNode.symbol} retargeted call ${rem.calleeSymbol} -> ${add.calleeSymbol}`,
|
|
552
|
+
});
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
const semanticChangedSymbols = [
|
|
557
|
+
...new Set(entityDeltas
|
|
558
|
+
.filter((d) => !d.semanticallyUnchanged)
|
|
559
|
+
.flatMap((d) => [d.beforeSymbol, d.afterSymbol])
|
|
560
|
+
.filter((s) => Boolean(s))),
|
|
561
|
+
].sort();
|
|
562
|
+
const baselineChangedSymbols = [...(options?.baselineChangedSymbols ?? [])].sort();
|
|
563
|
+
const idChurnSymbols = new Set(entityDeltas
|
|
564
|
+
.filter((d) => d.classification === "id-churn")
|
|
565
|
+
.map((d) => d.afterSymbol)
|
|
566
|
+
.filter((s) => Boolean(s)));
|
|
567
|
+
// False matches in baseline: symbols reported in baselineChangedSymbols that only experienced harmless id-churn
|
|
568
|
+
const falseMatches = baselineChangedSymbols.filter((sym) => idChurnSymbols.has(sym));
|
|
569
|
+
// Missed changes in baseline: before-rename names, removed symbols, or retargeted targets absent from baselineChangedSymbols
|
|
570
|
+
const baselineSet = new Set(baselineChangedSymbols);
|
|
571
|
+
const missedChanges = semanticChangedSymbols.filter((sym) => !baselineSet.has(sym));
|
|
572
|
+
const semanticDeltaLatencyMs = Math.round((performance.now() - startedAt) * 1000) / 1000;
|
|
573
|
+
const partialReport = {
|
|
574
|
+
schemaVersion: 1,
|
|
575
|
+
beforeRevision: before.revision,
|
|
576
|
+
afterRevision: after.revision,
|
|
577
|
+
beforeSourceDigest: before.sourceDigest,
|
|
578
|
+
afterSourceDigest: after.sourceDigest,
|
|
579
|
+
entityDeltas,
|
|
580
|
+
edgeDeltas,
|
|
581
|
+
omissions,
|
|
582
|
+
unknowns,
|
|
583
|
+
baselineComparison: {
|
|
584
|
+
baselineChangedSymbols,
|
|
585
|
+
semanticChangedSymbols,
|
|
586
|
+
falseMatches,
|
|
587
|
+
missedChanges,
|
|
588
|
+
baselineBytes: options?.baselineBytes ?? Buffer.byteLength(JSON.stringify(baselineChangedSymbols)),
|
|
589
|
+
semanticDeltaBytes: 0,
|
|
590
|
+
baselineLatencyMs: options?.baselineLatencyMs ?? 0,
|
|
591
|
+
semanticDeltaLatencyMs,
|
|
592
|
+
},
|
|
593
|
+
};
|
|
594
|
+
partialReport.baselineComparison.semanticDeltaBytes = Buffer.byteLength(JSON.stringify(partialReport), "utf8");
|
|
595
|
+
return partialReport;
|
|
596
|
+
}
|
|
597
|
+
/**
|
|
598
|
+
* Build a SemanticGraphDeltaReport for a repository review across changed files.
|
|
599
|
+
*/
|
|
600
|
+
export function buildRepositorySemanticDelta(repoPath, baseRevision, toRevision, changedFiles, mappedChangedFiles, unmappedChangedFiles, baselineChangedSymbols, baselineLatencyMs) {
|
|
601
|
+
try {
|
|
602
|
+
const beforeFiles = {};
|
|
603
|
+
const afterFiles = {};
|
|
604
|
+
const gitEnv = {
|
|
605
|
+
...process.env,
|
|
606
|
+
GIT_TERMINAL_PROMPT: "0",
|
|
607
|
+
};
|
|
608
|
+
const fromRef = baseRevision || "HEAD~1";
|
|
609
|
+
for (const relFile of mappedChangedFiles.slice(0, 50)) {
|
|
610
|
+
try {
|
|
611
|
+
const beforeContent = execFileSync("git", ["show", `${fromRef}:${relFile}`], {
|
|
612
|
+
cwd: repoPath,
|
|
613
|
+
encoding: "utf8",
|
|
614
|
+
env: gitEnv,
|
|
615
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
616
|
+
});
|
|
617
|
+
beforeFiles[relFile] = beforeContent;
|
|
618
|
+
}
|
|
619
|
+
catch {
|
|
620
|
+
// File may be newly added after fromRef
|
|
621
|
+
}
|
|
622
|
+
try {
|
|
623
|
+
if (toRevision) {
|
|
624
|
+
const afterContent = execFileSync("git", ["show", `${toRevision}:${relFile}`], {
|
|
625
|
+
cwd: repoPath,
|
|
626
|
+
encoding: "utf8",
|
|
627
|
+
env: gitEnv,
|
|
628
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
629
|
+
});
|
|
630
|
+
afterFiles[relFile] = afterContent;
|
|
631
|
+
}
|
|
632
|
+
else {
|
|
633
|
+
const absPath = path.resolve(repoPath, relFile);
|
|
634
|
+
if (fs.existsSync(absPath)) {
|
|
635
|
+
afterFiles[relFile] = fs.readFileSync(absPath, "utf8");
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
catch {
|
|
640
|
+
// File may have been deleted
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
const beforeSnap = extractSemanticSnapshotFromFiles(fromRef, beforeFiles);
|
|
644
|
+
const afterSnap = extractSemanticSnapshotFromFiles(toRevision ?? "WORKTREE", afterFiles, {
|
|
645
|
+
unknownFiles: unmappedChangedFiles,
|
|
646
|
+
});
|
|
647
|
+
return computeSemanticGraphDelta(beforeSnap, afterSnap, {
|
|
648
|
+
baselineChangedSymbols,
|
|
649
|
+
baselineLatencyMs,
|
|
650
|
+
});
|
|
651
|
+
}
|
|
652
|
+
catch {
|
|
653
|
+
return undefined;
|
|
654
|
+
}
|
|
655
|
+
}
|