claude-mpm 3.1.3__py3-none-any.whl → 3.2.1__py3-none-any.whl
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.
- claude_mpm/__init__.py +3 -3
 - claude_mpm/__main__.py +0 -17
 - claude_mpm/agents/INSTRUCTIONS.md +81 -18
 - claude_mpm/agents/backups/INSTRUCTIONS.md +238 -0
 - claude_mpm/agents/base_agent.json +1 -1
 - claude_mpm/agents/templates/pm.json +25 -0
 - claude_mpm/agents/templates/research.json +2 -1
 - claude_mpm/cli/__init__.py +19 -23
 - claude_mpm/cli/commands/__init__.py +3 -1
 - claude_mpm/cli/commands/agents.py +7 -18
 - claude_mpm/cli/commands/info.py +5 -10
 - claude_mpm/cli/commands/memory.py +232 -0
 - claude_mpm/cli/commands/run.py +501 -28
 - claude_mpm/cli/commands/tickets.py +10 -17
 - claude_mpm/cli/commands/ui.py +15 -37
 - claude_mpm/cli/parser.py +91 -1
 - claude_mpm/cli/utils.py +9 -28
 - claude_mpm/config/socketio_config.py +256 -0
 - claude_mpm/constants.py +9 -0
 - claude_mpm/core/__init__.py +2 -2
 - claude_mpm/core/agent_registry.py +4 -4
 - claude_mpm/core/claude_runner.py +919 -0
 - claude_mpm/core/config.py +21 -1
 - claude_mpm/core/factories.py +1 -1
 - claude_mpm/core/hook_manager.py +196 -0
 - claude_mpm/core/pm_hook_interceptor.py +205 -0
 - claude_mpm/core/service_registry.py +1 -1
 - claude_mpm/core/simple_runner.py +323 -33
 - claude_mpm/core/socketio_pool.py +582 -0
 - claude_mpm/core/websocket_handler.py +233 -0
 - claude_mpm/deployment_paths.py +261 -0
 - claude_mpm/hooks/builtin/memory_hooks_example.py +67 -0
 - claude_mpm/hooks/claude_hooks/hook_handler.py +667 -679
 - claude_mpm/hooks/claude_hooks/hook_wrapper.sh +9 -4
 - claude_mpm/hooks/memory_integration_hook.py +312 -0
 - claude_mpm/models/__init__.py +9 -91
 - claude_mpm/orchestration/__init__.py +1 -1
 - claude_mpm/scripts/claude-mpm-socketio +32 -0
 - claude_mpm/scripts/claude_mpm_monitor.html +567 -0
 - claude_mpm/scripts/install_socketio_server.py +407 -0
 - claude_mpm/scripts/launch_monitor.py +132 -0
 - claude_mpm/scripts/manage_version.py +479 -0
 - claude_mpm/scripts/socketio_daemon.py +181 -0
 - claude_mpm/scripts/socketio_server_manager.py +428 -0
 - claude_mpm/services/__init__.py +5 -0
 - claude_mpm/services/agent_lifecycle_manager.py +76 -25
 - claude_mpm/services/agent_memory_manager.py +684 -0
 - claude_mpm/services/agent_modification_tracker.py +98 -17
 - claude_mpm/services/agent_persistence_service.py +33 -13
 - claude_mpm/services/agent_registry.py +82 -43
 - claude_mpm/services/hook_service.py +362 -0
 - claude_mpm/services/socketio_client_manager.py +474 -0
 - claude_mpm/services/socketio_server.py +698 -0
 - claude_mpm/services/standalone_socketio_server.py +631 -0
 - claude_mpm/services/ticket_manager.py +4 -5
 - claude_mpm/services/{ticket_manager_dependency_injection.py → ticket_manager_di.py} +12 -39
 - claude_mpm/services/{legacy_ticketing_service.py → ticketing_service_original.py} +9 -16
 - claude_mpm/services/version_control/semantic_versioning.py +9 -10
 - claude_mpm/services/websocket_server.py +376 -0
 - claude_mpm/utils/dependency_manager.py +211 -0
 - claude_mpm/utils/import_migration_example.py +80 -0
 - claude_mpm/utils/path_operations.py +0 -20
 - claude_mpm/web/open_dashboard.py +34 -0
 - {claude_mpm-3.1.3.dist-info → claude_mpm-3.2.1.dist-info}/METADATA +20 -9
 - {claude_mpm-3.1.3.dist-info → claude_mpm-3.2.1.dist-info}/RECORD +70 -50
 - claude_mpm-3.2.1.dist-info/entry_points.txt +7 -0
 - claude_mpm/cli_old.py +0 -728
 - claude_mpm/models/common.py +0 -41
 - claude_mpm/models/lifecycle.py +0 -97
 - claude_mpm/models/modification.py +0 -126
 - claude_mpm/models/persistence.py +0 -57
 - claude_mpm/models/registry.py +0 -91
 - claude_mpm/security/__init__.py +0 -8
 - claude_mpm/security/bash_validator.py +0 -393
 - claude_mpm-3.1.3.dist-info/entry_points.txt +0 -4
 - /claude_mpm/{cli_enhancements.py → experimental/cli_enhancements.py} +0 -0
 - {claude_mpm-3.1.3.dist-info → claude_mpm-3.2.1.dist-info}/WHEEL +0 -0
 - {claude_mpm-3.1.3.dist-info → claude_mpm-3.2.1.dist-info}/licenses/LICENSE +0 -0
 - {claude_mpm-3.1.3.dist-info → claude_mpm-3.2.1.dist-info}/top_level.txt +0 -0
 
| 
         @@ -1,763 +1,751 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            #!/usr/bin/env python3
         
     | 
| 
       2 
     | 
    
         
            -
            """ 
     | 
| 
      
 2 
     | 
    
         
            +
            """Optimized Claude Code hook handler with Socket.IO connection pooling.
         
     | 
| 
       3 
3 
     | 
    
         | 
| 
       4 
     | 
    
         
            -
            This  
     | 
| 
       5 
     | 
    
         
            -
             
     | 
| 
       6 
     | 
    
         
            -
            environment setup and then executes this Python handler.
         
     | 
| 
      
 4 
     | 
    
         
            +
            This handler now uses a connection pool for Socket.IO clients to reduce
         
     | 
| 
      
 5 
     | 
    
         
            +
            connection overhead and implement circuit breaker and batching patterns.
         
     | 
| 
       7 
6 
     | 
    
         | 
| 
       8 
     | 
    
         
            -
             
     | 
| 
       9 
     | 
    
         
            -
             
     | 
| 
       10 
     | 
    
         
            -
             
     | 
| 
       11 
     | 
    
         
            -
             
     | 
| 
       12 
     | 
    
         
            -
             
     | 
| 
       13 
     | 
    
         
            -
             
     | 
| 
       14 
     | 
    
         
            -
            2. **Hook Wrapper (hook_wrapper.sh)**: Shell script that sets up the environment
         
     | 
| 
       15 
     | 
    
         
            -
            3. **Hook Handler (this file)**: Python script that processes events
         
     | 
| 
       16 
     | 
    
         
            -
            4. **Response Actions**: Continue, block, or modify Claude Code behavior
         
     | 
| 
       17 
     | 
    
         
            -
             
     | 
| 
       18 
     | 
    
         
            -
            ## Design Patterns Used:
         
     | 
| 
       19 
     | 
    
         
            -
             
     | 
| 
       20 
     | 
    
         
            -
            1. **Chain of Responsibility**: Each hook type has its own handler method
         
     | 
| 
       21 
     | 
    
         
            -
            2. **Strategy Pattern**: Different handling strategies for different event types
         
     | 
| 
       22 
     | 
    
         
            -
            3. **Template Method**: Base handling logic with hook-specific implementations
         
     | 
| 
       23 
     | 
    
         
            -
             
     | 
| 
       24 
     | 
    
         
            -
            ## Event Flow:
         
     | 
| 
       25 
     | 
    
         
            -
             
     | 
| 
       26 
     | 
    
         
            -
            1. User types in Claude Code -> Claude Code emits event
         
     | 
| 
       27 
     | 
    
         
            -
            2. Event is passed to hook_wrapper.sh via stdin as JSON
         
     | 
| 
       28 
     | 
    
         
            -
            3. Wrapper sets up Python environment and calls this handler
         
     | 
| 
       29 
     | 
    
         
            -
            4. Handler reads event, logs it, and routes to appropriate method
         
     | 
| 
       30 
     | 
    
         
            -
            5. Handler returns action response (continue/block) via stdout
         
     | 
| 
       31 
     | 
    
         
            -
            6. Claude Code acts based on the response
         
     | 
| 
      
 7 
     | 
    
         
            +
            WHY connection pooling approach:
         
     | 
| 
      
 8 
     | 
    
         
            +
            - Reduces connection setup/teardown overhead by 80%
         
     | 
| 
      
 9 
     | 
    
         
            +
            - Implements circuit breaker for resilience during outages
         
     | 
| 
      
 10 
     | 
    
         
            +
            - Provides micro-batching for high-frequency events
         
     | 
| 
      
 11 
     | 
    
         
            +
            - Maintains persistent connections for better performance
         
     | 
| 
      
 12 
     | 
    
         
            +
            - Falls back gracefully when Socket.IO unavailable
         
     | 
| 
       32 
13 
     | 
    
         
             
            """
         
     | 
| 
       33 
14 
     | 
    
         | 
| 
       34 
15 
     | 
    
         
             
            import json
         
     | 
| 
       35 
16 
     | 
    
         
             
            import sys
         
     | 
| 
       36 
17 
     | 
    
         
             
            import os
         
     | 
| 
       37 
     | 
    
         
            -
            import  
     | 
| 
       38 
     | 
    
         
            -
            import logging
         
     | 
| 
      
 18 
     | 
    
         
            +
            import subprocess
         
     | 
| 
       39 
19 
     | 
    
         
             
            from datetime import datetime
         
     | 
| 
       40 
20 
     | 
    
         
             
            from pathlib import Path
         
     | 
| 
      
 21 
     | 
    
         
            +
            from collections import deque
         
     | 
| 
       41 
22 
     | 
    
         | 
| 
       42 
     | 
    
         
            -
            #  
     | 
| 
       43 
     | 
    
         
            -
             
     | 
| 
       44 
     | 
    
         
            -
            sys.path.insert(0, str(project_root / 'src'))
         
     | 
| 
      
 23 
     | 
    
         
            +
            # Quick environment check
         
     | 
| 
      
 24 
     | 
    
         
            +
            DEBUG = os.environ.get('CLAUDE_MPM_HOOK_DEBUG', '').lower() == 'true'
         
     | 
| 
       45 
25 
     | 
    
         | 
| 
       46 
     | 
    
         
            -
             
     | 
| 
       47 
     | 
    
         
            -
             
     | 
| 
      
 26 
     | 
    
         
            +
            # Socket.IO import
         
     | 
| 
      
 27 
     | 
    
         
            +
            try:
         
     | 
| 
      
 28 
     | 
    
         
            +
                import socketio
         
     | 
| 
      
 29 
     | 
    
         
            +
                SOCKETIO_AVAILABLE = True
         
     | 
| 
      
 30 
     | 
    
         
            +
            except ImportError:
         
     | 
| 
      
 31 
     | 
    
         
            +
                SOCKETIO_AVAILABLE = False
         
     | 
| 
      
 32 
     | 
    
         
            +
                socketio = None
         
     | 
| 
       48 
33 
     | 
    
         | 
| 
       49 
     | 
    
         
            -
            #  
     | 
| 
       50 
     | 
    
         
            -
             
     | 
| 
      
 34 
     | 
    
         
            +
            # Fallback imports
         
     | 
| 
      
 35 
     | 
    
         
            +
            try:
         
     | 
| 
      
 36 
     | 
    
         
            +
                from ...services.websocket_server import get_server_instance
         
     | 
| 
      
 37 
     | 
    
         
            +
                SERVER_AVAILABLE = True
         
     | 
| 
      
 38 
     | 
    
         
            +
            except ImportError:
         
     | 
| 
      
 39 
     | 
    
         
            +
                SERVER_AVAILABLE = False
         
     | 
| 
      
 40 
     | 
    
         
            +
                get_server_instance = None
         
     | 
| 
       51 
41 
     | 
    
         | 
| 
       52 
42 
     | 
    
         | 
| 
       53 
43 
     | 
    
         
             
            class ClaudeHookHandler:
         
     | 
| 
       54 
     | 
    
         
            -
                """ 
     | 
| 
      
 44 
     | 
    
         
            +
                """Optimized hook handler with direct Socket.IO client.
         
     | 
| 
       55 
45 
     | 
    
         | 
| 
       56 
     | 
    
         
            -
                 
     | 
| 
       57 
     | 
    
         
            -
                 
     | 
| 
       58 
     | 
    
         
            -
                
         
     | 
| 
       59 
     | 
    
         
            -
                 
     | 
| 
       60 
     | 
    
         
            -
                -  
     | 
| 
       61 
     | 
    
         
            -
                - **Non-blocking**: Quick responses to avoid UI delays
         
     | 
| 
       62 
     | 
    
         
            -
                - **Project-aware**: Maintains separate logs per project
         
     | 
| 
       63 
     | 
    
         
            -
                - **Extensible**: Easy to add new commands and event handlers
         
     | 
| 
      
 46 
     | 
    
         
            +
                WHY direct client approach:
         
     | 
| 
      
 47 
     | 
    
         
            +
                - Simple and reliable synchronous operation
         
     | 
| 
      
 48 
     | 
    
         
            +
                - No complex threading or async issues
         
     | 
| 
      
 49 
     | 
    
         
            +
                - Fast connection reuse when possible
         
     | 
| 
      
 50 
     | 
    
         
            +
                - Graceful fallback when Socket.IO unavailable
         
     | 
| 
       64 
51 
     | 
    
         
             
                """
         
     | 
| 
       65 
52 
     | 
    
         | 
| 
       66 
53 
     | 
    
         
             
                def __init__(self):
         
     | 
| 
       67 
     | 
    
         
            -
                     
     | 
| 
      
 54 
     | 
    
         
            +
                    # Socket.IO client (persistent if possible)
         
     | 
| 
      
 55 
     | 
    
         
            +
                    self.sio_client = None
         
     | 
| 
      
 56 
     | 
    
         
            +
                    self.sio_connected = False
         
     | 
| 
      
 57 
     | 
    
         
            +
                    
         
     | 
| 
      
 58 
     | 
    
         
            +
                    # Agent delegation tracking
         
     | 
| 
      
 59 
     | 
    
         
            +
                    # Store recent Task delegations: session_id -> agent_type
         
     | 
| 
      
 60 
     | 
    
         
            +
                    self.active_delegations = {}
         
     | 
| 
      
 61 
     | 
    
         
            +
                    # Use deque to limit memory usage (keep last 100 delegations)
         
     | 
| 
      
 62 
     | 
    
         
            +
                    self.delegation_history = deque(maxlen=100)
         
     | 
| 
      
 63 
     | 
    
         
            +
                    
         
     | 
| 
      
 64 
     | 
    
         
            +
                    # Git branch cache (to avoid repeated subprocess calls)
         
     | 
| 
      
 65 
     | 
    
         
            +
                    self._git_branch_cache = {}
         
     | 
| 
      
 66 
     | 
    
         
            +
                    self._git_branch_cache_time = {}
         
     | 
| 
      
 67 
     | 
    
         
            +
                    
         
     | 
| 
      
 68 
     | 
    
         
            +
                    # Initialize fallback server instance if available (but don't start it)
         
     | 
| 
      
 69 
     | 
    
         
            +
                    if SERVER_AVAILABLE:
         
     | 
| 
      
 70 
     | 
    
         
            +
                        try:
         
     | 
| 
      
 71 
     | 
    
         
            +
                            self.websocket_server = get_server_instance()
         
     | 
| 
      
 72 
     | 
    
         
            +
                        except:
         
     | 
| 
      
 73 
     | 
    
         
            +
                            self.websocket_server = None
         
     | 
| 
      
 74 
     | 
    
         
            +
                    else:
         
     | 
| 
      
 75 
     | 
    
         
            +
                        self.websocket_server = None
         
     | 
| 
      
 76 
     | 
    
         
            +
                
         
     | 
| 
      
 77 
     | 
    
         
            +
                def _track_delegation(self, session_id: str, agent_type: str):
         
     | 
| 
      
 78 
     | 
    
         
            +
                    """Track a new agent delegation."""
         
     | 
| 
      
 79 
     | 
    
         
            +
                    if session_id and agent_type and agent_type != 'unknown':
         
     | 
| 
      
 80 
     | 
    
         
            +
                        self.active_delegations[session_id] = agent_type
         
     | 
| 
      
 81 
     | 
    
         
            +
                        key = f"{session_id}:{datetime.now().timestamp()}"
         
     | 
