@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,356 @@
|
|
|
1
|
+
/** TOOLS.md §5 character budget for `ei_changes`. */
|
|
2
|
+
export const CHANGES_BUDGET = 8_000;
|
|
3
|
+
function plural(count, singular, pluralForm) {
|
|
4
|
+
return `${count.toLocaleString('en-US')} ${count === 1 ? singular : pluralForm}`;
|
|
5
|
+
}
|
|
6
|
+
function shortSymbolName(symbol) {
|
|
7
|
+
const hash = symbol.indexOf('#');
|
|
8
|
+
return hash === -1 ? symbol : symbol.slice(hash + 1);
|
|
9
|
+
}
|
|
10
|
+
function fileOfKey(symbol) {
|
|
11
|
+
const hash = symbol.indexOf('#');
|
|
12
|
+
return hash === -1 ? symbol : symbol.slice(0, hash);
|
|
13
|
+
}
|
|
14
|
+
function formatBase(result) {
|
|
15
|
+
return result.mergeBase !== undefined
|
|
16
|
+
? `${result.base} (merge-base ${result.mergeBase})`
|
|
17
|
+
: result.base;
|
|
18
|
+
}
|
|
19
|
+
function formatFooter(result) {
|
|
20
|
+
const state = result.indexState === 'stale'
|
|
21
|
+
? 'index stale (full hash)'
|
|
22
|
+
: result.indexState === 'updated'
|
|
23
|
+
? (result.updatedFiles ?? 0) > 0
|
|
24
|
+
? `index updated (${plural(result.updatedFiles ?? 0, 'file', 'files')}, full hash)`
|
|
25
|
+
: 'index updated (full hash)'
|
|
26
|
+
: 'index fresh (full hash)';
|
|
27
|
+
const limits = result.limits;
|
|
28
|
+
const limitParts = [];
|
|
29
|
+
if (limits.unboundedDynamicImports > 0) {
|
|
30
|
+
limitParts.push(plural(limits.unboundedDynamicImports, 'unbounded dynamic import', 'unbounded dynamic imports'));
|
|
31
|
+
}
|
|
32
|
+
if (limits.excludedFiles > 0) {
|
|
33
|
+
limitParts.push(plural(limits.excludedFiles, 'excluded file', 'excluded files'));
|
|
34
|
+
}
|
|
35
|
+
if (limits.failedFiles > 0) {
|
|
36
|
+
limitParts.push(`${plural(limits.failedFiles, 'file', 'files')} failed to index`);
|
|
37
|
+
}
|
|
38
|
+
const limitsText = limitParts.length > 0 ? ` · limits: ${limitParts.join(', ')}` : '';
|
|
39
|
+
const tsVer = !result.engine.typescript || result.engine.typescript === 'unknown'
|
|
40
|
+
? 'n/a'
|
|
41
|
+
: result.engine.typescript;
|
|
42
|
+
const engVer = !result.engine.version || result.engine.version === 'unknown' ? 'n/a' : result.engine.version;
|
|
43
|
+
return `— ${state} · ${plural(result.scannedFiles ?? 0, 'file', 'files')}${limitsText} · ts ${tsVer} · engine ${engVer}`;
|
|
44
|
+
}
|
|
45
|
+
function formatPrioritySymbol(sym) {
|
|
46
|
+
const name = shortSymbolName(sym.symbol);
|
|
47
|
+
const file = sym.span?.file ?? fileOfKey(sym.symbol);
|
|
48
|
+
const line = sym.span?.startLine ?? 1;
|
|
49
|
+
const lines = [` ${name} ${file}:${line}`];
|
|
50
|
+
if (sym.change === 'removed') {
|
|
51
|
+
for (const user of sym.liveUsers ?? []) {
|
|
52
|
+
const note = user.reason === 'unresolved-import'
|
|
53
|
+
? 'UNRESOLVED_IMPORT (points at removed symbol/file)'
|
|
54
|
+
: user.reason === 'unresolved-name-match'
|
|
55
|
+
? 'UNRESOLVED_NAME_MATCH (points at nothing)'
|
|
56
|
+
: user.reason === 'string-name-match'
|
|
57
|
+
? 'STRING_MATCH'
|
|
58
|
+
: 'RE-EXPORT (points at removed symbol)';
|
|
59
|
+
lines.push(` ${user.file}:${user.line} ${shortSymbolName(user.source)} ${note}`);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
const callers = sym.directCallers ?? [];
|
|
64
|
+
for (const node of callers) {
|
|
65
|
+
const markers = [];
|
|
66
|
+
if (node.route)
|
|
67
|
+
markers.push('route');
|
|
68
|
+
if (node.strength === 'heuristic')
|
|
69
|
+
markers.push('heuristic');
|
|
70
|
+
if (node.boundaries)
|
|
71
|
+
markers.push(...node.boundaries);
|
|
72
|
+
if (node.gapScoped)
|
|
73
|
+
markers.push('gap');
|
|
74
|
+
const suffix = markers.length > 0 ? ` ${markers.join(' ')}` : '';
|
|
75
|
+
lines.push(` ${node.span.file}:${node.span.startLine} ${shortSymbolName(node.symbol)} ${node.via}${suffix}`);
|
|
76
|
+
}
|
|
77
|
+
const extra = (sym.callers ?? callers.length) - callers.length;
|
|
78
|
+
if (extra > 0) {
|
|
79
|
+
lines.push(` (+${extra.toLocaleString('en-US')} transitive callers)`);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return lines;
|
|
83
|
+
}
|
|
84
|
+
function formatSummarySymbolLine(sym) {
|
|
85
|
+
const name = shortSymbolName(sym.symbol);
|
|
86
|
+
const file = sym.span?.file ?? fileOfKey(sym.symbol);
|
|
87
|
+
const line = sym.span?.startLine ?? 1;
|
|
88
|
+
const callers = sym.callers ?? 0;
|
|
89
|
+
const callerText = sym.change === 'removed'
|
|
90
|
+
? callers === 0
|
|
91
|
+
? '0 users'
|
|
92
|
+
: plural(callers, 'user', 'users')
|
|
93
|
+
: plural(callers, 'caller', 'callers');
|
|
94
|
+
return ` ${name} ${file}:${line} ${callerText}`;
|
|
95
|
+
}
|
|
96
|
+
function formatNext(result) {
|
|
97
|
+
const hasUnindexed = result.unindexedChanges.length > 0;
|
|
98
|
+
const unindexedNote = hasUnindexed ? ' and the "Not analysed" list' : '';
|
|
99
|
+
const testCount = result.impact.tests.length;
|
|
100
|
+
const { action, level } = result.verdict;
|
|
101
|
+
if (action === 'ask-owner') {
|
|
102
|
+
const batchNote = level === 'critical' ? '; do not batch with other changes' : '';
|
|
103
|
+
return `Next: include this verdict${unindexedNote} in your report; ask the owner before proceeding${batchNote}.`;
|
|
104
|
+
}
|
|
105
|
+
if (action === 'check-gaps') {
|
|
106
|
+
const testPart = testCount > 0 ? ` and run ${testCount === 1 ? 'the test above' : 'the tests above'}` : '';
|
|
107
|
+
return `Next: include this verdict${unindexedNote} in your report; inspect the listed gap sites${testPart} before reporting done.`;
|
|
108
|
+
}
|
|
109
|
+
if (action === 'run-tests') {
|
|
110
|
+
const testPart = testCount === 1
|
|
111
|
+
? 'run the test above'
|
|
112
|
+
: testCount > 1
|
|
113
|
+
? 'run the tests above'
|
|
114
|
+
: 'run the package tests';
|
|
115
|
+
return `Next: ${testPart}, and include this verdict${unindexedNote} in your report.`;
|
|
116
|
+
}
|
|
117
|
+
return `Next: include this verdict${unindexedNote} in your report.`;
|
|
118
|
+
}
|
|
119
|
+
function compactSection(title, items, maxItems = 3) {
|
|
120
|
+
if (items.length === 0)
|
|
121
|
+
return [];
|
|
122
|
+
if (items.length <= maxItems)
|
|
123
|
+
return [`${title} (${items.length.toLocaleString('en-US')})`, ...items];
|
|
124
|
+
const shown = items.slice(0, maxItems).map((line) => line.trim());
|
|
125
|
+
return [
|
|
126
|
+
`${title} (${items.length.toLocaleString('en-US')}): ${shown.join('; ')} (+${(items.length - maxItems).toLocaleString('en-US')} more)`,
|
|
127
|
+
];
|
|
128
|
+
}
|
|
129
|
+
export function renderChangesOutcome(outcome, budget = CHANGES_BUDGET, extra) {
|
|
130
|
+
if (outcome.kind === 'failure') {
|
|
131
|
+
if (outcome.message === 'Cancelled')
|
|
132
|
+
return 'Cancelled';
|
|
133
|
+
if (outcome.code === 'INVALID_INPUT') {
|
|
134
|
+
return `INVALID_INPUT: ${outcome.message}`.slice(0, budget);
|
|
135
|
+
}
|
|
136
|
+
const prefix = outcome.message.startsWith('index-failed:')
|
|
137
|
+
? outcome.message
|
|
138
|
+
: `index-failed: ${outcome.message}`;
|
|
139
|
+
return `${prefix}. Retry once; if it fails again, continue without ei_changes and say so in your report.`.slice(0, budget);
|
|
140
|
+
}
|
|
141
|
+
if (outcome.kind === 'guidance') {
|
|
142
|
+
return 'INDEX BUILDING — index build still running after 60 s. Call again; the answer will use the finished index.'.slice(0, budget);
|
|
143
|
+
}
|
|
144
|
+
return renderChanges(outcome.value, budget, extra);
|
|
145
|
+
}
|
|
146
|
+
function formatUserLine(user) {
|
|
147
|
+
const note = user.reason === 'unresolved-import'
|
|
148
|
+
? 'UNRESOLVED_IMPORT (points at removed symbol/file)'
|
|
149
|
+
: user.reason === 'unresolved-name-match'
|
|
150
|
+
? 'UNRESOLVED_NAME_MATCH (points at nothing)'
|
|
151
|
+
: user.reason === 'string-name-match'
|
|
152
|
+
? 'STRING_MATCH'
|
|
153
|
+
: user.reason === 're-export'
|
|
154
|
+
? 'RE-EXPORT (points at removed symbol)'
|
|
155
|
+
: 'CALLS';
|
|
156
|
+
return ` ${user.file}:${user.line} ${shortSymbolName(user.source)} ${note}`;
|
|
157
|
+
}
|
|
158
|
+
export function renderChanges(result, budget = CHANGES_BUDGET, extra) {
|
|
159
|
+
const renderStart = performance.now();
|
|
160
|
+
const finish = (text) => {
|
|
161
|
+
if (result.timings !== undefined && !Object.isFrozen(result.timings)) {
|
|
162
|
+
result.timings.renderMs = Math.round((performance.now() - renderStart) * 100) / 100;
|
|
163
|
+
}
|
|
164
|
+
return text;
|
|
165
|
+
};
|
|
166
|
+
if (result.nothingChanged === true || result.changedFiles.length === 0) {
|
|
167
|
+
return finish(`CHANGES vs ${formatBase(result)} — nothing changed in the working tree.`.slice(0, budget));
|
|
168
|
+
}
|
|
169
|
+
const isStale = result.indexState === 'stale' ||
|
|
170
|
+
result.impact.completeness.reasons.some((r) => r.startsWith('stale-index:'));
|
|
171
|
+
const zoneText = result.verdict.zones.length > 0
|
|
172
|
+
? ` (${result.verdict.zones.length === 1 ? 'zone' : 'zones'} ${result.verdict.zones.join(', ')})`
|
|
173
|
+
: '';
|
|
174
|
+
const line1 = `CHANGES vs ${formatBase(result)} — ${plural(result.changedFiles.length, 'file', 'files')}, ${plural(result.changedSymbols.length, 'symbol', 'symbols')}`;
|
|
175
|
+
const line2 = isStale
|
|
176
|
+
? `ACTION ${result.verdict.action} · level ${result.verdict.level}${zoneText} · picture partial (stale index)`
|
|
177
|
+
: result.impact.completeness.status !== 'complete'
|
|
178
|
+
? `ACTION ${result.verdict.action} · level ${result.verdict.level}${zoneText} · picture ${result.impact.completeness.status} (${plural(result.impact.completeness.reasons.length, 'gap', 'gaps')})`
|
|
179
|
+
: `ACTION ${result.verdict.action} · level ${result.verdict.level}${zoneText} · picture complete`;
|
|
180
|
+
const { reach, factors } = result.impact;
|
|
181
|
+
const factorsText = factors.length === 0 ? 'none' : factors.join(', ');
|
|
182
|
+
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: ${factorsText}`;
|
|
183
|
+
const headerLines = [line1, line2, line3, ''];
|
|
184
|
+
const removedWithUsers = result.changedSymbols.filter((s) => s.change === 'removed' && ((s.callers ?? 0) > 0 || (s.liveUsers?.length ?? 0) > 0));
|
|
185
|
+
const sigChangedWithUsers = result.changedSymbols.filter((s) => s.change === 'signature-changed' && ((s.callers ?? 0) > 0 || (s.liveUsers?.length ?? 0) > 0));
|
|
186
|
+
const sigChangedNoUsers = result.changedSymbols.filter((s) => s.change === 'signature-changed' &&
|
|
187
|
+
(s.callers ?? 0) === 0 &&
|
|
188
|
+
(s.liveUsers?.length ?? 0) === 0);
|
|
189
|
+
const modified = result.changedSymbols.filter((s) => s.change === 'modified');
|
|
190
|
+
const added = result.changedSymbols.filter((s) => s.change === 'added');
|
|
191
|
+
const removedNoUsers = result.changedSymbols.filter((s) => s.change === 'removed' && (s.callers ?? 0) === 0 && (s.liveUsers?.length ?? 0) === 0);
|
|
192
|
+
// Priority sections: removed/signature-changed symbols with live users are kept in full first.
|
|
193
|
+
const priorityLines = [];
|
|
194
|
+
if (removedWithUsers.length > 0) {
|
|
195
|
+
priorityLines.push(`Removed, still used (${removedWithUsers.length.toLocaleString('en-US')})`);
|
|
196
|
+
for (const sym of removedWithUsers)
|
|
197
|
+
priorityLines.push(...formatPrioritySymbol(sym));
|
|
198
|
+
}
|
|
199
|
+
if (sigChangedWithUsers.length > 0) {
|
|
200
|
+
priorityLines.push(`Signature changed, still called (${sigChangedWithUsers.length.toLocaleString('en-US')})`);
|
|
201
|
+
for (const sym of sigChangedWithUsers)
|
|
202
|
+
priorityLines.push(...formatPrioritySymbol(sym));
|
|
203
|
+
}
|
|
204
|
+
// Plan sections (D58)
|
|
205
|
+
const planLines = [];
|
|
206
|
+
if (result.plan?.provided) {
|
|
207
|
+
if (result.plan.notInPlan.length > 0) {
|
|
208
|
+
planLines.push(`Not in plan (${result.plan.notInPlan.length.toLocaleString('en-US')})`);
|
|
209
|
+
for (const item of result.plan.notInPlan) {
|
|
210
|
+
const symObj = {
|
|
211
|
+
symbol: item.symbol,
|
|
212
|
+
change: item.change,
|
|
213
|
+
callers: item.callers,
|
|
214
|
+
liveUsers: item.liveUsers,
|
|
215
|
+
};
|
|
216
|
+
planLines.push(formatSummarySymbolLine(symObj));
|
|
217
|
+
for (const user of item.liveUsers) {
|
|
218
|
+
planLines.push(formatUserLine(user));
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
if (result.plan.unchanged.length > 0) {
|
|
223
|
+
planLines.push(`Planned, unchanged (${result.plan.unchanged.length.toLocaleString('en-US')})`);
|
|
224
|
+
for (const target of result.plan.unchanged)
|
|
225
|
+
planLines.push(` ${target}`);
|
|
226
|
+
}
|
|
227
|
+
if (result.plan.ambiguous.length > 0) {
|
|
228
|
+
planLines.push(`Plan guidance (${result.plan.ambiguous.length.toLocaleString('en-US')})`);
|
|
229
|
+
for (const amb of result.plan.ambiguous) {
|
|
230
|
+
planLines.push(` AMBIGUOUS ${amb.target} — ${amb.candidates.join(', ')}`);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
const symSection = (title, list) => list.length === 0
|
|
235
|
+
? []
|
|
236
|
+
: [`${title} (${list.length.toLocaleString('en-US')})`, ...list.map(formatSummarySymbolLine)];
|
|
237
|
+
const depthLines = [];
|
|
238
|
+
for (let d = 1; d < result.impact.byDepth.length; d++) {
|
|
239
|
+
const levelNodes = result.impact.byDepth[d] ?? [];
|
|
240
|
+
if (levelNodes.length === 0)
|
|
241
|
+
continue;
|
|
242
|
+
depthLines.push(`Depth ${d + 1} (${levelNodes.length.toLocaleString('en-US')})`);
|
|
243
|
+
for (const node of levelNodes) {
|
|
244
|
+
const markers = [];
|
|
245
|
+
if (node.route)
|
|
246
|
+
markers.push('route');
|
|
247
|
+
if (node.strength === 'heuristic')
|
|
248
|
+
markers.push('heuristic');
|
|
249
|
+
if (node.boundaries)
|
|
250
|
+
markers.push(...node.boundaries);
|
|
251
|
+
if (node.gapScoped)
|
|
252
|
+
markers.push('gap');
|
|
253
|
+
const suffix = markers.length > 0 ? ` ${markers.join(' ')}` : '';
|
|
254
|
+
depthLines.push(` ${node.span.file}:${node.span.startLine} ${shortSymbolName(node.symbol)} ${node.via}${suffix}`);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
const symbolDetailLines = [
|
|
258
|
+
...symSection('Signature changed', sigChangedNoUsers),
|
|
259
|
+
...symSection('Modified', modified),
|
|
260
|
+
...symSection('Added', added),
|
|
261
|
+
...symSection('Removed', removedNoUsers),
|
|
262
|
+
...depthLines,
|
|
263
|
+
];
|
|
264
|
+
const unindexedItems = (result.unindexedDetails ?? []).map((d) => ` ${d.path} — ${d.reason}`);
|
|
265
|
+
const unindexedLines = unindexedItems.length === 0
|
|
266
|
+
? []
|
|
267
|
+
: [`Not analysed (${unindexedItems.length.toLocaleString('en-US')})`, ...unindexedItems];
|
|
268
|
+
const testsLines = result.impact.tests.length === 0
|
|
269
|
+
? ['Tests to run: none']
|
|
270
|
+
: [
|
|
271
|
+
`Tests to run (${result.impact.tests.length.toLocaleString('en-US')})`,
|
|
272
|
+
...result.impact.tests.map((t) => ` ${t}`),
|
|
273
|
+
];
|
|
274
|
+
const reasons = result.impact.completeness.reasons;
|
|
275
|
+
const gapsLines = reasons.length === 0
|
|
276
|
+
? ['Gaps: none']
|
|
277
|
+
: [`Gaps (${reasons.length.toLocaleString('en-US')})`, ...reasons.map((r) => ` ${r}`)];
|
|
278
|
+
const nextLine = `${formatNext(result)}${extra?.nextHint ?? ''}`;
|
|
279
|
+
const footerLine = formatFooter(result);
|
|
280
|
+
const tailLines = extra?.line === undefined ? [nextLine, footerLine] : [extra.line, nextLine, footerLine];
|
|
281
|
+
const assemble = (p, pl, sd, ui, ts, gp) => [...headerLines, ...p, ...pl, ...sd, ...ui, ...ts, ...gp, ...tailLines].join('\n');
|
|
282
|
+
const full = assemble(priorityLines, planLines, symbolDetailLines, unindexedLines, testsLines, gapsLines);
|
|
283
|
+
if (full.length <= budget)
|
|
284
|
+
return finish(full);
|
|
285
|
+
// Progressive compaction preserving header, verdict, reach, priority sections, Next, and footer.
|
|
286
|
+
const cGaps = reasons.length <= 1
|
|
287
|
+
? gapsLines
|
|
288
|
+
: compactSection('Gaps', reasons.map((r) => ` ${r}`), 2);
|
|
289
|
+
let text = assemble(priorityLines, planLines, symbolDetailLines, unindexedLines, testsLines, cGaps);
|
|
290
|
+
if (text.length <= budget)
|
|
291
|
+
return finish(text);
|
|
292
|
+
const cTests = result.impact.tests.length <= 1
|
|
293
|
+
? testsLines
|
|
294
|
+
: compactSection('Tests to run', result.impact.tests.map((t) => ` ${t}`), 2);
|
|
295
|
+
text = assemble(priorityLines, planLines, symbolDetailLines, unindexedLines, cTests, cGaps);
|
|
296
|
+
if (text.length <= budget)
|
|
297
|
+
return finish(text);
|
|
298
|
+
const cUnindexed = compactSection('Not analysed', unindexedItems, 3);
|
|
299
|
+
text = assemble(priorityLines, planLines, symbolDetailLines, cUnindexed, cTests, cGaps);
|
|
300
|
+
if (text.length <= budget)
|
|
301
|
+
return finish(text);
|
|
302
|
+
const cSymDetails = [
|
|
303
|
+
...compactSection('Signature changed', sigChangedNoUsers.map(formatSummarySymbolLine), 2),
|
|
304
|
+
...compactSection('Modified', modified.map(formatSummarySymbolLine), 2),
|
|
305
|
+
...compactSection('Added', added.map(formatSummarySymbolLine), 2),
|
|
306
|
+
...compactSection('Removed', removedNoUsers.map(formatSummarySymbolLine), 2),
|
|
307
|
+
];
|
|
308
|
+
text = assemble(priorityLines, planLines, cSymDetails, cUnindexed, cTests, cGaps);
|
|
309
|
+
if (text.length <= budget)
|
|
310
|
+
return finish(text);
|
|
311
|
+
const cPlan = result.plan?.provided
|
|
312
|
+
? [
|
|
313
|
+
...(result.plan.notInPlan.length > 0
|
|
314
|
+
? [`Not in plan (${result.plan.notInPlan.length.toLocaleString('en-US')})`]
|
|
315
|
+
: []),
|
|
316
|
+
...(result.plan.unchanged.length > 0
|
|
317
|
+
? [`Planned, unchanged (${result.plan.unchanged.length.toLocaleString('en-US')})`]
|
|
318
|
+
: []),
|
|
319
|
+
...(result.plan.ambiguous.length > 0
|
|
320
|
+
? [`Plan guidance (${result.plan.ambiguous.length.toLocaleString('en-US')})`]
|
|
321
|
+
: []),
|
|
322
|
+
]
|
|
323
|
+
: [];
|
|
324
|
+
text = assemble(priorityLines, cPlan, cSymDetails, cUnindexed, cTests, cGaps);
|
|
325
|
+
if (text.length <= budget)
|
|
326
|
+
return finish(text);
|
|
327
|
+
// Even tighter summary counts while keeping header + verdict + reach + priorityLines intact.
|
|
328
|
+
const cSummary = [
|
|
329
|
+
...(sigChangedNoUsers.length > 0
|
|
330
|
+
? [`Signature changed (${sigChangedNoUsers.length.toLocaleString('en-US')})`]
|
|
331
|
+
: []),
|
|
332
|
+
...(modified.length > 0 ? [`Modified (${modified.length.toLocaleString('en-US')})`] : []),
|
|
333
|
+
...(added.length > 0 ? [`Added (${added.length.toLocaleString('en-US')})`] : []),
|
|
334
|
+
...(removedNoUsers.length > 0
|
|
335
|
+
? [`Removed (${removedNoUsers.length.toLocaleString('en-US')})`]
|
|
336
|
+
: []),
|
|
337
|
+
...(unindexedItems.length > 0
|
|
338
|
+
? [`Not analysed (${unindexedItems.length.toLocaleString('en-US')})`]
|
|
339
|
+
: []),
|
|
340
|
+
...(result.impact.tests.length > 0
|
|
341
|
+
? [`Tests to run (${result.impact.tests.length.toLocaleString('en-US')})`]
|
|
342
|
+
: []),
|
|
343
|
+
...(reasons.length > 0 ? [`Gaps (${reasons.length.toLocaleString('en-US')})`] : []),
|
|
344
|
+
];
|
|
345
|
+
text = assemble(priorityLines, cPlan, cSummary, [], [], []);
|
|
346
|
+
if (text.length <= budget)
|
|
347
|
+
return finish(text);
|
|
348
|
+
// Last-resort hard cap: keep line1-3 + priorityLines as far as budget permits + tailLines.
|
|
349
|
+
const fixedTail = `\n${tailLines.join('\n')}`;
|
|
350
|
+
const fixedHead = `${line1}\n${line2}\n${line3}\n\n`;
|
|
351
|
+
const remaining = Math.max(0, budget - fixedHead.length - fixedTail.length);
|
|
352
|
+
const body = [...priorityLines, ...cPlan, ...cSummary].join('\n');
|
|
353
|
+
if (body.length <= remaining)
|
|
354
|
+
return finish(`${fixedHead}${body}${fixedTail}`);
|
|
355
|
+
return finish(`${fixedHead}${body.slice(0, Math.max(0, remaining - 20))}\n… (truncated)${fixedTail}`.slice(0, budget));
|
|
356
|
+
}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { chmod, mkdir } from 'node:fs/promises';
|
|
2
|
+
import { isRecord } from '../kernel/guards.js';
|
|
3
|
+
import { createEngineHost } from './host.js';
|
|
4
|
+
import { acquireBuildLock } from './lock.js';
|
|
5
|
+
import { chooseRuntime, memoryLimitMb } from './runtime.js';
|
|
6
|
+
import { pinnedTypescriptVersion, resolveTypescript } from './typescript.js';
|
|
7
|
+
/** Owner commands are long (a cold oh-my-pi build is minutes) but never unbounded. */
|
|
8
|
+
const OWNER_COMMAND_MS = 60 * 60_000;
|
|
9
|
+
/**
|
|
10
|
+
* Runs one engine op for the repository with the pinned TypeScript verified and its build lock
|
|
11
|
+
* held. Only the lock holder starts a child (D35); the child is stopped afterwards.
|
|
12
|
+
*/
|
|
13
|
+
async function runLocked(workspace, op, args, options) {
|
|
14
|
+
if (options.signal?.aborted) {
|
|
15
|
+
return { ok: false, code: 'index-failed', message: 'Cancelled', fix: '' };
|
|
16
|
+
}
|
|
17
|
+
const typescript = await resolveTypescript({
|
|
18
|
+
version: pinnedTypescriptVersion(),
|
|
19
|
+
cacheHome: options.cacheHome,
|
|
20
|
+
repoRoot: workspace.root,
|
|
21
|
+
useWorkspaceTypescript: workspace.config.impact.useWorkspaceTypescript,
|
|
22
|
+
});
|
|
23
|
+
if (!typescript.ok) {
|
|
24
|
+
return { ok: false, code: 'degraded', message: typescript.reason, fix: typescript.fix };
|
|
25
|
+
}
|
|
26
|
+
if (options.signal?.aborted) {
|
|
27
|
+
return { ok: false, code: 'index-failed', message: 'Cancelled', fix: '' };
|
|
28
|
+
}
|
|
29
|
+
await mkdir(workspace.cacheDir, { recursive: true, mode: 0o700 });
|
|
30
|
+
await chmod(workspace.cacheDir, 0o700);
|
|
31
|
+
const lock = acquireBuildLock(workspace.cacheDir);
|
|
32
|
+
if (!lock.ok) {
|
|
33
|
+
const holder = lock.holder;
|
|
34
|
+
return {
|
|
35
|
+
ok: false,
|
|
36
|
+
code: 'busy',
|
|
37
|
+
message: holder
|
|
38
|
+
? `index build in progress (pid ${holder.pid} since ${holder.startedAt})`
|
|
39
|
+
: 'index build lock is held by another session',
|
|
40
|
+
fix: 'wait for it to publish, then run: ei status',
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
const runtime = await chooseRuntime(options.env);
|
|
44
|
+
if (options.signal?.aborted) {
|
|
45
|
+
lock.release();
|
|
46
|
+
return { ok: false, code: 'index-failed', message: 'Cancelled', fix: '' };
|
|
47
|
+
}
|
|
48
|
+
const host = createEngineHost({
|
|
49
|
+
runtime,
|
|
50
|
+
memoryLimitMb: memoryLimitMb(workspace.config.impact.maxMemoryMb),
|
|
51
|
+
env: options.env,
|
|
52
|
+
});
|
|
53
|
+
const onAbort = () => {
|
|
54
|
+
void host.stop();
|
|
55
|
+
};
|
|
56
|
+
options.signal?.addEventListener('abort', onAbort, { once: true });
|
|
57
|
+
if (options.signal?.aborted)
|
|
58
|
+
onAbort();
|
|
59
|
+
try {
|
|
60
|
+
const reply = await host.request(op, {
|
|
61
|
+
...args,
|
|
62
|
+
root: workspace.root,
|
|
63
|
+
cacheDir: workspace.cacheDir,
|
|
64
|
+
typescriptPath: typescript.runtime.path,
|
|
65
|
+
...(options.onHudProgress === undefined ? {} : { hudProgress: true }),
|
|
66
|
+
}, {
|
|
67
|
+
onProgress: options.onHudProgress === undefined
|
|
68
|
+
? options.onProgress
|
|
69
|
+
: (message, data) => {
|
|
70
|
+
if (message === '__hud_progress__' &&
|
|
71
|
+
isRecord(data) &&
|
|
72
|
+
isRecord(data.hudProgress)) {
|
|
73
|
+
const event = data.hudProgress;
|
|
74
|
+
if (typeof event.phase === 'string') {
|
|
75
|
+
try {
|
|
76
|
+
options.onHudProgress?.({
|
|
77
|
+
phase: event.phase,
|
|
78
|
+
...(typeof event.done === 'number' ? { done: event.done } : {}),
|
|
79
|
+
...(typeof event.total === 'number' ? { total: event.total } : {}),
|
|
80
|
+
...(typeof event.file === 'string' ? { file: event.file } : {}),
|
|
81
|
+
...(event.kind === 'cold' || event.kind === 'update'
|
|
82
|
+
? { kind: event.kind }
|
|
83
|
+
: {}),
|
|
84
|
+
...(typeof event.reason === 'string' ? { reason: event.reason } : {}),
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
catch {
|
|
88
|
+
// Presentation cannot change the build outcome.
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
options.onProgress?.(message, data);
|
|
94
|
+
},
|
|
95
|
+
timeoutMs: options.timeoutMs ?? OWNER_COMMAND_MS,
|
|
96
|
+
});
|
|
97
|
+
if (options.signal?.aborted) {
|
|
98
|
+
return { ok: false, code: 'index-failed', message: 'Cancelled', fix: '' };
|
|
99
|
+
}
|
|
100
|
+
if (reply.ok && isRecord(reply.value))
|
|
101
|
+
return { ok: true, value: reply.value };
|
|
102
|
+
if (reply.ok) {
|
|
103
|
+
return { ok: false, code: 'index-failed', message: 'engine reply is not an object', fix: '' };
|
|
104
|
+
}
|
|
105
|
+
const fix = reply.reason === 'memory-limit'
|
|
106
|
+
? 'narrow impact.include or raise impact.maxMemoryMb, then run: ei index'
|
|
107
|
+
: reply.reason === 'timeout'
|
|
108
|
+
? `the engine hung or is far slower than expected; retry ei ${op}`
|
|
109
|
+
: `retry once: ei ${op}`;
|
|
110
|
+
return { ok: false, code: 'index-failed', message: reply.message, fix };
|
|
111
|
+
}
|
|
112
|
+
finally {
|
|
113
|
+
options.signal?.removeEventListener('abort', onAbort);
|
|
114
|
+
await host.stop();
|
|
115
|
+
lock.release();
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
/** `ei index [--full]`: an update from the published index, or a cold build. */
|
|
119
|
+
export function buildIndex(workspace, options = {}) {
|
|
120
|
+
return runLocked(workspace, 'index', { full: options.full === true, freshness: 'hash' }, options);
|
|
121
|
+
}
|
|
122
|
+
/** `ei doctor --deep [--differential N]`: cold rebuild into a temp database, diffed. */
|
|
123
|
+
export function doctorDeep(workspace, options = {}) {
|
|
124
|
+
return runLocked(workspace, 'doctor', { differential: options.differential ?? 0 }, options);
|
|
125
|
+
}
|
|
126
|
+
export async function runChangesDiff(workspace, files, options = {}) {
|
|
127
|
+
if (options.signal?.aborted) {
|
|
128
|
+
return { ok: false, code: 'index-failed', message: 'Cancelled', fix: '' };
|
|
129
|
+
}
|
|
130
|
+
const typescript = await resolveTypescript({
|
|
131
|
+
version: pinnedTypescriptVersion(),
|
|
132
|
+
cacheHome: options.cacheHome,
|
|
133
|
+
repoRoot: workspace.root,
|
|
134
|
+
useWorkspaceTypescript: workspace.config.impact.useWorkspaceTypescript,
|
|
135
|
+
});
|
|
136
|
+
if (!typescript.ok) {
|
|
137
|
+
return { ok: false, code: 'degraded', message: typescript.reason, fix: typescript.fix };
|
|
138
|
+
}
|
|
139
|
+
const runtime = await chooseRuntime(options.env);
|
|
140
|
+
if (options.signal?.aborted) {
|
|
141
|
+
return { ok: false, code: 'index-failed', message: 'Cancelled', fix: '' };
|
|
142
|
+
}
|
|
143
|
+
const host = createEngineHost({
|
|
144
|
+
runtime,
|
|
145
|
+
memoryLimitMb: memoryLimitMb(workspace.config.impact.maxMemoryMb),
|
|
146
|
+
env: options.env,
|
|
147
|
+
});
|
|
148
|
+
const onAbort = () => {
|
|
149
|
+
void host.stop();
|
|
150
|
+
};
|
|
151
|
+
options.signal?.addEventListener('abort', onAbort, { once: true });
|
|
152
|
+
if (options.signal?.aborted)
|
|
153
|
+
onAbort();
|
|
154
|
+
try {
|
|
155
|
+
const reply = await host.request('changes-diff', {
|
|
156
|
+
typescriptPath: typescript.runtime.path,
|
|
157
|
+
files,
|
|
158
|
+
}, { onProgress: options.onProgress, timeoutMs: options.timeoutMs ?? 60_000 });
|
|
159
|
+
if (options.signal?.aborted) {
|
|
160
|
+
return { ok: false, code: 'index-failed', message: 'Cancelled', fix: '' };
|
|
161
|
+
}
|
|
162
|
+
if (reply.ok && isRecord(reply.value))
|
|
163
|
+
return { ok: true, value: reply.value };
|
|
164
|
+
if (reply.ok) {
|
|
165
|
+
return { ok: false, code: 'index-failed', message: 'engine reply is not an object', fix: '' };
|
|
166
|
+
}
|
|
167
|
+
return {
|
|
168
|
+
ok: false,
|
|
169
|
+
code: 'index-failed',
|
|
170
|
+
message: reply.message,
|
|
171
|
+
fix: 'retry once: ei changes',
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
finally {
|
|
175
|
+
options.signal?.removeEventListener('abort', onAbort);
|
|
176
|
+
await host.stop();
|
|
177
|
+
}
|
|
178
|
+
}
|