@sparkleideas/shared 3.0.0-alpha.7

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.
Files changed (96) hide show
  1. package/README.md +323 -0
  2. package/__tests__/hooks/bash-safety.test.ts +289 -0
  3. package/__tests__/hooks/file-organization.test.ts +335 -0
  4. package/__tests__/hooks/git-commit.test.ts +336 -0
  5. package/__tests__/hooks/index.ts +23 -0
  6. package/__tests__/hooks/session-hooks.test.ts +357 -0
  7. package/__tests__/hooks/task-hooks.test.ts +193 -0
  8. package/docs/EVENTS_IMPLEMENTATION_SUMMARY.md +388 -0
  9. package/docs/EVENTS_QUICK_REFERENCE.md +470 -0
  10. package/docs/EVENTS_README.md +352 -0
  11. package/package.json +39 -0
  12. package/src/core/config/defaults.ts +207 -0
  13. package/src/core/config/index.ts +15 -0
  14. package/src/core/config/loader.ts +271 -0
  15. package/src/core/config/schema.ts +188 -0
  16. package/src/core/config/validator.ts +209 -0
  17. package/src/core/event-bus.ts +236 -0
  18. package/src/core/index.ts +22 -0
  19. package/src/core/interfaces/agent.interface.ts +251 -0
  20. package/src/core/interfaces/coordinator.interface.ts +363 -0
  21. package/src/core/interfaces/event.interface.ts +267 -0
  22. package/src/core/interfaces/index.ts +19 -0
  23. package/src/core/interfaces/memory.interface.ts +332 -0
  24. package/src/core/interfaces/task.interface.ts +223 -0
  25. package/src/core/orchestrator/event-coordinator.ts +122 -0
  26. package/src/core/orchestrator/health-monitor.ts +214 -0
  27. package/src/core/orchestrator/index.ts +89 -0
  28. package/src/core/orchestrator/lifecycle-manager.ts +263 -0
  29. package/src/core/orchestrator/session-manager.ts +279 -0
  30. package/src/core/orchestrator/task-manager.ts +317 -0
  31. package/src/events/domain-events.ts +584 -0
  32. package/src/events/event-store.test.ts +387 -0
  33. package/src/events/event-store.ts +588 -0
  34. package/src/events/example-usage.ts +293 -0
  35. package/src/events/index.ts +90 -0
  36. package/src/events/projections.ts +561 -0
  37. package/src/events/state-reconstructor.ts +349 -0
  38. package/src/events.ts +367 -0
  39. package/src/hooks/INTEGRATION.md +658 -0
  40. package/src/hooks/README.md +532 -0
  41. package/src/hooks/example-usage.ts +499 -0
  42. package/src/hooks/executor.ts +379 -0
  43. package/src/hooks/hooks.test.ts +421 -0
  44. package/src/hooks/index.ts +131 -0
  45. package/src/hooks/registry.ts +333 -0
  46. package/src/hooks/safety/bash-safety.ts +604 -0
  47. package/src/hooks/safety/file-organization.ts +473 -0
  48. package/src/hooks/safety/git-commit.ts +623 -0
  49. package/src/hooks/safety/index.ts +46 -0
  50. package/src/hooks/session-hooks.ts +559 -0
  51. package/src/hooks/task-hooks.ts +513 -0
  52. package/src/hooks/types.ts +357 -0
  53. package/src/hooks/verify-exports.test.ts +125 -0
  54. package/src/index.ts +195 -0
  55. package/src/mcp/connection-pool.ts +438 -0
  56. package/src/mcp/index.ts +183 -0
  57. package/src/mcp/server.ts +774 -0
  58. package/src/mcp/session-manager.ts +428 -0
  59. package/src/mcp/tool-registry.ts +566 -0
  60. package/src/mcp/transport/http.ts +557 -0
  61. package/src/mcp/transport/index.ts +294 -0
  62. package/src/mcp/transport/stdio.ts +324 -0
  63. package/src/mcp/transport/websocket.ts +484 -0
  64. package/src/mcp/types.ts +565 -0
  65. package/src/plugin-interface.ts +663 -0
  66. package/src/plugin-loader.ts +638 -0
  67. package/src/plugin-registry.ts +604 -0
  68. package/src/plugins/index.ts +34 -0
  69. package/src/plugins/official/hive-mind-plugin.ts +330 -0
  70. package/src/plugins/official/index.ts +24 -0
  71. package/src/plugins/official/maestro-plugin.ts +508 -0
  72. package/src/plugins/types.ts +108 -0
  73. package/src/resilience/bulkhead.ts +277 -0
  74. package/src/resilience/circuit-breaker.ts +326 -0
  75. package/src/resilience/index.ts +26 -0
  76. package/src/resilience/rate-limiter.ts +420 -0
  77. package/src/resilience/retry.ts +224 -0
  78. package/src/security/index.ts +39 -0
  79. package/src/security/input-validation.ts +265 -0
  80. package/src/security/secure-random.ts +159 -0
  81. package/src/services/index.ts +16 -0
  82. package/src/services/v3-progress.service.ts +505 -0
  83. package/src/types/agent.types.ts +144 -0
  84. package/src/types/index.ts +22 -0
  85. package/src/types/mcp.types.ts +300 -0
  86. package/src/types/memory.types.ts +263 -0
  87. package/src/types/swarm.types.ts +255 -0
  88. package/src/types/task.types.ts +205 -0
  89. package/src/types.ts +367 -0
  90. package/src/utils/secure-logger.d.ts +69 -0
  91. package/src/utils/secure-logger.d.ts.map +1 -0
  92. package/src/utils/secure-logger.js +208 -0
  93. package/src/utils/secure-logger.js.map +1 -0
  94. package/src/utils/secure-logger.ts +257 -0
  95. package/tmp.json +0 -0
  96. package/tsconfig.json +9 -0
