adre-cli 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. adre/__init__.py +5 -0
  2. adre/analyzers/__init__.py +1 -0
  3. adre/analyzers/architecture.py +253 -0
  4. adre/analyzers/findings.py +457 -0
  5. adre/analyzers/inference.py +163 -0
  6. adre/cli.py +923 -0
  7. adre/core/__init__.py +1 -0
  8. adre/core/accepted.py +225 -0
  9. adre/core/adaptive.py +221 -0
  10. adre/core/baseline.py +1165 -0
  11. adre/core/config.py +60 -0
  12. adre/core/decision_engine.py +361 -0
  13. adre/core/declared.py +313 -0
  14. adre/core/evolution_intelligence.py +390 -0
  15. adre/core/explainer.py +413 -0
  16. adre/core/git_intelligence.py +392 -0
  17. adre/core/github_check.py +212 -0
  18. adre/core/incremental.py +288 -0
  19. adre/core/layers.py +306 -0
  20. adre/core/pr_intelligence.py +261 -0
  21. adre/core/pr_review.py +454 -0
  22. adre/core/prioritization.py +196 -0
  23. adre/core/project.py +41 -0
  24. adre/core/refactor_intelligence.py +201 -0
  25. adre/core/repositories.py +312 -0
  26. adre/core/review.py +96 -0
  27. adre/core/rules_shared.py +75 -0
  28. adre/core/sarif.py +141 -0
  29. adre/core/scope.py +141 -0
  30. adre/core/snapshots.py +242 -0
  31. adre/core/stability_intelligence.py +361 -0
  32. adre/graph/__init__.py +0 -0
  33. adre/graph/dependencies.py +379 -0
  34. adre/parsers/__init__.py +87 -0
  35. adre/parsers/go.py +140 -0
  36. adre/parsers/javascript.py +364 -0
  37. adre/parsers/jvm.py +127 -0
  38. adre/parsers/python.py +153 -0
  39. adre/reports/__init__.py +1 -0
  40. adre/reports/architecture.py +349 -0
  41. adre/rules.py +12 -0
  42. adre/web.py +3731 -0
  43. adre_cli-0.1.0.dist-info/METADATA +343 -0
  44. adre_cli-0.1.0.dist-info/RECORD +48 -0
  45. adre_cli-0.1.0.dist-info/WHEEL +5 -0
  46. adre_cli-0.1.0.dist-info/entry_points.txt +2 -0
  47. adre_cli-0.1.0.dist-info/licenses/LICENSE +202 -0
  48. adre_cli-0.1.0.dist-info/top_level.txt +1 -0
