UNPKG

codecrucible-synth

Version:

Production-Ready AI Development Platform with Multi-Voice Synthesis, Smithery MCP Integration, Enterprise Security, and Zero-Timeout Reliability

493 lines 19.7 kB
/** * System Bootstrap - Dependency Injection Orchestration * Breaks circular dependencies through controlled initialization * * Living Spiral Council Applied: * - Architect: Proper dependency order and lifecycle management * - Maintainer: Clear initialization phases and error handling * - Security Guardian: Secure service initialization and validation * - Performance Engineer: Optimized startup sequence and lazy loading * - Explorer: Extensible bootstrap process for new services */ import { DependencyContainer } from './dependency-container.js'; import { CLIENT_TOKEN, CLIENT_CONFIG_TOKEN, PROVIDER_REPOSITORY_TOKEN, HYBRID_ROUTER_TOKEN, CACHE_COORDINATOR_TOKEN, SECURITY_VALIDATOR_TOKEN, STREAMING_MANAGER_TOKEN, PERFORMANCE_MONITOR_TOKEN, SYNTHESIS_COORDINATOR_TOKEN, LOGGER_TOKEN, CONFIG_MANAGER_TOKEN, } from './service-tokens.js'; import { logger } from '../logger.js'; import { startupOptimizer } from '../performance/startup-optimizer.js'; /** * System Bootstrap Implementation * Orchestrates dependency injection to break circular dependencies */ export class SystemBootstrap { container; config; initializationStartTime = 0; warnings = []; errors = []; isBootstrapped = false; constructor(config = {}) { this.config = { skipValidation: false, enablePerformanceMonitoring: true, logLevel: 'info', initializationTimeout: 30000, // 30 seconds environment: 'development', ...config, }; this.container = new DependencyContainer(); } /** * Bootstrap the entire system */ async bootstrap() { if (this.isBootstrapped) { logger.debug('System already bootstrapped, skipping...'); return { container: this.container, client: await this.container.resolveAsync(CLIENT_TOKEN), initializationTime: 0, servicesInitialized: this.container.getRegisteredServices(), warnings: this.warnings, errors: this.errors, }; } this.initializationStartTime = Date.now(); try { logger.info('🚀 Starting optimized system bootstrap...'); // Register startup optimization tasks startupOptimizer.reset(); this.registerStartupTasks(); // Execute optimized startup const startupResult = await startupOptimizer.executeOptimizedStartup(); logger.info(`🚀 Startup optimization completed: ${startupResult.successCount}/${startupResult.totalTime}ms`); // Phase 1: Core infrastructure (no dependencies) await this.initializeCoreInfrastructure(); // Phase 2: Configuration and utilities await this.initializeConfiguration(); // Phase 3: Performance and monitoring await this.initializeMonitoring(); // Phase 4: Security services await this.initializeSecurity(); // Phase 5: Cache services await this.initializeCache(); // Phase 6: Provider services await this.initializeProviders(); // Phase 7: Routing services await this.initializeRouting(); // Phase 8: Streaming services await this.initializeStreaming(); // Phase 9: Main client (depends on all above) await this.initializeClient(); // Phase 10: Application layer services (depends on client) await this.initializeSynthesisCoordinator(); // Phase 11: Validation and health checks if (!this.config.skipValidation) { await this.validateSystemHealth(); } const client = await this.container.resolveAsync(CLIENT_TOKEN); const initializationTime = Date.now() - this.initializationStartTime; logger.info(`✅ System bootstrap completed in ${initializationTime}ms`); // Mark as successfully bootstrapped this.isBootstrapped = true; // Generate startup performance report const startupAnalytics = startupOptimizer.getStartupAnalytics(); const recommendations = startupOptimizer.getOptimizationRecommendations(); if (recommendations.length > 0) { logger.info('📊 Startup optimization recommendations:'); recommendations.forEach(rec => logger.info(` - ${rec}`)); } return { container: this.container, client, initializationTime, servicesInitialized: this.container.getRegisteredServices(), warnings: this.warnings, errors: this.errors, }; } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); this.errors.push(`Bootstrap failed: ${errorMessage}`); logger.error('❌ System bootstrap failed:', error); throw error; } } /** * Phase 1: Core infrastructure services (no dependencies) */ async initializeCoreInfrastructure() { logger.debug('Phase 1: Initializing core infrastructure...'); // Logger (already available as singleton) this.container.registerValue(LOGGER_TOKEN, logger); // Basic utilities this.container.register('error-utils', () => import('../../utils/error-utils.js'), { lifecycle: 'singleton', lazy: true, }); } /** * Phase 2: Configuration services */ async initializeConfiguration() { logger.debug('Phase 2: Initializing configuration...'); // Configuration manager this.container.register(CONFIG_MANAGER_TOKEN, async () => { const { ConfigManager } = await import('../../config/config-manager.js'); return new ConfigManager(); }, { lifecycle: 'singleton', lazy: true }); // Client configuration (using factory pattern to avoid circular deps) this.container.register(CLIENT_CONFIG_TOKEN, container => { // Return default configuration without importing client.ts return { providers: [ { type: 'ollama', endpoint: 'http://localhost:11434', timeout: 7200000 }, // 2 hour timeout - industry standard { type: 'lm-studio', endpoint: 'http://localhost:1234', timeout: 7200000 }, // 2 hour timeout - industry standard ], executionMode: 'auto', fallbackChain: ['ollama', 'lm-studio'], performanceThresholds: { maxResponseTime: 30000, maxConcurrentRequests: 3, }, security: { enableSandbox: true, maxInputLength: 50000, }, }; }, { lifecycle: 'singleton' }); } /** * Phase 3: Performance monitoring */ async initializeMonitoring() { logger.debug('Phase 3: Initializing performance monitoring...'); this.container.register(PERFORMANCE_MONITOR_TOKEN, async () => { const { PerformanceMonitor } = await import('../../utils/performance.js'); return new PerformanceMonitor(); }, { lifecycle: 'singleton', lazy: true }); } /** * Phase 4: Security services */ async initializeSecurity() { logger.debug('Phase 4: Initializing security services...'); this.container.register(SECURITY_VALIDATOR_TOKEN, async (container) => { const { SecurityValidator } = await import('../security/security-validator.js'); const config = container.resolve(CLIENT_CONFIG_TOKEN); return new SecurityValidator(config.security); }, { lifecycle: 'singleton', dependencies: [CLIENT_CONFIG_TOKEN.name], lazy: true, }); } /** * Phase 5: Cache services */ async initializeCache() { logger.debug('Phase 5: Initializing cache services...'); this.container.register(CACHE_COORDINATOR_TOKEN, async () => { const { CacheCoordinator } = await import('../caching/cache-coordinator.js'); const coordinator = new CacheCoordinator(); return coordinator; }, { lifecycle: 'singleton' }); } /** * Phase 6: Provider services */ async initializeProviders() { logger.debug('Phase 6: Initializing provider services...'); this.container.register(PROVIDER_REPOSITORY_TOKEN, async (container) => { const { ProviderRepository } = await import('../providers/provider-repository.js'); const config = container.resolve(CLIENT_CONFIG_TOKEN); const providerRepository = new ProviderRepository(); // Initialize with provider configurations from CLIENT_CONFIG await providerRepository.initialize(config.providers || []); return providerRepository; }, { lifecycle: 'singleton', lazy: true, dependencies: [CLIENT_CONFIG_TOKEN.name], }); } /** * Phase 7: Routing services */ async initializeRouting() { logger.debug('Phase 7: Initializing routing services...'); this.container.register(HYBRID_ROUTER_TOKEN, async (container) => { const { HybridLLMRouter } = await import('../hybrid/hybrid-llm-router.js'); const config = container.resolve(CLIENT_CONFIG_TOKEN); // Create hybrid config without circular dependencies const hybridConfig = { lmStudio: { endpoint: 'http://localhost:1234', enabled: true, models: ['codellama:7b-instruct', 'qwen2.5-coder:7b'], maxConcurrent: 2, strengths: ['speed', 'lightweight', 'efficiency'], }, ollama: { endpoint: 'http://localhost:11434', enabled: true, models: ['codellama:34b', 'qwen2.5:72b', 'deepseek-coder:8b'], maxConcurrent: 1, strengths: ['analysis', 'reasoning', 'security', 'architecture'], }, routing: { defaultProvider: 'auto', escalationThreshold: 0.7, confidenceScoring: true, learningEnabled: true, }, }; return new HybridLLMRouter(hybridConfig); }, { lifecycle: 'singleton', dependencies: [CLIENT_CONFIG_TOKEN.name], lazy: true, }); } /** * Phase 8: Streaming services */ async initializeStreaming() { logger.debug('Phase 8: Initializing streaming services...'); this.container.register(STREAMING_MANAGER_TOKEN, async (container) => { const { StreamingManager } = await import('../streaming/streaming-manager.js'); const config = container.resolve(CLIENT_CONFIG_TOKEN); return new StreamingManager(config.streaming); }, { lifecycle: 'singleton', dependencies: [CLIENT_CONFIG_TOKEN.name], lazy: true, }); } /** * Phase 9: Main client (depends on all above services) */ async initializeClient() { logger.debug('Phase 9: Initializing main client...'); this.container.register(CLIENT_TOKEN, async (container) => { // Import client class dynamically to avoid circular imports const { UnifiedModelClient } = await import('../../refactor/unified-model-client.js'); // Resolve all dependencies (await async ones properly) const config = container.resolve(CLIENT_CONFIG_TOKEN); const providerRepository = await container.resolveAsync(PROVIDER_REPOSITORY_TOKEN); const hybridRouter = await container.resolveAsync(HYBRID_ROUTER_TOKEN); const cacheCoordinator = await container.resolveAsync(CACHE_COORDINATOR_TOKEN); const securityValidator = await container.resolveAsync(SECURITY_VALIDATOR_TOKEN); const streamingManager = await container.resolveAsync(STREAMING_MANAGER_TOKEN); const performanceMonitor = await container.resolveAsync(PERFORMANCE_MONITOR_TOKEN); // Create client with all dependencies injected using new DI constructor const client = new UnifiedModelClient(config, { providerRepository, securityValidator, streamingManager, cacheCoordinator, performanceMonitor, hybridRouter, }); return client; }, { lifecycle: 'singleton', dependencies: [ CLIENT_CONFIG_TOKEN.name, PROVIDER_REPOSITORY_TOKEN.name, HYBRID_ROUTER_TOKEN.name, CACHE_COORDINATOR_TOKEN.name, SECURITY_VALIDATOR_TOKEN.name, STREAMING_MANAGER_TOKEN.name, PERFORMANCE_MONITOR_TOKEN.name, ], lazy: true, }); } /** * Phase 10: Application layer services */ async initializeSynthesisCoordinator() { logger.debug('Phase 10: Initializing SynthesisCoordinator...'); this.container.register(SYNTHESIS_COORDINATOR_TOKEN, async (container) => { const { SynthesisCoordinator } = await import('../application/synthesis-coordinator.js'); return new SynthesisCoordinator(container); }, { lifecycle: 'singleton', dependencies: [ CLIENT_TOKEN.name, HYBRID_ROUTER_TOKEN.name, CACHE_COORDINATOR_TOKEN.name, SECURITY_VALIDATOR_TOKEN.name, STREAMING_MANAGER_TOKEN.name, PERFORMANCE_MONITOR_TOKEN.name, ], lazy: true, }); } /** * Phase 11: System validation */ async validateSystemHealth() { logger.debug('Phase 11: Validating system health...'); // Validate dependency graph const validation = this.container.validateDependencyGraph(); if (!validation.isValid) { const error = `Circular dependencies detected: ${validation.cycles.join(', ')}`; this.errors.push(error); throw new Error(error); } // Initialize eager services await this.container.initializeEagerServices(); // Basic health check try { const client = this.container.resolve(CLIENT_TOKEN); await client.initialize(); logger.info('✅ Client initialization successful'); } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); this.warnings.push(`Client initialization warning: ${errorMessage}`); logger.warn('⚠️ Client initialization had issues:', error); } } /** * Get the dependency container */ getContainer() { return this.container; } /** * Register startup optimization tasks */ registerStartupTasks() { // Critical path tasks startupOptimizer.registerTask({ name: 'core_infrastructure', priority: 'critical', timeout: 1000, task: async () => { await this.initializeCoreInfrastructure(); return true; } }); startupOptimizer.registerTask({ name: 'configuration', priority: 'critical', timeout: 1000, task: async () => { await this.initializeConfiguration(); return true; }, dependencies: ['core_infrastructure'] }); // High priority parallel tasks startupOptimizer.registerTask({ name: 'security_services', priority: 'high', timeout: 2000, task: async () => { await this.initializeSecurity(); return true; }, dependencies: ['configuration'] }); startupOptimizer.registerTask({ name: 'cache_services', priority: 'high', timeout: 2000, task: async () => { await this.initializeCache(); return true; }, dependencies: ['configuration'] }); startupOptimizer.registerTask({ name: 'monitoring_services', priority: 'high', timeout: 2000, task: async () => { await this.initializeMonitoring(); return true; }, dependencies: ['configuration'] }); // Medium priority tasks startupOptimizer.registerTask({ name: 'provider_services', priority: 'medium', timeout: 3000, task: async () => { await this.initializeProviders(); return true; }, dependencies: ['configuration', 'cache_services'] }); startupOptimizer.registerTask({ name: 'routing_services', priority: 'medium', timeout: 3000, task: async () => { await this.initializeRouting(); return true; }, dependencies: ['configuration', 'provider_services'] }); startupOptimizer.registerTask({ name: 'streaming_services', priority: 'medium', timeout: 2000, task: async () => { await this.initializeStreaming(); return true; }, dependencies: ['configuration'] }); // Low priority (can fail without blocking) startupOptimizer.registerTask({ name: 'client_initialization', priority: 'low', timeout: 5000, task: async () => { await this.initializeClient(); return true; }, dependencies: ['provider_services', 'routing_services', 'streaming_services', 'security_services', 'monitoring_services'] }); startupOptimizer.registerTask({ name: 'synthesis_coordinator', priority: 'low', timeout: 3000, task: async () => { await this.initializeSynthesisCoordinator(); return true; }, dependencies: ['client_initialization'] }); } /** * Shutdown the system */ async shutdown() { logger.info('🛑 Shutting down system...'); await this.container.dispose(); logger.info('✅ System shutdown completed'); } } /** * Create and bootstrap the system */ export async function createSystem(config) { const bootstrap = new SystemBootstrap(config); return await bootstrap.bootstrap(); } /** * Create system for testing with minimal dependencies */ export async function createTestSystem() { return await createSystem({ skipValidation: true, enablePerformanceMonitoring: false, logLevel: 'error', environment: 'test', }); } //# sourceMappingURL=system-bootstrap.js.map