@massu/core 0.1.1 → 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 (87) hide show
  1. package/README.md +2 -2
  2. package/dist/hooks/cost-tracker.js +23 -35
  3. package/dist/hooks/post-edit-context.js +2 -2
  4. package/dist/hooks/post-tool-use.js +43 -58
  5. package/dist/hooks/pre-compact.js +23 -38
  6. package/dist/hooks/pre-delete-check.js +18 -31
  7. package/dist/hooks/quality-event.js +23 -35
  8. package/dist/hooks/session-end.js +62 -78
  9. package/dist/hooks/session-start.js +33 -42
  10. package/dist/hooks/user-prompt.js +23 -38
  11. package/package.json +8 -14
  12. package/src/adr-generator.ts +9 -2
  13. package/src/analytics.ts +9 -3
  14. package/src/audit-trail.ts +10 -3
  15. package/src/cloud-sync.ts +14 -18
  16. package/src/commands/init.ts +1 -5
  17. package/src/cost-tracker.ts +11 -6
  18. package/src/dependency-scorer.ts +9 -2
  19. package/src/docs-tools.ts +13 -10
  20. package/src/hooks/post-edit-context.ts +3 -3
  21. package/src/hooks/session-end.ts +3 -3
  22. package/src/hooks/session-start.ts +2 -2
  23. package/src/memory-db.ts +1351 -23
  24. package/src/memory-tools.ts +14 -15
  25. package/src/observability-tools.ts +13 -2
  26. package/src/prompt-analyzer.ts +9 -2
  27. package/src/regression-detector.ts +9 -3
  28. package/src/security-scorer.ts +9 -2
  29. package/src/sentinel-db.ts +43 -88
  30. package/src/sentinel-tools.ts +8 -11
  31. package/src/server.ts +1 -2
  32. package/src/team-knowledge.ts +9 -2
  33. package/src/tools.ts +771 -35
  34. package/src/validate-features-runner.ts +0 -1
  35. package/src/validation-engine.ts +9 -2
  36. package/dist/cli.js +0 -7890
  37. package/dist/server.js +0 -7008
  38. package/src/__tests__/adr-generator.test.ts +0 -260
  39. package/src/__tests__/analytics.test.ts +0 -282
  40. package/src/__tests__/audit-trail.test.ts +0 -382
  41. package/src/__tests__/backfill-sessions.test.ts +0 -690
  42. package/src/__tests__/cli.test.ts +0 -290
  43. package/src/__tests__/cloud-sync.test.ts +0 -261
  44. package/src/__tests__/config-sections.test.ts +0 -359
  45. package/src/__tests__/config.test.ts +0 -732
  46. package/src/__tests__/cost-tracker.test.ts +0 -348
  47. package/src/__tests__/db.test.ts +0 -177
  48. package/src/__tests__/dependency-scorer.test.ts +0 -325
  49. package/src/__tests__/docs-integration.test.ts +0 -178
  50. package/src/__tests__/docs-tools.test.ts +0 -199
  51. package/src/__tests__/domains.test.ts +0 -236
  52. package/src/__tests__/hooks.test.ts +0 -221
  53. package/src/__tests__/import-resolver.test.ts +0 -95
  54. package/src/__tests__/integration/path-traversal.test.ts +0 -134
  55. package/src/__tests__/integration/pricing-consistency.test.ts +0 -88
  56. package/src/__tests__/integration/tool-registration.test.ts +0 -146
  57. package/src/__tests__/memory-db.test.ts +0 -404
  58. package/src/__tests__/memory-enhancements.test.ts +0 -316
  59. package/src/__tests__/memory-tools.test.ts +0 -199
  60. package/src/__tests__/middleware-tree.test.ts +0 -177
  61. package/src/__tests__/observability-tools.test.ts +0 -595
  62. package/src/__tests__/observability.test.ts +0 -437
  63. package/src/__tests__/observation-extractor.test.ts +0 -167
  64. package/src/__tests__/page-deps.test.ts +0 -60
  65. package/src/__tests__/prompt-analyzer.test.ts +0 -298
  66. package/src/__tests__/regression-detector.test.ts +0 -295
  67. package/src/__tests__/rules.test.ts +0 -87
  68. package/src/__tests__/schema-mapper.test.ts +0 -29
  69. package/src/__tests__/security-scorer.test.ts +0 -238
  70. package/src/__tests__/security-utils.test.ts +0 -175
  71. package/src/__tests__/sentinel-db.test.ts +0 -491
  72. package/src/__tests__/sentinel-scanner.test.ts +0 -750
  73. package/src/__tests__/sentinel-tools.test.ts +0 -324
  74. package/src/__tests__/sentinel-types.test.ts +0 -750
  75. package/src/__tests__/server.test.ts +0 -452
  76. package/src/__tests__/session-archiver.test.ts +0 -524
  77. package/src/__tests__/session-state-generator.test.ts +0 -900
  78. package/src/__tests__/team-knowledge.test.ts +0 -327
  79. package/src/__tests__/tools.test.ts +0 -340
  80. package/src/__tests__/transcript-parser.test.ts +0 -195
  81. package/src/__tests__/trpc-index.test.ts +0 -25
  82. package/src/__tests__/validate-features-runner.test.ts +0 -517
  83. package/src/__tests__/validation-engine.test.ts +0 -300
  84. package/src/core-tools.ts +0 -685
  85. package/src/memory-queries.ts +0 -804
  86. package/src/memory-schema.ts +0 -546
  87. package/src/tool-helpers.ts +0 -41
package/src/tools.ts CHANGED
@@ -1,12 +1,23 @@
1
1
  // Copyright (c) 2026 Massu. All rights reserved.
2
2
  // Licensed under BSL 1.1 - see LICENSE file for details.
