Brixyy commited on
Commit
1156e7e
·
unverified ·
1 Parent(s): 54857ba

fix: scope memory health diagnostics to allowed prefixes (#367)

Browse files

When memoryAllowedPrefixes is configured, scan only those subdirectories instead of the entire MEMORY_PATH. Falls back to full scan if no prefixes are set.\n\nCloses #366

Files changed (1) hide show
  1. src/app/api/memory/health/route.ts +42 -0
src/app/api/memory/health/route.ts CHANGED
@@ -1,4 +1,6 @@
1
  import { NextRequest, NextResponse } from 'next/server'
 
 
2
  import { config } from '@/lib/config'
3
  import { requireRole } from '@/lib/auth'
4
  import { readLimiter } from '@/lib/rate-limit'
@@ -6,6 +8,35 @@ import { runHealthDiagnostics } from '@/lib/memory-utils'
6
  import { logger } from '@/lib/logger'
7
 
8
  const MEMORY_PATH = config.memoryDir
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  export async function GET(request: NextRequest) {
11
  const auth = requireRole(request, 'viewer')
@@ -19,6 +50,17 @@ export async function GET(request: NextRequest) {
19
  }
20
 
21
  try {
 
 
 
 
 
 
 
 
 
 
 
22
  const report = await runHealthDiagnostics(MEMORY_PATH)
23
  return NextResponse.json(report)
24
  } catch (err) {
 
1
  import { NextRequest, NextResponse } from 'next/server'
2
+ import { existsSync } from 'fs'
3
+ import { join } from 'path'
4
  import { config } from '@/lib/config'
5
  import { requireRole } from '@/lib/auth'
6
  import { readLimiter } from '@/lib/rate-limit'
 
8
  import { logger } from '@/lib/logger'
9
 
10
  const MEMORY_PATH = config.memoryDir
11
+ const MEMORY_ALLOWED_PREFIXES = (config.memoryAllowedPrefixes || []).map((p) => p.replace(/\\/g, '/'))
12
+
13
+ function mergeReports(reports: Awaited<ReturnType<typeof runHealthDiagnostics>>[]) {
14
+ const allCategories = reports.flatMap((report) => report.categories)
15
+ const mergedCategories = Array.from(new Set(allCategories.map((category) => category.name))).map((name) => {
16
+ const group = allCategories.filter((category) => category.name === name)
17
+ const score = Math.round(group.reduce((sum, category) => sum + category.score, 0) / group.length)
18
+ const status = score >= 80 ? 'healthy' : score >= 50 ? 'warning' : 'critical'
19
+ return {
20
+ name,
21
+ status,
22
+ score,
23
+ issues: group.flatMap((category) => category.issues).slice(0, 10),
24
+ suggestions: Array.from(new Set(group.flatMap((category) => category.suggestions))),
25
+ }
26
+ })
27
+
28
+ const overallScore = mergedCategories.length > 0
29
+ ? Math.round(mergedCategories.reduce((sum, category) => sum + category.score, 0) / mergedCategories.length)
30
+ : 100
31
+ const overall = overallScore >= 70 ? 'healthy' : overallScore >= 40 ? 'warning' : 'critical'
32
+
33
+ return {
34
+ overall,
35
+ overallScore,
36
+ categories: mergedCategories,
37
+ generatedAt: Date.now(),
38
+ }
39
+ }
40
 
41
  export async function GET(request: NextRequest) {
42
  const auth = requireRole(request, 'viewer')
 
50
  }
51
 
52
  try {
53
+ if (MEMORY_ALLOWED_PREFIXES.length) {
54
+ const reports = []
55
+ for (const prefix of MEMORY_ALLOWED_PREFIXES) {
56
+ const folder = prefix.replace(/\/$/, '')
57
+ const fullPath = join(MEMORY_PATH, folder)
58
+ if (!existsSync(fullPath)) continue
59
+ reports.push(await runHealthDiagnostics(fullPath))
60
+ }
61
+ return NextResponse.json(reports.length > 0 ? mergeReports(reports) : await runHealthDiagnostics(MEMORY_PATH))
62
+ }
63
+
64
  const report = await runHealthDiagnostics(MEMORY_PATH)
65
  return NextResponse.json(report)
66
  } catch (err) {