@angular-modernizer/adapter-mcp 1.0.0 → 1.2.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.
- package/README.md +81 -65
- package/dist/cli.js +8 -0
- package/dist/core/project-analyzer.d.ts +3 -12
- package/dist/core/project-analyzer.js +3 -12
- package/dist/index.d.ts +118 -3
- package/dist/index.js +46 -3
- package/dist/mcp-watcher.d.ts +28 -8
- package/dist/mcp-watcher.js +7 -7
- package/dist/scan-host/scan-host.d.ts +119 -0
- package/dist/scan-host/scan-host.js +376 -0
- package/dist/scan-host/scan-protocol.d.ts +35 -0
- package/dist/scan-host/scan-protocol.js +6 -0
- package/dist/scan-host/scan-worker.d.ts +14 -0
- package/dist/scan-host/scan-worker.js +48 -0
- package/dist/server/limited-stdio-transport.d.ts +7 -0
- package/dist/server/mcp-server.d.ts +11 -7
- package/dist/server/mcp-server.js +4 -7
- package/dist/server/sdk-server.d.ts +1 -0
- package/dist/server/tool-registry.d.ts +3 -7
- package/dist/server/tool-registry.js +1 -7
- package/dist/streaming/progress-store.d.ts +22 -5
- package/dist/streaming/progress-store.js +9 -5
- package/dist/tools/analyze-build-impact.d.ts +58 -36
- package/dist/tools/analyze-build-impact.js +36 -29
- package/dist/tools/analyze-core-to-shared.d.ts +42 -12
- package/dist/tools/analyze-core-to-shared.js +11 -10
- package/dist/tools/analyze-parser-performance.d.ts +23 -22
- package/dist/tools/analyze-parser-performance.js +20 -18
- package/dist/tools/analyze-project.d.ts +87 -14
- package/dist/tools/analyze-project.js +140 -122
- package/dist/tools/check-incremental-compatibility.d.ts +13 -11
- package/dist/tools/check-incremental-compatibility.js +11 -8
- package/dist/tools/collect-feedback.d.ts +41 -26
- package/dist/tools/collect-feedback.js +32 -23
- package/dist/tools/compare-health.d.ts +76 -19
- package/dist/tools/compare-health.js +26 -19
- package/dist/tools/extract-libraries.d.ts +44 -20
- package/dist/tools/extract-libraries.js +16 -17
- package/dist/tools/find-angular-symbol.d.ts +27 -25
- package/dist/tools/find-angular-symbol.js +19 -16
- package/dist/tools/find-component-hosts.d.ts +33 -27
- package/dist/tools/find-component-hosts.js +27 -24
- package/dist/tools/find-usages.d.ts +25 -23
- package/dist/tools/find-usages.js +21 -18
- package/dist/tools/get-bootstrap-execution.d.ts +35 -27
- package/dist/tools/get-bootstrap-execution.js +28 -25
- package/dist/tools/get-call-graph.d.ts +19 -14
- package/dist/tools/get-call-graph.js +26 -23
- package/dist/tools/get-operation-progress-tool.d.ts +12 -6
- package/dist/tools/get-operation-progress-tool.js +11 -6
- package/dist/tools/get-operation-progress.d.ts +26 -0
- package/dist/tools/helpers/analysis-output.d.ts +5 -0
- package/dist/tools/helpers/batch-kernel.d.ts +14 -0
- package/dist/tools/helpers/batch-kernel.js +12 -0
- package/dist/tools/helpers/compact-output.d.ts +2 -1
- package/dist/tools/helpers/compact-output.js +2 -1
- package/dist/tools/helpers/health-delta.d.ts +27 -14
- package/dist/tools/helpers/health-delta.js +11 -14
- package/dist/tools/helpers/project-cache.d.ts +123 -36
- package/dist/tools/helpers/project-cache.js +227 -43
- package/dist/tools/helpers/rule-runner.d.ts +23 -16
- package/dist/tools/helpers/rule-runner.js +24 -17
- package/dist/tools/helpers/scan-engine.d.ts +107 -0
- package/dist/tools/helpers/scan-engine.js +197 -0
- package/dist/tools/helpers/scan-plan.d.ts +1 -1
- package/dist/tools/helpers/scan-plan.js +1 -1
- package/dist/tools/helpers/suggestion-generator.d.ts +9 -13
- package/dist/tools/helpers/suggestion-generator.js +0 -12
- package/dist/tools/helpers/tool-cache.d.ts +11 -0
- package/dist/tools/helpers/tool-cache.js +15 -0
- package/dist/tools/helpers/tool-context.d.ts +11 -8
- package/dist/tools/helpers/tool-context.js +5 -8
- package/dist/tools/inspect-form-group.d.ts +34 -27
- package/dist/tools/inspect-form-group.js +27 -24
- package/dist/tools/inspect-i18n.d.ts +31 -25
- package/dist/tools/inspect-i18n.js +23 -20
- package/dist/tools/inspect-template.d.ts +32 -25
- package/dist/tools/inspect-template.js +26 -23
- package/dist/tools/learn-patterns.d.ts +88 -12
- package/dist/tools/learn-patterns.js +17 -11
- package/dist/tools/list-imports.d.ts +45 -28
- package/dist/tools/list-imports.js +26 -23
- package/dist/tools/orchestrate-migration.d.ts +31 -9
- package/dist/tools/orchestrate-migration.js +12 -7
- package/dist/tools/parse-stack-trace.d.ts +23 -21
- package/dist/tools/parse-stack-trace.js +10 -7
- package/dist/tools/plan-migration.d.ts +67 -27
- package/dist/tools/plan-migration.js +22 -21
- package/dist/tools/resolve-type.d.ts +20 -10
- package/dist/tools/resolve-type.js +21 -13
- package/dist/tools/review-feedback.d.ts +27 -24
- package/dist/tools/review-feedback.js +24 -20
- package/dist/tools/rollback-changes.d.ts +13 -9
- package/dist/tools/rollback-changes.js +10 -7
- package/dist/tools/run-angular-cli.d.ts +12 -10
- package/dist/tools/run-angular-cli.js +11 -8
- package/dist/tools/run-build.d.ts +13 -11
- package/dist/tools/run-build.js +11 -8
- package/dist/tools/run-schematic.d.ts +12 -10
- package/dist/tools/run-schematic.js +11 -8
- package/dist/tools/scan-all.d.ts +69 -12
- package/dist/tools/scan-all.js +55 -122
- package/dist/tools/scan-angular.d.ts +41 -9
- package/dist/tools/scan-angular.js +66 -135
- package/dist/tools/scan-architecture.d.ts +75 -14
- package/dist/tools/scan-architecture.js +180 -185
- package/dist/tools/scan-solid.d.ts +40 -9
- package/dist/tools/scan-solid.js +64 -133
- package/dist/tools/search-codebase.d.ts +32 -26
- package/dist/tools/search-codebase.js +15 -12
- package/dist/tools/server-status.d.ts +116 -24
- package/dist/tools/server-status.js +85 -26
- package/dist/tools/suggest-improvements.d.ts +22 -19
- package/dist/tools/suggest-improvements.js +21 -17
- package/dist/tools/trace-output-flow.d.ts +34 -27
- package/dist/tools/trace-output-flow.js +27 -24
- package/dist/tools/transform-code.d.ts +71 -10
- package/dist/tools/transform-code.js +148 -54
- package/dist/tools/validate-before-transform.d.ts +24 -9
- package/dist/tools/validate-before-transform.js +10 -7
- package/dist/tools/validate-project-compatibility.d.ts +18 -13
- package/dist/tools/validate-project-compatibility.js +17 -11
- package/dist/tools/validate-transformation.d.ts +28 -11
- package/dist/tools/validate-transformation.js +14 -8
- package/dist/tools/verify-citations.d.ts +37 -16
- package/dist/tools/verify-citations.js +19 -16
- package/dist/types.d.ts +40 -10
- package/dist/types.js +0 -5
- package/package.json +12 -18
- package/dist/tools/shared/scan-types.d.ts +0 -200
- package/dist/tools/shared/scan-types.js +0 -7
|
@@ -1,69 +1,54 @@
|
|
|
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
1
|
import { Project } from 'ts-morph';
|
|
2
|
+
import type { ToolContext } from '../../types.js';
|
|
35
3
|
/** A cached project as reported by `server-status`. */
|
|
36
4
|
export interface CachedProjectInfo {
|
|
5
|
+
/** tsconfig the project was loaded from. */
|
|
37
6
|
tsConfigPath: string;
|
|
7
|
+
/** Number of source files in the project. */
|
|
38
8
|
sourceFiles: number;
|
|
9
|
+
/** Load time (ISO 8601). */
|
|
39
10
|
loadedAt: string;
|
|
11
|
+
/** Time of the last `get()` (ISO 8601). */
|
|
40
12
|
lastUsedAt: string;
|
|
41
13
|
/** Heap growth while the project was loaded, an estimate of its size. */
|
|
42
14
|
loadHeapMB: number;
|
|
43
15
|
/** Source files re-read or removed because they changed on disk. */
|
|
44
16
|
refreshedFiles: number;
|
|
17
|
+
/** Time of the last refresh of changed files (ISO 8601). */
|
|
45
18
|
lastRefreshAt?: string;
|
|
46
19
|
}
|
|
47
20
|
/** Why a project left the cache. */
|
|
48
21
|
export type EvictionReason = 'max-entries' | 'max-heap';
|
|
49
22
|
/** One eviction with the memory around it. */
|
|
50
23
|
export interface ProjectEviction {
|
|
24
|
+
/** tsconfig of the evicted project. */
|
|
51
25
|
tsConfigPath: string;
|
|
26
|
+
/** Limit that caused the eviction. */
|
|
52
27
|
reason: EvictionReason;
|
|
28
|
+
/** Time of the eviction (ISO 8601). */
|
|
53
29
|
at: string;
|
|
30
|
+
/** Number of source files of the evicted project. */
|
|
54
31
|
sourceFiles: number;
|
|
32
|
+
/** V8 heap in use, MB. */
|
|
55
33
|
heapMB: {
|
|
34
|
+
/** Before the eviction. */
|
|
56
35
|
before: number;
|
|
36
|
+
/** After the eviction. */
|
|
57
37
|
after: number;
|
|
58
38
|
};
|
|
39
|
+
/** Resident set size, MB. */
|
|
59
40
|
rssMB: {
|
|
41
|
+
/** Before the eviction. */
|
|
60
42
|
before: number;
|
|
43
|
+
/** After the eviction. */
|
|
61
44
|
after: number;
|
|
62
45
|
};
|
|
63
46
|
}
|
|
64
47
|
/** Evictions since the server started. */
|
|
65
48
|
export interface EvictionStats {
|
|
49
|
+
/** Number of evictions. */
|
|
66
50
|
count: number;
|
|
51
|
+
/** The most recent eviction. */
|
|
67
52
|
last?: ProjectEviction;
|
|
68
53
|
}
|
|
69
54
|
/** Heap and RSS reader, plus a full collection when node exposes one. */
|
|
@@ -81,12 +66,41 @@ export interface ProjectCacheLimits {
|
|
|
81
66
|
/** Heap limit in MB checked after each load; 0 switches it off. */
|
|
82
67
|
maxHeapMb: number;
|
|
83
68
|
}
|
|
69
|
+
/**
|
|
70
|
+
* A project opened for a batched load: compiler options of the tsconfig set,
|
|
71
|
+
* no source files added yet, and the files the tsconfig lists.
|
|
72
|
+
*/
|
|
73
|
+
export interface OpenedProject {
|
|
74
|
+
project: Project;
|
|
75
|
+
files: readonly string[];
|
|
76
|
+
}
|
|
77
|
+
/** Abort signal and load progress of one `acquire()` call. */
|
|
78
|
+
export interface AcquireControl {
|
|
79
|
+
/** Leaves the load; the load stops once no caller waits for it. */
|
|
80
|
+
signal?: AbortSignal;
|
|
81
|
+
/** Files added so far and their total, once per batch. */
|
|
82
|
+
onProgress?: (loaded: number, total: number) => void;
|
|
83
|
+
}
|
|
84
|
+
/** Thrown by `acquire()` when its signal aborts. */
|
|
85
|
+
export declare class ProjectLoadCancelledError extends Error {
|
|
86
|
+
constructor(tsConfigPath: string);
|
|
87
|
+
}
|
|
84
88
|
export interface ProjectCacheOptions extends Partial<ProjectCacheLimits> {
|
|
89
|
+
/** Loads a project in one piece; replaces the batched load (tests). */
|
|
85
90
|
createProject?: (tsConfigFilePath: string) => Project;
|
|
91
|
+
/** Opens a project for the batched load (tests); default: {@link openTsConfigProject}. */
|
|
92
|
+
openProject?: (tsConfigFilePath: string) => OpenedProject;
|
|
93
|
+
/** Files added per batch before `acquire()` yields, at least 1. */
|
|
94
|
+
batchSize?: number;
|
|
86
95
|
memory?: MemoryProbe;
|
|
87
96
|
/** mtime of a file in ms, undefined when it cannot be read. */
|
|
88
97
|
statFile?: (filePath: string) => number | undefined;
|
|
89
98
|
}
|
|
99
|
+
/**
|
|
100
|
+
* Default files per batch. On a large app (3869 files) a batch of 50 takes 50
|
|
101
|
+
* to 75 ms to read and parse, 160 ms under heavy load.
|
|
102
|
+
*/
|
|
103
|
+
export declare const DEFAULT_LOAD_BATCH_SIZE = 50;
|
|
90
104
|
/** Default number of workspaces kept in memory at once. */
|
|
91
105
|
export declare const DEFAULT_MAX_ENTRIES = 3;
|
|
92
106
|
/**
|
|
@@ -98,12 +112,52 @@ export declare const DEFAULT_MAX_ENTRIES = 3;
|
|
|
98
112
|
export declare const DEFAULT_MAX_HEAP_MB = 2048;
|
|
99
113
|
/** Bytes to MB, one decimal. */
|
|
100
114
|
export declare function toMB(bytes: number): number;
|
|
101
|
-
/**
|
|
115
|
+
/**
|
|
116
|
+
* Opens the project of a tsconfig with its compiler options and the file list
|
|
117
|
+
* `new Project({ tsConfigFilePath })` would add, without adding the files.
|
|
118
|
+
*/
|
|
119
|
+
export declare function openTsConfigProject(tsConfigFilePath: string): OpenedProject;
|
|
120
|
+
/**
|
|
121
|
+
* LRU cache of ts-morph Projects keyed by tsconfig path. Keeps `Project`
|
|
122
|
+
* instances alive across tool calls, so repeated investigation calls on the
|
|
123
|
+
* same workspace do not re-parse the whole program each time (seconds on a
|
|
124
|
+
* large Angular app).
|
|
125
|
+
*
|
|
126
|
+
* @remarks
|
|
127
|
+
* Same idea as `SharedProjectManager` in core (one long-lived Project instead
|
|
128
|
+
* of one per call), but keyed per tsconfig because the MCP server can serve
|
|
129
|
+
* several workspaces in one session. An entry is rebuilt when the tsconfig
|
|
130
|
+
* mtime changes. Source files are kept current file by file: the mtime of
|
|
131
|
+
* every program file is recorded at load and compared on each `get()` (about
|
|
132
|
+
* 6 ms for 3939 files); changed files are re-read, deleted ones
|
|
133
|
+
* removed. The type checker then rebuilds lazily on the next query. Files
|
|
134
|
+
* that were not in the program at load (new files) need a tsconfig touch or a
|
|
135
|
+
* server restart.
|
|
136
|
+
*
|
|
137
|
+
* Two limits bound the cache: `maxEntries` projects, and `maxHeapMb` of V8
|
|
138
|
+
* heap, checked on every `get()` because a project keeps growing after the
|
|
139
|
+
* load as the type checker resolves more of it, and on a miss also before
|
|
140
|
+
* the load. Over either limit the least recently used project is evicted;
|
|
141
|
+
* the project being requested never is.
|
|
142
|
+
*
|
|
143
|
+
* The heap is the signal, not the RSS: dropping a Project frees its heap
|
|
144
|
+
* (the TypeScript program, checker and ts-morph wrappers hang off it and
|
|
145
|
+
* nothing else keeps them), but V8 does not reliably unmap the pages, so the
|
|
146
|
+
* RSS can stay at its peak after an eviction and an RSS limit would then
|
|
147
|
+
* evict everything on every call. Freed pages are reused by the next load.
|
|
148
|
+
* When node runs with `--expose-gc` the cache collects once the raw heap
|
|
149
|
+
* reading passes the limit and after each eviction, so garbage does not count
|
|
150
|
+
* against the cache; otherwise it subtracts the heap growth recorded when the
|
|
151
|
+
* evicted project was loaded, and garbage makes it evict earlier.
|
|
152
|
+
*/
|
|
102
153
|
export declare class ProjectCache {
|
|
103
154
|
private readonly entries;
|
|
104
155
|
private readonly maxEntries;
|
|
105
156
|
private readonly maxHeapMb;
|
|
106
157
|
private readonly createProject;
|
|
158
|
+
private readonly openProject;
|
|
159
|
+
private readonly batchSize;
|
|
160
|
+
private readonly loading;
|
|
107
161
|
private readonly memory;
|
|
108
162
|
private readonly statFile;
|
|
109
163
|
private evictionCount;
|
|
@@ -113,8 +167,25 @@ export declare class ProjectCache {
|
|
|
113
167
|
* the last two.
|
|
114
168
|
*/
|
|
115
169
|
constructor(options?: ProjectCacheOptions);
|
|
116
|
-
/**
|
|
170
|
+
/**
|
|
171
|
+
* Returns the cached Project for `tsConfigPath`, building it on a miss in
|
|
172
|
+
* one synchronous piece. Tools use `acquire()`.
|
|
173
|
+
*/
|
|
117
174
|
get(tsConfigPath: string): Project;
|
|
175
|
+
/**
|
|
176
|
+
* Returns the cached Project for `tsConfigPath`; on a miss loads it in
|
|
177
|
+
* batches of files with one event-loop turn after each, then builds the
|
|
178
|
+
* program and, after another turn, the type checker. The server keeps
|
|
179
|
+
* reading requests in between, and an aborted signal leaves the load at
|
|
180
|
+
* the next turn.
|
|
181
|
+
*
|
|
182
|
+
* Calls for the same tsconfig while it loads share the load. It stops only
|
|
183
|
+
* when every caller has left; a stopped load is not cached. A hit behaves
|
|
184
|
+
* like `get()`.
|
|
185
|
+
*
|
|
186
|
+
* @throws ProjectLoadCancelledError when `control.signal` aborts.
|
|
187
|
+
*/
|
|
188
|
+
acquire(tsConfigPath: string, control?: AcquireControl): Promise<Project>;
|
|
118
189
|
/** Number of cached projects. */
|
|
119
190
|
get size(): number;
|
|
120
191
|
/** Most projects kept at once. */
|
|
@@ -141,7 +212,18 @@ export declare class ProjectCache {
|
|
|
141
212
|
/** Re-reads program files whose mtime changed and removes deleted ones. */
|
|
142
213
|
private refreshChangedFiles;
|
|
143
214
|
private recordMtimes;
|
|
144
|
-
|
|
215
|
+
/**
|
|
216
|
+
* The load as steps: one per batch of added files (files added, total),
|
|
217
|
+
* the program built after the last one. An injected `createProject` loads
|
|
218
|
+
* in one step.
|
|
219
|
+
*/
|
|
220
|
+
private loadSteps;
|
|
221
|
+
private startLoad;
|
|
222
|
+
private loadInBatches;
|
|
223
|
+
private stopIfCancelled;
|
|
224
|
+
/** Joins `job` until it settles or `control.signal` aborts. */
|
|
225
|
+
private waitFor;
|
|
226
|
+
private store;
|
|
145
227
|
private evictOverCount;
|
|
146
228
|
/**
|
|
147
229
|
* Evicts least recently used projects, never `current`, until the heap is
|
|
@@ -181,4 +263,9 @@ export interface ParsedProjectCacheLimits {
|
|
|
181
263
|
* with a warning so a typo does not stop the server.
|
|
182
264
|
*/
|
|
183
265
|
export declare function parseProjectCacheLimits(argv: readonly string[], env: Readonly<Record<string, string | undefined>>): ParsedProjectCacheLimits;
|
|
266
|
+
/**
|
|
267
|
+
* Project of a tool call from the shared cache: the call's signal leaves the
|
|
268
|
+
* load, and with a progress token the files loaded are reported per batch.
|
|
269
|
+
*/
|
|
270
|
+
export declare function acquireProject(projects: ProjectCache, tsConfigPath: string, ctx?: ToolContext): Promise<Project>;
|
|
184
271
|
//# sourceMappingURL=project-cache.d.ts.map
|
|
@@ -1,39 +1,18 @@
|
|
|
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
1
|
import { statSync } from 'node:fs';
|
|
35
|
-
import { Project } from 'ts-morph';
|
|
2
|
+
import { Project, ts } from 'ts-morph';
|
|
36
3
|
import { findUnresolvedLibraryImports, libraryImportWarning, } from './library-imports.js';
|
|
4
|
+
/** Thrown by `acquire()` when its signal aborts. */
|
|
5
|
+
export class ProjectLoadCancelledError extends Error {
|
|
6
|
+
constructor(tsConfigPath) {
|
|
7
|
+
super(`Loading ${tsConfigPath} was cancelled`);
|
|
8
|
+
this.name = 'ProjectLoadCancelledError';
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Default files per batch. On a large app (3869 files) a batch of 50 takes 50
|
|
13
|
+
* to 75 ms to read and parse, 160 ms under heavy load.
|
|
14
|
+
*/
|
|
15
|
+
export const DEFAULT_LOAD_BATCH_SIZE = 50;
|
|
37
16
|
/** Default number of workspaces kept in memory at once. */
|
|
38
17
|
export const DEFAULT_MAX_ENTRIES = 3;
|
|
39
18
|
/**
|
|
@@ -56,18 +35,82 @@ function fileMtime(filePath) {
|
|
|
56
35
|
return undefined;
|
|
57
36
|
}
|
|
58
37
|
}
|
|
38
|
+
/** Runs a generator to its end and returns its result. */
|
|
39
|
+
function runToEnd(steps) {
|
|
40
|
+
let step = steps.next();
|
|
41
|
+
while (step.done !== true) {
|
|
42
|
+
step = steps.next();
|
|
43
|
+
}
|
|
44
|
+
return step.value;
|
|
45
|
+
}
|
|
46
|
+
/** One turn of the event loop (stdin data, timers, other requests). */
|
|
47
|
+
function nextTurn() {
|
|
48
|
+
return new Promise((resolve) => setImmediate(resolve));
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Opens the project of a tsconfig with its compiler options and the file list
|
|
52
|
+
* `new Project({ tsConfigFilePath })` would add, without adding the files.
|
|
53
|
+
*/
|
|
54
|
+
export function openTsConfigProject(tsConfigFilePath) {
|
|
55
|
+
const project = new Project({
|
|
56
|
+
tsConfigFilePath,
|
|
57
|
+
skipAddingFilesFromTsConfig: true,
|
|
58
|
+
});
|
|
59
|
+
const parsed = ts.getParsedCommandLineOfConfigFile(tsConfigFilePath, {}, {
|
|
60
|
+
...ts.sys,
|
|
61
|
+
onUnRecoverableConfigFileDiagnostic: (diagnostic) => {
|
|
62
|
+
throw new Error(ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n'));
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
return { project, files: parsed?.fileNames ?? [] };
|
|
66
|
+
}
|
|
59
67
|
const processMemory = {
|
|
60
68
|
usage: () => process.memoryUsage(),
|
|
61
69
|
...(typeof globalThis.gc === 'function'
|
|
62
70
|
? { collect: globalThis.gc.bind(globalThis) }
|
|
63
71
|
: {}),
|
|
64
72
|
};
|
|
65
|
-
/**
|
|
73
|
+
/**
|
|
74
|
+
* LRU cache of ts-morph Projects keyed by tsconfig path. Keeps `Project`
|
|
75
|
+
* instances alive across tool calls, so repeated investigation calls on the
|
|
76
|
+
* same workspace do not re-parse the whole program each time (seconds on a
|
|
77
|
+
* large Angular app).
|
|
78
|
+
*
|
|
79
|
+
* @remarks
|
|
80
|
+
* Same idea as `SharedProjectManager` in core (one long-lived Project instead
|
|
81
|
+
* of one per call), but keyed per tsconfig because the MCP server can serve
|
|
82
|
+
* several workspaces in one session. An entry is rebuilt when the tsconfig
|
|
83
|
+
* mtime changes. Source files are kept current file by file: the mtime of
|
|
84
|
+
* every program file is recorded at load and compared on each `get()` (about
|
|
85
|
+
* 6 ms for 3939 files); changed files are re-read, deleted ones
|
|
86
|
+
* removed. The type checker then rebuilds lazily on the next query. Files
|
|
87
|
+
* that were not in the program at load (new files) need a tsconfig touch or a
|
|
88
|
+
* server restart.
|
|
89
|
+
*
|
|
90
|
+
* Two limits bound the cache: `maxEntries` projects, and `maxHeapMb` of V8
|
|
91
|
+
* heap, checked on every `get()` because a project keeps growing after the
|
|
92
|
+
* load as the type checker resolves more of it, and on a miss also before
|
|
93
|
+
* the load. Over either limit the least recently used project is evicted;
|
|
94
|
+
* the project being requested never is.
|
|
95
|
+
*
|
|
96
|
+
* The heap is the signal, not the RSS: dropping a Project frees its heap
|
|
97
|
+
* (the TypeScript program, checker and ts-morph wrappers hang off it and
|
|
98
|
+
* nothing else keeps them), but V8 does not reliably unmap the pages, so the
|
|
99
|
+
* RSS can stay at its peak after an eviction and an RSS limit would then
|
|
100
|
+
* evict everything on every call. Freed pages are reused by the next load.
|
|
101
|
+
* When node runs with `--expose-gc` the cache collects once the raw heap
|
|
102
|
+
* reading passes the limit and after each eviction, so garbage does not count
|
|
103
|
+
* against the cache; otherwise it subtracts the heap growth recorded when the
|
|
104
|
+
* evicted project was loaded, and garbage makes it evict earlier.
|
|
105
|
+
*/
|
|
66
106
|
export class ProjectCache {
|
|
67
107
|
entries = new Map();
|
|
68
108
|
maxEntries;
|
|
69
109
|
maxHeapMb;
|
|
70
110
|
createProject;
|
|
111
|
+
openProject;
|
|
112
|
+
batchSize;
|
|
113
|
+
loading = new Map();
|
|
71
114
|
memory;
|
|
72
115
|
statFile;
|
|
73
116
|
evictionCount = 0;
|
|
@@ -79,13 +122,16 @@ export class ProjectCache {
|
|
|
79
122
|
constructor(options = {}) {
|
|
80
123
|
this.maxEntries = Math.max(1, options.maxEntries ?? DEFAULT_MAX_ENTRIES);
|
|
81
124
|
this.maxHeapMb = Math.max(0, options.maxHeapMb ?? DEFAULT_MAX_HEAP_MB);
|
|
82
|
-
this.createProject =
|
|
83
|
-
|
|
84
|
-
|
|
125
|
+
this.createProject = options.createProject;
|
|
126
|
+
this.openProject = options.openProject ?? openTsConfigProject;
|
|
127
|
+
this.batchSize = Math.max(1, Math.floor(options.batchSize ?? DEFAULT_LOAD_BATCH_SIZE));
|
|
85
128
|
this.memory = options.memory ?? processMemory;
|
|
86
129
|
this.statFile = options.statFile ?? fileMtime;
|
|
87
130
|
}
|
|
88
|
-
/**
|
|
131
|
+
/**
|
|
132
|
+
* Returns the cached Project for `tsConfigPath`, building it on a miss in
|
|
133
|
+
* one synchronous piece. Tools use `acquire()`.
|
|
134
|
+
*/
|
|
89
135
|
get(tsConfigPath) {
|
|
90
136
|
const mtimeMs = statSync(tsConfigPath).mtimeMs;
|
|
91
137
|
let project = this.touch(tsConfigPath, mtimeMs);
|
|
@@ -93,12 +139,43 @@ export class ProjectCache {
|
|
|
93
139
|
// Make room first: a load on top of a heap already over the limit
|
|
94
140
|
// would stack two large programs before the check below evicts one.
|
|
95
141
|
this.evictOverHeap(tsConfigPath);
|
|
96
|
-
|
|
142
|
+
const heapBefore = this.memory.usage().heapUsed;
|
|
143
|
+
project = this.store(tsConfigPath, mtimeMs, heapBefore, runToEnd(this.loadSteps(tsConfigPath)));
|
|
97
144
|
}
|
|
98
145
|
this.evictOverCount();
|
|
99
146
|
this.evictOverHeap(tsConfigPath);
|
|
100
147
|
return project;
|
|
101
148
|
}
|
|
149
|
+
/**
|
|
150
|
+
* Returns the cached Project for `tsConfigPath`; on a miss loads it in
|
|
151
|
+
* batches of files with one event-loop turn after each, then builds the
|
|
152
|
+
* program and, after another turn, the type checker. The server keeps
|
|
153
|
+
* reading requests in between, and an aborted signal leaves the load at
|
|
154
|
+
* the next turn.
|
|
155
|
+
*
|
|
156
|
+
* Calls for the same tsconfig while it loads share the load. It stops only
|
|
157
|
+
* when every caller has left; a stopped load is not cached. A hit behaves
|
|
158
|
+
* like `get()`.
|
|
159
|
+
*
|
|
160
|
+
* @throws ProjectLoadCancelledError when `control.signal` aborts.
|
|
161
|
+
*/
|
|
162
|
+
async acquire(tsConfigPath, control = {}) {
|
|
163
|
+
if (control.signal?.aborted === true) {
|
|
164
|
+
throw new ProjectLoadCancelledError(tsConfigPath);
|
|
165
|
+
}
|
|
166
|
+
const mtimeMs = statSync(tsConfigPath).mtimeMs;
|
|
167
|
+
let job = this.loading.get(tsConfigPath);
|
|
168
|
+
if (job === undefined) {
|
|
169
|
+
const project = this.touch(tsConfigPath, mtimeMs);
|
|
170
|
+
if (project !== undefined) {
|
|
171
|
+
this.evictOverCount();
|
|
172
|
+
this.evictOverHeap(tsConfigPath);
|
|
173
|
+
return project;
|
|
174
|
+
}
|
|
175
|
+
job = this.startLoad(tsConfigPath, mtimeMs);
|
|
176
|
+
}
|
|
177
|
+
return this.waitFor(tsConfigPath, job, control);
|
|
178
|
+
}
|
|
102
179
|
/** Number of cached projects. */
|
|
103
180
|
get size() {
|
|
104
181
|
return this.entries.size;
|
|
@@ -200,9 +277,101 @@ export class ProjectCache {
|
|
|
200
277
|
}
|
|
201
278
|
return mtimes;
|
|
202
279
|
}
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
280
|
+
/**
|
|
281
|
+
* The load as steps: one per batch of added files (files added, total),
|
|
282
|
+
* the program built after the last one. An injected `createProject` loads
|
|
283
|
+
* in one step.
|
|
284
|
+
*/
|
|
285
|
+
*loadSteps(tsConfigPath) {
|
|
286
|
+
if (this.createProject !== undefined) {
|
|
287
|
+
return this.createProject(tsConfigPath);
|
|
288
|
+
}
|
|
289
|
+
const { project, files } = this.openProject(tsConfigPath);
|
|
290
|
+
for (let start = 0; start < files.length; start += this.batchSize) {
|
|
291
|
+
for (const filePath of files.slice(start, start + this.batchSize)) {
|
|
292
|
+
project.addSourceFileAtPath(filePath);
|
|
293
|
+
}
|
|
294
|
+
yield [Math.min(start + this.batchSize, files.length), files.length];
|
|
295
|
+
}
|
|
296
|
+
project.resolveSourceFileDependencies();
|
|
297
|
+
return project;
|
|
298
|
+
}
|
|
299
|
+
startLoad(tsConfigPath, mtimeMs) {
|
|
300
|
+
const state = { waiters: new Set(), cancelled: false };
|
|
301
|
+
const job = Object.assign(state, {
|
|
302
|
+
promise: this.loadInBatches(tsConfigPath, mtimeMs, state),
|
|
303
|
+
});
|
|
304
|
+
// Every waiter handles the outcome; a load nobody waits for any more
|
|
305
|
+
// must not end as an unhandled rejection.
|
|
306
|
+
job.promise.catch(() => undefined);
|
|
307
|
+
this.loading.set(tsConfigPath, job);
|
|
308
|
+
return job;
|
|
309
|
+
}
|
|
310
|
+
async loadInBatches(tsConfigPath, mtimeMs, job) {
|
|
311
|
+
try {
|
|
312
|
+
// The first turn lets the caller register as a waiter.
|
|
313
|
+
await nextTurn();
|
|
314
|
+
this.stopIfCancelled(tsConfigPath, job);
|
|
315
|
+
this.evictOverHeap(tsConfigPath);
|
|
316
|
+
const heapBefore = this.memory.usage().heapUsed;
|
|
317
|
+
const steps = this.loadSteps(tsConfigPath);
|
|
318
|
+
let step = steps.next();
|
|
319
|
+
while (step.done !== true) {
|
|
320
|
+
const [loaded, total] = step.value;
|
|
321
|
+
for (const waiter of job.waiters) {
|
|
322
|
+
waiter.onProgress?.(loaded, total);
|
|
323
|
+
}
|
|
324
|
+
await nextTurn();
|
|
325
|
+
this.stopIfCancelled(tsConfigPath, job);
|
|
326
|
+
step = steps.next();
|
|
327
|
+
}
|
|
328
|
+
const project = step.value;
|
|
329
|
+
await nextTurn();
|
|
330
|
+
this.stopIfCancelled(tsConfigPath, job);
|
|
331
|
+
project.getProgram().compilerObject.getTypeChecker();
|
|
332
|
+
const cached = this.entries.get(tsConfigPath);
|
|
333
|
+
const result = cached?.mtimeMs === mtimeMs
|
|
334
|
+
? cached.project
|
|
335
|
+
: this.store(tsConfigPath, mtimeMs, heapBefore, project);
|
|
336
|
+
this.evictOverCount();
|
|
337
|
+
this.evictOverHeap(tsConfigPath);
|
|
338
|
+
return result;
|
|
339
|
+
}
|
|
340
|
+
finally {
|
|
341
|
+
// Synchronously, so a call after a stopped load starts a new one.
|
|
342
|
+
if (this.loading.get(tsConfigPath) === job) {
|
|
343
|
+
this.loading.delete(tsConfigPath);
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
stopIfCancelled(tsConfigPath, job) {
|
|
348
|
+
if (job.cancelled) {
|
|
349
|
+
throw new ProjectLoadCancelledError(tsConfigPath);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
/** Joins `job` until it settles or `control.signal` aborts. */
|
|
353
|
+
waitFor(tsConfigPath, job, control) {
|
|
354
|
+
const waiter = { onProgress: control.onProgress };
|
|
355
|
+
job.waiters.add(waiter);
|
|
356
|
+
// A caller joining a load whose last caller just left keeps it going.
|
|
357
|
+
job.cancelled = false;
|
|
358
|
+
const { signal } = control;
|
|
359
|
+
if (signal === undefined) {
|
|
360
|
+
return job.promise;
|
|
361
|
+
}
|
|
362
|
+
return new Promise((resolve, reject) => {
|
|
363
|
+
const onAbort = () => {
|
|
364
|
+
job.waiters.delete(waiter);
|
|
365
|
+
job.cancelled = job.waiters.size === 0;
|
|
366
|
+
reject(new ProjectLoadCancelledError(tsConfigPath));
|
|
367
|
+
};
|
|
368
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
369
|
+
void job.promise
|
|
370
|
+
.then(resolve, reject)
|
|
371
|
+
.finally(() => signal.removeEventListener('abort', onAbort));
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
store(tsConfigPath, mtimeMs, heapBefore, project) {
|
|
206
375
|
const now = new Date();
|
|
207
376
|
this.entries.set(tsConfigPath, {
|
|
208
377
|
project,
|
|
@@ -328,4 +497,19 @@ export function parseProjectCacheLimits(argv, env) {
|
|
|
328
497
|
}
|
|
329
498
|
return { options, warnings };
|
|
330
499
|
}
|
|
500
|
+
/**
|
|
501
|
+
* Project of a tool call from the shared cache: the call's signal leaves the
|
|
502
|
+
* load, and with a progress token the files loaded are reported per batch.
|
|
503
|
+
*/
|
|
504
|
+
export function acquireProject(projects, tsConfigPath, ctx = {}) {
|
|
505
|
+
const { progress } = ctx;
|
|
506
|
+
return projects.acquire(tsConfigPath, {
|
|
507
|
+
signal: ctx.signal,
|
|
508
|
+
onProgress: progress === undefined
|
|
509
|
+
? undefined
|
|
510
|
+
: (loaded, total) => {
|
|
511
|
+
progress(loaded, total, `${loaded}/${total} files loaded`);
|
|
512
|
+
},
|
|
513
|
+
});
|
|
514
|
+
}
|
|
331
515
|
//# sourceMappingURL=project-cache.js.map
|
|
@@ -1,18 +1,3 @@
|
|
|
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
1
|
import type { Project } from 'ts-morph';
|
|
17
2
|
import { type AnalysisRule } from '@angular-modernizer/plugin-system';
|
|
18
3
|
/** Default scan time budget: 5 minutes. */
|
|
@@ -29,8 +14,11 @@ export interface RuleRunnerViolation {
|
|
|
29
14
|
suggestedFix?: string;
|
|
30
15
|
metadata?: Record<string, unknown>;
|
|
31
16
|
}
|
|
17
|
+
/** Total analysis time of one rule over all files of a scan. */
|
|
32
18
|
export interface RuleTiming {
|
|
19
|
+
/** Rule id. */
|
|
33
20
|
ruleId: string;
|
|
21
|
+
/** Time spent in the rule in milliseconds, summed over all files. */
|
|
34
22
|
ms: number;
|
|
35
23
|
}
|
|
36
24
|
export interface RuleRunResult {
|
|
@@ -59,14 +47,33 @@ export interface RuleRunOptions {
|
|
|
59
47
|
signal?: AbortSignal;
|
|
60
48
|
isCancelled?: () => boolean;
|
|
61
49
|
onFileAnalyzed?: (analyzed: number, total: number, violations: number) => void;
|
|
50
|
+
/** Findings of each analyzed file, in analysis order. */
|
|
51
|
+
onFileResults?: (filePath: string, violations: RuleRunnerViolation[]) => void;
|
|
52
|
+
/** Keep the findings in `violations` (default); `false` leaves it empty. */
|
|
53
|
+
collect?: boolean;
|
|
62
54
|
}
|
|
63
55
|
/**
|
|
64
56
|
* Parse the `maxDurationMs` tool parameter.
|
|
65
57
|
*/
|
|
66
58
|
export declare function parseMaxDurationMs(value: unknown): number;
|
|
59
|
+
/**
|
|
60
|
+
* Runs analysis rules over a file list with a time budget.
|
|
61
|
+
*
|
|
62
|
+
* @remarks
|
|
63
|
+
* - All files are added to the project first, so project-wide rules
|
|
64
|
+
* (library extraction, circular dependencies, type resolution) see the same,
|
|
65
|
+
* complete project for every file instead of a prefix that grows per file;
|
|
66
|
+
* the load yields and checks the signal per file as well.
|
|
67
|
+
* - The budget and the abort signal are checked between files; a rule that
|
|
68
|
+
* is running cannot be interrupted (analysis is synchronous CPU work), so
|
|
69
|
+
* the overrun is at most one file. With a signal the loop yields to the
|
|
70
|
+
* event loop once per file, otherwise `notifications/cancelled` is not read
|
|
71
|
+
* before the scan ends.
|
|
72
|
+
* - Time per rule is recorded so slow rules show up in the result.
|
|
73
|
+
*/
|
|
67
74
|
export declare function runRulesOverFiles(options: RuleRunOptions): Promise<RuleRunResult>;
|
|
68
75
|
/**
|
|
69
76
|
* Human-readable note for an incomplete run.
|
|
70
77
|
*/
|
|
71
|
-
export declare function incompleteNote(result: RuleRunResult, budgetMs: number): string | undefined;
|
|
78
|
+
export declare function incompleteNote(result: Omit<RuleRunResult, 'violations'>, budgetMs: number): string | undefined;
|
|
72
79
|
//# sourceMappingURL=rule-runner.d.ts.map
|