-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdeduplication.py
More file actions
238 lines (197 loc) · 8.1 KB
/
deduplication.py
File metadata and controls
238 lines (197 loc) · 8.1 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
"""Deduplication logic for tracking and comparing conflict states across runs."""
import json
import os
from datetime import datetime, timedelta, timezone
from typing import Any
from conflict_detector import ConflictResult
from fingerprint import ( # re-exported for consumers
ConflictFingerprint,
DeduplicationResult,
PRDisplayInfo,
conflict_to_fingerprint,
dict_to_fingerprint,
fingerprint_to_dict,
)
__all__ = [
# Re-exported fingerprint types for backward compatibility
"ConflictFingerprint",
"DeduplicationResult",
"PRDisplayInfo",
"conflict_to_fingerprint",
"dict_to_fingerprint",
"fingerprint_to_dict",
# Core deduplication functions
"compare_conflicts",
"load_state",
"prune_expired_conflicts",
"save_state",
"update_state_with_current",
]
RESOLVED_MAX_AGE_DAYS = 7
MAX_RESOLVED_PER_PR = 10
STATE_FILE = ".pr-conflict-state.json"
EXPIRY_DAYS = 42
def load_state(repo_path: str = ".") -> dict[str, Any]:
"""Load the conflict state file from the repository.
Args:
repo_path: Path to the repository root.
Returns:
Dictionary with 'conflicts' list, or empty structure if file doesn't exist.
"""
state_path = os.path.join(repo_path, STATE_FILE)
if not os.path.exists(state_path):
return {"conflicts": []}
try:
with open(state_path, "r", encoding="utf-8") as f:
return json.load(f)
except (json.JSONDecodeError, OSError) as e:
print(f"Warning: Failed to load state file: {e}")
return {"conflicts": []}
def save_state(state: dict[str, Any], repo_path: str = ".") -> None:
"""Save the conflict state file to the repository using atomic writes.
Writes to a temporary file first, then atomically renames it to the
target path. This prevents partial/corrupt state files if the process
is interrupted mid-write (e.g., job cancellation, timeout).
Args:
state: State dictionary with 'conflicts' list.
repo_path: Path to the repository root.
"""
state_path = os.path.join(repo_path, STATE_FILE)
tmp_path = state_path + ".tmp"
try:
with open(tmp_path, "w", encoding="utf-8") as f:
json.dump(state, f, indent=2)
os.replace(tmp_path, state_path) # atomic on POSIX
except OSError as e:
print(f"Warning: Failed to save state file: {e}")
# Clean up partial temp file if it exists
try:
os.remove(tmp_path)
except OSError:
# Best-effort cleanup; safe to ignore if temp file is already gone
pass
def prune_expired_conflicts(
state: dict[str, Any], expiry_days: int = EXPIRY_DAYS
) -> dict[str, Any]:
"""Remove conflicts older than the expiry threshold.
Args:
state: State dictionary with 'conflicts' list.
expiry_days: Number of days after which to expire active conflicts.
Returns:
Updated state with expired conflicts removed.
"""
cutoff = datetime.now(timezone.utc) - timedelta(days=expiry_days)
pruned_conflicts = []
for conflict in state.get("conflicts", []):
try:
first_seen = datetime.fromisoformat(conflict["first_seen"])
if first_seen.tzinfo is None:
first_seen = first_seen.replace(tzinfo=timezone.utc)
if first_seen >= cutoff:
pruned_conflicts.append(conflict)
except (ValueError, KeyError):
# Skip malformed entries
continue
state["conflicts"] = pruned_conflicts
# Prune resolved conflicts older than RESOLVED_MAX_AGE_DAYS
resolved_cutoff = datetime.now(timezone.utc) - timedelta(days=RESOLVED_MAX_AGE_DAYS)
pruned_resolved = []
for resolved in state.get("resolved_conflicts", []):
try:
resolved_at = datetime.fromisoformat(resolved["resolved_at"])
if resolved_at.tzinfo is None:
resolved_at = resolved_at.replace(tzinfo=timezone.utc)
if resolved_at >= resolved_cutoff:
pruned_resolved.append(resolved)
except (ValueError, KeyError):
continue
state["resolved_conflicts"] = pruned_resolved
return state
def compare_conflicts(
current_conflicts: dict[str, list[ConflictResult]], state: dict[str, Any]
) -> DeduplicationResult:
"""Compare current conflicts against historical state.
Args:
current_conflicts: Dict mapping repo name to list of ConflictResult objects.
state: State dictionary with 'conflicts' list of fingerprints.
Returns:
DeduplicationResult categorizing conflicts as new, changed, or unchanged.
"""
result = DeduplicationResult()
# Build lookup of existing fingerprints by (repo, pr_a, pr_b)
existing: dict[tuple[str, int, int], ConflictFingerprint] = {}
for fp_dict in state.get("conflicts", []):
fp = dict_to_fingerprint(fp_dict)
key = (fp.repo, fp.pr_a, fp.pr_b)
existing[key] = fp
# Track which existing conflicts we've seen in current run
seen_keys: set[tuple[str, int, int]] = set()
for repo, conflicts in current_conflicts.items():
for conflict in conflicts:
key = (repo, conflict.pr_a.number, conflict.pr_b.number)
seen_keys.add(key)
if key not in existing:
# New conflict
result.new_conflicts.append(conflict)
else:
# Exists — check if files changed
old_fp = existing[key]
new_files = sorted({fo.filename for fo in conflict.conflicting_files})
if new_files != old_fp.files:
result.changed_conflicts.append(conflict)
else:
result.unchanged_conflicts.append(conflict)
# Find resolved conflicts (in state but not in current)
for key, fp in existing.items():
if key not in seen_keys:
result.resolved_fingerprints.append(fp)
return result
def update_state_with_current(
current_conflicts: dict[str, list[ConflictResult]], state: dict[str, Any]
) -> dict[str, Any]:
"""Update state with current conflicts, preserving timestamps for unchanged.
Also tracks newly resolved conflicts by moving them from the active list
to the resolved list with a resolved_at timestamp.
Args:
current_conflicts: Dict mapping repo name to list of ConflictResult objects.
state: Existing state dictionary.
Returns:
Updated state dictionary with 'conflicts', 'resolved_conflicts',
and 'last_run' timestamp.
"""
# Build lookup of existing timestamps
existing_timestamps: dict[tuple[str, int, int], str] = {}
for fp_dict in state.get("conflicts", []):
fp = dict_to_fingerprint(fp_dict)
key = (fp.repo, fp.pr_a, fp.pr_b)
existing_timestamps[key] = fp.first_seen
# Build new state with current conflicts
new_fingerprints = []
current_keys: set[tuple[str, int, int]] = set()
for repo, conflicts in current_conflicts.items():
for conflict in conflicts:
key = (repo, conflict.pr_a.number, conflict.pr_b.number)
current_keys.add(key)
# Preserve timestamp if conflict existed before, otherwise use current time
timestamp = existing_timestamps.get(key)
fp = conflict_to_fingerprint(conflict, repo, timestamp)
new_fingerprints.append(fingerprint_to_dict(fp))
# Carry forward existing resolved conflicts, excluding any that reappeared
resolved = [
r
for r in state.get("resolved_conflicts", [])
if (r.get("repo", ""), r.get("pr_a"), r.get("pr_b")) not in current_keys
]
# Add newly resolved conflicts (in previous state but not in current)
now = datetime.now(timezone.utc).isoformat()
for fp_dict in state.get("conflicts", []):
fp = dict_to_fingerprint(fp_dict)
key = (fp.repo, fp.pr_a, fp.pr_b)
if key not in current_keys:
fp.resolved_at = now
resolved.append(fingerprint_to_dict(fp))
return {
"conflicts": new_fingerprints,
"resolved_conflicts": resolved,
"last_run": datetime.now(timezone.utc).isoformat(),
}