patchproof-core 0.3.1__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 (56) hide show
  1. patchproof/__init__.py +44 -0
  2. patchproof/__main__.py +8 -0
  3. patchproof/benchmarks/__init__.py +17 -0
  4. patchproof/benchmarks/swe_bench.py +159 -0
  5. patchproof/cli.py +1254 -0
  6. patchproof/core/__init__.py +33 -0
  7. patchproof/core/agent_init.py +103 -0
  8. patchproof/core/anti_cheat.py +396 -0
  9. patchproof/core/applier.py +159 -0
  10. patchproof/core/ast_analyzer.py +254 -0
  11. patchproof/core/detector.py +78 -0
  12. patchproof/core/git_provider.py +298 -0
  13. patchproof/core/healer.py +104 -0
  14. patchproof/core/mutator.py +223 -0
  15. patchproof/core/normalizer.py +338 -0
  16. patchproof/core/polyglot_guard.py +167 -0
  17. patchproof/core/runtime_guard.py +188 -0
  18. patchproof/core/search_replace.py +222 -0
  19. patchproof/core/slicer.py +172 -0
  20. patchproof/core/sparse_tree.py +95 -0
  21. patchproof/docker/runner.Dockerfile +42 -0
  22. patchproof/exceptions.py +37 -0
  23. patchproof/factory/__init__.py +22 -0
  24. patchproof/factory/blast_radius.py +120 -0
  25. patchproof/factory/engine.py +134 -0
  26. patchproof/factory/models.py +75 -0
  27. patchproof/mcp.py +532 -0
  28. patchproof/models/__init__.py +57 -0
  29. patchproof/models/ast.py +78 -0
  30. patchproof/models/patch.py +106 -0
  31. patchproof/models/sandbox.py +417 -0
  32. patchproof/reporting/__init__.py +19 -0
  33. patchproof/reporting/asymmetric.py +88 -0
  34. patchproof/reporting/badge.py +86 -0
  35. patchproof/reporting/certificate.py +230 -0
  36. patchproof/reporting/diagnostic.py +330 -0
  37. patchproof/reporting/html_report.py +367 -0
  38. patchproof/reporting/in_toto.py +188 -0
  39. patchproof/reporting/markdown.py +179 -0
  40. patchproof/reporting/sbom.py +152 -0
  41. patchproof/sandbox/__init__.py +37 -0
  42. patchproof/sandbox/classifier.py +289 -0
  43. patchproof/sandbox/comparative.py +478 -0
  44. patchproof/sandbox/docker_cli.py +384 -0
  45. patchproof/sandbox/doctor.py +605 -0
  46. patchproof/sandbox/flakiness.py +87 -0
  47. patchproof/sandbox/network_bridge.py +145 -0
  48. patchproof/sandbox/parser.py +172 -0
  49. patchproof/sandbox/runner.py +424 -0
  50. patchproof/sandbox/setup_runner.py +261 -0
  51. patchproof_core-0.3.1.dist-info/METADATA +212 -0
  52. patchproof_core-0.3.1.dist-info/RECORD +56 -0
  53. patchproof_core-0.3.1.dist-info/WHEEL +5 -0
  54. patchproof_core-0.3.1.dist-info/entry_points.txt +2 -0
  55. patchproof_core-0.3.1.dist-info/licenses/LICENSE +13 -0
  56. patchproof_core-0.3.1.dist-info/top_level.txt +1 -0