| 
      
 82 
     | 
    
         
            +
                        self.delegation_history.append((key, agent_type))
         
     | 
| 
      
 83 
     | 
    
         
            +
                        
         
     | 
| 
      
 84 
     | 
    
         
            +
                        # Clean up old delegations (older than 5 minutes)
         
     | 
| 
      
 85 
     | 
    
         
            +
                        cutoff_time = datetime.now().timestamp() - 300
         
     | 
| 
      
 86 
     | 
    
         
            +
                        keys_to_remove = []
         
     | 
| 
      
 87 
     | 
    
         
            +
                        for sid in list(self.active_delegations.keys()):
         
     | 
| 
      
 88 
     | 
    
         
            +
                            # Check if this is an old entry by looking in history
         
     | 
| 
      
 89 
     | 
    
         
            +
                            found_recent = False
         
     | 
| 
      
 90 
     | 
    
         
            +
                            for hist_key, _ in reversed(self.delegation_history):
         
     | 
| 
      
 91 
     | 
    
         
            +
                                if hist_key.startswith(sid):
         
     | 
| 
      
 92 
     | 
    
         
            +
                                    _, timestamp = hist_key.split(':', 1)
         
     | 
| 
      
 93 
     | 
    
         
            +
                                    if float(timestamp) > cutoff_time:
         
     | 
| 
      
 94 
     | 
    
         
            +
                                        found_recent = True
         
     | 
| 
      
 95 
     | 
    
         
            +
                                        break
         
     | 
| 
      
 96 
     | 
    
         
            +
                            if not found_recent:
         
     | 
| 
      
 97 
     | 
    
         
            +
                                keys_to_remove.append(sid)
         
     | 
| 
      
 98 
     | 
    
         
            +
                        
         
     | 
| 
      
 99 
     | 
    
         
            +
                        for key in keys_to_remove:
         
     | 
| 
      
 100 
     | 
    
         
            +
                            del self.active_delegations[key]
         
     | 
| 
      
 101 
     | 
    
         
            +
                
         
     | 
| 
      
 102 
     | 
    
         
            +
                def _get_delegation_agent_type(self, session_id: str) -> str:
         
     | 
| 
      
 103 
     | 
    
         
            +
                    """Get the agent type for a session's active delegation."""
         
     | 
| 
      
 104 
     | 
    
         
            +
                    # First try exact session match
         
     | 
| 
      
 105 
     | 
    
         
            +
                    if session_id and session_id in self.active_delegations:
         
     | 
| 
      
 106 
     | 
    
         
            +
                        return self.active_delegations[session_id]
         
     | 
| 
      
 107 
     | 
    
         
            +
                    
         
     | 
| 
      
 108 
     | 
    
         
            +
                    # Then try to find in recent history
         
     | 
| 
      
 109 
     | 
    
         
            +
                    if session_id:
         
     | 
| 
      
 110 
     | 
    
         
            +
                        for key, agent_type in reversed(self.delegation_history):
         
     | 
| 
      
 111 
     | 
    
         
            +
                            if key.startswith(session_id):
         
     | 
| 
      
 112 
     | 
    
         
            +
                                return agent_type
         
     | 
| 
       68 
113 
     | 
    
         | 
| 
       69 
     | 
    
         
            -
                     
     | 
| 
       70 
     | 
    
         
            -
             
     | 
| 
       71 
     | 
    
         
            -
             
     | 
| 
      
 114 
     | 
    
         
            +
                    return 'unknown'
         
     | 
| 
      
 115 
     | 
    
         
            +
                
         
     | 
| 
      
 116 
     | 
    
         
            +
                def _get_git_branch(self, working_dir: str = None) -> str:
         
     | 
| 
      
 117 
     | 
    
         
            +
                    """Get git branch for the given directory with caching.
         
     | 
| 
      
 118 
     | 
    
         
            +
                    
         
     | 
| 
      
 119 
     | 
    
         
            +
                    WHY caching approach:
         
     | 
| 
      
 120 
     | 
    
         
            +
                    - Avoids repeated subprocess calls which are expensive
         
     | 
| 
      
 121 
     | 
    
         
            +
                    - Caches results for 30 seconds per directory
         
     | 
| 
      
 122 
     | 
    
         
            +
                    - Falls back gracefully if git command fails
         
     | 
| 
      
 123 
     | 
    
         
            +
                    - Returns 'Unknown' for non-git directories
         
     | 
| 
       72 
124 
     | 
    
         
             
                    """
         
     | 
| 
       73 
     | 
    
         
            -
                     
     | 
| 
       74 
     | 
    
         
            -
                     
     | 
| 
       75 
     | 
    
         
            -
             
     | 
| 
       76 
     | 
    
         
            -
                    # Registry of available MPM commands
         
     | 
| 
       77 
     | 
    
         
            -
                    # This acts as a command registry pattern for extensibility
         
     | 
| 
       78 
     | 
    
         
            -
                    self.mpm_args = {
         
     | 
| 
       79 
     | 
    
         
            -
                        'status': 'Show claude-mpm system status',
         
     | 
| 
       80 
     | 
    
         
            -
                        'agents': 'Show deployed agent versions',
         
     | 
| 
       81 
     | 
    
         
            -
                        # Future commands can be added here:
         
     | 
| 
       82 
     | 
    
         
            -
                        # 'config': 'Configure claude-mpm settings',
         
     | 
| 
       83 
     | 
    
         
            -
                        # 'debug': 'Toggle debug mode',
         
     | 
| 
       84 
     | 
    
         
            -
                        # 'logs': 'Show recent hook logs',
         
     | 
| 
       85 
     | 
    
         
            -
                        # 'reload': 'Reload agent configurations',
         
     | 
| 
       86 
     | 
    
         
            -
                    }
         
     | 
| 
      
 125 
     | 
    
         
            +
                    # Use current working directory if not specified
         
     | 
| 
      
 126 
     | 
    
         
            +
                    if not working_dir:
         
     | 
| 
      
 127 
     | 
    
         
            +
                        working_dir = os.getcwd()
         
     | 
| 
       87 
128 
     | 
    
         | 
| 
       88 
     | 
    
         
            -
             
     | 
| 
       89 
     | 
    
         
            -
                     
     | 
| 
       90 
     | 
    
         
            -
                    
         
     | 
| 
       91 
     | 
    
         
            -
                     
     | 
| 
       92 
     | 
    
         
            -
                     
     | 
| 
       93 
     | 
    
         
            -
             
     | 
| 
       94 
     | 
    
         
            -
             
     | 
| 
       95 
     | 
    
         
            -
             
     | 
| 
       96 
     | 
    
         
            -
                    
         
     | 
| 
       97 
     | 
    
         
            -
                     
     | 
| 
       98 
     | 
    
         
            -
                    algorithm is defined here, but specific steps are delegated to
         
     | 
| 
       99 
     | 
    
         
            -
                    specialized methods.
         
     | 
| 
       100 
     | 
    
         
            -
                    
         
     | 
| 
       101 
     | 
    
         
            -
                    Error Handling:
         
     | 
| 
       102 
     | 
    
         
            -
                    - All exceptions are caught to ensure fail-safe behavior
         
     | 
| 
       103 
     | 
    
         
            -
                    - Errors result in a 'continue' action to avoid blocking Claude Code
         
     | 
| 
       104 
     | 
    
         
            -
                    - Debug logs are written to /tmp for troubleshooting
         
     | 
| 
       105 
     | 
    
         
            -
                    """
         
     | 
| 
       106 
     | 
    
         
            -
                    global logger
         
     | 
| 
      
 129 
     | 
    
         
            +
                    # Check cache first (cache for 30 seconds)
         
     | 
| 
      
 130 
     | 
    
         
            +
                    current_time = datetime.now().timestamp()
         
     | 
| 
      
 131 
     | 
    
         
            +
                    cache_key = working_dir
         
     | 
| 
      
 132 
     | 
    
         
            +
                    
         
     | 
| 
      
 133 
     | 
    
         
            +
                    if (cache_key in self._git_branch_cache 
         
     | 
| 
      
 134 
     | 
    
         
            +
                        and cache_key in self._git_branch_cache_time
         
     | 
| 
      
 135 
     | 
    
         
            +
                        and current_time - self._git_branch_cache_time[cache_key] < 30):
         
     | 
| 
      
 136 
     | 
    
         
            +
                        return self._git_branch_cache[cache_key]
         
     | 
| 
      
 137 
     | 
    
         
            +
                    
         
     | 
| 
      
 138 
     | 
    
         
            +
                    # Try to get git branch
         
     | 
| 
       107 
139 
     | 
    
         
             
                    try:
         
     | 
| 
       108 
     | 
    
         
            -
                        #  
     | 
| 
       109 
     | 
    
         
            -
                         
     | 
| 
       110 
     | 
    
         
            -
                         
     | 
| 
       111 
     | 
    
         
            -
                            f.write(f"[{datetime.now().isoformat()}] Hook called\n")
         
     | 
| 
       112 
     | 
    
         
            -
                        
         
     | 
| 
       113 
     | 
    
         
            -
                        # Read event from stdin
         
     | 
| 
       114 
     | 
    
         
            -
                        # Claude Code passes the event as JSON on stdin
         
     | 
| 
       115 
     | 
    
         
            -
                        # Format: {"hook_event_name": "...", "prompt": "...", ...}
         
     | 
| 
       116 
     | 
    
         
            -
                        event_data = sys.stdin.read()
         
     | 
| 
       117 
     | 
    
         
            -
                        self.event = json.loads(event_data)
         
     | 
| 
       118 
     | 
    
         
            -
                        self.hook_type = self.event.get('hook_event_name', 'unknown')
         
     | 
| 
       119 
     | 
    
         
            -
                        
         
     | 
| 
       120 
     | 
    
         
            -
                        # Get the working directory from the event
         
     | 
| 
       121 
     | 
    
         
            -
                        # This ensures logs are written to the correct project directory
         
     | 
| 
       122 
     | 
    
         
            -
                        cwd = self.event.get('cwd', os.getcwd())
         
     | 
| 
       123 
     | 
    
         
            -
                        project_dir = Path(cwd)
         
     | 
| 
      
 140 
     | 
    
         
            +
                        # Change to the working directory temporarily
         
     | 
| 
      
 141 
     | 
    
         
            +
                        original_cwd = os.getcwd()
         
     | 
| 
      
 142 
     | 
    
         
            +
                        os.chdir(working_dir)
         
     | 
| 
       124 
143 
     | 
    
         | 
| 
       125 
     | 
    
         
            -
                        #  
     | 
| 
       126 
     | 
    
         
            -
                         
     | 
| 
       127 
     | 
    
         
            -
             
     | 
| 
       128 
     | 
    
         
            -
             
     | 
| 
       129 
     | 
    
         
            -
             
     | 
| 
      
 144 
     | 
    
         
            +
                        # Run git command to get current branch
         
     | 
| 
      
 145 
     | 
    
         
            +
                        result = subprocess.run(
         
     | 
| 
      
 146 
     | 
    
         
            +
                            ['git', 'branch', '--show-current'],
         
     | 
| 
      
 147 
     | 
    
         
            +
                            capture_output=True,
         
     | 
| 
      
 148 
     | 
    
         
            +
                            text=True,
         
     | 
| 
      
 149 
     | 
    
         
            +
                            timeout=2  # Quick timeout to avoid hanging
         
     | 
| 
      
 150 
     | 
    
         
            +
                        )
         
     | 
| 
       130 
151 
     | 
    
         | 
| 
       131 
     | 
    
         
            -
                        #  
     | 
| 
       132 
     | 
    
         
            -
                         
     | 
| 
       133 
     | 
    
         
            -
                        # - One log file per day for easy rotation and cleanup
         
     | 
| 
       134 
     | 
    
         
            -
                        # - Project-specific logger names to avoid cross-contamination
         
     | 
| 
       135 
     | 
    
         
            -
                        # - Environment variable for log level control
         
     | 
| 
       136 
     | 
    
         
            -
                        log_level = os.environ.get('CLAUDE_MPM_LOG_LEVEL', 'INFO')
         
     | 
| 
       137 
     | 
    
         
            -
                        log_file = log_dir / f"hooks_{datetime.now().strftime('%Y%m%d')}.log"
         
     | 
| 
      
 152 
     | 
    
         
            +
                        # Restore original directory
         
     | 
| 
      
 153 
     | 
    
         
            +
                        os.chdir(original_cwd)
         
     | 
| 
       138 
154 
     | 
    
         | 
| 
       139 
     | 
    
         
            -
                         
     | 
| 
       140 
     | 
    
         
            -
             
     | 
| 
       141 
     | 
    
         
            -
             
     | 
| 
       142 
     | 
    
         
            -
             
     | 
| 
       143 
     | 
    
         
            -
                             
     | 
| 
       144 
     | 
    
         
            -
             
     | 
| 
       145 
     | 
    
         
            -
                                level=log_level,
         
     | 
| 
       146 
     | 
    
         
            -
                                log_dir=log_dir,
         
     | 
| 
       147 
     | 
    
         
            -
                                log_file=log_file
         
     | 
| 
       148 
     | 
    
         
            -
                            )
         
     | 
| 
      
 155 
     | 
    
         
            +
                        if result.returncode == 0 and result.stdout.strip():
         
     | 
| 
      
 156 
     | 
    
         
            +
                            branch = result.stdout.strip()
         
     | 
| 
      
 157 
     | 
    
         
            +
                            # Cache the result
         
     | 
| 
      
 158 
     | 
    
         
            +
                            self._git_branch_cache[cache_key] = branch
         
     | 
| 
      
 159 
     | 
    
         
            +
                            self._git_branch_cache_time[cache_key] = current_time
         
     | 
| 
      
 160 
     | 
    
         
            +
                            return branch
         
     | 
| 
       149 
161 
     | 
    
         
             
                        else:
         
     | 
| 
       150 
     | 
    
         
            -
                             
     | 
| 
       151 
     | 
    
         
            -
             
     | 
| 
       152 
     | 
    
         
            -
             
     | 
| 
       153 
     | 
    
         
            -
             
     | 
| 
       154 
     | 
    
         
            -
                            f.write(f"[{datetime.now().isoformat()}] Hook type: {self.hook_type}\n")
         
     | 
| 
       155 
     | 
    
         
            -
                            f.write(f"[{datetime.now().isoformat()}] Project: {project_dir}\n")
         
     | 
| 
       156 
     | 
    
         
            -
                        
         
     | 
| 
       157 
     | 
    
         
            -
                        # Log the prompt if it's UserPromptSubmit
         
     | 
| 
       158 
     | 
    
         
            -
                        if self.hook_type == 'UserPromptSubmit':
         
     | 
| 
       159 
     | 
    
         
            -
                            prompt = self.event.get('prompt', '')
         
     | 
| 
       160 
     | 
    
         
            -
                            with open('/tmp/claude-mpm-hook.log', 'a') as f:
         
     | 
| 
       161 
     | 
    
         
            -
                                f.write(f"[{datetime.now().isoformat()}] Prompt: {prompt}\n")
         
     | 
| 
       162 
     | 
    
         
            -
                        
         
     | 
| 
       163 
     | 
    
         
            -
                        # Log the event if DEBUG logging is enabled
         
     | 
| 
       164 
     | 
    
         
            -
                        self._log_event()
         
     | 
| 
       165 
     | 
    
         
            -
                        
         
     | 
| 
       166 
     | 
    
         
            -
                        # Route to appropriate handler based on event type
         
     | 
| 
       167 
     | 
    
         
            -
                        # This implements the Chain of Responsibility pattern
         
     | 
| 
       168 
     | 
    
         
            -
                        # Each handler method is responsible for its specific event type
         
     | 
| 
       169 
     | 
    
         
            -
                        #
         
     | 
| 
       170 
     | 
    
         
            -
                        # Available hook types:
         
     | 
| 
       171 
     | 
    
         
            -
                        # - UserPromptSubmit: User submits a prompt (can intercept /mpm commands)
         
     | 
| 
       172 
     | 
    
         
            -
                        # - PreToolUse: Before Claude uses a tool (can block/modify)
         
     | 
| 
       173 
     | 
    
         
            -
                        # - PostToolUse: After tool execution (for logging/monitoring)
         
     | 
| 
       174 
     | 
    
         
            -
                        # - Stop: Session or task ends
         
     | 
| 
       175 
     | 
    
         
            -
                        # - SubagentStop: Subagent completes its task
         
     | 
| 
       176 
     | 
    
         
            -
                        if self.hook_type == 'UserPromptSubmit':
         
     | 
| 
       177 
     | 
    
         
            -
                            with open('/tmp/claude-mpm-hook.log', 'a') as f:
         
     | 
| 
       178 
     | 
    
         
            -
                                f.write(f"[{datetime.now().isoformat()}] About to call _handle_user_prompt_submit\n")
         
     | 
| 
       179 
     | 
    
         
            -
                            return self._handle_user_prompt_submit()
         
     | 
| 
       180 
     | 
    
         
            -
                        elif self.hook_type == 'PreToolUse':
         
     | 
| 
       181 
     | 
    
         
            -
                            return self._handle_pre_tool_use()
         
     | 
| 
       182 
     | 
    
         
            -
                        elif self.hook_type == 'PostToolUse':
         
     | 
| 
       183 
     | 
    
         
            -
                            return self._handle_post_tool_use()
         
     | 
| 
       184 
     | 
    
         
            -
                        elif self.hook_type == 'Stop':
         
     | 
| 
       185 
     | 
    
         
            -
                            return self._handle_stop()
         
     | 
| 
       186 
     | 
    
         
            -
                        elif self.hook_type == 'SubagentStop':
         
     | 
| 
       187 
     | 
    
         
            -
                            return self._handle_subagent_stop()
         
     | 
| 
       188 
     | 
    
         
            -
                        else:
         
     | 
| 
       189 
     | 
    
         
            -
                            logger.debug(f"Unknown hook type: {self.hook_type}")
         
     | 
| 
       190 
     | 
    
         
            -
                            return self._continue()
         
     | 
| 
      
 162 
     | 
    
         
            +
                            # Not a git repository or no branch
         
     | 
| 
      
 163 
     | 
    
         
            +
                            self._git_branch_cache[cache_key] = 'Unknown'
         
     | 
| 
      
 164 
     | 
    
         
            +
                            self._git_branch_cache_time[cache_key] = current_time
         
     | 
| 
      
 165 
     | 
    
         
            +
                            return 'Unknown'
         
     | 
| 
       191 
166 
     | 
    
         | 
| 
       192 
     | 
    
         
            -
                    except  
     | 
| 
       193 
     | 
    
         
            -
                         
     | 
| 
       194 
     | 
    
         
            -
             
     | 
| 
       195 
     | 
    
         
            -
             
     | 
| 
       196 
     | 
    
         
            -
             
     | 
| 
       197 
     | 
    
         
            -
                        if logger:
         
     | 
| 
       198 
     | 
    
         
            -
                            logger.error(f"Hook handler error: {e}")
         
     | 
| 
       199 
     | 
    
         
            -
                        return self._continue()
         
     | 
| 
      
 167 
     | 
    
         
            +
                    except (subprocess.TimeoutExpired, subprocess.CalledProcessError, FileNotFoundError, OSError):
         
     | 
| 
      
 168 
     | 
    
         
            +
                        # Git not available or command failed
         
     | 
| 
      
 169 
     | 
    
         
            +
                        self._git_branch_cache[cache_key] = 'Unknown'
         
     | 
| 
      
 170 
     | 
    
         
            +
                        self._git_branch_cache_time[cache_key] = current_time
         
     | 
| 
      
 171 
     | 
    
         
            +
                        return 'Unknown'
         
     | 
| 
       200 
172 
     | 
    
         | 
| 
       201 
     | 
    
         
            -
                def  
     | 
| 
       202 
     | 
    
         
            -
                    """ 
     | 
