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,242 +1,242 @@
1
- import type { Database } from "bun:sqlite";
2
- import type {
3
- ArchetypeKind,
4
- DomainCatalogResponse,
5
- DomainConfig,
6
- DomainRecord,
7
- ExtractedDependency,
8
- ExtractedSymbol,
9
- FileRecord,
10
- GetSymbolDetailsResponse,
11
- GetSymbolImpactResponse,
12
- InboundCaller,
13
- SymbolKind,
14
- SymbolRecord,
15
- VerticalSliceRecord,
16
- } from "../../types/index.ts";
17
-
18
- export type {
19
- ArchetypeKind,
20
- DomainCatalogResponse,
21
- DomainConfig,
22
- DomainRecord,
23
- ExtractedDependency,
24
- ExtractedSymbol,
25
- FileRecord,
26
- GetSymbolDetailsResponse,
27
- GetSymbolImpactResponse,
28
- InboundCaller,
29
- SymbolKind,
30
- SymbolRecord,
31
- VerticalSliceRecord,
32
- } from "../../types/index.ts";
33
- import { DependencyRepository } from "./repositories/dependency.repository.ts";
34
- import { DomainRepository } from "./repositories/domain.repository.ts";
35
- import { FileRepository } from "./repositories/file.repository.ts";
36
- import { MetaRepository } from "./repositories/meta.repository.ts";
37
- import { SymbolRepository } from "./repositories/symbol.repository.ts";
38
-
39
- export type DatabaseRepository = SeptumRepository;
40
-
41
- export class SeptumRepository {
42
- public readonly domains: DomainRepository;
43
- public readonly files: FileRepository;
44
- public readonly symbols: SymbolRepository;
45
- public readonly dependencies: DependencyRepository;
46
- public readonly meta: MetaRepository;
47
-
48
- constructor(private db: Database) {
49
- this.domains = new DomainRepository(db);
50
- this.files = new FileRepository(db);
51
- this.symbols = new SymbolRepository(db);
52
- this.dependencies = new DependencyRepository(db);
53
- this.meta = new MetaRepository(db);
54
- }
55
-
56
- public runInTransaction<T>(fn: () => T): T {
57
- return this.db.transaction(fn)();
58
- }
59
-
60
- // --- Domain Methods ---
61
- public upsertDomain(name: string, config: DomainConfig): number {
62
- return this.domains.upsertDomain(name, config);
63
- }
64
-
65
- public getDomainByName(name: string): DomainRecord | null {
66
- return this.domains.getDomainByName(name);
67
- }
68
-
69
- public getAllDomains(): DomainRecord[] {
70
- return this.domains.getAllDomains();
71
- }
72
-
73
- // --- File Methods ---
74
- public getFileByPath(path: string): FileRecord | null {
75
- return this.files.getFileByPath(path);
76
- }
77
-
78
- public getFilesByDomain(domainId: number): FileRecord[] {
79
- return this.files.getFilesByDomain(domainId);
80
- }
81
-
82
- public upsertFile(
83
- domainId: number,
84
- path: string,
85
- archetype: ArchetypeKind,
86
- contentHash: string,
87
- lineCount: number,
88
- mtimeMs: number = 0,
89
- sizeBytes: number = 0
90
- ): number {
91
- return this.files.upsertFile(
92
- domainId,
93
- path,
94
- archetype,
95
- contentHash,
96
- lineCount,
97
- mtimeMs,
98
- sizeBytes
99
- );
100
- }
101
-
102
- public updateFileMetadataOnly(id: number, mtimeMs: number, sizeBytes: number): void {
103
- this.files.updateFileMetadataOnly(id, mtimeMs, sizeBytes);
104
- }
105
-
106
- public deleteFilesNotInPaths(domainId: number, validPaths: string[]): number {
107
- return this.files.deleteFilesNotInPaths(domainId, validPaths);
108
- }
109
-
110
- public deleteFile(path: string): void {
111
- this.files.deleteFile(path);
112
- }
113
-
114
- public getDomainCatalog(
115
- domainName: string,
116
- archetypeFilter?: string
117
- ): DomainCatalogResponse | null {
118
- return this.domains.getDomainCatalog(domainName, archetypeFilter);
119
- }
120
-
121
- // --- Symbol Methods ---
122
- public replaceFileSymbols(fileId: number, symbols: ExtractedSymbol[]): void {
123
- this.symbols.replaceFileSymbols(fileId, symbols);
124
- }
125
-
126
- public getSymbolsByFileId(fileId: number): SymbolRecord[] {
127
- return this.symbols.getSymbolsByFileId(fileId);
128
- }
129
-
130
- public findSymbolsByName(name: string): Array<SymbolRecord & { file_path: string }> {
131
- return this.symbols.findSymbolsByName(name);
132
- }
133
-
134
- public findContainers(
135
- containerName: string
136
- ): Array<{ file: FileRecord; symbol?: SymbolRecord }> {
137
- return this.symbols.findContainers(containerName);
138
- }
139
-
140
- public getAllSymbolsWithFiles(): Array<SymbolRecord & { file_path: string }> {
141
- return this.symbols.getAllSymbolsWithFiles();
142
- }
143
-
144
- // --- Dependency & Slice Methods ---
145
- public replaceFileDependencies(fileId: number, deps: ExtractedDependency[]): void {
146
- this.dependencies.replaceFileDependencies(fileId, deps);
147
- }
148
-
149
- public upsertVerticalSlice(
150
- slice: Omit<VerticalSliceRecord, "id" | "created_at">
151
- ): void {
152
- this.dependencies.upsertVerticalSlice(slice);
153
- }
154
-
155
- public findVerticalSlices(searchTerm: string): VerticalSliceRecord[] {
156
- return this.dependencies.findVerticalSlices(searchTerm);
157
- }
158
-
159
- public getAllVerticalSlices(): VerticalSliceRecord[] {
160
- return this.dependencies.getAllVerticalSlices();
161
- }
162
-
163
- public clearVerticalSlices(domainId?: number): void {
164
- this.dependencies.clearVerticalSlices(domainId);
165
- }
166
-
167
- public getAllDependenciesWithDomains(): Array<{
168
- source_file: string;
169
- source_domain: string;
170
- line_number: number;
171
- target: string;
172
- statement: string;
173
- is_external: boolean;
174
- }> {
175
- return this.dependencies.getAllDependenciesWithDomains();
176
- }
177
-
178
- public findSymbolWithDomain(
179
- symbolName: string
180
- ): {
181
- symbol_name: string;
182
- kind: string;
183
- file_path: string;
184
- domain_name: string;
185
- } | null {
186
- return this.symbols.findSymbolWithDomain(symbolName);
187
- }
188
-
189
- public findSymbolWithMembers(symbolName: string): {
190
- symbol_name: string;
191
- kind: string;
192
- signature: string;
193
- file_path: string;
194
- domain_name: string;
195
- methods: string[];
196
- properties: string[];
197
- } | null {
198
- return this.symbols.findSymbolWithMembers(symbolName);
199
- }
200
-
201
- public getFileWithDomain(filePath: string): {
202
- file_id: number;
203
- file_path: string;
204
- domain_name: string;
205
- } | null {
206
- return this.symbols.getFileWithDomain(filePath);
207
- }
208
-
209
- public getHotspotSymbols(options: {
210
- minLines?: number;
211
- kind?: SymbolKind;
212
- domain?: string;
213
- limit?: number;
214
- } = {}): Array<SymbolRecord & { file_path: string; domain_name: string }> {
215
- return this.symbols.getHotspotSymbols(options);
216
- }
217
-
218
- public getSymbolDetails(
219
- symbolQuery: string,
220
- options: { domain?: string; includeDependencies?: boolean } = {}
221
- ): GetSymbolDetailsResponse | null {
222
- return this.symbols.getSymbolDetails(symbolQuery, options);
223
- }
224
-
225
- public getSymbolImpact(symbolName: string): GetSymbolImpactResponse {
226
- return this.dependencies.getSymbolImpact(symbolName);
227
- }
228
-
229
- // --- Metadata Methods ---
230
- public getMeta(key: string): string | null {
231
- return this.meta.getMeta(key);
232
- }
233
-
234
- public setMeta(key: string, value: string): void {
235
- this.meta.setMeta(key, value);
236
- }
237
-
238
- public getAllMeta(): Record<string, string> {
239
- return this.meta.getAllMeta();
240
- }
241
- }
242
-
1
+ import type { Database } from "bun:sqlite";
2
+ import type {
3
+ ArchetypeKind,
4
+ DomainCatalogResponse,
5
+ DomainConfig,
6
+ DomainRecord,
7
+ ExtractedDependency,
8
+ ExtractedSymbol,
9
+ FileRecord,
10
+ GetSymbolDetailsResponse,
11
+ GetSymbolImpactResponse,
12
+ InboundCaller,
13
+ SymbolKind,
14
+ SymbolRecord,
15
+ VerticalSliceRecord,
16
+ } from "../../types/index.ts";
17
+
18
+ export type {
19
+ ArchetypeKind,
20
+ DomainCatalogResponse,
21
+ DomainConfig,
22
+ DomainRecord,
23
+ ExtractedDependency,
24
+ ExtractedSymbol,
25
+ FileRecord,
26
+ GetSymbolDetailsResponse,
27
+ GetSymbolImpactResponse,
28
+ InboundCaller,
29
+ SymbolKind,
30
+ SymbolRecord,
31
+ VerticalSliceRecord,
32
+ } from "../../types/index.ts";
33
+ import { DependencyRepository } from "./repositories/dependency.repository.ts";
34
+ import { DomainRepository } from "./repositories/domain.repository.ts";
35
+ import { FileRepository } from "./repositories/file.repository.ts";
36
+ import { MetaRepository } from "./repositories/meta.repository.ts";
37
+ import { SymbolRepository } from "./repositories/symbol.repository.ts";
38
+
39
+ export type DatabaseRepository = SeptumRepository;
40
+
41
+ export class SeptumRepository {
42
+ public readonly domains: DomainRepository;
43
+ public readonly files: FileRepository;
44
+ public readonly symbols: SymbolRepository;
45
+ public readonly dependencies: DependencyRepository;
46
+ public readonly meta: MetaRepository;
47
+
48
+ constructor(private db: Database) {
49
+ this.domains = new DomainRepository(db);
50
+ this.files = new FileRepository(db);
51
+ this.symbols = new SymbolRepository(db);
52
+ this.dependencies = new DependencyRepository(db);
53
+ this.meta = new MetaRepository(db);
54
+ }
55
+
56
+ public runInTransaction<T>(fn: () => T): T {
57
+ return this.db.transaction(fn)();
58
+ }
59
+
60
+ // --- Domain Methods ---
61
+ public upsertDomain(name: string, config: DomainConfig): number {
62
+ return this.domains.upsertDomain(name, config);
63
+ }
64
+
65
+ public getDomainByName(name: string): DomainRecord | null {
66
+ return this.domains.getDomainByName(name);
67
+ }
68
+
69
+ public getAllDomains(): DomainRecord[] {
70
+ return this.domains.getAllDomains();
71
+ }
72
+
73
+ // --- File Methods ---
74
+ public getFileByPath(path: string): FileRecord | null {
75
+ return this.files.getFileByPath(path);
76
+ }
77
+
78
+ public getFilesByDomain(domainId: number): FileRecord[] {
79
+ return this.files.getFilesByDomain(domainId);
80
+ }
81
+
82
+ public upsertFile(
83
+ domainId: number,
84
+ path: string,
85
+ archetype: ArchetypeKind,
86
+ contentHash: string,
87
+ lineCount: number,
88
+ mtimeMs: number = 0,
89
+ sizeBytes: number = 0
90
+ ): number {
91
+ return this.files.upsertFile(
92
+ domainId,
93
+ path,
94
+ archetype,
95
+ contentHash,
96
+ lineCount,
97
+ mtimeMs,
98
+ sizeBytes
99
+ );
100
+ }
101
+
102
+ public updateFileMetadataOnly(id: number, mtimeMs: number, sizeBytes: number): void {
103
+ this.files.updateFileMetadataOnly(id, mtimeMs, sizeBytes);
104
+ }
105
+
106
+ public deleteFilesNotInPaths(domainId: number, validPaths: string[]): number {
107
+ return this.files.deleteFilesNotInPaths(domainId, validPaths);
108
+ }
109
+
110
+ public deleteFile(path: string): void {
111
+ this.files.deleteFile(path);
112
+ }
113
+
114
+ public getDomainCatalog(
115
+ domainName: string,
116
+ archetypeFilter?: string
117
+ ): DomainCatalogResponse | null {
118
+ return this.domains.getDomainCatalog(domainName, archetypeFilter);
119
+ }
120
+
121
+ // --- Symbol Methods ---
122
+ public replaceFileSymbols(fileId: number, symbols: ExtractedSymbol[]): void {
123
+ this.symbols.replaceFileSymbols(fileId, symbols);
124
+ }
125
+
126
+ public getSymbolsByFileId(fileId: number): SymbolRecord[] {
127
+ return this.symbols.getSymbolsByFileId(fileId);
128
+ }
129
+
130
+ public findSymbolsByName(name: string): Array<SymbolRecord & { file_path: string }> {
131
+ return this.symbols.findSymbolsByName(name);
132
+ }
133
+
134
+ public findContainers(
135
+ containerName: string
136
+ ): Array<{ file: FileRecord; symbol?: SymbolRecord }> {
137
+ return this.symbols.findContainers(containerName);
138
+ }
139
+
140
+ public getAllSymbolsWithFiles(): Array<SymbolRecord & { file_path: string }> {
141
+ return this.symbols.getAllSymbolsWithFiles();
142
+ }
143
+
144
+ // --- Dependency & Slice Methods ---
145
+ public replaceFileDependencies(fileId: number, deps: ExtractedDependency[]): void {
146
+ this.dependencies.replaceFileDependencies(fileId, deps);
147
+ }
148
+
149
+ public upsertVerticalSlice(
150
+ slice: Omit<VerticalSliceRecord, "id" | "created_at">
151
+ ): void {
152
+ this.dependencies.upsertVerticalSlice(slice);
153
+ }
154
+
155
+ public findVerticalSlices(searchTerm: string): VerticalSliceRecord[] {
156
+ return this.dependencies.findVerticalSlices(searchTerm);
157
+ }
158
+
159
+ public getAllVerticalSlices(): VerticalSliceRecord[] {
160
+ return this.dependencies.getAllVerticalSlices();
161
+ }
162
+
163
+ public clearVerticalSlices(domainId?: number): void {
164
+ this.dependencies.clearVerticalSlices(domainId);
165
+ }
166
+
167
+ public getAllDependenciesWithDomains(): Array<{
168
+ source_file: string;
169
+ source_domain: string;
170
+ line_number: number;
171
+ target: string;
172
+ statement: string;
173
+ is_external: boolean;
174
+ }> {
175
+ return this.dependencies.getAllDependenciesWithDomains();
176
+ }
177
+
178
+ public findSymbolWithDomain(
179
+ symbolName: string
180
+ ): {
181
+ symbol_name: string;
182
+ kind: string;
183
+ file_path: string;
184
+ domain_name: string;
185
+ } | null {
186
+ return this.symbols.findSymbolWithDomain(symbolName);
187
+ }
188
+
189
+ public findSymbolWithMembers(symbolName: string): {
190
+ symbol_name: string;
191
+ kind: string;
192
+ signature: string;
193
+ file_path: string;
194
+ domain_name: string;
195
+ methods: string[];
196
+ properties: string[];
197
+ } | null {
198
+ return this.symbols.findSymbolWithMembers(symbolName);
199
+ }
200
+
201
+ public getFileWithDomain(filePath: string): {
202
+ file_id: number;
203
+ file_path: string;
204
+ domain_name: string;
205
+ } | null {
206
+ return this.symbols.getFileWithDomain(filePath);
207
+ }
208
+
209
+ public getHotspotSymbols(options: {
210
+ minLines?: number;
211
+ kind?: SymbolKind;
212
+ domain?: string;
213
+ limit?: number;
214
+ } = {}): Array<SymbolRecord & { file_path: string; domain_name: string }> {
215
+ return this.symbols.getHotspotSymbols(options);
216
+ }
217
+
218
+ public getSymbolDetails(
219
+ symbolQuery: string,
220
+ options: { domain?: string; includeDependencies?: boolean } = {}
221
+ ): GetSymbolDetailsResponse | null {
222
+ return this.symbols.getSymbolDetails(symbolQuery, options);
223
+ }
224
+
225
+ public getSymbolImpact(symbolName: string): GetSymbolImpactResponse {
226
+ return this.dependencies.getSymbolImpact(symbolName);
227
+ }
228
+
229
+ // --- Metadata Methods ---
230
+ public getMeta(key: string): string | null {
231
+ return this.meta.getMeta(key);
232
+ }
233
+
234
+ public setMeta(key: string, value: string): void {
235
+ this.meta.setMeta(key, value);
236
+ }
237
+
238
+ public getAllMeta(): Record<string, string> {
239
+ return this.meta.getAllMeta();
240
+ }
241
+ }
242
+
@@ -0,0 +1,120 @@
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
+ }