claude-mpm 0.3.0__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.

Potentially problematic release.


This version of claude-mpm might be problematic. Click here for more details.

Files changed (159) hide show
  1. claude_mpm/__init__.py +17 -0
  2. claude_mpm/__main__.py +14 -0
  3. claude_mpm/_version.py +32 -0
  4. claude_mpm/agents/BASE_AGENT_TEMPLATE.md +88 -0
  5. claude_mpm/agents/INSTRUCTIONS.md +375 -0
  6. claude_mpm/agents/__init__.py +118 -0
  7. claude_mpm/agents/agent_loader.py +621 -0
  8. claude_mpm/agents/agent_loader_integration.py +229 -0
  9. claude_mpm/agents/agents_metadata.py +204 -0
  10. claude_mpm/agents/base_agent.json +27 -0
  11. claude_mpm/agents/base_agent_loader.py +519 -0
  12. claude_mpm/agents/schema/agent_schema.json +160 -0
  13. claude_mpm/agents/system_agent_config.py +587 -0
  14. claude_mpm/agents/templates/__init__.py +101 -0
  15. claude_mpm/agents/templates/data_engineer_agent.json +46 -0
  16. claude_mpm/agents/templates/documentation_agent.json +45 -0
  17. claude_mpm/agents/templates/engineer_agent.json +49 -0
  18. claude_mpm/agents/templates/ops_agent.json +46 -0
  19. claude_mpm/agents/templates/qa_agent.json +45 -0
  20. claude_mpm/agents/templates/research_agent.json +49 -0
  21. claude_mpm/agents/templates/security_agent.json +46 -0
  22. claude_mpm/agents/templates/update-optimized-specialized-agents.json +374 -0
  23. claude_mpm/agents/templates/version_control_agent.json +46 -0
  24. claude_mpm/agents/test_fix_deployment/.claude-pm/config/project.json +6 -0
  25. claude_mpm/cli.py +655 -0
  26. claude_mpm/cli_main.py +13 -0
  27. claude_mpm/cli_module/__init__.py +15 -0
  28. claude_mpm/cli_module/args.py +222 -0
  29. claude_mpm/cli_module/commands.py +203 -0
  30. claude_mpm/cli_module/migration_example.py +183 -0
  31. claude_mpm/cli_module/refactoring_guide.md +253 -0
  32. claude_mpm/cli_old/__init__.py +1 -0
  33. claude_mpm/cli_old/ticket_cli.py +102 -0
  34. claude_mpm/config/__init__.py +5 -0
  35. claude_mpm/config/hook_config.py +42 -0
  36. claude_mpm/constants.py +150 -0
  37. claude_mpm/core/__init__.py +45 -0
  38. claude_mpm/core/agent_name_normalizer.py +248 -0
  39. claude_mpm/core/agent_registry.py +627 -0
  40. claude_mpm/core/agent_registry.py.bak +312 -0
  41. claude_mpm/core/agent_session_manager.py +273 -0
  42. claude_mpm/core/base_service.py +747 -0
  43. claude_mpm/core/base_service.py.bak +406 -0
  44. claude_mpm/core/config.py +334 -0
  45. claude_mpm/core/config_aliases.py +292 -0
  46. claude_mpm/core/container.py +347 -0
  47. claude_mpm/core/factories.py +281 -0
  48. claude_mpm/core/framework_loader.py +472 -0
  49. claude_mpm/core/injectable_service.py +206 -0
  50. claude_mpm/core/interfaces.py +539 -0
  51. claude_mpm/core/logger.py +468 -0
  52. claude_mpm/core/minimal_framework_loader.py +107 -0
  53. claude_mpm/core/mixins.py +150 -0
  54. claude_mpm/core/service_registry.py +299 -0
  55. claude_mpm/core/session_manager.py +190 -0
  56. claude_mpm/core/simple_runner.py +511 -0
  57. claude_mpm/core/tool_access_control.py +173 -0
  58. claude_mpm/hooks/README.md +243 -0
  59. claude_mpm/hooks/__init__.py +5 -0
  60. claude_mpm/hooks/base_hook.py +154 -0
  61. claude_mpm/hooks/builtin/__init__.py +1 -0
  62. claude_mpm/hooks/builtin/logging_hook_example.py +165 -0
  63. claude_mpm/hooks/builtin/post_delegation_hook_example.py +124 -0
  64. claude_mpm/hooks/builtin/pre_delegation_hook_example.py +125 -0
  65. claude_mpm/hooks/builtin/submit_hook_example.py +100 -0
  66. claude_mpm/hooks/builtin/ticket_extraction_hook_example.py +237 -0
  67. claude_mpm/hooks/builtin/todo_agent_prefix_hook.py +239 -0
  68. claude_mpm/hooks/builtin/workflow_start_hook.py +181 -0
  69. claude_mpm/hooks/hook_client.py +264 -0
  70. claude_mpm/hooks/hook_runner.py +370 -0
  71. claude_mpm/hooks/json_rpc_executor.py +259 -0
  72. claude_mpm/hooks/json_rpc_hook_client.py +319 -0
  73. claude_mpm/hooks/tool_call_interceptor.py +204 -0
  74. claude_mpm/init.py +246 -0
  75. claude_mpm/orchestration/SUBPROCESS_DESIGN.md +66 -0
  76. claude_mpm/orchestration/__init__.py +6 -0
  77. claude_mpm/orchestration/archive/direct_orchestrator.py +195 -0
  78. claude_mpm/orchestration/archive/factory.py +215 -0
  79. claude_mpm/orchestration/archive/hook_enabled_orchestrator.py +188 -0
  80. claude_mpm/orchestration/archive/hook_integration_example.py +178 -0
  81. claude_mpm/orchestration/archive/interactive_subprocess_orchestrator.py +826 -0
  82. claude_mpm/orchestration/archive/orchestrator.py +501 -0
  83. claude_mpm/orchestration/archive/pexpect_orchestrator.py +252 -0
  84. claude_mpm/orchestration/archive/pty_orchestrator.py +270 -0
  85. claude_mpm/orchestration/archive/simple_orchestrator.py +82 -0
  86. claude_mpm/orchestration/archive/subprocess_orchestrator.py +801 -0
  87. claude_mpm/orchestration/archive/system_prompt_orchestrator.py +278 -0
  88. claude_mpm/orchestration/archive/wrapper_orchestrator.py +187 -0
  89. claude_mpm/scripts/__init__.py +1 -0
  90. claude_mpm/scripts/ticket.py +269 -0
  91. claude_mpm/services/__init__.py +10 -0
  92. claude_mpm/services/agent_deployment.py +955 -0
  93. claude_mpm/services/agent_lifecycle_manager.py +948 -0
  94. claude_mpm/services/agent_management_service.py +596 -0
  95. claude_mpm/services/agent_modification_tracker.py +841 -0
  96. claude_mpm/services/agent_profile_loader.py +606 -0
  97. claude_mpm/services/agent_registry.py +677 -0
  98. claude_mpm/services/base_agent_manager.py +380 -0
  99. claude_mpm/services/framework_agent_loader.py +337 -0
  100. claude_mpm/services/framework_claude_md_generator/README.md +92 -0
  101. claude_mpm/services/framework_claude_md_generator/__init__.py +206 -0
  102. claude_mpm/services/framework_claude_md_generator/content_assembler.py +151 -0
  103. claude_mpm/services/framework_claude_md_generator/content_validator.py +126 -0
  104. claude_mpm/services/framework_claude_md_generator/deployment_manager.py +137 -0
  105. claude_mpm/services/framework_claude_md_generator/section_generators/__init__.py +106 -0
  106. claude_mpm/services/framework_claude_md_generator/section_generators/agents.py +582 -0
  107. claude_mpm/services/framework_claude_md_generator/section_generators/claude_pm_init.py +97 -0
  108. claude_mpm/services/framework_claude_md_generator/section_generators/core_responsibilities.py +27 -0
  109. claude_mpm/services/framework_claude_md_generator/section_generators/delegation_constraints.py +23 -0
  110. claude_mpm/services/framework_claude_md_generator/section_generators/environment_config.py +23 -0
  111. claude_mpm/services/framework_claude_md_generator/section_generators/footer.py +20 -0
  112. claude_mpm/services/framework_claude_md_generator/section_generators/header.py +26 -0
  113. claude_mpm/services/framework_claude_md_generator/section_generators/orchestration_principles.py +30 -0
  114. claude_mpm/services/framework_claude_md_generator/section_generators/role_designation.py +37 -0
  115. claude_mpm/services/framework_claude_md_generator/section_generators/subprocess_validation.py +111 -0
  116. claude_mpm/services/framework_claude_md_generator/section_generators/todo_task_tools.py +89 -0
  117. claude_mpm/services/framework_claude_md_generator/section_generators/troubleshooting.py +39 -0
  118. claude_mpm/services/framework_claude_md_generator/section_manager.py +106 -0
  119. claude_mpm/services/framework_claude_md_generator/version_manager.py +121 -0
  120. claude_mpm/services/framework_claude_md_generator.py +621 -0
  121. claude_mpm/services/hook_service.py +388 -0
  122. claude_mpm/services/hook_service_manager.py +223 -0
  123. claude_mpm/services/json_rpc_hook_manager.py +92 -0
  124. claude_mpm/services/parent_directory_manager/README.md +83 -0
  125. claude_mpm/services/parent_directory_manager/__init__.py +577 -0
  126. claude_mpm/services/parent_directory_manager/backup_manager.py +258 -0
  127. claude_mpm/services/parent_directory_manager/config_manager.py +210 -0
  128. claude_mpm/services/parent_directory_manager/deduplication_manager.py +279 -0
  129. claude_mpm/services/parent_directory_manager/framework_protector.py +143 -0
  130. claude_mpm/services/parent_directory_manager/operations.py +186 -0
  131. claude_mpm/services/parent_directory_manager/state_manager.py +624 -0
  132. claude_mpm/services/parent_directory_manager/template_deployer.py +579 -0
  133. claude_mpm/services/parent_directory_manager/validation_manager.py +378 -0
  134. claude_mpm/services/parent_directory_manager/version_control_helper.py +339 -0
  135. claude_mpm/services/parent_directory_manager/version_manager.py +222 -0
  136. claude_mpm/services/shared_prompt_cache.py +819 -0
  137. claude_mpm/services/ticket_manager.py +213 -0
  138. claude_mpm/services/ticket_manager_di.py +318 -0
  139. claude_mpm/services/ticketing_service_original.py +508 -0
  140. claude_mpm/services/version_control/VERSION +1 -0
  141. claude_mpm/services/version_control/__init__.py +70 -0
  142. claude_mpm/services/version_control/branch_strategy.py +670 -0
  143. claude_mpm/services/version_control/conflict_resolution.py +744 -0
  144. claude_mpm/services/version_control/git_operations.py +784 -0
  145. claude_mpm/services/version_control/semantic_versioning.py +703 -0
  146. claude_mpm/ui/__init__.py +1 -0
  147. claude_mpm/ui/rich_terminal_ui.py +295 -0
  148. claude_mpm/ui/terminal_ui.py +328 -0
  149. claude_mpm/utils/__init__.py +16 -0
  150. claude_mpm/utils/config_manager.py +468 -0
  151. claude_mpm/utils/import_migration_example.py +80 -0
  152. claude_mpm/utils/imports.py +182 -0
  153. claude_mpm/utils/path_operations.py +357 -0
  154. claude_mpm/utils/paths.py +289 -0
  155. claude_mpm-0.3.0.dist-info/METADATA +290 -0
  156. claude_mpm-0.3.0.dist-info/RECORD +159 -0
  157. claude_mpm-0.3.0.dist-info/WHEEL +5 -0
  158. claude_mpm-0.3.0.dist-info/entry_points.txt +4 -0
  159. claude_mpm-0.3.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,519 @@