adre/core/explainer.py ADDED
@@ -0,0 +1,413 @@
1
+ import os
2
+ from collections import Counter, defaultdict
3
+
4
+ from adre.analyzers.architecture import analyze_project_architecture
5
+
6
+
7
+ def subsystem_name(file_path):
8
+ parts = file_path.replace("\\", "/").split("/")
9
+ if len(parts) > 1:
10
+ return parts[0]
11
+ return "root"
12
+
13
+
14
+ def human_architecture_type(architecture_type):
15
+ return architecture_type.replace("_", " ") if architecture_type else "unknown architecture"
16
+
17
+
18
+ def risk_level(score):
19
+ if score >= 75:
20
+ return "high"
21
+ if score >= 40:
22
+ return "medium"
23
+ return "low"
24
+
25
+
26
+ def attention_level(score, cycle_participant=False, hotspot=False):
27
+ if cycle_participant or score >= 80:
28
+ return "high"
29
+ if hotspot or score >= 45:
30
+ return "medium"
31
+ return "low"
32
+
33
+
34
+ def build_inbound_map(resolved_graph):
35
+ inbound = defaultdict(set)
36
+ for source, targets in resolved_graph.items():
37
+ for target in targets:
38
+ inbound[target].add(source)
39
+ return inbound
40
+
41
+
42
+ def files_in_cycles(cycles):
43
+ return {file for cycle in cycles for file in cycle if file}
44
+
45
+
46
+ def issue_subsystem_counts(issues):
47
+ counts = Counter()
48
+ for issue in issues:
49
+ file_path = issue.get("file")
50
+ if file_path:
51
+ counts[subsystem_name(file_path)] += 1
52
+ return counts
53
+
54
+
55
+ def subsystem_role(subsystem):
56
+ roles = {
57
+ "routes": "entrypoint and request handling boundary",
58
+ "controllers": "entrypoint and orchestration boundary",
59
+ "services": "business logic and workflow coordination layer",
60
+ "models": "domain data and persistence-facing layer",
61
+ "repositories": "data access layer",
62
+ "schemas": "data contract layer",
63
+ "core": "shared platform and configuration layer",
64
+ "utils": "shared helper layer",
65
+ "root": "top-level module boundary",
66
+ }
67
+ return roles.get(subsystem, "application subsystem")
68
+
69
+
70
+ def describe_subsystem(subsystem, files, issue_count, cycle_count, inbound_count, outbound_count, hotspot_count):
71
+ role = subsystem_role(subsystem)
72
+ pressure = []
73
+ if cycle_count:
74
+ pressure.append("cycle participation")
75
+ if hotspot_count:
76
+ pressure.append("hotspot pressure")
77
+ if issue_count:
78
+ pressure.append("layer boundary violations")
79
+ if inbound_count > outbound_count and inbound_count:
80
+ pressure.append("incoming dependency concentration")
81
+ if outbound_count > inbound_count and outbound_count:
82
+ pressure.append("outgoing dependency reach")
83
+
84
+ if pressure:
85
+ return (
86
+ f"{subsystem}/ is acting as the {role}. Its current pressure comes from "
87
+ f"{', '.join(pressure)}, which makes changes in this area more likely to affect other parts of the system."
88
+ )
89
+
90
+ return (
91
+ f"{subsystem}/ is acting as the {role}. It currently has a relatively quiet architecture signal, "
92
+ "so it is not a primary source of structural risk."
93
+ )
94
+
95
+
96
+ def summarize_architecture(analysis):
97
+ architecture_type = human_architecture_type(analysis.get("architecture_type"))
98
+ confidence = analysis.get("architecture_confidence", 0)
99
+ file_count = len(analysis.get("dependency_graph", {}))
100
+ detected_layers = analysis.get("detected_layers", {})
101
+ if isinstance(detected_layers, dict):
102
+ layer_names = sorted(detected_layers.keys())
103
+ else:
104
+ layer_names = sorted(detected_layers)
105
+ score = analysis.get("score", 0)
106
+
107
+ if layer_names:
108
+ layer_text = ", ".join(layer_names)
109
+ else:
110
+ layer_text = "no strong layer boundaries"
111
+
112
+ return (
113
+ f"This project is detected as a {architecture_type} with {confidence:.2f} confidence. "
114
+ f"The analyzer found {file_count} architecture-relevant files across {layer_text}. "
115
+ f"The current architecture score is {score}/100, which means the system is "
116
+ f"{'healthy' if score >= 80 else 'under architectural pressure' if score >= 50 else 'structurally fragile'}."
117
+ )
118
+
119
+
120
+ def detect_core_patterns(analysis):
121
+ patterns = []
122
+ architecture_type = analysis.get("architecture_type", "unknown")
123
+ detected_layers = analysis.get("detected_layers", {})
124
+ if isinstance(detected_layers, dict):
125
+ layers = sorted(detected_layers.keys())
126
+ else:
127
+ layers = sorted(detected_layers)
128
+
129
+ patterns.append({
130
+ "name": human_architecture_type(architecture_type).title(),
131
+ "explanation": "The codebase is organized around recognizable layer or subsystem boundaries.",
132
+ })
133
+
134
+ if layers:
135
+ patterns.append({
136
+ "name": "Layered Boundaries",
137
+ "explanation": f"Detected boundaries include {', '.join(layers)}.",
138
+ })
139
+
140
+ if analysis.get("cycles"):
141
+ patterns.append({
142
+ "name": "Bidirectional Coupling",
143
+ "explanation": "At least one dependency cycle shows that two or more modules depend on each other directly or indirectly.",
144
+ })
145
+
146
+ if analysis.get("hotspot_files"):
147
+ patterns.append({
148
+ "name": "Centralized Dependency Pressure",
149
+ "explanation": "Some files have become dependency hubs, increasing blast radius when they change.",
150
+ })
151
+
152
+ if analysis.get("indirect_dependencies"):
153
+ patterns.append({
154
+ "name": "Transitive Dependency Chains",
155
+ "explanation": "The system contains multi-step dependency paths where changes can propagate through several modules.",
156
+ })
157
+
158
+ return patterns
159
+
160
+
161
+ def build_subsystem_explanations(analysis):
162
+ dependency_graph = analysis.get("dependency_graph", {})
163
+ resolved_graph = analysis.get("resolved_graph", {})
164
+ inbound = build_inbound_map(resolved_graph)
165
+ cycle_files = files_in_cycles(analysis.get("cycles", []))
166
+ hotspot_files = {item.get("file") for item in analysis.get("hotspot_files", [])}
167
+ issue_counts = issue_subsystem_counts(analysis.get("issues", []))
168
+
169
+ files_by_subsystem = defaultdict(list)
170
+ for file_path in dependency_graph:
171
+ files_by_subsystem[subsystem_name(file_path)].append(file_path)
172
+
173
+ subsystem_rows = []
174
+ for subsystem, files in sorted(files_by_subsystem.items()):
175
+ outbound_count = sum(len(resolved_graph.get(file_path, [])) for file_path in files)
176
+ inbound_count = sum(len(inbound.get(file_path, [])) for file_path in files)
177
+ cycle_count = sum(1 for file_path in files if file_path in cycle_files)
178
+ hotspot_count = sum(1 for file_path in files if file_path in hotspot_files)
179
+ issue_count = issue_counts.get(subsystem, 0)
180
+ subsystem_score = issue_count * 18 + cycle_count * 28 + hotspot_count * 20 + max(0, inbound_count - outbound_count) * 3
181
+
182
+ subsystem_rows.append({
183
+ "subsystem": subsystem,
184
+ "role": subsystem_role(subsystem),
185
+ "file_count": len(files),
186
+ "issue_count": issue_count,
187
+ "cycle_participants": cycle_count,
188
+ "hotspots": hotspot_count,
189
+ "inbound_dependencies": inbound_count,
190
+ "outbound_dependencies": outbound_count,
191
+ "risk_level": risk_level(subsystem_score),
192
+ "story": describe_subsystem(
193
+ subsystem,
194
+ files,
195
+ issue_count,
196
+ cycle_count,
197
+ inbound_count,
198
+ outbound_count,
199
+ hotspot_count,
200
+ ),
201
+ })
202
+
203
+ return sorted(
204
+ subsystem_rows,
205
+ key=lambda item: (
206
+ {"high": 2, "medium": 1, "low": 0}[item["risk_level"]],
207
+ item["issue_count"],
208
+ item["cycle_participants"],
209
+ item["hotspots"],
210
+ item["subsystem"],
211
+ ),
212
+ reverse=True,
213
+ )
214
+
215
+
216
+ def explain_coupling(analysis):
217
+ resolved_graph = analysis.get("resolved_graph", {})
218
+ inbound = build_inbound_map(resolved_graph)
219
+ hotspot_files = analysis.get("hotspot_files", [])
220
+ issues = analysis.get("issues", [])
221
+ cycles = analysis.get("cycles", [])
222
+
223
+ explanations = []
224
+ for hotspot in hotspot_files[:5]:
225
+ file_path = hotspot.get("file")
226
+ explanations.append({
227
+ "type": "hotspot",
228
+ "file": file_path,
229
+ "explanation": (
230
+ f"{file_path} is depended on by {hotspot.get('dependency_count', len(inbound.get(file_path, [])))} file(s). "
231
+ "That makes it a coordination point where unrelated changes may become coupled."
232
+ ),
233
+ })
234
+
235
+ for cycle in cycles[:5]:
236
+ explanations.append({
237
+ "type": "cycle",
238
+ "file": cycle[0] if cycle else "",
239
+ "explanation": (
240
+ f"{' -> '.join(cycle)} forms a loop, so ownership is unclear and import order can become fragile."
241
+ ),
242
+ })
243
+
244
+ for issue in issues[:5]:
245
+ if issue.get("issue_type") == "cycle":
246
+ continue
247
+ explanations.append({
248
+ "type": "layer_violation",
249
+ "file": issue.get("file"),
250
+ "explanation": (
251
+ f"{issue.get('file')} reaches from {issue.get('layer', 'unknown')} into "
252
+ f"{issue.get('imported_layer', 'unknown')}, which weakens the expected boundary."
253
+ ),
254
+ })
255
+
256
+ if not explanations:
257
+ explanations.append({
258
+ "type": "healthy",
259
+ "file": "",
260
+ "explanation": "No major coupling concentration was detected in the current dependency graph.",
261
+ })
262
+
263
+ return explanations
264
+
265
+
266
+ def build_risk_narrative(analysis, subsystems):
267
+ cycles = len(analysis.get("cycles", []))
268
+ hotspots = len(analysis.get("hotspot_files", []))
269
+ severity = analysis.get("severity_counts", {})
270
+ top_subsystem = subsystems[0]["subsystem"] if subsystems else "the project"
271
+
272
+ if cycles:
273
+ return (
274
+ f"The highest architectural risk is circular dependency pressure, concentrated around {top_subsystem}/. "
275
+ "Cycles usually mean responsibilities are flowing in both directions, so fixes should start by extracting "
276
+ "shared contracts or moving orchestration back into the intended layer."
277
+ )
278
+
279
+ if severity.get("critical", 0) or severity.get("high", 0):
280
+ return (
281
+ f"The main risk is boundary erosion around {top_subsystem}/. High-severity layer violations indicate that "
282
+ "modules are bypassing the intended architecture and coupling directly to implementation details."
283
+ )
284
+
285
+ if hotspots:
286
+ return (
287
+ f"The main risk is dependency concentration. {hotspots} hotspot file(s) are collecting inbound dependencies, "
288
+ "which increases blast radius and makes future changes harder to isolate."
289
+ )
290
+
291
+ return "The current architecture has no dominant structural failure mode. Continue monitoring dependency growth and subsystem boundaries."
292
+
293
+
294
+ def build_dependency_narrative(analysis):
295
+ resolved_graph = analysis.get("resolved_graph", {})
296
+ inbound = build_inbound_map(resolved_graph)
297
+ edge_count = sum(len(targets) for targets in resolved_graph.values())
298
+ file_count = max(1, len(resolved_graph))
299
+ average_outbound = edge_count / file_count
300
+ hotspots = analysis.get("hotspot_files", [])
301
+
302
+ if hotspots:
303
+ top = hotspots[0]
304
+ return (
305
+ f"The dependency graph has {edge_count} resolved edge(s), averaging {average_outbound:.1f} outgoing dependencies per file. "
306
+ f"The strongest dependency hub is {top.get('file')}, with {top.get('dependency_count', len(inbound.get(top.get('file'), [])))} inbound dependency signal(s)."
307
+ )
308
+
309
+ return (
310
+ f"The dependency graph has {edge_count} resolved edge(s), averaging {average_outbound:.1f} outgoing dependencies per file. "
311
+ "No single file currently dominates inbound dependency pressure."
312
+ )
313
+
314
+
315
+ def generate_architecture_explanation(project_path):
316
+ analysis = analyze_project_architecture(project_path)
317
+ subsystems = build_subsystem_explanations(analysis)
318
+ coupling = explain_coupling(analysis)
319
+
320
+ return {
321
+ "project_path": project_path,
322
+ "architecture_summary": summarize_architecture(analysis),
323
+ "core_patterns": detect_core_patterns(analysis),
324
+ "key_subsystems": subsystems,
325
+ "subsystem_stories": {
326
+ item["subsystem"]: item["story"]
327
+ for item in subsystems
328
+ },
329
+ "coupling_explanation": coupling,
330
+ "dependency_narrative": build_dependency_narrative(analysis),
331
+ "risk_narrative": build_risk_narrative(analysis, subsystems),
332
+ }
333
+
334
+
335
+ def normalize_file_path(file_path, project_path):
336
+ if not file_path:
337
+ return file_path
338
+ if os.path.isabs(file_path):
339
+ return os.path.relpath(file_path, project_path)
340
+ if file_path.startswith(project_path):
341
+ return os.path.relpath(file_path, project_path)
342
+ return file_path
343
+
344
+
345
+ def explain_path(file_path, project_path=None):
346
+ project_path = project_path or os.getcwd()
347
+ analysis = analyze_project_architecture(project_path)
348
+ target = normalize_file_path(file_path, project_path)
349
+ resolved_graph = analysis.get("resolved_graph", {})
350
+ inbound = build_inbound_map(resolved_graph)
351
+ cycle_files = files_in_cycles(analysis.get("cycles", []))
352
+ hotspot_files = {item.get("file") for item in analysis.get("hotspot_files", [])}
353
+ file_risks = {
354
+ item.get("file"): item
355
+ for item in analysis.get("file_risk_ranking", [])
356
+ }
357
+
358
+ if target not in analysis.get("dependency_graph", {}):
359
+ return {
360
+ "file": target,
361
+ "role": "unknown",
362
+ "dependency_impact": "This file was not found in the analyzed dependency graph.",
363
+ "depends_on": [],
364
+ "depended_on_by": [],
365
+ "risk_contribution": "unknown",
366
+ "recommended_attention_level": "low",
367
+ }
368
+
369
+ depends_on = sorted(resolved_graph.get(target, []))
370
+ depended_on_by = sorted(inbound.get(target, []))
371
+ layer = analysis["dependency_graph"].get(target, {}).get("layer", "unknown")
372
+ subsystem = subsystem_name(target)
373
+ risk = file_risks.get(target, {})
374
+ score = risk.get("risk_score", len(depended_on_by) * 12 + len(depends_on) * 4)
375
+ is_cycle_file = target in cycle_files
376
+ is_hotspot = target in hotspot_files
377
+
378
+ reasons = []
379
+ if is_cycle_file:
380
+ reasons.append("it participates in a dependency cycle")
381
+ if is_hotspot:
382
+ reasons.append("it is a dependency hotspot")
383
+ if depended_on_by:
384
+ reasons.append(f"{len(depended_on_by)} file(s) depend on it")
385
+ if depends_on:
386
+ reasons.append(f"it depends on {len(depends_on)} file(s)")
387
+ if not reasons:
388
+ reasons.append("it has low current dependency pressure")
389
+
390
+ return {
391
+ "file": target,
392
+ "role": f"{target} belongs to the {subsystem}/ subsystem and acts within the {layer} layer.",
393
+ "depends_on": depends_on,
394
+ "depended_on_by": depended_on_by,
395
+ "dependency_impact": (
396
+ f"This file matters architecturally because {', '.join(reasons)}."
397
+ ),
398
+ "risk_contribution": risk_level(score),
399
+ "recommended_attention_level": attention_level(score, cycle_participant=is_cycle_file, hotspot=is_hotspot),
400
+ }
401
+
402
+
403
+ def attach_explanation_context_to_decisions(decision_report, explanation):
404
+ subsystem_stories = explanation.get("subsystem_stories", {})
405
+ for decision in decision_report.get("decisions", []):
406
+ affected_files = decision.get("affected_files", [])
407
+ subsystems = sorted({subsystem_name(file_path) for file_path in affected_files})
408
+ stories = [subsystem_stories[subsystem] for subsystem in subsystems if subsystem in subsystem_stories]
409
+ decision["explanation_context"] = {
410
+ "subsystems": subsystems,
411
+ "why_it_matters": stories[0] if stories else explanation.get("risk_narrative", ""),
412
+ }
413
+ return decision_report