septum 0.1.0 → 0.1.2

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.
Files changed (65) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +100 -100
  3. package/bin/septum.ts +4 -4
  4. package/package.json +63 -62
  5. package/src/cli/commands/check.ts +161 -161
  6. package/src/cli/commands/hook.ts +188 -188
  7. package/src/cli/commands/ingest.ts +29 -29
  8. package/src/cli/commands/init.ts +211 -211
  9. package/src/cli/commands/locate.ts +61 -61
  10. package/src/cli/commands/query.ts +64 -64
  11. package/src/cli/commands/serve.ts +5 -5
  12. package/src/cli/commands/slice.ts +90 -90
  13. package/src/cli/commands/sync.ts +31 -31
  14. package/src/cli/index.ts +199 -178
  15. package/src/cli/templates/hooks.ts +75 -75
  16. package/src/cli/templates/rules.ts +38 -38
  17. package/src/cli/templates/skill.ts +66 -66
  18. package/src/core/boundary/evaluator.ts +210 -210
  19. package/src/core/config/loader.ts +84 -84
  20. package/src/core/config/schema.ts +39 -39
  21. package/src/core/database/client.ts +44 -132
  22. package/src/core/database/repositories/dependency.repository.ts +279 -279
  23. package/src/core/database/repositories/domain.repository.ts +198 -198
  24. package/src/core/database/repositories/file.repository.ts +99 -99
  25. package/src/core/database/repositories/meta.repository.ts +33 -33
  26. package/src/core/database/repositories/symbol.repository.ts +424 -424
  27. package/src/core/database/repository.ts +242 -242
  28. package/src/core/database/schema.ts +120 -0
  29. package/src/core/discovery/topology-detector.ts +418 -418
  30. package/src/core/ingestion/hasher.ts +5 -5
  31. package/src/core/ingestion/pipeline.ts +438 -438
  32. package/src/core/parser/boundary-tracker.ts +128 -128
  33. package/src/core/parser/extractors/base.ts +60 -60
  34. package/src/core/parser/extractors/frontend.ts +194 -194
  35. package/src/core/parser/extractors/go.ts +176 -176
  36. package/src/core/parser/extractors/laravel-semantic.ts +426 -426
  37. package/src/core/parser/extractors/mcp-cli-semantic.ts +199 -199
  38. package/src/core/parser/extractors/nestjs-semantic.ts +119 -119
  39. package/src/core/parser/extractors/php.ts +149 -149
  40. package/src/core/parser/extractors/python.ts +226 -226
  41. package/src/core/parser/extractors/registry.ts +49 -49
  42. package/src/core/parser/extractors/semantic-extractor.interface.ts +21 -21
  43. package/src/core/parser/extractors/typescript.ts +177 -177
  44. package/src/core/parser/tree-sitter.ts +39 -39
  45. package/src/core/resolver/call-graph-tracer.ts +668 -668
  46. package/src/core/resolver/module-resolver.ts +342 -323
  47. package/src/core/resolver/symbol-locator.ts +543 -405
  48. package/src/core/resolver/vertical-slice-tracer.ts +237 -236
  49. package/src/core/session/session-manager.ts +56 -56
  50. package/src/core/telemetry/telemetry.ts +86 -86
  51. package/src/index.ts +18 -18
  52. package/src/mcp/schemas.ts +68 -68
  53. package/src/mcp/server.ts +612 -602
  54. package/src/mcp/tools/check-boundary.ts +102 -102
  55. package/src/mcp/tools/get-domain-catalog.ts +91 -91
  56. package/src/mcp/tools/get-feature-context.ts +116 -116
  57. package/src/mcp/tools/get-symbol-hotspots.ts +49 -49
  58. package/src/mcp/tools/get-symbol-impact.ts +26 -26
  59. package/src/mcp/tools/get-symbol.ts +63 -63
  60. package/src/mcp/tools/locate-symbol.ts +74 -74
  61. package/src/mcp/tools/register-domain.ts +64 -64
  62. package/src/mcp/tools/trace-vertical-slice.ts +102 -102
  63. package/src/types/index.ts +296 -296
  64. package/src/version.ts +3 -0
  65. package/src/core/database/schema.sql +0 -92
