@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,687 @@
|
|
|
1
|
+
import type Parser from "web-tree-sitter";
|
|
2
|
+
import { parseSourceWithTreeSitter } from "../wasm-loader.ts";
|
|
3
|
+
import { readOptionalSourceFile } from "../../resources/source-files.ts";
|
|
4
|
+
import { isIgnoredDirectory } from "../../resources/ignore.ts";
|
|
5
|
+
import { ImportResolver } from "../../resolver/import-resolver.ts";
|
|
6
|
+
import * as fs from "node:fs";
|
|
7
|
+
import * as path from "node:path";
|
|
8
|
+
import type { SemanticSliceExtractor, VerticalSliceCandidate } from "./semantic-extractor.interface.ts";
|
|
9
|
+
|
|
10
|
+
interface ViewMethodInfo {
|
|
11
|
+
method: string;
|
|
12
|
+
action: string;
|
|
13
|
+
line: number;
|
|
14
|
+
isCustomAction?: boolean;
|
|
15
|
+
isDetail?: boolean;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface ViewInfo {
|
|
19
|
+
file: string;
|
|
20
|
+
relPath: string;
|
|
21
|
+
line: number;
|
|
22
|
+
kind: "cbv" | "fbv" | "viewset";
|
|
23
|
+
serializer?: string;
|
|
24
|
+
model?: string;
|
|
25
|
+
methods: ViewMethodInfo[];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
interface RouterRegistration {
|
|
29
|
+
urlFile: string;
|
|
30
|
+
prefix: string;
|
|
31
|
+
viewName: string;
|
|
32
|
+
basename?: string;
|
|
33
|
+
line: number;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
interface PathRoute {
|
|
37
|
+
urlFile: string;
|
|
38
|
+
pattern: string;
|
|
39
|
+
viewName: string;
|
|
40
|
+
isAsView: boolean;
|
|
41
|
+
routeName?: string;
|
|
42
|
+
line: number;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
interface UrlInclusion {
|
|
46
|
+
parentUrlFile: string;
|
|
47
|
+
pattern: string;
|
|
48
|
+
targetModule: string;
|
|
49
|
+
line: number;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function cleanLiteral(node: Parser.SyntaxNode | null | undefined): string | undefined {
|
|
53
|
+
if (!node) return undefined;
|
|
54
|
+
if (node.type === "string") {
|
|
55
|
+
const raw = node.text.trim();
|
|
56
|
+
const match = raw.match(/^[rRbBuU]*('''|"""|'|")([\s\S]*?)\1$/);
|
|
57
|
+
if (match) return match[2];
|
|
58
|
+
return raw.replace(/^[rRbBuU]*['"]|['"]$/g, "");
|
|
59
|
+
}
|
|
60
|
+
return undefined;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function normalizeUri(prefix: string, sub: string): string {
|
|
64
|
+
const p = (prefix ?? "").trim().replace(/^\/+|\/+$/g, "");
|
|
65
|
+
const s = (sub ?? "").trim().replace(/^\/+|\/+$/g, "");
|
|
66
|
+
if (!p && !s) return "/";
|
|
67
|
+
if (!p) return `/${s}`;
|
|
68
|
+
if (!s) return `/${p}`;
|
|
69
|
+
return `/${p}/${s}`;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function cleanRoutePattern(raw: string): string {
|
|
73
|
+
return raw
|
|
74
|
+
.replace(/^\^+|\$+$/g, "")
|
|
75
|
+
.replace(/\(\?P<([a-zA-Z0-9_]+)>[^)]+\)/g, "{$1}")
|
|
76
|
+
.replace(/<([a-zA-Z0-9_]+:[a-zA-Z0-9_]+)>/g, (_match, p) => `{${p.split(":").pop()}}`)
|
|
77
|
+
.replace(/<([a-zA-Z0-9_]+)>/g, "{$1}");
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export class DjangoSemanticExtractor implements SemanticSliceExtractor {
|
|
81
|
+
public readonly id = "django";
|
|
82
|
+
public readonly name = "Django / DRF Semantic Slice Extractor";
|
|
83
|
+
|
|
84
|
+
public canHandle(projectRoot: string, framework?: string): boolean {
|
|
85
|
+
if (framework === "django") return true;
|
|
86
|
+
|
|
87
|
+
if (
|
|
88
|
+
fs.existsSync(path.join(projectRoot, "manage.py")) ||
|
|
89
|
+
fs.existsSync(path.join(projectRoot, "wsgi.py")) ||
|
|
90
|
+
fs.existsSync(path.join(projectRoot, "asgi.py"))
|
|
91
|
+
) {
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
for (const manifest of ["requirements.txt", "pyproject.toml", "Pipfile", "setup.py"]) {
|
|
96
|
+
const fullPath = path.join(projectRoot, manifest);
|
|
97
|
+
if (fs.existsSync(fullPath)) {
|
|
98
|
+
try {
|
|
99
|
+
const content = fs.readFileSync(fullPath, "utf-8").toLowerCase();
|
|
100
|
+
if (content.includes("django")) return true;
|
|
101
|
+
} catch {}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
public async extractSlices(projectRoot: string, domainId?: number, signal?: AbortSignal): Promise<VerticalSliceCandidate[]> {
|
|
109
|
+
const slices: VerticalSliceCandidate[] = [];
|
|
110
|
+
const pyFiles = this.findPythonFiles(projectRoot);
|
|
111
|
+
const resolver = new ImportResolver(projectRoot);
|
|
112
|
+
|
|
113
|
+
const viewsMap = new Map<string, ViewInfo>();
|
|
114
|
+
const routerRegistrations: RouterRegistration[] = [];
|
|
115
|
+
const pathRoutes: PathRoute[] = [];
|
|
116
|
+
const urlInclusions: UrlInclusion[] = [];
|
|
117
|
+
|
|
118
|
+
// 1. Locate and parse all views across Python files using Tree-sitter AST
|
|
119
|
+
for (const file of pyFiles) {
|
|
120
|
+
signal?.throwIfAborted();
|
|
121
|
+
const fileName = path.basename(file);
|
|
122
|
+
const isCandidate =
|
|
123
|
+
fileName.includes("view") ||
|
|
124
|
+
fileName.includes("api") ||
|
|
125
|
+
fileName.includes("urls");
|
|
126
|
+
|
|
127
|
+
const content = readOptionalSourceFile(file, "utf-8");
|
|
128
|
+
if (!content) continue;
|
|
129
|
+
|
|
130
|
+
const hasViewClues =
|
|
131
|
+
content.includes("ViewSet") ||
|
|
132
|
+
content.includes("APIView") ||
|
|
133
|
+
content.includes("View") ||
|
|
134
|
+
content.includes("api_view") ||
|
|
135
|
+
content.includes("router.register") ||
|
|
136
|
+
content.includes("urlpatterns");
|
|
137
|
+
|
|
138
|
+
if (!isCandidate && !hasViewClues) continue;
|
|
139
|
+
|
|
140
|
+
const relPath = path.relative(projectRoot, file).replace(/\\/g, "/");
|
|
141
|
+
|
|
142
|
+
const parsed = await parseSourceWithTreeSitter("python", content, (root) => {
|
|
143
|
+
const walk = (node: Parser.SyntaxNode, visit: (node: Parser.SyntaxNode) => void) => {
|
|
144
|
+
visit(node);
|
|
145
|
+
for (const child of node.namedChildren) walk(child, visit);
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
// A. Class-Based Views and ViewSets
|
|
149
|
+
walk(root, (node) => {
|
|
150
|
+
if (node.type !== "class_definition") return;
|
|
151
|
+
const nameNode = node.childForFieldName("name");
|
|
152
|
+
if (!nameNode) return;
|
|
153
|
+
const className = nameNode.text.trim();
|
|
154
|
+
const classLine = node.startPosition.row + 1;
|
|
155
|
+
|
|
156
|
+
const superNode = node.childForFieldName("superclasses");
|
|
157
|
+
const baseClasses: string[] = [];
|
|
158
|
+
if (superNode) {
|
|
159
|
+
for (const child of superNode.namedChildren) {
|
|
160
|
+
baseClasses.push(child.text.trim());
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const isViewSet = baseClasses.some((b) => /ViewSet/i.test(b));
|
|
165
|
+
const isCbv = isViewSet || baseClasses.some((b) => /View/i.test(b));
|
|
166
|
+
|
|
167
|
+
let serializer: string | undefined;
|
|
168
|
+
let model: string | undefined;
|
|
169
|
+
const explicitMethods: Array<{
|
|
170
|
+
method: string;
|
|
171
|
+
action: string;
|
|
172
|
+
line: number;
|
|
173
|
+
isCustomAction?: boolean;
|
|
174
|
+
isDetail?: boolean;
|
|
175
|
+
}> = [];
|
|
176
|
+
|
|
177
|
+
const body = node.childForFieldName("body");
|
|
178
|
+
if (body) {
|
|
179
|
+
for (const child of body.namedChildren) {
|
|
180
|
+
if (child.type === "expression_statement") {
|
|
181
|
+
const assign = child.namedChildren[0];
|
|
182
|
+
if (assign?.type === "assignment") {
|
|
183
|
+
const left = assign.childForFieldName("left")?.text?.trim();
|
|
184
|
+
const right = assign.childForFieldName("right");
|
|
185
|
+
if (left === "serializer_class") {
|
|
186
|
+
serializer = right?.text?.trim();
|
|
187
|
+
} else if (left === "model") {
|
|
188
|
+
model = right?.text?.trim();
|
|
189
|
+
} else if (left === "queryset" && right) {
|
|
190
|
+
let cur = right.type === "call" ? right.childForFieldName("function") : right;
|
|
191
|
+
while (cur?.type === "attribute") {
|
|
192
|
+
cur = cur.childForFieldName("object");
|
|
193
|
+
}
|
|
194
|
+
if (cur?.type === "identifier" && /^[A-Z]/.test(cur.text)) {
|
|
195
|
+
model = cur.text;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
} else if (child.type === "function_definition") {
|
|
200
|
+
const fnName = child.childForFieldName("name")?.text?.trim();
|
|
201
|
+
const fnLine = child.startPosition.row + 1;
|
|
202
|
+
if (!fnName) continue;
|
|
203
|
+
|
|
204
|
+
if (isViewSet) {
|
|
205
|
+
const actionMap: Record<string, string> = {
|
|
206
|
+
list: "GET",
|
|
207
|
+
create: "POST",
|
|
208
|
+
retrieve: "GET",
|
|
209
|
+
update: "PUT",
|
|
210
|
+
partial_update: "PATCH",
|
|
211
|
+
destroy: "DELETE",
|
|
212
|
+
};
|
|
213
|
+
if (actionMap[fnName]) {
|
|
214
|
+
explicitMethods.push({ method: actionMap[fnName], action: fnName, line: fnLine });
|
|
215
|
+
}
|
|
216
|
+
} else {
|
|
217
|
+
if (/^(get|post|put|patch|delete|options|head)$/.test(fnName)) {
|
|
218
|
+
explicitMethods.push({ method: fnName.toUpperCase(), action: fnName, line: fnLine });
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
} else if (child.type === "decorated_definition") {
|
|
222
|
+
const defNode = child.childForFieldName("definition");
|
|
223
|
+
if (defNode?.type === "function_definition") {
|
|
224
|
+
const fnName = defNode.childForFieldName("name")?.text?.trim();
|
|
225
|
+
const fnLine = defNode.startPosition.row + 1;
|
|
226
|
+
if (fnName && isViewSet) {
|
|
227
|
+
for (const dec of child.namedChildren.filter((c) => c.type === "decorator")) {
|
|
228
|
+
const decCall = dec.namedChildren[0];
|
|
229
|
+
if (decCall?.type === "call" && decCall.childForFieldName("function")?.text?.endsWith("action")) {
|
|
230
|
+
const args = decCall.childForFieldName("arguments")?.namedChildren ?? [];
|
|
231
|
+
const detailKw = args.find(
|
|
232
|
+
(c) => c.type === "keyword_argument" && c.childForFieldName("name")?.text === "detail"
|
|
233
|
+
);
|
|
234
|
+
const isDetail = detailKw?.childForFieldName("value")?.text?.toLowerCase() === "true";
|
|
235
|
+
const methodsKw = args.find(
|
|
236
|
+
(c) => c.type === "keyword_argument" && c.childForFieldName("name")?.text === "methods"
|
|
237
|
+
);
|
|
238
|
+
const actMethods: string[] = [];
|
|
239
|
+
if (methodsKw) {
|
|
240
|
+
const val = methodsKw.childForFieldName("value");
|
|
241
|
+
if (val?.type === "list") {
|
|
242
|
+
for (const m of val.namedChildren) {
|
|
243
|
+
const lit = cleanLiteral(m)?.toUpperCase();
|
|
244
|
+
if (lit) actMethods.push(lit);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
if (actMethods.length === 0) actMethods.push("GET");
|
|
249
|
+
for (const method of actMethods) {
|
|
250
|
+
explicitMethods.push({ method, action: fnName, line: fnLine, isCustomAction: true, isDetail });
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
const methods: ViewMethodInfo[] = [];
|
|
261
|
+
|
|
262
|
+
if (isViewSet) {
|
|
263
|
+
const fullCrud = baseClasses.some((b) => /\bModelViewSet\b/.test(b));
|
|
264
|
+
const readOnly = baseClasses.some((b) => /\bReadOnlyModelViewSet\b/.test(b));
|
|
265
|
+
const hasListMixin = baseClasses.some((b) => /\bListModelMixin\b/.test(b));
|
|
266
|
+
const hasCreateMixin = baseClasses.some((b) => /\bCreateModelMixin\b/.test(b));
|
|
267
|
+
const hasRetrieveMixin = baseClasses.some((b) => /\bRetrieveModelMixin\b/.test(b));
|
|
268
|
+
const hasUpdateMixin = baseClasses.some((b) => /\bUpdateModelMixin\b/.test(b));
|
|
269
|
+
const hasDestroyMixin = baseClasses.some((b) => /\bDestroyModelMixin\b/.test(b));
|
|
270
|
+
|
|
271
|
+
const defaultActions: Array<[string, string, boolean]> = [
|
|
272
|
+
["GET", "list", fullCrud || readOnly || hasListMixin],
|
|
273
|
+
["POST", "create", fullCrud || hasCreateMixin],
|
|
274
|
+
["GET", "retrieve", fullCrud || readOnly || hasRetrieveMixin],
|
|
275
|
+
["PUT", "update", fullCrud || hasUpdateMixin],
|
|
276
|
+
["PATCH", "partial_update", fullCrud || hasUpdateMixin],
|
|
277
|
+
["DELETE", "destroy", fullCrud || hasDestroyMixin],
|
|
278
|
+
];
|
|
279
|
+
|
|
280
|
+
for (const [method, action, allowed] of defaultActions) {
|
|
281
|
+
const explicit = explicitMethods.find((m) => m.action === action && !m.isCustomAction);
|
|
282
|
+
if (explicit) {
|
|
283
|
+
methods.push(explicit);
|
|
284
|
+
} else if (allowed) {
|
|
285
|
+
methods.push({ method, action, line: classLine });
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
for (const custom of explicitMethods.filter((m) => m.isCustomAction)) {
|
|
290
|
+
methods.push(custom);
|
|
291
|
+
}
|
|
292
|
+
} else {
|
|
293
|
+
methods.push(...explicitMethods);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
if (methods.length > 0 || isViewSet || serializer || model || isCbv) {
|
|
297
|
+
viewsMap.set(className, {
|
|
298
|
+
file,
|
|
299
|
+
relPath,
|
|
300
|
+
line: classLine,
|
|
301
|
+
kind: isViewSet ? "viewset" : "cbv",
|
|
302
|
+
serializer,
|
|
303
|
+
model,
|
|
304
|
+
methods,
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
// B. Function-Based Views with @api_view
|
|
310
|
+
walk(root, (node) => {
|
|
311
|
+
if (node.type !== "decorated_definition") return;
|
|
312
|
+
const fn = node.childForFieldName("definition");
|
|
313
|
+
if (fn?.type !== "function_definition") return;
|
|
314
|
+
const fnName = fn.childForFieldName("name")?.text?.trim();
|
|
315
|
+
if (!fnName) return;
|
|
316
|
+
const fnLine = fn.startPosition.row + 1;
|
|
317
|
+
|
|
318
|
+
for (const decorator of node.namedChildren.filter((c) => c.type === "decorator")) {
|
|
319
|
+
const call = decorator.namedChildren[0];
|
|
320
|
+
if (call?.type !== "call") continue;
|
|
321
|
+
const fnExpr = call.childForFieldName("function")?.text?.trim();
|
|
322
|
+
if (fnExpr !== "api_view" && fnExpr !== "rest_framework.decorators.api_view") continue;
|
|
323
|
+
|
|
324
|
+
const listArg = call.childForFieldName("arguments")?.namedChildren[0];
|
|
325
|
+
const httpMethods: string[] = [];
|
|
326
|
+
if (listArg?.type === "list") {
|
|
327
|
+
for (const item of listArg.namedChildren) {
|
|
328
|
+
const m = cleanLiteral(item)?.toUpperCase();
|
|
329
|
+
if (m) httpMethods.push(m);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
if (httpMethods.length > 0) {
|
|
334
|
+
viewsMap.set(fnName, {
|
|
335
|
+
file,
|
|
336
|
+
relPath,
|
|
337
|
+
line: fnLine,
|
|
338
|
+
kind: "fbv",
|
|
339
|
+
methods: httpMethods.map((m) => ({ method: m, action: fnName, line: fnLine })),
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
// C. URLConf: router.register, path(), and re_path()
|
|
346
|
+
walk(root, (node) => {
|
|
347
|
+
if (node.type !== "call") return;
|
|
348
|
+
const fn = node.childForFieldName("function");
|
|
349
|
+
const args = node.childForFieldName("arguments")?.namedChildren ?? [];
|
|
350
|
+
|
|
351
|
+
// router.register(r'orders', OrderViewSet, basename='order')
|
|
352
|
+
if (fn?.type === "attribute" && fn.childForFieldName("attribute")?.text === "register") {
|
|
353
|
+
const firstArg = args[0];
|
|
354
|
+
const secondArg = args[1];
|
|
355
|
+
if (firstArg && secondArg) {
|
|
356
|
+
const prefix = cleanLiteral(firstArg);
|
|
357
|
+
if (prefix !== undefined) {
|
|
358
|
+
const viewName = secondArg.text.trim().split(".").pop() || secondArg.text.trim();
|
|
359
|
+
const bnKw = args.find(
|
|
360
|
+
(c) => c.type === "keyword_argument" && c.childForFieldName("name")?.text === "basename"
|
|
361
|
+
);
|
|
362
|
+
const basename = cleanLiteral(bnKw?.childForFieldName("value"));
|
|
363
|
+
routerRegistrations.push({
|
|
364
|
+
urlFile: relPath,
|
|
365
|
+
prefix,
|
|
366
|
+
viewName,
|
|
367
|
+
basename,
|
|
368
|
+
line: node.startPosition.row + 1,
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
return;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
// path('api/checkout/', CheckoutView.as_view(), name='checkout')
|
|
376
|
+
// path('api/', include('orders.urls'))
|
|
377
|
+
const fnName = fn?.text?.trim();
|
|
378
|
+
if (fnName === "path" || fnName === "re_path") {
|
|
379
|
+
const firstArg = args[0];
|
|
380
|
+
const secondArg = args[1];
|
|
381
|
+
if (!firstArg || !secondArg) return;
|
|
382
|
+
|
|
383
|
+
const pattern = cleanLiteral(firstArg);
|
|
384
|
+
if (pattern === undefined) return;
|
|
385
|
+
|
|
386
|
+
const nameKw = args.find(
|
|
387
|
+
(c) => c.type === "keyword_argument" && c.childForFieldName("name")?.text === "name"
|
|
388
|
+
);
|
|
389
|
+
const routeName = cleanLiteral(nameKw?.childForFieldName("value"));
|
|
390
|
+
|
|
391
|
+
// Check if secondArg is include(...)
|
|
392
|
+
if (secondArg.type === "call" && secondArg.childForFieldName("function")?.text?.endsWith("include")) {
|
|
393
|
+
const incArgs = secondArg.childForFieldName("arguments")?.namedChildren ?? [];
|
|
394
|
+
let incTarget: string | undefined;
|
|
395
|
+
if (incArgs[0]?.type === "string") {
|
|
396
|
+
incTarget = cleanLiteral(incArgs[0]);
|
|
397
|
+
} else if (incArgs[0]?.type === "tuple" || incArgs[0]?.type === "list") {
|
|
398
|
+
incTarget = cleanLiteral(incArgs[0].namedChildren[0]);
|
|
399
|
+
}
|
|
400
|
+
if (incTarget) {
|
|
401
|
+
urlInclusions.push({
|
|
402
|
+
parentUrlFile: relPath,
|
|
403
|
+
pattern,
|
|
404
|
+
targetModule: incTarget,
|
|
405
|
+
line: node.startPosition.row + 1,
|
|
406
|
+
});
|
|
407
|
+
}
|
|
408
|
+
return;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
// Check if secondArg is View.as_view() or FBV
|
|
412
|
+
let isAsView = false;
|
|
413
|
+
let targetView = secondArg.text.trim();
|
|
414
|
+
if (secondArg.type === "call") {
|
|
415
|
+
const callFn = secondArg.childForFieldName("function");
|
|
416
|
+
if (callFn?.type === "attribute" && callFn.childForFieldName("attribute")?.text === "as_view") {
|
|
417
|
+
isAsView = true;
|
|
418
|
+
targetView = callFn.childForFieldName("object")?.text?.trim() || targetView;
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
targetView = targetView.split(".").pop() || targetView;
|
|
422
|
+
|
|
423
|
+
pathRoutes.push({
|
|
424
|
+
urlFile: relPath,
|
|
425
|
+
pattern,
|
|
426
|
+
viewName: targetView,
|
|
427
|
+
isAsView,
|
|
428
|
+
routeName,
|
|
429
|
+
line: node.startPosition.row + 1,
|
|
430
|
+
});
|
|
431
|
+
}
|
|
432
|
+
});
|
|
433
|
+
|
|
434
|
+
return { symbols: [], dependencies: [] };
|
|
435
|
+
});
|
|
436
|
+
|
|
437
|
+
if (parsed?.status !== "parsed") {
|
|
438
|
+
throw new Error(`Django AST unavailable or degraded: ${relPath}`);
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
// 2. Resolve Multi-file URLConf Inclusions (path('api/', include('orders.urls')))
|
|
443
|
+
const urlParentMap = new Map<string, Array<{ parentFile: string; prefix: string }>>();
|
|
444
|
+
for (const inc of urlInclusions) {
|
|
445
|
+
const parentAbs = path.resolve(projectRoot, inc.parentUrlFile);
|
|
446
|
+
const res = resolver.resolve(inc.targetModule, parentAbs);
|
|
447
|
+
if (res.status === "resolved" && res.targets[0]?.files?.[0]) {
|
|
448
|
+
const targetRel = res.targets[0].files[0];
|
|
449
|
+
if (!urlParentMap.has(targetRel)) urlParentMap.set(targetRel, []);
|
|
450
|
+
urlParentMap.get(targetRel)!.push({ parentFile: inc.parentUrlFile, prefix: inc.pattern });
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
// Compute effective prefix for each urls.py file
|
|
455
|
+
const urlPrefixesByFile = new Map<string, string[]>();
|
|
456
|
+
const allUrlFiles = new Set<string>([
|
|
457
|
+
...routerRegistrations.map((r) => r.urlFile),
|
|
458
|
+
...pathRoutes.map((p) => p.urlFile),
|
|
459
|
+
...urlInclusions.map((i) => i.parentUrlFile),
|
|
460
|
+
]);
|
|
461
|
+
|
|
462
|
+
for (const uFile of allUrlFiles) {
|
|
463
|
+
const parents = urlParentMap.get(uFile);
|
|
464
|
+
if (!parents || parents.length === 0) {
|
|
465
|
+
urlPrefixesByFile.set(uFile, [""]);
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
const queue = Array.from(urlPrefixesByFile.keys());
|
|
470
|
+
const visitedIncs = new Set<string>();
|
|
471
|
+
|
|
472
|
+
while (queue.length > 0) {
|
|
473
|
+
const currentFile = queue.shift()!;
|
|
474
|
+
const currentPrefixes = urlPrefixesByFile.get(currentFile) ?? [""];
|
|
475
|
+
|
|
476
|
+
// Check children included by currentFile
|
|
477
|
+
for (const [targetFile, parents] of urlParentMap.entries()) {
|
|
478
|
+
for (const p of parents) {
|
|
479
|
+
if (p.parentFile === currentFile) {
|
|
480
|
+
const edgeKey = `${currentFile}->${targetFile}:${p.prefix}`;
|
|
481
|
+
if (visitedIncs.has(edgeKey)) continue;
|
|
482
|
+
visitedIncs.add(edgeKey);
|
|
483
|
+
|
|
484
|
+
const nextPrefixes = currentPrefixes.map((cp) => normalizeUri(cp, p.prefix));
|
|
485
|
+
const existing = urlPrefixesByFile.get(targetFile) ?? [];
|
|
486
|
+
const merged = Array.from(new Set([...existing, ...nextPrefixes]));
|
|
487
|
+
urlPrefixesByFile.set(targetFile, merged);
|
|
488
|
+
queue.push(targetFile);
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
// 3. Generate Vertical Slices from DRF Router Registrations
|
|
495
|
+
for (const reg of routerRegistrations) {
|
|
496
|
+
const prefixes = urlPrefixesByFile.get(reg.urlFile) ?? [""];
|
|
497
|
+
const viewInfo = viewsMap.get(reg.viewName);
|
|
498
|
+
if (!viewInfo) continue;
|
|
499
|
+
|
|
500
|
+
for (const pfx of prefixes) {
|
|
501
|
+
const routePrefix = normalizeUri(pfx, reg.prefix);
|
|
502
|
+
|
|
503
|
+
for (const m of viewInfo.methods) {
|
|
504
|
+
const uriSuffix = m.isCustomAction
|
|
505
|
+
? (m.isDetail ? `/{id}/${m.action}` : `/${m.action}`)
|
|
506
|
+
: (m.action === "list" || m.action === "create" ? "" : "/{id}");
|
|
507
|
+
const fullUri = `${routePrefix === "/" ? "" : routePrefix}${uriSuffix}` || "/";
|
|
508
|
+
|
|
509
|
+
const chain: Array<{ stage: string; symbol: string; file?: string; line?: number; description?: string }> = [
|
|
510
|
+
{
|
|
511
|
+
stage: "ingress",
|
|
512
|
+
symbol: `${m.method} ${fullUri}`,
|
|
513
|
+
file: reg.urlFile,
|
|
514
|
+
line: reg.line,
|
|
515
|
+
description: `Django / DRF Route: ${m.method} ${fullUri}`,
|
|
516
|
+
},
|
|
517
|
+
];
|
|
518
|
+
|
|
519
|
+
if (viewInfo.serializer) {
|
|
520
|
+
chain.push({
|
|
521
|
+
stage: "validation",
|
|
522
|
+
symbol: viewInfo.serializer,
|
|
523
|
+
description: "DRF Serializer Validation",
|
|
524
|
+
});
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
chain.push({
|
|
528
|
+
stage: "controller",
|
|
529
|
+
symbol: `${reg.viewName}::${m.action}`,
|
|
530
|
+
file: viewInfo.relPath,
|
|
531
|
+
line: m.line,
|
|
532
|
+
description:
|
|
533
|
+
m.isCustomAction
|
|
534
|
+
? "DRF ViewSet Custom Action"
|
|
535
|
+
: m.line === viewInfo.line
|
|
536
|
+
? "Inherited DRF action (inferred from base class or mixin; line points to class)"
|
|
537
|
+
: "DRF ViewSet Action",
|
|
538
|
+
});
|
|
539
|
+
|
|
540
|
+
if (viewInfo.model) {
|
|
541
|
+
chain.push({
|
|
542
|
+
stage: "model",
|
|
543
|
+
symbol: viewInfo.model,
|
|
544
|
+
description: "Django ORM Model",
|
|
545
|
+
});
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
const evidencedChain = chain.map((node) => ({
|
|
549
|
+
...node,
|
|
550
|
+
relationship: {
|
|
551
|
+
kind:
|
|
552
|
+
node.stage === "ingress" || node.stage === "controller"
|
|
553
|
+
? "route_binding"
|
|
554
|
+
: node.stage === "validation" || node.stage === "model"
|
|
555
|
+
? "schema_binding"
|
|
556
|
+
: "calls",
|
|
557
|
+
derivation: node.file && node.line ? "source" : "inferred",
|
|
558
|
+
source: node.file && node.line ? { file: node.file, line: node.line } : undefined,
|
|
559
|
+
reason: node.description ?? "static adapter evidence",
|
|
560
|
+
},
|
|
561
|
+
}));
|
|
562
|
+
|
|
563
|
+
slices.push({
|
|
564
|
+
domain_id: domainId ?? null,
|
|
565
|
+
feature_key: null,
|
|
566
|
+
http_method: m.method,
|
|
567
|
+
route_uri: fullUri,
|
|
568
|
+
route_name: `${reg.viewName}.${m.action}`,
|
|
569
|
+
controller_class: reg.viewName,
|
|
570
|
+
action_name: m.action,
|
|
571
|
+
controller_file: viewInfo.relPath,
|
|
572
|
+
controller_line: m.line,
|
|
573
|
+
architecture_style: "django-drf",
|
|
574
|
+
entry_kind: "http_route",
|
|
575
|
+
execution_chain_json: JSON.stringify(evidencedChain),
|
|
576
|
+
});
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
// 4. Generate Vertical Slices from path() & re_path() Routes
|
|
582
|
+
for (const route of pathRoutes) {
|
|
583
|
+
const prefixes = urlPrefixesByFile.get(route.urlFile) ?? [""];
|
|
584
|
+
const viewInfo = viewsMap.get(route.viewName);
|
|
585
|
+
if (!viewInfo || viewInfo.methods.length === 0) continue;
|
|
586
|
+
|
|
587
|
+
for (const pfx of prefixes) {
|
|
588
|
+
const fullUri = normalizeUri(pfx, cleanRoutePattern(route.pattern));
|
|
589
|
+
|
|
590
|
+
for (const m of viewInfo.methods) {
|
|
591
|
+
const chain: Array<{ stage: string; symbol: string; file?: string; line?: number; description?: string }> = [
|
|
592
|
+
{
|
|
593
|
+
stage: "ingress",
|
|
594
|
+
symbol: `${m.method} ${fullUri}`,
|
|
595
|
+
file: route.urlFile,
|
|
596
|
+
line: route.line,
|
|
597
|
+
description: `Django Route: ${m.method} ${fullUri}`,
|
|
598
|
+
},
|
|
599
|
+
];
|
|
600
|
+
|
|
601
|
+
if (viewInfo.serializer) {
|
|
602
|
+
chain.push({
|
|
603
|
+
stage: "validation",
|
|
604
|
+
symbol: viewInfo.serializer,
|
|
605
|
+
description: "Serializer Validation",
|
|
606
|
+
});
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
chain.push({
|
|
610
|
+
stage: "controller",
|
|
611
|
+
symbol: `${route.viewName}::${m.action}`,
|
|
612
|
+
file: viewInfo.relPath,
|
|
613
|
+
line: m.line,
|
|
614
|
+
description:
|
|
615
|
+
viewInfo.kind === "viewset" && m.line === viewInfo.line
|
|
616
|
+
? "Inherited DRF action (inferred from base class or mixin; line points to class)"
|
|
617
|
+
: "Django View Handler",
|
|
618
|
+
});
|
|
619
|
+
|
|
620
|
+
if (viewInfo.model) {
|
|
621
|
+
chain.push({
|
|
622
|
+
stage: "model",
|
|
623
|
+
symbol: viewInfo.model,
|
|
624
|
+
description: "Django Model",
|
|
625
|
+
});
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
const evidencedChain = chain.map((node) => ({
|
|
629
|
+
...node,
|
|
630
|
+
relationship: {
|
|
631
|
+
kind:
|
|
632
|
+
node.stage === "ingress" || node.stage === "controller"
|
|
633
|
+
? "route_binding"
|
|
634
|
+
: node.stage === "validation" || node.stage === "model"
|
|
635
|
+
? "schema_binding"
|
|
636
|
+
: "calls",
|
|
637
|
+
derivation: node.file && node.line ? "source" : "inferred",
|
|
638
|
+
source: node.file && node.line ? { file: node.file, line: node.line } : undefined,
|
|
639
|
+
reason: node.description ?? "static adapter evidence",
|
|
640
|
+
},
|
|
641
|
+
}));
|
|
642
|
+
|
|
643
|
+
slices.push({
|
|
644
|
+
domain_id: domainId ?? null,
|
|
645
|
+
feature_key: null,
|
|
646
|
+
http_method: m.method,
|
|
647
|
+
route_uri: fullUri,
|
|
648
|
+
route_name: route.routeName || `${route.viewName}.${m.action}`,
|
|
649
|
+
controller_class: route.viewName,
|
|
650
|
+
action_name: m.action,
|
|
651
|
+
controller_file: viewInfo.relPath,
|
|
652
|
+
controller_line: m.line,
|
|
653
|
+
architecture_style: "django-mvc",
|
|
654
|
+
entry_kind: "http_route",
|
|
655
|
+
execution_chain_json: JSON.stringify(evidencedChain),
|
|
656
|
+
});
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
return slices;
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
private findPythonFiles(dir: string): string[] {
|
|
665
|
+
const results: string[] = [];
|
|
666
|
+
const traverse = (currentDir: string) => {
|
|
667
|
+
if (!fs.existsSync(currentDir)) return;
|
|
668
|
+
try {
|
|
669
|
+
const entries = fs.readdirSync(currentDir, { withFileTypes: true });
|
|
670
|
+
for (const entry of entries) {
|
|
671
|
+
if (isIgnoredDirectory(entry.name)) continue;
|
|
672
|
+
|
|
673
|
+
const fullPath = path.join(currentDir, entry.name);
|
|
674
|
+
if (entry.isDirectory()) {
|
|
675
|
+
traverse(fullPath);
|
|
676
|
+
} else if (entry.isFile() && entry.name.endsWith(".py")) {
|
|
677
|
+
results.push(fullPath);
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
} catch {}
|
|
681
|
+
};
|
|
682
|
+
|
|
683
|
+
traverse(dir);
|
|
684
|
+
return results;
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
|