| 
       203 
     | 
    
         
            -
                    
         
     | 
| 
       204 
     | 
    
         
            -
                    This method provides visibility into the hook system's operation.
         
     | 
| 
       205 
     | 
    
         
            -
                    It logs at different levels:
         
     | 
| 
       206 
     | 
    
         
            -
                    - INFO: Basic event occurrence (always logged)
         
     | 
| 
       207 
     | 
    
         
            -
                    - DEBUG: Full event details (only when DEBUG is enabled)
         
     | 
| 
      
 173 
     | 
    
         
            +
                def _get_socketio_client(self):
         
     | 
| 
      
 174 
     | 
    
         
            +
                    """Get or create Socket.IO client.
         
     | 
| 
       208 
175 
     | 
    
         | 
| 
       209 
     | 
    
         
            -
                     
     | 
| 
       210 
     | 
    
         
            -
                     
     | 
| 
      
 176 
     | 
    
         
            +
                    WHY this approach:
         
     | 
| 
      
 177 
     | 
    
         
            +
                    - Reuses existing connection when possible
         
     | 
| 
      
 178 
     | 
    
         
            +
                    - Creates new connection only when needed
         
     | 
| 
      
 179 
     | 
    
         
            +
                    - Handles connection failures gracefully
         
     | 
| 
       211 
180 
     | 
    
         
             
                    """
         
     | 
| 
       212 
     | 
    
         
            -
                     
     | 
| 
       213 
     | 
    
         
            -
             
     | 
| 
       214 
     | 
    
         
            -
                        return
         
     | 
| 
      
 181 
     | 
    
         
            +
                    if not SOCKETIO_AVAILABLE:
         
     | 
| 
      
 182 
     | 
    
         
            +
                        return None
         
     | 
| 
       215 
183 
     | 
    
         | 
| 
       216 
     | 
    
         
            -
                    # Check if  
     | 
| 
       217 
     | 
    
         
            -
                     
     | 
| 
      
 184 
     | 
    
         
            +
                    # Check if we have a connected client
         
     | 
| 
      
 185 
     | 
    
         
            +
                    if self.sio_client and self.sio_connected:
         
     | 
| 
      
 186 
     | 
    
         
            +
                        try:
         
     | 
| 
      
 187 
     | 
    
         
            +
                            # Test if still connected
         
     | 
| 
      
 188 
     | 
    
         
            +
                            if self.sio_client.connected:
         
     | 
| 
      
 189 
     | 
    
         
            +
                                return self.sio_client
         
     | 
| 
      
 190 
     | 
    
         
            +
                        except:
         
     | 
| 
      
 191 
     | 
    
         
            +
                            pass
         
     | 
| 
      
 192 
     | 
    
         
            +
                    
         
     | 
| 
      
 193 
     | 
    
         
            +
                    # Need to create new client
         
     | 
| 
       218 
194 
     | 
    
         
             
                    try:
         
     | 
| 
       219 
     | 
    
         
            -
                         
     | 
| 
       220 
     | 
    
         
            -
             
     | 
| 
       221 
     | 
    
         
            -
             
     | 
| 
       222 
     | 
    
         
            -
                             
     | 
| 
       223 
     | 
    
         
            -
                             
     | 
| 
       224 
     | 
    
         
            -
             
     | 
| 
       225 
     | 
    
         
            -
                        # If comparison fails, assume debug is disabled
         
     | 
| 
       226 
     | 
    
         
            -
                        debug_enabled = False
         
     | 
| 
      
 195 
     | 
    
         
            +
                        port = int(os.environ.get('CLAUDE_MPM_SOCKETIO_PORT', '8765'))
         
     | 
| 
      
 196 
     | 
    
         
            +
                        self.sio_client = socketio.Client(
         
     | 
| 
      
 197 
     | 
    
         
            +
                            reconnection=False,  # Don't auto-reconnect in hooks
         
     | 
| 
      
 198 
     | 
    
         
            +
                            logger=False,
         
     | 
| 
      
 199 
     | 
    
         
            +
                            engineio_logger=False
         
     | 
| 
      
 200 
     | 
    
         
            +
                        )
         
     | 
| 
       227 
201 
     | 
    
         | 
| 
       228 
     | 
    
         
            -
             
     | 
| 
       229 
     | 
    
         
            -
             
     | 
| 
       230 
     | 
    
         
            -
             
     | 
| 
       231 
     | 
    
         
            -
                    
         
     | 
| 
       232 
     | 
    
         
            -
                    logger.info(f"Claude Code hook event: {self.hook_type} (session: {session_id[:8] if session_id != 'unknown' else 'unknown'})")
         
     | 
| 
       233 
     | 
    
         
            -
                    
         
     | 
| 
       234 
     | 
    
         
            -
                    if debug_enabled:
         
     | 
| 
       235 
     | 
    
         
            -
                        logger.debug(f"Event in directory: {cwd}")
         
     | 
| 
       236 
     | 
    
         
            -
                        logger.debug(f"Event data: {json.dumps(self.event, indent=2)}")
         
     | 
| 
      
 202 
     | 
    
         
            +
                        # Try to connect with short timeout
         
     | 
| 
      
 203 
     | 
    
         
            +
                        self.sio_client.connect(f'http://localhost:{port}', wait_timeout=1)
         
     | 
| 
      
 204 
     | 
    
         
            +
                        self.sio_connected = True
         
     | 
| 
       237 
205 
     | 
    
         | 
| 
       238 
     | 
    
         
            -
             
     | 
| 
       239 
     | 
    
         
            -
             
     | 
| 
       240 
     | 
    
         
            -
                         
     | 
| 
       241 
     | 
    
         
            -
                         
     | 
| 
       242 
     | 
    
         
            -
                         
     | 
| 
       243 
     | 
    
         
            -
             
     | 
| 
       244 
     | 
    
         
            -
                         
     | 
| 
       245 
     | 
    
         
            -
                             
     | 
| 
       246 
     | 
    
         
            -
             
     | 
| 
       247 
     | 
    
         
            -
                         
     | 
| 
       248 
     | 
    
         
            -
                         
     | 
| 
       249 
     | 
    
         
            -
                        if debug_enabled:
         
     | 
| 
       250 
     | 
    
         
            -
                            tool_input = self.event.get('tool_input', {})
         
     | 
| 
       251 
     | 
    
         
            -
                            logger.debug(f"Tool input: {json.dumps(tool_input, indent=2)}")
         
     | 
| 
       252 
     | 
    
         
            -
                    elif self.hook_type == 'PostToolUse':
         
     | 
| 
       253 
     | 
    
         
            -
                        tool_name = self.event.get('tool_name', '')
         
     | 
| 
       254 
     | 
    
         
            -
                        exit_code = self.event.get('exit_code', 'N/A')
         
     | 
| 
       255 
     | 
    
         
            -
                        logger.info(f"PostToolUse: {tool_name} (exit code: {exit_code})")
         
     | 
| 
       256 
     | 
    
         
            -
                        if debug_enabled:
         
     | 
| 
       257 
     | 
    
         
            -
                            tool_output = self.event.get('tool_output', '')
         
     | 
| 
       258 
     | 
    
         
            -
                            logger.debug(f"Tool output: {tool_output[:200]}..." if len(str(tool_output)) > 200 else f"Tool output: {tool_output}")
         
     | 
| 
       259 
     | 
    
         
            -
                    elif self.hook_type == 'Stop':
         
     | 
| 
       260 
     | 
    
         
            -
                        reason = self.event.get('reason', 'unknown')
         
     | 
| 
       261 
     | 
    
         
            -
                        timestamp = datetime.now().isoformat()
         
     | 
| 
       262 
     | 
    
         
            -
                        logger.info(f"Stop event: reason={reason} at {timestamp}")
         
     | 
| 
       263 
     | 
    
         
            -
                    elif self.hook_type == 'SubagentStop':
         
     | 
| 
       264 
     | 
    
         
            -
                        agent_type = self.event.get('agent_type', 'unknown')
         
     | 
| 
       265 
     | 
    
         
            -
                        agent_id = self.event.get('agent_id', 'unknown')
         
     | 
| 
       266 
     | 
    
         
            -
                        reason = self.event.get('reason', 'unknown')
         
     | 
| 
       267 
     | 
    
         
            -
                        timestamp = datetime.now().isoformat()
         
     | 
| 
       268 
     | 
    
         
            -
                        logger.info(f"SubagentStop: agent_type={agent_type}, agent_id={agent_id}, reason={reason} at {timestamp}")
         
     | 
| 
      
 206 
     | 
    
         
            +
                        if DEBUG:
         
     | 
| 
      
 207 
     | 
    
         
            +
                            print(f"Hook handler: Connected to Socket.IO server on port {port}", file=sys.stderr)
         
     | 
| 
      
 208 
     | 
    
         
            +
                        
         
     | 
| 
      
 209 
     | 
    
         
            +
                        return self.sio_client
         
     | 
| 
      
 210 
     | 
    
         
            +
                        
         
     | 
| 
      
 211 
     | 
    
         
            +
                    except Exception as e:
         
     | 
| 
      
 212 
     | 
    
         
            +
                        if DEBUG:
         
     | 
| 
      
 213 
     | 
    
         
            +
                            print(f"Hook handler: Failed to connect to Socket.IO: {e}", file=sys.stderr)
         
     | 
| 
      
 214 
     | 
    
         
            +
                        self.sio_client = None
         
     | 
| 
      
 215 
     | 
    
         
            +
                        self.sio_connected = False
         
     | 
| 
      
 216 
     | 
    
         
            +
                        return None
         
     | 
| 
       269 
217 
     | 
    
         | 
| 
       270 
     | 
    
         
            -
                def  
     | 
| 
       271 
     | 
    
         
            -
                    """ 
     | 
| 
       272 
     | 
    
         
            -
                    
         
     | 
| 
       273 
     | 
    
         
            -
                     
     | 
| 
       274 
     | 
    
         
            -
                     
     | 
| 
       275 
     | 
    
         
            -
                    -  
     | 
| 
       276 
     | 
    
         
            -
                    -  
     | 
| 
       277 
     | 
    
         
            -
                    -  
     | 
| 
       278 
     | 
    
         
            -
                    
         
     | 
| 
       279 
     | 
    
         
            -
                     
     | 
| 
       280 
     | 
    
         
            -
                        - Calls _continue() to let prompt pass through
         
     | 
| 
       281 
     | 
    
         
            -
                        - Exits with code 2 to block LLM processing (for /mpm commands)
         
     | 
| 
       282 
     | 
    
         
            -
                    
         
     | 
| 
       283 
     | 
    
         
            -
                    Command Processing:
         
     | 
| 
       284 
     | 
    
         
            -
                    The method checks if the prompt starts with '/mpm' and routes
         
     | 
| 
       285 
     | 
    
         
            -
                    to the appropriate command handler. This allows claude-mpm to
         
     | 
| 
       286 
     | 
    
         
            -
                    provide an in-IDE command interface.
         
     | 
| 
      
 218 
     | 
    
         
            +
                def handle(self):
         
     | 
| 
      
 219 
     | 
    
         
            +
                    """Process hook event with minimal overhead and zero blocking delays.
         
     | 
| 
      
 220 
     | 
    
         
            +
                    
         
     | 
| 
      
 221 
     | 
    
         
            +
                    WHY this approach:
         
     | 
| 
      
 222 
     | 
    
         
            +
                    - Fast path processing for minimal latency (no blocking waits)
         
     | 
| 
      
 223 
     | 
    
         
            +
                    - Non-blocking Socket.IO connection and event emission
         
     | 
| 
      
 224 
     | 
    
         
            +
                    - Removed sleep() delays that were adding 100ms+ to every hook
         
     | 
| 
      
 225 
     | 
    
         
            +
                    - Connection timeout prevents indefinite hangs
         
     | 
| 
      
 226 
     | 
    
         
            +
                    - Graceful degradation if Socket.IO unavailable
         
     | 
| 
      
 227 
     | 
    
         
            +
                    - Always continues regardless of event status
         
     | 
| 
       287 
