Coverage for /Users/les/Projects/crackerjack/crackerjack/mcp/tools/error_analyzer.py: 0%

101 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-09-03 15:39 -0700

1import typing as t 

2from contextlib import suppress 

3 

4 

5def analyze_errors_with_caching( 

6 context: t.Any, use_cache: bool = True 

7) -> dict[str, t.Any]: 

8 try: 

9 cached_patterns = _get_cached_patterns(context, use_cache) 

10 return _build_error_analysis(cached_patterns, context) 

11 

12 except Exception as e: 

13 return { 

14 "status": "error", 

15 "message": f"Error analysis failed: {e}", 

16 "recommendations": [], 

17 "patterns": [], 

18 } 

19 

20 

21def _get_cached_patterns(context: t.Any, use_cache: bool) -> list[t.Any]: 

22 if not use_cache: 

23 return [] 

24 

25 with suppress(Exception): 

26 cache = getattr(context, "cache", None) 

27 if cache and hasattr(cache, "get_error_patterns"): 

28 return cache.get_error_patterns() 

29 

30 return [] 

31 

32 

33def _build_error_analysis(patterns: list[t.Any], context: t.Any) -> dict[str, t.Any]: 

34 analysis = { 

35 "status": "success", 

36 "patterns_found": len(patterns), 

37 "recommendations": [], 

38 "error_categories": {}, 

39 "fix_suggestions": [], 

40 "urgency_level": "low", 

41 } 

42 

43 if not patterns: 

44 analysis.update( 

45 { 

46 "message": "No cached error patterns found-this indicates clean execution history", 

47 "recommendations": [ 

48 "Continue with current development practices", 

49 "Consider running comprehensive quality checks if issues arise", 

50 ], 

51 } 

52 ) 

53 return analysis 

54 

55 categories = _categorize_error_patterns(patterns) 

56 analysis["error_categories"] = categories 

57 

58 recommendations = _generate_error_recommendations(categories) 

59 analysis["recommendations"] = recommendations 

60 

61 analysis["urgency_level"] = _calculate_urgency_level(categories) 

62 

63 analysis["fix_suggestions"] = _generate_fix_suggestions(categories) 

64 

65 analysis["message"] = ( 

66 f"Found {len(patterns)} cached error patterns across {len(categories)} categories" 

67 ) 

68 

69 return analysis 

70 

71 

72def _categorize_error_patterns(patterns: list[t.Any]) -> dict[str, list[t.Any]]: 

73 categories = { 

74 "syntax_errors": [], 

75 "import_errors": [], 

76 "type_errors": [], 

77 "test_failures": [], 

78 "security_issues": [], 

79 "complexity_issues": [], 

80 "dependency_issues": [], 

81 "formatting_issues": [], 

82 "unknown": [], 

83 } 

84 

85 for pattern in patterns: 

86 category = _classify_error_pattern(pattern) 

87 categories[category].append(pattern) 

88 

89 return {k: v for k, v in categories.items() if v} 

90 

91 

92def _classify_error_pattern(pattern: t.Any) -> str: 

93 pattern_str = str(pattern).lower() 

94 

95 if any( 

96 keyword in pattern_str 

97 for keyword in ("syntax", "invalid syntax", "unexpected token") 

98 ): 

99 return "syntax_errors" 

100 elif any( 

101 keyword in pattern_str for keyword in ("import", "module", "no module named") 

102 ): 

103 return "import_errors" 

104 elif any( 

105 keyword in pattern_str for keyword in ("type", "annotation", "mypy", "pyright") 

106 ): 

107 return "type_errors" 

108 elif any( 

109 keyword in pattern_str for keyword in ("test", "assert", "pytest", "failed") 

110 ): 

111 return "test_failures" 

112 elif any( 

113 keyword in pattern_str for keyword in ("security", "bandit", "vulnerability") 

114 ): 

115 return "security_issues" 

116 elif any( 

117 keyword in pattern_str for keyword in ("complexity", "complex", "cognitive") 

118 ): 

119 return "complexity_issues" 

120 elif any( 

121 keyword in pattern_str for keyword in ("dependency", "requirement", "package") 

122 ): 

123 return "dependency_issues" 

124 elif any( 

125 keyword in pattern_str for keyword in ("format", "style", "ruff", "black") 

126 ): 

127 return "formatting_issues" 

128 return "unknown" 

129 

130 

131def _generate_error_recommendations(categories: dict[str, list[t.Any]]) -> list[str]: 

