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58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
"use strict";
/**
 * Native Worker Runner for RuVector
 *
 * Direct integration with:
 * - ONNX embedder (384d, SIMD-accelerated)
 * - VectorDB (HNSW indexing)
 * - Intelligence engine (Q-learning, memory)
 *
 * No delegation to external tools - pure ruvector execution.
 */
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.NativeWorker = void 0;
exports.createSecurityWorker = createSecurityWorker;
exports.createAnalysisWorker = createAnalysisWorker;
exports.createLearningWorker = createLearningWorker;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const glob_1 = require("glob");
const onnx_embedder_1 = require("../core/onnx-embedder");
const security_1 = require("../analysis/security");
const complexity_1 = require("../analysis/complexity");
const patterns_1 = require("../analysis/patterns");
// Lazy imports for optional dependencies
let VectorDb = null;
let intelligence = null;
async function loadOptionalDeps() {
    try {
        const core = await Promise.resolve().then(() => __importStar(require('@ruvector/core')));
        VectorDb = core.VectorDb;
    }
    catch {
        // VectorDB not available
    }
    try {
        const intel = await Promise.resolve().then(() => __importStar(require('../core/intelligence-engine')));
        intelligence = intel;
    }
    catch {
        // Intelligence not available
    }
}
/**
 * Native Worker Runner
 */
