-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharchitecture_agent.py
More file actions
288 lines (238 loc) · 11.3 KB
/
architecture_agent.py
File metadata and controls
288 lines (238 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Architecture Analysis Agent for Java Codebases.
This is a lightweight wrapper that orchestrates the architecture analysis
workflow using the refactored analysis modules.
"""
from pathlib import Path
from typing import Optional, Any
from config import REPO_ROOT
from utils.logging_config import get_logger
logger = get_logger(__name__)
class ArchitectureAgent:
"""
Agent-like workflow for architecture analysis and recommendations.
Workflow:
1. Analyze dependencies using static analysis
2. Detect architecture issues
3. Generate recommendations
4. Optionally enrich with RAG context
"""
def __init__(
self,
repo_root: Path = REPO_ROOT,
rag_pipeline: Optional[Any] = None
) -> None:
"""
Initialize the architecture agent.
Args:
repo_root: Path to the repository root
rag_pipeline: Optional RAG pipeline for context enrichment
"""
self.repo_root = repo_root
self.rag_pipeline = rag_pipeline
self._analyzer: Optional[Any] = None
self._issues: list[Any] = []
self._recommendations: list[Any] = []
def analyze(self) -> dict[str, Any]:
"""
Run the full architecture analysis workflow.
Returns:
Dict with summary, issues, recommendations, and metrics
"""
from analysis.dependency_analyzer import DependencyAnalyzer
from analysis.issue_detector import ArchitectureIssueDetector
from analysis.refactoring_advisor import RefactoringAdvisor
logger.info("=" * 60)
logger.info("ARCHITECTURE ANALYSIS")
logger.info("=" * 60)
# Step 1: Dependency Analysis
logger.info("Step 1/3: Analyzing dependencies...")
self._analyzer = DependencyAnalyzer(self.repo_root)
classes = self._analyzer.analyze()
# Step 2: Issue Detection
logger.info("Step 2/3: Detecting architecture issues...")
detector = ArchitectureIssueDetector(self._analyzer)
self._issues = detector.detect_all_issues()
logger.info(f"Found {len(self._issues)} potential issues")
# Step 3: Generate Recommendations
logger.info("Step 3/3: Generating recommendations...")
advisor = RefactoringAdvisor(self._issues, self._analyzer)
self._recommendations = advisor.generate_recommendations()
logger.info(f"Generated {len(self._recommendations)} recommendations")
# Compute metrics
metrics = self._compute_metrics(classes)
return {
"summary": self._generate_summary(metrics),
"issues": self._issues,
"recommendations": self._recommendations,
"metrics": metrics
}
def _compute_metrics(self, classes: dict[str, Any]) -> dict[str, Any]:
"""Compute overall codebase metrics."""
if not classes:
return {}
total_loc = sum(c.lines_of_code for c in classes.values())
total_methods = sum(c.method_count for c in classes.values())
packages = set(c.package for c in classes.values())
avg_deps = sum(
len(c.internal_deps) + len(c.external_deps)
for c in classes.values()
) / len(classes)
return {
"total_classes": len(classes),
"total_packages": len(packages),
"total_lines_of_code": total_loc,
"total_methods": total_methods,
"avg_loc_per_class": total_loc / len(classes),
"avg_methods_per_class": total_methods / len(classes),
"avg_dependencies_per_class": avg_deps
}
def _generate_summary(self, metrics: dict[str, Any]) -> str:
"""Generate a human-readable summary."""
high_issues = len([i for i in self._issues if i.severity == "high"])
medium_issues = len([i for i in self._issues if i.severity == "medium"])
low_issues = len([i for i in self._issues if i.severity == "low"])
return f"""
Analyzed {metrics.get('total_classes', 0)} classes across {metrics.get('total_packages', 0)} packages.
CODEBASE METRICS:
- Total lines of code: {metrics.get('total_lines_of_code', 0):,}
- Average LOC per class: {metrics.get('avg_loc_per_class', 0):.1f}
- Average methods per class: {metrics.get('avg_methods_per_class', 0):.1f}
- Average dependencies per class: {metrics.get('avg_dependencies_per_class', 0):.1f}
ISSUES FOUND:
- High severity: {high_issues}
- Medium severity: {medium_issues}
- Low severity: {low_issues}
RECOMMENDATIONS: {len(self._recommendations)} actionable suggestions generated.
""".strip()
def print_report(
self,
result: dict[str, Any],
max_issues: int = 5,
max_recommendations: int = 5
) -> None:
"""Print a formatted report of the analysis."""
print("\n" + "=" * 60)
print("ARCHITECTURE ANALYSIS REPORT")
print("=" * 60)
print("\n" + result["summary"])
if result["issues"]:
print("\n" + "-" * 60)
print(f"TOP ISSUES (showing {min(max_issues, len(result['issues']))} of {len(result['issues'])})")
print("-" * 60)
sorted_issues = sorted(
result["issues"],
key=lambda i: {"high": 0, "medium": 1, "low": 2}[i.severity]
)
for i, issue in enumerate(sorted_issues[:max_issues], 1):
print(f"\n{i}. [{issue.severity.upper()}] {issue.title}")
print(f" {issue.description}")
files_str = ', '.join(issue.affected_files[:3])
if len(issue.affected_files) > 3:
files_str += '...'
print(f" Files: {files_str}")
if issue.issue_type == "cyclic_dependency" and issue.evidence.get("critical_imports"):
print(" Critical imports creating the cycle:")
for ci in issue.evidence["critical_imports"]:
print(f" → {ci['file']}:L{ci['line']} - import {ci['import']}")
if issue.issue_type == "oversized_module":
loc = issue.evidence.get("lines_of_code", 0)
methods = issue.evidence.get("method_count", 0)
print(f" Metrics: {loc:,} total lines, {methods} methods")
if result["recommendations"]:
print("\n" + "-" * 60)
print(f"TOP RECOMMENDATIONS (showing {min(max_recommendations, len(result['recommendations']))} of {len(result['recommendations'])})")
print("-" * 60)
# Separate critical (high severity) from improvement (medium/low) recommendations
critical_recs = [r for r in result["recommendations"] if getattr(r, 'source_severity', 'medium') == 'high']
improvement_recs = [r for r in result["recommendations"] if getattr(r, 'source_severity', 'medium') != 'high']
rec_num = 1
shown = 0
# Print critical recommendations first
if critical_recs:
print("\n🔴 CRITICAL: Address these first to resolve major architecture issues")
print("-" * 40)
for rec in critical_recs:
if shown >= max_recommendations:
break
print(f"\n{rec_num}. [CRITICAL] {rec.title}")
print(f" {rec.description}")
print(f" Rationale: {rec.rationale}")
print(f" Effort: {rec.effort}")
print(" Impact:")
for attr, impact in rec.quality_impact.items():
print(f" - {attr}: {impact}")
if rec.concrete_examples:
print(" Concrete Examples:")
for example in rec.concrete_examples:
print(f" • {example}")
rec_num += 1
shown += 1
# Separator after critical issues
if improvement_recs and shown < max_recommendations:
print("\n" + "=" * 60)
print("✅ Critical issues addressed above.")
print(" The following are GOOD PRACTICES for further improvement:")
print("=" * 60)
# Print improvement recommendations
for rec in improvement_recs:
if shown >= max_recommendations:
break
severity_tag = getattr(rec, 'source_severity', 'medium').upper()
print(f"\n{rec_num}. [{severity_tag}] {rec.title}")
print(f" {rec.description}")
print(f" Rationale: {rec.rationale}")
print(f" Effort: {rec.effort}")
print(" Impact:")
for attr, impact in rec.quality_impact.items():
print(f" - {attr}: {impact}")
if rec.concrete_examples:
print(" Concrete Examples:")
for example in rec.concrete_examples:
print(f" • {example}")
rec_num += 1
shown += 1
print("\n" + "=" * 60)
def enrich_with_rag(self, issue: Any) -> Optional[str]:
"""Use RAG to get additional context about an issue."""
if not self.rag_pipeline:
return None
query = f"What is the purpose of {issue.affected_files[0] if issue.affected_files else 'this class'}?"
result = self.rag_pipeline.query(query, top_k=3)
if not result.uncertainty:
return result.answer
return None
def analyze_architecture(
repo_root: Path = REPO_ROOT,
rag_pipeline: Optional[Any] = None,
max_issues: int = 50,
max_recommendations: int = 15
) -> dict[str, Any]:
"""
Convenience function to run architecture analysis.
Args:
repo_root: Path to the repository root
rag_pipeline: Optional RAG pipeline for context enrichment
max_issues: Maximum number of issues to display
max_recommendations: Maximum number of recommendations to display
Returns:
Analysis results dictionary
"""
agent = ArchitectureAgent(repo_root, rag_pipeline)
result = agent.analyze()
agent.print_report(result, max_issues=max_issues, max_recommendations=max_recommendations)
return result