weavatrix 0.1.3 → 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.
Files changed (78) hide show
  1. package/README.md +168 -63
  2. package/SECURITY.md +25 -8
  3. package/package.json +2 -2
  4. package/skill/SKILL.md +108 -39
  5. package/src/analysis/architecture-contract.js +343 -0
  6. package/src/analysis/audit-debt.js +94 -0
  7. package/src/analysis/change-classification.js +509 -0
  8. package/src/analysis/cycle-route.js +14 -0
  9. package/src/analysis/dead-check.js +13 -6
  10. package/src/analysis/dead-code-review.js +245 -0
  11. package/src/analysis/dep-check-ecosystems.js +163 -0
  12. package/src/analysis/dep-check.js +69 -147
  13. package/src/analysis/dep-rules.js +47 -31
  14. package/src/analysis/duplicates.compute.js +47 -7
  15. package/src/analysis/endpoints-java.js +186 -0
  16. package/src/analysis/endpoints-rust.js +124 -0
  17. package/src/analysis/endpoints.js +18 -5
  18. package/src/analysis/findings.js +8 -1
  19. package/src/analysis/git-history.js +549 -0
  20. package/src/analysis/git-ref-graph.js +74 -0
  21. package/src/analysis/graph-analysis.aggregate.js +29 -28
  22. package/src/analysis/graph-analysis.edges.js +24 -0
  23. package/src/analysis/graph-analysis.js +1 -1
  24. package/src/analysis/graph-analysis.summaries.js +4 -1
  25. package/src/analysis/http-contracts.js +581 -0
  26. package/src/analysis/internal-audit.collect.js +43 -2
  27. package/src/analysis/internal-audit.reach.js +52 -1
  28. package/src/analysis/internal-audit.run.js +66 -13
  29. package/src/analysis/java-source.js +36 -0
  30. package/src/analysis/package-reachability.js +39 -0
  31. package/src/analysis/static-test-reachability.js +133 -0
  32. package/src/build-graph.js +72 -13
  33. package/src/graph/build-worker.js +3 -4
  34. package/src/graph/builder/lang-java.js +177 -14
  35. package/src/graph/builder/lang-js.js +71 -12
  36. package/src/graph/builder/lang-rust.js +129 -6
  37. package/src/graph/community.js +27 -0
  38. package/src/graph/file-lock.js +69 -0
  39. package/src/graph/freshness-probe.js +141 -0
  40. package/src/graph/graph-filter.js +28 -11
  41. package/src/graph/incremental-refresh.js +232 -0
  42. package/src/graph/internal-builder.barrels.js +169 -0
  43. package/src/graph/internal-builder.build.js +111 -16
  44. package/src/graph/internal-builder.java.js +33 -0
  45. package/src/graph/internal-builder.langs.js +2 -1
  46. package/src/graph/internal-builder.resolvers.js +109 -2
  47. package/src/graph/layout.js +21 -5
  48. package/src/graph/relations.js +4 -0
  49. package/src/graph/repo-registry.js +124 -0
  50. package/src/mcp/catalog.mjs +64 -21
  51. package/src/mcp/evidence-snapshot.architecture.mjs +80 -0
  52. package/src/mcp/evidence-snapshot.common.mjs +160 -0
  53. package/src/mcp/evidence-snapshot.duplicates.mjs +217 -0
  54. package/src/mcp/evidence-snapshot.health.mjs +95 -0
  55. package/src/mcp/evidence-snapshot.inventory.mjs +148 -0
  56. package/src/mcp/evidence-snapshot.mjs +50 -0
  57. package/src/mcp/evidence-snapshot.package-graph.mjs +249 -0
  58. package/src/mcp/evidence-snapshot.structure.mjs +81 -0
  59. package/src/mcp/graph-context.mjs +106 -181
  60. package/src/mcp/graph-diff.mjs +217 -0
  61. package/src/mcp/staleness-notice.mjs +20 -0
  62. package/src/mcp/sync-evidence.mjs +418 -0
  63. package/src/mcp/sync-payload.mjs +194 -8
  64. package/src/mcp/tool-result.mjs +51 -0
  65. package/src/mcp/tools-actions.mjs +114 -33
  66. package/src/mcp/tools-architecture.mjs +144 -0
  67. package/src/mcp/tools-company.mjs +273 -0
  68. package/src/mcp/tools-graph-hubs.mjs +58 -0
  69. package/src/mcp/tools-graph.mjs +36 -68
  70. package/src/mcp/tools-health.mjs +354 -20
  71. package/src/mcp/tools-history.mjs +22 -0
  72. package/src/mcp/tools-impact-change.mjs +261 -0
  73. package/src/mcp/tools-impact.mjs +35 -11
  74. package/src/mcp-server.mjs +100 -17
  75. package/src/mcp-source-tools.mjs +11 -3
  76. package/src/path-classification.js +201 -0
  77. package/src/path-ignore.js +69 -0
  78. package/src/security/typosquat.js +1 -1
