patchpilot-mcp 0.4.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.
- patchpilot/__init__.py +3 -0
- patchpilot/__main__.py +246 -0
- patchpilot/analysis.py +20 -0
- patchpilot/changed.py +464 -0
- patchpilot/common.py +75 -0
- patchpilot/compact.py +292 -0
- patchpilot/config.py +217 -0
- patchpilot/diff.py +581 -0
- patchpilot/external_sarif.py +219 -0
- patchpilot/formatting.py +175 -0
- patchpilot/js_review.py +253 -0
- patchpilot/json_review.py +177 -0
- patchpilot/python_review.py +316 -0
- patchpilot/python_rules.py +216 -0
- patchpilot/sarif.py +59 -0
- patchpilot/server.py +274 -0
- patchpilot/signatures.py +224 -0
- patchpilot/snapshot.py +572 -0
- patchpilot_mcp-0.4.1.dist-info/METADATA +381 -0
- patchpilot_mcp-0.4.1.dist-info/RECORD +24 -0
- patchpilot_mcp-0.4.1.dist-info/WHEEL +5 -0
- patchpilot_mcp-0.4.1.dist-info/entry_points.txt +3 -0
- patchpilot_mcp-0.4.1.dist-info/licenses/LICENSE +21 -0
- patchpilot_mcp-0.4.1.dist-info/top_level.txt +1 -0
patchpilot/compact.py
ADDED
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
"""Compact projections of explicit review results; never imply omitted data is clean."""
|
|
2
|
+
|
|
3
|
+
from .analysis import InputError, MAX_FINDINGS, MAX_FILES
|
|
4
|
+
from .snapshot import normalize_finding
|
|
5
|
+
|
|
6
|
+
UNKNOWN_STATUS = {
|
|
7
|
+
"unknown_missing_source",
|
|
8
|
+
"unknown_empty_input",
|
|
9
|
+
"checked_incomplete_findings",
|
|
10
|
+
"unknown_unsupported_language",
|
|
11
|
+
"unknown_input_limit",
|
|
12
|
+
"unknown_parser_unavailable",
|
|
13
|
+
"unknown_digest_only",
|
|
14
|
+
"context_mismatch",
|
|
15
|
+
"not_checked",
|
|
16
|
+
"not_rechecked",
|
|
17
|
+
}
|
|
18
|
+
ALLOWED_STATUS = UNKNOWN_STATUS | {
|
|
19
|
+
"checked_complete",
|
|
20
|
+
"checked_fragment",
|
|
21
|
+
"removed_no_post_source",
|
|
22
|
+
"grammar_checked",
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def _count(value):
|
|
27
|
+
if type(value) is not int or not 0 <= value <= 1000000:
|
|
28
|
+
raise InputError("Invalid bounded report count")
|
|
29
|
+
return value
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _nullable_bool(value):
|
|
33
|
+
if value is not None and type(value) is not bool:
|
|
34
|
+
raise InputError("Invalid report boolean")
|
|
35
|
+
return value
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _metadata(report):
|
|
39
|
+
original_count = _count(report.get("original_finding_count", 0))
|
|
40
|
+
omitted_policy = _count(report.get("omitted_policy_diagnostics", 0))
|
|
41
|
+
policy = report.get("policy_diagnostics", [])
|
|
42
|
+
if (
|
|
43
|
+
not isinstance(policy, list)
|
|
44
|
+
or len(policy) > 6000
|
|
45
|
+
or any(not isinstance(x, dict) for x in policy)
|
|
46
|
+
):
|
|
47
|
+
raise InputError("Invalid policy diagnostics")
|
|
48
|
+
_nullable_bool(report.get("coverage_complete"))
|
|
49
|
+
_nullable_bool(report.get("syntax_ok"))
|
|
50
|
+
return original_count, omitted_policy, policy
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def _display_suppression(row, original, field):
|
|
54
|
+
suppression = original.get("suppression")
|
|
55
|
+
if (
|
|
56
|
+
field == "suppressed_findings"
|
|
57
|
+
and isinstance(suppression, dict)
|
|
58
|
+
and isinstance(suppression.get("kind"), str)
|
|
59
|
+
and suppression.get("kind") in {"inline", "config"}
|
|
60
|
+
and isinstance(suppression.get("reason"), str)
|
|
61
|
+
and len(suppression["reason"]) <= 200
|
|
62
|
+
):
|
|
63
|
+
row["suppression"] = dict(
|
|
64
|
+
kind=suppression["kind"], reason=suppression["reason"]
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def _display(f, original, field):
|
|
69
|
+
row = {
|
|
70
|
+
k: f[k]
|
|
71
|
+
for k in (
|
|
72
|
+
"id",
|
|
73
|
+
"code",
|
|
74
|
+
"severity",
|
|
75
|
+
"evidence",
|
|
76
|
+
"path",
|
|
77
|
+
"line",
|
|
78
|
+
"recommendation",
|
|
79
|
+
)
|
|
80
|
+
}
|
|
81
|
+
if "column" in f:
|
|
82
|
+
row["column"] = f["column"]
|
|
83
|
+
for key in ("state", "change_scope"):
|
|
84
|
+
if isinstance(original.get(key), str) and original.get(key) in {
|
|
85
|
+
"current",
|
|
86
|
+
"carried_unknown",
|
|
87
|
+
"carried_unchanged",
|
|
88
|
+
"file_syntax_error",
|
|
89
|
+
"changed_line",
|
|
90
|
+
"preexisting_verified",
|
|
91
|
+
"outside_changed_lines_unverified",
|
|
92
|
+
"outside_changed_lines_newly_observed",
|
|
93
|
+
"outside_changed_lines",
|
|
94
|
+
}:
|
|
95
|
+
row[key] = original[key]
|
|
96
|
+
_display_suppression(row, original, field)
|
|
97
|
+
return row
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def _normalized_group(raw):
|
|
101
|
+
normalized = []
|
|
102
|
+
counts = {"error": 0, "warning": 0, "info": 0}
|
|
103
|
+
for item in raw:
|
|
104
|
+
f = normalize_finding(item)
|
|
105
|
+
counts[f["severity"]] += 1
|
|
106
|
+
normalized.append((f, item))
|
|
107
|
+
normalized.sort(
|
|
108
|
+
key=lambda pair: (
|
|
109
|
+
{"error": 0, "warning": 1, "info": 2}[pair[0]["severity"]],
|
|
110
|
+
pair[0]["path"],
|
|
111
|
+
pair[0]["line"] or 0,
|
|
112
|
+
pair[0]["id"],
|
|
113
|
+
)
|
|
114
|
+
)
|
|
115
|
+
return normalized, counts
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def _group(report, field, max_findings):
|
|
119
|
+
raw = report.get(field, [])
|
|
120
|
+
if not isinstance(raw, list) or len(raw) > MAX_FINDINGS:
|
|
121
|
+
raise InputError("Finding groups exceed supported bounds")
|
|
122
|
+
omitted_name = {
|
|
123
|
+
"findings": "omitted_findings",
|
|
124
|
+
"open_findings": "omitted_open_findings",
|
|
125
|
+
"outside_changed_findings": "omitted_outside_findings",
|
|
126
|
+
"suppressed_findings": "omitted_suppressed_findings",
|
|
127
|
+
}[field]
|
|
128
|
+
omitted = report.get(omitted_name, 0)
|
|
129
|
+
if type(omitted) is not int or not 0 <= omitted <= 100000:
|
|
130
|
+
raise InputError("Invalid omitted-finding count")
|
|
131
|
+
normalized, counts = _normalized_group(raw)
|
|
132
|
+
shown = []
|
|
133
|
+
for f, original in normalized[:max_findings]:
|
|
134
|
+
shown.append(_display(f, original, field))
|
|
135
|
+
hidden = normalized[max_findings:]
|
|
136
|
+
group = dict(
|
|
137
|
+
counts=counts,
|
|
138
|
+
shown=shown,
|
|
139
|
+
hidden_known=len(hidden),
|
|
140
|
+
hidden_errors=sum(f["severity"] == "error" for f, _ in hidden),
|
|
141
|
+
hidden_warnings=sum(f["severity"] == "warning" for f, _ in hidden),
|
|
142
|
+
source_omitted_unknown_severity=omitted,
|
|
143
|
+
)
|
|
144
|
+
return group, [f for f, _ in normalized]
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def _baseline_state(f):
|
|
148
|
+
baseline = f.get("baseline_coverage")
|
|
149
|
+
if baseline is not None and (
|
|
150
|
+
not isinstance(baseline, str)
|
|
151
|
+
or baseline not in ALLOWED_STATUS | {"not_supplied", "unknown_or_incomplete"}
|
|
152
|
+
):
|
|
153
|
+
raise InputError("Invalid baseline coverage state")
|
|
154
|
+
return baseline
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def _coverage_row(f):
|
|
158
|
+
if not isinstance(f, dict):
|
|
159
|
+
raise InputError("Invalid coverage record")
|
|
160
|
+
path = f.get("path")
|
|
161
|
+
if not isinstance(path, str) or len(path) > 1024:
|
|
162
|
+
raise InputError("Invalid coverage path")
|
|
163
|
+
state = f.get("coverage", "not_checked")
|
|
164
|
+
if state is None:
|
|
165
|
+
state = "not_checked"
|
|
166
|
+
if not isinstance(state, str) or state not in ALLOWED_STATUS:
|
|
167
|
+
raise InputError("Unsupported coverage state")
|
|
168
|
+
baseline = _baseline_state(f)
|
|
169
|
+
return dict(
|
|
170
|
+
path=path,
|
|
171
|
+
coverage=state,
|
|
172
|
+
syntax_ok=_nullable_bool(f.get("syntax_ok")),
|
|
173
|
+
context_validated=_nullable_bool(f.get("context_validated")),
|
|
174
|
+
baseline_coverage=baseline,
|
|
175
|
+
)
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
def _coverage_unknown(item):
|
|
179
|
+
return (
|
|
180
|
+
item["coverage"] in UNKNOWN_STATUS
|
|
181
|
+
or item["coverage"] == "checked_fragment"
|
|
182
|
+
or item.get("baseline_coverage")
|
|
183
|
+
in {"context_mismatch", "unknown_or_incomplete", "unknown_input_limit"}
|
|
184
|
+
or item.get("context_validated") is False
|
|
185
|
+
)
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def _single_source_coverage(report, files, coverage, unknown):
|
|
189
|
+
if not files and "coverage" in report:
|
|
190
|
+
item = _coverage_row(
|
|
191
|
+
dict(
|
|
192
|
+
path=report.get("path", ""),
|
|
193
|
+
coverage=report["coverage"],
|
|
194
|
+
syntax_ok=report.get("syntax_ok"),
|
|
195
|
+
)
|
|
196
|
+
)
|
|
197
|
+
# Keep the prior source-only output shape.
|
|
198
|
+
item = {k: item[k] for k in ("path", "coverage", "syntax_ok")}
|
|
199
|
+
coverage.append(item)
|
|
200
|
+
if _coverage_unknown(item):
|
|
201
|
+
unknown.append(item["path"])
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def _coverage(report, original_count):
|
|
205
|
+
files = report.get("files", [])
|
|
206
|
+
if not isinstance(files, list) or len(files) > 2 * MAX_FILES:
|
|
207
|
+
raise InputError("Invalid coverage records")
|
|
208
|
+
coverage, unknown = [], []
|
|
209
|
+
for f in files:
|
|
210
|
+
item = _coverage_row(f)
|
|
211
|
+
coverage.append(item)
|
|
212
|
+
if f.get("original_finding_count") is not None:
|
|
213
|
+
original_count = max(original_count, _count(f["original_finding_count"]))
|
|
214
|
+
if _coverage_unknown(item):
|
|
215
|
+
unknown.append(item["path"])
|
|
216
|
+
_single_source_coverage(report, files, coverage, unknown)
|
|
217
|
+
return coverage, unknown, original_count
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
def _action(severe, incomplete, rows):
|
|
221
|
+
if severe:
|
|
222
|
+
return "Address reported errors first; then rerun the same bounded review."
|
|
223
|
+
if incomplete:
|
|
224
|
+
return "Resolve unknown or omitted coverage using explicit matching source and smaller review scopes."
|
|
225
|
+
if rows:
|
|
226
|
+
return "Review the highest-priority observations in context and run focused project tests."
|
|
227
|
+
return "No active observations in the supplied bounded scope; verify coverage and run the relevant existing tests."
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
def _reported_error(report, rows, coverage):
|
|
231
|
+
return (
|
|
232
|
+
any(f["severity"] == "error" for f in rows)
|
|
233
|
+
or any(f.get("syntax_ok") is False for f in coverage)
|
|
234
|
+
or report.get("syntax_ok") is False
|
|
235
|
+
)
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
def _report_status(
|
|
239
|
+
report, groups, rows, coverage, unknown, original_count, omitted_policy
|
|
240
|
+
):
|
|
241
|
+
omitted = sum(g["source_omitted_unknown_severity"] for g in groups.values())
|
|
242
|
+
incomplete = bool(
|
|
243
|
+
unknown
|
|
244
|
+
or omitted
|
|
245
|
+
or report.get("coverage_complete") is False
|
|
246
|
+
or original_count >= MAX_FINDINGS
|
|
247
|
+
or omitted_policy
|
|
248
|
+
)
|
|
249
|
+
severe = _reported_error(report, rows, coverage)
|
|
250
|
+
return incomplete, severe
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
def compact_report(report: dict, max_findings: int = 20) -> dict:
|
|
254
|
+
if (
|
|
255
|
+
not isinstance(report, dict)
|
|
256
|
+
or type(max_findings) is not int
|
|
257
|
+
or not 1 <= max_findings <= 50
|
|
258
|
+
):
|
|
259
|
+
raise InputError("Expected a review report and max_findings from 1 to 50")
|
|
260
|
+
|
|
261
|
+
original_count, omitted_policy, policy = _metadata(report)
|
|
262
|
+
groups, rows = {}, []
|
|
263
|
+
for field in (
|
|
264
|
+
"findings",
|
|
265
|
+
"open_findings",
|
|
266
|
+
"outside_changed_findings",
|
|
267
|
+
"suppressed_findings",
|
|
268
|
+
):
|
|
269
|
+
group, findings = _group(report, field, max_findings)
|
|
270
|
+
groups[field] = group
|
|
271
|
+
if field != "suppressed_findings":
|
|
272
|
+
rows.extend(findings)
|
|
273
|
+
coverage, unknown, original_count = _coverage(report, original_count)
|
|
274
|
+
incomplete, severe = _report_status(
|
|
275
|
+
report, groups, rows, coverage, unknown, original_count, omitted_policy
|
|
276
|
+
)
|
|
277
|
+
next_action = _action(severe, incomplete, rows)
|
|
278
|
+
return dict(
|
|
279
|
+
report_kind="compact_review",
|
|
280
|
+
finding_groups=groups,
|
|
281
|
+
coverage=coverage,
|
|
282
|
+
unknown_coverage_paths=unknown,
|
|
283
|
+
coverage_incomplete=incomplete,
|
|
284
|
+
has_reported_errors=severe,
|
|
285
|
+
next_action=next_action,
|
|
286
|
+
policy_diagnostic_count=len(policy),
|
|
287
|
+
omitted_policy_diagnostics=omitted_policy,
|
|
288
|
+
limitations=[
|
|
289
|
+
"Compact output is a projection; retain the full report and manifest for subsequent incremental review.",
|
|
290
|
+
"Known hidden errors/warnings and unknown-severity source omissions are counted separately; no clean status is inferred.",
|
|
291
|
+
],
|
|
292
|
+
)
|
patchpilot/config.py
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
"""Explicit TOML review policy and documented inline heuristic suppressions."""
|
|
2
|
+
|
|
3
|
+
import fnmatch
|
|
4
|
+
import io
|
|
5
|
+
import re
|
|
6
|
+
import tokenize
|
|
7
|
+
from dataclasses import dataclass
|
|
8
|
+
from .analysis import InputError, bounded
|
|
9
|
+
|
|
10
|
+
HEURISTIC_RULES = frozenset(
|
|
11
|
+
{
|
|
12
|
+
"DUPLICATE_DEFINITION",
|
|
13
|
+
"UNREACHABLE_STATEMENT",
|
|
14
|
+
"BRANCH_COMPLEXITY",
|
|
15
|
+
"POSSIBLY_UNUSED_IMPORT",
|
|
16
|
+
"MUTABLE_DEFAULT",
|
|
17
|
+
"BARE_EXCEPT",
|
|
18
|
+
"BUILTIN_SHADOWING",
|
|
19
|
+
"POSSIBLY_UNUSED_LOCAL",
|
|
20
|
+
"JS_DUPLICATE_LEXICAL",
|
|
21
|
+
"JS_UNREACHABLE_STATEMENT",
|
|
22
|
+
"NO_TEST_PATH",
|
|
23
|
+
}
|
|
24
|
+
)
|
|
25
|
+
FACT_RULES = frozenset(
|
|
26
|
+
{
|
|
27
|
+
"TRAILING_WHITESPACE",
|
|
28
|
+
"CHANGE_BUDGET",
|
|
29
|
+
"DUPLICATE_LITERAL_KEY",
|
|
30
|
+
"JSON_DUPLICATE_KEY",
|
|
31
|
+
}
|
|
32
|
+
)
|
|
33
|
+
PARSE_RULES = frozenset(
|
|
34
|
+
{"PYTHON_SYNTAX", "JSON_SYNTAX", "JS_TS_SYNTAX", "STATIC_SYNTAX"}
|
|
35
|
+
)
|
|
36
|
+
DIRECTIVE = re.compile(
|
|
37
|
+
r'^patchpilot:\s*ignore=([A-Z_]+)\s+reason="([^"\r\n]{3,200})"\s*$'
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
@dataclass(frozen=True)
|
|
42
|
+
class Policy:
|
|
43
|
+
threshold: int
|
|
44
|
+
rules: dict
|
|
45
|
+
scoped: tuple[tuple[str, dict], ...]
|
|
46
|
+
|
|
47
|
+
def for_path(self, path):
|
|
48
|
+
settings = dict(self.rules)
|
|
49
|
+
threshold = self.threshold
|
|
50
|
+
for pattern, scope in self.scoped:
|
|
51
|
+
if fnmatch.fnmatchcase(path, pattern):
|
|
52
|
+
settings.update(scope.get("rules", {}))
|
|
53
|
+
threshold = scope.get("complexity_threshold", threshold)
|
|
54
|
+
return threshold, settings
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def _rules(value):
|
|
58
|
+
if not isinstance(value, dict) or len(value) > 50:
|
|
59
|
+
raise InputError("rules must be a bounded TOML table")
|
|
60
|
+
result = {}
|
|
61
|
+
for code, severity in value.items():
|
|
62
|
+
if code not in HEURISTIC_RULES | FACT_RULES:
|
|
63
|
+
raise InputError(f"Unknown or immutable rule: {code}")
|
|
64
|
+
allowed = {"info", "warning", "error"}
|
|
65
|
+
if code in HEURISTIC_RULES:
|
|
66
|
+
allowed.add("off")
|
|
67
|
+
if not isinstance(severity, str) or severity not in allowed:
|
|
68
|
+
raise InputError(f"Invalid severity for {code}")
|
|
69
|
+
result[code] = severity
|
|
70
|
+
return result
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def _threshold(value):
|
|
74
|
+
if type(value) is not int or not 2 <= value <= 50:
|
|
75
|
+
raise InputError("complexity_threshold must be 2–50")
|
|
76
|
+
return value
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def _load_policy(config_toml):
|
|
80
|
+
if not isinstance(config_toml, str):
|
|
81
|
+
raise InputError("Config must be explicit TOML text")
|
|
82
|
+
bounded(config_toml, name="config")
|
|
83
|
+
if len(config_toml.encode("utf-8")) > 16384:
|
|
84
|
+
raise InputError("Config exceeds 16 KiB")
|
|
85
|
+
try:
|
|
86
|
+
try:
|
|
87
|
+
import tomllib
|
|
88
|
+
except ImportError:
|
|
89
|
+
import tomli as tomllib
|
|
90
|
+
data = tomllib.loads(config_toml)
|
|
91
|
+
except (ValueError, RecursionError) as exc:
|
|
92
|
+
raise InputError("Invalid TOML review config") from exc
|
|
93
|
+
return data
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def _parse_paths(data):
|
|
97
|
+
paths = data.get("path_rules", {})
|
|
98
|
+
if not isinstance(paths, dict) or len(paths) > 50:
|
|
99
|
+
raise InputError("At most 50 path scopes")
|
|
100
|
+
scoped = []
|
|
101
|
+
for pattern, scope in paths.items():
|
|
102
|
+
if (
|
|
103
|
+
not isinstance(pattern, str)
|
|
104
|
+
or not 1 <= len(pattern) <= 200
|
|
105
|
+
or not isinstance(scope, dict)
|
|
106
|
+
or set(scope) - {"rules", "complexity_threshold"}
|
|
107
|
+
):
|
|
108
|
+
raise InputError("Invalid path-rule scope")
|
|
109
|
+
parsed = {"rules": _rules(scope.get("rules", {}))}
|
|
110
|
+
if "complexity_threshold" in scope:
|
|
111
|
+
parsed["complexity_threshold"] = _threshold(scope["complexity_threshold"])
|
|
112
|
+
scoped.append((pattern, parsed))
|
|
113
|
+
return tuple(scoped)
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def parse_policy(config_toml=None, threshold=10) -> Policy:
|
|
117
|
+
threshold = _threshold(threshold)
|
|
118
|
+
if config_toml is None:
|
|
119
|
+
return Policy(threshold, {}, ())
|
|
120
|
+
data = _load_policy(config_toml)
|
|
121
|
+
if set(data) - {"review", "rules", "path_rules"}:
|
|
122
|
+
raise InputError("Unexpected TOML section")
|
|
123
|
+
review = data.get("review", {})
|
|
124
|
+
if not isinstance(review, dict) or set(review) - {"complexity_threshold"}:
|
|
125
|
+
raise InputError("Invalid review configuration")
|
|
126
|
+
threshold = _threshold(review.get("complexity_threshold", threshold))
|
|
127
|
+
rules = _rules(data.get("rules", {}))
|
|
128
|
+
return Policy(threshold, rules, _parse_paths(data))
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def python_comments(source, start_line):
|
|
132
|
+
comments = {}
|
|
133
|
+
try:
|
|
134
|
+
for item in tokenize.generate_tokens(io.StringIO(source).readline):
|
|
135
|
+
if item.type == tokenize.COMMENT:
|
|
136
|
+
comments.setdefault(item.start[0] + start_line - 1, []).append(
|
|
137
|
+
item.string
|
|
138
|
+
)
|
|
139
|
+
except (tokenize.TokenError, SyntaxError, IndentationError):
|
|
140
|
+
pass
|
|
141
|
+
return comments
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def _ignore_directives(comments):
|
|
145
|
+
directives = {}
|
|
146
|
+
diagnostics = []
|
|
147
|
+
for line, values in comments.items():
|
|
148
|
+
for comment in values:
|
|
149
|
+
text = (
|
|
150
|
+
comment.removeprefix("#")
|
|
151
|
+
.removeprefix("//")
|
|
152
|
+
.removeprefix("/*")
|
|
153
|
+
.removesuffix("*/")
|
|
154
|
+
.strip()
|
|
155
|
+
)
|
|
156
|
+
if not text.startswith("patchpilot:"):
|
|
157
|
+
continue
|
|
158
|
+
match = DIRECTIVE.fullmatch(text)
|
|
159
|
+
if (
|
|
160
|
+
match is None
|
|
161
|
+
or match.group(1) not in HEURISTIC_RULES
|
|
162
|
+
or not match.group(2).strip()
|
|
163
|
+
):
|
|
164
|
+
diagnostics.append(
|
|
165
|
+
dict(
|
|
166
|
+
line=line,
|
|
167
|
+
code="INVALID_IGNORE_DIRECTIVE",
|
|
168
|
+
reason='Use patchpilot: ignore=HEURISTIC_RULE reason="explanation". Parse and factual findings cannot be suppressed.',
|
|
169
|
+
)
|
|
170
|
+
)
|
|
171
|
+
continue
|
|
172
|
+
directives[(line, match.group(1))] = match.group(2)
|
|
173
|
+
return directives, diagnostics
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def _eligible(finding):
|
|
177
|
+
return finding["code"] in HEURISTIC_RULES and finding.get("evidence") == "heuristic"
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
def apply_policy(findings, comments, settings):
|
|
181
|
+
directives, diagnostics = _ignore_directives(comments)
|
|
182
|
+
suppressed, active = [], []
|
|
183
|
+
matched = set()
|
|
184
|
+
for value in findings:
|
|
185
|
+
f = dict(value)
|
|
186
|
+
code = f["code"]
|
|
187
|
+
configured = settings.get(code)
|
|
188
|
+
key = (f.get("line"), code)
|
|
189
|
+
eligible = _eligible(f)
|
|
190
|
+
if eligible and key in directives:
|
|
191
|
+
matched.add(key)
|
|
192
|
+
suppressed.append(
|
|
193
|
+
dict(f, suppression=dict(kind="inline", reason=directives[key]))
|
|
194
|
+
)
|
|
195
|
+
elif configured == "off" and eligible:
|
|
196
|
+
suppressed.append(
|
|
197
|
+
dict(
|
|
198
|
+
f,
|
|
199
|
+
suppression=dict(
|
|
200
|
+
kind="config", reason="Explicit supplied rule policy is off."
|
|
201
|
+
),
|
|
202
|
+
)
|
|
203
|
+
)
|
|
204
|
+
else:
|
|
205
|
+
if configured in {"info", "warning", "error"} and code not in PARSE_RULES:
|
|
206
|
+
f["original_severity"] = f["severity"]
|
|
207
|
+
f["severity"] = configured
|
|
208
|
+
active.append(f)
|
|
209
|
+
for key in directives.keys() - matched:
|
|
210
|
+
diagnostics.append(
|
|
211
|
+
dict(
|
|
212
|
+
line=key[0],
|
|
213
|
+
code="UNMATCHED_IGNORE_DIRECTIVE",
|
|
214
|
+
reason="No matching eligible heuristic finding on this line in the supplied review.",
|
|
215
|
+
)
|
|
216
|
+
)
|
|
217
|
+
return active, suppressed, sorted(diagnostics, key=lambda x: (x["line"], x["code"]))
|