3
3
 
4
+ import { readFileSync, existsSync } from 'fs';
5
+ import { resolve } from 'path';
4
6
  import type Database from 'better-sqlite3';
5
- import { getMemoryToolDefinitions, handleMemoryToolCall, isMemoryTool } from './memory-tools.ts';
7
+ import { matchRules } from './rules.ts';
8
+ import { buildImportIndex } from './import-resolver.ts';
9
+ import { buildTrpcIndex } from './trpc-index.ts';
10
+ import { buildPageDeps, findAffectedPages } from './page-deps.ts';
11
+ import { buildMiddlewareTree, isInMiddlewareTree, getMiddlewareTree } from './middleware-tree.ts';
12
+ import { classifyFile, classifyRouter, findCrossDomainImports, getFilesInDomain } from './domains.ts';
13
+ import { parsePrismaSchema, detectMismatches, findColumnUsageInRouters } from './schema-mapper.ts';
14
+ import { isDataStale, updateBuildTimestamp } from './db.ts';
15
+ import { getMemoryToolDefinitions, handleMemoryToolCall } from './memory-tools.ts';
6
16
  import { getMemoryDb } from './memory-db.ts';
7
- import { getDocsToolDefinitions, handleDocsToolCall, isDocsTool } from './docs-tools.ts';
17
+ import { getDocsToolDefinitions, handleDocsToolCall } from './docs-tools.ts';
8
18
  import { getObservabilityToolDefinitions, handleObservabilityToolCall, isObservabilityTool } from './observability-tools.ts';
9
- import { getSentinelToolDefinitions, handleSentinelToolCall, isSentinelTool } from './sentinel-tools.ts';
19
+ import { getSentinelToolDefinitions, handleSentinelToolCall } from './sentinel-tools.ts';
20
+ import { runFeatureScan } from './sentinel-scanner.ts';
10
21
  import { getAnalyticsToolDefinitions, isAnalyticsTool, handleAnalyticsToolCall } from './analytics.ts';
11
22
  import { getCostToolDefinitions, isCostTool, handleCostToolCall } from './cost-tracker.ts';
12
23
  import { getPromptToolDefinitions, isPromptTool, handlePromptToolCall } from './prompt-analyzer.ts';
@@ -17,20 +28,71 @@ import { getSecurityToolDefinitions, isSecurityTool, handleSecurityToolCall } fr
17
28
  import { getDependencyToolDefinitions, isDependencyTool, handleDependencyToolCall } from './dependency-scorer.ts';
18
29
  import { getTeamToolDefinitions, isTeamTool, handleTeamToolCall } from './team-knowledge.ts';
19
30
  import { getRegressionToolDefinitions, isRegressionTool, handleRegressionToolCall } from './regression-detector.ts';
20
- import { getCoreToolDefinitions, isCoreTool, handleCoreToolCall, ensureIndexes } from './core-tools.ts';
21
- import { getConfig } from './config.ts';
22
- import { text } from './tool-helpers.ts';
23
- import type { ToolDefinition, ToolResult } from './tool-helpers.ts';
31
+ import { getConfig, getProjectRoot } from './config.ts';
24
32
 
25
- export type { ToolDefinition, ToolResult } from './tool-helpers.ts';
33
+ export interface ToolDefinition {
34
+ name: string;
35
+ description: string;
36
+ inputSchema: Record<string, unknown>;
37
+ }
38
+
39
+ export interface ToolResult {
40
+ content: { type: 'text'; text: string }[];
41
+ }
42
+
43
+ /** Get the configured tool prefix (e.g., 'massu' or 'myapp') */
44
+ function prefix(): string {
45
+ return getConfig().toolPrefix;
46
+ }
47
+
48
+ /** Prefix a tool name with the configured prefix */
49
+ function p(name: string): string {
50
+ return `${prefix()}_${name}`;
51
+ }
26
52
 
27
53
  /**
28
- * Run a function with a memoryDb instance, ensuring it is closed after use.
54
+ * Strip the configured prefix from a tool name to get the base name.
55
+ * e.g., "massu_sync" -> "sync", "massu_memory_search" -> "memory_search"
29
56
  */