class NativeWorker {
    constructor(config) {
        this.vectorDb = null;
        this.findings = [];
        this.stats = {
            filesAnalyzed: 0,
            patternsFound: 0,
            embeddingsGenerated: 0,
            vectorsStored: 0,
        };
        this.config = config;
    }
    /**
     * Initialize worker with capabilities
     */
    async init() {
        await loadOptionalDeps();
        // Initialize ONNX embedder if needed
        if (this.config.capabilities?.onnxEmbeddings) {
            await (0, onnx_embedder_1.initOnnxEmbedder)();
        }
        // Initialize VectorDB if needed
        if (this.config.capabilities?.vectorDb && VectorDb) {
            const dbPath = path.join(process.cwd(), '.ruvector', 'workers', `${this.config.name}.db`);
            fs.mkdirSync(path.dirname(dbPath), { recursive: true });
            this.vectorDb = new VectorDb({
                dimensions: 384,
                storagePath: dbPath,
            });
        }
    }
    /**
     * Run all phases in sequence
     */
    async run(targetPath = '.') {
        const startTime = performance.now();
        const phaseResults = [];
        await this.init();
        let context = { targetPath, files: [], patterns: [], embeddings: [] };
        for (const phaseConfig of this.config.phases) {
            const phaseStart = performance.now();
            try {
                context = await this.executePhase(phaseConfig.type, context, phaseConfig.config);
                phaseResults.push({
                    phase: phaseConfig.type,
                    success: true,
                    data: this.summarizePhaseData(phaseConfig.type, context),
                    timeMs: performance.now() - phaseStart,
                });
            }
            catch (error) {
                phaseResults.push({
                    phase: phaseConfig.type,
                    success: false,
                    data: null,
                    timeMs: performance.now() - phaseStart,
                    error: error.message,
                });
                // Continue to next phase on error (fault-tolerant)
            }
        }
        const totalTimeMs = performance.now() - startTime;
        return {
            worker: this.config.name,
            success: phaseResults.every(p => p.success),
            phases: phaseResults,
            totalTimeMs,
            summary: {
                filesAnalyzed: this.stats.filesAnalyzed,
                patternsFound: this.stats.patternsFound,
                embeddingsGenerated: this.stats.embeddingsGenerated,
                vectorsStored: this.stats.vectorsStored,
                findings: this.findings,
            },
        };
    }
    /**
     * Execute a single phase
     */
    async executePhase(type, context, config) {
        switch (type) {
            case 'file-discovery':
                return this.phaseFileDiscovery(context, config);
            case 'pattern-extraction':
                return this.phasePatternExtraction(context, config);
            case 'embedding-generation':
                return this.phaseEmbeddingGeneration(context, config);
            case 'vector-storage':
                return this.phaseVectorStorage(context, config);
            case 'similarity-search':
                return this.phaseSimilaritySearch(context, config);
            case 'security-scan':
                return this.phaseSecurityScan(context, config);
            case 'complexity-analysis':
                return this.phaseComplexityAnalysis(context, config);
            case 'summarization':
                return this.phaseSummarization(context, config);
            default:
                throw new Error(`Unknown phase: ${type}`);
        }
    }
    /**
     * Phase: File Discovery
     */
    async phaseFileDiscovery(context, config) {
        const patterns = config?.patterns || ['**/*.ts', '**/*.js', '**/*.tsx', '**/*.jsx'];
        const exclude = config?.exclude || ['**/node_modules/**', '**/dist/**', '**/.git/**'];
        const files = [];
        for (const pattern of patterns) {
            const matches = await (0, glob_1.glob)(pattern, {
                cwd: context.targetPath,
                ignore: exclude,
                nodir: true,
            });
            files.push(...matches.map(f => path.join(context.targetPath, f)));
        }
        this.stats.filesAnalyzed = files.length;
        return { ...context, files };
    }
    /**
     * Phase: Pattern Extraction (uses shared analysis module)
     */
    async phasePatternExtraction(context, config) {
        const patterns = [];
        const patternTypes = config?.types || ['function', 'class', 'import', 'export', 'todo'];
        for (const file of context.files.slice(0, 100)) {
            try {
                const filePatterns = (0, patterns_1.extractAllPatterns)(file);
                const matches = (0, patterns_1.toPatternMatches)(filePatterns);
                // Filter by requested pattern types
                for (const match of matches) {
                    if (patternTypes.includes(match.type)) {
                        patterns.push(match);
                        // Add findings for TODOs
                        if (match.type === 'todo') {
                            this.findings.push({
                                type: 'info',
                                message: match.match,
                                file,
                            });
                        }
                    }
                }
            }
            catch {
                // Skip unreadable files
            }
        }
        this.stats.patternsFound = patterns.length;
        return { ...context, patterns };
    }
    /**
     * Phase: Embedding Generation (ONNX)
     */
    async phaseEmbeddingGeneration(context, config) {
        if (!(0, onnx_embedder_1.isReady)()) {
            await (0, onnx_embedder_1.initOnnxEmbedder)();
        }
        const embeddings = [];
        const batchSize = config?.batchSize || 32;
        // Collect texts to embed
        const texts = [];
        // Embed file content summaries
        for (const file of context.files.slice(0, 50)) {
            try {
                const content = fs.readFileSync(file, 'utf-8');
                const summary = content.slice(0, 512); // First 512 chars
                texts.push({ text: summary, file });
            }
            catch {
                // Skip
            }
        }
        // Embed patterns
        for (const pattern of context.patterns.slice(0, 100)) {
            texts.push({ text: pattern.match, file: pattern.file });
        }
        // Batch embed
        for (let i = 0; i < texts.length; i += batchSize) {
            const batch = texts.slice(i, i + batchSize);
            const results = await (0, onnx_embedder_1.embedBatch)(batch.map(t => t.text));
            for (let j = 0; j < results.length; j++) {
                embeddings.push({
                    text: batch[j].text,
                    embedding: results[j].embedding,
                    file: batch[j].file,
                });
            }
        }
        this.stats.embeddingsGenerated = embeddings.length;
        return { ...context, embeddings };
    }
    /**
     * Phase: Vector Storage
     */
    async phaseVectorStorage(context, config) {
        if (!this.vectorDb) {
            return context;
        }
        let stored = 0;
        for (const item of context.embeddings) {
            try {
                await this.vectorDb.insert({
                    vector: new Float32Array(item.embedding),
                    metadata: {
                        text: item.text.slice(0, 200),
                        file: item.file,
                        worker: this.config.name,
                        timestamp: Date.now(),
                    },
                });
                stored++;
            }
            catch {
                // Skip duplicates/errors
            }
        }
        this.stats.vectorsStored = stored;
        return context;
    }
    /**
     * Phase: Similarity Search
     */
    async phaseSimilaritySearch(context, config) {
        if (!this.vectorDb || context.embeddings.length === 0) {
            return context;
        }
        const query = config?.query || context.embeddings[0]?.text;
        if (!query)
            return context;
        const queryResult = await (0, onnx_embedder_1.embed)(query);
        const results = await this.vectorDb.search({
            vector: new Float32Array(queryResult.embedding),
            k: config?.k || 5,
        });
        return { ...context, searchResults: results };
    }
    /**
     * Phase: Security Scan (uses shared analysis module)
     */
    async phaseSecurityScan(context, config) {
        // Use consolidated security scanner
        const findings = (0, security_1.scanFiles)(context.files, undefined, 100);
        // Convert to worker findings format
        for (const finding of findings) {
            this.findings.push({
                type: 'security',
                message: `${finding.rule}: ${finding.message}`,
                file: finding.file,
                line: finding.line,
                severity: finding.severity === 'critical' ? 4 :
                    finding.severity === 'high' ? 3 :
                        finding.severity === 'medium' ? 2 : 1,
            });
        }
        return context;
    }
    /**
     * Phase: Complexity Analysis (uses shared analysis module)
     */
    async phaseComplexityAnalysis(context, config) {
        const complexityThreshold = config?.threshold || 10;
        const complexFiles = [];
        for (const file of context.files.slice(0, 50)) {
            // Use consolidated complexity analyzer
            const result = (0, complexity_1.analyzeFile)(file);
            if (result.cyclomaticComplexity > complexityThreshold) {
                complexFiles.push(result);
                const rating = (0, complexity_1.getComplexityRating)(result.cyclomaticComplexity);
                this.findings.push({
                    type: 'warning',
                    message: `High complexity: ${result.cyclomaticComplexity} (threshold: ${complexityThreshold})`,
                    file,
                    severity: rating === 'critical' ? 4 : rating === 'high' ? 3 : 2,
                });
            }
        }
        return { ...context, complexFiles };
    }
    /**
     * Phase: Summarization
     */
    async phaseSummarization(context, config) {
        const summary = {
            filesAnalyzed: context.files?.length || 0,
            patternsFound: context.patterns?.length || 0,
            embeddingsGenerated: context.embeddings?.length || 0,
            findingsCount: this.findings.length,
            findingsByType: {
                info: this.findings.filter(f => f.type === 'info').length,
                warning: this.findings.filter(f => f.type === 'warning').length,
                error: this.findings.filter(f => f.type === 'error').length,
                security: this.findings.filter(f => f.type === 'security').length,
            },
            topFindings: this.findings.slice(0, 10),
        };
        return { ...context, summary };
    }
    /**
     * Summarize phase data for results
     */
    summarizePhaseData(type, context) {
        switch (type) {
            case 'file-discovery':
                return { filesFound: context.files?.length || 0 };
            case 'pattern-extraction':
                return { patternsFound: context.patterns?.length || 0 };
            case 'embedding-generation':
                return { embeddingsGenerated: context.embeddings?.length || 0 };
            case 'vector-storage':
                return { vectorsStored: this.stats.vectorsStored };
            case 'similarity-search':
                return { resultsFound: context.searchResults?.length || 0 };
            case 'security-scan':
                return { securityFindings: this.findings.filter(f => f.type === 'security').length };
            case 'complexity-analysis':
                return { complexFiles: context.complexFiles?.length || 0 };
            case 'summarization':
                return context.summary;
            default:
                return {};
        }
    }
}
exports.NativeWorker = NativeWorker;
/**
 * Quick worker factory functions
 */
