septum 0.1.2 → 0.1.4

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 (46) hide show
  1. package/README.md +7 -2
  2. package/package.json +1 -1
  3. package/src/cli/commands/slice.ts +4 -46
  4. package/src/cli/commands/topology.ts +119 -0
  5. package/src/cli/index.ts +11 -0
  6. package/src/cli/templates/skill.ts +7 -4
  7. package/src/core/config/loader.ts +21 -8
  8. package/src/core/database/repositories/dependency.repository.ts +70 -0
  9. package/src/core/database/repositories/environment.repository.ts +207 -0
  10. package/src/core/database/repositories/file.repository.ts +7 -0
  11. package/src/core/database/repositories/symbol.repository.ts +28 -9
  12. package/src/core/database/repository.ts +19 -1
  13. package/src/core/database/schema.ts +153 -120
  14. package/src/core/discovery/topology-detector.ts +71 -3
  15. package/src/core/ingestion/pipeline.ts +98 -1
  16. package/src/core/parser/behavioral-analyzer.ts +119 -0
  17. package/src/core/parser/boundary-tracker.ts +160 -0
  18. package/src/core/parser/extractors/base.ts +2 -0
  19. package/src/core/parser/extractors/blade.ts +158 -0
  20. package/src/core/parser/extractors/go.ts +5 -1
  21. package/src/core/parser/extractors/laravel-semantic.ts +506 -31
  22. package/src/core/parser/extractors/php.ts +5 -1
  23. package/src/core/parser/extractors/python.ts +3 -1
  24. package/src/core/parser/extractors/registry.ts +21 -0
  25. package/src/core/parser/extractors/typescript.ts +5 -1
  26. package/src/core/parser/schema/providers/laravel-migration-provider.ts +215 -0
  27. package/src/core/parser/schema/providers/prisma-schema-provider.ts +89 -0
  28. package/src/core/parser/schema/schema-provider-registry.ts +43 -0
  29. package/src/core/parser/schema/schema-provider.interface.ts +33 -0
  30. package/src/core/parser/tree-sitter.ts +2 -0
  31. package/src/core/resolver/path-resolver.ts +147 -0
  32. package/src/core/resolver/vertical-slice-tracer.ts +56 -8
  33. package/src/core/topology/environment-detector.ts +860 -0
  34. package/src/core/topology/types.ts +83 -0
  35. package/src/index.ts +3 -0
  36. package/src/mcp/schemas.ts +264 -46
  37. package/src/mcp/server.ts +130 -26
  38. package/src/mcp/tools/check-boundary.ts +58 -8
  39. package/src/mcp/tools/get-domain-catalog.ts +31 -1
  40. package/src/mcp/tools/get-environment-topology.ts +141 -0
  41. package/src/mcp/tools/get-symbol-hotspots.ts +13 -3
  42. package/src/mcp/tools/locate-symbol.ts +119 -3
  43. package/src/mcp/tools/register-domain.ts +1 -1
  44. package/src/mcp/tools/trace-vertical-slice.ts +11 -65
  45. package/src/types/index.ts +20 -1
  46. package/src/version.ts +3 -3
@@ -17,12 +17,13 @@ export class SymbolRepository {
17
17
  if (symbols.length === 0) return;
18
18
 
19
19
  const stmt = this.db.query(
20
- `INSERT INTO symbols (file_id, name, kind, signature, visibility, line_start, line_end, line_count)
21
- VALUES (?, ?, ?, ?, ?, ?, ?, ?)`
20
+ `INSERT INTO symbols (file_id, name, kind, signature, visibility, line_start, line_end, line_count, nesting_depth)
21
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`
22
22
  );
23
23
 
24
24
  for (const sym of symbols) {
25
25
  const lineCount = sym.line_count ?? Math.max(1, sym.line_end - sym.line_start + 1);
26
+ const nestingDepth = sym.nesting_depth ?? 1;
26
27
  stmt.run(
27
28
  fileId,
28
29
  sym.name,
@@ -31,7 +32,8 @@ export class SymbolRepository {
31
32
  sym.visibility,
32
33
  sym.line_start,
33
34
  sym.line_end,
34
- lineCount
35
+ lineCount,
36
+ nestingDepth
35
37
  );
36
38
  }
37
39
  }
@@ -98,6 +100,7 @@ export class SymbolRepository {
98
100
  line_start: r.line_start,
99
101
  line_end: r.line_end,
100
102
  line_count: r.line_count ?? (r.line_end - r.line_start + 1),
103
+ nesting_depth: r.nesting_depth ?? 1,
101
104
  },
102
105
  }));
