LintingManager.ts•10.4 kB
import { TitanMemoryModel } from '../model.js';
import type { WorkflowConfig, LintConfig, TitanMemorySystem } from '../types.js';
export class LintingManager {
private config: WorkflowConfig;
private memory: TitanMemorySystem;
private lintConfig: LintConfig;
constructor(config: WorkflowConfig, memory: TitanMemorySystem) {
this.config = config;
this.memory = memory;
this.lintConfig = config.features.linting;
}
/**
* Run comprehensive quality checks
*/
async runFullQualitySuite(): Promise<{
passed: boolean;
results: Record<string, any>;
issues: string[];
suggestions: string[];
}> {
const results: Record<string, any> = {};
const issues: string[] = [];
const suggestions: string[] = [];
let passed = true;
try {
// Syntax checking
if (this.lintConfig.levels.syntax.enabled) {
results.syntax = await this.runSyntaxChecks();
if (!results.syntax.passed && this.lintConfig.levels.syntax.failOnError) {
passed = false;
issues.push(...results.syntax.errors);
}
}
// Style checking
if (this.lintConfig.levels.style.enabled) {
results.style = await this.runStyleChecks();
suggestions.push(...results.style.suggestions || []);
}
// Security scanning
if (this.lintConfig.levels.security.enabled) {
results.security = await this.runSecurityChecks();
if (results.security.vulnerabilities > 0) {
if (this.lintConfig.levels.security.severity === 'error') {
passed = false;
}
issues.push(`Found ${results.security.vulnerabilities} security vulnerabilities`);
}
}
// Performance analysis
if (this.lintConfig.levels.performance.enabled) {
results.performance = await this.runPerformanceChecks();
suggestions.push(...results.performance.recommendations || []);
}
// Store results in memory for learning
await this.memory.storeMemory(`Quality check results: ${passed ? 'PASSED' : 'FAILED'}. Issues: ${issues.length}, Suggestions: ${suggestions.length}`);
return { passed, results, issues, suggestions };
} catch (error) {
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
console.error('Error running quality suite:', error);
return {
passed: false,
results: { error: errorMessage },
issues: [`Quality suite failed: ${errorMessage}`],
suggestions: []
};
}
}
/**
* Run syntax validation
*/
private async runSyntaxChecks(): Promise<{
passed: boolean;
errors: string[];
warnings: string[];
}> {
const errors: string[] = [];
const warnings: string[] = [];
// Mock implementation - would integrate with actual linting tools
for (const tool of this.lintConfig.levels.syntax.tools) {
try {
// Simulate running linting tool
console.log(`Running syntax check with ${tool}`);
// Mock some common issues for demonstration
if (Math.random() > 0.8) {
errors.push(`${tool}: Syntax error found in line 42`);
}
if (Math.random() > 0.6) {
warnings.push(`${tool}: Unused variable detected`);
}
} catch (error) {
errors.push(`Failed to run ${tool}: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
}
return {
passed: errors.length === 0,
errors,
warnings
};
}
/**
* Run style checking
*/
private async runStyleChecks(): Promise<{
passed: boolean;
suggestions: string[];
autoFixed: boolean;
}> {
const suggestions: string[] = [];
let autoFixed = false;
try {
console.log(`Running style checks with config: ${this.lintConfig.levels.style.config}`);
// Mock style suggestions
if (Math.random() > 0.7) {
suggestions.push('Consider using consistent indentation');
}
if (Math.random() > 0.8) {
suggestions.push('Add missing semicolons');
}
// Auto-fix if enabled
if (this.lintConfig.levels.style.autoFix && suggestions.length > 0) {
console.log('Auto-fixing style issues...');
autoFixed = true;
}
return {
passed: suggestions.length === 0,
suggestions,
autoFixed
};
} catch (error) {
console.error('Style check failed:', error);
return {
passed: false,
suggestions: [`Style check failed: ${error instanceof Error ? error.message : 'Unknown error'}`],
autoFixed: false
};
}
}
/**
* Run security scanning
*/
private async runSecurityChecks(): Promise<{
vulnerabilities: number;
issues: Array<{
severity: 'low' | 'medium' | 'high' | 'critical';
description: string;
file?: string;
line?: number;
}>;
recommendations: string[];
}> {
const issues: Array<{
severity: 'low' | 'medium' | 'high' | 'critical';
description: string;
file?: string;
line?: number;
}> = [];
const recommendations: string[] = [];
try {
for (const tool of this.lintConfig.levels.security.tools) {
console.log(`Running security scan with ${tool}`);
// Mock security issues
if (Math.random() > 0.9) {
issues.push({
severity: 'high',
description: 'Potential SQL injection vulnerability',
file: 'src/database.ts',
line: 156
});
}
if (Math.random() > 0.85) {
issues.push({
severity: 'medium',
description: 'Hardcoded credentials detected',
file: 'src/config.ts',
line: 23
});
}
}
if (issues.length > 0) {
recommendations.push('Update dependencies to latest secure versions');
recommendations.push('Implement proper input validation');
recommendations.push('Use environment variables for sensitive data');
}
return {
vulnerabilities: issues.length,
issues,
recommendations
};
} catch (error) {
console.error('Security scan failed:', error);
return {
vulnerabilities: 0,
issues: [],
recommendations: [`Security scan failed: ${error instanceof Error ? error.message : 'Unknown error'}`]
};
}
}
/**
* Run performance analysis
*/
private async runPerformanceChecks(): Promise<{
score: number;
recommendations: string[];
metrics: Record<string, number>;
}> {
const recommendations: string[] = [];
const metrics: Record<string, number> = {};
try {
// Mock performance metrics
metrics.bundleSize = Math.floor(Math.random() * 1000) + 500; // KB
metrics.loadTime = Math.random() * 2 + 1; // seconds
metrics.memoryUsage = Math.floor(Math.random() * 100) + 50; // MB
// Check against thresholds
const thresholds = this.lintConfig.levels.performance.thresholds;
if (thresholds.bundleSize && metrics.bundleSize > thresholds.bundleSize) {
recommendations.push(`Bundle size (${metrics.bundleSize}KB) exceeds threshold (${thresholds.bundleSize}KB)`);
}
if (thresholds.loadTime && metrics.loadTime > thresholds.loadTime) {
recommendations.push(`Load time (${metrics.loadTime.toFixed(2)}s) exceeds threshold (${thresholds.loadTime}s)`);
}
if (thresholds.memoryUsage && metrics.memoryUsage > thresholds.memoryUsage) {
recommendations.push(`Memory usage (${metrics.memoryUsage}MB) exceeds threshold (${thresholds.memoryUsage}MB)`);
}
// Calculate performance score (0-100)
let score = 100;
score -= recommendations.length * 10; // Penalty for each recommendation
score = Math.max(0, Math.min(100, score));
return {
score,
recommendations,
metrics
};
} catch (error) {
console.error('Performance check failed:', error);
return {
score: 0,
recommendations: [`Performance check failed: ${error instanceof Error ? error.message : 'Unknown error'}`],
metrics: {}
};
}
}
/**
* Get linting configuration
*/
getConfig(): LintConfig {
return this.lintConfig;
}
/**
* Update linting configuration
*/
updateConfig(newConfig: Partial<LintConfig>): void {
this.lintConfig = { ...this.lintConfig, ...newConfig };
}
/**
* Get quality metrics history from memory
*/
async getQualityHistory(limit = 10): Promise<any[]> {
try {
const history = await this.memory.recallMemory('quality check', limit);
return history.map(tensor => Array.from(tensor.dataSync()));
} catch (error) {
console.error('Failed to retrieve quality history:', error);
return [];
}
}
}