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
patchproof/__init__.py ADDED
@@ -0,0 +1,44 @@
1
+ """PatchProof - Deterministic Code-Patch Verification & Reliability Engine."""
2
+
3
+ from patchproof.core.ast_analyzer import (
4
+ analyze_patch_ast,
5
+ analyze_python_source,
6
+ extract_delta_line_ranges,
7
+ )
8
+ from patchproof.core.normalizer import parse_patch
9
+ from patchproof.core.search_replace import parse_search_replace
10
+ from patchproof.exceptions import InvalidPatchFormatError, PatchProofError
11
+ from patchproof.models.ast import (
12
+ ASTAnalysisReport,
13
+ ASTEntity,
14
+ EntityType,
15
+ FileASTReport,
16
+ )
17
+ from patchproof.models.patch import (
18
+ CanonicalPatch,
19
+ ChangeType,
20
+ FileDelta,
21
+ PatchHunk,
22
+ PatchType,
23
+ )
24
+
25
+ __version__ = "0.3.1"
26
+
27
+ __all__ = [
28
+ "ASTAnalysisReport",
29
+ "ASTEntity",
30
+ "CanonicalPatch",
31
+ "ChangeType",
32
+ "EntityType",
33
+ "FileASTReport",
34
+ "FileDelta",
35
+ "InvalidPatchFormatError",
36
+ "PatchHunk",
37
+ "PatchProofError",
38
+ "PatchType",
39
+ "analyze_patch_ast",
40
+ "analyze_python_source",
41
+ "extract_delta_line_ranges",
42
+ "parse_patch",
43
+ "parse_search_replace",
44
+ ]
patchproof/__main__.py ADDED
@@ -0,0 +1,8 @@
1
+ """Entrypoint for python -m patchproof."""
2
+
3
+ import sys
4
+
5
+ from patchproof.cli import main
6
+
7
+ if __name__ == "__main__":
8
+ sys.exit(main())
@@ -0,0 +1,17 @@
1
+ """SWE-bench and Agent Benchmark Evaluation Subsystem for PatchProof."""
2
+
3
+ from patchproof.benchmarks.swe_bench import (
4
+ SWEBenchAggregateReport,
5
+ SWEBenchAuditResult,
6
+ SWEBenchTaskPrediction,
7
+ audit_swe_bench_predictions,
8
+ export_swe_bench_register_csv,
9
+ )
10
+
11
+ __all__ = [
12
+ "SWEBenchAggregateReport",
13
+ "SWEBenchAuditResult",
14
+ "SWEBenchTaskPrediction",
15
+ "audit_swe_bench_predictions",
16
+ "export_swe_bench_register_csv",
17
+ ]
@@ -0,0 +1,159 @@
1
+ """SWE-bench Anti-Cheat Benchmark Evaluation Engine.
2
+
3
+ Audits candidate AI patches on benchmark tasks (e.g. SWE-bench Lite / Verified)
4
+ to identify test tampering, assertion stripping, decorator subversion, and cheating.
5
+ """
6
+
7
+ from __future__ import annotations
8
+
9
+ import csv
10
+ import pathlib
11
+ from dataclasses import asdict, dataclass
12
+ from typing import Any
13
+
14
+ from patchproof.core.anti_cheat import audit_patch_integrity
15
+ from patchproof.core.normalizer import parse_patch
16
+
17
+
18
+ @dataclass(frozen=True)
19
+ class SWEBenchTaskPrediction:
20
+ """Represents an agent prediction entry for a SWE-bench instance."""
21
+
22
+ instance_id: str
23
+ model_name: str
24
+ patch: str
25
+
26
+
27
+ @dataclass(frozen=True)
28
+ class SWEBenchAuditResult:
29
+ """Audit outcome for a single SWE-bench prediction."""
30
+
31
+ instance_id: str
32
+ model_name: str
33
+ tampering_detected: bool
34
+ tampering_violations: tuple[str, ...]
35
+ tests_deleted_count: int
36
+ tests_suppressed_count: int
37
+ assertion_delta: int
38
+ is_honest: bool
39
+
40
+
41
+ @dataclass(frozen=True)
42
+ class SWEBenchAggregateReport:
43
+ """Aggregate benchmark evaluation results across all audited tasks."""
44
+
45
+ total_tasks: int
46
+ honest_count: int
47
+ tampered_count: int
48
+ cheat_rate_pct: float
49
+ task_results: tuple[SWEBenchAuditResult, ...]
50
+
51
+ def to_dict(self) -> dict[str, Any]:
52
+ """Convert report to serializable dictionary."""
53
+ return {
54
+ "total_tasks": self.total_tasks,
55
+ "honest_count": self.honest_count,
56
+ "tampered_count": self.tampered_count,
57
+ "cheat_rate_pct": self.cheat_rate_pct,
58
+ "task_results": [asdict(t) for t in self.task_results],
59
+ }
60
+
61
+
62
+ def audit_swe_bench_predictions(
63
+ predictions: list[dict[str, Any]],
64
+ repo_root: pathlib.Path | None = None,
65
+ ) -> SWEBenchAggregateReport:
66
+ """Audit a list of SWE-bench predictions for test tampering and agent cheating.
67
+
68
+ Args:
69
+ predictions: List of dicts with 'instance_id', 'patch', and optional 'model_name'.
70
+ repo_root: Optional baseline repository root directory.
71
+
72
+ Returns:
73
+ SWEBenchAggregateReport: Summary of honest vs tampered predictions.
74
+ """
75
+ root = repo_root or pathlib.Path.cwd()
76
+ task_results: list[SWEBenchAuditResult] = []
77
+
78
+ for pred in predictions:
79
+ inst_id = str(pred.get("instance_id", "unknown"))
80
+ model = str(pred.get("model_name", "agent"))
81
+ patch_str = str(pred.get("patch", ""))
82
+
83
+ try:
84
+ canonical = parse_patch(patch_str)
85
+ audit = audit_patch_integrity(canonical, repo_root=root)
86
+ tampered = audit.tampering_detected
87
+ violations = tuple(v.details for v in audit.violations)
88
+ deleted = audit.tests_deleted_count
89
+ suppressed = audit.tests_suppressed_count
90
+ assert_delta = audit.assertion_delta
91
+ except Exception as err:
92
+ tampered = True
93
+ violations = (f"Failed to parse or audit patch: {err}",)
94
+ deleted = 0
95
+ suppressed = 0
96
+ assert_delta = 0
97
+
98
+ is_honest = not tampered
99
+
100
+ task_results.append(
101
+ SWEBenchAuditResult(
102
+ instance_id=inst_id,
103
+ model_name=model,
104
+ tampering_detected=tampered,
105
+ tampering_violations=violations,
106
+ tests_deleted_count=deleted,
107
+ tests_suppressed_count=suppressed,
108
+ assertion_delta=assert_delta,
109
+ is_honest=is_honest,
110
+ )
111
+ )
112
+
113
+ total = len(task_results)
114
+ honest = sum(1 for t in task_results if t.is_honest)
115
+ tampered_cnt = total - honest
116
+ cheat_rate = (tampered_cnt / total * 100.0) if total > 0 else 0.0
117
+
118
+ return SWEBenchAggregateReport(
119
+ total_tasks=total,
120
+ honest_count=honest,
121
+ tampered_count=tampered_cnt,
122
+ cheat_rate_pct=cheat_rate,
123
+ task_results=tuple(task_results),
124
+ )
125
+
126
+
127
+ def export_swe_bench_register_csv(
128
+ report: SWEBenchAggregateReport,
129
+ out_path: pathlib.Path,
130
+ ) -> None:
131
+ """Export SWE-bench audit results to standardized CSV compliance register."""
132
+ fieldnames = [
133
+ "instance_id",
134
+ "model_name",
135
+ "verdict",
136
+ "tampering_detected",
137
+ "tests_deleted_count",
138
+ "tests_suppressed_count",
139
+ "assertion_delta",
140
+ "violations",
141
+ ]
142
+
143
+ out_path.parent.mkdir(parents=True, exist_ok=True)
144
+ with out_path.open("w", newline="", encoding="utf-8") as f:
145
+ writer = csv.DictWriter(f, fieldnames=fieldnames)
146
+ writer.writeheader()
147
+ for t in report.task_results:
148
+ writer.writerow(
149
+ {
150
+ "instance_id": t.instance_id,
151
+ "model_name": t.model_name,
152
+ "verdict": "HONEST" if t.is_honest else "TAMPERED",
153
+ "tampering_detected": str(t.tampering_detected),
154
+ "tests_deleted_count": t.tests_deleted_count,
155
+ "tests_suppressed_count": t.tests_suppressed_count,
156
+ "assertion_delta": t.assertion_delta,
157
+ "violations": " | ".join(t.tampering_violations),
158
+ }
159
+ )