@@ -0,0 +1,638 @@
1
+ /**
2
+ * V3 Plugin Loader
3
+ * Domain-Driven Design - Plugin-Based Architecture (ADR-004)
4
+ *
5
+ * Handles plugin loading, dependency resolution, and lifecycle management
6
+ */
7
+
8
+ import type {
9
+ ClaudeFlowPlugin,
10
+ PluginContext,
11
+ PluginInfo,
12
+ PluginLifecycleState,
13
+ PluginError as PluginErrorType,
14
+ PluginErrorCode,
15
+ } from './plugin-interface.js';
16
+ import { PluginError } from './plugin-interface.js';
17
+ import type { PluginRegistry } from './plugin-registry.js';
18
+
19
+ /**
20
+ * Plugin loader configuration
21
+ */
22
+ export interface PluginLoaderConfig {
23
+ /**
24
+ * Maximum time to wait for plugin initialization (ms)
25
+ */
26
+ initializationTimeout?: number;
27
+
28
+ /**
29
+ * Maximum time to wait for plugin shutdown (ms)
30
+ */
31
+ shutdownTimeout?: number;
32
+
33
+ /**
34
+ * Enable parallel plugin initialization
35
+ */
36
+ parallelInitialization?: boolean;
37
+
38
+ /**
39
+ * Enable strict dependency checking
40
+ */
41
+ strictDependencies?: boolean;
42
+
43
+ /**
44
+ * Enable health checks
45
+ */
46
+ enableHealthChecks?: boolean;
47
+
48
+ /**
49
+ * Health check interval (ms)
50
+ */
51
+ healthCheckInterval?: number;
52
+ }
53
+
54
+ /**
55
+ * Default plugin loader configuration
56
+ */
57
+ const DEFAULT_CONFIG: Required<PluginLoaderConfig> = {
58
+ initializationTimeout: 30000, // 30 seconds
59
+ shutdownTimeout: 10000, // 10 seconds
60
+ parallelInitialization: false, // Sequential by default for safety
61
+ strictDependencies: true,
62
+ enableHealthChecks: false,
63
+ healthCheckInterval: 60000, // 1 minute
64
+ };
65
+
66
+ /**
67
+ * Plugin dependency graph node
68
+ */
69
+ interface DependencyNode {
70
+ plugin: ClaudeFlowPlugin;
71
+ dependencies: Set<string>;
72
+ dependents: Set<string>;
73
+ depth: number;
74
+ }
75
+
76
+ /**
77
+ * Plugin loader for managing plugin lifecycle
78
+ */
79
+ export class PluginLoader {
80
+ private config: Required<PluginLoaderConfig>;
81
+ private registry: PluginRegistry;
82
+ private initializationOrder: string[] = [];
83
+ private healthCheckIntervalId?: NodeJS.Timeout;
84
+
85
+ constructor(registry: PluginRegistry, config?: PluginLoaderConfig) {
86
+ this.registry = registry;
87
+ this.config = { ...DEFAULT_CONFIG, ...config };
88
+ }
89
+
90
+ /**
91
+ * Load a single plugin
92
+ */
93
+ async loadPlugin(plugin: ClaudeFlowPlugin, context: PluginContext): Promise<void> {
94
+ // Validate plugin
95
+ this.validatePlugin(plugin);
96
+
97
+ // Check for duplicates
98
+ if (this.registry.hasPlugin(plugin.name)) {
99
+ throw new PluginError(
100
+ `Plugin '${plugin.name}' is already loaded`,
101
+ plugin.name,
102
+ 'DUPLICATE_PLUGIN'
103
+ );
104
+ }
105
+
106
+ // Register plugin in uninitialized state
107
+ this.registry.registerPlugin(plugin, 'uninitialized', context);
108
+
109
+ // Resolve dependencies
110
+ if (this.config.strictDependencies) {
111
+ this.validateDependencies(plugin);
112
+ }
113
+
114
+ // Initialize plugin
115
+ await this.initializePlugin(plugin, context);
116
+
117
+ // Update initialization order
118
+ this.initializationOrder.push(plugin.name);
119
+ }
120
+
121
+ /**
122
+ * Load multiple plugins with dependency resolution
123
+ */
124
+ async loadPlugins(
125
+ plugins: ClaudeFlowPlugin[],
126
+ context: PluginContext
127
+ ): Promise<LoadPluginsResult> {
128
+ const results: LoadPluginsResult = {
129
+ successful: [],
130
+ failed: [],
131
+ totalDuration: 0,
132
+ };
133
+
134
+ const startTime = Date.now();
135
+
136
+ try {
137
+ // Validate all plugins first
138
+ for (const plugin of plugins) {
139
+ this.validatePlugin(plugin);
140
+ }
141
+
142
+ // Build dependency graph
143
+ const dependencyGraph = this.buildDependencyGraph(plugins);
144
+
145
+ // Detect circular dependencies
146
+ this.detectCircularDependencies(dependencyGraph);
147
+
148
+ // Sort plugins by dependency order (topological sort)
149
+ const sortedPlugins = this.topologicalSort(dependencyGraph);
150
+
151
+ // Initialize plugins in order
152
+ if (this.config.parallelInitialization) {
153
+ await this.initializePluginsParallel(sortedPlugins, context, results);
154
+ } else {
155
+ await this.initializePluginsSequential(sortedPlugins, context, results);
156
+ }
157
+ } catch (error) {
158
+ // If error during setup, mark all as failed
159
+ for (const plugin of plugins) {
160
+ if (!results.successful.includes(plugin.name) && !results.failed.some((f) => f.name === plugin.name)) {
161
+ results.failed.push({
162
+ name: plugin.name,
163
+ error: error instanceof Error ? error : new Error(String(error)),
164
+ });
165
+ }
166
+ }
167
+ } finally {
168
+ results.totalDuration = Date.now() - startTime;
169
+ }
170
+
171
+ // Start health checks if enabled
172
+ if (this.config.enableHealthChecks) {
173
+ this.startHealthChecks();
174
+ }
175
+
176
+ return results;
177
+ }
178
+
179
+ /**
180
+ * Unload a single plugin
181
+ */
182
+ async unloadPlugin(pluginName: string): Promise<void> {
183
+ const pluginInfo = this.registry.getPlugin(pluginName);
184
+ if (!pluginInfo) {
185
+ throw new PluginError(
186
+ `Plugin '${pluginName}' not found`,
187
+ pluginName,
188
+ 'INVALID_PLUGIN'
189
+ );
190
+ }
191
+
192
+ // Check for dependents
193
+ const dependents = this.findDependents(pluginName);
194
+ if (dependents.length > 0) {
195
+ throw new PluginError(
196
+ `Cannot unload plugin '${pluginName}': depended on by ${dependents.join(', ')}`,
197
+ pluginName,
198
+ 'DEPENDENCY_NOT_FOUND'
199
+ );
200
+ }
201
+
202
+ // Shutdown plugin
203
+ await this.shutdownPlugin(pluginInfo.plugin);
204
+
205
+ // Unregister plugin
206
+ this.registry.unregisterPlugin(pluginName);
207
+
208
+ // Remove from initialization order
209
+ const index = this.initializationOrder.indexOf(pluginName);
210
+ if (index !== -1) {
211
+ this.initializationOrder.splice(index, 1);
212
+ }
213
+ }
214
+
215
+ /**
216
+ * Unload all plugins in reverse initialization order
217
+ */
218
+ async unloadAll(): Promise<void> {
219
+ // Stop health checks
220
+ if (this.healthCheckIntervalId) {
221
+ clearInterval(this.healthCheckIntervalId);
222
+ this.healthCheckIntervalId = undefined;
223
+ }
224
+
225
+ // Shutdown in reverse order
226
+ const pluginsToShutdown = [...this.initializationOrder].reverse();
227
+
228
+ for (const pluginName of pluginsToShutdown) {
229
+ try {
230
+ await this.unloadPlugin(pluginName);
231
+ } catch (error) {
232
+ // Log error but continue shutting down other plugins
233
+ console.error(`Error unloading plugin '${pluginName}':`, error);
234
+ }
235
+ }
236
+
237
+ this.initializationOrder = [];
238
+ }
239
+
240
+ /**
241
+ * Reload a plugin
242
+ */
243
+ async reloadPlugin(pluginName: string, newPlugin: ClaudeFlowPlugin, context: PluginContext): Promise<void> {
244
+ await this.unloadPlugin(pluginName);
245
+ await this.loadPlugin(newPlugin, context);
246
+ }
247
+
248
+ /**
249
+ * Get plugin initialization order
250
+ */
251
+ getInitializationOrder(): string[] {
252
+ return [...this.initializationOrder];
253
+ }
254
+
255
+ /**
256
+ * Validate plugin interface
257
+ */
258
+ private validatePlugin(plugin: ClaudeFlowPlugin): void {
259
+ if (!plugin.name) {
260
+ throw new PluginError(
261
+ 'Plugin must have a name',
262
+ '<unknown>',
263
+ 'INVALID_PLUGIN'
264
+ );
265
+ }
266
+
267
+ if (!plugin.version) {
268
+ throw new PluginError(
269
+ `Plugin '${plugin.name}' must have a version`,
270
+ plugin.name,
271
+ 'INVALID_PLUGIN'
272
+ );
273
+ }
274
+
275
+ if (typeof plugin.initialize !== 'function') {
276
+ throw new PluginError(
277
+ `Plugin '${plugin.name}' must implement initialize()`,
278
+ plugin.name,
279
+ 'INVALID_PLUGIN'
280
+ );
281
+ }
282
+
283
+ if (typeof plugin.shutdown !== 'function') {
284
+ throw new PluginError(
285
+ `Plugin '${plugin.name}' must implement shutdown()`,
286
+ plugin.name,
287
+ 'INVALID_PLUGIN'
288
+ );
289
+ }
290
+ }
291
+
292
+ /**
293
+ * Validate plugin dependencies
294
+ */
295
+ private validateDependencies(plugin: ClaudeFlowPlugin): void {
296
+ if (!plugin.dependencies || plugin.dependencies.length === 0) {
297
+ return;
298
+ }
299
+
300
+ for (const dep of plugin.dependencies) {
301
+ if (!this.registry.hasPlugin(dep)) {
302
+ throw new PluginError(
303
+ `Plugin '${plugin.name}' depends on '${dep}' which is not loaded`,
304
+ plugin.name,
305
+ 'DEPENDENCY_NOT_FOUND'
306
+ );
307
+ }
308
+
309
+ // Check dependency is initialized
310
+ const depInfo = this.registry.getPlugin(dep);
311
+ if (depInfo && depInfo.state !== 'initialized') {
312
+ throw new PluginError(
313
+ `Plugin '${plugin.name}' depends on '${dep}' which is not initialized (state: ${depInfo.state})`,
314
+ plugin.name,
315
+ 'DEPENDENCY_NOT_FOUND'
316
+ );
317
+ }
318
+ }
319
+ }
320
+
321
+ /**
322
+ * Initialize a single plugin
323
+ */
324
+ private async initializePlugin(plugin: ClaudeFlowPlugin, context: PluginContext): Promise<void> {
325
+ this.registry.updatePluginState(plugin.name, 'initializing');
326
+
327
+ try {
328
+ // Run initialization with timeout
329
+ await this.withTimeout(
330
+ plugin.initialize(context),
331
+ this.config.initializationTimeout,
332
+ `Plugin '${plugin.name}' initialization timed out`
333
+ );
334
+
335
+ this.registry.updatePluginState(plugin.name, 'initialized');
336
+ this.registry.collectPluginMetrics(plugin.name);
337
+ } catch (error) {
338
+ this.registry.updatePluginState(plugin.name, 'error', error instanceof Error ? error : new Error(String(error)));
339
+ throw new PluginError(
340
+ `Failed to initialize plugin '${plugin.name}': ${error}`,
341
+ plugin.name,
342
+ 'INITIALIZATION_FAILED',
343
+ error instanceof Error ? error : undefined
344
+ );
345
+ }
346
+ }
347
+
348
+ /**
349
+ * Shutdown a single plugin
350
+ */
351
+ private async shutdownPlugin(plugin: ClaudeFlowPlugin): Promise<void> {
352
+ this.registry.updatePluginState(plugin.name, 'shutting-down');
353
+
354
+ try {
355
+ await this.withTimeout(
356
+ plugin.shutdown(),
357
+ this.config.shutdownTimeout,
358
+ `Plugin '${plugin.name}' shutdown timed out`
359
+ );
360
+
361
+ this.registry.updatePluginState(plugin.name, 'shutdown');
362
+ } catch (error) {
363
+ this.registry.updatePluginState(plugin.name, 'error', error instanceof Error ? error : new Error(String(error)));
364
+ throw new PluginError(
365
+ `Failed to shutdown plugin '${plugin.name}': ${error}`,
366
+ plugin.name,
367
+ 'SHUTDOWN_FAILED',
368
+ error instanceof Error ? error : undefined
369
+ );
370
+ }
371
+ }
372
+
373
+ /**
374
+ * Initialize plugins sequentially
375
+ */
376
+ private async initializePluginsSequential(
377
+ plugins: ClaudeFlowPlugin[],
378
+ context: PluginContext,
379
+ results: LoadPluginsResult
380
+ ): Promise<void> {
381
+ for (const plugin of plugins) {
382
+ try {
383
+ // Register and initialize
384
+ this.registry.registerPlugin(plugin, 'uninitialized', context);
385
+ await this.initializePlugin(plugin, context);
386
+
387
+ results.successful.push(plugin.name);
388
+ this.initializationOrder.push(plugin.name);
389
+ } catch (error) {
390
+ results.failed.push({
391
+ name: plugin.name,
392
+ error: error instanceof Error ? error : new Error(String(error)),
393
+ });
394
+
395
+ // Stop on first failure in sequential mode if strict
396
+ if (this.config.strictDependencies) {
397
+ break;
398
+ }
399
+ }
400
+ }
401
+ }
402
+
403
+ /**
404
+ * Initialize plugins in parallel (by dependency level)
405
+ */
406
+ private async initializePluginsParallel(
407
+ plugins: ClaudeFlowPlugin[],
408
+ context: PluginContext,
409
+ results: LoadPluginsResult
410
+ ): Promise<void> {
411
+ // Group plugins by dependency depth
412
+ const dependencyGraph = this.buildDependencyGraph(plugins);
413
+ const levels = this.groupByDepth(dependencyGraph);
414
+
415
+ // Initialize each level in parallel
416
+ for (const level of levels) {
417
+ const promises = level.map(async (plugin) => {
418
+ try {
419
+ this.registry.registerPlugin(plugin, 'uninitialized', context);
420
+ await this.initializePlugin(plugin, context);
421
+
422
+ results.successful.push(plugin.name);
423
+ this.initializationOrder.push(plugin.name);
424
+ } catch (error) {
425
+ results.failed.push({
426
+ name: plugin.name,
427
+ error: error instanceof Error ? error : new Error(String(error)),
428
+ });
429
+ }
430
+ });
431
+
432
+ await Promise.all(promises);
433
+
434
+ // Stop on failures in level if strict
435
+ if (this.config.strictDependencies && results.failed.length > 0) {
436
+ break;
437
+ }
438
+ }
439
+ }
440
+
441
+ /**
442
+ * Build dependency graph
443
+ */
444
+ private buildDependencyGraph(plugins: ClaudeFlowPlugin[]): Map<string, DependencyNode> {
445
+ const graph = new Map<string, DependencyNode>();
446
+
447
+ // Create nodes
448
+ for (const plugin of plugins) {
449
+ graph.set(plugin.name, {
450
+ plugin,
451
+ dependencies: new Set(plugin.dependencies || []),
452
+ dependents: new Set(),
453
+ depth: 0,
454
+ });
455
+ }
456
+
457
+ // Build dependency links
458
+ for (const [name, node] of Array.from(graph.entries())) {
459
+ for (const dep of Array.from(node.dependencies)) {
460
+ const depNode = graph.get(dep);
461
+ if (depNode) {
462
+ depNode.dependents.add(name);
463
+ }
464
+ }
465
+ }
466
+
467
+ // Calculate depths
468
+ this.calculateDepths(graph);
469
+
470
+ return graph;
471
+ }
472
+
473
+ /**
474
+ * Calculate depth of each node (for topological sorting)
475
+ */
476
+ private calculateDepths(graph: Map<string, DependencyNode>): void {
477
+ const visited = new Set<string>();
478
+
479
+ const visit = (name: string): number => {
480
+ if (visited.has(name)) {
481
+ const node = graph.get(name);
482
+ return node ? node.depth : 0;
483
+ }
484
+
485
+ visited.add(name);
486
+ const node = graph.get(name);
487
+ if (!node) return 0;
488
+
489
+ let maxDepth = 0;
490
+ for (const dep of Array.from(node.dependencies)) {
491
+ maxDepth = Math.max(maxDepth, visit(dep) + 1);
492
+ }
493
+
494
+ node.depth = maxDepth;
495
+ return maxDepth;
496
+ };
497
+
498
+ for (const name of Array.from(graph.keys())) {
499
+ visit(name);
500
+ }
501
+ }
502
+
503
+ /**
504
+ * Topological sort (dependency order)
505
+ */
506
+ private topologicalSort(graph: Map<string, DependencyNode>): ClaudeFlowPlugin[] {
507
+ const sorted: ClaudeFlowPlugin[] = [];
508
+ const nodes = Array.from(graph.values());
509
+
510
+ // Sort by depth (dependencies first)
511
+ nodes.sort((a, b) => a.depth - b.depth);
512
+
513
+ for (const node of nodes) {
514
+ sorted.push(node.plugin);
515
+ }
516
+
517
+ return sorted;
518
+ }
519
+
520
+ /**
521
+ * Group plugins by dependency depth (for parallel initialization)
522
+ */
523
+ private groupByDepth(graph: Map<string, DependencyNode>): ClaudeFlowPlugin[][] {
524
+ const levels: ClaudeFlowPlugin[][] = [];
525
+ const maxDepth = Math.max(...Array.from(graph.values()).map((n) => n.depth));
526
+
527
+ for (let depth = 0; depth <= maxDepth; depth++) {
528
+ const level: ClaudeFlowPlugin[] = [];
529
+ for (const node of Array.from(graph.values())) {
530
+ if (node.depth === depth) {
531
+ level.push(node.plugin);
532
+ }
533
+ }
534
+ if (level.length > 0) {
535
+ levels.push(level);
536
+ }
537
+ }
538
+
539
+ return levels;
540
+ }
541
+
542
+ /**
543
+ * Detect circular dependencies
544
+ */
545
+ private detectCircularDependencies(graph: Map<string, DependencyNode>): void {
546
+ const visited = new Set<string>();
547
+ const stack = new Set<string>();
548
+
549
+ const visit = (name: string, path: string[]): void => {
550
+ if (stack.has(name)) {
551
+ const cycle = [...path, name];
552
+ throw new PluginError(
553
+ `Circular dependency detected: ${cycle.join(' -> ')}`,
554
+ name,
555
+ 'CIRCULAR_DEPENDENCY'
556
+ );
557
+ }
558
+
559
+ if (visited.has(name)) {
560
+ return;
561
+ }
562
+
563
+ visited.add(name);
564
+ stack.add(name);
565
+
566
+ const node = graph.get(name);
567
+ if (node) {
568
+ for (const dep of Array.from(node.dependencies)) {
569
+ visit(dep, [...path, name]);
570
+ }
571
+ }
572
+
573
+ stack.delete(name);
574
+ };
575
+
576
+ for (const name of Array.from(graph.keys())) {
577
+ visit(name, []);
578
+ }
579
+ }
580
+
581
+ /**
582
+ * Find plugins that depend on a given plugin
583
+ */
584
+ private findDependents(pluginName: string): string[] {
585
+ const dependents: string[] = [];
586
+
587
+ for (const [name, info] of Array.from(this.registry.getAllPlugins().entries())) {
588
+ if (info.plugin.dependencies?.includes(pluginName)) {
589
+ dependents.push(name);
590
+ }
591
+ }
592
+
593
+ return dependents;
594
+ }
595
+
596
+ /**
597
+ * Start periodic health checks
598
+ */
599
+ private startHealthChecks(): void {
600
+ this.healthCheckIntervalId = setInterval(async () => {
601
+ for (const [name, info] of Array.from(this.registry.getAllPlugins().entries())) {
602
+ if (info.state === 'initialized' && info.plugin.healthCheck) {
603
+ try {
604
+ const healthy = await info.plugin.healthCheck();
605
+ if (!healthy) {
606
+ console.warn(`Plugin '${name}' health check failed`);
607
+ this.registry.updatePluginState(name, 'error', new Error('Health check failed'));
608
+ }
609
+ } catch (error) {
610
+ console.error(`Plugin '${name}' health check error:`, error);
611
+ this.registry.updatePluginState(name, 'error', error instanceof Error ? error : new Error(String(error)));
612
+ }
613
+ }
614
+ }
615
+ }, this.config.healthCheckInterval);
616
+ }
617
+
618
+ /**
619
+ * Utility: Run promise with timeout
620
+ */
621
+ private async withTimeout<T>(promise: Promise<T>, timeoutMs: number, errorMessage: string): Promise<T> {
622
+ return Promise.race([
623
+ promise,
624
+ new Promise<T>((_, reject) =>
625
+ setTimeout(() => reject(new Error(errorMessage)), timeoutMs)
626
+ ),
627
+ ]);
628
+ }
629
+ }
630
+
631
+ /**
632
+ * Load plugins result
633
+ */
634
+ export interface LoadPluginsResult {
635
+ successful: string[];
636
+ failed: Array<{ name: string; error: Error }>;
637
+ totalDuration: number;
638
+ }