@@ -0,0 +1,289 @@
1
+ """Baseline-versus-patched regression classification and empirical verdict synthesis."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from patchproof.core.runtime_guard import (
6
+ extract_runtime_profile,
7
+ reconcile_runtime_profiles,
8
+ )
9
+ from patchproof.models.sandbox import (
10
+ ComparativeExecutionResult,
11
+ ComparativeTestCase,
12
+ ExecutionResult,
13
+ PatchVerdict,
14
+ RegressionClassification,
15
+ SandboxFailureReason,
16
+ TestCaseResult,
17
+ TestOutcome,
18
+ )
19
+
20
+
21
+ class RegressionClassifier:
22
+ """Reconciles test run reports and failure signals between baseline and patched executions."""
23
+
24
+ def classify(
25
+ self,
26
+ baseline_result: ExecutionResult,
27
+ patched_result: ExecutionResult,
28
+ ) -> ComparativeExecutionResult:
29
+ """Compare baseline and patched test results to produce an immutable comparative report."""
30
+ base_report = baseline_result.test_report
31
+ patch_report = patched_result.test_report
32
+
33
+ # 1. Check for infrastructure failures in baseline
34
+ if (
35
+ baseline_result.failure_reason
36
+ in (
37
+ SandboxFailureReason.TIMEOUT,
38
+ SandboxFailureReason.OOM_KILLED,
39
+ SandboxFailureReason.DOCKER_UNAVAILABLE,
40
+ SandboxFailureReason.UNSUPPORTED_ENGINE,
41
+ )
42
+ or (base_report is not None and base_report.collection_failed)
43
+ or base_report is None
44
+ ):
45
+ summary = self._summarize_failure(
46
+ "Baseline execution failed",
47
+ baseline_result,
48
+ base_report.collection_error if base_report else None,
49
+ )
50
+ return ComparativeExecutionResult(
51
+ baseline_result=baseline_result,
52
+ patched_result=patched_result,
53
+ verdict=PatchVerdict.INFRASTRUCTURE_FAILURE,
54
+ summary=summary,
55
+ )
56
+
57
+ # 2. Check for patched run hard infrastructure/lifecycle failures
58
+ if patched_result.failure_reason == SandboxFailureReason.TIMEOUT:
59
+ return ComparativeExecutionResult(
60
+ baseline_result=baseline_result,
61
+ patched_result=patched_result,
62
+ verdict=PatchVerdict.REJECTED,
63
+ summary="Patched execution exceeded host timeout quota",
64
+ )
65
+
66
+ if patched_result.failure_reason == SandboxFailureReason.OOM_KILLED:
67
+ return ComparativeExecutionResult(
68
+ baseline_result=baseline_result,
69
+ patched_result=patched_result,
70
+ verdict=PatchVerdict.REJECTED,
71
+ summary="Patched execution was terminated by Linux cgroup OOM killer",
72
+ )
73
+
74
+ if patched_result.failure_reason == SandboxFailureReason.SETUP_ERROR:
75
+ err_msg = patched_result.stderr or "Setup error"
76
+ return ComparativeExecutionResult(
77
+ baseline_result=baseline_result,
78
+ patched_result=patched_result,
79
+ verdict=PatchVerdict.REJECTED,
80
+ summary=f"Patched environment setup failed: {err_msg}",
81
+ )
82
+
83
+ if patch_report is not None and patch_report.collection_failed:
84
+ coll_err = patch_report.collection_error or "Collection error"
85
+ return ComparativeExecutionResult(
86
+ baseline_result=baseline_result,
87
+ patched_result=patched_result,
88
+ verdict=PatchVerdict.REJECTED,
89
+ collection_failure_occurred=True,
90
+ summary=f"Patched test collection failed: {coll_err}",
91
+ )
92
+
93
+ if patch_report is None:
94
+ return ComparativeExecutionResult(
95
+ baseline_result=baseline_result,
96
+ patched_result=patched_result,
97
+ verdict=PatchVerdict.REJECTED,
98
+ summary=f"Patched run terminated abnormally (exit {patched_result.exit_code})",
99
+ )
100
+
101
+ # 3. Reconcile individual test cases
102
+ base_map: dict[str, TestCaseResult] = base_report.test_map
103
+ patch_map: dict[str, TestCaseResult] = patch_report.test_map
104
+ all_test_names = sorted(set(base_map.keys()) | set(patch_map.keys()))
105
+
106
+ comparisons: list[ComparativeTestCase] = []
107
+ introduced_regressions = 0
108
+ resolved_defects = 0
109
+ pre_existing_failures = 0
110
+ suppressed_tests = 0
111
+ removed_tests = 0
112
+
113
+ for name in all_test_names:
114
+ b_case = base_map.get(name)
115
+ p_case = patch_map.get(name)
116
+
117
+ b_outcome = b_case.outcome if b_case else None
118
+ p_outcome = p_case.outcome if p_case else None
119
+
120
+ classification, msg = self._classify_transition(b_outcome, p_outcome)
121
+
122
+ if classification == RegressionClassification.INTRODUCED_REGRESSION:
123
+ introduced_regressions += 1
124
+ elif classification == RegressionClassification.RESOLVED_DEFECT:
125
+ resolved_defects += 1
126
+ elif classification == RegressionClassification.PRE_EXISTING_FAILURE:
127
+ pre_existing_failures += 1
128
+ elif classification == RegressionClassification.SUPPRESSION_ALERT:
129
+ suppressed_tests += 1
130
+ elif classification == RegressionClassification.REMOVED_TEST:
131
+ removed_tests += 1
132
+
133
+ comparisons.append(
134
+ ComparativeTestCase(
135
+ test_name=name,
136
+ baseline_outcome=b_outcome,
137
+ patched_outcome=p_outcome,
138
+ classification=classification,
139
+ message=msg,
140
+ )
141
+ )
142
+
143
+ # 4. Dynamic runtime profile reconciliation
144
+ base_profile = extract_runtime_profile(baseline_result.stdout)
145
+ patch_profile = extract_runtime_profile(patched_result.stdout)
146
+ dynamic_tampered, dynamic_reason = reconcile_runtime_profiles(base_profile, patch_profile)
147
+
148
+ # 5. Formulate empirical verdict
149
+ if introduced_regressions > 0:
150
+ verdict = PatchVerdict.REJECTED
151
+ summary = f"Patch rejected: introduced {introduced_regressions} regression(s)"
152
+ elif dynamic_tampered:
153
+ verdict = PatchVerdict.SUSPICIOUS
154
+ summary = f"Patch flagged suspicious: {dynamic_reason}"
155
+ elif removed_tests > 0:
156
+ verdict = PatchVerdict.SUSPICIOUS
157
+ summary = (
158
+ f"Patch flagged suspicious: {removed_tests} previously existing test(s) "
159
+ "were removed or uncollected"
160
+ )
161
+ elif suppressed_tests > 0:
162
+ verdict = PatchVerdict.SUSPICIOUS
163
+ summary = (
164
+ f"Patch flagged suspicious: {suppressed_tests} previously passing test(s) "
165
+ "were suppressed"
166
+ )
167
+ elif pre_existing_failures > 0 and patched_result.exit_code != 0:
168
+ verdict = PatchVerdict.SUSPICIOUS
169
+ summary = (
170
+ f"Patch left {pre_existing_failures} pre-existing failure(s) unresolved "
171
+ f"({resolved_defects} defect(s) resolved)"
172
+ )
173
+ elif len(all_test_names) == 0:
174
+ verdict = PatchVerdict.INCONCLUSIVE
175
+ summary = "No test cases were discovered in either baseline or patched execution"
176
+ elif (
177
+ patched_result.test_report is not None
178
+ and patched_result.test_report.passed_count == 0
179
+ and len(all_test_names) > 0
180
+ ):
181
+ verdict = PatchVerdict.INCONCLUSIVE
182
+ summary = "No tests passed in patched execution; patch cannot be verified as ACCEPTED"
183
+ else:
184
+ verdict = PatchVerdict.ACCEPTED
185
+ summary = (
186
+ f"Patch verified clean: {len(comparisons)} test(s) preserved, "
187
+ f"{resolved_defects} defect(s) resolved, 0 regressions"
188
+ )
189
+
190
+ return ComparativeExecutionResult(
191
+ baseline_result=baseline_result,
192
+ patched_result=patched_result,
193
+ verdict=verdict,
194
+ test_comparisons=tuple(comparisons),
195
+ introduced_regressions_count=introduced_regressions,
196
+ resolved_defects_count=resolved_defects,
197
+ pre_existing_failures_count=pre_existing_failures,
198
+ suppressed_tests_count=suppressed_tests,
199
+ removed_tests_count=removed_tests,
200
+ test_tampering_detected=dynamic_tampered,
201
+ tampering_details=(dynamic_reason,) if dynamic_reason else (),
202
+ collection_failure_occurred=False,
203
+ summary=summary,
204
+ )
205
+
206
+ def _classify_transition(
207
+ self,
208
+ b_outcome: TestOutcome | None,
209
+ p_outcome: TestOutcome | None,
210
+ ) -> tuple[RegressionClassification, str | None]:
211
+ """Classify the outcome shift for a single test case."""
212
+ # 1. New test added
213
+ if b_outcome is None:
214
+ if p_outcome == TestOutcome.PASS:
215
+ return RegressionClassification.NEW_TEST_PASSING, "New test passing"
216
+ if p_outcome in (TestOutcome.FAIL, TestOutcome.ERROR):
217
+ return RegressionClassification.NEW_TEST_FAILING, "New test failing"
218
+ return RegressionClassification.NEW_TEST_SKIPPED, "New test skipped"
219
+
220
+ # 2. Existing test removed
221
+ if p_outcome is None:
222
+ return (
223
+ RegressionClassification.REMOVED_TEST,
224
+ "Test removed or uncollected in patched code",
225
+ )
226
+
227
+ # 3. Clean preservation
228
+ if b_outcome == TestOutcome.PASS and p_outcome == TestOutcome.PASS:
229
+ return RegressionClassification.CLEAN_PRESERVATION, None
230
+
231
+ # 4. Introduced regression
232
+ if b_outcome == TestOutcome.PASS and p_outcome in (TestOutcome.FAIL, TestOutcome.ERROR):
233
+ return (
234
+ RegressionClassification.INTRODUCED_REGRESSION,
235
+ f"Regression: shifted from {b_outcome.value} to {p_outcome.value}",
236
+ )
237
+
238
+ # 5. Resolved defect
239
+ if b_outcome in (TestOutcome.FAIL, TestOutcome.ERROR) and p_outcome == TestOutcome.PASS:
240
+ return (
241
+ RegressionClassification.RESOLVED_DEFECT,
242
+ f"Resolved: shifted from {b_outcome.value} to {p_outcome.value}",
243
+ )
244
+
245
+ # 6. Pre-existing failure
246
+ if b_outcome in (TestOutcome.FAIL, TestOutcome.ERROR) and p_outcome in (
247
+ TestOutcome.FAIL,
248
+ TestOutcome.ERROR,
249
+ ):
250
+ return (
251
+ RegressionClassification.PRE_EXISTING_FAILURE,
252
+ "Pre-existing failure unchanged",
253
+ )
254
+
255
+ # 7. Suppression alert
256
+ if b_outcome == TestOutcome.PASS and p_outcome == TestOutcome.SKIPPED:
257
+ return (
258
+ RegressionClassification.SUPPRESSION_ALERT,
259
+ "Suppression: passing test became skipped in patched code",
260
+ )
261
+
262
+ # 8. Pre-existing skip
263
+ if b_outcome == TestOutcome.SKIPPED and p_outcome == TestOutcome.SKIPPED:
264
+ return RegressionClassification.PRE_EXISTING_SKIP, None
265
+
266
+ # Fallback
267
+ if p_outcome in (TestOutcome.FAIL, TestOutcome.ERROR):
268
+ return (
269
+ RegressionClassification.INTRODUCED_REGRESSION,
270
+ f"Shifted from {b_outcome.value} to {p_outcome.value}",
271
+ )
272
+
273
+ return RegressionClassification.CLEAN_PRESERVATION, None
274
+
275
+ def _summarize_failure(
276
+ self,
277
+ prefix: str,
278
+ res: ExecutionResult,
279
+ extra_err: str | None = None,
280
+ ) -> str:
281
+ """Construct human-readable diagnostic failure summary."""
282
+ parts = [prefix]
283
+ if res.failure_reason != SandboxFailureReason.NONE:
284
+ parts.append(f"reason={res.failure_reason.value}")
285
+ if res.exit_code != 0:
286
+ parts.append(f"exit_code={res.exit_code}")
287
+ if extra_err:
288
+ parts.append(f"error={extra_err[:80]}...")
289
+ return ": ".join(parts)