@grafema/util 0.3.0-beta

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 (324) hide show
  1. package/LICENSE +190 -0
  2. package/dist/api/GraphAPI.d.ts +87 -0
  3. package/dist/api/GraphAPI.d.ts.map +1 -0
  4. package/dist/api/GraphAPI.js +212 -0
  5. package/dist/api/GraphAPI.js.map +1 -0
  6. package/dist/api/GuaranteeAPI.d.ts +147 -0
  7. package/dist/api/GuaranteeAPI.d.ts.map +1 -0
  8. package/dist/api/GuaranteeAPI.js +290 -0
  9. package/dist/api/GuaranteeAPI.js.map +1 -0
  10. package/dist/config/ConfigLoader.d.ts +214 -0
  11. package/dist/config/ConfigLoader.d.ts.map +1 -0
  12. package/dist/config/ConfigLoader.js +441 -0
  13. package/dist/config/ConfigLoader.js.map +1 -0
  14. package/dist/config/index.d.ts +6 -0
  15. package/dist/config/index.d.ts.map +1 -0
  16. package/dist/config/index.js +5 -0
  17. package/dist/config/index.js.map +1 -0
  18. package/dist/core/CoverageAnalyzer.d.ts +65 -0
  19. package/dist/core/CoverageAnalyzer.d.ts.map +1 -0
  20. package/dist/core/CoverageAnalyzer.js +199 -0
  21. package/dist/core/CoverageAnalyzer.js.map +1 -0
  22. package/dist/core/FileExplainer.d.ts +101 -0
  23. package/dist/core/FileExplainer.d.ts.map +1 -0
  24. package/dist/core/FileExplainer.js +140 -0
  25. package/dist/core/FileExplainer.js.map +1 -0
  26. package/dist/core/FileOverview.d.ts +124 -0
  27. package/dist/core/FileOverview.d.ts.map +1 -0
  28. package/dist/core/FileOverview.js +279 -0
  29. package/dist/core/FileOverview.js.map +1 -0
  30. package/dist/core/GrafemaUri.d.ts +66 -0
  31. package/dist/core/GrafemaUri.d.ts.map +1 -0
  32. package/dist/core/GrafemaUri.js +191 -0
  33. package/dist/core/GrafemaUri.js.map +1 -0
  34. package/dist/core/GraphBackend.d.ts +158 -0
  35. package/dist/core/GraphBackend.d.ts.map +1 -0
  36. package/dist/core/GraphBackend.js +85 -0
  37. package/dist/core/GraphBackend.js.map +1 -0
  38. package/dist/core/GraphFreshnessChecker.d.ts +33 -0
  39. package/dist/core/GraphFreshnessChecker.d.ts.map +1 -0
  40. package/dist/core/GraphFreshnessChecker.js +104 -0
  41. package/dist/core/GraphFreshnessChecker.js.map +1 -0
  42. package/dist/core/GuaranteeManager.d.ts +254 -0
  43. package/dist/core/GuaranteeManager.d.ts.map +1 -0
  44. package/dist/core/GuaranteeManager.js +447 -0
  45. package/dist/core/GuaranteeManager.js.map +1 -0
  46. package/dist/core/HashUtils.d.ts +24 -0
  47. package/dist/core/HashUtils.d.ts.map +1 -0
  48. package/dist/core/HashUtils.js +46 -0
  49. package/dist/core/HashUtils.js.map +1 -0
  50. package/dist/core/IncrementalReanalyzer.d.ts +33 -0
  51. package/dist/core/IncrementalReanalyzer.d.ts.map +1 -0
  52. package/dist/core/IncrementalReanalyzer.js +67 -0
  53. package/dist/core/IncrementalReanalyzer.js.map +1 -0
  54. package/dist/core/ResourceRegistry.d.ts +17 -0
  55. package/dist/core/ResourceRegistry.d.ts.map +1 -0
  56. package/dist/core/ResourceRegistry.js +32 -0
  57. package/dist/core/ResourceRegistry.js.map +1 -0
  58. package/dist/core/SemanticId.d.ts +159 -0
  59. package/dist/core/SemanticId.d.ts.map +1 -0
  60. package/dist/core/SemanticId.js +291 -0
  61. package/dist/core/SemanticId.js.map +1 -0
  62. package/dist/core/VersionManager.d.ts +166 -0
  63. package/dist/core/VersionManager.d.ts.map +1 -0
  64. package/dist/core/VersionManager.js +239 -0
  65. package/dist/core/VersionManager.js.map +1 -0
  66. package/dist/core/brandNodeInternal.d.ts +14 -0
  67. package/dist/core/brandNodeInternal.d.ts.map +1 -0
  68. package/dist/core/brandNodeInternal.js +4 -0
  69. package/dist/core/brandNodeInternal.js.map +1 -0
  70. package/dist/core/nodes/GuaranteeNode.d.ts +76 -0
  71. package/dist/core/nodes/GuaranteeNode.d.ts.map +1 -0
  72. package/dist/core/nodes/GuaranteeNode.js +118 -0
  73. package/dist/core/nodes/GuaranteeNode.js.map +1 -0
  74. package/dist/core/nodes/IssueNode.d.ts +73 -0
  75. package/dist/core/nodes/IssueNode.d.ts.map +1 -0
  76. package/dist/core/nodes/IssueNode.js +130 -0
  77. package/dist/core/nodes/IssueNode.js.map +1 -0
  78. package/dist/core/nodes/NodeKind.d.ts +104 -0
  79. package/dist/core/nodes/NodeKind.d.ts.map +1 -0
  80. package/dist/core/nodes/NodeKind.js +166 -0
  81. package/dist/core/nodes/NodeKind.js.map +1 -0
  82. package/dist/diagnostics/DiagnosticCollector.d.ts +103 -0
  83. package/dist/diagnostics/DiagnosticCollector.d.ts.map +1 -0
  84. package/dist/diagnostics/DiagnosticCollector.js +133 -0
  85. package/dist/diagnostics/DiagnosticCollector.js.map +1 -0
  86. package/dist/diagnostics/DiagnosticReporter.d.ts +122 -0
  87. package/dist/diagnostics/DiagnosticReporter.d.ts.map +1 -0
  88. package/dist/diagnostics/DiagnosticReporter.js +300 -0
  89. package/dist/diagnostics/DiagnosticReporter.js.map +1 -0
  90. package/dist/diagnostics/DiagnosticWriter.d.ts +31 -0
  91. package/dist/diagnostics/DiagnosticWriter.d.ts.map +1 -0
  92. package/dist/diagnostics/DiagnosticWriter.js +44 -0
  93. package/dist/diagnostics/DiagnosticWriter.js.map +1 -0
  94. package/dist/diagnostics/categories.d.ts +57 -0
  95. package/dist/diagnostics/categories.d.ts.map +1 -0
  96. package/dist/diagnostics/categories.js +71 -0
  97. package/dist/diagnostics/categories.js.map +1 -0
  98. package/dist/diagnostics/index.d.ts +17 -0
  99. package/dist/diagnostics/index.d.ts.map +1 -0
  100. package/dist/diagnostics/index.js +15 -0
  101. package/dist/diagnostics/index.js.map +1 -0
  102. package/dist/errors/GrafemaError.d.ts +200 -0
  103. package/dist/errors/GrafemaError.d.ts.map +1 -0
  104. package/dist/errors/GrafemaError.js +209 -0
  105. package/dist/errors/GrafemaError.js.map +1 -0
  106. package/dist/index.d.ts +75 -0
  107. package/dist/index.d.ts.map +1 -0
  108. package/dist/index.js +76 -0
  109. package/dist/index.js.map +1 -0
  110. package/dist/instructions/index.d.ts +8 -0
  111. package/dist/instructions/index.d.ts.map +1 -0
  112. package/dist/instructions/index.js +20 -0
  113. package/dist/instructions/index.js.map +1 -0
  114. package/dist/instructions/onboarding.md +133 -0
  115. package/dist/knowledge/KnowledgeBase.d.ts +113 -0
  116. package/dist/knowledge/KnowledgeBase.d.ts.map +1 -0
  117. package/dist/knowledge/KnowledgeBase.js +420 -0
  118. package/dist/knowledge/KnowledgeBase.js.map +1 -0
  119. package/dist/knowledge/SemanticAddressResolver.d.ts +59 -0
  120. package/dist/knowledge/SemanticAddressResolver.d.ts.map +1 -0
  121. package/dist/knowledge/SemanticAddressResolver.js +160 -0
  122. package/dist/knowledge/SemanticAddressResolver.js.map +1 -0
  123. package/dist/knowledge/git-ingest.d.ts +58 -0
  124. package/dist/knowledge/git-ingest.d.ts.map +1 -0
  125. package/dist/knowledge/git-ingest.js +301 -0
  126. package/dist/knowledge/git-ingest.js.map +1 -0
  127. package/dist/knowledge/git-queries.d.ts +86 -0
  128. package/dist/knowledge/git-queries.d.ts.map +1 -0
  129. package/dist/knowledge/git-queries.js +177 -0
  130. package/dist/knowledge/git-queries.js.map +1 -0
  131. package/dist/knowledge/index.d.ts +14 -0
  132. package/dist/knowledge/index.d.ts.map +1 -0
  133. package/dist/knowledge/index.js +10 -0
  134. package/dist/knowledge/index.js.map +1 -0
  135. package/dist/knowledge/parser.d.ts +39 -0
  136. package/dist/knowledge/parser.d.ts.map +1 -0
  137. package/dist/knowledge/parser.js +292 -0
  138. package/dist/knowledge/parser.js.map +1 -0
  139. package/dist/knowledge/types.d.ts +133 -0
  140. package/dist/knowledge/types.d.ts.map +1 -0
  141. package/dist/knowledge/types.js +8 -0
  142. package/dist/knowledge/types.js.map +1 -0
  143. package/dist/logging/Logger.d.ts +98 -0
  144. package/dist/logging/Logger.d.ts.map +1 -0
  145. package/dist/logging/Logger.js +274 -0
  146. package/dist/logging/Logger.js.map +1 -0
  147. package/dist/notation/archetypes.d.ts +36 -0
  148. package/dist/notation/archetypes.d.ts.map +1 -0
  149. package/dist/notation/archetypes.js +173 -0
  150. package/dist/notation/archetypes.js.map +1 -0
  151. package/dist/notation/fold.d.ts +25 -0
  152. package/dist/notation/fold.d.ts.map +1 -0
  153. package/dist/notation/fold.js +598 -0
  154. package/dist/notation/fold.js.map +1 -0
  155. package/dist/notation/index.d.ts +18 -0
  156. package/dist/notation/index.d.ts.map +1 -0
  157. package/dist/notation/index.js +16 -0
  158. package/dist/notation/index.js.map +1 -0
  159. package/dist/notation/lodExtractor.d.ts +32 -0
  160. package/dist/notation/lodExtractor.d.ts.map +1 -0
  161. package/dist/notation/lodExtractor.js +149 -0
  162. package/dist/notation/lodExtractor.js.map +1 -0
  163. package/dist/notation/nameShortener.d.ts +22 -0
  164. package/dist/notation/nameShortener.d.ts.map +1 -0
  165. package/dist/notation/nameShortener.js +24 -0
  166. package/dist/notation/nameShortener.js.map +1 -0
  167. package/dist/notation/perspectives.d.ts +11 -0
  168. package/dist/notation/perspectives.d.ts.map +1 -0
  169. package/dist/notation/perspectives.js +16 -0
  170. package/dist/notation/perspectives.js.map +1 -0
  171. package/dist/notation/renderer.d.ts +31 -0
  172. package/dist/notation/renderer.d.ts.map +1 -0
  173. package/dist/notation/renderer.js +315 -0
  174. package/dist/notation/renderer.js.map +1 -0
  175. package/dist/notation/traceRenderer.d.ts +39 -0
  176. package/dist/notation/traceRenderer.d.ts.map +1 -0
  177. package/dist/notation/traceRenderer.js +358 -0
  178. package/dist/notation/traceRenderer.js.map +1 -0
  179. package/dist/notation/types.d.ts +66 -0
  180. package/dist/notation/types.d.ts.map +1 -0
  181. package/dist/notation/types.js +10 -0
  182. package/dist/notation/types.js.map +1 -0
  183. package/dist/queries/NodeContext.d.ts +81 -0
  184. package/dist/queries/NodeContext.d.ts.map +1 -0
  185. package/dist/queries/NodeContext.js +196 -0
  186. package/dist/queries/NodeContext.js.map +1 -0
  187. package/dist/queries/findCallsInFunction.d.ts +62 -0
  188. package/dist/queries/findCallsInFunction.d.ts.map +1 -0
  189. package/dist/queries/findCallsInFunction.js +169 -0
  190. package/dist/queries/findCallsInFunction.js.map +1 -0
  191. package/dist/queries/findContainingFunction.d.ts +57 -0
  192. package/dist/queries/findContainingFunction.d.ts.map +1 -0
  193. package/dist/queries/findContainingFunction.js +91 -0
  194. package/dist/queries/findContainingFunction.js.map +1 -0
  195. package/dist/queries/index.d.ts +18 -0
  196. package/dist/queries/index.d.ts.map +1 -0
  197. package/dist/queries/index.js +14 -0
  198. package/dist/queries/index.js.map +1 -0
  199. package/dist/queries/traceDataflow.d.ts +65 -0
  200. package/dist/queries/traceDataflow.d.ts.map +1 -0
  201. package/dist/queries/traceDataflow.js +754 -0
  202. package/dist/queries/traceDataflow.js.map +1 -0
  203. package/dist/queries/traceValues.d.ts +70 -0
  204. package/dist/queries/traceValues.d.ts.map +1 -0
  205. package/dist/queries/traceValues.js +373 -0
  206. package/dist/queries/traceValues.js.map +1 -0
  207. package/dist/queries/types.d.ts +166 -0
  208. package/dist/queries/types.d.ts.map +1 -0
  209. package/dist/queries/types.js +10 -0
  210. package/dist/queries/types.js.map +1 -0
  211. package/dist/schema/GraphSchemaExtractor.d.ts +53 -0
  212. package/dist/schema/GraphSchemaExtractor.d.ts.map +1 -0
  213. package/dist/schema/GraphSchemaExtractor.js +125 -0
  214. package/dist/schema/GraphSchemaExtractor.js.map +1 -0
  215. package/dist/schema/InterfaceSchemaExtractor.d.ts +73 -0
  216. package/dist/schema/InterfaceSchemaExtractor.d.ts.map +1 -0
  217. package/dist/schema/InterfaceSchemaExtractor.js +113 -0
  218. package/dist/schema/InterfaceSchemaExtractor.js.map +1 -0
  219. package/dist/schema/index.d.ts +5 -0
  220. package/dist/schema/index.d.ts.map +1 -0
  221. package/dist/schema/index.js +3 -0
  222. package/dist/schema/index.js.map +1 -0
  223. package/dist/storage/backends/RFDBServerBackend.d.ts +356 -0
  224. package/dist/storage/backends/RFDBServerBackend.d.ts.map +1 -0
  225. package/dist/storage/backends/RFDBServerBackend.js +748 -0
  226. package/dist/storage/backends/RFDBServerBackend.js.map +1 -0
  227. package/dist/storage/backends/typeValidation.d.ts +47 -0
  228. package/dist/storage/backends/typeValidation.d.ts.map +1 -0
  229. package/dist/storage/backends/typeValidation.js +141 -0
  230. package/dist/storage/backends/typeValidation.js.map +1 -0
  231. package/dist/utils/findRfdbBinary.d.ts +67 -0
  232. package/dist/utils/findRfdbBinary.d.ts.map +1 -0
  233. package/dist/utils/findRfdbBinary.js +261 -0
  234. package/dist/utils/findRfdbBinary.js.map +1 -0
  235. package/dist/utils/lazyDownload.d.ts +43 -0
  236. package/dist/utils/lazyDownload.d.ts.map +1 -0
  237. package/dist/utils/lazyDownload.js +175 -0
  238. package/dist/utils/lazyDownload.js.map +1 -0
  239. package/dist/utils/moduleResolution.d.ts +134 -0
  240. package/dist/utils/moduleResolution.d.ts.map +1 -0
  241. package/dist/utils/moduleResolution.js +189 -0
  242. package/dist/utils/moduleResolution.js.map +1 -0
  243. package/dist/utils/resolveNodeFile.d.ts +13 -0
  244. package/dist/utils/resolveNodeFile.d.ts.map +1 -0
  245. package/dist/utils/resolveNodeFile.js +18 -0
  246. package/dist/utils/resolveNodeFile.js.map +1 -0
  247. package/dist/utils/startRfdbServer.d.ts +63 -0
  248. package/dist/utils/startRfdbServer.d.ts.map +1 -0
  249. package/dist/utils/startRfdbServer.js +142 -0
  250. package/dist/utils/startRfdbServer.js.map +1 -0
  251. package/dist/validation/PathValidator.d.ts +80 -0
  252. package/dist/validation/PathValidator.d.ts.map +1 -0
  253. package/dist/validation/PathValidator.js +252 -0
  254. package/dist/validation/PathValidator.js.map +1 -0
  255. package/dist/version.d.ts +11 -0
  256. package/dist/version.d.ts.map +1 -0
  257. package/dist/version.js +26 -0
  258. package/dist/version.js.map +1 -0
  259. package/package.json +50 -0
  260. package/src/api/GraphAPI.ts +307 -0
  261. package/src/api/GuaranteeAPI.ts +402 -0
  262. package/src/config/ConfigLoader.ts +653 -0
  263. package/src/config/index.ts +13 -0
  264. package/src/core/CoverageAnalyzer.ts +243 -0
  265. package/src/core/FileExplainer.ts +179 -0
  266. package/src/core/FileOverview.ts +397 -0
  267. package/src/core/GrafemaUri.ts +216 -0
  268. package/src/core/GraphBackend.ts +266 -0
  269. package/src/core/GraphFreshnessChecker.ts +145 -0
  270. package/src/core/GuaranteeManager.ts +684 -0
  271. package/src/core/HashUtils.ts +48 -0
  272. package/src/core/IncrementalReanalyzer.ts +106 -0
  273. package/src/core/ResourceRegistry.ts +39 -0
  274. package/src/core/SemanticId.ts +423 -0
  275. package/src/core/VersionManager.ts +405 -0
  276. package/src/core/brandNodeInternal.ts +16 -0
  277. package/src/core/nodes/GuaranteeNode.ts +162 -0
  278. package/src/core/nodes/IssueNode.ts +177 -0
  279. package/src/core/nodes/NodeKind.ts +192 -0
  280. package/src/diagnostics/DiagnosticCollector.ts +170 -0
  281. package/src/diagnostics/DiagnosticReporter.ts +395 -0
  282. package/src/diagnostics/DiagnosticWriter.ts +50 -0
  283. package/src/diagnostics/categories.ts +104 -0
  284. package/src/diagnostics/index.ts +30 -0
  285. package/src/errors/GrafemaError.ts +297 -0
  286. package/src/index.ts +261 -0
  287. package/src/instructions/index.ts +21 -0
  288. package/src/instructions/onboarding.md +133 -0
  289. package/src/knowledge/KnowledgeBase.ts +486 -0
  290. package/src/knowledge/SemanticAddressResolver.ts +191 -0
  291. package/src/knowledge/git-ingest.ts +402 -0
  292. package/src/knowledge/git-queries.ts +269 -0
  293. package/src/knowledge/index.ts +29 -0
  294. package/src/knowledge/parser.ts +294 -0
  295. package/src/knowledge/types.ts +146 -0
  296. package/src/logging/Logger.ts +303 -0
  297. package/src/notation/archetypes.ts +189 -0
  298. package/src/notation/fold.ts +696 -0
  299. package/src/notation/index.ts +27 -0
  300. package/src/notation/lodExtractor.ts +177 -0
  301. package/src/notation/nameShortener.ts +24 -0
  302. package/src/notation/perspectives.ts +18 -0
  303. package/src/notation/renderer.ts +394 -0
  304. package/src/notation/traceRenderer.ts +458 -0
  305. package/src/notation/types.ts +79 -0
  306. package/src/queries/NodeContext.ts +280 -0
  307. package/src/queries/findCallsInFunction.ts +249 -0
  308. package/src/queries/findContainingFunction.ts +124 -0
  309. package/src/queries/index.ts +44 -0
  310. package/src/queries/traceDataflow.ts +838 -0
  311. package/src/queries/traceValues.ts +531 -0
  312. package/src/queries/types.ts +191 -0
  313. package/src/schema/GraphSchemaExtractor.ts +177 -0
  314. package/src/schema/InterfaceSchemaExtractor.ts +173 -0
  315. package/src/schema/index.ts +5 -0
  316. package/src/storage/backends/RFDBServerBackend.ts +895 -0
  317. package/src/storage/backends/typeValidation.ts +154 -0
  318. package/src/utils/findRfdbBinary.ts +288 -0
  319. package/src/utils/lazyDownload.ts +206 -0
  320. package/src/utils/moduleResolution.ts +271 -0
  321. package/src/utils/resolveNodeFile.ts +18 -0
  322. package/src/utils/startRfdbServer.ts +197 -0
  323. package/src/validation/PathValidator.ts +334 -0
  324. package/src/version.ts +28 -0
