@dimassetoid/septum-mcp 0.0.0-stage → 0.2.1
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 +124 -2
- package/bin/septum.ts +4 -0
- package/package.json +70 -4
- package/scripts/postinstall.ts +25 -0
- package/src/cli/commands/check.ts +122 -0
- package/src/cli/commands/hook.ts +268 -0
- package/src/cli/commands/init.ts +553 -0
- package/src/cli/commands/serve.ts +5 -0
- package/src/cli/commands/sync.ts +41 -0
- package/src/cli/commands/topology.ts +161 -0
- package/src/cli/index.ts +167 -0
- package/src/cli/scaffold/harness-config.ts +621 -0
- package/src/cli/templates/hooks.ts +75 -0
- package/src/cli/templates/rules.ts +50 -0
- package/src/cli/templates/skill.ts +51 -0
- package/src/cli/ui/prompts.ts +122 -0
- package/src/core/boundary/evaluator.ts +442 -0
- package/src/core/boundary/staged-auditor.ts +58 -0
- package/src/core/config/loader.ts +122 -0
- package/src/core/config/schema.ts +69 -0
- package/src/core/database/client.ts +60 -0
- package/src/core/database/repositories/dependency.repository.ts +712 -0
- package/src/core/database/repositories/domain.repository.ts +330 -0
- package/src/core/database/repositories/environment.repository.ts +207 -0
- package/src/core/database/repositories/file.repository.ts +117 -0
- package/src/core/database/repositories/meta.repository.ts +33 -0
- package/src/core/database/repositories/symbol.repository.ts +508 -0
- package/src/core/database/repository.ts +303 -0
- package/src/core/database/schema.ts +154 -0
- package/src/core/discovery/topology-detector.ts +805 -0
- package/src/core/ingestion/hasher.ts +5 -0
- package/src/core/ingestion/pipeline.ts +623 -0
- package/src/core/parser/behavioral-analyzer.ts +119 -0
- package/src/core/parser/boundary-tracker.ts +443 -0
- package/src/core/parser/extractors/base.ts +67 -0
- package/src/core/parser/extractors/blade.ts +159 -0
- package/src/core/parser/extractors/csharp.ts +244 -0
- package/src/core/parser/extractors/django-semantic.ts +687 -0
- package/src/core/parser/extractors/elysia-semantic.ts +538 -0
- package/src/core/parser/extractors/express-semantic.ts +444 -0
- package/src/core/parser/extractors/fastapi-semantic.ts +736 -0
- package/src/core/parser/extractors/fastify-semantic.ts +774 -0
- package/src/core/parser/extractors/flask-semantic.ts +515 -0
- package/src/core/parser/extractors/frontend.ts +149 -0
- package/src/core/parser/extractors/go-semantic.ts +611 -0
- package/src/core/parser/extractors/go.ts +491 -0
- package/src/core/parser/extractors/hono-semantic.ts +567 -0
- package/src/core/parser/extractors/java.ts +225 -0
- package/src/core/parser/extractors/laravel-semantic.ts +1122 -0
- package/src/core/parser/extractors/mcp-cli-semantic.ts +368 -0
- package/src/core/parser/extractors/nestjs-semantic.ts +543 -0
- package/src/core/parser/extractors/php.ts +569 -0
- package/src/core/parser/extractors/python.ts +498 -0
- package/src/core/parser/extractors/registry.ts +78 -0
- package/src/core/parser/extractors/ruby.ts +234 -0
- package/src/core/parser/extractors/rust.ts +286 -0
- package/src/core/parser/extractors/semantic-extractor.interface.ts +22 -0
- package/src/core/parser/extractors/symfony-semantic.ts +354 -0
- package/src/core/parser/extractors/typescript.ts +826 -0
- package/src/core/parser/language-capabilities.ts +71 -0
- package/src/core/parser/schema/providers/laravel-migration-provider.ts +216 -0
- package/src/core/parser/schema/providers/prisma-schema-provider.ts +90 -0
- package/src/core/parser/schema/schema-provider-registry.ts +43 -0
- package/src/core/parser/schema/schema-provider.interface.ts +33 -0
- package/src/core/parser/tree-sitter.ts +63 -0
- package/src/core/parser/wasm-assets.ts +25 -0
- package/src/core/parser/wasm-loader.ts +274 -0
- package/src/core/resolver/call-graph-tracer.ts +724 -0
- package/src/core/resolver/import-resolver.ts +437 -0
- package/src/core/resolver/module-resolver.ts +482 -0
- package/src/core/resolver/path-resolver.ts +200 -0
- package/src/core/resolver/symbol-locator.ts +276 -0
- package/src/core/resolver/symbol-reference.ts +53 -0
- package/src/core/resolver/vertical-slice-tracer.ts +366 -0
- package/src/core/resources/ignore.ts +123 -0
- package/src/core/resources/limits.ts +14 -0
- package/src/core/resources/source-files.ts +62 -0
- package/src/core/resources/work-queue.ts +74 -0
- package/src/core/session/session-manager.ts +107 -0
- package/src/core/telemetry/telemetry.ts +97 -0
- package/src/core/topology/environment-detector.ts +973 -0
- package/src/core/topology/types.ts +83 -0
- package/src/index.ts +21 -0
- package/src/mcp/schemas.ts +1105 -0
- package/src/mcp/server.ts +774 -0
- package/src/mcp/tools/boundary.ts +17 -0
- package/src/mcp/tools/check-boundary.ts +387 -0
- package/src/mcp/tools/domain.ts +40 -0
- package/src/mcp/tools/feature.ts +67 -0
- package/src/mcp/tools/get-domain-catalog.ts +226 -0
- package/src/mcp/tools/get-environment-topology.ts +138 -0
- package/src/mcp/tools/get-feature-context.ts +224 -0
- package/src/mcp/tools/get-symbol-hotspots.ts +54 -0
- package/src/mcp/tools/get-symbol-impact.ts +64 -0
- package/src/mcp/tools/get-symbol.ts +175 -0
- package/src/mcp/tools/locate-symbol.ts +196 -0
- package/src/mcp/tools/register-domain.ts +75 -0
- package/src/mcp/tools/register-feature.ts +110 -0
- package/src/mcp/tools/slice.ts +16 -0
- package/src/mcp/tools/symbol.ts +93 -0
- package/src/mcp/tools/trace-vertical-slice.ts +80 -0
- package/src/mcp/workspace-manager.ts +159 -0
- package/src/mcp/workspace-watcher.ts +202 -0
- package/src/types/index.ts +456 -0
- package/src/types/wasm.d.ts +4 -0
- package/src/utils/version-check.ts +209 -0
- package/src/version.ts +1 -0
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import * as path from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import Parser from "web-tree-sitter";
|
|
5
|
+
import { WASM_ASSETS } from "./wasm-assets.ts";
|
|
6
|
+
import { grammarFileForKey } from "./language-capabilities.ts";
|
|
7
|
+
import type { ParserDiagnostic, ParsedFileAST } from "../../types/index.ts";
|
|
8
|
+
|
|
9
|
+
let initPromise: Promise<boolean> | null = null;
|
|
10
|
+
let sharedParser: Parser | null = null;
|
|
11
|
+
const languageCache = new Map<string, Parser.Language>();
|
|
12
|
+
const languageLoadingPromises = new Map<string, Promise<LanguageResult>>();
|
|
13
|
+
|
|
14
|
+
function getModuleDir(): string {
|
|
15
|
+
if (typeof import.meta.dir === "string") {
|
|
16
|
+
return import.meta.dir;
|
|
17
|
+
}
|
|
18
|
+
try {
|
|
19
|
+
return path.dirname(fileURLToPath(import.meta.url));
|
|
20
|
+
} catch {
|
|
21
|
+
return process.cwd();
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Deterministic 4-tier WASM path resolver for both source execution and compiled binaries.
|
|
27
|
+
*/
|
|
28
|
+
export function resolveWasmPath(fileName: string, isCoreRuntime: boolean = false): string | null {
|
|
29
|
+
const candidates: string[] = [];
|
|
30
|
+
const relativeNodeModulesPath = isCoreRuntime
|
|
31
|
+
? path.join("node_modules", "web-tree-sitter", fileName)
|
|
32
|
+
: path.join("node_modules", "tree-sitter-wasms", "out", fileName);
|
|
33
|
+
|
|
34
|
+
// Tier 1: Explicit environment variable override
|
|
35
|
+
const envDir = process.env.SEPTUM_WASM_DIR;
|
|
36
|
+
if (envDir) {
|
|
37
|
+
candidates.push(path.join(envDir, fileName));
|
|
38
|
+
candidates.push(path.join(envDir, "out", fileName));
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (WASM_ASSETS[fileName]) candidates.push(WASM_ASSETS[fileName]);
|
|
42
|
+
|
|
43
|
+
// Tier 2: Walk up from import.meta.dir to package root
|
|
44
|
+
let currentDir = getModuleDir();
|
|
45
|
+
for (let depth = 0; depth < 7; depth++) {
|
|
46
|
+
candidates.push(path.join(currentDir, relativeNodeModulesPath));
|
|
47
|
+
candidates.push(path.join(currentDir, "wasm", fileName));
|
|
48
|
+
const parent = path.dirname(currentDir);
|
|
49
|
+
if (parent === currentDir) break;
|
|
50
|
+
currentDir = parent;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Tier 3: Relative to process.execPath (for `bun build --compile` standalone binaries)
|
|
54
|
+
if (process.execPath) {
|
|
55
|
+
const execDir = path.dirname(process.execPath);
|
|
56
|
+
candidates.push(path.join(execDir, "wasm", fileName));
|
|
57
|
+
candidates.push(path.join(execDir, fileName));
|
|
58
|
+
candidates.push(path.join(execDir, "..", "wasm", fileName));
|
|
59
|
+
candidates.push(path.join(execDir, relativeNodeModulesPath));
|
|
60
|
+
candidates.push(path.join(execDir, "..", relativeNodeModulesPath));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Tier 4: Fallback to process.cwd()/node_modules/...
|
|
64
|
+
const cwd = process.cwd();
|
|
65
|
+
candidates.push(path.join(cwd, relativeNodeModulesPath));
|
|
66
|
+
candidates.push(path.join(cwd, "wasm", fileName));
|
|
67
|
+
|
|
68
|
+
for (const candidate of candidates) {
|
|
69
|
+
try {
|
|
70
|
+
if (fs.existsSync(candidate)) {
|
|
71
|
+
return candidate;
|
|
72
|
+
}
|
|
73
|
+
} catch {
|
|
74
|
+
// Ignore permission or stat errors on intermediate paths
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Resolves a language key or file path/extension to its WASM grammar filename.
|
|
83
|
+
*/
|
|
84
|
+
export function resolveGrammarFileName(langKeyOrPath: string): string | null {
|
|
85
|
+
return grammarFileForKey(langKeyOrPath);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Initializes the core web-tree-sitter runtime once as a singleton.
|
|
90
|
+
*/
|
|
91
|
+
export async function ensureTreeSitterInitialized(): Promise<boolean> {
|
|
92
|
+
if (initPromise) {
|
|
93
|
+
return initPromise;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
initPromise = (async () => {
|
|
97
|
+
try {
|
|
98
|
+
const coreWasmPath = resolveWasmPath("tree-sitter.wasm", true);
|
|
99
|
+
if (!coreWasmPath) {
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
await Parser.init({
|
|
103
|
+
locateFile: () => coreWasmPath,
|
|
104
|
+
});
|
|
105
|
+
if (!sharedParser) {
|
|
106
|
+
sharedParser = new Parser();
|
|
107
|
+
}
|
|
108
|
+
return true;
|
|
109
|
+
} catch {
|
|
110
|
+
initPromise = null;
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
})();
|
|
114
|
+
|
|
115
|
+
return initPromise;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Loads and caches a Tree-Sitter Language grammar from its WASM file.
|
|
120
|
+
*/
|
|
121
|
+
export async function loadTreeSitterLanguage(langKeyOrPath: string): Promise<Parser.Language | null> {
|
|
122
|
+
const result = await loadLanguageResult(langKeyOrPath);
|
|
123
|
+
return "language" in result ? result.language : null;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
type LanguageResult = { language: Parser.Language } | { error: ParserDiagnostic };
|
|
127
|
+
async function loadLanguageResult(langKeyOrPath: string): Promise<LanguageResult> {
|
|
128
|
+
const wasmFileName = resolveGrammarFileName(langKeyOrPath);
|
|
129
|
+
if (!wasmFileName) {
|
|
130
|
+
return { error: { code: "grammar_unsupported", message: `No grammar mapped for '${langKeyOrPath}'.` } };
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const cached = languageCache.get(wasmFileName);
|
|
134
|
+
if (cached) {
|
|
135
|
+
return { language: cached };
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const inFlight = languageLoadingPromises.get(wasmFileName);
|
|
139
|
+
if (inFlight) {
|
|
140
|
+
return inFlight;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const loadPromise = (async (): Promise<LanguageResult> => {
|
|
144
|
+
try {
|
|
145
|
+
const initialized = await ensureTreeSitterInitialized();
|
|
146
|
+
if (!initialized) {
|
|
147
|
+
return { error: { code: "runtime_unavailable", message: "Tree-sitter runtime initialization failed." } };
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const wasmPath = resolveWasmPath(wasmFileName, false);
|
|
151
|
+
if (!wasmPath) {
|
|
152
|
+
return { error: { code: "grammar_missing", message: `Grammar asset not found: ${wasmFileName}` } };
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const wasmBytes = fs.readFileSync(wasmPath);
|
|
156
|
+
const lang = await Parser.Language.load(wasmBytes);
|
|
157
|
+
languageCache.set(wasmFileName, lang);
|
|
158
|
+
return { language: lang };
|
|
159
|
+
} catch {
|
|
160
|
+
return { error: { code: "grammar_invalid", message: `Cannot load grammar asset: ${wasmFileName}` } };
|
|
161
|
+
} finally {
|
|
162
|
+
languageLoadingPromises.delete(wasmFileName);
|
|
163
|
+
}
|
|
164
|
+
})();
|
|
165
|
+
|
|
166
|
+
languageLoadingPromises.set(wasmFileName, loadPromise);
|
|
167
|
+
return loadPromise;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Parses source content using web-tree-sitter and executes a visitor over the root SyntaxNode.
|
|
172
|
+
* Releases the parsed tree inside `finally`, including when the visitor throws.
|
|
173
|
+
*/
|
|
174
|
+
export async function parseWithTreeSitter<T>(
|
|
175
|
+
langKey: string,
|
|
176
|
+
content: string,
|
|
177
|
+
visitor: (rootNode: Parser.SyntaxNode) => T,
|
|
178
|
+
onFailure?: (diagnostic: ParserDiagnostic) => void
|
|
179
|
+
): Promise<T | null> {
|
|
180
|
+
const loaded = await loadLanguageResult(langKey);
|
|
181
|
+
if ("error" in loaded) {
|
|
182
|
+
onFailure?.(loaded.error);
|
|
183
|
+
return null;
|
|
184
|
+
}
|
|
185
|
+
if (!sharedParser) {
|
|
186
|
+
onFailure?.({ code: "runtime_unavailable", message: "Tree-sitter parser unavailable." });
|
|
187
|
+
return null;
|
|
188
|
+
}
|
|
189
|
+
const language = loaded.language;
|
|
190
|
+
|
|
191
|
+
let tree: Parser.Tree | null = null;
|
|
192
|
+
try {
|
|
193
|
+
sharedParser.setLanguage(language);
|
|
194
|
+
tree = sharedParser.parse(content);
|
|
195
|
+
if (!tree) {
|
|
196
|
+
onFailure?.({ code: "parse_failed", message: "Tree-sitter returned no tree." });
|
|
197
|
+
return null;
|
|
198
|
+
}
|
|
199
|
+
try {
|
|
200
|
+
return visitor(tree.rootNode);
|
|
201
|
+
} catch {
|
|
202
|
+
onFailure?.({ code: "extraction_failed", message: "AST visitor failed." });
|
|
203
|
+
return null;
|
|
204
|
+
}
|
|
205
|
+
} catch {
|
|
206
|
+
onFailure?.({ code: "parse_failed", message: "Tree-sitter parsing failed." });
|
|
207
|
+
return null;
|
|
208
|
+
} finally {
|
|
209
|
+
if (tree) {
|
|
210
|
+
tree.delete();
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
export async function parseSourceWithTreeSitter(
|
|
216
|
+
langKey: string,
|
|
217
|
+
content: string,
|
|
218
|
+
visitor: (rootNode: Parser.SyntaxNode) => ParsedFileAST,
|
|
219
|
+
fallback?: () => ParsedFileAST
|
|
220
|
+
): Promise<ParsedFileAST | null> {
|
|
221
|
+
let failure: ParserDiagnostic | undefined;
|
|
222
|
+
const result = await parseWithTreeSitter(langKey, content, rootNode => ({
|
|
223
|
+
...visitor(rootNode),
|
|
224
|
+
status: rootNode.hasError ? "degraded" as const : "parsed" as const,
|
|
225
|
+
extraction: { method: "tree-sitter" as const, coverage: "partial" as const },
|
|
226
|
+
diagnostics: rootNode.hasError ? [{ code: "syntax_recovery" as const, message: "Grammar recovered from syntax errors; extraction may be incomplete." }] : [],
|
|
227
|
+
}), diagnostic => { failure = diagnostic; });
|
|
228
|
+
if (result || !fallback) return result;
|
|
229
|
+
return {
|
|
230
|
+
...fallback(), status: "degraded",
|
|
231
|
+
extraction: { method: "fallback", coverage: "partial" },
|
|
232
|
+
diagnostics: failure ? [failure] : [],
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
export function getNodeHeader(node: Parser.SyntaxNode): string {
|
|
237
|
+
const body = node.childForFieldName("body");
|
|
238
|
+
const end = body ?? node.children.findLast(child => child.type === "end");
|
|
239
|
+
return (end ? node.text.slice(0, end.startIndex - node.startIndex) : node.text)
|
|
240
|
+
.trim().replace(/;$/, "");
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Computes maximum control-flow nesting depth directly from a Tree-Sitter function/method AST node.
|
|
245
|
+
* Base depth for a function/method body is 1.
|
|
246
|
+
*/
|
|
247
|
+
export function calculateAstControlFlowNesting(
|
|
248
|
+
rootNode: Parser.SyntaxNode,
|
|
249
|
+
controlNodeTypes: ReadonlySet<string>,
|
|
250
|
+
boundaryNodeTypes?: ReadonlySet<string>
|
|
251
|
+
): number {
|
|
252
|
+
let maxDepth = 1;
|
|
253
|
+
|
|
254
|
+
function walk(node: Parser.SyntaxNode, currentDepth: number, isRoot: boolean): void {
|
|
255
|
+
if (!isRoot && boundaryNodeTypes && boundaryNodeTypes.has(node.type)) {
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
let nextDepth = currentDepth;
|
|
260
|
+
if (!isRoot && controlNodeTypes.has(node.type)) {
|
|
261
|
+
nextDepth = currentDepth + 1;
|
|
262
|
+
if (nextDepth > maxDepth) {
|
|
263
|
+
maxDepth = nextDepth;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
for (const child of node.namedChildren) {
|
|
268
|
+
walk(child, nextDepth, false);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
walk(rootNode, 1, true);
|
|
273
|
+
return maxDepth;
|
|
274
|
+
}
|