@dimassetoid/septum-mcp 0.2.1 → 0.3.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/README.md +46 -28
- package/package.json +4 -4
- package/src/cli/commands/check.ts +27 -16
- package/src/cli/commands/hook.ts +38 -101
- package/src/cli/commands/init.ts +26 -9
- package/src/cli/commands/sync.ts +4 -6
- package/src/cli/hook-payload.ts +43 -0
- package/src/cli/index.ts +9 -52
- package/src/cli/scaffold/harness-config.ts +48 -8
- package/src/cli/templates/hooks.ts +49 -14
- package/src/cli/templates/rules.ts +34 -49
- package/src/cli/templates/skill.ts +39 -35
- package/src/core/boundary/evaluator.ts +67 -83
- package/src/core/boundary/staged-auditor.ts +102 -40
- package/src/core/config/domain-ownership.ts +45 -0
- package/src/core/config/loader.ts +81 -29
- package/src/core/config/schema.ts +0 -10
- package/src/core/database/repositories/dependency.repository.ts +169 -148
- package/src/core/database/repositories/domain.repository.ts +46 -3
- package/src/core/database/repositories/symbol.repository.ts +226 -87
- package/src/core/database/repository.ts +73 -18
- package/src/core/database/schema.ts +2 -33
- package/src/core/discovery/navigation.ts +105 -0
- package/src/core/discovery/topology-detector.ts +60 -2
- package/src/core/ingestion/pipeline.ts +96 -129
- package/src/core/parser/behavioral-analyzer.ts +26 -22
- package/src/core/parser/boundary-tracker.ts +15 -0
- package/src/core/parser/extractors/base.ts +7 -3
- package/src/core/parser/extractors/csharp.ts +7 -3
- package/src/core/parser/extractors/django-semantic.ts +4 -36
- package/src/core/parser/extractors/elysia-semantic.ts +3 -24
- package/src/core/parser/extractors/express-semantic.ts +3 -18
- package/src/core/parser/extractors/fastapi-semantic.ts +19 -53
- package/src/core/parser/extractors/fastify-semantic.ts +3 -18
- package/src/core/parser/extractors/flask-semantic.ts +22 -55
- package/src/core/parser/extractors/go-semantic.ts +92 -33
- package/src/core/parser/extractors/hono-semantic.ts +3 -24
- package/src/core/parser/extractors/java.ts +5 -3
- package/src/core/parser/extractors/laravel-semantic.ts +44 -16
- package/src/core/parser/extractors/mcp-cli-semantic.ts +6 -7
- package/src/core/parser/extractors/nestjs-semantic.ts +3 -19
- package/src/core/parser/extractors/php.ts +16 -0
- package/src/core/parser/extractors/python.ts +9 -4
- package/src/core/parser/extractors/registry.ts +26 -5
- package/src/core/parser/extractors/ruby.ts +8 -5
- package/src/core/parser/extractors/rust.ts +31 -13
- package/src/core/parser/extractors/semantic-extractor.interface.ts +3 -1
- package/src/core/parser/extractors/symfony-semantic.ts +4 -37
- package/src/core/parser/extractors/typescript.ts +14 -1
- package/src/core/parser/implementation-fingerprint.ts +97 -0
- package/src/core/parser/language-capabilities.ts +23 -7
- package/src/core/parser/schema/providers/laravel-migration-provider.ts +73 -50
- package/src/core/parser/schema/schema-provider-registry.ts +1 -3
- package/src/core/parser/schema/schema-provider.interface.ts +2 -1
- package/src/core/parser/semantic-revision.ts +3 -0
- package/src/core/parser/static-string.ts +51 -0
- package/src/core/resolver/call-graph-tracer.ts +337 -28
- package/src/core/resolver/import-resolver.ts +107 -179
- package/src/core/resolver/interpretation-inputs.ts +7 -0
- package/src/core/resolver/languages/go.ts +103 -0
- package/src/core/resolver/languages/managed.ts +200 -0
- package/src/core/resolver/languages/python.ts +115 -0
- package/src/core/resolver/languages/ruby.ts +40 -0
- package/src/core/resolver/languages/rust.ts +127 -0
- package/src/core/resolver/languages/shared.ts +71 -0
- package/src/core/resolver/module-resolver.ts +8 -4
- package/src/core/resolver/slice-catalog.ts +79 -0
- package/src/core/resolver/slice-format.ts +9 -0
- package/src/core/resolver/symbol-impact.ts +381 -0
- package/src/core/resolver/vertical-slice-tracer.ts +93 -115
- package/src/core/resources/ignore.ts +22 -1
- package/src/core/resources/source-files.ts +143 -20
- package/src/core/resources/source-inventory.ts +55 -0
- package/src/core/retrieval/context-page.ts +109 -0
- package/src/core/retrieval/context.ts +320 -0
- package/src/core/retrieval/resolution-warnings.ts +21 -0
- package/src/core/retrieval/symbol-evidence.ts +97 -0
- package/src/core/retrieval/typescript-references.ts +262 -0
- package/src/core/telemetry/telemetry.ts +54 -9
- package/src/index.ts +0 -3
- package/src/mcp/schemas.ts +61 -420
- package/src/mcp/server.ts +116 -290
- package/src/mcp/tools/boundary.ts +2 -2
- package/src/mcp/tools/check-boundary.ts +55 -60
- package/src/mcp/tools/get-domain-catalog.ts +32 -219
- package/src/mcp/tools/get-symbol-impact.ts +45 -5
- package/src/mcp/tools/get-symbol.ts +227 -3
- package/src/mcp/tools/locate-symbol.ts +54 -14
- package/src/mcp/tools/{domain.ts → map.ts} +4 -18
- package/src/mcp/tools/register-domain.ts +41 -3
- package/src/mcp/tools/symbol.ts +23 -21
- package/src/mcp/tools/trace-vertical-slice.ts +36 -10
- package/src/mcp/workspace-manager.ts +15 -4
- package/src/mcp/workspace-watcher.ts +8 -8
- package/src/types/index.ts +35 -53
- package/src/cli/commands/topology.ts +0 -161
- package/src/core/database/repositories/environment.repository.ts +0 -207
- package/src/core/parser/schema/providers/prisma-schema-provider.ts +0 -90
- package/src/core/session/session-manager.ts +0 -107
- package/src/core/topology/environment-detector.ts +0 -973
- package/src/core/topology/types.ts +0 -83
- package/src/mcp/tools/feature.ts +0 -67
- package/src/mcp/tools/get-environment-topology.ts +0 -138
- package/src/mcp/tools/get-feature-context.ts +0 -224
- package/src/mcp/tools/get-symbol-hotspots.ts +0 -54
- package/src/mcp/tools/register-feature.ts +0 -110
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { closeSync, existsSync, openSync, readFileSync, readSync, readdirSync, realpathSync, statSync } from "node:fs";
|
|
3
|
+
import { dirname, join, relative, resolve } from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { WASM_ASSETS } from "./wasm-assets.ts";
|
|
6
|
+
|
|
7
|
+
/** Content identity, independent of checkout paths, mtimes and release counters. */
|
|
8
|
+
export function fingerprintImplementation(options: {
|
|
9
|
+
sourceRoot?: string;
|
|
10
|
+
executablePath?: string;
|
|
11
|
+
assets?: Record<string, string>;
|
|
12
|
+
}): string {
|
|
13
|
+
const hash = createHash("sha256");
|
|
14
|
+
const chunk = Buffer.allocUnsafe(256 * 1024);
|
|
15
|
+
const addFile = (label: string, file: string): void => {
|
|
16
|
+
const expectedSize = statSync(file).size;
|
|
17
|
+
hash.update(JSON.stringify([label, expectedSize]));
|
|
18
|
+
// Bound memory while hashing standalone executables, which include the Bun runtime.
|
|
19
|
+
const fd = openSync(file, "r");
|
|
20
|
+
try {
|
|
21
|
+
let total = 0;
|
|
22
|
+
for (;;) {
|
|
23
|
+
const size = readSync(fd, chunk, 0, chunk.length, null);
|
|
24
|
+
if (!size) break;
|
|
25
|
+
hash.update(chunk.subarray(0, size));
|
|
26
|
+
total += size;
|
|
27
|
+
}
|
|
28
|
+
if (total !== expectedSize) throw new Error(`Implementation changed while fingerprinting '${label}'. Restart Septum.`);
|
|
29
|
+
} finally { closeSync(fd); }
|
|
30
|
+
hash.update("\0");
|
|
31
|
+
};
|
|
32
|
+
if (options.executablePath) {
|
|
33
|
+
addFile("executable", options.executablePath);
|
|
34
|
+
} else if (options.sourceRoot) {
|
|
35
|
+
const root = resolve(options.sourceRoot);
|
|
36
|
+
const ancestors = new Set<string>();
|
|
37
|
+
const visit = (directory: string, base = root, prefix = ""): void => {
|
|
38
|
+
const identity = realpathSync(directory);
|
|
39
|
+
if (ancestors.has(identity)) throw new Error(`Implementation directory cycle at '${directory}'.`);
|
|
40
|
+
ancestors.add(identity);
|
|
41
|
+
try {
|
|
42
|
+
for (const entry of readdirSync(directory, { withFileTypes: true }).sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0)) {
|
|
43
|
+
const file = join(directory, entry.name);
|
|
44
|
+
const info = entry.isSymbolicLink() ? statSync(file) : entry;
|
|
45
|
+
if (info.isDirectory()) visit(file, base, prefix);
|
|
46
|
+
else if (info.isFile()) addFile([prefix, relative(base, file).replace(/\\/g, "/")].filter(Boolean).join("/"), file);
|
|
47
|
+
}
|
|
48
|
+
} finally { ancestors.delete(identity); }
|
|
49
|
+
};
|
|
50
|
+
visit(join(root, "src"));
|
|
51
|
+
visit(join(root, "bin"));
|
|
52
|
+
for (const name of ["package.json", "bun.lock", "bun.lockb"]) {
|
|
53
|
+
const file = join(root, name);
|
|
54
|
+
if (existsSync(file)) addFile(name, file);
|
|
55
|
+
}
|
|
56
|
+
// Include installed bytes: local dependency patches can change analysis
|
|
57
|
+
// without changing a package manifest or lockfile.
|
|
58
|
+
const manifest = JSON.parse(readFileSync(join(root, "package.json"), "utf8"));
|
|
59
|
+
for (const name of Object.keys(manifest.dependencies ?? {}).sort()) {
|
|
60
|
+
let directory = root;
|
|
61
|
+
while (!existsSync(join(directory, "node_modules", name, "package.json"))) {
|
|
62
|
+
const parent = dirname(directory);
|
|
63
|
+
if (parent === directory) throw new Error(`Cannot fingerprint installed dependency '${name}'.`);
|
|
64
|
+
directory = parent;
|
|
65
|
+
}
|
|
66
|
+
const packageRoot = join(directory, "node_modules", name);
|
|
67
|
+
visit(packageRoot, packageRoot, `dependency:${name}`);
|
|
68
|
+
}
|
|
69
|
+
} else {
|
|
70
|
+
throw new Error("Implementation fingerprint requires sourceRoot or executablePath.");
|
|
71
|
+
}
|
|
72
|
+
for (const [name, file] of Object.entries(options.assets ?? {}).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0)) {
|
|
73
|
+
// Embedded WASM paths support readFileSync in compiled Bun executables.
|
|
74
|
+
hash.update(JSON.stringify(["asset", name]));
|
|
75
|
+
hash.update(readFileSync(file));
|
|
76
|
+
hash.update("\0");
|
|
77
|
+
}
|
|
78
|
+
hash.update(JSON.stringify(["runtime", Bun.version, process.platform, process.arch]));
|
|
79
|
+
return `sha256:${hash.digest("hex")}`;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const standalone = typeof Bun.isStandaloneExecutable === "boolean"
|
|
83
|
+
? Bun.isStandaloneExecutable : Bun.embeddedFiles.length > 0;
|
|
84
|
+
const assets = Object.fromEntries(Object.entries(WASM_ASSETS).map(([name, embedded]) => {
|
|
85
|
+
const override = process.env.SEPTUM_WASM_DIR;
|
|
86
|
+
const candidates = override ? [join(override, name), join(override, "out", name), embedded] : [embedded];
|
|
87
|
+
return [name, candidates.find(file => existsSync(file)) ?? embedded];
|
|
88
|
+
}));
|
|
89
|
+
|
|
90
|
+
// One identity per loaded process. A daemon must restart to load changed implementation code.
|
|
91
|
+
export const ANALYSIS_FINGERPRINT = fingerprintImplementation(standalone ? {
|
|
92
|
+
executablePath: process.platform === "linux" ? "/proc/self/exe" : process.execPath,
|
|
93
|
+
assets,
|
|
94
|
+
} : {
|
|
95
|
+
sourceRoot: resolve(dirname(fileURLToPath(import.meta.url)), "../../.."),
|
|
96
|
+
assets,
|
|
97
|
+
});
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import * as path from "node:path";
|
|
2
2
|
import grammarPackage from "tree-sitter-wasms/package.json";
|
|
3
|
+
import { ANALYSIS_FINGERPRINT } from "./implementation-fingerprint.ts";
|
|
3
4
|
|
|
4
|
-
|
|
5
|
-
export const PARSER_REVISION = "4";
|
|
5
|
+
export const PARSER_FINGERPRINT = ANALYSIS_FINGERPRINT;
|
|
6
6
|
|
|
7
7
|
type LanguageDefinition = {
|
|
8
8
|
id: string;
|
|
@@ -22,10 +22,10 @@ export const LANGUAGE_CAPABILITIES: readonly LanguageDefinition[] = [
|
|
|
22
22
|
{ id: "php", extensions: [".php"], aliases: [], extractor: "php", method: "tree-sitter", grammar: "php", limits: "Selected declarations/imports; partial PSR-4/domain resolution, no runtime reflection." },
|
|
23
23
|
{ id: "python", extensions: [".py"], aliases: ["py"], extractor: "python", method: "tree-sitter", grammar: "python", limits: "Selected declarations/imports; package resolution and dynamic attributes incomplete." },
|
|
24
24
|
{ id: "go", extensions: [".go"], aliases: ["golang"], extractor: "go", method: "tree-sitter", grammar: "go", limits: "Selected declarations/imports; no complete module/package or receiver resolution." },
|
|
25
|
-
{ id: "rust", extensions: [".rs"], aliases: ["rs"], extractor: "rust", method: "tree-sitter", grammar: "rust", limits: "
|
|
26
|
-
{ id: "java", extensions: [".java"], aliases: [], extractor: "java", method: "tree-sitter", grammar: "java", limits: "
|
|
27
|
-
{ id: "csharp", extensions: [".cs"], aliases: ["cs", "c_sharp"], extractor: "csharp", method: "tree-sitter", grammar: "c_sharp", limits: "
|
|
28
|
-
{ id: "ruby", extensions: [".rb"], aliases: ["rb"], extractor: "ruby", method: "tree-sitter", grammar: "ruby", limits: "
|
|
25
|
+
{ id: "rust", extensions: [".rs"], aliases: ["rs"], extractor: "rust", method: "tree-sitter", grammar: "rust", limits: "Literal Cargo/module/path dependency resolution; cfg remains conditional, no macro expansion or trait dispatch proof." },
|
|
26
|
+
{ id: "java", extensions: [".java"], aliases: [], extractor: "java", method: "tree-sitter", grammar: "java", limits: "Local source types/packages and literal Maven/Gradle project roots; no complete binary classpath or build execution." },
|
|
27
|
+
{ id: "csharp", extensions: [".cs"], aliases: ["cs", "c_sharp"], extractor: "csharp", method: "tree-sitter", grammar: "c_sharp", limits: "Local namespaces/types and literal project references; conditional MSBuild inputs remain uncertain, no assembly namespace inference from package names." },
|
|
28
|
+
{ id: "ruby", extensions: [".rb"], aliases: ["rb"], extractor: "ruby", method: "tree-sitter", grammar: "ruby", limits: "Literal require_relative, conventional lib roots and declared gems; no dynamic load-path, Zeitwerk configuration or metaprogramming execution." },
|
|
29
29
|
{ id: "frontend", extensions: [".vue", ".astro", ".svelte"], aliases: [], extractor: "frontend", method: "embedded-script", limits: "Scans script/frontmatter as TypeScript; no whole-template AST validation." },
|
|
30
30
|
{ id: "kotlin", extensions: [".kt"], aliases: [], extractor: null, method: "unsupported", limits: "Recognized for unsupported-file diagnostics; no registered grammar or extractor." },
|
|
31
31
|
{ id: "scala", extensions: [".scala"], aliases: [], extractor: null, method: "unsupported", limits: "Recognized only; no registered grammar or extractor." },
|
|
@@ -37,6 +37,21 @@ export function getLanguageCapability(filePath: string): LanguageDefinition | un
|
|
|
37
37
|
return LANGUAGE_CAPABILITIES.find(language => language.extensions.some(ext => lower.endsWith(ext)));
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
+
export function getOperationCapabilities(filePath: string) {
|
|
41
|
+
const language = getLanguageCapability(filePath);
|
|
42
|
+
const declarations = language?.extractor ? "partial" : "unsupported";
|
|
43
|
+
return {
|
|
44
|
+
language: language?.id ?? "unknown",
|
|
45
|
+
declarations,
|
|
46
|
+
import_resolution: language && ["typescript", "javascript", "php", "python", "go", "rust", "java", "csharp", "ruby", "frontend"].includes(language.id) ? "partial" : "unsupported",
|
|
47
|
+
source_context: declarations,
|
|
48
|
+
deep_context: /\.(?:ts|mts|cts)$/.test(filePath) ? "bounded_references" : "unsupported",
|
|
49
|
+
caller_analysis: language?.extractor ? "lexical_heuristic" : "unsupported",
|
|
50
|
+
runtime_dispatch: "unverified",
|
|
51
|
+
limits: language?.limits ?? "No registered language extractor.",
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
40
55
|
export function grammarFileForKey(keyOrPath: string): string | null {
|
|
41
56
|
const key = keyOrPath.trim().toLowerCase();
|
|
42
57
|
if (key.endsWith(".wasm")) return key;
|
|
@@ -49,7 +64,7 @@ export function grammarFileForKey(keyOrPath: string): string | null {
|
|
|
49
64
|
|
|
50
65
|
export function getParserCapabilities(full: boolean) {
|
|
51
66
|
const common = {
|
|
52
|
-
parser_revision:
|
|
67
|
+
parser_revision: PARSER_FINGERPRINT,
|
|
53
68
|
grammar_package: `${grammarPackage.name}@${grammarPackage.version}`,
|
|
54
69
|
coverage: "partial",
|
|
55
70
|
status_contract: "parsed = parser/scanner completed; degraded = recovery/fallback; unsupported = no extractor. None implies complete extraction or runtime correctness.",
|
|
@@ -61,6 +76,7 @@ export function getParserCapabilities(full: boolean) {
|
|
|
61
76
|
coverage: l.extractor ? "partial" : "none",
|
|
62
77
|
grammars: [...new Set(l.extensions.map(grammarFileForKey).filter(Boolean))],
|
|
63
78
|
limits: l.limits,
|
|
79
|
+
operations: getOperationCapabilities(`source${l.extensions[0]}`),
|
|
64
80
|
evidence: l.method === "tree-sitter" ? "tests/unit/language-capabilities.test.ts; tests/unit/parser-recovery.test.ts" : "tests/unit/language-capabilities.test.ts",
|
|
65
81
|
})),
|
|
66
82
|
} : {
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { blankCommentsAndStrings } from "../../boundary-tracker.ts";
|
|
1
2
|
import { readOptionalSourceFile } from "../../../resources/source-files.ts";
|
|
2
3
|
import * as fs from "node:fs";
|
|
3
4
|
import * as path from "node:path";
|
|
@@ -27,23 +28,26 @@ export class LaravelMigrationSchemaProvider implements StackSchemaProvider {
|
|
|
27
28
|
const tableName = this.resolveTableName(projectRoot, entitySymbol, entityFilePath);
|
|
28
29
|
if (!tableName) return null;
|
|
29
30
|
|
|
30
|
-
const migrationFiles = this.
|
|
31
|
+
const migrationFiles = this.findMigrationFiles(migrationsDir);
|
|
31
32
|
if (migrationFiles.length === 0) return null;
|
|
32
33
|
|
|
33
34
|
const fieldsMap = new Map<string, SchemaFieldSummary>();
|
|
34
|
-
let primarySourceFile
|
|
35
|
+
let primarySourceFile: string | undefined;
|
|
35
36
|
|
|
36
37
|
for (const migFile of migrationFiles) {
|
|
37
38
|
const fullPath = path.join(migrationsDir, migFile);
|
|
38
39
|
try {
|
|
39
40
|
const content = readOptionalSourceFile(fullPath, "utf-8");
|
|
40
|
-
|
|
41
|
+
const before = JSON.stringify([...fieldsMap]);
|
|
42
|
+
if (!this.extractColumnsFromMigration(content, tableName, fieldsMap)) return null;
|
|
43
|
+
if (JSON.stringify([...fieldsMap]) !== before) primarySourceFile = migFile;
|
|
41
44
|
} catch {
|
|
42
|
-
//
|
|
45
|
+
// An unreadable migration can change the summary; do not certify older fields.
|
|
46
|
+
return null;
|
|
43
47
|
}
|
|
44
48
|
}
|
|
45
49
|
|
|
46
|
-
if (fieldsMap.size === 0) return null;
|
|
50
|
+
if (fieldsMap.size === 0 || !primarySourceFile) return null;
|
|
47
51
|
|
|
48
52
|
const relSourceFile = path.relative(projectRoot, path.join(migrationsDir, primarySourceFile));
|
|
49
53
|
|
|
@@ -52,6 +56,7 @@ export class LaravelMigrationSchemaProvider implements StackSchemaProvider {
|
|
|
52
56
|
tableName,
|
|
53
57
|
schemaSourceFile: relSourceFile,
|
|
54
58
|
fields: Array.from(fieldsMap.values()),
|
|
59
|
+
interpretation: "static_migration_summary",
|
|
55
60
|
};
|
|
56
61
|
}
|
|
57
62
|
|
|
@@ -68,10 +73,11 @@ export class LaravelMigrationSchemaProvider implements StackSchemaProvider {
|
|
|
68
73
|
if (fs.existsSync(fullEntityPath)) {
|
|
69
74
|
try {
|
|
70
75
|
const content = readOptionalSourceFile(fullEntityPath, "utf-8");
|
|
71
|
-
const
|
|
72
|
-
|
|
73
|
-
return tableMatch[1]
|
|
76
|
+
const mask = blankCommentsAndStrings(content);
|
|
77
|
+
for (const tableMatch of content.matchAll(/protected\s+\$table\s*=\s*['"]([^'"]+)['"]/g)) {
|
|
78
|
+
if (/protected\s+\$table\s*=/.test(mask.slice(tableMatch.index, tableMatch.index + tableMatch[0].length))) return tableMatch[1]!;
|
|
74
79
|
}
|
|
80
|
+
if (/protected\s+\$table\s*=/.test(mask)) return "";
|
|
75
81
|
} catch {}
|
|
76
82
|
}
|
|
77
83
|
}
|
|
@@ -97,55 +103,71 @@ export class LaravelMigrationSchemaProvider implements StackSchemaProvider {
|
|
|
97
103
|
return `${snake}s`;
|
|
98
104
|
}
|
|
99
105
|
|
|
100
|
-
private
|
|
101
|
-
|
|
102
|
-
const matched: string[] = [];
|
|
103
|
-
|
|
104
|
-
// Priority 1: *_create_${tableName}_table.php
|
|
105
|
-
const createTarget = `_create_${tableName}_table.php`;
|
|
106
|
-
for (const f of files) {
|
|
107
|
-
if (f.endsWith(createTarget)) {
|
|
108
|
-
matched.push(f);
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
// Priority 2: Other migration files altering this table
|
|
113
|
-
for (const f of files) {
|
|
114
|
-
if (matched.includes(f)) continue;
|
|
115
|
-
if (f.includes(`_${tableName}_`)) {
|
|
116
|
-
matched.push(f);
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
// If still not found by filename, do a lightweight content search
|
|
121
|
-
if (matched.length === 0) {
|
|
122
|
-
for (const f of files) {
|
|
123
|
-
try {
|
|
124
|
-
const content = readOptionalSourceFile(path.join(migrationsDir, f), "utf-8");
|
|
125
|
-
if (
|
|
126
|
-
content.includes(`Schema::create('${tableName}'`) ||
|
|
127
|
-
content.includes(`Schema::create("${tableName}"`) ||
|
|
128
|
-
content.includes(`Schema::table('${tableName}'`) ||
|
|
129
|
-
content.includes(`Schema::table("${tableName}"`)
|
|
130
|
-
) {
|
|
131
|
-
matched.push(f);
|
|
132
|
-
}
|
|
133
|
-
} catch {}
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
return matched;
|
|
106
|
+
private findMigrationFiles(migrationsDir: string): string[] {
|
|
107
|
+
return fs.readdirSync(migrationsDir).filter(file => file.endsWith(".php")).sort();
|
|
138
108
|
}
|
|
139
109
|
|
|
140
110
|
private extractColumnsFromMigration(
|
|
141
111
|
content: string,
|
|
142
112
|
tableName: string,
|
|
143
113
|
fieldsMap: Map<string, SchemaFieldSummary>
|
|
144
|
-
):
|
|
145
|
-
const
|
|
146
|
-
|
|
147
|
-
|
|
114
|
+
): boolean {
|
|
115
|
+
const mask = blankCommentsAndStrings(content, { hashComments: true });
|
|
116
|
+
const blockEnd = (start: number) => {
|
|
117
|
+
let depth = 0;
|
|
118
|
+
for (let i = start; i < mask.length; i++) {
|
|
119
|
+
if (mask[i] === "{") depth++;
|
|
120
|
+
if (mask[i] === "}" && --depth === 0) return i;
|
|
121
|
+
}
|
|
122
|
+
return mask.length;
|
|
123
|
+
};
|
|
124
|
+
const up = /function\s+up\s*\([^)]*\)[^{]*\{/.exec(mask);
|
|
125
|
+
if (!up && /function\s+down\s*\(/.test(mask)) return true;
|
|
126
|
+
const start = up ? up.index + up[0].length : 0;
|
|
127
|
+
const end = up ? blockEnd(start - 1) : mask.length;
|
|
128
|
+
const operations = /Schema::(create|table|dropIfExists|drop|rename)\s*\(/g;
|
|
129
|
+
operations.lastIndex = start;
|
|
130
|
+
let operation: RegExpExecArray | null;
|
|
131
|
+
const bodies: string[] = [];
|
|
132
|
+
while ((operation = operations.exec(mask)) && operation.index < end) {
|
|
133
|
+
const argsStart = operations.lastIndex;
|
|
134
|
+
const literal = /^\s*['"]([^'"]+)['"]/.exec(content.slice(argsStart));
|
|
135
|
+
if (!literal) return false; // Dynamic table names cannot yield a reliable static summary.
|
|
136
|
+
if (operation[1] === "rename") {
|
|
137
|
+
const names = /^\s*['"]([^'"]+)['"]\s*,\s*['"]([^'"]+)['"]/.exec(content.slice(argsStart));
|
|
138
|
+
if (!names || names[1] === tableName || names[2] === tableName) return false;
|
|
139
|
+
}
|
|
140
|
+
if (literal[1] !== tableName) continue;
|
|
141
|
+
const kind = operation[1];
|
|
142
|
+
if (["drop", "dropIfExists", "rename"].includes(kind!)) {
|
|
143
|
+
bodies.push("__reset__");
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
const open = mask.indexOf("{", argsStart);
|
|
147
|
+
const close = open >= 0 ? blockEnd(open) : -1;
|
|
148
|
+
if (open < 0 || close >= end) return false;
|
|
149
|
+
if (kind === "create") bodies.push("__reset__");
|
|
150
|
+
const bodyMask = mask.slice(open + 1, close);
|
|
151
|
+
const body = content.slice(open + 1, close);
|
|
152
|
+
// Copy executable statements only; quoted/comment decoys cannot provide a table call.
|
|
153
|
+
for (const statement of bodyMask.matchAll(/\$table->[^;]+;/g)) {
|
|
154
|
+
bodies.push(body.slice(statement.index!, statement.index! + statement[0].length));
|
|
155
|
+
}
|
|
156
|
+
operations.lastIndex = close + 1;
|
|
157
|
+
}
|
|
158
|
+
for (const rawLine of bodies) {
|
|
148
159
|
const line = rawLine.trim();
|
|
160
|
+
if (line === "__reset__") { fieldsMap.clear(); continue; }
|
|
161
|
+
const drop = /\$table->dropColumn\s*\(\s*['"]([^'"]+)['"]/.exec(line);
|
|
162
|
+
if (drop) { fieldsMap.delete(drop[1]!); continue; }
|
|
163
|
+
const rename = /\$table->renameColumn\s*\(\s*['"]([^'"]+)['"]\s*,\s*['"]([^'"]+)['"]/.exec(line);
|
|
164
|
+
if (rename) {
|
|
165
|
+
const field = fieldsMap.get(rename[1]!);
|
|
166
|
+
fieldsMap.delete(rename[1]!);
|
|
167
|
+
if (field) fieldsMap.set(rename[2]!, { ...field, name: rename[2]! });
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
if (/\$table->(?:dropColumn|renameColumn)\s*\(/.test(line)) return false;
|
|
149
171
|
if (!line.includes("$table->")) continue;
|
|
150
172
|
|
|
151
173
|
// Special shorthand: $table->id();
|
|
@@ -212,5 +234,6 @@ export class LaravelMigrationSchemaProvider implements StackSchemaProvider {
|
|
|
212
234
|
});
|
|
213
235
|
}
|
|
214
236
|
}
|
|
237
|
+
return true;
|
|
215
238
|
}
|
|
216
239
|
}
|
|
@@ -3,12 +3,10 @@ import type {
|
|
|
3
3
|
StackSchemaProvider,
|
|
4
4
|
} from "./schema-provider.interface.ts";
|
|
5
5
|
import { LaravelMigrationSchemaProvider } from "./providers/laravel-migration-provider.ts";
|
|
6
|
-
import { PrismaSchemaProvider } from "./providers/prisma-schema-provider.ts";
|
|
7
6
|
|
|
8
7
|
export class SchemaProviderRegistry {
|
|
9
8
|
private static providers: StackSchemaProvider[] = [
|
|
10
9
|
new LaravelMigrationSchemaProvider(),
|
|
11
|
-
new PrismaSchemaProvider(),
|
|
12
10
|
];
|
|
13
11
|
|
|
14
12
|
/**
|
|
@@ -30,7 +28,7 @@ export class SchemaProviderRegistry {
|
|
|
30
28
|
if (provider.canHandle(projectRoot)) {
|
|
31
29
|
try {
|
|
32
30
|
const res = provider.resolveEntitySchema(projectRoot, entitySymbol, entityFilePath);
|
|
33
|
-
if (res
|
|
31
|
+
if (res) {
|
|
34
32
|
return res;
|
|
35
33
|
}
|
|
36
34
|
} catch {
|
|
@@ -7,6 +7,7 @@ export interface SchemaFieldSummary {
|
|
|
7
7
|
}
|
|
8
8
|
|
|
9
9
|
export interface SchemaMetadata {
|
|
10
|
+
interpretation?: "static_migration_summary";
|
|
10
11
|
entityName: string;
|
|
11
12
|
tableName: string;
|
|
12
13
|
schemaSourceFile: string;
|
|
@@ -29,5 +30,5 @@ export interface StackSchemaProvider {
|
|
|
29
30
|
projectRoot: string,
|
|
30
31
|
entitySymbol: string,
|
|
31
32
|
entityFilePath?: string
|
|
32
|
-
):
|
|
33
|
+
): SchemaMetadata | null;
|
|
33
34
|
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type Parser from "web-tree-sitter";
|
|
2
|
+
|
|
3
|
+
// Decode literal values without executing source. Unsupported evaluation stays unresolved.
|
|
4
|
+
export function readPythonStaticString(node: Parser.SyntaxNode | null | undefined): string | undefined {
|
|
5
|
+
if (!node) return undefined;
|
|
6
|
+
if (node.type === "concatenated_string") {
|
|
7
|
+
let value = "";
|
|
8
|
+
for (const child of node.namedChildren) {
|
|
9
|
+
if (child.type === "comment") continue;
|
|
10
|
+
const part = readPythonStaticString(child);
|
|
11
|
+
if (part === undefined) return undefined;
|
|
12
|
+
value += part;
|
|
13
|
+
}
|
|
14
|
+
return value;
|
|
15
|
+
}
|
|
16
|
+
if (node.type !== "string") return undefined;
|
|
17
|
+
const match = /^([rRuU]*)("""|'''|"|')([\s\S]*)\2$/.exec(node.text);
|
|
18
|
+
if (!match || node.namedChildren.some(child => child.type === "interpolation")) return undefined;
|
|
19
|
+
const body = match[3];
|
|
20
|
+
if (match[1].toLowerCase().includes("r")) return body;
|
|
21
|
+
|
|
22
|
+
let value = "";
|
|
23
|
+
const escapes: Record<string, string> = { "\\": "\\", "'": "'", '"': '"', a: "\x07", b: "\b", f: "\f", n: "\n", r: "\r", t: "\t", v: "\v" };
|
|
24
|
+
for (let i = 0; i < body.length; i++) {
|
|
25
|
+
if (body[i] !== "\\") { value += body[i]; continue; }
|
|
26
|
+
const escaped = body[++i];
|
|
27
|
+
if (escaped === undefined) return undefined;
|
|
28
|
+
if (escaped === "\n") continue;
|
|
29
|
+
if (escaped === "\r" && body[i + 1] === "\n") { i++; continue; }
|
|
30
|
+
if (escapes[escaped] !== undefined) { value += escapes[escaped]; continue; }
|
|
31
|
+
if (/[0-7]/.test(escaped)) {
|
|
32
|
+
const octal = body.slice(i).match(/^[0-7]{1,3}/)![0];
|
|
33
|
+
value += String.fromCodePoint(parseInt(octal, 8));
|
|
34
|
+
i += octal.length - 1;
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
const digits = escaped === "x" ? 2 : escaped === "u" ? 4 : escaped === "U" ? 8 : 0;
|
|
38
|
+
if (digits) {
|
|
39
|
+
const hex = body.slice(i + 1, i + 1 + digits);
|
|
40
|
+
if (hex.length !== digits || !/^[0-9a-f]+$/i.test(hex)) return undefined;
|
|
41
|
+
const point = parseInt(hex, 16);
|
|
42
|
+
if (point > 0x10ffff || point >= 0xd800 && point <= 0xdfff) return undefined;
|
|
43
|
+
value += String.fromCodePoint(point);
|
|
44
|
+
i += digits;
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
if (escaped === "N") return undefined; // Unicode names require a Unicode database.
|
|
48
|
+
value += `\\${escaped}`;
|
|
49
|
+
}
|
|
50
|
+
return value;
|
|
51
|
+
}
|