Coverage for /Users/les/Projects/crackerjack/crackerjack/agents/proactive_agent.py: 0%
38 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
2from abc import abstractmethod
4from .base import AgentContext, FixResult, Issue, SubAgent
7class ProactiveAgent(SubAgent):
8 def __init__(self, context: AgentContext) -> None:
9 super().__init__(context)
10 self._planning_cache: dict[str, dict[str, t.Any]] = {}
11 self._pattern_cache: dict[str, t.Any] = {}
13 @abstractmethod
14 async def plan_before_action(self, issue: Issue) -> dict[str, t.Any]:
15 pass
17 async def analyze_and_fix_proactively(self, issue: Issue) -> FixResult:
18 cache_key = self._get_planning_cache_key(issue)
19 if cache_key in self._planning_cache:
20 plan = self._planning_cache[cache_key]
21 self.log(f"Using cached plan for {cache_key}")
22 else:
23 plan = await self.plan_before_action(issue)
24 self._planning_cache[cache_key] = plan
25 self.log(f"Created new plan for {cache_key}")
27 result = await self._execute_with_plan(issue, plan)
29 if result.success and result.confidence >= 0.8:
30 self._cache_successful_pattern(issue, plan, result)
32 return result
34 async def _execute_with_plan(
35 self, issue: Issue, plan: dict[str, t.Any]
36 ) -> FixResult:
37 return await self.analyze_and_fix(issue)
39 def _get_planning_cache_key(self, issue: Issue) -> str:
40 return f"{issue.type.value}:{issue.file_path}:{issue.line_number}"
42 def _cache_successful_pattern(
43 self, issue: Issue, plan: dict[str, t.Any], result: FixResult
44 ) -> None:
45 pattern_key = f"{issue.type.value}_{plan.get('strategy', 'default')}"
46 self._pattern_cache[pattern_key] = {
47 "plan": plan,
48 "confidence": result.confidence,
49 "files_modified": result.files_modified,
50 "fixes_applied": result.fixes_applied,
51 }
52 self.log(f"Cached successful pattern: {pattern_key}")
54 def get_cached_patterns(self) -> dict[str, t.Any]:
55 return self._pattern_cache.copy()
57 def clear_pattern_cache(self) -> None:
58 self._pattern_cache.clear()
59 self.log("Cleared pattern cache")
61 def get_planning_confidence(self, issue: Issue) -> float:
62 issue_patterns = [
63 key for key in self._pattern_cache if key.startswith(issue.type.value)
64 ]
66 if issue_patterns:
67 return min(0.9, 0.6 + (len(issue_patterns) * 0.1))
69 return 0.5