@angular-modernizer/adapter-mcp 0.2.0 → 1.0.0

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 (255) hide show
  1. package/README.md +188 -235
  2. package/dist/cli.js +77 -27
  3. package/dist/index.d.ts +11 -3
  4. package/dist/index.js +10 -2
  5. package/dist/mcp-watcher.d.ts +6 -1
  6. package/dist/mcp-watcher.js +14 -10
  7. package/dist/server/limited-stdio-transport.d.ts +68 -0
  8. package/dist/server/limited-stdio-transport.js +115 -0
  9. package/dist/server/mcp-server.d.ts +41 -12
  10. package/dist/server/mcp-server.js +113 -94
  11. package/dist/server/package-version.d.ts +16 -0
  12. package/dist/server/package-version.js +37 -0
  13. package/dist/server/sdk-server.d.ts +70 -0
  14. package/dist/server/sdk-server.js +113 -0
  15. package/dist/server/stdio-guard.d.ts +20 -0
  16. package/dist/server/stdio-guard.js +48 -0
  17. package/dist/server/tool-registry.d.ts +3 -2
  18. package/dist/server/tool-registry.js +5 -5
  19. package/dist/tools/analyze-build-impact.d.ts +346 -14
  20. package/dist/tools/analyze-build-impact.js +315 -31
  21. package/dist/tools/analyze-core-to-shared.d.ts +134 -4
  22. package/dist/tools/analyze-core-to-shared.js +113 -9
  23. package/dist/tools/analyze-parser-performance.d.ts +200 -7
  24. package/dist/tools/analyze-parser-performance.js +108 -47
  25. package/dist/tools/analyze-project.d.ts +267 -5
  26. package/dist/tools/analyze-project.js +196 -8
  27. package/dist/tools/check-incremental-compatibility.d.ts +61 -1
  28. package/dist/tools/check-incremental-compatibility.js +58 -2
  29. package/dist/tools/collect-feedback.d.ts +199 -10
  30. package/dist/tools/collect-feedback.js +171 -99
  31. package/dist/tools/compare-health.d.ts +356 -0
  32. package/dist/tools/compare-health.js +472 -0
  33. package/dist/tools/extract-libraries.d.ts +156 -4
  34. package/dist/tools/extract-libraries.js +115 -10
  35. package/dist/tools/find-angular-symbol.d.ts +98 -9
  36. package/dist/tools/find-angular-symbol.js +104 -12
  37. package/dist/tools/find-component-hosts.d.ts +684 -0
  38. package/dist/tools/find-component-hosts.js +441 -0
  39. package/dist/tools/find-usages.d.ts +99 -4
  40. package/dist/tools/find-usages.js +131 -11
  41. package/dist/tools/get-bootstrap-execution.d.ts +258 -0
  42. package/dist/tools/get-bootstrap-execution.js +324 -0
  43. package/dist/tools/get-call-graph.d.ts +139 -4
  44. package/dist/tools/get-call-graph.js +123 -13
  45. package/dist/tools/get-operation-progress-tool.d.ts +114 -2
  46. package/dist/tools/get-operation-progress-tool.js +114 -7
  47. package/dist/tools/get-operation-progress.d.ts +1 -1
  48. package/dist/tools/get-operation-progress.js +5 -5
  49. package/dist/tools/helpers/analysis-output.d.ts +177 -0
  50. package/dist/tools/helpers/analysis-output.js +165 -0
  51. package/dist/tools/helpers/build-schemas.d.ts +36 -0
  52. package/dist/tools/helpers/build-schemas.js +23 -0
  53. package/dist/tools/helpers/citation-file-source.d.ts +10 -0
  54. package/dist/tools/helpers/citation-file-source.js +82 -0
  55. package/dist/tools/helpers/compact-output.d.ts +61 -0
  56. package/dist/tools/helpers/compact-output.js +83 -0
  57. package/dist/tools/helpers/finding-window.d.ts +43 -0
  58. package/dist/tools/helpers/finding-window.js +64 -0
  59. package/dist/tools/helpers/health-delta.d.ts +86 -0
  60. package/dist/tools/helpers/health-delta.js +239 -0
  61. package/dist/tools/helpers/investigation-schemas.d.ts +93 -0
  62. package/dist/tools/helpers/investigation-schemas.js +48 -0
  63. package/dist/tools/helpers/library-imports.d.ts +57 -0
  64. package/dist/tools/helpers/library-imports.js +151 -0
  65. package/dist/tools/helpers/load-source-files.d.ts +30 -0
  66. package/dist/tools/helpers/load-source-files.js +34 -0
  67. package/dist/tools/helpers/output-contract.d.ts +32 -0
  68. package/dist/tools/helpers/output-contract.js +32 -0
  69. package/dist/tools/helpers/platform-root-resolver.d.ts +55 -25
  70. package/dist/tools/helpers/platform-root-resolver.js +75 -48
  71. package/dist/tools/helpers/process-output.d.ts +19 -0
  72. package/dist/tools/helpers/process-output.js +31 -0
  73. package/dist/tools/helpers/project-cache.d.ts +184 -0
  74. package/dist/tools/helpers/project-cache.js +331 -0
  75. package/dist/tools/helpers/project-decorators.d.ts +14 -0
  76. package/dist/tools/helpers/project-decorators.js +22 -0
  77. package/dist/tools/helpers/rule-matcher.js +5 -0
  78. package/dist/tools/helpers/rule-runner.d.ts +72 -0
  79. package/dist/tools/helpers/rule-runner.js +131 -0
  80. package/dist/tools/helpers/scan-cache.d.ts +22 -0
  81. package/dist/tools/helpers/scan-cache.js +34 -0
  82. package/dist/tools/helpers/scan-plan.d.ts +48 -0
  83. package/dist/tools/helpers/scan-plan.js +166 -0
  84. package/dist/tools/helpers/scope-paths.d.ts +17 -0
  85. package/dist/tools/helpers/scope-paths.js +32 -0
  86. package/dist/tools/helpers/stack-frame-file-index.d.ts +10 -0
  87. package/dist/tools/helpers/stack-frame-file-index.js +71 -0
  88. package/dist/tools/helpers/tool-context.d.ts +60 -0
  89. package/dist/tools/helpers/tool-context.js +82 -0
  90. package/dist/tools/helpers/transform-safety.d.ts +40 -0
  91. package/dist/tools/helpers/transform-safety.js +87 -0
  92. package/dist/tools/helpers/validation-output.d.ts +53 -0
  93. package/dist/tools/helpers/validation-output.js +48 -0
  94. package/dist/tools/inspect-form-group.d.ts +324 -0
  95. package/dist/tools/inspect-form-group.js +279 -0
  96. package/dist/tools/inspect-i18n.d.ts +466 -0
  97. package/dist/tools/inspect-i18n.js +400 -0
  98. package/dist/tools/inspect-template.d.ts +320 -0
  99. package/dist/tools/inspect-template.js +324 -0
  100. package/dist/tools/learn-patterns.d.ts +122 -1
  101. package/dist/tools/learn-patterns.js +97 -2
  102. package/dist/tools/list-imports.d.ts +128 -11
  103. package/dist/tools/list-imports.js +128 -8
  104. package/dist/tools/orchestrate-migration.d.ts +129 -5
  105. package/dist/tools/orchestrate-migration.js +166 -12
  106. package/dist/tools/parse-stack-trace.d.ts +59 -2
  107. package/dist/tools/parse-stack-trace.js +52 -4
  108. package/dist/tools/plan-migration.d.ts +418 -1
  109. package/dist/tools/plan-migration.js +355 -8
  110. package/dist/tools/resolve-type.d.ts +42 -1
  111. package/dist/tools/resolve-type.js +37 -4
  112. package/dist/tools/review-feedback.d.ts +206 -11
  113. package/dist/tools/review-feedback.js +132 -51
  114. package/dist/tools/rollback-changes.d.ts +79 -2
  115. package/dist/tools/rollback-changes.js +66 -14
  116. package/dist/tools/run-angular-cli.d.ts +45 -2
  117. package/dist/tools/run-angular-cli.js +67 -7
  118. package/dist/tools/run-build.d.ts +114 -2
  119. package/dist/tools/run-build.js +117 -8
  120. package/dist/tools/run-schematic.d.ts +70 -2
  121. package/dist/tools/run-schematic.js +104 -10
  122. package/dist/tools/scan-all.d.ts +24 -16
  123. package/dist/tools/scan-all.js +145 -137
  124. package/dist/tools/scan-angular.d.ts +20 -11
  125. package/dist/tools/scan-angular.js +52 -24
  126. package/dist/tools/scan-architecture.d.ts +42 -14
  127. package/dist/tools/scan-architecture.js +206 -163
  128. package/dist/tools/scan-solid.d.ts +20 -11
  129. package/dist/tools/scan-solid.js +52 -24
  130. package/dist/tools/search-codebase.d.ts +85 -1
  131. package/dist/tools/search-codebase.js +61 -4
  132. package/dist/tools/server-status.d.ts +370 -0
  133. package/dist/tools/server-status.js +369 -0
  134. package/dist/tools/suggest-improvements.d.ts +97 -5
  135. package/dist/tools/suggest-improvements.js +80 -44
  136. package/dist/tools/trace-output-flow.d.ts +791 -0
  137. package/dist/tools/trace-output-flow.js +668 -0
  138. package/dist/tools/transform-code-batch.d.ts +3 -0
  139. package/dist/tools/transform-code-batch.js +10 -5
  140. package/dist/tools/transform-code.d.ts +335 -12
  141. package/dist/tools/transform-code.js +649 -608
  142. package/dist/tools/validate-before-transform.d.ts +116 -2
  143. package/dist/tools/validate-before-transform.js +55 -2
  144. package/dist/tools/validate-project-compatibility.d.ts +64 -2
  145. package/dist/tools/validate-project-compatibility.js +67 -4
  146. package/dist/tools/validate-transformation.d.ts +220 -2
  147. package/dist/tools/validate-transformation.js +113 -15
  148. package/dist/tools/verify-citations.d.ts +181 -0
  149. package/dist/tools/verify-citations.js +218 -0
  150. package/dist/types.d.ts +45 -1
  151. package/dist/utils/format-tool-response.d.ts +22 -59
  152. package/dist/utils/format-tool-response.js +25 -134
  153. package/package.json +30 -13
  154. package/dist/cli.d.ts.map +0 -1
  155. package/dist/cli.js.map +0 -1
  156. package/dist/core/project-analyzer.d.ts.map +0 -1
  157. package/dist/core/project-analyzer.js.map +0 -1
  158. package/dist/index.d.ts.map +0 -1
  159. package/dist/index.js.map +0 -1
  160. package/dist/mcp-watcher.d.ts.map +0 -1
  161. package/dist/mcp-watcher.js.map +0 -1
  162. package/dist/server/json-rpc-handler.d.ts +0 -57
  163. package/dist/server/json-rpc-handler.d.ts.map +0 -1
  164. package/dist/server/json-rpc-handler.js +0 -222
  165. package/dist/server/json-rpc-handler.js.map +0 -1
  166. package/dist/server/mcp-server.d.ts.map +0 -1
  167. package/dist/server/mcp-server.js.map +0 -1
  168. package/dist/server/tool-registry.d.ts.map +0 -1
  169. package/dist/server/tool-registry.js.map +0 -1
  170. package/dist/streaming/progress-store.d.ts.map +0 -1
  171. package/dist/streaming/progress-store.js.map +0 -1
  172. package/dist/tools/analyze-build-impact.d.ts.map +0 -1
  173. package/dist/tools/analyze-build-impact.js.map +0 -1
  174. package/dist/tools/analyze-core-to-shared.d.ts.map +0 -1
  175. package/dist/tools/analyze-core-to-shared.js.map +0 -1
  176. package/dist/tools/analyze-parser-performance.d.ts.map +0 -1
  177. package/dist/tools/analyze-parser-performance.js.map +0 -1
  178. package/dist/tools/analyze-project.d.ts.map +0 -1
  179. package/dist/tools/analyze-project.js.map +0 -1
  180. package/dist/tools/check-incremental-compatibility.d.ts.map +0 -1
  181. package/dist/tools/check-incremental-compatibility.js.map +0 -1
  182. package/dist/tools/collect-feedback.d.ts.map +0 -1
  183. package/dist/tools/collect-feedback.js.map +0 -1
  184. package/dist/tools/extract-libraries.d.ts.map +0 -1
  185. package/dist/tools/extract-libraries.js.map +0 -1
  186. package/dist/tools/find-angular-symbol.d.ts.map +0 -1
  187. package/dist/tools/find-angular-symbol.js.map +0 -1
  188. package/dist/tools/find-usages.d.ts.map +0 -1
  189. package/dist/tools/find-usages.js.map +0 -1
  190. package/dist/tools/get-call-graph.d.ts.map +0 -1
  191. package/dist/tools/get-call-graph.js.map +0 -1
  192. package/dist/tools/get-operation-progress-tool.d.ts.map +0 -1
  193. package/dist/tools/get-operation-progress-tool.js.map +0 -1
  194. package/dist/tools/get-operation-progress.d.ts.map +0 -1
  195. package/dist/tools/get-operation-progress.js.map +0 -1
  196. package/dist/tools/helpers/feedback-analyzer.d.ts.map +0 -1
  197. package/dist/tools/helpers/feedback-analyzer.js.map +0 -1
  198. package/dist/tools/helpers/find-ts-config.d.ts.map +0 -1
  199. package/dist/tools/helpers/find-ts-config.js.map +0 -1
  200. package/dist/tools/helpers/platform-root-resolver.d.ts.map +0 -1
  201. package/dist/tools/helpers/platform-root-resolver.js.map +0 -1
  202. package/dist/tools/helpers/rule-matcher.d.ts.map +0 -1
  203. package/dist/tools/helpers/rule-matcher.js.map +0 -1
  204. package/dist/tools/helpers/suggestion-generator.d.ts.map +0 -1
  205. package/dist/tools/helpers/suggestion-generator.js.map +0 -1
  206. package/dist/tools/learn-patterns.d.ts.map +0 -1
  207. package/dist/tools/learn-patterns.js.map +0 -1
  208. package/dist/tools/list-imports.d.ts.map +0 -1
  209. package/dist/tools/list-imports.js.map +0 -1
  210. package/dist/tools/orchestrate-migration.d.ts.map +0 -1
  211. package/dist/tools/orchestrate-migration.js.map +0 -1
  212. package/dist/tools/parse-stack-trace.d.ts.map +0 -1
  213. package/dist/tools/parse-stack-trace.js.map +0 -1
  214. package/dist/tools/plan-migration.d.ts.map +0 -1
  215. package/dist/tools/plan-migration.js.map +0 -1
  216. package/dist/tools/resolve-type.d.ts.map +0 -1
  217. package/dist/tools/resolve-type.js.map +0 -1
  218. package/dist/tools/review-feedback.d.ts.map +0 -1
  219. package/dist/tools/review-feedback.js.map +0 -1
  220. package/dist/tools/rollback-changes.d.ts.map +0 -1
  221. package/dist/tools/rollback-changes.js.map +0 -1
  222. package/dist/tools/run-angular-cli.d.ts.map +0 -1
  223. package/dist/tools/run-angular-cli.js.map +0 -1
  224. package/dist/tools/run-build.d.ts.map +0 -1
  225. package/dist/tools/run-build.js.map +0 -1
  226. package/dist/tools/run-schematic.d.ts.map +0 -1
  227. package/dist/tools/run-schematic.js.map +0 -1
  228. package/dist/tools/scan-all.d.ts.map +0 -1
  229. package/dist/tools/scan-all.js.map +0 -1
  230. package/dist/tools/scan-angular.d.ts.map +0 -1
  231. package/dist/tools/scan-angular.js.map +0 -1
  232. package/dist/tools/scan-architecture.d.ts.map +0 -1
  233. package/dist/tools/scan-architecture.js.map +0 -1
  234. package/dist/tools/scan-solid.d.ts.map +0 -1
  235. package/dist/tools/scan-solid.js.map +0 -1
  236. package/dist/tools/search-codebase.d.ts.map +0 -1
  237. package/dist/tools/search-codebase.js.map +0 -1
  238. package/dist/tools/shared/scan-types.d.ts.map +0 -1
  239. package/dist/tools/shared/scan-types.js.map +0 -1
  240. package/dist/tools/suggest-improvements.d.ts.map +0 -1
  241. package/dist/tools/suggest-improvements.js.map +0 -1
  242. package/dist/tools/transform-code-batch.d.ts.map +0 -1
  243. package/dist/tools/transform-code-batch.js.map +0 -1
  244. package/dist/tools/transform-code.d.ts.map +0 -1
  245. package/dist/tools/transform-code.js.map +0 -1
  246. package/dist/tools/validate-before-transform.d.ts.map +0 -1
  247. package/dist/tools/validate-before-transform.js.map +0 -1
  248. package/dist/tools/validate-project-compatibility.d.ts.map +0 -1
  249. package/dist/tools/validate-project-compatibility.js.map +0 -1
  250. package/dist/tools/validate-transformation.d.ts.map +0 -1
  251. package/dist/tools/validate-transformation.js.map +0 -1
  252. package/dist/types.d.ts.map +0 -1
  253. package/dist/types.js.map +0 -1
  254. package/dist/utils/format-tool-response.d.ts.map +0 -1
  255. package/dist/utils/format-tool-response.js.map +0 -1
