@krisada-m/ksd-intelligence 0.0.0-stage → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.antigravity-plugin/plugin.json +20 -0
- package/LICENSE +21 -0
- package/README.md +195 -2
- package/agy-skills/erosiano-review/SKILL.md +17 -0
- package/dist/adapters/agy/skills.js +252 -0
- package/dist/adapters/cli/check-changed.js +3 -0
- package/dist/adapters/cli/main.js +1013 -0
- package/dist/adapters/cli/policy-bin.js +10 -0
- package/dist/adapters/cli/review-verdict.js +3 -0
- package/dist/adapters/omp/extension.js +884 -0
- package/dist/adapters/omp/skills.js +130 -0
- package/dist/adapters/ui/hud.js +389 -0
- package/dist/adapters/ui/tree.js +471 -0
- package/dist/core/audit/index.js +2 -0
- package/dist/core/audit/log.js +505 -0
- package/dist/core/audit/summary.js +188 -0
- package/dist/core/changes/changes.js +591 -0
- package/dist/core/changes/git.js +266 -0
- package/dist/core/changes/plan.js +109 -0
- package/dist/core/changes/render.js +356 -0
- package/dist/core/engine/build.js +178 -0
- package/dist/core/engine/child.js +389 -0
- package/dist/core/engine/host.js +206 -0
- package/dist/core/engine/lock.js +133 -0
- package/dist/core/engine/protocol.js +43 -0
- package/dist/core/engine/runtime.js +44 -0
- package/dist/core/engine/status.js +452 -0
- package/dist/core/engine/typescript.js +194 -0
- package/dist/core/engine/workspace.js +72 -0
- package/dist/core/graph/graph.js +127 -0
- package/dist/core/graph/limits.js +16 -0
- package/dist/core/impact/query.js +1292 -0
- package/dist/core/impact/render.js +590 -0
- package/dist/core/impact/session.js +694 -0
- package/dist/core/indexing/declarations.js +455 -0
- package/dist/core/indexing/differential.js +345 -0
- package/dist/core/indexing/doctor.js +120 -0
- package/dist/core/indexing/indexer.js +811 -0
- package/dist/core/indexing/keys.js +451 -0
- package/dist/core/indexing/lexical.js +212 -0
- package/dist/core/indexing/packages.js +57 -0
- package/dist/core/indexing/program.js +171 -0
- package/dist/core/indexing/resolve.js +1004 -0
- package/dist/core/indexing/stamp.js +52 -0
- package/dist/core/indexing/surface.js +335 -0
- package/dist/core/indexing/ui-progress.js +37 -0
- package/dist/core/indexing/verify.js +76 -0
- package/dist/core/kernel/config.js +383 -0
- package/dist/core/kernel/glob.js +105 -0
- package/dist/core/kernel/guards.js +4 -0
- package/dist/core/kernel/types.js +3 -0
- package/dist/core/kernel/verdict.js +142 -0
- package/dist/core/knowledge/anchors.js +139 -0
- package/dist/core/knowledge/knowledge.js +95 -0
- package/dist/core/knowledge/learning.js +102 -0
- package/dist/core/knowledge/render.js +76 -0
- package/dist/core/knowledge/store.js +109 -0
- package/dist/core/knowledge/yaml.js +87 -0
- package/dist/core/search/lexical.js +382 -0
- package/dist/core/search/render.js +98 -0
- package/dist/core/search/segments.js +118 -0
- package/dist/core/verify/parse.js +127 -0
- package/dist/core/verify/render.js +67 -0
- package/dist/core/verify/report.js +40 -0
- package/dist/core/verify/revision.js +79 -0
- package/dist/core/verify/run.js +117 -0
- package/dist/core/verify/store.js +66 -0
- package/dist/core/verify/verify.js +173 -0
- package/dist/core/workspace/init.js +267 -0
- package/dist/core/workspace/probe.js +56 -0
- package/dist/core/workspace/scan.js +420 -0
- package/dist/engineering/baseline.js +337 -0
- package/dist/engineering/change-scope.js +500 -0
- package/dist/engineering/check-changed.js +40 -0
- package/dist/engineering/config.js +129 -0
- package/dist/engineering/dist/policy.d.ts +17 -0
- package/dist/engineering/dist/policy.js +150 -0
- package/dist/engineering/dist/runtime.d.ts +40 -0
- package/dist/engineering/dist/runtime.js +77 -0
- package/dist/engineering/dist/sdlc.d.ts +7 -0
- package/dist/engineering/dist/sdlc.js +27 -0
- package/dist/engineering/eslint-rules/index.js +107 -0
- package/dist/engineering/index.d.ts +7 -0
- package/dist/engineering/index.js +319 -0
- package/dist/engineering/intelligence.js +44 -0
- package/dist/engineering/package.json +36 -0
- package/dist/engineering/policy.js +2 -0
- package/dist/engineering/practice-tool.js +72 -0
- package/dist/engineering/practices.js +283 -0
- package/dist/engineering/practices.json +2453 -0
- package/dist/engineering/review-verdict.js +262 -0
- package/dist/engineering/sdlc.js +2 -0
- package/dist/index.js +20 -0
- package/dist/storage/records.js +245 -0
- package/dist/storage/sqlite.js +314 -0
- package/dist/workflow/catalog.js +69 -0
- package/dist/workflow/classify.js +55 -0
- package/dist/workflow/loader.js +140 -0
- package/dist/workflow/packs.js +469 -0
- package/dist/workflow/registry.js +359 -0
- package/dist/workflow/render.js +61 -0
- package/dist/workflow/router.js +141 -0
- package/dist/workflow/sources.js +229 -0
- package/dist/workflow/types.js +44 -0
- package/dist/workflow/updates.js +532 -0
- package/engineering.lock.json +94 -0
- package/package.json +81 -4
- package/rules/engineering.md +20 -0
- package/scripts/agy-mcp.mjs +27 -0
- package/scripts/mcp/engineering.mjs +91 -0
- package/scripts/mcp/stdio.mjs +304 -0
- package/scripts/mcp/tools.mjs +491 -0
- package/skills/ksd-intelligence/SKILL.md +54 -0
- package/skills/matt-sdlc/SKILL.md +42 -0
- package/skills/osmani-quality/SKILL.md +38 -0
- package/skills/poteto-mode/SKILL.md +40 -0
- package/skills/pstack-analysis/SKILL.md +36 -0
- package/skills.lock.json +71 -0
- package/skills.registry.json +181 -0
|
@@ -0,0 +1,1292 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { posix } from 'node:path';
|
|
3
|
+
import { engineVersion } from '../engine/typescript.js';
|
|
4
|
+
import { Graph } from '../graph/graph.js';
|
|
5
|
+
import { repositoryLimits } from '../graph/limits.js';
|
|
6
|
+
import { MODULE_NAME } from '../indexing/declarations.js';
|
|
7
|
+
import { isTestFile } from '../indexing/resolve.js';
|
|
8
|
+
import { matchAny, matchGlob } from '../kernel/glob.js';
|
|
9
|
+
import { isRecord } from '../kernel/guards.js';
|
|
10
|
+
import { deriveVerdict, matchedRiskZones } from '../kernel/verdict.js';
|
|
11
|
+
import { readMeta } from '../../storage/sqlite.js';
|
|
12
|
+
/** G14: the traversal always runs to closure; this budget is the only stop. */
|
|
13
|
+
const NODE_BUDGET = 50_000;
|
|
14
|
+
const EDGE_KINDS = {
|
|
15
|
+
CALLS: true,
|
|
16
|
+
REFERENCES: true,
|
|
17
|
+
CONSTRUCTS: true,
|
|
18
|
+
IMPLEMENTS: true,
|
|
19
|
+
};
|
|
20
|
+
const STRENGTHS = {
|
|
21
|
+
compiler: true,
|
|
22
|
+
'syntax-exact': true,
|
|
23
|
+
'scope-resolved': true,
|
|
24
|
+
heuristic: true,
|
|
25
|
+
unknown: true,
|
|
26
|
+
};
|
|
27
|
+
// docs/RISK.md §3.2, matched by specifier text (installed or not), subpaths included.
|
|
28
|
+
const SQL_MODULES = [
|
|
29
|
+
'pg',
|
|
30
|
+
'postgres',
|
|
31
|
+
'mysql2',
|
|
32
|
+
'better-sqlite3',
|
|
33
|
+
'drizzle-orm',
|
|
34
|
+
'@prisma/client',
|
|
35
|
+
'kysely',
|
|
36
|
+
'knex',
|
|
37
|
+
];
|
|
38
|
+
// An entry value that names code: a module path with or without a script extension.
|
|
39
|
+
const CODE_ENTRY = /(\.([cm]?[jt]sx?)|\/[^./]*)$|^[^.]*$/;
|
|
40
|
+
const ANONYMOUS_REASON = 'outside-index-scope: member of an anonymous object type is not indexed; reads through an alias of the parent type or a generic key may be missing';
|
|
41
|
+
const BATCH_BUCKETS = [1, 4, 16, 64, 256, 512, 1024];
|
|
42
|
+
const EMPTY_ANCESTORS = [];
|
|
43
|
+
const SQL_SPECIFIER_WHERE = SQL_MODULES.map((name) => `specifier = '${name}' OR (specifier >= '${name}/' AND specifier < '${name}0')`).join(' OR ');
|
|
44
|
+
/** `file#A.b.c` → `file#A.b`, `file#A`: the declarations that hold a member. */
|
|
45
|
+
function ancestors(key) {
|
|
46
|
+
const hash = key.indexOf('#');
|
|
47
|
+
if (key.indexOf('.', hash + 1) === -1)
|
|
48
|
+
return [];
|
|
49
|
+
const parts = key.slice(hash + 1).split('.');
|
|
50
|
+
const out = [];
|
|
51
|
+
for (let n = parts.length - 1; n > 0; n--)
|
|
52
|
+
out.push(`${key.slice(0, hash + 1)}${parts.slice(0, n).join('.')}`);
|
|
53
|
+
return out;
|
|
54
|
+
}
|
|
55
|
+
function declaration(row) {
|
|
56
|
+
const key = String(row.key);
|
|
57
|
+
const hash = key.indexOf('#');
|
|
58
|
+
return {
|
|
59
|
+
id: Number(row.id),
|
|
60
|
+
key,
|
|
61
|
+
file: String(row.file),
|
|
62
|
+
simple: String(row.simple),
|
|
63
|
+
kind: String(row.kind),
|
|
64
|
+
exported: Number(row.exported) === 1,
|
|
65
|
+
start: Number(row.start_line),
|
|
66
|
+
end: Number(row.end_line),
|
|
67
|
+
ancestors: key.indexOf('.', hash + 1) === -1 ? EMPTY_ANCESTORS : ancestors(key),
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
function packagesOf(raw) {
|
|
71
|
+
const parsed = JSON.parse(raw ?? '[]');
|
|
72
|
+
if (!Array.isArray(parsed))
|
|
73
|
+
throw new Error('index meta.packages is not a list');
|
|
74
|
+
return parsed.filter((item) => isRecord(item) &&
|
|
75
|
+
typeof item.dir === 'string' &&
|
|
76
|
+
typeof item.private === 'boolean' &&
|
|
77
|
+
Array.isArray(item.entries) &&
|
|
78
|
+
Array.isArray(item.unresolved));
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* `ei_impact` answers over one published index (DESIGN §6.2, golden "Result contract"). Pure reads:
|
|
82
|
+
* no TypeScript program, no source or manifest file is opened. The caller owns `db`.
|
|
83
|
+
*/
|
|
84
|
+
export class ImpactQuery {
|
|
85
|
+
db;
|
|
86
|
+
impact;
|
|
87
|
+
/** Prefix-less `import(x)`/`require(x)` sites: scoped per D55 to files nothing imports statically. */
|
|
88
|
+
noImporter = [];
|
|
89
|
+
/** Unmatched alias/package prefixes retain their pre-D55 conservative export scope. */
|
|
90
|
+
unmatchedPrefix = [];
|
|
91
|
+
graph;
|
|
92
|
+
meta;
|
|
93
|
+
engine;
|
|
94
|
+
freshness;
|
|
95
|
+
packageOf = new Map();
|
|
96
|
+
// Gap sites by how golden P4 scopes them.
|
|
97
|
+
everywhere = [];
|
|
98
|
+
byPrefix = [];
|
|
99
|
+
byFile = new Map();
|
|
100
|
+
byInterface = new Map();
|
|
101
|
+
byOwner = new Map();
|
|
102
|
+
unresolvedByName = new Map();
|
|
103
|
+
publicEntryFiles = new Set();
|
|
104
|
+
publicEntryGlobs = [];
|
|
105
|
+
publicEntryFileCache = new Map();
|
|
106
|
+
publicKeyCache = new Map();
|
|
107
|
+
openPackages = [];
|
|
108
|
+
indexableFiles = new Set();
|
|
109
|
+
indexableList = [];
|
|
110
|
+
staticImportedCache = new Map();
|
|
111
|
+
/** Entry files of workspace packages: loaded directly, never "unimported" (D55). */
|
|
112
|
+
entryFiles = new Set();
|
|
113
|
+
limits;
|
|
114
|
+
scannedFiles;
|
|
115
|
+
nodeBudget;
|
|
116
|
+
markers = new Map();
|
|
117
|
+
sqlFiles = new Set();
|
|
118
|
+
boundaryCache = new Map();
|
|
119
|
+
implementsCache = new Map();
|
|
120
|
+
implementedIdsCache = new Map();
|
|
121
|
+
incomingCache = new Map();
|
|
122
|
+
incomingFetchedIds = new Set();
|
|
123
|
+
checkedContainerKeys = new Set();
|
|
124
|
+
containerKeyToId = new Map();
|
|
125
|
+
declByKey = new Map();
|
|
126
|
+
declById = new Map();
|
|
127
|
+
containerByKeyStmts = new Map();
|
|
128
|
+
declByKeyStmts = new Map();
|
|
129
|
+
declByIdStmts = new Map();
|
|
130
|
+
incomingByIdStmts = new Map();
|
|
131
|
+
implementsById = null;
|
|
132
|
+
riskZones;
|
|
133
|
+
globalGapsLoaded = false;
|
|
134
|
+
boundarySourcesLoaded = false;
|
|
135
|
+
hasComputedGaps = null;
|
|
136
|
+
statements;
|
|
137
|
+
constructor(db, impact, options = {}) {
|
|
138
|
+
this.db = db;
|
|
139
|
+
this.impact = impact;
|
|
140
|
+
this.nodeBudget = options.nodeBudget ?? NODE_BUDGET;
|
|
141
|
+
this.riskZones = options.riskZones ?? [];
|
|
142
|
+
this.graph = new Graph(db);
|
|
143
|
+
this.meta = options.meta ?? readMeta(db);
|
|
144
|
+
this.engine = {
|
|
145
|
+
version: engineVersion(),
|
|
146
|
+
typescript: this.meta.typescript_version ?? 'unknown',
|
|
147
|
+
extractor: this.meta.extractor_stamp ?? 'unknown',
|
|
148
|
+
};
|
|
149
|
+
this.statements = {
|
|
150
|
+
declaration: db.prepare('SELECT d.id, s.key, d.file, d.simple, d.kind, d.exported, d.start_line, d.end_line FROM symbols s JOIN declarations d ON d.id = s.id WHERE s.key = ?'),
|
|
151
|
+
incoming: db.prepare(`SELECT s.key AS source, e.kind, e.strength FROM edges e JOIN symbols t ON t.id = e.target
|
|
152
|
+
JOIN symbols s ON s.id = e.source WHERE t.key = ? AND e.kind NOT IN ('IMPORTS', 'TESTS')
|
|
153
|
+
ORDER BY s.key, e.kind`),
|
|
154
|
+
implements: db.prepare(`SELECT t.key FROM edges e JOIN symbols s ON s.id = e.source JOIN symbols t ON t.id = e.target
|
|
155
|
+
WHERE s.key = ? AND e.kind = 'IMPLEMENTS'`),
|
|
156
|
+
gapByCodeDetail: db.prepare('SELECT file, line, code, detail FROM gap_sites WHERE code = ? AND detail = ? ORDER BY file, line, code, detail'),
|
|
157
|
+
unresolvedByName: db.prepare('SELECT file, line, name, reason FROM unresolved WHERE name = ? ORDER BY file, line, name'),
|
|
158
|
+
staticMarker: db.prepare('SELECT 1 FROM markers WHERE kind = ? LIMIT 1'),
|
|
159
|
+
tokens: db.prepare("SELECT file, lines FROM name_tokens WHERE name = ? AND kind = 'str' ORDER BY file"),
|
|
160
|
+
nameCount: db.prepare("SELECT COUNT(*) AS n FROM declarations WHERE simple = ? AND name != '<module>'"),
|
|
161
|
+
census: db.prepare('SELECT status, reason FROM files WHERE path = ?'),
|
|
162
|
+
lines: db.prepare('SELECT lines FROM file_index WHERE file = ?'),
|
|
163
|
+
fileDecls: db.prepare(`SELECT d.id, s.key, d.file, d.simple, d.kind, d.exported, d.start_line, d.end_line FROM declarations d
|
|
164
|
+
JOIN symbols s ON s.id = d.id WHERE d.file = ? AND d.name != '<module>' ORDER BY s.key`),
|
|
165
|
+
outsideUser: db.prepare(`SELECT 1 FROM edges e JOIN symbols t ON t.id = e.target JOIN declarations d ON d.id = e.source
|
|
166
|
+
WHERE t.key = ? AND d.file != ? AND e.kind NOT IN ('IMPORTS', 'TESTS') LIMIT 1`),
|
|
167
|
+
tests: db.prepare(`SELECT DISTINCT d.file FROM edges e JOIN symbols t ON t.id = e.target
|
|
168
|
+
JOIN declarations d ON d.id = e.source WHERE t.key = ? AND e.kind = 'TESTS' ORDER BY d.file`),
|
|
169
|
+
reexports: db.prepare('SELECT file FROM exports WHERE target = ?'),
|
|
170
|
+
};
|
|
171
|
+
const hash = createHash('sha256');
|
|
172
|
+
const fileRows = options.files ??
|
|
173
|
+
db.prepare('SELECT path, hash, package, status FROM files ORDER BY path').all();
|
|
174
|
+
for (const row of fileRows) {
|
|
175
|
+
const path = String(row.path);
|
|
176
|
+
hash.update(`${path}\0${String(row.hash)}\n`);
|
|
177
|
+
this.packageOf.set(path, row.package === null ? null : String(row.package));
|
|
178
|
+
if (row.status === 'indexable') {
|
|
179
|
+
this.indexableList.push(path);
|
|
180
|
+
this.indexableFiles.add(path);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
// The census lists git's tracked and untracked files, or walks the tree: untracked files are in.
|
|
184
|
+
this.freshness = {
|
|
185
|
+
workingTreeHash: hash.digest('hex'),
|
|
186
|
+
includesUntracked: this.meta.listing === 'git' || this.meta.listing === 'walk',
|
|
187
|
+
};
|
|
188
|
+
const packages = packagesOf(this.meta.packages);
|
|
189
|
+
for (const pkg of packages) {
|
|
190
|
+
for (const entry of pkg.entries) {
|
|
191
|
+
this.entryFiles.add(entry);
|
|
192
|
+
if (!pkg.private)
|
|
193
|
+
this.publicEntryFiles.add(entry);
|
|
194
|
+
}
|
|
195
|
+
if (pkg.private)
|
|
196
|
+
continue;
|
|
197
|
+
for (const value of pkg.unresolved) {
|
|
198
|
+
if (value.includes('*')) {
|
|
199
|
+
this.publicEntryGlobs.push(posix.join(pkg.dir, value));
|
|
200
|
+
}
|
|
201
|
+
else if (CODE_ENTRY.test(value)) {
|
|
202
|
+
// Build output the index does not hold: which exports it re-exports is unknown.
|
|
203
|
+
this.openPackages.push(pkg.dir === '.' ? '' : `${pkg.dir}/`);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
this.limits = repositoryLimits(db);
|
|
208
|
+
this.scannedFiles = this.packageOf.size;
|
|
209
|
+
}
|
|
210
|
+
hasIndexablePrefix(prefix) {
|
|
211
|
+
if (prefix === '')
|
|
212
|
+
return this.indexableList.length > 0;
|
|
213
|
+
let lo = 0;
|
|
214
|
+
let hi = this.indexableList.length - 1;
|
|
215
|
+
while (lo <= hi) {
|
|
216
|
+
const mid = (lo + hi) >>> 1;
|
|
217
|
+
const candidate = this.indexableList[mid];
|
|
218
|
+
if (candidate.startsWith(prefix))
|
|
219
|
+
return true;
|
|
220
|
+
if (candidate < prefix)
|
|
221
|
+
lo = mid + 1;
|
|
222
|
+
else
|
|
223
|
+
hi = mid - 1;
|
|
224
|
+
}
|
|
225
|
+
return false;
|
|
226
|
+
}
|
|
227
|
+
ensureGlobalGaps() {
|
|
228
|
+
if (this.globalGapsLoaded)
|
|
229
|
+
return;
|
|
230
|
+
this.globalGapsLoaded = true;
|
|
231
|
+
const codes = this.db
|
|
232
|
+
.prepare('SELECT DISTINCT code FROM gap_sites')
|
|
233
|
+
.all()
|
|
234
|
+
.map((row) => String(row.code))
|
|
235
|
+
.filter((code) => code !== 'interface-dispatch' && code !== 'computed-member-access');
|
|
236
|
+
if (codes.length === 0)
|
|
237
|
+
return;
|
|
238
|
+
const placeholders = codes.map(() => '?').join(',');
|
|
239
|
+
const rows = this.db
|
|
240
|
+
.prepare(`SELECT file, line, code, detail FROM gap_sites
|
|
241
|
+
WHERE code IN (${placeholders}) ORDER BY file, line, code, detail`)
|
|
242
|
+
.all(...codes);
|
|
243
|
+
for (const row of rows) {
|
|
244
|
+
const gap = {
|
|
245
|
+
file: String(row.file),
|
|
246
|
+
line: Number(row.line),
|
|
247
|
+
code: String(row.code),
|
|
248
|
+
detail: String(row.detail),
|
|
249
|
+
};
|
|
250
|
+
this.classifyGlobalGap(gap);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
classifyGlobalGap(gap) {
|
|
254
|
+
const push = (map, key) => {
|
|
255
|
+
const list = map.get(key) ?? [];
|
|
256
|
+
list.push(gap);
|
|
257
|
+
map.set(key, list);
|
|
258
|
+
};
|
|
259
|
+
const prefix = gap.detail === './' ? '' : gap.detail;
|
|
260
|
+
switch (gap.code) {
|
|
261
|
+
case 'parse-error':
|
|
262
|
+
case 'unsupported-syntax':
|
|
263
|
+
this.everywhere.push(gap);
|
|
264
|
+
return;
|
|
265
|
+
case 'dynamic-import':
|
|
266
|
+
if (gap.detail === '*')
|
|
267
|
+
this.noImporter.push(gap);
|
|
268
|
+
else if (this.hasIndexablePrefix(prefix))
|
|
269
|
+
this.byPrefix.push(gap);
|
|
270
|
+
else
|
|
271
|
+
this.unmatchedPrefix.push(gap);
|
|
272
|
+
return;
|
|
273
|
+
case 'cjs-require':
|
|
274
|
+
if (gap.detail === '*')
|
|
275
|
+
this.noImporter.push(gap);
|
|
276
|
+
else if (this.indexableFiles.has(gap.detail))
|
|
277
|
+
push(this.byFile, gap.detail);
|
|
278
|
+
else if (this.hasIndexablePrefix(prefix))
|
|
279
|
+
this.byPrefix.push(gap);
|
|
280
|
+
else
|
|
281
|
+
push(this.byFile, gap.detail); // External package names scope no indexed file.
|
|
282
|
+
return;
|
|
283
|
+
default:
|
|
284
|
+
// An unknown code is still a gap; never let it vanish.
|
|
285
|
+
this.everywhere.push(gap);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
interfaceGaps(detail) {
|
|
289
|
+
let list = this.byInterface.get(detail);
|
|
290
|
+
if (list === undefined) {
|
|
291
|
+
list = this.statements.gapByCodeDetail.all('interface-dispatch', detail).map((row) => ({
|
|
292
|
+
file: String(row.file),
|
|
293
|
+
line: Number(row.line),
|
|
294
|
+
code: String(row.code),
|
|
295
|
+
detail: String(row.detail),
|
|
296
|
+
}));
|
|
297
|
+
this.byInterface.set(detail, list);
|
|
298
|
+
}
|
|
299
|
+
return list;
|
|
300
|
+
}
|
|
301
|
+
hasComputedMemberGaps() {
|
|
302
|
+
if (this.hasComputedGaps === null) {
|
|
303
|
+
const row = this.db
|
|
304
|
+
.prepare("SELECT 1 FROM gap_sites WHERE code = 'computed-member-access' LIMIT 1")
|
|
305
|
+
.get();
|
|
306
|
+
this.hasComputedGaps = row !== undefined;
|
|
307
|
+
}
|
|
308
|
+
return this.hasComputedGaps;
|
|
309
|
+
}
|
|
310
|
+
ownerGaps(detail) {
|
|
311
|
+
let list = this.byOwner.get(detail);
|
|
312
|
+
if (list === undefined) {
|
|
313
|
+
list = this.statements.gapByCodeDetail.all('computed-member-access', detail).map((row) => ({
|
|
314
|
+
file: String(row.file),
|
|
315
|
+
line: Number(row.line),
|
|
316
|
+
code: String(row.code),
|
|
317
|
+
detail: String(row.detail),
|
|
318
|
+
}));
|
|
319
|
+
this.byOwner.set(detail, list);
|
|
320
|
+
}
|
|
321
|
+
return list;
|
|
322
|
+
}
|
|
323
|
+
unresolvedOf(name) {
|
|
324
|
+
let list = this.unresolvedByName.get(name);
|
|
325
|
+
if (list === undefined) {
|
|
326
|
+
list = this.statements.unresolvedByName.all(name).map((row) => ({
|
|
327
|
+
file: String(row.file),
|
|
328
|
+
line: Number(row.line),
|
|
329
|
+
code: String(row.reason),
|
|
330
|
+
detail: String(row.name),
|
|
331
|
+
}));
|
|
332
|
+
this.unresolvedByName.set(name, list);
|
|
333
|
+
}
|
|
334
|
+
return list;
|
|
335
|
+
}
|
|
336
|
+
isStaticImported(file) {
|
|
337
|
+
let result = this.staticImportedCache.get(file);
|
|
338
|
+
if (result === undefined) {
|
|
339
|
+
result = this.statements.staticMarker.get(`static-import:${file}`) !== undefined;
|
|
340
|
+
this.staticImportedCache.set(file, result);
|
|
341
|
+
}
|
|
342
|
+
return result;
|
|
343
|
+
}
|
|
344
|
+
/** golden P2: what is reachable from the entry of a workspace package that is not private. */
|
|
345
|
+
isPublicEntryFile(file) {
|
|
346
|
+
if (this.publicEntryFiles.has(file))
|
|
347
|
+
return true;
|
|
348
|
+
let cached = this.publicEntryFileCache.get(file);
|
|
349
|
+
if (cached === undefined) {
|
|
350
|
+
cached =
|
|
351
|
+
this.indexableFiles.has(file) &&
|
|
352
|
+
this.publicEntryGlobs.some((glob) => matchGlob(glob, file));
|
|
353
|
+
this.publicEntryFileCache.set(file, cached);
|
|
354
|
+
}
|
|
355
|
+
return cached;
|
|
356
|
+
}
|
|
357
|
+
isPublicKey(key) {
|
|
358
|
+
let cached = this.publicKeyCache.get(key);
|
|
359
|
+
if (cached === undefined) {
|
|
360
|
+
cached = this.statements.reexports
|
|
361
|
+
.all(key)
|
|
362
|
+
.some((row) => this.isPublicEntryFile(String(row.file)));
|
|
363
|
+
this.publicKeyCache.set(key, cached);
|
|
364
|
+
}
|
|
365
|
+
return cached;
|
|
366
|
+
}
|
|
367
|
+
isPublic(decl) {
|
|
368
|
+
if (this.isPublicKey(decl.key) || decl.ancestors.some((key) => this.isPublicKey(key)))
|
|
369
|
+
return true;
|
|
370
|
+
return decl.exported && this.openPackages.some((dir) => decl.file.startsWith(dir));
|
|
371
|
+
}
|
|
372
|
+
/**
|
|
373
|
+
* Keys of object-literal holders: a module-level `const` or a property that ADR 0003 indexes
|
|
374
|
+
* members under (kind `variable`/`property` with dotted children). Classes, interfaces, enums
|
|
375
|
+
* and namespaces hold members too, but a use of the whole container does not read a member's
|
|
376
|
+
* value, so they are not containers in the D55 sense.
|
|
377
|
+
*/
|
|
378
|
+
isContainer(parentKey) {
|
|
379
|
+
if (this.checkedContainerKeys.has(parentKey))
|
|
380
|
+
return this.containerKeyToId.has(parentKey);
|
|
381
|
+
const decl = this.get(parentKey);
|
|
382
|
+
const result = decl !== undefined && (decl.kind === 'variable' || decl.kind === 'property');
|
|
383
|
+
this.checkedContainerKeys.add(parentKey);
|
|
384
|
+
if (result && decl !== undefined)
|
|
385
|
+
this.containerKeyToId.set(parentKey, decl.id);
|
|
386
|
+
return result;
|
|
387
|
+
}
|
|
388
|
+
preloadContainersByKeys(keys) {
|
|
389
|
+
const missing = [];
|
|
390
|
+
for (const key of keys) {
|
|
391
|
+
if (this.checkedContainerKeys.has(key))
|
|
392
|
+
continue;
|
|
393
|
+
const existing = this.declByKey.get(key);
|
|
394
|
+
if (existing !== undefined) {
|
|
395
|
+
this.checkedContainerKeys.add(key);
|
|
396
|
+
if (existing !== null && (existing.kind === 'variable' || existing.kind === 'property')) {
|
|
397
|
+
this.containerKeyToId.set(key, existing.id);
|
|
398
|
+
}
|
|
399
|
+
continue;
|
|
400
|
+
}
|
|
401
|
+
this.checkedContainerKeys.add(key);
|
|
402
|
+
missing.push(key);
|
|
403
|
+
}
|
|
404
|
+
if (missing.length === 0)
|
|
405
|
+
return;
|
|
406
|
+
this.runBucketed(missing, this.containerByKeyStmts, (ph) => `SELECT d.id, s.key, d.file, d.simple, d.kind, d.exported, d.start_line, d.end_line
|
|
407
|
+
FROM symbols s JOIN declarations d ON d.id = s.id
|
|
408
|
+
WHERE s.key IN (${ph}) AND d.kind IN ('variable', 'property')`, (row) => {
|
|
409
|
+
const decl = declaration(row);
|
|
410
|
+
this.declByKey.set(decl.key, decl);
|
|
411
|
+
this.declById.set(decl.id, decl);
|
|
412
|
+
this.containerKeyToId.set(decl.key, decl.id);
|
|
413
|
+
});
|
|
414
|
+
}
|
|
415
|
+
get(key) {
|
|
416
|
+
const cached = this.declByKey.get(key);
|
|
417
|
+
if (cached !== undefined)
|
|
418
|
+
return cached ?? undefined;
|
|
419
|
+
const row = this.statements.declaration.get(key);
|
|
420
|
+
if (row === undefined) {
|
|
421
|
+
this.declByKey.set(key, null);
|
|
422
|
+
return undefined;
|
|
423
|
+
}
|
|
424
|
+
const decl = declaration(row);
|
|
425
|
+
this.declByKey.set(decl.key, decl);
|
|
426
|
+
this.declById.set(decl.id, decl);
|
|
427
|
+
return decl;
|
|
428
|
+
}
|
|
429
|
+
runBucketed(items, cache, sqlForCount, onRow) {
|
|
430
|
+
let i = 0;
|
|
431
|
+
while (i < items.length) {
|
|
432
|
+
const remaining = items.length - i;
|
|
433
|
+
const bucket = BATCH_BUCKETS.find((size) => size >= remaining) ?? BATCH_BUCKETS[BATCH_BUCKETS.length - 1];
|
|
434
|
+
const take = Math.min(remaining, bucket);
|
|
435
|
+
const args = items.slice(i, i + take);
|
|
436
|
+
const pad = args[args.length - 1];
|
|
437
|
+
while (args.length < bucket)
|
|
438
|
+
args.push(pad);
|
|
439
|
+
let stmt = cache.get(bucket);
|
|
440
|
+
if (stmt === undefined) {
|
|
441
|
+
stmt = this.db.prepare(sqlForCount(Array(bucket).fill('?').join(',')));
|
|
442
|
+
cache.set(bucket, stmt);
|
|
443
|
+
}
|
|
444
|
+
for (const row of stmt.all(...args))
|
|
445
|
+
onRow(row);
|
|
446
|
+
i += take;
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
preloadDeclarationsByKeys(keys) {
|
|
450
|
+
let missing;
|
|
451
|
+
let seen;
|
|
452
|
+
for (const key of keys) {
|
|
453
|
+
if (!this.declByKey.has(key)) {
|
|
454
|
+
seen ??= new Set();
|
|
455
|
+
if (!seen.has(key)) {
|
|
456
|
+
seen.add(key);
|
|
457
|
+
missing ??= [];
|
|
458
|
+
missing.push(key);
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
if (missing === undefined)
|
|
463
|
+
return;
|
|
464
|
+
this.runBucketed(missing, this.declByKeyStmts, (ph) => `SELECT d.id, s.key, d.file, d.simple, d.kind, d.exported, d.start_line, d.end_line
|
|
465
|
+
FROM symbols s JOIN declarations d ON d.id = s.id WHERE s.key IN (${ph})`, (row) => {
|
|
466
|
+
const decl = declaration(row);
|
|
467
|
+
this.declByKey.set(decl.key, decl);
|
|
468
|
+
this.declById.set(decl.id, decl);
|
|
469
|
+
});
|
|
470
|
+
for (const key of missing) {
|
|
471
|
+
if (!this.declByKey.has(key))
|
|
472
|
+
this.declByKey.set(key, null);
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
preloadDeclarationsByIds(ids) {
|
|
476
|
+
if (ids.length === 0)
|
|
477
|
+
return;
|
|
478
|
+
this.runBucketed(ids, this.declByIdStmts, (ph) => `SELECT d.id, s.key, d.file, d.simple, d.kind, d.exported, d.start_line, d.end_line
|
|
479
|
+
FROM declarations d JOIN symbols s ON s.id = d.id WHERE d.id IN (${ph})`, (row) => {
|
|
480
|
+
const decl = declaration(row);
|
|
481
|
+
this.declById.set(decl.id, decl);
|
|
482
|
+
this.declByKey.set(decl.key, decl);
|
|
483
|
+
});
|
|
484
|
+
}
|
|
485
|
+
ensureImplementsGraph() {
|
|
486
|
+
if (this.implementsById === null) {
|
|
487
|
+
this.implementsById = new Map();
|
|
488
|
+
for (const row of this.db
|
|
489
|
+
.prepare("SELECT source, target FROM edges WHERE kind = 'IMPLEMENTS'")
|
|
490
|
+
.all()) {
|
|
491
|
+
const source = Number(row.source);
|
|
492
|
+
const target = Number(row.target);
|
|
493
|
+
const list = this.implementsById.get(source);
|
|
494
|
+
if (list === undefined)
|
|
495
|
+
this.implementsById.set(source, [target]);
|
|
496
|
+
else
|
|
497
|
+
list.push(target);
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
return this.implementsById;
|
|
501
|
+
}
|
|
502
|
+
implementedTargetIds(id) {
|
|
503
|
+
let cached = this.implementedIdsCache.get(id);
|
|
504
|
+
if (cached !== undefined)
|
|
505
|
+
return cached;
|
|
506
|
+
const implGraph = this.ensureImplementsGraph();
|
|
507
|
+
if (!implGraph.has(id)) {
|
|
508
|
+
cached = [];
|
|
509
|
+
this.implementedIdsCache.set(id, cached);
|
|
510
|
+
return cached;
|
|
511
|
+
}
|
|
512
|
+
const seen = new Set([id]);
|
|
513
|
+
const targets = [];
|
|
514
|
+
for (const next of implGraph.get(id) ?? []) {
|
|
515
|
+
if (!seen.has(next)) {
|
|
516
|
+
seen.add(next);
|
|
517
|
+
targets.push(next);
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
for (let i = 0; i < targets.length; i++) {
|
|
521
|
+
for (const next of implGraph.get(targets[i]) ?? []) {
|
|
522
|
+
if (!seen.has(next)) {
|
|
523
|
+
seen.add(next);
|
|
524
|
+
targets.push(next);
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
this.implementedIdsCache.set(id, targets);
|
|
529
|
+
return targets;
|
|
530
|
+
}
|
|
531
|
+
preloadFrontier(frontier, heuristic, exactOrder) {
|
|
532
|
+
this.preloadDeclarationsByKeys(frontier);
|
|
533
|
+
const ancestorKeys = [];
|
|
534
|
+
for (const key of frontier) {
|
|
535
|
+
const decl = this.declByKey.get(key);
|
|
536
|
+
if (decl !== null && decl !== undefined && decl.ancestors.length > 0) {
|
|
537
|
+
for (const parent of decl.ancestors) {
|
|
538
|
+
if (!this.checkedContainerKeys.has(parent))
|
|
539
|
+
ancestorKeys.push(parent);
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
if (ancestorKeys.length > 0)
|
|
544
|
+
this.preloadContainersByKeys(ancestorKeys);
|
|
545
|
+
const targetIds = [];
|
|
546
|
+
const targetIdSet = new Set();
|
|
547
|
+
const addTargetId = (id) => {
|
|
548
|
+
if (!this.incomingFetchedIds.has(id) && !targetIdSet.has(id)) {
|
|
549
|
+
targetIdSet.add(id);
|
|
550
|
+
targetIds.push(id);
|
|
551
|
+
}
|
|
552
|
+
};
|
|
553
|
+
const implTargetsToLoad = [];
|
|
554
|
+
const implLoadSet = new Set();
|
|
555
|
+
for (const key of frontier) {
|
|
556
|
+
const decl = this.declByKey.get(key);
|
|
557
|
+
if (decl === null || decl === undefined)
|
|
558
|
+
continue;
|
|
559
|
+
addTargetId(decl.id);
|
|
560
|
+
if (heuristic) {
|
|
561
|
+
for (const implId of this.implementedTargetIds(decl.id)) {
|
|
562
|
+
if (!this.declById.has(implId) && !implLoadSet.has(implId)) {
|
|
563
|
+
implLoadSet.add(implId);
|
|
564
|
+
implTargetsToLoad.push(implId);
|
|
565
|
+
}
|
|
566
|
+
addTargetId(implId);
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
for (const parent of decl.ancestors) {
|
|
570
|
+
const containerId = this.containerKeyToId.get(parent);
|
|
571
|
+
if (containerId !== undefined)
|
|
572
|
+
addTargetId(containerId);
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
if (implTargetsToLoad.length > 0)
|
|
576
|
+
this.preloadDeclarationsByIds(implTargetsToLoad);
|
|
577
|
+
if (targetIds.length === 0)
|
|
578
|
+
return;
|
|
579
|
+
const rawByTarget = new Map();
|
|
580
|
+
for (const id of targetIds) {
|
|
581
|
+
this.incomingFetchedIds.add(id);
|
|
582
|
+
rawByTarget.set(id, []);
|
|
583
|
+
}
|
|
584
|
+
const sourceIds = [];
|
|
585
|
+
const sourceIdSet = new Set();
|
|
586
|
+
this.runBucketed(targetIds, this.incomingByIdStmts, (ph) => `SELECT target, source, kind, strength FROM edges
|
|
587
|
+
WHERE target IN (${ph}) AND kind NOT IN ('IMPORTS', 'TESTS')`, (row) => {
|
|
588
|
+
const targetId = Number(row.target);
|
|
589
|
+
const sourceId = Number(row.source);
|
|
590
|
+
const kind = String(row.kind);
|
|
591
|
+
const strength = String(row.strength);
|
|
592
|
+
if (!Object.hasOwn(EDGE_KINDS, kind) || !Object.hasOwn(STRENGTHS, strength)) {
|
|
593
|
+
const targetKey = this.declById.get(targetId)?.key ?? String(targetId);
|
|
594
|
+
throw new Error(`index edge into ${targetKey} has kind ${kind}, strength ${strength}`);
|
|
595
|
+
}
|
|
596
|
+
if (!this.declById.has(sourceId) && !sourceIdSet.has(sourceId)) {
|
|
597
|
+
sourceIdSet.add(sourceId);
|
|
598
|
+
sourceIds.push(sourceId);
|
|
599
|
+
}
|
|
600
|
+
rawByTarget.get(targetId)?.push({
|
|
601
|
+
source: '',
|
|
602
|
+
sourceId,
|
|
603
|
+
kind: kind,
|
|
604
|
+
strength: strength,
|
|
605
|
+
});
|
|
606
|
+
});
|
|
607
|
+
if (sourceIds.length > 0)
|
|
608
|
+
this.preloadDeclarationsByIds(sourceIds);
|
|
609
|
+
for (const [targetId, rawEdges] of rawByTarget) {
|
|
610
|
+
const targetDecl = this.declById.get(targetId);
|
|
611
|
+
if (targetDecl === undefined)
|
|
612
|
+
continue;
|
|
613
|
+
for (const raw of rawEdges) {
|
|
614
|
+
raw.source = this.declById.get(raw.sourceId).key;
|
|
615
|
+
}
|
|
616
|
+
if (rawEdges.length > 1) {
|
|
617
|
+
if (exactOrder) {
|
|
618
|
+
rawEdges.sort((a, b) => a.source < b.source
|
|
619
|
+
? -1
|
|
620
|
+
: a.source > b.source
|
|
621
|
+
? 1
|
|
622
|
+
: a.kind < b.kind
|
|
623
|
+
? -1
|
|
624
|
+
: a.kind > b.kind
|
|
625
|
+
? 1
|
|
626
|
+
: 0);
|
|
627
|
+
}
|
|
628
|
+
else {
|
|
629
|
+
rawEdges.sort((a, b) => a.sourceId - b.sourceId || (a.kind < b.kind ? -1 : a.kind > b.kind ? 1 : 0));
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
this.incomingCache.set(targetDecl.key, rawEdges);
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
incoming(key) {
|
|
636
|
+
let edges = this.incomingCache.get(key);
|
|
637
|
+
if (edges === undefined) {
|
|
638
|
+
edges = [];
|
|
639
|
+
for (const row of this.statements.incoming.all(key)) {
|
|
640
|
+
const kind = String(row.kind);
|
|
641
|
+
const strength = String(row.strength);
|
|
642
|
+
if (!Object.hasOwn(EDGE_KINDS, kind) || !Object.hasOwn(STRENGTHS, strength)) {
|
|
643
|
+
throw new Error(`index edge into ${key} has kind ${kind}, strength ${strength}`);
|
|
644
|
+
}
|
|
645
|
+
edges.push({
|
|
646
|
+
source: String(row.source),
|
|
647
|
+
kind: kind,
|
|
648
|
+
strength: strength,
|
|
649
|
+
});
|
|
650
|
+
}
|
|
651
|
+
this.incomingCache.set(key, edges);
|
|
652
|
+
}
|
|
653
|
+
return edges;
|
|
654
|
+
}
|
|
655
|
+
/**
|
|
656
|
+
* Gaps that hide users of `decl` itself: `computed-member-access`, `interface-dispatch` and
|
|
657
|
+
* `dynamic-import`/`cjs-require` in scope. Not name matches and not repository-wide gaps (R2, D56).
|
|
658
|
+
*/
|
|
659
|
+
userHidingGaps(decl) {
|
|
660
|
+
this.ensureGlobalGaps();
|
|
661
|
+
const found = [];
|
|
662
|
+
if (decl.exported || decl.kind === 'module') {
|
|
663
|
+
found.push(...this.unmatchedPrefix);
|
|
664
|
+
for (const gap of this.byPrefix) {
|
|
665
|
+
const prefix = gap.detail === './' ? '' : gap.detail;
|
|
666
|
+
if (decl.file.startsWith(prefix))
|
|
667
|
+
found.push(gap);
|
|
668
|
+
}
|
|
669
|
+
// D55: a prefix-less import can load any module nothing else loads; package entries and
|
|
670
|
+
// test files are loaded directly or by a runner, so they are out of this scope.
|
|
671
|
+
if (this.noImporter.length > 0 &&
|
|
672
|
+
!isTestFile(decl.file) &&
|
|
673
|
+
!this.entryFiles.has(decl.file) &&
|
|
674
|
+
!this.isStaticImported(decl.file)) {
|
|
675
|
+
found.push(...this.noImporter);
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
found.push(...(this.byFile.get(decl.file) ?? []));
|
|
679
|
+
for (const targetKey of this.implementedTargets(decl.key)) {
|
|
680
|
+
found.push(...this.interfaceGaps(targetKey));
|
|
681
|
+
}
|
|
682
|
+
if (this.hasComputedMemberGaps()) {
|
|
683
|
+
// A computed access on an object reads its members and the values stored in them,
|
|
684
|
+
// and a namespace computed access (`ns[k]`) reads every export and re-export of that module.
|
|
685
|
+
const owners = new Set([decl.key, ...ancestors(decl.key)]);
|
|
686
|
+
if (decl.exported || ancestors(decl.key).some((key) => this.get(key)?.exported)) {
|
|
687
|
+
owners.add(`${decl.file}#${MODULE_NAME}`);
|
|
688
|
+
}
|
|
689
|
+
for (const row of this.statements.reexports.all(decl.key)) {
|
|
690
|
+
owners.add(`${String(row.file)}#${MODULE_NAME}`);
|
|
691
|
+
}
|
|
692
|
+
for (const edge of this.incoming(decl.key)) {
|
|
693
|
+
if (edge.kind !== 'REFERENCES')
|
|
694
|
+
continue;
|
|
695
|
+
owners.add(edge.source);
|
|
696
|
+
for (const key of ancestors(edge.source))
|
|
697
|
+
owners.add(key);
|
|
698
|
+
}
|
|
699
|
+
for (const owner of owners)
|
|
700
|
+
found.push(...this.ownerGaps(owner));
|
|
701
|
+
}
|
|
702
|
+
return found;
|
|
703
|
+
}
|
|
704
|
+
/**
|
|
705
|
+
* Gap sites and unresolved records whose scope covers `decl` (golden P4).
|
|
706
|
+
* When `decl.simple` is shared by more than `impact.commonName` declarations and has name-match
|
|
707
|
+
* sites, those sites become the `common-name` factor instead of completeness reasons (D56, G15).
|
|
708
|
+
*/
|
|
709
|
+
gapsOf(decl) {
|
|
710
|
+
this.ensureGlobalGaps();
|
|
711
|
+
const found = [...this.everywhere, ...this.userHidingGaps(decl)];
|
|
712
|
+
const unresolved = this.unresolvedOf(decl.simple);
|
|
713
|
+
const unresolvedImports = unresolved.filter((gap) => gap.code === 'unresolved-import');
|
|
714
|
+
const untypedUses = unresolved.filter((gap) => gap.code === 'unresolved-name-match');
|
|
715
|
+
const stringMatches = [];
|
|
716
|
+
for (const row of this.statements.tokens.all(decl.simple)) {
|
|
717
|
+
const lines = JSON.parse(String(row.lines));
|
|
718
|
+
if (!Array.isArray(lines))
|
|
719
|
+
continue;
|
|
720
|
+
for (const line of lines) {
|
|
721
|
+
stringMatches.push({
|
|
722
|
+
file: String(row.file),
|
|
723
|
+
line: Number(line),
|
|
724
|
+
code: 'string-name-match',
|
|
725
|
+
detail: decl.simple,
|
|
726
|
+
});
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
const nameMatches = unresolvedImports.length + untypedUses.length;
|
|
730
|
+
if (nameMatches + stringMatches.length > 0) {
|
|
731
|
+
const shared = Number(this.statements.nameCount.get(decl.simple)?.n ?? 0);
|
|
732
|
+
if (shared > (this.impact.commonName ?? 10)) {
|
|
733
|
+
return {
|
|
734
|
+
gaps: found,
|
|
735
|
+
commonNameBasis: `common-name: ${shared.toLocaleString('en-US')} declarations named "${decl.simple}"; ${nameMatches.toLocaleString('en-US')} untyped uses, ${stringMatches.length.toLocaleString('en-US')} equal strings not attributed`,
|
|
736
|
+
};
|
|
737
|
+
}
|
|
738
|
+
found.push(...unresolvedImports, ...untypedUses, ...stringMatches);
|
|
739
|
+
}
|
|
740
|
+
return { gaps: found, commonNameBasis: null };
|
|
741
|
+
}
|
|
742
|
+
ensureBoundarySources() {
|
|
743
|
+
if (this.boundarySourcesLoaded)
|
|
744
|
+
return;
|
|
745
|
+
this.boundarySourcesLoaded = true;
|
|
746
|
+
for (const row of this.db
|
|
747
|
+
.prepare("SELECT file, kind FROM markers WHERE kind < 'static-import:' OR kind >= 'static-import;'")
|
|
748
|
+
.all()) {
|
|
749
|
+
const file = String(row.file);
|
|
750
|
+
const kinds = this.markers.get(file) ?? new Set();
|
|
751
|
+
kinds.add(String(row.kind));
|
|
752
|
+
this.markers.set(file, kinds);
|
|
753
|
+
}
|
|
754
|
+
for (const row of this.db
|
|
755
|
+
.prepare(`SELECT DISTINCT file FROM external_imports WHERE ${SQL_SPECIFIER_WHERE}`)
|
|
756
|
+
.all()) {
|
|
757
|
+
this.sqlFiles.add(String(row.file));
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
boundaries(file) {
|
|
761
|
+
let found = this.boundaryCache.get(file);
|
|
762
|
+
if (found === undefined) {
|
|
763
|
+
this.ensureBoundarySources();
|
|
764
|
+
const kinds = new Set(this.markers.get(file) ?? []);
|
|
765
|
+
if (this.sqlFiles.has(file))
|
|
766
|
+
kinds.add('boundary:sql');
|
|
767
|
+
if (/(^|\/)(android|ios)\//.test(file))
|
|
768
|
+
kinds.add('boundary:native');
|
|
769
|
+
found = [...kinds].sort();
|
|
770
|
+
this.boundaryCache.set(file, found);
|
|
771
|
+
}
|
|
772
|
+
return found;
|
|
773
|
+
}
|
|
774
|
+
query(target, depth = 2) {
|
|
775
|
+
if (typeof target !== 'string' ||
|
|
776
|
+
target.trim() === '' ||
|
|
777
|
+
!Number.isInteger(depth) ||
|
|
778
|
+
depth < 1 ||
|
|
779
|
+
depth > 10) {
|
|
780
|
+
return {
|
|
781
|
+
input: String(target),
|
|
782
|
+
kind: 'failure',
|
|
783
|
+
code: 'INVALID_INPUT',
|
|
784
|
+
message: 'a target is a non-empty string; depth is 1–10',
|
|
785
|
+
};
|
|
786
|
+
}
|
|
787
|
+
const asked = target.trim();
|
|
788
|
+
const atLine = /^(.+):(\d+)$/.exec(asked);
|
|
789
|
+
const file = atLine?.[1] ?? (asked.includes('#') ? asked.slice(0, asked.indexOf('#')) : asked);
|
|
790
|
+
const census = this.statements.census.get(file);
|
|
791
|
+
if (census !== undefined && census.status !== 'indexable') {
|
|
792
|
+
return {
|
|
793
|
+
input: asked,
|
|
794
|
+
kind: 'guidance',
|
|
795
|
+
code: 'target-not-found',
|
|
796
|
+
message: `NOT INDEXED ${file} — ${String(census.reason ?? census.status)}`,
|
|
797
|
+
};
|
|
798
|
+
}
|
|
799
|
+
if (census !== undefined && atLine === null && !asked.includes('#'))
|
|
800
|
+
return this.fileAnswer(asked, depth);
|
|
801
|
+
if (atLine !== null) {
|
|
802
|
+
const lines = this.statements.lines.get(file);
|
|
803
|
+
const n = Number(atLine[2]);
|
|
804
|
+
if (lines === undefined || n < 1 || n > Number(lines.lines)) {
|
|
805
|
+
return {
|
|
806
|
+
input: asked,
|
|
807
|
+
kind: 'guidance',
|
|
808
|
+
code: 'target-not-found',
|
|
809
|
+
message: `NOT FOUND ${asked}: no such line in an indexed file`,
|
|
810
|
+
nearest: [],
|
|
811
|
+
};
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
let match = this.graph.symbol(asked);
|
|
815
|
+
let parentTarget;
|
|
816
|
+
// D51: a member of an anonymous object type or an object literal deeper than ADR 0003's
|
|
817
|
+
// 3-level limit answers for its nearest indexed parent (property or variable).
|
|
818
|
+
if (match.kind === 'not-found' &&
|
|
819
|
+
atLine === null &&
|
|
820
|
+
(asked.includes('#') || !asked.includes('/'))) {
|
|
821
|
+
for (let parent = asked; parent.includes('.') && parentTarget === undefined;) {
|
|
822
|
+
parent = parent.slice(0, parent.lastIndexOf('.'));
|
|
823
|
+
const found = this.graph.symbol(parent);
|
|
824
|
+
if (found.kind === 'found') {
|
|
825
|
+
const kind = this.get(found.key)?.kind;
|
|
826
|
+
if (kind === 'property' || kind === 'variable') {
|
|
827
|
+
match = found;
|
|
828
|
+
parentTarget = found.key;
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
else if (found.kind === 'ambiguous') {
|
|
832
|
+
const matching = found.candidates.filter((key) => {
|
|
833
|
+
const kind = this.get(key)?.kind;
|
|
834
|
+
return kind === 'property' || kind === 'variable';
|
|
835
|
+
});
|
|
836
|
+
if (matching.length === 1) {
|
|
837
|
+
match = { kind: 'found', key: matching[0] };
|
|
838
|
+
parentTarget = matching[0];
|
|
839
|
+
}
|
|
840
|
+
else if (matching.length > 1) {
|
|
841
|
+
match = { kind: 'ambiguous', candidates: matching };
|
|
842
|
+
break;
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
}
|
|
846
|
+
}
|
|
847
|
+
if (match.kind === 'ambiguous') {
|
|
848
|
+
return {
|
|
849
|
+
input: asked,
|
|
850
|
+
kind: 'guidance',
|
|
851
|
+
code: 'ambiguous-target',
|
|
852
|
+
message: `AMBIGUOUS ${asked} — ${match.candidates.length} declarations`,
|
|
853
|
+
candidates: [...match.candidates].sort(),
|
|
854
|
+
};
|
|
855
|
+
}
|
|
856
|
+
if (match.kind === 'not-found') {
|
|
857
|
+
return {
|
|
858
|
+
input: asked,
|
|
859
|
+
kind: 'guidance',
|
|
860
|
+
code: 'target-not-found',
|
|
861
|
+
message: `NOT FOUND ${asked}`,
|
|
862
|
+
nearest: match.nearest.slice(0, 5),
|
|
863
|
+
};
|
|
864
|
+
}
|
|
865
|
+
return {
|
|
866
|
+
input: asked,
|
|
867
|
+
kind: 'result',
|
|
868
|
+
value: this.impactOf(match.key, depth, parentTarget !== undefined),
|
|
869
|
+
...(atLine === null ? {} : { resolvedFromLine: Number(atLine[2]) }),
|
|
870
|
+
...(parentTarget === undefined ? {} : { parentTarget }),
|
|
871
|
+
};
|
|
872
|
+
}
|
|
873
|
+
/** Up to five targets; one ambiguous or missing target does not block the others. */
|
|
874
|
+
batch(targets, depth = 2) {
|
|
875
|
+
if (!Array.isArray(targets) || targets.length < 1 || targets.length > 5) {
|
|
876
|
+
return [{ input: '', kind: 'failure', code: 'INVALID_INPUT', message: 'give 1–5 targets' }];
|
|
877
|
+
}
|
|
878
|
+
return targets.map((target) => this.query(target, depth));
|
|
879
|
+
}
|
|
880
|
+
fileAnswer(file, depth) {
|
|
881
|
+
const declarations = this.statements.fileDecls.all(file).map((row) => {
|
|
882
|
+
const decl = declaration(row);
|
|
883
|
+
this.declByKey.set(decl.key, decl);
|
|
884
|
+
this.declById.set(decl.id, decl);
|
|
885
|
+
return decl;
|
|
886
|
+
});
|
|
887
|
+
const moduleDecl = this.get(`${file}#${MODULE_NAME}`);
|
|
888
|
+
const allDecls = moduleDecl !== undefined ? [...declarations, moduleDecl] : declarations;
|
|
889
|
+
const used = declarations.filter((decl) => decl.exported && this.statements.outsideUser.get(decl.key, file) !== undefined);
|
|
890
|
+
const span = {
|
|
891
|
+
file,
|
|
892
|
+
startLine: 1,
|
|
893
|
+
endLine: Math.max(1, ...allDecls.map((decl) => decl.end)),
|
|
894
|
+
};
|
|
895
|
+
return {
|
|
896
|
+
input: file,
|
|
897
|
+
kind: 'result',
|
|
898
|
+
value: {
|
|
899
|
+
file,
|
|
900
|
+
results: used.map((decl) => this.impactOf(decl.key, depth, false)),
|
|
901
|
+
aggregate: this.buildImpact(allDecls, `${file}#${MODULE_NAME}`, span, depth, false),
|
|
902
|
+
remainder: declarations.length - used.length,
|
|
903
|
+
limits: { ...this.limits },
|
|
904
|
+
engine: { ...this.engine },
|
|
905
|
+
scannedFiles: this.scannedFiles,
|
|
906
|
+
},
|
|
907
|
+
};
|
|
908
|
+
}
|
|
909
|
+
/**
|
|
910
|
+
* Breadth first over incoming edges to closure: each symbol once at its shallowest depth,
|
|
911
|
+
* stopped only by the node budget (G14); `depth` limits listing elsewhere. `heuristic` adds
|
|
912
|
+
* the callers of interface members the symbol implements (golden C06).
|
|
913
|
+
*/
|
|
914
|
+
traverse(key, heuristic, preloaded = false, displayDepth = 10) {
|
|
915
|
+
const seeds = typeof key === 'string' ? [key] : key;
|
|
916
|
+
const seen = new Set(seeds);
|
|
917
|
+
const levels = [];
|
|
918
|
+
let frontier = [...seeds];
|
|
919
|
+
let capped = false;
|
|
920
|
+
while (frontier.length > 0) {
|
|
921
|
+
const exactOrder = levels.length < displayDepth || this.nodeBudget <= 20_000;
|
|
922
|
+
if (!preloaded)
|
|
923
|
+
this.preloadFrontier(frontier, heuristic, exactOrder);
|
|
924
|
+
const level = [];
|
|
925
|
+
for (const current of frontier) {
|
|
926
|
+
for (const edge of this.dependentsOf(current, heuristic)) {
|
|
927
|
+
if (!heuristic && (edge.strength === 'heuristic' || edge.strength === 'unknown'))
|
|
928
|
+
continue;
|
|
929
|
+
if (seen.has(edge.source))
|
|
930
|
+
continue;
|
|
931
|
+
if (seen.size >= this.nodeBudget) {
|
|
932
|
+
capped = true;
|
|
933
|
+
continue;
|
|
934
|
+
}
|
|
935
|
+
seen.add(edge.source);
|
|
936
|
+
level.push({ key: edge.source, edge });
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
if (level.length === 0)
|
|
940
|
+
break;
|
|
941
|
+
if (exactOrder) {
|
|
942
|
+
level.sort((a, b) => (a.key < b.key ? -1 : a.key > b.key ? 1 : 0));
|
|
943
|
+
}
|
|
944
|
+
levels.push(level);
|
|
945
|
+
frontier = level.map((item) => item.key);
|
|
946
|
+
}
|
|
947
|
+
return { levels, capped };
|
|
948
|
+
}
|
|
949
|
+
/**
|
|
950
|
+
* Who depends on `key`: its incoming edges, plus the whole-value users of each enclosing
|
|
951
|
+
* object literal (spread, passed, returned, assigned — D55; that is reach, not a gap).
|
|
952
|
+
* `heuristic` adds the callers of interface members the symbol implements.
|
|
953
|
+
*/
|
|
954
|
+
dependentsOf(key, heuristic) {
|
|
955
|
+
const direct = this.incoming(key);
|
|
956
|
+
const decl = this.declByKey.get(key);
|
|
957
|
+
const parentKeys = decl !== null && decl !== undefined ? decl.ancestors : ancestors(key);
|
|
958
|
+
const implTargets = heuristic ? this.implementedTargets(key) : EMPTY_ANCESTORS;
|
|
959
|
+
if (parentKeys.length === 0 && implTargets.length === 0)
|
|
960
|
+
return direct;
|
|
961
|
+
const edges = [...direct];
|
|
962
|
+
for (const parent of parentKeys) {
|
|
963
|
+
if (!this.isContainer(parent))
|
|
964
|
+
continue;
|
|
965
|
+
edges.push(...this.incoming(parent));
|
|
966
|
+
}
|
|
967
|
+
for (const targetKey of implTargets) {
|
|
968
|
+
for (const edge of this.incoming(targetKey)) {
|
|
969
|
+
if (edge.kind === 'CALLS')
|
|
970
|
+
edges.push({ ...edge, strength: 'heuristic' });
|
|
971
|
+
}
|
|
972
|
+
}
|
|
973
|
+
return edges;
|
|
974
|
+
}
|
|
975
|
+
/** Transitive `IMPLEMENTS` targets (interfaces / base members), guarded against cycles. */
|
|
976
|
+
implementedTargets(key) {
|
|
977
|
+
const cached = this.implementsCache.get(key);
|
|
978
|
+
if (cached !== undefined)
|
|
979
|
+
return cached;
|
|
980
|
+
const decl = this.get(key);
|
|
981
|
+
if (decl !== undefined) {
|
|
982
|
+
const targetIds = this.implementedTargetIds(decl.id);
|
|
983
|
+
if (targetIds.length === 0) {
|
|
984
|
+
const empty = [];
|
|
985
|
+
this.implementsCache.set(key, empty);
|
|
986
|
+
return empty;
|
|
987
|
+
}
|
|
988
|
+
const missingIds = targetIds.filter((id) => !this.declById.has(id));
|
|
989
|
+
if (missingIds.length > 0)
|
|
990
|
+
this.preloadDeclarationsByIds(missingIds);
|
|
991
|
+
const targets = targetIds
|
|
992
|
+
.map((id) => this.declById.get(id)?.key)
|
|
993
|
+
.filter((k) => k !== undefined);
|
|
994
|
+
this.implementsCache.set(key, targets);
|
|
995
|
+
return targets;
|
|
996
|
+
}
|
|
997
|
+
const seen = new Set([key]);
|
|
998
|
+
const targets = [];
|
|
999
|
+
const enqueue = (from) => {
|
|
1000
|
+
for (const row of this.statements.implements.all(from)) {
|
|
1001
|
+
const next = String(row.key);
|
|
1002
|
+
if (!seen.has(next)) {
|
|
1003
|
+
seen.add(next);
|
|
1004
|
+
targets.push(next);
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
1007
|
+
};
|
|
1008
|
+
enqueue(key);
|
|
1009
|
+
for (let i = 0; i < targets.length; i++) {
|
|
1010
|
+
enqueue(targets[i]);
|
|
1011
|
+
}
|
|
1012
|
+
this.implementsCache.set(key, targets);
|
|
1013
|
+
return targets;
|
|
1014
|
+
}
|
|
1015
|
+
impactOf(key, depth, anonymous) {
|
|
1016
|
+
const target = this.get(key);
|
|
1017
|
+
if (target === undefined)
|
|
1018
|
+
throw new Error(`indexed declaration missing: ${key}`);
|
|
1019
|
+
return this.buildImpact([target], key, { file: target.file, startLine: target.start, endLine: target.end }, depth, anonymous);
|
|
1020
|
+
}
|
|
1021
|
+
unionForChanges(keys, depth, changedFilePaths = []) {
|
|
1022
|
+
const perSymbol = new Map();
|
|
1023
|
+
const compilerBySymbol = new Map();
|
|
1024
|
+
const allInvolvedFiles = new Set(changedFilePaths);
|
|
1025
|
+
const tests = new Set();
|
|
1026
|
+
const reasons = new Set();
|
|
1027
|
+
const factors = new Set();
|
|
1028
|
+
const basis = new Set();
|
|
1029
|
+
for (const key of keys) {
|
|
1030
|
+
if (perSymbol.has(key))
|
|
1031
|
+
continue;
|
|
1032
|
+
const decl = this.get(key);
|
|
1033
|
+
if (decl === undefined)
|
|
1034
|
+
continue;
|
|
1035
|
+
const { result, involvedFiles, compilerLevels } = this.buildImpactWithMeta([decl], key, { file: decl.file, startLine: decl.start, endLine: decl.end }, depth, false);
|
|
1036
|
+
perSymbol.set(key, result);
|
|
1037
|
+
compilerBySymbol.set(key, {
|
|
1038
|
+
levels: compilerLevels,
|
|
1039
|
+
targetPkg: this.packageOf.get(decl.file) ?? null,
|
|
1040
|
+
});
|
|
1041
|
+
for (const file of involvedFiles)
|
|
1042
|
+
allInvolvedFiles.add(file);
|
|
1043
|
+
for (const test of result.tests)
|
|
1044
|
+
tests.add(test);
|
|
1045
|
+
for (const reason of result.completeness.reasons)
|
|
1046
|
+
reasons.add(reason);
|
|
1047
|
+
for (const factor of result.factors)
|
|
1048
|
+
factors.add(factor);
|
|
1049
|
+
for (const b of result.verdict.basis) {
|
|
1050
|
+
if (!b.startsWith('zone:'))
|
|
1051
|
+
basis.add(b);
|
|
1052
|
+
}
|
|
1053
|
+
}
|
|
1054
|
+
for (const file of new Set(changedFilePaths)) {
|
|
1055
|
+
for (const row of this.statements.tests.all(`${file}#${MODULE_NAME}`)) {
|
|
1056
|
+
tests.add(String(row.file));
|
|
1057
|
+
}
|
|
1058
|
+
if (this.impact.routes.length > 0 && matchAny(this.impact.routes, file)) {
|
|
1059
|
+
factors.add('route');
|
|
1060
|
+
basis.add('route');
|
|
1061
|
+
}
|
|
1062
|
+
for (const kind of this.boundaries(file)) {
|
|
1063
|
+
factors.add(kind);
|
|
1064
|
+
basis.add(kind);
|
|
1065
|
+
}
|
|
1066
|
+
}
|
|
1067
|
+
// Union byDepth across all changed symbols, keeping each symbol at its shallowest depth.
|
|
1068
|
+
const byDepth = [];
|
|
1069
|
+
const seenListing = new Set();
|
|
1070
|
+
for (let d = 0; d < depth; d++) {
|
|
1071
|
+
const levelMap = new Map();
|
|
1072
|
+
for (const result of perSymbol.values()) {
|
|
1073
|
+
for (const node of result.byDepth[d] ?? []) {
|
|
1074
|
+
if (seenListing.has(node.symbol))
|
|
1075
|
+
continue;
|
|
1076
|
+
levelMap.set(`${node.symbol}\0${node.via}`, node);
|
|
1077
|
+
}
|
|
1078
|
+
}
|
|
1079
|
+
if (levelMap.size === 0)
|
|
1080
|
+
continue;
|
|
1081
|
+
for (const node of levelMap.values())
|
|
1082
|
+
seenListing.add(node.symbol);
|
|
1083
|
+
byDepth.push([...levelMap.values()].sort((a, b) => {
|
|
1084
|
+
if (a.span.file !== b.span.file)
|
|
1085
|
+
return a.span.file < b.span.file ? -1 : 1;
|
|
1086
|
+
if (a.span.startLine !== b.span.startLine)
|
|
1087
|
+
return a.span.startLine - b.span.startLine;
|
|
1088
|
+
if (a.symbol !== b.symbol)
|
|
1089
|
+
return a.symbol < b.symbol ? -1 : 1;
|
|
1090
|
+
return a.via < b.via ? -1 : a.via > b.via ? 1 : 0;
|
|
1091
|
+
}));
|
|
1092
|
+
}
|
|
1093
|
+
// Union compiler-grade reach across all changed symbols at shallowest compiler depth.
|
|
1094
|
+
const seenCompiler = new Set();
|
|
1095
|
+
const directCompiler = new Set();
|
|
1096
|
+
const reachedFiles = new Set();
|
|
1097
|
+
const maxCompilerDepth = Math.max(0, ...[...compilerBySymbol.values()].map((c) => c.levels.length));
|
|
1098
|
+
const targetPackages = new Set([...compilerBySymbol.values()].map((c) => c.targetPkg));
|
|
1099
|
+
let crossPackage = [...perSymbol.values()].some((r) => r.reach.crossPackage);
|
|
1100
|
+
for (let d = 0; d < maxCompilerDepth; d++) {
|
|
1101
|
+
const levelSymbols = [];
|
|
1102
|
+
for (const { levels } of compilerBySymbol.values()) {
|
|
1103
|
+
for (const item of levels[d] ?? []) {
|
|
1104
|
+
if (seenCompiler.has(item.key))
|
|
1105
|
+
continue;
|
|
1106
|
+
levelSymbols.push(item.key);
|
|
1107
|
+
}
|
|
1108
|
+
}
|
|
1109
|
+
for (const sym of levelSymbols) {
|
|
1110
|
+
seenCompiler.add(sym);
|
|
1111
|
+
if (d === 0)
|
|
1112
|
+
directCompiler.add(sym);
|
|
1113
|
+
const file = this.declByKey.get(sym)?.file ?? sym.slice(0, sym.indexOf('#'));
|
|
1114
|
+
reachedFiles.add(file);
|
|
1115
|
+
if (targetPackages.size > 0 && !targetPackages.has(this.packageOf.get(file) ?? null)) {
|
|
1116
|
+
crossPackage = true;
|
|
1117
|
+
}
|
|
1118
|
+
}
|
|
1119
|
+
}
|
|
1120
|
+
const reachedPackages = new Set([...reachedFiles].map((f) => this.packageOf.get(f) ?? null));
|
|
1121
|
+
const reach = {
|
|
1122
|
+
direct: directCompiler.size,
|
|
1123
|
+
total: seenCompiler.size,
|
|
1124
|
+
files: reachedFiles.size,
|
|
1125
|
+
packages: reachedPackages.size,
|
|
1126
|
+
crossPackage,
|
|
1127
|
+
routes: this.impact.routes.length > 0
|
|
1128
|
+
? [...reachedFiles].filter((file) => matchAny(this.impact.routes, file)).length
|
|
1129
|
+
: 0,
|
|
1130
|
+
};
|
|
1131
|
+
if (reach.total >= this.impact.manyCallers) {
|
|
1132
|
+
factors.add('many-callers');
|
|
1133
|
+
basis.add('many-callers');
|
|
1134
|
+
}
|
|
1135
|
+
if (crossPackage) {
|
|
1136
|
+
factors.add('cross-package');
|
|
1137
|
+
basis.add('cross-package');
|
|
1138
|
+
}
|
|
1139
|
+
const sortedReasons = [...reasons].sort();
|
|
1140
|
+
return {
|
|
1141
|
+
perSymbol,
|
|
1142
|
+
union: {
|
|
1143
|
+
byDepth,
|
|
1144
|
+
tests: [...tests].sort(),
|
|
1145
|
+
completeness: {
|
|
1146
|
+
status: sortedReasons.length > 0 ? 'partial' : 'complete',
|
|
1147
|
+
reasons: sortedReasons,
|
|
1148
|
+
},
|
|
1149
|
+
reach,
|
|
1150
|
+
factors: [...factors].sort(),
|
|
1151
|
+
},
|
|
1152
|
+
allInvolvedFiles,
|
|
1153
|
+
basis: [...basis],
|
|
1154
|
+
limits: { ...this.limits },
|
|
1155
|
+
scannedFiles: this.scannedFiles,
|
|
1156
|
+
freshness: { ...this.freshness },
|
|
1157
|
+
engine: { ...this.engine },
|
|
1158
|
+
};
|
|
1159
|
+
}
|
|
1160
|
+
buildImpact(targets, targetKey, span, depth, anonymous) {
|
|
1161
|
+
return this.buildImpactWithMeta(targets, targetKey, span, depth, anonymous).result;
|
|
1162
|
+
}
|
|
1163
|
+
buildImpactWithMeta(targets, targetKey, span, depth, anonymous) {
|
|
1164
|
+
const keys = targets.map((target) => target.key);
|
|
1165
|
+
const listing = this.traverse(keys, true, false, depth);
|
|
1166
|
+
// DESIGN §7.2: reach is over compiler-grade edges only, so a path through a heuristic hop is out.
|
|
1167
|
+
const compiler = this.traverse(keys, false, !listing.capped, depth);
|
|
1168
|
+
const reasons = new Set();
|
|
1169
|
+
const commonNameBases = [];
|
|
1170
|
+
for (const target of targets) {
|
|
1171
|
+
const { gaps: targetGaps, commonNameBasis } = this.gapsOf(target);
|
|
1172
|
+
for (const gap of targetGaps) {
|
|
1173
|
+
reasons.add(`${gap.code}: ${gap.file}:${gap.line} — ${gap.detail}`);
|
|
1174
|
+
}
|
|
1175
|
+
if (commonNameBasis !== null)
|
|
1176
|
+
commonNameBases.push(commonNameBasis);
|
|
1177
|
+
}
|
|
1178
|
+
if (anonymous)
|
|
1179
|
+
reasons.add(ANONYMOUS_REASON);
|
|
1180
|
+
if (listing.capped || compiler.capped)
|
|
1181
|
+
reasons.add('depth-limit: node budget reached with callers unvisited');
|
|
1182
|
+
const byDepth = [];
|
|
1183
|
+
const listed = [];
|
|
1184
|
+
for (const level of listing.levels) {
|
|
1185
|
+
const includeNodes = byDepth.length < depth;
|
|
1186
|
+
const nodes = [];
|
|
1187
|
+
for (const { key: source, edge } of level) {
|
|
1188
|
+
const decl = this.get(source);
|
|
1189
|
+
if (decl === undefined)
|
|
1190
|
+
throw new Error(`indexed declaration missing: ${source}`);
|
|
1191
|
+
listed.push(decl);
|
|
1192
|
+
if (includeNodes) {
|
|
1193
|
+
const isRoute = matchAny(this.impact.routes, decl.file);
|
|
1194
|
+
const nodeBoundaries = this.boundaries(decl.file);
|
|
1195
|
+
nodes.push({
|
|
1196
|
+
symbol: source,
|
|
1197
|
+
via: edge.kind,
|
|
1198
|
+
strength: edge.strength,
|
|
1199
|
+
span: { file: decl.file, startLine: decl.start, endLine: decl.end },
|
|
1200
|
+
...(this.userHidingGaps(decl).length > 0 ? { gapScoped: true } : {}),
|
|
1201
|
+
...(isRoute ? { route: true } : {}),
|
|
1202
|
+
...(nodeBoundaries.length > 0 ? { boundaries: nodeBoundaries } : {}),
|
|
1203
|
+
});
|
|
1204
|
+
}
|
|
1205
|
+
}
|
|
1206
|
+
if (includeNodes)
|
|
1207
|
+
byDepth.push(nodes);
|
|
1208
|
+
}
|
|
1209
|
+
let reachedTotal = 0;
|
|
1210
|
+
const reachedFiles = new Set();
|
|
1211
|
+
for (const level of compiler.levels) {
|
|
1212
|
+
reachedTotal += level.length;
|
|
1213
|
+
for (const item of level) {
|
|
1214
|
+
const file = this.declByKey.get(item.key)?.file ?? item.key.slice(0, item.key.indexOf('#'));
|
|
1215
|
+
reachedFiles.add(file);
|
|
1216
|
+
}
|
|
1217
|
+
}
|
|
1218
|
+
const targetPackage = this.packageOf.get(span.file) ?? null;
|
|
1219
|
+
const packages = new Set([...reachedFiles].map((file) => this.packageOf.get(file) ?? null));
|
|
1220
|
+
const reach = {
|
|
1221
|
+
direct: compiler.levels[0]?.length ?? 0,
|
|
1222
|
+
total: reachedTotal,
|
|
1223
|
+
files: reachedFiles.size,
|
|
1224
|
+
packages: packages.size,
|
|
1225
|
+
crossPackage: [...packages].some((pkg) => pkg !== targetPackage),
|
|
1226
|
+
// Count distinct route files until M4a provides route records (R7).
|
|
1227
|
+
routes: this.impact.routes.length > 0
|
|
1228
|
+
? [...reachedFiles].filter((file) => matchAny(this.impact.routes, file)).length
|
|
1229
|
+
: 0,
|
|
1230
|
+
};
|
|
1231
|
+
// golden P2 over the target(s) and every listed symbol: factors may only raise the level.
|
|
1232
|
+
const involved = [...targets, ...listed];
|
|
1233
|
+
const involvedFiles = new Set(involved.map((decl) => decl.file));
|
|
1234
|
+
const hasRoutes = this.impact.routes.length > 0;
|
|
1235
|
+
const factors = [];
|
|
1236
|
+
if (involved.some((decl) => this.isPublic(decl)))
|
|
1237
|
+
factors.push('public-export-surface');
|
|
1238
|
+
if (hasRoutes && [...involvedFiles].some((file) => matchAny(this.impact.routes, file)))
|
|
1239
|
+
factors.push('route');
|
|
1240
|
+
if ([...involvedFiles].some((file) => (this.packageOf.get(file) ?? null) !== targetPackage))
|
|
1241
|
+
factors.push('cross-package');
|
|
1242
|
+
if (reach.total >= this.impact.manyCallers)
|
|
1243
|
+
factors.push('many-callers');
|
|
1244
|
+
if (commonNameBases.length > 0)
|
|
1245
|
+
factors.push('common-name');
|
|
1246
|
+
const boundaries = new Set();
|
|
1247
|
+
for (const file of involvedFiles) {
|
|
1248
|
+
for (const kind of this.boundaries(file))
|
|
1249
|
+
boundaries.add(kind);
|
|
1250
|
+
}
|
|
1251
|
+
for (const kind of ['boundary:sql', 'boundary:native'])
|
|
1252
|
+
if (boundaries.has(kind))
|
|
1253
|
+
factors.push(kind);
|
|
1254
|
+
const tests = new Set(listed.filter((decl) => isTestFile(decl.file)).map((decl) => decl.file));
|
|
1255
|
+
for (const file of new Set(targets.map((target) => target.file))) {
|
|
1256
|
+
for (const row of this.statements.tests.all(`${file}#${MODULE_NAME}`)) {
|
|
1257
|
+
tests.add(String(row.file));
|
|
1258
|
+
}
|
|
1259
|
+
}
|
|
1260
|
+
const sortedReasons = [...reasons].sort();
|
|
1261
|
+
const incomplete = sortedReasons.length > 0;
|
|
1262
|
+
const factorBasis = factors.flatMap((factor) => factor === 'common-name' && commonNameBases.length > 0
|
|
1263
|
+
? [...new Set(commonNameBases)]
|
|
1264
|
+
: [factor]);
|
|
1265
|
+
const zonePaths = new Set([span.file, ...involvedFiles]);
|
|
1266
|
+
const matchedZones = matchedRiskZones(zonePaths, this.riskZones);
|
|
1267
|
+
return {
|
|
1268
|
+
result: {
|
|
1269
|
+
target: targetKey,
|
|
1270
|
+
span,
|
|
1271
|
+
byDepth,
|
|
1272
|
+
bridged: [],
|
|
1273
|
+
tests: [...tests].sort(),
|
|
1274
|
+
completeness: { status: incomplete ? 'partial' : 'complete', reasons: sortedReasons },
|
|
1275
|
+
reach,
|
|
1276
|
+
factors,
|
|
1277
|
+
limits: { ...this.limits },
|
|
1278
|
+
scannedFiles: this.scannedFiles,
|
|
1279
|
+
verdict: deriveVerdict({
|
|
1280
|
+
incomplete,
|
|
1281
|
+
hasFactors: factors.length > 0,
|
|
1282
|
+
basis: [...sortedReasons, ...factorBasis],
|
|
1283
|
+
matchedZones,
|
|
1284
|
+
}),
|
|
1285
|
+
freshness: { ...this.freshness },
|
|
1286
|
+
engine: { ...this.engine },
|
|
1287
|
+
},
|
|
1288
|
+
involvedFiles: zonePaths,
|
|
1289
|
+
compilerLevels: compiler.levels,
|
|
1290
|
+
};
|
|
1291
|
+
}
|
|
1292
|
+
}
|