|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +User Story Epic Assignment Script |
| 4 | +Systematically assigns all user stories to appropriate epics based on content analysis. |
| 5 | +
|
| 6 | +Created: 2025-09-05 |
| 7 | +Last Updated: 2025-09-05 |
| 8 | +Temporal Authority: Local Machine (Always Trusted) |
| 9 | +""" |
| 10 | + |
| 11 | +import os |
| 12 | +import re |
| 13 | +import json |
| 14 | +from pathlib import Path |
| 15 | +from typing import Dict, List, Tuple |
| 16 | +from datetime import datetime |
| 17 | + |
| 18 | +class UserStoryEpicAssigner: |
| 19 | + """Systematically assign user stories to epics based on content analysis.""" |
| 20 | + |
| 21 | + def __init__(self): |
| 22 | + self.epic_mappings = { |
| 23 | + # EPIC-0: Development Excellence (Foundation) |
| 24 | + "EPIC-0": { |
| 25 | + "keywords": ["test", "testing", "quality", "code quality", "refactor", "architecture", |
| 26 | + "clean code", "technical debt", "foundation", "infrastructure", "build", |
| 27 | + "deployment", "CI/CD", "automation pipeline", "development excellence"], |
| 28 | + "patterns": [r"US-000", r"test.*fail", r"refactor", r"architecture", r"clean.*code", |
| 29 | + r"technical.*debt", r"build.*system", r"deployment", r"infrastructure"] |
| 30 | + }, |
| 31 | + |
| 32 | + # EPIC-2: Intelligent Prompt Engineering |
| 33 | + "EPIC-2": { |
| 34 | + "keywords": ["prompt", "engineering", "optimization", "template", "LLM", "model", |
| 35 | + "AI interaction", "prompt database", "prompt management", "prompt optimization"], |
| 36 | + "patterns": [r"prompt", r"template", r"LLM", r"model.*optimization", r"AI.*interaction"] |
| 37 | + }, |
| 38 | + |
| 39 | + # EPIC-3: Agent Development & Optimization |
| 40 | + "EPIC-3": { |
| 41 | + "keywords": ["agent", "swarm", "agent framework", "agent development", "AI agent", |
| 42 | + "multi-agent", "agent coordination", "agent architecture", "agent system"], |
| 43 | + "patterns": [r"agent(?!.*logging)", r"swarm", r"multi.*agent", r"agent.*framework", |
| 44 | + r"agent.*development", r"agent.*coordination"] |
| 45 | + }, |
| 46 | + |
| 47 | + # EPIC-4: Integrated System Intelligence & Organic Metabolic Architecture |
| 48 | + "EPIC-4": { |
| 49 | + "keywords": ["intelligence", "cognitive", "learning", "ontological", "reasoning", |
| 50 | + "knowledge", "context", "decision making", "pattern recognition", |
| 51 | + "self-optimization", "metabolic", "health monitoring", "system vitality", |
| 52 | + "agent logging", "agent ontological", "cognitive architecture"], |
| 53 | + "patterns": [r"intelligence", r"cognitive", r"learning", r"ontological", r"reasoning", |
| 54 | + r"knowledge", r"context.*detection", r"decision.*making", r"pattern.*recognition", |
| 55 | + r"self.*optimization", r"health.*monitoring", r"agent.*logging", r"metabolic"] |
| 56 | + }, |
| 57 | + |
| 58 | + # EPIC-6: Full Cursor Automation |
| 59 | + "EPIC-6": { |
| 60 | + "keywords": ["cursor", "automation", "workflow", "IDE", "editor", "development tools", |
| 61 | + "full automation", "workflow composition", "context switching"], |
| 62 | + "patterns": [r"cursor", r"automation", r"workflow", r"IDE", r"editor", |
| 63 | + r"development.*tools", r"workflow.*composition"] |
| 64 | + }, |
| 65 | + |
| 66 | + # EPIC-7: Formal Principles Excellence |
| 67 | + "EPIC-7": { |
| 68 | + "keywords": ["formal", "principles", "mathematical", "logical", "systematic", |
| 69 | + "verification", "validation", "consistency", "formal methods"], |
| 70 | + "patterns": [r"formal", r"principles", r"mathematical", r"logical", r"systematic", |
| 71 | + r"verification", r"validation", r"consistency", r"formal.*methods"] |
| 72 | + }, |
| 73 | + |
| 74 | + # EPIC-8: Developer Delight & Maximum Usefulness |
| 75 | + "EPIC-8": { |
| 76 | + "keywords": ["user experience", "UX", "UI", "interface", "usability", "developer experience", |
| 77 | + "user interface", "dashboard", "visualization", "delight", "usefulness"], |
| 78 | + "patterns": [r"user.*experience", r"UX", r"UI", r"interface", r"usability", |
| 79 | + r"developer.*experience", r"dashboard", r"visualization", r"delight"] |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + self.assignment_results = [] |
| 84 | + self.project_root = Path("C:/Users/pogawal/WorkFolder/Documents/Python/ai-dev-agent") |
| 85 | + |
| 86 | + def analyze_user_story_content(self, file_path: Path) -> Tuple[str, str]: |
| 87 | + """Analyze user story content to determine best epic assignment.""" |
| 88 | + try: |
| 89 | + with open(file_path, 'r', encoding='utf-8') as f: |
| 90 | + content = f.read().lower() |
| 91 | + |
| 92 | + # Score each epic based on keyword and pattern matches |
| 93 | + epic_scores = {} |
| 94 | + |
| 95 | + for epic_id, criteria in self.epic_mappings.items(): |
| 96 | + score = 0 |
| 97 | + matches = [] |
| 98 | + |
| 99 | + # Check keywords |
| 100 | + for keyword in criteria["keywords"]: |
| 101 | + if keyword.lower() in content: |
| 102 | + score += 1 |
| 103 | + matches.append(f"keyword: {keyword}") |
| 104 | + |
| 105 | + # Check patterns |
| 106 | + for pattern in criteria["patterns"]: |
| 107 | + if re.search(pattern, content, re.IGNORECASE): |
| 108 | + score += 2 # Patterns worth more than keywords |
| 109 | + matches.append(f"pattern: {pattern}") |
| 110 | + |
| 111 | + epic_scores[epic_id] = { |
| 112 | + "score": score, |
| 113 | + "matches": matches |
| 114 | + } |
| 115 | + |
| 116 | + # Find best match |
| 117 | + best_epic = max(epic_scores.keys(), key=lambda x: epic_scores[x]["score"]) |
| 118 | + best_score = epic_scores[best_epic]["score"] |
| 119 | + |
| 120 | + # If no clear match, assign to EPIC-0 (Development Excellence) as default |
| 121 | + if best_score == 0: |
| 122 | + best_epic = "EPIC-0" |
| 123 | + reasoning = "Default assignment (no specific epic indicators found)" |
| 124 | + else: |
| 125 | + reasoning = f"Score: {best_score}, Matches: {epic_scores[best_epic]['matches'][:3]}" |
| 126 | + |
| 127 | + return best_epic, reasoning |
| 128 | + |
| 129 | + except Exception as e: |
| 130 | + return "EPIC-0", f"Error reading file: {e}" |
| 131 | + |
| 132 | + def find_all_user_stories(self) -> List[Path]: |
| 133 | + """Find all user story files in the project.""" |
| 134 | + user_story_files = [] |
| 135 | + |
| 136 | + # Search in docs/agile directory |
| 137 | + agile_dir = self.project_root / "docs" / "agile" |
| 138 | + if agile_dir.exists(): |
| 139 | + for file_path in agile_dir.rglob("US-*.md"): |
| 140 | + if file_path.is_file() and not file_path.name.endswith('.backup_20250901_090524') and not file_path.name.endswith('.backup_20250901_090525'): |
| 141 | + user_story_files.append(file_path) |
| 142 | + |
| 143 | + return sorted(user_story_files) |
| 144 | + |
| 145 | + def update_user_story_epic_assignment(self, file_path: Path, epic_id: str) -> bool: |
| 146 | + """Update a user story file with the assigned epic.""" |
| 147 | + try: |
| 148 | + with open(file_path, 'r', encoding='utf-8') as f: |
| 149 | + content = f.read() |
| 150 | + |
| 151 | + epic_name_map = { |
| 152 | + "EPIC-0": "Development Excellence", |
| 153 | + "EPIC-2": "Intelligent Prompt Engineering", |
| 154 | + "EPIC-3": "Agent Development & Optimization", |
| 155 | + "EPIC-4": "Integrated System Intelligence & Organic Metabolic Architecture", |
| 156 | + "EPIC-6": "Full Cursor Automation", |
| 157 | + "EPIC-7": "Formal Principles Excellence", |
| 158 | + "EPIC-8": "Developer Delight & Maximum Usefulness" |
| 159 | + } |
| 160 | + |
| 161 | + epic_line = f"**Epic**: {epic_id} - {epic_name_map.get(epic_id, 'Unknown Epic')}" |
| 162 | + |
| 163 | + # Look for existing epic assignment and update it |
| 164 | + lines = content.split('\n') |
| 165 | + updated_lines = [] |
| 166 | + epic_found = False |
| 167 | + |
| 168 | + for line in lines: |
| 169 | + if re.match(r'\*\*Epic\*\*:', line) or re.match(r'Epic:', line): |
| 170 | + updated_lines.append(epic_line) |
| 171 | + epic_found = True |
| 172 | + elif '## 🎯 **Epic Link**' in line: |
| 173 | + updated_lines.append(line) |
| 174 | + # Add epic line after Epic Link header if not found elsewhere |
| 175 | + if not epic_found: |
| 176 | + updated_lines.append(epic_line) |
| 177 | + epic_found = True |
| 178 | + else: |
| 179 | + updated_lines.append(line) |
| 180 | + |
| 181 | + # If no epic assignment found, add it after the title |
| 182 | + if not epic_found: |
| 183 | + new_lines = [] |
| 184 | + title_found = False |
| 185 | + for i, line in enumerate(updated_lines): |
| 186 | + new_lines.append(line) |
| 187 | + if line.startswith('# ') and not title_found: |
| 188 | + title_found = True |
| 189 | + # Add epic line after title and separator |
| 190 | + new_lines.append('') |
| 191 | + new_lines.append(epic_line) |
| 192 | + new_lines.append('') |
| 193 | + updated_lines = new_lines |
| 194 | + |
| 195 | + # Write updated content |
| 196 | + with open(file_path, 'w', encoding='utf-8') as f: |
| 197 | + f.write('\n'.join(updated_lines)) |
| 198 | + |
| 199 | + return True |
| 200 | + |
| 201 | + except Exception as e: |
| 202 | + print(f"Error updating {file_path}: {e}") |
| 203 | + return False |
| 204 | + |
| 205 | + def assign_all_user_stories(self) -> Dict: |
| 206 | + """Assign all user stories to appropriate epics.""" |
| 207 | + user_story_files = self.find_all_user_stories() |
| 208 | + |
| 209 | + print(f"Found {len(user_story_files)} user story files to process...") |
| 210 | + |
| 211 | + results = { |
| 212 | + "total_files": len(user_story_files), |
| 213 | + "successful_assignments": 0, |
| 214 | + "failed_assignments": 0, |
| 215 | + "assignments_by_epic": {}, |
| 216 | + "assignment_details": [] |
| 217 | + } |
| 218 | + |
| 219 | + for file_path in user_story_files: |
| 220 | + try: |
| 221 | + # Analyze content and determine epic |
| 222 | + assigned_epic, reasoning = self.analyze_user_story_content(file_path) |
| 223 | + |
| 224 | + # Update the file |
| 225 | + success = self.update_user_story_epic_assignment(file_path, assigned_epic) |
| 226 | + |
| 227 | + if success: |
| 228 | + results["successful_assignments"] += 1 |
| 229 | + if assigned_epic not in results["assignments_by_epic"]: |
| 230 | + results["assignments_by_epic"][assigned_epic] = [] |
| 231 | + results["assignments_by_epic"][assigned_epic].append(file_path.name) |
| 232 | + else: |
| 233 | + results["failed_assignments"] += 1 |
| 234 | + |
| 235 | + # Track details |
| 236 | + results["assignment_details"].append({ |
| 237 | + "file": str(file_path.relative_to(self.project_root)), |
| 238 | + "assigned_epic": assigned_epic, |
| 239 | + "reasoning": reasoning, |
| 240 | + "success": success |
| 241 | + }) |
| 242 | + |
| 243 | + print(f"✅ {file_path.name} → {assigned_epic}") |
| 244 | + |
| 245 | + except Exception as e: |
| 246 | + results["failed_assignments"] += 1 |
| 247 | + print(f"❌ Error processing {file_path.name}: {e}") |
| 248 | + |
| 249 | + return results |
| 250 | + |
| 251 | +def main(): |
| 252 | + """Main function to run the epic assignment process.""" |
| 253 | + print("🎯 Starting User Story Epic Assignment Process...") |
| 254 | + print("=" * 60) |
| 255 | + |
| 256 | + assigner = UserStoryEpicAssigner() |
| 257 | + results = assigner.assign_all_user_stories() |
| 258 | + |
| 259 | + print("\n" + "=" * 60) |
| 260 | + print("📊 ASSIGNMENT RESULTS:") |
| 261 | + print(f"Total Files Processed: {results['total_files']}") |
| 262 | + print(f"Successful Assignments: {results['successful_assignments']}") |
| 263 | + print(f"Failed Assignments: {results['failed_assignments']}") |
| 264 | + |
| 265 | + print("\n📋 ASSIGNMENTS BY EPIC:") |
| 266 | + for epic_id, files in results["assignments_by_epic"].items(): |
| 267 | + print(f"{epic_id}: {len(files)} stories") |
| 268 | + for file in files[:5]: # Show first 5 |
| 269 | + print(f" - {file}") |
| 270 | + if len(files) > 5: |
| 271 | + print(f" ... and {len(files) - 5} more") |
| 272 | + |
| 273 | + # Save detailed results |
| 274 | + results_file = Path("epic_assignment_results.json") |
| 275 | + with open(results_file, 'w') as f: |
| 276 | + json.dump(results, f, indent=2, default=str) |
| 277 | + |
| 278 | + print(f"\n💾 Detailed results saved to: {results_file}") |
| 279 | + print("🎉 Epic assignment process completed!") |
| 280 | + |
| 281 | +if __name__ == "__main__": |
| 282 | + main() |
0 commit comments