Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 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 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 | /** * V3 Plugin Loader * Domain-Driven Design - Plugin-Based Architecture (ADR-004) * * Handles plugin loading, dependency resolution, and lifecycle management */ import type { ClaudeFlowPlugin, PluginContext, PluginInfo, PluginLifecycleState, PluginError as PluginErrorType, PluginErrorCode, } from './plugin-interface.js'; import { PluginError } from './plugin-interface.js'; import type { PluginRegistry } from './plugin-registry.js'; /** * Plugin loader configuration */ export interface PluginLoaderConfig { /** * Maximum time to wait for plugin initialization (ms) */ initializationTimeout?: number; /** * Maximum time to wait for plugin shutdown (ms) */ shutdownTimeout?: number; /** * Enable parallel plugin initialization */ parallelInitialization?: boolean; /** * Enable strict dependency checking */ strictDependencies?: boolean; /** * Enable health checks */ enableHealthChecks?: boolean; /** * Health check interval (ms) */ healthCheckInterval?: number; } /** * Default plugin loader configuration */ const DEFAULT_CONFIG: Required<PluginLoaderConfig> = { initializationTimeout: 30000, // 30 seconds shutdownTimeout: 10000, // 10 seconds parallelInitialization: false, // Sequential by default for safety strictDependencies: true, enableHealthChecks: false, healthCheckInterval: 60000, // 1 minute }; /** * Plugin dependency graph node */ interface DependencyNode { plugin: ClaudeFlowPlugin; dependencies: Set<string>; dependents: Set<string>; depth: number; } /** * Plugin loader for managing plugin lifecycle */ export class PluginLoader { private config: Required<PluginLoaderConfig>; private registry: PluginRegistry; private initializationOrder: string[] = []; private healthCheckIntervalId?: NodeJS.Timeout; constructor(registry: PluginRegistry, config?: PluginLoaderConfig) { this.registry = registry; this.config = { ...DEFAULT_CONFIG, ...config }; } /** * Load a single plugin */ async loadPlugin(plugin: ClaudeFlowPlugin, context: PluginContext): Promise<void> { // Validate plugin this.validatePlugin(plugin); // Check for duplicates if (this.registry.hasPlugin(plugin.name)) { throw new PluginError( `Plugin '${plugin.name}' is already loaded`, plugin.name, 'DUPLICATE_PLUGIN' ); } // Register plugin in uninitialized state this.registry.registerPlugin(plugin, 'uninitialized', context); // Resolve dependencies if (this.config.strictDependencies) { this.validateDependencies(plugin); } // Initialize plugin await this.initializePlugin(plugin, context); // Update initialization order this.initializationOrder.push(plugin.name); } /** * Load multiple plugins with dependency resolution */ async loadPlugins( plugins: ClaudeFlowPlugin[], context: PluginContext ): Promise<LoadPluginsResult> { const results: LoadPluginsResult = { successful: [], failed: [], totalDuration: 0, }; const startTime = Date.now(); try { // Validate all plugins first for (const plugin of plugins) { this.validatePlugin(plugin); } // Build dependency graph const dependencyGraph = this.buildDependencyGraph(plugins); // Detect circular dependencies this.detectCircularDependencies(dependencyGraph); // Sort plugins by dependency order (topological sort) const sortedPlugins = this.topologicalSort(dependencyGraph); // Initialize plugins in order if (this.config.parallelInitialization) { await this.initializePluginsParallel(sortedPlugins, context, results); } else { await this.initializePluginsSequential(sortedPlugins, context, results); } } catch (error) { // If error during setup, mark all as failed for (const plugin of plugins) { if (!results.successful.includes(plugin.name) && !results.failed.some((f) => f.name === plugin.name)) { results.failed.push({ name: plugin.name, error: error instanceof Error ? error : new Error(String(error)), }); } } } finally { results.totalDuration = Date.now() - startTime; } // Start health checks if enabled if (this.config.enableHealthChecks) { this.startHealthChecks(); } return results; } /** * Unload a single plugin */ async unloadPlugin(pluginName: string): Promise<void> { const pluginInfo = this.registry.getPlugin(pluginName); if (!pluginInfo) { throw new PluginError( `Plugin '${pluginName}' not found`, pluginName, 'INVALID_PLUGIN' ); } // Check for dependents const dependents = this.findDependents(pluginName); if (dependents.length > 0) { throw new PluginError( `Cannot unload plugin '${pluginName}': depended on by ${dependents.join(', ')}`, pluginName, 'DEPENDENCY_NOT_FOUND' ); } // Shutdown plugin await this.shutdownPlugin(pluginInfo.plugin); // Unregister plugin this.registry.unregisterPlugin(pluginName); // Remove from initialization order const index = this.initializationOrder.indexOf(pluginName); if (index !== -1) { this.initializationOrder.splice(index, 1); } } /** * Unload all plugins in reverse initialization order */ async unloadAll(): Promise<void> { // Stop health checks if (this.healthCheckIntervalId) { clearInterval(this.healthCheckIntervalId); this.healthCheckIntervalId = undefined; } // Shutdown in reverse order const pluginsToShutdown = [...this.initializationOrder].reverse(); for (const pluginName of pluginsToShutdown) { try { await this.unloadPlugin(pluginName); } catch (error) { // Log error but continue shutting down other plugins console.error(`Error unloading plugin '${pluginName}':`, error); } } this.initializationOrder = []; } /** * Reload a plugin */ async reloadPlugin(pluginName: string, newPlugin: ClaudeFlowPlugin, context: PluginContext): Promise<void> { await this.unloadPlugin(pluginName); await this.loadPlugin(newPlugin, context); } /** * Get plugin initialization order */ getInitializationOrder(): string[] { return [...this.initializationOrder]; } /** * Validate plugin interface */ private validatePlugin(plugin: ClaudeFlowPlugin): void { if (!plugin.name) { throw new PluginError( 'Plugin must have a name', '<unknown>', 'INVALID_PLUGIN' ); } if (!plugin.version) { throw new PluginError( `Plugin '${plugin.name}' must have a version`, plugin.name, 'INVALID_PLUGIN' ); } if (typeof plugin.initialize !== 'function') { throw new PluginError( `Plugin '${plugin.name}' must implement initialize()`, plugin.name, 'INVALID_PLUGIN' ); } if (typeof plugin.shutdown !== 'function') { throw new PluginError( `Plugin '${plugin.name}' must implement shutdown()`, plugin.name, 'INVALID_PLUGIN' ); } } /** * Validate plugin dependencies */ private validateDependencies(plugin: ClaudeFlowPlugin): void { if (!plugin.dependencies || plugin.dependencies.length === 0) { return; } for (const dep of plugin.dependencies) { if (!this.registry.hasPlugin(dep)) { throw new PluginError( `Plugin '${plugin.name}' depends on '${dep}' which is not loaded`, plugin.name, 'DEPENDENCY_NOT_FOUND' ); } // Check dependency is initialized const depInfo = this.registry.getPlugin(dep); if (depInfo && depInfo.state !== 'initialized') { throw new PluginError( `Plugin '${plugin.name}' depends on '${dep}' which is not initialized (state: ${depInfo.state})`, plugin.name, 'DEPENDENCY_NOT_FOUND' ); } } } /** * Initialize a single plugin */ private async initializePlugin(plugin: ClaudeFlowPlugin, context: PluginContext): Promise<void> { this.registry.updatePluginState(plugin.name, 'initializing'); try { // Run initialization with timeout await this.withTimeout( plugin.initialize(context), this.config.initializationTimeout, `Plugin '${plugin.name}' initialization timed out` ); this.registry.updatePluginState(plugin.name, 'initialized'); this.registry.collectPluginMetrics(plugin.name); } catch (error) { this.registry.updatePluginState(plugin.name, 'error', error instanceof Error ? error : new Error(String(error))); throw new PluginError( `Failed to initialize plugin '${plugin.name}': ${error}`, plugin.name, 'INITIALIZATION_FAILED', error instanceof Error ? error : undefined ); } } /** * Shutdown a single plugin */ private async shutdownPlugin(plugin: ClaudeFlowPlugin): Promise<void> { this.registry.updatePluginState(plugin.name, 'shutting-down'); try { await this.withTimeout( plugin.shutdown(), this.config.shutdownTimeout, `Plugin '${plugin.name}' shutdown timed out` ); this.registry.updatePluginState(plugin.name, 'shutdown'); } catch (error) { this.registry.updatePluginState(plugin.name, 'error', error instanceof Error ? error : new Error(String(error))); throw new PluginError( `Failed to shutdown plugin '${plugin.name}': ${error}`, plugin.name, 'SHUTDOWN_FAILED', error instanceof Error ? error : undefined ); } } /** * Initialize plugins sequentially */ private async initializePluginsSequential( plugins: ClaudeFlowPlugin[], context: PluginContext, results: LoadPluginsResult ): Promise<void> { for (const plugin of plugins) { try { // Register and initialize this.registry.registerPlugin(plugin, 'uninitialized', context); await this.initializePlugin(plugin, context); results.successful.push(plugin.name); this.initializationOrder.push(plugin.name); } catch (error) { results.failed.push({ name: plugin.name, error: error instanceof Error ? error : new Error(String(error)), }); // Stop on first failure in sequential mode if strict if (this.config.strictDependencies) { break; } } } } /** * Initialize plugins in parallel (by dependency level) */ private async initializePluginsParallel( plugins: ClaudeFlowPlugin[], context: PluginContext, results: LoadPluginsResult ): Promise<void> { // Group plugins by dependency depth const dependencyGraph = this.buildDependencyGraph(plugins); const levels = this.groupByDepth(dependencyGraph); // Initialize each level in parallel for (const level of levels) { const promises = level.map(async (plugin) => { try { this.registry.registerPlugin(plugin, 'uninitialized', context); await this.initializePlugin(plugin, context); results.successful.push(plugin.name); this.initializationOrder.push(plugin.name); } catch (error) { results.failed.push({ name: plugin.name, error: error instanceof Error ? error : new Error(String(error)), }); } }); await Promise.all(promises); // Stop on failures in level if strict if (this.config.strictDependencies && results.failed.length > 0) { break; } } } /** * Build dependency graph */ private buildDependencyGraph(plugins: ClaudeFlowPlugin[]): Map<string, DependencyNode> { const graph = new Map<string, DependencyNode>(); // Create nodes for (const plugin of plugins) { graph.set(plugin.name, { plugin, dependencies: new Set(plugin.dependencies || []), dependents: new Set(), depth: 0, }); } // Build dependency links for (const [name, node] of Array.from(graph.entries())) { for (const dep of Array.from(node.dependencies)) { const depNode = graph.get(dep); if (depNode) { depNode.dependents.add(name); } } } // Calculate depths this.calculateDepths(graph); return graph; } /** * Calculate depth of each node (for topological sorting) */ private calculateDepths(graph: Map<string, DependencyNode>): void { const visited = new Set<string>(); const visit = (name: string): number => { if (visited.has(name)) { const node = graph.get(name); return node ? node.depth : 0; } visited.add(name); const node = graph.get(name); if (!node) return 0; let maxDepth = 0; for (const dep of Array.from(node.dependencies)) { maxDepth = Math.max(maxDepth, visit(dep) + 1); } node.depth = maxDepth; return maxDepth; }; for (const name of Array.from(graph.keys())) { visit(name); } } /** * Topological sort (dependency order) */ private topologicalSort(graph: Map<string, DependencyNode>): ClaudeFlowPlugin[] { const sorted: ClaudeFlowPlugin[] = []; const nodes = Array.from(graph.values()); // Sort by depth (dependencies first) nodes.sort((a, b) => a.depth - b.depth); for (const node of nodes) { sorted.push(node.plugin); } return sorted; } /** * Group plugins by dependency depth (for parallel initialization) */ private groupByDepth(graph: Map<string, DependencyNode>): ClaudeFlowPlugin[][] { const levels: ClaudeFlowPlugin[][] = []; const maxDepth = Math.max(...Array.from(graph.values()).map((n) => n.depth)); for (let depth = 0; depth <= maxDepth; depth++) { const level: ClaudeFlowPlugin[] = []; for (const node of Array.from(graph.values())) { if (node.depth === depth) { level.push(node.plugin); } } if (level.length > 0) { levels.push(level); } } return levels; } /** * Detect circular dependencies */ private detectCircularDependencies(graph: Map<string, DependencyNode>): void { const visited = new Set<string>(); const stack = new Set<string>(); const visit = (name: string, path: string[]): void => { if (stack.has(name)) { const cycle = [...path, name]; throw new PluginError( `Circular dependency detected: ${cycle.join(' -> ')}`, name, 'CIRCULAR_DEPENDENCY' ); } if (visited.has(name)) { return; } visited.add(name); stack.add(name); const node = graph.get(name); if (node) { for (const dep of Array.from(node.dependencies)) { visit(dep, [...path, name]); } } stack.delete(name); }; for (const name of Array.from(graph.keys())) { visit(name, []); } } /** * Find plugins that depend on a given plugin */ private findDependents(pluginName: string): string[] { const dependents: string[] = []; for (const [name, info] of Array.from(this.registry.getAllPlugins().entries())) { if (info.plugin.dependencies?.includes(pluginName)) { dependents.push(name); } } return dependents; } /** * Start periodic health checks */ private startHealthChecks(): void { this.healthCheckIntervalId = setInterval(async () => { for (const [name, info] of Array.from(this.registry.getAllPlugins().entries())) { if (info.state === 'initialized' && info.plugin.healthCheck) { try { const healthy = await info.plugin.healthCheck(); if (!healthy) { console.warn(`Plugin '${name}' health check failed`); this.registry.updatePluginState(name, 'error', new Error('Health check failed')); } } catch (error) { console.error(`Plugin '${name}' health check error:`, error); this.registry.updatePluginState(name, 'error', error instanceof Error ? error : new Error(String(error))); } } } }, this.config.healthCheckInterval); } /** * Utility: Run promise with timeout */ private async withTimeout<T>(promise: Promise<T>, timeoutMs: number, errorMessage: string): Promise<T> { return Promise.race([ promise, new Promise<T>((_, reject) => setTimeout(() => reject(new Error(errorMessage)), timeoutMs) ), ]); } } /** * Load plugins result */ export interface LoadPluginsResult { successful: string[]; failed: Array<{ name: string; error: Error }>; totalDuration: number; } |