claude-mpm 4.5.5__py3-none-any.whl → 4.5.8__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/VERSION CHANGED
@@ -1 +1 @@
1
- 4.5.5
1
+ 4.5.8
@@ -4,6 +4,16 @@ All Ops agents inherit these common operational patterns and requirements.
4
4
 
5
5
  ## Core Ops Principles
6
6
 
7
+ ### Local Development Server Management
8
+ **CRITICAL IMPERATIVES FOR LOCAL-OPS AGENTS:**
9
+ - **MAINTAIN SINGLE STABLE INSTANCES**: Always strive to keep a single instance of each development server running stably. Avoid creating multiple instances of the same service.
10
+ - **NEVER INTERRUPT OTHER PROJECTS**: Before stopping ANY service, verify it's not being used by another project or Claude Code session. Check process ownership and working directories.
11
+ - **PROTECT CLAUDE CODE SERVICES**: Never terminate or interfere with Claude MPM services, monitor servers, or any processes that might be used by Claude Code.
12
+ - **PORT MANAGEMENT**: Always check if a port is in use before attempting to use it. If occupied, find an alternative rather than killing the existing process.
13
+ - **GRACEFUL OPERATIONS**: Use graceful shutdown procedures. Always attempt soft stops before forceful termination.
14
+ - **SESSION AWARENESS**: Be aware that multiple Claude Code sessions might be active. Coordinate rather than conflict.
15
+ - **HEALTH BEFORE ACTION**: Always verify service health before making changes. A running service should be left running unless explicitly requested to stop it.
16
+
7
17
  ### Infrastructure as Code
8
18
  - All infrastructure must be version controlled
9
19
  - Use declarative configuration over imperative scripts
@@ -164,9 +164,31 @@ Read: /mpm-doctor # WRONG - not a file to read
164
164
  **User mentions error → PM delegates to Ops for logs (NEVER debugs)**
165
165
  **User wants analysis → PM delegates to Code Analyzer (NEVER analyzes)**
166
166
 
