@dimassetoid/septum-mcp 0.3.0 → 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 (129) hide show
  1. package/README.md +38 -55
  2. package/package.json +7 -5
  3. package/src/cli/commands/check.ts +14 -10
  4. package/src/cli/commands/hook.ts +41 -6
  5. package/src/cli/commands/init.ts +10 -1
  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 +32 -4
  9. package/src/cli/index.ts +7 -0
  10. package/src/cli/templates/hooks.ts +3 -14
  11. package/src/cli/templates/skill.ts +17 -10
  12. package/src/core/boundary/audit.ts +383 -0
  13. package/src/core/boundary/evaluator.ts +251 -114
  14. package/src/core/boundary/staged-auditor.ts +147 -55
  15. package/src/core/config/domain-ownership.ts +6 -4
  16. package/src/core/config/domain-registration.ts +141 -0
  17. package/src/core/config/loader.ts +1 -25
  18. package/src/core/config/schema.ts +24 -36
  19. package/src/core/database/client.ts +2 -0
  20. package/src/core/database/repositories/dependency.repository.ts +334 -572
  21. package/src/core/database/repositories/domain.repository.ts +2 -1
  22. package/src/core/database/repositories/file.repository.ts +10 -0
  23. package/src/core/database/repositories/symbol.repository.ts +97 -398
  24. package/src/core/database/repository.ts +68 -9
  25. package/src/core/database/schema.ts +26 -0
  26. package/src/core/discovery/declarative-routes.ts +69 -0
  27. package/src/core/discovery/framework-detector.ts +149 -0
  28. package/src/core/discovery/navigation.ts +222 -58
  29. package/src/core/discovery/topology-detector.ts +93 -168
  30. package/src/core/errors.ts +19 -0
  31. package/src/core/ingestion/pipeline.ts +287 -88
  32. package/src/core/parser/extractors/base.ts +11 -3
  33. package/src/core/parser/extractors/csharp.ts +2 -2
  34. package/src/core/parser/extractors/django-routing.ts +163 -0
  35. package/src/core/parser/extractors/django-semantic.ts +397 -481
  36. package/src/core/parser/extractors/elysia-semantic.ts +172 -475
  37. package/src/core/parser/extractors/express-semantic.ts +195 -383
  38. package/src/core/parser/extractors/fastapi-semantic.ts +178 -361
  39. package/src/core/parser/extractors/fastify-semantic.ts +216 -717
  40. package/src/core/parser/extractors/flask-semantic.ts +89 -352
  41. package/src/core/parser/extractors/go-routing.ts +344 -0
  42. package/src/core/parser/extractors/go-semantic.ts +205 -562
  43. package/src/core/parser/extractors/go.ts +6 -3
  44. package/src/core/parser/extractors/graphql-entrypoint.ts +146 -0
  45. package/src/core/parser/extractors/hono-semantic.ts +132 -509
  46. package/src/core/parser/extractors/java.ts +2 -2
  47. package/src/core/parser/extractors/laravel-routing.ts +263 -0
  48. package/src/core/parser/extractors/laravel-semantic.ts +143 -335
  49. package/src/core/parser/extractors/manifest-helpers.ts +64 -0
  50. package/src/core/parser/extractors/mcp-cli-dispatch.ts +268 -0
  51. package/src/core/parser/extractors/mcp-cli-semantic.ts +82 -333
  52. package/src/core/parser/extractors/nestjs-semantic.ts +238 -494
  53. package/src/core/parser/extractors/nextjs-semantic.ts +164 -0
  54. package/src/core/parser/extractors/php-ast-helpers.ts +51 -0
  55. package/src/core/parser/extractors/php.ts +1 -1
  56. package/src/core/parser/extractors/python-route-ast.ts +249 -0
  57. package/src/core/parser/extractors/python.ts +5 -9
  58. package/src/core/parser/extractors/registry.ts +2 -0
  59. package/src/core/parser/extractors/route-prefixes.ts +43 -0
  60. package/src/core/parser/extractors/ruby.ts +2 -2
  61. package/src/core/parser/extractors/rust.ts +4 -4
  62. package/src/core/parser/extractors/symfony-routing.ts +114 -0
  63. package/src/core/parser/extractors/symfony-semantic.ts +72 -226
  64. package/src/core/parser/extractors/typescript-bindings.ts +333 -0
  65. package/src/core/parser/extractors/typescript-compiler-fallback.ts +380 -0
  66. package/src/core/parser/extractors/typescript-fallback.ts +207 -0
  67. package/src/core/parser/extractors/typescript-route-context.ts +58 -0
  68. package/src/core/parser/extractors/typescript.ts +23 -216
  69. package/src/core/parser/language-capabilities.ts +1 -1
  70. package/src/core/parser/schema/providers/laravel-migration-provider.ts +4 -1
  71. package/src/core/parser/wasm-loader.ts +6 -2
  72. package/src/core/resolver/ast-method-tracer.ts +230 -0
  73. package/src/core/resolver/call-graph-helpers.ts +72 -0
  74. package/src/core/resolver/call-graph-tracer.ts +169 -414
  75. package/src/core/resolver/dependency-analysis.ts +593 -0
  76. package/src/core/resolver/import-candidates.ts +120 -0
  77. package/src/core/resolver/import-evidence.ts +88 -0
  78. package/src/core/resolver/import-resolver.ts +60 -7
  79. package/src/core/resolver/interpretation-inputs.ts +60 -2
  80. package/src/core/resolver/languages/python.ts +31 -13
  81. package/src/core/resolver/languages/shared.ts +4 -1
  82. package/src/core/resolver/module-resolver.ts +10 -3
  83. package/src/core/resolver/path-resolver.ts +6 -3
  84. package/src/core/resolver/polyglot-slice-tracer.ts +197 -0
  85. package/src/core/resolver/slice-catalog.ts +21 -6
  86. package/src/core/resolver/slice-format.ts +13 -0
  87. package/src/core/resolver/slice-selection.ts +237 -0
  88. package/src/core/resolver/symbol-impact.ts +104 -8
  89. package/src/core/resolver/symbol-locator.ts +82 -15
  90. package/src/core/resolver/vertical-slice-tracer.ts +31 -230
  91. package/src/core/resources/ignore.ts +0 -1
  92. package/src/core/resources/limits.ts +2 -1
  93. package/src/core/resources/work-queue.ts +167 -4
  94. package/src/core/retrieval/context-page.ts +11 -4
  95. package/src/core/retrieval/context.ts +363 -226
  96. package/src/{mcp/tools/get-symbol.ts → core/retrieval/source-context.ts} +145 -96
  97. package/src/core/retrieval/symbol-details.ts +453 -0
  98. package/src/core/retrieval/symbol-evidence.ts +3 -3
  99. package/src/core/retrieval/typescript-references.ts +38 -7
  100. package/src/core/retrieval/universal-context.ts +182 -0
  101. package/src/core/telemetry/summary.ts +114 -0
  102. package/src/core/telemetry/telemetry.ts +192 -49
  103. package/src/mcp/formatters/compact-text.ts +439 -0
  104. package/src/mcp/resources-and-prompts.ts +253 -0
  105. package/src/mcp/schemas/boundary.ts +145 -0
  106. package/src/mcp/schemas/map.ts +250 -0
  107. package/src/mcp/schemas/shared.ts +52 -0
  108. package/src/mcp/schemas/slice.ts +60 -0
  109. package/src/mcp/schemas/symbol.ts +268 -0
  110. package/src/mcp/schemas.ts +5 -746
  111. package/src/mcp/server.ts +269 -429
  112. package/src/mcp/tool-catalog.ts +331 -0
  113. package/src/mcp/tools/boundary.ts +68 -3
  114. package/src/mcp/tools/map.ts +104 -4
  115. package/src/mcp/tools/slice.ts +101 -3
  116. package/src/mcp/tools/symbol.ts +469 -17
  117. package/src/mcp/ui/app-client.ts +899 -0
  118. package/src/mcp/ui/app.html +478 -0
  119. package/src/mcp/workflow-playbooks.ts +181 -0
  120. package/src/mcp/workspace-boundary.ts +50 -0
  121. package/src/mcp/workspace-manager.ts +88 -15
  122. package/src/mcp/workspace-watcher.ts +30 -11
  123. package/src/types/index.ts +12 -0
  124. package/src/mcp/tools/check-boundary.ts +0 -382
  125. package/src/mcp/tools/get-domain-catalog.ts +0 -39
  126. package/src/mcp/tools/get-symbol-impact.ts +0 -104
  127. package/src/mcp/tools/locate-symbol.ts +0 -236
  128. package/src/mcp/tools/register-domain.ts +0 -113
  129. package/src/mcp/tools/trace-vertical-slice.ts +0 -106