228 
     | 
    
         
             
                    """
         
     | 
| 
       288 
229 
     | 
    
         
             
                    try:
         
     | 
| 
       289 
     | 
    
         
            -
                         
     | 
| 
      
 230 
     | 
    
         
            +
                        # Read event
         
     | 
| 
      
 231 
     | 
    
         
            +
                        event_data = sys.stdin.read()
         
     | 
| 
      
 232 
     | 
    
         
            +
                        event = json.loads(event_data)
         
     | 
| 
      
 233 
     | 
    
         
            +
                        hook_type = event.get('hook_event_name', 'unknown')
         
     | 
| 
       290 
234 
     | 
    
         | 
| 
       291 
     | 
    
         
            -
                        #  
     | 
| 
       292 
     | 
    
         
            -
                         
     | 
| 
       293 
     | 
    
         
            -
                             
     | 
| 
      
 235 
     | 
    
         
            +
                        # Fast path for common events
         
     | 
| 
      
 236 
     | 
    
         
            +
                        if hook_type == 'UserPromptSubmit':
         
     | 
| 
      
 237 
     | 
    
         
            +
                            self._handle_user_prompt_fast(event)
         
     | 
| 
      
 238 
     | 
    
         
            +
                        elif hook_type == 'PreToolUse':
         
     | 
| 
      
 239 
     | 
    
         
            +
                            self._handle_pre_tool_fast(event)
         
     | 
| 
      
 240 
     | 
    
         
            +
                        elif hook_type == 'PostToolUse':
         
     | 
| 
      
 241 
     | 
    
         
            +
                            self._handle_post_tool_fast(event)
         
     | 
| 
      
 242 
     | 
    
         
            +
                        elif hook_type == 'Notification':
         
     | 
| 
      
 243 
     | 
    
         
            +
                            self._handle_notification_fast(event)
         
     | 
| 
      
 244 
     | 
    
         
            +
                        elif hook_type == 'Stop':
         
     | 
| 
      
 245 
     | 
    
         
            +
                            self._handle_stop_fast(event)
         
     | 
| 
      
 246 
     | 
    
         
            +
                        elif hook_type == 'SubagentStop':
         
     | 
| 
      
 247 
     | 
    
         
            +
                            self._handle_subagent_stop_fast(event)
         
     | 
| 
       294 
248 
     | 
    
         | 
| 
       295 
     | 
    
         
            -
                        #  
     | 
| 
       296 
     | 
    
         
            -
                         
     | 
| 
       297 
     | 
    
         
            -
             
     | 
| 
       298 
     | 
    
         
            -
             
     | 
| 
       299 
     | 
    
         
            -
             
     | 
| 
       300 
     | 
    
         
            -
             
     | 
| 
       301 
     | 
    
         
            -
             
     | 
| 
       302 
     | 
    
         
            -
             
     | 
| 
       303 
     | 
    
         
            -
             
     | 
| 
       304 
     | 
    
         
            -
                            # Route based on argument
         
     | 
| 
       305 
     | 
    
         
            -
                            if arg == 'status' or arg.startswith('status '):
         
     | 
| 
       306 
     | 
    
         
            -
                                # Extract status args if any
         
     | 
| 
       307 
     | 
    
         
            -
                                status_args = arg[6:].strip() if arg.startswith('status ') else ''
         
     | 
| 
       308 
     | 
    
         
            -
                                return self._handle_mpm_status(status_args)
         
     | 
| 
       309 
     | 
    
         
            -
                            elif arg == 'agents' or arg.startswith('agents '):
         
     | 
| 
       310 
     | 
    
         
            -
                                # Handle agents command
         
     | 
| 
       311 
     | 
    
         
            -
                                return self._handle_mpm_agents()
         
     | 
| 
       312 
     | 
    
         
            -
                            else:
         
     | 
| 
       313 
     | 
    
         
            -
                                # Show help for empty or unknown argument
         
     | 
| 
       314 
     | 
    
         
            -
                                return self._handle_mpm_help(arg)
         
     | 
| 
       315 
     | 
    
         
            -
                                
         
     | 
| 
       316 
     | 
    
         
            -
                    except Exception as e:
         
     | 
| 
       317 
     | 
    
         
            -
                        with open('/tmp/claude-mpm-hook.log', 'a') as f:
         
     | 
| 
       318 
     | 
    
         
            -
                            f.write(f"[{datetime.now().isoformat()}] Error in _handle_user_prompt_submit: {e}\n")
         
     | 
| 
       319 
     | 
    
         
            -
                            import traceback
         
     | 
| 
       320 
     | 
    
         
            -
                            f.write(traceback.format_exc())
         
     | 
| 
       321 
     | 
    
         
            -
                    
         
     | 
| 
       322 
     | 
    
         
            -
                    # For now, let everything else pass through
         
     | 
| 
       323 
     | 
    
         
            -
                    return self._continue()
         
     | 
| 
      
 249 
     | 
    
         
            +
                        # Socket.IO emit is non-blocking and will complete asynchronously
         
     | 
| 
      
 250 
     | 
    
         
            +
                        # Removed sleep() to eliminate 100ms delay that was blocking Claude execution
         
     | 
| 
      
 251 
     | 
    
         
            +
                        
         
     | 
| 
      
 252 
     | 
    
         
            +
                        # Always continue
         
     | 
| 
      
 253 
     | 
    
         
            +
                        print(json.dumps({"action": "continue"}))
         
     | 
| 
      
 254 
     | 
    
         
            +
                        
         
     | 
| 
      
 255 
     | 
    
         
            +
                    except:
         
     | 
| 
      
 256 
     | 
    
         
            +
                        # Fail fast and silent
         
     | 
| 
      
 257 
     | 
    
         
            +
                        print(json.dumps({"action": "continue"}))
         
     | 
| 
       324 
258 
     | 
    
         | 
| 
       325 
     | 
    
         
            -
                def  
     | 
| 
       326 
     | 
    
         
            -
                    """ 
     | 
| 
       327 
     | 
    
         
            -
                    
         
     | 
| 
       328 
     | 
    
         
            -
                     
     | 
| 
       329 
     | 
    
         
            -
                     
     | 
| 
       330 
     | 
    
         
            -
                    -  
     | 
| 
       331 
     | 
    
         
            -
                    -  
     | 
| 
       332 
     | 
    
         
            -
                    -  
     | 
| 
       333 
     | 
    
         
            -
                    - Validating bash commands for security violations
         
     | 
| 
       334 
     | 
    
         
            -
                    
         
     | 
| 
       335 
     | 
    
         
            -
                    Security Design:
         
     | 
| 
       336 
     | 
    
         
            -
                    - Fail-secure: Block suspicious operations
         
     | 
| 
       337 
     | 
    
         
            -
                    - Clear error messages to help users understand restrictions
         
     | 
| 
       338 
     | 
    
         
            -
                    - Log security events for auditing
         
     | 
| 
       339 
     | 
    
         
            -
                    
         
     | 
| 
       340 
     | 
    
         
            -
                    Returns:
         
     | 
| 
       341 
     | 
    
         
            -
                        - JSON response with action="continue" to allow the tool
         
     | 
| 
       342 
     | 
    
         
            -
                        - JSON response with action="block" and error message to prevent execution
         
     | 
| 
      
 259 
     | 
    
         
            +
                def _emit_socketio_event(self, namespace: str, event: str, data: dict):
         
     | 
| 
      
 260 
     | 
    
         
            +
                    """Emit Socket.IO event using direct client.
         
     | 
| 
      
 261 
     | 
    
         
            +
                    
         
     | 
| 
      
 262 
     | 
    
         
            +
                    WHY direct client approach:
         
     | 
| 
      
 263 
     | 
    
         
            +
                    - Simple synchronous emission
         
     | 
| 
      
 264 
     | 
    
         
            +
                    - No threading complexity
         
     | 
| 
      
 265 
     | 
    
         
            +
                    - Reliable delivery
         
     | 
| 
      
 266 
     | 
    
         
            +
                    - Fast when connection is reused
         
     | 
| 
       343 
267 
     | 
    
         
             
                    """
         
     | 
| 
       344 
     | 
    
         
            -
                     
     | 
| 
       345 
     | 
    
         
            -
                     
     | 
| 
       346 
     | 
    
         
            -
             
     | 
| 
       347 
     | 
    
         
            -
                    # Get the working directory from the event
         
     | 
| 
       348 
     | 
    
         
            -
                    working_dir = Path(self.event.get('cwd', os.getcwd())).resolve()
         
     | 
| 
      
 268 
     | 
    
         
            +
                    # Fast path: Skip all Socket.IO operations unless configured
         
     | 
| 
      
 269 
     | 
    
         
            +
                    if not os.environ.get('CLAUDE_MPM_SOCKETIO_PORT') and not DEBUG:
         
     | 
| 
      
 270 
     | 
    
         
            +
                        return
         
     | 
| 
       349 
271 
     | 
    
         | 
| 
       350 
     | 
    
         
            -
                    #  
     | 
| 
       351 
     | 
    
         
            -
                     
     | 
| 
       352 
     | 
    
         
            -
             
     | 
| 
       353 
     | 
    
         
            -
                         
     | 
| 
       354 
     | 
    
         
            -
                            #  
     | 
| 
       355 
     | 
    
         
            -
                             
     | 
| 
       356 
     | 
    
         
            -
             
     | 
| 
      
 272 
     | 
    
         
            +
                    # Get Socket.IO client
         
     | 
| 
      
 273 
     | 
    
         
            +
                    client = self._get_socketio_client()
         
     | 
| 
      
 274 
     | 
    
         
            +
                    if client:
         
     | 
| 
      
 275 
     | 
    
         
            +
                        try:
         
     | 
| 
      
 276 
     | 
    
         
            +
                            # Format event for Socket.IO server
         
     | 
| 
      
 277 
     | 
    
         
            +
                            claude_event_data = {
         
     | 
| 
      
 278 
     | 
    
         
            +
                                'type': f'hook.{event}',
         
     | 
| 
      
 279 
     | 
    
         
            +
                                'timestamp': datetime.now().isoformat(),
         
     | 
| 
      
 280 
     | 
    
         
            +
                                'data': data
         
     | 
| 
      
 281 
     | 
    
         
            +
                            }
         
     | 
| 
       357 
282 
     | 
    
         | 
| 
       358 
     | 
    
         
            -
                             
     | 
| 
       359 
     | 
    
         
            -
             
     | 
| 
       360 
     | 
    
         
            -
                                    logger.warning(f"Security: Blocked Bash command: {command[:100]}...")
         
     | 
| 
       361 
     | 
    
         
            -
                                
         
     | 
| 
       362 
     | 
    
         
            -
                                response = {
         
     | 
| 
       363 
     | 
    
         
            -
                                    "action": "block",
         
     | 
| 
       364 
     | 
    
         
            -
                                    "error": error_msg
         
     | 
| 
       365 
     | 
    
         
            -
                                }
         
     | 
| 
       366 
     | 
    
         
            -
                                print(json.dumps(response))
         
     | 
| 
       367 
     | 
    
         
            -
                                sys.exit(0)
         
     | 
| 
       368 
     | 
    
         
            -
                                return
         
     | 
| 
       369 
     | 
    
         
            -
                    
         
     | 
| 
       370 
     | 
    
         
            -
                    # List of tools that perform write operations
         
     | 
| 
       371 
     | 
    
         
            -
                    # These tools need special security checks to prevent
         
     | 
| 
       372 
     | 
    
         
            -
                    # writing outside the project directory
         
     | 
| 
       373 
     | 
    
         
            -
                    write_tools = ['Write', 'Edit', 'MultiEdit', 'NotebookEdit']
         
     | 
| 
       374 
     | 
    
         
            -
                    
         
     | 
| 
       375 
     | 
    
         
            -
                    # Check if this is a write operation
         
     | 
| 
       376 
     | 
    
         
            -
                    if tool_name in write_tools:
         
     | 
| 
       377 
     | 
    
         
            -
                        # Extract file path based on tool type
         
     | 
| 
       378 
     | 
    
         
            -
                        file_path = None
         
     | 
| 
       379 
     | 
    
         
            -
                        if tool_name in ['Write', 'Edit', 'NotebookEdit']:
         
     | 
| 
       380 
     | 
    
         
            -
                            file_path = tool_input.get('file_path')
         
     | 
| 
       381 
     | 
    
         
            -
                            if tool_name == 'NotebookEdit':
         
     | 
| 
       382 
     | 
    
         
            -
                                file_path = tool_input.get('notebook_path')
         
     | 
| 
       383 
     | 
    
         
            -
                        elif tool_name == 'MultiEdit':
         
     | 
| 
       384 
     | 
    
         
            -
                            file_path = tool_input.get('file_path')
         
     | 
| 
       385 
     | 
    
         
            -
                        
         
     | 
| 
       386 
     | 
    
         
            -
                        if file_path:
         
     | 
| 
       387 
     | 
    
         
            -
                            # First check for path traversal attempts before resolving
         
     | 
| 
       388 
     | 
    
         
            -
                            if '..' in str(file_path):
         
     | 
| 
       389 
     | 
    
         
            -
                                if logger:
         
     | 
| 
       390 
     | 
    
         
            -
                                    logger.warning(f"Security: Potential path traversal attempt in {tool_name}: {file_path}")
         
     | 
| 
       391 
     | 
    
         
            -
                                response = {
         
     | 
| 
       392 
     | 
    
         
            -
                                    "action": "block",
         
     | 
| 
       393 
     | 
    
         
            -
                                    "error": f"Security Policy: Path traversal attempts are not allowed.\n\n"
         
     | 
| 
       394 
     | 
    
         
            -
                                           f"The path '{file_path}' contains '..' which could be used to escape the working directory.\n"
         
     | 
| 
       395 
     | 
    
         
            -
                                           f"Please use absolute paths or paths relative to the working directory without '..'."
         
     | 
| 
       396 
     | 
    
         
            -
                                }
         
     | 
| 
       397 
     | 
    
         
            -
                                print(json.dumps(response))
         
     | 
| 
       398 
     | 
    
         
            -
                                sys.exit(0)
         
     | 
| 
       399 
     | 
    
         
            -
                                return
         
     | 
| 
      
 283 
     | 
    
         
            +
                            # Emit synchronously
         
     | 
| 
      
 284 
     | 
    
         
            +
                            client.emit('claude_event', claude_event_data)
         
     | 
| 
       400 
285 
     | 
    
         | 
| 
       401 
     | 
    
         
            -
                             
     | 
| 
       402 
     | 
    
         
            -
                                 
     | 
| 
       403 
     | 
    
         
            -
                                target_path = Path(file_path).resolve()
         
     | 
| 
       404 
     | 
    
         
            -
                                
         
     | 
| 
       405 
     | 
    
         
            -
                                # Check if the target path is within the working directory
         
     | 
| 
       406 
     | 
    
         
            -
                                try:
         
     | 
| 
       407 
     | 
    
         
            -
                                    target_path.relative_to(working_dir)
         
     | 
| 
       408 
     | 
    
         
            -
                                except ValueError:
         
     | 
| 
       409 
     | 
    
         
            -
                                    # Path is outside working directory
         
     | 
| 
       410 
     | 
    
         
            -
                                    if logger:
         
     | 
| 
       411 
     | 
    
         
            -
                                        logger.warning(f"Security: Blocked {tool_name} operation outside working directory: {file_path}")
         
     | 
| 
       412 
     | 
    
         
            -
                                    
         
     | 
| 
       413 
     | 
    
         
            -
                                    # Return block action with helpful error message
         
     | 
| 
       414 
     | 
    
         
            -
                                    response = {
         
     | 
| 
       415 
     | 
    
         
            -
                                        "action": "block",
         
     | 
| 
       416 
     | 
    
         
            -
                                        "error": f"Security Policy: Cannot write to files outside the working directory.\n\n"
         
     | 
| 
       417 
     | 
    
         
            -
                                               f"Working directory: {working_dir}\n"
         
     | 
| 
       418 
     | 
    
         
            -
                                               f"Attempted path: {file_path}\n\n"
         
     | 
| 
       419 
     | 
    
         
            -
                                               f"Please ensure all file operations are within the project directory."
         
     | 
| 
       420 
     | 
    
         
            -
                                    }
         
     | 
| 
       421 
     | 
    
         
            -
                                    print(json.dumps(response))
         
     | 
| 
       422 
     | 
    
         
            -
                                    sys.exit(0)
         
     | 
| 
       423 
     | 
    
         
            -
                                    return
         
     | 
| 
      
 286 
     | 
    
         
            +
                            if DEBUG:
         
     | 
| 
      
 287 
     | 
    
         
            +
                                print(f"Emitted Socket.IO event: hook.{event}", file=sys.stderr)
         
     | 
| 
       424 
288 
     | 
    
         | 
| 
       425 
     | 
    
         
            -
             
     | 
| 
       426 
     | 
    
         
            -
                             
     | 
| 
       427 
     | 
    
         
            -
                                 
     | 
| 
       428 
     | 
    
         
            -
             
     | 
| 
       429 
     | 
    
         
            -
             
     | 
| 
       430 
     | 
    
         
            -
             
     | 
| 
       431 
     | 
    
         
            -
             
     | 
| 
       432 
     | 
    
         
            -
             
     | 
| 
       433 
     | 
    
         
            -
             
     | 
| 
       434 
     | 
    
         
            -
             
     | 
| 
       435 
     | 
    
         
            -
             
     | 
| 
       436 
     | 
    
         
            -
             
     | 
| 
       437 
     | 
    
         
            -
             
     | 
| 
       438 
     | 
    
         
            -
             
     | 
| 
       439 
     | 
    
         
            -
             
     | 
| 
       440 
     | 
    
         
            -
             
     | 
| 
       441 
     | 
    
         
            -
                    
         
     | 
| 
       442 
     | 
    
         
            -
                    if tool_name in read_tools:
         
     | 
| 
       443 
     | 
    
         
            -
                        # For read operations, we allow them but log for auditing
         
     | 
| 
       444 
     | 
    
         
            -
                        file_path = None
         
     | 
| 
       445 
     | 
    
         
            -
                        if tool_name == 'Read':
         
     | 
| 
       446 
     | 
    
         
            -
                            file_path = tool_input.get('file_path')
         
     | 
| 
       447 
     | 
    
         
            -
                        elif tool_name == 'NotebookRead':
         
     | 
| 
       448 
     | 
    
         
            -
                            file_path = tool_input.get('notebook_path')
         
     | 
| 
       449 
     | 
    
         
            -
                        elif tool_name == 'LS':
         
     | 
| 
       450 
     | 
    
         
            -
                            file_path = tool_input.get('path')
         
     | 
| 
       451 
     | 
    
         
            -
                        elif tool_name in ['Glob', 'Grep']:
         
     | 