@@ -8,18 +8,25 @@
8
8
  import { readFileSync } from "node:fs";
9
9
  import { posix } from "node:path";
10
10
  import { createRepoBoundary } from "../repo-path.js";
11
+ import { isStructuralRelation } from "../graph/relations.js";
12
+ import { createPathClassifier, hasPathClass } from "../path-classification.js";
11
13
 
12
14
  const IDENT_RE = /[A-Za-z_$][\w$]*/g;
13
15
  const bareName = (label) => String(label || "").replace(/\s*\(.*$/, "").replace(/[()]/g, "").trim();
14
16
  // entry surfaces are never dead even with no inbound edge (framework/CLI/HTTP enter them externally).
15
17
  // Exported for internal-audit.js (reachability entry set) — keep the two in lockstep.
16
18
  export const ENTRY_FILE = /(^|[\\/])(index|main|app|server|cli|cmd|bootstrap|entry|run|__main__|manage|wsgi|asgi|setup|conftest)\.[a-z0-9]+$|(^|[\\/])(bin|cmd)[\\/]|(^|[\\/])main\.go$/i;
17
- const TEST_FILE = /(^|[\\/])[^\\/]*[._-](test|spec)\.[a-z0-9]+$|(^|[\\/])(test|tests|__tests__|spec)[\\/]/i;
19
+ const defaultPathClassifier = createPathClassifier(null);
20
+ const isTestFile = (file) => hasPathClass(defaultPathClassifier.explain(file), "test", "e2e");
18
21
 
19
22
  // Framework-owned entry modules are invoked by convention rather than a source import. Keep this narrow:
20
23
  // these are Next.js App/Pages Router surfaces and framework metadata files, not every file under `app/`.
21
24
  export const NEXT_ENTRY_FILE = /(^|\/)(?:src\/)?app\/(?:.*\/)?(?:page|layout|template|loading|error|global-error|not-found|default|route|robots|sitemap|manifest|opengraph-image|twitter-image|icon|apple-icon)\.[cm]?[jt]sx?$|(^|\/)(?:src\/)?pages\/(?!.*\/(?:components?|lib|utils?)\/).+\.[cm]?[jt]sx?$|(^|\/)(?:middleware|instrumentation)\.[cm]?[jt]s$/i;
22
- export const isFrameworkEntryFile = (file) => NEXT_ENTRY_FILE.test(String(file || "").replace(/\\/g, "/"));
25
+ export const RUST_ENTRY_FILE = /(^|\/)(?:build\.rs|src\/(?:lib|main)\.rs)$/i;
26
+ export const isFrameworkEntryFile = (file) => {
27
+ const normalized = String(file || "").replace(/\\/g, "/");
28
+ return NEXT_ENTRY_FILE.test(normalized) || RUST_ENTRY_FILE.test(normalized);
29
+ };
23
30
 
24
31
  const lineOfNode = (n) => {
25
32
  const m = /@(\d+)$/.exec(String(n.id || "")) || /L(\d+)/.exec(String(n.source_location || ""));
@@ -93,7 +100,7 @@ export function computeDead(graph, sources, { entrySet = new Set() } = {}) {
93
100
  const nodes = graph.nodes || [], links = graph.links || [];
94
101
  const ep = (v) => (v && typeof v === "object" ? v.id : v);
95
102
  const inbound = new Set();
96
- for (const l of links) if (l.relation !== "contains") inbound.add(ep(l.target));
103
+ for (const l of links) if (!isStructuralRelation(l.relation)) inbound.add(ep(l.target));
97
104
 
98
105
  // whole-repo identifier frequency: a symbol whose name appears MORE than once total (its definition + at least
99
106
  // one use, same-file OR cross-file) is referenced. Errs toward "alive" (common-named symbols never flagged).
@@ -139,7 +146,7 @@ export function computeDead(graph, sources, { entrySet = new Set() } = {}) {
139
146
  const deadSymbols = [];
140
147
  for (const n of symById.values()) {
141
148
  if (isReferenced(n)) continue;
142
- const test = TEST_FILE.test(n.source_file);
149
+ const test = isTestFile(n.source_file);
143
150
  deadSymbols.push({ id: n.id, file: n.source_file, label: n.label, test, reason: "no inbound edge and name unreferenced outside its file" });
144
151
  }
145
152
  const deadSet = new Set(deadSymbols.map((s) => s.id));
@@ -168,7 +175,7 @@ export function computeUnusedExports(graph, sources, { dynamicTargets = new Set(
168
175
  const nodes = graph.nodes || [], links = graph.links || [];
169
176
  const ep = (v) => (v && typeof v === "object" ? v.id : v);
170
177
  const inbound = new Set();
171
- for (const l of links) if (l.relation !== "contains") inbound.add(ep(l.target));
178
+ for (const l of links) if (!isStructuralRelation(l.relation)) inbound.add(ep(l.target));
172
179
 
173
180
  const namespaceConsumed = namespaceConsumedFiles(sources, graph);
174
181
  const candidates = [];
@@ -197,7 +204,7 @@ export function computeUnusedExports(graph, sources, { dynamicTargets = new Set(
197
204
  for (const { n, nm } of candidates) {
198
205
  const occ = occursIn.get(nm);
199
206
  if (occ && [...occ].some((f) => f !== n.source_file)) continue; // referenced beyond its own file → alive
200
- out.push({ id: n.id, file: n.source_file, label: n.label, test: TEST_FILE.test(n.source_file), reason: "exported but never imported or referenced outside its own file" });
207
+ out.push({ id: n.id, file: n.source_file, label: n.label, test: isTestFile(n.source_file), reason: "exported but never imported or referenced outside its own file" });
201
208
  }
202
209
  return out;
203
210
  }
@@ -0,0 +1,245 @@
1
+ // Conservative, source-free review queue for statically unreferenced code. This deliberately builds
2
+ // on computeDead instead of inventing a second liveness model. Candidates are evidence for review,
3
+ // never deletion instructions: framework entry, dynamic loading, reflection and public API surfaces
4
+ // lower confidence and remain explicit in the returned record.
5
+ import { computeDead, isFrameworkEntryFile } from "./dead-check.js";
6
+ import { createPathClassifier, hasPathClass } from "../path-classification.js";
7
+
8
+ const CONFIDENCE_RANK = Object.freeze({ high: 0, medium: 1, low: 2 });
9
+ const CLASSIFIED_NON_PRODUCT = Object.freeze(["generated", "mock", "story", "docs", "benchmark", "temp"]);
10
+ const DYNAMIC_RE = /(?:\bimport\s*\(|\brequire\s*\(\s*(?!["'])|\bcreateRequire\s*\(|\b__import__\s*\(|\bimportlib\.|(?:^|[^\w.$])(?:eval|exec)\s*\()/m;
11
+ const REFLECTION_RE = /(?:\b(?:Class\.forName|get(?:Declared)?Method|getattr|setattr|hasattr|Method\.Invoke|GetMethod|GetProcAddress|dlsym)\s*\(|\b(?:globals|locals)\s*\(\s*\)\s*\[|\breflect\.[A-Za-z_$][\w$]*\s*\()/i;
12
+
13
+ const normalizedPath = (value) => String(value || "").replace(/\\/g, "/").replace(/^\.\//, "");
14
+ const lineOf = (node) => {
15
+ const match = /@(\d+)$/.exec(String(node?.id || "")) || /L(\d+)/.exec(String(node?.source_location || ""));
16
+ return match ? Number(match[1]) : 0;
17
+ };
18
+ const bareLabel = (value) => String(value || "").replace(/\s*\(.*$/, "").replace(/[()]/g, "").trim();
19
+
20
+ function kindOf(node) {
21
+ const symbolKind = String(node?.symbol_kind || "").toLowerCase();
22
+ if (symbolKind === "method" || symbolKind === "constructor") return "method";
23
+ if (["function", "function_definition", "func", "fn"].includes(symbolKind)) return "function";
24
+ // Old graphs may not retain symbol_kind. Only use call syntax as a compatibility fallback;
25
+ // member_of alone also describes fields and must never promote them to methods.
26
+ if (!symbolKind && /\([^)]*\)\s*$/.test(String(node?.label || ""))) return node?.member_of ? "method" : "function";
27
+ return "symbol";
28
+ }
29
+
30
+ function isPublicSurface(node) {
31
+ const visibility = String(node?.visibility || "").toLowerCase();
32
+ return node?.exported === true || visibility === "public" || visibility === "protected";
33
+ }
34
+
35
+ function pathAllowed(info, { includeTests, includeClassified }) {
36
+ if (!includeTests && hasPathClass(info, "test", "e2e")) return { ok: false, bucket: "tests" };
37
+ if (!includeClassified && (info?.excluded || hasPathClass(info, ...CLASSIFIED_NON_PRODUCT))) {
38
+ return { ok: false, bucket: "classified" };
39
+ }
40
+ return { ok: true };
41
+ }
42
+
43
+ function symbolCandidate(item, node, context) {
44
+ const file = normalizedPath(item.file);
45
+ const source = String(context.sources.get(file) || "");
46
+ const pathInfo = context.classify(file, source);
47
+ const publicSurface = isPublicSurface(node);
48
+ const externalEntry = context.entrySet.has(file) || isFrameworkEntryFile(file);
49
+ const framework = context.frameworkByFile.get(file) || null;
50
+ const dynamicFile = context.dynamicTargets.has(file) || DYNAMIC_RE.test(source);
51
+ const reflectionFile = REFLECTION_RE.test(source);
52
+ const kind = kindOf(node);
53
+ let confidence = (String(node?.visibility || "").toLowerCase() === "private" || (!publicSurface && ["method", "function"].includes(kind)))
54
+ ? "high" : "medium";
55
+ const caveats = [];
56
+
57
+ if (publicSurface) {
58
+ confidence = "low";
59
+ caveats.push("Public/exported APIs can be consumed by downstream packages, interfaces, reflection, dependency injection, templates, or configuration outside this repository.");
60
+ }
61
+ if (externalEntry || framework) {
62
+ confidence = "low";
63
+ caveats.push(framework?.reason || "This file is an externally entered or framework-owned surface; static inbound edges are not complete usage evidence.");
64
+ }
65
+ if (node?.decorated) {
66
+ confidence = "low";
67
+ caveats.push("Decorators/annotations can register this symbol without a direct caller.");
68
+ }
69
+ if (dynamicFile) {
70
+ confidence = "low";
71
+ caveats.push("The declaring file uses or is reached through dynamic loading, so the static graph can miss callers.");
72
+ }
73
+ if (reflectionFile || (publicSurface && context.repoSignals.reflection)) {
74
+ confidence = "low";
75
+ caveats.push("Reflection is present and may invoke names without a resolvable static edge.");
76
+ }
77
+
78
+ return {
79
+ id: String(item.id),
80
+ kind,
81
+ classification: publicSurface ? `public-${kind}` : kind === "method" ? "internal-method" : kind === "function" ? "internal-function" : "unreferenced-symbol",
82
+ file,
83
+ line: lineOf(node),
84
+ symbol: bareLabel(node?.label || item.label),
85
+ owner: node?.member_of || null,
86
+ symbolKind: node?.symbol_kind || null,
87
+ visibility: node?.visibility || (node?.exported ? "exported" : "internal"),
88
+ confidence,
89
+ reason: item.reason,
90
+ evidence: [
91
+ { kind: "graph", fact: "No inbound non-structural graph edge targets this symbol." },
92
+ { kind: "source-index", fact: "Its identifier has no second indexed occurrence that establishes a caller." },
93
+ ],
94
+ caveats,
95
+ publicApi: publicSurface,
96
+ externallyEnteredFile: externalEntry,
97
+ pathClasses: pathInfo.classes || [],
98
+ matchedPathRule: pathInfo.matchedRule || null,
99
+ reviewAction: "Confirm with read_source, get_dependents, exact search, framework/config inspection and tests; never auto-delete.",
100
+ };
101
+ }
102
+
103
+ function fileCandidate(item, symbols, context) {
104
+ const file = normalizedPath(item.file);
105
+ const source = String(context.sources.get(file) || "");
106
+ const pathInfo = context.classify(file, source);
107
+ const publicSymbols = symbols.filter((symbol) => symbol.publicApi);
108
+ const dynamicFile = context.dynamicTargets.has(file) || DYNAMIC_RE.test(source);
109
+ const reflectionFile = REFLECTION_RE.test(source);
110
+ // Whole-file liveness always remains at most medium: external launchers/manifests can exist outside
111
+ // the indexed import graph even when every internal symbol signal is otherwise strong.
112
+ let confidence = "medium";
113
+ const caveats = ["Files can be launched by scripts, plugins, manifests, framework conventions, generated consumers, or external tooling without an import edge."];
114
+ if (publicSymbols.length) {
115
+ confidence = "low";
116
+ caveats.push(`${publicSymbols.length} indexed public/exported symbol(s) may be consumed outside this repository.`);
117
+ }
118
+ if (dynamicFile) {
119
+ confidence = "low";
120
+ caveats.push("Dynamic loading is present in or targets this file.");
121
+ }
122
+ if (reflectionFile) {
123
+ confidence = "low";
124
+ caveats.push("Reflection is present in this file.");
125
+ }
126
+ return {
127
+ id: `file:${file}`,
128
+ kind: "file",
129
+ classification: "unreferenced-file",
130
+ file,
131
+ line: 1,
132
+ symbol: null,
133
+ owner: null,
134
+ symbolKind: null,
135
+ visibility: null,
136
+ confidence,
137
+ reason: item.reason,
138
+ evidence: [
139
+ { kind: "graph", fact: "No indexed module imports this file." },
140
+ { kind: "symbol-liveness", fact: "Every indexed symbol in the file is statically unreferenced." },
141
+ ],
142
+ caveats,
143
+ publicApi: publicSymbols.length > 0,
144
+ externallyEnteredFile: false,
145
+ pathClasses: pathInfo.classes || [],
146
+ matchedPathRule: pathInfo.matchedRule || null,
147
+ reviewAction: "Verify package scripts, manifests, framework discovery, dynamic loading and external consumers; never auto-delete.",
148
+ };
149
+ }
150
+
151
+ // Pure review model. Filesystem collection/entry inference stays in the MCP adapter so tests can pass
152
+ // exact source maps and convention evidence without touching the working tree.
153
+ export function computeDeadCodeReview(graph, sources, options = {}) {
154
+ const entrySet = options.entrySet instanceof Set ? options.entrySet : new Set(options.entrySet || []);
155
+ const dynamicTargets = options.dynamicTargets instanceof Set ? options.dynamicTargets : new Set(options.dynamicTargets || []);
156
+ const frameworkByFile = new Map((options.frameworkEvidence || []).map((entry) => [normalizedPath(entry.file), entry]));
157
+ const classifier = options.pathClassifier || createPathClassifier(null);
158
+ const classificationCache = new Map();
159
+ const classify = (file, source) => {
160
+ if (!classificationCache.has(file)) classificationCache.set(file, classifier.explain(file, { content: source }));
161
+ return classificationCache.get(file);
162
+ };
163
+ const repoSignals = {
164
+ dynamicLoading: (graph.externalImports || []).some((entry) => entry?.dynamic) || [...sources.values()].some((text) => DYNAMIC_RE.test(String(text || ""))),
165
+ reflection: [...sources.values()].some((text) => REFLECTION_RE.test(String(text || ""))),
166
+ };
167
+ const includeTests = options.includeTests === true;
168
+ const includeClassified = options.includeClassified === true;
169
+ const minConfidence = Object.hasOwn(CONFIDENCE_RANK, options.minConfidence) ? options.minConfidence : "medium";
170
+ const pathPrefix = normalizedPath(options.path || "").replace(/\/+$/, "");
171
+ const requestedKinds = new Set(Array.isArray(options.kinds) && options.kinds.length ? options.kinds : ["file", "function", "method", "symbol"]);
172
+ const context = { sources, entrySet, dynamicTargets, frameworkByFile, classify, repoSignals };
173
+ const dead = computeDead(graph, sources, { entrySet });
174
+ const nodesById = new Map((graph.nodes || []).map((node) => [String(node.id), node]));
175
+ const rawSymbols = dead.deadSymbols
176
+ .map((item) => ({ item, node: nodesById.get(String(item.id)) }))
177
+ .filter((entry) => entry.node)
178
+ .map(({ item, node }) => symbolCandidate(item, node, context));
179
+ const symbolsByFile = new Map();
180
+ for (const candidate of rawSymbols) {
181
+ if (!symbolsByFile.has(candidate.file)) symbolsByFile.set(candidate.file, []);
182
+ symbolsByFile.get(candidate.file).push(candidate);
183
+ }
184
+ const rawFiles = dead.deadFiles.map((item) => fileCandidate(item, symbolsByFile.get(normalizedPath(item.file)) || [], context));
185
+ const raw = [...rawSymbols, ...rawFiles];
186
+ const suppressed = { tests: 0, classified: 0, confidence: 0, path: 0, kind: 0 };
187
+ const candidates = [];
188
+ for (const candidate of raw) {
189
+ const info = classify(candidate.file, sources.get(candidate.file));
190
+ const allowed = pathAllowed(info, { includeTests, includeClassified });
191
+ if (!allowed.ok) { suppressed[allowed.bucket] += 1; continue; }
192
+ if (pathPrefix && candidate.file !== pathPrefix && !candidate.file.startsWith(`${pathPrefix}/`)) { suppressed.path += 1; continue; }
193
+ if (!requestedKinds.has(candidate.kind)) { suppressed.kind += 1; continue; }
194
+ if (CONFIDENCE_RANK[candidate.confidence] > CONFIDENCE_RANK[minConfidence]) { suppressed.confidence += 1; continue; }
195
+ candidates.push(candidate);
196
+ }
197
+ candidates.sort((left, right) =>
198
+ CONFIDENCE_RANK[left.confidence] - CONFIDENCE_RANK[right.confidence]
199
+ || left.file.localeCompare(right.file)
200
+ || left.line - right.line
201
+ || left.id.localeCompare(right.id));
202
+
203
+ const warnings = [{
204
+ code: "STATIC_LIVENESS_IS_NOT_RUNTIME_PROOF",
205
+ message: "No static reference is not proof of dead runtime code. Review every candidate; never bulk-delete or auto-delete.",
206
+ }];
207
+ if (repoSignals.dynamicLoading) warnings.push({
208
+ code: "DYNAMIC_LOADING_PRESENT",
209
+ message: "Dynamic loading exists in the repository; static callers may be incomplete.",
210
+ });
211
+ if (repoSignals.reflection) warnings.push({
212
+ code: "REFLECTION_PRESENT",
213
+ message: "Reflection-like APIs exist in the repository; public or name-addressed symbols may be invoked without graph edges.",
214
+ });
215
+ if (suppressed.confidence) warnings.push({
216
+ code: "LOW_CONFIDENCE_SUPPRESSED",
217
+ message: `${suppressed.confidence} low-confidence candidate(s), including public/framework-sensitive code, were suppressed; set min_confidence=low to review them explicitly.`,
218
+ });
219
+
220
+ return {
221
+ candidates,
222
+ warnings,
223
+ suppressed,
224
+ repoSignals,
225
+ totals: {
226
+ indexedSymbols: dead.stats.symbols,
227
+ indexedFiles: dead.stats.files,
228
+ rawDeadSymbols: rawSymbols.length,
229
+ rawDeadFiles: rawFiles.length,
230
+ reviewCandidates: candidates.length,
231
+ byConfidence: {
232
+ high: candidates.filter((candidate) => candidate.confidence === "high").length,
233
+ medium: candidates.filter((candidate) => candidate.confidence === "medium").length,
234
+ low: candidates.filter((candidate) => candidate.confidence === "low").length,
235
+ },
236
+ },
237
+ policy: {
238
+ verdict: "REVIEW_REQUIRED",
239
+ autoDelete: false,
240
+ minConfidence,
241
+ includeTests,
242
+ includeClassified,
243
+ },
244
+ };
245
+ }
@@ -0,0 +1,163 @@
1
+ import { makeFinding } from "./findings.js";
2
+
3
+ const TEST_PATH_RE = /(^|[/\\])(test|tests|__tests__|spec|e2e|__mocks__)([/\\]|$)|[._-](test|spec)\.[a-z0-9]+$|_test\.go$/i;
4
+ const escRe = (s) => String(s).replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
5
+ const mentioned = (blob, name) => new RegExp(`(^|[^\\w@.-])${escRe(name)}($|[^\\w.-])`).test(blob);
6
+
7
+ // ---- Go: set math over ecosystem:"Go" externalImports vs go.mod requires ----
8
+ // goMod = parseGoMod() output. Only DIRECT requires can be "unused" (indirect ones belong to `go mod
9
+ // tidy`); replace targets and the own module never count. Missing = imported module with no require
10
+ // prefix — rare in Go (builds fail), so it usually flags vendored/replaced setups: keep it medium.
11
+ export function computeGoDepFindings({ externalImports = [], goMod = null, nonRuntimeRoots = [] } = {}) {
12
+ const findings = [];
13
+ if (!goMod || !goMod.module) return { findings, declared: new Set() };
14
+ const requires = goMod.requires || [];
15
+ const declared = new Set(requires.map((r) => r.path));
16
+ const replaced = new Set((goMod.replaces || []).map((r) => r.from));
17
+ const moduleOf = (spec) => { let best = ""; for (const p of declared) if ((spec === p || spec.startsWith(p + "/")) && p.length > best.length) best = p; return best; };
18
+
19
+ const usedModules = new Set();
20
+ const missing = new Map(); // pkg → { files:Set, lines:Map }
21
+ const inNonRuntimeRoot = (file) => (nonRuntimeRoots || []).some((root) => {
22
+ const f = String(file || "").replace(/\\/g, "/"), r = String(root || "").replace(/\\/g, "/").replace(/^\.\//, "").replace(/^\/+|\/+$/g, "");
23
+ return !!r && (f === r || f.startsWith(`${r}/`));
24
+ });
25
+ for (const e of externalImports) {
26
+ if (e.ecosystem !== "Go" || e.builtin || e.unresolved || !e.pkg) continue;
27
+ const mod = moduleOf(e.spec || e.pkg) || (declared.has(e.pkg) ? e.pkg : "");
28
+ if (mod) { usedModules.add(mod); continue; }
29
+ let m = missing.get(e.pkg);
30
+ if (!m) missing.set(e.pkg, (m = { files: new Set(), lines: new Map() }));
31
+ m.files.add(e.file);
32
+ if (!m.lines.has(e.file)) m.lines.set(e.file, e.line || 0);
33
+ }
34
+
35
+ for (const r of requires) {
36
+ if (r.indirect || usedModules.has(r.path)) continue;
37
+ findings.push(makeFinding({
38
+ category: "unused",
39
+ rule: "unused-dep",
40
+ severity: "low",
41
+ confidence: "medium",
42
+ title: `Unused Go module: ${r.path}`,
43
+ reason: "A direct go.mod requirement has no matching recorded Go import; build-tagged usage remains possible.",
44
+ detail: `"${r.path}" is required (direct) in go.mod but no .go file imports it or any of its packages. Build-tag-guarded files can hide usage — confirm with \`go mod tidy\` before removing.`,
45
+ package: r.path,
46
+ version: r.version,
47
+ source: "internal",
48
+ fixHint: "go mod tidy (drops requires nothing imports)",
49
+ }));
50
+ }
51
+ for (const [pkg, use] of missing) {
52
+ if (replaced.has(pkg)) continue;
53
+ const files = [...use.files];
54
+ if (files.every(inNonRuntimeRoot)) continue;
55
+ findings.push(makeFinding({
56
+ category: "unused",
57
+ rule: "missing-dep",
58
+ severity: "medium",
59
+ confidence: "medium",
60
+ title: `Missing Go module: ${pkg}`,
61
+ reason: "A recorded Go import has no matching direct go.mod requirement or replace entry.",
62
+ detail: `"${pkg}" is imported by ${files.length} file(s) but go.mod has no matching require — a replace/workspace/vendor setup, or the module was never added.`,
63
+ package: pkg,
64
+ file: files[0],
65
+ line: use.lines.get(files[0]) || 0,
66
+ evidence: files.slice(0, 5).map((f) => ({ file: f, line: use.lines.get(f) || 0, snippet: "" })),
67
+ source: "internal",
68
+ fixHint: `go get ${pkg}`,
69
+ }));
70
+ }
71
+ return { findings, declared };
72
+ }
73
+
74
+ // ---- Python: ecosystem:"PyPI" externalImports vs requirements/pyproject/Pipfile ----
75
+ // Import→dist naming is heuristic (yaml→PyYAML, python-X/X-python variants), so matching is GENEROUS for
76
+ // suppression and every unused finding stays low-confidence. CLI-only tools (pytest, black, gunicorn …)
77
+ // and stub/plugin conventions (types-*, *-stubs, pytest-*, flake8-*) are never flagged unused.
78
+ const PY_TOOL_DISTS = new Set(("pytest tox nox black ruff flake8 pylint mypy pyright isort bandit coverage pre-commit pip setuptools wheel build twine poetry poetry-core " +
79
+ "pip-tools uv virtualenv pipenv gunicorn uwsgi supervisor ipython jupyter jupyterlab notebook ipykernel codecov autopep8 yapf commitizen detect-secrets safety pip-audit hatchling flit flit-core pdm").split(" "));
80
+ const pyNorm = (n) => String(n || "").toLowerCase().replace(/[-_.]+/g, "-");
81
+
82
+ export function computePyDepFindings({
83
+ externalImports = [], pyManifest = null, configTexts = new Map(),
84
+ managedDependencies = [], ignoredDependencies = [], nonRuntimeRoots = [],
85
+ } = {}) {
86
+ const findings = [];
87
+ const deps = (pyManifest && pyManifest.deps) || [];
88
+ const present = !!(pyManifest && pyManifest.present);
89
+ const declared = new Set(deps.map((d) => pyNorm(d.name)));
90
+ const managed = new Set((managedDependencies || []).map(pyNorm));
91
+ const ignored = new Set((ignoredDependencies || []).map(pyNorm));
92
+ const inNonRuntimeRoot = (file) => (nonRuntimeRoots || []).some((root) => {
93
+ const f = String(file || "").replace(/\\/g, "/"), r = String(root || "").replace(/\\/g, "/").replace(/^\.\//, "").replace(/^\/+|\/+$/g, "");
94
+ return !!r && (f === r || f.startsWith(`${r}/`));
95
+ });
96
+ for (const name of managed) declared.add(name);
97
+
98
+ const used = new Map(); // top import → { dist, files:Set, lines:Map }
99
+ for (const e of externalImports) {
100
+ if (e.ecosystem !== "PyPI" || e.builtin || e.unresolved || !e.pkg) continue;
101
+ const top = String(e.spec || e.pkg).split(".")[0];
102
+ let u = used.get(top);
103
+ if (!u) used.set(top, (u = { dist: e.pkg, files: new Set(), lines: new Map() }));
104
+ u.files.add(e.file);
105
+ if (!u.lines.has(e.file)) u.lines.set(e.file, e.line || 0);
106
+ }
107
+ // generous match: does declared dist D cover import top t (dist guess g)?
108
+ const covers = (D, t, g) => {
109
+ const d = pyNorm(D), nt = pyNorm(t), ng = pyNorm(g);
110
+ return d === nt || d === ng || d === `python-${nt}` || d === `${nt}-python` || d === `${nt}-binary` || d.replace(/\d+$/, "") === nt.replace(/\d+$/, "");
111
+ };
112
+
113
+ const configBlob = [...configTexts.values()].join("\n");
114
+ if (present) {
115
+ for (const d of deps) {
116
+ const n = pyNorm(d.name);
117
+ if (managed.has(n) || ignored.has(n)) continue;
118
+ if (d.buildSystem || PY_TOOL_DISTS.has(n) || /^types-|-stubs$|^pytest-|^flake8-|^sphinx/.test(n)) continue;
119
+ let hit = false;
120
+ for (const [top, u] of used) if (covers(d.name, top, u.dist)) { hit = true; break; }
121
+ if (hit || mentioned(configBlob, d.name)) continue;
122
+ findings.push(makeFinding({
123
+ category: "unused",
124
+ rule: "unused-dep",
125
+ severity: d.dev ? "info" : "low",
126
+ confidence: "low",
127
+ title: `Unused Python dependency: ${d.name}`,
128
+ reason: "No recorded Python import maps to this declared distribution; import-to-distribution mapping and plugin loading are heuristic.",
129
+ detail: `"${d.name}" is declared but no .py file imports a module that maps to it. Import-name↔package-name mapping is heuristic and plugins/CLI tools load dynamically — review before removing.`,
130
+ package: d.name,
131
+ source: "internal",
132
+ fixHint: `remove "${d.name}" from the manifest after confirming nothing imports or shells out to it`,
133
+ }));
134
+ }
135
+ }
136
+ for (const [top, u] of used) {
137
+ if (ignored.has(pyNorm(top)) || ignored.has(pyNorm(u.dist)) || managed.has(pyNorm(top)) || managed.has(pyNorm(u.dist))) continue;
138
+ let hit = false;
139
+ for (const D of declared) if (covers(D, top, u.dist)) { hit = true; break; }
140
+ if (hit) continue;
141
+ const files = [...u.files];
142
+ if (files.every(inNonRuntimeRoot)) continue;
143
+ const testOnly = files.every((f) => TEST_PATH_RE.test(f));
144
+ findings.push(makeFinding({
145
+ category: "unused",
146
+ rule: "missing-dep",
147
+ severity: present ? (testOnly ? "low" : "medium") : "low",
148
+ confidence: present ? "medium" : "low",
149
+ title: `Missing Python dependency: ${u.dist}`,
150
+ reason: present
151
+ ? `A recorded Python import maps to "${u.dist}", but no declared distribution covers it.`
152
+ : `A recorded Python import maps to "${u.dist}", but no Python dependency manifest is present; a managed runtime may provide it.`,
153
+ detail: `"${top}" is imported by ${files.length} file(s) but ${present ? "no declared dependency provides it" : "the repo has no Python dependency manifest (requirements.txt / pyproject / Pipfile); a bundled or managed runtime may provide it"}${u.dist !== top ? ` (PyPI package is likely "${u.dist}")` : ""}.${present ? "" : " If this is intentional, declare it under python.managedDependencies in .weavatrix-deps.json."}`,
154
+ package: u.dist,
155
+ file: files[0],
156
+ line: u.lines.get(files[0]) || 0,
157
+ evidence: files.slice(0, 5).map((f) => ({ file: f, line: u.lines.get(f) || 0, snippet: "" })),
158
+ source: "internal",
159
+ fixHint: present ? `pip install ${u.dist} (and add it to requirements.txt / pyproject)` : `add ${u.dist} to a Python manifest, or declare it as a managed runtime dependency`,
160
+ }));
161
+ }
162
+ return { findings, declared, managed };
163
+ }