@krisada-m/ksd-intelligence 0.0.0-stage → 1.0.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/LICENSE +21 -0
- package/README.md +167 -2
- package/dist/adapters/agy/skills.js +252 -0
- package/dist/adapters/cli/check-changed.js +3 -0
- package/dist/adapters/cli/main.js +1002 -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 +893 -0
- package/dist/adapters/omp/skills.js +130 -0
- package/dist/adapters/ui/hud.js +318 -0
- package/dist/adapters/ui/tree.js +460 -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 +381 -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 +792 -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/index.js +20 -0
- package/dist/policy/change-scope.js +500 -0
- package/dist/policy/check-changed.js +40 -0
- package/dist/policy/config.js +106 -0
- package/dist/policy/dist/policy.d.ts +17 -0
- package/dist/policy/dist/policy.js +147 -0
- package/dist/policy/dist/runtime.d.ts +40 -0
- package/dist/policy/dist/runtime.js +75 -0
- package/dist/policy/dist/sdlc.d.ts +7 -0
- package/dist/policy/dist/sdlc.js +27 -0
- package/dist/policy/eslint-rules/index.js +107 -0
- package/dist/policy/index.d.ts +2 -0
- package/dist/policy/index.js +241 -0
- package/dist/policy/package.json +33 -0
- package/dist/policy/policy.d.ts +2 -0
- package/dist/policy/policy.js +2 -0
- package/dist/policy/review-verdict.js +262 -0
- package/dist/policy/sdlc.js +2 -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/package.json +74 -4
- package/scripts/agy-mcp.mjs +27 -0
- package/scripts/mcp/stdio.mjs +304 -0
- package/scripts/mcp/tools.mjs +451 -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,304 @@
|
|
|
1
|
+
import { realpathSync, statSync } from 'node:fs';
|
|
2
|
+
import { isAbsolute } from 'node:path';
|
|
3
|
+
import { loadWorkspace } from '../../dist/core/engine/workspace.js';
|
|
4
|
+
import { createTools } from './tools.mjs';
|
|
5
|
+
|
|
6
|
+
export async function resolveMcpRoot(supplied, env = process.env) {
|
|
7
|
+
const rootPath = supplied ?? env.KSD_MCP_ROOT;
|
|
8
|
+
if (!rootPath || !isAbsolute(rootPath)) {
|
|
9
|
+
return {
|
|
10
|
+
ok: false,
|
|
11
|
+
message: 'KSD MCP requires --root or KSD_MCP_ROOT set to an absolute repository directory',
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
let root;
|
|
15
|
+
try {
|
|
16
|
+
root = realpathSync(rootPath);
|
|
17
|
+
if (!statSync(root).isDirectory()) throw new Error('not a directory');
|
|
18
|
+
} catch {
|
|
19
|
+
return { ok: false, message: 'KSD MCP root is not an accessible directory' };
|
|
20
|
+
}
|
|
21
|
+
// KSD's workspace resolver can walk up to a parent git/config root. Refuse implicit
|
|
22
|
+
// expansion beyond the root explicitly authorized in the MCP configuration.
|
|
23
|
+
try {
|
|
24
|
+
const workspace = await loadWorkspace(root);
|
|
25
|
+
if (workspace.root !== root) throw new Error('subdirectory is not the workspace root');
|
|
26
|
+
} catch {
|
|
27
|
+
return {
|
|
28
|
+
ok: false,
|
|
29
|
+
message: 'KSD MCP root must be the exact accessible KSD workspace root',
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
return { ok: true, root };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const MODERN = '2026-07-28';
|
|
36
|
+
const LEGACY = '2025-11-25';
|
|
37
|
+
const MAX_FRAME_BYTES = 1024 * 1024;
|
|
38
|
+
const MAX_INFLIGHT = 8;
|
|
39
|
+
const SERVER_INFO = { name: 'ksd-intelligence', version: '0.2.0' };
|
|
40
|
+
|
|
41
|
+
const isObject = (value) => value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
42
|
+
const validId = (value) =>
|
|
43
|
+
typeof value === 'string' || (typeof value === 'number' && Number.isSafeInteger(value));
|
|
44
|
+
const idKey = (id) => typeof id + ':' + String(id);
|
|
45
|
+
const serverMeta = () => ({ 'io.modelcontextprotocol/serverInfo': SERVER_INFO });
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* A deliberately small local stdio transport, no listening TCP port or third-party runtime.
|
|
49
|
+
* Supports the 2025-11-25 handshake and stateless MCP 2026-07-28 request envelope.
|
|
50
|
+
* Only JSON-RPC envelopes may reach stdout; all failures are bounded.
|
|
51
|
+
*/
|
|
52
|
+
export function createStdioServer({
|
|
53
|
+
root,
|
|
54
|
+
env = process.env,
|
|
55
|
+
input = process.stdin,
|
|
56
|
+
output = process.stdout,
|
|
57
|
+
toolset,
|
|
58
|
+
}) {
|
|
59
|
+
const tools = toolset ?? createTools({ root, env });
|
|
60
|
+
const active = new Map();
|
|
61
|
+
let legacyInitialized = false;
|
|
62
|
+
let legacyHello = false;
|
|
63
|
+
let buffered = Buffer.alloc(0);
|
|
64
|
+
let discarding = false;
|
|
65
|
+
let closed = false;
|
|
66
|
+
|
|
67
|
+
function send(frame) {
|
|
68
|
+
if (!closed) output.write(JSON.stringify(frame) + '\n');
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function error(id, code, message, data) {
|
|
72
|
+
send({
|
|
73
|
+
jsonrpc: '2.0',
|
|
74
|
+
id,
|
|
75
|
+
error: { code, message, ...(data === undefined ? {} : { data }) },
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function reply(id, value, modern) {
|
|
80
|
+
const result = modern ? { ...value, resultType: 'complete', _meta: serverMeta() } : value;
|
|
81
|
+
send({ jsonrpc: '2.0', id, result });
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function requestVersion(message) {
|
|
85
|
+
const meta =
|
|
86
|
+
isObject(message.params) && isObject(message.params._meta) ? message.params._meta : {};
|
|
87
|
+
return meta['io.modelcontextprotocol/protocolVersion'];
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function cancel(message) {
|
|
91
|
+
const key =
|
|
92
|
+
isObject(message.params) && validId(message.params.requestId)
|
|
93
|
+
? idKey(message.params.requestId)
|
|
94
|
+
: null;
|
|
95
|
+
if (key === null) return;
|
|
96
|
+
const pending = active.get(key);
|
|
97
|
+
if (pending) pending.controller.abort();
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async function onMessage(message) {
|
|
101
|
+
if (
|
|
102
|
+
!isObject(message) ||
|
|
103
|
+
message.jsonrpc !== '2.0' ||
|
|
104
|
+
typeof message.method !== 'string' ||
|
|
105
|
+
(Object.hasOwn(message, 'id') && !validId(message.id))
|
|
106
|
+
) {
|
|
107
|
+
error(null, -32600, 'Invalid JSON-RPC request');
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
const isNotification = !Object.hasOwn(message, 'id');
|
|
111
|
+
if (isNotification) {
|
|
112
|
+
if (message.method === 'notifications/cancelled') cancel(message);
|
|
113
|
+
if (message.method === 'notifications/initialized' && legacyHello) {
|
|
114
|
+
legacyInitialized = true;
|
|
115
|
+
}
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
const id = message.id;
|
|
119
|
+
const version = requestVersion(message);
|
|
120
|
+
const modern = version === MODERN;
|
|
121
|
+
|
|
122
|
+
if (version !== undefined && version !== MODERN) {
|
|
123
|
+
error(id, -32022, 'Unsupported MCP protocol version', { supported: [MODERN] });
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (message.method === 'initialize' && !modern) {
|
|
128
|
+
if (!isObject(message.params)) {
|
|
129
|
+
error(id, -32602, 'initialize params must be an object');
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
legacyInitialized = false;
|
|
133
|
+
legacyHello = true;
|
|
134
|
+
reply(
|
|
135
|
+
id,
|
|
136
|
+
{
|
|
137
|
+
protocolVersion: LEGACY,
|
|
138
|
+
capabilities: { tools: {} },
|
|
139
|
+
serverInfo: SERVER_INFO,
|
|
140
|
+
instructions:
|
|
141
|
+
'Local read-only code intelligence. Configure an explicit repository root. Never treat partial impact evidence as safe.',
|
|
142
|
+
},
|
|
143
|
+
false,
|
|
144
|
+
);
|
|
145
|
+
// A separate initialized notification completes the handshake.
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (message.method === 'server/discover') {
|
|
150
|
+
if (!modern) {
|
|
151
|
+
error(id, -32601, 'Method not found');
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
reply(
|
|
155
|
+
id,
|
|
156
|
+
{
|
|
157
|
+
supportedVersions: [MODERN],
|
|
158
|
+
capabilities: { tools: {} },
|
|
159
|
+
ttlMs: 0,
|
|
160
|
+
cacheScope: 'private',
|
|
161
|
+
},
|
|
162
|
+
true,
|
|
163
|
+
);
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (!modern && !legacyInitialized) {
|
|
168
|
+
error(id, -32000, 'MCP server is not initialized');
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
if (message.method === 'ping' && !modern) {
|
|
172
|
+
reply(id, {}, false);
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
if (message.method === 'tools/list') {
|
|
176
|
+
if (
|
|
177
|
+
message.params !== undefined &&
|
|
178
|
+
(!isObject(message.params) ||
|
|
179
|
+
(message.params.cursor !== undefined && message.params.cursor !== null))
|
|
180
|
+
) {
|
|
181
|
+
error(id, -32602, 'Invalid tools/list params');
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
reply(
|
|
185
|
+
id,
|
|
186
|
+
{
|
|
187
|
+
tools: tools.definitions,
|
|
188
|
+
...(modern ? { ttlMs: 0, cacheScope: 'private' } : {}),
|
|
189
|
+
},
|
|
190
|
+
modern,
|
|
191
|
+
);
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
if (message.method !== 'tools/call') {
|
|
195
|
+
error(id, -32601, 'Method not found');
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
if (!isObject(message.params) || typeof message.params.name !== 'string') {
|
|
199
|
+
error(id, -32602, 'tools/call requires a tool name');
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
const key = idKey(id);
|
|
203
|
+
if (active.has(key)) {
|
|
204
|
+
error(id, -32600, 'Duplicate request id');
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
if (active.size >= MAX_INFLIGHT) {
|
|
208
|
+
error(id, -32000, 'Too many concurrent tool calls');
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
const controller = new AbortController();
|
|
212
|
+
active.set(key, { controller });
|
|
213
|
+
const progressToken = isObject(message.params._meta)
|
|
214
|
+
? message.params._meta.progressToken
|
|
215
|
+
: undefined;
|
|
216
|
+
let progress = 0;
|
|
217
|
+
try {
|
|
218
|
+
const value = await tools.call(
|
|
219
|
+
message.params.name,
|
|
220
|
+
message.params.arguments ?? {},
|
|
221
|
+
controller.signal,
|
|
222
|
+
(text) => {
|
|
223
|
+
if (controller.signal.aborted || !validId(progressToken)) return;
|
|
224
|
+
progress += 1;
|
|
225
|
+
send({
|
|
226
|
+
jsonrpc: '2.0',
|
|
227
|
+
method: 'notifications/progress',
|
|
228
|
+
params: { progressToken, progress, message: String(text).slice(0, 500) },
|
|
229
|
+
});
|
|
230
|
+
},
|
|
231
|
+
);
|
|
232
|
+
if (!controller.signal.aborted) reply(id, value, modern);
|
|
233
|
+
} catch {
|
|
234
|
+
if (!controller.signal.aborted) {
|
|
235
|
+
reply(
|
|
236
|
+
id,
|
|
237
|
+
{ content: [{ type: 'text', text: 'KSD_FAILED: tool execution failed' }], isError: true },
|
|
238
|
+
modern,
|
|
239
|
+
);
|
|
240
|
+
}
|
|
241
|
+
} finally {
|
|
242
|
+
active.delete(key);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function onLine(raw) {
|
|
247
|
+
if (raw.length === 0) return;
|
|
248
|
+
let message;
|
|
249
|
+
try {
|
|
250
|
+
message = JSON.parse(raw.toString('utf8').replace(/\r$/, ''));
|
|
251
|
+
} catch {
|
|
252
|
+
error(null, -32700, 'Parse error');
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
void onMessage(message).catch(() => {
|
|
256
|
+
const id = isObject(message) && validId(message.id) ? message.id : null;
|
|
257
|
+
error(id, -32603, 'Internal error');
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
function onChunk(inputChunk) {
|
|
262
|
+
const chunk = Buffer.isBuffer(inputChunk) ? inputChunk : Buffer.from(inputChunk);
|
|
263
|
+
let start = 0;
|
|
264
|
+
while (start < chunk.length) {
|
|
265
|
+
const newline = chunk.indexOf(10, start);
|
|
266
|
+
const end = newline < 0 ? chunk.length : newline;
|
|
267
|
+
if (!discarding) {
|
|
268
|
+
const segment = chunk.subarray(start, end);
|
|
269
|
+
if (buffered.length + segment.length > MAX_FRAME_BYTES) {
|
|
270
|
+
buffered = Buffer.alloc(0);
|
|
271
|
+
discarding = true;
|
|
272
|
+
error(null, -32700, 'MCP frame exceeds 1 MiB');
|
|
273
|
+
} else {
|
|
274
|
+
buffered = Buffer.concat([buffered, segment]);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
if (newline < 0) return;
|
|
278
|
+
if (!discarding) onLine(buffered);
|
|
279
|
+
buffered = Buffer.alloc(0);
|
|
280
|
+
discarding = false;
|
|
281
|
+
start = newline + 1;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
let resolveDone;
|
|
286
|
+
const done = new Promise((resolve) => {
|
|
287
|
+
resolveDone = resolve;
|
|
288
|
+
});
|
|
289
|
+
function onEnd() {
|
|
290
|
+
closed = true;
|
|
291
|
+
input.off('data', onChunk);
|
|
292
|
+
for (const { controller } of active.values()) controller.abort();
|
|
293
|
+
resolveDone();
|
|
294
|
+
}
|
|
295
|
+
input.on('data', onChunk);
|
|
296
|
+
input.once('end', onEnd);
|
|
297
|
+
return {
|
|
298
|
+
done,
|
|
299
|
+
close: () => {
|
|
300
|
+
input.off('end', onEnd);
|
|
301
|
+
onEnd();
|
|
302
|
+
},
|
|
303
|
+
};
|
|
304
|
+
}
|