@@ -0,0 +1,243 @@
1
+ /**
2
+ * CoverageAnalyzer - Calculates analysis coverage for a project
3
+ *
4
+ * Determines which files in a project have been analyzed and categorizes
5
+ * files into three groups:
6
+ * - Analyzed: Files that appear as MODULE nodes in the graph
7
+ * - Unsupported: Files with extensions that no indexer can handle
8
+ * - Unreachable: Files with supported extensions but not in the graph
9
+ *
10
+ * Usage:
11
+ * const analyzer = new CoverageAnalyzer(graphBackend, '/path/to/project');
12
+ * const result = await analyzer.analyze();
13
+ */
14
+
15
+ import { readdirSync, existsSync, lstatSync } from 'fs';
16
+ import { join, relative, extname } from 'path';
17
+ import type { GraphBackend } from '@grafema/types';
18
+
19
+ /**
20
+ * Supported file extensions by language
21
+ */
22
+ const JS_SUPPORTED = ['.js', '.mjs', '.cjs', '.jsx', '.ts', '.tsx'];
23
+ const RUST_SUPPORTED = ['.rs'];
24
+ const SUPPORTED_EXTENSIONS = new Set([...JS_SUPPORTED, ...RUST_SUPPORTED]);
25
+
26
+ /**
27
+ * Known code file extensions (for scanning)
28
+ * Files with these extensions are considered source code
29
+ */
30
+ const CODE_EXTENSIONS = new Set([
31
+ // Supported
32
+ ...JS_SUPPORTED,
33
+ ...RUST_SUPPORTED,
34
+ // Unsupported but tracked
35
+ '.go', '.kt', '.java', '.py', '.rb', '.php', '.c', '.cpp', '.h', '.hpp',
36
+ '.cs', '.swift', '.scala', '.sql', '.graphql', '.gql',
37
+ ]);
38
+
39
+ /**
40
+ * Directories to always skip during scanning
41
+ */
42
+ const SKIP_DIRS = new Set([
43
+ 'node_modules',
44
+ '.git',
45
+ '.grafema',
46
+ 'dist',
47
+ 'build',
48
+ 'out',
49
+ 'coverage',
50
+ '.next',
51
+ '.nuxt',
52
+ '__pycache__',
53
+ 'target', // Rust
54
+ 'vendor',
55
+ ]);
56
+
57
+ /**
58
+ * Coverage analysis result
59
+ */
60
+ export interface CoverageResult {
61
+ projectPath: string;
62
+ total: number;
63
+ analyzed: {
64
+ count: number;
65
+ files: string[];
66
+ };
67
+ unsupported: {
68
+ count: number;
69
+ byExtension: Record<string, string[]>;
70
+ };
71
+ unreachable: {
72
+ count: number;
73
+ files: string[];
74
+ byExtension: Record<string, string[]>;
75
+ };
76
+ percentages: {
77
+ analyzed: number;
78
+ unsupported: number;
79
+ unreachable: number;
80
+ };
81
+ }
82
+
83
+ /**
84
+ * CoverageAnalyzer calculates what percentage of a codebase has been analyzed.
85
+ */
86
+ export class CoverageAnalyzer {
87
+ private graph: GraphBackend;
88
+ private projectPath: string;
89
+
90
+ constructor(graph: GraphBackend, projectPath: string) {
91
+ this.graph = graph;
92
+ this.projectPath = projectPath;
93
+ }
94
+
95
+ /**
96
+ * Analyze the project and return coverage statistics
97
+ */
98
+ async analyze(): Promise<CoverageResult> {
99
+ // Step 1: Get all MODULE nodes from the graph
100
+ const analyzedFiles = await this.getAnalyzedFiles();
101
+ const analyzedSet = new Set(analyzedFiles);
102
+
103
+ // Step 2: Scan project for all code files
104
+ const allCodeFiles = this.scanProjectFiles();
105
+
106
+ // Step 3: Categorize files
107
+ const unsupportedByExt: Record<string, string[]> = {};
108
+ const unreachableByExt: Record<string, string[]> = {};
109
+ const unreachableFiles: string[] = [];
110
+
111
+ for (const file of allCodeFiles) {
112
+ if (analyzedSet.has(file)) {
113
+ continue; // Already analyzed
114
+ }
115
+
116
+ const ext = extname(file).toLowerCase();
117
+
118
+ if (SUPPORTED_EXTENSIONS.has(ext)) {
119
+ // Supported but not in graph = unreachable
120
+ unreachableFiles.push(file);
121
+ if (!unreachableByExt[ext]) {
122
+ unreachableByExt[ext] = [];
123
+ }
124
+ unreachableByExt[ext].push(file);
125
+ } else {
126
+ // Not supported = unsupported
127
+ if (!unsupportedByExt[ext]) {
128
+ unsupportedByExt[ext] = [];
129
+ }
130
+ unsupportedByExt[ext].push(file);
131
+ }
132
+ }
133
+
134
+ // Calculate counts
135
+ const unsupportedCount = Object.values(unsupportedByExt)
136
+ .reduce((sum, files) => sum + files.length, 0);
137
+ const unreachableCount = unreachableFiles.length;
138
+ const analyzedCount = analyzedFiles.length;
139
+ const total = analyzedCount + unsupportedCount + unreachableCount;
140
+
141
+ // Calculate percentages (handle division by zero)
142
+ const percentages = {
143
+ analyzed: total > 0 ? Math.round((analyzedCount / total) * 100) : 0,
144
+ unsupported: total > 0 ? Math.round((unsupportedCount / total) * 100) : 0,
145
+ unreachable: total > 0 ? Math.round((unreachableCount / total) * 100) : 0,
146
+ };
147
+
148
+ return {
149
+ projectPath: this.projectPath,
150
+ total,
151
+ analyzed: {
152
+ count: analyzedCount,
153
+ files: analyzedFiles,
154
+ },
155
+ unsupported: {
156
+ count: unsupportedCount,
157
+ byExtension: unsupportedByExt,
158
+ },
159
+ unreachable: {
160
+ count: unreachableCount,
161
+ files: unreachableFiles,
162
+ byExtension: unreachableByExt,
163
+ },
164
+ percentages,
165
+ };
166
+ }
167
+
168
+ /**
169
+ * Get list of analyzed files from the graph (MODULE nodes)
170
+ */
171
+ private async getAnalyzedFiles(): Promise<string[]> {
172
+ const files: string[] = [];
173
+
174
+ // Use queryNodes with type: 'MODULE'
175
+ for await (const node of this.graph.queryNodes({ type: 'MODULE' })) {
176
+ if (node.file) {
177
+ // Store relative path for consistency
178
+ const relativePath = node.file.startsWith(this.projectPath)
179
+ ? relative(this.projectPath, node.file)
180
+ : node.file;
181
+ files.push(relativePath);
182
+ }
183
+ }
184
+
185
+ return files;
186
+ }
187
+
188
+ /**
189
+ * Scan project directory for all code files
190
+ * Respects common ignore patterns (node_modules, .git, etc.)
191
+ */
192
+ private scanProjectFiles(): string[] {
193
+ const files: string[] = [];
194
+ this.walkDirectory(this.projectPath, files);
195
+ return files;
196
+ }
197
+
198
+ /**
199
+ * Recursively walk directory and collect code files
200
+ */
201
+ private walkDirectory(dir: string, files: string[], depth = 0): void {
202
+ // Safety: limit recursion depth
203
+ if (depth > 20) return;
204
+
205
+ if (!existsSync(dir)) return;
206
+
207
+ let entries: string[];
208
+ try {
209
+ entries = readdirSync(dir);
210
+ } catch {
211
+ return; // Skip directories we can't read
212
+ }
213
+
214
+ for (const entry of entries) {
215
+ // Skip hidden files and directories
216
+ if (entry.startsWith('.')) continue;
217
+
218
+ // Skip known non-source directories
219
+ if (SKIP_DIRS.has(entry)) continue;
220
+
221
+ const fullPath = join(dir, entry);
222
+
223
+ // Skip symlinks to avoid infinite loops
224
+ try {
225
+ const stat = lstatSync(fullPath);
226
+ if (stat.isSymbolicLink()) continue;
227
+
228
+ if (stat.isDirectory()) {
229
+ this.walkDirectory(fullPath, files, depth + 1);
230
+ } else if (stat.isFile()) {
231
+ const ext = extname(entry).toLowerCase();
232
+ if (CODE_EXTENSIONS.has(ext)) {
233
+ // Store relative path
234
+ files.push(relative(this.projectPath, fullPath));
235
+ }
236
+ }
237
+ } catch {
238
+ // Skip files we can't stat
239
+ continue;
240
+ }
241
+ }
242
+ }
243
+ }
@@ -0,0 +1,179 @@
1
+ /**
2
+ * FileExplainer - Show what nodes exist in a file for discovery
3
+ *
4
+ * Purpose: Help users discover what nodes exist in the graph for a file,
5
+ * displaying semantic IDs so users can query them.
6
+ *
7
+ * This addresses the core UX problem: users can't find nodes because
8
+ * semantic IDs are opaque (e.g., "src/app.ts->fetchData->try#0->VARIABLE->response").
9
+ * This tool shows what's in the graph so users know what they can query.
10
+ *
11
+ * @see _tasks/REG-177/006-don-revised-plan.md for design rationale
12
+ */
13
+
14
+ import type { GraphBackend, BaseNodeRecord, NodeFilter } from '@grafema/types';
15
+
16
+ /**
17
+ * Result of explaining a file's graph contents
18
+ */
19
+ export interface FileExplainResult {
20
+ /** The file path that was explained */
21
+ file: string;
22
+ /** Whether the file has been analyzed */
23
+ status: 'ANALYZED' | 'NOT_ANALYZED';
24
+ /** All nodes in the graph for this file, enhanced with context */
25
+ nodes: EnhancedNode[];
26
+ /** Node counts grouped by type */
27
+ byType: Record<string, number>;
28
+ /** Total number of nodes in the file */
29
+ totalCount: number;
30
+ }
31
+
32
+ /**
33
+ * A node record enhanced with scope context information.
34
+ *
35
+ * The context field provides human-readable information about
36
+ * where the node appears (e.g., "inside try block", "catch parameter").
37
+ */
38
+ export interface EnhancedNode extends BaseNodeRecord {
39
+ /** Human-readable context about the node's scope */
40
+ context?: string;
41
+ }
42
+
43
+ /**
44
+ * Scope patterns to detect from semantic IDs.
45
+ * Order matters - more specific patterns should come first.
46
+ */
47
+ const SCOPE_PATTERNS: Array<{ pattern: RegExp; context: string }> = [
48
+ { pattern: /->catch#\d+->/, context: 'inside catch block' },
49
+ { pattern: /->try#\d+->/, context: 'inside try block' },
50
+ { pattern: /->if#\d+->/, context: 'inside conditional' },
51
+ { pattern: /->else#\d+->/, context: 'inside else block' },
52
+ { pattern: /->for#\d+->/, context: 'inside loop' },
53
+ { pattern: /->while#\d+->/, context: 'inside loop' },
54
+ { pattern: /->switch#\d+->/, context: 'inside switch' },
55
+ ];
56
+
57
+ /**
58
+ * FileExplainer class - explains what nodes exist in a file's graph.
59
+ *
60
+ * Use this when:
61
+ * - User can't find a variable/function they expect to be in the graph
62
+ * - User wants to understand what's been analyzed for a file
63
+ * - User needs semantic IDs to construct queries
64
+ *
65
+ * Example:
66
+ * ```typescript
67
+ * const explainer = new FileExplainer(graphBackend);
68
+ * const result = await explainer.explain('src/app.ts');
69
+ *
70
+ * if (result.status === 'NOT_ANALYZED') {
71
+ * console.log('File not in graph. Run: grafema analyze');
72
+ * } else {
73
+ * for (const node of result.nodes) {
74
+ * console.log(`[${node.type}] ${node.name}`);
75
+ * console.log(` ID: ${node.id}`);
76
+ * if (node.context) {
77
+ * console.log(` Context: ${node.context}`);
78
+ * }
79
+ * }
80
+ * }
81
+ * ```
82
+ */
83
+ export class FileExplainer {
84
+ constructor(private graph: GraphBackend) {}
85
+
86
+ /**
87
+ * Explain what nodes exist in the graph for a file.
88
+ *
89
+ * @param filePath - The file path to explain (relative or absolute)
90
+ * @returns FileExplainResult with all nodes, grouped by type, with context
91
+ */
92
+ async explain(filePath: string): Promise<FileExplainResult> {
93
+ // Query graph for all nodes in this file
94
+ const nodes = await this.getNodesForFile(filePath);
95
+
96
+ // Group by type
97
+ const byType = this.groupByType(nodes);
98
+
99
+ // Enhance with context from semantic ID parsing
100
+ const enhanced = this.enhanceWithContext(nodes);
101
+
102
+ // Sort nodes: by type, then by name
103
+ enhanced.sort((a, b) => {
104
+ const typeCompare = a.type.localeCompare(b.type);
105
+ if (typeCompare !== 0) return typeCompare;
106
+ return (a.name || '').localeCompare(b.name || '');
107
+ });
108
+
109
+ return {
110
+ file: filePath,
111
+ status: nodes.length > 0 ? 'ANALYZED' : 'NOT_ANALYZED',
112
+ nodes: enhanced,
113
+ byType,
114
+ totalCount: nodes.length,
115
+ };
116
+ }
117
+
118
+ /**
119
+ * Query graph for all nodes in a file
120
+ *
121
+ * Note: The server-side file filter may not work correctly in all cases,
122
+ * so we also filter client-side to ensure only nodes from the requested file are returned.
123
+ */
124
+ private async getNodesForFile(filePath: string): Promise<BaseNodeRecord[]> {
125
+ const filter: NodeFilter = { file: filePath };
126
+ const nodes: BaseNodeRecord[] = [];
127
+
128
+ for await (const node of this.graph.queryNodes(filter)) {
129
+ // Client-side filter as backup (server filter may not work correctly)
130
+ if (node.file === filePath) {
131
+ nodes.push(node);
132
+ }
133
+ }
134
+
135
+ return nodes;
136
+ }
137
+
138
+ /**
139
+ * Group nodes by type, counting occurrences
140
+ */
141
+ private groupByType(nodes: BaseNodeRecord[]): Record<string, number> {
142
+ const counts: Record<string, number> = {};
143
+
144
+ for (const node of nodes) {
145
+ const type = node.type || 'UNKNOWN';
146
+ counts[type] = (counts[type] || 0) + 1;
147
+ }
148
+
149
+ return counts;
150
+ }
151
+
152
+ /**
153
+ * Enhance nodes with human-readable scope context.
154
+ *
155
+ * Detects patterns in semantic IDs like:
156
+ * - "file->func->try#0->VARIABLE->x" → "inside try block"
157
+ * - "file->func->catch#0->VARIABLE->error" → "inside catch block"
158
+ */
159
+ private enhanceWithContext(nodes: BaseNodeRecord[]): EnhancedNode[] {
160
+ return nodes.map((node) => {
161
+ const context = this.detectScopeContext(node.id);
162
+ return context ? { ...node, context } : { ...node };
163
+ });
164
+ }
165
+
166
+ /**
167
+ * Detect scope context from semantic ID patterns.
168
+ *
169
+ * Returns human-readable context string or undefined if no special scope.
170
+ */
171
+ private detectScopeContext(semanticId: string): string | undefined {
172
+ for (const { pattern, context } of SCOPE_PATTERNS) {
173
+ if (pattern.test(semanticId)) {
174
+ return context;
175
+ }
176
+ }
177
+ return undefined;
178
+ }
179
+ }