-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsafe_pr_gate.py
More file actions
179 lines (148 loc) · 5.4 KB
/
Copy pathsafe_pr_gate.py
File metadata and controls
179 lines (148 loc) · 5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
"""Deterministic local safety gate for agent-assisted PR workflows."""
from __future__ import annotations
import argparse
import json
import subprocess
import sys
from dataclasses import dataclass
from pathlib import Path
from typing import Any
REPO_ROOT = Path(__file__).resolve().parents[1]
if str(REPO_ROOT) not in sys.path:
sys.path.insert(0, str(REPO_ROOT))
@dataclass(frozen=True, slots=True)
class GateState:
branch: str
status_short: tuple[str, ...]
changed_paths: tuple[str, ...]
@dataclass(frozen=True, slots=True)
class GateResult:
ok: bool
branch: str
allow_dirty: bool
allowed_prefixes: tuple[str, ...]
status_short: tuple[str, ...]
changed_paths: tuple[str, ...]
problems: tuple[str, ...]
def to_dict(self) -> dict[str, Any]:
return {
"allowed_prefixes": list(self.allowed_prefixes),
"allow_dirty": self.allow_dirty,
"branch": self.branch,
"changed_paths": list(self.changed_paths),
"ok": self.ok,
"problems": list(self.problems),
"result": "PASS" if self.ok else "FAIL",
"status_short": list(self.status_short),
}
def _run_git(args: list[str]) -> str:
try:
completed = subprocess.run(
["git", *args],
cwd=REPO_ROOT,
check=True,
capture_output=True,
text=True,
)
except FileNotFoundError as exc:
raise RuntimeError(f"git executable not found while running: git {' '.join(args)}") from exc
except subprocess.CalledProcessError as exc:
raise RuntimeError(
f"git command failed with exit code {exc.returncode}: git {' '.join(args)}"
) from exc
return completed.stdout
def _parse_porcelain_paths(output: str) -> tuple[str, ...]:
if not output:
return ()
entries = output.split("\0")
paths: list[str] = []
index = 0
while index < len(entries):
entry = entries[index]
if not entry:
index += 1
continue
path = entry[3:]
status = entry[:2]
if status and any(char in {"R", "C"} for char in status):
if index + 1 < len(entries) and entries[index + 1]:
paths.append(entries[index + 1])
index += 2
continue
paths.append(path)
index += 1
return tuple(paths)
def collect_gate_state() -> GateState:
branch = _run_git(["branch", "--show-current"]).strip()
status_short_output = _run_git(["status", "--short", "--untracked-files=all"])
porcelain_output = _run_git(["status", "--porcelain=v1", "-z"])
status_short = tuple(line for line in status_short_output.splitlines() if line)
changed_paths = _parse_porcelain_paths(porcelain_output)
return GateState(branch=branch, status_short=status_short, changed_paths=changed_paths)
def _path_in_prefix(path: str, prefix: str) -> bool:
normalized = prefix.rstrip("/")
return path == normalized or path.startswith(normalized + "/")
def evaluate_gate(
state: GateState,
*,
allow_dirty: bool = False,
allowed_prefixes: tuple[str, ...] = (),
) -> GateResult:
problems: list[str] = []
if state.branch == "main":
problems.append("on_main_branch")
if state.status_short and not allow_dirty:
problems.append("dirty_working_tree")
if allowed_prefixes:
disallowed_paths = sorted(
path
for path in state.changed_paths
if not any(_path_in_prefix(path, prefix) for prefix in allowed_prefixes)
)
if disallowed_paths:
problems.append("changed_files_outside_allowed_prefixes")
problems.extend(f"outside_prefix:{path}" for path in disallowed_paths)
return GateResult(
ok=not problems,
branch=state.branch,
allow_dirty=allow_dirty,
allowed_prefixes=tuple(sorted(allowed_prefixes)),
status_short=state.status_short,
changed_paths=state.changed_paths,
problems=tuple(problems),
)
def _parse_args(argv: list[str]) -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Run a deterministic local safety gate for PR-ready agent work.")
parser.add_argument("--allow-dirty", action="store_true", help="Permit a dirty working tree while still checking allowed prefixes.")
parser.add_argument(
"--allowed-prefix",
action="append",
default=[],
help="Require changed paths to stay within this repo-relative prefix. May be repeated.",
)
return parser.parse_args(argv)
def _error_response(exc: RuntimeError) -> dict[str, Any]:
return {
"error": {
"message": str(exc),
"type": exc.__class__.__name__,
},
"ok": False,
"result": "ERROR",
}
def main(argv: list[str] | None = None) -> int:
args = _parse_args(sys.argv[1:] if argv is None else argv)
try:
state = collect_gate_state()
result = evaluate_gate(
state,
allow_dirty=args.allow_dirty,
allowed_prefixes=tuple(args.allowed_prefix),
)
sys.stdout.write(json.dumps(result.to_dict(), indent=2, sort_keys=True) + "\n")
return 0 if result.ok else 1
except RuntimeError as exc:
sys.stdout.write(json.dumps(_error_response(exc), indent=2, sort_keys=True) + "\n")
return 1
if __name__ == "__main__":
raise SystemExit(main())