code-guardian-ai 0.0.0-stage → 0.3.1
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/.env.example +24 -0
- package/LICENSE +21 -0
- package/README.md +774 -2
- package/bin/cli.js +2 -0
- package/dist/build-manifest.json +136 -0
- package/dist/cli/commands/check.js +56 -0
- package/dist/cli/commands/doctor.js +55 -0
- package/dist/cli/commands/find.js +31 -0
- package/dist/cli/commands/init-files.js +13 -0
- package/dist/cli/commands/init.js +50 -0
- package/dist/cli/commands/optimize.js +53 -0
- package/dist/cli/commands/scan.js +47 -0
- package/dist/cli/index.js +62 -0
- package/dist/cli/output.js +4 -0
- package/dist/context.js +11 -0
- package/dist/core/ast/analysis.js +455 -0
- package/dist/core/ast/cached-info.js +46 -0
- package/dist/core/ast/discovery.js +485 -0
- package/dist/core/ast/extract-js.js +531 -0
- package/dist/core/ast/extract-py.js +298 -0
- package/dist/core/ast/graph.js +92 -0
- package/dist/core/ast/health.js +338 -0
- package/dist/core/ast/indexer.js +286 -0
- package/dist/core/ast/packages.js +343 -0
- package/dist/core/ast/parser.js +91 -0
- package/dist/core/ast/report.js +154 -0
- package/dist/core/ast/resolver.js +392 -0
- package/dist/core/ast/rules.js +37 -0
- package/dist/core/ast/scope-js.js +191 -0
- package/dist/core/ast/shared-index.js +131 -0
- package/dist/core/ast/tsconfig.js +301 -0
- package/dist/core/ast/types.js +1 -0
- package/dist/core/ast/util.js +156 -0
- package/dist/core/git/git.js +430 -0
- package/dist/core/llm/policy.js +35 -0
- package/dist/core/llm/provider.js +199 -0
- package/dist/core/prompt/compressor.js +118 -0
- package/dist/core/prompt/criteria.js +36 -0
- package/dist/core/prompt/find.js +47 -0
- package/dist/core/prompt/guidelines.js +22 -0
- package/dist/core/prompt/model-output.js +178 -0
- package/dist/core/prompt/refiner.js +194 -0
- package/dist/core/prompt/relevance.js +265 -0
- package/dist/core/prompt/untrusted.js +57 -0
- package/dist/core/security/masker.js +330 -0
- package/dist/core/security/outbound.js +40 -0
- package/dist/core/security/safe-file.js +396 -0
- package/dist/core/security/sandbox.js +181 -0
- package/dist/core/validation/baseline.js +121 -0
- package/dist/core/validation/delta.js +243 -0
- package/dist/core/validation/policy.js +123 -0
- package/dist/db/cache-key.js +221 -0
- package/dist/db/client.js +563 -0
- package/dist/db/schema-check.js +149 -0
- package/dist/db/schema.js +36 -0
- package/dist/db/windows-acl.js +150 -0
- package/dist/index.js +55 -0
- package/dist/mcp/server.js +20 -0
- package/dist/mcp/tools/find-reusable.js +22 -0
- package/dist/mcp/tools/get-context.js +39 -0
- package/dist/mcp/tools/optimize-prompt.js +31 -0
- package/dist/mcp/tools/result.js +31 -0
- package/dist/mcp/tools/validate-rules.js +28 -0
- package/dist/utils/config.js +117 -0
- package/dist/utils/errors.js +9 -0
- package/dist/utils/logger.js +19 -0
- package/dist/utils/pace.js +19 -0
- package/dist/utils/text.js +22 -0
- package/dist/utils/tokens.js +14 -0
- package/package.json +69 -4
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
const FRAMEWORK_DEPENDENCIES = [
|
|
3
|
+
['nuxt', /^(?:nuxt|nuxt3|@nuxt\/kit)$/],
|
|
4
|
+
['sveltekit', /^@sveltejs\/kit$/],
|
|
5
|
+
['next', /^next$/],
|
|
6
|
+
['vite', /^vite$/],
|
|
7
|
+
['astro', /^astro$/],
|
|
8
|
+
];
|
|
9
|
+
const asObject = (value) => typeof value === 'object' && value !== null && !Array.isArray(value) ? value : undefined;
|
|
10
|
+
const str = (value) => (typeof value === 'string' && value ? value : undefined);
|
|
11
|
+
const dirOf = (file) => (path.posix.dirname(file) === '.' ? '' : path.posix.dirname(file));
|
|
12
|
+
// Console-script entry points declared in pyproject.toml ([project.scripts], [project.gui-scripts], [tool.poetry.scripts]):
|
|
13
|
+
// `name = "pkg.module:function"` makes pkg/module.py (or pkg/module/__init__.py) a file that is run, not imported.
|
|
14
|
+
// Under [tool.poetry.scripts] Poetry also accepts a table, read by inlineScriptTargets below:
|
|
15
|
+
// `name = { reference = "pkg/tool.py", type = "file" }`.
|
|
16
|
+
const SCRIPT_TABLES = new Set(['project.scripts', 'project.gui-scripts', 'tool.poetry.scripts']);
|
|
17
|
+
// The dotted name of a table header, however TOML spells it: `[ project . "scripts" ] # comment` is project.scripts.
|
|
18
|
+
function tableName(header) {
|
|
19
|
+
const inner = /^\[\s*(.*?)\s*\]\s*(?:#.*)?$/.exec(header)?.[1] ?? '';
|
|
20
|
+
return inner.split('.').map((part) => part.trim().replace(/^(["'])(.*)\1$/, '$2')).join('.');
|
|
21
|
+
}
|
|
22
|
+
// A script declared as a string: `name = "pkg.mod:fn"`, where the name may be bare, "quoted" or 'quoted', and the value may be
|
|
23
|
+
// followed by extras (`pkg.mod:fn [extra]`). Returns null for anything else that TOML allows (an escape sequence, a module name
|
|
24
|
+
// that is not an identifier), so the caller can report it instead of dropping it.
|
|
25
|
+
const STRING_ENTRY = /^(?:[\w.-]+|"[^"]+"|'[^']+')\s*=\s*(?:"([^"\\]*)"|'([^']*)')\s*(?:#.*)?$/;
|
|
26
|
+
const CALLABLE = /^([A-Za-z_][\w.]*)(?:\s*:\s*[\w.]+)?(?:\s*\[[^\]]*\])?$/;
|
|
27
|
+
function stringScriptTargets(line) {
|
|
28
|
+
const match = STRING_ENTRY.exec(line);
|
|
29
|
+
const value = match?.[1] ?? match?.[2];
|
|
30
|
+
const module = value === undefined ? undefined : CALLABLE.exec(value.trim())?.[1];
|
|
31
|
+
return module === undefined ? null : moduleTargets(module.replace(/\./g, '/'));
|
|
32
|
+
}
|
|
33
|
+
const moduleTargets = (module) => ['', 'src/'].flatMap((base) => [`${base}${module}.py`, `${base}${module}/__init__.py`]);
|
|
34
|
+
export function pythonScriptTargets(toml) {
|
|
35
|
+
const targets = new Set();
|
|
36
|
+
let table = null; // the script table being read, null outside one
|
|
37
|
+
for (const line of toml.split(/\r?\n/)) {
|
|
38
|
+
const trimmed = line.trim();
|
|
39
|
+
if (trimmed.startsWith('[')) {
|
|
40
|
+
const name = tableName(trimmed);
|
|
41
|
+
table = SCRIPT_TABLES.has(name) ? name : null;
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
if (table === null)
|
|
45
|
+
continue;
|
|
46
|
+
const found = stringScriptTargets(trimmed) ?? inlineScriptTargets(table, trimmed) ?? [];
|
|
47
|
+
for (const target of found)
|
|
48
|
+
targets.add(target);
|
|
49
|
+
}
|
|
50
|
+
return [...targets];
|
|
51
|
+
}
|
|
52
|
+
// A single-line table of string values, the only shape of table entry that is interpreted. Poetry's documented keys are
|
|
53
|
+
// `reference` and `type`; a table with any other key, a non-string value, a repeated key or text this does not account for is
|
|
54
|
+
// not interpreted, and pyprojectProblem reports it instead of letting the entry point silently disappear.
|
|
55
|
+
const INLINE_ENTRY = /^(?:[\w.-]+|"[^"]+"|'[^']+')\s*=\s*\{(.*)\}\s*(?:#.*)?$/;
|
|
56
|
+
const INLINE_PAIR = /\s*([\w-]+)\s*=\s*("[^"\\]*"|'[^']*')\s*(?:,|$)/y;
|
|
57
|
+
function inlineValues(body) {
|
|
58
|
+
const text = body.trim();
|
|
59
|
+
const pair = new RegExp(INLINE_PAIR);
|
|
60
|
+
const values = {};
|
|
61
|
+
let at = 0;
|
|
62
|
+
while (at < text.length) {
|
|
63
|
+
pair.lastIndex = at;
|
|
64
|
+
const match = pair.exec(text);
|
|
65
|
+
const key = match?.[1];
|
|
66
|
+
if (!match || key === undefined || (key !== 'reference' && key !== 'type') || Object.hasOwn(values, key))
|
|
67
|
+
return null;
|
|
68
|
+
values[key] = match[2].slice(1, -1);
|
|
69
|
+
at = pair.lastIndex;
|
|
70
|
+
}
|
|
71
|
+
return values;
|
|
72
|
+
}
|
|
73
|
+
// The file a `type = "file"` reference names, relative to the pyproject.toml. Absolute paths, Windows paths, backslashes and
|
|
74
|
+
// anything that leaves the project directory are refused rather than guessed at.
|
|
75
|
+
function referencedFile(reference) {
|
|
76
|
+
if (reference === '' || /[\\\0]/.test(reference) || reference.startsWith('/') || /^[A-Za-z]:/.test(reference))
|
|
77
|
+
return null;
|
|
78
|
+
const normal = path.posix.normalize(reference);
|
|
79
|
+
return normal === '.' || normal === '..' || normal.startsWith('../') ? null : normal;
|
|
80
|
+
}
|
|
81
|
+
// The Python files a table entry runs, or null when the entry cannot be safely interpreted. An empty list means it was
|
|
82
|
+
// understood and names nothing this analysis tracks (a reference to a file that is not Python source).
|
|
83
|
+
function inlineScriptTargets(table, line) {
|
|
84
|
+
const body = INLINE_ENTRY.exec(line)?.[1];
|
|
85
|
+
if (body === undefined)
|
|
86
|
+
return null;
|
|
87
|
+
if (table !== 'tool.poetry.scripts')
|
|
88
|
+
return null; // PEP 621 script values are strings, so a table there is not a script declaration
|
|
89
|
+
const values = inlineValues(body);
|
|
90
|
+
const reference = values?.reference;
|
|
91
|
+
if (!values || reference === undefined)
|
|
92
|
+
return null;
|
|
93
|
+
if (values.type === 'file') {
|
|
94
|
+
const file = referencedFile(reference);
|
|
95
|
+
return file === null ? null : file.endsWith('.py') ? [file] : [];
|
|
96
|
+
}
|
|
97
|
+
if (values.type === 'console') {
|
|
98
|
+
const module = /^([A-Za-z_][\w.]*)(?::[\w.]+)?$/.exec(reference)?.[1];
|
|
99
|
+
return module === undefined ? null : moduleTargets(module.replace(/\./g, '/'));
|
|
100
|
+
}
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
// pyproject.toml is read only for its console-script tables, so only what affects that is checked, and the reader is not a
|
|
104
|
+
// TOML parser: a header that is never closed, a script entry that is not `name = "module:function"`, or scripts declared in a
|
|
105
|
+
// form this reader does not follow (a dotted key or an inline table under [project]) all mean the entry points are unknown.
|
|
106
|
+
const SCRIPT_ENTRY = /^(?:[\w.-]+|"[^"]+"|'[^']+')\s*=\s*(?:"[^"]*"|'[^']*'|\{.*\})\s*(?:#.*)?$/;
|
|
107
|
+
const TABLE_HEADER = /^\[\[?[^[\]]+\]\]?\s*(?:#.*)?$/;
|
|
108
|
+
const STRINGS = /"(?:\\.|[^"\\])*"|'[^']*'/g;
|
|
109
|
+
export function pyprojectProblem(toml) {
|
|
110
|
+
let table = '';
|
|
111
|
+
let depth = 0; // open brackets of a multi-line array or inline table
|
|
112
|
+
let block = null; // inside a multi-line """ or ''' string
|
|
113
|
+
const lines = toml.split(/\r?\n/);
|
|
114
|
+
for (const [index, raw] of lines.entries()) {
|
|
115
|
+
if (block !== null) {
|
|
116
|
+
if (raw.includes(block))
|
|
117
|
+
block = null;
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
const line = raw.trim();
|
|
121
|
+
if (depth > 0) {
|
|
122
|
+
depth += bracketDelta(line);
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
if (line === '' || line.startsWith('#'))
|
|
126
|
+
continue;
|
|
127
|
+
if (line.startsWith('[')) {
|
|
128
|
+
if (!TABLE_HEADER.test(line))
|
|
129
|
+
return `has a table header that is not closed (line ${index + 1})`;
|
|
130
|
+
table = tableName(line);
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
const opener = /("""|''')/.exec(line);
|
|
134
|
+
if (opener && line.indexOf(opener[1]) === line.lastIndexOf(opener[1])) {
|
|
135
|
+
block = opener[1];
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
depth = Math.max(0, bracketDelta(line.slice(line.indexOf('=') + 1)));
|
|
139
|
+
const inScripts = SCRIPT_TABLES.has(table);
|
|
140
|
+
if (inScripts && depth === 0 && !SCRIPT_ENTRY.test(line))
|
|
141
|
+
return `has a console script entry that could not be parsed (line ${index + 1})`;
|
|
142
|
+
// Valid TOML that was not interpreted is reported, never dropped: a dropped entry point makes its module look unused.
|
|
143
|
+
const inline = INLINE_ENTRY.test(line);
|
|
144
|
+
if (inScripts && (depth > 0 || (inline && inlineScriptTargets(table, line) === null) || (!inline && stringScriptTargets(line) === null))) {
|
|
145
|
+
return `has a console script entry in a form that is not read (line ${index + 1}); supported: "module:function" or, under [tool.poetry.scripts], { reference = "file.py", type = "file" }`;
|
|
146
|
+
}
|
|
147
|
+
if ((table === 'project' || table === 'tool.poetry') && /^(?:gui-)?scripts\s*[.=]/.test(line))
|
|
148
|
+
return `declares console scripts in a form that is not read (line ${index + 1}); use a [project.scripts] table`;
|
|
149
|
+
}
|
|
150
|
+
return depth > 0 || block !== null ? 'ends inside an unclosed array or string' : null;
|
|
151
|
+
}
|
|
152
|
+
// Brackets and braces opened minus closed on a line, ignoring those inside strings and comments.
|
|
153
|
+
function bracketDelta(text) {
|
|
154
|
+
const bare = text.replace(STRINGS, '""').replace(/#.*$/, '');
|
|
155
|
+
return (bare.match(/[[{]/g)?.length ?? 0) - (bare.match(/[\]}]/g)?.length ?? 0);
|
|
156
|
+
}
|
|
157
|
+
// Console entry points can also be declared in setup.cfg or setup.py; those are not read, so a project that relies on them
|
|
158
|
+
// has entry points this analysis cannot see.
|
|
159
|
+
const LEGACY_ENTRY_POINTS = /console_scripts|gui_scripts|\[options\.entry_points\]/;
|
|
160
|
+
// Configuration problems that affect Python entry-point detection. Only relevant when the project has Python source.
|
|
161
|
+
export function pythonConfigProblems(read, files) {
|
|
162
|
+
const problems = [];
|
|
163
|
+
const withScripts = new Set();
|
|
164
|
+
for (const file of [...files.pyproject].sort()) {
|
|
165
|
+
const text = read(file);
|
|
166
|
+
if (text === null) {
|
|
167
|
+
problems.push(`${file} could not be read or parsed`);
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
const problem = pyprojectProblem(text);
|
|
171
|
+
if (problem)
|
|
172
|
+
problems.push(`${file} ${problem}`);
|
|
173
|
+
else if (pythonScriptTargets(text).length > 0)
|
|
174
|
+
withScripts.add(dirOf(file));
|
|
175
|
+
}
|
|
176
|
+
for (const file of [...files.legacy].sort()) {
|
|
177
|
+
if (withScripts.has(dirOf(file)))
|
|
178
|
+
continue;
|
|
179
|
+
const text = read(file);
|
|
180
|
+
if (text !== null && LEGACY_ENTRY_POINTS.test(text))
|
|
181
|
+
problems.push(`${file} declares console entry points, which are not read (only pyproject.toml scripts are)`);
|
|
182
|
+
}
|
|
183
|
+
return problems;
|
|
184
|
+
}
|
|
185
|
+
export function loadPackages(read, files) {
|
|
186
|
+
const found = [];
|
|
187
|
+
for (const file of [...files].sort()) {
|
|
188
|
+
const content = read(file);
|
|
189
|
+
if (content === null)
|
|
190
|
+
continue;
|
|
191
|
+
if (path.posix.basename(file) === 'pyproject.toml') {
|
|
192
|
+
const bin = pythonScriptTargets(content);
|
|
193
|
+
if (bin.length > 0)
|
|
194
|
+
found.push({ file, dir: dirOf(file), bin, frameworks: [] });
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
197
|
+
let json;
|
|
198
|
+
try {
|
|
199
|
+
json = asObject(JSON.parse(content));
|
|
200
|
+
}
|
|
201
|
+
catch {
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
if (!json)
|
|
205
|
+
continue;
|
|
206
|
+
const dependencies = Object.keys({ ...asObject(json.dependencies), ...asObject(json.devDependencies), ...asObject(json.peerDependencies) });
|
|
207
|
+
found.push({
|
|
208
|
+
file,
|
|
209
|
+
dir: dirOf(file),
|
|
210
|
+
name: str(json.name),
|
|
211
|
+
main: str(json.main),
|
|
212
|
+
module: str(json.module),
|
|
213
|
+
types: str(json.types) ?? str(json.typings),
|
|
214
|
+
bin: typeof json.bin === 'string' ? [json.bin] : Object.values(asObject(json.bin) ?? {}).filter((v) => typeof v === 'string'),
|
|
215
|
+
exports: json.exports,
|
|
216
|
+
imports: asObject(json.imports),
|
|
217
|
+
frameworks: FRAMEWORK_DEPENDENCIES.filter(([, pattern]) => dependencies.some((d) => pattern.test(d))).map(([name]) => name),
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
return found;
|
|
221
|
+
}
|
|
222
|
+
const CONDITIONS = new Set(['types', 'import', 'require', 'node', 'default', 'module']);
|
|
223
|
+
// A package.json can come from node_modules or from a hostile checkout, and its `exports` map can nest as deep as the file allows
|
|
224
|
+
// (JSON.parse itself does not overflow). Every walk below therefore uses an explicit stack with a hard cap on depth and on nodes
|
|
225
|
+
// visited, and returns what it found so far when a cap is hit: a pathological map yields fewer entries, never a crash. Real maps
|
|
226
|
+
// nest a handful of levels and hold well under a thousand entries.
|
|
227
|
+
const MAX_MAP_DEPTH = 32;
|
|
228
|
+
const MAX_MAP_NODES = 10_000;
|
|
229
|
+
// Conditions are tried in the order the object lists them, as Node does.
|
|
230
|
+
function pick(value) {
|
|
231
|
+
const pending = [{ value, depth: 0 }];
|
|
232
|
+
let visited = 0;
|
|
233
|
+
for (let entry = pending.pop(); entry; entry = pending.pop()) {
|
|
234
|
+
if (++visited > MAX_MAP_NODES)
|
|
235
|
+
return undefined;
|
|
236
|
+
if (typeof entry.value === 'string') {
|
|
237
|
+
if (entry.value)
|
|
238
|
+
return entry.value;
|
|
239
|
+
continue;
|
|
240
|
+
}
|
|
241
|
+
if (entry.depth >= MAX_MAP_DEPTH)
|
|
242
|
+
continue;
|
|
243
|
+
// Pushed in reverse so the first listed alternative is tried first.
|
|
244
|
+
if (Array.isArray(entry.value)) {
|
|
245
|
+
for (let i = entry.value.length - 1; i >= 0; i--)
|
|
246
|
+
pending.push({ value: entry.value[i], depth: entry.depth + 1 });
|
|
247
|
+
continue;
|
|
248
|
+
}
|
|
249
|
+
const object = asObject(entry.value);
|
|
250
|
+
if (!object)
|
|
251
|
+
continue;
|
|
252
|
+
const keys = Object.keys(object).filter((key) => CONDITIONS.has(key));
|
|
253
|
+
for (let i = keys.length - 1; i >= 0; i--)
|
|
254
|
+
pending.push({ value: object[keys[i]], depth: entry.depth + 1 });
|
|
255
|
+
}
|
|
256
|
+
return undefined;
|
|
257
|
+
}
|
|
258
|
+
function patternMatch(pattern, subpath) {
|
|
259
|
+
const star = pattern.indexOf('*');
|
|
260
|
+
if (star === -1)
|
|
261
|
+
return pattern === subpath ? '' : null;
|
|
262
|
+
const prefix = pattern.slice(0, star);
|
|
263
|
+
const suffix = pattern.slice(star + 1);
|
|
264
|
+
if (subpath.length < prefix.length + suffix.length || !subpath.startsWith(prefix) || !subpath.endsWith(suffix))
|
|
265
|
+
return null;
|
|
266
|
+
return subpath.slice(prefix.length, subpath.length - suffix.length);
|
|
267
|
+
}
|
|
268
|
+
// `subpath` is "." for the package itself or "./thing" for a deep import. Returns the file the map points at, relative to the package.
|
|
269
|
+
export function resolveMap(map, subpath) {
|
|
270
|
+
const object = asObject(map);
|
|
271
|
+
if (typeof map === 'string' || Array.isArray(map) || (object && !Object.keys(object).some((k) => k.startsWith('.') || k.startsWith('#')))) {
|
|
272
|
+
return subpath === '.' ? pick(map) : undefined;
|
|
273
|
+
}
|
|
274
|
+
if (!object)
|
|
275
|
+
return undefined;
|
|
276
|
+
if (subpath in object)
|
|
277
|
+
return pick(object[subpath]);
|
|
278
|
+
let best;
|
|
279
|
+
for (const key of Object.keys(object)) {
|
|
280
|
+
const capture = key.includes('*') ? patternMatch(key, subpath) : null;
|
|
281
|
+
if (capture !== null && (!best || key.indexOf('*') > best.key.indexOf('*')))
|
|
282
|
+
best = { key, capture };
|
|
283
|
+
}
|
|
284
|
+
const target = best ? pick(object[best.key]) : undefined;
|
|
285
|
+
return target && best ? target.replace(/\*/g, best.capture) : undefined;
|
|
286
|
+
}
|
|
287
|
+
// Every target a package could serve for a subpath, in the order its map lists them. Custom conditions such as
|
|
288
|
+
// "@scope/source" or "development" often point straight at source files, so unknown conditions are kept as fallbacks.
|
|
289
|
+
function pickAll(value) {
|
|
290
|
+
const found = [];
|
|
291
|
+
collectStrings(value, found);
|
|
292
|
+
return found;
|
|
293
|
+
}
|
|
294
|
+
export function packageEntries(pkg, subpath) {
|
|
295
|
+
const primary = packageEntry(pkg, subpath);
|
|
296
|
+
if (pkg.exports === undefined)
|
|
297
|
+
return primary ? [primary] : [];
|
|
298
|
+
const object = asObject(pkg.exports);
|
|
299
|
+
let selected;
|
|
300
|
+
if (typeof pkg.exports === 'string' || Array.isArray(pkg.exports))
|
|
301
|
+
selected = subpath === '.' ? pkg.exports : undefined;
|
|
302
|
+
else if (object && !Object.keys(object).some((k) => k.startsWith('.')))
|
|
303
|
+
selected = subpath === '.' ? object : undefined;
|
|
304
|
+
else if (object)
|
|
305
|
+
selected = object[subpath];
|
|
306
|
+
const all = pickAll(selected);
|
|
307
|
+
return [...new Set([...(primary ? [primary] : []), ...all])];
|
|
308
|
+
}
|
|
309
|
+
export function packageEntry(pkg, subpath) {
|
|
310
|
+
if (pkg.exports !== undefined)
|
|
311
|
+
return resolveMap(pkg.exports, subpath);
|
|
312
|
+
if (subpath !== '.')
|
|
313
|
+
return `./${subpath.slice(2)}`;
|
|
314
|
+
return pkg.types ?? pkg.module ?? pkg.main ?? './index';
|
|
315
|
+
}
|
|
316
|
+
// Every string in a map, in the order the map lists them, bounded as described above.
|
|
317
|
+
function collectStrings(value, into) {
|
|
318
|
+
const pending = [{ value, depth: 0 }];
|
|
319
|
+
let visited = 0;
|
|
320
|
+
for (let entry = pending.pop(); entry; entry = pending.pop()) {
|
|
321
|
+
if (++visited > MAX_MAP_NODES)
|
|
322
|
+
return;
|
|
323
|
+
if (typeof entry.value === 'string') {
|
|
324
|
+
into.push(entry.value);
|
|
325
|
+
continue;
|
|
326
|
+
}
|
|
327
|
+
if (entry.depth >= MAX_MAP_DEPTH)
|
|
328
|
+
continue;
|
|
329
|
+
const inner = Array.isArray(entry.value) ? entry.value : Object.values(asObject(entry.value) ?? {});
|
|
330
|
+
for (let i = inner.length - 1; i >= 0; i--)
|
|
331
|
+
pending.push({ value: inner[i], depth: entry.depth + 1 });
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
// Every file a package publishes as its public surface: exports, main, module, types and bin.
|
|
335
|
+
export function publicTargets(pkg) {
|
|
336
|
+
const found = [];
|
|
337
|
+
collectStrings(pkg.exports, found);
|
|
338
|
+
for (const single of [pkg.main, pkg.module, pkg.types])
|
|
339
|
+
if (single)
|
|
340
|
+
found.push(single);
|
|
341
|
+
found.push(...pkg.bin);
|
|
342
|
+
return [...new Set(found.filter((t) => t.startsWith('.') || !t.includes(':')))];
|
|
343
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import { createRequire } from 'node:module';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { extractJs } from './extract-js.js';
|
|
6
|
+
import { extractPython } from './extract-py.js';
|
|
7
|
+
import { assessParse, exceedsBudget } from './health.js';
|
|
8
|
+
const require = createRequire(import.meta.url);
|
|
9
|
+
const ts = require('@vscode/tree-sitter-wasm');
|
|
10
|
+
const wasmDir = path.dirname(require.resolve('@vscode/tree-sitter-wasm'));
|
|
11
|
+
const grammar = {
|
|
12
|
+
typescript: 'tree-sitter-typescript.wasm',
|
|
13
|
+
tsx: 'tree-sitter-tsx.wasm',
|
|
14
|
+
javascript: 'tree-sitter-javascript.wasm',
|
|
15
|
+
python: 'tree-sitter-python.wasm',
|
|
16
|
+
};
|
|
17
|
+
let parser;
|
|
18
|
+
let booting;
|
|
19
|
+
const languages = new Map();
|
|
20
|
+
const queries = new Map();
|
|
21
|
+
function boot() {
|
|
22
|
+
booting ??= ts.Parser.init({ locateFile: (file) => path.join(wasmDir, file) }).then(() => {
|
|
23
|
+
parser = new ts.Parser();
|
|
24
|
+
});
|
|
25
|
+
return booting;
|
|
26
|
+
}
|
|
27
|
+
export async function loadLanguages(langs) {
|
|
28
|
+
await boot();
|
|
29
|
+
for (const lang of new Set(langs)) {
|
|
30
|
+
if (!languages.has(lang)) {
|
|
31
|
+
languages.set(lang, await ts.Language.load(fs.readFileSync(path.join(wasmDir, grammar[lang]))));
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
export function parseFile(relPath, lang, source) {
|
|
36
|
+
const language = languages.get(lang);
|
|
37
|
+
if (!parser || !language)
|
|
38
|
+
throw new Error(`Language ${lang} is not loaded`);
|
|
39
|
+
parser.setLanguage(language);
|
|
40
|
+
const tree = parser.parse(source);
|
|
41
|
+
if (!tree)
|
|
42
|
+
return null;
|
|
43
|
+
const query = (src) => {
|
|
44
|
+
const key = `${lang}:${createHash('sha1').update(src).digest('hex')}`;
|
|
45
|
+
let q = queries.get(key);
|
|
46
|
+
if (!q) {
|
|
47
|
+
q = new ts.Query(language, src);
|
|
48
|
+
queries.set(key, q);
|
|
49
|
+
}
|
|
50
|
+
return q;
|
|
51
|
+
};
|
|
52
|
+
try {
|
|
53
|
+
// A pathologically large or deeply nested tree is not analysed: every pass below walks it, some once per reference up the
|
|
54
|
+
// parent chain. The file is returned as an empty, degraded result, so it counts as partial and is never reported as clean.
|
|
55
|
+
const limit = exceedsBudget(tree.rootNode);
|
|
56
|
+
if (limit)
|
|
57
|
+
return unanalysed(relPath, lang, source, limit);
|
|
58
|
+
const input = { root: tree.rootNode, source, lang, path: relPath, query };
|
|
59
|
+
const extracted = lang === 'python' ? extractPython(input) : extractJs(input);
|
|
60
|
+
// Tree-sitter always returns a tree, repairing what it cannot parse. The repairs are recorded, never hidden.
|
|
61
|
+
const parse = assessParse(tree.rootNode, lang, source);
|
|
62
|
+
return {
|
|
63
|
+
path: relPath,
|
|
64
|
+
lang,
|
|
65
|
+
bytes: Buffer.byteLength(source),
|
|
66
|
+
lines: source.split('\n').length,
|
|
67
|
+
...extracted,
|
|
68
|
+
...(parse ? { parse } : {}),
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
finally {
|
|
72
|
+
tree.delete();
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
function unanalysed(relPath, lang, source, limit) {
|
|
76
|
+
return {
|
|
77
|
+
path: relPath,
|
|
78
|
+
lang,
|
|
79
|
+
bytes: Buffer.byteLength(source),
|
|
80
|
+
lines: source.split('\n').length,
|
|
81
|
+
symbols: [],
|
|
82
|
+
imports: [],
|
|
83
|
+
reexports: [],
|
|
84
|
+
unusedImports: [],
|
|
85
|
+
suppressions: [],
|
|
86
|
+
dynamicLoads: [],
|
|
87
|
+
decorated: false,
|
|
88
|
+
script: false,
|
|
89
|
+
parse: { status: 'degraded', errors: 0, missing: 0, lines: [], limit },
|
|
90
|
+
};
|
|
91
|
+
}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { describeHealth } from './health.js';
|
|
2
|
+
import { safe } from '../../utils/text.js';
|
|
3
|
+
const TITLES = {
|
|
4
|
+
'duplicate-body': 'Textually identical implementations',
|
|
5
|
+
'similar-structure': 'Structurally similar implementations',
|
|
6
|
+
'duplicate-symbol': 'Same name defined in several files',
|
|
7
|
+
'circular-dependency': 'Circular dependencies',
|
|
8
|
+
'unresolved-import': 'Imports that could not be resolved',
|
|
9
|
+
'orphan-file': 'Orphan files (nothing imports them)',
|
|
10
|
+
'dead-export': 'Exports nobody imports',
|
|
11
|
+
'unused-import': 'Unused imports',
|
|
12
|
+
suppression: 'Suppressed type/lint checks',
|
|
13
|
+
};
|
|
14
|
+
const ORDER = [
|
|
15
|
+
'duplicate-body',
|
|
16
|
+
'similar-structure',
|
|
17
|
+
'duplicate-symbol',
|
|
18
|
+
'circular-dependency',
|
|
19
|
+
'suppression',
|
|
20
|
+
'unresolved-import',
|
|
21
|
+
'orphan-file',
|
|
22
|
+
'dead-export',
|
|
23
|
+
'unused-import',
|
|
24
|
+
];
|
|
25
|
+
export function renderFindings(findings, perRule = 15, explain = false) {
|
|
26
|
+
if (findings.length === 0)
|
|
27
|
+
return 'No issues found.';
|
|
28
|
+
const sections = [];
|
|
29
|
+
for (const rule of ORDER) {
|
|
30
|
+
const group = findings.filter((f) => f.rule === rule);
|
|
31
|
+
if (group.length === 0)
|
|
32
|
+
continue;
|
|
33
|
+
const lines = group.slice(0, perRule).map((f) => {
|
|
34
|
+
const where = f.line ? `${safe(f.file)}:${f.line}` : safe(f.file);
|
|
35
|
+
const label = f.classification ? `${f.severity}, ${f.confidence} confidence, ${f.classification}` : `${f.severity}, ${f.confidence} confidence`;
|
|
36
|
+
const head = `- [${label}] ${where} ${safe(f.message)}`;
|
|
37
|
+
if (!explain)
|
|
38
|
+
return head;
|
|
39
|
+
const why = f.evidence.map((e) => ` · ${safe(e)}`).join('\n');
|
|
40
|
+
return `${head}\n${why}\n → ${safe(f.remediation)}`;
|
|
41
|
+
});
|
|
42
|
+
if (group.length > perRule)
|
|
43
|
+
lines.push(`- … and ${group.length - perRule} more`);
|
|
44
|
+
sections.push(`## ${TITLES[rule]} (${group.length})\n${lines.join('\n')}`);
|
|
45
|
+
}
|
|
46
|
+
return sections.join('\n\n');
|
|
47
|
+
}
|
|
48
|
+
const STOP_TEXT = {
|
|
49
|
+
'entry-limit': 'file discovery stopped at maxDiscoveryEntries, so part of the workspace was never listed',
|
|
50
|
+
'time-limit': 'file discovery stopped at maxDiscoveryMs, so part of the workspace was never listed',
|
|
51
|
+
'memory-limit': 'file discovery stopped at its memory budget (maxDiscoveryMemoryMb), so part of the workspace was never listed',
|
|
52
|
+
'index-time-limit': 'indexing stopped at maxIndexMs before every file was processed (files already processed are cached: run again to continue)',
|
|
53
|
+
};
|
|
54
|
+
export function describeCoverage(project, maxFiles) {
|
|
55
|
+
if (project.complete)
|
|
56
|
+
return `Analysis is complete: all ${project.files.length} eligible files were indexed.`;
|
|
57
|
+
const reasons = [];
|
|
58
|
+
if (project.stopped) {
|
|
59
|
+
const left = project.unindexed ?? 0;
|
|
60
|
+
reasons.push(`${STOP_TEXT[project.stopped]}${left > 0 ? ` (${left} discovered file(s) not processed)` : ''}; raise the limit in .guardianrc`);
|
|
61
|
+
}
|
|
62
|
+
if (project.truncated)
|
|
63
|
+
reasons.push(`only the first ${maxFiles} of ${project.eligible} eligible files (sorted by path) were indexed, raise maxFiles in .guardianrc`);
|
|
64
|
+
if (project.skipped.length > 0) {
|
|
65
|
+
const sample = project.skipped.slice(0, 3).map((s) => `${safe(s.path)} (${s.reason})`).join(', ');
|
|
66
|
+
reasons.push(`${project.skipped.length} file(s) were skipped: ${sample}${project.skipped.length > 3 ? ', …' : ''}`);
|
|
67
|
+
}
|
|
68
|
+
const allIssues = project.parseIssues ?? [];
|
|
69
|
+
const unanalysed = allIssues.filter((i) => i.limit);
|
|
70
|
+
if (unanalysed.length > 0) {
|
|
71
|
+
const sample = unanalysed.slice(0, 3).map((i) => `${safe(i.path)} (${i.limit === 'depth' ? 'nested too deeply' : 'too many syntax nodes'})`).join(', ');
|
|
72
|
+
reasons.push(`${unanalysed.length} file(s) exceeded the parser's size or nesting budget and were not analysed: ${sample}${unanalysed.length > 3 ? ', …' : ''}`);
|
|
73
|
+
}
|
|
74
|
+
const issues = allIssues.filter((i) => !i.limit);
|
|
75
|
+
if (issues.length > 0) {
|
|
76
|
+
const degraded = issues.filter((i) => i.status === 'degraded').length;
|
|
77
|
+
const sample = issues.slice(0, 3).map((i) => `${safe(i.path)} (${describeHealth(i)})`).join('; ');
|
|
78
|
+
reasons.push(`${issues.length} file(s) have syntax errors the parser had to repair${degraded > 0 ? `, ${degraded} of them at the top level or inside an import/export` : ''}: ${sample}${issues.length > 3 ? '; …' : ''}. Their valid symbols were kept, but the extraction may be incomplete`);
|
|
79
|
+
}
|
|
80
|
+
const configProblems = project.configProblems ?? [];
|
|
81
|
+
if (configProblems.length > 0) {
|
|
82
|
+
const sample = configProblems.slice(0, 3).map(safe).join(', ');
|
|
83
|
+
reasons.push(`configuration was unavailable (${sample}${configProblems.length > 3 ? ', …' : ''}), so path aliases, workspace packages, entry points or ignore rules may be missing or not applied`);
|
|
84
|
+
}
|
|
85
|
+
return `Analysis is PARTIAL: ${reasons.join('; ')}. Findings that depend on seeing every file are marked low confidence.`;
|
|
86
|
+
}
|
|
87
|
+
const countOf = (findings) => findings.reduce((n, f) => n + f.count, 0);
|
|
88
|
+
const SKIP_TEXT = {
|
|
89
|
+
'over-limit': 'over the comparison limit',
|
|
90
|
+
'baseline-unreadable': 'base version unreadable',
|
|
91
|
+
'baseline-too-large': 'base version too large',
|
|
92
|
+
'baseline-unparseable': 'base version could not be parsed',
|
|
93
|
+
};
|
|
94
|
+
function partialNotice(v) {
|
|
95
|
+
const lines = ['PARTIAL VALIDATION: the baseline could not be rebuilt completely, so what follows is provisional, not a definitive list of introduced or fixed problems.'];
|
|
96
|
+
for (const reason of v.partialReasons)
|
|
97
|
+
lines.push(`- ${safe(reason)}`);
|
|
98
|
+
const c = v.coverage;
|
|
99
|
+
if (c) {
|
|
100
|
+
lines.push(`- Compared ${c.evaluated.length} of ${c.relevantChanges} changed source file(s) (limit ${c.limit}).`);
|
|
101
|
+
if (c.skipped.length > 0) {
|
|
102
|
+
const sample = c.skipped.slice(0, 5).map((s) => `${safe(s.path)} (${SKIP_TEXT[s.reason]})`).join(', ');
|
|
103
|
+
lines.push(`- Not compared (${c.skipped.length}): ${sample}${c.skipped.length > 5 ? ', …' : ''}`);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return lines.join('\n');
|
|
107
|
+
}
|
|
108
|
+
function downgradeNotice(v) {
|
|
109
|
+
const lines = ['CONFIGURATION WEAKENED: this change lowers what the checks look for, so findings that disappeared are not necessarily fixes.'];
|
|
110
|
+
for (const d of v.configDowngrades)
|
|
111
|
+
lines.push(`- ${safe(d.detail)}`);
|
|
112
|
+
if (v.suppressedByPolicy.length > 0) {
|
|
113
|
+
const sample = v.suppressedByPolicy.slice(0, 5).map((f) => `${f.rule} in ${safe(f.file)}`).join(', ');
|
|
114
|
+
lines.push(`- ${v.suppressedByPolicy.length} finding(s) reported at the base commit are no longer reported only because of this, and are not counted as fixed: ${sample}${v.suppressedByPolicy.length > 5 ? ', …' : ''}`);
|
|
115
|
+
}
|
|
116
|
+
return lines.join('\n');
|
|
117
|
+
}
|
|
118
|
+
export function renderValidation(v, options = {}) {
|
|
119
|
+
const scopes = options.scopes ?? [];
|
|
120
|
+
const inScope = (list) => scopes.length === 0 ? list : list.filter((f) => scopes.some((s) => f.file === s || f.file.startsWith(`${s}/`)));
|
|
121
|
+
const out = [];
|
|
122
|
+
if (v.scope === 'workspace') {
|
|
123
|
+
out.push(`Scope: whole workspace. Could not isolate your changes: ${v.fallback}.`);
|
|
124
|
+
out.push(renderFindings(inScope(v.introduced)));
|
|
125
|
+
return out.join('\n\n');
|
|
126
|
+
}
|
|
127
|
+
const kinds = new Map();
|
|
128
|
+
for (const c of v.changes)
|
|
129
|
+
kinds.set(c.status, (kinds.get(c.status) ?? 0) + 1);
|
|
130
|
+
const summary = [...kinds].map(([status, n]) => `${n} ${status}`).join(', ') || 'no source files changed';
|
|
131
|
+
out.push(`Scope: changes against ${safe(v.baseLabel ?? 'HEAD')}, ${summary}.`);
|
|
132
|
+
if (v.partial)
|
|
133
|
+
out.push(partialNotice(v));
|
|
134
|
+
if (v.configDowngrades.length > 0)
|
|
135
|
+
out.push(downgradeNotice(v));
|
|
136
|
+
for (const note of v.baselineNotes)
|
|
137
|
+
out.push(`Note: ${safe(note)}`);
|
|
138
|
+
const introduced = inScope(v.introduced);
|
|
139
|
+
const direct = introduced.filter((f) => f.inChangedFile);
|
|
140
|
+
const indirect = introduced.filter((f) => !f.inChangedFile);
|
|
141
|
+
const heading = v.partial ? 'Possibly introduced by this change (provisional)' : 'Introduced by this change';
|
|
142
|
+
out.push(introduced.length === 0 ? `## ${heading} (0)\n${v.partial ? 'None found in the part that could be compared.' : 'No new problems.'}` : `## ${heading} (${introduced.length})\n${renderFindings(direct.length > 0 ? direct : introduced, 15, true)}`);
|
|
143
|
+
if (direct.length > 0 && indirect.length > 0) {
|
|
144
|
+
out.push(`## Caused in files you did not edit (${indirect.length})\n${renderFindings(indirect, 15, true)}`);
|
|
145
|
+
}
|
|
146
|
+
if (v.fixed.length > 0)
|
|
147
|
+
out.push(v.partial ? `Possibly fixed by this change (provisional): ${v.fixed.length} finding(s).` : `Fixed by this change: ${v.fixed.length} finding(s).`);
|
|
148
|
+
const existing = inScope(v.existing);
|
|
149
|
+
if (options.includeExisting)
|
|
150
|
+
out.push(`## Pre-existing (${existing.length})\n${renderFindings(existing)}`);
|
|
151
|
+
else if (existing.length > 0)
|
|
152
|
+
out.push(`Pre-existing findings not shown: ${existing.length} (${countOf(existing)} occurrences). Pass include_existing to list them.`);
|
|
153
|
+
return out.join('\n\n');
|
|
154
|
+
}
|