@massu/core 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +71 -0
- package/README.md +2 -2
- package/dist/hooks/cost-tracker.js +149 -11527
- package/dist/hooks/post-edit-context.js +127 -11493
- package/dist/hooks/post-tool-use.js +169 -11550
- package/dist/hooks/pre-compact.js +149 -11530
- package/dist/hooks/pre-delete-check.js +144 -11523
- package/dist/hooks/quality-event.js +149 -11527
- package/dist/hooks/session-end.js +188 -11570
- package/dist/hooks/session-start.js +159 -11534
- package/dist/hooks/user-prompt.js +149 -11530
- package/package.json +14 -19
- package/src/adr-generator.ts +292 -0
- package/src/analytics.ts +373 -0
- package/src/audit-trail.ts +450 -0
- package/src/backfill-sessions.ts +180 -0
- package/src/cli.ts +105 -0
- package/src/cloud-sync.ts +190 -0
- package/src/commands/doctor.ts +300 -0
- package/src/commands/init.ts +395 -0
- package/src/commands/install-hooks.ts +26 -0
- package/src/config.ts +357 -0
- package/src/cost-tracker.ts +355 -0
- package/src/db.ts +233 -0
- package/src/dependency-scorer.ts +337 -0
- package/src/docs-map.json +100 -0
- package/src/docs-tools.ts +517 -0
- package/src/domains.ts +181 -0
- package/src/hooks/cost-tracker.ts +66 -0
- package/src/hooks/intent-suggester.ts +131 -0
- package/src/hooks/post-edit-context.ts +91 -0
- package/src/hooks/post-tool-use.ts +175 -0
- package/src/hooks/pre-compact.ts +146 -0
- package/src/hooks/pre-delete-check.ts +153 -0
- package/src/hooks/quality-event.ts +127 -0
- package/src/hooks/security-gate.ts +121 -0
- package/src/hooks/session-end.ts +467 -0
- package/src/hooks/session-start.ts +210 -0
- package/src/hooks/user-prompt.ts +91 -0
- package/src/import-resolver.ts +224 -0
- package/src/memory-db.ts +1376 -0
- package/src/memory-tools.ts +391 -0
- package/src/middleware-tree.ts +70 -0
- package/src/observability-tools.ts +343 -0
- package/src/observation-extractor.ts +411 -0
- package/src/page-deps.ts +283 -0
- package/src/prompt-analyzer.ts +332 -0
- package/src/regression-detector.ts +319 -0
- package/src/rules.ts +57 -0
- package/src/schema-mapper.ts +232 -0
- package/src/security-scorer.ts +405 -0
- package/src/security-utils.ts +133 -0
- package/src/sentinel-db.ts +578 -0
- package/src/sentinel-scanner.ts +405 -0
- package/src/sentinel-tools.ts +512 -0
- package/src/sentinel-types.ts +140 -0
- package/src/server.ts +189 -0
- package/src/session-archiver.ts +112 -0
- package/src/session-state-generator.ts +174 -0
- package/src/team-knowledge.ts +407 -0
- package/src/tools.ts +847 -0
- package/src/transcript-parser.ts +458 -0
- package/src/trpc-index.ts +214 -0
- package/src/validate-features-runner.ts +106 -0
- package/src/validation-engine.ts +358 -0
- package/dist/cli.js +0 -7890
- package/dist/server.js +0 -7008
package/src/tools.ts
ADDED
|
@@ -0,0 +1,847 @@
|
|
|
1
|
+
// Copyright (c) 2026 Massu. All rights reserved.
|
|
2
|
+
// Licensed under BSL 1.1 - see LICENSE file for details.
|
|
3
|
+
|
|
4
|
+
import { readFileSync, existsSync } from 'fs';
|
|
5
|
+
import { resolve } from 'path';
|
|
6
|
+
import type Database from 'better-sqlite3';
|
|
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';
|
|
16
|
+
import { getMemoryDb } from './memory-db.ts';
|
|
17
|
+
import { getDocsToolDefinitions, handleDocsToolCall } from './docs-tools.ts';
|
|
18
|
+
import { getObservabilityToolDefinitions, handleObservabilityToolCall, isObservabilityTool } from './observability-tools.ts';
|
|
19
|
+
import { getSentinelToolDefinitions, handleSentinelToolCall } from './sentinel-tools.ts';
|
|
20
|
+
import { runFeatureScan } from './sentinel-scanner.ts';
|
|
21
|
+
import { getAnalyticsToolDefinitions, isAnalyticsTool, handleAnalyticsToolCall } from './analytics.ts';
|
|
22
|
+
import { getCostToolDefinitions, isCostTool, handleCostToolCall } from './cost-tracker.ts';
|
|
23
|
+
import { getPromptToolDefinitions, isPromptTool, handlePromptToolCall } from './prompt-analyzer.ts';
|
|
24
|
+
import { getAuditToolDefinitions, isAuditTool, handleAuditToolCall } from './audit-trail.ts';
|
|
25
|
+
import { getValidationToolDefinitions, isValidationTool, handleValidationToolCall } from './validation-engine.ts';
|
|
26
|
+
import { getAdrToolDefinitions, isAdrTool, handleAdrToolCall } from './adr-generator.ts';
|
|
27
|
+
import { getSecurityToolDefinitions, isSecurityTool, handleSecurityToolCall } from './security-scorer.ts';
|
|
28
|
+
import { getDependencyToolDefinitions, isDependencyTool, handleDependencyToolCall } from './dependency-scorer.ts';
|
|
29
|
+
import { getTeamToolDefinitions, isTeamTool, handleTeamToolCall } from './team-knowledge.ts';
|
|
30
|
+
import { getRegressionToolDefinitions, isRegressionTool, handleRegressionToolCall } from './regression-detector.ts';
|
|
31
|
+
import { getConfig, getProjectRoot } from './config.ts';
|
|
32
|
+
|
|
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
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
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"
|
|
56
|
+
*/
|
|
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')}`;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Get all tool definitions for the MCP server.
|
|
100
|
+
*/
|
|
101
|
+
export function getToolDefinitions(): ToolDefinition[] {
|
|
102
|
+
const config = getConfig();
|
|
103
|
+
|
|
104
|
+
return [
|
|
105
|
+
// Memory tools
|
|
106
|
+
...getMemoryToolDefinitions(),
|
|
107
|
+
// Observability tools
|
|
108
|
+
...getObservabilityToolDefinitions(),
|
|
109
|
+
// Docs tools
|
|
110
|
+
...getDocsToolDefinitions(),
|
|
111
|
+
// Sentinel tools (feature registry)
|
|
112
|
+
...getSentinelToolDefinitions(),
|
|
113
|
+
// Analytics layer (quality trends, cost tracking, prompt analysis)
|
|
114
|
+
...getAnalyticsToolDefinitions(),
|
|
115
|
+
...getCostToolDefinitions(),
|
|
116
|
+
...getPromptToolDefinitions(),
|
|
117
|
+
// Governance layer (audit trail, validation, ADR)
|
|
118
|
+
...getAuditToolDefinitions(),
|
|
119
|
+
...getValidationToolDefinitions(),
|
|
120
|
+
...getAdrToolDefinitions(),
|
|
121
|
+
// Security layer (security scoring, dependency risk)
|
|
122
|
+
...getSecurityToolDefinitions(),
|
|
123
|
+
...getDependencyToolDefinitions(),
|
|
124
|
+
// Enterprise layer (team knowledge — cloud-only; regression detection — always)
|
|
125
|
+
...(config.cloud?.enabled ? getTeamToolDefinitions() : []),
|
|
126
|
+
...getRegressionToolDefinitions(),
|
|
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
|
+
}] : []),
|
|
215
|
+
];
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Handle a tool call and return the result.
|
|
220
|
+
*/
|
|
221
|
+
export function handleToolCall(
|
|
222
|
+
name: string,
|
|
223
|
+
args: Record<string, unknown>,
|
|
224
|
+
dataDb: Database.Database,
|
|
225
|
+
codegraphDb: Database.Database
|
|
226
|
+
): ToolResult {
|
|
227
|
+
// Ensure indexes are built before any tool call
|
|
228
|
+
const syncMessage = ensureIndexes(dataDb, codegraphDb);
|
|
229
|
+
const pfx = prefix();
|
|
230
|
+
|
|
231
|
+
try {
|
|
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)
|
|
309
|
+
if (isTeamTool(name)) {
|
|
310
|
+
if (!getConfig().cloud?.enabled) {
|
|
311
|
+
return text('This tool requires Cloud Team or Enterprise. Configure cloud sync to enable.');
|
|
312
|
+
}
|
|
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(); }
|
|
321
|
+
}
|
|
322
|
+
|
|
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
|
+
}
|
|
343
|
+
} catch (error) {
|
|
344
|
+
return text(`Error in ${name}: ${error instanceof Error ? error.message : String(error)}`);
|
|
345
|
+
}
|
|
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
|
+
}
|