@dimassetoid/septum-mcp 0.2.1 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (161) hide show
  1. package/README.md +46 -45
  2. package/package.json +8 -6
  3. package/src/cli/commands/check.ts +41 -26
  4. package/src/cli/commands/hook.ts +77 -105
  5. package/src/cli/commands/init.ts +35 -9
  6. package/src/cli/commands/stats.ts +33 -0
  7. package/src/cli/commands/sync-progress.ts +71 -0
  8. package/src/cli/commands/sync.ts +35 -9
  9. package/src/cli/hook-payload.ts +43 -0
  10. package/src/cli/index.ts +15 -51
  11. package/src/cli/scaffold/harness-config.ts +48 -8
  12. package/src/cli/templates/hooks.ts +51 -27
  13. package/src/cli/templates/rules.ts +34 -49
  14. package/src/cli/templates/skill.ts +46 -35
  15. package/src/core/boundary/audit.ts +383 -0
  16. package/src/core/boundary/evaluator.ts +299 -178
  17. package/src/core/boundary/staged-auditor.ts +188 -34
  18. package/src/core/config/domain-ownership.ts +47 -0
  19. package/src/core/config/domain-registration.ts +141 -0
  20. package/src/core/config/loader.ts +82 -54
  21. package/src/core/config/schema.ts +24 -46
  22. package/src/core/database/client.ts +2 -0
  23. package/src/core/database/repositories/dependency.repository.ts +326 -543
  24. package/src/core/database/repositories/domain.repository.ts +48 -4
  25. package/src/core/database/repositories/file.repository.ts +10 -0
  26. package/src/core/database/repositories/symbol.repository.ts +97 -259
  27. package/src/core/database/repository.ts +138 -24
  28. package/src/core/database/schema.ts +28 -33
  29. package/src/core/discovery/declarative-routes.ts +69 -0
  30. package/src/core/discovery/framework-detector.ts +149 -0
  31. package/src/core/discovery/navigation.ts +269 -0
  32. package/src/core/discovery/topology-detector.ts +153 -170
  33. package/src/core/errors.ts +19 -0
  34. package/src/core/ingestion/pipeline.ts +378 -212
  35. package/src/core/parser/behavioral-analyzer.ts +26 -22
  36. package/src/core/parser/boundary-tracker.ts +15 -0
  37. package/src/core/parser/extractors/base.ts +16 -4
  38. package/src/core/parser/extractors/csharp.ts +9 -5
  39. package/src/core/parser/extractors/django-routing.ts +163 -0
  40. package/src/core/parser/extractors/django-semantic.ts +400 -516
  41. package/src/core/parser/extractors/elysia-semantic.ts +172 -496
  42. package/src/core/parser/extractors/express-semantic.ts +195 -398
  43. package/src/core/parser/extractors/fastapi-semantic.ts +182 -399
  44. package/src/core/parser/extractors/fastify-semantic.ts +216 -732
  45. package/src/core/parser/extractors/flask-semantic.ts +92 -388
  46. package/src/core/parser/extractors/go-routing.ts +344 -0
  47. package/src/core/parser/extractors/go-semantic.ts +210 -508
  48. package/src/core/parser/extractors/go.ts +6 -3
  49. package/src/core/parser/extractors/graphql-entrypoint.ts +146 -0
  50. package/src/core/parser/extractors/hono-semantic.ts +132 -530
  51. package/src/core/parser/extractors/java.ts +7 -5
  52. package/src/core/parser/extractors/laravel-routing.ts +263 -0
  53. package/src/core/parser/extractors/laravel-semantic.ts +169 -333
  54. package/src/core/parser/extractors/manifest-helpers.ts +64 -0
  55. package/src/core/parser/extractors/mcp-cli-dispatch.ts +268 -0
  56. package/src/core/parser/extractors/mcp-cli-semantic.ts +84 -336
  57. package/src/core/parser/extractors/nestjs-semantic.ts +238 -510
  58. package/src/core/parser/extractors/nextjs-semantic.ts +164 -0
  59. package/src/core/parser/extractors/php-ast-helpers.ts +51 -0
  60. package/src/core/parser/extractors/php.ts +17 -1
  61. package/src/core/parser/extractors/python-route-ast.ts +249 -0
  62. package/src/core/parser/extractors/python.ts +9 -8
  63. package/src/core/parser/extractors/registry.ts +28 -5
  64. package/src/core/parser/extractors/route-prefixes.ts +43 -0
  65. package/src/core/parser/extractors/ruby.ts +10 -7
  66. package/src/core/parser/extractors/rust.ts +35 -17
  67. package/src/core/parser/extractors/semantic-extractor.interface.ts +3 -1
  68. package/src/core/parser/extractors/symfony-routing.ts +114 -0
  69. package/src/core/parser/extractors/symfony-semantic.ts +75 -262
  70. package/src/core/parser/extractors/typescript-bindings.ts +333 -0
  71. package/src/core/parser/extractors/typescript-compiler-fallback.ts +380 -0
  72. package/src/core/parser/extractors/typescript-fallback.ts +207 -0
  73. package/src/core/parser/extractors/typescript-route-context.ts +58 -0
  74. package/src/core/parser/extractors/typescript.ts +36 -216
  75. package/src/core/parser/implementation-fingerprint.ts +97 -0
  76. package/src/core/parser/language-capabilities.ts +23 -7
  77. package/src/core/parser/schema/providers/laravel-migration-provider.ts +77 -51
  78. package/src/core/parser/schema/schema-provider-registry.ts +1 -3
  79. package/src/core/parser/schema/schema-provider.interface.ts +2 -1
  80. package/src/core/parser/semantic-revision.ts +3 -0
  81. package/src/core/parser/static-string.ts +51 -0
  82. package/src/core/parser/wasm-loader.ts +6 -2
  83. package/src/core/resolver/ast-method-tracer.ts +230 -0
  84. package/src/core/resolver/call-graph-helpers.ts +72 -0
  85. package/src/core/resolver/call-graph-tracer.ts +375 -311
  86. package/src/core/resolver/dependency-analysis.ts +593 -0
  87. package/src/core/resolver/import-candidates.ts +120 -0
  88. package/src/core/resolver/import-evidence.ts +88 -0
  89. package/src/core/resolver/import-resolver.ts +162 -181
  90. package/src/core/resolver/interpretation-inputs.ts +65 -0
  91. package/src/core/resolver/languages/go.ts +103 -0
  92. package/src/core/resolver/languages/managed.ts +200 -0
  93. package/src/core/resolver/languages/python.ts +133 -0
  94. package/src/core/resolver/languages/ruby.ts +40 -0
  95. package/src/core/resolver/languages/rust.ts +127 -0
  96. package/src/core/resolver/languages/shared.ts +74 -0
  97. package/src/core/resolver/module-resolver.ts +18 -7
  98. package/src/core/resolver/path-resolver.ts +6 -3
  99. package/src/core/resolver/polyglot-slice-tracer.ts +197 -0
  100. package/src/core/resolver/slice-catalog.ts +94 -0
  101. package/src/core/resolver/slice-format.ts +22 -0
  102. package/src/core/resolver/slice-selection.ts +237 -0
  103. package/src/core/resolver/symbol-impact.ts +477 -0
  104. package/src/core/resolver/symbol-locator.ts +82 -15
  105. package/src/core/resolver/vertical-slice-tracer.ts +57 -278
  106. package/src/core/resources/ignore.ts +21 -1
  107. package/src/core/resources/limits.ts +2 -1
  108. package/src/core/resources/source-files.ts +143 -20
  109. package/src/core/resources/source-inventory.ts +55 -0
  110. package/src/core/resources/work-queue.ts +167 -4
  111. package/src/core/retrieval/context-page.ts +116 -0
  112. package/src/core/retrieval/context.ts +457 -0
  113. package/src/core/retrieval/resolution-warnings.ts +21 -0
  114. package/src/core/retrieval/source-context.ts +448 -0
  115. package/src/core/retrieval/symbol-details.ts +453 -0
  116. package/src/core/retrieval/symbol-evidence.ts +97 -0
  117. package/src/core/retrieval/typescript-references.ts +293 -0
  118. package/src/core/retrieval/universal-context.ts +182 -0
  119. package/src/core/telemetry/summary.ts +114 -0
  120. package/src/core/telemetry/telemetry.ts +225 -37
  121. package/src/index.ts +0 -3
  122. package/src/mcp/formatters/compact-text.ts +439 -0
  123. package/src/mcp/resources-and-prompts.ts +253 -0
  124. package/src/mcp/schemas/boundary.ts +145 -0
  125. package/src/mcp/schemas/map.ts +250 -0
  126. package/src/mcp/schemas/shared.ts +52 -0
  127. package/src/mcp/schemas/slice.ts +60 -0
  128. package/src/mcp/schemas/symbol.ts +268 -0
  129. package/src/mcp/schemas.ts +5 -1105
  130. package/src/mcp/server.ts +263 -597
  131. package/src/mcp/tool-catalog.ts +331 -0
  132. package/src/mcp/tools/boundary.ts +69 -4
  133. package/src/mcp/tools/map.ts +126 -0
  134. package/src/mcp/tools/slice.ts +101 -3
  135. package/src/mcp/tools/symbol.ts +480 -26
  136. package/src/mcp/ui/app-client.ts +899 -0
  137. package/src/mcp/ui/app.html +478 -0
  138. package/src/mcp/workflow-playbooks.ts +181 -0
  139. package/src/mcp/workspace-boundary.ts +50 -0
  140. package/src/mcp/workspace-manager.ts +102 -18
  141. package/src/mcp/workspace-watcher.ts +38 -19
  142. package/src/types/index.ts +47 -53
  143. package/src/cli/commands/topology.ts +0 -161
  144. package/src/core/database/repositories/environment.repository.ts +0 -207
  145. package/src/core/parser/schema/providers/prisma-schema-provider.ts +0 -90
  146. package/src/core/session/session-manager.ts +0 -107
  147. package/src/core/topology/environment-detector.ts +0 -973
  148. package/src/core/topology/types.ts +0 -83
  149. package/src/mcp/tools/check-boundary.ts +0 -387
  150. package/src/mcp/tools/domain.ts +0 -40
  151. package/src/mcp/tools/feature.ts +0 -67
  152. package/src/mcp/tools/get-domain-catalog.ts +0 -226
  153. package/src/mcp/tools/get-environment-topology.ts +0 -138
  154. package/src/mcp/tools/get-feature-context.ts +0 -224
  155. package/src/mcp/tools/get-symbol-hotspots.ts +0 -54
  156. package/src/mcp/tools/get-symbol-impact.ts +0 -64
  157. package/src/mcp/tools/get-symbol.ts +0 -175
  158. package/src/mcp/tools/locate-symbol.ts +0 -196
  159. package/src/mcp/tools/register-domain.ts +0 -75
  160. package/src/mcp/tools/register-feature.ts +0 -110
  161. package/src/mcp/tools/trace-vertical-slice.ts +0 -80