132 recommendations = [] 

133 

134 if categories.get("syntax_errors"): 

135 recommendations.append( 

136 "🔧 Review syntax errors and fix basic Python syntax issues" 

137 ) 

138 

139 if categories.get("import_errors"): 

140 recommendations.extend( 

141 [ 

142 "📦 Check imports and module dependencies", 

143 "🔍 Verify all required packages are installed", 

144 ] 

145 ) 

146 

147 if categories.get("type_errors"): 

148 recommendations.extend( 

149 [ 

150 "🏷️ Add missing type annotations", 

151 "🔧 Fix type mismatches and annotation issues", 

152 ] 

153 ) 

154 

155 if categories.get("test_failures"): 

156 recommendations.extend( 

157 [ 

158 "🧪 Fix failing tests and improve test reliability", 

159 "🔬 Review test fixtures and dependencies", 

160 ] 

161 ) 

162 

163 if categories.get("security_issues"): 

164 recommendations.extend( 

165 [ 

166 "🔒 Address security vulnerabilities immediately", 

167 "🛡️ Follow security best practices", 

168 ] 

169 ) 

170 

171 if categories.get("complexity_issues"): 

172 recommendations.extend( 

173 [ 

174 "📐 Refactor complex functions to reduce cognitive load", 

175 "🔄 Break down large functions into smaller components", 

176 ] 

177 ) 

178 

179 if categories.get("dependency_issues"): 

180 recommendations.extend( 

181 [ 

182 "📚 Update dependency management", 

183 "🔄 Review and clean up requirements", 

184 ] 

185 ) 

186 

187 if categories.get("formatting_issues"): 

188 recommendations.extend( 

189 [ 

190 "💅 Apply code formatting and style fixes", 

191 "📋 Ensure consistent code style", 

192 ] 

193 ) 

194 

195 if len(categories) > 3: 

196 recommendations.extend( 

197 [ 

198 "🎯 Consider running AI agent auto-fixing for comprehensive resolution", 

199 "📊 Monitor quality metrics to prevent regression", 

200 ] 

201 ) 

202 

203 return recommendations 

204 

205 

206def _calculate_urgency_level(categories: dict[str, list[t.Any]]) -> str: 

207 total_errors = sum(len(errors) for errors in categories.values()) 

208 

209 if categories.get("security_issues"): 

210 return "high" 

211 

212 if categories.get("test_failures") and len(categories["test_failures"]) > 5: 

213 return "high" 

214 

215 if categories.get("syntax_errors"): 

216 return "medium" 

217 

218 if total_errors > 20: 

219 return "medium" 

220 

221 if len(categories) > 4: 

222 return "medium" 

223 

224 return "low" 

225 

226 

227def _generate_fix_suggestions( 

228 categories: dict[str, list[t.Any]], 

229) -> list[dict[str, str]]: 

230 suggestions = [] 

231 

232 if categories.get("formatting_issues"): 

233 suggestions.append( 

234 { 

235 "category": "formatting", 

236 "action": "Run code formatting", 

237 "command": "python - m crackerjack - - skip-tests", 

238 "description": "Fix formatting and style issues", 

239 } 

240 ) 

241 

242 if categories.get("type_errors"): 

243 suggestions.append( 

244 { 

245 "category": "types", 

246 "action": "Fix type annotations", 

247 "command": "python - m crackerjack - - ai-agent", 

248 "description": "Add missing type hints and resolve type conflicts", 

249 } 

250 ) 

251 

252 if categories.get("test_failures"): 

253 suggestions.append( 

254 { 

255 "category": "tests", 

256 "action": "Fix test failures", 

257 "command": "python - m crackerjack - t - - ai-agent", 

258 "description": "Run tests with AI auto-fixing enabled", 

259 } 

260 ) 

261 

262 if categories.get("security_issues"): 

263 suggestions.append( 

264 { 

265 "category": "security", 

266 "action": "Address security issues", 

267 "command": "python - m crackerjack - - ai - agent-t", 

268 "description": "Fix security vulnerabilities with AI assistance", 

269 } 

270 ) 

271 

272 if len(categories) > 3: 

273 suggestions.append( 

274 { 

275 "category": "comprehensive", 

276 "action": "Full quality check with AI fixing", 

277 "command": "python - m crackerjack - - ai - agent-t", 

278 "description": "Comprehensive quality check with autonomous fixing", 

279 } 

280 ) 

281 

282 return suggestions