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 ADDED
@@ -0,0 +1,3 @@
1
+ """PatchPilot: bounded, incremental review for supplied changes."""
2
+
3
+ __version__ = "0.4.1"
patchpilot/__main__.py ADDED
@@ -0,0 +1,246 @@
1
+ """Explicit stdin review; never discovers files or invokes source-control commands."""
2
+
3
+ import argparse
4
+ import json
5
+ import sys
6
+ from .analysis import MAX_BYTES, InputError, review_diff, review_python
7
+ from .common import no_input
8
+ from .json_review import review_json
9
+ from .js_review import review_javascript
10
+ from .sarif import export_sarif
11
+ from .snapshot import review_snapshot, attach_ids, MAX_BATCH_BYTES
12
+ from .changed import review_source, review_changed
13
+ from .signatures import compare_call_signature
14
+ from .external_sarif import import_external_sarif
15
+ from .compact import compact_report
16
+ from .formatting import suggest_formatting_patch
17
+
18
+
19
+ def _snapshot_unknown(report):
20
+ if not report["files"] and not report.get("complete_snapshot"):
21
+ return True
22
+ return any(
23
+ f["coverage"].startswith("unknown_")
24
+ or f["coverage"] in {"checked_incomplete_findings", "checked_fragment"}
25
+ or f.get("status") == "omitted_unknown"
26
+ for f in report["files"]
27
+ )
28
+
29
+
30
+ def _has_errors(report):
31
+ return any(f["severity"] == "error" for f in report["findings"]) or any(
32
+ f.get("syntax_ok") is False for f in report.get("files", [])
33
+ )
34
+
35
+
36
+ def _omitted(report):
37
+ return any(
38
+ report.get(field, 0)
39
+ for field in (
40
+ "omitted_findings",
41
+ "omitted_open_findings",
42
+ "omitted_policy_diagnostics",
43
+ "omitted_suppressed_findings",
44
+ "omitted_outside_findings",
45
+ )
46
+ )
47
+
48
+
49
+ def _review_exit(report, action):
50
+ if _has_errors(report):
51
+ return 1
52
+ if (
53
+ report.get("syntax_ok", True) is None
54
+ or report.get("coverage_complete", True) is False
55
+ or _omitted(report)
56
+ ):
57
+ return 3
58
+ if action == "snapshot" and _snapshot_unknown(report):
59
+ return 3
60
+ return 0
61
+
62
+
63
+ def _parser():
64
+ parser = argparse.ArgumentParser(prog="patchpilot")
65
+ sub = parser.add_subparsers(dest="action", required=True)
66
+ diff = sub.add_parser("diff", help="Read unified diff from stdin")
67
+ diff.add_argument("--change-budget", type=int, default=300)
68
+ diff.add_argument("--file-budget", type=int, default=10)
69
+ source = sub.add_parser("python", help="Read Python source from stdin")
70
+ source.add_argument("--path", default="snippet.py")
71
+ source.add_argument("--start-line", type=int, default=1)
72
+ source.add_argument("--complexity-threshold", type=int, default=10)
73
+ source.add_argument("--complete-module", action="store_true")
74
+ json_parser = sub.add_parser("json", help="Read JSON document from stdin")
75
+ json_parser.add_argument("--path", default="document.json")
76
+ json_parser.add_argument("--start-line", type=int, default=1)
77
+ js = sub.add_parser(
78
+ "javascript", help="Read JavaScript/JSX/TypeScript/TSX source from stdin"
79
+ )
80
+ js.add_argument("--path", default="snippet.js")
81
+ js.add_argument("--start-line", type=int, default=1)
82
+ js.add_argument(
83
+ "--language",
84
+ choices=["javascript", "jsx", "typescript", "tsx"],
85
+ default="javascript",
86
+ )
87
+ sub.add_parser(
88
+ "sarif",
89
+ help="Convert supplied findings array or report JSON from stdin to SARIF",
90
+ )
91
+ sub.add_parser("snapshot", help="Read snapshot request JSON from stdin")
92
+ for action in [
93
+ "source",
94
+ "changed",
95
+ "signature",
96
+ "import-sarif",
97
+ "compact",
98
+ "formatting",
99
+ ]:
100
+ sub.add_parser(
101
+ action, help="Read an explicit structured JSON request from stdin"
102
+ )
103
+ return parser
104
+
105
+
106
+ def _read_input(action):
107
+ limit = (
108
+ MAX_BATCH_BYTES * 6 + MAX_BYTES
109
+ if action
110
+ in {
111
+ "snapshot",
112
+ "sarif",
113
+ "source",
114
+ "changed",
115
+ "signature",
116
+ "import-sarif",
117
+ "compact",
118
+ "formatting",
119
+ }
120
+ else MAX_BYTES
121
+ )
122
+ raw = sys.stdin.buffer.read(limit + 1)
123
+ if len(raw) > limit:
124
+ raise InputError(f"Input exceeds {limit} UTF-8 bytes")
125
+ try:
126
+ text = raw.decode("utf-8", errors="strict")
127
+ except UnicodeDecodeError as exc:
128
+ raise InputError(
129
+ f"Invalid UTF-8 encoding at byte offset {exc.start}; supply valid UTF-8 text."
130
+ ) from exc
131
+ return raw, text
132
+
133
+
134
+ def _direct_review(args, text):
135
+ if args.action == "diff":
136
+ report = review_diff(text, args.change_budget, args.file_budget)
137
+ elif args.action == "python":
138
+ report = review_python(
139
+ text,
140
+ args.path,
141
+ args.start_line,
142
+ args.complexity_threshold,
143
+ args.complete_module,
144
+ )
145
+ elif args.action == "json":
146
+ report = review_json(text, args.path, args.start_line)
147
+ report["findings"] = attach_ids(report["findings"])
148
+ elif args.action == "javascript":
149
+ report = review_javascript(text, args.path, args.start_line, args.language)
150
+ report["findings"] = attach_ids(report["findings"])
151
+ return report
152
+
153
+
154
+ def _structured_request(action, text):
155
+ request = json.loads(text)
156
+ if not isinstance(request, dict):
157
+ raise InputError("Expected request object")
158
+ required, optional, fn = {
159
+ "source": (
160
+ {"source"},
161
+ {
162
+ "path",
163
+ "language",
164
+ "config_toml",
165
+ "start_line",
166
+ "complete",
167
+ "complexity_threshold",
168
+ },
169
+ review_source,
170
+ ),
171
+ "changed": ({"diff", "entries"}, {"config_toml"}, review_changed),
172
+ "signature": (
173
+ {"call_source", "signature"},
174
+ {"language"},
175
+ compare_call_signature,
176
+ ),
177
+ "import-sarif": ({"sarif_text"}, {"diff"}, import_external_sarif),
178
+ "compact": ({"report"}, {"max_findings"}, compact_report),
179
+ "formatting": ({"diff", "entries"}, set(), suggest_formatting_patch),
180
+ }[action]
181
+ if not required <= request.keys() or set(request) - required - optional:
182
+ raise InputError("Invalid request fields")
183
+ report = fn(**request)
184
+ return report
185
+
186
+
187
+ def _snapshot_request(text):
188
+ request = json.loads(text)
189
+ if (
190
+ not isinstance(request, dict)
191
+ or "entries" not in request
192
+ or set(request)
193
+ - {
194
+ "entries",
195
+ "previous_manifest",
196
+ "complete_snapshot",
197
+ "complexity_threshold",
198
+ "recheck_unchanged",
199
+ "previous_findings",
200
+ "config_toml",
201
+ }
202
+ ):
203
+ raise InputError("Malformed snapshot request")
204
+ return review_snapshot(**request)
205
+
206
+
207
+ def _json_review(action, text):
208
+ if action == "sarif":
209
+ request = json.loads(text)
210
+ if isinstance(request, dict):
211
+ request = request.get("open_findings", request.get("findings"))
212
+ return export_sarif(request), 0
213
+ if action == "snapshot":
214
+ report = _snapshot_request(text)
215
+ return report, _review_exit(report, action)
216
+ report = _structured_request(action, text)
217
+ if action in {"signature", "import-sarif", "compact", "formatting"}:
218
+ return report, int(action == "signature" and report["status"] == "mismatch")
219
+ return report, _review_exit(report, action)
220
+
221
+
222
+ def _execute(args, raw, text):
223
+ if not raw and args.action not in {"python", "json", "javascript", "diff"}:
224
+ report = no_input(getattr(args, "path", ""))
225
+ report["findings"] = attach_ids(report["findings"])
226
+ return report, 3
227
+ if args.action in {"diff", "python", "json", "javascript"}:
228
+ report = _direct_review(args, text)
229
+ return report, _review_exit(report, args.action)
230
+ return _json_review(args.action, text)
231
+
232
+
233
+ def main() -> int:
234
+ args = _parser().parse_args()
235
+ try:
236
+ raw, text = _read_input(args.action)
237
+ report, status = _execute(args, raw, text)
238
+ print(json.dumps(report, indent=2, ensure_ascii=True))
239
+ return status
240
+ except (InputError, UnicodeError, json.JSONDecodeError, RecursionError) as exc:
241
+ print(json.dumps({"error": str(exc)}), file=sys.stderr)
242
+ return 2
243
+
244
+
245
+ if __name__ == "__main__":
246
+ raise SystemExit(main())
patchpilot/analysis.py ADDED
@@ -0,0 +1,20 @@
1
+ """Backward-compatible public interface; implementation lives in focused modules."""
2
+
3
+ from .common import (
4
+ MAX_BYTES as MAX_BYTES,
5
+ MAX_LINES as MAX_LINES,
6
+ MAX_FILES as MAX_FILES,
7
+ MAX_FINDINGS as MAX_FINDINGS,
8
+ MAX_AST_NODES as MAX_AST_NODES,
9
+ InputError as InputError,
10
+ bounded as bounded,
11
+ finding as finding,
12
+ )
13
+ from .diff import (
14
+ ChangedFile as ChangedFile,
15
+ HUNK as HUNK,
16
+ label as label,
17
+ parse_diff as parse_diff,
18
+ review_diff as review_diff,
19
+ )
20
+ from .python_review import review_python as review_python