@@ -1,16 +1,27 @@
1
+ import type { ImportEvidence, IndexedDependency } from "../resolver/import-evidence.ts";
1
2
  import { posix, relative, resolve, isAbsolute } from "node:path";
2
- import type { BoundaryViolation } from "../../types/index.ts";
3
+ import { existsSync } from "node:fs";
4
+ import type { BoundaryViolation, ImportResolution } from "../../types/index.ts";
3
5
  import type { ValidatedSeptumConfig } from "../config/schema.ts";
4
6
  import { ConfigLoader } from "../config/loader.ts";
5
7
  import type { SeptumRepository } from "../database/repository.ts";
6
8
  import { ModuleResolver } from "../resolver/module-resolver.ts";
7
9
  import { matchesSimpleGlob } from "../parser/extractors/base.ts";
10
+ import { assertUniqueDomainRoots } from "../config/domain-ownership.ts";
11
+
12
+ function dependencyKey(file: string, line: number, target: string, statement: string): string {
13
+ return JSON.stringify([file, line, target, statement]);
14
+ }
15
+
16
+ function indexedDependencyKey(dependency: IndexedDependency): string {
17
+ return dependencyKey(dependency.source_file, dependency.line_number, dependency.target, dependency.statement);
18
+ }
8
19
 
9
20
  export class BoundaryEvaluator {
10
- private repo: SeptumRepository;
21
+ private repo?: SeptumRepository;
11
22
  private resolver: ModuleResolver;
12
23
 
13
- constructor(repo: SeptumRepository, resolver?: ModuleResolver) {
24
+ constructor(repo?: SeptumRepository, resolver?: ModuleResolver) {
14
25
  this.repo = repo;
15
26
  this.resolver = resolver ?? new ModuleResolver(process.cwd(), repo);
16
27
  }
@@ -19,104 +30,156 @@ export class BoundaryEvaluator {
19
30
  return this.resolver;
20
31
  }
21
32
 
33
+ public withResolver(resolver: ModuleResolver): BoundaryEvaluator {
34
+ return new BoundaryEvaluator(this.repo, resolver);
35
+ }
36
+
22
37
  public evaluate(
23
38
  filePath: string,
24
39
  proposedImports: string[] = [],
25
- featureKey?: string,
26
- config?: ValidatedSeptumConfig
40
+ config?: ValidatedSeptumConfig,
41
+ snapshot?: ImportEvidence
27
42
  ): BoundaryViolation[] {
28
43
  let activeConfig = config;
29
44
  if (!activeConfig) {
30
- const fromRepo = ConfigLoader.loadFromRepository(this.repo);
45
+ const fromRepo = this.repo ? ConfigLoader.loadFromRepository(this.repo) : undefined;
31
46
  activeConfig =
32
- Object.keys(fromRepo.domains).length > 0
47
+ fromRepo && Object.keys(fromRepo.domains).length > 0
33
48
  ? fromRepo
34
- : ConfigLoader.loadFromDatabaseOrDefaults();
49
+ : ConfigLoader.load(undefined, this.resolver.getProjectRoot());
35
50
  }
51
+ assertUniqueDomainRoots(activeConfig.domains, this.resolver.getProjectRoot());
36
52
  const violations: BoundaryViolation[] = [];
37
53
 
38
- if (featureKey) {
39
- violations.push(...this.checkFeatureTouchpoints(featureKey, filePath, activeConfig));
40
- }
41
-
42
54
  if (proposedImports.length > 0) {
43
55
  violations.push(...this.checkProposedChanges(filePath, proposedImports, activeConfig));
56
+ const sourceDomain = this.resolveSourceDomain(filePath, activeConfig);
57
+ if (sourceDomain && typeof this.repo?.getAllDependenciesWithDomains === "function") {
58
+ // Resolutions from import-evidence (requires workspace_root meta) — optional, used for
59
+ // richer cycle detection. Falls back to raw indexed deps when unavailable (e.g. in tests).
60
+ const indexed = this.repo?.getMeta?.("workspace_root");
61
+ const auditEvidence = snapshot ?? (indexed && this.repo ? this.repo.getImportEvidence(this.resolver.getProjectRoot()) : undefined);
62
+ const evidence = indexed ? auditEvidence?.relationships : undefined;
63
+ const resolutions = evidence && new Map(evidence.filter(edge => edge.freshness === "current").map(edge => [
64
+ dependencyKey(edge.source.file, edge.source.line, edge.resolution.source.target, edge.source.statement),
65
+ edge.resolution,
66
+ ]));
67
+
68
+ const allDeps = auditEvidence?.dependencies ?? this.repo.getAllDependenciesWithDomains();
69
+ const existingDeps = allDeps.filter(
70
+ (dep) => dep.source_file !== filePath
71
+ );
72
+ const proposedDeps = proposedImports.map((statement) => ({
73
+ source_file: filePath,
74
+ source_domain: sourceDomain,
75
+ line_number: 1,
76
+ target: statement,
77
+ statement,
78
+ is_external: false,
79
+ }));
80
+
81
+ const detectedCycles = this.detectCircularDependencies(
82
+ activeConfig,
83
+ [...existingDeps, ...proposedDeps],
84
+ new Set([filePath]),
85
+ resolutions
86
+ );
87
+
88
+ if (detectedCycles.length > 0) {
89
+ // Baseline: detect cycles that already exist in the indexed graph, independent of
90
+ // workspace_root / import-evidence so the tagging works even in in-memory test repos.
91
+ const baselineCycles = this.detectCircularDependencies(
92
+ activeConfig,
93
+ allDeps,
94
+ undefined, // no relevantFiles filter — capture all pre-existing cycles
95
+ resolutions
96
+ );
97
+ // Normalize cycle messages to a rotation-agnostic canonical form:
98
+ // extract the node list, sort it, then join. This handles DFS start-node differences.
99
+ const extractCycleNodeSet = (msg: string): string => {
100
+ const match = /detected: (.+)\.$/.exec(msg);
101
+ if (!match) return msg;
102
+ // "a -> b -> a" → split, dedupe, sort
103
+ const nodes = match[1]!.split(" -> ").filter((n, i, arr) => !(i === arr.length - 1 && n === arr[0]));
104
+ return nodes.sort().join(",");
105
+ };
106
+ const baselineCanonicals = new Set(baselineCycles.map((c) => extractCycleNodeSet(c.message)));
107
+
108
+ for (const cycle of detectedCycles) {
109
+ if (baselineCanonicals.has(extractCycleNodeSet(cycle.message))) {
110
+ cycle.pre_existing = true;
111
+ }
112
+ violations.push(cycle);
113
+ }
114
+ }
115
+ }
44
116
  }
45
117
 
46
118
  return violations;
47
119
  }
48
120
 
49
- public auditCodebase(config: ValidatedSeptumConfig): BoundaryViolation[] {
50
- const violations: BoundaryViolation[] = [];
51
- const allDependencies = this.repo.getAllDependenciesWithDomains();
121
+ public auditCodebase(config: ValidatedSeptumConfig, snapshot?: ImportEvidence): BoundaryViolation[] {
122
+ return this.auditCodebaseWithWarnings(config, snapshot).violations;
123
+ }
52
124
 
53
- const evidence = this.repo.getMeta?.("workspace_root") ? this.repo.getImportRelationships(this.resolver.getProjectRoot()) : undefined;
125
+ public auditCodebaseWithWarnings(
126
+ config: ValidatedSeptumConfig,
127
+ snapshot?: ImportEvidence
128
+ ): { violations: BoundaryViolation[]; warnings: BoundaryViolation[] } {
129
+ assertUniqueDomainRoots(config.domains, this.resolver.getProjectRoot());
130
+ const violations: BoundaryViolation[] = [];
131
+ const warnings: BoundaryViolation[] = [];
132
+ const indexed = this.repo?.getMeta?.("workspace_root");
133
+ const auditEvidence = snapshot ?? (indexed && this.repo ? this.repo.getImportEvidence(this.resolver.getProjectRoot()) : undefined);
134
+ const allDependencies = auditEvidence?.dependencies ?? this.repo?.getAllDependenciesWithDomains() ?? [];
135
+ const evidence = indexed ? auditEvidence?.relationships : undefined;
136
+ const resolutions = evidence && new Map(evidence.filter(edge => edge.freshness === "current").map(edge => [
137
+ dependencyKey(edge.source.file, edge.source.line, edge.resolution.source.target, edge.source.statement),
138
+ edge.resolution,
139
+ ]));
54
140
  for (const dep of allDependencies) {
55
- if (evidence && !evidence.some(edge => edge.source.file === dep.source_file && edge.source.line === dep.line_number && edge.freshness === "current" && edge.resolution.status === "resolved" && edge.resolution.coverage !== "conditional")) continue;
56
- const targetDomain = this.resolveTargetDomain(dep.target, config, dep.source_file);
57
- if (!targetDomain || targetDomain === dep.source_domain) {
58
- continue;
59
- }
60
-
61
- const sourceDomainConfig = config.domains[dep.source_domain];
62
- if (!sourceDomainConfig) {
63
- continue;
64
- }
65
-
66
- const forbidden = sourceDomainConfig.forbidden_dependencies ?? [];
67
- const allowed = sourceDomainConfig.allowed_dependencies ?? [];
68
-
69
- // 1. Explicit forbidden check
70
- if (forbidden.includes(targetDomain)) {
71
- violations.push({
72
- file: dep.source_file,
73
- line: dep.line_number,
74
- source_domain: dep.source_domain,
75
- target_domain: targetDomain,
76
- imported_target: dep.target,
77
- rule: "forbidden_dependency",
78
- message: `Forbidden cross-domain dependency detected: '${dep.source_domain}' is strictly prohibited from importing domain '${targetDomain}'.`,
79
- });
80
- continue;
81
- }
82
-
83
- // 2. Strict enforcement allowed check
84
- if (config.settings.enforcement === "strict" && !allowed.includes(targetDomain)) {
85
- violations.push({
86
- file: dep.source_file,
87
- line: dep.line_number,
88
- source_domain: dep.source_domain,
89
- target_domain: targetDomain,
90
- imported_target: dep.target,
91
- rule: "disallowed_dependency",
92
- message: `Disallowed dependency: '${targetDomain}' is not declared in allowed_dependencies for domain '${dep.source_domain}'.`,
93
- });
94
- }
141
+ const resolution = resolutions?.get(indexedDependencyKey(dep));
142
+ if (resolutions && (resolution?.status !== "resolved" || resolution.coverage === "conditional")) continue;
143
+ const evaluation = this.evaluateDependencyRuleWithWarning({
144
+ sourceFile: dep.source_file,
145
+ sourceDomain: dep.source_domain,
146
+ line: dep.line_number,
147
+ target: dep.target,
148
+ statement: dep.statement,
149
+ config,
150
+ mode: "audit",
151
+ resolution,
152
+ });
153
+ if (evaluation.violation) violations.push(evaluation.violation);
154
+ if (evaluation.warning) warnings.push(evaluation.warning);
95
155
  }
96
156
 
97
- const currentDependencies = evidence ? allDependencies.filter(dep => evidence.some(edge => edge.source.file === dep.source_file && edge.source.line === dep.line_number && edge.freshness === "current")) : allDependencies;
98
- violations.push(...this.detectCircularDependencies(config, currentDependencies));
157
+ const currentDependencies = resolutions ? allDependencies.filter(dep => resolutions.has(indexedDependencyKey(dep))) : allDependencies;
158
+ violations.push(...this.detectCircularDependencies(config, currentDependencies, undefined, resolutions));
99
159
 
100
- return violations;
160
+ return { violations, warnings };
101
161
  }
102
162
 
103
163
  public detectCircularDependencies(
104
164
  config: ValidatedSeptumConfig,
105
- preloadedDeps?: Array<{
106
- source_file: string;
107
- source_domain: string;
108
- line_number: number;
109
- target: string;
110
- statement: string;
111
- is_external: boolean;
112
- }>,
113
- relevantFiles?: ReadonlySet<string>
165
+ preloadedDeps?: IndexedDependency[],
166
+ relevantFiles?: ReadonlySet<string>,
167
+ resolutions?: ReadonlyMap<string, ImportResolution>,
114
168
  ): BoundaryViolation[] {
169
+ assertUniqueDomainRoots(config.domains, this.resolver.getProjectRoot());
115
170
  const violations: BoundaryViolation[] = [];
116
- let allDependencies = preloadedDeps ?? this.repo.getAllDependenciesWithDomains();
117
- if (!preloadedDeps && this.repo.getMeta?.("workspace_root")) {
171
+ let allDependencies = preloadedDeps ?? this.repo?.getAllDependenciesWithDomains() ?? [];
172
+ if (!preloadedDeps && this.repo?.getMeta?.("workspace_root")) {
118
173
  const current = this.repo.getImportRelationships(this.resolver.getProjectRoot());
119
- allDependencies = allDependencies.filter(dep => current.some(edge => edge.source.file === dep.source_file && edge.source.line === dep.line_number && edge.freshness === "current"));
174
+ const currentEdgeKeys = new Set<string>();
175
+ for (const edge of current) {
176
+ if (edge.freshness === "current") {
177
+ currentEdgeKeys.add(`${edge.source.file}\0${edge.source.line}`);
178
+ }
179
+ }
180
+ allDependencies = allDependencies.filter((dep) =>
181
+ currentEdgeKeys.has(`${dep.source_file}\0${dep.line_number}`)
182
+ );
120
183
  }
121
184
 
122
185
  // Prepare file lookup maps for internal file-to-file resolution
@@ -133,7 +196,7 @@ export class BoundaryEvaluator {
133
196
  }
134
197
  };
135
198
 
136
- if (typeof this.repo.getAllFiles === "function") {
199
+ if (typeof this.repo?.getAllFiles === "function") {
137
200
  for (const f of this.repo.getAllFiles()) {
138
201
  registerKnownFile(f.path);
139
202
  }
@@ -142,9 +205,9 @@ export class BoundaryEvaluator {
142
205
  registerKnownFile(d.source_file);
143
206
  }
144
207
 
145
- const resolveTargetFile = (sourceFile: string, rawTarget: string): string | null => {
146
- if (this.repo.getMeta?.("workspace_root")) {
147
- const result = this.resolver.resolveImport(rawTarget,sourceFile);
208
+ const resolveTargetFile = (sourceFile: string, rawTarget: string, statement?: string): string | null => {
209
+ if (this.repo?.getMeta?.("workspace_root") || existsSync(resolve(this.resolver.getProjectRoot(), sourceFile))) {
210
+ const result = this.resolver.resolveImport(rawTarget,sourceFile,statement);
148
211
  const files = result.targets.flatMap(target => target.files);
149
212
  return result.status === "resolved" && result.coverage !== "conditional" && files.length === 1 ? files[0]! : null;
150
213
  }
@@ -183,16 +246,31 @@ export class BoundaryEvaluator {
183
246
  const fileGraph = new Map<string, Map<string, EdgeMeta>>();
184
247
  const domainGraph = new Map<string, Map<string, EdgeMeta>>();
185
248
 
249
+ const isTypeOnlyImport = (statement?: string): boolean => {
250
+ if (!statement) return false;
251
+ return /^\s*(?:import|export)\s+type\b/.test(statement.trim());
252
+ };
253
+
186
254
  for (const dep of allDependencies) {
187
- if (this.repo.getMeta?.("workspace_root")) {
188
- const result = this.resolver.resolveImport(dep.target,dep.source_file);
189
- if (result.status !== "resolved" || result.coverage === "conditional") continue;
255
+ const indexed = this.repo?.getMeta?.("workspace_root");
256
+ const resolution = resolutions?.get(indexedDependencyKey(dep)) ??
257
+ (indexed ? this.resolver.resolveImport(dep.target, dep.source_file, dep.statement) : undefined);
258
+ if (resolution) {
259
+ if (resolution.status !== "resolved" || resolution.coverage === "conditional") continue;
190
260
  } else if (dep.is_external) continue;
191
261
 
192
- const targetDomain = this.resolveTargetDomain(dep.target, config, dep.source_file);
193
-
194
- // 1. Domain-to-domain edge
195
- if (targetDomain && targetDomain !== dep.source_domain) {
262
+ const targetDomain = resolution
263
+ ? this.resolver.resolveDomainFromEvidence(resolution, config)
264
+ : this.resolveTargetDomain(dep.target, config, dep.source_file, dep.statement);
265
+
266
+ // 1. Domain-to-domain edge (exclude virtual/top-level aggregator "root" domain and type-only imports)
267
+ if (
268
+ targetDomain &&
269
+ targetDomain !== dep.source_domain &&
270
+ dep.source_domain !== "root" &&
271
+ targetDomain !== "root" &&
272
+ !isTypeOnlyImport(dep.statement)
273
+ ) {
196
274
  let outMap = domainGraph.get(dep.source_domain);
197
275
  if (!outMap) {
198
276
  outMap = new Map();
@@ -211,8 +289,15 @@ export class BoundaryEvaluator {
211
289
  outMap.get(targetDomain)!.files.add(dep.source_file);
212
290
  }
213
291
 
214
- // 2. File-to-file edge
215
- const targetFile = resolveTargetFile(dep.source_file, dep.target);
292
+ // 2. File-to-file edge (type-only imports are erased at compile time and do not create runtime cycles)
293
+ if (isTypeOnlyImport(dep.statement)) {
294
+ continue;
295
+ }
296
+
297
+ const targetFiles = resolution?.targets.flatMap(target => target.files);
298
+ const targetFile = targetFiles
299
+ ? targetFiles.length === 1 ? targetFiles[0] : null
300
+ : resolveTargetFile(dep.source_file, dep.target, dep.statement);
216
301
  if (targetFile && targetFile !== dep.source_file) {
217
302
  let outMap = fileGraph.get(dep.source_file);
218
303
  if (!outMap) {
@@ -237,7 +322,7 @@ export class BoundaryEvaluator {
237
322
  kindLabel: "file" | "domain"
238
323
  ) => {
239
324
  const visited = new Set<string>();
240
- const inStack = new Set<string>();
325
+ const stackIndex = new Map<string, number>();
241
326
  const stack: string[] = [];
242
327
  const reportedCycles = new Set<string>();
243
328
 
@@ -253,7 +338,7 @@ export class BoundaryEvaluator {
253
338
 
254
339
  const dfs = (node: string) => {
255
340
  visited.add(node);
256
- inStack.add(node);
341
+ stackIndex.set(node, stack.length);
257
342
  stack.push(node);
258
343
 
259
344
  const neighbors = graph.get(node);
@@ -261,9 +346,9 @@ export class BoundaryEvaluator {
261
346
  for (const [nextNode, edgeMeta] of neighbors.entries()) {
262
347
  if (!visited.has(nextNode)) {
263
348
  dfs(nextNode);
264
- } else if (inStack.has(nextNode)) {
265
- const cycleStartIndex = stack.indexOf(nextNode);
266
- if (cycleStartIndex !== -1) {
349
+ } else {
350
+ const cycleStartIndex = stackIndex.get(nextNode);
351
+ if (cycleStartIndex !== undefined) {
267
352
  const cycleNodes = stack.slice(cycleStartIndex);
268
353
  const sig = canonicalizeCycle(cycleNodes);
269
354
  const cycleEdges = cycleNodes.map((source, index) => graph.get(source)?.get(cycleNodes[(index + 1) % cycleNodes.length]));
@@ -290,7 +375,7 @@ export class BoundaryEvaluator {
290
375
  }
291
376
 
292
377
  stack.pop();
293
- inStack.delete(node);
378
+ stackIndex.delete(node);
294
379
  };
295
380
 
296
381
  for (const startNode of graph.keys()) {
@@ -309,7 +394,8 @@ export class BoundaryEvaluator {
309
394
  public checkProposedChanges(
310
395
  sourceFilePath: string,
311
396
  proposedImports: string[],
312
- config: ValidatedSeptumConfig
397
+ config: ValidatedSeptumConfig,
398
+ importStatements?: string[]
313
399
  ): BoundaryViolation[] {
314
400
  const violations: BoundaryViolation[] = [];
315
401
  const sourceDomain = this.resolveSourceDomain(sourceFilePath, config);
@@ -327,44 +413,139 @@ export class BoundaryEvaluator {
327
413
  return violations;
328
414
  }
329
415
 
416
+ for (const [index, statement] of proposedImports.entries()) {
417
+ const violation = this.evaluateDependencyRule({
418
+ sourceFile: sourceFilePath,
419
+ sourceDomain,
420
+ line: 1,
421
+ target: statement,
422
+ statement: importStatements?.[index],
423
+ config,
424
+ mode: "proposal",
425
+ });
426
+ if (violation) violations.push(violation);
427
+ }
428
+
429
+ return violations;
430
+ }
431
+
432
+ private evaluateDependencyRule(params: {
433
+ sourceFile: string;
434
+ sourceDomain: string;
435
+ line: number;
436
+ target: string;
437
+ statement?: string;
438
+ config: ValidatedSeptumConfig;
439
+ mode: "audit" | "proposal";
440
+ resolution?: ImportResolution;
441
+ }): BoundaryViolation | null {
442
+ return this.evaluateDependencyRuleWithWarning(params).violation;
443
+ }
444
+
445
+ private evaluateDependencyRuleWithWarning(params: {
446
+ sourceFile: string;
447
+ sourceDomain: string;
448
+ line: number;
449
+ target: string;
450
+ statement?: string;
451
+ config: ValidatedSeptumConfig;
452
+ mode: "audit" | "proposal";
453
+ resolution?: ImportResolution;
454
+ }): { violation: BoundaryViolation | null; warning: BoundaryViolation | null } {
455
+ const { sourceFile, sourceDomain, line, target, statement, config, mode, resolution: evidence } = params;
330
456
  const sourceDomainConfig = config.domains[sourceDomain];
331
- if (!sourceDomainConfig) {
332
- return violations;
457
+ if (!sourceDomainConfig) return { violation: null, warning: null };
458
+
459
+ const targetDomain = evidence
460
+ ? this.resolver.resolveDomainFromEvidence(evidence, config)
461
+ : this.resolveTargetDomain(target, config, sourceFile, statement);
462
+ if (!targetDomain) {
463
+ if (
464
+ config.settings.enforcement === "strict" &&
465
+ (this.repo?.getMeta?.("workspace_root") || existsSync(resolve(this.resolver.getProjectRoot(), sourceFile)))
466
+ ) {
467
+ const resolution = evidence ?? this.resolver.resolveImport(target, sourceFile, statement);
468
+ if (
469
+ resolution.status === "resolved" &&
470
+ resolution.coverage !== "conditional" &&
471
+ resolution.targets.some((t) => t.kind === "file" && t.files.length > 0)
472
+ ) {
473
+ return {
474
+ violation: {
475
+ file: sourceFile,
476
+ line,
477
+ source_domain: sourceDomain,
478
+ target_domain: "unmapped",
479
+ imported_target: target,
480
+ rule: "unmapped_domain",
481
+ message: `Unmapped internal target: '${target}' resolves inside the workspace outside any declared bounded context.`,
482
+ },
483
+ warning: null,
484
+ };
485
+ }
486
+ }
487
+ return { violation: null, warning: null };
333
488
  }
334
489
 
490
+ if (targetDomain === sourceDomain) return { violation: null, warning: null };
491
+
335
492
  const forbidden = sourceDomainConfig.forbidden_dependencies ?? [];
336
493
  const allowed = sourceDomainConfig.allowed_dependencies ?? [];
337
494
 
338
- for (const statement of proposedImports) {
339
- const targetDomain = this.resolveTargetDomain(statement, config, sourceFilePath);
340
- if (!targetDomain || targetDomain === sourceDomain) {
341
- continue;
342
- }
343
-
344
- if (forbidden.includes(targetDomain)) {
345
- violations.push({
346
- file: sourceFilePath,
347
- line: 1,
495
+ if (forbidden.includes(targetDomain)) {
496
+ return {
497
+ violation: {
498
+ file: sourceFile,
499
+ line,
348
500
  source_domain: sourceDomain,
349
501
  target_domain: targetDomain,
350
- imported_target: statement,
502
+ imported_target: target,
351
503
  rule: "forbidden_dependency",
352
- message: `Illegal import blocked: Domain '${sourceDomain}' is strictly forbidden from importing domain '${targetDomain}'.`,
353
- });
354
- } else if (config.settings.enforcement === "strict" && !allowed.includes(targetDomain)) {
355
- violations.push({
356
- file: sourceFilePath,
357
- line: 1,
504
+ message:
505
+ mode === "proposal"
506
+ ? `Illegal import blocked: Domain '${sourceDomain}' is strictly forbidden from importing domain '${targetDomain}'.`
507
+ : `Forbidden cross-domain dependency detected: '${sourceDomain}' is strictly prohibited from importing domain '${targetDomain}'.`,
508
+ },
509
+ warning: null,
510
+ };
511
+ }
512
+
513
+ if (!allowed.includes(targetDomain)) {
514
+ if (config.settings.enforcement === "strict") {
515
+ return {
516
+ violation: {
517
+ file: sourceFile,
518
+ line,
519
+ source_domain: sourceDomain,
520
+ target_domain: targetDomain,
521
+ imported_target: target,
522
+ rule: "disallowed_dependency",
523
+ message:
524
+ mode === "proposal"
525
+ ? `Unlisted domain dependency: '${targetDomain}' must be explicitly added to allowed_dependencies before domain '${sourceDomain}' can import it.`
526
+ : `Disallowed dependency: '${targetDomain}' is not declared in allowed_dependencies for domain '${sourceDomain}'.`,
527
+ },
528
+ warning: null,
529
+ };
530
+ }
531
+ return {
532
+ violation: null,
533
+ warning: {
534
+ file: sourceFile,
535
+ line,
358
536
  source_domain: sourceDomain,
359
537
  target_domain: targetDomain,
360
- imported_target: statement,
538
+ imported_target: target,
361
539
  rule: "disallowed_dependency",
362
- message: `Unlisted domain dependency: '${targetDomain}' must be explicitly added to allowed_dependencies before domain '${sourceDomain}' can import it.`,
363
- });
364
- }
540
+ message:
541
+ mode === "proposal"
542
+ ? `Unlisted dependency warning (warn mode): '${targetDomain}' is not declared in allowed_dependencies for domain '${sourceDomain}'.`
543
+ : `Unlisted dependency warning (warn mode): '${targetDomain}' is not declared in allowed_dependencies for domain '${sourceDomain}'.`,
544
+ },
545
+ };
365
546
  }
366
547
 
367
- return violations;
548
+ return { violation: null, warning: null };
368
549
  }
369
550
 
370
551
  public resolveSourceDomain(filePath: string, config: ValidatedSeptumConfig): string | null {
@@ -374,69 +555,9 @@ export class BoundaryEvaluator {
374
555
  public resolveTargetDomain(
375
556
  targetStatement: string,
376
557
  config: ValidatedSeptumConfig,
377
- sourceFilePath?: string
558
+ sourceFilePath?: string,
559
+ statement?: string
378
560
  ): string | null {
379
- return this.resolver.resolveTargetDomain(targetStatement, sourceFilePath, config);
380
- }
381
-
382
- public checkFeatureTouchpoints(
383
- featureKey: string,
384
- targetFilePath: string,
385
- config: ValidatedSeptumConfig
386
- ): BoundaryViolation[] {
387
- const violations: BoundaryViolation[] = [];
388
- const features = config.features ?? {};
389
- const featureConfig = features[featureKey];
390
-
391
- if (!featureConfig) {
392
- violations.push({
393
- file: targetFilePath,
394
- line: 1,
395
- source_domain: "unknown",
396
- target_domain: "unknown",
397
- imported_target: targetFilePath,
398
- rule: "unknown_feature",
399
- message: `Feature '${featureKey}' is not defined in septum configuration.`,
400
- });
401
- return violations;
402
- }
403
-
404
- const root = this.resolver.getProjectRoot();
405
- const normalizedTarget = relative(root, resolve(root, targetFilePath.replace(/\\/g, "/"))).replace(/\\/g, "/");
406
- const allowedTouchpoints = featureConfig.allowed_touchpoints.map((tp) =>
407
- isAbsolute(tp) ? relative(root, tp).replace(/\\/g, "/") : posix.normalize(tp.replace(/\\/g, "/"))
408
- );
409
-
410
- const domainCfg = config.domains[featureConfig.domain];
411
- const domainRoot = domainCfg?.root ? domainCfg.root.replace(/\\/g, "/").replace(/^\.\//, "") : "";
412
-
413
- const matches = (pattern: string): boolean => {
414
- if (normalizedTarget === pattern) return true;
415
- if (pattern.endsWith("/**")) {
416
- return normalizedTarget.startsWith(pattern.slice(0, -3) + "/");
417
- }
418
- if (pattern.endsWith("/*")) {
419
- const prefix = pattern.slice(0, -2) + "/";
420
- return normalizedTarget.startsWith(prefix) && !normalizedTarget.slice(prefix.length).includes("/");
421
- }
422
- return matchesSimpleGlob(normalizedTarget, pattern, false);
423
- };
424
- const isAllowed = allowedTouchpoints.some(pattern =>
425
- matches(pattern) || Boolean(domainRoot && matches(posix.normalize(`${domainRoot}/${pattern}`)))
426
- );
427
-
428
- if (!isAllowed) {
429
- violations.push({
430
- file: targetFilePath,
431
- line: 1,
432
- source_domain: featureConfig.domain,
433
- target_domain: "unknown",
434
- imported_target: targetFilePath,
435
- rule: "touchpoint_violation",
436
- message: `Scope violation: File '${targetFilePath}' is outside the declared allowed_touchpoints for feature '${featureKey}'. Permitted: [${allowedTouchpoints.join(", ")}].`,
437
- });
438
- }
439
-
440
- return violations;
561
+ return this.resolver.resolveTargetDomain(targetStatement, sourceFilePath, config, statement);
441
562
  }
442
563
  }