103
106
  }
@@ -225,12 +228,16 @@ export class SymbolRepository {
225
228
 
226
229
  public getHotspotSymbols(options: {
227
230
  minLines?: number;
231
+ minNesting?: number;
228
232
  kind?: SymbolKind;
229
233
  domain?: string;
230
234
  limit?: number;
231
- } = {}): Array<SymbolRecord & { file_path: string; domain_name: string }> {
232
- const minLines = options.minLines ?? 30;
235
+ sortBy?: "lines" | "nesting" | "risk_score";
236
+ } = {}): Array<SymbolRecord & { file_path: string; domain_name: string; risk_score: number }> {
237
+ const minLines = options.minLines ?? 0;
238
+ const minNesting = options.minNesting ?? 1;
233
239
  const limit = options.limit ?? 50;
240
+ const sortBy = options.sortBy ?? "risk_score";
234
241
 
235
242
  let sql = `
236
243
  SELECT
@@ -243,15 +250,16 @@ export class SymbolRepository {
243
250
  s.line_start,
244
251
  s.line_end,
245
252
  s.line_count,
253
+ s.nesting_depth,
246
254
  f.path AS file_path,
247
255
  d.name AS domain_name
248
256
  FROM symbols s
249
257
  JOIN files f ON s.file_id = f.id
250
258
  JOIN domains d ON f.domain_id = d.id
251
- WHERE s.line_count >= ?
259
+ WHERE s.line_count >= ? AND s.nesting_depth >= ?
252
260
  `;
253
261
 
254
- const params: any[] = [minLines];
262
+ const params: any[] = [minLines, minNesting];
255
263
 
256
264
  if (options.kind) {
257
265
  sql += " AND s.kind = ?";
@@ -263,10 +271,21 @@ export class SymbolRepository {
263
271
  params.push(options.domain);
264
272
  }
265
273
 
266
- sql += " ORDER BY s.line_count DESC, s.name ASC LIMIT ?";
274
+ if (sortBy === "nesting") {
275
+ sql += " ORDER BY s.nesting_depth DESC, s.line_count DESC, s.name ASC LIMIT ?";
276
+ } else if (sortBy === "lines") {
277
+ sql += " ORDER BY s.line_count DESC, s.nesting_depth DESC, s.name ASC LIMIT ?";
278
+ } else {
279
+ sql += " ORDER BY (s.line_count * (1.0 + s.nesting_depth * 0.3)) DESC, s.nesting_depth DESC, s.name ASC LIMIT ?";
280
+ }
267
281
  params.push(limit);
268
282
 
269
- return this.db.query<any, any[]>(sql).all(...params);
283
+ const rows = this.db.query<any, any[]>(sql).all(...params);
284
+ return rows.map((r) => ({
285
+ ...r,
286
+ nesting_depth: r.nesting_depth ?? 1,
287
+ risk_score: Math.round((r.line_count ?? 1) * (1.0 + (r.nesting_depth ?? 1) * 0.3)),
288
+ }));
270
289
  }
271
290
 
272
291
  public getSymbolDetails(
@@ -32,6 +32,7 @@ export type {
32
32
  } from "../../types/index.ts";
33
33
  import { DependencyRepository } from "./repositories/dependency.repository.ts";
34
34
  import { DomainRepository } from "./repositories/domain.repository.ts";
35
+ import { EnvironmentRepository } from "./repositories/environment.repository.ts";
35
36
  import { FileRepository } from "./repositories/file.repository.ts";
36
37
  import { MetaRepository } from "./repositories/meta.repository.ts";
37
38
  import { SymbolRepository } from "./repositories/symbol.repository.ts";
@@ -44,6 +45,7 @@ export class SeptumRepository {
44
45
  public readonly symbols: SymbolRepository;
45
46
  public readonly dependencies: DependencyRepository;
46
47
  public readonly meta: MetaRepository;
48
+ public readonly environment: EnvironmentRepository;
47
49
 
48
50
  constructor(private db: Database) {
49
51
  this.domains = new DomainRepository(db);
@@ -51,6 +53,7 @@ export class SeptumRepository {
51
53
  this.symbols = new SymbolRepository(db);
52
54
  this.dependencies = new DependencyRepository(db);
53
55
  this.meta = new MetaRepository(db);
56
+ this.environment = new EnvironmentRepository(db);
54
57
  }
55
58
 
56
59
  public runInTransaction<T>(fn: () => T): T {
@@ -75,6 +78,10 @@ export class SeptumRepository {
75
78
  return this.files.getFileByPath(path);
76
79
  }
77
80
 
81
+ public countFiles(): number {
82
+ return this.files.countFiles();
83
+ }
84
+
78
85
  public getFilesByDomain(domainId: number): FileRecord[] {
79
86
  return this.files.getFilesByDomain(domainId);
80
87
  }
@@ -208,10 +215,12 @@ export class SeptumRepository {
208
215
 
209
216
  public getHotspotSymbols(options: {
210
217
  minLines?: number;
218
+ minNesting?: number;
211
219
  kind?: SymbolKind;
212
220
  domain?: string;
213
221
  limit?: number;
214
- } = {}): Array<SymbolRecord & { file_path: string; domain_name: string }> {
222
+ sortBy?: "lines" | "nesting" | "risk_score";
223
+ } = {}): Array<SymbolRecord & { file_path: string; domain_name: string; risk_score: number }> {
215
224
  return this.symbols.getHotspotSymbols(options);
216
225
  }
217
226
 
@@ -226,6 +235,15 @@ export class SeptumRepository {
226
235
  return this.dependencies.getSymbolImpact(symbolName);
227
236
  }
228
237
 
238
+ public getFileInboundImpact(filePath: string): {
239
+ direct_dependents_count: number;
240
+ top_consumers: string[];
241
+ symbols_count: number;
242
+ risk_level: "low" | "medium" | "high";
243
+ } {
244
+ return this.dependencies.getFileInboundImpact(filePath);
245
+ }
246
+
229
247
  // --- Metadata Methods ---
230
248
  public getMeta(key: string): string | null {
231
249
  return this.meta.getMeta(key);
@@ -1,120 +1,153 @@
1
- import type { Database } from "bun:sqlite";
2
-
3
- export const CURRENT_SCHEMA_VERSION = 2;
4
-
5
- export const TABLE_STATEMENTS = [
6
- `CREATE TABLE IF NOT EXISTS domains (
7
- id INTEGER PRIMARY KEY AUTOINCREMENT,
8
- name TEXT NOT NULL UNIQUE,
9
- root_path TEXT NOT NULL,
10
- allowed_deps_json TEXT NOT NULL DEFAULT '[]',
11
- forbidden_deps_json TEXT NOT NULL DEFAULT '[]',
12
- archetypes_json TEXT NOT NULL DEFAULT '{}',
13
- created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
14
- updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
15
- );`,
16
-
17
- `CREATE TABLE IF NOT EXISTS files (
18
- id INTEGER PRIMARY KEY AUTOINCREMENT,
19
- domain_id INTEGER NOT NULL,
20
- path TEXT NOT NULL UNIQUE,
21
- archetype TEXT NOT NULL DEFAULT 'unknown',
22
- content_hash TEXT NOT NULL,
23
- line_count INTEGER NOT NULL DEFAULT 0,
24
- mtime_ms INTEGER NOT NULL DEFAULT 0,
25
- size_bytes INTEGER NOT NULL DEFAULT 0,
26
- last_scanned_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
27
- FOREIGN KEY (domain_id) REFERENCES domains(id) ON DELETE CASCADE
28
- );`,
29
-
30
- `CREATE TABLE IF NOT EXISTS symbols (
31
- id INTEGER PRIMARY KEY AUTOINCREMENT,
32
- file_id INTEGER NOT NULL,
33
- name TEXT NOT NULL,
34
- kind TEXT NOT NULL,
35
- signature TEXT NOT NULL,
36
- visibility TEXT NOT NULL DEFAULT 'public',
37
- line_start INTEGER NOT NULL DEFAULT 0,
38
- line_end INTEGER NOT NULL DEFAULT 0,
39
- line_count INTEGER NOT NULL DEFAULT 1,
40
- FOREIGN KEY (file_id) REFERENCES files(id) ON DELETE CASCADE
41
- );`,
42
-
43
- `CREATE TABLE IF NOT EXISTS dependencies (
44
- id INTEGER PRIMARY KEY AUTOINCREMENT,
45
- source_file_id INTEGER NOT NULL,
46
- target_symbol_or_path TEXT NOT NULL,
47
- import_statement TEXT NOT NULL,
48
- line_number INTEGER NOT NULL DEFAULT 0,
49
- is_external BOOLEAN NOT NULL DEFAULT 0,
50
- FOREIGN KEY (source_file_id) REFERENCES files(id) ON DELETE CASCADE
51
- );`,
52
-
53
- `CREATE TABLE IF NOT EXISTS vertical_slices (
54
- id INTEGER PRIMARY KEY AUTOINCREMENT,
55
- domain_id INTEGER,
56
- feature_key TEXT,
57
- http_method TEXT NOT NULL,
58
- route_uri TEXT NOT NULL,
59
- route_name TEXT,
60
- controller_class TEXT NOT NULL,
61
- action_name TEXT NOT NULL,
62
- controller_file TEXT,
63
- controller_line INTEGER DEFAULT 0,
64
- architecture_style TEXT NOT NULL DEFAULT 'clean',
65
- entry_kind TEXT NOT NULL DEFAULT 'http_route',
66
- execution_chain_json TEXT NOT NULL,
67
- created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
68
- FOREIGN KEY (domain_id) REFERENCES domains(id) ON DELETE CASCADE
69
- );`,
70
-
71
- `CREATE TABLE IF NOT EXISTS repo_meta (
72
- key TEXT PRIMARY KEY,
73
- value TEXT NOT NULL,
74
- updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
75
- );`,
76
- ];
77
-
78
- export const INDEX_STATEMENTS = [
79
- "CREATE INDEX IF NOT EXISTS idx_domains_name ON domains(name);",
80
- "CREATE INDEX IF NOT EXISTS idx_files_domain_id ON files(domain_id);",
81
- "CREATE INDEX IF NOT EXISTS idx_files_path ON files(path);",
82
- "CREATE INDEX IF NOT EXISTS idx_symbols_file_id ON symbols(file_id);",
83
- "CREATE INDEX IF NOT EXISTS idx_symbols_name ON symbols(name);",
84
- "CREATE INDEX IF NOT EXISTS idx_symbols_kind ON symbols(kind);",
85
- "CREATE INDEX IF NOT EXISTS idx_symbols_line_count ON symbols(kind, line_count DESC);",
86
- "CREATE INDEX IF NOT EXISTS idx_dependencies_source ON dependencies(source_file_id);",
87
- "CREATE INDEX IF NOT EXISTS idx_dependencies_target ON dependencies(target_symbol_or_path);",
88
- "CREATE INDEX IF NOT EXISTS idx_slices_route_uri ON vertical_slices(route_uri);",
89
- "CREATE INDEX IF NOT EXISTS idx_slices_route_name ON vertical_slices(route_name);",
90
- "CREATE INDEX IF NOT EXISTS idx_slices_controller ON vertical_slices(controller_class, action_name);",
91
- ];
92
-
93
- const DROP_ALL_TABLES = `
94
- DROP TABLE IF EXISTS repo_meta;
95
- DROP TABLE IF EXISTS vertical_slices;
96
- DROP TABLE IF EXISTS dependencies;
97
- DROP TABLE IF EXISTS symbols;
98
- DROP TABLE IF EXISTS files;
99
- DROP TABLE IF EXISTS domains;
100
- `;
101
-
102
- export function initializeDatabaseSchema(db: Database): void {
103
- const versionRow = db.query<{ user_version: number }, []>("PRAGMA user_version;").get();
104
- const userVersion = versionRow?.user_version ?? 0;
105
-
106
- if (userVersion !== 0 && userVersion !== CURRENT_SCHEMA_VERSION) {
107
- // Zero-kludge cache invalidation: outdated schema versions are cleanly dropped and recreated
108
- db.run(DROP_ALL_TABLES);
109
- }
110
-
111
- for (const tableSql of TABLE_STATEMENTS) {
112
- db.run(tableSql);
113
- }
114
-
115
- for (const indexSql of INDEX_STATEMENTS) {
116
- db.run(indexSql);
117
- }
118
-
119
- db.run(`PRAGMA user_version = ${CURRENT_SCHEMA_VERSION};`);
120
- }
1
+ import type { Database } from "bun:sqlite";
2
+
3
+ export const CURRENT_SCHEMA_VERSION = 4;
4
+
5
+ export const TABLE_STATEMENTS = [
6
+ `CREATE TABLE IF NOT EXISTS domains (
7
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
8
+ name TEXT NOT NULL UNIQUE,
9
+ root_path TEXT NOT NULL,
10
+ allowed_deps_json TEXT NOT NULL DEFAULT '[]',
11
+ forbidden_deps_json TEXT NOT NULL DEFAULT '[]',
12
+ archetypes_json TEXT NOT NULL DEFAULT '{}',
13
+ created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
14
+ updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
15
+ );`,
16
+
17
+ `CREATE TABLE IF NOT EXISTS files (
18
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
19
+ domain_id INTEGER NOT NULL,
20
+ path TEXT NOT NULL UNIQUE,
21
+ archetype TEXT NOT NULL DEFAULT 'unknown',
22
+ content_hash TEXT NOT NULL,
23
+ line_count INTEGER NOT NULL DEFAULT 0,
24
+ mtime_ms INTEGER NOT NULL DEFAULT 0,
25
+ size_bytes INTEGER NOT NULL DEFAULT 0,
26
+ last_scanned_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
27
+ FOREIGN KEY (domain_id) REFERENCES domains(id) ON DELETE CASCADE
28
+ );`,
29
+
30
+ `CREATE TABLE IF NOT EXISTS symbols (
31
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
32
+ file_id INTEGER NOT NULL,
33
+ name TEXT NOT NULL,
34
+ kind TEXT NOT NULL,
35
+ signature TEXT NOT NULL,
36
+ visibility TEXT NOT NULL DEFAULT 'public',
37
+ line_start INTEGER NOT NULL DEFAULT 0,
38
+ line_end INTEGER NOT NULL DEFAULT 0,
39
+ line_count INTEGER NOT NULL DEFAULT 1,
40
+ nesting_depth INTEGER NOT NULL DEFAULT 1,
41
+ FOREIGN KEY (file_id) REFERENCES files(id) ON DELETE CASCADE
42
+ );`,
43
+
44
+ `CREATE TABLE IF NOT EXISTS dependencies (
45
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
46
+ source_file_id INTEGER NOT NULL,
47
+ target_symbol_or_path TEXT NOT NULL,
48
+ import_statement TEXT NOT NULL,
49
+ line_number INTEGER NOT NULL DEFAULT 0,
50
+ is_external BOOLEAN NOT NULL DEFAULT 0,
51
+ FOREIGN KEY (source_file_id) REFERENCES files(id) ON DELETE CASCADE
52
+ );`,
53
+
54
+ `CREATE TABLE IF NOT EXISTS vertical_slices (
55
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
56
+ domain_id INTEGER,
57
+ feature_key TEXT,
58
+ http_method TEXT NOT NULL,
59
+ route_uri TEXT NOT NULL,
60
+ route_name TEXT,
61
+ controller_class TEXT NOT NULL,
62
+ action_name TEXT NOT NULL,
63
+ controller_file TEXT,
64
+ controller_line INTEGER DEFAULT 0,
65
+ architecture_style TEXT NOT NULL DEFAULT 'clean',
66
+ entry_kind TEXT NOT NULL DEFAULT 'http_route',
67
+ execution_chain_json TEXT NOT NULL,
68
+ created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
69
+ FOREIGN KEY (domain_id) REFERENCES domains(id) ON DELETE CASCADE
70
+ );`,
71
+
72
+ `CREATE TABLE IF NOT EXISTS repo_meta (
73
+ key TEXT PRIMARY KEY,
74
+ value TEXT NOT NULL,
75
+ updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
76
+ );`,
77
+
78
+ `CREATE TABLE IF NOT EXISTS environment_topology (
79
+ layer TEXT PRIMARY KEY,
80
+ platform TEXT,
81
+ status TEXT NOT NULL,
82
+ detected_from_json TEXT NOT NULL DEFAULT '[]',
83
+ constraints_json TEXT NOT NULL DEFAULT '[]',
84
+ unresolved_inquiry TEXT,
85
+ updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
86
+ );`,
87
+
88
+ `CREATE TABLE IF NOT EXISTS environment_nodes (
89
+ id TEXT PRIMARY KEY,
90
+ layer TEXT NOT NULL,
91
+ name TEXT NOT NULL,
92
+ platform TEXT NOT NULL,
93
+ canonical_tag TEXT NOT NULL DEFAULT 'unspecified',
94
+ domain_or_ip TEXT,
95
+ role TEXT,
96
+ status TEXT NOT NULL,
97
+ detected_from_json TEXT NOT NULL DEFAULT '[]',
98
+ constraints_json TEXT NOT NULL DEFAULT '[]',
99
+ connected_to_json TEXT NOT NULL DEFAULT '[]',
100
+ unresolved_inquiry TEXT,
101
+ updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
102
+ );`,
103
+ ];
104
+
105
+ export const INDEX_STATEMENTS = [
106
+ "CREATE INDEX IF NOT EXISTS idx_domains_name ON domains(name);",
107
+ "CREATE INDEX IF NOT EXISTS idx_files_domain_id ON files(domain_id);",
108
+ "CREATE INDEX IF NOT EXISTS idx_files_path ON files(path);",
109
+ "CREATE INDEX IF NOT EXISTS idx_symbols_file_id ON symbols(file_id);",
110
+ "CREATE INDEX IF NOT EXISTS idx_symbols_name ON symbols(name);",
111
+ "CREATE INDEX IF NOT EXISTS idx_symbols_kind ON symbols(kind);",
112
+ "CREATE INDEX IF NOT EXISTS idx_symbols_line_count ON symbols(kind, line_count DESC);",
113
+ "CREATE INDEX IF NOT EXISTS idx_symbols_nesting_depth ON symbols(nesting_depth DESC);",
114
+ "CREATE INDEX IF NOT EXISTS idx_dependencies_source ON dependencies(source_file_id);",
115
+ "CREATE INDEX IF NOT EXISTS idx_dependencies_target ON dependencies(target_symbol_or_path);",
116
+ "CREATE INDEX IF NOT EXISTS idx_slices_route_uri ON vertical_slices(route_uri);",
117
+ "CREATE INDEX IF NOT EXISTS idx_slices_route_name ON vertical_slices(route_name);",
118
+ "CREATE INDEX IF NOT EXISTS idx_slices_controller ON vertical_slices(controller_class, action_name);",
119
+ "CREATE INDEX IF NOT EXISTS idx_env_topology_status ON environment_topology(status);",
120
+ "CREATE INDEX IF NOT EXISTS idx_env_nodes_layer ON environment_nodes(layer);",
121
+ "CREATE INDEX IF NOT EXISTS idx_env_nodes_status ON environment_nodes(status);",
122
+ ];
123
+
124
+ const DROP_ALL_TABLES = `
125
+ DROP TABLE IF EXISTS environment_nodes;
126
+ DROP TABLE IF EXISTS environment_topology;
127
+ DROP TABLE IF EXISTS repo_meta;
128
+ DROP TABLE IF EXISTS vertical_slices;
129
+ DROP TABLE IF EXISTS dependencies;
130
+ DROP TABLE IF EXISTS symbols;
131
+ DROP TABLE IF EXISTS files;
132
+ DROP TABLE IF EXISTS domains;
133
+ `;
134
+
135
+ export function initializeDatabaseSchema(db: Database): void {
136
+ const versionRow = db.query<{ user_version: number }, []>("PRAGMA user_version;").get();
137
+ const userVersion = versionRow?.user_version ?? 0;
138
+
139
+ if (userVersion !== 0 && userVersion !== CURRENT_SCHEMA_VERSION) {
140
+ // Zero-kludge cache invalidation: outdated schema versions are cleanly dropped and recreated
141
+ db.run(DROP_ALL_TABLES);
142
+ }
143
+
144
+ for (const tableSql of TABLE_STATEMENTS) {
145
+ db.run(tableSql);
146
+ }
147
+
148
+ for (const indexSql of INDEX_STATEMENTS) {
149
+ db.run(indexSql);
150
+ }
151
+
152
+ db.run(`PRAGMA user_version = ${CURRENT_SCHEMA_VERSION};`);
153
+ }
@@ -43,8 +43,74 @@ export class TopologyDetector {
43
43
  ".idea",
44
44
  ".agents",
45
45
  "bin",
46
+ "locales",
47
+ "resources",
48
+ "public",
49
+ "assets",
50
+ "static",
51
+ ".output",
52
+ ".nuxt",
53
+ ".next",
54
+ ".turbo",
55
+ ".cache",
46
56
  ]);
47
57
 
58
+ private static readonly SOURCE_EXTENSIONS = new Set([
59
+ ".ts",
60
+ ".js",
61
+ ".tsx",
62
+ ".jsx",
63
+ ".php",
64
+ ".py",
65
+ ".go",
66
+ ".rs",
67
+ ".vue",
68
+ ]);
69
+
70
+ /**
71
+ * Reads compilerOptions.paths from tsconfig.json or jsconfig.json.
72
+ */
73
+ public static readTsConfigPaths(projectRoot: string): Record<string, string[]> {
74
+ for (const file of ["tsconfig.json", "jsconfig.json"]) {
75
+ const fullPath = path.join(projectRoot, file);
76
+ if (fs.existsSync(fullPath)) {
77
+ try {
78
+ const raw = fs.readFileSync(fullPath, "utf-8");
79
+ const stripped = raw.replace(/\/\*[\s\S]*?\*\/|\/\/.*/g, "");
80
+ const parsed = JSON.parse(stripped);
81
+ if (parsed.compilerOptions?.paths) {
82
+ return parsed.compilerOptions.paths;
83
+ }
84
+ } catch {
85
+ // Ignore tsconfig parse error
86
+ }
87
+ }
88
+ }
89
+ return {};
90
+ }
91
+
92
+ /**
93
+ * Checks whether a directory recursively contains any source code files.
94
+ */
95
+ private static hasSourceFiles(dir: string): boolean {
96
+ try {
97
+ const entries = fs.readdirSync(dir, { withFileTypes: true });
98
+ for (const entry of entries) {
99
+ if (entry.name.startsWith(".") || this.IGNORED_DIRS.has(entry.name)) continue;
100
+ const full = path.join(dir, entry.name);
101
+ if (entry.isFile()) {
102
+ const ext = path.extname(entry.name).toLowerCase();
103
+ if (this.SOURCE_EXTENSIONS.has(ext)) return true;
104
+ } else if (entry.isDirectory()) {
105
+ if (this.hasSourceFiles(full)) return true;
106
+ }
107
+ }
108
+ } catch {
109
+ // Ignore read errors
110
+ }
111
+ return false;
112
+ }
113
+
48
114
  /**
49
115
  * Detects project topology, classifies project type & architecture style, and maps domains.
50
116
  */
@@ -213,9 +279,11 @@ export class TopologyDetector {
213
279
  }
214
280
  }
215
281
 
216
- // 5. Fallback: Top-level directories
282
+ // 5. Fallback: Top-level directories that contain source code
217
283
  if (Object.keys(domains).length === 0) {
218
- const topDirs = this.getSubdirectories(projectRoot);
284
+ const topDirs = this.getSubdirectories(projectRoot).filter((d) =>
285
+ this.hasSourceFiles(path.join(projectRoot, d))
286
+ );
219
287
  for (const d of topDirs) {
220
288
  domains[d.toLowerCase()] = {
221
289
  root: d,
@@ -384,8 +452,8 @@ export class TopologyDetector {
384
452
  return "generic-php";
385
453
  }
386
454
  if (language === "typescript" || language === "javascript") {
455
+ const pkgJson = path.join(projectRoot, "package.json");
387
456
  try {
388
- const pkgJson = path.join(projectRoot, "package.json");
389
457
  if (fs.existsSync(pkgJson)) {
390
458
  const pkg = JSON.parse(fs.readFileSync(pkgJson, "utf-8"));
391
459
  const allDeps = { ...pkg.dependencies, ...pkg.devDependencies };
@@ -291,6 +291,19 @@ export class IngestionPipeline {
291
291
  middleware: "app/Http/Middleware/**",
292
292
  },
293
293
  };
294
+
295
+ if (existsSync(join(projectRoot, "resources/views"))) {
296
+ domains["views"] = {
297
+ root: "resources/views",
298
+ description: "Laravel Blade Templates and UI Views (auto-discovered)",
299
+ allowed_dependencies: [],
300
+ forbidden_dependencies: [],
301
+ archetypes: {
302
+ view: "resources/views/**/*.blade.php",
303
+ },
304
+ };
305
+ }
306
+
294
307
  return domains;
295
308
  }
296
309
 
@@ -433,6 +446,90 @@ export class IngestionPipeline {
433
446
  }
434
447
 
435
448
  private isSourceFile(path: string): boolean {
436
- return /\.(ts|tsx|js|jsx|mjs|cjs|php|py|go|rs)$/i.test(path);
449
+ return /\.(ts|tsx|js|jsx|mjs|cjs|php|py|go|rs|vue|astro|svelte)$/i.test(path);
450
+ }
451
+
452
+ /**
453
+ * JIT single-file ingestion: quickly verifies and updates a single file's AST,
454
+ * symbols, and dependencies in SQLite if its mtime or content hash has changed.
455
+ * Runs in < 5ms for rapid delta sync without full project scans.
456
+ */
457
+ public async ingestFile(targetFilePath: string, config: ValidatedSeptumConfig): Promise<boolean> {
458
+ const fullPath = resolve(targetFilePath);
459
+ if (!existsSync(fullPath)) return false;
460
+
461
+ let stat;
462
+ try {
463
+ stat = statSync(fullPath);
464
+ if (!stat.isFile() || !this.isSourceFile(fullPath)) return false;
465
+ } catch {
466
+ return false;
467
+ }
468
+
469
+ const mtimeMs = Math.round(stat.mtimeMs);
470
+ const sizeBytes = stat.size;
471
+
472
+ const projectRoot = this.customWorkspaceRoot || process.cwd();
473
+ const relPath = relative(projectRoot, fullPath).replace(/\\/g, "/");
474
+
475
+ let targetDomainName: string | null = null;
476
+ let targetDomainCfg = null;
477
+
478
+ for (const [dName, dCfg] of Object.entries(config.domains)) {
479
+ const absRoot = resolve(projectRoot, dCfg.root).replace(/\\/g, "/");
480
+ const normFullPath = fullPath.replace(/\\/g, "/");
481
+ if (normFullPath === absRoot || normFullPath.startsWith(absRoot + "/")) {
482
+ targetDomainName = dName;
483
+ targetDomainCfg = dCfg;
484
+ break;
485
+ }
486
+ }
487
+
488
+ if (!targetDomainName || !targetDomainCfg) {
489
+ targetDomainName = "core";
490
+ targetDomainCfg = { root: ".", allowed_dependencies: [], forbidden_dependencies: [], archetypes: {} };
491
+ }
492
+
493
+ const domainId = this.repo.upsertDomain(targetDomainName, targetDomainCfg);
494
+ const existingFile = this.repo.getFileByPath(relPath);
495
+
496
+ // Tier 1: Fast OS metadata check
497
+ if (
498
+ existingFile &&
499
+ existingFile.mtime_ms === mtimeMs &&
500
+ existingFile.size_bytes === sizeBytes
501
+ ) {
502
+ return false; // Completely up to date
503
+ }
504
+
505
+ const content = readFileSync(fullPath, "utf-8");
506
+ const currentHash = computeContentHash(content);
507
+
508
+ // Tier 2: Hash verification
509
+ if (existingFile && existingFile.content_hash === currentHash) {
510
+ this.repo.updateFileMetadataOnly(existingFile.id, mtimeMs, sizeBytes);
511
+ return false;
512
+ }
513
+
514
+ const lineCount = content.split("\n").length;
515
+ const archetype = detectArchetype(relPath, targetDomainCfg.archetypes ?? {});
516
+ const ast = await this.parserEngine.parseFile(fullPath, content);
517
+
518
+ this.repo.runInTransaction(() => {
519
+ const fileId = this.repo.upsertFile(
520
+ domainId,
521
+ relPath,
522
+ archetype,
523
+ currentHash,
524
+ lineCount,
525
+ mtimeMs,
526
+ sizeBytes
527
+ );
528
+ this.repo.replaceFileSymbols(fileId, ast.symbols);
529
+ this.repo.replaceFileDependencies(fileId, ast.dependencies);
530
+ });
531
+
532
+ return true;
437
533
  }
438
534
  }
535
+