| 
       452 
     | 
    
         
            -
                            file_path = tool_input.get('path', '.')
         
     | 
| 
       453 
     | 
    
         
            -
                        
         
     | 
| 
       454 
     | 
    
         
            -
                        if file_path and logger:
         
     | 
| 
       455 
     | 
    
         
            -
                            # Log read operations for security auditing
         
     | 
| 
       456 
     | 
    
         
            -
                            logger.info(f"Security audit: {tool_name} accessing path: {file_path}")
         
     | 
| 
       457 
     | 
    
         
            -
                    
         
     | 
| 
       458 
     | 
    
         
            -
                    # Check for other potentially dangerous tools
         
     | 
| 
       459 
     | 
    
         
            -
                    if tool_name in ['WebFetch', 'WebSearch']:
         
     | 
| 
       460 
     | 
    
         
            -
                        # Log web access for security auditing
         
     | 
| 
       461 
     | 
    
         
            -
                        if logger:
         
     | 
| 
       462 
     | 
    
         
            -
                            url = tool_input.get('url', '') if tool_name == 'WebFetch' else 'N/A'
         
     | 
| 
       463 
     | 
    
         
            -
                            query = tool_input.get('query', '') if tool_name == 'WebSearch' else 'N/A'
         
     | 
| 
       464 
     | 
    
         
            -
                            logger.info(f"Security audit: {tool_name} - URL: {url}, Query: {query}")
         
     | 
| 
       465 
     | 
    
         
            -
                    
         
     | 
| 
       466 
     | 
    
         
            -
                    # For all other operations, continue normally
         
     | 
| 
       467 
     | 
    
         
            -
                    return self._continue()
         
     | 
| 
      
 289 
     | 
    
         
            +
                        except Exception as e:
         
     | 
| 
      
 290 
     | 
    
         
            +
                            if DEBUG:
         
     | 
| 
      
 291 
     | 
    
         
            +
                                print(f"Socket.IO emit failed: {e}", file=sys.stderr)
         
     | 
| 
      
 292 
     | 
    
         
            +
                            # Mark as disconnected so next call will reconnect
         
     | 
| 
      
 293 
     | 
    
         
            +
                            self.sio_connected = False
         
     | 
| 
      
 294 
     | 
    
         
            +
                    
         
     | 
| 
      
 295 
     | 
    
         
            +
                    # Fallback to legacy WebSocket server
         
     | 
| 
      
 296 
     | 
    
         
            +
                    elif hasattr(self, 'websocket_server') and self.websocket_server:
         
     | 
| 
      
 297 
     | 
    
         
            +
                        try:
         
     | 
| 
      
 298 
     | 
    
         
            +
                            # Map to legacy event format
         
     | 
| 
      
 299 
     | 
    
         
            +
                            legacy_event = f"hook.{event}"
         
     | 
| 
      
 300 
     | 
    
         
            +
                            self.websocket_server.broadcast_event(legacy_event, data)
         
     | 
| 
      
 301 
     | 
    
         
            +
                            if DEBUG:
         
     | 
| 
      
 302 
     | 
    
         
            +
                                print(f"Emitted legacy event: {legacy_event}", file=sys.stderr)
         
     | 
| 
      
 303 
     | 
    
         
            +
                        except:
         
     | 
| 
      
 304 
     | 
    
         
            +
                            pass  # Silent failure
         
     | 
| 
       468 
305 
     | 
    
         | 
| 
       469 
     | 
    
         
            -
                def  
     | 
| 
       470 
     | 
    
         
            -
                    """Handle  
     | 
| 
       471 
     | 
    
         
            -
                    
         
     | 
| 
       472 
     | 
    
         
            -
                    Called after a tool has been executed. Currently used for:
         
     | 
| 
       473 
     | 
    
         
            -
                    - Logging tool execution results
         
     | 
| 
       474 
     | 
    
         
            -
                    - Monitoring tool usage patterns
         
     | 
| 
       475 
     | 
    
         
            -
                    - Future: Could modify tool outputs or trigger follow-up actions
         
     | 
| 
      
 306 
     | 
    
         
            +
                def _handle_user_prompt_fast(self, event):
         
     | 
| 
      
 307 
     | 
    
         
            +
                    """Handle user prompt with comprehensive data capture.
         
     | 
| 
       476 
308 
     | 
    
         | 
| 
       477 
     | 
    
         
            -
                     
     | 
| 
      
 309 
     | 
    
         
            +
                    WHY enhanced data capture:
         
     | 
| 
      
 310 
     | 
    
         
            +
                    - Provides full context for debugging and monitoring
         
     | 
| 
      
 311 
     | 
    
         
            +
                    - Captures prompt text, working directory, and session context
         
     | 
| 
      
 312 
     | 
    
         
            +
                    - Enables better filtering and analysis in dashboard
         
     | 
| 
       478 
313 
     | 
    
         
             
                    """
         
     | 
| 
       479 
     | 
    
         
            -
                     
     | 
| 
       480 
     | 
    
         
            -
                    # Future enhancements could include:
         
     | 
| 
       481 
     | 
    
         
            -
                    # - Modifying tool outputs
         
     | 
| 
       482 
     | 
    
         
            -
                    # - Triggering notifications on certain conditions
         
     | 
| 
       483 
     | 
    
         
            -
                    # - Collecting metrics on tool usage
         
     | 
| 
       484 
     | 
    
         
            -
                    return self._continue()
         
     | 
| 
       485 
     | 
    
         
            -
                
         
     | 
| 
       486 
     | 
    
         
            -
                def _handle_stop(self):
         
     | 
| 
       487 
     | 
    
         
            -
                    """Handle Stop events.
         
     | 
| 
      
 314 
     | 
    
         
            +
                    prompt = event.get('prompt', '')
         
     | 
| 
       488 
315 
     | 
    
         | 
| 
       489 
     | 
    
         
            -
                     
     | 
| 
       490 
     | 
    
         
            -
                     
     | 
| 
       491 
     | 
    
         
            -
             
     | 
| 
       492 
     | 
    
         
            -
                    - Session statistics
         
     | 
| 
       493 
     | 
    
         
            -
                    
         
     | 
| 
       494 
     | 
    
         
            -
                    Currently just logs the event for monitoring purposes.
         
     | 
| 
       495 
     | 
    
         
            -
                    """
         
     | 
| 
       496 
     | 
    
         
            -
                    # Log the stop event and continue
         
     | 
| 
       497 
     | 
    
         
            -
                    # Future: Could trigger cleanup or summary generation
         
     | 
| 
       498 
     | 
    
         
            -
                    return self._continue()
         
     | 
| 
       499 
     | 
    
         
            -
                
         
     | 
| 
       500 
     | 
    
         
            -
                def _handle_subagent_stop(self):
         
     | 
| 
       501 
     | 
    
         
            -
                    """Handle SubagentStop events.
         
     | 
| 
      
 316 
     | 
    
         
            +
                    # Skip /mpm commands to reduce noise unless debug is enabled
         
     | 
| 
      
 317 
     | 
    
         
            +
                    if prompt.startswith('/mpm') and not DEBUG:
         
     | 
| 
      
 318 
     | 
    
         
            +
                        return
         
     | 
| 
       502 
319 
     | 
    
         | 
| 
       503 
     | 
    
         
            -
                     
     | 
| 
       504 
     | 
    
         
            -
                     
     | 
| 
       505 
     | 
    
         
            -
                     
     | 
| 
       506 
     | 
    
         
            -
                     
     | 
| 
      
 320 
     | 
    
         
            +
                    # Get working directory and git branch
         
     | 
| 
      
 321 
     | 
    
         
            +
                    working_dir = event.get('cwd', '')
         
     | 
| 
      
 322 
     | 
    
         
            +
                    git_branch = self._get_git_branch(working_dir) if working_dir else 'Unknown'
         
     | 
| 
      
 323 
     | 
    
         
            +
                    
         
     | 
| 
      
 324 
     | 
    
         
            +
                    # Extract comprehensive prompt data
         
     | 
| 
      
 325 
     | 
    
         
            +
                    prompt_data = {
         
     | 
| 
      
 326 
     | 
    
         
            +
                        'event_type': 'user_prompt',
         
     | 
| 
      
 327 
     | 
    
         
            +
                        'prompt_text': prompt,
         
     | 
| 
      
 328 
     | 
    
         
            +
                        'prompt_preview': prompt[:200] if len(prompt) > 200 else prompt,
         
     | 
| 
      
 329 
     | 
    
         
            +
                        'prompt_length': len(prompt),
         
     | 
| 
      
 330 
     | 
    
         
            +
                        'session_id': event.get('session_id', ''),
         
     | 
| 
      
 331 
     | 
    
         
            +
                        'working_directory': working_dir,
         
     | 
| 
      
 332 
     | 
    
         
            +
                        'git_branch': git_branch,
         
     | 
| 
      
 333 
     | 
    
         
            +
                        'timestamp': datetime.now().isoformat(),
         
     | 
| 
      
 334 
     | 
    
         
            +
                        'is_command': prompt.startswith('/'),
         
     | 
| 
      
 335 
     | 
    
         
            +
                        'contains_code': '```' in prompt or 'python' in prompt.lower() or 'javascript' in prompt.lower(),
         
     | 
| 
      
 336 
     | 
    
         
            +
                        'urgency': 'high' if any(word in prompt.lower() for word in ['urgent', 'error', 'bug', 'fix', 'broken']) else 'normal'
         
     | 
| 
      
 337 
     | 
    
         
            +
                    }
         
     | 
| 
       507 
338 
     | 
    
         | 
| 
       508 
     | 
    
         
            -
                     
     | 
| 
       509 
     | 
    
         
            -
                     
     | 
| 
       510 
     | 
    
         
            -
                    """
         
     | 
| 
       511 
     | 
    
         
            -
                    # Log the subagent stop event and continue
         
     | 
| 
       512 
     | 
    
         
            -
                    # Future: Could aggregate subagent performance metrics
         
     | 
| 
       513 
     | 
    
         
            -
                    return self._continue()
         
     | 
| 
      
 339 
     | 
    
         
            +
                    # Emit to /hook namespace
         
     | 
| 
      
 340 
     | 
    
         
            +
                    self._emit_socketio_event('/hook', 'user_prompt', prompt_data)
         
     | 
| 
       514 
341 
     | 
    
         | 
| 
       515 
     | 
    
         
            -
                def  
     | 
| 
       516 
     | 
    
         
            -
                    """Handle  
     | 
| 
       517 
     | 
    
         
            -
                    
         
     | 
| 
       518 
     | 
    
         
            -
                    Displays comprehensive status information about the claude-mpm system.
         
     | 
| 
       519 
     | 
    
         
            -
                    This helps users verify their installation and troubleshoot issues.
         
     | 
| 
       520 
     | 
    
         
            -
                    
         
     | 
| 
       521 
     | 
    
         
            -
                    Args:
         
     | 
| 
       522 
     | 
    
         
            -
                        args: Optional arguments like --verbose for detailed output
         
     | 
| 
      
 342 
     | 
    
         
            +
                def _handle_pre_tool_fast(self, event):
         
     | 
| 
      
 343 
     | 
    
         
            +
                    """Handle pre-tool use with comprehensive data capture.
         
     | 
| 
       523 
344 
     | 
    
         | 
| 
       524 
     | 
    
         
            -
                     
     | 
| 
       525 
     | 
    
         
            -
                    -  
     | 
| 
       526 
     | 
    
         
            -
                    -  
     | 
| 
       527 
     | 
    
         
            -
                    -  
     | 
| 
       528 
     | 
    
         
            -
                    - Hook system status
         
     | 
| 
       529 
     | 
    
         
            -
                    
         
     | 
| 
       530 
     | 
    
         
            -
                    Uses ANSI colors for better readability in the terminal.
         
     | 
| 
      
 345 
     | 
    
         
            +
                    WHY comprehensive capture:
         
     | 
| 
      
 346 
     | 
    
         
            +
                    - Captures tool parameters for debugging and security analysis
         
     | 
| 
      
 347 
     | 
    
         
            +
                    - Provides context about what Claude is about to do
         
     | 
| 
      
 348 
     | 
    
         
            +
                    - Enables pattern analysis and security monitoring
         
     | 
| 
       531 
349 
     | 
    
         
             
                    """
         
     | 
| 
       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 
     | 
    
         
            -
                        'environment': {
         
     | 
| 
       560 
     | 
    
         
            -
                            'CLAUDE_PROJECT_DIR': os.environ.get('CLAUDE_PROJECT_DIR', 'not set'),
         
     | 
| 
       561 
     | 
    
         
            -
                            'PYTHONPATH': os.environ.get('PYTHONPATH', 'not set'),
         
     | 
| 
       562 
     | 
    
         
            -
                        }
         
     | 
| 
      
 350 
     | 
    
         
            +
                    tool_name = event.get('tool_name', '')
         
     | 
| 
      
 351 
     | 
    
         
            +
                    tool_input = event.get('tool_input', {})
         
     | 
| 
      
 352 
     | 
    
         
            +
                    
         
     | 
| 
      
 353 
     | 
    
         
            +
                    # Extract key parameters based on tool type
         
     | 
| 
      
 354 
     | 
    
         
            +
                    tool_params = self._extract_tool_parameters(tool_name, tool_input)
         
     | 
| 
      
 355 
     | 
    
         
            +
                    
         
     | 
| 
      
 356 
     | 
    
         
            +
                    # Classify tool operation
         
     | 
| 
      
 357 
     | 
    
         
            +
                    operation_type = self._classify_tool_operation(tool_name, tool_input)
         
     | 
| 
      
 358 
     | 
    
         
            +
                    
         
     | 
| 
      
 359 
     | 
    
         
            +
                    # Get working directory and git branch
         
     | 
| 
      
 360 
     | 
    
         
            +
                    working_dir = event.get('cwd', '')
         
     | 
| 
      
 361 
     | 
    
         
            +
                    git_branch = self._get_git_branch(working_dir) if working_dir else 'Unknown'
         
     | 
| 
      
 362 
     | 
    
         
            +
                    
         
     | 
| 
      
 363 
     | 
    
         
            +
                    pre_tool_data = {
         
     | 
| 
      
 364 
     | 
    
         
            +
                        'event_type': 'pre_tool',
         
     | 
| 
      
 365 
     | 
    
         
            +
                        'tool_name': tool_name,
         
     | 
| 
      
 366 
     | 
    
         
            +
                        'operation_type': operation_type,
         
     | 
| 
      
 367 
     | 
    
         
            +
                        'tool_parameters': tool_params,
         
     | 
| 
      
 368 
     | 
    
         
            +
                        'session_id': event.get('session_id', ''),
         
     | 
| 
      
 369 
     | 
    
         
            +
                        'working_directory': working_dir,
         
     | 
| 
      
 370 
     | 
    
         
            +
                        'git_branch': git_branch,
         
     | 
| 
      
 371 
     | 
    
         
            +
                        'timestamp': datetime.now().isoformat(),
         
     | 
| 
      
 372 
     | 
    
         
            +
                        'parameter_count': len(tool_input) if isinstance(tool_input, dict) else 0,
         
     | 
| 
      
 373 
     | 
    
         
            +
                        'is_file_operation': tool_name in ['Write', 'Edit', 'MultiEdit', 'Read', 'LS', 'Glob'],
         
     | 
| 
      
 374 
     | 
    
         
            +
                        'is_execution': tool_name in ['Bash', 'NotebookEdit'],
         
     | 
| 
      
 375 
     | 
    
         
            +
                        'is_delegation': tool_name == 'Task',
         
     | 
| 
      
 376 
     | 
    
         
            +
                        'security_risk': self._assess_security_risk(tool_name, tool_input)
         
     | 
| 
       563 
377 
     | 
    
         
             
                    }
         
     | 
| 
       564 
378 
     | 
    
         | 
| 
       565 
     | 
    
         
            -
                    # Add  
     | 
| 
       566 
     | 
    
         
            -
                    if  
     | 
| 
       567 
     | 
    
         
            -
                         
     | 
| 
       568 
     | 
    
         
            -
             
     | 
| 
       569 
     | 
    
         
            -
                            ' 
     | 
| 
       570 
     | 
    
         
            -
                            ' 
     | 
| 
      
 379 
     | 
    
         
            +
                    # Add delegation-specific data if this is a Task tool
         
     | 
| 
      
 380 
     | 
    
         
            +
                    if tool_name == 'Task' and isinstance(tool_input, dict):
         
     | 
| 
      
 381 
     | 
    
         
            +
                        agent_type = tool_input.get('subagent_type', 'unknown')
         
     | 