@@ -0,0 +1,184 @@
1
+ /**
2
+ * @angular-modernizer/adapter-mcp - Project Cache
3
+ *
4
+ * Keeps ts-morph `Project` instances alive across tool calls, keyed by the
5
+ * tsconfig path, so repeated investigation calls on the same workspace do not
6
+ * re-parse the whole program each time (seconds on a large Angular app).
7
+ *
8
+ * @remarks
9
+ * Same idea as `SharedProjectManager` in core (one long-lived Project instead
10
+ * of one per call), but keyed per tsconfig because the MCP server can serve
11
+ * several workspaces in one session. An entry is rebuilt when the tsconfig
12
+ * mtime changes. Source files are kept current file by file: the mtime of
13
+ * every program file is recorded at load and compared on each `get()` (about
14
+ * 6 ms for 3939 files); changed files are re-read, deleted ones
15
+ * removed. The type checker then rebuilds lazily on the next query. Files
16
+ * that were not in the program at load (new files) need a tsconfig touch or a
17
+ * server restart.
18
+ *
19
+ * Two limits bound the cache: `maxEntries` projects, and `maxHeapMb` of V8
20
+ * heap, checked on every `get()` because a project keeps growing after the
21
+ * load as the type checker resolves more of it, and on a miss also before
22
+ * the load. Over either limit the least recently used project is evicted;
23
+ * the project being requested never is.
24
+ * The heap is the signal, not the RSS: dropping a Project frees its heap
25
+ * (the TypeScript program, checker and ts-morph wrappers hang off it and
26
+ * nothing else keeps them), but V8 does not reliably unmap the pages, so the
27
+ * RSS can stay at its peak after an eviction and an RSS limit would then
28
+ * evict everything on every call. Freed pages are reused by the next load.
29
+ * When node runs with `--expose-gc` the cache collects once the raw heap
30
+ * reading passes the limit and after each eviction, so garbage does not count
31
+ * against the cache; otherwise it subtracts the heap growth recorded when the
32
+ * evicted project was loaded, and garbage makes it evict earlier.
33
+ */
34
+ import { Project } from 'ts-morph';
35
+ /** A cached project as reported by `server-status`. */
36
+ export interface CachedProjectInfo {
37
+ tsConfigPath: string;
38
+ sourceFiles: number;
39
+ loadedAt: string;
40
+ lastUsedAt: string;
41
+ /** Heap growth while the project was loaded, an estimate of its size. */
42
+ loadHeapMB: number;
43
+ /** Source files re-read or removed because they changed on disk. */
44
+ refreshedFiles: number;
45
+ lastRefreshAt?: string;
46
+ }
47
+ /** Why a project left the cache. */
48
+ export type EvictionReason = 'max-entries' | 'max-heap';
49
+ /** One eviction with the memory around it. */
50
+ export interface ProjectEviction {
51
+ tsConfigPath: string;
52
+ reason: EvictionReason;
53
+ at: string;
54
+ sourceFiles: number;
55
+ heapMB: {
56
+ before: number;
57
+ after: number;
58
+ };
59
+ rssMB: {
60
+ before: number;
61
+ after: number;
62
+ };
63
+ }
64
+ /** Evictions since the server started. */
65
+ export interface EvictionStats {
66
+ count: number;
67
+ last?: ProjectEviction;
68
+ }
69
+ /** Heap and RSS reader, plus a full collection when node exposes one. */
70
+ export interface MemoryProbe {
71
+ usage(): {
72
+ heapUsed: number;
73
+ rss: number;
74
+ };
75
+ collect?: () => void;
76
+ }
77
+ /** Limits of a project cache. */
78
+ export interface ProjectCacheLimits {
79
+ /** Most projects kept at once, at least 1. */
80
+ maxEntries: number;
81
+ /** Heap limit in MB checked after each load; 0 switches it off. */
82
+ maxHeapMb: number;
83
+ }
84
+ export interface ProjectCacheOptions extends Partial<ProjectCacheLimits> {
85
+ createProject?: (tsConfigFilePath: string) => Project;
86
+ memory?: MemoryProbe;
87
+ /** mtime of a file in ms, undefined when it cannot be read. */
88
+ statFile?: (filePath: string) => number | undefined;
89
+ }
90
+ /** Default number of workspaces kept in memory at once. */
91
+ export declare const DEFAULT_MAX_ENTRIES = 3;
92
+ /**
93
+ * Default heap limit. A workspace of 3939 files takes 1.0 to 1.3 GB of heap
94
+ * once loaded and queried; V8's heap limit on the dev machines is about 4.1 GB.
95
+ * 2048 MB keeps such a workspace together with small ones, and evicts it before a
96
+ * second workspace of its size drives the process towards the V8 limit.
97
+ */
98
+ export declare const DEFAULT_MAX_HEAP_MB = 2048;
99
+ /** Bytes to MB, one decimal. */
100
+ export declare function toMB(bytes: number): number;
101
+ /** LRU cache of ts-morph Projects keyed by tsconfig path. */
102
+ export declare class ProjectCache {
103
+ private readonly entries;
104
+ private readonly maxEntries;
105
+ private readonly maxHeapMb;
106
+ private readonly createProject;
107
+ private readonly memory;
108
+ private readonly statFile;
109
+ private evictionCount;
110
+ private lastEviction;
111
+ /**
112
+ * @param options - Limits, project factory and memory probe; tests inject
113
+ * the last two.
114
+ */
115
+ constructor(options?: ProjectCacheOptions);
116
+ /** Returns the cached Project for `tsConfigPath`, building it on a miss. */
117
+ get(tsConfigPath: string): Project;
118
+ /** Number of cached projects. */
119
+ get size(): number;
120
+ /** Most projects kept at once. */
121
+ get capacity(): number;
122
+ /** Limits in effect. */
123
+ get limits(): ProjectCacheLimits;
124
+ /** Eviction count and the most recent eviction. */
125
+ get evictions(): EvictionStats;
126
+ /**
127
+ * Warnings about the loaded project for `tsConfigPath`: today the library
128
+ * imports that do not resolve (missing or incomplete node_modules). Checked
129
+ * once per load, on the first call, and kept until the project is reloaded;
130
+ * empty when the project is not cached.
131
+ */
132
+ warnings(tsConfigPath: string): string[];
133
+ /** Cached projects, least recently used first. */
134
+ describe(): CachedProjectInfo[];
135
+ /**
136
+ * Moves a fresh entry to the most recently used end and returns its
137
+ * project; drops a stale one. Kept apart from `get()` so no reference to a
138
+ * dropped entry is alive while `evictOverHeap` collects.
139
+ */
140
+ private touch;
141
+ /** Re-reads program files whose mtime changed and removes deleted ones. */
142
+ private refreshChangedFiles;
143
+ private recordMtimes;
144
+ private load;
145
+ private evictOverCount;
146
+ /**
147
+ * Evicts least recently used projects, never `current`, until the heap is
148
+ * back under `maxHeapMb`. Under the limit this is one `memoryUsage()` call;
149
+ * over it, a collection runs first so garbage does not count.
150
+ */
151
+ private evictOverHeap;
152
+ /** Drops `key`, records the eviction, returns the heap estimate after it. */
153
+ private evict;
154
+ /**
155
+ * Deletes `key` and returns the figures the eviction record needs. A
156
+ * separate frame, so the entry is unreachable once it returns and the
157
+ * following collection can free the project.
158
+ */
159
+ private remove;
160
+ }
161
+ /** Command-line flags and environment variables for the cache limits. */
162
+ export declare const PROJECT_CACHE_FLAGS: {
163
+ readonly maxEntries: {
164
+ readonly flag: "--max-projects";
165
+ readonly env: "ANGULAR_MODERNIZER_MAX_PROJECTS";
166
+ };
167
+ readonly maxHeapMb: {
168
+ readonly flag: "--max-heap-mb";
169
+ readonly env: "ANGULAR_MODERNIZER_MAX_HEAP_MB";
170
+ };
171
+ };
172
+ /** Limits read from argv and env, plus warnings for rejected values. */
173
+ export interface ParsedProjectCacheLimits {
174
+ options: Partial<ProjectCacheLimits>;
175
+ warnings: string[];
176
+ }
177
+ /**
178
+ * Reads the cache limits from `--max-projects` / `--max-heap-mb` (either
179
+ * `--flag=N` or `--flag N`) or their environment variables; the flag wins.
180
+ * `--max-heap-mb 0` switches the heap limit off. Invalid values are dropped
181
+ * with a warning so a typo does not stop the server.
182
+ */
183
+ export declare function parseProjectCacheLimits(argv: readonly string[], env: Readonly<Record<string, string | undefined>>): ParsedProjectCacheLimits;
184
+ //# sourceMappingURL=project-cache.d.ts.map
@@ -0,0 +1,331 @@
1
+ /**
2
+ * @angular-modernizer/adapter-mcp - Project Cache
3
+ *
4
+ * Keeps ts-morph `Project` instances alive across tool calls, keyed by the
5
+ * tsconfig path, so repeated investigation calls on the same workspace do not
6
+ * re-parse the whole program each time (seconds on a large Angular app).
7
+ *
8
+ * @remarks
9
+ * Same idea as `SharedProjectManager` in core (one long-lived Project instead
10
+ * of one per call), but keyed per tsconfig because the MCP server can serve
11
+ * several workspaces in one session. An entry is rebuilt when the tsconfig
12
+ * mtime changes. Source files are kept current file by file: the mtime of
13
+ * every program file is recorded at load and compared on each `get()` (about
14
+ * 6 ms for 3939 files); changed files are re-read, deleted ones
15
+ * removed. The type checker then rebuilds lazily on the next query. Files
16
+ * that were not in the program at load (new files) need a tsconfig touch or a
17
+ * server restart.
18
+ *
19
+ * Two limits bound the cache: `maxEntries` projects, and `maxHeapMb` of V8
20
+ * heap, checked on every `get()` because a project keeps growing after the
21
+ * load as the type checker resolves more of it, and on a miss also before
22
+ * the load. Over either limit the least recently used project is evicted;
23
+ * the project being requested never is.
24
+ * The heap is the signal, not the RSS: dropping a Project frees its heap
25
+ * (the TypeScript program, checker and ts-morph wrappers hang off it and
26
+ * nothing else keeps them), but V8 does not reliably unmap the pages, so the
27
+ * RSS can stay at its peak after an eviction and an RSS limit would then
28
+ * evict everything on every call. Freed pages are reused by the next load.
29
+ * When node runs with `--expose-gc` the cache collects once the raw heap
30
+ * reading passes the limit and after each eviction, so garbage does not count
31
+ * against the cache; otherwise it subtracts the heap growth recorded when the
32
+ * evicted project was loaded, and garbage makes it evict earlier.
33
+ */
34
+ import { statSync } from 'node:fs';
35
+ import { Project } from 'ts-morph';
36
+ import { findUnresolvedLibraryImports, libraryImportWarning, } from './library-imports.js';
37
+ /** Default number of workspaces kept in memory at once. */
38
+ export const DEFAULT_MAX_ENTRIES = 3;
39
+ /**
40
+ * Default heap limit. A workspace of 3939 files takes 1.0 to 1.3 GB of heap
41
+ * once loaded and queried; V8's heap limit on the dev machines is about 4.1 GB.
42
+ * 2048 MB keeps such a workspace together with small ones, and evicts it before a
43
+ * second workspace of its size drives the process towards the V8 limit.
44
+ */
45
+ export const DEFAULT_MAX_HEAP_MB = 2048;
46
+ const BYTES_PER_MB = 1024 * 1024;
47
+ /** Bytes to MB, one decimal. */
48
+ export function toMB(bytes) {
49
+ return Math.round((bytes / BYTES_PER_MB) * 10) / 10;
50
+ }
51
+ function fileMtime(filePath) {
52
+ try {
53
+ return statSync(filePath).mtimeMs;
54
+ }
55
+ catch {
56
+ return undefined;
57
+ }
58
+ }
59
+ const processMemory = {
60
+ usage: () => process.memoryUsage(),
61
+ ...(typeof globalThis.gc === 'function'
62
+ ? { collect: globalThis.gc.bind(globalThis) }
63
+ : {}),
64
+ };
65
+ /** LRU cache of ts-morph Projects keyed by tsconfig path. */
66
+ export class ProjectCache {
67
+ entries = new Map();
68
+ maxEntries;
69
+ maxHeapMb;
70
+ createProject;
71
+ memory;
72
+ statFile;
73
+ evictionCount = 0;
74
+ lastEviction;
75
+ /**
76
+ * @param options - Limits, project factory and memory probe; tests inject
77
+ * the last two.
78
+ */
79
+ constructor(options = {}) {
80
+ this.maxEntries = Math.max(1, options.maxEntries ?? DEFAULT_MAX_ENTRIES);
81
+ this.maxHeapMb = Math.max(0, options.maxHeapMb ?? DEFAULT_MAX_HEAP_MB);
82
+ this.createProject =
83
+ options.createProject ??
84
+ ((tsConfigFilePath) => new Project({ tsConfigFilePath }));
85
+ this.memory = options.memory ?? processMemory;
86
+ this.statFile = options.statFile ?? fileMtime;
87
+ }
88
+ /** Returns the cached Project for `tsConfigPath`, building it on a miss. */
89
+ get(tsConfigPath) {
90
+ const mtimeMs = statSync(tsConfigPath).mtimeMs;
91
+ let project = this.touch(tsConfigPath, mtimeMs);
92
+ if (project === undefined) {
93
+ // Make room first: a load on top of a heap already over the limit
94
+ // would stack two large programs before the check below evicts one.
95
+ this.evictOverHeap(tsConfigPath);
96
+ project = this.load(tsConfigPath, mtimeMs);
97
+ }
98
+ this.evictOverCount();
99
+ this.evictOverHeap(tsConfigPath);
100
+ return project;
101
+ }
102
+ /** Number of cached projects. */
103
+ get size() {
104
+ return this.entries.size;
105
+ }
106
+ /** Most projects kept at once. */
107
+ get capacity() {
108
+ return this.maxEntries;
109
+ }
110
+ /** Limits in effect. */
111
+ get limits() {
112
+ return { maxEntries: this.maxEntries, maxHeapMb: this.maxHeapMb };
113
+ }
114
+ /** Eviction count and the most recent eviction. */
115
+ get evictions() {
116
+ return this.lastEviction === undefined
117
+ ? { count: this.evictionCount }
118
+ : { count: this.evictionCount, last: this.lastEviction };
119
+ }
120
+ /**
121
+ * Warnings about the loaded project for `tsConfigPath`: today the library
122
+ * imports that do not resolve (missing or incomplete node_modules). Checked
123
+ * once per load, on the first call, and kept until the project is reloaded;
124
+ * empty when the project is not cached.
125
+ */
126
+ warnings(tsConfigPath) {
127
+ const entry = this.entries.get(tsConfigPath);
128
+ if (entry === undefined) {
129
+ return [];
130
+ }
131
+ if (entry.warnings === undefined) {
132
+ const warning = libraryImportWarning(findUnresolvedLibraryImports(entry.project, tsConfigPath));
133
+ entry.warnings = warning === undefined ? [] : [warning];
134
+ }
135
+ return [...entry.warnings];
136
+ }
137
+ /** Cached projects, least recently used first. */
138
+ describe() {
139
+ return [...this.entries].map(([tsConfigPath, entry]) => ({
140
+ tsConfigPath,
141
+ sourceFiles: entry.project.getSourceFiles().length,
142
+ loadedAt: entry.loadedAt.toISOString(),
143
+ lastUsedAt: entry.lastUsedAt.toISOString(),
144
+ loadHeapMB: toMB(entry.loadHeapBytes),
145
+ refreshedFiles: entry.refreshedFiles,
146
+ ...(entry.lastRefreshAt === undefined
147
+ ? {}
148
+ : { lastRefreshAt: entry.lastRefreshAt.toISOString() }),
149
+ }));
150
+ }
151
+ /**
152
+ * Moves a fresh entry to the most recently used end and returns its
153
+ * project; drops a stale one. Kept apart from `get()` so no reference to a
154
+ * dropped entry is alive while `evictOverHeap` collects.
155
+ */
156
+ touch(tsConfigPath, mtimeMs) {
157
+ const cached = this.entries.get(tsConfigPath);
158
+ this.entries.delete(tsConfigPath);
159
+ if (cached?.mtimeMs !== mtimeMs) {
160
+ return undefined;
161
+ }
162
+ cached.lastUsedAt = new Date();
163
+ this.entries.set(tsConfigPath, cached);
164
+ this.refreshChangedFiles(cached);
165
+ return cached.project;
166
+ }
167
+ /** Re-reads program files whose mtime changed and removes deleted ones. */
168
+ refreshChangedFiles(entry) {
169
+ let changed = 0;
170
+ for (const [filePath, mtimeMs] of entry.fileMtimes) {
171
+ const now = this.statFile(filePath);
172
+ if (now === mtimeMs) {
173
+ continue;
174
+ }
175
+ changed++;
176
+ const sourceFile = entry.project.getSourceFile(filePath);
177
+ if (now === undefined) {
178
+ entry.fileMtimes.delete(filePath);
179
+ if (sourceFile !== undefined) {
180
+ entry.project.removeSourceFile(sourceFile);
181
+ }
182
+ continue;
183
+ }
184
+ entry.fileMtimes.set(filePath, now);
185
+ sourceFile?.refreshFromFileSystemSync();
186
+ }
187
+ if (changed > 0) {
188
+ entry.refreshedFiles += changed;
189
+ entry.lastRefreshAt = new Date();
190
+ }
191
+ }
192
+ recordMtimes(project) {
193
+ const mtimes = new Map();
194
+ for (const sourceFile of project.getSourceFiles()) {
195
+ const filePath = sourceFile.getFilePath();
196
+ const mtimeMs = this.statFile(filePath);
197
+ if (mtimeMs !== undefined) {
198
+ mtimes.set(filePath, mtimeMs);
199
+ }
200
+ }
201
+ return mtimes;
202
+ }
203
+ load(tsConfigPath, mtimeMs) {
204
+ const heapBefore = this.memory.usage().heapUsed;
205
+ const project = this.createProject(tsConfigPath);
206
+ const now = new Date();
207
+ this.entries.set(tsConfigPath, {
208
+ project,
209
+ mtimeMs,
210
+ loadedAt: now,
211
+ lastUsedAt: now,
212
+ loadHeapBytes: Math.max(0, this.memory.usage().heapUsed - heapBefore),
213
+ fileMtimes: this.recordMtimes(project),
214
+ refreshedFiles: 0,
215
+ });
216
+ return project;
217
+ }
218
+ evictOverCount() {
219
+ for (const key of this.entries.keys()) {
220
+ if (this.entries.size <= this.maxEntries) {
221
+ return;
222
+ }
223
+ this.evict(key, 'max-entries');
224
+ }
225
+ }
226
+ /**
227
+ * Evicts least recently used projects, never `current`, until the heap is
228
+ * back under `maxHeapMb`. Under the limit this is one `memoryUsage()` call;
229
+ * over it, a collection runs first so garbage does not count.
230
+ */
231
+ evictOverHeap(current) {
232
+ if (this.maxHeapMb === 0) {
233
+ return;
234
+ }
235
+ const limit = this.maxHeapMb * BYTES_PER_MB;
236
+ let heapUsed = this.memory.usage().heapUsed;
237
+ if (heapUsed > limit && this.memory.collect !== undefined) {
238
+ this.memory.collect();
239
+ heapUsed = this.memory.usage().heapUsed;
240
+ }
241
+ for (const key of this.entries.keys()) {
242
+ if (heapUsed <= limit) {
243
+ return;
244
+ }
245
+ if (key !== current) {
246
+ heapUsed = this.evict(key, 'max-heap', heapUsed);
247
+ }
248
+ }
249
+ }
250
+ /** Drops `key`, records the eviction, returns the heap estimate after it. */
251
+ evict(key, reason, heapUsed) {
252
+ const before = this.memory.usage();
253
+ const { sourceFiles, loadHeapBytes } = this.remove(key);
254
+ this.memory.collect?.();
255
+ const after = this.memory.usage();
256
+ const heapAfter = this.memory.collect === undefined
257
+ ? (heapUsed ?? before.heapUsed) - loadHeapBytes
258
+ : after.heapUsed;
259
+ this.evictionCount++;
260
+ this.lastEviction = {
261
+ tsConfigPath: key,
262
+ reason,
263
+ at: new Date().toISOString(),
264
+ sourceFiles,
265
+ heapMB: { before: toMB(before.heapUsed), after: toMB(after.heapUsed) },
266
+ rssMB: { before: toMB(before.rss), after: toMB(after.rss) },
267
+ };
268
+ return heapAfter;
269
+ }
270
+ /**
271
+ * Deletes `key` and returns the figures the eviction record needs. A
272
+ * separate frame, so the entry is unreachable once it returns and the
273
+ * following collection can free the project.
274
+ */
275
+ remove(key) {
276
+ const entry = this.entries.get(key);
277
+ this.entries.delete(key);
278
+ return {
279
+ sourceFiles: entry?.project.getSourceFiles().length ?? 0,
280
+ loadHeapBytes: entry?.loadHeapBytes ?? 0,
281
+ };
282
+ }
283
+ }
284
+ /** Command-line flags and environment variables for the cache limits. */
285
+ export const PROJECT_CACHE_FLAGS = {
286
+ maxEntries: { flag: '--max-projects', env: 'ANGULAR_MODERNIZER_MAX_PROJECTS' },
287
+ maxHeapMb: { flag: '--max-heap-mb', env: 'ANGULAR_MODERNIZER_MAX_HEAP_MB' },
288
+ };
289
+ function readSetting(argv, env, names) {
290
+ const prefix = `${names.flag}=`;
291
+ const index = argv.findIndex((a) => a === names.flag || a.startsWith(prefix));
292
+ if (index >= 0) {
293
+ const arg = argv[index] ?? '';
294
+ const value = arg === names.flag ? argv[index + 1] : arg.slice(prefix.length);
295
+ return { value: value ?? '', source: names.flag };
296
+ }
297
+ const fromEnv = env[names.env];
298
+ return fromEnv === undefined ? undefined : { value: fromEnv, source: names.env };
299
+ }
300
+ /**
301
+ * Reads the cache limits from `--max-projects` / `--max-heap-mb` (either
302
+ * `--flag=N` or `--flag N`) or their environment variables; the flag wins.
303
+ * `--max-heap-mb 0` switches the heap limit off. Invalid values are dropped
304
+ * with a warning so a typo does not stop the server.
305
+ */
306
+ export function parseProjectCacheLimits(argv, env) {
307
+ const options = {};
308
+ const warnings = [];
309
+ const entries = readSetting(argv, env, PROJECT_CACHE_FLAGS.maxEntries);
310
+ if (entries !== undefined) {
311
+ const n = Number(entries.value);
312
+ if (Number.isInteger(n) && n >= 1) {
313
+ options.maxEntries = n;
314
+ }
315
+ else {
316
+ warnings.push(`${entries.source}: expected an integer >= 1, got "${entries.value}"; using ${DEFAULT_MAX_ENTRIES}.`);
317
+ }
318
+ }
319
+ const heap = readSetting(argv, env, PROJECT_CACHE_FLAGS.maxHeapMb);
320
+ if (heap !== undefined) {
321
+ const n = Number(heap.value);
322
+ if (heap.value.trim() !== '' && Number.isFinite(n) && n >= 0) {
323
+ options.maxHeapMb = n;
324
+ }
325
+ else {
326
+ warnings.push(`${heap.source}: expected a number of MB >= 0, got "${heap.value}"; using ${DEFAULT_MAX_HEAP_MB}.`);
327
+ }
328
+ }
329
+ return { options, warnings };
330
+ }
331
+ //# sourceMappingURL=project-cache.js.map
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Project component decorators for the investigation tools.
3
+ *
4
+ * Reads `parser.componentDecorators` from `.angular-modernizer.json` in the
5
+ * project root. The list adds to `Component`; the api classes treat a missing
6
+ * and an empty list the same (Component only).
7
+ */
8
+ /**
9
+ * `parser.componentDecorators` of the project, or `undefined` when the project
10
+ * has no config file or the config does not load. A broken config must not
11
+ * break investigation; the tools then fall back to `Component` only.
12
+ */
13
+ export declare function configuredComponentDecorators(projectRoot: string): readonly string[] | undefined;
14
+ //# sourceMappingURL=project-decorators.d.ts.map
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Project component decorators for the investigation tools.
3
+ *
4
+ * Reads `parser.componentDecorators` from `.angular-modernizer.json` in the
5
+ * project root. The list adds to `Component`; the api classes treat a missing
6
+ * and an empty list the same (Component only).
7
+ */
8
+ import { loadConfig } from '@angular-modernizer/core';
9
+ /**
10
+ * `parser.componentDecorators` of the project, or `undefined` when the project
11
+ * has no config file or the config does not load. A broken config must not
12
+ * break investigation; the tools then fall back to `Component` only.
13
+ */
14
+ export function configuredComponentDecorators(projectRoot) {
15
+ try {
16
+ return loadConfig(projectRoot).parser?.componentDecorators;
17
+ }
18
+ catch {
19
+ return undefined;
20
+ }
21
+ }
22
+ //# sourceMappingURL=project-decorators.js.map
@@ -199,6 +199,11 @@ export function getAvailableRules(inputSchema) {
199
199
  name: 'Subscription Transform',
200
200
  description: 'Add takeUntilDestroyed() to raw .subscribe() calls in lifecycle hooks',
201
201
  },