@@ -0,0 +1,439 @@
1
+ /**
2
+ * Token-efficient text formatters for all 4 Septum MCP tools (default format='text' in v0.4.0).
3
+ * Preserves all architectural signals, freshness/coverage limits, and next-step hints
4
+ * while eliminating repetitive JSON punctuation and field keys.
5
+ */
6
+
7
+ export function formatMapAsText(payload: Record<string, any>): string {
8
+ if (payload.status === "not_found" || payload.status === "NOT_INDEXED") {
9
+ const lines = [`[${String(payload.status).toUpperCase()}] ${payload.message ?? "No navigation results."}`];
10
+ if (Array.isArray(payload.available_domains) && payload.available_domains.length) {
11
+ lines.push(`Domains: ${payload.available_domains.join(", ")}`);
12
+ }
13
+ return lines.join("\n");
14
+ }
15
+
16
+ const scope = payload.scope ?? {};
17
+ const headerScope = scope.file_filter
18
+ ? `${scope.domain ?? "workspace"} (${scope.file_filter})`
19
+ : scope.domain ?? "workspace";
20
+ const health = payload.health ?? {};
21
+ const acc = payload.accounting ?? {};
22
+ const lines: string[] = [
23
+ `# Septum Map: ${headerScope} [${payload.status}]`,
24
+ `Health: index=${health.index ?? "unknown"} coverage=${health.coverage ?? "unknown"} sync_needed=${Boolean(health.sync_needed)} unresolved=${health.unresolved_imports ?? 0}`,
25
+ ];
26
+
27
+ if (Array.isArray(payload.domains) && payload.domains.length) {
28
+ lines.push("", "## Domains");
29
+ for (const d of payload.domains) {
30
+ const allowed = Array.isArray(d.allowed_dependencies) && d.allowed_dependencies.length ? d.allowed_dependencies.join(",") : "-";
31
+ const forbidden = Array.isArray(d.forbidden_dependencies) && d.forbidden_dependencies.length ? ` forbidden:[${d.forbidden_dependencies.join(",")}]` : "";
32
+ lines.push(`- ${d.name} (${d.root}, ${d.file_count} files) allowed:[${allowed}]${forbidden}`);
33
+ }
34
+ }
35
+
36
+ if (Array.isArray(payload.submodules) && payload.submodules.length) {
37
+ lines.push("", "## Submodules (zoom in via file_filter)");
38
+ for (const sub of payload.submodules) {
39
+ const anchors = Array.isArray(sub.top_anchors) && sub.top_anchors.length ? ` anchors: ${sub.top_anchors.join(", ")}` : "";
40
+ lines.push(`- ${sub.path} (${sub.file_count} files)${anchors}`);
41
+ }
42
+ }
43
+
44
+ if (Array.isArray(payload.files) && payload.files.length) {
45
+ const omitted = acc.omitted_files ? ` (+${acc.omitted_files} omitted)` : "";
46
+ lines.push("", `## Files (${payload.files.length}/${acc.matching_files ?? payload.files.length}${omitted})`);
47
+ for (const f of payload.files) {
48
+ const anchor = f.anchor ? ` anchor:${f.anchor}` : "";
49
+ lines.push(`- ${f.path} [${f.archetype}] (${f.symbol_count} syms${anchor})`);
50
+ }
51
+ }
52
+
53
+ if (Array.isArray(payload.dependencies) && payload.dependencies.length) {
54
+ const isAggregated = Array.isArray(payload.submodules) && payload.submodules.length > 0;
55
+ const omitted = !isAggregated && acc.omitted_dependencies ? ` (+${acc.omitted_dependencies} omitted)` : "";
56
+ const depTitle = isAggregated
57
+ ? `## Dependencies (aggregated by submodule, ${payload.dependencies.length} groups)`
58
+ : `## Dependencies (${payload.dependencies.length}/${acc.total_dependencies ?? payload.dependencies.length}${omitted})`;
59
+ lines.push("", depTitle);
60
+ for (const dep of payload.dependencies) {
61
+ const count = dep.count > 1 ? ` x${dep.count}` : "";
62
+ lines.push(`- ${dep.from} -> ${dep.to}${count}`);
63
+ }
64
+ }
65
+
66
+ if (Array.isArray(payload.entrypoints) && payload.entrypoints.length) {
67
+ lines.push("", `## Entrypoints (${payload.entrypoints.length})`);
68
+ for (const ep of payload.entrypoints) {
69
+ lines.push(`- ${ep.method} ${ep.target} -> ${ep.handler} (${ep.file ?? "unknown"})`);
70
+ }
71
+ }
72
+
73
+ if (Array.isArray(payload.evidence) && payload.evidence.length) {
74
+ lines.push("", `## Import Evidence (${payload.evidence.length})`);
75
+ for (const ev of payload.evidence) {
76
+ const targets = ev.resolution?.targets?.flatMap((t: any) => t.files ?? []).join(", ") || ev.resolution?.reason || "unresolved";
77
+ lines.push(`- L${ev.source?.line ?? "?"}: ${ev.resolution?.source?.target ?? "?"} => ${ev.resolution?.status ?? "?"} (${targets})`);
78
+ }
79
+ }
80
+
81
+ if (Array.isArray(payload.next) && payload.next.length) {
82
+ lines.push("", "## Next");
83
+ for (const n of payload.next) {
84
+ lines.push(`- ${n.tool}(${JSON.stringify(n.args)})`);
85
+ }
86
+ }
87
+
88
+ if (payload.relationship_limits) {
89
+ lines.push("", `Limits: ${payload.relationship_limits}`);
90
+ }
91
+ return lines.join("\n");
92
+ }
93
+
94
+ export function formatSliceAsText(payload: Record<string, any>): string {
95
+ const ep = payload.entrypoint;
96
+ const lines: string[] = [
97
+ `# Septum Slice: ${payload.query} [${payload.status}]`,
98
+ `Match: found=${Boolean(payload.found)} exact=${Boolean(payload.is_exact_match)} confidence=${payload.confidence ?? "none"} style=${payload.architecture_style ?? "unknown"} depth=${payload.max_depth_applied ?? "?"}`,
99
+ ];
100
+
101
+ if (ep) {
102
+ lines.push(`Entrypoint: ${ep.method} ${ep.uri} -> ${ep.controller}@${ep.action} (${ep.file ?? "?"}:${ep.line ?? "?"})`);
103
+ }
104
+
105
+ if (Array.isArray(payload.diagnostics) && payload.diagnostics.length) {
106
+ lines.push(`Diagnostics: ${payload.diagnostics.join(", ")}`);
107
+ }
108
+
109
+ if (Array.isArray(payload.chain) && payload.chain.length) {
110
+ lines.push("", `## Chain (${payload.chain.length} nodes)`);
111
+ for (const node of payload.chain) {
112
+ const indent = typeof node.depth === "number" && node.depth > 0 ? " ".repeat(Math.min(node.depth, 6)) : "";
113
+ const loc = node.file ? ` @ ${node.file}:${node.line ?? 1}` : "";
114
+ const rel = node.relationship ? ` [${node.relationship.kind}; ${node.relationship.reason}]` : "";
115
+ const via = node.parent_symbol ? ` (via ${node.parent_symbol})` : "";
116
+ lines.push(`${indent}- [${String(node.stage).toUpperCase()}] ${node.symbol}${loc}${rel}${via}`);
117
+ }
118
+ } else if (payload.summary) {
119
+ lines.push("", payload.summary);
120
+ }
121
+
122
+ if (Array.isArray(payload.alternatives) && payload.alternatives.length) {
123
+ lines.push("", "## Alternatives");
124
+ for (const alt of payload.alternatives.slice(0, 5)) {
125
+ lines.push(`- ${alt.method} ${alt.uri} -> ${alt.controller}@${alt.action}${alt.file ? ` (${alt.file})` : ""}`);
126
+ }
127
+ }
128
+
129
+ if (Array.isArray(payload.next) && payload.next.length) {
130
+ lines.push("", "## Next");
131
+ for (const n of payload.next) {
132
+ lines.push(`- ${typeof n === "string" ? n : JSON.stringify(n)}`);
133
+ }
134
+ }
135
+
136
+ if (payload.relationship_limits) {
137
+ lines.push("", `Limits: ${payload.relationship_limits}`);
138
+ }
139
+ return lines.join("\n");
140
+ }
141
+
142
+ export function formatLocateAsText(payload: Record<string, any>): string {
143
+ const status = payload.status ?? (payload.found ? "success" : "not_found");
144
+ const freshness = payload.retrieval?.freshness ?? "unknown";
145
+ const lines: string[] = [`# Septum Locate: ${payload.query ?? payload.file_path ?? "?"} [${status}]`];
146
+
147
+ if (status === "NOT_INDEXED" || status === "stale_reference") {
148
+ lines.push(payload.message ?? "Workspace not indexed or reference stale.");
149
+ return lines.join("\n");
150
+ }
151
+
152
+ // File outline mode (query="*" with file_path)
153
+ if (Array.isArray(payload.candidates) && payload.file_path && !payload.exact_symbol && status === "success") {
154
+ const omitted = payload.omitted_candidates ? ` (+${payload.omitted_candidates} omitted)` : "";
155
+ lines.push(`File: ${payload.file_path} (${payload.candidates.length}/${payload.total_candidates ?? payload.candidates.length} declarations${omitted}) freshness=${freshness}`, "");
156
+ for (const c of payload.candidates) {
157
+ lines.push(`- L${c.start_line}-${c.end_line} [${c.kind}] ${c.name}${c.signature ? `: ${String(c.signature).replace(/\s+/g, " ").slice(0, 120)}` : ""}`);
158
+ }
159
+ return lines.join("\n");
160
+ }
161
+
162
+ // Ambiguous candidates
163
+ if (status === "ambiguous" && Array.isArray(payload.candidates)) {
164
+ lines.push(payload.message ?? "Multiple matching declarations.", "");
165
+ for (const c of payload.candidates) {
166
+ const ref = c.symbol_ref;
167
+ const span = ref ? `:${ref.start_line}-${ref.end_line}` : "";
168
+ lines.push(`- ${c.name} @ ${c.file_path}${span}${c.signature ? ` — ${String(c.signature).replace(/\s+/g, " ").slice(0, 100)}` : ""}`);
169
+ }
170
+ return lines.join("\n");
171
+ }
172
+
173
+ // Exact match
174
+ if (payload.found && payload.exact_symbol) {
175
+ const sym = payload.exact_symbol;
176
+ const ref = payload.symbol_ref;
177
+ const sig = ref?.signature ?? sym.signature;
178
+ lines.push(`Target: ${sym.name} [${sym.kind}, ${sym.visibility ?? "public"}] @ ${payload.file_path}:${sym.start_line ?? sym.line_start}-${sym.end_line ?? sym.line_end} (${sym.total_lines ?? "?"} lines) freshness=${freshness}`);
179
+ if (sig) lines.push(`Signature: ${String(sig).replace(/\s+/g, " ")}`);
180
+ if (payload.complexity_smell?.warning) lines.push(`Warning: ${payload.complexity_smell.warning}`);
181
+ if (Array.isArray(payload.sibling_methods) && payload.sibling_methods.length) {
182
+ lines.push(`Siblings (${payload.sibling_methods.length}): ${payload.sibling_methods.slice(0, 15).join(", ")}${payload.sibling_methods.length > 15 ? ", ..." : ""}`);
183
+ }
184
+ return lines.join("\n");
185
+ }
186
+
187
+ // Not found + suggestions
188
+ lines.push(payload.message ?? `Symbol '${payload.query}' not found.`);
189
+ if (Array.isArray(payload.suggestions) && payload.suggestions.length) {
190
+ lines.push("", "## Suggestions");
191
+ for (const s of payload.suggestions) {
192
+ lines.push(`- ${s.name} [${s.kind}] (${s.similarity_score}) @ ${s.file_path ?? "?"}:${s.start_line ?? s.line_start}`);
193
+ }
194
+ }
195
+ return lines.join("\n");
196
+ }
197
+
198
+ export function formatSourceContextAsText(payload: Record<string, any>): string {
199
+ if (!payload.symbol && payload.status) {
200
+ return `# Septum Symbol Context [${payload.status}]\n${payload.message ?? "Unable to inspect symbol."}`;
201
+ }
202
+ const range = payload.exact_range ?? {};
203
+ const freshness = payload.retrieval?.freshness ?? "unknown";
204
+ const lines: string[] = [
205
+ `# Septum Symbol: ${payload.symbol} [${payload.kind ?? "symbol"}] @ ${payload.file_path}:${range.start_line ?? "?"}-${range.end_line ?? "?"} (${range.total_lines ?? "?"}L)`,
206
+ `Domain: ${payload.domain ?? "unknown"} | Visibility: ${payload.visibility ?? "unknown"} | Freshness: ${freshness} | Mode: source`,
207
+ ];
208
+ if (payload.signature) lines.push(`Signature: ${String(payload.signature).replace(/\s+/g, " ")}`);
209
+ if (Array.isArray(payload.dependencies_injected) && payload.dependencies_injected.length) {
210
+ lines.push(`Injected/Params: ${payload.dependencies_injected.join(", ")}`);
211
+ }
212
+ if (Array.isArray(payload.resolved_outbound_calls) && payload.resolved_outbound_calls.length) {
213
+ lines.push("", `## Outbound Calls (${payload.resolved_outbound_calls.length})`);
214
+ for (const c of payload.resolved_outbound_calls) {
215
+ const target = c.target_file
216
+ ? ` -> ${c.target_file}:${c.target_range?.start_line ?? "?"}-${c.target_range?.end_line ?? "?"}`
217
+ : " (unresolved)";
218
+ lines.push(`- ${c.symbol}${target}`);
219
+ }
220
+ } else if (Array.isArray(payload.outbound_calls) && payload.outbound_calls.length) {
221
+ lines.push(`Outbound Calls: ${payload.outbound_calls.join(", ")}`);
222
+ }
223
+ if (payload.connected_files) {
224
+ const cf = payload.connected_files;
225
+ if (Array.isArray(cf.imports_from) && cf.imports_from.length) {
226
+ lines.push("", `Imports (${cf.imports_from.length}): ${cf.imports_from.join(", ")}`);
227
+ }
228
+ if (Array.isArray(cf.imported_by) && cf.imported_by.length) {
229
+ lines.push(`Imported by (${cf.imported_by_count ?? cf.imported_by.length}): ${cf.imported_by.join(", ")}`);
230
+ }
231
+ }
232
+ const sourceText = typeof payload.source_code === "string"
233
+ ? payload.source_code
234
+ : (typeof payload.source_slice?.text === "string" ? payload.source_slice.text : undefined);
235
+ if (typeof sourceText === "string") {
236
+ const slice = payload.source_slice;
237
+ const sliceNote = slice && slice.start_line !== undefined && slice.end_line !== undefined
238
+ ? ` (L${slice.start_line}-${slice.end_line}${slice.truncated ? `, next_start_line=${slice.next_start_line}` : ""})`
239
+ : "";
240
+ lines.push("", `## Source${sliceNote}`, "```", sourceText, "```");
241
+ }
242
+ return lines.join("\n");
243
+ }
244
+
245
+ export function formatDeepContextAsText(payload: Record<string, any>): string {
246
+ if (!Array.isArray(payload.items)) {
247
+ const lines = [`# Septum Deep Context [${payload.status ?? "error"}]`];
248
+ if (payload.message) lines.push(payload.message);
249
+ if (Array.isArray(payload.candidates) && payload.candidates.length) {
250
+ lines.push("", "## Candidates");
251
+ for (const c of payload.candidates) {
252
+ lines.push(`- ${c.name} [${c.kind}] @ ${c.file_path}:${c.start_line}-${c.end_line}`);
253
+ }
254
+ }
255
+ return lines.join("\n");
256
+ }
257
+
258
+ const targetName = typeof payload.target === "string" ? payload.target : (payload.target?.name ?? "?");
259
+ const targetFile = typeof payload.file_path === "string" ? payload.file_path : (payload.target?.file_path ?? "?");
260
+ const targetStart = payload.target?.start_line ?? "";
261
+ const targetEnd = payload.target?.end_line ?? "";
262
+ const targetRange = targetStart && targetEnd ? `:${targetStart}-${targetEnd}` : "";
263
+ const acc = payload.accounting ?? {};
264
+ const lines: string[] = [
265
+ `# Septum Deep Context: ${targetName} @ ${targetFile}${targetRange} [${payload.status}]`,
266
+ `Freshness: ${payload.freshness ?? "unknown"} | Depth: ${payload.dependency_depth ?? "?"} | Items: ${acc.returned ?? payload.items.length}/${acc.total ?? payload.items.length} (+${acc.omitted_after ?? 0} after) | Bytes: ${payload.output_bytes ?? "?"}/${payload.budget_bytes ?? "?"}`,
267
+ ];
268
+
269
+ const diagnostics: string[] = [];
270
+ for (const item of payload.items) {
271
+ if (item.kind === "source" && item.source) {
272
+ const s = item.source;
273
+ const frag = item.fragment ? ` [fragment ${item.fragment.start_utf16}..${item.fragment.end_utf16}/${item.fragment.total_utf16}]` : "";
274
+ const lang = s.file_path.endsWith(".py") ? "python" : s.file_path.endsWith(".go") ? "go" : s.file_path.endsWith(".php") ? "php" : "ts";
275
+ lines.push(
276
+ "",
277
+ `## [hop=${item.depth ?? 0}] ${item.name} @ ${s.file_path}:${s.start_line}-${s.end_line} (${item.reason}${frag})`,
278
+ `\`\`\`${lang}`,
279
+ s.text,
280
+ "```"
281
+ );
282
+ } else if (item.kind === "type_contract") {
283
+ lines.push(
284
+ "",
285
+ `## [hop=${item.depth ?? 1}] ${item.name} @ ${item.file_path}:${item.start_line}-${item.end_line} (type_contract)`,
286
+ "```ts",
287
+ item.text,
288
+ "```"
289
+ );
290
+ } else if (item.kind === "relationship" && item.relationship) {
291
+ const rel = item.relationship;
292
+ lines.push(
293
+ "",
294
+ `## [import] ${rel.source.file}:${rel.source.line} -> ${rel.resolution?.source?.target ?? rel.target} [${rel.resolution?.status ?? "resolved"}]`
295
+ );
296
+ } else if (item.kind === "diagnostic") {
297
+ diagnostics.push(`${item.reason}${item.file_path ? ` (${item.file_path})` : ""}${item.detail ? `: ${item.detail}` : ""}`);
298
+ }
299
+ }
300
+
301
+ if (diagnostics.length) {
302
+ lines.push("", "## Diagnostics");
303
+ for (const d of diagnostics.slice(0, 10)) lines.push(`- ${d}`);
304
+ }
305
+ if (payload.continuation) {
306
+ lines.push("", `Continuation: ${payload.continuation}`);
307
+ }
308
+ lines.push("", `Limits: ${payload.capability_limits ?? "static references, not calls"}; ${payload.completeness_scope ?? ""}`);
309
+ return lines.join("\n");
310
+ }
311
+
312
+ export function formatImpactAsText(payload: Record<string, any>): string {
313
+ if (payload.status === "ambiguous" || payload.status === "stale_reference") {
314
+ const lines = [`# Septum Impact: ${payload.target_symbol ?? "?"} [${payload.status}]`, payload.message ?? ""];
315
+ if (Array.isArray(payload.candidates)) {
316
+ for (const c of payload.candidates) lines.push(`- ${c.name} @ ${c.file_path}`);
317
+ }
318
+ return lines.join("\n");
319
+ }
320
+
321
+ const ref = payload.symbol_ref;
322
+ const loc = ref ? ` @ ${ref.file_path}:${ref.start_line}-${ref.end_line}` : "";
323
+ const sum = payload.impact_summary ?? {};
324
+ const freshness = payload.retrieval?.freshness ?? "unknown";
325
+ const callSites = Array.isArray(payload.call_sites) ? payload.call_sites : [];
326
+ const runtimeImports = Array.isArray(payload.runtime_imports) ? payload.runtime_imports : [];
327
+ const typeOnlyImports = Array.isArray(payload.type_only_imports) ? payload.type_only_imports : [];
328
+
329
+ const lines: string[] = [
330
+ `# Septum Impact: ${payload.target_symbol ?? "?"}${loc} [risk=${sum.risk_level ?? "unknown"}]`,
331
+ `Files: ${sum.affected_files_count ?? sum.total_dependents ?? 0} | Call sites: ${callSites.length} | Runtime imports: ${runtimeImports.length} | Type-only imports: ${typeOnlyImports.length} | Freshness: ${freshness}`,
332
+ ];
333
+
334
+ if (!payload.found_in_index) {
335
+ if (payload.warning) lines.push(`Warning: ${payload.warning}`);
336
+ if (Array.isArray(payload.suggestions) && payload.suggestions.length) {
337
+ lines.push(`Suggestions: ${payload.suggestions.join(", ")}`);
338
+ }
339
+ return lines.join("\n");
340
+ }
341
+
342
+ if (callSites.length) {
343
+ lines.push("", `## Call Sites (${callSites.length})`);
344
+ for (const c of callSites) {
345
+ lines.push(`- ${c.file}:${c.line} ${c.caller} [${c.kind ?? "call"}${c.domain ? `, ${c.domain}` : ""}]`);
346
+ }
347
+ }
348
+
349
+ if (runtimeImports.length) {
350
+ lines.push("", `## Runtime Imports (${runtimeImports.length})`);
351
+ for (const r of runtimeImports) {
352
+ lines.push(`- ${r.file}:${r.line}${r.domain ? ` [${r.domain}]` : ""}`);
353
+ }
354
+ }
355
+
356
+ if (typeOnlyImports.length) {
357
+ lines.push("", `## Type-Only Imports (${typeOnlyImports.length})`);
358
+ for (const t of typeOnlyImports) {
359
+ lines.push(`- ${t.file}:${t.line}${t.domain ? ` [${t.domain}]` : ""}`);
360
+ }
361
+ }
362
+
363
+ if (Array.isArray(payload.resolution_warnings) && payload.resolution_warnings.length) {
364
+ lines.push("", `## Resolution Warnings (${payload.resolution_warnings.length})`);
365
+ for (const w of payload.resolution_warnings.slice(0, 10)) {
366
+ lines.push(`- ${w.source?.file ?? "?"}:${w.source?.line ?? "?"} (${w.resolution?.reason ?? "unresolved"})`);
367
+ }
368
+ }
369
+
370
+ lines.push("", `Limits: ${payload.relationship_limits ?? "Incomplete static consumer evidence."}`);
371
+ return lines.join("\n");
372
+ }
373
+
374
+ export function formatBoundaryAsText(payload: Record<string, any>): string {
375
+ const scope = payload.scope ?? "file";
376
+ const target = payload.file ? ` (${payload.file})` : payload.domain ? ` (${payload.domain})` : "";
377
+ const mode = payload.enforcement_mode ? ` enforcement=${payload.enforcement_mode}` : "";
378
+ const lines: string[] = [
379
+ `# Septum Boundary: ${scope}${target} [${payload.status}]${mode}`,
380
+ ];
381
+
382
+ if (payload.message) lines.push(payload.message);
383
+
384
+ if (payload.total_domains_checked !== undefined) {
385
+ lines.push(`Checked: ${payload.total_domains_checked} domains | Violations: ${payload.total_violations ?? 0} | Warnings: ${payload.total_warnings ?? 0} | Circular: ${payload.circular_dependencies_count ?? 0}`);
386
+ } else if (payload.violations_count !== undefined || payload.total_violations !== undefined) {
387
+ lines.push(`Violations: ${payload.total_violations ?? payload.violations_count ?? 0}`);
388
+ }
389
+
390
+ if (Array.isArray(payload.violations) && payload.violations.length) {
391
+ lines.push("", `## Violations (${payload.violations.length})`);
392
+ for (const v of payload.violations) {
393
+ lines.push(`- [${v.rule}] ${v.file}:${v.line ?? 1} (${v.source_domain} -> ${v.target_domain}): ${v.message}`);
394
+ }
395
+ }
396
+
397
+ if (Array.isArray(payload.warnings) && payload.warnings.length) {
398
+ lines.push("", `## Warnings (${payload.warnings.length})`);
399
+ for (const w of payload.warnings) {
400
+ lines.push(`- [${w.rule}] ${w.file}:${w.line ?? 1} (${w.source_domain} -> ${w.target_domain}): ${w.message}`);
401
+ }
402
+ }
403
+
404
+ if (Array.isArray(payload.pre_existing_violations) && payload.pre_existing_violations.length) {
405
+ lines.push("", `## Pre-existing Codebase Violations (${payload.pre_existing_violations.length})`);
406
+ for (const v of payload.pre_existing_violations.slice(0, 5)) {
407
+ lines.push(`- [${v.rule}] ${v.file}:${v.line ?? 1}: ${v.message}`);
408
+ }
409
+ if (payload.pre_existing_violations.length > 5) {
410
+ lines.push(` ... and ${payload.pre_existing_violations.length - 5} more pre-existing violations.`);
411
+ }
412
+ }
413
+
414
+ if (Array.isArray(payload.circular_dependencies) && payload.circular_dependencies.length) {
415
+ lines.push("", `## Circular Dependencies (${payload.circular_dependencies.length})`);
416
+ for (const c of payload.circular_dependencies) {
417
+ lines.push(`- ${typeof c === "string" ? c : JSON.stringify(c)}`);
418
+ }
419
+ }
420
+
421
+ if (payload.impact_summary) {
422
+ const imp = payload.impact_summary;
423
+ lines.push("", `Impact: ${imp.direct_dependents ?? 0} direct dependents, risk=${imp.risk_level ?? "unknown"}${Array.isArray(imp.top_consumers) && imp.top_consumers.length ? ` (${imp.top_consumers.join(", ")})` : ""}`);
424
+ }
425
+
426
+ if (Array.isArray(payload.files) && payload.files.length) {
427
+ lines.push("", `## Files (${payload.files.length})`);
428
+ for (const f of payload.files) {
429
+ lines.push(`- ${f.file}: [${f.status}] violations=${f.violations_count ?? f.total_violations ?? 0}`);
430
+ }
431
+ }
432
+
433
+ if (Array.isArray(payload.next) && payload.next.length) {
434
+ lines.push("", "## Next");
435
+ for (const n of payload.next) lines.push(`- ${typeof n === "string" ? n : JSON.stringify(n)}`);
436
+ }
437
+
438
+ return lines.join("\n");
439
+ }