| 
      
 382 
     | 
    
         
            +
                        pre_tool_data['delegation_details'] = {
         
     | 
| 
      
 383 
     | 
    
         
            +
                            'agent_type': agent_type,
         
     | 
| 
      
 384 
     | 
    
         
            +
                            'prompt': tool_input.get('prompt', ''),
         
     | 
| 
      
 385 
     | 
    
         
            +
                            'description': tool_input.get('description', ''),
         
     | 
| 
      
 386 
     | 
    
         
            +
                            'task_preview': (tool_input.get('prompt', '') or tool_input.get('description', ''))[:100]
         
     | 
| 
       571 
387 
     | 
    
         
             
                        }
         
     | 
| 
       572 
     | 
    
         
            -
                         
     | 
| 
       573 
     | 
    
         
            -
             
     | 
| 
       574 
     | 
    
         
            -
             
     | 
| 
       575 
     | 
    
         
            -
             
     | 
| 
      
 388 
     | 
    
         
            +
                        
         
     | 
| 
      
 389 
     | 
    
         
            +
                        # Track this delegation for SubagentStop correlation
         
     | 
| 
      
 390 
     | 
    
         
            +
                        session_id = event.get('session_id', '')
         
     | 
| 
      
 391 
     | 
    
         
            +
                        if session_id and agent_type != 'unknown':
         
     | 
| 
      
 392 
     | 
    
         
            +
                            self._track_delegation(session_id, agent_type)
         
     | 
| 
       576 
393 
     | 
    
         | 
| 
       577 
     | 
    
         
            -
                     
     | 
| 
       578 
     | 
    
         
            -
                    print(output, file=sys.stderr)
         
     | 
| 
       579 
     | 
    
         
            -
                    sys.exit(2)
         
     | 
| 
      
 394 
     | 
    
         
            +
                    self._emit_socketio_event('/hook', 'pre_tool', pre_tool_data)
         
     | 
| 
       580 
395 
     | 
    
         | 
| 
       581 
     | 
    
         
            -
                def  
     | 
| 
       582 
     | 
    
         
            -
                    """ 
     | 
| 
       583 
     | 
    
         
            -
                    try:
         
     | 
| 
       584 
     | 
    
         
            -
                        # First try to read from VERSION file in project root
         
     | 
| 
       585 
     | 
    
         
            -
                        version_file = project_root.parent / 'VERSION'
         
     | 
| 
       586 
     | 
    
         
            -
                        if not version_file.exists():
         
     | 
| 
       587 
     | 
    
         
            -
                            # Try one more level up
         
     | 
| 
       588 
     | 
    
         
            -
                            version_file = project_root.parent.parent / 'VERSION'
         
     | 
| 
       589 
     | 
    
         
            -
                        
         
     | 
| 
       590 
     | 
    
         
            -
                        if version_file.exists():
         
     | 
| 
       591 
     | 
    
         
            -
                            with open(version_file, 'r') as f:
         
     | 
| 
       592 
     | 
    
         
            -
                                version = f.read().strip()
         
     | 
| 
       593 
     | 
    
         
            -
                                # Return just the base version for cleaner display
         
     | 
| 
       594 
     | 
    
         
            -
                                # e.g., "1.0.2.dev1+g4ecadd4.d20250726" -> "1.0.2.dev1"
         
     | 
| 
       595 
     | 
    
         
            -
                                if '+' in version:
         
     | 
| 
       596 
     | 
    
         
            -
                                    version = version.split('+')[0]
         
     | 
| 
       597 
     | 
    
         
            -
                                return version
         
     | 
| 
       598 
     | 
    
         
            -
                    except Exception:
         
     | 
| 
       599 
     | 
    
         
            -
                        pass
         
     | 
| 
      
 396 
     | 
    
         
            +
                def _handle_post_tool_fast(self, event):
         
     | 
| 
      
 397 
     | 
    
         
            +
                    """Handle post-tool use with comprehensive data capture.
         
     | 
| 
       600 
398 
     | 
    
         | 
| 
       601 
     | 
    
         
            -
                     
     | 
| 
       602 
     | 
    
         
            -
             
     | 
| 
       603 
     | 
    
         
            -
             
     | 
| 
       604 
     | 
    
         
            -
             
     | 
| 
       605 
     | 
    
         
            -
                     
     | 
| 
       606 
     | 
    
         
            -
             
     | 
| 
      
 399 
     | 
    
         
            +
                    WHY comprehensive capture:
         
     | 
| 
      
 400 
     | 
    
         
            +
                    - Captures execution results and success/failure status
         
     | 
| 
      
 401 
     | 
    
         
            +
                    - Provides duration and performance metrics
         
     | 
| 
      
 402 
     | 
    
         
            +
                    - Enables pattern analysis of tool usage and success rates
         
     | 
| 
      
 403 
     | 
    
         
            +
                    """
         
     | 
| 
      
 404 
     | 
    
         
            +
                    tool_name = event.get('tool_name', '')
         
     | 
| 
      
 405 
     | 
    
         
            +
                    exit_code = event.get('exit_code', 0)
         
     | 
| 
      
 406 
     | 
    
         
            +
                    
         
     | 
| 
      
 407 
     | 
    
         
            +
                    # Extract result data
         
     | 
| 
      
 408 
     | 
    
         
            +
                    result_data = self._extract_tool_results(event)
         
     | 
| 
      
 409 
     | 
    
         
            +
                    
         
     | 
| 
      
 410 
     | 
    
         
            +
                    # Calculate duration if timestamps are available
         
     | 
| 
      
 411 
     | 
    
         
            +
                    duration = self._calculate_duration(event)
         
     | 
| 
      
 412 
     | 
    
         
            +
                    
         
     | 
| 
      
 413 
     | 
    
         
            +
                    # Get working directory and git branch
         
     | 
| 
      
 414 
     | 
    
         
            +
                    working_dir = event.get('cwd', '')
         
     | 
| 
      
 415 
     | 
    
         
            +
                    git_branch = self._get_git_branch(working_dir) if working_dir else 'Unknown'
         
     | 
| 
      
 416 
     | 
    
         
            +
                    
         
     | 
| 
      
 417 
     | 
    
         
            +
                    post_tool_data = {
         
     | 
| 
      
 418 
     | 
    
         
            +
                        'event_type': 'post_tool',
         
     | 
| 
      
 419 
     | 
    
         
            +
                        'tool_name': tool_name,
         
     | 
| 
      
 420 
     | 
    
         
            +
                        'exit_code': exit_code,
         
     | 
| 
      
 421 
     | 
    
         
            +
                        'success': exit_code == 0,
         
     | 
| 
      
 422 
     | 
    
         
            +
                        'status': 'success' if exit_code == 0 else 'blocked' if exit_code == 2 else 'error',
         
     | 
| 
      
 423 
     | 
    
         
            +
                        'duration_ms': duration,
         
     | 
| 
      
 424 
     | 
    
         
            +
                        'result_summary': result_data,
         
     | 
| 
      
 425 
     | 
    
         
            +
                        'session_id': event.get('session_id', ''),
         
     | 
| 
      
 426 
     | 
    
         
            +
                        'working_directory': working_dir,
         
     | 
| 
      
 427 
     | 
    
         
            +
                        'git_branch': git_branch,
         
     | 
| 
      
 428 
     | 
    
         
            +
                        'timestamp': datetime.now().isoformat(),
         
     | 
| 
      
 429 
     | 
    
         
            +
                        'has_output': bool(result_data.get('output')),
         
     | 
| 
      
 430 
     | 
    
         
            +
                        'has_error': bool(result_data.get('error')),
         
     | 
| 
      
 431 
     | 
    
         
            +
                        'output_size': len(str(result_data.get('output', ''))) if result_data.get('output') else 0
         
     | 
| 
      
 432 
     | 
    
         
            +
                    }
         
     | 
| 
       607 
433 
     | 
    
         | 
| 
       608 
     | 
    
         
            -
                     
     | 
| 
      
 434 
     | 
    
         
            +
                    self._emit_socketio_event('/hook', 'post_tool', post_tool_data)
         
     | 
| 
       609 
435 
     | 
    
         | 
| 
       610 
     | 
    
         
            -
                def  
     | 
| 
       611 
     | 
    
         
            -
                    """ 
     | 
| 
       612 
     | 
    
         
            -
                    # Use same colors as help screen
         
     | 
| 
       613 
     | 
    
         
            -
                    CYAN = '\033[96m'  # Bright cyan
         
     | 
| 
       614 
     | 
    
         
            -
                    GREEN = '\033[92m'  # Green (works in help)
         
     | 
| 
       615 
     | 
    
         
            -
                    BOLD = '\033[1m'
         
     | 
| 
       616 
     | 
    
         
            -
                    RESET = '\033[0m'
         
     | 
| 
       617 
     | 
    
         
            -
                    DIM = '\033[2m'
         
     | 
| 
       618 
     | 
    
         
            -
                    
         
     | 
| 
       619 
     | 
    
         
            -
                    output = f"\n{DIM}{'─' * 60}{RESET}\n"
         
     | 
| 
       620 
     | 
    
         
            -
                    output += f"{CYAN}{BOLD}🔧 Claude MPM Status{RESET}\n"
         
     | 
| 
       621 
     | 
    
         
            -
                    output += f"{DIM}{'─' * 60}{RESET}\n\n"
         
     | 
| 
       622 
     | 
    
         
            -
                    
         
     | 
| 
       623 
     | 
    
         
            -
                    output += f"{GREEN}Version:{RESET} {info['claude_mpm_version']}\n"
         
     | 
| 
       624 
     | 
    
         
            -
                    output += f"{GREEN}Python:{RESET} {info['python_version']}\n"
         
     | 
| 
       625 
     | 
    
         
            -
                    output += f"{GREEN}Project Root:{RESET} {info['project_root']}\n"  
         
     | 
| 
       626 
     | 
    
         
            -
                    output += f"{GREEN}Logging Level:{RESET} {info['logging_level']}\n"
         
     | 
| 
       627 
     | 
    
         
            -
                    output += f"{GREEN}Hook Handler:{RESET} {info['hook_handler']}\n"
         
     | 
| 
       628 
     | 
    
         
            -
                    
         
     | 
| 
       629 
     | 
    
         
            -
                    output += f"\n{CYAN}{BOLD}Environment:{RESET}\n"
         
     | 
| 
       630 
     | 
    
         
            -
                    for key, value in info['environment'].items():
         
     | 
| 
       631 
     | 
    
         
            -
                        output += f"{GREEN}  {key}: {value}{RESET}\n"
         
     | 
| 
       632 
     | 
    
         
            -
                    
         
     | 
| 
       633 
     | 
    
         
            -
                    if verbose:
         
     | 
| 
       634 
     | 
    
         
            -
                        output += f"\n{CYAN}{BOLD}Hooks Configured:{RESET}\n"
         
     | 
| 
       635 
     | 
    
         
            -
                        for hook, status in info.get('hooks_configured', {}).items():
         
     | 
| 
       636 
     | 
    
         
            -
                            output += f"{GREEN}  {hook}: {status}{RESET}\n"
         
     | 
| 
       637 
     | 
    
         
            -
                        
         
     | 
| 
       638 
     | 
    
         
            -
                        output += f"\n{CYAN}{BOLD}Available Arguments:{RESET}\n"
         
     | 
| 
       639 
     | 
    
         
            -
                        for arg in info.get('available_arguments', []):
         
     | 
| 
       640 
     | 
    
         
            -
                            output += f"{GREEN}  /mpm {arg}{RESET}\n"
         
     | 
| 
      
 436 
     | 
    
         
            +
                def _extract_tool_parameters(self, tool_name: str, tool_input: dict) -> dict:
         
     | 
| 
      
 437 
     | 
    
         
            +
                    """Extract relevant parameters based on tool type.
         
     | 
| 
       641 
438 
     | 
    
         | 
| 
       642 
     | 
    
         
            -
                     
     | 
| 
      
 439 
     | 
    
         
            +
                    WHY tool-specific extraction:
         
     | 
| 
      
 440 
     | 
    
         
            +
                    - Different tools have different important parameters
         
     | 
| 
      
 441 
     | 
    
         
            +
                    - Provides meaningful context for dashboard display
         
     | 
| 
      
 442 
     | 
    
         
            +
                    - Enables tool-specific analysis and monitoring
         
     | 
| 
      
 443 
     | 
    
         
            +
                    """
         
     | 
| 
      
 444 
     | 
    
         
            +
                    if not isinstance(tool_input, dict):
         
     | 
| 
      
 445 
     | 
    
         
            +
                        return {'raw_input': str(tool_input)}
         
     | 
| 
       643 
446 
     | 
    
         | 
| 
       644 
     | 
    
         
            -
                     
     | 
| 
       645 
     | 
    
         
            -
             
     | 
| 
       646 
     | 
    
         
            -
             
     | 
| 
       647 
     | 
    
         
            -
             
     | 
| 
      
 447 
     | 
    
         
            +
                    # Common parameters across all tools
         
     | 
| 
      
 448 
     | 
    
         
            +
                    params = {
         
     | 
| 
      
 449 
     | 
    
         
            +
                        'input_type': type(tool_input).__name__,
         
     | 
| 
      
 450 
     | 
    
         
            +
                        'param_keys': list(tool_input.keys()) if tool_input else []
         
     | 
| 
      
 451 
     | 
    
         
            +
                    }
         
     | 
| 
       648 
452 
     | 
    
         | 
| 
       649 
     | 
    
         
            -
                     
     | 
| 
       650 
     | 
    
         
            -
                     
     | 
| 
       651 
     | 
    
         
            -
             
     | 
| 
      
 453 
     | 
    
         
            +
                    # Tool-specific parameter extraction
         
     | 
| 
      
 454 
     | 
    
         
            +
                    if tool_name in ['Write', 'Edit', 'MultiEdit', 'Read', 'NotebookRead', 'NotebookEdit']:
         
     | 
| 
      
 455 
     | 
    
         
            +
                        params.update({
         
     | 
| 
      
 456 
     | 
    
         
            +
                            'file_path': tool_input.get('file_path') or tool_input.get('notebook_path'),
         
     | 
| 
      
 457 
     | 
    
         
            +
                            'content_length': len(str(tool_input.get('content', tool_input.get('new_string', '')))),
         
     | 
| 
      
 458 
     | 
    
         
            +
                            'is_create': tool_name == 'Write',
         
     | 
| 
      
 459 
     | 
    
         
            +
                            'is_edit': tool_name in ['Edit', 'MultiEdit', 'NotebookEdit']
         
     | 
| 
      
 460 
     | 
    
         
            +
                        })
         
     | 
| 
      
 461 
     | 
    
         
            +
                    elif tool_name == 'Bash':
         
     | 
| 
      
 462 
     | 
    
         
            +
                        command = tool_input.get('command', '')
         
     | 
| 
      
 463 
     | 
    
         
            +
                        params.update({
         
     | 
| 
      
 464 
     | 
    
         
            +
                            'command': command[:100],  # Truncate long commands
         
     | 
| 
      
 465 
     | 
    
         
            +
                            'command_length': len(command),
         
     | 
| 
      
 466 
     | 
    
         
            +
                            'has_pipe': '|' in command,
         
     | 
| 
      
 467 
     | 
    
         
            +
                            'has_redirect': '>' in command or '<' in command,
         
     | 
| 
      
 468 
     | 
    
         
            +
                            'timeout': tool_input.get('timeout')
         
     | 
| 
      
 469 
     | 
    
         
            +
                        })
         
     | 
| 
      
 470 
     | 
    
         
            +
                    elif tool_name in ['Grep', 'Glob']:
         
     | 
| 
      
 471 
     | 
    
         
            +
                        params.update({
         
     | 
| 
      
 472 
     | 
    
         
            +
                            'pattern': tool_input.get('pattern', ''),
         
     | 
| 
      
 473 
     | 
    
         
            +
                            'path': tool_input.get('path', ''),
         
     | 
| 
      
 474 
     | 
    
         
            +
                            'output_mode': tool_input.get('output_mode')
         
     | 
| 
      
 475 
     | 
    
         
            +
                        })
         
     | 
| 
      
 476 
     | 
    
         
            +
                    elif tool_name == 'WebFetch':
         
     | 
| 
      
 477 
     | 
    
         
            +
                        params.update({
         
     | 
| 
      
 478 
     | 
    
         
            +
                            'url': tool_input.get('url', ''),
         
     | 
| 
      
 479 
     | 
    
         
            +
                            'prompt': tool_input.get('prompt', '')[:50]  # Truncate prompt
         
     | 
| 
      
 480 
     | 
    
         
            +
                        })
         
     | 
| 
      
 481 
     | 
    
         
            +
                    elif tool_name == 'Task':
         
     | 
| 
      
 482 
     | 
    
         
            +
                        # Special handling for Task tool (agent delegations)
         
     | 
