@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,590 @@
|
|
|
1
|
+
import { deriveVerdict } from '../kernel/verdict.js';
|
|
2
|
+
/** TOOLS.md §4 per-target character budget. */
|
|
3
|
+
export const TARGET_BUDGET = 4_000;
|
|
4
|
+
/** TOOLS.md §4 whole-batch character budget (up to 5 targets). */
|
|
5
|
+
export const BATCH_BUDGET = 12_000;
|
|
6
|
+
const DEFAULT_INDEX = {
|
|
7
|
+
state: 'fresh',
|
|
8
|
+
updatedFiles: 0,
|
|
9
|
+
driftedFiles: [],
|
|
10
|
+
};
|
|
11
|
+
function plural(count, singular, pluralForm) {
|
|
12
|
+
return `${count.toLocaleString('en-US')} ${count === 1 ? singular : pluralForm}`;
|
|
13
|
+
}
|
|
14
|
+
function shortName(keyOrInput) {
|
|
15
|
+
const hash = keyOrInput.indexOf('#');
|
|
16
|
+
return hash === -1 ? keyOrInput : keyOrInput.slice(hash + 1);
|
|
17
|
+
}
|
|
18
|
+
function fileOfKey(key) {
|
|
19
|
+
const hash = key.indexOf('#');
|
|
20
|
+
return hash === -1 ? key : key.slice(0, hash);
|
|
21
|
+
}
|
|
22
|
+
function sortNodes(nodes) {
|
|
23
|
+
return [...nodes].sort((a, b) => {
|
|
24
|
+
if (a.span.file !== b.span.file)
|
|
25
|
+
return a.span.file < b.span.file ? -1 : 1;
|
|
26
|
+
if (a.span.startLine !== b.span.startLine)
|
|
27
|
+
return a.span.startLine - b.span.startLine;
|
|
28
|
+
if (a.symbol !== b.symbol)
|
|
29
|
+
return a.symbol < b.symbol ? -1 : 1;
|
|
30
|
+
return a.via < b.via ? -1 : a.via > b.via ? 1 : 0;
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
function formatNodeRow(node) {
|
|
34
|
+
const loc = `${node.span.file}:${node.span.startLine}`;
|
|
35
|
+
const sym = shortName(node.symbol);
|
|
36
|
+
const markers = [];
|
|
37
|
+
if (node.route)
|
|
38
|
+
markers.push('route');
|
|
39
|
+
if (node.strength === 'heuristic')
|
|
40
|
+
markers.push('heuristic');
|
|
41
|
+
if (node.boundaries)
|
|
42
|
+
markers.push(...node.boundaries);
|
|
43
|
+
if (node.gapScoped)
|
|
44
|
+
markers.push('gap');
|
|
45
|
+
const suffix = markers.length > 0 ? ` ${markers.join(' ')}` : '';
|
|
46
|
+
return ` ${loc} ${sym} ${node.via}${suffix}`;
|
|
47
|
+
}
|
|
48
|
+
function formatGapReason(reason, targetFile, targetLine) {
|
|
49
|
+
const withLoc = /^([a-z-]+): ([^:;\s]+:\d+) — (.+)$/.exec(reason);
|
|
50
|
+
if (withLoc !== null) {
|
|
51
|
+
const [, code = '', loc = '', detail = ''] = withLoc;
|
|
52
|
+
switch (code) {
|
|
53
|
+
case 'dynamic-import':
|
|
54
|
+
return `dynamic-import ${loc} — computed path (${detail}); check whether it can load this file`;
|
|
55
|
+
case 'cjs-require':
|
|
56
|
+
return `cjs-require ${loc} — CommonJS require (${detail}); check callers at this require site`;
|
|
57
|
+
case 'computed-member-access':
|
|
58
|
+
return `computed-member-access ${loc} — computed key access on ${detail}; check dynamic readers`;
|
|
59
|
+
case 'interface-dispatch':
|
|
60
|
+
return `interface-dispatch ${loc} — call via interface ${detail}; check whether runtime receiver is this implementation`;
|
|
61
|
+
case 'parse-error':
|
|
62
|
+
return `parse-error ${loc} — syntax error in ${detail}; fix syntax so callers in this file can be indexed`;
|
|
63
|
+
case 'unsupported-syntax':
|
|
64
|
+
return `unsupported-syntax ${loc} — ${detail}; check this site manually`;
|
|
65
|
+
case 'unresolved-import':
|
|
66
|
+
return `unresolved-import ${loc} — unresolved import of ${detail}; check whether it targets this declaration`;
|
|
67
|
+
case 'unresolved-name-match':
|
|
68
|
+
return `unresolved-name-match ${loc} — untyped access to ${detail}; check whether receiver can be this target`;
|
|
69
|
+
case 'string-name-match':
|
|
70
|
+
return `string-name-match ${loc} — string literal "${detail}"; check whether it references this declaration dynamically`;
|
|
71
|
+
default:
|
|
72
|
+
return `${code} ${loc} — ${detail}; check this site`;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
const withoutLoc = /^([a-z-]+): (.+)$/.exec(reason);
|
|
76
|
+
const loc = `${targetFile}:${targetLine}`;
|
|
77
|
+
if (withoutLoc !== null) {
|
|
78
|
+
const [, code = '', detail = ''] = withoutLoc;
|
|
79
|
+
if (code === 'stale-index') {
|
|
80
|
+
return `stale-index — drifted: ${detail}; re-run after the index update`;
|
|
81
|
+
}
|
|
82
|
+
return `${code} ${loc} — ${detail}`;
|
|
83
|
+
}
|
|
84
|
+
return `gap ${loc} — ${reason}`;
|
|
85
|
+
}
|
|
86
|
+
function formatFooter(limits, scannedFiles, engine, index, isStale) {
|
|
87
|
+
const stateText = isStale
|
|
88
|
+
? 'index stale'
|
|
89
|
+
: index.state === 'updated'
|
|
90
|
+
? index.updatedFiles > 0
|
|
91
|
+
? `index updated (${plural(index.updatedFiles, 'file', 'files')})`
|
|
92
|
+
: 'index updated'
|
|
93
|
+
: 'index fresh';
|
|
94
|
+
const limitParts = [];
|
|
95
|
+
if (limits.unboundedDynamicImports > 0) {
|
|
96
|
+
limitParts.push(plural(limits.unboundedDynamicImports, 'unbounded dynamic import', 'unbounded dynamic imports'));
|
|
97
|
+
}
|
|
98
|
+
if (limits.excludedFiles > 0) {
|
|
99
|
+
limitParts.push(plural(limits.excludedFiles, 'excluded file', 'excluded files'));
|
|
100
|
+
}
|
|
101
|
+
if (limits.failedFiles > 0) {
|
|
102
|
+
limitParts.push(`${plural(limits.failedFiles, 'file', 'files')} failed to index`);
|
|
103
|
+
}
|
|
104
|
+
const limitsText = limitParts.length > 0 ? ` · limits: ${limitParts.join(', ')}` : '';
|
|
105
|
+
const tsVer = !engine.typescript || engine.typescript === 'unknown' ? 'n/a' : engine.typescript;
|
|
106
|
+
const engVer = !engine.version || engine.version === 'unknown' ? 'n/a' : engine.version;
|
|
107
|
+
return `— ${stateText} · ${plural(scannedFiles, 'file', 'files')}${limitsText} · ts ${tsVer} · engine ${engVer}`;
|
|
108
|
+
}
|
|
109
|
+
function formatNext(result, isStale, hasGapRows) {
|
|
110
|
+
const testCount = result.tests.length;
|
|
111
|
+
let base;
|
|
112
|
+
if (result.verdict.action === 'ask-owner') {
|
|
113
|
+
const hasGaps = isStale || result.completeness.status !== 'complete';
|
|
114
|
+
const subject = hasGaps
|
|
115
|
+
? 'present the listed gap sites and planned change to the owner before editing'
|
|
116
|
+
: 'present the planned change to the owner before editing';
|
|
117
|
+
base =
|
|
118
|
+
result.verdict.level === 'critical'
|
|
119
|
+
? `${subject}, and do not batch with other changes; call ei_changes before you report done.`
|
|
120
|
+
: `${subject}; call ei_changes before you report done.`;
|
|
121
|
+
}
|
|
122
|
+
else if (isStale || result.verdict.action === 'check-gaps') {
|
|
123
|
+
base =
|
|
124
|
+
testCount > 0
|
|
125
|
+
? `check the listed gap sites before relying on the caller list, then run ${testCount === 1 ? 'the test above' : 'the tests above'} and call ei_changes before you report done.`
|
|
126
|
+
: 'check the listed gap sites before relying on the caller list, and call ei_changes before you report done.';
|
|
127
|
+
}
|
|
128
|
+
else if (result.verdict.action === 'run-tests') {
|
|
129
|
+
base =
|
|
130
|
+
testCount === 1
|
|
131
|
+
? 'after your change, run the test above; call ei_changes before you report done.'
|
|
132
|
+
: testCount > 1
|
|
133
|
+
? 'after your change, run the tests above; call ei_changes before you report done.'
|
|
134
|
+
: 'after your change, run the package tests; call ei_changes before you report done.';
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
base =
|
|
138
|
+
testCount > 0
|
|
139
|
+
? `after your change, run ${testCount === 1 ? 'the test above' : 'the tests above'}; call ei_changes before you report done.`
|
|
140
|
+
: 'make your change and call ei_changes before you report done.';
|
|
141
|
+
}
|
|
142
|
+
const gapSuffix = hasGapRows
|
|
143
|
+
? ' Rows marked gap may have users the index cannot see; call ei_impact on them if you change what they receive.'
|
|
144
|
+
: '';
|
|
145
|
+
return `Next: ${base}${gapSuffix}`;
|
|
146
|
+
}
|
|
147
|
+
function depthOverflowLine(minDepth, maxDepth, count, fileCount, inBatch) {
|
|
148
|
+
const depthLabel = minDepth === maxDepth ? `Depth ${minDepth}` : `Depth ${minDepth}–${maxDepth}`;
|
|
149
|
+
const head = `${depthLabel}: ${count.toLocaleString('en-US')} more in ${plural(fileCount, 'file', 'files')}.`;
|
|
150
|
+
if (maxDepth === 1 && !inBatch)
|
|
151
|
+
return head;
|
|
152
|
+
return `${head} Call again with depth ${minDepth} and a single target to list them.`;
|
|
153
|
+
}
|
|
154
|
+
function populatedDepthRange(sortedLevels) {
|
|
155
|
+
let minDepth = 0;
|
|
156
|
+
let maxDepth = 0;
|
|
157
|
+
for (let i = 0; i < sortedLevels.length; i++) {
|
|
158
|
+
if ((sortedLevels[i]?.length ?? 0) === 0)
|
|
159
|
+
continue;
|
|
160
|
+
if (minDepth === 0)
|
|
161
|
+
minDepth = i + 1;
|
|
162
|
+
maxDepth = i + 1;
|
|
163
|
+
}
|
|
164
|
+
return minDepth === 0 ? [1, 1] : [minDepth, maxDepth];
|
|
165
|
+
}
|
|
166
|
+
function distinctFiles(nodes) {
|
|
167
|
+
return new Set(nodes.map((node) => node.span.file)).size;
|
|
168
|
+
}
|
|
169
|
+
function buildCallersSection(sortedLevels, charBudget, inBatch) {
|
|
170
|
+
const nonEmpty = sortedLevels.some((level) => level.length > 0);
|
|
171
|
+
if (!nonEmpty)
|
|
172
|
+
return ['Callers: none'];
|
|
173
|
+
// First try rendering every depth level in full.
|
|
174
|
+
const fullLines = [];
|
|
175
|
+
for (let i = 0; i < sortedLevels.length; i++) {
|
|
176
|
+
const nodes = sortedLevels[i] ?? [];
|
|
177
|
+
if (nodes.length === 0)
|
|
178
|
+
continue;
|
|
179
|
+
const d = i + 1;
|
|
180
|
+
fullLines.push(d === 1
|
|
181
|
+
? `Callers, depth 1 (${nodes.length.toLocaleString('en-US')})`
|
|
182
|
+
: `Depth ${d} (${nodes.length.toLocaleString('en-US')})`);
|
|
183
|
+
for (const node of nodes)
|
|
184
|
+
fullLines.push(formatNodeRow(node));
|
|
185
|
+
}
|
|
186
|
+
if (fullLines.join('\n').length <= charBudget)
|
|
187
|
+
return fullLines;
|
|
188
|
+
// Over budget: reserve summary lines for levels d >= 2 first, keep depth 1 complete as far as possible.
|
|
189
|
+
const deeperSummaries = new Map();
|
|
190
|
+
for (let i = 1; i < sortedLevels.length; i++) {
|
|
191
|
+
const nodes = sortedLevels[i] ?? [];
|
|
192
|
+
if (nodes.length === 0)
|
|
193
|
+
continue;
|
|
194
|
+
deeperSummaries.set(i + 1, depthOverflowLine(i + 1, i + 1, nodes.length, distinctFiles(nodes), inBatch));
|
|
195
|
+
}
|
|
196
|
+
const d1 = sortedLevels[0] ?? [];
|
|
197
|
+
const d1Header = `Callers, depth 1 (${d1.length.toLocaleString('en-US')})`;
|
|
198
|
+
const reservedForDeeper = [...deeperSummaries.values()].reduce((sum, line) => sum + line.length + 1, 0);
|
|
199
|
+
// Check if all of depth 1 fits alongside deeper summaries.
|
|
200
|
+
const d1Rows = [];
|
|
201
|
+
let d1Len = d1Header.length;
|
|
202
|
+
let allD1Fits = true;
|
|
203
|
+
const overflowReserve = 120;
|
|
204
|
+
for (let idx = 0; idx < d1.length; idx++) {
|
|
205
|
+
const node = d1[idx];
|
|
206
|
+
const row = formatNodeRow(node);
|
|
207
|
+
const isLast = idx === d1.length - 1;
|
|
208
|
+
const needed = isLast
|
|
209
|
+
? d1Len + 1 + row.length + reservedForDeeper
|
|
210
|
+
: d1Len + 1 + row.length + reservedForDeeper + overflowReserve;
|
|
211
|
+
if (needed > charBudget) {
|
|
212
|
+
const rest = d1.slice(idx);
|
|
213
|
+
d1Rows.push(` ${depthOverflowLine(1, 1, rest.length, distinctFiles(rest), inBatch)}`);
|
|
214
|
+
allD1Fits = false;
|
|
215
|
+
break;
|
|
216
|
+
}
|
|
217
|
+
d1Rows.push(row);
|
|
218
|
+
d1Len += 1 + row.length;
|
|
219
|
+
}
|
|
220
|
+
const out = [d1Header, ...d1Rows];
|
|
221
|
+
if (!allD1Fits) {
|
|
222
|
+
for (const summary of deeperSummaries.values())
|
|
223
|
+
out.push(summary);
|
|
224
|
+
if (out.join('\n').length <= charBudget)
|
|
225
|
+
return out;
|
|
226
|
+
const allNodes = sortedLevels.flat();
|
|
227
|
+
const [minDepth, maxDepth] = populatedDepthRange(sortedLevels);
|
|
228
|
+
return [
|
|
229
|
+
depthOverflowLine(minDepth, maxDepth, allNodes.length, distinctFiles(allNodes), inBatch),
|
|
230
|
+
];
|
|
231
|
+
}
|
|
232
|
+
// Depth 1 fit completely! Try expanding depth 2, 3, ... in order if budget permits.
|
|
233
|
+
let currentLen = out.join('\n').length;
|
|
234
|
+
for (let i = 1; i < sortedLevels.length; i++) {
|
|
235
|
+
const nodes = sortedLevels[i] ?? [];
|
|
236
|
+
if (nodes.length === 0)
|
|
237
|
+
continue;
|
|
238
|
+
const d = i + 1;
|
|
239
|
+
const summary = deeperSummaries.get(d);
|
|
240
|
+
deeperSummaries.delete(d);
|
|
241
|
+
const remainingSummariesLen = [...deeperSummaries.values()].reduce((sum, line) => sum + line.length + 1, 0);
|
|
242
|
+
const levelHeader = `Depth ${d} (${nodes.length.toLocaleString('en-US')})`;
|
|
243
|
+
const levelRows = nodes.map(formatNodeRow);
|
|
244
|
+
const levelBlockLen = 1 + levelHeader.length + levelRows.reduce((s, r) => s + 1 + r.length, 0);
|
|
245
|
+
if (currentLen + levelBlockLen + remainingSummariesLen <= charBudget) {
|
|
246
|
+
out.push(levelHeader, ...levelRows);
|
|
247
|
+
currentLen += levelBlockLen;
|
|
248
|
+
}
|
|
249
|
+
else {
|
|
250
|
+
out.push(summary);
|
|
251
|
+
for (const restSummary of deeperSummaries.values())
|
|
252
|
+
out.push(restSummary);
|
|
253
|
+
break;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
return out;
|
|
257
|
+
}
|
|
258
|
+
function renderSymbolBlock(line1, result, index, budget, inBatch) {
|
|
259
|
+
const isStale = index.state === 'stale' ||
|
|
260
|
+
result.completeness.reasons.some((reason) => reason.startsWith('stale-index:'));
|
|
261
|
+
const zoneText = result.verdict.zones.length > 0
|
|
262
|
+
? ` (${result.verdict.zones.length === 1 ? 'zone' : 'zones'} ${result.verdict.zones.join(', ')})`
|
|
263
|
+
: '';
|
|
264
|
+
const line2 = isStale
|
|
265
|
+
? `ACTION ${result.verdict.action} · level ${result.verdict.level}${zoneText} · picture partial (stale index)`
|
|
266
|
+
: result.completeness.status !== 'complete'
|
|
267
|
+
? `ACTION ${result.verdict.action} · level ${result.verdict.level}${zoneText} · picture ${result.completeness.status} (${plural(result.completeness.reasons.length, 'gap', 'gaps')})`
|
|
268
|
+
: `ACTION ${result.verdict.action} · level ${result.verdict.level}${zoneText} · picture complete`;
|
|
269
|
+
const { reach } = result;
|
|
270
|
+
const factors = result.factors.length === 0 ? 'none' : result.factors.join(', ');
|
|
271
|
+
const line3 = `REACH ${reach.direct.toLocaleString('en-US')} direct · ${reach.total.toLocaleString('en-US')} total · ${plural(reach.files, 'file', 'files')} · ${plural(reach.packages, 'package', 'packages')} · ${plural(reach.routes, 'route', 'routes')} · factors: ${factors}`;
|
|
272
|
+
const targetFile = result.span?.file ?? fileOfKey(result.target);
|
|
273
|
+
const targetLine = result.span?.startLine ?? 1;
|
|
274
|
+
const sortedLevels = result.byDepth.map(sortNodes);
|
|
275
|
+
const hasGapRows = sortedLevels.some((level) => level.some((node) => node.gapScoped === true));
|
|
276
|
+
const testsLines = result.tests.length === 0
|
|
277
|
+
? ['Tests: none']
|
|
278
|
+
: [
|
|
279
|
+
`Tests (${result.tests.length.toLocaleString('en-US')})`,
|
|
280
|
+
...result.tests.slice(0, 10).map((t) => ` ${t}`),
|
|
281
|
+
...(result.tests.length > 10
|
|
282
|
+
? [` (+${(result.tests.length - 10).toLocaleString('en-US')} more)`]
|
|
283
|
+
: []),
|
|
284
|
+
];
|
|
285
|
+
const reasons = result.completeness.reasons;
|
|
286
|
+
const gapsLines = reasons.length === 0
|
|
287
|
+
? ['Gaps: none']
|
|
288
|
+
: [
|
|
289
|
+
`Gaps (${reasons.length.toLocaleString('en-US')})`,
|
|
290
|
+
...reasons.slice(0, 10).map((r) => ` ${formatGapReason(r, targetFile, targetLine)}`),
|
|
291
|
+
...(reasons.length > 10
|
|
292
|
+
? [` (+${(reasons.length - 10).toLocaleString('en-US')} more)`]
|
|
293
|
+
: []),
|
|
294
|
+
];
|
|
295
|
+
const nextLine = formatNext(result, isStale, hasGapRows);
|
|
296
|
+
const footerLine = formatFooter(result.limits, result.scannedFiles ?? result.reach.files, result.engine, index, isStale);
|
|
297
|
+
const headerBlock = [line1, line2, line3].join('\n');
|
|
298
|
+
let tailLines = [...testsLines, ...gapsLines, nextLine, footerLine];
|
|
299
|
+
let tailBlock = tailLines.join('\n');
|
|
300
|
+
// If even header + tail is tight against budget, compact Tests and Gaps so callers/summary fit.
|
|
301
|
+
if (headerBlock.length + 2 + tailBlock.length + 140 > budget) {
|
|
302
|
+
const compactTests = result.tests.length === 0
|
|
303
|
+
? 'Tests: none'
|
|
304
|
+
: `Tests (${result.tests.length.toLocaleString('en-US')}): ${result.tests[0]}${result.tests.length > 1 ? ` (+${result.tests.length - 1} more)` : ''}`;
|
|
305
|
+
const compactGaps = reasons.length === 0
|
|
306
|
+
? 'Gaps: none'
|
|
307
|
+
: `Gaps (${reasons.length.toLocaleString('en-US')}): ${formatGapReason(reasons[0], targetFile, targetLine)}${reasons.length > 1 ? ` (+${reasons.length - 1} more)` : ''}`;
|
|
308
|
+
tailLines = [compactTests, compactGaps, nextLine, footerLine];
|
|
309
|
+
tailBlock = tailLines.join('\n');
|
|
310
|
+
}
|
|
311
|
+
const callersBudget = Math.max(0, budget - headerBlock.length - tailBlock.length - 3);
|
|
312
|
+
const callersLines = buildCallersSection(sortedLevels, callersBudget, inBatch);
|
|
313
|
+
const assembled = `${headerBlock}\n\n${callersLines.join('\n')}\n${tailBlock}`;
|
|
314
|
+
if (assembled.length <= budget)
|
|
315
|
+
return assembled;
|
|
316
|
+
// Hard ceiling fallback: keep first three lines, one overflow summary line, and footer.
|
|
317
|
+
const totalCallers = sortedLevels.reduce((sum, level) => sum + level.length, 0);
|
|
318
|
+
const allNodes = sortedLevels.flat();
|
|
319
|
+
const [minDepth, maxDepth] = populatedDepthRange(sortedLevels);
|
|
320
|
+
const overflow = depthOverflowLine(minDepth, maxDepth, totalCallers, distinctFiles(allNodes), inBatch);
|
|
321
|
+
const summary = totalCallers > 0 ? overflow : 'Callers: none';
|
|
322
|
+
const minimal = [headerBlock, summary, nextLine, footerLine].join('\n');
|
|
323
|
+
if (minimal.length <= budget)
|
|
324
|
+
return minimal;
|
|
325
|
+
return [headerBlock, overflow].join('\n').slice(0, budget);
|
|
326
|
+
}
|
|
327
|
+
function renderFileBlock(fileResult, index, budget, inBatch) {
|
|
328
|
+
const { file, results, remainder, aggregate } = fileResult;
|
|
329
|
+
if (aggregate !== undefined) {
|
|
330
|
+
const single = results.length === 1 ? results[0] : undefined;
|
|
331
|
+
const header = single !== undefined
|
|
332
|
+
? `IMPACT ${shortName(single.target)} — ${single.span?.file ?? file}:${single.span?.startLine ?? 1} (${file}; ${plural(remainder, 'other declaration', 'other declarations')} with no callers outside file)`
|
|
333
|
+
: results.length === 0
|
|
334
|
+
? `IMPACT ${file}:1 — 0 exported declarations with callers outside file (${plural(remainder, 'other declaration', 'other declarations')} in file)`
|
|
335
|
+
: `IMPACT ${results.map((r) => shortName(r.target)).join(', ')} — ${file}:1 (${plural(remainder, 'other declaration', 'other declarations')} with no callers outside file)`;
|
|
336
|
+
return renderSymbolBlock(header, aggregate, index, budget, inBatch);
|
|
337
|
+
}
|
|
338
|
+
if (results.length === 1) {
|
|
339
|
+
const single = results[0];
|
|
340
|
+
const locFile = single.span?.file ?? file;
|
|
341
|
+
const locLine = single.span?.startLine ?? 1;
|
|
342
|
+
const line1 = `IMPACT ${shortName(single.target)} — ${locFile}:${locLine} (${file}; ${plural(remainder, 'other declaration', 'other declarations')} with no callers outside file)`;
|
|
343
|
+
return renderSymbolBlock(line1, single, index, budget, inBatch);
|
|
344
|
+
}
|
|
345
|
+
const staleReason = index.state === 'stale'
|
|
346
|
+
? `stale-index: ${index.driftedFiles.slice(0, 10).join(', ') || 'working tree drifted'}`
|
|
347
|
+
: null;
|
|
348
|
+
if (results.length === 0) {
|
|
349
|
+
const emptyResult = {
|
|
350
|
+
target: `${file}#<module>`,
|
|
351
|
+
span: { file, startLine: 1, endLine: 1 },
|
|
352
|
+
byDepth: [],
|
|
353
|
+
bridged: [],
|
|
354
|
+
tests: [],
|
|
355
|
+
completeness: staleReason !== null
|
|
356
|
+
? { status: 'partial', reasons: [staleReason] }
|
|
357
|
+
: { status: 'complete', reasons: [] },
|
|
358
|
+
reach: { direct: 0, total: 0, files: 0, packages: 0, crossPackage: false, routes: 0 },
|
|
359
|
+
factors: [],
|
|
360
|
+
limits: fileResult.limits ?? {
|
|
361
|
+
unboundedDynamicImports: 0,
|
|
362
|
+
excludedFiles: 0,
|
|
363
|
+
failedFiles: 0,
|
|
364
|
+
},
|
|
365
|
+
scannedFiles: fileResult.scannedFiles ?? 0,
|
|
366
|
+
verdict: staleReason !== null
|
|
367
|
+
? {
|
|
368
|
+
level: 'unknown',
|
|
369
|
+
action: 'check-gaps',
|
|
370
|
+
zones: [],
|
|
371
|
+
requiredActions: [],
|
|
372
|
+
basis: ['stale-index'],
|
|
373
|
+
}
|
|
374
|
+
: { level: 'low', action: 'proceed', zones: [], requiredActions: [], basis: [] },
|
|
375
|
+
freshness: { workingTreeHash: '', includesUntracked: true },
|
|
376
|
+
engine: fileResult.engine ?? { version: '0.2.0', typescript: 'n/a', extractor: '' },
|
|
377
|
+
};
|
|
378
|
+
const line1 = `IMPACT ${file}:1 — 0 exported declarations with callers outside file (${plural(remainder, 'other declaration', 'other declarations')} in file)`;
|
|
379
|
+
return renderSymbolBlock(line1, emptyResult, index, budget, inBatch);
|
|
380
|
+
}
|
|
381
|
+
// Fallback for synthetic FileImpactResult without aggregate: deduplicate callers at shallowest depth.
|
|
382
|
+
const first = results[0];
|
|
383
|
+
const maxDepth = Math.max(...results.map((r) => r.byDepth.length));
|
|
384
|
+
const seenSymbols = new Set();
|
|
385
|
+
const byDepth = [];
|
|
386
|
+
for (let d = 0; d < maxDepth; d++) {
|
|
387
|
+
const levelMap = new Map();
|
|
388
|
+
for (const r of results) {
|
|
389
|
+
for (const node of r.byDepth[d] ?? []) {
|
|
390
|
+
if (seenSymbols.has(node.symbol))
|
|
391
|
+
continue;
|
|
392
|
+
levelMap.set(`${node.symbol}\0${node.via}`, node);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
for (const node of levelMap.values())
|
|
396
|
+
seenSymbols.add(node.symbol);
|
|
397
|
+
byDepth.push([...levelMap.values()]);
|
|
398
|
+
}
|
|
399
|
+
const uniqueNodes = byDepth.flat();
|
|
400
|
+
const distinctNodeFiles = new Set(uniqueNodes.map((n) => n.span.file));
|
|
401
|
+
const distinctRouteFiles = new Set(uniqueNodes.filter((n) => n.route).map((n) => n.span.file));
|
|
402
|
+
const reasons = [...new Set(results.flatMap((r) => r.completeness.reasons))].sort();
|
|
403
|
+
const factors = [...new Set(results.flatMap((r) => r.factors))];
|
|
404
|
+
const tests = [...new Set(results.flatMap((r) => r.tests))].sort();
|
|
405
|
+
const incomplete = reasons.length > 0 || results.some((r) => r.completeness.status !== 'complete');
|
|
406
|
+
const matchedZoneMap = new Map();
|
|
407
|
+
for (const r of results) {
|
|
408
|
+
const floor = r.verdict.level === 'critical' ? 'critical' : 'high';
|
|
409
|
+
for (const zone of r.verdict.zones) {
|
|
410
|
+
const existing = matchedZoneMap.get(zone);
|
|
411
|
+
if (existing === undefined || (existing.floor === 'high' && floor === 'critical')) {
|
|
412
|
+
matchedZoneMap.set(zone, { zone, floor, match: [] });
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
const combined = {
|
|
417
|
+
target: `${file}#<module>`,
|
|
418
|
+
span: { file, startLine: 1, endLine: 1 },
|
|
419
|
+
byDepth,
|
|
420
|
+
bridged: [],
|
|
421
|
+
tests,
|
|
422
|
+
completeness: { status: incomplete ? 'partial' : 'complete', reasons },
|
|
423
|
+
reach: uniqueNodes.length > 0
|
|
424
|
+
? {
|
|
425
|
+
direct: new Set((byDepth[0] ?? []).map((n) => n.symbol)).size,
|
|
426
|
+
total: seenSymbols.size,
|
|
427
|
+
files: distinctNodeFiles.size,
|
|
428
|
+
packages: Math.max(...results.map((r) => r.reach.packages)),
|
|
429
|
+
crossPackage: results.some((r) => r.reach.crossPackage),
|
|
430
|
+
routes: distinctRouteFiles.size,
|
|
431
|
+
}
|
|
432
|
+
: {
|
|
433
|
+
direct: Math.max(...results.map((r) => r.reach.direct)),
|
|
434
|
+
total: Math.max(...results.map((r) => r.reach.total)),
|
|
435
|
+
files: Math.max(...results.map((r) => r.reach.files)),
|
|
436
|
+
packages: Math.max(...results.map((r) => r.reach.packages)),
|
|
437
|
+
crossPackage: results.some((r) => r.reach.crossPackage),
|
|
438
|
+
routes: Math.max(...results.map((r) => r.reach.routes)),
|
|
439
|
+
},
|
|
440
|
+
factors,
|
|
441
|
+
limits: first.limits,
|
|
442
|
+
scannedFiles: first.scannedFiles,
|
|
443
|
+
verdict: deriveVerdict({
|
|
444
|
+
incomplete,
|
|
445
|
+
hasFactors: factors.length > 0,
|
|
446
|
+
basis: [...new Set(results.flatMap((r) => r.verdict.basis))],
|
|
447
|
+
matchedZones: [...matchedZoneMap.values()],
|
|
448
|
+
}),
|
|
449
|
+
freshness: first.freshness,
|
|
450
|
+
engine: first.engine,
|
|
451
|
+
};
|
|
452
|
+
const names = results.map((r) => shortName(r.target)).join(', ');
|
|
453
|
+
const line1 = `IMPACT ${names} — ${file}:1 (${plural(remainder, 'other declaration', 'other declarations')} with no callers outside file)`;
|
|
454
|
+
return renderSymbolBlock(line1, combined, index, budget, inBatch);
|
|
455
|
+
}
|
|
456
|
+
/** Render one `ImpactAnswer` within `budget` characters (default 4,000). */
|
|
457
|
+
export function renderAnswer(answer, index = DEFAULT_INDEX, options = {}) {
|
|
458
|
+
const budget = options.budget ?? TARGET_BUDGET;
|
|
459
|
+
const inBatch = options.inBatch ?? false;
|
|
460
|
+
if (answer.kind === 'failure') {
|
|
461
|
+
if (answer.message === 'Cancelled' || answer.message === 'index-failed: Cancelled') {
|
|
462
|
+
return 'Cancelled';
|
|
463
|
+
}
|
|
464
|
+
if (answer.code === 'index-failed') {
|
|
465
|
+
const prefix = answer.message.startsWith('index-failed:')
|
|
466
|
+
? answer.message
|
|
467
|
+
: `index-failed: ${answer.message}`;
|
|
468
|
+
const clean = prefix.endsWith('.') ? prefix.slice(0, -1) : prefix;
|
|
469
|
+
return `${clean}. Retry once; if it fails again, continue without ei_impact and say so in your report.`.slice(0, budget);
|
|
470
|
+
}
|
|
471
|
+
return `INVALID_INPUT: ${answer.message}`.slice(0, budget);
|
|
472
|
+
}
|
|
473
|
+
if (answer.kind === 'guidance') {
|
|
474
|
+
if (answer.code === 'ambiguous-target') {
|
|
475
|
+
const candidates = [...(answer.candidates ?? [])].sort();
|
|
476
|
+
const header = `AMBIGUOUS ${answer.input} — ${plural(candidates.length, 'declaration', 'declarations')}. Call again with one of:`;
|
|
477
|
+
const lines = [header];
|
|
478
|
+
let len = header.length;
|
|
479
|
+
for (let i = 0; i < candidates.length; i++) {
|
|
480
|
+
const line = ` ${candidates[i]}`;
|
|
481
|
+
const remaining = candidates.length - i;
|
|
482
|
+
const overflow = ` (+${remaining.toLocaleString('en-US')} more. Call again with path#name to select one.)`;
|
|
483
|
+
if (len + 1 + line.length + (i + 1 < candidates.length ? 1 + overflow.length : 0) >
|
|
484
|
+
budget) {
|
|
485
|
+
lines.push(overflow);
|
|
486
|
+
break;
|
|
487
|
+
}
|
|
488
|
+
lines.push(line);
|
|
489
|
+
len += 1 + line.length;
|
|
490
|
+
}
|
|
491
|
+
return lines.join('\n').slice(0, budget);
|
|
492
|
+
}
|
|
493
|
+
if (answer.code === 'index-building') {
|
|
494
|
+
return 'INDEX BUILDING — index build still running after 60 s. Call again; the answer will use the finished index.'.slice(0, budget);
|
|
495
|
+
}
|
|
496
|
+
// target-not-found (including NOT INDEXED <path> — <reason>)
|
|
497
|
+
if (answer.message.startsWith('NOT INDEXED ')) {
|
|
498
|
+
return answer.message.slice(0, budget);
|
|
499
|
+
}
|
|
500
|
+
const nearest = (answer.nearest ?? []).slice(0, 5);
|
|
501
|
+
const lines = nearest.length > 0
|
|
502
|
+
? [
|
|
503
|
+
`NOT FOUND ${answer.input}. Closest names in the index:`,
|
|
504
|
+
...nearest.map((item) => ` ${item}`),
|
|
505
|
+
'If the symbol is new and unsaved, save the file and call again.',
|
|
506
|
+
]
|
|
507
|
+
: [`${answer.message}.`, 'If the symbol is new and unsaved, save the file and call again.'];
|
|
508
|
+
return lines.join('\n').slice(0, budget);
|
|
509
|
+
}
|
|
510
|
+
if ('file' in answer.value) {
|
|
511
|
+
return renderFileBlock(answer.value, index, budget, inBatch);
|
|
512
|
+
}
|
|
513
|
+
const result = answer.value;
|
|
514
|
+
const file = result.span?.file ?? fileOfKey(result.target);
|
|
515
|
+
const line = result.span?.startLine ?? 1;
|
|
516
|
+
const name = shortName(result.target);
|
|
517
|
+
let line1;
|
|
518
|
+
if (answer.parentTarget !== undefined) {
|
|
519
|
+
const asked = shortName(answer.input);
|
|
520
|
+
line1 = `IMPACT ${asked} (nearest indexed parent ${answer.parentTarget}) — ${file}:${line}`;
|
|
521
|
+
}
|
|
522
|
+
else if (answer.resolvedFromLine !== undefined) {
|
|
523
|
+
line1 = `IMPACT ${name} — ${file}:${line} (nearest indexed declaration enclosing line ${answer.resolvedFromLine})`;
|
|
524
|
+
}
|
|
525
|
+
else {
|
|
526
|
+
line1 = `IMPACT ${name} — ${file}:${line}`;
|
|
527
|
+
}
|
|
528
|
+
return renderSymbolBlock(line1, result, index, budget, inBatch);
|
|
529
|
+
}
|
|
530
|
+
/**
|
|
531
|
+
* Render an `ImpactBatch` (1–5 targets) within per-target (4,000) and batch (12,000) budgets.
|
|
532
|
+
* When a batch exceeds 12,000 characters, per-target budgets are scaled down while preserving
|
|
533
|
+
* at least the first three lines (`IMPACT`, `ACTION`, `REACH`) of every target.
|
|
534
|
+
*/
|
|
535
|
+
export function renderImpact(batch, options = {}) {
|
|
536
|
+
const targetBudget = options.targetBudget ?? TARGET_BUDGET;
|
|
537
|
+
const batchBudget = options.batchBudget ?? BATCH_BUDGET;
|
|
538
|
+
const { answers, index } = batch;
|
|
539
|
+
if (answers.length === 0)
|
|
540
|
+
return '';
|
|
541
|
+
const inBatch = answers.length > 1;
|
|
542
|
+
const blocks = answers.map((answer) => renderAnswer(answer, index, { budget: targetBudget, inBatch }));
|
|
543
|
+
const separatorLen = (answers.length - 1) * 2;
|
|
544
|
+
const totalLen = blocks.reduce((sum, block) => sum + block.length, 0) + separatorLen;
|
|
545
|
+
if (totalLen <= batchBudget)
|
|
546
|
+
return blocks.join('\n\n');
|
|
547
|
+
// Scale per-target budgets so the joined blocks fit within batchBudget while keeping first 3 lines.
|
|
548
|
+
const available = Math.max(0, batchBudget - separatorLen);
|
|
549
|
+
const equalShare = Math.floor(available / answers.length);
|
|
550
|
+
let remainingBudget = available;
|
|
551
|
+
let largeCount = 0;
|
|
552
|
+
for (const block of blocks) {
|
|
553
|
+
if (block.length <= equalShare)
|
|
554
|
+
remainingBudget -= block.length;
|
|
555
|
+
else
|
|
556
|
+
largeCount++;
|
|
557
|
+
}
|
|
558
|
+
const largeShare = largeCount > 0 ? Math.floor(remainingBudget / largeCount) : equalShare;
|
|
559
|
+
const scaled = answers.map((answer, i) => {
|
|
560
|
+
const current = blocks[i];
|
|
561
|
+
if (current.length <= equalShare)
|
|
562
|
+
return current;
|
|
563
|
+
return renderAnswer(answer, index, { budget: Math.min(targetBudget, largeShare), inBatch });
|
|
564
|
+
});
|
|
565
|
+
return scaled.join('\n\n').slice(0, batchBudget);
|
|
566
|
+
}
|
|
567
|
+
/** Typed `details` payload for `ei_impact` and `ei impact --json`: `ImpactResult[]` and guidance objects. */
|
|
568
|
+
export function impactDetails(batch) {
|
|
569
|
+
const out = [];
|
|
570
|
+
for (const answer of batch.answers) {
|
|
571
|
+
if (answer.kind === 'result') {
|
|
572
|
+
if ('target' in answer.value) {
|
|
573
|
+
out.push(answer.value);
|
|
574
|
+
}
|
|
575
|
+
else if (answer.value.aggregate !== undefined) {
|
|
576
|
+
out.push(answer.value.aggregate);
|
|
577
|
+
}
|
|
578
|
+
else if (answer.value.results.length > 0) {
|
|
579
|
+
out.push(...answer.value.results);
|
|
580
|
+
}
|
|
581
|
+
else {
|
|
582
|
+
out.push(answer.value);
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
else {
|
|
586
|
+
out.push(answer);
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
return out;
|
|
590
|
+
}
|