@cognium/mcp-server 0.8.1 → 0.9.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/CHANGELOG.md +229 -0
- package/README.md +165 -5
- package/dist/bin.js +32 -3
- package/dist/bin.js.map +1 -1
- package/dist/deps/resolve.d.ts +70 -0
- package/dist/deps/resolve.d.ts.map +1 -0
- package/dist/deps/resolve.js +256 -0
- package/dist/deps/resolve.js.map +1 -0
- package/dist/deps/snapshot.d.ts +42 -0
- package/dist/deps/snapshot.d.ts.map +1 -0
- package/dist/deps/snapshot.js +104 -0
- package/dist/deps/snapshot.js.map +1 -0
- package/dist/deps/tycho-target.d.ts +31 -0
- package/dist/deps/tycho-target.d.ts.map +1 -0
- package/dist/deps/tycho-target.js +62 -0
- package/dist/deps/tycho-target.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/modules.d.ts +14 -2
- package/dist/modules.d.ts.map +1 -1
- package/dist/modules.js +11 -3
- package/dist/modules.js.map +1 -1
- package/dist/roots.d.ts +146 -0
- package/dist/roots.d.ts.map +1 -0
- package/dist/roots.js +354 -0
- package/dist/roots.js.map +1 -0
- package/dist/server.d.ts +38 -4
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +131 -7
- package/dist/server.js.map +1 -1
- package/dist/startup.d.ts.map +1 -1
- package/dist/startup.js +3 -4
- package/dist/startup.js.map +1 -1
- package/dist/status.d.ts +43 -0
- package/dist/status.d.ts.map +1 -0
- package/dist/status.js +54 -0
- package/dist/status.js.map +1 -0
- package/dist/tools/analyze-dependencies.d.ts +377 -0
- package/dist/tools/analyze-dependencies.d.ts.map +1 -0
- package/dist/tools/analyze-dependencies.js +387 -0
- package/dist/tools/analyze-dependencies.js.map +1 -0
- package/dist/tools/attack-surface-summary.d.ts +11 -0
- package/dist/tools/attack-surface-summary.d.ts.map +1 -1
- package/dist/tools/attack-surface-summary.js +2 -0
- package/dist/tools/attack-surface-summary.js.map +1 -1
- package/dist/tools/check-sanitizer.d.ts +10 -0
- package/dist/tools/check-sanitizer.d.ts.map +1 -1
- package/dist/tools/check-sanitizer.js +2 -0
- package/dist/tools/check-sanitizer.js.map +1 -1
- package/dist/tools/describe-sink.d.ts +14 -0
- package/dist/tools/describe-sink.d.ts.map +1 -1
- package/dist/tools/describe-sink.js +2 -0
- package/dist/tools/describe-sink.js.map +1 -1
- package/dist/tools/describe-source.d.ts +8 -0
- package/dist/tools/describe-source.d.ts.map +1 -1
- package/dist/tools/describe-source.js +2 -0
- package/dist/tools/describe-source.js.map +1 -1
- package/dist/tools/explain-finding.d.ts +5 -0
- package/dist/tools/explain-finding.d.ts.map +1 -1
- package/dist/tools/explain-finding.js +2 -0
- package/dist/tools/explain-finding.js.map +1 -1
- package/dist/tools/find-similar.d.ts +10 -0
- package/dist/tools/find-similar.d.ts.map +1 -1
- package/dist/tools/find-similar.js +2 -0
- package/dist/tools/find-similar.js.map +1 -1
- package/dist/tools/graph-facts.d.ts +829 -0
- package/dist/tools/graph-facts.d.ts.map +1 -0
- package/dist/tools/graph-facts.js +735 -0
- package/dist/tools/graph-facts.js.map +1 -0
- package/dist/tools/list-entry-points.d.ts +11 -0
- package/dist/tools/list-entry-points.d.ts.map +1 -1
- package/dist/tools/list-entry-points.js +2 -0
- package/dist/tools/list-entry-points.js.map +1 -1
- package/dist/tools/list-reachable-sinks.d.ts +11 -0
- package/dist/tools/list-reachable-sinks.d.ts.map +1 -1
- package/dist/tools/list-reachable-sinks.js +2 -0
- package/dist/tools/list-reachable-sinks.js.map +1 -1
- package/dist/tools/navigation.d.ts +22 -0
- package/dist/tools/navigation.d.ts.map +1 -1
- package/dist/tools/navigation.js +3 -0
- package/dist/tools/navigation.js.map +1 -1
- package/dist/tools/output-shapes.d.ts +159 -0
- package/dist/tools/output-shapes.d.ts.map +1 -0
- package/dist/tools/output-shapes.js +153 -0
- package/dist/tools/output-shapes.js.map +1 -0
- package/dist/tools/refresh.d.ts +7 -0
- package/dist/tools/refresh.d.ts.map +1 -1
- package/dist/tools/refresh.js +2 -0
- package/dist/tools/refresh.js.map +1 -1
- package/dist/tools/scan.d.ts +18 -0
- package/dist/tools/scan.d.ts.map +1 -1
- package/dist/tools/scan.js +2 -0
- package/dist/tools/scan.js.map +1 -1
- package/dist/tools/taint-paths.d.ts +10 -0
- package/dist/tools/taint-paths.d.ts.map +1 -1
- package/dist/tools/taint-paths.js +2 -0
- package/dist/tools/taint-paths.js.map +1 -1
- package/dist/tools/types.d.ts +17 -5
- package/dist/tools/types.d.ts.map +1 -1
- package/dist/tools/types.js +9 -0
- package/dist/tools/types.js.map +1 -1
- package/package.json +6 -6
|
@@ -0,0 +1,735 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `find_dead_code` and `removal_reach` — the engine's graph facts over stable
|
|
3
|
+
* ids (C4 0.3.0 §11, `language` from 0.4.0; `dead-code@0.1` and
|
|
4
|
+
* `removal-reach@0.1`).
|
|
5
|
+
*
|
|
6
|
+
* Both read a TypeScript / JavaScript, Java, Python or Go tree, build the
|
|
7
|
+
* engine's call graph with its entry-point index (`buildTsCallGraph`,
|
|
8
|
+
* `buildJavaGraph`, `buildPythonGraph` or `buildGoGraph`), and pass the
|
|
9
|
+
* engine's answer through **unchanged**. Each engine fact keeps every field
|
|
10
|
+
* the engine gave it; the server adds, after them:
|
|
11
|
+
*
|
|
12
|
+
* - its envelope: `projectRoot`, `baselineRoot`, `language`, `engine`,
|
|
13
|
+
* truncation and `provenance`;
|
|
14
|
+
* - **`decision`** on each fact: the outcome of one rule,
|
|
15
|
+
* `no-inferred-edge-decides@0.1`, and the facts it rests on (`because`);
|
|
16
|
+
* - **`entrySet`**: the roots the walk started from (kind, certainty, place),
|
|
17
|
+
* with their digest, so "no path from any entry" can be read against them;
|
|
18
|
+
* - **`scope`**: how many files were read and which directories were not;
|
|
19
|
+
* - **`decisions`**: the outcome counts (and, for `removal_reach`,
|
|
20
|
+
* `allUnreachable`).
|
|
21
|
+
*
|
|
22
|
+
* The rule (a removal is safe only when the symbol is unreachable from every
|
|
23
|
+
* entry point, and no inferred edge decides), read both ways — an inferred edge
|
|
24
|
+
* neither proves a declaration live nor proves it dead:
|
|
25
|
+
*
|
|
26
|
+
* - `reachable`: a strong path, from a `declared` or `conventional` entry over
|
|
27
|
+
* `exact` / `polymorphic` calls and structural edges only.
|
|
28
|
+
* - `unreachable`: no strong path, no weak path, no unresolved site that could
|
|
29
|
+
* bind to it, no unparsed file that could reach it, and at least one root.
|
|
30
|
+
* - `undecided`: everything else — the only path starts at an `inferred`
|
|
31
|
+
* entry, crosses an `inferred` call or a `reference`; an unresolved site
|
|
32
|
+
* could bind to it; an unparsed file could reach it; the root set is empty;
|
|
33
|
+
* or (`removal_reach`) its file did not parse at the head, so it may not
|
|
34
|
+
* have been removed at all. A person decides these.
|
|
35
|
+
*
|
|
36
|
+
* `engine` names the `circle-ir` version that computed the answer, because
|
|
37
|
+
* the result schemes carry none and a record must name what it was decided
|
|
38
|
+
* with.
|
|
39
|
+
*/
|
|
40
|
+
import { z } from 'zod';
|
|
41
|
+
import { existsSync, lstatSync, readdirSync, readFileSync, statSync } from 'fs';
|
|
42
|
+
import { createRequire } from 'node:module';
|
|
43
|
+
import { isAbsolute, join, relative, resolve, sep } from 'path';
|
|
44
|
+
import { buildGoGraph, buildJavaGraph, buildPythonGraph, buildTsCallGraph, deadCode, deriveStableIds, entryIndexDigest, removalReach, } from 'circle-ir';
|
|
45
|
+
import { collectFiles, SKIPPED_DIRECTORIES } from '../util/files.js';
|
|
46
|
+
import { ensureAnalyzer } from '../util/wasm.js';
|
|
47
|
+
import { DETERMINISTIC, errorResult, textResult, READ_ONLY_ANNOTATIONS } from './types.js';
|
|
48
|
+
/** Default cap on `facts[]` / `removed[]` and on `entrySet.entries`; a cut list always says so. */
|
|
49
|
+
export const DEFAULT_GRAPH_FACT_LIMIT = 500;
|
|
50
|
+
export const MAX_GRAPH_FACT_LIMIT = 5000;
|
|
51
|
+
/**
|
|
52
|
+
* Most source files read from one tree. A tree that reaches it is refused:
|
|
53
|
+
* a file left unread is a caller left out, and "no path" over part of a tree
|
|
54
|
+
* would read as an answer over all of it.
|
|
55
|
+
*/
|
|
56
|
+
export const MAX_GRAPH_FILES = 50_000;
|
|
57
|
+
/** The rule `decision` applies; versioned so a record can name it. */
|
|
58
|
+
export const DECISION_RULE = 'no-inferred-edge-decides@0.1';
|
|
59
|
+
const ENTRY_KINDS = [
|
|
60
|
+
'route', 'listener', 'scheduled', 'main', 'export', 'bean', 'reflection', 'lifecycle', 'test',
|
|
61
|
+
];
|
|
62
|
+
const CERTAINTIES = ['declared', 'conventional', 'inferred'];
|
|
63
|
+
const OUTCOMES = ['unreachable', 'reachable', 'undecided'];
|
|
64
|
+
/**
|
|
65
|
+
* What a decision rests on. `unreachable` always lists the four `no-*`
|
|
66
|
+
* facts; `reachable` lists `strong-path`; `undecided` lists every reason
|
|
67
|
+
* that applies.
|
|
68
|
+
*/
|
|
69
|
+
const BECAUSE = [
|
|
70
|
+
'no-strong-path',
|
|
71
|
+
'no-weak-path',
|
|
72
|
+
'no-unresolved-site',
|
|
73
|
+
'no-partial-file',
|
|
74
|
+
'strong-path',
|
|
75
|
+
'inferred-entry',
|
|
76
|
+
'inferred-call',
|
|
77
|
+
'reference',
|
|
78
|
+
'weak-path',
|
|
79
|
+
'unresolved-site',
|
|
80
|
+
'partial-file',
|
|
81
|
+
'partial-at-head',
|
|
82
|
+
'no-entries',
|
|
83
|
+
];
|
|
84
|
+
const require_ = createRequire(import.meta.url);
|
|
85
|
+
/** The engine that computed an answer: `circle-ir` and its version, from its manifest. */
|
|
86
|
+
export function engineIdentity() {
|
|
87
|
+
try {
|
|
88
|
+
const main = require_.resolve('circle-ir');
|
|
89
|
+
// `circle-ir`'s entry is `dist/index.js`; its manifest is two levels up.
|
|
90
|
+
const pkg = JSON.parse(readFileSync(join(main, '..', '..', 'package.json'), 'utf8'));
|
|
91
|
+
return { package: 'circle-ir', version: pkg.version ?? 'unknown' };
|
|
92
|
+
}
|
|
93
|
+
catch {
|
|
94
|
+
return { package: 'circle-ir', version: 'unknown' };
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
// ---------------------------------------------------------------------------
|
|
98
|
+
// The rule
|
|
99
|
+
/** Which weak steps the shortest weak path takes: the facts that keep it from deciding. */
|
|
100
|
+
export function weakReasons(weak) {
|
|
101
|
+
const out = new Set();
|
|
102
|
+
if (weak.entry.certainty === 'inferred')
|
|
103
|
+
out.add('inferred-entry');
|
|
104
|
+
for (const s of weak.steps) {
|
|
105
|
+
if (s.via === 'call' && s.tier === 'inferred')
|
|
106
|
+
out.add('inferred-call');
|
|
107
|
+
if (s.via === 'reference')
|
|
108
|
+
out.add('reference');
|
|
109
|
+
}
|
|
110
|
+
// A weak relation this server does not know yet still keeps the path weak.
|
|
111
|
+
if (out.size === 0)
|
|
112
|
+
out.add('weak-path');
|
|
113
|
+
return BECAUSE.filter(b => out.has(b));
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* `no-inferred-edge-decides@0.1` over one engine fact. `roots` is how many
|
|
117
|
+
* entries the walk started from; `partialReach` whether a file that did not
|
|
118
|
+
* parse could reach the declaration; `partialAtHead` whether its file did not
|
|
119
|
+
* parse at the head (`removal_reach` only).
|
|
120
|
+
*/
|
|
121
|
+
export function decide(fact, ctx) {
|
|
122
|
+
const because = [];
|
|
123
|
+
if (ctx.partialAtHead)
|
|
124
|
+
because.push('partial-at-head');
|
|
125
|
+
if (fact.strong) {
|
|
126
|
+
because.push('strong-path');
|
|
127
|
+
// A head file that did not parse loses declarations it still holds: the
|
|
128
|
+
// removal itself is not established, so a strong path cannot convict it.
|
|
129
|
+
return { outcome: ctx.partialAtHead ? 'undecided' : 'reachable', because };
|
|
130
|
+
}
|
|
131
|
+
if (ctx.roots === 0)
|
|
132
|
+
because.push('no-entries');
|
|
133
|
+
if (fact.weak)
|
|
134
|
+
because.push(...weakReasons(fact.weak));
|
|
135
|
+
if (fact.unresolvedCount > 0)
|
|
136
|
+
because.push('unresolved-site');
|
|
137
|
+
if (ctx.partialReach)
|
|
138
|
+
because.push('partial-file');
|
|
139
|
+
if (because.length > 0)
|
|
140
|
+
return { outcome: 'undecided', because };
|
|
141
|
+
return { outcome: 'unreachable', because: ['no-strong-path', 'no-weak-path', 'no-unresolved-site', 'no-partial-file'] };
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* The roots a walk started from: the entries of the root kinds, counted,
|
|
145
|
+
* digested and listed. With `files`, an entry that names no declaration in
|
|
146
|
+
* them is left out, as the engine's walk leaves it out.
|
|
147
|
+
*/
|
|
148
|
+
export function entrySet(entries, rootKinds, limit, files) {
|
|
149
|
+
const kinds = new Set(rootKinds);
|
|
150
|
+
const known = files ? new Set(files.flatMap(f => f.decls.map(d => d.id))) : null;
|
|
151
|
+
const roots = entries
|
|
152
|
+
.filter(e => kinds.has(e.kind) && (!known || known.has(e.id)))
|
|
153
|
+
.map(e => ({
|
|
154
|
+
id: e.id,
|
|
155
|
+
kind: e.kind,
|
|
156
|
+
certainty: e.certainty,
|
|
157
|
+
...(e.method ? { method: e.method } : {}),
|
|
158
|
+
...(e.path ? { path: e.path } : {}),
|
|
159
|
+
file: e.file,
|
|
160
|
+
line: e.line,
|
|
161
|
+
}))
|
|
162
|
+
.sort((a, b) => cmp(a.file, b.file) || a.line - b.line || cmp(a.id, b.id));
|
|
163
|
+
const byKind = {};
|
|
164
|
+
for (const k of ENTRY_KINDS) {
|
|
165
|
+
const n = roots.filter(r => r.kind === k).length;
|
|
166
|
+
if (n > 0)
|
|
167
|
+
byKind[k] = n;
|
|
168
|
+
}
|
|
169
|
+
const byCertainty = Object.fromEntries(CERTAINTIES.map(c => [c, roots.filter(r => r.certainty === c).length]));
|
|
170
|
+
return {
|
|
171
|
+
rootKinds: [...rootKinds],
|
|
172
|
+
total: roots.length,
|
|
173
|
+
byKind,
|
|
174
|
+
byCertainty,
|
|
175
|
+
digest: entryIndexDigest(roots),
|
|
176
|
+
entries: roots.slice(0, limit),
|
|
177
|
+
truncated: roots.length > limit,
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
function countOutcomes(decisions) {
|
|
181
|
+
const out = { unreachable: 0, reachable: 0, undecided: 0 };
|
|
182
|
+
for (const d of decisions)
|
|
183
|
+
out[d.outcome]++;
|
|
184
|
+
return out;
|
|
185
|
+
}
|
|
186
|
+
const NO_ENTRIES_NOTE = 'No entry of the root kinds was found, so nothing can be shown unreachable: every `decision` is ' +
|
|
187
|
+
'`undecided` (`no-entries`). Check `root_kinds`, or whether the engine recognises this project\'s entry points.';
|
|
188
|
+
// ---------------------------------------------------------------------------
|
|
189
|
+
// Reading a tree
|
|
190
|
+
function cmp(a, b) {
|
|
191
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
192
|
+
}
|
|
193
|
+
function toRepoPath(root, file) {
|
|
194
|
+
return relative(root, file).split(sep).join('/');
|
|
195
|
+
}
|
|
196
|
+
const FAMILY_NAMES = {
|
|
197
|
+
typescript: 'TypeScript/JavaScript',
|
|
198
|
+
java: 'Java',
|
|
199
|
+
python: 'Python',
|
|
200
|
+
go: 'Go',
|
|
201
|
+
};
|
|
202
|
+
const FAMILY_LANGUAGES = {
|
|
203
|
+
typescript: ['typescript', 'javascript'],
|
|
204
|
+
java: ['java'],
|
|
205
|
+
python: ['python'],
|
|
206
|
+
go: ['go'],
|
|
207
|
+
};
|
|
208
|
+
class TooManyFiles extends Error {
|
|
209
|
+
}
|
|
210
|
+
/** The tree's files of one family, repository-relative, in path order. */
|
|
211
|
+
function sourcesOf(root, language, maxFiles) {
|
|
212
|
+
const files = FAMILY_LANGUAGES[language].flatMap(l => collectFiles(root, { language: l, maxFiles }));
|
|
213
|
+
if (files.length >= maxFiles) {
|
|
214
|
+
throw new TooManyFiles(`${root} has ${maxFiles} or more ${FAMILY_NAMES[language]} files; an answer over part of them could ` +
|
|
215
|
+
'call live code dead. Point the tool at a smaller tree.');
|
|
216
|
+
}
|
|
217
|
+
return files
|
|
218
|
+
.map(f => ({ path: toRepoPath(root, f.filePath), code: f.code }))
|
|
219
|
+
.sort((a, b) => cmp(a.path, b.path));
|
|
220
|
+
}
|
|
221
|
+
/** Every `package.json` outside skipped directories, as manifests for the entry index. */
|
|
222
|
+
function manifests(root, sources) {
|
|
223
|
+
const dirs = new Set(['']);
|
|
224
|
+
for (const s of sources) {
|
|
225
|
+
const parts = s.path.split('/');
|
|
226
|
+
for (let i = 1; i < parts.length; i++)
|
|
227
|
+
dirs.add(parts.slice(0, i).join('/'));
|
|
228
|
+
}
|
|
229
|
+
const skipped = new Set(SKIPPED_DIRECTORIES);
|
|
230
|
+
const out = [];
|
|
231
|
+
for (const d of [...dirs].sort()) {
|
|
232
|
+
if (d.split('/').some(p => skipped.has(p)))
|
|
233
|
+
continue;
|
|
234
|
+
const file = join(root, d, 'package.json');
|
|
235
|
+
if (isPlainFile(file))
|
|
236
|
+
out.push({ root: d, packageJson: readFileSync(file, 'utf8') });
|
|
237
|
+
}
|
|
238
|
+
return out;
|
|
239
|
+
}
|
|
240
|
+
/** A regular file, not followed through a symlink (which could lead out of the tree). */
|
|
241
|
+
function isPlainFile(file) {
|
|
242
|
+
try {
|
|
243
|
+
return lstatSync(file).isFile();
|
|
244
|
+
}
|
|
245
|
+
catch {
|
|
246
|
+
return false;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
/** Console scripts from `[project.scripts]` in a root `pyproject.toml`, as `pkg.mod:func`. */
|
|
250
|
+
function consoleScripts(root) {
|
|
251
|
+
const file = join(root, 'pyproject.toml');
|
|
252
|
+
if (!isPlainFile(file))
|
|
253
|
+
return [];
|
|
254
|
+
const out = [];
|
|
255
|
+
let inScripts = false;
|
|
256
|
+
for (const raw of readFileSync(file, 'utf8').split(/\r?\n/)) {
|
|
257
|
+
const line = raw.trim();
|
|
258
|
+
if (line.startsWith('[')) {
|
|
259
|
+
inScripts = line === '[project.scripts]';
|
|
260
|
+
continue;
|
|
261
|
+
}
|
|
262
|
+
if (!inScripts)
|
|
263
|
+
continue;
|
|
264
|
+
const m = /^[A-Za-z0-9_.-]+\s*=\s*["']([A-Za-z0-9_.]+:[A-Za-z0-9_.]+)["']/.exec(line);
|
|
265
|
+
if (m)
|
|
266
|
+
out.push(m[1]);
|
|
267
|
+
}
|
|
268
|
+
return out;
|
|
269
|
+
}
|
|
270
|
+
/** The module path from a root `go.mod` (`module example.com/app`), if any. */
|
|
271
|
+
function goModulePath(root) {
|
|
272
|
+
const file = join(root, 'go.mod');
|
|
273
|
+
if (!isPlainFile(file))
|
|
274
|
+
return undefined;
|
|
275
|
+
const m = /^\s*module\s+(\S+)/m.exec(readFileSync(file, 'utf8'));
|
|
276
|
+
return m ? m[1].replace(/^"|"$/g, '') : undefined;
|
|
277
|
+
}
|
|
278
|
+
/** `META-INF/services/<interface>` files: each class they list is a `reflection` entry. */
|
|
279
|
+
function serviceFiles(root) {
|
|
280
|
+
const skipped = new Set(SKIPPED_DIRECTORIES);
|
|
281
|
+
const out = [];
|
|
282
|
+
const walk = (dir) => {
|
|
283
|
+
let names;
|
|
284
|
+
try {
|
|
285
|
+
names = readdirSync(dir).sort();
|
|
286
|
+
}
|
|
287
|
+
catch {
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
for (const name of names) {
|
|
291
|
+
if (skipped.has(name) || name.startsWith('.'))
|
|
292
|
+
continue;
|
|
293
|
+
const abs = join(dir, name);
|
|
294
|
+
let st;
|
|
295
|
+
try {
|
|
296
|
+
// lstat: a symlink is not followed, so the walk cannot leave the tree.
|
|
297
|
+
st = lstatSync(abs);
|
|
298
|
+
}
|
|
299
|
+
catch {
|
|
300
|
+
continue;
|
|
301
|
+
}
|
|
302
|
+
if (st.isDirectory())
|
|
303
|
+
walk(abs);
|
|
304
|
+
else if (st.isFile() && /(^|[\\/])META-INF[\\/]services[\\/][^\\/]+$/.test(abs)) {
|
|
305
|
+
out.push({ path: toRepoPath(root, abs), text: readFileSync(abs, 'utf8') });
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
};
|
|
309
|
+
walk(root);
|
|
310
|
+
return out;
|
|
311
|
+
}
|
|
312
|
+
function familiesIn(root) {
|
|
313
|
+
const present = new Set();
|
|
314
|
+
for (const family of Object.keys(FAMILY_LANGUAGES)) {
|
|
315
|
+
if (FAMILY_LANGUAGES[family].some(l => collectFiles(root, { language: l, maxFiles: 1 }).length > 0))
|
|
316
|
+
present.add(family);
|
|
317
|
+
}
|
|
318
|
+
return present;
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* The family to build: the one asked for, else the only one present. A tree
|
|
322
|
+
* with more than one family must say which; a tree with none of the chosen
|
|
323
|
+
* family is refused, because an empty graph would call nothing reachable (or,
|
|
324
|
+
* at the head of `removal_reach`, everything removed).
|
|
325
|
+
*/
|
|
326
|
+
function pickLanguage(trees, asked) {
|
|
327
|
+
const present = trees.map(t => ({ ...t, families: familiesIn(t.root) }));
|
|
328
|
+
let language;
|
|
329
|
+
if (asked) {
|
|
330
|
+
language = asked === 'javascript' ? 'typescript' : asked;
|
|
331
|
+
}
|
|
332
|
+
else {
|
|
333
|
+
const all = new Set(present.flatMap(t => [...t.families]));
|
|
334
|
+
const found = ['java', 'typescript', 'python', 'go'].filter(l => all.has(l));
|
|
335
|
+
if (found.length > 1) {
|
|
336
|
+
return { error: `the tree has ${found.map(l => FAMILY_NAMES[l]).join(', ')} files: pass \`language\`` };
|
|
337
|
+
}
|
|
338
|
+
if (found.length === 0)
|
|
339
|
+
return { error: `no Java, TypeScript, JavaScript, Python or Go files under ${trees.map(t => t.name).join(' or ')}` };
|
|
340
|
+
language = found[0];
|
|
341
|
+
}
|
|
342
|
+
const empty = present.find(t => !t.families.has(language));
|
|
343
|
+
if (empty)
|
|
344
|
+
return { error: `no ${FAMILY_NAMES[language]} files under ${empty.name}: ${empty.root}` };
|
|
345
|
+
return { language };
|
|
346
|
+
}
|
|
347
|
+
/** The engine's reach input for one tree, with how many files it read. */
|
|
348
|
+
async function buildGraph(root, language, maxFiles) {
|
|
349
|
+
if (language === 'java') {
|
|
350
|
+
const files = sourcesOf(root, 'java', maxFiles);
|
|
351
|
+
return { graph: await buildJavaGraph(files, { serviceFiles: serviceFiles(root) }), files: files.length };
|
|
352
|
+
}
|
|
353
|
+
if (language === 'python') {
|
|
354
|
+
const files = sourcesOf(root, 'python', maxFiles);
|
|
355
|
+
return { graph: await buildPythonGraph(files, { consoleScripts: consoleScripts(root) }), files: files.length };
|
|
356
|
+
}
|
|
357
|
+
if (language === 'go') {
|
|
358
|
+
const files = sourcesOf(root, 'go', maxFiles);
|
|
359
|
+
const modulePath = goModulePath(root);
|
|
360
|
+
return { graph: await buildGoGraph(files, modulePath ? { modulePath } : {}), files: files.length };
|
|
361
|
+
}
|
|
362
|
+
const sources = sourcesOf(root, 'typescript', maxFiles);
|
|
363
|
+
return { graph: await buildTsCallGraph(sources, { packages: manifests(root, sources) }), files: sources.length };
|
|
364
|
+
}
|
|
365
|
+
/** The head's stable ids (no graph is needed at the head). */
|
|
366
|
+
async function headIds(root, language, maxFiles) {
|
|
367
|
+
if (language === 'java')
|
|
368
|
+
return (await buildJavaGraph(sourcesOf(root, 'java', maxFiles))).files;
|
|
369
|
+
if (language === 'python')
|
|
370
|
+
return (await buildPythonGraph(sourcesOf(root, 'python', maxFiles))).files;
|
|
371
|
+
if (language === 'go') {
|
|
372
|
+
const modulePath = goModulePath(root);
|
|
373
|
+
return (await buildGoGraph(sourcesOf(root, 'go', maxFiles), modulePath ? { modulePath } : {})).files;
|
|
374
|
+
}
|
|
375
|
+
const out = [];
|
|
376
|
+
for (const s of sourcesOf(root, 'typescript', maxFiles)) {
|
|
377
|
+
const lang = /\.(ts|tsx|mts|cts)$/.test(s.path) ? 'typescript' : 'javascript';
|
|
378
|
+
out.push(await deriveStableIds(s.code, s.path, lang));
|
|
379
|
+
}
|
|
380
|
+
return out;
|
|
381
|
+
}
|
|
382
|
+
/** What was not read: the denominator for "no path". */
|
|
383
|
+
function excluded() {
|
|
384
|
+
return [
|
|
385
|
+
...SKIPPED_DIRECTORIES.map(d => ({ pattern: `**/${d}/**`, reason: 'not project source' })),
|
|
386
|
+
{ pattern: '**/.*', reason: 'hidden file or directory' },
|
|
387
|
+
];
|
|
388
|
+
}
|
|
389
|
+
function checkRoot(path, name) {
|
|
390
|
+
const abs = resolve(path);
|
|
391
|
+
if (!existsSync(abs))
|
|
392
|
+
return `${name} does not exist: ${abs}`;
|
|
393
|
+
if (!statSync(abs).isDirectory())
|
|
394
|
+
return `${name} is not a directory: ${abs}`;
|
|
395
|
+
return null;
|
|
396
|
+
}
|
|
397
|
+
/** `child` is `parent` or inside it. */
|
|
398
|
+
function within(child, parent) {
|
|
399
|
+
const rel = relative(parent, child);
|
|
400
|
+
return rel === '' || (rel !== '..' && !rel.startsWith(`..${sep}`) && !isAbsolute(rel));
|
|
401
|
+
}
|
|
402
|
+
function uniqueKinds(kinds) {
|
|
403
|
+
return kinds ? ENTRY_KINDS.filter(k => kinds.includes(k)) : undefined;
|
|
404
|
+
}
|
|
405
|
+
// ---------------------------------------------------------------------------
|
|
406
|
+
// Schemas
|
|
407
|
+
const rootKindsShape = z
|
|
408
|
+
.array(z.enum(ENTRY_KINDS))
|
|
409
|
+
.min(1)
|
|
410
|
+
.optional()
|
|
411
|
+
.describe('Entry kinds used as roots. Default: every kind except `test` (code only tests reach is dead product code). ' +
|
|
412
|
+
'An empty list is refused: with no roots, nothing could be shown reachable.');
|
|
413
|
+
const limitShape = z
|
|
414
|
+
.number()
|
|
415
|
+
.int()
|
|
416
|
+
.positive()
|
|
417
|
+
.max(MAX_GRAPH_FACT_LIMIT)
|
|
418
|
+
.optional()
|
|
419
|
+
.describe(`Most items returned in the fact list and in \`entrySet.entries\`, each (default ${DEFAULT_GRAPH_FACT_LIMIT}, ` +
|
|
420
|
+
`max ${MAX_GRAPH_FACT_LIMIT}). Counts are over everything; a cut list says so.`);
|
|
421
|
+
const languageShape = z
|
|
422
|
+
.enum(['typescript', 'javascript', 'java', 'python', 'go'])
|
|
423
|
+
.optional()
|
|
424
|
+
.describe('Which graph to build: `java`, `python`, `go`, or `typescript` / `javascript` (one graph for both). ' +
|
|
425
|
+
'Default: the only family present; a tree with more than one must say.');
|
|
426
|
+
const siteShape = z.looseObject({ file: z.string(), line: z.number(), col: z.number() });
|
|
427
|
+
const pathShape = z.looseObject({
|
|
428
|
+
class: z.enum(['strong', 'weak']),
|
|
429
|
+
entry: z.looseObject({ id: z.string(), kind: z.string(), certainty: z.string() }),
|
|
430
|
+
steps: z.array(z.looseObject({ decl: z.string(), via: z.string() })),
|
|
431
|
+
});
|
|
432
|
+
const decisionShape = z.object({ outcome: z.enum(OUTCOMES), because: z.array(z.enum(BECAUSE)) });
|
|
433
|
+
const factFields = {
|
|
434
|
+
id: z.string(),
|
|
435
|
+
qualified_name: z.string(),
|
|
436
|
+
kind: z.string(),
|
|
437
|
+
file: z.string(),
|
|
438
|
+
strong: pathShape.nullable(),
|
|
439
|
+
weak: pathShape.nullable(),
|
|
440
|
+
unresolved: z.array(z.looseObject({ kind: z.string(), reason: z.string(), site: siteShape })),
|
|
441
|
+
unresolvedCount: z.number().int(),
|
|
442
|
+
};
|
|
443
|
+
const entrySetShape = z.object({
|
|
444
|
+
rootKinds: z.array(z.string()),
|
|
445
|
+
total: z.number().int(),
|
|
446
|
+
byKind: z.record(z.string(), z.number().int()),
|
|
447
|
+
byCertainty: z.object({ declared: z.number().int(), conventional: z.number().int(), inferred: z.number().int() }),
|
|
448
|
+
digest: z.string(),
|
|
449
|
+
entries: z.array(z.object({
|
|
450
|
+
id: z.string(),
|
|
451
|
+
kind: z.string(),
|
|
452
|
+
certainty: z.string(),
|
|
453
|
+
method: z.string().optional(),
|
|
454
|
+
path: z.string().optional(),
|
|
455
|
+
file: z.string(),
|
|
456
|
+
line: z.number().int(),
|
|
457
|
+
})),
|
|
458
|
+
truncated: z.boolean(),
|
|
459
|
+
});
|
|
460
|
+
const excludedShape = z.array(z.object({ pattern: z.string(), reason: z.string() }));
|
|
461
|
+
const engineShape = z.object({ package: z.literal('circle-ir'), version: z.string() });
|
|
462
|
+
const graphLanguageShape = z.enum(['typescript', 'java', 'python', 'go']);
|
|
463
|
+
// ---------------------------------------------------------------------------
|
|
464
|
+
// find_dead_code
|
|
465
|
+
export const findDeadCodeInputShape = {
|
|
466
|
+
project_root: z.string().describe('Absolute path to the project root (Java, Python, Go, or TypeScript / JavaScript).'),
|
|
467
|
+
language: languageShape,
|
|
468
|
+
root_kinds: rootKindsShape,
|
|
469
|
+
verdicts: z
|
|
470
|
+
.array(z.enum(['live', 'may-be-live', 'dead']))
|
|
471
|
+
.min(1)
|
|
472
|
+
.optional()
|
|
473
|
+
.describe('Return only facts with these verdicts. Default: all three. `summary` and `decisions` always count every fact.'),
|
|
474
|
+
limit: limitShape,
|
|
475
|
+
};
|
|
476
|
+
export const findDeadCodeOutputShape = {
|
|
477
|
+
projectRoot: z.string(),
|
|
478
|
+
language: graphLanguageShape,
|
|
479
|
+
engine: engineShape,
|
|
480
|
+
scheme: z.literal('dead-code@0.1'),
|
|
481
|
+
ids: z.literal('ids@0.1'),
|
|
482
|
+
rootKinds: z.array(z.string()),
|
|
483
|
+
entryIndexDigest: z.string(),
|
|
484
|
+
partial: z.array(z.string()),
|
|
485
|
+
partialFiles: z.array(z.looseObject({ file: z.string() })),
|
|
486
|
+
summary: z.looseObject({ live: z.number(), 'may-be-live': z.number(), dead: z.number() }),
|
|
487
|
+
decisions: z.object({
|
|
488
|
+
rule: z.literal(DECISION_RULE),
|
|
489
|
+
unreachable: z.number().int(),
|
|
490
|
+
reachable: z.number().int(),
|
|
491
|
+
undecided: z.number().int(),
|
|
492
|
+
}),
|
|
493
|
+
totalFacts: z.number().int(),
|
|
494
|
+
truncated: z.boolean(),
|
|
495
|
+
facts: z.array(z.looseObject({
|
|
496
|
+
...factFields,
|
|
497
|
+
verdict: z.enum(['live', 'may-be-live', 'dead']),
|
|
498
|
+
reasons: z.array(z.string()),
|
|
499
|
+
reachedByTest: z.boolean(),
|
|
500
|
+
test: z.string().optional(),
|
|
501
|
+
decision: decisionShape,
|
|
502
|
+
})),
|
|
503
|
+
entrySet: entrySetShape,
|
|
504
|
+
scope: z.object({
|
|
505
|
+
searched: z.object({ files: z.number().int(), languages: z.array(z.string()) }),
|
|
506
|
+
excluded: excludedShape,
|
|
507
|
+
}),
|
|
508
|
+
note: z.string().optional(),
|
|
509
|
+
provenance: z.string(),
|
|
510
|
+
};
|
|
511
|
+
export const findDeadCodeConfig = {
|
|
512
|
+
title: 'Find dead code',
|
|
513
|
+
description: 'Per declaration of a Java, Python, Go or TypeScript / JavaScript project, whether an entry point reaches it ' +
|
|
514
|
+
'(the engine\'s `dead-code@0.1`): `live` (a strong path), `may-be-live` (only a weak path, an unresolved site ' +
|
|
515
|
+
'that could reach it, or a file that did not parse), or `dead`. Each fact carries the shortest strong and weak ' +
|
|
516
|
+
'path, the unresolved sites, and `reachedByTest` with the first test that reaches it (remove or update that ' +
|
|
517
|
+
'test with the code). Test files and type-only declarations are left out.\n\n' +
|
|
518
|
+
'Each fact also carries `decision` under the rule `no-inferred-edge-decides@0.1`: `unreachable` only when no ' +
|
|
519
|
+
'path of any kind leads to it from any root in `entrySet` (`because` lists the four facts that show it), ' +
|
|
520
|
+
'`reachable` on a strong path, and `undecided` when an inferred entry, an inferred call, a reference, an ' +
|
|
521
|
+
'unresolved site or an unparsed file is all that keeps it alive — a person decides those. With no roots, ' +
|
|
522
|
+
'everything is `undecided`. `entrySet` lists the roots used; `scope` says how many files were read. ' +
|
|
523
|
+
'A fact, never a removal on its own.',
|
|
524
|
+
inputSchema: findDeadCodeInputShape,
|
|
525
|
+
outputSchema: findDeadCodeOutputShape,
|
|
526
|
+
annotations: READ_ONLY_ANNOTATIONS,
|
|
527
|
+
};
|
|
528
|
+
export function makeFindDeadCodeHandler(opts = {}) {
|
|
529
|
+
const maxFiles = opts.maxFiles ?? MAX_GRAPH_FILES;
|
|
530
|
+
return async (args) => {
|
|
531
|
+
const bad = checkRoot(args.project_root, 'project_root');
|
|
532
|
+
if (bad)
|
|
533
|
+
return errorResult(bad);
|
|
534
|
+
if (args.root_kinds?.length === 0)
|
|
535
|
+
return errorResult('root_kinds is empty: pass at least one entry kind, or leave it out');
|
|
536
|
+
if (args.verdicts?.length === 0)
|
|
537
|
+
return errorResult('verdicts is empty: pass at least one verdict, or leave it out');
|
|
538
|
+
await ensureAnalyzer();
|
|
539
|
+
const root = resolve(args.project_root);
|
|
540
|
+
const picked = pickLanguage([{ name: 'project_root', root }], args.language);
|
|
541
|
+
if ('error' in picked)
|
|
542
|
+
return errorResult(picked.error);
|
|
543
|
+
let built;
|
|
544
|
+
try {
|
|
545
|
+
built = await buildGraph(root, picked.language, maxFiles);
|
|
546
|
+
}
|
|
547
|
+
catch (err) {
|
|
548
|
+
if (err instanceof TooManyFiles)
|
|
549
|
+
return errorResult(err.message);
|
|
550
|
+
throw err;
|
|
551
|
+
}
|
|
552
|
+
const { graph, files } = built;
|
|
553
|
+
const rootKinds = uniqueKinds(args.root_kinds);
|
|
554
|
+
const result = deadCode(graph, rootKinds ? { rootKinds } : {});
|
|
555
|
+
const limit = args.limit ?? DEFAULT_GRAPH_FACT_LIMIT;
|
|
556
|
+
const roots = entrySet(graph.entries, result.rootKinds, limit, graph.files);
|
|
557
|
+
const decided = result.facts.map(f => ({
|
|
558
|
+
...f,
|
|
559
|
+
decision: decide(f, { roots: roots.total, partialReach: f.reasons.includes('partial-files') }),
|
|
560
|
+
}));
|
|
561
|
+
const wanted = args.verdicts ? new Set(args.verdicts) : null;
|
|
562
|
+
const facts = wanted ? decided.filter(f => wanted.has(f.verdict)) : decided;
|
|
563
|
+
const payload = {
|
|
564
|
+
projectRoot: root,
|
|
565
|
+
language: picked.language,
|
|
566
|
+
engine: engineIdentity(),
|
|
567
|
+
scheme: result.scheme,
|
|
568
|
+
ids: result.ids,
|
|
569
|
+
rootKinds: result.rootKinds,
|
|
570
|
+
entryIndexDigest: entryIndexDigest(graph.entries),
|
|
571
|
+
partial: result.partial,
|
|
572
|
+
partialFiles: result.partialFiles,
|
|
573
|
+
summary: result.summary,
|
|
574
|
+
decisions: { rule: DECISION_RULE, ...countOutcomes(decided.map(f => f.decision)) },
|
|
575
|
+
totalFacts: facts.length,
|
|
576
|
+
truncated: facts.length > limit,
|
|
577
|
+
facts: facts.slice(0, limit),
|
|
578
|
+
entrySet: roots,
|
|
579
|
+
scope: { searched: { files, languages: [...FAMILY_LANGUAGES[picked.language]] }, excluded: excluded() },
|
|
580
|
+
...(roots.total === 0 ? { note: NO_ENTRIES_NOTE } : {}),
|
|
581
|
+
};
|
|
582
|
+
return textResult(payload, { ...payload, provenance: DETERMINISTIC });
|
|
583
|
+
};
|
|
584
|
+
}
|
|
585
|
+
// ---------------------------------------------------------------------------
|
|
586
|
+
// removal_reach
|
|
587
|
+
export const removalReachInputShape = {
|
|
588
|
+
project_root: z.string().describe('Absolute path to the head tree (the pin under test).'),
|
|
589
|
+
baseline_root: z.string().describe('Absolute path to the base tree (the pin before the change). Must not overlap the head.'),
|
|
590
|
+
language: languageShape,
|
|
591
|
+
root_kinds: rootKindsShape,
|
|
592
|
+
limit: limitShape,
|
|
593
|
+
};
|
|
594
|
+
export const removalReachOutputShape = {
|
|
595
|
+
projectRoot: z.string(),
|
|
596
|
+
baselineRoot: z.string(),
|
|
597
|
+
language: graphLanguageShape,
|
|
598
|
+
engine: engineShape,
|
|
599
|
+
scheme: z.literal('removal-reach@0.1'),
|
|
600
|
+
ids: z.literal('ids@0.1'),
|
|
601
|
+
rootKinds: z.array(z.string()),
|
|
602
|
+
entryIndexDigest: z.string(),
|
|
603
|
+
partial: z.array(z.string()),
|
|
604
|
+
partialFiles: z.array(z.looseObject({ file: z.string() })),
|
|
605
|
+
moved: z.array(z.looseObject({ from: z.string(), to: z.string(), relation: z.string(), evidence: z.string() })),
|
|
606
|
+
decisions: z.object({
|
|
607
|
+
rule: z.literal(DECISION_RULE),
|
|
608
|
+
unreachable: z.number().int(),
|
|
609
|
+
reachable: z.number().int(),
|
|
610
|
+
undecided: z.number().int(),
|
|
611
|
+
allUnreachable: z.boolean().nullable(),
|
|
612
|
+
}),
|
|
613
|
+
totalRemoved: z.number().int(),
|
|
614
|
+
truncated: z.boolean(),
|
|
615
|
+
removed: z.array(z.looseObject({
|
|
616
|
+
...factFields,
|
|
617
|
+
moveCandidates: z.array(z.looseObject({ to: z.string(), relation: z.string() })),
|
|
618
|
+
decision: decisionShape,
|
|
619
|
+
})),
|
|
620
|
+
entrySet: entrySetShape,
|
|
621
|
+
scope: z.object({
|
|
622
|
+
searched: z.object({ files: z.number().int(), headFiles: z.number().int(), languages: z.array(z.string()) }),
|
|
623
|
+
excluded: excludedShape,
|
|
624
|
+
}),
|
|
625
|
+
note: z.string().optional(),
|
|
626
|
+
provenance: z.string(),
|
|
627
|
+
};
|
|
628
|
+
export const removalReachConfig = {
|
|
629
|
+
title: 'Removal reach',
|
|
630
|
+
description: 'For a change between two checked-out trees of a Java, Python, Go or TypeScript / JavaScript project, every ' +
|
|
631
|
+
'declaration that disappeared (moves and renames are listed apart in `moved[]`), with what reached it at the ' +
|
|
632
|
+
'base (the engine\'s `removal-reach@0.1`): the shortest strong path from an entry, the shortest weak path, the ' +
|
|
633
|
+
'unresolved sites that could reach it, and move candidates that might explain it. `partial[]` lists files that ' +
|
|
634
|
+
'did not parse at either pin.\n\n' +
|
|
635
|
+
'Each removed declaration carries `decision` under the rule `no-inferred-edge-decides@0.1`, read at the base: ' +
|
|
636
|
+
'`reachable` (a strong path: live code was removed), `unreachable` (no path of any kind from any root in ' +
|
|
637
|
+
'`entrySet`, nothing unresolved, no unparsed file could reach it), or `undecided` (only an inferred entry, an ' +
|
|
638
|
+
'inferred call, a reference, an unresolved site or an unparsed file — or its file did not parse at the head, ' +
|
|
639
|
+
'so it may not be removed at all): a person decides those. `decisions.allUnreachable` is `true` only when ' +
|
|
640
|
+
'something was removed and every removal is `unreachable`, `false` when any is `reachable`, else `null`. ' +
|
|
641
|
+
'`entrySet` lists the base roots used. Tests are not roots by default; pass `root_kinds: ["test"]` to see ' +
|
|
642
|
+
'which kept tests reach a removal.',
|
|
643
|
+
inputSchema: removalReachInputShape,
|
|
644
|
+
outputSchema: removalReachOutputShape,
|
|
645
|
+
annotations: READ_ONLY_ANNOTATIONS,
|
|
646
|
+
};
|
|
647
|
+
/**
|
|
648
|
+
* The base declarations a file that did not parse could reach, by the
|
|
649
|
+
* engine's own `dead-code@0.1` reasoning (`partial-files`). With no entries
|
|
650
|
+
* and no roots every declaration is reported, and its only possible reason
|
|
651
|
+
* is that one.
|
|
652
|
+
*/
|
|
653
|
+
function partialReach(base) {
|
|
654
|
+
if (base.partial.length === 0)
|
|
655
|
+
return new Set();
|
|
656
|
+
const scoped = deadCode({ ...base, entries: [], typeOnly: [] }, { rootKinds: [] });
|
|
657
|
+
return new Set(scoped.facts.filter(f => f.reasons.includes('partial-files')).map(f => f.id));
|
|
658
|
+
}
|
|
659
|
+
export function makeRemovalReachHandler(opts = {}) {
|
|
660
|
+
const maxFiles = opts.maxFiles ?? MAX_GRAPH_FILES;
|
|
661
|
+
return async (args) => {
|
|
662
|
+
const bad = checkRoot(args.project_root, 'project_root') ?? checkRoot(args.baseline_root, 'baseline_root');
|
|
663
|
+
if (bad)
|
|
664
|
+
return errorResult(bad);
|
|
665
|
+
if (args.root_kinds?.length === 0)
|
|
666
|
+
return errorResult('root_kinds is empty: pass at least one entry kind, or leave it out');
|
|
667
|
+
const head = resolve(args.project_root);
|
|
668
|
+
const base = resolve(args.baseline_root);
|
|
669
|
+
if (within(head, base) || within(base, head)) {
|
|
670
|
+
return errorResult(head === base
|
|
671
|
+
? 'project_root and baseline_root are the same directory: pass two checkouts, the head and the base'
|
|
672
|
+
: 'project_root and baseline_root overlap (one holds the other): pass two separate checkouts');
|
|
673
|
+
}
|
|
674
|
+
await ensureAnalyzer();
|
|
675
|
+
const picked = pickLanguage([
|
|
676
|
+
{ name: 'baseline_root', root: base },
|
|
677
|
+
{ name: 'project_root', root: head },
|
|
678
|
+
], args.language);
|
|
679
|
+
if ('error' in picked)
|
|
680
|
+
return errorResult(picked.error);
|
|
681
|
+
let built;
|
|
682
|
+
let headFiles;
|
|
683
|
+
try {
|
|
684
|
+
built = await buildGraph(base, picked.language, maxFiles);
|
|
685
|
+
headFiles = await headIds(head, picked.language, maxFiles);
|
|
686
|
+
}
|
|
687
|
+
catch (err) {
|
|
688
|
+
if (err instanceof TooManyFiles)
|
|
689
|
+
return errorResult(err.message);
|
|
690
|
+
throw err;
|
|
691
|
+
}
|
|
692
|
+
const baseGraph = built.graph;
|
|
693
|
+
const rootKinds = uniqueKinds(args.root_kinds);
|
|
694
|
+
const result = removalReach(baseGraph, headFiles, rootKinds ? { rootKinds } : {});
|
|
695
|
+
const limit = args.limit ?? DEFAULT_GRAPH_FACT_LIMIT;
|
|
696
|
+
const roots = entrySet(baseGraph.entries, result.rootKinds, limit, baseGraph.files);
|
|
697
|
+
const reachedByPartial = partialReach(baseGraph);
|
|
698
|
+
const partialAtHead = new Set(headFiles.filter(f => f.partial).map(f => f.file));
|
|
699
|
+
const removed = result.removed.map(r => ({
|
|
700
|
+
...r,
|
|
701
|
+
decision: decide(r, {
|
|
702
|
+
roots: roots.total,
|
|
703
|
+
partialReach: reachedByPartial.has(r.id),
|
|
704
|
+
partialAtHead: partialAtHead.has(r.file),
|
|
705
|
+
}),
|
|
706
|
+
}));
|
|
707
|
+
const counts = countOutcomes(removed.map(r => r.decision));
|
|
708
|
+
const allUnreachable = counts.reachable > 0 ? false : removed.length > 0 && counts.undecided === 0 ? true : null;
|
|
709
|
+
const payload = {
|
|
710
|
+
projectRoot: head,
|
|
711
|
+
baselineRoot: base,
|
|
712
|
+
language: picked.language,
|
|
713
|
+
engine: engineIdentity(),
|
|
714
|
+
scheme: result.scheme,
|
|
715
|
+
ids: result.ids,
|
|
716
|
+
rootKinds: result.rootKinds,
|
|
717
|
+
entryIndexDigest: result.entryIndexDigest,
|
|
718
|
+
partial: result.partial,
|
|
719
|
+
partialFiles: result.partialFiles,
|
|
720
|
+
moved: result.moved,
|
|
721
|
+
decisions: { rule: DECISION_RULE, ...counts, allUnreachable },
|
|
722
|
+
totalRemoved: removed.length,
|
|
723
|
+
truncated: removed.length > limit,
|
|
724
|
+
removed: removed.slice(0, limit),
|
|
725
|
+
entrySet: roots,
|
|
726
|
+
scope: {
|
|
727
|
+
searched: { files: built.files, headFiles: headFiles.length, languages: [...FAMILY_LANGUAGES[picked.language]] },
|
|
728
|
+
excluded: excluded(),
|
|
729
|
+
},
|
|
730
|
+
...(roots.total === 0 ? { note: NO_ENTRIES_NOTE } : {}),
|
|
731
|
+
};
|
|
732
|
+
return textResult(payload, { ...payload, provenance: DETERMINISTIC });
|
|
733
|
+
};
|
|
734
|
+
}
|
|
735
|
+
//# sourceMappingURL=graph-facts.js.map
|