revgraph 0.0.0-stage → 0.2.0
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/LICENSE +21 -0
- package/README.md +136 -2
- package/README.zh-CN.md +85 -0
- package/assets/logo.svg +62 -0
- package/examples/cc-switch.rules.json +77 -0
- package/package.json +41 -4
- package/skills/revgraph/SKILL.md +153 -0
- package/skills/revgraph/references/rules.json +127 -0
- package/skills/revgraph/scripts/cli.mjs +526 -0
- package/skills/revgraph/scripts/git.mjs +765 -0
- package/skills/revgraph/scripts/graph.mjs +634 -0
- package/skills/revgraph/scripts/parsers/common.mjs +308 -0
- package/skills/revgraph/scripts/parsers/go.mjs +137 -0
- package/skills/revgraph/scripts/parsers/python.mjs +181 -0
- package/skills/revgraph/scripts/parsers/rust.mjs +253 -0
- package/skills/revgraph/scripts/parsers/tauri.mjs +363 -0
- package/skills/revgraph/scripts/parsers/ts.mjs +199 -0
|
@@ -0,0 +1,634 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { getFileDiffInfo, listRepoFiles, matchesAnyGlob, readFileAtTarget } from "./git.mjs";
|
|
4
|
+
import { escapeRegExp, GENERIC_SYMBOLS } from "./parsers/common.mjs";
|
|
5
|
+
import { buildTsAliasIndex, parseTsFile } from "./parsers/ts.mjs";
|
|
6
|
+
import { parsePythonFile } from "./parsers/python.mjs";
|
|
7
|
+
import { buildGoModuleIndex, parseGoFile } from "./parsers/go.mjs";
|
|
8
|
+
import { buildRustModuleIndex, parseRustFile } from "./parsers/rust.mjs";
|
|
9
|
+
import { analyzeTauriBridges, detectTauriRepo } from "./parsers/tauri.mjs";
|
|
10
|
+
|
|
11
|
+
const PARSED_EXT_RE = /\.(?:ts|tsx|js|jsx|mjs|cjs|py|go|rs)$/;
|
|
12
|
+
|
|
13
|
+
function detectLang(relPath) {
|
|
14
|
+
if (/\.(?:ts|tsx|js|jsx|mjs|cjs)$/.test(relPath)) return "ts";
|
|
15
|
+
if (relPath.endsWith(".py")) return "python";
|
|
16
|
+
if (relPath.endsWith(".go")) return "go";
|
|
17
|
+
if (relPath.endsWith(".rs")) return "rust";
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function isBarrelFile(relPath) {
|
|
22
|
+
return (
|
|
23
|
+
relPath.endsWith("/mod.rs") ||
|
|
24
|
+
relPath.endsWith("/lib.rs") ||
|
|
25
|
+
relPath === "lib.rs" ||
|
|
26
|
+
relPath.endsWith("/index.ts") ||
|
|
27
|
+
relPath.endsWith("/index.tsx") ||
|
|
28
|
+
relPath.endsWith("/index.js") ||
|
|
29
|
+
relPath.endsWith("/__init__.py") ||
|
|
30
|
+
relPath === "__init__.py"
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function isTestFile(relPath) {
|
|
35
|
+
return (
|
|
36
|
+
/(?:^|\/)(?:tests?|__tests__)\//.test(relPath) ||
|
|
37
|
+
/\.(?:test|spec)\.[a-z]+$/i.test(relPath) ||
|
|
38
|
+
/(?:^|\/)test_[^/]+\.py$/.test(relPath) ||
|
|
39
|
+
/_tests?\.(?:go|rs|py)$/.test(relPath)
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function isDocOrConfigFile(relPath) {
|
|
44
|
+
return (
|
|
45
|
+
/\.(?:md|mdx|txt|rst|json|yaml|yml|toml|ini|env|lock)$/i.test(relPath) ||
|
|
46
|
+
/(?:^|\/)(?:\.[^/]+|LICENSE|NOTICE|AUTHORS|CODEOWNERS|README)$/i.test(relPath)
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function isContractFile(relPath) {
|
|
51
|
+
return (
|
|
52
|
+
/\.(?:sql|proto|graphql|gql|avsc)$/i.test(relPath) ||
|
|
53
|
+
/(?:^|\/)(?:schema|migrations?|types|contracts?|openapi)\b/i.test(relPath) ||
|
|
54
|
+
/\.d\.ts$/.test(relPath)
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function rangesOverlap(aStart, aEnd, ranges) {
|
|
59
|
+
for (const r of ranges) {
|
|
60
|
+
if (r.start <= aEnd && r.end >= aStart) return true;
|
|
61
|
+
}
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Compute bottom-up topological levels within the changed-files subgraph.
|
|
67
|
+
* Leaf dependencies (files depended on by other changed files) get level 0 so they are reviewed first.
|
|
68
|
+
*/
|
|
69
|
+
function computeChangedTopoLevels(changedPaths, outgoing) {
|
|
70
|
+
const changedSet = new Set(changedPaths);
|
|
71
|
+
const inDegree = new Map();
|
|
72
|
+
const revAdj = new Map(); // dep -> [dependents]
|
|
73
|
+
|
|
74
|
+
for (const p of changedPaths) {
|
|
75
|
+
inDegree.set(p, 0);
|
|
76
|
+
revAdj.set(p, []);
|
|
77
|
+
}
|
|
78
|
+
for (const p of changedPaths) {
|
|
79
|
+
for (const dep of outgoing.get(p) || []) {
|
|
80
|
+
if (changedSet.has(dep) && dep !== p) {
|
|
81
|
+
inDegree.set(p, inDegree.get(p) + 1);
|
|
82
|
+
revAdj.get(dep).push(p);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const levels = new Map();
|
|
88
|
+
const queue = [];
|
|
89
|
+
for (const [p, deg] of inDegree.entries()) {
|
|
90
|
+
if (deg === 0) {
|
|
91
|
+
levels.set(p, 0);
|
|
92
|
+
queue.push(p);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
let head = 0;
|
|
97
|
+
while (head < queue.length) {
|
|
98
|
+
const cur = queue[head++];
|
|
99
|
+
const curLvl = levels.get(cur);
|
|
100
|
+
for (const dependent of revAdj.get(cur) || []) {
|
|
101
|
+
const nextLvl = Math.max(levels.get(dependent) ?? 0, curLvl + 1);
|
|
102
|
+
levels.set(dependent, nextLvl);
|
|
103
|
+
const rem = inDegree.get(dependent) - 1;
|
|
104
|
+
inDegree.set(dependent, rem);
|
|
105
|
+
if (rem === 0) queue.push(dependent);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// Any nodes remaining in a cycle get a deterministic middle level
|
|
110
|
+
for (const p of changedPaths) {
|
|
111
|
+
if (!levels.has(p)) levels.set(p, 1);
|
|
112
|
+
}
|
|
113
|
+
return levels;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Build full repository dependency & changed-symbol impact graph.
|
|
118
|
+
*/
|
|
119
|
+
export function buildDependencyGraph(target, rulesData = {}) {
|
|
120
|
+
const t0 = Date.now();
|
|
121
|
+
const repoRoot = target.repo_root;
|
|
122
|
+
const changedSet = new Set(target.reviewable_files.map((f) => f.path));
|
|
123
|
+
const deletedSet = new Set(
|
|
124
|
+
target.reviewable_files.filter((f) => f.status === "deleted").map((f) => f.path),
|
|
125
|
+
);
|
|
126
|
+
|
|
127
|
+
const repoFiles = listRepoFiles(target);
|
|
128
|
+
const allRepoFileSet = new Set(repoFiles);
|
|
129
|
+
for (const f of target.reviewable_files) {
|
|
130
|
+
if (f.status === "deleted") allRepoFileSet.delete(f.path);
|
|
131
|
+
else allRepoFileSet.add(f.path);
|
|
132
|
+
}
|
|
133
|
+
const allRepoFiles = Array.from(allRepoFileSet).sort();
|
|
134
|
+
|
|
135
|
+
// Include deleted source files in resolution set so surviving imports to deleted files resolve -> BROKEN_IMPORT
|
|
136
|
+
const resolveFileSet = new Set(allRepoFiles);
|
|
137
|
+
for (const d of deletedSet) resolveFileSet.add(d);
|
|
138
|
+
|
|
139
|
+
const codeFiles = allRepoFiles.filter((f) => PARSED_EXT_RE.test(f));
|
|
140
|
+
const enableTauri = detectTauriRepo(allRepoFiles);
|
|
141
|
+
|
|
142
|
+
const tsAliasIndex = buildTsAliasIndex(repoRoot, allRepoFiles);
|
|
143
|
+
const goIndex = buildGoModuleIndex(repoRoot, Array.from(resolveFileSet));
|
|
144
|
+
const rustFilesForIndex = Array.from(resolveFileSet).filter((f) => f.endsWith(".rs"));
|
|
145
|
+
const rustIndex = buildRustModuleIndex(rustFilesForIndex);
|
|
146
|
+
|
|
147
|
+
const fileContents = new Map();
|
|
148
|
+
const targetReadCache = new Map();
|
|
149
|
+
const parsed = new Map();
|
|
150
|
+
|
|
151
|
+
for (const rel of codeFiles) {
|
|
152
|
+
let content = null;
|
|
153
|
+
if (changedSet.has(rel) && !deletedSet.has(rel)) {
|
|
154
|
+
content = readFileAtTarget(target, rel, targetReadCache);
|
|
155
|
+
}
|
|
156
|
+
if (content === null) {
|
|
157
|
+
// shortcut: read unchanged files from working tree for speed; falls back to git show if absent locally
|
|
158
|
+
const abs = path.join(repoRoot, rel);
|
|
159
|
+
if (fs.existsSync(abs) && fs.statSync(abs).isFile()) {
|
|
160
|
+
try {
|
|
161
|
+
content = fs.readFileSync(abs, "utf8");
|
|
162
|
+
} catch {
|
|
163
|
+
content = null;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
if (content === null && target.head_git_ref) {
|
|
167
|
+
content = readFileAtTarget(target, rel, targetReadCache);
|
|
168
|
+
}
|
|
169
|
+
if (content === null) content = "";
|
|
170
|
+
}
|
|
171
|
+
fileContents.set(rel, content);
|
|
172
|
+
|
|
173
|
+
const lang = detectLang(rel);
|
|
174
|
+
if (lang === "ts") {
|
|
175
|
+
parsed.set(rel, parseTsFile(rel, content, resolveFileSet, tsAliasIndex, { enableTauri }));
|
|
176
|
+
} else if (lang === "python") {
|
|
177
|
+
parsed.set(rel, parsePythonFile(rel, content, resolveFileSet));
|
|
178
|
+
} else if (lang === "go") {
|
|
179
|
+
parsed.set(rel, parseGoFile(rel, content, goIndex));
|
|
180
|
+
} else if (lang === "rust") {
|
|
181
|
+
parsed.set(rel, parseRustFile(rel, content, resolveFileSet, rustIndex, { enableTauri }));
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// Adjacency lists
|
|
186
|
+
const outgoing = new Map();
|
|
187
|
+
const incoming = new Map();
|
|
188
|
+
const addEdge = (from, to) => {
|
|
189
|
+
if (from === to) return;
|
|
190
|
+
if (!outgoing.has(from)) outgoing.set(from, new Set());
|
|
191
|
+
if (!incoming.has(to)) incoming.set(to, new Set());
|
|
192
|
+
outgoing.get(from).add(to);
|
|
193
|
+
incoming.get(to).add(from);
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
for (const [rel, info] of parsed.entries()) {
|
|
197
|
+
for (const imp of info.imports) {
|
|
198
|
+
addEdge(rel, imp.target);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// Precompute diffs for reviewable files
|
|
203
|
+
const diffInfoByFile = new Map();
|
|
204
|
+
for (const fileEntry of target.reviewable_files) {
|
|
205
|
+
diffInfoByFile.set(fileEntry.path, getFileDiffInfo(target, fileEntry));
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// Optional auto-detected Tauri IPC bridges & checks
|
|
209
|
+
const tauriResult = enableTauri
|
|
210
|
+
? analyzeTauriBridges({ parsed, changedSet, diffInfoByFile, rangesOverlap, addEdge })
|
|
211
|
+
: { ipcBridges: [], staticAlerts: [], tsInvokeMap: new Map() };
|
|
212
|
+
|
|
213
|
+
const staticAlerts = [...tauriResult.staticAlerts];
|
|
214
|
+
const ipcBridges = tauriResult.ipcBridges;
|
|
215
|
+
|
|
216
|
+
// Universal static check: BROKEN_IMPORT (surviving file imports a deleted file)
|
|
217
|
+
for (const delPath of deletedSet) {
|
|
218
|
+
const importers = Array.from(incoming.get(delPath) || []).filter((f) => !deletedSet.has(f)).sort();
|
|
219
|
+
if (importers.length > 0) {
|
|
220
|
+
const sample = importers.slice(0, 5).map((f) => `\`${f}\``).join(", ");
|
|
221
|
+
const more = importers.length > 5 ? ` (+${importers.length - 5} more)` : "";
|
|
222
|
+
staticAlerts.push({
|
|
223
|
+
severity: "critical",
|
|
224
|
+
code: "BROKEN_IMPORT",
|
|
225
|
+
file: delPath,
|
|
226
|
+
message: `Deleted file is still imported by ${importers.length} file(s): ${sample}${more}.`,
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// Declarative static alerts from rules.json
|
|
232
|
+
const declarativeAlerts = rulesData.static_alerts || [];
|
|
233
|
+
const compiledDeclarative = declarativeAlerts.map((rule) => {
|
|
234
|
+
let diffRe = null;
|
|
235
|
+
let importRe = null;
|
|
236
|
+
try {
|
|
237
|
+
if (rule.diff_regex) diffRe = new RegExp(rule.diff_regex);
|
|
238
|
+
if (rule.import_regex) importRe = new RegExp(rule.import_regex);
|
|
239
|
+
} catch (err) {
|
|
240
|
+
throw new Error(`Invalid regex in static_alerts "${rule.id || "unnamed"}": ${err.message}`);
|
|
241
|
+
}
|
|
242
|
+
return { ...rule, _diffRe: diffRe, _importRe: importRe };
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
// Per-changed-file analysis
|
|
246
|
+
const fileAnalysis = [];
|
|
247
|
+
const seenFiles = new Set();
|
|
248
|
+
let totalInsertions = 0;
|
|
249
|
+
let totalDeletions = 0;
|
|
250
|
+
|
|
251
|
+
for (const fileEntry of target.reviewable_files) {
|
|
252
|
+
const rel = fileEntry.path;
|
|
253
|
+
if (seenFiles.has(rel)) continue;
|
|
254
|
+
seenFiles.add(rel);
|
|
255
|
+
totalInsertions += fileEntry.insertions || 0;
|
|
256
|
+
totalDeletions += fileEntry.deletions || 0;
|
|
257
|
+
|
|
258
|
+
const diffInfo = diffInfoByFile.get(rel);
|
|
259
|
+
const info = parsed.get(rel);
|
|
260
|
+
|
|
261
|
+
// Run declarative static alerts
|
|
262
|
+
for (const rule of compiledDeclarative) {
|
|
263
|
+
if (rule.patterns && rule.patterns.length > 0 && !matchesAnyGlob(rel, rule.patterns)) {
|
|
264
|
+
continue;
|
|
265
|
+
}
|
|
266
|
+
if (rule._diffRe) {
|
|
267
|
+
const touched = [...diffInfo.added_text, ...diffInfo.removed_text].some((l) =>
|
|
268
|
+
rule._diffRe.test(l),
|
|
269
|
+
);
|
|
270
|
+
if (touched) {
|
|
271
|
+
staticAlerts.push({
|
|
272
|
+
severity: rule.severity || "high",
|
|
273
|
+
code: rule.id || "RULE_ALERT",
|
|
274
|
+
file: rel,
|
|
275
|
+
message: rule.message,
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
if (rule._importRe && info) {
|
|
280
|
+
const matchedImports = info.imports
|
|
281
|
+
.map((i) => i.target)
|
|
282
|
+
.filter((t) => rule._importRe.test(t));
|
|
283
|
+
if (matchedImports.length > 0) {
|
|
284
|
+
staticAlerts.push({
|
|
285
|
+
severity: rule.severity || "medium",
|
|
286
|
+
code: rule.id || "IMPORT_BOUNDARY_ALERT",
|
|
287
|
+
file: rel,
|
|
288
|
+
message: `${rule.message} (matched: ${matchedImports.slice(0, 3).join(", ")})`,
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
const up1 = Array.from(incoming.get(rel) || []).sort();
|
|
295
|
+
const down1 = Array.from(outgoing.get(rel) || []).sort();
|
|
296
|
+
|
|
297
|
+
// 2-hop upstream callers with barrel-file fan-out guard
|
|
298
|
+
const up1Set = new Set(up1);
|
|
299
|
+
const up2Set = new Set();
|
|
300
|
+
const modStem = path.posix.basename(rel, path.posix.extname(rel));
|
|
301
|
+
for (const caller of up1) {
|
|
302
|
+
const callerIsBarrel = isBarrelFile(caller);
|
|
303
|
+
for (const caller2 of incoming.get(caller) || []) {
|
|
304
|
+
if (caller2 === rel || up1Set.has(caller2)) continue;
|
|
305
|
+
if (callerIsBarrel) {
|
|
306
|
+
const txt = fileContents.get(caller2) || "";
|
|
307
|
+
if (modStem && !["mod", "index", "__init__", "lib"].includes(modStem) && txt.includes(modStem)) {
|
|
308
|
+
up2Set.add(caller2);
|
|
309
|
+
}
|
|
310
|
+
} else {
|
|
311
|
+
up2Set.add(caller2);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
const up2 = Array.from(up2Set).sort();
|
|
316
|
+
|
|
317
|
+
// Changed symbols intersecting diff hunks
|
|
318
|
+
const changedSymbols = [];
|
|
319
|
+
if (info && diffInfo.added_lines.length > 0) {
|
|
320
|
+
const rustCmdNames = new Set((info.tauriCommands || []).map((c) => c.name));
|
|
321
|
+
for (const sym of info.symbols) {
|
|
322
|
+
if (rangesOverlap(sym.startLine, sym.endLine, diffInfo.added_lines)) {
|
|
323
|
+
const sigChanged = rangesOverlap(sym.startLine, sym.sigEndLine, diffInfo.added_lines);
|
|
324
|
+
const lookupName = sym.shortName || sym.name;
|
|
325
|
+
const isTauriCmd = rustCmdNames.has(lookupName);
|
|
326
|
+
const shouldSearchCallers = sym.exported || isTauriCmd || info.lang === "go";
|
|
327
|
+
const callers = shouldSearchCallers
|
|
328
|
+
? findSymbolCallers({
|
|
329
|
+
defFile: rel,
|
|
330
|
+
symbolName: lookupName,
|
|
331
|
+
lang: info.lang,
|
|
332
|
+
up1,
|
|
333
|
+
up2,
|
|
334
|
+
tsInvokeMap: tauriResult.tsInvokeMap,
|
|
335
|
+
fileContents,
|
|
336
|
+
parsed,
|
|
337
|
+
changedSet,
|
|
338
|
+
goIndex,
|
|
339
|
+
})
|
|
340
|
+
: { sites: [], total: 0 };
|
|
341
|
+
changedSymbols.push({
|
|
342
|
+
name: sym.name,
|
|
343
|
+
kind: isTauriCmd ? "tauri_command" : sym.kind,
|
|
344
|
+
exported: sym.exported,
|
|
345
|
+
lines: `${sym.startLine}-${sym.endLine}`,
|
|
346
|
+
signature_changed: sigChanged,
|
|
347
|
+
callers: callers.sites.slice(0, 8),
|
|
348
|
+
total_caller_sites: callers.total,
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
const fileIpcEdges = ipcBridges.filter((b) => b.from_file === rel || b.to_file === rel);
|
|
355
|
+
const hasSigChange = changedSymbols.some((s) => s.signature_changed && s.exported);
|
|
356
|
+
const blastScore =
|
|
357
|
+
up1.length * 3 +
|
|
358
|
+
Math.min(up2.length, 30) +
|
|
359
|
+
fileIpcEdges.length * 5 +
|
|
360
|
+
(hasSigChange ? 5 : 0) +
|
|
361
|
+
(isContractFile(rel) ? 10 : 0);
|
|
362
|
+
|
|
363
|
+
const riskTier = blastScore >= 25 ? "high" : blastScore >= 10 ? "medium" : "low";
|
|
364
|
+
|
|
365
|
+
fileAnalysis.push({
|
|
366
|
+
path: rel,
|
|
367
|
+
status: fileEntry.status,
|
|
368
|
+
lang: detectLang(rel) || "other",
|
|
369
|
+
insertions: fileEntry.insertions,
|
|
370
|
+
deletions: fileEntry.deletions,
|
|
371
|
+
blast_score: blastScore,
|
|
372
|
+
risk_tier: riskTier,
|
|
373
|
+
upstream_1hop: up1.slice(0, 15),
|
|
374
|
+
upstream_1hop_total: up1.length,
|
|
375
|
+
upstream_2hop_count: up2.length,
|
|
376
|
+
downstream_1hop: down1.slice(0, 15),
|
|
377
|
+
downstream_1hop_total: down1.length,
|
|
378
|
+
changed_symbols: changedSymbols.slice(0, 12),
|
|
379
|
+
ipc_bridges: fileIpcEdges,
|
|
380
|
+
});
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
// Universal PR/scope triage alerts
|
|
384
|
+
if (fileAnalysis.length >= 20 || totalInsertions >= 1000) {
|
|
385
|
+
staticAlerts.push({
|
|
386
|
+
severity: "medium",
|
|
387
|
+
code: "LARGE_PR",
|
|
388
|
+
file: fileAnalysis[0]?.path || ".",
|
|
389
|
+
message: `Large change (${fileAnalysis.length} reviewable files, +${totalInsertions}/-${totalDeletions} lines). Verify scope and architectural alignment before line-by-line review.`,
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
if (target.pr?.overlapping_prs?.length > 0) {
|
|
394
|
+
const summary = target.pr.overlapping_prs
|
|
395
|
+
.map((p) => `#${p.number}${p.is_draft ? " [Draft]" : ""} "${p.title}" (${p.reason})`)
|
|
396
|
+
.join(" | ");
|
|
397
|
+
staticAlerts.push({
|
|
398
|
+
severity: "high",
|
|
399
|
+
code: "POTENTIAL_OVERLAPPING_PR",
|
|
400
|
+
file: `PR #${target.pr.number}`,
|
|
401
|
+
message: `Potential overlapping open PR(s) detected: ${summary}. Inspect them first to avoid reviewing duplicate implementations.`,
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
// Automatic bottom-up Recommended Review Order (contracts -> topological leaf code -> consumers -> tests -> docs)
|
|
406
|
+
const topoLevels = computeChangedTopoLevels(
|
|
407
|
+
fileAnalysis.map((f) => f.path),
|
|
408
|
+
outgoing,
|
|
409
|
+
);
|
|
410
|
+
const orderBucket = (f) => {
|
|
411
|
+
if (isTestFile(f.path)) return 80;
|
|
412
|
+
if (isDocOrConfigFile(f.path)) return 90;
|
|
413
|
+
if (isContractFile(f.path)) return 0;
|
|
414
|
+
return 10 + (topoLevels.get(f.path) || 0);
|
|
415
|
+
};
|
|
416
|
+
|
|
417
|
+
const reviewOrder = [...fileAnalysis]
|
|
418
|
+
.sort((a, b) => orderBucket(a) - orderBucket(b) || b.blast_score - a.blast_score || a.path.localeCompare(b.path))
|
|
419
|
+
.map((f, idx) => ({
|
|
420
|
+
order: idx + 1,
|
|
421
|
+
path: f.path,
|
|
422
|
+
lang: f.lang,
|
|
423
|
+
risk_tier: f.risk_tier,
|
|
424
|
+
blast_score: f.blast_score,
|
|
425
|
+
reason: buildOrderReason(f, topoLevels.get(f.path) || 0),
|
|
426
|
+
}));
|
|
427
|
+
|
|
428
|
+
const mermaid = buildMermaidGraph(fileAnalysis, ipcBridges, changedSet);
|
|
429
|
+
|
|
430
|
+
return {
|
|
431
|
+
stats: {
|
|
432
|
+
indexed_files: codeFiles.length,
|
|
433
|
+
changed_files: fileAnalysis.length,
|
|
434
|
+
ipc_bridges: ipcBridges.length,
|
|
435
|
+
static_alerts: staticAlerts.length,
|
|
436
|
+
tauri_detected: enableTauri,
|
|
437
|
+
elapsed_ms: Date.now() - t0,
|
|
438
|
+
},
|
|
439
|
+
static_alerts: staticAlerts,
|
|
440
|
+
review_order: reviewOrder,
|
|
441
|
+
ipc_bridges: ipcBridges,
|
|
442
|
+
files: fileAnalysis,
|
|
443
|
+
mermaid,
|
|
444
|
+
};
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
function findSymbolCallers({
|
|
448
|
+
defFile,
|
|
449
|
+
symbolName,
|
|
450
|
+
lang,
|
|
451
|
+
up1,
|
|
452
|
+
up2,
|
|
453
|
+
tsInvokeMap,
|
|
454
|
+
fileContents,
|
|
455
|
+
parsed,
|
|
456
|
+
changedSet,
|
|
457
|
+
goIndex,
|
|
458
|
+
}) {
|
|
459
|
+
const sites = [];
|
|
460
|
+
let total = 0;
|
|
461
|
+
|
|
462
|
+
if (lang === "rust" && tsInvokeMap.has(symbolName)) {
|
|
463
|
+
for (const inv of tsInvokeMap.get(symbolName)) {
|
|
464
|
+
total++;
|
|
465
|
+
sites.push({ file: inv.file, line: inv.line, via: "tauri:invoke" });
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
if (!symbolName || symbolName.length < 3 || GENERIC_SYMBOLS.has(symbolName)) {
|
|
470
|
+
return { sites, total };
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
const candidates = new Set([...up1, ...up2.slice(0, 40)]);
|
|
474
|
+
for (const ch of changedSet) {
|
|
475
|
+
if (ch !== defFile && parsed.get(ch)?.lang === lang) candidates.add(ch);
|
|
476
|
+
}
|
|
477
|
+
// In Go, sibling files in the same package share symbols without importing each other
|
|
478
|
+
if (lang === "go") {
|
|
479
|
+
const dir = path.posix.dirname(defFile);
|
|
480
|
+
const key = dir === "." ? "" : dir;
|
|
481
|
+
for (const sib of goIndex.pkgFiles.get(key) || []) {
|
|
482
|
+
if (sib !== defFile) candidates.add(sib);
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
const symRe = new RegExp(`\\b${escapeRegExp(symbolName)}\\b`);
|
|
487
|
+
for (const cand of candidates) {
|
|
488
|
+
if (cand === defFile) continue;
|
|
489
|
+
const text = fileContents.get(cand);
|
|
490
|
+
if (!text || !text.includes(symbolName)) continue;
|
|
491
|
+
const lines = text.split("\n");
|
|
492
|
+
for (let i = 0; i < lines.length; i++) {
|
|
493
|
+
if (symRe.test(lines[i])) {
|
|
494
|
+
if (/^\s*(?:import|from|export|use|pub\s+use)\b/.test(lines[i])) continue;
|
|
495
|
+
total++;
|
|
496
|
+
if (sites.length < 10) {
|
|
497
|
+
sites.push({
|
|
498
|
+
file: cand,
|
|
499
|
+
line: i + 1,
|
|
500
|
+
via: up1.includes(cand) ? "direct" : "transitive",
|
|
501
|
+
snippet: lines[i].trim().slice(0, 90),
|
|
502
|
+
});
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
return { sites, total };
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
function buildOrderReason(f, topoLevel) {
|
|
512
|
+
const parts = [];
|
|
513
|
+
if (f.ipc_bridges.length > 0) parts.push(`${f.ipc_bridges.length} IPC bridge(s)`);
|
|
514
|
+
if (isContractFile(f.path)) parts.push("contract/schema");
|
|
515
|
+
if (f.upstream_1hop_total > 0) parts.push(`${f.upstream_1hop_total} direct dependent(s)`);
|
|
516
|
+
if (f.changed_symbols.some((s) => s.signature_changed)) parts.push("signature changed");
|
|
517
|
+
if (parts.length === 0) {
|
|
518
|
+
if (isTestFile(f.path)) parts.push("test");
|
|
519
|
+
else if (isDocOrConfigFile(f.path)) parts.push("docs/config");
|
|
520
|
+
else parts.push(topoLevel === 0 ? "leaf module" : `consumer (depth ${topoLevel})`);
|
|
521
|
+
}
|
|
522
|
+
return parts.join(", ");
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
function buildMermaidGraph(fileAnalysis, ipcBridges, changedSet) {
|
|
526
|
+
const MAX_NODES = 32;
|
|
527
|
+
const nodeIds = new Map();
|
|
528
|
+
let nextId = 1;
|
|
529
|
+
|
|
530
|
+
const getId = (rel) => {
|
|
531
|
+
if (!nodeIds.has(rel)) {
|
|
532
|
+
if (nodeIds.size >= MAX_NODES) return null;
|
|
533
|
+
nodeIds.set(rel, `N${nextId++}`);
|
|
534
|
+
}
|
|
535
|
+
return nodeIds.get(rel);
|
|
536
|
+
};
|
|
537
|
+
|
|
538
|
+
const escapeLabel = (s) => s.replace(/"/g, "'");
|
|
539
|
+
|
|
540
|
+
const lines = ["graph LR"];
|
|
541
|
+
const edges = new Set();
|
|
542
|
+
|
|
543
|
+
const graphCandidates = [...fileAnalysis]
|
|
544
|
+
.filter(
|
|
545
|
+
(f) =>
|
|
546
|
+
PARSED_EXT_RE.test(f.path) ||
|
|
547
|
+
f.upstream_1hop_total > 0 ||
|
|
548
|
+
f.downstream_1hop_total > 0 ||
|
|
549
|
+
f.ipc_bridges.length > 0,
|
|
550
|
+
)
|
|
551
|
+
.sort((a, b) => b.blast_score - a.blast_score);
|
|
552
|
+
|
|
553
|
+
for (const f of graphCandidates) {
|
|
554
|
+
const id = getId(f.path);
|
|
555
|
+
if (!id) break;
|
|
556
|
+
const tag = f.risk_tier === "high" ? "🔥 " : "";
|
|
557
|
+
lines.push(` ${id}["${tag}${escapeLabel(f.path)}"]:::changed`);
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
for (const b of ipcBridges) {
|
|
561
|
+
const fromId = getId(b.from_file);
|
|
562
|
+
const toId = getId(b.to_file);
|
|
563
|
+
if (!fromId || !toId) continue;
|
|
564
|
+
if (!changedSet.has(b.from_file)) {
|
|
565
|
+
lines.push(` ${fromId}["${escapeLabel(b.from_file)}"]:::caller`);
|
|
566
|
+
}
|
|
567
|
+
if (!changedSet.has(b.to_file)) {
|
|
568
|
+
lines.push(` ${toId}["${escapeLabel(b.to_file)}"]:::dep`);
|
|
569
|
+
}
|
|
570
|
+
const label = b.type === "tauri_command" ? `invoke:${b.name}` : `event:${b.name}`;
|
|
571
|
+
const edgeStr = ` ${fromId} ==>|"${label}"| ${toId}`;
|
|
572
|
+
if (!edges.has(edgeStr)) {
|
|
573
|
+
edges.add(edgeStr);
|
|
574
|
+
lines.push(edgeStr);
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
for (const f of fileAnalysis) {
|
|
579
|
+
const fId = nodeIds.get(f.path);
|
|
580
|
+
if (!fId) continue;
|
|
581
|
+
|
|
582
|
+
for (const dep of f.downstream_1hop) {
|
|
583
|
+
if (!changedSet.has(dep)) continue;
|
|
584
|
+
const dId = nodeIds.get(dep);
|
|
585
|
+
if (dId) {
|
|
586
|
+
const edgeStr = ` ${fId} --> ${dId}`;
|
|
587
|
+
if (!edges.has(edgeStr)) {
|
|
588
|
+
edges.add(edgeStr);
|
|
589
|
+
lines.push(edgeStr);
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
// Top 3 unchanged upstream callers per changed file
|
|
595
|
+
let shownCallers = 0;
|
|
596
|
+
const changedCallerCount = f.upstream_1hop.filter((c) => changedSet.has(c)).length;
|
|
597
|
+
for (const caller of f.upstream_1hop) {
|
|
598
|
+
if (changedSet.has(caller)) continue;
|
|
599
|
+
if (shownCallers >= 3) break;
|
|
600
|
+
const cId = getId(caller);
|
|
601
|
+
if (!cId) break;
|
|
602
|
+
lines.push(` ${cId}["${escapeLabel(caller)}"]:::caller`);
|
|
603
|
+
const edgeStr = ` ${cId} --> ${fId}`;
|
|
604
|
+
if (!edges.has(edgeStr)) {
|
|
605
|
+
edges.add(edgeStr);
|
|
606
|
+
lines.push(edgeStr);
|
|
607
|
+
shownCallers++;
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
const externalTotal = Math.max(0, f.upstream_1hop_total - changedCallerCount);
|
|
611
|
+
if (externalTotal > shownCallers) {
|
|
612
|
+
const rest = externalTotal - shownCallers;
|
|
613
|
+
const extraId = `MORE_${fId}`;
|
|
614
|
+
lines.push(` ${extraId}["+${rest} more callers"]:::muted`);
|
|
615
|
+
lines.push(` ${extraId} -.-> ${fId}`);
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
lines.push(" classDef changed fill:#fff3cd,stroke:#d97706,stroke-width:2px,color:#111");
|
|
620
|
+
lines.push(" classDef caller fill:#e0f2fe,stroke:#0284c7,color:#111");
|
|
621
|
+
lines.push(" classDef dep fill:#f3e8ff,stroke:#7c3aed,color:#111");
|
|
622
|
+
lines.push(" classDef muted fill:#f3f4f6,stroke:#9ca3af,stroke-dasharray:3 3,color:#555");
|
|
623
|
+
|
|
624
|
+
const seenDecl = new Set();
|
|
625
|
+
const deduped = [];
|
|
626
|
+
for (const l of lines) {
|
|
627
|
+
if (/^\s+N\d+\[/.test(l)) {
|
|
628
|
+
if (seenDecl.has(l)) continue;
|
|
629
|
+
seenDecl.add(l);
|
|
630
|
+
}
|
|
631
|
+
deduped.push(l);
|
|
632
|
+
}
|
|
633
|
+
return deduped.join("\n");
|
|
634
|
+
}
|