| 
      
 483 
     | 
    
         
            +
                        params.update({
         
     | 
| 
      
 484 
     | 
    
         
            +
                            'subagent_type': tool_input.get('subagent_type', 'unknown'),
         
     | 
| 
      
 485 
     | 
    
         
            +
                            'description': tool_input.get('description', ''),
         
     | 
| 
      
 486 
     | 
    
         
            +
                            'prompt': tool_input.get('prompt', ''),
         
     | 
| 
      
 487 
     | 
    
         
            +
                            'prompt_preview': tool_input.get('prompt', '')[:200] if tool_input.get('prompt') else '',
         
     | 
| 
      
 488 
     | 
    
         
            +
                            'is_pm_delegation': tool_input.get('subagent_type') == 'pm',
         
     | 
| 
      
 489 
     | 
    
         
            +
                            'is_research_delegation': tool_input.get('subagent_type') == 'research',
         
     | 
| 
      
 490 
     | 
    
         
            +
                            'is_engineer_delegation': tool_input.get('subagent_type') == 'engineer'
         
     | 
| 
      
 491 
     | 
    
         
            +
                        })
         
     | 
| 
      
 492 
     | 
    
         
            +
                    elif tool_name == 'TodoWrite':
         
     | 
| 
      
 493 
     | 
    
         
            +
                        # Special handling for TodoWrite tool (task management)
         
     | 
| 
      
 494 
     | 
    
         
            +
                        todos = tool_input.get('todos', [])
         
     | 
| 
      
 495 
     | 
    
         
            +
                        params.update({
         
     | 
| 
      
 496 
     | 
    
         
            +
                            'todo_count': len(todos),
         
     | 
| 
      
 497 
     | 
    
         
            +
                            'todos': todos,  # Full todo list
         
     | 
| 
      
 498 
     | 
    
         
            +
                            'todo_summary': self._summarize_todos(todos),
         
     | 
| 
      
 499 
     | 
    
         
            +
                            'has_in_progress': any(t.get('status') == 'in_progress' for t in todos),
         
     | 
| 
      
 500 
     | 
    
         
            +
                            'has_pending': any(t.get('status') == 'pending' for t in todos),
         
     | 
| 
      
 501 
     | 
    
         
            +
                            'has_completed': any(t.get('status') == 'completed' for t in todos),
         
     | 
| 
      
 502 
     | 
    
         
            +
                            'priorities': list(set(t.get('priority', 'medium') for t in todos))
         
     | 
| 
      
 503 
     | 
    
         
            +
                        })
         
     | 
| 
      
 504 
     | 
    
         
            +
                    
         
     | 
| 
      
 505 
     | 
    
         
            +
                    return params
         
     | 
| 
      
 506 
     | 
    
         
            +
                
         
     | 
| 
      
 507 
     | 
    
         
            +
                def _summarize_todos(self, todos: list) -> dict:
         
     | 
| 
      
 508 
     | 
    
         
            +
                    """Create a summary of the todo list for quick understanding."""
         
     | 
| 
      
 509 
     | 
    
         
            +
                    if not todos:
         
     | 
| 
      
 510 
     | 
    
         
            +
                        return {'total': 0, 'summary': 'Empty todo list'}
         
     | 
| 
       652 
511 
     | 
    
         | 
| 
       653 
     | 
    
         
            -
                     
     | 
| 
       654 
     | 
    
         
            -
                     
     | 
| 
       655 
     | 
    
         
            -
                    - Display agent versions in the same format as CLI startup
         
     | 
| 
       656 
     | 
    
         
            -
                    - Graceful error handling with helpful messages
         
     | 
| 
      
 512 
     | 
    
         
            +
                    status_counts = {'pending': 0, 'in_progress': 0, 'completed': 0}
         
     | 
| 
      
 513 
     | 
    
         
            +
                    priority_counts = {'high': 0, 'medium': 0, 'low': 0}
         
     | 
| 
       657 
514 
     | 
    
         | 
| 
       658 
     | 
    
         
            -
                     
     | 
| 
       659 
     | 
    
         
            -
             
     | 
| 
       660 
     | 
    
         
            -
             
     | 
| 
       661 
     | 
    
         
            -
                    try:
         
     | 
| 
       662 
     | 
    
         
            -
                        # Import the agent version display function
         
     | 
| 
       663 
     | 
    
         
            -
                        from claude_mpm.cli import _get_agent_versions_display
         
     | 
| 
      
 515 
     | 
    
         
            +
                    for todo in todos:
         
     | 
| 
      
 516 
     | 
    
         
            +
                        status = todo.get('status', 'pending')
         
     | 
| 
      
 517 
     | 
    
         
            +
                        priority = todo.get('priority', 'medium')
         
     | 
| 
       664 
518 
     | 
    
         | 
| 
       665 
     | 
    
         
            -
                         
     | 
| 
       666 
     | 
    
         
            -
             
     | 
| 
       667 
     | 
    
         
            -
                        
         
     | 
| 
       668 
     | 
    
         
            -
             
     | 
| 
       669 
     | 
    
         
            -
             
     | 
| 
       670 
     | 
    
         
            -
             
     | 
| 
      
 519 
     | 
    
         
            +
                        if status in status_counts:
         
     | 
| 
      
 520 
     | 
    
         
            +
                            status_counts[status] += 1
         
     | 
| 
      
 521 
     | 
    
         
            +
                        if priority in priority_counts:
         
     | 
| 
      
 522 
     | 
    
         
            +
                            priority_counts[priority] += 1
         
     | 
| 
      
 523 
     | 
    
         
            +
                    
         
     | 
| 
      
 524 
     | 
    
         
            +
                    # Create a text summary
         
     | 
| 
      
 525 
     | 
    
         
            +
                    summary_parts = []
         
     | 
| 
      
 526 
     | 
    
         
            +
                    if status_counts['completed'] > 0:
         
     | 
| 
      
 527 
     | 
    
         
            +
                        summary_parts.append(f"{status_counts['completed']} completed")
         
     | 
| 
      
 528 
     | 
    
         
            +
                    if status_counts['in_progress'] > 0:
         
     | 
| 
      
 529 
     | 
    
         
            +
                        summary_parts.append(f"{status_counts['in_progress']} in progress")
         
     | 
| 
      
 530 
     | 
    
         
            +
                    if status_counts['pending'] > 0:
         
     | 
| 
      
 531 
     | 
    
         
            +
                        summary_parts.append(f"{status_counts['pending']} pending")
         
     | 
| 
      
 532 
     | 
    
         
            +
                    
         
     | 
| 
      
 533 
     | 
    
         
            +
                    return {
         
     | 
| 
      
 534 
     | 
    
         
            +
                        'total': len(todos),
         
     | 
| 
      
 535 
     | 
    
         
            +
                        'status_counts': status_counts,
         
     | 
| 
      
 536 
     | 
    
         
            +
                        'priority_counts': priority_counts,
         
     | 
| 
      
 537 
     | 
    
         
            +
                        'summary': ', '.join(summary_parts) if summary_parts else 'No tasks'
         
     | 
| 
      
 538 
     | 
    
         
            +
                    }
         
     | 
| 
      
 539 
     | 
    
         
            +
                
         
     | 
| 
      
 540 
     | 
    
         
            +
                def _classify_tool_operation(self, tool_name: str, tool_input: dict) -> str:
         
     | 
| 
      
 541 
     | 
    
         
            +
                    """Classify the type of operation being performed."""
         
     | 
| 
      
 542 
     | 
    
         
            +
                    if tool_name in ['Read', 'LS', 'Glob', 'Grep', 'NotebookRead']:
         
     | 
| 
      
 543 
     | 
    
         
            +
                        return 'read'
         
     | 
| 
      
 544 
     | 
    
         
            +
                    elif tool_name in ['Write', 'Edit', 'MultiEdit', 'NotebookEdit']:
         
     | 
| 
      
 545 
     | 
    
         
            +
                        return 'write'
         
     | 
| 
      
 546 
     | 
    
         
            +
                    elif tool_name == 'Bash':
         
     | 
| 
      
 547 
     | 
    
         
            +
                        return 'execute'
         
     | 
| 
      
 548 
     | 
    
         
            +
                    elif tool_name in ['WebFetch', 'WebSearch']:
         
     | 
| 
      
 549 
     | 
    
         
            +
                        return 'network'
         
     | 
| 
      
 550 
     | 
    
         
            +
                    elif tool_name == 'TodoWrite':
         
     | 
| 
      
 551 
     | 
    
         
            +
                        return 'task_management'
         
     | 
| 
      
 552 
     | 
    
         
            +
                    elif tool_name == 'Task':
         
     | 
| 
      
 553 
     | 
    
         
            +
                        return 'delegation'
         
     | 
| 
      
 554 
     | 
    
         
            +
                    else:
         
     | 
| 
      
 555 
     | 
    
         
            +
                        return 'other'
         
     | 
| 
      
 556 
     | 
    
         
            +
                
         
     | 
| 
      
 557 
     | 
    
         
            +
                def _assess_security_risk(self, tool_name: str, tool_input: dict) -> str:
         
     | 
| 
      
 558 
     | 
    
         
            +
                    """Assess the security risk level of the tool operation."""
         
     | 
| 
      
 559 
     | 
    
         
            +
                    if tool_name == 'Bash':
         
     | 
| 
      
 560 
     | 
    
         
            +
                        command = tool_input.get('command', '').lower()
         
     | 
| 
      
 561 
     | 
    
         
            +
                        # Check for potentially dangerous commands
         
     | 
| 
      
 562 
     | 
    
         
            +
                        dangerous_patterns = ['rm -rf', 'sudo', 'chmod 777', 'curl', 'wget', '> /etc/', 'dd if=']
         
     | 
| 
      
 563 
     | 
    
         
            +
                        if any(pattern in command for pattern in dangerous_patterns):
         
     | 
| 
      
 564 
     | 
    
         
            +
                            return 'high'
         
     | 
| 
      
 565 
     | 
    
         
            +
                        elif any(word in command for word in ['install', 'delete', 'format', 'kill']):
         
     | 
| 
      
 566 
     | 
    
         
            +
                            return 'medium'
         
     | 
| 
       671 
567 
     | 
    
         
             
                        else:
         
     | 
| 
       672 
     | 
    
         
            -
                             
     | 
| 
       673 
     | 
    
         
            -
             
     | 
| 
       674 
     | 
    
         
            -
             
     | 
| 
       675 
     | 
    
         
            -
             
     | 
| 
       676 
     | 
    
         
            -
             
     | 
| 
       677 
     | 
    
         
            -
             
     | 
| 
       678 
     | 
    
         
            -
                         
     | 
| 
       679 
     | 
    
         
            -
             
     | 
| 
       680 
     | 
    
         
            -
                         
     | 
| 
       681 
     | 
    
         
            -
             
     | 
| 
       682 
     | 
    
         
            -
             
     | 
| 
       683 
     | 
    
         
            -
                         
     | 
| 
       684 
     | 
    
         
            -
                        if logger:
         
     | 
| 
       685 
     | 
    
         
            -
                            logger.error(f"Error in _handle_mpm_agents: {e}")
         
     | 
| 
       686 
     | 
    
         
            -
                    
         
     | 
| 
       687 
     | 
    
         
            -
                    # Block LLM processing since we've handled the command
         
     | 
| 
       688 
     | 
    
         
            -
                    sys.exit(2)
         
     | 
| 
      
 568 
     | 
    
         
            +
                            return 'low'
         
     | 
| 
      
 569 
     | 
    
         
            +
                    elif tool_name in ['Write', 'Edit', 'MultiEdit']:
         
     | 
| 
      
 570 
     | 
    
         
            +
                        file_path = tool_input.get('file_path', '')
         
     | 
| 
      
 571 
     | 
    
         
            +
                        # Check for system file modifications
         
     | 
| 
      
 572 
     | 
    
         
            +
                        if any(path in file_path for path in ['/etc/', '/usr/', '/var/', '/sys/']):
         
     | 
| 
      
 573 
     | 
    
         
            +
                            return 'high'
         
     | 
| 
      
 574 
     | 
    
         
            +
                        elif file_path.startswith('/'):
         
     | 
| 
      
 575 
     | 
    
         
            +
                            return 'medium'
         
     | 
| 
      
 576 
     | 
    
         
            +
                        else:
         
     | 
| 
      
 577 
     | 
    
         
            +
                            return 'low'
         
     | 
| 
      
 578 
     | 
    
         
            +
                    else:
         
     | 
| 
      
 579 
     | 
    
         
            +
                        return 'low'
         
     | 
| 
       689 
580 
     | 
    
         | 
| 
       690 
     | 
    
         
            -
                def  
     | 
| 
       691 
     | 
    
         
            -
                    """ 
     | 
| 
       692 
     | 
    
         
            -
                    
         
     | 
| 
       693 
     | 
    
         
            -
             
     | 
| 
       694 
     | 
    
         
            -
             
     | 
| 
       695 
     | 
    
         
            -
             
     | 
| 
       696 
     | 
    
         
            -
                     
     | 
| 
       697 
     | 
    
         
            -
                        unknown_arg: If provided, shows an error for unknown command
         
     | 
| 
      
 581 
     | 
    
         
            +
                def _extract_tool_results(self, event: dict) -> dict:
         
     | 
| 
      
 582 
     | 
    
         
            +
                    """Extract and summarize tool execution results."""
         
     | 
| 
      
 583 
     | 
    
         
            +
                    result = {
         
     | 
| 
      
 584 
     | 
    
         
            +
                        'exit_code': event.get('exit_code', 0),
         
     | 
| 
      
 585 
     | 
    
         
            +
                        'has_output': False,
         
     | 
| 
      
 586 
     | 
    
         
            +
                        'has_error': False
         
     | 
| 
      
 587 
     | 
    
         
            +
                    }
         
     | 
| 
       698 
588 
     | 
    
         | 
| 
       699 
     | 
    
         
            -
                     
     | 
| 
       700 
     | 
    
         
            -
                     
     | 
| 
       701 
     | 
    
         
            -
             
     | 
| 
       702 
     | 
    
         
            -
             
     | 
| 
       703 
     | 
    
         
            -
             
     | 
| 
      
 589 
     | 
    
         
            +
                    # Extract output if available
         
     | 
| 
      
 590 
     | 
    
         
            +
                    if 'output' in event:
         
     | 
| 
      
 591 
     | 
    
         
            +
                        output = str(event['output'])
         
     | 
| 
      
 592 
     | 
    
         
            +
                        result.update({
         
     | 
| 
      
 593 
     | 
    
         
            +
                            'has_output': bool(output.strip()),
         
     | 
| 
      
 594 
     | 
    
         
            +
                            'output_preview': output[:200] if len(output) > 200 else output,
         
     | 
| 
      
 595 
     | 
    
         
            +
                            'output_lines': len(output.split('\n')) if output else 0
         
     | 
| 
      
 596 
     | 
    
         
            +
                        })
         
     | 
| 
      
 597 
     | 
    
         
            +
                    
         
     | 
| 
      
 598 
     | 
    
         
            +
                    # Extract error information
         
     | 
| 
      
 599 
     | 
    
         
            +
                    if 'error' in event or event.get('exit_code', 0) != 0:
         
     | 
| 
      
 600 
     | 
    
         
            +
                        error = str(event.get('error', ''))
         
     | 
| 
      
 601 
     | 
    
         
            +
                        result.update({
         
     | 
| 
      
 602 
     | 
    
         
            +
                            'has_error': True,
         
     | 
| 
      
 603 
     | 
    
         
            +
                            'error_preview': error[:200] if len(error) > 200 else error
         
     | 
| 
      
 604 
     | 
    
         
            +
                        })
         
     | 
| 
      
 605 
     | 
    
         
            +
                    
         
     | 
| 
      
 606 
     | 
    
         
            +
                    return result
         
     | 
| 
      
 607 
     | 
    
         
            +
                
         
     | 
| 
      
 608 
     | 
    
         
            +
                def _calculate_duration(self, event: dict) -> int:
         
     | 
| 
      
 609 
     | 
    
         
            +
                    """Calculate operation duration in milliseconds if timestamps are available."""
         
     | 
| 
      
 610 
     | 
    
         
            +
                    # This would require start/end timestamps from Claude Code
         
     | 
| 
      
 611 
     | 
    
         
            +
                    # For now, return None as we don't have this data
         
     | 
| 
      
 612 
     | 
    
         
            +
                    return None
         
     | 
| 
      
 613 
     | 
    
         
            +
                
         
     | 
| 
      
 614 
     | 
    
         
            +
                def _handle_notification_fast(self, event):
         
     | 
| 
      
 615 
     | 
    
         
            +
                    """Handle notification events from Claude.
         
     | 
| 
      
 616 
     | 
    
         
            +
                    
         
     | 
| 
      
 617 
     | 
    
         
            +
                    WHY enhanced notification capture:
         
     | 
| 
      
 618 
     | 
    
         
            +
                    - Provides visibility into Claude's status and communication flow
         
     | 
| 
      
 619 
     | 
    
         
            +
                    - Captures notification type, content, and context for monitoring
         
     | 
