@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,591 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises';
|
|
2
|
+
import { join, posix } from 'node:path';
|
|
3
|
+
import { runChangesDiff } from '../engine/build.js';
|
|
4
|
+
import { engineVersion } from '../engine/typescript.js';
|
|
5
|
+
import { ImpactQuery } from '../impact/query.js';
|
|
6
|
+
import { ImpactSession } from '../impact/session.js';
|
|
7
|
+
import { MODULE_NAME } from '../indexing/declarations.js';
|
|
8
|
+
import { matchAny } from '../kernel/glob.js';
|
|
9
|
+
import { isRecord } from '../kernel/guards.js';
|
|
10
|
+
import { applyRiskZones, deriveVerdict, matchedRiskZones } from '../kernel/verdict.js';
|
|
11
|
+
import { exclusionOf, isCodeKind, kindOf, neverReadOf } from '../workspace/scan.js';
|
|
12
|
+
import { readFiles, readMeta } from '../../storage/sqlite.js';
|
|
13
|
+
import { collectWorkingTreeChanges, readBaseBlob, resolveDiffBase, validateBaseRef, } from './git.js';
|
|
14
|
+
import { evaluatePlan } from './plan.js';
|
|
15
|
+
function unindexedReasonDescription(reason) {
|
|
16
|
+
switch (reason) {
|
|
17
|
+
case 'secret':
|
|
18
|
+
return 'secret file; content never read';
|
|
19
|
+
case 'excluded':
|
|
20
|
+
return 'excluded by configuration; content never read';
|
|
21
|
+
case 'outside-index-scope':
|
|
22
|
+
return 'outside impact.include scope';
|
|
23
|
+
case 'no-tsconfig':
|
|
24
|
+
return 'orphan file (no owning tsconfig)';
|
|
25
|
+
case 'not-in-program':
|
|
26
|
+
return 'file excluded by owning tsconfig';
|
|
27
|
+
case 'other-kind':
|
|
28
|
+
return 'non-TS/JS file; not analysed';
|
|
29
|
+
case 'json':
|
|
30
|
+
return 'JSON file; not analysed';
|
|
31
|
+
case 'binary':
|
|
32
|
+
return 'binary file; not analysed';
|
|
33
|
+
case 'symlink':
|
|
34
|
+
return 'symbolic link; not followed';
|
|
35
|
+
case 'submodule':
|
|
36
|
+
return 'git submodule; not analysed';
|
|
37
|
+
case 'stale-index':
|
|
38
|
+
return 'added while index update failed; not in stale index';
|
|
39
|
+
case 'unreadable':
|
|
40
|
+
return 'file could not be read';
|
|
41
|
+
default:
|
|
42
|
+
return reason;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
function classifyUnindexedKind(path, impact) {
|
|
46
|
+
const kind = kindOf(path);
|
|
47
|
+
const excl = exclusionOf(path, impact);
|
|
48
|
+
if (excl !== null)
|
|
49
|
+
return excl;
|
|
50
|
+
if (kind === 'json')
|
|
51
|
+
return 'json';
|
|
52
|
+
if (!isCodeKind(kind))
|
|
53
|
+
return 'other-kind';
|
|
54
|
+
if (!matchAny(impact.include, path))
|
|
55
|
+
return 'outside-index-scope';
|
|
56
|
+
return 'no-tsconfig';
|
|
57
|
+
}
|
|
58
|
+
function normalizeSpecifierTarget(fromFile, specifier) {
|
|
59
|
+
if (!specifier.startsWith('.'))
|
|
60
|
+
return [];
|
|
61
|
+
const joined = posix.normalize(posix.join(posix.dirname(fromFile), specifier));
|
|
62
|
+
const stripped = joined.replace(/\.(?:[cm]?[jt]sx?)$/i, '');
|
|
63
|
+
return [
|
|
64
|
+
joined,
|
|
65
|
+
`${stripped}.ts`,
|
|
66
|
+
`${stripped}.tsx`,
|
|
67
|
+
`${stripped}.mts`,
|
|
68
|
+
`${stripped}.cts`,
|
|
69
|
+
`${stripped}.js`,
|
|
70
|
+
`${stripped}.jsx`,
|
|
71
|
+
`${stripped}.mjs`,
|
|
72
|
+
`${stripped}.cjs`,
|
|
73
|
+
`${stripped}/index.ts`,
|
|
74
|
+
`${stripped}/index.tsx`,
|
|
75
|
+
`${stripped}/index.js`,
|
|
76
|
+
`${stripped}/index.jsx`,
|
|
77
|
+
];
|
|
78
|
+
}
|
|
79
|
+
function findRemovedLiveUsers(db, removedSymbols, deletedFiles) {
|
|
80
|
+
const usersBySymbol = new Map();
|
|
81
|
+
const gapReasons = new Set();
|
|
82
|
+
if (removedSymbols.length === 0)
|
|
83
|
+
return { usersBySymbol, gapReasons: [] };
|
|
84
|
+
const unresolvedByNameStmt = db.prepare(`SELECT file, line, reason, source FROM unresolved WHERE name = ? AND reason IN ('unresolved-import', 'unresolved-name-match') ORDER BY file, line`);
|
|
85
|
+
const tokensByNameStmt = db.prepare(`SELECT file, lines FROM name_tokens WHERE name = ? AND kind = 'str' ORDER BY file`);
|
|
86
|
+
const exportsByTargetStmt = db.prepare(`SELECT file, name FROM exports WHERE target = ? ORDER BY file, name`);
|
|
87
|
+
const allUnresolvedImports = deletedFiles.size > 0
|
|
88
|
+
? db
|
|
89
|
+
.prepare(`SELECT file, line, name, source FROM unresolved WHERE reason = 'unresolved-import' ORDER BY file, line`)
|
|
90
|
+
.all()
|
|
91
|
+
: [];
|
|
92
|
+
for (const sym of removedSymbols) {
|
|
93
|
+
const hashIdx = sym.symbol.indexOf('#');
|
|
94
|
+
const file = hashIdx === -1 ? sym.symbol : sym.symbol.slice(0, hashIdx);
|
|
95
|
+
const qualified = hashIdx === -1 ? '' : sym.symbol.slice(hashIdx + 1);
|
|
96
|
+
const simple = qualified
|
|
97
|
+
.split('.')
|
|
98
|
+
.pop()
|
|
99
|
+
?.replace(/^static:/, '')
|
|
100
|
+
.replace(/~\d+$/, '') ?? '';
|
|
101
|
+
const seen = new Set();
|
|
102
|
+
const users = [];
|
|
103
|
+
const addUser = (user, reasonDetail) => {
|
|
104
|
+
const k = `${user.file}\0${user.line}\0${user.source}\0${user.reason}`;
|
|
105
|
+
if (seen.has(k))
|
|
106
|
+
return;
|
|
107
|
+
seen.add(k);
|
|
108
|
+
users.push(user);
|
|
109
|
+
if (reasonDetail !== undefined)
|
|
110
|
+
gapReasons.add(reasonDetail);
|
|
111
|
+
};
|
|
112
|
+
if (simple !== '' && simple !== MODULE_NAME && simple !== 'default') {
|
|
113
|
+
const rows = unresolvedByNameStmt.all(simple);
|
|
114
|
+
for (const row of rows) {
|
|
115
|
+
const r = row.reason === 'unresolved-import' ? 'unresolved-import' : 'unresolved-name-match';
|
|
116
|
+
addUser({ file: row.file, line: row.line, source: row.source, reason: r }, `${r}: ${row.file}:${row.line} — ${simple} (removed from ${file})`);
|
|
117
|
+
}
|
|
118
|
+
const tokenRows = tokensByNameStmt.all(simple);
|
|
119
|
+
for (const row of tokenRows) {
|
|
120
|
+
let parsedLines = [];
|
|
121
|
+
try {
|
|
122
|
+
parsedLines = JSON.parse(row.lines);
|
|
123
|
+
}
|
|
124
|
+
catch {
|
|
125
|
+
parsedLines = [];
|
|
126
|
+
}
|
|
127
|
+
const firstLine = Array.isArray(parsedLines) && typeof parsedLines[0] === 'number' ? parsedLines[0] : 1;
|
|
128
|
+
addUser({
|
|
129
|
+
file: row.file,
|
|
130
|
+
line: firstLine,
|
|
131
|
+
source: `${row.file}#${MODULE_NAME}`,
|
|
132
|
+
reason: 'string-name-match',
|
|
133
|
+
}, `string-name-match: ${row.file}:${firstLine} — ${simple}`);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
const exportRows = exportsByTargetStmt.all(sym.symbol);
|
|
137
|
+
for (const row of exportRows) {
|
|
138
|
+
addUser({
|
|
139
|
+
file: row.file,
|
|
140
|
+
line: 1,
|
|
141
|
+
source: `${row.file}#${row.name}`,
|
|
142
|
+
reason: 're-export',
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
if (deletedFiles.has(file)) {
|
|
146
|
+
for (const row of allUnresolvedImports) {
|
|
147
|
+
if (!row.name.startsWith('.'))
|
|
148
|
+
continue;
|
|
149
|
+
const candidates = normalizeSpecifierTarget(row.file, row.name);
|
|
150
|
+
if (candidates.includes(file)) {
|
|
151
|
+
addUser({ file: row.file, line: row.line, source: row.source, reason: 'unresolved-import' }, `unresolved-import: ${row.file}:${row.line} — ${row.name} (deleted/renamed ${file})`);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
if (users.length > 0) {
|
|
156
|
+
users.sort((a, b) => (a.file !== b.file ? (a.file < b.file ? -1 : 1) : a.line - b.line));
|
|
157
|
+
usersBySymbol.set(sym.symbol, users);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return { usersBySymbol, gapReasons: [...gapReasons].sort() };
|
|
161
|
+
}
|
|
162
|
+
function symbolOrderWeight(sym) {
|
|
163
|
+
const hasUsers = (sym.callers ?? 0) > 0 || (sym.liveUsers?.length ?? 0) > 0;
|
|
164
|
+
if ((sym.change === 'removed' || sym.change === 'signature-changed') && hasUsers)
|
|
165
|
+
return 0;
|
|
166
|
+
if (sym.change === 'signature-changed')
|
|
167
|
+
return 1;
|
|
168
|
+
if (sym.change === 'removed')
|
|
169
|
+
return 2;
|
|
170
|
+
if (sym.change === 'modified')
|
|
171
|
+
return 3;
|
|
172
|
+
return 4;
|
|
173
|
+
}
|
|
174
|
+
async function readWorkingText(root, path) {
|
|
175
|
+
try {
|
|
176
|
+
const buf = await readFile(join(root, path));
|
|
177
|
+
if (buf.includes(0))
|
|
178
|
+
return null;
|
|
179
|
+
return buf.toString('utf8');
|
|
180
|
+
}
|
|
181
|
+
catch {
|
|
182
|
+
return null;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
export async function runChanges(sessionOrWorkspace, options = {}) {
|
|
186
|
+
const session = sessionOrWorkspace instanceof ImpactSession
|
|
187
|
+
? sessionOrWorkspace
|
|
188
|
+
: new ImpactSession(sessionOrWorkspace);
|
|
189
|
+
const workspace = session.getWorkspace();
|
|
190
|
+
if (options.signal?.aborted) {
|
|
191
|
+
return { kind: 'failure', code: 'index-failed', message: 'Cancelled', retry: true };
|
|
192
|
+
}
|
|
193
|
+
const depth = options.depth ?? 1;
|
|
194
|
+
if (!Number.isInteger(depth) || depth < 1 || depth > 10) {
|
|
195
|
+
return {
|
|
196
|
+
kind: 'failure',
|
|
197
|
+
code: 'INVALID_INPUT',
|
|
198
|
+
message: 'depth must be an integer 1–10',
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
if (options.base !== undefined) {
|
|
202
|
+
const baseErr = validateBaseRef(options.base);
|
|
203
|
+
if (baseErr !== null) {
|
|
204
|
+
return { kind: 'failure', code: 'INVALID_INPUT', message: baseErr };
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
const baseRes = await resolveDiffBase(workspace.root, options.base);
|
|
208
|
+
if (!baseRes.ok) {
|
|
209
|
+
return { kind: 'failure', code: 'INVALID_INPUT', message: baseRes.message };
|
|
210
|
+
}
|
|
211
|
+
const { baseLabel, baseSha, mergeBase, gitPrefix } = baseRes.value;
|
|
212
|
+
const diffRes = await collectWorkingTreeChanges(workspace.root, baseSha);
|
|
213
|
+
if (!diffRes.ok) {
|
|
214
|
+
return { kind: 'failure', code: 'index-failed', message: diffRes.message, retry: true };
|
|
215
|
+
}
|
|
216
|
+
const rawChanges = diffRes.value;
|
|
217
|
+
// Empty diff fast-path: return immediately before probing or updating the index (M2c §Round 2).
|
|
218
|
+
if (rawChanges.length === 0) {
|
|
219
|
+
return {
|
|
220
|
+
kind: 'result',
|
|
221
|
+
value: {
|
|
222
|
+
base: baseLabel,
|
|
223
|
+
...(mergeBase === undefined ? {} : { mergeBase }),
|
|
224
|
+
changedFiles: [],
|
|
225
|
+
changedSymbols: [],
|
|
226
|
+
unindexedChanges: [],
|
|
227
|
+
unindexedDetails: [],
|
|
228
|
+
impact: {
|
|
229
|
+
byDepth: [],
|
|
230
|
+
tests: [],
|
|
231
|
+
completeness: { status: 'complete', reasons: [] },
|
|
232
|
+
reach: { direct: 0, total: 0, files: 0, packages: 0, crossPackage: false, routes: 0 },
|
|
233
|
+
factors: [],
|
|
234
|
+
},
|
|
235
|
+
verdict: deriveVerdict({
|
|
236
|
+
incomplete: false,
|
|
237
|
+
hasFactors: false,
|
|
238
|
+
basis: [],
|
|
239
|
+
matchedZones: [],
|
|
240
|
+
}),
|
|
241
|
+
freshness: { workingTreeHash: '', includesUntracked: true },
|
|
242
|
+
engine: { version: engineVersion(), typescript: 'n/a', extractor: '' },
|
|
243
|
+
limits: { unboundedDynamicImports: 0, excludedFiles: 0, failedFiles: 0 },
|
|
244
|
+
scannedFiles: 0,
|
|
245
|
+
nothingChanged: true,
|
|
246
|
+
timings: { updateMs: 0, baseParseMs: 0, traverseMs: 0, renderMs: 0 },
|
|
247
|
+
},
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
const updateStart = performance.now();
|
|
251
|
+
const ctx = await session.prepareForChanges({
|
|
252
|
+
signal: options.signal,
|
|
253
|
+
onProgress: options.onProgress,
|
|
254
|
+
});
|
|
255
|
+
const updateMs = Math.round((performance.now() - updateStart) * 100) / 100;
|
|
256
|
+
if (ctx.indexState === 'building') {
|
|
257
|
+
return {
|
|
258
|
+
kind: 'guidance',
|
|
259
|
+
code: 'index-building',
|
|
260
|
+
message: 'index-building: index update is still running; retry shortly',
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
if (ctx.indexState === 'failed' || ctx.db === null) {
|
|
264
|
+
return {
|
|
265
|
+
kind: 'failure',
|
|
266
|
+
code: 'index-failed',
|
|
267
|
+
message: ctx.failureMessage ?? 'index update failed',
|
|
268
|
+
retry: true,
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
const db = ctx.db;
|
|
272
|
+
try {
|
|
273
|
+
const files = readFiles(db);
|
|
274
|
+
const fileMap = new Map(files.map((f) => [f.path, f]));
|
|
275
|
+
const extractionMap = new Map();
|
|
276
|
+
for (const row of db.prepare('SELECT file, extraction FROM file_index').all()) {
|
|
277
|
+
extractionMap.set(row.file, row.extraction);
|
|
278
|
+
}
|
|
279
|
+
const meta = readMeta(db);
|
|
280
|
+
const { impact } = workspace.config;
|
|
281
|
+
const changedFiles = [];
|
|
282
|
+
const unindexedDetails = [];
|
|
283
|
+
const diffInputs = [];
|
|
284
|
+
const deletedOrRenamedOldPaths = new Set();
|
|
285
|
+
const recordUnindexed = (entry, reason) => {
|
|
286
|
+
changedFiles.push({
|
|
287
|
+
path: entry.path,
|
|
288
|
+
status: entry.status,
|
|
289
|
+
indexed: false,
|
|
290
|
+
...(entry.oldPath === undefined ? {} : { oldPath: entry.oldPath }),
|
|
291
|
+
});
|
|
292
|
+
unindexedDetails.push({
|
|
293
|
+
path: entry.path,
|
|
294
|
+
reason: unindexedReasonDescription(reason),
|
|
295
|
+
status: entry.status,
|
|
296
|
+
});
|
|
297
|
+
};
|
|
298
|
+
for (const entry of rawChanges) {
|
|
299
|
+
if (entry.invalidUtf8) {
|
|
300
|
+
recordUnindexed(entry, 'unreadable');
|
|
301
|
+
continue;
|
|
302
|
+
}
|
|
303
|
+
if (entry.isSubmodule) {
|
|
304
|
+
recordUnindexed(entry, 'submodule');
|
|
305
|
+
continue;
|
|
306
|
+
}
|
|
307
|
+
const kind = kindOf(entry.path);
|
|
308
|
+
// Secret & excluded files are classified strictly by path before any disk or git-blob read.
|
|
309
|
+
const neverRead = neverReadOf(entry.path, kind, impact);
|
|
310
|
+
const excl = exclusionOf(entry.path, impact);
|
|
311
|
+
if (neverRead !== null || excl !== null) {
|
|
312
|
+
recordUnindexed(entry, neverRead ?? excl ?? 'excluded');
|
|
313
|
+
if (entry.status === 'R' && entry.oldPath !== undefined) {
|
|
314
|
+
const oldNever = neverReadOf(entry.oldPath, kindOf(entry.oldPath), impact);
|
|
315
|
+
const oldExcl = exclusionOf(entry.oldPath, impact);
|
|
316
|
+
if (oldNever === null && oldExcl === null && isCodeKind(kindOf(entry.oldPath))) {
|
|
317
|
+
const oldWasIndexed = ctx.preUpdateIndexable?.has(entry.oldPath) === true ||
|
|
318
|
+
matchAny(impact.include, entry.oldPath);
|
|
319
|
+
if (oldWasIndexed) {
|
|
320
|
+
const baseText = await readBaseBlob(workspace.root, baseSha, gitPrefix, entry.oldPath);
|
|
321
|
+
if (baseText !== null) {
|
|
322
|
+
deletedOrRenamedOldPaths.add(entry.oldPath);
|
|
323
|
+
diffInputs.push({
|
|
324
|
+
path: entry.oldPath,
|
|
325
|
+
status: 'D',
|
|
326
|
+
baseText,
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
continue;
|
|
333
|
+
}
|
|
334
|
+
if (entry.status === 'D') {
|
|
335
|
+
const wasIndexable = isCodeKind(kind) &&
|
|
336
|
+
(ctx.preUpdateIndexable?.has(entry.path) === true ||
|
|
337
|
+
matchAny(impact.include, entry.path));
|
|
338
|
+
if (!wasIndexable) {
|
|
339
|
+
recordUnindexed(entry, classifyUnindexedKind(entry.path, impact));
|
|
340
|
+
continue;
|
|
341
|
+
}
|
|
342
|
+
const baseText = await readBaseBlob(workspace.root, baseSha, gitPrefix, entry.path);
|
|
343
|
+
if (baseText === null) {
|
|
344
|
+
recordUnindexed(entry, 'binary');
|
|
345
|
+
continue;
|
|
346
|
+
}
|
|
347
|
+
deletedOrRenamedOldPaths.add(entry.path);
|
|
348
|
+
changedFiles.push({ path: entry.path, status: 'D', indexed: true });
|
|
349
|
+
diffInputs.push({ path: entry.path, status: 'D', baseText });
|
|
350
|
+
continue;
|
|
351
|
+
}
|
|
352
|
+
// Working-tree file: A, M, or R
|
|
353
|
+
const census = fileMap.get(entry.path);
|
|
354
|
+
const extraction = extractionMap.get(entry.path);
|
|
355
|
+
let isIndexed = false;
|
|
356
|
+
let unindexedReason = null;
|
|
357
|
+
if (census !== undefined) {
|
|
358
|
+
if (census.status === 'indexable' && extraction !== 'not-in-program') {
|
|
359
|
+
isIndexed = true;
|
|
360
|
+
}
|
|
361
|
+
else if (extraction === 'not-in-program') {
|
|
362
|
+
unindexedReason = 'not-in-program';
|
|
363
|
+
}
|
|
364
|
+
else {
|
|
365
|
+
unindexedReason = census.reason ?? classifyUnindexedKind(entry.path, impact);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
else if (ctx.indexState === 'stale') {
|
|
369
|
+
unindexedReason =
|
|
370
|
+
isCodeKind(kind) && matchAny(impact.include, entry.path)
|
|
371
|
+
? 'stale-index'
|
|
372
|
+
: classifyUnindexedKind(entry.path, impact);
|
|
373
|
+
}
|
|
374
|
+
else {
|
|
375
|
+
unindexedReason = classifyUnindexedKind(entry.path, impact);
|
|
376
|
+
}
|
|
377
|
+
if (!isIndexed) {
|
|
378
|
+
recordUnindexed(entry, unindexedReason ?? 'outside-index-scope');
|
|
379
|
+
if (entry.status === 'R' && entry.oldPath !== undefined) {
|
|
380
|
+
const oldNever = neverReadOf(entry.oldPath, kindOf(entry.oldPath), impact);
|
|
381
|
+
const oldExcl = exclusionOf(entry.oldPath, impact);
|
|
382
|
+
if (oldNever === null && oldExcl === null && isCodeKind(kindOf(entry.oldPath))) {
|
|
383
|
+
const oldWasIndexed = ctx.preUpdateIndexable?.has(entry.oldPath) === true ||
|
|
384
|
+
matchAny(impact.include, entry.oldPath);
|
|
385
|
+
if (oldWasIndexed) {
|
|
386
|
+
const baseText = await readBaseBlob(workspace.root, baseSha, gitPrefix, entry.oldPath);
|
|
387
|
+
if (baseText !== null) {
|
|
388
|
+
deletedOrRenamedOldPaths.add(entry.oldPath);
|
|
389
|
+
diffInputs.push({ path: entry.oldPath, status: 'D', baseText });
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
continue;
|
|
395
|
+
}
|
|
396
|
+
const workText = await readWorkingText(workspace.root, entry.path);
|
|
397
|
+
if (workText === null) {
|
|
398
|
+
recordUnindexed(entry, 'binary');
|
|
399
|
+
continue;
|
|
400
|
+
}
|
|
401
|
+
let baseText;
|
|
402
|
+
if (entry.status === 'M') {
|
|
403
|
+
const blob = await readBaseBlob(workspace.root, baseSha, gitPrefix, entry.path);
|
|
404
|
+
if (blob === null) {
|
|
405
|
+
recordUnindexed(entry, 'binary');
|
|
406
|
+
continue;
|
|
407
|
+
}
|
|
408
|
+
baseText = blob;
|
|
409
|
+
}
|
|
410
|
+
else if (entry.status === 'R') {
|
|
411
|
+
const oldPath = entry.oldPath;
|
|
412
|
+
if (oldPath !== undefined &&
|
|
413
|
+
neverReadOf(oldPath, kindOf(oldPath), impact) === null &&
|
|
414
|
+
exclusionOf(oldPath, impact) === null) {
|
|
415
|
+
const blob = await readBaseBlob(workspace.root, baseSha, gitPrefix, oldPath);
|
|
416
|
+
if (blob !== null) {
|
|
417
|
+
baseText = blob;
|
|
418
|
+
deletedOrRenamedOldPaths.add(oldPath);
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
changedFiles.push({
|
|
423
|
+
path: entry.path,
|
|
424
|
+
status: entry.status,
|
|
425
|
+
indexed: true,
|
|
426
|
+
...(entry.oldPath === undefined ? {} : { oldPath: entry.oldPath }),
|
|
427
|
+
});
|
|
428
|
+
diffInputs.push({
|
|
429
|
+
path: entry.path,
|
|
430
|
+
status: entry.status,
|
|
431
|
+
...(entry.oldPath === undefined ? {} : { oldPath: entry.oldPath }),
|
|
432
|
+
...(baseText === undefined ? {} : { baseText }),
|
|
433
|
+
workText,
|
|
434
|
+
});
|
|
435
|
+
}
|
|
436
|
+
const baseParseStart = performance.now();
|
|
437
|
+
let parsedSymbols = [];
|
|
438
|
+
let parseErrors = [];
|
|
439
|
+
if (diffInputs.length > 0) {
|
|
440
|
+
const diffOutcome = await runChangesDiff(workspace, diffInputs, {
|
|
441
|
+
signal: options.signal,
|
|
442
|
+
onProgress: options.onProgress,
|
|
443
|
+
});
|
|
444
|
+
if (!diffOutcome.ok) {
|
|
445
|
+
return {
|
|
446
|
+
kind: 'failure',
|
|
447
|
+
code: 'index-failed',
|
|
448
|
+
message: diffOutcome.message,
|
|
449
|
+
retry: true,
|
|
450
|
+
};
|
|
451
|
+
}
|
|
452
|
+
const rawSymbols = diffOutcome.value.symbols;
|
|
453
|
+
if (Array.isArray(rawSymbols)) {
|
|
454
|
+
parsedSymbols = rawSymbols.filter((item) => isRecord(item) && typeof item.symbol === 'string' && typeof item.change === 'string');
|
|
455
|
+
}
|
|
456
|
+
const rawParseErrors = diffOutcome.value.parseErrors;
|
|
457
|
+
if (Array.isArray(rawParseErrors)) {
|
|
458
|
+
parseErrors = rawParseErrors.filter((item) => isRecord(item) &&
|
|
459
|
+
typeof item.file === 'string' &&
|
|
460
|
+
typeof item.line === 'number' &&
|
|
461
|
+
typeof item.detail === 'string');
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
const baseParseMs = Math.round((performance.now() - baseParseStart) * 100) / 100;
|
|
465
|
+
const traverseStart = performance.now();
|
|
466
|
+
const removedSymbols = parsedSymbols
|
|
467
|
+
.filter((s) => s.change === 'removed')
|
|
468
|
+
.map((s) => ({ symbol: s.symbol, change: s.change, ...(s.span ? { span: s.span } : {}) }));
|
|
469
|
+
const existingChanged = parsedSymbols.filter((s) => s.change !== 'removed');
|
|
470
|
+
const existingKeys = existingChanged.map((s) => s.symbol);
|
|
471
|
+
const { usersBySymbol: removedLiveUsers, gapReasons: removedGapReasons } = findRemovedLiveUsers(db, removedSymbols, deletedOrRenamedOldPaths);
|
|
472
|
+
const indexedFilePaths = changedFiles.filter((f) => f.indexed).map((f) => f.path);
|
|
473
|
+
const impactQuery = new ImpactQuery(db, impact, {
|
|
474
|
+
files,
|
|
475
|
+
meta,
|
|
476
|
+
riskZones: workspace.config.riskZones,
|
|
477
|
+
});
|
|
478
|
+
const unionResult = impactQuery.unionForChanges(existingKeys, depth, indexedFilePaths);
|
|
479
|
+
const traverseMs = Math.round((performance.now() - traverseStart) * 100) / 100;
|
|
480
|
+
const combinedSymbols = [];
|
|
481
|
+
for (const s of existingChanged) {
|
|
482
|
+
const symResult = unionResult.perSymbol.get(s.symbol);
|
|
483
|
+
const d1 = symResult?.byDepth[0] ?? [];
|
|
484
|
+
const liveUsers = d1.map((node) => ({
|
|
485
|
+
file: node.span.file,
|
|
486
|
+
line: node.span.startLine,
|
|
487
|
+
source: node.symbol,
|
|
488
|
+
reason: 'caller',
|
|
489
|
+
}));
|
|
490
|
+
combinedSymbols.push({
|
|
491
|
+
symbol: s.symbol,
|
|
492
|
+
change: s.change,
|
|
493
|
+
...(s.span ? { span: s.span } : symResult?.span ? { span: symResult.span } : {}),
|
|
494
|
+
callers: symResult?.reach.total ?? 0,
|
|
495
|
+
...(d1.length > 0 ? { directCallers: d1 } : {}),
|
|
496
|
+
...(liveUsers.length > 0 ? { liveUsers } : {}),
|
|
497
|
+
});
|
|
498
|
+
}
|
|
499
|
+
for (const s of removedSymbols) {
|
|
500
|
+
const users = removedLiveUsers.get(s.symbol) ?? [];
|
|
501
|
+
combinedSymbols.push({
|
|
502
|
+
...s,
|
|
503
|
+
callers: users.length,
|
|
504
|
+
...(users.length > 0 ? { liveUsers: users } : {}),
|
|
505
|
+
});
|
|
506
|
+
}
|
|
507
|
+
combinedSymbols.sort((a, b) => {
|
|
508
|
+
const wa = symbolOrderWeight(a);
|
|
509
|
+
const wb = symbolOrderWeight(b);
|
|
510
|
+
if (wa !== wb)
|
|
511
|
+
return wa - wb;
|
|
512
|
+
return a.symbol < b.symbol ? -1 : a.symbol > b.symbol ? 1 : 0;
|
|
513
|
+
});
|
|
514
|
+
const unindexedChanges = unindexedDetails.map((d) => d.path).sort();
|
|
515
|
+
const extraReasons = new Set(unionResult.union.completeness.reasons);
|
|
516
|
+
for (const detail of unindexedDetails) {
|
|
517
|
+
extraReasons.add(`outside-index-scope: ${detail.path} — ${detail.reason}`);
|
|
518
|
+
}
|
|
519
|
+
for (const pe of parseErrors) {
|
|
520
|
+
extraReasons.add(`parse-error: ${pe.file}:${pe.line} — ${pe.detail}`);
|
|
521
|
+
}
|
|
522
|
+
for (const r of removedGapReasons) {
|
|
523
|
+
extraReasons.add(r);
|
|
524
|
+
}
|
|
525
|
+
if (ctx.indexState === 'stale' && ctx.staleReason !== null) {
|
|
526
|
+
extraReasons.add(ctx.staleReason);
|
|
527
|
+
}
|
|
528
|
+
const sortedReasons = [...extraReasons].sort();
|
|
529
|
+
const incomplete = sortedReasons.length > 0 || ctx.indexState === 'stale';
|
|
530
|
+
const allZonePaths = new Set([
|
|
531
|
+
...unionResult.allInvolvedFiles,
|
|
532
|
+
...changedFiles.map((f) => f.path),
|
|
533
|
+
...changedFiles.flatMap((f) => (f.oldPath === undefined ? [] : [f.oldPath])),
|
|
534
|
+
...unindexedChanges,
|
|
535
|
+
]);
|
|
536
|
+
const matchedZones = matchedRiskZones(allZonePaths, workspace.config.riskZones);
|
|
537
|
+
const basis = [
|
|
538
|
+
...unionResult.basis,
|
|
539
|
+
...sortedReasons,
|
|
540
|
+
...(ctx.indexState === 'stale' ? ['stale-index'] : []),
|
|
541
|
+
];
|
|
542
|
+
const baseVerdict = deriveVerdict({
|
|
543
|
+
incomplete,
|
|
544
|
+
hasFactors: unionResult.union.factors.length > 0,
|
|
545
|
+
basis,
|
|
546
|
+
matchedZones,
|
|
547
|
+
});
|
|
548
|
+
const verdict = applyRiskZones(baseVerdict, allZonePaths, workspace.config.riskZones, {
|
|
549
|
+
incomplete,
|
|
550
|
+
hasFactors: unionResult.union.factors.length > 0,
|
|
551
|
+
preMatchedZones: matchedZones.map((z) => z.zone),
|
|
552
|
+
});
|
|
553
|
+
const plan = evaluatePlan(options.planned, {
|
|
554
|
+
db,
|
|
555
|
+
changedFiles,
|
|
556
|
+
changedSymbols: combinedSymbols,
|
|
557
|
+
unindexedChanges,
|
|
558
|
+
});
|
|
559
|
+
return {
|
|
560
|
+
kind: 'result',
|
|
561
|
+
value: {
|
|
562
|
+
base: baseLabel,
|
|
563
|
+
...(mergeBase === undefined ? {} : { mergeBase }),
|
|
564
|
+
changedFiles,
|
|
565
|
+
changedSymbols: combinedSymbols,
|
|
566
|
+
unindexedChanges,
|
|
567
|
+
unindexedDetails: unindexedDetails.sort((a, b) => a.path < b.path ? -1 : a.path > b.path ? 1 : 0),
|
|
568
|
+
impact: {
|
|
569
|
+
...unionResult.union,
|
|
570
|
+
completeness: {
|
|
571
|
+
status: incomplete ? 'partial' : 'complete',
|
|
572
|
+
reasons: sortedReasons,
|
|
573
|
+
},
|
|
574
|
+
},
|
|
575
|
+
verdict,
|
|
576
|
+
freshness: unionResult.freshness,
|
|
577
|
+
engine: unionResult.engine,
|
|
578
|
+
limits: unionResult.limits,
|
|
579
|
+
scannedFiles: unionResult.scannedFiles,
|
|
580
|
+
indexState: ctx.indexState === 'stale' ? 'stale' : ctx.indexState === 'updated' ? 'updated' : 'fresh',
|
|
581
|
+
updatedFiles: ctx.updatedFiles,
|
|
582
|
+
nothingChanged: false,
|
|
583
|
+
...(plan.provided ? { plan } : {}),
|
|
584
|
+
timings: { updateMs, baseParseMs, traverseMs, renderMs: 0 },
|
|
585
|
+
},
|
|
586
|
+
};
|
|
587
|
+
}
|
|
588
|
+
finally {
|
|
589
|
+
db.close();
|
|
590
|
+
}
|
|
591
|
+
}
|