@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,532 @@
1
+ # V3 Hooks System
2
+
3
+ Extensible hook points for tool execution, file operations, and lifecycle events. Integrates with the event bus for coordination and monitoring.
4
+
5
+ ## Features
6
+
7
+ - **26 Hook Events**: Comprehensive lifecycle hooks for tools, files, commands, sessions, agents, tasks, memory, and errors
8
+ - **Priority-Based Execution**: Control execution order with 5 priority levels
9
+ - **Timeout Handling**: Configurable timeouts per hook with graceful degradation
10
+ - **Error Recovery**: Continue execution on errors or abort based on configuration
11
+ - **Context Modification**: Hooks can modify context for downstream hooks
12
+ - **Parallel & Sequential Execution**: Execute hooks in parallel or chain them sequentially
13
+ - **Event Bus Integration**: Emit coordination events to the event bus
14
+ - **Statistics Tracking**: Monitor hook performance and execution metrics
15
+
16
+ ## Installation
17
+
18
+ ```typescript
19
+ import {
20
+ createHookRegistry,
21
+ createHookExecutor,
22
+ HookEvent,
23
+ HookPriority
24
+ } from '@claude-flow/shared/hooks';
25
+ ```
26
+
27
+ ## Quick Start
28
+
29
+ ### 1. Create Registry and Executor
30
+
31
+ ```typescript
32
+ import { createHookRegistry, createHookExecutor } from '@claude-flow/shared/hooks';
33
+ import { createEventBus } from '@claude-flow/shared/core';
34
+
35
+ const registry = createHookRegistry();
36
+ const eventBus = createEventBus();
37
+ const executor = createHookExecutor(registry, eventBus);
38
+ ```
39
+
40
+ ### 2. Register a Hook
41
+
42
+ ```typescript
43
+ const hookId = registry.register(
44
+ HookEvent.PreToolUse,
45
+ async (context) => {
46
+ console.log(`About to use tool: ${context.tool?.name}`);
47
+
48
+ // Validate tool parameters
49
+ if (!context.tool?.parameters) {
50
+ return {
51
+ success: false,
52
+ error: new Error('Missing tool parameters'),
53
+ abort: true // Abort tool execution
54
+ };
55
+ }
56
+
57
+ return { success: true };
58
+ },
59
+ HookPriority.High,
60
+ {
61
+ name: 'Tool Validation Hook',
62
+ timeout: 5000
63
+ }
64
+ );
65
+ ```
66
+
67
+ ### 3. Execute Hooks
68
+
69
+ ```typescript
70
+ const result = await executor.execute(
71
+ HookEvent.PreToolUse,
72
+ {
73
+ event: HookEvent.PreToolUse,
74
+ timestamp: new Date(),
75
+ tool: {
76
+ name: 'Read',
77
+ parameters: { path: '/path/to/file.ts' },
78
+ category: 'file'
79
+ }
80
+ }
81
+ );
82
+
83
+ if (result.success) {
84
+ console.log(`✓ All hooks passed (${result.hooksExecuted} executed)`);
85
+ } else {
86
+ console.log(`✗ Hook execution failed: ${result.hooksFailed} failures`);
87
+ }
88
+ ```
89
+
90
+ ## Hook Events
91
+
92
+ ### Tool Execution
93
+
94
+ ```typescript
95
+ HookEvent.PreToolUse // Before any tool is used
96
+ HookEvent.PostToolUse // After tool execution completes
97
+ ```
98
+
99
+ ### File Operations
100
+
101
+ ```typescript
102
+ HookEvent.PreRead // Before reading a file
103
+ HookEvent.PostRead // After reading a file
104
+ HookEvent.PreWrite // Before writing a file
105
+ HookEvent.PostWrite // After writing a file
106
+ HookEvent.PreEdit // Before editing a file
107
+ HookEvent.PostEdit // After editing a file
108
+ ```
109
+
110
+ ### Command Execution
111
+
112
+ ```typescript
113
+ HookEvent.PreCommand // Before executing bash command
114
+ HookEvent.PostCommand // After command execution
115
+ ```
116
+
117
+ ### Session Lifecycle
118
+
119
+ ```typescript
120
+ HookEvent.SessionStart // When session starts
121
+ HookEvent.SessionEnd // When session ends
122
+ HookEvent.SessionPause // When session is paused
123
+ HookEvent.SessionResume // When session resumes
124
+ ```
125
+
126
+ ### Agent Lifecycle
127
+
128
+ ```typescript
129
+ HookEvent.PreAgentSpawn // Before spawning agent
130
+ HookEvent.PostAgentSpawn // After agent spawned
131
+ HookEvent.PreAgentTerminate // Before terminating agent
132
+ HookEvent.PostAgentTerminate // After agent terminated
133
+ ```
134
+
135
+ ### Task Lifecycle
136
+
137
+ ```typescript
138
+ HookEvent.PreTaskExecute // Before task execution
139
+ HookEvent.PostTaskExecute // After task execution
140
+ HookEvent.PreTaskComplete // Before marking task complete
141
+ HookEvent.PostTaskComplete // After task completed
142
+ ```
143
+
144
+ ### Memory Operations
145
+
146
+ ```typescript
147
+ HookEvent.PreMemoryStore // Before storing memory
148
+ HookEvent.PostMemoryStore // After storing memory
149
+ HookEvent.PreMemoryRetrieve // Before retrieving memory
150
+ HookEvent.PostMemoryRetrieve // After retrieving memory
151
+ ```
152
+
153
+ ### Error Handling
154
+
155
+ ```typescript
156
+ HookEvent.OnError // When error occurs
157
+ HookEvent.OnWarning // When warning occurs
158
+ ```
159
+
160
+ ## Hook Priorities
161
+
162
+ Control execution order with priority levels:
163
+
164
+ ```typescript
165
+ HookPriority.Critical = 1000 // Execute first
166
+ HookPriority.High = 500
167
+ HookPriority.Normal = 0 // Default
168
+ HookPriority.Low = -500
169
+ HookPriority.Lowest = -1000 // Execute last
170
+ ```
171
+
172
+ ## Advanced Usage
173
+
174
+ ### Context Modification
175
+
176
+ Hooks can modify context for downstream hooks:
177
+
178
+ ```typescript
179
+ registry.register(
180
+ HookEvent.PreCommand,
181
+ async (context) => {
182
+ // Add risk assessment to context
183
+ const isDestructive = context.command?.command.includes('rm -rf');
184
+
185
+ return {
186
+ success: true,
187
+ data: {
188
+ metadata: {
189
+ ...context.metadata,
190
+ riskLevel: isDestructive ? 'high' : 'low'
191
+ }
192
+ }
193
+ };
194
+ },
195
+ HookPriority.High
196
+ );
197
+
198
+ // Later hook can access the risk level
199
+ registry.register(
200
+ HookEvent.PreCommand,
201
+ async (context) => {
202
+ if (context.metadata?.riskLevel === 'high') {
203
+ console.warn('⚠️ High-risk command detected!');
204
+ }
205
+ return { success: true };
206
+ },
207
+ HookPriority.Normal
208
+ );
209
+ ```
210
+
211
+ ### Abort Operations
212
+
213
+ Hooks can abort the operation:
214
+
215
+ ```typescript
216
+ registry.register(
217
+ HookEvent.PreCommand,
218
+ async (context) => {
219
+ const isDangerous = context.command?.command.includes('format c:');
220
+
221
+ if (isDangerous) {
222
+ return {
223
+ success: false,
224
+ abort: true, // Prevent command execution
225
+ error: new Error('Dangerous command blocked by security hook')
226
+ };
227
+ }
228
+
229
+ return { success: true };
230
+ },
231
+ HookPriority.Critical
232
+ );
233
+ ```
234
+
235
+ ### Timeout Handling
236
+
237
+ Configure timeouts per hook:
238
+
239
+ ```typescript
240
+ registry.register(
241
+ HookEvent.PreToolUse,
242
+ async (context) => {
243
+ // Expensive operation
244
+ await analyzeCodeComplexity(context.tool?.parameters);
245
+ return { success: true };
246
+ },
247
+ HookPriority.Normal,
248
+ {
249
+ timeout: 3000 // 3 second timeout
250
+ }
251
+ );
252
+ ```
253
+
254
+ ### Parallel Execution
255
+
256
+ Execute hooks for multiple events in parallel:
257
+
258
+ ```typescript
259
+ const results = await executor.executeParallel(
260
+ [HookEvent.PreRead, HookEvent.PreWrite, HookEvent.PreEdit],
261
+ [
262
+ { event: HookEvent.PreRead, timestamp: new Date(), file: { path: 'a.ts', operation: 'read' } },
263
+ { event: HookEvent.PreWrite, timestamp: new Date(), file: { path: 'b.ts', operation: 'write' } },
264
+ { event: HookEvent.PreEdit, timestamp: new Date(), file: { path: 'c.ts', operation: 'edit' } }
265
+ ],
266
+ { maxParallel: 3 }
267
+ );
268
+ ```
269
+
270
+ ### Sequential Execution with Context Chaining
271
+
272
+ Execute hooks sequentially, passing context modifications:
273
+
274
+ ```typescript
275
+ const result = await executor.executeSequential(
276
+ [
277
+ HookEvent.PreTaskExecute,
278
+ HookEvent.PostTaskExecute,
279
+ HookEvent.PreTaskComplete,
280
+ HookEvent.PostTaskComplete
281
+ ],
282
+ {
283
+ event: HookEvent.PreTaskExecute,
284
+ timestamp: new Date(),
285
+ task: { id: 'task-1', description: 'Process data' }
286
+ }
287
+ );
288
+
289
+ // result.finalContext contains all modifications from all hooks
290
+ ```
291
+
292
+ ## Hook Statistics
293
+
294
+ Track hook performance:
295
+
296
+ ```typescript
297
+ const stats = registry.getStats();
298
+
299
+ console.log(`Total hooks: ${stats.totalHooks}`);
300
+ console.log(`Total executions: ${stats.totalExecutions}`);
301
+ console.log(`Total failures: ${stats.totalFailures}`);
302
+ console.log(`Average execution time: ${stats.avgExecutionTime}ms`);
303
+
304
+ // Hooks by event type
305
+ for (const [event, count] of Object.entries(stats.byEvent)) {
306
+ console.log(`${event}: ${count} hooks`);
307
+ }
308
+ ```
309
+
310
+ ## Integration with Event Bus
311
+
312
+ The executor emits coordination events to the event bus:
313
+
314
+ ```typescript
315
+ eventBus.on('hooks:pre-execute', (event) => {
316
+ console.log(`Executing ${event.payload.hookCount} hooks for ${event.payload.event}`);
317
+ });
318
+
319
+ eventBus.on('hooks:post-execute', (event) => {
320
+ console.log(`Completed in ${event.payload.totalExecutionTime}ms`);
321
+ });
322
+
323
+ eventBus.on('hooks:error', (event) => {
324
+ console.error(`Hook ${event.payload.hookId} failed:`, event.payload.error);
325
+ });
326
+ ```
327
+
328
+ ## Best Practices
329
+
330
+ ### 1. Use Appropriate Priorities
331
+
332
+ - `Critical`: Security checks, validation that must happen first
333
+ - `High`: Important preprocessing (risk assessment, logging)
334
+ - `Normal`: Standard business logic
335
+ - `Low`: Optional enhancements, metrics
336
+ - `Lowest`: Cleanup, final logging
337
+
338
+ ### 2. Handle Errors Gracefully
339
+
340
+ ```typescript
341
+ registry.register(
342
+ HookEvent.PreToolUse,
343
+ async (context) => {
344
+ try {
345
+ await performValidation(context);
346
+ return { success: true };
347
+ } catch (error) {
348
+ return {
349
+ success: false,
350
+ error: error instanceof Error ? error : new Error(String(error)),
351
+ continueChain: true // Allow other hooks to run
352
+ };
353
+ }
354
+ }
355
+ );
356
+ ```
357
+
358
+ ### 3. Keep Hooks Fast
359
+
360
+ Hooks should be fast (<100ms). For expensive operations:
361
+
362
+ ```typescript
363
+ registry.register(
364
+ HookEvent.PostToolUse,
365
+ async (context) => {
366
+ // Queue expensive work for background processing
367
+ backgroundQueue.add({
368
+ type: 'analyze-tool-usage',
369
+ context
370
+ });
371
+
372
+ return { success: true };
373
+ },
374
+ HookPriority.Low
375
+ );
376
+ ```
377
+
378
+ ### 4. Use Descriptive Names
379
+
380
+ ```typescript
381
+ registry.register(
382
+ HookEvent.PreCommand,
383
+ handler,
384
+ HookPriority.Critical,
385
+ {
386
+ name: 'Security: Prevent Destructive Commands',
387
+ metadata: {
388
+ purpose: 'Block dangerous shell commands',
389
+ blockedPatterns: ['rm -rf /', 'format c:']
390
+ }
391
+ }
392
+ );
393
+ ```
394
+
395
+ ## Example: Pre-Edit Hook for Learning
396
+
397
+ ```typescript
398
+ import { HookEvent, HookPriority } from '@claude-flow/shared/hooks';
399
+
400
+ // Register pre-edit hook for context retrieval
401
+ registry.register(
402
+ HookEvent.PreEdit,
403
+ async (context) => {
404
+ const filePath = context.file?.path;
405
+
406
+ if (!filePath) {
407
+ return { success: true };
408
+ }
409
+
410
+ // Get similar past edits from ReasoningBank
411
+ const similarEdits = await reasoningBank.searchPatterns({
412
+ task: `Edit file: ${filePath}`,
413
+ k: 5,
414
+ minReward: 0.85
415
+ });
416
+
417
+ console.log(`📚 Found ${similarEdits.length} similar past edits`);
418
+
419
+ return {
420
+ success: true,
421
+ data: {
422
+ metadata: {
423
+ ...context.metadata,
424
+ learningContext: similarEdits,
425
+ editCount: similarEdits.length
426
+ }
427
+ }
428
+ };
429
+ },
430
+ HookPriority.High,
431
+ {
432
+ name: 'Learning: Pre-Edit Context Retrieval',
433
+ timeout: 2000
434
+ }
435
+ );
436
+
437
+ // Register post-edit hook for learning storage
438
+ registry.register(
439
+ HookEvent.PostEdit,
440
+ async (context) => {
441
+ const success = context.metadata?.success ?? true;
442
+ const filePath = context.file?.path;
443
+
444
+ if (!filePath) {
445
+ return { success: true };
446
+ }
447
+
448
+ // Store edit pattern for future learning
449
+ await reasoningBank.storePattern({
450
+ sessionId: context.session?.id || 'unknown',
451
+ task: `Edit file: ${filePath}`,
452
+ input: context.file?.previousContent || '',
453
+ output: context.file?.content || '',
454
+ reward: success ? 0.9 : 0.3,
455
+ success,
456
+ tokensUsed: estimateTokens(context.file?.content),
457
+ latencyMs: context.metadata?.executionTime || 0
458
+ });
459
+
460
+ return { success: true };
461
+ },
462
+ HookPriority.Normal,
463
+ {
464
+ name: 'Learning: Post-Edit Pattern Storage',
465
+ timeout: 1000
466
+ }
467
+ );
468
+ ```
469
+
470
+ ## API Reference
471
+
472
+ ### HookRegistry
473
+
474
+ - `register(event, handler, priority, options)` - Register a hook
475
+ - `unregister(hookId)` - Unregister a hook
476
+ - `unregisterAll(event?)` - Unregister all hooks for an event
477
+ - `getHandlers(event, includeDisabled)` - Get handlers for an event
478
+ - `getHook(hookId)` - Get hook by ID
479
+ - `enable(hookId)` - Enable a hook
480
+ - `disable(hookId)` - Disable a hook
481
+ - `listHooks(filter)` - List all hooks with optional filter
482
+ - `getEventTypes()` - Get all event types with hooks
483
+ - `count(event?)` - Get hook count
484
+ - `getStats()` - Get execution statistics
485
+ - `resetStats()` - Reset statistics
486
+ - `has(hookId)` - Check if hook exists
487
+ - `clear()` - Clear all hooks
488
+
489
+ ### HookExecutor
490
+
491
+ - `execute(event, context, options)` - Execute hooks for an event
492
+ - `executeWithTimeout(event, context, timeout)` - Execute with timeout
493
+ - `executeParallel(events, contexts, options)` - Execute multiple events in parallel
494
+ - `executeSequential(events, initialContext, options)` - Execute sequentially with context chaining
495
+ - `setEventBus(eventBus)` - Set event bus for coordination
496
+ - `getRegistry()` - Get hook registry
497
+
498
+ ## Testing
499
+
500
+ Run the test suite:
501
+
502
+ ```bash
503
+ cd /workspaces/claude-flow/v3/@claude-flow/shared
504
+ npm test -- hooks.test.ts
505
+ ```
506
+
507
+ All 23 tests pass:
508
+ - 11 registry tests
509
+ - 12 executor tests
510
+
511
+ ## File Structure
512
+
513
+ ```
514
+ v3/@claude-flow/shared/src/hooks/
515
+ ├── types.ts # Type definitions (~150 lines)
516
+ ├── registry.ts # Hook registry (~200 lines)
517
+ ├── executor.ts # Hook executor (~250 lines)
518
+ ├── index.ts # Main exports
519
+ ├── hooks.test.ts # Test suite (~20 tests)
520
+ └── README.md # This file
521
+ ```
522
+
523
+ ## Integration Points
524
+
525
+ - **Event Bus**: Emits coordination events (`hooks:pre-execute`, `hooks:post-execute`, `hooks:error`)
526
+ - **Event Store**: Can log hook executions for audit trail (ADR-007)
527
+ - **ReasoningBank**: Hooks can integrate with learning system for context retrieval and pattern storage
528
+ - **Security**: Critical hooks can enforce security policies (CVE-1, CVE-2, CVE-3)
529
+
530
+ ## License
531
+
532
+ Part of Claude-Flow V3 - See main LICENSE file