Coverage for src/governed_harness/checks/risk.py: 100%
168 statements
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-12 16:06 +0000
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-12 16:06 +0000
1"""Risk factors of a ChangeSet diff.
3The harness raises the review depth of a change by what it touches, not by what the agent says it
4touched. These signals are lexical heuristics over added and removed lines: each one names the
5factor, the file and the line so a reviewer can confirm or dismiss it. They are not findings by
6themselves; the caller decides what a factor implies (a stricter gate, a second reviewer).
8Documentation files (``.md``, ``.rst``, ``.adoc``) do not raise the ``authentication`` factor: a
9README that mentions tokens changes no access control.
11A line removed and added again with the same text is not a change: a file rewritten with other line
12endings (CRLF to LF, the diff lists every line) or a line moved within the file raises no factor
13for the lines it already had."""
15from __future__ import annotations
17import re
18from collections import Counter
19from collections.abc import Collection, Iterable, Sequence
20from dataclasses import dataclass
21from pathlib import PurePosixPath
23from .model import DiffFile, DiffLine, is_test_path
25FACTORS = (
26 "newDependency",
27 "authentication",
28 "destructiveMigration",
29 "publicContract",
30 "network",
31 "floatMoney",
32 "sensitiveLogging",
33 "deletedWithoutTests",
34)
35_DELETED_LINES_THRESHOLD = 20
36_DOC_SUFFIXES = (".md", ".rst", ".adoc")
37_SOURCE_SUFFIXES = (
38 ".py",
39 ".js",
40 ".jsx",
41 ".ts",
42 ".tsx",
43 ".mjs",
44 ".cjs",
45 ".go",
46 ".java",
47 ".kt",
48 ".rb",
49 ".rs",
50 ".cs",
51 ".php",
52 ".swift",
53 ".c",
54 ".h",
55 ".cpp",
56 ".hpp",
57 ".scala",
58 ".sql",
59)
62@dataclass(frozen=True)
63class RiskSignal:
64 factor: str
65 message: str
66 path: str | None = None
67 line: int | None = None
70def _normalized(path: str) -> str:
71 return path.replace("\\", "/").removeprefix("./")
74def _code(text: str) -> bool:
75 stripped = text.strip()
76 return bool(stripped) and not stripped.startswith(("#", "//", "--"))
79def _quote(text: str) -> str:
80 value = text.strip()
81 return value if len(value) <= 120 else value[:119] + "…"
84# ----- newDependency -------------------------------------------------------------------------
85_REQUIREMENT = r"[A-Za-z0-9][A-Za-z0-9._-]*(?:\[[^\]]*\])?"
86_PYPROJECT_REQUIREMENT = re.compile(
87 rf"^\s*[\"']{_REQUIREMENT}\s*(?:[<>=!~]=?|===|;|@)[^\"']*[\"']\s*,?\s*(?:#.*)?$"
88)
89_PYPROJECT_INLINE = re.compile(
90 rf"^\s*dependencies\s*=\s*\[\s*[\"']{_REQUIREMENT}"
91 rf"|^\s*[\w-]+\s*=\s*\[\s*[\"']{_REQUIREMENT}\s*(?:[<>=!~]=?|;|@)"
92)
93# The value ends at its last character before the trailing ``\s*,?\s*`` (a comma also stays
94# in the value when another comma follows it); only an all-blank value keeps one blank.
95_KEY_VERSION = re.compile(
96 r"^\s*[\"']?(?P<name>[\w.@/-]+)[\"']?\s*[=:]\s*"
97 r"(?P<value>\S(?:[^\n]*[^\s,])?(?:[^\S\n]*,(?=\s*,))*|[^\S\n](?=\n*$))\s*(?:,\s*)?$"
98)
99_VERSION_VALUE = re.compile(
100 r"^(?:[\"'](?:[\^~<>=*]|\d|latest\b|workspace:|file:|git\+|npm:)"
101 r"|\{[^}]*\b(?:version|git|path|url)\s*=)"
102)
103_NOT_A_DEPENDENCY = frozenset(
104 {
105 "version",
106 "requires-python",
107 "python_requires",
108 "python",
109 "python_version",
110 "python_full_version",
111 "target-version",
112 "minversion",
113 "node",
114 "npm",
115 "yarn",
116 "pnpm",
117 "url",
118 "verify_ssl",
119 "name",
120 }
121)
122_GO_REQUIRE = re.compile(r"^\s*(?:require\s+)?[\w.-]+(?:/[\w.~-]+)+\s+v\d")
125def _dependency_line(name: str, line: DiffLine) -> bool:
126 text = line.text
127 if not _code(text):
128 return False
129 if name == "pyproject.toml":
130 if _PYPROJECT_REQUIREMENT.match(text) or _PYPROJECT_INLINE.match(text):
131 return not text.lstrip().startswith(("requires", "build-backend"))
132 return _poetry_dependency(text)
133 if name.startswith("requirements") and name.endswith((".txt", ".in")):
134 return not text.lstrip().startswith("-")
135 if name in {"package.json", "Pipfile"}:
136 return _poetry_dependency(text)
137 if name == "go.mod":
138 return _GO_REQUIRE.match(text) is not None
139 return False
142def _poetry_dependency(text: str) -> bool:
143 match = _KEY_VERSION.match(text)
144 if not match or text.lstrip().startswith("["):
145 return False
146 return (
147 match.group("name").lower() not in _NOT_A_DEPENDENCY
148 and _VERSION_VALUE.match(match.group("value")) is not None
149 )
152def _new_dependencies(diff: Sequence[DiffFile]) -> Iterable[RiskSignal]:
153 for file in diff:
154 path = _normalized(file.path)
155 name = PurePosixPath(path).name
156 for line in file.added:
157 if _dependency_line(name, line):
158 yield RiskSignal(
159 "newDependency", f"dependency added: {_quote(line.text)}", path, line.number
160 )
163# ----- authentication ------------------------------------------------------------------------
164_AUTH = re.compile(
165 r"auth|login|password|passwd|session|token|permission|role|jwt|oauth|credential",
166 re.IGNORECASE,
167)
170def _authentication(diff: Sequence[DiffFile]) -> Iterable[RiskSignal]:
171 for file in diff:
172 path = _normalized(file.path)
173 if is_test_path(path) or path.lower().endswith(_DOC_SUFFIXES):
174 continue
175 match = _AUTH.search(path)
176 if match:
177 yield RiskSignal(
178 "authentication", f"path mentions {match.group(0).lower()!r}", path, None
179 )
180 continue
181 for line in file.added:
182 match = _AUTH.search(line.text)
183 if match:
184 yield RiskSignal(
185 "authentication",
186 f"added code mentions {match.group(0).lower()!r}: {_quote(line.text)}",
187 path,
188 line.number,
189 )
190 break
193# ----- destructiveMigration ------------------------------------------------------------------
194_DESTRUCTIVE = re.compile(
195 r"\bDROP\s+TABLE\b|\bDROP\s+COLUMN\b|\bALTER\s+TABLE\b.*\bDROP\b|\bTRUNCATE\b"
196 r"|\bop\.drop_table\b|\bop\.drop_column\b",
197 re.IGNORECASE,
198)
199_DELETE_FROM = re.compile(r"\bDELETE\s+FROM\b", re.IGNORECASE)
200_WHERE = re.compile(r"\bWHERE\b", re.IGNORECASE)
203def _destructive_migrations(diff: Sequence[DiffFile]) -> Iterable[RiskSignal]:
204 for file in diff:
205 path = _normalized(file.path)
206 if not (path.lower().endswith(".sql") or "migration" in path.lower()):
207 continue
208 added = file.added
209 for index, line in enumerate(added):
210 following = added[index + 1] if index + 1 < len(added) else None
211 unbounded_delete = (
212 _DELETE_FROM.search(line.text) is not None
213 and not _WHERE.search(line.text)
214 and not (
215 following is not None
216 and not line.text.rstrip().endswith(";")
217 and following.number == line.number + 1
218 and _WHERE.search(following.text)
219 )
220 )
221 if _DESTRUCTIVE.search(line.text) or unbounded_delete:
222 yield RiskSignal(
223 "destructiveMigration",
224 f"destructive statement: {_quote(line.text)}",
225 path,
226 line.number,
227 )
230# ----- publicContract ------------------------------------------------------------------------
231_FUNCTION_DEFINITION = re.compile(r"^\s*(?:async\s+)?def\s+(?P<name>[A-Za-z]\w*)\s*\(")
232_CLASS_DEFINITION = re.compile(r"^\s*(?:export\s+)?class\s+(?P<name>[A-Za-z]\w*)\b")
235def _definitions(lines: Iterable[DiffLine]) -> list[tuple[str, DiffLine]]:
236 found: list[tuple[str, DiffLine]] = []
237 for line in lines:
238 match = _FUNCTION_DEFINITION.match(line.text) or _CLASS_DEFINITION.match(line.text)
239 if match:
240 found.append((match.group("name"), line))
241 return found
244def _public_contract(
245 diff: Sequence[DiffFile], interface_paths: Collection[str]
246) -> Iterable[RiskSignal]:
247 interfaces = {_normalized(path) for path in interface_paths}
248 for file in diff:
249 paths = {_normalized(path) for path in (file.old_path, file.new_path) if path}
250 path = _normalized(file.path)
251 if paths & interfaces:
252 yield RiskSignal("publicContract", "declared interface file changed", path, None)
253 exported = next(
254 (line for line in [*file.removed, *file.added] if "__all__" in line.text), None
255 )
256 if exported is not None:
257 yield RiskSignal(
258 "publicContract",
259 f"__all__ changed: {_quote(exported.text)}",
260 path,
261 exported.number,
262 )
263 if is_test_path(path):
264 continue
265 readded = {name for name, _ in _definitions(file.added)}
266 for name, line in _definitions(file.removed):
267 if name not in readded:
268 yield RiskSignal(
269 "publicContract",
270 f"public definition {name!r} removed: {_quote(line.text)}",
271 path,
272 line.number,
273 )
276# ----- network -------------------------------------------------------------------------------
277_NETWORK = re.compile(
278 r"^\s*(?:import|from)\s+(?:requests|httpx|aiohttp|urllib\.request|http\.client|socket"
279 r"|websockets|grpc)\b"
280 r"|^\s*from\s+(?:urllib\s+import\s+request|http\s+import\s+client)\b"
281 r"|(?<![\w.])fetch\(|\baxios\b"
282)
285def _network(diff: Sequence[DiffFile]) -> Iterable[RiskSignal]:
286 for file in diff:
287 path = _normalized(file.path)
288 if is_test_path(path):
289 continue
290 for line in file.added:
291 if _code(line.text) and _NETWORK.search(line.text):
292 yield RiskSignal(
293 "network", f"network access added: {_quote(line.text)}", path, line.number
294 )
297# ----- floatMoney ----------------------------------------------------------------------------
298_MONEY = re.compile(
299 r"price|amount|total|fare|cost|balance|fee|subtotal|money|charge|payment", re.IGNORECASE
300)
301_FLOAT = re.compile(r"\bfloat\(|:\s*float\b|->\s*float\b")
304def _float_money(diff: Sequence[DiffFile]) -> Iterable[RiskSignal]:
305 for file in diff:
306 path = _normalized(file.path)
307 if is_test_path(path):
308 continue
309 for line in file.added:
310 if _code(line.text) and _MONEY.search(line.text) and _FLOAT.search(line.text):
311 yield RiskSignal(
312 "floatMoney",
313 f"money handled as float: {_quote(line.text)}",
314 path,
315 line.number,
316 )
319# ----- sensitiveLogging ----------------------------------------------------------------------
320_LOG_CALL = re.compile(
321 r"\b(?:log|logger|logging|_log|_logger|LOG|LOGGER)\.\w+\(|(?<![\w.])print\("
322 r"|\bconsole\.(?:log|info|warn|error|debug)\("
323)
324_SENSITIVE = re.compile(r"password|passwd|secret|token|card|cvc|cvv|ssn|api_key", re.IGNORECASE)
327def _sensitive_logging(diff: Sequence[DiffFile]) -> Iterable[RiskSignal]:
328 for file in diff:
329 path = _normalized(file.path)
330 if is_test_path(path):
331 continue
332 for line in file.added:
333 call = _LOG_CALL.search(line.text)
334 if call and _code(line.text) and _SENSITIVE.search(line.text, call.end()):
335 yield RiskSignal(
336 "sensitiveLogging",
337 f"sensitive data may be logged: {_quote(line.text)}",
338 path,
339 line.number,
340 )
343# ----- deletedWithoutTests -------------------------------------------------------------------
344def _deleted_without_tests(diff: Sequence[DiffFile]) -> Iterable[RiskSignal]:
345 if any(is_test_path(_normalized(path)) for file in diff for path in _paths(file)):
346 return
347 removed = sum(
348 1
349 for file in diff
350 if file.path.lower().endswith(_SOURCE_SUFFIXES) and not is_test_path(file.path)
351 for line in file.removed
352 if line.text.strip()
353 )
354 if removed >= _DELETED_LINES_THRESHOLD:
355 yield RiskSignal(
356 "deletedWithoutTests",
357 f"{removed} non-test source lines removed and no test file changed",
358 )
361def _net(file: DiffFile) -> DiffFile:
362 """``file`` without the lines it removes and adds again with the same text."""
363 common = Counter(line.text for line in file.added) & Counter(line.text for line in file.removed)
364 if not common:
365 return file
367 def kept(lines: list[DiffLine]) -> list[DiffLine]:
368 left = Counter(common)
369 result: list[DiffLine] = []
370 for line in lines:
371 if left[line.text]:
372 left[line.text] -= 1
373 else:
374 result.append(line)
375 return result
377 return DiffFile(file.old_path, file.new_path, kept(file.added), kept(file.removed))
380def _paths(file: DiffFile) -> list[str]:
381 return [path for path in (file.old_path, file.new_path) if path]
384def detect_risk_factors(
385 diff: Sequence[DiffFile], *, interface_paths: Collection[str] = ()
386) -> list[RiskSignal]:
387 """Every risk signal of ``diff``, ordered by factor (the order of ``FACTORS``), then path,
388 then line."""
389 diff = [_net(file) for file in diff]
390 signals = [
391 *_new_dependencies(diff),
392 *_authentication(diff),
393 *_destructive_migrations(diff),
394 *_public_contract(diff, interface_paths),
395 *_network(diff),
396 *_float_money(diff),
397 *_sensitive_logging(diff),
398 *_deleted_without_tests(diff),
399 ]
400 return sorted(
401 signals,
402 key=lambda signal: (
403 FACTORS.index(signal.factor),
404 signal.path or "",
405 signal.line or 0,
406 ),
407 )