weavatrix 0.1.2 → 0.1.3
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 +66 -11
- package/package.json +1 -1
- package/skill/SKILL.md +54 -3
- package/src/analysis/dead-check.js +80 -4
- package/src/analysis/dep-check.js +84 -8
- package/src/analysis/dep-rules.js +74 -8
- package/src/analysis/duplicates.compute.js +12 -18
- package/src/analysis/endpoints.js +45 -2
- package/src/analysis/graph-analysis.aggregate.js +11 -3
- package/src/analysis/internal-audit.collect.js +122 -25
- package/src/analysis/internal-audit.reach.js +52 -11
- package/src/analysis/internal-audit.run.js +65 -17
- package/src/graph/builder/lang-js.js +66 -23
- package/src/graph/internal-builder.build.js +48 -10
- package/src/graph/internal-builder.langs.js +43 -2
- package/src/graph/internal-builder.resolvers.js +88 -19
- package/src/mcp/catalog.mjs +2 -2
- package/src/mcp/graph-context.mjs +100 -13
- package/src/mcp/sync-payload.mjs +9 -1
- package/src/mcp/tools-actions.mjs +22 -7
- package/src/mcp/tools-graph.mjs +46 -12
- package/src/mcp/tools-health.mjs +42 -18
- package/src/mcp/tools-impact.mjs +52 -41
- package/src/mcp-server.mjs +3 -0
- package/src/security/advisory-store.js +51 -7
package/src/mcp/tools-health.mjs
CHANGED
|
@@ -38,19 +38,38 @@ export function tFindDuplicates(g, args, ctx) {
|
|
|
38
38
|
if (args.mode === 'semantic') {
|
|
39
39
|
const data = computeDuplicates(ctx.repoRoot, ctx.graphPath, {nameTwins: true})
|
|
40
40
|
const frags = data.frags
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
.filter((
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
41
|
+
const candidates = []
|
|
42
|
+
for (const twin of data.nameTwins || []) {
|
|
43
|
+
const allowed = new Set(twin.members.filter((i) => (!skipTests || !frags[i].test) && frags[i].n >= tokMin))
|
|
44
|
+
const pairs = (twin.pairs || []).filter((p) => allowed.has(p.a) && allowed.has(p.b))
|
|
45
|
+
if (!pairs.length) continue
|
|
46
|
+
const closest = pairs.slice().sort((a, b) => b.similarity - a.similarity)[0]
|
|
47
|
+
const farthest = pairs.slice().sort((a, b) => a.similarity - b.similarity)[0]
|
|
48
|
+
if (closest.similarity >= 85) candidates.push({kind: 'clone', label: twin.label, pair: closest})
|
|
49
|
+
if (farthest.similarity <= 45) candidates.push({kind: 'collision', label: twin.label, pair: farthest})
|
|
50
|
+
}
|
|
51
|
+
for (const item of candidates) item.tokens = frags[item.pair.a].n + frags[item.pair.b].n
|
|
52
|
+
candidates.sort((a, b) => {
|
|
53
|
+
if (a.kind !== b.kind) return a.kind === 'clone' ? -1 : 1
|
|
54
|
+
return a.kind === 'clone'
|
|
55
|
+
? b.pair.similarity - a.pair.similarity || b.tokens - a.tokens
|
|
56
|
+
: b.tokens - a.tokens || a.pair.similarity - b.pair.similarity
|
|
52
57
|
})
|
|
53
|
-
|
|
58
|
+
if (!candidates.length) return 'No actionable same-name pairs across files (semantic mode; ambiguous middle-similarity pairs are suppressed).'
|
|
59
|
+
const top = candidates.slice(0, Math.min(30, Math.max(1, Number(args.top_n) || 15)))
|
|
60
|
+
const lines = top.map((item, k) => {
|
|
61
|
+
const a = frags[item.pair.a]
|
|
62
|
+
const b = frags[item.pair.b]
|
|
63
|
+
const verdict = item.kind === 'clone'
|
|
64
|
+
? 'near-identical duplicate candidate — review, then extract shared logic if the contract is truly shared'
|
|
65
|
+
: 'name collision, not a duplicate — inspect only if these definitions should share a contract'
|
|
66
|
+
return [
|
|
67
|
+
`${k + 1}. "${item.label}" — ${item.pair.similarity}% similar; ${verdict}`,
|
|
68
|
+
` ${a.file}:${a.start}-${a.end} (${a.n} tok)`,
|
|
69
|
+
` ${b.file}:${b.start}-${b.end} (${b.n} tok)`,
|
|
70
|
+
].join('\n')
|
|
71
|
+
})
|
|
72
|
+
return `Found ${candidates.length} actionable same-name pair(s) across files (semantic mode; one closest clone and/or farthest collision per name). Top ${top.length}:\n\n${lines.join('\n\n')}\n\nThese are review candidates, not automatic refactors. Use read_source on both sites before changing code.`
|
|
54
73
|
}
|
|
55
74
|
const data = computeDuplicates(ctx.repoRoot, ctx.graphPath, {includeStrings})
|
|
56
75
|
const groups = groupClones(data, {simMin, tokMin, mode, skipTests})
|
|
@@ -87,14 +106,15 @@ export async function tRunAudit(g, args, ctx) {
|
|
|
87
106
|
const sev = audit.summary.bySeverity
|
|
88
107
|
const bycat = audit.summary.byCategory
|
|
89
108
|
const line = (f) => {
|
|
90
|
-
const where = f.file ? ` (${f.file}${f.symbol ? ` ${f.symbol}` : ''})` : f.package ? ` (pkg ${f.package}${f.version ? `@${f.version}` : ''})` : ''
|
|
109
|
+
const where = f.file ? ` (${f.file}${f.symbol ? ` ${f.symbol}` : ''})` : f.package ? ` (pkg ${f.package}${f.version ? `@${f.version}` : ''}${f.manifest ? `; ${f.manifest}` : ''})` : ''
|
|
91
110
|
return ` [${f.severity}/${f.confidence || '?'}] ${f.rule}: ${f.title}${where}${f.fixHint ? `\n fix: ${f.fixHint}` : ''}`
|
|
92
111
|
}
|
|
112
|
+
const check = (name, state) => `${name} ${state?.status || 'ERROR'}${state?.detail ? ` — ${state.detail}` : ''}`
|
|
93
113
|
return [
|
|
94
114
|
`Internal audit of ${audit.repo} (${audit.scanned.files} files, ${audit.scanned.symbols} symbols, ${audit.scanned.externalImports} external imports; malware scan: ${audit.scanned.malwareScanMode}).`,
|
|
95
115
|
`Severity: critical ${sev.critical}, high ${sev.high}, medium ${sev.medium}, low ${sev.low}, info ${sev.info}. Categories: unused ${bycat.unused}, structure ${bycat.structure}, vulnerability ${bycat.vulnerability}, malware ${bycat.malware}.`,
|
|
96
|
-
`Structure: ${audit.structureReport?.cycles ?? 0} cycle(s), ${audit.structureReport?.orphans ?? 0} orphan(s). Dead: ${audit.deadReport.deadFiles} file(s), ${audit.deadReport.unusedExports} unused export(s).`,
|
|
97
|
-
|
|
116
|
+
`Structure: ${audit.structureReport?.runtimeCycles ?? audit.structureReport?.cycles ?? 0} runtime cycle(s), ${audit.structureReport?.typeCouplings ?? 0} type-induced coupling group(s), ${audit.structureReport?.orphans ?? 0} orphan(s); import edges: ${audit.structureReport?.runtimeImportEdges ?? audit.structureReport?.importEdges ?? 0} runtime + ${audit.structureReport?.typeOnlyImportEdges ?? 0} type-only. Dead: ${audit.deadReport.deadFiles} file(s), ${audit.deadReport.unusedExports} unused export(s).`,
|
|
117
|
+
`Checks: ${check('OSV', audit.checks?.osv)}; ${check('malware', audit.checks?.malware)}. A NOT_CHECKED/PARTIAL/ERROR check is incomplete or unknown, never a clean zero.`,
|
|
98
118
|
``,
|
|
99
119
|
`Showing ${shown.length} of ${filtered.length} finding(s)${cat ? ` in category "${cat}"` : ''}${args.min_severity ? ` at ≥${args.min_severity}` : ''}:`,
|
|
100
120
|
...shown.map(line),
|
|
@@ -120,13 +140,17 @@ export function tModuleMap(g, args, ctx) {
|
|
|
120
140
|
const topN = Math.max(1, Math.min(60, Number(args.top_n) || 25))
|
|
121
141
|
const mods = agg.modules.slice(0, topN)
|
|
122
142
|
const edges = agg.moduleEdges.slice(0, Math.min(50, topN * 2))
|
|
143
|
+
const typeEdges = (agg.typeOnlyModuleEdges || []).slice(0, Math.min(50, topN * 2))
|
|
123
144
|
return [
|
|
124
|
-
`Module map: ${agg.totals.files} files in ${agg.modules.length} folder-modules, ${agg.totals.moduleEdges} module
|
|
145
|
+
`Module map: ${agg.totals.files} files in ${agg.modules.length} folder-modules, ${agg.totals.moduleEdges} runtime module dependencies and ${agg.totals.typeOnlyModuleEdges || 0} type-only dependencies. Top ${mods.length}:`,
|
|
125
146
|
...mods.map((m) => ` ${m.name} — ${m.fileCount} files, ${m.symbolCount} symbols`),
|
|
126
147
|
``,
|
|
127
|
-
`Strongest module dependencies:`,
|
|
148
|
+
`Strongest runtime module dependencies:`,
|
|
128
149
|
...edges.map((e) => ` ${e.from} → ${e.to} (${e.count})`),
|
|
129
|
-
|
|
150
|
+
typeEdges.length ? `` : null,
|
|
151
|
+
typeEdges.length ? `Type-only module dependencies (compile-time contracts, not runtime coupling):` : null,
|
|
152
|
+
...typeEdges.map((e) => ` ${e.from} → ${e.to} (${e.count})`),
|
|
153
|
+
].filter((line) => line != null).join('\n')
|
|
130
154
|
}
|
|
131
155
|
|
|
132
156
|
// Coverage × graph: map an EXISTING coverage report (istanbul/lcov/coverage.py/Go — read offline,
|
package/src/mcp/tools-impact.mjs
CHANGED
|
@@ -10,6 +10,46 @@ import {
|
|
|
10
10
|
} from './graph-context.mjs'
|
|
11
11
|
import {readCoverageForRepo} from '../analysis/coverage-reports.js'
|
|
12
12
|
|
|
13
|
+
function reverseReach(g, seeds, maxDepth) {
|
|
14
|
+
const states = new Map([...seeds].map((id) => [String(id), {
|
|
15
|
+
runtimeDepth: 0, runtimeRelation: null, compileDepth: null, compileRelation: null,
|
|
16
|
+
}]))
|
|
17
|
+
const frontier = [...seeds].map((id) => ({id: String(id), depth: 0, compileOnly: false}))
|
|
18
|
+
for (let cursor = 0; cursor < frontier.length; cursor++) {
|
|
19
|
+
const current = frontier[cursor]
|
|
20
|
+
if (current.depth >= maxDepth) continue
|
|
21
|
+
for (const e of g.inn.get(current.id) || []) {
|
|
22
|
+
if (e.relation === 'contains') continue
|
|
23
|
+
const id = String(e.id)
|
|
24
|
+
const compileOnly = current.compileOnly || e.typeOnly === true
|
|
25
|
+
const depth = current.depth + 1
|
|
26
|
+
const entry = states.get(id) || {
|
|
27
|
+
runtimeDepth: null, runtimeRelation: null, compileDepth: null, compileRelation: null,
|
|
28
|
+
}
|
|
29
|
+
const depthKey = compileOnly ? 'compileDepth' : 'runtimeDepth'
|
|
30
|
+
const relationKey = compileOnly ? 'compileRelation' : 'runtimeRelation'
|
|
31
|
+
if (entry[depthKey] != null && entry[depthKey] <= depth) continue
|
|
32
|
+
entry[depthKey] = depth
|
|
33
|
+
entry[relationKey] = e.relation || 'rel'
|
|
34
|
+
states.set(id, entry)
|
|
35
|
+
frontier.push({id, depth, compileOnly})
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return new Map([...states].map(([id, entry]) => [id, {
|
|
39
|
+
...entry,
|
|
40
|
+
depth: entry.runtimeDepth ?? entry.compileDepth ?? 0,
|
|
41
|
+
compileOnly: entry.runtimeDepth == null,
|
|
42
|
+
relation: entry.runtimeDepth != null ? entry.runtimeRelation : entry.compileRelation,
|
|
43
|
+
}]))
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const impactKind = (entry) => {
|
|
47
|
+
if (entry?.runtimeDepth != null) {
|
|
48
|
+
return entry.compileDepth != null ? `runtime + compile-time(d${entry.compileDepth})` : 'runtime'
|
|
49
|
+
}
|
|
50
|
+
return 'compile-time'
|
|
51
|
+
}
|
|
52
|
+
|
|
13
53
|
// Transitive blast-radius: who is affected if this node changes. Walks REVERSE dependency edges
|
|
14
54
|
// (calls/imports/inherits — not structural `contains`) out to `depth`. For a symbol, also seeds its
|
|
15
55
|
// containing file, because importers depend on the file rather than the individual symbol.
|
|
@@ -30,26 +70,10 @@ export function tGetDependents(g, {label, depth = 3, max_nodes = 40} = {}) {
|
|
|
30
70
|
seeds.add(containingFile)
|
|
31
71
|
}
|
|
32
72
|
}
|
|
33
|
-
const
|
|
34
|
-
const
|
|
35
|
-
let frontier = [...seeds]
|
|
36
|
-
for (let d = 0; d < maxDepth && frontier.length; d++) {
|
|
37
|
-
const next = []
|
|
38
|
-
for (const cur of frontier) {
|
|
39
|
-
for (const e of g.inn.get(cur) || []) {
|
|
40
|
-
if (e.relation === 'contains') continue // structural nesting is not a dependency
|
|
41
|
-
const nid = String(e.id)
|
|
42
|
-
if (depthOf.has(nid)) continue
|
|
43
|
-
depthOf.set(nid, d + 1)
|
|
44
|
-
relOf.set(nid, e.relation || 'rel')
|
|
45
|
-
next.push(nid)
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
frontier = next
|
|
49
|
-
}
|
|
50
|
-
const ranked = [...depthOf.entries()]
|
|
73
|
+
const reached = reverseReach(g, seeds, maxDepth)
|
|
74
|
+
const ranked = [...reached.entries()]
|
|
51
75
|
.filter(([nid]) => !seeds.has(nid))
|
|
52
|
-
.map(([nid,
|
|
76
|
+
.map(([nid, entry]) => ({id: nid, d: entry.depth, entry, deg: degreeOf(g, nid)}))
|
|
53
77
|
.sort((a, b) => a.d - b.d || b.deg - a.deg)
|
|
54
78
|
if (!ranked.length) return [note, `No dependents found for ${n.label ?? id} within depth ${maxDepth} — nothing in the graph calls, imports, or inherits it.`].filter(Boolean).join('\n')
|
|
55
79
|
const shown = ranked.slice(0, cap)
|
|
@@ -57,7 +81,7 @@ export function tGetDependents(g, {label, depth = 3, max_nodes = 40} = {}) {
|
|
|
57
81
|
note,
|
|
58
82
|
`Dependents of ${n.label ?? id} (reverse calls/imports/inherits, depth ≤${maxDepth}): ${ranked.length} found, showing ${shown.length} by proximity + connectivity.`,
|
|
59
83
|
containingFile ? `Includes importers of its containing file ${labelOf(g, containingFile)}.` : null,
|
|
60
|
-
...shown.map((r) => ` [d${r.d}
|
|
84
|
+
...shown.map((r) => ` [d${r.d} ${impactKind(r.entry)}] ${r.entry.relation || 'rel'} ${labelOf(g, r.id)} (deg ${r.deg}) [${r.id}]`),
|
|
61
85
|
].filter(Boolean).join('\n')
|
|
62
86
|
}
|
|
63
87
|
|
|
@@ -72,7 +96,8 @@ export function tGraphDiff(g, args, ctx) {
|
|
|
72
96
|
const filter = args.path ? String(args.path).replace(/\\/g, '/') : null
|
|
73
97
|
const scope = (graph) => filter ? {
|
|
74
98
|
nodes: (graph.nodes || []).filter((n) => String(n.id).startsWith(filter)),
|
|
75
|
-
links: (graph.links || []).filter((l) => String(edgeEndpoint(l.source)).startsWith(filter) || String(edgeEndpoint(l.target)).startsWith(filter))
|
|
99
|
+
links: (graph.links || []).filter((l) => String(edgeEndpoint(l.source)).startsWith(filter) || String(edgeEndpoint(l.target)).startsWith(filter)),
|
|
100
|
+
edgeTypesV: graph.edgeTypesV || 0,
|
|
76
101
|
} : graph
|
|
77
102
|
return [
|
|
78
103
|
`Graph diff (previous rebuild state → current)${filter ? `, scoped to ${filter}` : ''}:`,
|
|
@@ -144,27 +169,11 @@ export function tChangeImpact(g, args, ctx) {
|
|
|
144
169
|
|
|
145
170
|
const maxDepth = Math.max(1, Math.min(4, Number(args.depth) || 2))
|
|
146
171
|
const cap = Math.max(5, Math.min(120, Number(args.max_nodes) || 40))
|
|
147
|
-
const
|
|
148
|
-
const relOf = new Map()
|
|
149
|
-
let frontier = [...seeds]
|
|
150
|
-
for (let d = 0; d < maxDepth && frontier.length; d++) {
|
|
151
|
-
const next = []
|
|
152
|
-
for (const cur of frontier) {
|
|
153
|
-
for (const e of g.inn.get(cur) || []) {
|
|
154
|
-
if (e.relation === 'contains') continue
|
|
155
|
-
const nid = String(e.id)
|
|
156
|
-
if (depthOf.has(nid)) continue
|
|
157
|
-
depthOf.set(nid, d + 1)
|
|
158
|
-
relOf.set(nid, e.relation || 'rel')
|
|
159
|
-
next.push(nid)
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
frontier = next
|
|
163
|
-
}
|
|
172
|
+
const reached = reverseReach(g, seeds, maxDepth)
|
|
164
173
|
const fileOfNode = (id) => { const s = String(id); const h = s.indexOf('#'); return h < 0 ? s : s.slice(0, h) }
|
|
165
|
-
const impacted = [...
|
|
174
|
+
const impacted = [...reached.entries()]
|
|
166
175
|
.filter(([nid]) => !seeds.has(nid) && !changedSet.has(fileOfNode(nid)))
|
|
167
|
-
.map(([nid,
|
|
176
|
+
.map(([nid, entry]) => ({id: nid, d: entry.depth, entry, deg: degreeOf(g, nid), file: fileOfNode(nid)}))
|
|
168
177
|
.sort((a, b) => a.d - b.d || b.deg - a.deg)
|
|
169
178
|
|
|
170
179
|
// coverage overlay from EXISTING reports (fractions 0..1) — see coverage_map for details
|
|
@@ -172,6 +181,7 @@ export function tChangeImpact(g, args, ctx) {
|
|
|
172
181
|
const coverage = readCoverageForRepo(ctx.repoRoot, knownFiles)
|
|
173
182
|
const covOf = (file) => coverage.get(String(file).replace(/\\/g, '/'))?.pct ?? null
|
|
174
183
|
const pctStr = (v) => (v == null ? 'cov n/a' : `cov ${Math.round(v * 100)}%`)
|
|
184
|
+
const hasCoverage = coverage.size > 0
|
|
175
185
|
|
|
176
186
|
const shown = impacted.slice(0, cap)
|
|
177
187
|
const untestedHotspots = shown.filter((n) => { const c = covOf(n.file); return c != null && c < 0.5 && n.deg >= 5 })
|
|
@@ -180,7 +190,8 @@ export function tChangeImpact(g, args, ctx) {
|
|
|
180
190
|
impacted.length
|
|
181
191
|
? `${impacted.length} impacted node(s) within ${maxDepth} reverse hop(s), showing ${shown.length}:`
|
|
182
192
|
: `Nothing else in the graph depends on the changed code within ${maxDepth} hop(s).`,
|
|
183
|
-
|
|
193
|
+
hasCoverage ? `Coverage: existing report mapped where available.` : `Coverage: unavailable (no supported report found); per-node coverage labels omitted.`,
|
|
194
|
+
...shown.map((n) => ` [d${n.d} ${impactKind(n.entry)}] ${n.entry.relation || 'rel'} ${labelOf(g, n.id)} (deg ${n.deg}${hasCoverage ? `, ${pctStr(covOf(n.file))}` : ''}) [${n.id}]`),
|
|
184
195
|
untestedHotspots.length ? `` : null,
|
|
185
196
|
untestedHotspots.length ? `Untested hotspots in the blast radius (<50% coverage, deg ≥5) — cover these before shipping:` : null,
|
|
186
197
|
...untestedHotspots.slice(0, 10).map((n) => ` ${labelOf(g, n.id)} (${pctStr(covOf(n.file))}, deg ${n.deg}) ${n.file}`),
|
package/src/mcp-server.mjs
CHANGED
|
@@ -131,6 +131,9 @@ async function main() {
|
|
|
131
131
|
if (!graph && tool.cap !== 'build' && tool.cap !== 'retarget') return reply(id, {content: [{type: 'text', text: `Graph unavailable: ${graphError}`}], isError: true})
|
|
132
132
|
try {
|
|
133
133
|
let text = String(await tool.run(graph, params?.arguments || {}, ctx))
|
|
134
|
+
if (graph && graph.edgeTypesV < 1 && (tool.cap === 'graph' || tool.cap === 'health')) {
|
|
135
|
+
text += '\n\nWarning: this saved graph predates typed import edges, so type-only imports cannot be separated from runtime dependencies. Call rebuild_graph once before acting on cycle, boundary, dependency, or blast-radius findings.'
|
|
136
|
+
}
|
|
134
137
|
// Graph answers silently reflect a point-in-time build — surface staleness on every graph tool.
|
|
135
138
|
if (tool.cap === 'graph') {
|
|
136
139
|
const warn = api.stalenessLine(ctx)
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
import { existsSync, readFileSync, writeFileSync, mkdirSync } from "node:fs";
|
|
11
11
|
import { join, dirname } from "node:path";
|
|
12
12
|
import { homedir } from "node:os";
|
|
13
|
+
import { createHash } from "node:crypto";
|
|
13
14
|
import { uniqueBy } from "../util.js";
|
|
14
15
|
|
|
15
16
|
export const DEFAULT_STORE = join(homedir(), ".weavatrix", "advisories.json");
|
|
@@ -22,6 +23,13 @@ const keyOf = (ecosystem, name) => `${ecosystem}|${ecosystem === "PyPI" ? String
|
|
|
22
23
|
|
|
23
24
|
const uniquePackages = (pkgs) => uniqueBy(pkgs, (p) => `${p.ecosystem}|${p.name}|${p.version}`);
|
|
24
25
|
|
|
26
|
+
export function advisoryQueryFingerprint(installed = []) {
|
|
27
|
+
const rows = uniquePackages(installed.filter((p) => p?.ecosystem && p?.name && p?.version && OSV_SUPPORTED_ECOSYSTEMS.has(p.ecosystem)))
|
|
28
|
+
.map((p) => `${p.ecosystem}|${p.name}|${p.version}`)
|
|
29
|
+
.sort();
|
|
30
|
+
return createHash("sha256").update(rows.join("\n"), "utf8").digest("hex");
|
|
31
|
+
}
|
|
32
|
+
|
|
25
33
|
async function fetchJson(fetcher, url, options, timeoutMs) {
|
|
26
34
|
const timeout = Math.max(50, Number(timeoutMs) || DEFAULT_FETCH_TIMEOUT_MS);
|
|
27
35
|
const ctrl = typeof AbortController === "function" ? new AbortController() : null;
|
|
@@ -119,6 +127,7 @@ export async function refreshAdvisories({ installed = [], storePath = DEFAULT_ST
|
|
|
119
127
|
const store = loadStore(storePath);
|
|
120
128
|
const idsByPkg = new Map(); // pkgIndex -> [vuln ids]
|
|
121
129
|
const errors = [];
|
|
130
|
+
let queriedOk = 0;
|
|
122
131
|
|
|
123
132
|
for (let i = 0; i < pkgs.length; i += batchSize) {
|
|
124
133
|
const batch = pkgs.slice(i, i + batchSize);
|
|
@@ -128,7 +137,22 @@ export async function refreshAdvisories({ installed = [], storePath = DEFAULT_ST
|
|
|
128
137
|
headers: { "Content-Type": "application/json" },
|
|
129
138
|
body: JSON.stringify({ queries: batch.map((p) => ({ package: { ecosystem: p.ecosystem, name: p.name }, version: p.version })) }),
|
|
130
139
|
}, timeoutMs);
|
|
131
|
-
|
|
140
|
+
if (!Array.isArray(json?.results) || json.results.length !== batch.length) {
|
|
141
|
+
throw new Error(`OSV querybatch returned ${Array.isArray(json?.results) ? json.results.length : "no"} result(s) for ${batch.length} query item(s)`);
|
|
142
|
+
}
|
|
143
|
+
for (const [resultIndex, result] of json.results.entries()) {
|
|
144
|
+
if (!result || typeof result !== "object" || Array.isArray(result)) {
|
|
145
|
+
throw new Error(`OSV querybatch result ${resultIndex + 1} is not an object`);
|
|
146
|
+
}
|
|
147
|
+
if (result.vulns !== undefined && !Array.isArray(result.vulns)) {
|
|
148
|
+
throw new Error(`OSV querybatch result ${resultIndex + 1} has a non-array vulns field`);
|
|
149
|
+
}
|
|
150
|
+
if (Array.isArray(result.vulns) && result.vulns.some((v) => !v || typeof v.id !== "string" || !v.id.trim())) {
|
|
151
|
+
throw new Error(`OSV querybatch result ${resultIndex + 1} contains an advisory without a valid id`);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
queriedOk += batch.length;
|
|
155
|
+
json.results.forEach((r, j) => { if (r && Array.isArray(r.vulns) && r.vulns.length) idsByPkg.set(i + j, r.vulns.map((v) => v.id)); });
|
|
132
156
|
} catch (error) {
|
|
133
157
|
errors.push(`querybatch ${Math.floor(i / batchSize) + 1}/${Math.ceil(pkgs.length / batchSize)}: ${error.message}`);
|
|
134
158
|
}
|
|
@@ -141,16 +165,24 @@ export async function refreshAdvisories({ installed = [], storePath = DEFAULT_ST
|
|
|
141
165
|
for (const [id, pkgList] of wanted) {
|
|
142
166
|
try {
|
|
143
167
|
const rec = await fetchJson(fetcher, OSV_VULN_URL + encodeURIComponent(id), {}, timeoutMs);
|
|
144
|
-
if (!rec || !rec.id)
|
|
145
|
-
|
|
168
|
+
if (!rec || typeof rec.id !== "string" || !rec.id) throw new Error("OSV detail response is missing its advisory id");
|
|
169
|
+
if (rec.id !== id) throw new Error(`OSV detail id mismatch (expected ${id}, received ${rec.id})`);
|
|
170
|
+
let normalized = 0;
|
|
146
171
|
for (const p of pkgList) {
|
|
147
172
|
const row = normalizeRecord(rec, p.ecosystem, p.name);
|
|
148
|
-
if (!row)
|
|
173
|
+
if (!row) {
|
|
174
|
+
errors.push(`${id}: OSV detail does not describe ${p.ecosystem}:${p.name} reported by querybatch`);
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
normalized++;
|
|
149
178
|
const key = keyOf(p.ecosystem, p.name);
|
|
150
179
|
const list = store.records[key] || (store.records[key] = []);
|
|
151
180
|
const at = list.findIndex((x) => x.id === row.id);
|
|
152
181
|
if (at >= 0) list[at] = row; else list.push(row);
|
|
153
182
|
}
|
|
183
|
+
// Count a detail response as fetched only after at least one package-specific row was
|
|
184
|
+
// validated. A malformed or unrelated response must never make coverage look complete.
|
|
185
|
+
if (normalized > 0) fetched++;
|
|
154
186
|
} catch (error) {
|
|
155
187
|
errors.push(`${id}: ${error.message}`);
|
|
156
188
|
}
|
|
@@ -158,20 +190,32 @@ export async function refreshAdvisories({ installed = [], storePath = DEFAULT_ST
|
|
|
158
190
|
|
|
159
191
|
// A refresh where NOTHING was fetched but errors occurred (offline, OSV blocked) must NOT stamp
|
|
160
192
|
// fetched_at — that would turn an empty cache into "No known vulnerabilities as of <today>".
|
|
161
|
-
if (errors.length &&
|
|
193
|
+
if (errors.length && queriedOk === 0) {
|
|
162
194
|
return { ok: false, queried: pkgs.length, unsupported, error: `advisory refresh failed: ${errors[0]}${errors.length > 1 ? ` (+${errors.length - 1} more)` : ""}`, errors };
|
|
163
195
|
}
|
|
164
196
|
store.meta.fetched_at = new Date().toISOString();
|
|
197
|
+
const status = errors.length ? "PARTIAL" : "OK";
|
|
165
198
|
// per-repo stamp: the cache only covers packages that were QUERIED — a repo that never refreshed must
|
|
166
199
|
// show "fetch advisories", not "0 vulnerabilities as of <someone else's date>" (false assurance).
|
|
167
200
|
// repoKeys[] lets one online pass (a "refresh all repos") stamp every repo whose packages it covered.
|
|
168
201
|
const stampRepos = [...new Set([repoKey, ...repoKeys].filter(Boolean))];
|
|
169
|
-
if (stampRepos.length) {
|
|
202
|
+
if (stampRepos.length) {
|
|
203
|
+
store.meta.repos = store.meta.repos || {};
|
|
204
|
+
for (const k of stampRepos) store.meta.repos[k] = {
|
|
205
|
+
fetched_at: store.meta.fetched_at,
|
|
206
|
+
status,
|
|
207
|
+
queried: pkgs.length,
|
|
208
|
+
queried_ok: queriedOk,
|
|
209
|
+
unsupported,
|
|
210
|
+
error_count: errors.length,
|
|
211
|
+
query_fingerprint: advisoryQueryFingerprint(pkgs),
|
|
212
|
+
};
|
|
213
|
+
}
|
|
170
214
|
try {
|
|
171
215
|
mkdirSync(dirname(storePath), { recursive: true });
|
|
172
216
|
writeFileSync(storePath, JSON.stringify(store), "utf8");
|
|
173
217
|
} catch (error) {
|
|
174
218
|
return { ok: false, error: `store write failed: ${error.message}`, errors };
|
|
175
219
|
}
|
|
176
|
-
return { ok: true, queried: pkgs.length, unsupported, vulnerable: wanted.size, fetched, saved: existsSync(storePath), errors };
|
|
220
|
+
return { ok: true, status, queried: pkgs.length, queriedOk, unsupported, vulnerable: wanted.size, fetched, saved: existsSync(storePath), errors };
|
|
177
221
|
}
|