@@ -1,418 +1,418 @@
1
- import * as fs from "node:fs";
2
- import * as path from "node:path";
3
- import type { DomainConfig } from "../../types/index.ts";
4
- import type { SeptumRepository } from "../database/repository.ts";
5
-
6
- export type ProjectType =
7
- | "monorepo"
8
- | "fullstack"
9
- | "backend"
10
- | "frontend"
11
- | "cli-mcp"
12
- | "generic";
13
-
14
- export type ArchitectureStyle =
15
- | "modular-ddd"
16
- | "layered-monolith"
17
- | "clean-architecture"
18
- | "monorepo-workspaces"
19
- | "cli-adapter"
20
- | "generic";
21
-
22
- export interface DiscoveredProjectTopology {
23
- framework: string;
24
- language: "typescript" | "javascript" | "php" | "python" | "go" | "generic";
25
- projectType: ProjectType;
26
- architectureStyle: ArchitectureStyle;
27
- domains: Record<string, DomainConfig>;
28
- }
29
-
30
- export class TopologyDetector {
31
- private static readonly IGNORED_DIRS = new Set([
32
- "node_modules",
33
- "vendor",
34
- "dist",
35
- "build",
36
- ".git",
37
- ".septum",
38
- ".system_generated",
39
- "tests",
40
- "test",
41
- "coverage",
42
- ".vscode",
43
- ".idea",
44
- ".agents",
45
- "bin",
46
- ]);
47
-
48
- /**
49
- * Detects project topology, classifies project type & architecture style, and maps domains.
50
- */
51
- public static detect(projectRoot: string = process.cwd()): DiscoveredProjectTopology {
52
- const language = this.detectLanguage(projectRoot);
53
- const framework = this.detectFramework(projectRoot, language);
54
- const isMonorepo = this.detectMonorepo(projectRoot);
55
-
56
- const domains: Record<string, DomainConfig> = {};
57
- let architectureStyle: ArchitectureStyle = "generic";
58
-
59
- // 1. Monorepo Workspaces (apps/*, packages/*)
60
- if (isMonorepo) {
61
- architectureStyle = "monorepo-workspaces";
62
- const workspaceDirs = ["apps", "packages", "modules"];
63
- const appDomains: string[] = [];
64
- const packageDomains: string[] = [];
65
-
66
- for (const parent of workspaceDirs) {
67
- const fullParent = path.join(projectRoot, parent);
68
- if (fs.existsSync(fullParent) && fs.statSync(fullParent).isDirectory()) {
69
- const subs = this.getSubdirectories(fullParent);
70
- for (const sub of subs) {
71
- const relRoot = path.join(parent, sub);
72
- const domainName = sub.toLowerCase();
73
- if (parent === "apps") appDomains.push(domainName);
74
- else packageDomains.push(domainName);
75
-
76
- domains[domainName] = {
77
- root: relRoot,
78
- description: `Monorepo ${parent} package: ${sub}`,
79
- allowed_dependencies: parent === "apps" ? packageDomains : [],
80
- forbidden_dependencies: parent !== "apps" ? appDomains : [],
81
- archetypes: this.inferDefaultArchetypes(path.join(projectRoot, relRoot)),
82
- };
83
- }
84
- }
85
- }
86
- }
87
-
88
- // 2. Modular DDD (app/Domain, src/modules, src/domains, etc.)
89
- if (Object.keys(domains).length === 0) {
90
- const modularCandidates = [
91
- { prefix: "domain", dir: path.join(projectRoot, "app", "Domain") },
92
- { prefix: "domain", dir: path.join(projectRoot, "app", "Domains") },
93
- { prefix: "domain", dir: path.join(projectRoot, "src", "domains") },
94
- { prefix: "feature", dir: path.join(projectRoot, "src", "features") },
95
- { prefix: "module", dir: path.join(projectRoot, "src", "modules") },
96
- ];
97
-
98
- for (const cand of modularCandidates) {
99
- if (fs.existsSync(cand.dir) && fs.statSync(cand.dir).isDirectory()) {
100
- const subdirs = this.getSubdirectories(cand.dir);
101
- if (subdirs.length > 0) {
102
- architectureStyle = "modular-ddd";
103
- for (const sub of subdirs) {
104
- const relRoot = path.relative(projectRoot, path.join(cand.dir, sub));
105
- domains[sub.toLowerCase()] = {
106
- root: relRoot,
107
- description: `Modular ${cand.prefix} domain: ${sub}`,
108
- allowed_dependencies: ["shared", "common"],
109
- forbidden_dependencies: [],
110
- archetypes: this.inferDefaultArchetypes(path.join(projectRoot, relRoot)),
111
- };
112
- }
113
- }
114
- }
115
- }
116
- }
117
-
118
- // 3. Layered Monolith / Clean Architecture in src/
119
- const srcDir = path.join(projectRoot, "src");
120
- if (Object.keys(domains).length === 0 && fs.existsSync(srcDir) && fs.statSync(srcDir).isDirectory()) {
121
- const subdirs = this.getSubdirectories(srcDir);
122
- if (subdirs.length > 0) {
123
- architectureStyle =
124
- framework === "mcp-server" || framework === "cli"
125
- ? "cli-adapter"
126
- : "layered-monolith";
127
-
128
- const sharedKernelNames = new Set(["types", "shared", "common", "contracts"]);
129
- const coreNames = new Set(["core", "domain", "services", "usecase", "internal"]);
130
- const adapterNames = new Set([
131
- "cli",
132
- "mcp",
133
- "api",
134
- "controllers",
135
- "routes",
136
- "handlers",
137
- "delivery",
138
- ]);
139
-
140
- for (const sub of subdirs) {
141
- const relRoot = path.join("src", sub);
142
- const name = sub.toLowerCase();
143
- let allowed: string[] = [];
144
- let forbidden: string[] = [];
145
-
146
- if (sharedKernelNames.has(name)) {
147
- allowed = [];
148
- forbidden = Array.from(adapterNames)
149
- .concat(Array.from(coreNames))
150
- .filter((n) => subdirs.map((s) => s.toLowerCase()).includes(n));
151
- } else if (coreNames.has(name)) {
152
- allowed = subdirs
153
- .map((s) => s.toLowerCase())
154
- .filter((s) => sharedKernelNames.has(s));
155
- forbidden = subdirs
156
- .map((s) => s.toLowerCase())
157
- .filter((s) => adapterNames.has(s));
158
- } else {
159
- // Adapters
160
- allowed = subdirs
161
- .map((s) => s.toLowerCase())
162
- .filter((s) => sharedKernelNames.has(s) || coreNames.has(s));
163
- forbidden = [];
164
- }
165
-
166
- domains[name] = {
167
- root: relRoot,
168
- description: `${sub} layer`,
169
- allowed_dependencies: allowed,
170
- forbidden_dependencies: forbidden,
171
- archetypes: this.inferDefaultArchetypes(path.join(projectRoot, relRoot)),
172
- };
173
- }
174
- }
175
- }
176
-
177
- // 4. Framework-specific structural patterns (Laravel, Go)
178
- if (Object.keys(domains).length === 0) {
179
- if (language === "php" || framework.includes("laravel")) {
180
- const appDir = path.join(projectRoot, "app");
181
- if (fs.existsSync(appDir)) {
182
- architectureStyle = "layered-monolith";
183
- const subdirs = this.getSubdirectories(appDir);
184
- for (const sub of subdirs) {
185
- domains[sub.toLowerCase()] = {
186
- root: path.join("app", sub),
187
- description: `Laravel layer: ${sub}`,
188
- allowed_dependencies: [],
189
- forbidden_dependencies: [],
190
- archetypes: this.inferDefaultArchetypes(path.join(projectRoot, "app", sub)),
191
- };
192
- }
193
- }
194
- } else if (language === "go") {
195
- architectureStyle = "clean-architecture";
196
- for (const goDir of ["cmd", "internal", "pkg"]) {
197
- const full = path.join(projectRoot, goDir);
198
- if (fs.existsSync(full)) {
199
- domains[goDir] = {
200
- root: goDir,
201
- description: `Go layer: ${goDir}`,
202
- allowed_dependencies:
203
- goDir === "cmd"
204
- ? ["internal", "pkg"]
205
- : goDir === "internal"
206
- ? ["pkg"]
207
- : [],
208
- forbidden_dependencies: goDir === "pkg" ? ["internal", "cmd"] : [],
209
- archetypes: this.inferDefaultArchetypes(full),
210
- };
211
- }
212
- }
213
- }
214
- }
215
-
216
- // 5. Fallback: Top-level directories
217
- if (Object.keys(domains).length === 0) {
218
- const topDirs = this.getSubdirectories(projectRoot);
219
- for (const d of topDirs) {
220
- domains[d.toLowerCase()] = {
221
- root: d,
222
- description: `Top-level domain: ${d}`,
223
- allowed_dependencies: [],
224
- forbidden_dependencies: [],
225
- archetypes: this.inferDefaultArchetypes(path.join(projectRoot, d)),
226
- };
227
- }
228
- }
229
-
230
- const projectType = this.detectProjectType(projectRoot, framework, language, isMonorepo);
231
-
232
- return {
233
- framework,
234
- language,
235
- projectType,
236
- architectureStyle,
237
- domains,
238
- };
239
- }
240
-
241
- /**
242
- * Discovers project topology and persists domains and macro metadata directly into SQLite SSOT.
243
- */
244
- public static discoverAndPersist(
245
- repo: SeptumRepository,
246
- projectRoot: string = process.cwd()
247
- ): Record<string, DomainConfig> {
248
- const topology = this.detect(projectRoot);
249
-
250
- for (const [name, config] of Object.entries(topology.domains)) {
251
- repo.upsertDomain(name, config);
252
- }
253
-
254
- // Persist Macro Metadata SSOT
255
- repo.setMeta("project_type", topology.projectType);
256
- repo.setMeta("architecture_style", topology.architectureStyle);
257
- repo.setMeta("framework", topology.framework);
258
- repo.setMeta("language", topology.language);
259
- repo.setMeta(
260
- "macro_summary",
261
- JSON.stringify({
262
- projectType: topology.projectType,
263
- framework: topology.framework,
264
- architectureStyle: topology.architectureStyle,
265
- domainsCount: Object.keys(topology.domains).length,
266
- domains: Object.keys(topology.domains),
267
- })
268
- );
269
-
270
- return topology.domains;
271
- }
272
-
273
- private static detectMonorepo(projectRoot: string): boolean {
274
- if (
275
- fs.existsSync(path.join(projectRoot, "pnpm-workspace.yaml")) ||
276
- fs.existsSync(path.join(projectRoot, "lerna.json")) ||
277
- fs.existsSync(path.join(projectRoot, "turbo.json"))
278
- ) {
279
- return true;
280
- }
281
-
282
- const hasApps = fs.existsSync(path.join(projectRoot, "apps"));
283
- const hasPackages = fs.existsSync(path.join(projectRoot, "packages"));
284
- if (hasApps && hasPackages) return true;
285
-
286
- const pkgPath = path.join(projectRoot, "package.json");
287
- if (fs.existsSync(pkgPath)) {
288
- try {
289
- const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf-8"));
290
- if (pkg.workspaces && Array.isArray(pkg.workspaces) && pkg.workspaces.length > 0) {
291
- return true;
292
- }
293
- } catch (err: unknown) {
294
- const msg = err instanceof Error ? err.message : String(err);
295
- console.warn(`[Septum Topology] Could not parse ${pkgPath}: ${msg}`);
296
- }
297
- }
298
-
299
- return false;
300
- }
301
-
302
- private static detectProjectType(
303
- projectRoot: string,
304
- framework: string,
305
- language: string,
306
- isMonorepo: boolean
307
- ): ProjectType {
308
- if (isMonorepo) return "monorepo";
309
-
310
- if (framework === "mcp-server" || framework === "cli") return "cli-mcp";
311
-
312
- if (framework === "nextjs" || framework === "nuxt" || framework === "astro") {
313
- return "fullstack";
314
- }
315
-
316
- if (framework.includes("laravel")) {
317
- const hasJs = fs.existsSync(path.join(projectRoot, "resources/js"));
318
- const hasViews = fs.existsSync(path.join(projectRoot, "resources/views"));
319
- return hasJs || hasViews ? "fullstack" : "backend";
320
- }
321
-
322
- if (framework === "vue" || framework === "react") {
323
- return "frontend";
324
- }
325
-
326
- if (
327
- framework === "nestjs" ||
328
- framework === "express" ||
329
- framework === "fastify" ||
330
- language === "go" ||
331
- language === "python" ||
332
- language === "php"
333
- ) {
334
- return "backend";
335
- }
336
-
337
- return "generic";
338
- }
339
-
340
- private static inferDefaultArchetypes(fullDir: string): Record<string, string> {
341
- const archetypes: Record<string, string> = {};
342
- if (!fs.existsSync(fullDir)) return archetypes;
343
-
344
- try {
345
- const items = fs.readdirSync(fullDir, { withFileTypes: true });
346
- for (const it of items) {
347
- if (it.isDirectory() && !it.name.startsWith(".")) {
348
- const name = it.name.toLowerCase();
349
- archetypes[name] = `${it.name}/**`;
350
- }
351
- }
352
- if (Object.keys(archetypes).length === 0) {
353
- archetypes["source"] = "**/*.*";
354
- }
355
- } catch {}
356
-
357
- return archetypes;
358
- }
359
-
360
- private static detectLanguage(projectRoot: string): DiscoveredProjectTopology["language"] {
361
- if (fs.existsSync(path.join(projectRoot, "composer.json"))) return "php";
362
- if (fs.existsSync(path.join(projectRoot, "go.mod"))) return "go";
363
- if (
364
- fs.existsSync(path.join(projectRoot, "pyproject.toml")) ||
365
- fs.existsSync(path.join(projectRoot, "requirements.txt"))
366
- ) {
367
- return "python";
368
- }
369
- if (
370
- fs.existsSync(path.join(projectRoot, "tsconfig.json")) ||
371
- fs.existsSync(path.join(projectRoot, "package.json"))
372
- ) {
373
- return "typescript";
374
- }
375
- return "generic";
376
- }
377
-
378
- private static detectFramework(
379
- projectRoot: string,
380
- language: DiscoveredProjectTopology["language"]
381
- ): string {
382
- if (language === "php") {
383
- if (fs.existsSync(path.join(projectRoot, "artisan"))) return "laravel";
384
- return "generic-php";
385
- }
386
- if (language === "typescript" || language === "javascript") {
387
- try {
388
- const pkgJson = path.join(projectRoot, "package.json");
389
- if (fs.existsSync(pkgJson)) {
390
- const pkg = JSON.parse(fs.readFileSync(pkgJson, "utf-8"));
391
- const allDeps = { ...pkg.dependencies, ...pkg.devDependencies };
392
- if (allDeps.next) return "nextjs";
393
- if (allDeps.nuxt) return "nuxt";
394
- if (allDeps.astro) return "astro";
395
- if (allDeps.vue) return "vue";
396
- if (allDeps["@nestjs/core"]) return "nestjs";
397
- if (allDeps["@modelcontextprotocol/sdk"]) return "mcp-server";
398
- }
399
- } catch (err: unknown) {
400
- const msg = err instanceof Error ? err.message : String(err);
401
- console.warn(`[Septum Topology] Failed to inspect dependencies in ${pkgJson}: ${msg}`);
402
- }
403
- return "generic-node";
404
- }
405
- return "generic";
406
- }
407
-
408
- private static getSubdirectories(dir: string): string[] {
409
- try {
410
- return fs
411
- .readdirSync(dir, { withFileTypes: true })
412
- .filter((e) => e.isDirectory() && !this.IGNORED_DIRS.has(e.name) && !e.name.startsWith("."))
413
- .map((e) => e.name);
414
- } catch {
415
- return [];
416
- }
417
- }
418
- }
1
+ import * as fs from "node:fs";
2
+ import * as path from "node:path";
3
+ import type { DomainConfig } from "../../types/index.ts";
4
+ import type { SeptumRepository } from "../database/repository.ts";
5
+
6
+ export type ProjectType =
7
+ | "monorepo"
8
+ | "fullstack"
9
+ | "backend"
10
+ | "frontend"
11
+ | "cli-mcp"
12
+ | "generic";
13
+
14
+ export type ArchitectureStyle =
15
+ | "modular-ddd"
16
+ | "layered-monolith"
17
+ | "clean-architecture"
18
+ | "monorepo-workspaces"
19
+ | "cli-adapter"
20
+ | "generic";
21
+
22
+ export interface DiscoveredProjectTopology {
23
+ framework: string;
24
+ language: "typescript" | "javascript" | "php" | "python" | "go" | "generic";
25
+ projectType: ProjectType;
26
+ architectureStyle: ArchitectureStyle;
27
+ domains: Record<string, DomainConfig>;
28
+ }
29
+
30
+ export class TopologyDetector {
31
+ private static readonly IGNORED_DIRS = new Set([
32
+ "node_modules",
33
+ "vendor",
34
+ "dist",
35
+ "build",
36
+ ".git",
37
+ ".septum",
38
+ ".system_generated",
39
+ "tests",
40
+ "test",
41
+ "coverage",
42
+ ".vscode",
43
+ ".idea",
44
+ ".agents",
45
+ "bin",
46
+ ]);
47
+
48
+ /**
49
+ * Detects project topology, classifies project type & architecture style, and maps domains.
50
+ */
51
+ public static detect(projectRoot: string = process.cwd()): DiscoveredProjectTopology {
52
+ const language = this.detectLanguage(projectRoot);
53
+ const framework = this.detectFramework(projectRoot, language);
54
+ const isMonorepo = this.detectMonorepo(projectRoot);
55
+
56
+ const domains: Record<string, DomainConfig> = {};
57
+ let architectureStyle: ArchitectureStyle = "generic";
58
+
59
+ // 1. Monorepo Workspaces (apps/*, packages/*)
60
+ if (isMonorepo) {
61
+ architectureStyle = "monorepo-workspaces";
62
+ const workspaceDirs = ["apps", "packages", "modules"];
63
+ const appDomains: string[] = [];
64
+ const packageDomains: string[] = [];
65
+
66
+ for (const parent of workspaceDirs) {
67
+ const fullParent = path.join(projectRoot, parent);
68
+ if (fs.existsSync(fullParent) && fs.statSync(fullParent).isDirectory()) {
69
+ const subs = this.getSubdirectories(fullParent);
70
+ for (const sub of subs) {
71
+ const relRoot = path.join(parent, sub);
72
+ const domainName = sub.toLowerCase();
73
+ if (parent === "apps") appDomains.push(domainName);
74
+ else packageDomains.push(domainName);
75
+
76
+ domains[domainName] = {
77
+ root: relRoot,
78
+ description: `Monorepo ${parent} package: ${sub}`,
79
+ allowed_dependencies: parent === "apps" ? packageDomains : [],
80
+ forbidden_dependencies: parent !== "apps" ? appDomains : [],
81
+ archetypes: this.inferDefaultArchetypes(path.join(projectRoot, relRoot)),
82
+ };
83
+ }
84
+ }
85
+ }
86
+ }
87
+
88
+ // 2. Modular DDD (app/Domain, src/modules, src/domains, etc.)
89
+ if (Object.keys(domains).length === 0) {
90
+ const modularCandidates = [
91
+ { prefix: "domain", dir: path.join(projectRoot, "app", "Domain") },
92
+ { prefix: "domain", dir: path.join(projectRoot, "app", "Domains") },
93
+ { prefix: "domain", dir: path.join(projectRoot, "src", "domains") },
94
+ { prefix: "feature", dir: path.join(projectRoot, "src", "features") },
95
+ { prefix: "module", dir: path.join(projectRoot, "src", "modules") },
96
+ ];
97
+
98
+ for (const cand of modularCandidates) {
99
+ if (fs.existsSync(cand.dir) && fs.statSync(cand.dir).isDirectory()) {
100
+ const subdirs = this.getSubdirectories(cand.dir);
101
+ if (subdirs.length > 0) {
102
+ architectureStyle = "modular-ddd";
103
+ for (const sub of subdirs) {
104
+ const relRoot = path.relative(projectRoot, path.join(cand.dir, sub));
105
+ domains[sub.toLowerCase()] = {
106
+ root: relRoot,
107
+ description: `Modular ${cand.prefix} domain: ${sub}`,
108
+ allowed_dependencies: ["shared", "common"],
109
+ forbidden_dependencies: [],
110
+ archetypes: this.inferDefaultArchetypes(path.join(projectRoot, relRoot)),
111
+ };
112
+ }
113
+ }
114
+ }
115
+ }
116
+ }
117
+
118
+ // 3. Layered Monolith / Clean Architecture in src/
119
+ const srcDir = path.join(projectRoot, "src");
120
+ if (Object.keys(domains).length === 0 && fs.existsSync(srcDir) && fs.statSync(srcDir).isDirectory()) {
121
+ const subdirs = this.getSubdirectories(srcDir);
122
+ if (subdirs.length > 0) {
123
+ architectureStyle =
124
+ framework === "mcp-server" || framework === "cli"
125
+ ? "cli-adapter"
126
+ : "layered-monolith";
127
+
128
+ const sharedKernelNames = new Set(["types", "shared", "common", "contracts"]);
129
+ const coreNames = new Set(["core", "domain", "services", "usecase", "internal"]);
130
+ const adapterNames = new Set([
131
+ "cli",
132
+ "mcp",
133
+ "api",
134
+ "controllers",
135
+ "routes",
136
+ "handlers",
137
+ "delivery",
138
+ ]);
139
+
140
+ for (const sub of subdirs) {
141
+ const relRoot = path.join("src", sub);
142
+ const name = sub.toLowerCase();
143
+ let allowed: string[] = [];
144
+ let forbidden: string[] = [];
145
+
146
+ if (sharedKernelNames.has(name)) {
147
+ allowed = [];
148
+ forbidden = Array.from(adapterNames)
149
+ .concat(Array.from(coreNames))
150
+ .filter((n) => subdirs.map((s) => s.toLowerCase()).includes(n));
151
+ } else if (coreNames.has(name)) {
152
+ allowed = subdirs
153
+ .map((s) => s.toLowerCase())
154
+ .filter((s) => sharedKernelNames.has(s));
155
+ forbidden = subdirs
156
+ .map((s) => s.toLowerCase())
157
+ .filter((s) => adapterNames.has(s));
158
+ } else {
159
+ // Adapters
160
+ allowed = subdirs
161
+ .map((s) => s.toLowerCase())
162
+ .filter((s) => sharedKernelNames.has(s) || coreNames.has(s));
163
+ forbidden = [];
164
+ }
165
+
166
+ domains[name] = {
167
+ root: relRoot,
168
+ description: `${sub} layer`,
169
+ allowed_dependencies: allowed,
170
+ forbidden_dependencies: forbidden,
171
+ archetypes: this.inferDefaultArchetypes(path.join(projectRoot, relRoot)),
172
+ };
173
+ }
174
+ }
175
+ }
176
+
177
+ // 4. Framework-specific structural patterns (Laravel, Go)
178
+ if (Object.keys(domains).length === 0) {
179
+ if (language === "php" || framework.includes("laravel")) {
180
+ const appDir = path.join(projectRoot, "app");
181
+ if (fs.existsSync(appDir)) {
182
+ architectureStyle = "layered-monolith";
183
+ const subdirs = this.getSubdirectories(appDir);
184
+ for (const sub of subdirs) {
185
+ domains[sub.toLowerCase()] = {
186
+ root: path.join("app", sub),
187
+ description: `Laravel layer: ${sub}`,
188
+ allowed_dependencies: [],
189
+ forbidden_dependencies: [],
190
+ archetypes: this.inferDefaultArchetypes(path.join(projectRoot, "app", sub)),
191
+ };
192
+ }
193
+ }
194
+ } else if (language === "go") {
195
+ architectureStyle = "clean-architecture";
196
+ for (const goDir of ["cmd", "internal", "pkg"]) {
197
+ const full = path.join(projectRoot, goDir);
198
+ if (fs.existsSync(full)) {
199
+ domains[goDir] = {
200
+ root: goDir,
201
+ description: `Go layer: ${goDir}`,
202
+ allowed_dependencies:
203
+ goDir === "cmd"
204
+ ? ["internal", "pkg"]
205
+ : goDir === "internal"
206
+ ? ["pkg"]
207
+ : [],
208
+ forbidden_dependencies: goDir === "pkg" ? ["internal", "cmd"] : [],
209
+ archetypes: this.inferDefaultArchetypes(full),
210
+ };
211
+ }
212
+ }
213
+ }
214
+ }
215
+
216
+ // 5. Fallback: Top-level directories
217
+ if (Object.keys(domains).length === 0) {
218
+ const topDirs = this.getSubdirectories(projectRoot);
219
+ for (const d of topDirs) {
220
+ domains[d.toLowerCase()] = {
221
+ root: d,
222
+ description: `Top-level domain: ${d}`,
223
+ allowed_dependencies: [],
224
+ forbidden_dependencies: [],
225
+ archetypes: this.inferDefaultArchetypes(path.join(projectRoot, d)),
226
+ };
227
+ }
228
+ }
229
+
230
+ const projectType = this.detectProjectType(projectRoot, framework, language, isMonorepo);
231
+
232
+ return {
233
+ framework,
234
+ language,
235
+ projectType,
236
+ architectureStyle,
237
+ domains,
238
+ };
239
+ }
240
+
241
+ /**
242
+ * Discovers project topology and persists domains and macro metadata directly into SQLite SSOT.
243
+ */
244
+ public static discoverAndPersist(
245
+ repo: SeptumRepository,
246
+ projectRoot: string = process.cwd()
247
+ ): Record<string, DomainConfig> {
248
+ const topology = this.detect(projectRoot);
249
+
250
+ for (const [name, config] of Object.entries(topology.domains)) {
251
+ repo.upsertDomain(name, config);
252
+ }
253
+
254
+ // Persist Macro Metadata SSOT
255
+ repo.setMeta("project_type", topology.projectType);
256
+ repo.setMeta("architecture_style", topology.architectureStyle);
257
+ repo.setMeta("framework", topology.framework);
258
+ repo.setMeta("language", topology.language);
259
+ repo.setMeta(
260
+ "macro_summary",
261
+ JSON.stringify({
262
+ projectType: topology.projectType,
263
+ framework: topology.framework,
264
+ architectureStyle: topology.architectureStyle,
265
+ domainsCount: Object.keys(topology.domains).length,
266
+ domains: Object.keys(topology.domains),
267
+ })
268
+ );
269
+
270
+ return topology.domains;
271
+ }
272
+
273
+ private static detectMonorepo(projectRoot: string): boolean {
274
+ if (
275
+ fs.existsSync(path.join(projectRoot, "pnpm-workspace.yaml")) ||
276
+ fs.existsSync(path.join(projectRoot, "lerna.json")) ||
277
+ fs.existsSync(path.join(projectRoot, "turbo.json"))
278
+ ) {
279
+ return true;
280
+ }
281
+
282
+ const hasApps = fs.existsSync(path.join(projectRoot, "apps"));
283
+ const hasPackages = fs.existsSync(path.join(projectRoot, "packages"));
284
+ if (hasApps && hasPackages) return true;
285
+
286
+ const pkgPath = path.join(projectRoot, "package.json");
287
+ if (fs.existsSync(pkgPath)) {
288
+ try {
289
+ const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf-8"));
290
+ if (pkg.workspaces && Array.isArray(pkg.workspaces) && pkg.workspaces.length > 0) {
291
+ return true;
292
+ }
293
+ } catch (err: unknown) {
294
+ const msg = err instanceof Error ? err.message : String(err);
295
+ console.warn(`[Septum Topology] Could not parse ${pkgPath}: ${msg}`);
296
+ }
297
+ }
298
+
299
+ return false;
300
+ }
301
+
302
+ private static detectProjectType(
303
+ projectRoot: string,
304
+ framework: string,
305
+ language: string,
306
+ isMonorepo: boolean
307
+ ): ProjectType {
308
+ if (isMonorepo) return "monorepo";
309
+
310
+ if (framework === "mcp-server" || framework === "cli") return "cli-mcp";
311
+
312
+ if (framework === "nextjs" || framework === "nuxt" || framework === "astro") {
313
+ return "fullstack";
314
+ }
315
+
316
+ if (framework.includes("laravel")) {
317
+ const hasJs = fs.existsSync(path.join(projectRoot, "resources/js"));
318
+ const hasViews = fs.existsSync(path.join(projectRoot, "resources/views"));
319
+ return hasJs || hasViews ? "fullstack" : "backend";
320
+ }
321
+
322
+ if (framework === "vue" || framework === "react") {
323
+ return "frontend";
324
+ }
325
+
326
+ if (
327
+ framework === "nestjs" ||
328
+ framework === "express" ||
329
+ framework === "fastify" ||
330
+ language === "go" ||
331
+ language === "python" ||
332
+ language === "php"
333
+ ) {
334
+ return "backend";
335
+ }
336
+
337
+ return "generic";
338
+ }
339
+
340
+ private static inferDefaultArchetypes(fullDir: string): Record<string, string> {
341
+ const archetypes: Record<string, string> = {};
342
+ if (!fs.existsSync(fullDir)) return archetypes;
343
+
344
+ try {
345
+ const items = fs.readdirSync(fullDir, { withFileTypes: true });
346
+ for (const it of items) {
347
+ if (it.isDirectory() && !it.name.startsWith(".")) {
348
+ const name = it.name.toLowerCase();
349
+ archetypes[name] = `${it.name}/**`;
350
+ }
351
+ }
352
+ if (Object.keys(archetypes).length === 0) {
353
+ archetypes["source"] = "**/*.*";
354
+ }
355
+ } catch {}
356
+
357
+ return archetypes;
358
+ }
359
+
360
+ private static detectLanguage(projectRoot: string): DiscoveredProjectTopology["language"] {
361
+ if (fs.existsSync(path.join(projectRoot, "composer.json"))) return "php";
362
+ if (fs.existsSync(path.join(projectRoot, "go.mod"))) return "go";
363
+ if (
364
+ fs.existsSync(path.join(projectRoot, "pyproject.toml")) ||
365
+ fs.existsSync(path.join(projectRoot, "requirements.txt"))
366
+ ) {
367
+ return "python";
368
+ }
369
+ if (
370
+ fs.existsSync(path.join(projectRoot, "tsconfig.json")) ||
371
+ fs.existsSync(path.join(projectRoot, "package.json"))
372
+ ) {
373
+ return "typescript";
374
+ }
375
+ return "generic";
376
+ }
377
+
378
+ private static detectFramework(
379
+ projectRoot: string,
380
+ language: DiscoveredProjectTopology["language"]
381
+ ): string {
382
+ if (language === "php") {
383
+ if (fs.existsSync(path.join(projectRoot, "artisan"))) return "laravel";
384
+ return "generic-php";
385
+ }
386
+ if (language === "typescript" || language === "javascript") {
387
+ try {
388
+ const pkgJson = path.join(projectRoot, "package.json");
389
+ if (fs.existsSync(pkgJson)) {
390
+ const pkg = JSON.parse(fs.readFileSync(pkgJson, "utf-8"));
391
+ const allDeps = { ...pkg.dependencies, ...pkg.devDependencies };
392
+ if (allDeps.next) return "nextjs";
393
+ if (allDeps.nuxt) return "nuxt";
394
+ if (allDeps.astro) return "astro";
395
+ if (allDeps.vue) return "vue";
396
+ if (allDeps["@nestjs/core"]) return "nestjs";
397
+ if (allDeps["@modelcontextprotocol/sdk"]) return "mcp-server";
398
+ }
399
+ } catch (err: unknown) {
400
+ const msg = err instanceof Error ? err.message : String(err);
401
+ console.warn(`[Septum Topology] Failed to inspect dependencies in ${pkgJson}: ${msg}`);
402
+ }
403
+ return "generic-node";
404
+ }
405
+ return "generic";
406
+ }
407
+
408
+ private static getSubdirectories(dir: string): string[] {
409
+ try {
410
+ return fs
411
+ .readdirSync(dir, { withFileTypes: true })
412
+ .filter((e) => e.isDirectory() && !this.IGNORED_DIRS.has(e.name) && !e.name.startsWith("."))
413
+ .map((e) => e.name);
414
+ } catch {
415
+ return [];
416
+ }
417
+ }
418
+ }