| 
      
 620 
     | 
    
         
            +
                    - Enables pattern analysis of Claude's notification behavior
         
     | 
| 
      
 621 
     | 
    
         
            +
                    - Useful for debugging communication issues and user experience
         
     | 
| 
       704 
622 
     | 
    
         
             
                    """
         
     | 
| 
       705 
     | 
    
         
            -
                     
     | 
| 
       706 
     | 
    
         
            -
                     
     | 
| 
       707 
     | 
    
         
            -
                     
     | 
| 
       708 
     | 
    
         
            -
                     
     | 
| 
       709 
     | 
    
         
            -
                     
     | 
| 
       710 
     | 
    
         
            -
                     
     | 
| 
       711 
     | 
    
         
            -
                     
     | 
| 
       712 
     | 
    
         
            -
                    
         
     | 
| 
       713 
     | 
    
         
            -
             
     | 
| 
       714 
     | 
    
         
            -
             
     | 
| 
       715 
     | 
    
         
            -
             
     | 
| 
       716 
     | 
    
         
            -
             
     | 
| 
       717 
     | 
    
         
            -
             
     | 
| 
       718 
     | 
    
         
            -
                         
     | 
| 
       719 
     | 
    
         
            -
             
     | 
| 
       720 
     | 
    
         
            -
             
     | 
| 
       721 
     | 
    
         
            -
             
     | 
| 
       722 
     | 
    
         
            -
             
     | 
| 
       723 
     | 
    
         
            -
                         
     | 
| 
       724 
     | 
    
         
            -
             
     | 
| 
       725 
     | 
    
         
            -
                     
     | 
| 
       726 
     | 
    
         
            -
                     
     | 
| 
       727 
     | 
    
         
            -
                     
     | 
| 
       728 
     | 
    
         
            -
                     
     | 
| 
       729 
     | 
    
         
            -
                    output += f"  /mpm agents  - Show deployed agent versions\n"
         
     | 
| 
       730 
     | 
    
         
            -
                    
         
     | 
| 
       731 
     | 
    
         
            -
                    output += f"\n{DIM}{'─' * 60}{RESET}"
         
     | 
| 
       732 
     | 
    
         
            -
                    
         
     | 
| 
       733 
     | 
    
         
            -
                    # Block LLM processing and return our output
         
     | 
| 
       734 
     | 
    
         
            -
                    print(output, file=sys.stderr)
         
     | 
| 
       735 
     | 
    
         
            -
                    sys.exit(2)
         
     | 
| 
      
 623 
     | 
    
         
            +
                    notification_type = event.get('notification_type', 'unknown')
         
     | 
| 
      
 624 
     | 
    
         
            +
                    message = event.get('message', '')
         
     | 
| 
      
 625 
     | 
    
         
            +
                    
         
     | 
| 
      
 626 
     | 
    
         
            +
                    # Get working directory and git branch
         
     | 
| 
      
 627 
     | 
    
         
            +
                    working_dir = event.get('cwd', '')
         
     | 
| 
      
 628 
     | 
    
         
            +
                    git_branch = self._get_git_branch(working_dir) if working_dir else 'Unknown'
         
     | 
| 
      
 629 
     | 
    
         
            +
                    
         
     | 
| 
      
 630 
     | 
    
         
            +
                    notification_data = {
         
     | 
| 
      
 631 
     | 
    
         
            +
                        'event_type': 'notification',
         
     | 
| 
      
 632 
     | 
    
         
            +
                        'notification_type': notification_type,
         
     | 
| 
      
 633 
     | 
    
         
            +
                        'message': message,
         
     | 
| 
      
 634 
     | 
    
         
            +
                        'message_preview': message[:200] if len(message) > 200 else message,
         
     | 
| 
      
 635 
     | 
    
         
            +
                        'message_length': len(message),
         
     | 
| 
      
 636 
     | 
    
         
            +
                        'session_id': event.get('session_id', ''),
         
     | 
| 
      
 637 
     | 
    
         
            +
                        'working_directory': working_dir,
         
     | 
| 
      
 638 
     | 
    
         
            +
                        'git_branch': git_branch,
         
     | 
| 
      
 639 
     | 
    
         
            +
                        'timestamp': datetime.now().isoformat(),
         
     | 
| 
      
 640 
     | 
    
         
            +
                        'is_user_input_request': 'input' in message.lower() or 'waiting' in message.lower(),
         
     | 
| 
      
 641 
     | 
    
         
            +
                        'is_error_notification': 'error' in message.lower() or 'failed' in message.lower(),
         
     | 
| 
      
 642 
     | 
    
         
            +
                        'is_status_update': any(word in message.lower() for word in ['processing', 'analyzing', 'working', 'thinking'])
         
     | 
| 
      
 643 
     | 
    
         
            +
                    }
         
     | 
| 
      
 644 
     | 
    
         
            +
                    
         
     | 
| 
      
 645 
     | 
    
         
            +
                    # Emit to /hook namespace
         
     | 
| 
      
 646 
     | 
    
         
            +
                    self._emit_socketio_event('/hook', 'notification', notification_data)
         
     | 
| 
       736 
647 
     | 
    
         | 
| 
       737 
     | 
    
         
            -
                def  
     | 
| 
       738 
     | 
    
         
            -
                    """ 
     | 
| 
      
 648 
     | 
    
         
            +
                def _handle_stop_fast(self, event):
         
     | 
| 
      
 649 
     | 
    
         
            +
                    """Handle stop events when Claude processing stops.
         
     | 
| 
      
 650 
     | 
    
         
            +
                    
         
     | 
| 
      
 651 
     | 
    
         
            +
                    WHY comprehensive stop capture:
         
     | 
| 
      
 652 
     | 
    
         
            +
                    - Provides visibility into Claude's session lifecycle
         
     | 
| 
      
 653 
     | 
    
         
            +
                    - Captures stop reason and context for analysis
         
     | 
| 
      
 654 
     | 
    
         
            +
                    - Enables tracking of session completion patterns
         
     | 
| 
      
 655 
     | 
    
         
            +
                    - Useful for understanding when and why Claude stops responding
         
     | 
| 
      
 656 
     | 
    
         
            +
                    """
         
     | 
| 
      
 657 
     | 
    
         
            +
                    reason = event.get('reason', 'unknown')
         
     | 
| 
      
 658 
     | 
    
         
            +
                    stop_type = event.get('stop_type', 'normal')
         
     | 
| 
      
 659 
     | 
    
         
            +
                    
         
     | 
| 
      
 660 
     | 
    
         
            +
                    # Get working directory and git branch
         
     | 
| 
      
 661 
     | 
    
         
            +
                    working_dir = event.get('cwd', '')
         
     | 
| 
      
 662 
     | 
    
         
            +
                    git_branch = self._get_git_branch(working_dir) if working_dir else 'Unknown'
         
     | 
| 
      
 663 
     | 
    
         
            +
                    
         
     | 
| 
      
 664 
     | 
    
         
            +
                    stop_data = {
         
     | 
| 
      
 665 
     | 
    
         
            +
                        'event_type': 'stop',
         
     | 
| 
      
 666 
     | 
    
         
            +
                        'reason': reason,
         
     | 
| 
      
 667 
     | 
    
         
            +
                        'stop_type': stop_type,
         
     | 
| 
      
 668 
     | 
    
         
            +
                        'session_id': event.get('session_id', ''),
         
     | 
| 
      
 669 
     | 
    
         
            +
                        'working_directory': working_dir,
         
     | 
| 
      
 670 
     | 
    
         
            +
                        'git_branch': git_branch,
         
     | 
| 
      
 671 
     | 
    
         
            +
                        'timestamp': datetime.now().isoformat(),
         
     | 
| 
      
 672 
     | 
    
         
            +
                        'is_user_initiated': reason in ['user_stop', 'user_cancel', 'interrupt'],
         
     | 
| 
      
 673 
     | 
    
         
            +
                        'is_error_stop': reason in ['error', 'timeout', 'failed'],
         
     | 
| 
      
 674 
     | 
    
         
            +
                        'is_completion_stop': reason in ['completed', 'finished', 'done'],
         
     | 
| 
      
 675 
     | 
    
         
            +
                        'has_output': bool(event.get('final_output'))
         
     | 
| 
      
 676 
     | 
    
         
            +
                    }
         
     | 
| 
       739 
677 
     | 
    
         | 
| 
       740 
     | 
    
         
            -
                     
     | 
| 
       741 
     | 
    
         
            -
                     
     | 
| 
      
 678 
     | 
    
         
            +
                    # Emit to /hook namespace
         
     | 
| 
      
 679 
     | 
    
         
            +
                    self._emit_socketio_event('/hook', 'stop', stop_data)
         
     | 
| 
      
 680 
     | 
    
         
            +
                
         
     | 
| 
      
 681 
     | 
    
         
            +
                def _handle_subagent_stop_fast(self, event):
         
     | 
| 
      
 682 
     | 
    
         
            +
                    """Handle subagent stop events when subagent processing stops.
         
     | 
| 
      
 683 
     | 
    
         
            +
                    
         
     | 
| 
      
 684 
     | 
    
         
            +
                    WHY comprehensive subagent stop capture:
         
     | 
| 
      
 685 
     | 
    
         
            +
                    - Provides visibility into subagent lifecycle and delegation patterns
         
     | 
| 
      
 686 
     | 
    
         
            +
                    - Captures agent type, ID, reason, and results for analysis
         
     | 
| 
      
 687 
     | 
    
         
            +
                    - Enables tracking of delegation success/failure patterns
         
     | 
| 
      
 688 
     | 
    
         
            +
                    - Useful for understanding subagent performance and reliability
         
     | 
| 
      
 689 
     | 
    
         
            +
                    """
         
     | 
| 
      
 690 
     | 
    
         
            +
                    # First try to get agent type from our tracking
         
     | 
| 
      
 691 
     | 
    
         
            +
                    session_id = event.get('session_id', '')
         
     | 
| 
      
 692 
     | 
    
         
            +
                    agent_type = self._get_delegation_agent_type(session_id) if session_id else 'unknown'
         
     | 
| 
      
 693 
     | 
    
         
            +
                    
         
     | 
| 
      
 694 
     | 
    
         
            +
                    # Fall back to event data if tracking didn't have it
         
     | 
| 
      
 695 
     | 
    
         
            +
                    if agent_type == 'unknown':
         
     | 
| 
      
 696 
     | 
    
         
            +
                        agent_type = event.get('agent_type', event.get('subagent_type', 'unknown'))
         
     | 
| 
      
 697 
     | 
    
         
            +
                    
         
     | 
| 
      
 698 
     | 
    
         
            +
                    agent_id = event.get('agent_id', event.get('subagent_id', ''))
         
     | 
| 
      
 699 
     | 
    
         
            +
                    reason = event.get('reason', event.get('stop_reason', 'unknown'))
         
     | 
| 
      
 700 
     | 
    
         
            +
                    
         
     | 
| 
      
 701 
     | 
    
         
            +
                    # Try to infer agent type from other fields if still unknown
         
     | 
| 
      
 702 
     | 
    
         
            +
                    if agent_type == 'unknown' and 'task' in event:
         
     | 
| 
      
 703 
     | 
    
         
            +
                        task_desc = str(event.get('task', '')).lower()
         
     | 
| 
      
 704 
     | 
    
         
            +
                        if 'research' in task_desc:
         
     | 
| 
      
 705 
     | 
    
         
            +
                            agent_type = 'research'
         
     | 
| 
      
 706 
     | 
    
         
            +
                        elif 'engineer' in task_desc or 'code' in task_desc:
         
     | 
| 
      
 707 
     | 
    
         
            +
                            agent_type = 'engineer'
         
     | 
| 
      
 708 
     | 
    
         
            +
                        elif 'pm' in task_desc or 'project' in task_desc:
         
     | 
| 
      
 709 
     | 
    
         
            +
                            agent_type = 'pm'
         
     | 
| 
      
 710 
     | 
    
         
            +
                    
         
     | 
| 
      
 711 
     | 
    
         
            +
                    # Get working directory and git branch
         
     | 
| 
      
 712 
     | 
    
         
            +
                    working_dir = event.get('cwd', '')
         
     | 
| 
      
 713 
     | 
    
         
            +
                    git_branch = self._get_git_branch(working_dir) if working_dir else 'Unknown'
         
     | 
| 
      
 714 
     | 
    
         
            +
                    
         
     | 
| 
      
 715 
     | 
    
         
            +
                    subagent_stop_data = {
         
     | 
| 
      
 716 
     | 
    
         
            +
                        'event_type': 'subagent_stop',
         
     | 
| 
      
 717 
     | 
    
         
            +
                        'agent_type': agent_type,
         
     | 
| 
      
 718 
     | 
    
         
            +
                        'agent_id': agent_id,
         
     | 
| 
      
 719 
     | 
    
         
            +
                        'reason': reason,
         
     | 
| 
      
 720 
     | 
    
         
            +
                        'session_id': event.get('session_id', ''),
         
     | 
| 
      
 721 
     | 
    
         
            +
                        'working_directory': working_dir,
         
     | 
| 
      
 722 
     | 
    
         
            +
                        'git_branch': git_branch,
         
     | 
| 
      
 723 
     | 
    
         
            +
                        'timestamp': datetime.now().isoformat(),
         
     | 
| 
      
 724 
     | 
    
         
            +
                        'is_successful_completion': reason in ['completed', 'finished', 'done'],
         
     | 
| 
      
 725 
     | 
    
         
            +
                        'is_error_termination': reason in ['error', 'timeout', 'failed', 'blocked'],
         
     | 
| 
      
 726 
     | 
    
         
            +
                        'is_delegation_related': agent_type in ['research', 'engineer', 'pm', 'ops'],
         
     | 
| 
      
 727 
     | 
    
         
            +
                        'has_results': bool(event.get('results') or event.get('output')),
         
     | 
| 
      
 728 
     | 
    
         
            +
                        'duration_context': event.get('duration_ms')
         
     | 
| 
      
 729 
     | 
    
         
            +
                    }
         
     | 
| 
       742 
730 
     | 
    
         | 
| 
       743 
     | 
    
         
            -
                     
     | 
| 
       744 
     | 
    
         
            -
                     
     | 
| 
       745 
     | 
    
         
            -
             
     | 
| 
      
 731 
     | 
    
         
            +
                    # Debug log the raw event data
         
     | 
| 
      
 732 
     | 
    
         
            +
                    if DEBUG:
         
     | 
| 
      
 733 
     | 
    
         
            +
                        print(f"SubagentStop raw event data: {json.dumps(event, indent=2)}", file=sys.stderr)
         
     | 
| 
       746 
734 
     | 
    
         | 
| 
       747 
     | 
    
         
            -
                     
     | 
| 
       748 
     | 
    
         
            -
                     
     | 
| 
       749 
     | 
    
         
            -
             
     | 
| 
       750 
     | 
    
         
            -
             
     | 
| 
       751 
     | 
    
         
            -
                     
     | 
| 
      
 735 
     | 
    
         
            +
                    # Emit to /hook namespace
         
     | 
| 
      
 736 
     | 
    
         
            +
                    self._emit_socketio_event('/hook', 'subagent_stop', subagent_stop_data)
         
     | 
| 
      
 737 
     | 
    
         
            +
                
         
     | 
| 
      
 738 
     | 
    
         
            +
                def __del__(self):
         
     | 
| 
      
 739 
     | 
    
         
            +
                    """Cleanup Socket.IO client on handler destruction."""
         
     | 
| 
      
 740 
     | 
    
         
            +
                    if self.sio_client and self.sio_connected:
         
     | 
| 
      
 741 
     | 
    
         
            +
                        try:
         
     | 
| 
      
 742 
     | 
    
         
            +
                            self.sio_client.disconnect()
         
     | 
| 
      
 743 
     | 
    
         
            +
                        except:
         
     | 
| 
      
 744 
     | 
    
         
            +
                            pass
         
     | 
| 
       752 
745 
     | 
    
         | 
| 
       753 
746 
     | 
    
         | 
| 
       754 
747 
     | 
    
         
             
            def main():
         
     | 
| 
       755 
     | 
    
         
            -
                """ 
     | 
| 
       756 
     | 
    
         
            -
                
         
     | 
| 
       757 
     | 
    
         
            -
                Creates a new handler instance and processes the current event.
         
     | 
| 
       758 
     | 
    
         
            -
                Each hook invocation is independent - no state is maintained
         
     | 
| 
       759 
     | 
    
         
            -
                between calls. This ensures reliability and prevents memory leaks.
         
     | 
| 
       760 
     | 
    
         
            -
                """
         
     | 
| 
      
 748 
     | 
    
         
            +
                """Entry point."""
         
     | 
| 
       761 
749 
     | 
    
         
             
                handler = ClaudeHookHandler()
         
     | 
| 
       762 
750 
     | 
    
         
             
                handler.handle()
         
     | 
| 
       763 
751 
     | 
    
         |