167
+ ### 🔥 LOCAL-OPS-AGENT PRIORITY RULE 🔥
168
+
169
+ **MANDATORY**: For ANY localhost/local development work, ALWAYS use **local-ops-agent** as the PRIMARY choice:
170
+ - **Local servers**: localhost:3000, dev servers → **local-ops-agent** (NOT generic Ops)
171
+ - **PM2 operations**: pm2 start/stop/status → **local-ops-agent** (EXPERT in PM2)
172
+ - **Port management**: Port conflicts, EADDRINUSE → **local-ops-agent** (HANDLES gracefully)
173
+ - **npm/yarn/pnpm**: npm start, yarn dev → **local-ops-agent** (PREFERRED)
174
+ - **Process management**: ps, kill, restart → **local-ops-agent** (SAFE operations)
175
+ - **Docker local**: docker-compose up → **local-ops-agent** (MANAGES containers)
176
+
177
+ **WHY local-ops-agent?**
178
+ - Maintains single stable instances (no duplicates)
179
+ - Never interrupts other projects or Claude Code
180
+ - Smart port allocation (finds alternatives, doesn't kill)
181
+ - Graceful operations (soft stops, proper cleanup)
182
+ - Session-aware (coordinates with multiple Claude sessions)
183
+
167
184
  ### Quick Delegation Matrix
168
185
  | User Says | PM's IMMEDIATE Response | You MUST Delegate To |
169
186
  |-----------|------------------------|---------------------|
187
+ | "localhost", "local server", "dev server" | "I'll delegate to local-ops agent" | **local-ops-agent** (PRIMARY) |
188
+ | "PM2", "process manager", "pm2 start" | "I'll have local-ops manage PM2" | **local-ops-agent** (ALWAYS) |
189
+ | "port 3000", "port conflict", "EADDRINUSE" | "I'll have local-ops handle ports" | **local-ops-agent** (EXPERT) |
190
+ | "npm start", "npm run dev", "yarn dev" | "I'll have local-ops run the dev server" | **local-ops-agent** (PREFERRED) |
191
+ | "start my app", "run locally" | "I'll delegate to local-ops agent" | **local-ops-agent** (DEFAULT) |
170
192
  | "fix", "implement", "code", "create" | "I'll delegate this to Engineer" | Engineer |
171
193
  | "test", "verify", "check" | "I'll have QA verify this" | QA (or web-qa/api-qa) |
172
194
  | "deploy", "host", "launch" | "I'll delegate to Ops" | Ops (or platform-specific) |
@@ -245,7 +267,8 @@ START → [DELEGATE Research] → [DELEGATE Code Analyzer] → [DELEGATE Impleme
245
267
 
246
268
  | Deployment Type | Ops Agent | Required Verifications |
247
269
  |----------------|-----------|------------------------|
248
- | Local Dev (PM2, Docker) | Ops | Read logs, check process status, fetch endpoint, Playwright if UI |
270
+ | Local Dev (PM2, Docker) | **local-ops-agent** (PRIMARY) | Read logs, check process status, fetch endpoint, Playwright if UI |
271
+ | Local npm/yarn/pnpm | **local-ops-agent** (ALWAYS) | Process monitoring, port management, graceful operations |
249
272
  | Vercel | vercel-ops-agent | Read build logs, fetch deployment URL, check function logs, Playwright for pages |
250
273
  | Railway | railway-ops-agent | Read deployment logs, check health endpoint, verify database connections |
251
274
  | GCP/Cloud Run | gcp-ops-agent | Check Cloud Run logs, verify service status, test endpoints |
@@ -274,9 +297,10 @@ Requirements:
274
297
  ## LOCAL DEPLOYMENT MANDATORY VERIFICATION
275
298
 
276
299
  **CRITICAL**: PM MUST NEVER claim "running on localhost" without verification.
300
+ **PRIMARY AGENT**: Always use **local-ops-agent** for ALL localhost work.
277
301
 
278
302
  ### Required for ALL Local Deployments (PM2, Docker, npm start, etc.):
279
- 1. PM MUST delegate to local-ops-agent (or Ops) for deployment
303
+ 1. PM MUST delegate to **local-ops-agent** (NEVER generic Ops) for deployment
280
304
  2. Ops agent MUST verify with ALL of these:
281
305
  - Process status check (ps, pm2 status, docker ps)
282
306
  - Log examination for startup errors
@@ -309,7 +333,7 @@ These phrases without fetch evidence = IMMEDIATE VIOLATION:
309
333
  |------|-------------|----------|----------------|
310
334
  | API | HTTP calls | curl/fetch output | web-qa (MANDATORY) |
311
335
  | Web UI | Browser automation | Playwright results | web-qa with Playwright |
312
- | Local Deploy | PM2/Docker status + fetch/Playwright | Logs + endpoint tests | Ops (MUST verify) |
336
+ | Local Deploy | PM2/Docker status + fetch/Playwright | Logs + endpoint tests | **local-ops-agent** (MUST verify) |
313
337
  | Vercel Deploy | Build success + fetch/Playwright | Deployment URL active | vercel-ops-agent (MUST verify) |
314
338
  | Railway Deploy | Service healthy + fetch tests | Logs + endpoint response | railway-ops-agent (MUST verify) |
315
339
  | GCP Deploy | Cloud Run active + endpoint tests | Service logs + HTTP 200 | gcp-ops-agent (MUST verify) |
@@ -597,7 +621,7 @@ Documentation → Report
597
621
  - Web UI: Research → Analyzer → web-ui/react-engineer → Ops (deploy) → Ops (VERIFY with Playwright) → web-qa → Docs
598
622
  - Vercel Site: Research → Analyzer → Engineer → vercel-ops (deploy) → vercel-ops (VERIFY) → web-qa → Docs
599
623
  - Railway App: Research → Analyzer → Engineer → railway-ops (deploy) → railway-ops (VERIFY) → api-qa → Docs
600
- - Local Dev: Research → Analyzer → Engineer → Ops (PM2/Docker) → Ops (VERIFY logs+fetch) → QA → Docs
624
+ - Local Dev: Research → Analyzer → Engineer → **local-ops-agent** (PM2/Docker) → **local-ops-agent** (VERIFY logs+fetch) → QA → Docs
601
625
  - Bug Fix: Research → Analyzer → Engineer → Deploy → Ops (VERIFY) → web-qa (regression) → version-control
602
626
 
603
627
  ### Success Criteria
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "local-ops",
3
3
  "display_name": "Local Operations Agent",
4
- "description": "Specialized agent for managing local development deployments with authority over PM2, Docker, and native processes",
5
- "version": "1.0.0",
4
+ "description": "Specialized agent for managing local development deployments with focus on maintaining single stable instances, protecting existing services, and never interfering with other projects or Claude Code services",
5
+ "version": "1.0.1",
6
6
  "author": "Claude MPM",
7
7
  "authority": {
8
8
  "level": "deployment_manager",
@@ -65,7 +65,16 @@
65
65
  "state_file": ".claude-mpm/deployment-state.json",
66
66
  "health_check_interval": 30,
67
67
  "auto_restart_attempts": 3,
68
- "cleanup_on_exit": false
68
+ "cleanup_on_exit": false,
69
+ "stability_policy": {
70
+ "single_instance_enforcement": true,
71
+ "reuse_existing_processes": true,
72
+ "protect_external_services": true,
73
+ "avoid_port_conflicts": true,
74
+ "graceful_shutdown_timeout": 10000,
75
+ "check_process_ownership": true,
76
+ "preserve_claude_mpm_services": true
77
+ }
69
78
  },
70
79
  "commands": {
71
80
  "deploy": {
@@ -78,9 +87,11 @@
78
87
  "workflow": [
79
88
  "detect_framework",
80
89
  "check_existing_deployments",
81
- "allocate_port",
90
+ "verify_no_conflicts",
91
+ "check_process_ownership",
92
+ "reuse_or_allocate_port",
82
93
  "build_if_needed",
83
- "start_process",
94
+ "start_or_attach_to_process",
84
95
  "monitor_health",
85
96
  "report_status"
86
97
  ]
@@ -213,7 +224,23 @@
213
224
  "error_recovery": {
214
225
  "port_conflict": {
215
226
  "detection": "EADDRINUSE",
216
- "action": "allocate_next_available_port"
227
+ "action": "check_process_owner_then_allocate_alternative_port",
228
+ "never": "kill_existing_process_without_verification"
229
+ },
230
+ "existing_service": {
231
+ "detection": "service_already_running",
232
+ "action": "attach_to_existing_or_report_status",
233
+ "never": "create_duplicate_instance"
234
+ },
235
+ "external_ownership": {
236
+ "detection": "process_owned_by_other_project",
237
+ "action": "allocate_different_resources",
238
+ "never": "interfere_with_external_process"
239
+ },
240
+ "claude_mpm_service": {
241
+ "detection": "claude-mpm|mcp|monitor",
242
+ "action": "report_status_only",
243
+ "never": "stop_or_restart_system_services"
217
244
  },
218
245
  "build_failure": {
219
246
  "detection": "npm ERR!|ERROR|Failed",
@@ -235,11 +262,20 @@
235
262
  "secrets_handling": "environment_variables"
236
263
  },
237
264
  "integration": {
265
+ "operational_principles": {
266
+ "single_instance_policy": "Always maintain single stable instances of services",
267
+ "non_interference": "Never interrupt services owned by other projects or Claude Code",
268
+ "service_protection": "Protect all Claude MPM, MCP, and monitor services",
269
+ "graceful_operations": "Always prefer graceful operations over forceful actions",
270
+ "conflict_avoidance": "Find alternative resources rather than stopping existing services"
271
+ },
238
272
  "hooks": {
239
- "pre_deploy": "validate_requirements",
273
+ "pre_deploy": "check_conflicts_and_validate_requirements",
240
274
  "post_deploy": "notify_status",
241
- "pre_stop": "graceful_shutdown",
242
- "on_crash": "auto_restart_with_backoff"
275
+ "pre_stop": "verify_ownership_then_graceful_shutdown",
276
+ "on_crash": "auto_restart_with_backoff",
277
+ "before_port_use": "check_existing_process_owner",
278
+ "on_conflict": "find_alternative_resources"
243
279
  },
244
280
  "monitoring": {
245
281
  "export_metrics": true,
@@ -31,6 +31,9 @@ from typing import Any, Dict, Optional
31
31
  from claude_mpm.core.constants import PerformanceConfig, SystemLimits, TimeoutConfig
32
32
  from claude_mpm.core.logging_utils import get_logger
33
33
 
34
+ # Import centralized session manager
35
+ from claude_mpm.services.session_manager import get_session_manager
36
+
34
37
  # Import configuration manager
35
38
  from ..core.config import Config
36
39
 
@@ -68,8 +71,13 @@ class AsyncSessionLogger:
68
71
  - Configurable log formats (JSON, syslog, journald)
69
72
  - Fire-and-forget pattern for zero latency impact
70
73
  - Graceful degradation on errors
74
+ - Thread-safe singleton pattern with initialization flag
71
75
  """
72
76
 
77
+ _initialization_lock = Lock()
78
+ _initialized = False
79
+ _worker_started = False
80
+
73
81
  def __init__(
74
82
  self,
75
83
  base_dir: Optional[Path] = None,
@@ -90,104 +98,108 @@ class AsyncSessionLogger:
90
98
  enable_compression: Enable gzip compression for JSON logs (overrides config)
91
99
  config: Configuration instance to use (creates new if not provided)
92
100
  """
93
- # Load configuration from YAML file or use provided config
94
- if config is None:
95
- config = Config()
96
- self.config = config
97
-
98
- # Get response logging configuration section
99
- response_config = self.config.get("response_logging", {})
100
-
101
- # Apply configuration with parameter overrides
102
- self.base_dir = Path(
103
- base_dir
104
- or response_config.get("session_directory", ".claude-mpm/responses")
105
- )
106
-
107
- # Convert log format string to enum
108
- format_str = response_config.get("format", "json").lower()
109
- if log_format is not None:
110
- self.log_format = log_format
111
- elif format_str == "syslog":
112
- self.log_format = LogFormat.SYSLOG
113
- elif format_str == "journald":
114
- self.log_format = LogFormat.JOURNALD
115
- else:
116
- self.log_format = LogFormat.JSON
101
+ # Use initialization flag to prevent duplicate setup
102
+ with self._initialization_lock:
103
+ if self._initialized and hasattr(self, "config"):
104
+ logger.debug("AsyncSessionLogger already initialized, skipping setup")
105
+ return
117
106
 
118
- self.max_queue_size = (
119
- max_queue_size
120
- if max_queue_size is not None
121
- else response_config.get("max_queue_size", SystemLimits.MAX_QUEUE_SIZE)
122
- )
107
+ # Load configuration from YAML file or use provided config
108
+ if config is None:
109
+ config = Config()
110
+ self.config = config
123
111
 
124
- # Handle async configuration with backward compatibility
125
- if enable_async is not None:
126
- self.enable_async = enable_async
127
- else:
128
- # Check configuration first, then environment variables for backward compatibility
129
- self.enable_async = response_config.get("use_async", True)
130
- # Override with environment variable if set (backward compatibility)
131
- if os.environ.get("CLAUDE_USE_ASYNC_LOG"):
132
- self.enable_async = (
133
- os.environ.get("CLAUDE_USE_ASYNC_LOG", "true").lower() == "true"
134
- )
112
+ # Get response logging configuration section
113
+ response_config = self.config.get("response_logging", {})
135
114
 
136
- # Check debug sync mode (forces synchronous for debugging)
137
- if (
138
- response_config.get("debug_sync", False)
139
- or os.environ.get("CLAUDE_LOG_SYNC", "").lower() == "true"
140
- ):
141
- logger.info("Debug sync mode enabled - forcing synchronous logging")
142
- self.enable_async = False
143
-
144
- self.enable_compression = (
145
- enable_compression
146
- if enable_compression is not None
147
- else response_config.get("enable_compression", False)
148
- )
115
+ # Apply configuration with parameter overrides
116
+ self.base_dir = Path(
117
+ base_dir
118
+ or response_config.get("session_directory", ".claude-mpm/responses")
119
+ )
149
120
 
150
- # Create base directory
151
- self.base_dir.mkdir(parents=True, exist_ok=True)
121
+ # Convert log format string to enum
122
+ format_str = response_config.get("format", "json").lower()
123
+ if log_format is not None:
124
+ self.log_format = log_format
125
+ elif format_str == "syslog":
126
+ self.log_format = LogFormat.SYSLOG
127
+ elif format_str == "journald":
128
+ self.log_format = LogFormat.JOURNALD
129
+ else:
130
+ self.log_format = LogFormat.JSON
152
131
 
153
- # Session management
154
- self.session_id = self._get_claude_session_id()
132
+ self.max_queue_size = (
133
+ max_queue_size
134
+ if max_queue_size is not None
135
+ else response_config.get("max_queue_size", SystemLimits.MAX_QUEUE_SIZE)
136
+ )
155
137
 
156
- # Async infrastructure
157
- self._queue: Queue = Queue(maxsize=self.max_queue_size)
158
- self._worker_thread: Optional[Thread] = None
159
- self._shutdown = False
160
- self._lock = Lock()
138
+ # Handle async configuration with backward compatibility
139
+ if enable_async is not None:
140
+ self.enable_async = enable_async
141
+ else:
142
+ # Check configuration first, then environment variables for backward compatibility
143
+ self.enable_async = response_config.get("use_async", True)
144
+ # Override with environment variable if set (backward compatibility)
145
+ if os.environ.get("CLAUDE_USE_ASYNC_LOG"):
146
+ self.enable_async = (
147
+ os.environ.get("CLAUDE_USE_ASYNC_LOG", "true").lower() == "true"
148
+ )
161
149
 
162
- # Statistics
163
- self.stats = {
164
- "logged": 0,
165
- "queued": 0,
166
- "dropped": 0,
167
- "errors": 0,
168
- "avg_write_time_ms": 0.0,
169
- }
150
+ # Check debug sync mode (forces synchronous for debugging)
151
+ if (
152
+ response_config.get("debug_sync", False)
153
+ or os.environ.get("CLAUDE_LOG_SYNC", "").lower() == "true"
154
+ ):
155
+ logger.info("Debug sync mode enabled - forcing synchronous logging")
156
+ self.enable_async = False
157
+
158
+ self.enable_compression = (
159
+ enable_compression
160
+ if enable_compression is not None
161
+ else response_config.get("enable_compression", False)
162
+ )
170
163
 
171
- # Initialize format-specific handlers
172
- self._init_format_handler()
164
+ # Create base directory
165
+ self.base_dir.mkdir(parents=True, exist_ok=True)
166
+
167
+ # Use centralized SessionManager for session ID
168
+ session_manager = get_session_manager()
169
+ self.session_id = session_manager.get_session_id()
170
+
171
+ # Async infrastructure
172
+ self._queue: Queue = Queue(maxsize=self.max_queue_size)
173
+ self._worker_thread: Optional[Thread] = None
174
+ self._shutdown = False
175
+ self._lock = Lock()
176
+
177
+ # Statistics
178
+ self.stats = {
179
+ "logged": 0,
180
+ "queued": 0,
181
+ "dropped": 0,
182
+ "errors": 0,
183
+ "avg_write_time_ms": 0.0,
184
+ }
185
+
186
+ # Initialize format-specific handlers
187
+ self._init_format_handler()
188
+
189
+ # Mark as initialized
190
+ self._initialized = True
191
+
192
+ # Log initialization status
193
+ logger.info(
194
+ f"AsyncSessionLogger initialized with SessionManager: session_id={self.session_id}, async={self.enable_async}, format={self.log_format.value}"
195
+ )
173
196
 
174
- # Start background worker if async enabled
175
- if self.enable_async:
176
- self._start_worker()
177
-
178
- def _get_claude_session_id(self) -> str:
179
- """Get or generate a Claude session ID."""
180
- # Check environment variables in order of preference
181
- for env_var in ["CLAUDE_SESSION_ID", "ANTHROPIC_SESSION_ID", "SESSION_ID"]:
182
- session_id = os.environ.get(env_var)
183
- if session_id:
184
- logger.info(f"Using session ID from {env_var}: {session_id}")
185
- return session_id
186
-
187
- # Generate timestamp-based session ID
188
- session_id = datetime.now(timezone.utc).strftime("%Y%m%d_%H%M%S")
189
- logger.info(f"Generated session ID: {session_id}")
190
- return session_id
197
+ # Start background worker if async enabled (outside initialization lock)
198
+ if self.enable_async and not self._worker_started:
199
+ with self._initialization_lock:
200
+ if not self._worker_started:
201
+ self._start_worker()
202
+ self._worker_started = True
191
203
 
192
204
  def _init_format_handler(self):
193
205
  """Initialize format-specific logging handlers."""
@@ -471,8 +483,19 @@ class AsyncSessionLogger:
471
483
  Args:
472
484
  timeout: Maximum time to wait for shutdown
473
485
  """
486
+ # Only log shutdown if we're actually shutting down an active logger
487
+ if self._shutdown:
488
+ logger.debug("AsyncSessionLogger already shut down")
489
+ return
490
+
474
491
  if self.enable_async:
475
- logger.info("Shutting down async logger")
492
+ # Only log at INFO level if we actually processed something
493
+ if self.stats.get("logged", 0) > 0 or self.stats.get("queued", 0) > 0:
494
+ logger.info(
495
+ f"Shutting down async logger (logged: {self.stats.get('logged', 0)}, queued: {self.stats.get('queued', 0)})"
496
+ )
497
+ else:
498
+ logger.debug("Shutting down async logger (no activity)")
476
499
 
477
500
  # Signal shutdown
478
501
  self._shutdown = True
@@ -486,10 +509,16 @@ class AsyncSessionLogger:
486
509
 
487
510
  # Log final statistics only if we actually logged something
488
511
  if self.stats.get("logged", 0) > 0:
489
- logger.info(f"Logger stats: {self.stats}")
512
+ logger.info(f"AsyncSessionLogger final stats: {self.stats}")
513
+ elif self.stats.get("queued", 0) > 0 or self.stats.get("dropped", 0) > 0:
514
+ logger.debug(
515
+ f"AsyncSessionLogger stats (incomplete session): {self.stats}"
516
+ )
490
517
  else:
491
518
  # Use debug level when nothing was logged
492
- logger.debug(f"Logger stats (no sessions logged): {self.stats}")
519
+ logger.debug(
520
+ f"AsyncSessionLogger stats (no sessions logged): {self.stats}"
521
+ )
493
522
 
494
523
  def get_stats(self) -> Dict[str, Any]:
495
524
  """Get logger statistics."""
@@ -497,8 +526,17 @@ class AsyncSessionLogger:
497
526
  return self.stats.copy()
498
527
 
499
528
  def set_session_id(self, session_id: str):
500
- """Set a new session ID."""
529
+ """Set a new session ID.
530
+
531
+ Note: This updates both the local session ID and the SessionManager.
532
+
533
+ Args:
534
+ session_id: The new session ID to use
535
+ """
501
536
  self.session_id = session_id
537
+ # Also update SessionManager to keep consistency
538
+ session_manager = get_session_manager()
539
+ session_manager.set_session_id(session_id)
502
540
  logger.info(f"Session ID updated to: {session_id}")
503
541
 
504
542
  def is_enabled(self) -> bool:
@@ -13,11 +13,15 @@ Configuration via .claude-mpm/configuration.yaml.
13
13
  import json
14
14
  import os
15
15
  from datetime import datetime, timezone
16
+ from threading import Lock
16
17
  from typing import Any, Dict, Optional
17
18
 
18
19
  # Import configuration manager
19
20
  from claude_mpm.core.config import Config
20
21
 
22
+ # Import centralized session manager
23
+ from claude_mpm.services.session_manager import get_session_manager
24
+
21
25
  # Try to import async logger for performance optimization
22
26
  try:
23
27
  from claude_mpm.services.async_session_logger import (
@@ -38,6 +42,9 @@ logger = get_logger(__name__)
38
42
  class ClaudeSessionLogger:
39
43
  """Simplified response logger for Claude Code sessions."""
40
44
 
45
+ _initialization_lock = Lock()
46
+ _initialized = False
47
+
41
48
  def __init__(
42
49
  self,
43
50
  base_dir: Optional[Path] = None,
@@ -52,29 +59,41 @@ class ClaudeSessionLogger:
52
59
  use_async: Use async logging if available. Overrides config.
53
60
  config: Configuration instance to use (creates new if not provided)
54
61
  """
55
- # Load configuration
56
- if config is None:
57
- config = Config()
58
- self.config = config
59
-
60
- # Get response logging configuration
61
- response_config = self.config.get("response_logging", {})
62
-
63
- # Determine base directory
64
- if base_dir is None:
65
- # Check configuration first
66
- base_dir = response_config.get("session_directory")
67
- if not base_dir:
68
- # Fall back to default response directory
69
- base_dir = ".claude-mpm/responses"
70
- base_dir = Path(base_dir)
71
-
72
- self.base_dir = Path(base_dir)
73
- self.base_dir.mkdir(parents=True, exist_ok=True)
62
+ # Use initialization flag to prevent duplicate setup
63
+ with self._initialization_lock:
64
+ if self._initialized and hasattr(self, "config"):
65
+ logger.debug("ClaudeSessionLogger already initialized, skipping setup")
66
+ return
67
+
68
+ # Load configuration
69
+ if config is None:
70
+ config = Config()
71
+ self.config = config
72
+
73
+ # Get response logging configuration
74
+ response_config = self.config.get("response_logging", {})
75
+
76
+ # Determine base directory
77
+ if base_dir is None:
78
+ # Check configuration first
79
+ base_dir = response_config.get("session_directory")
80
+ if not base_dir:
81
+ # Fall back to default response directory
82
+ base_dir = ".claude-mpm/responses"
83
+ base_dir = Path(base_dir)
84
+
85
+ self.base_dir = Path(base_dir)
86
+ self.base_dir.mkdir(parents=True, exist_ok=True)
87
+
88
+ # Use centralized SessionManager for session ID
89
+ session_manager = get_session_manager()
90
+ self.session_id = session_manager.get_session_id()
91
+ logger.info(
92
+ f"ClaudeSessionLogger using session ID from SessionManager: {self.session_id}"
93
+ )
74
94
 
75
- # Try to get session ID from environment
76
- self.session_id = self._get_claude_session_id()
77
- self.response_counter = {} # Track response count per session
95
+ self.response_counter = {} # Track response count per session
96
+ self._initialized = True
78
97
 
79
98
  # Determine if we should use async logging
80
99
  if use_async is None:
@@ -101,46 +120,22 @@ class ClaudeSessionLogger:
101
120
 
102
121
  if self.use_async:
103
122
  try:
123
+ # Pass our session_id to async logger to avoid duplicate generation
104
124
  self._async_logger = get_async_logger(config=config)
105
- logger.info("Using async logger for improved performance")
125
+ # Synchronize session IDs - use the one we already generated
126
+ if self.session_id and hasattr(self._async_logger, "set_session_id"):
127
+ self._async_logger.set_session_id(self.session_id)
128
+ logger.info(
129
+ f"Using async logger with session ID: {self.session_id}"
130
+ )
131
+ else:
132
+ logger.info("Using async logger for improved performance")
106
133
  except Exception as e:
107
134
  logger.warning(
108
135
  f"Failed to initialize async logger, falling back to sync: {e}"
109
136
  )
110
137
  self.use_async = False
111
138
 
112
- def _get_claude_session_id(self) -> Optional[str]:
113
- """
114
- Get the Claude Code session ID from environment.
115
-
116
- Returns:
117
- Session ID if available, None otherwise
118
- """
119
- # Claude Code may set various environment variables
120
- # Check common patterns
121
- session_id = None
122
-
123
- # Check for CLAUDE_SESSION_ID
124
- session_id = os.environ.get("CLAUDE_SESSION_ID")
125
-
126
- # Check for ANTHROPIC_SESSION_ID
127
- if not session_id:
128
- session_id = os.environ.get("ANTHROPIC_SESSION_ID")
129
-
130
- # Check for generic SESSION_ID
131
- if not session_id:
132
- session_id = os.environ.get("SESSION_ID")
133
-
134
- # Generate a default based on timestamp if nothing found
135
- if not session_id:
136
- # Use a timestamp-based session ID as fallback
137
- session_id = datetime.now(timezone.utc).strftime("%Y%m%d_%H%M%S")
138
- logger.info(f"Session ID: {session_id} (generated)")
139
- else:
140
- logger.info(f"Session ID: {session_id}")
141
-
142
- return session_id
143
-
144
139
  def _generate_filename(self, agent: Optional[str] = None) -> str:
145
140
  """
146
141
  Generate a flat filename with session ID, agent, and timestamp.
@@ -243,10 +238,15 @@ class ClaudeSessionLogger:
243
238
  """
244
239
  Manually set the session ID.
245
240
 
241
+ Note: This updates both the local session ID and the SessionManager.
242
+
246
243
  Args:
247
244
  session_id: The session ID to use
248
245
  """
249
246
  self.session_id = session_id
247
+ # Also update SessionManager to keep consistency
248
+ session_manager = get_session_manager()
249
+ session_manager.set_session_id(session_id)
250
250
  logger.info(f"Session ID set to: {session_id}")
251
251
 
252
252
  def get_session_path(self) -> Optional[Path]:
@@ -272,13 +272,16 @@ class ClaudeSessionLogger:
272
272
  return self.session_id is not None
273
273
 
274
274
 
275
- # Singleton instance for easy access
275
+ # Singleton instance with thread-safe initialization
276
276
  _logger_instance = None
277
+ _logger_lock = Lock()
277
278
 
278
279
 
279
280
  def get_session_logger(config: Optional[Config] = None) -> ClaudeSessionLogger:
280
281
  """
281
- Get the singleton session logger instance.
282
+ Get the singleton session logger instance with thread-safe initialization.
283
+
284
+ Uses double-checked locking pattern to ensure thread safety.
282
285
 
283
286
  Args:
284
287
  config: Optional configuration instance to use
@@ -287,9 +290,16 @@ def get_session_logger(config: Optional[Config] = None) -> ClaudeSessionLogger:
287
290
  The shared ClaudeSessionLogger instance
288
291
  """
289
292
  global _logger_instance
290
- if _logger_instance is None:
291
- _logger_instance = ClaudeSessionLogger(config=config)
292
- return _logger_instance
293
+
294
+ # Fast path - check without lock
295
+ if _logger_instance is not None:
296
+ return _logger_instance
297
+
298
+ # Slow path - acquire lock and double-check
299
+ with _logger_lock:
300
+ if _logger_instance is None:
301
+ _logger_instance = ClaudeSessionLogger(config=config)
302
+ return _logger_instance
293
303
 
294
304
 
295
305
  def log_response(