@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
|
@@ -0,0 +1,578 @@
|
|
|
1
|
+
// Copyright (c) 2026 Massu. All rights reserved.
|
|
2
|
+
// Licensed under BSL 1.1 - see LICENSE file for details.
|
|
3
|
+
|
|
4
|
+
import type Database from 'better-sqlite3';
|
|
5
|
+
import { existsSync } from 'fs';
|
|
6
|
+
import { resolve } from 'path';
|
|
7
|
+
import type {
|
|
8
|
+
Feature,
|
|
9
|
+
FeatureInput,
|
|
10
|
+
FeatureComponent,
|
|
11
|
+
FeatureProcedure,
|
|
12
|
+
FeaturePage,
|
|
13
|
+
FeatureDep,
|
|
14
|
+
FeatureChangeLog,
|
|
15
|
+
FeatureWithCounts,
|
|
16
|
+
FeatureDetail,
|
|
17
|
+
ImpactReport,
|
|
18
|
+
ImpactItem,
|
|
19
|
+
ValidationReport,
|
|
20
|
+
ValidationItem,
|
|
21
|
+
ParityReport,
|
|
22
|
+
ParityItem,
|
|
23
|
+
ComponentRole,
|
|
24
|
+
} from './sentinel-types.ts';
|
|
25
|
+
import { getProjectRoot } from './config.ts';
|
|
26
|
+
|
|
27
|
+
// ============================================================
|
|
28
|
+
// Sentinel: Feature Registry Data Access Layer
|
|
29
|
+
// ============================================================
|
|
30
|
+
|
|
31
|
+
const PROJECT_ROOT = getProjectRoot();
|
|
32
|
+
|
|
33
|
+
function parsePortalScope(raw: string | null): string[] {
|
|
34
|
+
if (!raw) return [];
|
|
35
|
+
try {
|
|
36
|
+
return JSON.parse(raw);
|
|
37
|
+
} catch {
|
|
38
|
+
return [];
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function toFeature(row: Record<string, unknown>): Feature {
|
|
43
|
+
return {
|
|
44
|
+
id: row.id as number,
|
|
45
|
+
feature_key: row.feature_key as string,
|
|
46
|
+
domain: row.domain as string,
|
|
47
|
+
subdomain: (row.subdomain as string) || null,
|
|
48
|
+
title: row.title as string,
|
|
49
|
+
description: (row.description as string) || null,
|
|
50
|
+
status: row.status as Feature['status'],
|
|
51
|
+
priority: row.priority as Feature['priority'],
|
|
52
|
+
portal_scope: parsePortalScope(row.portal_scope as string),
|
|
53
|
+
created_at: row.created_at as string,
|
|
54
|
+
updated_at: row.updated_at as string,
|
|
55
|
+
removed_at: (row.removed_at as string) || null,
|
|
56
|
+
removed_reason: (row.removed_reason as string) || null,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// ============================================================
|
|
61
|
+
// Core CRUD
|
|
62
|
+
// ============================================================
|
|
63
|
+
|
|
64
|
+
export function upsertFeature(db: Database.Database, input: FeatureInput): number {
|
|
65
|
+
const existing = db.prepare('SELECT id FROM massu_sentinel WHERE feature_key = ?').get(input.feature_key) as { id: number } | undefined;
|
|
66
|
+
|
|
67
|
+
if (existing) {
|
|
68
|
+
db.prepare(`
|
|
69
|
+
UPDATE massu_sentinel SET
|
|
70
|
+
domain = ?, subdomain = ?, title = ?, description = ?,
|
|
71
|
+
status = COALESCE(?, status), priority = COALESCE(?, priority),
|
|
72
|
+
portal_scope = COALESCE(?, portal_scope),
|
|
73
|
+
updated_at = datetime('now')
|
|
74
|
+
WHERE id = ?
|
|
75
|
+
`).run(
|
|
76
|
+
input.domain,
|
|
77
|
+
input.subdomain || null,
|
|
78
|
+
input.title,
|
|
79
|
+
input.description || null,
|
|
80
|
+
input.status || null,
|
|
81
|
+
input.priority || null,
|
|
82
|
+
input.portal_scope ? JSON.stringify(input.portal_scope) : null,
|
|
83
|
+
existing.id
|
|
84
|
+
);
|
|
85
|
+
return existing.id;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const result = db.prepare(`
|
|
89
|
+
INSERT INTO massu_sentinel (feature_key, domain, subdomain, title, description, status, priority, portal_scope)
|
|
90
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
|
91
|
+
`).run(
|
|
92
|
+
input.feature_key,
|
|
93
|
+
input.domain,
|
|
94
|
+
input.subdomain || null,
|
|
95
|
+
input.title,
|
|
96
|
+
input.description || null,
|
|
97
|
+
input.status || 'active',
|
|
98
|
+
input.priority || 'standard',
|
|
99
|
+
JSON.stringify(input.portal_scope || [])
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
return Number(result.lastInsertRowid);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export function getFeature(db: Database.Database, featureKey: string): Feature | null {
|
|
106
|
+
const row = db.prepare('SELECT * FROM massu_sentinel WHERE feature_key = ?').get(featureKey) as Record<string, unknown> | undefined;
|
|
107
|
+
return row ? toFeature(row) : null;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function getFeatureById(db: Database.Database, id: number): Feature | null {
|
|
111
|
+
const row = db.prepare('SELECT * FROM massu_sentinel WHERE id = ?').get(id) as Record<string, unknown> | undefined;
|
|
112
|
+
return row ? toFeature(row) : null;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// ============================================================
|
|
116
|
+
// Search & Query
|
|
117
|
+
// ============================================================
|
|
118
|
+
|
|
119
|
+
export function searchFeatures(
|
|
120
|
+
db: Database.Database,
|
|
121
|
+
query: string,
|
|
122
|
+
filters?: { domain?: string; subdomain?: string; status?: string; portal?: string; page_route?: string }
|
|
123
|
+
): FeatureWithCounts[] {
|
|
124
|
+
let sql: string;
|
|
125
|
+
const params: unknown[] = [];
|
|
126
|
+
|
|
127
|
+
if (query && query.trim()) {
|
|
128
|
+
// FTS5 search
|
|
129
|
+
sql = `
|
|
130
|
+
SELECT s.*, fts.rank,
|
|
131
|
+
(SELECT COUNT(*) FROM massu_sentinel_components WHERE feature_id = s.id) as component_count,
|
|
132
|
+
(SELECT COUNT(*) FROM massu_sentinel_procedures WHERE feature_id = s.id) as procedure_count,
|
|
133
|
+
(SELECT COUNT(*) FROM massu_sentinel_pages WHERE feature_id = s.id) as page_count
|
|
134
|
+
FROM massu_sentinel s
|
|
135
|
+
JOIN massu_sentinel_fts fts ON s.id = fts.rowid
|
|
136
|
+
WHERE massu_sentinel_fts MATCH ?
|
|
137
|
+
`;
|
|
138
|
+
params.push(query);
|
|
139
|
+
} else {
|
|
140
|
+
sql = `
|
|
141
|
+
SELECT s.*,
|
|
142
|
+
(SELECT COUNT(*) FROM massu_sentinel_components WHERE feature_id = s.id) as component_count,
|
|
143
|
+
(SELECT COUNT(*) FROM massu_sentinel_procedures WHERE feature_id = s.id) as procedure_count,
|
|
144
|
+
(SELECT COUNT(*) FROM massu_sentinel_pages WHERE feature_id = s.id) as page_count
|
|
145
|
+
FROM massu_sentinel s
|
|
146
|
+
WHERE 1=1
|
|
147
|
+
`;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (filters?.domain) {
|
|
151
|
+
sql += ' AND s.domain = ?';
|
|
152
|
+
params.push(filters.domain);
|
|
153
|
+
}
|
|
154
|
+
if (filters?.subdomain) {
|
|
155
|
+
sql += ' AND s.subdomain = ?';
|
|
156
|
+
params.push(filters.subdomain);
|
|
157
|
+
}
|
|
158
|
+
if (filters?.status) {
|
|
159
|
+
sql += ' AND s.status = ?';
|
|
160
|
+
params.push(filters.status);
|
|
161
|
+
}
|
|
162
|
+
if (filters?.portal) {
|
|
163
|
+
sql += ` AND s.portal_scope LIKE ? ESCAPE '\\'`;
|
|
164
|
+
const escapedPortal = filters.portal.replace(/[%_]/g, '\\$&');
|
|
165
|
+
params.push(`%"${escapedPortal}"%`);
|
|
166
|
+
}
|
|
167
|
+
if (filters?.page_route) {
|
|
168
|
+
sql += ' AND s.id IN (SELECT feature_id FROM massu_sentinel_pages WHERE page_route = ?)';
|
|
169
|
+
params.push(filters.page_route);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
sql += query && query.trim() ? ' ORDER BY fts.rank LIMIT 100' : ' ORDER BY s.domain, s.subdomain, s.feature_key LIMIT 100';
|
|
173
|
+
|
|
174
|
+
const rows = db.prepare(sql).all(...params) as (Record<string, unknown>)[];
|
|
175
|
+
return rows.map(row => ({
|
|
176
|
+
...toFeature(row),
|
|
177
|
+
component_count: row.component_count as number,
|
|
178
|
+
procedure_count: row.procedure_count as number,
|
|
179
|
+
page_count: row.page_count as number,
|
|
180
|
+
}));
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export function getFeaturesByDomain(db: Database.Database, domain: string): Feature[] {
|
|
184
|
+
const rows = db.prepare('SELECT * FROM massu_sentinel WHERE domain = ? ORDER BY subdomain, feature_key').all(domain) as Record<string, unknown>[];
|
|
185
|
+
return rows.map(toFeature);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
export function getFeaturesByFile(db: Database.Database, filePath: string): Feature[] {
|
|
189
|
+
const rows = db.prepare(`
|
|
190
|
+
SELECT DISTINCT s.* FROM massu_sentinel s
|
|
191
|
+
JOIN massu_sentinel_components c ON c.feature_id = s.id
|
|
192
|
+
WHERE c.component_file = ?
|
|
193
|
+
ORDER BY s.feature_key
|
|
194
|
+
`).all(filePath) as Record<string, unknown>[];
|
|
195
|
+
return rows.map(toFeature);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
export function getFeaturesByRoute(db: Database.Database, route: string): Feature[] {
|
|
199
|
+
const rows = db.prepare(`
|
|
200
|
+
SELECT DISTINCT s.* FROM massu_sentinel s
|
|
201
|
+
JOIN massu_sentinel_pages p ON p.feature_id = s.id
|
|
202
|
+
WHERE p.page_route = ?
|
|
203
|
+
ORDER BY s.feature_key
|
|
204
|
+
`).all(route) as Record<string, unknown>[];
|
|
205
|
+
return rows.map(toFeature);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// ============================================================
|
|
209
|
+
// Feature Detail (full join)
|
|
210
|
+
// ============================================================
|
|
211
|
+
|
|
212
|
+
export function getFeatureDetail(db: Database.Database, featureKeyOrId: string | number): FeatureDetail | null {
|
|
213
|
+
let feature: Feature | null;
|
|
214
|
+
if (typeof featureKeyOrId === 'number') {
|
|
215
|
+
feature = getFeatureById(db, featureKeyOrId);
|
|
216
|
+
} else {
|
|
217
|
+
feature = getFeature(db, featureKeyOrId);
|
|
218
|
+
}
|
|
219
|
+
if (!feature) return null;
|
|
220
|
+
|
|
221
|
+
const components = db.prepare('SELECT * FROM massu_sentinel_components WHERE feature_id = ?').all(feature.id) as FeatureComponent[];
|
|
222
|
+
const procedures = db.prepare('SELECT * FROM massu_sentinel_procedures WHERE feature_id = ?').all(feature.id) as FeatureProcedure[];
|
|
223
|
+
const pages = db.prepare('SELECT * FROM massu_sentinel_pages WHERE feature_id = ?').all(feature.id) as FeaturePage[];
|
|
224
|
+
const dependencies = db.prepare('SELECT * FROM massu_sentinel_deps WHERE feature_id = ?').all(feature.id) as FeatureDep[];
|
|
225
|
+
const changelog = db.prepare('SELECT * FROM massu_sentinel_changelog WHERE feature_id = ? ORDER BY created_at DESC LIMIT 50').all(feature.id) as FeatureChangeLog[];
|
|
226
|
+
|
|
227
|
+
return { ...feature, components, procedures, pages, dependencies, changelog };
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// ============================================================
|
|
231
|
+
// Orphan & Impact Detection
|
|
232
|
+
// ============================================================
|
|
233
|
+
|
|
234
|
+
export function getOrphanedFeatures(db: Database.Database): Feature[] {
|
|
235
|
+
// Active features with no living primary component files
|
|
236
|
+
const features = db.prepare(`
|
|
237
|
+
SELECT s.* FROM massu_sentinel s
|
|
238
|
+
WHERE s.status = 'active'
|
|
239
|
+
AND NOT EXISTS (
|
|
240
|
+
SELECT 1 FROM massu_sentinel_components c
|
|
241
|
+
WHERE c.feature_id = s.id AND c.is_primary = 1
|
|
242
|
+
)
|
|
243
|
+
ORDER BY s.domain, s.feature_key
|
|
244
|
+
`).all() as Record<string, unknown>[];
|
|
245
|
+
return features.map(toFeature);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
export function getFeatureImpact(db: Database.Database, filePaths: string[]): ImpactReport {
|
|
249
|
+
const fileSet = new Set(filePaths);
|
|
250
|
+
const affectedFeatureIds = new Set<number>();
|
|
251
|
+
|
|
252
|
+
// Find all features linked to these files
|
|
253
|
+
for (const filePath of filePaths) {
|
|
254
|
+
const links = db.prepare(
|
|
255
|
+
'SELECT feature_id FROM massu_sentinel_components WHERE component_file = ?'
|
|
256
|
+
).all(filePath) as { feature_id: number }[];
|
|
257
|
+
for (const link of links) {
|
|
258
|
+
affectedFeatureIds.add(link.feature_id);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
const orphaned: ImpactItem[] = [];
|
|
263
|
+
const degraded: ImpactItem[] = [];
|
|
264
|
+
const unaffected: ImpactItem[] = [];
|
|
265
|
+
|
|
266
|
+
for (const featureId of affectedFeatureIds) {
|
|
267
|
+
const feature = getFeatureById(db, featureId);
|
|
268
|
+
if (!feature || feature.status !== 'active') continue;
|
|
269
|
+
|
|
270
|
+
const allComponents = db.prepare(
|
|
271
|
+
'SELECT component_file, is_primary FROM massu_sentinel_components WHERE feature_id = ?'
|
|
272
|
+
).all(featureId) as { component_file: string; is_primary: number }[];
|
|
273
|
+
|
|
274
|
+
const affected = allComponents.filter(c => fileSet.has(c.component_file));
|
|
275
|
+
const remaining = allComponents.filter(c => !fileSet.has(c.component_file));
|
|
276
|
+
const primaryAffected = affected.some(c => c.is_primary);
|
|
277
|
+
|
|
278
|
+
const item: ImpactItem = {
|
|
279
|
+
feature,
|
|
280
|
+
affected_files: affected.map(c => c.component_file),
|
|
281
|
+
remaining_files: remaining.map(c => c.component_file),
|
|
282
|
+
status: 'unaffected',
|
|
283
|
+
};
|
|
284
|
+
|
|
285
|
+
if (primaryAffected && remaining.filter(c => c.is_primary).length === 0) {
|
|
286
|
+
item.status = 'orphaned';
|
|
287
|
+
orphaned.push(item);
|
|
288
|
+
} else if (affected.length > 0) {
|
|
289
|
+
item.status = 'degraded';
|
|
290
|
+
degraded.push(item);
|
|
291
|
+
} else {
|
|
292
|
+
unaffected.push(item);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
const hasCriticalOrphans = orphaned.some(o => o.feature.priority === 'critical');
|
|
297
|
+
const hasStandardOrphans = orphaned.some(o => o.feature.priority === 'standard');
|
|
298
|
+
|
|
299
|
+
return {
|
|
300
|
+
files_analyzed: filePaths,
|
|
301
|
+
orphaned,
|
|
302
|
+
degraded,
|
|
303
|
+
unaffected,
|
|
304
|
+
blocked: hasCriticalOrphans || hasStandardOrphans,
|
|
305
|
+
block_reason: hasCriticalOrphans
|
|
306
|
+
? `BLOCKED: ${orphaned.length} features would be orphaned (includes critical features). Create migration plan first.`
|
|
307
|
+
: hasStandardOrphans
|
|
308
|
+
? `BLOCKED: ${orphaned.length} standard features would be orphaned. Create migration plan first.`
|
|
309
|
+
: null,
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// ============================================================
|
|
314
|
+
// Links: Components, Procedures, Pages
|
|
315
|
+
// ============================================================
|
|
316
|
+
|
|
317
|
+
export function linkComponent(
|
|
318
|
+
db: Database.Database,
|
|
319
|
+
featureId: number,
|
|
320
|
+
filePath: string,
|
|
321
|
+
componentName: string | null,
|
|
322
|
+
role: ComponentRole = 'implementation',
|
|
323
|
+
isPrimary: boolean = false
|
|
324
|
+
): void {
|
|
325
|
+
db.prepare(`
|
|
326
|
+
INSERT OR REPLACE INTO massu_sentinel_components (feature_id, component_file, component_name, role, is_primary)
|
|
327
|
+
VALUES (?, ?, ?, ?, ?)
|
|
328
|
+
`).run(featureId, filePath, componentName, role, isPrimary ? 1 : 0);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
export function linkProcedure(
|
|
332
|
+
db: Database.Database,
|
|
333
|
+
featureId: number,
|
|
334
|
+
routerName: string,
|
|
335
|
+
procedureName: string,
|
|
336
|
+
procedureType?: string
|
|
337
|
+
): void {
|
|
338
|
+
db.prepare(`
|
|
339
|
+
INSERT OR REPLACE INTO massu_sentinel_procedures (feature_id, router_name, procedure_name, procedure_type)
|
|
340
|
+
VALUES (?, ?, ?, ?)
|
|
341
|
+
`).run(featureId, routerName, procedureName, procedureType || null);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
export function linkPage(
|
|
345
|
+
db: Database.Database,
|
|
346
|
+
featureId: number,
|
|
347
|
+
route: string,
|
|
348
|
+
portal?: string
|
|
349
|
+
): void {
|
|
350
|
+
db.prepare(`
|
|
351
|
+
INSERT OR REPLACE INTO massu_sentinel_pages (feature_id, page_route, portal)
|
|
352
|
+
VALUES (?, ?, ?)
|
|
353
|
+
`).run(featureId, route, portal || null);
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
// ============================================================
|
|
357
|
+
// Changelog
|
|
358
|
+
// ============================================================
|
|
359
|
+
|
|
360
|
+
export function logChange(
|
|
361
|
+
db: Database.Database,
|
|
362
|
+
featureId: number,
|
|
363
|
+
changeType: string,
|
|
364
|
+
detail: string | null,
|
|
365
|
+
commitHash?: string,
|
|
366
|
+
changedBy: string = 'claude-code'
|
|
367
|
+
): void {
|
|
368
|
+
db.prepare(`
|
|
369
|
+
INSERT INTO massu_sentinel_changelog (feature_id, change_type, changed_by, change_detail, commit_hash)
|
|
370
|
+
VALUES (?, ?, ?, ?, ?)
|
|
371
|
+
`).run(featureId, changeType, changedBy, detail, commitHash || null);
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
// ============================================================
|
|
375
|
+
// Validation
|
|
376
|
+
// ============================================================
|
|
377
|
+
|
|
378
|
+
export function validateFeatures(db: Database.Database, domainFilter?: string): ValidationReport {
|
|
379
|
+
let sql = `SELECT * FROM massu_sentinel WHERE status = 'active'`;
|
|
380
|
+
const params: unknown[] = [];
|
|
381
|
+
if (domainFilter) {
|
|
382
|
+
sql += ' AND domain = ?';
|
|
383
|
+
params.push(domainFilter);
|
|
384
|
+
}
|
|
385
|
+
sql += ' ORDER BY domain, feature_key';
|
|
386
|
+
|
|
387
|
+
const features = db.prepare(sql).all(...params) as Record<string, unknown>[];
|
|
388
|
+
const details: ValidationItem[] = [];
|
|
389
|
+
let alive = 0;
|
|
390
|
+
let orphaned = 0;
|
|
391
|
+
let degradedCount = 0;
|
|
392
|
+
|
|
393
|
+
for (const row of features) {
|
|
394
|
+
const feature = toFeature(row);
|
|
395
|
+
const components = db.prepare('SELECT * FROM massu_sentinel_components WHERE feature_id = ?').all(feature.id) as FeatureComponent[];
|
|
396
|
+
const procedures = db.prepare('SELECT * FROM massu_sentinel_procedures WHERE feature_id = ?').all(feature.id) as FeatureProcedure[];
|
|
397
|
+
const pages = db.prepare('SELECT * FROM massu_sentinel_pages WHERE feature_id = ?').all(feature.id) as FeaturePage[];
|
|
398
|
+
|
|
399
|
+
const missingComponents: string[] = [];
|
|
400
|
+
const missingProcedures: { router: string; procedure: string }[] = [];
|
|
401
|
+
const missingPages: string[] = [];
|
|
402
|
+
|
|
403
|
+
// Check component files exist on disk
|
|
404
|
+
for (const comp of components) {
|
|
405
|
+
const absPath = resolve(PROJECT_ROOT, comp.component_file);
|
|
406
|
+
if (!existsSync(absPath)) {
|
|
407
|
+
missingComponents.push(comp.component_file);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
// Check procedure files exist and contain the procedure
|
|
412
|
+
for (const proc of procedures) {
|
|
413
|
+
// Look for the router file based on convention
|
|
414
|
+
const routerPath = resolve(PROJECT_ROOT, `src/server/api/routers/${proc.router_name}.ts`);
|
|
415
|
+
if (!existsSync(routerPath)) {
|
|
416
|
+
missingProcedures.push({ router: proc.router_name, procedure: proc.procedure_name });
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
// Check page routes exist
|
|
421
|
+
for (const page of pages) {
|
|
422
|
+
const routeToPath = page.page_route
|
|
423
|
+
.replace(/^\/(portal-[^/]+\/)?/, 'src/app/')
|
|
424
|
+
.replace(/\/$/, '')
|
|
425
|
+
+ '/page.tsx';
|
|
426
|
+
const absPath = resolve(PROJECT_ROOT, routeToPath);
|
|
427
|
+
// Page route checking is approximate - don't flag as missing if path conversion is ambiguous
|
|
428
|
+
if (page.page_route.startsWith('/') && !existsSync(absPath)) {
|
|
429
|
+
// Try alternative path patterns
|
|
430
|
+
const altPath = resolve(PROJECT_ROOT, `src/app${page.page_route}/page.tsx`);
|
|
431
|
+
if (!existsSync(altPath)) {
|
|
432
|
+
missingPages.push(page.page_route);
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
const hasMissing = missingComponents.length > 0 || missingProcedures.length > 0;
|
|
438
|
+
const primaryMissing = components.filter(c => c.is_primary && missingComponents.includes(c.component_file)).length > 0;
|
|
439
|
+
const allPrimaryMissing = components.filter(c => c.is_primary).length > 0 &&
|
|
440
|
+
components.filter(c => c.is_primary).every(c => missingComponents.includes(c.component_file));
|
|
441
|
+
|
|
442
|
+
let status: 'alive' | 'orphaned' | 'degraded';
|
|
443
|
+
if (allPrimaryMissing || (components.length > 0 && missingComponents.length === components.length)) {
|
|
444
|
+
status = 'orphaned';
|
|
445
|
+
orphaned++;
|
|
446
|
+
} else if (hasMissing) {
|
|
447
|
+
status = 'degraded';
|
|
448
|
+
degradedCount++;
|
|
449
|
+
} else {
|
|
450
|
+
status = 'alive';
|
|
451
|
+
alive++;
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
details.push({
|
|
455
|
+
feature,
|
|
456
|
+
missing_components: missingComponents,
|
|
457
|
+
missing_procedures: missingProcedures,
|
|
458
|
+
missing_pages: missingPages,
|
|
459
|
+
status,
|
|
460
|
+
});
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
return { alive, orphaned, degraded: degradedCount, details };
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
// ============================================================
|
|
467
|
+
// Parity Check (for rebuild scenarios)
|
|
468
|
+
// ============================================================
|
|
469
|
+
|
|
470
|
+
export function checkParity(db: Database.Database, oldFiles: string[], newFiles: string[]): ParityReport {
|
|
471
|
+
const oldFileSet = new Set(oldFiles);
|
|
472
|
+
const newFileSet = new Set(newFiles);
|
|
473
|
+
|
|
474
|
+
// Find features linked to old files
|
|
475
|
+
const oldFeatureIds = new Set<number>();
|
|
476
|
+
for (const file of oldFiles) {
|
|
477
|
+
const links = db.prepare('SELECT feature_id FROM massu_sentinel_components WHERE component_file = ?').all(file) as { feature_id: number }[];
|
|
478
|
+
for (const link of links) {
|
|
479
|
+
oldFeatureIds.add(link.feature_id);
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
// Find features linked to new files
|
|
484
|
+
const newFeatureIds = new Set<number>();
|
|
485
|
+
for (const file of newFiles) {
|
|
486
|
+
const links = db.prepare('SELECT feature_id FROM massu_sentinel_components WHERE component_file = ?').all(file) as { feature_id: number }[];
|
|
487
|
+
for (const link of links) {
|
|
488
|
+
newFeatureIds.add(link.feature_id);
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
const done: ParityItem[] = [];
|
|
493
|
+
const gaps: ParityItem[] = [];
|
|
494
|
+
const newFeatures: ParityItem[] = [];
|
|
495
|
+
|
|
496
|
+
// Features in old that are also in new = DONE
|
|
497
|
+
// Features in old but NOT in new = GAP
|
|
498
|
+
for (const fId of oldFeatureIds) {
|
|
499
|
+
const feature = getFeatureById(db, fId);
|
|
500
|
+
if (!feature) continue;
|
|
501
|
+
|
|
502
|
+
const oldComps = db.prepare('SELECT component_file FROM massu_sentinel_components WHERE feature_id = ? AND component_file IN (' + oldFiles.map(() => '?').join(',') + ')').all(fId, ...oldFiles) as { component_file: string }[];
|
|
503
|
+
const newComps = db.prepare('SELECT component_file FROM massu_sentinel_components WHERE feature_id = ? AND component_file IN (' + newFiles.map(() => '?').join(',') + ')').all(fId, ...newFiles) as { component_file: string }[];
|
|
504
|
+
|
|
505
|
+
const item: ParityItem = {
|
|
506
|
+
feature_key: feature.feature_key,
|
|
507
|
+
title: feature.title,
|
|
508
|
+
status: newFeatureIds.has(fId) ? 'DONE' : 'GAP',
|
|
509
|
+
old_files: oldComps.map(c => c.component_file),
|
|
510
|
+
new_files: newComps.map(c => c.component_file),
|
|
511
|
+
};
|
|
512
|
+
|
|
513
|
+
if (item.status === 'DONE') {
|
|
514
|
+
done.push(item);
|
|
515
|
+
} else {
|
|
516
|
+
gaps.push(item);
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
// Features only in new = NEW
|
|
521
|
+
for (const fId of newFeatureIds) {
|
|
522
|
+
if (oldFeatureIds.has(fId)) continue;
|
|
523
|
+
const feature = getFeatureById(db, fId);
|
|
524
|
+
if (!feature) continue;
|
|
525
|
+
|
|
526
|
+
const newComps = db.prepare('SELECT component_file FROM massu_sentinel_components WHERE feature_id = ? AND component_file IN (' + newFiles.map(() => '?').join(',') + ')').all(fId, ...newFiles) as { component_file: string }[];
|
|
527
|
+
|
|
528
|
+
newFeatures.push({
|
|
529
|
+
feature_key: feature.feature_key,
|
|
530
|
+
title: feature.title,
|
|
531
|
+
status: 'NEW',
|
|
532
|
+
old_files: [],
|
|
533
|
+
new_files: newComps.map(c => c.component_file),
|
|
534
|
+
});
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
const total = done.length + gaps.length;
|
|
538
|
+
const parityPercentage = total > 0 ? Math.round((done.length / total) * 100) : 100;
|
|
539
|
+
|
|
540
|
+
return { done, gaps, new_features: newFeatures, parity_percentage: parityPercentage };
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
// ============================================================
|
|
544
|
+
// Bulk operations for scanner
|
|
545
|
+
// ============================================================
|
|
546
|
+
|
|
547
|
+
export function clearAutoDiscoveredFeatures(db: Database.Database): void {
|
|
548
|
+
// Only clear features that were auto-discovered (not manually registered)
|
|
549
|
+
// We use the changelog to distinguish: auto-discovered ones have changed_by = 'scanner'
|
|
550
|
+
const autoIds = db.prepare(`
|
|
551
|
+
SELECT DISTINCT feature_id FROM massu_sentinel_changelog
|
|
552
|
+
WHERE changed_by = 'scanner' AND change_type = 'created'
|
|
553
|
+
`).all() as { feature_id: number }[];
|
|
554
|
+
|
|
555
|
+
// Don't delete features that also have manual changes
|
|
556
|
+
for (const { feature_id } of autoIds) {
|
|
557
|
+
const hasManualChanges = db.prepare(`
|
|
558
|
+
SELECT 1 FROM massu_sentinel_changelog
|
|
559
|
+
WHERE feature_id = ? AND changed_by != 'scanner'
|
|
560
|
+
`).get(feature_id);
|
|
561
|
+
|
|
562
|
+
if (!hasManualChanges) {
|
|
563
|
+
db.prepare('DELETE FROM massu_sentinel WHERE id = ?').run(feature_id);
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
export function bulkUpsertFeatures(db: Database.Database, features: FeatureInput[]): number {
|
|
569
|
+
let count = 0;
|
|
570
|
+
const upsert = db.transaction((items: FeatureInput[]) => {
|
|
571
|
+
for (const item of items) {
|
|
572
|
+
upsertFeature(db, item);
|
|
573
|
+
count++;
|
|
574
|
+
}
|
|
575
|
+
});
|
|
576
|
+
upsert(features);
|
|
577
|
+
return count;
|
|
578
|
+
}
|