function createSecurityWorker(name = 'security-scanner') {
    return new NativeWorker({
        name,
        description: 'Security vulnerability scanner',
        phases: [
            { type: 'file-discovery', config: { patterns: ['**/*.ts', '**/*.js', '**/*.tsx', '**/*.jsx'] } },
            { type: 'security-scan' },
            { type: 'summarization' },
        ],
        capabilities: { onnxEmbeddings: false, vectorDb: false },
    });
}
function createAnalysisWorker(name = 'code-analyzer') {
    return new NativeWorker({
        name,
        description: 'Code analysis with embeddings',
        phases: [
            { type: 'file-discovery' },
            { type: 'pattern-extraction' },
            { type: 'embedding-generation' },
            { type: 'vector-storage' },
            { type: 'complexity-analysis' },
            { type: 'summarization' },
        ],
        capabilities: { onnxEmbeddings: true, vectorDb: true },
    });
}
function createLearningWorker(name = 'pattern-learner') {
    return new NativeWorker({
        name,
        description: 'Pattern learning with vector storage',
        phases: [
            { type: 'file-discovery' },
            { type: 'pattern-extraction' },
            { type: 'embedding-generation' },
            { type: 'vector-storage' },
            { type: 'summarization' },
        ],
        capabilities: { onnxEmbeddings: true, vectorDb: true, intelligenceMemory: true },
    });
}