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/snapshot.py
ADDED
|
@@ -0,0 +1,572 @@
|
|
|
1
|
+
"""Caller-supplied manifests and bounded changed-only review. No filesystem access."""
|
|
2
|
+
|
|
3
|
+
import hashlib
|
|
4
|
+
import json
|
|
5
|
+
import re
|
|
6
|
+
from typing import Any
|
|
7
|
+
from .analysis import (
|
|
8
|
+
MAX_FILES,
|
|
9
|
+
MAX_FINDINGS,
|
|
10
|
+
InputError,
|
|
11
|
+
bounded,
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
MAX_BATCH_BYTES = 1024 * 1024
|
|
15
|
+
OPTIONAL_FIELDS = {"digest", "language", "complete", "start_line"}
|
|
16
|
+
DIGEST = re.compile(r"^[0-9a-f]{64}$")
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def infer_language(path: str) -> str:
|
|
20
|
+
suffix = path.lower().rsplit(".", 1)[-1]
|
|
21
|
+
return {
|
|
22
|
+
"py": "python",
|
|
23
|
+
"pyi": "python",
|
|
24
|
+
"json": "json",
|
|
25
|
+
"js": "javascript",
|
|
26
|
+
"jsx": "jsx",
|
|
27
|
+
"mjs": "javascript",
|
|
28
|
+
"cjs": "javascript",
|
|
29
|
+
"ts": "typescript",
|
|
30
|
+
"tsx": "tsx",
|
|
31
|
+
"go": "go",
|
|
32
|
+
"java": "java",
|
|
33
|
+
"cs": "csharp",
|
|
34
|
+
"dart": "dart",
|
|
35
|
+
}.get(suffix, "unknown")
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def hash_source(source: str) -> str:
|
|
39
|
+
return hashlib.sha256(source.encode("utf-8")).hexdigest()
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _integer(value: Any, name: str, low: int, high: int) -> int:
|
|
43
|
+
if type(value) is not int or not low <= value <= high:
|
|
44
|
+
raise InputError(f"{name} must be an integer {low}–{high}")
|
|
45
|
+
return value
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _entry_identity(entry):
|
|
49
|
+
path = entry.get("path")
|
|
50
|
+
if (
|
|
51
|
+
not isinstance(path, str)
|
|
52
|
+
or not 1 <= len(path) <= 1024
|
|
53
|
+
or any(c in path for c in "\r\n\x00")
|
|
54
|
+
):
|
|
55
|
+
raise InputError("Path must be a bounded nonempty label")
|
|
56
|
+
language = entry.get("language", infer_language(path))
|
|
57
|
+
if language not in (
|
|
58
|
+
"python",
|
|
59
|
+
"json",
|
|
60
|
+
"javascript",
|
|
61
|
+
"jsx",
|
|
62
|
+
"typescript",
|
|
63
|
+
"tsx",
|
|
64
|
+
"go",
|
|
65
|
+
"java",
|
|
66
|
+
"csharp",
|
|
67
|
+
"dart",
|
|
68
|
+
"unknown",
|
|
69
|
+
):
|
|
70
|
+
raise InputError("Unsupported language label")
|
|
71
|
+
complete = entry.get("complete", True)
|
|
72
|
+
if type(complete) is not bool:
|
|
73
|
+
raise InputError("complete must be a boolean")
|
|
74
|
+
start = _integer(entry.get("start_line", 1), "start_line", 1, 10**8)
|
|
75
|
+
if complete and start != 1:
|
|
76
|
+
raise InputError("A complete file must start at line 1")
|
|
77
|
+
return path, language, complete, start
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def _entry_content(entry):
|
|
81
|
+
digest = entry.get("digest")
|
|
82
|
+
if digest is not None and (
|
|
83
|
+
not isinstance(digest, str) or not DIGEST.fullmatch(digest)
|
|
84
|
+
):
|
|
85
|
+
raise InputError("digest must be lowercase SHA-256 hex")
|
|
86
|
+
source = entry.get("source")
|
|
87
|
+
if source is not None:
|
|
88
|
+
if not isinstance(source, str):
|
|
89
|
+
raise InputError("source must be text")
|
|
90
|
+
bounded(source, name="source")
|
|
91
|
+
actual = hash_source(source)
|
|
92
|
+
if digest is not None and actual != digest:
|
|
93
|
+
raise InputError("Supplied digest does not match source")
|
|
94
|
+
digest = actual
|
|
95
|
+
if digest is None:
|
|
96
|
+
raise InputError("Each entry requires source text or SHA-256 digest")
|
|
97
|
+
return digest, source
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def _previous_stamp(entry, meta):
|
|
101
|
+
reviewed = entry.get("reviewed", False)
|
|
102
|
+
profile = entry.get("review_profile", "")
|
|
103
|
+
if type(reviewed) is not bool or not isinstance(profile, str) or len(profile) > 64:
|
|
104
|
+
raise InputError("Invalid previous review metadata")
|
|
105
|
+
meta.update(reviewed=reviewed, review_profile=profile)
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def _normalize(
|
|
109
|
+
entry: dict[str, Any], *, previous: bool = False
|
|
110
|
+
) -> tuple[dict, str | None]:
|
|
111
|
+
allowed = (
|
|
112
|
+
{"path", "reviewed", "review_profile", *OPTIONAL_FIELDS}
|
|
113
|
+
if previous
|
|
114
|
+
else {"path", "source", *OPTIONAL_FIELDS}
|
|
115
|
+
)
|
|
116
|
+
if not isinstance(entry, dict) or set(entry) - allowed:
|
|
117
|
+
raise InputError("Unexpected manifest entry field")
|
|
118
|
+
path, language, complete, start = _entry_identity(entry)
|
|
119
|
+
digest, source = _entry_content(entry)
|
|
120
|
+
meta = dict(
|
|
121
|
+
path=path, digest=digest, language=language, complete=complete, start_line=start
|
|
122
|
+
)
|
|
123
|
+
if previous:
|
|
124
|
+
_previous_stamp(entry, meta)
|
|
125
|
+
return meta, source
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def attach_ids(findings: list[dict]) -> list[dict]:
|
|
129
|
+
"""IDs repeat for equal observations at equal source coordinates."""
|
|
130
|
+
unique = {}
|
|
131
|
+
for f in findings:
|
|
132
|
+
components = [f.get(k) for k in ("code", "path", "line", "column", "message")]
|
|
133
|
+
key = hashlib.sha256(
|
|
134
|
+
json.dumps(components, ensure_ascii=True, separators=(",", ":")).encode()
|
|
135
|
+
).hexdigest()[:24]
|
|
136
|
+
unique.setdefault(key, dict(f, id=f"pp-{key}"))
|
|
137
|
+
return list(unique.values())
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def _tests(entry: dict, status: str) -> list[str]:
|
|
141
|
+
if status in ("unchanged", "omitted_unknown"):
|
|
142
|
+
return []
|
|
143
|
+
if status == "removed":
|
|
144
|
+
return [
|
|
145
|
+
"Confirm intentional removal and test callers or consumers that depended on the old behavior."
|
|
146
|
+
]
|
|
147
|
+
if entry["language"] == "json":
|
|
148
|
+
return [
|
|
149
|
+
"Validate applicable schema separately and exercise consumers using changed configurations."
|
|
150
|
+
]
|
|
151
|
+
if entry["language"] == "python":
|
|
152
|
+
return [
|
|
153
|
+
"Run nearby unit tests; cover changed behavior and boundary cases.",
|
|
154
|
+
"Check dependent callers using existing project conventions.",
|
|
155
|
+
]
|
|
156
|
+
return [
|
|
157
|
+
"Use the normal language-specific syntax checker and focus existing tests on changed behavior."
|
|
158
|
+
]
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def _validate_snapshot(
|
|
162
|
+
entries,
|
|
163
|
+
previous_manifest,
|
|
164
|
+
complexity_threshold,
|
|
165
|
+
recheck_unchanged,
|
|
166
|
+
complete_snapshot,
|
|
167
|
+
):
|
|
168
|
+
if (
|
|
169
|
+
not isinstance(entries, list)
|
|
170
|
+
or not isinstance(previous_manifest, list)
|
|
171
|
+
and previous_manifest is not None
|
|
172
|
+
):
|
|
173
|
+
raise InputError("entries and previous_manifest must be lists")
|
|
174
|
+
if len(entries) > MAX_FILES or len(previous_manifest or []) > MAX_FILES:
|
|
175
|
+
raise InputError(f"Each manifest is limited to {MAX_FILES} entries")
|
|
176
|
+
_integer(complexity_threshold, "complexity_threshold", 2, 50)
|
|
177
|
+
if type(recheck_unchanged) is not bool:
|
|
178
|
+
raise InputError("recheck_unchanged must be a boolean")
|
|
179
|
+
if type(complete_snapshot) is not bool:
|
|
180
|
+
raise InputError("complete_snapshot must be a boolean")
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def _prior_findings(previous_findings):
|
|
184
|
+
prior_open = []
|
|
185
|
+
if previous_findings is not None:
|
|
186
|
+
if (
|
|
187
|
+
not isinstance(previous_findings, list)
|
|
188
|
+
or len(previous_findings) > MAX_FINDINGS
|
|
189
|
+
):
|
|
190
|
+
raise InputError(f"previous_findings limited to {MAX_FINDINGS} entries")
|
|
191
|
+
for raw in previous_findings:
|
|
192
|
+
prior_open.append(normalize_finding(raw))
|
|
193
|
+
return prior_open
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
def _prior_manifest(previous_manifest):
|
|
197
|
+
prior = {}
|
|
198
|
+
|
|
199
|
+
for raw in previous_manifest or []:
|
|
200
|
+
meta, _ = _normalize(raw, previous=True)
|
|
201
|
+
if meta["path"] in prior:
|
|
202
|
+
raise InputError("Repeated path in previous_manifest")
|
|
203
|
+
prior[meta["path"]] = meta
|
|
204
|
+
return prior
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
def _batch_entries(entries):
|
|
208
|
+
normalized = {}
|
|
209
|
+
total_bytes = 0
|
|
210
|
+
for raw in entries:
|
|
211
|
+
meta, source = _normalize(raw)
|
|
212
|
+
if meta["path"] in normalized:
|
|
213
|
+
raise InputError("Repeated path in entries")
|
|
214
|
+
if source is not None:
|
|
215
|
+
total_bytes += len(source.encode("utf-8"))
|
|
216
|
+
if total_bytes > MAX_BATCH_BYTES:
|
|
217
|
+
raise InputError(f"Batch source exceeds {MAX_BATCH_BYTES} bytes")
|
|
218
|
+
normalized[meta["path"]] = (meta, source)
|
|
219
|
+
|
|
220
|
+
return normalized
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
def _entry_status(path, meta, prior):
|
|
224
|
+
if path not in prior:
|
|
225
|
+
return "added"
|
|
226
|
+
if all(prior[path].get(k) == v for k, v in meta.items()):
|
|
227
|
+
return "unchanged"
|
|
228
|
+
return "changed"
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
def _review_needed(status, previous_review, profile, recheck_unchanged):
|
|
232
|
+
reviewed = (
|
|
233
|
+
status == "unchanged"
|
|
234
|
+
and previous_review.get("reviewed") is True
|
|
235
|
+
and previous_review.get("review_profile") == profile
|
|
236
|
+
)
|
|
237
|
+
return reviewed, status != "unchanged" or recheck_unchanged or not reviewed
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
def _run_entry_review(meta, source, need_review, config_toml, complexity_threshold):
|
|
241
|
+
from .changed import review_source
|
|
242
|
+
|
|
243
|
+
if not need_review:
|
|
244
|
+
return "not_rechecked", None
|
|
245
|
+
if source is None:
|
|
246
|
+
return "unknown_digest_only", None
|
|
247
|
+
try:
|
|
248
|
+
result = review_source(
|
|
249
|
+
source,
|
|
250
|
+
meta["path"],
|
|
251
|
+
meta["language"],
|
|
252
|
+
config_toml,
|
|
253
|
+
meta["start_line"],
|
|
254
|
+
meta["complete"],
|
|
255
|
+
complexity_threshold,
|
|
256
|
+
)
|
|
257
|
+
return result["coverage"], result
|
|
258
|
+
except InputError:
|
|
259
|
+
return "unknown_input_limit", None
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
def _report_entry(meta, status, outcome, result, need_review):
|
|
263
|
+
return dict(
|
|
264
|
+
meta,
|
|
265
|
+
status=status,
|
|
266
|
+
coverage=outcome,
|
|
267
|
+
syntax_ok=result.get("syntax_ok") if result else None,
|
|
268
|
+
finding_count=len(result["findings"]) if result else None,
|
|
269
|
+
original_finding_count=result.get("original_finding_count") if result else None,
|
|
270
|
+
omitted_source_findings=result.get("omitted_findings", 0) if result else 0,
|
|
271
|
+
test_plan=_tests(
|
|
272
|
+
meta, "changed" if status == "unchanged" and need_review else status
|
|
273
|
+
),
|
|
274
|
+
)
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
def _add_omitted(prior, normalized, complete_snapshot, counts, manifest, report):
|
|
278
|
+
for path, meta in sorted(prior.items()):
|
|
279
|
+
if path in normalized:
|
|
280
|
+
continue
|
|
281
|
+
status = "removed" if complete_snapshot else "omitted_unknown"
|
|
282
|
+
counts[status] += 1
|
|
283
|
+
if status == "omitted_unknown":
|
|
284
|
+
manifest.append(meta)
|
|
285
|
+
report.append(
|
|
286
|
+
dict(
|
|
287
|
+
meta,
|
|
288
|
+
status=status,
|
|
289
|
+
coverage="not_checked",
|
|
290
|
+
syntax_ok=None,
|
|
291
|
+
finding_count=None,
|
|
292
|
+
test_plan=_tests(meta, status),
|
|
293
|
+
)
|
|
294
|
+
)
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
def _retained_state(row):
|
|
298
|
+
if row is None:
|
|
299
|
+
return "carried_unknown"
|
|
300
|
+
if row["status"] == "removed":
|
|
301
|
+
return None
|
|
302
|
+
if (
|
|
303
|
+
row["coverage"] == "checked_complete"
|
|
304
|
+
and (row.get("original_finding_count") or 0) < MAX_FINDINGS
|
|
305
|
+
and row["syntax_ok"] is True
|
|
306
|
+
):
|
|
307
|
+
return None
|
|
308
|
+
if row["coverage"] == "not_rechecked":
|
|
309
|
+
return "carried_unchanged"
|
|
310
|
+
return "carried_unknown"
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
def _ordered_findings(findings):
|
|
314
|
+
order = {"error": 0, "warning": 1, "info": 2}
|
|
315
|
+
return sorted(
|
|
316
|
+
attach_ids(findings),
|
|
317
|
+
key=lambda f: (
|
|
318
|
+
order.get(f["severity"], 3),
|
|
319
|
+
f["path"],
|
|
320
|
+
f["line"] or 0,
|
|
321
|
+
f.get("column", 0),
|
|
322
|
+
f["id"],
|
|
323
|
+
),
|
|
324
|
+
)
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
def _open_findings(prior_open, report, all_findings):
|
|
328
|
+
by_path = {row["path"]: row for row in report}
|
|
329
|
+
still_open = []
|
|
330
|
+
for previous in prior_open:
|
|
331
|
+
state = _retained_state(by_path.get(previous["path"]))
|
|
332
|
+
if state is not None:
|
|
333
|
+
still_open.append(dict(previous, state=state))
|
|
334
|
+
# New observations take precedence over retained caller observations with same stable identity.
|
|
335
|
+
open_by_id = {f["id"]: f for f in attach_ids(still_open)}
|
|
336
|
+
for f in all_findings:
|
|
337
|
+
open_by_id[f["id"]] = dict(f, state="current")
|
|
338
|
+
order = {"error": 0, "warning": 1, "info": 2}
|
|
339
|
+
return sorted(
|
|
340
|
+
open_by_id.values(),
|
|
341
|
+
key=lambda f: (order.get(f["severity"], 3), f["path"], f["line"] or 0, f["id"]),
|
|
342
|
+
)
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
def _current_entries(
|
|
346
|
+
normalized, prior, profile, recheck_unchanged, config_toml, complexity_threshold
|
|
347
|
+
):
|
|
348
|
+
report = []
|
|
349
|
+
manifest = []
|
|
350
|
+
findings = []
|
|
351
|
+
suppressed = []
|
|
352
|
+
policy_diagnostics = []
|
|
353
|
+
counts = {
|
|
354
|
+
"added": 0,
|
|
355
|
+
"changed": 0,
|
|
356
|
+
"unchanged": 0,
|
|
357
|
+
"removed": 0,
|
|
358
|
+
"omitted_unknown": 0,
|
|
359
|
+
}
|
|
360
|
+
checked = 0
|
|
361
|
+
source_omissions = 0
|
|
362
|
+
for path, (meta, source) in sorted(normalized.items()):
|
|
363
|
+
status = _entry_status(path, meta, prior)
|
|
364
|
+
counts[status] += 1
|
|
365
|
+
reviewed, need_review = _review_needed(
|
|
366
|
+
status,
|
|
367
|
+
prior.get(path, {}),
|
|
368
|
+
profile,
|
|
369
|
+
recheck_unchanged,
|
|
370
|
+
)
|
|
371
|
+
outcome, result = _run_entry_review(
|
|
372
|
+
meta,
|
|
373
|
+
source,
|
|
374
|
+
need_review,
|
|
375
|
+
config_toml,
|
|
376
|
+
complexity_threshold,
|
|
377
|
+
)
|
|
378
|
+
if result is not None:
|
|
379
|
+
checked += int(result["syntax_ok"] is not None)
|
|
380
|
+
reviewed = result["syntax_ok"] is not None and not result.get(
|
|
381
|
+
"omitted_findings", 0
|
|
382
|
+
)
|
|
383
|
+
source_omissions += result.get("omitted_findings", 0)
|
|
384
|
+
findings.extend(result["findings"])
|
|
385
|
+
suppressed.extend(result.get("suppressed_findings", []))
|
|
386
|
+
policy_diagnostics.extend(
|
|
387
|
+
dict(x, path=path) for x in result.get("policy_diagnostics", [])
|
|
388
|
+
)
|
|
389
|
+
elif need_review:
|
|
390
|
+
reviewed = False
|
|
391
|
+
manifest.append(
|
|
392
|
+
dict(meta, reviewed=reviewed, review_profile=profile if reviewed else "")
|
|
393
|
+
)
|
|
394
|
+
report.append(_report_entry(meta, status, outcome, result, need_review))
|
|
395
|
+
return (
|
|
396
|
+
report,
|
|
397
|
+
manifest,
|
|
398
|
+
findings,
|
|
399
|
+
suppressed,
|
|
400
|
+
policy_diagnostics,
|
|
401
|
+
counts,
|
|
402
|
+
checked,
|
|
403
|
+
source_omissions,
|
|
404
|
+
)
|
|
405
|
+
|
|
406
|
+
|
|
407
|
+
def review_snapshot(
|
|
408
|
+
entries: list[dict[str, Any]],
|
|
409
|
+
previous_manifest: list[dict[str, Any]] | None = None,
|
|
410
|
+
complete_snapshot: bool = False,
|
|
411
|
+
complexity_threshold: int = 10,
|
|
412
|
+
recheck_unchanged: bool = False,
|
|
413
|
+
previous_findings: list[dict] | None = None,
|
|
414
|
+
config_toml: str | None = None,
|
|
415
|
+
) -> dict:
|
|
416
|
+
_validate_snapshot(
|
|
417
|
+
entries,
|
|
418
|
+
previous_manifest,
|
|
419
|
+
complexity_threshold,
|
|
420
|
+
recheck_unchanged,
|
|
421
|
+
complete_snapshot,
|
|
422
|
+
)
|
|
423
|
+
from .config import parse_policy
|
|
424
|
+
|
|
425
|
+
parse_policy(config_toml, complexity_threshold)
|
|
426
|
+
prior_open = _prior_findings(previous_findings)
|
|
427
|
+
prior = _prior_manifest(previous_manifest)
|
|
428
|
+
normalized = _batch_entries(entries)
|
|
429
|
+
profile = (
|
|
430
|
+
f"0.4.1/t={complexity_threshold}/policy={hash_source(config_toml or '')[:24]}"
|
|
431
|
+
)
|
|
432
|
+
(
|
|
433
|
+
report,
|
|
434
|
+
manifest,
|
|
435
|
+
findings,
|
|
436
|
+
suppressed,
|
|
437
|
+
policy_diagnostics,
|
|
438
|
+
counts,
|
|
439
|
+
checked,
|
|
440
|
+
source_omissions,
|
|
441
|
+
) = _current_entries(
|
|
442
|
+
normalized,
|
|
443
|
+
prior,
|
|
444
|
+
profile,
|
|
445
|
+
recheck_unchanged,
|
|
446
|
+
config_toml,
|
|
447
|
+
complexity_threshold,
|
|
448
|
+
)
|
|
449
|
+
_add_omitted(prior, normalized, complete_snapshot, counts, manifest, report)
|
|
450
|
+
if len(manifest) > MAX_FILES:
|
|
451
|
+
raise InputError("Merged manifest exceeds supported file count")
|
|
452
|
+
all_findings = _ordered_findings(findings)
|
|
453
|
+
open_findings = _open_findings(prior_open, report, all_findings)
|
|
454
|
+
return dict(
|
|
455
|
+
manifest=sorted(manifest, key=lambda x: x["path"]),
|
|
456
|
+
complete_snapshot=complete_snapshot,
|
|
457
|
+
counts=counts,
|
|
458
|
+
checked_files=checked,
|
|
459
|
+
files=sorted(report, key=lambda x: x["path"]),
|
|
460
|
+
findings=all_findings[:MAX_FINDINGS],
|
|
461
|
+
omitted_findings=max(0, len(all_findings) - MAX_FINDINGS) + source_omissions,
|
|
462
|
+
omitted_source_findings=source_omissions,
|
|
463
|
+
suppressed_findings=suppressed[:MAX_FINDINGS],
|
|
464
|
+
omitted_suppressed_findings=max(0, len(suppressed) - MAX_FINDINGS),
|
|
465
|
+
policy_diagnostics=policy_diagnostics[:MAX_FINDINGS],
|
|
466
|
+
omitted_policy_diagnostics=max(0, len(policy_diagnostics) - MAX_FINDINGS),
|
|
467
|
+
open_findings=open_findings[:MAX_FINDINGS],
|
|
468
|
+
omitted_open_findings=max(0, len(open_findings) - MAX_FINDINGS)
|
|
469
|
+
+ source_omissions,
|
|
470
|
+
limitations=[
|
|
471
|
+
"Review stamps are caller-supplied, not independently verified. Digest equality is not proof of correctness.",
|
|
472
|
+
"Omitted paths are unknown unless complete_snapshot=true. Prior open findings are retained only when explicitly supplied.",
|
|
473
|
+
"Only a complete error-free syntax review below the per-file cap replaces prior findings; stale observations are labeled carried_unknown.",
|
|
474
|
+
"Fragments are checked in isolation and unsupported languages have no syntax coverage.",
|
|
475
|
+
"No source contents are retained; callers must retain the manifest for the next review.",
|
|
476
|
+
"IDs are stable for equal code, label, coordinates and message, not across line moves.",
|
|
477
|
+
"No tests run; findings do not establish whole-project correctness.",
|
|
478
|
+
],
|
|
479
|
+
)
|
|
480
|
+
|
|
481
|
+
|
|
482
|
+
def _validate_finding(raw):
|
|
483
|
+
if not isinstance(raw, dict) or set(raw) - {
|
|
484
|
+
"code",
|
|
485
|
+
"severity",
|
|
486
|
+
"evidence",
|
|
487
|
+
"path",
|
|
488
|
+
"line",
|
|
489
|
+
"column",
|
|
490
|
+
"message",
|
|
491
|
+
"recommendation",
|
|
492
|
+
"id",
|
|
493
|
+
"state",
|
|
494
|
+
"change_scope",
|
|
495
|
+
"original_severity",
|
|
496
|
+
"suppression",
|
|
497
|
+
"evidence_source",
|
|
498
|
+
}:
|
|
499
|
+
raise InputError("Invalid supplied finding fields")
|
|
500
|
+
allowed_codes = {
|
|
501
|
+
"TRAILING_WHITESPACE",
|
|
502
|
+
"CHANGE_BUDGET",
|
|
503
|
+
"NO_TEST_PATH",
|
|
504
|
+
"NO_INPUT",
|
|
505
|
+
"PYTHON_SYNTAX",
|
|
506
|
+
"DUPLICATE_DEFINITION",
|
|
507
|
+
"UNREACHABLE_STATEMENT",
|
|
508
|
+
"DUPLICATE_LITERAL_KEY",
|
|
509
|
+
"BRANCH_COMPLEXITY",
|
|
510
|
+
"POSSIBLY_UNUSED_IMPORT",
|
|
511
|
+
"JSON_SYNTAX",
|
|
512
|
+
"JSON_DUPLICATE_KEY",
|
|
513
|
+
"MUTABLE_DEFAULT",
|
|
514
|
+
"BARE_EXCEPT",
|
|
515
|
+
"BUILTIN_SHADOWING",
|
|
516
|
+
"POSSIBLY_UNUSED_LOCAL",
|
|
517
|
+
"JS_TS_SYNTAX",
|
|
518
|
+
"STATIC_SYNTAX",
|
|
519
|
+
"JS_DUPLICATE_LEXICAL",
|
|
520
|
+
"JS_UNREACHABLE_STATEMENT",
|
|
521
|
+
}
|
|
522
|
+
if not isinstance(raw.get("code"), str) or raw["code"] not in allowed_codes:
|
|
523
|
+
raise InputError("Unsupported ordinary review finding code")
|
|
524
|
+
|
|
525
|
+
|
|
526
|
+
def _finding_text(raw):
|
|
527
|
+
result = {}
|
|
528
|
+
for field, limit in (
|
|
529
|
+
("code", 80),
|
|
530
|
+
("path", 1024),
|
|
531
|
+
("message", 2048),
|
|
532
|
+
("recommendation", 2048),
|
|
533
|
+
):
|
|
534
|
+
value = raw.get(field)
|
|
535
|
+
if (
|
|
536
|
+
not isinstance(value, str)
|
|
537
|
+
or len(value) > limit
|
|
538
|
+
or (field != "path" and not value)
|
|
539
|
+
):
|
|
540
|
+
raise InputError(f"Invalid finding {field}")
|
|
541
|
+
if "\x00" in value:
|
|
542
|
+
raise InputError(f"NUL in finding {field}")
|
|
543
|
+
try:
|
|
544
|
+
value.encode("utf-8")
|
|
545
|
+
except UnicodeError as exc:
|
|
546
|
+
raise InputError(f"Invalid UTF-8 finding {field}") from exc
|
|
547
|
+
result[field] = value
|
|
548
|
+
return result
|
|
549
|
+
|
|
550
|
+
|
|
551
|
+
def _finding_classes(raw):
|
|
552
|
+
if (
|
|
553
|
+
not isinstance(raw.get("severity"), str)
|
|
554
|
+
or raw["severity"] not in {"error", "warning", "info"}
|
|
555
|
+
or not isinstance(raw.get("evidence"), str)
|
|
556
|
+
or raw["evidence"] not in {"fact", "heuristic"}
|
|
557
|
+
):
|
|
558
|
+
raise InputError("Invalid finding severity/evidence")
|
|
559
|
+
return {"severity": raw["severity"], "evidence": raw["evidence"]}
|
|
560
|
+
|
|
561
|
+
|
|
562
|
+
def normalize_finding(raw: dict) -> dict:
|
|
563
|
+
_validate_finding(raw)
|
|
564
|
+
result = _finding_text(raw)
|
|
565
|
+
result.update(_finding_classes(raw))
|
|
566
|
+
result["line"] = (
|
|
567
|
+
None if raw.get("line") is None else _integer(raw["line"], "line", 1, 10**9)
|
|
568
|
+
)
|
|
569
|
+
if raw.get("column") is not None:
|
|
570
|
+
result["column"] = _integer(raw["column"], "column", 1, 10**9)
|
|
571
|
+
# Recompute identity; never trust IDs attached by callers.
|
|
572
|
+
return attach_ids([result])[0]
|