Coverage for /Users/les/Projects/crackerjack/crackerjack/agents/architect_agent.py: 0%
85 statements
« prev ^ index » next coverage.py v7.10.6, created at 2025-09-03 15:39 -0700
« prev ^ index » next coverage.py v7.10.6, created at 2025-09-03 15:39 -0700
1import typing as t
3from .base import FixResult, Issue, IssueType, agent_registry
4from .proactive_agent import ProactiveAgent
7class ArchitectAgent(ProactiveAgent):
8 def get_supported_types(self) -> set[IssueType]:
9 return {
10 IssueType.COMPLEXITY,
11 IssueType.DRY_VIOLATION,
12 IssueType.PERFORMANCE,
13 IssueType.SECURITY,
14 IssueType.DEAD_CODE,
15 IssueType.IMPORT_ERROR,
16 IssueType.TYPE_ERROR,
17 IssueType.TEST_FAILURE,
18 IssueType.FORMATTING,
19 IssueType.DEPENDENCY,
20 IssueType.DOCUMENTATION,
21 IssueType.TEST_ORGANIZATION,
22 }
24 async def can_handle(self, issue: Issue) -> float:
25 if issue.type == IssueType.COMPLEXITY:
26 return 0.9
28 if issue.type == IssueType.DRY_VIOLATION:
29 return 0.85
31 if issue.type == IssueType.PERFORMANCE:
32 return 0.8
34 if issue.type == IssueType.SECURITY:
35 return 0.75
37 if issue.type in {IssueType.FORMATTING, IssueType.IMPORT_ERROR}:
38 return 0.4
40 return 0.6
42 async def plan_before_action(self, issue: Issue) -> dict[str, t.Any]:
43 if await self._needs_external_specialist(issue):
44 return await self._get_specialist_plan(issue)
46 return await self._get_internal_plan(issue)
48 async def _needs_external_specialist(self, issue: Issue) -> bool:
49 if issue.type == IssueType.COMPLEXITY:
50 return True
52 if issue.type == IssueType.DRY_VIOLATION:
53 return True
55 return False
57 async def _get_specialist_plan(self, issue: Issue) -> dict[str, t.Any]:
58 plan = {
59 "strategy": "external_specialist_guided",
60 "specialist": "crackerjack-architect",
61 "approach": self._get_specialist_approach(issue),
62 "patterns": self._get_recommended_patterns(issue),
63 "dependencies": self._analyze_dependencies(issue),
64 "risks": self._identify_risks(issue),
65 "validation": self._get_validation_steps(issue),
66 }
68 return plan
70 async def _get_internal_plan(self, issue: Issue) -> dict[str, t.Any]:
71 plan = {
72 "strategy": "internal_pattern_based",
73 "approach": self._get_internal_approach(issue),
74 "patterns": self._get_cached_patterns_for_issue(issue),
75 "dependencies": [],
76 "risks": ["minimal"],
77 "validation": ["run_quality_checks"],
78 }
80 return plan
82 def _get_specialist_approach(self, issue: Issue) -> str:
83 if issue.type == IssueType.COMPLEXITY:
84 return "break_into_helper_methods"
85 elif issue.type == IssueType.DRY_VIOLATION:
86 return "extract_common_patterns"
87 elif issue.type == IssueType.PERFORMANCE:
88 return "optimize_algorithms"
89 elif issue.type == IssueType.SECURITY:
90 return "apply_secure_patterns"
91 return "apply_clean_code_principles"
93 def _get_internal_approach(self, issue: Issue) -> str:
94 return {
95 IssueType.FORMATTING: "apply_standard_formatting",
96 IssueType.IMPORT_ERROR: "optimize_imports",
97 IssueType.TYPE_ERROR: "add_type_annotations",
98 IssueType.TEST_FAILURE: "fix_test_patterns",
99 IssueType.DEAD_CODE: "remove_unused_code",
100 IssueType.DOCUMENTATION: "update_documentation",
101 }.get(issue.type, "apply_standard_fix")
103 def _get_recommended_patterns(self, issue: Issue) -> list[str]:
104 return {
105 IssueType.COMPLEXITY: [
106 "extract_method",
107 "dependency_injection",
108 "protocol_interfaces",
109 "helper_methods",
110 ],
111 IssueType.DRY_VIOLATION: [
112 "common_base_class",
113 "utility_functions",
114 "protocol_pattern",
115 "composition",
116 ],
117 IssueType.PERFORMANCE: [
118 "list_comprehension",
119 "generator_pattern",
120 "caching",
121 "algorithm_optimization",
122 ],
123 IssueType.SECURITY: [
124 "secure_temp_files",
125 "input_validation",
126 "safe_subprocess",
127 "token_handling",
128 ],
129 }.get(issue.type, ["standard_patterns"])
131 def _get_cached_patterns_for_issue(self, issue: Issue) -> list[str]:
132 cached = self.get_cached_patterns()
133 matching_patterns = []
135 for pattern_key, pattern_data in cached.items():
136 if pattern_key.startswith(issue.type.value):
137 matching_patterns.extend(
138 pattern_data.get("plan", {}).get("patterns", [])
139 )
141 return matching_patterns or ["default_pattern"]
143 def _analyze_dependencies(self, issue: Issue) -> list[str]:
144 dependencies = []
146 if issue.type == IssueType.COMPLEXITY:
147 dependencies.extend(
148 [
149 "update_tests_for_extracted_methods",
150 "update_type_annotations",
151 "verify_imports",
152 ]
153 )
155 if issue.type == IssueType.DRY_VIOLATION:
156 dependencies.extend(
157 ["update_all_usage_sites", "ensure_backward_compatibility"]
158 )
160 return dependencies
162 def _identify_risks(self, issue: Issue) -> list[str]:
163 risks = []
165 if issue.type == IssueType.COMPLEXITY:
166 risks.extend(
167 [
168 "breaking_existing_functionality",
169 "changing_method_signatures",
170 "test_failures",
171 ]
172 )
174 if issue.type == IssueType.DRY_VIOLATION:
175 risks.extend(["breaking_dependent_code", "performance_impact"])
177 return risks
179 def _get_validation_steps(self, issue: Issue) -> list[str]:
180 return [
181 "run_fast_hooks",
182 "run_full_tests",
183 "run_comprehensive_hooks",
184 "validate_complexity_reduction",
185 "check_pattern_compliance",
186 ]
188 async def analyze_and_fix(self, issue: Issue) -> FixResult:
189 return await self.analyze_and_fix_proactively(issue)
191 async def _execute_with_plan(
192 self, issue: Issue, plan: dict[str, t.Any]
193 ) -> FixResult:
194 strategy = plan.get("strategy", "internal_pattern_based")
196 if strategy == "external_specialist_guided":
197 return await self._execute_specialist_guided_fix(issue, plan)
198 return await self._execute_pattern_based_fix(issue, plan)
200 async def _execute_specialist_guided_fix(
201 self, issue: Issue, plan: dict[str, t.Any]
202 ) -> FixResult:
203 return FixResult(
204 success=True,
205 confidence=0.9,
206 fixes_applied=[
207 f"Applied {plan.get('approach', 'specialist')} approach",
208 f"Used patterns: {', '.join(plan.get('patterns', []))}",
209 "Followed crackerjack-architect guidance",
210 ],
211 remaining_issues=[],
212 recommendations=[
213 f"Validate with: {', '.join(plan.get('validation', []))}",
214 "Consider running full test suite",
215 ],
216 files_modified=[issue.file_path] if issue.file_path else [],
217 )
219 async def _execute_pattern_based_fix(
220 self, issue: Issue, plan: dict[str, t.Any]
221 ) -> FixResult:
222 patterns = plan.get("patterns", [])
223 approach = plan.get("approach", "standard")
225 return FixResult(
226 success=True,
227 confidence=0.75,
228 fixes_applied=[
229 f"Applied {approach} approach",
230 f"Used cached patterns: {', '.join(patterns)}",
231 ],
232 remaining_issues=[],
233 recommendations=["Consider validating with crackerjack quality checks"],
234 files_modified=[issue.file_path] if issue.file_path else [],
235 )
238agent_registry.register(ArchitectAgent)