@angular-modernizer/adapter-mcp 1.0.0 → 1.1.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 +78 -64
- 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 +56 -34
- package/dist/tools/analyze-build-impact.js +30 -25
- 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 +82 -12
- package/dist/tools/analyze-project.js +98 -101
- 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 +25 -23
- package/dist/tools/find-angular-symbol.js +17 -14
- package/dist/tools/find-component-hosts.d.ts +31 -25
- package/dist/tools/find-component-hosts.js +24 -21
- package/dist/tools/find-usages.d.ts +23 -21
- package/dist/tools/find-usages.js +19 -16
- package/dist/tools/get-bootstrap-execution.d.ts +33 -25
- package/dist/tools/get-bootstrap-execution.js +25 -22
- package/dist/tools/get-call-graph.d.ts +18 -13
- package/dist/tools/get-call-graph.js +24 -21
- 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 +50 -34
- package/dist/tools/helpers/project-cache.js +33 -34
- 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/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 +32 -25
- package/dist/tools/inspect-form-group.js +24 -21
- package/dist/tools/inspect-i18n.d.ts +28 -22
- package/dist/tools/inspect-i18n.js +20 -17
- package/dist/tools/inspect-template.d.ts +31 -24
- package/dist/tools/inspect-template.js +23 -20
- package/dist/tools/learn-patterns.d.ts +88 -12
- package/dist/tools/learn-patterns.js +17 -11
- package/dist/tools/list-imports.d.ts +42 -25
- package/dist/tools/list-imports.js +23 -20
- 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 +65 -25
- package/dist/tools/plan-migration.js +19 -19
- package/dist/tools/resolve-type.d.ts +19 -9
- package/dist/tools/resolve-type.js +19 -11
- 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 +30 -24
- package/dist/tools/search-codebase.js +13 -10
- 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 +32 -25
- package/dist/tools/trace-output-flow.js +24 -21
- package/dist/tools/transform-code.d.ts +71 -10
- package/dist/tools/transform-code.js +60 -42
- 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
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @angular-modernizer/adapter-mcp - Scan worker host
|
|
3
|
+
*
|
|
4
|
+
* Runs the analysis part of a scan ({@link runScanEngine}) in a worker
|
|
5
|
+
* thread. The first type query of a scan builds the TypeScript program and
|
|
6
|
+
* type checker synchronously (large app: 2784 program files, 1.6 s in one
|
|
7
|
+
* block); on the main thread the server reads no stdin meanwhile, so a
|
|
8
|
+
* `notifications/cancelled` and every other request wait. In a worker the
|
|
9
|
+
* main thread keeps serving, and a cancel ends the worker with `terminate()`,
|
|
10
|
+
* which stops synchronous JavaScript too, the checker build included.
|
|
11
|
+
*
|
|
12
|
+
* One scan worker serves the scans one after the other, each with a new
|
|
13
|
+
* kernel; further scans wait in a queue (heap). The worker stays alive after
|
|
14
|
+
* a finished scan: a new isolate loads ts-morph, core and api in about 400 ms
|
|
15
|
+
* and runs the TypeScript code unoptimized, which made each scan on a small
|
|
16
|
+
* scope 1.5 to 3 s slower than on the main thread (large app). A cancel, an
|
|
17
|
+
* error or the heap limit ends the worker; the host then starts the next one,
|
|
18
|
+
* which loads the modules while it waits. The worker heap is limited to
|
|
19
|
+
* `--max-heap-mb`; it is not part of the main thread's `heapUsed`, which the
|
|
20
|
+
* project cache checks.
|
|
21
|
+
*/
|
|
22
|
+
import { type ScanEngineResult, type ScanSink, type ScanSpec } from '../tools/helpers/scan-engine.js';
|
|
23
|
+
/** Settings of the scan host. */
|
|
24
|
+
export interface ScanHostSettings {
|
|
25
|
+
/** Run scans in a worker; off means in the calling thread. */
|
|
26
|
+
enabled: boolean;
|
|
27
|
+
/** Old-generation heap limit of a scan worker in MB; 0 means no limit. */
|
|
28
|
+
maxHeapMb: number;
|
|
29
|
+
}
|
|
30
|
+
/** A running scan worker, as server-status shows it. */
|
|
31
|
+
export interface ScanWorkerInfo {
|
|
32
|
+
/** Thread id of the worker. */
|
|
33
|
+
threadId: number;
|
|
34
|
+
/** Tool that started it. */
|
|
35
|
+
tool: string;
|
|
36
|
+
/** Start time of the scan (ISO). */
|
|
37
|
+
startedAt: string;
|
|
38
|
+
/** Files the scan analyzes. */
|
|
39
|
+
files: number;
|
|
40
|
+
/** Files analyzed so far. */
|
|
41
|
+
filesDone: number;
|
|
42
|
+
/** Last heap the worker reported, in MB. */
|
|
43
|
+
heapUsedMB?: number;
|
|
44
|
+
}
|
|
45
|
+
/** State of the scan host for server-status. */
|
|
46
|
+
export interface ScanHostStatus {
|
|
47
|
+
/** Scans run in a worker. */
|
|
48
|
+
enabled: boolean;
|
|
49
|
+
/** Heap limit of a scan worker in MB; 0 means no limit. */
|
|
50
|
+
maxHeapMB: number;
|
|
51
|
+
/** A worker with loaded modules waits for the next scan (started ahead or from the last scan). */
|
|
52
|
+
standby: boolean;
|
|
53
|
+
/** Running workers (at most one). */
|
|
54
|
+
running: ScanWorkerInfo[];
|
|
55
|
+
/** Scans waiting for the running one. */
|
|
56
|
+
queued: number;
|
|
57
|
+
/** Scans started in a worker since the server started. */
|
|
58
|
+
started: number;
|
|
59
|
+
/** Workers terminated by a cancel. */
|
|
60
|
+
cancelled: number;
|
|
61
|
+
/** Workers that failed (error, crash, heap limit). */
|
|
62
|
+
failed: number;
|
|
63
|
+
}
|
|
64
|
+
/** Command-line flag and environment variable of the scan worker switch. */
|
|
65
|
+
export declare const SCAN_WORKER_FLAG: {
|
|
66
|
+
readonly flag: "--scan-worker";
|
|
67
|
+
readonly env: "ANGULAR_MODERNIZER_SCAN_WORKER";
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* Sets how scans run. The CLI turns the worker on, which also starts the
|
|
71
|
+
* waiting worker; without this call (Jest, library use) scans run in the
|
|
72
|
+
* calling thread.
|
|
73
|
+
*/
|
|
74
|
+
export declare function configureScanHost(next: Partial<ScanHostSettings>): void;
|
|
75
|
+
/** Ends the waiting worker (tests; the server ends with the process). */
|
|
76
|
+
export declare function stopStandby(): void;
|
|
77
|
+
/** Current settings. */
|
|
78
|
+
export declare function scanHostSettings(): ScanHostSettings;
|
|
79
|
+
/** State for server-status. */
|
|
80
|
+
export declare function describeScanHost(): ScanHostStatus;
|
|
81
|
+
/**
|
|
82
|
+
* Reads the scan worker switch from `--scan-worker` (`--scan-worker=0`,
|
|
83
|
+
* `--scan-worker 0`) or `ANGULAR_MODERNIZER_SCAN_WORKER`; the flag wins.
|
|
84
|
+
* Default on; `0`, `false`, `off` switch it off. Other values keep the
|
|
85
|
+
* default with a warning.
|
|
86
|
+
*/
|
|
87
|
+
export declare function parseScanWorkerSetting(argv: readonly string[], env: Readonly<Record<string, string | undefined>>): {
|
|
88
|
+
enabled: boolean;
|
|
89
|
+
warnings: string[];
|
|
90
|
+
};
|
|
91
|
+
/** Options of a scan run. */
|
|
92
|
+
export interface RunScanOptions {
|
|
93
|
+
/** Aborted by `notifications/cancelled`. */
|
|
94
|
+
signal?: AbortSignal;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Runs a scan in a worker when the host is enabled and there is something to
|
|
98
|
+
* analyze, otherwise in the calling thread. Both report through `sink`; a
|
|
99
|
+
* cancelled worker run resolves with `cancelled: true` and only the files the
|
|
100
|
+
* sink got before the cancel.
|
|
101
|
+
*/
|
|
102
|
+
export declare function runScan(spec: ScanSpec, sink: ScanSink, options?: RunScanOptions): Promise<ScanEngineResult>;
|
|
103
|
+
/** Options of {@link runScanInWorker}. */
|
|
104
|
+
export interface WorkerRunOptions {
|
|
105
|
+
signal?: AbortSignal;
|
|
106
|
+
/** Old-generation heap limit in MB; 0 or unset means no limit. */
|
|
107
|
+
maxHeapMb?: number;
|
|
108
|
+
/** Worker script; default the scan worker next to this module (standby when ready). */
|
|
109
|
+
workerUrl?: URL;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Runs `spec` in a worker. Abort terminates the worker at once and resolves
|
|
113
|
+
* with `cancelled: true`; messages arriving after the abort are dropped. A
|
|
114
|
+
* worker that fails or exits without a result rejects. The call returns once
|
|
115
|
+
* the worker has exited, so the next queued scan never shares the memory
|
|
116
|
+
* with it.
|
|
117
|
+
*/
|
|
118
|
+
export declare function runScanInWorker(spec: ScanSpec, sink: ScanSink, options?: WorkerRunOptions): Promise<ScanEngineResult>;
|
|
119
|
+
//# sourceMappingURL=scan-host.d.ts.map
|
|
@@ -0,0 +1,376 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @angular-modernizer/adapter-mcp - Scan worker host
|
|
3
|
+
*
|
|
4
|
+
* Runs the analysis part of a scan ({@link runScanEngine}) in a worker
|
|
5
|
+
* thread. The first type query of a scan builds the TypeScript program and
|
|
6
|
+
* type checker synchronously (large app: 2784 program files, 1.6 s in one
|
|
7
|
+
* block); on the main thread the server reads no stdin meanwhile, so a
|
|
8
|
+
* `notifications/cancelled` and every other request wait. In a worker the
|
|
9
|
+
* main thread keeps serving, and a cancel ends the worker with `terminate()`,
|
|
10
|
+
* which stops synchronous JavaScript too, the checker build included.
|
|
11
|
+
*
|
|
12
|
+
* One scan worker serves the scans one after the other, each with a new
|
|
13
|
+
* kernel; further scans wait in a queue (heap). The worker stays alive after
|
|
14
|
+
* a finished scan: a new isolate loads ts-morph, core and api in about 400 ms
|
|
15
|
+
* and runs the TypeScript code unoptimized, which made each scan on a small
|
|
16
|
+
* scope 1.5 to 3 s slower than on the main thread (large app). A cancel, an
|
|
17
|
+
* error or the heap limit ends the worker; the host then starts the next one,
|
|
18
|
+
* which loads the modules while it waits. The worker heap is limited to
|
|
19
|
+
* `--max-heap-mb`; it is not part of the main thread's `heapUsed`, which the
|
|
20
|
+
* project cache checks.
|
|
21
|
+
*/
|
|
22
|
+
import { Worker } from 'node:worker_threads';
|
|
23
|
+
import { runScanEngine, } from '../tools/helpers/scan-engine.js';
|
|
24
|
+
/** Command-line flag and environment variable of the scan worker switch. */
|
|
25
|
+
export const SCAN_WORKER_FLAG = {
|
|
26
|
+
flag: '--scan-worker',
|
|
27
|
+
env: 'ANGULAR_MODERNIZER_SCAN_WORKER',
|
|
28
|
+
};
|
|
29
|
+
const MAX_RUNNING = 1;
|
|
30
|
+
let settings = { enabled: false, maxHeapMb: 0 };
|
|
31
|
+
let standby;
|
|
32
|
+
const running = new Map();
|
|
33
|
+
const waiting = [];
|
|
34
|
+
let active = 0;
|
|
35
|
+
let started = 0;
|
|
36
|
+
let cancelled = 0;
|
|
37
|
+
let failed = 0;
|
|
38
|
+
/**
|
|
39
|
+
* Sets how scans run. The CLI turns the worker on, which also starts the
|
|
40
|
+
* waiting worker; without this call (Jest, library use) scans run in the
|
|
41
|
+
* calling thread.
|
|
42
|
+
*/
|
|
43
|
+
export function configureScanHost(next) {
|
|
44
|
+
settings = { ...settings, ...next };
|
|
45
|
+
stopStandby();
|
|
46
|
+
prepareStandby();
|
|
47
|
+
}
|
|
48
|
+
/** Ends the waiting worker (tests; the server ends with the process). */
|
|
49
|
+
export function stopStandby() {
|
|
50
|
+
if (standby !== undefined) {
|
|
51
|
+
void standby.worker.terminate();
|
|
52
|
+
standby = undefined;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/** Current settings. */
|
|
56
|
+
export function scanHostSettings() {
|
|
57
|
+
return settings;
|
|
58
|
+
}
|
|
59
|
+
/** State for server-status. */
|
|
60
|
+
export function describeScanHost() {
|
|
61
|
+
return {
|
|
62
|
+
enabled: settings.enabled,
|
|
63
|
+
maxHeapMB: settings.maxHeapMb,
|
|
64
|
+
standby: standby !== undefined && !standby.exited,
|
|
65
|
+
running: [...running.values()],
|
|
66
|
+
queued: waiting.length,
|
|
67
|
+
started,
|
|
68
|
+
cancelled,
|
|
69
|
+
failed,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Reads the scan worker switch from `--scan-worker` (`--scan-worker=0`,
|
|
74
|
+
* `--scan-worker 0`) or `ANGULAR_MODERNIZER_SCAN_WORKER`; the flag wins.
|
|
75
|
+
* Default on; `0`, `false`, `off` switch it off. Other values keep the
|
|
76
|
+
* default with a warning.
|
|
77
|
+
*/
|
|
78
|
+
export function parseScanWorkerSetting(argv, env) {
|
|
79
|
+
const prefix = `${SCAN_WORKER_FLAG.flag}=`;
|
|
80
|
+
const index = argv.findIndex((a) => a === SCAN_WORKER_FLAG.flag || a.startsWith(prefix));
|
|
81
|
+
let value;
|
|
82
|
+
let source = SCAN_WORKER_FLAG.env;
|
|
83
|
+
if (index >= 0) {
|
|
84
|
+
const arg = argv[index] ?? '';
|
|
85
|
+
value = arg === SCAN_WORKER_FLAG.flag ? argv[index + 1] : arg.slice(prefix.length);
|
|
86
|
+
source = SCAN_WORKER_FLAG.flag;
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
value = env[SCAN_WORKER_FLAG.env];
|
|
90
|
+
}
|
|
91
|
+
if (value === undefined) {
|
|
92
|
+
return { enabled: true, warnings: [] };
|
|
93
|
+
}
|
|
94
|
+
const normalized = value.trim().toLowerCase();
|
|
95
|
+
if (['1', 'true', 'on'].includes(normalized)) {
|
|
96
|
+
return { enabled: true, warnings: [] };
|
|
97
|
+
}
|
|
98
|
+
if (['0', 'false', 'off'].includes(normalized)) {
|
|
99
|
+
return { enabled: false, warnings: [] };
|
|
100
|
+
}
|
|
101
|
+
return {
|
|
102
|
+
enabled: true,
|
|
103
|
+
warnings: [`${source}: expected 0 or 1, got "${value}"; scans run in a worker.`],
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Runs a scan in a worker when the host is enabled and there is something to
|
|
108
|
+
* analyze, otherwise in the calling thread. Both report through `sink`; a
|
|
109
|
+
* cancelled worker run resolves with `cancelled: true` and only the files the
|
|
110
|
+
* sink got before the cancel.
|
|
111
|
+
*/
|
|
112
|
+
export async function runScan(spec, sink, options = {}) {
|
|
113
|
+
if (!settings.enabled || spec.files.length === 0) {
|
|
114
|
+
return runScanEngine(spec, sink, { signal: options.signal });
|
|
115
|
+
}
|
|
116
|
+
try {
|
|
117
|
+
return await runScanInWorker(spec, sink, {
|
|
118
|
+
signal: options.signal,
|
|
119
|
+
maxHeapMb: settings.maxHeapMb,
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
finally {
|
|
123
|
+
prepareStandby();
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Runs `spec` in a worker. Abort terminates the worker at once and resolves
|
|
128
|
+
* with `cancelled: true`; messages arriving after the abort are dropped. A
|
|
129
|
+
* worker that fails or exits without a result rejects. The call returns once
|
|
130
|
+
* the worker has exited, so the next queued scan never shares the memory
|
|
131
|
+
* with it.
|
|
132
|
+
*/
|
|
133
|
+
export async function runScanInWorker(spec, sink, options = {}) {
|
|
134
|
+
const { signal } = options;
|
|
135
|
+
if (!(await acquire(signal))) {
|
|
136
|
+
return { cancelled: true, ruleIds: [] };
|
|
137
|
+
}
|
|
138
|
+
let released = Promise.resolve();
|
|
139
|
+
try {
|
|
140
|
+
const run = startScan(spec, sink, options);
|
|
141
|
+
released = run.released;
|
|
142
|
+
return await run.result;
|
|
143
|
+
}
|
|
144
|
+
finally {
|
|
145
|
+
await released;
|
|
146
|
+
release();
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
function startScan(spec, sink, options) {
|
|
150
|
+
const { signal } = options;
|
|
151
|
+
const heapLimit = options.maxHeapMb ?? 0;
|
|
152
|
+
const script = workerScript(options.workerUrl);
|
|
153
|
+
const worker = takeWorker(script, heapLimit);
|
|
154
|
+
started++;
|
|
155
|
+
const info = {
|
|
156
|
+
threadId: worker.threadId,
|
|
157
|
+
tool: spec.tool,
|
|
158
|
+
startedAt: new Date().toISOString(),
|
|
159
|
+
files: spec.files.length,
|
|
160
|
+
filesDone: 0,
|
|
161
|
+
};
|
|
162
|
+
running.set(info.threadId, info);
|
|
163
|
+
let markReleased;
|
|
164
|
+
const released = new Promise((resolve) => {
|
|
165
|
+
markReleased = resolve;
|
|
166
|
+
});
|
|
167
|
+
const exited = new Promise((resolve) => {
|
|
168
|
+
worker.once('exit', () => resolve());
|
|
169
|
+
});
|
|
170
|
+
const result = new Promise((resolve, reject) => {
|
|
171
|
+
let ruleIds = [];
|
|
172
|
+
let settled = false;
|
|
173
|
+
/**
|
|
174
|
+
* Ends this scan. A finished scan hands the worker back for the next one;
|
|
175
|
+
* otherwise the worker is terminated and released once it has exited.
|
|
176
|
+
*/
|
|
177
|
+
const finish = (outcome, keepWorker = false) => {
|
|
178
|
+
if (settled) {
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
settled = true;
|
|
182
|
+
signal?.removeEventListener('abort', onAbort);
|
|
183
|
+
worker.off('message', onMessage);
|
|
184
|
+
worker.off('error', onError);
|
|
185
|
+
worker.off('exit', onExit);
|
|
186
|
+
running.delete(info.threadId);
|
|
187
|
+
if (keepWorker) {
|
|
188
|
+
keepForNextScan(worker, script, heapLimit);
|
|
189
|
+
markReleased();
|
|
190
|
+
}
|
|
191
|
+
else {
|
|
192
|
+
void worker.terminate();
|
|
193
|
+
void exited.then(markReleased);
|
|
194
|
+
}
|
|
195
|
+
outcome();
|
|
196
|
+
};
|
|
197
|
+
const fail = (error) => {
|
|
198
|
+
if (!settled) {
|
|
199
|
+
failed++;
|
|
200
|
+
}
|
|
201
|
+
finish(() => reject(error));
|
|
202
|
+
};
|
|
203
|
+
const onAbort = () => {
|
|
204
|
+
if (!settled) {
|
|
205
|
+
cancelled++;
|
|
206
|
+
}
|
|
207
|
+
finish(() => resolve({ cancelled: true, ruleIds }));
|
|
208
|
+
};
|
|
209
|
+
const onMessage = (message) => {
|
|
210
|
+
if (settled) {
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
try {
|
|
214
|
+
switch (message.type) {
|
|
215
|
+
case 'ready':
|
|
216
|
+
ruleIds = message.ruleIds;
|
|
217
|
+
info.heapUsedMB = toMB(message.heapUsed);
|
|
218
|
+
sink.ready?.(message.ruleIds);
|
|
219
|
+
return;
|
|
220
|
+
case 'file':
|
|
221
|
+
info.filesDone++;
|
|
222
|
+
if (message.heapUsed !== undefined) {
|
|
223
|
+
info.heapUsedMB = toMB(message.heapUsed);
|
|
224
|
+
}
|
|
225
|
+
sink.file(message.event);
|
|
226
|
+
return;
|
|
227
|
+
case 'batch':
|
|
228
|
+
sink.batchDone?.(message.files);
|
|
229
|
+
return;
|
|
230
|
+
case 'done':
|
|
231
|
+
finish(() => resolve(message.result), true);
|
|
232
|
+
return;
|
|
233
|
+
case 'error': {
|
|
234
|
+
const error = new Error(message.message);
|
|
235
|
+
if (message.stack !== undefined) {
|
|
236
|
+
error.stack = message.stack;
|
|
237
|
+
}
|
|
238
|
+
fail(error);
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
catch (error) {
|
|
244
|
+
fail(error instanceof Error ? error : new Error(String(error)));
|
|
245
|
+
}
|
|
246
|
+
};
|
|
247
|
+
const onError = (error) => {
|
|
248
|
+
fail(error.code === 'ERR_WORKER_OUT_OF_MEMORY'
|
|
249
|
+
? new Error(`Scan worker ran out of memory (heap limit ${heapLimit} MB, --max-heap-mb). ` +
|
|
250
|
+
'Narrow the scope, lower maxFiles, or raise --max-heap-mb.')
|
|
251
|
+
: error);
|
|
252
|
+
};
|
|
253
|
+
const onExit = (code) => {
|
|
254
|
+
fail(new Error(`Scan worker exited with code ${code} before the scan finished`));
|
|
255
|
+
};
|
|
256
|
+
worker.on('message', onMessage);
|
|
257
|
+
worker.on('error', onError);
|
|
258
|
+
worker.on('exit', onExit);
|
|
259
|
+
if (signal?.aborted === true) {
|
|
260
|
+
onAbort();
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
signal?.addEventListener('abort', onAbort, { once: true });
|
|
264
|
+
const start = { type: 'start', spec };
|
|
265
|
+
worker.postMessage(start);
|
|
266
|
+
});
|
|
267
|
+
return { result, released };
|
|
268
|
+
}
|
|
269
|
+
/** The waiting worker when it runs the same script and limit, otherwise a new one. */
|
|
270
|
+
function takeWorker(script, maxHeapMb) {
|
|
271
|
+
const candidate = standby;
|
|
272
|
+
if (candidate !== undefined &&
|
|
273
|
+
!candidate.exited &&
|
|
274
|
+
candidate.script === scriptKey(script) &&
|
|
275
|
+
candidate.maxHeapMb === maxHeapMb) {
|
|
276
|
+
standby = undefined;
|
|
277
|
+
candidate.worker.ref();
|
|
278
|
+
return candidate.worker;
|
|
279
|
+
}
|
|
280
|
+
return spawnWorker(script, maxHeapMb);
|
|
281
|
+
}
|
|
282
|
+
/** Starts a waiting worker for the default script unless one waits already or the host is off. */
|
|
283
|
+
function prepareStandby() {
|
|
284
|
+
if (!settings.enabled || (standby !== undefined && !standby.exited)) {
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
const script = workerScript(undefined);
|
|
288
|
+
keepForNextScan(spawnWorker(script, settings.maxHeapMb), script, settings.maxHeapMb);
|
|
289
|
+
}
|
|
290
|
+
/** Parks `worker` as the waiting worker; an older waiting worker ends. */
|
|
291
|
+
function keepForNextScan(worker, script, maxHeapMb) {
|
|
292
|
+
if (standby !== undefined && standby.worker !== worker) {
|
|
293
|
+
void standby.worker.terminate();
|
|
294
|
+
}
|
|
295
|
+
const entry = { worker, script: scriptKey(script), maxHeapMb, exited: false };
|
|
296
|
+
worker.once('exit', () => {
|
|
297
|
+
entry.exited = true;
|
|
298
|
+
if (standby === entry) {
|
|
299
|
+
standby = undefined;
|
|
300
|
+
}
|
|
301
|
+
});
|
|
302
|
+
// A waiting worker must not keep the process alive
|
|
303
|
+
worker.unref();
|
|
304
|
+
standby = entry;
|
|
305
|
+
}
|
|
306
|
+
function spawnWorker(script, maxHeapMb) {
|
|
307
|
+
const worker = new Worker(script.script, {
|
|
308
|
+
...(script.eval === true ? { eval: true } : {}),
|
|
309
|
+
// Worker stdout would land on the parent's stdout, the JSON-RPC channel
|
|
310
|
+
stdout: true,
|
|
311
|
+
stderr: false,
|
|
312
|
+
...(maxHeapMb > 0 ? { resourceLimits: { maxOldGenerationSizeMb: maxHeapMb } } : {}),
|
|
313
|
+
});
|
|
314
|
+
worker.stdout.pipe(process.stderr, { end: false });
|
|
315
|
+
return worker;
|
|
316
|
+
}
|
|
317
|
+
/** Waits for a free slot; `false` when the signal aborts first. */
|
|
318
|
+
function acquire(signal) {
|
|
319
|
+
if (signal?.aborted === true) {
|
|
320
|
+
return Promise.resolve(false);
|
|
321
|
+
}
|
|
322
|
+
if (active < MAX_RUNNING) {
|
|
323
|
+
active++;
|
|
324
|
+
return Promise.resolve(true);
|
|
325
|
+
}
|
|
326
|
+
return new Promise((resolve) => {
|
|
327
|
+
const entry = {
|
|
328
|
+
start: () => {
|
|
329
|
+
signal?.removeEventListener('abort', onAbort);
|
|
330
|
+
active++;
|
|
331
|
+
resolve(true);
|
|
332
|
+
},
|
|
333
|
+
};
|
|
334
|
+
const onAbort = () => {
|
|
335
|
+
const index = waiting.indexOf(entry);
|
|
336
|
+
if (index >= 0) {
|
|
337
|
+
waiting.splice(index, 1);
|
|
338
|
+
}
|
|
339
|
+
resolve(false);
|
|
340
|
+
};
|
|
341
|
+
waiting.push(entry);
|
|
342
|
+
signal?.addEventListener('abort', onAbort, { once: true });
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
function release() {
|
|
346
|
+
active--;
|
|
347
|
+
waiting.shift()?.start();
|
|
348
|
+
}
|
|
349
|
+
function workerScript(workerUrl) {
|
|
350
|
+
return workerUrl === undefined ? defaultWorkerScript() : { script: workerUrl };
|
|
351
|
+
}
|
|
352
|
+
function scriptKey(script) {
|
|
353
|
+
return script.script instanceof URL ? script.script.href : script.script;
|
|
354
|
+
}
|
|
355
|
+
/**
|
|
356
|
+
* The worker script next to this module. From source (tsx, the stdio
|
|
357
|
+
* integration tests) the worker gets a bootstrap that registers tsx first:
|
|
358
|
+
* tsx's `--import` loader, which the worker inherits, does not register its
|
|
359
|
+
* hooks in worker threads, so `.js` imports of `.ts` files would not resolve.
|
|
360
|
+
*/
|
|
361
|
+
function defaultWorkerScript() {
|
|
362
|
+
if (!import.meta.url.endsWith('.ts')) {
|
|
363
|
+
return { script: new URL('./scan-worker.js', import.meta.url) };
|
|
364
|
+
}
|
|
365
|
+
const tsxApi = import.meta.resolve('tsx/esm/api');
|
|
366
|
+
const entry = new URL('./scan-worker.ts', import.meta.url).href;
|
|
367
|
+
return {
|
|
368
|
+
script: `import(${JSON.stringify(tsxApi)})` +
|
|
369
|
+
`.then((api) => { api.register(); return import(${JSON.stringify(entry)}); });`,
|
|
370
|
+
eval: true,
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
function toMB(bytes) {
|
|
374
|
+
return Math.round(bytes / (1024 * 1024));
|
|
375
|
+
}
|
|
376
|
+
//# sourceMappingURL=scan-host.js.map
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @angular-modernizer/adapter-mcp - Messages between scan host and scan worker
|
|
3
|
+
*/
|
|
4
|
+
import type { ScanEngineResult, ScanFileEvent, ScanSpec } from '../tools/helpers/scan-engine.js';
|
|
5
|
+
/**
|
|
6
|
+
* Message from the host to a scan worker. A worker starts without its scan
|
|
7
|
+
* (standby) and runs the one `start` it gets.
|
|
8
|
+
*/
|
|
9
|
+
export interface ScanHostMessage {
|
|
10
|
+
type: 'start';
|
|
11
|
+
spec: ScanSpec;
|
|
12
|
+
}
|
|
13
|
+
/** Messages from a scan worker to the host. */
|
|
14
|
+
export type ScanWorkerMessage = {
|
|
15
|
+
type: 'ready';
|
|
16
|
+
ruleIds: string[];
|
|
17
|
+
heapUsed: number;
|
|
18
|
+
} | {
|
|
19
|
+
type: 'file';
|
|
20
|
+
event: ScanFileEvent;
|
|
21
|
+
heapUsed?: number;
|
|
22
|
+
} | {
|
|
23
|
+
type: 'batch';
|
|
24
|
+
files: number;
|
|
25
|
+
} | {
|
|
26
|
+
type: 'done';
|
|
27
|
+
result: ScanEngineResult;
|
|
28
|
+
} | {
|
|
29
|
+
type: 'error';
|
|
30
|
+
message: string;
|
|
31
|
+
stack?: string;
|
|
32
|
+
};
|
|
33
|
+
/** A worker reports its heap with every n-th file. */
|
|
34
|
+
export declare const HEAP_REPORT_EVERY = 20;
|
|
35
|
+
//# sourceMappingURL=scan-protocol.d.ts.map
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @angular-modernizer/adapter-mcp - Scan worker entry
|
|
3
|
+
*
|
|
4
|
+
* Loads the scan engine (ts-morph, core, api) on start, then runs one scan
|
|
5
|
+
* per `start` message from the host (`scan-host.ts`), one after the other,
|
|
6
|
+
* each with its own kernel, and posts each analyzed file. The worker stays
|
|
7
|
+
* alive between scans so its code stays optimized; the host ends it on a
|
|
8
|
+
* cancel, an error or the heap limit. Findings go through a JSON round
|
|
9
|
+
* trip before they are posted: the scan cache stores them as JSON anyway, and
|
|
10
|
+
* a function or class instance in `metadata` would otherwise make
|
|
11
|
+
* `postMessage` throw a `DataCloneError`.
|
|
12
|
+
*/
|
|
13
|
+
export {};
|
|
14
|
+
//# sourceMappingURL=scan-worker.d.ts.map
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @angular-modernizer/adapter-mcp - Scan worker entry
|
|
3
|
+
*
|
|
4
|
+
* Loads the scan engine (ts-morph, core, api) on start, then runs one scan
|
|
5
|
+
* per `start` message from the host (`scan-host.ts`), one after the other,
|
|
6
|
+
* each with its own kernel, and posts each analyzed file. The worker stays
|
|
7
|
+
* alive between scans so its code stays optimized; the host ends it on a
|
|
8
|
+
* cancel, an error or the heap limit. Findings go through a JSON round
|
|
9
|
+
* trip before they are posted: the scan cache stores them as JSON anyway, and
|
|
10
|
+
* a function or class instance in `metadata` would otherwise make
|
|
11
|
+
* `postMessage` throw a `DataCloneError`.
|
|
12
|
+
*/
|
|
13
|
+
import { parentPort } from 'node:worker_threads';
|
|
14
|
+
import { routeConsoleToStderr } from '../server/stdio-guard.js';
|
|
15
|
+
import { runScanEngine } from '../tools/helpers/scan-engine.js';
|
|
16
|
+
import { HEAP_REPORT_EVERY, } from './scan-protocol.js';
|
|
17
|
+
// The host pipes the worker's stdout to stderr as well; this keeps console
|
|
18
|
+
// output off stdout even before that pipe is attached.
|
|
19
|
+
routeConsoleToStderr();
|
|
20
|
+
const port = parentPort;
|
|
21
|
+
if (port === null) {
|
|
22
|
+
throw new Error('scan-worker must run as a worker thread');
|
|
23
|
+
}
|
|
24
|
+
const post = (message) => port.postMessage(message);
|
|
25
|
+
port.on('message', (message) => {
|
|
26
|
+
let files = 0;
|
|
27
|
+
runScanEngine(message.spec, {
|
|
28
|
+
ready: (ruleIds) => post({ type: 'ready', ruleIds, heapUsed: process.memoryUsage().heapUsed }),
|
|
29
|
+
file: (event) => {
|
|
30
|
+
files++;
|
|
31
|
+
post({
|
|
32
|
+
type: 'file',
|
|
33
|
+
event: JSON.parse(JSON.stringify(event)),
|
|
34
|
+
...(files % HEAP_REPORT_EVERY === 0 ? { heapUsed: process.memoryUsage().heapUsed } : {}),
|
|
35
|
+
});
|
|
36
|
+
},
|
|
37
|
+
batchDone: (count) => post({ type: 'batch', files: count }),
|
|
38
|
+
}).then((result) => {
|
|
39
|
+
post({ type: 'done', result });
|
|
40
|
+
// The scan's project is garbage now; with --expose-gc free it before the next scan
|
|
41
|
+
globalThis.gc?.();
|
|
42
|
+
}, (error) => post({
|
|
43
|
+
type: 'error',
|
|
44
|
+
message: error instanceof Error ? error.message : String(error),
|
|
45
|
+
...(error instanceof Error && error.stack !== undefined ? { stack: error.stack } : {}),
|
|
46
|
+
}));
|
|
47
|
+
});
|
|
48
|
+
//# sourceMappingURL=scan-worker.js.map
|
|
@@ -20,6 +20,7 @@ import { type JSONRPCMessage, type RequestId } from '@modelcontextprotocol/serve
|
|
|
20
20
|
export declare const MAX_RESPONSE_BYTES: number;
|
|
21
21
|
/** One response sent on stdout. */
|
|
22
22
|
export interface ResponseRecord {
|
|
23
|
+
/** JSON-RPC id of the response. */
|
|
23
24
|
id: string | number | null;
|
|
24
25
|
/** Tool behind a tools/call response. */
|
|
25
26
|
tool?: string;
|
|
@@ -27,14 +28,20 @@ export interface ResponseRecord {
|
|
|
27
28
|
bytes: number;
|
|
28
29
|
/** The response exceeded the limit and was replaced by an error. */
|
|
29
30
|
replaced: boolean;
|
|
31
|
+
/** When the response was sent (ISO 8601). */
|
|
30
32
|
at: string;
|
|
31
33
|
}
|
|
32
34
|
/** Response sizes since the server started, for `server-status`. */
|
|
33
35
|
export interface ResponseStats {
|
|
36
|
+
/** Number of responses sent. */
|
|
34
37
|
count: number;
|
|
38
|
+
/** Sum of all response sizes in bytes. */
|
|
35
39
|
totalBytes: number;
|
|
40
|
+
/** Number of responses over the limit that were replaced by an error. */
|
|
36
41
|
oversized: number;
|
|
42
|
+
/** The most recent response. */
|
|
37
43
|
last?: ResponseRecord;
|
|
44
|
+
/** The largest response so far. */
|
|
38
45
|
largest?: ResponseRecord;
|
|
39
46
|
}
|
|
40
47
|
export interface LimitedStdioTransportOptions {
|
|
@@ -1,17 +1,13 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @angular-modernizer/adapter-mcp - MCP Server
|
|
3
|
-
*
|
|
4
|
-
* Owns the stdio server lifecycle. The protocol is served by the MCP SDK
|
|
5
|
-
* (`serveStdio`, both eras) over a size-limited stdio transport; the tools
|
|
6
|
-
* come from the tool registry.
|
|
7
|
-
*/
|
|
8
1
|
import { type ProtocolInfo } from './sdk-server.js';
|
|
9
2
|
import { type ResponseStats } from './limited-stdio-transport.js';
|
|
10
3
|
import { type MCPTool, type MCPServerConfig } from '../types.js';
|
|
11
4
|
/** Runtime figures for the `server-status` tool. */
|
|
12
5
|
export interface ServerRuntimeStats {
|
|
6
|
+
/** Start time of the server (ISO 8601). */
|
|
13
7
|
startedAt: string;
|
|
8
|
+
/** Number of registered tools. */
|
|
14
9
|
toolCount: number;
|
|
10
|
+
/** Response sizes measured by the stdio transport. */
|
|
15
11
|
responses: ResponseStats;
|
|
16
12
|
/** Era and protocol revision of the connection, once a client spoke. */
|
|
17
13
|
protocol: ProtocolInfo;
|
|
@@ -20,6 +16,10 @@ export interface ServerRuntimeStats {
|
|
|
20
16
|
* Main MCP server class that handles the complete MCP protocol lifecycle.
|
|
21
17
|
*
|
|
22
18
|
* @remarks
|
|
19
|
+
* Owns the stdio server lifecycle. The protocol is served by the MCP SDK
|
|
20
|
+
* (`serveStdio`, both eras) over a size-limited stdio transport; the tools
|
|
21
|
+
* come from the tool registry.
|
|
22
|
+
*
|
|
23
23
|
* The stdio server ends when the client goes away: stdin `end`, `close` or
|
|
24
24
|
* `error`, a failing stdout (EPIPE), SIGINT, SIGTERM, SIGHUP, or its parent
|
|
25
25
|
* process disappearing. The last check covers clients that die while another
|
|
@@ -57,9 +57,13 @@ export declare class MCPServer {
|
|
|
57
57
|
* Get server statistics.
|
|
58
58
|
*/
|
|
59
59
|
getStats(): {
|
|
60
|
+
/** Whether `start()` ran and the server has not stopped. */
|
|
60
61
|
running: boolean;
|
|
62
|
+
/** Registered tools. */
|
|
61
63
|
tools: {
|
|
64
|
+
/** Number of registered tools. */
|
|
62
65
|
totalTools: number;
|
|
66
|
+
/** Names of the registered tools. */
|
|
63
67
|
toolNames: string[];
|
|
64
68
|
};
|
|
65
69
|
};
|