baseguard 1.0.2 → 1.0.4
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/.baseguardrc.example.json +63 -63
- package/.eslintrc.json +24 -24
- package/.prettierrc +7 -7
- package/CHANGELOG.md +195 -195
- package/DEPLOYMENT.md +624 -624
- package/DEPLOYMENT_CHECKLIST.md +239 -239
- package/DEPLOYMENT_SUMMARY_v1.0.2.md +202 -202
- package/QUICK_START.md +134 -134
- package/README.md +488 -488
- package/RELEASE_NOTES_v1.0.2.md +434 -434
- package/bin/base.js +628 -613
- package/dist/ai/fix-manager.d.ts.map +1 -1
- package/dist/ai/fix-manager.js +1 -1
- package/dist/ai/fix-manager.js.map +1 -1
- package/dist/ai/gemini-analyzer.d.ts.map +1 -1
- package/dist/ai/gemini-analyzer.js +29 -35
- package/dist/ai/gemini-analyzer.js.map +1 -1
- package/dist/ai/gemini-code-fixer.d.ts.map +1 -1
- package/dist/ai/gemini-code-fixer.js +58 -58
- package/dist/ai/gemini-code-fixer.js.map +1 -1
- package/dist/ai/jules-implementer.d.ts +3 -0
- package/dist/ai/jules-implementer.d.ts.map +1 -1
- package/dist/ai/jules-implementer.js +63 -32
- package/dist/ai/jules-implementer.js.map +1 -1
- package/dist/ai/unified-code-fixer.js.map +1 -1
- package/dist/commands/check.d.ts.map +1 -1
- package/dist/commands/check.js +1 -1
- package/dist/commands/check.js.map +1 -1
- package/dist/commands/config.js +2 -1
- package/dist/commands/config.js.map +1 -1
- package/dist/commands/fix.d.ts.map +1 -1
- package/dist/commands/fix.js +44 -15
- package/dist/commands/fix.js.map +1 -1
- package/dist/core/api-key-manager.js +2 -2
- package/dist/core/api-key-manager.js.map +1 -1
- package/dist/core/baseguard.d.ts +1 -0
- package/dist/core/baseguard.d.ts.map +1 -1
- package/dist/core/baseguard.js +13 -10
- package/dist/core/baseguard.js.map +1 -1
- package/dist/core/baseline-checker.d.ts.map +1 -1
- package/dist/core/baseline-checker.js +2 -1
- package/dist/core/baseline-checker.js.map +1 -1
- package/dist/core/configuration-recovery.d.ts.map +1 -1
- package/dist/core/configuration-recovery.js +1 -1
- package/dist/core/configuration-recovery.js.map +1 -1
- package/dist/core/debug-logger.d.ts.map +1 -1
- package/dist/core/debug-logger.js +1 -1
- package/dist/core/debug-logger.js.map +1 -1
- package/dist/core/error-handler.d.ts.map +1 -1
- package/dist/core/error-handler.js +2 -1
- package/dist/core/error-handler.js.map +1 -1
- package/dist/core/gitignore-manager.js +5 -5
- package/dist/core/graceful-degradation-manager.d.ts.map +1 -1
- package/dist/core/graceful-degradation-manager.js +16 -16
- package/dist/core/graceful-degradation-manager.js.map +1 -1
- package/dist/core/lazy-loader.d.ts.map +1 -1
- package/dist/core/lazy-loader.js +9 -2
- package/dist/core/lazy-loader.js.map +1 -1
- package/dist/core/memory-manager.d.ts +0 -3
- package/dist/core/memory-manager.d.ts.map +1 -1
- package/dist/core/memory-manager.js.map +1 -1
- package/dist/core/parser-worker.d.ts +2 -0
- package/dist/core/parser-worker.d.ts.map +1 -0
- package/dist/core/parser-worker.js +19 -0
- package/dist/core/parser-worker.js.map +1 -0
- package/dist/core/startup-optimizer.d.ts +2 -0
- package/dist/core/startup-optimizer.d.ts.map +1 -1
- package/dist/core/startup-optimizer.js +19 -12
- package/dist/core/startup-optimizer.js.map +1 -1
- package/dist/core/system-error-handler.d.ts.map +1 -1
- package/dist/core/system-error-handler.js +18 -11
- package/dist/core/system-error-handler.js.map +1 -1
- package/dist/git/automation-engine.d.ts.map +1 -1
- package/dist/git/automation-engine.js +5 -4
- package/dist/git/automation-engine.js.map +1 -1
- package/dist/git/github-manager.d.ts.map +1 -1
- package/dist/git/github-manager.js.map +1 -1
- package/dist/git/hook-manager.js +5 -5
- package/dist/git/hook-manager.js.map +1 -1
- package/dist/parsers/parser-manager.d.ts.map +1 -1
- package/dist/parsers/parser-manager.js +1 -1
- package/dist/parsers/parser-manager.js.map +1 -1
- package/dist/parsers/svelte-parser.js +1 -1
- package/dist/parsers/svelte-parser.js.map +1 -1
- package/dist/parsers/vanilla-parser.d.ts.map +1 -1
- package/dist/parsers/vanilla-parser.js.map +1 -1
- package/dist/parsers/vue-parser.d.ts.map +1 -1
- package/dist/parsers/vue-parser.js.map +1 -1
- package/dist/ui/components.d.ts +1 -1
- package/dist/ui/components.d.ts.map +1 -1
- package/dist/ui/components.js +11 -11
- package/dist/ui/components.js.map +1 -1
- package/dist/ui/terminal-header.js +14 -14
- package/package.json +105 -105
- package/src/ai/__tests__/gemini-analyzer.test.ts +180 -180
- package/src/ai/agentkit-orchestrator.ts +533 -533
- package/src/ai/fix-manager.ts +362 -362
- package/src/ai/gemini-analyzer.ts +665 -671
- package/src/ai/gemini-code-fixer.ts +539 -540
- package/src/ai/index.ts +3 -3
- package/src/ai/jules-implementer.ts +504 -460
- package/src/ai/unified-code-fixer.ts +347 -347
- package/src/commands/automation.ts +343 -343
- package/src/commands/check.ts +298 -299
- package/src/commands/config.ts +584 -583
- package/src/commands/fix.ts +264 -238
- package/src/commands/index.ts +6 -6
- package/src/commands/init.ts +155 -155
- package/src/commands/status.ts +306 -306
- package/src/core/api-key-manager.ts +298 -298
- package/src/core/baseguard.ts +757 -756
- package/src/core/baseline-checker.ts +564 -563
- package/src/core/cache-manager.ts +271 -271
- package/src/core/configuration-recovery.ts +672 -673
- package/src/core/configuration.ts +595 -595
- package/src/core/debug-logger.ts +590 -590
- package/src/core/directory-filter.ts +420 -420
- package/src/core/error-handler.ts +518 -517
- package/src/core/file-processor.ts +337 -337
- package/src/core/gitignore-manager.ts +168 -168
- package/src/core/graceful-degradation-manager.ts +596 -596
- package/src/core/index.ts +16 -16
- package/src/core/lazy-loader.ts +317 -307
- package/src/core/memory-manager.ts +290 -295
- package/src/core/parser-worker.ts +33 -0
- package/src/core/startup-optimizer.ts +246 -243
- package/src/core/system-error-handler.ts +755 -750
- package/src/git/automation-engine.ts +361 -361
- package/src/git/github-manager.ts +190 -192
- package/src/git/hook-manager.ts +210 -210
- package/src/git/index.ts +3 -3
- package/src/index.ts +7 -7
- package/src/parsers/feature-validator.ts +558 -558
- package/src/parsers/index.ts +7 -7
- package/src/parsers/parser-manager.ts +418 -419
- package/src/parsers/parser.ts +25 -25
- package/src/parsers/react-parser-optimized.ts +160 -160
- package/src/parsers/react-parser.ts +358 -358
- package/src/parsers/svelte-parser.ts +510 -510
- package/src/parsers/vanilla-parser.ts +685 -686
- package/src/parsers/vue-parser.ts +476 -478
- package/src/types/index.ts +95 -95
- package/src/ui/components.ts +567 -567
- package/src/ui/help.ts +192 -192
- package/src/ui/index.ts +3 -3
- package/src/ui/prompts.ts +680 -680
- package/src/ui/terminal-header.ts +58 -58
- package/test-build.js +40 -40
- package/test-config-commands.js +55 -55
- package/test-header-simple.js +32 -32
- package/test-terminal-header.js +11 -11
- package/test-ui.js +28 -28
- package/tests/e2e/baseguard.e2e.test.ts +515 -515
- package/tests/e2e/cross-platform.e2e.test.ts +419 -419
- package/tests/e2e/git-integration.e2e.test.ts +486 -486
- package/tests/fixtures/react-project/package.json +13 -13
- package/tests/fixtures/react-project/src/App.css +75 -75
- package/tests/fixtures/react-project/src/App.tsx +76 -76
- package/tests/fixtures/svelte-project/package.json +10 -10
- package/tests/fixtures/svelte-project/src/App.svelte +368 -368
- package/tests/fixtures/vanilla-project/index.html +75 -75
- package/tests/fixtures/vanilla-project/script.js +330 -330
- package/tests/fixtures/vanilla-project/styles.css +358 -358
- package/tests/fixtures/vue-project/package.json +11 -11
- package/tests/fixtures/vue-project/src/App.vue +215 -215
- package/tsconfig.json +34 -34
- package/vitest.config.ts +11 -11
- package/dist/terminal-header.d.ts +0 -12
- package/dist/terminal-header.js +0 -45
|
@@ -1,338 +1,338 @@
|
|
|
1
|
-
import { Worker } from 'worker_threads';
|
|
2
|
-
import { cpus } from 'os';
|
|
3
|
-
import { join, dirname } from 'path';
|
|
4
|
-
import { fileURLToPath } from 'url';
|
|
5
|
-
import type { DetectedFeature } from '../types/index.js';
|
|
6
|
-
import { CacheManager } from './cache-manager.js';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Worker task for file parsing
|
|
10
|
-
*/
|
|
11
|
-
interface WorkerTask {
|
|
12
|
-
id: string;
|
|
13
|
-
filePath: string;
|
|
14
|
-
content?: string;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Worker result
|
|
19
|
-
*/
|
|
20
|
-
interface WorkerResult {
|
|
21
|
-
id: string;
|
|
22
|
-
features: DetectedFeature[];
|
|
23
|
-
error?: string;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* File processor with concurrent parsing using worker threads
|
|
28
|
-
*/
|
|
29
|
-
export class FileProcessor {
|
|
30
|
-
private readonly maxWorkers: number;
|
|
31
|
-
private readonly cacheManager: CacheManager;
|
|
32
|
-
private workers: Worker[] = [];
|
|
33
|
-
private taskQueue: WorkerTask[] = [];
|
|
34
|
-
private pendingTasks = new Map<string, {
|
|
35
|
-
resolve: (features: DetectedFeature[]) => void;
|
|
36
|
-
reject: (error: Error) => void;
|
|
37
|
-
}>();
|
|
38
|
-
private isProcessing = false;
|
|
39
|
-
|
|
40
|
-
constructor(options: {
|
|
41
|
-
maxWorkers?: number;
|
|
42
|
-
cacheManager?: CacheManager;
|
|
43
|
-
} = {}) {
|
|
44
|
-
this.maxWorkers = options.maxWorkers || Math.min(cpus().length, 8);
|
|
45
|
-
this.cacheManager = options.cacheManager || new CacheManager();
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Process multiple files concurrently with caching
|
|
50
|
-
*/
|
|
51
|
-
async processFiles(filePaths: string[]): Promise<DetectedFeature[]> {
|
|
52
|
-
// Check cache for unchanged files
|
|
53
|
-
const { changed, unchanged } = await this.cacheManager.getChangedFiles(filePaths);
|
|
54
|
-
|
|
55
|
-
// Get cached results for unchanged files
|
|
56
|
-
const cachedFeatures: DetectedFeature[] = [];
|
|
57
|
-
for (const filePath of unchanged) {
|
|
58
|
-
const cached = await this.cacheManager.getCachedParseResult(filePath);
|
|
59
|
-
if (cached) {
|
|
60
|
-
cachedFeatures.push(...cached);
|
|
61
|
-
} else {
|
|
62
|
-
// Cache miss, need to process
|
|
63
|
-
changed.push(filePath);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
// Process changed files
|
|
68
|
-
const newFeatures = changed.length > 0
|
|
69
|
-
? await this.processFilesWithWorkers(changed)
|
|
70
|
-
: [];
|
|
71
|
-
|
|
72
|
-
return [...cachedFeatures, ...newFeatures];
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Process files using worker threads
|
|
77
|
-
*/
|
|
78
|
-
private async processFilesWithWorkers(filePaths: string[]): Promise<DetectedFeature[]> {
|
|
79
|
-
if (filePaths.length === 0) {
|
|
80
|
-
return [];
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
// Create worker tasks
|
|
84
|
-
const tasks: WorkerTask[] = filePaths.map((filePath, index) => ({
|
|
85
|
-
id: `task_${index}_${Date.now()}`,
|
|
86
|
-
filePath
|
|
87
|
-
}));
|
|
88
|
-
|
|
89
|
-
// Process tasks in batches
|
|
90
|
-
const batchSize = Math.max(1, Math.ceil(tasks.length / this.maxWorkers));
|
|
91
|
-
const batches: WorkerTask[][] = [];
|
|
92
|
-
|
|
93
|
-
for (let i = 0; i < tasks.length; i += batchSize) {
|
|
94
|
-
batches.push(tasks.slice(i, i + batchSize));
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
const allFeatures: DetectedFeature[] = [];
|
|
98
|
-
|
|
99
|
-
// Process each batch
|
|
100
|
-
for (const batch of batches) {
|
|
101
|
-
const batchResults = await Promise.allSettled(
|
|
102
|
-
batch.map(task => this.processTask(task))
|
|
103
|
-
);
|
|
104
|
-
|
|
105
|
-
batchResults.forEach((result, index) => {
|
|
106
|
-
const task = batch[index];
|
|
107
|
-
if (!task) return;
|
|
108
|
-
|
|
109
|
-
if (result.status === 'fulfilled') {
|
|
110
|
-
allFeatures.push(...result.value);
|
|
111
|
-
// Cache the result
|
|
112
|
-
this.cacheManager.setCachedParseResult(task.filePath, result.value);
|
|
113
|
-
} else {
|
|
114
|
-
console.warn(`Failed to process ${task.filePath}: ${result.reason}`);
|
|
115
|
-
}
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
// Small delay between batches to prevent overwhelming
|
|
119
|
-
if (batches.length > 1) {
|
|
120
|
-
await this.sleep(10);
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
return allFeatures;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* Process a single task
|
|
129
|
-
*/
|
|
130
|
-
private async processTask(task: WorkerTask): Promise<DetectedFeature[]> {
|
|
131
|
-
return new Promise((resolve, reject) => {
|
|
132
|
-
// Check cache first
|
|
133
|
-
this.cacheManager.getCachedParseResult(task.filePath)
|
|
134
|
-
.then(cached => {
|
|
135
|
-
if (cached) {
|
|
136
|
-
resolve(cached);
|
|
137
|
-
return;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
// Add to pending tasks
|
|
141
|
-
this.pendingTasks.set(task.id, { resolve, reject });
|
|
142
|
-
this.taskQueue.push(task);
|
|
143
|
-
|
|
144
|
-
// Start processing if not already running
|
|
145
|
-
if (!this.isProcessing) {
|
|
146
|
-
this.startProcessing();
|
|
147
|
-
}
|
|
148
|
-
})
|
|
149
|
-
.catch(reject);
|
|
150
|
-
});
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
/**
|
|
154
|
-
* Start worker processing
|
|
155
|
-
*/
|
|
156
|
-
private async startProcessing(): Promise<void> {
|
|
157
|
-
if (this.isProcessing) {
|
|
158
|
-
return;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
this.isProcessing = true;
|
|
162
|
-
|
|
163
|
-
try {
|
|
164
|
-
// Initialize workers
|
|
165
|
-
await this.initializeWorkers();
|
|
166
|
-
|
|
167
|
-
// Process tasks
|
|
168
|
-
while (this.taskQueue.length > 0 || this.pendingTasks.size > 0) {
|
|
169
|
-
await this.processBatch();
|
|
170
|
-
await this.sleep(10);
|
|
171
|
-
}
|
|
172
|
-
} finally {
|
|
173
|
-
// Cleanup
|
|
174
|
-
await this.terminateWorkers();
|
|
175
|
-
this.isProcessing = false;
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
/**
|
|
180
|
-
* Initialize worker threads
|
|
181
|
-
*/
|
|
182
|
-
private async initializeWorkers(): Promise<void> {
|
|
183
|
-
const workerScript = this.getWorkerScriptPath();
|
|
184
|
-
const numWorkers = Math.min(this.maxWorkers, this.taskQueue.length);
|
|
185
|
-
|
|
186
|
-
for (let i = 0; i < numWorkers; i++) {
|
|
187
|
-
try {
|
|
188
|
-
const worker = new Worker(workerScript);
|
|
189
|
-
|
|
190
|
-
worker.on('message', (result: WorkerResult) => {
|
|
191
|
-
this.handleWorkerResult(result);
|
|
192
|
-
});
|
|
193
|
-
|
|
194
|
-
worker.on('error', (error) => {
|
|
195
|
-
console.error(`Worker error: ${error.message}`);
|
|
196
|
-
});
|
|
197
|
-
|
|
198
|
-
this.workers.push(worker);
|
|
199
|
-
} catch (error) {
|
|
200
|
-
console.warn(`Failed to create worker: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
/**
|
|
206
|
-
* Process a batch of tasks with available workers
|
|
207
|
-
*/
|
|
208
|
-
private async processBatch(): Promise<void> {
|
|
209
|
-
const availableWorkers = this.workers.length;
|
|
210
|
-
const tasksToProcess = this.taskQueue.splice(0, availableWorkers);
|
|
211
|
-
|
|
212
|
-
if (tasksToProcess.length === 0) {
|
|
213
|
-
return;
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
// Assign tasks to workers
|
|
217
|
-
const promises = tasksToProcess.map((task, index) => {
|
|
218
|
-
const worker = this.workers[index % this.workers.length];
|
|
219
|
-
return this.assignTaskToWorker(worker, task);
|
|
220
|
-
});
|
|
221
|
-
|
|
222
|
-
await Promise.allSettled(promises);
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
/**
|
|
226
|
-
* Assign task to worker
|
|
227
|
-
*/
|
|
228
|
-
private async assignTaskToWorker(worker: Worker | undefined, task: WorkerTask): Promise<void> {
|
|
229
|
-
if (!worker) {
|
|
230
|
-
throw new Error('Worker is not available');
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
return new Promise((resolve) => {
|
|
234
|
-
const timeout = setTimeout(() => {
|
|
235
|
-
console.warn(`Worker task ${task.id} timed out`);
|
|
236
|
-
this.handleWorkerError(task.id, new Error('Worker timeout'));
|
|
237
|
-
resolve();
|
|
238
|
-
}, 30000); // 30 second timeout
|
|
239
|
-
|
|
240
|
-
const messageHandler = (result: WorkerResult) => {
|
|
241
|
-
if (result.id === task.id) {
|
|
242
|
-
clearTimeout(timeout);
|
|
243
|
-
worker.off('message', messageHandler);
|
|
244
|
-
resolve();
|
|
245
|
-
}
|
|
246
|
-
};
|
|
247
|
-
|
|
248
|
-
worker.on('message', messageHandler);
|
|
249
|
-
worker.postMessage(task);
|
|
250
|
-
});
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
/**
|
|
254
|
-
* Handle worker result
|
|
255
|
-
*/
|
|
256
|
-
private handleWorkerResult(result: WorkerResult): void {
|
|
257
|
-
const pending = this.pendingTasks.get(result.id);
|
|
258
|
-
if (!pending) {
|
|
259
|
-
return;
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
this.pendingTasks.delete(result.id);
|
|
263
|
-
|
|
264
|
-
if (result.error) {
|
|
265
|
-
pending.reject(new Error(result.error));
|
|
266
|
-
} else {
|
|
267
|
-
pending.resolve(result.features);
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
/**
|
|
272
|
-
* Handle worker error
|
|
273
|
-
*/
|
|
274
|
-
private handleWorkerError(taskId: string, error: Error): void {
|
|
275
|
-
const pending = this.pendingTasks.get(taskId);
|
|
276
|
-
if (pending) {
|
|
277
|
-
this.pendingTasks.delete(taskId);
|
|
278
|
-
pending.reject(error);
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
/**
|
|
283
|
-
* Terminate all workers
|
|
284
|
-
*/
|
|
285
|
-
private async terminateWorkers(): Promise<void> {
|
|
286
|
-
await Promise.all(
|
|
287
|
-
this.workers.map(worker => worker.terminate())
|
|
288
|
-
);
|
|
289
|
-
this.workers = [];
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
/**
|
|
293
|
-
* Get worker script path
|
|
294
|
-
*/
|
|
295
|
-
private getWorkerScriptPath(): string {
|
|
296
|
-
// For now, we'll use a simple fallback since we don't have worker threads implemented yet
|
|
297
|
-
// In a real implementation, this would point to a separate worker script
|
|
298
|
-
const __filename = fileURLToPath(import.meta.url);
|
|
299
|
-
const __dirname = dirname(__filename);
|
|
300
|
-
return join(__dirname, 'parser-worker.js');
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
/**
|
|
304
|
-
* Sleep utility
|
|
305
|
-
*/
|
|
306
|
-
private sleep(ms: number): Promise<void> {
|
|
307
|
-
return new Promise(resolve => setTimeout(resolve, ms));
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
/**
|
|
311
|
-
* Get processing statistics
|
|
312
|
-
*/
|
|
313
|
-
getStats(): {
|
|
314
|
-
maxWorkers: number;
|
|
315
|
-
activeWorkers: number;
|
|
316
|
-
queuedTasks: number;
|
|
317
|
-
pendingTasks: number;
|
|
318
|
-
cacheStats: any;
|
|
319
|
-
} {
|
|
320
|
-
return {
|
|
321
|
-
maxWorkers: this.maxWorkers,
|
|
322
|
-
activeWorkers: this.workers.length,
|
|
323
|
-
queuedTasks: this.taskQueue.length,
|
|
324
|
-
pendingTasks: this.pendingTasks.size,
|
|
325
|
-
cacheStats: this.cacheManager.getStats()
|
|
326
|
-
};
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
/**
|
|
330
|
-
* Cleanup resources
|
|
331
|
-
*/
|
|
332
|
-
async cleanup(): Promise<void> {
|
|
333
|
-
await this.terminateWorkers();
|
|
334
|
-
this.taskQueue = [];
|
|
335
|
-
this.pendingTasks.clear();
|
|
336
|
-
this.isProcessing = false;
|
|
337
|
-
}
|
|
1
|
+
import { Worker } from 'worker_threads';
|
|
2
|
+
import { cpus } from 'os';
|
|
3
|
+
import { join, dirname } from 'path';
|
|
4
|
+
import { fileURLToPath } from 'url';
|
|
5
|
+
import type { DetectedFeature } from '../types/index.js';
|
|
6
|
+
import { CacheManager } from './cache-manager.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Worker task for file parsing
|
|
10
|
+
*/
|
|
11
|
+
interface WorkerTask {
|
|
12
|
+
id: string;
|
|
13
|
+
filePath: string;
|
|
14
|
+
content?: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Worker result
|
|
19
|
+
*/
|
|
20
|
+
interface WorkerResult {
|
|
21
|
+
id: string;
|
|
22
|
+
features: DetectedFeature[];
|
|
23
|
+
error?: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* File processor with concurrent parsing using worker threads
|
|
28
|
+
*/
|
|
29
|
+
export class FileProcessor {
|
|
30
|
+
private readonly maxWorkers: number;
|
|
31
|
+
private readonly cacheManager: CacheManager;
|
|
32
|
+
private workers: Worker[] = [];
|
|
33
|
+
private taskQueue: WorkerTask[] = [];
|
|
34
|
+
private pendingTasks = new Map<string, {
|
|
35
|
+
resolve: (features: DetectedFeature[]) => void;
|
|
36
|
+
reject: (error: Error) => void;
|
|
37
|
+
}>();
|
|
38
|
+
private isProcessing = false;
|
|
39
|
+
|
|
40
|
+
constructor(options: {
|
|
41
|
+
maxWorkers?: number;
|
|
42
|
+
cacheManager?: CacheManager;
|
|
43
|
+
} = {}) {
|
|
44
|
+
this.maxWorkers = options.maxWorkers || Math.min(cpus().length, 8);
|
|
45
|
+
this.cacheManager = options.cacheManager || new CacheManager();
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Process multiple files concurrently with caching
|
|
50
|
+
*/
|
|
51
|
+
async processFiles(filePaths: string[]): Promise<DetectedFeature[]> {
|
|
52
|
+
// Check cache for unchanged files
|
|
53
|
+
const { changed, unchanged } = await this.cacheManager.getChangedFiles(filePaths);
|
|
54
|
+
|
|
55
|
+
// Get cached results for unchanged files
|
|
56
|
+
const cachedFeatures: DetectedFeature[] = [];
|
|
57
|
+
for (const filePath of unchanged) {
|
|
58
|
+
const cached = await this.cacheManager.getCachedParseResult(filePath);
|
|
59
|
+
if (cached) {
|
|
60
|
+
cachedFeatures.push(...cached);
|
|
61
|
+
} else {
|
|
62
|
+
// Cache miss, need to process
|
|
63
|
+
changed.push(filePath);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Process changed files
|
|
68
|
+
const newFeatures = changed.length > 0
|
|
69
|
+
? await this.processFilesWithWorkers(changed)
|
|
70
|
+
: [];
|
|
71
|
+
|
|
72
|
+
return [...cachedFeatures, ...newFeatures];
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Process files using worker threads
|
|
77
|
+
*/
|
|
78
|
+
private async processFilesWithWorkers(filePaths: string[]): Promise<DetectedFeature[]> {
|
|
79
|
+
if (filePaths.length === 0) {
|
|
80
|
+
return [];
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Create worker tasks
|
|
84
|
+
const tasks: WorkerTask[] = filePaths.map((filePath, index) => ({
|
|
85
|
+
id: `task_${index}_${Date.now()}`,
|
|
86
|
+
filePath
|
|
87
|
+
}));
|
|
88
|
+
|
|
89
|
+
// Process tasks in batches
|
|
90
|
+
const batchSize = Math.max(1, Math.ceil(tasks.length / this.maxWorkers));
|
|
91
|
+
const batches: WorkerTask[][] = [];
|
|
92
|
+
|
|
93
|
+
for (let i = 0; i < tasks.length; i += batchSize) {
|
|
94
|
+
batches.push(tasks.slice(i, i + batchSize));
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const allFeatures: DetectedFeature[] = [];
|
|
98
|
+
|
|
99
|
+
// Process each batch
|
|
100
|
+
for (const batch of batches) {
|
|
101
|
+
const batchResults = await Promise.allSettled(
|
|
102
|
+
batch.map(task => this.processTask(task))
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
batchResults.forEach((result, index) => {
|
|
106
|
+
const task = batch[index];
|
|
107
|
+
if (!task) return;
|
|
108
|
+
|
|
109
|
+
if (result.status === 'fulfilled') {
|
|
110
|
+
allFeatures.push(...result.value);
|
|
111
|
+
// Cache the result
|
|
112
|
+
this.cacheManager.setCachedParseResult(task.filePath, result.value);
|
|
113
|
+
} else {
|
|
114
|
+
console.warn(`Failed to process ${task.filePath}: ${result.reason}`);
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
// Small delay between batches to prevent overwhelming
|
|
119
|
+
if (batches.length > 1) {
|
|
120
|
+
await this.sleep(10);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return allFeatures;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Process a single task
|
|
129
|
+
*/
|
|
130
|
+
private async processTask(task: WorkerTask): Promise<DetectedFeature[]> {
|
|
131
|
+
return new Promise((resolve, reject) => {
|
|
132
|
+
// Check cache first
|
|
133
|
+
this.cacheManager.getCachedParseResult(task.filePath)
|
|
134
|
+
.then(cached => {
|
|
135
|
+
if (cached) {
|
|
136
|
+
resolve(cached);
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// Add to pending tasks
|
|
141
|
+
this.pendingTasks.set(task.id, { resolve, reject });
|
|
142
|
+
this.taskQueue.push(task);
|
|
143
|
+
|
|
144
|
+
// Start processing if not already running
|
|
145
|
+
if (!this.isProcessing) {
|
|
146
|
+
this.startProcessing();
|
|
147
|
+
}
|
|
148
|
+
})
|
|
149
|
+
.catch(reject);
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Start worker processing
|
|
155
|
+
*/
|
|
156
|
+
private async startProcessing(): Promise<void> {
|
|
157
|
+
if (this.isProcessing) {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
this.isProcessing = true;
|
|
162
|
+
|
|
163
|
+
try {
|
|
164
|
+
// Initialize workers
|
|
165
|
+
await this.initializeWorkers();
|
|
166
|
+
|
|
167
|
+
// Process tasks
|
|
168
|
+
while (this.taskQueue.length > 0 || this.pendingTasks.size > 0) {
|
|
169
|
+
await this.processBatch();
|
|
170
|
+
await this.sleep(10);
|
|
171
|
+
}
|
|
172
|
+
} finally {
|
|
173
|
+
// Cleanup
|
|
174
|
+
await this.terminateWorkers();
|
|
175
|
+
this.isProcessing = false;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Initialize worker threads
|
|
181
|
+
*/
|
|
182
|
+
private async initializeWorkers(): Promise<void> {
|
|
183
|
+
const workerScript = this.getWorkerScriptPath();
|
|
184
|
+
const numWorkers = Math.min(this.maxWorkers, this.taskQueue.length);
|
|
185
|
+
|
|
186
|
+
for (let i = 0; i < numWorkers; i++) {
|
|
187
|
+
try {
|
|
188
|
+
const worker = new Worker(workerScript);
|
|
189
|
+
|
|
190
|
+
worker.on('message', (result: WorkerResult) => {
|
|
191
|
+
this.handleWorkerResult(result);
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
worker.on('error', (error) => {
|
|
195
|
+
console.error(`Worker error: ${error.message}`);
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
this.workers.push(worker);
|
|
199
|
+
} catch (error) {
|
|
200
|
+
console.warn(`Failed to create worker: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Process a batch of tasks with available workers
|
|
207
|
+
*/
|
|
208
|
+
private async processBatch(): Promise<void> {
|
|
209
|
+
const availableWorkers = this.workers.length;
|
|
210
|
+
const tasksToProcess = this.taskQueue.splice(0, availableWorkers);
|
|
211
|
+
|
|
212
|
+
if (tasksToProcess.length === 0) {
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// Assign tasks to workers
|
|
217
|
+
const promises = tasksToProcess.map((task, index) => {
|
|
218
|
+
const worker = this.workers[index % this.workers.length];
|
|
219
|
+
return this.assignTaskToWorker(worker, task);
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
await Promise.allSettled(promises);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Assign task to worker
|
|
227
|
+
*/
|
|
228
|
+
private async assignTaskToWorker(worker: Worker | undefined, task: WorkerTask): Promise<void> {
|
|
229
|
+
if (!worker) {
|
|
230
|
+
throw new Error('Worker is not available');
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
return new Promise((resolve) => {
|
|
234
|
+
const timeout = setTimeout(() => {
|
|
235
|
+
console.warn(`Worker task ${task.id} timed out`);
|
|
236
|
+
this.handleWorkerError(task.id, new Error('Worker timeout'));
|
|
237
|
+
resolve();
|
|
238
|
+
}, 30000); // 30 second timeout
|
|
239
|
+
|
|
240
|
+
const messageHandler = (result: WorkerResult) => {
|
|
241
|
+
if (result.id === task.id) {
|
|
242
|
+
clearTimeout(timeout);
|
|
243
|
+
worker.off('message', messageHandler);
|
|
244
|
+
resolve();
|
|
245
|
+
}
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
worker.on('message', messageHandler);
|
|
249
|
+
worker.postMessage(task);
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Handle worker result
|
|
255
|
+
*/
|
|
256
|
+
private handleWorkerResult(result: WorkerResult): void {
|
|
257
|
+
const pending = this.pendingTasks.get(result.id);
|
|
258
|
+
if (!pending) {
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
this.pendingTasks.delete(result.id);
|
|
263
|
+
|
|
264
|
+
if (result.error) {
|
|
265
|
+
pending.reject(new Error(result.error));
|
|
266
|
+
} else {
|
|
267
|
+
pending.resolve(result.features);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Handle worker error
|
|
273
|
+
*/
|
|
274
|
+
private handleWorkerError(taskId: string, error: Error): void {
|
|
275
|
+
const pending = this.pendingTasks.get(taskId);
|
|
276
|
+
if (pending) {
|
|
277
|
+
this.pendingTasks.delete(taskId);
|
|
278
|
+
pending.reject(error);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* Terminate all workers
|
|
284
|
+
*/
|
|
285
|
+
private async terminateWorkers(): Promise<void> {
|
|
286
|
+
await Promise.all(
|
|
287
|
+
this.workers.map(worker => worker.terminate())
|
|
288
|
+
);
|
|
289
|
+
this.workers = [];
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* Get worker script path
|
|
294
|
+
*/
|
|
295
|
+
private getWorkerScriptPath(): string {
|
|
296
|
+
// For now, we'll use a simple fallback since we don't have worker threads implemented yet
|
|
297
|
+
// In a real implementation, this would point to a separate worker script
|
|
298
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
299
|
+
const __dirname = dirname(__filename);
|
|
300
|
+
return join(__dirname, 'parser-worker.js');
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Sleep utility
|
|
305
|
+
*/
|
|
306
|
+
private sleep(ms: number): Promise<void> {
|
|
307
|
+
return new Promise(resolve => setTimeout(resolve, ms));
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Get processing statistics
|
|
312
|
+
*/
|
|
313
|
+
getStats(): {
|
|
314
|
+
maxWorkers: number;
|
|
315
|
+
activeWorkers: number;
|
|
316
|
+
queuedTasks: number;
|
|
317
|
+
pendingTasks: number;
|
|
318
|
+
cacheStats: any;
|
|
319
|
+
} {
|
|
320
|
+
return {
|
|
321
|
+
maxWorkers: this.maxWorkers,
|
|
322
|
+
activeWorkers: this.workers.length,
|
|
323
|
+
queuedTasks: this.taskQueue.length,
|
|
324
|
+
pendingTasks: this.pendingTasks.size,
|
|
325
|
+
cacheStats: this.cacheManager.getStats()
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* Cleanup resources
|
|
331
|
+
*/
|
|
332
|
+
async cleanup(): Promise<void> {
|
|
333
|
+
await this.terminateWorkers();
|
|
334
|
+
this.taskQueue = [];
|
|
335
|
+
this.pendingTasks.clear();
|
|
336
|
+
this.isProcessing = false;
|
|
337
|
+
}
|
|
338
338
|
}
|