Coverage for little_loops / issue_history / regressions.py: 84%
68 statements
« prev ^ index » next coverage.py v7.12.0, created at 2026-03-04 01:17 -0600
« prev ^ index » next coverage.py v7.12.0, created at 2026-03-04 01:17 -0600
1"""Issue history regression clustering analysis."""
3from __future__ import annotations
5from pathlib import Path
7from little_loops.issue_history.models import (
8 CompletedIssue,
9 RegressionAnalysis,
10 RegressionCluster,
11)
12from little_loops.issue_history.parsing import _extract_paths_from_issue
15def analyze_regression_clustering(
16 issues: list[CompletedIssue],
17 contents: dict[Path, str] | None = None,
18) -> RegressionAnalysis:
19 """Detect files where bug fixes frequently lead to new bugs.
21 Uses heuristics:
22 1. Temporal proximity: Bug B completed within 7 days of Bug A
23 2. File overlap: Both bugs affect same file(s)
25 Args:
26 issues: List of completed issues
27 contents: Pre-loaded issue file contents (path -> content)
29 Returns:
30 RegressionAnalysis with clusters of related regressions
31 """
32 # Filter to bugs only and sort by completion date
33 bugs = [i for i in issues if i.issue_type == "BUG" and i.completed_date]
34 bugs.sort(key=lambda i: i.completed_date) # type: ignore
36 if len(bugs) < 2:
37 return RegressionAnalysis()
39 # Extract file paths for each bug
40 bug_files: dict[str, set[str]] = {} # issue_id -> set of files
41 for bug in bugs:
42 if contents is not None and bug.path in contents:
43 content = contents[bug.path]
44 paths = _extract_paths_from_issue(content)
45 bug_files[bug.issue_id] = set(paths)
46 else:
47 try:
48 content = bug.path.read_text(encoding="utf-8")
49 paths = _extract_paths_from_issue(content)
50 bug_files[bug.issue_id] = set(paths)
51 except Exception:
52 bug_files[bug.issue_id] = set()
54 # Find regression pairs (temporal proximity + file overlap)
55 regression_pairs: list[tuple[CompletedIssue, CompletedIssue, set[str]]] = []
57 for i, bug_a in enumerate(bugs[:-1]):
58 files_a = bug_files.get(bug_a.issue_id, set())
59 if not files_a:
60 continue
62 for bug_b in bugs[i + 1 :]:
63 # Check temporal proximity (within 7 days)
64 days_apart = (bug_b.completed_date - bug_a.completed_date).days # type: ignore
65 if days_apart > 7:
66 break # Bugs are sorted, no need to check further
68 files_b = bug_files.get(bug_b.issue_id, set())
69 if not files_b:
70 continue
72 # Check file overlap
73 overlap = files_a & files_b
74 if overlap:
75 regression_pairs.append((bug_a, bug_b, overlap))
77 if not regression_pairs:
78 return RegressionAnalysis()
80 # Group by primary file (most common overlapping file)
81 file_regressions: dict[str, list[tuple[str, str, int]]] = {} # file -> [(id_a, id_b, days)]
83 for bug_a, bug_b, overlap in regression_pairs:
84 days = (bug_b.completed_date - bug_a.completed_date).days # type: ignore
85 for file_path in overlap:
86 if file_path not in file_regressions:
87 file_regressions[file_path] = []
88 file_regressions[file_path].append((bug_a.issue_id, bug_b.issue_id, days))
90 # Build clusters
91 clusters: list[RegressionCluster] = []
93 for file_path, pairs in file_regressions.items():
94 # Determine time pattern
95 avg_days = sum(d for _, _, d in pairs) / len(pairs)
96 if avg_days < 3:
97 time_pattern = "immediate"
98 elif len(pairs) >= 3:
99 time_pattern = "chronic"
100 else:
101 time_pattern = "delayed"
103 # Determine severity
104 if len(pairs) >= 4:
105 severity = "critical"
106 elif len(pairs) >= 2:
107 severity = "high"
108 else:
109 severity = "medium"
111 # Collect related files
112 related_files: set[str] = set()
113 for bug_a, bug_b, _ in regression_pairs:
114 if file_path in (
115 bug_files.get(bug_a.issue_id, set()) & bug_files.get(bug_b.issue_id, set())
116 ):
117 related_files.update(bug_files.get(bug_a.issue_id, set()))
118 related_files.update(bug_files.get(bug_b.issue_id, set()))
119 related_files.discard(file_path)
121 clusters.append(
122 RegressionCluster(
123 primary_file=file_path,
124 regression_count=len(pairs),
125 fix_bug_pairs=[(a, b) for a, b, _ in pairs],
126 related_files=sorted(related_files),
127 time_pattern=time_pattern,
128 severity=severity,
129 )
130 )
132 # Sort by regression count descending
133 clusters.sort(key=lambda c: (-c.regression_count, c.primary_file))
135 # Identify most fragile files
136 most_fragile = [c.primary_file for c in clusters[:5]]
138 return RegressionAnalysis(
139 clusters=clusters[:10], # Top 10
140 total_regression_chains=len(regression_pairs),
141 most_fragile_files=most_fragile,
142 )