1
+ #!/usr/bin/env python3
2
+ """
3
+ Base Agent Loader Utility
4
+ ========================
5
+
6
+ Provides functionality to load and prepend base agent instructions to all agent prompts.
7
+ Integrates with SharedPromptCache for performance optimization.
8
+
9
+ Key Features:
10
+ - Load base_agent.md content with caching
11
+ - Prepend base instructions to agent prompts
12
+ - Thread-safe operations
13
+ - Error handling for missing base instructions
14
+ - Integration with SharedPromptCache
15
+ - Dynamic prompt templates based on task complexity
16
+
17
+ Usage:
18
+ from claude_pm.agents.base_agent_loader import prepend_base_instructions
19
+
20
+ # Get agent prompt with base instructions prepended
21
+ full_prompt = prepend_base_instructions(get_documentation_agent_prompt())
22
+ """
23
+
24
+ import logging
25
+ import os
26
+ from pathlib import Path
27
+ from typing import Optional, Dict, Any
28
+ from enum import Enum
29
+
30
+ from ..services.shared_prompt_cache import SharedPromptCache
31
+
32
+ # Module-level logger
33
+ logger = logging.getLogger(__name__)
34
+
35
+ # Cache key for base agent instructions
36
+ BASE_AGENT_CACHE_KEY = "base_agent:instructions"
37
+
38
+ def _get_base_agent_file() -> Path:
39
+ """Get the base agent file path dynamically."""
40
+ # Check if we're running from a wheel installation
41
+ try:
42
+ import claude_pm
43
+ package_path = Path(claude_pm.__file__).parent
44
+ path_str = str(package_path.resolve())
45
+ if 'site-packages' in path_str or 'dist-packages' in path_str:
46
+ # For wheel installations, check data directory
47
+ data_base_agent = package_path / "data" / "framework" / "agent-roles" / "base_agent.md"
48
+ if data_base_agent.exists():
49
+ logger.debug(f"Using wheel installation base_agent: {data_base_agent}")
50
+ return data_base_agent
51
+ except Exception:
52
+ pass
53
+
54
+ # For development, find framework directory
55
+ current = Path.cwd()
56
+
57
+ # Check current directory and parents
58
+ for path in [current] + list(current.parents):
59
+ framework_base_agent = path / "framework" / "agent-roles" / "base_agent.md"
60
+ if framework_base_agent.exists():
61
+ logger.debug(f"Using development base_agent: {framework_base_agent}")
62
+ return framework_base_agent
63
+
64
+ # Fallback to old behavior (though this shouldn't happen)
65
+ fallback = Path(__file__).parent.parent.parent / "framework" / "agent-roles" / "base_agent.md"
66
+ logger.warning(f"Using fallback base_agent path: {fallback}")
67
+ return fallback
68
+
69
+
70
+ # Base agent file path (dynamically determined)
71
+ BASE_AGENT_FILE = _get_base_agent_file()
72
+
73
+
74
+ class PromptTemplate(Enum):
75
+ """Dynamic prompt template levels."""
76
+ MINIMAL = "MINIMAL" # Core instructions only (~300 chars)
77
+ STANDARD = "STANDARD" # Core + context + basic integration (~700 chars)
78
+ FULL = "FULL" # All sections including escalation (~1500 chars)
79
+
80
+
81
+ # Template section definitions
82
+ # Optimized to reduce STANDARD template size while maintaining essential guidance
83
+ TEMPLATE_SECTIONS = {
84
+ "core_principles": {
85
+ "templates": ["MINIMAL", "STANDARD", "FULL"],
86
+ "content": "Core Agent Principles"
87
+ },
88
+ "communication_standards": {
89
+ "templates": ["MINIMAL", "STANDARD", "FULL"],
90
+ "content": "Communication Standards"
91
+ },
92
+ "test_protocols": {
93
+ "templates": ["FULL"], # Moved to FULL only - not needed for most tasks
94
+ "content": "Test Response Protocol"
95
+ },
96
+ "reporting_requirements": {
97
+ "templates": ["STANDARD", "FULL"],
98
+ "content": "Reporting Requirements"
99
+ },
100
+ "error_handling": {
101
+ "templates": ["STANDARD", "FULL"],
102
+ "content": "Error Handling"
103
+ },
104
+ "security_awareness": {
105
+ "templates": ["FULL"],
106
+ "content": "Security Awareness"
107
+ },
108
+ "temporal_context": {
109
+ "templates": ["FULL"], # Moved to FULL - not essential for STANDARD
110
+ "content": "Temporal Context Integration"
111
+ },
112
+ "quality_standards": {
113
+ "templates": ["FULL"],
114
+ "content": "Quality Standards"
115
+ },
116
+ "tool_usage": {
117
+ "templates": ["FULL"], # Moved to FULL - agent-specific guidance suffices
118
+ "content": "Tool Usage Guidelines"
119
+ },
120
+ "collaboration_protocols": {
121
+ "templates": ["STANDARD", "FULL"], # Keep for STANDARD - essential
122
+ "content": "Collaboration Protocols"
123
+ },
124
+ "cross_agent_dependencies": {
125
+ "templates": ["FULL"], # Only needed for complex multi-agent tasks
126
+ "content": "Cross-Agent Dependencies"
127
+ },
128
+ "performance_optimization": {
129
+ "templates": ["FULL"],
130
+ "content": "Performance Optimization"
131
+ },
132
+ "escalation_triggers": {
133
+ "templates": ["FULL"],
134
+ "content": "Escalation Triggers"
135
+ },
136
+ "output_formatting": {
137
+ "templates": ["FULL"], # Moved to FULL - basic formatting in STANDARD suffices
138
+ "content": "Output Formatting Standards"
139
+ },
140
+ "framework_integration": {
141
+ "templates": ["FULL"],
142
+ "content": "Framework Integration"
143
+ },
144
+ "constraints": {
145
+ "templates": ["MINIMAL", "STANDARD", "FULL"],
146
+ "content": "Universal Constraints"
147
+ },
148
+ "success_criteria": {
149
+ "templates": ["FULL"], # Moved to FULL - implicit for simpler tasks
150
+ "content": "Success Criteria"
151
+ }
152
+ }
153
+
154
+
155
+ def load_base_agent_instructions(force_reload: bool = False) -> Optional[str]:
156
+ """
157
+ Load base agent instructions from base_agent.md with caching.
158
+ Conditionally includes test-mode instructions based on CLAUDE_PM_TEST_MODE.
159
+
160
+ Args:
161
+ force_reload: Force reload from file, bypassing cache
162
+
163
+ Returns:
164
+ str: Base agent instructions content, or None if file not found
165
+ """
166
+ try:
167
+ # Check if we're in test mode
168
+ test_mode = os.environ.get('CLAUDE_PM_TEST_MODE', '').lower() in ['true', '1', 'yes']
169
+
170
+ # Get cache instance
171
+ cache = SharedPromptCache.get_instance()
172
+
173
+ # Different cache keys for test mode vs normal mode
174
+ cache_key = f"{BASE_AGENT_CACHE_KEY}:{'test' if test_mode else 'normal'}"
175
+
176
+ # Check cache first (unless force reload)
177
+ if not force_reload:
178
+ cached_content = cache.get(cache_key)
179
+ if cached_content is not None:
180
+ logger.debug(f"Base agent instructions loaded from cache (test_mode={test_mode})")
181
+ return str(cached_content)
182
+
183
+ # Get fresh base agent file path
184
+ base_agent_file = _get_base_agent_file()
185
+
186
+ # Load from file
187
+ if not base_agent_file.exists():
188
+ logger.warning(f"Base agent instructions file not found: {base_agent_file}")
189
+ return None
190
+
191
+ logger.debug(f"Loading base agent instructions from: {base_agent_file}")
192
+ content = base_agent_file.read_text(encoding='utf-8')
193
+
194
+ # If NOT in test mode, remove test-specific instructions to save context
195
+ if not test_mode:
196
+ content = _remove_test_mode_instructions(content)
197
+ logger.debug("Test-mode instructions removed (not in test mode)")
198
+ else:
199
+ logger.info("Test-mode instructions included (CLAUDE_PM_TEST_MODE is enabled)")
200
+
201
+ # Cache the content with 1 hour TTL
202
+ cache.set(cache_key, content, ttl=3600)
203
+ logger.debug(f"Base agent instructions cached successfully (test_mode={test_mode})")
204
+
205
+ return content
206
+
207
+ except Exception as e:
208
+ logger.error(f"Error loading base agent instructions: {e}")
209
+ return None
210
+
211
+
212
+ def _remove_test_mode_instructions(content: str) -> str:
213
+ """
214
+ Remove test-mode specific instructions from base agent content.
215
+
216
+ This removes the "Standard Test Response Protocol"
217
+ sections to save context when not in test mode.
218
+
219
+ Args:
220
+ content: Full base agent instructions content
221
+
222
+ Returns:
223
+ str: Content with test-mode instructions removed
224
+ """
225
+ lines = content.split('\n')
226
+ filtered_lines = []
227
+ skip_section = False
228
+
229
+ i = 0
230
+ while i < len(lines):
231
+ line = lines[i]
232
+
233
+ # Check if we're entering the test response protocol section
234
+ if line.strip() == '## Standard Test Response Protocol':
235
+ skip_section = True
236
+ i += 1
237
+ continue
238
+
239
+ # Check if we're in the test section and need to continue skipping
240
+ if skip_section:
241
+ # Check if we've reached a section that's NOT part of test protocol
242
+ # This includes any heading that's not a subsection of the test protocol
243
+ if (line.startswith('####') or line.startswith('###') or line.startswith('##')) and \
244
+ 'Standard Test Response Protocol' not in line:
245
+ skip_section = False
246
+ # Don't skip this line - it's the start of a new section
247
+ filtered_lines.append(line)
248
+ i += 1
249
+ continue
250
+ # Skip this line as we're still in test section
251
+ i += 1
252
+ continue
253
+
254
+ # Not in test section, keep the line
255
+ filtered_lines.append(line)
256
+ i += 1
257
+
258
+ # Join back and clean up extra blank lines
259
+ result = '\n'.join(filtered_lines)
260
+
261
+ # Replace multiple consecutive newlines with double newlines
262
+ while '\n\n\n' in result:
263
+ result = result.replace('\n\n\n', '\n\n')
264
+
265
+ return result.strip()
266
+
267
+
268
+ def _build_dynamic_prompt(content: str, template: PromptTemplate) -> str:
269
+ """
270
+ Build a dynamic prompt based on the template level.
271
+
272
+ Args:
273
+ content: Full base agent content
274
+ template: Template level to use
275
+
276
+ Returns:
277
+ str: Filtered content based on template
278
+ """
279
+ if template == PromptTemplate.FULL:
280
+ # Return full content for FULL template
281
+ return content
282
+
283
+ # Parse content into sections
284
+ sections = _parse_content_sections(content)
285
+
286
+ # Build prompt based on template sections
287
+ filtered_lines = []
288
+ filtered_lines.append("# Base Agent Instructions\n")
289
+ filtered_lines.append("## 🤖 Agent Framework Context\n")
290
+ filtered_lines.append("You are operating as a specialized agent within the Claude PM Framework. You have been delegated a specific task by the PM Orchestrator and must complete it according to your specialized role and authority.\n")
291
+
292
+ # Add sections based on template
293
+ template_name = template.value
294
+ for section_key, section_config in TEMPLATE_SECTIONS.items():
295
+ if template_name in section_config["templates"]:
296
+ section_name = section_config["content"]
297
+ assert isinstance(section_name, str), "Section name must be string"
298
+ if section_name in sections:
299
+ filtered_lines.append(sections[section_name])
300
+
301
+ # Clean up multiple newlines
302
+ result = '\n'.join(filtered_lines)
303
+ while '\n\n\n' in result:
304
+ result = result.replace('\n\n\n', '\n\n')
305
+
306
+ return result.strip()
307
+
308
+
309
+ def _parse_content_sections(content: str) -> Dict[str, str]:
310
+ """
311
+ Parse content into named sections.
312
+
313
+ Args:
314
+ content: Full content to parse
315
+
316
+ Returns:
317
+ Dict mapping section names to their content
318
+ """
319
+ sections = {}
320
+ current_section = None
321
+ current_content = []
322
+
323
+ lines = content.split('\n')
324
+
325
+ for line in lines:
326
+ # Check if this is a section header
327
+ if line.startswith('### '):
328
+ # Save previous section if exists
329
+ if current_section:
330
+ sections[current_section] = '\n'.join(current_content)
331
+ current_content = []
332
+
333
+ # Extract section name
334
+ current_section = line[4:].strip()
335
+ current_content.append(line)
336
+
337
+ elif line.startswith('## ') and 'Agent Framework Context' not in line:
338
+ # Handle ## level sections (skip the main header)
339
+ if current_section:
340
+ sections[current_section] = '\n'.join(current_content)
341
+ current_content = []
342
+
343
+ current_section = line[3:].strip()
344
+ current_content.append(line)
345
+
346
+ elif line.startswith('#### '):
347
+ # Handle #### level subsections
348
+ if current_section:
349
+ # Check for PM Orchestrator Integration vs PM Workflow Integration
350
+ subsection_name = line[5:].strip()
351
+ if subsection_name in ["PM Orchestrator Integration", "PM Workflow Integration"]:
352
+ # Merge these redundant sections under "Collaboration Protocols"
353
+ if current_section != "Collaboration Protocols":
354
+ current_section = "PM Integration"
355
+ current_content.append(line)
356
+
357
+ elif current_section:
358
+ current_content.append(line)
359
+
360
+ # Save final section
361
+ if current_section and current_content:
362
+ sections[current_section] = '\n'.join(current_content)
363
+
364
+ # Merge redundant PM sections if both exist
365
+ if "PM Orchestrator Integration" in sections and "PM Workflow Integration" in sections:
366
+ # Combine into single PM Integration section
367
+ sections["PM Integration"] = (
368
+ "#### PM Integration\n" +
369
+ sections["PM Orchestrator Integration"].replace("#### PM Orchestrator Integration", "").strip() +
370
+ "\n\n" +
371
+ sections["PM Workflow Integration"].replace("#### PM Workflow Integration", "").strip()
372
+ )
373
+ # Remove redundant sections
374
+ del sections["PM Orchestrator Integration"]
375
+ del sections["PM Workflow Integration"]
376
+
377
+ return sections
378
+
379
+
380
+ def prepend_base_instructions(
381
+ agent_prompt: str,
382
+ separator: str = "\n\n---\n\n",
383
+ template: Optional[PromptTemplate] = None,
384
+ complexity_score: Optional[int] = None
385
+ ) -> str:
386
+ """
387
+ Prepend base agent instructions to an agent-specific prompt.
388
+
389
+ Args:
390
+ agent_prompt: The agent-specific prompt to prepend to
391
+ separator: String to separate base instructions from agent prompt
392
+ template: Optional template level to use (auto-selected if not provided)
393
+ complexity_score: Optional complexity score for template selection
394
+
395
+ Returns:
396
+ str: Combined prompt with base instructions prepended
397
+ """
398
+ # Auto-select template based on complexity if not provided
399
+ if template is None:
400
+ if complexity_score is not None:
401
+ if complexity_score <= 30:
402
+ template = PromptTemplate.MINIMAL
403
+ elif complexity_score <= 70:
404
+ template = PromptTemplate.STANDARD
405
+ else:
406
+ template = PromptTemplate.FULL
407
+ else:
408
+ # Default to STANDARD if no complexity info
409
+ template = PromptTemplate.STANDARD
410
+
411
+ # Check if we're in test mode - always use FULL template for tests
412
+ test_mode = os.environ.get('CLAUDE_PM_TEST_MODE', '').lower() in ['true', '1', 'yes']
413
+ if test_mode:
414
+ template = PromptTemplate.FULL
415
+
416
+ # Get cache instance
417
+ cache = SharedPromptCache.get_instance()
418
+
419
+ # Different cache keys for different templates and test mode
420
+ cache_key = f"{BASE_AGENT_CACHE_KEY}:{template.value}:{'test' if test_mode else 'normal'}"
421
+
422
+ # Check cache first
423
+ cached_content = cache.get(cache_key)
424
+ if cached_content is not None:
425
+ logger.debug(f"Base agent instructions loaded from cache (template={template.value}, test_mode={test_mode})")
426
+ base_instructions = cached_content
427
+ else:
428
+ # Load full content
429
+ full_content = load_base_agent_instructions()
430
+
431
+ # If no base instructions, return original prompt
432
+ if not full_content:
433
+ logger.warning("No base instructions available, returning original prompt")
434
+ return agent_prompt
435
+
436
+ # Build dynamic prompt based on template
437
+ base_instructions = _build_dynamic_prompt(full_content, template)
438
+
439
+ # Cache the filtered content
440
+ cache.set(cache_key, base_instructions, ttl=3600)
441
+ logger.debug(f"Dynamic base agent instructions cached (template={template.value})")
442
+
443
+ # Log template selection
444
+ if complexity_score is not None:
445
+ logger.info(
446
+ f"Using {template.value} prompt template "
447
+ f"(complexity_score={complexity_score}, size={len(base_instructions)} chars)"
448
+ )
449
+
450
+ # Combine base instructions with agent prompt
451
+ combined_prompt = f"{base_instructions}{separator}{agent_prompt}"
452
+
453
+ return combined_prompt
454
+
455
+
456
+ def clear_base_agent_cache() -> None:
457
+ """Clear the cached base agent instructions for all templates and modes."""
458
+ try:
459
+ cache = SharedPromptCache.get_instance()
460
+ # Clear caches for all template levels and modes
461
+ for template in PromptTemplate:
462
+ for mode in ['normal', 'test']:
463
+ cache_key = f"{BASE_AGENT_CACHE_KEY}:{template.value}:{mode}"
464
+ cache.invalidate(cache_key)
465
+
466
+ # Also clear the old-style caches for backward compatibility
467
+ cache.invalidate(f"{BASE_AGENT_CACHE_KEY}:normal")
468
+ cache.invalidate(f"{BASE_AGENT_CACHE_KEY}:test")
469
+
470
+ logger.debug("Base agent cache cleared (all templates and modes)")
471
+ except Exception as e:
472
+ logger.error(f"Error clearing base agent cache: {e}")
473
+
474
+
475
+ def get_base_agent_path() -> Path:
476
+ """Get the path to the base agent instructions file."""
477
+ return BASE_AGENT_FILE
478
+
479
+
480
+ def validate_base_agent_file() -> bool:
481
+ """
482
+ Validate that base agent file exists and is readable.
483
+
484
+ Returns:
485
+ bool: True if file exists and is readable, False otherwise
486
+ """
487
+ try:
488
+ if not BASE_AGENT_FILE.exists():
489
+ logger.error(f"Base agent file does not exist: {BASE_AGENT_FILE}")
490
+ return False
491
+
492
+ if not BASE_AGENT_FILE.is_file():
493
+ logger.error(f"Base agent path is not a file: {BASE_AGENT_FILE}")
494
+ return False
495
+
496
+ # Try to read the file
497
+ BASE_AGENT_FILE.read_text(encoding='utf-8')
498
+ return True
499
+
500
+ except Exception as e:
501
+ logger.error(f"Base agent file validation failed: {e}")
502
+ return False
503
+
504
+
505
+ # Module initialization - validate base agent file on import
506
+ if not validate_base_agent_file():
507
+ logger.warning("Base agent file validation failed during module import")
508
+
509
+
510
+ # Export key components
511
+ __all__ = [
512
+ 'prepend_base_instructions',
513
+ 'load_base_agent_instructions',
514
+ 'clear_base_agent_cache',
515
+ 'get_base_agent_path',
516
+ 'validate_base_agent_file',
517
+ 'PromptTemplate',
518
+ 'TEMPLATE_SECTIONS'
519
+ ]
@@ -0,0 +1,160 @@
1
+ {
2
+ "$schema": "http://json-schema.org/draft-07/schema#",
3
+ "title": "Claude MPM Agent Definition Schema",
4
+ "description": "Schema for defining Claude MPM agents matching the target YAML structure",
5
+ "type": "object",
6
+ "required": ["version", "agent_type"],
7
+ "properties": {
8
+ "version": {
9
+ "type": "integer",
10
+ "description": "Agent template version number",
11
+ "minimum": 1
12
+ },
13
+ "agent_type": {
14
+ "type": "string",
15
+ "description": "Type of agent (base or specific agent name)",
16
+ "enum": ["base", "engineer", "qa", "documentation", "research", "security", "ops", "data_engineer", "version_control"]
17
+ },
18
+ "narrative_fields": {
19
+ "type": "object",
20
+ "description": "Narrative content that gets combined between base and specific agents",
21
+ "properties": {
22
+ "when_to_use": {
23
+ "type": "array",
24
+ "items": {
25
+ "type": "string"
26
+ },
27
+ "description": "List of when to use this agent - maps to YAML when_to_use section"
28
+ },
29
+ "specialized_knowledge": {
30
+ "type": "array",
31
+ "items": {
32
+ "type": "string"
33
+ },
34
+ "description": "List of specialized knowledge areas - maps to YAML rationale.specialized_knowledge"
35
+ },
36
+ "unique_capabilities": {
37
+ "type": "array",
38
+ "items": {
39
+ "type": "string"
40
+ },
41
+ "description": "List of unique capabilities - maps to YAML rationale.unique_capabilities"
42
+ },
43
+ "instructions": {
44
+ "type": "string",
45
+ "description": "Additional markdown instructions for the agent - appears after YAML in system prompt"
46
+ }
47
+ },
48
+ "additionalProperties": false
49
+ },
50
+ "configuration_fields": {
51
+ "type": "object",
52
+ "description": "Configuration values where specific agent overrides base agent",
53
+ "properties": {
54
+ "description": {
55
+ "type": "string",
56
+ "description": "Agent description - maps to YAML description field"
57
+ },
58
+ "tags": {
59
+ "type": "array",
60
+ "items": {
61
+ "type": "string"
62
+ },
63
+ "description": "Agent tags - maps to YAML tags field"
64
+ },
65
+ "tools": {
66
+ "type": "array",
67
+ "items": {
68
+ "type": "string"
69
+ },
70
+ "description": "Available tools for the agent - maps to YAML tools field"
71
+ },
72
+ "temperature": {
73
+ "type": "number",
74
+ "minimum": 0,
75
+ "maximum": 1,
76
+ "description": "Model temperature setting - maps to YAML temperature field"
77
+ },
78
+ "timeout": {
79
+ "type": "integer",
80
+ "minimum": 1,
81
+ "description": "Timeout in seconds - maps to YAML timeout and execution_timeout fields"
82
+ },
83
+ "max_tokens": {
84
+ "type": "integer",
85
+ "minimum": 1,
86
+ "description": "Maximum tokens for responses - maps to YAML max_tokens field"
87
+ },
88
+ "memory_limit": {
89
+ "type": "integer",
90
+ "minimum": 1,
91
+ "description": "Memory limit in MB - maps to YAML memory_limit field"
92
+ },
93
+ "cpu_limit": {
94
+ "type": "integer",
95
+ "minimum": 1,
96
+ "maximum": 100,
97
+ "description": "CPU limit percentage - maps to YAML cpu_limit field"
98
+ },
99
+ "network_access": {
100
+ "type": "boolean",
101
+ "description": "Whether agent has network access - maps to YAML network_access field"
102
+ },
103
+ "primary_role": {
104
+ "type": "string",
105
+ "description": "Primary role of the agent - maps to YAML capabilities.primary_role field"
106
+ },
107
+ "specializations": {
108
+ "type": "array",
109
+ "items": {
110
+ "type": "string"
111
+ },
112
+ "description": "Agent specializations - maps to YAML capabilities.specializations field"
113
+ },
114
+ "authority": {
115
+ "type": "string",
116
+ "description": "Authority level of the agent - maps to YAML capabilities.authority field"
117
+ },
118
+ "model": {
119
+ "type": "string",
120
+ "description": "Model to use - maps to YAML model field",
121
+ "default": "claude-3-5-sonnet-20241022"
122
+ },
123
+ "file_access": {
124
+ "type": "string",
125
+ "description": "File access level - maps to YAML file_access field",
126
+ "enum": ["project", "limited", "none"],
127
+ "default": "project"
128
+ },
129
+ "dangerous_tools": {
130
+ "type": "boolean",
131
+ "description": "Whether dangerous tools are allowed - maps to YAML dangerous_tools field",
132
+ "default": false
133
+ },
134
+ "review_required": {
135
+ "type": "boolean",
136
+ "description": "Whether review is required - maps to YAML review_required field",
137
+ "default": false
138
+ },
139
+ "team": {
140
+ "type": "string",
141
+ "description": "Team name - maps to YAML team field",
142
+ "default": "mpm-framework"
143
+ },
144
+ "project": {
145
+ "type": "string",
146
+ "description": "Project name - maps to YAML project field",
147
+ "default": "claude-mpm"
148
+ },
149
+ "priority": {
150
+ "type": "string",
151
+ "description": "Priority level - maps to YAML priority field",
152
+ "enum": ["low", "medium", "high"],
153
+ "default": "high"
154
+ }
155
+ },
156
+ "additionalProperties": false
157
+ }
158
+ },
159
+ "additionalProperties": false
160
+ }