202
+ 'service-bag-transform': {
203
+ id: 'refactor:service-bag-split',
204
+ name: 'Service Bag Split',
205
+ description: 'Split a service bag into focused services by concern and backend area',
206
+ },
202
207
  };
203
208
  return transformationEnum.map((transformationType) => {
204
209
  const mapping = ruleMapping[transformationType];
@@ -0,0 +1,72 @@
1
+ /**
2
+ * @angular-modernizer/adapter-mcp - Rule runner for scan tools
3
+ *
4
+ * Runs analysis rules over a file list with a time budget:
5
+ * - all files are added to the project first, so project-wide rules
6
+ * (library extraction, circular dependencies, type resolution) see the same,
7
+ * complete project for every file instead of a prefix that grows per file;
8
+ * the load yields and checks the signal per file as well
9
+ * - the budget and the abort signal are checked between files; a rule that
10
+ * is running cannot be interrupted (analysis is synchronous CPU work), so
11
+ * the overrun is at most one file. With a signal the loop yields to the
12
+ * event loop once per file, otherwise `notifications/cancelled` is not read
13
+ * before the scan ends
14
+ * - time per rule is recorded so slow rules show up in the result
15
+ */
16
+ import type { Project } from 'ts-morph';
17
+ import { type AnalysisRule } from '@angular-modernizer/plugin-system';
18
+ /** Default scan time budget: 5 minutes. */
19
+ export declare const DEFAULT_MAX_DURATION_MS = 300000;
20
+ export interface RuleRunnerViolation {
21
+ id: string;
22
+ ruleId: string;
23
+ severity: 'error' | 'warning' | 'info';
24
+ message: string;
25
+ filePath: string;
26
+ line?: number;
27
+ column?: number;
28
+ category: string;
29
+ suggestedFix?: string;
30
+ metadata?: Record<string, unknown>;
31
+ }
32
+ export interface RuleTiming {
33
+ ruleId: string;
34
+ ms: number;
35
+ }
36
+ export interface RuleRunResult {
37
+ violations: RuleRunnerViolation[];
38
+ filesAnalyzed: number;
39
+ /** `true` when the budget or a cancellation stopped the run early. */
40
+ incomplete: boolean;
41
+ stopReason?: 'time-budget' | 'cancelled';
42
+ /** Files that were not analyzed because the run stopped early. */
43
+ skippedFiles: number;
44
+ /** Rules ordered by total time, slowest first. */
45
+ ruleTimings: RuleTiming[];
46
+ durationMs: number;
47
+ }
48
+ export interface RuleRunOptions {
49
+ project: Project;
50
+ files: readonly string[];
51
+ rules: readonly AnalysisRule[];
52
+ api: unknown;
53
+ config: Record<string, unknown>;
54
+ /** Time budget in ms; `0` disables it. Default {@link DEFAULT_MAX_DURATION_MS}. */
55
+ maxDurationMs?: number;
56
+ /** Start of the budget (defaults to now), e.g. the tool's start time. */
57
+ startTime?: number;
58
+ /** Aborts the run between files (MCP `notifications/cancelled`). */
59
+ signal?: AbortSignal;
60
+ isCancelled?: () => boolean;
61
+ onFileAnalyzed?: (analyzed: number, total: number, violations: number) => void;
62
+ }
63
+ /**
64
+ * Parse the `maxDurationMs` tool parameter.
65
+ */
66
+ export declare function parseMaxDurationMs(value: unknown): number;
67
+ export declare function runRulesOverFiles(options: RuleRunOptions): Promise<RuleRunResult>;
68
+ /**
69
+ * Human-readable note for an incomplete run.
70
+ */
71
+ export declare function incompleteNote(result: RuleRunResult, budgetMs: number): string | undefined;
72
+ //# sourceMappingURL=rule-runner.d.ts.map