weavatrix 0.1.0 → 0.1.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/README.md +59 -18
- package/SECURITY.md +31 -0
- package/package.json +16 -4
- package/skill/SKILL.md +16 -10
- package/src/analysis/coverage-reports.js +14 -11
- package/src/analysis/dead-check.js +7 -2
- package/src/analysis/duplicates.compute.js +221 -215
- package/src/analysis/duplicates.js +15 -15
- package/src/analysis/duplicates.run.js +52 -51
- package/src/analysis/duplicates.tokenize.js +182 -182
- package/src/analysis/endpoints.js +5 -3
- package/src/analysis/graph-analysis.aggregate.js +5 -1
- package/src/analysis/internal-audit.collect.js +37 -11
- package/src/analysis/internal-audit.run.js +8 -5
- package/src/build-graph.js +2 -1
- package/src/child-env.js +7 -0
- package/src/graph/internal-builder.build.js +6 -3
- package/src/graph/internal-builder.langs.js +17 -6
- package/src/graph/internal-builder.resolvers.js +10 -3
- package/src/mcp/catalog.mjs +9 -7
- package/src/mcp/graph-context.mjs +8 -5
- package/src/mcp/sync-payload.mjs +102 -0
- package/src/mcp/tools-actions.mjs +22 -13
- package/src/mcp/tools-impact.mjs +3 -2
- package/src/mcp-rg.mjs +2 -1
- package/src/mcp-server.mjs +26 -17
- package/src/mcp-source-tools.mjs +16 -5
- package/src/process.js +4 -3
- package/src/repo-path.js +53 -0
- package/src/security/installed.js +44 -31
- package/src/security/malware-heuristics.exclusions.js +134 -134
- package/src/security/malware-heuristics.js +15 -15
- package/src/security/malware-heuristics.roots.js +142 -142
- package/src/security/malware-heuristics.scan.js +85 -85
- package/src/security/malware-heuristics.sweep.js +122 -122
package/src/mcp-server.mjs
CHANGED
|
@@ -15,15 +15,17 @@
|
|
|
15
15
|
// go to stderr. Two argv forms:
|
|
16
16
|
// weavatrix-mcp <repoRoot> [caps] — graph path derived from the standard layout
|
|
17
17
|
// weavatrix-mcp <graph.json> <repoRoot> [caps] — explicit graph file (classic form)
|
|
18
|
-
import {existsSync, statSync} from 'node:fs'
|
|
18
|
+
import {existsSync, statSync, realpathSync} from 'node:fs'
|
|
19
19
|
import {join, dirname} from 'node:path'
|
|
20
20
|
import {fileURLToPath} from 'node:url'
|
|
21
21
|
import process from 'node:process'
|
|
22
22
|
import {loadGraph} from './mcp/graph-context.mjs'
|
|
23
|
-
import {loadHotApi, HOT_FILES} from './mcp/catalog.mjs'
|
|
24
23
|
import {graphOutDirForRepo} from './graph/layout.js'
|
|
24
|
+
import {createRequire} from 'node:module'
|
|
25
25
|
|
|
26
|
-
|
|
26
|
+
// version comes from package.json so serverInfo can never drift from the published package again
|
|
27
|
+
const PKG_VERSION = (() => { try { return createRequire(import.meta.url)('../package.json').version } catch { return '0.0.0' } })()
|
|
28
|
+
const SERVER_INFO = {name: 'weavatrix', version: PKG_VERSION}
|
|
27
29
|
const DEFAULT_PROTOCOL = '2024-11-05'
|
|
28
30
|
const log = (...a) => process.stderr.write(`[weavatrix] ${a.join(' ')}\n`)
|
|
29
31
|
|
|
@@ -31,19 +33,20 @@ const log = (...a) => process.stderr.write(`[weavatrix] ${a.join(' ')}\n`)
|
|
|
31
33
|
// otherwise it is the graph.json path and the repo root follows it.
|
|
32
34
|
let GRAPH_PATH = process.argv[2]
|
|
33
35
|
let repoArg = process.argv[3]
|
|
34
|
-
// caps ABSENT (undefined) =
|
|
35
|
-
//
|
|
36
|
+
// caps ABSENT (undefined) = offline defaults (including explicit-call repo retargeting, no network).
|
|
37
|
+
// PRESENT (even the empty string) = explicit set — see catalog.loadHotApi.
|
|
36
38
|
let CAPS_ARG = process.argv[4]
|
|
37
39
|
try {
|
|
38
40
|
if (GRAPH_PATH && statSync(GRAPH_PATH).isDirectory()) {
|
|
39
|
-
repoArg =
|
|
41
|
+
repoArg = realpathSync.native(GRAPH_PATH)
|
|
40
42
|
CAPS_ARG = process.argv[3]
|
|
41
43
|
GRAPH_PATH = join(graphOutDirForRepo(repoArg), 'graph.json')
|
|
42
|
-
if (!existsSync(GRAPH_PATH)) log(`no graph built yet for ${repoArg} — ask the agent to call rebuild_graph
|
|
44
|
+
if (!existsSync(GRAPH_PATH)) log(`no graph built yet for ${repoArg} — ask the agent to call rebuild_graph; it builds into the standard weavatrix-graphs layout`)
|
|
43
45
|
}
|
|
44
46
|
} catch { /* argv[2] is not a directory → classic <graph.json> <repoRoot> form */ }
|
|
45
47
|
// repo source root for search_code / read_source; null → those tools degrade.
|
|
46
|
-
|
|
48
|
+
let REPO_ROOT = null
|
|
49
|
+
try { if (repoArg && statSync(repoArg).isDirectory()) REPO_ROOT = realpathSync.native(repoArg) } catch { /* invalid repo root */ }
|
|
47
50
|
|
|
48
51
|
// ---- hot reload of tool implementations -----------------------------------------------------------
|
|
49
52
|
// Node caches modules at spawn, so edits to the tool code would otherwise be invisible until the MCP
|
|
@@ -52,16 +55,19 @@ const REPO_ROOT = repoArg && existsSync(repoArg) ? repoArg : null
|
|
|
52
55
|
// swap the tool table, then notify the client. The stdio shell, graph-context (its caches), and the
|
|
53
56
|
// analysis engines are NOT swapped — changing those still needs a reconnect.
|
|
54
57
|
const MCP_DIR = join(dirname(fileURLToPath(import.meta.url)), 'mcp')
|
|
55
|
-
|
|
58
|
+
const CATALOG_URL = new URL('./mcp/catalog.mjs', import.meta.url)
|
|
59
|
+
const loadCatalog = (version = 0) => import(version ? `${CATALOG_URL.href}?v=${version}` : CATALOG_URL.href)
|
|
60
|
+
function hotVersion(hotFiles) {
|
|
56
61
|
let v = 0
|
|
57
|
-
for (const f of
|
|
62
|
+
for (const f of hotFiles) {
|
|
58
63
|
try { const t = statSync(join(MCP_DIR, f)).mtimeMs; if (t > v) v = t } catch { /* missing file just doesn't bump the version */ }
|
|
59
64
|
}
|
|
60
65
|
return v
|
|
61
66
|
}
|
|
62
67
|
|
|
63
68
|
async function main() {
|
|
64
|
-
let
|
|
69
|
+
let catalog = await loadCatalog()
|
|
70
|
+
let api = await catalog.loadHotApi(0, CAPS_ARG)
|
|
65
71
|
let graph = null
|
|
66
72
|
let graphError = null
|
|
67
73
|
// ctx owns the CURRENT target: rebuild_graph reloads it, open_repo retargets graphPath/repoRoot
|
|
@@ -78,20 +84,23 @@ async function main() {
|
|
|
78
84
|
log(`failed to load graph: ${e.message}`)
|
|
79
85
|
}
|
|
80
86
|
log(`repo root: ${REPO_ROOT || '(none — source/action tools disabled)'}`)
|
|
81
|
-
log(`capabilities: ${
|
|
87
|
+
log(`capabilities: ${[...api.caps].join(',') || '(none)'} (${api.tools.length} tools)`)
|
|
82
88
|
|
|
83
89
|
let protocolVersion = DEFAULT_PROTOCOL
|
|
84
90
|
const send = (msg) => process.stdout.write(JSON.stringify(msg) + '\n')
|
|
85
91
|
const reply = (id, result) => send({jsonrpc: '2.0', id, result})
|
|
86
92
|
const fail = (id, code, message) => send({jsonrpc: '2.0', id, error: {code, message}})
|
|
87
93
|
|
|
88
|
-
let loadedVersion = hotVersion()
|
|
94
|
+
let loadedVersion = hotVersion(catalog.HOT_FILES)
|
|
89
95
|
let lastFailedVersion = 0
|
|
90
96
|
const maybeHotReload = async () => {
|
|
91
|
-
const v = hotVersion()
|
|
97
|
+
const v = hotVersion(catalog.HOT_FILES)
|
|
92
98
|
if (v <= loadedVersion || v === lastFailedVersion) return
|
|
93
99
|
try {
|
|
94
|
-
|
|
100
|
+
const nextCatalog = await loadCatalog(v)
|
|
101
|
+
const nextApi = await nextCatalog.loadHotApi(v, CAPS_ARG)
|
|
102
|
+
catalog = nextCatalog
|
|
103
|
+
api = nextApi
|
|
95
104
|
loadedVersion = v
|
|
96
105
|
log(`hot-reloaded tool implementations from changed source (${api.tools.length} tools)`)
|
|
97
106
|
send({jsonrpc: '2.0', method: 'notifications/tools/list_changed'})
|
|
@@ -118,8 +127,8 @@ async function main() {
|
|
|
118
127
|
await maybeHotReload()
|
|
119
128
|
const tool = api.byName.get(params?.name)
|
|
120
129
|
if (!tool) return reply(id, {content: [{type: 'text', text: `Unknown tool: ${params?.name}`}], isError: true})
|
|
121
|
-
// action tools
|
|
122
|
-
if (!graph && tool.cap !== 'build') return reply(id, {content: [{type: 'text', text: `Graph unavailable: ${graphError}`}], isError: true})
|
|
130
|
+
// action tools can establish/retarget a graph; read tools need one already loaded
|
|
131
|
+
if (!graph && tool.cap !== 'build' && tool.cap !== 'retarget') return reply(id, {content: [{type: 'text', text: `Graph unavailable: ${graphError}`}], isError: true})
|
|
123
132
|
try {
|
|
124
133
|
let text = String(await tool.run(graph, params?.arguments || {}, ctx))
|
|
125
134
|
// Graph answers silently reflect a point-in-time build — surface staleness on every graph tool.
|
package/src/mcp-source-tools.mjs
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
import { existsSync, readFileSync, readdirSync, statSync } from 'node:fs'
|
|
2
2
|
import { spawnSync } from 'node:child_process'
|
|
3
3
|
import { extname, join, relative } from 'node:path'
|
|
4
|
+
import { resolveRepoPath } from './repo-path.js'
|
|
5
|
+
import { childProcessEnv } from './child-env.js'
|
|
4
6
|
|
|
5
7
|
const SEARCH_SKIP = new Set(['.git', 'node_modules', 'dist', 'build', 'out', '.next', 'coverage', 'vendor', '.venv', 'venv', 'env', 'target', '__pycache__', '.idea', '.vscode', '.cache', 'bin', 'obj', 'weavatrix-graphs'])
|
|
6
8
|
const BINARY_EXT = new Set(['.png', '.jpg', '.jpeg', '.gif', '.webp', '.ico', '.pdf', '.zip', '.gz', '.tar', '.exe', '.dll', '.so', '.dylib', '.woff', '.woff2', '.ttf', '.eot', '.mp4', '.mp3', '.wasm', '.class', '.jar', '.node', '.bin'])
|
|
7
9
|
const MAX_SEARCH_FILE_BYTES = 1024 * 1024
|
|
10
|
+
const MAX_SOURCE_FILE_BYTES = 2 * 1024 * 1024
|
|
11
|
+
const MAX_SOURCE_CONTEXT_LINES = 1000
|
|
8
12
|
|
|
9
13
|
function rgSearch(repoRoot, resolveRg, query, { isRegex, glob, maxResults }) {
|
|
10
14
|
const rg = resolveRg()
|
|
@@ -13,7 +17,7 @@ function rgSearch(repoRoot, resolveRg, query, { isRegex, glob, maxResults }) {
|
|
|
13
17
|
if (!isRegex) args.push('--fixed-strings')
|
|
14
18
|
if (glob) args.push('-g', glob)
|
|
15
19
|
args.push('--', query, repoRoot)
|
|
16
|
-
const res = spawnSync(rg, args, { encoding: 'utf8', maxBuffer: 16 * 1024 * 1024, timeout: 15000 })
|
|
20
|
+
const res = spawnSync(rg, args, { encoding: 'utf8', maxBuffer: 16 * 1024 * 1024, timeout: 15000, env: childProcessEnv() })
|
|
17
21
|
if (res.status !== 0 && res.status !== 1) return null
|
|
18
22
|
const out = []
|
|
19
23
|
for (const line of (res.stdout || '').split(/\r?\n/)) {
|
|
@@ -122,14 +126,21 @@ export function readSource({ repoRoot, resolveNode, isSymbol }, g, { label, path
|
|
|
122
126
|
}
|
|
123
127
|
// explicit anchor wins: window = start_line-before .. start_line+after (how a path read escapes the file head)
|
|
124
128
|
if (start_line != null && Number(start_line) > 0) focusLine = Math.floor(Number(start_line))
|
|
125
|
-
const
|
|
126
|
-
if (
|
|
129
|
+
const resolved = resolveRepoPath(repoRoot, file)
|
|
130
|
+
if (resolved.reason === 'escape') return `Refusing to read "${file}": path escapes the repository root.`
|
|
131
|
+
if (resolved.reason === 'not-found') return `File not found: ${file}`
|
|
132
|
+
if (!resolved.ok) return `Could not resolve ${file} inside the repository root.`
|
|
133
|
+
const abs = resolved.path
|
|
134
|
+
let st
|
|
135
|
+
try { st = statSync(abs) } catch (e) { return `Could not inspect ${file}: ${e.message}` }
|
|
136
|
+
if (!st.isFile()) return `Could not read ${file}: not a regular file.`
|
|
137
|
+
if (st.size > MAX_SOURCE_FILE_BYTES) return `Could not read ${file}: file exceeds the ${MAX_SOURCE_FILE_BYTES / 1024 / 1024} MB source-read limit.`
|
|
127
138
|
let text
|
|
128
139
|
try { text = readFileSync(abs, 'utf8') } catch (e) { return `Could not read ${file}: ${e.message}` }
|
|
129
140
|
const lines = text.split(/\r?\n/)
|
|
130
141
|
if (focusLine) focusLine = Math.min(focusLine, lines.length) // an anchor past EOF shows the tail, not nothing
|
|
131
|
-
const b = Math.max(0, Number(before) || 0)
|
|
132
|
-
const a = Math.max(1, Number(after) || 40)
|
|
142
|
+
const b = Math.min(MAX_SOURCE_CONTEXT_LINES, Math.max(0, Number(before) || 0))
|
|
143
|
+
const a = Math.min(MAX_SOURCE_CONTEXT_LINES, Math.max(1, Number(after) || 40))
|
|
133
144
|
const start = focusLine ? Math.max(1, focusLine - b) : 1
|
|
134
145
|
const end = focusLine ? Math.min(lines.length, focusLine + a) : Math.min(lines.length, 1 + b + a)
|
|
135
146
|
const width = String(end).length
|
package/src/process.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// Subprocess runner shared by the search engines and security sweeps.
|
|
2
2
|
import { spawn } from "node:child_process";
|
|
3
|
+
import { childProcessEnv } from "./child-env.js";
|
|
3
4
|
|
|
4
5
|
// Windows: .cmd/.ps1 shims (npx, etc.) can't be spawned directly by Node — they need a
|
|
5
6
|
// shell. With shell:true Node does NOT quote args, so we build a quoted command line ourselves
|
|
@@ -18,13 +19,13 @@ export function runCommand(command, args = [], options = {}) {
|
|
|
18
19
|
const child = needsShell
|
|
19
20
|
? spawn([command, ...args].map(winQuote).join(" "), [], {
|
|
20
21
|
cwd: options.cwd || undefined,
|
|
21
|
-
env:
|
|
22
|
+
env: childProcessEnv(options.env || {}),
|
|
22
23
|
shell: true,
|
|
23
24
|
windowsHide: true
|
|
24
25
|
})
|
|
25
26
|
: spawn(command, args, {
|
|
26
27
|
cwd: options.cwd || undefined,
|
|
27
|
-
env:
|
|
28
|
+
env: childProcessEnv(options.env || {}),
|
|
28
29
|
windowsHide: true
|
|
29
30
|
});
|
|
30
31
|
|
|
@@ -46,7 +47,7 @@ export function runCommand(command, args = [], options = {}) {
|
|
|
46
47
|
const killTree = () => {
|
|
47
48
|
if (process.platform === "win32" && child.pid) {
|
|
48
49
|
try {
|
|
49
|
-
spawn("taskkill", ["/pid", String(child.pid), "/T", "/F"], { windowsHide: true });
|
|
50
|
+
spawn("taskkill", ["/pid", String(child.pid), "/T", "/F"], { windowsHide: true, env: childProcessEnv() });
|
|
50
51
|
} catch {
|
|
51
52
|
try { child.kill(); } catch { /* process may already be gone */ }
|
|
52
53
|
}
|
package/src/repo-path.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { realpathSync } from "node:fs";
|
|
2
|
+
import { isAbsolute, relative, resolve, sep } from "node:path";
|
|
3
|
+
|
|
4
|
+
export function isPathInside(root, target) {
|
|
5
|
+
const rel = relative(root, target);
|
|
6
|
+
return rel === "" || (rel !== ".." && !rel.startsWith(`..${sep}`) && !isAbsolute(rel));
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
// Cache the canonical root for callers that resolve many graph-derived paths.
|
|
10
|
+
export function createRepoBoundary(repoRoot) {
|
|
11
|
+
let root;
|
|
12
|
+
let rootError;
|
|
13
|
+
try {
|
|
14
|
+
root = realpathSync.native(repoRoot);
|
|
15
|
+
} catch (error) {
|
|
16
|
+
rootError = error;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return {
|
|
20
|
+
root,
|
|
21
|
+
resolve(candidate) {
|
|
22
|
+
if (!root) return { ok: false, reason: "invalid-root", error: rootError };
|
|
23
|
+
const input = String(candidate ?? "");
|
|
24
|
+
if (!input || input.includes("\0")) return { ok: false, reason: "invalid-path" };
|
|
25
|
+
if (isAbsolute(input)) return { ok: false, reason: "escape" };
|
|
26
|
+
|
|
27
|
+
let lexical;
|
|
28
|
+
try {
|
|
29
|
+
lexical = resolve(root, input);
|
|
30
|
+
} catch (error) {
|
|
31
|
+
return { ok: false, reason: "invalid-path", error };
|
|
32
|
+
}
|
|
33
|
+
if (!isPathInside(root, lexical)) return { ok: false, reason: "escape" };
|
|
34
|
+
|
|
35
|
+
let target;
|
|
36
|
+
try {
|
|
37
|
+
target = realpathSync.native(lexical);
|
|
38
|
+
} catch (error) {
|
|
39
|
+
return { ok: false, reason: error?.code === "ENOENT" ? "not-found" : "unreadable", error };
|
|
40
|
+
}
|
|
41
|
+
if (!isPathInside(root, target)) return { ok: false, reason: "escape" };
|
|
42
|
+
|
|
43
|
+
return { ok: true, path: target };
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Resolve one existing repo-relative path without allowing lexical traversal or
|
|
49
|
+
// symlink/junction escapes. Callers get a reason instead of an exception so MCP
|
|
50
|
+
// tools can refuse the read without exposing host filesystem details.
|
|
51
|
+
export function resolveRepoPath(repoRoot, candidate) {
|
|
52
|
+
return createRepoBoundary(repoRoot).resolve(candidate);
|
|
53
|
+
}
|
|
@@ -2,10 +2,11 @@
|
|
|
2
2
|
// (package-lock v1/v2/v3, basic yarn.lock, requirements.txt ==pins, go.sum), plus a top-level
|
|
3
3
|
// node_modules walk — which also yields the lockfile-DRIFT signal (installed ≠ locked → tampering or
|
|
4
4
|
// stale install). Parsers are pure + exported for tests; collectInstalled is the thin fs wrapper.
|
|
5
|
-
import {
|
|
6
|
-
import { join } from "node:path";
|
|
5
|
+
import { readFileSync, readdirSync } from "node:fs";
|
|
6
|
+
import { join, relative } from "node:path";
|
|
7
7
|
import { parseGoMod } from "../analysis/manifests.js";
|
|
8
8
|
import { uniqueBy } from "../util.js";
|
|
9
|
+
import { createRepoBoundary } from "../repo-path.js";
|
|
9
10
|
|
|
10
11
|
const pep503 = (name) => String(name).toLowerCase().replace(/[-_.]+/g, "-"); // PyPI canonical name
|
|
11
12
|
|
|
@@ -144,21 +145,19 @@ export function parseGoModPackages(text) {
|
|
|
144
145
|
}
|
|
145
146
|
|
|
146
147
|
// Top-level node_modules walk (incl. @scopes): the ground truth of what is REALLY on disk.
|
|
147
|
-
function walkNodeModules(repoPath) {
|
|
148
|
+
function walkNodeModules(repoPath, { readJsonFile = readJson, readdir = readdirSync } = {}) {
|
|
148
149
|
const out = [];
|
|
149
150
|
const nm = join(repoPath, "node_modules");
|
|
150
151
|
const readPkg = (dir, name) => {
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
if (pj && pj.version) out.push({ ecosystem: "npm", name: pj.name || name, version: pj.version, dev: false, integrity: "", source: "node_modules" });
|
|
154
|
-
} catch { /* not a package dir */ }
|
|
152
|
+
const pj = readJsonFile(join(dir, "package.json"));
|
|
153
|
+
if (pj && pj.version) out.push({ ecosystem: "npm", name: pj.name || name, version: pj.version, dev: false, integrity: "", source: "node_modules" });
|
|
155
154
|
};
|
|
156
155
|
let entries;
|
|
157
|
-
try { entries =
|
|
156
|
+
try { entries = readdir(nm); } catch { return out; }
|
|
158
157
|
for (const e of entries) {
|
|
159
158
|
if (e.startsWith(".")) continue;
|
|
160
159
|
if (e.startsWith("@")) {
|
|
161
|
-
let scoped; try { scoped =
|
|
160
|
+
let scoped; try { scoped = readdir(join(nm, e)); } catch { continue; }
|
|
162
161
|
for (const s of scoped) readPkg(join(nm, e, s), `${e}/${s}`);
|
|
163
162
|
} else readPkg(join(nm, e), e);
|
|
164
163
|
}
|
|
@@ -170,58 +169,72 @@ const readJson = (p) => { try { return JSON.parse(readFileSync(p, "utf8")); } ca
|
|
|
170
169
|
|
|
171
170
|
const SUBPROJECT_SKIP = new Set([".git", ".idea", ".vscode", ".venv", "venv", "env", "node_modules", "vendor", "dist", "build", "coverage", "__pycache__", ".tox", "testdata"]);
|
|
172
171
|
|
|
173
|
-
function projectDirs(
|
|
174
|
-
const dirs = [
|
|
172
|
+
function projectDirs(boundary) {
|
|
173
|
+
const dirs = boundary.root ? [boundary.root] : [];
|
|
175
174
|
let entries = [];
|
|
176
|
-
try { entries = readdirSync(
|
|
175
|
+
try { entries = readdirSync(boundary.root, { withFileTypes: true }); } catch { return dirs; }
|
|
177
176
|
for (const e of entries) {
|
|
178
177
|
if (!e.isDirectory() || e.name.startsWith(".") || SUBPROJECT_SKIP.has(e.name)) continue;
|
|
179
|
-
|
|
178
|
+
const resolved = boundary.resolve(e.name);
|
|
179
|
+
if (resolved.ok) dirs.push(resolved.path);
|
|
180
180
|
if (dirs.length >= 101) break;
|
|
181
181
|
}
|
|
182
182
|
return dirs;
|
|
183
183
|
}
|
|
184
184
|
|
|
185
|
-
function collectReqFiles(dir) {
|
|
185
|
+
function collectReqFiles(dir, readdir = readdirSync) {
|
|
186
186
|
const reqFiles = [];
|
|
187
|
-
try { for (const n of
|
|
188
|
-
try { for (const n of
|
|
187
|
+
try { for (const n of readdir(dir)) if (/^requirements[\w.-]*\.(txt|in)$/i.test(n)) reqFiles.push(join(dir, n)); } catch { /* unreadable root */ }
|
|
188
|
+
try { for (const n of readdir(join(dir, "requirements"))) if (/\.(txt|in)$/i.test(n)) reqFiles.push(join(dir, "requirements", n)); } catch { /* no requirements/ dir */ }
|
|
189
189
|
return reqFiles;
|
|
190
190
|
}
|
|
191
191
|
|
|
192
192
|
// → { installed: [{ecosystem,name,version,dev,source}], drift: [{name, locked, installed}] }
|
|
193
193
|
export function collectInstalled(repoPath) {
|
|
194
|
-
const
|
|
194
|
+
const boundary = createRepoBoundary(repoPath);
|
|
195
|
+
if (!boundary.root) return { installed: [], drift: [] };
|
|
196
|
+
const inside = (path) => boundary.resolve(relative(boundary.root, path) || ".");
|
|
197
|
+
const boundedText = (path) => { const resolved = inside(path); return resolved.ok ? readText(resolved.path) : null; };
|
|
198
|
+
const boundedJson = (path) => { const resolved = inside(path); return resolved.ok ? readJson(resolved.path) : null; };
|
|
199
|
+
const boundedReaddir = (path, options) => {
|
|
200
|
+
const resolved = inside(path);
|
|
201
|
+
if (!resolved.ok) throw new Error("path is outside the repository");
|
|
202
|
+
return readdirSync(resolved.path, options);
|
|
203
|
+
};
|
|
204
|
+
const dirs = projectDirs(boundary);
|
|
195
205
|
const lock = [];
|
|
196
206
|
const yarn = [];
|
|
197
207
|
const disk = [];
|
|
198
208
|
for (const dir of dirs) {
|
|
199
|
-
const lockHere = parsePackageLock(
|
|
209
|
+
const lockHere = parsePackageLock(boundedJson(join(dir, "package-lock.json")) || {});
|
|
200
210
|
lock.push(...lockHere);
|
|
201
|
-
if (!lockHere.length) yarn.push(...parseYarnLock(
|
|
202
|
-
disk.push(...walkNodeModules(dir));
|
|
211
|
+
if (!lockHere.length) yarn.push(...parseYarnLock(boundedText(join(dir, "yarn.lock")) || ""));
|
|
212
|
+
disk.push(...walkNodeModules(dir, { readJsonFile: boundedJson, readdir: boundedReaddir }));
|
|
203
213
|
}
|
|
204
214
|
// Python: venv site-packages = the ground truth (exact installed versions); requirements*.txt (root +
|
|
205
215
|
// a requirements/ dir) and poetry/uv/Pipfile locks fill in when there's no venv. venv wins per name.
|
|
206
|
-
const venvPy = dirs.flatMap((dir) => collectVenvPackages(dir));
|
|
207
|
-
const reqFiles = dirs.flatMap((dir) => collectReqFiles(dir));
|
|
216
|
+
const venvPy = dirs.flatMap((dir) => collectVenvPackages(dir, { readdir: boundedReaddir }));
|
|
217
|
+
const reqFiles = dirs.flatMap((dir) => collectReqFiles(dir, boundedReaddir));
|
|
208
218
|
const venvNames = new Set(venvPy.map((p) => p.name));
|
|
209
219
|
const pyDeclared = [
|
|
210
|
-
...reqFiles.flatMap((f) => parseRequirements(
|
|
211
|
-
...dirs.flatMap((dir) => parseTomlLockPackages(
|
|
212
|
-
...dirs.flatMap((dir) => parseTomlLockPackages(
|
|
213
|
-
...dirs.flatMap((dir) => parsePipfileLock(
|
|
220
|
+
...reqFiles.flatMap((f) => parseRequirements(boundedText(f) || "")),
|
|
221
|
+
...dirs.flatMap((dir) => parseTomlLockPackages(boundedText(join(dir, "poetry.lock")) || "", "poetry-lock")),
|
|
222
|
+
...dirs.flatMap((dir) => parseTomlLockPackages(boundedText(join(dir, "uv.lock")) || "", "uv-lock")),
|
|
223
|
+
...dirs.flatMap((dir) => parsePipfileLock(boundedJson(join(dir, "Pipfile.lock")) || null)),
|
|
214
224
|
].filter((p) => !venvNames.has(p.name)); // installed version supersedes the declared pin
|
|
215
225
|
const py = [...venvPy, ...pyDeclared];
|
|
216
226
|
// Go: go.sum (exact, hashed) first, then go.mod require versions as a fallback. Walk the repo root
|
|
217
227
|
// AND its immediate subdirectories so a Go MONOREPO (per-folder modules, no root go.mod — e.g. gpro)
|
|
218
228
|
// still scans; dedupe() collapses the go.sum/go.mod overlap and cross-module shared deps.
|
|
219
|
-
const goFromDir = (dir) => [...parseGoSum(
|
|
220
|
-
const go = [...goFromDir(
|
|
221
|
-
if (!go.length ||
|
|
229
|
+
const goFromDir = (dir) => [...parseGoSum(boundedText(join(dir, "go.sum")) || ""), ...parseGoModPackages(boundedText(join(dir, "go.mod")) || "")];
|
|
230
|
+
const go = [...goFromDir(boundary.root)];
|
|
231
|
+
if (!go.length || boundary.resolve("go.work").ok) {
|
|
222
232
|
let subs = [];
|
|
223
|
-
try { subs =
|
|
224
|
-
for (const s of subs.slice(0, 100))
|
|
233
|
+
try { subs = boundedReaddir(boundary.root, { withFileTypes: true }).filter((e) => e.isDirectory() && !e.name.startsWith(".") && !["node_modules", "vendor", "dist", "build", "testdata"].includes(e.name)); } catch { /* unreadable root */ }
|
|
234
|
+
for (const s of subs.slice(0, 100)) {
|
|
235
|
+
const resolved = boundary.resolve(s.name);
|
|
236
|
+
if (resolved.ok) go.push(...goFromDir(resolved.path));
|
|
237
|
+
}
|
|
225
238
|
}
|
|
226
239
|
|
|
227
240
|
// lockfile wins as the version source; disk fills gaps + powers the drift signal. A package
|