30
- function withMemoryDb<T>(fn: (db: Database.Database) => T): T {
31
- const memDb = getMemoryDb();
32
- try { return fn(memDb); }
33
- finally { memDb.close(); }
57
+ function stripPrefix(name: string): string {
58
+ const pfx = prefix() + '_';
59
+ if (name.startsWith(pfx)) {
60
+ return name.slice(pfx.length);
61
+ }
62
+ return name;
63
+ }
64
+
65
+ /**
66
+ * Ensure indexes are built and up-to-date.
67
+ * Lazy initialization: only rebuilds if stale.
68
+ */
69
+ function ensureIndexes(dataDb: Database.Database, codegraphDb: Database.Database, force: boolean = false): string {
70
+ if (!force && !isDataStale(dataDb, codegraphDb)) {
71
+ return 'Indexes are up-to-date.';
72
+ }
73
+
74
+ const results: string[] = [];
75
+
76
+ const importCount = buildImportIndex(dataDb, codegraphDb);
77
+ results.push(`Import edges: ${importCount}`);
78
+
79
+ const config = getConfig();
80
+
81
+ if (config.framework.router === 'trpc') {
82
+ const trpcStats = buildTrpcIndex(dataDb);
83
+ results.push(`tRPC procedures: ${trpcStats.totalProcedures} (${trpcStats.withCallers} with UI, ${trpcStats.withoutCallers} without)`);
84
+ }
85
+
86
+ const pageCount = buildPageDeps(dataDb, codegraphDb);
87
+ results.push(`Page deps: ${pageCount} pages`);
88
+
89
+ if (config.paths.middleware) {
90
+ const middlewareCount = buildMiddlewareTree(dataDb);
91
+ results.push(`Middleware tree: ${middlewareCount} files`);
92
+ }
93
+
94
+ updateBuildTimestamp(dataDb);
95
+ return `Indexes rebuilt:\n${results.join('\n')}`;
34
96
  }
35
97
 
36
98
  /**
@@ -62,8 +124,94 @@ export function getToolDefinitions(): ToolDefinition[] {
62
124
  // Enterprise layer (team knowledge — cloud-only; regression detection — always)
63
125
  ...(config.cloud?.enabled ? getTeamToolDefinitions() : []),
64
126
  ...getRegressionToolDefinitions(),
65
- // Core tools (sync, context, impact, domains, schema, trpc_map, coupling_check)
66
- ...getCoreToolDefinitions(),
127
+ // Core tools
128
+ {
129
+ name: p('sync'),
130
+ description: 'Force rebuild all indexes (import edges, tRPC mappings, page deps, middleware tree). Run this after significant code changes.',
131
+ inputSchema: {
132
+ type: 'object',
133
+ properties: {},
134
+ required: [],
135
+ },
136
+ },
137
+ {
138
+ name: p('context'),
139
+ description: 'Get context for a file: applicable rules, pattern warnings, schema mismatch alerts, and whether the file is in the middleware import tree.',
140
+ inputSchema: {
141
+ type: 'object',
142
+ properties: {
143
+ file: { type: 'string', description: 'File path relative to project root' },
144
+ },
145
+ required: ['file'],
146
+ },
147
+ },
148
+ ...(config.framework.router === 'trpc' ? [
149
+ {
150
+ name: p('trpc_map'),
151
+ description: 'Map tRPC procedures to their UI call sites. Find which components call a router, which procedures have no UI callers, or list all procedures for a router.',
152
+ inputSchema: {
153
+ type: 'object',
154
+ properties: {
155
+ router: { type: 'string', description: 'Router name (e.g., "orders")' },
156
+ procedure: { type: 'string', description: 'Procedure name to search across all routers' },
157
+ uncoupled: { type: 'boolean', description: 'If true, show only procedures with ZERO UI callers' },
158
+ },
159
+ required: [],
160
+ },
161
+ },
162
+ {
163
+ name: p('coupling_check'),
164
+ description: 'Automated coupling check. Finds all procedures with zero UI callers and components not rendered in any page.',
165
+ inputSchema: {
166
+ type: 'object',
167
+ properties: {
168
+ staged_files: {
169
+ type: 'array',
170
+ items: { type: 'string' },
171
+ description: 'Optional: only check these specific files',
172
+ },
173
+ },
174
+ required: [],
175
+ },
176
+ },
177
+ ] : []),
178
+ {
179
+ name: p('impact'),
180
+ description: 'Full impact analysis for a file: which pages are affected, which database tables are in the chain, middleware tree membership, domain crossings.',
181
+ inputSchema: {
182
+ type: 'object',
183
+ properties: {
184
+ file: { type: 'string', description: 'File path relative to project root' },
185
+ },
186
+ required: ['file'],
187
+ },
188
+ },
189
+ ...(config.domains.length > 0 ? [{
190
+ name: p('domains'),
191
+ description: 'Domain boundary information. Classify a file into its domain, show cross-domain imports, or list all files in a domain.',
192
+ inputSchema: {
193
+ type: 'object',
194
+ properties: {
195
+ file: { type: 'string', description: 'File to classify into a domain' },
196
+ crossings: { type: 'boolean', description: 'Show all cross-domain imports (violations highlighted)' },
197
+ domain: { type: 'string', description: 'Domain name to list all files for' },
198
+ },
199
+ required: [],
200
+ },
201
+ }] : []),
202
+ ...(config.framework.orm === 'prisma' ? [{
203
+ name: p('schema'),
204
+ description: 'Prisma schema cross-reference. Show columns for a table, detect mismatches between code and schema, or verify column references in a file.',
205
+ inputSchema: {
206
+ type: 'object',
207
+ properties: {
208
+ table: { type: 'string', description: 'Table/model name to inspect' },
209
+ mismatches: { type: 'boolean', description: 'Show all detected column name mismatches' },
210
+ verify: { type: 'string', description: 'File path to verify column references against schema' },
211
+ },
212
+ required: [],
213
+ },
214
+ }] : []),
67
215
  ];
68
216
  }
69
217
 
@@ -77,35 +225,623 @@ export function handleToolCall(
77
225
  codegraphDb: Database.Database
78
226
  ): ToolResult {
79
227
  // Ensure indexes are built before any tool call
80
- ensureIndexes(dataDb, codegraphDb);
228
+ const syncMessage = ensureIndexes(dataDb, codegraphDb);
229
+ const pfx = prefix();
81
230
 
82
231
  try {
83
- // Route to module tools via isTool() matchers + withMemoryDb helper
84
- if (isMemoryTool(name)) return withMemoryDb(db => handleMemoryToolCall(name, args, db));
85
- if (isObservabilityTool(name)) return withMemoryDb(db => handleObservabilityToolCall(name, args, db));
86
- if (isDocsTool(name)) return handleDocsToolCall(name, args);
87
- if (isSentinelTool(name)) return handleSentinelToolCall(name, args, dataDb);
88
- if (isAnalyticsTool(name)) return withMemoryDb(db => handleAnalyticsToolCall(name, args, db));
89
- if (isCostTool(name)) return withMemoryDb(db => handleCostToolCall(name, args, db));
90
- if (isPromptTool(name)) return withMemoryDb(db => handlePromptToolCall(name, args, db));
91
- if (isAuditTool(name)) return withMemoryDb(db => handleAuditToolCall(name, args, db));
92
- if (isValidationTool(name)) return withMemoryDb(db => handleValidationToolCall(name, args, db));
93
- if (isAdrTool(name)) return withMemoryDb(db => handleAdrToolCall(name, args, db));
94
- if (isSecurityTool(name)) return withMemoryDb(db => handleSecurityToolCall(name, args, db));
95
- if (isDependencyTool(name)) return withMemoryDb(db => handleDependencyToolCall(name, args, db));
232
+ // Route memory tools to memory handler
233
+ if (name.startsWith(pfx + '_memory_')) {
234
+ const memDb = getMemoryDb();
235
+ try {
236
+ return handleMemoryToolCall(name, args, memDb);
237
+ } finally {
238
+ memDb.close();
239
+ }
240
+ }
241
+
242
+ // Route observability tools to observability handler
243
+ if (isObservabilityTool(name)) {
244
+ const memDb = getMemoryDb();
245
+ try {
246
+ return handleObservabilityToolCall(name, args, memDb);
247
+ } finally {
248
+ memDb.close();
249
+ }
250
+ }
251
+
252
+ // Route docs tools to docs handler
253
+ if (name.startsWith(pfx + '_docs_')) {
254
+ return handleDocsToolCall(name, args);
255
+ }
256
+
257
+ // Route sentinel tools to sentinel handler
258
+ if (name.startsWith(pfx + '_sentinel_')) {
259
+ return handleSentinelToolCall(name, args, dataDb);
260
+ }
261
+
262
+ // Route analytics layer tools
263
+ if (isAnalyticsTool(name)) {
264
+ const memDb = getMemoryDb();
265
+ try { return handleAnalyticsToolCall(name, args, memDb); }
266
+ finally { memDb.close(); }
267
+ }
268
+ if (isCostTool(name)) {
269
+ const memDb = getMemoryDb();
270
+ try { return handleCostToolCall(name, args, memDb); }
271
+ finally { memDb.close(); }
272
+ }
273
+ if (isPromptTool(name)) {
274
+ const memDb = getMemoryDb();
275
+ try { return handlePromptToolCall(name, args, memDb); }
276
+ finally { memDb.close(); }
277
+ }
278
+
279
+ // Route governance layer tools
280
+ if (isAuditTool(name)) {
281
+ const memDb = getMemoryDb();
282
+ try { return handleAuditToolCall(name, args, memDb); }
283
+ finally { memDb.close(); }
284
+ }
285
+ if (isValidationTool(name)) {
286
+ const memDb = getMemoryDb();
287
+ try { return handleValidationToolCall(name, args, memDb); }
288
+ finally { memDb.close(); }
289
+ }
290
+ if (isAdrTool(name)) {
291
+ const memDb = getMemoryDb();
292
+ try { return handleAdrToolCall(name, args, memDb); }
293
+ finally { memDb.close(); }
294
+ }
295
+
296
+ // Route security layer tools
297
+ if (isSecurityTool(name)) {
298
+ const memDb = getMemoryDb();
299
+ try { return handleSecurityToolCall(name, args, memDb); }
300
+ finally { memDb.close(); }
301
+ }
302
+ if (isDependencyTool(name)) {
303
+ const memDb = getMemoryDb();
304
+ try { return handleDependencyToolCall(name, args, memDb); }
305
+ finally { memDb.close(); }
306
+ }
307
+
308
+ // Route enterprise layer tools (team tools require cloud sync)
96
309
  if (isTeamTool(name)) {
97
310
  if (!getConfig().cloud?.enabled) {
98
311
  return text('This tool requires Cloud Team or Enterprise. Configure cloud sync to enable.');
99
312
  }
100
- return withMemoryDb(db => handleTeamToolCall(name, args, db));
313
+ const memDb = getMemoryDb();
314
+ try { return handleTeamToolCall(name, args, memDb); }
315
+ finally { memDb.close(); }
316
+ }
317
+ if (isRegressionTool(name)) {
318
+ const memDb = getMemoryDb();
319
+ try { return handleRegressionToolCall(name, args, memDb); }
320
+ finally { memDb.close(); }
101
321
  }
102
- if (isRegressionTool(name)) return withMemoryDb(db => handleRegressionToolCall(name, args, db));
103
-
104
- // Core tools (sync, context, trpc_map, coupling_check, impact, domains, schema)
105
- if (isCoreTool(name)) return handleCoreToolCall(name, args, dataDb, codegraphDb);
106
322
 
107
- return text(`Unknown tool: ${name}`);
323
+ // Match core tools by base name
324
+ const baseName = stripPrefix(name);
325
+ switch (baseName) {
326
+ case 'sync':
327
+ return handleSync(dataDb, codegraphDb);
328
+ case 'context':
329
+ return handleContext(args.file as string, dataDb, codegraphDb);
330
+ case 'trpc_map':
331
+ return handleTrpcMap(args, dataDb);
332
+ case 'coupling_check':
333
+ return handleCouplingCheck(args, dataDb, codegraphDb);
334
+ case 'impact':
335
+ return handleImpact(args.file as string, dataDb, codegraphDb);
336
+ case 'domains':
337
+ return handleDomains(args, dataDb, codegraphDb);
338
+ case 'schema':
339
+ return handleSchema(args);
340
+ default:
341
+ return text(`Unknown tool: ${name}`);
342
+ }
108
343
  } catch (error) {
109
344
  return text(`Error in ${name}: ${error instanceof Error ? error.message : String(error)}`);
110
345
  }
111
346
  }
347
+
348
+ function text(content: string): ToolResult {
349
+ return { content: [{ type: 'text', text: content }] };
350
+ }
351
+
352
+ // === Tool Handlers ===
353
+
354
+ function handleSync(dataDb: Database.Database, codegraphDb: Database.Database): ToolResult {
355
+ const result = ensureIndexes(dataDb, codegraphDb, true);
356
+
357
+ // Run feature auto-discovery after index rebuild
358
+ try {
359
+ const scanResult = runFeatureScan(dataDb);
360
+ return text(`${result}\n\nFeature scan: ${scanResult.registered} features registered (${scanResult.fromProcedures} from procedures, ${scanResult.fromPages} from pages, ${scanResult.fromComponents} from components)`);
361
+ } catch (error) {
362
+ return text(`${result}\n\nFeature scan failed: ${error instanceof Error ? error.message : String(error)}`);
363
+ }
364
+ }
365
+
366
+ function handleContext(file: string, dataDb: Database.Database, codegraphDb: Database.Database): ToolResult {
367
+ const lines: string[] = [];
368
+
369
+ // 1. CodeGraph context
370
+ const nodes = codegraphDb.prepare(
371
+ "SELECT name, kind, start_line, end_line FROM nodes WHERE file_path = ? ORDER BY start_line"
372
+ ).all(file) as { name: string; kind: string; start_line: number; end_line: number }[];
373
+
374
+ if (nodes.length > 0) {
375
+ lines.push('## CodeGraph Nodes');
376
+ for (const node of nodes.slice(0, 30)) {
377
+ lines.push(`- ${node.kind}: ${node.name} (L${node.start_line}-${node.end_line})`);
378
+ }
379
+ if (nodes.length > 30) {
380
+ lines.push(`... and ${nodes.length - 30} more`);
381
+ }
382
+ lines.push('');
383
+ }
384
+
385
+ // 2. Applicable rules
386
+ const rules = matchRules(file);
387
+ if (rules.length > 0) {
388
+ lines.push('## Applicable Rules');
389
+ for (const rule of rules) {
390
+ const severity = rule.severity ? `[${rule.severity}]` : '';
391
+ for (const r of rule.rules) {
392
+ lines.push(`- ${severity} ${r}`);
393
+ }
394
+ if (rule.patternFile) {
395
+ lines.push(` See: .claude/${rule.patternFile}`);
396
+ }
397
+ }
398
+ lines.push('');
399
+ }
400
+
401
+ // 3. Middleware tree check
402
+ if (isInMiddlewareTree(dataDb, file)) {
403
+ lines.push('## WARNING: Middleware Import Tree');
404
+ lines.push('This file is imported (directly or transitively) by the middleware entry point.');
405
+ lines.push('NO Node.js dependencies allowed (pino, winston, fs, crypto, path, child_process).');
406
+ lines.push('');
407
+ }
408
+
409
+ // 4. Domain classification
410
+ const domain = classifyFile(file);
411
+ lines.push(`## Domain: ${domain}`);
412
+ lines.push('');
413
+
414
+ // 5. Import edges
415
+ const imports = dataDb.prepare(
416
+ 'SELECT target_file, imported_names FROM massu_imports WHERE source_file = ? LIMIT 20'
417
+ ).all(file) as { target_file: string; imported_names: string }[];
418
+
419
+ if (imports.length > 0) {
420
+ lines.push('## Imports (from this file)');
421
+ for (const imp of imports) {
422
+ const names = JSON.parse(imp.imported_names);
423
+ lines.push(`- ${imp.target_file}${names.length > 0 ? ': ' + names.join(', ') : ''}`);
424
+ }
425
+ lines.push('');
426
+ }
427
+
428
+ // 6. Imported BY
429
+ const importedBy = dataDb.prepare(
430
+ 'SELECT source_file FROM massu_imports WHERE target_file = ? LIMIT 20'
431
+ ).all(file) as { source_file: string }[];
432
+
433
+ if (importedBy.length > 0) {
434
+ lines.push('## Imported By');
435
+ for (const imp of importedBy) {
436
+ lines.push(`- ${imp.source_file}`);
437
+ }
438
+ lines.push('');
439
+ }
440
+
441
+ return text(lines.join('\n') || 'No context available for this file.');
442
+ }
443
+
444
+ function handleTrpcMap(args: Record<string, unknown>, dataDb: Database.Database): ToolResult {
445
+ const lines: string[] = [];
446
+
447
+ if (args.uncoupled) {
448
+ const uncoupled = dataDb.prepare(
449
+ 'SELECT router_name, procedure_name, procedure_type, router_file FROM massu_trpc_procedures WHERE has_ui_caller = 0 ORDER BY router_name, procedure_name'
450
+ ).all() as { router_name: string; procedure_name: string; procedure_type: string; router_file: string }[];
451
+
452
+ lines.push(`## Uncoupled Procedures (${uncoupled.length} total)`);
453
+ lines.push('These procedures have ZERO UI callers.');
454
+ lines.push('');
455
+
456
+ let currentRouter = '';
457
+ for (const proc of uncoupled) {
458
+ if (proc.router_name !== currentRouter) {
459
+ currentRouter = proc.router_name;
460
+ lines.push(`### ${currentRouter} (${proc.router_file})`);
461
+ }
462
+ lines.push(`- ${proc.procedure_name} (${proc.procedure_type})`);
463
+ }
464
+ } else if (args.router) {
465
+ const procs = dataDb.prepare(
466
+ 'SELECT id, procedure_name, procedure_type, has_ui_caller FROM massu_trpc_procedures WHERE router_name = ? ORDER BY procedure_name'
467
+ ).all(args.router as string) as { id: number; procedure_name: string; procedure_type: string; has_ui_caller: number }[];
468
+
469
+ lines.push(`## Router: ${args.router} (${procs.length} procedures)`);
470
+ lines.push('');
471
+
472
+ for (const proc of procs) {
473
+ const status = proc.has_ui_caller ? '' : ' [NO UI CALLERS]';
474
+ lines.push(`### ${args.router}.${proc.procedure_name} (${proc.procedure_type})${status}`);
475
+
476
+ const callSites = dataDb.prepare(
477
+ 'SELECT file, line, call_pattern FROM massu_trpc_call_sites WHERE procedure_id = ?'
478
+ ).all(proc.id) as { file: string; line: number; call_pattern: string }[];
479
+
480
+ if (callSites.length > 0) {
481
+ lines.push('UI Call Sites:');
482
+ for (const site of callSites) {
483
+ lines.push(` - ${site.file}:${site.line} -> ${site.call_pattern}`);
484
+ }
485
+ } else {
486
+ lines.push('UI Call Sites: NONE');
487
+ }
488
+ lines.push('');
489
+ }
490
+ } else if (args.procedure) {
491
+ const procs = dataDb.prepare(
492
+ 'SELECT id, router_name, router_file, procedure_type, has_ui_caller FROM massu_trpc_procedures WHERE procedure_name = ? ORDER BY router_name'
493
+ ).all(args.procedure as string) as { id: number; router_name: string; router_file: string; procedure_type: string; has_ui_caller: number }[];
494
+
495
+ lines.push(`## Procedure "${args.procedure}" found in ${procs.length} routers`);
496
+ lines.push('');
497
+
498
+ for (const proc of procs) {
499
+ lines.push(`### ${proc.router_name}.${args.procedure} (${proc.procedure_type})`);
500
+ lines.push(`File: ${proc.router_file}`);
501
+
502
+ const callSites = dataDb.prepare(
503
+ 'SELECT file, line, call_pattern FROM massu_trpc_call_sites WHERE procedure_id = ?'
504
+ ).all(proc.id) as { file: string; line: number; call_pattern: string }[];
505
+
506
+ if (callSites.length > 0) {
507
+ lines.push('UI Call Sites:');
508
+ for (const site of callSites) {
509
+ lines.push(` - ${site.file}:${site.line} -> ${site.call_pattern}`);
510
+ }
511
+ } else {
512
+ lines.push('UI Call Sites: NONE');
513
+ }
514
+ lines.push('');
515
+ }
516
+ } else {
517
+ const total = dataDb.prepare('SELECT COUNT(*) as count FROM massu_trpc_procedures').get() as { count: number };
518
+ const coupled = dataDb.prepare('SELECT COUNT(*) as count FROM massu_trpc_procedures WHERE has_ui_caller = 1').get() as { count: number };
519
+ const uncoupled = total.count - coupled.count;
520
+
521
+ lines.push('## tRPC Procedure Summary');
522
+ lines.push(`- Total procedures: ${total.count}`);
523
+ lines.push(`- With UI callers: ${coupled.count}`);
524
+ lines.push(`- Without UI callers: ${uncoupled}`);
525
+ lines.push('');
526
+ lines.push('Use { router: "name" } to see details for a specific router.');
527
+ lines.push('Use { uncoupled: true } to see all procedures without UI callers.');
528
+ }
529
+
530
+ return text(lines.join('\n'));
531
+ }
532
+
533
+ function handleCouplingCheck(args: Record<string, unknown>, dataDb: Database.Database, codegraphDb: Database.Database): ToolResult {
534
+ const lines: string[] = [];
535
+ const stagedFiles = args.staged_files as string[] | undefined;
536
+
537
+ let uncoupledProcs;
538
+ if (stagedFiles) {
539
+ uncoupledProcs = dataDb.prepare(
540
+ `SELECT router_name, procedure_name, procedure_type, router_file FROM massu_trpc_procedures WHERE has_ui_caller = 0 AND router_file IN (${stagedFiles.map(() => '?').join(',')})`
541
+ ).all(...stagedFiles) as { router_name: string; procedure_name: string; procedure_type: string; router_file: string }[];
542
+ } else {
543
+ uncoupledProcs = dataDb.prepare(
544
+ 'SELECT router_name, procedure_name, procedure_type, router_file FROM massu_trpc_procedures WHERE has_ui_caller = 0'
545
+ ).all() as { router_name: string; procedure_name: string; procedure_type: string; router_file: string }[];
546
+ }
547
+
548
+ lines.push('## Coupling Check Results');
549
+ lines.push('');
550
+
551
+ if (uncoupledProcs.length > 0) {
552
+ lines.push(`### Uncoupled Procedures: ${uncoupledProcs.length}`);
553
+ for (const proc of uncoupledProcs) {
554
+ lines.push(`- ${proc.router_name}.${proc.procedure_name} (${proc.procedure_type}) in ${proc.router_file}`);
555
+ }
556
+ lines.push('');
557
+ } else {
558
+ lines.push('### Uncoupled Procedures: 0 (PASS)');
559
+ lines.push('');
560
+ }
561
+
562
+ const allPages = codegraphDb.prepare(
563
+ "SELECT path FROM files WHERE path LIKE 'src/app/%/page.tsx' OR path = 'src/app/page.tsx'"
564
+ ).all() as { path: string }[];
565
+
566
+ const pageImports = new Set<string>();
567
+ for (const page of allPages) {
568
+ const imports = dataDb.prepare(
569
+ 'SELECT target_file FROM massu_imports WHERE source_file = ?'
570
+ ).all(page.path) as { target_file: string }[];
571
+ for (const imp of imports) {
572
+ pageImports.add(imp.target_file);
573
+ }
574
+ }
575
+
576
+ let componentFiles: { path: string }[];
577
+ if (stagedFiles) {
578
+ const placeholders = stagedFiles.map(() => '?').join(',');
579
+ componentFiles = codegraphDb.prepare(
580
+ `SELECT path FROM files WHERE path LIKE 'src/components/%' AND path IN (${placeholders})`
581
+ ).all(...stagedFiles) as { path: string }[];
582
+ } else {
583
+ componentFiles = [];
584
+ }
585
+
586
+ const orphanComponents = componentFiles.filter(f => !pageImports.has(f.path));
587
+ if (orphanComponents.length > 0) {
588
+ lines.push(`### Orphan Components: ${orphanComponents.length}`);
589
+ for (const comp of orphanComponents) {
590
+ lines.push(`- ${comp.path} (not imported by any page.tsx)`);
591
+ }
592
+ lines.push('');
593
+ }
594
+
595
+ const totalIssues = uncoupledProcs.length + orphanComponents.length;
596
+ lines.push(`### RESULT: ${totalIssues === 0 ? 'PASS' : `FAIL (${totalIssues} issues)`}`);
597
+
598
+ return text(lines.join('\n'));
599
+ }
600
+
601
+ function handleImpact(file: string, dataDb: Database.Database, codegraphDb: Database.Database): ToolResult {
602
+ const lines: string[] = [];
603
+
604
+ lines.push(`## Impact Analysis: ${file}`);
605
+ lines.push('');
606
+
607
+ const affectedPages = findAffectedPages(dataDb, file);
608
+
609
+ if (affectedPages.length > 0) {
610
+ const portals = [...new Set(affectedPages.map(p => p.portal))];
611
+ const allTables = [...new Set(affectedPages.flatMap(p => p.tables))];
612
+ const allRouters = [...new Set(affectedPages.flatMap(p => p.routers))];
613
+
614
+ lines.push(`### Pages Affected: ${affectedPages.length}`);
615
+ for (const page of affectedPages) {
616
+ lines.push(`- ${page.route} (${page.portal})`);
617
+ }
618
+ lines.push('');
619
+
620
+ lines.push(`### Scopes Affected: ${portals.join(', ')}`);
621
+ lines.push('');
622
+
623
+ if (allRouters.length > 0) {
624
+ lines.push(`### Routers Called (via hooks/components):`);
625
+ for (const router of allRouters) {
626
+ lines.push(`- ${router}`);
627
+ }
628
+ lines.push('');
629
+ }
630
+
631
+ if (allTables.length > 0) {
632
+ lines.push(`### Database Tables:`);
633
+ for (const table of allTables) {
634
+ lines.push(`- ${table}`);
635
+ }
636
+ lines.push('');
637
+ }
638
+ } else {
639
+ lines.push('No pages affected (file may not be in any page dependency chain).');
640
+ lines.push('');
641
+ }
642
+
643
+ const inMiddleware = isInMiddlewareTree(dataDb, file);
644
+ if (inMiddleware) {
645
+ lines.push('### WARNING: In Middleware Import Tree');
646
+ lines.push('Changes to this file affect Edge Runtime. No Node.js deps allowed.');
647
+ } else {
648
+ lines.push('### Middleware: NOT in middleware import tree (safe)');
649
+ }
650
+ lines.push('');
651
+
652
+ const fileDomain = classifyFile(file);
653
+ lines.push(`### Domain: ${fileDomain}`);
654
+
655
+ const imports = dataDb.prepare(
656
+ 'SELECT target_file FROM massu_imports WHERE source_file = ?'
657
+ ).all(file) as { target_file: string }[];
658
+
659
+ const crossings: string[] = [];
660
+ for (const imp of imports) {
661
+ const targetDomain = classifyFile(imp.target_file);
662
+ if (targetDomain !== fileDomain && targetDomain !== 'Unknown') {
663
+ crossings.push(`${imp.target_file} (${targetDomain})`);
664
+ }
665
+ }
666
+
667
+ if (crossings.length > 0) {
668
+ lines.push(`### Domain Crossings: ${crossings.length}`);
669
+ for (const crossing of crossings) {
670
+ lines.push(`- -> ${crossing}`);
671
+ }
672
+ }
673
+
674
+ return text(lines.join('\n'));
675
+ }
676
+
677
+ function handleDomains(args: Record<string, unknown>, dataDb: Database.Database, codegraphDb: Database.Database): ToolResult {
678
+ const lines: string[] = [];
679
+ const domains = getConfig().domains;
680
+
681
+ if (args.file) {
682
+ const file = args.file as string;
683
+ const domain = classifyFile(file);
684
+ lines.push(`## ${file}`);
685
+ lines.push(`Domain: ${domain}`);
686
+
687
+ const domainConfig = domains.find(d => d.name === domain);
688
+ if (domainConfig) {
689
+ lines.push(`Allowed imports from: ${domainConfig.allowedImportsFrom.join(', ') || 'any domain (system)'}`);
690
+ }
691
+ } else if (args.crossings) {
692
+ const crossings = findCrossDomainImports(dataDb);
693
+ const violations = crossings.filter(c => !c.allowed);
694
+ const allowed = crossings.filter(c => c.allowed);
695
+
696
+ lines.push(`## Cross-Domain Import Analysis`);
697
+ lines.push(`Total crossings: ${crossings.length}`);
698
+ lines.push(`Violations: ${violations.length}`);
699
+ lines.push(`Allowed: ${allowed.length}`);
700
+ lines.push('');
701
+
702
+ if (violations.length > 0) {
703
+ lines.push('### Violations (Disallowed Cross-Domain Imports)');
704
+ for (const v of violations.slice(0, 50)) {
705
+ lines.push(`- ${v.source} (${v.sourceDomain}) -> ${v.target} (${v.targetDomain})`);
706
+ }
707
+ if (violations.length > 50) {
708
+ lines.push(`... and ${violations.length - 50} more`);
709
+ }
710
+ }
711
+ } else if (args.domain) {
712
+ const domainName = args.domain as string;
713
+ const files = getFilesInDomain(dataDb, codegraphDb, domainName);
714
+ const config = domains.find(d => d.name === domainName);
715
+
716
+ lines.push(`## Domain: ${domainName}`);
717
+ if (config) {
718
+ lines.push(`Allowed imports from: ${config.allowedImportsFrom.join(', ') || 'any domain (system)'}`);
719
+ }
720
+ lines.push('');
721
+
722
+ lines.push(`### Routers (${files.routers.length})`);
723
+ for (const r of files.routers) lines.push(`- ${r}`);
724
+ lines.push('');
725
+
726
+ lines.push(`### Pages (${files.pages.length})`);
727
+ for (const p of files.pages) lines.push(`- ${p}`);
728
+ lines.push('');
729
+
730
+ lines.push(`### Components (${files.components.length})`);
731
+ for (const c of files.components.slice(0, 30)) lines.push(`- ${c}`);
732
+ if (files.components.length > 30) lines.push(`... and ${files.components.length - 30} more`);
733
+ } else {
734
+ lines.push('## Domain Summary');
735
+ for (const domain of domains) {
736
+ lines.push(`- **${domain.name}**: ${domain.routers.length} router patterns, imports from: ${domain.allowedImportsFrom.join(', ') || 'any'}`);
737
+ }
738
+ }
739
+
740
+ return text(lines.join('\n'));
741
+ }
742
+
743
+ function handleSchema(args: Record<string, unknown>): ToolResult {
744
+ const lines: string[] = [];
745
+ const models = parsePrismaSchema();
746
+
747
+ if (args.mismatches) {
748
+ const mismatches = detectMismatches(models);
749
+
750
+ lines.push(`## Schema Mismatches Detected: ${mismatches.length}`);
751
+ lines.push('');
752
+
753
+ for (const m of mismatches) {
754
+ lines.push(`### ${m.table}.${m.codeColumn} [${m.severity}]`);
755
+ lines.push(`Code uses "${m.codeColumn}" but this column does NOT exist in the schema.`);
756
+ lines.push(`Files affected:`);
757
+ for (const f of m.files) {
758
+ lines.push(` - ${f}`);
759
+ }
760
+ lines.push('');
761
+ }
762
+
763
+ if (mismatches.length === 0) {
764
+ lines.push('No known mismatches detected in code.');
765
+ }
766
+ } else if (args.table) {
767
+ const tableName = args.table as string;
768
+ const model = models.find(m => m.tableName === tableName || m.name === tableName);
769
+
770
+ if (!model) {
771
+ return text(`Model/table "${tableName}" not found in Prisma schema.`);
772
+ }
773
+
774
+ lines.push(`## ${model.name} (table: ${model.tableName})`);
775
+ lines.push('');
776
+ lines.push('### Fields');
777
+ for (const field of model.fields) {
778
+ const nullable = field.nullable ? '?' : '';
779
+ const relation = field.isRelation ? ' [RELATION]' : '';
780
+ lines.push(`- ${field.name}: ${field.type}${nullable}${relation}`);
781
+ }
782
+ lines.push('');
783
+
784
+ const usage = findColumnUsageInRouters(model.tableName);
785
+ if (usage.size > 0) {
786
+ lines.push('### Column Usage in Routers');
787
+ for (const [col, usages] of usage) {
788
+ const validField = model.fields.find(f => f.name === col);
789
+ const status = validField ? '' : ' [NOT IN SCHEMA]';
790
+ lines.push(`- ${col}${status}: ${usages.length} references`);
791
+ }
792
+ }
793
+ } else if (args.verify) {
794
+ const file = args.verify as string;
795
+ lines.push(`## Schema Verification: ${file}`);
796
+ lines.push('Checking all column references against Prisma schema...');
797
+ lines.push('');
798
+
799
+ const absPath = resolve(getProjectRoot(), file);
800
+
801
+ if (!existsSync(absPath)) {
802
+ return text(`File not found: ${file}`);
803
+ }
804
+
805
+ const source = readFileSync(absPath, 'utf-8');
806
+
807
+ // Use configurable db access pattern
808
+ const config = getConfig();
809
+ const dbPattern = config.dbAccessPattern ?? 'ctx.db.{table}';
810
+ const regexStr = dbPattern
811
+ .replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
812
+ .replace('\\{table\\}', '(\\w+)');
813
+ const tableRegex = new RegExp(regexStr + '\\.', 'g');
814
+ const tableRefs = new Set<string>();
815
+ let match;
816
+ while ((match = tableRegex.exec(source)) !== null) {
817
+ tableRefs.add(match[1]);
818
+ }
819
+
820
+ for (const table of tableRefs) {
821
+ const model = models.find(m => m.tableName === table || m.name.toLowerCase() === table);
822
+ if (!model) {
823
+ lines.push(`### ${table}: MODEL NOT FOUND IN SCHEMA`);
824
+ continue;
825
+ }
826
+
827
+ lines.push(`### ${table} (model: ${model.name})`);
828
+ const fieldNames = new Set(model.fields.map(f => f.name));
829
+ lines.push(`Schema has ${fieldNames.size} fields.`);
830
+ lines.push('');
831
+ }
832
+ } else {
833
+ lines.push(`## Prisma Schema Summary`);
834
+ lines.push(`Models: ${models.length}`);
835
+ lines.push('');
836
+
837
+ const mismatches = detectMismatches(models);
838
+ if (mismatches.length > 0) {
839
+ lines.push(`### Active Mismatches: ${mismatches.length}`);
840
+ for (const m of mismatches) {
841
+ lines.push(`- ${m.table}.${m.codeColumn} [${m.severity}]`);
842
+ }
843
+ }
844
+ }
845
+
846
+ return text(lines.join('\n'));
847
+ }